1 @c This is part of the Emacs manual.
2 @c Copyright (C) 2004-2011 Free Software Foundation, Inc.
3 @c See file emacs.texi for copying conditions.
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).
12 @cindex Fortran fixed form and free form
13 @cindex Fortran 77 and Fortran 90, 95, 2003
16 Fortran mode is meant for editing ``fixed form'' (and also ``tab
17 format'') source code (normally Fortran 77). For editing more modern
18 ``free form'' source code (Fortran 90, 95, 2003), use F90 mode
19 (@code{f90-mode}). Emacs normally uses Fortran mode for files with
20 extension @samp{.f}, @samp{.F} or @samp{.for}, and F90 mode for the
21 extensions @samp{.f90} and @samp{.f95}. Customize
22 @code{auto-mode-alist} to add more extensions. GNU Fortran supports
23 both free and fixed form. This manual mainly documents Fortran mode,
24 but the corresponding F90 mode features are mentioned when revelant.
26 Fortran mode provides special motion commands for Fortran statements
27 and subprograms, and indentation commands that understand Fortran
28 conventions of nesting, line numbers and continuation statements.
29 Fortran mode has support for Auto Fill mode that breaks long lines into
30 proper Fortran continuation lines. Fortran mode also supports Hideshow
33 (@pxref{Hideshow,,, emacs, the Emacs Manual}),
40 (@pxref{Imenu,,, emacs, the Emacs Manual}).
46 Special commands for comments are provided because Fortran comments
47 are unlike those of other languages. Built-in abbrevs optionally save
48 typing when you insert Fortran keywords.
50 Use @kbd{M-x fortran-mode} to switch to this major mode. This
51 command runs the hook @code{fortran-mode-hook}.
53 @xref{Hooks,,, emacs, the Emacs Manual}.
60 * Motion: Fortran Motion. Moving point by statements or subprograms.
61 * Indent: Fortran Indent. Indentation commands for Fortran.
62 * Comments: Fortran Comments. Inserting and aligning comments.
63 * Autofill: Fortran Autofill. Auto fill support for Fortran.
64 * Columns: Fortran Columns. Measuring columns for valid Fortran.
65 * Abbrev: Fortran Abbrev. Built-in abbrevs for Fortran keywords.
69 @subsection Motion Commands
71 In addition to the normal commands for moving by and operating on
72 ``defuns'' (Fortran subprograms---functions and subroutines, using the
73 commands @code{fortran-beginning-of-subprogram} and
74 @code{fortran-end-of-subprogram}; as well as modules for F90 mode),
75 Fortran mode provides special commands to move by statements and other
79 @kindex C-c C-n @r{(Fortran mode)}
80 @findex fortran-next-statement
81 @findex f90-next-statement
83 Move to the beginning of the next statement
84 (@code{fortran-next-statement}/@code{f90-next-statement}).
86 @kindex C-c C-p @r{(Fortran mode)}
87 @findex fortran-previous-statement
88 @findex f90-previous-statement
90 Move to the beginning of the previous statement
91 (@code{fortran-previous-statement}/@code{f90-previous-statement}).
92 If there is no previous statement (i.e. if called from the first
93 statement in the buffer), move to the start of the buffer.
95 @kindex C-c C-e @r{(F90 mode)}
96 @findex f90-next-block
98 Move point forward to the start of the next code block, or the end of
99 the current block, whichever is encountered first.
100 (@code{f90-next-block}). A code block is a subroutine,
101 @code{if}--@code{endif} statement, and so forth. This command exists
102 for F90 mode only, not Fortran mode. With a numeric argument, this
103 moves forward that many blocks.
105 @kindex C-c C-a @r{(F90 mode)}
106 @findex f90-previous-block
108 Move point backward to the previous code block
109 (@code{f90-previous-block}). This is like @code{f90-next-block}, but
112 @kindex C-M-n @r{(Fortran mode)}
113 @findex fortran-end-of-block
114 @findex f90-end-of-block
116 Move to the end of the current code block
117 (@code{fortran-end-of-block}/@code{f90-end-of-block}). With a numeric
118 argument, move forward that number of blocks. The mark is set before
119 moving point. The F90 mode version of this command checks for
120 consistency of block types and labels (if present), but it does not
121 check the outermost block since that may be incomplete.
123 @kindex C-M-p @r{(Fortran mode)}
124 @findex fortran-beginning-of-block
125 @findex f90-beginning-of-block
127 Move to the start of the current code block
128 (@code{fortran-beginning-of-block}/@code{f90-beginning-of-block}). This
129 is like @code{fortran-end-of-block}, but moves backwards.
132 The commands @code{fortran-beginning-of-subprogram} and
133 @code{fortran-end-of-subprogram} move to the start or end of the
134 current subprogram, respectively. The commands @code{fortran-mark-do}
135 and @code{fortran-mark-if} mark the end of the current @code{do} or
136 @code{if} block, and move point to the start.
140 @subsection Fortran Indentation
142 Special commands and features are needed for indenting fixed (or tab)
143 form Fortran code in order to make sure various syntactic entities (line
144 numbers, comment line indicators and continuation line flags) appear in
145 the required columns.
148 * Commands: ForIndent Commands. Commands for indenting and filling Fortran.
149 * Contline: ForIndent Cont. How continuation lines indent.
150 * Numbers: ForIndent Num. How line numbers auto-indent.
151 * Conv: ForIndent Conv. Conventions you must obey to avoid trouble.
152 * Vars: ForIndent Vars. Variables controlling Fortran indent style.
155 @node ForIndent Commands
156 @subsubsection Fortran Indentation and Filling Commands
160 Break the current line at point and set up a continuation line
161 (@code{fortran-split-line}).
163 Join this line to the previous line (@code{fortran-join-line}).
165 Indent all the lines of the subprogram point is in
166 (@code{fortran-indent-subprogram}).
168 Fill a comment block or statement (using @code{fortran-fill-paragraph}
169 or @code{fortran-fill-statement}).
172 @kindex C-M-q @r{(Fortran mode)}
173 @findex fortran-indent-subprogram
174 The key @kbd{C-M-q} runs @code{fortran-indent-subprogram}, a command
175 to reindent all the lines of the Fortran subprogram (function or
176 subroutine) containing point.
178 @kindex C-M-j @r{(Fortran mode)}
179 @findex fortran-split-line
180 The key @kbd{C-M-j} runs @code{fortran-split-line}, which splits
181 a line in the appropriate fashion for Fortran. In a non-comment line,
182 the second half becomes a continuation line and is indented
183 accordingly. In a comment line, both halves become separate comment
186 @kindex M-^ @r{(Fortran mode)}
187 @kindex C-c C-d @r{(Fortran mode)}
188 @findex fortran-join-line
189 @kbd{M-^} or @kbd{C-c C-d} runs the command @code{fortran-join-line},
190 which joins a continuation line back to the previous line, roughly as
191 the inverse of @code{fortran-split-line}. The point must be on a
192 continuation line when this command is invoked.
194 @kindex M-q @r{(Fortran mode)}
195 @kbd{M-q} in Fortran mode fills the comment block or statement that
196 point is in. This removes any excess statement continuations.
199 @subsubsection Continuation Lines
200 @cindex Fortran continuation lines
202 @vindex fortran-continuation-string
203 Most Fortran 77 compilers allow two ways of writing continuation lines.
204 If the first non-space character on a line is in column 5, then that
205 line is a continuation of the previous line. We call this @dfn{fixed
206 form}. (In GNU Emacs we always count columns from 0; but note that
207 the Fortran standard counts from 1.) The variable
208 @code{fortran-continuation-string} specifies what character to put in
209 column 5. A line that starts with a tab character followed by any digit
210 except @samp{0} is also a continuation line. We call this style of
211 continuation @dfn{tab format}. (Fortran 90 introduced ``free form,''
212 with another style of continuation lines).
214 @vindex indent-tabs-mode @r{(Fortran mode)}
215 @vindex fortran-analyze-depth
216 @vindex fortran-tab-mode-default
217 Fortran mode can use either style of continuation line. When you
218 enter Fortran mode, it tries to deduce the proper continuation style
219 automatically from the buffer contents. It does this by scanning up to
220 @code{fortran-analyze-depth} (default 100) lines from the start of the
221 buffer. The first line that begins with either a tab character or six
222 spaces determines the choice. If the scan fails (for example, if the
223 buffer is new and therefore empty), the value of
224 @code{fortran-tab-mode-default} (@code{nil} for fixed form, and
225 non-@code{nil} for tab format) is used. @samp{/t}
226 (@code{fortran-tab-mode-string}) in the mode line indicates tab format
227 is selected. Fortran mode sets the value of @code{indent-tabs-mode}
230 If the text on a line starts with the Fortran continuation marker
231 @samp{$}, or if it begins with any non-whitespace character in column
232 5, Fortran mode treats it as a continuation line. When you indent a
233 continuation line with @key{TAB}, it converts the line to the current
234 continuation style. When you split a Fortran statement with
235 @kbd{C-M-j}, the continuation marker on the newline is created according
236 to the continuation style.
238 The setting of continuation style affects several other aspects of
239 editing in Fortran mode. In fixed form mode, the minimum column
240 number for the body of a statement is 6. Lines inside of Fortran
241 blocks that are indented to larger column numbers always use only the
242 space character for whitespace. In tab format mode, the minimum
243 column number for the statement body is 8, and the whitespace before
244 column 8 must always consist of one tab character.
247 @subsubsection Line Numbers
249 If a number is the first non-whitespace in the line, Fortran
250 indentation assumes it is a line number and moves it to columns 0
251 through 4. (Columns always count from 0 in GNU Emacs.)
253 @vindex fortran-line-number-indent
254 Line numbers of four digits or less are normally indented one space.
255 The variable @code{fortran-line-number-indent} controls this; it
256 specifies the maximum indentation a line number can have. The default
257 value of the variable is 1. Fortran mode tries to prevent line number
258 digits passing column 4, reducing the indentation below the specified
259 maximum if necessary. If @code{fortran-line-number-indent} has the
260 value 5, line numbers are right-justified to end in column 4.
262 @vindex fortran-electric-line-number
263 Simply inserting a line number is enough to indent it according to
264 these rules. As each digit is inserted, the indentation is recomputed.
265 To turn off this feature, set the variable
266 @code{fortran-electric-line-number} to @code{nil}.
270 @subsubsection Syntactic Conventions
272 Fortran mode assumes that you follow certain conventions that simplify
273 the task of understanding a Fortran program well enough to indent it
278 Two nested @samp{do} loops never share a @samp{continue} statement.
281 Fortran keywords such as @samp{if}, @samp{else}, @samp{then}, @samp{do}
282 and others are written without embedded whitespace or line breaks.
284 Fortran compilers generally ignore whitespace outside of string
285 constants, but Fortran mode does not recognize these keywords if they
286 are not contiguous. Constructs such as @samp{else if} or @samp{end do}
287 are acceptable, but the second word should be on the same line as the
288 first and not on a continuation line.
292 If you fail to follow these conventions, the indentation commands may
293 indent some lines unaesthetically. However, a correct Fortran program
294 retains its meaning when reindented even if the conventions are not
298 @subsubsection Variables for Fortran Indentation
300 @vindex fortran-do-indent
301 @vindex fortran-if-indent
302 @vindex fortran-structure-indent
303 @vindex fortran-continuation-indent
304 @vindex fortran-check-all-num@dots{}
305 @vindex fortran-minimum-statement-indent@dots{}
306 Several additional variables control how Fortran indentation works:
309 @item fortran-do-indent
310 Extra indentation within each level of @samp{do} statement (default 3).
312 @item fortran-if-indent
313 Extra indentation within each level of @samp{if}, @samp{select case}, or
314 @samp{where} statements (default 3).
316 @item fortran-structure-indent
317 Extra indentation within each level of @samp{structure}, @samp{union},
318 @samp{map}, or @samp{interface} statements (default 3).
320 @item fortran-continuation-indent
321 Extra indentation for bodies of continuation lines (default 5).
323 @item fortran-check-all-num-for-matching-do
324 In Fortran 77, a numbered @samp{do} statement is ended by any statement
325 with a matching line number. It is common (but not compulsory) to use a
326 @samp{continue} statement for this purpose. If this variable has a
327 non-@code{nil} value, indenting any numbered statement must check for a
328 @samp{do} that ends there. If you always end @samp{do} statements with
329 a @samp{continue} line (or if you use the more modern @samp{enddo}),
330 then you can speed up indentation by setting this variable to
331 @code{nil}. The default is @code{nil}.
333 @item fortran-blink-matching-if
334 If this is @code{t}, indenting an @samp{endif} (or @samp{enddo}
335 statement moves the cursor momentarily to the matching @samp{if} (or
336 @samp{do}) statement to show where it is. The default is @code{nil}.
338 @item fortran-minimum-statement-indent-fixed
339 Minimum indentation for Fortran statements when using fixed form
340 continuation line style. Statement bodies are never indented less than
341 this much. The default is 6.
343 @item fortran-minimum-statement-indent-tab
344 Minimum indentation for Fortran statements for tab format continuation line
345 style. Statement bodies are never indented less than this much. The
349 The variables controlling the indentation of comments are described in
350 the following section.
352 @node Fortran Comments
353 @subsection Fortran Comments
355 The usual Emacs comment commands assume that a comment can follow a
356 line of code. In Fortran 77, the standard comment syntax requires an
357 entire line to be just a comment. Therefore, Fortran mode replaces the
358 standard Emacs comment commands and defines some new variables.
360 @vindex fortran-comment-line-start
361 Fortran mode can also handle the Fortran 90 comment syntax where
362 comments start with @samp{!} and can follow other text. Because only
363 some Fortran 77 compilers accept this syntax, Fortran mode will not
364 insert such comments unless you have said in advance to do so. To do
365 this, set the variable @code{fortran-comment-line-start} to @samp{"!"}.
366 If you use an unusual value, you may also need to adjust
367 @code{fortran-comment-line-start-skip}.
372 Align comment or insert new comment (@code{comment-dwim}).
375 Applies to nonstandard @samp{!} comments only.
378 Turn all lines of the region into comments, or (with argument) turn them back
379 into real code (@code{fortran-comment-region}).
382 @kbd{M-;} in Fortran mode runs the standard @code{comment-dwim}.
383 This recognizes any kind of existing comment and aligns its text
384 appropriately; if there is no existing comment, a comment is inserted
385 and aligned. Inserting and aligning comments are not the same in
386 Fortran mode as in other modes.
388 When a new comment must be inserted, if the current line is blank, a
389 full-line comment is inserted. On a non-blank line, a nonstandard @samp{!}
390 comment is inserted if you have said you want to use them. Otherwise a
391 full-line comment is inserted on a new line before the current line.
393 Nonstandard @samp{!} comments are aligned like comments in other
394 languages, but full-line comments are different. In a standard full-line
395 comment, the comment delimiter itself must always appear in column zero.
396 What can be aligned is the text within the comment. You can choose from
397 three styles of alignment by setting the variable
398 @code{fortran-comment-indent-style} to one of these values:
400 @vindex fortran-comment-indent-style
401 @vindex fortran-comment-line-extra-indent
404 Align the text at a fixed column, which is the sum of
405 @code{fortran-comment-line-extra-indent} and the minimum statement
406 indentation. This is the default.
408 The minimum statement indentation is
409 @code{fortran-minimum-statement-indent-fixed} for fixed form
410 continuation line style and @code{fortran-minimum-statement-indent-tab}
411 for tab format style.
414 Align the text as if it were a line of code, but with an additional
415 @code{fortran-comment-line-extra-indent} columns of indentation.
418 Don't move text in full-line comments automatically.
421 @vindex fortran-comment-indent-char
422 In addition, you can specify the character to be used to indent within
423 full-line comments by setting the variable
424 @code{fortran-comment-indent-char} to the single-character string you want
427 @vindex fortran-directive-re
428 Compiler directive lines, or preprocessor lines, have much the same
429 appearance as comment lines. It is important, though, that such lines
430 never be indented at all, no matter what the value of
431 @code{fortran-comment-indent-style}. The variable
432 @code{fortran-directive-re} is a regular expression that specifies which
433 lines are directives. Matching lines are never indented, and receive
434 distinctive font-locking.
436 The normal Emacs comment command @kbd{C-x ;} has not been redefined. If
437 you use @samp{!} comments, this command can be used with them. Otherwise
438 it is useless in Fortran mode.
440 @kindex C-c ; @r{(Fortran mode)}
441 @findex fortran-comment-region
442 @vindex fortran-comment-region
443 The command @kbd{C-c ;} (@code{fortran-comment-region}) turns all the
444 lines of the region into comments by inserting the string @samp{C$$$} at
445 the front of each one. With a numeric argument, it turns the region
446 back into live code by deleting @samp{C$$$} from the front of each line
447 in it. The string used for these comments can be controlled by setting
448 the variable @code{fortran-comment-region}. Note that here we have an
449 example of a command and a variable with the same name; these two uses
450 of the name never conflict because in Lisp and in Emacs it is always
451 clear from the context which one is meant.
453 @node Fortran Autofill
454 @subsection Auto Fill in Fortran Mode
456 Fortran mode has specialized support for Auto Fill mode, which is a
457 minor mode that automatically splits statements as you insert them
458 when they become too wide. Splitting a statement involves making
459 continuation lines using @code{fortran-continuation-string}
460 (@pxref{ForIndent Cont}). This splitting happens when you type
461 @key{SPC}, @key{RET}, or @key{TAB}, and also in the Fortran
462 indentation commands. You activate Auto Fill in Fortran mode in the
465 @xref{Auto Fill,,, emacs, the Emacs Manual}.
471 @vindex fortran-break-before-delimiters
472 Auto Fill breaks lines at spaces or delimiters when the lines get
473 longer than the desired width (the value of @code{fill-column}). The
474 delimiters (besides whitespace) that Auto Fill can break at are
475 @samp{+}, @samp{-}, @samp{/}, @samp{*}, @samp{=}, @samp{<}, @samp{>},
476 and @samp{,}. The line break comes after the delimiter if the
477 variable @code{fortran-break-before-delimiters} is @code{nil}.
478 Otherwise (and by default), the break comes before the delimiter.
480 To enable Auto Fill in all Fortran buffers, add
481 @code{turn-on-auto-fill} to @code{fortran-mode-hook}.
483 @xref{Hooks,,, emacs, the Emacs Manual}.
489 @node Fortran Columns
490 @subsection Checking Columns in Fortran
492 @vindex fortran-line-length
493 In standard Fortran 77, anything beyond column 72 is ignored.
494 Most compilers provide an option to change this (for example,
495 @samp{-ffixed-line-length-N} in gfortran). Customize the variable
496 @code{fortran-line-length} to change the line length in Fortran mode.
497 Anything beyond this point is font-locked as a comment. (Unless it is
498 inside a string: strings that extend beyond @code{fortran-line-length}
499 will confuse font-lock.)
503 Display a ``column ruler'' momentarily above the current line
504 (@code{fortran-column-ruler}).
506 Split the current window horizontally temporarily so that it is
507 @code{fortran-line-length} columns wide
508 (@code{fortran-window-create-momentarily}). This may help you avoid
509 making lines longer than the character limit imposed by your Fortran
512 Split the current window horizontally so that it is
513 @code{fortran-line-length} columns wide (@code{fortran-window-create}).
514 You can then continue editing.
515 @item M-x fortran-strip-sequence-nos
516 Delete all text in column @code{fortran-line-length} and beyond.
519 @kindex C-c C-r @r{(Fortran mode)}
520 @findex fortran-column-ruler
521 The command @kbd{C-c C-r} (@code{fortran-column-ruler}) shows a column
522 ruler momentarily above the current line. The comment ruler is two lines
523 of text that show you the locations of columns with special significance in
524 Fortran programs. Square brackets show the limits of the columns for line
525 numbers, and curly brackets show the limits of the columns for the
526 statement body. Column numbers appear above them.
528 Note that the column numbers count from zero, as always in GNU Emacs.
529 As a result, the numbers may be one less than those you are familiar
530 with; but the positions they indicate in the line are standard for
533 @vindex fortran-column-ruler-fixed
534 @vindex fortran-column-ruler-tabs
535 The text used to display the column ruler depends on the value of the
536 variable @code{indent-tabs-mode}. If @code{indent-tabs-mode} is
537 @code{nil}, then the value of the variable
538 @code{fortran-column-ruler-fixed} is used as the column ruler.
539 Otherwise, the value of the variable @code{fortran-column-ruler-tab} is
540 displayed. By changing these variables, you can change the column ruler
543 @kindex C-c C-w @r{(Fortran mode)}
544 @findex fortran-window-create-momentarily
545 @kbd{C-c C-w} (@code{fortran-window-create-momentarily}) temporarily
546 splits the current window horizontally, making a window
547 @code{fortran-line-length} columns wide, so you can see any lines that
548 are too long. Type a space to restore the normal width.
550 @kindex C-u C-c C-w @r{(Fortran mode)}
551 @findex fortran-window-create
552 You can also split the window horizontally and continue editing with
553 the split in place. To do this, use @kbd{C-u C-c C-w} (@code{M-x
554 fortran-window-create}). By editing in this window you can
555 immediately see when you make a line too wide to be correct Fortran.
557 @findex fortran-strip-sequence-nos
558 The command @kbd{M-x fortran-strip-sequence-nos} deletes all text in
559 column @code{fortran-line-length} and beyond, on all lines in the
560 current buffer. This is the easiest way to get rid of old sequence
564 @subsection Fortran Keyword Abbrevs
566 Fortran mode provides many built-in abbrevs for common keywords and
567 declarations. These are the same sort of abbrev that you can define
568 yourself. To use them, you must turn on Abbrev mode.
570 @xref{Abbrevs,,, emacs, the Emacs Manual}.
576 The built-in abbrevs are unusual in one way: they all start with a
577 semicolon. For example, one built-in Fortran abbrev is @samp{;c} for
578 @samp{continue}. If you insert @samp{;c} and then insert a punctuation
579 character such as a space or a newline, the @samp{;c} expands automatically
580 to @samp{continue}, provided Abbrev mode is enabled.@refill
582 Type @samp{;?} or @samp{;C-h} to display a list of all the built-in
583 Fortran abbrevs and what they stand for.