| 1 |
This is ../info/ccmode, produced by makeinfo version 4.2 from |
|---|
| 2 |
cc-mode.texi. |
|---|
| 3 |
|
|---|
| 4 |
INFO-DIR-SECTION Emacs |
|---|
| 5 |
START-INFO-DIR-ENTRY |
|---|
| 6 |
* CC Mode: (ccmode). Emacs mode for editing C, C++, Objective-C, |
|---|
| 7 |
Java, Pike, and IDL code. |
|---|
| 8 |
END-INFO-DIR-ENTRY |
|---|
| 9 |
|
|---|
| 10 |
Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001 Free Software |
|---|
| 11 |
Foundation, Inc. |
|---|
| 12 |
|
|---|
| 13 |
Permission is granted to copy, distribute and/or modify this document |
|---|
| 14 |
under the terms of the GNU Free Documentation License, Version 1.1 or |
|---|
| 15 |
any later version published by the Free Software Foundation; with the |
|---|
| 16 |
Invariant Sections being "The GNU Manifesto", "Distribution" and "GNU |
|---|
| 17 |
GENERAL PUBLIC LICENSE", with the Front-Cover texts being "A GNU |
|---|
| 18 |
Manual", and with the Back-Cover Texts as in (a) below. A copy of the |
|---|
| 19 |
license is included in the section entitled "GNU Free Documentation |
|---|
| 20 |
License" in the Emacs manual. |
|---|
| 21 |
|
|---|
| 22 |
(a) The FSF's Back-Cover Text is: "You have freedom to copy and |
|---|
| 23 |
modify this GNU Manual, like GNU software. Copies published by the Free |
|---|
| 24 |
Software Foundation raise funds for GNU development." |
|---|
| 25 |
|
|---|
| 26 |
This document is part of a collection distributed under the GNU Free |
|---|
| 27 |
Documentation License. If you want to distribute this document |
|---|
| 28 |
separately from the collection, you can do so by adding a copy of the |
|---|
| 29 |
license to the document, as described in section 6 of the license. |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
File: ccmode, Node: Movement Commands, Next: Other Commands, Prev: Indentation Commands, Up: Commands |
|---|
| 33 |
|
|---|
| 34 |
Movement Commands |
|---|
| 35 |
================= |
|---|
| 36 |
|
|---|
| 37 |
CC Mode contains some useful command for moving around in C code. |
|---|
| 38 |
|
|---|
| 39 |
`M-x c-beginning-of-defun' |
|---|
| 40 |
Moves point back to the least-enclosing brace. This function is |
|---|
| 41 |
analogous to the Emacs built-in command `beginning-of-defun', |
|---|
| 42 |
except it eliminates the constraint that the top-level opening |
|---|
| 43 |
brace must be in column zero. See `beginning-of-defun' for more |
|---|
| 44 |
information. |
|---|
| 45 |
|
|---|
| 46 |
Depending on the coding style being used, you might prefer |
|---|
| 47 |
`c-beginning-of-defun' to `beginning-of-defun'. If so, consider |
|---|
| 48 |
binding `C-M-a' to the former instead. For backwards |
|---|
| 49 |
compatibility reasons, the default binding remains in effect. |
|---|
| 50 |
|
|---|
| 51 |
`M-x c-end-of-defun' |
|---|
| 52 |
Moves point to the end of the current top-level definition. This |
|---|
| 53 |
function is analogous to the Emacs built-in command `end-of-defun', |
|---|
| 54 |
except it eliminates the constraint that the top-level opening |
|---|
| 55 |
brace of the defun must be in column zero. See |
|---|
| 56 |
`beginning-of-defun' for more information. |
|---|
| 57 |
|
|---|
| 58 |
Depending on the coding style being used, you might prefer |
|---|
| 59 |
`c-end-of-defun' to `end-of-defun'. If so, consider binding |
|---|
| 60 |
`C-M-e' to the former instead. For backwards compatibility |
|---|
| 61 |
reasons, the default binding remains in effect. |
|---|
| 62 |
|
|---|
| 63 |
`C-c C-u' (`c-up-conditional') |
|---|
| 64 |
Move point back to the containing preprocessor conditional, |
|---|
| 65 |
leaving the mark behind. A prefix argument acts as a repeat |
|---|
| 66 |
count. With a negative argument, move point forward to the end of |
|---|
| 67 |
the containing preprocessor conditional. |
|---|
| 68 |
|
|---|
| 69 |
`#elif' is treated like `#else' followed by `#if', so the function |
|---|
| 70 |
stops at them when going backward, but not when going forward. |
|---|
| 71 |
|
|---|
| 72 |
`M-x c-up-conditional-with-else' |
|---|
| 73 |
A variety of `c-up-conditional' that also stops at `#else' lines. |
|---|
| 74 |
Normally those lines are ignored. |
|---|
| 75 |
|
|---|
| 76 |
`M-x c-down-conditional' |
|---|
| 77 |
Move point forward into the next nested preprocessor conditional, |
|---|
| 78 |
leaving the mark behind. A prefix argument acts as a repeat count. |
|---|
| 79 |
With a negative argument, move point backward into the previous |
|---|
| 80 |
nested preprocessor conditional. |
|---|
| 81 |
|
|---|
| 82 |
`#elif' is treated like `#else' followed by `#if', so the function |
|---|
| 83 |
stops at them when going forward, but not when going backward. |
|---|
| 84 |
|
|---|
| 85 |
`M-x c-down-conditional-with-else' |
|---|
| 86 |
A variety of `c-down-conditional' that also stops at `#else' |
|---|
| 87 |
lines. Normally those lines are ignored. |
|---|
| 88 |
|
|---|
| 89 |
`C-c C-p' (`c-backward-conditional') |
|---|
| 90 |
Move point back over a preprocessor conditional, leaving the mark |
|---|
| 91 |
behind. A prefix argument acts as a repeat count. With a negative |
|---|
| 92 |
argument, move forward. |
|---|
| 93 |
|
|---|
| 94 |
`C-c C-n' (`c-forward-conditional') |
|---|
| 95 |
Move point forward across a preprocessor conditional, leaving the |
|---|
| 96 |
mark behind. A prefix argument acts as a repeat count. With a |
|---|
| 97 |
negative argument, move backward. |
|---|
| 98 |
|
|---|
| 99 |
`M-a' (`c-beginning-of-statement') |
|---|
| 100 |
Move point to the beginning of the innermost C statement. If |
|---|
| 101 |
point is already at the beginning of a statement, move to the |
|---|
| 102 |
beginning of the closest preceding statement, even if that means |
|---|
| 103 |
moving into a block (you can use `M-C-b' to move over a balanced |
|---|
| 104 |
block). With prefix argument N, move back N - 1 statements. |
|---|
| 105 |
|
|---|
| 106 |
If point is within or next to a comment or a string which spans |
|---|
| 107 |
more than one line, this command moves by sentences instead of |
|---|
| 108 |
statements. |
|---|
| 109 |
|
|---|
| 110 |
When called from a program, this function takes three optional |
|---|
| 111 |
arguments: the repetition count, a buffer position limit which is |
|---|
| 112 |
the farthest back to search for the syntactic context, and a flag |
|---|
| 113 |
saying whether to do sentence motion in or near comments and |
|---|
| 114 |
multiline strings. |
|---|
| 115 |
|
|---|
| 116 |
`M-e' (`c-end-of-statement') |
|---|
| 117 |
Move point to the end of the innermost C statement. If point is |
|---|
| 118 |
at the end of a statement, move to the end of the next statement, |
|---|
| 119 |
even if it's inside a nested block (use `M-C-f' to move to the |
|---|
| 120 |
other side of the block). With prefix argument N, move forward N |
|---|
| 121 |
- 1 statements. |
|---|
| 122 |
|
|---|
| 123 |
If point is within or next to a comment or a string which spans |
|---|
| 124 |
more than one line, this command moves by sentences instead of |
|---|
| 125 |
statements. |
|---|
| 126 |
|
|---|
| 127 |
When called from a program, this function takes three optional |
|---|
| 128 |
arguments: the repetition count, a buffer position limit which is |
|---|
| 129 |
the farthest back to search for the syntactic context, and a flag |
|---|
| 130 |
saying whether to do sentence motion in or near comments and |
|---|
| 131 |
multiline strings. |
|---|
| 132 |
|
|---|
| 133 |
`M-x c-forward-into-nomenclature' |
|---|
| 134 |
A popular programming style, especially for object-oriented |
|---|
| 135 |
languages such as C++ is to write symbols in a mixed case format, |
|---|
| 136 |
where the first letter of each word is capitalized, and not |
|---|
| 137 |
separated by underscores. E.g. |
|---|
| 138 |
`SymbolsWithMixedCaseAndNoUnderlines'. |
|---|
| 139 |
|
|---|
| 140 |
This command moves point forward to next capitalized word. With |
|---|
| 141 |
prefix argument N, move N times. |
|---|
| 142 |
|
|---|
| 143 |
`M-x c-backward-into-nomenclature' |
|---|
| 144 |
Move point backward to beginning of the next capitalized word. |
|---|
| 145 |
With prefix argument N, move N times. If N is negative, move |
|---|
| 146 |
forward. |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
File: ccmode, Node: Other Commands, Prev: Movement Commands, Up: Commands |
|---|
| 150 |
|
|---|
| 151 |
Other Commands |
|---|
| 152 |
============== |
|---|
| 153 |
|
|---|
| 154 |
CC Mode contains a few other useful commands: |
|---|
| 155 |
|
|---|
| 156 |
`C-c :' (`c-scope-operator') |
|---|
| 157 |
In C++, it is also sometimes desirable to insert the double-colon |
|---|
| 158 |
scope operator without performing the electric behavior of colon |
|---|
| 159 |
insertion. `C-c :' does just this. |
|---|
| 160 |
|
|---|
| 161 |
`C-c C-\' (`c-backslash-region') |
|---|
| 162 |
This function is handy when editing macros split over several |
|---|
| 163 |
lines by ending each line with a backslash. It inserts and |
|---|
| 164 |
aligns, or deletes these end-of-line backslashes in the current |
|---|
| 165 |
region. |
|---|
| 166 |
|
|---|
| 167 |
With no prefix argument, it inserts any missing backslashes and |
|---|
| 168 |
aligns them to the column specified by the `c-backslash-column' |
|---|
| 169 |
style variable. With a prefix argument, it deletes any |
|---|
| 170 |
backslashes. |
|---|
| 171 |
|
|---|
| 172 |
The function does not modify blank lines at the start of the |
|---|
| 173 |
region. If the region ends at the start of a line, it always |
|---|
| 174 |
deletes the backslash (if any) at the end of the previous line. |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
File: ccmode, Node: Customizing Indentation, Next: Syntactic Symbols, Prev: Commands, Up: Top |
|---|
| 178 |
|
|---|
| 179 |
Customizing Indentation |
|---|
| 180 |
*********************** |
|---|
| 181 |
|
|---|
| 182 |
The style variable `c-offsets-alist' contains the mappings between |
|---|
| 183 |
syntactic symbols and the offsets to apply for those symbols. It's set |
|---|
| 184 |
at mode initialization from a _style_ you may specify. Styles are |
|---|
| 185 |
groupings of syntactic symbol offsets and other style variable values. |
|---|
| 186 |
Most likely, you'll find that one of the pre-defined styles will suit |
|---|
| 187 |
your needs. *Note Styles::, for an explanation of how to set up named |
|---|
| 188 |
styles. |
|---|
| 189 |
|
|---|
| 190 |
Only syntactic symbols not already bound on `c-offsets-alist' will |
|---|
| 191 |
be set from styles. This means that any association you set on it, be |
|---|
| 192 |
it before or after mode initialization, will not be changed. The |
|---|
| 193 |
`c-offsets-alist' variable may therefore be used from e.g. the |
|---|
| 194 |
Customization interface(1) to easily change indentation offsets without |
|---|
| 195 |
having to bother about styles. Initially `c-offsets-alist' is empty, |
|---|
| 196 |
so that all syntactic symbols are set by the style system. |
|---|
| 197 |
|
|---|
| 198 |
You can use the command `C-c C-o' (`c-set-offset') as the way to set |
|---|
| 199 |
offsets, both interactively and from your mode hook(2). |
|---|
| 200 |
|
|---|
| 201 |
The offset associated with any particular syntactic symbol can be |
|---|
| 202 |
any of an integer, a function or lambda expression, a variable name, a |
|---|
| 203 |
vector, a list, or one of the following symbols: `+', `-', `++', `--', |
|---|
| 204 |
`*', or `/'. |
|---|
| 205 |
|
|---|
| 206 |
Those last special symbols describe an offset in multiples of the |
|---|
| 207 |
value of the style variable `c-basic-offset'. By defining a style's |
|---|
| 208 |
indentation in terms of this fundamental variable, you can change the |
|---|
| 209 |
amount of whitespace given to an indentation level while maintaining the |
|---|
| 210 |
same basic shape of your code. Here are the values that the special |
|---|
| 211 |
symbols correspond to: |
|---|
| 212 |
|
|---|
| 213 |
`+' |
|---|
| 214 |
`c-basic-offset' times 1 |
|---|
| 215 |
|
|---|
| 216 |
`-' |
|---|
| 217 |
`c-basic-offset' times -1 |
|---|
| 218 |
|
|---|
| 219 |
`++' |
|---|
| 220 |
`c-basic-offset' times 2 |
|---|
| 221 |
|
|---|
| 222 |
`--' |
|---|
| 223 |
`c-basic-offset' times -2 |
|---|
| 224 |
|
|---|
| 225 |
`*' |
|---|
| 226 |
`c-basic-offset' times 0.5 |
|---|
| 227 |
|
|---|
| 228 |
`/' |
|---|
| 229 |
`c-basic-offset' times -0.5 |
|---|
| 230 |
|
|---|
| 231 |
When a function is used as offset, it's called an "indentation |
|---|
| 232 |
function". Such functions are useful when more context than just the |
|---|
| 233 |
syntactic symbol is needed to get the desired indentation. *Note |
|---|
| 234 |
Indentation Functions::, and *Note Custom Indentation Functions::, for |
|---|
| 235 |
details about them. |
|---|
| 236 |
|
|---|
| 237 |
If the offset is a vector, its first element sets the absolute |
|---|
| 238 |
indentation column, which will override any relative indentation. |
|---|
| 239 |
|
|---|
| 240 |
The offset can also be a list, in which case it is evaluated |
|---|
| 241 |
recursively using the semantics described above. The first element of |
|---|
| 242 |
the list that returns a non-`nil' value succeeds and the evaluation |
|---|
| 243 |
stops. If none of the list elements return a non-`nil' value, then an |
|---|
| 244 |
offset of 0 (zero) is used(3). |
|---|
| 245 |
|
|---|
| 246 |
So, for example, because most of the default offsets are defined in |
|---|
| 247 |
terms of `+', `-', and `0', if you like the general indentation style, |
|---|
| 248 |
but you use 4 spaces instead of 2 spaces per level, you can probably |
|---|
| 249 |
achieve your style just by changing `c-basic-offset' like so(4): |
|---|
| 250 |
|
|---|
| 251 |
_M-x set-variable RET_ |
|---|
| 252 |
Set variable: _c-basic-offset RET_ |
|---|
| 253 |
Set c-basic-offset to value: _4 RET_ |
|---|
| 254 |
|
|---|
| 255 |
This would change |
|---|
| 256 |
|
|---|
| 257 |
int add( int val, int incr, int doit ) |
|---|
| 258 |
{ |
|---|
| 259 |
if( doit ) |
|---|
| 260 |
{ |
|---|
| 261 |
return( val + incr ); |
|---|
| 262 |
} |
|---|
| 263 |
return( val ); |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
to |
|---|
| 267 |
|
|---|
| 268 |
int add( int val, int incr, int doit ) |
|---|
| 269 |
{ |
|---|
| 270 |
if( doit ) |
|---|
| 271 |
{ |
|---|
| 272 |
return( val + incr ); |
|---|
| 273 |
} |
|---|
| 274 |
return( val ); |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
To change indentation styles more radically, you will want to change |
|---|
| 278 |
the offsets associated with other syntactic symbols. First, I'll show |
|---|
| 279 |
you how to do that interactively, then I'll describe how to make |
|---|
| 280 |
changes to your `.emacs' file so that your changes are more permanent. |
|---|
| 281 |
|
|---|
| 282 |
* Menu: |
|---|
| 283 |
|
|---|
| 284 |
* Interactive Customization:: |
|---|
| 285 |
* Permanent Customization:: |
|---|
| 286 |
* Hooks:: |
|---|
| 287 |
* Styles:: |
|---|
| 288 |
* Advanced Customizations:: |
|---|
| 289 |
|
|---|
| 290 |
---------- Footnotes ---------- |
|---|
| 291 |
|
|---|
| 292 |
(1) Available in Emacs 20 and later, and XEmacs 19.15 and later. |
|---|
| 293 |
|
|---|
| 294 |
(2) Obviously, you use the key binding interactively, and the |
|---|
| 295 |
function call programmatically! |
|---|
| 296 |
|
|---|
| 297 |
(3) There is however a variable `c-strict-syntax-p' that, when set |
|---|
| 298 |
to non-`nil', will cause an error to be signalled in that case. It's |
|---|
| 299 |
now considered obsolete since it doesn't work well with some of the |
|---|
| 300 |
alignment functions that now returns `nil' instead of zero to be more |
|---|
| 301 |
usable in lists. You should therefore leave `c-strict-syntax-p' set to |
|---|
| 302 |
`nil'. |
|---|
| 303 |
|
|---|
| 304 |
(4) You can try this interactively in a C buffer by typing the text |
|---|
| 305 |
that appears in italics. |
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
File: ccmode, Node: Interactive Customization, Next: Permanent Customization, Up: Customizing Indentation |
|---|
| 309 |
|
|---|
| 310 |
Interactive Customization |
|---|
| 311 |
========================= |
|---|
| 312 |
|
|---|
| 313 |
As an example of how to customize indentation, let's change the |
|---|
| 314 |
style of this example(1): |
|---|
| 315 |
|
|---|
| 316 |
1: int add( int val, int incr, int doit ) |
|---|
| 317 |
2: { |
|---|
| 318 |
3: if( doit ) |
|---|
| 319 |
4: { |
|---|
| 320 |
5: return( val + incr ); |
|---|
| 321 |
6: } |
|---|
| 322 |
7: return( val ); |
|---|
| 323 |
8: } |
|---|
| 324 |
|
|---|
| 325 |
to: |
|---|
| 326 |
|
|---|
| 327 |
1: int add( int val, int incr, int doit ) |
|---|
| 328 |
2: { |
|---|
| 329 |
3: if( doit ) |
|---|
| 330 |
4: { |
|---|
| 331 |
5: return( val + incr ); |
|---|
| 332 |
6: } |
|---|
| 333 |
7: return( val ); |
|---|
| 334 |
8: } |
|---|
| 335 |
|
|---|
| 336 |
In other words, we want to change the indentation of braces that |
|---|
| 337 |
open a block following a condition so that the braces line up under the |
|---|
| 338 |
conditional, instead of being indented. Notice that the construct we |
|---|
| 339 |
want to change starts on line 4. To change the indentation of a line, |
|---|
| 340 |
we need to see which syntactic components affect the offset calculations |
|---|
| 341 |
for that line. Hitting `C-c C-s' on line 4 yields: |
|---|
| 342 |
|
|---|
| 343 |
((substatement-open . 44)) |
|---|
| 344 |
|
|---|
| 345 |
so we know that to change the offset of the open brace, we need to |
|---|
| 346 |
change the indentation for the `substatement-open' syntactic symbol. |
|---|
| 347 |
To do this interactively, just hit `C-c C-o'. This prompts you for the |
|---|
| 348 |
syntactic symbol to change, providing a reasonable default. In this |
|---|
| 349 |
case, the default is `substatement-open', which is just the syntactic |
|---|
| 350 |
symbol we want to change! |
|---|
| 351 |
|
|---|
| 352 |
After you hit return, CC Mode will then prompt you for the new |
|---|
| 353 |
offset value, with the old value as the default. The default in this |
|---|
| 354 |
case is `+', but we want no extra indentation so enter `0' and `RET'. |
|---|
| 355 |
This will associate the offset 0 with the syntactic symbol |
|---|
| 356 |
`substatement-open'. |
|---|
| 357 |
|
|---|
| 358 |
To check your changes quickly, just hit `C-c C-q' (`c-indent-defun') |
|---|
| 359 |
to reindent the entire function. The example should now look like: |
|---|
| 360 |
|
|---|
| 361 |
1: int add( int val, int incr, int doit ) |
|---|
| 362 |
2: { |
|---|
| 363 |
3: if( doit ) |
|---|
| 364 |
4: { |
|---|
| 365 |
5: return( val + incr ); |
|---|
| 366 |
6: } |
|---|
| 367 |
7: return( val ); |
|---|
| 368 |
8: } |
|---|
| 369 |
|
|---|
| 370 |
Notice how just changing the open brace offset on line 4 is all we |
|---|
| 371 |
needed to do. Since the other affected lines are indented relative to |
|---|
| 372 |
line 4, they are automatically indented the way you'd expect. For more |
|---|
| 373 |
complicated examples, this may not always work. The general approach to |
|---|
| 374 |
take is to always start adjusting offsets for lines higher up in the |
|---|
| 375 |
file, then re-indent and see if any following lines need further |
|---|
| 376 |
adjustments. |
|---|
| 377 |
|
|---|
| 378 |
---------- Footnotes ---------- |
|---|
| 379 |
|
|---|
| 380 |
(1) In this an subsequent examples, the original code is formatted |
|---|
| 381 |
using the `gnu' style unless otherwise indicated. *Note Styles::. |
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
File: ccmode, Node: Permanent Customization, Next: Hooks, Prev: Interactive Customization, Up: Customizing Indentation |
|---|
| 385 |
|
|---|
| 386 |
Permanent Customization |
|---|
| 387 |
======================= |
|---|
| 388 |
|
|---|
| 389 |
To make your changes permanent, you need to add some lisp code to |
|---|
| 390 |
your `.emacs' file. CC Mode supports many different ways to be |
|---|
| 391 |
configured, from the straightforward way by setting variables globally |
|---|
| 392 |
in `.emacs' or in the Customization interface, to the complex and |
|---|
| 393 |
precisely controlled way by using styles and hook functions. |
|---|
| 394 |
|
|---|
| 395 |
The simplest way of customizing CC Mode permanently is to set the |
|---|
| 396 |
variables in your `.emacs' with `setq' and similar commands. So to |
|---|
| 397 |
make the setting of `substatement-open' permanent, add this to the |
|---|
| 398 |
`.emacs' file: |
|---|
| 399 |
|
|---|
| 400 |
(require 'cc-mode) |
|---|
| 401 |
(c-set-offset 'substatement-open 0) |
|---|
| 402 |
|
|---|
| 403 |
The `require' line is only needed once in the beginning to make sure |
|---|
| 404 |
CC Mode is loaded so that the `c-set-offset' function is defined. |
|---|
| 405 |
|
|---|
| 406 |
You can also use the more user friendly Customization interface, but |
|---|
| 407 |
this manual does not cover how that works. |
|---|
| 408 |
|
|---|
| 409 |
Variables set like this at the top level in `.emacs' take effect in |
|---|
| 410 |
all CC Mode buffers, regardless of language. The indentation style |
|---|
| 411 |
related variables, e.g. `c-basic-offset', that you don't set this way |
|---|
| 412 |
get their value from the style system (*note Styles::), and they |
|---|
| 413 |
therefore depend on the setting of `c-default-style'. Note that if you |
|---|
| 414 |
use Customize, this means that the greyed-out default values presented |
|---|
| 415 |
there might not be the ones you actually get, since the actual values |
|---|
| 416 |
depend on the style, which may very well be different for different |
|---|
| 417 |
languages. |
|---|
| 418 |
|
|---|
| 419 |
If you want to make more advanced configurations, e.g. |
|---|
| 420 |
language-specific customization, global variable settings isn't enough. |
|---|
| 421 |
For that you can use the language hooks, see *Note Hooks::, and/or the |
|---|
| 422 |
style system, see *Note Styles::. |
|---|
| 423 |
|
|---|
| 424 |
By default, all style variables are global, so that every buffer will |
|---|
| 425 |
share the same style settings. This is fine if you primarily edit one |
|---|
| 426 |
style of code, but if you edit several languages and want to use |
|---|
| 427 |
different styles for them, you need finer control by making the style |
|---|
| 428 |
variables buffer local. The recommended way to do this is to set the |
|---|
| 429 |
variable `c-style-variables-are-local-p' to `t'. The variables will be |
|---|
| 430 |
made buffer local when CC Mode is activated in a buffer for the first |
|---|
| 431 |
time in the Emacs session. Note that once the style variables are made |
|---|
| 432 |
buffer local, they cannot be made global again, without restarting |
|---|
| 433 |
Emacs. |
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
File: ccmode, Node: Hooks, Next: Styles, Prev: Permanent Customization, Up: Customizing Indentation |
|---|
| 437 |
|
|---|
| 438 |
Hooks |
|---|
| 439 |
===== |
|---|
| 440 |
|
|---|
| 441 |
CC Mode provides several hooks that you can use to customize the mode |
|---|
| 442 |
according to your coding style. Each language mode has its own hook, |
|---|
| 443 |
adhering to standard Emacs major mode conventions. There is also one |
|---|
| 444 |
general hook and one package initialization hook: |
|---|
| 445 |
|
|---|
| 446 |
* `c-mode-hook' -- For C buffers only. |
|---|
| 447 |
|
|---|
| 448 |
* `c++-mode-hook' -- For C++ buffers only. |
|---|
| 449 |
|
|---|
| 450 |
* `objc-mode-hook' -- For Objective-C buffers only. |
|---|
| 451 |
|
|---|
| 452 |
* `java-mode-hook' -- For Java buffers only. |
|---|
| 453 |
|
|---|
| 454 |
* `idl-mode-hook' -- For CORBA IDL buffers only. |
|---|
| 455 |
|
|---|
| 456 |
* `pike-mode-hook' -- For Pike buffers only. |
|---|
| 457 |
|
|---|
| 458 |
* `c-mode-common-hook' -- Common across all languages. |
|---|
| 459 |
|
|---|
| 460 |
* `c-initialization-hook' -- Hook run only once per Emacs session, |
|---|
| 461 |
when CC Mode is initialized. |
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
The language hooks get run as the last thing when you enter that |
|---|
| 465 |
language mode. The `c-mode-common-hook' is run by all supported modes |
|---|
| 466 |
_before_ the language specific hook, and thus can contain |
|---|
| 467 |
customizations that are common across all languages. Most of the |
|---|
| 468 |
examples in this section will assume you are using the common hook. |
|---|
| 469 |
|
|---|
| 470 |
Note that all the language-specific mode setup that CC Mode does is |
|---|
| 471 |
done prior to both `c-mode-common-hook' and the language specific hook. |
|---|
| 472 |
That includes installing the indentation style, which can be mode |
|---|
| 473 |
specific (and also is by default for Java mode). Thus, any style |
|---|
| 474 |
settings done in `c-mode-common-hook' will override whatever |
|---|
| 475 |
language-specific style is chosen by `c-default-style'. |
|---|
| 476 |
|
|---|
| 477 |
Here's a simplified example of what you can add to your `.emacs' |
|---|
| 478 |
file to do things whenever any CC Mode language is edited. See the |
|---|
| 479 |
Emacs manuals for more information on customizing Emacs via hooks. |
|---|
| 480 |
*Note Sample .emacs File::, for a more complete sample `.emacs' file. |
|---|
| 481 |
|
|---|
| 482 |
(defun my-c-mode-common-hook () |
|---|
| 483 |
;; my customizations for all of c-mode and related modes |
|---|
| 484 |
(no-case-fold-search) |
|---|
| 485 |
) |
|---|
| 486 |
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook) |
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
File: ccmode, Node: Styles, Next: Advanced Customizations, Prev: Hooks, Up: Customizing Indentation |
|---|
| 490 |
|
|---|
| 491 |
Styles |
|---|
| 492 |
====== |
|---|
| 493 |
|
|---|
| 494 |
Most people only need to edit code formatted in just a few |
|---|
| 495 |
well-defined and consistent styles. For example, their organization |
|---|
| 496 |
might impose a "blessed" style that all its programmers must conform |
|---|
| 497 |
to. Similarly, people who work on GNU software will have to use the |
|---|
| 498 |
GNU coding style. Some shops are more lenient, allowing a variety of |
|---|
| 499 |
coding styles, and as programmers come and go, there could be a number |
|---|
| 500 |
of styles in use. For this reason, CC Mode makes it convenient for you |
|---|
| 501 |
to set up logical groupings of customizations called "styles", |
|---|
| 502 |
associate a single name for any particular style, and pretty easily |
|---|
| 503 |
start editing new or existing code using these styles. |
|---|
| 504 |
|
|---|
| 505 |
The variables that the style system affect are called "style |
|---|
| 506 |
variables". They are handled specially in several ways: |
|---|
| 507 |
|
|---|
| 508 |
* Style variables are by default global variables, i.e. they have |
|---|
| 509 |
the same value in all Emacs buffers. However, they can instead be |
|---|
| 510 |
made always buffer local by setting |
|---|
| 511 |
`c-style-variables-are-local-p' to non-`nil' before CC Mode is |
|---|
| 512 |
initialized. |
|---|
| 513 |
|
|---|
| 514 |
* The default value of any style variable (with two exceptions -- see |
|---|
| 515 |
below) is the special symbol `set-from-style'. Variables that are |
|---|
| 516 |
still set to that symbol when a CC Mode buffer is initialized will |
|---|
| 517 |
be set according to the current style, otherwise they will keep |
|---|
| 518 |
their current value(1). |
|---|
| 519 |
|
|---|
| 520 |
Note that when we talk about the "default value" for a style |
|---|
| 521 |
variable, we don't mean the `set-from-style' symbol that all style |
|---|
| 522 |
variables are set to initially, but instead the value it will get |
|---|
| 523 |
at mode initialization when neither a style nor a global setting |
|---|
| 524 |
has set its value. |
|---|
| 525 |
|
|---|
| 526 |
The style variable `c-offsets-alist' is handled a little |
|---|
| 527 |
differently from the other style variables. It's an association |
|---|
| 528 |
list, and is thus by default set to the empty list, `nil'. When |
|---|
| 529 |
the style system is initialized, any syntactic symbols already on |
|---|
| 530 |
it are kept -- only the missing ones are filled in from the chosen |
|---|
| 531 |
style. |
|---|
| 532 |
|
|---|
| 533 |
The style variable `c-special-indent-hook' is also handled in a |
|---|
| 534 |
special way. Styles may only add more functions on this hook, so |
|---|
| 535 |
the global settings on it are always preserved(2). |
|---|
| 536 |
|
|---|
| 537 |
* The global settings of style variables get captured in the special |
|---|
| 538 |
`user' style, which is used as the base for all the other styles. |
|---|
| 539 |
*Note Built-in Styles::, for details. |
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
The style variables are: `c-basic-offset', |
|---|
| 543 |
`c-comment-only-line-offset', `c-block-comment-prefix', |
|---|
| 544 |
`c-comment-prefix-regexp', `c-cleanup-list', `c-hanging-braces-alist', |
|---|
| 545 |
`c-hanging-colons-alist', `c-hanging-semi&comma-criteria', |
|---|
| 546 |
`c-backslash-column', `c-special-indent-hook', |
|---|
| 547 |
`c-label-minimum-indentation', and `c-offsets-alist'. |
|---|
| 548 |
|
|---|
| 549 |
* Menu: |
|---|
| 550 |
|
|---|
| 551 |
* Built-in Styles:: |
|---|
| 552 |
* Adding Styles:: |
|---|
| 553 |
* File Styles:: |
|---|
| 554 |
|
|---|
| 555 |
---------- Footnotes ---------- |
|---|
| 556 |
|
|---|
| 557 |
(1) This is a big change from versions of CC Mode earlier than 5.26, |
|---|
| 558 |
where such settings would get overridden by the style system unless |
|---|
| 559 |
special precautions were taken. That was changed since it was |
|---|
| 560 |
counterintuitive and confusing, especially to novice users. If your |
|---|
| 561 |
configuration depends on the old overriding behavior, you can set the |
|---|
| 562 |
variable `c-old-style-variable-behavior' to non-`nil'. |
|---|
| 563 |
|
|---|
| 564 |
(2) This did not change in version 5.26. |
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
File: ccmode, Node: Built-in Styles, Next: Adding Styles, Up: Styles |
|---|
| 568 |
|
|---|
| 569 |
Built-in Styles |
|---|
| 570 |
--------------- |
|---|
| 571 |
|
|---|
| 572 |
If you're lucky, one of CC Mode's built-in styles might be just what |
|---|
| 573 |
you're looking for. These include: |
|---|
| 574 |
|
|---|
| 575 |
* `gnu' -- Coding style blessed by the Free Software Foundation for |
|---|
| 576 |
C code in GNU programs. |
|---|
| 577 |
|
|---|
| 578 |
* `k&r' -- The classic Kernighan and Ritchie style for C code. |
|---|
| 579 |
|
|---|
| 580 |
* `bsd' -- Also known as "Allman style" after Eric Allman. |
|---|
| 581 |
|
|---|
| 582 |
* `whitesmith' -- Popularized by the examples that came with |
|---|
| 583 |
Whitesmiths C, an early commercial C compiler. |
|---|
| 584 |
|
|---|
| 585 |
* `stroustrup' -- The classic Stroustrup style for C++ code. |
|---|
| 586 |
|
|---|
| 587 |
* `ellemtel' -- Popular C++ coding standards as defined by |
|---|
| 588 |
"Programming in C++, Rules and Recommendations," Erik Nyquist and |
|---|
| 589 |
Mats Henricson, Ellemtel(1). |
|---|
| 590 |
|
|---|
| 591 |
* `linux' -- C coding standard for Linux (the kernel). |
|---|
| 592 |
|
|---|
| 593 |
* `python' -- C coding standard for Python extension modules(2). |
|---|
| 594 |
|
|---|
| 595 |
* `java' -- The style for editing Java code. Note that the default |
|---|
| 596 |
value for `c-default-style' installs this style when you enter |
|---|
| 597 |
`java-mode'. |
|---|
| 598 |
|
|---|
| 599 |
* `user' -- This is a special style for several reasons. First, the |
|---|
| 600 |
CC Mode customizations you do by using either the Customization |
|---|
| 601 |
interface, or by writing `setq''s at the top level of your |
|---|
| 602 |
`.emacs' file, will be captured in the `user' style. Also, all |
|---|
| 603 |
other styles implicitly inherit their settings from `user' style. |
|---|
| 604 |
This means that for any styles you add via `c-add-style' (*note |
|---|
| 605 |
Adding Styles::) you need only define the differences between your |
|---|
| 606 |
new style and `user' style. |
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
The default style in all newly created buffers is `gnu', but you can |
|---|
| 610 |
change this by setting variable `c-default-style'. Although the `user' |
|---|
| 611 |
style is not the default style, any style variable settings you do with |
|---|
| 612 |
the Customization interface or on the top level in your `.emacs' file |
|---|
| 613 |
will by default override the style system, so you don't need to set |
|---|
| 614 |
`c-default-style' to `user' to see the effect of these settings. |
|---|
| 615 |
|
|---|
| 616 |
`c-default-style' takes either a style name string, or an |
|---|
| 617 |
association list of major mode symbols to style names. Thus you can |
|---|
| 618 |
control exactly which default style is used for which CC Mode language |
|---|
| 619 |
mode. Here are the rules: |
|---|
| 620 |
|
|---|
| 621 |
1. When `c-default-style' is a string, it must be an existing style |
|---|
| 622 |
name as found in `c-style-alist'. This style is then used for all |
|---|
| 623 |
modes. |
|---|
| 624 |
|
|---|
| 625 |
2. When `c-default-style' is an association list, the current major |
|---|
| 626 |
mode is looked up to find a style name string. In this case, this |
|---|
| 627 |
style is always used exactly as specified and an error will occur |
|---|
| 628 |
if the named style does not exist. |
|---|
| 629 |
|
|---|
| 630 |
3. If `c-default-style' is an association list, but the current major |
|---|
| 631 |
mode isn't found, then the special symbol `other' is looked up. If |
|---|
| 632 |
this value is found, the associated style is used. |
|---|
| 633 |
|
|---|
| 634 |
4. If `other' is not found, then the `gnu' style is used. |
|---|
| 635 |
|
|---|
| 636 |
5. In all cases, the style described in `c-default-style' is installed |
|---|
| 637 |
_before_ the language hooks are run, so you can always override |
|---|
| 638 |
this setting by including an explicit call to `c-set-style' in your |
|---|
| 639 |
language mode hook, or in `c-mode-common-hook'. |
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
If you'd like to experiment with these built-in styles you can simply |
|---|
| 643 |
type the following in a CC Mode buffer: |
|---|
| 644 |
|
|---|
| 645 |
C-c . STYLE-NAME RET |
|---|
| 646 |
|
|---|
| 647 |
`C-c .' runs the command `c-set-style'. Note that all style names are |
|---|
| 648 |
case insensitive, even the ones you define. |
|---|
| 649 |
|
|---|
| 650 |
Setting a style in this way does _not_ automatically re-indent your |
|---|
| 651 |
file. For commands that you can use to view the effect of your changes, |
|---|
| 652 |
see *Note Commands::. |
|---|
| 653 |
|
|---|
| 654 |
Note that for BOCM compatibility, `gnu' is the default style, and |
|---|
| 655 |
any non-style based customizations you make (i.e. in |
|---|
| 656 |
`c-mode-common-hook' in your `.emacs' file) will be based on `gnu' |
|---|
| 657 |
style unless you set `c-default-style' or do a `c-set-style' as the |
|---|
| 658 |
first thing in your hook. The variable `c-indentation-style' always |
|---|
| 659 |
contains the buffer's current style name, as a string. |
|---|
| 660 |
|
|---|
| 661 |
---------- Footnotes ---------- |
|---|
| 662 |
|
|---|
| 663 |
(1) This document is available at |
|---|
| 664 |
`http://www.doc.ic.ac.uk/lab/cplus/c++.rules/' among other places. |
|---|
| 665 |
|
|---|
| 666 |
(2) Python is a high level scripting language with a C/C++ foreign |
|---|
| 667 |
function interface. For more information, see `http://www.python.org/'. |
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
File: ccmode, Node: Adding Styles, Next: File Styles, Prev: Built-in Styles, Up: Styles |
|---|
| 671 |
|
|---|
| 672 |
Adding Styles |
|---|
| 673 |
------------- |
|---|
| 674 |
|
|---|
| 675 |
If none of the built-in styles is appropriate, you'll probably want |
|---|
| 676 |
to add a new "style definition". Styles are kept in the |
|---|
| 677 |
`c-style-alist' variable, but you should never modify this variable |
|---|
| 678 |
directly. Instead, CC Mode provides the function `c-add-style' that |
|---|
| 679 |
you can use to easily add new styles or change existing styles. This |
|---|
| 680 |
function takes two arguments, a STYLENAME string, and an association |
|---|
| 681 |
list DESCRIPTION of style customizations. If STYLENAME is not already |
|---|
| 682 |
in `c-style-alist', the new style is added, otherwise the style is |
|---|
| 683 |
changed to the new DESCRIPTION. This function also takes an optional |
|---|
| 684 |
third argument, which if non-`nil', automatically applies the new style |
|---|
| 685 |
to the current buffer. |
|---|
| 686 |
|
|---|
| 687 |
The sample `.emacs' file provides a concrete example of how a new |
|---|
| 688 |
style can be added and automatically set. *Note Sample .emacs File::. |
|---|
| 689 |
|
|---|
| 690 |
|
|---|
| 691 |
File: ccmode, Node: File Styles, Prev: Adding Styles, Up: Styles |
|---|
| 692 |
|
|---|
| 693 |
File Styles |
|---|
| 694 |
----------- |
|---|
| 695 |
|
|---|
| 696 |
The Emacs manual describes how you can customize certain variables |
|---|
| 697 |
on a per-file basis by including a "Local Variable" block at the end of |
|---|
| 698 |
the file. So far, you've only seen a functional interface to CC Mode |
|---|
| 699 |
customization, which is highly inconvenient for use in a Local Variable |
|---|
| 700 |
block. CC Mode provides two variables that make it easier for you to |
|---|
| 701 |
customize your style on a per-file basis. |
|---|
| 702 |
|
|---|
| 703 |
The variable `c-file-style' can be set to a style name string. When |
|---|
| 704 |
the file is visited, CC Mode will automatically set the file's style to |
|---|
| 705 |
this style using `c-set-style'. |
|---|
| 706 |
|
|---|
| 707 |
Another variable, `c-file-offsets', takes an association list |
|---|
| 708 |
similar to what is allowed in `c-offsets-alist'. When the file is |
|---|
| 709 |
visited, CC Mode will automatically institute these offsets using |
|---|
| 710 |
`c-set-offset'. |
|---|
| 711 |
|
|---|
| 712 |
Note that file style settings (i.e. `c-file-style') are applied |
|---|
| 713 |
before file offset settings (i.e. `c-file-offsets'). Also, if either |
|---|
| 714 |
of these are set in a file's local variable section, all the style |
|---|
| 715 |
variable values are made local to that buffer. |
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
File: ccmode, Node: Advanced Customizations, Prev: Styles, Up: Customizing Indentation |
|---|
| 719 |
|
|---|
| 720 |
Advanced Customizations |
|---|
| 721 |
======================= |
|---|
| 722 |
|
|---|
| 723 |
For most users, CC Mode will support their coding styles with very |
|---|
| 724 |
little need for more advanced customizations. Usually, one of the |
|---|
| 725 |
standard styles defined in `c-style-alist' will do the trick. At most, |
|---|
| 726 |
perhaps one of the syntactic symbol offsets will need to be tweaked |
|---|
| 727 |
slightly, or maybe `c-basic-offset' will need to be changed. However, |
|---|
| 728 |
some styles require a more flexible framework for customization, and |
|---|
| 729 |
one of the real strengths of CC Mode is that the syntactic analysis |
|---|
| 730 |
model provides just such a framework. This allows you to implement |
|---|
| 731 |
custom indentation calculations for situations not handled by the mode |
|---|
| 732 |
directly. |
|---|
| 733 |
|
|---|
| 734 |
* Menu: |
|---|
| 735 |
|
|---|
| 736 |
* Custom Indentation Functions:: |
|---|
| 737 |
* Custom Brace and Colon Hanging:: |
|---|
| 738 |
* Customizing Semi-colons and Commas:: |
|---|
| 739 |
* Other Special Indentations:: |
|---|
| 740 |
|
|---|
| 741 |
|
|---|
| 742 |
File: ccmode, Node: Custom Indentation Functions, Next: Custom Brace and Colon Hanging, Up: Advanced Customizations |
|---|
| 743 |
|
|---|
| 744 |
Custom Indentation Functions |
|---|
| 745 |
---------------------------- |
|---|
| 746 |
|
|---|
| 747 |
The most flexible way to customize CC Mode is by writing custom |
|---|
| 748 |
indentation functions, and associating them with specific syntactic |
|---|
| 749 |
symbols (*note Syntactic Symbols::). CC Mode itself uses indentation |
|---|
| 750 |
functions to provide more sophisticated indentation, for example when |
|---|
| 751 |
lining up C++ stream operator blocks: |
|---|
| 752 |
|
|---|
| 753 |
1: void main(int argc, char**) |
|---|
| 754 |
2: { |
|---|
| 755 |
3: cout << "There were " |
|---|
| 756 |
4: << argc |
|---|
| 757 |
5: << "arguments passed to the program" |
|---|
| 758 |
6: << endl; |
|---|
| 759 |
7: } |
|---|
| 760 |
|
|---|
| 761 |
In this example, lines 4 through 6 are assigned the `stream-op' |
|---|
| 762 |
syntactic symbol. Here, `stream-op' has an offset of `+', and with a |
|---|
| 763 |
`c-basic-offset' of 2, you can see that lines 4 through 6 are simply |
|---|
| 764 |
indented two spaces to the right of line 3. But perhaps we'd like CC |
|---|
| 765 |
Mode to be a little more intelligent so that it aligns all the `<<' |
|---|
| 766 |
symbols in lines 3 through 6. To do this, we have to write a custom |
|---|
| 767 |
indentation function which finds the column of first stream operator on |
|---|
| 768 |
the first line of the statement. Here is sample lisp code implementing |
|---|
| 769 |
this: |
|---|
| 770 |
|
|---|
| 771 |
(defun c-lineup-streamop (langelem) |
|---|
| 772 |
;; lineup stream operators |
|---|
| 773 |
(save-excursion |
|---|
| 774 |
(let* ((relpos (cdr langelem)) |
|---|
| 775 |
(curcol (progn (goto-char relpos) |
|---|
| 776 |
(current-column)))) |
|---|
| 777 |
(re-search-forward "<<\\|>>" (c-point 'eol) 'move) |
|---|
| 778 |
(goto-char (match-beginning 0)) |
|---|
| 779 |
(- (current-column) curcol)))) |
|---|
| 780 |
|
|---|
| 781 |
Indentation functions take a single argument, which is a syntactic |
|---|
| 782 |
component cons cell (*note Syntactic Analysis::). The function returns |
|---|
| 783 |
an integer offset value that will be added to the running total |
|---|
| 784 |
indentation for the line. Note that what actually gets returned is the |
|---|
| 785 |
difference between the column that the first stream operator is on, and |
|---|
| 786 |
the column of the buffer relative position passed in the function's |
|---|
| 787 |
argument. Remember that CC Mode automatically adds in the column of |
|---|
| 788 |
the component's relative buffer position and we don't the column offset |
|---|
| 789 |
added in twice. |
|---|
| 790 |
|
|---|
| 791 |
The function should return `nil' if it's used in a situation where |
|---|
| 792 |
it doesn't want to do any decision. If the function is used in a list |
|---|
| 793 |
expression (*note Customizing Indentation::), that will cause CC Mode |
|---|
| 794 |
to go on and check the next entry in the list. |
|---|
| 795 |
|
|---|
| 796 |
Now, to associate the function `c-lineup-streamop' with the |
|---|
| 797 |
`stream-op' syntactic symbol, we can add something like the following |
|---|
| 798 |
to our `c++-mode-hook'(1): |
|---|
| 799 |
|
|---|
| 800 |
(c-set-offset 'stream-op 'c-lineup-streamop) |
|---|
| 801 |
|
|---|
| 802 |
Now the function looks like this after re-indenting (using `C-c |
|---|
| 803 |
C-q'): |
|---|
| 804 |
|
|---|
| 805 |
1: void main(int argc, char**) |
|---|
| 806 |
2: { |
|---|
| 807 |
3: cout << "There were " |
|---|
| 808 |
4: << argc |
|---|
| 809 |
5: << " arguments passed to the program" |
|---|
| 810 |
6: << endl; |
|---|
| 811 |
7: } |
|---|
| 812 |
|
|---|
| 813 |
Custom indentation functions can be as simple or as complex as you |
|---|
| 814 |
like, and any syntactic symbol that appears in `c-offsets-alist' can |
|---|
| 815 |
have a custom indentation function associated with it. |
|---|
| 816 |
|
|---|
| 817 |
CC Mode comes with an extensive set of predefined indentation |
|---|
| 818 |
functions, not all of which are used by the default styles. So there's |
|---|
| 819 |
a good chance the function you want already exists. *Note Indentation |
|---|
| 820 |
Functions::, for a list of them. If you have written an indentation |
|---|
| 821 |
function that you think is generally useful, you're very welcome to |
|---|
| 822 |
contribute it; please contact <bug-cc-mode@gnu.org>. |
|---|
| 823 |
|
|---|
| 824 |
---------- Footnotes ---------- |
|---|
| 825 |
|
|---|
| 826 |
(1) It probably makes more sense to add this to `c++-mode-hook' than |
|---|
| 827 |
`c-mode-common-hook' since stream operators are only relevant for C++. |
|---|
| 828 |
|
|---|
| 829 |
|
|---|
| 830 |
File: ccmode, Node: Custom Brace and Colon Hanging, Next: Customizing Semi-colons and Commas, Prev: Custom Indentation Functions, Up: Advanced Customizations |
|---|
| 831 |
|
|---|
| 832 |
Custom Brace and Colon Hanging |
|---|
| 833 |
------------------------------ |
|---|
| 834 |
|
|---|
| 835 |
Syntactic symbols aren't the only place where you can customize CC |
|---|
| 836 |
Mode with the lisp equivalent of callback functions. Brace "hanginess" |
|---|
| 837 |
can also be determined by custom functions associated with syntactic |
|---|
| 838 |
symbols on the `c-hanging-braces-alist' style variable. Remember that |
|---|
| 839 |
ACTION's are typically a list containing some combination of the |
|---|
| 840 |
symbols `before' and `after' (*note Hanging Braces::). However, an |
|---|
| 841 |
ACTION can also be a function which gets called when a brace matching |
|---|
| 842 |
that syntactic symbol is entered. |
|---|
| 843 |
|
|---|
| 844 |
These ACTION functions are called with two arguments: the syntactic |
|---|
| 845 |
symbol for the brace, and the buffer position at which the brace was |
|---|
| 846 |
inserted. The ACTION function is expected to return a list containing |
|---|
| 847 |
some combination of `before' and `after', including neither of them |
|---|
| 848 |
(i.e. `nil'). This return value has the normal brace hanging semantics. |
|---|
| 849 |
|
|---|
| 850 |
As an example, CC Mode itself uses this feature to dynamically |
|---|
| 851 |
determine the hanginess of braces which close "do-while" constructs: |
|---|
| 852 |
|
|---|
| 853 |
void do_list( int count, char** atleast_one_string ) |
|---|
| 854 |
{ |
|---|
| 855 |
int i=0; |
|---|
| 856 |
do { |
|---|
| 857 |
handle_string( atleast_one_string[i] ); |
|---|
| 858 |
i++; |
|---|
| 859 |
} while( i < count ); |
|---|
| 860 |
} |
|---|
| 861 |
|
|---|
| 862 |
CC Mode assigns the `block-close' syntactic symbol to the brace that |
|---|
| 863 |
closes the `do' construct, and normally we'd like the line that follows |
|---|
| 864 |
a `block-close' brace to begin on a separate line. However, with |
|---|
| 865 |
"do-while" constructs, we want the `while' clause to follow the closing |
|---|
| 866 |
brace. To do this, we associate the `block-close' symbol with the |
|---|
| 867 |
ACTION function `c-snug-do-while': |
|---|
| 868 |
|
|---|
| 869 |
(defun c-snug-do-while (syntax pos) |
|---|
| 870 |
"Dynamically calculate brace hanginess for do-while statements. |
|---|
| 871 |
Using this function, `while' clauses that end a `do-while' block will |
|---|
| 872 |
remain on the same line as the brace that closes that block. |
|---|
| 873 |
|
|---|
| 874 |
See `c-hanging-braces-alist' for how to utilize this function as an |
|---|
| 875 |
ACTION associated with `block-close' syntax." |
|---|
| 876 |
(save-excursion |
|---|
| 877 |
(let (langelem) |
|---|
| 878 |
(if (and (eq syntax 'block-close) |
|---|
| 879 |
(setq langelem (assq 'block-close c-syntactic-context)) |
|---|
| 880 |
(progn (goto-char (cdr langelem)) |
|---|
| 881 |
(if (= (following-char) ?{) |
|---|
| 882 |
(forward-sexp -1)) |
|---|
| 883 |
(looking-at "\\<do\\>[^_]"))) |
|---|
| 884 |
'(before) |
|---|
| 885 |
'(before after))))) |
|---|
| 886 |
|
|---|
| 887 |
This function simply looks to see if the brace closes a "do-while" |
|---|
| 888 |
clause and if so, returns the list `(before)' indicating that a newline |
|---|
| 889 |
should be inserted before the brace, but not after it. In all other |
|---|
| 890 |
cases, it returns the list `(before after)' so that the brace appears |
|---|
| 891 |
on a line by itself. |
|---|
| 892 |
|
|---|
| 893 |
During the call to the brace hanging ACTION function, the variable |
|---|
| 894 |
`c-syntactic-context' is bound to the full syntactic analysis list. |
|---|
| 895 |
|
|---|
| 896 |
Note that for symmetry, colon hanginess should be customizable by |
|---|
| 897 |
allowing function symbols as ACTIONs on the `c-hanging-colon-alist' |
|---|
| 898 |
style variable. Since no use has actually been found for this feature, |
|---|
| 899 |
it isn't currently implemented! |
|---|
| 900 |
|
|---|
| 901 |
|
|---|
| 902 |
File: ccmode, Node: Customizing Semi-colons and Commas, Next: Other Special Indentations, Prev: Custom Brace and Colon Hanging, Up: Advanced Customizations |
|---|
| 903 |
|
|---|
| 904 |
Customizing Semi-colons and Commas |
|---|
| 905 |
---------------------------------- |
|---|
| 906 |
|
|---|
| 907 |
You can also customize the insertion of newlines after semi-colons |
|---|
| 908 |
and commas, when the auto-newline minor mode is enabled (*note Minor |
|---|
| 909 |
Modes::). This is controlled by the style variable |
|---|
| 910 |
`c-hanging-semi&comma-criteria', which contains a list of functions |
|---|
| 911 |
that are called in the order they appear. Each function is called with |
|---|
| 912 |
zero arguments, and is expected to return one of the following values: |
|---|
| 913 |
|
|---|
| 914 |
* non-`nil' -- A newline is inserted, and no more functions from the |
|---|
| 915 |
list are called. |
|---|
| 916 |
|
|---|
| 917 |
* `stop' -- No more functions from the list are called, but no |
|---|
| 918 |
newline is inserted. |
|---|
| 919 |
|
|---|
| 920 |
* `nil' -- No determination is made, and the next function in the |
|---|
| 921 |
list is called. |
|---|
| 922 |
|
|---|
| 923 |
|
|---|
| 924 |
If every function in the list is called without a determination being |
|---|
| 925 |
made, then no newline is added. The default value for this variable is a |
|---|
| 926 |
list containing a single function which inserts newlines only after |
|---|
| 927 |
semi-colons which do not appear inside parenthesis lists (i.e. those |
|---|
| 928 |
that separate `for'-clause statements). |
|---|
| 929 |
|
|---|
| 930 |
Here's an example of a criteria function, provided by CC Mode, that |
|---|
| 931 |
will prevent newlines from being inserted after semicolons when there is |
|---|
| 932 |
a non-blank following line. Otherwise, it makes no determination. To |
|---|
| 933 |
use, add this to the front of the `c-hanging-semi&comma-criteria' list. |
|---|
| 934 |
|
|---|
| 935 |
|
|---|
| 936 |
(defun c-semi&comma-no-newlines-before-nonblanks () |
|---|
| 937 |
(save-excursion |
|---|
| 938 |
(if (and (eq last-command-char ?\;) |
|---|
| 939 |
(zerop (forward-line 1)) |
|---|
| 940 |
(not (looking-at "^[ \t]*$"))) |
|---|
| 941 |
'stop |
|---|
| 942 |
nil))) |
|---|
| 943 |
|
|---|
| 944 |
The function `c-semi&comma-inside-parenlist' is what prevents |
|---|
| 945 |
newlines from being inserted inside the parenthesis list of `for' |
|---|
| 946 |
statements. In addition to `c-semi&comma-no-newlines-before-nonblanks' |
|---|
| 947 |
described above, CC Mode also comes with the criteria function |
|---|
| 948 |
`c-semi&comma-no-newlines-for-oneline-inliners', which suppresses |
|---|
| 949 |
newlines after semicolons inside one-line inline method definitions |
|---|
| 950 |
(i.e. in C++ or Java). |
|---|
| 951 |
|
|---|
| 952 |
|
|---|
| 953 |
File: ccmode, Node: Other Special Indentations, Prev: Customizing Semi-colons and Commas, Up: Advanced Customizations |
|---|
| 954 |
|
|---|
| 955 |
Other Special Indentations |
|---|
| 956 |
-------------------------- |
|---|
| 957 |
|
|---|
| 958 |
In `gnu' style (*note Built-in Styles::), a minimum indentation is |
|---|
| 959 |
imposed on lines inside top-level constructs. This minimum indentation |
|---|
| 960 |
is controlled by the style variable `c-label-minimum-indentation'. The |
|---|
| 961 |
default value for this variable is 1. |
|---|
| 962 |
|
|---|
| 963 |
One other customization variable is available in CC Mode: The style |
|---|
| 964 |
variable `c-special-indent-hook'. This is a standard hook variable |
|---|
| 965 |
that is called after every line is indented by CC Mode. You can use it |
|---|
| 966 |
to do any special indentation or line adjustments your style dictates, |
|---|
| 967 |
such as adding extra indentation to constructors or destructor |
|---|
| 968 |
declarations in a class definition, etc. Note however, that you should |
|---|
| 969 |
not change point or mark inside your `c-special-indent-hook' functions |
|---|
| 970 |
(i.e. you'll probably want to wrap your function in a `save-excursion'). |
|---|
| 971 |
|
|---|
| 972 |
Setting `c-special-indent-hook' in your style definition is handled |
|---|
| 973 |
slightly differently than other variables. In your style definition, |
|---|
| 974 |
you should set the value for `c-special-indent-hook' to a function or |
|---|
| 975 |
list of functions, which will be appended to `c-special-indent-hook' |
|---|
| 976 |
using `add-hook'. That way, the current setting for the buffer local |
|---|
| 977 |
value of `c-special-indent-hook' won't be overridden. |
|---|
| 978 |
|
|---|
| 979 |
Normally, the standard Emacs command `M-;' (`indent-for-comment') |
|---|
| 980 |
will indent comment only lines to `comment-column'. Some users |
|---|
| 981 |
however, prefer that `M-;' act just like `TAB' for purposes of |
|---|
| 982 |
indenting comment-only lines; i.e. they want the comments to always |
|---|
| 983 |
indent as they would for normal code, regardless of whether `TAB' or |
|---|
| 984 |
`M-;' were used. This behavior is controlled by the variable |
|---|
| 985 |
`c-indent-comments-syntactically-p'. When `nil' (the default), `M-;' |
|---|
| 986 |
indents comment-only lines to `comment-column', otherwise, they are |
|---|
| 987 |
indented just as they would be if `TAB' were typed. |
|---|
| 988 |
|
|---|
| 989 |
Note that this has no effect for comment lines that are inserted with |
|---|
| 990 |
`M-;' at the end of regular code lines. These comments will always |
|---|
| 991 |
start at `comment-column'. |
|---|
| 992 |
|
|---|