LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How do you change default emacs behavior on .h files? (http://www.linuxquestions.org/questions/programming-9/how-do-you-change-default-emacs-behavior-on-h-files-623760/)

BrianK 02-25-2008 01:33 PM

How do you change default emacs behavior on .h files?
 
I almost always program in C++ vs C.

I typically use ".h" for header files.

emacs sees .h files as C files rather than C++ files, and therefore does syntax highlighting and indenting based on C rather than C++.

How can I change the default to C++? This is a fairly vanilla install of emacs & I don't have admin privs on this machine. Can I change my local elisp file to change this behavior? Maybe I need to start using a .emacs? Maybe it's simpler than that?

Thanks

Hko 02-25-2008 04:43 PM

I know too little of elisp to provide a real solution, but renaming header files to *.hpp makes emacs think it is C++.

BrianK 02-25-2008 06:36 PM

Quote:

Originally Posted by Hko (Post 3069555)
I know too little of elisp to provide a real solution, but renaming header files to *.hpp makes emacs think it is C++.

yeah, I know. I just prefer .h over .hpp.

ntubski 02-26-2008 06:59 PM

put this in .emacs file
Code:

(setq auto-mode-alist (cons '("\\.h\\'" . c++-mode) auto-mode-alist))
or (this loads cl.el which can increase startup time)
Code:

(push '("\\.h\\'" . c++-mode) auto-mode-alist)
Or you can put
Code:

// -*- mode: c++ -*-
as the first line of the .h file.

Or you can put
Code:

/*                                                                                                                                                                                                                 
*** Local Variables: **                                                                                                                                                                                           
*** mode: c++ **                                                                                                                                                                                             
*** End: **                                                                                                                                                                                                       
 */

At the end of the file.

Quote:

Can I change my local elisp file to change this behavior? Maybe I need to start using a .emacs?
A .emacs is a local elisp file, not sure what you mean here?

BrianK 02-26-2008 07:11 PM

Quote:

Originally Posted by ntubski (Post 3070870)
put this in .emacs file
Code:

(setq auto-mode-alist (cons '("\\.h\\'" . c++-mode) auto-mode-alist))
or (this loads cl.el which can increase startup time)
Code:

(push '("\\.h\\'" . c++-mode) auto-mode-alist)
Or you can put
Code:

// -*- mode: c++ -*-
as the first line of the .h file.

Or you can put
Code:

/*                                                                                                                                                                                                                 
*** Local Variables: **                                                                                                                                                                                           
*** mode: c++ **                                                                                                                                                                                             
*** End: **                                                                                                                                                                                                       
 */

At the end of the file.

awesome, these are all great options.

Quote:

A .emacs is a local elisp file, not sure what you mean here?
I was confused. For some reason, I didn't think I'd created a .emacs file on this machine, but I had (and have put things in it... how soon we forget).

Thanks!


All times are GMT -5. The time now is 08:54 AM.