| 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: Top, Next: Introduction, Prev: (dir), Up: (dir) |
|---|
| 33 |
|
|---|
| 34 |
CC Mode |
|---|
| 35 |
******* |
|---|
| 36 |
|
|---|
| 37 |
CC Mode is a GNU Emacs mode for editing files containing C, C++, |
|---|
| 38 |
Objective-C, Java, CORBA IDL, and Pike code. It provides syntax-based |
|---|
| 39 |
indentation and has several handy commands and some minor modes to make |
|---|
| 40 |
the editing easier. Note that CC Mode does _not_ provide font-locking; |
|---|
| 41 |
there are other Emacs packages for that. |
|---|
| 42 |
|
|---|
| 43 |
* Menu: |
|---|
| 44 |
|
|---|
| 45 |
* Introduction:: |
|---|
| 46 |
* Getting Connected:: |
|---|
| 47 |
* New Indentation Engine:: |
|---|
| 48 |
* Minor Modes:: |
|---|
| 49 |
* Text Filling and Line Breaking:: |
|---|
| 50 |
* Commands:: |
|---|
| 51 |
* Customizing Indentation:: |
|---|
| 52 |
* Syntactic Symbols:: |
|---|
| 53 |
* Indentation Functions:: |
|---|
| 54 |
* Performance Issues:: |
|---|
| 55 |
* Limitations and Known Bugs:: |
|---|
| 56 |
* Frequently Asked Questions:: |
|---|
| 57 |
* Getting the Latest CC Mode Release:: |
|---|
| 58 |
* Mailing Lists and Submitting Bug Reports:: |
|---|
| 59 |
* Sample .emacs File:: |
|---|
| 60 |
|
|---|
| 61 |
--- Indices --- |
|---|
| 62 |
|
|---|
| 63 |
* Concept Index:: |
|---|
| 64 |
* Command Index:: |
|---|
| 65 |
* Key Index:: |
|---|
| 66 |
* Variable Index:: |
|---|
| 67 |
|
|---|
| 68 |
--- The Detailed Node Listing --- |
|---|
| 69 |
|
|---|
| 70 |
New Indentation Engine |
|---|
| 71 |
|
|---|
| 72 |
* Syntactic Analysis:: |
|---|
| 73 |
* Indentation Calculation:: |
|---|
| 74 |
|
|---|
| 75 |
Minor Modes |
|---|
| 76 |
|
|---|
| 77 |
* Auto-newline Insertion:: |
|---|
| 78 |
* Hungry-deletion of Whitespace:: |
|---|
| 79 |
|
|---|
| 80 |
Auto-newline Insertion |
|---|
| 81 |
|
|---|
| 82 |
* Hanging Braces:: |
|---|
| 83 |
* Hanging Colons:: |
|---|
| 84 |
* Hanging Semi-colons and Commas:: |
|---|
| 85 |
* Other Electric Commands:: |
|---|
| 86 |
* Clean-ups:: |
|---|
| 87 |
|
|---|
| 88 |
Commands |
|---|
| 89 |
|
|---|
| 90 |
* Indentation Commands:: |
|---|
| 91 |
* Movement Commands:: |
|---|
| 92 |
* Other Commands:: |
|---|
| 93 |
|
|---|
| 94 |
Customizing Indentation |
|---|
| 95 |
|
|---|
| 96 |
* Interactive Customization:: |
|---|
| 97 |
* Permanent Customization:: |
|---|
| 98 |
* Hooks:: |
|---|
| 99 |
* Styles:: |
|---|
| 100 |
* Advanced Customizations:: |
|---|
| 101 |
|
|---|
| 102 |
Styles |
|---|
| 103 |
|
|---|
| 104 |
* Built-in Styles:: |
|---|
| 105 |
* Adding Styles:: |
|---|
| 106 |
* File Styles:: |
|---|
| 107 |
|
|---|
| 108 |
Advanced Customizations |
|---|
| 109 |
|
|---|
| 110 |
* Custom Indentation Functions:: |
|---|
| 111 |
* Custom Brace and Colon Hanging:: |
|---|
| 112 |
* Customizing Semi-colons and Commas:: |
|---|
| 113 |
* Other Special Indentations:: |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
File: ccmode, Node: Introduction, Next: Getting Connected, Prev: Top, Up: Top |
|---|
| 117 |
|
|---|
| 118 |
Introduction |
|---|
| 119 |
************ |
|---|
| 120 |
|
|---|
| 121 |
Welcome to CC Mode, a GNU Emacs mode for editing files containing C, |
|---|
| 122 |
C++, Objective-C, Java, CORBA IDL, and Pike code. This incarnation of |
|---|
| 123 |
the mode is descendant from `c-mode.el' (also called "Boring Old C |
|---|
| 124 |
Mode" or BOCM :-), and `c++-mode.el' version 2, which Barry has been |
|---|
| 125 |
maintaining since 1992. CC Mode represents a significant milestone in |
|---|
| 126 |
the mode's life. It has been fully merged back with Emacs 19's |
|---|
| 127 |
`c-mode.el'. Also a new, more intuitive and flexible mechanism for |
|---|
| 128 |
controlling indentation has been developed. Late in 1997, Martin |
|---|
| 129 |
joined the CC Mode Maintainers Team, and implemented the Pike support. |
|---|
| 130 |
As of 2000 Martin has taken over as the sole maintainer. |
|---|
| 131 |
|
|---|
| 132 |
This manual describes CC Mode version 5.28. |
|---|
| 133 |
|
|---|
| 134 |
CC Mode supports the editing of K&R and ANSI C, "ARM" (1) C++, |
|---|
| 135 |
Objective-C, Java, CORBA's Interface Definition Language, and Pike(2) |
|---|
| 136 |
files. In this way, you can easily set up consistent coding styles for |
|---|
| 137 |
use in editing all of these languages. CC Mode does _not_ handle |
|---|
| 138 |
font-locking (a.k.a. syntax coloring, keyword highlighting) or anything |
|---|
| 139 |
of that nature, for any of these modes. Font-locking is handled by |
|---|
| 140 |
other Emacs packages. |
|---|
| 141 |
|
|---|
| 142 |
This manual will describe the following: |
|---|
| 143 |
|
|---|
| 144 |
* How to get started using CC Mode. |
|---|
| 145 |
|
|---|
| 146 |
* How the new indentation engine works. |
|---|
| 147 |
|
|---|
| 148 |
* How to customize the new indentation engine. |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
Note that the name of this package is "CC Mode," but there is no top |
|---|
| 152 |
level `cc-mode' entry point. All of the variables, commands, and |
|---|
| 153 |
functions in CC Mode are prefixed with `c-<THING>', and `c-mode', |
|---|
| 154 |
`c++-mode', `objc-mode', `java-mode', `idl-mode', and `pike-mode' entry |
|---|
| 155 |
points are provided. This package is intended to be a replacement for |
|---|
| 156 |
`c-mode.el' and `c++-mode.el'. |
|---|
| 157 |
|
|---|
| 158 |
This distribution also contains a file called `cc-compat.el' which |
|---|
| 159 |
should ease your transition from BOCM to CC Mode. If you have a BOCM |
|---|
| 160 |
configuration you are really happy with, and want to postpone learning |
|---|
| 161 |
how to configure CC Mode, take a look at that file. It maps BOCM |
|---|
| 162 |
configuration variables to CC Mode's new indentation model. It is not |
|---|
| 163 |
actively supported so for the long run, you should learn how to |
|---|
| 164 |
customize CC Mode to support your coding style. |
|---|
| 165 |
|
|---|
| 166 |
A special word of thanks goes to Krishna Padmasola for his work in |
|---|
| 167 |
converting the original `README' file to Texinfo format. I'd also like |
|---|
| 168 |
to thank all the CC Mode victims who help enormously during the early |
|---|
| 169 |
beta stages of CC Mode's development. |
|---|
| 170 |
|
|---|
| 171 |
---------- Footnotes ---------- |
|---|
| 172 |
|
|---|
| 173 |
(1) `The Annotated C++ Reference Manual', by Ellis and Stroustrup. |
|---|
| 174 |
|
|---|
| 175 |
(2) A C-like scripting language with its roots in the LPC language |
|---|
| 176 |
used in some MUD engines. See `http://pike.idonex.se/'. |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
File: ccmode, Node: Getting Connected, Next: New Indentation Engine, Prev: Introduction, Up: Top |
|---|
| 180 |
|
|---|
| 181 |
Getting Connected |
|---|
| 182 |
***************** |
|---|
| 183 |
|
|---|
| 184 |
If you got this version of CC Mode with Emacs or XEmacs, it should |
|---|
| 185 |
work just fine right out of the box. Note however that you may not have |
|---|
| 186 |
the latest CC Mode release and may want to upgrade your copy. |
|---|
| 187 |
|
|---|
| 188 |
If you are upgrading an existing CC Mode installation, please see the |
|---|
| 189 |
`README' file for installation details. CC Mode may not work with |
|---|
| 190 |
older versions of Emacs or XEmacs. See the CC Mode release notes Web |
|---|
| 191 |
pages for the latest information on Emacs version and package |
|---|
| 192 |
compatibility (*note Getting the Latest CC Mode Release::). |
|---|
| 193 |
|
|---|
| 194 |
_Note that CC Mode no longer works with Emacs 18!_, so if you |
|---|
| 195 |
haven't upgraded from Emacs 18 by now, you are out of luck. |
|---|
| 196 |
|
|---|
| 197 |
You can find out what version of CC Mode you are using by visiting a |
|---|
| 198 |
C file and entering `M-x c-version RET'. You should see this message in |
|---|
| 199 |
the echo area: |
|---|
| 200 |
|
|---|
| 201 |
Using CC Mode version 5.XX |
|---|
| 202 |
|
|---|
| 203 |
where `XX' is the minor release number. |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
File: ccmode, Node: New Indentation Engine, Next: Minor Modes, Prev: Getting Connected, Up: Top |
|---|
| 207 |
|
|---|
| 208 |
New Indentation Engine |
|---|
| 209 |
********************** |
|---|
| 210 |
|
|---|
| 211 |
CC Mode has a new indentation engine, providing a simplified, yet |
|---|
| 212 |
flexible and general mechanism for customizing indentation. It separates |
|---|
| 213 |
indentation calculation into two steps: first, CC Mode analyzes the |
|---|
| 214 |
line of code being indented to determine the kind of language construct |
|---|
| 215 |
it's looking at, then it applies user defined offsets to the current |
|---|
| 216 |
line based on this analysis. |
|---|
| 217 |
|
|---|
| 218 |
This section will briefly cover how indentation is calculated in CC |
|---|
| 219 |
Mode. It is important to understand the indentation model being used so |
|---|
| 220 |
that you will know how to customize CC Mode for your personal coding |
|---|
| 221 |
style. |
|---|
| 222 |
|
|---|
| 223 |
* Menu: |
|---|
| 224 |
|
|---|
| 225 |
* Syntactic Analysis:: |
|---|
| 226 |
* Indentation Calculation:: |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
File: ccmode, Node: Syntactic Analysis, Next: Indentation Calculation, Up: New Indentation Engine |
|---|
| 230 |
|
|---|
| 231 |
Syntactic Analysis |
|---|
| 232 |
================== |
|---|
| 233 |
|
|---|
| 234 |
The first thing CC Mode does when indenting a line of code, is to |
|---|
| 235 |
analyze the line, determining the "syntactic component list" of the |
|---|
| 236 |
construct on that line. A syntactic component consists of a pair of |
|---|
| 237 |
information (in lisp parlance, a _cons cell_), where the first part is |
|---|
| 238 |
a "syntactic symbol", and the second part is a "relative buffer |
|---|
| 239 |
position". Syntactic symbols describe elements of C code (1), e.g. |
|---|
| 240 |
`statement', `substatement', `class-open', `class-close', etc. *Note |
|---|
| 241 |
Syntactic Symbols::, for a complete list of currently recognized |
|---|
| 242 |
syntactic symbols and their semantics. The style variable |
|---|
| 243 |
`c-offsets-alist' also contains the list of currently supported |
|---|
| 244 |
syntactic symbols. |
|---|
| 245 |
|
|---|
| 246 |
Conceptually, a line of C code is always indented relative to the |
|---|
| 247 |
indentation of some line higher up in the buffer. This is represented |
|---|
| 248 |
by the relative buffer position in the syntactic component. |
|---|
| 249 |
|
|---|
| 250 |
Here is an example. Suppose we had the following code as the only |
|---|
| 251 |
thing in a C++ buffer (2): |
|---|
| 252 |
|
|---|
| 253 |
1: void swap( int& a, int& b ) |
|---|
| 254 |
2: { |
|---|
| 255 |
3: int tmp = a; |
|---|
| 256 |
4: a = b; |
|---|
| 257 |
5: b = tmp; |
|---|
| 258 |
6: } |
|---|
| 259 |
|
|---|
| 260 |
We can use the command `C-c C-s' (`c-show-syntactic-information') to |
|---|
| 261 |
simply report what the syntactic analysis is for the current line. |
|---|
| 262 |
Running this command on line 4 of this example, we'd see in the echo |
|---|
| 263 |
area(3): |
|---|
| 264 |
|
|---|
| 265 |
((statement . 35)) |
|---|
| 266 |
|
|---|
| 267 |
This tells us that the line is a statement and it is indented |
|---|
| 268 |
relative to buffer position 35, which happens to be the `i' in `int' on |
|---|
| 269 |
line 3. If you were to move point to line 3 and hit `C-c C-s', you |
|---|
| 270 |
would see: |
|---|
| 271 |
|
|---|
| 272 |
((defun-block-intro . 29)) |
|---|
| 273 |
|
|---|
| 274 |
This indicates that the `int' line is the first statement in a top |
|---|
| 275 |
level function block, and is indented relative to buffer position 29, |
|---|
| 276 |
which is the brace just after the function header. |
|---|
| 277 |
|
|---|
| 278 |
Here's another example: |
|---|
| 279 |
|
|---|
| 280 |
1: int add( int val, int incr, int doit ) |
|---|
| 281 |
2: { |
|---|
| 282 |
3: if( doit ) |
|---|
| 283 |
4: { |
|---|
| 284 |
5: return( val + incr ); |
|---|
| 285 |
6: } |
|---|
| 286 |
7: return( val ); |
|---|
| 287 |
8: } |
|---|
| 288 |
|
|---|
| 289 |
Hitting `C-c C-s' on line 4 gives us: |
|---|
| 290 |
|
|---|
| 291 |
((substatement-open . 46)) |
|---|
| 292 |
|
|---|
| 293 |
which tells us that this is a brace that _opens_ a substatement block. |
|---|
| 294 |
(4) |
|---|
| 295 |
|
|---|
| 296 |
Syntactic component lists can contain more than one component, and |
|---|
| 297 |
individual syntactic components need not have relative buffer positions. |
|---|
| 298 |
The most common example of this is a line that contains a "comment only |
|---|
| 299 |
line". |
|---|
| 300 |
|
|---|
| 301 |
1: void draw_list( List<Drawables>& drawables ) |
|---|
| 302 |
2: { |
|---|
| 303 |
3: // call the virtual draw() method on each element in list |
|---|
| 304 |
4: for( int i=0; i < drawables.count(), ++i ) |
|---|
| 305 |
5: { |
|---|
| 306 |
6: drawables[i].draw(); |
|---|
| 307 |
7: } |
|---|
| 308 |
8: } |
|---|
| 309 |
|
|---|
| 310 |
Hitting `C-c C-s' on line 3 of this example gives: |
|---|
| 311 |
|
|---|
| 312 |
((comment-intro) (defun-block-intro . 46)) |
|---|
| 313 |
|
|---|
| 314 |
and you can see that the syntactic component list contains two syntactic |
|---|
| 315 |
components. Also notice that the first component, `(comment-intro)' |
|---|
| 316 |
has no relative buffer position. |
|---|
| 317 |
|
|---|
| 318 |
---------- Footnotes ---------- |
|---|
| 319 |
|
|---|
| 320 |
(1) Unless otherwise noted, the term "C code" to refers to all the |
|---|
| 321 |
C-like languages. |
|---|
| 322 |
|
|---|
| 323 |
(2) The line numbers in this and future examples don't actually |
|---|
| 324 |
appear in the buffer, of course! |
|---|
| 325 |
|
|---|
| 326 |
(3) With a universal argument (i.e. `C-u C-c C-s') the analysis is |
|---|
| 327 |
inserted into the buffer as a comment on the current line. |
|---|
| 328 |
|
|---|
| 329 |
(4) A "substatement" is the line after a conditional statement, such |
|---|
| 330 |
as `if', `else', `while', `do', `switch', etc. A "substatement block" |
|---|
| 331 |
is a brace block following one of these conditional statements. |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
File: ccmode, Node: Indentation Calculation, Prev: Syntactic Analysis, Up: New Indentation Engine |
|---|
| 335 |
|
|---|
| 336 |
Indentation Calculation |
|---|
| 337 |
======================= |
|---|
| 338 |
|
|---|
| 339 |
Indentation for a line is calculated using the syntactic component |
|---|
| 340 |
list derived in step 1 above (*note Syntactic Analysis::). Each |
|---|
| 341 |
component contributes to the final total indentation of the line in two |
|---|
| 342 |
ways. |
|---|
| 343 |
|
|---|
| 344 |
First, the syntactic symbols are looked up in the `c-offsets-alist' |
|---|
| 345 |
style variable, which is an association list of syntactic symbols and |
|---|
| 346 |
the offsets to apply for those symbols. These offsets are added to a |
|---|
| 347 |
running total. |
|---|
| 348 |
|
|---|
| 349 |
Second, if the component has a relative buffer position, CC Mode |
|---|
| 350 |
adds the column number of that position to the running total. By adding |
|---|
| 351 |
up the offsets and columns for every syntactic component on the list, |
|---|
| 352 |
the final total indentation for the current line is computed. |
|---|
| 353 |
|
|---|
| 354 |
Let's use our two code examples above to see how this works. Here is |
|---|
| 355 |
our first example again: |
|---|
| 356 |
|
|---|
| 357 |
1: void swap( int& a, int& b ) |
|---|
| 358 |
2: { |
|---|
| 359 |
3: int tmp = a; |
|---|
| 360 |
4: a = b; |
|---|
| 361 |
5: b = tmp; |
|---|
| 362 |
6: } |
|---|
| 363 |
|
|---|
| 364 |
Let's say point is on line 3 and we hit the `TAB' key to re-indent |
|---|
| 365 |
the line. Remember that the syntactic component list for that line is: |
|---|
| 366 |
|
|---|
| 367 |
((defun-block-intro . 29)) |
|---|
| 368 |
|
|---|
| 369 |
CC Mode looks up `defun-block-intro' in the `c-offsets-alist' style |
|---|
| 370 |
variable. Let's say it finds the value `4'; it adds this to the |
|---|
| 371 |
running total (initialized to zero), yielding a running total |
|---|
| 372 |
indentation of 4 spaces. |
|---|
| 373 |
|
|---|
| 374 |
Next CC Mode goes to buffer position 29 and asks for the current |
|---|
| 375 |
column. This brace is in column zero, so CC Mode adds `0' to the |
|---|
| 376 |
running total. Since there is only one syntactic component on the list |
|---|
| 377 |
for this line, indentation calculation is complete, and the total |
|---|
| 378 |
indentation for the line is 4 spaces. |
|---|
| 379 |
|
|---|
| 380 |
Here's another example: |
|---|
| 381 |
|
|---|
| 382 |
1: int add( int val, int incr, int doit ) |
|---|
| 383 |
2: { |
|---|
| 384 |
3: if( doit ) |
|---|
| 385 |
4: { |
|---|
| 386 |
5: return( val + incr ); |
|---|
| 387 |
6: } |
|---|
| 388 |
7: return( val ); |
|---|
| 389 |
8: } |
|---|
| 390 |
|
|---|
| 391 |
If we were to hit `TAB' on line 4 in the above example, the same |
|---|
| 392 |
basic process is performed, despite the differences in the syntactic |
|---|
| 393 |
component list. Remember that the list for this line is: |
|---|
| 394 |
|
|---|
| 395 |
((substatement-open . 46)) |
|---|
| 396 |
|
|---|
| 397 |
Here, CC Mode first looks up the `substatement-open' symbol in |
|---|
| 398 |
`c-offsets-alist'. Let's say it finds the value `4'. This yields a |
|---|
| 399 |
running total of 4. CC Mode then goes to buffer position 46, which is |
|---|
| 400 |
the `i' in `if' on line 3. This character is in the fourth column on |
|---|
| 401 |
that line so adding this to the running total yields an indentation for |
|---|
| 402 |
the line of 8 spaces. |
|---|
| 403 |
|
|---|
| 404 |
Simple, huh? |
|---|
| 405 |
|
|---|
| 406 |
Actually, the mode usually just does The Right Thing without you |
|---|
| 407 |
having to think about it in this much detail. But when customizing |
|---|
| 408 |
indentation, it's helpful to understand the general indentation model |
|---|
| 409 |
being used. |
|---|
| 410 |
|
|---|
| 411 |
As you configure CC Mode, you might want to set the variable |
|---|
| 412 |
`c-echo-syntactic-information-p' to non-`nil' so that the syntactic |
|---|
| 413 |
component list and calculated offset will always be echoed in the |
|---|
| 414 |
minibuffer when you hit `TAB'. |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
File: ccmode, Node: Minor Modes, Next: Text Filling and Line Breaking, Prev: New Indentation Engine, Up: Top |
|---|
| 418 |
|
|---|
| 419 |
Minor Modes |
|---|
| 420 |
*********** |
|---|
| 421 |
|
|---|
| 422 |
CC Mode contains two minor-mode-like features that you should find |
|---|
| 423 |
useful while you enter new C code. The first is called "auto-newline" |
|---|
| 424 |
mode, and the second is called "hungry-delete" mode. These minor modes |
|---|
| 425 |
can be toggled on and off independently, and CC Mode can be configured |
|---|
| 426 |
so that it starts up with any combination of these minor modes. By |
|---|
| 427 |
default, both of these minor modes are turned off. |
|---|
| 428 |
|
|---|
| 429 |
The state of the minor modes is always reflected in the minor mode |
|---|
| 430 |
list on the modeline of the CC Mode buffer. When auto-newline mode is |
|---|
| 431 |
enabled, you will see `C/a' on the mode line (1). When hungry delete |
|---|
| 432 |
mode is enabled you would see `C/h' and when both modes are enabled, |
|---|
| 433 |
you'd see `C/ah'. |
|---|
| 434 |
|
|---|
| 435 |
CC Mode provides key bindings which allow you to toggle the minor |
|---|
| 436 |
modes on the fly while editing code. To toggle just the auto-newline |
|---|
| 437 |
state, hit `C-c C-a' (`c-toggle-auto-state'). When you do this, you |
|---|
| 438 |
should see the `a' indicator either appear or disappear on the |
|---|
| 439 |
modeline. Similarly, to toggle just the hungry-delete state, use `C-c |
|---|
| 440 |
C-d' (`c-toggle-hungry-state'), and to toggle both states, use `C-c |
|---|
| 441 |
C-t' (`c-toggle-auto-hungry-state'). |
|---|
| 442 |
|
|---|
| 443 |
To set up the auto-newline and hungry-delete states to your preferred |
|---|
| 444 |
values, you would need to add some lisp to your `.emacs' file that |
|---|
| 445 |
called one of the `c-toggle-*-state' functions directly. When called |
|---|
| 446 |
programmatically, each function takes a numeric value, where a positive |
|---|
| 447 |
number enables the minor mode, a negative number disables the mode, and |
|---|
| 448 |
zero toggles the current state of the mode. |
|---|
| 449 |
|
|---|
| 450 |
So for example, if you wanted to enable both auto-newline and |
|---|
| 451 |
hungry-delete for all your C file editing, you could add the following |
|---|
| 452 |
to your `.emacs' file: |
|---|
| 453 |
|
|---|
| 454 |
(add-hook 'c-mode-common-hook |
|---|
| 455 |
(lambda () (c-toggle-auto-hungry-state 1))) |
|---|
| 456 |
|
|---|
| 457 |
* Menu: |
|---|
| 458 |
|
|---|
| 459 |
* Auto-newline Insertion:: |
|---|
| 460 |
* Hungry-deletion of Whitespace:: |
|---|
| 461 |
|
|---|
| 462 |
---------- Footnotes ---------- |
|---|
| 463 |
|
|---|
| 464 |
(1) The `C' would be replaced with `C++', `ObjC', `Java', `IDL', or |
|---|
| 465 |
`Pike' for the respective languages. |
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
File: ccmode, Node: Auto-newline Insertion, Next: Hungry-deletion of Whitespace, Up: Minor Modes |
|---|
| 469 |
|
|---|
| 470 |
Auto-newline Insertion |
|---|
| 471 |
====================== |
|---|
| 472 |
|
|---|
| 473 |
Auto-newline minor mode works by enabling certain "electric |
|---|
| 474 |
commands". Electric commands are typically bound to special characters |
|---|
| 475 |
such as the left and right braces, colons, semi-colons, etc., which when |
|---|
| 476 |
typed, perform some magic formatting in addition to inserting the typed |
|---|
| 477 |
character. As a general rule, electric commands are only electric when |
|---|
| 478 |
the following conditions apply: |
|---|
| 479 |
|
|---|
| 480 |
* Auto-newline minor mode is enabled, as evidenced by a `C/a' or |
|---|
| 481 |
`C/ah' indicator on the modeline. |
|---|
| 482 |
|
|---|
| 483 |
* The character was not typed inside of a literal (1). |
|---|
| 484 |
|
|---|
| 485 |
* No numeric argument was supplied to the command (i.e. it was typed |
|---|
| 486 |
as normal, with no `C-u' prefix). |
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
* Menu: |
|---|
| 490 |
|
|---|
| 491 |
* Hanging Braces:: |
|---|
| 492 |
* Hanging Colons:: |
|---|
| 493 |
* Hanging Semi-colons and Commas:: |
|---|
| 494 |
* Other Electric Commands:: |
|---|
| 495 |
* Clean-ups:: |
|---|
| 496 |
|
|---|
| 497 |
---------- Footnotes ---------- |
|---|
| 498 |
|
|---|
| 499 |
(1) A "literal" is defined as any comment, string, or C preprocessor |
|---|
| 500 |
macro definition. These constructs are also known as "syntactic |
|---|
| 501 |
whitespace" since they are usually ignored when scanning C code. |
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
File: ccmode, Node: Hanging Braces, Next: Hanging Colons, Up: Auto-newline Insertion |
|---|
| 505 |
|
|---|
| 506 |
Hanging Braces |
|---|
| 507 |
-------------- |
|---|
| 508 |
|
|---|
| 509 |
When you type either an open or close brace (i.e. `{' or `}'), the |
|---|
| 510 |
electric command `c-electric-brace' gets run. This command has two |
|---|
| 511 |
electric formatting behaviors. First, it will perform some |
|---|
| 512 |
re-indentation of the line the brace was typed on, and second, it will |
|---|
| 513 |
add various newlines before and/or after the typed brace. |
|---|
| 514 |
Re-indentation occurs automatically whenever the electric behavior is |
|---|
| 515 |
enabled. If the brace ends up on a line other than the one it was typed |
|---|
| 516 |
on, then that line is also re-indented. |
|---|
| 517 |
|
|---|
| 518 |
The default in auto-newline mode is to insert newlines both before |
|---|
| 519 |
and after a brace, but that can be controlled by the |
|---|
| 520 |
`c-hanging-braces-alist' style variable. This variable contains a |
|---|
| 521 |
mapping between syntactic symbols related to braces, and a list of |
|---|
| 522 |
places to insert a newline. The syntactic symbols that are useful for |
|---|
| 523 |
this list are: `class-open', `class-close', `defun-open', |
|---|
| 524 |
`defun-close', `inline-open', `inline-close', `brace-list-open', |
|---|
| 525 |
`brace-list-close', `brace-list-intro', `brace-entry-open', |
|---|
| 526 |
`block-open', `block-close', `substatement-open', |
|---|
| 527 |
`statement-case-open', `extern-lang-open', `extern-lang-close', |
|---|
| 528 |
`namespace-open', `namespace-close', `inexpr-class-open', and |
|---|
| 529 |
`inexpr-class-close'(1). *Note Syntactic Symbols::, for a more detailed |
|---|
| 530 |
description of these syntactic symbols, except for `inexpr-class-open' |
|---|
| 531 |
and `inexpr-class-close', which aren't actual syntactic symbols. |
|---|
| 532 |
|
|---|
| 533 |
The braces of anonymous inner classes in Java are given the special |
|---|
| 534 |
symbols `inexpr-class-open' and `inexpr-class-close', so that they can |
|---|
| 535 |
be distinguished from the braces of normal classes(2). |
|---|
| 536 |
|
|---|
| 537 |
The value associated with each syntactic symbol in this association |
|---|
| 538 |
list is called an ACTION which can be either a function or a list. |
|---|
| 539 |
*Note Custom Brace and Colon Hanging::, for a more detailed discussion |
|---|
| 540 |
of using a function as a brace hanging ACTION. |
|---|
| 541 |
|
|---|
| 542 |
When the ACTION is a list, it can contain any combination of the |
|---|
| 543 |
symbols `before' and `after', directing CC Mode where to put newlines |
|---|
| 544 |
in relationship to the brace being inserted. Thus, if the list |
|---|
| 545 |
contains only the symbol `after', then the brace is said to "hang" on |
|---|
| 546 |
the right side of the line, as in: |
|---|
| 547 |
|
|---|
| 548 |
// here, open braces always `hang' |
|---|
| 549 |
void spam( int i ) { |
|---|
| 550 |
if( i == 7 ) { |
|---|
| 551 |
dosomething(i); |
|---|
| 552 |
} |
|---|
| 553 |
} |
|---|
| 554 |
|
|---|
| 555 |
When the list contains both `after' and `before', the braces will |
|---|
| 556 |
appear on a line by themselves, as shown by the close braces in the |
|---|
| 557 |
above example. The list can also be empty, in which case no newlines |
|---|
| 558 |
are added either before or after the brace. |
|---|
| 559 |
|
|---|
| 560 |
If a syntactic symbol is missing entirely from |
|---|
| 561 |
`c-hanging-braces-alist', it's treated in the same way as an ACTION |
|---|
| 562 |
with a list containing `before' and `after', so that braces by default |
|---|
| 563 |
end up on their own line. |
|---|
| 564 |
|
|---|
| 565 |
For example, the default value of `c-hanging-braces-alist' is: |
|---|
| 566 |
|
|---|
| 567 |
((brace-list-open) |
|---|
| 568 |
(brace-entry-open) |
|---|
| 569 |
(substatement-open after) |
|---|
| 570 |
(block-close . c-snug-do-while) |
|---|
| 571 |
(extern-lang-open after) |
|---|
| 572 |
(inexpr-class-open after) |
|---|
| 573 |
(inexpr-class-close before)) |
|---|
| 574 |
|
|---|
| 575 |
which says that `brace-list-open' and `brace-entry-open' braces should |
|---|
| 576 |
both hang on the right side, and allow subsequent text to follow on the |
|---|
| 577 |
same line as the brace. Also, `substatement-open', `extern-lang-open', |
|---|
| 578 |
and `inexpr-class-open' braces should hang on the right side, but |
|---|
| 579 |
subsequent text should follow on the next line. The opposite holds for |
|---|
| 580 |
`inexpr-class-close' braces; they won't hang, but the following text |
|---|
| 581 |
continues on the same line. Here, in the `block-close' entry, you also |
|---|
| 582 |
see an example of using a function as an ACTION. In all other cases, |
|---|
| 583 |
braces are put on a line by themselves. |
|---|
| 584 |
|
|---|
| 585 |
A word of caution: it is not a good idea to hang top-level construct |
|---|
| 586 |
introducing braces, such as `class-open' or `defun-open'. Emacs makes |
|---|
| 587 |
an assumption that such braces will always appear in column zero, |
|---|
| 588 |
hanging them can introduce performance problems. *Note Performance |
|---|
| 589 |
Issues::, for more information. |
|---|
| 590 |
|
|---|
| 591 |
---------- Footnotes ---------- |
|---|
| 592 |
|
|---|
| 593 |
(1) Note that the aggregate constructs in Pike mode, `({', `})', |
|---|
| 594 |
`([', `])', and `(<', `>)', do not count as brace lists in this regard, |
|---|
| 595 |
even though they do for normal indentation purposes. It's currently |
|---|
| 596 |
not possible to set automatic newlines on these constructs. |
|---|
| 597 |
|
|---|
| 598 |
(2) The braces of anonymous classes produces a combination of |
|---|
| 599 |
`inexpr-class', and `class-open' or `class-close' in normal indentation |
|---|
| 600 |
analysis. |
|---|
| 601 |
|
|---|
| 602 |
|
|---|
| 603 |
File: ccmode, Node: Hanging Colons, Next: Hanging Semi-colons and Commas, Prev: Hanging Braces, Up: Auto-newline Insertion |
|---|
| 604 |
|
|---|
| 605 |
Hanging Colons |
|---|
| 606 |
-------------- |
|---|
| 607 |
|
|---|
| 608 |
Using a mechanism similar to brace hanging (*note Hanging Braces::), |
|---|
| 609 |
colons can also be made to hang using the style variable |
|---|
| 610 |
`c-hanging-colons-alist'. The syntactic symbols appropriate for this |
|---|
| 611 |
association list are: `case-label', `label', `access-label', |
|---|
| 612 |
`member-init-intro', and `inher-intro'. Note however that for |
|---|
| 613 |
`c-hanging-colons-alist', ACTIONs as functions are not supported. See |
|---|
| 614 |
also *Note Custom Brace and Colon Hanging:: for details. |
|---|
| 615 |
|
|---|
| 616 |
In C++, double-colons are used as a scope operator but because these |
|---|
| 617 |
colons always appear right next to each other, newlines before and after |
|---|
| 618 |
them are controlled by a different mechanism, called "clean-ups" in CC |
|---|
| 619 |
Mode. *Note Clean-ups::, for details. |
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 622 |
File: ccmode, Node: Hanging Semi-colons and Commas, Next: Other Electric Commands, Prev: Hanging Colons, Up: Auto-newline Insertion |
|---|
| 623 |
|
|---|
| 624 |
Hanging Semi-colons and Commas |
|---|
| 625 |
------------------------------ |
|---|
| 626 |
|
|---|
| 627 |
Semicolons and commas are also electric in CC Mode, but since these |
|---|
| 628 |
characters do not correspond directly to syntactic symbols, a different |
|---|
| 629 |
mechanism is used to determine whether newlines should be automatically |
|---|
| 630 |
inserted after these characters. *Note Customizing Semi-colons and |
|---|
| 631 |
Commas::, for details. |
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 |
File: ccmode, Node: Other Electric Commands, Next: Clean-ups, Prev: Hanging Semi-colons and Commas, Up: Auto-newline Insertion |
|---|
| 635 |
|
|---|
| 636 |
Other Electric Commands |
|---|
| 637 |
----------------------- |
|---|
| 638 |
|
|---|
| 639 |
A few other keys also provide electric behavior. For example `#' |
|---|
| 640 |
(`c-electric-pound') is electric when typed as the first non-whitespace |
|---|
| 641 |
character on a line. In this case, the variable |
|---|
| 642 |
`c-electric-pound-behavior' is consulted for the electric behavior. |
|---|
| 643 |
This variable takes a list value, although the only element currently |
|---|
| 644 |
defined is `alignleft', which tells this command to force the `#' |
|---|
| 645 |
character into column zero. This is useful for entering C preprocessor |
|---|
| 646 |
macro definitions. |
|---|
| 647 |
|
|---|
| 648 |
Stars and slashes (i.e. `*' and `/', `c-electric-star' and |
|---|
| 649 |
`c-electric-slash' respectively) are also electric under certain |
|---|
| 650 |
circumstances. If a star is inserted as the second character of a C |
|---|
| 651 |
style block comment on a comment-only line, then the comment delimiter |
|---|
| 652 |
is indented as defined by `c-offsets-alist'. A comment-only line is |
|---|
| 653 |
defined as a line which contains only a comment, as in: |
|---|
| 654 |
|
|---|
| 655 |
void spam( int i ) |
|---|
| 656 |
{ |
|---|
| 657 |
// this is a comment-only line... |
|---|
| 658 |
if( i == 7 ) // but this is not |
|---|
| 659 |
{ |
|---|
| 660 |
dosomething(i); |
|---|
| 661 |
} |
|---|
| 662 |
} |
|---|
| 663 |
|
|---|
| 664 |
Likewise, if a slash is inserted as the second slash in a C++ style |
|---|
| 665 |
line comment (also only on a comment-only line), then the line is |
|---|
| 666 |
indented as defined by `c-offsets-alist'. |
|---|
| 667 |
|
|---|
| 668 |
Less-than and greater-than signs (`c-electric-lt-gt') are also |
|---|
| 669 |
electric, but only in C++ mode. Hitting the second of two `<' or `>' |
|---|
| 670 |
keys re-indents the line if it is a C++ style stream operator. |
|---|
| 671 |
|
|---|
| 672 |
The normal parenthesis characters `(' and `)' also reindent the |
|---|
| 673 |
current line if they are used in normal code. This is useful for |
|---|
| 674 |
getting the closing parenthesis of an argument list aligned |
|---|
| 675 |
automatically. |
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
File: ccmode, Node: Clean-ups, Prev: Other Electric Commands, Up: Auto-newline Insertion |
|---|
| 679 |
|
|---|
| 680 |
Clean-ups |
|---|
| 681 |
--------- |
|---|
| 682 |
|
|---|
| 683 |
"Clean-ups" are mechanisms complementary to colon and brace hanging. |
|---|
| 684 |
On the surface, it would seem that clean-ups overlap the functionality |
|---|
| 685 |
provided by the `c-hanging-*-alist' variables. Clean-ups are however |
|---|
| 686 |
used to adjust code "after-the-fact," i.e. to adjust the whitespace in |
|---|
| 687 |
constructs after they are typed. |
|---|
| 688 |
|
|---|
| 689 |
Most of the clean-ups are only applicable to counteract automatically |
|---|
| 690 |
inserted newlines, and will therefore only have any effect if the |
|---|
| 691 |
auto-newline minor mode is turned on. Others will work all the time. |
|---|
| 692 |
|
|---|
| 693 |
You can configure CC Mode's clean-ups by setting the style variable |
|---|
| 694 |
`c-cleanup-list', which is a list of clean-up symbols. By default, CC |
|---|
| 695 |
Mode cleans up only the `scope-operator' construct, which is necessary |
|---|
| 696 |
for proper C++ support. Note that clean-ups are only performed when |
|---|
| 697 |
the construct does not occur within a literal (*note Auto-newline |
|---|
| 698 |
Insertion::), and when there is nothing but whitespace appearing |
|---|
| 699 |
between the individual components of the construct. |
|---|
| 700 |
|
|---|
| 701 |
These are the clean-ups that only are active in the auto-newline |
|---|
| 702 |
minor mode: |
|---|
| 703 |
|
|---|
| 704 |
* `brace-else-brace' -- Clean up `} else {' constructs by placing |
|---|
| 705 |
the entire construct on a single line. Clean-up occurs when the |
|---|
| 706 |
open brace after the `else' is typed. So for example, this: |
|---|
| 707 |
|
|---|
| 708 |
void spam(int i) |
|---|
| 709 |
{ |
|---|
| 710 |
if( i==7 ) |
|---|
| 711 |
{ |
|---|
| 712 |
dosomething(); |
|---|
| 713 |
} |
|---|
| 714 |
else |
|---|
| 715 |
{ |
|---|
| 716 |
|
|---|
| 717 |
appears like this after the open brace is typed: |
|---|
| 718 |
|
|---|
| 719 |
void spam(int i) |
|---|
| 720 |
{ |
|---|
| 721 |
if( i==7 ) { |
|---|
| 722 |
dosomething(); |
|---|
| 723 |
} else { |
|---|
| 724 |
|
|---|
| 725 |
* `brace-elseif-brace' -- Similar to the `brace-else-brace' |
|---|
| 726 |
clean-up, but this cleans up `} else if (...) {' constructs. For |
|---|
| 727 |
example: |
|---|
| 728 |
|
|---|
| 729 |
void spam(int i) |
|---|
| 730 |
{ |
|---|
| 731 |
if( i==7 ) |
|---|
| 732 |
{ |
|---|
| 733 |
dosomething(); |
|---|
| 734 |
} |
|---|
| 735 |
else if( i==3 ) |
|---|
| 736 |
{ |
|---|
| 737 |
|
|---|
| 738 |
appears like this after the open parenthesis is typed: |
|---|
| 739 |
|
|---|
| 740 |
void spam(int i) |
|---|
| 741 |
{ |
|---|
| 742 |
if( i==7 ) { |
|---|
| 743 |
dosomething(); |
|---|
| 744 |
} else if( i==3 ) |
|---|
| 745 |
{ |
|---|
| 746 |
|
|---|
| 747 |
and like this after the open brace is typed: |
|---|
| 748 |
|
|---|
| 749 |
void spam(int i) |
|---|
| 750 |
{ |
|---|
| 751 |
if( i==7 ) { |
|---|
| 752 |
dosomething(); |
|---|
| 753 |
} else if( i==3 ) { |
|---|
| 754 |
|
|---|
| 755 |
* `brace-catch-brace' -- Analogous to `brace-elseif-brace', but |
|---|
| 756 |
cleans up `} catch (...) {' in C++ and Java mode. |
|---|
| 757 |
|
|---|
| 758 |
* `empty-defun-braces' -- Clean up braces following a top-level |
|---|
| 759 |
function or class definition that contains no body. Clean up |
|---|
| 760 |
occurs when the closing brace is typed. Thus the following: |
|---|
| 761 |
|
|---|
| 762 |
class Spam |
|---|
| 763 |
{ |
|---|
| 764 |
} |
|---|
| 765 |
|
|---|
| 766 |
is transformed into this when the close brace is typed: |
|---|
| 767 |
|
|---|
| 768 |
class Spam |
|---|
| 769 |
{} |
|---|
| 770 |
|
|---|
| 771 |
* `defun-close-semi' -- Clean up the terminating semi-colon on |
|---|
| 772 |
top-level function or class definitions when they follow a close |
|---|
| 773 |
brace. Clean up occurs when the semi-colon is typed. So for |
|---|
| 774 |
example, the following: |
|---|
| 775 |
|
|---|
| 776 |
class Spam |
|---|
| 777 |
{ |
|---|
| 778 |
} |
|---|
| 779 |
; |
|---|
| 780 |
|
|---|
| 781 |
is transformed into this when the semi-colon is typed: |
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
class Spam |
|---|
| 785 |
{ |
|---|
| 786 |
}; |
|---|
| 787 |
|
|---|
| 788 |
* `list-close-comma' -- Clean up commas following braces in array |
|---|
| 789 |
and aggregate initializers. Clean up occurs when the comma is |
|---|
| 790 |
typed. |
|---|
| 791 |
|
|---|
| 792 |
* `scope-operator' -- Clean up double colons which may designate a |
|---|
| 793 |
C++ scope operator split across multiple lines(1). Clean up |
|---|
| 794 |
occurs when the second colon is typed. You will always want |
|---|
| 795 |
`scope-operator' in the `c-cleanup-list' when you are editing C++ |
|---|
| 796 |
code. |
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
The following clean-ups are always active when they occur on |
|---|
| 800 |
`c-cleanup-list', and are thus not affected by the auto-newline minor |
|---|
| 801 |
mode: |
|---|
| 802 |
|
|---|
| 803 |
* `space-before-funcall' -- Insert a space between the function name |
|---|
| 804 |
and the opening parenthesis of a function call. This produces |
|---|
| 805 |
function calls in the style mandated by the GNU coding standards, |
|---|
| 806 |
e.g. `signal (SIGINT, SIG_IGN)' and `abort ()'. Clean up occurs |
|---|
| 807 |
when the opening parenthesis is typed. |
|---|
| 808 |
|
|---|
| 809 |
* `compact-empty-funcall' -- Clean up any space between the function |
|---|
| 810 |
name and the opening parenthesis of a function call that have no |
|---|
| 811 |
arguments. This is typically used together with |
|---|
| 812 |
`space-before-funcall' if you prefer the GNU function call style |
|---|
| 813 |
for functions with arguments but think it looks ugly when it's |
|---|
| 814 |
only an empty parenthesis pair. I.e. you will get `signal (SIGINT, |
|---|
| 815 |
SIG_IGN)', but `abort()'. Clean up occurs when the closing |
|---|
| 816 |
parenthesis is typed. |
|---|
| 817 |
|
|---|
| 818 |
|
|---|
| 819 |
---------- Footnotes ---------- |
|---|
| 820 |
|
|---|
| 821 |
(1) Certain C++ constructs introduce ambiguous situations, so |
|---|
| 822 |
`scope-operator' clean-ups may not always be correct. This usually |
|---|
| 823 |
only occurs when scoped identifiers appear in switch label tags. |
|---|
| 824 |
|
|---|
| 825 |
|
|---|
| 826 |
File: ccmode, Node: Hungry-deletion of Whitespace, Prev: Auto-newline Insertion, Up: Minor Modes |
|---|
| 827 |
|
|---|
| 828 |
Hungry-deletion of Whitespace |
|---|
| 829 |
============================= |
|---|
| 830 |
|
|---|
| 831 |
Hungry deletion of whitespace, or as it more commonly called, |
|---|
| 832 |
"hungry-delete mode", is a simple feature that some people find |
|---|
| 833 |
extremely useful. In fact, you might find yourself wanting |
|---|
| 834 |
hungry-delete in *all* your editing modes! |
|---|
| 835 |
|
|---|
| 836 |
In a nutshell, when hungry-delete mode is enabled, hitting the |
|---|
| 837 |
<Backspace> key(1) will consume all preceding whitespace, including |
|---|
| 838 |
newlines and tabs. This can really cut down on the number of |
|---|
| 839 |
<Backspace>'s you have to type if, for example you made a mistake on |
|---|
| 840 |
the preceding line. |
|---|
| 841 |
|
|---|
| 842 |
By default, when you hit the <Backspace> key CC Mode runs the |
|---|
| 843 |
command `c-electric-backspace', which deletes text in the backwards |
|---|
| 844 |
direction. When deleting a single character, or when <Backspace> is |
|---|
| 845 |
hit in a literal (*note Auto-newline Insertion::), or when hungry-delete |
|---|
| 846 |
mode is disabled, the function contained in the `c-backspace-function' |
|---|
| 847 |
variable is called with one argument (the number of characters to |
|---|
| 848 |
delete). This variable is set to `backward-delete-char-untabify' by |
|---|
| 849 |
default. |
|---|
| 850 |
|
|---|
| 851 |
The default behavior of the <Delete> key depends on the flavor of |
|---|
| 852 |
Emacs you are using. By default in XEmacs 20.3 and beyond, the |
|---|
| 853 |
<Delete> key is bound to `c-electric-delete'. You control the |
|---|
| 854 |
direction that the <Delete> key deletes by setting the variable |
|---|
| 855 |
`delete-key-deletes-forward', a standard XEmacs variable. When this |
|---|
| 856 |
variable is non-`nil' and hungry-delete mode is enabled, |
|---|
| 857 |
`c-electric-delete' will consume all whitespace _following_ point. |
|---|
| 858 |
When `delete-key-deletes-forward' is `nil', it deletes all whitespace |
|---|
| 859 |
_preceding_ point(2) When deleting a single character, or if <Delete> |
|---|
| 860 |
is hit in a literal, or hungry-delete mode is disabled, the function |
|---|
| 861 |
contained in `c-delete-function' is called with one argument: the |
|---|
| 862 |
number of characters to delete. This variable is set to `delete-char' |
|---|
| 863 |
by default. |
|---|
| 864 |
|
|---|
| 865 |
In Emacs 19 or Emacs 20, both the <Delete> and <Backspace> keys are |
|---|
| 866 |
bound to `c-electric-backspace', however you can change this by |
|---|
| 867 |
explicitly binding `[delete]'(3). |
|---|
| 868 |
|
|---|
| 869 |
XEmacsen older than 20.3 behave similar to Emacs 19 and Emacs 20. |
|---|
| 870 |
|
|---|
| 871 |
---------- Footnotes ---------- |
|---|
| 872 |
|
|---|
| 873 |
(1) I say "hit the <Backspace> key" but what I really mean is "when |
|---|
| 874 |
Emacs receives the `BackSpace' key event." The difference usually |
|---|
| 875 |
isn't significant to most users, but advanced users will realize that |
|---|
| 876 |
under window systems such as X, any physical key (keycap) on the |
|---|
| 877 |
keyboard can be configured to generate any keysym, and thus any Emacs |
|---|
| 878 |
key event. Also, the use of Emacs on TTYs will affect which keycap |
|---|
| 879 |
generates which key event. From a pedantic point of view, here we are |
|---|
| 880 |
only concerned with the key event that Emacs receives. |
|---|
| 881 |
|
|---|
| 882 |
(2) i.e. it literally calls `c-electric-backspace'. |
|---|
| 883 |
|
|---|
| 884 |
(3) E.g. to `c-electric-delete' in your `.emacs' file. Note |
|---|
| 885 |
however, that Emacs 20 does not have a standard variable such as |
|---|
| 886 |
`delete-key-deletes-forward'. |
|---|
| 887 |
|
|---|
| 888 |
|
|---|
| 889 |
File: ccmode, Node: Text Filling and Line Breaking, Next: Commands, Prev: Minor Modes, Up: Top |
|---|
| 890 |
|
|---|
| 891 |
Text Filling and Line Breaking |
|---|
| 892 |
****************************** |
|---|
| 893 |
|
|---|
| 894 |
Since there's a lot of normal text in comments and string literals, |
|---|
| 895 |
CC Mode provides features to edit these like in text mode. The goal is |
|---|
| 896 |
to do it as seamlessly as possible, i.e. you can use auto fill mode, |
|---|
| 897 |
sentence and paragraph movement, paragraph filling, adaptive filling etc |
|---|
| 898 |
wherever there's a piece of normal text without having to think much |
|---|
| 899 |
about it. CC Mode should keep the indentation, fix the comment line |
|---|
| 900 |
decorations, and so on, for you. It does that by hooking in on the |
|---|
| 901 |
different line breaking functions and tuning relevant variables as |
|---|
| 902 |
necessary. |
|---|
| 903 |
|
|---|
| 904 |
To make Emacs recognize comments and treat text in them as normal |
|---|
| 905 |
paragraphs, CC Mode makes several standard variables(1) buffer local |
|---|
| 906 |
and modifies them according to the language syntax and the style of |
|---|
| 907 |
line decoration that starts every line in a comment. The style variable |
|---|
| 908 |
`c-comment-prefix-regexp' contains the regexp used to recognize this |
|---|
| 909 |
"comment line prefix". The default is `//+\\|\\**', which matches C++ |
|---|
| 910 |
style line comments like |
|---|
| 911 |
|
|---|
| 912 |
// blah blah |
|---|
| 913 |
|
|---|
| 914 |
with two or more slashes in front of them, and C style block comments |
|---|
| 915 |
like |
|---|
| 916 |
|
|---|
| 917 |
/* |
|---|
| 918 |
* blah blah |
|---|
| 919 |
*/ |
|---|
| 920 |
|
|---|
| 921 |
with zero or more stars at the beginning of every line. If you change |
|---|
| 922 |
that variable, please make sure it still matches the comment starter |
|---|
| 923 |
(i.e. `//') of line comments _and_ the line prefix inside block |
|---|
| 924 |
comments. Also note that since CC Mode uses the value of |
|---|
| 925 |
`c-comment-prefix-regexp' to set up several other variables at mode |
|---|
| 926 |
initialization, you need to reinitialize the program mode if you change |
|---|
| 927 |
it inside a CC Mode buffer. |
|---|
| 928 |
|
|---|
| 929 |
Line breaks are by default handled (almost) the same regardless |
|---|
| 930 |
whether they are made by auto fill mode (*note Auto Fill: (emacs)Auto |
|---|
| 931 |
Fill.), paragraph filling (e.g. with `M-q'), or explicitly with `M-j' |
|---|
| 932 |
or similar methods. In string literals, the new line gets the same |
|---|
| 933 |
indentation as the previous nonempty line (may be changed with the |
|---|
| 934 |
`string' syntactic symbol). In comments, CC Mode uses |
|---|
| 935 |
`c-comment-prefix-regexp' to adapt the line prefix from the other lines |
|---|
| 936 |
in the comment. |
|---|
| 937 |
|
|---|
| 938 |
CC Mode uses adaptive fill mode (*note Adaptive Fill: |
|---|
| 939 |
(emacs)Adaptive Fill.) to make Emacs correctly keep the line prefix |
|---|
| 940 |
when filling paragraphs. That also makes Emacs preserve the text |
|---|
| 941 |
indentation _inside_ the comment line prefix. E.g. in the following |
|---|
| 942 |
comment, both paragraphs will be filled with the left margins kept |
|---|
| 943 |
intact: |
|---|
| 944 |
|
|---|
| 945 |
/* Make a balanced b-tree of the nodes in the incoming |
|---|
| 946 |
* stream. But, to quote the famous words of Donald E. |
|---|
| 947 |
* Knuth, |
|---|
| 948 |
* |
|---|
| 949 |
* Beware of bugs in the above code; I have only |
|---|
| 950 |
* proved it correct, not tried it. |
|---|
| 951 |
*/ |
|---|
| 952 |
|
|---|
| 953 |
It's also possible to use other adaptive filling packages, notably |
|---|
| 954 |
Kyle E. Jones' Filladapt package(2), which handles things like bulleted |
|---|
| 955 |
lists nicely. There's a convenience function `c-setup-filladapt' that |
|---|
| 956 |
tunes the relevant variables in Filladapt for use in CC Mode. Call it |
|---|
| 957 |
from a mode hook, e.g. with something like this in your `.emacs': |
|---|
| 958 |
|
|---|
| 959 |
(defun my-c-mode-common-hook () |
|---|
| 960 |
(c-setup-filladapt) |
|---|
| 961 |
(filladapt-mode 1)) |
|---|
| 962 |
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook) |
|---|
| 963 |
|
|---|
| 964 |
Normally the comment line prefix inserted for a new line inside a |
|---|
| 965 |
comment is deduced from other lines in it. However there's one |
|---|
| 966 |
situation when there's no clue about how the prefix should look, namely |
|---|
| 967 |
when a block comment is broken for the first time. The string in the |
|---|
| 968 |
style variable `c-block-comment-prefix'(3) is used in that case. It |
|---|
| 969 |
defaults to `* ', which makes a comment |
|---|
| 970 |
|
|---|
| 971 |
/* Got O(n^2) here, which is a Bad Thing. */ |
|---|
| 972 |
|
|---|
| 973 |
break into |
|---|
| 974 |
|
|---|
| 975 |
/* Got O(n^2) here, |
|---|
| 976 |
* which is a Bad Thing. */ |
|---|
| 977 |
|
|---|
| 978 |
Note that it won't work to justify the indentation by putting leading |
|---|
| 979 |
spaces in the `c-block-comment-prefix' string, since CC Mode still uses |
|---|
| 980 |
the normal indentation engine to indent the line. Thus, the right way |
|---|
| 981 |
to fix the indentation is by setting the `c' syntactic symbol. It |
|---|
| 982 |
defaults to `c-lineup-C-comments', which handles the indentation of |
|---|
| 983 |
most common comment styles, see *Note Indentation Functions::. |
|---|
| 984 |
|
|---|
| 985 |
When auto fill mode is enabled, CC Mode can selectively ignore it |
|---|
| 986 |
depending on the context the line break would occur in, e.g. to never |
|---|
| 987 |
break a line automatically inside a string literal. This behavior can |
|---|
| 988 |
be controlled with the `c-ignore-auto-fill' variable. It takes a list |
|---|
| 989 |
of symbols for the different contexts where auto-filling never should |
|---|
| 990 |
occur: |
|---|
| 991 |
|
|---|
| 992 |
* `string' -- Inside a string or character literal. |
|---|
| 993 |
|
|---|
| 994 |
* `c' -- Inside a C style block comment. |
|---|
| 995 |
|
|---|
| 996 |
* `c++' -- Inside a C++ style line comment. |
|---|
| 997 |
|
|---|
| 998 |
* `cpp' -- Inside a preprocessor directive. |
|---|
| 999 |
|
|---|
| 1000 |
* `code' -- Anywhere else, i.e. in normal code. |
|---|
| 1001 |
|
|---|
| 1002 |
By default, `c-ignore-auto-fill' is set to `'(string cpp code)', |
|---|
| 1003 |
which means that auto-filling only occurs in comments when auto-fill |
|---|
| 1004 |
mode is activated. In literals, it's often desirable to have explicit |
|---|
| 1005 |
control over newlines. In preprocessor directives, the necessary `\' |
|---|
| 1006 |
escape character before the newline is not automatically inserted, so |
|---|
| 1007 |
an automatic line break would produce invalid code. In normal code, |
|---|
| 1008 |
line breaks are normally dictated by some logical structure in the code |
|---|
| 1009 |
rather than the last whitespace character, so automatic line breaks |
|---|
| 1010 |
there will produce poor results in the current implementation. |
|---|
| 1011 |
|
|---|
| 1012 |
The commands that does the actual work follows. |
|---|
| 1013 |
|
|---|
| 1014 |
`M-q' (`c-fill-paragraph') |
|---|
| 1015 |
This is the replacement for `fill-paragraph' in CC Mode buffers. |
|---|
| 1016 |
It's used to fill multiline string literals and both block and |
|---|
| 1017 |
line style comments. In Java buffers, the Javadoc markup words are |
|---|
| 1018 |
recognized as paragraph starters. The line oriented Pike autodoc |
|---|
| 1019 |
markup words are recognized in the same way in Pike mode. |
|---|
| 1020 |
|
|---|
| 1021 |
The function keeps the comment starters and enders of block |
|---|
| 1022 |
comments as they were before the filling. This means that a |
|---|
| 1023 |
comment ender on the same line as the paragraph being filled will |
|---|
| 1024 |
be filled with the paragraph, and one on a line by itself will |
|---|
| 1025 |
stay as it is. The comment starter is handled similarly(4). |
|---|
| 1026 |
|
|---|
| 1027 |
`M-j' (`c-indent-new-comment-line') |
|---|
| 1028 |
This is the replacement for `indent-new-comment-line'. It breaks |
|---|
| 1029 |
the line at point and indents the new line like the current one. |
|---|
| 1030 |
|
|---|
| 1031 |
If inside a comment and `comment-multi-line' is non-`nil', the |
|---|
| 1032 |
indentation and line prefix are preserved. If inside a comment and |
|---|
| 1033 |
`comment-multi-line' is `nil', a new comment of the same type is |
|---|
| 1034 |
started on the next line and indented as appropriate for comments. |
|---|
| 1035 |
|
|---|
| 1036 |
`M-x c-context-line-break' |
|---|
| 1037 |
This is a function that works like `indent-new-comment-line' in |
|---|
| 1038 |
comments and `newline-and-indent' elsewhere, thus combining those |
|---|
| 1039 |
two in a way that uses each one in the context it's best suited |
|---|
| 1040 |
for. I.e. in comments the comment line prefix and indentation is |
|---|
| 1041 |
kept for the new line, and in normal code it's indented according |
|---|
| 1042 |
to context by the indentation engine. |
|---|
| 1043 |
|
|---|
| 1044 |
It's not bound to a key by default, but it's intended to be used |
|---|
| 1045 |
on the `RET' key. If you like the behavior of |
|---|
| 1046 |
`newline-and-indent' on `RET', you might consider switching to |
|---|
| 1047 |
this function. |
|---|
| 1048 |
|
|---|
| 1049 |
---------- Footnotes ---------- |
|---|
| 1050 |
|
|---|
| 1051 |
(1) `comment-start', `comment-end', `comment-start-skip', |
|---|
| 1052 |
`paragraph-start', `paragraph-separate', `paragraph-ignore-fill-prefix', |
|---|
| 1053 |
`adaptive-fill-mode', `adaptive-fill-regexp', and |
|---|
| 1054 |
`adaptive-fill-first-line-regexp'. |
|---|
| 1055 |
|
|---|
| 1056 |
(2) It's available from `http://www.wonderworks.com/'. As of |
|---|
| 1057 |
version 2.12, it does however lack a feature that makes it work |
|---|
| 1058 |
suboptimally when `c-comment-prefix-regexp' matches the empty string |
|---|
| 1059 |
(which it does by default). A patch for that is available from the CC |
|---|
| 1060 |
Mode site. |
|---|
| 1061 |
|
|---|
| 1062 |
(3) In versions before 5.26, this variable was called |
|---|
| 1063 |
`c-comment-continuation-stars'. As a compatibility measure, CC Mode |
|---|
| 1064 |
still uses the value on that variable if it's set. |
|---|
| 1065 |
|
|---|
| 1066 |
(4) This means that the variables `c-hanging-comment-starter-p' and |
|---|
| 1067 |
`c-hanging-comment-ender-p', which controlled this behavior in earlier |
|---|
| 1068 |
versions of CC Mode, are now obsolete. |
|---|
| 1069 |
|
|---|
| 1070 |
|
|---|
| 1071 |
File: ccmode, Node: Commands, Next: Customizing Indentation, Prev: Text Filling and Line Breaking, Up: Top |
|---|
| 1072 |
|
|---|
| 1073 |
Commands |
|---|
| 1074 |
******** |
|---|
| 1075 |
|
|---|
| 1076 |
* Menu: |
|---|
| 1077 |
|
|---|
| 1078 |
* Indentation Commands:: |
|---|
| 1079 |
* Movement Commands:: |
|---|
| 1080 |
* Other Commands:: |
|---|
| 1081 |
|
|---|
| 1082 |
See also *Note Text Filling and Line Breaking::, for commands |
|---|
| 1083 |
concerning that bit. |
|---|
| 1084 |
|
|---|
| 1085 |
|
|---|
| 1086 |
File: ccmode, Node: Indentation Commands, Next: Movement Commands, Up: Commands |
|---|
| 1087 |
|
|---|
| 1088 |
Indentation Commands |
|---|
| 1089 |
==================== |
|---|
| 1090 |
|
|---|
| 1091 |
The following list of commands re-indent C constructs. Note that |
|---|
| 1092 |
when you change your coding style, either interactively or through some |
|---|
| 1093 |
other means, your file does _not_ automatically get re-indented. You |
|---|
| 1094 |
will need to execute one of the following commands to see the effects of |
|---|
| 1095 |
your changes. |
|---|
| 1096 |
|
|---|
| 1097 |
Also, variables like `c-hanging-*' and `c-cleanup-list' only affect |
|---|
| 1098 |
how on-the-fly code is formatted. Changing the "hanginess" of a brace |
|---|
| 1099 |
and then re-indenting, will not move the brace to a different line. |
|---|
| 1100 |
For this, you're better off getting an external program like GNU |
|---|
| 1101 |
`indent', which will re-arrange brace location, among other things. |
|---|
| 1102 |
|
|---|
| 1103 |
Re-indenting large sections of code can take a long time. When CC |
|---|
| 1104 |
Mode reindents a region of code, it is essentially equivalent to |
|---|
| 1105 |
hitting `TAB' on every line of the region. Especially vulnerable is |
|---|
| 1106 |
code generator output(1). |
|---|
| 1107 |
|
|---|
| 1108 |
These commands are useful when indenting code: |
|---|
| 1109 |
|
|---|
| 1110 |
`TAB' (`c-indent-command') |
|---|
| 1111 |
Indents the current line. The actual behavior is controlled by |
|---|
| 1112 |
several variables, described below. See `c-tab-always-indent', |
|---|
| 1113 |
`c-insert-tab-function', and `indent-tabs-mode'. With a numeric |
|---|
| 1114 |
argument, this command rigidly indents the region, preserving the |
|---|
| 1115 |
relative indentation among the lines. |
|---|
| 1116 |
|
|---|
| 1117 |
`M-C-q' (`c-indent-exp') |
|---|
| 1118 |
Indent an entire balanced brace or parenthesis expression. Note |
|---|
| 1119 |
that point must be on the opening brace or parenthesis of the |
|---|
| 1120 |
expression you want to indent. |
|---|
| 1121 |
|
|---|
| 1122 |
`C-c C-q' (`c-indent-defun') |
|---|
| 1123 |
Indents the entire top-level function or class definition |
|---|
| 1124 |
encompassing point. It leaves point unchanged. This function |
|---|
| 1125 |
can't be used to re-indent a nested brace construct, such as a |
|---|
| 1126 |
nested class or function, or a Java method. The top-level |
|---|
| 1127 |
construct being re-indented must be complete, i.e. it must have |
|---|
| 1128 |
both a beginning brace and an ending brace. |
|---|
| 1129 |
|
|---|
| 1130 |
`M-C-\' (`indent-region') |
|---|
| 1131 |
Indents an arbitrary region of code. This is a standard Emacs |
|---|
| 1132 |
command, tailored for C code in a CC Mode buffer. Note that of |
|---|
| 1133 |
course, point and mark must delineate the region you want to |
|---|
| 1134 |
indent. |
|---|
| 1135 |
|
|---|
| 1136 |
`M-C-h' (`c-mark-function') |
|---|
| 1137 |
While not strictly an indentation command, this is useful for |
|---|
| 1138 |
marking the current top-level function or class definition as the |
|---|
| 1139 |
current region. As with `c-indent-defun', this command operates on |
|---|
| 1140 |
top-level constructs, and can't be used to mark say, a Java method. |
|---|
| 1141 |
|
|---|
| 1142 |
These variables are also useful when indenting code: |
|---|
| 1143 |
|
|---|
| 1144 |
`c-tab-always-indent' |
|---|
| 1145 |
This variable controls how `TAB' `c-indent-command' operates. |
|---|
| 1146 |
When this variable is `t', `TAB' always just indents the current |
|---|
| 1147 |
line. When it is `nil', the line is indented only if point is at |
|---|
| 1148 |
the left margin, or on or before the first non-whitespace |
|---|
| 1149 |
character on the line, otherwise some whitespace is inserted. If |
|---|
| 1150 |
this variable is the symbol `other', then some whitespace is |
|---|
| 1151 |
inserted only within strings and comments (literals), an inside |
|---|
| 1152 |
preprocessor directives, but the line is always reindented. |
|---|
| 1153 |
|
|---|
| 1154 |
`c-insert-tab-function' |
|---|
| 1155 |
When "some whitespace" is inserted as described above, what |
|---|
| 1156 |
actually happens is that the function stored in |
|---|
| 1157 |
`c-insert-tab-function' is called. Normally, this just inserts a |
|---|
| 1158 |
real tab character, or the equivalent number of spaces, depending |
|---|
| 1159 |
on `indent-tabs-mode'. Some people, however, set |
|---|
| 1160 |
`c-insert-tab-function' to `tab-to-t |
|---|