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