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/>.
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).
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.
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.
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
)
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")
69 (defgroup fortran-indent nil
70 "Indentation variables in Fortran mode."
74 (defgroup fortran-comment nil
75 "Comment-handling variables in Fortran mode."
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."
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
94 (make-mode-line-mouse-map 'mouse-1
98 'fortran-tab-mode-string
))))
99 "String to appear in mode line in TAB format buffers.
100 See Info node `(emacs)ForIndent Cont'."
103 :group
'fortran-indent
)
105 (defcustom fortran-do-indent
3
106 "Extra indentation applied to DO blocks."
109 :group
'fortran-indent
)
111 (defcustom fortran-if-indent
3
112 "Extra indentation applied to IF, SELECT CASE and WHERE blocks."
115 :group
'fortran-indent
)
117 (defcustom fortran-structure-indent
3
118 "Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
121 :group
'fortran-indent
)
123 (defcustom fortran-continuation-indent
5
124 "Extra indentation applied to continuation lines."
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."
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."
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."
165 :group
'fortran-comment
)
167 (defcustom fortran-directive-re
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."
175 :group
'fortran-indent
)
177 (defcustom fortran-minimum-statement-indent-fixed
6
178 "Minimum statement indentation for fixed format continuation style."
181 :group
'fortran-indent
)
183 (defcustom fortran-minimum-statement-indent-tab
(max tab-width
6)
184 "Minimum statement indentation for TAB format continuation style."
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.
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."
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."
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."
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 \"$\"."
228 :safe
(lambda (value) (and (stringp value
) (= (length value
) 1)))
231 (defcustom fortran-comment-region
"c$$$"
232 "String inserted by \\[fortran-comment-region] at start of each \
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."
244 ;; TODO use fortran-line-length, somehow.
245 (defcustom fortran-column-ruler-fixed
246 "0 4 6 10 20 30 40 5\
248 \[ ]|{ | | | | | | | | \
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."
257 ;; TODO use fortran-line-length, somehow.
258 (defcustom fortran-column-ruler-tab
261 \[ ]| { | | | | | | | | \
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."
270 (defcustom fortran-analyze-depth
100
271 "Number of lines to scan to identify fixed or TAB format style."
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'."
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'."
295 :initialize
'custom-initialize-default
296 :set
(lambda (symbol value
)
297 ;; Do all fortran buffers, and the default.
298 (fortran-line-length value t
))
302 (make-variable-buffer-local 'fortran-line-length
)
304 (defcustom fortran-mode-hook nil
305 "Hook run when entering Fortran mode."
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
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
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
348 (mapconcat 'identity
; " " -> "[ \t]*"
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"
361 (mapcar (lambda (x) (concat "implicit " x
))
364 (mapcar (lambda (x) (concat "end " x
))
367 other-types
)) 'paren
))
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
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"
394 ;; Builtin operators.
395 (concat "\\." (regexp-opt
396 '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
397 "neqv" "not" "or" "true")
399 ;; do/goto keywords and targets, and goto tags.
400 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
401 (1 font-lock-keyword-face
)
402 (2 font-lock-constant-face nil t
))
403 '("^ *\\([0-9]+\\)" . font-lock-constant-face
)))
404 "Medium level highlighting for Fortran mode.")
406 (defvar fortran-font-lock-keywords-3
408 fortran-font-lock-keywords-1
409 ;; All type specifiers plus their declared items.
411 (list (concat fortran-type-types
"[ \t(/]*\\(*\\)?")
413 '(1 font-lock-type-face
)
414 ;; Declaration item (or just /.../ block name).
415 `(font-lock-match-c-style-declaration-item-and-skip-to-next
416 ;; Start after any *(...) expression.
418 (and (match-beginning ,(1+ (regexp-opt-depth
419 fortran-type-types
)))
423 ;; No need to clean up.
425 ;; Fontify as a variable name, functions fontified elsewhere.
426 (1 font-lock-variable-name-face nil t
))))
427 ;; Things extra to `fortran-font-lock-keywords-3' (must be done first).
429 ;; Goto-like `err=label'/`end=label' in read/write statements.
430 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
431 (1 font-lock-keyword-face
) (4 font-lock-constant-face nil t
))
432 ;; Standard continuation character and in a TAB-formatted line.
433 '("^ \\{5\\}\\([^ 0\n]\\)" 1 font-lock-string-face
)
434 '("^\t\\([1-9]\\)" 1 font-lock-string-face
))
435 `((,fortran-directive-re
(0 font-lock-keyword-face t
)))
436 ;; `fortran-font-lock-keywords-2' without types (see above).
437 (cdr (nthcdr (length fortran-font-lock-keywords-1
)
438 fortran-font-lock-keywords-2
)))
439 "Gaudy level highlighting for Fortran mode.")
441 (defvar fortran-font-lock-keywords-4
442 (append fortran-font-lock-keywords-3
446 '("int" "ifix" "idint" "real" "float" "sngl"
447 "dble" "cmplx" "ichar" "char" "aint" "dint"
448 "anint" "dnint" "nint" "idnint" "iabs" "abs"
449 "dabs" "cabs" "mod" "amod" "dmod" "isign"
450 "sign" "dsign" "idim" "dim" "ddim" "dprod"
451 "max" "max0" "amax1" "dmax1" "amax0" "max1"
452 "min" "min0" "amin1" "dmin1" "amin0" "min1"
453 "len" "index" "lge" "lgt" "lle" "llt" "aimag"
454 "conjg" "sqrt" "dsqrt" "csqrt" "exp" "dexp"
455 "cexp" "log" "alog" "dlog" "clog" "log10"
456 "alog10" "dlog10" "sin" "dsin" "csin" "cos"
457 "dcos" "ccos" "tan" "dtan" "asin" "dasin"
458 "acos" "dacos" "atan" "datan" "atan2" "datan2"
459 "sinh" "dsinh" "cosh" "dcosh" "tanh" "dtanh")
460 'paren
) "[ \t]*(") '(1 font-lock-builtin-face
))))
461 "Maximum highlighting for Fortran mode.
462 Consists of level 3 plus all other intrinsics not already highlighted.")
464 ;; Comments are real pain in Fortran because there is no way to
465 ;; represent the standard comment syntax in an Emacs syntax table.
466 ;; (We can do so for F90-style). Therefore an unmatched quote in a
467 ;; standard comment will throw fontification off on the wrong track.
468 ;; So we do syntactic fontification with regexps.
469 (defun fortran-font-lock-syntactic-keywords ()
470 "Return a value for `font-lock-syntactic-keywords' in Fortran mode.
471 This varies according to the value of `fortran-line-length'.
472 This is used to fontify fixed-format Fortran comments."
473 `(("^[cd\\*]" 0 (11))
474 (,(format "^[^cd\\*\t\n].\\{%d\\}\\([^\n]+\\)" (1- fortran-line-length
))
477 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
478 "Default expressions to highlight in Fortran mode.")
480 (defvar fortran-imenu-generic-expression
481 ;; These patterns could be confused by sequence nos. in cols 72+ and
482 ;; don't allow continuations everywhere.
486 ;; [This will be fooled by `end function' allowed by G77. Also,
487 ;; it assumes sensible whitespace is employed.]
489 ;; leading whitespace:
491 ;; function declaration with optional type, e.g. `real',
492 ;; `real*4', character(*), `double precision':
493 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
494 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
495 ;; Possible statement continuation:
496 "[ \t" fortran-continuation-string
"]+"
497 ;; Variable to index:
500 ;; Un-named block data.
501 '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
502 "Value for `imenu-generic-expression' in Fortran mode.")
506 (defconst fortran-blocks-re
507 (concat "block[ \t]*data\\|select[ \t]*case\\|"
508 (regexp-opt '("do" "if" "interface" "function" "map" "program"
509 "structure" "subroutine" "union" "where")))
510 "Regexp potentially indicating the start or end of a Fortran \"block\".
511 Omits naked END statements, and DO-loops closed by anything other
514 (defconst fortran-end-block-re
515 ;; Do-loops terminated by things other than ENDDO cannot be handled
516 ;; with a regexp. This omission does not seem to matter to hideshow...
517 (concat "^[ \t0-9]*\\<end[ \t]*\\("
519 ;; Naked END statement.
521 "Regexp matching the end of a Fortran \"block\", from the line start.
522 Note that only ENDDO is handled for the end of a DO-loop. Used
523 in the Fortran entry in `hs-special-modes-alist'.")
525 (defconst fortran-start-block-re
527 "^[ \t0-9]*\\(" ; statement number
528 ;; Structure label for DO, IF, SELECT, WHERE.
529 "\\(\\(\\sw+[ \t]*:[ \t]*\\)?"
530 ;; IF blocks are a nuisance:
531 ;; IF ( ... ) foo is not a block, but a single statement.
532 ;; IF ( ... ) THEN can be split over multiple lines.
533 ;; [So can, eg, a DO WHILE (... ), but that is less common, I hope.]
534 ;; The regexp below allows for it to be split over at most 2 lines.
535 ;; That leads to the problem of not matching two consecutive IF
536 ;; statements as one, eg:
539 ;; It simply is not possible to do this in a 100% correct fashion
540 ;; using a regexp - see the functions fortran-end-if,
541 ;; fortran-beginning-if for the hoops we have to go through.
542 ;; An alternative is to match on THEN at a line end, eg:
543 ;; ".*)[ \t]*then[ \t]*\\($\\|!\\)"
544 ;; This would also match ELSE branches, though. This does not seem
545 ;; right to me, because then one has neighbouring blocks that are
546 ;; not nested in each other.
547 "\\(if[ \t]*(\\(.*\\|"
548 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
549 "do\\|select[ \t]*case\\|where\\)\\)\\|"
550 (regexp-opt '("interface" "function" "map" "program"
551 "structure" "subroutine" "union"))
552 "\\|block[ \t]*data\\)[ \t]*")
553 "Regexp matching the start of a Fortran \"block\", from the line start.
554 A simple regexp cannot do this in fully correct fashion, so this
555 tries to strike a compromise between complexity and flexibility.
556 Used in the Fortran entry in `hs-special-modes-alist'.")
558 (add-to-list 'hs-special-modes-alist
559 `(fortran-mode ,fortran-start-block-re
,fortran-end-block-re
560 "^[cC*!]" fortran-end-of-block nil
))
563 (defvar fortran-mode-syntax-table
564 (let ((table (make-syntax-table)))
565 ;; Was a word-constituent (for abbrevs), now punctuation (g77
566 ;; multi-statement lines).
567 (modify-syntax-entry ?\
; "." table)
568 (modify-syntax-entry ?
\r " " table
)
569 (modify-syntax-entry ?
+ "." table
)
570 (modify-syntax-entry ?-
"." table
)
571 (modify-syntax-entry ?
= "." table
)
572 (modify-syntax-entry ?
* "." table
)
573 (modify-syntax-entry ?
/ "." table
)
574 (modify-syntax-entry ?
\' "\"" table
)
575 (modify-syntax-entry ?
\" "\"" table
)
576 ;; Consistent with GNU Fortran's default -- see the manual.
577 ;; The F77 standard imposes no rule on this issue.
578 (modify-syntax-entry ?
\\ "\\" table
)
579 ;; This might be better as punctuation, as for C, but this way you
580 ;; can treat floating-point numbers as symbols.
581 (modify-syntax-entry ?.
"_" table
) ; e.g. `a.ne.b'
582 (modify-syntax-entry ?_
"_" table
)
583 (modify-syntax-entry ?$
"_" table
) ; esp. VMSisms
584 (modify-syntax-entry ?\
! "<" table
)
585 (modify-syntax-entry ?
\n ">" table
)
587 "Syntax table used in Fortran mode.")
589 (defvar fortran-gud-syntax-table
590 (let ((st (make-syntax-table fortran-mode-syntax-table
)))
591 (modify-syntax-entry ?
\n "." st
)
593 "Syntax table used to parse Fortran expressions for printing in GUD.")
595 (defvar fortran-mode-map
596 (let ((map (make-sparse-keymap)))
597 (define-key map
";" 'fortran-abbrev-start
)
598 (define-key map
"\C-c;" 'fortran-comment-region
)
599 ;; The default comment-dwim does at least as much as this.
600 ;;; (define-key map "\M-;" 'fortran-indent-comment)
601 (define-key map
"\M-\n" 'fortran-split-line
)
602 (define-key map
"\M-\C-n" 'fortran-end-of-block
)
603 (define-key map
"\M-\C-p" 'fortran-beginning-of-block
)
604 (define-key map
"\M-\C-q" 'fortran-indent-subprogram
)
605 (define-key map
"\C-c\C-w" 'fortran-window-create-momentarily
)
606 (define-key map
"\C-c\C-r" 'fortran-column-ruler
)
607 (define-key map
"\C-c\C-p" 'fortran-previous-statement
)
608 (define-key map
"\C-c\C-n" 'fortran-next-statement
)
609 (define-key map
"\C-c\C-d" 'fortran-join-line
) ; like f90
610 (define-key map
"\M-^" 'fortran-join-line
) ; subvert delete-indentation
611 (define-key map
"0" 'fortran-electric-line-number
)
612 (define-key map
"1" 'fortran-electric-line-number
)
613 (define-key map
"2" 'fortran-electric-line-number
)
614 (define-key map
"3" 'fortran-electric-line-number
)
615 (define-key map
"4" 'fortran-electric-line-number
)
616 (define-key map
"5" 'fortran-electric-line-number
)
617 (define-key map
"6" 'fortran-electric-line-number
)
618 (define-key map
"7" 'fortran-electric-line-number
)
619 (define-key map
"8" 'fortran-electric-line-number
)
620 (define-key map
"9" 'fortran-electric-line-number
)
622 (easy-menu-define fortran-menu map
"Menu for Fortran mode."
624 ["Manual" (info "(emacs)Fortran") :active t
625 :help
"Read the Emacs manual chapter on Fortran mode"]
627 ,(custom-menu-create 'fortran
)
629 ["Set" Custom-set
:active t
630 :help
"Set current value of all edited settings in the buffer"]
631 ["Save" Custom-save
:active t
632 :help
"Set and save all edited settings"]
633 ["Reset to Current" Custom-reset-current
:active t
634 :help
"Reset all edited settings to current"]
635 ["Reset to Saved" Custom-reset-saved
:active t
636 :help
"Reset all edited or set settings to saved"]
637 ["Reset to Standard Settings" Custom-reset-standard
:active t
638 :help
"Erase all cusomizations in buffer"]
641 ["Comment Region" fortran-comment-region mark-active
]
643 (fortran-comment-region (region-beginning) (region-end) 1)
645 ["Indent Region" indent-region mark-active
]
646 ["Indent Subprogram" fortran-indent-subprogram t
]
648 ["Beginning of Subprogram" fortran-beginning-of-subprogram
:active t
649 :help
"Move point to the start of the current subprogram"]
650 ["End of Subprogram" fortran-end-of-subprogram
:active t
651 :help
"Move point to the end of the current subprogram"]
653 :help
"Mark a region of code"
654 ["Subprogram" mark-defun
t]
655 ["IF Block" fortran-mark-if t
]
656 ["DO Block" fortran-mark-do t
]
658 ["Narrow to Subprogram" narrow-to-defun
t]
661 ["Temporary Column Ruler" fortran-column-ruler
:active t
662 :help
"Briefly display Fortran column numbers"]
663 ;; May not be '72', depending on fortran-line-length, but this
664 ;; seems ok for a menu item.
665 ["72-column Window" fortran-window-create
:active t
666 :help
"Set window width to Fortran line length"]
668 (enlarge-window-horizontally (- (frame-width) (window-width)))
669 :active
(not (window-full-width-p))
670 :help
"Make window full width"]
671 ["Momentary 72-Column Window" fortran-window-create-momentarily
672 :active t
:help
"Briefly set window width to Fortran line length"]
674 ["Break Line at Point" fortran-split-line
:active t
675 :help
"Break the current line at point"]
676 ["Join Line" fortran-join-line
:active t
677 :help
"Join the current line to the previous one"]
678 ["Fill Statement/Comment" fill-paragraph t
]
680 ["Toggle Auto Fill" auto-fill-mode
:selected auto-fill-function
682 :help
"Automatically fill text while typing in this buffer"]
683 ["Toggle Abbrev Mode" abbrev-mode
:selected abbrev-mode
684 :style toggle
:help
"Expand abbreviations while typing in this buffer"]
685 ["Add Imenu Menu" imenu-add-menubar-index
686 :active
(not (lookup-key (current-local-map) [menu-bar index
]))
687 :included
(fboundp 'imenu-add-to-menubar
)
688 :help
"Add an index menu to the menu-bar"]))
690 "Keymap used in Fortran mode.")
693 (define-abbrev-table 'fortran-mode-abbrev-table
694 (mapcar (lambda (e) (list (car e
) (cdr e
) nil
:system t
))
695 '((";au" .
"automatic" )
697 (";bd" .
"block data" )
698 (";ch" .
"character" )
704 (";di" .
"dimension" )
706 (";dc" .
"double complex" )
707 (";dp" .
"double precision" )
708 (";dw" .
"do while" )
713 (";eq" .
"equivalence" )
714 (";ew" .
"endwhere" )
715 (";ex" .
"external" )
719 (";fu" .
"function" )
721 (";im" .
"implicit" )
722 (";ib" .
"implicit byte" )
723 (";ic" .
"implicit complex" )
724 (";ich" .
"implicit character")
725 (";ii" .
"implicit integer" )
726 (";il" .
"implicit logical" )
727 (";ir" .
"implicit real" )
728 (";inc" .
"include" )
730 (";intr" .
"intrinsic" )
733 (";o" .
"open" ) ; was ;op
734 (";pa" .
"parameter" )
745 (";st" .
"structure" )
747 (";su" .
"subroutine" )
750 (";vo" .
"volatile" )
753 "Abbrev table for Fortran mode."
754 ;; Accept ; as the first char of an abbrev. Also allow _ in abbrevs.
755 :regexp
"\\(?:[^[:word:]_;]\\|^\\)\\(;?[[:word:]_]+\\)[^[:word:]_]*")
759 (defun fortran-mode ()
760 "Major mode for editing Fortran code in fixed format.
761 For free format code, use `f90-mode'.
763 \\[fortran-indent-line] indents the current Fortran line correctly.
764 Note that DO statements must not share a common CONTINUE.
766 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
772 Variables controlling indentation style and extra features:
774 `fortran-comment-line-start'
775 To use comments starting with `!', set this to the string \"!\".
777 Extra indentation within DO blocks (default 3).
779 Extra indentation within IF blocks (default 3).
780 `fortran-structure-indent'
781 Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
783 `fortran-continuation-indent'
784 Extra indentation applied to continuation statements (default 5).
785 `fortran-comment-line-extra-indent'
786 Amount of extra indentation for text in full-line comments (default 0).
787 `fortran-comment-indent-style'
788 How to indent the text in full-line comments. Allowed values are:
789 nil don't change the indentation
790 fixed indent to `fortran-comment-line-extra-indent' beyond the
792 `fortran-minimum-statement-indent-fixed' (fixed format) or
793 `fortran-minimum-statement-indent-tab' (TAB format),
794 depending on the continuation format in use.
795 relative indent to `fortran-comment-line-extra-indent' beyond the
796 indentation for a line of code.
798 `fortran-comment-indent-char'
799 Single-character string to be inserted instead of space for
800 full-line comment indentation (default \" \").
801 `fortran-minimum-statement-indent-fixed'
802 Minimum indentation for statements in fixed format mode (default 6).
803 `fortran-minimum-statement-indent-tab'
804 Minimum indentation for statements in TAB format mode (default 9).
805 `fortran-line-number-indent'
806 Maximum indentation for line numbers (default 1). A line number will
807 get less than this much indentation if necessary to avoid reaching
809 `fortran-check-all-num-for-matching-do'
810 Non-nil causes all numbered lines to be treated as possible \"continue\"
811 statements (default nil).
812 `fortran-blink-matching-if'
813 Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
814 to blink on the matching IF (or DO [WHILE]). (default nil)
815 `fortran-continuation-string'
816 Single-character string to be inserted in column 5 of a continuation
817 line (default \"$\").
818 `fortran-comment-region'
819 String inserted by \\[fortran-comment-region] at start of each line in
820 the region (default \"c$$$\").
821 `fortran-electric-line-number'
822 Non-nil causes line number digits to be moved to the correct column
823 as typed (default t).
824 `fortran-break-before-delimiters'
825 Non-nil causes lines to be broken before delimiters (default t).
827 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
828 with no args, if that value is non-nil."
830 (kill-all-local-variables)
831 (setq major-mode
'fortran-mode
833 local-abbrev-table fortran-mode-abbrev-table
)
834 (set-syntax-table fortran-mode-syntax-table
)
835 (use-local-map fortran-mode-map
)
836 (set (make-local-variable 'indent-line-function
) 'fortran-indent-line
)
837 (set (make-local-variable 'indent-region-function
)
839 (let (fortran-blink-matching-if ; avoid blinking delay
840 indent-region-function
)
841 (indent-region start end nil
))))
842 (set (make-local-variable 'require-final-newline
) mode-require-final-newline
)
843 ;; The syntax tables don't understand the column-0 comment-markers.
844 (set (make-local-variable 'comment-use-syntax
) nil
)
845 (set (make-local-variable 'comment-padding
) "$$$")
846 (set (make-local-variable 'comment-start
) fortran-comment-line-start
)
847 (set (make-local-variable 'comment-start-skip
)
848 ;; We can't reuse `fortran-comment-line-start-skip' directly because
849 ;; it contains backrefs whereas we need submatch-1 to end at the
850 ;; beginning of the comment delimiter.
851 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
852 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
853 (set (make-local-variable 'comment-indent-function
) 'fortran-comment-indent
)
854 (set (make-local-variable 'comment-region-function
) 'fortran-comment-region
)
855 (set (make-local-variable 'uncomment-region-function
)
856 'fortran-uncomment-region
)
857 (set (make-local-variable 'comment-insert-comment-function
)
858 'fortran-indent-comment
)
859 (set (make-local-variable 'abbrev-all-caps
) t
)
860 (set (make-local-variable 'normal-auto-fill-function
) 'fortran-auto-fill
)
861 (set (make-local-variable 'indent-tabs-mode
) (fortran-analyze-file-format))
862 (setq mode-line-process
'(indent-tabs-mode fortran-tab-mode-string
))
863 (set (make-local-variable 'fill-column
) fortran-line-length
)
864 (set (make-local-variable 'fill-paragraph-function
) 'fortran-fill-paragraph
)
865 (set (make-local-variable 'font-lock-defaults
)
866 '((fortran-font-lock-keywords
867 fortran-font-lock-keywords-1
868 fortran-font-lock-keywords-2
869 fortran-font-lock-keywords-3
870 fortran-font-lock-keywords-4
)
871 nil t
((?
/ .
"$/") ("_$" .
"w"))
872 fortran-beginning-of-subprogram
873 (font-lock-syntactic-keywords
874 . fortran-font-lock-syntactic-keywords
)))
875 (set (make-local-variable 'imenu-case-fold-search
) t
)
876 (set (make-local-variable 'imenu-generic-expression
)
877 fortran-imenu-generic-expression
)
878 (set (make-local-variable 'imenu-syntax-alist
) '(("_$" .
"w")))
879 (set (make-local-variable 'beginning-of-defun-function
)
880 #'fortran-beginning-of-subprogram
)
881 (set (make-local-variable 'end-of-defun-function
)
882 #'fortran-end-of-subprogram
)
883 (set (make-local-variable 'add-log-current-defun-function
)
884 #'fortran-current-defun
)
885 (set (make-local-variable 'dabbrev-case-fold-search
) 'case-fold-search
)
886 (set (make-local-variable 'gud-find-expr-function
) 'fortran-gud-find-expr
)
887 (add-hook 'hack-local-variables-hook
'fortran-hack-local-variables nil t
)
888 (run-mode-hooks 'fortran-mode-hook
))
891 (defun fortran-line-length (nchars &optional global
)
892 "Set the length of fixed-form Fortran lines to NCHARS.
893 This normally only affects the current buffer, which must be in
894 Fortran mode. If the optional argument GLOBAL is non-nil, it
895 affects all Fortran buffers, and also the default."
899 (with-current-buffer buff
900 (when (eq major-mode
'fortran-mode
)
901 (setq fortran-line-length nchars
902 fill-column fortran-line-length
903 new
(fortran-font-lock-syntactic-keywords))
904 ;; Refontify only if necessary.
905 (unless (equal new font-lock-syntactic-keywords
)
906 (setq font-lock-syntactic-keywords
907 (fortran-font-lock-syntactic-keywords))
908 (if font-lock-mode
(font-lock-mode 1))))))
911 (list (current-buffer))))
913 (setq-default fortran-line-length nchars
))))
915 (defun fortran-hack-local-variables ()
916 "Fortran mode adds this to `hack-local-variables-hook'."
917 (fortran-line-length fortran-line-length
))
919 (declare-function gud-find-c-expr
"gud.el" nil
)
921 (defun fortran-gud-find-expr ()
922 ;; Consider \n as punctuation (end of expression).
923 (with-syntax-table fortran-gud-syntax-table
926 (defsubst fortran-comment-indent
()
927 "Return the indentation appropriate for the current comment line.
928 This is 0 for a line matching `fortran-comment-line-start-skip', else
929 the value of `comment-column' (leaving at least one space after code)."
930 (if (looking-at fortran-comment-line-start-skip
) 0
932 (skip-chars-backward " \t")
933 (max (1+ (current-column)) comment-column
))))
935 (defun fortran-indent-comment ()
936 "Align or create comment on current line.
937 Existing comments of all types are recognized and aligned.
938 If the line has no comment, a side-by-side comment is inserted and aligned,
939 if the value of `comment-start' is not nil and allows such comments.
940 Otherwise, a separate-line comment is inserted, on this line
941 or on a new line inserted before this line if this line is not blank."
944 ;; Recognize existing comments of either kind.
945 (cond ((fortran-find-comment-start-skip 'all
)
946 (goto-char (match-beginning 0))
948 (fortran-indent-line)
949 (unless (= (current-column) (fortran-comment-indent))
950 (delete-horizontal-space)
951 (indent-to (fortran-comment-indent)))))
952 ;; No existing comment.
953 ;; If side-by-side comments are defined, insert one,
954 ;; unless line is now blank.
955 ((and comment-start
(not (looking-at "[ \t]*$"))
956 (string-match comment-start-skip
(concat " " comment-start
)))
958 (delete-horizontal-space)
959 (indent-to (fortran-comment-indent))
960 (insert comment-start
))
961 ;; Else insert separate-line comment, making a new line if nec.
963 (if (looking-at "^[ \t]*$")
964 (delete-horizontal-space)
968 (insert fortran-comment-line-start
)
969 (insert-char (if (stringp fortran-comment-indent-char
)
970 (aref fortran-comment-indent-char
0)
971 fortran-comment-indent-char
)
972 (- (fortran-calculate-indent) (current-column))))))
974 (defun fortran-comment-region (beg-region end-region arg
)
975 "Comment every line in the region.
976 Inserts the string variable `fortran-comment-region' at the beginning of
977 every line in the region.
978 BEG-REGION and END-REGION specify the region boundaries.
979 With non-nil ARG, uncomments the region."
980 (interactive "*r\nP")
981 (let ((end-region-mark (copy-marker end-region
))
982 (save-point (point-marker)))
983 (goto-char beg-region
)
986 (let ((com (regexp-quote fortran-comment-region
))) ; uncomment
988 (delete-region (point) (match-end 0)))
989 (while (and (zerop (forward-line 1))
990 (< (point) end-region-mark
))
992 (delete-region (point) (match-end 0)))))
993 (insert fortran-comment-region
) ; comment
994 (while (and (zerop (forward-line 1))
995 (< (point) end-region-mark
))
996 (insert fortran-comment-region
)))
997 (goto-char save-point
)
998 (set-marker end-region-mark nil
)
999 (set-marker save-point nil
)))
1001 ;; uncomment-region calls this with 3 args.
1002 (defun fortran-uncomment-region (start end
&optional ignored
)
1003 "Uncomment every line in the region."
1004 (fortran-comment-region start end t
))
1007 (defun fortran-abbrev-start ()
1008 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
1009 Any other key combination is executed normally."
1011 (insert last-command-event
)
1012 (let* ((event (if (fboundp 'next-command-event
) ; XEmacs
1013 (next-command-event)
1015 (char (if (fboundp 'event-to-character
)
1016 (event-to-character event
) event
)))
1017 ;; Insert char if not equal to `?', or if abbrev-mode is off.
1018 (if (and abbrev-mode
(or (eq char ??
) (eq char help-char
)
1019 (memq event help-event-list
)))
1020 (fortran-abbrev-help)
1021 (push event unread-command-events
))))
1023 (defun fortran-abbrev-help ()
1024 "List the currently defined abbrevs in Fortran mode."
1026 (message "Listing abbrev table...")
1027 (display-buffer (fortran-prepare-abbrev-list-buffer))
1028 (message "Listing abbrev table...done"))
1030 (defun fortran-prepare-abbrev-list-buffer ()
1031 "Create a buffer listing the Fortran mode abbreviations."
1032 (with-current-buffer (get-buffer-create "*Abbrevs*")
1034 (insert-abbrev-table-description 'fortran-mode-abbrev-table t
)
1035 (goto-char (point-min))
1036 (set-buffer-modified-p nil
)
1037 (edit-abbrevs-mode))
1038 (get-buffer-create "*Abbrevs*"))
1040 (defun fortran-column-ruler ()
1041 "Insert a column ruler momentarily above current line, till next keystroke.
1042 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
1043 format mode, and `fortran-column-ruler-tab' in TAB format mode.
1044 The next key typed is executed unless it is SPC."
1046 (momentary-string-display
1047 (if indent-tabs-mode
1048 fortran-column-ruler-tab
1049 fortran-column-ruler-fixed
)
1052 (if (eq (window-start (selected-window))
1053 (window-point (selected-window)))
1054 (line-beginning-position 2)
1056 nil
"Type SPC or any command to erase ruler."))
1058 (defun fortran-window-create ()
1059 "Make the window `fortran-line-length' (default 72) columns wide.
1060 See also `fortran-window-create-momentarily'."
1062 (let ((window-min-width 2))
1063 (unless (window-full-width-p)
1064 (enlarge-window-horizontally (- (frame-width)
1066 (let* ((window-edges (window-edges))
1067 (scroll-bar-width (- (nth 2 window-edges
)
1070 (split-window-horizontally (+ fortran-line-length scroll-bar-width
)))
1072 (switch-to-buffer " fortran-window-extra" t
)
1073 (select-window (previous-window))))
1075 (defun fortran-window-create-momentarily (&optional arg
)
1076 "Momentarily make the window `fortran-line-length' (default 72) columns wide.
1077 Optional ARG non-nil and non-unity disables the momentary feature.
1078 See also `fortran-window-create'."
1082 (save-window-excursion
1085 (fortran-window-create)
1086 (error (error "No room for Fortran window")))
1087 (message "Type SPC to continue editing.")
1088 (let ((char (read-event)))
1089 (or (equal char ?\s
)
1090 (setq unread-command-events
(list char
))))))
1091 (fortran-window-create)))
1093 (defun fortran-split-line ()
1094 "Break line at point and insert continuation marker and alignment."
1096 (delete-horizontal-space)
1098 (let ((pos (point)))
1100 (and (fortran-find-comment-start-skip 'all
)
1101 (< (match-beginning 0) pos
))))
1102 (insert ?
\n (match-string 0))
1103 (if indent-tabs-mode
1104 (insert ?
\n ?
\t (fortran-numerical-continuation-char))
1105 (insert "\n " fortran-continuation-string
))) ; space after \n important
1106 (fortran-indent-line)) ; when cont string is C, c or *
1108 (defun fortran-remove-continuation ()
1109 "Delete any Fortran continuation characters at point.
1110 Returns t if anything actually deleted."
1111 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1113 (delete-indentation)
1116 (defun fortran-join-line (arg)
1117 "Join current line to the previous one and re-indent.
1118 With a prefix argument, repeat this operation that many times.
1119 If the prefix argument ARG is negative, join the next -ARG lines.
1120 Continuation lines are correctly handled."
1126 (while (not (zerop arg
))
1128 (or (fortran-remove-continuation)
1129 (delete-indentation))
1130 (setq arg
(1- arg
)))
1131 (fortran-indent-line)))
1133 (defun fortran-numerical-continuation-char ()
1134 "Return a digit for tab-digit style of continuation lines.
1135 If previous line is a tab-digit continuation line, return that digit
1136 plus one, otherwise return 1. Zero not allowed."
1139 (if (looking-at "\t[1-9]")
1140 (+ ?
1 (%
(- (char-after (1+ (point))) ?
0) 9))
1143 (put 'fortran-electric-line-number
'delete-selection t
)
1144 (defun fortran-electric-line-number (arg)
1145 "Self insert, but if part of a Fortran line number indent it automatically.
1146 Auto-indent does not happen if a numeric ARG is used."
1148 (if (or arg
(not fortran-electric-line-number
))
1150 (self-insert-command (prefix-numeric-value arg
))
1151 (self-insert-command 1))
1152 (if (or (and (= 5 (current-column))
1155 ;; In col 5 with only spaces to the left.
1156 (looking-at " \\{5\\}")))
1157 (and (= (if indent-tabs-mode
1158 fortran-minimum-statement-indent-tab
1159 fortran-minimum-statement-indent-fixed
) (current-column))
1160 ;; In col 8 with a single tab to the left.
1161 (eq ?
\t (char-after (line-beginning-position)))
1162 (not (or (eq last-command
'fortran-indent-line
)
1164 'fortran-indent-new-line
))))
1166 (re-search-backward "[^ \t0-9]"
1167 (line-beginning-position)
1168 t
)) ; not a line number
1169 (looking-at "[0-9]")) ; within a line number
1170 (self-insert-command (prefix-numeric-value arg
))
1171 (skip-chars-backward " \t")
1172 (insert last-command-event
)
1173 (fortran-indent-line))))
1176 (defun fortran-check-end-prog-re ()
1177 "Check a preliminary match against `fortran-end-prog-re'."
1178 ;; Having got a possible match for the subprogram end, we need a
1179 ;; match of whitespace, avoiding possible column 73+ stuff.
1181 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
1182 (buffer-substring (match-end 0)
1183 (min (line-end-position)
1184 (+ fortran-line-length
1185 (line-beginning-position)))))))
1187 ;; This is more complex than first expected because the beginning of a
1188 ;; main program may be implicit (ie not marked by a PROGRAM statement).
1189 ;; This would be fine (we could just go to bob in the absence of a match),
1190 ;; except it need not even be the first subprogram in the file (eg it
1191 ;; could follow a subroutine). Hence we have to search for END
1192 ;; statements instead.
1193 ;; cf fortran-beginning-of-block, f90-beginning-of-subprogram
1194 ;; Note that unlike the latter, we don't have to worry about nested
1197 (defun fortran-beginning-of-subprogram ()
1198 "Move point to the beginning of the current Fortran subprogram."
1200 (let ((case-fold-search t
))
1201 ;; If called already at the start of subprogram, go to the previous.
1202 (beginning-of-line (if (bolp) 0 1))
1204 (or (looking-at fortran-start-prog-re
)
1205 ;; This leaves us at bob if before the first subprogram.
1206 (eq (fortran-previous-statement) 'first-statement
)
1208 (while (re-search-backward fortran-end-prog-re nil
'move
)
1209 (if (fortran-check-end-prog-re) (throw 'ok t
))))
1210 ;; If the search failed, must be at bob.
1211 ;; First code line is the start of the subprogram.
1212 ;; FIXME use a more rigorous test, cf fortran-next-statement?
1213 ;; Though that needs to handle continuations too.
1214 (not (looking-at "^\\([ \t]*[0-9]\\|[ \t]+[^!#]\\)")))
1215 (fortran-next-statement))))))
1217 ;; This is simpler than f-beginning-of-s because the end of a
1218 ;; subprogram is never implicit.
1219 (defun fortran-end-of-subprogram ()
1220 "Move point to the end of the current Fortran subprogram."
1222 (let ((case-fold-search t
))
1225 (while (and (re-search-forward fortran-end-prog-re nil
'move
)
1226 (not (fortran-check-end-prog-re))))
1229 (defun fortran-previous-statement ()
1230 "Move point to beginning of the previous Fortran statement.
1231 Returns 'first-statement if that statement is the first
1232 non-comment Fortran statement in the file, and nil otherwise.
1233 Directive lines are treated as comments."
1235 (let (not-first-statement continue-test
)
1239 (not (looking-at fortran-comment-line-start-skip
))
1240 (not (looking-at fortran-directive-re
))
1243 (regexp-quote fortran-continuation-string
)))
1244 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1245 (while (and (setq not-first-statement
(zerop (forward-line -
1)))
1246 (or (looking-at fortran-comment-line-start-skip
)
1247 (looking-at fortran-directive-re
)
1250 (regexp-quote fortran-continuation-string
)))
1251 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1252 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1253 (cond ((and continue-test
1254 (not not-first-statement
))
1255 (message "Incomplete continuation statement."))
1257 (fortran-previous-statement))
1258 ((not not-first-statement
)
1259 'first-statement
))))
1261 (defun fortran-next-statement ()
1262 "Move point to beginning of the next Fortran statement.
1263 Returns 'last-statement if that statement is the last
1264 non-comment Fortran statement in the file, and nil otherwise.
1265 Directive lines are treated as comments."
1267 (let (not-last-statement)
1269 (while (and (setq not-last-statement
1270 (and (zerop (forward-line 1))
1272 (or (looking-at fortran-comment-line-start-skip
)
1273 (looking-at fortran-directive-re
)
1274 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1275 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1276 (if (not not-last-statement
)
1279 (defun fortran-looking-at-if-then ()
1280 "Return non-nil if at the start of a line with an IF ... THEN statement."
1281 ;; cf f90-looking-at-if-then.
1283 (i (fortran-beginning-if)))
1290 ;; Used in hs-special-modes-alist.
1291 (defun fortran-end-of-block (&optional num
)
1292 "Move point forward to the end of the current code block.
1293 With optional argument NUM, go forward that many balanced blocks.
1294 If NUM is negative, go backward to the start of a block. Does
1295 not check for consistency of block types. Interactively, pushes
1296 mark before moving point."
1298 (if (called-interactively-p 'any
) (push-mark (point) t
))
1299 (and num
(< num
0) (fortran-beginning-of-block (- num
)))
1300 (let ((case-fold-search t
)
1303 (while (and (> count
0)
1305 (concat "\\(" fortran-blocks-re
1306 (if fortran-check-all-num-for-matching-do
1307 "\\|^[ \t]*[0-9]+" "")
1308 "\\|continue\\|end\\)\\>")
1311 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re
))
1312 (fortran-looking-at-if-then)
1313 (looking-at fortran-start-block-re
))
1314 (setq count
(1+ count
))
1315 (if (or (looking-at fortran-end-block-re
)
1316 (and (or (looking-at "^[0-9 \t]*continue")
1317 (and fortran-check-all-num-for-matching-do
1318 (looking-at "[ \t]*[0-9]+")))
1319 (fortran-check-for-matching-do)))
1320 (setq count
(1- count
))))
1322 (if (> count
0) (error "Missing block end"))))
1324 (defun fortran-beginning-of-block (&optional num
)
1325 "Move point backwards to the start of the current code block.
1326 With optional argument NUM, go backward that many balanced
1327 blocks. If NUM is negative, go forward to the end of a block.
1328 Does not check for consistency of block types. Interactively,
1329 pushes mark before moving point."
1331 (if (called-interactively-p 'any
) (push-mark (point) t
))
1332 (and num
(< num
0) (fortran-end-of-block (- num
)))
1333 (let ((case-fold-search t
)
1336 (while (and (> count
0)
1338 (concat "\\(" fortran-blocks-re
1339 (if fortran-check-all-num-for-matching-do
1340 "\\|^[ \t]*[0-9]+" "")
1341 "\\|continue\\|end\\)\\>")
1344 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re
))
1345 (fortran-looking-at-if-then)
1346 (looking-at fortran-start-block-re
))
1347 (setq count
(1- count
))
1348 (if (or (looking-at fortran-end-block-re
)
1349 (and (or (looking-at "^[0-9 \t]*continue")
1350 (and fortran-check-all-num-for-matching-do
1351 (looking-at "[ \t]*[0-9]+")))
1352 (fortran-check-for-matching-do)))
1353 (setq count
(1+ count
)))))
1354 ;; Includes an un-named main program block.
1355 (if (> count
0) (error "Missing block start"))))
1358 (defun fortran-blink-match (regex keyword find-begin
)
1359 "From a line matching REGEX, blink matching KEYWORD statement line.
1360 Use function FIND-BEGIN to match it."
1361 (let ((top-of-window (window-start))
1363 (case-fold-search t
)
1366 (when (save-excursion
1368 (skip-chars-forward " \t0-9")
1370 (if (not (setq matching
(funcall find-begin
)))
1371 (setq message
(concat "No matching " keyword
"."))
1372 (if (< matching top-of-window
)
1374 (goto-char matching
)
1378 (buffer-substring (point)
1379 (line-end-position)))))))
1381 (message "%s" message
)
1382 (goto-char matching
)
1383 (sit-for blink-matching-delay
)
1384 (goto-char end-point
)))))
1386 (defun fortran-blink-matching-if ()
1387 "From an ENDIF or ELSE statement, blink the matching IF statement."
1388 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1389 "if" #'fortran-beginning-if
))
1391 (defun fortran-blink-matching-do ()
1392 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1393 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do
))
1395 (defun fortran-mark-do ()
1396 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1397 The marks are pushed."
1399 (let (enddo-point do-point
)
1400 (if (setq enddo-point
(fortran-end-do))
1401 (if (not (setq do-point
(fortran-beginning-do)))
1402 (message "No matching do.")
1403 (goto-char enddo-point
)
1405 (goto-char do-point
)))))
1407 (defun fortran-end-do ()
1408 "Search forward for first unmatched ENDDO.
1409 Return point or nil."
1410 (let ((case-fold-search t
))
1411 (if (save-excursion (beginning-of-line)
1412 (skip-chars-forward " \t0-9")
1413 (looking-at "end[ \t]*do\\b"))
1419 (while (and (not (zerop count
))
1420 (not (eq (fortran-next-statement) 'last-statement
))
1421 ;; Keep local to subprogram.
1422 (not (and (looking-at fortran-end-prog-re
)
1423 (fortran-check-end-prog-re))))
1424 (skip-chars-forward " \t0-9")
1425 (cond ((looking-at "end[ \t]*do\\b")
1426 (setq count
(1- count
)))
1428 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1429 (setq count
(1+ count
)))))
1431 ;; All pairs accounted for.
1434 (defun fortran-beginning-do ()
1435 "Search backwards for first unmatched DO [WHILE].
1436 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1437 (let ((case-fold-search t
)
1438 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1441 (skip-chars-forward " \t0-9")
1442 (looking-at dostart-re
))
1448 (while (and (not (zerop count
))
1449 (not (eq (fortran-previous-statement) 'first-statement
))
1450 ;; Keep local to subprogram.
1451 (not (and (looking-at fortran-end-prog-re
)
1452 (fortran-check-end-prog-re))))
1453 (skip-chars-forward " \t0-9")
1454 (cond ((looking-at dostart-re
)
1455 (setq count
(1- count
)))
1456 ;; Note labelled loop ends not considered.
1457 ((looking-at "end[ \t]*do\\b")
1458 (setq count
(1+ count
)))))
1460 ;; All pairs accounted for.
1463 (defun fortran-mark-if ()
1464 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1465 The marks are pushed."
1467 (let (endif-point if-point
)
1468 (if (setq endif-point
(fortran-end-if))
1469 (if (not (setq if-point
(fortran-beginning-if)))
1470 (message "No matching if.")
1471 ;; Set mark, move point.
1472 (goto-char endif-point
)
1474 (goto-char if-point
)))))
1476 (defun fortran-end-if ()
1477 "Search forwards for first unmatched ENDIF.
1478 Return point or nil."
1479 (let ((case-fold-search t
))
1480 (if (save-excursion (beginning-of-line)
1481 (skip-chars-forward " \t0-9")
1482 (looking-at "end[ \t]*if\\b"))
1485 ;; Search for one. The point has been already been moved to first
1486 ;; letter on line but this should not cause troubles.
1489 (while (and (not (zerop count
))
1490 (not (eq (fortran-next-statement) 'last-statement
))
1491 ;; Keep local to subprogram.
1492 (not (and (looking-at fortran-end-prog-re
)
1493 (fortran-check-end-prog-re))))
1494 (skip-chars-forward " \t0-9")
1495 (cond ((looking-at "end[ \t]*if\\b")
1496 (setq count
(1- count
)))
1497 ((looking-at fortran-if-start-re
)
1500 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1501 (let (then-test) ; multi-line if-then
1504 (zerop (forward-line 1))
1505 ;; Search forward for then.
1506 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1510 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1512 (setq count
(1+ count
)))))))
1514 ;; All pairs accounted for.
1517 (defun fortran-beginning-if ()
1518 "Search backwards for first unmatched IF-THEN.
1519 Return point or nil."
1520 (let ((case-fold-search t
))
1522 ;; May be sitting on multi-line if-then statement, first
1523 ;; move to beginning of current statement. Note:
1524 ;; `fortran-previous-statement' moves to previous statement
1525 ;; *unless* current statement is first one. Only move
1526 ;; forward if not first-statement.
1527 (if (not (eq (fortran-previous-statement) 'first-statement
))
1528 (fortran-next-statement))
1529 (skip-chars-forward " \t0-9")
1531 (looking-at fortran-if-start-re
)
1533 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1534 ;; Multi-line if-then.
1537 (and (zerop (forward-line 1))
1538 ;; Search forward for then.
1539 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1543 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1550 (while (and (not (zerop count
))
1551 (not (eq (fortran-previous-statement) 'first-statement
))
1552 ;; Keep local to subprogram.
1553 (not (and (looking-at fortran-end-prog-re
)
1554 (fortran-check-end-prog-re))))
1555 (skip-chars-forward " \t0-9")
1556 (cond ((looking-at fortran-if-start-re
)
1559 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1560 (let (then-test) ; multi-line if-then
1563 (zerop (forward-line 1))
1564 ;; Search forward for then.
1565 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1569 (concat ".*then\\b[ \t]*"
1570 "[^ \t(=a-z0-9]"))))))
1572 (setq count
(1- count
)))))
1573 ((looking-at "end[ \t]*if\\b")
1574 (setq count
(1+ count
)))))
1576 ;; All pairs accounted for.
1580 (defun fortran-indent-line ()
1581 "Indent current Fortran line based on its contents and on previous lines."
1583 (let ((cfi (fortran-calculate-indent)))
1586 (if (or (not (= cfi
(fortran-current-line-indentation)))
1587 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
1588 (not (fortran-line-number-indented-correctly-p))))
1589 (fortran-indent-to-column cfi
)
1591 (if (fortran-find-comment-start-skip)
1592 (fortran-indent-comment))))
1593 ;; Never leave point in left margin.
1594 (if (< (current-column) cfi
)
1595 (move-to-column cfi
))
1596 (and auto-fill-function
1597 (> (save-excursion (end-of-line) (current-column))
1602 (when fortran-blink-matching-if
1603 (fortran-blink-matching-if)
1604 (fortran-blink-matching-do))))
1606 (defun fortran-auto-fill ()
1607 "Function to use for `normal-auto-fill-function' in Fortran mode."
1608 (if (> (current-column) (current-fill-column))
1609 (let ((cfi (fortran-calculate-indent)))
1612 (if (or (not (= cfi
(fortran-current-line-indentation)))
1613 (and (re-search-forward "^[ \t]*[0-9]+"
1615 (not (fortran-line-number-indented-correctly-p))))
1616 (fortran-indent-to-column cfi
)
1618 (if (fortran-find-comment-start-skip)
1619 (fortran-indent-comment))))
1621 ;; Never leave point in left margin.
1622 (if (< (current-column) cfi
)
1623 (move-to-column cfi
)))))
1625 ;; Historically this was a separate function which advertised itself
1626 ;; as reindenting but only did so where `most likely to be necessary'.
1627 (defalias 'fortran-indent-new-line
'reindent-then-newline-and-indent
)
1629 (defun fortran-indent-subprogram ()
1630 "Properly indent the Fortran subprogram containing point."
1634 (message "Indenting subprogram...")
1635 (indent-region (point) (mark) nil
))
1636 (message "Indenting subprogram...done."))
1638 (defun fortran-calculate-indent ()
1639 "Calculates the Fortran indent column based on previous lines."
1640 (let (icol first-statement
(case-fold-search t
)
1641 (fortran-minimum-statement-indent
1642 (if indent-tabs-mode
1643 fortran-minimum-statement-indent-tab
1644 fortran-minimum-statement-indent-fixed
)))
1646 (setq first-statement
(fortran-previous-statement))
1648 (setq icol fortran-minimum-statement-indent
)
1649 (if (= (point) (point-min))
1650 (setq icol fortran-minimum-statement-indent
)
1651 (setq icol
(fortran-current-line-indentation)))
1652 (skip-chars-forward " \t0-9")
1653 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1654 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1655 (let (then-test) ; multi-line if-then
1656 (while (and (zerop (forward-line 1))
1657 ;; Search forward for then.
1658 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1659 (not (setq then-test
1662 *[^ \t_$(=a-z0-9]")))))
1664 (setq icol
(+ icol fortran-if-indent
))))
1665 ((looking-at "else\\(if\\)?\\b")
1666 (setq icol
(+ icol fortran-if-indent
)))
1667 ((looking-at "select[ \t]*case[ \t](.*)")
1668 (setq icol
(+ icol fortran-if-indent
)))
1669 ((looking-at "case[ \t]*(.*)")
1670 (setq icol
(+ icol fortran-if-indent
)))
1671 ((looking-at "case[ \t]*default\\b")
1672 (setq icol
(+ icol fortran-if-indent
)))
1673 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1674 (setq icol
(+ icol fortran-if-indent
)))
1675 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1676 (setq icol
(+ icol fortran-if-indent
)))
1677 ((looking-at "do\\b")
1678 (setq icol
(+ icol fortran-do-indent
)))
1680 "\\(structure\\|union\\|map\\|interface\\)\
1681 \\b[ \t]*[^ \t=(a-z]")
1682 (setq icol
(+ icol fortran-structure-indent
)))
1683 ((and (looking-at fortran-end-prog-re1
)
1684 (fortran-check-end-prog-re))
1685 ;; Previous END resets indent to minimum.
1686 (setq icol fortran-minimum-statement-indent
))
1687 ;; Previous statement was a numbered DO loop without a
1688 ;; closing CONTINUE or END DO, so we indented the
1689 ;; terminator like the loop body.
1690 ((and fortran-check-all-num-for-matching-do
1691 (not (looking-at "\\(continue\\|end[ \t]*do\\)\\>"))
1694 (and (looking-at "[ \t]*[0-9]+")
1695 (fortran-check-for-matching-do))))
1696 (setq icol
(- icol fortran-do-indent
))))))
1699 (cond ((looking-at "[ \t]*$"))
1700 ;; Check for directive before comment, so as not to indent.
1701 ((looking-at fortran-directive-re
)
1702 (setq fortran-minimum-statement-indent
0 icol
0))
1703 ((looking-at fortran-comment-line-start-skip
)
1704 (cond ((eq fortran-comment-indent-style
'relative
)
1705 (setq icol
(+ icol fortran-comment-line-extra-indent
)))
1706 ((eq fortran-comment-indent-style
'fixed
)
1707 (setq icol
(+ fortran-minimum-statement-indent
1708 fortran-comment-line-extra-indent
))))
1709 (setq fortran-minimum-statement-indent
0))
1710 ((or (looking-at (concat "[ \t]*"
1712 fortran-continuation-string
)))
1713 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1714 (skip-chars-forward " \t")
1715 ;; Do not introduce extra whitespace into a broken string.
1717 (if (fortran-is-in-string-p (point))
1719 (+ icol fortran-continuation-indent
))))
1721 ;; The terminating statement is actually part of the
1722 ;; loop body, so unless it is a CONTINUE or END DO, we
1723 ;; indent it like the loop body (see above).
1724 ((and fortran-check-all-num-for-matching-do
1725 (looking-at "[ \t]*[0-9]+[ \t]*\
1726 \\(continue\\|end[ \t]*do\\)\\>")
1727 (fortran-check-for-matching-do))
1728 (setq icol
(- icol fortran-do-indent
)))
1730 (skip-chars-forward " \t0-9")
1731 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1732 (setq icol
(- icol fortran-if-indent
)))
1733 ((looking-at "else\\(if\\)?\\b")
1734 (setq icol
(- icol fortran-if-indent
)))
1735 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1736 (setq icol
(- icol fortran-if-indent
)))
1737 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1738 (setq icol
(- icol fortran-if-indent
)))
1739 ((and (looking-at "continue\\b")
1740 (fortran-check-for-matching-do))
1741 (setq icol
(- icol fortran-do-indent
)))
1742 ((looking-at "end[ \t]*do\\b")
1743 (setq icol
(- icol fortran-do-indent
)))
1744 ((looking-at "end[ \t]*\
1745 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1746 (setq icol
(- icol fortran-structure-indent
)))
1747 ((and (looking-at fortran-end-prog-re1
)
1748 (fortran-check-end-prog-re)
1749 (not (= icol fortran-minimum-statement-indent
)))
1750 (message "Warning: `end' not in column %d. Probably\
1751 an unclosed block." fortran-minimum-statement-indent
))))))
1752 (max fortran-minimum-statement-indent icol
)))
1755 (defun fortran-current-line-indentation ()
1756 "Indentation of current line, ignoring Fortran line number or continuation.
1757 This is the column position of the first non-whitespace character
1758 aside from the line number and/or column 5/8 line-continuation character.
1759 For comment lines, returns indentation of the first
1760 non-indentation text within the comment."
1763 (cond ((looking-at fortran-comment-line-start-skip
)
1764 (goto-char (match-end 0))
1766 (if (stringp fortran-comment-indent-char
)
1767 fortran-comment-indent-char
1768 (char-to-string fortran-comment-indent-char
))))
1769 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1770 (goto-char (match-end 0)))
1772 ;; Move past line number.
1773 (skip-chars-forward "[ \t0-9]")))
1774 ;; Move past whitespace.
1775 (skip-chars-forward " \t")
1778 (defun fortran-indent-to-column (col)
1779 "Indent current line to column COL.
1780 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1781 line, and this continuation character is retained on indentation;
1782 2) If `fortran-continuation-string' is the first non-whitespace
1783 character, this is a continuation line;
1784 3) A non-continuation line which has a number as the first
1785 non-whitespace character is a numbered line.
1786 4) A TAB followed by a digit indicates a continuation line."
1789 (if (looking-at fortran-comment-line-start-skip
)
1790 (if fortran-comment-indent-style
1791 (let* ((char (if (stringp fortran-comment-indent-char
)
1792 (aref fortran-comment-indent-char
0)
1793 fortran-comment-indent-char
))
1794 (chars (string ?\s ?
\t char
)))
1795 (goto-char (match-end 0))
1796 (skip-chars-backward chars
)
1797 (delete-region (point) (progn (skip-chars-forward chars
)
1799 (insert-char char
(- col
(current-column)))))
1800 (if (looking-at "\t[1-9]")
1801 (if indent-tabs-mode
1802 (goto-char (match-end 0))
1805 (insert fortran-continuation-string
))
1806 (if (looking-at " \\{5\\}[^ 0\n]")
1807 (if indent-tabs-mode
1808 (progn (delete-char 6)
1809 (insert ?
\t (fortran-numerical-continuation-char) 1))
1811 (delete-horizontal-space)
1812 ;; Put line number in columns 0-4, or
1813 ;; continuation character in column 5.
1815 ((looking-at (regexp-quote fortran-continuation-string
))
1816 (if indent-tabs-mode
1819 (if indent-tabs-mode
1820 fortran-minimum-statement-indent-tab
1821 fortran-minimum-statement-indent-fixed
))
1823 (insert-char (fortran-numerical-continuation-char) 1))
1826 ((looking-at "[0-9]+")
1827 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1828 (if (< extra-space
0)
1829 (message "Warning: line number exceeds 5-digit limit.")
1830 (indent-to (min fortran-line-number-indent extra-space
))))
1831 (skip-chars-forward "0-9")))))
1832 ;; Point is now after any continuation character or line number.
1833 ;; Put body of statement where specified.
1834 (delete-horizontal-space)
1836 ;; Indent any comment following code on the same line.
1837 (when (fortran-find-comment-start-skip)
1838 (goto-char (match-beginning 0))
1839 (unless (= (current-column) (fortran-comment-indent))
1840 (delete-horizontal-space)
1841 (indent-to (fortran-comment-indent)))))))
1843 (defun fortran-line-number-indented-correctly-p ()
1844 "Return t if current line's line number is correctly indented.
1845 Do not call if there is no line number."
1848 (skip-chars-forward " \t")
1849 (and (<= (current-column) fortran-line-number-indent
)
1850 (or (= (current-column) fortran-line-number-indent
)
1851 (progn (skip-chars-forward "0-9")
1852 (= (current-column) 5))))))
1854 (defun fortran-check-for-matching-do ()
1855 "When called from a numbered statement, return t if matching DO is found.
1856 Otherwise return nil."
1857 (let ((case-fold-search t
)
1861 (when (looking-at "[ \t]*[0-9]+")
1862 (skip-chars-forward " \t")
1863 (skip-chars-forward "0") ; skip past leading zeros
1865 (buffer-substring (point) (progn
1866 (skip-chars-forward "0-9")
1872 (and (re-search-backward
1874 "\\(^[ \t0-9]*do[ \t]*0*"
1875 charnum
"\\b\\)\\|" "\\(^[ \t]*0*"
1879 (concat "^[ \t0-9]*do[ \t]*0*"
1882 (defun fortran-find-comment-start-skip (&optional all
)
1883 "Move to past `comment-start-skip' found on current line.
1884 Return non-nil if `comment-start-skip' found, nil if not.
1885 If ALL is nil, only match comments that start in column > 0."
1886 ;; Hopefully at some point we can just use the line below! -stef
1887 ;; (comment-search-forward (line-end-position) t))
1888 (when (or all comment-start-skip
)
1890 (css (if comment-start-skip
1891 (concat fortran-comment-line-start-skip
1892 "\\|" comment-start-skip
)
1893 fortran-comment-line-start-skip
)))
1894 (when (re-search-forward css
(line-end-position) t
)
1895 (if (and (or all
(> (match-beginning 0) (line-beginning-position)))
1896 (or (save-match-data
1897 (not (fortran-is-in-string-p (match-beginning 0))))
1898 ;; Recurse for rest of line.
1899 (fortran-find-comment-start-skip all
)))
1904 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1905 ;; Test if TAB format continuation lines work.
1906 (defun fortran-is-in-string-p (where)
1907 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
1911 ((bolp) nil
) ; bol is never inside a string
1912 ((save-excursion ; comment lines too
1914 (looking-at fortran-comment-line-start-skip
)) nil
)
1915 (t (let ((parse-state '(0 nil nil nil nil nil
0))
1916 (quoted-comment-start (if comment-start
1917 (regexp-quote comment-start
)))
1919 parse-limit end-of-line
)
1920 ;; Move to start of current statement.
1921 (fortran-next-statement)
1922 (fortran-previous-statement)
1923 ;; Now parse up to WHERE.
1925 (if (or ;; Skip to next line if:
1927 (looking-at fortran-comment-line-start-skip
)
1928 ;; - at end of line?
1930 ;; - not in a string and after comment-start?
1931 (and (not (nth 3 parse-state
))
1933 (equal comment-start
1934 (char-to-string (preceding-char)))))
1935 (if (> (forward-line) 0)
1936 (setq not-done nil
))
1938 ;; If we are at beginning of code line, skip any
1939 ;; whitespace, labels and tab continuation markers.
1940 (if (bolp) (skip-chars-forward " \t0-9"))
1941 ;; If we are in column <= 5 now, check for continuation char.
1942 (cond ((= 5 (current-column)) (forward-char 1))
1943 ((and (< (current-column) 5)
1944 (equal fortran-continuation-string
1945 (char-to-string (following-char)))
1947 ;; Find out parse-limit from here.
1948 (setq end-of-line
(line-end-position))
1949 (setq parse-limit
(min where end-of-line
))
1950 ;; Parse max up to comment-start, if non-nil and in current line.
1953 (if (re-search-forward quoted-comment-start end-of-line t
)
1954 (setq parse-limit
(min (point) parse-limit
)))))
1955 ;; Now parse if still in limits.
1956 (if (< (point) where
)
1957 (setq parse-state
(parse-partial-sexp
1958 (point) parse-limit nil nil parse-state
))
1959 (setq not-done nil
))))
1961 (nth 3 parse-state
))))))
1963 ;; From old version.
1964 (defalias 'fortran-auto-fill-mode
'auto-fill-mode
)
1966 (defun fortran-fill ()
1967 "Fill the current line at an appropriate point(s)."
1968 (let* ((auto-fill-function #'fortran-auto-fill
)
1970 (bol (line-beginning-position))
1971 (eol (line-end-position))
1972 (bos (min eol
(+ bol
(fortran-current-line-indentation))))
1973 ;; If in a string at fill-column, break it either before the
1974 ;; initial quote, or at fill-col (if string is too long).
1978 ;; OK to break quotes on comment lines.
1979 (unless (looking-at fortran-comment-line-start-skip
)
1980 (let (fcpoint start
)
1981 (move-to-column fill-column
)
1982 (when (fortran-is-in-string-p (setq fcpoint
(point)))
1984 (re-search-backward "\\S\"\\s\"\\S\"?" bol t
)
1986 (if fortran-break-before-delimiters
1989 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t
)
1991 ;; If the current string is longer than (fill-column
1992 ;; - 6) chars, break it at the fill column (else
1994 (if (> (- (point) start
)
1995 (- fill-column
6 fortran-continuation-indent
))
1998 ;; Decide where to split the line. If a position for a quoted
1999 ;; string was found above then use that, else break the line
2000 ;; before/after the last delimiter.
2004 ;; If f-b-b-d is t, have an extra column to play with,
2005 ;; since delimiter gets shifted to new line.
2006 (move-to-column (if fortran-break-before-delimiters
2012 ;; Adapted from f90-find-breakpoint.
2013 (re-search-backward fortran-break-delimiters-re bol
)
2014 (if (not fortran-break-before-delimiters
)
2015 (if (looking-at fortran-no-break-re
)
2016 ;; Deal with cases such as "**" split over
2017 ;; fill-col. Simpler alternative would be
2018 ;; to start from (1- fill-column) above.
2019 (if (> (+ 2 (current-column)) fill-column
)
2024 (or (looking-at fortran-no-break-re
)
2026 ;; Line indented beyond fill-column?
2027 (when (<= (point) bos
)
2028 (move-to-column (1+ fill-column
))
2029 ;; What is this doing???
2030 (or (re-search-forward "[\t\n,'+-/*)=]" eol t
)
2033 (re-search-forward "[ \t]" opoint t
))
2035 ;; If we are in an in-line comment, don't break unless the
2036 ;; line of code is longer than it should be. Otherwise
2037 ;; break the line at the column computed above.
2039 ;; Need to use fortran-find-comment-start-skip to make sure that
2040 ;; quoted !'s don't prevent a break.
2041 (when (and (save-excursion
2043 (if (not (fortran-find-comment-start-skip))
2045 (goto-char (match-beginning 0))
2046 (>= (point) fill-point
)))
2048 (goto-char fill-point
)
2053 (min (1+ fill-column
)
2054 (+ (fortran-calculate-indent)
2055 fortran-continuation-indent
))))
2056 (goto-char fill-point
)
2057 (fortran-break-line)
2060 (defun fortran-break-line ()
2061 "Call `fortran-split-line'. Joins continuation lines first, then refills."
2062 (let ((bol (line-beginning-position))
2065 (if (fortran-find-comment-start-skip)
2066 (delete-and-extract-region
2067 (match-beginning 0) (line-end-position))))))
2068 ;; Forward line 1 really needs to go to next non white line.
2069 (if (save-excursion (forward-line)
2070 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
2073 (delete-region (point) (match-end 0))
2074 (delete-horizontal-space)
2076 (fortran-split-line))
2081 (delete-horizontal-space)
2082 (indent-to (fortran-comment-indent))
2083 (insert comment-string
)))))
2085 (defun fortran-analyze-file-format ()
2086 "Return nil if fixed format is used, t if TAB formatting is used.
2087 Use `fortran-tab-mode-default' if no non-comment statements are found
2088 before the end or in the first `fortran-analyze-depth' lines."
2091 (goto-char (point-min))
2094 (eq (char-after) ?
\t)
2095 (looking-at " \\{6\\}")
2096 (> i fortran-analyze-depth
)))
2100 ((eq (char-after) ?
\t) t
)
2101 ((looking-at " \\{6\\}") nil
)
2102 (t fortran-tab-mode-default
)))))
2104 (defun fortran-fill-paragraph (&optional justify
)
2105 "Fill surrounding comment block as paragraphs, else fill statement.
2106 Intended as the value of `fill-paragraph-function'.
2107 A comment block is filled by calling `fill-comment-paragraph' with
2108 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
2109 Always returns non-nil (to prevent `fill-paragraph' being called)."
2111 (or (fill-comment-paragraph justify
)
2112 (fortran-fill-statement)
2115 (defun fortran-fill-statement ()
2116 "Fill a Fortran statement up to `fill-column'."
2118 (let ((auto-fill-function #'fortran-auto-fill
))
2119 (unless (save-excursion
2121 (or (looking-at "[ \t]*$")
2122 (looking-at fortran-comment-line-start-skip
)
2123 (and comment-start-skip
2124 (looking-at (concat "[ \t]*" comment-start-skip
)))))
2126 ;; Find beginning of statement.
2127 (fortran-next-statement)
2128 (fortran-previous-statement)
2129 ;; Re-indent initially.
2130 (fortran-indent-line)
2131 ;; Replace newline plus continuation field plus indentation with
2135 (fortran-remove-continuation)))
2136 (fortran-previous-statement)))
2137 (fortran-indent-line)))
2139 (defun fortran-strip-sequence-nos (&optional do-space
)
2140 "Delete all text in column `fortran-line-length' (default 72) and up.
2141 This is assumed to be sequence numbers. Normally also deletes
2142 trailing whitespace after stripping such text. Supplying prefix
2143 arg DO-SPACE prevents stripping the whitespace."
2146 (goto-char (point-min))
2147 (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length
)
2149 (replace-match "" nil nil nil
1)
2150 (unless do-space
(delete-horizontal-space)))))
2152 ;; This code used to live in add-log.el, but this is a better place for it.
2153 (defun fortran-current-defun ()
2154 "Function to use for `add-log-current-defun-function' in Fortran mode."
2156 ;; We must be inside function body for this to work.
2157 (fortran-beginning-of-subprogram)
2158 (let ((case-fold-search t
))
2159 ;; Search for fortran subprogram start.
2160 (if (re-search-forward
2161 fortran-start-prog-re
2162 (save-excursion (fortran-end-of-subprogram)
2165 (or (match-string-no-properties 2)
2167 ;; Move to EOL or before first left paren.
2168 (if (re-search-forward "[(\n]" nil t
)
2169 (progn (backward-char)
2170 (skip-chars-backward " \t"))
2172 ;; Use the name preceding that.
2173 (buffer-substring-no-properties (point) (progn (backward-sexp)
2179 ;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
2180 ;;; fortran.el ends here