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