]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/f90.el
(bdf-generate-font): New argument CHARSET. Give WIDTH
[gnu-emacs] / lisp / progmodes / f90.el
index aa6de50b3aa04479a088b3b3ab0ce98ea12a2d67..857fe7cfa4e17acc779cdcf7b35a4fb19b3e5764 100644 (file)
@@ -1,9 +1,9 @@
 ;;; 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>
-;; Created: Sep. 21, 1995
+;; Last Change: May 29 1997
 ;; Keywords: fortran, f90, languages
 
 ;; This file is part of GNU Emacs.
 ;;                        f90-leave-line-no  nil
 ;;                        f90-startup-message t
 ;;                        indent-tabs-mode nil
+;;                        f90-font-lock-keywords f90-font-lock-keywords-2
 ;;                  )
 ;;       ;;The rest is not default.
 ;;       (abbrev-mode 1)             ; turn on abbreviation mode
-;;       (f90-auto-fill-mode 1)      ; turn on auto-filling
 ;;       (turn-on-font-lock)         ; for highlighting
+;;       (f90-add-imenu-menu)        ; extra menu with functions etc.
 ;;       (if f90-auto-keyword-case   ; change case of all keywords on startup
 ;;           (f90-change-keywords f90-auto-keyword-case))
 ;;      ))
 ;; in your .emacs file (the shown values are the defaults). You can also
 ;; change the values of the lists f90-keywords etc.
 ;; The auto-fill and abbreviation minor modes are accessible from the menu,
-;; or by using M-x f90-auto-fill-mode and M-x abbrev-mode, respectively.
+;; or by using M-x auto-fill-mode and M-x abbrev-mode, respectively.
 
 ;; Remarks
 ;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
 ;;    mechanism for treating multi-line directives (continued by \ ).
 ;; 7) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
 ;;    You are urged to use f90-do loops (with labels if you wish).
+;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
 
 ;; List of user commands
 ;;   f90-previous-statement         f90-next-statement
 ;;   f90-indent-region    (can be called by calling indent-region)
 ;;   f90-indent-subprogram
 ;;   f90-break-line                 f90-join-lines
-;;   f90-auto-fill-mode
 ;;   f90-fill-region
 ;;   f90-insert-end
 ;;   f90-upcase-keywords            f90-upcase-region-keywords
 ;;   f90-downcase-keywords          f90-downcase-region-keywords
 ;;   f90-capitalize-keywords        f90-capitalize-region-keywords
+;;   f90-add-imenu-menu
+;;   f90-font-lock-1, f90-font-lock-2, f90-font-lock-3, f90-font-lock-4
 
 ;; Thanks to all the people who have tested the mode. Special thanks to Jens
 ;; Bloch Helmers for encouraging me to write this code, for creative
   "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-auto-hilit19 t
