* mule.texi (Select Input Method): Fix typo.
[bpt/emacs.git] / doc / emacs / programs.texi
CommitLineData
6bf7aab6 1@c This is part of the Emacs manual.
40279251 2@c Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 1999, 2000,
6ed161e1 3@c 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6bf7aab6
DL
4@c See file emacs.texi for copying conditions.
5@node Programs, Building, Text, Top
6@chapter Editing Programs
7@cindex Lisp editing
8@cindex C editing
9@cindex program editing
10
e79c6b89
RS
11 Emacs provides many features to facilitate editing programs. Some
12of these features can
6bf7aab6
DL
13
14@itemize @bullet
15@item
93da5dff 16Find or move over top-level definitions (@pxref{Defuns}).
6bf7aab6 17@item
93da5dff
RS
18Apply the usual indentation conventions of the language
19(@pxref{Program Indent}).
6bf7aab6 20@item
93da5dff 21Balance parentheses (@pxref{Parentheses}).
cf1c48d4 22@item
ea118de1
SE
23Insert, kill or align comments (@pxref{Comments}).
24@item
cf1c48d4 25Highlight program syntax (@pxref{Font Lock}).
6bf7aab6
DL
26@end itemize
27
e79c6b89
RS
28 This chapter describes these features and many more.
29
6bf7aab6
DL
30@menu
31* Program Modes:: Major modes for editing programs.
93da5dff
RS
32* Defuns:: Commands to operate on major top-level parts
33 of a program.
6bf7aab6 34* Program Indent:: Adjusting indentation to show the nesting.
93da5dff 35* Parentheses:: Commands that operate on parentheses.
c8b21b5e 36* Comments:: Inserting, killing, and aligning comments.
93da5dff 37* Documentation:: Getting documentation of functions you plan to call.
51ed0ea0 38* Hideshow:: Displaying blocks selectively.
93da5dff 39* Symbol Completion:: Completion on symbol names of your program or language.
3b8b8888 40* Glasses:: Making identifiersLikeThis more readable.
93da5dff 41* Misc for Programs:: Other Emacs features useful for editing programs.
79214ddf 42* C Modes:: Special commands of C, C++, Objective-C,
6bf7aab6 43 Java, and Pike modes.
51ed0ea0 44* Asm Mode:: Asm mode and its special features.
b23ef7a5
EZ
45@ifnottex
46* Fortran:: Fortran mode and its special features.
47@end ifnottex
6bf7aab6
DL
48@end menu
49
50@node Program Modes
51@section Major Modes for Programming Languages
6bf7aab6 52@cindex modes for programming languages
cf1c48d4
RS
53
54 Emacs has specialized major modes for various programming languages.
55@xref{Major Modes}. A programming language major mode typically
56specifies the syntax of expressions, the customary rules for
57indentation, how to do syntax highlighting for the language, and how
e722aa81
CY
58to find the beginning or end of a function definition. It often
59customizes or provides facilities for compiling and debugging programs
60as well.
cf1c48d4
RS
61
62 Ideally, Emacs should provide a major mode for each programming
63language that you might want to edit; if it doesn't have a mode for
64your favorite language, you can contribute one. But often the mode
65for one language can serve for other syntactically similar languages.
66The major mode for language @var{l} is called @code{@var{l}-mode},
e79c6b89 67and you can select it by typing @kbd{M-x @var{l}-mode @key{RET}}.
cf1c48d4
RS
68@xref{Choosing Modes}.
69
6bf7aab6
DL
70@cindex Perl mode
71@cindex Icon mode
6bf7aab6
DL
72@cindex Makefile mode
73@cindex Tcl mode
74@cindex CPerl mode
138a8f12
DL
75@cindex DSSSL mode
76@cindex Octave mode
77@cindex Metafont mode
78@cindex Modula2 mode
79@cindex Prolog mode
7b703414 80@cindex Python mode
e37d4360 81@cindex Ruby mode
138a8f12
DL
82@cindex Simula mode
83@cindex VHDL mode
84@cindex M4 mode
85@cindex Shell-script mode
3b8b8888
DL
86@cindex Delphi mode
87@cindex PostScript mode
8758a7da
RS
88@cindex Conf mode
89@cindex DNS mode
e722aa81
CY
90 The existing programming language major modes include Lisp, Scheme
91(a variant of Lisp) and the Scheme-based DSSSL expression language,
92Ada, ASM, AWK, C, C++, Delphi (Object Pascal), Fortran, Icon, IDL
93(CORBA), IDLWAVE, Java, Metafont (@TeX{}'s companion for font
94creation), Modula2, Objective-C, Octave, Pascal, Perl, Pike,
e37d4360
CY
95PostScript, Prolog, Python, Ruby, Simula, Tcl, and VHDL. An
96alternative mode for Perl is called CPerl mode. Modes are available
97for the scripting languages of the common GNU and Unix shells, VMS
98DCL, and MS-DOS/MS-Windows @samp{BAT} files. There are also major
99modes for editing makefiles, DNS master files, and various sorts of
e722aa81 100configuration files.
6bf7aab6
DL
101
102@kindex DEL @r{(programming modes)}
4f7666dc 103@findex c-electric-backspace
93da5dff
RS
104 In most programming languages, indentation should vary from line to
105line to illustrate the structure of the program. So the major modes
e79c6b89 106for programming languages arrange for @key{TAB} to update the
e722aa81
CY
107indentation of the current line (@pxref{Program Indent}). They also
108rebind @key{DEL} to treat a tab as if it were the equivalent number of
109spaces; this lets you delete one column of indentation without
110worrying whether the whitespace consists of spaces or tabs. Use
111@kbd{C-b C-d} to delete a tab character before point, in these modes.
6bf7aab6 112
cf1c48d4 113 Separate manuals are available for the modes for Ada (@pxref{Top, , Ada
7ae8ad94 114Mode, ada-mode, Ada Mode}), C/C++/Objective C/Java/Corba IDL/Pike/AWK
cf1c48d4 115(@pxref{Top, , CC Mode, ccmode, CC Mode}) and the IDLWAVE modes
e0fc8fa2 116(@pxref{Top, , IDLWAVE, idlwave, IDLWAVE User Manual}). For Fortran
b23ef7a5
EZ
117mode, see
118@iftex
9dc999d3 119@ref{Fortran,,, emacs-xtra, Specialized Emacs Features}.
b23ef7a5
EZ
120@end iftex
121@ifnottex
122@ref{Fortran}.
123@end ifnottex
124
6bf7aab6
DL
125@cindex mode hook
126@vindex c-mode-hook
127@vindex lisp-mode-hook
128@vindex emacs-lisp-mode-hook
129@vindex lisp-interaction-mode-hook
130@vindex scheme-mode-hook
d2fab838
RS
131 Turning on a major mode runs a normal hook called the @dfn{mode
132hook}, which is the value of a Lisp variable. Each major mode has a
133mode hook, and the hook's name is always made from the mode command's
134name by adding @samp{-hook}. For example, turning on C mode runs the
135hook @code{c-mode-hook}, while turning on Lisp mode runs the hook
136@code{lisp-mode-hook}. The purpose of the mode hook is to give you a
137place to set up customizations for that major mode. @xref{Hooks}.
6bf7aab6 138
93da5dff
RS
139@node Defuns
140@section Top-Level Definitions, or Defuns
6bf7aab6 141
e722aa81
CY
142 In Emacs, a major definition at the top level in the buffer, such as
143a function, is called a @dfn{defun}. The name comes from Lisp, but in
144Emacs we use it for all languages.
6bf7aab6 145
93da5dff
RS
146@menu
147* Left Margin Paren:: An open-paren or similar opening delimiter
148 starts a defun if it is at the left margin.
149* Moving by Defuns:: Commands to move over or mark a major definition.
150* Imenu:: Making buffer indexes as menus.
151* Which Function:: Which Function mode shows which function you are in.
152@end menu
6bf7aab6 153
93da5dff
RS
154@node Left Margin Paren
155@subsection Left Margin Convention
6bf7aab6 156
93da5dff
RS
157@cindex open-parenthesis in leftmost column
158@cindex ( in leftmost column
e722aa81
CY
159 Many programming-language modes assume by default that any opening
160delimiter found at the left margin is the start of a top-level
161definition, or defun. Therefore, @strong{don't put an opening
162delimiter at the left margin unless it should have that significance}.
163For instance, never put an open-parenthesis at the left margin in a
164Lisp file unless it is the start of a top-level list.
165
166 The convention speeds up many Emacs operations, which would
167otherwise have to scan back to the beginning of the buffer to analyze
168the syntax of the code.
93da5dff
RS
169
170 If you don't follow this convention, not only will you have trouble
171when you explicitly use the commands for motion by defuns; other
e722aa81
CY
172features that use them will also give you trouble. This includes the
173indentation commands (@pxref{Program Indent}) and Font Lock mode
174(@pxref{Font Lock}).
93da5dff
RS
175
176 The most likely problem case is when you want an opening delimiter
177at the start of a line inside a string. To avoid trouble, put an
aca2cfd2
AM
178escape character (@samp{\}, in C and Emacs Lisp, @samp{/} in some
179other Lisp dialects) before the opening delimiter. This will not
180affect the contents of the string, but will prevent that opening
181delimiter from starting a defun. Here's an example:
6bf7aab6 182
93da5dff
RS
183@example
184 (insert "Foo:
185\(bar)
186")
187@end example
6bf7aab6 188
5b8fe684
RS
189 To help you catch violations of this convention, Font Lock mode
190highlights confusing opening delimiters (those that ought to be
191quoted) in bold red.
192
e722aa81
CY
193 If you need to override this convention, you can do so by setting
194this user option:
aca2cfd2
AM
195
196@defvar open-paren-in-column-0-is-defun-start
197If this user option is set to @code{t} (the default), opening
198parentheses or braces at column zero always start defuns. When it's
199@code{nil}, defuns are found by searching for parens or braces at the
200outermost level.
201@end defvar
202
e722aa81
CY
203 Usually, you should leave this option at its default value of
204@code{t}. If your buffer contains parentheses or braces in column
205zero which don't start defuns, and it is somehow impractical to remove
206these parentheses or braces, it might be helpful to set the option to
207@code{nil}. Be aware that this might make scrolling and display in
208large buffers quite sluggish. Furthermore, the parentheses and braces
209must be correctly matched throughout the buffer for it to work
210properly.
93da5dff
RS
211
212@node Moving by Defuns
213@subsection Moving by Defuns
6bf7aab6
DL
214@cindex defuns
215
93da5dff
RS
216 These commands move point or set up the region based on top-level
217major definitions, also called @dfn{defuns}.
520c3f4c 218
6bf7aab6
DL
219@table @kbd
220@item C-M-a
221Move to beginning of current or preceding defun
222(@code{beginning-of-defun}).
223@item C-M-e
224Move to end of current or following defun (@code{end-of-defun}).
225@item C-M-h
226Put region around whole current or following defun (@code{mark-defun}).
227@end table
228
f772775c
RS
229@cindex move to beginning or end of function
230@cindex function, move to beginning or end
231@kindex C-M-a
232@kindex C-M-e
233@kindex C-M-h
234@findex beginning-of-defun
235@findex end-of-defun
236@findex mark-defun
237 The commands to move to the beginning and end of the current defun
238are @kbd{C-M-a} (@code{beginning-of-defun}) and @kbd{C-M-e}
239(@code{end-of-defun}). If you repeat one of these commands, or use a
240positive numeric argument, each repetition moves to the next defun in
241the direction of motion.
242
243 @kbd{C-M-a} with a negative argument @minus{}@var{n} moves forward
244@var{n} times to the next beginning of a defun. This is not exactly
245the same place that @kbd{C-M-e} with argument @var{n} would move to;
246the end of this defun is not usually exactly the same place as the
93da5dff
RS
247beginning of the following defun. (Whitespace, comments, and perhaps
248declarations can separate them.) Likewise, @kbd{C-M-e} with a
249negative argument moves back to an end of a defun, which is not quite
250the same as @kbd{C-M-a} with a positive argument.
f772775c 251
4946337d 252@kindex C-M-h @r{(C mode)}
6bf7aab6 253@findex c-mark-function
25716538
CY
254 To operate on the current defun, use @kbd{C-M-h}
255(@code{mark-defun}), which sets the mark at the end of the current
256defun and puts point at its beginning. @xref{Marking Objects}. This
257is the easiest way to get ready to kill the defun in order to move it
258to a different place in the file. If you use the command while point
259is between defuns, it uses the following defun. If you use the
260command while the mark is already active, it sets the mark but does
261not move point; furthermore, each successive use of @kbd{C-M-h}
262extends the end of the region to include one more defun.
93da5dff
RS
263
264 In C mode, @kbd{C-M-h} runs the function @code{c-mark-function},
265which is almost the same as @code{mark-defun}; the difference is that
266it backs up over the argument declarations, function name and returned
e79c6b89
RS
267data type so that the entire C function is inside the region. This is
268an example of how major modes adjust the standard key bindings so that
269they do their standard jobs in a way better fitting a particular
270language. Other major modes may replace any or all of these key
271bindings for that purpose.
6bf7aab6 272
93da5dff
RS
273@node Imenu
274@subsection Imenu
e79c6b89
RS
275@cindex index of buffer definitions
276@cindex buffer definitions index
93da5dff 277
269b7745 278 The Imenu facility offers a way to find the major definitions in
5e6f9132
RS
279a file by name. It is also useful in text formatter major modes,
280where it treats each chapter, section, etc., as a definition.
e79c6b89 281(@xref{Tags}, for a more powerful feature that handles multiple files
5e6f9132 282together.)
93da5dff
RS
283
284@findex imenu
5e6f9132 285 If you type @kbd{M-x imenu}, it reads the name of a definition using
e79c6b89
RS
286the minibuffer, then moves point to that definition. You can use
287completion to specify the name; the command always displays the whole
288list of valid names.
d2fab838 289
5e6f9132 290@findex imenu-add-menubar-index
d2fab838 291 Alternatively, you can bind the command @code{imenu} to a mouse
e79c6b89
RS
292click. Then it displays mouse menus for you to select a definition
293name. You can also add the buffer's index to the menu bar by calling
294@code{imenu-add-menubar-index}. If you want to have this menu bar
295item available for all buffers in a certain major mode, you can do
296this by adding @code{imenu-add-menubar-index} to its mode hook. But
dfec8297
RS
297if you have done that, you will have to wait a little while each time
298you visit a file in that mode, while Emacs finds all the definitions
299in that buffer.
93da5dff
RS
300
301@vindex imenu-auto-rescan
302 When you change the contents of a buffer, if you add or delete
e79c6b89 303definitions, you can update the buffer's index based on the
d2fab838 304new contents by invoking the @samp{*Rescan*} item in the menu.
dcace646
EZ
305Rescanning happens automatically if you set @code{imenu-auto-rescan} to
306a non-@code{nil} value. There is no need to rescan because of small
e79c6b89 307changes in the text.
93da5dff
RS
308
309@vindex imenu-sort-function
d2fab838 310 You can customize the way the menus are sorted by setting the
e79c6b89 311variable @code{imenu-sort-function}. By default, names are ordered as
5e6f9132
RS
312they occur in the buffer; if you want alphabetic sorting, use the
313symbol @code{imenu--sort-by-name} as the value. You can also
314define your own comparison function by writing Lisp code.
93da5dff
RS
315
316 Imenu provides the information to guide Which Function mode
317@ifnottex
318(@pxref{Which Function}).
319@end ifnottex
320@iftex
321(see below).
322@end iftex
323The Speedbar can also use it (@pxref{Speedbar}).
324
325@node Which Function
326@subsection Which Function Mode
af056954 327@cindex current function name in mode line
93da5dff
RS
328
329 Which Function mode is a minor mode that displays the current
330function name in the mode line, updating it as you move around in a
331buffer.
332
333@findex which-function-mode
334@vindex which-func-modes
df7593dd 335 To either enable or disable Which Function mode, use the command
e722aa81
CY
336@kbd{M-x which-function-mode}. This command applies to all buffers,
337both existing ones and those yet to be created. However, it takes
338effect only in certain major modes, those listed in the value of
339@code{which-func-modes}. If the value of @code{which-func-modes} is
340@code{t} rather than a list of modes, then Which Function mode applies
341to all major modes that know how to support it---in other words, all
342the major modes that support Imenu.
6bf7aab6
DL
343
344@node Program Indent
345@section Indentation for Programs
346@cindex indentation for programs
347
348 The best way to keep a program properly indented is to use Emacs to
e722aa81
CY
349reindent it as you change it. Emacs has commands to indent either a
350single line, a specified number of lines, or all of the lines inside a
351single parenthetical grouping.
6bf7aab6
DL
352
353@menu
354* Basic Indent:: Indenting a single line.
355* Multi-line Indent:: Commands to reindent many lines at once.
356* Lisp Indent:: Specifying how each Lisp function should be indented.
357* C Indent:: Extra features for indenting C and related modes.
358* Custom C Indent:: Controlling indentation style for C and related modes.
359@end menu
360
d2fab838 361@cindex pretty-printer
6bf7aab6
DL
362 Emacs also provides a Lisp pretty-printer in the library @code{pp}.
363This program reformats a Lisp object with indentation chosen to look nice.
364
365@node Basic Indent
366@subsection Basic Program Indentation Commands
367
d2fab838
RS
368 The basic indentation commands indent a single line according to the
369usual conventions of the language you are editing.
cf1c48d4 370
6bf7aab6
DL
371@table @kbd
372@item @key{TAB}
373Adjust indentation of current line.
374@item C-j
bb63d706
RS
375Insert a newline, then adjust indentation of following line
376(@code{newline-and-indent}).
6bf7aab6
DL
377@end table
378
379@kindex TAB @r{(programming modes)}
4f7666dc
RS
380@findex c-indent-command
381@findex indent-line-function
f772775c 382@findex indent-for-tab-command
e722aa81
CY
383 The basic indentation command is @key{TAB}. In any
384programming-language major mode, @key{TAB} gives the current line the
385correct indentation as determined from the previous lines. It does
386this by inserting or deleting whitespace at the beginning of the
387current line. If point was inside the whitespace at the beginning of
388the line, @key{TAB} puts it at the end of that whitespace; otherwise,
389@key{TAB} keeps point fixed with respect to the characters around it.
390If the region is active (@pxref{Mark}), @key{TAB} indents every line
391within the region instead of just the current line. The function that
392@key{TAB} runs depends on the major mode; for instance, it is
393@code{c-indent-line-or-region} in C mode. Each function is aware of
394the syntax and conventions for its particular language.
6bf7aab6 395
64e207c0 396 Use @kbd{C-q @key{TAB}} to insert a tab character at point.
6bf7aab6
DL
397
398@kindex C-j
399@findex newline-and-indent
cf1c48d4 400 When entering lines of new code, use @kbd{C-j}
bb63d706 401(@code{newline-and-indent}), which inserts a newline and then adjusts
14f4194d 402indentation after it. (It also deletes any trailing whitespace which
e722aa81
CY
403remains before the new newline.) For instance, @kbd{C-j} at the end
404of a line creates a blank line with appropriate indentation. In
405programming language modes, it is equivalent to @key{RET} @key{TAB}.
406
407 When Emacs indents a line that starts within a parenthetical
408grouping, it usually places the start of the line under the preceding
409line within the group, or under the text after the parenthesis. If
410you manually give one of these lines a nonstandard indentation, the
411lines below will tend to follow it. This behavior is convenient in
412cases where you have overridden the standard result of @key{TAB}
413indentation (e.g., for aesthetic purposes).
414
415 Many programming-language modes assume that an open-parenthesis,
416open-brace or other opening delimiter at the left margin is the start
417of a function. This assumption speeds up indentation commands. If
418the text you are editing contains opening delimiters in column zero
419that aren't the beginning of a functions---even if these delimiters
420occur inside strings or comments---then you must set
108262a0 421@code{open-paren-in-column-0-is-defun-start}. @xref{Left Margin
e722aa81 422Paren}.
6bf7aab6 423
e722aa81
CY
424 Normally, Emacs indents lines using an ``optimal'' mix of tab and
425space characters. If you want Emacs to use spaces only, set
426@code{indent-tabs-mode} (@pxref{Just Spaces}).
5151db0c 427
6bf7aab6
DL
428@node Multi-line Indent
429@subsection Indenting Several Lines
430
e722aa81
CY
431 Sometimes, you may want to reindent several lines of code at a time.
432One way to do this is to use the mark; when the mark is active and the
433region is non-empty, @key{TAB} indents every line within the region.
434In addition, Emacs provides several other commands for indenting large
435chunks of code:
6bf7aab6
DL
436
437@table @kbd
438@item C-M-q
e722aa81 439Reindent all the lines within one parenthetical grouping.
e79c6b89
RS
440@item C-M-\
441Reindent all lines in the region (@code{indent-region}).
6bf7aab6 442@item C-u @key{TAB}
93da5dff
RS
443Shift an entire parenthetical grouping rigidly sideways so that its
444first line is properly indented.
5cc06e0b
EZ
445@item M-x indent-code-rigidly
446Shift all the lines in the region rigidly sideways, but do not alter
447lines that start inside comments and strings.
6bf7aab6
DL
448@end table
449
450@kindex C-M-q
6daf3e15 451@findex indent-pp-sexp
e722aa81
CY
452 To reindent the contents of a single parenthetical grouping,
453position point before the beginning of the grouping and type
454@kbd{C-M-q}. This changes the relative indentation within the
455grouping, without affecting its overall indentation (i.e., the
456indentation of the line where the grouping starts). The function that
457@kbd{C-M-q} runs depends on the major mode; it is
458@code{indent-pp-sexp} in Lisp mode, @code{c-indent-exp} in C mode,
459etc. To correct the overall indentation as well, type @key{TAB}
460first.
461
462 @kbd{C-M-\} (@code{indent-region}) applies @key{TAB} to the region.
463This is useful when Transient Mark mode is disabled (@pxref{Persistent
464Mark}), because in that case @key{TAB} does not act on the region.
e79c6b89 465
6bf7aab6 466@kindex C-u TAB
e722aa81
CY
467 If you like the relative indentation within a grouping but not the
468indentation of its first line, move point to that first line and type
469@kbd{C-u @key{TAB}}. In Lisp, C, and some other major modes,
470@key{TAB} with a numeric argument reindents the current line as usual,
471then reindents by the same amount all the lines in the parenthetical
472grouping starting on the current line. It is clever, though, and does
473not alter lines that start inside strings. Neither does it alter C
474preprocessor lines when in C mode, but it does reindent any
475continuation lines that may be attached to them.
6bf7aab6 476
5cc06e0b 477@findex indent-code-rigidly
e722aa81
CY
478 The command @kbd{M-x indent-code-rigidly} rigidly shifts all the
479lines in the region sideways, like @code{indent-rigidly} does
480(@pxref{Indentation Commands}). It doesn't alter the indentation of
481lines that start inside a string, unless the region also starts inside
482that string. The prefix arg specifies the number of columns to
483indent.
6bf7aab6
DL
484
485@node Lisp Indent
486@subsection Customizing Lisp Indentation
487@cindex customizing Lisp indentation
488
489 The indentation pattern for a Lisp expression can depend on the function
490called by the expression. For each Lisp function, you can choose among
491several predefined patterns of indentation, or define an arbitrary one with
492a Lisp program.
493
494 The standard pattern of indentation is as follows: the second line of the
495expression is indented under the first argument, if that is on the same
496line as the beginning of the expression; otherwise, the second line is
497indented underneath the function name. Each following line is indented
498under the previous line whose nesting depth is the same.
499
500@vindex lisp-indent-offset
501 If the variable @code{lisp-indent-offset} is non-@code{nil}, it overrides
502the usual indentation pattern for the second line of an expression, so that
503such lines are always indented @code{lisp-indent-offset} more columns than
504the containing list.
505
506@vindex lisp-body-indent
d2fab838 507 Certain functions override the standard pattern. Functions whose
269b7745 508names start with @code{def} treat the second lines as the start of
d2fab838
RS
509a @dfn{body}, by indenting the second line @code{lisp-body-indent}
510additional columns beyond the open-parenthesis that starts the
511expression.
6bf7aab6 512
b771b258 513@cindex @code{lisp-indent-function} property
d2fab838 514 You can override the standard pattern in various ways for individual
690a6d08
RS
515functions, according to the @code{lisp-indent-function} property of
516the function name. Normally you would use this for macro definitions
517and specify it using the @code{declare} construct (@pxref{Defining
518Macros,,, elisp, the Emacs Lisp Reference Manual}).
6bf7aab6
DL
519
520@node C Indent
521@subsection Commands for C Indentation
522
93da5dff 523 Here are special features for indentation in C mode and related modes:
6bf7aab6
DL
524
525@table @code
526@item C-c C-q
527@kindex C-c C-q @r{(C mode)}
528@findex c-indent-defun
529Reindent the current top-level function definition or aggregate type
530declaration (@code{c-indent-defun}).
531
532@item C-M-q
533@kindex C-M-q @r{(C mode)}
534@findex c-indent-exp
535Reindent each line in the balanced expression that follows point
7ae8ad94
RS
536(@code{c-indent-exp}). A prefix argument inhibits warning messages
537about invalid syntax.
6bf7aab6
DL
538
539@item @key{TAB}
540@findex c-indent-command
541Reindent the current line, and/or in some cases insert a tab character
542(@code{c-indent-command}).
543
7ae8ad94 544@vindex c-tab-always-indent
6bf7aab6
DL
545If @code{c-tab-always-indent} is @code{t}, this command always reindents
546the current line and does nothing else. This is the default.
547
548If that variable is @code{nil}, this command reindents the current line
549only if point is at the left margin or in the line's indentation;
550otherwise, it inserts a tab (or the equivalent number of spaces,
551if @code{indent-tabs-mode} is @code{nil}).
552
553Any other value (not @code{nil} or @code{t}) means always reindent the
7ae8ad94 554line, and also insert a tab if within a comment or a string.
6bf7aab6
DL
555@end table
556
557 To reindent the whole current buffer, type @kbd{C-x h C-M-\}. This
558first selects the whole buffer as the region, then reindents that
559region.
560
561 To reindent the current block, use @kbd{C-M-u C-M-q}. This moves
562to the front of the block and then reindents it all.
563
564@node Custom C Indent
565@subsection Customizing C Indentation
93da5dff 566@cindex style (for indentation)
6bf7aab6 567
7ae8ad94
RS
568 C mode and related modes use a flexible mechanism for customizing
569indentation. C mode indents a source line in two steps: first it
570classifies the line syntactically according to its contents and
571context; second, it determines the indentation offset associated by
572your selected @dfn{style} with the syntactic construct and adds this
573onto the indentation of the @dfn{anchor statement}.
6bf7aab6 574
93da5dff 575@table @kbd
7ae8ad94
RS
576@item C-c . @key{RET} @var{style} @key{RET}
577Select a predefined style @var{style} (@code{c-set-style}).
93da5dff 578@end table
6bf7aab6 579
108262a0
AM
580 A @dfn{style} is a named collection of customizations that can be
581used in C mode and the related modes. @ref{Styles,,, ccmode, The CC
582Mode Manual}, for a complete description. Emacs comes with several
93da5dff
RS
583predefined styles, including @code{gnu}, @code{k&r}, @code{bsd},
584@code{stroustrup}, @code{linux}, @code{python}, @code{java},
108262a0
AM
585@code{whitesmith}, @code{ellemtel}, and @code{awk}. Some of these
586styles are primarily intended for one language, but any of them can be
587used with any of the languages supported by these modes. To find out
588what a style looks like, select it and reindent some code, e.g., by
589typing @key{C-M-q} at the start of a function definition.
6bf7aab6 590
7ae8ad94 591@kindex C-c . @r{(C mode)}
93da5dff 592@findex c-set-style
dfec8297
RS
593 To choose a style for the current buffer, use the command @w{@kbd{C-c
594.}}. Specify a style name as an argument (case is not significant).
7ae8ad94
RS
595This command affects the current buffer only, and it affects only
596future invocations of the indentation commands; it does not reindent
108262a0
AM
597the code already in the buffer. To reindent the whole buffer in the
598new style, you can type @kbd{C-x h C-M-\}.
6bf7aab6 599
93da5dff
RS
600@vindex c-default-style
601 You can also set the variable @code{c-default-style} to specify the
7ae8ad94
RS
602default style for various major modes. Its value should be either the
603style's name (a string) or an alist, in which each element specifies
604one major mode and which indentation style to use for it. For
605example,
6bf7aab6
DL
606
607@example
93da5dff 608(setq c-default-style
108262a0 609 '((java-mode . "java") (awk-mode . "awk") (other . "gnu")))
6bf7aab6
DL
610@end example
611
93da5dff 612@noindent
108262a0
AM
613specifies explicit choices for Java and AWK modes, and the default
614@samp{gnu} style for the other C-like modes. (These settings are
615actually the defaults.) This variable takes effect when you select
616one of the C-like major modes; thus, if you specify a new default
617style for Java mode, you can make it take effect in an existing Java
618mode buffer by typing @kbd{M-x java-mode} there.
6bf7aab6 619
93da5dff
RS
620 The @code{gnu} style specifies the formatting recommended by the GNU
621Project for C; it is the default, so as to encourage use of our
622recommended style.
6bf7aab6 623
108262a0
AM
624 @xref{Indentation Engine Basics,,, ccmode, the CC Mode Manual}, and
625@ref{Customizing Indentation,,, ccmode, the CC Mode Manual}, for more
626information on customizing indentation for C and related modes,
93da5dff
RS
627including how to override parts of an existing style and how to define
628your own styles.
6bf7aab6 629
93da5dff
RS
630@node Parentheses
631@section Commands for Editing with Parentheses
6bf7aab6 632
93da5dff
RS
633@findex check-parens
634@cindex unbalanced parentheses and quotes
635 This section describes the commands and features that take advantage
636of the parenthesis structure in a program, or help you keep it
637balanced.
6bf7aab6 638
93da5dff
RS
639 When talking about these facilities, the term ``parenthesis'' also
640includes braces, brackets, or whatever delimiters are defined to match
e79c6b89
RS
641in pairs. The major mode controls which delimiters are significant,
642through the syntax table (@pxref{Syntax}). In Lisp, only parentheses
643count; in C, these commands apply to braces and brackets too.
6bf7aab6 644
93da5dff
RS
645 You can use @kbd{M-x check-parens} to find any unbalanced
646parentheses and unbalanced string quotes in the buffer.
6bf7aab6 647
93da5dff
RS
648@menu
649* Expressions:: Expressions with balanced parentheses.
650* Moving by Parens:: Commands for moving up, down and across
651 in the structure of parentheses.
652* Matching:: Insertion of a close-delimiter flashes matching open.
653@end menu
6bf7aab6 654
93da5dff
RS
655@node Expressions
656@subsection Expressions with Balanced Parentheses
6bf7aab6 657
93da5dff
RS
658@cindex sexp
659@cindex expression
660@cindex balanced expression
661 These commands deal with balanced expressions, also called
662@dfn{sexps}@footnote{The word ``sexp'' is used to refer to an
663expression in Lisp.}.
6bf7aab6 664
93da5dff
RS
665@table @kbd
666@item C-M-f
667Move forward over a balanced expression (@code{forward-sexp}).
668@item C-M-b
ea118de1 669Move backward over a balanced expression (@code{backward-sexp}).
93da5dff
RS
670@item C-M-k
671Kill balanced expression forward (@code{kill-sexp}).
93da5dff
RS
672@item C-M-t
673Transpose expressions (@code{transpose-sexps}).
674@item C-M-@@
649d1cbe 675@itemx C-M-@key{SPC}
93da5dff
RS
676Put mark after following expression (@code{mark-sexp}).
677@end table
6bf7aab6 678
93da5dff
RS
679 Each programming language major mode customizes the definition of
680balanced expressions to suit that language. Balanced expressions
681typically include symbols, numbers, and string constants, as well as
e79c6b89 682any pair of matching delimiters and their contents. Some languages
93da5dff
RS
683have obscure forms of expression syntax that nobody has bothered to
684implement in Emacs.
6bf7aab6 685
93da5dff 686@cindex Control-Meta
e79c6b89
RS
687 By convention, the keys for these commands are all Control-Meta
688characters. They usually act on expressions just as the corresponding
689Meta characters act on words. For instance, the command @kbd{C-M-b}
690moves backward over a balanced expression, just as @kbd{M-b} moves
691back over a word.
6bf7aab6 692
93da5dff
RS
693@kindex C-M-f
694@kindex C-M-b
695@findex forward-sexp
696@findex backward-sexp
697 To move forward over a balanced expression, use @kbd{C-M-f}
698(@code{forward-sexp}). If the first significant character after point
699is an opening delimiter (@samp{(} in Lisp; @samp{(}, @samp{[} or
700@samp{@{} in C), @kbd{C-M-f} moves past the matching closing
701delimiter. If the character begins a symbol, string, or number,
702@kbd{C-M-f} moves over that.
6bf7aab6 703
93da5dff
RS
704 The command @kbd{C-M-b} (@code{backward-sexp}) moves backward over a
705balanced expression. The detailed rules are like those above for
706@kbd{C-M-f}, but with directions reversed. If there are prefix
707characters (single-quote, backquote and comma, in Lisp) preceding the
708expression, @kbd{C-M-b} moves back over them as well. The balanced
709expression commands move across comments as if they were whitespace,
710in most modes.
6bf7aab6 711
93da5dff
RS
712 @kbd{C-M-f} or @kbd{C-M-b} with an argument repeats that operation the
713specified number of times; with a negative argument, it moves in the
714opposite direction.
6bf7aab6 715
93da5dff
RS
716@cindex killing expressions
717@kindex C-M-k
718@findex kill-sexp
93da5dff 719 Killing a whole balanced expression can be done with @kbd{C-M-k}
880b0421
RS
720(@code{kill-sexp}). @kbd{C-M-k} kills the characters that @kbd{C-M-f}
721would move over.
6bf7aab6 722
93da5dff
RS
723@cindex transposition of expressions
724@kindex C-M-t
725@findex transpose-sexps
726 A somewhat random-sounding command which is nevertheless handy is
727@kbd{C-M-t} (@code{transpose-sexps}), which drags the previous
728balanced expression across the next one. An argument serves as a
108262a0
AM
729repeat count, moving the previous expression over that many following
730ones. A negative argument drags the previous balanced expression
731backwards across those before it (thus canceling out the effect of
732@kbd{C-M-t} with a positive argument). An argument of zero, rather
733than doing nothing, transposes the balanced expressions ending at or
734after point and the mark.
6bf7aab6 735
93da5dff 736@kindex C-M-@@
649d1cbe 737@kindex C-M-@key{SPC}
93da5dff
RS
738@findex mark-sexp
739 To set the region around the next balanced expression in the buffer,
25716538
CY
740use @kbd{C-M-@key{SPC}} (@code{mark-sexp}), which sets mark at the
741same place that @kbd{C-M-f} would move to. @kbd{C-M-@key{SPC}} treats
742numeric arguments in the same way as @kbd{C-M-f}; in particular, a
743negative argument puts the mark at the beginning of the previous
744balanced expression. The alias @kbd{C-M-@@} is equivalent to
745@kbd{C-M-@key{SPC}}. While the mark is active, each successive use of
746@kbd{C-M-@key{SPC}} extends the region by shifting the mark by one
747sexp.
93da5dff
RS
748
749 In languages that use infix operators, such as C, it is not possible
750to recognize all balanced expressions as such because there can be
751multiple possibilities at a given position. For example, C mode does
752not treat @samp{foo + bar} as a single expression, even though it
753@emph{is} one C expression; instead, it recognizes @samp{foo} as one
754expression and @samp{bar} as another, with the @samp{+} as punctuation
755between them. Both @samp{foo + bar} and @samp{foo} are legitimate
756choices for ``the expression following point'' when point is at the
e79c6b89
RS
757@samp{f}, so the expression commands must perforce choose one or the
758other to operate on. Note that @samp{(foo + bar)} is recognized as a
759single expression in C mode, because of the parentheses.
93da5dff
RS
760
761@node Moving by Parens
762@subsection Moving in the Parenthesis Structure
763
764@cindex parenthetical groupings
765@cindex parentheses, moving across
766@cindex matching parenthesis and braces, moving to
767@cindex braces, moving across
768@cindex list commands
3fbb05ff 769
93da5dff
RS
770 The Emacs commands for handling parenthetical groupings see nothing
771except parentheses (or whatever characters must balance in the
3fbb05ff
AM
772language you are working with). They ignore strings and comments
773(including any parentheses within them) and ignore parentheses quoted
774by an escape character. They are mainly intended for editing
93da5dff
RS
775programs, but can be useful for editing any text that has parentheses.
776They are sometimes called ``list'' commands because in Lisp these
777groupings are lists.
6bf7aab6 778
3fbb05ff
AM
779These commands assume that the starting point is not inside a string
780or a comment. Sometimes you can invoke them usefully from one of
781these places (for example, when you have a parenthesised clause in a
782comment) but this is unreliable.
783
6bf7aab6 784@table @kbd
93da5dff
RS
785@item C-M-n
786Move forward over a parenthetical group (@code{forward-list}).
787@item C-M-p
ea118de1 788Move backward over a parenthetical group (@code{backward-list}).
93da5dff
RS
789@item C-M-u
790Move up in parenthesis structure (@code{backward-up-list}).
791@item C-M-d
792Move down in parenthesis structure (@code{down-list}).
6bf7aab6
DL
793@end table
794
93da5dff
RS
795@kindex C-M-n
796@kindex C-M-p
797@findex forward-list
798@findex backward-list
799 The ``list'' commands @kbd{C-M-n} (@code{forward-list}) and
3fbb05ff
AM
800@kbd{C-M-p} (@code{backward-list}) move forward or backward over one
801(or @var{n}) parenthetical groupings.
6bf7aab6 802
93da5dff 803@kindex C-M-u
93da5dff 804@findex backward-up-list
93da5dff
RS
805 @kbd{C-M-n} and @kbd{C-M-p} try to stay at the same level in the
806parenthesis structure. To move @emph{up} one (or @var{n}) levels, use
807@kbd{C-M-u} (@code{backward-up-list}). @kbd{C-M-u} moves backward up
808past one unmatched opening delimiter. A positive argument serves as a
809repeat count; a negative argument reverses the direction of motion, so
d2fab838 810that the command moves forward and up one or more levels.
93da5dff 811
dfec8297
RS
812@kindex C-M-d
813@findex down-list
93da5dff
RS
814 To move @emph{down} in the parenthesis structure, use @kbd{C-M-d}
815(@code{down-list}). In Lisp mode, where @samp{(} is the only opening
816delimiter, this is nearly the same as searching for a @samp{(}. An
817argument specifies the number of levels to go down.
6bf7aab6
DL
818
819@node Matching
93da5dff 820@subsection Automatic Display Of Matching Parentheses
6bf7aab6
DL
821@cindex matching parentheses
822@cindex parentheses, displaying matches
823
824 The Emacs parenthesis-matching feature is designed to show
93da5dff
RS
825automatically how parentheses (and other matching delimiters) match in
826the text. Whenever you type a self-inserting character that is a
827closing delimiter, the cursor moves momentarily to the location of the
828matching opening delimiter, provided that is on the screen. If it is
e79c6b89
RS
829not on the screen, Emacs displays some of the text near it in the echo
830area. Either way, you can tell which grouping you are closing off.
93da5dff
RS
831
832 If the opening delimiter and closing delimiter are mismatched---such
833as in @samp{[x)}---a warning message is displayed in the echo area.
6bf7aab6
DL
834
835@vindex blink-matching-paren
836@vindex blink-matching-paren-distance
837@vindex blink-matching-delay
054af0fd
SE
838 Three variables control parenthesis match display:
839
840 @code{blink-matching-paren} turns the feature on or off: @code{nil}
93da5dff 841disables it, but the default is @code{t} to enable match display.
f772775c
RS
842
843 @code{blink-matching-delay} says how many seconds to leave the
93da5dff 844cursor on the matching opening delimiter, before bringing it back to
f772775c
RS
845the real location of point; the default is 1, but on some systems it
846is useful to specify a fraction of a second.
847
848 @code{blink-matching-paren-distance} specifies how many characters
849back to search to find the matching opening delimiter. If the match
8b6f4c0a 850is not found in that distance, scanning stops, and nothing is displayed.
93da5dff 851This is to prevent the scan for the matching delimiter from wasting
f772775c 852lots of time when there is no match. The default is 25600.
6bf7aab6
DL
853
854@cindex Show Paren mode
79f9f655 855@cindex highlighting matching parentheses
6bf7aab6 856@findex show-paren-mode
93da5dff 857 Show Paren mode provides a more powerful kind of automatic matching.
e722aa81
CY
858Whenever point is before an opening delimiter or after a closing
859delimiter, both that delimiter and its opposite delimiter are
860highlighted. Use the command @kbd{M-x show-paren-mode} to enable or
861disable this mode.
79f9f655 862
dfec8297
RS
863 Show Paren mode uses the faces @code{show-paren-match} and
864@code{show-paren-mismatch} to highlight parentheses; you can customize
865them to control how highlighting looks. @xref{Face Customization}.
6bf7aab6
DL
866
867@node Comments
868@section Manipulating Comments
869@cindex comments
870
871 Because comments are such an important part of programming, Emacs
8f50b630
RS
872provides special commands for editing and inserting comments. It can
873also do spell checking on comments with Flyspell Prog mode
874(@pxref{Spelling}).
6bf7aab6
DL
875
876@menu
5b31640c 877* Comment Commands:: Inserting, killing, and aligning comments.
93da5dff
RS
878* Multi-Line Comments:: Commands for adding and editing multi-line comments.
879* Options for Comments::Customizing the comment features.
6bf7aab6
DL
880@end menu
881
882@node Comment Commands
883@subsection Comment Commands
6bf7aab6 884@cindex indentation for comments
5b31640c 885@cindex alignment for comments
6bf7aab6 886
e722aa81 887 The commands in this table insert, kill and align comments:
6bf7aab6 888
7ae8ad94
RS
889@table @asis
890@item @kbd{M-;}
9234c238
RS
891Insert or realign comment on current line; alternatively, comment or
892uncomment the region (@code{comment-dwim}).
7ae8ad94 893@item @kbd{C-u M-;}
9234c238 894Kill comment on current line (@code{comment-kill}).
7ae8ad94 895@item @kbd{C-x ;}
47c1b5f4 896Set comment column (@code{comment-set-column}).
7ae8ad94
RS
897@item @kbd{C-M-j}
898@itemx @kbd{M-j}
6bf7aab6 899Like @key{RET} followed by inserting and aligning a comment
108262a0 900(@code{comment-indent-new-line}). @xref{Multi-Line Comments}.
7ae8ad94
RS
901@item @kbd{M-x comment-region}
902@itemx @kbd{C-c C-c} (in C-like modes)
6bf7aab6
DL
903Add or remove comment delimiters on all the lines in the region.
904@end table
905
9234c238
RS
906@kindex M-;
907@findex comment-dwim
908 The command to create or align a comment is @kbd{M-;}
909(@code{comment-dwim}). The word ``dwim'' is an acronym for ``Do What
910I Mean''; it indicates that this command can be used for many
911different jobs relating to comments, depending on the situation where
912you use it.
913
8474de5b
CY
914 When a region is active, @kbd{M-;} either adds or removes comment
915delimiters on each line of the region. @xref{Mark}. If every line in
916the region is a comment, it removes comment delimiters from each;
917otherwise, it adds comment delimiters to each. You can also use the
918commands @code{comment-region} and @code{uncomment-region} to
919explicitly comment or uncomment the text in the region
920(@pxref{Multi-Line Comments}). If you supply a prefix argument to
921@kbd{M-;} when a region is active, that specifies how many comment
922delimiters to add or how many to delete.
923
924 If the region is not active, @kbd{M-;} inserts a new comment if
925there is no comment already on the line. The new comment is normally
926aligned at a specific column called the @dfn{comment column}; if the
927text of the line extends past the comment column, @kbd{M-;} aligns the
928comment start string to a suitable boundary (usually, at least one
929space is inserted). The comment begins with the string Emacs thinks
930comments should start with (the value of @code{comment-start}; see
931below). Emacs places point after that string, so you can insert the
932text of the comment right away. If the major mode has specified a
933string to terminate comments, @kbd{M-;} inserts that string after
934point, to keep the syntax valid.
9234c238
RS
935
936 You can also use @kbd{M-;} to align an existing comment. If a line
5b31640c 937already contains the comment-start string, @kbd{M-;} realigns it to
9234c238
RS
938the conventional alignment and moves point after it. (Exception:
939comments starting in column 0 are not moved.) Even when an existing
940comment is properly aligned, @kbd{M-;} is still useful for moving
941directly to the start of the text inside the comment.
942
943@findex comment-kill
944@kindex C-u M-;
945 @kbd{C-u M-;} kills any comment on the current line, along with the
946whitespace before it. To reinsert the comment on another line, move
947to the end of that line, do @kbd{C-y}, and then do @kbd{M-;} to
948realign it.
949
950 Note that @kbd{C-u M-;} is not a distinct key; it is @kbd{M-;}
951(@code{comment-dwim}) with a prefix argument. That command is
952programmed so that when it receives a prefix argument it calls
953@code{comment-kill}. However, @code{comment-kill} is a valid command
954in its own right, and you can bind it directly to a key if you wish.
955
5b31640c 956 Some major modes have special rules for aligning certain kinds of
6bf7aab6
DL
957comments in certain contexts. For example, in Lisp code, comments which
958start with two semicolons are indented as if they were lines of code,
959instead of at the comment column. Comments which start with three
24a768a0
SM
960semicolons are supposed to start at the left margin and are often used
961for sectioning purposes. Emacs understands
6bf7aab6
DL
962these conventions by indenting a double-semicolon comment using @key{TAB},
963and by not changing the indentation of a triple-semicolon comment at all.
964
965@example
91ec56a9 966;; This function is just an example.
24a768a0 967;;; Here either two or three semicolons are appropriate.
6bf7aab6 968(defun foo (x)
91ec56a9 969;;; And now, the first part of the function:
6bf7aab6
DL
970 ;; The following line adds one.
971 (1+ x)) ; This line adds one.
972@end example
973
e722aa81
CY
974 For C-like modes, you can configure the exact effect of @kbd{M-;} by
975setting the variables @code{c-indent-comment-alist} and
108262a0
AM
976@code{c-indent-comments-syntactically-p}. For example, on a line
977ending in a closing brace, @kbd{M-;} puts the comment one space after
978the brace rather than at @code{comment-column}. For full details see
e722aa81 979@ref{Comment Commands,,, ccmode, The CC Mode Manual}.
6bf7aab6 980
6bf7aab6
DL
981@node Multi-Line Comments
982@subsection Multiple Lines of Comments
983
984@kindex C-M-j
7ae8ad94 985@kindex M-j
6bf7aab6 986@cindex blank lines in programs
47c1b5f4 987@findex comment-indent-new-line
108262a0 988
6bf7aab6 989 If you are typing a comment and wish to continue it on another line,
7ae8ad94 990you can use the command @kbd{C-M-j} or @kbd{M-j}
f5eb910a
RS
991(@code{comment-indent-new-line}). If @code{comment-multi-line}
992(@pxref{Options for Comments}) is non-@code{nil}, it moves to a new
993line within the comment. Otherwise it closes the comment and starts a
994new comment on a new line. When Auto Fill mode is on, going past the
995fill column while typing a comment causes the comment to be continued
996in just this fashion.
7ae8ad94
RS
997
998@kindex C-c C-c (C mode)
6bf7aab6
DL
999@findex comment-region
1000 To turn existing lines into comment lines, use the @kbd{M-x
dfec8297 1001comment-region} command (or type @kbd{C-c C-c} in C-like modes). It
108262a0
AM
1002adds comment delimiters to the lines that start in the region, thus
1003commenting them out. With a negative argument, it does the
1004opposite---it deletes comment delimiters from the lines in the region.
6bf7aab6
DL
1005
1006 With a positive argument, @code{comment-region} duplicates the last
5b31640c
RS
1007character of the comment start sequence it adds; the argument
1008specifies how many copies of the character to insert. Thus, in Lisp
1009mode, @kbd{C-u 2 M-x comment-region} adds @samp{;;} to each line.
1010Duplicating the comment delimiter is a way of calling attention to the
1011comment. It can also affect how the comment is aligned or indented.
1012In Lisp, for proper indentation, you should use an argument of two or
1013three, if between defuns; if within a defun, it must be three.
6bf7aab6 1014
108262a0
AM
1015 You can configure C Mode such that when you type a @samp{/} at the
1016start of a line in a multi-line block comment, this closes the
1017comment. Enable the @code{comment-close-slash} clean-up for this.
1018@xref{Clean-ups,,, ccmode, The CC Mode Manual}.
1019
6bf7aab6
DL
1020@node Options for Comments
1021@subsection Options Controlling Comments
1022
1023@vindex comment-column
1024@kindex C-x ;
47c1b5f4 1025@findex comment-set-column
7ae8ad94
RS
1026 The @dfn{comment column}, the column at which Emacs tries to place
1027comments, is stored in the variable @code{comment-column}. You can
1028set it to a number explicitly. Alternatively, the command @kbd{C-x ;}
1029(@code{comment-set-column}) sets the comment column to the column
1030point is at. @kbd{C-u C-x ;} sets the comment column to match the
1031last comment before point in the buffer, and then does a @kbd{M-;} to
1032align the current line's comment under the previous one.
6bf7aab6
DL
1033
1034 The variable @code{comment-column} is per-buffer: setting the variable
1035in the normal fashion affects only the current buffer, but there is a
1036default value which you can change with @code{setq-default}.
1037@xref{Locals}. Many major modes initialize this variable for the
1038current buffer.
1039
1040@vindex comment-start-skip
1041 The comment commands recognize comments based on the regular
1042expression that is the value of the variable @code{comment-start-skip}.
1043Make sure this regexp does not match the null string. It may match more
1044than the comment starting delimiter in the strictest sense of the word;
47c1b5f4
RS
1045for example, in C mode the value of the variable is
1046@c This stops M-q from breaking the line inside that @code.
7ae8ad94 1047@code{@w{"/\\*+ *\\|//+ *"}}, which matches extra stars and spaces
47c1b5f4 1048after the @samp{/*} itself, and accepts C++ style comments also.
6bf7aab6
DL
1049(Note that @samp{\\} is needed in Lisp syntax to include a @samp{\} in
1050the string, which is needed to deny the first star its special meaning
bd428736 1051in regexp syntax. @xref{Regexp Backslash}.)
6bf7aab6
DL
1052
1053@vindex comment-start
1054@vindex comment-end
1055 When a comment command makes a new comment, it inserts the value of
1056@code{comment-start} to begin it. The value of @code{comment-end} is
aa2d3478
RS
1057inserted after point, so that it will follow the text that you will
1058insert into the comment. When @code{comment-end} is non-empty, it
1059should start with a space. For example, in C mode,
1060@code{comment-start} has the value @w{@code{"/* "}} and
1061@code{comment-end} has the value @w{@code{" */"}}.
6bf7aab6 1062
9234c238
RS
1063@vindex comment-padding
1064 The variable @code{comment-padding} specifies how many spaces
7ae8ad94
RS
1065@code{comment-region} should insert on each line between the comment
1066delimiter and the line's original text. The default is 1, to insert
1067one space. @code{nil} means 0. Alternatively, @code{comment-padding}
1068can hold the actual string to insert.
9234c238 1069
6bf7aab6
DL
1070@vindex comment-multi-line
1071 The variable @code{comment-multi-line} controls how @kbd{C-M-j}
7ae8ad94 1072(@code{indent-new-comment-line}) behaves when used inside a comment.
108262a0
AM
1073Specifically, when @code{comment-multi-line} is @code{nil}, the
1074command inserts a comment terminator, begins a new line, and finally
1075inserts a comment starter. Otherwise it does not insert the
1076terminator and starter, so it effectively continues the current
1077comment across multiple lines. In languages that allow multi-line
1078comments, the choice of value for this variable is a matter of taste.
1079The default for this variable depends on the major mode.
6bf7aab6 1080
4190ce5c 1081@vindex comment-indent-function
6bf7aab6 1082 The variable @code{comment-indent-function} should contain a function
5b31640c 1083that will be called to compute the alignment for a newly inserted
6bf7aab6
DL
1084comment or for aligning an existing comment. It is set differently by
1085various major modes. The function is called with no arguments, but with
1086point at the beginning of the comment, or at the end of a line if a new
1087comment is to be inserted. It should return the column in which the
1088comment ought to start. For example, in Lisp mode, the indent hook
1089function bases its decision on how many semicolons begin an existing
1090comment, and on the code in the preceding lines.
1091
93da5dff
RS
1092@node Documentation
1093@section Documentation Lookup
6bf7aab6 1094
93da5dff
RS
1095 Emacs provides several features you can use to look up the
1096documentation of functions, variables and commands that you plan to
1097use in your program.
6bf7aab6 1098
93da5dff
RS
1099@menu
1100* Info Lookup:: Looking up library functions and commands
1101 in Info files.
1102* Man Page:: Looking up man pages of library functions and commands.
1103* Lisp Doc:: Looking up Emacs Lisp functions, etc.
1104@end menu
6bf7aab6 1105
93da5dff
RS
1106@node Info Lookup
1107@subsection Info Documentation Lookup
85750656 1108
93da5dff
RS
1109@findex info-lookup-symbol
1110@findex info-lookup-file
d2f9ea87 1111@kindex C-h S
e722aa81
CY
1112 For major modes that apply to languages which have documentation in
1113Info, you can use @kbd{C-h S} (@code{info-lookup-symbol}) to view the
1114Info documentation for a symbol used in the program. You specify the
1115symbol with the minibuffer; the default is the symbol appearing in the
1116buffer at point. For example, in C mode this looks for the symbol in
1117the C Library Manual. The command only works if the appropriate
1118manual's Info files are installed.
6bf7aab6 1119
93da5dff
RS
1120 The major mode determines where to look for documentation for the
1121symbol---which Info files to look in, and which indices to search.
1122You can also use @kbd{M-x info-lookup-file} to look for documentation
1123for a file name.
6bf7aab6 1124
dfec8297 1125 If you use @kbd{C-h S} in a major mode that does not support it,
5a7f4c1b 1126it asks you to specify the ``symbol help mode.'' You should enter
dfec8297
RS
1127a command such as @code{c-mode} that would select a major
1128mode which @kbd{C-h S} does support.
6bf7aab6 1129
93da5dff
RS
1130@node Man Page
1131@subsection Man Page Lookup
6bf7aab6 1132
e79c6b89
RS
1133@cindex manual page
1134 On Unix, the main form of on-line documentation was the @dfn{manual
dfec8297 1135page} or @dfn{man page}. In the GNU operating system, we aim to
e79c6b89
RS
1136replace man pages with better-organized manuals that you can browse
1137with Info (@pxref{Misc Help}). This process is not finished, so it is
1138still useful to read manual pages.
6bf7aab6 1139
93da5dff 1140@findex manual-entry
e79c6b89 1141 You can read the man page for an operating system command, library
7ae8ad94 1142function, or system call, with the @kbd{M-x man} command. It
e79c6b89
RS
1143runs the @code{man} program to format the man page; if the system
1144permits, it runs @code{man} asynchronously, so that you can keep on
1145editing while the page is being formatted. (On MS-DOS and MS-Windows
11463, you cannot edit while Emacs waits for @code{man} to finish.) The
1147result goes in a buffer named @samp{*Man @var{topic}*}. These buffers
1148use a special major mode, Man mode, that facilitates scrolling and
1149jumping to other manual pages. For details, type @kbd{C-h m} while in
1150a man page buffer.
6bf7aab6 1151
93da5dff 1152@cindex sections of manual pages
e79c6b89
RS
1153 Each man page belongs to one of ten or more @dfn{sections}, each
1154named by a digit or by a digit and a letter. Sometimes there are
1155multiple man pages with the same name in different sections. To read
1156a man page from a specific section, type
93da5dff
RS
1157@samp{@var{topic}(@var{section})} or @samp{@var{section} @var{topic}}
1158when @kbd{M-x manual-entry} prompts for the topic. For example, to
1159read the man page for the C library function @code{chmod} (as opposed
e79c6b89 1160to a command of the same name), type @kbd{M-x manual-entry @key{RET}
dfec8297
RS
1161chmod(2) @key{RET}}. (@code{chmod} is a system call, so it is in
1162section @samp{2}.)
6bf7aab6 1163
08220274 1164@vindex Man-switches
93da5dff 1165 If you do not specify a section, the results depend on how the
08220274 1166@code{man} program works on your system. Some of them display only
93da5dff
RS
1167the first man page they find. Others display all man pages that have
1168the specified name, so you can move between them with the @kbd{M-n}
08220274
EZ
1169and @kbd{M-p} keys@footnote{On some systems, the @code{man} program
1170accepts a @samp{-a} command-line option which tells it to display all
1171the man pages for the specified topic. If you want this behavior, you
1172can add this option to the value of the variable @code{Man-switches}.}.
1173The mode line shows how many manual pages are present in the Man buffer.
6bf7aab6 1174
93da5dff 1175@vindex Man-fontify-manpage-flag
e79c6b89
RS
1176 By default, Emacs highlights the text in man pages. For a long man
1177page, highlighting can take substantial time. You can turn off
1178highlighting of man pages by setting the variable
1179@code{Man-fontify-manpage-flag} to @code{nil}.
6bf7aab6 1180
93da5dff
RS
1181@findex Man-fontify-manpage
1182 If you insert the text of a man page into an Emacs buffer in some
1183other fashion, you can use the command @kbd{M-x Man-fontify-manpage} to
1184perform the same conversions that @kbd{M-x manual-entry} does.
1185
1186@findex woman
1187@cindex manual pages, on MS-DOS/MS-Windows
1188 An alternative way of reading manual pages is the @kbd{M-x woman}
1189command@footnote{The name of the command, @code{woman}, is an acronym
1190for ``w/o (without) man,'' since it doesn't use the @code{man}
1191program.}. Unlike @kbd{M-x man}, it does not run any external
1192programs to format and display the man pages; instead it does the job
1193in Emacs Lisp, so it works on systems such as MS-Windows, where the
ea118de1 1194@code{man} program (and other programs it uses) are not generally
d2fab838
RS
1195available.
1196
1197 @kbd{M-x woman} prompts for a name of a manual page, and provides
1198completion based on the list of manual pages that are installed on
1199your machine; the list of available manual pages is computed
1200automatically the first time you invoke @code{woman}. The word at
1201point in the current buffer is used to suggest the default for the
1202name the manual page.
93da5dff
RS
1203
1204 With a numeric argument, @kbd{M-x woman} recomputes the list of the
1205manual pages used for completion. This is useful if you add or delete
1206manual pages.
1207
1208 If you type a name of a manual page and @kbd{M-x woman} finds that
1209several manual pages by the same name exist in different sections, it
1210pops up a window with possible candidates asking you to choose one of
1211them.
1212
93da5dff
RS
1213 For more information about setting up and using @kbd{M-x woman}, see
1214@ref{Top, WoMan, Browse UN*X Manual Pages WithOut Man, woman, The WoMan
1215Manual}.
1216
1217@node Lisp Doc
1218@subsection Emacs Lisp Documentation Lookup
1219
1220 As you edit Lisp code to be run in Emacs, you can use the commands
1221@kbd{C-h f} (@code{describe-function}) and @kbd{C-h v}
1222(@code{describe-variable}) to view documentation of functions and
1223variables that you want to use. These commands use the minibuffer to
1224read the name of a function or variable to document, and display the
1225documentation in a window. Their default arguments are based on the
1226code in the neighborhood of point. For @kbd{C-h f}, the default is
1227the function called in the innermost list containing point. @kbd{C-h
1228v} uses the symbol name around or adjacent to point as its default.
1229
1230@cindex Eldoc mode
1231@findex eldoc-mode
1232 A more automatic but less powerful method is Eldoc mode. This minor
1233mode constantly displays in the echo area the argument list for the
1234function being called at point. (In other words, it finds the
1235function call that point is contained in, and displays the argument
054af0fd 1236list of that function.) If point is over a documented variable, it
ea802fce
LT
1237shows the first line of the variable's docstring. Eldoc mode applies
1238in Emacs Lisp and Lisp Interaction modes, and perhaps a few others
1239that provide special support for looking up doc strings. Use the
1240command @kbd{M-x eldoc-mode} to enable or disable this feature.
6bf7aab6 1241
51ed0ea0
DL
1242@node Hideshow
1243@section Hideshow minor mode
1244
1245@findex hs-minor-mode
9234c238 1246 Hideshow minor mode provides selective display of portions of a
93da5dff
RS
1247program, known as @dfn{blocks}. You can use @kbd{M-x hs-minor-mode}
1248to enable or disable this mode, or add @code{hs-minor-mode} to the
1249mode hook for certain major modes in order to enable it automatically
1250for those modes.
51ed0ea0 1251
9234c238
RS
1252 Just what constitutes a block depends on the major mode. In C mode
1253or C++ mode, they are delimited by braces, while in Lisp mode and
1254similar modes they are delimited by parentheses. Multi-line comments
1255also count as blocks.
51ed0ea0
DL
1256
1257@findex hs-hide-all
1258@findex hs-hide-block
1259@findex hs-show-all
1260@findex hs-show-block
1261@findex hs-show-region
1262@findex hs-hide-level
1263@findex hs-minor-mode
6401dc86
EZ
1264@kindex C-c @@ C-h
1265@kindex C-c @@ C-s
1266@kindex C-c @@ C-M-h
1267@kindex C-c @@ C-M-s
1268@kindex C-c @@ C-r
1269@kindex C-c @@ C-l
9234c238
RS
1270@kindex S-Mouse-2
1271@table @kbd
6401dc86 1272@item C-c @@ C-h
9234c238 1273Hide the current block (@code{hs-hide-block}).
6401dc86 1274@item C-c @@ C-s
9234c238 1275Show the current block (@code{hs-show-block}).
6401dc86 1276@item C-c @@ C-c
ea118de1 1277Either hide or show the current block (@code{hs-toggle-hiding}).
9234c238 1278@item S-Mouse-2
ea118de1 1279Either hide or show the block you click on (@code{hs-mouse-toggle-hiding}).
6401dc86 1280@item C-c @@ C-M-h
9234c238 1281Hide all top-level blocks (@code{hs-hide-all}).
6401dc86 1282@item C-c @@ C-M-s
9234c238 1283Show everything in the buffer (@code{hs-show-all}).
6401dc86 1284@item C-c @@ C-l
9234c238
RS
1285Hide all blocks @var{n} levels below this block
1286(@code{hs-hide-level}).
1287@end table
51ed0ea0
DL
1288
1289@vindex hs-hide-comments-when-hiding-all
51ed0ea0
DL
1290@vindex hs-isearch-open
1291@vindex hs-special-modes-alist
19b2c4ca 1292 These variables exist for customizing Hideshow mode.
9234c238 1293
51ed0ea0
DL
1294@table @code
1295@item hs-hide-comments-when-hiding-all
9234c238 1296Non-@code{nil} says that @kbd{hs-hide-all} should hide comments too.
d2fab838 1297
51ed0ea0 1298@item hs-isearch-open
dfec8297
RS
1299Specifies what kind of hidden blocks incremental search should make
1300visible. The value should be one of these four symbols:
d2fab838
RS
1301
1302@table @code
9198a323
RS
1303@item code
1304Open only code blocks.
d2fab838
RS
1305@item comment
1306Open only comments.
1307@item t
9198a323 1308Open both code blocks and comments.
d2fab838 1309@item nil
9198a323 1310Open neither code blocks nor comments.
d2fab838
RS
1311@end table
1312
51ed0ea0 1313@item hs-special-modes-alist
e79c6b89 1314A list of elements, each specifying how to initialize Hideshow
d2fab838
RS
1315variables for one major mode. See the variable's documentation string
1316for more information.
51ed0ea0
DL
1317@end table
1318
93da5dff
RS
1319@node Symbol Completion
1320@section Completion for Symbol Names
1321@cindex completion (symbol names)
3b8b8888 1322
e722aa81
CY
1323 In Emacs, completion is something you normally do in the minibuffer
1324(@pxref{Completion}). But one kind of completion is available in all
1325buffers: completion for symbol names.
3b8b8888 1326
93da5dff 1327@kindex M-TAB
e79c6b89
RS
1328 The character @kbd{M-@key{TAB}} runs a command to complete the
1329partial symbol before point against the set of meaningful symbol
1330names. This command inserts at point any additional characters that
3a8d6df3
RS
1331it can determine from the partial name.
1332
1333 If your window manager defines @kbd{M-@key{TAB}} to switch windows,
1334you can type @kbd{@key{ESC} @key{TAB}} or @kbd{C-M-i} instead.
8474de5b
CY
1335However, most window managers let you customize these shortcuts, so
1336you can change any that interfere with the way you use Emacs.
6bf7aab6 1337
e79c6b89
RS
1338 If the partial name in the buffer has multiple possible completions
1339that differ in the very next character, so that it is impossible to
1340complete even one more character, @kbd{M-@key{TAB}} displays a list of
1341all possible completions in another window.
6bf7aab6 1342
93da5dff
RS
1343@cindex tags-based completion
1344@cindex Info index completion
1345@findex complete-symbol
1346 In most programming language major modes, @kbd{M-@key{TAB}} runs the
1347command @code{complete-symbol}, which provides two kinds of completion.
1348Normally it does completion based on a tags table (@pxref{Tags}); with a
1349numeric argument (regardless of the value), it does completion based on
1350the names listed in the Info file indexes for your language. Thus, to
1351complete the name of a symbol defined in your own program, use
1352@kbd{M-@key{TAB}} with no argument; to complete the name of a standard
1353library function, use @kbd{C-u M-@key{TAB}}. Of course, Info-based
1354completion works only if there is an Info file for the standard library
1355functions of your language, and only if it is installed at your site.
6bf7aab6 1356
93da5dff
RS
1357@cindex Lisp symbol completion
1358@cindex completion (Lisp symbols)
1359@findex lisp-complete-symbol
1360 In Emacs-Lisp mode, the name space for completion normally consists of
1361nontrivial symbols present in Emacs---those that have function
1362definitions, values or properties. However, if there is an
1363open-parenthesis immediately before the beginning of the partial symbol,
1364only symbols with function definitions are considered as completions.
1365The command which implements this is @code{lisp-complete-symbol}.
6bf7aab6 1366
93da5dff
RS
1367 In Text mode and related modes, @kbd{M-@key{TAB}} completes words
1368based on the spell-checker's dictionary. @xref{Spelling}.
6bf7aab6 1369
93da5dff
RS
1370@node Glasses
1371@section Glasses minor mode
1372@cindex Glasses mode
1373@cindex identifiers, making long ones readable
1374@cindex StudlyCaps, making them readable
1375@findex glasses-mode
6bf7aab6 1376
93da5dff 1377 Glasses minor mode makes @samp{unreadableIdentifiersLikeThis}
e79c6b89
RS
1378readable by altering the way they display. It knows two different
1379ways to do this: by displaying underscores between a lower-case letter
1380and the following capital letter, and by emboldening the capital
1381letters. It does not alter the buffer text, only the way they
1382display, so you can use it even on read-only buffers. You can use the
1383command @kbd{M-x glasses-mode} to enable or disable the mode in the
1384current buffer; you can also add @code{glasses-mode} to the mode hook
1385of the programming language major modes in which you normally want
177c0ea7 1386to use Glasses mode.
6bf7aab6 1387
93da5dff
RS
1388@node Misc for Programs
1389@section Other Features Useful for Editing Programs
6bf7aab6 1390
93da5dff 1391 A number of Emacs commands that aren't designed specifically for
e79c6b89 1392editing programs are useful for that nonetheless.
6bf7aab6 1393
93da5dff
RS
1394 The Emacs commands that operate on words, sentences and paragraphs
1395are useful for editing code. Most symbols names contain words
1396(@pxref{Words}); sentences can be found in strings and comments
e79c6b89 1397(@pxref{Sentences}). Paragraphs in the strict sense can be found in
93da5dff
RS
1398program code (in long comments), but the paragraph commands are useful
1399in other places too, because programming language major modes define
1400paragraphs to begin and end at blank lines (@pxref{Paragraphs}).
1401Judicious use of blank lines to make the program clearer will also
1402provide useful chunks of text for the paragraph commands to work on.
1403Auto Fill mode, if enabled in a programming language major mode,
1404indents the new lines which it creates.
6bf7aab6 1405
93da5dff
RS
1406 The selective display feature is useful for looking at the overall
1407structure of a function (@pxref{Selective Display}). This feature
1408hides the lines that are indented more than a specified amount.
1409Programming modes often support Outline minor mode (@pxref{Outline
1410Mode}). The Foldout package provides folding-editor features
1411(@pxref{Foldout}).
6bf7aab6 1412
93da5dff
RS
1413 The ``automatic typing'' features may be useful for writing programs.
1414@xref{Top,,Autotyping, autotype, Autotyping}.
6bf7aab6
DL
1415
1416@node C Modes
1417@section C and Related Modes
1418@cindex C mode
1419@cindex Java mode
1420@cindex Pike mode
1421@cindex IDL mode
1422@cindex CORBA IDL mode
1423@cindex Objective C mode
1424@cindex C++ mode
7ae8ad94 1425@cindex AWK mode
6bf7aab6
DL
1426@cindex mode, Java
1427@cindex mode, C
7ae8ad94 1428@cindex mode, C++
6bf7aab6
DL
1429@cindex mode, Objective C
1430@cindex mode, CORBA IDL
1431@cindex mode, Pike
7ae8ad94 1432@cindex mode, AWK
6bf7aab6 1433
9234c238 1434 This section gives a brief description of the special features
7ae8ad94 1435available in C, C++, Objective-C, Java, CORBA IDL, Pike and AWK modes.
5d80fe1f
EZ
1436(These are called ``C mode and related modes.'') @xref{Top, , CC Mode,
1437ccmode, CC Mode}, for a more extensive description of these modes
9234c238 1438and their special features.
51ed0ea0 1439
6bf7aab6 1440@menu
7ae8ad94
RS
1441* Motion in C:: Commands to move by C statements, etc.
1442* Electric C:: Colon and other chars can automatically reindent.
1443* Hungry Delete:: A more powerful DEL command.
1444* Other C Commands:: Filling comments, viewing expansion of macros,
1445 and other neat features.
6bf7aab6
DL
1446@end menu
1447
1448@node Motion in C
1449@subsection C Mode Motion Commands
1450
1451 This section describes commands for moving point, in C mode and
1452related modes.
1453
1454@table @code
7ae8ad94
RS
1455@item M-x c-beginning-of-defun
1456@itemx M-x c-end-of-defun
1457@findex c-beginning-of-defun
1458@findex c-end-of-defun
1459Move point to the beginning or end of the current function or
1460top-level definition. These are found by searching for the least
1461enclosing braces. (By contrast, @code{beginning-of-defun} and
1462@code{end-of-defun} search for braces in column zero.) If you are
1463editing code where the opening brace of a function isn't placed in
1464column zero, you may wish to bind @code{C-M-a} and @code{C-M-e} to
1465these commands. @xref{Moving by Defuns}.
1466
6bf7aab6
DL
1467@item C-c C-u
1468@kindex C-c C-u @r{(C mode)}
1469@findex c-up-conditional
1470Move point back to the containing preprocessor conditional, leaving the
1471mark behind. A prefix argument acts as a repeat count. With a negative
1472argument, move point forward to the end of the containing
7ae8ad94
RS
1473preprocessor conditional.
1474
1475@samp{#elif} is equivalent to @samp{#else} followed by @samp{#if}, so
1476the function will stop at a @samp{#elif} when going backward, but not
1477when going forward.
6bf7aab6
DL
1478
1479@item C-c C-p
1480@kindex C-c C-p @r{(C mode)}
1481@findex c-backward-conditional
1482Move point back over a preprocessor conditional, leaving the mark
1483behind. A prefix argument acts as a repeat count. With a negative
1484argument, move forward.
1485
1486@item C-c C-n
1487@kindex C-c C-n @r{(C mode)}
1488@findex c-forward-conditional
1489Move point forward across a preprocessor conditional, leaving the mark
1490behind. A prefix argument acts as a repeat count. With a negative
1491argument, move backward.
1492
1493@item M-a
7ae8ad94 1494@kindex M-a (C mode)
6bf7aab6
DL
1495@findex c-beginning-of-statement
1496Move point to the beginning of the innermost C statement
1497(@code{c-beginning-of-statement}). If point is already at the beginning
1498of a statement, move to the beginning of the preceding statement. With
1499prefix argument @var{n}, move back @var{n} @minus{} 1 statements.
1500
7ae8ad94
RS
1501In comments or in strings which span more than one line, this command
1502moves by sentences instead of statements.
6bf7aab6
DL
1503
1504@item M-e
7ae8ad94 1505@kindex M-e (C mode)
6bf7aab6 1506@findex c-end-of-statement
7ae8ad94
RS
1507Move point to the end of the innermost C statement or sentence; like
1508@kbd{M-a} except that it moves in the other direction
1509(@code{c-end-of-statement}).
6bf7aab6
DL
1510@end table
1511
1512@node Electric C
1513@subsection Electric C Characters
1514
1515 In C mode and related modes, certain printing characters are
108262a0
AM
1516@dfn{electric}---in addition to inserting themselves, they also
1517reindent the current line, and optionally also insert newlines. The
64e207c0
RS
1518``electric'' characters are @kbd{@{}, @kbd{@}}, @kbd{:}, @kbd{#},
1519@kbd{;}, @kbd{,}, @kbd{<}, @kbd{>}, @kbd{/}, @kbd{*}, @kbd{(}, and
f5eb910a 1520@kbd{)}.
108262a0
AM
1521
1522 You might find electric indentation inconvenient if you are editing
1523chaotically indented code. If you are new to CC Mode, you might find
1524it disconcerting. You can toggle electric action with the command
1525@kbd{C-c C-l}; when it is enabled, @samp{/l} appears in the mode line
1526after the mode name:
6bf7aab6 1527
108262a0
AM
1528@table @kbd
1529@item C-c C-l
1530@kindex C-c C-l @r{(C mode)}
1531@findex c-toggle-electric-state
1532Toggle electric action (@code{c-toggle-electric-state}). With a
1533prefix argument, this command enables electric action if the argument
1534is positive, disables it if it is negative.
1535@end table
1536
1537 Electric characters insert newlines only when, in addition to the
1538electric state, the @dfn{auto-newline} feature is enabled (indicated
1539by @samp{/la} in the mode line after the mode name). You can turn
1540this feature on or off with the command @kbd{C-c C-a}:
6bf7aab6
DL
1541
1542@table @kbd
1543@item C-c C-a
1544@kindex C-c C-a @r{(C mode)}
108262a0
AM
1545@findex c-toggle-auto-newline
1546Toggle the auto-newline feature (@code{c-toggle-auto-newline}). With a
6bf7aab6
DL
1547prefix argument, this command turns the auto-newline feature on if the
1548argument is positive, and off if it is negative.
1549@end table
1550
f5eb910a
RS
1551 Usually the CC Mode style configures the exact circumstances in
1552which Emacs inserts auto-newlines. You can also configure this
1553directly. @xref{Custom Auto-newlines,,, ccmode, The CC Mode Manual}.
6bf7aab6
DL
1554
1555@node Hungry Delete
1556@subsection Hungry Delete Feature in C
7ae8ad94 1557@cindex hungry deletion (C Mode)
6bf7aab6 1558
108262a0
AM
1559 If you want to delete an entire block of whitespace at point, you
1560can use @dfn{hungry deletion}. This deletes all the contiguous
1561whitespace either before point or after point in a single operation.
1562@dfn{Whitespace} here includes tabs and newlines, but not comments or
1563preprocessor commands.
6bf7aab6
DL
1564
1565@table @kbd
69d271a7
AM
1566@item C-c C-@key{DEL}
1567@itemx C-c @key{DEL}
aca2cfd2 1568@findex c-hungry-delete-backwards
69d271a7
AM
1569@kindex C-c C-@key{DEL} (C Mode)
1570@kindex C-c @key{DEL} (C Mode)
aca2cfd2 1571@code{c-hungry-delete-backwards}---Delete the entire block of whitespace
108262a0
AM
1572preceding point.
1573
6bf7aab6 1574@item C-c C-d
69d271a7
AM
1575@itemx C-c C-@key{DELETE}
1576@itemx C-c @key{DELETE}
108262a0
AM
1577@findex c-hungry-delete-forward
1578@kindex C-c C-d (C Mode)
69d271a7
AM
1579@kindex C-c C-@key{DELETE} (C Mode)
1580@kindex C-c @key{DELETE} (C Mode)
108262a0
AM
1581@code{c-hungry-delete-forward}---Delete the entire block of whitespace
1582following point.
1583@end table
1584
1585 As an alternative to the above commands, you can enable @dfn{hungry
1586delete mode}. When this feature is enabled (indicated by @samp{/h} in
d884be12
RS
1587the mode line after the mode name), a single @key{DEL} deletes all
1588preceding whitespace, not just one space, and a single @kbd{C-c C-d}
1589(but @emph{not} plain @key{DELETE}) deletes all following whitespace.
6bf7aab6 1590
108262a0
AM
1591@table @kbd
1592@item M-x c-toggle-hungry-state
1593@findex c-toggle-hungry-state
1594Toggle the hungry-delete feature
1595(@code{c-toggle-hungry-state})@footnote{This command had the binding
1596@kbd{C-c C-d} in earlier versions of Emacs. @kbd{C-c C-d} is now
1597bound to @code{c-hungry-delete-forward}.}. With a prefix argument,
1598this command turns the hungry-delete feature on if the argument is
1599positive, and off if it is negative.
6bf7aab6
DL
1600@end table
1601
1602@vindex c-hungry-delete-key
1603 The variable @code{c-hungry-delete-key} controls whether the
1604hungry-delete feature is enabled.
1605
1606@node Other C Commands
1607@subsection Other Commands for C Mode
1608
1609@table @kbd
108262a0
AM
1610@item C-c C-w
1611@itemx M-x c-subword-mode
1612@findex c-subword-mode
f5eb910a 1613Enable (or disable) @dfn{subword mode}. In subword mode, Emacs's word
8a75579f 1614commands recognize upper case letters in
f5eb910a
RS
1615@samp{StudlyCapsIdentifiers} as word boundaries. This is indicated by
1616the flag @samp{/w} on the mode line after the mode name
1617(e.g. @samp{C/law}). You can even use @kbd{M-x c-subword-mode} in
1618non-CC Mode buffers.
108262a0 1619
dfec8297
RS
1620In the GNU project, we recommend using underscores to separate words
1621within an identifier in C or C++, rather than using case distinctions.
1622
7ae8ad94
RS
1623@item M-x c-context-line-break
1624@findex c-context-line-break
1625This command inserts a line break and indents the new line in a manner
1626appropriate to the context. In normal code, it does the work of
1627@kbd{C-j} (@code{newline-and-indent}), in a C preprocessor line it
1628additionally inserts a @samp{\} at the line break, and within comments
1629it's like @kbd{M-j} (@code{c-indent-new-comment-line}).
1630
1631@code{c-context-line-break} isn't bound to a key by default, but it
1632needs a binding to be useful. The following code will bind it to
108262a0
AM
1633@kbd{C-j}. We use @code{c-initialization-hook} here to make sure
1634the keymap is loaded before we try to change it.
1635
444246ca 1636@smallexample
108262a0
AM
1637(defun my-bind-clb ()
1638 (define-key c-mode-base-map "\C-j" 'c-context-line-break))
1639(add-hook 'c-initialization-hook 'my-bind-clb)
444246ca 1640@end smallexample
7ae8ad94 1641
6bf7aab6 1642@item C-M-h
6bf7aab6
DL
1643Put mark at the end of a function definition, and put point at the
1644beginning (@code{c-mark-function}).
1645
1646@item M-q
1647@kindex M-q @r{(C mode)}
1648@findex c-fill-paragraph
1649Fill a paragraph, handling C and C++ comments (@code{c-fill-paragraph}).
1650If any part of the current line is a comment or within a comment, this
1651command fills the comment or the paragraph of it that point is in,
1652preserving the comment indentation and comment delimiters.
1653
1654@item C-c C-e
1655@cindex macro expansion in C
1656@cindex expansion of C macros
1657@findex c-macro-expand
1658@kindex C-c C-e @r{(C mode)}
1659Run the C preprocessor on the text in the region, and show the result,
1660which includes the expansion of all the macro calls
1661(@code{c-macro-expand}). The buffer text before the region is also
1662included in preprocessing, for the sake of macros defined there, but the
1663output from this part isn't shown.
1664
1665When you are debugging C code that uses macros, sometimes it is hard to
1666figure out precisely how the macros expand. With this command, you
1667don't have to figure it out; you can see the expansions.
1668
1669@item C-c C-\
1670@findex c-backslash-region
1671@kindex C-c C-\ @r{(C mode)}
1672Insert or align @samp{\} characters at the ends of the lines of the
1673region (@code{c-backslash-region}). This is useful after writing or
1674editing a C macro definition.
1675
1676If a line already ends in @samp{\}, this command adjusts the amount of
1677whitespace before it. Otherwise, it inserts a new @samp{\}. However,
1678the last line in the region is treated specially; no @samp{\} is
1679inserted on that line, and any @samp{\} there is deleted.
1680
1681@item M-x cpp-highlight-buffer
1682@cindex preprocessor highlighting
1683@findex cpp-highlight-buffer
1684Highlight parts of the text according to its preprocessor conditionals.
1685This command displays another buffer named @samp{*CPP Edit*}, which
1686serves as a graphic menu for selecting how to display particular kinds
1687of conditionals and their contents. After changing various settings,
1688click on @samp{[A]pply these settings} (or go to that buffer and type
1689@kbd{a}) to rehighlight the C mode buffer accordingly.
1690
1691@item C-c C-s
1692@findex c-show-syntactic-information
1693@kindex C-c C-s @r{(C mode)}
1694Display the syntactic information about the current source line
054af0fd
SE
1695(@code{c-show-syntactic-information}). This information directs how
1696the line is indented.
3b8b8888
DL
1697
1698@item M-x cwarn-mode
1699@itemx M-x global-cwarn-mode
1700@findex cwarn-mode
1701@findex global-cwarn-mode
7ae8ad94 1702@vindex global-cwarn-mode
3b8b8888
DL
1703@cindex CWarn mode
1704@cindex suspicious constructions in C, C++
9234c238 1705CWarn minor mode highlights certain suspicious C and C++ constructions:
3b8b8888
DL
1706
1707@itemize @bullet{}
1708@item
9234c238 1709Assignments inside expressions.
3b8b8888
DL
1710@item
1711Semicolon following immediately after @samp{if}, @samp{for}, and @samp{while}
1712(except after a @samp{do @dots{} while} statement);
1713@item
1714C++ functions with reference parameters.
1715@end itemize
1716
1717@noindent
9234c238
RS
1718You can enable the mode for one buffer with the command @kbd{M-x
1719cwarn-mode}, or for all suitable buffers with the command @kbd{M-x
1720global-cwarn-mode} or by customizing the variable
1721@code{global-cwarn-mode}. You must also enable Font Lock mode to make
1722it work.
3b8b8888
DL
1723
1724@item M-x hide-ifdef-mode
1725@findex hide-ifdef-mode
1726@cindex Hide-ifdef mode
8474de5b 1727@vindex hide-ifdef-shadow
3b8b8888 1728Hide-ifdef minor mode hides selected code within @samp{#if} and
8474de5b
CY
1729@samp{#ifdef} preprocessor blocks. If you change the variable
1730@code{hide-ifdef-shadow} to @code{t}, Hide-ifdef minor mode
1731``shadows'' preprocessor blocks by displaying them with a less
1732prominent face, instead of hiding them entirely. See the
1733documentation string of @code{hide-ifdef-mode} for more information.
9234c238
RS
1734
1735@item M-x ff-find-related-file
1736@cindex related files
1737@findex ff-find-related-file
1738@vindex ff-related-file-alist
1739Find a file ``related'' in a special way to the file visited by the
1740current buffer. Typically this will be the header file corresponding
1741to a C/C++ source file, or vice versa. The variable
1742@code{ff-related-file-alist} specifies how to compute related file
1743names.
6bf7aab6
DL
1744@end table
1745
6bf7aab6
DL
1746@node Asm Mode
1747@section Asm Mode
1748
1749@cindex Asm mode
9234c238 1750@cindex assembler mode
6bf7aab6
DL
1751Asm mode is a major mode for editing files of assembler code. It
1752defines these commands:
1753
1754@table @kbd
1755@item @key{TAB}
1756@code{tab-to-tab-stop}.
1757@item C-j
1758Insert a newline and then indent using @code{tab-to-tab-stop}.
1759@item :
1760Insert a colon and then remove the indentation from before the label
1761preceding colon. Then do @code{tab-to-tab-stop}.
1762@item ;
1763Insert or align a comment.
1764@end table
1765
1766 The variable @code{asm-comment-char} specifies which character
1767starts comments in assembler syntax.
ab5796a9 1768
b23ef7a5
EZ
1769@ifnottex
1770@include fortran-xtra.texi
1771@end ifnottex
1772
ab5796a9
MB
1773@ignore
1774 arch-tag: c7ee7409-40a4-45c7-bfb7-ae7f2c74d0c0
1775@end ignore