Fix variable names in buffer-face-mode-invoke
[emacs.git] / lisp / progmodes / fortran.el
blobd5b551b78e23e73b0cd38a2fd237e1abcb52caa7
1 ;;; fortran.el --- Fortran mode for GNU Emacs
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.
7 ;; Author: Michael D. Prange <prange@erl.mit.edu>
8 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Keywords: fortran, languages
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;;; Commentary:
28 ;; This mode is documented in the Emacs manual.
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).
36 ;;; History:
38 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
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.
45 ;;; Code:
47 ;; Todo:
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.
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)
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)
69 (defgroup fortran-indent nil
70 "Indentation variables in Fortran mode."
71 :prefix "fortran-"
72 :group 'fortran)
74 (defgroup fortran-comment nil
75 "Comment-handling variables in Fortran mode."
76 :prefix "fortran-"
77 :group 'fortran)
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)
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)
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)
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)
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)
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)
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))))
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)
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)
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)
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)
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)
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)
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)))))
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)
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)
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)
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))))
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)
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)
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)
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)
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)
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)
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)
304 (put 'fortran-line-length 'safe-local-variable 'integerp)
305 (make-variable-buffer-local 'fortran-line-length)
307 (defcustom fortran-mode-hook nil
308 "Hook run when entering Fortran mode."
309 :type 'hook
310 :group 'fortran)
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'.")
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.")
328 (defconst fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*("
329 "Regexp matching the start of an IF statement.")
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.")
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'.")
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.")
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.")
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" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
393 "neqv" "not" "or" "true")
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.")
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.
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.")
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.")
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))))
473 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
474 "Default expressions to highlight in Fortran mode.")
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
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+\\)")
496 ;; Un-named block data.
497 '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
498 "Value for `imenu-generic-expression' in Fortran mode.")
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.")
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'.")
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'.")
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))
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.")
585 (defvar fortran-gud-syntax-table
586 (let ((st (make-syntax-table fortran-mode-syntax-table)))
587 (modify-syntax-entry ?\n "." st)
589 "Syntax table used to parse Fortran expressions for printing in GUD.")
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)
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]
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]
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.")
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:]_]*")
736 ;;;###autoload
737 (defun fortran-mode ()
738 "Major mode for editing Fortran code in fixed format.
739 For free format code, use `f90-mode'.
741 \\[fortran-indent-line] indents the current Fortran line correctly.
742 Note that DO statements must not share a common CONTINUE.
744 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
745 Fortran keywords.
747 Key definitions:
748 \\{fortran-mode-map}
750 Variables controlling indentation style and extra features:
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).
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))
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))))
893 (defun fortran-hack-local-variables ()
894 "Fortran mode adds this to `hack-local-variables-hook'."
895 (fortran-line-length fortran-line-length))
897 (declare-function gud-find-c-expr "gud.el" nil)
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)))
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))))
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.
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))))))
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)))
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))
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))))
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"))
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*"))
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."))
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))))
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)))
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 *
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)
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)))
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)))
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))))
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)))))))
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)))))
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 (when (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))))))
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))))
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)))
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)))))
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"))))
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"))))
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)))))
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))
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))
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)))))
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)))))))
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)))))))
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)))))
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)))))))
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)))))))
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))))
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)))))
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)
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."))
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 ;; Previous statement was a numbered DO loop without a
1656 ;; closing CONTINUE or END DO, so we indented the
1657 ;; terminator like the loop body.
1658 ((and fortran-check-all-num-for-matching-do
1659 (not (looking-at "\\(continue\\|end[ \t]*do\\)\\>"))
1660 (progn
1661 (beginning-of-line)
1662 (and (looking-at "[ \t]*[0-9]+")
1663 (fortran-check-for-matching-do))))
1664 (setq icol (- icol fortran-do-indent))))))
1665 (save-excursion
1666 (beginning-of-line)
1667 (cond ((looking-at "[ \t]*$"))
1668 ;; Check for directive before comment, so as not to indent.
1669 ((looking-at fortran-directive-re)
1670 (setq fortran-minimum-statement-indent 0 icol 0))
1671 ((looking-at fortran-comment-line-start-skip)
1672 (cond ((eq fortran-comment-indent-style 'relative)
1673 (setq icol (+ icol fortran-comment-line-extra-indent)))
1674 ((eq fortran-comment-indent-style 'fixed)
1675 (setq icol (+ fortran-minimum-statement-indent
1676 fortran-comment-line-extra-indent))))
1677 (setq fortran-minimum-statement-indent 0))
1678 ((or (looking-at (concat "[ \t]*"
1679 (regexp-quote
1680 fortran-continuation-string)))
1681 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1682 (skip-chars-forward " \t")
1683 ;; Do not introduce extra whitespace into a broken string.
1684 (setq icol
1685 (if (fortran-is-in-string-p (point))
1687 (+ icol fortran-continuation-indent))))
1688 (first-statement)
1689 ;; The terminating statement is actually part of the
1690 ;; loop body, so unless it is a CONTINUE or END DO, we
1691 ;; indent it like the loop body (see above).
1692 ((and fortran-check-all-num-for-matching-do
1693 (looking-at "[ \t]*[0-9]+[ \t]*\
1694 \\(continue\\|end[ \t]*do\\)\\>")
1695 (fortran-check-for-matching-do))
1696 (setq icol (- icol fortran-do-indent)))
1698 (skip-chars-forward " \t0-9")
1699 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1700 (setq icol (- icol fortran-if-indent)))
1701 ((looking-at "else\\(if\\)?\\b")
1702 (setq icol (- icol fortran-if-indent)))
1703 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1704 (setq icol (- icol fortran-if-indent)))
1705 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1706 (setq icol (- icol fortran-if-indent)))
1707 ((and (looking-at "continue\\b")
1708 (fortran-check-for-matching-do))
1709 (setq icol (- icol fortran-do-indent)))
1710 ((looking-at "end[ \t]*do\\b")
1711 (setq icol (- icol fortran-do-indent)))
1712 ((looking-at "end[ \t]*\
1713 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1714 (setq icol (- icol fortran-structure-indent)))
1715 ((and (looking-at fortran-end-prog-re1)
1716 (fortran-check-end-prog-re)
1717 (not (= icol fortran-minimum-statement-indent)))
1718 (message "Warning: `end' not in column %d. Probably\
1719 an unclosed block." fortran-minimum-statement-indent))))))
1720 (max fortran-minimum-statement-indent icol)))
1723 (defun fortran-current-line-indentation ()
1724 "Indentation of current line, ignoring Fortran line number or continuation.
1725 This is the column position of the first non-whitespace character
1726 aside from the line number and/or column 5/8 line-continuation character.
1727 For comment lines, returns indentation of the first
1728 non-indentation text within the comment."
1729 (save-excursion
1730 (beginning-of-line)
1731 (cond ((looking-at fortran-comment-line-start-skip)
1732 (goto-char (match-end 0))
1733 (skip-chars-forward
1734 (if (stringp fortran-comment-indent-char)
1735 fortran-comment-indent-char
1736 (char-to-string fortran-comment-indent-char))))
1737 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1738 (goto-char (match-end 0)))
1740 ;; Move past line number.
1741 (skip-chars-forward "[ \t0-9]")))
1742 ;; Move past whitespace.
1743 (skip-chars-forward " \t")
1744 (current-column)))
1746 (defun fortran-indent-to-column (col)
1747 "Indent current line to column COL.
1748 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1749 line, and this continuation character is retained on indentation;
1750 2) If `fortran-continuation-string' is the first non-whitespace
1751 character, this is a continuation line;
1752 3) A non-continuation line which has a number as the first
1753 non-whitespace character is a numbered line.
1754 4) A TAB followed by a digit indicates a continuation line."
1755 (save-excursion
1756 (beginning-of-line)
1757 (if (looking-at fortran-comment-line-start-skip)
1758 (if fortran-comment-indent-style
1759 (let* ((char (if (stringp fortran-comment-indent-char)
1760 (aref fortran-comment-indent-char 0)
1761 fortran-comment-indent-char))
1762 (chars (string ?\s ?\t char)))
1763 (goto-char (match-end 0))
1764 (skip-chars-backward chars)
1765 (delete-region (point) (progn (skip-chars-forward chars)
1766 (point)))
1767 (insert-char char (- col (current-column)))))
1768 (if (looking-at "\t[1-9]")
1769 (if indent-tabs-mode
1770 (goto-char (match-end 0))
1771 (delete-char 2)
1772 (insert-char ?\s 5)
1773 (insert fortran-continuation-string))
1774 (if (looking-at " \\{5\\}[^ 0\n]")
1775 (if indent-tabs-mode
1776 (progn (delete-char 6)
1777 (insert ?\t (fortran-numerical-continuation-char) 1))
1778 (forward-char 6))
1779 (delete-horizontal-space)
1780 ;; Put line number in columns 0-4, or
1781 ;; continuation character in column 5.
1782 (cond ((eobp))
1783 ((looking-at (regexp-quote fortran-continuation-string))
1784 (if indent-tabs-mode
1785 (progn
1786 (indent-to
1787 (if indent-tabs-mode
1788 fortran-minimum-statement-indent-tab
1789 fortran-minimum-statement-indent-fixed))
1790 (delete-char 1)
1791 (insert-char (fortran-numerical-continuation-char) 1))
1792 (indent-to 5)
1793 (forward-char 1)))
1794 ((looking-at "[0-9]+")
1795 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1796 (if (< extra-space 0)
1797 (message "Warning: line number exceeds 5-digit limit.")
1798 (indent-to (min fortran-line-number-indent extra-space))))
1799 (skip-chars-forward "0-9")))))
1800 ;; Point is now after any continuation character or line number.
1801 ;; Put body of statement where specified.
1802 (delete-horizontal-space)
1803 (indent-to col)
1804 ;; Indent any comment following code on the same line.
1805 (when (fortran-find-comment-start-skip)
1806 (goto-char (match-beginning 0))
1807 (unless (= (current-column) (fortran-comment-indent))
1808 (delete-horizontal-space)
1809 (indent-to (fortran-comment-indent)))))))
1811 (defun fortran-line-number-indented-correctly-p ()
1812 "Return t if current line's line number is correctly indented.
1813 Do not call if there is no line number."
1814 (save-excursion
1815 (beginning-of-line)
1816 (skip-chars-forward " \t")
1817 (and (<= (current-column) fortran-line-number-indent)
1818 (or (= (current-column) fortran-line-number-indent)
1819 (progn (skip-chars-forward "0-9")
1820 (= (current-column) 5))))))
1822 (defun fortran-check-for-matching-do ()
1823 "When called from a numbered statement, return t if matching DO is found.
1824 Otherwise return nil."
1825 (let ((case-fold-search t)
1826 charnum)
1827 (save-excursion
1828 (beginning-of-line)
1829 (when (looking-at "[ \t]*[0-9]+")
1830 (skip-chars-forward " \t")
1831 (skip-chars-forward "0") ; skip past leading zeros
1832 (setq charnum
1833 (buffer-substring (point) (progn
1834 (skip-chars-forward "0-9")
1835 (point))))
1836 (beginning-of-line)
1837 (save-restriction
1838 (save-excursion
1839 (narrow-to-defun)
1840 (and (re-search-backward
1841 (concat
1842 "\\(^[ \t0-9]*do[ \t]*0*"
1843 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1844 charnum "\\b\\)")
1845 nil t)
1846 (looking-at
1847 (concat "^[ \t0-9]*do[ \t]*0*"
1848 charnum)))))))))
1850 (defun fortran-find-comment-start-skip (&optional all)
1851 "Move to past `comment-start-skip' found on current line.
1852 Return non-nil if `comment-start-skip' found, nil if not.
1853 If ALL is nil, only match comments that start in column > 0."
1854 ;; Hopefully at some point we can just use the line below! -stef
1855 ;; (comment-search-forward (line-end-position) t))
1856 (when (or all comment-start-skip)
1857 (let ((pos (point))
1858 (css (if comment-start-skip
1859 (concat fortran-comment-line-start-skip
1860 "\\|" comment-start-skip)
1861 fortran-comment-line-start-skip)))
1862 (when (re-search-forward css (line-end-position) t)
1863 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1864 (or (save-match-data
1865 (not (fortran-is-in-string-p (match-beginning 0))))
1866 ;; Recurse for rest of line.
1867 (fortran-find-comment-start-skip all)))
1868 (point)
1869 (goto-char pos)
1870 nil)))))
1872 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1873 ;; Test if TAB format continuation lines work.
1874 (defun fortran-is-in-string-p (where)
1875 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
1876 (save-excursion
1877 (goto-char where)
1878 (cond
1879 ((bolp) nil) ; bol is never inside a string
1880 ((save-excursion ; comment lines too
1881 (beginning-of-line)
1882 (looking-at fortran-comment-line-start-skip)) nil)
1883 (t (let ((parse-state '(0 nil nil nil nil nil 0))
1884 (quoted-comment-start (if comment-start
1885 (regexp-quote comment-start)))
1886 (not-done t)
1887 parse-limit end-of-line)
1888 ;; Move to start of current statement.
1889 (fortran-next-statement)
1890 (fortran-previous-statement)
1891 ;; Now parse up to WHERE.
1892 (while not-done
1893 (if (or ;; Skip to next line if:
1894 ;; - comment line?
1895 (looking-at fortran-comment-line-start-skip)
1896 ;; - at end of line?
1897 (eolp)
1898 ;; - not in a string and after comment-start?
1899 (and (not (nth 3 parse-state))
1900 comment-start
1901 (equal comment-start
1902 (char-to-string (preceding-char)))))
1903 (if (> (forward-line) 0)
1904 (setq not-done nil))
1905 ;; else:
1906 ;; If we are at beginning of code line, skip any
1907 ;; whitespace, labels and tab continuation markers.
1908 (if (bolp) (skip-chars-forward " \t0-9"))
1909 ;; If we are in column <= 5 now, check for continuation char.
1910 (cond ((= 5 (current-column)) (forward-char 1))
1911 ((and (< (current-column) 5)
1912 (equal fortran-continuation-string
1913 (char-to-string (following-char)))
1914 (forward-char 1))))
1915 ;; Find out parse-limit from here.
1916 (setq end-of-line (line-end-position))
1917 (setq parse-limit (min where end-of-line))
1918 ;; Parse max up to comment-start, if non-nil and in current line.
1919 (if comment-start
1920 (save-excursion
1921 (if (re-search-forward quoted-comment-start end-of-line t)
1922 (setq parse-limit (min (point) parse-limit)))))
1923 ;; Now parse if still in limits.
1924 (if (< (point) where)
1925 (setq parse-state (parse-partial-sexp
1926 (point) parse-limit nil nil parse-state))
1927 (setq not-done nil))))
1928 ;; Result.
1929 (nth 3 parse-state))))))
1931 ;; From old version.
1932 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1934 (defun fortran-fill ()
1935 "Fill the current line at an appropriate point(s)."
1936 (let* ((auto-fill-function #'fortran-auto-fill)
1937 (opoint (point))
1938 (bol (line-beginning-position))
1939 (eol (line-end-position))
1940 (bos (min eol (+ bol (fortran-current-line-indentation))))
1941 ;; If in a string at fill-column, break it either before the
1942 ;; initial quote, or at fill-col (if string is too long).
1943 (quote
1944 (save-excursion
1945 (goto-char bol)
1946 ;; OK to break quotes on comment lines.
1947 (unless (looking-at fortran-comment-line-start-skip)
1948 (let (fcpoint start)
1949 (move-to-column fill-column)
1950 (when (fortran-is-in-string-p (setq fcpoint (point)))
1951 (save-excursion
1952 (re-search-backward "\\S\"\\s\"\\S\"?" bol t)
1953 (setq start
1954 (if fortran-break-before-delimiters
1955 (point)
1956 (1+ (point)))))
1957 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t)
1958 (backward-char 2))
1959 ;; If the current string is longer than (fill-column
1960 ;; - 6) chars, break it at the fill column (else
1961 ;; infinite loop).
1962 (if (> (- (point) start)
1963 (- fill-column 6 fortran-continuation-indent))
1964 fcpoint
1965 start))))))
1966 ;; Decide where to split the line. If a position for a quoted
1967 ;; string was found above then use that, else break the line
1968 ;; before/after the last delimiter.
1969 (fill-point
1970 (or quote
1971 (save-excursion
1972 ;; If f-b-b-d is t, have an extra column to play with,
1973 ;; since delimiter gets shifted to new line.
1974 (move-to-column (if fortran-break-before-delimiters
1975 (1+ fill-column)
1976 fill-column))
1977 (let ((repeat t))
1978 (while repeat
1979 (setq repeat nil)
1980 ;; Adapted from f90-find-breakpoint.
1981 (re-search-backward fortran-break-delimiters-re bol)
1982 (if (not fortran-break-before-delimiters)
1983 (if (looking-at fortran-no-break-re)
1984 ;; Deal with cases such as "**" split over
1985 ;; fill-col. Simpler alternative would be
1986 ;; to start from (1- fill-column) above.
1987 (if (> (+ 2 (current-column)) fill-column)
1988 (setq repeat t)
1989 (forward-char 2))
1990 (forward-char 1))
1991 (backward-char)
1992 (or (looking-at fortran-no-break-re)
1993 (forward-char)))))
1994 ;; Line indented beyond fill-column?
1995 (when (<= (point) bos)
1996 (move-to-column (1+ fill-column))
1997 ;; What is this doing???
1998 (or (re-search-forward "[\t\n,'+-/*)=]" eol t)
1999 (goto-char bol)))
2000 (if (bolp)
2001 (re-search-forward "[ \t]" opoint t))
2002 (point)))))
2003 ;; If we are in an in-line comment, don't break unless the
2004 ;; line of code is longer than it should be. Otherwise
2005 ;; break the line at the column computed above.
2007 ;; Need to use fortran-find-comment-start-skip to make sure that
2008 ;; quoted !'s don't prevent a break.
2009 (when (and (save-excursion
2010 (beginning-of-line)
2011 (if (not (fortran-find-comment-start-skip))
2013 (goto-char (match-beginning 0))
2014 (>= (point) fill-point)))
2015 (save-excursion
2016 (goto-char fill-point)
2017 (not (bolp)))
2018 (> (save-excursion
2019 (goto-char opoint)
2020 (current-column))
2021 (min (1+ fill-column)
2022 (+ (fortran-calculate-indent)
2023 fortran-continuation-indent))))
2024 (goto-char fill-point)
2025 (fortran-break-line)
2026 (end-of-line))))
2028 (defun fortran-break-line ()
2029 "Call `fortran-split-line'. Joins continuation lines first, then refills."
2030 (let ((bol (line-beginning-position))
2031 (comment-string
2032 (save-excursion
2033 (if (fortran-find-comment-start-skip)
2034 (delete-and-extract-region
2035 (match-beginning 0) (line-end-position))))))
2036 ;; Forward line 1 really needs to go to next non white line.
2037 (if (save-excursion (forward-line)
2038 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
2039 (progn
2040 (end-of-line)
2041 (delete-region (point) (match-end 0))
2042 (delete-horizontal-space)
2043 (fortran-fill))
2044 (fortran-split-line))
2045 (if comment-string
2046 (save-excursion
2047 (goto-char bol)
2048 (end-of-line)
2049 (delete-horizontal-space)
2050 (indent-to (fortran-comment-indent))
2051 (insert comment-string)))))
2053 (defun fortran-analyze-file-format ()
2054 "Return nil if fixed format is used, t if TAB formatting is used.
2055 Use `fortran-tab-mode-default' if no non-comment statements are found
2056 before the end or in the first `fortran-analyze-depth' lines."
2057 (let ((i 0))
2058 (save-excursion
2059 (goto-char (point-min))
2060 (while (not (or
2061 (eobp)
2062 (eq (char-after) ?\t)
2063 (looking-at " \\{6\\}")
2064 (> i fortran-analyze-depth)))
2065 (forward-line)
2066 (setq i (1+ i)))
2067 (cond
2068 ((eq (char-after) ?\t) t)
2069 ((looking-at " \\{6\\}") nil)
2070 (t fortran-tab-mode-default)))))
2072 (defun fortran-fill-paragraph (&optional justify)
2073 "Fill surrounding comment block as paragraphs, else fill statement.
2074 Intended as the value of `fill-paragraph-function'.
2075 A comment block is filled by calling `fill-comment-paragraph' with
2076 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
2077 Always returns non-nil (to prevent `fill-paragraph' being called)."
2078 (interactive "*P")
2079 (or (fill-comment-paragraph justify)
2080 (fortran-fill-statement)
2083 (defun fortran-fill-statement ()
2084 "Fill a Fortran statement up to `fill-column'."
2085 (interactive "*")
2086 (let ((auto-fill-function #'fortran-auto-fill))
2087 (unless (save-excursion
2088 (beginning-of-line)
2089 (or (looking-at "[ \t]*$")
2090 (looking-at fortran-comment-line-start-skip)
2091 (and comment-start-skip
2092 (looking-at (concat "[ \t]*" comment-start-skip)))))
2093 (save-excursion
2094 ;; Find beginning of statement.
2095 (fortran-next-statement)
2096 (fortran-previous-statement)
2097 ;; Re-indent initially.
2098 (fortran-indent-line)
2099 ;; Replace newline plus continuation field plus indentation with
2100 ;; single space.
2101 (while (progn
2102 (forward-line)
2103 (fortran-remove-continuation)))
2104 (fortran-previous-statement)))
2105 (fortran-indent-line)))
2107 (defun fortran-strip-sequence-nos (&optional do-space)
2108 "Delete all text in column `fortran-line-length' (default 72) and up.
2109 This is assumed to be sequence numbers. Normally also deletes
2110 trailing whitespace after stripping such text. Supplying prefix
2111 arg DO-SPACE prevents stripping the whitespace."
2112 (interactive "*p")
2113 (save-excursion
2114 (goto-char (point-min))
2115 (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length)
2116 nil t)
2117 (replace-match "" nil nil nil 1)
2118 (unless do-space (delete-horizontal-space)))))
2120 ;; This code used to live in add-log.el, but this is a better place
2121 ;; for it.
2122 (defun fortran-current-defun ()
2123 "Function to use for `add-log-current-defun-function' in Fortran mode."
2124 (save-excursion
2125 ;; We must be inside function body for this to work.
2126 (fortran-beginning-of-subprogram)
2127 (let ((case-fold-search t)) ; case-insensitive
2128 ;; Search for fortran subprogram start.
2129 (if (re-search-forward
2130 (concat "^[ \t]*\\(program\\|subroutine\\|function"
2131 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
2132 "\\(block[ \t]*data\\)\\)")
2133 (save-excursion (fortran-end-of-subprogram)
2134 (point))
2136 (or (match-string-no-properties 2)
2137 (progn
2138 ;; Move to EOL or before first left paren.
2139 (if (re-search-forward "[(\n]" nil t)
2140 (progn (backward-char)
2141 (skip-chars-backward " \t"))
2142 (end-of-line))
2143 ;; Use the name preceding that.
2144 (buffer-substring-no-properties (point) (progn (backward-sexp)
2145 (point)))))
2146 "main"))))
2148 (provide 'fortran)
2150 ;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
2151 ;;; fortran.el ends here