-  "*Automatic highlight of line at every indent or newline (for hilit19).")
+(defcustom f90-leave-line-no nil
+  "*If nil, left-justify linenumbers."
+  :type 'boolean
+  :group 'f90)
 
-(defvar f90-leave-line-no nil
-  "*If nil, left-justify linenumbers.")
-
-(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"
@@ -204,9 +246,9 @@ whether to blink the matching beginning.")
   ;;"elseif" "elsewhere" "end" "enddo" "endfile" "endif" "entry" "equivalence"
   ;;"exit" "external" "forall" "format" "function" "goto" "if" "implicit"
   ;;"include" "inquire" "integer" "intent" "interface" "intrinsic" "logical"
-  ;;"module" "namelist" "none" "nullify" "only" "open" "optional" "parameter"
+  ;;"module" "namelist" "none" "nullify" "only" "open" "operator" "optional" "parameter"
   ;;"pause" "pointer" "precision" "print" "private" "procedure" "program"
-  ;;"public" "read" "real" "recursive" "return" "rewind" "save" "select"
+  ;;"public" "read" "real" "recursive" "result" "return" "rewind" "save" "select"
   ;;"sequence" "stop" "subroutine" "target" "then" "type" "use" "where"
   ;;"while" "write")
   (concat
@@ -217,25 +259,28 @@ whether to blink the matching beginning.")
    "if\\)\\|try\\)\\|quivalence\\|x\\(it\\|ternal\\)\\)\\|f\\(or\\(all\\|"
    "mat\\)\\|unction\\)\\|goto\\|i\\(f\\|mplicit\\|n\\(clude\\|quire\\|t\\("
    "e\\(ger\\|nt\\|rface\\)\\|rinsic\\)\\)\\)\\|logical\\|module\\|n\\("
-   "amelist\\|one\\|ullify\\)\\|o\\(nly\\|p\\(en\\|tional\\)\\)\\|p\\(a\\("
+   "amelist\\|one\\|ullify\\)\\|o\\(nly\\|p\\(en\\|erator\\|tional\\)\\)\\|p\\(a\\("
    "rameter\\|use\\)\\|ointer\\|r\\(ecision\\|i\\(nt\\|vate\\)\\|o\\("
-   "cedure\\|gram\\)\\)\\|ublic\\)\\|re\\(a[dl]\\|cursive\\|turn\\|wind\\)\\|"
+   "cedure\\|gram\\)\\)\\|ublic\\)\\|re\\(a[dl]\\|cursive\\|sult\\|turn\\|wind\\)\\|"
    "s\\(ave\\|e\\(lect\\|quence\\)\\|top\\|ubroutine\\)\\|t\\(arget\\|hen\\|"
    "ype\\)\\|use\\|w\\(h\\(ere\\|ile\\)\\|rite\\)\\)\\>")
   "Regexp for F90 keywords.")
 
 (defconst f90-keywords-level-3-re
- ;; ("allocate" "allocatable" "assign" "assignment" "backspace" "close"
- ;; "deallocate" "endfile" "entry" "equivalence" "external" "inquire" "intent"
- ;; "intrinsic" "only" "open" "optional" "parameter" "pause" "pointer" "print"
- ;; "private" "public" "read" "recursive" "rewind" "save" "select" "sequence"
- ;; "target"  "write")
+  ;; ("allocate" "allocatable" "assign" "assignment" "backspace" "close"
+  ;; "deallocate" "dimension" "endfile" "entry" "equivalence" "external"
+  ;; "inquire" "intent" "intrinsic" "nullify" "only" "open" "operator"
+  ;; "optional" "parameter" "pause" "pointer" "print" "private" "public"
+  ;; "read" "recursive" "result" "rewind" "save" "select" "sequence"
+  ;; "target"  "write")
   (concat
    "\\<\\(a\\(llocat\\(able\\|e\\)\\|ssign\\(\\|ment\\)\\)\\|backspace\\|"
-   "close\\|deallocate\\|e\\(n\\(dfile\\|try\\)\\|quivalence\\|xternal\\)\\|"
-   "in\\(quire\\|t\\(ent\\|rinsic\\)\\)\\|o\\(nly\\|p\\(en\\|tional\\)\\)\\|"
+   "close\\|d\\(eallocate\\|imension\\)\\|e\\(n\\(dfile\\|try\\)\\|"
+   "quivalence\\|xternal\\)\\|"
+   "in\\(quire\\|t\\(ent\\|rinsic\\)\\)\\|nullify\\|"
+   "o\\(nly\\|p\\(en\\|erator\\|tional\\)\\)\\|"
    "p\\(a\\(rameter\\|use\\)\\|ointer\\|ri\\(nt\\|vate\\)\\|ublic\\)\\|re\\("
-   "ad\\|cursive\\|wind\\)\\|s\\(ave\\|e\\(lect\\|quence\\)\\)\\|target\\|"
+   "ad\\|cursive\\|sult\\|wind\\)\\|s\\(ave\\|e\\(lect\\|quence\\)\\)\\|target\\|"
    "write\\)\\>")
 "Keyword-regexp for font-lock level >= 3.")
 
