I am a user of Aquamacs under OS X, which by default does not recognize .m files as Objective-C, but instead treats them as matlab files - from the mailing list, the reason is that it was felt that he wanted a clean way to distinguish Obj-C files from others before he added the solution to the distribution.
Thus, I tried to come up with a suitable magic mode-alist, which will help to correctly identify Objective-C files and switch in the correct mode - after some thoughts it seemed that all I wanted to check was one of three keywords:
@implementation @interface @protocol
Any Objective-C file must have one of these lines in it, and they also most likely will not get into the Matlab file.
Then I tried to come up with a magic mode option to recognize @implementation , my first attempt:
(add-to-list 'magic-mode-alist '(".*^\@implementation.*" . objc-mode))
Basically, check the possibility of @implementation anywhere in the file at the beginning of the line.
Putting this in my .emacs file, it seemed to have no effect - opening the .m file with @implementation in it did not switch to Obj-C mode.
Does anyone know what could be with my magic style record? The only examples I could find were consistent text at the very beginning of the file (for nxml-mode), and not for the lines in the middle of the file. Something seems to be wrong with my regular expression, but as noted, several variations of the same theme seemed to have no effect.
I have work entries in my .emacs file for auto-mode-alist to pull the correct mode today, so I donโt need any advice on how to do this, I would like to solve this problem the โrightโ way.
EDIT:
Thanks to the accepted answer below, here is the last code I have in the .Emacs file:
(setq magic-mode-alist (append (list '("\\(.\\|\n\\)*\ n@implementation " . objc-mode) '("\\(.\\|\n\\)*\ n@interface " . objc-mode) '("\\(.\\|\n\\)*\ n@protocol " . objc-mode)) magic-mode-alist))
Please note that I use "\ n" as a replacement for "^" (beginning of line) in the regular expression, since it is important to determine the type precisely - otherwise the .emacs file will appear in obj-c mode simply because there was @implementation.
Something else to know that I came across research is that magic-mode-alist only gets a certain number of characters at the beginning of the file to work (defined by magic-mode-regexp-match-limit , by default 4000 characters), presumably for performance reasons. This is usually more than enough to find the beginning of the @implementation or @interface , but if you insert editorial comments in the file headers, as some people do, you may need to significantly increase this. Just like backing up, I plan to also include code that always opens .m files always in obj mode using auto-mode-alist, since my Matlab days are far behind me, but hopefully this change in magic mode can go into the official distribution of Aquamacs at least if not Emacs 23 (regular expressions probably need to be increased a bit more for this to happen).