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