]> code.delx.au - gnu-emacs/blob - man/programs.texi
(Authors): New node.
[gnu-emacs] / man / programs.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985,86,87,93,94,95,97,99,2000 Free Software Foundation, Inc.
3 @c See file emacs.texi for copying conditions.
4 @node Programs, Building, Text, Top
5 @chapter Editing Programs
6 @cindex Lisp editing
7 @cindex C editing
8 @cindex program editing
9
10 Emacs has many commands designed to understand the syntax of programming
11 languages such as Lisp and C. These commands can
12
13 @itemize @bullet
14 @item
15 Move over or kill balanced expressions or @dfn{sexps} (@pxref{Lists}).
16 @item
17 Move over or mark top-level expressions---@dfn{defuns}, in Lisp;
18 functions, in C (@pxref{Defuns}).
19 @item
20 Show how parentheses balance (@pxref{Matching}).
21 @item
22 Insert, kill or align comments (@pxref{Comments}).
23 @item
24 Follow the usual indentation conventions of the language
25 (@pxref{Program Indent}).
26 @end itemize
27
28 The commands for words, sentences and paragraphs are very useful in
29 editing code even though their canonical application is for editing
30 human language text. Most symbols contain words (@pxref{Words});
31 sentences can be found in strings and comments (@pxref{Sentences}).
32 Paragraphs per se don't exist in code, but the paragraph commands are
33 useful anyway, because programming language major modes define
34 paragraphs to begin and end at blank lines (@pxref{Paragraphs}).
35 Judicious use of blank lines to make the program clearer will also
36 provide useful chunks of text for the paragraph commands to work
37 on.
38
39 @cindex selective display
40 @cindex outline
41 @cindex folding
42 @findex outline-minor-mode
43 @cindex outlines
44 The selective display feature is useful for looking at the overall
45 structure of a function (@pxref{Selective Display}). This feature
46 causes only the lines that are indented less than a specified amount to
47 appear on the screen. Programming modes often support Outline minor
48 mode (@pxref{Outline Mode}). The Foldout package (@pxref{Foldout}) can
49 provide convenient folding-editor features on top of the minor mode.
50 The Hideshow package (@pxref{Hideshow}) can also be used to display
51 bocks of code selectively.
52
53 The `automatic typing' features may be useful when writing programs.
54 @xref{Top, Autotyping, autotype, Features for Automatic Typing}.
55
56 @menu
57 * Program Modes:: Major modes for editing programs.
58 * Lists:: Expressions with balanced parentheses.
59 * List Commands:: The commands for working with list and sexps.
60 * Defuns:: Each program is made up of separate functions.
61 There are editing commands to operate on them.
62 * Program Indent:: Adjusting indentation to show the nesting.
63 * Matching:: Insertion of a close-delimiter flashes matching open.
64 * Comments:: Inserting, killing, and aligning comments.
65 * Balanced Editing:: Inserting two matching parentheses at once, etc.
66 * Symbol Completion:: Completion on symbol names of your program or language.
67 * Which Function:: Which Function mode shows which function you are in.
68 * Hideshow:: Displaying blocks selectively.
69 * Documentation:: Getting documentation of functions you plan to call.
70 * Change Log:: Maintaining a change history for your program.
71 * Authors:: Maintaining an @file{AUTHORS} file.
72 * Tags:: Go direct to any function in your program in one
73 command. Tags remembers which file it is in.
74 * Imenu:: Making buffer indexes as menus.
75 * Emerge:: A convenient way of merging two versions of a program.
76 * C Modes:: Special commands of C, C++, Objective-C,
77 Java, and Pike modes.
78 * Fortran:: Fortran mode and its special features.
79 * Asm Mode:: Asm mode and its special features.
80 @end menu
81
82 @node Program Modes
83 @section Major Modes for Programming Languages
84
85 @cindex modes for programming languages
86 @cindex Perl mode
87 @cindex Icon mode
88 @cindex Awk mode
89 @cindex Makefile mode
90 @cindex Tcl mode
91 @cindex CPerl mode
92 @cindex DSSSL mode
93 @cindex Octave mode
94 @cindex Metafont mode
95 @cindex Modula2 mode
96 @cindex Prolog mode
97 @cindex Simula mode
98 @cindex VHDL mode
99 @cindex M4 mode
100 @cindex Shell-script mode
101 Emacs also has major modes for the programming languages Lisp, Scheme
102 (a variant of Lisp) and the Scheme-based DSSSL expression language, Ada,
103 Awk, C, C++, Fortran (free and fixed format), Icon, IDLWAVE,
104 Java, Metafont (@TeX{}'s companion for font creation), Modula2,
105 Objective-C, Octave, Pascal, Perl, Pike, Prolog, Simula, VHDL, CORBA
106 IDL, and Tcl. There is also a major mode for makefiles, called Makefile
107 mode. An alternative mode for Perl is called CPerl mode. Modes
108 are available for scripts for the common Unix shells, VMS DCL and
109 MS-DOS/MS-Windows `BAT' files. In a similar fashion to programming
110 languages, modes are provided for editing various sorts of configuration
111 files.
112
113 Separate manuals are available for th modes for Ada (@pxref{Top, , Ada Mode,
114 ada-mode, Ada Mode}), C/C++/Objective C/Java/Corba IDL (@pxref{Top, , CC Mode,
115 ccmode, CC Mode}) and the IDLWAVE modes (@pxref{Top, , IDLWAVE,
116 idlwave, IDLWAVE User Manual}).
117
118 Ideally, a major mode should be implemented for each programming
119 language that you might want to edit with Emacs; but often the mode for
120 one language can serve for other syntactically similar languages. The
121 language modes that exist are those that someone decided to take the
122 trouble to write.
123
124 There are several forms of Lisp mode, which differ in the way they
125 interface to Lisp execution. @xref{Executing Lisp}.
126
127 Each of the programming language major modes defines the @key{TAB} key
128 to run an indentation function that knows the indentation conventions of
129 that language and updates the current line's indentation accordingly.
130 For example, in C mode @key{TAB} is bound to @code{c-indent-line}.
131 @kbd{C-j} is normally defined to do @key{RET} followed by @key{TAB};
132 thus, it too indents in a mode-specific fashion.
133
134 @kindex DEL @r{(programming modes)}
135 @findex backward-delete-char-untabify
136 In most programming languages, indentation is likely to vary from line to
137 line. So the major modes for those languages rebind @key{DEL} to treat a
138 tab as if it were the equivalent number of spaces (using the command
139 @code{backward-delete-char-untabify}). This makes it possible to rub out
140 indentation one column at a time without worrying whether it is made up of
141 spaces or tabs. Use @kbd{C-b C-d} to delete a tab character before point,
142 in these modes.
143
144 Programming language modes define paragraphs to be separated only by
145 blank lines, so that the paragraph commands remain useful. Auto Fill mode,
146 if enabled in a programming language major mode, indents the new lines
147 which it creates.
148
149 @cindex mode hook
150 @vindex c-mode-hook
151 @vindex lisp-mode-hook
152 @vindex emacs-lisp-mode-hook
153 @vindex lisp-interaction-mode-hook
154 @vindex scheme-mode-hook
155 Turning on a major mode runs a normal hook called the @dfn{mode hook},
156 which is the value of a Lisp variable. Each major mode has a mode hook,
157 and the hook's name is always made from the mode command's name by
158 adding @samp{-hook}. For example, turning on C mode runs the hook
159 @code{c-mode-hook}, while turning on Lisp mode runs the hook
160 @code{lisp-mode-hook}. @xref{Hooks}.
161
162 @node Lists
163 @section Lists and Sexps
164
165 @cindex Control-Meta
166 By convention, Emacs keys for dealing with balanced expressions are
167 usually Control-Meta characters. They tend to be analogous in
168 function to their Control and Meta equivalents. These commands are
169 usually thought of as pertaining to expressions in programming
170 languages, but can be useful with any language in which some sort of
171 parentheses exist (including human languages).
172
173 @cindex list
174 @cindex sexp
175 @cindex expression
176 @cindex parentheses, moving across
177 @cindex matching parenthesis, moving to
178 These commands fall into two classes. Some deal only with @dfn{lists}
179 (parenthetical groupings). They see nothing except parentheses, brackets,
180 braces (whichever ones must balance in the language you are working with),
181 and escape characters that might be used to quote those.
182
183 The other commands deal with expressions or @dfn{sexps}. The word `sexp'
184 is derived from @dfn{s-expression}, the ancient term for an expression in
185 Lisp. But in Emacs, the notion of `sexp' is not limited to Lisp. It
186 refers to an expression in whatever language your program is written in.
187 Each programming language has its own major mode, which customizes the
188 syntax tables so that expressions in that language count as sexps.
189
190 Sexps typically include symbols, numbers, and string constants, as well
191 as anything contained in parentheses, brackets or braces.
192
193 In languages that use prefix and infix operators, such as C, it is not
194 possible for all expressions to be sexps. For example, C mode does not
195 recognize @samp{foo + bar} as a sexp, even though it @emph{is} a C expression;
196 it recognizes @samp{foo} as one sexp and @samp{bar} as another, with the
197 @samp{+} as punctuation between them. This is a fundamental ambiguity:
198 both @samp{foo + bar} and @samp{foo} are legitimate choices for the sexp to
199 move over if point is at the @samp{f}. Note that @samp{(foo + bar)} is a
200 single sexp in C mode.
201
202 Some languages have obscure forms of expression syntax that nobody
203 has bothered to make Emacs understand properly.
204
205 @node List Commands
206 @section List And Sexp Commands
207
208 @c doublewidecommands
209 @table @kbd
210 @item C-M-f
211 Move forward over a sexp (@code{forward-sexp}).
212 @item C-M-b
213 Move backward over a sexp (@code{backward-sexp}).
214 @item C-M-k
215 Kill sexp forward (@code{kill-sexp}).
216 @item C-M-@key{DEL}
217 Kill sexp backward (@code{backward-kill-sexp}).
218 @item C-M-u
219 Move up and backward in list structure (@code{backward-up-list}).
220 @item C-M-d
221 Move down and forward in list structure (@code{down-list}).
222 @item C-M-n
223 Move forward over a list (@code{forward-list}).
224 @item C-M-p
225 Move backward over a list (@code{backward-list}).
226 @item C-M-t
227 Transpose expressions (@code{transpose-sexps}).
228 @item C-M-@@
229 Put mark after following expression (@code{mark-sexp}).
230 @end table
231
232 @kindex C-M-f
233 @kindex C-M-b
234 @findex forward-sexp
235 @findex backward-sexp
236 To move forward over a sexp, use @kbd{C-M-f} (@code{forward-sexp}). If
237 the first significant character after point is an opening delimiter
238 (@samp{(} in Lisp; @samp{(}, @samp{[} or @samp{@{} in C), @kbd{C-M-f}
239 moves past the matching closing delimiter. If the character begins a
240 symbol, string, or number, @kbd{C-M-f} moves over that.
241
242 The command @kbd{C-M-b} (@code{backward-sexp}) moves backward over a
243 sexp. The detailed rules are like those above for @kbd{C-M-f}, but with
244 directions reversed. If there are any prefix characters (single-quote,
245 backquote and comma, in Lisp) preceding the sexp, @kbd{C-M-b} moves back
246 over them as well. The sexp commands move across comments as if they
247 were whitespace in most modes.
248
249 @kbd{C-M-f} or @kbd{C-M-b} with an argument repeats that operation the
250 specified number of times; with a negative argument, it moves in the
251 opposite direction.
252
253 @kindex C-M-k
254 @findex kill-sexp
255 @kindex C-M-DEL
256 @findex backward-kill-sexp
257 Killing a whole sexp can be done with @kbd{C-M-k} (@code{kill-sexp})
258 or @kbd{C-M-@key{DEL}} (@code{backward-kill-sexp}). @kbd{C-M-k} kills
259 the characters that @kbd{C-M-f} would move over, and @kbd{C-M-@key{DEL}}
260 kills the characters that @kbd{C-M-b} would move over.
261
262 @kindex C-M-n
263 @kindex C-M-p
264 @findex forward-list
265 @findex backward-list
266 The @dfn{list commands} move over lists, as the sexp commands do, but skip
267 blithely over any number of other kinds of sexps (symbols, strings, etc.).
268 They are @kbd{C-M-n} (@code{forward-list}) and @kbd{C-M-p}
269 (@code{backward-list}). The main reason they are useful is that they
270 usually ignore comments (since the comments usually do not contain any
271 lists).@refill
272
273 @kindex C-M-u
274 @kindex C-M-d
275 @findex backward-up-list
276 @findex down-list
277 @kbd{C-M-n} and @kbd{C-M-p} stay at the same level in parentheses, when
278 that's possible. To move @emph{up} one (or @var{n}) levels, use @kbd{C-M-u}
279 (@code{backward-up-list}).
280 @kbd{C-M-u} moves backward up past one unmatched opening delimiter. A
281 positive argument serves as a repeat count; a negative argument reverses
282 direction of motion and also requests repetition, so it moves forward and
283 up one or more levels.@refill
284
285 To move @emph{down} in list structure, use @kbd{C-M-d}
286 (@code{down-list}). In Lisp mode, where @samp{(} is the only opening
287 delimiter, this is nearly the same as searching for a @samp{(}. An
288 argument specifies the number of levels of parentheses to go down.
289
290 @cindex transposition
291 @kindex C-M-t
292 @findex transpose-sexps
293 A somewhat random-sounding command which is nevertheless handy is
294 @kbd{C-M-t} (@code{transpose-sexps}), which drags the previous sexp
295 across the next one. An argument serves as a repeat count, and a
296 negative argument drags backwards (thus canceling out the effect of
297 @kbd{C-M-t} with a positive argument). An argument of zero, rather than
298 doing nothing, transposes the sexps ending after point and the mark.
299
300 @kindex C-M-@@
301 @findex mark-sexp
302 To set the region around the next sexp in the buffer, use @kbd{C-M-@@}
303 (@code{mark-sexp}), which sets mark at the same place that @kbd{C-M-f}
304 would move to. @kbd{C-M-@@} takes arguments like @kbd{C-M-f}. In
305 particular, a negative argument is useful for putting the mark at the
306 beginning of the previous sexp.
307
308 The list and sexp commands' understanding of syntax is completely
309 controlled by the syntax table. Any character can, for example, be
310 declared to be an opening delimiter and act like an open parenthesis.
311 @xref{Syntax}.
312
313 @node Defuns
314 @section Defuns
315 @cindex defuns
316
317 In Emacs, a parenthetical grouping at the top level in the buffer is
318 called a @dfn{defun}. The name derives from the fact that most top-level
319 lists in a Lisp file are instances of the special form @code{defun}, but
320 any top-level parenthetical grouping counts as a defun in Emacs parlance
321 regardless of what its contents are, and regardless of the programming
322 language in use. For example, in C, the body of a function definition is a
323 defun.
324
325 @c doublewidecommands
326 @table @kbd
327 @item C-M-a
328 Move to beginning of current or preceding defun
329 (@code{beginning-of-defun}).
330 @item C-M-e
331 Move to end of current or following defun (@code{end-of-defun}).
332 @item C-M-h
333 Put region around whole current or following defun (@code{mark-defun}).
334 @end table
335
336 @kindex C-M-a
337 @kindex C-M-e
338 @kindex C-M-h
339 @findex beginning-of-defun
340 @findex end-of-defun
341 @findex mark-defun
342 The commands to move to the beginning and end of the current defun are
343 @kbd{C-M-a} (@code{beginning-of-defun}) and @kbd{C-M-e} (@code{end-of-defun}).
344
345 @findex c-mark-function
346 If you wish to operate on the current defun, use @kbd{C-M-h}
347 (@code{mark-defun}) which puts point at the beginning and mark at the end
348 of the current or next defun. For example, this is the easiest way to get
349 ready to move the defun to a different place in the text. In C mode,
350 @kbd{C-M-h} runs the function @code{c-mark-function}, which is almost the
351 same as @code{mark-defun}; the difference is that it backs up over the
352 argument declarations, function name and returned data type so that the
353 entire C function is inside the region. @xref{Marking Objects}.
354
355 @cindex open-parenthesis in leftmost column
356 @cindex ( in leftmost column
357 Emacs assumes that any open-parenthesis found in the leftmost column
358 is the start of a defun. Therefore, @strong{never put an
359 open-parenthesis at the left margin in a Lisp file unless it is the
360 start of a top-level list. Never put an open-brace or other opening
361 delimiter at the beginning of a line of C code unless it starts the body
362 of a function.} The most likely problem case is when you want an
363 opening delimiter at the start of a line inside a string. To avoid
364 trouble, put an escape character (@samp{\}, in C and Emacs Lisp,
365 @samp{/} in some other Lisp dialects) before the opening delimiter. It
366 will not affect the contents of the string.
367
368 In the remotest past, the original Emacs found defuns by moving upward a
369 level of parentheses until there were no more levels to go up. This always
370 required scanning all the way back to the beginning of the buffer, even for
371 a small function. To speed up the operation, Emacs was changed to assume
372 that any @samp{(} (or other character assigned the syntactic class of
373 opening-delimiter) at the left margin is the start of a defun. This
374 heuristic is nearly always right and avoids the costly scan; however,
375 it mandates the convention described above.
376
377 @node Program Indent
378 @section Indentation for Programs
379 @cindex indentation for programs
380
381 The best way to keep a program properly indented is to use Emacs to
382 reindent it as you change it. Emacs has commands to indent properly
383 either a single line, a specified number of lines, or all of the lines
384 inside a single parenthetical grouping.
385
386 @menu
387 * Basic Indent:: Indenting a single line.
388 * Multi-line Indent:: Commands to reindent many lines at once.
389 * Lisp Indent:: Specifying how each Lisp function should be indented.
390 * C Indent:: Extra features for indenting C and related modes.
391 * Custom C Indent:: Controlling indentation style for C and related modes.
392 @end menu
393
394 Emacs also provides a Lisp pretty-printer in the library @code{pp}.
395 This program reformats a Lisp object with indentation chosen to look nice.
396
397 @node Basic Indent
398 @subsection Basic Program Indentation Commands
399
400 @c WideCommands
401 @table @kbd
402 @item @key{TAB}
403 Adjust indentation of current line.
404 @item C-j
405 Equivalent to @key{RET} followed by @key{TAB} (@code{newline-and-indent}).
406 @end table
407
408 @kindex TAB @r{(programming modes)}
409 @findex c-indent-line
410 @findex lisp-indent-line
411 The basic indentation command is @key{TAB}, which gives the current line
412 the correct indentation as determined from the previous lines. The
413 function that @key{TAB} runs depends on the major mode; it is @code{lisp-indent-line}
414 in Lisp mode, @code{c-indent-line} in C mode, etc. These functions
415 understand different syntaxes for different languages, but they all do
416 about the same thing. @key{TAB} in any programming-language major mode
417 inserts or deletes whitespace at the beginning of the current line,
418 independent of where point is in the line. If point is inside the
419 whitespace at the beginning of the line, @key{TAB} leaves it at the end of
420 that whitespace; otherwise, @key{TAB} leaves point fixed with respect to
421 the characters around it.
422
423 Use @kbd{C-q @key{TAB}} to insert a tab at point.
424
425 @kindex C-j
426 @findex newline-and-indent
427 When entering lines of new code, use @kbd{C-j} (@code{newline-and-indent}),
428 which is equivalent to a @key{RET} followed by a @key{TAB}. @kbd{C-j} creates
429 a blank line and then gives it the appropriate indentation.
430
431 @key{TAB} indents the second and following lines of the body of a
432 parenthetical grouping each under the preceding one; therefore, if you
433 alter one line's indentation to be nonstandard, the lines below will
434 tend to follow it. This behavior is convenient in cases where you have
435 overridden the standard result of @key{TAB} because you find it
436 unaesthetic for a particular line.
437
438 Remember that an open-parenthesis, open-brace or other opening delimiter
439 at the left margin is assumed by Emacs (including the indentation routines)
440 to be the start of a function. Therefore, you must never have an opening
441 delimiter in column zero that is not the beginning of a function, not even
442 inside a string. This restriction is vital for making the indentation
443 commands fast; you must simply accept it. @xref{Defuns}, for more
444 information on this.
445
446 @node Multi-line Indent
447 @subsection Indenting Several Lines
448
449 When you wish to reindent several lines of code which have been altered
450 or moved to a different level in the list structure, you have several
451 commands available.
452
453 @table @kbd
454 @item C-M-q
455 Reindent all the lines within one list (@code{indent-sexp}).
456 @item C-u @key{TAB}
457 Shift an entire list rigidly sideways so that its first line
458 is properly indented.
459 @item C-M-\
460 Reindent all lines in the region (@code{indent-region}).
461 @end table
462
463 @kindex C-M-q
464 @findex indent-sexp
465 You can reindent the contents of a single list by positioning point
466 before the beginning of it and typing @kbd{C-M-q} (@code{indent-sexp} in
467 Lisp mode, @code{c-indent-exp} in C mode; also bound to other suitable
468 commands in other modes). The indentation of the line the sexp starts on
469 is not changed; therefore, only the relative indentation within the list,
470 and not its position, is changed. To correct the position as well, type a
471 @key{TAB} before the @kbd{C-M-q}.
472
473 @kindex C-u TAB
474 If the relative indentation within a list is correct but the
475 indentation of its first line is not, go to that line and type @kbd{C-u
476 @key{TAB}}. @key{TAB} with a numeric argument reindents the current
477 line as usual, then reindents by the same amount all the lines in the
478 grouping starting on the current line. In other words, it reindents the
479 whole grouping rigidly as a unit. It is clever, though, and does not
480 alter lines that start inside strings, or C preprocessor lines when in C
481 mode.
482
483 Another way to specify the range to be reindented is with the region.
484 The command @kbd{C-M-\} (@code{indent-region}) applies @key{TAB} to
485 every line whose first character is between point and mark.
486
487 @node Lisp Indent
488 @subsection Customizing Lisp Indentation
489 @cindex customizing Lisp indentation
490
491 The indentation pattern for a Lisp expression can depend on the function
492 called by the expression. For each Lisp function, you can choose among
493 several predefined patterns of indentation, or define an arbitrary one with
494 a Lisp program.
495
496 The standard pattern of indentation is as follows: the second line of the
497 expression is indented under the first argument, if that is on the same
498 line as the beginning of the expression; otherwise, the second line is
499 indented underneath the function name. Each following line is indented
500 under the previous line whose nesting depth is the same.
501
502 @vindex lisp-indent-offset
503 If the variable @code{lisp-indent-offset} is non-@code{nil}, it overrides
504 the usual indentation pattern for the second line of an expression, so that
505 such lines are always indented @code{lisp-indent-offset} more columns than
506 the containing list.
507
508 @vindex lisp-body-indent
509 The standard pattern is overridden for certain functions. Functions
510 whose names start with @code{def} always indent the second line by
511 @code{lisp-body-indent} extra columns beyond the open-parenthesis
512 starting the expression.
513
514 The standard pattern can be overridden in various ways for individual
515 functions, according to the @code{lisp-indent-function} property of the
516 function name. There are four possibilities for this property:
517
518 @table @asis
519 @item @code{nil}
520 This is the same as no property; the standard indentation pattern is used.
521 @item @code{defun}
522 The pattern used for function names that start with @code{def} is used for
523 this function also.
524 @item a number, @var{number}
525 The first @var{number} arguments of the function are
526 @dfn{distinguished} arguments; the rest are considered the @dfn{body}
527 of the expression. A line in the expression is indented according to
528 whether the first argument on it is distinguished or not. If the
529 argument is part of the body, the line is indented @code{lisp-body-indent}
530 more columns than the open-parenthesis starting the containing
531 expression. If the argument is distinguished and is either the first
532 or second argument, it is indented @emph{twice} that many extra columns.
533 If the argument is distinguished and not the first or second argument,
534 the standard pattern is followed for that line.
535 @item a symbol, @var{symbol}
536 @var{symbol} should be a function name; that function is called to
537 calculate the indentation of a line within this expression. The
538 function receives two arguments:
539 @table @asis
540 @item @var{state}
541 The value returned by @code{parse-partial-sexp} (a Lisp primitive for
542 indentation and nesting computation) when it parses up to the
543 beginning of this line.
544 @item @var{pos}
545 The position at which the line being indented begins.
546 @end table
547 @noindent
548 It should return either a number, which is the number of columns of
549 indentation for that line, or a list whose car is such a number. The
550 difference between returning a number and returning a list is that a
551 number says that all following lines at the same nesting level should
552 be indented just like this one; a list says that following lines might
553 call for different indentations. This makes a difference when the
554 indentation is being computed by @kbd{C-M-q}; if the value is a
555 number, @kbd{C-M-q} need not recalculate indentation for the following
556 lines until the end of the list.
557 @end table
558
559 @node C Indent
560 @subsection Commands for C Indentation
561
562 Here are the commands for indentation in C mode and related modes:
563
564 @table @code
565 @item C-c C-q
566 @kindex C-c C-q @r{(C mode)}
567 @findex c-indent-defun
568 Reindent the current top-level function definition or aggregate type
569 declaration (@code{c-indent-defun}).
570
571 @item C-M-q
572 @kindex C-M-q @r{(C mode)}
573 @findex c-indent-exp
574 Reindent each line in the balanced expression that follows point
575 (@code{c-indent-exp}). A prefix argument inhibits error checking and
576 warning messages about invalid syntax.
577
578 @item @key{TAB}
579 @findex c-indent-command
580 Reindent the current line, and/or in some cases insert a tab character
581 (@code{c-indent-command}).
582
583 If @code{c-tab-always-indent} is @code{t}, this command always reindents
584 the current line and does nothing else. This is the default.
585
586 If that variable is @code{nil}, this command reindents the current line
587 only if point is at the left margin or in the line's indentation;
588 otherwise, it inserts a tab (or the equivalent number of spaces,
589 if @code{indent-tabs-mode} is @code{nil}).
590
591 Any other value (not @code{nil} or @code{t}) means always reindent the
592 line, and also insert a tab if within a comment, a string, or a
593 preprocessor directive.
594
595 @item C-u @key{TAB}
596 Reindent the current line according to its syntax; also rigidly reindent
597 any other lines of the expression that starts on the current line.
598 @xref{Multi-line Indent}.
599 @end table
600
601 To reindent the whole current buffer, type @kbd{C-x h C-M-\}. This
602 first selects the whole buffer as the region, then reindents that
603 region.
604
605 To reindent the current block, use @kbd{C-M-u C-M-q}. This moves
606 to the front of the block and then reindents it all.
607
608 @node Custom C Indent
609 @subsection Customizing C Indentation
610
611 C mode and related modes use a simple yet flexible mechanism for
612 customizing indentation. The mechanism works in two steps: first it
613 classifies the line syntactically according to its contents and context;
614 second, it associates each kind of syntactic construct with an
615 indentation offset which you can customize.
616
617 @menu
618 * Syntactic Analysis::
619 * Indentation Calculation::
620 * Changing Indent Style::
621 * Syntactic Symbols::
622 * Variables for C Indent::
623 * C Indent Styles::
624 @end menu
625
626 @node Syntactic Analysis
627 @subsubsection Step 1---Syntactic Analysis
628 @cindex syntactic analysis
629
630 In the first step, the C indentation mechanism looks at the line
631 before the one you are currently indenting and determines the syntactic
632 components of the construct on that line. It builds a list of these
633 syntactic components, each of which contains a @dfn{syntactic symbol}
634 and sometimes also a buffer position. Some syntactic symbols describe
635 grammatical elements, for example @code{statement} and
636 @code{substatement}; others describe locations amidst grammatical
637 elements, for example @code{class-open} and @code{knr-argdecl}.
638
639 Conceptually, a line of C code is always indented relative to the
640 indentation of some line higher up in the buffer. This is represented
641 by the buffer positions in the syntactic component list.
642
643 Here is an example. Suppose we have the following code in a C++ mode
644 buffer (the line numbers don't actually appear in the buffer):
645
646 @example
647 1: void swap (int& a, int& b)
648 2: @{
649 3: int tmp = a;
650 4: a = b;
651 5: b = tmp;
652 6: @}
653 @end example
654
655 If you type @kbd{C-c C-s} (which runs the command
656 @code{c-show-syntactic-information}) on line 4, it shows the result of
657 the indentation mechanism for that line:
658
659 @example
660 ((statement . 32))
661 @end example
662
663 This indicates that the line is a statement and it is indented
664 relative to buffer position 32, which happens to be the @samp{i} in
665 @code{int} on line 3. If you move the cursor to line 3 and type
666 @kbd{C-c C-s}, it displays this:
667
668 @example
669 ((defun-block-intro . 28))
670 @end example
671
672 This indicates that the @code{int} line is the first statement in a
673 block, and is indented relative to buffer position 28, which is the
674 brace just after the function header.
675
676 @noindent
677 Here is another example:
678
679 @example
680 1: int add (int val, int incr, int doit)
681 2: @{
682 3: if (doit)
683 4: @{
684 5: return (val + incr);
685 6: @}
686 7: return (val);
687 8: @}
688 @end example
689
690 @noindent
691 Typing @kbd{C-c C-s} on line 4 displays this:
692
693 @example
694 ((substatement-open . 43))
695 @end example
696
697 This says that the brace @emph{opens} a substatement block. By the
698 way, a @dfn{substatement} indicates the line after an @code{if},
699 @code{else}, @code{while}, @code{do}, @code{switch}, @code{for},
700 @code{try}, @code{catch}, @code{finally}, or @code{synchronized}
701 statement.
702
703 @cindex syntactic component
704 @cindex syntactic symbol
705 @vindex c-syntactic-context
706 Within the C indentation commands, after a line has been analyzed
707 syntactically for indentation, the variable @code{c-syntactic-context}
708 contains a list that describes the results. Each element in this list
709 is a @dfn{syntactic component}: a cons cell containing a syntactic
710 symbol and (optionally) its corresponding buffer position. There may be
711 several elements in a component list; typically only one element has a
712 buffer position.
713
714 @node Indentation Calculation
715 @subsubsection Step 2---Indentation Calculation
716 @cindex Indentation Calculation
717
718 The C indentation mechanism calculates the indentation for the current
719 line using the list of syntactic components, @code{c-syntactic-context},
720 derived from syntactic analysis. Each component is a cons cell that
721 contains a syntactic symbol and may also contain a buffer position.
722
723 Each component contributes to the final total indentation of the line
724 in two ways. First, the syntactic symbol identifies an element of
725 @code{c-offsets-alist}, which is an association list mapping syntactic
726 symbols into indentation offsets. Each syntactic symbol's offset adds
727 to the total indentation. Second, if the component includes a buffer
728 position, the column number of that position adds to the indentation.
729 All these offsets and column numbers, added together, give the total
730 indentation.
731
732 The following examples demonstrate the workings of the C indentation
733 mechanism:
734
735 @example
736 1: void swap (int& a, int& b)
737 2: @{
738 3: int tmp = a;
739 4: a = b;
740 5: b = tmp;
741 6: @}
742 @end example
743
744 Suppose that point is on line 3 and you type @key{TAB} to reindent the
745 line. As explained above (@pxref{Syntactic Analysis}), the syntactic
746 component list for that line is:
747
748 @example
749 ((defun-block-intro . 28))
750 @end example
751
752 In this case, the indentation calculation first looks up
753 @code{defun-block-intro} in the @code{c-offsets-alist} alist. Suppose
754 that it finds the integer 2; it adds this to the running total
755 (initialized to zero), yielding a updated total indentation of 2 spaces.
756
757 The next step is to find the column number of buffer position 28.
758 Since the brace at buffer position 28 is in column zero, this adds 0 to
759 the running total. Since this line has only one syntactic component,
760 the total indentation for the line is 2 spaces.
761
762 @example
763 1: int add (int val, int incr, int doit)
764 2: @{
765 3: if (doit)
766 4: @{
767 5: return(val + incr);
768 6: @}
769 7: return(val);
770 8: @}
771 @end example
772
773 If you type @key{TAB} on line 4, the same process is performed, but
774 with different data. The syntactic component list for this line is:
775
776 @example
777 ((substatement-open . 43))
778 @end example
779
780 Here, the indentation calculation's first job is to look up the
781 symbol @code{substatement-open} in @code{c-offsets-alist}. Let's assume
782 that the offset for this symbol is 2. At this point the running total
783 is 2 (0 + 2 = 2). Then it adds the column number of buffer position 43,
784 which is the @samp{i} in @code{if} on line 3. This character is in
785 column 2 on that line. Adding this yields a total indentation of 4
786 spaces.
787
788 @vindex c-strict-syntax-p
789 If a syntactic symbol in the analysis of a line does not appear in
790 @code{c-offsets-alist}, it is ignored; if in addition the variable
791 @code{c-strict-syntax-p} is non-@code{nil}, it is an error.
792
793 @node Changing Indent Style
794 @subsubsection Changing Indentation Style
795
796 There are two ways to customize the indentation style for the C-like
797 modes. First, you can select one of several predefined styles, each of
798 which specifies offsets for all the syntactic symbols. For more
799 flexibility, you can customize the handling of individual syntactic
800 symbols. @xref{Syntactic Symbols}, for a list of all defined syntactic
801 symbols.
802
803 @table @kbd
804 @item M-x c-set-style @key{RET} @var{style} @key{RET}
805 Select predefined indentation style @var{style}. Type @kbd{?} when
806 entering @var{style} to see a list of supported styles; to find out what
807 a style looks like, select it and reindent some C code.
808
809 @item C-c C-o @var{symbol} @key{RET} @var{offset} @key{RET}
810 Set the indentation offset for syntactic symbol @var{symbol}
811 (@code{c-set-offset}). The second argument @var{offset} specifies the
812 new indentation offset.
813 @end table
814
815 The @code{c-offsets-alist} variable controls the amount of
816 indentation to give to each syntactic symbol. Its value is an
817 association list, and each element of the list has the form
818 @code{(@var{syntactic-symbol} . @var{offset})}. By changing the offsets
819 for various syntactic symbols, you can customize indentation in fine
820 detail. To change this alist, use @code{c-set-offset} (see below).
821
822 Each offset value in @code{c-offsets-alist} can be an integer, a
823 function or variable name, a list, or one of the following symbols: @code{+},
824 @code{-}, @code{++}, @code{--}, @code{*}, or @code{/}, indicating positive or negative
825 multiples of the variable @code{c-basic-offset}. Thus, if you want to
826 change the levels of indentation to be 3 spaces instead of 2 spaces, set
827 @code{c-basic-offset} to 3.
828
829 Using a function as the offset value provides the ultimate flexibility
830 in customizing indentation. The function is called with a single
831 argument containing the @code{cons} of the syntactic symbol and
832 the buffer position, if any. The function should return an integer
833 offset.
834
835 If the offset value is a list, its elements are processed according
836 to the rules above until a non-@code{nil} value is found. That value is
837 then added to the total indentation in the normal manner. The primary
838 use for this is to combine the results of several functions.
839
840 @kindex C-c C-o @r{(C mode)}
841 @findex c-set-offset
842 The command @kbd{C-c C-o} (@code{c-set-offset}) is the easiest way to
843 set offsets, both interactively or in your @file{~/.emacs} file. First
844 specify the syntactic symbol, then the offset you want. @xref{Syntactic
845 Symbols}, for a list of valid syntactic symbols and their meanings.
846
847 @node Syntactic Symbols
848 @subsubsection Syntactic Symbols
849
850 Here is a table of valid syntactic symbols for indentation in C and
851 related modes, with their syntactic meanings. Normally, most of these
852 symbols are assigned offsets in @code{c-offsets-alist}.
853
854 @table @code
855 @item string
856 Inside a multi-line string.
857
858 @item c
859 Inside a multi-line C style block comment.
860
861 @item defun-open
862 On a brace that opens a function definition.
863
864 @item defun-close
865 On a brace that closes a function definition.
866
867 @item defun-block-intro
868 In the first line in a top-level defun.
869
870 @item class-open
871 On a brace that opens a class definition.
872
873 @item class-close
874 On a brace that closes a class definition.
875
876 @item inline-open
877 On a brace that opens an in-class inline method.
878
879 @item inline-close
880 On a brace that closes an in-class inline method.
881
882 @item extern-lang-open
883 On a brace that opens an external language block.
884
885 @item extern-lang-close
886 On a brace that closes an external language block.
887
888 @item func-decl-cont
889 The region between a function definition's argument list and the defun
890 opening brace (excluding K&R function definitions). In C, you cannot
891 put anything but whitespace and comments between them; in C++ and Java,
892 @code{throws} declarations and other things can appear in this context.
893
894 @item knr-argdecl-intro
895 On the first line of a K&R C argument declaration.
896
897 @item knr-argdecl
898 In one of the subsequent lines in a K&R C argument declaration.
899
900 @item topmost-intro
901 On the first line in a topmost construct definition.
902
903 @item topmost-intro-cont
904 On the topmost definition continuation lines.
905
906 @item member-init-intro
907 On the first line in a member initialization list.
908
909 @item member-init-cont
910 On one of the subsequent member initialization list lines.
911
912 @item inher-intro
913 On the first line of a multiple inheritance list.
914
915 @item inher-cont
916 On one of the subsequent multiple inheritance lines.
917
918 @item block-open
919 On a statement block open brace.
920
921 @item block-close
922 On a statement block close brace.
923
924 @item brace-list-open
925 On the opening brace of an @code{enum} or @code{static} array list.
926
927 @item brace-list-close
928 On the closing brace of an @code{enum} or @code{static} array list.
929
930 @item brace-list-intro
931 On the first line in an @code{enum} or @code{static} array list.
932
933 @item brace-list-entry
934 On one of the subsequent lines in an @code{enum} or @code{static} array
935 list.
936
937 @item brace-entry-open
938 On one of the subsequent lines in an @code{enum} or @code{static} array
939 list, when the line begins with an open brace.
940
941 @item statement
942 On an ordinary statement.
943
944 @item statement-cont
945 On a continuation line of a statement.
946
947 @item statement-block-intro
948 On the first line in a new statement block.
949
950 @item statement-case-intro
951 On the first line in a @code{case} ``block.''
952
953 @item statement-case-open
954 On the first line in a @code{case} block starting with brace.
955
956 @item inexpr-statement
957 On a statement block inside an expression. This is used for a GNU
958 extension to the C language, and for Pike special functions that take a
959 statement block as an argument.
960
961 @item inexpr-class
962 On a class definition inside an expression. This is used for anonymous
963 classes and anonymous array initializers in Java.
964
965 @item substatement
966 On the first line after an @code{if}, @code{while}, @code{for},
967 @code{do}, or @code{else}.
968
969 @item substatement-open
970 On the brace that opens a substatement block.
971
972 @item case-label
973 On a @code{case} or @code{default} label.
974
975 @item access-label
976 On a C++ @code{private}, @code{protected}, or @code{public} access label.
977
978 @item label
979 On any ordinary label.
980
981 @item do-while-closure
982 On the @code{while} that ends a @code{do}-@code{while} construct.
983
984 @item else-clause
985 On the @code{else} of an @code{if}-@code{else} construct.
986
987 @item catch-clause
988 On the @code{catch} and @code{finally} lines in
989 @code{try}@dots{}@code{catch} constructs in C++ and Java.
990
991 @item comment-intro
992 On a line containing only a comment introduction.
993
994 @item arglist-intro
995 On the first line in an argument list.
996
997 @item arglist-cont
998 On one of the subsequent argument list lines when no arguments follow on
999 the same line as the arglist opening parenthesis.
1000
1001 @item arglist-cont-nonempty
1002 On one of the subsequent argument list lines when at least one argument
1003 follows on the same line as the arglist opening parenthesis.
1004
1005 @item arglist-close
1006 On the closing parenthesis of an argument list.
1007
1008 @item stream-op
1009 On one of the lines continuing a stream operator construct.
1010
1011 @item inclass
1012 On a construct that is nested inside a class definition. The
1013 indentation is relative to the open brace of the class definition.
1014
1015 @item inextern-lang
1016 On a construct that is nested inside an external language block.
1017
1018 @item inexpr-statement
1019 On the first line of statement block inside an expression. This is used
1020 for the GCC extension to C that uses the syntax @code{(@{ @dots{} @})}.
1021 It is also used for the special functions that takes a statement block
1022 as an argument in Pike.
1023
1024 @item inexpr-class
1025 On the first line of a class definition inside an expression. This is
1026 used for anonymous classes and anonymous array initializers in Java.
1027
1028 @item cpp-macro
1029 On the start of a cpp macro.
1030
1031 @item friend
1032 On a C++ @code{friend} declaration.
1033
1034 @item objc-method-intro
1035 On the first line of an Objective-C method definition.
1036
1037 @item objc-method-args-cont
1038 On one of the lines continuing an Objective-C method definition.
1039
1040 @item objc-method-call-cont
1041 On one of the lines continuing an Objective-C method call.
1042
1043 @item inlambda
1044 Like @code{inclass}, but used inside lambda (i.e. anonymous) functions. Only
1045 used in Pike.
1046
1047 @item lambda-intro-cont
1048 On a line continuing the header of a lambda function, between the
1049 @code{lambda} keyword and the function body. Only used in Pike.
1050 @end table
1051
1052 @node Variables for C Indent
1053 @subsubsection Variables for C Indentation
1054
1055 This section describes additional variables which control the
1056 indentation behavior of C mode and related mode.
1057
1058 @table @code
1059 @item c-offsets-alist
1060 @vindex c-offsets-alist
1061 Association list of syntactic symbols and their indentation offsets.
1062 You should not set this directly, only with @code{c-set-offset}.
1063 @xref{Changing Indent Style}, for details.
1064
1065 @item c-style-alist
1066 @vindex c-style-alist
1067 Variable for defining indentation styles; see below.
1068
1069 @item c-basic-offset
1070 @vindex c-basic-offset
1071 Amount of basic offset used by @code{+} and @code{-} symbols in
1072 @code{c-offsets-alist}.@refill
1073
1074 @item c-special-indent-hook
1075 @vindex c-special-indent-hook
1076 Hook for user-defined special indentation adjustments. This hook is
1077 called after a line is indented by C mode and related modes.
1078 @end table
1079
1080 The variable @code{c-style-alist} specifies the predefined indentation
1081 styles. Each element has form @code{(@var{name}
1082 @var{variable-setting}@dots{})}, where @var{name} is the name of the
1083 style. Each @var{variable-setting} has the form @code{(@var{variable}
1084 . @var{value})}; @var{variable} is one of the customization variables
1085 used by C mode, and @var{value} is the value for that variable when
1086 using the selected style.
1087
1088 When @var{variable} is @code{c-offsets-alist}, that is a special case:
1089 @var{value} is appended to the front of the value of @code{c-offsets-alist}
1090 instead of replacing that value outright. Therefore, it is not necessary
1091 for @var{value} to specify each and every syntactic symbol---only those
1092 for which the style differs from the default.
1093
1094 The indentation of lines containing only comments is also affected by
1095 the variable @code{c-comment-only-line-offset} (@pxref{Comments in C}).
1096
1097 @node C Indent Styles
1098 @subsubsection C Indentation Styles
1099 @cindex c indentation styles
1100
1101 A @dfn{C style} is a collection of indentation style customizations.
1102 Emacs comes with several predefined indentation styles for C and related
1103 modes, including @code{gnu}, @code{k&r}, @code{bsd}, @code{stroustrup},
1104 @code{linux}, @code{python}, @code{java}, @code{whitesmith},
1105 @code{ellemtel}, @code{cc-mode}, and @code{user}.
1106
1107 @findex c-set-style
1108 @vindex c-default-style
1109 To choose the style you want, use the command @kbd{M-x c-set-style}.
1110 Specify a style name as an argument (case is not significant in C style
1111 names). The chosen style only affects newly visited buffers, not those
1112 you are already editing. You can also set the variable
1113 @code{c-default-style} to specify the style for various major modes.
1114 Its value should be an alist, in which each element specifies one major
1115 mode and which indentation style to use for it. For example,
1116
1117 @example
1118 (setq c-default-style
1119 '((java-mode . "java") (other . "gnu")))
1120 @end example
1121
1122 @noindent
1123 specifies an explicit choice for Java mode, and the default @samp{gnu}
1124 style for the other C-like modes.
1125
1126 The style @code{gnu} defines the formatting recommend by the GNU
1127 Project; it is the default, so as to encourage the indentation we
1128 recommend. If you make changes in variables such as
1129 @code{c-basic-offset} and @code{c-offsets-alist} in your @file{~/.emacs}
1130 file, they will however take precedence.
1131
1132 @findex c-add-style
1133 To define a new C indentation style, call the function
1134 @code{c-add-style}:
1135
1136 @example
1137 (c-add-style @var{name} @var{values} @var{use-now})
1138 @end example
1139
1140 @noindent
1141 Here @var{name} is the name of the new style (a string), and
1142 @var{values} is an alist whose elements have the form
1143 @code{(@var{variable} . @var{value})}. The variables you specify should
1144 be among those documented in @ref{Variables for C Indent}.
1145
1146 If @var{use-now} is non-@code{nil}, @code{c-add-style} selects the new
1147 style after defining it.
1148
1149 @node Matching
1150 @section Automatic Display Of Matching Parentheses
1151 @cindex matching parentheses
1152 @cindex parentheses, displaying matches
1153
1154 The Emacs parenthesis-matching feature is designed to show
1155 automatically how parentheses match in the text. Whenever you type a
1156 self-inserting character that is a closing delimiter, the cursor moves
1157 momentarily to the location of the matching opening delimiter, provided
1158 that is on the screen. If it is not on the screen, some text near it is
1159 displayed in the echo area. Either way, you can tell what grouping is
1160 being closed off.
1161
1162 In Lisp, automatic matching applies only to parentheses. In C, it
1163 applies to braces and brackets too. Emacs knows which characters to regard
1164 as matching delimiters based on the syntax table, which is set by the major
1165 mode. @xref{Syntax}.
1166
1167 If the opening delimiter and closing delimiter are mismatched---such as
1168 in @samp{[x)}---a warning message is displayed in the echo area. The
1169 correct matches are specified in the syntax table.
1170
1171 @vindex blink-matching-paren
1172 @vindex blink-matching-paren-distance
1173 @vindex blink-matching-delay
1174 Three variables control parenthesis match display.
1175 @code{blink-matching-paren} turns the feature on or off; @code{nil}
1176 turns it off, but the default is @code{t} to turn match display on.
1177 @code{blink-matching-delay} says how many seconds to wait; the default
1178 is 1, but on some systems it is useful to specify a fraction of a
1179 second. @code{blink-matching-paren-distance} specifies how many
1180 characters back to search to find the matching opening delimiter. If
1181 the match is not found in that far, scanning stops, and nothing is
1182 displayed. This is to prevent scanning for the matching delimiter from
1183 wasting lots of time when there is no match. The default is 12,000.
1184
1185 @cindex Show Paren mode
1186 @findex show-paren-mode
1187 When using X Windows, you can request a more powerful alternative kind
1188 of automatic parenthesis matching by enabling Show Paren mode. This
1189 mode turns off the usual kind of matching parenthesis display and
1190 instead uses highlighting to show what matches. Whenever point is after
1191 a close parenthesis, the close parenthesis and its matching open
1192 parenthesis are both highlighted; otherwise, if point is before an open
1193 parenthesis, the matching close parenthesis is highlighted. (There is
1194 no need to highlight the open parenthesis after point because the cursor
1195 appears on top of that character.) Use the command @kbd{M-x
1196 show-paren-mode} to enable or disable this mode.
1197
1198 @node Comments
1199 @section Manipulating Comments
1200 @cindex comments
1201
1202 Because comments are such an important part of programming, Emacs
1203 provides special commands for editing and inserting comments.
1204
1205 @menu
1206 * Comment Commands::
1207 * Multi-Line Comments::
1208 * Options for Comments::
1209 @end menu
1210
1211 @node Comment Commands
1212 @subsection Comment Commands
1213
1214 @kindex M-;
1215 @cindex indentation for comments
1216 @findex indent-for-comment
1217
1218 The comment commands insert, kill and align comments.
1219
1220 @c WideCommands
1221 @table @kbd
1222 @item M-;
1223 Insert or align comment (@code{indent-for-comment}).
1224 @item C-x ;
1225 Set comment column (@code{set-comment-column}).
1226 @item C-u - C-x ;
1227 Kill comment on current line (@code{kill-comment}).
1228 @item C-M-j
1229 Like @key{RET} followed by inserting and aligning a comment
1230 (@code{indent-new-comment-line}).
1231 @item M-x comment-region
1232 Add or remove comment delimiters on all the lines in the region.
1233 @end table
1234
1235 The command that creates a comment is @kbd{M-;} (@code{indent-for-comment}).
1236 If there is no comment already on the line, a new comment is created,
1237 aligned at a specific column called the @dfn{comment column}. The comment
1238 is created by inserting the string Emacs thinks comments should start with
1239 (the value of @code{comment-start}; see below). Point is left after that
1240 string. If the text of the line extends past the comment column, then the
1241 indentation is done to a suitable boundary (usually, at least one space is
1242 inserted). If the major mode has specified a string to terminate comments,
1243 that is inserted after point, to keep the syntax valid.
1244
1245 @kbd{M-;} can also be used to align an existing comment. If a line
1246 already contains the string that starts comments, then @kbd{M-;} just moves
1247 point after it and reindents it to the conventional place. Exception:
1248 comments starting in column 0 are not moved.
1249
1250 Some major modes have special rules for indenting certain kinds of
1251 comments in certain contexts. For example, in Lisp code, comments which
1252 start with two semicolons are indented as if they were lines of code,
1253 instead of at the comment column. Comments which start with three
1254 semicolons are supposed to start at the left margin. Emacs understands
1255 these conventions by indenting a double-semicolon comment using @key{TAB},
1256 and by not changing the indentation of a triple-semicolon comment at all.
1257
1258 @example
1259 ;; This function is just an example
1260 ;;; Here either two or three semicolons are appropriate.
1261 (defun foo (x)
1262 ;;; And now, the first part of the function:
1263 ;; The following line adds one.
1264 (1+ x)) ; This line adds one.
1265 @end example
1266
1267 In C code, a comment preceded on its line by nothing but whitespace
1268 is indented like a line of code.
1269
1270 Even when an existing comment is properly aligned, @kbd{M-;} is still
1271 useful for moving directly to the start of the comment.
1272
1273 @kindex C-u - C-x ;
1274 @findex kill-comment
1275 @kbd{C-u - C-x ;} (@code{kill-comment}) kills the comment on the current line,
1276 if there is one. The indentation before the start of the comment is killed
1277 as well. If there does not appear to be a comment in the line, nothing is
1278 done. To reinsert the comment on another line, move to the end of that
1279 line, do @kbd{C-y}, and then do @kbd{M-;} to realign it. Note that
1280 @kbd{C-u - C-x ;} is not a distinct key; it is @kbd{C-x ;} (@code{set-comment-column})
1281 with a negative argument. That command is programmed so that when it
1282 receives a negative argument it calls @code{kill-comment}. However,
1283 @code{kill-comment} is a valid command which you could bind directly to a
1284 key if you wanted to.
1285
1286 @node Multi-Line Comments
1287 @subsection Multiple Lines of Comments
1288
1289 @kindex C-M-j
1290 @cindex blank lines in programs
1291 @findex indent-new-comment-line
1292 If you are typing a comment and wish to continue it on another line,
1293 you can use the command @kbd{C-M-j} (@code{indent-new-comment-line}).
1294 This terminates the comment you are typing, creates a new blank line
1295 afterward, and begins a new comment indented under the old one. When
1296 Auto Fill mode is on, going past the fill column while typing a comment
1297 causes the comment to be continued in just this fashion. If point is
1298 not at the end of the line when @kbd{C-M-j} is typed, the text on
1299 the rest of the line becomes part of the new comment line.
1300
1301 @findex comment-region
1302 To turn existing lines into comment lines, use the @kbd{M-x
1303 comment-region} command. It adds comment delimiters to the lines that start
1304 in the region, thus commenting them out. With a negative argument, it
1305 does the opposite---it deletes comment delimiters from the lines in the
1306 region.
1307
1308 With a positive argument, @code{comment-region} duplicates the last
1309 character of the comment start sequence it adds; the argument specifies
1310 how many copies of the character to insert. Thus, in Lisp mode,
1311 @kbd{C-u 2 M-x comment-region} adds @samp{;;} to each line. Duplicating
1312 the comment delimiter is a way of calling attention to the comment. It
1313 can also affect how the comment is indented. In Lisp, for proper
1314 indentation, you should use an argument of two, if between defuns, and
1315 three, if within a defun.
1316
1317 @vindex comment-padding
1318 The variable @code{comment-padding} specifies how many spaces
1319 @code{comment-region} should insert on each line between the
1320 comment delimiter and the line's original text. The default is 1.
1321
1322 @node Options for Comments
1323 @subsection Options Controlling Comments
1324
1325 @vindex comment-column
1326 @kindex C-x ;
1327 @findex set-comment-column
1328 The comment column is stored in the variable @code{comment-column}. You
1329 can set it to a number explicitly. Alternatively, the command @kbd{C-x ;}
1330 (@code{set-comment-column}) sets the comment column to the column point is
1331 at. @kbd{C-u C-x ;} sets the comment column to match the last comment
1332 before point in the buffer, and then does a @kbd{M-;} to align the
1333 current line's comment under the previous one. Note that @kbd{C-u - C-x ;}
1334 runs the function @code{kill-comment} as described above.
1335
1336 The variable @code{comment-column} is per-buffer: setting the variable
1337 in the normal fashion affects only the current buffer, but there is a
1338 default value which you can change with @code{setq-default}.
1339 @xref{Locals}. Many major modes initialize this variable for the
1340 current buffer.
1341
1342 @vindex comment-start-skip
1343 The comment commands recognize comments based on the regular
1344 expression that is the value of the variable @code{comment-start-skip}.
1345 Make sure this regexp does not match the null string. It may match more
1346 than the comment starting delimiter in the strictest sense of the word;
1347 for example, in C mode the value of the variable is @code{@t{"/\\*+
1348 *"}}, which matches extra stars and spaces after the @samp{/*} itself.
1349 (Note that @samp{\\} is needed in Lisp syntax to include a @samp{\} in
1350 the string, which is needed to deny the first star its special meaning
1351 in regexp syntax. @xref{Regexps}.)
1352
1353 @vindex comment-start
1354 @vindex comment-end
1355 When a comment command makes a new comment, it inserts the value of
1356 @code{comment-start} to begin it. The value of @code{comment-end} is
1357 inserted after point, so that it will follow the text that you will insert
1358 into the comment. In C mode, @code{comment-start} has the value
1359 @w{@code{"/* "}} and @code{comment-end} has the value @w{@code{" */"}}.
1360
1361 @vindex comment-multi-line
1362 The variable @code{comment-multi-line} controls how @kbd{C-M-j}
1363 (@code{indent-new-comment-line}) behaves when used inside a comment. If
1364 @code{comment-multi-line} is @code{nil}, as it normally is, then the
1365 comment on the starting line is terminated and a new comment is started
1366 on the new following line. If @code{comment-multi-line} is not
1367 @code{nil}, then the new following line is set up as part of the same
1368 comment that was found on the starting line. This is done by not
1369 inserting a terminator on the old line, and not inserting a starter on
1370 the new line. In languages where multi-line comments work, the choice
1371 of value for this variable is a matter of taste.
1372
1373 @vindex comment-indent-function
1374 The variable @code{comment-indent-function} should contain a function
1375 that will be called to compute the indentation for a newly inserted
1376 comment or for aligning an existing comment. It is set differently by
1377 various major modes. The function is called with no arguments, but with
1378 point at the beginning of the comment, or at the end of a line if a new
1379 comment is to be inserted. It should return the column in which the
1380 comment ought to start. For example, in Lisp mode, the indent hook
1381 function bases its decision on how many semicolons begin an existing
1382 comment, and on the code in the preceding lines.
1383
1384 @node Balanced Editing
1385 @section Editing Without Unbalanced Parentheses
1386
1387 @table @kbd
1388 @item M-(
1389 Put parentheses around next sexp(s) (@code{insert-parentheses}).
1390 @item M-)
1391 Move past next close parenthesis and reindent
1392 (@code{move-past-close-and-reindent}).
1393 @end table
1394
1395 @kindex M-(
1396 @kindex M-)
1397 @findex insert-parentheses
1398 @findex move-past-close-and-reindent
1399 The commands @kbd{M-(} (@code{insert-parentheses}) and @kbd{M-)}
1400 (@code{move-past-close-and-reindent}) are designed to facilitate a style
1401 of editing which keeps parentheses balanced at all times. @kbd{M-(}
1402 inserts a pair of parentheses, either together as in @samp{()}, or, if
1403 given an argument, around the next several sexps. It leaves point after
1404 the open parenthesis. The command @kbd{M-)} moves past the close
1405 parenthesis, deleting any indentation preceding it, and indenting with
1406 @kbd{C-j} after it.
1407
1408 For example, instead of typing @kbd{( F O O )}, you can type @kbd{M-(
1409 F O O}, which has the same effect except for leaving the cursor before
1410 the close parenthesis.
1411
1412 @vindex parens-require-spaces
1413 @kbd{M-(} may insert a space before the open parenthesis, depending on
1414 the syntax class of the preceding character. Set
1415 @code{parens-require-spaces} to @code{nil} value if you wish to inhibit
1416 this.
1417
1418 @findex check-parens
1419 You can use @kbd{M-x check-parens} to find any unbalanced parentheses in
1420 a buffer.
1421
1422 @node Symbol Completion
1423 @section Completion for Symbol Names
1424 @cindex completion (symbol names)
1425
1426 Usually completion happens in the minibuffer. But one kind of completion
1427 is available in all buffers: completion for symbol names.
1428
1429 @kindex M-TAB
1430 The character @kbd{M-@key{TAB}} runs a command to complete the partial
1431 symbol before point against the set of meaningful symbol names. Any
1432 additional characters determined by the partial name are inserted at
1433 point.
1434
1435 If the partial name in the buffer has more than one possible completion
1436 and they have no additional characters in common, a list of all possible
1437 completions is displayed in another window.
1438
1439 @cindex completion using tags
1440 @cindex tags completion
1441 @cindex Info index completion
1442 @findex complete-symbol
1443 In most programming language major modes, @kbd{M-@key{TAB}} runs the
1444 command @code{complete-symbol}, which provides two kinds of completion.
1445 Normally it does completion based on a tags table (@pxref{Tags}); with a
1446 numeric argument (regardless of the value), it does completion based on
1447 the names listed in the Info file indexes for your language. Thus, to
1448 complete the name of a symbol defined in your own program, use
1449 @kbd{M-@key{TAB}} with no argument; to complete the name of a standard
1450 library function, use @kbd{C-u M-@key{TAB}}. Of course, Info-based
1451 completion works only if there is an Info file for the standard library
1452 functions of your language, and only if it is installed at your site.
1453
1454 @cindex Lisp symbol completion
1455 @cindex completion in Lisp
1456 @findex lisp-complete-symbol
1457 In Emacs-Lisp mode, the name space for completion normally consists of
1458 nontrivial symbols present in Emacs---those that have function
1459 definitions, values or properties. However, if there is an
1460 open-parenthesis immediately before the beginning of the partial symbol,
1461 only symbols with function definitions are considered as completions.
1462 The command which implements this is @code{lisp-complete-symbol}.
1463
1464 In Text mode and related modes, @kbd{M-@key{TAB}} completes words
1465 based on the spell-checker's dictionary. @xref{Spelling}.
1466
1467 @node Which Function
1468 @section Which Function Mode
1469
1470 Which Function mode is a minor mode that displays the current function
1471 name in the mode line, as you move around in a buffer.
1472
1473 @findex which-function-mode
1474 @vindex which-func-modes
1475 To enable (or disable) Which Function mode, use the command @kbd{M-x
1476 which-function-mode}. This command is global; it applies to all
1477 buffers, both existing ones and those yet to be created. However, this
1478 only affects certain major modes, those listed in the value of
1479 @code{which-func-modes}. (If the value is @code{t}, then Which Function
1480 mode applies to all major modes that know how to support it---which are
1481 the major modes that support Imenu.)
1482
1483 @node Hideshow
1484 @section Hideshow minor mode
1485
1486 @findex hs-minor-mode
1487 Hideshow minor mode provides selective display of blocks. Use @kbd{M-x
1488 hs-minor-mode} to toggle the mode or add @code{hs-minor-mode} to the
1489 hook for major modes with which you want to use it and which support it.
1490
1491 Blocks are defined dependent on the mode. In C mode or C++ mode, they
1492 are delimited by braces, while in Lisp-ish modes they are delimited by
1493 parens. Multi-line comments can also be hidden.
1494
1495 @findex hs-hide-all
1496 @findex hs-hide-block
1497 @findex hs-show-all
1498 @findex hs-show-block
1499 @findex hs-show-region
1500 @findex hs-hide-level
1501 @findex hs-minor-mode
1502 @kindex C-c h
1503 @kindex C-c s
1504 @kindex C-c H
1505 @kindex C-c S
1506 @kindex C-c R
1507 @kindex C-c L
1508 @kindex S-mouse-2
1509 The mode provides the commands @kbd{C-c h} (@kbd{M-x hs-hide-all}),
1510 @kbd{C-c s} (@kbd{M-x hs-hide-block}), @kbd{C-c H} (@kbd{M-x
1511 hs-show-all}), @kbd{C-c S} (@kbd{M-x hs-show-block}), @kbd{C-c R}
1512 (@kbd{M-x hs-show-region}) and @kbd{C-c L} (@kbd{M-x hs-hide-level})
1513 with obvious functions and @kbd{S-mouse-2} toggles hiding of a block
1514 with the mouse.
1515
1516 @vindex hs-hide-comments-when-hiding-all
1517 @vindex hs-show-hidden-short-form
1518 @vindex hs-isearch-open
1519 @vindex hs-special-modes-alist
1520 Hideshow is customized by the variables
1521 @table @code
1522 @item hs-hide-comments-when-hiding-all
1523 Specifies whether @kbd{hs-hide-all} should hide comments too.
1524 @item hs-show-hidden-short-form
1525 Specifies whether or not the last line in a form is omitted (saving
1526 screen space).
1527 @item hs-isearch-open
1528 Specifies what kind of hidden blocks to open in Isearch mode.
1529 @item hs-special-modes-alist
1530 Initializes Hideshow variables for different modes.
1531 @end table
1532
1533 @node Documentation, Change Log, Hideshow, Programs
1534 @section Documentation Commands
1535
1536 As you edit Lisp code to be run in Emacs, the commands @kbd{C-h f}
1537 (@code{describe-function}) and @kbd{C-h v} (@code{describe-variable}) can
1538 be used to print documentation of functions and variables that you want to
1539 call. These commands use the minibuffer to read the name of a function or
1540 variable to document, and display the documentation in a window.
1541
1542 For extra convenience, these commands provide default arguments based on
1543 the code in the neighborhood of point. @kbd{C-h f} sets the default to the
1544 function called in the innermost list containing point. @kbd{C-h v} uses
1545 the symbol name around or adjacent to point as its default.
1546
1547 @cindex Eldoc mode
1548 @findex eldoc-mode
1549 For Emacs Lisp code, you can also use Eldoc mode. This minor mode
1550 constantly displays in the echo area the argument list for the function
1551 being called at point. (In other words, it finds the function call that
1552 point is contained in, and displays the argument list of that function.)
1553 Eldoc mode applies in Emacs Lisp and Lisp Interaction modes only. Use
1554 the command @kbd{M-x eldoc-mode} to enable or disable this feature.
1555
1556 @findex info-lookup-symbol
1557 @findex info-lookup-file
1558 @kindex C-h C-i
1559 For C, Lisp, and other languages, you can use @kbd{C-h C-i}
1560 (@code{info-lookup-symbol}) to view the Info documentation for a symbol.
1561 You specify the symbol with the minibuffer; by default, it uses the
1562 symbol that appears in the buffer at point. The major mode determines
1563 where to look for documentation for the symbol---which Info files and
1564 which indices. You can also use @kbd{M-x info-lookup-file} to look for
1565 documentation for a file name. Currently the modes supported by
1566 Info-lookup are: Awk, Autoconf, Bison, C, Emacs Lisp, LaTeX, M4,
1567 Makefile, Octave, Perl, Scheme and Texinfo. The relevant Info files
1568 mostly must be obtained separately, typically from the appropriate GNU
1569 package.
1570
1571 @findex manual-entry
1572 @cindex manual pages
1573 You can read the ``man page'' for an operating system command, library
1574 function, or system call, with the @kbd{M-x manual-entry} command. It
1575 runs the @code{man} program to format the man page, and runs it
1576 asynchronously if your system permits, so that you can keep on editing
1577 while the page is being formatted. (MS-DOS and MS-Windows 3 do not
1578 permit asynchronous subprocesses, so on these systems you cannot edit
1579 while Emacs waits for @code{man} to exit.) The result goes in a buffer
1580 named @samp{*Man @var{topic}*}. These buffers use a special major mode,
1581 Man mode, that facilitates scrolling and examining other manual pages.
1582 For details, type @kbd{C-h m} while in a man page buffer.
1583
1584 @cindex sections of manual pages
1585 Man pages are subdivided into @dfn{sections}, and some man pages have
1586 identical names, but belong to different sections. To read a man page
1587 from a certain section, type @kbd{@var{topic}(@var{section})} or
1588 @kbd{@var{section} @var{topic}} when @kbd{M-x manual-entry} prompts for
1589 the topic. For example, to read the man page for the C library function
1590 @code{chmod} (as opposed to a command by the same name), type @kbd{M-x
1591 manual-entry @key{RET} chmod(2v) @key{RET}} (assuming @code{chmod} is in
1592 section @code{2v}).
1593
1594 If you do not specify a section, the results depend on how the
1595 @code{man} command works on your system. Some of them display only the
1596 first man page they find, others display all the man pages, and you can
1597 page between them with the @kbd{M-n} and @kbd{M-p} keys. The mode line
1598 shows how many manual pages are available in the Man buffer.
1599
1600 @vindex Man-fontify-manpage-flag
1601 For a long man page, setting the faces properly can take substantial
1602 time. By default, Emacs uses faces in man pages if Emacs can display
1603 different fonts or colors. You can turn off use of faces in man pages
1604 by setting the variable @code{Man-fontify-manpage-flag} to @code{nil}.
1605
1606 @findex Man-fontify-manpage
1607 If you insert the text of a man page into an Emacs buffer in some
1608 other fashion, you can use the command @kbd{M-x Man-fontify-manpage} to
1609 perform the same conversions that @kbd{M-x manual-entry} does.
1610
1611 @findex woman
1612 @cindex manual pages, on MS-DOS/MS-Windows
1613 An alternative way of reading manual pages is the @kbd{M-x woman}
1614 command@footnote{The name of the command, @code{woman}, is an acronym
1615 for ``w/o (without) man'', since it doesn't use the @code{man}
1616 program.}. Unlike @kbd{M-x man}, it does not run any external programs
1617 to format and display the man pages, instead it does that entirely in
1618 Emacs Lisp. Thus, it is useful on systems such as MS-Windows, where the
1619 @code{man} program and the programs it runs are not readily available.
1620 When invoked, @kbd{M-x woman} prompts for a name of a manual page and
1621 provides completion based on the list of manual pages that are installed
1622 on your machine; the list of available manual pages is computed
1623 automatically the first time you invoke @code{woman}. The word at point
1624 in the current buffer is used to suggest the default name of the manual
1625 page.
1626
1627 With a numeric argument, @kbd{M-x woman} recomputes the list of the
1628 manual pages used for completion. This is useful if you add or delete
1629 manual pages.
1630
1631 If you type a name of a manual page and @kbd{M-x woman} finds that
1632 several manual pages by the same name exist in different sections, it
1633 pops up a window with possible candidates asking you to choose one of
1634 them.
1635
1636 @vindex woman-manpath
1637 By default, @kbd{M-x woman} looks up the manual pages in directories
1638 listed by the @code{MANPATH} environment variable. (If @code{MANPATH}
1639 is not set, @code{woman} uses a suitable default value, which can be
1640 customized.) More precisely, @code{woman} looks for subdirectories that
1641 match the shell wildcard @file{man*} in each one of these directories,
1642 and tries to find the manual pages in those subdirectories. When first
1643 invoked, @kbd{M-x woman} converts the value of @code{MANPATH} to a list
1644 of directory names and stores that list in the @code{woman-manpath}
1645 variable. By changing the value of this variable, you can customize the
1646 list of directories where @code{woman} looks for manual pages.
1647
1648 @vindex woman-path
1649 In addition, you can augment the list of directories searched by
1650 @code{woman} by setting the value of the @code{woman-path} variable.
1651 This variable should hold a list of specific directories which
1652 @code{woman} should search, in addition to those in
1653 @code{woman-manpath}. Unlike @code{woman-manpath}, the directories in
1654 @code{woman-path} are searched for the manual pages, not for @file{man*}
1655 subdirectories.
1656
1657 @findex woman-find-file
1658 Occasionally, you might need to display manual pages that are not in
1659 any of the directories listed by @code{woman-manpath} and
1660 @code{woman-path}. The @kbd{M-x woman-find-file} command prompts for a
1661 name of a manual page file, with completion, and then formats and
1662 displays that file like @kbd{M-x woman} does.
1663
1664 @vindex woman-dired-keys
1665 First time you invoke @kbd{M-x woman}, it defines the Dired @kbd{W}
1666 key to run the @code{woman-find-file} command on the current line's
1667 file. You can disable this by setting the variable
1668 @code{woman-dired-keys} to @code{nil}. @xref{Dired}. In addition, the
1669 Tar-mode @kbd{w} key is bound to @code{woman-find-file} on the current
1670 line's archive member.
1671
1672 For more information about setting up and using @kbd{M-x woman}, see
1673 @ref{Top, WoMan, Browse UN*X Manual Pages WithOut Man, woman, The WoMan
1674 Manual}.
1675
1676 Eventually the GNU project hopes to replace most man pages with
1677 better-organized manuals that you can browse with Info. @xref{Misc
1678 Help}. Since this process is only partially completed, it is still
1679 useful to read manual pages.
1680
1681 @node Change Log
1682 @section Change Logs
1683
1684 @cindex change log
1685 @kindex C-x 4 a
1686 @findex add-change-log-entry-other-window
1687 The Emacs command @kbd{C-x 4 a} adds a new entry to the change log
1688 file for the file you are editing
1689 (@code{add-change-log-entry-other-window}).
1690
1691 A change log file contains a chronological record of when and why you
1692 have changed a program, consisting of a sequence of entries describing
1693 individual changes. Normally it is kept in a file called
1694 @file{ChangeLog} in the same directory as the file you are editing, or
1695 one of its parent directories. A single @file{ChangeLog} file can
1696 record changes for all the files in its directory and all its
1697 subdirectories.
1698
1699 A change log entry starts with a header line that contains your name,
1700 your email address (taken from the variable @code{user-mail-address}),
1701 and the current date and time. Aside from these header lines, every
1702 line in the change log starts with a space or a tab. The bulk of the
1703 entry consists of @dfn{items}, each of which starts with a line starting
1704 with whitespace and a star. Here are two entries, both dated in May
1705 1993, each with two items:
1706
1707 @iftex
1708 @medbreak
1709 @end iftex
1710 @smallexample
1711 1993-05-25 Richard Stallman <rms@@gnu.org>
1712
1713 * man.el: Rename symbols `man-*' to `Man-*'.
1714 (manual-entry): Make prompt string clearer.
1715
1716 * simple.el (blink-matching-paren-distance):
1717 Change default to 12,000.
1718
1719 1993-05-24 Richard Stallman <rms@@gnu.org>
1720
1721 * vc.el (minor-mode-map-alist): Don't use it if it's void.
1722 (vc-cancel-version): Doc fix.
1723 @end smallexample
1724
1725 @noindent
1726 (Previous Emacs versions used a different format for the date.)
1727
1728 One entry can describe several changes; each change should have its
1729 own item. Normally there should be a blank line between items. When
1730 items are related (parts of the same change, in different places), group
1731 them by leaving no blank line between them. The second entry above
1732 contains two items grouped in this way.
1733
1734 @vindex add-log-keep-changes-together
1735 @kbd{C-x 4 a} visits the change log file and creates a new entry
1736 unless the most recent entry is for today's date and your name. It also
1737 creates a new item for the current file. For many languages, it can
1738 even guess the name of the function or other object that was changed.
1739 When the option @code{add-log-keep-changes-together} is set, @kbd{C-x 4
1740 a} adds to any existing entry for the file rather than starting a new
1741 entry.
1742
1743 @cindex Change Log mode
1744 @findex change-log-mode
1745 The change log file is visited in Change Log mode. In this major
1746 mode, each bunch of grouped items counts as one paragraph, and each
1747 entry is considered a page. This facilitates editing the entries.
1748 @kbd{C-j} and auto-fill indent each new line like the previous line;
1749 this is convenient for entering the contents of an entry.
1750
1751 @findex change-log-merge
1752 The command @kbd{M-x change-log-merge} can be used to merge other log
1753 files into a buffer in Change Log Mode, preserving the date ordering
1754 of entries with either the current or old-style date formats.
1755
1756 Version control systems are another way to keep track of changes in your
1757 program and keep a change log. @xref{Log Buffer}.
1758
1759 @node Authors
1760 @section @file{AUTHORS} files
1761 @cindex @file{AUTHORS} file
1762
1763 Programs which have many contributors usually include a file named
1764 @file{AUTHORS} in their distribution, which lists the individual
1765 contributions. Emacs has a special command for maintaining the
1766 @file{AUTHORS} file that is part of the Emacs distribution.
1767
1768 @findex authors
1769 The @kbd{M-x authors} command prompts for the name of the root of the
1770 Emacs source directory. It then scans @file{ChageLog} files and Lisp
1771 source files under that directory for information about authors of
1772 individual packages and people who made changes in source files, and
1773 puts the information it gleans into a buffer named @samp{*Authors*}.
1774 You can then edit the contents of that buffer and merge it with the
1775 exisiting @file{AUTHORS} file.
1776
1777 @node Tags
1778 @section Tags Tables
1779 @cindex tags table
1780
1781 A @dfn{tags table} is a description of how a multi-file program is
1782 broken up into files. It lists the names of the component files and the
1783 names and positions of the functions (or other named subunits) in each
1784 file. Grouping the related files makes it possible to search or replace
1785 through all the files with one command. Recording the function names
1786 and positions makes possible the @kbd{M-.} command which finds the
1787 definition of a function by looking up which of the files it is in.
1788
1789 Tags tables are stored in files called @dfn{tags table files}. The
1790 conventional name for a tags table file is @file{TAGS}.
1791
1792 Each entry in the tags table records the name of one tag, the name of the
1793 file that the tag is defined in (implicitly), and the position in that file
1794 of the tag's definition.
1795
1796 Just what names from the described files are recorded in the tags table
1797 depends on the programming language of the described file. They
1798 normally include all functions and subroutines, and may also include
1799 global variables, data types, and anything else convenient. Each name
1800 recorded is called a @dfn{tag}.
1801
1802 @menu
1803 * Tag Syntax:: Tag syntax for various types of code and text files.
1804 * Create Tags Table:: Creating a tags table with @code{etags}.
1805 * Etags Regexps:: Create arbitrary tags using regular expressions.
1806 * Select Tags Table:: How to visit a tags table.
1807 * Find Tag:: Commands to find the definition of a specific tag.
1808 * Tags Search:: Using a tags table for searching and replacing.
1809 * List Tags:: Listing and finding tags defined in a file.
1810 @end menu
1811
1812 @node Tag Syntax
1813 @subsection Source File Tag Syntax
1814
1815 Here is how tag syntax is defined for the most popular languages:
1816
1817 @itemize @bullet
1818 @item
1819 In C code, any C function or typedef is a tag, and so are definitions of
1820 @code{struct}, @code{union} and @code{enum}. You can tag function
1821 declarations and external variables in addition to function definitions
1822 by giving the @samp{--declarations} option to @code{etags}.
1823 @code{#define} macro definitions and @code{enum} constants are also
1824 tags, unless you specify @samp{--no-defines} when making the tags table.
1825 Similarly, global variables are tags, unless you specify
1826 @samp{--no-globals}. Use of @samp{--no-globals} and @samp{--no-defines}
1827 can make the tags table file much smaller.
1828
1829 @item
1830 In C++ code, in addition to all the tag constructs of C code, member
1831 functions are also recognized, and optionally member variables if you
1832 use the @samp{--members} option. Tags for variables and functions in
1833 classes are named @samp{@var{class}::@var{variable}} and
1834 @samp{@var{class}::@var{function}}. @code{operator} functions tags are
1835 named, for example @samp{operator+}.
1836
1837 @item
1838 In Java code, tags include all the constructs recognized in C++, plus
1839 the @code{interface}, @code{extends} and @code{implements} constructs.
1840 Tags for variables and functions in classes are named
1841 @samp{@var{class}.@var{variable}} and @samp{@var{class}.@var{function}}.
1842
1843 @item
1844 In La@TeX{} text, the argument of any of the commands @code{\chapter},
1845 @code{\section}, @code{\subsection}, @code{\subsubsection},
1846 @code{\eqno}, @code{\label}, @code{\ref}, @code{\cite}, @code{\bibitem},
1847 @code{\part}, @code{\appendix}, @code{\entry}, or @code{\index}, is a
1848 tag.@refill
1849
1850 Other commands can make tags as well, if you specify them in the
1851 environment variable @env{TEXTAGS} before invoking @code{etags}. The
1852 value of this environment variable should be a colon-separated list of
1853 command names. For example,
1854
1855 @example
1856 TEXTAGS="def:newcommand:newenvironment"
1857 export TEXTAGS
1858 @end example
1859
1860 @noindent
1861 specifies (using Bourne shell syntax) that the commands @samp{\def},
1862 @samp{\newcommand} and @samp{\newenvironment} also define tags.
1863
1864 @item
1865 In Lisp code, any function defined with @code{defun}, any variable
1866 defined with @code{defvar} or @code{defconst}, and in general the first
1867 argument of any expression that starts with @samp{(def} in column zero, is
1868 a tag.
1869
1870 @item
1871 In Scheme code, tags include anything defined with @code{def} or with a
1872 construct whose name starts with @samp{def}. They also include variables
1873 set with @code{set!} at top level in the file.
1874 @end itemize
1875
1876 Several other languages are also supported:
1877
1878 @itemize @bullet
1879
1880 @item
1881 In Ada code, functions, procedures, packages, tasks, and types are
1882 tags. Use the @samp{--packages-only} option to create tags for packages
1883 only.
1884
1885 @item
1886 In assembler code, labels appearing at the beginning of a line,
1887 followed by a colon, are tags.
1888
1889 @item
1890 In Bison or Yacc input files, each rule defines as a tag the nonterminal
1891 it constructs. The portions of the file that contain C code are parsed
1892 as C code.
1893
1894 @item
1895 In Cobol code, tags are paragraph names; that is, any word starting in
1896 column 8 and followed by a period.
1897
1898 @item
1899 In Erlang code, the tags are the functions, records, and macros defined
1900 in the file.
1901
1902 @item
1903 In Fortran code, functions, subroutines and blockdata are tags.
1904
1905 @item
1906 In Objective C code, tags include Objective C definitions for classes,
1907 class categories, methods, and protocols.
1908
1909 @item
1910 In Pascal code, the tags are the functions and procedures defined in
1911 the file.
1912
1913 @item
1914 In Perl code, the tags are the procedures defined by the @code{sub},
1915 @code{my} and @code{local} keywords. Use @samp{--globals} if you want
1916 to tag global variables.
1917
1918 @item
1919 In PostScript code, the tags are the functions.
1920
1921 @item
1922 In Prolog code, a tag name appears at the left margin.
1923
1924 @item
1925 In Python code, @code{def} or @code{class} at the beginning of a line
1926 generate a tag.
1927 @end itemize
1928
1929 You can also generate tags based on regexp matching (@pxref{Etags
1930 Regexps}) to handle other formats and languages.
1931
1932 @node Create Tags Table
1933 @subsection Creating Tags Tables
1934 @cindex @code{etags} program
1935
1936 The @code{etags} program is used to create a tags table file. It knows
1937 the syntax of several languages, as described in
1938 @iftex
1939 the previous section.
1940 @end iftex
1941 @ifinfo
1942 @ref{Tag Syntax}.
1943 @end ifinfo
1944 Here is how to run @code{etags}:
1945
1946 @example
1947 etags @var{inputfiles}@dots{}
1948 @end example
1949
1950 @noindent
1951 The @code{etags} program reads the specified files, and writes a tags
1952 table named @file{TAGS} in the current working directory. You can
1953 intermix compressed and plain text source file names. @code{etags}
1954 knows about the most common compression formats, and does the right
1955 thing. So you can compress all your source files and have @code{etags}
1956 look for compressed versions of its file name arguments, if it does not
1957 find uncompressed versions. Under MS-DOS, @code{etags} also looks for
1958 file names like @samp{mycode.cgz} if it is given @samp{mycode.c} on the
1959 command line and @samp{mycode.c} does not exist.
1960
1961 @code{etags} recognizes the language used in an input file based on
1962 its file name and contents. You can specify the language with the
1963 @samp{--language=@var{name}} option, described below.
1964
1965 If the tags table data become outdated due to changes in the files
1966 described in the table, the way to update the tags table is the same way it
1967 was made in the first place. It is not necessary to do this often.
1968
1969 If the tags table fails to record a tag, or records it for the wrong
1970 file, then Emacs cannot possibly find its definition. However, if the
1971 position recorded in the tags table becomes a little bit wrong (due to
1972 some editing in the file that the tag definition is in), the only
1973 consequence is a slight delay in finding the tag. Even if the stored
1974 position is very wrong, Emacs will still find the tag, but it must
1975 search the entire file for it.
1976
1977 So you should update a tags table when you define new tags that you want
1978 to have listed, or when you move tag definitions from one file to another,
1979 or when changes become substantial. Normally there is no need to update
1980 the tags table after each edit, or even every day.
1981
1982 One tags table can effectively include another. Specify the included
1983 tags file name with the @samp{--include=@var{file}} option when creating
1984 the file that is to include it. The latter file then acts as if it
1985 contained all the files specified in the included file, as well as the
1986 files it directly contains.
1987
1988 If you specify the source files with relative file names when you run
1989 @code{etags}, the tags file will contain file names relative to the
1990 directory where the tags file was initially written. This way, you can
1991 move an entire directory tree containing both the tags file and the
1992 source files, and the tags file will still refer correctly to the source
1993 files.
1994
1995 If you specify absolute file names as arguments to @code{etags}, then
1996 the tags file will contain absolute file names. This way, the tags file
1997 will still refer to the same files even if you move it, as long as the
1998 source files remain in the same place. Absolute file names start with
1999 @samp{/}, or with @samp{@var{device}:/} on MS-DOS and MS-Windows.
2000
2001 When you want to make a tags table from a great number of files, you
2002 may have problems listing them on the command line, because some systems
2003 have a limit on its length. The simplest way to circumvent this limit
2004 is to tell @code{etags} to read the file names from its standard input,
2005 by typing a dash in place of the file names, like this:
2006
2007 @smallexample
2008 find . -name "*.[chCH]" -print | etags -
2009 @end smallexample
2010
2011 Use the option @samp{--language=@var{name}} to specify the language
2012 explicitly. You can intermix these options with file names; each one
2013 applies to the file names that follow it. Specify
2014 @samp{--language=auto} to tell @code{etags} to resume guessing the
2015 language from the file names and file contents. Specify
2016 @samp{--language=none} to turn off language-specific processing
2017 entirely; then @code{etags} recognizes tags by regexp matching alone
2018 (@pxref{Etags Regexps}).
2019
2020 @samp{etags --help} prints the list of the languages @code{etags}
2021 knows, and the file name rules for guessing the language. It also prints
2022 a list of all the available @code{etags} options, together with a short
2023 explanation.
2024
2025 @node Etags Regexps
2026 @subsection Etags Regexps
2027
2028 The @samp{--regex} option provides a general way of recognizing tags
2029 based on regexp matching. You can freely intermix it with file names.
2030 Each @samp{--regex} option adds to the preceding ones, and applies only
2031 to the following files. The syntax is:
2032
2033 @smallexample
2034 --regex=/@var{tagregexp}[/@var{nameregexp}]/
2035 @end smallexample
2036
2037 @noindent
2038 where @var{tagregexp} is used to match the lines to tag. It is always
2039 anchored, that is, it behaves as if preceded by @samp{^}. If you want
2040 to account for indentation, just match any initial number of blanks by
2041 beginning your regular expression with @samp{[ \t]*}. In the regular
2042 expressions, @samp{\} quotes the next character, and @samp{\t} stands
2043 for the tab character. Note that @code{etags} does not handle the other
2044 C escape sequences for special characters.
2045
2046 @cindex interval operator (in regexps)
2047 The syntax of regular expressions in @code{etags} is the same as in
2048 Emacs, augmented with the @dfn{interval operator}, which works as in
2049 @code{grep} and @code{ed}. The syntax of an interval operator is
2050 @samp{\@{@var{m},@var{n}\@}}, and its meaning is to match the preceding
2051 expression at least @var{m} times and up to @var{n} times.
2052
2053 You should not match more characters with @var{tagregexp} than that
2054 needed to recognize what you want to tag. If the match is such that
2055 more characters than needed are unavoidably matched by @var{tagregexp}
2056 (as will usually be the case), you should add a @var{nameregexp}, to
2057 pick out just the tag. This will enable Emacs to find tags more
2058 accurately and to do completion on tag names more reliably. You can
2059 find some examples below.
2060
2061 The option @samp{--ignore-case-regex} (or @samp{-c}) is like
2062 @samp{--regex}, except that the regular expression provided will be
2063 matched without regard to case, which is appropriate for various
2064 programming languages.
2065
2066 The @samp{-R} option deletes all the regexps defined with
2067 @samp{--regex} options. It applies to the file names following it, as
2068 you can see from the following example:
2069
2070 @smallexample
2071 etags --regex=/@var{reg1}/ voo.doo --regex=/@var{reg2}/ \
2072 bar.ber -R --lang=lisp los.er
2073 @end smallexample
2074
2075 @noindent
2076 Here @code{etags} chooses the parsing language for @file{voo.doo} and
2077 @file{bar.ber} according to their contents. @code{etags} also uses
2078 @var{reg1} to recognize additional tags in @file{voo.doo}, and both
2079 @var{reg1} and @var{reg2} to recognize additional tags in
2080 @file{bar.ber}. @code{etags} uses the Lisp tags rules, and no regexp
2081 matching, to recognize tags in @file{los.er}.
2082
2083 A regular expression can be bound to a given language, by prepending
2084 it with @samp{@{lang@}}. When you do this, @code{etags} will use the
2085 regular expression only for files of that language. @samp{etags --help}
2086 prints the list of languages recognised by @code{etags}. The following
2087 example tags the @code{DEFVAR} macros in the Emacs source files.
2088 @code{etags} applies this regular expression to C files only:
2089
2090 @smallexample
2091 --regex='@{c@}/[ \t]*DEFVAR_[A-Z_ \t(]+"\([^"]+\)"/'
2092 @end smallexample
2093
2094 @noindent
2095 This feature is particularly useful when storing a list of regular
2096 expressions in a file. The following option syntax instructs
2097 @code{etags} to read two files of regular expressions. The regular
2098 expressions contained in the second file are matched without regard to
2099 case.
2100
2101 @smallexample
2102 --regex=@@first-file --ignore-case-regex=@@second-file
2103 @end smallexample
2104
2105 @noindent
2106 A regex file contains one regular expressions per line. Empty lines,
2107 and lines beginning with space or tab are ignored. When the first
2108 character in a line is @samp{@@}, @code{etags} assumes that the rest of
2109 the line is the name of a file of regular expressions. This means that
2110 such files can be nested. All the other lines are taken to be regular
2111 expressions. For example, one can create a file called
2112 @samp{emacs.tags} with the following contents (the first line in the
2113 file is a comment):
2114
2115 @smallexample
2116 -- This is for GNU Emacs source files
2117 @{c@}/[ \t]*DEFVAR_[A-Z_ \t(]+"\([^"]+\)"/\1/
2118 @end smallexample
2119
2120 @noindent
2121 and then use it like this:
2122
2123 @smallexample
2124 etags --regex=@@emacs.tags *.[ch] */*.[ch]
2125 @end smallexample
2126
2127 Here are some more examples. The regexps are quoted to protect them
2128 from shell interpretation.
2129
2130 @itemize @bullet
2131
2132 @item
2133 Tag Octave files:
2134
2135 @smallexample
2136 etags --language=none \
2137 --regex='/[ \t]*function.*=[ \t]*\([^ \t]*\)[ \t]*(/\1/' \
2138 --regex='/###key \(.*\)/\1/' \
2139 --regex='/[ \t]*global[ \t].*/' \
2140 *.m
2141 @end smallexample
2142
2143 @noindent
2144 Note that tags are not generated for scripts so that you have to add a
2145 line by yourself of the form `###key <script-name>' if you want to jump
2146 to it.
2147
2148 @item
2149 Tag Tcl files:
2150
2151 @smallexample
2152 etags --language=none --regex='/proc[ \t]+\([^ \t]+\)/\1/' *.tcl
2153 @end smallexample
2154
2155 @item
2156 Tag VHDL files:
2157
2158 @smallexample
2159 --language=none \
2160 --regex='/[ \t]*\(ARCHITECTURE\|CONFIGURATION\) +[^ ]* +OF/' \
2161 --regex='/[ \t]*\(ATTRIBUTE\|ENTITY\|FUNCTION\|PACKAGE\
2162 \( BODY\)?\|PROCEDURE\|PROCESS\|TYPE\)[ \t]+\([^ \t(]+\)/\3/'
2163 @end smallexample
2164 @end itemize
2165
2166 @node Select Tags Table
2167 @subsection Selecting a Tags Table
2168
2169 @vindex tags-file-name
2170 @findex visit-tags-table
2171 Emacs has at any time one @dfn{selected} tags table, and all the commands
2172 for working with tags tables use the selected one. To select a tags table,
2173 type @kbd{M-x visit-tags-table}, which reads the tags table file name as an
2174 argument. The name @file{TAGS} in the default directory is used as the
2175 default file name.
2176
2177 All this command does is store the file name in the variable
2178 @code{tags-file-name}. Emacs does not actually read in the tags table
2179 contents until you try to use them. Setting this variable yourself is just
2180 as good as using @code{visit-tags-table}. The variable's initial value is
2181 @code{nil}; that value tells all the commands for working with tags tables
2182 that they must ask for a tags table file name to use.
2183
2184 Using @code{visit-tags-table} when a tags table is already loaded
2185 gives you a choice: you can add the new tags table to the current list
2186 of tags tables, or start a new list. The tags commands use all the tags
2187 tables in the current list. If you start a new list, the new tags table
2188 is used @emph{instead} of others. If you add the new table to the
2189 current list, it is used @emph{as well as} the others. When the tags
2190 commands scan the list of tags tables, they don't always start at the
2191 beginning of the list; they start with the first tags table (if any)
2192 that describes the current file, proceed from there to the end of the
2193 list, and then scan from the beginning of the list until they have
2194 covered all the tables in the list.
2195
2196 @vindex tags-table-list
2197 You can specify a precise list of tags tables by setting the variable
2198 @code{tags-table-list} to a list of strings, like this:
2199
2200 @c keep this on two lines for formatting in smallbook
2201 @example
2202 @group
2203 (setq tags-table-list
2204 '("~/emacs" "/usr/local/lib/emacs/src"))
2205 @end group
2206 @end example
2207
2208 @noindent
2209 This tells the tags commands to look at the @file{TAGS} files in your
2210 @file{~/emacs} directory and in the @file{/usr/local/lib/emacs/src}
2211 directory. The order depends on which file you are in and which tags
2212 table mentions that file, as explained above.
2213
2214 Do not set both @code{tags-file-name} and @code{tags-table-list}.
2215
2216 @node Find Tag
2217 @subsection Finding a Tag
2218
2219 The most important thing that a tags table enables you to do is to find
2220 the definition of a specific tag.
2221
2222 @table @kbd
2223 @item M-.@: @var{tag} @key{RET}
2224 Find first definition of @var{tag} (@code{find-tag}).
2225 @item C-u M-.
2226 Find next alternate definition of last tag specified.
2227 @item C-u - M-.
2228 Go back to previous tag found.
2229 @item C-M-. @var{pattern} @key{RET}
2230 Find a tag whose name matches @var{pattern} (@code{find-tag-regexp}).
2231 @item C-u C-M-.
2232 Find the next tag whose name matches the last pattern used.
2233 @item C-x 4 .@: @var{tag} @key{RET}
2234 Find first definition of @var{tag}, but display it in another window
2235 (@code{find-tag-other-window}).
2236 @item C-x 5 .@: @var{tag} @key{RET}
2237 Find first definition of @var{tag}, and create a new frame to select the
2238 buffer (@code{find-tag-other-frame}).
2239 @item M-*
2240 Pop back to where you previously invoked @kbd{M-.} and friends.
2241 @end table
2242
2243 @kindex M-.
2244 @findex find-tag
2245 @kbd{M-.}@: (@code{find-tag}) is the command to find the definition of
2246 a specified tag. It searches through the tags table for that tag, as a
2247 string, and then uses the tags table info to determine the file that the
2248 definition is in and the approximate character position in the file of
2249 the definition. Then @code{find-tag} visits that file, moves point to
2250 the approximate character position, and searches ever-increasing
2251 distances away to find the tag definition.
2252
2253 If an empty argument is given (just type @key{RET}), the sexp in the
2254 buffer before or around point is used as the @var{tag} argument.
2255 @xref{Lists}, for info on sexps.
2256
2257 You don't need to give @kbd{M-.} the full name of the tag; a part
2258 will do. This is because @kbd{M-.} finds tags in the table which
2259 contain @var{tag} as a substring. However, it prefers an exact match
2260 to a substring match. To find other tags that match the same
2261 substring, give @code{find-tag} a numeric argument, as in @kbd{C-u
2262 M-.}; this does not read a tag name, but continues searching the tags
2263 table's text for another tag containing the same substring last used.
2264 If you have a real @key{META} key, @kbd{M-0 M-.}@: is an easier
2265 alternative to @kbd{C-u M-.}.
2266
2267 @kindex C-x 4 .
2268 @findex find-tag-other-window
2269 @kindex C-x 5 .
2270 @findex find-tag-other-frame
2271 Like most commands that can switch buffers, @code{find-tag} has a
2272 variant that displays the new buffer in another window, and one that
2273 makes a new frame for it. The former is @kbd{C-x 4 .}, which invokes
2274 the command @code{find-tag-other-window}. The latter is @kbd{C-x 5 .},
2275 which invokes @code{find-tag-other-frame}.
2276
2277 To move back to places you've found tags recently, use @kbd{C-u -
2278 M-.}; more generally, @kbd{M-.} with a negative numeric argument. This
2279 command can take you to another buffer. @kbd{C-x 4 .} with a negative
2280 argument finds the previous tag location in another window.
2281
2282 @kindex M-*
2283 @findex pop-tag-mark
2284 @vindex find-tag-marker-ring-length
2285 As well as going back to places you've found tags recently, you can go
2286 back to places @emph{from where} you found them. Use @kbd{M-*}, which
2287 invokes the command @code{pop-tag-mark}, for this. Typically you would
2288 find and study the definition of something with @kbd{M-.} and then
2289 return to where you were with @kbd{M-*}.
2290
2291 Both @kbd{C-u - M-.} and @kbd{M-*} allow you to retrace your steps to
2292 a depth determined by the variable @code{find-tag-marker-ring-length}.
2293
2294 @findex find-tag-regexp
2295 @kindex C-M-.
2296 The command @kbd{C-M-.} (@code{find-tag-regexp}) visits the tags that
2297 match a specified regular expression. It is just like @kbd{M-.} except
2298 that it does regexp matching instead of substring matching.
2299
2300 @node Tags Search
2301 @subsection Searching and Replacing with Tags Tables
2302
2303 The commands in this section visit and search all the files listed in the
2304 selected tags table, one by one. For these commands, the tags table serves
2305 only to specify a sequence of files to search.
2306
2307 @table @kbd
2308 @item M-x tags-search @key{RET} @var{regexp} @key{RET}
2309 Search for @var{regexp} through the files in the selected tags
2310 table.
2311 @item M-x tags-query-replace @key{RET} @var{regexp} @key{RET} @var{replacement} @key{RET}
2312 Perform a @code{query-replace-regexp} on each file in the selected tags table.
2313 @item M-,
2314 Restart one of the commands above, from the current location of point
2315 (@code{tags-loop-continue}).
2316 @end table
2317
2318 @findex tags-search
2319 @kbd{M-x tags-search} reads a regexp using the minibuffer, then
2320 searches for matches in all the files in the selected tags table, one
2321 file at a time. It displays the name of the file being searched so you
2322 can follow its progress. As soon as it finds an occurrence,
2323 @code{tags-search} returns.
2324
2325 @kindex M-,
2326 @findex tags-loop-continue
2327 Having found one match, you probably want to find all the rest. To find
2328 one more match, type @kbd{M-,} (@code{tags-loop-continue}) to resume the
2329 @code{tags-search}. This searches the rest of the current buffer, followed
2330 by the remaining files of the tags table.@refill
2331
2332 @findex tags-query-replace
2333 @kbd{M-x tags-query-replace} performs a single
2334 @code{query-replace-regexp} through all the files in the tags table. It
2335 reads a regexp to search for and a string to replace with, just like
2336 ordinary @kbd{M-x query-replace-regexp}. It searches much like @kbd{M-x
2337 tags-search}, but repeatedly, processing matches according to your
2338 input. @xref{Replace}, for more information on query replace.
2339
2340 It is possible to get through all the files in the tags table with a
2341 single invocation of @kbd{M-x tags-query-replace}. But often it is
2342 useful to exit temporarily, which you can do with any input event that
2343 has no special query replace meaning. You can resume the query replace
2344 subsequently by typing @kbd{M-,}; this command resumes the last tags
2345 search or replace command that you did.
2346
2347 The commands in this section carry out much broader searches than the
2348 @code{find-tag} family. The @code{find-tag} commands search only for
2349 definitions of tags that match your substring or regexp. The commands
2350 @code{tags-search} and @code{tags-query-replace} find every occurrence
2351 of the regexp, as ordinary search commands and replace commands do in
2352 the current buffer.
2353
2354 These commands create buffers only temporarily for the files that they
2355 have to search (those which are not already visited in Emacs buffers).
2356 Buffers in which no match is found are quickly killed; the others
2357 continue to exist.
2358
2359 It may have struck you that @code{tags-search} is a lot like
2360 @code{grep}. You can also run @code{grep} itself as an inferior of
2361 Emacs and have Emacs show you the matching lines one by one. This works
2362 much like running a compilation; finding the source locations of the
2363 @code{grep} matches works like finding the compilation errors.
2364 @xref{Compilation}.
2365
2366 @node List Tags
2367 @subsection Tags Table Inquiries
2368
2369 @table @kbd
2370 @item M-x list-tags @key{RET} @var{file} @key{RET}
2371 Display a list of the tags defined in the program file @var{file}.
2372 @item M-x tags-apropos @key{RET} @var{regexp} @key{RET}
2373 Display a list of all tags matching @var{regexp}.
2374 @end table
2375
2376 @findex list-tags
2377 @kbd{M-x list-tags} reads the name of one of the files described by
2378 the selected tags table, and displays a list of all the tags defined in
2379 that file. The ``file name'' argument is really just a string to
2380 compare against the file names recorded in the tags table; it is read as
2381 a string rather than as a file name. Therefore, completion and
2382 defaulting are not available, and you must enter the file name the same
2383 way it appears in the tags table. Do not include a directory as part of
2384 the file name unless the file name recorded in the tags table includes a
2385 directory.
2386
2387 @findex tags-apropos
2388 @kbd{M-x tags-apropos} is like @code{apropos} for tags
2389 (@pxref{Apropos}). It reads a regexp, then finds all the tags in the
2390 selected tags table whose entries match that regexp, and displays the
2391 tag names found.
2392 @vindex tags-apropos-additional-actions
2393 You can display additional output with @kbd{M-x tags-apropos} by customizing
2394 the variable @code{tags-apropos-additional-actions}. See its
2395 documentation for details.
2396
2397 You can also perform completion in the buffer on the name space of tag
2398 names in the current tags tables. @xref{Symbol Completion}.
2399
2400 @node Imenu
2401 @section Imenu
2402 @cindex indexes of buffer contents
2403 @cindex buffer content indexes
2404 @cindex tags
2405
2406 The Imenu package provides mode-specific indexes of the contents of
2407 single buffers and provides selection from a menu. Selecting a menu
2408 item takes you to the indexed point in the buffer, in a similar way to
2409 the Tags facility. Indexing is typically by names of program routines
2410 and variables but in Texinfo mode, for instance, node names are indexed.
2411 Most major modes for which it is appropriate have Imenu support.
2412
2413 @findex imenu
2414 @findex imenu-add-menu-bar-index
2415 @kbd{M-x imenu} builds the index if necessary and presents you with an
2416 electric buffer menu from which to select an entry (with completion).
2417 If you bind @code{imenu} to a mouse event (@pxref{Mouse Buttons}) and
2418 invoke it that way, the index will appear as a popup menu; there is no
2419 such binding by default. You can add an index menubar on the menubar
2420 with @kbd{imenu-add-menu-bar-index}.
2421
2422 Some major modes provide facilities for invoking Imenu; otherwise you
2423 could add @code{imenu-add-menu-bar-index} to a major mode's hook to
2424 generate an index for each buffer created in that mode. (If you do
2425 that, it takes sime time to generate the index when finding a file,
2426 depending on the file's size and the complexity of the indexing function
2427 for that mode.)
2428
2429 @vindex imenu-auto-rescan
2430 The index should be regenerated (via the @samp{*Rescan*} menu item) when
2431 indexable items are added to or deleted from the buffer. Rescanning is
2432 done when a menu selction is requested if the option
2433 @code{imenu-auto-rescan} is set. By default buffer positions are in
2434 terms of markers, so that changing non-indexable text doesn't require
2435 rescanning.
2436
2437 @vindex imenu-sort-function
2438 The way the menus are sorted can be customized via the option
2439 @code{imenu-sort-function}. By default names are ordered as they occur
2440 in the buffer; alphabetic sorting is provided as an alternative.
2441
2442 Imenu provides the information used by Which Function mode (@pxref{Which
2443 Function}). It may also be used by Speedbar (@pxref{Speedbar}).
2444
2445 @node Emerge, C Modes, Imenu, Programs
2446 @section Merging Files with Emerge
2447 @cindex Emerge
2448 @cindex merging files
2449
2450 It's not unusual for programmers to get their signals crossed and modify
2451 the same program in two different directions. To recover from this
2452 confusion, you need to merge the two versions. Emerge makes this
2453 easier. See also @ref{Comparing Files}, for commands to compare
2454 in a more manual fashion, and @ref{Emerge,,, ediff, The Ediff Manual}.
2455
2456 @menu
2457 * Overview of Emerge:: How to start Emerge. Basic concepts.
2458 * Submodes of Emerge:: Fast mode vs. Edit mode.
2459 Skip Prefers mode and Auto Advance mode.
2460 * State of Difference:: You do the merge by specifying state A or B
2461 for each difference.
2462 * Merge Commands:: Commands for selecting a difference,
2463 changing states of differences, etc.
2464 * Exiting Emerge:: What to do when you've finished the merge.
2465 * Combining in Emerge:: How to keep both alternatives for a difference.
2466 * Fine Points of Emerge:: Misc.
2467 @end menu
2468
2469 @node Overview of Emerge
2470 @subsection Overview of Emerge
2471
2472 To start Emerge, run one of these four commands:
2473
2474 @table @kbd
2475 @item M-x emerge-files
2476 @findex emerge-files
2477 Merge two specified files.
2478
2479 @item M-x emerge-files-with-ancestor
2480 @findex emerge-files-with-ancestor
2481 Merge two specified files, with reference to a common ancestor.
2482
2483 @item M-x emerge-buffers
2484 @findex emerge-buffers
2485 Merge two buffers.
2486
2487 @item M-x emerge-buffers-with-ancestor
2488 @findex emerge-buffers-with-ancestor
2489 Merge two buffers with reference to a common ancestor in a third
2490 buffer.
2491 @end table
2492
2493 @cindex merge buffer (Emerge)
2494 @cindex A and B buffers (Emerge)
2495 The Emerge commands compare two files or buffers, and display the
2496 comparison in three buffers: one for each input text (the @dfn{A buffer}
2497 and the @dfn{B buffer}), and one (the @dfn{merge buffer}) where merging
2498 takes place. The merge buffer shows the full merged text, not just the
2499 differences. Wherever the two input texts differ, you can choose which
2500 one of them to include in the merge buffer.
2501
2502 The Emerge commands that take input from existing buffers use only the
2503 accessible portions of those buffers, if they are narrowed
2504 (@pxref{Narrowing}).
2505
2506 If a common ancestor version is available, from which the two texts to
2507 be merged were both derived, Emerge can use it to guess which
2508 alternative is right. Wherever one current version agrees with the
2509 ancestor, Emerge presumes that the other current version is a deliberate
2510 change which should be kept in the merged version. Use the
2511 @samp{with-ancestor} commands if you want to specify a common ancestor
2512 text. These commands read three file or buffer names---variant A,
2513 variant B, and the common ancestor.
2514
2515 After the comparison is done and the buffers are prepared, the
2516 interactive merging starts. You control the merging by typing special
2517 @dfn{merge commands} in the merge buffer. The merge buffer shows you a
2518 full merged text, not just differences. For each run of differences
2519 between the input texts, you can choose which one of them to keep, or
2520 edit them both together.
2521
2522 The merge buffer uses a special major mode, Emerge mode, with commands
2523 for making these choices. But you can also edit the buffer with
2524 ordinary Emacs commands.
2525
2526 At any given time, the attention of Emerge is focused on one
2527 particular difference, called the @dfn{selected} difference. This
2528 difference is marked off in the three buffers like this:
2529
2530 @example
2531 vvvvvvvvvvvvvvvvvvvv
2532 @var{text that differs}
2533 ^^^^^^^^^^^^^^^^^^^^
2534 @end example
2535
2536 @noindent
2537 Emerge numbers all the differences sequentially and the mode
2538 line always shows the number of the selected difference.
2539
2540 Normally, the merge buffer starts out with the A version of the text.
2541 But when the A version of a difference agrees with the common ancestor,
2542 then the B version is initially preferred for that difference.
2543
2544 Emerge leaves the merged text in the merge buffer when you exit. At
2545 that point, you can save it in a file with @kbd{C-x C-w}. If you give a
2546 numeric argument to @code{emerge-files} or
2547 @code{emerge-files-with-ancestor}, it reads the name of the output file
2548 using the minibuffer. (This is the last file name those commands read.)
2549 Then exiting from Emerge saves the merged text in the output file.
2550
2551 Normally, Emerge commands save the output buffer in its file when you
2552 exit. If you abort Emerge with @kbd{C-]}, the Emerge command does not
2553 save the output buffer, but you can save it yourself if you wish.
2554
2555 @node Submodes of Emerge
2556 @subsection Submodes of Emerge
2557
2558 You can choose between two modes for giving merge commands: Fast mode
2559 and Edit mode. In Fast mode, basic merge commands are single
2560 characters, but ordinary Emacs commands are disabled. This is
2561 convenient if you use only merge commands. In Edit mode, all merge
2562 commands start with the prefix key @kbd{C-c C-c}, and the normal Emacs
2563 commands are also available. This allows editing the merge buffer, but
2564 slows down Emerge operations.
2565
2566 Use @kbd{e} to switch to Edit mode, and @kbd{C-c C-c f} to switch to
2567 Fast mode. The mode line indicates Edit and Fast modes with @samp{E}
2568 and @samp{F}.
2569
2570 Emerge has two additional submodes that affect how particular merge
2571 commands work: Auto Advance mode and Skip Prefers mode.
2572
2573 If Auto Advance mode is in effect, the @kbd{a} and @kbd{b} commands
2574 advance to the next difference. This lets you go through the merge
2575 faster as long as you simply choose one of the alternatives from the
2576 input. The mode line indicates Auto Advance mode with @samp{A}.
2577
2578 If Skip Prefers mode is in effect, the @kbd{n} and @kbd{p} commands
2579 skip over differences in states prefer-A and prefer-B (@pxref{State of
2580 Difference}). Thus you see only differences for which neither version
2581 is presumed ``correct.'' The mode line indicates Skip Prefers mode with
2582 @samp{S}.
2583
2584 @findex emerge-auto-advance-mode
2585 @findex emerge-skip-prefers-mode
2586 Use the command @kbd{s a} (@code{emerge-auto-advance-mode}) to set or
2587 clear Auto Advance mode. Use @kbd{s s}
2588 (@code{emerge-skip-prefers-mode}) to set or clear Skip Prefers mode.
2589 These commands turn on the mode with a positive argument, turns it off
2590 with a negative or zero argument, and toggle the mode with no argument.
2591
2592 @node State of Difference
2593 @subsection State of a Difference
2594
2595 In the merge buffer, a difference is marked with lines of @samp{v} and
2596 @samp{^} characters. Each difference has one of these seven states:
2597
2598 @table @asis
2599 @item A
2600 The difference is showing the A version. The @kbd{a} command always
2601 produces this state; the mode line indicates it with @samp{A}.
2602
2603 @item B
2604 The difference is showing the B version. The @kbd{b} command always
2605 produces this state; the mode line indicates it with @samp{B}.
2606
2607 @item default-A
2608 @itemx default-B
2609 The difference is showing the A or the B state by default, because you
2610 haven't made a choice. All differences start in the default-A state
2611 (and thus the merge buffer is a copy of the A buffer), except those for
2612 which one alternative is ``preferred'' (see below).
2613
2614 When you select a difference, its state changes from default-A or
2615 default-B to plain A or B. Thus, the selected difference never has
2616 state default-A or default-B, and these states are never displayed in
2617 the mode line.
2618
2619 The command @kbd{d a} chooses default-A as the default state, and @kbd{d
2620 b} chooses default-B. This chosen default applies to all differences
2621 which you haven't ever selected and for which no alternative is preferred.
2622 If you are moving through the merge sequentially, the differences you
2623 haven't selected are those following the selected one. Thus, while
2624 moving sequentially, you can effectively make the A version the default
2625 for some sections of the merge buffer and the B version the default for
2626 others by using @kbd{d a} and @kbd{d b} between sections.
2627
2628 @item prefer-A
2629 @itemx prefer-B
2630 The difference is showing the A or B state because it is
2631 @dfn{preferred}. This means that you haven't made an explicit choice,
2632 but one alternative seems likely to be right because the other
2633 alternative agrees with the common ancestor. Thus, where the A buffer
2634 agrees with the common ancestor, the B version is preferred, because
2635 chances are it is the one that was actually changed.
2636
2637 These two states are displayed in the mode line as @samp{A*} and @samp{B*}.
2638
2639 @item combined
2640 The difference is showing a combination of the A and B states, as a
2641 result of the @kbd{x c} or @kbd{x C} commands.
2642
2643 Once a difference is in this state, the @kbd{a} and @kbd{b} commands
2644 don't do anything to it unless you give them a numeric argument.
2645
2646 The mode line displays this state as @samp{comb}.
2647 @end table
2648
2649 @node Merge Commands
2650 @subsection Merge Commands
2651
2652 Here are the Merge commands for Fast mode; in Edit mode, precede them
2653 with @kbd{C-c C-c}:
2654
2655 @table @kbd
2656 @item p
2657 Select the previous difference.
2658
2659 @item n
2660 Select the next difference.
2661
2662 @item a
2663 Choose the A version of this difference.
2664
2665 @item b
2666 Choose the B version of this difference.
2667
2668 @item C-u @var{n} j
2669 Select difference number @var{n}.
2670
2671 @item .
2672 Select the difference containing point. You can use this command in the
2673 merge buffer or in the A or B buffer.
2674
2675 @item q
2676 Quit---finish the merge.
2677
2678 @item C-]
2679 Abort---exit merging and do not save the output.
2680
2681 @item f
2682 Go into Fast mode. (In Edit mode, this is actually @kbd{C-c C-c f}.)
2683
2684 @item e
2685 Go into Edit mode.
2686
2687 @item l
2688 Recenter (like @kbd{C-l}) all three windows.
2689
2690 @item -
2691 Specify part of a prefix numeric argument.
2692
2693 @item @var{digit}
2694 Also specify part of a prefix numeric argument.
2695
2696 @item d a
2697 Choose the A version as the default from here down in
2698 the merge buffer.
2699
2700 @item d b
2701 Choose the B version as the default from here down in
2702 the merge buffer.
2703
2704 @item c a
2705 Copy the A version of this difference into the kill ring.
2706
2707 @item c b
2708 Copy the B version of this difference into the kill ring.
2709
2710 @item i a
2711 Insert the A version of this difference at point.
2712
2713 @item i b
2714 Insert the B version of this difference at point.
2715
2716 @item m
2717 Put point and mark around the difference.
2718
2719 @item ^
2720 Scroll all three windows down (like @kbd{M-v}).
2721
2722 @item v
2723 Scroll all three windows up (like @kbd{C-v}).
2724
2725 @item <
2726 Scroll all three windows left (like @kbd{C-x <}).
2727
2728 @item >
2729 Scroll all three windows right (like @kbd{C-x >}).
2730
2731 @item |
2732 Reset horizontal scroll on all three windows.
2733
2734 @item x 1
2735 Shrink the merge window to one line. (Use @kbd{C-u l} to restore it
2736 to full size.)
2737
2738 @item x c
2739 Combine the two versions of this difference (@pxref{Combining in
2740 Emerge}).
2741
2742 @item x f
2743 Show the names of the files/buffers Emerge is operating on, in a Help
2744 window. (Use @kbd{C-u l} to restore windows.)
2745
2746 @item x j
2747 Join this difference with the following one.
2748 (@kbd{C-u x j} joins this difference with the previous one.)
2749
2750 @item x s
2751 Split this difference into two differences. Before you use this
2752 command, position point in each of the three buffers at the place where
2753 you want to split the difference.
2754
2755 @item x t
2756 Trim identical lines off the top and bottom of the difference.
2757 Such lines occur when the A and B versions are
2758 identical but differ from the ancestor version.
2759 @end table
2760
2761 @node Exiting Emerge
2762 @subsection Exiting Emerge
2763
2764 The @kbd{q} command (@code{emerge-quit}) finishes the merge, storing
2765 the results into the output file if you specified one. It restores the
2766 A and B buffers to their proper contents, or kills them if they were
2767 created by Emerge and you haven't changed them. It also disables the
2768 Emerge commands in the merge buffer, since executing them later could
2769 damage the contents of the various buffers.
2770
2771 @kbd{C-]} aborts the merge. This means exiting without writing the
2772 output file. If you didn't specify an output file, then there is no
2773 real difference between aborting and finishing the merge.
2774
2775 If the Emerge command was called from another Lisp program, then its
2776 return value is @code{t} for successful completion, or @code{nil} if you
2777 abort.
2778
2779 @node Combining in Emerge
2780 @subsection Combining the Two Versions
2781
2782 Sometimes you want to keep @emph{both} alternatives for a particular
2783 difference. To do this, use @kbd{x c}, which edits the merge buffer
2784 like this:
2785
2786 @example
2787 @group
2788 #ifdef NEW
2789 @var{version from A buffer}
2790 #else /* not NEW */
2791 @var{version from B buffer}
2792 #endif /* not NEW */
2793 @end group
2794 @end example
2795
2796 @noindent
2797 @vindex emerge-combine-versions-template
2798 While this example shows C preprocessor conditionals delimiting the two
2799 alternative versions, you can specify the strings to use by setting
2800 the variable @code{emerge-combine-versions-template} to a string of your
2801 choice. In the string, @samp{%a} says where to put version A, and
2802 @samp{%b} says where to put version B. The default setting, which
2803 produces the results shown above, looks like this:
2804
2805 @example
2806 @group
2807 "#ifdef NEW\n%a#else /* not NEW */\n%b#endif /* not NEW */\n"
2808 @end group
2809 @end example
2810
2811 @node Fine Points of Emerge
2812 @subsection Fine Points of Emerge
2813
2814 During the merge, you mustn't try to edit the A and B buffers yourself.
2815 Emerge modifies them temporarily, but ultimately puts them back the way
2816 they were.
2817
2818 You can have any number of merges going at once---just don't use any one
2819 buffer as input to more than one merge at once, since the temporary
2820 changes made in these buffers would get in each other's way.
2821
2822 Starting Emerge can take a long time because it needs to compare the
2823 files fully. Emacs can't do anything else until @code{diff} finishes.
2824 Perhaps in the future someone will change Emerge to do the comparison in
2825 the background when the input files are large---then you could keep on
2826 doing other things with Emacs until Emerge is ready to accept
2827 commands.
2828
2829 @vindex emerge-startup-hook
2830 After setting up the merge, Emerge runs the hook
2831 @code{emerge-startup-hook} (@pxref{Hooks}).
2832
2833 @node C Modes
2834 @section C and Related Modes
2835 @cindex C mode
2836 @cindex Java mode
2837 @cindex Pike mode
2838 @cindex IDL mode
2839 @cindex CORBA IDL mode
2840 @cindex Objective C mode
2841 @cindex C++ mode
2842 @cindex mode, Java
2843 @cindex mode, C
2844 @cindex mode, Objective C
2845 @cindex mode, CORBA IDL
2846 @cindex mode, Pike
2847
2848 This section describes special features available in C, C++,
2849 Objective-C, Java, CORBA IDL, and Pike modes. When we say ``C mode and
2850 related modes,'' those are the modes we mean.
2851
2852 Additional information is available in the separate manual for these
2853 modes. @xref{Top, CC Mode, ccmode, , CC Mode}.
2854
2855 @menu
2856 * Motion in C::
2857 * Electric C::
2858 * Hungry Delete::
2859 * Other C Commands::
2860 * Comments in C::
2861 @end menu
2862
2863 @node Motion in C
2864 @subsection C Mode Motion Commands
2865
2866 This section describes commands for moving point, in C mode and
2867 related modes.
2868
2869 @table @code
2870 @item C-c C-u
2871 @kindex C-c C-u @r{(C mode)}
2872 @findex c-up-conditional
2873 Move point back to the containing preprocessor conditional, leaving the
2874 mark behind. A prefix argument acts as a repeat count. With a negative
2875 argument, move point forward to the end of the containing
2876 preprocessor conditional. When going backwards, @code{#elif} is treated
2877 like @code{#else} followed by @code{#if}. When going forwards,
2878 @code{#elif} is ignored.@refill
2879
2880 @item C-c C-p
2881 @kindex C-c C-p @r{(C mode)}
2882 @findex c-backward-conditional
2883 Move point back over a preprocessor conditional, leaving the mark
2884 behind. A prefix argument acts as a repeat count. With a negative
2885 argument, move forward.
2886
2887 @item C-c C-n
2888 @kindex C-c C-n @r{(C mode)}
2889 @findex c-forward-conditional
2890 Move point forward across a preprocessor conditional, leaving the mark
2891 behind. A prefix argument acts as a repeat count. With a negative
2892 argument, move backward.
2893
2894 @item M-a
2895 @kindex ESC a
2896 @findex c-beginning-of-statement
2897 Move point to the beginning of the innermost C statement
2898 (@code{c-beginning-of-statement}). If point is already at the beginning
2899 of a statement, move to the beginning of the preceding statement. With
2900 prefix argument @var{n}, move back @var{n} @minus{} 1 statements.
2901
2902 If point is within a string or comment, or next to a comment (only
2903 whitespace between them), this command moves by sentences instead of
2904 statements.
2905
2906 When called from a program, this function takes three optional
2907 arguments: the numeric prefix argument, a buffer position limit
2908 (don't move back before that place), and a flag that controls whether
2909 to do sentence motion when inside of a comment.
2910
2911 @item M-e
2912 @kindex ESC e
2913 @findex c-end-of-statement
2914 Move point to the end of the innermost C statement; like @kbd{M-a}
2915 except that it moves in the other direction (@code{c-end-of-statement}).
2916
2917 @item M-x c-backward-into-nomenclature
2918 @findex c-backward-into-nomenclature
2919 Move point backward to beginning of a C++ nomenclature section or word.
2920 With prefix argument @var{n}, move @var{n} times. If @var{n} is
2921 negative, move forward. C++ nomenclature means a symbol name in the
2922 style of NamingSymbolsWithMixedCaseAndNoUnderlines; each capital letter
2923 begins a section or word.
2924
2925 In the GNU project, we recommend using underscores to separate words
2926 within an identifier in C or C++, rather than using case distinctions.
2927
2928 @item M-x c-forward-into-nomenclature
2929 @findex c-forward-into-nomenclature
2930 Move point forward to end of a C++ nomenclature section or word.
2931 With prefix argument @var{n}, move @var{n} times.
2932 @end table
2933
2934 @node Electric C
2935 @subsection Electric C Characters
2936
2937 In C mode and related modes, certain printing characters are
2938 ``electric''---in addition to inserting themselves, they also reindent
2939 the current line and may insert newlines. This feature is controlled by
2940 the variable @code{c-auto-newline}. The ``electric'' characters are
2941 @kbd{@{}, @kbd{@}}, @kbd{:}, @kbd{#}, @kbd{;}, @kbd{,}, @kbd{<},
2942 @kbd{>}, @kbd{/}, @kbd{*}, @kbd{(}, and @kbd{)}.
2943
2944 Electric characters insert newlines only when the @dfn{auto-newline}
2945 feature is enabled (indicated by @samp{/a} in the mode line after the
2946 mode name). This feature is controlled by the variable
2947 @code{c-auto-newline}. You can turn this feature on or off with the
2948 command @kbd{C-c C-a}:
2949
2950 @table @kbd
2951 @item C-c C-a
2952 @kindex C-c C-a @r{(C mode)}
2953 @findex c-toggle-auto-state
2954 Toggle the auto-newline feature (@code{c-toggle-auto-state}). With a
2955 prefix argument, this command turns the auto-newline feature on if the
2956 argument is positive, and off if it is negative.
2957 @end table
2958
2959 The colon character is electric because that is appropriate for a
2960 single colon. But when you want to insert a double colon in C++, the
2961 electric behavior of colon is inconvenient. You can insert a double
2962 colon with no reindentation or newlines by typing @kbd{C-c :}:
2963
2964 @table @kbd
2965 @item C-c :
2966 @kindex C-c : @r{(C mode)}
2967 @findex c-scope-operator
2968 Insert a double colon scope operator at point, without reindenting the
2969 line or adding any newlines (@code{c-scope-operator}).
2970 @end table
2971
2972 The electric @kbd{#} key reindents the line if it appears to be the
2973 beginning of a preprocessor directive. This happens when the value of
2974 @code{c-electric-pound-behavior} is @code{(alignleft)}. You can turn
2975 this feature off by setting @code{c-electric-pound-behavior} to
2976 @code{nil}.
2977
2978 The variable @code{c-hanging-braces-alist} controls the insertion of
2979 newlines before and after inserted braces. It is an association list
2980 with elements of the following form: @code{(@var{syntactic-symbol}
2981 . @var{nl-list})}. Most of the syntactic symbols that appear in
2982 @code{c-offsets-alist} are meaningful here as well.
2983
2984 The list @var{nl-list} may contain either of the symbols
2985 @code{before} or @code{after}, or both; or it may be @code{nil}. When a
2986 brace is inserted, the syntactic context it defines is looked up in
2987 @code{c-hanging-braces-alist}; if it is found, the @var{nl-list} is used
2988 to determine where newlines are inserted: either before the brace,
2989 after, or both. If not found, the default is to insert a newline both
2990 before and after braces.
2991
2992 The variable @code{c-hanging-colons-alist} controls the insertion of
2993 newlines before and after inserted colons. It is an association list
2994 with elements of the following form: @code{(@var{syntactic-symbol}
2995 . @var{nl-list})}. The list @var{nl-list} may contain either of the
2996 symbols @code{before} or @code{after}, or both; or it may be @code{nil}.
2997
2998 When a colon is inserted, the syntactic symbol it defines is looked
2999 up in this list, and if found, the @var{nl-list} is used to determine
3000 where newlines are inserted: either before the brace, after, or both.
3001 If the syntactic symbol is not found in this list, no newlines are
3002 inserted.
3003
3004 Electric characters can also delete newlines automatically when the
3005 auto-newline feature is enabled. This feature makes auto-newline more
3006 acceptable, by deleting the newlines in the most common cases where you
3007 do not want them. Emacs can recognize several cases in which deleting a
3008 newline might be desirable; by setting the variable
3009 @code{c-cleanup-list}, you can specify @emph{which} of these cases that
3010 should happen. The variable's value is a list of symbols, each
3011 describing one case for possible deletion of a newline. Here are the
3012 meaningful symbols, and their meanings:
3013
3014 @table @code
3015 @item brace-catch-brace
3016 Clean up @samp{@} catch (@var{condition}) @{} constructs by placing the
3017 entire construct on a single line. The clean-up occurs when you type
3018 the @samp{@{}, if there is nothing between the braces aside from
3019 @code{catch} and @var{condition}.
3020
3021 @item brace-else-brace
3022 Clean up @samp{@} else @{} constructs by placing the entire construct on
3023 a single line. The clean-up occurs when you type the @samp{@{} after
3024 the @code{else}, but only if there is nothing but white space between
3025 the braces and the @code{else}.
3026
3027 @item brace-elseif-brace
3028 Clean up @samp{@} else if (@dots{}) @{} constructs by placing the entire
3029 construct on a single line. The clean-up occurs when you type the
3030 @samp{@{}, if there is nothing but white space between the @samp{@}} and
3031 @samp{@{} aside from the keywords and the @code{if}-condition.
3032
3033 @item empty-defun-braces
3034 Clean up empty defun braces by placing the braces on the same
3035 line. Clean-up occurs when you type the closing brace.
3036
3037 @item defun-close-semi
3038 Clean up the semicolon after a @code{struct} or similar type
3039 declaration, by placing the semicolon on the same line as the closing
3040 brace. Clean-up occurs when you type the semicolon.
3041
3042 @item list-close-comma
3043 Clean up commas following braces in array and aggregate
3044 initializers. Clean-up occurs when you type the comma.
3045
3046 @item scope-operator
3047 Clean up double colons which may designate a C++ scope operator, by
3048 placing the colons together. Clean-up occurs when you type the second
3049 colon, but only when the two colons are separated by nothing but
3050 whitespace.
3051 @end table
3052
3053 @node Hungry Delete
3054 @subsection Hungry Delete Feature in C
3055
3056 When the @dfn{hungry-delete} feature is enabled (indicated by
3057 @samp{/h} or @samp{/ah} in the mode line after the mode name), a single
3058 @key{DEL} command deletes all preceding whitespace, not just one space.
3059 To turn this feature on or off, use @kbd{C-c C-d}:
3060
3061 @table @kbd
3062 @item C-c C-d
3063 @kindex C-c C-d @r{(C mode)}
3064 @findex c-toggle-hungry-state
3065 Toggle the hungry-delete feature (@code{c-toggle-hungry-state}). With a
3066 prefix argument, this command turns the hungry-delete feature on if the
3067 argument is positive, and off if it is negative.
3068
3069 @item C-c C-t
3070 @kindex C-c C-t @r{(C mode)}
3071 @findex c-toggle-auto-hungry-state
3072 Toggle the auto-newline and hungry-delete features, both at once
3073 (@code{c-toggle-auto-hungry-state}).
3074 @end table
3075
3076 @vindex c-hungry-delete-key
3077 The variable @code{c-hungry-delete-key} controls whether the
3078 hungry-delete feature is enabled.
3079
3080 @node Other C Commands
3081 @subsection Other Commands for C Mode
3082
3083 @table @kbd
3084 @item C-M-h
3085 @findex c-mark-function
3086 @kindex C-M-h @r{(C mode)}
3087 Put mark at the end of a function definition, and put point at the
3088 beginning (@code{c-mark-function}).
3089
3090 @item M-q
3091 @kindex M-q @r{(C mode)}
3092 @findex c-fill-paragraph
3093 Fill a paragraph, handling C and C++ comments (@code{c-fill-paragraph}).
3094 If any part of the current line is a comment or within a comment, this
3095 command fills the comment or the paragraph of it that point is in,
3096 preserving the comment indentation and comment delimiters.
3097
3098 @item C-c C-e
3099 @cindex macro expansion in C
3100 @cindex expansion of C macros
3101 @findex c-macro-expand
3102 @kindex C-c C-e @r{(C mode)}
3103 Run the C preprocessor on the text in the region, and show the result,
3104 which includes the expansion of all the macro calls
3105 (@code{c-macro-expand}). The buffer text before the region is also
3106 included in preprocessing, for the sake of macros defined there, but the
3107 output from this part isn't shown.
3108
3109 When you are debugging C code that uses macros, sometimes it is hard to
3110 figure out precisely how the macros expand. With this command, you
3111 don't have to figure it out; you can see the expansions.
3112
3113 @item C-c C-\
3114 @findex c-backslash-region
3115 @kindex C-c C-\ @r{(C mode)}
3116 Insert or align @samp{\} characters at the ends of the lines of the
3117 region (@code{c-backslash-region}). This is useful after writing or
3118 editing a C macro definition.
3119
3120 If a line already ends in @samp{\}, this command adjusts the amount of
3121 whitespace before it. Otherwise, it inserts a new @samp{\}. However,
3122 the last line in the region is treated specially; no @samp{\} is
3123 inserted on that line, and any @samp{\} there is deleted.
3124
3125 @item M-x cpp-highlight-buffer
3126 @cindex preprocessor highlighting
3127 @findex cpp-highlight-buffer
3128 Highlight parts of the text according to its preprocessor conditionals.
3129 This command displays another buffer named @samp{*CPP Edit*}, which
3130 serves as a graphic menu for selecting how to display particular kinds
3131 of conditionals and their contents. After changing various settings,
3132 click on @samp{[A]pply these settings} (or go to that buffer and type
3133 @kbd{a}) to rehighlight the C mode buffer accordingly.
3134
3135 @item C-c C-s
3136 @findex c-show-syntactic-information
3137 @kindex C-c C-s @r{(C mode)}
3138 Display the syntactic information about the current source line
3139 (@code{c-show-syntactic-information}). This is the information that
3140 directs how the line is indented.
3141 @end table
3142
3143 @node Comments in C
3144 @subsection Comments in C Modes
3145
3146 C mode and related modes use a number of variables for controlling
3147 comment format.
3148
3149 @table @code
3150 @item c-comment-only-line-offset
3151 @vindex c-comment-only-line-offset
3152 Extra offset for line which contains only the start of a comment. It
3153 can be either an integer or a cons cell of the form
3154 @code{(@var{non-anchored-offset} . @var{anchored-offset})}, where
3155 @var{non-anchored-offset} is the amount of offset given to
3156 non-column-zero anchored comment-only lines, and @var{anchored-offset}
3157 is the amount of offset to give column-zero anchored comment-only lines.
3158 Just an integer as value is equivalent to @code{(@var{val} . 0)}.
3159
3160 @item c-comment-start-regexp
3161 @vindex c-comment-start-regexp
3162 This buffer-local variable specifies how to recognize the start of a comment.
3163
3164 @item c-hanging-comment-ender-p
3165 @vindex c-hanging-comment-ender-p
3166 If this variable is @code{nil}, @code{c-fill-paragraph} leaves the
3167 comment terminator of a block comment on a line by itself. The default
3168 value is @code{t}, which puts the comment-end delimiter @samp{*/} at the
3169 end of the last line of the comment text.
3170
3171 @item c-hanging-comment-starter-p
3172 @vindex c-hanging-comment-starter-p
3173 If this variable is @code{nil}, @code{c-fill-paragraph} leaves the
3174 starting delimiter of a block comment on a line by itself. The default
3175 value is @code{t}, which puts the comment-start delimiter @samp{/*} at
3176 the beginning of the first line of the comment text.
3177 @end table
3178
3179 @node Fortran
3180 @section Fortran Mode
3181 @cindex Fortran mode
3182 @cindex mode, Fortran
3183
3184 Fortran mode provides special motion commands for Fortran statements and
3185 subprograms, and indentation commands that understand Fortran conventions
3186 of nesting, line numbers and continuation statements. Fortran mode has
3187 its own Auto Fill mode that breaks long lines into proper Fortran
3188 continuation lines.
3189
3190 Special commands for comments are provided because Fortran comments
3191 are unlike those of other languages. Built-in abbrevs optionally save
3192 typing when you insert Fortran keywords.
3193
3194 @findex fortran-mode
3195 Use @kbd{M-x fortran-mode} to switch to this major mode. This command
3196 runs the hook @code{fortran-mode-hook} (@pxref{Hooks}).
3197
3198 @cindex Fortran77
3199 @cindex Fortran90
3200 @findex f90-mode
3201 @findex fortran-mode
3202 Note that Fortan mode described here (obtained with the
3203 @code{fortran-mode} command) is for editing the old Fortran77
3204 idiosyncratic `fixed format' source form. For editing the modern
3205 Fortran90 `free format' source form (which is supported by the GNU
3206 Fortran compiler) use @code{f90-mode}.
3207
3208 By default @code{fortran-mode} is invoked on files with extension
3209 @samp{.f}, @samp{.F} or @samp{.for} and @code{f90-mode} is invoked for
3210 the extension @samp{.f90}.
3211
3212 @menu
3213 * Motion: Fortran Motion. Moving point by statements or subprograms.
3214 * Indent: Fortran Indent. Indentation commands for Fortran.
3215 * Comments: Fortran Comments. Inserting and aligning comments.
3216 * Autofill: Fortran Autofill. Auto fill minor mode for Fortran.
3217 * Columns: Fortran Columns. Measuring columns for valid Fortran.
3218 * Abbrev: Fortran Abbrev. Built-in abbrevs for Fortran keywords.
3219 * Misc: Fortran Misc. Other Fortran mode features.
3220 @end menu
3221
3222 @node Fortran Motion
3223 @subsection Motion Commands
3224
3225 In addition to the normal commands for moving by and operating on
3226 `defuns' (Fortran subprograms---functions
3227 and subroutines) Fortran mode provides special commands to move by statements.
3228
3229 @kindex C-c C-p @r{(Fortran mode)}
3230 @kindex C-c C-n @r{(Fortran mode)}
3231 @findex fortran-previous-statement
3232 @findex fortran-next-statement
3233
3234 @table @kbd
3235 @item C-c C-n
3236 Move to beginning of current or next statement
3237 (@code{fortran-next-statement}).
3238 @item C-c C-p
3239 Move to beginning of current or previous statement
3240 (@code{fortran-previous-statement}).
3241 @end table
3242
3243 @node Fortran Indent
3244 @subsection Fortran Indentation
3245
3246 Special commands and features are needed for indenting Fortran code in
3247 order to make sure various syntactic entities (line numbers, comment line
3248 indicators and continuation line flags) appear in the columns that are
3249 required for standard Fortran.
3250
3251 @menu
3252 * Commands: ForIndent Commands. Commands for indenting and filling Fortran.
3253 * Contline: ForIndent Cont. How continuation lines indent.
3254 * Numbers: ForIndent Num. How line numbers auto-indent.
3255 * Conv: ForIndent Conv. Conventions you must obey to avoid trouble.
3256 * Vars: ForIndent Vars. Variables controlling Fortran indent style.
3257 @end menu
3258
3259 @node ForIndent Commands
3260 @subsubsection Fortran-Specific Indentation and Filling Commands
3261
3262 @table @kbd
3263 @item C-M-j
3264 Break the current line and set up a continuation line
3265 (@code{fortran-split-line}).
3266 @item M-^
3267 Join this line to the previous line (@code{fortran-join-line}).
3268 @item C-M-q
3269 Indent all the lines of the subprogram point is in
3270 (@code{fortran-indent-subprogram}).
3271 @item M-q
3272 Fill a comment block or statement.
3273 @end table
3274
3275 @kindex C-M-q @r{(Fortran mode)}
3276 @findex fortran-indent-subprogram
3277 The key @kbd{C-M-q} runs @code{fortran-indent-subprogram}, a command
3278 to reindent all the lines of the Fortran subprogram (function or
3279 subroutine) containing point.
3280
3281 @kindex C-M-j @r{(Fortran mode)}
3282 @findex fortran-split-line
3283 The key @kbd{C-M-j} runs @code{fortran-split-line}, which splits
3284 a line in the appropriate fashion for Fortran. In a non-comment line,
3285 the second half becomes a continuation line and is indented
3286 accordingly. In a comment line, both halves become separate comment
3287 lines.
3288
3289 @kindex M-^ @r{(Fortran mode)}
3290 @kindex C-c C-d @r{(Fortran mode)}
3291 @findex fortran-join-line
3292 @kbd{M-^} or @kbd{C-c C-d} runs the command @code{fortran-join-line},
3293 which joins a continuation line back to the previous line, roughly as
3294 the inverse of @code{fortran-split-line}. The point must be on a
3295 continuation line when this command is invoked.
3296
3297 @kindex M-q @r{(Fortran mode)}
3298 Fortran mode defines the function for filling paragraphs such that
3299 @kbd{M-q} fills the comment block or statement around point. Filling a
3300 statement removes excess statement continuations.
3301
3302 @node ForIndent Cont
3303 @subsubsection Continuation Lines
3304 @cindex Fortran continuation lines
3305
3306 @vindex fortran-continuation-string
3307 Most modern Fortran compilers allow two ways of writing continuation
3308 lines. If the first non-space character on a line is in column 5, then
3309 that line is a continuation of the previous line. We call this
3310 @dfn{fixed format}. (In GNU Emacs we always count columns from 0.) The
3311 variable @code{fortran-continuation-string} specifies what character to
3312 put on column 5. A line that starts with a tab character followed by
3313 any digit except @samp{0} is also a continuation line. We call this
3314 style of continuation @dfn{tab format}.
3315
3316 @vindex indent-tabs-mode @r{(Fortran mode)}
3317 Fortran mode can make either style of continuation line, but you
3318 must specify which one you prefer. The value of the variable
3319 @code{indent-tabs-mode} controls the choice: @code{nil} for fixed
3320 format, and non-@code{nil} for tab format. You can tell which style
3321 is presently in effect by the presence or absence of the string
3322 @samp{Tab} in the mode line.
3323
3324 If the text on a line starts with the conventional Fortran
3325 continuation marker @samp{$}, or if it begins with any non-whitespace
3326 character in column 5, Fortran mode treats it as a continuation line.
3327 When you indent a continuation line with @key{TAB}, it converts the line
3328 to the current continuation style. When you split a Fortran statement
3329 with @kbd{C-M-j}, the continuation marker on the newline is created
3330 according to the continuation style.
3331
3332 The setting of continuation style affects several other aspects of
3333 editing in Fortran mode. In fixed format mode, the minimum column
3334 number for the body of a statement is 6. Lines inside of Fortran
3335 blocks that are indented to larger column numbers always use only the
3336 space character for whitespace. In tab format mode, the minimum
3337 column number for the statement body is 8, and the whitespace before
3338 column 8 must always consist of one tab character.
3339
3340 @vindex fortran-tab-mode-default
3341 @vindex fortran-analyze-depth
3342 When you enter Fortran mode for an existing file, it tries to deduce the
3343 proper continuation style automatically from the file contents. The first
3344 line that begins with either a tab character or six spaces determines the
3345 choice. The variable @code{fortran-analyze-depth} specifies how many lines
3346 to consider (at the beginning of the file); if none of those lines
3347 indicates a style, then the variable @code{fortran-tab-mode-default}
3348 specifies the style. If it is @code{nil}, that specifies fixed format, and
3349 non-@code{nil} specifies tab format.
3350
3351 @node ForIndent Num
3352 @subsubsection Line Numbers
3353
3354 If a number is the first non-whitespace in the line, Fortran
3355 indentation assumes it is a line number and moves it to columns 0
3356 through 4. (Columns always count from 0 in GNU Emacs.)
3357
3358 @vindex fortran-line-number-indent
3359 Line numbers of four digits or less are normally indented one space.
3360 The variable @code{fortran-line-number-indent} controls this; it
3361 specifies the maximum indentation a line number can have. Line numbers
3362 are indented to right-justify them to end in column 4 unless that would
3363 require more than this maximum indentation. The default value of the
3364 variable is 1.
3365
3366 @vindex fortran-electric-line-number
3367 Simply inserting a line number is enough to indent it according to
3368 these rules. As each digit is inserted, the indentation is recomputed.
3369 To turn off this feature, set the variable
3370 @code{fortran-electric-line-number} to @code{nil}. Then inserting line
3371 numbers is like inserting anything else.
3372
3373 @node ForIndent Conv
3374 @subsubsection Syntactic Conventions
3375
3376 Fortran mode assumes that you follow certain conventions that simplify
3377 the task of understanding a Fortran program well enough to indent it
3378 properly:
3379
3380 @itemize @bullet
3381 @item
3382 Two nested @samp{do} loops never share a @samp{continue} statement.
3383
3384 @item
3385 Fortran keywords such as @samp{if}, @samp{else}, @samp{then}, @samp{do}
3386 and others are written without embedded whitespace or line breaks.
3387
3388 Fortran compilers generally ignore whitespace outside of string
3389 constants, but Fortran mode does not recognize these keywords if they
3390 are not contiguous. Constructs such as @samp{else if} or @samp{end do}
3391 are acceptable, but the second word should be on the same line as the
3392 first and not on a continuation line.
3393 @end itemize
3394
3395 @noindent
3396 If you fail to follow these conventions, the indentation commands may
3397 indent some lines unaesthetically. However, a correct Fortran program
3398 retains its meaning when reindented even if the conventions are not
3399 followed.
3400
3401 @node ForIndent Vars
3402 @subsubsection Variables for Fortran Indentation
3403
3404 @vindex fortran-do-indent
3405 @vindex fortran-if-indent
3406 @vindex fortran-structure-indent
3407 @vindex fortran-continuation-indent
3408 @vindex fortran-check-all-num@dots{}
3409 @vindex fortran-minimum-statement-indent@dots{}
3410 Several additional variables control how Fortran indentation works:
3411
3412 @table @code
3413 @item fortran-do-indent
3414 Extra indentation within each level of @samp{do} statement (default 3).
3415
3416 @item fortran-if-indent
3417 Extra indentation within each level of @samp{if} statement (default 3).
3418 This value is also used for extra indentation within each level of the
3419 Fortran 90 @samp{where} statement.
3420
3421 @item fortran-structure-indent
3422 Extra indentation within each level of @samp{structure}, @samp{union}, or
3423 @samp{map} statements (default 3).
3424
3425 @item fortran-continuation-indent
3426 Extra indentation for bodies of continuation lines (default 5).
3427
3428 @item fortran-check-all-num-for-matching-do
3429 If this is @code{nil}, indentation assumes that each @samp{do} statement
3430 ends on a @samp{continue} statement. Therefore, when computing
3431 indentation for a statement other than @samp{continue}, it can save time
3432 by not checking for a @samp{do} statement ending there. If this is
3433 non-@code{nil}, indenting any numbered statement must check for a
3434 @samp{do} that ends there. The default is @code{nil}.
3435
3436 @item fortran-blink-matching-if
3437 If this is @code{t}, indenting an @samp{endif} statement moves the
3438 cursor momentarily to the matching @samp{if} statement to show where it
3439 is. The default is @code{nil}.
3440
3441 @item fortran-minimum-statement-indent-fixed
3442 Minimum indentation for fortran statements when using fixed format
3443 continuation line style. Statement bodies are never indented less than
3444 this much. The default is 6.
3445
3446 @item fortran-minimum-statement-indent-tab
3447 Minimum indentation for fortran statements for tab format continuation line
3448 style. Statement bodies are never indented less than this much. The
3449 default is 8.
3450 @end table
3451
3452 @node Fortran Comments
3453 @subsection Fortran Comments
3454
3455 The usual Emacs comment commands assume that a comment can follow a line
3456 of code. In Fortran, the standard comment syntax requires an entire line
3457 to be just a comment. Therefore, Fortran mode replaces the standard Emacs
3458 comment commands and defines some new variables.
3459
3460 Fortran mode can also handle the Fortran90 comment syntax where comments
3461 start with @samp{!} and can follow other text. Because only some Fortran77
3462 compilers accept this syntax, Fortran mode will not insert such comments
3463 unless you have said in advance to do so. To do this, set the variable
3464 @code{comment-start} to @samp{"!"} (@pxref{Variables}).
3465
3466 @table @kbd
3467 @item M-;
3468 Align comment or insert new comment (@code{fortran-comment-indent}).
3469
3470 @item C-x ;
3471 Applies to nonstandard @samp{!} comments only.
3472
3473 @item C-c ;
3474 Turn all lines of the region into comments, or (with argument) turn them back
3475 into real code (@code{fortran-comment-region}).
3476 @end table
3477
3478 @kbd{M-;} in Fortran mode is redefined as the command
3479 @code{fortran-comment-indent}. Like the usual @kbd{M-;} command, this
3480 recognizes any kind of existing comment and aligns its text appropriately;
3481 if there is no existing comment, a comment is inserted and aligned. But
3482 inserting and aligning comments are not the same in Fortran mode as in
3483 other modes.
3484
3485 When a new comment must be inserted, if the current line is blank, a
3486 full-line comment is inserted. On a non-blank line, a nonstandard @samp{!}
3487 comment is inserted if you have said you want to use them. Otherwise a
3488 full-line comment is inserted on a new line before the current line.
3489
3490 Nonstandard @samp{!} comments are aligned like comments in other
3491 languages, but full-line comments are different. In a standard full-line
3492 comment, the comment delimiter itself must always appear in column zero.
3493 What can be aligned is the text within the comment. You can choose from
3494 three styles of alignment by setting the variable
3495 @code{fortran-comment-indent-style} to one of these values:
3496
3497 @vindex fortran-comment-indent-style
3498 @vindex fortran-comment-line-extra-indent
3499 @table @code
3500 @item fixed
3501 Align the text at a fixed column, which is the sum of
3502 @code{fortran-comment-line-extra-indent} and the minimum statement
3503 indentation. This is the default.
3504
3505 The minimum statement indentation is
3506 @code{fortran-minimum-statement-indent-fixed} for fixed format
3507 continuation line style and @code{fortran-minimum-statement-indent-tab}
3508 for tab format style.
3509
3510 @item relative
3511 Align the text as if it were a line of code, but with an additional
3512 @code{fortran-comment-line-extra-indent} columns of indentation.
3513
3514 @item nil
3515 Don't move text in full-line comments automatically at all.
3516 @end table
3517
3518 @vindex fortran-comment-indent-char
3519 In addition, you can specify the character to be used to indent within
3520 full-line comments by setting the variable
3521 @code{fortran-comment-indent-char} to the single-character string you want
3522 to use.
3523
3524 @vindex comment-line-start
3525 @vindex comment-line-start-skip
3526 Fortran mode introduces two variables @code{comment-line-start} and
3527 @code{comment-line-start-skip}, which play for full-line comments the same
3528 roles played by @code{comment-start} and @code{comment-start-skip} for
3529 ordinary text-following comments. Normally these are set properly by
3530 Fortran mode, so you do not need to change them.
3531
3532 The normal Emacs comment command @kbd{C-x ;} has not been redefined. If
3533 you use @samp{!} comments, this command can be used with them. Otherwise
3534 it is useless in Fortran mode.
3535
3536 @kindex C-c ; @r{(Fortran mode)}
3537 @findex fortran-comment-region
3538 @vindex fortran-comment-region
3539 The command @kbd{C-c ;} (@code{fortran-comment-region}) turns all the
3540 lines of the region into comments by inserting the string @samp{C$$$} at
3541 the front of each one. With a numeric argument, it turns the region
3542 back into live code by deleting @samp{C$$$} from the front of each line
3543 in it. The string used for these comments can be controlled by setting
3544 the variable @code{fortran-comment-region}. Note that here we have an
3545 example of a command and a variable with the same name; these two uses
3546 of the name never conflict because in Lisp and in Emacs it is always
3547 clear from the context which one is meant.
3548
3549 @node Fortran Autofill
3550 @subsection Fortran Auto Fill Mode
3551
3552 Fortran Auto Fill mode is a minor mode which automatically splits
3553 Fortran statements as you insert them when they become too wide.
3554 Splitting a statement involves making continuation lines using
3555 @code{fortran-continuation-string} (@pxref{ForIndent Cont}). This
3556 splitting happens when you type @key{SPC}, @key{RET}, or @key{TAB}, and
3557 also in the Fortran indentation commands.
3558
3559 @findex fortran-auto-fill-mode
3560 @kbd{M-x fortran-auto-fill-mode} turns Fortran Auto Fill mode on if it
3561 was off, or off if it was on. This command works the same as @kbd{M-x
3562 auto-fill-mode} does for normal Auto Fill mode (@pxref{Filling}). A
3563 positive numeric argument turns Fortran Auto Fill mode on, and a
3564 negative argument turns it off. You can see when Fortran Auto Fill mode
3565 is in effect by the presence of the word @samp{Fill} in the mode line,
3566 inside the parentheses. Fortran Auto Fill mode is a minor mode, turned
3567 on or off for each buffer individually. @xref{Minor Modes}.
3568
3569 @vindex fortran-break-before-delimiters
3570 Fortran Auto Fill mode breaks lines at spaces or delimiters when the
3571 lines get longer than the desired width (the value of @code{fill-column}).
3572 The delimiters that Fortran Auto Fill mode may break at are @samp{,},
3573 @samp{'}, @samp{+}, @samp{-}, @samp{/}, @samp{*}, @samp{=}, and @samp{)}.
3574 The line break comes after the delimiter if the variable
3575 @code{fortran-break-before-delimiters} is @code{nil}. Otherwise (and by
3576 default), the break comes before the delimiter.
3577
3578 By default, Fortran Auto Fill mode is not enabled. If you want this
3579 feature turned on permanently, add a hook function to
3580 @code{fortran-mode-hook} to execute @code{(fortran-auto-fill-mode 1)}.
3581 @xref{Hooks}.
3582
3583 @node Fortran Columns
3584 @subsection Checking Columns in Fortran
3585
3586 @table @kbd
3587 @item C-c C-r
3588 Display a ``column ruler'' momentarily above the current line
3589 (@code{fortran-column-ruler}).
3590 @item C-c C-w
3591 Split the current window horizontally temporarily so that it is 72
3592 columns wide. This may help you avoid making lines longer than the
3593 72-character limit that some Fortran compilers impose
3594 (@code{fortran-window-create-momentarily}).
3595 @end table
3596
3597 @kindex C-c C-r @r{(Fortran mode)}
3598 @findex fortran-column-ruler
3599 @vindex fortran-column-ruler
3600 The command @kbd{C-c C-r} (@code{fortran-column-ruler}) shows a column
3601 ruler momentarily above the current line. The comment ruler is two lines
3602 of text that show you the locations of columns with special significance in
3603 Fortran programs. Square brackets show the limits of the columns for line
3604 numbers, and curly brackets show the limits of the columns for the
3605 statement body. Column numbers appear above them.
3606
3607 Note that the column numbers count from zero, as always in GNU Emacs.
3608 As a result, the numbers may be one less than those you are familiar
3609 with; but the positions they indicate in the line are standard for
3610 Fortran.
3611
3612 The text used to display the column ruler depends on the value of
3613 the variable @code{indent-tabs-mode}. If @code{indent-tabs-mode} is
3614 @code{nil}, then the value of the variable
3615 @code{fortran-column-ruler-fixed} is used as the column ruler.
3616 Otherwise, the variable @code{fortran-column-ruler-tab} is displayed.
3617 By changing these variables, you can change the column ruler display.
3618
3619 @kindex C-u C-c C-w @r{(Fortran mode)}
3620 @findex fortran-window-create
3621 For even more help, use @kbd{M-x fortran-window-create}), a
3622 command which splits the current window horizontally, making a window 72
3623 columns wide. By editing in this window you can immediately see when you
3624 make a line too wide to be correct Fortran.
3625
3626 @kindex C-c C-w @r{(Fortran mode)}
3627 @findex fortran-window-create-momentarily
3628 Also, @kbd{C-c C-w} (@code{fortran-window-create-momentarily}) can be
3629 used temporarily to split the current window horizontally, making a
3630 window 72 columns wide to check column widths rather than to edit in
3631 this mode. The normal width is restored when you type a space.
3632
3633 @node Fortran Abbrev
3634 @subsection Fortran Keyword Abbrevs
3635
3636 Fortran mode provides many built-in abbrevs for common keywords and
3637 declarations. These are the same sort of abbrev that you can define
3638 yourself. To use them, you must turn on Abbrev mode. @xref{Abbrevs}.
3639
3640 The built-in abbrevs are unusual in one way: they all start with a
3641 semicolon. You cannot normally use semicolon in an abbrev, but Fortran
3642 mode makes this possible by changing the syntax of semicolon to ``word
3643 constituent.''
3644
3645 For example, one built-in Fortran abbrev is @samp{;c} for
3646 @samp{continue}. If you insert @samp{;c} and then insert a punctuation
3647 character such as a space or a newline, the @samp{;c} expands automatically
3648 to @samp{continue}, provided Abbrev mode is enabled.@refill
3649
3650 Type @samp{;?} or @samp{;C-h} to display a list of all the built-in
3651 Fortran abbrevs and what they stand for.
3652
3653 @node Fortran Misc
3654 @subsection Other Fortran Mode Commands
3655
3656 The command @kbd{fortran-strip-sqeuence-nos} can be used to remove text
3657 past Fortran column 72, which is typically old `sequence numbers'.
3658
3659 @node Asm Mode
3660 @section Asm Mode
3661
3662 @cindex Asm mode
3663 @cindex Assembler mode
3664 Asm mode is a major mode for editing files of assembler code. It
3665 defines these commands:
3666
3667 @table @kbd
3668 @item @key{TAB}
3669 @code{tab-to-tab-stop}.
3670 @item C-j
3671 Insert a newline and then indent using @code{tab-to-tab-stop}.
3672 @item :
3673 Insert a colon and then remove the indentation from before the label
3674 preceding colon. Then do @code{tab-to-tab-stop}.
3675 @item ;
3676 Insert or align a comment.
3677 @end table
3678
3679 The variable @code{asm-comment-char} specifies which character
3680 starts comments in assembler syntax.