Refill some long/short copyright headers.
[bpt/emacs.git] / lisp / progmodes / fortran.el
CommitLineData
e5167999
ER
1;;; fortran.el --- Fortran mode for GNU Emacs
2
95df8112 3;; Copyright (C) 1986, 1993-1995, 1997-2011 Free Software Foundation, Inc.
9750e079 4
e5167999 5;; Author: Michael D. Prange <prange@erl.mit.edu>
aff88519 6;; Maintainer: Glenn Morris <rgm@gnu.org>
5b04210c 7;; Keywords: fortran, languages
1a06eabd 8
e5167999
ER
9;; This file is part of GNU Emacs.
10
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
e5167999 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
e5167999
ER
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
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
e5167999
ER
23
24;;; Commentary:
25
7977773d
DL
26;; This mode is documented in the Emacs manual.
27;;
28;; Note that it is for editing Fortran77 or Fortran90 fixed source
45cf60ae 29;; form. For editing Fortran 90 free format source, use `f90-mode'
b2523604
DL
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).
7977773d
DL
33
34;;; History:
35
36;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
23029d77
JB
37
38;; We acknowledge many contributions and valuable suggestions by
b8cbdf43 39;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
60db3594 40;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
b8cbdf43
RS
41;; Gary Sabot and Richard Stallman.
42
e5167999
ER
43;;; Code:
44
45cf60ae 45;; Todo:
1eb6bf70 46
315aa1de 47;; * Tidy it all up (more)!
1eb6bf70
DL
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.
b2523604 51;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
1eb6bf70 52
5d724235
SM
53;; silence compiler
54(defvar dabbrev-case-fold-search)
5d724235
SM
55(defvar gud-find-expr-function)
56(defvar imenu-case-fold-search)
57(defvar imenu-syntax-alist)
e83d1fe8
DN
58(defvar comment-region-function)
59(defvar uncomment-region-function)
5b04210c 60
fcad5199 61(defgroup fortran nil
5b04210c 62 "Major mode for editing fixed format Fortran code."
8ec3bce0
JL
63 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
64 :link '(custom-manual "(emacs)Fortran")
fcad5199
RS
65 :group 'languages)
66
67(defgroup fortran-indent nil
5b04210c 68 "Indentation variables in Fortran mode."
fcad5199 69 :prefix "fortran-"
5b04210c 70 :group 'fortran)
fcad5199
RS
71
72(defgroup fortran-comment nil
5b04210c 73 "Comment-handling variables in Fortran mode."
fcad5199 74 :prefix "fortran-"
5b04210c 75 :group 'fortran)
fcad5199
RS
76
77
fcad5199 78(defcustom fortran-tab-mode-default nil
694cf05e 79 "Default tabbing/carriage control style for empty files in Fortran mode.
5b04210c 80A non-nil value specifies tab-digit style of continuation control.
e80f2147 81A value of nil specifies that continuation lines are marked
fcad5199 82with a character in column 6."
5b04210c 83 :type 'boolean
2fa20711 84 :safe 'booleanp
fcad5199 85 :group 'fortran-indent)
e80f2147 86
694cf05e
GM
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))))
cd9b5d3e
GM
97 "String to appear in mode line in TAB format buffers.
98See Info node `(emacs)ForIndent Cont'."
5b04210c 99 :type 'string
2fa20711 100 :risky t
fcad5199 101 :group 'fortran-indent)
694cf05e 102
fcad5199 103(defcustom fortran-do-indent 3
694cf05e 104 "Extra indentation applied to DO blocks."
5b04210c 105 :type 'integer
2fa20711 106 :safe 'integerp
fcad5199
RS
107 :group 'fortran-indent)
108
109(defcustom fortran-if-indent 3
694cf05e 110 "Extra indentation applied to IF, SELECT CASE and WHERE blocks."
5b04210c 111 :type 'integer
2fa20711 112 :safe 'integerp
fcad5199
RS
113 :group 'fortran-indent)
114
115(defcustom fortran-structure-indent 3
694cf05e 116 "Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
5b04210c 117 :type 'integer
2fa20711 118 :safe 'integerp
fcad5199
RS
119 :group 'fortran-indent)
120
121(defcustom fortran-continuation-indent 5
694cf05e 122 "Extra indentation applied to continuation lines."
5b04210c 123 :type 'integer
2fa20711 124 :safe 'integerp
fcad5199
RS
125 :group 'fortran-indent)
126
127(defcustom fortran-comment-indent-style 'fixed
694cf05e 128 "How to indent comments.
5b04210c
GM
129nil 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);
ff451e17
SM
133`relative' indents to current Fortran indentation plus
134 `fortran-comment-line-extra-indent'."
5b04210c 135 :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative))
2fa20711 136 :safe (lambda (value) (memq value '(nil fixed relative)))
fcad5199
RS
137 :group 'fortran-indent)
138
139(defcustom fortran-comment-line-extra-indent 0
694cf05e 140 "Amount of extra indentation for text within full-line comments."
5b04210c 141 :type 'integer
2fa20711 142 :safe 'integerp
fcad5199
RS
143 :group 'fortran-indent
144 :group 'fortran-comment)
145
68ca306c 146(defcustom fortran-comment-line-start "C"
694cf05e 147 "Delimiter inserted to start new full-line comment.
cd9b5d3e
GM
148You might want to change this to \"*\", for instance; or \"!\" to
149allow trailing comments on a line."
7dae727d 150 :version "21.1"
5b04210c 151 :type 'string
2fa20711 152 :safe 'stringp
5b04210c 153 :group 'fortran-comment)
fcad5199 154
68ca306c
DL
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
8b31236d 158 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
a227f163 159 "Regexp to match the start of a full-line comment."
7dae727d 160 :version "21.1"
5b04210c 161 :type 'regexp
2fa20711 162 :safe 'stringp
5b04210c 163 :group 'fortran-comment)
fcad5199 164
5a73972b 165(defcustom fortran-directive-re
8edfcc7d 166 "^[ \t]*#.*"
694cf05e 167 "Regexp to match a directive line.
5a73972b
GM
168The matching text will be fontified with `font-lock-keyword-face'.
169The matching line will be given zero indentation."
bf247b6e 170 :version "22.1"
5b04210c 171 :type 'regexp
2fa20711 172 :safe 'stringp
5b04210c 173 :group 'fortran-indent)
8edfcc7d 174
fcad5199 175(defcustom fortran-minimum-statement-indent-fixed 6
694cf05e 176 "Minimum statement indentation for fixed format continuation style."
5b04210c 177 :type 'integer
2fa20711 178 :safe 'integerp
fcad5199
RS
179 :group 'fortran-indent)
180
181(defcustom fortran-minimum-statement-indent-tab (max tab-width 6)
694cf05e 182 "Minimum statement indentation for TAB format continuation style."
5b04210c 183 :type 'integer
2fa20711 184 :safe 'integerp
fcad5199 185 :group 'fortran-indent)
3dd63760
JB
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.
fcad5199 190(defcustom fortran-comment-indent-char " "
694cf05e 191 "Single-character string inserted for Fortran comment indentation.
fcad5199 192Normally a space."
5b04210c 193 :type 'string
2fa20711
GM
194 :safe (lambda (value) (or (characterp value)
195 (and (stringp value) (= (length value) 1))))
fcad5199 196 :group 'fortran-comment)
3dd63760 197
fcad5199 198(defcustom fortran-line-number-indent 1
694cf05e 199 "Maximum indentation for Fortran line numbers.
fcad5199 2005 means right-justify them within their five-column field."
5b04210c 201 :type 'integer
2fa20711 202 :safe 'integerp
fcad5199 203 :group 'fortran-indent)
3dd63760 204
fcad5199 205(defcustom fortran-check-all-num-for-matching-do nil
694cf05e 206 "Non-nil causes all numbered lines to be treated as possible DO loop ends."
5b04210c 207 :type 'boolean
2fa20711 208 :safe 'booleanp
fcad5199 209 :group 'fortran)
3dd63760 210
fcad5199 211(defcustom fortran-blink-matching-if nil
694cf05e 212 "Non-nil causes \\[fortran-indent-line] on ENDIF to blink on matching IF.
fcad5199 213Also, from an ENDDO statement blink on matching DO [WHILE] statement."
5b04210c 214 :type 'boolean
2fa20711 215 :safe 'booleanp
fcad5199 216 :group 'fortran)
3dd63760 217
fcad5199 218(defcustom fortran-continuation-string "$"
694cf05e 219 "Single-character string used for Fortran continuation lines.
3dd63760
JB
220In fixed format continuation style, this character is inserted in
221column 6 by \\[fortran-split-line] to begin a continuation line.
5b04210c
GM
222Also, if \\[fortran-indent-line] finds this at the beginning of a
223line, it will convert the line into a continuation line of the
cd9b5d3e 224appropriate style. Normally \"$\"."
5b04210c 225 :type 'string
2fa20711 226 :safe (lambda (value) (and (stringp value) (= (length value) 1)))
fcad5199 227 :group 'fortran)
3dd63760 228
fcad5199 229(defcustom fortran-comment-region "c$$$"
694cf05e 230 "String inserted by \\[fortran-comment-region] at start of each \
1eb6bf70 231line in region."
5b04210c 232 :type 'string
2fa20711 233 :safe 'stringp
fcad5199 234 :group 'fortran-comment)
3dd63760 235
fcad5199 236(defcustom fortran-electric-line-number t
694cf05e 237 "Non-nil causes line numbers to be moved to the correct column as typed."
5b04210c 238 :type 'boolean
2fa20711 239 :safe 'booleanp
fcad5199 240 :group 'fortran)
3dd63760 241
694cf05e 242;; TODO use fortran-line-length, somehow.
5b04210c 243(defcustom fortran-column-ruler-fixed
23029d77 244 "0 4 6 10 20 30 40 5\
7977773d 2450 60 70\n\
f022dd89
SM
246\[ ]|{ | | | | | | | | \
247\| | | | |}\n"
dc6579ac 248 "String displayed above current line by \\[fortran-column-ruler].
5b04210c
GM
249This variable is used in fixed format mode.
250See the variable `fortran-column-ruler-tab' for TAB format mode."
251 :type 'string
2fa20711 252 :safe 'stringp
5b04210c 253 :group 'fortran)
23029d77 254
694cf05e 255;; TODO use fortran-line-length, somehow.
5b04210c 256(defcustom fortran-column-ruler-tab
23029d77 257 "0 810 20 30 40 5\
7977773d 2580 60 70\n\
f022dd89
SM
259\[ ]| { | | | | | | | | \
260\| | | | |}\n"
dc6579ac 261 "String displayed above current line by \\[fortran-column-ruler].
5b04210c
GM
262This variable is used in TAB format mode.
263See the variable `fortran-column-ruler-fixed' for fixed format mode."
264 :type 'string
2fa20711 265 :safe 'stringp
5b04210c 266 :group 'fortran)
3dd63760 267
5b04210c
GM
268(defcustom fortran-analyze-depth 100
269 "Number of lines to scan to identify fixed or TAB format style."
270 :type 'integer
2fa20711 271 :safe 'integerp
5b04210c 272 :group 'fortran)
3dd63760 273
fcad5199 274(defcustom fortran-break-before-delimiters t
694cf05e 275 "Non-nil causes filling to break lines before delimiters.
337c50a5 276Delimiters are characters matching the regexp `fortran-break-delimiters-re'."
5b04210c 277 :type 'boolean
2fa20711 278 :safe 'booleanp
fcad5199 279 :group 'fortran)
b8cbdf43 280
694cf05e
GM
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.
284Characters beyond this point are treated as comments. Setting
285this variable directly (after fortran mode is loaded) does not
286take effect. Use either \\[customize] (which affects all Fortran
287buffers and the default) or the function
288`fortran-line-length' (which can also operate on just the current
289buffer). This corresponds to the g77 compiler option
290`-ffixed-line-length-N'."
291 :type 'integer
2fa20711 292 :safe 'integerp
694cf05e
GM
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
694cf05e
GM
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
337c50a5
GM
308(defconst fortran-break-delimiters-re "[-+*/><=, \t]"
309 "Regexp matching delimiter characters at which lines may be broken.
310There are certain tokens comprised entirely of characters
311matching this regexp that should not be split, and these are
312specified 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.
318This regexp matches certain tokens comprised entirely of
319characters matching the regexp `fortran-break-delimiters-re' that should
320not be split by filling. Each element is assumed to be two
321characters long.")
322
694cf05e 323(defconst fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*("
5b04210c
GM
324 "Regexp matching the start of an IF statement.")
325
4a948dbf
GM
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
694cf05e 333(defconst fortran-end-prog-re1
5b04210c
GM
334 "end\
335\\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
336\\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?"
337 "Regexp possibly matching the end of a subprogram.")
563f09b5 338
694cf05e 339(defconst fortran-end-prog-re
5b04210c
GM
340 (concat "^[ \t0-9]*" fortran-end-prog-re1)
341 "Regexp possibly matching the end of a subprogram, from the line start.
342See 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\\|\
372program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?"
373 (1 font-lock-keyword-face)
374 (2 font-lock-function-name-face nil t)))
f3d4eb7b 375 "Subdued level highlighting for Fortran mode.")
563f09b5 376
5b04210c
GM
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"
8cb8832f
GM
389 "cycle" "exit" "rewind" "backspace"
390 "where" "elsewhere")
5b04210c
GM
391 'paren) "\\>")
392 ;; Builtin operators.
393 (concat "\\." (regexp-opt
ac342f28
GM
394 '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
395 "neqv" "not" "or" "true")
5b04210c
GM
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)))
f408b027
SM
402 "Medium level highlighting for Fortran mode.")
403
0a23b2c3 404;; See bug#1385. Never really looked into _why_ this matters...
119850e9
GM
405(defun fortran-match-and-skip-declaration (limit)
406 "Like `font-lock-match-c-style-declaration-item-and-skip-to-next'.
407The 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
5b04210c
GM
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).
119850e9 435 `(fortran-match-and-skip-declaration
5b04210c
GM
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)))
f408b027
SM
459 "Gaudy level highlighting for Fortran mode.")
460
8cb8832f
GM
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"
742695df
GM
472 "min" "min0" "amin1" "dmin1" "amin0" "min1"
473 "len" "index" "lge" "lgt" "lle" "llt" "aimag"
8cb8832f
GM
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.
482Consists of level 3 plus all other intrinsics not already highlighted.")
483
5b04210c
GM
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.
b879a6e2
SM
489(defun fortran-make-syntax-propertize-function (line-length)
490 "Return a value for `syntax-propertize-function' in Fortran mode.
491This varies according to the value of LINE-LENGTH.
694cf05e 492This is used to fontify fixed-format Fortran comments."
b879a6e2
SM
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 ½% 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 "<")))))
0f1057e9 505
f3d4eb7b
SM
506(defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
507 "Default expressions to highlight in Fortran mode.")
5b04210c 508
9645c179 509(defvar fortran-imenu-generic-expression
240e9cda
DL
510 ;; These patterns could be confused by sequence nos. in cols 72+ and
511 ;; don't allow continuations everywhere.
7977773d
DL
512 (list
513 (list
514 nil
a8189dfe
DL
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+\\)")
7977773d 528 3)
5b04210c
GM
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
8cb8832f
GM
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\".
540Omits naked END statements, and DO-loops closed by anything other
541than 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.
551Note that only ENDDO is handled for the end of a DO-loop. Used
552in 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.
583A simple regexp cannot do this in fully correct fashion, so this
584tries to strike a compromise between complexity and flexibility.
585Used in the Fortran entry in `hs-special-modes-alist'.")
586
587(add-to-list 'hs-special-modes-alist
9d23c925 588 `(fortran-mode ,fortran-start-block-re ,fortran-end-block-re
8cb8832f
GM
589 "^[cC*!]" fortran-end-of-block nil))
590
591\f
5b04210c
GM
592(defvar fortran-mode-syntax-table
593 (let ((table (make-syntax-table)))
17cb3e0e
GM
594 ;; Was a word-constituent (for abbrevs), now punctuation (g77
595 ;; multi-statement lines).
596 (modify-syntax-entry ?\; "." table)
5b04210c
GM
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)
8cb8832f
GM
605 ;; Consistent with GNU Fortran's default -- see the manual.
606 ;; The F77 standard imposes no rule on this issue.
5b04210c
GM
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.")
f408b027 617
f6bf87c5
NR
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
7a7db8e5 624(defvar fortran-mode-map
315aa1de 625 (let ((map (make-sparse-keymap)))
5b04210c
GM
626 (define-key map ";" 'fortran-abbrev-start)
627 (define-key map "\C-c;" 'fortran-comment-region)
7a05f2bd
GM
628 ;; The default comment-dwim does at least as much as this.
629;;; (define-key map "\M-;" 'fortran-indent-comment)
5b04210c 630 (define-key map "\M-\n" 'fortran-split-line)
8cb8832f
GM
631 (define-key map "\M-\C-n" 'fortran-end-of-block)
632 (define-key map "\M-\C-p" 'fortran-beginning-of-block)
5b04210c 633 (define-key map "\M-\C-q" 'fortran-indent-subprogram)
315aa1de
DL
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
5b04210c 639 (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation
315aa1de
DL
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)
7a7db8e5 650
5b04210c
GM
651 (easy-menu-define fortran-menu map "Menu for Fortran mode."
652 `("Fortran"
9028691d
GM
653 ["Manual" (info "(emacs)Fortran") :active t
654 :help "Read the Emacs manual chapter on Fortran mode"]
5b04210c
GM
655 ("Customization"
656 ,(custom-menu-create 'fortran)
9028691d
GM
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"]
5b04210c
GM
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 "--"
9028691d
GM
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"]
5b04210c 681 ("Mark"
9028691d 682 :help "Mark a region of code"
5b04210c
GM
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 "--"
9028691d
GM
690 ["Temporary Column Ruler" fortran-column-ruler :active t
691 :help "Briefly display Fortran column numbers"]
694cf05e
GM
692 ;; May not be '72', depending on fortran-line-length, but this
693 ;; seems ok for a menu item.
9028691d
GM
694 ["72-column Window" fortran-window-create :active t
695 :help "Set window width to Fortran line length"]
5b04210c
GM
696 ["Full Width Window"
697 (enlarge-window-horizontally (- (frame-width) (window-width)))
9028691d
GM
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"]
5b04210c 702 "--"
9028691d
GM
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]
5b04210c 708 "--"
9028691d
GM
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
5b04210c 715 :active (not (lookup-key (current-local-map) [menu-bar index]))
9028691d
GM
716 :included (fboundp 'imenu-add-to-menubar)
717 :help "Add an index menu to the menu-bar"]))
315aa1de 718 map)
b8cbdf43 719 "Keymap used in Fortran mode.")
5b04210c 720
3dd63760 721\f
17cb3e0e
GM
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:]_]*")
7977773d 785
5b04210c 786\f
3dd63760 787;;;###autoload
e75c1e7d 788(define-derived-mode fortran-mode prog-mode "Fortran"
5b04210c
GM
789 "Major mode for editing Fortran code in fixed format.
790For free format code, use `f90-mode'.
791
60db3594 792\\[fortran-indent-line] indents the current Fortran line correctly.
5b04210c 793Note that DO statements must not share a common CONTINUE.
3dd63760 794
5b04210c
GM
795Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
796 Fortran keywords.
3dd63760
JB
797
798Key definitions:
799\\{fortran-mode-map}
800
801Variables controlling indentation style and extra features:
802
8cb8832f 803`fortran-comment-line-start'
5b04210c
GM
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
9d23c925 825 indentation for a line of code.
5b04210c
GM
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).
3dd63760 855
b8cbdf43 856Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
3dd63760 857with no args, if that value is non-nil."
e75c1e7d
GM
858 :group 'fortran
859 :syntax-table fortran-mode-syntax-table
860 :abbrev-table fortran-mode-abbrev-table
5b04210c
GM
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))))
fd98e23a 867 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
5b04210c
GM
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)
ff451e17
SM
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]*")
5b04210c 878 (set (make-local-variable 'comment-indent-function) 'fortran-comment-indent)
7a05f2bd
GM
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)
5b04210c
GM
884 (set (make-local-variable 'abbrev-all-caps) t)
885 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
5b04210c 886 (set (make-local-variable 'indent-tabs-mode) (fortran-analyze-file-format))
a7113309 887 (setq mode-line-process '(indent-tabs-mode fortran-tab-mode-string))
694cf05e 888 (set (make-local-variable 'fill-column) fortran-line-length)
1eb6bf70 889 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
5b04210c
GM
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
8cb8832f
GM
894 fortran-font-lock-keywords-3
895 fortran-font-lock-keywords-4)
5b04210c 896 nil t ((?/ . "$/") ("_$" . "w"))
cf38dd42 897 fortran-beginning-of-subprogram))
cf38dd42 898 (set (make-local-variable 'syntax-propertize-function)
b879a6e2 899 (fortran-make-syntax-propertize-function fortran-line-length))
5b04210c
GM
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")))
26ef1c87
DL
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)
68ca306c
DL
908 (set (make-local-variable 'add-log-current-defun-function)
909 #'fortran-current-defun)
315aa1de 910 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
5727e748 911 (set (make-local-variable 'gud-find-expr-function) 'fortran-gud-find-expr)
e75c1e7d 912 (add-hook 'hack-local-variables-hook 'fortran-hack-local-variables nil t))
5b04210c 913
3dd63760 914\f
694cf05e
GM
915(defun fortran-line-length (nchars &optional global)
916 "Set the length of fixed-form Fortran lines to NCHARS.
917This normally only affects the current buffer, which must be in
918Fortran mode. If the optional argument GLOBAL is non-nil, it
b879a6e2
SM
919affects all Fortran buffers, and also the default.
920If a numeric prefix argument is specified, it will be used as NCHARS,
921otherwise is a non-numeric prefix arg is specified, the length will be
922provided 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))))))
8c5de2a1 941 (if global
b879a6e2 942 (setq-default fortran-line-length nchars)))
694cf05e
GM
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
2e49e9f7
GM
948(declare-function gud-find-c-expr "gud.el" nil)
949
5727e748
SM
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
315aa1de 955(defsubst fortran-comment-indent ()
5b04210c
GM
956 "Return the indentation appropriate for the current comment line.
957This is 0 for a line matching `fortran-comment-line-start-skip', else
958the value of `comment-column' (leaving at least one space after code)."
959 (if (looking-at fortran-comment-line-start-skip) 0
960 (save-excursion
ff451e17 961 (skip-chars-backward " \t")
0a39a75c 962 (max (1+ (current-column)) comment-column))))
3dd63760
JB
963
964(defun fortran-indent-comment ()
965 "Align or create comment on current line.
966Existing comments of all types are recognized and aligned.
5b04210c 967If the line has no comment, a side-by-side comment is inserted and aligned,
ff451e17 968if the value of `comment-start' is not nil and allows such comments.
3dd63760
JB
969Otherwise, a separate-line comment is inserted, on this line
970or on a new line inserted before this line if this line is not blank."
5b04210c 971 (interactive "*")
3dd63760
JB
972 (beginning-of-line)
973 ;; Recognize existing comments of either kind.
ff451e17 974 (cond ((fortran-find-comment-start-skip 'all)
9d23c925
GM
975 (goto-char (match-beginning 0))
976 (if (bolp)
977 (fortran-indent-line)
978 (unless (= (current-column) (fortran-comment-indent))
5b04210c
GM
979 (delete-horizontal-space)
980 (indent-to (fortran-comment-indent)))))
9d23c925
GM
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))))))
3dd63760
JB
1002
1003(defun fortran-comment-region (beg-region end-region arg)
5b04210c
GM
1004 "Comment every line in the region.
1005Inserts the string variable `fortran-comment-region' at the beginning of
1006every line in the region.
1007BEG-REGION and END-REGION specify the region boundaries.
3dd63760
JB
1008With non-nil ARG, uncomments the region."
1009 (interactive "*r\nP")
b937fd1e 1010 (let ((end-region-mark (copy-marker end-region))
9d23c925 1011 (save-point (point-marker)))
3dd63760
JB
1012 (goto-char beg-region)
1013 (beginning-of-line)
5b04210c 1014 (if arg
0a39a75c 1015 (let ((com (regexp-quote fortran-comment-region))) ; uncomment
5b04210c
GM
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)))))
0a39a75c 1022 (insert fortran-comment-region) ; comment
5b04210c
GM
1023 (while (and (zerop (forward-line 1))
1024 (< (point) end-region-mark))
1025 (insert fortran-comment-region)))
3dd63760
JB
1026 (goto-char save-point)
1027 (set-marker end-region-mark nil)
1028 (set-marker save-point nil)))
5b04210c 1029
7a05f2bd
GM
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
3dd63760
JB
1035\f
1036(defun fortran-abbrev-start ()
60db3594 1037 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
3dd63760 1038Any other key combination is executed normally."
5b04210c 1039 (interactive "*")
1ba983e8 1040 (insert last-command-event)
5d724235
SM
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)))
be35ca9f 1046 ;; Insert char if not equal to `?', or if abbrev-mode is off.
5d724235
SM
1047 (if (and abbrev-mode (or (eq char ??) (eq char help-char)
1048 (memq event help-event-list)))
9d23c925 1049 (fortran-abbrev-help)
5d724235 1050 (push event unread-command-events))))
3dd63760
JB
1051
1052(defun fortran-abbrev-help ()
1053 "List the currently defined abbrevs in Fortran mode."
1054 (interactive)
1055 (message "Listing abbrev table...")
e80f2147 1056 (display-buffer (fortran-prepare-abbrev-list-buffer))
3dd63760
JB
1057 (message "Listing abbrev table...done"))
1058
e80f2147 1059(defun fortran-prepare-abbrev-list-buffer ()
5b04210c 1060 "Create a buffer listing the Fortran mode abbreviations."
5d724235 1061 (with-current-buffer (get-buffer-create "*Abbrevs*")
e80f2147 1062 (erase-buffer)
4632a893 1063 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
e80f2147
RS
1064 (goto-char (point-min))
1065 (set-buffer-modified-p nil)
1066 (edit-abbrevs-mode))
1067 (get-buffer-create "*Abbrevs*"))
1068
3dd63760 1069(defun fortran-column-ruler ()
7977773d 1070 "Insert a column ruler momentarily above current line, till next keystroke.
0a39a75c
GM
1071The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
1072format mode, and `fortran-column-ruler-tab' in TAB format mode.
1073The next key typed is executed unless it is SPC."
3dd63760 1074 (interactive)
60db3594 1075 (momentary-string-display
23029d77
JB
1076 (if indent-tabs-mode
1077 fortran-column-ruler-tab
1078 fortran-column-ruler-fixed)
1079 (save-excursion
60db3594 1080 (beginning-of-line)
23029d77 1081 (if (eq (window-start (selected-window))
9d23c925
GM
1082 (window-point (selected-window)))
1083 (line-beginning-position 2)
23029d77 1084 (point)))
3dd63760
JB
1085 nil "Type SPC or any command to erase ruler."))
1086
1087(defun fortran-window-create ()
694cf05e 1088 "Make the window `fortran-line-length' (default 72) columns wide.
fe668515 1089See also `fortran-window-create-momentarily'."
3dd63760 1090 (interactive)
b2523604 1091 (let ((window-min-width 2))
ca03b5a9 1092 (unless (window-full-width-p)
9d23c925
GM
1093 (enlarge-window-horizontally (- (frame-width)
1094 (window-width) 1)))
b2523604 1095 (let* ((window-edges (window-edges))
9d23c925
GM
1096 (scroll-bar-width (- (nth 2 window-edges)
1097 (car window-edges)
1098 (window-width))))
694cf05e 1099 (split-window-horizontally (+ fortran-line-length scroll-bar-width)))
b2523604
DL
1100 (other-window 1)
1101 (switch-to-buffer " fortran-window-extra" t)
1102 (select-window (previous-window))))
3dd63760
JB
1103
1104(defun fortran-window-create-momentarily (&optional arg)
694cf05e 1105 "Momentarily make the window `fortran-line-length' (default 72) columns wide.
3dd63760 1106Optional ARG non-nil and non-unity disables the momentary feature.
fe668515 1107See also `fortran-window-create'."
3dd63760
JB
1108 (interactive "p")
1109 (if (or (not arg)
9d23c925 1110 (= arg 1))
3dd63760 1111 (save-window-excursion
9d23c925
GM
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))))))
3dd63760
JB
1120 (fortran-window-create)))
1121
1122(defun fortran-split-line ()
1123 "Break line at point and insert continuation marker and alignment."
5b04210c 1124 (interactive "*")
3dd63760 1125 (delete-horizontal-space)
7dae727d 1126 (if (save-excursion
9d23c925
GM
1127 (let ((pos (point)))
1128 (beginning-of-line)
1129 (and (fortran-find-comment-start-skip 'all)
1130 (< (match-beginning 0) pos))))
ff451e17 1131 (insert ?\n (match-string 0))
23029d77 1132 (if indent-tabs-mode
9d23c925 1133 (insert ?\n ?\t (fortran-numerical-continuation-char))
0a39a75c
GM
1134 (insert "\n " fortran-continuation-string))) ; space after \n important
1135 (fortran-indent-line)) ; when cont string is C, c or *
1eb6bf70
DL
1136
1137(defun fortran-remove-continuation ()
5b04210c
GM
1138 "Delete any Fortran continuation characters at point.
1139Returns t if anything actually deleted."
1140 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1141 (replace-match "")
1142 (delete-indentation)
1143 t))
3dd63760 1144
46d4d7bf
DL
1145(defun fortran-join-line (arg)
1146 "Join current line to the previous one and re-indent.
1147With a prefix argument, repeat this operation that many times.
1148If the prefix argument ARG is negative, join the next -ARG lines.
1149Continuation lines are correctly handled."
1150 (interactive "*p")
7977773d 1151 (save-excursion
46d4d7bf
DL
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)))
7977773d
DL
1160 (fortran-indent-line)))
1161
3dd63760 1162(defun fortran-numerical-continuation-char ()
b56eb7c9 1163 "Return a digit for tab-digit style of continuation lines.
5b04210c
GM
1164If previous line is a tab-digit continuation line, return that digit
1165plus one, otherwise return 1. Zero not allowed."
3dd63760
JB
1166 (save-excursion
1167 (forward-line -1)
1168 (if (looking-at "\t[1-9]")
9d23c925 1169 (+ ?1 (% (- (char-after (1+ (point))) ?0) 9))
3dd63760
JB
1170 ?1)))
1171
556dd629 1172(put 'fortran-electric-line-number 'delete-selection t)
3dd63760
JB
1173(defun fortran-electric-line-number (arg)
1174 "Self insert, but if part of a Fortran line number indent it automatically.
7977773d 1175Auto-indent does not happen if a numeric ARG is used."
5b04210c 1176 (interactive "*P")
3dd63760 1177 (if (or arg (not fortran-electric-line-number))
60db3594 1178 (if arg
9d23c925
GM
1179 (self-insert-command (prefix-numeric-value arg))
1180 (self-insert-command 1))
3dd63760 1181 (if (or (and (= 5 (current-column))
9d23c925
GM
1182 (save-excursion
1183 (beginning-of-line)
5b04210c 1184 ;; In col 5 with only spaces to the left.
9d23c925
GM
1185 (looking-at " \\{5\\}")))
1186 (and (= (if indent-tabs-mode
1187 fortran-minimum-statement-indent-tab
1188 fortran-minimum-statement-indent-fixed) (current-column))
5b04210c 1189 ;; In col 8 with a single tab to the left.
9d23c925
GM
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))
3dd63760 1200 (skip-chars-backward " \t")
1ba983e8 1201 (insert last-command-event)
3dd63760 1202 (fortran-indent-line))))
5b04210c 1203
3dd63760 1204\f
823ab5da
DL
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
bd6cabcf 1210 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
9d23c925
GM
1211 (buffer-substring (match-end 0)
1212 (min (line-end-position)
1213 (+ fortran-line-length
694cf05e 1214 (line-beginning-position)))))))
823ab5da 1215
4a948dbf
GM
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?
7dae727d 1226(defun fortran-beginning-of-subprogram ()
b2523604 1227 "Move point to the beginning of the current Fortran subprogram."
3dd63760 1228 (interactive)
4a948dbf
GM
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.
7dae727d 1248(defun fortran-end-of-subprogram ()
b2523604 1249 "Move point to the end of the current Fortran subprogram."
3dd63760 1250 (interactive)
4a948dbf
GM
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))))
3dd63760 1257
3dd63760 1258(defun fortran-previous-statement ()
b2523604 1259 "Move point to beginning of the previous Fortran statement.
8edfcc7d
GM
1260Returns 'first-statement if that statement is the first
1261non-comment Fortran statement in the file, and nil otherwise.
5a73972b 1262Directive lines are treated as comments."
3dd63760
JB
1263 (interactive)
1264 (let (not-first-statement continue-test)
1265 (beginning-of-line)
1266 (setq continue-test
9d23c925
GM
1267 (and
1268 (not (looking-at fortran-comment-line-start-skip))
5a73972b 1269 (not (looking-at fortran-directive-re))
9d23c925
GM
1270 (or (looking-at
1271 (concat "[ \t]*"
1272 (regexp-quote fortran-continuation-string)))
1273 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
5b04210c 1274 (while (and (setq not-first-statement (zerop (forward-line -1)))
9d23c925 1275 (or (looking-at fortran-comment-line-start-skip)
5a73972b 1276 (looking-at fortran-directive-re)
98110b1f
GM
1277 (looking-at
1278 (concat "[ \t]*"
1279 (regexp-quote fortran-continuation-string)))
9d23c925
GM
1280 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1281 (looking-at (concat "[ \t]*" comment-start-skip)))))
3dd63760 1282 (cond ((and continue-test
9d23c925
GM
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))))
3dd63760
JB
1289
1290(defun fortran-next-statement ()
b2523604 1291 "Move point to beginning of the next Fortran statement.
8edfcc7d
GM
1292Returns 'last-statement if that statement is the last
1293non-comment Fortran statement in the file, and nil otherwise.
5a73972b 1294Directive lines are treated as comments."
3dd63760
JB
1295 (interactive)
1296 (let (not-last-statement)
1297 (beginning-of-line)
b8cbdf43 1298 (while (and (setq not-last-statement
9d23c925
GM
1299 (and (zerop (forward-line 1))
1300 (not (eobp))))
1301 (or (looking-at fortran-comment-line-start-skip)
5a73972b 1302 (looking-at fortran-directive-re)
9d23c925
GM
1303 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1304 (looking-at (concat "[ \t]*" comment-start-skip)))))
3dd63760 1305 (if (not not-last-statement)
9d23c925 1306 'last-statement)))
5b04210c 1307
8cb8832f
GM
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)
9b026d9f 1316 (= (line-beginning-position) p)))))
8cb8832f
GM
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.
1321With optional argument NUM, go forward that many balanced blocks.
1322If NUM is negative, go backward to the start of a block. Does
1323not check for consistency of block types. Interactively, pushes
1324mark before moving point."
1325 (interactive "p")
d47afbc0 1326 (if (called-interactively-p 'any) (push-mark (point) t))
8cb8832f
GM
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.
1354With optional argument NUM, go backward that many balanced
1355blocks. If NUM is negative, go forward to the end of a block.
1356Does not check for consistency of block types. Interactively,
1357pushes mark before moving point."
1358 (interactive "p")
d47afbc0 1359 (if (called-interactively-p 'any) (push-mark (point) t))
8cb8832f
GM
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
3dd63760 1385\f
7dae727d
DL
1386(defun fortran-blink-match (regex keyword find-begin)
1387 "From a line matching REGEX, blink matching KEYWORD statement line.
1388Use function FIND-BEGIN to match it."
1eb6bf70 1389 (let ((top-of-window (window-start))
9d23c925
GM
1390 (end-point (point))
1391 (case-fold-search t)
1392 matching
1393 message)
5b04210c
GM
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)
7533f3b5 1411 (sit-for blink-matching-delay)
5b04210c 1412 (goto-char end-point)))))
7dae727d
DL
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"
9d23c925 1417 "if" #'fortran-beginning-if))
947388af
RS
1418
1419(defun fortran-blink-matching-do ()
45cf60ae 1420 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
7dae727d 1421 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
c5af0a18
RS
1422
1423(defun fortran-mark-do ()
60db3594 1424 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
c5af0a18
RS
1425The 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.")
c5af0a18
RS
1431 (goto-char enddo-point)
1432 (push-mark)
1433 (goto-char do-point)))))
1434
1435(defun fortran-end-do ()
45cf60ae
DL
1436 "Search forward for first unmatched ENDDO.
1437Return point or nil."
1eb6bf70
DL
1438 (let ((case-fold-search t))
1439 (if (save-excursion (beginning-of-line)
9d23c925
GM
1440 (skip-chars-forward " \t0-9")
1441 (looking-at "end[ \t]*do\\b"))
1442 ;; Sitting on one.
1443 (match-beginning 0)
1eb6bf70
DL
1444 ;; Search for one.
1445 (save-excursion
9d23c925
GM
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)))))))
c5af0a18
RS
1461
1462(defun fortran-beginning-do ()
45cf60ae 1463 "Search backwards for first unmatched DO [WHILE].
845d331e
GM
1464Return 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]"))
7dae727d 1467 (if (save-excursion
9d23c925
GM
1468 (beginning-of-line)
1469 (skip-chars-forward " \t0-9")
1470 (looking-at dostart-re))
1471 ;; Sitting on one.
1472 (match-beginning 0)
1eb6bf70
DL
1473 ;; Search for one.
1474 (save-excursion
9d23c925
GM
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)))
845d331e 1484 ;; Note labelled loop ends not considered.
9d23c925
GM
1485 ((looking-at "end[ \t]*do\\b")
1486 (setq count (1+ count)))))
1487 (and (zerop count)
1488 ;; All pairs accounted for.
1489 (point)))))))
c5af0a18
RS
1490
1491(defun fortran-mark-if ()
1492 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1493The 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 ()
45cf60ae
DL
1505 "Search forwards for first unmatched ENDIF.
1506Return point or nil."
1eb6bf70
DL
1507 (let ((case-fold-search t))
1508 (if (save-excursion (beginning-of-line)
9d23c925
GM
1509 (skip-chars-forward " \t0-9")
1510 (looking-at "end[ \t]*if\\b"))
1511 ;; Sitting on one.
1512 (match-beginning 0)
1eb6bf70
DL
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
9d23c925
GM
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)))))))
c5af0a18
RS
1544
1545(defun fortran-beginning-if ()
45cf60ae
DL
1546 "Search backwards for first unmatched IF-THEN.
1547Return point or nil."
1eb6bf70
DL
1548 (let ((case-fold-search t))
1549 (if (save-excursion
9d23c925
GM
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
5b04210c 1565 (and (zerop (forward-line 1))
9d23c925
GM
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)
1eb6bf70
DL
1575 ;; Search for one.
1576 (save-excursion
9d23c925
GM
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)))))))
5b04210c 1606
3dd63760
JB
1607\f
1608(defun fortran-indent-line ()
7977773d 1609 "Indent current Fortran line based on its contents and on previous lines."
5b04210c 1610 (interactive "*")
1eb6bf70 1611 (let ((cfi (fortran-calculate-indent)))
3dd63760
JB
1612 (save-excursion
1613 (beginning-of-line)
1614 (if (or (not (= cfi (fortran-current-line-indentation)))
9d23c925
GM
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))))
3dd63760
JB
1621 ;; Never leave point in left margin.
1622 (if (< (current-column) cfi)
9d23c925 1623 (move-to-column cfi))
5b04210c
GM
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))))
3dd63760 1633
315aa1de 1634(defun fortran-auto-fill ()
5b04210c 1635 "Function to use for `normal-auto-fill-function' in Fortran mode."
315aa1de
DL
1636 (if (> (current-column) (current-fill-column))
1637 (let ((cfi (fortran-calculate-indent)))
9d23c925
GM
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)))))
315aa1de 1652
5aca2648
DL
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)
b8cbdf43 1656
3dd63760 1657(defun fortran-indent-subprogram ()
5b04210c
GM
1658 "Properly indent the Fortran subprogram containing point."
1659 (interactive "*")
3dd63760 1660 (save-excursion
7a7db8e5 1661 (mark-defun)
c4c42b2e
DL
1662 (message "Indenting subprogram...")
1663 (indent-region (point) (mark) nil))
3dd63760
JB
1664 (message "Indenting subprogram...done."))
1665
1eb6bf70 1666(defun fortran-calculate-indent ()
b8cbdf43 1667 "Calculates the Fortran indent column based on previous lines."
3dd63760 1668 (let (icol first-statement (case-fold-search t)
9d23c925
GM
1669 (fortran-minimum-statement-indent
1670 (if indent-tabs-mode
1671 fortran-minimum-statement-indent-tab
1672 fortran-minimum-statement-indent-fixed)))
3dd63760
JB
1673 (save-excursion
1674 (setq first-statement (fortran-previous-statement))
1675 (if first-statement
9d23c925 1676 (setq icol fortran-minimum-statement-indent)
98110b1f
GM
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]")
9d23c925 1683 (let (then-test) ; multi-line if-then
5b04210c 1684 (while (and (zerop (forward-line 1))
0a39a75c 1685 ;; Search forward for then.
98110b1f
GM
1686 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1687 (not (setq then-test
1688 (looking-at
1689 ".*then\\b[ \t]\
b8cbdf43 1690*[^ \t_$(=a-z0-9]")))))
98110b1f
GM
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\\)\
7dae727d 1709\\b[ \t]*[^ \t=(a-z]")
98110b1f
GM
1710 (setq icol (+ icol fortran-structure-indent)))
1711 ((and (looking-at fortran-end-prog-re1)
1712 (fortran-check-end-prog-re))
0a39a75c 1713 ;; Previous END resets indent to minimum.
dd08226a
GM
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))))))
3dd63760
JB
1725 (save-excursion
1726 (beginning-of-line)
1727 (cond ((looking-at "[ \t]*$"))
5a73972b 1728 ;; Check for directive before comment, so as not to indent.
9d23c925
GM
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]"))
98110b1f
GM
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))))
9d23c925 1748 (first-statement)
dd08226a
GM
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).
9d23c925 1752 ((and fortran-check-all-num-for-matching-do
dd08226a
GM
1753 (looking-at "[ \t]*[0-9]+[ \t]*\
1754\\(continue\\|end[ \t]*do\\)\\>")
9d23c925
GM
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)))
1eb6bf70 1763 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
9d23c925
GM
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]*\
5a8d870b 1773\\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
9d23c925
GM
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\
3dd63760
JB
1779 an unclosed block." fortran-minimum-statement-indent))))))
1780 (max fortran-minimum-statement-indent icol)))
5b04210c 1781
3dd63760
JB
1782\f
1783(defun fortran-current-line-indentation ()
1784 "Indentation of current line, ignoring Fortran line number or continuation.
1785This is the column position of the first non-whitespace character
1786aside from the line number and/or column 5/8 line-continuation character.
1787For comment lines, returns indentation of the first
1788non-indentation text within the comment."
1789 (save-excursion
1790 (beginning-of-line)
7dae727d 1791 (cond ((looking-at fortran-comment-line-start-skip)
9d23c925
GM
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]")))
3dd63760
JB
1802 ;; Move past whitespace.
1803 (skip-chars-forward " \t")
1804 (current-column)))
1805
1806(defun fortran-indent-to-column (col)
5b04210c 1807 "Indent current line to column COL.
3dd63760
JB
1808notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1809 line, and this continuation character is retained on indentation;
b8cbdf43
RS
1810 2) If `fortran-continuation-string' is the first non-whitespace
1811 character, this is a continuation line;
3dd63760
JB
1812 3) A non-continuation line which has a number as the first
1813 non-whitespace character is a numbered line.
b8cbdf43 1814 4) A TAB followed by a digit indicates a continuation line."
3dd63760
JB
1815 (save-excursion
1816 (beginning-of-line)
7dae727d 1817 (if (looking-at fortran-comment-line-start-skip)
9d23c925
GM
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)))))
3dd63760 1828 (if (looking-at "\t[1-9]")
9d23c925
GM
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
0a39a75c 1841 ;; continuation character in column 5.
9d23c925
GM
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")))))
3dd63760
JB
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.
5b04210c
GM
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)))))))
3dd63760
JB
1870
1871(defun fortran-line-number-indented-correctly-p ()
1872 "Return t if current line's line number is correctly indented.
1873Do 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)
9d23c925
GM
1878 (or (= (current-column) fortran-line-number-indent)
1879 (progn (skip-chars-forward "0-9")
1880 (= (current-column) 5))))))
3dd63760
JB
1881
1882(defun fortran-check-for-matching-do ()
7977773d
DL
1883 "When called from a numbered statement, return t if matching DO is found.
1884Otherwise return nil."
7dae727d 1885 (let ((case-fold-search t)
9d23c925 1886 charnum)
3dd63760
JB
1887 (save-excursion
1888 (beginning-of-line)
5b04210c
GM
1889 (when (looking-at "[ \t]*[0-9]+")
1890 (skip-chars-forward " \t")
9d23c925 1891 (skip-chars-forward "0") ; skip past leading zeros
5b04210c
GM
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)))))))))
3dd63760 1909
ff451e17 1910(defun fortran-find-comment-start-skip (&optional all)
b8cbdf43 1911 "Move to past `comment-start-skip' found on current line.
ff451e17
SM
1912Return non-nil if `comment-start-skip' found, nil if not.
1913If ALL is nil, only match comments that start in column > 0."
ff451e17
SM
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))
9d23c925
GM
1918 (css (if comment-start-skip
1919 (concat fortran-comment-line-start-skip
1920 "\\|" comment-start-skip)
1921 fortran-comment-line-start-skip)))
ff451e17 1922 (when (re-search-forward css (line-end-position) t)
9d23c925
GM
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)))))
f022dd89 1931
5b04210c 1932;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1eb6bf70 1933;; Test if TAB format continuation lines work.
b56eb7c9 1934(defun fortran-is-in-string-p (where)
e7f767c2 1935 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
b56eb7c9
RS
1936 (save-excursion
1937 (goto-char where)
1938 (cond
9d23c925
GM
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)
0a39a75c 1943 (t (let ((parse-state '(0 nil nil nil nil nil 0))
9d23c925
GM
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))))))
b8cbdf43 1990
7aabd23e
DL
1991;; From old version.
1992(defalias 'fortran-auto-fill-mode 'auto-fill-mode)
b8cbdf43 1993
4254fe58 1994(defun fortran-fill ()
5b04210c 1995 "Fill the current line at an appropriate point(s)."
315aa1de 1996 (let* ((auto-fill-function #'fortran-auto-fill)
9d23c925
GM
1997 (opoint (point))
1998 (bol (line-beginning-position))
1999 (eol (line-end-position))
2000 (bos (min eol (+ bol (fortran-current-line-indentation))))
337c50a5
GM
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).
9d23c925
GM
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)
98110b1f 2008 (let (fcpoint start)
337c50a5
GM
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))
8cb8832f
GM
2019 ;; If the current string is longer than (fill-column
2020 ;; - 6) chars, break it at the fill column (else
2021 ;; infinite loop).
337c50a5
GM
2022 (if (> (- (point) start)
2023 (- fill-column 6 fortran-continuation-indent))
2024 fcpoint
2025 start))))))
9d23c925
GM
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
337c50a5
GM
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.
8aa7b879 2041 (re-search-backward fortran-break-delimiters-re bol)
337c50a5
GM
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?
9d23c925 2055 (when (<= (point) bos)
98110b1f 2056 (move-to-column (1+ fill-column))
0a39a75c 2057 ;; What is this doing???
98110b1f
GM
2058 (or (re-search-forward "[\t\n,'+-/*)=]" eol t)
2059 (goto-char bol)))
9d23c925
GM
2060 (if (bolp)
2061 (re-search-forward "[ \t]" opoint t))
337c50a5 2062 (point)))))
0a39a75c 2063 ;; If we are in an in-line comment, don't break unless the
b8cbdf43
RS
2064 ;; line of code is longer than it should be. Otherwise
2065 ;; break the line at the column computed above.
2066 ;;
0a39a75c
GM
2067 ;; Need to use fortran-find-comment-start-skip to make sure that
2068 ;; quoted !'s don't prevent a break.
ff451e17 2069 (when (and (save-excursion
9d23c925
GM
2070 (beginning-of-line)
2071 (if (not (fortran-find-comment-start-skip))
0ab47edc 2072 t
9d23c925
GM
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))))
ff451e17
SM
2084 (goto-char fill-point)
2085 (fortran-break-line)
2086 (end-of-line))))
7dae727d 2087
b8cbdf43 2088(defun fortran-break-line ()
5b04210c 2089 "Call `fortran-split-line'. Joins continuation lines first, then refills."
5d724235 2090 (let ((bol (line-beginning-position))
9d23c925
GM
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))))))
0a39a75c 2096 ;; Forward line 1 really needs to go to next non white line.
1eb6bf70 2097 (if (save-excursion (forward-line)
9d23c925
GM
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))
b8cbdf43
RS
2104 (fortran-split-line))
2105 (if comment-string
9d23c925
GM
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)))))
b8cbdf43 2112
23029d77 2113(defun fortran-analyze-file-format ()
7977773d 2114 "Return nil if fixed format is used, t if TAB formatting is used.
5b04210c
GM
2115Use `fortran-tab-mode-default' if no non-comment statements are found
2116before the end or in the first `fortran-analyze-depth' lines."
23029d77
JB
2117 (let ((i 0))
2118 (save-excursion
2119 (goto-char (point-min))
23029d77 2120 (while (not (or
9d23c925
GM
2121 (eobp)
2122 (eq (char-after) ?\t)
2123 (looking-at " \\{6\\}")
2124 (> i fortran-analyze-depth)))
2125 (forward-line)
2126 (setq i (1+ i)))
23029d77 2127 (cond
7dae727d 2128 ((eq (char-after) ?\t) t)
315aa1de 2129 ((looking-at " \\{6\\}") nil)
a7113309 2130 (t fortran-tab-mode-default)))))
23029d77 2131
1eb6bf70
DL
2132(defun fortran-fill-paragraph (&optional justify)
2133 "Fill surrounding comment block as paragraphs, else fill statement.
5b04210c
GM
2134Intended as the value of `fill-paragraph-function'.
2135A comment block is filled by calling `fill-comment-paragraph' with
2136argument JUSTIFY, otherwise `fortran-fill-statement' is called.
2137Always returns non-nil (to prevent `fill-paragraph' being called)."
2138 (interactive "*P")
2bcfe15e
SM
2139 (or (fill-comment-paragraph justify)
2140 (fortran-fill-statement)
2141 t))
1eb6bf70
DL
2142
2143(defun fortran-fill-statement ()
5b04210c
GM
2144 "Fill a Fortran statement up to `fill-column'."
2145 (interactive "*")
315aa1de 2146 (let ((auto-fill-function #'fortran-auto-fill))
5b04210c
GM
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)))
e04196d3 2165 (fortran-indent-line)))
1eb6bf70 2166
cfe9d0b5 2167(defun fortran-strip-sequence-nos (&optional do-space)
694cf05e
GM
2168 "Delete all text in column `fortran-line-length' (default 72) and up.
2169This is assumed to be sequence numbers. Normally also deletes
2170trailing whitespace after stripping such text. Supplying prefix
2171arg DO-SPACE prevents stripping the whitespace."
5b04210c 2172 (interactive "*p")
3b4613a4
DL
2173 (save-excursion
2174 (goto-char (point-min))
694cf05e
GM
2175 (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length)
2176 nil t)
3b4613a4
DL
2177 (replace-match "" nil nil nil 1)
2178 (unless do-space (delete-horizontal-space)))))
2179
4a948dbf 2180;; This code used to live in add-log.el, but this is a better place for it.
68ca306c
DL
2181(defun fortran-current-defun ()
2182 "Function to use for `add-log-current-defun-function' in Fortran mode."
a245ece5
GM
2183 (save-excursion
2184 ;; We must be inside function body for this to work.
2185 (fortran-beginning-of-subprogram)
4a948dbf 2186 (let ((case-fold-search t))
0a39a75c 2187 ;; Search for fortran subprogram start.
a245ece5 2188 (if (re-search-forward
4a948dbf 2189 fortran-start-prog-re
a245ece5
GM
2190 (save-excursion (fortran-end-of-subprogram)
2191 (point))
2192 t)
2193 (or (match-string-no-properties 2)
2194 (progn
0a39a75c 2195 ;; Move to EOL or before first left paren.
a245ece5
GM
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"))))
68ca306c 2204
49116ac0
JB
2205(provide 'fortran)
2206
1a06eabd 2207;;; fortran.el ends here