1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;; Copyright (c) 1986, 93, 94, 95, 97, 98, 99, 2000, 2001
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-mode-map
423 (let ((map (make-sparse-keymap)))
424 (define-key map
";" 'fortran-abbrev-start
)
425 (define-key map
"\C-c;" 'fortran-comment-region
)
426 (define-key map
"\M-;" 'fortran-indent-comment
)
427 (define-key map
"\M-\n" 'fortran-split-line
)
428 (define-key map
"\M-\C-q" 'fortran-indent-subprogram
)
429 (define-key map
"\C-c\C-w" 'fortran-window-create-momentarily
)
430 (define-key map
"\C-c\C-r" 'fortran-column-ruler
)
431 (define-key map
"\C-c\C-p" 'fortran-previous-statement
)
432 (define-key map
"\C-c\C-n" 'fortran-next-statement
)
433 (define-key map
"\C-c\C-d" 'fortran-join-line
) ; like f90
434 (define-key map
"\M-^" 'fortran-join-line
) ; subvert delete-indentation
435 (define-key map
"0" 'fortran-electric-line-number
)
436 (define-key map
"1" 'fortran-electric-line-number
)
437 (define-key map
"2" 'fortran-electric-line-number
)
438 (define-key map
"3" 'fortran-electric-line-number
)
439 (define-key map
"4" 'fortran-electric-line-number
)
440 (define-key map
"5" 'fortran-electric-line-number
)
441 (define-key map
"6" 'fortran-electric-line-number
)
442 (define-key map
"7" 'fortran-electric-line-number
)
443 (define-key map
"8" 'fortran-electric-line-number
)
444 (define-key map
"9" 'fortran-electric-line-number
)
446 (easy-menu-define fortran-menu map
"Menu for Fortran mode."
448 ["Manual" (info "(emacs)Fortran")]
450 ,(custom-menu-create 'fortran
)
452 ["Save" Custom-save t
]
453 ["Reset to Current" Custom-reset-current t
]
454 ["Reset to Saved" Custom-reset-saved t
]
455 ["Reset to Standard Settings" Custom-reset-standard t
]
458 ["Comment Region" fortran-comment-region mark-active
]
460 (fortran-comment-region (region-beginning) (region-end) 1)
462 ["Indent Region" indent-region mark-active
]
463 ["Indent Subprogram" fortran-indent-subprogram t
]
465 ["Beginning of Subprogram" fortran-beginning-of-subprogram t
]
466 ["End of Subprogram" fortran-end-of-subprogram t
]
468 ["Subprogram" mark-defun
t]
469 ["IF Block" fortran-mark-if t
]
470 ["DO Block" fortran-mark-do t
]
472 ["Narrow to Subprogram" narrow-to-defun
t]
475 ["Temporary column ruler" fortran-column-ruler t
]
476 ["72-column window" fortran-window-create t
]
478 (enlarge-window-horizontally (- (frame-width) (window-width)))
479 (< (window-width) (frame-width))]
480 ["Momentary 72-column window" fortran-window-create-momentarily t
]
482 ["Break Line at Point" fortran-split-line t
]
483 ["Join Line" fortran-join-line t
]
484 ["Fill Statement/Comment" fill-paragraph t
]
486 ["Toggle auto-fill" auto-fill-mode
:selected auto-fill-function
488 ["Toggle abbrev-mode" abbrev-mode
:selected abbrev-mode
490 ["Add imenu Menu" imenu-add-menubar-index
491 :active
(not (lookup-key (current-local-map) [menu-bar index
]))
492 :included
(fboundp 'imenu-add-to-menubar
)]))
494 "Keymap used in Fortran mode.")
497 (defvar fortran-mode-abbrev-table
498 (let (abbrevs-changed)
499 (define-abbrev-table 'fortran-mode-abbrev-table nil
)
500 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
501 ;; Only use `apply' to quieten the byte-compiler.
503 (function (lambda (element)
505 (apply 'define-abbrev fortran-mode-abbrev-table
506 (append element
'(nil 0 t
)))
507 (wrong-number-of-arguments
508 (apply 'define-abbrev fortran-mode-abbrev-table
509 (append element
'(nil 0)))))))
510 '((";au" "automatic" )
512 (";bd" "block data" )
521 (";dc" "double complex" )
522 (";dp" "double precision" )
528 (";eq" "equivalence" )
537 (";ib" "implicit byte" )
538 (";ic" "implicit complex" )
539 (";ich" "implicit character")
540 (";ii" "implicit integer" )
541 (";il" "implicit logical" )
542 (";ir" "implicit real" )
545 (";intr" "intrinsic" )
548 (";o" "open" ) ; was ;op
562 (";su" "subroutine" )
568 fortran-mode-abbrev-table
))
573 (defun fortran-mode ()
574 "Major mode for editing Fortran code in fixed format.
575 For free format code, use `f90-mode'.
577 \\[fortran-indent-line] indents the current Fortran line correctly.
578 Note that DO statements must not share a common CONTINUE.
580 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
586 Variables controlling indentation style and extra features:
589 To use comments starting with `!', set this to the string \"!\".
591 Extra indentation within DO blocks (default 3).
593 Extra indentation within IF blocks (default 3).
594 `fortran-structure-indent'
595 Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
597 `fortran-continuation-indent'
598 Extra indentation applied to continuation statements (default 5).
599 `fortran-comment-line-extra-indent'
600 Amount of extra indentation for text in full-line comments (default 0).
601 `fortran-comment-indent-style'
602 How to indent the text in full-line comments. Allowed values are:
603 nil don't change the indentation
604 fixed indent to `fortran-comment-line-extra-indent' beyond the
606 `fortran-minimum-statement-indent-fixed' (fixed format) or
607 `fortran-minimum-statement-indent-tab' (TAB format),
608 depending on the continuation format in use.
609 relative indent to `fortran-comment-line-extra-indent' beyond the
610 indentation for a line of code.
612 `fortran-comment-indent-char'
613 Single-character string to be inserted instead of space for
614 full-line comment indentation (default \" \").
615 `fortran-minimum-statement-indent-fixed'
616 Minimum indentation for statements in fixed format mode (default 6).
617 `fortran-minimum-statement-indent-tab'
618 Minimum indentation for statements in TAB format mode (default 9).
619 `fortran-line-number-indent'
620 Maximum indentation for line numbers (default 1). A line number will
621 get less than this much indentation if necessary to avoid reaching
623 `fortran-check-all-num-for-matching-do'
624 Non-nil causes all numbered lines to be treated as possible \"continue\"
625 statements (default nil).
626 `fortran-blink-matching-if'
627 Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
628 to blink on the matching IF (or DO [WHILE]). (default nil)
629 `fortran-continuation-string'
630 Single-character string to be inserted in column 5 of a continuation
631 line (default \"$\").
632 `fortran-comment-region'
633 String inserted by \\[fortran-comment-region] at start of each line in
634 the region (default \"c$$$\").
635 `fortran-electric-line-number'
636 Non-nil causes line number digits to be moved to the correct column
637 as typed (default t).
638 `fortran-break-before-delimiters'
639 Non-nil causes lines to be broken before delimiters (default t).
641 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
642 with no args, if that value is non-nil."
644 (kill-all-local-variables)
645 (setq major-mode
'fortran-mode
647 local-abbrev-table fortran-mode-abbrev-table
)
648 (set-syntax-table fortran-mode-syntax-table
)
649 (use-local-map fortran-mode-map
)
650 (set (make-local-variable 'indent-line-function
) 'fortran-indent-line
)
651 (set (make-local-variable 'indent-region-function
)
653 (let (fortran-blink-matching-if ; avoid blinking delay
654 indent-region-function
)
655 (indent-region start end nil
))))
656 (set (make-local-variable 'require-final-newline
) t
)
657 ;; The syntax tables don't understand the column-0 comment-markers.
658 (set (make-local-variable 'comment-use-syntax
) nil
)
659 (set (make-local-variable 'comment-padding
) "$$$")
660 (set (make-local-variable 'comment-start
) fortran-comment-line-start
)
661 (set (make-local-variable 'comment-start-skip
)
662 ;; We can't reuse `fortran-comment-line-start-skip' directly because
663 ;; it contains backrefs whereas we need submatch-1 to end at the
664 ;; beginning of the comment delimiter.
665 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
666 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
667 (set (make-local-variable 'comment-indent-function
) 'fortran-comment-indent
)
668 (set (make-local-variable 'abbrev-all-caps
) t
)
669 (set (make-local-variable 'normal-auto-fill-function
) 'fortran-auto-fill
)
670 (set (make-local-variable 'indent-tabs-mode
) (fortran-analyze-file-format))
671 (setq mode-line-process
'(indent-tabs-mode fortran-tab-mode-string
))
672 (set (make-local-variable 'fill-column
) 72)
673 (set (make-local-variable 'fill-paragraph-function
) 'fortran-fill-paragraph
)
674 (set (make-local-variable 'font-lock-defaults
)
675 '((fortran-font-lock-keywords
676 fortran-font-lock-keywords-1
677 fortran-font-lock-keywords-2
678 fortran-font-lock-keywords-3
)
679 nil t
((?
/ .
"$/") ("_$" .
"w"))
680 fortran-beginning-of-subprogram
))
681 (set (make-local-variable 'font-lock-syntactic-keywords
)
682 fortran-font-lock-syntactic-keywords
)
683 (set (make-local-variable 'imenu-case-fold-search
) t
)
684 (set (make-local-variable 'imenu-generic-expression
)
685 fortran-imenu-generic-expression
)
686 (set (make-local-variable 'imenu-syntax-alist
) '(("_$" .
"w")))
687 (set (make-local-variable 'beginning-of-defun-function
)
688 #'fortran-beginning-of-subprogram
)
689 (set (make-local-variable 'end-of-defun-function
)
690 #'fortran-end-of-subprogram
)
691 (set (make-local-variable 'add-log-current-defun-function
)
692 #'fortran-current-defun
)
693 (set (make-local-variable 'dabbrev-case-fold-search
) 'case-fold-search
)
694 (run-hooks 'fortran-mode-hook
))
697 (defsubst fortran-comment-indent
()
698 "Return the indentation appropriate for the current comment line.
699 This is 0 for a line matching `fortran-comment-line-start-skip', else
700 the value of `comment-column' (leaving at least one space after code)."
701 (if (looking-at fortran-comment-line-start-skip
) 0
703 (skip-chars-backward " \t")
704 (max (1+ (current-column)) comment-column
))))
706 (defun fortran-indent-comment ()
707 "Align or create comment on current line.
708 Existing comments of all types are recognized and aligned.
709 If the line has no comment, a side-by-side comment is inserted and aligned,
710 if the value of `comment-start' is not nil and allows such comments.
711 Otherwise, a separate-line comment is inserted, on this line
712 or on a new line inserted before this line if this line is not blank."
715 ;; Recognize existing comments of either kind.
716 (cond ((fortran-find-comment-start-skip 'all
)
717 (goto-char (match-beginning 0))
719 (fortran-indent-line)
720 (unless (= (current-column) (fortran-comment-indent))
721 (delete-horizontal-space)
722 (indent-to (fortran-comment-indent)))))
723 ;; No existing comment.
724 ;; If side-by-side comments are defined, insert one,
725 ;; unless line is now blank.
726 ((and comment-start
(not (looking-at "[ \t]*$"))
727 (string-match comment-start-skip
(concat " " comment-start
)))
729 (delete-horizontal-space)
730 (indent-to (fortran-comment-indent))
731 (insert comment-start
))
732 ;; Else insert separate-line comment, making a new line if nec.
734 (if (looking-at "^[ \t]*$")
735 (delete-horizontal-space)
739 (insert fortran-comment-line-start
)
740 (insert-char (if (stringp fortran-comment-indent-char
)
741 (aref fortran-comment-indent-char
0)
742 fortran-comment-indent-char
)
743 (- (fortran-calculate-indent) (current-column))))))
745 (defun fortran-comment-region (beg-region end-region arg
)
746 "Comment every line in the region.
747 Inserts the string variable `fortran-comment-region' at the beginning of
748 every line in the region.
749 BEG-REGION and END-REGION specify the region boundaries.
750 With non-nil ARG, uncomments the region."
751 (interactive "*r\nP")
752 (let ((end-region-mark (copy-marker end-region
))
753 (save-point (point-marker)))
754 (goto-char beg-region
)
757 (let ((com (regexp-quote fortran-comment-region
))) ; uncomment
759 (delete-region (point) (match-end 0)))
760 (while (and (zerop (forward-line 1))
761 (< (point) end-region-mark
))
763 (delete-region (point) (match-end 0)))))
764 (insert fortran-comment-region
) ; comment
765 (while (and (zerop (forward-line 1))
766 (< (point) end-region-mark
))
767 (insert fortran-comment-region
)))
768 (goto-char save-point
)
769 (set-marker end-region-mark nil
)
770 (set-marker save-point nil
)))
773 (defun fortran-abbrev-start ()
774 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
775 Any other key combination is executed normally."
777 (insert last-command-char
)
779 (if (fboundp 'next-command-event
) ; XEmacs
780 (setq event
(next-command-event)
781 char
(event-to-character event
))
782 (setq event
(read-event)
784 ;; Insert char if not equal to `?', or if abbrev-mode is off.
785 (if (and abbrev-mode
(or (eq char ??
) (eq char help-char
)))
786 (fortran-abbrev-help)
787 (setq unread-command-events
(list event
)))))
789 (defun fortran-abbrev-help ()
790 "List the currently defined abbrevs in Fortran mode."
792 (message "Listing abbrev table...")
793 (display-buffer (fortran-prepare-abbrev-list-buffer))
794 (message "Listing abbrev table...done"))
796 (defun fortran-prepare-abbrev-list-buffer ()
797 "Create a buffer listing the Fortran mode abbreviations."
799 (set-buffer (get-buffer-create "*Abbrevs*"))
801 (insert-abbrev-table-description 'fortran-mode-abbrev-table t
)
802 (goto-char (point-min))
803 (set-buffer-modified-p nil
)
805 (get-buffer-create "*Abbrevs*"))
807 (defun fortran-column-ruler ()
808 "Insert a column ruler momentarily above current line, till next keystroke.
809 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
810 format mode, and `fortran-column-ruler-tab' in TAB format mode.
811 The next key typed is executed unless it is SPC."
813 (momentary-string-display
815 fortran-column-ruler-tab
816 fortran-column-ruler-fixed
)
819 (if (eq (window-start (selected-window))
820 (window-point (selected-window)))
821 (line-beginning-position 2)
823 nil
"Type SPC or any command to erase ruler."))
825 (defun fortran-window-create ()
826 "Make the window 72 columns wide.
827 See also `fortran-window-create-momentarily'."
829 (let ((window-min-width 2))
830 (if (< (window-width) (frame-width))
831 (enlarge-window-horizontally (- (frame-width)
833 (let* ((window-edges (window-edges))
834 (scroll-bar-width (- (nth 2 window-edges
)
837 (split-window-horizontally (+ 72 scroll-bar-width
)))
839 (switch-to-buffer " fortran-window-extra" t
)
840 (select-window (previous-window))))
842 (defun fortran-window-create-momentarily (&optional arg
)
843 "Momentarily make the window 72 columns wide.
844 Optional ARG non-nil and non-unity disables the momentary feature.
845 See also `fortran-window-create'."
849 (save-window-excursion
852 (fortran-window-create)
853 (error (error "No room for Fortran window")))
854 (message "Type SPC to continue editing.")
855 (let ((char (read-event)))
856 (or (equal char
(string-to-char " "))
857 (setq unread-command-events
(list char
))))))
858 (fortran-window-create)))
860 (defun fortran-split-line ()
861 "Break line at point and insert continuation marker and alignment."
863 (delete-horizontal-space)
867 (and (fortran-find-comment-start-skip 'all
)
868 (< (match-beginning 0) pos
))))
869 (insert ?
\n (match-string 0))
871 (insert ?
\n ?
\t (fortran-numerical-continuation-char))
872 (insert "\n " fortran-continuation-string
))) ; space after \n important
873 (fortran-indent-line)) ; when cont string is C, c or *
875 (defun fortran-remove-continuation ()
876 "Delete any Fortran continuation characters at point.
877 Returns t if anything actually deleted."
878 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
883 (defun fortran-join-line (arg)
884 "Join current line to the previous one and re-indent.
885 With a prefix argument, repeat this operation that many times.
886 If the prefix argument ARG is negative, join the next -ARG lines.
887 Continuation lines are correctly handled."
893 (while (not (zerop arg
))
895 (or (fortran-remove-continuation)
896 (delete-indentation))
898 (fortran-indent-line)))
900 (defun fortran-numerical-continuation-char ()
901 "Return a digit for tab-digit style of continuation lines.
902 If previous line is a tab-digit continuation line, return that digit
903 plus one, otherwise return 1. Zero not allowed."
906 (if (looking-at "\t[1-9]")
907 (+ ?
1 (%
(- (char-after (1+ (point))) ?
0) 9))
910 (put 'fortran-electric-line-number
'delete-selection t
)
911 (defun fortran-electric-line-number (arg)
912 "Self insert, but if part of a Fortran line number indent it automatically.
913 Auto-indent does not happen if a numeric ARG is used."
915 (if (or arg
(not fortran-electric-line-number
))
917 (self-insert-command (prefix-numeric-value arg
))
918 (self-insert-command 1))
919 (if (or (and (= 5 (current-column))
922 ;; In col 5 with only spaces to the left.
923 (looking-at " \\{5\\}")))
924 (and (= (if indent-tabs-mode
925 fortran-minimum-statement-indent-tab
926 fortran-minimum-statement-indent-fixed
) (current-column))
927 ;; In col 8 with a single tab to the left.
928 (eq ?
\t (char-after (line-beginning-position)))
929 (not (or (eq last-command
'fortran-indent-line
)
931 'fortran-indent-new-line
))))
933 (re-search-backward "[^ \t0-9]"
934 (line-beginning-position)
935 t
)) ; not a line number
936 (looking-at "[0-9]")) ; within a line number
937 (self-insert-command (prefix-numeric-value arg
))
938 (skip-chars-backward " \t")
939 (insert last-command-char
)
940 (fortran-indent-line))))
943 (defun fortran-check-end-prog-re ()
944 "Check a preliminary match against `fortran-end-prog-re'."
945 ;; Having got a possible match for the subprogram end, we need a
946 ;; match of whitespace, avoiding possible column 73+ stuff.
948 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
949 (buffer-substring (match-end 0)
950 (min (line-end-position)
951 (+ 72 (line-beginning-position)))))))
953 ;; Note that you can't just check backwards for `subroutine' &c in
954 ;; case of un-marked main programs not at the start of the file.
955 (defun fortran-beginning-of-subprogram ()
956 "Move point to the beginning of the current Fortran subprogram."
959 (let ((case-fold-search t
))
960 (beginning-of-line -
1)
962 (while (re-search-backward fortran-end-prog-re nil
'move
)
963 (if (fortran-check-end-prog-re)
967 (defun fortran-end-of-subprogram ()
968 "Move point to the end of the current Fortran subprogram."
971 (let ((case-fold-search t
))
972 (if (save-excursion ; on END
974 (and (looking-at fortran-end-prog-re
)
975 (fortran-check-end-prog-re)))
977 (beginning-of-line 2)
979 (while (re-search-forward fortran-end-prog-re nil
'move
)
980 (if (fortran-check-end-prog-re)
982 (goto-char (match-beginning 0))
985 (defun fortran-previous-statement ()
986 "Move point to beginning of the previous Fortran statement.
987 Returns 'first-statement if that statement is the first
988 non-comment Fortran statement in the file, and nil otherwise.
989 Directive lines are treated as comments."
991 (let (not-first-statement continue-test
)
995 (not (looking-at fortran-comment-line-start-skip
))
996 (not (looking-at fortran-directive-re
))
999 (regexp-quote fortran-continuation-string
)))
1000 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1001 (while (and (setq not-first-statement
(zerop (forward-line -
1)))
1002 (or (looking-at fortran-comment-line-start-skip
)
1003 (looking-at fortran-directive-re
)
1006 (regexp-quote fortran-continuation-string
)))
1007 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1008 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1009 (cond ((and continue-test
1010 (not not-first-statement
))
1011 (message "Incomplete continuation statement."))
1013 (fortran-previous-statement))
1014 ((not not-first-statement
)
1015 'first-statement
))))
1017 (defun fortran-next-statement ()
1018 "Move point to beginning of the next Fortran statement.
1019 Returns 'last-statement if that statement is the last
1020 non-comment Fortran statement in the file, and nil otherwise.
1021 Directive lines are treated as comments."
1023 (let (not-last-statement)
1025 (while (and (setq not-last-statement
1026 (and (zerop (forward-line 1))
1028 (or (looking-at fortran-comment-line-start-skip
)
1029 (looking-at fortran-directive-re
)
1030 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1031 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1032 (if (not not-last-statement
)
1036 (defun fortran-blink-match (regex keyword find-begin
)
1037 "From a line matching REGEX, blink matching KEYWORD statement line.
1038 Use function FIND-BEGIN to match it."
1039 (let ((top-of-window (window-start))
1041 (case-fold-search t
)
1044 (when (save-excursion
1046 (skip-chars-forward " \t0-9")
1048 (if (not (setq matching
(funcall find-begin
)))
1049 (setq message
(concat "No matching " keyword
"."))
1050 (if (< matching top-of-window
)
1052 (goto-char matching
)
1056 (buffer-substring (point)
1057 (line-end-position)))))))
1059 (message "%s" message
)
1060 (goto-char matching
)
1062 (goto-char end-point
)))))
1064 (defun fortran-blink-matching-if ()
1065 "From an ENDIF or ELSE statement, blink the matching IF statement."
1066 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1067 "if" #'fortran-beginning-if
))
1069 (defun fortran-blink-matching-do ()
1070 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1071 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do
))
1073 (defun fortran-mark-do ()
1074 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1075 The marks are pushed."
1077 (let (enddo-point do-point
)
1078 (if (setq enddo-point
(fortran-end-do))
1079 (if (not (setq do-point
(fortran-beginning-do)))
1080 (message "No matching do.")
1081 (goto-char enddo-point
)
1083 (goto-char do-point
)))))
1085 (defun fortran-end-do ()
1086 "Search forward for first unmatched ENDDO.
1087 Return point or nil."
1088 (let ((case-fold-search t
))
1089 (if (save-excursion (beginning-of-line)
1090 (skip-chars-forward " \t0-9")
1091 (looking-at "end[ \t]*do\\b"))
1097 (while (and (not (zerop count
))
1098 (not (eq (fortran-next-statement) 'last-statement
))
1099 ;; Keep local to subprogram.
1100 (not (and (looking-at fortran-end-prog-re
)
1101 (fortran-check-end-prog-re))))
1102 (skip-chars-forward " \t0-9")
1103 (cond ((looking-at "end[ \t]*do\\b")
1104 (setq count
(1- count
)))
1106 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1107 (setq count
(1+ count
)))))
1109 ;; All pairs accounted for.
1112 (defun fortran-beginning-do ()
1113 "Search backwards for first unmatched DO [WHILE].
1114 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1115 (let ((case-fold-search t
)
1116 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1119 (skip-chars-forward " \t0-9")
1120 (looking-at dostart-re
))
1126 (while (and (not (zerop count
))
1127 (not (eq (fortran-previous-statement) 'first-statement
))
1128 ;; Keep local to subprogram.
1129 (not (and (looking-at fortran-end-prog-re
)
1130 (fortran-check-end-prog-re))))
1131 (skip-chars-forward " \t0-9")
1132 (cond ((looking-at dostart-re
)
1133 (setq count
(1- count
)))
1134 ;; Note labelled loop ends not considered.
1135 ((looking-at "end[ \t]*do\\b")
1136 (setq count
(1+ count
)))))
1138 ;; All pairs accounted for.
1141 (defun fortran-mark-if ()
1142 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1143 The marks are pushed."
1145 (let (endif-point if-point
)
1146 (if (setq endif-point
(fortran-end-if))
1147 (if (not (setq if-point
(fortran-beginning-if)))
1148 (message "No matching if.")
1149 ;; Set mark, move point.
1150 (goto-char endif-point
)
1152 (goto-char if-point
)))))
1154 (defun fortran-end-if ()
1155 "Search forwards for first unmatched ENDIF.
1156 Return point or nil."
1157 (let ((case-fold-search t
))
1158 (if (save-excursion (beginning-of-line)
1159 (skip-chars-forward " \t0-9")
1160 (looking-at "end[ \t]*if\\b"))
1163 ;; Search for one. The point has been already been moved to first
1164 ;; letter on line but this should not cause troubles.
1167 (while (and (not (zerop count
))
1168 (not (eq (fortran-next-statement) 'last-statement
))
1169 ;; Keep local to subprogram.
1170 (not (and (looking-at fortran-end-prog-re
)
1171 (fortran-check-end-prog-re))))
1172 (skip-chars-forward " \t0-9")
1173 (cond ((looking-at "end[ \t]*if\\b")
1174 (setq count
(1- count
)))
1175 ((looking-at fortran-if-start-re
)
1178 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1179 (let (then-test) ; multi-line if-then
1182 (zerop (forward-line 1))
1183 ;; Search forward for then.
1184 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1188 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1190 (setq count
(1+ count
)))))))
1192 ;; All pairs accounted for.
1195 (defun fortran-beginning-if ()
1196 "Search backwards for first unmatched IF-THEN.
1197 Return point or nil."
1198 (let ((case-fold-search t
))
1200 ;; May be sitting on multi-line if-then statement, first
1201 ;; move to beginning of current statement. Note:
1202 ;; `fortran-previous-statement' moves to previous statement
1203 ;; *unless* current statement is first one. Only move
1204 ;; forward if not first-statement.
1205 (if (not (eq (fortran-previous-statement) 'first-statement
))
1206 (fortran-next-statement))
1207 (skip-chars-forward " \t0-9")
1209 (looking-at fortran-if-start-re
)
1211 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1212 ;; Multi-line if-then.
1215 (and (zerop (forward-line 1))
1216 ;; Search forward for then.
1217 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1221 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1228 (while (and (not (zerop count
))
1229 (not (eq (fortran-previous-statement) 'first-statement
))
1230 ;; Keep local to subprogram.
1231 (not (and (looking-at fortran-end-prog-re
)
1232 (fortran-check-end-prog-re))))
1233 (skip-chars-forward " \t0-9")
1234 (cond ((looking-at fortran-if-start-re
)
1237 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1238 (let (then-test) ; multi-line if-then
1241 (zerop (forward-line 1))
1242 ;; Search forward for then.
1243 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1247 (concat ".*then\\b[ \t]*"
1248 "[^ \t(=a-z0-9]"))))))
1250 (setq count
(1- count
)))))
1251 ((looking-at "end[ \t]*if\\b")
1252 (setq count
(1+ count
)))))
1254 ;; All pairs accounted for.
1258 (defun fortran-indent-line ()
1259 "Indent current Fortran line based on its contents and on previous lines."
1261 (let ((cfi (fortran-calculate-indent)))
1264 (if (or (not (= cfi
(fortran-current-line-indentation)))
1265 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
1266 (not (fortran-line-number-indented-correctly-p))))
1267 (fortran-indent-to-column cfi
)
1269 (if (fortran-find-comment-start-skip)
1270 (fortran-indent-comment))))
1271 ;; Never leave point in left margin.
1272 (if (< (current-column) cfi
)
1273 (move-to-column cfi
))
1274 (and auto-fill-function
1275 (> (save-excursion (end-of-line) (current-column))
1280 (when fortran-blink-matching-if
1281 (fortran-blink-matching-if)
1282 (fortran-blink-matching-do))))
1284 (defun fortran-auto-fill ()
1285 "Function to use for `normal-auto-fill-function' in Fortran mode."
1286 (if (> (current-column) (current-fill-column))
1287 (let ((cfi (fortran-calculate-indent)))
1290 (if (or (not (= cfi
(fortran-current-line-indentation)))
1291 (and (re-search-forward "^[ \t]*[0-9]+"
1293 (not (fortran-line-number-indented-correctly-p))))
1294 (fortran-indent-to-column cfi
)
1296 (if (fortran-find-comment-start-skip)
1297 (fortran-indent-comment))))
1299 ;; Never leave point in left margin.
1300 (if (< (current-column) cfi
)
1301 (move-to-column cfi
)))))
1303 ;; Historically this was a separate function which advertised itself
1304 ;; as reindenting but only did so where `most likely to be necessary'.
1305 (defalias 'fortran-indent-new-line
'reindent-then-newline-and-indent
)
1307 (defun fortran-indent-subprogram ()
1308 "Properly indent the Fortran subprogram containing point."
1312 (message "Indenting subprogram...")
1313 (indent-region (point) (mark) nil
))
1314 (message "Indenting subprogram...done."))
1316 (defun fortran-calculate-indent ()
1317 "Calculates the Fortran indent column based on previous lines."
1318 (let (icol first-statement
(case-fold-search t
)
1319 (fortran-minimum-statement-indent
1320 (if indent-tabs-mode
1321 fortran-minimum-statement-indent-tab
1322 fortran-minimum-statement-indent-fixed
)))
1324 (setq first-statement
(fortran-previous-statement))
1326 (setq icol fortran-minimum-statement-indent
)
1327 (if (= (point) (point-min))
1328 (setq icol fortran-minimum-statement-indent
)
1329 (setq icol
(fortran-current-line-indentation)))
1330 (skip-chars-forward " \t0-9")
1331 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1332 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1333 (let (then-test) ; multi-line if-then
1334 (while (and (zerop (forward-line 1))
1335 ;; Search forward for then.
1336 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1337 (not (setq then-test
1340 *[^ \t_$(=a-z0-9]")))))
1342 (setq icol
(+ icol fortran-if-indent
))))
1343 ((looking-at "else\\(if\\)?\\b")
1344 (setq icol
(+ icol fortran-if-indent
)))
1345 ((looking-at "select[ \t]*case[ \t](.*)")
1346 (setq icol
(+ icol fortran-if-indent
)))
1347 ((looking-at "case[ \t]*(.*)")
1348 (setq icol
(+ icol fortran-if-indent
)))
1349 ((looking-at "case[ \t]*default\\b")
1350 (setq icol
(+ icol fortran-if-indent
)))
1351 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1352 (setq icol
(+ icol fortran-if-indent
)))
1353 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1354 (setq icol
(+ icol fortran-if-indent
)))
1355 ((looking-at "do\\b")
1356 (setq icol
(+ icol fortran-do-indent
)))
1358 "\\(structure\\|union\\|map\\|interface\\)\
1359 \\b[ \t]*[^ \t=(a-z]")
1360 (setq icol
(+ icol fortran-structure-indent
)))
1361 ((and (looking-at fortran-end-prog-re1
)
1362 (fortran-check-end-prog-re))
1363 ;; Previous END resets indent to minimum.
1364 (setq icol fortran-minimum-statement-indent
)))))
1367 (cond ((looking-at "[ \t]*$"))
1368 ;; Check for directive before comment, so as not to indent.
1369 ((looking-at fortran-directive-re
)
1370 (setq fortran-minimum-statement-indent
0 icol
0))
1371 ((looking-at fortran-comment-line-start-skip
)
1372 (cond ((eq fortran-comment-indent-style
'relative
)
1373 (setq icol
(+ icol fortran-comment-line-extra-indent
)))
1374 ((eq fortran-comment-indent-style
'fixed
)
1375 (setq icol
(+ fortran-minimum-statement-indent
1376 fortran-comment-line-extra-indent
))))
1377 (setq fortran-minimum-statement-indent
0))
1378 ((or (looking-at (concat "[ \t]*"
1380 fortran-continuation-string
)))
1381 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1382 (skip-chars-forward " \t")
1383 ;; Do not introduce extra whitespace into a broken string.
1385 (if (fortran-is-in-string-p (point))
1387 (+ icol fortran-continuation-indent
))))
1389 ((and fortran-check-all-num-for-matching-do
1390 (looking-at "[ \t]*[0-9]+")
1391 (fortran-check-for-matching-do))
1392 (setq icol
(- icol fortran-do-indent
)))
1394 (skip-chars-forward " \t0-9")
1395 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1396 (setq icol
(- icol fortran-if-indent
)))
1397 ((looking-at "else\\(if\\)?\\b")
1398 (setq icol
(- icol fortran-if-indent
)))
1399 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1400 (setq icol
(- icol fortran-if-indent
)))
1401 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1402 (setq icol
(- icol fortran-if-indent
)))
1403 ((and (looking-at "continue\\b")
1404 (fortran-check-for-matching-do))
1405 (setq icol
(- icol fortran-do-indent
)))
1406 ((looking-at "end[ \t]*do\\b")
1407 (setq icol
(- icol fortran-do-indent
)))
1408 ((looking-at "end[ \t]*\
1409 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1410 (setq icol
(- icol fortran-structure-indent
)))
1411 ((and (looking-at fortran-end-prog-re1
)
1412 (fortran-check-end-prog-re)
1413 (not (= icol fortran-minimum-statement-indent
)))
1414 (message "Warning: `end' not in column %d. Probably\
1415 an unclosed block." fortran-minimum-statement-indent
))))))
1416 (max fortran-minimum-statement-indent icol
)))
1419 (defun fortran-current-line-indentation ()
1420 "Indentation of current line, ignoring Fortran line number or continuation.
1421 This is the column position of the first non-whitespace character
1422 aside from the line number and/or column 5/8 line-continuation character.
1423 For comment lines, returns indentation of the first
1424 non-indentation text within the comment."
1427 (cond ((looking-at fortran-comment-line-start-skip
)
1428 (goto-char (match-end 0))
1430 (if (stringp fortran-comment-indent-char
)
1431 fortran-comment-indent-char
1432 (char-to-string fortran-comment-indent-char
))))
1433 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1434 (goto-char (match-end 0)))
1436 ;; Move past line number.
1437 (skip-chars-forward "[ \t0-9]")))
1438 ;; Move past whitespace.
1439 (skip-chars-forward " \t")
1442 (defun fortran-indent-to-column (col)
1443 "Indent current line to column COL.
1444 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1445 line, and this continuation character is retained on indentation;
1446 2) If `fortran-continuation-string' is the first non-whitespace
1447 character, this is a continuation line;
1448 3) A non-continuation line which has a number as the first
1449 non-whitespace character is a numbered line.
1450 4) A TAB followed by a digit indicates a continuation line."
1453 (if (looking-at fortran-comment-line-start-skip
)
1454 (if fortran-comment-indent-style
1455 (let* ((char (if (stringp fortran-comment-indent-char
)
1456 (aref fortran-comment-indent-char
0)
1457 fortran-comment-indent-char
))
1458 (chars (string ? ?
\t char
)))
1459 (goto-char (match-end 0))
1460 (skip-chars-backward chars
)
1461 (delete-region (point) (progn (skip-chars-forward chars
)
1463 (insert-char char
(- col
(current-column)))))
1464 (if (looking-at "\t[1-9]")
1465 (if indent-tabs-mode
1466 (goto-char (match-end 0))
1469 (insert fortran-continuation-string
))
1470 (if (looking-at " \\{5\\}[^ 0\n]")
1471 (if indent-tabs-mode
1472 (progn (delete-char 6)
1473 (insert ?
\t (fortran-numerical-continuation-char) 1))
1475 (delete-horizontal-space)
1476 ;; Put line number in columns 0-4, or
1477 ;; continuation character in column 5.
1479 ((looking-at (regexp-quote fortran-continuation-string
))
1480 (if indent-tabs-mode
1483 (if indent-tabs-mode
1484 fortran-minimum-statement-indent-tab
1485 fortran-minimum-statement-indent-fixed
))
1487 (insert-char (fortran-numerical-continuation-char) 1))
1490 ((looking-at "[0-9]+")
1491 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1492 (if (< extra-space
0)
1493 (message "Warning: line number exceeds 5-digit limit.")
1494 (indent-to (min fortran-line-number-indent extra-space
))))
1495 (skip-chars-forward "0-9")))))
1496 ;; Point is now after any continuation character or line number.
1497 ;; Put body of statement where specified.
1498 (delete-horizontal-space)
1500 ;; Indent any comment following code on the same line.
1501 (when (fortran-find-comment-start-skip)
1502 (goto-char (match-beginning 0))
1503 (unless (= (current-column) (fortran-comment-indent))
1504 (delete-horizontal-space)
1505 (indent-to (fortran-comment-indent)))))))
1507 (defun fortran-line-number-indented-correctly-p ()
1508 "Return t if current line's line number is correctly indented.
1509 Do not call if there is no line number."
1512 (skip-chars-forward " \t")
1513 (and (<= (current-column) fortran-line-number-indent
)
1514 (or (= (current-column) fortran-line-number-indent
)
1515 (progn (skip-chars-forward "0-9")
1516 (= (current-column) 5))))))
1518 (defun fortran-check-for-matching-do ()
1519 "When called from a numbered statement, return t if matching DO is found.
1520 Otherwise return nil."
1521 (let ((case-fold-search t
)
1525 (when (looking-at "[ \t]*[0-9]+")
1526 (skip-chars-forward " \t")
1527 (skip-chars-forward "0") ; skip past leading zeros
1529 (buffer-substring (point) (progn
1530 (skip-chars-forward "0-9")
1536 (and (re-search-backward
1538 "\\(^[ \t0-9]*do[ \t]*0*"
1539 charnum
"\\b\\)\\|" "\\(^[ \t]*0*"
1543 (concat "^[ \t0-9]*do[ \t]*0*"
1546 (defun fortran-find-comment-start-skip (&optional all
)
1547 "Move to past `comment-start-skip' found on current line.
1548 Return non-nil if `comment-start-skip' found, nil if not.
1549 If ALL is nil, only match comments that start in column > 0."
1550 ;; Hopefully at some point we can just use the line below! -stef
1551 ;; (comment-search-forward (line-end-position) t))
1552 (when (or all comment-start-skip
)
1554 (css (if comment-start-skip
1555 (concat fortran-comment-line-start-skip
1556 "\\|" comment-start-skip
)
1557 fortran-comment-line-start-skip
)))
1558 (when (re-search-forward css
(line-end-position) t
)
1559 (if (and (or all
(> (match-beginning 0) (line-beginning-position)))
1560 (or (save-match-data
1561 (not (fortran-is-in-string-p (match-beginning 0))))
1562 ;; Recurse for rest of line.
1563 (fortran-find-comment-start-skip all
)))
1568 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1569 ;; Test if TAB format continuation lines work.
1570 (defun fortran-is-in-string-p (where)
1571 "Return non-nil iff WHERE (a buffer position) is inside a Fortran string."
1575 ((bolp) nil
) ; bol is never inside a string
1576 ((save-excursion ; comment lines too
1578 (looking-at fortran-comment-line-start-skip
)) nil
)
1579 (t (let ((parse-state '(0 nil nil nil nil nil
0))
1580 (quoted-comment-start (if comment-start
1581 (regexp-quote comment-start
)))
1583 parse-limit end-of-line
)
1584 ;; Move to start of current statement.
1585 (fortran-next-statement)
1586 (fortran-previous-statement)
1587 ;; Now parse up to WHERE.
1589 (if (or ;; Skip to next line if:
1591 (looking-at fortran-comment-line-start-skip
)
1592 ;; - at end of line?
1594 ;; - not in a string and after comment-start?
1595 (and (not (nth 3 parse-state
))
1597 (equal comment-start
1598 (char-to-string (preceding-char)))))
1599 (if (> (forward-line) 0)
1600 (setq not-done nil
))
1602 ;; If we are at beginning of code line, skip any
1603 ;; whitespace, labels and tab continuation markers.
1604 (if (bolp) (skip-chars-forward " \t0-9"))
1605 ;; If we are in column <= 5 now, check for continuation char.
1606 (cond ((= 5 (current-column)) (forward-char 1))
1607 ((and (< (current-column) 5)
1608 (equal fortran-continuation-string
1609 (char-to-string (following-char)))
1611 ;; Find out parse-limit from here.
1612 (setq end-of-line
(line-end-position))
1613 (setq parse-limit
(min where end-of-line
))
1614 ;; Parse max up to comment-start, if non-nil and in current line.
1617 (if (re-search-forward quoted-comment-start end-of-line t
)
1618 (setq parse-limit
(min (point) parse-limit
)))))
1619 ;; Now parse if still in limits.
1620 (if (< (point) where
)
1621 (setq parse-state
(parse-partial-sexp
1622 (point) parse-limit nil nil parse-state
))
1623 (setq not-done nil
))))
1625 (nth 3 parse-state
))))))
1627 ;; From old version.
1628 (defalias 'fortran-auto-fill-mode
'auto-fill-mode
)
1630 (defun fortran-fill ()
1631 "Fill the current line at an appropriate point(s)."
1632 (let* ((auto-fill-function #'fortran-auto-fill
)
1634 (bol (line-beginning-position))
1635 (eol (line-end-position))
1636 (bos (min eol
(+ bol
(fortran-current-line-indentation))))
1640 ;; OK to break quotes on comment lines.
1641 (unless (looking-at fortran-comment-line-start-skip
)
1642 (let (fcpoint start
)
1643 (move-to-column fill-column
)
1644 (when (fortran-is-in-string-p (setq fcpoint
(point)))
1646 (re-search-backward "\\S\"\\s\"\\S\"" bol t
)
1648 (if fortran-break-before-delimiters
1651 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t
)
1653 ;; If the current string is longer than 72 - 6 chars,
1654 ;; break it at the fill column (else infinite loop).
1655 (if (> (- (point) start
)
1656 (- fill-column
6 fortran-continuation-indent
))
1659 ;; Decide where to split the line. If a position for a quoted
1660 ;; string was found above then use that, else break the line
1661 ;; before the last delimiter.
1662 ;; Delimiters are whitespace, commas, and operators.
1663 ;; Will break before a pair of *'s.
1667 (move-to-column (1+ fill-column
))
1668 ;; GM Make this a defcustom as in f90-mode? Add ", (?
1669 (skip-chars-backward "^ \t\n,'+-/*=)"
1670 ;;; (if fortran-break-before-delimiters
1671 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1673 (when (<= (point) (1+ bos
))
1674 (move-to-column (1+ fill-column
))
1675 ;; What is this doing???
1676 (or (re-search-forward "[\t\n,'+-/*)=]" eol t
)
1679 (re-search-forward "[ \t]" opoint t
)
1681 (if (looking-at "\\s\"")
1683 (skip-chars-backward " \t\*")))
1684 (if fortran-break-before-delimiters
1687 ;; If we are in an in-line comment, don't break unless the
1688 ;; line of code is longer than it should be. Otherwise
1689 ;; break the line at the column computed above.
1691 ;; Need to use fortran-find-comment-start-skip to make sure that
1692 ;; quoted !'s don't prevent a break.
1693 (when (and (save-excursion
1695 (if (not (fortran-find-comment-start-skip))
1697 (goto-char (match-beginning 0))
1698 (>= (point) fill-point
)))
1700 (goto-char fill-point
)
1705 (min (1+ fill-column
)
1706 (+ (fortran-calculate-indent)
1707 fortran-continuation-indent
))))
1708 (goto-char fill-point
)
1709 (fortran-break-line)
1712 (defun fortran-break-line ()
1713 "Call `fortran-split-line'. Joins continuation lines first, then refills."
1714 (let ((opoint (point))
1715 (bol (line-beginning-position))
1718 (if (fortran-find-comment-start-skip)
1719 (delete-and-extract-region
1720 (match-beginning 0) (line-end-position))))))
1721 ;; Forward line 1 really needs to go to next non white line.
1722 (if (save-excursion (forward-line)
1723 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1726 (delete-region (point) (match-end 0))
1727 (delete-horizontal-space)
1729 (fortran-split-line))
1734 (delete-horizontal-space)
1735 (indent-to (fortran-comment-indent))
1736 (insert comment-string
)))))
1738 (defun fortran-analyze-file-format ()
1739 "Return nil if fixed format is used, t if TAB formatting is used.
1740 Use `fortran-tab-mode-default' if no non-comment statements are found
1741 before the end or in the first `fortran-analyze-depth' lines."
1744 (goto-char (point-min))
1747 (eq (char-after) ?
\t)
1748 (looking-at " \\{6\\}")
1749 (> i fortran-analyze-depth
)))
1753 ((eq (char-after) ?
\t) t
)
1754 ((looking-at " \\{6\\}") nil
)
1755 (t fortran-tab-mode-default
)))))
1757 (defun fortran-fill-paragraph (&optional justify
)
1758 "Fill surrounding comment block as paragraphs, else fill statement.
1759 Intended as the value of `fill-paragraph-function'.
1760 A comment block is filled by calling `fill-comment-paragraph' with
1761 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
1762 Always returns non-nil (to prevent `fill-paragraph' being called)."
1764 (or (fill-comment-paragraph justify
)
1765 (fortran-fill-statement)
1768 (defun fortran-fill-statement ()
1769 "Fill a Fortran statement up to `fill-column'."
1771 (let ((auto-fill-function #'fortran-auto-fill
))
1772 (unless (save-excursion
1774 (or (looking-at "[ \t]*$")
1775 (looking-at fortran-comment-line-start-skip
)
1776 (and comment-start-skip
1777 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1779 ;; Find beginning of statement.
1780 (fortran-next-statement)
1781 (fortran-previous-statement)
1782 ;; Re-indent initially.
1783 (fortran-indent-line)
1784 ;; Replace newline plus continuation field plus indentation with
1788 (fortran-remove-continuation)))
1789 (fortran-previous-statement)))
1790 (fortran-indent-line)))
1792 (defun fortran-strip-sequence-nos (&optional do-space
)
1793 "Delete all text in column 72 and up (assumed to be sequence numbers).
1794 Normally also deletes trailing whitespace after stripping such text.
1795 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
1798 (goto-char (point-min))
1799 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t
)
1800 (replace-match "" nil nil nil
1)
1801 (unless do-space
(delete-horizontal-space)))))
1803 ;; This code used to live in add-log.el, but this is a better place
1805 (defun fortran-current-defun ()
1806 "Function to use for `add-log-current-defun-function' in Fortran mode."
1808 ;; We must be inside function body for this to work.
1809 (fortran-beginning-of-subprogram)
1810 (let ((case-fold-search t
)) ; case-insensitive
1811 ;; Search for fortran subprogram start.
1812 (if (re-search-forward
1813 (concat "^[ \t]*\\(program\\|subroutine\\|function"
1814 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
1815 "\\(block[ \t]*data\\)\\)")
1816 (save-excursion (fortran-end-of-subprogram)
1819 (or (match-string-no-properties 2)
1821 ;; Move to EOL or before first left paren.
1822 (if (re-search-forward "[(\n]" nil t
)
1823 (progn (backward-char)
1824 (skip-chars-backward " \t"))
1826 ;; Use the name preceding that.
1827 (buffer-substring-no-properties (point) (progn (backward-sexp)
1833 ;;; fortran.el ends here