objective-cemacs

Distinguishing Objective C *.h files from those of other C dialects in Emacs


I'm starting to use objective C, and objective C mode works perfectly fine. However, ObjC uses .h files, just like C and C++. I need to be able to use all three. Does anyone know how to get emacs to tell if it should be in objC mode or C/C++ mode?

EDIT: There seems to be some confusion on what I'm asking for. The problem is that I have some .h files that are associated with .m files, and some .h files that are associated with .c files and some that are associated with .cpp files. What I WANT is something that I can stick in my c-mode-common-hook or somewhere that will check to see if it's an objective C .h file, and then force it to objective C-mode, or something. The idea being that then I can just open a .h file and it will automatically be in the correct mode.

Obviously I can manually change mode when I'm in the file, but that's a pain, and I often forget until I hit tab and something wacky happens. This is the solution I'm using now.

I think that the header comment is probably the right way to do this, but in order for it to be TRULY useful, I need to figure out how to get XCode to put the comment in there when It's creating the file for me...

EDIT2:

I'm currently using the header-comment solution. Until I can figure out how to make XCode automatically add the comment, I'm using the following elisp function:

(defun bp-add-objC-comment ()
  "Adds the /* -*- mode: objc -*- */ line at the top of the file"
  (interactive)
  (objc-mode)
  (let((p (point)))
    (goto-char 0)
    (insert "/* -*- mode: objc -*- */\n")
    (goto-char (+ p  (length "/* -*- mode: objc -*- */\n")))))

Solution

  • You can put a comment like this in the first line of the file:

    /* -*- mode: objc -*- */
    

    or

    // -*- mode: c++ -*-
    

    as appropriate. More details in Specifying File Variables in the Emacs manual.