]> code.delx.au - gnu-emacs/blob - lisp/progmodes/fortran.el
Merge from emacs--rel--22
[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 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; This mode is documented in the Emacs manual.
29 ;;
30 ;; Note that it is for editing Fortran77 or Fortran90 fixed source
31 ;; form. For editing Fortran 90 free format source, use `f90-mode'
32 ;; (f90.el). It is meant to support the GNU Fortran language
33 ;; implemented by g77 (its extensions to Fortran77 and
34 ;; interpretations, e.g. of blackslash in strings).
35
36 ;;; History:
37
38 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
39
40 ;; We acknowledge many contributions and valuable suggestions by
41 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
42 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
43 ;; Gary Sabot and Richard Stallman.
44
45 ;;; Code:
46
47 ;; Todo:
48
49 ;; * Tidy it all up (more)!
50 ;; * Implement insertion and removal of statement continuations in
51 ;; mixed f77/f90 style, with the first `&' past column 72 and the
52 ;; second in column 6.
53 ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
54
55 ;; silence compiler
56 (defvar dabbrev-case-fold-search)
57 (defvar gud-find-expr-function)
58 (defvar imenu-case-fold-search)
59 (defvar imenu-syntax-alist)
60 (defvar comment-region-function)
61 (defvar uncomment-region-function)
62
63 (defgroup fortran nil
64 "Major mode for editing fixed format Fortran code."
65 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
66 :link '(custom-manual "(emacs)Fortran")
67 :group 'languages)
68
69 (defgroup fortran-indent nil
70 "Indentation variables in Fortran mode."
71 :prefix "fortran-"
72 :group 'fortran)
73
74 (defgroup fortran-comment nil
75 "Comment-handling variables in Fortran mode."
76 :prefix "fortran-"
77 :group 'fortran)
78
79
80 (defcustom fortran-tab-mode-default nil
81 "Default tabbing/carriage control style for empty files in Fortran mode.
82 A non-nil value specifies tab-digit style of continuation control.
83 A value of nil specifies that continuation lines are marked
84 with a character in column 6."
85 :type 'boolean
86 :group 'fortran-indent)
87 (put 'fortran-tab-mode-default 'safe-local-variable 'booleanp)
88
89 ;; TODO add more detail of what tab mode is to doc string.
90 (defcustom fortran-tab-mode-string
91 (propertize "/t" 'help-echo "This buffer is in Fortran TAB mode"
92 'mouse-face 'mode-line-highlight
93 'local-map
94 (make-mode-line-mouse-map 'mouse-1
95 (lambda ()
96 (interactive)
97 (describe-variable
98 'fortran-tab-mode-string))))
99 "String to appear in mode line in TAB format buffers.
100 See Info node `(emacs)ForIndent Cont'."
101 :type 'string
102 :group 'fortran-indent)
103 (put 'fortran-tab-mode-string 'risky-local-variable t)
104
105 (defcustom fortran-do-indent 3
106 "Extra indentation applied to DO blocks."
107 :type 'integer
108 :group 'fortran-indent)
109 (put 'fortran-do-indent 'safe-local-variable 'integerp)
110
111 (defcustom fortran-if-indent 3
112 "Extra indentation applied to IF, SELECT CASE and WHERE blocks."
113 :type 'integer
114 :group 'fortran-indent)
115 (put 'fortran-if-indent 'safe-local-variable 'integerp)
116
117 (defcustom fortran-structure-indent 3
118 "Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
119 :type 'integer
120 :group 'fortran-indent)
121 (put 'fortran-structure-indent 'safe-local-variable 'integerp)
122
123 (defcustom fortran-continuation-indent 5
124 "Extra indentation applied to continuation lines."
125 :type 'integer
126 :group 'fortran-indent)
127 (put 'fortran-continuation-indent 'safe-local-variable 'integerp)
128
129 (defcustom fortran-comment-indent-style 'fixed
130 "How to indent comments.
131 nil forces comment lines not to be touched;
132 `fixed' indents to `fortran-comment-line-extra-indent' columns beyond
133 `fortran-minimum-statement-indent-fixed' (if `indent-tabs-mode' nil), or
134 `fortran-minimum-statement-indent-tab' (if `indent-tabs-mode' non-nil);
135 `relative' indents to current Fortran indentation plus
136 `fortran-comment-line-extra-indent'."
137 :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative))
138 :group 'fortran-indent)
139 (put 'fortran-comment-indent 'safe-local-variable
140 (lambda (value) (memq value '(nil fixed relative))))
141
142 (defcustom fortran-comment-line-extra-indent 0
143 "Amount of extra indentation for text within full-line comments."
144 :type 'integer
145 :group 'fortran-indent
146 :group 'fortran-comment)
147 (put 'fortran-comment-line-extra-indent 'safe-local-variable 'integerp)
148
149 (defcustom fortran-comment-line-start "C"
150 "Delimiter inserted to start new full-line comment.
151 You might want to change this to \"*\", for instance; or \"!\" to
152 allow trailing comments on a line."
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" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
395 "neqv" "not" "or" "true")
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 ;; Was a word-constituent (for abbrevs), now punctuation (g77
564 ;; multi-statement lines).
565 (modify-syntax-entry ?\; "." 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 (define-abbrev-table 'fortran-mode-abbrev-table
674 (mapcar (lambda (e) (list (car e) (cdr e) nil :system t))
675 '((";au" . "automatic" )
676 (";b" . "byte" )
677 (";bd" . "block data" )
678 (";ch" . "character" )
679 (";cl" . "close" )
680 (";c" . "continue" )
681 (";cm" . "common" )
682 (";cx" . "complex" )
683 (";df" . "define" )
684 (";di" . "dimension" )
685 (";do" . "double" )
686 (";dc" . "double complex" )
687 (";dp" . "double precision" )
688 (";dw" . "do while" )
689 (";e" . "else" )
690 (";ed" . "enddo" )
691 (";el" . "elseif" )
692 (";en" . "endif" )
693 (";eq" . "equivalence" )
694 (";ew" . "endwhere" )
695 (";ex" . "external" )
696 (";ey" . "entry" )
697 (";f" . "format" )
698 (";fa" . ".false." )
699 (";fu" . "function" )
700 (";g" . "goto" )
701 (";im" . "implicit" )
702 (";ib" . "implicit byte" )
703 (";ic" . "implicit complex" )
704 (";ich" . "implicit character")
705 (";ii" . "implicit integer" )
706 (";il" . "implicit logical" )
707 (";ir" . "implicit real" )
708 (";inc" . "include" )
709 (";in" . "integer" )
710 (";intr" . "intrinsic" )
711 (";l" . "logical" )
712 (";n" . "namelist" )
713 (";o" . "open" ) ; was ;op
714 (";pa" . "parameter" )
715 (";pr" . "program" )
716 (";ps" . "pause" )
717 (";p" . "print" )
718 (";rc" . "record" )
719 (";re" . "real" )
720 (";r" . "read" )
721 (";rt" . "return" )
722 (";rw" . "rewind" )
723 (";s" . "stop" )
724 (";sa" . "save" )
725 (";st" . "structure" )
726 (";sc" . "static" )
727 (";su" . "subroutine" )
728 (";tr" . ".true." )
729 (";ty" . "type" )
730 (";vo" . "volatile" )
731 (";w" . "write" )
732 (";wh" . "where" )))
733 "Abbrev table for Fortran mode."
734 ;; Accept ; as the first char of an abbrev. Also allow _ in abbrevs.
735 :regexp "\\(?:[^[:word:]_;]\\|^\\)\\(;?[[:word:]_]+\\)[^[:word:]_]*")
736
737 \f
738 ;;;###autoload
739 (defun fortran-mode ()
740 "Major mode for editing Fortran code in fixed format.
741 For free format code, use `f90-mode'.
742
743 \\[fortran-indent-line] indents the current Fortran line correctly.
744 Note that DO statements must not share a common CONTINUE.
745
746 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
747 Fortran keywords.
748
749 Key definitions:
750 \\{fortran-mode-map}
751
752 Variables controlling indentation style and extra features:
753
754 `fortran-comment-line-start'
755 To use comments starting with `!', set this to the string \"!\".
756 `fortran-do-indent'
757 Extra indentation within DO blocks (default 3).
758 `fortran-if-indent'
759 Extra indentation within IF blocks (default 3).
760 `fortran-structure-indent'
761 Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
762 (default 3)
763 `fortran-continuation-indent'
764 Extra indentation applied to continuation statements (default 5).
765 `fortran-comment-line-extra-indent'
766 Amount of extra indentation for text in full-line comments (default 0).
767 `fortran-comment-indent-style'
768 How to indent the text in full-line comments. Allowed values are:
769 nil don't change the indentation
770 fixed indent to `fortran-comment-line-extra-indent' beyond the
771 value of either
772 `fortran-minimum-statement-indent-fixed' (fixed format) or
773 `fortran-minimum-statement-indent-tab' (TAB format),
774 depending on the continuation format in use.
775 relative indent to `fortran-comment-line-extra-indent' beyond the
776 indentation for a line of code.
777 (default 'fixed)
778 `fortran-comment-indent-char'
779 Single-character string to be inserted instead of space for
780 full-line comment indentation (default \" \").
781 `fortran-minimum-statement-indent-fixed'
782 Minimum indentation for statements in fixed format mode (default 6).
783 `fortran-minimum-statement-indent-tab'
784 Minimum indentation for statements in TAB format mode (default 9).
785 `fortran-line-number-indent'
786 Maximum indentation for line numbers (default 1). A line number will
787 get less than this much indentation if necessary to avoid reaching
788 column 5.
789 `fortran-check-all-num-for-matching-do'
790 Non-nil causes all numbered lines to be treated as possible \"continue\"
791 statements (default nil).
792 `fortran-blink-matching-if'
793 Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
794 to blink on the matching IF (or DO [WHILE]). (default nil)
795 `fortran-continuation-string'
796 Single-character string to be inserted in column 5 of a continuation
797 line (default \"$\").
798 `fortran-comment-region'
799 String inserted by \\[fortran-comment-region] at start of each line in
800 the region (default \"c$$$\").
801 `fortran-electric-line-number'
802 Non-nil causes line number digits to be moved to the correct column
803 as typed (default t).
804 `fortran-break-before-delimiters'
805 Non-nil causes lines to be broken before delimiters (default t).
806
807 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
808 with no args, if that value is non-nil."
809 (interactive)
810 (kill-all-local-variables)
811 (setq major-mode 'fortran-mode
812 mode-name "Fortran"
813 local-abbrev-table fortran-mode-abbrev-table)
814 (set-syntax-table fortran-mode-syntax-table)
815 (use-local-map fortran-mode-map)
816 (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
817 (set (make-local-variable 'indent-region-function)
818 (lambda (start end)
819 (let (fortran-blink-matching-if ; avoid blinking delay
820 indent-region-function)
821 (indent-region start end nil))))
822 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
823 ;; The syntax tables don't understand the column-0 comment-markers.
824 (set (make-local-variable 'comment-use-syntax) nil)
825 (set (make-local-variable 'comment-padding) "$$$")
826 (set (make-local-variable 'comment-start) fortran-comment-line-start)
827 (set (make-local-variable 'comment-start-skip)
828 ;; We can't reuse `fortran-comment-line-start-skip' directly because
829 ;; it contains backrefs whereas we need submatch-1 to end at the
830 ;; beginning of the comment delimiter.
831 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
832 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
833 (set (make-local-variable 'comment-indent-function) 'fortran-comment-indent)
834 (set (make-local-variable 'comment-region-function) 'fortran-comment-region)
835 (set (make-local-variable 'uncomment-region-function)
836 'fortran-uncomment-region)
837 (set (make-local-variable 'comment-insert-comment-function)
838 'fortran-indent-comment)
839 (set (make-local-variable 'abbrev-all-caps) t)
840 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
841 (set (make-local-variable 'indent-tabs-mode) (fortran-analyze-file-format))
842 (setq mode-line-process '(indent-tabs-mode fortran-tab-mode-string))
843 (set (make-local-variable 'fill-column) fortran-line-length)
844 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
845 (set (make-local-variable 'font-lock-defaults)
846 '((fortran-font-lock-keywords
847 fortran-font-lock-keywords-1
848 fortran-font-lock-keywords-2
849 fortran-font-lock-keywords-3
850 fortran-font-lock-keywords-4)
851 nil t ((?/ . "$/") ("_$" . "w"))
852 fortran-beginning-of-subprogram
853 (font-lock-syntactic-keywords
854 . fortran-font-lock-syntactic-keywords)))
855 (set (make-local-variable 'imenu-case-fold-search) t)
856 (set (make-local-variable 'imenu-generic-expression)
857 fortran-imenu-generic-expression)
858 (set (make-local-variable 'imenu-syntax-alist) '(("_$" . "w")))
859 (set (make-local-variable 'beginning-of-defun-function)
860 #'fortran-beginning-of-subprogram)
861 (set (make-local-variable 'end-of-defun-function)
862 #'fortran-end-of-subprogram)
863 (set (make-local-variable 'add-log-current-defun-function)
864 #'fortran-current-defun)
865 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
866 (set (make-local-variable 'gud-find-expr-function) 'fortran-gud-find-expr)
867 (add-hook 'hack-local-variables-hook 'fortran-hack-local-variables nil t)
868 (run-mode-hooks 'fortran-mode-hook))
869
870 \f
871 (defun fortran-line-length (nchars &optional global)
872 "Set the length of fixed-form Fortran lines to NCHARS.
873 This normally only affects the current buffer, which must be in
874 Fortran mode. If the optional argument GLOBAL is non-nil, it
875 affects all Fortran buffers, and also the default."
876 (interactive "p")
877 (let (new)
878 (mapc (lambda (buff)
879 (with-current-buffer buff
880 (when (eq major-mode 'fortran-mode)
881 (setq fortran-line-length nchars
882 fill-column fortran-line-length
883 new (fortran-font-lock-syntactic-keywords))
884 ;; Refontify only if necessary.
885 (unless (equal new font-lock-syntactic-keywords)
886 (setq font-lock-syntactic-keywords
887 (fortran-font-lock-syntactic-keywords))
888 (if font-lock-mode (font-lock-mode 1))))))
889 (if global
890 (buffer-list)
891 (list (current-buffer))))
892 (if global
893 (setq-default fortran-line-length nchars))))
894
895 (defun fortran-hack-local-variables ()
896 "Fortran mode adds this to `hack-local-variables-hook'."
897 (fortran-line-length fortran-line-length))
898
899 (declare-function gud-find-c-expr "gud.el" nil)
900
901 (defun fortran-gud-find-expr ()
902 ;; Consider \n as punctuation (end of expression).
903 (with-syntax-table fortran-gud-syntax-table
904 (gud-find-c-expr)))
905
906 (defsubst fortran-comment-indent ()
907 "Return the indentation appropriate for the current comment line.
908 This is 0 for a line matching `fortran-comment-line-start-skip', else
909 the value of `comment-column' (leaving at least one space after code)."
910 (if (looking-at fortran-comment-line-start-skip) 0
911 (save-excursion
912 (skip-chars-backward " \t")
913 (max (1+ (current-column)) comment-column))))
914
915 (defun fortran-indent-comment ()
916 "Align or create comment on current line.
917 Existing comments of all types are recognized and aligned.
918 If the line has no comment, a side-by-side comment is inserted and aligned,
919 if the value of `comment-start' is not nil and allows such comments.
920 Otherwise, a separate-line comment is inserted, on this line
921 or on a new line inserted before this line if this line is not blank."
922 (interactive "*")
923 (beginning-of-line)
924 ;; Recognize existing comments of either kind.
925 (cond ((fortran-find-comment-start-skip 'all)
926 (goto-char (match-beginning 0))
927 (if (bolp)
928 (fortran-indent-line)
929 (unless (= (current-column) (fortran-comment-indent))
930 (delete-horizontal-space)
931 (indent-to (fortran-comment-indent)))))
932 ;; No existing comment.
933 ;; If side-by-side comments are defined, insert one,
934 ;; unless line is now blank.
935 ((and comment-start (not (looking-at "[ \t]*$"))
936 (string-match comment-start-skip (concat " " comment-start)))
937 (end-of-line)
938 (delete-horizontal-space)
939 (indent-to (fortran-comment-indent))
940 (insert comment-start))
941 ;; Else insert separate-line comment, making a new line if nec.
942 (t
943 (if (looking-at "^[ \t]*$")
944 (delete-horizontal-space)
945 (beginning-of-line)
946 (insert ?\n)
947 (forward-char -1))
948 (insert fortran-comment-line-start)
949 (insert-char (if (stringp fortran-comment-indent-char)
950 (aref fortran-comment-indent-char 0)
951 fortran-comment-indent-char)
952 (- (fortran-calculate-indent) (current-column))))))
953
954 (defun fortran-comment-region (beg-region end-region arg)
955 "Comment every line in the region.
956 Inserts the string variable `fortran-comment-region' at the beginning of
957 every line in the region.
958 BEG-REGION and END-REGION specify the region boundaries.
959 With non-nil ARG, uncomments the region."
960 (interactive "*r\nP")
961 (let ((end-region-mark (copy-marker end-region))
962 (save-point (point-marker)))
963 (goto-char beg-region)
964 (beginning-of-line)
965 (if arg
966 (let ((com (regexp-quote fortran-comment-region))) ; uncomment
967 (if (looking-at com)
968 (delete-region (point) (match-end 0)))
969 (while (and (zerop (forward-line 1))
970 (< (point) end-region-mark))
971 (if (looking-at com)
972 (delete-region (point) (match-end 0)))))
973 (insert fortran-comment-region) ; comment
974 (while (and (zerop (forward-line 1))
975 (< (point) end-region-mark))
976 (insert fortran-comment-region)))
977 (goto-char save-point)
978 (set-marker end-region-mark nil)
979 (set-marker save-point nil)))
980
981 ;; uncomment-region calls this with 3 args.
982 (defun fortran-uncomment-region (start end &optional ignored)
983 "Uncomment every line in the region."
984 (fortran-comment-region start end t))
985
986 \f
987 (defun fortran-abbrev-start ()
988 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
989 Any other key combination is executed normally."
990 (interactive "*")
991 (insert last-command-char)
992 (let* ((event (if (fboundp 'next-command-event) ; XEmacs
993 (next-command-event)
994 (read-event)))
995 (char (if (fboundp 'event-to-character)
996 (event-to-character event) event)))
997 ;; Insert char if not equal to `?', or if abbrev-mode is off.
998 (if (and abbrev-mode (or (eq char ??) (eq char help-char)
999 (memq event help-event-list)))
1000 (fortran-abbrev-help)
1001 (push event unread-command-events))))
1002
1003 (defun fortran-abbrev-help ()
1004 "List the currently defined abbrevs in Fortran mode."
1005 (interactive)
1006 (message "Listing abbrev table...")
1007 (display-buffer (fortran-prepare-abbrev-list-buffer))
1008 (message "Listing abbrev table...done"))
1009
1010 (defun fortran-prepare-abbrev-list-buffer ()
1011 "Create a buffer listing the Fortran mode abbreviations."
1012 (with-current-buffer (get-buffer-create "*Abbrevs*")
1013 (erase-buffer)
1014 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
1015 (goto-char (point-min))
1016 (set-buffer-modified-p nil)
1017 (edit-abbrevs-mode))
1018 (get-buffer-create "*Abbrevs*"))
1019
1020 (defun fortran-column-ruler ()
1021 "Insert a column ruler momentarily above current line, till next keystroke.
1022 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
1023 format mode, and `fortran-column-ruler-tab' in TAB format mode.
1024 The next key typed is executed unless it is SPC."
1025 (interactive)
1026 (momentary-string-display
1027 (if indent-tabs-mode
1028 fortran-column-ruler-tab
1029 fortran-column-ruler-fixed)
1030 (save-excursion
1031 (beginning-of-line)
1032 (if (eq (window-start (selected-window))
1033 (window-point (selected-window)))
1034 (line-beginning-position 2)
1035 (point)))
1036 nil "Type SPC or any command to erase ruler."))
1037
1038 (defun fortran-window-create ()
1039 "Make the window `fortran-line-length' (default 72) columns wide.
1040 See also `fortran-window-create-momentarily'."
1041 (interactive)
1042 (let ((window-min-width 2))
1043 (unless (window-full-width-p)
1044 (enlarge-window-horizontally (- (frame-width)
1045 (window-width) 1)))
1046 (let* ((window-edges (window-edges))
1047 (scroll-bar-width (- (nth 2 window-edges)
1048 (car window-edges)
1049 (window-width))))
1050 (split-window-horizontally (+ fortran-line-length scroll-bar-width)))
1051 (other-window 1)
1052 (switch-to-buffer " fortran-window-extra" t)
1053 (select-window (previous-window))))
1054
1055 (defun fortran-window-create-momentarily (&optional arg)
1056 "Momentarily make the window `fortran-line-length' (default 72) columns wide.
1057 Optional ARG non-nil and non-unity disables the momentary feature.
1058 See also `fortran-window-create'."
1059 (interactive "p")
1060 (if (or (not arg)
1061 (= arg 1))
1062 (save-window-excursion
1063 (progn
1064 (condition-case nil
1065 (fortran-window-create)
1066 (error (error "No room for Fortran window")))
1067 (message "Type SPC to continue editing.")
1068 (let ((char (read-event)))
1069 (or (equal char ?\s)
1070 (setq unread-command-events (list char))))))
1071 (fortran-window-create)))
1072
1073 (defun fortran-split-line ()
1074 "Break line at point and insert continuation marker and alignment."
1075 (interactive "*")
1076 (delete-horizontal-space)
1077 (if (save-excursion
1078 (let ((pos (point)))
1079 (beginning-of-line)
1080 (and (fortran-find-comment-start-skip 'all)
1081 (< (match-beginning 0) pos))))
1082 (insert ?\n (match-string 0))
1083 (if indent-tabs-mode
1084 (insert ?\n ?\t (fortran-numerical-continuation-char))
1085 (insert "\n " fortran-continuation-string))) ; space after \n important
1086 (fortran-indent-line)) ; when cont string is C, c or *
1087
1088 (defun fortran-remove-continuation ()
1089 "Delete any Fortran continuation characters at point.
1090 Returns t if anything actually deleted."
1091 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1092 (replace-match "")
1093 (delete-indentation)
1094 t))
1095
1096 (defun fortran-join-line (arg)
1097 "Join current line to the previous one and re-indent.
1098 With a prefix argument, repeat this operation that many times.
1099 If the prefix argument ARG is negative, join the next -ARG lines.
1100 Continuation lines are correctly handled."
1101 (interactive "*p")
1102 (save-excursion
1103 (when (> 0 arg)
1104 (setq arg (- arg))
1105 (forward-line arg))
1106 (while (not (zerop arg))
1107 (beginning-of-line)
1108 (or (fortran-remove-continuation)
1109 (delete-indentation))
1110 (setq arg (1- arg)))
1111 (fortran-indent-line)))
1112
1113 (defun fortran-numerical-continuation-char ()
1114 "Return a digit for tab-digit style of continuation lines.
1115 If previous line is a tab-digit continuation line, return that digit
1116 plus one, otherwise return 1. Zero not allowed."
1117 (save-excursion
1118 (forward-line -1)
1119 (if (looking-at "\t[1-9]")
1120 (+ ?1 (% (- (char-after (1+ (point))) ?0) 9))
1121 ?1)))
1122
1123 (put 'fortran-electric-line-number 'delete-selection t)
1124 (defun fortran-electric-line-number (arg)
1125 "Self insert, but if part of a Fortran line number indent it automatically.
1126 Auto-indent does not happen if a numeric ARG is used."
1127 (interactive "*P")
1128 (if (or arg (not fortran-electric-line-number))
1129 (if arg
1130 (self-insert-command (prefix-numeric-value arg))
1131 (self-insert-command 1))
1132 (if (or (and (= 5 (current-column))
1133 (save-excursion
1134 (beginning-of-line)
1135 ;; In col 5 with only spaces to the left.
1136 (looking-at " \\{5\\}")))
1137 (and (= (if indent-tabs-mode
1138 fortran-minimum-statement-indent-tab
1139 fortran-minimum-statement-indent-fixed) (current-column))
1140 ;; In col 8 with a single tab to the left.
1141 (eq ?\t (char-after (line-beginning-position)))
1142 (not (or (eq last-command 'fortran-indent-line)
1143 (eq last-command
1144 'fortran-indent-new-line))))
1145 (save-excursion
1146 (re-search-backward "[^ \t0-9]"
1147 (line-beginning-position)
1148 t)) ; not a line number
1149 (looking-at "[0-9]")) ; within a line number
1150 (self-insert-command (prefix-numeric-value arg))
1151 (skip-chars-backward " \t")
1152 (insert last-command-char)
1153 (fortran-indent-line))))
1154
1155 \f
1156 (defun fortran-check-end-prog-re ()
1157 "Check a preliminary match against `fortran-end-prog-re'."
1158 ;; Having got a possible match for the subprogram end, we need a
1159 ;; match of whitespace, avoiding possible column 73+ stuff.
1160 (save-match-data
1161 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
1162 (buffer-substring (match-end 0)
1163 (min (line-end-position)
1164 (+ fortran-line-length
1165 (line-beginning-position)))))))
1166
1167 ;; Note that you can't just check backwards for `subroutine' &c in
1168 ;; case of un-marked main programs not at the start of the file.
1169 (defun fortran-beginning-of-subprogram ()
1170 "Move point to the beginning of the current Fortran subprogram."
1171 (interactive)
1172 (save-match-data
1173 (let ((case-fold-search t))
1174 (beginning-of-line -1)
1175 (if (catch 'ok
1176 (while (re-search-backward fortran-end-prog-re nil 'move)
1177 (if (fortran-check-end-prog-re)
1178 (throw 'ok t))))
1179 (forward-line)))))
1180
1181 (defun fortran-end-of-subprogram ()
1182 "Move point to the end of the current Fortran subprogram."
1183 (interactive)
1184 (save-match-data
1185 (let ((case-fold-search t))
1186 (if (save-excursion ; on END
1187 (beginning-of-line)
1188 (and (looking-at fortran-end-prog-re)
1189 (fortran-check-end-prog-re)))
1190 (forward-line)
1191 (beginning-of-line 2)
1192 (when (catch 'ok
1193 (while (re-search-forward fortran-end-prog-re nil 'move)
1194 (if (fortran-check-end-prog-re)
1195 (throw 'ok t))))
1196 (goto-char (match-beginning 0))
1197 (forward-line))))))
1198
1199 (defun fortran-previous-statement ()
1200 "Move point to beginning of the previous Fortran statement.
1201 Returns 'first-statement if that statement is the first
1202 non-comment Fortran statement in the file, and nil otherwise.
1203 Directive lines are treated as comments."
1204 (interactive)
1205 (let (not-first-statement continue-test)
1206 (beginning-of-line)
1207 (setq continue-test
1208 (and
1209 (not (looking-at fortran-comment-line-start-skip))
1210 (not (looking-at fortran-directive-re))
1211 (or (looking-at
1212 (concat "[ \t]*"
1213 (regexp-quote fortran-continuation-string)))
1214 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1215 (while (and (setq not-first-statement (zerop (forward-line -1)))
1216 (or (looking-at fortran-comment-line-start-skip)
1217 (looking-at fortran-directive-re)
1218 (looking-at
1219 (concat "[ \t]*"
1220 (regexp-quote fortran-continuation-string)))
1221 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1222 (looking-at (concat "[ \t]*" comment-start-skip)))))
1223 (cond ((and continue-test
1224 (not not-first-statement))
1225 (message "Incomplete continuation statement."))
1226 (continue-test
1227 (fortran-previous-statement))
1228 ((not not-first-statement)
1229 'first-statement))))
1230
1231 (defun fortran-next-statement ()
1232 "Move point to beginning of the next Fortran statement.
1233 Returns 'last-statement if that statement is the last
1234 non-comment Fortran statement in the file, and nil otherwise.
1235 Directive lines are treated as comments."
1236 (interactive)
1237 (let (not-last-statement)
1238 (beginning-of-line)
1239 (while (and (setq not-last-statement
1240 (and (zerop (forward-line 1))
1241 (not (eobp))))
1242 (or (looking-at fortran-comment-line-start-skip)
1243 (looking-at fortran-directive-re)
1244 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1245 (looking-at (concat "[ \t]*" comment-start-skip)))))
1246 (if (not not-last-statement)
1247 'last-statement)))
1248
1249 (defun fortran-looking-at-if-then ()
1250 "Return non-nil if at the start of a line with an IF ... THEN statement."
1251 ;; cf f90-looking-at-if-then.
1252 (let ((p (point))
1253 (i (fortran-beginning-if)))
1254 (if i
1255 (save-excursion
1256 (goto-char i)
1257 (beginning-of-line)
1258 (= (point) p)))))
1259
1260 ;; Used in hs-special-modes-alist.
1261 (defun fortran-end-of-block (&optional num)
1262 "Move point forward to the end of the current code block.
1263 With optional argument NUM, go forward that many balanced blocks.
1264 If NUM is negative, go backward to the start of a block. Does
1265 not check for consistency of block types. Interactively, pushes
1266 mark before moving point."
1267 (interactive "p")
1268 (if (interactive-p) (push-mark (point) t))
1269 (and num (< num 0) (fortran-beginning-of-block (- num)))
1270 (let ((case-fold-search t)
1271 (count (or num 1)))
1272 (end-of-line)
1273 (while (and (> count 0)
1274 (re-search-forward
1275 (concat "\\(" fortran-blocks-re
1276 (if fortran-check-all-num-for-matching-do
1277 "\\|^[ \t]*[0-9]+" "")
1278 "\\|continue\\|end\\)\\>")
1279 nil 'move))
1280 (beginning-of-line)
1281 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1282 (fortran-looking-at-if-then)
1283 (looking-at fortran-start-block-re))
1284 (setq count (1+ count))
1285 (if (or (looking-at fortran-end-block-re)
1286 (and (or (looking-at "^[0-9 \t]*continue")
1287 (and fortran-check-all-num-for-matching-do
1288 (looking-at "[ \t]*[0-9]+")))
1289 (fortran-check-for-matching-do)))
1290 (setq count (1- count))))
1291 (end-of-line))
1292 (if (> count 0) (error "Missing block end"))))
1293
1294 (defun fortran-beginning-of-block (&optional num)
1295 "Move point backwards to the start of the current code block.
1296 With optional argument NUM, go backward that many balanced
1297 blocks. If NUM is negative, go forward to the end of a block.
1298 Does not check for consistency of block types. Interactively,
1299 pushes mark before moving point."
1300 (interactive "p")
1301 (if (interactive-p) (push-mark (point) t))
1302 (and num (< num 0) (fortran-end-of-block (- num)))
1303 (let ((case-fold-search t)
1304 (count (or num 1)))
1305 (beginning-of-line)
1306 (while (and (> count 0)
1307 (re-search-backward
1308 (concat "\\(" fortran-blocks-re
1309 (if fortran-check-all-num-for-matching-do
1310 "\\|^[ \t]*[0-9]+" "")
1311 "\\|continue\\|end\\)\\>")
1312 nil 'move))
1313 (beginning-of-line)
1314 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1315 (fortran-looking-at-if-then)
1316 (looking-at fortran-start-block-re))
1317 (setq count (1- count))
1318 (if (or (looking-at fortran-end-block-re)
1319 (and (or (looking-at "^[0-9 \t]*continue")
1320 (and fortran-check-all-num-for-matching-do
1321 (looking-at "[ \t]*[0-9]+")))
1322 (fortran-check-for-matching-do)))
1323 (setq count (1+ count)))))
1324 ;; Includes an un-named main program block.
1325 (if (> count 0) (error "Missing block start"))))
1326
1327 \f
1328 (defun fortran-blink-match (regex keyword find-begin)
1329 "From a line matching REGEX, blink matching KEYWORD statement line.
1330 Use function FIND-BEGIN to match it."
1331 (let ((top-of-window (window-start))
1332 (end-point (point))
1333 (case-fold-search t)
1334 matching
1335 message)
1336 (when (save-excursion
1337 (beginning-of-line)
1338 (skip-chars-forward " \t0-9")
1339 (looking-at regex))
1340 (if (not (setq matching (funcall find-begin)))
1341 (setq message (concat "No matching " keyword "."))
1342 (if (< matching top-of-window)
1343 (save-excursion
1344 (goto-char matching)
1345 (beginning-of-line)
1346 (setq message
1347 (concat "Matches "
1348 (buffer-substring (point)
1349 (line-end-position)))))))
1350 (if message
1351 (message "%s" message)
1352 (goto-char matching)
1353 (sit-for blink-matching-delay)
1354 (goto-char end-point)))))
1355
1356 (defun fortran-blink-matching-if ()
1357 "From an ENDIF or ELSE statement, blink the matching IF statement."
1358 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1359 "if" #'fortran-beginning-if))
1360
1361 (defun fortran-blink-matching-do ()
1362 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1363 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
1364
1365 (defun fortran-mark-do ()
1366 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1367 The marks are pushed."
1368 (interactive)
1369 (let (enddo-point do-point)
1370 (if (setq enddo-point (fortran-end-do))
1371 (if (not (setq do-point (fortran-beginning-do)))
1372 (message "No matching do.")
1373 (goto-char enddo-point)
1374 (push-mark)
1375 (goto-char do-point)))))
1376
1377 (defun fortran-end-do ()
1378 "Search forward for first unmatched ENDDO.
1379 Return point or nil."
1380 (let ((case-fold-search t))
1381 (if (save-excursion (beginning-of-line)
1382 (skip-chars-forward " \t0-9")
1383 (looking-at "end[ \t]*do\\b"))
1384 ;; Sitting on one.
1385 (match-beginning 0)
1386 ;; Search for one.
1387 (save-excursion
1388 (let ((count 1))
1389 (while (and (not (zerop count))
1390 (not (eq (fortran-next-statement) 'last-statement))
1391 ;; Keep local to subprogram.
1392 (not (and (looking-at fortran-end-prog-re)
1393 (fortran-check-end-prog-re))))
1394 (skip-chars-forward " \t0-9")
1395 (cond ((looking-at "end[ \t]*do\\b")
1396 (setq count (1- count)))
1397 ((looking-at
1398 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1399 (setq count (1+ count)))))
1400 (and (zerop count)
1401 ;; All pairs accounted for.
1402 (point)))))))
1403
1404 (defun fortran-beginning-do ()
1405 "Search backwards for first unmatched DO [WHILE].
1406 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1407 (let ((case-fold-search t)
1408 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1409 (if (save-excursion
1410 (beginning-of-line)
1411 (skip-chars-forward " \t0-9")
1412 (looking-at dostart-re))
1413 ;; Sitting on one.
1414 (match-beginning 0)
1415 ;; Search for one.
1416 (save-excursion
1417 (let ((count 1))
1418 (while (and (not (zerop count))
1419 (not (eq (fortran-previous-statement) 'first-statement))
1420 ;; Keep local to subprogram.
1421 (not (and (looking-at fortran-end-prog-re)
1422 (fortran-check-end-prog-re))))
1423 (skip-chars-forward " \t0-9")
1424 (cond ((looking-at dostart-re)
1425 (setq count (1- count)))
1426 ;; Note labelled loop ends not considered.
1427 ((looking-at "end[ \t]*do\\b")
1428 (setq count (1+ count)))))
1429 (and (zerop count)
1430 ;; All pairs accounted for.
1431 (point)))))))
1432
1433 (defun fortran-mark-if ()
1434 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1435 The marks are pushed."
1436 (interactive)
1437 (let (endif-point if-point)
1438 (if (setq endif-point (fortran-end-if))
1439 (if (not (setq if-point (fortran-beginning-if)))
1440 (message "No matching if.")
1441 ;; Set mark, move point.
1442 (goto-char endif-point)
1443 (push-mark)
1444 (goto-char if-point)))))
1445
1446 (defun fortran-end-if ()
1447 "Search forwards for first unmatched ENDIF.
1448 Return point or nil."
1449 (let ((case-fold-search t))
1450 (if (save-excursion (beginning-of-line)
1451 (skip-chars-forward " \t0-9")
1452 (looking-at "end[ \t]*if\\b"))
1453 ;; Sitting on one.
1454 (match-beginning 0)
1455 ;; Search for one. The point has been already been moved to first
1456 ;; letter on line but this should not cause troubles.
1457 (save-excursion
1458 (let ((count 1))
1459 (while (and (not (zerop count))
1460 (not (eq (fortran-next-statement) 'last-statement))
1461 ;; Keep local to subprogram.
1462 (not (and (looking-at fortran-end-prog-re)
1463 (fortran-check-end-prog-re))))
1464 (skip-chars-forward " \t0-9")
1465 (cond ((looking-at "end[ \t]*if\\b")
1466 (setq count (1- count)))
1467 ((looking-at fortran-if-start-re)
1468 (save-excursion
1469 (if (or
1470 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1471 (let (then-test) ; multi-line if-then
1472 (while
1473 (and
1474 (zerop (forward-line 1))
1475 ;; Search forward for then.
1476 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1477 (not
1478 (setq then-test
1479 (looking-at
1480 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1481 then-test))
1482 (setq count (1+ count)))))))
1483 (and (zerop count)
1484 ;; All pairs accounted for.
1485 (point)))))))
1486
1487 (defun fortran-beginning-if ()
1488 "Search backwards for first unmatched IF-THEN.
1489 Return point or nil."
1490 (let ((case-fold-search t))
1491 (if (save-excursion
1492 ;; May be sitting on multi-line if-then statement, first
1493 ;; move to beginning of current statement. Note:
1494 ;; `fortran-previous-statement' moves to previous statement
1495 ;; *unless* current statement is first one. Only move
1496 ;; forward if not first-statement.
1497 (if (not (eq (fortran-previous-statement) 'first-statement))
1498 (fortran-next-statement))
1499 (skip-chars-forward " \t0-9")
1500 (and
1501 (looking-at fortran-if-start-re)
1502 (save-match-data
1503 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1504 ;; Multi-line if-then.
1505 (let (then-test)
1506 (while
1507 (and (zerop (forward-line 1))
1508 ;; Search forward for then.
1509 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1510 (not
1511 (setq then-test
1512 (looking-at
1513 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1514 then-test)))))
1515 ;; Sitting on one.
1516 (match-beginning 0)
1517 ;; Search for one.
1518 (save-excursion
1519 (let ((count 1))
1520 (while (and (not (zerop count))
1521 (not (eq (fortran-previous-statement) 'first-statement))
1522 ;; Keep local to subprogram.
1523 (not (and (looking-at fortran-end-prog-re)
1524 (fortran-check-end-prog-re))))
1525 (skip-chars-forward " \t0-9")
1526 (cond ((looking-at fortran-if-start-re)
1527 (save-excursion
1528 (if (or
1529 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1530 (let (then-test) ; multi-line if-then
1531 (while
1532 (and
1533 (zerop (forward-line 1))
1534 ;; Search forward for then.
1535 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1536 (not
1537 (setq then-test
1538 (looking-at
1539 (concat ".*then\\b[ \t]*"
1540 "[^ \t(=a-z0-9]"))))))
1541 then-test))
1542 (setq count (1- count)))))
1543 ((looking-at "end[ \t]*if\\b")
1544 (setq count (1+ count)))))
1545 (and (zerop count)
1546 ;; All pairs accounted for.
1547 (point)))))))
1548
1549 \f
1550 (defun fortran-indent-line ()
1551 "Indent current Fortran line based on its contents and on previous lines."
1552 (interactive "*")
1553 (let ((cfi (fortran-calculate-indent)))
1554 (save-excursion
1555 (beginning-of-line)
1556 (if (or (not (= cfi (fortran-current-line-indentation)))
1557 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1558 (not (fortran-line-number-indented-correctly-p))))
1559 (fortran-indent-to-column cfi)
1560 (beginning-of-line)
1561 (if (fortran-find-comment-start-skip)
1562 (fortran-indent-comment))))
1563 ;; Never leave point in left margin.
1564 (if (< (current-column) cfi)
1565 (move-to-column cfi))
1566 (and auto-fill-function
1567 (> (save-excursion (end-of-line) (current-column))
1568 fill-column)
1569 (save-excursion
1570 (end-of-line)
1571 (fortran-fill)))
1572 (when fortran-blink-matching-if
1573 (fortran-blink-matching-if)
1574 (fortran-blink-matching-do))))
1575
1576 (defun fortran-auto-fill ()
1577 "Function to use for `normal-auto-fill-function' in Fortran mode."
1578 (if (> (current-column) (current-fill-column))
1579 (let ((cfi (fortran-calculate-indent)))
1580 (save-excursion
1581 (beginning-of-line)
1582 (if (or (not (= cfi (fortran-current-line-indentation)))
1583 (and (re-search-forward "^[ \t]*[0-9]+"
1584 (+ (point) 4) t)
1585 (not (fortran-line-number-indented-correctly-p))))
1586 (fortran-indent-to-column cfi)
1587 (beginning-of-line)
1588 (if (fortran-find-comment-start-skip)
1589 (fortran-indent-comment))))
1590 (fortran-fill)
1591 ;; Never leave point in left margin.
1592 (if (< (current-column) cfi)
1593 (move-to-column cfi)))))
1594
1595 ;; Historically this was a separate function which advertised itself
1596 ;; as reindenting but only did so where `most likely to be necessary'.
1597 (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
1598
1599 (defun fortran-indent-subprogram ()
1600 "Properly indent the Fortran subprogram containing point."
1601 (interactive "*")
1602 (save-excursion
1603 (mark-defun)
1604 (message "Indenting subprogram...")
1605 (indent-region (point) (mark) nil))
1606 (message "Indenting subprogram...done."))
1607
1608 (defun fortran-calculate-indent ()
1609 "Calculates the Fortran indent column based on previous lines."
1610 (let (icol first-statement (case-fold-search t)
1611 (fortran-minimum-statement-indent
1612 (if indent-tabs-mode
1613 fortran-minimum-statement-indent-tab
1614 fortran-minimum-statement-indent-fixed)))
1615 (save-excursion
1616 (setq first-statement (fortran-previous-statement))
1617 (if first-statement
1618 (setq icol fortran-minimum-statement-indent)
1619 (if (= (point) (point-min))
1620 (setq icol fortran-minimum-statement-indent)
1621 (setq icol (fortran-current-line-indentation)))
1622 (skip-chars-forward " \t0-9")
1623 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1624 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1625 (let (then-test) ; multi-line if-then
1626 (while (and (zerop (forward-line 1))
1627 ;; Search forward for then.
1628 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1629 (not (setq then-test
1630 (looking-at
1631 ".*then\\b[ \t]\
1632 *[^ \t_$(=a-z0-9]")))))
1633 then-test))
1634 (setq icol (+ icol fortran-if-indent))))
1635 ((looking-at "else\\(if\\)?\\b")
1636 (setq icol (+ icol fortran-if-indent)))
1637 ((looking-at "select[ \t]*case[ \t](.*)")
1638 (setq icol (+ icol fortran-if-indent)))
1639 ((looking-at "case[ \t]*(.*)")
1640 (setq icol (+ icol fortran-if-indent)))
1641 ((looking-at "case[ \t]*default\\b")
1642 (setq icol (+ icol fortran-if-indent)))
1643 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1644 (setq icol (+ icol fortran-if-indent)))
1645 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1646 (setq icol (+ icol fortran-if-indent)))
1647 ((looking-at "do\\b")
1648 (setq icol (+ icol fortran-do-indent)))
1649 ((looking-at
1650 "\\(structure\\|union\\|map\\|interface\\)\
1651 \\b[ \t]*[^ \t=(a-z]")
1652 (setq icol (+ icol fortran-structure-indent)))
1653 ((and (looking-at fortran-end-prog-re1)
1654 (fortran-check-end-prog-re))
1655 ;; Previous END resets indent to minimum.
1656 (setq icol fortran-minimum-statement-indent))
1657 ;; Previous statement was a numbered DO loop without a
1658 ;; closing CONTINUE or END DO, so we indented the
1659 ;; terminator like the loop body.
1660 ((and fortran-check-all-num-for-matching-do
1661 (not (looking-at "\\(continue\\|end[ \t]*do\\)\\>"))
1662 (progn
1663 (beginning-of-line)
1664 (and (looking-at "[ \t]*[0-9]+")
1665 (fortran-check-for-matching-do))))
1666 (setq icol (- icol fortran-do-indent))))))
1667 (save-excursion
1668 (beginning-of-line)
1669 (cond ((looking-at "[ \t]*$"))
1670 ;; Check for directive before comment, so as not to indent.
1671 ((looking-at fortran-directive-re)
1672 (setq fortran-minimum-statement-indent 0 icol 0))
1673 ((looking-at fortran-comment-line-start-skip)
1674 (cond ((eq fortran-comment-indent-style 'relative)
1675 (setq icol (+ icol fortran-comment-line-extra-indent)))
1676 ((eq fortran-comment-indent-style 'fixed)
1677 (setq icol (+ fortran-minimum-statement-indent
1678 fortran-comment-line-extra-indent))))
1679 (setq fortran-minimum-statement-indent 0))
1680 ((or (looking-at (concat "[ \t]*"
1681 (regexp-quote
1682 fortran-continuation-string)))
1683 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1684 (skip-chars-forward " \t")
1685 ;; Do not introduce extra whitespace into a broken string.
1686 (setq icol
1687 (if (fortran-is-in-string-p (point))
1688 6
1689 (+ icol fortran-continuation-indent))))
1690 (first-statement)
1691 ;; The terminating statement is actually part of the
1692 ;; loop body, so unless it is a CONTINUE or END DO, we
1693 ;; indent it like the loop body (see above).
1694 ((and fortran-check-all-num-for-matching-do
1695 (looking-at "[ \t]*[0-9]+[ \t]*\
1696 \\(continue\\|end[ \t]*do\\)\\>")
1697 (fortran-check-for-matching-do))
1698 (setq icol (- icol fortran-do-indent)))
1699 (t
1700 (skip-chars-forward " \t0-9")
1701 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1702 (setq icol (- icol fortran-if-indent)))
1703 ((looking-at "else\\(if\\)?\\b")
1704 (setq icol (- icol fortran-if-indent)))
1705 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1706 (setq icol (- icol fortran-if-indent)))
1707 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1708 (setq icol (- icol fortran-if-indent)))
1709 ((and (looking-at "continue\\b")
1710 (fortran-check-for-matching-do))
1711 (setq icol (- icol fortran-do-indent)))
1712 ((looking-at "end[ \t]*do\\b")
1713 (setq icol (- icol fortran-do-indent)))
1714 ((looking-at "end[ \t]*\
1715 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1716 (setq icol (- icol fortran-structure-indent)))
1717 ((and (looking-at fortran-end-prog-re1)
1718 (fortran-check-end-prog-re)
1719 (not (= icol fortran-minimum-statement-indent)))
1720 (message "Warning: `end' not in column %d. Probably\
1721 an unclosed block." fortran-minimum-statement-indent))))))
1722 (max fortran-minimum-statement-indent icol)))
1723
1724 \f
1725 (defun fortran-current-line-indentation ()
1726 "Indentation of current line, ignoring Fortran line number or continuation.
1727 This is the column position of the first non-whitespace character
1728 aside from the line number and/or column 5/8 line-continuation character.
1729 For comment lines, returns indentation of the first
1730 non-indentation text within the comment."
1731 (save-excursion
1732 (beginning-of-line)
1733 (cond ((looking-at fortran-comment-line-start-skip)
1734 (goto-char (match-end 0))
1735 (skip-chars-forward
1736 (if (stringp fortran-comment-indent-char)
1737 fortran-comment-indent-char
1738 (char-to-string fortran-comment-indent-char))))
1739 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1740 (goto-char (match-end 0)))
1741 (t
1742 ;; Move past line number.
1743 (skip-chars-forward "[ \t0-9]")))
1744 ;; Move past whitespace.
1745 (skip-chars-forward " \t")
1746 (current-column)))
1747
1748 (defun fortran-indent-to-column (col)
1749 "Indent current line to column COL.
1750 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1751 line, and this continuation character is retained on indentation;
1752 2) If `fortran-continuation-string' is the first non-whitespace
1753 character, this is a continuation line;
1754 3) A non-continuation line which has a number as the first
1755 non-whitespace character is a numbered line.
1756 4) A TAB followed by a digit indicates a continuation line."
1757 (save-excursion
1758 (beginning-of-line)
1759 (if (looking-at fortran-comment-line-start-skip)
1760 (if fortran-comment-indent-style
1761 (let* ((char (if (stringp fortran-comment-indent-char)
1762 (aref fortran-comment-indent-char 0)
1763 fortran-comment-indent-char))
1764 (chars (string ?\s ?\t char)))
1765 (goto-char (match-end 0))
1766 (skip-chars-backward chars)
1767 (delete-region (point) (progn (skip-chars-forward chars)
1768 (point)))
1769 (insert-char char (- col (current-column)))))
1770 (if (looking-at "\t[1-9]")
1771 (if indent-tabs-mode
1772 (goto-char (match-end 0))
1773 (delete-char 2)
1774 (insert-char ?\s 5)
1775 (insert fortran-continuation-string))
1776 (if (looking-at " \\{5\\}[^ 0\n]")
1777 (if indent-tabs-mode
1778 (progn (delete-char 6)
1779 (insert ?\t (fortran-numerical-continuation-char) 1))
1780 (forward-char 6))
1781 (delete-horizontal-space)
1782 ;; Put line number in columns 0-4, or
1783 ;; continuation character in column 5.
1784 (cond ((eobp))
1785 ((looking-at (regexp-quote fortran-continuation-string))
1786 (if indent-tabs-mode
1787 (progn
1788 (indent-to
1789 (if indent-tabs-mode
1790 fortran-minimum-statement-indent-tab
1791 fortran-minimum-statement-indent-fixed))
1792 (delete-char 1)
1793 (insert-char (fortran-numerical-continuation-char) 1))
1794 (indent-to 5)
1795 (forward-char 1)))
1796 ((looking-at "[0-9]+")
1797 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1798 (if (< extra-space 0)
1799 (message "Warning: line number exceeds 5-digit limit.")
1800 (indent-to (min fortran-line-number-indent extra-space))))
1801 (skip-chars-forward "0-9")))))
1802 ;; Point is now after any continuation character or line number.
1803 ;; Put body of statement where specified.
1804 (delete-horizontal-space)
1805 (indent-to col)
1806 ;; Indent any comment following code on the same line.
1807 (when (fortran-find-comment-start-skip)
1808 (goto-char (match-beginning 0))
1809 (unless (= (current-column) (fortran-comment-indent))
1810 (delete-horizontal-space)
1811 (indent-to (fortran-comment-indent)))))))
1812
1813 (defun fortran-line-number-indented-correctly-p ()
1814 "Return t if current line's line number is correctly indented.
1815 Do not call if there is no line number."
1816 (save-excursion
1817 (beginning-of-line)
1818 (skip-chars-forward " \t")
1819 (and (<= (current-column) fortran-line-number-indent)
1820 (or (= (current-column) fortran-line-number-indent)
1821 (progn (skip-chars-forward "0-9")
1822 (= (current-column) 5))))))
1823
1824 (defun fortran-check-for-matching-do ()
1825 "When called from a numbered statement, return t if matching DO is found.
1826 Otherwise return nil."
1827 (let ((case-fold-search t)
1828 charnum)
1829 (save-excursion
1830 (beginning-of-line)
1831 (when (looking-at "[ \t]*[0-9]+")
1832 (skip-chars-forward " \t")
1833 (skip-chars-forward "0") ; skip past leading zeros
1834 (setq charnum
1835 (buffer-substring (point) (progn
1836 (skip-chars-forward "0-9")
1837 (point))))
1838 (beginning-of-line)
1839 (save-restriction
1840 (save-excursion
1841 (narrow-to-defun)
1842 (and (re-search-backward
1843 (concat
1844 "\\(^[ \t0-9]*do[ \t]*0*"
1845 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1846 charnum "\\b\\)")
1847 nil t)
1848 (looking-at
1849 (concat "^[ \t0-9]*do[ \t]*0*"
1850 charnum)))))))))
1851
1852 (defun fortran-find-comment-start-skip (&optional all)
1853 "Move to past `comment-start-skip' found on current line.
1854 Return non-nil if `comment-start-skip' found, nil if not.
1855 If ALL is nil, only match comments that start in column > 0."
1856 ;; Hopefully at some point we can just use the line below! -stef
1857 ;; (comment-search-forward (line-end-position) t))
1858 (when (or all comment-start-skip)
1859 (let ((pos (point))
1860 (css (if comment-start-skip
1861 (concat fortran-comment-line-start-skip
1862 "\\|" comment-start-skip)
1863 fortran-comment-line-start-skip)))
1864 (when (re-search-forward css (line-end-position) t)
1865 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1866 (or (save-match-data
1867 (not (fortran-is-in-string-p (match-beginning 0))))
1868 ;; Recurse for rest of line.
1869 (fortran-find-comment-start-skip all)))
1870 (point)
1871 (goto-char pos)
1872 nil)))))
1873
1874 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1875 ;; Test if TAB format continuation lines work.
1876 (defun fortran-is-in-string-p (where)
1877 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
1878 (save-excursion
1879 (goto-char where)
1880 (cond
1881 ((bolp) nil) ; bol is never inside a string
1882 ((save-excursion ; comment lines too
1883 (beginning-of-line)
1884 (looking-at fortran-comment-line-start-skip)) nil)
1885 (t (let ((parse-state '(0 nil nil nil nil nil 0))
1886 (quoted-comment-start (if comment-start
1887 (regexp-quote comment-start)))
1888 (not-done t)
1889 parse-limit end-of-line)
1890 ;; Move to start of current statement.
1891 (fortran-next-statement)
1892 (fortran-previous-statement)
1893 ;; Now parse up to WHERE.
1894 (while not-done
1895 (if (or ;; Skip to next line if:
1896 ;; - comment line?
1897 (looking-at fortran-comment-line-start-skip)
1898 ;; - at end of line?
1899 (eolp)
1900 ;; - not in a string and after comment-start?
1901 (and (not (nth 3 parse-state))
1902 comment-start
1903 (equal comment-start
1904 (char-to-string (preceding-char)))))
1905 (if (> (forward-line) 0)
1906 (setq not-done nil))
1907 ;; else:
1908 ;; If we are at beginning of code line, skip any
1909 ;; whitespace, labels and tab continuation markers.
1910 (if (bolp) (skip-chars-forward " \t0-9"))
1911 ;; If we are in column <= 5 now, check for continuation char.
1912 (cond ((= 5 (current-column)) (forward-char 1))
1913 ((and (< (current-column) 5)
1914 (equal fortran-continuation-string
1915 (char-to-string (following-char)))
1916 (forward-char 1))))
1917 ;; Find out parse-limit from here.
1918 (setq end-of-line (line-end-position))
1919 (setq parse-limit (min where end-of-line))
1920 ;; Parse max up to comment-start, if non-nil and in current line.
1921 (if comment-start
1922 (save-excursion
1923 (if (re-search-forward quoted-comment-start end-of-line t)
1924 (setq parse-limit (min (point) parse-limit)))))
1925 ;; Now parse if still in limits.
1926 (if (< (point) where)
1927 (setq parse-state (parse-partial-sexp
1928 (point) parse-limit nil nil parse-state))
1929 (setq not-done nil))))
1930 ;; Result.
1931 (nth 3 parse-state))))))
1932
1933 ;; From old version.
1934 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1935
1936 (defun fortran-fill ()
1937 "Fill the current line at an appropriate point(s)."
1938 (let* ((auto-fill-function #'fortran-auto-fill)
1939 (opoint (point))
1940 (bol (line-beginning-position))
1941 (eol (line-end-position))
1942 (bos (min eol (+ bol (fortran-current-line-indentation))))
1943 ;; If in a string at fill-column, break it either before the
1944 ;; initial quote, or at fill-col (if string is too long).
1945 (quote
1946 (save-excursion
1947 (goto-char bol)
1948 ;; OK to break quotes on comment lines.
1949 (unless (looking-at fortran-comment-line-start-skip)
1950 (let (fcpoint start)
1951 (move-to-column fill-column)
1952 (when (fortran-is-in-string-p (setq fcpoint (point)))
1953 (save-excursion
1954 (re-search-backward "\\S\"\\s\"\\S\"?" bol t)
1955 (setq start
1956 (if fortran-break-before-delimiters
1957 (point)
1958 (1+ (point)))))
1959 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t)
1960 (backward-char 2))
1961 ;; If the current string is longer than (fill-column
1962 ;; - 6) chars, break it at the fill column (else
1963 ;; infinite loop).
1964 (if (> (- (point) start)
1965 (- fill-column 6 fortran-continuation-indent))
1966 fcpoint
1967 start))))))
1968 ;; Decide where to split the line. If a position for a quoted
1969 ;; string was found above then use that, else break the line
1970 ;; before/after the last delimiter.
1971 (fill-point
1972 (or quote
1973 (save-excursion
1974 ;; If f-b-b-d is t, have an extra column to play with,
1975 ;; since delimiter gets shifted to new line.
1976 (move-to-column (if fortran-break-before-delimiters
1977 (1+ fill-column)
1978 fill-column))
1979 (let ((repeat t))
1980 (while repeat
1981 (setq repeat nil)
1982 ;; Adapted from f90-find-breakpoint.
1983 (re-search-backward fortran-break-delimiters-re bol)
1984 (if (not fortran-break-before-delimiters)
1985 (if (looking-at fortran-no-break-re)
1986 ;; Deal with cases such as "**" split over
1987 ;; fill-col. Simpler alternative would be
1988 ;; to start from (1- fill-column) above.
1989 (if (> (+ 2 (current-column)) fill-column)
1990 (setq repeat t)
1991 (forward-char 2))
1992 (forward-char 1))
1993 (backward-char)
1994 (or (looking-at fortran-no-break-re)
1995 (forward-char)))))
1996 ;; Line indented beyond fill-column?
1997 (when (<= (point) bos)
1998 (move-to-column (1+ fill-column))
1999 ;; What is this doing???
2000 (or (re-search-forward "[\t\n,'+-/*)=]" eol t)
2001 (goto-char bol)))
2002 (if (bolp)
2003 (re-search-forward "[ \t]" opoint t))
2004 (point)))))
2005 ;; If we are in an in-line comment, don't break unless the
2006 ;; line of code is longer than it should be. Otherwise
2007 ;; break the line at the column computed above.
2008 ;;
2009 ;; Need to use fortran-find-comment-start-skip to make sure that
2010 ;; quoted !'s don't prevent a break.
2011 (when (and (save-excursion
2012 (beginning-of-line)
2013 (if (not (fortran-find-comment-start-skip))
2014 t
2015 (goto-char (match-beginning 0))
2016 (>= (point) fill-point)))
2017 (save-excursion
2018 (goto-char fill-point)
2019 (not (bolp)))
2020 (> (save-excursion
2021 (goto-char opoint)
2022 (current-column))
2023 (min (1+ fill-column)
2024 (+ (fortran-calculate-indent)
2025 fortran-continuation-indent))))
2026 (goto-char fill-point)
2027 (fortran-break-line)
2028 (end-of-line))))
2029
2030 (defun fortran-break-line ()
2031 "Call `fortran-split-line'. Joins continuation lines first, then refills."
2032 (let ((bol (line-beginning-position))
2033 (comment-string
2034 (save-excursion
2035 (if (fortran-find-comment-start-skip)
2036 (delete-and-extract-region
2037 (match-beginning 0) (line-end-position))))))
2038 ;; Forward line 1 really needs to go to next non white line.
2039 (if (save-excursion (forward-line)
2040 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
2041 (progn
2042 (end-of-line)
2043 (delete-region (point) (match-end 0))
2044 (delete-horizontal-space)
2045 (fortran-fill))
2046 (fortran-split-line))
2047 (if comment-string
2048 (save-excursion
2049 (goto-char bol)
2050 (end-of-line)
2051 (delete-horizontal-space)
2052 (indent-to (fortran-comment-indent))
2053 (insert comment-string)))))
2054
2055 (defun fortran-analyze-file-format ()
2056 "Return nil if fixed format is used, t if TAB formatting is used.
2057 Use `fortran-tab-mode-default' if no non-comment statements are found
2058 before the end or in the first `fortran-analyze-depth' lines."
2059 (let ((i 0))
2060 (save-excursion
2061 (goto-char (point-min))
2062 (while (not (or
2063 (eobp)
2064 (eq (char-after) ?\t)
2065 (looking-at " \\{6\\}")
2066 (> i fortran-analyze-depth)))
2067 (forward-line)
2068 (setq i (1+ i)))
2069 (cond
2070 ((eq (char-after) ?\t) t)
2071 ((looking-at " \\{6\\}") nil)
2072 (t fortran-tab-mode-default)))))
2073
2074 (defun fortran-fill-paragraph (&optional justify)
2075 "Fill surrounding comment block as paragraphs, else fill statement.
2076 Intended as the value of `fill-paragraph-function'.
2077 A comment block is filled by calling `fill-comment-paragraph' with
2078 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
2079 Always returns non-nil (to prevent `fill-paragraph' being called)."
2080 (interactive "*P")
2081 (or (fill-comment-paragraph justify)
2082 (fortran-fill-statement)
2083 t))
2084
2085 (defun fortran-fill-statement ()
2086 "Fill a Fortran statement up to `fill-column'."
2087 (interactive "*")
2088 (let ((auto-fill-function #'fortran-auto-fill))
2089 (unless (save-excursion
2090 (beginning-of-line)
2091 (or (looking-at "[ \t]*$")
2092 (looking-at fortran-comment-line-start-skip)
2093 (and comment-start-skip
2094 (looking-at (concat "[ \t]*" comment-start-skip)))))
2095 (save-excursion
2096 ;; Find beginning of statement.
2097 (fortran-next-statement)
2098 (fortran-previous-statement)
2099 ;; Re-indent initially.
2100 (fortran-indent-line)
2101 ;; Replace newline plus continuation field plus indentation with
2102 ;; single space.
2103 (while (progn
2104 (forward-line)
2105 (fortran-remove-continuation)))
2106 (fortran-previous-statement)))
2107 (fortran-indent-line)))
2108
2109 (defun fortran-strip-sequence-nos (&optional do-space)
2110 "Delete all text in column `fortran-line-length' (default 72) and up.
2111 This is assumed to be sequence numbers. Normally also deletes
2112 trailing whitespace after stripping such text. Supplying prefix
2113 arg DO-SPACE prevents stripping the whitespace."
2114 (interactive "*p")
2115 (save-excursion
2116 (goto-char (point-min))
2117 (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length)
2118 nil t)
2119 (replace-match "" nil nil nil 1)
2120 (unless do-space (delete-horizontal-space)))))
2121
2122 ;; This code used to live in add-log.el, but this is a better place
2123 ;; for it.
2124 (defun fortran-current-defun ()
2125 "Function to use for `add-log-current-defun-function' in Fortran mode."
2126 (save-excursion
2127 ;; We must be inside function body for this to work.
2128 (fortran-beginning-of-subprogram)
2129 (let ((case-fold-search t)) ; case-insensitive
2130 ;; Search for fortran subprogram start.
2131 (if (re-search-forward
2132 (concat "^[ \t]*\\(program\\|subroutine\\|function"
2133 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
2134 "\\(block[ \t]*data\\)\\)")
2135 (save-excursion (fortran-end-of-subprogram)
2136 (point))
2137 t)
2138 (or (match-string-no-properties 2)
2139 (progn
2140 ;; Move to EOL or before first left paren.
2141 (if (re-search-forward "[(\n]" nil t)
2142 (progn (backward-char)
2143 (skip-chars-backward " \t"))
2144 (end-of-line))
2145 ;; Use the name preceding that.
2146 (buffer-substring-no-properties (point) (progn (backward-sexp)
2147 (point)))))
2148 "main"))))
2149
2150 (provide 'fortran)
2151
2152 ;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
2153 ;;; fortran.el ends here