Go to the first, previous, next, last section, table of contents.


Getting Connected

`cc-mode.el' works well with the 2 main branches of Emacs 19: XEmacs and the Emacs 19 maintained by the FSF. Emacs 19 users will want to use Emacs version 19.21 or better, XEmacs users will want 19.6 or better. Earlier versions of these Emacsen have deficiencies and/or bugs which will adversely affect the performance and usability of cc-mode.

Similarly if you use the `cc-mode-18.el' compatibility file, `cc-mode.el' will work with Emacs 18, but only moderately well. A word of warning though, Emacs 18 lacks some fundamental functionality and that ultimately means using Emacs 18 is a losing battle. Hence cc-mode under Emacs 18 is no longer supported and it is highly recommended that you upgrade to Emacs 19. If you use cc-mode under Emacs 18, you're on your own. With cc-mode version 5, Emacs 18 support will be dropped altogether.

Note that as of XEmacs 19.13 and Emacs 19.30, your Emacs already comes with cc-mode version 4 preconfigured for your use. You should be able to safely skip the rest of the setup information in this chapter.

The first thing you will want to do is put `cc-mode.el' somewhere on your load-path so Emacs can find it. Do a C-h v load-path RET to see all the directories Emacs looks at when loading a file. If none of these directories are appropriate, create a new directory and add it to your load-path:

[in the shell]


% cd
% mkdir mylisp
% mv cc-mode.el mylisp
% cd mylisp

[in your .emacs file add]


(setq load-path (cons "~/mylisp" load-path))

Next you want to byte compile `cc-mode.el'. The mode uses a lot of macros so if you don't byte compile it, things will be unbearably slow. You can ignore all byte-compiler warnings! They are the result of the supporting different versions of Emacs, and none of the warnings have any effect on operation. Let me say this again: You really can ignore all byte-compiler warnings!

Here's what to do to byte-compile the file [in emacs]:


M-x byte-compile-file RET ~/mylisp/cc-mode.el RET

If you are running a version of Emacs or XEmacs that comes with cc-mode by default, you can simply add the following to your `.emacs' file in order to upgrade to the latest version of cc-mode:


(load "cc-mode")

Users of even older versions of Emacs 19, Emacs 18, or of the older Lucid Emacs will probably be running an Emacs that has BOCM `c-mode.el' and possible `c++-mode.el' pre-dumped. If your Emacs is dumped with either of these files you first need to make Emacs "forget" about those older modes.

If you can do a C-h v c-mode-map RET without getting an error, you need to add these lines at the top of your `.emacs' file:


(fmakunbound 'c-mode)
(makunbound  'c-mode-map)
(fmakunbound 'c++-mode)
(makunbound  'c++-mode-map)
(makunbound  'c-style-alist)

After those lines you will want to add the following autoloads to your `.emacs' file so that cc-mode gets loaded at the right time:


(autoload 'c++-mode  "cc-mode" "C++ Editing Mode" t)
(autoload 'c-mode    "cc-mode" "C Editing Mode" t)
(autoload 'objc-mode "cc-mode" "Objective-C Editing Mode" t)
(autoload 'java-mode "cc-mode" "Java Editing Mode" t)

Alternatively, if you want to make sure cc-mode is loaded when Emacs starts up, you could use this line instead of the three autoloads above:


(require 'cc-mode)

Next, you will want to set up Emacs so that it edits C files in c-mode, C++ files in c++-mode, and Objective-C files in objc-mode. All users should add the following to their `.emacs' file. Note that this assumes you'll be editing .h and .c files as C, .hh, .cc, .H, and .C files as C++, .m files as Objective-C, and .java files as Java code. YMMV:


(setq auto-mode-alist
  (append
    '(("\\.C$"    . c++-mode)
      ("\\.H$"    . c++-mode)
      ("\\.cc$"   . c++-mode)
      ("\\.hh$"   . c++-mode)
      ("\\.c$"    . c-mode)
      ("\\.h$"    . c-mode)
      ("\\.m$"    . objc-mode)
      ("\\.java$" . java-mode)
     ) auto-mode-alist))

You may already have some or all of these settings on your auto-mode-alist, but it won't hurt to put them on there again.

That's all you need -- I know, I know, it sounds like a lot :-), but after you've done all this, you should only need to quit and restart Emacs. The next time you visit a C, C++, Objective-C, or Java file you should be using cc-mode. You can check this easily by hitting M-x c-version RET in the c-mode, c++-mode, or objc-mode buffer. You should see this message in the echo area:


Using cc-mode version 4.xxx

Where xxx is the latest release minor number.


Go to the first, previous, next, last section, table of contents.