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