1 @c -*- coding: utf-8 -*-
2 @c This is part of the Emacs manual.
3 @c Copyright (C) 1985-1987, 1993-1995, 1997, 1999-2018 Free Software
5 @c See file emacs.texi for copying conditions.
7 @chapter Editing Programs
10 @cindex program editing
12 This chapter describes Emacs features for facilitating editing
13 programs. Some of the things these features can do are:
17 Find or move over top-level definitions (@pxref{Defuns}).
19 Apply the usual indentation conventions of the language
20 (@pxref{Program Indent}).
22 Balance parentheses (@pxref{Parentheses}).
24 Insert, kill or align comments (@pxref{Comments}).
26 Highlight program syntax (@pxref{Font Lock}).
30 * Program Modes:: Major modes for editing programs.
31 * Defuns:: Commands to operate on major top-level parts
33 * Program Indent:: Adjusting indentation to show the nesting.
34 * Parentheses:: Commands that operate on parentheses.
35 * Comments:: Inserting, killing, and aligning comments.
36 * Documentation:: Getting documentation of functions you plan to call.
37 * Hideshow:: Displaying blocks selectively.
38 * Symbol Completion:: Completion on symbol names of your program or language.
39 * MixedCase Words:: Dealing with identifiersLikeThis.
40 * Semantic:: Suite of editing tools based on source code parsing.
41 * Misc for Programs:: Other Emacs features useful for editing programs.
42 * C Modes:: Special commands of C, C++, Objective-C, Java,
43 IDL, Pike and AWK modes.
44 * Asm Mode:: Asm mode and its special features.
46 * Fortran:: Fortran mode and its special features.
51 @section Major Modes for Programming Languages
52 @cindex modes for programming languages
54 Emacs has specialized major modes (@pxref{Major Modes}) for many
55 programming languages. A programming language mode typically
56 specifies the syntax of expressions, the customary rules for
57 indentation, how to do syntax highlighting for the language, and how
58 to find the beginning or end of a function definition. It often has
59 features for compiling and debugging programs as well. The major mode
60 for each language is named after the language; for instance, the major
61 mode for the C programming language is @code{c-mode}.
79 @cindex Shell-script mode
82 @cindex PostScript mode
85 @cindex Javascript mode
87 Emacs has programming language modes for Lisp, Scheme, the
88 Scheme-based DSSSL expression language, Ada, ASM, AWK, C, C++,
89 Fortran, Icon, IDL (CORBA), IDLWAVE, Java, Javascript, M4, Makefiles,
90 Metafont (@TeX{}'s companion for font creation), Modula2, Object
91 Pascal, Objective-C, Octave, Pascal, Perl, Pike, PostScript, Prolog,
92 Python, Ruby, Simula, SQL, Tcl, Verilog, and VHDL@. An alternative
93 mode for Perl is called CPerl mode. Modes are also available for the
94 scripting languages of the common GNU and Unix shells, and
95 MS-DOS/MS-Windows @samp{BAT} files, and for makefiles, DNS master
96 files, and various sorts of configuration files.
98 Ideally, Emacs should have a major mode for each programming
99 language that you might want to edit. If it doesn't have a mode for
100 your favorite language, the mode might be implemented in a package not
101 distributed with Emacs (@pxref{Packages}); or you can contribute one.
103 @kindex DEL @r{(programming modes)}
104 @findex backward-delete-char-untabify
105 In most programming languages, indentation should vary from line to
106 line to illustrate the structure of the program. Therefore, in most
107 programming language modes, typing @kbd{@key{TAB}} updates the
108 indentation of the current line (@pxref{Program Indent}).
109 Furthermore, @kbd{@key{DEL}} is usually bound to
110 @code{backward-delete-char-untabify}, which deletes backward treating
111 each tab as if it were the equivalent number of spaces, so that you
112 can delete one column of indentation without worrying whether the
113 whitespace consists of spaces or tabs.
115 @cindex mode hook, and major modes
117 @vindex lisp-mode-hook
118 @vindex emacs-lisp-mode-hook
119 @vindex lisp-interaction-mode-hook
120 @vindex scheme-mode-hook
121 Entering a programming language mode runs the custom Lisp functions
122 specified in the hook variable @code{prog-mode-hook}, followed by
123 those specified in the mode's own mode hook (@pxref{Major Modes}).
124 For instance, entering C mode runs the hooks @code{prog-mode-hook} and
125 @code{c-mode-hook}. @xref{Hooks}, for information about hooks.
128 Separate manuals are available for the modes for Ada (@pxref{Top,,
129 Ada Mode, ada-mode, Ada Mode}), C/C++/Objective C/Java/Corba
130 IDL/Pike/AWK (@pxref{Top, , CC Mode, ccmode, CC Mode}), Octave, VHDL,
131 and IDLWAVE (@pxref{Top,, IDLWAVE, idlwave, IDLWAVE User Manual}).
134 The Emacs distribution contains Info manuals for the major modes for
135 Ada, C/C++/Objective C/Java/Corba IDL/Pike/AWK, Octave, VHDL, and
136 IDLWAVE@. For Fortran mode, @pxref{Fortran,,, emacs-xtra, Specialized
141 @section Top-Level Definitions, or Defuns
143 In Emacs, a major definition at the top level in the buffer, such as
144 a function, is called a @dfn{defun}. The name comes from Lisp, but in
145 Emacs we use it for all languages.
148 * Left Margin Paren:: An open-paren or similar opening delimiter
149 starts a defun if it is at the left margin.
150 * Moving by Defuns:: Commands to move over or mark a major definition.
151 * Imenu:: Making buffer indexes as menus.
152 * Which Function:: Which Function mode shows which function you are in.
155 @node Left Margin Paren
156 @subsection Left Margin Convention
158 @cindex open-parenthesis in leftmost column
159 @cindex ( in leftmost column
160 Many programming-language modes assume by default that any opening
161 delimiter found at the left margin is the start of a top-level
162 definition, or defun. Therefore, @strong{don't put an opening
163 delimiter at the left margin unless it should have that significance}.
164 For instance, never put an open-parenthesis at the left margin in a
165 Lisp file unless it is the start of a top-level list.
167 The convention speeds up many Emacs operations, which would
168 otherwise have to scan back to the beginning of the buffer to analyze
169 the syntax of the code.
171 If you don't follow this convention, not only will you have trouble
172 when you explicitly use the commands for motion by defuns; other
173 features that use them will also give you trouble. This includes the
174 indentation commands (@pxref{Program Indent}) and Font Lock mode
177 The most likely problem case is when you want an opening delimiter
178 at the start of a line inside a string. To avoid trouble, put an
179 escape character (@samp{\}, in C and Emacs Lisp, @samp{/} in some
180 other Lisp dialects) before the opening delimiter. This will not
181 affect the contents of the string, but will prevent that opening
182 delimiter from starting a defun. Here's an example:
190 To help you catch violations of this convention, Font Lock mode
191 highlights confusing opening delimiters (those that ought to be
194 @vindex open-paren-in-column-0-is-defun-start
195 If you need to override this convention, you can do so by setting
196 the variable @code{open-paren-in-column-0-is-defun-start}.
197 If this user option is set to @code{t} (the default), opening
198 parentheses or braces at column zero always start defuns. When it is
199 @code{nil}, defuns are found by searching for parens or braces at the
202 Usually, you should leave this option at its default value of
203 @code{t}. If your buffer contains parentheses or braces in column
204 zero which don't start defuns, and it is somehow impractical to remove
205 these parentheses or braces, it might be helpful to set the option to
206 @code{nil}. Be aware that this might make scrolling and display in
207 large buffers quite sluggish. Furthermore, the parentheses and braces
208 must be correctly matched throughout the buffer for it to work
211 @node Moving by Defuns
212 @subsection Moving by Defuns
215 These commands move point or set up the region based on top-level
216 major definitions, also called @dfn{defuns}.
220 Move to beginning of current or preceding defun
221 (@code{beginning-of-defun}).
223 Move to end of current or following defun (@code{end-of-defun}).
225 Put region around whole current or following defun (@code{mark-defun}).
228 @cindex move to beginning or end of function
229 @cindex function, move to beginning or end
233 @findex beginning-of-defun
236 The commands to move to the beginning and end of the current defun
237 are @kbd{C-M-a} (@code{beginning-of-defun}) and @kbd{C-M-e}
238 (@code{end-of-defun}). If you repeat one of these commands, or use a
239 positive numeric argument, each repetition moves to the next defun in
240 the direction of motion.
242 @kbd{C-M-a} with a negative argument @minus{}@var{n} moves forward
243 @var{n} times to the next beginning of a defun. This is not exactly
244 the same place that @kbd{C-M-e} with argument @var{n} would move to;
245 the end of this defun is not usually exactly the same place as the
246 beginning of the following defun. (Whitespace, comments, and perhaps
247 declarations can separate them.) Likewise, @kbd{C-M-e} with a
248 negative argument moves back to an end of a defun, which is not quite
249 the same as @kbd{C-M-a} with a positive argument.
251 @kindex C-M-h @r{(C mode)}
252 @findex c-mark-function
253 To operate on the current defun, use @kbd{C-M-h}
254 (@code{mark-defun}), which sets the mark at the end of the current
255 defun and puts point at its beginning. @xref{Marking Objects}. This
256 is the easiest way to get ready to kill the defun in order to move it
257 to a different place in the file. If the defun is directly preceded
258 by comments (with no intervening blank lines), they are marked, too.
259 If you use the command while point is between defuns, it uses the
260 following defun. If you use the command while the mark is already
261 active, it extends the end of the region to include one more defun.
262 With a prefix argument, it marks that many defuns or extends the
263 region by the appropriate number of defuns. With negative prefix
264 argument it marks defuns in the opposite direction and also changes
265 the direction of selecting for subsequent uses of @code{mark-defun}.
267 In C mode, @kbd{C-M-h} runs the function @code{c-mark-function},
268 which is almost the same as @code{mark-defun}; the difference is that
269 it backs up over the argument declarations, function name and returned
270 data type so that the entire C function is inside the region. This is
271 an example of how major modes adjust the standard key bindings so that
272 they do their standard jobs in a way better fitting a particular
273 language. Other major modes may replace any or all of these key
274 bindings for that purpose.
278 @cindex index of buffer definitions
279 @cindex buffer definitions index
281 The Imenu facility offers a way to find the major definitions in
282 a file by name. It is also useful in text formatter major modes,
283 where it treats each chapter, section, etc., as a definition.
284 (@xref{Xref}, for a more powerful feature that handles multiple files
288 If you type @kbd{M-x imenu}, it reads the name of a definition using
289 the minibuffer, then moves point to that definition. You can use
290 completion to specify the name; the command always displays the whole
293 @findex imenu-add-menubar-index
294 Alternatively, you can bind the command @code{imenu} to a mouse
295 click. Then it displays mouse menus for you to select a definition
296 name. 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
298 item available for all buffers in a certain major mode, you can do
299 this by adding @code{imenu-add-menubar-index} to its mode hook. But
300 if you have done that, you will have to wait a little while each time
301 you visit a file in that mode, while Emacs finds all the definitions
304 @vindex imenu-auto-rescan
305 When you change the contents of a buffer, if you add or delete
306 definitions, you can update the buffer's index based on the
307 new contents by invoking the @samp{*Rescan*} item in the menu.
308 Rescanning happens automatically if you set @code{imenu-auto-rescan} to
309 a non-@code{nil} value. There is no need to rescan because of small
312 @vindex imenu-sort-function
313 You can customize the way the menus are sorted by setting the
314 variable @code{imenu-sort-function}. By default, names are ordered as
315 they occur in the buffer; if you want alphabetic sorting, use the
316 symbol @code{imenu--sort-by-name} as the value. You can also
317 define your own comparison function by writing Lisp code.
319 Imenu provides the information to guide Which Function mode
321 (@pxref{Which Function}).
326 The Speedbar can also use it (@pxref{Speedbar}).
329 @subsection Which Function Mode
330 @cindex current function name in mode line
332 Which Function mode is a global minor mode (@pxref{Minor Modes})
333 which displays the current function name in the mode line, updating it
334 as you move around in a buffer.
336 @findex which-function-mode
337 @vindex which-func-modes
338 To either enable or disable Which Function mode, use the command
339 @kbd{M-x which-function-mode}. Which Function mode is a global minor
340 mode. By default, it takes effect in all major modes that
341 know how to support it (i.e., all the major modes that support
342 Imenu). You can restrict it to a specific list of major modes by
343 changing the value of the variable @code{which-func-modes} from
344 @code{t} (which means to support all available major modes) to a list
348 @section Indentation for Programs
349 @cindex indentation for programs
351 The best way to keep a program properly indented is to use Emacs to
352 reindent it as you change it. Emacs has commands to indent either a
353 single line, a specified number of lines, or all of the lines inside a
354 single parenthetical grouping.
356 @xref{Indentation}, for general information about indentation. This
357 section describes indentation features specific to programming
361 * Basic Indent:: Indenting a single line.
362 * Multi-line Indent:: Commands to reindent many lines at once.
363 * Lisp Indent:: Specifying how each Lisp function should be indented.
364 * C Indent:: Extra features for indenting C and related modes.
365 * Custom C Indent:: Controlling indentation style for C and related modes.
368 @cindex pretty-printer
369 Emacs also provides a Lisp pretty-printer in the @code{pp} package,
370 which reformats Lisp objects with nice-looking indentation.
371 @xref{Output Functions, pp,, elisp, The Emacs Lisp Reference Manual}.
374 @subsection Basic Program Indentation Commands
378 Adjust indentation of current line (@code{indent-for-tab-command}).
380 Insert a newline, then adjust indentation of following line
384 @kindex TAB @r{(programming modes)}
385 @findex indent-line-function
386 The basic indentation command is @kbd{@key{TAB}}
387 (@code{indent-for-tab-command}), which was documented in
388 @ref{Indentation}. In programming language modes, @kbd{@key{TAB}}
389 indents the current line, based on the indentation and syntactic
390 content of the preceding lines; if the region is active,
391 @kbd{@key{TAB}} indents each line within the region, not just the
394 The command @kbd{@key{RET}} (@code{newline}), which was documented
395 in @ref{Inserting Text}, does the same as @kbd{C-j} followed by
396 @kbd{@key{TAB}}: it inserts a new line, then adjusts the line's
399 When indenting a line that starts within a parenthetical grouping,
400 Emacs usually places the start of the line under the preceding line
401 within the group, or under the text after the parenthesis. If you
402 manually give one of these lines a nonstandard indentation (e.g., for
403 aesthetic purposes), the lines below will follow it.
405 The indentation commands for most programming language modes assume
406 that an open-parenthesis, open-brace or other opening delimiter at the
407 left margin is the start of a function. If the code you are editing
408 violates this assumption---even if the delimiters occur in strings or
409 comments---you must set @code{open-paren-in-column-0-is-defun-start}
410 to @code{nil} for indentation to work properly. @xref{Left Margin
413 @node Multi-line Indent
414 @subsection Indenting Several Lines
416 Sometimes, you may want to reindent several lines of code at a time.
417 One way to do this is to use the mark; when the mark is active and the
418 region is non-empty, @kbd{@key{TAB}} indents every line in the region.
419 Alternatively, the command @kbd{C-M-\} (@code{indent-region}) indents
420 every line in the region, whether or not the mark is active
421 (@pxref{Indentation Commands}).
423 In addition, Emacs provides the following commands for indenting
424 large chunks of code:
428 Reindent all the lines within one parenthetical grouping.
430 Shift an entire parenthetical grouping rigidly sideways so that its
431 first line is properly indented.
432 @item M-x indent-code-rigidly
433 Shift all the lines in the region rigidly sideways, but do not alter
434 lines that start inside comments and strings.
438 @findex indent-pp-sexp
439 To reindent the contents of a single parenthetical grouping,
440 position point before the beginning of the grouping and type
441 @kbd{C-M-q}. This changes the relative indentation within the
442 grouping, without affecting its overall indentation (i.e., the
443 indentation of the line where the grouping starts). The function that
444 @kbd{C-M-q} runs depends on the major mode; it is
445 @code{indent-pp-sexp} in Lisp mode, @code{c-indent-exp} in C mode,
446 etc. To correct the overall indentation as well, type @kbd{@key{TAB}}
450 If you like the relative indentation within a grouping but not the
451 indentation of its first line, move point to that first line and type
452 @kbd{C-u @key{TAB}}. In Lisp, C, and some other major modes,
453 @kbd{@key{TAB}} with a numeric argument reindents the current line as
454 usual, then reindents by the same amount all the lines in the
455 parenthetical grouping starting on the current line. It is clever,
456 though, and does not alter lines that start inside strings. Neither
457 does it alter C preprocessor lines when in C mode, but it does
458 reindent any continuation lines that may be attached to them.
460 @findex indent-code-rigidly
461 The command @kbd{M-x indent-code-rigidly} rigidly shifts all the
462 lines in the region sideways, like @code{indent-rigidly} does
463 (@pxref{Indentation Commands}). It doesn't alter the indentation of
464 lines that start inside a string, unless the region also starts inside
465 that string. The prefix arg specifies the number of columns to
469 @subsection Customizing Lisp Indentation
470 @cindex customizing Lisp indentation
472 The indentation pattern for a Lisp expression can depend on the function
473 called by the expression. For each Lisp function, you can choose among
474 several predefined patterns of indentation, or define an arbitrary one with
477 The standard pattern of indentation is as follows: the second line of the
478 expression is indented under the first argument, if that is on the same
479 line as the beginning of the expression; otherwise, the second line is
480 indented underneath the function name. Each following line is indented
481 under the previous line whose nesting depth is the same.
483 @vindex lisp-indent-offset
484 If the variable @code{lisp-indent-offset} is non-@code{nil}, it overrides
485 the usual indentation pattern for the second line of an expression, so that
486 such lines are always indented @code{lisp-indent-offset} more columns than
489 @vindex lisp-body-indent
490 Certain functions override the standard pattern. Functions whose
491 names start with @code{def} treat the second lines as the start of
492 a @dfn{body}, by indenting the second line @code{lisp-body-indent}
493 additional columns beyond the open-parenthesis that starts the
496 @cindex @code{lisp-indent-function} property
497 You can override the standard pattern in various ways for individual
498 functions, according to the @code{lisp-indent-function} property of
499 the function name. This is normally done for macro definitions, using
500 the @code{declare} construct. @xref{Defining Macros,,, elisp, The
501 Emacs Lisp Reference Manual}.
504 @subsection Commands for C Indentation
506 Here are special features for indentation in C mode and related modes:
510 @kindex C-c C-q @r{(C mode)}
511 @findex c-indent-defun
512 Reindent the current top-level function definition or aggregate type
513 declaration (@code{c-indent-defun}).
516 @kindex C-M-q @r{(C mode)}
518 Reindent each line in the balanced expression that follows point
519 (@code{c-indent-exp}). A prefix argument inhibits warning messages
520 about invalid syntax.
523 @findex c-indent-command
524 Reindent the current line, and/or in some cases insert a tab character
525 (@code{c-indent-command}).
527 @vindex c-tab-always-indent
528 If @code{c-tab-always-indent} is @code{t}, this command always reindents
529 the current line and does nothing else. This is the default.
531 If that variable is @code{nil}, this command reindents the current line
532 only if point is at the left margin or in the line's indentation;
533 otherwise, it inserts a tab (or the equivalent number of spaces,
534 if @code{indent-tabs-mode} is @code{nil}).
536 Any other value (not @code{nil} or @code{t}) means always reindent the
537 line, and also insert a tab if within a comment or a string.
540 To reindent the whole current buffer, type @kbd{C-x h C-M-\}. This
541 first selects the whole buffer as the region, then reindents that
544 To reindent the current block, use @kbd{C-M-u C-M-q}. This moves
545 to the front of the block and then reindents it all.
547 @node Custom C Indent
548 @subsection Customizing C Indentation
549 @cindex style (for indentation)
551 C mode and related modes use a flexible mechanism for customizing
552 indentation. C mode indents a source line in two steps: first it
553 classifies the line syntactically according to its contents and
554 context; second, it determines the indentation offset associated by
555 your selected @dfn{style} with the syntactic construct and adds this
556 onto the indentation of the @dfn{anchor statement}.
559 @item C-c . @var{style} @key{RET}
560 Select a predefined style @var{style} (@code{c-set-style}).
563 A @dfn{style} is a named collection of customizations that can be
564 used in C mode and the related modes. @ref{Styles,,, ccmode, The CC
565 Mode Manual}, for a complete description. Emacs comes with several
566 predefined styles, including @code{gnu}, @code{k&r}, @code{bsd},
567 @code{stroustrup}, @code{linux}, @code{python}, @code{java},
568 @code{whitesmith}, @code{ellemtel}, and @code{awk}. Some of these
569 styles are primarily intended for one language, but any of them can be
570 used with any of the languages supported by these modes. To find out
571 what a style looks like, select it and reindent some code, e.g., by
572 typing @kbd{C-M-q} at the start of a function definition.
574 @kindex C-c . @r{(C mode)}
576 To choose a style for the current buffer, use the command @w{@kbd{C-c
577 .}}. Specify a style name as an argument (case is not significant).
578 This command affects the current buffer only, and it affects only
579 future invocations of the indentation commands; it does not reindent
580 the code already in the buffer. To reindent the whole buffer in the
581 new style, you can type @kbd{C-x h C-M-\}.
583 @vindex c-default-style
584 You can also set the variable @code{c-default-style} to specify the
585 default style for various major modes. Its value should be either the
586 style's name (a string) or an alist, in which each element specifies
587 one major mode and which indentation style to use for it. For
591 (setq c-default-style
592 '((java-mode . "java")
598 specifies explicit choices for Java and AWK modes, and the default
599 @samp{gnu} style for the other C-like modes. (These settings are
600 actually the defaults.) This variable takes effect when you select
601 one of the C-like major modes; thus, if you specify a new default
602 style for Java mode, you can make it take effect in an existing Java
603 mode buffer by typing @kbd{M-x java-mode} there.
605 The @code{gnu} style specifies the formatting recommended by the GNU
606 Project for C; it is the default, so as to encourage use of our
609 @xref{Indentation Engine Basics,,, ccmode, the CC Mode Manual}, and
610 @ref{Customizing Indentation,,, ccmode, the CC Mode Manual}, for more
611 information on customizing indentation for C and related modes,
612 including how to override parts of an existing style and how to define
616 @findex c-guess-install
617 As an alternative to specifying a style, you can tell Emacs to guess
618 a style by typing @kbd{M-x c-guess} in a sample code buffer. You can
619 then apply the guessed style to other buffers with @kbd{M-x
620 c-guess-install}. @xref{Guessing the Style,,, ccmode, the CC Mode
621 Manual}, for details.
624 @section Commands for Editing with Parentheses
627 @cindex unbalanced parentheses and quotes
628 This section describes the commands and features that take advantage
629 of the parenthesis structure in a program, or help you keep it
632 When talking about these facilities, the term ``parenthesis'' also
633 includes braces, brackets, or whatever delimiters are defined to match
634 in pairs. The major mode controls which delimiters are significant,
635 through the syntax table (@pxref{Syntax Tables,, Syntax Tables, elisp,
636 The Emacs Lisp Reference Manual}). In Lisp, only parentheses count;
637 in C, these commands apply to braces and brackets too.
639 You can use @kbd{M-x check-parens} to find any unbalanced
640 parentheses and unbalanced string quotes in the buffer.
643 * Expressions:: Expressions with balanced parentheses.
644 * Moving by Parens:: Commands for moving up, down and across
645 in the structure of parentheses.
646 * Matching:: Insertion of a close-delimiter flashes matching open.
650 @subsection Expressions with Balanced Parentheses
654 @cindex balanced expression
655 Each programming language mode has its own definition of a
656 @dfn{balanced expression}. Balanced expressions typically include
657 individual symbols, numbers, and string constants, as well as pieces
658 of code enclosed in a matching pair of delimiters. The following
659 commands deal with balanced expressions (in Emacs, such expressions
660 are referred to internally as @dfn{sexps}@footnote{The word ``sexp''
661 is used to refer to an expression in Lisp.}).
665 Move forward over a balanced expression (@code{forward-sexp}).
667 Move backward over a balanced expression (@code{backward-sexp}).
669 Kill balanced expression forward (@code{kill-sexp}).
671 Transpose expressions (@code{transpose-sexps}).
674 Put mark after following expression (@code{mark-sexp}).
680 @findex backward-sexp
681 To move forward over a balanced expression, use @kbd{C-M-f}
682 (@code{forward-sexp}). If the first significant character after point
683 is an opening delimiter (e.g., @samp{(}, @samp{[} or @samp{@{} in C),
684 this command moves past the matching closing delimiter. If the
685 character begins a symbol, string, or number, the command moves over
688 The command @kbd{C-M-b} (@code{backward-sexp}) moves backward over a
689 balanced expression---like @kbd{C-M-f}, but in the reverse direction.
690 If the expression is preceded by any prefix characters (single-quote,
691 backquote and comma, in Lisp), the command moves back over them as
694 @kbd{C-M-f} or @kbd{C-M-b} with an argument repeats that operation
695 the specified number of times; with a negative argument means to move
696 in the opposite direction. In most modes, these two commands move
697 across comments as if they were whitespace. Note that their keys,
698 @kbd{C-M-f} and @kbd{C-M-b}, are analogous to @kbd{C-f} and @kbd{C-b},
699 which move by characters (@pxref{Moving Point}), and @kbd{M-f} and
700 @kbd{M-b}, which move by words (@pxref{Words}).
702 @cindex killing expressions
705 To kill a whole balanced expression, type @kbd{C-M-k}
706 (@code{kill-sexp}). This kills the text that @kbd{C-M-f} would move
709 @cindex transposition of expressions
711 @findex transpose-sexps
712 @kbd{C-M-t} (@code{transpose-sexps}) switches the positions of the
713 previous balanced expression and the next one. It is analogous to the
714 @kbd{C-t} command, which transposes characters (@pxref{Transpose}).
715 An argument to @kbd{C-M-t} serves as a repeat count, moving the
716 previous expression over that many following ones. A negative
717 argument moves the previous balanced expression backwards across those
718 before it. An argument of zero, rather than doing nothing, transposes
719 the balanced expressions ending at or after point and the mark.
722 To operate on balanced expressions with a command which acts on the
723 region, type @kbd{C-M-@key{SPC}} (@code{mark-sexp}). This sets the
724 mark where @kbd{C-M-f} would move to. While the mark is active, each
725 successive call to this command extends the region by shifting the
726 mark by one expression. Positive or negative numeric arguments move
727 the mark forward or backward by the specified number of expressions.
728 The alias @kbd{C-M-@@} is equivalent to @kbd{C-M-@key{SPC}}.
729 @xref{Marking Objects}, for more information about this and related
732 In languages that use infix operators, such as C, it is not possible
733 to recognize all balanced expressions because there can be multiple
734 possibilities at a given position. For example, C mode does not treat
735 @samp{foo + bar} as a single expression, even though it @emph{is} one
736 C expression; instead, it recognizes @samp{foo} as one expression and
737 @samp{bar} as another, with the @samp{+} as punctuation between them.
738 However, C mode recognizes @samp{(foo + bar)} as a single expression,
739 because of the parentheses.
741 @node Moving by Parens
742 @subsection Moving in the Parenthesis Structure
744 @cindex parenthetical groupings
745 @cindex parentheses, moving across
746 @cindex matching parenthesis and braces, moving to
747 @cindex braces, moving across
748 @cindex list commands
750 The following commands move over groupings delimited by parentheses
751 (or whatever else serves as delimiters in the language you are working
752 with). They ignore strings and comments, including any parentheses
753 within them, and also ignore parentheses that are quoted with an
754 escape character. These commands are mainly intended for editing
755 programs, but can be useful for editing any text containing
756 parentheses. They are referred to internally as ``list commands''
757 because in Lisp these groupings are lists.
759 These commands assume that the starting point is not inside a string
760 or a comment. If you invoke them from inside a string or comment, the
761 results are unreliable.
765 Move forward over a parenthetical group (@code{forward-list}).
767 Move backward over a parenthetical group (@code{backward-list}).
769 Move up in parenthesis structure (@code{backward-up-list}).
771 Move down in parenthesis structure (@code{down-list}).
777 @findex backward-list
778 The list commands @kbd{C-M-n} (@code{forward-list}) and
779 @kbd{C-M-p} (@code{backward-list}) move forward or backward over one
780 (or @var{n}) parenthetical groupings.
783 @findex backward-up-list
784 @kbd{C-M-n} and @kbd{C-M-p} try to stay at the same level in the
785 parenthesis structure. To move @emph{up} one (or @var{n}) levels, use
786 @kbd{C-M-u} (@code{backward-up-list}). @kbd{C-M-u} moves backward up
787 past one unmatched opening delimiter. A positive argument serves as a
788 repeat count; a negative argument reverses the direction of motion, so
789 that the command moves forward and up one or more levels.
793 To move @emph{down} in the parenthesis structure, use @kbd{C-M-d}
794 (@code{down-list}). In Lisp mode, where @samp{(} is the only opening
795 delimiter, this is nearly the same as searching for a @samp{(}. An
796 argument specifies the number of levels to go down.
799 @subsection Matching Parentheses
800 @cindex matching parentheses
801 @cindex parentheses, displaying matches
803 Emacs has a number of @dfn{parenthesis matching} features, which
804 make it easy to see how and whether parentheses (or other delimiters)
807 Whenever you type a self-inserting character that is a closing
808 delimiter, Emacs briefly indicates the location of the matching
809 opening delimiter, provided that is on the screen. If it is not on
810 the screen, Emacs displays some of the text near it in the echo area.
811 Either way, you can tell which grouping you are closing off. If the
812 opening delimiter and closing delimiter are mismatched---such as in
813 @samp{[x)}---a warning message is displayed in the echo area.
815 @vindex blink-matching-paren
816 @vindex blink-matching-paren-distance
817 @vindex blink-matching-delay
818 Three variables control the display of matching parentheses:
822 @code{blink-matching-paren} turns the feature on or off: @code{nil}
823 disables it, but the default is @code{t} to enable it. Set it to
824 @code{jump} to make indication work by momentarily moving the cursor
825 to the matching opening delimiter. Set it to @code{jump-offscreen} to
826 make the cursor jump, even if the opening delimiter is off screen.
829 @code{blink-matching-delay} says how many seconds to keep indicating
830 the matching opening delimiter. This may be an integer or
831 floating-point number; the default is 1.
834 @code{blink-matching-paren-distance} specifies how many characters
835 back to search to find the matching opening delimiter. If the match
836 is not found in that distance, Emacs stops scanning and nothing is
837 displayed. The default is 102400.
840 @cindex Show Paren mode
841 @cindex highlighting matching parentheses
842 @findex show-paren-mode
843 Show Paren mode, a global minor mode, provides a more powerful kind
844 of automatic matching. Whenever point is before an opening delimiter
845 or after a closing delimiter, the delimiter, its matching delimiter,
846 and optionally the text between them are highlighted. To toggle Show
847 Paren mode, type @kbd{M-x show-paren-mode}. To customize it, type
848 @kbd{M-x customize-group @key{RET} paren-showing}. The customizable
849 options which control the operation of this mode include:
853 @code{show-paren-highlight-open-paren} controls whether to highlight
854 an open paren when point stands just before it, and hence its position
855 is marked by the cursor anyway. The default is non-@code{nil} (yes).
858 @code{show-paren-style} controls whether just the two parens, or also
859 the space between them get highlighted. The valid options here are
860 @code{parenthesis} (show the matching paren), @code{expression}
861 (highlight the entire expression enclosed by the parens), and
862 @code{mixed} (highlight the matching paren if it is visible, the
863 expression otherwise).
866 @code{show-paren-when-point-inside-paren}, when non-@code{nil}, causes
867 highlighting also when point is on the inside of a parenthesis.
870 @code{show-paren-when-point-in-periphery}, when non-@code{nil}, causes
871 highlighting also when point is in whitespace at the beginning or end
872 of a line, and there is a paren at, respectively, the first or last,
873 or the last, non-whitespace position on the line.
876 @cindex Electric Pair mode
877 @cindex inserting matching parentheses
878 @findex electric-pair-mode
879 Electric Pair mode, a global minor mode, provides a way to easily
880 insert matching delimiters: parentheses, braces, brackets, etc.
881 Whenever you insert an opening delimiter, the matching closing
882 delimiter is automatically inserted as well, leaving point between the
883 two. Conversely, when you insert a closing delimiter over an existing
884 one, no insertion takes places, and that position is simply skipped
885 over. If the region is active (@pxref{Mark}), insertion of a
886 delimiter operates on the region: the characters in the region are
887 enclosed in a pair of matching delimiters, leaving point after the
890 These variables control additional features of Electric Pair mode:
894 @vindex electric-pair-preserve-balance
895 @code{electric-pair-preserve-balance}, when non-@code{nil}, makes the
896 default pairing logic balance out the number of opening and closing
900 @vindex electric-pair-delete-adjacent-pairs
901 @code{electric-pair-delete-adjacent-pairs}, when non-@code{nil}, makes
902 backspacing between two adjacent delimiters also automatically delete
903 the closing delimiter.
906 @vindex electric-pair-open-newline-between-pairs
907 @code{electric-pair-open-newline-between-pairs}, when non-@code{nil},
908 makes inserting a newline between two adjacent pairs also
909 automatically open an extra newline after point.
912 @vindex electric-pair-skip-whitespace
913 @code{electric-pair-skip-whitespace}, when non-@code{nil}, causes the minor
914 mode to skip whitespace forward before deciding whether to skip over
915 the closing delimiter.
918 To toggle Electric Pair mode, type @kbd{M-x electric-pair-mode}. To
919 toggle the mode in a single buffer, use @kbd{M-x
920 electric-pair-local-mode}.
923 @section Manipulating Comments
926 Because comments are such an important part of programming, Emacs
927 provides special commands for editing and inserting comments. It can
928 also do spell checking on comments with Flyspell Prog mode
931 Some major modes have special rules for indenting different kinds of
932 comments. For example, in Lisp code, comments starting with two
933 semicolons are indented as if they were lines of code, while those
934 starting with three semicolons are supposed to be aligned to the left
935 margin and are often used for sectioning purposes. Emacs understands
936 these conventions; for instance, typing @kbd{@key{TAB}} on a comment
937 line will indent the comment to the appropriate position.
940 ;; This function is just an example.
941 ;;; Here either two or three semicolons are appropriate.
943 ;;; And now, the first part of the function:
944 ;; The following line adds one.
945 (1+ x)) ; This line adds one.
949 * Comment Commands:: Inserting, killing, and aligning comments.
950 * Multi-Line Comments:: Commands for adding and editing multi-line comments.
951 * Options for Comments::Customizing the comment features.
954 @node Comment Commands
955 @subsection Comment Commands
956 @cindex indentation for comments
957 @cindex alignment for comments
959 The following commands operate on comments:
963 Insert or realign comment on current line; if the region is active,
964 comment or uncomment the region instead (@code{comment-dwim}).
966 Comment or uncomment the current line (@code{comment-line}). If the
967 region is active, comment or uncomment the lines in the region
970 Kill comment on current line (@code{comment-kill}).
972 Set comment column (@code{comment-set-column}).
975 Like @kbd{@key{RET}} followed by inserting and aligning a comment
976 (@code{comment-indent-new-line}). @xref{Multi-Line Comments}.
977 @item @kbd{M-x comment-region}
978 @itemx @kbd{C-c C-c} (in C-like modes)
979 Add comment delimiters to all the lines in the region.
984 The command to create or align a comment is @kbd{M-;}
985 (@code{comment-dwim}). The word ``dwim'' is an acronym for ``Do What
986 I Mean''; it indicates that this command can be used for many
987 different jobs relating to comments, depending on the situation where
990 When a region is active (@pxref{Mark}), @kbd{M-;} either adds
991 comment delimiters to the region, or removes them. If every line in
992 the region is already a comment, it uncomments each of those lines
993 by removing their comment delimiters. Otherwise, it adds comment
994 delimiters to enclose the text in the region.
996 If you supply a prefix argument to @kbd{M-;} when a region is
997 active, that specifies the number of comment delimiters to add or
998 delete. A positive argument @var{n} adds @var{n} delimiters, while a
999 negative argument @var{-n} removes @var{n} delimiters.
1001 If the region is not active, and there is no existing comment on the
1002 current line, @kbd{M-;} adds a new comment to the current line. If
1003 the line is blank (i.e., empty or containing only whitespace
1004 characters), the comment is indented to the same position where
1005 @kbd{@key{TAB}} would indent to (@pxref{Basic Indent}). If the line
1006 is non-blank, the comment is placed after the last non-whitespace
1007 character on the line. Emacs tries to fit the comment between the
1008 columns specified by the variables @code{comment-column} and
1009 @code{comment-fill-column} (@pxref{Options for Comments}), if
1010 possible. Otherwise, it will choose some other suitable position,
1011 usually separated from the non-comment text by at least one space. In
1012 each case, Emacs places point after the comment's starting delimiter,
1013 so that you can start typing the comment text right away.
1015 You can also use @kbd{M-;} to align an existing comment. If a line
1016 already contains the comment-start string, @kbd{M-;} realigns it to
1017 the conventional alignment and moves point after the comment's
1018 starting delimiter. As an exception, comments starting in column 0
1019 are not moved. Even when an existing comment is properly aligned,
1020 @kbd{M-;} is still useful for moving directly to the start of the
1023 @findex comment-line
1025 @kbd{C-x C-;} (@code{comment-line}) comments or uncomments complete
1026 lines. When a region is active (@pxref{Mark}), @kbd{C-x C-;} either
1027 comments or uncomments the lines in the region. If the region is not
1028 active, this command comments or uncomments the line point is on.
1029 With a positive prefix argument @var{n}, it operates on @var{n} lines
1030 starting with the current one; with a negative @var{n}, it affects
1031 @var{n} preceding lines. After invoking this command with a negative
1032 argument, successive invocations with a positive argument will operate
1033 on preceding lines as if the argument were negated.
1035 @findex comment-kill
1037 @kbd{C-u M-;} (@code{comment-dwim} with a prefix argument) kills any
1038 comment on the current line, along with the whitespace before it.
1039 Since the comment is saved to the kill ring, you can reinsert it on
1040 another line by moving to the end of that line, doing @kbd{C-y}, and
1041 then @kbd{M-;} to realign the comment. You can achieve the same
1042 effect as @kbd{C-u M-;} by typing @kbd{M-x comment-kill}
1043 (@code{comment-dwim} actually calls @code{comment-kill} as a
1044 subroutine when it is given a prefix argument).
1046 @kindex C-c C-c (C mode)
1047 @findex comment-region
1048 @findex uncomment-region
1049 The command @kbd{M-x comment-region} is equivalent to calling
1050 @kbd{M-;} on an active region, except that it always acts on the
1051 region, even if the mark is inactive. In C mode and related modes,
1052 this command is bound to @kbd{C-c C-c}. The command @kbd{M-x
1053 uncomment-region} uncomments each line in the region; a numeric prefix
1054 argument specifies the number of comment delimiters to remove
1055 (negative arguments specify the number of comment delimiters to add).
1057 For C-like modes, you can configure the exact effect of @kbd{M-;} by
1058 setting the variables @code{c-indent-comment-alist} and
1059 @code{c-indent-comments-syntactically-p}. For example, on a line
1060 ending in a closing brace, @kbd{M-;} puts the comment one space after
1061 the brace rather than at @code{comment-column}. For full details see
1062 @ref{Comment Commands,,, ccmode, The CC Mode Manual}.
1064 @node Multi-Line Comments
1065 @subsection Multiple Lines of Comments
1069 @cindex blank lines in programs
1070 @findex comment-indent-new-line
1071 @vindex comment-multi-line
1072 If you are typing a comment and wish to continue it to another line,
1073 type @kbd{M-j} or @kbd{C-M-j} (@code{comment-indent-new-line}). This
1074 breaks the current line, and inserts the necessary comment delimiters
1075 and indentation to continue the comment.
1077 For languages with closing comment delimiters (e.g., @samp{*/} in
1078 C), the exact behavior of @kbd{M-j} depends on the value of the
1079 variable @code{comment-multi-line}. If the value is @code{nil}, the
1080 command closes the comment on the old line and starts a new comment on
1081 the new line. Otherwise, it opens a new line within the current
1084 When Auto Fill mode is on, going past the fill column while typing a
1085 comment also continues the comment, in the same way as an explicit
1086 invocation of @kbd{M-j}.
1088 To turn existing lines into comment lines, use @kbd{M-;} with the
1089 region active, or use @kbd{M-x comment-region}
1091 (@pxref{Comment Commands}).
1094 as described in the preceding section.
1097 You can configure C Mode such that when you type a @samp{/} at the
1098 start of a line in a multi-line block comment, this closes the
1099 comment. Enable the @code{comment-close-slash} clean-up for this.
1100 @xref{Clean-ups,,, ccmode, The CC Mode Manual}.
1102 @node Options for Comments
1103 @subsection Options Controlling Comments
1105 @vindex comment-column
1106 @vindex comment-fill-column
1108 @findex comment-set-column
1109 As mentioned in @ref{Comment Commands}, when the @kbd{M-j} command
1110 adds a comment to a line, it tries to place the comment between the
1111 columns specified by the buffer-local variables @code{comment-column}
1112 and @code{comment-fill-column} (or if that is @code{nil}, then the
1113 value of @code{fill-column}, @pxref{Fill Commands}). You can set
1114 either the local value or the default value of these buffer-local
1115 variables in the usual way (@pxref{Locals}). Alternatively, you can
1116 type @kbd{C-x ;} (@code{comment-set-column}) to set the value of
1117 @code{comment-column} in the current buffer to the column where point
1118 is currently located. @kbd{C-u C-x ;} sets the comment column to
1119 match the last comment before point in the buffer, and then does a
1120 @kbd{M-;} to align the current line's comment under the previous one.
1122 @vindex comment-start-skip
1123 The comment commands recognize comments based on the regular
1124 expression that is the value of the variable @code{comment-start-skip}.
1125 Make sure this regexp does not match the null string. It may match more
1126 than the comment starting delimiter in the strictest sense of the word;
1127 for example, in C mode the value of the variable is
1128 @c This stops M-q from breaking the line inside that @code.
1129 @code{@w{"\\(//+\\|/\\*+\\)\\s *"}}, which matches extra stars and
1130 spaces after the @samp{/*} itself, and accepts C++ style comments
1131 also. (Note that @samp{\\} is needed in Lisp syntax to include a
1132 @samp{\} in the string, which is needed to deny the first star its
1133 special meaning in regexp syntax. @xref{Regexp Backslash}.)
1135 @vindex comment-start
1137 When a comment command makes a new comment, it inserts the value of
1138 @code{comment-start} as an opening comment delimiter. It also inserts
1139 the value of @code{comment-end} after point, as a closing comment
1140 delimiter. For example, in Lisp mode, @code{comment-start} is
1141 @samp{";"} and @code{comment-end} is @code{""} (the empty string). In
1142 C mode, @code{comment-start} is @code{"/* "} and @code{comment-end} is
1145 @vindex comment-padding
1146 The variable @code{comment-padding} specifies a string that the
1147 commenting commands should insert between the comment delimiter(s) and
1148 the comment text. The default, @samp{" "}, specifies a single space.
1149 Alternatively, the value can be a number, which specifies that number
1150 of spaces, or @code{nil}, which means no spaces at all.
1152 The variable @code{comment-multi-line} controls how @kbd{M-j} and
1153 Auto Fill mode continue comments over multiple lines.
1154 @xref{Multi-Line Comments}.
1156 @vindex comment-indent-function
1157 The variable @code{comment-indent-function} should contain a function
1158 that will be called to compute the alignment for a newly inserted
1159 comment or for aligning an existing comment. It is set differently by
1160 various major modes. The function is called with no arguments, but with
1161 point at the beginning of the comment, or at the end of a line if a new
1162 comment is to be inserted. It should return the column in which the
1163 comment ought to start. For example, the default function bases its
1164 decision on how many comment characters begin an existing comment.
1166 Emacs also tries to align comments on adjacent lines. To override
1167 this, the function may return a cons of two (possibly equal) integers
1168 to indicate an acceptable range of indentation.
1171 @section Documentation Lookup
1173 Emacs provides several features you can use to look up the
1174 documentation of functions, variables and commands that you plan to
1175 use in your program.
1178 * Info Lookup:: Looking up library functions and commands in Info files.
1179 * Man Page:: Looking up man pages of library functions and commands.
1180 * Lisp Doc:: Looking up Emacs Lisp functions, etc.
1184 @subsection Info Documentation Lookup
1186 @findex info-lookup-file
1187 For major modes that apply to languages which have documentation in
1188 Info, you can use @kbd{C-h S} (@code{info-lookup-symbol}) to view the
1189 Info documentation for a symbol used in the program. You specify the
1190 symbol with the minibuffer; the default is the symbol appearing in the
1191 buffer at point. For example, in C mode this looks for the symbol in
1192 the C Library Manual. The command only works if the appropriate
1193 manual's Info files are installed.
1195 Emacs determines where to look for documentation for the
1196 symbol---which Info files to look in, and which indices to
1197 search---based on the major mode. You can also use @kbd{M-x
1198 info-lookup-file} to look for documentation for a file name.
1200 If you use @kbd{C-h S} in a major mode that does not support it,
1201 it asks you to specify the symbol help mode. You should enter
1202 a command such as @code{c-mode} that would select a major
1203 mode which @kbd{C-h S} does support.
1206 @subsection Man Page Lookup
1209 On Unix, the main form of on-line documentation was the @dfn{manual
1210 page} or @dfn{man page}. In the GNU operating system, we aim to
1211 replace man pages with better-organized manuals that you can browse
1212 with Info (@pxref{Misc Help}). This process is not finished, so it is
1213 still useful to read manual pages.
1216 You can read the man page for an operating system command, library
1217 function, or system call, with the @kbd{M-x man} command. This
1218 prompts for a topic, with completion (@pxref{Completion}), and runs
1219 the @command{man} program to format the corresponding man page. If
1220 the system permits, it runs @command{man} asynchronously, so that you
1221 can keep on editing while the page is being formatted. The result
1222 goes in a buffer named @file{*Man @var{topic}*}. These buffers use a
1223 special major mode, Man mode, that facilitates scrolling and jumping
1224 to other manual pages. For details, type @kbd{C-h m} while in a Man
1227 @cindex sections of manual pages
1228 Each man page belongs to one of ten or more @dfn{sections}, each
1229 named by a digit or by a digit and a letter. Sometimes there are man
1230 pages with the same name in different sections. To read a man page
1231 from a specific section, type @samp{@var{topic}(@var{section})} or
1232 @samp{@var{section} @var{topic}} when @kbd{M-x man} prompts for the
1233 topic. For example, the man page for the C library function
1234 @code{chmod} is in section 2, but there is a shell command of the same
1235 name, whose man page is in section 1; to view the former, type
1236 @w{@kbd{M-x man @key{RET} chmod(2) @key{RET}}}.
1238 @vindex Man-switches
1239 @kindex M-n @r{(Man mode)}
1240 @kindex M-p @r{(Man mode)}
1241 If you do not specify a section, @kbd{M-x man} normally displays
1242 only the first man page found. On some systems, the @code{man}
1243 program accepts a @samp{-a} command-line option, which tells it to
1244 display all the man pages for the specified topic. To make use of
1245 this, change the value of the variable @code{Man-switches} to
1246 @samp{"-a"}. Then, in the Man mode buffer, you can type @kbd{M-n} and
1247 @kbd{M-p} to switch between man pages in different sections. The mode
1248 line shows how many manual pages are available.
1251 @cindex manual pages, on MS-DOS/MS-Windows
1252 An alternative way of reading manual pages is the @kbd{M-x woman}
1253 command. Unlike @kbd{M-x man}, it does not run any external programs
1254 to format and display the man pages; the formatting is done by Emacs,
1255 so it works on systems such as MS-Windows where the @command{man}
1256 program may be unavailable. It prompts for a man page, and displays
1257 it in a buffer named @file{*WoMan @var{section} @var{topic}}.
1259 @kbd{M-x woman} computes the completion list for manpages the first
1260 time you invoke the command. With a numeric argument, it recomputes
1261 this list; this is useful if you add or delete manual pages.
1263 If you type a name of a manual page and @kbd{M-x woman} finds that
1264 several manual pages by the same name exist in different sections, it
1265 pops up a window with possible candidates asking you to choose one of
1268 Note that @kbd{M-x woman} doesn't yet support the latest features of
1269 modern man pages, so we recommend using @kbd{M-x man} if that is
1270 available on your system.
1272 For more information about setting up and using @kbd{M-x woman}, see
1274 @ref{Top, WoMan, Browse UN*X Manual Pages WithOut Man, woman, The
1278 the WoMan Info manual, which is distributed with Emacs.
1282 @subsection Emacs Lisp Documentation Lookup
1284 When editing Emacs Lisp code, you can use the commands @kbd{C-h f}
1285 (@code{describe-function}) and @kbd{C-h v} (@code{describe-variable})
1286 to view the built-in documentation for the Lisp functions and
1287 variables that you want to use. @xref{Name Help}.
1291 @findex global-eldoc-mode
1292 Eldoc is a buffer-local minor mode that helps with looking up Lisp
1293 documentation. When it is enabled, the echo area displays some useful
1294 information whenever there is a Lisp function or variable at point;
1295 for a function, it shows the argument list, and for a variable it
1296 shows the first line of the variable's documentation string. To
1297 toggle Eldoc mode, type @kbd{M-x eldoc-mode}. There's also a Global
1298 Eldoc mode, which is turned on by default, and affects buffers, such
1299 as @samp{*scratch*}, whose major mode is Emacs Lisp or Lisp
1300 Interaction (@w{@kbd{M-x global-eldoc-mode}} to turn it off globally).
1303 @section Hideshow minor mode
1304 @cindex Hideshow mode
1305 @cindex mode, Hideshow
1307 @findex hs-minor-mode
1308 Hideshow mode is a buffer-local minor mode that allows you to
1309 selectively display portions of a program, which are referred to as
1310 @dfn{blocks}. Type @kbd{M-x hs-minor-mode} to toggle this minor mode
1311 (@pxref{Minor Modes}).
1313 When you use Hideshow mode to hide a block, the block disappears
1314 from the screen, to be replaced by an ellipsis (three periods in a
1315 row). Just what constitutes a block depends on the major mode. In C
1316 mode and related modes, blocks are delimited by braces, while in Lisp
1317 mode they are delimited by parentheses. Multi-line comments also
1320 Hideshow mode provides the following commands:
1323 @findex hs-hide-block
1325 @findex hs-show-block
1326 @findex hs-show-region
1327 @findex hs-hide-level
1330 @kindex C-c @@ C-M-h
1331 @kindex C-c @@ C-M-s
1338 Hide the current block (@code{hs-hide-block}).
1340 Show the current block (@code{hs-show-block}).
1343 Either hide or show the current block (@code{hs-toggle-hiding}).
1345 Toggle hiding for the block you click on (@code{hs-mouse-toggle-hiding}).
1348 Hide all top-level blocks (@code{hs-hide-all}).
1351 Show all blocks in the buffer (@code{hs-show-all}).
1352 @item C-u @var{n} C-c @@ C-l
1353 Hide all blocks @var{n} levels below this block
1354 (@code{hs-hide-level}).
1357 @vindex hs-hide-comments-when-hiding-all
1358 @vindex hs-isearch-open
1359 @vindex hs-special-modes-alist
1360 These variables can be used to customize Hideshow mode:
1363 @item hs-hide-comments-when-hiding-all
1364 If non-@code{nil}, @kbd{C-c @@ C-M-h} (@code{hs-hide-all}) hides
1367 @item hs-isearch-open
1368 This variable specifies the conditions under which incremental search
1369 should unhide a hidden block when matching text occurs within the
1370 block. Its value should be either @code{code} (unhide only code
1371 blocks), @code{comment} (unhide only comments), @code{t} (unhide both
1372 code blocks and comments), or @code{nil} (unhide neither code blocks
1373 nor comments). The default value is @code{code}.
1376 @node Symbol Completion
1377 @section Completion for Symbol Names
1378 @cindex completion (symbol names)
1380 Completion is normally done in the minibuffer (@pxref{Completion}),
1381 but you can also complete symbol names in ordinary Emacs buffers.
1385 In programming language modes, type @kbd{C-M-i} or @kbd{M-@key{TAB}}
1386 to complete the partial symbol before point. On graphical displays,
1387 the @kbd{M-@key{TAB}} key is usually reserved by the window manager
1388 for switching graphical windows, so you should type @kbd{C-M-i} or
1389 @kbd{@key{ESC} @key{TAB}} instead.
1391 @cindex tags-based completion
1392 @findex completion-at-point@r{, in programming language modes}
1393 @cindex Lisp symbol completion
1394 @cindex completion (Lisp symbols)
1395 In most programming language modes, @kbd{C-M-i} (or
1396 @kbd{M-@key{TAB}}) invokes the command @code{completion-at-point},
1397 which generates its completion list in a flexible way. If Semantic
1398 mode is enabled, it tries to use the Semantic parser data for
1399 completion (@pxref{Semantic}). If Semantic mode is not enabled or
1400 fails at performing completion, it tries to complete using the
1401 selected tags table (@pxref{Tags Tables}). If in Emacs Lisp mode, it
1402 performs completion using the function, variable, or property names
1403 defined in the current Emacs session.
1405 In all other respects, in-buffer symbol completion behaves like
1406 minibuffer completion. For instance, if Emacs cannot complete to a
1407 unique symbol, it displays a list of completion alternatives in
1408 another window. @xref{Completion}.
1410 In Text mode and related modes, @kbd{M-@key{TAB}} completes words
1411 based on the spell-checker's dictionary. @xref{Spelling}.
1413 @node MixedCase Words
1414 @section MixedCase Words
1417 Some programming styles make use of mixed-case (or ``CamelCase'')
1418 symbols like @samp{unReadableSymbol}. (In the GNU project, we recommend
1419 using underscores to separate words within an identifier, rather than
1420 using case distinctions.) Emacs has various features to make it easier
1421 to deal with such symbols.
1423 @cindex Glasses mode
1424 @cindex mode, Glasses
1425 Glasses mode is a buffer-local minor mode that makes it easier to read
1426 such symbols, by altering how they are displayed. By default, it
1427 displays extra underscores between each lower-case letter and the
1428 following capital letter. This does not alter the buffer text, only how
1431 To toggle Glasses mode, type @kbd{M-x glasses-mode} (@pxref{Minor
1432 Modes}). When Glasses mode is enabled, the minor mode indicator
1433 @samp{o^o} appears in the mode line. For more information about
1434 Glasses mode, type @kbd{C-h P glasses @key{RET}}.
1436 @cindex Subword mode
1437 @findex subword-mode
1438 Subword mode is another buffer-local minor mode. In subword mode,
1439 Emacs's word commands recognize upper case letters in
1440 @samp{StudlyCapsIdentifiers} as word boundaries. When Subword mode is
1441 enabled, the minor mode indicator @samp{,} appears in the mode line.
1442 See also the similar @code{superword-mode} (@pxref{Misc for Programs}).
1446 @cindex Semantic package
1448 Semantic is a package that provides language-aware editing commands
1449 based on @code{source code parsers}. This section provides a brief
1450 description of Semantic; for full details,
1452 see @ref{Top, Semantic,, semantic, Semantic}.
1455 see the Semantic Info manual, which is distributed with Emacs.
1458 Most of the language-aware features in Emacs, such as Font Lock
1459 mode (@pxref{Font Lock}), rely on rules of thumb@footnote{Regular
1460 expressions and syntax tables.} that usually give good results but are
1461 never completely exact. In contrast, the parsers used by Semantic
1462 have an exact understanding of programming language syntax. This
1463 allows Semantic to provide search, navigation, and completion commands
1464 that are powerful and precise.
1466 @cindex Semantic mode
1467 @cindex mode, Semantic
1468 To begin using Semantic, type @kbd{M-x semantic-mode} or click on
1469 the menu item named @samp{Source Code Parsers (Semantic)} in the
1470 @samp{Tools} menu. This enables Semantic mode, a global minor mode.
1472 When Semantic mode is enabled, Emacs automatically attempts to
1473 parse each file you visit. Currently, Semantic understands C, C++,
1474 HTML, Java, Javascript, Make, Python, Scheme, SRecode, and Texinfo.
1475 Within each parsed buffer, the following commands are available:
1480 Prompt for the name of a function defined in the current file, and
1481 move point there (@code{semantic-complete-jump-local}).
1485 Prompt for the name of a function defined in any file Emacs has
1486 parsed, and move point there (@code{semantic-complete-jump}).
1488 @item C-c , @key{SPC}
1490 Display a list of possible completions for the symbol at point
1491 (@code{semantic-complete-analyze-inline}). This also activates a set
1492 of special key bindings for choosing a completion: @kbd{@key{RET}}
1493 accepts the current completion, @kbd{M-n} and @kbd{M-p} cycle through
1494 possible completions, @kbd{@key{TAB}} completes as far as possible and
1495 then cycles, and @kbd{C-g} or any other key aborts completion.
1499 Display a list of the possible completions of the symbol at point, in
1500 another window (@code{semantic-analyze-possible-completions}).
1504 In addition to the above commands, the Semantic package provides a
1505 variety of other ways to make use of parser information. For
1506 instance, you can use it to display a list of completions when Emacs
1509 @xref{Top, Semantic,, semantic, Semantic}, for details.
1512 @node Misc for Programs
1513 @section Other Features Useful for Editing Programs
1515 Some Emacs commands that aren't designed specifically for editing
1516 programs are useful for that nonetheless.
1518 The Emacs commands that operate on words, sentences and paragraphs
1519 are useful for editing code. Most symbol names contain words
1520 (@pxref{Words}), while sentences can be found in strings and comments
1521 (@pxref{Sentences}). As for paragraphs, they are defined in most
1522 programming language modes to begin and end at blank lines
1523 (@pxref{Paragraphs}). Therefore, judicious use of blank lines to make
1524 the program clearer will also provide useful chunks of text for the
1525 paragraph commands to work on. Auto Fill mode, if enabled in a
1526 programming language major mode, indents the new lines which it
1529 @findex superword-mode
1530 Superword mode is a buffer-local minor mode that causes editing and
1531 motion commands to treat symbols (e.g., @samp{this_is_a_symbol}) as words.
1532 When Superword mode is enabled, the minor mode indicator
1539 appears in the mode line. See also the similar @code{subword-mode}
1540 (@pxref{MixedCase Words}).
1542 @findex electric-layout-mode
1543 Electric Layout mode (@kbd{M-x electric-layout-mode}) is a global
1544 minor mode that automatically inserts newlines when you type certain
1545 characters; for example, @samp{@{}, @samp{@}} and @samp{;} in Javascript
1548 Apart from Hideshow mode (@pxref{Hideshow}), another way to
1549 selectively display parts of a program is to use the selective display
1550 feature (@pxref{Selective Display}). Programming modes often also
1551 support Outline minor mode (@pxref{Outline Mode}), which can be used
1552 with the Foldout package (@pxref{Foldout}).
1555 The automatic typing features may be useful for writing programs.
1556 @xref{Top,,Autotyping, autotype, Autotyping}.
1559 @findex prettify-symbols-mode
1560 Prettify Symbols mode is a buffer-local minor mode that replaces
1561 certain strings with more attractive versions for display purposes.
1562 For example, in Emacs Lisp mode, it replaces the string @samp{lambda}
1563 with the Greek lambda character @samp{λ}. In a @TeX{} buffer, it will
1564 replace @samp{\alpha} @dots{} @samp{\omega} and other math macros with
1565 their Unicode characters. You may wish to use this in non-programming
1566 modes as well. You can customize the mode by adding more entries to
1567 @code{prettify-symbols-alist}. More elaborate customization is
1568 available via customizing @code{prettify-symbols-compose-predicate} if
1569 its default value @code{prettify-symbols-default-compose-p} is not
1570 appropriate. There is also a global version,
1571 @code{global-prettify-symbols-mode}, which enables the mode in all
1572 buffers that support it.
1574 The symbol at point can be shown in its original form. This is
1575 controlled by the variable @code{prettify-symbols-unprettify-at-point}:
1576 if non-@code{nil}, the original form of symbol at point will be
1577 restored for as long as point is at it.
1581 @section C and Related Modes
1586 @cindex CORBA IDL mode
1587 @cindex Objective C mode
1593 @cindex mode, Objective C
1594 @cindex mode, CORBA IDL
1598 This section gives a brief description of the special features
1599 available in C, C++, Objective-C, Java, CORBA IDL, Pike and AWK modes.
1600 (These are called ``C mode and related modes''.)
1602 @xref{Top,, CC Mode, ccmode, CC Mode}, for more details.
1605 For more details, see the CC mode Info manual, which is distributed
1610 * Motion in C:: Commands to move by C statements, etc.
1611 * Electric C:: Colon and other chars can automatically reindent.
1612 * Hungry Delete:: A more powerful DEL command.
1613 * Other C Commands:: Filling comments, viewing expansion of macros,
1614 and other neat features.
1618 @subsection C Mode Motion Commands
1620 This section describes commands for moving point, in C mode and
1626 @findex c-beginning-of-defun
1627 @findex c-end-of-defun
1628 Move point to the beginning or end of the current function or
1629 top-level definition. In languages with enclosing scopes (such as
1630 C++'s classes) the @dfn{current function} is the immediate one,
1631 possibly inside a scope. Otherwise it is the one defined by the least
1632 enclosing braces. (By contrast, @code{beginning-of-defun} and
1633 @code{end-of-defun} search for braces in column zero.) @xref{Moving
1637 @kindex C-c C-u @r{(C mode)}
1638 @findex c-up-conditional
1639 Move point back to the containing preprocessor conditional, leaving the
1640 mark behind. A prefix argument acts as a repeat count. With a negative
1641 argument, move point forward to the end of the containing
1642 preprocessor conditional.
1644 @samp{#elif} is equivalent to @samp{#else} followed by @samp{#if}, so
1645 the function will stop at a @samp{#elif} when going backward, but not
1649 @kindex C-c C-p @r{(C mode)}
1650 @findex c-backward-conditional
1651 Move point back over a preprocessor conditional, leaving the mark
1652 behind. A prefix argument acts as a repeat count. With a negative
1653 argument, move forward.
1656 @kindex C-c C-n @r{(C mode)}
1657 @findex c-forward-conditional
1658 Move point forward across a preprocessor conditional, leaving the mark
1659 behind. A prefix argument acts as a repeat count. With a negative
1660 argument, move backward.
1663 @kindex M-a (C mode)
1664 @findex c-beginning-of-statement
1665 Move point to the beginning of the innermost C statement
1666 (@code{c-beginning-of-statement}). If point is already at the beginning
1667 of a statement, move to the beginning of the preceding statement. With
1668 prefix argument @var{n}, move back @var{n} @minus{} 1 statements.
1670 In comments or in strings which span more than one line, this command
1671 moves by sentences instead of statements.
1674 @kindex M-e (C mode)
1675 @findex c-end-of-statement
1676 Move point to the end of the innermost C statement or sentence; like
1677 @kbd{M-a} except that it moves in the other direction
1678 (@code{c-end-of-statement}).
1682 @subsection Electric C Characters
1684 In C mode and related modes, certain printing characters are
1685 @dfn{electric}---in addition to inserting themselves, they also
1686 reindent the current line, and optionally also insert newlines. The
1687 electric characters are @kbd{@{}, @kbd{@}}, @kbd{:}, @kbd{#},
1688 @kbd{;}, @kbd{,}, @kbd{<}, @kbd{>}, @kbd{/}, @kbd{*}, @kbd{(}, and
1691 You might find electric indentation inconvenient if you are editing
1692 chaotically indented code. If you are new to CC Mode, you might find
1693 it disconcerting. You can toggle electric action with the command
1694 @kbd{C-c C-l}; when it is enabled, @samp{/@var{c}l} appears in the
1695 mode line after the mode name (where @var{c}, if present, is @samp{*}
1700 @kindex C-c C-l @r{(C mode)}
1701 @findex c-toggle-electric-state
1702 Toggle electric action (@code{c-toggle-electric-state}). With a
1703 positive prefix argument, this command enables electric action, with a
1704 negative one it disables it.
1707 Electric characters insert newlines only when, in addition to the
1708 electric state, the @dfn{auto-newline} feature is enabled (indicated
1709 by @samp{/@var{c}la} in the mode line after the mode name). You can
1710 turn this feature on or off with the command @kbd{C-c C-a}:
1714 @kindex C-c C-a @r{(C mode)}
1715 @findex c-toggle-auto-newline
1716 Toggle the auto-newline feature (@code{c-toggle-auto-newline}). With a
1717 prefix argument, this command turns the auto-newline feature on if the
1718 argument is positive, and off if it is negative.
1721 Usually the CC Mode style configures the exact circumstances in
1722 which Emacs inserts auto-newlines. You can also configure this
1723 directly. @xref{Custom Auto-newlines,,, ccmode, The CC Mode Manual}.
1726 @subsection Hungry Delete Feature in C
1727 @cindex hungry deletion (C Mode)
1729 If you want to delete an entire block of whitespace at point, you
1730 can use @dfn{hungry deletion}. This deletes all the contiguous
1731 whitespace either before point or after point in a single operation.
1732 @dfn{Whitespace} here includes tabs and newlines, but not comments or
1733 preprocessor commands.
1736 @item C-c C-@key{DEL}
1737 @itemx C-c @key{DEL}
1738 @findex c-hungry-delete-backwards
1739 @kindex C-c C-DEL (C Mode)
1740 @kindex C-c DEL (C Mode)
1741 Delete the entire block of whitespace preceding point (@code{c-hungry-delete-backwards}).
1744 @itemx C-c C-@key{Delete}
1745 @itemx C-c @key{Delete}
1746 @findex c-hungry-delete-forward
1747 @kindex C-c C-d (C Mode)
1748 @kindex C-c C-Delete (C Mode)
1749 @kindex C-c Delete (C Mode)
1750 Delete the entire block of whitespace after point (@code{c-hungry-delete-forward}).
1753 As an alternative to the above commands, you can enable @dfn{hungry
1754 delete mode}. When this feature is enabled (indicated by @samp{h}
1755 after a @samp{/} in the mode line after the mode name), a single
1756 @kbd{@key{DEL}} deletes all preceding whitespace, not just one space,
1757 and a single @kbd{C-d} (but @emph{not} plain @kbd{@key{Delete}})
1758 deletes all following whitespace.
1761 @item M-x c-toggle-hungry-state
1762 @findex c-toggle-hungry-state
1763 Toggle the hungry-delete feature
1764 (@code{c-toggle-hungry-state}). With a prefix argument,
1765 this command turns the hungry-delete feature on if the argument is
1766 positive, and off if it is negative.
1769 @vindex c-hungry-delete-key
1770 The variable @code{c-hungry-delete-key} controls whether the
1771 hungry-delete feature is enabled.
1773 @node Other C Commands
1774 @subsection Other Commands for C Mode
1777 @item M-x c-context-line-break
1778 @findex c-context-line-break
1779 This command inserts a line break and indents the new line in a manner
1780 appropriate to the context. In normal code, it does the work of
1781 @kbd{@key{RET}} (@code{newline}), in a C preprocessor line it additionally
1782 inserts a @samp{\} at the line break, and within comments it's like
1783 @kbd{M-j} (@code{c-indent-new-comment-line}).
1785 @code{c-context-line-break} isn't bound to a key by default, but it
1786 needs a binding to be useful. The following code will bind it to
1787 @kbd{@key{RET}}. We use @code{c-initialization-hook} here to make
1788 sure the keymap is loaded before we try to change it.
1791 (defun my-bind-clb ()
1792 (define-key c-mode-base-map "\C-m"
1793 'c-context-line-break))
1794 (add-hook 'c-initialization-hook 'my-bind-clb)
1798 Put mark at the end of a function definition, and put point at the
1799 beginning (@code{c-mark-function}).
1802 @kindex M-q @r{(C mode)}
1803 @findex c-fill-paragraph
1804 Fill a paragraph, handling C and C++ comments (@code{c-fill-paragraph}).
1805 If any part of the current line is a comment or within a comment, this
1806 command fills the comment or the paragraph of it that point is in,
1807 preserving the comment indentation and comment delimiters.
1810 @cindex macro expansion in C
1811 @cindex expansion of C macros
1812 @findex c-macro-expand
1813 @kindex C-c C-e @r{(C mode)}
1814 Run the C preprocessor on the text in the region, and show the result,
1815 which includes the expansion of all the macro calls
1816 (@code{c-macro-expand}). The buffer text before the region is also
1817 included in preprocessing, for the sake of macros defined there, but the
1818 output from this part isn't shown.
1820 When you are debugging C code that uses macros, sometimes it is hard to
1821 figure out precisely how the macros expand. With this command, you
1822 don't have to figure it out; you can see the expansions.
1825 @findex c-backslash-region
1826 @kindex C-c C-\ @r{(C mode)}
1827 Insert or align @samp{\} characters at the ends of the lines of the
1828 region (@code{c-backslash-region}). This is useful after writing or
1829 editing a C macro definition.
1831 If a line already ends in @samp{\}, this command adjusts the amount of
1832 whitespace before it. Otherwise, it inserts a new @samp{\}. However,
1833 the last line in the region is treated specially; no @samp{\} is
1834 inserted on that line, and any @samp{\} there is deleted.
1836 @item M-x cpp-highlight-buffer
1837 @cindex preprocessor highlighting
1838 @findex cpp-highlight-buffer
1839 Highlight parts of the text according to its preprocessor conditionals.
1840 This command displays another buffer named @file{*CPP Edit*}, which
1841 serves as a graphic menu for selecting how to display particular kinds
1842 of conditionals and their contents. After changing various settings,
1843 click on @samp{[A]pply these settings} (or go to that buffer and type
1844 @kbd{a}) to rehighlight the C mode buffer accordingly.
1847 @findex c-show-syntactic-information
1848 @kindex C-c C-s @r{(C mode)}
1849 Display the syntactic information about the current source line
1850 (@code{c-show-syntactic-information}). This information directs how
1851 the line is indented.
1853 @item M-x cwarn-mode
1854 @itemx M-x global-cwarn-mode
1856 @findex global-cwarn-mode
1857 @vindex global-cwarn-mode
1859 @cindex suspicious constructions in C, C++
1860 CWarn minor mode highlights certain suspicious C and C++ constructions:
1864 Assignments inside expressions.
1866 Semicolon following immediately after @samp{if}, @samp{for}, and @samp{while}
1867 (except after a @samp{do @dots{} while} statement);
1869 C++ functions with reference parameters.
1873 You can enable the mode for one buffer with the command @kbd{M-x
1874 cwarn-mode}, or for all suitable buffers with the command @kbd{M-x
1875 global-cwarn-mode} or by customizing the variable
1876 @code{global-cwarn-mode}. You must also enable Font Lock mode to make
1879 @item M-x hide-ifdef-mode
1880 @findex hide-ifdef-mode
1881 @cindex Hide-ifdef mode
1882 @vindex hide-ifdef-shadow
1883 Hide-ifdef minor mode hides selected code within @samp{#if} and
1884 @samp{#ifdef} preprocessor blocks. If you change the variable
1885 @code{hide-ifdef-shadow} to @code{t}, Hide-ifdef minor mode
1886 shadows preprocessor blocks by displaying them with a less
1887 prominent face, instead of hiding them entirely. See the
1888 documentation string of @code{hide-ifdef-mode} for more information.
1890 @item M-x ff-find-related-file
1891 @cindex related files
1892 @findex ff-find-related-file
1893 @vindex ff-related-file-alist
1894 Find a file related in a special way to the file visited by the
1895 current buffer. Typically this will be the header file corresponding
1896 to a C/C++ source file, or vice versa. The variable
1897 @code{ff-related-file-alist} specifies how to compute related file
1905 @cindex assembler mode
1906 Asm mode is a major mode for editing files of assembler code. It
1907 defines these commands:
1911 @code{tab-to-tab-stop}.
1912 @c FIXME: Maybe this should be consistent with other programming modes.
1915 Insert a newline and then indent using @code{tab-to-tab-stop}.
1918 Insert a colon and then remove the indentation from before the label
1919 preceding colon. Then do @code{tab-to-tab-stop}.
1922 Insert or align a comment.
1925 The variable @code{asm-comment-char} specifies which character
1926 starts comments in assembler syntax.
1929 @include fortran-xtra.texi