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