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