(f90-get-beg-of-line): Removed and replaced with line-beginning-position.
[bpt/emacs.git] / lisp / progmodes / f90.el
index 8debddd..a70e4e7 100644 (file)
@@ -1,9 +1,9 @@
 ;;; f90.el --- Fortran-90 mode (free format)
 
-;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
 
-;; Author: Torbj\"orn Einarsson <T.Einarsson@clab.ericsson.se>
-;; Last Change: May 29 1997
+;; Author: Torbj\"orn Einarsson <Torbjorn.Einarsson@era.ericsson.se>
+;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
 ;; Keywords: fortran, f90, languages
 
 ;; This file is part of GNU Emacs.
@@ -74,7 +74,7 @@
 
 ;; To customize the f90-mode for your taste, use, for example:
 ;;    (you don't have to specify values for all the parameters below)
-;;(setq f90-mode-hook
+;;(add-hook 'f90-mode-hook
 ;;      '(lambda () (setq f90-do-indent 3
 ;;                        f90-if-indent 3
 ;;                        f90-type-indent 3
@@ -95,7 +95,6 @@
 ;;                  )
 ;;       ;;The rest is not default.
 ;;       (abbrev-mode 1)             ; turn on abbreviation mode
-;;       (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))
 
 ;;; Code:
 
-(defconst bug-f90-mode "T.Einarsson@clab.ericsson.se"
-  "Address of mailing list for F90 mode bugs.")
-
 ;; User options
 
 (defgroup f90 nil
   "Fortran-90 mode"
-  :group 'fortran)
+  :group 'languages)
 
 (defgroup f90-indent nil
   "Fortran-90 indentation"
@@ -240,143 +236,106 @@ whether to blink the matching beginning."
   :group 'f90)
 
 (defconst f90-keywords-re
-  ;;("allocate" "allocatable" "assign" "assignment" "backspace" "block"
-  ;;"call" "case" "character" "close" "common" "complex" "contains"
-  ;;"continue" "cycle" "data" "deallocate" "dimension" "do" "double" "else"
-  ;;"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" "operator" "optional" "parameter"
-  ;;"pause" "pointer" "precision" "print" "private" "procedure" "program"
-  ;;"public" "read" "real" "recursive" "result" "return" "rewind" "save" "select"
-  ;;"sequence" "stop" "subroutine" "target" "then" "type" "use" "where"
-  ;;"while" "write")
-  (concat
-   "\\<\\(a\\(llocat\\(able\\|e\\)\\|ssign\\(\\|ment\\)\\)\\|b\\(ackspace\\|"
-   "lock\\)\\|c\\(a\\(ll\\|se\\)\\|haracter\\|lose\\|o\\(m\\(mon\\|plex\\)\\|"
-   "nt\\(ains\\|inue\\)\\)\\|ycle\\)\\|d\\(ata\\|eallocate\\|imension\\|"
-   "o\\(\\|uble\\)\\)\\|e\\(lse\\(\\|if\\|where\\)\\|n\\(d\\(\\|do\\|file\\|"
-   "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\\|erator\\|tional\\)\\)\\|p\\(a\\("
-   "rameter\\|use\\)\\|ointer\\|r\\(ecision\\|i\\(nt\\|vate\\)\\|o\\("
-   "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-opt '("allocatable" "allocate" "assign" "assignment" "backspace"
+               "block" "call" "case" "character" "close" "common" "complex"
+               "contains" "continue" "cycle" "data" "deallocate"
+               "dimension" "do" "double" "else" "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" "operator" "optional" "parameter"
+               "pause" "pointer" "precision" "print" "private" "procedure"
+               "program" "public" "read" "real" "recursive" "result" "return"
+               "rewind" "save" "select" "sequence" "stop" "subroutine"
+               "target" "then" "type" "use" "where" "while" "write"
+               ;; F95 keywords.
+               "elemental" "pure") 'words)
   "Regexp for F90 keywords.")
 
 (defconst f90-keywords-level-3-re
-  ;; ("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\\|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\\|sult\\|wind\\)\\|s\\(ave\\|e\\(lect\\|quence\\)\\)\\|target\\|"
-   "write\\)\\>")
-"Keyword-regexp for font-lock level >= 3.")
+  (regexp-opt
+   '("allocatable" "allocate" "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"
+     ;; F95 keywords.
+     "elemental" "pure") 'words)
+  "Keyword-regexp for font-lock level >= 3.")
 
 
 (defconst f90-procedures-re
-  ;; ("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint" "all" "allocated"
-  ;; "anint" "any" "asin" "associated" "atan" "atan2" "bit_size" "btest"
-  ;; "ceiling" "char" "cmplx" "conjg" "cos" "cosh" "count" "cshift"
-  ;; "date_and_time" "dble" "digits" "dim" "dot_product" "dprod" "eoshift"
-  ;; "epsilon" "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
-  ;; "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior" "ishft"
-  ;; "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt" "lle" "llt" "log"
-  ;; "logical" "log10" "matmul" "max" "maxexponent" "maxloc" "maxval" "merge"
-  ;; "min" "minexponent" "minloc" "minval" "mod" "modulo" "mvbits" "nearest"
-  ;; "nint" "not" "pack" "precision" "present" "product" "radix"
-  ;; "random_number" "random_seed" "range" "real" "repeat" "reshape"
-  ;; "rrspacing" "scale" "scan" "selected_int_kind" "selected_real_kind"
-  ;; "set_exponent" "shape" "sign" "sin" "sinh" "size" "spacing" "spread"
-  ;; "sqrt" "sum" "system_clock" "tan" "tanh" "tiny" "transfer" "transpose"
-  ;; "trim" "ubound" "unpack" "verify")
-  ;; A left parenthesis to avoid highlighting non-procedures.
-  ;; Real is taken out here to avoid highlighting declarations.
-  (concat
-   "\\<\\(a\\(bs\\|c\\(har\\|os\\)\\|djust[lr]\\|i\\(mag\\|nt\\)\\|ll\\(\\|"
-   "ocated\\)\\|n\\(int\\|y\\)\\|s\\(in\\|sociated\\)\\|tan2?\\)\\|b\\("
-   "it_size\\|test\\)\\|c\\(eiling\\|har\\|mplx\\|o\\(njg\\|sh?\\|unt\\)\\|"
-   "shift\\)\\|d\\(ate_and_time\\|ble\\|i\\(gits\\|m\\)\\|ot_product\\|prod"
-   "\\)\\|e\\(oshift\\|psilon\\|xp\\(\\|onent\\)\\)\\|f\\(loor\\|"
-   "raction\\)\\|huge\\|i\\(a\\(char\\|nd\\)\\|b\\(clr\\|its\\|set\\)\\|"
-   "char\\|eor\\|n\\(dex\\|t\\)\\|or\\|shftc?\\)\\|kind\\|l\\(bound\\|"
-   "en\\(\\|_trim\\)\\|g[et]\\|l[et]\\|og\\(\\|10\\|ical\\)\\)\\|m\\(a\\("
-   "tmul\\|x\\(\\|exponent\\|loc\\|val\\)\\)\\|erge\\|in\\(\\|exponent\\|"
-   "loc\\|val\\)\\|od\\(\\|ulo\\)\\|vbits\\)\\|n\\(earest\\|int\\|ot\\)\\|"
-   "p\\(ack\\|r\\(e\\(cision\\|sent\\)\\|oduct\\)\\)\\|r\\(a\\(dix\\|n\\("
-   "dom_\\(number\\|seed\\)\\|ge\\)\\)\\|e\\(peat\\|shape\\)\\|rspacing\\)\\|"
-   "s\\(ca\\(le\\|n\\)\\|e\\(lected_\\(int_kind\\|real_kind\\)\\|"
-   "t_exponent\\)\\|hape\\|i\\(gn\\|nh?\\|ze\\)\\|p\\(acing\\|read\\)\\|"
-   "qrt\\|um\\|ystem_clock\\)\\|t\\(anh?\\|iny\\|r\\(ans\\(fer\\|pose\\)\\|"
-   "im\\)\\)\\|u\\(bound\\|npack\\)\\|verify\\)[ \t]*(")
+  (concat "\\<"
+   (regexp-opt
+    '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
+      "all" "allocated" "anint" "any" "asin" "associated"
+      "atan" "atan2" "bit_size" "btest" "ceiling" "char" "cmplx"
+      "conjg" "cos" "cosh" "count" "cshift" "date_and_time" "dble"
+      "digits" "dim" "dot_product" "dprod" "eoshift" "epsilon"
+      "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
+      "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior"
+      "ishft" "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt"
+      "lle" "llt" "log" "log10" "logical" "matmul" "max"
+      "maxexponent" "maxloc" "maxval" "merge" "min" "minexponent"
+      "minloc" "minval" "mod" "modulo" "mvbits" "nearest" "nint"
+      "not" "pack" "precision" "present" "product" "radix"
+      ;; Real is taken out here to avoid highlighting declarations.
+      "random_number" "random_seed" "range" ;; "real"
+      "repeat" "reshape" "rrspacing" "scale" "scan"
+      "selected_int_kind" "selected_real_kind" "set_exponent"
+      "shape" "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt"
+      "sum" "system_clock" "tan" "tanh" "tiny" "transfer"
+      "transpose" "trim" "ubound" "unpack" "verify"
+      ;; F95 intrinsic functions.
+      "null" "cpu_time") t)
+   ;; A left parenthesis to avoid highlighting non-procedures.
+   "[ \t]*(")
   "Regexp whose first part matches F90 intrinsic procedures.")
 
 (defconst f90-operators-re
-;; "and" "or" "not" "eqv" "neqv" "eq" "ne" "lt" "le" "gt" "ge" "true" "false"
- (concat
-   "\\.\\(and\\|eqv?\\|false\\|g[et]\\|l[et]\\|n\\(e\\(\\|qv\\)\\|"
-   "ot\\)\\|or\\|true\\)\\.")
+ (concat "\\."
+        (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
+                      "neqv" "not" "or" "true") t)
+        "\\.")
   "Regexp matching intrinsic operators.")
 
 (defconst f90-hpf-keywords-re
-  ;; Intrinsic procedures
-  ;; ("all_prefix" "all_scatter" "all_suffix" "any_prefix" "any_scatter"
-  ;; "any_suffix" "copy_prefix" "copy_scatter" "copy_suffix" "count_prefix"
-  ;; "count_scatter" "count_suffix" "grade_down" "grade_up" "hpf_alignment"
-  ;; "hpf_template" "hpf_distribution" "iall" "iall_prefix" "iall_scatter"
-  ;; "iall_suffix" "iany" "iany_prefix" "iany_scatter" "iany_suffix" "iparity"
-  ;; "iparity_prefix" "iparity_scatter" "iparity_suffix" "leadz"
-  ;; "maxval_prefix" "maxval_scatter" "maxval_suffix" "minval_prefix"
-  ;; "minval_scatter" "minval_suffix" "parity" "parity_prefix"
-  ;; "parity_scatter" "parity_suffix" "popcnt" "poppar" "product_prefix"
-  ;; "product_scatter" "product_suffix" "sum_prefix" "sum_scatter"
-  ;; "sum_suffix" "ilen" "number_of_processors" "processors_shape")
-  ;; Directives
-  ;; ("align" "distribute" "dynamic" "inherit" "template" "processors"
-  ;; "realign" "redistribute" "independent")
-  ;; Keywords
-  ;; ("pure" "extrinsic" "new" "with" "onto" "block" "cyclic")
-  (concat
-   "\\<\\(a\\(l\\(ign\\|l_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|ny_\\("
-   "prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|block\\|c\\(o\\(py_\\(prefix\\|"
-   "s\\(catter\\|uffix\\)\\)\\|unt_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|"
-   "yclic\\)\\|d\\(istribute\\|ynamic\\)\\|extrinsic\\|grade_\\(down\\|"
-   "up\\)\\|hpf_\\(alignment\\|distribution\\|template\\)\\|i\\(a\\(ll\\(\\|"
-   "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|ny\\(\\|_\\(prefix\\|s\\("
-   "catter\\|uffix\\)\\)\\)\\)\\|len\\|n\\(dependent\\|herit\\)\\|parity\\(\\|"
-   "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\)\\|leadz\\|m\\(axval_\\("
-   "prefix\\|s\\(catter\\|uffix\\)\\)\\|inval_\\(prefix\\|s\\(catter\\|"
-   "uffix\\)\\)\\)\\|n\\(ew\\|umber_of_processors\\)\\|onto\\|p\\(arity\\(\\|"
-   "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|op\\(cnt\\|par\\)\\|ro\\("
-   "cessors\\(\\|_shape\\)\\|duct_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|"
-   "ure\\)\\|re\\(align\\|distribute\\)\\|sum_\\(prefix\\|s\\(catter\\|"
-   "uffix\\)\\)\\|template\\|with\\)\\>")
+  (regexp-opt
+   ;; Intrinsic procedures
+   '("all_prefix" "all_scatter" "all_suffix" "any_prefix"
+     "any_scatter" "any_suffix" "copy_prefix" "copy_scatter"
+     "copy_suffix" "count_prefix" "count_scatter" "count_suffix"
+     "grade_down" "grade_up"
+     "hpf_alignment" "hpf_distribution" "hpf_template" "iall" "iall_prefix"
+     "iall_scatter" "iall_suffix" "iany" "iany_prefix" "iany_scatter"
+     "iany_suffix" "ilen" "iparity" "iparity_prefix"
+     "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
+     "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter"
+     "minval_suffix" "number_of_processors" "parity"
+     "parity_prefix" "parity_scatter" "parity_suffix" "popcnt" "poppar"
+     "processors_shape" "product_prefix" "product_scatter"
+     "product_suffix" "sum_prefix" "sum_scatter" "sum_suffix"
+     ;; Directives
+     "align" "distribute" "dynamic" "independent" "inherit" "processors"
+     "realign" "redistribute" "template" 
+     ;; Keywords
+     "block" "cyclic" "extrinsic" "new" "onto" "pure" "with") 'words)
   "Regexp for all HPF keywords, procedures and directives.")
 
 ;; Highlighting patterns
 
 (defvar f90-font-lock-keywords-1
-  (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))
+  (list
    ;; Special highlighting of "module procedure foo-list"
-   '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face t))
+   '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face))
    ;; Highlight definition of new type
-   '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
+   '("\\<\\(type\\)[ \t]*\\(.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
      (1 font-lock-keyword-face) (3 font-lock-function-name-face))
