1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;; Copyright (C) 1986, 1993-1995, 1997-2012 Free Software Foundation, Inc.
5 ;; Author: Michael D. Prange <prange@erl.mit.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
7 ;; Keywords: fortran, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This mode is documented in the Emacs manual.
28 ;; Note that it is for editing Fortran77 or Fortran90 fixed source
29 ;; form. For editing Fortran 90 free format source, use `f90-mode'
30 ;; (f90.el). It is meant to support the GNU Fortran language
31 ;; implemented by g77 (its extensions to Fortran77 and
32 ;; interpretations, e.g. of backslash in strings).
36 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
38 ;; We acknowledge many contributions and valuable suggestions by
39 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
40 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
41 ;; Gary Sabot and Richard Stallman.
47 ;; * Tidy it all up (more)!
48 ;; * Implement insertion and removal of statement continuations in
49 ;; mixed f77/f90 style, with the first `&' past column 72 and the
50 ;; second in column 6.
51 ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
54 (defvar dabbrev-case-fold-search
)
55 (defvar gud-find-expr-function
)
56 (defvar imenu-case-fold-search
)
57 (defvar imenu-syntax-alist
)
58 (defvar comment-region-function
)
59 (defvar uncomment-region-function
)
62 "Major mode for editing fixed format Fortran code."
63 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
64 :link
'(custom-manual "(emacs)Fortran")
67 (defgroup fortran-indent nil
68 "Indentation variables in Fortran mode."
72 (defgroup fortran-comment nil
73 "Comment-handling variables in Fortran mode."
78 (defcustom fortran-tab-mode-default nil
79 "Default tabbing/carriage control style for empty files in Fortran mode.
80 A non-nil value specifies tab-digit style of continuation control.
81 A value of nil specifies that continuation lines are marked
82 with a character in column 6."
85 :group
'fortran-indent
)
87 ;; TODO add more detail of what tab mode is to doc string.
88 (defcustom fortran-tab-mode-string
89 (propertize "/t" 'help-echo
"This buffer is in Fortran TAB mode"
90 'mouse-face
'mode-line-highlight
92 (make-mode-line-mouse-map 'mouse-1
96 'fortran-tab-mode-string
))))
97 "String to appear in mode line in TAB format buffers.
98 See Info node `(emacs)ForIndent Cont'."
101 :group
'fortran-indent
)
103 (defcustom fortran-do-indent
3
104 "Extra indentation applied to DO blocks."
107 :group
'fortran-indent
)
109 (defcustom fortran-if-indent
3
110 "Extra indentation applied to IF, SELECT CASE and WHERE blocks."
113 :group
'fortran-indent
)
115 (defcustom fortran-structure-indent
3
116 "Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
119 :group
'fortran-indent
)
121 (defcustom fortran-continuation-indent
5
122 "Extra indentation applied to continuation lines."
125 :group
'fortran-indent
)
127 (defcustom fortran-comment-indent-style
'fixed
128 "How to indent comments.
129 nil forces comment lines not to be touched;
130 `fixed' indents to `fortran-comment-line-extra-indent' columns beyond
131 `fortran-minimum-statement-indent-fixed' (if `indent-tabs-mode' nil), or
132 `fortran-minimum-statement-indent-tab' (if `indent-tabs-mode' non-nil);
133 `relative' indents to current Fortran indentation plus
134 `fortran-comment-line-extra-indent'."
135 :type
'(radio (const :tag
"Untouched" nil
) (const fixed
) (const relative
))
136 :safe
(lambda (value) (memq value
'(nil fixed relative
)))
137 :group
'fortran-indent
)
139 (defcustom fortran-comment-line-extra-indent
0
140 "Amount of extra indentation for text within full-line comments."
143 :group
'fortran-indent
144 :group
'fortran-comment
)
146 (defcustom fortran-comment-line-start
"C"
147 "Delimiter inserted to start new full-line comment.
148 You might want to change this to \"*\", for instance; or \"!\" to
149 allow trailing comments on a line."
153 :group
'fortran-comment
)
155 ;; This used to match preprocessor lines too, but that messes up
156 ;; filling and doesn't seem to be necessary.
157 (defcustom fortran-comment-line-start-skip
158 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
159 "Regexp to match the start of a full-line comment."
163 :group
'fortran-comment
)
165 (defcustom fortran-directive-re
167 "Regexp to match a directive line.
168 The matching text will be fontified with `font-lock-keyword-face'.
169 The matching line will be given zero indentation."
173 :group
'fortran-indent
)
175 (defcustom fortran-minimum-statement-indent-fixed
6
176 "Minimum statement indentation for fixed format continuation style."
179 :group
'fortran-indent
)
181 (defcustom fortran-minimum-statement-indent-tab
(max tab-width
6)
182 "Minimum statement indentation for TAB format continuation style."
185 :group
'fortran-indent
)
187 ;; Note that this is documented in the v18 manuals as being a string
188 ;; of length one rather than a single character.
189 ;; The code in this file accepts either format for compatibility.
190 (defcustom fortran-comment-indent-char
" "
191 "Single-character string inserted for Fortran comment indentation.
194 :safe
(lambda (value) (or (characterp value
)
195 (and (stringp value
) (= (length value
) 1))))
196 :group
'fortran-comment
)
198 (defcustom fortran-line-number-indent
1
199 "Maximum indentation for Fortran line numbers.
200 5 means right-justify them within their five-column field."
203 :group
'fortran-indent
)
205 (defcustom fortran-check-all-num-for-matching-do nil
206 "Non-nil causes all numbered lines to be treated as possible DO loop ends."
211 (defcustom fortran-blink-matching-if nil
212 "Non-nil causes \\[fortran-indent-line] on ENDIF to blink on matching IF.
213 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
218 (defcustom fortran-continuation-string
"$"
219 "Single-character string used for Fortran continuation lines.
220 In fixed format continuation style, this character is inserted in
221 column 6 by \\[fortran-split-line] to begin a continuation line.
222 Also, if \\[fortran-indent-line] finds this at the beginning of a
223 line, it will convert the line into a continuation line of the
224 appropriate style. Normally \"$\"."
226 :safe
(lambda (value) (and (stringp value
) (= (length value
) 1)))
229 (defcustom fortran-comment-region
"c$$$"
230 "String inserted by \\[fortran-comment-region] at start of each \
234 :group
'fortran-comment
)
236 (defcustom fortran-electric-line-number t
237 "Non-nil causes line numbers to be moved to the correct column as typed."
242 ;; TODO use fortran-line-length, somehow.
243 (defcustom fortran-column-ruler-fixed
244 "0 4 6 10 20 30 40 5\
246 \[ ]|{ | | | | | | | | \
248 "String displayed above current line by \\[fortran-column-ruler].
249 This variable is used in fixed format mode.
250 See the variable `fortran-column-ruler-tab' for TAB format mode."
255 ;; TODO use fortran-line-length, somehow.
256 (defcustom fortran-column-ruler-tab
259 \[ ]| { | | | | | | | | \
261 "String displayed above current line by \\[fortran-column-ruler].
262 This variable is used in TAB format mode.
263 See the variable `fortran-column-ruler-fixed' for fixed format mode."
268 (defcustom fortran-analyze-depth
100
269 "Number of lines to scan to identify fixed or TAB format style."
274 (defcustom fortran-break-before-delimiters t
275 "Non-nil causes filling to break lines before delimiters.
276 Delimiters are characters matching the regexp `fortran-break-delimiters-re'."
281 ;; TODO 0 as no-limit, as per g77.
282 (defcustom fortran-line-length
72
283 "Maximum number of characters in a line of fixed-form Fortran code.
284 Characters beyond this point are treated as comments. Setting
285 this variable directly (after fortran mode is loaded) does not
286 take effect. Use either \\[customize] (which affects all Fortran
287 buffers and the default) or the function
288 `fortran-line-length' (which can also operate on just the current
289 buffer). This corresponds to the g77 compiler option
290 `-ffixed-line-length-N'."
293 :initialize
'custom-initialize-default
294 :set
(lambda (_symbol value
)
295 ;; Do all fortran buffers, and the default.
296 (fortran-line-length value t
))
300 (make-variable-buffer-local 'fortran-line-length
)
302 (defcustom fortran-mode-hook nil
303 "Hook run when entering Fortran mode."
308 (defconst fortran-break-delimiters-re
"[-+*/><=, \t]"
309 "Regexp matching delimiter characters at which lines may be broken.
310 There are certain tokens comprised entirely of characters
311 matching this regexp that should not be split, and these are
312 specified by the constant `fortran-no-break-re'.")
314 ;; The ">=", etc F77 extensions are supported by g77.
315 (defconst fortran-no-break-re
316 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=") 'paren
)
317 "Regexp specifying where not to break lines when filling.
318 This regexp matches certain tokens comprised entirely of
319 characters matching the regexp `fortran-break-delimiters-re' that should
320 not be split by filling. Each element is assumed to be two
323 (defconst fortran-if-start-re
"\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*("
324 "Regexp matching the start of an IF statement.")
326 ;; Note fortran-current-defun uses the subgroups.
327 (defconst fortran-start-prog-re
328 "^[ \t]*\\(program\\|subroutine\\|function\
329 \\|[ \ta-z0-9*()]*[ \t]+function\\|\
330 \\(block[ \t]*data\\)\\)"
331 "Regexp matching the start of a subprogram, from the line start.")
333 (defconst fortran-end-prog-re1
335 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
336 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?"
337 "Regexp possibly matching the end of a subprogram.")
339 (defconst fortran-end-prog-re
340 (concat "^[ \t0-9]*" fortran-end-prog-re1
)
341 "Regexp possibly matching the end of a subprogram, from the line start.
342 See also `fortran-end-prog-re1'.")
344 (defconst fortran-type-types
346 (mapconcat 'identity
; " " -> "[ \t]*"
350 '("character" "byte" "integer" "logical"
351 "none" "real" "complex"
352 "double precision" "double complex"))
353 (structured-types '("structure" "union" "map"))
354 (other-types '("record" "dimension"
355 "parameter" "common" "save"
356 "external" "intrinsic" "data"
359 (mapcar (lambda (x) (concat "implicit " x
))
362 (mapcar (lambda (x) (concat "end " x
))
365 other-types
)) 'paren
))
367 "Regexp matching Fortran types.")
369 (defvar fortran-font-lock-keywords-1
370 ;; Program, subroutine and function declarations, plus calls.
371 '(("\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|\
372 program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?"
373 (1 font-lock-keyword-face
)
374 (2 font-lock-function-name-face nil t
)))
375 "Subdued level highlighting for Fortran mode.")
377 (defvar fortran-font-lock-keywords-2
378 (append fortran-font-lock-keywords-1
380 ;; Fontify all type specifiers (must be first - see below).
381 (cons fortran-type-types
'font-lock-type-face
)
382 ;; Builtin keywords (except logical, do and goto - see below).
383 (concat "\\<" (regexp-opt
384 '("continue" "format" "end" "enddo"
385 "if" "then" "else" "endif" "elseif"
386 "while" "inquire" "stop" "return"
387 "include" "open" "close" "read"
388 "write" "format" "print" "select" "case"
389 "cycle" "exit" "rewind" "backspace"
392 ;; Builtin operators.
393 (concat "\\." (regexp-opt
394 '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
395 "neqv" "not" "or" "true")
397 ;; do/goto keywords and targets, and goto tags.
398 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
399 (1 font-lock-keyword-face
)
400 (2 font-lock-constant-face nil t
))
401 '("^ *\\([0-9]+\\)" . font-lock-constant-face
)))
402 "Medium level highlighting for Fortran mode.")
404 ;; See bug#1385. Never really looked into _why_ this matters...
405 (defun fortran-match-and-skip-declaration (limit)
406 "Like `font-lock-match-c-style-declaration-item-and-skip-to-next'.
407 The only difference is, it returns t in a case when the default returns nil."
408 (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
409 (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
411 (skip-chars-backward " \t\n")
412 (skip-syntax-backward "w")
413 (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
415 (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
419 (narrow-to-region (point-min) limit
)
420 (goto-char (match-end 1))
421 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
422 (goto-char (or (scan-sexps (point) 1) (point-max))))
423 (goto-char (match-end 2)))
426 (defvar fortran-font-lock-keywords-3
428 fortran-font-lock-keywords-1
429 ;; All type specifiers plus their declared items.
431 (list (concat fortran-type-types
"[ \t(/]*\\(*\\)?")
433 '(1 font-lock-type-face
)
434 ;; Declaration item (or just /.../ block name).
435 `(fortran-match-and-skip-declaration
436 ;; Start after any *(...) expression.
438 (and (match-beginning ,(1+ (regexp-opt-depth
439 fortran-type-types
)))
443 ;; No need to clean up.
445 ;; Fontify as a variable name, functions fontified elsewhere.
446 (1 font-lock-variable-name-face nil t
))))
447 ;; Things extra to `fortran-font-lock-keywords-3' (must be done first).
449 ;; Goto-like `err=label'/`end=label' in read/write statements.
450 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
451 (1 font-lock-keyword-face
) (4 font-lock-constant-face nil t
))
452 ;; Standard continuation character and in a TAB-formatted line.
453 '("^ \\{5\\}\\([^ 0\n]\\)" 1 font-lock-string-face
)
454 '("^\t\\([1-9]\\)" 1 font-lock-string-face
))
455 `((,fortran-directive-re
(0 font-lock-keyword-face t
)))
456 ;; `fortran-font-lock-keywords-2' without types (see above).
457 (cdr (nthcdr (length fortran-font-lock-keywords-1
)
458 fortran-font-lock-keywords-2
)))
459 "Gaudy level highlighting for Fortran mode.")
461 (defvar fortran-font-lock-keywords-4
462 (append fortran-font-lock-keywords-3
466 '("int" "ifix" "idint" "real" "float" "sngl"
467 "dble" "cmplx" "ichar" "char" "aint" "dint"
468 "anint" "dnint" "nint" "idnint" "iabs" "abs"
469 "dabs" "cabs" "mod" "amod" "dmod" "isign"
470 "sign" "dsign" "idim" "dim" "ddim" "dprod"
471 "max" "max0" "amax1" "dmax1" "amax0" "max1"
472 "min" "min0" "amin1" "dmin1" "amin0" "min1"
473 "len" "index" "lge" "lgt" "lle" "llt" "aimag"
474 "conjg" "sqrt" "dsqrt" "csqrt" "exp" "dexp"
475 "cexp" "log" "alog" "dlog" "clog" "log10"
476 "alog10" "dlog10" "sin" "dsin" "csin" "cos"
477 "dcos" "ccos" "tan" "dtan" "asin" "dasin"
478 "acos" "dacos" "atan" "datan" "atan2" "datan2"
479 "sinh" "dsinh" "cosh" "dcosh" "tanh" "dtanh")
480 'paren
) "[ \t]*(") '(1 font-lock-builtin-face
))))
481 "Maximum highlighting for Fortran mode.
482 Consists of level 3 plus all other intrinsics not already highlighted.")
484 ;; Comments are real pain in Fortran because there is no way to
485 ;; represent the standard comment syntax in an Emacs syntax table.
486 ;; (We can do so for F90-style). Therefore an unmatched quote in a
487 ;; standard comment will throw fontification off on the wrong track.
488 ;; So we do syntactic fontification with regexps.
489 (defun fortran-make-syntax-propertize-function (line-length)
490 "Return a value for `syntax-propertize-function' in Fortran mode.
491 This varies according to the value of LINE-LENGTH.
492 This is used to fontify fixed-format Fortran comments."
493 ;; This results in a non-byte-compiled function. We could pass it through
494 ;; `byte-compile', but simple benchmarks indicate that it's probably not
495 ;; worth the trouble (about 0.5% of slow down).
496 (eval ;I hate `eval', but it's hard to avoid it here.
497 `(syntax-propertize-rules
499 ;; We mark all chars after line-length as "comment-start", rather than
500 ;; just the first one. This is so that a closing ' that's past the
501 ;; line-length will indeed be ignored (and will result in a string that
502 ;; leaks into subsequent lines).
503 ((format "^[^cd\\*\t\n].\\{%d\\}\\(.+\\)" (1- line-length
))
506 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
507 "Default expressions to highlight in Fortran mode.")
509 (defvar fortran-imenu-generic-expression
510 ;; These patterns could be confused by sequence nos. in cols 72+ and
511 ;; don't allow continuations everywhere.
515 ;; [This will be fooled by `end function' allowed by G77. Also,
516 ;; it assumes sensible whitespace is employed.]
518 ;; leading whitespace:
520 ;; function declaration with optional type, e.g. `real',
521 ;; `real*4', character(*), `double precision':
522 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
523 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
524 ;; Possible statement continuation:
525 "[ \t" fortran-continuation-string
"]+"
526 ;; Variable to index:
529 ;; Un-named block data.
530 '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
531 "Value for `imenu-generic-expression' in Fortran mode.")
535 (defconst fortran-blocks-re
536 (concat "block[ \t]*data\\|select[ \t]*case\\|"
537 (regexp-opt '("do" "if" "interface" "function" "map" "program"
538 "structure" "subroutine" "union" "where")))
539 "Regexp potentially indicating the start or end of a Fortran \"block\".
540 Omits naked END statements, and DO-loops closed by anything other
543 (defconst fortran-end-block-re
544 ;; Do-loops terminated by things other than ENDDO cannot be handled
545 ;; with a regexp. This omission does not seem to matter to hideshow...
546 (concat "^[ \t0-9]*\\<end[ \t]*\\("
548 ;; Naked END statement.
550 "Regexp matching the end of a Fortran \"block\", from the line start.
551 Note that only ENDDO is handled for the end of a DO-loop. Used
552 in the Fortran entry in `hs-special-modes-alist'.")
554 (defconst fortran-start-block-re
556 "^[ \t0-9]*\\(" ; statement number
557 ;; Structure label for DO, IF, SELECT, WHERE.
558 "\\(\\(\\sw+[ \t]*:[ \t]*\\)?"
559 ;; IF blocks are a nuisance:
560 ;; IF ( ... ) foo is not a block, but a single statement.
561 ;; IF ( ... ) THEN can be split over multiple lines.
562 ;; [So can, eg, a DO WHILE (... ), but that is less common, I hope.]
563 ;; The regexp below allows for it to be split over at most 2 lines.
564 ;; That leads to the problem of not matching two consecutive IF
565 ;; statements as one, eg:
568 ;; It simply is not possible to do this in a 100% correct fashion
569 ;; using a regexp - see the functions fortran-end-if,
570 ;; fortran-beginning-if for the hoops we have to go through.
571 ;; An alternative is to match on THEN at a line end, eg:
572 ;; ".*)[ \t]*then[ \t]*\\($\\|!\\)"
573 ;; This would also match ELSE branches, though. This does not seem
574 ;; right to me, because then one has neighboring blocks that are
575 ;; not nested in each other.
576 "\\(if[ \t]*(\\(.*\\|"
577 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
578 "do\\|select[ \t]*case\\|where\\)\\)\\|"
579 (regexp-opt '("interface" "function" "map" "program"
580 "structure" "subroutine" "union"))
581 "\\|block[ \t]*data\\)[ \t]*")
582 "Regexp matching the start of a Fortran \"block\", from the line start.
583 A simple regexp cannot do this in fully correct fashion, so this
584 tries to strike a compromise between complexity and flexibility.
585 Used in the Fortran entry in `hs-special-modes-alist'.")
587 (add-to-list 'hs-special-modes-alist
588 `(fortran-mode ,fortran-start-block-re
,fortran-end-block-re
589 "^[cC*!]" fortran-end-of-block nil
))
592 (defvar fortran-mode-syntax-table
593 (let ((table (make-syntax-table)))
594 ;; Was a word-constituent (for abbrevs), now punctuation (g77
595 ;; multi-statement lines).
596 (modify-syntax-entry ?\
; "." table)
597 (modify-syntax-entry ?
\r " " table
)
598 (modify-syntax-entry ?
+ "." table
)
599 (modify-syntax-entry ?-
"." table
)
600 (modify-syntax-entry ?
= "." table
)
601 (modify-syntax-entry ?
* "." table
)
602 (modify-syntax-entry ?
/ "." table
)
603 (modify-syntax-entry ?%
"." table
) ; bug#8820
604 (modify-syntax-entry ?
\' "\"" table
)
605 (modify-syntax-entry ?
\" "\"" table
)
606 ;; Consistent with GNU Fortran's default -- see the manual.
607 ;; The F77 standard imposes no rule on this issue.
608 (modify-syntax-entry ?
\\ "\\" table
)
609 ;; This might be better as punctuation, as for C, but this way you
610 ;; can treat floating-point numbers as symbols.
611 (modify-syntax-entry ?.
"_" table
) ; e.g. `a.ne.b'
612 (modify-syntax-entry ?_
"_" table
)
613 (modify-syntax-entry ?$
"_" table
) ; esp. VMSisms
614 (modify-syntax-entry ?\
! "<" table
)
615 (modify-syntax-entry ?
\n ">" table
)
617 "Syntax table used in Fortran mode.")
619 (defvar fortran-gud-syntax-table
620 (let ((st (make-syntax-table fortran-mode-syntax-table
)))
621 (modify-syntax-entry ?
\n "." st
)
623 "Syntax table used to parse Fortran expressions for printing in GUD.")
625 (defvar fortran-mode-map
626 (let ((map (make-sparse-keymap)))
627 (define-key map
";" 'fortran-abbrev-start
)
628 (define-key map
"\C-c;" 'fortran-comment-region
)
629 ;; The default comment-dwim does at least as much as this.
630 ;;; (define-key map "\M-;" 'fortran-indent-comment)
631 (define-key map
"\M-\n" 'fortran-split-line
)
632 (define-key map
"\M-\C-n" 'fortran-end-of-block
)
633 (define-key map
"\M-\C-p" 'fortran-beginning-of-block
)
634 (define-key map
"\M-\C-q" 'fortran-indent-subprogram
)
635 (define-key map
"\C-c\C-w" 'fortran-window-create-momentarily
)
636 (define-key map
"\C-c\C-r" 'fortran-column-ruler
)
637 (define-key map
"\C-c\C-p" 'fortran-previous-statement
)
638 (define-key map
"\C-c\C-n" 'fortran-next-statement
)
639 (define-key map
"\C-c\C-d" 'fortran-join-line
) ; like f90
640 (define-key map
"\M-^" 'fortran-join-line
) ; subvert delete-indentation
641 (define-key map
"0" 'fortran-electric-line-number
)
642 (define-key map
"1" 'fortran-electric-line-number
)
643 (define-key map
"2" 'fortran-electric-line-number
)
644 (define-key map
"3" 'fortran-electric-line-number
)
645 (define-key map
"4" 'fortran-electric-line-number
)
646 (define-key map
"5" 'fortran-electric-line-number
)
647 (define-key map
"6" 'fortran-electric-line-number
)
648 (define-key map
"7" 'fortran-electric-line-number
)
649 (define-key map
"8" 'fortran-electric-line-number
)
650 (define-key map
"9" 'fortran-electric-line-number
)
652 (easy-menu-define fortran-menu map
"Menu for Fortran mode."
654 ["Manual" (info "(emacs)Fortran") :active t
655 :help
"Read the Emacs manual chapter on Fortran mode"]
657 ,(custom-menu-create 'fortran
)
659 ["Set" Custom-set
:active t
660 :help
"Set current value of all edited settings in the buffer"]
661 ["Save" Custom-save
:active t
662 :help
"Set and save all edited settings"]
663 ["Reset to Current" Custom-reset-current
:active t
664 :help
"Reset all edited settings to current"]
665 ["Reset to Saved" Custom-reset-saved
:active t
666 :help
"Reset all edited or set settings to saved"]
667 ["Reset to Standard Settings" Custom-reset-standard
:active t
668 :help
"Erase all customizations in buffer"]
671 ["Comment Region" fortran-comment-region mark-active
]
673 (fortran-comment-region (region-beginning) (region-end) 1)
675 ["Indent Region" indent-region mark-active
]
676 ["Indent Subprogram" fortran-indent-subprogram t
]
678 ["Beginning of Subprogram" fortran-beginning-of-subprogram
:active t
679 :help
"Move point to the start of the current subprogram"]
680 ["End of Subprogram" fortran-end-of-subprogram
:active t
681 :help
"Move point to the end of the current subprogram"]
683 :help
"Mark a region of code"
684 ["Subprogram" mark-defun
t]
685 ["IF Block" fortran-mark-if t
]
686 ["DO Block" fortran-mark-do t
]
688 ["Narrow to Subprogram" narrow-to-defun
t]
691 ["Temporary Column Ruler" fortran-column-ruler
:active t
692 :help
"Briefly display Fortran column numbers"]
693 ;; May not be '72', depending on fortran-line-length, but this
694 ;; seems ok for a menu item.
695 ["72-column Window" fortran-window-create
:active t
696 :help
"Set window width to Fortran line length"]
698 (enlarge-window-horizontally (- (frame-width) (window-width)))
699 :active
(not (window-full-width-p))
700 :help
"Make window full width"]
701 ["Momentary 72-Column Window" fortran-window-create-momentarily
702 :active t
:help
"Briefly set window width to Fortran line length"]
704 ["Break Line at Point" fortran-split-line
:active t
705 :help
"Break the current line at point"]
706 ["Join Line" fortran-join-line
:active t
707 :help
"Join the current line to the previous one"]
708 ["Fill Statement/Comment" fill-paragraph t
]
710 ["Toggle Auto Fill" auto-fill-mode
:selected auto-fill-function
712 :help
"Automatically fill text while typing in this buffer"]
713 ["Toggle Abbrev Mode" abbrev-mode
:selected abbrev-mode
714 :style toggle
:help
"Expand abbreviations while typing in this buffer"]
715 ["Add Imenu Menu" imenu-add-menubar-index
716 :active
(not (lookup-key (current-local-map) [menu-bar index
]))
717 :included
(fboundp 'imenu-add-to-menubar
)
718 :help
"Add an index menu to the menu-bar"]))
720 "Keymap used in Fortran mode.")
723 (define-abbrev-table 'fortran-mode-abbrev-table
724 (mapcar (lambda (e) (list (car e
) (cdr e
) nil
:system t
))
725 '((";au" .
"automatic" )
727 (";bd" .
"block data" )
728 (";ch" .
"character" )
734 (";di" .
"dimension" )
736 (";dc" .
"double complex" )
737 (";dp" .
"double precision" )
738 (";dw" .
"do while" )
743 (";eq" .
"equivalence" )
744 (";ew" .
"endwhere" )
745 (";ex" .
"external" )
749 (";fu" .
"function" )
751 (";im" .
"implicit" )
752 (";ib" .
"implicit byte" )
753 (";ic" .
"implicit complex" )
754 (";ich" .
"implicit character")
755 (";ii" .
"implicit integer" )
756 (";il" .
"implicit logical" )
757 (";ir" .
"implicit real" )
758 (";inc" .
"include" )
760 (";intr" .
"intrinsic" )
763 (";o" .
"open" ) ; was ;op
764 (";pa" .
"parameter" )
775 (";st" .
"structure" )
777 (";su" .
"subroutine" )
780 (";vo" .
"volatile" )
783 "Abbrev table for Fortran mode."
784 ;; Accept ; as the first char of an abbrev. Also allow _ in abbrevs.
785 :regexp
"\\(?:[^[:word:]_;]\\|^\\)\\(;?[[:word:]_]+\\)[^[:word:]_]*")
789 (define-derived-mode fortran-mode prog-mode
"Fortran"
790 "Major mode for editing Fortran code in fixed format.
791 For free format code, use `f90-mode'.
793 \\[fortran-indent-line] indents the current Fortran line correctly.
794 Note that DO statements must not share a common CONTINUE.
796 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
802 Variables controlling indentation style and extra features:
804 `fortran-comment-line-start'
805 To use comments starting with `!', set this to the string \"!\".
807 Extra indentation within DO blocks (default 3).
809 Extra indentation within IF blocks (default 3).
810 `fortran-structure-indent'
811 Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
813 `fortran-continuation-indent'
814 Extra indentation applied to continuation statements (default 5).
815 `fortran-comment-line-extra-indent'
816 Amount of extra indentation for text in full-line comments (default 0).
817 `fortran-comment-indent-style'
818 How to indent the text in full-line comments. Allowed values are:
819 nil don't change the indentation
820 fixed indent to `fortran-comment-line-extra-indent' beyond the
822 `fortran-minimum-statement-indent-fixed' (fixed format) or
823 `fortran-minimum-statement-indent-tab' (TAB format),
824 depending on the continuation format in use.
825 relative indent to `fortran-comment-line-extra-indent' beyond the
826 indentation for a line of code.
828 `fortran-comment-indent-char'
829 Single-character string to be inserted instead of space for
830 full-line comment indentation (default \" \").
831 `fortran-minimum-statement-indent-fixed'
832 Minimum indentation for statements in fixed format mode (default 6).
833 `fortran-minimum-statement-indent-tab'
834 Minimum indentation for statements in TAB format mode (default 9).
835 `fortran-line-number-indent'
836 Maximum indentation for line numbers (default 1). A line number will
837 get less than this much indentation if necessary to avoid reaching
839 `fortran-check-all-num-for-matching-do'
840 Non-nil causes all numbered lines to be treated as possible \"continue\"
841 statements (default nil).
842 `fortran-blink-matching-if'
843 Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
844 to blink on the matching IF (or DO [WHILE]). (default nil)
845 `fortran-continuation-string'
846 Single-character string to be inserted in column 5 of a continuation
847 line (default \"$\").
848 `fortran-comment-region'
849 String inserted by \\[fortran-comment-region] at start of each line in
850 the region (default \"c$$$\").
851 `fortran-electric-line-number'
852 Non-nil causes line number digits to be moved to the correct column
853 as typed (default t).
854 `fortran-break-before-delimiters'
855 Non-nil causes lines to be broken before delimiters (default t).
857 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
858 with no args, if that value is non-nil."
860 :syntax-table fortran-mode-syntax-table
861 :abbrev-table fortran-mode-abbrev-table
862 (set (make-local-variable 'indent-line-function
) 'fortran-indent-line
)
863 (set (make-local-variable 'indent-region-function
)
865 (let (fortran-blink-matching-if ; avoid blinking delay
866 indent-region-function
)
867 (indent-region start end nil
))))
868 (set (make-local-variable 'require-final-newline
) mode-require-final-newline
)
869 ;; The syntax tables don't understand the column-0 comment-markers.
870 (set (make-local-variable 'comment-use-syntax
) nil
)
871 (set (make-local-variable 'comment-padding
) "$$$")
872 (set (make-local-variable 'comment-start
) fortran-comment-line-start
)
873 (set (make-local-variable 'comment-start-skip
)
874 ;; We can't reuse `fortran-comment-line-start-skip' directly because
875 ;; it contains backrefs whereas we need submatch-1 to end at the
876 ;; beginning of the comment delimiter.
877 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
878 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
879 (set (make-local-variable 'comment-indent-function
) 'fortran-comment-indent
)
880 (set (make-local-variable 'comment-region-function
) 'fortran-comment-region
)
881 (set (make-local-variable 'uncomment-region-function
)
882 'fortran-uncomment-region
)
883 (set (make-local-variable 'comment-insert-comment-function
)
884 'fortran-indent-comment
)
885 (set (make-local-variable 'abbrev-all-caps
) t
)
886 (set (make-local-variable 'normal-auto-fill-function
) 'fortran-auto-fill
)
887 (set (make-local-variable 'indent-tabs-mode
) (fortran-analyze-file-format))
888 (setq mode-line-process
'(indent-tabs-mode fortran-tab-mode-string
))
889 (set (make-local-variable 'fill-column
) fortran-line-length
)
890 (set (make-local-variable 'fill-paragraph-function
) 'fortran-fill-paragraph
)
891 (set (make-local-variable 'font-lock-defaults
)
892 '((fortran-font-lock-keywords
893 fortran-font-lock-keywords-1
894 fortran-font-lock-keywords-2
895 fortran-font-lock-keywords-3
896 fortran-font-lock-keywords-4
)
897 nil t
((?
/ .
"$/") ("_$" .
"w"))
898 fortran-beginning-of-subprogram
))
899 (set (make-local-variable 'syntax-propertize-function
)
900 (fortran-make-syntax-propertize-function fortran-line-length
))
901 (set (make-local-variable 'imenu-case-fold-search
) t
)
902 (set (make-local-variable 'imenu-generic-expression
)
903 fortran-imenu-generic-expression
)
904 (set (make-local-variable 'imenu-syntax-alist
) '(("_$" .
"w")))
905 (set (make-local-variable 'beginning-of-defun-function
)
906 #'fortran-beginning-of-subprogram
)
907 (set (make-local-variable 'end-of-defun-function
)
908 #'fortran-end-of-subprogram
)
909 (set (make-local-variable 'add-log-current-defun-function
)
910 #'fortran-current-defun
)
911 (set (make-local-variable 'dabbrev-case-fold-search
) 'case-fold-search
)
912 (set (make-local-variable 'gud-find-expr-function
) 'fortran-gud-find-expr
)
913 (add-hook 'hack-local-variables-hook
'fortran-hack-local-variables nil t
))
916 (defun fortran-line-length (nchars &optional global
)
917 "Set the length of fixed-form Fortran lines to NCHARS.
918 This normally only affects the current buffer, which must be in
919 Fortran mode. If the optional argument GLOBAL is non-nil, it
920 affects all Fortran buffers, and also the default.
921 If a numeric prefix argument is specified, it will be used as NCHARS,
922 otherwise is a non-numeric prefix arg is specified, the length will be
923 provided via the minibuffer, and otherwise the current column is used."
926 ((numberp current-prefix-arg
) current-prefix-arg
)
928 (read-number "Line length: " (default-value 'fortran-line-length
)))
929 (t (current-column)))))
930 (dolist (buff (if global
932 (list (current-buffer))))
933 (with-current-buffer buff
934 (when (derived-mode-p 'fortran-mode
)
935 (unless (eq fortran-line-length nchars
)
936 (setq fortran-line-length nchars
937 fill-column fortran-line-length
938 syntax-propertize-function
939 (fortran-make-syntax-propertize-function nchars
))
940 (syntax-ppss-flush-cache (point-min))
941 (if font-lock-mode
(font-lock-mode 1))))))
943 (setq-default fortran-line-length nchars
)))
945 (defun fortran-hack-local-variables ()
946 "Fortran mode adds this to `hack-local-variables-hook'."
947 (fortran-line-length fortran-line-length
))
949 (declare-function gud-find-c-expr
"gud.el" nil
)
951 (defun fortran-gud-find-expr ()
952 ;; Consider \n as punctuation (end of expression).
953 (with-syntax-table fortran-gud-syntax-table
956 (defsubst fortran-comment-indent
()
957 "Return the indentation appropriate for the current comment line.
958 This is 0 for a line matching `fortran-comment-line-start-skip', else
959 the value of `comment-column' (leaving at least one space after code)."
960 (if (looking-at fortran-comment-line-start-skip
) 0
962 (skip-chars-backward " \t")
963 (max (1+ (current-column)) comment-column
))))
965 (defun fortran-indent-comment ()
966 "Align or create comment on current line.
967 Existing comments of all types are recognized and aligned.
968 If the line has no comment, a side-by-side comment is inserted and aligned,
969 if the value of `comment-start' is not nil and allows such comments.
970 Otherwise, a separate-line comment is inserted, on this line
971 or on a new line inserted before this line if this line is not blank."
974 ;; Recognize existing comments of either kind.
975 (cond ((fortran-find-comment-start-skip 'all
)
976 (goto-char (match-beginning 0))
978 (fortran-indent-line)
979 (unless (= (current-column) (fortran-comment-indent))
980 (delete-horizontal-space)
981 (indent-to (fortran-comment-indent)))))
982 ;; No existing comment.
983 ;; If side-by-side comments are defined, insert one,
984 ;; unless line is now blank.
985 ((and comment-start
(not (looking-at "[ \t]*$"))
986 (string-match comment-start-skip
(concat " " comment-start
)))
988 (delete-horizontal-space)
989 (indent-to (fortran-comment-indent))
990 (insert comment-start
))
991 ;; Else insert separate-line comment, making a new line if nec.
993 (if (looking-at "^[ \t]*$")
994 (delete-horizontal-space)
998 (insert fortran-comment-line-start
)
999 (insert-char (if (stringp fortran-comment-indent-char
)
1000 (aref fortran-comment-indent-char
0)
1001 fortran-comment-indent-char
)
1002 (- (fortran-calculate-indent) (current-column))))))
1004 (defun fortran-comment-region (beg-region end-region arg
)
1005 "Comment every line in the region.
1006 Inserts the string variable `fortran-comment-region' at the beginning of
1007 every line in the region.
1008 BEG-REGION and END-REGION specify the region boundaries.
1009 With non-nil ARG, uncomments the region."
1010 (interactive "*r\nP")
1011 (let ((end-region-mark (copy-marker end-region
))
1012 (save-point (point-marker)))
1013 (goto-char beg-region
)
1016 (let ((com (regexp-quote fortran-comment-region
))) ; uncomment
1017 (if (looking-at com
)
1018 (delete-region (point) (match-end 0)))
1019 (while (and (zerop (forward-line 1))
1020 (< (point) end-region-mark
))
1021 (if (looking-at com
)
1022 (delete-region (point) (match-end 0)))))
1023 (insert fortran-comment-region
) ; comment
1024 (while (and (zerop (forward-line 1))
1025 (< (point) end-region-mark
))
1026 (insert fortran-comment-region
)))
1027 (goto-char save-point
)
1028 (set-marker end-region-mark nil
)
1029 (set-marker save-point nil
)))
1031 ;; uncomment-region calls this with 3 args.
1032 (defun fortran-uncomment-region (start end
&optional ignored
)
1033 "Uncomment every line in the region."
1034 (fortran-comment-region start end t
))
1037 (defun fortran-abbrev-start ()
1038 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
1039 Any other key combination is executed normally."
1041 (insert last-command-event
)
1042 (let* ((event (if (fboundp 'next-command-event
) ; XEmacs
1043 (next-command-event)
1045 (char (if (fboundp 'event-to-character
)
1046 (event-to-character event
) event
)))
1047 ;; Insert char if not equal to `?', or if abbrev-mode is off.
1048 (if (and abbrev-mode
(or (eq char ??
) (eq char help-char
)
1049 (memq event help-event-list
)))
1050 (fortran-abbrev-help)
1051 (push event unread-command-events
))))
1053 (defun fortran-abbrev-help ()
1054 "List the currently defined abbrevs in Fortran mode."
1056 (message "Listing abbrev table...")
1057 (display-buffer (fortran-prepare-abbrev-list-buffer))
1058 (message "Listing abbrev table...done"))
1060 (defun fortran-prepare-abbrev-list-buffer ()
1061 "Create a buffer listing the Fortran mode abbreviations."
1062 (with-current-buffer (get-buffer-create "*Abbrevs*")
1064 (insert-abbrev-table-description 'fortran-mode-abbrev-table t
)
1065 (goto-char (point-min))
1066 (set-buffer-modified-p nil
)
1067 (edit-abbrevs-mode))
1068 (get-buffer-create "*Abbrevs*"))
1070 (defun fortran-column-ruler ()
1071 "Insert a column ruler momentarily above current line, till next keystroke.
1072 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
1073 format mode, and `fortran-column-ruler-tab' in TAB format mode.
1074 The next key typed is executed unless it is SPC."
1076 (momentary-string-display
1077 (if indent-tabs-mode
1078 fortran-column-ruler-tab
1079 fortran-column-ruler-fixed
)
1082 (if (eq (window-start (selected-window))
1083 (window-point (selected-window)))
1084 (line-beginning-position 2)
1086 nil
"Type SPC or any command to erase ruler."))
1088 (defun fortran-window-create ()
1089 "Make the window `fortran-line-length' (default 72) columns wide.
1090 See also `fortran-window-create-momentarily'."
1092 (let ((window-min-width 2))
1093 (unless (window-full-width-p)
1094 (enlarge-window-horizontally (- (frame-width)
1096 (let* ((window-edges (window-edges))
1097 (scroll-bar-width (- (nth 2 window-edges
)
1100 (split-window-right (+ fortran-line-length scroll-bar-width
)))
1102 (switch-to-buffer " fortran-window-extra" t
)
1103 (select-window (previous-window))))
1105 (defun fortran-window-create-momentarily (&optional arg
)
1106 "Momentarily make the window `fortran-line-length' (default 72) columns wide.
1107 Optional ARG non-nil and non-unity disables the momentary feature.
1108 See also `fortran-window-create'."
1112 (save-window-excursion
1115 (fortran-window-create)
1116 (error (error "No room for Fortran window")))
1117 (message "Type SPC to continue editing.")
1118 (let ((char (read-event)))
1119 (or (equal char ?\s
)
1120 (setq unread-command-events
(list char
))))))
1121 (fortran-window-create)))
1123 (defun fortran-split-line ()
1124 "Break line at point and insert continuation marker and alignment."
1126 (delete-horizontal-space)
1128 (let ((pos (point)))
1130 (and (fortran-find-comment-start-skip 'all
)
1131 (< (match-beginning 0) pos
))))
1132 (insert ?
\n (match-string 0))
1133 (if indent-tabs-mode
1134 (insert ?
\n ?
\t (fortran-numerical-continuation-char))
1135 (insert "\n " fortran-continuation-string
))) ; space after \n important
1136 (fortran-indent-line)) ; when cont string is C, c or *
1138 (defun fortran-remove-continuation ()
1139 "Delete any Fortran continuation characters at point.
1140 Returns t if anything actually deleted."
1141 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1143 (delete-indentation)
1146 (defun fortran-join-line (arg)
1147 "Join current line to the previous one and re-indent.
1148 With a prefix argument, repeat this operation that many times.
1149 If the prefix argument ARG is negative, join the next -ARG lines.
1150 Continuation lines are correctly handled."
1156 (while (not (zerop arg
))
1158 (or (fortran-remove-continuation)
1159 (delete-indentation))
1160 (setq arg
(1- arg
)))
1161 (fortran-indent-line)))
1163 (defun fortran-numerical-continuation-char ()
1164 "Return a digit for tab-digit style of continuation lines.
1165 If previous line is a tab-digit continuation line, return that digit
1166 plus one, otherwise return 1. Zero not allowed."
1169 (if (looking-at "\t[1-9]")
1170 (+ ?
1 (%
(- (char-after (1+ (point))) ?
0) 9))
1173 (put 'fortran-electric-line-number
'delete-selection t
)
1174 (defun fortran-electric-line-number (arg)
1175 "Self insert, but if part of a Fortran line number indent it automatically.
1176 Auto-indent does not happen if a numeric ARG is used."
1178 (if (or arg
(not fortran-electric-line-number
))
1180 (self-insert-command (prefix-numeric-value arg
))
1181 (self-insert-command 1))
1182 (if (or (and (= 5 (current-column))
1185 ;; In col 5 with only spaces to the left.
1186 (looking-at " \\{5\\}")))
1187 (and (= (if indent-tabs-mode
1188 fortran-minimum-statement-indent-tab
1189 fortran-minimum-statement-indent-fixed
) (current-column))
1190 ;; In col 8 with a single tab to the left.
1191 (eq ?
\t (char-after (line-beginning-position)))
1192 (not (or (eq last-command
'fortran-indent-line
)
1194 'fortran-indent-new-line
))))
1196 (re-search-backward "[^ \t0-9]"
1197 (line-beginning-position)
1198 t
)) ; not a line number
1199 (looking-at "[0-9]")) ; within a line number
1200 (self-insert-command (prefix-numeric-value arg
))
1201 (skip-chars-backward " \t")
1202 (insert last-command-event
)
1203 (fortran-indent-line))))
1206 (defun fortran-check-end-prog-re ()
1207 "Check a preliminary match against `fortran-end-prog-re'."
1208 ;; Having got a possible match for the subprogram end, we need a
1209 ;; match of whitespace, avoiding possible column 73+ stuff.
1211 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
1212 (buffer-substring (match-end 0)
1213 (min (line-end-position)
1214 (+ fortran-line-length
1215 (line-beginning-position)))))))
1217 ;; This is more complex than first expected because the beginning of a
1218 ;; main program may be implicit (ie not marked by a PROGRAM statement).
1219 ;; This would be fine (we could just go to bob in the absence of a match),
1220 ;; except it need not even be the first subprogram in the file (eg it
1221 ;; could follow a subroutine). Hence we have to search for END
1222 ;; statements instead.
1223 ;; cf fortran-beginning-of-block, f90-beginning-of-subprogram
1224 ;; Note that unlike the latter, we don't have to worry about nested
1227 (defun fortran-beginning-of-subprogram ()
1228 "Move point to the beginning of the current Fortran subprogram."
1230 (let ((case-fold-search t
))
1231 ;; If called already at the start of subprogram, go to the previous.
1232 (beginning-of-line (if (bolp) 0 1))
1234 (or (looking-at fortran-start-prog-re
)
1235 ;; This leaves us at bob if before the first subprogram.
1236 (eq (fortran-previous-statement) 'first-statement
)
1238 (while (re-search-backward fortran-end-prog-re nil
'move
)
1239 (if (fortran-check-end-prog-re) (throw 'ok t
))))
1240 ;; If the search failed, must be at bob.
1241 ;; First code line is the start of the subprogram.
1242 ;; FIXME use a more rigorous test, cf fortran-next-statement?
1243 ;; Though that needs to handle continuations too.
1244 (not (looking-at "^\\([ \t]*[0-9]\\|[ \t]+[^!#]\\)")))
1245 (fortran-next-statement))))))
1247 ;; This is simpler than f-beginning-of-s because the end of a
1248 ;; subprogram is never implicit.
1249 (defun fortran-end-of-subprogram ()
1250 "Move point to the end of the current Fortran subprogram."
1252 (let ((case-fold-search t
))
1255 (while (and (re-search-forward fortran-end-prog-re nil
'move
)
1256 (not (fortran-check-end-prog-re))))
1259 (defun fortran-previous-statement ()
1260 "Move point to beginning of the previous Fortran statement.
1261 Returns 'first-statement if that statement is the first
1262 non-comment Fortran statement in the file, and nil otherwise.
1263 Directive lines are treated as comments."
1265 (let (not-first-statement continue-test
)
1269 (not (looking-at fortran-comment-line-start-skip
))
1270 (not (looking-at fortran-directive-re
))
1273 (regexp-quote fortran-continuation-string
)))
1274 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1275 (while (and (setq not-first-statement
(zerop (forward-line -
1)))
1276 (or (looking-at fortran-comment-line-start-skip
)
1277 (looking-at fortran-directive-re
)
1280 (regexp-quote fortran-continuation-string
)))
1281 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1282 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1283 (cond ((and continue-test
1284 (not not-first-statement
))
1285 (message "Incomplete continuation statement."))
1287 (fortran-previous-statement))
1288 ((not not-first-statement
)
1289 'first-statement
))))
1291 (defun fortran-next-statement ()
1292 "Move point to beginning of the next Fortran statement.
1293 Returns 'last-statement if that statement is the last
1294 non-comment Fortran statement in the file, and nil otherwise.
1295 Directive lines are treated as comments."
1297 (let (not-last-statement)
1299 (while (and (setq not-last-statement
1300 (and (zerop (forward-line 1))
1302 (or (looking-at fortran-comment-line-start-skip
)
1303 (looking-at fortran-directive-re
)
1304 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1305 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1306 (if (not not-last-statement
)
1309 (defun fortran-looking-at-if-then ()
1310 "Return non-nil if at the start of a line with an IF ... THEN statement."
1311 ;; cf f90-looking-at-if-then.
1313 (i (fortran-beginning-if)))
1317 (= (line-beginning-position) p
)))))
1319 ;; Used in hs-special-modes-alist.
1320 (defun fortran-end-of-block (&optional num
)
1321 "Move point forward to the end of the current code block.
1322 With optional argument NUM, go forward that many balanced blocks.
1323 If NUM is negative, go backward to the start of a block. Does
1324 not check for consistency of block types. Interactively, pushes
1325 mark before moving point."
1327 (if (called-interactively-p 'any
) (push-mark (point) t
))
1328 (and num
(< num
0) (fortran-beginning-of-block (- num
)))
1329 (let ((case-fold-search t
)
1332 (while (and (> count
0)
1334 (concat "\\(" fortran-blocks-re
1335 (if fortran-check-all-num-for-matching-do
1336 "\\|^[ \t]*[0-9]+" "")
1337 "\\|continue\\|end\\)\\>")
1340 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re
))
1341 (fortran-looking-at-if-then)
1342 (looking-at fortran-start-block-re
))
1343 (setq count
(1+ count
))
1344 (if (or (looking-at fortran-end-block-re
)
1345 (and (or (looking-at "^[0-9 \t]*continue")
1346 (and fortran-check-all-num-for-matching-do
1347 (looking-at "[ \t]*[0-9]+")))
1348 (fortran-check-for-matching-do)))
1349 (setq count
(1- count
))))
1351 (if (> count
0) (error "Missing block end"))))
1353 (defun fortran-beginning-of-block (&optional num
)
1354 "Move point backwards to the start of the current code block.
1355 With optional argument NUM, go backward that many balanced
1356 blocks. If NUM is negative, go forward to the end of a block.
1357 Does not check for consistency of block types. Interactively,
1358 pushes mark before moving point."
1360 (if (called-interactively-p 'any
) (push-mark (point) t
))
1361 (and num
(< num
0) (fortran-end-of-block (- num
)))
1362 (let ((case-fold-search t
)
1365 (while (and (> count
0)
1367 (concat "\\(" fortran-blocks-re
1368 (if fortran-check-all-num-for-matching-do
1369 "\\|^[ \t]*[0-9]+" "")
1370 "\\|continue\\|end\\)\\>")
1373 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re
))
1374 (fortran-looking-at-if-then)
1375 (looking-at fortran-start-block-re
))
1376 (setq count
(1- count
))
1377 (if (or (looking-at fortran-end-block-re
)
1378 (and (or (looking-at "^[0-9 \t]*continue")
1379 (and fortran-check-all-num-for-matching-do
1380 (looking-at "[ \t]*[0-9]+")))
1381 (fortran-check-for-matching-do)))
1382 (setq count
(1+ count
)))))
1383 ;; Includes an un-named main program block.
1384 (if (> count
0) (error "Missing block start"))))
1387 (defun fortran-blink-match (regex keyword find-begin
)
1388 "From a line matching REGEX, blink matching KEYWORD statement line.
1389 Use function FIND-BEGIN to match it."
1390 (let ((top-of-window (window-start))
1392 (case-fold-search t
)
1395 (when (save-excursion
1397 (skip-chars-forward " \t0-9")
1399 (if (not (setq matching
(funcall find-begin
)))
1400 (setq message
(concat "No matching " keyword
"."))
1401 (if (< matching top-of-window
)
1403 (goto-char matching
)
1407 (buffer-substring (point)
1408 (line-end-position)))))))
1410 (message "%s" message
)
1411 (goto-char matching
)
1412 (sit-for blink-matching-delay
)
1413 (goto-char end-point
)))))
1415 (defun fortran-blink-matching-if ()
1416 "From an ENDIF or ELSE statement, blink the matching IF statement."
1417 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1418 "if" #'fortran-beginning-if
))
1420 (defun fortran-blink-matching-do ()
1421 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1422 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do
))
1424 (defun fortran-mark-do ()
1425 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1426 The marks are pushed."
1428 (let (enddo-point do-point
)
1429 (if (setq enddo-point
(fortran-end-do))
1430 (if (not (setq do-point
(fortran-beginning-do)))
1431 (message "No matching do.")
1432 (goto-char enddo-point
)
1434 (goto-char do-point
)))))
1436 (defun fortran-end-do ()
1437 "Search forward for first unmatched ENDDO.
1438 Return point or nil."
1439 (let ((case-fold-search t
))
1440 (if (save-excursion (beginning-of-line)
1441 (skip-chars-forward " \t0-9")
1442 (looking-at "end[ \t]*do\\b"))
1448 (while (and (not (zerop count
))
1449 (not (eq (fortran-next-statement) 'last-statement
))
1450 ;; Keep local to subprogram.
1451 (not (and (looking-at fortran-end-prog-re
)
1452 (fortran-check-end-prog-re))))
1453 (skip-chars-forward " \t0-9")
1454 (cond ((looking-at "end[ \t]*do\\b")
1455 (setq count
(1- count
)))
1457 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1458 (setq count
(1+ count
)))))
1460 ;; All pairs accounted for.
1463 (defun fortran-beginning-do ()
1464 "Search backwards for first unmatched DO [WHILE].
1465 Return point or nil. Ignores labeled DO loops (ie DO 10 ... 10 CONTINUE)."
1466 (let ((case-fold-search t
)
1467 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1470 (skip-chars-forward " \t0-9")
1471 (looking-at dostart-re
))
1477 (while (and (not (zerop count
))
1478 (not (eq (fortran-previous-statement) 'first-statement
))
1479 ;; Keep local to subprogram.
1480 (not (and (looking-at fortran-end-prog-re
)
1481 (fortran-check-end-prog-re))))
1482 (skip-chars-forward " \t0-9")
1483 (cond ((looking-at dostart-re
)
1484 (setq count
(1- count
)))
1485 ;; Note labeled loop ends not considered.
1486 ((looking-at "end[ \t]*do\\b")
1487 (setq count
(1+ count
)))))
1489 ;; All pairs accounted for.
1492 (defun fortran-mark-if ()
1493 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1494 The marks are pushed."
1496 (let (endif-point if-point
)
1497 (if (setq endif-point
(fortran-end-if))
1498 (if (not (setq if-point
(fortran-beginning-if)))
1499 (message "No matching if.")
1500 ;; Set mark, move point.
1501 (goto-char endif-point
)
1503 (goto-char if-point
)))))
1505 (defun fortran-end-if ()
1506 "Search forwards for first unmatched ENDIF.
1507 Return point or nil."
1508 (let ((case-fold-search t
))
1509 (if (save-excursion (beginning-of-line)
1510 (skip-chars-forward " \t0-9")
1511 (looking-at "end[ \t]*if\\b"))
1514 ;; Search for one. The point has been already been moved to first
1515 ;; letter on line but this should not cause troubles.
1518 (while (and (not (zerop count
))
1519 (not (eq (fortran-next-statement) 'last-statement
))
1520 ;; Keep local to subprogram.
1521 (not (and (looking-at fortran-end-prog-re
)
1522 (fortran-check-end-prog-re))))
1523 (skip-chars-forward " \t0-9")
1524 (cond ((looking-at "end[ \t]*if\\b")
1525 (setq count
(1- count
)))
1526 ((looking-at fortran-if-start-re
)
1529 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1530 (let (then-test) ; multi-line if-then
1533 (zerop (forward-line 1))
1534 ;; Search forward for then.
1535 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1539 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1541 (setq count
(1+ count
)))))))
1543 ;; All pairs accounted for.
1546 (defun fortran-beginning-if ()
1547 "Search backwards for first unmatched IF-THEN.
1548 Return point or nil."
1549 (let ((case-fold-search t
))
1551 ;; May be sitting on multi-line if-then statement, first
1552 ;; move to beginning of current statement. Note:
1553 ;; `fortran-previous-statement' moves to previous statement
1554 ;; *unless* current statement is first one. Only move
1555 ;; forward if not first-statement.
1556 (if (not (eq (fortran-previous-statement) 'first-statement
))
1557 (fortran-next-statement))
1558 (skip-chars-forward " \t0-9")
1560 (looking-at fortran-if-start-re
)
1562 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1563 ;; Multi-line if-then.
1566 (and (zerop (forward-line 1))
1567 ;; Search forward for then.
1568 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1572 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1579 (while (and (not (zerop count
))
1580 (not (eq (fortran-previous-statement) 'first-statement
))
1581 ;; Keep local to subprogram.
1582 (not (and (looking-at fortran-end-prog-re
)
1583 (fortran-check-end-prog-re))))
1584 (skip-chars-forward " \t0-9")
1585 (cond ((looking-at fortran-if-start-re
)
1588 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1589 (let (then-test) ; multi-line if-then
1592 (zerop (forward-line 1))
1593 ;; Search forward for then.
1594 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1598 (concat ".*then\\b[ \t]*"
1599 "[^ \t(=a-z0-9]"))))))
1601 (setq count
(1- count
)))))
1602 ((looking-at "end[ \t]*if\\b")
1603 (setq count
(1+ count
)))))
1605 ;; All pairs accounted for.
1609 (defun fortran-indent-line ()
1610 "Indent current Fortran line based on its contents and on previous lines."
1612 (let ((cfi (fortran-calculate-indent)))
1615 (if (or (not (= cfi
(fortran-current-line-indentation)))
1616 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
1617 (not (fortran-line-number-indented-correctly-p))))
1618 (fortran-indent-to-column cfi
)
1620 (if (fortran-find-comment-start-skip)
1621 (fortran-indent-comment))))
1622 ;; Never leave point in left margin.
1623 (if (< (current-column) cfi
)
1624 (move-to-column cfi
))
1625 (and auto-fill-function
1626 (> (save-excursion (end-of-line) (current-column))
1631 (when fortran-blink-matching-if
1632 (fortran-blink-matching-if)
1633 (fortran-blink-matching-do))))
1635 (defun fortran-auto-fill ()
1636 "Function to use for `normal-auto-fill-function' in Fortran mode."
1637 (if (> (current-column) (current-fill-column))
1638 (let ((cfi (fortran-calculate-indent)))
1641 (if (or (not (= cfi
(fortran-current-line-indentation)))
1642 (and (re-search-forward "^[ \t]*[0-9]+"
1644 (not (fortran-line-number-indented-correctly-p))))
1645 (fortran-indent-to-column cfi
)
1647 (if (fortran-find-comment-start-skip)
1648 (fortran-indent-comment))))
1650 ;; Never leave point in left margin.
1651 (if (< (current-column) cfi
)
1652 (move-to-column cfi
)))))
1654 ;; Historically this was a separate function which advertised itself
1655 ;; as reindenting but only did so where `most likely to be necessary'.
1656 (defalias 'fortran-indent-new-line
'reindent-then-newline-and-indent
)
1658 (defun fortran-indent-subprogram ()
1659 "Properly indent the Fortran subprogram containing point."
1663 (message "Indenting subprogram...")
1664 (indent-region (point) (mark) nil
))
1665 (message "Indenting subprogram...done."))
1667 (defun fortran-calculate-indent ()
1668 "Calculates the Fortran indent column based on previous lines."
1669 (let (icol first-statement
(case-fold-search t
)
1670 (fortran-minimum-statement-indent
1671 (if indent-tabs-mode
1672 fortran-minimum-statement-indent-tab
1673 fortran-minimum-statement-indent-fixed
)))
1675 (setq first-statement
(fortran-previous-statement))
1677 (setq icol fortran-minimum-statement-indent
)
1678 (if (= (point) (point-min))
1679 (setq icol fortran-minimum-statement-indent
)
1680 (setq icol
(fortran-current-line-indentation)))
1681 (skip-chars-forward " \t0-9")
1682 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1683 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1684 (let (then-test) ; multi-line if-then
1685 (while (and (zerop (forward-line 1))
1686 ;; Search forward for then.
1687 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1688 (not (setq then-test
1691 *[^ \t_$(=a-z0-9]")))))
1693 (setq icol
(+ icol fortran-if-indent
))))
1694 ((looking-at "else\\(if\\)?\\b")
1695 (setq icol
(+ icol fortran-if-indent
)))
1696 ((looking-at "select[ \t]*case[ \t](.*)")
1697 (setq icol
(+ icol fortran-if-indent
)))
1698 ((looking-at "case[ \t]*(.*)")
1699 (setq icol
(+ icol fortran-if-indent
)))
1700 ((looking-at "case[ \t]*default\\b")
1701 (setq icol
(+ icol fortran-if-indent
)))
1702 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1703 (setq icol
(+ icol fortran-if-indent
)))
1704 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1705 (setq icol
(+ icol fortran-if-indent
)))
1706 ((looking-at "do\\b")
1707 (setq icol
(+ icol fortran-do-indent
)))
1709 "\\(structure\\|union\\|map\\|interface\\)\
1710 \\b[ \t]*[^ \t=(a-z]")
1711 (setq icol
(+ icol fortran-structure-indent
)))
1712 ((and (looking-at fortran-end-prog-re1
)
1713 (fortran-check-end-prog-re))
1714 ;; Previous END resets indent to minimum.
1715 (setq icol fortran-minimum-statement-indent
))
1716 ;; Previous statement was a numbered DO loop without a
1717 ;; closing CONTINUE or END DO, so we indented the
1718 ;; terminator like the loop body.
1719 ((and fortran-check-all-num-for-matching-do
1720 (not (looking-at "\\(continue\\|end[ \t]*do\\)\\>"))
1723 (and (looking-at "[ \t]*[0-9]+")
1724 (fortran-check-for-matching-do))))
1725 (setq icol
(- icol fortran-do-indent
))))))
1728 (cond ((looking-at "[ \t]*$"))
1729 ;; Check for directive before comment, so as not to indent.
1730 ((looking-at fortran-directive-re
)
1731 (setq fortran-minimum-statement-indent
0 icol
0))
1732 ((looking-at fortran-comment-line-start-skip
)
1733 (cond ((eq fortran-comment-indent-style
'relative
)
1734 (setq icol
(+ icol fortran-comment-line-extra-indent
)))
1735 ((eq fortran-comment-indent-style
'fixed
)
1736 (setq icol
(+ fortran-minimum-statement-indent
1737 fortran-comment-line-extra-indent
))))
1738 (setq fortran-minimum-statement-indent
0))
1739 ((or (looking-at (concat "[ \t]*"
1741 fortran-continuation-string
)))
1742 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1743 (skip-chars-forward " \t")
1744 ;; Do not introduce extra whitespace into a broken string.
1746 (if (fortran-is-in-string-p (point))
1748 (+ icol fortran-continuation-indent
))))
1750 ;; The terminating statement is actually part of the
1751 ;; loop body, so unless it is a CONTINUE or END DO, we
1752 ;; indent it like the loop body (see above).
1753 ((and fortran-check-all-num-for-matching-do
1754 (looking-at "[ \t]*[0-9]+[ \t]*\
1755 \\(continue\\|end[ \t]*do\\)\\>")
1756 (fortran-check-for-matching-do))
1757 (setq icol
(- icol fortran-do-indent
)))
1759 (skip-chars-forward " \t0-9")
1760 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1761 (setq icol
(- icol fortran-if-indent
)))
1762 ((looking-at "else\\(if\\)?\\b")
1763 (setq icol
(- icol fortran-if-indent
)))
1764 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1765 (setq icol
(- icol fortran-if-indent
)))
1766 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1767 (setq icol
(- icol fortran-if-indent
)))
1768 ((and (looking-at "continue\\b")
1769 (fortran-check-for-matching-do))
1770 (setq icol
(- icol fortran-do-indent
)))
1771 ((looking-at "end[ \t]*do\\b")
1772 (setq icol
(- icol fortran-do-indent
)))
1773 ((looking-at "end[ \t]*\
1774 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1775 (setq icol
(- icol fortran-structure-indent
)))
1776 ((and (looking-at fortran-end-prog-re1
)
1777 (fortran-check-end-prog-re)
1778 (not (= icol fortran-minimum-statement-indent
)))
1779 (message "Warning: `end' not in column %d. Probably\
1780 an unclosed block." fortran-minimum-statement-indent
))))))
1781 (max fortran-minimum-statement-indent icol
)))
1784 (defun fortran-current-line-indentation ()
1785 "Indentation of current line, ignoring Fortran line number or continuation.
1786 This is the column position of the first non-whitespace character
1787 aside from the line number and/or column 5/8 line-continuation character.
1788 For comment lines, returns indentation of the first
1789 non-indentation text within the comment."
1792 (cond ((looking-at fortran-comment-line-start-skip
)
1793 (goto-char (match-end 0))
1795 (if (stringp fortran-comment-indent-char
)
1796 fortran-comment-indent-char
1797 (char-to-string fortran-comment-indent-char
))))
1798 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1799 (goto-char (match-end 0)))
1801 ;; Move past line number.
1802 (skip-chars-forward "[ \t0-9]")))
1803 ;; Move past whitespace.
1804 (skip-chars-forward " \t")
1807 (defun fortran-indent-to-column (col)
1808 "Indent current line to column COL.
1809 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1810 line, and this continuation character is retained on indentation;
1811 2) If `fortran-continuation-string' is the first non-whitespace
1812 character, this is a continuation line;
1813 3) A non-continuation line which has a number as the first
1814 non-whitespace character is a numbered line.
1815 4) A TAB followed by a digit indicates a continuation line."
1818 (if (looking-at fortran-comment-line-start-skip
)
1819 (if fortran-comment-indent-style
1820 (let* ((char (if (stringp fortran-comment-indent-char
)
1821 (aref fortran-comment-indent-char
0)
1822 fortran-comment-indent-char
))
1823 (chars (string ?\s ?
\t char
)))
1824 (goto-char (match-end 0))
1825 (skip-chars-backward chars
)
1826 (delete-region (point) (progn (skip-chars-forward chars
)
1828 (insert-char char
(- col
(current-column)))))
1829 (if (looking-at "\t[1-9]")
1830 (if indent-tabs-mode
1831 (goto-char (match-end 0))
1834 (insert fortran-continuation-string
))
1835 (if (looking-at " \\{5\\}[^ 0\n]")
1836 (if indent-tabs-mode
1837 (progn (delete-char 6)
1838 (insert ?
\t (fortran-numerical-continuation-char) 1))
1840 (delete-horizontal-space)
1841 ;; Put line number in columns 0-4, or
1842 ;; continuation character in column 5.
1844 ((looking-at (regexp-quote fortran-continuation-string
))
1845 (if indent-tabs-mode
1848 (if indent-tabs-mode
1849 fortran-minimum-statement-indent-tab
1850 fortran-minimum-statement-indent-fixed
))
1852 (insert-char (fortran-numerical-continuation-char) 1))
1855 ((looking-at "[0-9]+")
1856 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1857 (if (< extra-space
0)
1858 (message "Warning: line number exceeds 5-digit limit.")
1859 (indent-to (min fortran-line-number-indent extra-space
))))
1860 (skip-chars-forward "0-9")))))
1861 ;; Point is now after any continuation character or line number.
1862 ;; Put body of statement where specified.
1863 (delete-horizontal-space)
1865 ;; Indent any comment following code on the same line.
1866 (when (fortran-find-comment-start-skip)
1867 (goto-char (match-beginning 0))
1868 (unless (= (current-column) (fortran-comment-indent))
1869 (delete-horizontal-space)
1870 (indent-to (fortran-comment-indent)))))))
1872 (defun fortran-line-number-indented-correctly-p ()
1873 "Return t if current line's line number is correctly indented.
1874 Do not call if there is no line number."
1877 (skip-chars-forward " \t")
1878 (and (<= (current-column) fortran-line-number-indent
)
1879 (or (= (current-column) fortran-line-number-indent
)
1880 (progn (skip-chars-forward "0-9")
1881 (= (current-column) 5))))))
1883 (defun fortran-check-for-matching-do ()
1884 "When called from a numbered statement, return t if matching DO is found.
1885 Otherwise return nil."
1886 (let ((case-fold-search t
)
1890 (when (looking-at "[ \t]*[0-9]+")
1891 (skip-chars-forward " \t")
1892 (skip-chars-forward "0") ; skip past leading zeros
1894 (buffer-substring (point) (progn
1895 (skip-chars-forward "0-9")
1901 (and (re-search-backward
1903 "\\(^[ \t0-9]*do[ \t]*0*"
1904 charnum
"\\b\\)\\|" "\\(^[ \t]*0*"
1908 (concat "^[ \t0-9]*do[ \t]*0*"
1911 (defun fortran-find-comment-start-skip (&optional all
)
1912 "Move to past `comment-start-skip' found on current line.
1913 Return non-nil if `comment-start-skip' found, nil if not.
1914 If ALL is nil, only match comments that start in column > 0."
1915 ;; Hopefully at some point we can just use the line below! -stef
1916 ;; (comment-search-forward (line-end-position) t))
1917 (when (or all comment-start-skip
)
1919 (css (if comment-start-skip
1920 (concat fortran-comment-line-start-skip
1921 "\\|" comment-start-skip
)
1922 fortran-comment-line-start-skip
)))
1923 (when (re-search-forward css
(line-end-position) t
)
1924 (if (and (or all
(> (match-beginning 0) (line-beginning-position)))
1925 (or (save-match-data
1926 (not (fortran-is-in-string-p (match-beginning 0))))
1927 ;; Recurse for rest of line.
1928 (fortran-find-comment-start-skip all
)))
1933 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1934 ;; Test if TAB format continuation lines work.
1935 (defun fortran-is-in-string-p (where)
1936 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
1940 ((bolp) nil
) ; bol is never inside a string
1941 ((save-excursion ; comment lines too
1943 (looking-at fortran-comment-line-start-skip
)) nil
)
1944 (t (let ((parse-state '(0 nil nil nil nil nil
0))
1945 (quoted-comment-start (if comment-start
1946 (regexp-quote comment-start
)))
1948 parse-limit end-of-line
)
1949 ;; Move to start of current statement.
1950 (fortran-next-statement)
1951 (fortran-previous-statement)
1952 ;; Now parse up to WHERE.
1954 (if (or ;; Skip to next line if:
1956 (looking-at fortran-comment-line-start-skip
)
1957 ;; - at end of line?
1959 ;; - not in a string and after comment-start?
1960 (and (not (nth 3 parse-state
))
1962 (equal comment-start
1963 (char-to-string (preceding-char)))))
1964 (if (> (forward-line) 0)
1965 (setq not-done nil
))
1967 ;; If we are at beginning of code line, skip any
1968 ;; whitespace, labels and tab continuation markers.
1969 (if (bolp) (skip-chars-forward " \t0-9"))
1970 ;; If we are in column <= 5 now, check for continuation char.
1971 (cond ((= 5 (current-column)) (forward-char 1))
1972 ((and (< (current-column) 5)
1973 (equal fortran-continuation-string
1974 (char-to-string (following-char)))
1976 ;; Find out parse-limit from here.
1977 (setq end-of-line
(line-end-position))
1978 (setq parse-limit
(min where end-of-line
))
1979 ;; Parse max up to comment-start, if non-nil and in current line.
1982 (if (re-search-forward quoted-comment-start end-of-line t
)
1983 (setq parse-limit
(min (point) parse-limit
)))))
1984 ;; Now parse if still in limits.
1985 (if (< (point) where
)
1986 (setq parse-state
(parse-partial-sexp
1987 (point) parse-limit nil nil parse-state
))
1988 (setq not-done nil
))))
1990 (nth 3 parse-state
))))))
1992 ;; From old version.
1993 (defalias 'fortran-auto-fill-mode
'auto-fill-mode
)
1995 (defun fortran-fill ()
1996 "Fill the current line at an appropriate point(s)."
1997 (let* ((auto-fill-function #'fortran-auto-fill
)
1999 (bol (line-beginning-position))
2000 (eol (line-end-position))
2001 (bos (min eol
(+ bol
(fortran-current-line-indentation))))
2002 ;; If in a string at fill-column, break it either before the
2003 ;; initial quote, or at fill-col (if string is too long).
2007 ;; OK to break quotes on comment lines.
2008 (unless (looking-at fortran-comment-line-start-skip
)
2009 (let (fcpoint start
)
2010 (move-to-column fill-column
)
2011 (when (fortran-is-in-string-p (setq fcpoint
(point)))
2013 (re-search-backward "\\S\"\\s\"\\S\"?" bol t
)
2015 (if fortran-break-before-delimiters
2018 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t
)
2020 ;; If the current string is longer than (fill-column
2021 ;; - 6) chars, break it at the fill column (else
2023 (if (> (- (point) start
)
2024 (- fill-column
6 fortran-continuation-indent
))
2027 ;; Decide where to split the line. If a position for a quoted
2028 ;; string was found above then use that, else break the line
2029 ;; before/after the last delimiter.
2033 ;; If f-b-b-d is t, have an extra column to play with,
2034 ;; since delimiter gets shifted to new line.
2035 (move-to-column (if fortran-break-before-delimiters
2041 ;; Adapted from f90-find-breakpoint.
2042 (re-search-backward fortran-break-delimiters-re bol
)
2043 (if (not fortran-break-before-delimiters
)
2044 (if (looking-at fortran-no-break-re
)
2045 ;; Deal with cases such as "**" split over
2046 ;; fill-col. Simpler alternative would be
2047 ;; to start from (1- fill-column) above.
2048 (if (> (+ 2 (current-column)) fill-column
)
2053 (or (looking-at fortran-no-break-re
)
2055 ;; Line indented beyond fill-column?
2056 (when (<= (point) bos
)
2057 (move-to-column (1+ fill-column
))
2058 ;; What is this doing???
2059 (or (re-search-forward "[\t\n,'+-/*)=]" eol t
)
2062 (re-search-forward "[ \t]" opoint t
))
2064 ;; If we are in an in-line comment, don't break unless the
2065 ;; line of code is longer than it should be. Otherwise
2066 ;; break the line at the column computed above.
2068 ;; Need to use fortran-find-comment-start-skip to make sure that
2069 ;; quoted !'s don't prevent a break.
2070 (when (and (save-excursion
2072 (if (not (fortran-find-comment-start-skip))
2074 (goto-char (match-beginning 0))
2075 (>= (point) fill-point
)))
2077 (goto-char fill-point
)
2082 (min (1+ fill-column
)
2083 (+ (fortran-calculate-indent)
2084 fortran-continuation-indent
))))
2085 (goto-char fill-point
)
2086 (fortran-break-line)
2089 (defun fortran-break-line ()
2090 "Call `fortran-split-line'. Joins continuation lines first, then refills."
2091 (let ((bol (line-beginning-position))
2094 (if (fortran-find-comment-start-skip)
2095 (delete-and-extract-region
2096 (match-beginning 0) (line-end-position))))))
2097 ;; Forward line 1 really needs to go to next non white line.
2098 (if (save-excursion (forward-line)
2099 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
2102 (delete-region (point) (match-end 0))
2103 (delete-horizontal-space)
2105 (fortran-split-line))
2110 (delete-horizontal-space)
2111 (indent-to (fortran-comment-indent))
2112 (insert comment-string
)))))
2114 (defun fortran-analyze-file-format ()
2115 "Return nil if fixed format is used, t if TAB formatting is used.
2116 Use `fortran-tab-mode-default' if no non-comment statements are found
2117 before the end or in the first `fortran-analyze-depth' lines."
2120 (goto-char (point-min))
2123 (eq (char-after) ?
\t)
2124 (looking-at " \\{6\\}")
2125 (> i fortran-analyze-depth
)))
2129 ((eq (char-after) ?
\t) t
)
2130 ((looking-at " \\{6\\}") nil
)
2131 (t fortran-tab-mode-default
)))))
2133 (defun fortran-fill-paragraph (&optional justify
)
2134 "Fill surrounding comment block as paragraphs, else fill statement.
2135 Intended as the value of `fill-paragraph-function'.
2136 A comment block is filled by calling `fill-comment-paragraph' with
2137 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
2138 Always returns non-nil (to prevent `fill-paragraph' being called)."
2140 (or (fill-comment-paragraph justify
)
2141 (fortran-fill-statement)
2144 (defun fortran-fill-statement ()
2145 "Fill a Fortran statement up to `fill-column'."
2147 (let ((auto-fill-function #'fortran-auto-fill
))
2148 (unless (save-excursion
2150 (or (looking-at "[ \t]*$")
2151 (looking-at fortran-comment-line-start-skip
)
2152 (and comment-start-skip
2153 (looking-at (concat "[ \t]*" comment-start-skip
)))))
2155 ;; Find beginning of statement.
2156 (fortran-next-statement)
2157 (fortran-previous-statement)
2158 ;; Re-indent initially.
2159 (fortran-indent-line)
2160 ;; Replace newline plus continuation field plus indentation with
2164 (fortran-remove-continuation)))
2165 (fortran-previous-statement)))
2166 (fortran-indent-line)))
2168 (defun fortran-strip-sequence-nos (&optional do-space
)
2169 "Delete all text in column `fortran-line-length' (default 72) and up.
2170 This is assumed to be sequence numbers. Normally also deletes
2171 trailing whitespace after stripping such text. Supplying prefix
2172 arg DO-SPACE prevents stripping the whitespace."
2175 (goto-char (point-min))
2176 (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length
)
2178 (replace-match "" nil nil nil
1)
2179 (unless do-space
(delete-horizontal-space)))))
2181 ;; This code used to live in add-log.el, but this is a better place for it.
2182 (defun fortran-current-defun ()
2183 "Function to use for `add-log-current-defun-function' in Fortran mode."
2185 ;; We must be inside function body for this to work.
2186 (fortran-beginning-of-subprogram)
2187 (let ((case-fold-search t
))
2188 ;; Search for fortran subprogram start.
2189 (if (re-search-forward
2190 fortran-start-prog-re
2191 (save-excursion (fortran-end-of-subprogram)
2194 (or (match-string-no-properties 2)
2196 ;; Move to EOL or before first left paren.
2197 (if (re-search-forward "[(\n]" nil t
)
2198 (progn (backward-char)
2199 (skip-chars-backward " \t"))
2201 ;; Use the name preceding that.
2202 (buffer-substring-no-properties (point) (progn (backward-sexp)
2208 ;;; fortran.el ends here