;;; f90.el --- Fortran-90 mode (free format)
-;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
;; Author: Torbj\"orn Einarsson <T.Einarsson@clab.ericsson.se>
-;; Last Change: Aug. 12, 1996
+;; Last Change: May 29 1997
;; Keywords: fortran, f90, languages
;; This file is part of GNU Emacs.
"Address of mailing list for F90 mode bugs.")
;; User options
-(defvar f90-do-indent 3
- "*Extra indentation applied to DO blocks.")
-(defvar f90-if-indent 3
- "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks.")
+(defgroup f90 nil
+ "Fortran-90 mode"
+ :group 'fortran)
-(defvar f90-type-indent 3
- "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks.")
+(defgroup f90-indent nil
+ "Fortran-90 indentation"
+ :prefix "f90-"
+ :group 'f90)
-(defvar f90-program-indent 2
- "*Extra indentation applied to PROGRAM/MODULE/SUBROUTINE/FUNCTION blocks.")
-(defvar f90-continuation-indent 5
- "*Extra indentation applied to F90 continuation lines.")
+(defcustom f90-do-indent 3
+ "*Extra indentation applied to DO blocks."
+ :type 'integer
+ :group 'f90-indent)
-(defvar f90-comment-region "!!$"
- "*String inserted by \\[f90-comment-region]\
- at start of each line in region.")
+(defcustom f90-if-indent 3
+ "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
+ :type 'integer
+ :group 'f90-indent)
-(defvar f90-indented-comment-re "!"
- "*Regexp saying which comments to be indented like code.")
+(defcustom f90-type-indent 3
+ "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks."
+ :type 'integer
+ :group 'f90-indent)
-(defvar f90-directive-comment-re "!hpf\\$"
- "*Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented.")
+(defcustom f90-program-indent 2
+ "*Extra indentation applied to PROGRAM/MODULE/SUBROUTINE/FUNCTION blocks."
+ :type 'integer
+ :group 'f90-indent)
-(defvar f90-beginning-ampersand t
- "*t makes automatic insertion of \& at beginning of continuation line.")
+(defcustom f90-continuation-indent 5
+ "*Extra indentation applied to F90 continuation lines."
+ :type 'integer
+ :group 'f90-indent)
-(defvar f90-smart-end 'blink
+(defcustom f90-comment-region "!!$"
+ "*String inserted by \\[f90-comment-region]\
+ at start of each line in region."
+ :type 'string
+ :group 'f90-indent)
+
+(defcustom f90-indented-comment-re "!"
+ "*Regexp saying which comments to be indented like code."
+ :type 'regexp
+ :group 'f90-indent)
+
+(defcustom f90-directive-comment-re "!hpf\\$"
+ "*Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
+ :type 'regexp
+ :group 'f90-indent)
+
+(defcustom f90-beginning-ampersand t
+ "*t makes automatic insertion of \& at beginning of continuation line."
+ :type 'boolean
+ :group 'f90)
+
+(defcustom f90-smart-end 'blink
"*From an END statement, check and fill the end using matching block start.
Allowed values are 'blink, 'no-blink, and nil, which determine
-whether to blink the matching beginning.")
+whether to blink the matching beginning."
+ :type '(choice (const blink) (const no-blink) (const nil))
+ :group 'f90)
-(defvar f90-break-delimiters "[-+\\*/><=,% \t]"
- "*Regexp holding list of delimiters at which lines may be broken.")
+(defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
+ "*Regexp holding list of delimiters at which lines may be broken."
+ :type 'regexp
+ :group 'f90)
-(defvar f90-break-before-delimiters t
- "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters.")
+(defcustom f90-break-before-delimiters t
+ "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
+ :type 'boolean
+ :group 'f90)
-(defvar f90-auto-keyword-case nil
+(defcustom f90-auto-keyword-case nil
"*Automatic case conversion of keywords.
- The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil")
+ The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil"
+ :type '(choice (const downcase-word) (const upcase-word)
+ (const capitalize-word) (const nil))
+ :group 'f90)
-(defvar f90-leave-line-no nil
- "*If nil, left-justify linenumbers.")
+(defcustom f90-leave-line-no nil
+ "*If nil, left-justify linenumbers."
+ :type 'boolean
+ :group 'f90)
-(defvar f90-startup-message t
- "*Non-nil displays a startup message when F90 mode is first called.")
+(defcustom f90-startup-message t
+ "*Non-nil displays a startup message when F90 mode is first called."
+ :type 'boolean
+ :group 'f90)
(defconst f90-keywords-re
;;("allocate" "allocatable" "assign" "assignment" "backspace" "block"
;; Variable declarations (avoid the real function call)
'("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\([^!\n]*\\)"
(1 font-lock-type-face) (4 font-lock-variable-name-face))
- ;; do, if and select constructs
- '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)?"
- (1 font-lock-keyword-face) (3 font-lock-reference-face nil t))
- '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
- (2 font-lock-reference-face nil t) (3 font-lock-keyword-face))
+ ;; do, if, select, where, and forall constructs
+ '("\\<\\(end[ \t]*\\(do\\|if\\|select\\|forall\\|where\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)?"
+ (1 font-lock-keyword-face) (3 font-lock-constant-face nil t))
+ '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
+ (2 font-lock-constant-face nil t) (3 font-lock-keyword-face))
;; implicit declaration
'("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>" (1 font-lock-keyword-face) (2 font-lock-type-face))
- '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/" (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
- '("\\<\\(where\\|forall\\)[ \t]*(" . 1)
- "\\<e\\(lse\\([ \t]*if\\|where\\)?\\|nd[ \t]*\\(where\\|forall\\)\\)\\>"
+ '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/" (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
+ "\\<else\\([ \t]*if\\|where\\)?\\>"
"\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
'("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>"
- (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
+ (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
'("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
'("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
- (1 font-lock-keyword-face) (2 font-lock-reference-face))
+ (1 font-lock-keyword-face) (2 font-lock-constant-face))
;; line numbers (lines whose first character after number is letter)
- '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-reference-face t))))
+ '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t))))
"Highlights declarations, do-loops and other constructions")
(defvar f90-font-lock-keywords-3
(define-key f90-mode-map "\C-c\C-p" 'f90-previous-statement)
(define-key f90-mode-map "\C-c\C-n" 'f90-next-statement)
(define-key f90-mode-map "\C-c\C-w" 'f90-insert-end)
- (define-key f90-mode-map "\t" 'f90-indent-line))
+ (define-key f90-mode-map "\t" 'f90-indent-line)
+ (define-key f90-mode-map "," 'f90-electric-insert)
+ (define-key f90-mode-map "+" 'f90-electric-insert)
+ (define-key f90-mode-map "-" 'f90-electric-insert)
+ (define-key f90-mode-map "*" 'f90-electric-insert)
+ (define-key f90-mode-map "/" 'f90-electric-insert))
;; menus
(setq normal-auto-fill-function 'f90-do-auto-fill)
(setq indent-tabs-mode nil)
;; Setting up things for font-lock
- (if (string-match "Xemacs" emacs-version)
+ (if (string-match "XEmacs" emacs-version)
(progn
(put 'f90-mode 'font-lock-keywords-case-fold-search t)
(if (and current-menubar
(setq font-lock-defaults '(f90-font-lock-keywords nil t))
;; Tell imenu how to handle f90.
+ (setq imenu-case-fold-search t)
(make-local-variable 'imenu-generic-expression)
(setq imenu-generic-expression f90-imenu-generic-expression))
(run-hooks 'f90-mode-hook)
(setq struct (f90-match-piece 3))
(if (looking-at "\\(\\sw+\\)[ \t]*\:")
(setq label (f90-match-piece 1)))
- (goto-char (scan-lists (point) 1 0))
+ (let ((pos (scan-lists (point) 1 0)))
+ (and pos (goto-char pos)))
(skip-chars-forward " \t")
(if (or (looking-at "then\\>")
(if (f90-line-continued)
(list struct label)))))))
(defsubst f90-looking-at-where-or-forall ()
- "Return (kind nil) if where/forall...end starts after point."
- (save-excursion
- (let (command)
- (if (looking-at "\\(where\\|forall\\)[ \t]*(")
- (progn
- (setq command (list (f90-match-piece 1) nil))
- (goto-char (scan-lists (point) 1 0))
- (skip-chars-forward " \t")
- (if (looking-at "\\(!\\|$\\)")
- command))))))
+ "Return (kind name) if a where or forall block starts after point.
+Name is nil if the statement has no label."
+ (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(where\\|forall\\)[ \t]*(.*)[ \t]*\\(!\\|$\\)")
+ (let (label
+ (struct (f90-match-piece 3)))
+ (if (looking-at "\\(\\sw+\\)[ \t]*\:")
+ (setq label (f90-match-piece 1)))
+ (list struct label))))
(defsubst f90-looking-at-type-like ()
"Return (kind name) at the start of a type/interface/block-data block.
((and f90-directive-comment-re
(looking-at f90-directive-comment-re)) 0)
((looking-at (regexp-quote f90-comment-region)) 0)
- ((looking-at f90-indented-comment-re)
+ ((and (looking-at f90-indented-comment-re)
+ ;; Don't attempt to indent trailing comment as code.
+ (save-excursion
+ (skip-chars-backward " \t")
+ (bolp)))
(f90-calculate-indent))
(t (skip-chars-backward " \t")
(max (if (bolp) 0 (1+ (current-column))) comment-column))))
(let ((eol (f90-get-end-of-line)))
(save-excursion
(not (or (looking-at "end")
- (looking-at "\\(do\\|if\\|else\\|select[ \t]*case\\|\
-case\\|where\\|forall\\)\\>")
+ (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
+\\|select[ \t]*case\\|case\\|where\\|forall\\)\\>")
(looking-at "\\(program\\|module\\|interface\\|\
block[ \t]*data\\)\\>")
(looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
(if f90-auto-keyword-case
(f90-change-keywords f90-auto-keyword-case bol eol))))))
\f
+(defun f90-electric-insert ()
+ (interactive)
+ "Calls f90-do-auto-fill at each operator insertion."
+ (self-insert-command 1)
+ (f90-update-line)
+ (if auto-fill-function (f90-do-auto-fill)))
+
(defun f90-get-correct-indent ()
"Get correct indent for a line starting with line number.
Does not check type and subprogram indentation."
(if (< (point) (marker-position pos))
(goto-char (marker-position pos)))
(if (not no-update) (f90-update-line))
- (if (and auto-fill-function
- (> (save-excursion (end-of-line) (current-column)) fill-column))
- (save-excursion (f90-do-auto-fill)))
+ (if auto-fill-function (f90-do-auto-fill))
(set-marker pos nil)))
(defun f90-indent-new-line ()
(if (not no-update) (f90-update-line))
(newline)
(if f90-beginning-ampersand (insert "&")))))
- (if (not no-update) (f90-indent-line)))
+ (f90-indent-line))
(defun f90-find-breakpoint ()
"From fill-column, search backward for break-delimiter."
(forward-char)))))
(defun f90-do-auto-fill ()
- "Break line if non-white characters beyond fill-column."
+ "Break line if non-white characters beyond fill-column. Also, update line. "
(interactive)
;; Break the line before or after the last delimiter (non-word char) if
;; position is beyond fill-column.
;; Will not break **, //, or => (specified by f90-no-break-re).
+ (f90-update-line)
(while (> (current-column) fill-column)
(let ((pos-mark (point-marker)))
(move-to-column fill-column)
"Fill every line in region by forward parsing. Join lines if possible."
(interactive "*r")
(let ((end-region-mark (make-marker))
- (f90-smart-end nil) (f90-auto-keyword-case nil) indent (go-on t)
- (af-function auto-fill-function) (auto-fill-function nil))
+ (f90-smart-end nil) (f90-auto-keyword-case nil) (go-on t)
+ (auto-fill-function nil))
(set-marker end-region-mark end-region)
(goto-char beg-region)
(while go-on
;; join as much as possible
- (while (f90-join-lines));
- (setq indent (+ (f90-current-indentation) f90-continuation-indent))
+ (while (f90-join-lines))
;; chop the line if necessary
(while (> (save-excursion (end-of-line) (current-column))
fill-column)
(move-to-column fill-column)
- (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
- (delete-horizontal-space)
- (f90-find-breakpoint)
- (f90-break-line 'no-update)
- (f90-indent-to indent 'no-line-no)))
+ (f90-find-breakpoint)
+ (f90-break-line 'no-update))
(setq go-on (and (< (point) (marker-position end-region-mark))
(zerop (forward-line 1))))
(setq f90-cache-position (point)))
- (setq auto-fill-function af-function)
(setq f90-cache-position nil)
(if (string-match "XEmacs" emacs-version)
(zmacs-deactivate-region)