Fortran Mode
============

Fortran mode is meant for editing ``fixed form'' (and also ``tab format'') source code (normally Fortran 77).  For editing more modern ``free form'' source code (Fortran 90, 95, 2003, 2008), use F90 mode (f90-mode).  Emacs normally uses Fortran mode for files with extension .f, .F or .for, and F90 mode for the extensions .f90, .f95, .f03 and .f08. Customize auto-mode-alist to add more extensions.  GNU Fortran supports both free and fixed form.  This manual mainly documents Fortran mode, but the corresponding F90 mode features are mentioned when revelant.

Fortran mode provides special motion commands for Fortran statements and subprograms, and indentation commands that understand Fortran conventions of nesting, line numbers and continuation statements. Fortran mode has support for Auto Fill mode that breaks long lines into proper Fortran continuation lines.  Fortran mode also supports Hideshow minor mode (see Hideshow), and Imenu (see Imenu).

Special commands for comments are provided because Fortran comments are unlike those of other languages.  Built-in abbrevs optionally save typing when you insert Fortran keywords.

Use M-x fortran-mode to switch to this major mode.  This command runs the hook fortran-mode-hook. See Hooks.

Motion Commands
---------------

In addition to the normal commands for moving by and operating on ``defuns'' (Fortran subprograms---functions and subroutines, using the commands fortran-beginning-of-subprogram and fortran-end-of-subprogram; as well as modules for F90 mode), Fortran mode provides special commands to move by statements and other program units.

C-c C-n
     Move to the beginning of the next statement (fortran-next-statement/f90-next-statement).

C-c C-p
     Move to the beginning of the previous statement (fortran-previous-statement/f90-previous-statement). If there is no previous statement (i.e. if called from the first statement in the buffer), move to the start of the buffer.

C-c C-e
     Move point forward to the start of the next code block, or the end of the current block, whichever is encountered first. (f90-next-block).  A code block is a subroutine, if--endif statement, and so forth.  This command exists for F90 mode only, not Fortran mode.  With a numeric argument, this moves forward that many blocks.

C-c C-a
     Move point backward to the previous code block (f90-previous-block).  This is like f90-next-block, but moves backwards.

C-M-n
     Move to the end of the current code block (fortran-end-of-block/f90-end-of-block).  With a numeric argument, move forward that number of blocks.  The mark is set before moving point.  The F90 mode version of this command checks for consistency of block types and labels (if present), but it does not check the outermost block since that may be incomplete.

C-M-p
     Move to the start of the current code block (fortran-beginning-of-block/f90-beginning-of-block). This is like fortran-end-of-block, but moves backwards.

The commands fortran-beginning-of-subprogram and fortran-end-of-subprogram move to the start or end of the current subprogram, respectively.  The commands fortran-mark-do and fortran-mark-if mark the end of the current do or if block, and move point to the start.

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.

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 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 runs 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.

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.)  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,'' with another style of 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 always 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 always consist of one tab character.

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 GNU Emacs.)

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.

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.

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 ended 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 is nil.

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 less than this much.  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 less than this much.  The default is 8.

The variables controlling the indentation of comments are described in the following section.

Fortran Comments
----------------

The usual Emacs comment commands assume that a comment can follow a line of code.  In Fortran 77, the standard comment syntax requires an entire line to be just a comment.  Therefore, Fortran mode replaces the standard Emacs comment commands and defines some new variables.

Fortran mode can also handle the Fortran 90 comment syntax where comments start with ! and can follow other text.  Because only some Fortran 77 compilers accept this syntax, Fortran mode will not insert such comments unless you have said in advance to do so.  To do this, set the variable fortran-comment-line-start to "!". If you use an unusual value, you may also need to adjust fortran-comment-line-start-skip.

M-;
     Align comment or insert new comment (comment-dwim).

C-x ;
     Applies to nonstandard ! comments only.

C-c ;
     Turn all lines of the region into comments, or (with argument) turn them back into real code (fortran-comment-region).

M-; in Fortran mode runs the standard comment-dwim. This recognizes any kind of existing comment and aligns its text appropriately; if there is no existing comment, a comment is inserted and aligned.  Inserting and aligning comments are not the same in Fortran mode as in other modes.

When a new comment must be inserted, if the current line is blank, a full-line comment is inserted.  On a non-blank line, a nonstandard ! comment is inserted if you have said you want to use them.  Otherwise a full-line comment is inserted on a new line before the current line.

Nonstandard ! comments are aligned like comments in other languages, but full-line comments are different.  In a standard full-line comment, the comment delimiter itself must always appear in column zero. What can be aligned is the text within the comment.  You can choose from three styles of alignment by setting the variable fortran-comment-indent-style to one of these values:

fixed
     Align the text at a fixed column, which is the sum of fortran-comment-line-extra-indent and the minimum statement indentation.  This is the default.

     The minimum statement indentation is fortran-minimum-statement-indent-fixed for fixed form continuation line style and fortran-minimum-statement-indent-tab for tab format style.

