26.14.2 Fortran Indentation
Special commands and features are needed for indenting fixed (or tab) form Fortran code in order to make sure various syntactic entities (line numbers, comment line indicators and continuation line flags) appear in the required columns.
• Commands |   | Commands for indenting and filling Fortran. |
• Contline |   | How continuation lines indent. |
• Numbers |   | How line numbers auto-indent. |
• Conv |   | Conventions you must obey to avoid trouble. |
• Vars |   | Variables controlling Fortran indent style. |
26.14.2.1 Fortran Indentation and Filling Commands​
C-M-j
​
Break the current line at point and set up a continuation line (fortran-split-line
).
M-^
​
Join this line to the previous line (fortran-join-line
).
C-M-q
​
Indent all the lines of the subprogram that point is in (fortran-indent-subprogram
).
M-q
​
Fill a comment block or statement (using fortran-fill-paragraph
or fortran-fill-statement
).
The key C-M-q
runs fortran-indent-subprogram
, a command to reindent all the lines of the Fortran subprogram (function or subroutine) containing point.
The key C-M-j
runs fortran-split-line
, which splits a line in the appropriate fashion for Fortran. In a non-comment line, the second half becomes a continuation line and is indented accordingly. In a comment line, both halves become separate comment lines.
M-^
or C-c C-d
run the command fortran-join-line
, which joins a continuation line back to the previous line, roughly as the inverse of fortran-split-line
. The point must be on a continuation line when this command is invoked.
M-q
in Fortran mode fills the comment block or statement that point is in. This removes any excess statement continuations.
26.14.2.2 Continuation Lines​
Most Fortran 77 compilers allow two ways of writing continuation lines. If the first non-space character on a line is in column 5, then that line is a continuation of the previous line. We call this fixed form. (In GNU Emacs we always count columns from 0; but note that the Fortran standard counts from 1. You can customize the variable column-number-indicator-zero-based
to make the column display Fortran-like; see Optional Mode Line.) The variable fortran-continuation-string
specifies what character to put in column 5. A line that starts with a tab character followed by any digit except ‘0
’ is also a continuation line. We call this style of continuation tab format. (Fortran 90 introduced free-form continuation lines.)
Fortran mode can use either style of continuation line. When you enter Fortran mode, it tries to deduce the proper continuation style automatically from the buffer contents. It does this by scanning up to fortran-analyze-depth
(default 100) lines from the start of the buffer. The first line that begins with either a tab character or six spaces determines the choice. If the scan fails (for example, if the buffer is new and therefore empty), the value of fortran-tab-mode-default
(nil
for fixed form, and non-nil
for tab format) is used. ‘/t
’ (fortran-tab-mode-string
) in the mode line indicates tab format is selected. Fortran mode sets the value of indent-tabs-mode
accordingly.
If the text on a line starts with the Fortran continuation marker ‘$
’, or if it begins with any non-whitespace character in column 5, Fortran mode treats it as a continuation line. When you indent a continuation line with TAB
, it converts the line to the current continuation style. When you split a Fortran statement with C-M-j
, the continuation marker on the newline is created according to the continuation style.
The setting of continuation style affects several other aspects of editing in Fortran mode. In fixed form mode, the minimum column number for the body of a statement is 6. Lines inside of Fortran blocks that are indented to larger column numbers must use only the space character for whitespace. In tab format mode, the minimum column number for the statement body is 8, and the whitespace before column 8 must consist of one tab character.
26.14.2.3 Line Numbers​
If a number is the first non-whitespace in the line, Fortran indentation assumes it is a line number and moves it to columns 0 through 4. (Columns always count from 0 in Emacs, but setting column-number-indicator-zero-based
to nil
can change that, see Optional Mode Line.)
Line numbers of four digits or less are normally indented one space. The variable fortran-line-number-indent
controls this; it specifies the maximum indentation a line number can have. The default value of the variable is 1. Fortran mode tries to prevent line number digits passing column 4, reducing the indentation below the specified maximum if necessary. If fortran-line-number-indent
has the value 5, line numbers are right-justified to end in column 4.
Simply inserting a line number is enough to indent it according to these rules. As each digit is inserted, the indentation is recomputed. To turn off this feature, set the variable fortran-electric-line-number
to nil
.
26.14.2.4 Syntactic Conventions​
Fortran mode assumes that you follow certain conventions that simplify the task of understanding a Fortran program well enough to indent it properly:
Two nested ‘
do
’ loops never share a ‘continue
’ statement.Fortran keywords such as ‘
if
’, ‘else
’, ‘then
’, ‘do
’ and others are written without embedded whitespace or line breaks.Fortran compilers generally ignore whitespace outside of string constants, but Fortran mode does not recognize these keywords if they are not contiguous. Constructs such as ‘
else if
’ or ‘end do
’ are acceptable, but the second word should be on the same line as the first and not on a continuation line.
If you fail to follow these conventions, the indentation commands may indent some lines unaesthetically. However, a correct Fortran program retains its meaning when reindented even if the conventions are not followed.
26.14.2.5 Variables for Fortran Indentation​
Several additional variables control how Fortran indentation works:
fortran-do-indent
​
Extra indentation within each level of ‘do
’ statement (default 3).
fortran-if-indent
​
Extra indentation within each level of ‘if
’, ‘select case
’, or ‘where
’ statements (default 3).
fortran-structure-indent
​
Extra indentation within each level of ‘structure
’, ‘union
’, ‘map
’, or ‘interface
’ statements (default 3).
fortran-continuation-indent
​
Extra indentation for bodies of continuation lines (default 5).
fortran-check-all-num-for-matching-do
​
In Fortran 77, a numbered ‘do
’ statement is terminated by any statement with a matching line number. It is common (but not compulsory) to use a ‘continue
’ statement for this purpose. If this variable has a non-nil
value, indenting any numbered statement must check for a ‘do
’ that ends there. If you always end ‘do
’ statements with a ‘continue
’ line (or if you use the more modern ‘enddo
’), then you can speed up indentation by setting this variable to nil
(the default).
fortran-blink-matching-if
​
If this is t
, indenting an ‘endif
’ (or ‘enddo
’) statement moves the cursor momentarily to the matching ‘if
’ (or ‘do
’) statement to show where it is. The default is nil
.
fortran-minimum-statement-indent-fixed
​
Minimum indentation for Fortran statements when using fixed form continuation line style. Statement bodies are never indented by less than this. The default is 6.
fortran-minimum-statement-indent-tab
​
Minimum indentation for Fortran statements for tab format continuation line style. Statement bodies are never indented by less than this. The default is 8.
The following section describes the variables controlling the indentation of comments.