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