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