relative
     Align the text as if it were a line of code, but with an additional fortran-comment-line-extra-indent columns of indentation.

nil
     Don't move text in full-line comments automatically.

In addition, you can specify the character to be used to indent within full-line comments by setting the variable fortran-comment-indent-char to the single-character string you want to use.

Compiler directive lines, or preprocessor lines, have much the same appearance as comment lines.  It is important, though, that such lines never be indented at all, no matter what the value of fortran-comment-indent-style.  The variable fortran-directive-re is a regular expression that specifies which lines are directives.  Matching lines are never indented, and receive distinctive font-locking.

The normal Emacs comment command C-x ; has not been redefined.  If you use ! comments, this command can be used with them.  Otherwise it is useless in Fortran mode.

The command C-c ; (fortran-comment-region) turns all the lines of the region into comments by inserting the string C$$$ at the front of each one.  With a numeric argument, it turns the region back into live code by deleting C$$$ from the front of each line in it.  The string used for these comments can be controlled by setting the variable fortran-comment-region.  Note that here we have an example of a command and a variable with the same name; these two uses of the name never conflict because in Lisp and in Emacs it is always clear from the context which one is meant.

Auto Fill in Fortran Mode
-------------------------

Fortran mode has specialized support for Auto Fill mode, which is a minor mode that automatically splits statements as you insert them when they become too wide.  Splitting a statement involves making continuation lines using fortran-continuation-string (see ForIndent Cont).  This splitting happens when you type SPC, RET, or TAB, and also in the Fortran indentation commands.  You activate Auto Fill in Fortran mode in the normal way. See Auto Fill.

Auto Fill breaks lines at spaces or delimiters when the lines get longer than the desired width (the value of fill-column).  The delimiters (besides whitespace) that Auto Fill can break at are +, -, /, *, =, <, >, and ,.  The line break comes after the delimiter if the variable fortran-break-before-delimiters is nil. Otherwise (and by default), the break comes before the delimiter.

To enable Auto Fill in all Fortran buffers, add auto-fill-mode to fortran-mode-hook. See Hooks.

Checking Columns in Fortran
---------------------------

In standard Fortran 77, anything beyond column 72 is ignored. Most compilers provide an option to change this (for example, -ffixed-line-length-N in gfortran).  Customize the variable fortran-line-length to change the line length in Fortran mode. Anything beyond this point is font-locked as a comment.  (Unless it is inside a string: strings that extend beyond fortran-line-length will confuse font-lock.)

C-c C-r
     Display a ``column ruler'' momentarily above the current line (fortran-column-ruler).

C-c C-w
     Split the current window horizontally temporarily so that it is fortran-line-length columns wide (fortran-window-create-momentarily).  This may help you avoid making lines longer than the character limit imposed by your Fortran compiler.

C-u C-c C-w
     Split the current window horizontally so that it is fortran-line-length columns wide (fortran-window-create). You can then continue editing.

M-x fortran-strip-sequence-nos
     Delete all text in column fortran-line-length and beyond.

The command C-c C-r (fortran-column-ruler) shows a column ruler momentarily above the current line.  The comment ruler is two lines of text that show you the locations of columns with special significance in Fortran programs.  Square brackets show the limits of the columns for line numbers, and curly brackets show the limits of the columns for the statement body.  Column numbers appear above them.

Note that the column numbers count from zero, as always in GNU Emacs. As a result, the numbers may be one less than those you are familiar with; but the positions they indicate in the line are standard for Fortran.

The text used to display the column ruler depends on the value of the variable indent-tabs-mode.  If indent-tabs-mode is nil, then the value of the variable fortran-column-ruler-fixed is used as the column ruler. Otherwise, the value of the variable fortran-column-ruler-tab is displayed.  By changing these variables, you can change the column ruler display.

C-c C-w (fortran-window-create-momentarily) temporarily splits the current window horizontally, making a window fortran-line-length columns wide, so you can see any lines that are too long.  Type a space to restore the normal width.

You can also split the window horizontally and continue editing with the split in place.  To do this, use C-u C-c C-w (M-x fortran-window-create).  By editing in this window you can immediately see when you make a line too wide to be correct Fortran.

The command M-x fortran-strip-sequence-nos deletes all text in column fortran-line-length and beyond, on all lines in the current buffer.  This is the easiest way to get rid of old sequence numbers.

Fortran Keyword Abbrevs
-----------------------

Fortran mode provides many built-in abbrevs for common keywords and declarations.  These are the same sort of abbrev that you can define yourself.  To use them, you must turn on Abbrev mode. See Abbrevs.

The built-in abbrevs are unusual in one way: they all start with a semicolon.  For example, one built-in Fortran abbrev is ;c for continue.  If you insert ;c and then insert a punctuation character such as a space or a newline, the ;c expands automatically to continue, provided Abbrev mode is enabled.

Type ;? or ;C-h to display a list of all the built-in Fortran abbrevs and what they stand for.