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