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