+   ;; Other functions and declarations.
+   '("\\<\\(\\(?:end[ \t]*\\)?\\(program\\|module\\|function\\|subroutine\\|type\\)\\|use\\|call\\)\\>[ \t]*\\(\\sw+\\)?"
+     (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
    "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
   "This does fairly subdued highlighting of comments and function calls.")
 
@@ -388,21 +347,21 @@ whether to blink the matching beginning."
         (1 font-lock-type-face) (4 font-lock-variable-name-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-reference-face nil t))
+        (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-reference-face nil t) (3 font-lock-keyword-face))
+        (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))
+       '("\\<\\(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
@@ -410,11 +369,9 @@ whether to blink the matching beginning."
    (list
     f90-keywords-level-3-re
     f90-operators-re
-    (if (string-match "XEmacs" emacs-version)
-       (append (list f90-procedures-re) '(1 font-lock-keyword-face t))
-      (list f90-procedures-re '(1 font-lock-keyword-face t)))
+    (list f90-procedures-re '(1 font-lock-keyword-face keep))
     "\\<real\\>"                       ; Avoid overwriting real defs.
-    ))
+   ))
   "Highlights all F90 keywords and intrinsic procedures.")
 
 (defvar f90-font-lock-keywords-4
@@ -445,7 +402,7 @@ whether to blink the matching beginning."
   (modify-syntax-entry ?= "." f90-mode-syntax-table)
   (modify-syntax-entry ?* "." f90-mode-syntax-table)
   (modify-syntax-entry ?/ "." f90-mode-syntax-table)
-  (modify-syntax-entry ?\\ "/" f90-mode-syntax-table)) ; escape chars
+  (modify-syntax-entry ?\\ "\\" f90-mode-syntax-table)) ; escape chars
 
 ;; keys
 (defvar f90-mode-map ()
@@ -658,7 +615,7 @@ program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
 (defconst f90-end-type-re 
   "end[ \t]*\\(type\\|interface\\|block[ \t]*data\\)")
 (defconst f90-type-def-re
-  "\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)")
+  "\\<\\(type\\)\\([^(\n]*\\)\\(::\\)?[ \t]*\\b\\(\\sw+\\)")
 (defconst f90-no-break-re  "\\(\\*\\*\\|//\\|=>\\)")
 ;; A temporary position to make region operators faster
 (defvar f90-cache-position nil)
