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