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