;; This mode is documented in the Emacs manual.
;;
;; Note that it is for editing Fortran77 or Fortran90 fixed source
-;; form. For editing Fortran90 free format source, use `f90-mode'
+;; form. For editing Fortran 90 free format source, use `f90-mode'
;; (f90.el).
;;; History:
;;; Code:
-;; Todo:
+;; Todo:
+;; * Tidy it all up! (including renaming non-`fortran' prefixed
+;; functions).
;; * Implement insertion and removal of statement continuations in
;; mixed f77/f90 style, with the first `&' past column 72 and the
;; second in column 6.
-;; * Support other f90-style stuff grokked by GNU Fortran.
+;; * Support any other extensions to f77 grokked by GNU Fortran.
+;; * Change fontification to use font-lock-syntactic-keywords for
+;; fixed-form comments. (Done, but doesn't work properly with
+;; lazy-lock in pre-20.4.)
(require 'easymenu)
((string= "\"" match)
(re-search-forward "\\([^\"\n]*\"?\\)" limit)))))
-(let ((comment-chars "c!*")
+(let ((comment-chars "c!*d") ; `d' for `debugging' comments
(fortran-type-types
; (eval-when-compile
; (regexp-opt
(list
;;
;; Fontify all type specifiers (must be first; see below).
- (cons (concat "\\<\\(" fortran-type-types "\\)\\>")
+ (cons (concat "\\<\\(" fortran-type-types "\\)\\>")
'font-lock-type-face)
;;
;; Fontify all builtin keywords (except logical, do
3)
;; Un-named block data
(list nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
- "imenu generic expression for `imenu-default-create-index-function'.")
+ "Imenu generic expression for `imenu-default-create-index-function'.")
(defvar fortran-mode-map ()
"Keymap used in Fortran mode.")
Variables controlling indentation style and extra features:
- comment-start
+ `comment-start'
Normally nil in Fortran mode. If you want to use comments
starting with `!', set this to the string \"!\".
- fortran-do-indent
+ `fortran-do-indent'
Extra indentation within do blocks. (default 3)
- fortran-if-indent
+ `fortran-if-indent'
Extra indentation within if blocks. (default 3)
- fortran-structure-indent
+ `fortran-structure-indent'
Extra indentation within structure, union, map and interface blocks.
(default 3)
- fortran-continuation-indent
+ `fortran-continuation-indent'
Extra indentation applied to continuation statements. (default 5)
- fortran-comment-line-extra-indent
- Amount of extra indentation for text within full-line comments. (default 0)
- fortran-comment-indent-style
+ `fortran-comment-line-extra-indent'
+ Amount of extra indentation for text within full-line comments. (default 0)
+ `fortran-comment-indent-style'
nil means don't change indentation of text in full-line comments,
fixed means indent that text at `fortran-comment-line-extra-indent' beyond
the value of `fortran-minimum-statement-indent-fixed' (for fixed
relative means indent at `fortran-comment-line-extra-indent' beyond the
indentation for a line of code.
(default 'fixed)
- fortran-comment-indent-char
+ `fortran-comment-indent-char'
Single-character string to be inserted instead of space for
full-line comment indentation. (default \" \")
- fortran-minimum-statement-indent-fixed
- Minimum indentation for Fortran statements in fixed format mode. (def.6)
- fortran-minimum-statement-indent-tab
- Minimum indentation for Fortran statements in TAB format mode. (default 9)
- fortran-line-number-indent
+ `fortran-minimum-statement-indent-fixed'
+ Minimum indentation for Fortran statements in fixed format mode. (def.6)
+ `fortran-minimum-statement-indent-tab'
+ Minimum indentation for Fortran statements in TAB format mode. (default 9)
+ `fortran-line-number-indent'
Maximum indentation for line numbers. A line number will get
less than this much indentation if necessary to avoid reaching
column 5. (default 1)
- fortran-check-all-num-for-matching-do
+ `fortran-check-all-num-for-matching-do'
Non-nil causes all numbered lines to be treated as possible \"continue\"
statements. (default nil)
- fortran-blink-matching-if
+ `fortran-blink-matching-if'
Non-nil causes \\[fortran-indent-line] on an ENDIF statement to blink on
matching IF. Also, from an ENDDO statement, blink on matching DO [WHILE]
statement. (default nil)
- fortran-continuation-string
+ `fortran-continuation-string'
Single-character string to be inserted in column 5 of a continuation
line. (default \"$\")
- fortran-comment-region
+ `fortran-comment-region'
String inserted by \\[fortran-comment-region] at start of each line in
region. (default \"c$$$\")
- fortran-electric-line-number
+ `fortran-electric-line-number'
Non-nil causes line number digits to be moved to the correct column
as typed. (default t)
- fortran-break-before-delimiters
+ `fortran-break-before-delimiters'
Non-nil causes `fortran-fill' to break lines before delimiters.
(default t)
(make-local-variable 'fortran-minimum-statement-indent-fixed)
(make-local-variable 'fortran-minimum-statement-indent-tab)
(make-local-variable 'fortran-column-ruler-fixed)
- (make-local-variable 'fortran-column-ruler-tab)
+ (make-local-variable 'fortran-column-ruler-tab)
(setq fortran-tab-mode-string " TAB-format")
(setq indent-tabs-mode (fortran-analyze-file-format))
(setq imenu-case-fold-search t)
"end\
\\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
\\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?")
-
(defvar fortran-end-prog-re
(concat "^[ \t0-9]*" fortran-end-prog-re1)
"Regexp possibly marking subprogram end.")
(mark-fortran-subprogram)
(narrow-to-region (region-beginning)
(region-end))))
+
+(defmacro fortran-with-subprogram-narrowing (&rest forms)
+ "Execute FORMS with buffer temporarily narrowed to current subprogram.
+Doesn't push a mark."
+ `(save-restriction
+ (save-excursion
+ (narrow-to-region (progn
+ (beginning-of-fortran-subprogram)
+ (point))
+ (progn
+ (end-of-fortran-subprogram)
+ (point))))
+ ,@forms))
\f
(defun fortran-blink-matching-if ()
- ;; From a Fortran ENDIF statement, blink the matching IF statement.
+ "From an ENDIF statement, blink the matching IF statement."
(let ((top-of-window (window-start))
(endif-point (point))
(case-fold-search t)
(goto-char endif-point))))))
(defun fortran-blink-matching-do ()
- ;; From a Fortran ENDDO statement, blink on the matching DO or DO WHILE
- ;; statement. This is basically copied from fortran-blink-matching-if.
+ "From an ENDDO statement, blink the matching DO or DO WHILE statement."
+ ;; This is basically copied from fortran-blink-matching-if.
(let ((top-of-window (window-start))
(enddo-point (point))
(case-fold-search t)
(goto-char do-point)))))
(defun fortran-end-do ()
- ;; Search forward for first unmatched ENDDO. Return point or nil.
+ "Search forward for first unmatched ENDDO.
+Return point or nil."
(let ((case-fold-search t))
(if (save-excursion (beginning-of-line)
(skip-chars-forward " \t0-9")
(while (and (not (= count 0))
(not (eq (fortran-next-statement) 'last-statement))
;; Keep local to subprogram
- (not (looking-at fortran-end-prog-re)))
+ (not (and (looking-at fortran-end-prog-re)
+ (fortran-check-end-prog-re))))
(skip-chars-forward " \t0-9")
(cond ((looking-at "end[ \t]*do\\b")
(point)))))))
(defun fortran-beginning-do ()
- ;; Search backwards for first unmatched DO [WHILE]. Return point or nil.
+ "Search backwards for first unmatched DO [WHILE].
+Return point or nil."
(let ((case-fold-search t))
(if (save-excursion (beginning-of-line)
(skip-chars-forward " \t0-9")
(while (and (not (= count 0))
(not (eq (fortran-previous-statement) 'first-statement))
;; Keep local to subprogram
- (not (looking-at fortran-end-prog-re)))
+ (not (and (looking-at fortran-end-prog-re)
+ (fortran-check-end-prog-re))))
(skip-chars-forward " \t0-9")
(cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
(defvar fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
(defun fortran-end-if ()
- ;; Search forwards for first unmatched ENDIF. Return point or nil.
+ "Search forwards for first unmatched ENDIF.
+Return point or nil."
(let ((case-fold-search t))
(if (save-excursion (beginning-of-line)
(skip-chars-forward " \t0-9")
(while (and (not (= count 0))
(not (eq (fortran-next-statement) 'last-statement))
;; Keep local to subprogram.
- (not (looking-at fortran-end-prog-re)))
+ (not (and (looking-at fortran-end-prog-re)
+ (fortran-check-end-prog-re))))
(skip-chars-forward " \t0-9")
(cond ((looking-at "end[ \t]*if\\b")
(point)))))))
(defun fortran-beginning-if ()
- ;; Search backwards for first unmatched IF-THEN. Return point or nil.
+ "Search backwards for first unmatched IF-THEN.
+Return point or nil."
(let ((case-fold-search t))
(if (save-excursion
;; May be sitting on multi-line if-then statement, first move to
(while (and (not (= count 0))
(not (eq (fortran-previous-statement) 'first-statement))
;; Keep local to subprogram.
- (not (looking-at fortran-end-prog-re)))
+ (not (and (looking-at fortran-end-prog-re)
+ (fortran-check-end-prog-re))))
(skip-chars-forward " \t0-9")
(cond ((looking-at fortran-if-start-re)
(defun fortran-indent-new-line ()
"Reindent the current Fortran line, insert a newline and indent the newline.
-An abbrev before point is expanded if `abbrev-mode' is non-nil."
+An abbrev before point is expanded if variable `abbrev-mode' is non-nil."
(interactive)
(if abbrev-mode (expand-abbrev))
(save-excursion
((looking-at
"\\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
(setq icol (+ icol fortran-structure-indent)))
- ((looking-at fortran-end-prog-re1)
+ ((and (looking-at fortran-end-prog-re1)
+ (fortran-check-end-prog-re))
;; Previous END resets indent to minimum
(setq icol fortran-minimum-statement-indent))))))
(save-excursion
\\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
(setq icol (- icol fortran-structure-indent)))
((and (looking-at fortran-end-prog-re1)
+ (fortran-check-end-prog-re)
(not (= icol fortran-minimum-statement-indent)))
(message "Warning: `end' not in column %d. Probably\
an unclosed block." fortran-minimum-statement-indent))))))
(progn (skip-chars-forward "0-9")
(point))))
(beginning-of-line)
- (and (re-search-backward
- (concat "\\(" fortran-end-prog-re "\\)\\|"
- "\\(^[ \t0-9]*do[ \t]*0*" charnum "\\b\\)\\|"
- "\\(^[ \t]*0*" charnum "\\b\\)")
- nil t)
- (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum))))))))
+ (fortran-with-subprogram-narrowing
+ (and (re-search-backward
+ (concat "\\(^[ \t0-9]*do[ \t]*0*" charnum "\\b\\)\\|"
+ "\\(^[ \t]*0*" charnum "\\b\\)")
+ nil t)
+ (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum)))))))))
(defun fortran-find-comment-start-skip ()
"Move to past `comment-start-skip' found on current line.
(fortran-indent-line)
;; Replace newline plus continuation field plus indentation with
;; single space.
- (while (progn
+ (while (progn
(forward-line)
(fortran-remove-continuation)))
(fortran-previous-statement)))