(Wdired): Mention C-x C-q key binding.
[bpt/emacs.git] / man / fortran-xtra.texi
CommitLineData
6f585e44 1@c This is part of the Emacs manual.
4e6835db 2@c Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6f585e44
EZ
3@c See file emacs.texi for copying conditions.
4@c
c5184807
EZ
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).
7@node Fortran
8@section Fortran Mode
9@cindex Fortran mode
10@cindex mode, Fortran
11
12 Fortran mode provides special motion commands for Fortran statements
13and subprograms, and indentation commands that understand Fortran
14conventions of nesting, line numbers and continuation statements.
15Fortran mode has support for Auto Fill mode that breaks long lines into
16proper Fortran continuation lines.
17
18 Special commands for comments are provided because Fortran comments
19are unlike those of other languages. Built-in abbrevs optionally save
20typing when you insert Fortran keywords.
21
22 Use @kbd{M-x fortran-mode} to switch to this major mode. This
1bbe7139
EZ
23command runs the hook @code{fortran-mode-hook}.
24@iftex
25@xref{Hooks,,, emacs, the Emacs Manual}.
26@end iftex
27@ifnottex
28@xref{Hooks}.
29@end ifnottex
c5184807
EZ
30
31@cindex Fortran77 and Fortran90
32@findex f90-mode
33@findex fortran-mode
34 Fortran mode is meant for editing Fortran77 ``fixed format'' (and also
35``tab format'') source code. For editing the modern Fortran90 or
36Fortran95 ``free format'' source code, use F90 mode (@code{f90-mode}).
37Emacs normally uses Fortran mode for files with extension @samp{.f},
38@samp{.F} or @samp{.for}, and F90 mode for the extension @samp{.f90} and
39@samp{.f95}. GNU Fortran supports both kinds of format.
40
41@menu
42* Motion: Fortran Motion. Moving point by statements or subprograms.
43* Indent: Fortran Indent. Indentation commands for Fortran.
44* Comments: Fortran Comments. Inserting and aligning comments.
45* Autofill: Fortran Autofill. Auto fill support for Fortran.
46* Columns: Fortran Columns. Measuring columns for valid Fortran.
47* Abbrev: Fortran Abbrev. Built-in abbrevs for Fortran keywords.
48@end menu
49
50@node Fortran Motion
51@subsection Motion Commands
52
53 In addition to the normal commands for moving by and operating on
54``defuns'' (Fortran subprograms---functions and subroutines, as well as
55modules for F90 mode), Fortran mode provides special commands to move by
56statements and other program units.
57
58@table @kbd
59@kindex C-c C-n @r{(Fortran mode)}
60@findex fortran-next-statement
61@findex f90-next-statement
62@item C-c C-n
63Move to the beginning of the next statement
64(@code{fortran-next-statement}/@code{f90-next-statement}).
65
66@kindex C-c C-p @r{(Fortran mode)}
67@findex fortran-previous-statement
68@findex f90-previous-statement
69@item C-c C-p
70Move to the beginning of the previous statement
71(@code{fortran-previous-statement}/@code{f90-previous-statement}).
72If there is no previous statement (i.e. if called from the first
73statement in the buffer), move to the start of the buffer.
74
75@kindex C-c C-e @r{(F90 mode)}
76@findex f90-next-block
77@item C-c C-e
78Move point forward to the start of the next code block
79(@code{f90-next-block}). A code block is a subroutine,
80@code{if}--@code{endif} statement, and so forth. This command exists
81for F90 mode only, not Fortran mode. With a numeric argument, this
82moves forward that many blocks.
83
84@kindex C-c C-a @r{(F90 mode)}
85@findex f90-previous-block
86@item C-c C-a
87Move point backward to the previous code block
88(@code{f90-previous-block}). This is like @code{f90-next-block}, but
89moves backwards.
90
91@kindex C-M-n @r{(Fortran mode)}
92@findex fortran-end-of-block
93@findex f90-end-of-block
94@item C-M-n
95Move to the end of the current code block
96(@code{fortran-end-of-block}/@code{f90-end-of-block}). With a numeric
8a75579f 97argument, move forward that number of blocks. The mark is set before
c5184807
EZ
98moving point. The F90 mode version of this command checks for
99consistency of block types and labels (if present), but it does not
100check the outermost block since that may be incomplete.
101
102@kindex C-M-p @r{(Fortran mode)}
103@findex fortran-beginning-of-block
104@findex f90-beginning-of-block
105@item C-M-p
106Move to the start of the current code block
107(@code{fortran-beginning-of-block}/@code{f90-beginning-of-block}). This
108is like @code{fortran-end-of-block}, but moves backwards.
109@end table
110
111@node Fortran Indent
112@subsection Fortran Indentation
113
114 Special commands and features are needed for indenting Fortran code in
115order to make sure various syntactic entities (line numbers, comment line
116indicators and continuation line flags) appear in the columns that are
117required for standard, fixed (or tab) format Fortran.
118
119@menu
120* Commands: ForIndent Commands. Commands for indenting and filling Fortran.
121* Contline: ForIndent Cont. How continuation lines indent.
122* Numbers: ForIndent Num. How line numbers auto-indent.
123* Conv: ForIndent Conv. Conventions you must obey to avoid trouble.
124* Vars: ForIndent Vars. Variables controlling Fortran indent style.
125@end menu
126
127@node ForIndent Commands
128@subsubsection Fortran Indentation and Filling Commands
129
130@table @kbd
131@item C-M-j
132Break the current line at point and set up a continuation line
133(@code{fortran-split-line}).
134@item M-^
135Join this line to the previous line (@code{fortran-join-line}).
136@item C-M-q
137Indent all the lines of the subprogram point is in
138(@code{fortran-indent-subprogram}).
139@item M-q
140Fill a comment block or statement.
141@end table
142
143@kindex C-M-q @r{(Fortran mode)}
144@findex fortran-indent-subprogram
145 The key @kbd{C-M-q} runs @code{fortran-indent-subprogram}, a command
146to reindent all the lines of the Fortran subprogram (function or
147subroutine) containing point.
148
149@kindex C-M-j @r{(Fortran mode)}
150@findex fortran-split-line
151 The key @kbd{C-M-j} runs @code{fortran-split-line}, which splits
152a line in the appropriate fashion for Fortran. In a non-comment line,
153the second half becomes a continuation line and is indented
154accordingly. In a comment line, both halves become separate comment
155lines.
156
157@kindex M-^ @r{(Fortran mode)}
158@kindex C-c C-d @r{(Fortran mode)}
159@findex fortran-join-line
160 @kbd{M-^} or @kbd{C-c C-d} runs the command @code{fortran-join-line},
161which joins a continuation line back to the previous line, roughly as
162the inverse of @code{fortran-split-line}. The point must be on a
163continuation line when this command is invoked.
164
165@kindex M-q @r{(Fortran mode)}
166@kbd{M-q} in Fortran mode fills the comment block or statement that
167point is in. This removes any excess statement continuations.
168
169@node ForIndent Cont
170@subsubsection Continuation Lines
171@cindex Fortran continuation lines
172
173@vindex fortran-continuation-string
174 Most Fortran77 compilers allow two ways of writing continuation lines.
175If the first non-space character on a line is in column 5, then that
176line is a continuation of the previous line. We call this @dfn{fixed
177format}. (In GNU Emacs we always count columns from 0; but note that
178the Fortran standard counts from 1.) The variable
179@code{fortran-continuation-string} specifies what character to put in
180column 5. A line that starts with a tab character followed by any digit
181except @samp{0} is also a continuation line. We call this style of
5a7f4c1b 182continuation @dfn{tab format}. (Fortran90 introduced ``free format,''
c5184807
EZ
183with another style of continuation lines).
184
185@vindex indent-tabs-mode @r{(Fortran mode)}
186@vindex fortran-analyze-depth
187@vindex fortran-tab-mode-default
188 Fortran mode can use either style of continuation line. When you
189enter Fortran mode, it tries to deduce the proper continuation style
190automatically from the buffer contents. It does this by scanning up to
191@code{fortran-analyze-depth} (default 100) lines from the start of the
192buffer. The first line that begins with either a tab character or six
193spaces determines the choice. If the scan fails (for example, if the
194buffer is new and therefore empty), the value of
195@code{fortran-tab-mode-default} (@code{nil} for fixed format, and
196non-@code{nil} for tab format) is used. @samp{/t} in the mode line
197indicates tab format is selected. Fortran mode sets the value of
198@code{indent-tabs-mode} accordingly.
199
200 If the text on a line starts with the Fortran continuation marker
201@samp{$}, or if it begins with any non-whitespace character in column
2025, Fortran mode treats it as a continuation line. When you indent a
203continuation line with @key{TAB}, it converts the line to the current
204continuation style. When you split a Fortran statement with
205@kbd{C-M-j}, the continuation marker on the newline is created according
206to the continuation style.
207
208 The setting of continuation style affects several other aspects of
209editing in Fortran mode. In fixed format mode, the minimum column
210number for the body of a statement is 6. Lines inside of Fortran
211blocks that are indented to larger column numbers always use only the
212space character for whitespace. In tab format mode, the minimum
213column number for the statement body is 8, and the whitespace before
214column 8 must always consist of one tab character.
215
216@node ForIndent Num
217@subsubsection Line Numbers
218
219 If a number is the first non-whitespace in the line, Fortran
220indentation assumes it is a line number and moves it to columns 0
221through 4. (Columns always count from 0 in GNU Emacs.)
222
223@vindex fortran-line-number-indent
224 Line numbers of four digits or less are normally indented one space.
225The variable @code{fortran-line-number-indent} controls this; it
226specifies the maximum indentation a line number can have. The default
227value of the variable is 1. Fortran mode tries to prevent line number
228digits passing column 4, reducing the indentation below the specified
229maximum if necessary. If @code{fortran-line-number-indent} has the
230value 5, line numbers are right-justified to end in column 4.
231
232@vindex fortran-electric-line-number
233 Simply inserting a line number is enough to indent it according to
234these rules. As each digit is inserted, the indentation is recomputed.
235To turn off this feature, set the variable
236@code{fortran-electric-line-number} to @code{nil}.
237
238
239@node ForIndent Conv
240@subsubsection Syntactic Conventions
241
242 Fortran mode assumes that you follow certain conventions that simplify
243the task of understanding a Fortran program well enough to indent it
244properly:
245
246@itemize @bullet
247@item
248Two nested @samp{do} loops never share a @samp{continue} statement.
249
250@item
251Fortran keywords such as @samp{if}, @samp{else}, @samp{then}, @samp{do}
252and others are written without embedded whitespace or line breaks.
253
254Fortran compilers generally ignore whitespace outside of string
255constants, but Fortran mode does not recognize these keywords if they
256are not contiguous. Constructs such as @samp{else if} or @samp{end do}
257are acceptable, but the second word should be on the same line as the
258first and not on a continuation line.
259@end itemize
260
261@noindent
262If you fail to follow these conventions, the indentation commands may
263indent some lines unaesthetically. However, a correct Fortran program
264retains its meaning when reindented even if the conventions are not
265followed.
266
267@node ForIndent Vars
268@subsubsection Variables for Fortran Indentation
269
270@vindex fortran-do-indent
271@vindex fortran-if-indent
272@vindex fortran-structure-indent
273@vindex fortran-continuation-indent
274@vindex fortran-check-all-num@dots{}
275@vindex fortran-minimum-statement-indent@dots{}
276 Several additional variables control how Fortran indentation works:
277
278@table @code
279@item fortran-do-indent
280Extra indentation within each level of @samp{do} statement (default 3).
281
282@item fortran-if-indent
283Extra indentation within each level of @samp{if}, @samp{select case}, or
284@samp{where} statements (default 3).
285
286@item fortran-structure-indent
287Extra indentation within each level of @samp{structure}, @samp{union},
288@samp{map}, or @samp{interface} statements (default 3).
289
290@item fortran-continuation-indent
291Extra indentation for bodies of continuation lines (default 5).
292
293@item fortran-check-all-num-for-matching-do
294In Fortran77, a numbered @samp{do} statement is ended by any statement
295with a matching line number. It is common (but not compulsory) to use a
296@samp{continue} statement for this purpose. If this variable has a
297non-@code{nil} value, indenting any numbered statement must check for a
298@samp{do} that ends there. If you always end @samp{do} statements with
299a @samp{continue} line (or if you use the more modern @samp{enddo}),
300then you can speed up indentation by setting this variable to
301@code{nil}. The default is @code{nil}.
302
303@item fortran-blink-matching-if
304If this is @code{t}, indenting an @samp{endif} (or @samp{enddo}
305statement moves the cursor momentarily to the matching @samp{if} (or
306@samp{do}) statement to show where it is. The default is @code{nil}.
307
308@item fortran-minimum-statement-indent-fixed
309Minimum indentation for Fortran statements when using fixed format
310continuation line style. Statement bodies are never indented less than
311this much. The default is 6.
312
313@item fortran-minimum-statement-indent-tab
314Minimum indentation for Fortran statements for tab format continuation line
315style. Statement bodies are never indented less than this much. The
316default is 8.
317@end table
318
319The variables controlling the indentation of comments are described in
320the following section.
321
322@node Fortran Comments
323@subsection Fortran Comments
324
325 The usual Emacs comment commands assume that a comment can follow a
326line of code. In Fortran77, the standard comment syntax requires an
327entire line to be just a comment. Therefore, Fortran mode replaces the
328standard Emacs comment commands and defines some new variables.
329
330@vindex fortran-comment-line-start
331 Fortran mode can also handle the Fortran90 comment syntax where comments
332start with @samp{!} and can follow other text. Because only some Fortran77
333compilers accept this syntax, Fortran mode will not insert such comments
334unless you have said in advance to do so. To do this, set the variable
335@code{fortran-comment-line-start} to @samp{"!"}.
336
337@table @kbd
338@item M-;
339Align comment or insert new comment (@code{fortran-indent-comment}).
340
341@item C-x ;
342Applies to nonstandard @samp{!} comments only.
343
344@item C-c ;
345Turn all lines of the region into comments, or (with argument) turn them back
346into real code (@code{fortran-comment-region}).
347@end table
348
349@findex fortran-indent-comment
350 @kbd{M-;} in Fortran mode is redefined as the command
351@code{fortran-indent-comment}. Like the usual @kbd{M-;} command, this
352recognizes any kind of existing comment and aligns its text appropriately;
353if there is no existing comment, a comment is inserted and aligned. But
354inserting and aligning comments are not the same in Fortran mode as in
355other modes.
356
357 When a new comment must be inserted, if the current line is blank, a
358full-line comment is inserted. On a non-blank line, a nonstandard @samp{!}
359comment is inserted if you have said you want to use them. Otherwise a
360full-line comment is inserted on a new line before the current line.
361
362 Nonstandard @samp{!} comments are aligned like comments in other
363languages, but full-line comments are different. In a standard full-line
364comment, the comment delimiter itself must always appear in column zero.
365What can be aligned is the text within the comment. You can choose from
366three styles of alignment by setting the variable
367@code{fortran-comment-indent-style} to one of these values:
368
369@vindex fortran-comment-indent-style
370@vindex fortran-comment-line-extra-indent
371@table @code
372@item fixed
373Align the text at a fixed column, which is the sum of
374@code{fortran-comment-line-extra-indent} and the minimum statement
375indentation. This is the default.
376
377The minimum statement indentation is
378@code{fortran-minimum-statement-indent-fixed} for fixed format
379continuation line style and @code{fortran-minimum-statement-indent-tab}
380for tab format style.
381
382@item relative
383Align the text as if it were a line of code, but with an additional
384@code{fortran-comment-line-extra-indent} columns of indentation.
385
386@item nil
387Don't move text in full-line comments automatically.
388@end table
389
390@vindex fortran-comment-indent-char
391 In addition, you can specify the character to be used to indent within
392full-line comments by setting the variable
393@code{fortran-comment-indent-char} to the single-character string you want
394to use.
395
396@vindex fortran-directive-re
397 Compiler directive lines, or preprocessor lines, have much the same
398appearance as comment lines. It is important, though, that such lines
399never be indented at all, no matter what the value of
400@code{fortran-comment-indent-style}. The variable
401@code{fortran-directive-re} is a regular expression that specifies which
402lines are directives. Matching lines are never indented, and receive
403distinctive font-locking.
404
405 The normal Emacs comment command @kbd{C-x ;} has not been redefined. If
406you use @samp{!} comments, this command can be used with them. Otherwise
407it is useless in Fortran mode.
408
409@kindex C-c ; @r{(Fortran mode)}
410@findex fortran-comment-region
411@vindex fortran-comment-region
412 The command @kbd{C-c ;} (@code{fortran-comment-region}) turns all the
413lines of the region into comments by inserting the string @samp{C$$$} at
414the front of each one. With a numeric argument, it turns the region
415back into live code by deleting @samp{C$$$} from the front of each line
416in it. The string used for these comments can be controlled by setting
417the variable @code{fortran-comment-region}. Note that here we have an
418example of a command and a variable with the same name; these two uses
419of the name never conflict because in Lisp and in Emacs it is always
420clear from the context which one is meant.
421
422@node Fortran Autofill
423@subsection Auto Fill in Fortran Mode
424
425 Fortran mode has specialized support for Auto Fill mode, which is a
426minor mode that automatically splits statements as you insert them
427when they become too wide. Splitting a statement involves making
428continuation lines using @code{fortran-continuation-string}
429(@pxref{ForIndent Cont}). This splitting happens when you type
430@key{SPC}, @key{RET}, or @key{TAB}, and also in the Fortran
431indentation commands. You activate Auto Fill in Fortran mode in the
1bbe7139
EZ
432normal way.
433@iftex
434@xref{Auto Fill,,, emacs, the Emacs Manual}.
435@end iftex
436@ifnottex
437@xref{Auto Fill}.
438@end ifnottex
c5184807
EZ
439
440@vindex fortran-break-before-delimiters
441 Auto Fill breaks lines at spaces or delimiters when the lines get
442longer than the desired width (the value of @code{fill-column}). The
443delimiters (besides whitespace) that Auto Fill can break at are
444@samp{+}, @samp{-}, @samp{/}, @samp{*}, @samp{=}, @samp{<}, @samp{>},
445and @samp{,}. The line break comes after the delimiter if the
446variable @code{fortran-break-before-delimiters} is @code{nil}.
447Otherwise (and by default), the break comes before the delimiter.
448
449 To enable Auto Fill in all Fortran buffers, add
1bbe7139
EZ
450@code{turn-on-auto-fill} to @code{fortran-mode-hook}.
451@iftex
452@xref{Hooks,,, emacs, the Emacs Manual}.
453@end iftex
454@ifnottex
455@xref{Hooks}.
456@end ifnottex
c5184807
EZ
457
458@node Fortran Columns
459@subsection Checking Columns in Fortran
460
461@table @kbd
462@item C-c C-r
463Display a ``column ruler'' momentarily above the current line
464(@code{fortran-column-ruler}).
465@item C-c C-w
466Split the current window horizontally temporarily so that it is 72
467columns wide (@code{fortran-window-create-momentarily}). This may
468help you avoid making lines longer than the 72-character limit that
469some Fortran compilers impose.
470@item C-u C-c C-w
471Split the current window horizontally so that it is 72 columns wide
472(@code{fortran-window-create}). You can then continue editing.
473@item M-x fortran-strip-sequence-nos
474Delete all text in column 72 and beyond.
475@end table
476
477@kindex C-c C-r @r{(Fortran mode)}
478@findex fortran-column-ruler
479 The command @kbd{C-c C-r} (@code{fortran-column-ruler}) shows a column
480ruler momentarily above the current line. The comment ruler is two lines
481of text that show you the locations of columns with special significance in
482Fortran programs. Square brackets show the limits of the columns for line
483numbers, and curly brackets show the limits of the columns for the
484statement body. Column numbers appear above them.
485
486 Note that the column numbers count from zero, as always in GNU Emacs.
487As a result, the numbers may be one less than those you are familiar
488with; but the positions they indicate in the line are standard for
489Fortran.
490
491@vindex fortran-column-ruler-fixed
492@vindex fortran-column-ruler-tabs
493 The text used to display the column ruler depends on the value of the
494variable @code{indent-tabs-mode}. If @code{indent-tabs-mode} is
495@code{nil}, then the value of the variable
496@code{fortran-column-ruler-fixed} is used as the column ruler.
497Otherwise, the value of the variable @code{fortran-column-ruler-tab} is
498displayed. By changing these variables, you can change the column ruler
499display.
500
501@kindex C-c C-w @r{(Fortran mode)}
502@findex fortran-window-create-momentarily
503 @kbd{C-c C-w} (@code{fortran-window-create-momentarily}) temporarily
504splits the current window horizontally, making a window 72 columns
505wide, so you can see any lines that are too long. Type a space to
506restore the normal width.
507
508@kindex C-u C-c C-w @r{(Fortran mode)}
509@findex fortran-window-create
510 You can also split the window horizontally and continue editing with
511the split in place. To do this, use @kbd{C-u C-c C-w} (@code{M-x
512fortran-window-create}). By editing in this window you can
513immediately see when you make a line too wide to be correct Fortran.
514
515@findex fortran-strip-sequence-nos
516 The command @kbd{M-x fortran-strip-sequence-nos} deletes all text in
517column 72 and beyond, on all lines in the current buffer. This is the
518easiest way to get rid of old sequence numbers.
519
520@node Fortran Abbrev
521@subsection Fortran Keyword Abbrevs
522
523 Fortran mode provides many built-in abbrevs for common keywords and
524declarations. These are the same sort of abbrev that you can define
525yourself. To use them, you must turn on Abbrev mode.
1bbe7139 526@iftex
c5184807 527@xref{Abbrevs,,, emacs, the Emacs Manual}.
1bbe7139
EZ
528@end iftex
529@ifnottex
530@xref{Abbrevs}.
531@end ifnottex
c5184807
EZ
532
533 The built-in abbrevs are unusual in one way: they all start with a
534semicolon. You cannot normally use semicolon in an abbrev, but Fortran
535mode makes this possible by changing the syntax of semicolon to ``word
536constituent.''
537
538 For example, one built-in Fortran abbrev is @samp{;c} for
539@samp{continue}. If you insert @samp{;c} and then insert a punctuation
540character such as a space or a newline, the @samp{;c} expands automatically
541to @samp{continue}, provided Abbrev mode is enabled.@refill
542
543 Type @samp{;?} or @samp{;C-h} to display a list of all the built-in
544Fortran abbrevs and what they stand for.
14831d20
MB
545
546@ignore
547 arch-tag: 23ed7c36-1517-4646-9235-2d5ade5f06f6
548@end ignore