]> code.delx.au - gnu-emacs/blob - lisp/progmodes/fortran.el
(c-before-font-lock-function): Correct a typo in the doc string.
[gnu-emacs] / lisp / progmodes / fortran.el
1 ;;; fortran.el --- Fortran mode for GNU Emacs
2
3 ;; Copyright (C) 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Michael D. Prange <prange@erl.mit.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: fortran, languages
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This mode is documented in the Emacs manual.
30 ;;
31 ;; Note that it is for editing Fortran77 or Fortran90 fixed source
32 ;; form. For editing Fortran 90 free format source, use `f90-mode'
33 ;; (f90.el). It is meant to support the GNU Fortran language
34 ;; implemented by g77 (its extensions to Fortran77 and
35 ;; interpretations, e.g. of blackslash in strings).
36
37 ;;; History:
38
39 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
40
41 ;; We acknowledge many contributions and valuable suggestions by
42 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
43 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
44 ;; Gary Sabot and Richard Stallman.
45
46 ;;; Code:
47
48 ;; Todo:
49
50 ;; * Tidy it all up (more)!
51 ;; * Implement insertion and removal of statement continuations in
52 ;; mixed f77/f90 style, with the first `&' past column 72 and the
53 ;; second in column 6.
54 ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
55
56 ;; silence compiler
57 (defvar dabbrev-case-fold-search)
58 (defvar font-lock-syntactic-keywords)
59 (defvar gud-find-expr-function)
60 (defvar imenu-case-fold-search)
61 (defvar imenu-syntax-alist)
62
63
64 (defgroup fortran nil
65 "Major mode for editing fixed format Fortran code."
66 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
67 :link '(custom-manual "(emacs)Fortran")
68 :group 'languages)
69
70 (defgroup fortran-indent nil
71 "Indentation variables in Fortran mode."
72 :prefix "fortran-"
73 :group 'fortran)
74
75 (defgroup fortran-comment nil
76 "Comment-handling variables in Fortran mode."
77 :prefix "fortran-"
78 :group 'fortran)
79
80
81 ;;;###autoload
82 (defcustom fortran-tab-mode-default nil
83 "*Default tabbing/carriage control style for empty files in Fortran mode.
84 A non-nil value specifies tab-digit style of continuation control.
85 A value of nil specifies that continuation lines are marked
86 with a character in column 6."
87 :type 'boolean
88 :group 'fortran-indent)
89 (put 'fortran-tab-mode-default 'safe-local-variable 'booleanp)
90
91 (defcustom fortran-tab-mode-string "/t"
92 "*String to appear in mode line in TAB format buffers."
93 :type 'string
94 :group 'fortran-indent)
95 (put 'fortran-tab-mode-string 'safe-local-variable 'stringp)
96
97 (defcustom fortran-do-indent 3
98 "*Extra indentation applied to DO blocks."
99 :type 'integer
100 :group 'fortran-indent)
101 (put 'fortran-do-indent 'safe-local-variable 'integerp)
102
103 (defcustom fortran-if-indent 3
104 "*Extra indentation applied to IF, SELECT CASE and WHERE blocks."
105 :type 'integer
106 :group 'fortran-indent)
107 (put 'fortran-if-indent 'safe-local-variable 'integerp)
108
109 (defcustom fortran-structure-indent 3
110 "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
111 :type 'integer
112 :group 'fortran-indent)
113 (put 'fortran-structure-indent 'safe-local-variable 'integerp)
114
115 (defcustom fortran-continuation-indent 5
116 "*Extra indentation applied to continuation lines."
117 :type 'integer
118 :group 'fortran-indent)
119 (put 'fortran-continuation-indent 'safe-local-variable 'integerp)
120
121 (defcustom fortran-comment-indent-style 'fixed
122 "*How to indent comments.
123 nil forces comment lines not to be touched;
124 `fixed' indents to `fortran-comment-line-extra-indent' columns beyond
125 `fortran-minimum-statement-indent-fixed' (if `indent-tabs-mode' nil), or
126 `fortran-minimum-statement-indent-tab' (if `indent-tabs-mode' non-nil);
127 `relative' indents to current Fortran indentation plus
128 `fortran-comment-line-extra-indent'."
129 :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative))
130 :group 'fortran-indent)
131 (put 'fortran-comment-indent 'safe-local-variable
132 (lambda (value) (memq value '(nil fixed relative))))
133
134 (defcustom fortran-comment-line-extra-indent 0
135 "*Amount of extra indentation for text within full-line comments."
136 :type 'integer
137 :group 'fortran-indent
138 :group 'fortran-comment)
139 (put 'fortran-comment-line-extra-indent 'safe-local-variable 'integerp)
140
141 (defcustom fortran-comment-line-start "C"
142 "*Delimiter inserted to start new full-line comment.
143 You might want to change this to \"*\", for instance."
144 :version "21.1"
145 :type 'string
146 :group 'fortran-comment)
147 (put 'fortran-comment-line-start 'safe-local-variable 'stringp)
148
149 ;; This used to match preprocessor lines too, but that messes up
150 ;; filling and doesn't seem to be necessary.
151 (defcustom fortran-comment-line-start-skip
152 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
153 "*Regexp to match the start of a full-line comment."
154 :version "21.1"
155 :type 'regexp
156 :group 'fortran-comment)
157 (put 'fortran-comment-line-start-skip 'safe-local-variable 'stringp)
158
159 (defcustom fortran-directive-re
160 "^[ \t]*#.*"
161 "*Regexp to match a directive line.
162 The matching text will be fontified with `font-lock-keyword-face'.
163 The matching line will be given zero indentation."
164 :version "22.1"
165 :type 'regexp
166 :group 'fortran-indent)
167 (put 'fortran-directive-re 'safe-local-variable 'stringp)
168
169 (defcustom fortran-minimum-statement-indent-fixed 6
170 "*Minimum statement indentation for fixed format continuation style."
171 :type 'integer
172 :group 'fortran-indent)
173 (put 'fortran-minimum-statement-indent-fixed 'safe-local-variable 'integerp)
174
175 (defcustom fortran-minimum-statement-indent-tab (max tab-width 6)
176 "*Minimum statement indentation for TAB format continuation style."
177 :type 'integer
178 :group 'fortran-indent)
179 (put 'fortran-minimum-statement-indent-tab 'safe-local-variable 'integerp)
180
181 ;; Note that this is documented in the v18 manuals as being a string
182 ;; of length one rather than a single character.
183 ;; The code in this file accepts either format for compatibility.
184 (defcustom fortran-comment-indent-char " "
185 "*Single-character string inserted for Fortran comment indentation.
186 Normally a space."
187 :type 'string
188 :group 'fortran-comment)
189 (put 'fortran-comment-indent-char 'safe-local-variable
190 (lambda (value) (or (char-valid-p value)
191 (and (stringp value)
192 (= (length value) 1)))))
193
194 (defcustom fortran-line-number-indent 1
195 "*Maximum indentation for Fortran line numbers.
196 5 means right-justify them within their five-column field."
197 :type 'integer
198 :group 'fortran-indent)
199 (put 'fortran-line-number-indent 'safe-local-variable 'integerp)
200
201 (defcustom fortran-check-all-num-for-matching-do nil
202 "*Non-nil causes all numbered lines to be treated as possible DO loop ends."
203 :type 'boolean
204 :group 'fortran)
205 (put 'fortran-check-all-num-for-matching-do 'safe-local-variable 'booleanp)
206
207 (defcustom fortran-blink-matching-if nil
208 "*Non-nil causes \\[fortran-indent-line] on ENDIF to blink on matching IF.
209 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
210 :type 'boolean
211 :group 'fortran)
212 (put 'fortran-blink-matching-if 'safe-local-variable 'booleanp)
213
214 (defcustom fortran-continuation-string "$"
215 "*Single-character string used for Fortran continuation lines.
216 In fixed format continuation style, this character is inserted in
217 column 6 by \\[fortran-split-line] to begin a continuation line.
218 Also, if \\[fortran-indent-line] finds this at the beginning of a
219 line, it will convert the line into a continuation line of the
220 appropriate style. Normally $."
221 :type 'string
222 :group 'fortran)
223 (put 'fortran-continuation-string 'safe-local-variable
224 (lambda (value) (and (stringp value)
225 (= (length value) 1))))
226
227 (defcustom fortran-comment-region "c$$$"
228 "*String inserted by \\[fortran-comment-region] at start of each \
229 line in region."
230 :type 'string
231 :group 'fortran-comment)
232 (put 'fortran-comment-region 'safe-local-variable 'stringp)
233
234 (defcustom fortran-electric-line-number t
235 "*Non-nil causes line numbers to be moved to the correct column as typed."
236 :type 'boolean
237 :group 'fortran)
238 (put 'fortran-electric-line-number 'safe-local-variable 'booleanp)
239
240 (defcustom fortran-column-ruler-fixed
241 "0 4 6 10 20 30 40 5\
242 0 60 70\n\
243 \[ ]|{ | | | | | | | | \
244 \| | | | |}\n"
245 "String displayed above current line by \\[fortran-column-ruler].
246 This variable is used in fixed format mode.
247 See the variable `fortran-column-ruler-tab' for TAB format mode."
248 :type 'string
249 :group 'fortran)
250 (put 'fortran-column-ruler-fixed 'safe-local-variable 'stringp)
251
252 (defcustom fortran-column-ruler-tab
253 "0 810 20 30 40 5\
254 0 60 70\n\
255 \[ ]| { | | | | | | | | \
256 \| | | | |}\n"
257 "String displayed above current line by \\[fortran-column-ruler].
258 This variable is used in TAB format mode.
259 See the variable `fortran-column-ruler-fixed' for fixed format mode."
260 :type 'string
261 :group 'fortran)
262 (put 'fortran-column-ruler-tab 'safe-local-variable 'stringp)
263
264 (defcustom fortran-analyze-depth 100
265 "Number of lines to scan to identify fixed or TAB format style."
266 :type 'integer
267 :group 'fortran)
268 (put 'fortran-analyze-depth 'safe-local-variable 'integerp)
269
270 (defcustom fortran-break-before-delimiters t
271 "*Non-nil causes filling to break lines before delimiters.
272 Delimiters are characters matching the regexp `fortran-break-delimiters-re'."
273 :type 'boolean
274 :group 'fortran)
275 (put 'fortran-break-before-delimiters 'safe-local-variable 'booleanp)
276
277 (defconst fortran-break-delimiters-re "[-+*/><=, \t]"
278 "Regexp matching delimiter characters at which lines may be broken.
279 There are certain tokens comprised entirely of characters
280 matching this regexp that should not be split, and these are
281 specified by the constant `fortran-no-break-re'.")
282
283 ;; The ">=", etc F77 extensions are supported by g77.
284 (defconst fortran-no-break-re
285 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=") 'paren)
286 "Regexp specifying where not to break lines when filling.
287 This regexp matches certain tokens comprised entirely of
288 characters matching the regexp `fortran-break-delimiters-re' that should
289 not be split by filling. Each element is assumed to be two
290 characters long.")
291
292 (defcustom fortran-mode-hook nil
293 "Hook run when entering Fortran mode."
294 :type 'hook
295 :group 'fortran)
296
297 \f
298 (defvar fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*("
299 "Regexp matching the start of an IF statement.")
300
301 (defvar fortran-end-prog-re1
302 "end\
303 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
304 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?"
305 "Regexp possibly matching the end of a subprogram.")
306
307 (defvar fortran-end-prog-re
308 (concat "^[ \t0-9]*" fortran-end-prog-re1)
309 "Regexp possibly matching the end of a subprogram, from the line start.
310 See also `fortran-end-prog-re1'.")
311
312 (defconst fortran-type-types
313 (concat "\\<"
314 (mapconcat 'identity ; " " -> "[ \t]*"
315 (split-string
316 (regexp-opt
317 (let ((simple-types
318 '("character" "byte" "integer" "logical"
319 "none" "real" "complex"
320 "double precision" "double complex"))
321 (structured-types '("structure" "union" "map"))
322 (other-types '("record" "dimension"
323 "parameter" "common" "save"
324 "external" "intrinsic" "data"
325 "equivalence")))
326 (append
327 (mapcar (lambda (x) (concat "implicit " x))
328 simple-types)
329 simple-types
330 (mapcar (lambda (x) (concat "end " x))
331 structured-types)
332 structured-types
333 other-types)) 'paren))
334 "[ \t]*") "\\>")
335 "Regexp matching Fortran types.")
336
337 (defvar fortran-font-lock-keywords-1
338 ;; Program, subroutine and function declarations, plus calls.
339 '(("\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|\
340 program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?"
341 (1 font-lock-keyword-face)
342 (2 font-lock-function-name-face nil t)))
343 "Subdued level highlighting for Fortran mode.")
344
345 (defvar fortran-font-lock-keywords-2
346 (append fortran-font-lock-keywords-1
347 (list
348 ;; Fontify all type specifiers (must be first - see below).
349 (cons fortran-type-types 'font-lock-type-face)
350 ;; Builtin keywords (except logical, do and goto - see below).
351 (concat "\\<" (regexp-opt
352 '("continue" "format" "end" "enddo"
353 "if" "then" "else" "endif" "elseif"
354 "while" "inquire" "stop" "return"
355 "include" "open" "close" "read"
356 "write" "format" "print" "select" "case"
357 "cycle" "exit" "rewind" "backspace"
358 "where" "elsewhere")
359 'paren) "\\>")
360 ;; Builtin operators.
361 (concat "\\." (regexp-opt
362 '("and" "or" "not" "lt" "le" "eq" "ge"
363 "gt" "ne" "true" "false")
364 'paren) "\\.")
365 ;; do/goto keywords and targets, and goto tags.
366 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
367 (1 font-lock-keyword-face)
368 (2 font-lock-constant-face nil t))
369 '("^ *\\([0-9]+\\)" . font-lock-constant-face)))
370 "Medium level highlighting for Fortran mode.")
371
372 (defvar fortran-font-lock-keywords-3
373 (append
374 fortran-font-lock-keywords-1
375 ;; All type specifiers plus their declared items.
376 (list
377 (list (concat fortran-type-types "[ \t(/]*\\(*\\)?")
378 ;; Type specifier.
379 '(1 font-lock-type-face)
380 ;; Declaration item (or just /.../ block name).
381 `(font-lock-match-c-style-declaration-item-and-skip-to-next
382 ;; Start after any *(...) expression.
383 (condition-case nil
384 (and (match-beginning ,(1+ (regexp-opt-depth
385 fortran-type-types)))
386 (forward-sexp)
387 (forward-sexp))
388 (error nil))
389 ;; No need to clean up.
390 nil
391 ;; Fontify as a variable name, functions fontified elsewhere.
392 (1 font-lock-variable-name-face nil t))))
393 ;; Things extra to `fortran-font-lock-keywords-3' (must be done first).
394 (list
395 ;; Goto-like `err=label'/`end=label' in read/write statements.
396 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
397 (1 font-lock-keyword-face) (4 font-lock-constant-face nil t))
398 ;; Standard continuation character and in a TAB-formatted line.
399 '("^ \\{5\\}\\([^ 0\n]\\)" 1 font-lock-string-face)
400 '("^\t\\([1-9]\\)" 1 font-lock-string-face))
401 `((,fortran-directive-re (0 font-lock-keyword-face t)))
402 ;; `fortran-font-lock-keywords-2' without types (see above).
403 (cdr (nthcdr (length fortran-font-lock-keywords-1)
404 fortran-font-lock-keywords-2)))
405 "Gaudy level highlighting for Fortran mode.")
406
407 (defvar fortran-font-lock-keywords-4
408 (append fortran-font-lock-keywords-3
409 (list (list
410 (concat "\\<"
411 (regexp-opt
412 '("int" "ifix" "idint" "real" "float" "sngl"
413 "dble" "cmplx" "ichar" "char" "aint" "dint"
414 "anint" "dnint" "nint" "idnint" "iabs" "abs"
415 "dabs" "cabs" "mod" "amod" "dmod" "isign"
416 "sign" "dsign" "idim" "dim" "ddim" "dprod"
417 "max" "max0" "amax1" "dmax1" "amax0" "max1"
418 "min" "min0" "amin1" "dmin1" "amin0" "min1"
419 "len" "index" "lge" "lgt" "lle" "llt" "aimag"
420 "conjg" "sqrt" "dsqrt" "csqrt" "exp" "dexp"
421 "cexp" "log" "alog" "dlog" "clog" "log10"
422 "alog10" "dlog10" "sin" "dsin" "csin" "cos"
423 "dcos" "ccos" "tan" "dtan" "asin" "dasin"
424 "acos" "dacos" "atan" "datan" "atan2" "datan2"
425 "sinh" "dsinh" "cosh" "dcosh" "tanh" "dtanh")
426 'paren) "[ \t]*(") '(1 font-lock-builtin-face))))
427 "Maximum highlighting for Fortran mode.
428 Consists of level 3 plus all other intrinsics not already highlighted.")
429
430 ;; Comments are real pain in Fortran because there is no way to
431 ;; represent the standard comment syntax in an Emacs syntax table.
432 ;; (We can do so for F90-style). Therefore an unmatched quote in a
433 ;; standard comment will throw fontification off on the wrong track.
434 ;; So we do syntactic fontification with regexps.
435 (defvar fortran-font-lock-syntactic-keywords
436 '(("^[cd\\*]" 0 (11))
437 ("^[^cd\\*\t\n].\\{71\\}\\([^\n]+\\)" 1 (11)))
438 "`font-lock-syntactic-keywords' for Fortran.
439 These get fixed-format comments fontified.")
440
441 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
442 "Default expressions to highlight in Fortran mode.")
443
444 (defvar fortran-imenu-generic-expression
445 ;; These patterns could be confused by sequence nos. in cols 72+ and
446 ;; don't allow continuations everywhere.
447 (list
448 (list
449 nil
450 ;; [This will be fooled by `end function' allowed by G77. Also,
451 ;; it assumes sensible whitespace is employed.]
452 (concat
453 ;; leading whitespace:
454 "^\\s-+\\("
455 ;; function declaration with optional type, e.g. `real',
456 ;; `real*4', character(*), `double precision':
457 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
458 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
459 ;; Possible statement continuation:
460 "[ \t" fortran-continuation-string "]+"
461 ;; Variable to index:
462 "\\(\\sw+\\)")
463 3)
464 ;; Un-named block data.
465 '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
466 "Value for `imenu-generic-expression' in Fortran mode.")
467
468 \f
469 ;; Hideshow support.
470 (defconst fortran-blocks-re
471 (concat "block[ \t]*data\\|select[ \t]*case\\|"
472 (regexp-opt '("do" "if" "interface" "function" "map" "program"
473 "structure" "subroutine" "union" "where")))
474 "Regexp potentially indicating the start or end of a Fortran \"block\".
475 Omits naked END statements, and DO-loops closed by anything other
476 than ENDDO.")
477
478 (defconst fortran-end-block-re
479 ;; Do-loops terminated by things other than ENDDO cannot be handled
480 ;; with a regexp. This omission does not seem to matter to hideshow...
481 (concat "^[ \t0-9]*\\<end[ \t]*\\("
482 fortran-blocks-re
483 ;; Naked END statement.
484 "\\|!\\|$\\)")
485 "Regexp matching the end of a Fortran \"block\", from the line start.
486 Note that only ENDDO is handled for the end of a DO-loop. Used
487 in the Fortran entry in `hs-special-modes-alist'.")
488
489 (defconst fortran-start-block-re
490 (concat
491 "^[ \t0-9]*\\(" ; statement number
492 ;; Structure label for DO, IF, SELECT, WHERE.
493 "\\(\\(\\sw+[ \t]*:[ \t]*\\)?"
494 ;; IF blocks are a nuisance:
495 ;; IF ( ... ) foo is not a block, but a single statement.
496 ;; IF ( ... ) THEN can be split over multiple lines.
497 ;; [So can, eg, a DO WHILE (... ), but that is less common, I hope.]
498 ;; The regexp below allows for it to be split over at most 2 lines.
499 ;; That leads to the problem of not matching two consecutive IF
500 ;; statements as one, eg:
501 ;; IF ( ... ) foo
502 ;; IF ( ... ) THEN
503 ;; It simply is not possible to do this in a 100% correct fashion
504 ;; using a regexp - see the functions fortran-end-if,
505 ;; fortran-beginning-if for the hoops we have to go through.
506 ;; An alternative is to match on THEN at a line end, eg:
507 ;; ".*)[ \t]*then[ \t]*\\($\\|!\\)"
508 ;; This would also match ELSE branches, though. This does not seem
509 ;; right to me, because then one has neighbouring blocks that are
510 ;; not nested in each other.
511 "\\(if[ \t]*(\\(.*\\|"
512 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
513 "do\\|select[ \t]*case\\|where\\)\\)\\|"
514 (regexp-opt '("interface" "function" "map" "program"
515 "structure" "subroutine" "union"))
516 "\\|block[ \t]*data\\)[ \t]*")
517 "Regexp matching the start of a Fortran \"block\", from the line start.
518 A simple regexp cannot do this in fully correct fashion, so this
519 tries to strike a compromise between complexity and flexibility.
520 Used in the Fortran entry in `hs-special-modes-alist'.")
521
522 (add-to-list 'hs-special-modes-alist
523 `(fortran-mode ,fortran-start-block-re ,fortran-end-block-re
524 "^[cC*!]" fortran-end-of-block nil))
525
526 \f
527 (defvar fortran-mode-syntax-table
528 (let ((table (make-syntax-table)))
529 ;; We might like `;' to be punctuation (g77 multi-statement
530 ;; lines), but that screws abbrevs.
531 (modify-syntax-entry ?\; "w" table)
532 (modify-syntax-entry ?\r " " table)
533 (modify-syntax-entry ?+ "." table)
534 (modify-syntax-entry ?- "." table)
535 (modify-syntax-entry ?= "." table)
536 (modify-syntax-entry ?* "." table)
537 (modify-syntax-entry ?/ "." table)
538 (modify-syntax-entry ?\' "\"" table)
539 (modify-syntax-entry ?\" "\"" table)
540 ;; Consistent with GNU Fortran's default -- see the manual.
541 ;; The F77 standard imposes no rule on this issue.
542 (modify-syntax-entry ?\\ "\\" table)
543 ;; This might be better as punctuation, as for C, but this way you
544 ;; can treat floating-point numbers as symbols.
545 (modify-syntax-entry ?. "_" table) ; e.g. `a.ne.b'
546 (modify-syntax-entry ?_ "_" table)
547 (modify-syntax-entry ?$ "_" table) ; esp. VMSisms
548 (modify-syntax-entry ?\! "<" table)
549 (modify-syntax-entry ?\n ">" table)
550 table)
551 "Syntax table used in Fortran mode.")
552
553 (defvar fortran-gud-syntax-table
554 (let ((st (make-syntax-table fortran-mode-syntax-table)))
555 (modify-syntax-entry ?\n "." st)
556 st)
557 "Syntax table used to parse Fortran expressions for printing in GUD.")
558
559 (defvar fortran-mode-map
560 (let ((map (make-sparse-keymap)))
561 (define-key map ";" 'fortran-abbrev-start)
562 (define-key map "\C-c;" 'fortran-comment-region)
563 (define-key map "\M-;" 'fortran-indent-comment)
564 (define-key map "\M-\n" 'fortran-split-line)
565 (define-key map "\M-\C-n" 'fortran-end-of-block)
566 (define-key map "\M-\C-p" 'fortran-beginning-of-block)
567 (define-key map "\M-\C-q" 'fortran-indent-subprogram)
568 (define-key map "\C-c\C-w" 'fortran-window-create-momentarily)
569 (define-key map "\C-c\C-r" 'fortran-column-ruler)
570 (define-key map "\C-c\C-p" 'fortran-previous-statement)
571 (define-key map "\C-c\C-n" 'fortran-next-statement)
572 (define-key map "\C-c\C-d" 'fortran-join-line) ; like f90
573 (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation
574 (define-key map "0" 'fortran-electric-line-number)
575 (define-key map "1" 'fortran-electric-line-number)
576 (define-key map "2" 'fortran-electric-line-number)
577 (define-key map "3" 'fortran-electric-line-number)
578 (define-key map "4" 'fortran-electric-line-number)
579 (define-key map "5" 'fortran-electric-line-number)
580 (define-key map "6" 'fortran-electric-line-number)
581 (define-key map "7" 'fortran-electric-line-number)
582 (define-key map "8" 'fortran-electric-line-number)
583 (define-key map "9" 'fortran-electric-line-number)
584
585 (easy-menu-define fortran-menu map "Menu for Fortran mode."
586 `("Fortran"
587 ["Manual" (info "(emacs)Fortran")]
588 ("Customization"
589 ,(custom-menu-create 'fortran)
590 ["Set" Custom-set t]
591 ["Save" Custom-save t]
592 ["Reset to Current" Custom-reset-current t]
593 ["Reset to Saved" Custom-reset-saved t]
594 ["Reset to Standard Settings" Custom-reset-standard t]
595 )
596 "--"
597 ["Comment Region" fortran-comment-region mark-active]
598 ["Uncomment Region"
599 (fortran-comment-region (region-beginning) (region-end) 1)
600 mark-active]
601 ["Indent Region" indent-region mark-active]
602 ["Indent Subprogram" fortran-indent-subprogram t]
603 "--"
604 ["Beginning of Subprogram" fortran-beginning-of-subprogram t]
605 ["End of Subprogram" fortran-end-of-subprogram t]
606 ("Mark"
607 ["Subprogram" mark-defun t]
608 ["IF Block" fortran-mark-if t]
609 ["DO Block" fortran-mark-do t]
610 )
611 ["Narrow to Subprogram" narrow-to-defun t]
612 ["Widen" widen t]
613 "--"
614 ["Temporary column ruler" fortran-column-ruler t]
615 ["72-column window" fortran-window-create t]
616 ["Full Width Window"
617 (enlarge-window-horizontally (- (frame-width) (window-width)))
618 (not (window-full-width-p))]
619 ["Momentary 72-column window" fortran-window-create-momentarily t]
620 "--"
621 ["Break Line at Point" fortran-split-line t]
622 ["Join Line" fortran-join-line t]
623 ["Fill Statement/Comment" fill-paragraph t]
624 "--"
625 ["Toggle auto-fill" auto-fill-mode :selected auto-fill-function
626 :style toggle]
627 ["Toggle abbrev-mode" abbrev-mode :selected abbrev-mode
628 :style toggle]
629 ["Add imenu Menu" imenu-add-menubar-index
630 :active (not (lookup-key (current-local-map) [menu-bar index]))
631 :included (fboundp 'imenu-add-to-menubar)]))
632 map)
633 "Keymap used in Fortran mode.")
634
635 \f
636 (defvar fortran-mode-abbrev-table
637 (progn
638 (define-abbrev-table 'fortran-mode-abbrev-table nil)
639 fortran-mode-abbrev-table)
640 "Abbrev table for Fortran mode.")
641
642 (let (abbrevs-changed)
643 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
644 ;; Only use `apply' to quieten the byte-compiler.
645 (mapcar
646 (function (lambda (element)
647 (condition-case nil
648 (apply 'define-abbrev fortran-mode-abbrev-table
649 (append element '(nil 0 t)))
650 (wrong-number-of-arguments
651 (apply 'define-abbrev fortran-mode-abbrev-table
652 (append element '(nil 0)))))))
653 '((";au" "automatic" )
654 (";b" "byte" )
655 (";bd" "block data" )
656 (";ch" "character" )
657 (";cl" "close" )
658 (";c" "continue" )
659 (";cm" "common" )
660 (";cx" "complex" )
661 (";df" "define" )
662 (";di" "dimension" )
663 (";do" "double" )
664 (";dc" "double complex" )
665 (";dp" "double precision" )
666 (";dw" "do while" )
667 (";e" "else" )
668 (";ed" "enddo" )
669 (";el" "elseif" )
670 (";en" "endif" )
671 (";eq" "equivalence" )
672 (";ew" "endwhere" )
673 (";ex" "external" )
674 (";ey" "entry" )
675 (";f" "format" )
676 (";fa" ".false." )
677 (";fu" "function" )
678 (";g" "goto" )
679 (";im" "implicit" )
680 (";ib" "implicit byte" )
681 (";ic" "implicit complex" )
682 (";ich" "implicit character")
683 (";ii" "implicit integer" )
684 (";il" "implicit logical" )
685 (";ir" "implicit real" )
686 (";inc" "include" )
687 (";in" "integer" )
688 (";intr" "intrinsic" )
689 (";l" "logical" )
690 (";n" "namelist" )
691 (";o" "open" ) ; was ;op
692 (";pa" "parameter" )
693 (";pr" "program" )
694 (";ps" "pause" )
695 (";p" "print" )
696 (";rc" "record" )
697 (";re" "real" )
698 (";r" "read" )
699 (";rt" "return" )
700 (";rw" "rewind" )
701 (";s" "stop" )
702 (";sa" "save" )
703 (";st" "structure" )
704 (";sc" "static" )
705 (";su" "subroutine" )
706 (";tr" ".true." )
707 (";ty" "type" )
708 (";vo" "volatile" )
709 (";w" "write" )
710 (";wh" "where" ))))
711
712 \f
713 ;;;###autoload
714 (defun fortran-mode ()
715 "Major mode for editing Fortran code in fixed format.
716 For free format code, use `f90-mode'.
717
718 \\[fortran-indent-line] indents the current Fortran line correctly.
719 Note that DO statements must not share a common CONTINUE.
720
721 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
722 Fortran keywords.
723
724 Key definitions:
725 \\{fortran-mode-map}
726
727 Variables controlling indentation style and extra features:
728
729 `fortran-comment-line-start'
730 To use comments starting with `!', set this to the string \"!\".
731 `fortran-do-indent'
732 Extra indentation within DO blocks (default 3).
733 `fortran-if-indent'
734 Extra indentation within IF blocks (default 3).
735 `fortran-structure-indent'
736 Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
737 (default 3)
738 `fortran-continuation-indent'
739 Extra indentation applied to continuation statements (default 5).
740 `fortran-comment-line-extra-indent'
741 Amount of extra indentation for text in full-line comments (default 0).
742 `fortran-comment-indent-style'
743 How to indent the text in full-line comments. Allowed values are:
744 nil don't change the indentation
745 fixed indent to `fortran-comment-line-extra-indent' beyond the
746 value of either
747 `fortran-minimum-statement-indent-fixed' (fixed format) or
748 `fortran-minimum-statement-indent-tab' (TAB format),
749 depending on the continuation format in use.
750 relative indent to `fortran-comment-line-extra-indent' beyond the
751 indentation for a line of code.
752 (default 'fixed)
753 `fortran-comment-indent-char'
754 Single-character string to be inserted instead of space for
755 full-line comment indentation (default \" \").
756 `fortran-minimum-statement-indent-fixed'
757 Minimum indentation for statements in fixed format mode (default 6).
758 `fortran-minimum-statement-indent-tab'
759 Minimum indentation for statements in TAB format mode (default 9).
760 `fortran-line-number-indent'
761 Maximum indentation for line numbers (default 1). A line number will
762 get less than this much indentation if necessary to avoid reaching
763 column 5.
764 `fortran-check-all-num-for-matching-do'
765 Non-nil causes all numbered lines to be treated as possible \"continue\"
766 statements (default nil).
767 `fortran-blink-matching-if'
768 Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
769 to blink on the matching IF (or DO [WHILE]). (default nil)
770 `fortran-continuation-string'
771 Single-character string to be inserted in column 5 of a continuation
772 line (default \"$\").
773 `fortran-comment-region'
774 String inserted by \\[fortran-comment-region] at start of each line in
775 the region (default \"c$$$\").
776 `fortran-electric-line-number'
777 Non-nil causes line number digits to be moved to the correct column
778 as typed (default t).
779 `fortran-break-before-delimiters'
780 Non-nil causes lines to be broken before delimiters (default t).
781
782 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
783 with no args, if that value is non-nil."
784 (interactive)
785 (kill-all-local-variables)
786 (setq major-mode 'fortran-mode
787 mode-name "Fortran"
788 local-abbrev-table fortran-mode-abbrev-table)
789 (set-syntax-table fortran-mode-syntax-table)
790 (use-local-map fortran-mode-map)
791 (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
792 (set (make-local-variable 'indent-region-function)
793 (lambda (start end)
794 (let (fortran-blink-matching-if ; avoid blinking delay
795 indent-region-function)
796 (indent-region start end nil))))
797 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
798 ;; The syntax tables don't understand the column-0 comment-markers.
799 (set (make-local-variable 'comment-use-syntax) nil)
800 (set (make-local-variable 'comment-padding) "$$$")
801 (set (make-local-variable 'comment-start) fortran-comment-line-start)
802 (set (make-local-variable 'comment-start-skip)
803 ;; We can't reuse `fortran-comment-line-start-skip' directly because
804 ;; it contains backrefs whereas we need submatch-1 to end at the
805 ;; beginning of the comment delimiter.
806 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
807 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
808 (set (make-local-variable 'comment-indent-function) 'fortran-comment-indent)
809 (set (make-local-variable 'abbrev-all-caps) t)
810 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
811 (set (make-local-variable 'indent-tabs-mode) (fortran-analyze-file-format))
812 (setq mode-line-process '(indent-tabs-mode fortran-tab-mode-string))
813 (set (make-local-variable 'fill-column) 72)
814 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
815 (set (make-local-variable 'font-lock-defaults)
816 '((fortran-font-lock-keywords
817 fortran-font-lock-keywords-1
818 fortran-font-lock-keywords-2
819 fortran-font-lock-keywords-3
820 fortran-font-lock-keywords-4)
821 nil t ((?/ . "$/") ("_$" . "w"))
822 fortran-beginning-of-subprogram))
823 (set (make-local-variable 'font-lock-syntactic-keywords)
824 fortran-font-lock-syntactic-keywords)
825 (set (make-local-variable 'imenu-case-fold-search) t)
826 (set (make-local-variable 'imenu-generic-expression)
827 fortran-imenu-generic-expression)
828 (set (make-local-variable 'imenu-syntax-alist) '(("_$" . "w")))
829 (set (make-local-variable 'beginning-of-defun-function)
830 #'fortran-beginning-of-subprogram)
831 (set (make-local-variable 'end-of-defun-function)
832 #'fortran-end-of-subprogram)
833 (set (make-local-variable 'add-log-current-defun-function)
834 #'fortran-current-defun)
835 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
836 (set (make-local-variable 'gud-find-expr-function) 'fortran-gud-find-expr)
837 (run-mode-hooks 'fortran-mode-hook))
838
839 \f
840 (defun fortran-gud-find-expr ()
841 ;; Consider \n as punctuation (end of expression).
842 (with-syntax-table fortran-gud-syntax-table
843 (gud-find-c-expr)))
844
845 (defsubst fortran-comment-indent ()
846 "Return the indentation appropriate for the current comment line.
847 This is 0 for a line matching `fortran-comment-line-start-skip', else
848 the value of `comment-column' (leaving at least one space after code)."
849 (if (looking-at fortran-comment-line-start-skip) 0
850 (save-excursion
851 (skip-chars-backward " \t")
852 (max (1+ (current-column)) comment-column))))
853
854 (defun fortran-indent-comment ()
855 "Align or create comment on current line.
856 Existing comments of all types are recognized and aligned.
857 If the line has no comment, a side-by-side comment is inserted and aligned,
858 if the value of `comment-start' is not nil and allows such comments.
859 Otherwise, a separate-line comment is inserted, on this line
860 or on a new line inserted before this line if this line is not blank."
861 (interactive "*")
862 (beginning-of-line)
863 ;; Recognize existing comments of either kind.
864 (cond ((fortran-find-comment-start-skip 'all)
865 (goto-char (match-beginning 0))
866 (if (bolp)
867 (fortran-indent-line)
868 (unless (= (current-column) (fortran-comment-indent))
869 (delete-horizontal-space)
870 (indent-to (fortran-comment-indent)))))
871 ;; No existing comment.
872 ;; If side-by-side comments are defined, insert one,
873 ;; unless line is now blank.
874 ((and comment-start (not (looking-at "[ \t]*$"))
875 (string-match comment-start-skip (concat " " comment-start)))
876 (end-of-line)
877 (delete-horizontal-space)
878 (indent-to (fortran-comment-indent))
879 (insert comment-start))
880 ;; Else insert separate-line comment, making a new line if nec.
881 (t
882 (if (looking-at "^[ \t]*$")
883 (delete-horizontal-space)
884 (beginning-of-line)
885 (insert ?\n)
886 (forward-char -1))
887 (insert fortran-comment-line-start)
888 (insert-char (if (stringp fortran-comment-indent-char)
889 (aref fortran-comment-indent-char 0)
890 fortran-comment-indent-char)
891 (- (fortran-calculate-indent) (current-column))))))
892
893 (defun fortran-comment-region (beg-region end-region arg)
894 "Comment every line in the region.
895 Inserts the string variable `fortran-comment-region' at the beginning of
896 every line in the region.
897 BEG-REGION and END-REGION specify the region boundaries.
898 With non-nil ARG, uncomments the region."
899 (interactive "*r\nP")
900 (let ((end-region-mark (copy-marker end-region))
901 (save-point (point-marker)))
902 (goto-char beg-region)
903 (beginning-of-line)
904 (if arg
905 (let ((com (regexp-quote fortran-comment-region))) ; uncomment
906 (if (looking-at com)
907 (delete-region (point) (match-end 0)))
908 (while (and (zerop (forward-line 1))
909 (< (point) end-region-mark))
910 (if (looking-at com)
911 (delete-region (point) (match-end 0)))))
912 (insert fortran-comment-region) ; comment
913 (while (and (zerop (forward-line 1))
914 (< (point) end-region-mark))
915 (insert fortran-comment-region)))
916 (goto-char save-point)
917 (set-marker end-region-mark nil)
918 (set-marker save-point nil)))
919
920 \f
921 (defun fortran-abbrev-start ()
922 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
923 Any other key combination is executed normally."
924 (interactive "*")
925 (insert last-command-char)
926 (let* ((event (if (fboundp 'next-command-event) ; XEmacs
927 (next-command-event)
928 (read-event)))
929 (char (if (fboundp 'event-to-character)
930 (event-to-character event) event)))
931 ;; Insert char if not equal to `?', or if abbrev-mode is off.
932 (if (and abbrev-mode (or (eq char ??) (eq char help-char)
933 (memq event help-event-list)))
934 (fortran-abbrev-help)
935 (push event unread-command-events))))
936
937 (defun fortran-abbrev-help ()
938 "List the currently defined abbrevs in Fortran mode."
939 (interactive)
940 (message "Listing abbrev table...")
941 (display-buffer (fortran-prepare-abbrev-list-buffer))
942 (message "Listing abbrev table...done"))
943
944 (defun fortran-prepare-abbrev-list-buffer ()
945 "Create a buffer listing the Fortran mode abbreviations."
946 (with-current-buffer (get-buffer-create "*Abbrevs*")
947 (erase-buffer)
948 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
949 (goto-char (point-min))
950 (set-buffer-modified-p nil)
951 (edit-abbrevs-mode))
952 (get-buffer-create "*Abbrevs*"))
953
954 (defun fortran-column-ruler ()
955 "Insert a column ruler momentarily above current line, till next keystroke.
956 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
957 format mode, and `fortran-column-ruler-tab' in TAB format mode.
958 The next key typed is executed unless it is SPC."
959 (interactive)
960 (momentary-string-display
961 (if indent-tabs-mode
962 fortran-column-ruler-tab
963 fortran-column-ruler-fixed)
964 (save-excursion
965 (beginning-of-line)
966 (if (eq (window-start (selected-window))
967 (window-point (selected-window)))
968 (line-beginning-position 2)
969 (point)))
970 nil "Type SPC or any command to erase ruler."))
971
972 (defun fortran-window-create ()
973 "Make the window 72 columns wide.
974 See also `fortran-window-create-momentarily'."
975 (interactive)
976 (let ((window-min-width 2))
977 (unless (window-full-width-p)
978 (enlarge-window-horizontally (- (frame-width)
979 (window-width) 1)))
980 (let* ((window-edges (window-edges))
981 (scroll-bar-width (- (nth 2 window-edges)
982 (car window-edges)
983 (window-width))))
984 (split-window-horizontally (+ 72 scroll-bar-width)))
985 (other-window 1)
986 (switch-to-buffer " fortran-window-extra" t)
987 (select-window (previous-window))))
988
989 (defun fortran-window-create-momentarily (&optional arg)
990 "Momentarily make the window 72 columns wide.
991 Optional ARG non-nil and non-unity disables the momentary feature.
992 See also `fortran-window-create'."
993 (interactive "p")
994 (if (or (not arg)
995 (= arg 1))
996 (save-window-excursion
997 (progn
998 (condition-case nil
999 (fortran-window-create)
1000 (error (error "No room for Fortran window")))
1001 (message "Type SPC to continue editing.")
1002 (let ((char (read-event)))
1003 (or (equal char ?\s)
1004 (setq unread-command-events (list char))))))
1005 (fortran-window-create)))
1006
1007 (defun fortran-split-line ()
1008 "Break line at point and insert continuation marker and alignment."
1009 (interactive "*")
1010 (delete-horizontal-space)
1011 (if (save-excursion
1012 (let ((pos (point)))
1013 (beginning-of-line)
1014 (and (fortran-find-comment-start-skip 'all)
1015 (< (match-beginning 0) pos))))
1016 (insert ?\n (match-string 0))
1017 (if indent-tabs-mode
1018 (insert ?\n ?\t (fortran-numerical-continuation-char))
1019 (insert "\n " fortran-continuation-string))) ; space after \n important
1020 (fortran-indent-line)) ; when cont string is C, c or *
1021
1022 (defun fortran-remove-continuation ()
1023 "Delete any Fortran continuation characters at point.
1024 Returns t if anything actually deleted."
1025 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1026 (replace-match "")
1027 (delete-indentation)
1028 t))
1029
1030 (defun fortran-join-line (arg)
1031 "Join current line to the previous one and re-indent.
1032 With a prefix argument, repeat this operation that many times.
1033 If the prefix argument ARG is negative, join the next -ARG lines.
1034 Continuation lines are correctly handled."
1035 (interactive "*p")
1036 (save-excursion
1037 (when (> 0 arg)
1038 (setq arg (- arg))
1039 (forward-line arg))
1040 (while (not (zerop arg))
1041 (beginning-of-line)
1042 (or (fortran-remove-continuation)
1043 (delete-indentation))
1044 (setq arg (1- arg)))
1045 (fortran-indent-line)))
1046
1047 (defun fortran-numerical-continuation-char ()
1048 "Return a digit for tab-digit style of continuation lines.
1049 If previous line is a tab-digit continuation line, return that digit
1050 plus one, otherwise return 1. Zero not allowed."
1051 (save-excursion
1052 (forward-line -1)
1053 (if (looking-at "\t[1-9]")
1054 (+ ?1 (% (- (char-after (1+ (point))) ?0) 9))
1055 ?1)))
1056
1057 (put 'fortran-electric-line-number 'delete-selection t)
1058 (defun fortran-electric-line-number (arg)
1059 "Self insert, but if part of a Fortran line number indent it automatically.
1060 Auto-indent does not happen if a numeric ARG is used."
1061 (interactive "*P")
1062 (if (or arg (not fortran-electric-line-number))
1063 (if arg
1064 (self-insert-command (prefix-numeric-value arg))
1065 (self-insert-command 1))
1066 (if (or (and (= 5 (current-column))
1067 (save-excursion
1068 (beginning-of-line)
1069 ;; In col 5 with only spaces to the left.
1070 (looking-at " \\{5\\}")))
1071 (and (= (if indent-tabs-mode
1072 fortran-minimum-statement-indent-tab
1073 fortran-minimum-statement-indent-fixed) (current-column))
1074 ;; In col 8 with a single tab to the left.
1075 (eq ?\t (char-after (line-beginning-position)))
1076 (not (or (eq last-command 'fortran-indent-line)
1077 (eq last-command
1078 'fortran-indent-new-line))))
1079 (save-excursion
1080 (re-search-backward "[^ \t0-9]"
1081 (line-beginning-position)
1082 t)) ; not a line number
1083 (looking-at "[0-9]")) ; within a line number
1084 (self-insert-command (prefix-numeric-value arg))
1085 (skip-chars-backward " \t")
1086 (insert last-command-char)
1087 (fortran-indent-line))))
1088
1089 \f
1090 (defun fortran-check-end-prog-re ()
1091 "Check a preliminary match against `fortran-end-prog-re'."
1092 ;; Having got a possible match for the subprogram end, we need a
1093 ;; match of whitespace, avoiding possible column 73+ stuff.
1094 (save-match-data
1095 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
1096 (buffer-substring (match-end 0)
1097 (min (line-end-position)
1098 (+ 72 (line-beginning-position)))))))
1099
1100 ;; Note that you can't just check backwards for `subroutine' &c in
1101 ;; case of un-marked main programs not at the start of the file.
1102 (defun fortran-beginning-of-subprogram ()
1103 "Move point to the beginning of the current Fortran subprogram."
1104 (interactive)
1105 (save-match-data
1106 (let ((case-fold-search t))
1107 (beginning-of-line -1)
1108 (if (catch 'ok
1109 (while (re-search-backward fortran-end-prog-re nil 'move)
1110 (if (fortran-check-end-prog-re)
1111 (throw 'ok t))))
1112 (forward-line)))))
1113
1114 (defun fortran-end-of-subprogram ()
1115 "Move point to the end of the current Fortran subprogram."
1116 (interactive)
1117 (save-match-data
1118 (let ((case-fold-search t))
1119 (if (save-excursion ; on END
1120 (beginning-of-line)
1121 (and (looking-at fortran-end-prog-re)
1122 (fortran-check-end-prog-re)))
1123 (forward-line)
1124 (beginning-of-line 2)
1125 (catch 'ok
1126 (while (re-search-forward fortran-end-prog-re nil 'move)
1127 (if (fortran-check-end-prog-re)
1128 (throw 'ok t))))
1129 (goto-char (match-beginning 0))
1130 (forward-line)))))
1131
1132 (defun fortran-previous-statement ()
1133 "Move point to beginning of the previous Fortran statement.
1134 Returns 'first-statement if that statement is the first
1135 non-comment Fortran statement in the file, and nil otherwise.
1136 Directive lines are treated as comments."
1137 (interactive)
1138 (let (not-first-statement continue-test)
1139 (beginning-of-line)
1140 (setq continue-test
1141 (and
1142 (not (looking-at fortran-comment-line-start-skip))
1143 (not (looking-at fortran-directive-re))
1144 (or (looking-at
1145 (concat "[ \t]*"
1146 (regexp-quote fortran-continuation-string)))
1147 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1148 (while (and (setq not-first-statement (zerop (forward-line -1)))
1149 (or (looking-at fortran-comment-line-start-skip)
1150 (looking-at fortran-directive-re)
1151 (looking-at
1152 (concat "[ \t]*"
1153 (regexp-quote fortran-continuation-string)))
1154 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1155 (looking-at (concat "[ \t]*" comment-start-skip)))))
1156 (cond ((and continue-test
1157 (not not-first-statement))
1158 (message "Incomplete continuation statement."))
1159 (continue-test
1160 (fortran-previous-statement))
1161 ((not not-first-statement)
1162 'first-statement))))
1163
1164 (defun fortran-next-statement ()
1165 "Move point to beginning of the next Fortran statement.
1166 Returns 'last-statement if that statement is the last
1167 non-comment Fortran statement in the file, and nil otherwise.
1168 Directive lines are treated as comments."
1169 (interactive)
1170 (let (not-last-statement)
1171 (beginning-of-line)
1172 (while (and (setq not-last-statement
1173 (and (zerop (forward-line 1))
1174 (not (eobp))))
1175 (or (looking-at fortran-comment-line-start-skip)
1176 (looking-at fortran-directive-re)
1177 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1178 (looking-at (concat "[ \t]*" comment-start-skip)))))
1179 (if (not not-last-statement)
1180 'last-statement)))
1181
1182 (defun fortran-looking-at-if-then ()
1183 "Return non-nil if at the start of a line with an IF ... THEN statement."
1184 ;; cf f90-looking-at-if-then.
1185 (let ((p (point))
1186 (i (fortran-beginning-if)))
1187 (if i
1188 (save-excursion
1189 (goto-char i)
1190 (beginning-of-line)
1191 (= (point) p)))))
1192
1193 ;; Used in hs-special-modes-alist.
1194 (defun fortran-end-of-block (&optional num)
1195 "Move point forward to the end of the current code block.
1196 With optional argument NUM, go forward that many balanced blocks.
1197 If NUM is negative, go backward to the start of a block. Does
1198 not check for consistency of block types. Interactively, pushes
1199 mark before moving point."
1200 (interactive "p")
1201 (if (interactive-p) (push-mark (point) t))
1202 (and num (< num 0) (fortran-beginning-of-block (- num)))
1203 (let ((case-fold-search t)
1204 (count (or num 1)))
1205 (end-of-line)
1206 (while (and (> count 0)
1207 (re-search-forward
1208 (concat "\\(" fortran-blocks-re
1209 (if fortran-check-all-num-for-matching-do
1210 "\\|^[ \t]*[0-9]+" "")
1211 "\\|continue\\|end\\)\\>")
1212 nil 'move))
1213 (beginning-of-line)
1214 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1215 (fortran-looking-at-if-then)
1216 (looking-at fortran-start-block-re))
1217 (setq count (1+ count))
1218 (if (or (looking-at fortran-end-block-re)
1219 (and (or (looking-at "^[0-9 \t]*continue")
1220 (and fortran-check-all-num-for-matching-do
1221 (looking-at "[ \t]*[0-9]+")))
1222 (fortran-check-for-matching-do)))
1223 (setq count (1- count))))
1224 (end-of-line))
1225 (if (> count 0) (error "Missing block end"))))
1226
1227 (defun fortran-beginning-of-block (&optional num)
1228 "Move point backwards to the start of the current code block.
1229 With optional argument NUM, go backward that many balanced
1230 blocks. If NUM is negative, go forward to the end of a block.
1231 Does not check for consistency of block types. Interactively,
1232 pushes mark before moving point."
1233 (interactive "p")
1234 (if (interactive-p) (push-mark (point) t))
1235 (and num (< num 0) (fortran-end-of-block (- num)))
1236 (let ((case-fold-search t)
1237 (count (or num 1)))
1238 (beginning-of-line)
1239 (while (and (> count 0)
1240 (re-search-backward
1241 (concat "\\(" fortran-blocks-re
1242 (if fortran-check-all-num-for-matching-do
1243 "\\|^[ \t]*[0-9]+" "")
1244 "\\|continue\\|end\\)\\>")
1245 nil 'move))
1246 (beginning-of-line)
1247 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1248 (fortran-looking-at-if-then)
1249 (looking-at fortran-start-block-re))
1250 (setq count (1- count))
1251 (if (or (looking-at fortran-end-block-re)
1252 (and (or (looking-at "^[0-9 \t]*continue")
1253 (and fortran-check-all-num-for-matching-do
1254 (looking-at "[ \t]*[0-9]+")))
1255 (fortran-check-for-matching-do)))
1256 (setq count (1+ count)))))
1257 ;; Includes an un-named main program block.
1258 (if (> count 0) (error "Missing block start"))))
1259
1260 \f
1261 (defun fortran-blink-match (regex keyword find-begin)
1262 "From a line matching REGEX, blink matching KEYWORD statement line.
1263 Use function FIND-BEGIN to match it."
1264 (let ((top-of-window (window-start))
1265 (end-point (point))
1266 (case-fold-search t)
1267 matching
1268 message)
1269 (when (save-excursion
1270 (beginning-of-line)
1271 (skip-chars-forward " \t0-9")
1272 (looking-at regex))
1273 (if (not (setq matching (funcall find-begin)))
1274 (setq message (concat "No matching " keyword "."))
1275 (if (< matching top-of-window)
1276 (save-excursion
1277 (goto-char matching)
1278 (beginning-of-line)
1279 (setq message
1280 (concat "Matches "
1281 (buffer-substring (point)
1282 (line-end-position)))))))
1283 (if message
1284 (message "%s" message)
1285 (goto-char matching)
1286 (sit-for blink-matching-delay)
1287 (goto-char end-point)))))
1288
1289 (defun fortran-blink-matching-if ()
1290 "From an ENDIF or ELSE statement, blink the matching IF statement."
1291 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1292 "if" #'fortran-beginning-if))
1293
1294 (defun fortran-blink-matching-do ()
1295 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1296 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
1297
1298 (defun fortran-mark-do ()
1299 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1300 The marks are pushed."
1301 (interactive)
1302 (let (enddo-point do-point)
1303 (if (setq enddo-point (fortran-end-do))
1304 (if (not (setq do-point (fortran-beginning-do)))
1305 (message "No matching do.")
1306 (goto-char enddo-point)
1307 (push-mark)
1308 (goto-char do-point)))))
1309
1310 (defun fortran-end-do ()
1311 "Search forward for first unmatched ENDDO.
1312 Return point or nil."
1313 (let ((case-fold-search t))
1314 (if (save-excursion (beginning-of-line)
1315 (skip-chars-forward " \t0-9")
1316 (looking-at "end[ \t]*do\\b"))
1317 ;; Sitting on one.
1318 (match-beginning 0)
1319 ;; Search for one.
1320 (save-excursion
1321 (let ((count 1))
1322 (while (and (not (zerop count))
1323 (not (eq (fortran-next-statement) 'last-statement))
1324 ;; Keep local to subprogram.
1325 (not (and (looking-at fortran-end-prog-re)
1326 (fortran-check-end-prog-re))))
1327 (skip-chars-forward " \t0-9")
1328 (cond ((looking-at "end[ \t]*do\\b")
1329 (setq count (1- count)))
1330 ((looking-at
1331 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1332 (setq count (1+ count)))))
1333 (and (zerop count)
1334 ;; All pairs accounted for.
1335 (point)))))))
1336
1337 (defun fortran-beginning-do ()
1338 "Search backwards for first unmatched DO [WHILE].
1339 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1340 (let ((case-fold-search t)
1341 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1342 (if (save-excursion
1343 (beginning-of-line)
1344 (skip-chars-forward " \t0-9")
1345 (looking-at dostart-re))
1346 ;; Sitting on one.
1347 (match-beginning 0)
1348 ;; Search for one.
1349 (save-excursion
1350 (let ((count 1))
1351 (while (and (not (zerop count))
1352 (not (eq (fortran-previous-statement) 'first-statement))
1353 ;; Keep local to subprogram.
1354 (not (and (looking-at fortran-end-prog-re)
1355 (fortran-check-end-prog-re))))
1356 (skip-chars-forward " \t0-9")
1357 (cond ((looking-at dostart-re)
1358 (setq count (1- count)))
1359 ;; Note labelled loop ends not considered.
1360 ((looking-at "end[ \t]*do\\b")
1361 (setq count (1+ count)))))
1362 (and (zerop count)
1363 ;; All pairs accounted for.
1364 (point)))))))
1365
1366 (defun fortran-mark-if ()
1367 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1368 The marks are pushed."
1369 (interactive)
1370 (let (endif-point if-point)
1371 (if (setq endif-point (fortran-end-if))
1372 (if (not (setq if-point (fortran-beginning-if)))
1373 (message "No matching if.")
1374 ;; Set mark, move point.
1375 (goto-char endif-point)
1376 (push-mark)
1377 (goto-char if-point)))))
1378
1379 (defun fortran-end-if ()
1380 "Search forwards for first unmatched ENDIF.
1381 Return point or nil."
1382 (let ((case-fold-search t))
1383 (if (save-excursion (beginning-of-line)
1384 (skip-chars-forward " \t0-9")
1385 (looking-at "end[ \t]*if\\b"))
1386 ;; Sitting on one.
1387 (match-beginning 0)
1388 ;; Search for one. The point has been already been moved to first
1389 ;; letter on line but this should not cause troubles.
1390 (save-excursion
1391 (let ((count 1))
1392 (while (and (not (zerop count))
1393 (not (eq (fortran-next-statement) 'last-statement))
1394 ;; Keep local to subprogram.
1395 (not (and (looking-at fortran-end-prog-re)
1396 (fortran-check-end-prog-re))))
1397 (skip-chars-forward " \t0-9")
1398 (cond ((looking-at "end[ \t]*if\\b")
1399 (setq count (1- count)))
1400 ((looking-at fortran-if-start-re)
1401 (save-excursion
1402 (if (or
1403 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1404 (let (then-test) ; multi-line if-then
1405 (while
1406 (and
1407 (zerop (forward-line 1))
1408 ;; Search forward for then.
1409 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1410 (not
1411 (setq then-test
1412 (looking-at
1413 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1414 then-test))
1415 (setq count (1+ count)))))))
1416 (and (zerop count)
1417 ;; All pairs accounted for.
1418 (point)))))))
1419
1420 (defun fortran-beginning-if ()
1421 "Search backwards for first unmatched IF-THEN.
1422 Return point or nil."
1423 (let ((case-fold-search t))
1424 (if (save-excursion
1425 ;; May be sitting on multi-line if-then statement, first
1426 ;; move to beginning of current statement. Note:
1427 ;; `fortran-previous-statement' moves to previous statement
1428 ;; *unless* current statement is first one. Only move
1429 ;; forward if not first-statement.
1430 (if (not (eq (fortran-previous-statement) 'first-statement))
1431 (fortran-next-statement))
1432 (skip-chars-forward " \t0-9")
1433 (and
1434 (looking-at fortran-if-start-re)
1435 (save-match-data
1436 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1437 ;; Multi-line if-then.
1438 (let (then-test)
1439 (while
1440 (and (zerop (forward-line 1))
1441 ;; Search forward for then.
1442 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1443 (not
1444 (setq then-test
1445 (looking-at
1446 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1447 then-test)))))
1448 ;; Sitting on one.
1449 (match-beginning 0)
1450 ;; Search for one.
1451 (save-excursion
1452 (let ((count 1))
1453 (while (and (not (zerop count))
1454 (not (eq (fortran-previous-statement) 'first-statement))
1455 ;; Keep local to subprogram.
1456 (not (and (looking-at fortran-end-prog-re)
1457 (fortran-check-end-prog-re))))
1458 (skip-chars-forward " \t0-9")
1459 (cond ((looking-at fortran-if-start-re)
1460 (save-excursion
1461 (if (or
1462 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1463 (let (then-test) ; multi-line if-then
1464 (while
1465 (and
1466 (zerop (forward-line 1))
1467 ;; Search forward for then.
1468 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1469 (not
1470 (setq then-test
1471 (looking-at
1472 (concat ".*then\\b[ \t]*"
1473 "[^ \t(=a-z0-9]"))))))
1474 then-test))
1475 (setq count (1- count)))))
1476 ((looking-at "end[ \t]*if\\b")
1477 (setq count (1+ count)))))
1478 (and (zerop count)
1479 ;; All pairs accounted for.
1480 (point)))))))
1481
1482 \f
1483 (defun fortran-indent-line ()
1484 "Indent current Fortran line based on its contents and on previous lines."
1485 (interactive "*")
1486 (let ((cfi (fortran-calculate-indent)))
1487 (save-excursion
1488 (beginning-of-line)
1489 (if (or (not (= cfi (fortran-current-line-indentation)))
1490 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1491 (not (fortran-line-number-indented-correctly-p))))
1492 (fortran-indent-to-column cfi)
1493 (beginning-of-line)
1494 (if (fortran-find-comment-start-skip)
1495 (fortran-indent-comment))))
1496 ;; Never leave point in left margin.
1497 (if (< (current-column) cfi)
1498 (move-to-column cfi))
1499 (and auto-fill-function
1500 (> (save-excursion (end-of-line) (current-column))
1501 fill-column)
1502 (save-excursion
1503 (end-of-line)
1504 (fortran-fill)))
1505 (when fortran-blink-matching-if
1506 (fortran-blink-matching-if)
1507 (fortran-blink-matching-do))))
1508
1509 (defun fortran-auto-fill ()
1510 "Function to use for `normal-auto-fill-function' in Fortran mode."
1511 (if (> (current-column) (current-fill-column))
1512 (let ((cfi (fortran-calculate-indent)))
1513 (save-excursion
1514 (beginning-of-line)
1515 (if (or (not (= cfi (fortran-current-line-indentation)))
1516 (and (re-search-forward "^[ \t]*[0-9]+"
1517 (+ (point) 4) t)
1518 (not (fortran-line-number-indented-correctly-p))))
1519 (fortran-indent-to-column cfi)
1520 (beginning-of-line)
1521 (if (fortran-find-comment-start-skip)
1522 (fortran-indent-comment))))
1523 (fortran-fill)
1524 ;; Never leave point in left margin.
1525 (if (< (current-column) cfi)
1526 (move-to-column cfi)))))
1527
1528 ;; Historically this was a separate function which advertised itself
1529 ;; as reindenting but only did so where `most likely to be necessary'.
1530 (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
1531
1532 (defun fortran-indent-subprogram ()
1533 "Properly indent the Fortran subprogram containing point."
1534 (interactive "*")
1535 (save-excursion
1536 (mark-defun)
1537 (message "Indenting subprogram...")
1538 (indent-region (point) (mark) nil))
1539 (message "Indenting subprogram...done."))
1540
1541 (defun fortran-calculate-indent ()
1542 "Calculates the Fortran indent column based on previous lines."
1543 (let (icol first-statement (case-fold-search t)
1544 (fortran-minimum-statement-indent
1545 (if indent-tabs-mode
1546 fortran-minimum-statement-indent-tab
1547 fortran-minimum-statement-indent-fixed)))
1548 (save-excursion
1549 (setq first-statement (fortran-previous-statement))
1550 (if first-statement
1551 (setq icol fortran-minimum-statement-indent)
1552 (if (= (point) (point-min))
1553 (setq icol fortran-minimum-statement-indent)
1554 (setq icol (fortran-current-line-indentation)))
1555 (skip-chars-forward " \t0-9")
1556 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1557 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1558 (let (then-test) ; multi-line if-then
1559 (while (and (zerop (forward-line 1))
1560 ;; Search forward for then.
1561 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1562 (not (setq then-test
1563 (looking-at
1564 ".*then\\b[ \t]\
1565 *[^ \t_$(=a-z0-9]")))))
1566 then-test))
1567 (setq icol (+ icol fortran-if-indent))))
1568 ((looking-at "else\\(if\\)?\\b")
1569 (setq icol (+ icol fortran-if-indent)))
1570 ((looking-at "select[ \t]*case[ \t](.*)")
1571 (setq icol (+ icol fortran-if-indent)))
1572 ((looking-at "case[ \t]*(.*)")
1573 (setq icol (+ icol fortran-if-indent)))
1574 ((looking-at "case[ \t]*default\\b")
1575 (setq icol (+ icol fortran-if-indent)))
1576 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1577 (setq icol (+ icol fortran-if-indent)))
1578 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1579 (setq icol (+ icol fortran-if-indent)))
1580 ((looking-at "do\\b")
1581 (setq icol (+ icol fortran-do-indent)))
1582 ((looking-at
1583 "\\(structure\\|union\\|map\\|interface\\)\
1584 \\b[ \t]*[^ \t=(a-z]")
1585 (setq icol (+ icol fortran-structure-indent)))
1586 ((and (looking-at fortran-end-prog-re1)
1587 (fortran-check-end-prog-re))
1588 ;; Previous END resets indent to minimum.
1589 (setq icol fortran-minimum-statement-indent)))))
1590 (save-excursion
1591 (beginning-of-line)
1592 (cond ((looking-at "[ \t]*$"))
1593 ;; Check for directive before comment, so as not to indent.
1594 ((looking-at fortran-directive-re)
1595 (setq fortran-minimum-statement-indent 0 icol 0))
1596 ((looking-at fortran-comment-line-start-skip)
1597 (cond ((eq fortran-comment-indent-style 'relative)
1598 (setq icol (+ icol fortran-comment-line-extra-indent)))
1599 ((eq fortran-comment-indent-style 'fixed)
1600 (setq icol (+ fortran-minimum-statement-indent
1601 fortran-comment-line-extra-indent))))
1602 (setq fortran-minimum-statement-indent 0))
1603 ((or (looking-at (concat "[ \t]*"
1604 (regexp-quote
1605 fortran-continuation-string)))
1606 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1607 (skip-chars-forward " \t")
1608 ;; Do not introduce extra whitespace into a broken string.
1609 (setq icol
1610 (if (fortran-is-in-string-p (point))
1611 6
1612 (+ icol fortran-continuation-indent))))
1613 (first-statement)
1614 ((and fortran-check-all-num-for-matching-do
1615 (looking-at "[ \t]*[0-9]+")
1616 (fortran-check-for-matching-do))
1617 (setq icol (- icol fortran-do-indent)))
1618 (t
1619 (skip-chars-forward " \t0-9")
1620 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1621 (setq icol (- icol fortran-if-indent)))
1622 ((looking-at "else\\(if\\)?\\b")
1623 (setq icol (- icol fortran-if-indent)))
1624 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1625 (setq icol (- icol fortran-if-indent)))
1626 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1627 (setq icol (- icol fortran-if-indent)))
1628 ((and (looking-at "continue\\b")
1629 (fortran-check-for-matching-do))
1630 (setq icol (- icol fortran-do-indent)))
1631 ((looking-at "end[ \t]*do\\b")
1632 (setq icol (- icol fortran-do-indent)))
1633 ((looking-at "end[ \t]*\
1634 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1635 (setq icol (- icol fortran-structure-indent)))
1636 ((and (looking-at fortran-end-prog-re1)
1637 (fortran-check-end-prog-re)
1638 (not (= icol fortran-minimum-statement-indent)))
1639 (message "Warning: `end' not in column %d. Probably\
1640 an unclosed block." fortran-minimum-statement-indent))))))
1641 (max fortran-minimum-statement-indent icol)))
1642
1643 \f
1644 (defun fortran-current-line-indentation ()
1645 "Indentation of current line, ignoring Fortran line number or continuation.
1646 This is the column position of the first non-whitespace character
1647 aside from the line number and/or column 5/8 line-continuation character.
1648 For comment lines, returns indentation of the first
1649 non-indentation text within the comment."
1650 (save-excursion
1651 (beginning-of-line)
1652 (cond ((looking-at fortran-comment-line-start-skip)
1653 (goto-char (match-end 0))
1654 (skip-chars-forward
1655 (if (stringp fortran-comment-indent-char)
1656 fortran-comment-indent-char
1657 (char-to-string fortran-comment-indent-char))))
1658 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1659 (goto-char (match-end 0)))
1660 (t
1661 ;; Move past line number.
1662 (skip-chars-forward "[ \t0-9]")))
1663 ;; Move past whitespace.
1664 (skip-chars-forward " \t")
1665 (current-column)))
1666
1667 (defun fortran-indent-to-column (col)
1668 "Indent current line to column COL.
1669 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1670 line, and this continuation character is retained on indentation;
1671 2) If `fortran-continuation-string' is the first non-whitespace
1672 character, this is a continuation line;
1673 3) A non-continuation line which has a number as the first
1674 non-whitespace character is a numbered line.
1675 4) A TAB followed by a digit indicates a continuation line."
1676 (save-excursion
1677 (beginning-of-line)
1678 (if (looking-at fortran-comment-line-start-skip)
1679 (if fortran-comment-indent-style
1680 (let* ((char (if (stringp fortran-comment-indent-char)
1681 (aref fortran-comment-indent-char 0)
1682 fortran-comment-indent-char))
1683 (chars (string ?\s ?\t char)))
1684 (goto-char (match-end 0))
1685 (skip-chars-backward chars)
1686 (delete-region (point) (progn (skip-chars-forward chars)
1687 (point)))
1688 (insert-char char (- col (current-column)))))
1689 (if (looking-at "\t[1-9]")
1690 (if indent-tabs-mode
1691 (goto-char (match-end 0))
1692 (delete-char 2)
1693 (insert-char ?\s 5)
1694 (insert fortran-continuation-string))
1695 (if (looking-at " \\{5\\}[^ 0\n]")
1696 (if indent-tabs-mode
1697 (progn (delete-char 6)
1698 (insert ?\t (fortran-numerical-continuation-char) 1))
1699 (forward-char 6))
1700 (delete-horizontal-space)
1701 ;; Put line number in columns 0-4, or
1702 ;; continuation character in column 5.
1703 (cond ((eobp))
1704 ((looking-at (regexp-quote fortran-continuation-string))
1705 (if indent-tabs-mode
1706 (progn
1707 (indent-to
1708 (if indent-tabs-mode
1709 fortran-minimum-statement-indent-tab
1710 fortran-minimum-statement-indent-fixed))
1711 (delete-char 1)
1712 (insert-char (fortran-numerical-continuation-char) 1))
1713 (indent-to 5)
1714 (forward-char 1)))
1715 ((looking-at "[0-9]+")
1716 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1717 (if (< extra-space 0)
1718 (message "Warning: line number exceeds 5-digit limit.")
1719 (indent-to (min fortran-line-number-indent extra-space))))
1720 (skip-chars-forward "0-9")))))
1721 ;; Point is now after any continuation character or line number.
1722 ;; Put body of statement where specified.
1723 (delete-horizontal-space)
1724 (indent-to col)
1725 ;; Indent any comment following code on the same line.
1726 (when (fortran-find-comment-start-skip)
1727 (goto-char (match-beginning 0))
1728 (unless (= (current-column) (fortran-comment-indent))
1729 (delete-horizontal-space)
1730 (indent-to (fortran-comment-indent)))))))
1731
1732 (defun fortran-line-number-indented-correctly-p ()
1733 "Return t if current line's line number is correctly indented.
1734 Do not call if there is no line number."
1735 (save-excursion
1736 (beginning-of-line)
1737 (skip-chars-forward " \t")
1738 (and (<= (current-column) fortran-line-number-indent)
1739 (or (= (current-column) fortran-line-number-indent)
1740 (progn (skip-chars-forward "0-9")
1741 (= (current-column) 5))))))
1742
1743 (defun fortran-check-for-matching-do ()
1744 "When called from a numbered statement, return t if matching DO is found.
1745 Otherwise return nil."
1746 (let ((case-fold-search t)
1747 charnum)
1748 (save-excursion
1749 (beginning-of-line)
1750 (when (looking-at "[ \t]*[0-9]+")
1751 (skip-chars-forward " \t")
1752 (skip-chars-forward "0") ; skip past leading zeros
1753 (setq charnum
1754 (buffer-substring (point) (progn
1755 (skip-chars-forward "0-9")
1756 (point))))
1757 (beginning-of-line)
1758 (save-restriction
1759 (save-excursion
1760 (narrow-to-defun)
1761 (and (re-search-backward
1762 (concat
1763 "\\(^[ \t0-9]*do[ \t]*0*"
1764 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1765 charnum "\\b\\)")
1766 nil t)
1767 (looking-at
1768 (concat "^[ \t0-9]*do[ \t]*0*"
1769 charnum)))))))))
1770
1771 (defun fortran-find-comment-start-skip (&optional all)
1772 "Move to past `comment-start-skip' found on current line.
1773 Return non-nil if `comment-start-skip' found, nil if not.
1774 If ALL is nil, only match comments that start in column > 0."
1775 ;; Hopefully at some point we can just use the line below! -stef
1776 ;; (comment-search-forward (line-end-position) t))
1777 (when (or all comment-start-skip)
1778 (let ((pos (point))
1779 (css (if comment-start-skip
1780 (concat fortran-comment-line-start-skip
1781 "\\|" comment-start-skip)
1782 fortran-comment-line-start-skip)))
1783 (when (re-search-forward css (line-end-position) t)
1784 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1785 (or (save-match-data
1786 (not (fortran-is-in-string-p (match-beginning 0))))
1787 ;; Recurse for rest of line.
1788 (fortran-find-comment-start-skip all)))
1789 (point)
1790 (goto-char pos)
1791 nil)))))
1792
1793 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1794 ;; Test if TAB format continuation lines work.
1795 (defun fortran-is-in-string-p (where)
1796 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
1797 (save-excursion
1798 (goto-char where)
1799 (cond
1800 ((bolp) nil) ; bol is never inside a string
1801 ((save-excursion ; comment lines too
1802 (beginning-of-line)
1803 (looking-at fortran-comment-line-start-skip)) nil)
1804 (t (let ((parse-state '(0 nil nil nil nil nil 0))
1805 (quoted-comment-start (if comment-start
1806 (regexp-quote comment-start)))
1807 (not-done t)
1808 parse-limit end-of-line)
1809 ;; Move to start of current statement.
1810 (fortran-next-statement)
1811 (fortran-previous-statement)
1812 ;; Now parse up to WHERE.
1813 (while not-done
1814 (if (or ;; Skip to next line if:
1815 ;; - comment line?
1816 (looking-at fortran-comment-line-start-skip)
1817 ;; - at end of line?
1818 (eolp)
1819 ;; - not in a string and after comment-start?
1820 (and (not (nth 3 parse-state))
1821 comment-start
1822 (equal comment-start
1823 (char-to-string (preceding-char)))))
1824 (if (> (forward-line) 0)
1825 (setq not-done nil))
1826 ;; else:
1827 ;; If we are at beginning of code line, skip any
1828 ;; whitespace, labels and tab continuation markers.
1829 (if (bolp) (skip-chars-forward " \t0-9"))
1830 ;; If we are in column <= 5 now, check for continuation char.
1831 (cond ((= 5 (current-column)) (forward-char 1))
1832 ((and (< (current-column) 5)
1833 (equal fortran-continuation-string
1834 (char-to-string (following-char)))
1835 (forward-char 1))))
1836 ;; Find out parse-limit from here.
1837 (setq end-of-line (line-end-position))
1838 (setq parse-limit (min where end-of-line))
1839 ;; Parse max up to comment-start, if non-nil and in current line.
1840 (if comment-start
1841 (save-excursion
1842 (if (re-search-forward quoted-comment-start end-of-line t)
1843 (setq parse-limit (min (point) parse-limit)))))
1844 ;; Now parse if still in limits.
1845 (if (< (point) where)
1846 (setq parse-state (parse-partial-sexp
1847 (point) parse-limit nil nil parse-state))
1848 (setq not-done nil))))
1849 ;; Result.
1850 (nth 3 parse-state))))))
1851
1852 ;; From old version.
1853 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1854
1855 (defun fortran-fill ()
1856 "Fill the current line at an appropriate point(s)."
1857 (let* ((auto-fill-function #'fortran-auto-fill)
1858 (opoint (point))
1859 (bol (line-beginning-position))
1860 (eol (line-end-position))
1861 (bos (min eol (+ bol (fortran-current-line-indentation))))
1862 ;; If in a string at fill-column, break it either before the
1863 ;; initial quote, or at fill-col (if string is too long).
1864 (quote
1865 (save-excursion
1866 (goto-char bol)
1867 ;; OK to break quotes on comment lines.
1868 (unless (looking-at fortran-comment-line-start-skip)
1869 (let (fcpoint start)
1870 (move-to-column fill-column)
1871 (when (fortran-is-in-string-p (setq fcpoint (point)))
1872 (save-excursion
1873 (re-search-backward "\\S\"\\s\"\\S\"?" bol t)
1874 (setq start
1875 (if fortran-break-before-delimiters
1876 (point)
1877 (1+ (point)))))
1878 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t)
1879 (backward-char 2))
1880 ;; If the current string is longer than (fill-column
1881 ;; - 6) chars, break it at the fill column (else
1882 ;; infinite loop).
1883 (if (> (- (point) start)
1884 (- fill-column 6 fortran-continuation-indent))
1885 fcpoint
1886 start))))))
1887 ;; Decide where to split the line. If a position for a quoted
1888 ;; string was found above then use that, else break the line
1889 ;; before/after the last delimiter.
1890 (fill-point
1891 (or quote
1892 (save-excursion
1893 ;; If f-b-b-d is t, have an extra column to play with,
1894 ;; since delimiter gets shifted to new line.
1895 (move-to-column (if fortran-break-before-delimiters
1896 (1+ fill-column)
1897 fill-column))
1898 (let ((repeat t))
1899 (while repeat
1900 (setq repeat nil)
1901 ;; Adapted from f90-find-breakpoint.
1902 (re-search-backward fortran-break-delimiters-re bol)
1903 (if (not fortran-break-before-delimiters)
1904 (if (looking-at fortran-no-break-re)
1905 ;; Deal with cases such as "**" split over
1906 ;; fill-col. Simpler alternative would be
1907 ;; to start from (1- fill-column) above.
1908 (if (> (+ 2 (current-column)) fill-column)
1909 (setq repeat t)
1910 (forward-char 2))
1911 (forward-char 1))
1912 (backward-char)
1913 (or (looking-at fortran-no-break-re)
1914 (forward-char)))))
1915 ;; Line indented beyond fill-column?
1916 (when (<= (point) bos)
1917 (move-to-column (1+ fill-column))
1918 ;; What is this doing???
1919 (or (re-search-forward "[\t\n,'+-/*)=]" eol t)
1920 (goto-char bol)))
1921 (if (bolp)
1922 (re-search-forward "[ \t]" opoint t))
1923 (point)))))
1924 ;; If we are in an in-line comment, don't break unless the
1925 ;; line of code is longer than it should be. Otherwise
1926 ;; break the line at the column computed above.
1927 ;;
1928 ;; Need to use fortran-find-comment-start-skip to make sure that
1929 ;; quoted !'s don't prevent a break.
1930 (when (and (save-excursion
1931 (beginning-of-line)
1932 (if (not (fortran-find-comment-start-skip))
1933 t
1934 (goto-char (match-beginning 0))
1935 (>= (point) fill-point)))
1936 (save-excursion
1937 (goto-char fill-point)
1938 (not (bolp)))
1939 (> (save-excursion
1940 (goto-char opoint)
1941 (current-column))
1942 (min (1+ fill-column)
1943 (+ (fortran-calculate-indent)
1944 fortran-continuation-indent))))
1945 (goto-char fill-point)
1946 (fortran-break-line)
1947 (end-of-line))))
1948
1949 (defun fortran-break-line ()
1950 "Call `fortran-split-line'. Joins continuation lines first, then refills."
1951 (let ((bol (line-beginning-position))
1952 (comment-string
1953 (save-excursion
1954 (if (fortran-find-comment-start-skip)
1955 (delete-and-extract-region
1956 (match-beginning 0) (line-end-position))))))
1957 ;; Forward line 1 really needs to go to next non white line.
1958 (if (save-excursion (forward-line)
1959 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1960 (progn
1961 (end-of-line)
1962 (delete-region (point) (match-end 0))
1963 (delete-horizontal-space)
1964 (fortran-fill))
1965 (fortran-split-line))
1966 (if comment-string
1967 (save-excursion
1968 (goto-char bol)
1969 (end-of-line)
1970 (delete-horizontal-space)
1971 (indent-to (fortran-comment-indent))
1972 (insert comment-string)))))
1973
1974 (defun fortran-analyze-file-format ()
1975 "Return nil if fixed format is used, t if TAB formatting is used.
1976 Use `fortran-tab-mode-default' if no non-comment statements are found
1977 before the end or in the first `fortran-analyze-depth' lines."
1978 (let ((i 0))
1979 (save-excursion
1980 (goto-char (point-min))
1981 (while (not (or
1982 (eobp)
1983 (eq (char-after) ?\t)
1984 (looking-at " \\{6\\}")
1985 (> i fortran-analyze-depth)))
1986 (forward-line)
1987 (setq i (1+ i)))
1988 (cond
1989 ((eq (char-after) ?\t) t)
1990 ((looking-at " \\{6\\}") nil)
1991 (t fortran-tab-mode-default)))))
1992
1993 (defun fortran-fill-paragraph (&optional justify)
1994 "Fill surrounding comment block as paragraphs, else fill statement.
1995 Intended as the value of `fill-paragraph-function'.
1996 A comment block is filled by calling `fill-comment-paragraph' with
1997 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
1998 Always returns non-nil (to prevent `fill-paragraph' being called)."
1999 (interactive "*P")
2000 (or (fill-comment-paragraph justify)
2001 (fortran-fill-statement)
2002 t))
2003
2004 (defun fortran-fill-statement ()
2005 "Fill a Fortran statement up to `fill-column'."
2006 (interactive "*")
2007 (let ((auto-fill-function #'fortran-auto-fill))
2008 (unless (save-excursion
2009 (beginning-of-line)
2010 (or (looking-at "[ \t]*$")
2011 (looking-at fortran-comment-line-start-skip)
2012 (and comment-start-skip
2013 (looking-at (concat "[ \t]*" comment-start-skip)))))
2014 (save-excursion
2015 ;; Find beginning of statement.
2016 (fortran-next-statement)
2017 (fortran-previous-statement)
2018 ;; Re-indent initially.
2019 (fortran-indent-line)
2020 ;; Replace newline plus continuation field plus indentation with
2021 ;; single space.
2022 (while (progn
2023 (forward-line)
2024 (fortran-remove-continuation)))
2025 (fortran-previous-statement)))
2026 (fortran-indent-line)))
2027
2028 (defun fortran-strip-sequence-nos (&optional do-space)
2029 "Delete all text in column 72 and up (assumed to be sequence numbers).
2030 Normally also deletes trailing whitespace after stripping such text.
2031 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
2032 (interactive "*p")
2033 (save-excursion
2034 (goto-char (point-min))
2035 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t)
2036 (replace-match "" nil nil nil 1)
2037 (unless do-space (delete-horizontal-space)))))
2038
2039 ;; This code used to live in add-log.el, but this is a better place
2040 ;; for it.
2041 (defun fortran-current-defun ()
2042 "Function to use for `add-log-current-defun-function' in Fortran mode."
2043 (save-excursion
2044 ;; We must be inside function body for this to work.
2045 (fortran-beginning-of-subprogram)
2046 (let ((case-fold-search t)) ; case-insensitive
2047 ;; Search for fortran subprogram start.
2048 (if (re-search-forward
2049 (concat "^[ \t]*\\(program\\|subroutine\\|function"
2050 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
2051 "\\(block[ \t]*data\\)\\)")
2052 (save-excursion (fortran-end-of-subprogram)
2053 (point))
2054 t)
2055 (or (match-string-no-properties 2)
2056 (progn
2057 ;; Move to EOL or before first left paren.
2058 (if (re-search-forward "[(\n]" nil t)
2059 (progn (backward-char)
2060 (skip-chars-backward " \t"))
2061 (end-of-line))
2062 ;; Use the name preceding that.
2063 (buffer-substring-no-properties (point) (progn (backward-sexp)
2064 (point)))))
2065 "main"))))
2066
2067 (provide 'fortran)
2068
2069 ;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
2070 ;;; fortran.el ends here