@@ -322,98 +367,42 @@ whether to blink the matching beginning.")
 ;; Highlighting patterns
 
 (defvar f90-font-lock-keywords-1
-  (if (string-match "XEmacs" emacs-version)
-      (list                            ; XEmacs
-       '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>"
-        1 font-lock-keyword-face)
-       '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)"
-        3 font-lock-function-name-face)
-       '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>"
-        1 font-lock-keyword-face)
-       '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)"
-        2 font-lock-function-name-face nil t)
-       ;; Special highlighting of "module procedure foo-list"
-       '("\\<\\(module[ \t]*procedure\\)\\>" 1 font-lock-keyword-face t)
-       ;; Highlight definition of new type
-       '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
-        1 font-lock-keyword-face)
-       '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
-        3 font-lock-function-name-face)
-       "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
-    (list                              ; Emacs
-     '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)?"
-       (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
-     '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
-       (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
-     ;; Special highlighting of "module procedure foo-list"
-     '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face t))
-     ;; Highlight definition of new type
-     '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
-       (1 font-lock-keyword-face) (3 font-lock-function-name-face))
-     "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>"))
+  (list                                ; Emacs
+   '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)?"
+     (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
+   '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
+     (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
+   ;; Special highlighting of "module procedure foo-list"
+   '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face t))
+   ;; Highlight definition of new type
+   '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
+     (1 font-lock-keyword-face) (3 font-lock-function-name-face))
+   "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
   "This does fairly subdued highlighting of comments and function calls.")
 
 (defvar f90-font-lock-keywords-2
   (append f90-font-lock-keywords-1
-     (if (string-match "XEmacs" emacs-version)
-      (list                            ; XEmacs
+      (list
        ;; Variable declarations (avoid the real function call)
-       '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)"
-        1 font-lock-type-face)
-       '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\(.*\\)"
-        4 font-lock-doc-string-face)
-       ;; do, if and select constructs
-       '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>"
-        1 font-lock-keyword-face)
-       '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)"
-        3 font-lock-doc-string-face)
-       '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
-        2 font-lock-doc-string-face)
-       '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
-        3 font-lock-keyword-face)
-      ;; implicit declaration
-       '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>"
-        1 font-lock-keyword-face)
-       '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>"
-        2 font-lock-type-face)
-       '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
-        1 font-lock-keyword-face)
-       '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)\/"
-        2 font-lock-doc-string-face nil t)
-       '("\\<\\(where\\|forall\\)[ \t]*(" . 1)
-       "\\<e\\(lse\\([ \t]*if\\|where\\)?\\|nd[ \t]*\\(where\\|forall\\)\\)\\>"
-       "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
-       '("\\<\\(exit\\|cycle\\)\\>" 
-        1 font-lock-keyword-face)
-       '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)2\\>" 
-        2 font-lock-doc-string-face)
-       '("\\<\\(case\\)[ \t]+\\(default\\|(\\)" . 1)
-       '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
-        1 font-lock-keyword-face)
-       '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
-        2 font-lock-doc-string-face)
-       '("^[ \t]*\\([0-9]+\\)" 1 font-lock-doc-string-face t))
-      (list                            ; Emacs
-       ;; Variable declarations (avoid the real function call)
-       '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\(.*\\)"
+       '("^[ \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))
-       '("\\<\\(case\\)[ \t]+\\(default\\|(\\)" . 1)
+        (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))
-       '("^[ \t]*\\([0-9]+\\)" (1 font-lock-reference-face t)))))
+        (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-constant-face t))))
   "Highlights declarations, do-loops and other constructions")
 
 (defvar f90-font-lock-keywords-3
@@ -437,90 +426,6 @@ whether to blink the matching beginning.")
       f90-font-lock-keywords-2
   "*Default expressions to highlight in F90 mode.")
 
