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