Merge from emacs--rel--22
[bpt/emacs.git] / 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
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 (char-valid-p 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 (defun fortran-gud-find-expr ()
912 ;; Consider \n as punctuation (end of expression).
913 (with-syntax-table fortran-gud-syntax-table
914 (gud-find-c-expr)))
915
916 (defsubst fortran-comment-indent ()
917 "Return the indentation appropriate for the current comment line.
918 This is 0 for a line matching `fortran-comment-line-start-skip', else
919 the value of `comment-column' (leaving at least one space after code)."
920 (if (looking-at fortran-comment-line-start-skip) 0
921 (save-excursion
922 (skip-chars-backward " \t")
923 (max (1+ (current-column)) comment-column))))
924
925 (defun fortran-indent-comment ()
926 "Align or create comment on current line.
927 Existing comments of all types are recognized and aligned.
928 If the line has no comment, a side-by-side comment is inserted and aligned,
929 if the value of `comment-start' is not nil and allows such comments.
930 Otherwise, a separate-line comment is inserted, on this line
931 or on a new line inserted before this line if this line is not blank."
932 (interactive "*")
933 (beginning-of-line)
934 ;; Recognize existing comments of either kind.
935 (cond ((fortran-find-comment-start-skip 'all)
936 (goto-char (match-beginning 0))
937 (if (bolp)
938 (fortran-indent-line)
939 (unless (= (current-column) (fortran-comment-indent))
940 (delete-horizontal-space)
941 (indent-to (fortran-comment-indent)))))
942 ;; No existing comment.
943 ;; If side-by-side comments are defined, insert one,
944 ;; unless line is now blank.
945 ((and comment-start (not (looking-at "[ \t]*$"))
946 (string-match comment-start-skip (concat " " comment-start)))
947 (end-of-line)
948 (delete-horizontal-space)
949 (indent-to (fortran-comment-indent))
950 (insert comment-start))
951 ;; Else insert separate-line comment, making a new line if nec.
952 (t
953 (if (looking-at "^[ \t]*$")
954 (delete-horizontal-space)
955 (beginning-of-line)
956 (insert ?\n)
957 (forward-char -1))
958 (insert fortran-comment-line-start)
959 (insert-char (if (stringp fortran-comment-indent-char)
960 (aref fortran-comment-indent-char 0)
961 fortran-comment-indent-char)
962 (- (fortran-calculate-indent) (current-column))))))
963
964 (defun fortran-comment-region (beg-region end-region arg)
965 "Comment every line in the region.
966 Inserts the string variable `fortran-comment-region' at the beginning of
967 every line in the region.
968 BEG-REGION and END-REGION specify the region boundaries.
969 With non-nil ARG, uncomments the region."
970 (interactive "*r\nP")
971 (let ((end-region-mark (copy-marker end-region))
972 (save-point (point-marker)))
973 (goto-char beg-region)
974 (beginning-of-line)
975 (if arg
976 (let ((com (regexp-quote fortran-comment-region))) ; uncomment
977 (if (looking-at com)
978 (delete-region (point) (match-end 0)))
979 (while (and (zerop (forward-line 1))
980 (< (point) end-region-mark))
981 (if (looking-at com)
982 (delete-region (point) (match-end 0)))))
983 (insert fortran-comment-region) ; comment
984 (while (and (zerop (forward-line 1))
985 (< (point) end-region-mark))
986 (insert fortran-comment-region)))
987 (goto-char save-point)
988 (set-marker end-region-mark nil)
989 (set-marker save-point nil)))
990
991 ;; uncomment-region calls this with 3 args.
992 (defun fortran-uncomment-region (start end &optional ignored)
993 "Uncomment every line in the region."
994 (fortran-comment-region start end t))
995
996 \f
997 (defun fortran-abbrev-start ()
998 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
999 Any other key combination is executed normally."
1000 (interactive "*")
1001 (insert last-command-char)
1002 (let* ((event (if (fboundp 'next-command-event) ; XEmacs
1003 (next-command-event)
1004 (read-event)))
1005 (char (if (fboundp 'event-to-character)
1006 (event-to-character event) event)))
1007 ;; Insert char if not equal to `?', or if abbrev-mode is off.
1008 (if (and abbrev-mode (or (eq char ??) (eq char help-char)
1009 (memq event help-event-list)))
1010 (fortran-abbrev-help)
1011 (push event unread-command-events))))
1012
1013 (defun fortran-abbrev-help ()
1014 "List the currently defined abbrevs in Fortran mode."
1015 (interactive)
1016 (message "Listing abbrev table...")
1017 (display-buffer (fortran-prepare-abbrev-list-buffer))
1018 (message "Listing abbrev table...done"))
1019
1020 (defun fortran-prepare-abbrev-list-buffer ()
1021 "Create a buffer listing the Fortran mode abbreviations."
1022 (with-current-buffer (get-buffer-create "*Abbrevs*")
1023 (erase-buffer)
1024 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
1025 (goto-char (point-min))
1026 (set-buffer-modified-p nil)
1027 (edit-abbrevs-mode))
1028 (get-buffer-create "*Abbrevs*"))
1029
1030 (defun fortran-column-ruler ()
1031 "Insert a column ruler momentarily above current line, till next keystroke.
1032 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
1033 format mode, and `fortran-column-ruler-tab' in TAB format mode.
1034 The next key typed is executed unless it is SPC."
1035 (interactive)
1036 (momentary-string-display
1037 (if indent-tabs-mode
1038 fortran-column-ruler-tab
1039 fortran-column-ruler-fixed)
1040 (save-excursion
1041 (beginning-of-line)
1042 (if (eq (window-start (selected-window))
1043 (window-point (selected-window)))
1044 (line-beginning-position 2)
1045 (point)))
1046 nil "Type SPC or any command to erase ruler."))
1047
1048 (defun fortran-window-create ()
1049 "Make the window `fortran-line-length' (default 72) columns wide.
1050 See also `fortran-window-create-momentarily'."
1051 (interactive)
1052 (let ((window-min-width 2))
1053 (unless (window-full-width-p)
1054 (enlarge-window-horizontally (- (frame-width)
1055 (window-width) 1)))
1056 (let* ((window-edges (window-edges))
1057 (scroll-bar-width (- (nth 2 window-edges)
1058 (car window-edges)
1059 (window-width))))
1060 (split-window-horizontally (+ fortran-line-length scroll-bar-width)))
1061 (other-window 1)
1062 (switch-to-buffer " fortran-window-extra" t)
1063 (select-window (previous-window))))
1064
1065 (defun fortran-window-create-momentarily (&optional arg)
1066 "Momentarily make the window `fortran-line-length' (default 72) columns wide.
1067 Optional ARG non-nil and non-unity disables the momentary feature.
1068 See also `fortran-window-create'."
1069 (interactive "p")
1070 (if (or (not arg)
1071 (= arg 1))
1072 (save-window-excursion
1073 (progn
1074 (condition-case nil
1075 (fortran-window-create)
1076 (error (error "No room for Fortran window")))
1077 (message "Type SPC to continue editing.")
1078 (let ((char (read-event)))
1079 (or (equal char ?\s)
1080 (setq unread-command-events (list char))))))
1081 (fortran-window-create)))
1082
1083 (defun fortran-split-line ()
1084 "Break line at point and insert continuation marker and alignment."
1085 (interactive "*")
1086 (delete-horizontal-space)
1087 (if (save-excursion
1088 (let ((pos (point)))
1089 (beginning-of-line)
1090 (and (fortran-find-comment-start-skip 'all)
1091 (< (match-beginning 0) pos))))
1092 (insert ?\n (match-string 0))
1093 (if indent-tabs-mode
1094 (insert ?\n ?\t (fortran-numerical-continuation-char))
1095 (insert "\n " fortran-continuation-string))) ; space after \n important
1096 (fortran-indent-line)) ; when cont string is C, c or *
1097
1098 (defun fortran-remove-continuation ()
1099 "Delete any Fortran continuation characters at point.
1100 Returns t if anything actually deleted."
1101 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1102 (replace-match "")
1103 (delete-indentation)
1104 t))
1105
1106 (defun fortran-join-line (arg)
1107 "Join current line to the previous one and re-indent.
1108 With a prefix argument, repeat this operation that many times.
1109 If the prefix argument ARG is negative, join the next -ARG lines.
1110 Continuation lines are correctly handled."
1111 (interactive "*p")
1112 (save-excursion
1113 (when (> 0 arg)
1114 (setq arg (- arg))
1115 (forward-line arg))
1116 (while (not (zerop arg))
1117 (beginning-of-line)
1118 (or (fortran-remove-continuation)
1119 (delete-indentation))
1120 (setq arg (1- arg)))
1121 (fortran-indent-line)))
1122
1123 (defun fortran-numerical-continuation-char ()
1124 "Return a digit for tab-digit style of continuation lines.
1125 If previous line is a tab-digit continuation line, return that digit
1126 plus one, otherwise return 1. Zero not allowed."
1127 (save-excursion
1128 (forward-line -1)
1129 (if (looking-at "\t[1-9]")
1130 (+ ?1 (% (- (char-after (1+ (point))) ?0) 9))
1131 ?1)))
1132
1133 (put 'fortran-electric-line-number 'delete-selection t)
1134 (defun fortran-electric-line-number (arg)
1135 "Self insert, but if part of a Fortran line number indent it automatically.
1136 Auto-indent does not happen if a numeric ARG is used."
1137 (interactive "*P")
1138 (if (or arg (not fortran-electric-line-number))
1139 (if arg
1140 (self-insert-command (prefix-numeric-value arg))
1141 (self-insert-command 1))
1142 (if (or (and (= 5 (current-column))
1143 (save-excursion
1144 (beginning-of-line)
1145 ;; In col 5 with only spaces to the left.
1146 (looking-at " \\{5\\}")))
1147 (and (= (if indent-tabs-mode
1148 fortran-minimum-statement-indent-tab
1149 fortran-minimum-statement-indent-fixed) (current-column))
1150 ;; In col 8 with a single tab to the left.
1151 (eq ?\t (char-after (line-beginning-position)))
1152 (not (or (eq last-command 'fortran-indent-line)
1153 (eq last-command
1154 'fortran-indent-new-line))))
1155 (save-excursion
1156 (re-search-backward "[^ \t0-9]"
1157 (line-beginning-position)
1158 t)) ; not a line number
1159 (looking-at "[0-9]")) ; within a line number
1160 (self-insert-command (prefix-numeric-value arg))
1161 (skip-chars-backward " \t")
1162 (insert last-command-char)
1163 (fortran-indent-line))))
1164
1165 \f
1166 (defun fortran-check-end-prog-re ()
1167 "Check a preliminary match against `fortran-end-prog-re'."
1168 ;; Having got a possible match for the subprogram end, we need a
1169 ;; match of whitespace, avoiding possible column 73+ stuff.
1170 (save-match-data
1171 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
1172 (buffer-substring (match-end 0)
1173 (min (line-end-position)
1174 (+ fortran-line-length
1175 (line-beginning-position)))))))
1176
1177 ;; Note that you can't just check backwards for `subroutine' &c in
1178 ;; case of un-marked main programs not at the start of the file.
1179 (defun fortran-beginning-of-subprogram ()
1180 "Move point to the beginning of the current Fortran subprogram."
1181 (interactive)
1182 (save-match-data
1183 (let ((case-fold-search t))
1184 (beginning-of-line -1)
1185 (if (catch 'ok
1186 (while (re-search-backward fortran-end-prog-re nil 'move)
1187 (if (fortran-check-end-prog-re)
1188 (throw 'ok t))))
1189 (forward-line)))))
1190
1191 (defun fortran-end-of-subprogram ()
1192 "Move point to the end of the current Fortran subprogram."
1193 (interactive)
1194 (save-match-data
1195 (let ((case-fold-search t))
1196 (if (save-excursion ; on END
1197 (beginning-of-line)
1198 (and (looking-at fortran-end-prog-re)
1199 (fortran-check-end-prog-re)))
1200 (forward-line)
1201 (beginning-of-line 2)
1202 (catch 'ok
1203 (while (re-search-forward fortran-end-prog-re nil 'move)
1204 (if (fortran-check-end-prog-re)
1205 (throw 'ok t))))
1206 (goto-char (match-beginning 0))
1207 (forward-line)))))
1208
1209 (defun fortran-previous-statement ()
1210 "Move point to beginning of the previous Fortran statement.
1211 Returns 'first-statement if that statement is the first
1212 non-comment Fortran statement in the file, and nil otherwise.
1213 Directive lines are treated as comments."
1214 (interactive)
1215 (let (not-first-statement continue-test)
1216 (beginning-of-line)
1217 (setq continue-test
1218 (and
1219 (not (looking-at fortran-comment-line-start-skip))
1220 (not (looking-at fortran-directive-re))
1221 (or (looking-at
1222 (concat "[ \t]*"
1223 (regexp-quote fortran-continuation-string)))
1224 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1225 (while (and (setq not-first-statement (zerop (forward-line -1)))
1226 (or (looking-at fortran-comment-line-start-skip)
1227 (looking-at fortran-directive-re)
1228 (looking-at
1229 (concat "[ \t]*"
1230 (regexp-quote fortran-continuation-string)))
1231 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1232 (looking-at (concat "[ \t]*" comment-start-skip)))))
1233 (cond ((and continue-test
1234 (not not-first-statement))
1235 (message "Incomplete continuation statement."))
1236 (continue-test
1237 (fortran-previous-statement))
1238 ((not not-first-statement)
1239 'first-statement))))
1240
1241 (defun fortran-next-statement ()
1242 "Move point to beginning of the next Fortran statement.
1243 Returns 'last-statement if that statement is the last
1244 non-comment Fortran statement in the file, and nil otherwise.
1245 Directive lines are treated as comments."
1246 (interactive)
1247 (let (not-last-statement)
1248 (beginning-of-line)
1249 (while (and (setq not-last-statement
1250 (and (zerop (forward-line 1))
1251 (not (eobp))))
1252 (or (looking-at fortran-comment-line-start-skip)
1253 (looking-at fortran-directive-re)
1254 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1255 (looking-at (concat "[ \t]*" comment-start-skip)))))
1256 (if (not not-last-statement)
1257 'last-statement)))
1258
1259 (defun fortran-looking-at-if-then ()
1260 "Return non-nil if at the start of a line with an IF ... THEN statement."
1261 ;; cf f90-looking-at-if-then.
1262 (let ((p (point))
1263 (i (fortran-beginning-if)))
1264 (if i
1265 (save-excursion
1266 (goto-char i)
1267 (beginning-of-line)
1268 (= (point) p)))))
1269
1270 ;; Used in hs-special-modes-alist.
1271 (defun fortran-end-of-block (&optional num)
1272 "Move point forward to the end of the current code block.
1273 With optional argument NUM, go forward that many balanced blocks.
1274 If NUM is negative, go backward to the start of a block. Does
1275 not check for consistency of block types. Interactively, pushes
1276 mark before moving point."
1277 (interactive "p")
1278 (if (interactive-p) (push-mark (point) t))
1279 (and num (< num 0) (fortran-beginning-of-block (- num)))
1280 (let ((case-fold-search t)
1281 (count (or num 1)))
1282 (end-of-line)
1283 (while (and (> count 0)
1284 (re-search-forward
1285 (concat "\\(" fortran-blocks-re
1286 (if fortran-check-all-num-for-matching-do
1287 "\\|^[ \t]*[0-9]+" "")
1288 "\\|continue\\|end\\)\\>")
1289 nil 'move))
1290 (beginning-of-line)
1291 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1292 (fortran-looking-at-if-then)
1293 (looking-at fortran-start-block-re))
1294 (setq count (1+ count))
1295 (if (or (looking-at fortran-end-block-re)
1296 (and (or (looking-at "^[0-9 \t]*continue")
1297 (and fortran-check-all-num-for-matching-do
1298 (looking-at "[ \t]*[0-9]+")))
1299 (fortran-check-for-matching-do)))
1300 (setq count (1- count))))
1301 (end-of-line))
1302 (if (> count 0) (error "Missing block end"))))
1303
1304 (defun fortran-beginning-of-block (&optional num)
1305 "Move point backwards to the start of the current code block.
1306 With optional argument NUM, go backward that many balanced
1307 blocks. If NUM is negative, go forward to the end of a block.
1308 Does not check for consistency of block types. Interactively,
1309 pushes mark before moving point."
1310 (interactive "p")
1311 (if (interactive-p) (push-mark (point) t))
1312 (and num (< num 0) (fortran-end-of-block (- num)))
1313 (let ((case-fold-search t)
1314 (count (or num 1)))
1315 (beginning-of-line)
1316 (while (and (> count 0)
1317 (re-search-backward
1318 (concat "\\(" fortran-blocks-re
1319 (if fortran-check-all-num-for-matching-do
1320 "\\|^[ \t]*[0-9]+" "")
1321 "\\|continue\\|end\\)\\>")
1322 nil 'move))
1323 (beginning-of-line)
1324 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1325 (fortran-looking-at-if-then)
1326 (looking-at fortran-start-block-re))
1327 (setq count (1- count))
1328 (if (or (looking-at fortran-end-block-re)
1329 (and (or (looking-at "^[0-9 \t]*continue")
1330 (and fortran-check-all-num-for-matching-do
1331 (looking-at "[ \t]*[0-9]+")))
1332 (fortran-check-for-matching-do)))
1333 (setq count (1+ count)))))
1334 ;; Includes an un-named main program block.
1335 (if (> count 0) (error "Missing block start"))))
1336
1337 \f
1338 (defun fortran-blink-match (regex keyword find-begin)
1339 "From a line matching REGEX, blink matching KEYWORD statement line.
1340 Use function FIND-BEGIN to match it."
1341 (let ((top-of-window (window-start))
1342 (end-point (point))
1343 (case-fold-search t)
1344 matching
1345 message)
1346 (when (save-excursion
1347 (beginning-of-line)
1348 (skip-chars-forward " \t0-9")
1349 (looking-at regex))
1350 (if (not (setq matching (funcall find-begin)))
1351 (setq message (concat "No matching " keyword "."))
1352 (if (< matching top-of-window)
1353 (save-excursion
1354 (goto-char matching)
1355 (beginning-of-line)
1356 (setq message
1357 (concat "Matches "
1358 (buffer-substring (point)
1359 (line-end-position)))))))
1360 (if message
1361 (message "%s" message)
1362 (goto-char matching)
1363 (sit-for blink-matching-delay)
1364 (goto-char end-point)))))
1365
1366 (defun fortran-blink-matching-if ()
1367 "From an ENDIF or ELSE statement, blink the matching IF statement."
1368 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1369 "if" #'fortran-beginning-if))
1370
1371 (defun fortran-blink-matching-do ()
1372 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1373 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
1374
1375 (defun fortran-mark-do ()
1376 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1377 The marks are pushed."
1378 (interactive)
1379 (let (enddo-point do-point)
1380 (if (setq enddo-point (fortran-end-do))
1381 (if (not (setq do-point (fortran-beginning-do)))
1382 (message "No matching do.")
1383 (goto-char enddo-point)
1384 (push-mark)
1385 (goto-char do-point)))))
1386
1387 (defun fortran-end-do ()
1388 "Search forward for first unmatched ENDDO.
1389 Return point or nil."
1390 (let ((case-fold-search t))
1391 (if (save-excursion (beginning-of-line)
1392 (skip-chars-forward " \t0-9")
1393 (looking-at "end[ \t]*do\\b"))
1394 ;; Sitting on one.
1395 (match-beginning 0)
1396 ;; Search for one.
1397 (save-excursion
1398 (let ((count 1))
1399 (while (and (not (zerop count))
1400 (not (eq (fortran-next-statement) 'last-statement))
1401 ;; Keep local to subprogram.
1402 (not (and (looking-at fortran-end-prog-re)
1403 (fortran-check-end-prog-re))))
1404 (skip-chars-forward " \t0-9")
1405 (cond ((looking-at "end[ \t]*do\\b")
1406 (setq count (1- count)))
1407 ((looking-at
1408 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1409 (setq count (1+ count)))))
1410 (and (zerop count)
1411 ;; All pairs accounted for.
1412 (point)))))))
1413
1414 (defun fortran-beginning-do ()
1415 "Search backwards for first unmatched DO [WHILE].
1416 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1417 (let ((case-fold-search t)
1418 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1419 (if (save-excursion
1420 (beginning-of-line)
1421 (skip-chars-forward " \t0-9")
1422 (looking-at dostart-re))
1423 ;; Sitting on one.
1424 (match-beginning 0)
1425 ;; Search for one.
1426 (save-excursion
1427 (let ((count 1))
1428 (while (and (not (zerop count))
1429 (not (eq (fortran-previous-statement) 'first-statement))
1430 ;; Keep local to subprogram.
1431 (not (and (looking-at fortran-end-prog-re)
1432 (fortran-check-end-prog-re))))
1433 (skip-chars-forward " \t0-9")
1434 (cond ((looking-at dostart-re)
1435 (setq count (1- count)))
1436 ;; Note labelled loop ends not considered.
1437 ((looking-at "end[ \t]*do\\b")
1438 (setq count (1+ count)))))
1439 (and (zerop count)
1440 ;; All pairs accounted for.
1441 (point)))))))
1442
1443 (defun fortran-mark-if ()
1444 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1445 The marks are pushed."
1446 (interactive)
1447 (let (endif-point if-point)
1448 (if (setq endif-point (fortran-end-if))
1449 (if (not (setq if-point (fortran-beginning-if)))
1450 (message "No matching if.")
1451 ;; Set mark, move point.
1452 (goto-char endif-point)
1453 (push-mark)
1454 (goto-char if-point)))))
1455
1456 (defun fortran-end-if ()
1457 "Search forwards for first unmatched ENDIF.
1458 Return point or nil."
1459 (let ((case-fold-search t))
1460 (if (save-excursion (beginning-of-line)
1461 (skip-chars-forward " \t0-9")
1462 (looking-at "end[ \t]*if\\b"))
1463 ;; Sitting on one.
1464 (match-beginning 0)
1465 ;; Search for one. The point has been already been moved to first
1466 ;; letter on line but this should not cause troubles.
1467 (save-excursion
1468 (let ((count 1))
1469 (while (and (not (zerop count))
1470 (not (eq (fortran-next-statement) 'last-statement))
1471 ;; Keep local to subprogram.
1472 (not (and (looking-at fortran-end-prog-re)
1473 (fortran-check-end-prog-re))))
1474 (skip-chars-forward " \t0-9")
1475 (cond ((looking-at "end[ \t]*if\\b")
1476 (setq count (1- count)))
1477 ((looking-at fortran-if-start-re)
1478 (save-excursion
1479 (if (or
1480 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1481 (let (then-test) ; multi-line if-then
1482 (while
1483 (and
1484 (zerop (forward-line 1))
1485 ;; Search forward for then.
1486 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1487 (not
1488 (setq then-test
1489 (looking-at
1490 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1491 then-test))
1492 (setq count (1+ count)))))))
1493 (and (zerop count)
1494 ;; All pairs accounted for.
1495 (point)))))))
1496
1497 (defun fortran-beginning-if ()
1498 "Search backwards for first unmatched IF-THEN.
1499 Return point or nil."
1500 (let ((case-fold-search t))
1501 (if (save-excursion
1502 ;; May be sitting on multi-line if-then statement, first
1503 ;; move to beginning of current statement. Note:
1504 ;; `fortran-previous-statement' moves to previous statement
1505 ;; *unless* current statement is first one. Only move
1506 ;; forward if not first-statement.
1507 (if (not (eq (fortran-previous-statement) 'first-statement))
1508 (fortran-next-statement))
1509 (skip-chars-forward " \t0-9")
1510 (and
1511 (looking-at fortran-if-start-re)
1512 (save-match-data
1513 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1514 ;; Multi-line if-then.
1515 (let (then-test)
1516 (while
1517 (and (zerop (forward-line 1))
1518 ;; Search forward for then.
1519 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1520 (not
1521 (setq then-test
1522 (looking-at
1523 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1524 then-test)))))
1525 ;; Sitting on one.
1526 (match-beginning 0)
1527 ;; Search for one.
1528 (save-excursion
1529 (let ((count 1))
1530 (while (and (not (zerop count))
1531 (not (eq (fortran-previous-statement) 'first-statement))
1532 ;; Keep local to subprogram.
1533 (not (and (looking-at fortran-end-prog-re)
1534 (fortran-check-end-prog-re))))
1535 (skip-chars-forward " \t0-9")
1536 (cond ((looking-at fortran-if-start-re)
1537 (save-excursion
1538 (if (or
1539 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1540 (let (then-test) ; multi-line if-then
1541 (while
1542 (and
1543 (zerop (forward-line 1))
1544 ;; Search forward for then.
1545 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1546 (not
1547 (setq then-test
1548 (looking-at
1549 (concat ".*then\\b[ \t]*"
1550 "[^ \t(=a-z0-9]"))))))
1551 then-test))
1552 (setq count (1- count)))))
1553 ((looking-at "end[ \t]*if\\b")
1554 (setq count (1+ count)))))
1555 (and (zerop count)
1556 ;; All pairs accounted for.
1557 (point)))))))
1558
1559 \f
1560 (defun fortran-indent-line ()
1561 "Indent current Fortran line based on its contents and on previous lines."
1562 (interactive "*")
1563 (let ((cfi (fortran-calculate-indent)))
1564 (save-excursion
1565 (beginning-of-line)
1566 (if (or (not (= cfi (fortran-current-line-indentation)))
1567 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1568 (not (fortran-line-number-indented-correctly-p))))
1569 (fortran-indent-to-column cfi)
1570 (beginning-of-line)
1571 (if (fortran-find-comment-start-skip)
1572 (fortran-indent-comment))))
1573 ;; Never leave point in left margin.
1574 (if (< (current-column) cfi)
1575 (move-to-column cfi))
1576 (and auto-fill-function
1577 (> (save-excursion (end-of-line) (current-column))
1578 fill-column)
1579 (save-excursion
1580 (end-of-line)
1581 (fortran-fill)))
1582 (when fortran-blink-matching-if
1583 (fortran-blink-matching-if)
1584 (fortran-blink-matching-do))))
1585
1586 (defun fortran-auto-fill ()
1587 "Function to use for `normal-auto-fill-function' in Fortran mode."
1588 (if (> (current-column) (current-fill-column))
1589 (let ((cfi (fortran-calculate-indent)))
1590 (save-excursion
1591 (beginning-of-line)
1592 (if (or (not (= cfi (fortran-current-line-indentation)))
1593 (and (re-search-forward "^[ \t]*[0-9]+"
1594 (+ (point) 4) t)
1595 (not (fortran-line-number-indented-correctly-p))))
1596 (fortran-indent-to-column cfi)
1597 (beginning-of-line)
1598 (if (fortran-find-comment-start-skip)
1599 (fortran-indent-comment))))
1600 (fortran-fill)
1601 ;; Never leave point in left margin.
1602 (if (< (current-column) cfi)
1603 (move-to-column cfi)))))
1604
1605 ;; Historically this was a separate function which advertised itself
1606 ;; as reindenting but only did so where `most likely to be necessary'.
1607 (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
1608
1609 (defun fortran-indent-subprogram ()
1610 "Properly indent the Fortran subprogram containing point."
1611 (interactive "*")
1612 (save-excursion
1613 (mark-defun)
1614 (message "Indenting subprogram...")
1615 (indent-region (point) (mark) nil))
1616 (message "Indenting subprogram...done."))
1617
1618 (defun fortran-calculate-indent ()
1619 "Calculates the Fortran indent column based on previous lines."
1620 (let (icol first-statement (case-fold-search t)
1621 (fortran-minimum-statement-indent
1622 (if indent-tabs-mode
1623 fortran-minimum-statement-indent-tab
1624 fortran-minimum-statement-indent-fixed)))
1625 (save-excursion
1626 (setq first-statement (fortran-previous-statement))
1627 (if first-statement
1628 (setq icol fortran-minimum-statement-indent)
1629 (if (= (point) (point-min))
1630 (setq icol fortran-minimum-statement-indent)
1631 (setq icol (fortran-current-line-indentation)))
1632 (skip-chars-forward " \t0-9")
1633 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1634 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1635 (let (then-test) ; multi-line if-then
1636 (while (and (zerop (forward-line 1))
1637 ;; Search forward for then.
1638 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1639 (not (setq then-test
1640 (looking-at
1641 ".*then\\b[ \t]\
1642 *[^ \t_$(=a-z0-9]")))))
1643 then-test))
1644 (setq icol (+ icol fortran-if-indent))))
1645 ((looking-at "else\\(if\\)?\\b")
1646 (setq icol (+ icol fortran-if-indent)))
1647 ((looking-at "select[ \t]*case[ \t](.*)")
1648 (setq icol (+ icol fortran-if-indent)))
1649 ((looking-at "case[ \t]*(.*)")
1650 (setq icol (+ icol fortran-if-indent)))
1651 ((looking-at "case[ \t]*default\\b")
1652 (setq icol (+ icol fortran-if-indent)))
1653 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1654 (setq icol (+ icol fortran-if-indent)))
1655 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1656 (setq icol (+ icol fortran-if-indent)))
1657 ((looking-at "do\\b")
1658 (setq icol (+ icol fortran-do-indent)))
1659 ((looking-at
1660 "\\(structure\\|union\\|map\\|interface\\)\
1661 \\b[ \t]*[^ \t=(a-z]")
1662 (setq icol (+ icol fortran-structure-indent)))
1663 ((and (looking-at fortran-end-prog-re1)
1664 (fortran-check-end-prog-re))
1665 ;; Previous END resets indent to minimum.
1666 (setq icol fortran-minimum-statement-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 ((and fortran-check-all-num-for-matching-do
1692 (looking-at "[ \t]*[0-9]+")
1693 (fortran-check-for-matching-do))
1694 (setq icol (- icol fortran-do-indent)))
1695 (t
1696 (skip-chars-forward " \t0-9")
1697 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1698 (setq icol (- icol fortran-if-indent)))
1699 ((looking-at "else\\(if\\)?\\b")
1700 (setq icol (- icol fortran-if-indent)))
1701 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1702 (setq icol (- icol fortran-if-indent)))
1703 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1704 (setq icol (- icol fortran-if-indent)))
1705 ((and (looking-at "continue\\b")
1706 (fortran-check-for-matching-do))
1707 (setq icol (- icol fortran-do-indent)))
1708 ((looking-at "end[ \t]*do\\b")
1709 (setq icol (- icol fortran-do-indent)))
1710 ((looking-at "end[ \t]*\
1711 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1712 (setq icol (- icol fortran-structure-indent)))
1713 ((and (looking-at fortran-end-prog-re1)
1714 (fortran-check-end-prog-re)
1715 (not (= icol fortran-minimum-statement-indent)))
1716 (message "Warning: `end' not in column %d. Probably\
1717 an unclosed block." fortran-minimum-statement-indent))))))
1718 (max fortran-minimum-statement-indent icol)))
1719
1720 \f
1721 (defun fortran-current-line-indentation ()
1722 "Indentation of current line, ignoring Fortran line number or continuation.
1723 This is the column position of the first non-whitespace character
1724 aside from the line number and/or column 5/8 line-continuation character.
1725 For comment lines, returns indentation of the first
1726 non-indentation text within the comment."
1727 (save-excursion
1728 (beginning-of-line)
1729 (cond ((looking-at fortran-comment-line-start-skip)
1730 (goto-char (match-end 0))
1731 (skip-chars-forward
1732 (if (stringp fortran-comment-indent-char)
1733 fortran-comment-indent-char
1734 (char-to-string fortran-comment-indent-char))))
1735 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1736 (goto-char (match-end 0)))
1737 (t
1738 ;; Move past line number.
1739 (skip-chars-forward "[ \t0-9]")))
1740 ;; Move past whitespace.
1741 (skip-chars-forward " \t")
1742 (current-column)))
1743
1744 (defun fortran-indent-to-column (col)
1745 "Indent current line to column COL.
1746 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1747 line, and this continuation character is retained on indentation;
1748 2) If `fortran-continuation-string' is the first non-whitespace
1749 character, this is a continuation line;
1750 3) A non-continuation line which has a number as the first
1751 non-whitespace character is a numbered line.
1752 4) A TAB followed by a digit indicates a continuation line."
1753 (save-excursion
1754 (beginning-of-line)
1755 (if (looking-at fortran-comment-line-start-skip)
1756 (if fortran-comment-indent-style
1757 (let* ((char (if (stringp fortran-comment-indent-char)
1758 (aref fortran-comment-indent-char 0)
1759 fortran-comment-indent-char))
1760 (chars (string ?\s ?\t char)))
1761 (goto-char (match-end 0))
1762 (skip-chars-backward chars)
1763 (delete-region (point) (progn (skip-chars-forward chars)
1764 (point)))
1765 (insert-char char (- col (current-column)))))
1766 (if (looking-at "\t[1-9]")
1767 (if indent-tabs-mode
1768 (goto-char (match-end 0))
1769 (delete-char 2)
1770 (insert-char ?\s 5)
1771 (insert fortran-continuation-string))
1772 (if (looking-at " \\{5\\}[^ 0\n]")
1773 (if indent-tabs-mode
1774 (progn (delete-char 6)
1775 (insert ?\t (fortran-numerical-continuation-char) 1))
1776 (forward-char 6))
1777 (delete-horizontal-space)
1778 ;; Put line number in columns 0-4, or
1779 ;; continuation character in column 5.
1780 (cond ((eobp))
1781 ((looking-at (regexp-quote fortran-continuation-string))
1782 (if indent-tabs-mode
1783 (progn
1784 (indent-to
1785 (if indent-tabs-mode
1786 fortran-minimum-statement-indent-tab
1787 fortran-minimum-statement-indent-fixed))
1788 (delete-char 1)
1789 (insert-char (fortran-numerical-continuation-char) 1))
1790 (indent-to 5)
1791 (forward-char 1)))
1792 ((looking-at "[0-9]+")
1793 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1794 (if (< extra-space 0)
1795 (message "Warning: line number exceeds 5-digit limit.")
1796 (indent-to (min fortran-line-number-indent extra-space))))
1797 (skip-chars-forward "0-9")))))
1798 ;; Point is now after any continuation character or line number.
1799 ;; Put body of statement where specified.
1800 (delete-horizontal-space)
1801 (indent-to col)
1802 ;; Indent any comment following code on the same line.
1803 (when (fortran-find-comment-start-skip)
1804 (goto-char (match-beginning 0))
1805 (unless (= (current-column) (fortran-comment-indent))
1806 (delete-horizontal-space)
1807 (indent-to (fortran-comment-indent)))))))
1808
1809 (defun fortran-line-number-indented-correctly-p ()
1810 "Return t if current line's line number is correctly indented.
1811 Do not call if there is no line number."
1812 (save-excursion
1813 (beginning-of-line)
1814 (skip-chars-forward " \t")
1815 (and (<= (current-column) fortran-line-number-indent)
1816 (or (= (current-column) fortran-line-number-indent)
1817 (progn (skip-chars-forward "0-9")
1818 (= (current-column) 5))))))
1819
1820 (defun fortran-check-for-matching-do ()
1821 "When called from a numbered statement, return t if matching DO is found.
1822 Otherwise return nil."
1823 (let ((case-fold-search t)
1824 charnum)
1825 (save-excursion
1826 (beginning-of-line)
1827 (when (looking-at "[ \t]*[0-9]+")
1828 (skip-chars-forward " \t")
1829 (skip-chars-forward "0") ; skip past leading zeros
1830 (setq charnum
1831 (buffer-substring (point) (progn
1832 (skip-chars-forward "0-9")
1833 (point))))
1834 (beginning-of-line)
1835 (save-restriction
1836 (save-excursion
1837 (narrow-to-defun)
1838 (and (re-search-backward
1839 (concat
1840 "\\(^[ \t0-9]*do[ \t]*0*"
1841 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1842 charnum "\\b\\)")
1843 nil t)
1844 (looking-at
1845 (concat "^[ \t0-9]*do[ \t]*0*"
1846 charnum)))))))))
1847
1848 (defun fortran-find-comment-start-skip (&optional all)
1849 "Move to past `comment-start-skip' found on current line.
1850 Return non-nil if `comment-start-skip' found, nil if not.
1851 If ALL is nil, only match comments that start in column > 0."
1852 ;; Hopefully at some point we can just use the line below! -stef
1853 ;; (comment-search-forward (line-end-position) t))
1854 (when (or all comment-start-skip)
1855 (let ((pos (point))
1856 (css (if comment-start-skip
1857 (concat fortran-comment-line-start-skip
1858 "\\|" comment-start-skip)
1859 fortran-comment-line-start-skip)))
1860 (when (re-search-forward css (line-end-position) t)
1861 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1862 (or (save-match-data
1863 (not (fortran-is-in-string-p (match-beginning 0))))
1864 ;; Recurse for rest of line.
1865 (fortran-find-comment-start-skip all)))
1866 (point)
1867 (goto-char pos)
1868 nil)))))
1869
1870 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1871 ;; Test if TAB format continuation lines work.
1872 (defun fortran-is-in-string-p (where)
1873 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
1874 (save-excursion
1875 (goto-char where)
1876 (cond
1877 ((bolp) nil) ; bol is never inside a string
1878 ((save-excursion ; comment lines too
1879 (beginning-of-line)
1880 (looking-at fortran-comment-line-start-skip)) nil)
1881 (t (let ((parse-state '(0 nil nil nil nil nil 0))
1882 (quoted-comment-start (if comment-start
1883 (regexp-quote comment-start)))
1884 (not-done t)
1885 parse-limit end-of-line)
1886 ;; Move to start of current statement.
1887 (fortran-next-statement)
1888 (fortran-previous-statement)
1889 ;; Now parse up to WHERE.
1890 (while not-done
1891 (if (or ;; Skip to next line if:
1892 ;; - comment line?
1893 (looking-at fortran-comment-line-start-skip)
1894 ;; - at end of line?
1895 (eolp)
1896 ;; - not in a string and after comment-start?
1897 (and (not (nth 3 parse-state))
1898 comment-start
1899 (equal comment-start
1900 (char-to-string (preceding-char)))))
1901 (if (> (forward-line) 0)
1902 (setq not-done nil))
1903 ;; else:
1904 ;; If we are at beginning of code line, skip any
1905 ;; whitespace, labels and tab continuation markers.
1906 (if (bolp) (skip-chars-forward " \t0-9"))
1907 ;; If we are in column <= 5 now, check for continuation char.
1908 (cond ((= 5 (current-column)) (forward-char 1))
1909 ((and (< (current-column) 5)
1910 (equal fortran-continuation-string
1911 (char-to-string (following-char)))
1912 (forward-char 1))))
1913 ;; Find out parse-limit from here.
1914 (setq end-of-line (line-end-position))
1915 (setq parse-limit (min where end-of-line))
1916 ;; Parse max up to comment-start, if non-nil and in current line.
1917 (if comment-start
1918 (save-excursion
1919 (if (re-search-forward quoted-comment-start end-of-line t)
1920 (setq parse-limit (min (point) parse-limit)))))
1921 ;; Now parse if still in limits.
1922 (if (< (point) where)
1923 (setq parse-state (parse-partial-sexp
1924 (point) parse-limit nil nil parse-state))
1925 (setq not-done nil))))
1926 ;; Result.
1927 (nth 3 parse-state))))))
1928
1929 ;; From old version.
1930 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1931
1932 (defun fortran-fill ()
1933 "Fill the current line at an appropriate point(s)."
1934 (let* ((auto-fill-function #'fortran-auto-fill)
1935 (opoint (point))
1936 (bol (line-beginning-position))
1937 (eol (line-end-position))
1938 (bos (min eol (+ bol (fortran-current-line-indentation))))
1939 ;; If in a string at fill-column, break it either before the
1940 ;; initial quote, or at fill-col (if string is too long).
1941 (quote
1942 (save-excursion
1943 (goto-char bol)
1944 ;; OK to break quotes on comment lines.
1945 (unless (looking-at fortran-comment-line-start-skip)
1946 (let (fcpoint start)
1947 (move-to-column fill-column)
1948 (when (fortran-is-in-string-p (setq fcpoint (point)))
1949 (save-excursion
1950 (re-search-backward "\\S\"\\s\"\\S\"?" bol t)
1951 (setq start
1952 (if fortran-break-before-delimiters
1953 (point)
1954 (1+ (point)))))
1955 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t)
1956 (backward-char 2))
1957 ;; If the current string is longer than (fill-column
1958 ;; - 6) chars, break it at the fill column (else
1959 ;; infinite loop).
1960 (if (> (- (point) start)
1961 (- fill-column 6 fortran-continuation-indent))
1962 fcpoint
1963 start))))))
1964 ;; Decide where to split the line. If a position for a quoted
1965 ;; string was found above then use that, else break the line
1966 ;; before/after the last delimiter.
1967 (fill-point
1968 (or quote
1969 (save-excursion
1970 ;; If f-b-b-d is t, have an extra column to play with,
1971 ;; since delimiter gets shifted to new line.
1972 (move-to-column (if fortran-break-before-delimiters
1973 (1+ fill-column)
1974 fill-column))
1975 (let ((repeat t))
1976 (while repeat
1977 (setq repeat nil)
1978 ;; Adapted from f90-find-breakpoint.
1979 (re-search-backward fortran-break-delimiters-re bol)
1980 (if (not fortran-break-before-delimiters)
1981 (if (looking-at fortran-no-break-re)
1982 ;; Deal with cases such as "**" split over
1983 ;; fill-col. Simpler alternative would be
1984 ;; to start from (1- fill-column) above.
1985 (if (> (+ 2 (current-column)) fill-column)
1986 (setq repeat t)
1987 (forward-char 2))
1988 (forward-char 1))
1989 (backward-char)
1990 (or (looking-at fortran-no-break-re)
1991 (forward-char)))))
1992 ;; Line indented beyond fill-column?
1993 (when (<= (point) bos)
1994 (move-to-column (1+ fill-column))
1995 ;; What is this doing???
1996 (or (re-search-forward "[\t\n,'+-/*)=]" eol t)
1997 (goto-char bol)))
1998 (if (bolp)
1999 (re-search-forward "[ \t]" opoint t))
2000 (point)))))
2001 ;; If we are in an in-line comment, don't break unless the
2002 ;; line of code is longer than it should be. Otherwise
2003 ;; break the line at the column computed above.
2004 ;;
2005 ;; Need to use fortran-find-comment-start-skip to make sure that
2006 ;; quoted !'s don't prevent a break.
2007 (when (and (save-excursion
2008 (beginning-of-line)
2009 (if (not (fortran-find-comment-start-skip))
2010 t
2011 (goto-char (match-beginning 0))
2012 (>= (point) fill-point)))
2013 (save-excursion
2014 (goto-char fill-point)
2015 (not (bolp)))
2016 (> (save-excursion
2017 (goto-char opoint)
2018 (current-column))
2019 (min (1+ fill-column)
2020 (+ (fortran-calculate-indent)
2021 fortran-continuation-indent))))
2022 (goto-char fill-point)
2023 (fortran-break-line)
2024 (end-of-line))))
2025
2026 (defun fortran-break-line ()
2027 "Call `fortran-split-line'. Joins continuation lines first, then refills."
2028 (let ((bol (line-beginning-position))
2029 (comment-string
2030 (save-excursion
2031 (if (fortran-find-comment-start-skip)
2032 (delete-and-extract-region
2033 (match-beginning 0) (line-end-position))))))
2034 ;; Forward line 1 really needs to go to next non white line.
2035 (if (save-excursion (forward-line)
2036 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
2037 (progn
2038 (end-of-line)
2039 (delete-region (point) (match-end 0))
2040 (delete-horizontal-space)
2041 (fortran-fill))
2042 (fortran-split-line))
2043 (if comment-string
2044 (save-excursion
2045 (goto-char bol)
2046 (end-of-line)
2047 (delete-horizontal-space)
2048 (indent-to (fortran-comment-indent))
2049 (insert comment-string)))))
2050
2051 (defun fortran-analyze-file-format ()
2052 "Return nil if fixed format is used, t if TAB formatting is used.
2053 Use `fortran-tab-mode-default' if no non-comment statements are found
2054 before the end or in the first `fortran-analyze-depth' lines."
2055 (let ((i 0))
2056 (save-excursion
2057 (goto-char (point-min))
2058 (while (not (or
2059 (eobp)
2060 (eq (char-after) ?\t)
2061 (looking-at " \\{6\\}")
2062 (> i fortran-analyze-depth)))
2063 (forward-line)
2064 (setq i (1+ i)))
2065 (cond
2066 ((eq (char-after) ?\t) t)
2067 ((looking-at " \\{6\\}") nil)
2068 (t fortran-tab-mode-default)))))
2069
2070 (defun fortran-fill-paragraph (&optional justify)
2071 "Fill surrounding comment block as paragraphs, else fill statement.
2072 Intended as the value of `fill-paragraph-function'.
2073 A comment block is filled by calling `fill-comment-paragraph' with
2074 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
2075 Always returns non-nil (to prevent `fill-paragraph' being called)."
2076 (interactive "*P")
2077 (or (fill-comment-paragraph justify)
2078 (fortran-fill-statement)
2079 t))
2080
2081 (defun fortran-fill-statement ()
2082 "Fill a Fortran statement up to `fill-column'."
2083 (interactive "*")
2084 (let ((auto-fill-function #'fortran-auto-fill))
2085 (unless (save-excursion
2086 (beginning-of-line)
2087 (or (looking-at "[ \t]*$")
2088 (looking-at fortran-comment-line-start-skip)
2089 (and comment-start-skip
2090 (looking-at (concat "[ \t]*" comment-start-skip)))))
2091 (save-excursion
2092 ;; Find beginning of statement.
2093 (fortran-next-statement)
2094 (fortran-previous-statement)
2095 ;; Re-indent initially.
2096 (fortran-indent-line)
2097 ;; Replace newline plus continuation field plus indentation with
2098 ;; single space.
2099 (while (progn
2100 (forward-line)
2101 (fortran-remove-continuation)))
2102 (fortran-previous-statement)))
2103 (fortran-indent-line)))
2104
2105 (defun fortran-strip-sequence-nos (&optional do-space)
2106 "Delete all text in column `fortran-line-length' (default 72) and up.
2107 This is assumed to be sequence numbers. Normally also deletes
2108 trailing whitespace after stripping such text. Supplying prefix
2109 arg DO-SPACE prevents stripping the whitespace."
2110 (interactive "*p")
2111 (save-excursion
2112 (goto-char (point-min))
2113 (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length)
2114 nil t)
2115 (replace-match "" nil nil nil 1)
2116 (unless do-space (delete-horizontal-space)))))
2117
2118 ;; This code used to live in add-log.el, but this is a better place
2119 ;; for it.
2120 (defun fortran-current-defun ()
2121 "Function to use for `add-log-current-defun-function' in Fortran mode."
2122 (save-excursion
2123 ;; We must be inside function body for this to work.
2124 (fortran-beginning-of-subprogram)
2125 (let ((case-fold-search t)) ; case-insensitive
2126 ;; Search for fortran subprogram start.
2127 (if (re-search-forward
2128 (concat "^[ \t]*\\(program\\|subroutine\\|function"
2129 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
2130 "\\(block[ \t]*data\\)\\)")
2131 (save-excursion (fortran-end-of-subprogram)
2132 (point))
2133 t)
2134 (or (match-string-no-properties 2)
2135 (progn
2136 ;; Move to EOL or before first left paren.
2137 (if (re-search-forward "[(\n]" nil t)
2138 (progn (backward-char)
2139 (skip-chars-backward " \t"))
2140 (end-of-line))
2141 ;; Use the name preceding that.
2142 (buffer-substring-no-properties (point) (progn (backward-sexp)
2143 (point)))))
2144 "main"))))
2145
2146 (provide 'fortran)
2147
2148 ;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
2149 ;;; fortran.el ends here