Add 2011 to FSF/AIST 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,
5df4f04c 4;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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
JB
780;;;###autoload
781(defun fortran-mode ()
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
JB
850with no args, if that value is non-nil."
851 (interactive)
852 (kill-all-local-variables)
5b04210c
GM
853 (setq major-mode 'fortran-mode
854 mode-name "Fortran"
855 local-abbrev-table fortran-mode-abbrev-table)
3dd63760 856 (set-syntax-table fortran-mode-syntax-table)
5b04210c
GM
857 (use-local-map fortran-mode-map)
858 (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
859 (set (make-local-variable 'indent-region-function)
860 (lambda (start end)
861 (let (fortran-blink-matching-if ; avoid blinking delay
862 indent-region-function)
863 (indent-region start end nil))))
fd98e23a 864 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
5b04210c
GM
865 ;; The syntax tables don't understand the column-0 comment-markers.
866 (set (make-local-variable 'comment-use-syntax) nil)
867 (set (make-local-variable 'comment-padding) "$$$")
868 (set (make-local-variable 'comment-start) fortran-comment-line-start)
ff451e17
SM
869 (set (make-local-variable 'comment-start-skip)
870 ;; We can't reuse `fortran-comment-line-start-skip' directly because
871 ;; it contains backrefs whereas we need submatch-1 to end at the
872 ;; beginning of the comment delimiter.
873 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
874 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
5b04210c 875 (set (make-local-variable 'comment-indent-function) 'fortran-comment-indent)
7a05f2bd
GM
876 (set (make-local-variable 'comment-region-function) 'fortran-comment-region)
877 (set (make-local-variable 'uncomment-region-function)
878 'fortran-uncomment-region)
879 (set (make-local-variable 'comment-insert-comment-function)
880 'fortran-indent-comment)
5b04210c
GM
881 (set (make-local-variable 'abbrev-all-caps) t)
882 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
5b04210c 883 (set (make-local-variable 'indent-tabs-mode) (fortran-analyze-file-format))
a7113309 884 (setq mode-line-process '(indent-tabs-mode fortran-tab-mode-string))
694cf05e 885 (set (make-local-variable 'fill-column) fortran-line-length)
1eb6bf70 886 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
5b04210c
GM
887 (set (make-local-variable 'font-lock-defaults)
888 '((fortran-font-lock-keywords
889 fortran-font-lock-keywords-1
890 fortran-font-lock-keywords-2
8cb8832f
GM
891 fortran-font-lock-keywords-3
892 fortran-font-lock-keywords-4)
5b04210c 893 nil t ((?/ . "$/") ("_$" . "w"))
a227f163
SM
894 fortran-beginning-of-subprogram
895 (font-lock-syntactic-keywords
a4c687e0 896 . fortran-font-lock-syntactic-keywords)))
5b04210c
GM
897 (set (make-local-variable 'imenu-case-fold-search) t)
898 (set (make-local-variable 'imenu-generic-expression)
899 fortran-imenu-generic-expression)
900 (set (make-local-variable 'imenu-syntax-alist) '(("_$" . "w")))
26ef1c87
DL
901 (set (make-local-variable 'beginning-of-defun-function)
902 #'fortran-beginning-of-subprogram)
903 (set (make-local-variable 'end-of-defun-function)
904 #'fortran-end-of-subprogram)
68ca306c
DL
905 (set (make-local-variable 'add-log-current-defun-function)
906 #'fortran-current-defun)
315aa1de 907 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
5727e748 908 (set (make-local-variable 'gud-find-expr-function) 'fortran-gud-find-expr)
694cf05e 909 (add-hook 'hack-local-variables-hook 'fortran-hack-local-variables nil t)
9a969196 910 (run-mode-hooks 'fortran-mode-hook))
5b04210c 911
3dd63760 912\f
694cf05e
GM
913(defun fortran-line-length (nchars &optional global)
914 "Set the length of fixed-form Fortran lines to NCHARS.
915This normally only affects the current buffer, which must be in
916Fortran mode. If the optional argument GLOBAL is non-nil, it
917affects all Fortran buffers, and also the default."
918 (interactive "p")
919 (let (new)
8c5de2a1
GM
920 (mapc (lambda (buff)
921 (with-current-buffer buff
922 (when (eq major-mode 'fortran-mode)
923 (setq fortran-line-length nchars
924 fill-column fortran-line-length
925 new (fortran-font-lock-syntactic-keywords))
926 ;; Refontify only if necessary.
927 (unless (equal new font-lock-syntactic-keywords)
928 (setq font-lock-syntactic-keywords
929 (fortran-font-lock-syntactic-keywords))
930 (if font-lock-mode (font-lock-mode 1))))))
931 (if global
932 (buffer-list)
933 (list (current-buffer))))
694cf05e
GM
934 (if global
935 (setq-default fortran-line-length nchars))))
936
937(defun fortran-hack-local-variables ()
938 "Fortran mode adds this to `hack-local-variables-hook'."
939 (fortran-line-length fortran-line-length))
940
2e49e9f7
GM
941(declare-function gud-find-c-expr "gud.el" nil)
942
5727e748
SM
943(defun fortran-gud-find-expr ()
944 ;; Consider \n as punctuation (end of expression).
945 (with-syntax-table fortran-gud-syntax-table
946 (gud-find-c-expr)))
947
315aa1de 948(defsubst fortran-comment-indent ()
5b04210c
GM
949 "Return the indentation appropriate for the current comment line.
950This is 0 for a line matching `fortran-comment-line-start-skip', else
951the value of `comment-column' (leaving at least one space after code)."
952 (if (looking-at fortran-comment-line-start-skip) 0
953 (save-excursion
ff451e17 954 (skip-chars-backward " \t")
0a39a75c 955 (max (1+ (current-column)) comment-column))))
3dd63760
JB
956
957(defun fortran-indent-comment ()
958 "Align or create comment on current line.
959Existing comments of all types are recognized and aligned.
5b04210c 960If the line has no comment, a side-by-side comment is inserted and aligned,
ff451e17 961if the value of `comment-start' is not nil and allows such comments.
3dd63760
JB
962Otherwise, a separate-line comment is inserted, on this line
963or on a new line inserted before this line if this line is not blank."
5b04210c 964 (interactive "*")
3dd63760
JB
965 (beginning-of-line)
966 ;; Recognize existing comments of either kind.
ff451e17 967 (cond ((fortran-find-comment-start-skip 'all)
9d23c925
GM
968 (goto-char (match-beginning 0))
969 (if (bolp)
970 (fortran-indent-line)
971 (unless (= (current-column) (fortran-comment-indent))
5b04210c
GM
972 (delete-horizontal-space)
973 (indent-to (fortran-comment-indent)))))
9d23c925
GM
974 ;; No existing comment.
975 ;; If side-by-side comments are defined, insert one,
976 ;; unless line is now blank.
977 ((and comment-start (not (looking-at "[ \t]*$"))
978 (string-match comment-start-skip (concat " " comment-start)))
979 (end-of-line)
980 (delete-horizontal-space)
981 (indent-to (fortran-comment-indent))
982 (insert comment-start))
983 ;; Else insert separate-line comment, making a new line if nec.
984 (t
985 (if (looking-at "^[ \t]*$")
986 (delete-horizontal-space)
987 (beginning-of-line)
988 (insert ?\n)
989 (forward-char -1))
990 (insert fortran-comment-line-start)
991 (insert-char (if (stringp fortran-comment-indent-char)
992 (aref fortran-comment-indent-char 0)
993 fortran-comment-indent-char)
994 (- (fortran-calculate-indent) (current-column))))))
3dd63760
JB
995
996(defun fortran-comment-region (beg-region end-region arg)
5b04210c
GM
997 "Comment every line in the region.
998Inserts the string variable `fortran-comment-region' at the beginning of
999every line in the region.
1000BEG-REGION and END-REGION specify the region boundaries.
3dd63760
JB
1001With non-nil ARG, uncomments the region."
1002 (interactive "*r\nP")
b937fd1e 1003 (let ((end-region-mark (copy-marker end-region))
9d23c925 1004 (save-point (point-marker)))
3dd63760
JB
1005 (goto-char beg-region)
1006 (beginning-of-line)
5b04210c 1007 (if arg
0a39a75c 1008 (let ((com (regexp-quote fortran-comment-region))) ; uncomment
5b04210c
GM
1009 (if (looking-at com)
1010 (delete-region (point) (match-end 0)))
1011 (while (and (zerop (forward-line 1))
1012 (< (point) end-region-mark))
1013 (if (looking-at com)
1014 (delete-region (point) (match-end 0)))))
0a39a75c 1015 (insert fortran-comment-region) ; comment
5b04210c
GM
1016 (while (and (zerop (forward-line 1))
1017 (< (point) end-region-mark))
1018 (insert fortran-comment-region)))
3dd63760
JB
1019 (goto-char save-point)
1020 (set-marker end-region-mark nil)
1021 (set-marker save-point nil)))
5b04210c 1022
7a05f2bd
GM
1023;; uncomment-region calls this with 3 args.
1024(defun fortran-uncomment-region (start end &optional ignored)
1025 "Uncomment every line in the region."
1026 (fortran-comment-region start end t))
1027
3dd63760
JB
1028\f
1029(defun fortran-abbrev-start ()
60db3594 1030 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
3dd63760 1031Any other key combination is executed normally."
5b04210c 1032 (interactive "*")
1ba983e8 1033 (insert last-command-event)
5d724235
SM
1034 (let* ((event (if (fboundp 'next-command-event) ; XEmacs
1035 (next-command-event)
1036 (read-event)))
1037 (char (if (fboundp 'event-to-character)
1038 (event-to-character event) event)))
be35ca9f 1039 ;; Insert char if not equal to `?', or if abbrev-mode is off.
5d724235
SM
1040 (if (and abbrev-mode (or (eq char ??) (eq char help-char)
1041 (memq event help-event-list)))
9d23c925 1042 (fortran-abbrev-help)
5d724235 1043 (push event unread-command-events))))
3dd63760
JB
1044
1045(defun fortran-abbrev-help ()
1046 "List the currently defined abbrevs in Fortran mode."
1047 (interactive)
1048 (message "Listing abbrev table...")
e80f2147 1049 (display-buffer (fortran-prepare-abbrev-list-buffer))
3dd63760
JB
1050 (message "Listing abbrev table...done"))
1051
e80f2147 1052(defun fortran-prepare-abbrev-list-buffer ()
5b04210c 1053 "Create a buffer listing the Fortran mode abbreviations."
5d724235 1054 (with-current-buffer (get-buffer-create "*Abbrevs*")
e80f2147 1055 (erase-buffer)
4632a893 1056 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
e80f2147
RS
1057 (goto-char (point-min))
1058 (set-buffer-modified-p nil)
1059 (edit-abbrevs-mode))
1060 (get-buffer-create "*Abbrevs*"))
1061
3dd63760 1062(defun fortran-column-ruler ()
7977773d 1063 "Insert a column ruler momentarily above current line, till next keystroke.
0a39a75c
GM
1064The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
1065format mode, and `fortran-column-ruler-tab' in TAB format mode.
1066The next key typed is executed unless it is SPC."
3dd63760 1067 (interactive)
60db3594 1068 (momentary-string-display
23029d77
JB
1069 (if indent-tabs-mode
1070 fortran-column-ruler-tab
1071 fortran-column-ruler-fixed)
1072 (save-excursion
60db3594 1073 (beginning-of-line)
23029d77 1074 (if (eq (window-start (selected-window))
9d23c925
GM
1075 (window-point (selected-window)))
1076 (line-beginning-position 2)
23029d77 1077 (point)))
3dd63760
JB
1078 nil "Type SPC or any command to erase ruler."))
1079
1080(defun fortran-window-create ()
694cf05e 1081 "Make the window `fortran-line-length' (default 72) columns wide.
fe668515 1082See also `fortran-window-create-momentarily'."
3dd63760 1083 (interactive)
b2523604 1084 (let ((window-min-width 2))
ca03b5a9 1085 (unless (window-full-width-p)
9d23c925
GM
1086 (enlarge-window-horizontally (- (frame-width)
1087 (window-width) 1)))
b2523604 1088 (let* ((window-edges (window-edges))
9d23c925
GM
1089 (scroll-bar-width (- (nth 2 window-edges)
1090 (car window-edges)
1091 (window-width))))
694cf05e 1092 (split-window-horizontally (+ fortran-line-length scroll-bar-width)))
b2523604
DL
1093 (other-window 1)
1094 (switch-to-buffer " fortran-window-extra" t)
1095 (select-window (previous-window))))
3dd63760
JB
1096
1097(defun fortran-window-create-momentarily (&optional arg)
694cf05e 1098 "Momentarily make the window `fortran-line-length' (default 72) columns wide.
3dd63760 1099Optional ARG non-nil and non-unity disables the momentary feature.
fe668515 1100See also `fortran-window-create'."
3dd63760
JB
1101 (interactive "p")
1102 (if (or (not arg)
9d23c925 1103 (= arg 1))
3dd63760 1104 (save-window-excursion
9d23c925
GM
1105 (progn
1106 (condition-case nil
1107 (fortran-window-create)
1108 (error (error "No room for Fortran window")))
1109 (message "Type SPC to continue editing.")
1110 (let ((char (read-event)))
1111 (or (equal char ?\s)
1112 (setq unread-command-events (list char))))))
3dd63760
JB
1113 (fortran-window-create)))
1114
1115(defun fortran-split-line ()
1116 "Break line at point and insert continuation marker and alignment."
5b04210c 1117 (interactive "*")
3dd63760 1118 (delete-horizontal-space)
7dae727d 1119 (if (save-excursion
9d23c925
GM
1120 (let ((pos (point)))
1121 (beginning-of-line)
1122 (and (fortran-find-comment-start-skip 'all)
1123 (< (match-beginning 0) pos))))
ff451e17 1124 (insert ?\n (match-string 0))
23029d77 1125 (if indent-tabs-mode
9d23c925 1126 (insert ?\n ?\t (fortran-numerical-continuation-char))
0a39a75c
GM
1127 (insert "\n " fortran-continuation-string))) ; space after \n important
1128 (fortran-indent-line)) ; when cont string is C, c or *
1eb6bf70
DL
1129
1130(defun fortran-remove-continuation ()
5b04210c
GM
1131 "Delete any Fortran continuation characters at point.
1132Returns t if anything actually deleted."
1133 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1134 (replace-match "")
1135 (delete-indentation)
1136 t))
3dd63760 1137
46d4d7bf
DL
1138(defun fortran-join-line (arg)
1139 "Join current line to the previous one and re-indent.
1140With a prefix argument, repeat this operation that many times.
1141If the prefix argument ARG is negative, join the next -ARG lines.
1142Continuation lines are correctly handled."
1143 (interactive "*p")
7977773d 1144 (save-excursion
46d4d7bf
DL
1145 (when (> 0 arg)
1146 (setq arg (- arg))
1147 (forward-line arg))
1148 (while (not (zerop arg))
1149 (beginning-of-line)
1150 (or (fortran-remove-continuation)
1151 (delete-indentation))
1152 (setq arg (1- arg)))
7977773d
DL
1153 (fortran-indent-line)))
1154
3dd63760 1155(defun fortran-numerical-continuation-char ()
b56eb7c9 1156 "Return a digit for tab-digit style of continuation lines.
5b04210c
GM
1157If previous line is a tab-digit continuation line, return that digit
1158plus one, otherwise return 1. Zero not allowed."
3dd63760
JB
1159 (save-excursion
1160 (forward-line -1)
1161 (if (looking-at "\t[1-9]")
9d23c925 1162 (+ ?1 (% (- (char-after (1+ (point))) ?0) 9))
3dd63760
JB
1163 ?1)))
1164
556dd629 1165(put 'fortran-electric-line-number 'delete-selection t)
3dd63760
JB
1166(defun fortran-electric-line-number (arg)
1167 "Self insert, but if part of a Fortran line number indent it automatically.
7977773d 1168Auto-indent does not happen if a numeric ARG is used."
5b04210c 1169 (interactive "*P")
3dd63760 1170 (if (or arg (not fortran-electric-line-number))
60db3594 1171 (if arg
9d23c925
GM
1172 (self-insert-command (prefix-numeric-value arg))
1173 (self-insert-command 1))
3dd63760 1174 (if (or (and (= 5 (current-column))
9d23c925
GM
1175 (save-excursion
1176 (beginning-of-line)
5b04210c 1177 ;; In col 5 with only spaces to the left.
9d23c925
GM
1178 (looking-at " \\{5\\}")))
1179 (and (= (if indent-tabs-mode
1180 fortran-minimum-statement-indent-tab
1181 fortran-minimum-statement-indent-fixed) (current-column))
5b04210c 1182 ;; In col 8 with a single tab to the left.
9d23c925
GM
1183 (eq ?\t (char-after (line-beginning-position)))
1184 (not (or (eq last-command 'fortran-indent-line)
1185 (eq last-command
1186 'fortran-indent-new-line))))
1187 (save-excursion
1188 (re-search-backward "[^ \t0-9]"
1189 (line-beginning-position)
1190 t)) ; not a line number
1191 (looking-at "[0-9]")) ; within a line number
1192 (self-insert-command (prefix-numeric-value arg))
3dd63760 1193 (skip-chars-backward " \t")
1ba983e8 1194 (insert last-command-event)
3dd63760 1195 (fortran-indent-line))))
5b04210c 1196
3dd63760 1197\f
823ab5da
DL
1198(defun fortran-check-end-prog-re ()
1199 "Check a preliminary match against `fortran-end-prog-re'."
1200 ;; Having got a possible match for the subprogram end, we need a
1201 ;; match of whitespace, avoiding possible column 73+ stuff.
1202 (save-match-data
bd6cabcf 1203 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
9d23c925
GM
1204 (buffer-substring (match-end 0)
1205 (min (line-end-position)
1206 (+ fortran-line-length
694cf05e 1207 (line-beginning-position)))))))
823ab5da 1208
4a948dbf
GM
1209;; This is more complex than first expected because the beginning of a
1210;; main program may be implicit (ie not marked by a PROGRAM statement).
1211;; This would be fine (we could just go to bob in the absence of a match),
1212;; except it need not even be the first subprogram in the file (eg it
1213;; could follow a subroutine). Hence we have to search for END
1214;; statements instead.
1215;; cf fortran-beginning-of-block, f90-beginning-of-subprogram
1216;; Note that unlike the latter, we don't have to worry about nested
1217;; subprograms (?).
1218;; FIXME push-mark?
7dae727d 1219(defun fortran-beginning-of-subprogram ()
b2523604 1220 "Move point to the beginning of the current Fortran subprogram."
3dd63760 1221 (interactive)
4a948dbf
GM
1222 (let ((case-fold-search t))
1223 ;; If called already at the start of subprogram, go to the previous.
1224 (beginning-of-line (if (bolp) 0 1))
1225 (save-match-data
1226 (or (looking-at fortran-start-prog-re)
1227 ;; This leaves us at bob if before the first subprogram.
1228 (eq (fortran-previous-statement) 'first-statement)
1229 (if (or (catch 'ok
1230 (while (re-search-backward fortran-end-prog-re nil 'move)
1231 (if (fortran-check-end-prog-re) (throw 'ok t))))
1232 ;; If the search failed, must be at bob.
1233 ;; First code line is the start of the subprogram.
1234 ;; FIXME use a more rigorous test, cf fortran-next-statement?
1235 ;; Though that needs to handle continuations too.
1236 (not (looking-at "^\\([ \t]*[0-9]\\|[ \t]+[^!#]\\)")))
1237 (fortran-next-statement))))))
1238
1239;; This is simpler than f-beginning-of-s because the end of a
1240;; subprogram is never implicit.
7dae727d 1241(defun fortran-end-of-subprogram ()
b2523604 1242 "Move point to the end of the current Fortran subprogram."
3dd63760 1243 (interactive)
4a948dbf
GM
1244 (let ((case-fold-search t))
1245 (beginning-of-line)
1246 (save-match-data
1247 (while (and (re-search-forward fortran-end-prog-re nil 'move)
1248 (not (fortran-check-end-prog-re))))
1249 (forward-line))))
3dd63760 1250
3dd63760 1251(defun fortran-previous-statement ()
b2523604 1252 "Move point to beginning of the previous Fortran statement.
8edfcc7d
GM
1253Returns 'first-statement if that statement is the first
1254non-comment Fortran statement in the file, and nil otherwise.
5a73972b 1255Directive lines are treated as comments."
3dd63760
JB
1256 (interactive)
1257 (let (not-first-statement continue-test)
1258 (beginning-of-line)
1259 (setq continue-test
9d23c925
GM
1260 (and
1261 (not (looking-at fortran-comment-line-start-skip))
5a73972b 1262 (not (looking-at fortran-directive-re))
9d23c925
GM
1263 (or (looking-at
1264 (concat "[ \t]*"
1265 (regexp-quote fortran-continuation-string)))
1266 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
5b04210c 1267 (while (and (setq not-first-statement (zerop (forward-line -1)))
9d23c925 1268 (or (looking-at fortran-comment-line-start-skip)
5a73972b 1269 (looking-at fortran-directive-re)
98110b1f
GM
1270 (looking-at
1271 (concat "[ \t]*"
1272 (regexp-quote fortran-continuation-string)))
9d23c925
GM
1273 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1274 (looking-at (concat "[ \t]*" comment-start-skip)))))
3dd63760 1275 (cond ((and continue-test
9d23c925
GM
1276 (not not-first-statement))
1277 (message "Incomplete continuation statement."))
1278 (continue-test
1279 (fortran-previous-statement))
1280 ((not not-first-statement)
1281 'first-statement))))
3dd63760
JB
1282
1283(defun fortran-next-statement ()
b2523604 1284 "Move point to beginning of the next Fortran statement.
8edfcc7d
GM
1285Returns 'last-statement if that statement is the last
1286non-comment Fortran statement in the file, and nil otherwise.
5a73972b 1287Directive lines are treated as comments."
3dd63760
JB
1288 (interactive)
1289 (let (not-last-statement)
1290 (beginning-of-line)
b8cbdf43 1291 (while (and (setq not-last-statement
9d23c925
GM
1292 (and (zerop (forward-line 1))
1293 (not (eobp))))
1294 (or (looking-at fortran-comment-line-start-skip)
5a73972b 1295 (looking-at fortran-directive-re)
9d23c925
GM
1296 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1297 (looking-at (concat "[ \t]*" comment-start-skip)))))
3dd63760 1298 (if (not not-last-statement)
9d23c925 1299 'last-statement)))
5b04210c 1300
8cb8832f
GM
1301(defun fortran-looking-at-if-then ()
1302 "Return non-nil if at the start of a line with an IF ... THEN statement."
1303 ;; cf f90-looking-at-if-then.
1304 (let ((p (point))
1305 (i (fortran-beginning-if)))
1306 (if i
1307 (save-excursion
1308 (goto-char i)
1309 (beginning-of-line)
1310 (= (point) p)))))
1311
1312;; Used in hs-special-modes-alist.
1313(defun fortran-end-of-block (&optional num)
1314 "Move point forward to the end of the current code block.
1315With optional argument NUM, go forward that many balanced blocks.
1316If NUM is negative, go backward to the start of a block. Does
1317not check for consistency of block types. Interactively, pushes
1318mark before moving point."
1319 (interactive "p")
d47afbc0 1320 (if (called-interactively-p 'any) (push-mark (point) t))
8cb8832f
GM
1321 (and num (< num 0) (fortran-beginning-of-block (- num)))
1322 (let ((case-fold-search t)
1323 (count (or num 1)))
1324 (end-of-line)
1325 (while (and (> count 0)
1326 (re-search-forward
1327 (concat "\\(" fortran-blocks-re
1328 (if fortran-check-all-num-for-matching-do
1329 "\\|^[ \t]*[0-9]+" "")
1330 "\\|continue\\|end\\)\\>")
1331 nil 'move))
1332 (beginning-of-line)
1333 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1334 (fortran-looking-at-if-then)
1335 (looking-at fortran-start-block-re))
1336 (setq count (1+ count))
1337 (if (or (looking-at fortran-end-block-re)
1338 (and (or (looking-at "^[0-9 \t]*continue")
1339 (and fortran-check-all-num-for-matching-do
1340 (looking-at "[ \t]*[0-9]+")))
1341 (fortran-check-for-matching-do)))
1342 (setq count (1- count))))
1343 (end-of-line))
1344 (if (> count 0) (error "Missing block end"))))
1345
1346(defun fortran-beginning-of-block (&optional num)
1347 "Move point backwards to the start of the current code block.
1348With optional argument NUM, go backward that many balanced
1349blocks. If NUM is negative, go forward to the end of a block.
1350Does not check for consistency of block types. Interactively,
1351pushes mark before moving point."
1352 (interactive "p")
d47afbc0 1353 (if (called-interactively-p 'any) (push-mark (point) t))
8cb8832f
GM
1354 (and num (< num 0) (fortran-end-of-block (- num)))
1355 (let ((case-fold-search t)
1356 (count (or num 1)))
1357 (beginning-of-line)
1358 (while (and (> count 0)
1359 (re-search-backward
1360 (concat "\\(" fortran-blocks-re
1361 (if fortran-check-all-num-for-matching-do
1362 "\\|^[ \t]*[0-9]+" "")
1363 "\\|continue\\|end\\)\\>")
1364 nil 'move))
1365 (beginning-of-line)
1366 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1367 (fortran-looking-at-if-then)
1368 (looking-at fortran-start-block-re))
1369 (setq count (1- count))
1370 (if (or (looking-at fortran-end-block-re)
1371 (and (or (looking-at "^[0-9 \t]*continue")
1372 (and fortran-check-all-num-for-matching-do
1373 (looking-at "[ \t]*[0-9]+")))
1374 (fortran-check-for-matching-do)))
1375 (setq count (1+ count)))))
1376 ;; Includes an un-named main program block.
1377 (if (> count 0) (error "Missing block start"))))
1378
3dd63760 1379\f
7dae727d
DL
1380(defun fortran-blink-match (regex keyword find-begin)
1381 "From a line matching REGEX, blink matching KEYWORD statement line.
1382Use function FIND-BEGIN to match it."
1eb6bf70 1383 (let ((top-of-window (window-start))
9d23c925
GM
1384 (end-point (point))
1385 (case-fold-search t)
1386 matching
1387 message)
5b04210c
GM
1388 (when (save-excursion
1389 (beginning-of-line)
1390 (skip-chars-forward " \t0-9")
1391 (looking-at regex))
1392 (if (not (setq matching (funcall find-begin)))
1393 (setq message (concat "No matching " keyword "."))
1394 (if (< matching top-of-window)
1395 (save-excursion
1396 (goto-char matching)
1397 (beginning-of-line)
1398 (setq message
1399 (concat "Matches "
1400 (buffer-substring (point)
1401 (line-end-position)))))))
1402 (if message
1403 (message "%s" message)
1404 (goto-char matching)
7533f3b5 1405 (sit-for blink-matching-delay)
5b04210c 1406 (goto-char end-point)))))
7dae727d
DL
1407
1408(defun fortran-blink-matching-if ()
1409 "From an ENDIF or ELSE statement, blink the matching IF statement."
1410 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
9d23c925 1411 "if" #'fortran-beginning-if))
947388af
RS
1412
1413(defun fortran-blink-matching-do ()
45cf60ae 1414 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
7dae727d 1415 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
c5af0a18
RS
1416
1417(defun fortran-mark-do ()
60db3594 1418 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
c5af0a18
RS
1419The marks are pushed."
1420 (interactive)
1421 (let (enddo-point do-point)
1422 (if (setq enddo-point (fortran-end-do))
1423 (if (not (setq do-point (fortran-beginning-do)))
1424 (message "No matching do.")
c5af0a18
RS
1425 (goto-char enddo-point)
1426 (push-mark)
1427 (goto-char do-point)))))
1428
1429(defun fortran-end-do ()
45cf60ae
DL
1430 "Search forward for first unmatched ENDDO.
1431Return point or nil."
1eb6bf70
DL
1432 (let ((case-fold-search t))
1433 (if (save-excursion (beginning-of-line)
9d23c925
GM
1434 (skip-chars-forward " \t0-9")
1435 (looking-at "end[ \t]*do\\b"))
1436 ;; Sitting on one.
1437 (match-beginning 0)
1eb6bf70
DL
1438 ;; Search for one.
1439 (save-excursion
9d23c925
GM
1440 (let ((count 1))
1441 (while (and (not (zerop count))
1442 (not (eq (fortran-next-statement) 'last-statement))
1443 ;; Keep local to subprogram.
1444 (not (and (looking-at fortran-end-prog-re)
1445 (fortran-check-end-prog-re))))
1446 (skip-chars-forward " \t0-9")
1447 (cond ((looking-at "end[ \t]*do\\b")
1448 (setq count (1- count)))
1449 ((looking-at
1450 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1451 (setq count (1+ count)))))
1452 (and (zerop count)
1453 ;; All pairs accounted for.
1454 (point)))))))
c5af0a18
RS
1455
1456(defun fortran-beginning-do ()
45cf60ae 1457 "Search backwards for first unmatched DO [WHILE].
845d331e
GM
1458Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1459 (let ((case-fold-search t)
1460 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
7dae727d 1461 (if (save-excursion
9d23c925
GM
1462 (beginning-of-line)
1463 (skip-chars-forward " \t0-9")
1464 (looking-at dostart-re))
1465 ;; Sitting on one.
1466 (match-beginning 0)
1eb6bf70
DL
1467 ;; Search for one.
1468 (save-excursion
9d23c925
GM
1469 (let ((count 1))
1470 (while (and (not (zerop count))
1471 (not (eq (fortran-previous-statement) 'first-statement))
1472 ;; Keep local to subprogram.
1473 (not (and (looking-at fortran-end-prog-re)
1474 (fortran-check-end-prog-re))))
1475 (skip-chars-forward " \t0-9")
1476 (cond ((looking-at dostart-re)
1477 (setq count (1- count)))
845d331e 1478 ;; Note labelled loop ends not considered.
9d23c925
GM
1479 ((looking-at "end[ \t]*do\\b")
1480 (setq count (1+ count)))))
1481 (and (zerop count)
1482 ;; All pairs accounted for.
1483 (point)))))))
c5af0a18
RS
1484
1485(defun fortran-mark-if ()
1486 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1487The marks are pushed."
1488 (interactive)
1489 (let (endif-point if-point)
1490 (if (setq endif-point (fortran-end-if))
1491 (if (not (setq if-point (fortran-beginning-if)))
1492 (message "No matching if.")
1493 ;; Set mark, move point.
1494 (goto-char endif-point)
1495 (push-mark)
1496 (goto-char if-point)))))
1497
1498(defun fortran-end-if ()
45cf60ae
DL
1499 "Search forwards for first unmatched ENDIF.
1500Return point or nil."
1eb6bf70
DL
1501 (let ((case-fold-search t))
1502 (if (save-excursion (beginning-of-line)
9d23c925
GM
1503 (skip-chars-forward " \t0-9")
1504 (looking-at "end[ \t]*if\\b"))
1505 ;; Sitting on one.
1506 (match-beginning 0)
1eb6bf70
DL
1507 ;; Search for one. The point has been already been moved to first
1508 ;; letter on line but this should not cause troubles.
1509 (save-excursion
9d23c925
GM
1510 (let ((count 1))
1511 (while (and (not (zerop count))
1512 (not (eq (fortran-next-statement) 'last-statement))
1513 ;; Keep local to subprogram.
1514 (not (and (looking-at fortran-end-prog-re)
1515 (fortran-check-end-prog-re))))
1516 (skip-chars-forward " \t0-9")
1517 (cond ((looking-at "end[ \t]*if\\b")
1518 (setq count (1- count)))
1519 ((looking-at fortran-if-start-re)
1520 (save-excursion
1521 (if (or
1522 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1523 (let (then-test) ; multi-line if-then
1524 (while
1525 (and
1526 (zerop (forward-line 1))
1527 ;; Search forward for then.
1528 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1529 (not
1530 (setq then-test
1531 (looking-at
1532 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1533 then-test))
1534 (setq count (1+ count)))))))
1535 (and (zerop count)
1536 ;; All pairs accounted for.
1537 (point)))))))
c5af0a18
RS
1538
1539(defun fortran-beginning-if ()
45cf60ae
DL
1540 "Search backwards for first unmatched IF-THEN.
1541Return point or nil."
1eb6bf70
DL
1542 (let ((case-fold-search t))
1543 (if (save-excursion
9d23c925
GM
1544 ;; May be sitting on multi-line if-then statement, first
1545 ;; move to beginning of current statement. Note:
1546 ;; `fortran-previous-statement' moves to previous statement
1547 ;; *unless* current statement is first one. Only move
1548 ;; forward if not first-statement.
1549 (if (not (eq (fortran-previous-statement) 'first-statement))
1550 (fortran-next-statement))
1551 (skip-chars-forward " \t0-9")
1552 (and
1553 (looking-at fortran-if-start-re)
1554 (save-match-data
1555 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1556 ;; Multi-line if-then.
1557 (let (then-test)
1558 (while
5b04210c 1559 (and (zerop (forward-line 1))
9d23c925
GM
1560 ;; Search forward for then.
1561 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1562 (not
1563 (setq then-test
1564 (looking-at
1565 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1566 then-test)))))
1567 ;; Sitting on one.
1568 (match-beginning 0)
1eb6bf70
DL
1569 ;; Search for one.
1570 (save-excursion
9d23c925
GM
1571 (let ((count 1))
1572 (while (and (not (zerop count))
1573 (not (eq (fortran-previous-statement) 'first-statement))
1574 ;; Keep local to subprogram.
1575 (not (and (looking-at fortran-end-prog-re)
1576 (fortran-check-end-prog-re))))
1577 (skip-chars-forward " \t0-9")
1578 (cond ((looking-at fortran-if-start-re)
1579 (save-excursion
1580 (if (or
1581 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1582 (let (then-test) ; multi-line if-then
1583 (while
1584 (and
1585 (zerop (forward-line 1))
1586 ;; Search forward for then.
1587 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1588 (not
1589 (setq then-test
1590 (looking-at
1591 (concat ".*then\\b[ \t]*"
1592 "[^ \t(=a-z0-9]"))))))
1593 then-test))
1594 (setq count (1- count)))))
1595 ((looking-at "end[ \t]*if\\b")
1596 (setq count (1+ count)))))
1597 (and (zerop count)
1598 ;; All pairs accounted for.
1599 (point)))))))
5b04210c 1600
3dd63760
JB
1601\f
1602(defun fortran-indent-line ()
7977773d 1603 "Indent current Fortran line based on its contents and on previous lines."
5b04210c 1604 (interactive "*")
1eb6bf70 1605 (let ((cfi (fortran-calculate-indent)))
3dd63760
JB
1606 (save-excursion
1607 (beginning-of-line)
1608 (if (or (not (= cfi (fortran-current-line-indentation)))
9d23c925
GM
1609 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1610 (not (fortran-line-number-indented-correctly-p))))
1611 (fortran-indent-to-column cfi)
1612 (beginning-of-line)
1613 (if (fortran-find-comment-start-skip)
1614 (fortran-indent-comment))))
3dd63760
JB
1615 ;; Never leave point in left margin.
1616 (if (< (current-column) cfi)
9d23c925 1617 (move-to-column cfi))
5b04210c
GM
1618 (and auto-fill-function
1619 (> (save-excursion (end-of-line) (current-column))
1620 fill-column)
1621 (save-excursion
1622 (end-of-line)
1623 (fortran-fill)))
1624 (when fortran-blink-matching-if
1625 (fortran-blink-matching-if)
1626 (fortran-blink-matching-do))))
3dd63760 1627
315aa1de 1628(defun fortran-auto-fill ()
5b04210c 1629 "Function to use for `normal-auto-fill-function' in Fortran mode."
315aa1de
DL
1630 (if (> (current-column) (current-fill-column))
1631 (let ((cfi (fortran-calculate-indent)))
9d23c925
GM
1632 (save-excursion
1633 (beginning-of-line)
1634 (if (or (not (= cfi (fortran-current-line-indentation)))
1635 (and (re-search-forward "^[ \t]*[0-9]+"
1636 (+ (point) 4) t)
1637 (not (fortran-line-number-indented-correctly-p))))
1638 (fortran-indent-to-column cfi)
1639 (beginning-of-line)
1640 (if (fortran-find-comment-start-skip)
1641 (fortran-indent-comment))))
1642 (fortran-fill)
1643 ;; Never leave point in left margin.
1644 (if (< (current-column) cfi)
1645 (move-to-column cfi)))))
315aa1de 1646
5aca2648
DL
1647;; Historically this was a separate function which advertised itself
1648;; as reindenting but only did so where `most likely to be necessary'.
1649(defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
b8cbdf43 1650
3dd63760 1651(defun fortran-indent-subprogram ()
5b04210c
GM
1652 "Properly indent the Fortran subprogram containing point."
1653 (interactive "*")
3dd63760 1654 (save-excursion
7a7db8e5 1655 (mark-defun)
c4c42b2e
DL
1656 (message "Indenting subprogram...")
1657 (indent-region (point) (mark) nil))
3dd63760
JB
1658 (message "Indenting subprogram...done."))
1659
1eb6bf70 1660(defun fortran-calculate-indent ()
b8cbdf43 1661 "Calculates the Fortran indent column based on previous lines."
3dd63760 1662 (let (icol first-statement (case-fold-search t)
9d23c925
GM
1663 (fortran-minimum-statement-indent
1664 (if indent-tabs-mode
1665 fortran-minimum-statement-indent-tab
1666 fortran-minimum-statement-indent-fixed)))
3dd63760
JB
1667 (save-excursion
1668 (setq first-statement (fortran-previous-statement))
1669 (if first-statement
9d23c925 1670 (setq icol fortran-minimum-statement-indent)
98110b1f
GM
1671 (if (= (point) (point-min))
1672 (setq icol fortran-minimum-statement-indent)
1673 (setq icol (fortran-current-line-indentation)))
1674 (skip-chars-forward " \t0-9")
1675 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1676 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
9d23c925 1677 (let (then-test) ; multi-line if-then
5b04210c 1678 (while (and (zerop (forward-line 1))
0a39a75c 1679 ;; Search forward for then.
98110b1f
GM
1680 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1681 (not (setq then-test
1682 (looking-at
1683 ".*then\\b[ \t]\
b8cbdf43 1684*[^ \t_$(=a-z0-9]")))))
98110b1f
GM
1685 then-test))
1686 (setq icol (+ icol fortran-if-indent))))
1687 ((looking-at "else\\(if\\)?\\b")
1688 (setq icol (+ icol fortran-if-indent)))
1689 ((looking-at "select[ \t]*case[ \t](.*)")
1690 (setq icol (+ icol fortran-if-indent)))
1691 ((looking-at "case[ \t]*(.*)")
1692 (setq icol (+ icol fortran-if-indent)))
1693 ((looking-at "case[ \t]*default\\b")
1694 (setq icol (+ icol fortran-if-indent)))
1695 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1696 (setq icol (+ icol fortran-if-indent)))
1697 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1698 (setq icol (+ icol fortran-if-indent)))
1699 ((looking-at "do\\b")
1700 (setq icol (+ icol fortran-do-indent)))
1701 ((looking-at
1702 "\\(structure\\|union\\|map\\|interface\\)\
7dae727d 1703\\b[ \t]*[^ \t=(a-z]")
98110b1f
GM
1704 (setq icol (+ icol fortran-structure-indent)))
1705 ((and (looking-at fortran-end-prog-re1)
1706 (fortran-check-end-prog-re))
0a39a75c 1707 ;; Previous END resets indent to minimum.
dd08226a
GM
1708 (setq icol fortran-minimum-statement-indent))
1709 ;; Previous statement was a numbered DO loop without a
1710 ;; closing CONTINUE or END DO, so we indented the
1711 ;; terminator like the loop body.
1712 ((and fortran-check-all-num-for-matching-do
1713 (not (looking-at "\\(continue\\|end[ \t]*do\\)\\>"))
1714 (progn
1715 (beginning-of-line)
1716 (and (looking-at "[ \t]*[0-9]+")
1717 (fortran-check-for-matching-do))))
1718 (setq icol (- icol fortran-do-indent))))))
3dd63760
JB
1719 (save-excursion
1720 (beginning-of-line)
1721 (cond ((looking-at "[ \t]*$"))
5a73972b 1722 ;; Check for directive before comment, so as not to indent.
9d23c925
GM
1723 ((looking-at fortran-directive-re)
1724 (setq fortran-minimum-statement-indent 0 icol 0))
1725 ((looking-at fortran-comment-line-start-skip)
1726 (cond ((eq fortran-comment-indent-style 'relative)
1727 (setq icol (+ icol fortran-comment-line-extra-indent)))
1728 ((eq fortran-comment-indent-style 'fixed)
1729 (setq icol (+ fortran-minimum-statement-indent
1730 fortran-comment-line-extra-indent))))
1731 (setq fortran-minimum-statement-indent 0))
1732 ((or (looking-at (concat "[ \t]*"
1733 (regexp-quote
1734 fortran-continuation-string)))
1735 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
98110b1f
GM
1736 (skip-chars-forward " \t")
1737 ;; Do not introduce extra whitespace into a broken string.
1738 (setq icol
1739 (if (fortran-is-in-string-p (point))
1740 6
1741 (+ icol fortran-continuation-indent))))
9d23c925 1742 (first-statement)
dd08226a
GM
1743 ;; The terminating statement is actually part of the
1744 ;; loop body, so unless it is a CONTINUE or END DO, we
1745 ;; indent it like the loop body (see above).
9d23c925 1746 ((and fortran-check-all-num-for-matching-do
dd08226a
GM
1747 (looking-at "[ \t]*[0-9]+[ \t]*\
1748\\(continue\\|end[ \t]*do\\)\\>")
9d23c925
GM
1749 (fortran-check-for-matching-do))
1750 (setq icol (- icol fortran-do-indent)))
1751 (t
1752 (skip-chars-forward " \t0-9")
1753 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1754 (setq icol (- icol fortran-if-indent)))
1755 ((looking-at "else\\(if\\)?\\b")
1756 (setq icol (- icol fortran-if-indent)))
1eb6bf70 1757 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
9d23c925
GM
1758 (setq icol (- icol fortran-if-indent)))
1759 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1760 (setq icol (- icol fortran-if-indent)))
1761 ((and (looking-at "continue\\b")
1762 (fortran-check-for-matching-do))
1763 (setq icol (- icol fortran-do-indent)))
1764 ((looking-at "end[ \t]*do\\b")
1765 (setq icol (- icol fortran-do-indent)))
1766 ((looking-at "end[ \t]*\
5a8d870b 1767\\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
9d23c925
GM
1768 (setq icol (- icol fortran-structure-indent)))
1769 ((and (looking-at fortran-end-prog-re1)
1770 (fortran-check-end-prog-re)
1771 (not (= icol fortran-minimum-statement-indent)))
1772 (message "Warning: `end' not in column %d. Probably\
3dd63760
JB
1773 an unclosed block." fortran-minimum-statement-indent))))))
1774 (max fortran-minimum-statement-indent icol)))
5b04210c 1775
3dd63760
JB
1776\f
1777(defun fortran-current-line-indentation ()
1778 "Indentation of current line, ignoring Fortran line number or continuation.
1779This is the column position of the first non-whitespace character
1780aside from the line number and/or column 5/8 line-continuation character.
1781For comment lines, returns indentation of the first
1782non-indentation text within the comment."
1783 (save-excursion
1784 (beginning-of-line)
7dae727d 1785 (cond ((looking-at fortran-comment-line-start-skip)
9d23c925
GM
1786 (goto-char (match-end 0))
1787 (skip-chars-forward
1788 (if (stringp fortran-comment-indent-char)
1789 fortran-comment-indent-char
1790 (char-to-string fortran-comment-indent-char))))
1791 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1792 (goto-char (match-end 0)))
1793 (t
1794 ;; Move past line number.
1795 (skip-chars-forward "[ \t0-9]")))
3dd63760
JB
1796 ;; Move past whitespace.
1797 (skip-chars-forward " \t")
1798 (current-column)))
1799
1800(defun fortran-indent-to-column (col)
5b04210c 1801 "Indent current line to column COL.
3dd63760
JB
1802notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1803 line, and this continuation character is retained on indentation;
b8cbdf43
RS
1804 2) If `fortran-continuation-string' is the first non-whitespace
1805 character, this is a continuation line;
3dd63760
JB
1806 3) A non-continuation line which has a number as the first
1807 non-whitespace character is a numbered line.
b8cbdf43 1808 4) A TAB followed by a digit indicates a continuation line."
3dd63760
JB
1809 (save-excursion
1810 (beginning-of-line)
7dae727d 1811 (if (looking-at fortran-comment-line-start-skip)
9d23c925
GM
1812 (if fortran-comment-indent-style
1813 (let* ((char (if (stringp fortran-comment-indent-char)
1814 (aref fortran-comment-indent-char 0)
1815 fortran-comment-indent-char))
1816 (chars (string ?\s ?\t char)))
1817 (goto-char (match-end 0))
1818 (skip-chars-backward chars)
1819 (delete-region (point) (progn (skip-chars-forward chars)
1820 (point)))
1821 (insert-char char (- col (current-column)))))
3dd63760 1822 (if (looking-at "\t[1-9]")
9d23c925
GM
1823 (if indent-tabs-mode
1824 (goto-char (match-end 0))
1825 (delete-char 2)
1826 (insert-char ?\s 5)
1827 (insert fortran-continuation-string))
1828 (if (looking-at " \\{5\\}[^ 0\n]")
1829 (if indent-tabs-mode
1830 (progn (delete-char 6)
1831 (insert ?\t (fortran-numerical-continuation-char) 1))
1832 (forward-char 6))
1833 (delete-horizontal-space)
1834 ;; Put line number in columns 0-4, or
0a39a75c 1835 ;; continuation character in column 5.
9d23c925
GM
1836 (cond ((eobp))
1837 ((looking-at (regexp-quote fortran-continuation-string))
1838 (if indent-tabs-mode
1839 (progn
1840 (indent-to
1841 (if indent-tabs-mode
1842 fortran-minimum-statement-indent-tab
1843 fortran-minimum-statement-indent-fixed))
1844 (delete-char 1)
1845 (insert-char (fortran-numerical-continuation-char) 1))
1846 (indent-to 5)
1847 (forward-char 1)))
1848 ((looking-at "[0-9]+")
1849 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1850 (if (< extra-space 0)
1851 (message "Warning: line number exceeds 5-digit limit.")
1852 (indent-to (min fortran-line-number-indent extra-space))))
1853 (skip-chars-forward "0-9")))))
3dd63760
JB
1854 ;; Point is now after any continuation character or line number.
1855 ;; Put body of statement where specified.
1856 (delete-horizontal-space)
1857 (indent-to col)
1858 ;; Indent any comment following code on the same line.
5b04210c
GM
1859 (when (fortran-find-comment-start-skip)
1860 (goto-char (match-beginning 0))
1861 (unless (= (current-column) (fortran-comment-indent))
1862 (delete-horizontal-space)
1863 (indent-to (fortran-comment-indent)))))))
3dd63760
JB
1864
1865(defun fortran-line-number-indented-correctly-p ()
1866 "Return t if current line's line number is correctly indented.
1867Do not call if there is no line number."
1868 (save-excursion
1869 (beginning-of-line)
1870 (skip-chars-forward " \t")
1871 (and (<= (current-column) fortran-line-number-indent)
9d23c925
GM
1872 (or (= (current-column) fortran-line-number-indent)
1873 (progn (skip-chars-forward "0-9")
1874 (= (current-column) 5))))))
3dd63760
JB
1875
1876(defun fortran-check-for-matching-do ()
7977773d
DL
1877 "When called from a numbered statement, return t if matching DO is found.
1878Otherwise return nil."
7dae727d 1879 (let ((case-fold-search t)
9d23c925 1880 charnum)
3dd63760
JB
1881 (save-excursion
1882 (beginning-of-line)
5b04210c
GM
1883 (when (looking-at "[ \t]*[0-9]+")
1884 (skip-chars-forward " \t")
9d23c925 1885 (skip-chars-forward "0") ; skip past leading zeros
5b04210c
GM
1886 (setq charnum
1887 (buffer-substring (point) (progn
1888 (skip-chars-forward "0-9")
1889 (point))))
1890 (beginning-of-line)
1891 (save-restriction
1892 (save-excursion
1893 (narrow-to-defun)
1894 (and (re-search-backward
1895 (concat
1896 "\\(^[ \t0-9]*do[ \t]*0*"
1897 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1898 charnum "\\b\\)")
1899 nil t)
1900 (looking-at
1901 (concat "^[ \t0-9]*do[ \t]*0*"
1902 charnum)))))))))
3dd63760 1903
ff451e17 1904(defun fortran-find-comment-start-skip (&optional all)
b8cbdf43 1905 "Move to past `comment-start-skip' found on current line.
ff451e17
SM
1906Return non-nil if `comment-start-skip' found, nil if not.
1907If ALL is nil, only match comments that start in column > 0."
ff451e17
SM
1908 ;; Hopefully at some point we can just use the line below! -stef
1909 ;; (comment-search-forward (line-end-position) t))
1910 (when (or all comment-start-skip)
1911 (let ((pos (point))
9d23c925
GM
1912 (css (if comment-start-skip
1913 (concat fortran-comment-line-start-skip
1914 "\\|" comment-start-skip)
1915 fortran-comment-line-start-skip)))
ff451e17 1916 (when (re-search-forward css (line-end-position) t)
9d23c925
GM
1917 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1918 (or (save-match-data
1919 (not (fortran-is-in-string-p (match-beginning 0))))
1920 ;; Recurse for rest of line.
1921 (fortran-find-comment-start-skip all)))
1922 (point)
1923 (goto-char pos)
1924 nil)))))
f022dd89 1925
5b04210c 1926;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1eb6bf70 1927;; Test if TAB format continuation lines work.
b56eb7c9 1928(defun fortran-is-in-string-p (where)
e7f767c2 1929 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
b56eb7c9
RS
1930 (save-excursion
1931 (goto-char where)
1932 (cond
9d23c925
GM
1933 ((bolp) nil) ; bol is never inside a string
1934 ((save-excursion ; comment lines too
1935 (beginning-of-line)
1936 (looking-at fortran-comment-line-start-skip)) nil)
0a39a75c 1937 (t (let ((parse-state '(0 nil nil nil nil nil 0))
9d23c925
GM
1938 (quoted-comment-start (if comment-start
1939 (regexp-quote comment-start)))
1940 (not-done t)
1941 parse-limit end-of-line)
1942 ;; Move to start of current statement.
1943 (fortran-next-statement)
1944 (fortran-previous-statement)
1945 ;; Now parse up to WHERE.
1946 (while not-done
1947 (if (or ;; Skip to next line if:
1948 ;; - comment line?
1949 (looking-at fortran-comment-line-start-skip)
1950 ;; - at end of line?
1951 (eolp)
1952 ;; - not in a string and after comment-start?
1953 (and (not (nth 3 parse-state))
1954 comment-start
1955 (equal comment-start
1956 (char-to-string (preceding-char)))))
1957 (if (> (forward-line) 0)
1958 (setq not-done nil))
1959 ;; else:
1960 ;; If we are at beginning of code line, skip any
1961 ;; whitespace, labels and tab continuation markers.
1962 (if (bolp) (skip-chars-forward " \t0-9"))
1963 ;; If we are in column <= 5 now, check for continuation char.
1964 (cond ((= 5 (current-column)) (forward-char 1))
1965 ((and (< (current-column) 5)
1966 (equal fortran-continuation-string
1967 (char-to-string (following-char)))
1968 (forward-char 1))))
1969 ;; Find out parse-limit from here.
1970 (setq end-of-line (line-end-position))
1971 (setq parse-limit (min where end-of-line))
1972 ;; Parse max up to comment-start, if non-nil and in current line.
1973 (if comment-start
1974 (save-excursion
1975 (if (re-search-forward quoted-comment-start end-of-line t)
1976 (setq parse-limit (min (point) parse-limit)))))
1977 ;; Now parse if still in limits.
1978 (if (< (point) where)
1979 (setq parse-state (parse-partial-sexp
1980 (point) parse-limit nil nil parse-state))
1981 (setq not-done nil))))
1982 ;; Result.
1983 (nth 3 parse-state))))))
b8cbdf43 1984
7aabd23e
DL
1985;; From old version.
1986(defalias 'fortran-auto-fill-mode 'auto-fill-mode)
b8cbdf43 1987
4254fe58 1988(defun fortran-fill ()
5b04210c 1989 "Fill the current line at an appropriate point(s)."
315aa1de 1990 (let* ((auto-fill-function #'fortran-auto-fill)
9d23c925
GM
1991 (opoint (point))
1992 (bol (line-beginning-position))
1993 (eol (line-end-position))
1994 (bos (min eol (+ bol (fortran-current-line-indentation))))
337c50a5
GM
1995 ;; If in a string at fill-column, break it either before the
1996 ;; initial quote, or at fill-col (if string is too long).
9d23c925
GM
1997 (quote
1998 (save-excursion
1999 (goto-char bol)
2000 ;; OK to break quotes on comment lines.
2001 (unless (looking-at fortran-comment-line-start-skip)
98110b1f 2002 (let (fcpoint start)
337c50a5
GM
2003 (move-to-column fill-column)
2004 (when (fortran-is-in-string-p (setq fcpoint (point)))
2005 (save-excursion
2006 (re-search-backward "\\S\"\\s\"\\S\"?" bol t)
2007 (setq start
2008 (if fortran-break-before-delimiters
2009 (point)
2010 (1+ (point)))))
2011 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t)
2012 (backward-char 2))
8cb8832f
GM
2013 ;; If the current string is longer than (fill-column
2014 ;; - 6) chars, break it at the fill column (else
2015 ;; infinite loop).
337c50a5
GM
2016 (if (> (- (point) start)
2017 (- fill-column 6 fortran-continuation-indent))
2018 fcpoint
2019 start))))))
9d23c925
GM
2020 ;; Decide where to split the line. If a position for a quoted
2021 ;; string was found above then use that, else break the line
2022 ;; before/after the last delimiter.
2023 (fill-point
2024 (or quote
2025 (save-excursion
337c50a5
GM
2026 ;; If f-b-b-d is t, have an extra column to play with,
2027 ;; since delimiter gets shifted to new line.
2028 (move-to-column (if fortran-break-before-delimiters
2029 (1+ fill-column)
2030 fill-column))
2031 (let ((repeat t))
2032 (while repeat
2033 (setq repeat nil)
2034 ;; Adapted from f90-find-breakpoint.
8aa7b879 2035 (re-search-backward fortran-break-delimiters-re bol)
337c50a5
GM
2036 (if (not fortran-break-before-delimiters)
2037 (if (looking-at fortran-no-break-re)
2038 ;; Deal with cases such as "**" split over
2039 ;; fill-col. Simpler alternative would be
2040 ;; to start from (1- fill-column) above.
2041 (if (> (+ 2 (current-column)) fill-column)
2042 (setq repeat t)
2043 (forward-char 2))
2044 (forward-char 1))
2045 (backward-char)
2046 (or (looking-at fortran-no-break-re)
2047 (forward-char)))))
2048 ;; Line indented beyond fill-column?
9d23c925 2049 (when (<= (point) bos)
98110b1f 2050 (move-to-column (1+ fill-column))
0a39a75c 2051 ;; What is this doing???
98110b1f
GM
2052 (or (re-search-forward "[\t\n,'+-/*)=]" eol t)
2053 (goto-char bol)))
9d23c925
GM
2054 (if (bolp)
2055 (re-search-forward "[ \t]" opoint t))
337c50a5 2056 (point)))))
0a39a75c 2057 ;; If we are in an in-line comment, don't break unless the
b8cbdf43
RS
2058 ;; line of code is longer than it should be. Otherwise
2059 ;; break the line at the column computed above.
2060 ;;
0a39a75c
GM
2061 ;; Need to use fortran-find-comment-start-skip to make sure that
2062 ;; quoted !'s don't prevent a break.
ff451e17 2063 (when (and (save-excursion
9d23c925
GM
2064 (beginning-of-line)
2065 (if (not (fortran-find-comment-start-skip))
0ab47edc 2066 t
9d23c925
GM
2067 (goto-char (match-beginning 0))
2068 (>= (point) fill-point)))
2069 (save-excursion
2070 (goto-char fill-point)
2071 (not (bolp)))
2072 (> (save-excursion
2073 (goto-char opoint)
2074 (current-column))
2075 (min (1+ fill-column)
2076 (+ (fortran-calculate-indent)
2077 fortran-continuation-indent))))
ff451e17
SM
2078 (goto-char fill-point)
2079 (fortran-break-line)
2080 (end-of-line))))
7dae727d 2081
b8cbdf43 2082(defun fortran-break-line ()
5b04210c 2083 "Call `fortran-split-line'. Joins continuation lines first, then refills."
5d724235 2084 (let ((bol (line-beginning-position))
9d23c925
GM
2085 (comment-string
2086 (save-excursion
2087 (if (fortran-find-comment-start-skip)
2088 (delete-and-extract-region
2089 (match-beginning 0) (line-end-position))))))
0a39a75c 2090 ;; Forward line 1 really needs to go to next non white line.
1eb6bf70 2091 (if (save-excursion (forward-line)
9d23c925
GM
2092 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
2093 (progn
2094 (end-of-line)
2095 (delete-region (point) (match-end 0))
2096 (delete-horizontal-space)
2097 (fortran-fill))
b8cbdf43
RS
2098 (fortran-split-line))
2099 (if comment-string
9d23c925
GM
2100 (save-excursion
2101 (goto-char bol)
2102 (end-of-line)
2103 (delete-horizontal-space)
2104 (indent-to (fortran-comment-indent))
2105 (insert comment-string)))))
b8cbdf43 2106
23029d77 2107(defun fortran-analyze-file-format ()
7977773d 2108 "Return nil if fixed format is used, t if TAB formatting is used.
5b04210c
GM
2109Use `fortran-tab-mode-default' if no non-comment statements are found
2110before the end or in the first `fortran-analyze-depth' lines."
23029d77
JB
2111 (let ((i 0))
2112 (save-excursion
2113 (goto-char (point-min))
23029d77 2114 (while (not (or
9d23c925
GM
2115 (eobp)
2116 (eq (char-after) ?\t)
2117 (looking-at " \\{6\\}")
2118 (> i fortran-analyze-depth)))
2119 (forward-line)
2120 (setq i (1+ i)))
23029d77 2121 (cond
7dae727d 2122 ((eq (char-after) ?\t) t)
315aa1de 2123 ((looking-at " \\{6\\}") nil)
a7113309 2124 (t fortran-tab-mode-default)))))
23029d77 2125
1eb6bf70
DL
2126(defun fortran-fill-paragraph (&optional justify)
2127 "Fill surrounding comment block as paragraphs, else fill statement.
5b04210c
GM
2128Intended as the value of `fill-paragraph-function'.
2129A comment block is filled by calling `fill-comment-paragraph' with
2130argument JUSTIFY, otherwise `fortran-fill-statement' is called.
2131Always returns non-nil (to prevent `fill-paragraph' being called)."
2132 (interactive "*P")
2bcfe15e
SM
2133 (or (fill-comment-paragraph justify)
2134 (fortran-fill-statement)
2135 t))
1eb6bf70
DL
2136
2137(defun fortran-fill-statement ()
5b04210c
GM
2138 "Fill a Fortran statement up to `fill-column'."
2139 (interactive "*")
315aa1de 2140 (let ((auto-fill-function #'fortran-auto-fill))
5b04210c
GM
2141 (unless (save-excursion
2142 (beginning-of-line)
2143 (or (looking-at "[ \t]*$")
2144 (looking-at fortran-comment-line-start-skip)
2145 (and comment-start-skip
2146 (looking-at (concat "[ \t]*" comment-start-skip)))))
2147 (save-excursion
2148 ;; Find beginning of statement.
2149 (fortran-next-statement)
2150 (fortran-previous-statement)
2151 ;; Re-indent initially.
2152 (fortran-indent-line)
2153 ;; Replace newline plus continuation field plus indentation with
2154 ;; single space.
2155 (while (progn
2156 (forward-line)
2157 (fortran-remove-continuation)))
2158 (fortran-previous-statement)))
e04196d3 2159 (fortran-indent-line)))
1eb6bf70 2160
cfe9d0b5 2161(defun fortran-strip-sequence-nos (&optional do-space)
694cf05e
GM
2162 "Delete all text in column `fortran-line-length' (default 72) and up.
2163This is assumed to be sequence numbers. Normally also deletes
2164trailing whitespace after stripping such text. Supplying prefix
2165arg DO-SPACE prevents stripping the whitespace."
5b04210c 2166 (interactive "*p")
3b4613a4
DL
2167 (save-excursion
2168 (goto-char (point-min))
694cf05e
GM
2169 (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length)
2170 nil t)
3b4613a4
DL
2171 (replace-match "" nil nil nil 1)
2172 (unless do-space (delete-horizontal-space)))))
2173
4a948dbf 2174;; This code used to live in add-log.el, but this is a better place for it.
68ca306c
DL
2175(defun fortran-current-defun ()
2176 "Function to use for `add-log-current-defun-function' in Fortran mode."
a245ece5
GM
2177 (save-excursion
2178 ;; We must be inside function body for this to work.
2179 (fortran-beginning-of-subprogram)
4a948dbf 2180 (let ((case-fold-search t))
0a39a75c 2181 ;; Search for fortran subprogram start.
a245ece5 2182 (if (re-search-forward
4a948dbf 2183 fortran-start-prog-re
a245ece5
GM
2184 (save-excursion (fortran-end-of-subprogram)
2185 (point))
2186 t)
2187 (or (match-string-no-properties 2)
2188 (progn
0a39a75c 2189 ;; Move to EOL or before first left paren.
a245ece5
GM
2190 (if (re-search-forward "[(\n]" nil t)
2191 (progn (backward-char)
2192 (skip-chars-backward " \t"))
2193 (end-of-line))
2194 ;; Use the name preceding that.
2195 (buffer-substring-no-properties (point) (progn (backward-sexp)
2196 (point)))))
2197 "main"))))
68ca306c 2198
49116ac0
JB
2199(provide 'fortran)
2200
5d724235 2201;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
1a06eabd 2202;;; fortran.el ends here