1 @c This is part of the Emacs manual.
2 @c Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 @c Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
6 @c This file is included either in emacs-xtra.texi (when producing the
7 @c printed version) or in the main Emacs manual (for the on-line version).
13 @cindex Fortran fixed form and free form
14 @cindex Fortran 77 and Fortran 90, 95, 2003
17 Fortran mode is meant for editing ``fixed form'' (and also ``tab
18 format'') source code (normally Fortran 77). For editing more modern
19 ``free form'' source code (Fortran 90, 95, 2003), use F90 mode
20 (@code{f90-mode}). Emacs normally uses Fortran mode for files with
21 extension @samp{.f}, @samp{.F} or @samp{.for}, and F90 mode for the
22 extensions @samp{.f90} and @samp{.f95}. Customize
23 @code{auto-mode-alist} to add more extensions. GNU Fortran supports
24 both free and fixed form. This manual mainly documents Fortran mode,
25 but the corresponding F90 mode features are mentioned when revelant.
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, using the
74 commands @code{fortran-beginning-of-subprogram} and
75 @code{fortran-end-of-subprogram}; as well as modules for F90 mode),
76 Fortran mode provides special commands to move by statements and other
80 @kindex C-c C-n @r{(Fortran mode)}
81 @findex fortran-next-statement
82 @findex f90-next-statement
84 Move to the beginning of the next statement
85 (@code{fortran-next-statement}/@code{f90-next-statement}).
87 @kindex C-c C-p @r{(Fortran mode)}
88 @findex fortran-previous-statement
89 @findex f90-previous-statement
91 Move to the beginning of the previous statement
92 (@code{fortran-previous-statement}/@code{f90-previous-statement}).
93 If there is no previous statement (i.e. if called from the first
94 statement in the buffer), move to the start of the buffer.
96 @kindex C-c C-e @r{(F90 mode)}
97 @findex f90-next-block
99 Move point forward to the start of the next code block, or the end of
100 the current block, whichever is encountered first.
101 (@code{f90-next-block}). A code block is a subroutine,
102 @code{if}--@code{endif} statement, and so forth. This command exists
103 for F90 mode only, not Fortran mode. With a numeric argument, this
104 moves forward that many blocks.
106 @kindex C-c C-a @r{(F90 mode)}
107 @findex f90-previous-block
109 Move point backward to the previous code block
110 (@code{f90-previous-block}). This is like @code{f90-next-block}, but
113 @kindex C-M-n @r{(Fortran mode)}
114 @findex fortran-end-of-block
115 @findex f90-end-of-block
117 Move to the end of the current code block
118 (@code{fortran-end-of-block}/@code{f90-end-of-block}). With a numeric
119 argument, move forward that number of blocks. The mark is set before
120 moving point. The F90 mode version of this command checks for
121 consistency of block types and labels (if present), but it does not
122 check the outermost block since that may be incomplete.
124 @kindex C-M-p @r{(Fortran mode)}
125 @findex fortran-beginning-of-block
126 @findex f90-beginning-of-block
128 Move to the start of the current code block
129 (@code{fortran-beginning-of-block}/@code{f90-beginning-of-block}). This
130 is like @code{fortran-end-of-block}, but moves backwards.
133 The commands @code{fortran-beginning-of-subprogram} and
134 @code{fortran-end-of-subprogram} move to the start or end of the
135 current subprogram, respectively. The commands @code{fortran-mark-do}
136 and @code{fortran-mark-if} mark the end of the current @code{do} or
137 @code{if} block, and move point to the start.
141 @subsection Fortran Indentation
143 Special commands and features are needed for indenting fixed (or tab)
144 form Fortran code in order to make sure various syntactic entities (line
145 numbers, comment line indicators and continuation line flags) appear in
146 the required columns.
149 * Commands: ForIndent Commands. Commands for indenting and filling Fortran.
150 * Contline: ForIndent Cont. How continuation lines indent.
151 * Numbers: ForIndent Num. How line numbers auto-indent.
152 * Conv: ForIndent Conv. Conventions you must obey to avoid trouble.
153 * Vars: ForIndent Vars. Variables controlling Fortran indent style.
156 @node ForIndent Commands
157 @subsubsection Fortran Indentation and Filling Commands
161 Break the current line at point and set up a continuation line
162 (@code{fortran-split-line}).
164 Join this line to the previous line (@code{fortran-join-line}).
166 Indent all the lines of the subprogram point is in
167 (@code{fortran-indent-subprogram}).
169 Fill a comment block or statement (using @code{fortran-fill-paragraph}
170 or @code{fortran-fill-statement}).
173 @kindex C-M-q @r{(Fortran mode)}
174 @findex fortran-indent-subprogram
175 The key @kbd{C-M-q} runs @code{fortran-indent-subprogram}, a command
176 to reindent all the lines of the Fortran subprogram (function or
177 subroutine) containing point.
179 @kindex C-M-j @r{(Fortran mode)}
180 @findex fortran-split-line
181 The key @kbd{C-M-j} runs @code{fortran-split-line}, which splits
182 a line in the appropriate fashion for Fortran. In a non-comment line,
183 the second half becomes a continuation line and is indented
184 accordingly. In a comment line, both halves become separate comment
187 @kindex M-^ @r{(Fortran mode)}
188 @kindex C-c C-d @r{(Fortran mode)}
189 @findex fortran-join-line
190 @kbd{M-^} or @kbd{C-c C-d} runs the command @code{fortran-join-line},
191 which joins a continuation line back to the previous line, roughly as
192 the inverse of @code{fortran-split-line}. The point must be on a
193 continuation line when this command is invoked.
195 @kindex M-q @r{(Fortran mode)}
196 @kbd{M-q} in Fortran mode fills the comment block or statement that
197 point is in. This removes any excess statement continuations.
200 @subsubsection Continuation Lines
201 @cindex Fortran continuation lines
203 @vindex fortran-continuation-string
204 Most Fortran 77 compilers allow two ways of writing continuation lines.
205 If the first non-space character on a line is in column 5, then that
206 line is a continuation of the previous line. We call this @dfn{fixed
207 form}. (In GNU Emacs we always count columns from 0; but note that
208 the Fortran standard counts from 1.) The variable
209 @code{fortran-continuation-string} specifies what character to put in
210 column 5. A line that starts with a tab character followed by any digit
211 except @samp{0} is also a continuation line. We call this style of
212 continuation @dfn{tab format}. (Fortran 90 introduced ``free form,''
213 with another style of continuation lines).
215 @vindex indent-tabs-mode @r{(Fortran mode)}
216 @vindex fortran-analyze-depth
217 @vindex fortran-tab-mode-default
218 Fortran mode can use either style of continuation line. When you
219 enter Fortran mode, it tries to deduce the proper continuation style
220 automatically from the buffer contents. It does this by scanning up to
221 @code{fortran-analyze-depth} (default 100) lines from the start of the
222 buffer. The first line that begins with either a tab character or six
223 spaces determines the choice. If the scan fails (for example, if the
224 buffer is new and therefore empty), the value of
225 @code{fortran-tab-mode-default} (@code{nil} for fixed form, and
226 non-@code{nil} for tab format) is used. @samp{/t}
227 (@code{fortran-tab-mode-string}) in the mode line indicates tab format
228 is selected. Fortran mode sets the value of @code{indent-tabs-mode}
231 If the text on a line starts with the Fortran continuation marker
232 @samp{$}, or if it begins with any non-whitespace character in column
233 5, Fortran mode treats it as a continuation line. When you indent a
234 continuation line with @key{TAB}, it converts the line to the current
235 continuation style. When you split a Fortran statement with
236 @kbd{C-M-j}, the continuation marker on the newline is created according
237 to the continuation style.
239 The setting of continuation style affects several other aspects of
240 editing in Fortran mode. In fixed form mode, the minimum column
241 number for the body of a statement is 6. Lines inside of Fortran
242 blocks that are indented to larger column numbers always use only the
243 space character for whitespace. In tab format mode, the minimum
244 column number for the statement body is 8, and the whitespace before
245 column 8 must always consist of one tab character.
248 @subsubsection Line Numbers
250 If a number is the first non-whitespace in the line, Fortran
251 indentation assumes it is a line number and moves it to columns 0
252 through 4. (Columns always count from 0 in GNU Emacs.)
254 @vindex fortran-line-number-indent
255 Line numbers of four digits or less are normally indented one space.
256 The variable @code{fortran-line-number-indent} controls this; it
257 specifies the maximum indentation a line number can have. The default
258 value of the variable is 1. Fortran mode tries to prevent line number
259 digits passing column 4, reducing the indentation below the specified
260 maximum if necessary. If @code{fortran-line-number-indent} has the
261 value 5, line numbers are right-justified to end in column 4.
263 @vindex fortran-electric-line-number
264 Simply inserting a line number is enough to indent it according to
265 these rules. As each digit is inserted, the indentation is recomputed.
266 To turn off this feature, set the variable
267 @code{fortran-electric-line-number} to @code{nil}.
271 @subsubsection Syntactic Conventions
273 Fortran mode assumes that you follow certain conventions that simplify
274 the task of understanding a Fortran program well enough to indent it
279 Two nested @samp{do} loops never share a @samp{continue} statement.
282 Fortran keywords such as @samp{if}, @samp{else}, @samp{then}, @samp{do}
283 and others are written without embedded whitespace or line breaks.
285 Fortran compilers generally ignore whitespace outside of string
286 constants, but Fortran mode does not recognize these keywords if they
287 are not contiguous. Constructs such as @samp{else if} or @samp{end do}
288 are acceptable, but the second word should be on the same line as the
289 first and not on a continuation line.
293 If you fail to follow these conventions, the indentation commands may
294 indent some lines unaesthetically. However, a correct Fortran program
295 retains its meaning when reindented even if the conventions are not
299 @subsubsection Variables for Fortran Indentation
301 @vindex fortran-do-indent
302 @vindex fortran-if-indent
303 @vindex fortran-structure-indent
304 @vindex fortran-continuation-indent
305 @vindex fortran-check-all-num@dots{}
306 @vindex fortran-minimum-statement-indent@dots{}
307 Several additional variables control how Fortran indentation works:
310 @item fortran-do-indent
311 Extra indentation within each level of @samp{do} statement (default 3).
313 @item fortran-if-indent
314 Extra indentation within each level of @samp{if}, @samp{select case}, or
315 @samp{where} statements (default 3).
317 @item fortran-structure-indent
318 Extra indentation within each level of @samp{structure}, @samp{union},
319 @samp{map}, or @samp{interface} statements (default 3).
321 @item fortran-continuation-indent
322 Extra indentation for bodies of continuation lines (default 5).
324 @item fortran-check-all-num-for-matching-do
325 In Fortran 77, a numbered @samp{do} statement is ended by any statement
326 with a matching line number. It is common (but not compulsory) to use a
327 @samp{continue} statement for this purpose. If this variable has a
328 non-@code{nil} value, indenting any numbered statement must check for a
329 @samp{do} that ends there. If you always end @samp{do} statements with
330 a @samp{continue} line (or if you use the more modern @samp{enddo}),
331 then you can speed up indentation by setting this variable to
332 @code{nil}. The default is @code{nil}.
334 @item fortran-blink-matching-if
335 If this is @code{t}, indenting an @samp{endif} (or @samp{enddo}
336 statement moves the cursor momentarily to the matching @samp{if} (or
337 @samp{do}) statement to show where it is. The default is @code{nil}.
339 @item fortran-minimum-statement-indent-fixed
340 Minimum indentation for Fortran statements when using fixed form
341 continuation line style. Statement bodies are never indented less than
342 this much. The default is 6.
344 @item fortran-minimum-statement-indent-tab
345 Minimum indentation for Fortran statements for tab format continuation line
346 style. Statement bodies are never indented less than this much. The
350 The variables controlling the indentation of comments are described in
351 the following section.
353 @node Fortran Comments
354 @subsection Fortran Comments
356 The usual Emacs comment commands assume that a comment can follow a
357 line of code. In Fortran 77, the standard comment syntax requires an
358 entire line to be just a comment. Therefore, Fortran mode replaces the
359 standard Emacs comment commands and defines some new variables.
361 @vindex fortran-comment-line-start
362 Fortran mode can also handle the Fortran 90 comment syntax where
363 comments start with @samp{!} and can follow other text. Because only
364 some Fortran 77 compilers accept this syntax, Fortran mode will not
365 insert such comments unless you have said in advance to do so. To do
366 this, set the variable @code{fortran-comment-line-start} to @samp{"!"}.
367 If you use an unusual value, you may also need to adjust
368 @code{fortran-comment-line-start-skip}.
373 Align comment or insert new comment (@code{comment-dwim}).
376 Applies to nonstandard @samp{!} comments only.
379 Turn all lines of the region into comments, or (with argument) turn them back
380 into real code (@code{fortran-comment-region}).
383 @kbd{M-;} in Fortran mode runs the standard @code{comment-dwim}.
384 This recognizes any kind of existing comment and aligns its text
385 appropriately; if there is no existing comment, a comment is inserted
386 and aligned. Inserting and aligning comments are not the same in
387 Fortran mode as in other modes.
389 When a new comment must be inserted, if the current line is blank, a
390 full-line comment is inserted. On a non-blank line, a nonstandard @samp{!}
391 comment is inserted if you have said you want to use them. Otherwise a
392 full-line comment is inserted on a new line before the current line.
394 Nonstandard @samp{!} comments are aligned like comments in other
395 languages, but full-line comments are different. In a standard full-line
396 comment, the comment delimiter itself must always appear in column zero.
397 What can be aligned is the text within the comment. You can choose from
398 three styles of alignment by setting the variable
399 @code{fortran-comment-indent-style} to one of these values:
401 @vindex fortran-comment-indent-style
402 @vindex fortran-comment-line-extra-indent
405 Align the text at a fixed column, which is the sum of
406 @code{fortran-comment-line-extra-indent} and the minimum statement
407 indentation. This is the default.
409 The minimum statement indentation is
410 @code{fortran-minimum-statement-indent-fixed} for fixed form
411 continuation line style and @code{fortran-minimum-statement-indent-tab}
412 for tab format style.
415 Align the text as if it were a line of code, but with an additional
416 @code{fortran-comment-line-extra-indent} columns of indentation.
419 Don't move text in full-line comments automatically.
422 @vindex fortran-comment-indent-char
423 In addition, you can specify the character to be used to indent within
424 full-line comments by setting the variable
425 @code{fortran-comment-indent-char} to the single-character string you want
428 @vindex fortran-directive-re
429 Compiler directive lines, or preprocessor lines, have much the same
430 appearance as comment lines. It is important, though, that such lines
431 never be indented at all, no matter what the value of
432 @code{fortran-comment-indent-style}. The variable
433 @code{fortran-directive-re} is a regular expression that specifies which
434 lines are directives. Matching lines are never indented, and receive
435 distinctive font-locking.
437 The normal Emacs comment command @kbd{C-x ;} has not been redefined. If
438 you use @samp{!} comments, this command can be used with them. Otherwise
439 it is useless in Fortran mode.
441 @kindex C-c ; @r{(Fortran mode)}
442 @findex fortran-comment-region
443 @vindex fortran-comment-region
444 The command @kbd{C-c ;} (@code{fortran-comment-region}) turns all the
445 lines of the region into comments by inserting the string @samp{C$$$} at
446 the front of each one. With a numeric argument, it turns the region
447 back into live code by deleting @samp{C$$$} from the front of each line
448 in it. The string used for these comments can be controlled by setting
449 the variable @code{fortran-comment-region}. Note that here we have an
450 example of a command and a variable with the same name; these two uses
451 of the name never conflict because in Lisp and in Emacs it is always
452 clear from the context which one is meant.
454 @node Fortran Autofill
455 @subsection Auto Fill in Fortran Mode
457 Fortran mode has specialized support for Auto Fill mode, which is a
458 minor mode that automatically splits statements as you insert them
459 when they become too wide. Splitting a statement involves making
460 continuation lines using @code{fortran-continuation-string}
461 (@pxref{ForIndent Cont}). This splitting happens when you type
462 @key{SPC}, @key{RET}, or @key{TAB}, and also in the Fortran
463 indentation commands. You activate Auto Fill in Fortran mode in the
466 @xref{Auto Fill,,, emacs, the Emacs Manual}.
472 @vindex fortran-break-before-delimiters
473 Auto Fill breaks lines at spaces or delimiters when the lines get
474 longer than the desired width (the value of @code{fill-column}). The
475 delimiters (besides whitespace) that Auto Fill can break at are
476 @samp{+}, @samp{-}, @samp{/}, @samp{*}, @samp{=}, @samp{<}, @samp{>},
477 and @samp{,}. The line break comes after the delimiter if the
478 variable @code{fortran-break-before-delimiters} is @code{nil}.
479 Otherwise (and by default), the break comes before the delimiter.
481 To enable Auto Fill in all Fortran buffers, add
482 @code{turn-on-auto-fill} to @code{fortran-mode-hook}.
484 @xref{Hooks,,, emacs, the Emacs Manual}.
490 @node Fortran Columns
491 @subsection Checking Columns in Fortran
493 @vindex fortran-line-length
494 In standard Fortran 77, anything beyond column 72 is ignored.
495 Most compilers provide an option to change this (for example,
496 @samp{-ffixed-line-length-N} in gfortran). Customize the variable
497 @code{fortran-line-length} to change the line length in Fortran mode.
498 Anything beyond this point is font-locked as a comment. (Unless it is
499 inside a string: strings that extend beyond @code{fortran-line-length}
500 will confuse font-lock.)
504 Display a ``column ruler'' momentarily above the current line
505 (@code{fortran-column-ruler}).
507 Split the current window horizontally temporarily so that it is
508 @code{fortran-line-length} columns wide
509 (@code{fortran-window-create-momentarily}). This may help you avoid
510 making lines longer than the character limit imposed by your Fortran
513 Split the current window horizontally so that it is
514 @code{fortran-line-length} columns wide (@code{fortran-window-create}).
515 You can then continue editing.
516 @item M-x fortran-strip-sequence-nos
517 Delete all text in column @code{fortran-line-length} and beyond.
520 @kindex C-c C-r @r{(Fortran mode)}
521 @findex fortran-column-ruler
522 The command @kbd{C-c C-r} (@code{fortran-column-ruler}) shows a column
523 ruler momentarily above the current line. The comment ruler is two lines
524 of text that show you the locations of columns with special significance in
525 Fortran programs. Square brackets show the limits of the columns for line
526 numbers, and curly brackets show the limits of the columns for the
527 statement body. Column numbers appear above them.
529 Note that the column numbers count from zero, as always in GNU Emacs.
530 As a result, the numbers may be one less than those you are familiar
531 with; but the positions they indicate in the line are standard for
534 @vindex fortran-column-ruler-fixed
535 @vindex fortran-column-ruler-tabs
536 The text used to display the column ruler depends on the value of the
537 variable @code{indent-tabs-mode}. If @code{indent-tabs-mode} is
538 @code{nil}, then the value of the variable
539 @code{fortran-column-ruler-fixed} is used as the column ruler.
540 Otherwise, the value of the variable @code{fortran-column-ruler-tab} is
541 displayed. By changing these variables, you can change the column ruler
544 @kindex C-c C-w @r{(Fortran mode)}
545 @findex fortran-window-create-momentarily
546 @kbd{C-c C-w} (@code{fortran-window-create-momentarily}) temporarily
547 splits the current window horizontally, making a window
548 @code{fortran-line-length} columns wide, so you can see any lines that
549 are too long. Type a space to restore the normal width.
551 @kindex C-u C-c C-w @r{(Fortran mode)}
552 @findex fortran-window-create
553 You can also split the window horizontally and continue editing with
554 the split in place. To do this, use @kbd{C-u C-c C-w} (@code{M-x
555 fortran-window-create}). By editing in this window you can
556 immediately see when you make a line too wide to be correct Fortran.
558 @findex fortran-strip-sequence-nos
559 The command @kbd{M-x fortran-strip-sequence-nos} deletes all text in
560 column @code{fortran-line-length} and beyond, on all lines in the
561 current buffer. This is the easiest way to get rid of old sequence
565 @subsection Fortran Keyword Abbrevs
567 Fortran mode provides many built-in abbrevs for common keywords and
568 declarations. These are the same sort of abbrev that you can define
569 yourself. To use them, you must turn on Abbrev mode.
571 @xref{Abbrevs,,, emacs, the Emacs Manual}.
577 The built-in abbrevs are unusual in one way: they all start with a
578 semicolon. For example, one built-in Fortran abbrev is @samp{;c} for
579 @samp{continue}. If you insert @samp{;c} and then insert a punctuation
580 character such as a space or a newline, the @samp{;c} expands automatically
581 to @samp{continue}, provided Abbrev mode is enabled.@refill
583 Type @samp{;?} or @samp{;C-h} to display a list of all the built-in
584 Fortran abbrevs and what they stand for.
587 arch-tag: 23ed7c36-1517-4646-9235-2d5ade5f06f6