]> code.delx.au - gnu-emacs/blob - man/fortran-xtra.texi
(Fortran, Fortran Autofill)
[gnu-emacs] / man / fortran-xtra.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
3 @c See file emacs.texi for copying conditions.
4 @c
5 @c This file is included either in emacs-xtra.texi (when producing the
6 @c printed version) or in the main Emacs manual (for the on-line version).
7 @node Fortran
8 @section Fortran Mode
9 @cindex Fortran mode
10 @cindex mode, Fortran
11
12 Fortran mode provides special motion commands for Fortran statements
13 and subprograms, and indentation commands that understand Fortran
14 conventions of nesting, line numbers and continuation statements.
15 Fortran mode has support for Auto Fill mode that breaks long lines into
16 proper Fortran continuation lines.
17
18 Special commands for comments are provided because Fortran comments
19 are unlike those of other languages. Built-in abbrevs optionally save
20 typing when you insert Fortran keywords.
21
22 Use @kbd{M-x fortran-mode} to switch to this major mode. This
23 command runs the hook @code{fortran-mode-hook}.
24 @iftex
25 @xref{Hooks,,, emacs, the Emacs Manual}.
26 @end iftex
27 @ifnottex
28 @xref{Hooks}.
29 @end ifnottex
30
31 @cindex Fortran77 and Fortran90
32 @findex f90-mode
33 @findex fortran-mode
34 Fortran mode is meant for editing Fortran77 ``fixed format'' (and also
35 ``tab format'') source code. For editing the modern Fortran90 or
36 Fortran95 ``free format'' source code, use F90 mode (@code{f90-mode}).
37 Emacs normally uses Fortran mode for files with extension @samp{.f},
38 @samp{.F} or @samp{.for}, and F90 mode for the extension @samp{.f90} and
39 @samp{.f95}. GNU Fortran supports both kinds of format.
40
41 @menu
42 * Motion: Fortran Motion. Moving point by statements or subprograms.
43 * Indent: Fortran Indent. Indentation commands for Fortran.
44 * Comments: Fortran Comments. Inserting and aligning comments.
45 * Autofill: Fortran Autofill. Auto fill support for Fortran.
46 * Columns: Fortran Columns. Measuring columns for valid Fortran.
47 * Abbrev: Fortran Abbrev. Built-in abbrevs for Fortran keywords.
48 @end menu
49
50 @node Fortran Motion
51 @subsection Motion Commands
52
53 In addition to the normal commands for moving by and operating on
54 ``defuns'' (Fortran subprograms---functions and subroutines, as well as
55 modules for F90 mode), Fortran mode provides special commands to move by
56 statements and other program units.
57
58 @table @kbd
59 @kindex C-c C-n @r{(Fortran mode)}
60 @findex fortran-next-statement
61 @findex f90-next-statement
62 @item C-c C-n
63 Move to the beginning of the next statement
64 (@code{fortran-next-statement}/@code{f90-next-statement}).
65
66 @kindex C-c C-p @r{(Fortran mode)}
67 @findex fortran-previous-statement
68 @findex f90-previous-statement
69 @item C-c C-p
70 Move to the beginning of the previous statement
71 (@code{fortran-previous-statement}/@code{f90-previous-statement}).
72 If there is no previous statement (i.e. if called from the first
73 statement in the buffer), move to the start of the buffer.
74
75 @kindex C-c C-e @r{(F90 mode)}
76 @findex f90-next-block
77 @item C-c C-e
78 Move point forward to the start of the next code block
79 (@code{f90-next-block}). A code block is a subroutine,
80 @code{if}--@code{endif} statement, and so forth. This command exists
81 for F90 mode only, not Fortran mode. With a numeric argument, this
82 moves forward that many blocks.
83
84 @kindex C-c C-a @r{(F90 mode)}
85 @findex f90-previous-block
86 @item C-c C-a
87 Move point backward to the previous code block
88 (@code{f90-previous-block}). This is like @code{f90-next-block}, but
89 moves backwards.
90
91 @kindex C-M-n @r{(Fortran mode)}
92 @findex fortran-end-of-block
93 @findex f90-end-of-block
94 @item C-M-n
95 Move to the end of the current code block
96 (@code{fortran-end-of-block}/@code{f90-end-of-block}). With a numeric
97 agument, move forward that number of blocks. The mark is set before
98 moving point. The F90 mode version of this command checks for
99 consistency of block types and labels (if present), but it does not
100 check the outermost block since that may be incomplete.
101
102 @kindex C-M-p @r{(Fortran mode)}
103 @findex fortran-beginning-of-block
104 @findex f90-beginning-of-block
105 @item C-M-p
106 Move to the start of the current code block
107 (@code{fortran-beginning-of-block}/@code{f90-beginning-of-block}). This
108 is like @code{fortran-end-of-block}, but moves backwards.
109 @end table
110
111 @node Fortran Indent
112 @subsection Fortran Indentation
113
114 Special commands and features are needed for indenting Fortran code in
115 order to make sure various syntactic entities (line numbers, comment line
116 indicators and continuation line flags) appear in the columns that are
117 required for standard, fixed (or tab) format Fortran.
118
119 @menu
120 * Commands: ForIndent Commands. Commands for indenting and filling Fortran.
121 * Contline: ForIndent Cont. How continuation lines indent.
122 * Numbers: ForIndent Num. How line numbers auto-indent.
123 * Conv: ForIndent Conv. Conventions you must obey to avoid trouble.
124 * Vars: ForIndent Vars. Variables controlling Fortran indent style.
125 @end menu
126
127 @node ForIndent Commands
128 @subsubsection Fortran Indentation and Filling Commands
129
130 @table @kbd
131 @item C-M-j
132 Break the current line at point and set up a continuation line
133 (@code{fortran-split-line}).
134 @item M-^
135 Join this line to the previous line (@code{fortran-join-line}).
136 @item C-M-q
137 Indent all the lines of the subprogram point is in
138 (@code{fortran-indent-subprogram}).
139 @item M-q
140 Fill a comment block or statement.
141 @end table
142
143 @kindex C-M-q @r{(Fortran mode)}
144 @findex fortran-indent-subprogram
145 The key @kbd{C-M-q} runs @code{fortran-indent-subprogram}, a command
146 to reindent all the lines of the Fortran subprogram (function or
147 subroutine) containing point.
148
149 @kindex C-M-j @r{(Fortran mode)}
150 @findex fortran-split-line
151 The key @kbd{C-M-j} runs @code{fortran-split-line}, which splits
152 a line in the appropriate fashion for Fortran. In a non-comment line,
153 the second half becomes a continuation line and is indented
154 accordingly. In a comment line, both halves become separate comment
155 lines.
156
157 @kindex M-^ @r{(Fortran mode)}
158 @kindex C-c C-d @r{(Fortran mode)}
159 @findex fortran-join-line
160 @kbd{M-^} or @kbd{C-c C-d} runs the command @code{fortran-join-line},
161 which joins a continuation line back to the previous line, roughly as
162 the inverse of @code{fortran-split-line}. The point must be on a
163 continuation line when this command is invoked.
164
165 @kindex M-q @r{(Fortran mode)}
166 @kbd{M-q} in Fortran mode fills the comment block or statement that
167 point is in. This removes any excess statement continuations.
168
169 @node ForIndent Cont
170 @subsubsection Continuation Lines
171 @cindex Fortran continuation lines
172
173 @vindex fortran-continuation-string
174 Most Fortran77 compilers allow two ways of writing continuation lines.
175 If the first non-space character on a line is in column 5, then that
176 line is a continuation of the previous line. We call this @dfn{fixed
177 format}. (In GNU Emacs we always count columns from 0; but note that
178 the Fortran standard counts from 1.) The variable
179 @code{fortran-continuation-string} specifies what character to put in
180 column 5. A line that starts with a tab character followed by any digit
181 except @samp{0} is also a continuation line. We call this style of
182 continuation @dfn{tab format}. (Fortran90 introduced ``free format'',
183 with another style of continuation lines).
184
185 @vindex indent-tabs-mode @r{(Fortran mode)}
186 @vindex fortran-analyze-depth
187 @vindex fortran-tab-mode-default
188 Fortran mode can use either style of continuation line. When you
189 enter Fortran mode, it tries to deduce the proper continuation style
190 automatically from the buffer contents. It does this by scanning up to
191 @code{fortran-analyze-depth} (default 100) lines from the start of the
192 buffer. The first line that begins with either a tab character or six
193 spaces determines the choice. If the scan fails (for example, if the
194 buffer is new and therefore empty), the value of
195 @code{fortran-tab-mode-default} (@code{nil} for fixed format, and
196 non-@code{nil} for tab format) is used. @samp{/t} in the mode line
197 indicates tab format is selected. Fortran mode sets the value of
198 @code{indent-tabs-mode} accordingly.
199
200 If the text on a line starts with the Fortran continuation marker
201 @samp{$}, or if it begins with any non-whitespace character in column
202 5, Fortran mode treats it as a continuation line. When you indent a
203 continuation line with @key{TAB}, it converts the line to the current
204 continuation style. When you split a Fortran statement with
205 @kbd{C-M-j}, the continuation marker on the newline is created according
206 to the continuation style.
207
208 The setting of continuation style affects several other aspects of
209 editing in Fortran mode. In fixed format mode, the minimum column
210 number for the body of a statement is 6. Lines inside of Fortran
211 blocks that are indented to larger column numbers always use only the
212 space character for whitespace. In tab format mode, the minimum
213 column number for the statement body is 8, and the whitespace before
214 column 8 must always consist of one tab character.
215
216 @node ForIndent Num
217 @subsubsection Line Numbers
218
219 If a number is the first non-whitespace in the line, Fortran
220 indentation assumes it is a line number and moves it to columns 0
221 through 4. (Columns always count from 0 in GNU Emacs.)
222
223 @vindex fortran-line-number-indent
224 Line numbers of four digits or less are normally indented one space.
225 The variable @code{fortran-line-number-indent} controls this; it
226 specifies the maximum indentation a line number can have. The default
227 value of the variable is 1. Fortran mode tries to prevent line number
228 digits passing column 4, reducing the indentation below the specified
229 maximum if necessary. If @code{fortran-line-number-indent} has the
230 value 5, line numbers are right-justified to end in column 4.
231
232 @vindex fortran-electric-line-number
233 Simply inserting a line number is enough to indent it according to
234 these rules. As each digit is inserted, the indentation is recomputed.
235 To turn off this feature, set the variable
236 @code{fortran-electric-line-number} to @code{nil}.
237
238
239 @node ForIndent Conv
240 @subsubsection Syntactic Conventions
241
242 Fortran mode assumes that you follow certain conventions that simplify
243 the task of understanding a Fortran program well enough to indent it
244 properly:
245
246 @itemize @bullet
247 @item
248 Two nested @samp{do} loops never share a @samp{continue} statement.
249
250 @item
251 Fortran keywords such as @samp{if}, @samp{else}, @samp{then}, @samp{do}
252 and others are written without embedded whitespace or line breaks.
253
254 Fortran compilers generally ignore whitespace outside of string
255 constants, but Fortran mode does not recognize these keywords if they
256 are not contiguous. Constructs such as @samp{else if} or @samp{end do}
257 are acceptable, but the second word should be on the same line as the
258 first and not on a continuation line.
259 @end itemize
260
261 @noindent
262 If you fail to follow these conventions, the indentation commands may
263 indent some lines unaesthetically. However, a correct Fortran program
264 retains its meaning when reindented even if the conventions are not
265 followed.
266
267 @node ForIndent Vars
268 @subsubsection Variables for Fortran Indentation
269
270 @vindex fortran-do-indent
271 @vindex fortran-if-indent
272 @vindex fortran-structure-indent
273 @vindex fortran-continuation-indent
274 @vindex fortran-check-all-num@dots{}
275 @vindex fortran-minimum-statement-indent@dots{}
276 Several additional variables control how Fortran indentation works:
277
278 @table @code
279 @item fortran-do-indent
280 Extra indentation within each level of @samp{do} statement (default 3).
281
282 @item fortran-if-indent
283 Extra indentation within each level of @samp{if}, @samp{select case}, or
284 @samp{where} statements (default 3).
285
286 @item fortran-structure-indent
287 Extra indentation within each level of @samp{structure}, @samp{union},
288 @samp{map}, or @samp{interface} statements (default 3).
289
290 @item fortran-continuation-indent
291 Extra indentation for bodies of continuation lines (default 5).
292
293 @item fortran-check-all-num-for-matching-do
294 In Fortran77, a numbered @samp{do} statement is ended by any statement
295 with a matching line number. It is common (but not compulsory) to use a
296 @samp{continue} statement for this purpose. If this variable has a
297 non-@code{nil} value, indenting any numbered statement must check for a
298 @samp{do} that ends there. If you always end @samp{do} statements with
299 a @samp{continue} line (or if you use the more modern @samp{enddo}),
300 then you can speed up indentation by setting this variable to
301 @code{nil}. The default is @code{nil}.
302
303 @item fortran-blink-matching-if
304 If this is @code{t}, indenting an @samp{endif} (or @samp{enddo}
305 statement moves the cursor momentarily to the matching @samp{if} (or
306 @samp{do}) statement to show where it is. The default is @code{nil}.
307
308 @item fortran-minimum-statement-indent-fixed
309 Minimum indentation for Fortran statements when using fixed format
310 continuation line style. Statement bodies are never indented less than
311 this much. The default is 6.
312
313 @item fortran-minimum-statement-indent-tab
314 Minimum indentation for Fortran statements for tab format continuation line
315 style. Statement bodies are never indented less than this much. The
316 default is 8.
317 @end table
318
319 The variables controlling the indentation of comments are described in
320 the following section.
321
322 @node Fortran Comments
323 @subsection Fortran Comments
324
325 The usual Emacs comment commands assume that a comment can follow a
326 line of code. In Fortran77, the standard comment syntax requires an
327 entire line to be just a comment. Therefore, Fortran mode replaces the
328 standard Emacs comment commands and defines some new variables.
329
330 @vindex fortran-comment-line-start
331 Fortran mode can also handle the Fortran90 comment syntax where comments
332 start with @samp{!} and can follow other text. Because only some Fortran77
333 compilers accept this syntax, Fortran mode will not insert such comments
334 unless you have said in advance to do so. To do this, set the variable
335 @code{fortran-comment-line-start} to @samp{"!"}.
336
337 @table @kbd
338 @item M-;
339 Align comment or insert new comment (@code{fortran-indent-comment}).
340
341 @item C-x ;
342 Applies to nonstandard @samp{!} comments only.
343
344 @item C-c ;
345 Turn all lines of the region into comments, or (with argument) turn them back
346 into real code (@code{fortran-comment-region}).
347 @end table
348
349 @findex fortran-indent-comment
350 @kbd{M-;} in Fortran mode is redefined as the command
351 @code{fortran-indent-comment}. Like the usual @kbd{M-;} command, this
352 recognizes any kind of existing comment and aligns its text appropriately;
353 if there is no existing comment, a comment is inserted and aligned. But
354 inserting and aligning comments are not the same in Fortran mode as in
355 other modes.
356
357 When a new comment must be inserted, if the current line is blank, a
358 full-line comment is inserted. On a non-blank line, a nonstandard @samp{!}
359 comment is inserted if you have said you want to use them. Otherwise a
360 full-line comment is inserted on a new line before the current line.
361
362 Nonstandard @samp{!} comments are aligned like comments in other
363 languages, but full-line comments are different. In a standard full-line
364 comment, the comment delimiter itself must always appear in column zero.
365 What can be aligned is the text within the comment. You can choose from
366 three styles of alignment by setting the variable
367 @code{fortran-comment-indent-style} to one of these values:
368
369 @vindex fortran-comment-indent-style
370 @vindex fortran-comment-line-extra-indent
371 @table @code
372 @item fixed
373 Align the text at a fixed column, which is the sum of
374 @code{fortran-comment-line-extra-indent} and the minimum statement
375 indentation. This is the default.
376
377 The minimum statement indentation is
378 @code{fortran-minimum-statement-indent-fixed} for fixed format
379 continuation line style and @code{fortran-minimum-statement-indent-tab}
380 for tab format style.
381
382 @item relative
383 Align the text as if it were a line of code, but with an additional
384 @code{fortran-comment-line-extra-indent} columns of indentation.
385
386 @item nil
387 Don't move text in full-line comments automatically.
388 @end table
389
390 @vindex fortran-comment-indent-char
391 In addition, you can specify the character to be used to indent within
392 full-line comments by setting the variable
393 @code{fortran-comment-indent-char} to the single-character string you want
394 to use.
395
396 @vindex fortran-directive-re
397 Compiler directive lines, or preprocessor lines, have much the same
398 appearance as comment lines. It is important, though, that such lines
399 never be indented at all, no matter what the value of
400 @code{fortran-comment-indent-style}. The variable
401 @code{fortran-directive-re} is a regular expression that specifies which
402 lines are directives. Matching lines are never indented, and receive
403 distinctive font-locking.
404
405 The normal Emacs comment command @kbd{C-x ;} has not been redefined. If
406 you use @samp{!} comments, this command can be used with them. Otherwise
407 it is useless in Fortran mode.
408
409 @kindex C-c ; @r{(Fortran mode)}
410 @findex fortran-comment-region
411 @vindex fortran-comment-region
412 The command @kbd{C-c ;} (@code{fortran-comment-region}) turns all the
413 lines of the region into comments by inserting the string @samp{C$$$} at
414 the front of each one. With a numeric argument, it turns the region
415 back into live code by deleting @samp{C$$$} from the front of each line
416 in it. The string used for these comments can be controlled by setting
417 the variable @code{fortran-comment-region}. Note that here we have an
418 example of a command and a variable with the same name; these two uses
419 of the name never conflict because in Lisp and in Emacs it is always
420 clear from the context which one is meant.
421
422 @node Fortran Autofill
423 @subsection Auto Fill in Fortran Mode
424
425 Fortran mode has specialized support for Auto Fill mode, which is a
426 minor mode that automatically splits statements as you insert them
427 when they become too wide. Splitting a statement involves making
428 continuation lines using @code{fortran-continuation-string}
429 (@pxref{ForIndent Cont}). This splitting happens when you type
430 @key{SPC}, @key{RET}, or @key{TAB}, and also in the Fortran
431 indentation commands. You activate Auto Fill in Fortran mode in the
432 normal way.
433 @iftex
434 @xref{Auto Fill,,, emacs, the Emacs Manual}.
435 @end iftex
436 @ifnottex
437 @xref{Auto Fill}.
438 @end ifnottex
439
440 @vindex fortran-break-before-delimiters
441 Auto Fill breaks lines at spaces or delimiters when the lines get
442 longer than the desired width (the value of @code{fill-column}). The
443 delimiters (besides whitespace) that Auto Fill can break at are
444 @samp{+}, @samp{-}, @samp{/}, @samp{*}, @samp{=}, @samp{<}, @samp{>},
445 and @samp{,}. The line break comes after the delimiter if the
446 variable @code{fortran-break-before-delimiters} is @code{nil}.
447 Otherwise (and by default), the break comes before the delimiter.
448
449 To enable Auto Fill in all Fortran buffers, add
450 @code{turn-on-auto-fill} to @code{fortran-mode-hook}.
451 @iftex
452 @xref{Hooks,,, emacs, the Emacs Manual}.
453 @end iftex
454 @ifnottex
455 @xref{Hooks}.
456 @end ifnottex
457
458 @node Fortran Columns
459 @subsection Checking Columns in Fortran
460
461 @table @kbd
462 @item C-c C-r
463 Display a ``column ruler'' momentarily above the current line
464 (@code{fortran-column-ruler}).
465 @item C-c C-w
466 Split the current window horizontally temporarily so that it is 72
467 columns wide (@code{fortran-window-create-momentarily}). This may
468 help you avoid making lines longer than the 72-character limit that
469 some Fortran compilers impose.
470 @item C-u C-c C-w
471 Split the current window horizontally so that it is 72 columns wide
472 (@code{fortran-window-create}). You can then continue editing.
473 @item M-x fortran-strip-sequence-nos
474 Delete all text in column 72 and beyond.
475 @end table
476
477 @kindex C-c C-r @r{(Fortran mode)}
478 @findex fortran-column-ruler
479 The command @kbd{C-c C-r} (@code{fortran-column-ruler}) shows a column
480 ruler momentarily above the current line. The comment ruler is two lines
481 of text that show you the locations of columns with special significance in
482 Fortran programs. Square brackets show the limits of the columns for line
483 numbers, and curly brackets show the limits of the columns for the
484 statement body. Column numbers appear above them.
485
486 Note that the column numbers count from zero, as always in GNU Emacs.
487 As a result, the numbers may be one less than those you are familiar
488 with; but the positions they indicate in the line are standard for
489 Fortran.
490
491 @vindex fortran-column-ruler-fixed
492 @vindex fortran-column-ruler-tabs
493 The text used to display the column ruler depends on the value of the
494 variable @code{indent-tabs-mode}. If @code{indent-tabs-mode} is
495 @code{nil}, then the value of the variable
496 @code{fortran-column-ruler-fixed} is used as the column ruler.
497 Otherwise, the value of the variable @code{fortran-column-ruler-tab} is
498 displayed. By changing these variables, you can change the column ruler
499 display.
500
501 @kindex C-c C-w @r{(Fortran mode)}
502 @findex fortran-window-create-momentarily
503 @kbd{C-c C-w} (@code{fortran-window-create-momentarily}) temporarily
504 splits the current window horizontally, making a window 72 columns
505 wide, so you can see any lines that are too long. Type a space to
506 restore the normal width.
507
508 @kindex C-u C-c C-w @r{(Fortran mode)}
509 @findex fortran-window-create
510 You can also split the window horizontally and continue editing with
511 the split in place. To do this, use @kbd{C-u C-c C-w} (@code{M-x
512 fortran-window-create}). By editing in this window you can
513 immediately see when you make a line too wide to be correct Fortran.
514
515 @findex fortran-strip-sequence-nos
516 The command @kbd{M-x fortran-strip-sequence-nos} deletes all text in
517 column 72 and beyond, on all lines in the current buffer. This is the
518 easiest way to get rid of old sequence numbers.
519
520 @node Fortran Abbrev
521 @subsection Fortran Keyword Abbrevs
522
523 Fortran mode provides many built-in abbrevs for common keywords and
524 declarations. These are the same sort of abbrev that you can define
525 yourself. To use them, you must turn on Abbrev mode.
526 @iftex
527 @xref{Abbrevs,,, emacs, the Emacs Manual}.
528 @end iftex
529 @ifnottex
530 @xref{Abbrevs}.
531 @end ifnottex
532
533 The built-in abbrevs are unusual in one way: they all start with a
534 semicolon. You cannot normally use semicolon in an abbrev, but Fortran
535 mode makes this possible by changing the syntax of semicolon to ``word
536 constituent.''
537
538 For example, one built-in Fortran abbrev is @samp{;c} for
539 @samp{continue}. If you insert @samp{;c} and then insert a punctuation
540 character such as a space or a newline, the @samp{;c} expands automatically
541 to @samp{continue}, provided Abbrev mode is enabled.@refill
542
543 Type @samp{;?} or @samp{;C-h} to display a list of all the built-in
544 Fortran abbrevs and what they stand for.
545
546 @ignore
547 arch-tag: 23ed7c36-1517-4646-9235-2d5ade5f06f6
548 @end ignore