@@ -721,61 +678,67 @@ program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
     ()
   (let ((ac abbrevs-changed))
     (define-abbrev-table 'f90-mode-abbrev-table ())
-    (define-abbrev f90-mode-abbrev-table  "`al"  "allocate" nil)
-    (define-abbrev f90-mode-abbrev-table  "`ab"  "allocatable" nil)
-    (define-abbrev f90-mode-abbrev-table  "`as"  "assignment" nil)
-    (define-abbrev f90-mode-abbrev-table  "`ba"  "backspace" nil)
-    (define-abbrev f90-mode-abbrev-table  "`bd"  "block data" nil)
-    (define-abbrev f90-mode-abbrev-table  "`c"   "character" nil)
-    (define-abbrev f90-mode-abbrev-table  "`cl"  "close" nil)
-    (define-abbrev f90-mode-abbrev-table  "`cm"  "common" nil)
-    (define-abbrev f90-mode-abbrev-table  "`cx"  "complex" nil)
-    (define-abbrev f90-mode-abbrev-table  "`cn"  "contains" nil)
-    (define-abbrev f90-mode-abbrev-table  "`cy"  "cycle" nil)
-    (define-abbrev f90-mode-abbrev-table  "`de"  "deallocate" nil)
-    (define-abbrev f90-mode-abbrev-table  "`df"  "define" nil)
-    (define-abbrev f90-mode-abbrev-table  "`di"  "dimension" nil)
-    (define-abbrev f90-mode-abbrev-table  "`dw"  "do while" nil)
-    (define-abbrev f90-mode-abbrev-table  "`el"  "else" nil)
-    (define-abbrev f90-mode-abbrev-table  "`eli" "else if" nil)
-    (define-abbrev f90-mode-abbrev-table  "`elw" "elsewhere" nil)
-    (define-abbrev f90-mode-abbrev-table  "`eq"  "equivalence" nil)
-    (define-abbrev f90-mode-abbrev-table  "`ex"  "external" nil)
-    (define-abbrev f90-mode-abbrev-table  "`ey"  "entry" nil)
-    (define-abbrev f90-mode-abbrev-table  "`fl"  "forall" nil)
-    (define-abbrev f90-mode-abbrev-table  "`fo"  "format" nil)
-    (define-abbrev f90-mode-abbrev-table  "`fu"  "function" nil)
-    (define-abbrev f90-mode-abbrev-table  "`fa"  ".false." nil)
-    (define-abbrev f90-mode-abbrev-table  "`im"  "implicit none" nil)
-    (define-abbrev f90-mode-abbrev-table  "`in " "include" nil)
-    (define-abbrev f90-mode-abbrev-table  "`i"   "integer" nil)
-    (define-abbrev f90-mode-abbrev-table  "`it"  "intent" nil)
-    (define-abbrev f90-mode-abbrev-table  "`if"  "interface" nil)
-    (define-abbrev f90-mode-abbrev-table  "`lo"  "logical" nil)
-    (define-abbrev f90-mode-abbrev-table  "`mo"  "module" nil)
-    (define-abbrev f90-mode-abbrev-table  "`na"  "namelist" nil)
-    (define-abbrev f90-mode-abbrev-table  "`nu"  "nullify" nil)
-    (define-abbrev f90-mode-abbrev-table  "`op"  "optional" nil)
-    (define-abbrev f90-mode-abbrev-table  "`pa"  "parameter" nil)
-    (define-abbrev f90-mode-abbrev-table  "`po"  "pointer" nil)
-    (define-abbrev f90-mode-abbrev-table  "`pr"  "print" nil)
-    (define-abbrev f90-mode-abbrev-table  "`pi"  "private" nil)
-    (define-abbrev f90-mode-abbrev-table  "`pm"  "program" nil)
-    (define-abbrev f90-mode-abbrev-table  "`pu"  "public" nil)
-    (define-abbrev f90-mode-abbrev-table  "`r"   "real" nil)
-    (define-abbrev f90-mode-abbrev-table  "`rc"  "recursive" nil)
-    (define-abbrev f90-mode-abbrev-table  "`rt"  "return" nil)
-    (define-abbrev f90-mode-abbrev-table  "`rw"  "rewind" nil)
-    (define-abbrev f90-mode-abbrev-table  "`se"  "select" nil)
-    (define-abbrev f90-mode-abbrev-table  "`sq"  "sequence" nil)
-    (define-abbrev f90-mode-abbrev-table  "`su"  "subroutine" nil)
-    (define-abbrev f90-mode-abbrev-table  "`ta"  "target" nil)
-    (define-abbrev f90-mode-abbrev-table  "`tr"  ".true." nil)
-    (define-abbrev f90-mode-abbrev-table  "`t"   "type" nil)
-    (define-abbrev f90-mode-abbrev-table  "`wh"  "where" nil)
-    (define-abbrev f90-mode-abbrev-table  "`wr"  "write" nil)
+    (define-abbrev f90-mode-abbrev-table  "`al"  "allocate" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`ab"  "allocatable" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`as"  "assignment" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`ba"  "backspace" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`bd"  "block data" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`c"   "character" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`cl"  "close" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`cm"  "common" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`cx"  "complex" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`cn"  "contains" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`cy"  "cycle" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`de"  "deallocate" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`df"  "define" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`di"  "dimension" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`dw"  "do while" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`el"  "else" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`eli" "else if" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`elw" "elsewhere" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`eq"  "equivalence" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`ex"  "external" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`ey"  "entry" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`fl"  "forall" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`fo"  "format" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`fu"  "function" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`fa"  ".false." nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`im"  "implicit none" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`in " "include" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`i"   "integer" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`it"  "intent" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`if"  "interface" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`lo"  "logical" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`mo"  "module" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`na"  "namelist" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`nu"  "nullify" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`op"  "optional" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`pa"  "parameter" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`po"  "pointer" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`pr"  "print" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`pi"  "private" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`pm"  "program" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`pu"  "public" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`r"   "real" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`rc"  "recursive" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`rt"  "return" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`rw"  "rewind" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`se"  "select" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`sq"  "sequence" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`su"  "subroutine" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`ta"  "target" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`tr"  ".true." nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`t"   "type" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`wh"  "where" nil 0 t)
+    (define-abbrev f90-mode-abbrev-table  "`wr"  "write" nil 0 t)
     (setq abbrevs-changed ac)))
 \f