-;; hilit19 customization and expressions
-(defvar f90-face-string 'named-param "*Face for strings.")
-(defvar f90-face-comment 'comment "*Face for comments.")
-(defvar f90-face-decl 'include "*Face for declarations.")
-(defvar f90-face-prog 'defun "*Face for program blocks.")
-(defvar f90-face-label 'Tomato-bold "*Face for labels.")
-(defvar f90-face-type 'defun "*Face for type blocks.")
-(defvar f90-face-interface 'defun "*Face for interface blocks.")
-(defvar f90-face-contains 'defun "*Face for contains statement.")
-(defvar f90-face-do 'SteelBlue-bold "*Face for do-structure.")
-(defvar f90-face-if 'define "*Face for if-structure.")
-(defvar f90-face-select 'define "*Face for select-case structure.")
-(defvar f90-face-stop 'defun "*Face for stop and return.")
-(defvar f90-face-exit 'SteelBlue-bold "*Face for exit and cycle.")
-(defvar f90-face-keyword 'struct "*Face for keywords.")
-(defvar f90-face-intrinsics 'struct "*Face for intrinsic procedures.")
-;; Highlighting for HPF (High-Peformance Fortran)
-(defvar f90-face-hpf-procedures 'struct "*Face for hpf procedures.")
-(defvar f90-face-hpf-directives 'struct "*Face for hpf directives.")
-(defvar f90-face-hpf-keywords   'struct "*Face for hpf keywords.")
-
-(defvar f90-hilit-patterns
-  (list
-   ;; Allow for strings delimited by ' and by " and for multirow strings.
-   ;; A multi-row string includes &\n& (+ possible whitespace and comments)
-   (list (concat
-         "\\(\"[^\"\n]*\\(&[ \t]*\\(![^\n]*\\)?\n[ \t]*&[^\"\n]*\\)*\""
-         "\\|'[^'\n]*\\(&[ \t]*\\(![^\n]*\\)?\n[ \t]*&[^'\n]*\\)*'\\)")
-        nil f90-face-string)
-   (list "!" "$" f90-face-comment)
-   (list "\\(\\(real\\|integer\\|character\\|complex\\|logical\
-\\|type[ \t]*(\\sw+)\\).*\\)::" 1 f90-face-decl)
-   (list "implicit[ \t]*none" nil f90-face-decl)
-   (list "^[ \t]*\\(program\\|module\\)[ \t]+\\sw+" 1 f90-face-prog)
-   (list "^[ \t]*\\(program\\|module\\)[ \t]+\\(\\sw+\\)" 2 f90-face-label)
-   (list "\\(^.*\\(function\\|subroutine\\)\\)[ \t]+\\sw+" 1
-        f90-face-prog)
-   (list "^.*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)" 2
-        f90-face-label)
-   (list "^[ \t]*end[ \t]*\\(program\\|module\\|function\
-\\|subroutine\\|type\\)" nil f90-face-prog)
-   (list (concat "^[ \t]*end[ \t]*\\(program\\|module\\|function\\|"
-                "subroutine\\|type\\)[ \t]+\\(\\sw+\\)") 2 f90-face-label)
-   (list "^[ \t]*\\(type\\)[ \t]+\\sw+" 1 f90-face-type)
-   (list "^[ \t]*type[ \t]+\\(\\sw+\\)" 1 f90-face-label)
-   (list "^[ \t]*\\(type[ \t]*,[ \t]*\\(private\\|public\\)\\)[ \t]*::[ \t]*\\(\\sw+\\)" 1 f90-face-type)
-   (list "^[ \t]*\\(type[ \t]*,[ \t]*\\(private\\|public\\)\\)[ \t]*::[ \t]*\\(\\sw+\\)" 3 f90-face-label)
-   (list "^[ \t]*\\(end[ \t]*\\)?interface\\>" nil f90-face-interface)
-   (list "^[ \t]*contains\\>" nil f90-face-contains)
-   (list "^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(do\\([ \t]*while\\)?\\)\\>"
-        2 f90-face-do)
-   (list "^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*\\(do\\([ \t]*while\\)?\\)\\>" 1
-        f90-face-label)
-   (list "^[ \t]*\\(end[ \t]*do\\)\\>" 1 f90-face-do)
-   (list "^[ \t]*end[ \t]*do[ \t]+\\(\\sw+\\)" 1 f90-face-label)
-   (list "^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(if\\)\\>" 2 f90-face-if)
-   (list "^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*if\\>" 1 f90-face-label)
-   (list "^[ \t]*\\(end[ \t]*if\\)\\>" 1 f90-face-if)
-   (list "^[ \t]*end[ \t]*if[ \t]+\\(\\sw+\\)" 1 f90-face-label)
-   (list "^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(select[ \t]*case\\)\\>" 2
-        f90-face-select)
-   (list "^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*\\(select[ \t]*case\\)\\>" 1
-        f90-face-label)
-   (list "^[ \t]*end[ \t]*select\\>" nil f90-face-select)
-   (list "^[ \t]*end[ \t]*select\\>[ \t]+\\(\\sw+\\)" 1 f90-face-label)
-   (list "\\(where\\|forall\\)[ \t]*(" 1 f90-face-if)
-   (list "\\<\\(elsewhere\\|else\\|else[ \t]*if\\)\\>" nil f90-face-if)
-   (list "\\<end[ \t]*\\(where\\|forall\\)\\>" nil f90-face-if)
-   (list "\\<then\\>" nil f90-face-if)
-   (list "\\<\\(exit\\|cycle\\)\\>" nil f90-face-exit)
-   (list "\\<\\(exit\\|cycle\\)[ \t]*\\sw+\\>" nil f90-face-label)
-   (list "\\<\\(stop\\|return\\)\\>" nil f90-face-stop)
-   (list "^[ \t]*\\(case\\)[ \t]*\\((\\|default\\)" 1 f90-face-select)
-   (list (concat "\\<\\("(mapconcat 'identity f90-keywords "\\|")
-                "\\)\\>") nil f90-face-keyword)
- (list (concat "\\<\\("(mapconcat 'identity f90-intrinsic-procedures "\\|")
-              "\\)\\>") nil f90-face-intrinsics)
- (list (concat "\\<\\("(mapconcat 'identity f90-hpf-procedures "\\|")
-              "\\)\\>") nil f90-face-hpf-procedures)
- (list (concat "\\<\\("(mapconcat 'identity f90-hpf-directives "\\|")
-              "\\)\\>") nil f90-face-hpf-directives)
- (list (concat "\\<\\("(mapconcat 'identity f90-hpf-keywords "\\|")
-              "\\)\\>") nil f90-face-hpf-keywords)))
-
 ;; syntax table
 (defvar f90-mode-syntax-table nil
   "Syntax table in use in F90 mode buffers.")
@@ -564,8 +469,14 @@ whether to blink the matching beginning.")
   (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
 (if (string-match "XEmacs" emacs-version)
     (defvar f90-xemacs-menu
@@ -582,7 +493,7 @@ whether to blink the matching beginning.")
        ["Break Line at Point"     f90-break-line t]
        ["Join with Next Line"     f90-join-lines t]
        ["Insert Newline"          newline t]
-       ["Insert End"              f90-insert-end t]
+       ["Insert Block End"        f90-insert-end t]
        "-----"
        ["Upcase Keywords (buffer)"      f90-upcase-keywords t]
        ["Upcase Keywords (region)"      f90-upcase-region-keywords
@@ -595,40 +506,135 @@ whether to blink the matching beginning.")
         f90-downcase-region-keywords t]
        "-----"
        ["Toggle abbrev-mode"   abbrev-mode             t]
-       ["Toggle auto-fill"     f90-auto-fill-mode      t])
+       ["Toggle auto-fill"     auto-fill-mode          t])
       "XEmacs menu for F90 mode.")
   ;; Emacs
+
+  (defvar f90-change-case-menu
+    (let ((map (make-sparse-keymap "Change Keyword Case")))
+      
+      (define-key map [dkr] (cons "Downcase Keywords (region)"
+                                 'f90-downcase-region-keywords))
+      (put 'f90-downcase-region-keywords 'menu-enable 'mark-active)
+      
+      (define-key map [ckr] (cons "Capitalize Keywords (region)"
+                                 'f90-capitalize-region-keywords))
+      (put 'f90-capitalize-region-keywords 'menu-enable 'mark-active)
+      
+      (define-key map [ukr] (cons "Upcase Keywords (region)"
+                                 'f90-upcase-region-keywords))
+      (put 'f90-upcase-region-keywords 'menu-enable 'mark-active)
+      
+      (define-key map [line] (list "-----------------"))
+      
+      (define-key map [dkb] (cons "Downcase Keywords (buffer)"
+                                 'f90-downcase-keywords))
+      
+      (define-key map [ckb] (cons "Capitalize Keywords (buffer)"
+                                 'f90-capitalize-keywords))
+      
+      (define-key map [ukb] (cons "Upcase Keywords (buffer)"
+                                 'f90-upcase-keywords))
+      map)
+    "Submenu for change of case.")
+  (defalias 'f90-change-case-menu f90-change-case-menu)
+
+  ;; font-lock-menu and function calls
+  (defalias 'f90-font-lock-on  'font-lock-mode)
+  (defalias 'f90-font-lock-off 'font-lock-mode)
+  (put 'f90-font-lock-on  'menu-enable 'font-lock-mode)
+  (put 'f90-font-lock-off 'menu-enable '(not font-lock-mode))
+  
+  (defun f90-font-lock-1 ()
+    (interactive)
+    "Set font-lock-keywords to f90-font-lock-keywords-1."
+    (font-lock-mode 1)
+    (setq font-lock-keywords f90-font-lock-keywords-1)
+    (font-lock-fontify-buffer))
+    
+  (defun f90-font-lock-2 ()
+    (interactive)
+    "Set font-lock-keywords to f90-font-lock-keywords-2."
+    (font-lock-mode 1)
+    (setq font-lock-keywords f90-font-lock-keywords-2)
+    (font-lock-fontify-buffer))
+    
+  (defun f90-font-lock-3 ()
+    (interactive)
+    "Set font-lock-keywords to f90-font-lock-keywords-3."
+    (font-lock-mode 1)
+    (setq font-lock-keywords f90-font-lock-keywords-3)
+    (font-lock-fontify-buffer))
+    
+  (defun f90-font-lock-4 ()
+    (interactive)
+    "Set font-lock-keywords to f90-font-lock-keywords-4."
+    (font-lock-mode 1)
+    (setq font-lock-keywords f90-font-lock-keywords-4)
+    (font-lock-fontify-buffer))
+    
+  (defvar f90-font-lock-menu
+    (let ((map (make-sparse-keymap "f90-font-lock-menu")))
+      (define-key map [h4] (cons "Maximum highlighting (level 4)"
+                                'f90-font-lock-4))
+      (define-key map [h3] (cons "Heavy highlighting (level 3)"
+                                'f90-font-lock-3))
+      (define-key map [h2] (cons "Default highlighting (level 2)"
+                                'f90-font-lock-2))
+      (define-key map [h1] (cons "Light highlighting (level 1)"
+                                'f90-font-lock-1))
+      (define-key map [line] (list "-----------------"))
+      (define-key map [floff] (cons "Turn off font-lock-mode"
+                                   'f90-font-lock-on))
+      (define-key map [flon] (cons "Turn on font-lock-mode"
+                                  'f90-font-lock-off))
+      map)
+    "Submenu for highlighting using font-lock-mode.")
+  (defalias 'f90-font-lock-menu f90-font-lock-menu)
+
   (define-key f90-mode-map [menu-bar] (make-sparse-keymap))
   (define-key f90-mode-map [menu-bar f90] 
     (cons "F90" (make-sparse-keymap "f90"))) 
+
+  (define-key f90-mode-map [menu-bar f90 f90-imenu-menu]
+    '("Add imenu Menu" . f90-add-imenu-menu))
   (define-key f90-mode-map [menu-bar f90 abbrev-mode]
     '("Toggle abbrev-mode" . abbrev-mode))
-  (define-key f90-mode-map [menu-bar f90 f90-auto-fill-mode]
-    '("Toggle auto-fill" . f90-auto-fill-mode))
-  (define-key f90-mode-map [menu-bar f90 f90-downcase-region-keywords]
-    '("Downcase Keywords (region)" . f90-downcase-region-keywords))
-  (define-key f90-mode-map [menu-bar f90 f90-downcase-keywords]
-    '("Downcase Keywords (buffer)" . f90-downcase-keywords))
-  (define-key f90-mode-map [menu-bar f90 f90-capitalize-keywords]
-    '("Capitalize Keywords (region)" . f90-capitalize-region-keywords))
-  (define-key f90-mode-map [menu-bar f90 f90-capitalize-region-keywords]
-    '("Capitalize Keywords (buffer)" . f90-capitalize-keywords))
-  (define-key f90-mode-map [menu-bar f90 f90-upcase-region-keywords]
-    '("Upcase Keywords (region)" . f90-upcase-region-keywords))
-  (define-key f90-mode-map [menu-bar f90 f90-upcase-keywords]
-    '("Upcase Keywords (buffer)" . f90-upcase-keywords))
+  (define-key f90-mode-map [menu-bar f90 auto-fill-mode]
+    '("Toggle auto-fill" . auto-fill-mode))
+  (define-key f90-mode-map [menu-bar f90 line1]
+    '("----"))
+  (define-key f90-mode-map [menu-bar f90 f90-change-case-menu]
+    (cons "Change Keyword Case" 'f90-change-case-menu))
+  (define-key f90-mode-map [menu-bar f90 f90-font-lock-menu]
+    (cons "Highlighting" 'f90-font-lock-menu))
+  (define-key f90-mode-map [menu-bar f90 line2]
+    '("----"))
+
   (define-key f90-mode-map [menu-bar f90 f90-insert-end]
-    '("Insert end" . f90-insert-end))
+    '("Insert Block End" . f90-insert-end))
   (define-key f90-mode-map [menu-bar f90 f90-join-lines]
     '("Join with Next Line" . f90-join-lines))
   (define-key f90-mode-map [menu-bar f90 f90-break-line]
     '("Break Line at Point" . f90-break-line))
+  
+  (define-key f90-mode-map [menu-bar f90 line3]
+    '("----"))
+  
   (define-key f90-mode-map [menu-bar f90 f90-fill-region]
     '("Fill Region" . f90-fill-region))
+  (put 'f90-fill-region 'menu-enable 'mark-active)
+  
   (define-key f90-mode-map [menu-bar f90 indent-region]
     '("Indent Region" . indent-region))
+  
   (define-key f90-mode-map [menu-bar f90 f90-comment-region]
     '("(Un)Comment Region" . f90-comment-region))
+  (put 'f90-comment-region 'menu-enable 'mark-active)
+  
+  (define-key f90-mode-map [menu-bar f90 line4]
+    '("----"))
+  
   (define-key f90-mode-map [menu-bar f90 f90-end-of-subprogram]
     '("End of Subprogram" . f90-end-of-subprogram))
   (define-key f90-mode-map [menu-bar f90 f90-beginning-of-subprogram]
@@ -636,8 +642,9 @@ whether to blink the matching beginning.")
   (define-key f90-mode-map [menu-bar f90 f90-mark-subprogram]
     '("Mark Subprogram" . f90-mark-subprogram))
   (define-key f90-mode-map [menu-bar f90 f90-indent-subprogram]
-    '("Indent Subprogram" . f90-indent-subprogram)))
-  
+    '("Indent Subprogram" . f90-indent-subprogram))
+  )
+
 ;; Regexps for finding program structures.
 (defconst f90-blocks-re 
   "\\(block[ \t]*data\\|do\\|if\\|interface\\|function\\|module\\|\
@@ -645,7 +652,7 @@ program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
 (defconst f90-program-block-re 
   "\\(program\\|module\\|subroutine\\|function\\)")
 (defconst f90-else-like-re 
-  "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\)\\>")
+  "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\)")
 (defconst f90-end-if-re 
   "end[ \t]*\\(if\\|select\\|where\\|forall\\)\\>")
 (defconst f90-end-type-re 
@@ -656,22 +663,50 @@ program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
 ;; A temporary position to make region operators faster
 (defvar f90-cache-position nil)
 (make-variable-buffer-local 'f90-cache-position)
+;; A flag to tell whether f90-imenu is turned on.
+(defvar f90-imenu nil)
+(make-variable-buffer-local 'f90-imenu)
 
+\f
 ;; Imenu support
 (defvar f90-imenu-generic-expression
-  (cons
-   (concat
-    "^[ \t0-9]*\\("
-    "program[ \t]+\\(\\sw+\\)\\|"
-    "module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)\\|"
-    "\\(recursive[ \t]*\\)?subroutine[ \t]+\\(\\sw+\\)\\|"
-    ; avoid end function, but allow for most other things
-    "\\([^!]*\\([^e!].[^ \t!]\\|.[^n!][^ \t!]\\|..[^d \t!]\\)"
-    "\\|[^!]?[^!]?\\)[ \t]*function[ \t]+\\(\\sw+\\)"
-    "\\)")
-   (list 2 3 6 9))
+  (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
+       (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]"))
+    (list
+     '(nil "^[ \t0-9]*program[ \t]+\\(\\sw+\\)" 1)
+     '("Modules" "^[ \t0-9]*module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
+     '("Types" "^[ \t0-9]*type[ \t]+\\(\\sw+\\)" 1)
+     (list
+      "Procedures" 
+      (concat
+       "^[ \t0-9]*"
+       "\\("
+       ;; At least three non-space characters before function/subroutine
+       ;; Check that the last three non-space characters don't spell E N D
+       "[^!\"\&\n]*\\("
+       not-e good-char good-char "\\|"
+       good-char not-n good-char "\\|"
+       good-char good-char not-d "\\)"
+       "\\|"
+       ;; Less than three non-space characters before function/subroutine
+       good-char "?" good-char "?"
+       "\\)"
+       "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
+    4)))
   "imenu generic expression for F90 mode.")
 
+(defun f90-add-imenu-menu ()
+  (interactive)
+  "Add an imenu menu to the menubar."
+  (if (not f90-imenu)
+      (progn
+       (imenu-add-to-menubar "F90-imenu")
+       (redraw-frame (selected-frame))
+       (setq f90-imenu t))
+    (message "%s" "F90-imenu already exists.")))
+(put 'f90-add-imenu-menu 'menu-enable '(not f90-imenu))
+
+
 ;; When compiling under GNU Emacs, load imenu during compilation.  If
 ;; you have 19.22 or earlier, comment this out, or get imenu.
 (and (fboundp 'eval-when-compile)
@@ -679,8 +714,6 @@ program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
        (if (not (string-match "XEmacs" emacs-version))
           (require 'imenu))
        ()))
-
-
 \f
 ;; abbrevs have generally two letters, except standard types `c, `i, `r, `t
 (defvar f90-mode-abbrev-table nil)
@@ -799,7 +832,7 @@ Variables controlling indentation style and extra features:
     Do not left-justify line numbers. (default nil)
  f90-startup-message
     Set to nil to inhibit message first time F90 mode is used. (default t)
- f90-keywords
+ f90-keywords-re
     List of keywords used for highlighting/upcase-keywords etc.
 
 Turning on F90 mode calls the value of the variable `f90-mode-hook'
@@ -825,13 +858,11 @@ with no args, if that value is non-nil."
   (setq comment-indent-function 'f90-comment-indent)
   (make-local-variable 'abbrev-all-caps)
   (setq abbrev-all-caps t)
+  (make-local-variable 'normal-auto-fill-function)
+  (setq normal-auto-fill-function 'f90-do-auto-fill)
   (setq indent-tabs-mode nil)
-
-  (if (fboundp 'hilit-set-mode-patterns)
-      (hilit-set-mode-patterns
-       'f90-mode f90-hilit-patterns nil 'case-insensitive))
   ;; 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
@@ -843,13 +874,10 @@ with no args, if that value is non-nil."
        (setq font-lock-keywords f90-font-lock-keywords))
     ;; Emacs
     (make-local-variable 'font-lock-defaults)
-    (setq font-lock-defaults 
-         '((f90-font-lock-keywords f90-font-lock-keywords-1
-                                   f90-font-lock-keywords-2
-                                   f90-font-lock-keywords-3
-                                   f90-font-lock-keywords-4)
-           nil t))
+    (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)
@@ -961,7 +989,8 @@ Name is nil if the statement has no label."
            (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)
@@ -972,16 +1001,14 @@ Name is nil if the statement has no label."
                (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.
@@ -1000,10 +1027,8 @@ Name is non-nil only for type."
    ((and (not (looking-at "module[ \t]*procedure\\>"))
         (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
     (list (f90-match-piece 1) (f90-match-piece 2)))
-   ((looking-at "\\(recursive[ \t]*\\)?\\(subroutine\\)[ \t]+\\(\\sw+\\)")
-    (list (f90-match-piece 2) (f90-match-piece 3)))
-   ((and (not (looking-at "end[ \t]*function"))
-        (looking-at ".*\\(function\\)[ \t]+\\(\\sw+\\)"))
+   ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
+        (looking-at "[^!\"\&\n]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)"))
     (list (f90-match-piece 1) (f90-match-piece 2)))))
 
 (defsubst f90-looking-at-program-block-end ()
@@ -1017,7 +1042,11 @@ Name is non-nil only for type."
        ((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))))
@@ -1046,8 +1075,8 @@ Name is non-nil only for type."
   (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]*:\\)")
@@ -1060,10 +1089,15 @@ block[ \t]*data\\)\\>")
        (progn (setq bol (f90-get-beg-of-line)
                     eol (f90-get-end-of-line))
               (if f90-auto-keyword-case
-                  (f90-change-keywords f90-auto-keyword-case bol eol)
-              (if (and f90-auto-hilit19 (fboundp 'hilit-rehighlight-region))
-                  (hilit-rehighlight-region bol eol t)))))))
+                  (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."
@@ -1280,9 +1314,7 @@ or, if already present, remove it."
     (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 ()
@@ -1405,7 +1437,7 @@ If run in the middle of a line, the line is not broken."
     (goto-char save-point)
     (set-marker end-region-mark nil)
     (set-marker save-point nil)
-    (if (string-match "Xemacs" emacs-version)
+    (if (string-match "XEmacs" emacs-version)
        (zmacs-deactivate-region)
       (deactivate-mark))))
 
@@ -1434,15 +1466,14 @@ If run in the middle of a line, the line is not broken."
     (cond ((f90-in-string)
           (insert "&") (newline) (insert "&"))
          ((f90-in-comment)
-          (delete-horizontal-space)
           (setq ctype (f90-get-present-comment-type))
-          (newline) (insert (concat ctype " ")))
-         (t (delete-horizontal-space)
-            (insert "&")
+          (newline)
+          (insert ctype))
+         (t (insert "&")
             (if (not no-update) (f90-update-line))
             (newline)
-            (if f90-beginning-ampersand (insert "& ")))))
-  (if (not no-update) (f90-indent-line)))
+            (if f90-beginning-ampersand (insert "&")))))
+  (f90-indent-line))
   
 (defun f90-find-breakpoint ()
   "From fill-column, search backward for break-delimiter."
@@ -1456,33 +1487,22 @@ If run in the middle of a line, the line is not broken."
          (forward-char 2)
        (forward-char)))))
 
-(defun f90-auto-fill-mode (arg)
-  "Toggle f90-auto-fill mode.
-With ARG, turn `f90-auto-fill' mode on iff ARG is positive.
-In `f90-auto-fill' mode, inserting a space at a column beyond `fill-column'
-automatically breaks the line at a previous space."
-  (interactive "P")
-  (prog1 (setq auto-fill-function
-              (if (if (null arg)
-                      (not auto-fill-function)
-                    (> (prefix-numeric-value arg) 0))
-                  'f90-do-auto-fill))
-    (force-mode-line-update)))
-
 (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).
+  ;; 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).
-  ;; Start by checking that line is longer than fill-column.
-  (if (> (save-excursion (end-of-line) (current-column)) fill-column)
-      (progn
+  (f90-update-line)
+  (while (> (current-column) fill-column)
+      (let ((pos-mark (point-marker)))
        (move-to-column fill-column)
-       (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
-           (delete-horizontal-space)
-         (f90-find-breakpoint)
-         (f90-break-line)
-         (end-of-line)))))
+       (if (not (f90-in-string))
+           (f90-find-breakpoint))
+       (f90-break-line)
+       (goto-char pos-mark)
+       (set-marker pos-mark nil))))
+
 
 (defun f90-join-lines ()
   "Join present line with next line, if this line ends with \&."
@@ -1511,28 +1531,22 @@ automatically breaks the line at a previous space."
   "Fill every line in region by forward parsing. Join lines if possible."
   (interactive "*r")
   (let ((end-region-mark (make-marker))
-       (f90-auto-hilit19 nil)
-       (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)
@@ -1580,9 +1594,19 @@ Leave point at the end of line."
          (setq end-name  (car (cdr end-struct)))
          (save-excursion
            (beginning-of-line)
-           (while (and (not (zerop count))
-                       (re-search-backward 
-                        (concat "\\(" f90-blocks-re "\\)") nil t))
+           (while 
+               (and (not (zerop count))
+                    (let ((stop nil) notexist)
+                      (while (not stop)
+                        (setq notexist
+                              (not (re-search-backward 
+                                    (concat "\\(" f90-blocks-re "\\)") nil t)))
+                        (if notexist
+                            (setq stop t)
+                          (setq stop
+                                (not (or (f90-in-string)
+                                         (f90-in-comment))))))
+                      (not notexist)))
              (beginning-of-line) (skip-chars-forward " \t0-9")
              (cond ((setq matching-beg
                           (cond
@@ -1600,7 +1624,7 @@ Leave point at the end of line."
              (f90-update-line)
              (if (eq f90-smart-end 'blink)
                  (if (< (point) top-of-window)
-                     (message "Matches %d: %s"
+                     (message "Matches %s: %s"
                               (what-line)
                               (buffer-substring
                                (progn (beginning-of-line) (point))
@@ -1627,10 +1651,11 @@ Any other key combination is executed normally."
   (interactive)
   (let (e c)
     (insert last-command-char)
-    (setq e (next-command-event)
-         c (if (string-match "XEmacs" emacs-version)
-               (event-to-character e)
-             (read-event)))
+    (if (string-match "XEmacs" emacs-version)
+       (progn
+         (setq e (next-command-event))
+         (setq c (event-to-character e)))
+      (setq c (read-event)))
     ;; insert char if not equal to `?'
     (if (or (= c ??) (eq c help-char))
        (f90-abbrev-help)