Difference between revisions of "C-mode"

From WikEmacs
Jump to navigation Jump to search
(Created page with "'''c-mode''' is the default major mode for editing C source files ({{File|.c}} and {{File|.h}} files). It uses {{ModeLink|cc-mode}}. <!-----DONT REMOVE ANYTHING FROM ...")
 
Line 28: Line 28:
  
 
== Change default indenting style ==
 
== Change default indenting style ==
 +
=== Style ==
 +
Any mode based on {{ModeLink|cc-mode}} provides automatic indentation. The default style is <code>gnu</code>. It is the one recommended by the Free Software Foundation for GNU projects but you can use a different one.
 +
 +
Here is a basic list of predefined styles:
 +
; <code>gnu</code>
 +
: The default style for GNU projects
 +
; <code>k&r</code>
 +
: What Kernighan and Ritchie, the authors of C used in their book
 +
; <code>bsd</code>
 +
: What BSD developers use, aka “Allman style” after Eric Allman.
 +
; <code>whitesmith</code>
 +
: Popularized by the examples that came with Whitesmiths C, an early commercial C compiler.
 +
; <code>stroustrup</code>
 +
: What Stroustrup, the author of C++ used in his book
 +
; <code>ellemtel</code>
 +
: Popular C++ coding standards as defined by “Programming in C++, Rules and Recommendations,” Erik Nyquist and Mats Henricson, Ellemtel
 +
; <code>linux</code>
 +
: What the Linux developers use for kernel development
 +
; <code>python</code>
 +
: What Python developers use for extension modules
 +
; <code>java</code>
 +
: The default style for java-mode (see below)
 +
; <code>user</code>
 +
: When you want to define your own style
 +
 +
You can look the [http://cc-mode.sourceforge.net/html-manual/Built_002din-Styles.html#Built_002din-Styles cc-mode documentation] for a complete list of builtin style.
 +
For visual example of each style you can look the [http://en.wikipedia.org/wiki/Indent_style Wikipedia article on indenting style].
 +
 +
* The default style is set via the variable {{Var|c-default-style}}
 +
* Use {{KeyCommand|C-c .|c-set-style}} to change the indentation style of the current buffer.
 +
* The amount of space use to indent is set via the variable {{Var|c-basic-offset}}.
 +
 +
To set the default style to K&R and use 4 spaces, add this to your {{EmacsConfigFile}}:
 +
 
<source lang="lisp">
 
<source lang="lisp">
 
; set k&r style
 
; set k&r style
(setq c-set-style "k&r")
+
(setq c-default-style "k&r")
 
; indent with 4 spaces
 
; indent with 4 spaces
 
(setq c-basic-offset 4)
 
(setq c-basic-offset 4)

Revision as of 12:19, 31 March 2012

c-mode is the default major mode for editing C source files (.c and .h files). It uses cc-mode.



Basic setup

c-mode is bundled with Emacs by default. No additional setup is required.

Helpful keybindings

[C-M-a]
Go to the beginning of the current function. If you are between 2 functions, go the start of the previous one.
[C-M-e]
Go to the end of the current function. If you are between 2 functions, go to the end of the next one.

Common Customization

Change default indenting style

= Style

Any mode based on cc-mode provides automatic indentation. The default style is gnu. It is the one recommended by the Free Software Foundation for GNU projects but you can use a different one.

Here is a basic list of predefined styles:

gnu
The default style for GNU projects
k&r
What Kernighan and Ritchie, the authors of C used in their book
bsd
What BSD developers use, aka “Allman style” after Eric Allman.
whitesmith
Popularized by the examples that came with Whitesmiths C, an early commercial C compiler.
stroustrup
What Stroustrup, the author of C++ used in his book
ellemtel
Popular C++ coding standards as defined by “Programming in C++, Rules and Recommendations,” Erik Nyquist and Mats Henricson, Ellemtel
linux
What the Linux developers use for kernel development
python
What Python developers use for extension modules
java
The default style for java-mode (see below)
user
When you want to define your own style

You can look the cc-mode documentation for a complete list of builtin style. For visual example of each style you can look the Wikipedia article on indenting style.

To set the default style to K&R and use 4 spaces, add this to your Emacs configuration file:

; set k&r style
(setq c-default-style "k&r")
; indent with 4 spaces
(setq c-basic-offset 4)


GNU/Linux

OS X

Windows

Emacs-24

Emacs-23

Recommended Reading

Project Pages


Tutorial Pages