+(defcustom f90-mode-hook nil
+  "Hook run by F90 mode."
+  :type 'hook
+  :options '(f90-add-imenu-menu)
+  :group 'f90)
+
 ;;;###autoload
 (defun f90-mode ()
   "Major mode for editing Fortran 90 code in free format.
@@ -865,32 +828,30 @@ with no args, if that value is non-nil."
   (if (string-match "XEmacs" emacs-version)
       (progn
        (put 'f90-mode 'font-lock-keywords-case-fold-search t)
-       (if (and current-menubar
+       (if (and (featurep 'menubar)
+                current-menubar
                 (not (assoc "F90" current-menubar)))
            (progn
              (set-buffer-menubar (copy-sequence current-menubar))
-             (add-submenu nil f90-xemacs-menu)))
-       (make-local-variable 'font-lock-keywords)
-       (setq font-lock-keywords f90-font-lock-keywords))
-    ;; Emacs
-    (make-local-variable 'font-lock-defaults)
-    (setq font-lock-defaults '(f90-font-lock-keywords nil t))
-
-    ;; Tell imenu how to handle f90.
-    (make-local-variable 'imenu-generic-expression)
-    (setq imenu-generic-expression f90-imenu-generic-expression))
+             (add-submenu nil f90-xemacs-menu)))))
+  ;; XEmacs: (Don't need a special case, since both emacsen work alike -sb)
+  (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))
+  ;; Tell imenu how to handle f90.
+  (set (make-local-variable 'imenu-case-fold-search) t)
+  (make-local-variable 'imenu-generic-expression)
+  (setq imenu-generic-expression f90-imenu-generic-expression)
+  (set (make-local-variable 'add-log-current-defun-function)
+       #'f90-current-defun)
   (run-hooks 'f90-mode-hook)
-  (if f90-startup-message
-      (message "Emacs F90 mode; please report bugs to %s" bug-f90-mode))
   (setq f90-startup-message nil))
 \f
 ;; inline-functions
-(defsubst f90-get-beg-of-line ()
-  (save-excursion (beginning-of-line) (point)))
-
-(defsubst f90-get-end-of-line ()
-  (save-excursion (end-of-line) (point)))
-
 (defsubst f90-in-string ()
   (let ((beg-pnt
         (if (and f90-cache-position (> (point) f90-cache-position))
@@ -907,7 +868,9 @@ with no args, if that value is non-nil."
 
 (defsubst f90-line-continued ()
   (save-excursion
-    (let ((bol (f90-get-beg-of-line)))
+    (beginning-of-line)
+    (while (and (looking-at "[ \t]*\\(!\\|$\\)") (zerop (forward-line -1))))
+    (let ((bol (line-beginning-position)))
       (end-of-line)
       (while (f90-in-comment)
        (search-backward "!" bol)
@@ -919,8 +882,7 @@ with no args, if that value is non-nil."
   "Return indentation of current line.
 Line-numbers are considered whitespace characters."
   (save-excursion
-    (beginning-of-line) (skip-chars-forward " \t0-9")
-    (current-column)))
+    (beginning-of-line) (skip-chars-forward " \t0-9")))
 
 (defsubst f90-indent-to (col &optional no-line-number)
   "Indent current line to column COL.
@@ -933,20 +895,16 @@ If no-line-number nil, jump over a possible line-number."
       (indent-to col)
     (indent-to col 1)))
 
-(defsubst f90-match-piece (arg)
-  (if (match-beginning arg)
-      (buffer-substring (match-beginning arg) (match-end arg))))
-
 (defsubst f90-get-present-comment-type ()
   (save-excursion
-    (let ((type nil) (eol (f90-get-end-of-line)))
+    (let ((type nil) (eol (line-end-position)))
       (if (f90-in-comment)
          (progn
            (beginning-of-line)
            (re-search-forward "[!]+" eol)
            (while (f90-in-string)
              (re-search-forward "[!]+" eol))
-           (setq type (buffer-substring (match-beginning 0) (match-end 0)))))
+           (setq type (match-string 0))))
       type)))
 
 (defsubst f90-equal-symbols (a b)
@@ -963,9 +921,9 @@ If no-line-number nil, jump over a possible line-number."
 Name is nil if the statement has no label."
   (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(do\\)\\>")
       (let (label
-           (struct (f90-match-piece 3)))
+           (struct (match-string 3)))
        (if (looking-at "\\(\\sw+\\)[ \t]*\:")
-           (setq label (f90-match-piece 1)))
+           (setq label (match-string 1)))
        (list struct label))))
 
 (defsubst f90-looking-at-select-case ()
@@ -973,9 +931,9 @@ Name is nil if the statement has no label."
 Name is nil if the statement has no label."
   (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(select\\)[ \t]*case[ \t]*(")
       (let (label
-           (struct (f90-match-piece 3)))
+           (struct (match-string 3)))
        (if (looking-at "\\(\\sw+\\)[ \t]*\:")
-           (setq label (f90-match-piece 1)))
+           (setq label (match-string 1)))
        (list struct label))))
 
 (defsubst f90-looking-at-if-then ()
@@ -985,10 +943,11 @@ Name is nil if the statement has no label."
     (let (struct (label nil))
       (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(if\\)\\>")
          (progn
-           (setq struct (f90-match-piece 3))
+           (setq struct (match-string 3))
            (if (looking-at "\\(\\sw+\\)[ \t]*\:")
-               (setq label (f90-match-piece 1)))
-           (goto-char (scan-lists (point) 1 0))
+               (setq label (match-string 1)))
+           (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)
@@ -1003,9 +962,9 @@ Name is nil if the statement has no label."
 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)))
+           (struct (match-string 3)))
        (if (looking-at "\\(\\sw+\\)[ \t]*\:")
-           (setq label (f90-match-piece 1)))
+           (setq label (match-string 1)))
        (list struct label))))
 
 (defsubst f90-looking-at-type-like ()
@@ -1013,34 +972,38 @@ Name is nil if the statement has no label."
 Name is non-nil only for type."
   (cond 
    ((looking-at f90-type-def-re)
-    (list (f90-match-piece 1) (f90-match-piece 3)))
+    (list (match-string 1) (match-string 4)))
    ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
-    (list (f90-match-piece 1) nil))))
+    (list (match-string 1) nil))))
 
 (defsubst f90-looking-at-program-block-start ()
   "Return (kind name) if a program block with name name starts after point."
   (cond
    ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
-    (list (f90-match-piece 1) (f90-match-piece 2)))
+    (list (match-string 1) (match-string 2)))
    ((and (not (looking-at "module[ \t]*procedure\\>"))
         (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
-    (list (f90-match-piece 1) (f90-match-piece 2)))
+    (list (match-string 1) (match-string 2)))
    ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
-        (looking-at "[^!\"\&\n]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)"))
-    (list (f90-match-piece 1) (f90-match-piece 2)))))
+        (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)"))
+    (list (match-string 1) (match-string 2)))))
 
 (defsubst f90-looking-at-program-block-end ()
   "Return list of type and name of end of block."
   (if (looking-at (concat "end[ \t]*" f90-blocks-re 
                          "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
-      (list (f90-match-piece 1) (f90-match-piece 3))))
+      (list (match-string 1) (match-string 3))))
 
 (defsubst f90-comment-indent ()
   (cond ((looking-at "!!!") 0)
        ((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))))
