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