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