1 @c This is part of the Emacs manual.
2 @c Copyright (C) 2004-2014 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, 2008
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, 2008), 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}, @samp{.f95}, @samp{.f03} and @samp{.f08}.
22 Customize @code{auto-mode-alist} to add more extensions. GNU Fortran
23 supports both free and fixed form. This manual mainly documents Fortran
24 mode, but the corresponding F90 mode features are mentioned when
27 Fortran mode provides special motion commands for Fortran statements
28 and subprograms, and indentation commands that understand Fortran
29 conventions of nesting, line numbers and continuation statements.
30 Fortran mode has support for Auto Fill mode that breaks long lines into
31 proper Fortran continuation lines. Fortran mode also supports Hideshow
34 (@pxref{Hideshow,,, emacs, the Emacs Manual}),
41 (@pxref{Imenu,,, emacs, the Emacs Manual}).
47 Special commands for comments are provided because Fortran comments
48 are unlike those of other languages. Built-in abbrevs optionally save
49 typing when you insert Fortran keywords.
51 Use @kbd{M-x fortran-mode} to switch to this major mode. This
52 command runs the hook @code{fortran-mode-hook}.
54 @xref{Hooks,,, emacs, the Emacs Manual}.
61 * Motion: Fortran Motion. Moving point by statements or subprograms.
62 * Indent: Fortran Indent. Indentation commands for Fortran.
63 * Comments: Fortran Comments. Inserting and aligning comments.
64 * Autofill: Fortran Autofill. Auto fill support for Fortran.
65 * Columns: Fortran Columns. Measuring columns for valid Fortran.
66 * Abbrev: Fortran Abbrev. Built-in abbrevs for Fortran keywords.
70 @subsection Motion Commands
72 In addition to the normal commands for moving by and operating on
73 ``defuns'' (Fortran subprograms---functions and subroutines, as well
74 as modules for F90 mode, using the commands @code{fortran-end-of-subprogram}
75 and @code{fortran-beginning-of-subprogram}), Fortran mode provides
76 special commands to move by statements and other program units.
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 one, whichever comes first (@code{f90-next-block}).
100 A code block is a subroutine, @code{if}--@code{endif} statement, and
101 so forth. This command exists for F90 mode only, not Fortran mode.
102 With a numeric argument, it moves forward that many blocks.
104 @kindex C-c C-a @r{(F90 mode)}
105 @findex f90-previous-block
107 Move point backward to the previous block
108 (@code{f90-previous-block}). This is like @code{f90-next-block}, but
111 @kindex C-M-n @r{(Fortran mode)}
112 @findex fortran-end-of-block
113 @findex f90-end-of-block
115 Move to the end of the current code block
116 (@code{fortran-end-of-block}/@code{f90-end-of-block}). With a numeric
117 argument, move forward that number of blocks. The mark is set before
118 moving point. The F90 mode version of this command checks for
119 consistency of block types and labels (if present), but it does not
120 check the outermost block since that may be incomplete.
122 @kindex C-M-p @r{(Fortran mode)}
123 @findex fortran-beginning-of-block
124 @findex f90-beginning-of-block
126 Move to the start of the current code block
127 (@code{fortran-beginning-of-block}/@code{f90-beginning-of-block}). This
128 is like @code{fortran-end-of-block}, but moves backwards.
131 The commands @code{fortran-beginning-of-subprogram} and
132 @code{fortran-end-of-subprogram} move to the start or end of the
133 current subprogram, respectively. The commands @code{fortran-mark-do}
134 and @code{fortran-mark-if} mark the end of the current @code{do} or
135 @code{if} block, and move point to the start.
139 @subsection Fortran Indentation
141 Special commands and features are needed for indenting fixed (or tab)
142 form Fortran code in order to make sure various syntactic entities (line
143 numbers, comment line indicators and continuation line flags) appear in
144 the required columns.
147 * Commands: ForIndent Commands. Commands for indenting and filling Fortran.
148 * Contline: ForIndent Cont. How continuation lines indent.
149 * Numbers: ForIndent Num. How line numbers auto-indent.
150 * Conv: ForIndent Conv. Conventions you must obey to avoid trouble.
151 * Vars: ForIndent Vars. Variables controlling Fortran indent style.
154 @node ForIndent Commands
155 @subsubsection Fortran Indentation and Filling Commands
159 Break the current line at point and set up a continuation line
160 (@code{fortran-split-line}).
162 Join this line to the previous line (@code{fortran-join-line}).
164 Indent all the lines of the subprogram that point is in
165 (@code{fortran-indent-subprogram}).
167 Fill a comment block or statement (using @code{fortran-fill-paragraph}
168 or @code{fortran-fill-statement}).
171 @kindex C-M-q @r{(Fortran mode)}
172 @findex fortran-indent-subprogram
173 The key @kbd{C-M-q} runs @code{fortran-indent-subprogram}, a command
174 to reindent all the lines of the Fortran subprogram (function or
175 subroutine) containing point.
177 @kindex C-M-j @r{(Fortran mode)}
178 @findex fortran-split-line
179 The key @kbd{C-M-j} runs @code{fortran-split-line}, which splits
180 a line in the appropriate fashion for Fortran. In a non-comment line,
181 the second half becomes a continuation line and is indented
182 accordingly. In a comment line, both halves become separate comment
185 @kindex M-^ @r{(Fortran mode)}
186 @kindex C-c C-d @r{(Fortran mode)}
187 @findex fortran-join-line
188 @kbd{M-^} or @kbd{C-c C-d} runs the command @code{fortran-join-line},
189 which joins a continuation line back to the previous line, roughly as
190 the inverse of @code{fortran-split-line}. The point must be on a
191 continuation line when this command is invoked.
193 @kindex M-q @r{(Fortran mode)}
194 @kbd{M-q} in Fortran mode fills the comment block or statement that
195 point is in. This removes any excess statement continuations.
198 @subsubsection Continuation Lines
199 @cindex Fortran continuation lines
201 @vindex fortran-continuation-string
202 Most Fortran 77 compilers allow two ways of writing continuation lines.
203 If the first non-space character on a line is in column 5, then that
204 line is a continuation of the previous line. We call this @dfn{fixed
205 form}. (In GNU Emacs we always count columns from 0; but note that
206 the Fortran standard counts from 1.) The variable
207 @code{fortran-continuation-string} specifies what character to put in
208 column 5. A line that starts with a tab character followed by any digit
209 except @samp{0} is also a continuation line. We call this style of
210 continuation @dfn{tab format}. (Fortran 90 introduced ``free form'',
211 with another style of continuation lines).
213 @vindex indent-tabs-mode @r{(Fortran mode)}
214 @vindex fortran-analyze-depth
215 @vindex fortran-tab-mode-default
216 Fortran mode can use either style of continuation line. When you
217 enter Fortran mode, it tries to deduce the proper continuation style
218 automatically from the buffer contents. It does this by scanning up to
219 @code{fortran-analyze-depth} (default 100) lines from the start of the
220 buffer. The first line that begins with either a tab character or six
221 spaces determines the choice. If the scan fails (for example, if the
222 buffer is new and therefore empty), the value of
223 @code{fortran-tab-mode-default} (@code{nil} for fixed form, and
224 non-@code{nil} for tab format) is used. @samp{/t}
225 (@code{fortran-tab-mode-string}) in the mode line indicates tab format
226 is selected. Fortran mode sets the value of @code{indent-tabs-mode}
229 If the text on a line starts with the Fortran continuation marker
230 @samp{$}, or if it begins with any non-whitespace character in column
231 5, Fortran mode treats it as a continuation line. When you indent a
232 continuation line with @key{TAB}, it converts the line to the current
233 continuation style. When you split a Fortran statement with
234 @kbd{C-M-j}, the continuation marker on the newline is created according
235 to the continuation style.
237 The setting of continuation style affects several other aspects of
238 editing in Fortran mode. In fixed form mode, the minimum column
239 number for the body of a statement is 6. Lines inside of Fortran
240 blocks that are indented to larger column numbers must use only the
241 space character for whitespace. In tab format mode, the minimum
242 column number for the statement body is 8, and the whitespace before
243 column 8 must consist of one tab character.
246 @subsubsection Line Numbers
248 If a number is the first non-whitespace in the line, Fortran
249 indentation assumes it is a line number and moves it to columns 0
250 through 4. (Columns always count from 0 in Emacs.)
252 @vindex fortran-line-number-indent
253 Line numbers of four digits or less are normally indented one space.
254 The variable @code{fortran-line-number-indent} controls this; it
255 specifies the maximum indentation a line number can have. The default
256 value of the variable is 1. Fortran mode tries to prevent line number
257 digits passing column 4, reducing the indentation below the specified
258 maximum if necessary. If @code{fortran-line-number-indent} has the
259 value 5, line numbers are right-justified to end in column 4.
261 @vindex fortran-electric-line-number
262 Simply inserting a line number is enough to indent it according to
263 these rules. As each digit is inserted, the indentation is recomputed.
264 To turn off this feature, set the variable
265 @code{fortran-electric-line-number} to @code{nil}.
269 @subsubsection Syntactic Conventions
271 Fortran mode assumes that you follow certain conventions that simplify
272 the task of understanding a Fortran program well enough to indent it
277 Two nested @samp{do} loops never share a @samp{continue} statement.
280 Fortran keywords such as @samp{if}, @samp{else}, @samp{then}, @samp{do}
281 and others are written without embedded whitespace or line breaks.
283 Fortran compilers generally ignore whitespace outside of string
284 constants, but Fortran mode does not recognize these keywords if they
285 are not contiguous. Constructs such as @samp{else if} or @samp{end do}
286 are acceptable, but the second word should be on the same line as the
287 first and not on a continuation line.
291 If you fail to follow these conventions, the indentation commands may
292 indent some lines unaesthetically. However, a correct Fortran program
293 retains its meaning when reindented even if the conventions are not
297 @subsubsection Variables for Fortran Indentation
299 @vindex fortran-do-indent
300 @vindex fortran-if-indent
301 @vindex fortran-structure-indent
302 @vindex fortran-continuation-indent
303 @vindex fortran-check-all-num@dots{}
304 @vindex fortran-minimum-statement-indent@dots{}
305 Several additional variables control how Fortran indentation works:
308 @item fortran-do-indent
309 Extra indentation within each level of @samp{do} statement (default 3).
311 @item fortran-if-indent
312 Extra indentation within each level of @samp{if}, @samp{select case}, or
313 @samp{where} statements (default 3).
315 @item fortran-structure-indent
316 Extra indentation within each level of @samp{structure}, @samp{union},
317 @samp{map}, or @samp{interface} statements (default 3).
319 @item fortran-continuation-indent
320 Extra indentation for bodies of continuation lines (default 5).
322 @item fortran-check-all-num-for-matching-do
323 In Fortran 77, a numbered @samp{do} statement is ended by any statement
324 with a matching line number. It is common (but not compulsory) to use a
325 @samp{continue} statement for this purpose. If this variable has a
326 non-@code{nil} value, indenting any numbered statement must check for a
327 @samp{do} that ends there. If you always end @samp{do} statements with
328 a @samp{continue} line (or if you use the more modern @samp{enddo}),
329 then you can speed up indentation by setting this variable to
330 @code{nil} (the default).
332 @item fortran-blink-matching-if
333 If this is @code{t}, indenting an @samp{endif} (or @samp{enddo}
334 statement moves the cursor momentarily to the matching @samp{if} (or
335 @samp{do}) statement to show where it is. The default is @code{nil}.
337 @item fortran-minimum-statement-indent-fixed
338 Minimum indentation for Fortran statements when using fixed form
339 continuation line style. Statement bodies are never indented by less than
340 this. The default is 6.
342 @item fortran-minimum-statement-indent-tab
343 Minimum indentation for Fortran statements for tab format continuation line
344 style. Statement bodies are never indented by less than this. The
348 The following section describes the variables controlling the
349 indentation of comments.
351 @node Fortran Comments
352 @subsection Fortran Comments
354 The usual Emacs comment commands assume that a comment can follow a
355 line of code. In Fortran 77, the standard comment syntax requires an
356 entire line to be just a comment. Therefore, Fortran mode replaces the
357 standard Emacs comment commands and defines some new variables.
359 @vindex fortran-comment-line-start
360 Fortran mode can also handle the Fortran 90 comment syntax where
361 comments start with @samp{!} and can follow other text. Because only
362 some Fortran 77 compilers accept this syntax, Fortran mode will not
363 insert such comments unless you have said in advance to do so. To do
364 this, set the variable @code{fortran-comment-line-start} to @samp{"!"}.
365 If you use an unusual value, you may need to change
366 @code{fortran-comment-line-start-skip}.
371 Align comment or insert new comment (@code{comment-dwim}).
374 Applies to nonstandard @samp{!} comments only (@code{comment-set-column}).
377 Turn all lines of the region into comments, or (with argument) turn them back
378 into real code (@code{fortran-comment-region}).
381 @kbd{M-;} in Fortran mode runs the standard @code{comment-dwim}.
382 This recognizes any kind of existing comment and aligns its text
383 appropriately; if there is no existing comment, a comment is inserted
384 and aligned. Inserting and aligning comments are not the same in
385 Fortran mode as in other modes.
387 When a new comment must be inserted, if the current line is blank, a
388 full-line comment is inserted. On a non-blank line, a nonstandard @samp{!}
389 comment is inserted if you have said you want to use them. Otherwise a
390 full-line comment is inserted on a new line before the current line.
392 Nonstandard @samp{!} comments are aligned like comments in other
393 languages, but full-line comments are different. In a standard full-line
394 comment, the comment delimiter itself must always appear in column zero.
395 What can be aligned is the text within the comment. You can choose from
396 three styles of alignment by setting the variable
397 @code{fortran-comment-indent-style} to one of these values:
399 @vindex fortran-comment-indent-style
400 @vindex fortran-comment-line-extra-indent
403 Align the text at a fixed column, which is the sum of
404 @code{fortran-comment-line-extra-indent} and the minimum statement
405 indentation. This is the default.
407 The minimum indentation is
408 @code{fortran-minimum-statement-indent-tab} for tab format
409 continuation line style and @code{fortran-minimum-statement-indent-fixed}
410 for fixed form style.
413 Align the text as if it were a line of code, but with an additional
414 @code{fortran-comment-line-extra-indent} columns of indentation.
417 Don't move text in full-line comments automatically.
420 @vindex fortran-comment-indent-char
421 In addition, you can specify the character to be used to indent within
422 full-line comments by setting the variable
423 @code{fortran-comment-indent-char} to the single-character string you want
426 @vindex fortran-directive-re
427 Compiler directive lines, or preprocessor lines, have much the same
428 appearance as comment lines. It is important, though, that such lines
429 never be indented at all, no matter what the value of
430 @code{fortran-comment-indent-style}. The variable
431 @code{fortran-directive-re} is a regular expression that specifies which
432 lines are directives. Matching lines are never indented, and receive
433 distinctive font-locking.
435 The normal Emacs comment command @kbd{C-x ;} (@code{comment-set-column})
436 has not been redefined. If you use @samp{!} comments, this command
437 can be used with them. Otherwise it is useless in Fortran mode.
439 @kindex C-c ; @r{(Fortran mode)}
440 @findex fortran-comment-region
441 @vindex fortran-comment-region
442 The command @kbd{C-c ;} (@code{fortran-comment-region}) turns all the
443 lines of the region into comments by inserting the string @samp{c$$$} at
444 the front of each one. With a numeric argument, it turns the region
445 back into live code by deleting @samp{c$$$} from the front of each line
446 in it. The string used for these comments can be controlled by setting
447 the variable @code{fortran-comment-region}. Note that here we have an
448 example of a command and a variable with the same name; these two uses
449 of the name never conflict because in Lisp and in Emacs it is always
450 clear from the context which one is meant.
452 @node Fortran Autofill
453 @subsection Auto Fill in Fortran Mode
455 Fortran mode has specialized support for Auto Fill mode, which is a
456 minor mode that automatically splits statements as you insert them
457 when they become too wide. Splitting a statement involves making
458 continuation lines using @code{fortran-continuation-string}
459 (@pxref{ForIndent Cont}). This splitting happens when you type
460 @key{SPC}, @key{RET}, or @key{TAB}, and also in the Fortran
461 indentation commands. You activate Auto Fill in Fortran mode in the
464 @xref{Auto Fill,,, emacs, the Emacs Manual}.
470 @vindex fortran-break-before-delimiters
471 Auto Fill breaks lines at spaces or delimiters when the lines get
472 longer than the desired width (the value of @code{fill-column}). The
473 delimiters (besides whitespace) that Auto Fill can break at are
474 @samp{+}, @samp{-}, @samp{/}, @samp{*}, @samp{=}, @samp{<}, @samp{>},
475 and @samp{,}. The line break comes after the delimiter if the
476 variable @code{fortran-break-before-delimiters} is @code{nil}.
477 Otherwise (and by default), the break comes before the delimiter.
479 To enable Auto Fill in all Fortran buffers, add
480 @code{auto-fill-mode} to @code{fortran-mode-hook}.
482 @xref{Hooks,,, emacs, the Emacs Manual}.
488 @node Fortran Columns
489 @subsection Checking Columns in Fortran
491 @vindex fortran-line-length
492 In standard Fortran 77, anything beyond column 72 is ignored.
493 Most compilers provide an option to change this (for example,
494 @samp{-ffixed-line-length-N} in gfortran). Customize the variable
495 @code{fortran-line-length} to change the line length in Fortran mode.
496 Anything beyond this point is font-locked as a comment. (Unless it is
497 inside a string: strings that extend beyond @code{fortran-line-length}
498 will confuse font-lock.)
502 Display a ``column ruler'' momentarily above the current line
503 (@code{fortran-column-ruler}).
505 Split the current window horizontally temporarily so that it is
506 @code{fortran-line-length} columns wide
507 (@code{fortran-window-create-momentarily}). This may help you avoid
508 making lines longer than the limit imposed by your Fortran compiler.
510 Split the current window horizontally so that it is
511 @code{fortran-line-length} columns wide (@code{fortran-window-create}).
512 You can then continue editing.
513 @item M-x fortran-strip-sequence-nos
514 Delete all text in column @code{fortran-line-length} and beyond.
517 @kindex C-c C-r @r{(Fortran mode)}
518 @findex fortran-column-ruler
519 The command @kbd{C-c C-r} (@code{fortran-column-ruler}) shows a column
520 ruler momentarily above the current line. The comment ruler is two lines
521 of text that show you the locations of columns with special significance in
522 Fortran programs. Square brackets show the limits of the columns for line
523 numbers, and curly brackets show the limits of the columns for the
524 statement body. Column numbers appear above them.
526 Note that the column numbers count from zero, as always in GNU Emacs.
527 As a result, the numbers may be one less than those you are familiar
528 with; but the positions they indicate in the line are standard for
531 @vindex fortran-column-ruler-fixed
532 @vindex fortran-column-ruler-tabs
533 The text used to display the column ruler depends on the value of the
534 variable @code{indent-tabs-mode}. If @code{indent-tabs-mode} is
535 @code{nil}, then the value of the variable
536 @code{fortran-column-ruler-fixed} is used as the column ruler.
537 Otherwise, the value of the variable @code{fortran-column-ruler-tab} is
538 displayed. By changing these variables, you can change the column ruler
541 @kindex C-c C-w @r{(Fortran mode)}
542 @findex fortran-window-create-momentarily
543 @kbd{C-c C-w} (@code{fortran-window-create-momentarily}) temporarily
544 splits the current window horizontally, making a window
545 @code{fortran-line-length} columns wide, so you can see any lines that
546 are too long. Type a space to restore the normal width.
548 @kindex C-u C-c C-w @r{(Fortran mode)}
549 @findex fortran-window-create
550 You can also split the window horizontally and continue editing with
551 the split in place. To do this, use @kbd{C-u C-c C-w} (@code{M-x
552 fortran-window-create}). By editing in this window you can
553 immediately see when you make a line too wide to be correct Fortran.
555 @findex fortran-strip-sequence-nos
556 The command @kbd{M-x fortran-strip-sequence-nos} deletes all text in
557 column @code{fortran-line-length} and beyond, on all lines in the
558 current buffer. This is the easiest way to get rid of old sequence
562 @subsection Fortran Keyword Abbrevs
564 Fortran mode provides many built-in abbrevs for common keywords and
565 declarations. These are the same sort of abbrev that you can define
566 yourself. To use them, you must turn on Abbrev mode.
568 @xref{Abbrevs,,, emacs, the Emacs Manual}.
574 The built-in abbrevs are unusual in one way: they all start with a
575 semicolon. For example, one built-in Fortran abbrev is @samp{;c} for
576 @samp{continue}. If you insert @samp{;c} and then insert a punctuation
577 character such as a space or a newline, the @samp{;c} expands automatically
578 to @samp{continue}, provided Abbrev mode is enabled.
580 Type @samp{;?} or @samp{;C-h} to display a list of all the built-in
581 Fortran abbrevs and what they stand for.