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