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