1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;; Copyright (c) 1986, 93, 94, 95, 97, 98, 99, 2000, 01, 2003
4 ;; Free Software Foundation, Inc.
6 ;; Author: Michael D. Prange <prange@erl.mit.edu>
7 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
8 ;; Keywords: fortran, languages
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This mode is documented in the Emacs manual.
31 ;; Note that it is for editing Fortran77 or Fortran90 fixed source
32 ;; form. For editing Fortran 90 free format source, use `f90-mode'
33 ;; (f90.el). It is meant to support the GNU Fortran language
34 ;; implemented by g77 (its extensions to Fortran77 and
35 ;; interpretations, e.g. of blackslash in strings).
39 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
41 ;; We acknowledge many contributions and valuable suggestions by
42 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
43 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
44 ;; Gary Sabot and Richard Stallman.
50 ;; * Tidy it all up (more)!
51 ;; * Implement insertion and removal of statement continuations in
52 ;; mixed f77/f90 style, with the first `&' past column 72 and the
53 ;; second in column 6.
54 ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
56 (eval-when-compile ; silence compiler
57 (defvar dabbrev-case-fold-search
)
58 (defvar imenu-case-fold-search
)
59 (defvar imenu-syntax-alist
))
63 "Major mode for editing fixed format Fortran code."
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."
79 (defcustom fortran-tab-mode-default nil
80 "*Default tabbing/carriage control style for empty files in Fortran mode.
81 A non-nil value specifies tab-digit style of continuation control.
82 A value of nil specifies that continuation lines are marked
83 with a character in column 6."
85 :group
'fortran-indent
)
87 (defcustom fortran-tab-mode-string
"/t"
88 "*String to appear in mode line in TAB format buffers."
90 :group
'fortran-indent
)
92 (defcustom fortran-do-indent
3
93 "*Extra indentation applied to DO blocks."
95 :group
'fortran-indent
)
97 (defcustom fortran-if-indent
3
98 "*Extra indentation applied to IF blocks."
100 :group
'fortran-indent
)
102 (defcustom fortran-structure-indent
3
103 "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
105 :group
'fortran-indent
)
107 (defcustom fortran-continuation-indent
5
108 "*Extra indentation applied to continuation lines."
110 :group
'fortran-indent
)
112 (defcustom fortran-comment-indent-style
'fixed
113 "*How to indent comments.
114 nil forces comment lines not to be touched;
115 `fixed' indents to `fortran-comment-line-extra-indent' columns beyond
116 `fortran-minimum-statement-indent-fixed' (if `indent-tabs-mode' nil), or
117 `fortran-minimum-statement-indent-tab' (if `indent-tabs-mode' non-nil);
118 `relative' indents to current Fortran indentation plus
119 `fortran-comment-line-extra-indent'."
120 :type
'(radio (const :tag
"Untouched" nil
) (const fixed
) (const relative
))
121 :group
'fortran-indent
)
123 (defcustom fortran-comment-line-extra-indent
0
124 "*Amount of extra indentation for text within full-line comments."
126 :group
'fortran-indent
127 :group
'fortran-comment
)
129 (defcustom fortran-comment-line-start
"C"
130 "*Delimiter inserted to start new full-line comment.
131 You might want to change this to \"*\", for instance."
134 :group
'fortran-comment
)
136 ;; This used to match preprocessor lines too, but that messes up
137 ;; filling and doesn't seem to be necessary.
138 (defcustom fortran-comment-line-start-skip
139 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
140 "*Regexp to match the start of a full-line comment."
143 :group
'fortran-comment
)
145 (defcustom fortran-directive-re
147 "*Regexp to match a directive line.
148 The matching text will be fontified with `font-lock-keyword-face'.
149 The matching line will be given zero indentation."
152 :group
'fortran-indent
)
154 (defcustom fortran-minimum-statement-indent-fixed
6
155 "*Minimum statement indentation for fixed format continuation style."
157 :group
'fortran-indent
)
159 (defcustom fortran-minimum-statement-indent-tab
(max tab-width
6)
160 "*Minimum statement indentation for TAB format continuation style."
162 :group
'fortran-indent
)
164 ;; Note that this is documented in the v18 manuals as being a string
165 ;; of length one rather than a single character.
166 ;; The code in this file accepts either format for compatibility.
167 (defcustom fortran-comment-indent-char
" "
168 "*Single-character string inserted for Fortran comment indentation.
171 :group
'fortran-comment
)
173 (defcustom fortran-line-number-indent
1
174 "*Maximum indentation for Fortran line numbers.
175 5 means right-justify them within their five-column field."
177 :group
'fortran-indent
)
179 (defcustom fortran-check-all-num-for-matching-do nil
180 "*Non-nil causes all numbered lines to be treated as possible DO loop ends."
184 (defcustom fortran-blink-matching-if nil
185 "*Non-nil causes \\[fortran-indent-line] on ENDIF to blink on matching IF.
186 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
190 (defcustom fortran-continuation-string
"$"
191 "*Single-character string used for Fortran continuation lines.
192 In fixed format continuation style, this character is inserted in
193 column 6 by \\[fortran-split-line] to begin a continuation line.
194 Also, if \\[fortran-indent-line] finds this at the beginning of a
195 line, it will convert the line into a continuation line of the
196 appropriate style. Normally $."
200 (defcustom fortran-comment-region
"c$$$"
201 "*String inserted by \\[fortran-comment-region] at start of each \
204 :group
'fortran-comment
)
206 (defcustom fortran-electric-line-number t
207 "*Non-nil causes line numbers to be moved to the correct column as typed."
211 (defcustom fortran-column-ruler-fixed
212 "0 4 6 10 20 30 40 5\
214 \[ ]|{ | | | | | | | | \
216 "String displayed above current line by \\[fortran-column-ruler].
217 This variable is used in fixed format mode.
218 See the variable `fortran-column-ruler-tab' for TAB format mode."
222 (defcustom fortran-column-ruler-tab
225 \[ ]| { | | | | | | | | \
227 "String displayed above current line by \\[fortran-column-ruler].
228 This variable is used in TAB format mode.
229 See the variable `fortran-column-ruler-fixed' for fixed format mode."
233 (defcustom fortran-analyze-depth
100
234 "Number of lines to scan to identify fixed or TAB format style."
238 (defcustom fortran-break-before-delimiters t
239 "*Non-nil causes filling to break lines before delimiters.
240 Delimiters are whitespace, commas, quotes, and operators."
244 (defcustom fortran-mode-hook nil
245 "Hook run when entering Fortran mode."
250 (defvar fortran-if-start-re
"\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*("
251 "Regexp matching the start of an IF statement.")
253 (defvar fortran-end-prog-re1
255 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
256 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?"
257 "Regexp possibly matching the end of a subprogram.")
259 (defvar fortran-end-prog-re
260 (concat "^[ \t0-9]*" fortran-end-prog-re1
)
261 "Regexp possibly matching the end of a subprogram, from the line start.
262 See also `fortran-end-prog-re1'.")
264 (defconst fortran-type-types
266 (mapconcat 'identity
; " " -> "[ \t]*"
270 '("character" "byte" "integer" "logical"
271 "none" "real" "complex"
272 "double precision" "double complex"))
273 (structured-types '("structure" "union" "map"))
274 (other-types '("record" "dimension"
275 "parameter" "common" "save"
276 "external" "intrinsic" "data"
279 (mapcar (lambda (x) (concat "implicit " x
))
282 (mapcar (lambda (x) (concat "end " x
))
285 other-types
)) 'paren
))
287 "Regexp matching Fortran types.")
289 (defvar fortran-font-lock-keywords-1
290 ;; Program, subroutine and function declarations, plus calls.
291 '(("\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|\
292 program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?"
293 (1 font-lock-keyword-face
)
294 (2 font-lock-function-name-face nil t
)))
295 "Subdued level highlighting for Fortran mode.")
297 (defvar fortran-font-lock-keywords-2
298 (append fortran-font-lock-keywords-1
300 ;; Fontify all type specifiers (must be first - see below).
301 (cons fortran-type-types
'font-lock-type-face
)
302 ;; Builtin keywords (except logical, do and goto - see below).
303 (concat "\\<" (regexp-opt
304 '("continue" "format" "end" "enddo"
305 "if" "then" "else" "endif" "elseif"
306 "while" "inquire" "stop" "return"
307 "include" "open" "close" "read"
308 "write" "format" "print" "select" "case"
309 "cycle" "exit" "rewind" "backspace")
311 ;; Builtin operators.
312 (concat "\\." (regexp-opt
313 '("and" "or" "not" "lt" "le" "eq" "ge"
314 "gt" "ne" "true" "false")
316 ;; do/goto keywords and targets, and goto tags.
317 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
318 (1 font-lock-keyword-face
)
319 (2 font-lock-constant-face nil t
))
320 '("^ *\\([0-9]+\\)" . font-lock-constant-face
)))
321 "Medium level highlighting for Fortran mode.")
323 (defvar fortran-font-lock-keywords-3
325 fortran-font-lock-keywords-1
326 ;; All type specifiers plus their declared items.
328 (list (concat fortran-type-types
"[ \t(/]*\\(*\\)?")
330 '(1 font-lock-type-face
)
331 ;; Declaration item (or just /.../ block name).
332 `(font-lock-match-c-style-declaration-item-and-skip-to-next
333 ;; Start after any *(...) expression.
335 (and (match-beginning ,(1+ (regexp-opt-depth
336 fortran-type-types
)))
340 ;; No need to clean up.
342 ;; Fontify as a variable name, functions fontified elsewhere.
343 (1 font-lock-variable-name-face nil t
))))
344 ;; Things extra to `fortran-font-lock-keywords-3' (must be done first).
346 ;; Goto-like `err=label'/`end=label' in read/write statements.
347 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
348 (1 font-lock-keyword-face
) (4 font-lock-constant-face nil t
))
349 ;; Standard continuation character and in a TAB-formatted line.
350 '("^ \\{5\\}\\([^ 0\n]\\)" 1 font-lock-string-face
)
351 '("^\t\\([1-9]\\)" 1 font-lock-string-face
))
352 `((,fortran-directive-re
(0 font-lock-keyword-face t
)))
353 ;; `fortran-font-lock-keywords-2' without types (see above).
354 (cdr (nthcdr (length fortran-font-lock-keywords-1
)
355 fortran-font-lock-keywords-2
)))
356 "Gaudy level highlighting for Fortran mode.")
358 ;; Comments are real pain in Fortran because there is no way to
359 ;; represent the standard comment syntax in an Emacs syntax table.
360 ;; (We can do so for F90-style). Therefore an unmatched quote in a
361 ;; standard comment will throw fontification off on the wrong track.
362 ;; So we do syntactic fontification with regexps.
363 (defvar fortran-font-lock-syntactic-keywords
364 '(("^[cd\\*]" 0 (11))
365 ("^[^cd\\*\t\n].\\{71\\}\\([^\n]+\\)" 1 (11)))
366 "`font-lock-syntactic-keywords' for Fortran.
367 These get fixed-format comments fontified.")
369 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
370 "Default expressions to highlight in Fortran mode.")
372 (defvar fortran-imenu-generic-expression
373 ;; These patterns could be confused by sequence nos. in cols 72+ and
374 ;; don't allow continuations everywhere.
378 ;; [This will be fooled by `end function' allowed by G77. Also,
379 ;; it assumes sensible whitespace is employed.]
381 ;; leading whitespace:
383 ;; function declaration with optional type, e.g. `real',
384 ;; `real*4', character(*), `double precision':
385 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
386 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
387 ;; Possible statement continuation:
388 "[ \t" fortran-continuation-string
"]+"
389 ;; Variable to index:
392 ;; Un-named block data.
393 '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
394 "Value for `imenu-generic-expression' in Fortran mode.")
397 (defvar fortran-mode-syntax-table
398 (let ((table (make-syntax-table)))
399 ;; We might like `;' to be punctuation (g77 multi-statement
400 ;; lines), but that screws abbrevs.
401 (modify-syntax-entry ?\
; "w" table)
402 (modify-syntax-entry ?
\r " " table
)
403 (modify-syntax-entry ?
+ "." table
)
404 (modify-syntax-entry ?-
"." table
)
405 (modify-syntax-entry ?
= "." table
)
406 (modify-syntax-entry ?
* "." table
)
407 (modify-syntax-entry ?
/ "." table
)
408 (modify-syntax-entry ?
\' "\"" table
)
409 (modify-syntax-entry ?
\" "\"" table
)
410 ;; Consistent with GNU Fortran -- see the manual.
411 (modify-syntax-entry ?
\\ "\\" table
)
412 ;; This might be better as punctuation, as for C, but this way you
413 ;; can treat floating-point numbers as symbols.
414 (modify-syntax-entry ?.
"_" table
) ; e.g. `a.ne.b'
415 (modify-syntax-entry ?_
"_" table
)
416 (modify-syntax-entry ?$
"_" table
) ; esp. VMSisms
417 (modify-syntax-entry ?\
! "<" table
)
418 (modify-syntax-entry ?
\n ">" table
)
420 "Syntax table used in Fortran mode.")
422 (defvar fortran-gud-syntax-table
423 (let ((st (make-syntax-table fortran-mode-syntax-table
)))
424 (modify-syntax-entry ?
\n "." st
)
426 "Syntax table used to parse Fortran expressions for printing in GUD.")
428 (defvar fortran-mode-map
429 (let ((map (make-sparse-keymap)))
430 (define-key map
";" 'fortran-abbrev-start
)
431 (define-key map
"\C-c;" 'fortran-comment-region
)
432 (define-key map
"\M-;" 'fortran-indent-comment
)
433 (define-key map
"\M-\n" 'fortran-split-line
)
434 (define-key map
"\M-\C-q" 'fortran-indent-subprogram
)
435 (define-key map
"\C-c\C-w" 'fortran-window-create-momentarily
)
436 (define-key map
"\C-c\C-r" 'fortran-column-ruler
)
437 (define-key map
"\C-c\C-p" 'fortran-previous-statement
)
438 (define-key map
"\C-c\C-n" 'fortran-next-statement
)
439 (define-key map
"\C-c\C-d" 'fortran-join-line
) ; like f90
440 (define-key map
"\M-^" 'fortran-join-line
) ; subvert delete-indentation
441 (define-key map
"0" 'fortran-electric-line-number
)
442 (define-key map
"1" 'fortran-electric-line-number
)
443 (define-key map
"2" 'fortran-electric-line-number
)
444 (define-key map
"3" 'fortran-electric-line-number
)
445 (define-key map
"4" 'fortran-electric-line-number
)
446 (define-key map
"5" 'fortran-electric-line-number
)
447 (define-key map
"6" 'fortran-electric-line-number
)
448 (define-key map
"7" 'fortran-electric-line-number
)
449 (define-key map
"8" 'fortran-electric-line-number
)
450 (define-key map
"9" 'fortran-electric-line-number
)
452 (easy-menu-define fortran-menu map
"Menu for Fortran mode."
454 ["Manual" (info "(emacs)Fortran")]
456 ,(custom-menu-create 'fortran
)
458 ["Save" Custom-save t
]
459 ["Reset to Current" Custom-reset-current t
]
460 ["Reset to Saved" Custom-reset-saved t
]
461 ["Reset to Standard Settings" Custom-reset-standard t
]
464 ["Comment Region" fortran-comment-region mark-active
]
466 (fortran-comment-region (region-beginning) (region-end) 1)
468 ["Indent Region" indent-region mark-active
]
469 ["Indent Subprogram" fortran-indent-subprogram t
]
471 ["Beginning of Subprogram" fortran-beginning-of-subprogram t
]
472 ["End of Subprogram" fortran-end-of-subprogram t
]
474 ["Subprogram" mark-defun
t]
475 ["IF Block" fortran-mark-if t
]
476 ["DO Block" fortran-mark-do t
]
478 ["Narrow to Subprogram" narrow-to-defun
t]
481 ["Temporary column ruler" fortran-column-ruler t
]
482 ["72-column window" fortran-window-create t
]
484 (enlarge-window-horizontally (- (frame-width) (window-width)))
485 (< (window-width) (frame-width))]
486 ["Momentary 72-column window" fortran-window-create-momentarily t
]
488 ["Break Line at Point" fortran-split-line t
]
489 ["Join Line" fortran-join-line t
]
490 ["Fill Statement/Comment" fill-paragraph t
]
492 ["Toggle auto-fill" auto-fill-mode
:selected auto-fill-function
494 ["Toggle abbrev-mode" abbrev-mode
:selected abbrev-mode
496 ["Add imenu Menu" imenu-add-menubar-index
497 :active
(not (lookup-key (current-local-map) [menu-bar index
]))
498 :included
(fboundp 'imenu-add-to-menubar
)]))
500 "Keymap used in Fortran mode.")
503 (defvar fortran-mode-abbrev-table
504 (let (abbrevs-changed)
505 (define-abbrev-table 'fortran-mode-abbrev-table nil
)
506 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
507 ;; Only use `apply' to quieten the byte-compiler.
509 (function (lambda (element)
511 (apply 'define-abbrev fortran-mode-abbrev-table
512 (append element
'(nil 0 t
)))
513 (wrong-number-of-arguments
514 (apply 'define-abbrev fortran-mode-abbrev-table
515 (append element
'(nil 0)))))))
516 '((";au" "automatic" )
518 (";bd" "block data" )
527 (";dc" "double complex" )
528 (";dp" "double precision" )
534 (";eq" "equivalence" )
543 (";ib" "implicit byte" )
544 (";ic" "implicit complex" )
545 (";ich" "implicit character")
546 (";ii" "implicit integer" )
547 (";il" "implicit logical" )
548 (";ir" "implicit real" )
551 (";intr" "intrinsic" )
554 (";o" "open" ) ; was ;op
568 (";su" "subroutine" )
574 fortran-mode-abbrev-table
))
579 (defun fortran-mode ()
580 "Major mode for editing Fortran code in fixed format.
581 For free format code, use `f90-mode'.
583 \\[fortran-indent-line] indents the current Fortran line correctly.
584 Note that DO statements must not share a common CONTINUE.
586 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
592 Variables controlling indentation style and extra features:
595 To use comments starting with `!', set this to the string \"!\".
597 Extra indentation within DO blocks (default 3).
599 Extra indentation within IF blocks (default 3).
600 `fortran-structure-indent'
601 Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
603 `fortran-continuation-indent'
604 Extra indentation applied to continuation statements (default 5).
605 `fortran-comment-line-extra-indent'
606 Amount of extra indentation for text in full-line comments (default 0).
607 `fortran-comment-indent-style'
608 How to indent the text in full-line comments. Allowed values are:
609 nil don't change the indentation
610 fixed indent to `fortran-comment-line-extra-indent' beyond the
612 `fortran-minimum-statement-indent-fixed' (fixed format) or
613 `fortran-minimum-statement-indent-tab' (TAB format),
614 depending on the continuation format in use.
615 relative indent to `fortran-comment-line-extra-indent' beyond the
616 indentation for a line of code.
618 `fortran-comment-indent-char'
619 Single-character string to be inserted instead of space for
620 full-line comment indentation (default \" \").
621 `fortran-minimum-statement-indent-fixed'
622 Minimum indentation for statements in fixed format mode (default 6).
623 `fortran-minimum-statement-indent-tab'
624 Minimum indentation for statements in TAB format mode (default 9).
625 `fortran-line-number-indent'
626 Maximum indentation for line numbers (default 1). A line number will
627 get less than this much indentation if necessary to avoid reaching
629 `fortran-check-all-num-for-matching-do'
630 Non-nil causes all numbered lines to be treated as possible \"continue\"
631 statements (default nil).
632 `fortran-blink-matching-if'
633 Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
634 to blink on the matching IF (or DO [WHILE]). (default nil)
635 `fortran-continuation-string'
636 Single-character string to be inserted in column 5 of a continuation
637 line (default \"$\").
638 `fortran-comment-region'
639 String inserted by \\[fortran-comment-region] at start of each line in
640 the region (default \"c$$$\").
641 `fortran-electric-line-number'
642 Non-nil causes line number digits to be moved to the correct column
643 as typed (default t).
644 `fortran-break-before-delimiters'
645 Non-nil causes lines to be broken before delimiters (default t).
647 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
648 with no args, if that value is non-nil."
650 (kill-all-local-variables)
651 (setq major-mode
'fortran-mode
653 local-abbrev-table fortran-mode-abbrev-table
)
654 (set-syntax-table fortran-mode-syntax-table
)
655 (use-local-map fortran-mode-map
)
656 (set (make-local-variable 'indent-line-function
) 'fortran-indent-line
)
657 (set (make-local-variable 'indent-region-function
)
659 (let (fortran-blink-matching-if ; avoid blinking delay
660 indent-region-function
)
661 (indent-region start end nil
))))
662 (set (make-local-variable 'require-final-newline
) t
)
663 ;; The syntax tables don't understand the column-0 comment-markers.
664 (set (make-local-variable 'comment-use-syntax
) nil
)
665 (set (make-local-variable 'comment-padding
) "$$$")
666 (set (make-local-variable 'comment-start
) fortran-comment-line-start
)
667 (set (make-local-variable 'comment-start-skip
)
668 ;; We can't reuse `fortran-comment-line-start-skip' directly because
669 ;; it contains backrefs whereas we need submatch-1 to end at the
670 ;; beginning of the comment delimiter.
671 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
672 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
673 (set (make-local-variable 'comment-indent-function
) 'fortran-comment-indent
)
674 (set (make-local-variable 'abbrev-all-caps
) t
)
675 (set (make-local-variable 'normal-auto-fill-function
) 'fortran-auto-fill
)
676 (set (make-local-variable 'indent-tabs-mode
) (fortran-analyze-file-format))
677 (setq mode-line-process
'(indent-tabs-mode fortran-tab-mode-string
))
678 (set (make-local-variable 'fill-column
) 72)
679 (set (make-local-variable 'fill-paragraph-function
) 'fortran-fill-paragraph
)
680 (set (make-local-variable 'font-lock-defaults
)
681 '((fortran-font-lock-keywords
682 fortran-font-lock-keywords-1
683 fortran-font-lock-keywords-2
684 fortran-font-lock-keywords-3
)
685 nil t
((?
/ .
"$/") ("_$" .
"w"))
686 fortran-beginning-of-subprogram
))
687 (set (make-local-variable 'font-lock-syntactic-keywords
)
688 fortran-font-lock-syntactic-keywords
)
689 (set (make-local-variable 'imenu-case-fold-search
) t
)
690 (set (make-local-variable 'imenu-generic-expression
)
691 fortran-imenu-generic-expression
)
692 (set (make-local-variable 'imenu-syntax-alist
) '(("_$" .
"w")))
693 (set (make-local-variable 'beginning-of-defun-function
)
694 #'fortran-beginning-of-subprogram
)
695 (set (make-local-variable 'end-of-defun-function
)
696 #'fortran-end-of-subprogram
)
697 (set (make-local-variable 'add-log-current-defun-function
)
698 #'fortran-current-defun
)
699 (set (make-local-variable 'dabbrev-case-fold-search
) 'case-fold-search
)
700 (set (make-local-variable 'gud-find-expr-function
) 'fortran-gud-find-expr
)
701 (run-hooks 'fortran-mode-hook
))
704 (defun fortran-gud-find-expr ()
705 ;; Consider \n as punctuation (end of expression).
706 (with-syntax-table fortran-gud-syntax-table
709 (defsubst fortran-comment-indent
()
710 "Return the indentation appropriate for the current comment line.
711 This is 0 for a line matching `fortran-comment-line-start-skip', else
712 the value of `comment-column' (leaving at least one space after code)."
713 (if (looking-at fortran-comment-line-start-skip
) 0
715 (skip-chars-backward " \t")
716 (max (1+ (current-column)) comment-column
))))
718 (defun fortran-indent-comment ()
719 "Align or create comment on current line.
720 Existing comments of all types are recognized and aligned.
721 If the line has no comment, a side-by-side comment is inserted and aligned,
722 if the value of `comment-start' is not nil and allows such comments.
723 Otherwise, a separate-line comment is inserted, on this line
724 or on a new line inserted before this line if this line is not blank."
727 ;; Recognize existing comments of either kind.
728 (cond ((fortran-find-comment-start-skip 'all
)
729 (goto-char (match-beginning 0))
731 (fortran-indent-line)
732 (unless (= (current-column) (fortran-comment-indent))
733 (delete-horizontal-space)
734 (indent-to (fortran-comment-indent)))))
735 ;; No existing comment.
736 ;; If side-by-side comments are defined, insert one,
737 ;; unless line is now blank.
738 ((and comment-start
(not (looking-at "[ \t]*$"))
739 (string-match comment-start-skip
(concat " " comment-start
)))
741 (delete-horizontal-space)
742 (indent-to (fortran-comment-indent))
743 (insert comment-start
))
744 ;; Else insert separate-line comment, making a new line if nec.
746 (if (looking-at "^[ \t]*$")
747 (delete-horizontal-space)
751 (insert fortran-comment-line-start
)
752 (insert-char (if (stringp fortran-comment-indent-char
)
753 (aref fortran-comment-indent-char
0)
754 fortran-comment-indent-char
)
755 (- (fortran-calculate-indent) (current-column))))))
757 (defun fortran-comment-region (beg-region end-region arg
)
758 "Comment every line in the region.
759 Inserts the string variable `fortran-comment-region' at the beginning of
760 every line in the region.
761 BEG-REGION and END-REGION specify the region boundaries.
762 With non-nil ARG, uncomments the region."
763 (interactive "*r\nP")
764 (let ((end-region-mark (copy-marker end-region
))
765 (save-point (point-marker)))
766 (goto-char beg-region
)
769 (let ((com (regexp-quote fortran-comment-region
))) ; uncomment
771 (delete-region (point) (match-end 0)))
772 (while (and (zerop (forward-line 1))
773 (< (point) end-region-mark
))
775 (delete-region (point) (match-end 0)))))
776 (insert fortran-comment-region
) ; comment
777 (while (and (zerop (forward-line 1))
778 (< (point) end-region-mark
))
779 (insert fortran-comment-region
)))
780 (goto-char save-point
)
781 (set-marker end-region-mark nil
)
782 (set-marker save-point nil
)))
785 (defun fortran-abbrev-start ()
786 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
787 Any other key combination is executed normally."
789 (insert last-command-char
)
791 (if (fboundp 'next-command-event
) ; XEmacs
792 (setq event
(next-command-event)
793 char
(event-to-character event
))
794 (setq event
(read-event)
796 ;; Insert char if not equal to `?', or if abbrev-mode is off.
797 (if (and abbrev-mode
(or (eq char ??
) (eq char help-char
)))
798 (fortran-abbrev-help)
799 (setq unread-command-events
(list event
)))))
801 (defun fortran-abbrev-help ()
802 "List the currently defined abbrevs in Fortran mode."
804 (message "Listing abbrev table...")
805 (display-buffer (fortran-prepare-abbrev-list-buffer))
806 (message "Listing abbrev table...done"))
808 (defun fortran-prepare-abbrev-list-buffer ()
809 "Create a buffer listing the Fortran mode abbreviations."
811 (set-buffer (get-buffer-create "*Abbrevs*"))
813 (insert-abbrev-table-description 'fortran-mode-abbrev-table t
)
814 (goto-char (point-min))
815 (set-buffer-modified-p nil
)
817 (get-buffer-create "*Abbrevs*"))
819 (defun fortran-column-ruler ()
820 "Insert a column ruler momentarily above current line, till next keystroke.
821 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
822 format mode, and `fortran-column-ruler-tab' in TAB format mode.
823 The next key typed is executed unless it is SPC."
825 (momentary-string-display
827 fortran-column-ruler-tab
828 fortran-column-ruler-fixed
)
831 (if (eq (window-start (selected-window))
832 (window-point (selected-window)))
833 (line-beginning-position 2)
835 nil
"Type SPC or any command to erase ruler."))
837 (defun fortran-window-create ()
838 "Make the window 72 columns wide.
839 See also `fortran-window-create-momentarily'."
841 (let ((window-min-width 2))
842 (if (< (window-width) (frame-width))
843 (enlarge-window-horizontally (- (frame-width)
845 (let* ((window-edges (window-edges))
846 (scroll-bar-width (- (nth 2 window-edges
)
849 (split-window-horizontally (+ 72 scroll-bar-width
)))
851 (switch-to-buffer " fortran-window-extra" t
)
852 (select-window (previous-window))))
854 (defun fortran-window-create-momentarily (&optional arg
)
855 "Momentarily make the window 72 columns wide.
856 Optional ARG non-nil and non-unity disables the momentary feature.
857 See also `fortran-window-create'."
861 (save-window-excursion
864 (fortran-window-create)
865 (error (error "No room for Fortran window")))
866 (message "Type SPC to continue editing.")
867 (let ((char (read-event)))
868 (or (equal char
(string-to-char " "))
869 (setq unread-command-events
(list char
))))))
870 (fortran-window-create)))
872 (defun fortran-split-line ()
873 "Break line at point and insert continuation marker and alignment."
875 (delete-horizontal-space)
879 (and (fortran-find-comment-start-skip 'all
)
880 (< (match-beginning 0) pos
))))
881 (insert ?
\n (match-string 0))
883 (insert ?
\n ?
\t (fortran-numerical-continuation-char))
884 (insert "\n " fortran-continuation-string
))) ; space after \n important
885 (fortran-indent-line)) ; when cont string is C, c or *
887 (defun fortran-remove-continuation ()
888 "Delete any Fortran continuation characters at point.
889 Returns t if anything actually deleted."
890 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
895 (defun fortran-join-line (arg)
896 "Join current line to the previous one and re-indent.
897 With a prefix argument, repeat this operation that many times.
898 If the prefix argument ARG is negative, join the next -ARG lines.
899 Continuation lines are correctly handled."
905 (while (not (zerop arg
))
907 (or (fortran-remove-continuation)
908 (delete-indentation))
910 (fortran-indent-line)))
912 (defun fortran-numerical-continuation-char ()
913 "Return a digit for tab-digit style of continuation lines.
914 If previous line is a tab-digit continuation line, return that digit
915 plus one, otherwise return 1. Zero not allowed."
918 (if (looking-at "\t[1-9]")
919 (+ ?
1 (%
(- (char-after (1+ (point))) ?
0) 9))
922 (put 'fortran-electric-line-number
'delete-selection t
)
923 (defun fortran-electric-line-number (arg)
924 "Self insert, but if part of a Fortran line number indent it automatically.
925 Auto-indent does not happen if a numeric ARG is used."
927 (if (or arg
(not fortran-electric-line-number
))
929 (self-insert-command (prefix-numeric-value arg
))
930 (self-insert-command 1))
931 (if (or (and (= 5 (current-column))
934 ;; In col 5 with only spaces to the left.
935 (looking-at " \\{5\\}")))
936 (and (= (if indent-tabs-mode
937 fortran-minimum-statement-indent-tab
938 fortran-minimum-statement-indent-fixed
) (current-column))
939 ;; In col 8 with a single tab to the left.
940 (eq ?
\t (char-after (line-beginning-position)))
941 (not (or (eq last-command
'fortran-indent-line
)
943 'fortran-indent-new-line
))))
945 (re-search-backward "[^ \t0-9]"
946 (line-beginning-position)
947 t
)) ; not a line number
948 (looking-at "[0-9]")) ; within a line number
949 (self-insert-command (prefix-numeric-value arg
))
950 (skip-chars-backward " \t")
951 (insert last-command-char
)
952 (fortran-indent-line))))
955 (defun fortran-check-end-prog-re ()
956 "Check a preliminary match against `fortran-end-prog-re'."
957 ;; Having got a possible match for the subprogram end, we need a
958 ;; match of whitespace, avoiding possible column 73+ stuff.
960 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
961 (buffer-substring (match-end 0)
962 (min (line-end-position)
963 (+ 72 (line-beginning-position)))))))
965 ;; Note that you can't just check backwards for `subroutine' &c in
966 ;; case of un-marked main programs not at the start of the file.
967 (defun fortran-beginning-of-subprogram ()
968 "Move point to the beginning of the current Fortran subprogram."
971 (let ((case-fold-search t
))
972 (beginning-of-line -
1)
974 (while (re-search-backward fortran-end-prog-re nil
'move
)
975 (if (fortran-check-end-prog-re)
979 (defun fortran-end-of-subprogram ()
980 "Move point to the end of the current Fortran subprogram."
983 (let ((case-fold-search t
))
984 (if (save-excursion ; on END
986 (and (looking-at fortran-end-prog-re
)
987 (fortran-check-end-prog-re)))
989 (beginning-of-line 2)
991 (while (re-search-forward fortran-end-prog-re nil
'move
)
992 (if (fortran-check-end-prog-re)
994 (goto-char (match-beginning 0))
997 (defun fortran-previous-statement ()
998 "Move point to beginning of the previous Fortran statement.
999 Returns 'first-statement if that statement is the first
1000 non-comment Fortran statement in the file, and nil otherwise.
1001 Directive lines are treated as comments."
1003 (let (not-first-statement continue-test
)
1007 (not (looking-at fortran-comment-line-start-skip
))
1008 (not (looking-at fortran-directive-re
))
1011 (regexp-quote fortran-continuation-string
)))
1012 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1013 (while (and (setq not-first-statement
(zerop (forward-line -
1)))
1014 (or (looking-at fortran-comment-line-start-skip
)
1015 (looking-at fortran-directive-re
)
1018 (regexp-quote fortran-continuation-string
)))
1019 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1020 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1021 (cond ((and continue-test
1022 (not not-first-statement
))
1023 (message "Incomplete continuation statement."))
1025 (fortran-previous-statement))
1026 ((not not-first-statement
)
1027 'first-statement
))))
1029 (defun fortran-next-statement ()
1030 "Move point to beginning of the next Fortran statement.
1031 Returns 'last-statement if that statement is the last
1032 non-comment Fortran statement in the file, and nil otherwise.
1033 Directive lines are treated as comments."
1035 (let (not-last-statement)
1037 (while (and (setq not-last-statement
1038 (and (zerop (forward-line 1))
1040 (or (looking-at fortran-comment-line-start-skip
)
1041 (looking-at fortran-directive-re
)
1042 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1043 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1044 (if (not not-last-statement
)
1048 (defun fortran-blink-match (regex keyword find-begin
)
1049 "From a line matching REGEX, blink matching KEYWORD statement line.
1050 Use function FIND-BEGIN to match it."
1051 (let ((top-of-window (window-start))
1053 (case-fold-search t
)
1056 (when (save-excursion
1058 (skip-chars-forward " \t0-9")
1060 (if (not (setq matching
(funcall find-begin
)))
1061 (setq message
(concat "No matching " keyword
"."))
1062 (if (< matching top-of-window
)
1064 (goto-char matching
)
1068 (buffer-substring (point)
1069 (line-end-position)))))))
1071 (message "%s" message
)
1072 (goto-char matching
)
1074 (goto-char end-point
)))))
1076 (defun fortran-blink-matching-if ()
1077 "From an ENDIF or ELSE statement, blink the matching IF statement."
1078 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1079 "if" #'fortran-beginning-if
))
1081 (defun fortran-blink-matching-do ()
1082 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1083 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do
))
1085 (defun fortran-mark-do ()
1086 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1087 The marks are pushed."
1089 (let (enddo-point do-point
)
1090 (if (setq enddo-point
(fortran-end-do))
1091 (if (not (setq do-point
(fortran-beginning-do)))
1092 (message "No matching do.")
1093 (goto-char enddo-point
)
1095 (goto-char do-point
)))))
1097 (defun fortran-end-do ()
1098 "Search forward for first unmatched ENDDO.
1099 Return point or nil."
1100 (let ((case-fold-search t
))
1101 (if (save-excursion (beginning-of-line)
1102 (skip-chars-forward " \t0-9")
1103 (looking-at "end[ \t]*do\\b"))
1109 (while (and (not (zerop count
))
1110 (not (eq (fortran-next-statement) 'last-statement
))
1111 ;; Keep local to subprogram.
1112 (not (and (looking-at fortran-end-prog-re
)
1113 (fortran-check-end-prog-re))))
1114 (skip-chars-forward " \t0-9")
1115 (cond ((looking-at "end[ \t]*do\\b")
1116 (setq count
(1- count
)))
1118 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1119 (setq count
(1+ count
)))))
1121 ;; All pairs accounted for.
1124 (defun fortran-beginning-do ()
1125 "Search backwards for first unmatched DO [WHILE].
1126 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1127 (let ((case-fold-search t
)
1128 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1131 (skip-chars-forward " \t0-9")
1132 (looking-at dostart-re
))
1138 (while (and (not (zerop count
))
1139 (not (eq (fortran-previous-statement) 'first-statement
))
1140 ;; Keep local to subprogram.
1141 (not (and (looking-at fortran-end-prog-re
)
1142 (fortran-check-end-prog-re))))
1143 (skip-chars-forward " \t0-9")
1144 (cond ((looking-at dostart-re
)
1145 (setq count
(1- count
)))
1146 ;; Note labelled loop ends not considered.
1147 ((looking-at "end[ \t]*do\\b")
1148 (setq count
(1+ count
)))))
1150 ;; All pairs accounted for.
1153 (defun fortran-mark-if ()
1154 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1155 The marks are pushed."
1157 (let (endif-point if-point
)
1158 (if (setq endif-point
(fortran-end-if))
1159 (if (not (setq if-point
(fortran-beginning-if)))
1160 (message "No matching if.")
1161 ;; Set mark, move point.
1162 (goto-char endif-point
)
1164 (goto-char if-point
)))))
1166 (defun fortran-end-if ()
1167 "Search forwards for first unmatched ENDIF.
1168 Return point or nil."
1169 (let ((case-fold-search t
))
1170 (if (save-excursion (beginning-of-line)
1171 (skip-chars-forward " \t0-9")
1172 (looking-at "end[ \t]*if\\b"))
1175 ;; Search for one. The point has been already been moved to first
1176 ;; letter on line but this should not cause troubles.
1179 (while (and (not (zerop count
))
1180 (not (eq (fortran-next-statement) 'last-statement
))
1181 ;; Keep local to subprogram.
1182 (not (and (looking-at fortran-end-prog-re
)
1183 (fortran-check-end-prog-re))))
1184 (skip-chars-forward " \t0-9")
1185 (cond ((looking-at "end[ \t]*if\\b")
1186 (setq count
(1- count
)))
1187 ((looking-at fortran-if-start-re
)
1190 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1191 (let (then-test) ; multi-line if-then
1194 (zerop (forward-line 1))
1195 ;; Search forward for then.
1196 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1200 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1202 (setq count
(1+ count
)))))))
1204 ;; All pairs accounted for.
1207 (defun fortran-beginning-if ()
1208 "Search backwards for first unmatched IF-THEN.
1209 Return point or nil."
1210 (let ((case-fold-search t
))
1212 ;; May be sitting on multi-line if-then statement, first
1213 ;; move to beginning of current statement. Note:
1214 ;; `fortran-previous-statement' moves to previous statement
1215 ;; *unless* current statement is first one. Only move
1216 ;; forward if not first-statement.
1217 (if (not (eq (fortran-previous-statement) 'first-statement
))
1218 (fortran-next-statement))
1219 (skip-chars-forward " \t0-9")
1221 (looking-at fortran-if-start-re
)
1223 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1224 ;; Multi-line if-then.
1227 (and (zerop (forward-line 1))
1228 ;; Search forward for then.
1229 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1233 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1240 (while (and (not (zerop count
))
1241 (not (eq (fortran-previous-statement) 'first-statement
))
1242 ;; Keep local to subprogram.
1243 (not (and (looking-at fortran-end-prog-re
)
1244 (fortran-check-end-prog-re))))
1245 (skip-chars-forward " \t0-9")
1246 (cond ((looking-at fortran-if-start-re
)
1249 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1250 (let (then-test) ; multi-line if-then
1253 (zerop (forward-line 1))
1254 ;; Search forward for then.
1255 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1259 (concat ".*then\\b[ \t]*"
1260 "[^ \t(=a-z0-9]"))))))
1262 (setq count
(1- count
)))))
1263 ((looking-at "end[ \t]*if\\b")
1264 (setq count
(1+ count
)))))
1266 ;; All pairs accounted for.
1270 (defun fortran-indent-line ()
1271 "Indent current Fortran line based on its contents and on previous lines."
1273 (let ((cfi (fortran-calculate-indent)))
1276 (if (or (not (= cfi
(fortran-current-line-indentation)))
1277 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
1278 (not (fortran-line-number-indented-correctly-p))))
1279 (fortran-indent-to-column cfi
)
1281 (if (fortran-find-comment-start-skip)
1282 (fortran-indent-comment))))
1283 ;; Never leave point in left margin.
1284 (if (< (current-column) cfi
)
1285 (move-to-column cfi
))
1286 (and auto-fill-function
1287 (> (save-excursion (end-of-line) (current-column))
1292 (when fortran-blink-matching-if
1293 (fortran-blink-matching-if)
1294 (fortran-blink-matching-do))))
1296 (defun fortran-auto-fill ()
1297 "Function to use for `normal-auto-fill-function' in Fortran mode."
1298 (if (> (current-column) (current-fill-column))
1299 (let ((cfi (fortran-calculate-indent)))
1302 (if (or (not (= cfi
(fortran-current-line-indentation)))
1303 (and (re-search-forward "^[ \t]*[0-9]+"
1305 (not (fortran-line-number-indented-correctly-p))))
1306 (fortran-indent-to-column cfi
)
1308 (if (fortran-find-comment-start-skip)
1309 (fortran-indent-comment))))
1311 ;; Never leave point in left margin.
1312 (if (< (current-column) cfi
)
1313 (move-to-column cfi
)))))
1315 ;; Historically this was a separate function which advertised itself
1316 ;; as reindenting but only did so where `most likely to be necessary'.
1317 (defalias 'fortran-indent-new-line
'reindent-then-newline-and-indent
)
1319 (defun fortran-indent-subprogram ()
1320 "Properly indent the Fortran subprogram containing point."
1324 (message "Indenting subprogram...")
1325 (indent-region (point) (mark) nil
))
1326 (message "Indenting subprogram...done."))
1328 (defun fortran-calculate-indent ()
1329 "Calculates the Fortran indent column based on previous lines."
1330 (let (icol first-statement
(case-fold-search t
)
1331 (fortran-minimum-statement-indent
1332 (if indent-tabs-mode
1333 fortran-minimum-statement-indent-tab
1334 fortran-minimum-statement-indent-fixed
)))
1336 (setq first-statement
(fortran-previous-statement))
1338 (setq icol fortran-minimum-statement-indent
)
1339 (if (= (point) (point-min))
1340 (setq icol fortran-minimum-statement-indent
)
1341 (setq icol
(fortran-current-line-indentation)))
1342 (skip-chars-forward " \t0-9")
1343 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1344 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1345 (let (then-test) ; multi-line if-then
1346 (while (and (zerop (forward-line 1))
1347 ;; Search forward for then.
1348 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1349 (not (setq then-test
1352 *[^ \t_$(=a-z0-9]")))))
1354 (setq icol
(+ icol fortran-if-indent
))))
1355 ((looking-at "else\\(if\\)?\\b")
1356 (setq icol
(+ icol fortran-if-indent
)))
1357 ((looking-at "select[ \t]*case[ \t](.*)")
1358 (setq icol
(+ icol fortran-if-indent
)))
1359 ((looking-at "case[ \t]*(.*)")
1360 (setq icol
(+ icol fortran-if-indent
)))
1361 ((looking-at "case[ \t]*default\\b")
1362 (setq icol
(+ icol fortran-if-indent
)))
1363 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1364 (setq icol
(+ icol fortran-if-indent
)))
1365 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1366 (setq icol
(+ icol fortran-if-indent
)))
1367 ((looking-at "do\\b")
1368 (setq icol
(+ icol fortran-do-indent
)))
1370 "\\(structure\\|union\\|map\\|interface\\)\
1371 \\b[ \t]*[^ \t=(a-z]")
1372 (setq icol
(+ icol fortran-structure-indent
)))
1373 ((and (looking-at fortran-end-prog-re1
)
1374 (fortran-check-end-prog-re))
1375 ;; Previous END resets indent to minimum.
1376 (setq icol fortran-minimum-statement-indent
)))))
1379 (cond ((looking-at "[ \t]*$"))
1380 ;; Check for directive before comment, so as not to indent.
1381 ((looking-at fortran-directive-re
)
1382 (setq fortran-minimum-statement-indent
0 icol
0))
1383 ((looking-at fortran-comment-line-start-skip
)
1384 (cond ((eq fortran-comment-indent-style
'relative
)
1385 (setq icol
(+ icol fortran-comment-line-extra-indent
)))
1386 ((eq fortran-comment-indent-style
'fixed
)
1387 (setq icol
(+ fortran-minimum-statement-indent
1388 fortran-comment-line-extra-indent
))))
1389 (setq fortran-minimum-statement-indent
0))
1390 ((or (looking-at (concat "[ \t]*"
1392 fortran-continuation-string
)))
1393 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1394 (skip-chars-forward " \t")
1395 ;; Do not introduce extra whitespace into a broken string.
1397 (if (fortran-is-in-string-p (point))
1399 (+ icol fortran-continuation-indent
))))
1401 ((and fortran-check-all-num-for-matching-do
1402 (looking-at "[ \t]*[0-9]+")
1403 (fortran-check-for-matching-do))
1404 (setq icol
(- icol fortran-do-indent
)))
1406 (skip-chars-forward " \t0-9")
1407 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1408 (setq icol
(- icol fortran-if-indent
)))
1409 ((looking-at "else\\(if\\)?\\b")
1410 (setq icol
(- icol fortran-if-indent
)))
1411 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1412 (setq icol
(- icol fortran-if-indent
)))
1413 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1414 (setq icol
(- icol fortran-if-indent
)))
1415 ((and (looking-at "continue\\b")
1416 (fortran-check-for-matching-do))
1417 (setq icol
(- icol fortran-do-indent
)))
1418 ((looking-at "end[ \t]*do\\b")
1419 (setq icol
(- icol fortran-do-indent
)))
1420 ((looking-at "end[ \t]*\
1421 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1422 (setq icol
(- icol fortran-structure-indent
)))
1423 ((and (looking-at fortran-end-prog-re1
)
1424 (fortran-check-end-prog-re)
1425 (not (= icol fortran-minimum-statement-indent
)))
1426 (message "Warning: `end' not in column %d. Probably\
1427 an unclosed block." fortran-minimum-statement-indent
))))))
1428 (max fortran-minimum-statement-indent icol
)))
1431 (defun fortran-current-line-indentation ()
1432 "Indentation of current line, ignoring Fortran line number or continuation.
1433 This is the column position of the first non-whitespace character
1434 aside from the line number and/or column 5/8 line-continuation character.
1435 For comment lines, returns indentation of the first
1436 non-indentation text within the comment."
1439 (cond ((looking-at fortran-comment-line-start-skip
)
1440 (goto-char (match-end 0))
1442 (if (stringp fortran-comment-indent-char
)
1443 fortran-comment-indent-char
1444 (char-to-string fortran-comment-indent-char
))))
1445 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1446 (goto-char (match-end 0)))
1448 ;; Move past line number.
1449 (skip-chars-forward "[ \t0-9]")))
1450 ;; Move past whitespace.
1451 (skip-chars-forward " \t")
1454 (defun fortran-indent-to-column (col)
1455 "Indent current line to column COL.
1456 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1457 line, and this continuation character is retained on indentation;
1458 2) If `fortran-continuation-string' is the first non-whitespace
1459 character, this is a continuation line;
1460 3) A non-continuation line which has a number as the first
1461 non-whitespace character is a numbered line.
1462 4) A TAB followed by a digit indicates a continuation line."
1465 (if (looking-at fortran-comment-line-start-skip
)
1466 (if fortran-comment-indent-style
1467 (let* ((char (if (stringp fortran-comment-indent-char
)
1468 (aref fortran-comment-indent-char
0)
1469 fortran-comment-indent-char
))
1470 (chars (string ? ?
\t char
)))
1471 (goto-char (match-end 0))
1472 (skip-chars-backward chars
)
1473 (delete-region (point) (progn (skip-chars-forward chars
)
1475 (insert-char char
(- col
(current-column)))))
1476 (if (looking-at "\t[1-9]")
1477 (if indent-tabs-mode
1478 (goto-char (match-end 0))
1481 (insert fortran-continuation-string
))
1482 (if (looking-at " \\{5\\}[^ 0\n]")
1483 (if indent-tabs-mode
1484 (progn (delete-char 6)
1485 (insert ?
\t (fortran-numerical-continuation-char) 1))
1487 (delete-horizontal-space)
1488 ;; Put line number in columns 0-4, or
1489 ;; continuation character in column 5.
1491 ((looking-at (regexp-quote fortran-continuation-string
))
1492 (if indent-tabs-mode
1495 (if indent-tabs-mode
1496 fortran-minimum-statement-indent-tab
1497 fortran-minimum-statement-indent-fixed
))
1499 (insert-char (fortran-numerical-continuation-char) 1))
1502 ((looking-at "[0-9]+")
1503 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1504 (if (< extra-space
0)
1505 (message "Warning: line number exceeds 5-digit limit.")
1506 (indent-to (min fortran-line-number-indent extra-space
))))
1507 (skip-chars-forward "0-9")))))
1508 ;; Point is now after any continuation character or line number.
1509 ;; Put body of statement where specified.
1510 (delete-horizontal-space)
1512 ;; Indent any comment following code on the same line.
1513 (when (fortran-find-comment-start-skip)
1514 (goto-char (match-beginning 0))
1515 (unless (= (current-column) (fortran-comment-indent))
1516 (delete-horizontal-space)
1517 (indent-to (fortran-comment-indent)))))))
1519 (defun fortran-line-number-indented-correctly-p ()
1520 "Return t if current line's line number is correctly indented.
1521 Do not call if there is no line number."
1524 (skip-chars-forward " \t")
1525 (and (<= (current-column) fortran-line-number-indent
)
1526 (or (= (current-column) fortran-line-number-indent
)
1527 (progn (skip-chars-forward "0-9")
1528 (= (current-column) 5))))))
1530 (defun fortran-check-for-matching-do ()
1531 "When called from a numbered statement, return t if matching DO is found.
1532 Otherwise return nil."
1533 (let ((case-fold-search t
)
1537 (when (looking-at "[ \t]*[0-9]+")
1538 (skip-chars-forward " \t")
1539 (skip-chars-forward "0") ; skip past leading zeros
1541 (buffer-substring (point) (progn
1542 (skip-chars-forward "0-9")
1548 (and (re-search-backward
1550 "\\(^[ \t0-9]*do[ \t]*0*"
1551 charnum
"\\b\\)\\|" "\\(^[ \t]*0*"
1555 (concat "^[ \t0-9]*do[ \t]*0*"
1558 (defun fortran-find-comment-start-skip (&optional all
)
1559 "Move to past `comment-start-skip' found on current line.
1560 Return non-nil if `comment-start-skip' found, nil if not.
1561 If ALL is nil, only match comments that start in column > 0."
1562 ;; Hopefully at some point we can just use the line below! -stef
1563 ;; (comment-search-forward (line-end-position) t))
1564 (when (or all comment-start-skip
)
1566 (css (if comment-start-skip
1567 (concat fortran-comment-line-start-skip
1568 "\\|" comment-start-skip
)
1569 fortran-comment-line-start-skip
)))
1570 (when (re-search-forward css
(line-end-position) t
)
1571 (if (and (or all
(> (match-beginning 0) (line-beginning-position)))
1572 (or (save-match-data
1573 (not (fortran-is-in-string-p (match-beginning 0))))
1574 ;; Recurse for rest of line.
1575 (fortran-find-comment-start-skip all
)))
1580 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1581 ;; Test if TAB format continuation lines work.
1582 (defun fortran-is-in-string-p (where)
1583 "Return non-nil iff WHERE (a buffer position) is inside a Fortran string."
1587 ((bolp) nil
) ; bol is never inside a string
1588 ((save-excursion ; comment lines too
1590 (looking-at fortran-comment-line-start-skip
)) nil
)
1591 (t (let ((parse-state '(0 nil nil nil nil nil
0))
1592 (quoted-comment-start (if comment-start
1593 (regexp-quote comment-start
)))
1595 parse-limit end-of-line
)
1596 ;; Move to start of current statement.
1597 (fortran-next-statement)
1598 (fortran-previous-statement)
1599 ;; Now parse up to WHERE.
1601 (if (or ;; Skip to next line if:
1603 (looking-at fortran-comment-line-start-skip
)
1604 ;; - at end of line?
1606 ;; - not in a string and after comment-start?
1607 (and (not (nth 3 parse-state
))
1609 (equal comment-start
1610 (char-to-string (preceding-char)))))
1611 (if (> (forward-line) 0)
1612 (setq not-done nil
))
1614 ;; If we are at beginning of code line, skip any
1615 ;; whitespace, labels and tab continuation markers.
1616 (if (bolp) (skip-chars-forward " \t0-9"))
1617 ;; If we are in column <= 5 now, check for continuation char.
1618 (cond ((= 5 (current-column)) (forward-char 1))
1619 ((and (< (current-column) 5)
1620 (equal fortran-continuation-string
1621 (char-to-string (following-char)))
1623 ;; Find out parse-limit from here.
1624 (setq end-of-line
(line-end-position))
1625 (setq parse-limit
(min where end-of-line
))
1626 ;; Parse max up to comment-start, if non-nil and in current line.
1629 (if (re-search-forward quoted-comment-start end-of-line t
)
1630 (setq parse-limit
(min (point) parse-limit
)))))
1631 ;; Now parse if still in limits.
1632 (if (< (point) where
)
1633 (setq parse-state
(parse-partial-sexp
1634 (point) parse-limit nil nil parse-state
))
1635 (setq not-done nil
))))
1637 (nth 3 parse-state
))))))
1639 ;; From old version.
1640 (defalias 'fortran-auto-fill-mode
'auto-fill-mode
)
1642 (defun fortran-fill ()
1643 "Fill the current line at an appropriate point(s)."
1644 (let* ((auto-fill-function #'fortran-auto-fill
)
1646 (bol (line-beginning-position))
1647 (eol (line-end-position))
1648 (bos (min eol
(+ bol
(fortran-current-line-indentation))))
1652 ;; OK to break quotes on comment lines.
1653 (unless (looking-at fortran-comment-line-start-skip
)
1654 (let (fcpoint start
)
1655 (move-to-column fill-column
)
1656 (when (fortran-is-in-string-p (setq fcpoint
(point)))
1658 (re-search-backward "\\S\"\\s\"\\S\"" bol t
)
1660 (if fortran-break-before-delimiters
1663 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t
)
1665 ;; If the current string is longer than 72 - 6 chars,
1666 ;; break it at the fill column (else infinite loop).
1667 (if (> (- (point) start
)
1668 (- fill-column
6 fortran-continuation-indent
))
1671 ;; Decide where to split the line. If a position for a quoted
1672 ;; string was found above then use that, else break the line
1673 ;; before the last delimiter.
1674 ;; Delimiters are whitespace, commas, and operators.
1675 ;; Will break before a pair of *'s.
1679 (move-to-column (1+ fill-column
))
1680 ;; GM Make this a defcustom as in f90-mode? Add ", (?
1681 (skip-chars-backward "^ \t\n,'+-/*=)"
1682 ;;; (if fortran-break-before-delimiters
1683 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1685 (when (<= (point) (1+ bos
))
1686 (move-to-column (1+ fill-column
))
1687 ;; What is this doing???
1688 (or (re-search-forward "[\t\n,'+-/*)=]" eol t
)
1691 (re-search-forward "[ \t]" opoint t
)
1693 (if (looking-at "\\s\"")
1695 (skip-chars-backward " \t\*")))
1696 (if fortran-break-before-delimiters
1699 ;; If we are in an in-line comment, don't break unless the
1700 ;; line of code is longer than it should be. Otherwise
1701 ;; break the line at the column computed above.
1703 ;; Need to use fortran-find-comment-start-skip to make sure that
1704 ;; quoted !'s don't prevent a break.
1705 (when (and (save-excursion
1707 (if (not (fortran-find-comment-start-skip))
1709 (goto-char (match-beginning 0))
1710 (>= (point) fill-point
)))
1712 (goto-char fill-point
)
1717 (min (1+ fill-column
)
1718 (+ (fortran-calculate-indent)
1719 fortran-continuation-indent
))))
1720 (goto-char fill-point
)
1721 (fortran-break-line)
1724 (defun fortran-break-line ()
1725 "Call `fortran-split-line'. Joins continuation lines first, then refills."
1726 (let ((opoint (point))
1727 (bol (line-beginning-position))
1730 (if (fortran-find-comment-start-skip)
1731 (delete-and-extract-region
1732 (match-beginning 0) (line-end-position))))))
1733 ;; Forward line 1 really needs to go to next non white line.
1734 (if (save-excursion (forward-line)
1735 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1738 (delete-region (point) (match-end 0))
1739 (delete-horizontal-space)
1741 (fortran-split-line))
1746 (delete-horizontal-space)
1747 (indent-to (fortran-comment-indent))
1748 (insert comment-string
)))))
1750 (defun fortran-analyze-file-format ()
1751 "Return nil if fixed format is used, t if TAB formatting is used.
1752 Use `fortran-tab-mode-default' if no non-comment statements are found
1753 before the end or in the first `fortran-analyze-depth' lines."
1756 (goto-char (point-min))
1759 (eq (char-after) ?
\t)
1760 (looking-at " \\{6\\}")
1761 (> i fortran-analyze-depth
)))
1765 ((eq (char-after) ?
\t) t
)
1766 ((looking-at " \\{6\\}") nil
)
1767 (t fortran-tab-mode-default
)))))
1769 (defun fortran-fill-paragraph (&optional justify
)
1770 "Fill surrounding comment block as paragraphs, else fill statement.
1771 Intended as the value of `fill-paragraph-function'.
1772 A comment block is filled by calling `fill-comment-paragraph' with
1773 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
1774 Always returns non-nil (to prevent `fill-paragraph' being called)."
1776 (or (fill-comment-paragraph justify
)
1777 (fortran-fill-statement)
1780 (defun fortran-fill-statement ()
1781 "Fill a Fortran statement up to `fill-column'."
1783 (let ((auto-fill-function #'fortran-auto-fill
))
1784 (unless (save-excursion
1786 (or (looking-at "[ \t]*$")
1787 (looking-at fortran-comment-line-start-skip
)
1788 (and comment-start-skip
1789 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1791 ;; Find beginning of statement.
1792 (fortran-next-statement)
1793 (fortran-previous-statement)
1794 ;; Re-indent initially.
1795 (fortran-indent-line)
1796 ;; Replace newline plus continuation field plus indentation with
1800 (fortran-remove-continuation)))
1801 (fortran-previous-statement)))
1802 (fortran-indent-line)))
1804 (defun fortran-strip-sequence-nos (&optional do-space
)
1805 "Delete all text in column 72 and up (assumed to be sequence numbers).
1806 Normally also deletes trailing whitespace after stripping such text.
1807 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
1810 (goto-char (point-min))
1811 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t
)
1812 (replace-match "" nil nil nil
1)
1813 (unless do-space
(delete-horizontal-space)))))
1815 ;; This code used to live in add-log.el, but this is a better place
1817 (defun fortran-current-defun ()
1818 "Function to use for `add-log-current-defun-function' in Fortran mode."
1820 ;; We must be inside function body for this to work.
1821 (fortran-beginning-of-subprogram)
1822 (let ((case-fold-search t
)) ; case-insensitive
1823 ;; Search for fortran subprogram start.
1824 (if (re-search-forward
1825 (concat "^[ \t]*\\(program\\|subroutine\\|function"
1826 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
1827 "\\(block[ \t]*data\\)\\)")
1828 (save-excursion (fortran-end-of-subprogram)
1831 (or (match-string-no-properties 2)
1833 ;; Move to EOL or before first left paren.
1834 (if (re-search-forward "[(\n]" nil t
)
1835 (progn (backward-char)
1836 (skip-chars-backward " \t"))
1838 ;; Use the name preceding that.
1839 (buffer-substring-no-properties (point) (progn (backward-sexp)
1845 ;;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
1846 ;;; fortran.el ends here