@@ -1066,7 +1029,7 @@ Name is non-nil only for type."
   (skip-chars-forward " \t0-9"))
 
 (defsubst f90-no-block-limit ()
-  (let ((eol (f90-get-end-of-line)))
+  (let ((eol (line-end-position)))
     (save-excursion
       (not (or (looking-at "end")
               (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
@@ -1080,8 +1043,8 @@ block[ \t]*data\\)\\>")
 (defsubst f90-update-line ()
   (let (bol eol)
     (if f90-auto-keyword-case
-       (progn (setq bol (f90-get-beg-of-line)
-                    eol (f90-get-end-of-line))
+       (progn (setq bol (line-beginning-position)
+                    eol (line-end-position))
               (if f90-auto-keyword-case
                   (f90-change-keywords f90-auto-keyword-case bol eol))))))
 \f
@@ -1095,7 +1058,7 @@ block[ \t]*data\\)\\>")
 (defun f90-get-correct-indent ()
   "Get correct indent for a line starting with line number.
 Does not check type and subprogram indentation."
-  (let ((epnt (f90-get-end-of-line)) icol cont)
+  (let ((epnt (line-end-position)) icol cont)
     (save-excursion
       (while (and (f90-previous-statement)
                  (or (progn
@@ -1105,7 +1068,7 @@ Does not check type and subprogram indentation."
       (setq icol (current-indentation))
       (beginning-of-line)
       (if (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
-                            (f90-get-end-of-line) t)
+                            (line-end-position) t)
          (progn
            (beginning-of-line) (skip-chars-forward " \t")
            (cond ((f90-looking-at-do)
@@ -1445,11 +1408,11 @@ If run in the middle of a line, the line is not broken."
          (progn
            (message "Indenting %s %s..."
                     (car program) (car (cdr program)))
-           (f90-indent-region (point) (mark))
+           (indent-region (point) (mark) nil)
            (message "Indenting %s %s...done"
                     (car program) (car (cdr program))))
        (message "Indenting the whole file...")
-       (f90-indent-region (point) (mark))
+       (indent-region (point) (mark) nil)
        (message "Indenting the whole file...done")))))
 
 ;; autofill and break-line
@@ -1458,20 +1421,20 @@ If run in the middle of a line, the line is not broken."
   (interactive)
   (let (ctype)
     (cond ((f90-in-string)
-          (insert "&") (newline) (insert "&"))
+          (insert "&") (newline 1) (insert "&"))
          ((f90-in-comment)
           (setq ctype (f90-get-present-comment-type))
-          (newline)
+          (newline 1)
           (insert ctype))
          (t (insert "&")
             (if (not no-update) (f90-update-line))
-            (newline)
+            (newline 1)
             (if f90-beginning-ampersand (insert "&")))))
-  (f90-indent-line))
+  (indent-according-to-mode))
   
 (defun f90-find-breakpoint ()
   "From fill-column, search backward for break-delimiter."
-  (let ((bol (f90-get-beg-of-line)))
+  (let ((bol (line-beginning-position)))
     (re-search-backward f90-break-delimiters bol)
     (if f90-break-before-delimiters
        (progn (backward-char)
@@ -1549,7 +1512,7 @@ If run in the middle of a line, the line is not broken."
 (defun f90-block-match (beg-block beg-name end-block end-name)
   "Match end-struct with beg-struct and complete end-block if possible.
 Leave point at the end of line."
-  (search-forward "end" (f90-get-end-of-line))
+  (search-forward "end" (line-end-position))
   (catch 'no-match
     (if (not (f90-equal-symbols beg-block end-block))
        (if end-block
@@ -1621,8 +1584,8 @@ Leave point at the end of line."
                      (message "Matches %s: %s"
                               (what-line)
                               (buffer-substring
-                               (progn (beginning-of-line) (point))
-                               (progn (end-of-line) (point))))
+                               (line-beginning-position)
+                               (line-end-position)))
                    (sit-for 1)))
              (setq beg-block (car matching-beg))
              (setq beg-name (car (cdr matching-beg)))
@@ -1651,7 +1614,7 @@ Any other key combination is executed normally."
          (setq c (event-to-character e)))
       (setq c (read-event)))
     ;; insert char if not equal to `?'
-    (if (or (= c ??) (eq c help-char))
+    (if (or (eq c ??) (eq c help-char))
        (f90-abbrev-help)
       (if (string-match "XEmacs" emacs-version)
          (setq unread-command-event e)
@@ -1734,6 +1697,12 @@ Any other key combination is executed normally."
                  (setq modified t))))
        (or modified (set-buffer-modified-p nil))))))
 
+
+(defun f90-current-defun ()
+  "Function to use for `add-log-current-defun-function' in F90 mode."
+  (save-excursion
+    (nth 1 (f90-beginning-of-subprogram))))
+
 (provide 'f90)
 
 ;;; f90.el ends here