]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/f90.el
Romain Francoise's and Ami Fischman's bugfixes.
[gnu-emacs] / lisp / progmodes / f90.el
index 3f8478c19f07e68bdba8716d77a93a10cf37c0ad..a3f44743514165951a3b522741ea1c16aa4f214f 100644 (file)
@@ -65,7 +65,7 @@
 ;; The function f90-comment-region toggles insertion of
 ;; the variable f90-comment-region in every line of the region.
 
-;; One common convention for free vs. fixed format is that free-format files
+;; One common convention for free vs. fixed format is that free format files
 ;; have the ending .f90 or .f95 while fixed format files have the ending .f.
 ;; Emacs automatically loads Fortran files in the appropriate mode based
 ;; on extension. You can modify this by adjusting the variable auto-mode-alist.
 ;; Also thanks to the authors of the fortran and pascal modes, on which some
 ;; of this code is built.
 
+;;; Code:
+
 ;; TODO
 ;; Support for hideshow, align.
 ;; OpenMP, preprocessor highlighting.
 
-;;; Code:
+(defvar comment-auto-fill-only-comments)
+(defvar font-lock-keywords)
 
 ;; User options
 
 (defgroup f90 nil
-  "Major mode for editing Fortran 90,95 code."
+  "Major mode for editing free format Fortran 90,95 code."
   :group 'languages)
 
 (defgroup f90-indent nil
-  "Indentation in free-format Fortran."
+  "Indentation in free format Fortran."
   :prefix "f90-"
-  :group 'f90)
+  :group  'f90)
 
 
 (defcustom f90-do-indent 3
   "*Extra indentation applied to DO blocks."
-  :type 'integer
+  :type  'integer
   :group 'f90-indent)
 
 (defcustom f90-if-indent 3
   "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
-  :type 'integer
+  :type  'integer
   :group 'f90-indent)
 
 (defcustom f90-type-indent 3
   "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks."
-  :type 'integer
+  :type  'integer
   :group 'f90-indent)
 
 (defcustom f90-program-indent 2
-  "*Extra indentation applied to PROGRAM/MODULE/SUBROUTINE/FUNCTION blocks."
-  :type 'integer
+  "*Extra indentation applied to PROGRAM, MODULE, SUBROUTINE, FUNCTION blocks."
+  :type  'integer
   :group 'f90-indent)
 
 (defcustom f90-continuation-indent 5
-  "*Extra indentation applied to F90 continuation lines."
-  :type 'integer
+  "*Extra indentation applied to continuation lines."
+  :type  'integer
   :group 'f90-indent)
 
 (defcustom f90-comment-region "!!$"
   "*String inserted by \\[f90-comment-region] at start of each line in region."
-  :type 'string
+  :type  'string
   :group 'f90-indent)
 
 (defcustom f90-indented-comment-re "!"
-  "*Regexp saying which comments to indent like code."
-  :type 'regexp
+  "*Regexp matching comments to indent as 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
+  :type  'regexp
   :group 'f90-indent)
 
 (defcustom f90-beginning-ampersand t
   "*Non-nil gives automatic insertion of \& at start of continuation line."
-  :type 'boolean
+  :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."
-  :type '(choice (const blink) (const no-blink) (const nil))
+  :type  '(choice (const blink) (const no-blink) (const nil))
   :group 'f90)
 
 (defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
   "*Regexp holding list of delimiters at which lines may be broken."
-  :type 'regexp
+  :type  'regexp
   :group 'f90)
 
 (defcustom f90-break-before-delimiters t
   "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
-  :type 'boolean
+  :type  'boolean
   :group 'f90)
 
 (defcustom f90-auto-keyword-case nil
   "*Automatic case conversion of keywords.
 The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil."
-  :type '(choice (const downcase-word) (const upcase-word)
-                (const capitalize-word) (const nil))
+  :type  '(choice (const downcase-word) (const upcase-word)
+                  (const capitalize-word) (const nil))
   :group 'f90)
 
 (defcustom f90-leave-line-no nil
   "*If non-nil, line numbers are not left justified."
-  :type 'boolean
+  :type  'boolean
   :group 'f90)
 
-(defconst f90-xemacs-flag (string-match "XEmacs\\|Lucid" emacs-version)
-  "Non-nil means F90 mode thinks it is running under XEmacs.")
+(defcustom f90-mode-hook nil
+  "Hook run when entering F90 mode."
+  :type    'hook
+  :options '(f90-add-imenu-menu)
+  :group   'f90)
+
+;; User options end here.
 
 (defconst f90-keywords-re
   (regexp-opt '("allocatable" "allocate" "assign" "assignment" "backspace"
@@ -339,12 +347,12 @@ The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil."
   (list
    ;; Special highlighting of "module procedure".
    '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face))
-   ;; Highlight declaration of derived type.
-;;;   '("\\<\\(type\\)[ \t]*\\(.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
-;;;     (1 font-lock-keyword-face) (3 font-lock-function-name-face))
+   ;; Highlight definition of derived type.
+   '("\\<\\(\\(?:end[ \t]*\\)?type\\)\\>\\([^()\n]*::\\)?[ \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+\\)?"
+subroutine\\)\\|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.")
@@ -355,8 +363,8 @@ subroutine\\|type\\)\\|use\\|call\\)\\>[ \t]*\\(\\sw+\\)?"
    (list
     ;; 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 t) (4 font-lock-variable-name-face))
+logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\([^&!\n]*\\)"
+      (1 font-lock-type-face t) (4 font-lock-variable-name-face t))
     ;; do, if, select, where, and forall constructs.
     '("\\<\\(end[ \t]*\\(do\\|if\\|select\\|forall\\|where\\)\\)\\>\
 \\([ \t]+\\(\\sw+\\)\\)?"
@@ -366,11 +374,12 @@ 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\\)\\>"
+\\|logical\\|type[ \t]*(\\sw+)\\|none\\)[ \t]*"
       (1 font-lock-keyword-face) (2 font-lock-type-face))
     '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
       (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
     "\\<else\\([ \t]*if\\|where\\)?\\>"
+    '("\\(&\\)[ \t]*\\(!\\|$\\)"  (1 font-lock-keyword-face))
     "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
     '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>"
       (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
@@ -398,7 +407,8 @@ do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
 
 (defvar f90-font-lock-keywords
   f90-font-lock-keywords-2
-  "*Default expressions to highlight in F90 mode.")
+  "*Default expressions to highlight in F90 mode.
+Can be overridden by the value of `font-lock-maximum-decoration'.")
 
 
 (defvar f90-mode-syntax-table
@@ -426,11 +436,15 @@ do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
     (define-key map "\C-\M-a"  'f90-beginning-of-subprogram)
     (define-key map "\C-\M-e"  'f90-end-of-subprogram)
     (define-key map "\C-\M-h"  'f90-mark-subprogram)
+    (define-key map "\C-\M-n"  'f90-end-of-block)
+    (define-key map "\C-\M-p"  'f90-beginning-of-block)
     (define-key map "\C-\M-q"  'f90-indent-subprogram)
     (define-key map "\C-j"     'f90-indent-new-line) ; LFD equals C-j
     (define-key map "\r"       'newline)
     (define-key map "\C-c\r"   'f90-break-line)
 ;;;  (define-key map [M-return] 'f90-break-line)
+    (define-key map "\C-c\C-a" 'f90-previous-block)
+    (define-key map "\C-c\C-e" 'f90-next-block)
     (define-key map "\C-c\C-d" 'f90-join-lines)
     (define-key map "\C-c\C-f" 'f90-fill-region)
     (define-key map "\C-c\C-p" 'f90-previous-statement)
@@ -442,165 +456,93 @@ do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
     (define-key map "-"        'f90-electric-insert)
     (define-key map "*"        'f90-electric-insert)
     (define-key map "/"        'f90-electric-insert)
+
+    (easy-menu-define f90-menu map "Menu for F90 mode."
+      `("F90"
+        ("Customization"
+         ,(custom-menu-create 'f90)
+         ["Set"  Custom-set t]
+         ["Save" Custom-save t]
+         ["Reset to Current" Custom-reset-current t]
+         ["Reset to Saved"   Custom-reset-saved t]
+         ["Reset to Standard Settings" Custom-reset-standard t]
+         )
+        "--"
+        ["Indent Subprogram"       f90-indent-subprogram       t]
+        ["Mark Subprogram"         f90-mark-subprogram         t]
+        ["Beginning of Subprogram" f90-beginning-of-subprogram t]
+        ["End of Subprogram"       f90-end-of-subprogram       t]
+        "--"
+        ["(Un)Comment Region" f90-comment-region mark-active]
+        ["Indent Region"      f90-indent-region  mark-active]
+        ["Fill Region"        f90-fill-region    mark-active]
+        "--"
+        ["Break Line at Point"     f90-break-line t]
+        ["Join with Previous Line" f90-join-lines t]
+        ["Insert Block End"        f90-insert-end t]
+        "--"
+        ("Highlighting"
+         ["Toggle font-lock-mode" font-lock-mode :selected font-lock-mode
+          :style toggle]
+         "--"
+         ["Light highlighting (level 1)"    f90-font-lock-1 t]
+         ["Moderate highlighting (level 2)" f90-font-lock-2 t]
+         ["Heavy highlighting (level 3)"    f90-font-lock-3 t]
+         ["Maximum highlighting (level 4)"  f90-font-lock-4 t]
+         )
+        ("Change Keyword Case"
+         ["Upcase Keywords (buffer)"     f90-upcase-keywords     t]
+         ["Capitalize Keywords (buffer)" f90-capitalize-keywords t]
+         ["Downcase Keywords (buffer)"   f90-downcase-keywords   t]
+         "--"
+         ["Upcase Keywords (region)"     f90-upcase-region-keywords
+          mark-active]
+         ["Capitalize Keywords (region)" f90-capitalize-region-keywords
+          mark-active]
+         ["Downcase Keywords (region)"   f90-downcase-region-keywords
+          mark-active]
+         )
+        "--"
+        ["Toggle auto-fill"   auto-fill-mode :selected auto-fill-function
+         :style toggle]
+        ["Toggle abbrev-mode" abbrev-mode    :selected abbrev-mode
+         :style toggle]
+        ["Add imenu Menu" f90-add-imenu-menu
+         :active   (not (lookup-key (current-local-map) [menu-bar index]))
+         :included (fboundp 'imenu-add-to-menubar)]))
     map)
   "Keymap used in F90 mode.")
 
-;; Menu bar support.
-(if f90-xemacs-flag
-    (defvar f90-xemacs-menu
-      '("F90"
-       ["Indent Subprogram"       f90-indent-subprogram t]
-       ["Mark Subprogram"         f90-mark-subprogram t]
-       ["Beginning of Subprogram" f90-beginning-of-subprogram t]
-       ["End of Subprogram"       f90-end-of-subprogram t]
-       "-----"
-       ["(Un)Comment Region"      f90-comment-region t]
-       ["Indent Region"           indent-region t]
-       ["Fill Region"             f90-fill-region t]
-       "-----"
-       ["Break Line at Point"     f90-break-line t]
-       ["Join with Next Line"     f90-join-lines t]
-       ["Insert Newline"          newline t]
-       ["Insert Block End"        f90-insert-end t]
-       "-----"
-       ["Upcase Keywords (buffer)"      f90-upcase-keywords t]
-       ["Upcase Keywords (region)"      f90-upcase-region-keywords t]
-       ["Capitalize Keywords (buffer)"  f90-capitalize-keywords t]
-       ["Capitalize Keywords (region)"  f90-capitalize-region-keywords t]
-       ["Downcase Keywords (buffer)"    f90-downcase-keywords t]
-       ["Downcase Keywords (region)"    f90-downcase-region-keywords t]
-       "-----"
-       ["Toggle abbrev-mode"   abbrev-mode t]
-       ["Toggle auto-fill"     auto-fill-mode t])
-      "XEmacs menu for F90 mode.")
-
-  ;; Emacs.
-  (defvar f90-menu-bar-menu
-    (let ((map (make-sparse-keymap "F90")))
-      (define-key map [f90-imenu-menu]
-        '("Add imenu Menu" . f90-add-imenu-menu))
-      (define-key map [abbrev-mode]
-        '("Toggle abbrev-mode" . abbrev-mode))
-      (define-key map [auto-fill-mode]
-        '("Toggle auto-fill" . auto-fill-mode))
-      (define-key map [line1] '("--"))
-      (define-key map [f90-change-case-menu]
-        '("Change Keyword Case" . f90-change-case-menu))
-      (define-key map [f90-font-lock-menu]
-        '("Highlighting" . f90-font-lock-menu))
-      (define-key map [line2] '("--"))
-      (define-key map [f90-insert-end]
-        '("Insert Block End" . f90-insert-end))
-      (define-key map [f90-join-lines]
-        '("Join with Next Line" . f90-join-lines))
-      (define-key map [f90-break-line]
-        '("Break Line at Point" . f90-break-line))
-      (define-key map [line3] '("--"))
-      (define-key map [f90-fill-region]
-        '("Fill Region" . f90-fill-region))
-      (put 'f90-fill-region 'menu-enable 'mark-active)
-      (define-key map [indent-region]
-        '("Indent Region" . indent-region))
-      (define-key map [f90-comment-region]
-        '("(Un)Comment Region" . f90-comment-region))
-      (put 'f90-comment-region 'menu-enable 'mark-active)
-      (define-key map [line4] '("--"))
-      (define-key map [f90-end-of-subprogram]
-        '("End of Subprogram" . f90-end-of-subprogram))
-      (define-key map [f90-beginning-of-subprogram]
-        '("Beginning of Subprogram" . f90-beginning-of-subprogram))
-      (define-key map [f90-mark-subprogram]
-        '("Mark Subprogram" . f90-mark-subprogram))
-      (define-key map [f90-indent-subprogram]
-        '("Indent Subprogram" . f90-indent-subprogram))
-      map)
-    "F90 mode top-level menu bar menu.")
-
-  (define-key f90-mode-map [menu-bar f90-menu]
-    (cons "F90" f90-menu-bar-menu))
-
-  (defvar f90-change-case-menu
-    (let ((map (make-sparse-keymap "Change Keyword Case")))
-      (define-key map [dkr]
-        '("Downcase Keywords (region)" . f90-downcase-region-keywords))
-      (put 'f90-downcase-region-keywords 'menu-enable 'mark-active)
-      (define-key map [ckr]
-        '("Capitalize Keywords (region)" . f90-capitalize-region-keywords))
-      (put 'f90-capitalize-region-keywords 'menu-enable 'mark-active)
-      (define-key map [ukr]
-        '("Upcase Keywords (region)" . f90-upcase-region-keywords))
-      (put 'f90-upcase-region-keywords 'menu-enable 'mark-active)
-      (define-key map [line] '("--"))
-      (define-key map [dkb]
-        '("Downcase Keywords (buffer)" . f90-downcase-keywords))
-      (define-key map [ckb]
-        '("Capitalize Keywords (buffer)" . f90-capitalize-keywords))
-      (define-key map [ukb]
-        '("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)
-  (put 'f90-font-lock-on 'menu-enable 'font-lock-mode)
-  (put 'f90-font-lock-on 'menu-alias t)
-
-  (defalias 'f90-font-lock-off 'font-lock-mode)
-  (put 'f90-font-lock-off 'menu-enable '(not font-lock-mode))
-  (put 'f90-font-lock-off 'menu-alias t)
-
-  (defun f90-font-lock-1 ()
-    "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
-    (interactive)
-    (font-lock-mode 1)
-    (setq font-lock-keywords f90-font-lock-keywords-1)
-    (font-lock-fontify-buffer))
-
-  (defun f90-font-lock-2 ()
-    "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
-    (interactive)
-    (font-lock-mode 1)
-    (setq font-lock-keywords f90-font-lock-keywords-2)
-    (font-lock-fontify-buffer))
-
-  (defun f90-font-lock-3 ()
-    "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
-    (interactive)
-    (font-lock-mode 1)
-    (setq font-lock-keywords f90-font-lock-keywords-3)
-    (font-lock-fontify-buffer))
-
-  (defun f90-font-lock-4 ()
-    "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
-    (interactive)
-    (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]
-        '("Maximum highlighting (level 4)" . f90-font-lock-4))
-      (define-key map [h3]
-        '("Heavy highlighting (level 3)" . f90-font-lock-3))
-      (define-key map [h2]
-        '("Default highlighting (level 2)" . f90-font-lock-2))
-      (define-key map [h1]
-        '("Light highlighting (level 1)" . f90-font-lock-1))
-      (define-key map [line] '("--"))
-      (define-key map [floff]
-        '("Turn off font-lock-mode" . f90-font-lock-on))
-      (define-key map [flon]
-        '("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)
-
-  )
 
+(defun f90-font-lock-1 ()
+  "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
+  (interactive)
+  (font-lock-mode 1)
+  (setq font-lock-keywords f90-font-lock-keywords-1)
+  (font-lock-fontify-buffer))
+
+(defun f90-font-lock-2 ()
+  "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
+  (interactive)
+  (font-lock-mode 1)
+  (setq font-lock-keywords f90-font-lock-keywords-2)
+  (font-lock-fontify-buffer))
+
+(defun f90-font-lock-3 ()
+  "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
+  (interactive)
+  (font-lock-mode 1)
+  (setq font-lock-keywords f90-font-lock-keywords-3)
+  (font-lock-fontify-buffer))
+
+(defun f90-font-lock-4 ()
+  "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
+  (interactive)
+  (font-lock-mode 1)
+  (setq font-lock-keywords f90-font-lock-keywords-4)
+  (font-lock-fontify-buffer))
+
+\f
 ;; Regexps for finding program structures.
 (defconst f90-blocks-re
   (concat "\\(block[ \t]*data\\|"
@@ -628,8 +570,8 @@ do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
   "Regexp matching the end of a TYPE, INTERFACE, BLOCK DATA section.")
 
 (defconst f90-type-def-re
-  "\\<\\(type\\)\\([^(\n]*\\)\\(::\\)?[ \t]*\\b\\(\\sw+\\)"
-  "Regexp matching the declaration of a variable of derived type.")
+  "\\<\\(type\\)\\>\\(?:[^()\n]*::\\)?[ \t]*\\(\\sw+\\)"
+  "Regexp matching the definition of a derived type.")
 
 (defconst f90-no-break-re
   (regexp-opt '("**" "//" "=>") 'paren)
@@ -639,10 +581,6 @@ do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
   "Temporary position used to speed up region operations.")
 (make-variable-buffer-local 'f90-cache-position)
 
-(defvar f90-imenu-flag nil
-  "Non-nil means this buffer already has an imenu.")
-(make-variable-buffer-local 'f90-imenu-flag)
-
 \f
 ;; Imenu support.
 (defvar f90-imenu-generic-expression
@@ -669,98 +607,96 @@ do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
        "\\)"
        "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
       4)))
-  "Generic imenu expression for F90 mode.")
+  "Value for `imenu-generic-expression' in F90 mode.")
 
 (defun f90-add-imenu-menu ()
   "Add an imenu menu to the menubar."
   (interactive)
-  (if f90-imenu-flag
+  (if (lookup-key (current-local-map) [menu-bar index])
       (message "%s" "F90-imenu already exists.")
     (imenu-add-to-menubar "F90-imenu")
-    (redraw-frame (selected-frame))
-    (setq f90-imenu-flag t)))
-
-(put 'f90-add-imenu-menu 'menu-enable '(not f90-imenu-flag))
-
-
-;; When compiling under GNU Emacs, load imenu during compilation.
-;; If you have 19.22 or earlier, comment this out, or get imenu.
-(or f90-xemacs-flag (eval-when-compile (require 'imenu)))
+    (redraw-frame (selected-frame))))
 
 \f
 ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
 (defvar f90-mode-abbrev-table
   (let (abbrevs-changed)
-    (define-abbrev-table 'f90-mode-abbrev-table 
-      '(("`al"  "allocate"      nil 0 t)
-        ("`ab"  "allocatable"   nil 0 t)
-        ("`as"  "assignment"    nil 0 t)
-        ("`ba"  "backspace"     nil 0 t)
-        ("`bd"  "block data"    nil 0 t)
-        ("`c"   "character"     nil 0 t)
-        ("`cl"  "close"         nil 0 t)
-        ("`cm"  "common"        nil 0 t)
-        ("`cx"  "complex"       nil 0 t)
-        ("`cn"  "contains"      nil 0 t)
-        ("`cy"  "cycle"         nil 0 t)
-        ("`de"  "deallocate"    nil 0 t)
-        ("`df"  "define"        nil 0 t)
-        ("`di"  "dimension"     nil 0 t)
-        ("`dw"  "do while"      nil 0 t)
-        ("`el"  "else"          nil 0 t)
-        ("`eli" "else if"       nil 0 t)
-        ("`elw" "elsewhere"     nil 0 t)
-        ("`eq"  "equivalence"   nil 0 t)
-        ("`ex"  "external"      nil 0 t)
-        ("`ey"  "entry"         nil 0 t)
-        ("`fl"  "forall"        nil 0 t)
-        ("`fo"  "format"        nil 0 t)
-        ("`fu"  "function"      nil 0 t)
-        ("`fa"  ".false."       nil 0 t)
-        ("`im"  "implicit none" nil 0 t)
-        ("`in " "include"       nil 0 t)
-        ("`i"   "integer"       nil 0 t)
-        ("`it"  "intent"        nil 0 t)
-        ("`if"  "interface"     nil 0 t)
-        ("`lo"  "logical"       nil 0 t)
-        ("`mo"  "module"        nil 0 t)
-        ("`na"  "namelist"      nil 0 t)
-        ("`nu"  "nullify"       nil 0 t)
-        ("`op"  "optional"      nil 0 t)
-        ("`pa"  "parameter"     nil 0 t)
-        ("`po"  "pointer"       nil 0 t)
-        ("`pr"  "print"         nil 0 t)
-        ("`pi"  "private"       nil 0 t)
-        ("`pm"  "program"       nil 0 t)
-        ("`pu"  "public"        nil 0 t)
-        ("`r"   "real"          nil 0 t)
-        ("`rc"  "recursive"     nil 0 t)
-        ("`rt"  "return"        nil 0 t)
-        ("`rw"  "rewind"        nil 0 t)
-        ("`se"  "select"        nil 0 t)
-        ("`sq"  "sequence"      nil 0 t)
-        ("`su"  "subroutine"    nil 0 t)
-        ("`ta"  "target"        nil 0 t)
-        ("`tr"  ".true."        nil 0 t)
-        ("`t"   "type"          nil 0 t)
-        ("`wh"  "where"         nil 0 t)
-        ("`wr"  "write"         nil 0 t)))
+    (define-abbrev-table 'f90-mode-abbrev-table nil)
+    ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
+    ;; A little baroque to quieten the byte-compiler.
+    (mapcar
+     (function (lambda (element)
+                 (condition-case nil
+                     (apply 'define-abbrev f90-mode-abbrev-table
+                            (append element '(nil 0 t)))
+                   (wrong-number-of-arguments
+                    (apply 'define-abbrev f90-mode-abbrev-table
+                           (append element '(nil 0)))))))
+     '(("`al"  "allocate"     )
+       ("`ab"  "allocatable"  )
+       ("`as"  "assignment"   )
+       ("`ba"  "backspace"    )
+       ("`bd"  "block data"   )
+       ("`c"   "character"    )
+       ("`cl"  "close"        )
+       ("`cm"  "common"       )
+       ("`cx"  "complex"      )
+       ("`cn"  "contains"     )
+       ("`cy"  "cycle"        )
+       ("`de"  "deallocate"   )
+       ("`df"  "define"       )
+       ("`di"  "dimension"    )
+       ("`dw"  "do while"     )
+       ("`el"  "else"         )
+       ("`eli" "else if"      )
+       ("`elw" "elsewhere"    )
+       ("`eq"  "equivalence"  )
+       ("`ex"  "external"     )
+       ("`ey"  "entry"        )
+       ("`fl"  "forall"       )
+       ("`fo"  "format"       )
+       ("`fu"  "function"     )
+       ("`fa"  ".false."      )
+       ("`im"  "implicit none")
+       ("`in"  "include"      )
+       ("`i"   "integer"      )
+       ("`it"  "intent"       )
+       ("`if"  "interface"    )
+       ("`lo"  "logical"      )
+       ("`mo"  "module"       )
+       ("`na"  "namelist"     )
+       ("`nu"  "nullify"      )
+       ("`op"  "optional"     )
+       ("`pa"  "parameter"    )
+       ("`po"  "pointer"      )
+       ("`pr"  "print"        )
+       ("`pi"  "private"      )
+       ("`pm"  "program"      )
+       ("`pu"  "public"       )
+       ("`r"   "real"         )
+       ("`rc"  "recursive"    )
+       ("`rt"  "return"       )
+       ("`rw"  "rewind"       )
+       ("`se"  "select"       )
+       ("`sq"  "sequence"     )
+       ("`su"  "subroutine"   )
+       ("`ta"  "target"       )
+       ("`tr"  ".true."       )
+       ("`t"   "type"         )
+       ("`wh"  "where"        )
+       ("`wr"  "write"        )))
     f90-mode-abbrev-table)
   "Abbrev table for F90 mode.")
 \f
-(defcustom f90-mode-hook nil
-  "Hook run when entering F90 mode."
-  :type 'hook
-  :options '(f90-add-imenu-menu)
-  :group 'f90)
 
 ;;;###autoload
 (defun f90-mode ()
   "Major mode for editing Fortran 90,95 code in free format.
+For fixed format code, use `fortran-mode'.
 
+\\[f90-indent-line] indents the current line.
 \\[f90-indent-new-line] indents current line and creates a new\
  indented line.
-\\[f90-indent-line] indents the current line.
 \\[f90-indent-subprogram] indents the current subprogram.
 
 Type `? or `\\[help-command] to display a list of built-in\
@@ -829,25 +765,18 @@ with no args, if that value is non-nil."
   (set (make-local-variable 'abbrev-all-caps) t)
   (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
   (setq indent-tabs-mode nil)           ; auto buffer local
-  ;; Setting up things for font-lock.
-  (when f90-xemacs-flag
-    (put 'f90-mode 'font-lock-keywords-case-fold-search t)
-    (when (and (featurep 'menubar)
-               current-menubar
-               (not (assoc "F90" current-menubar)))
-      (set-buffer-menubar (copy-sequence current-menubar))
-      (add-submenu nil f90-xemacs-menu)))
-  ;; XEmacs: Does not need a special case, since both emacsen work alike -sb.
   (set (make-local-variable '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)
   (set (make-local-variable 'imenu-generic-expression)
-        f90-imenu-generic-expression)
+       f90-imenu-generic-expression)
+  (set (make-local-variable 'beginning-of-defun-function)
+       'f90-beginning-of-subprogram)
+  (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
   (set (make-local-variable 'add-log-current-defun-function)
        #'f90-current-defun)
   (run-hooks 'f90-mode-hook))
@@ -888,6 +817,14 @@ not the last line of a continued statement."
     (skip-chars-backward " \t")
     (= (preceding-char) ?&)))
 
+;; GM this is not right, eg a continuation line starting with a number.
+;; Need f90-code-start-position function.
+;; And yet, things seems to work with this...
+;; cf f90-indent-line
+;;     (beginning-of-line)           ; digits after & \n are not line-nos
+;;     (if (not (save-excursion (and (f90-previous-statement)
+;;                                   (f90-line-continued))))
+;;         (f90-indent-line-no)
 (defsubst f90-current-indentation ()
   "Return indentation of current line.
 Line-numbers are considered whitespace characters."
@@ -898,12 +835,11 @@ Line-numbers are considered whitespace characters."
 If optional argument NO-LINE-NUMBER is nil, jump over a possible
 line-number before indenting."
   (beginning-of-line)
-  (if (not no-line-number)
+  (or no-line-number
       (skip-chars-forward " \t0-9"))
   (delete-horizontal-space)
-  (if (zerop (current-column))
-      (indent-to col)
-    (indent-to col 1)))                 ; leave >= 1 space after line number
+  ;; Leave >= 1 space after line number.
+  (indent-to col (if (zerop (current-column)) 0 1)))
 
 (defsubst f90-get-present-comment-type ()
   "If point lies within a comment, return the string starting the comment.
@@ -921,22 +857,18 @@ For example, \"!\" or \"!!\"."
   (equal (if a (downcase a) nil)
          (if b (downcase b) nil)))
 
-;; XEmacs 19.11 & 19.12 return a single char when matching an empty regexp.
-;; The next 2 functions are therefore longer than necessary.
 (defsubst f90-looking-at-do ()
   "Return (\"do\" NAME) if a do statement starts after point.
 NAME is nil if the statement has no label."
   (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\>")
-      (list (match-string 3)
-            (if (looking-at "\\(\\sw+\\)[ \t]*:") (match-string 1)))))
+      (list (match-string 3) (match-string 2))))
 
 (defsubst f90-looking-at-select-case ()
   "Return (\"select\" NAME) if a select-case statement starts after point.
 NAME is nil if the statement has no label."
   (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
 \\(select\\)[ \t]*case[ \t]*(")
-      (list (match-string 3)
-            (if (looking-at "\\(\\sw+\\)[ \t]*:") (match-string 1)))))
+      (list (match-string 3) (match-string 2))))
 
 (defsubst f90-looking-at-if-then ()
   "Return (\"if\" NAME) if an if () then statement starts after point.
@@ -944,7 +876,7 @@ NAME is nil if the statement has no label."
   (save-excursion
     (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\>")
       (let ((struct (match-string 3))
-            (label (if (looking-at "\\(\\sw+\\)[ \t]*:") (match-string 1)))
+            (label (match-string 2))
             (pos (scan-lists (point) 1 0)))
         (and pos (goto-char pos))
         (skip-chars-forward " \t")
@@ -955,14 +887,14 @@ NAME is nil if the statement has no label."
                   (looking-at "then\\>")))
             (list struct label))))))
 
-(defun f90-looking-at-where-or-forall ()
+(defsubst f90-looking-at-where-or-forall ()
   "Return (KIND NAME) if a where or forall block starts after point.
 NAME is nil if the statement has no label."
   (save-excursion
     (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
 \\(where\\|forall\\)\\>")
       (let ((struct (match-string 3))
-            (label (if (looking-at "\\(\\sw+\\)[ \t]*:") (match-string 1)))
+            (label (match-string 2))
             (pos (scan-lists (point) 1 0)))
         (and pos (goto-char pos))
         (skip-chars-forward " \t")
@@ -973,7 +905,7 @@ NAME is nil if the statement has no label."
 NAME is non-nil only for type."
   (cond
    ((looking-at f90-type-def-re)
-    (list (match-string 1) (match-string 4)))
+    (list (match-string 1) (match-string 2)))
    ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
     (list (match-string 1) nil))))
 
@@ -986,8 +918,8 @@ NAME is non-nil only for type."
         (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
     (list (match-string 1) (match-string 2)))
    ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
-        (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)\
-[ \t]+\\(\\sw+\\)"))
+        (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
+\\(\\sw+\\)"))
     (list (match-string 1) (match-string 2)))))
 
 (defsubst f90-looking-at-program-block-end ()
@@ -1037,24 +969,24 @@ Comment lines embedded amongst continued lines return 'middle."
   "If `f90-leave-line-no' is nil, left-justify a line number.
 Leaves point at the first non-blank character after the line number.
 Call from beginning of line."
-  (if (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]"))
-      (delete-horizontal-space))
+  (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
+       (delete-horizontal-space))
   (skip-chars-forward " \t0-9"))
 
 (defsubst f90-no-block-limit ()
   "Return nil if point is at the edge of a code block.
 Searches line forward for \"function\" or \"subroutine\",
 if all else fails."
-  (let ((eol (line-end-position)))
-    (save-excursion
-      (not (or (looking-at "end")
-              (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
+  (save-excursion
+    (not (or (looking-at "end")
+             (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
 \\|select[ \t]*case\\|case\\|where\\|forall\\)\\>")
-              (looking-at "\\(program\\|module\\|interface\\|\
+             (looking-at "\\(program\\|module\\|interface\\|\
 block[ \t]*data\\)\\>")
-              (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
-              (looking-at f90-type-def-re)
-              (re-search-forward "\\(function\\|subroutine\\)" eol t))))))
+             (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
+             (looking-at f90-type-def-re)
+             (re-search-forward "\\(function\\|subroutine\\)"
+                                (line-end-position) t)))))
 
 (defsubst f90-update-line ()
   "Change case of current line as per `f90-auto-keyword-case'."
@@ -1185,15 +1117,12 @@ Return nil if no later statement is found."
     not-last-statement))
 
 (defun f90-beginning-of-subprogram ()
-  "Move point to the beginning of subprogram.
+  "Move point to the beginning of the current subprogram.
 Return (TYPE NAME), or nil if not found."
   (interactive)
   (let ((count 1) (case-fold-search t) matching-beg)
     (beginning-of-line)
-    (skip-chars-forward " \t0-9")
-    (if (setq matching-beg (f90-looking-at-program-block-start))
-       (setq count (1- count)))
-    (while (and (not (zerop count))
+    (while (and (> count 0)
                (re-search-backward f90-program-block-re nil 'move))
       (beginning-of-line)
       (skip-chars-forward " \t0-9")
@@ -1208,16 +1137,14 @@ Return (TYPE NAME), or nil if not found."
       nil)))
 
 (defun f90-end-of-subprogram ()
-  "Move point to the end of subprogram.
+  "Move point to the end of the current subprogram.
 Return (TYPE NAME), or nil if not found."
   (interactive)
-  (let ((count 1) (case-fold-search t) matching-end)
-    (beginning-of-line)
-    (skip-chars-forward " \t0-9")
-    (if (setq matching-end (f90-looking-at-program-block-end))
-       (setq count (1- count)))
+  (let ((case-fold-search t)
+        (count 1) 
+        matching-end)
     (end-of-line)
-    (while (and (not (zerop count))
+    (while (and (> count 0)
                (re-search-forward f90-program-block-re nil 'move))
       (beginning-of-line)
       (skip-chars-forward " \t0-9")
@@ -1226,38 +1153,165 @@ Return (TYPE NAME), or nil if not found."
            ((setq matching-end (f90-looking-at-program-block-end))
             (setq count (1- count))))
       (end-of-line))
-    (forward-line 1)
+    ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
+    ;; has a net non-zero effect, which seems odd.
+;;;    (forward-line 1)
     (if (zerop count)
        matching-end
       (message "No end found.")
       nil)))
 
-(defvar f90-mark-subprogram-overlay nil
-  "Used internally by `f90-mark-subprogram' to highlight the subprogram.")
-(make-variable-buffer-local 'f90-mark-subprogram-overlay)
+
+(defun f90-end-of-block (&optional num)
+  "Move point forward to the end of the current code block.
+With optional argument NUM, go forward that many balanced blocks.
+If NUM is negative, go backward to the start of a block.
+Checks for consistency of block types and labels (if present),
+and completes outermost block if necessary."
+  (interactive "p")
+  (if (and num (< num 0)) (f90-beginning-of-block (- num)))
+  (let ((f90-smart-end nil)             ; for the final `f90-match-end'
+        (case-fold-search t)
+        (count (or num 1))
+        start-list start-this start-type start-label end-type end-label)
+    (if (interactive-p) (push-mark (point) t))
+    (end-of-line)                       ; probably want this
+    (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
+      (beginning-of-line)
+      (skip-chars-forward " \t0-9")
+      (cond ((or (f90-in-string) (f90-in-comment)))
+            ((setq start-this
+                   (or
+                    (f90-looking-at-do)
+                    (f90-looking-at-select-case)
+                    (f90-looking-at-type-like)
+                    (f90-looking-at-program-block-start)
+                    (f90-looking-at-if-then)
+                    (f90-looking-at-where-or-forall)))
+             (setq start-list (cons start-this start-list) ; not add-to-list!
+                   count (1+ count)))
+            ((looking-at (concat "end[ \t]*" f90-blocks-re
+                                 "[ \t]*\\(\\sw+\\)?"))
+             (setq end-type (match-string 1)
+                   end-label (match-string 2)
+                   count (1- count))
+             ;; Check any internal blocks.
+             (when start-list
+               (setq start-this (car start-list)
+                     start-list (cdr start-list)
+                     start-type (car start-this)
+                     start-label (cadr start-this))
+               (or (f90-equal-symbols start-type end-type)
+                   (error "End type `%s' does not match start type `%s'"
+                          end-type start-type))
+               (or (f90-equal-symbols start-label end-label)
+                   (error "End label `%s' does not match start label `%s'"
+                          end-label start-label)))))
+      (end-of-line))
+    (if (> count 0) (error "Missing block end"))
+    ;; Check outermost block.
+    (if (interactive-p)
+        (save-excursion
+          (beginning-of-line)
+          (skip-chars-forward " \t0-9")
+          (f90-match-end)))))
+
+(defun f90-beginning-of-block (&optional num)
+  "Move point backwards to the start of the current code block.
+With optional argument NUM, go backward that many balanced blocks.
+If NUM is negative, go forward to the end of a block.
+Checks for consistency of block types and labels (if present).
+Does not check the outermost block, because it may be incomplete."
+  (interactive "p")
+  (if (and num (< num 0)) (f90-end-of-block (- num)))
+  (let ((case-fold-search t)
+        (count (or num 1))
+        end-list end-this end-type end-label
+        start-this start-type start-label)
+    (if (interactive-p) (push-mark (point) t))
+    (beginning-of-line)                 ; probably want this
+    (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
+      (beginning-of-line)
+      (skip-chars-forward " \t0-9")
+      (cond ((or (f90-in-string) (f90-in-comment)))
+            ((looking-at (concat "end[ \t]*" f90-blocks-re
+                                 "[ \t]*\\(\\sw+\\)?"))
+             (setq end-list (cons (list (match-string 1) (match-string 2))
+                                  end-list)
+                   count (1+ count)))
+            ((setq start-this
+                   (or
+                    (f90-looking-at-do)
+                    (f90-looking-at-select-case)
+                    (f90-looking-at-type-like)
+                    (f90-looking-at-program-block-start)
+                    (f90-looking-at-if-then)
+                    (f90-looking-at-where-or-forall)))
+             (setq start-type (car start-this)
+                   start-label (cadr start-this)
+                   count (1- count))
+             ;; Check any internal blocks.
+             (when end-list
+               (setq end-this (car end-list)
+                     end-list (cdr end-list)
+                     end-type (car end-this)
+                     end-label (cadr end-this))
+               (or (f90-equal-symbols start-type end-type)
+                   (error "Start type `%s' does not match end type `%s'"
+                          start-type end-type))
+               (or (f90-equal-symbols start-label end-label)
+                   (error "Start label `%s' does not match end label `%s'"
+                          start-label end-label))))))
+     (if (> count 0) (error "Missing block start"))))
+
+(defun f90-next-block (&optional num)
+  "Move point forward to the next end or start of a code block.
+With optional argument NUM, go forward that many blocks.
+If NUM is negative, go backwards.
+A block is a subroutine, if-endif, etc."
+  (interactive "p")
+  (let ((case-fold-search t)
+        (count (if num (abs num) 1)))
+    (while (and (> count 0)
+                (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
+                  (re-search-backward f90-blocks-re nil 'move)))
+      (beginning-of-line)
+      (skip-chars-forward " \t0-9")
+      (cond ((or (f90-in-string) (f90-in-comment)))
+            ((or
+              (looking-at "end[ \t]*")
+              (f90-looking-at-do)
+              (f90-looking-at-select-case)
+              (f90-looking-at-type-like)
+              (f90-looking-at-program-block-start)
+              (f90-looking-at-if-then)
+              (f90-looking-at-where-or-forall))
+             (setq count (1- count))))
+      (if (> num 0) (end-of-line)
+        (beginning-of-line)))))
+
+
+(defun f90-previous-block (&optional num)
+  "Move point backward to the previous end or start of a code block.
+With optional argument NUM, go backward that many blocks.
+If NUM is negative, go forwards.
+A block is a subroutine, if-endif, etc."
+  (interactive "p")
+  (f90-next-block (- (or num 1))))
+
 
 (defun f90-mark-subprogram ()
-  "Put mark at end of F90 subprogram, point at beginning, push marks.
-If called interactively, highlight the subprogram with the face `highlight'.
-Call again to remove the highlighting."
+  "Put mark at end of F90 subprogram, point at beginning, push mark."
   (interactive)
   (let ((pos (point)) program)
     (f90-end-of-subprogram)
-    (push-mark (point) t)
+    (push-mark)
     (goto-char pos)
     (setq program (f90-beginning-of-subprogram))
-    ;; The keywords in the preceding lists assume case-insensitivity.
-    (if f90-xemacs-flag
-       (zmacs-activate-region)
+    (if (fboundp 'zmacs-activate-region)
+        (zmacs-activate-region)
       (setq mark-active t
-            deactivate-mark nil)
-      (if (interactive-p)
-         (if (overlayp f90-mark-subprogram-overlay)
-             (if (overlay-buffer f90-mark-subprogram-overlay)
-                 (delete-overlay f90-mark-subprogram-overlay)
-               (move-overlay f90-mark-subprogram-overlay (point) (mark)))
-           (setq f90-mark-subprogram-overlay (make-overlay (point) (mark)))
-           (overlay-put f90-mark-subprogram-overlay 'face 'highlight))))
+            deactivate-mark nil))
     program))
 
 (defun f90-comment-region (beg-region end-region)
@@ -1265,15 +1319,14 @@ Call again to remove the highlighting."
 Insert the variable `f90-comment-region' at the start of every line
 in the region, or, if already present, remove it."
   (interactive "*r")
-  (let ((end (make-marker)))
-    (set-marker end end-region)
+  (let ((end (copy-marker end-region)))
     (goto-char beg-region)
     (beginning-of-line)
     (if (looking-at (regexp-quote f90-comment-region))
        (delete-region (point) (match-end 0))
       (insert f90-comment-region))
     (while (and (zerop (forward-line 1))
-               (< (point) (marker-position end)))
+               (< (point) end))
       (if (looking-at (regexp-quote f90-comment-region))
          (delete-region (point) (match-end 0))
        (insert f90-comment-region)))
@@ -1283,58 +1336,57 @@ in the region, or, if already present, remove it."
   "Indent current line as F90 code.
 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
 after indenting."
-  (interactive)
-  (let (indent no-line-number (pos (make-marker)) (case-fold-search t))
-    (set-marker pos (point))
-    (beginning-of-line)                        ; digits after & \n are not line-nos
-    (if (save-excursion (and (f90-previous-statement) (f90-line-continued)))
-       (progn (setq no-line-number t) (skip-chars-forward " \t"))
-      (f90-indent-line-no))
+  (interactive "*P")
+  (let ((case-fold-search t)
+        (pos (point-marker))
+        indent no-line-number)
+    (beginning-of-line)           ; digits after & \n are not line-nos
+    (if (not (save-excursion (and (f90-previous-statement)
+                                  (f90-line-continued))))
+        (f90-indent-line-no)
+      (setq no-line-number t)
+      (skip-chars-forward " \t"))
     (if (looking-at "!")
        (setq indent (f90-comment-indent))
-      (if (and (looking-at "end") f90-smart-end)
-          (f90-match-end))
+      (and f90-smart-end (looking-at "end")
+           (f90-match-end))
       (setq indent (f90-calculate-indent)))
-    (if (not (zerop (- indent (current-column))))
+    (or (= indent (current-column))
         (f90-indent-to indent no-line-number))
     ;; If initial point was within line's indentation,
     ;; position after the indentation.  Else stay at same point in text.
-    (if (< (point) (marker-position pos))
-       (goto-char (marker-position pos)))
+    (and (< (point) pos)
+         (goto-char pos))
     (if auto-fill-function
-        (f90-do-auto-fill)              ; also updates line
-      (if (not no-update) (f90-update-line)))
+        ;; GM NO-UPDATE not honoured, since this calls f90-update-line.
+        (f90-do-auto-fill)
+      (or no-update (f90-update-line)))
     (set-marker pos nil)))
 
 (defun f90-indent-new-line ()
-  "Reindent current line, insert a newline and indent the newline.
+  "Re-indent current line, insert a newline and indent the newline.
 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
 If run in the middle of a line, the line is not broken."
-  (interactive)
-  (let (string cont (case-fold-search t))
-    (if abbrev-mode (expand-abbrev))
-    (beginning-of-line)                        ; reindent where likely to be needed
-    (f90-indent-line-no)
-    (f90-indent-line 'no-update)
-    (end-of-line)
-    (delete-horizontal-space)          ; destroy trailing whitespace
-    (setq string (f90-in-string)
-          cont (f90-line-continued))
-    (if (and string (not cont)) (insert "&"))
-    (f90-update-line)
+  (interactive "*")
+  (if abbrev-mode (expand-abbrev))
+  (beginning-of-line)             ; reindent where likely to be needed
+  (f90-indent-line)                ; calls indent-line-no, update-line
+  (end-of-line)
+  (delete-horizontal-space)            ; destroy trailing whitespace
+  (let ((string (f90-in-string))
+        (cont (f90-line-continued)))
+    (and string (not cont) (insert "&"))
     (newline)
-    (if (or string (and cont f90-beginning-ampersand)) (insert "&"))
-    (f90-indent-line 'no-update)))
+    (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
+  (f90-indent-line 'no-update))         ; nothing to update
 
 
 (defun f90-indent-region (beg-region end-region)
   "Indent every line in region by forward parsing."
   (interactive "*r")
-  (let ((end-region-mark (make-marker))
+  (let ((end-region-mark (copy-marker end-region))
         (save-point (point-marker))
-       block-list ind-lev ind-curr ind-b cont
-       struct beg-struct end-struct)
-    (set-marker end-region-mark end-region)
+       block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
     (goto-char beg-region)
     ;; First find a line which is not a continuation line or comment.
     (beginning-of-line)
@@ -1362,8 +1414,8 @@ If run in the middle of a line, the line is not broken."
                       f90-if-indent)
                      ((setq struct (f90-looking-at-type-like))
                       f90-type-indent)
-                     ((or(setq struct (f90-looking-at-program-block-start))
-                         (looking-at "contains[ \t]*\\($\\|!\\)"))
+                     ((or (setq struct (f90-looking-at-program-block-start))
+                           (looking-at "contains[ \t]*\\($\\|!\\)"))
                       f90-program-indent)))
     (if ind-b (setq ind-lev (+ ind-lev ind-b)))
     (if struct (setq block-list (cons struct block-list)))
@@ -1371,8 +1423,8 @@ If run in the middle of a line, the line is not broken."
                (< (point) end-region-mark))
       (if (looking-at "[ \t]*!")
           (f90-indent-to (f90-comment-indent))
-        (if (not (zerop (- (current-indentation)
-                           (+ ind-curr f90-continuation-indent))))
+        (or (= (current-indentation)
+               (+ ind-curr f90-continuation-indent))
             (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
     ;; Process all following lines.
     (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
@@ -1417,14 +1469,14 @@ If run in the middle of a line, the line is not broken."
             (setq ind-curr ind-lev))
            (t (setq ind-curr ind-lev)))
       ;; Do the indentation if necessary.
-      (if (not (zerop (- ind-curr (current-column))))
+      (or (= ind-curr (current-column))
          (f90-indent-to ind-curr))
       (while (and (f90-line-continued) (zerop (forward-line 1))
                  (< (point) end-region-mark))
         (if (looking-at "[ \t]*!")
             (f90-indent-to (f90-comment-indent))
-          (if (not (zerop (- (current-indentation)
-                             (+ ind-curr f90-continuation-indent))))
+          (or (= (current-indentation)
+                 (+ ind-curr f90-continuation-indent))
               (f90-indent-to
                (+ ind-curr f90-continuation-indent) 'no-line-no)))))
     ;; Restore point, etc.
@@ -1432,13 +1484,13 @@ 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 f90-xemacs-flag
-       (zmacs-deactivate-region)
+    (if (fboundp 'zmacs-deactivate-region)
+       (zmacs-deactivate-region)
       (deactivate-mark))))
 
 (defun f90-indent-subprogram ()
   "Properly indent the subprogram containing point."
-  (interactive)
+  (interactive "*")
   (save-excursion
     (let ((program (f90-mark-subprogram)))
       (if program
@@ -1456,7 +1508,7 @@ If run in the middle of a line, the line is not broken."
   "Break line at point, insert continuation marker(s) and indent.
 Unless in a string or comment, or if the optional argument NO-UPDATE
 is non-nil, call `f90-update-line' after inserting the continuation marker."
-  (interactive)
+  (interactive "*P")
   (cond ((f90-in-string)
          (insert "&\n&"))
         ((f90-in-comment)
@@ -1469,78 +1521,80 @@ is non-nil, call `f90-update-line' after inserting the continuation marker."
 
 (defun f90-find-breakpoint ()
   "From `fill-column', search backward for break-delimiter."
-  (let ((bol (line-beginning-position)))
-    (re-search-backward f90-break-delimiters bol)
-    (if (not f90-break-before-delimiters)
-        (if (looking-at f90-no-break-re)
-            (forward-char 2)
-          (forward-char))
-      (backward-char)
-      (if (not (looking-at f90-no-break-re))
-          (forward-char)))))
+  (re-search-backward f90-break-delimiters (line-beginning-position))
+  (if (not f90-break-before-delimiters)
+      (forward-char (if (looking-at f90-no-break-re) 2 1))
+    (backward-char)
+    (or (looking-at f90-no-break-re)
+        (forward-char))))
 
 (defun f90-do-auto-fill ()
   "Break line if non-white characters beyond `fill-column'.
 Update keyword case first."
-  (interactive)
+  (interactive "*")
   ;; Break line before or after last delimiter (non-word char) if
   ;; position is beyond fill-column.
   ;; Will not break **, //, or => (as specified by f90-no-break-re).
   (f90-update-line)
-  (while (> (current-column) fill-column)
-    (let ((pos-mark (point-marker)))
-      (move-to-column fill-column)
-      (or (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 \&."
-  (interactive)
-  (let (pos (oldpos (point)))
-    (end-of-line)
+  ;; Need this for `f90-electric-insert' and other f90- callers.
+  (unless (and (boundp 'comment-auto-fill-only-comments)
+               comment-auto-fill-only-comments
+               (not (f90-in-comment)))
+    (while (> (current-column) fill-column)
+      (let ((pos-mark (point-marker)))
+        (move-to-column fill-column)
+        (or (f90-in-string) (f90-find-breakpoint))
+        (f90-break-line)
+        (goto-char pos-mark)
+        (set-marker pos-mark nil)))))
+
+(defun f90-join-lines (&optional arg)
+  "Join current line to previous, fix whitespace, continuation, comments.
+With optional argument ARG, join current line to following line.
+Like `join-line', but handles F90 syntax."
+  (interactive "*P")
+  (beginning-of-line)
+  (if arg (forward-line 1))
+  (when (eq (preceding-char) ?\n)
+    (skip-chars-forward " \t")
+    (if (looking-at "\&") (delete-char 1))
+    (beginning-of-line)
+    (delete-region (point) (1- (point)))
     (skip-chars-backward " \t")
-    (when (= (preceding-char) ?&)
-      (delete-char -1)
-      (setq pos (point))
-      (forward-line 1)
-      (skip-chars-forward " \t")
-      (if (looking-at "\&") (delete-char 1))
-      (delete-region pos (point))
-      (unless (f90-in-string)
-        (delete-horizontal-space)
-        (insert " "))
-      (if (and auto-fill-function
-               (> (line-end-position) fill-column))
-          (f90-do-auto-fill))
-      (goto-char oldpos)
-      t)))                              ; return t if joined something
+    (and (eq (preceding-char) ?&) (delete-char -1))
+    (and (f90-in-comment)
+         (looking-at "[ \t]*!+")
+         (replace-match ""))
+    (or (f90-in-string)
+        (fixup-whitespace))))
 
 (defun f90-fill-region (beg-region end-region)
   "Fill every line in region by forward parsing.  Join lines if possible."
   (interactive "*r")
-  (let ((end-region-mark (make-marker))
+  (let ((end-region-mark (copy-marker end-region))
         (go-on t)
        f90-smart-end f90-auto-keyword-case auto-fill-function)
-    (set-marker end-region-mark end-region)
     (goto-char beg-region)
     (while go-on
       ;; Join as much as possible.
-      (while (f90-join-lines))
+      (while (progn
+               (end-of-line)
+               (skip-chars-backward " \t")
+               (eq (preceding-char) ?&))
+        (f90-join-lines 'forward))
       ;; Chop the line if necessary.
       (while (> (save-excursion (end-of-line) (current-column))
                fill-column)
        (move-to-column fill-column)
        (f90-find-breakpoint)
        (f90-break-line 'no-update))
-      (setq go-on (and (< (point) (marker-position end-region-mark))
+      (setq go-on (and (< (point) end-region-mark)
                        (zerop (forward-line 1)))
             f90-cache-position (point)))
     (setq f90-cache-position nil)
-    (if f90-xemacs-flag
-       (zmacs-deactivate-region)
+    (set-marker end-region-mark nil)
+    (if (fboundp 'zmacs-deactivate-region)
+       (zmacs-deactivate-region)
       (deactivate-mark))))
 \f
 (defun f90-block-match (beg-block beg-name end-block end-name)
@@ -1552,35 +1606,37 @@ END-NAME is the block end name (may be nil).
 Leave point at the end of line."
   (search-forward "end" (line-end-position))
   (catch 'no-match
-    (if (not (f90-equal-symbols beg-block end-block))
-       (if end-block
-           (progn
-             (message "END %s does not match %s." end-block beg-block)
-             (end-of-line)
-             (throw 'no-match nil))
-         (message "Inserting %s." beg-block)
-         (insert (concat " " beg-block)))
-      (search-forward end-block))
-    (if (not (f90-equal-symbols beg-name end-name))
-       (cond ((and beg-name (not end-name))
-              (message "Inserting %s." beg-name)
-              (insert (concat " " beg-name)))
-             ((and beg-name end-name)
-              (message "Replacing %s with %s." end-name beg-name)
-              (search-forward end-name)
-              (replace-match beg-name))
-             ((and (not beg-name) end-name)
-              (message "Deleting %s." end-name)
-              (search-forward end-name)
-              (replace-match "")))
-      (if end-name (search-forward end-name)))
-    (if (not (looking-at "[ \t]*!")) (delete-horizontal-space))))
+    (if (f90-equal-symbols beg-block end-block)
+        (search-forward end-block)
+      (if end-block
+          (progn
+            (message "END %s does not match %s." end-block beg-block)
+            (end-of-line)
+            (throw 'no-match nil))
+        (message "Inserting %s." beg-block)
+        (insert (concat " " beg-block))))
+    (if (f90-equal-symbols beg-name end-name)
+        (and end-name (search-forward end-name))
+      (cond ((and beg-name (not end-name))
+             (message "Inserting %s." beg-name)
+             (insert (concat " " beg-name)))
+            ((and beg-name end-name)
+             (message "Replacing %s with %s." end-name beg-name)
+             (search-forward end-name)
+             (replace-match beg-name))
+            ((and (not beg-name) end-name)
+             (message "Deleting %s." end-name)
+             (search-forward end-name)
+             (replace-match ""))))
+    (or (looking-at "[ \t]*!") (delete-horizontal-space))))
 
 (defun f90-match-end ()
   "From an end block statement, find the corresponding block and name."
   (interactive)
-  (let ((count 1) (top-of-window (window-start))
-       (end-point (point)) (case-fold-search t)
+  (let ((count 1)
+        (top-of-window (window-start))
+       (end-point (point))
+        (case-fold-search t)
        matching-beg beg-name end-name beg-block end-block end-struct)
     (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
                           (setq end-struct (f90-looking-at-program-block-end)))
@@ -1590,6 +1646,9 @@ Leave point at the end of line."
         (beginning-of-line)
         (while (and (> count 0) (re-search-backward f90-blocks-re nil t))
           (beginning-of-line)
+          ;; GM not a line number if continued line.
+;;;          (skip-chars-forward " \t")
+;;;          (skip-chars-forward "0-9")
           (skip-chars-forward " \t0-9")
           (cond ((or (f90-in-string) (f90-in-comment)))
                 ((setq matching-beg
@@ -1622,7 +1681,7 @@ Leave point at the end of line."
 
 (defun f90-insert-end ()
   "Insert a complete end statement matching beginning of present block."
-  (interactive)
+  (interactive "*")
   (let ((f90-smart-end (or f90-smart-end 'blink)))
     (insert "end")
     (f90-indent-new-line)))
@@ -1632,19 +1691,18 @@ Leave point at the end of line."
 (defun f90-abbrev-start ()
   "Typing `\\[help-command] or `? lists all the F90 abbrevs.
 Any other key combination is executed normally."
-  (interactive)
-  (let (e c)
-    (insert last-command-char)
-    (if (not f90-xemacs-flag)
-        (setq c (read-event))
-      (setq e (next-command-event)
-            c (event-to-character e)))
-    ;; Insert char if not equal to `?'.
-    (if (or (eq c ??) (eq c help-char))
+  (interactive "*")
+  (insert last-command-char)
+  (let (char event)
+    (if (fboundp 'next-command-event) ; XEmacs
+        (setq event (next-command-event)
+              char (event-to-character event))
+      (setq event (read-event)
+            char event))
+    ;; Insert char if not equal to `?', or if abbrev-mode is off.
+    (if (and abbrev-mode (or (eq char ??) (eq char help-char)))
        (f90-abbrev-help)
-      (if f90-xemacs-flag
-         (setq unread-command-event e)
-       (setq unread-command-events (list c))))))
+      (setq unread-command-events (list event)))))
 
 (defun f90-abbrev-help ()
   "List the currently defined abbrevs in F90 mode."
@@ -1666,17 +1724,17 @@ Any other key combination is executed normally."
 
 (defun f90-upcase-keywords ()
   "Upcase all F90 keywords in the buffer."
-  (interactive)
+  (interactive "*")
   (f90-change-keywords 'upcase-word))
 
 (defun f90-capitalize-keywords ()
   "Capitalize all F90 keywords in the buffer."
-  (interactive)
+  (interactive "*")
   (f90-change-keywords 'capitalize-word))
 
 (defun f90-downcase-keywords ()
   "Downcase all F90 keywords in the buffer."
-  (interactive)
+  (interactive "*")
   (f90-change-keywords 'downcase-word))
 
 (defun f90-upcase-region-keywords (beg end)
@@ -1697,7 +1755,7 @@ Any other key combination is executed normally."
 ;; Change the keywords according to argument.
 (defun f90-change-keywords (change-word &optional beg end)
   "Change the case of F90 keywords in the region (if specified) or buffer.
-CHANGE-WORD should be one of 'upcase-word, 'downcase-word, capitalize-word."
+CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
   (save-excursion
     (setq beg (or beg (point-min))
           end (or end (point-max)))
@@ -1705,7 +1763,7 @@ CHANGE-WORD should be one of 'upcase-word, 'downcase-word, capitalize-word."
           (concat "\\("
                   f90-keywords-re "\\|" f90-procedures-re "\\|"
                   f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
-         (ref-point (point-min)) 
+         (ref-point (point-min))
          (modified (buffer-modified-p))
           state saveword back-point)
       (goto-char beg)
@@ -1714,6 +1772,7 @@ CHANGE-WORD should be one of 'upcase-word, 'downcase-word, capitalize-word."
            (unless (progn
                       (setq state (parse-partial-sexp ref-point (point)))
                       (or (nth 3 state) (nth 4 state)
+                          ;; GM f90-directive-comment-re?
                           (save-excursion ; check for cpp directive
                             (beginning-of-line)
                             (skip-chars-forward " \t0-9")
@@ -1734,4 +1793,5 @@ CHANGE-WORD should be one of 'upcase-word, 'downcase-word, capitalize-word."
 
 (provide 'f90)
 
+;;; arch-tag: fceac97c-c147-44bd-aec0-172d4b560ef8
 ;;; f90.el ends here