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