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