home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / f90.el < prev    next >
Encoding:
Text File  |  1995-04-26  |  65.0 KB  |  1,585 lines

  1. ;; f90.el --- Fortran-90 mode (free format) for GNU Emacs and GNU XEmacs.
  2. ;; Copyright (C) 1995 Free Software Foundation, Inc.
  3.  
  4. ;; Author: Torbj\"orn Einarsson <tfkte@fy.chalmers.se>
  5. ;; Created: Apr. 13, 1995
  6. ;; Version: 1.05
  7. ;; Keywords: fortran, f90, languages
  8.  
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2 of the License, or
  12. ;; (at your option) any later version.
  13.  
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with this program; if not, write to the Free Software
  21. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Commentary:
  24. ;; Smart mode for editing F90 programs in FREE FORMAT.
  25. ;; Knows about continuation lines, named structured statements, and other
  26. ;; new features in F90 including HPF (High Performance Fortran) structures.
  27. ;; The basic feature is to provide an accurate indentation of F90 programs.
  28. ;; In addition, there are many more features like automatic matching of all
  29. ;; end statements, an auto-fill function to break long lines, a join-lines
  30. ;; function which joins continued lines etc etc.
  31. ;;  To facilitate typing, a fairly complete list of abbreviations is provided.
  32. ;;    For example, `i is short-hand for integer (if abbrev-mode is on).
  33.  
  34. ;; There are two separate features for highlighting the code.
  35. ;;   1) Upcasing or capitalizing of all keywords.
  36. ;;   2) Different colors/fonts for different structures. (X-windows)
  37. ;;         (using either font-lock-mode or hilit19)
  38. ;;  Automatic upcase of downcase of keywords is controlled by the parameter
  39. ;;  f90-auto-keyword-case.
  40. ;;  To use hilit19 it must be loaded before this file. Then the parameter
  41. ;;  f90-auto-hilit19 determines if each line should be updated automatically.
  42.  
  43. ;; The indentations of lines starting with ! is determined by the first of the
  44. ;; following matches:
  45. ;; start-string   indent          variable holding start-string
  46. ;;    !!!            0
  47. ;;    !hpf$          0            f90-directive-comment (default nil)
  48. ;;    !!$            0            f90-comment-region    (default !!$)
  49. ;;    !           as code         f90-indented-comment  (a regexp, default !)
  50. ;;    default     comment-column
  51. ;; Ex: Here is the result of three different settings of f90-indented-comment
  52. ;;     f90-indented-comment  !-indentation      !!-indentation
  53. ;;          !                  as code             as code
  54. ;;          !!                 comment-column      as code
  55. ;;          ![^!]              as code             comment-column
  56. ;; Trailing comments are indented to comment-column with indent-for-comment M-;
  57. ;; f90-comment-region (C-c;) toggles insertion of f90-comment-region in region.
  58.  
  59. ;; One common convention for free vs. fixed format is that free-format files
  60. ;; have the ending .f90 while the fixed format files have the ending .f. 
  61. ;; To make f90-mode work, put this file in, for example, your directory
  62. ;;  ~/lisp, and be sure that you have the following in your .emacs-file
  63. ;;     (setq load-path (append load-path '("~/lisp")))
  64. ;;     (autoload 'f90-mode "f90"
  65. ;;       "Major mode for editing Fortran 90 code in free format." t)
  66. ;;     (setq auto-mode-alist (append auto-mode-alist 
  67. ;;                           (list '("\\.f90$" . f90-mode))))
  68. ;; Once you have entered f90-mode, you may get more info by using
  69. ;; the command describe-mode (C-h m). For online help describing various
  70. ;; functions use  C-h f <Name of function you want described>
  71.  
  72. ;; To customize the f90-mode for your taste, use, for example:
  73. ;;    (you don't have to specify values for all the parameters below)
  74. ;;(setq f90-mode-hook
  75. ;;      '(lambda () (setq f90-do-indent 3
  76. ;;                        f90-if-indent 3
  77. ;;                        f90-type-indent 3
  78. ;;                        f90-program-indent 2
  79. ;;                        f90-continuation-indent 5
  80. ;;                        f90-comment-region "!!$"
  81. ;;                        f90-directive-comment nil
  82. ;;                        f90-indented-comment "!"
  83. ;;                        f90-break-delimiters "[-+\\*/,><=% \t]"
  84. ;;                        f90-break-before-delimiters t
  85. ;;                        f90-beginning-ampersand t
  86. ;;                        f90-smart-end 'blink
  87. ;;                        f90-auto-keyword-case nil
  88. ;;                        f90-auto-hilit19 t
  89. ;;                        f90-leave-line-no  nil
  90. ;;                        f90-startup-message t
  91. ;;                        indent-tabs-mode nil
  92. ;;                  )
  93. ;;       ;;The rest is not default.
  94. ;;       (abbrev-mode 1)             ; turn on abbreviation mode
  95. ;;       (f90-auto-fill-mode 1)      ; turn on auto-filling
  96. ;;       (if f90-auto-keyword-case   ; change case of all keywords on startup
  97. ;;           (f90-change-keywords f90-auto-keyword-case))
  98. ;;     ))
  99. ;; in your .emacs file (the shown values are the defaults). You can also
  100. ;; change the values of the lists f90-keywords etc.
  101. ;; The auto-fill and abbreviation minor modes are accessible from the menu,
  102. ;; or by using M-x f90-auto-fill-mode and M-x abbrev-mode, respectively.
  103.  
  104. ;; Remarks
  105. ;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
  106. ;;    non-nil, the line numbers are never touched.
  107. ;; 2) Multi-; statements like > do i=1,20 ; j=j+i ; end do < are not handled
  108. ;;    correctly, but I imagine them to be rare.
  109. ;; 3) You can use either hilit19 or font-lock-mode for your highlighting
  110. ;;   a) To use hilit19, be sure that hilit19 is loaded before this file
  111. ;;   b) To use font-lock-mode, nothing special is needed.
  112. ;; 4) For FIXED FORMAT code, use the ordinary fortran mode.
  113. ;; 5) This mode does not work under emacs-18.x.
  114. ;; 6) Preprocessor directives, i.e., lines starting with # are left-justified
  115. ;;    and are untouched by all case-changing commands. There is, at present, no
  116. ;;    mechanism for treating multi-line directives (continued by \ ).
  117.  
  118. ;; List of user commands
  119. ;;   f90-previous-statement         f90-next-statement
  120. ;;   f90-beginning-of-subprogram    f90-end-of-subprogram   f90-mark-subprogram
  121. ;;   f90-comment-region
  122. ;;   f90-indent-line                f90-indent-new-line
  123. ;;   f90-indent-region    (can be called by calling indent-region)
  124. ;;   f90-indent-subprogram
  125. ;;   f90-break-line                 f90-join-lines
  126. ;;   f90-auto-fill-mode
  127. ;;   f90-fill-region
  128. ;;   f90-insert-end
  129. ;;   f90-upcase-keywords            f90-upcase-region-keywords
  130. ;;   f90-downcase-keywords          f90-downcase-region-keywords
  131. ;;   f90-capitalize-keywords        f90-capitalize-region-keywords
  132.  
  133. ;; Thanks to all the people who have tested the mode. Special thanks to Jens
  134. ;; Bloch Helmers for encouraging me to write this code, for creative
  135. ;; suggestions as well as for the lists of hpf-commands.
  136. ;; Also thanks to the authors of the fortran and pascal modes, on which some
  137. ;; of this code is built.
  138.  
  139. ;;; Code:
  140. (defconst f90-mode-version "version 1.05")
  141. (defconst bug-f90-mode "tfkte@fy.chalmers.se"
  142.   "Address of mailing list for F90 mode bugs.")
  143.  
  144. ;; User options
  145. (defvar f90-do-indent 3
  146.   "*Extra indentation applied to DO blocks.")
  147.  
  148. (defvar f90-if-indent 3
  149.   "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks.")
  150.  
  151. (defvar f90-type-indent 3
  152.   "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks.")
  153.  
  154. (defvar f90-program-indent 2
  155.   "*Extra indentation applied to PROGRAM/MODULE/SUBROUTINE/FUNCTION blocks.")
  156.  
  157. (defvar f90-continuation-indent 5
  158.   "*Extra indentation applied to F90 continuation lines.")
  159.  
  160. (defvar f90-comment-region "!!$"
  161.   "*String inserted by \\[f90-comment-region]\
  162.  at start of each line in region.")
  163.  
  164. (defvar f90-indented-comment "!"
  165.   "*Comments to be indented like code.")
  166.  
  167. (defvar f90-directive-comment nil
  168.   "*String of comment-like directive like \"!HPF$\", not to be indented.")
  169.  
  170. (defvar f90-beginning-ampersand t
  171.   "*t makes automatic insertion of \& at beginning of continuation line.")
  172.  
  173. (defvar f90-smart-end 'blink
  174.   "*From an END statement, check and fill the end using matching block start.
  175. Allowed values are 'blink, 'no-blink, and nil, which determine
  176. whether to blink the matching beginning.")
  177.  
  178. (defvar f90-break-delimiters "[-+\\*/><=,% \t]"
  179.   "*Regexp holding list of delimiters at which lines may be broken.")
  180.  
  181. (defvar f90-break-before-delimiters t
  182.   "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters.")
  183.  
  184. (defvar f90-auto-keyword-case nil
  185.   "*Automatic case conversion of keywords.
  186.   The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil")
  187.  
  188. (defvar f90-auto-hilit19 t
  189.   "*Automatic highlight of line at every indent or newline (for hilit19).")
  190.  
  191. (defvar f90-leave-line-no nil
  192.   "*If nil, left-justify linenumbers.")
  193.  
  194. (defvar f90-startup-message t
  195.   "*Non-nil displays a startup message when F90 mode is first called.")
  196.  
  197. (defvar f90-keywords
  198.   '("allocate" "allocatable" "assign" "assignment" "backspace" "block"
  199.     "call" "case" "character" "close" "common" "complex" "contains"
  200.     "continue" "cycle" "data" "deallocate" "dimension" "do" "double" "else"
  201.     "elseif" "elsewhere" "end" "enddo" "endfile" "endif" "entry" "equivalence"
  202.     "exit" "external" "forall" "format" "function" "goto" "if" "implicit"
  203.     "include" "inquire" "integer" "intent" "interface" "intrinsic" "logical"
  204.     "module" "namelist" "none" "nullify" "only" "open" "optional" "parameter"
  205.     "pause" "pointer" "precision" "print" "private" "procedure" "program"
  206.     "public" "read" "real" "recursive" "return" "rewind" "save" "select"
  207.     "sequence" "stop" "subroutine" "target" "then" "type" "use" "where"
  208.     "while" "write")
  209.   "*List of f90-keywords.")
  210.  
  211. (defvar f90-intrinsic-procedures
  212.   '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint" "all" "allocated"
  213.     "anint" "any" "asin" "associated" "atan" "atan2" "bit_size" "btest"
  214.     "ceiling" "char" "cmplx" "conjg" "cos" "cosh" "count" "cshift"
  215.     "date_and_time" "dble" "digits" "dim" "dot_product" "dprod" "eoshift"
  216.     "epsilon" "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
  217.     "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior" "ishft"
  218.     "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt" "lle" "llt" "log"
  219.     "logical" "log10" "matmul" "max" "maxexponent" "maxloc" "maxval" "merge"
  220.     "min" "minexponent" "minloc" "minval" "mod" "modulo" "mvbits" "nearest"
  221.     "nint" "not" "pack" "precision" "present" "product" "radix" "random_number"
  222.     "random_seed" "range" "real" "repeat" "reshape" "rrspacing" "scale"
  223.     "scan" "selected_int_kind" "selected_real_kind" "set_exponent" "shape"
  224.     "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt" "sum"
  225.     "system_clock" "tan" "tanh" "tiny" "transfer" "transpose" "trim"
  226.     "ubound" "unpack" "verify")
  227.   "*List of F90 intrinsic procedures.")
  228.  
  229. (defvar f90-hpf-procedures
  230.   '("all_prefix" "all_scatter" "all_suffix" "any_prefix" "any_scatter"
  231.     "any_suffix" "copy_prefix" "copy_scatter" "copy_suffix" "count_prefix"
  232.     "count_scatter" "count_suffix" "grade_down" "grade_up" "hpf_alignment"
  233.     "hpf_template" "hpf_distribution" "iall" "iall_prefix" "iall_scatter"
  234.     "iall_suffix" "iany" "iany_prefix" "iany_scatter" "iany_suffix" "iparity"
  235.     "iparity_prefix" "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
  236.     "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter" 
  237.     "minval_suffix" "parity" "parity_prefix" "parity_scatter" "parity_suffix"
  238.     "popcnt" "poppar" "product_prefix" "product_scatter" "product_suffix"
  239.     "sum_prefix" "sum_scatter" "sum_suffix" "ilen" "number_of_processors"
  240.     "processors_shape")
  241.   "*List of hpf intrinsic procedures.")
  242.  
  243. (defvar f90-hpf-directives
  244.   '("align" "distribute" "dynamic" "inherit" "template" "processors"
  245.     "realign" "redistribute" "independent")
  246.   "*List of hpf directives.")
  247.  
  248. (defvar f90-hpf-keywords
  249.   '("pure" "extrinsic" "new" "with" "onto" "block" "cyclic")
  250.   "*List of hpf keywords.")
  251.  
  252. ;; Highlighting patterns
  253.  
  254. (defconst f90-font-lock-keywords-1
  255.   (purecopy
  256.    (list
  257.     ;; Subroutine and function declarations
  258.     '("^[ \t]*\\(program\\|module\\)[ \t]+\\sw+" 1 font-lock-keyword-face)
  259.     '("^[ \t]*\\(program\\|module\\)[ \t]+\\(\\sw+\\)" 2
  260.       font-lock-function-name-face)
  261.     '("\\(^.*\\(function\\|subroutine\\)\\)[ \t]+\\sw+" 1
  262.       font-lock-keyword-face)
  263.     '("^.*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)" 2
  264.       font-lock-function-name-face)
  265.     '("^[ \t]*end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)"
  266.       . font-lock-keyword-face)
  267.     (list (concat "^[ \t]*end[ \t]*\\(program\\|module\\|function\\|"
  268.           "subroutine\\|type\\)[ \t]+\\(\\sw+\\)") 2 
  269.           'font-lock-function-name-face)
  270.     '("^[ \t]*\\(type\\)[ \t]+\\sw+" 1 font-lock-keyword-face)
  271.     '("^[ \t]*type[ \t]+\\(\\sw+\\)" 1 font-lock-function-name-face)
  272.     '("^[ \t]*\\(type[ \t]*,[ \t]*\\(private\\|public\\)\\)[ \t]*::\
  273. [ \t]*\\(\\sw+\\)" 1 font-lock-keyword-face)
  274.     '("^[ \t]*\\(type[ \t]*,[ \t]*\\(private\\|public\\)\\)[ \t]*::\
  275. [ \t]*\\(\\sw+\\)" 3 font-lock-function-name-face)
  276.     '("^[ \t]*contains\\>" . font-lock-keyword-face)))
  277.   "For consideration as a value of `f90-font-lock-keywords-1'.
  278. This does fairly subdued highlighting of comments and function names.")
  279.  
  280. (defconst f90-font-lock-keywords-2
  281.   (purecopy
  282.    (append f90-font-lock-keywords-1
  283.     (list
  284.      ;; Variable declarations
  285.      '("\\(\\(real\\|integer\\|character\\|complex\\|logical\\|\
  286. type[ \t]*(\\sw+)\\).*\\)::" 1 font-lock-type-face)
  287.      '("implicit[ \t]*none" . font-lock-keyword-face)
  288.      '("^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(do\\([ \t]*while\\)?\\)\\>"
  289.        2 font-lock-keyword-face)
  290.      '("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*\\(do\\([ \t]*while\\)?\\)\\>" 1
  291.        font-lock-function-name-face)
  292.      '("^[ \t]*\\(end[ \t]*do\\)\\>" 1 font-lock-keyword-face)
  293.      '("^[ \t]*end[ \t]*do[ \t]+\\(\\sw+\\)" 1 font-lock-function-name-face)
  294.      '("^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(if\\)\\>" 2 
  295.        font-lock-keyword-face)
  296.      '("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*if\\>" 1 font-lock-function-name-face)
  297.      '("^[ \t]*\\(end[ \t]*if\\)\\>" 1 font-lock-keyword-face)
  298.      '("^[ \t]*end[ \t]*if[ \t]+\\(\\sw+\\)" 1 font-lock-function-name-face)
  299.      '("^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(select[ \t]*case\\)\\>" 2
  300.        font-lock-keyword-face)
  301.      '("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*\\(select[ \t]*case\\)\\>" 1
  302.        font-lock-function-name-face)
  303.      '("^[ \t]*end[ \t]*select\\>" . font-lock-keyword-face)
  304.      '("^[ \t]*end[ \t]*select\\>[ \t]+\\(\\sw+\\)" 1
  305.        font-lock-function-name-face)
  306.      '("\\(where\\|forall\\)[ \t]*(" 1 font-lock-keyword-face)
  307.      '("\\<\\(elsewhere\\|else\\|else[ \t]*if\\)\\>" . font-lock-keyword-face)
  308.      '("\\<end[ \t]*\\(where\\|forall\\)\\>" . font-lock-keyword-face)
  309.      '("\\<then\\>" . font-lock-keyword-face)
  310.      '("\\<\\(exit\\|cycle\\)\\>" . font-lock-keyword-face)
  311.      '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)\\>" 2
  312.        font-lock-function-name-face)
  313.      '("\\<\\(stop\\|return\\)\\>" . font-lock-keyword-face)
  314.      '("^[ \t]*\\(case\\)[ \t]*\\((\\|default\\)" 1 font-lock-keyword-face)
  315.      (concat "\\<\\("(mapconcat 'identity f90-keywords "\\|") "\\)\\>")
  316.      (concat "\\<\\("(mapconcat 'identity f90-intrinsic-procedures "\\|")
  317.          "\\)\\>")
  318.      (concat "\\<\\("(mapconcat 'identity f90-hpf-procedures "\\|")
  319.          "\\)\\>")
  320.      (concat "\\<\\("(mapconcat 'identity f90-hpf-directives "\\|")
  321.          "\\)\\>")
  322.      (concat "\\<\\("(mapconcat 'identity f90-hpf-keywords "\\|")
  323.          "\\)\\>"))))
  324.   "For consideration as a value of `f90-font-lock-keywords'.
  325. This highlights variable types, \"keywords,\" etc.")
  326.  
  327. (defvar f90-font-lock-keywords f90-font-lock-keywords-2
  328.   "*Additional expressions to highlight in F90 mode.")
  329.  
  330. ;; hilit19 customization and expressions
  331. (defvar f90-face-string 'named-param "*Face for strings.")
  332. (defvar f90-face-comment 'comment "*Face for comments.")
  333. (defvar f90-face-decl 'include "*Face for declarations.")
  334. (defvar f90-face-prog 'defun "*Face for program blocks.")
  335. (defvar f90-face-label 'Tomato-bold "*Face for labels.")
  336. (defvar f90-face-type 'defun "*Face for type blocks.")
  337. (defvar f90-face-interface 'defun "*Face for interface blocks.")
  338. (defvar f90-face-contains 'defun "*Face for contains statement.")
  339. (defvar f90-face-do 'SteelBlue-bold "*Face for do-structure.")
  340. (defvar f90-face-if 'define "*Face for if-structure.")
  341. (defvar f90-face-select 'define "*Face for select-case structure.")
  342. (defvar f90-face-stop 'defun "*Face for stop and return.")
  343. (defvar f90-face-exit 'SteelBlue-bold "*Face for exit and cycle.")
  344. (defvar f90-face-keyword 'struct "*Face for keywords.")
  345. (defvar f90-face-intrinsics 'struct "*Face for intrinsic procedures.")
  346. ;; Highlighting for HPF (High-Peformance Fortran)
  347. (defvar f90-face-hpf-procedures 'struct "*Face for hpf procedures.")
  348. (defvar f90-face-hpf-directives 'struct "*Face for hpf directives.")
  349. (defvar f90-face-hpf-keywords   'struct "*Face for hpf keywords.")
  350.  
  351. (if (fboundp 'hilit-set-mode-patterns)
  352.     (hilit-set-mode-patterns
  353.      'f90-mode
  354.      (list
  355.       ;; Allow for strings delimited by ' and by " and for multirow strings.
  356.       ;; A multi-row string includes &\n& (+ possible whitespace and comments)
  357.       (list (concat
  358.          "\\(\"[^\"\n]*\\(&[ \t]*\\(![^\n]*\\)?\n[ \t]*&[^\"\n]*\\)*\""
  359.          "\\|'[^'\n]*\\(&[ \t]*\\(![^\n]*\\)?\n[ \t]*&[^'\n]*\\)*'\\)")
  360.         nil f90-face-string)
  361.       (list "!" "$" f90-face-comment)
  362.       (list "\\(\\(real\\|integer\\|character\\|complex\\|logical\
  363. \\|type[ \t]*(\\sw+)\\).*\\)::" 1 f90-face-decl)
  364.       (list "implicit[ \t]*none" nil f90-face-decl)
  365.       (list "^[ \t]*\\(program\\|module\\)[ \t]+\\sw+" 1 f90-face-prog)
  366.       (list "^[ \t]*\\(program\\|module\\)[ \t]+\\(\\sw+\\)" 2 f90-face-label)
  367.       (list "\\(^.*\\(function\\|subroutine\\)\\)[ \t]+\\sw+" 1
  368.         f90-face-prog)
  369.       (list "^.*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)" 2
  370.         f90-face-label)
  371.       (list "^[ \t]*end[ \t]*\\(program\\|module\\|function\
  372. \\|subroutine\\|type\\)" nil f90-face-prog)
  373.       (list (concat "^[ \t]*end[ \t]*\\(program\\|module\\|function\\|"
  374.             "subroutine\\|type\\)[ \t]+\\(\\sw+\\)") 2 f90-face-label)
  375.       (list "^[ \t]*\\(type\\)[ \t]+\\sw+" 1 f90-face-type)
  376.       (list "^[ \t]*type[ \t]+\\(\\sw+\\)" 1 f90-face-label)
  377.       (list "^[ \t]*\\(type[ \t]*,[ \t]*\\(private\\|public\\)\\)[ \t]*::[ \t]*\\(\\sw+\\)" 1 f90-face-type)
  378.       (list "^[ \t]*\\(type[ \t]*,[ \t]*\\(private\\|public\\)\\)[ \t]*::[ \t]*\\(\\sw+\\)" 3 f90-face-label)
  379.       (list "^[ \t]*\\(end[ \t]*\\)?interface\\>" nil f90-face-interface)
  380.       (list "^[ \t]*contains\\>" nil f90-face-contains)
  381.       (list "^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(do\\([ \t]*while\\)?\\)\\>"
  382.         2 f90-face-do)
  383.       (list "^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*\\(do\\([ \t]*while\\)?\\)\\>" 1
  384.         f90-face-label)
  385.       (list "^[ \t]*\\(end[ \t]*do\\)\\>" 1 f90-face-do)
  386.       (list "^[ \t]*end[ \t]*do[ \t]+\\(\\sw+\\)" 1 f90-face-label)
  387.       (list "^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(if\\)\\>" 2 f90-face-if)
  388.       (list "^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*if\\>" 1 f90-face-label)
  389.       (list "^[ \t]*\\(end[ \t]*if\\)\\>" 1 f90-face-if)
  390.       (list "^[ \t]*end[ \t]*if[ \t]+\\(\\sw+\\)" 1 f90-face-label)
  391.       (list "^[ \t]*\\(\\sw+[ \t]*:[ \t]*\\)?\\(select[ \t]*case\\)\\>" 2
  392.         f90-face-select)
  393.       (list "^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*\\(select[ \t]*case\\)\\>" 1
  394.         f90-face-label)
  395.       (list "^[ \t]*end[ \t]*select\\>" nil f90-face-select)
  396.       (list "^[ \t]*end[ \t]*select\\>[ \t]+\\(\\sw+\\)" 1 f90-face-label)
  397.       (list "\\(where\\|forall\\)[ \t]*(" 1 f90-face-if)
  398.       (list "\\<\\(elsewhere\\|else\\|else[ \t]*if\\)\\>" nil f90-face-if)
  399.       (list "\\<end[ \t]*\\(where\\|forall\\)\\>" nil f90-face-if)
  400.       (list "\\<then\\>" nil f90-face-if)
  401.       (list "\\<\\(exit\\|cycle\\)\\>" nil f90-face-exit)
  402.       (list "\\<\\(exit\\|cycle\\)[ \t]*\\sw+\\>" nil f90-face-label)
  403.       (list "\\<\\(stop\\|return\\)\\>" nil f90-face-stop)
  404.       (list "^[ \t]*\\(case\\)[ \t]*\\((\\|default\\)" 1 f90-face-select)
  405.       (list (concat "\\<\\("(mapconcat 'identity f90-keywords "\\|")
  406.             "\\)\\>") nil f90-face-keyword)
  407.     (list (concat "\\<\\("(mapconcat 'identity f90-intrinsic-procedures "\\|")
  408.           "\\)\\>") nil f90-face-intrinsics)
  409.     (list (concat "\\<\\("(mapconcat 'identity f90-hpf-procedures "\\|")
  410.           "\\)\\>") nil f90-face-hpf-procedures)
  411.     (list (concat "\\<\\("(mapconcat 'identity f90-hpf-directives "\\|")
  412.           "\\)\\>") nil f90-face-hpf-directives)
  413.     (list (concat "\\<\\("(mapconcat 'identity f90-hpf-keywords "\\|")
  414.           "\\)\\>") nil f90-face-hpf-keywords))
  415.    nil 'case-insensitive))
  416.  
  417. ;; syntax table
  418. (defvar f90-mode-syntax-table nil
  419.   "Syntax table in use in F90 mode buffers.")
  420.  
  421. (if f90-mode-syntax-table
  422.     ()
  423.   (setq f90-mode-syntax-table (make-syntax-table))
  424.   (modify-syntax-entry ?\! "<" f90-mode-syntax-table)  ; beg. comment
  425.   (modify-syntax-entry ?\n ">" f90-mode-syntax-table)  ; end comment
  426.   (modify-syntax-entry ?_ "w" f90-mode-syntax-table)   ; underscore in names
  427.   (modify-syntax-entry ?\' "\"" f90-mode-syntax-table) ; string quote
  428.   (modify-syntax-entry ?\" "\"" f90-mode-syntax-table) ; string quote
  429.   (modify-syntax-entry ?\` "w" f90-mode-syntax-table)  ; for abbrevs
  430.   (modify-syntax-entry ?\r " " f90-mode-syntax-table)  ; return is whitespace
  431.   (modify-syntax-entry ?+ "." f90-mode-syntax-table)  
  432.   (modify-syntax-entry ?- "." f90-mode-syntax-table)
  433.   (modify-syntax-entry ?= "." f90-mode-syntax-table)
  434.   (modify-syntax-entry ?* "." f90-mode-syntax-table)
  435.   (modify-syntax-entry ?/ "." f90-mode-syntax-table)
  436.   (modify-syntax-entry ?\\ "/" f90-mode-syntax-table)) ; escape chars
  437.  
  438. ;; keys
  439. (defvar f90-mode-map ()
  440.   "Keymap used in F90 mode.")
  441. (if f90-mode-map
  442.     ()
  443.   (setq f90-mode-map (make-sparse-keymap))
  444.   (define-key f90-mode-map "`"        'f90-abbrev-start)
  445.   (define-key f90-mode-map "\C-c;"    'f90-comment-region)
  446.   (define-key f90-mode-map "\C-\M-a"  'f90-beginning-of-subprogram)
  447.   (define-key f90-mode-map "\C-\M-e"  'f90-end-of-subprogram)
  448.   (define-key f90-mode-map "\C-\M-h"  'f90-mark-subprogram)
  449.   (define-key f90-mode-map "\C-\M-q"  'f90-indent-subprogram)
  450.   (define-key f90-mode-map "\C-j"     'f90-indent-new-line) ; LFD equals C-j
  451.   (define-key f90-mode-map "\r"       'newline)
  452.   (define-key f90-mode-map "\C-c\r"   'f90-break-line)
  453.   ;;  (define-key f90-mode-map [M-return] 'f90-break-line)
  454.   (define-key f90-mode-map "\C-c\C-d" 'f90-join-lines)
  455.   (define-key f90-mode-map "\C-c\C-f" 'f90-fill-region)
  456.   (define-key f90-mode-map "\C-c\C-p" 'f90-previous-statement)
  457.   (define-key f90-mode-map "\C-c\C-n" 'f90-next-statement)
  458.   (define-key f90-mode-map "\C-c\C-w" 'f90-insert-end)
  459.   (define-key f90-mode-map "\t"       'f90-indent-line))
  460. ;; menus
  461. (if (string-match "Lucid" emacs-version)
  462.     ;; XEmacs
  463.     (progn
  464.       (add-menu nil "F90"
  465.         '(
  466.           ["Indent subprogram"       f90-indent-subprogram t]
  467.           ["Mark subprogram"         f90-mark-subprogram t]
  468.           ["Beginning of subprogram" f90-beginning-of-subprogram t]
  469.           ["End of subprogram"       f90-end-of-subprogram t]
  470.           "-----"
  471.           ["(Un)Comment region"      f90-comment-region t]
  472.           ["Indent region"           indent-region t]
  473.           ["Fill region"             f90-fill-region t]
  474.           "-----"
  475.           ["Break line at point"     f90-break-line t]
  476.           ["Join with next line"     f90-join-lines t]
  477.           ["Insert newline."         newline t]
  478.           ["Insert end."             f90-insert-end t]
  479.           "-----"
  480.           ["UPCASE keywords (buffer)"      f90-upcase-keywords t]
  481.           ["UPCASE keywords (region)"      f90-upcase-region-keywords
  482.            t]
  483.           ["Capitalize keywords (buffer)"  f90-capitalize-keywords t]
  484.           ["Capitalize keywords (region)" 
  485.            f90-capitalize-region-keywords t]
  486.           ["downcase keywords (buffer)"    f90-downcase-keywords t]
  487.           ["downcase keywords (region)"   
  488.            f90-downcase-region-keywords t]
  489.           "-----"
  490.           ["Toggle abbrev-mode"   abbrev-mode             t]
  491.           ["Toggle auto-fill"     f90-auto-fill-mode      t])))
  492.   ;; Emacs
  493.   (define-key f90-mode-map [menu-bar] (make-sparse-keymap))
  494.   (define-key f90-mode-map [menu-bar f90] 
  495.     (cons "F90" (make-sparse-keymap "f90"))) 
  496.   (define-key f90-mode-map [menu-bar f90 abbrev-mode]
  497.     '("Toggle abbrev-mode" . abbrev-mode))
  498.   (define-key f90-mode-map [menu-bar f90 f90-auto-fill-mode]
  499.     '("Toggle auto-fill" . f90-auto-fill-mode))
  500.   (define-key f90-mode-map [menu-bar f90 f90-downcase-region-keywords]
  501.     '("downcase keywords (region)" . f90-downcase-region-keywords))
  502.   (define-key f90-mode-map [menu-bar f90 f90-downcase-keywords]
  503.     '("downcase keywords (buffer)" . f90-downcase-keywords))
  504.   (define-key f90-mode-map [menu-bar f90 f90-capitalize-keywords]
  505.     '("Capitalize keywords (region)" . f90-capitalize-region-keywords))
  506.   (define-key f90-mode-map [menu-bar f90 f90-capitalize-region-keywords]
  507.     '("Capitalize keywords (buffer)" . f90-capitalize-keywords))
  508.   (define-key f90-mode-map [menu-bar f90 f90-upcase-region-keywords]
  509.     '("UPCASE keywords (region)" . f90-upcase-region-keywords))
  510.   (define-key f90-mode-map [menu-bar f90 f90-upcase-keywords]
  511.     '("UPCASE keywords (buffer)" . f90-upcase-keywords))
  512.   (define-key f90-mode-map [menu-bar f90 f90-insert-end]
  513.     '("Insert end." . f90-insert-end))
  514.   (define-key f90-mode-map [menu-bar f90 f90-join-lines]
  515.     '("Join with next line." . f90-join-lines))
  516.   (define-key f90-mode-map [menu-bar f90 f90-break-line]
  517.     '("Break line at point" . f90-break-line))
  518.   (define-key f90-mode-map [menu-bar f90 f90-fill-region]
  519.     '("Fill Region" . f90-fill-region))
  520.   (define-key f90-mode-map [menu-bar f90 indent-region]
  521.     '("Indent Region" . indent-region))
  522.   (define-key f90-mode-map [menu-bar f90 f90-comment-region]
  523.     '("(Un)Comment Region" . f90-comment-region))
  524.   (define-key f90-mode-map [menu-bar f90 f90-end-of-subprogram]
  525.     '("End of Subprogram" . f90-end-of-subprogram))
  526.   (define-key f90-mode-map [menu-bar f90 f90-beginning-of-subprogram]
  527.     '("Beginning of Subprogram" . f90-beginning-of-subprogram))
  528.   (define-key f90-mode-map [menu-bar f90 f90-mark-subprogram]
  529.     '("Mark Subprogram" . f90-mark-subprogram))
  530.   (define-key f90-mode-map [menu-bar f90 f90-indent-subprogram]
  531.     '("Indent Subprogram" . f90-indent-subprogram)))
  532.   
  533. (defconst f90-symbol-re "[a-z_][a-z_0-9]*")
  534. (defconst f90-blocks-re 
  535.   "\\(block[ \t]*data\\|do\\|if\\|interface\\|function\\|module\\|\
  536. program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
  537. (defconst f90-program-block-re 
  538.   "\\(program\\|module\\|subroutine\\|function\\)")
  539. (defconst f90-else-like-re 
  540.   "\\(else\\|else[ \t]*if\\|elsewhere\\|case[ \t]*(\\|case[ \t]*default\\)")
  541. (defconst f90-end-if-re 
  542.   "end[ \t]*\\(if\\|select\\|where\\|forall\\)\\>")
  543. (defconst f90-end-type-re 
  544.   "end[ \t]*\\(type\\|interface\\|block[ \t]*data\\)\\>")
  545. (defconst f90-no-break-re  "\\(\\*\\*\\|//\\|=>\\)")
  546. (defconst f90-p-type-re
  547.   (concat "\\(type\\)[ \t]*,[ \t]*\\(public\\|private\\)"
  548.       "[ \t]*::[ \t]*\\(" f90-symbol-re "\\)\\>"))
  549. ;; A temporary position to make region operators faster
  550. (defvar f90-cache-position nil)
  551. (make-variable-buffer-local 'f90-cache-position)
  552.  
  553. ;; abbrevs have generally two letters, except standard types `c, `i, `r, `t
  554. (defvar f90-mode-abbrev-table nil)
  555. (if f90-mode-abbrev-table
  556.     ()
  557.   (let ((ac abbrevs-changed))
  558.     (define-abbrev-table 'f90-mode-abbrev-table ())
  559.     (define-abbrev f90-mode-abbrev-table  "`al"  "allocate" nil)
  560.     (define-abbrev f90-mode-abbrev-table  "`ab"  "allocateable" nil)
  561.     (define-abbrev f90-mode-abbrev-table  "`as"  "assignment" nil)
  562.     (define-abbrev f90-mode-abbrev-table  "`ba"  "backspace" nil)
  563.     (define-abbrev f90-mode-abbrev-table  "`bd"  "block data" nil)
  564.     (define-abbrev f90-mode-abbrev-table  "`c"   "character" nil)
  565.     (define-abbrev f90-mode-abbrev-table  "`cl"  "close" nil)
  566.     (define-abbrev f90-mode-abbrev-table  "`cm"  "common" nil)
  567.     (define-abbrev f90-mode-abbrev-table  "`cx"  "complex" nil)
  568.     (define-abbrev f90-mode-abbrev-table  "`cn"  "contains" nil)
  569.     (define-abbrev f90-mode-abbrev-table  "`cy"  "cycle" nil)
  570.     (define-abbrev f90-mode-abbrev-table  "`de"  "deallocate" nil)
  571.     (define-abbrev f90-mode-abbrev-table  "`df"  "define" nil)
  572.     (define-abbrev f90-mode-abbrev-table  "`di"  "dimension" nil)
  573.     (define-abbrev f90-mode-abbrev-table  "`dw"  "do while" nil)
  574.     (define-abbrev f90-mode-abbrev-table  "`el"  "else" nil)
  575.     (define-abbrev f90-mode-abbrev-table  "`eli" "else if" nil)
  576.     (define-abbrev f90-mode-abbrev-table  "`elw" "elsewhere" nil)
  577.     (define-abbrev f90-mode-abbrev-table  "`eq"  "equivalence" nil)
  578.     (define-abbrev f90-mode-abbrev-table  "`ex"  "external" nil)
  579.     (define-abbrev f90-mode-abbrev-table  "`ey"  "entry" nil)
  580.     (define-abbrev f90-mode-abbrev-table  "`fl"  "forall" nil)
  581.     (define-abbrev f90-mode-abbrev-table  "`fo"  "format" nil)
  582.     (define-abbrev f90-mode-abbrev-table  "`fu"  "function" nil)
  583.     (define-abbrev f90-mode-abbrev-table  "`fa"  ".false." nil)
  584.     (define-abbrev f90-mode-abbrev-table  "`im"  "implicit none" nil)
  585.     (define-abbrev f90-mode-abbrev-table  "`in " "include" nil)
  586.     (define-abbrev f90-mode-abbrev-table  "`i"   "integer" nil)
  587.     (define-abbrev f90-mode-abbrev-table  "`it"  "intent" nil)
  588.     (define-abbrev f90-mode-abbrev-table  "`if"  "interface" nil)
  589.     (define-abbrev f90-mode-abbrev-table  "`lo"  "logical" nil)
  590.     (define-abbrev f90-mode-abbrev-table  "`mo"  "module" nil)
  591.     (define-abbrev f90-mode-abbrev-table  "`na"  "namelist" nil)
  592.     (define-abbrev f90-mode-abbrev-table  "`nu"  "nullify" nil)
  593.     (define-abbrev f90-mode-abbrev-table  "`op"  "optional" nil)
  594.     (define-abbrev f90-mode-abbrev-table  "`pa"  "parameter" nil)
  595.     (define-abbrev f90-mode-abbrev-table  "`po"  "pointer" nil)
  596.     (define-abbrev f90-mode-abbrev-table  "`pr"  "print" nil)
  597.     (define-abbrev f90-mode-abbrev-table  "`pi"  "private" nil)
  598.     (define-abbrev f90-mode-abbrev-table  "`pm"  "program" nil)
  599.     (define-abbrev f90-mode-abbrev-table  "`pu"  "public" nil)
  600.     (define-abbrev f90-mode-abbrev-table  "`r"   "real" nil)
  601.     (define-abbrev f90-mode-abbrev-table  "`rc"  "recursive" nil)
  602.     (define-abbrev f90-mode-abbrev-table  "`rt"  "return" nil)
  603.     (define-abbrev f90-mode-abbrev-table  "`rw"  "rewind" nil)
  604.     (define-abbrev f90-mode-abbrev-table  "`se"  "select" nil)
  605.     (define-abbrev f90-mode-abbrev-table  "`sq"  "sequence" nil)
  606.     (define-abbrev f90-mode-abbrev-table  "`su"  "subroutine" nil)
  607.     (define-abbrev f90-mode-abbrev-table  "`ta"  "target" nil)
  608.     (define-abbrev f90-mode-abbrev-table  "`tr"  ".true." nil)
  609.     (define-abbrev f90-mode-abbrev-table  "`t"   "type" nil)
  610.     (define-abbrev f90-mode-abbrev-table  "`wh"  "where" nil)
  611.     (define-abbrev f90-mode-abbrev-table  "`wr"  "write" nil)
  612.     (setq abbrevs-changed ac)))
  613.  
  614. ;;;###autoload
  615. (defun f90-mode ()
  616.   "Major mode for editing Fortran 90 code in free format.
  617.  
  618. \\[f90-indent-new-line] corrects current indentation and creates new\
  619.  indented line.
  620. \\[f90-indent-line] indents the current line correctly. 
  621. \\[f90-indent-subprogram] indents the current subprogram. 
  622.  
  623. Type `? or `\\[help-command] to display a list of built-in\
  624.  abbrevs for F90 keywords.
  625.  
  626. Key definitions:
  627. \\{f90-mode-map}
  628.  
  629. Variables controlling indentation style and extra features:
  630.  
  631.  f90-do-indent
  632.     Extra indentation within do blocks.  (default 3)
  633.  f90-if-indent
  634.     Extra indentation within if/select case/where/forall blocks. (default 3)
  635.  f90-type-indent
  636.     Extra indentation within type/interface/block-data blocks.  (default 3)
  637.  f90-program-indent
  638.     Extra indentation within program/module/subroutine/function blocks.
  639.       (default 2)
  640.  f90-continuation-indent
  641.     Extra indentation applied to continuation lines.  (default 5)
  642.  f90-comment-region
  643.     String inserted by \\[f90-comment-region] at start of each line in 
  644.     region.  (default \"!!!$\")
  645.  f90-indented-comment
  646.     String holding the type of comment to be intended like code.
  647.     This is a regular expression. (default \"!\")
  648.  f90-directive-comment
  649.     String of comment-like directive like \"!HPF$\", not to be indented.
  650.     (default nil)
  651.  f90-break-delimiters
  652.     Regexp holding list of delimiters at which lines may be broken.
  653.     (default \"[-+*/><=,% \\t]\")
  654.  f90-break-before-delimiters
  655.     Non-nil causes `f90-do-auto-fill' to break lines before delimiters.
  656.     (default t)
  657.  f90-beginning-ampersand 
  658.     Automatic insertion of \& at beginning of continuation lines. (default t)
  659.  f90-smart-end 
  660.     From an END statement, check and fill the end using matching block start.
  661.     Allowed values are 'blink, 'no-blink, and nil, which determine
  662.     whether to blink the matching beginning.) (default 'blink)
  663.  f90-auto-keyword-case
  664.     Automatic change of case of keywords. (default nil)
  665.     The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
  666.  f90-auto-hilit19  (default nil)
  667.     Automatic highlighting (if hilit19 is used) at every indent or newline.
  668.  f90-leave-line-no
  669.     Do not left-justify line numbers. (default nil)
  670.  f90-startup-message
  671.     Set to nil to inhibit message first time F90 mode is used. (default t)
  672.  f90-keywords
  673.     List of keywords used for highlighting/upcase-keywords etc.
  674.  
  675. Turning on F90 mode calls the value of the variable `f90-mode-hook'
  676. with no args, if that value is non-nil."
  677.   (interactive)
  678.   (kill-all-local-variables)
  679.   (setq major-mode 'f90-mode)
  680.   (setq mode-name "F90")
  681.   (setq local-abbrev-table f90-mode-abbrev-table)
  682.   (set-syntax-table f90-mode-syntax-table)
  683.   (use-local-map f90-mode-map)
  684.   (make-local-variable 'indent-line-function)
  685.   (setq indent-line-function 'f90-indent-line)
  686.   (make-local-variable 'indent-region-function)
  687.   (setq indent-region-function 'f90-indent-region)
  688.   (make-local-variable 'require-final-newline)
  689.   (setq require-final-newline t)
  690.   (make-local-variable 'comment-start)
  691.   (setq comment-start "!")
  692.   (make-local-variable 'comment-start-skip)
  693.   (setq comment-start-skip "!+ *")
  694.   (make-local-variable 'comment-indent-function)
  695.   (setq comment-indent-function 'f90-comment-indent)
  696.   (make-local-variable 'abbrev-all-caps)
  697.   (setq abbrev-all-caps t)
  698.   (setq indent-tabs-mode nil)
  699.   ;; Setting up things for font-lock
  700.   (if (string-match "Lucid" emacs-version)
  701.       (put 'f90-mode 'font-lock-keywords-case-fold-search t)
  702.     (make-local-variable 'font-lock-keywords) ; for Emacs version <= 19.28 
  703.     (setq font-lock-keywords f90-font-lock-keywords)
  704.     ;; (make-local-variable 'font-lock-defaults) ; for Emacs version > 19.28
  705.     ;; (setq font-lock-defaults '(f90-font-lock-keywords t))
  706.     )
  707.   (make-local-variable 'font-lock-keywords-case-fold-search)
  708.   (setq font-lock-keywords-case-fold-search t)
  709.   (run-hooks 'f90-mode-hook)
  710.   (if f90-startup-message
  711.       (message "Emacs F90 mode %s. Bugs to %s" f90-mode-version bug-f90-mode))
  712.   (setq f90-startup-message nil))
  713.  
  714. ;; inline-functions
  715. (defsubst f90-get-beg-of-line ()
  716.   (save-excursion (beginning-of-line) (point)))
  717.  
  718. (defsubst f90-get-end-of-line ()
  719.   (save-excursion (end-of-line) (point)))
  720.  
  721. (defsubst f90-in-string ()
  722.   (let ((beg-pnt
  723.      (if (and f90-cache-position (> (point) f90-cache-position))
  724.          f90-cache-position
  725.        (point-min))))
  726.     (nth 3 (parse-partial-sexp beg-pnt (point)))))
  727.         
  728. (defsubst f90-in-comment ()
  729.   (let ((beg-pnt
  730.      (if (and f90-cache-position (> (point) f90-cache-position))
  731.          f90-cache-position
  732.        (point-min))))
  733.     (nth 4 (parse-partial-sexp beg-pnt (point)))))
  734.  
  735. (defsubst f90-line-continued ()
  736.   (save-excursion
  737.     (let ((bol (f90-get-beg-of-line)))
  738.       (end-of-line)
  739.       (while (f90-in-comment)
  740.     (search-backward "!" bol)
  741.     (skip-chars-backward "!"))
  742.       (skip-chars-backward " \t")
  743.       (= (preceding-char) ?&))))
  744.  
  745. (defsubst f90-current-indentation ()
  746.   "Return indentation of current line.
  747. Line-numbers are considered whitespace characters."
  748.   (save-excursion
  749.     (beginning-of-line) (skip-chars-forward " \t0-9")
  750.     (current-column)))
  751.  
  752. (defsubst f90-indent-to (col &optional no-line-number)
  753.   "Indent current line to column COL.
  754. If no-line-number nil, jump over a possible line-number."
  755.   (beginning-of-line)
  756.   (if (not no-line-number)
  757.       (skip-chars-forward " \t0-9"))
  758.   (delete-horizontal-space)
  759.   (if (zerop (current-column))
  760.       (indent-to col)
  761.     (indent-to col 1)))
  762.  
  763. (defsubst f90-match-piece (arg)
  764.   (if (match-beginning arg)
  765.       (buffer-substring (match-beginning arg) (match-end arg))))
  766.  
  767. (defsubst f90-get-present-comment-type ()
  768.   (save-excursion
  769.     (let ((type nil) (eol (f90-get-end-of-line)))
  770.       (if (f90-in-comment)
  771.       (progn
  772.         (beginning-of-line)
  773.         (re-search-forward "[!]+" eol)
  774.         (while (f90-in-string)
  775.           (re-search-forward "[!]+" eol))
  776.         (setq type (buffer-substring (match-beginning 0) (match-end 0)))))
  777.       type)))
  778.  
  779. (defsubst f90-equal-symbols (a b)
  780.   "Compare strings neglecting case and allowing for nil value."
  781.   (let ((a-local (if a (downcase a) nil))
  782.     (b-local (if b (downcase b) nil)))
  783.     (equal a-local b-local)))
  784.  
  785. ;; There seems to be a bug in XEmacs matching of regular expressions.
  786. ;; One cannot extract the label directly for do,if, and select case.
  787. ;; Therefore, the following functions are longer than necessary.
  788.  
  789. (defsubst f90-looking-at-do ()
  790.   "Return (\"do\" name) if a do statement starts after point.
  791. Name is nil if the statement has no label."
  792.   (let (struct (label nil))
  793.     (if (looking-at (concat "\\(\\(" f90-symbol-re "\\)[ \t]*\:\\)?"
  794.                 "[ \t]*\\(do\\)\\b"))
  795.     (progn
  796.       (setq struct (f90-match-piece 3))
  797.       (if (looking-at (concat "\\(" f90-symbol-re "\\)[ \t]*\:"))
  798.           (setq label (f90-match-piece 1)))
  799.       (list struct label)))))
  800.  
  801. (defsubst f90-looking-at-if-then ()
  802.   "Return (\"if\" name) if an if () then statement starts after point.
  803. Name is nil if the statement has no label."
  804.   (save-excursion
  805.     (let (struct (label nil))
  806.       (if (looking-at (concat "\\(\\(" f90-symbol-re "\\)[ \t]*\:\\)?"
  807.                   "[ \t]*\\(if\\)\\b"))
  808.       (progn
  809.         (setq struct (f90-match-piece 3))
  810.         (if (looking-at (concat "\\(" f90-symbol-re "\\)[ \t]*\:"))
  811.         (setq label (f90-match-piece 1)))
  812.         (goto-char (scan-lists (point) 1 0))
  813.         (skip-chars-forward " \t")
  814.         (if (or (looking-at "then\\b")
  815.             (if (f90-line-continued)
  816.             (progn
  817.               (f90-next-statement)
  818.               (skip-chars-forward " \t0-9&")
  819.               (looking-at "then\\b"))))
  820.         (list struct label)))))))
  821.  
  822. (defsubst f90-looking-at-select-case ()
  823.   "Return (\"select\" name) if a select-case statement starts after point.
  824. Name is nil if the statement has no label."
  825.   (let (struct (label nil))
  826.     (if (looking-at (concat "\\(\\(" f90-symbol-re "\\)[ \t]*\:\\)?"
  827.                 "[ \t]*\\(select\\)[ \t]*case[ \t]*("))
  828.     (progn
  829.       (setq struct (f90-match-piece 3))
  830.       (if (looking-at (concat "\\(" f90-symbol-re "\\)[ \t]*\:"))
  831.           (setq label (f90-match-piece 1)))
  832.       (list struct label)))))
  833.  
  834. (defsubst f90-looking-at-where-or-forall ()
  835.   "Return (kind nil) if where/forall...end starts after point."
  836.   (save-excursion
  837.     (let (command)
  838.       (if (looking-at "\\(where\\|forall\\)[ \t]*(")
  839.       (progn
  840.         (setq command (list (f90-match-piece 1) nil))
  841.         (goto-char (scan-lists (point) 1 0))
  842.         (skip-chars-forward " \t")
  843.         (if (looking-at "\\(!\\|$\\)")
  844.         command))))))
  845.  
  846. (defsubst f90-looking-at-type-like ()
  847.   "Return (kind name) at the start of a type/interface/block-data block.
  848. Name is non-nil only for type."
  849.   (cond 
  850.    ((looking-at (concat "\\(type\\)[ \t]+\\(" f90-symbol-re "\\)\\>"))
  851.     (list (f90-match-piece 1) (f90-match-piece 2)))
  852.    ((looking-at f90-p-type-re)
  853.     (list (f90-match-piece 1) (f90-match-piece 3)))
  854.    ((looking-at "\\(interface\\)\\>")
  855.     (list (f90-match-piece 1) nil))
  856.    ((looking-at "\\(block[ \t]*data\\)\\>")
  857.     (list (f90-match-piece 1) nil))))
  858.  
  859. (defsubst f90-looking-at-program-block-start ()
  860.   "Return (kind name) if a program block with name name starts after point."
  861.   (cond
  862.    ((looking-at (concat "\\(program\\)[ \t]+\\(" f90-symbol-re "\\)\\b"))
  863.     (list (f90-match-piece 1) (f90-match-piece 2)))
  864.    ((and (not (looking-at "module[ \t]*procedure\\>"))
  865.      (looking-at (concat "\\(module\\)[ \t]+\\(" 
  866.                  f90-symbol-re "\\)\\b")))
  867.     (list (f90-match-piece 1) (f90-match-piece 2)))
  868.    ((looking-at (concat "\\(recursive[ \t]*\\)?\\(subroutine\\)[ \t]+\\("
  869.             f90-symbol-re "\\)"))
  870.     (list (f90-match-piece 2) (f90-match-piece 3)))
  871.    ((looking-at (concat "[a-z0-9()_ \t]*\\(function\\)[ \t]+\\("
  872.             f90-symbol-re "\\)[ \t]*("))
  873.     (list (f90-match-piece 1) (f90-match-piece 2)))))
  874.  
  875. (defsubst f90-looking-at-program-block-end ()
  876.   "Return list of type and name of end of block."
  877.   (if (looking-at (concat "end[ \t]*" f90-blocks-re "?\\([ \t]+\\("
  878.               f90-symbol-re  "\\)\\)?\\>"))
  879.       (list (f90-match-piece 1) (f90-match-piece 3))))
  880.  
  881. (defsubst f90-comment-indent ()
  882.   (cond ((looking-at "!!!") 0)
  883.     ((and f90-directive-comment
  884.           (looking-at (regexp-quote f90-directive-comment))) 0)
  885.     ((looking-at (regexp-quote f90-comment-region)) 0)
  886.     ((looking-at f90-indented-comment)
  887.      (f90-calculate-indent))
  888.     (t (skip-chars-backward " \t")
  889.        (max (if (bolp) 0 (1+ (current-column))) comment-column))))
  890.  
  891. (defsubst f90-present-statement-cont ()
  892.   "Return continuation properties of present statement."
  893.   (let (pcont cont)
  894.     (save-excursion
  895.       (setq pcont (if (f90-previous-statement) (f90-line-continued) nil)))
  896.     (setq cont (f90-line-continued))
  897.     (cond ((and (not pcont) (not cont)) 'single)
  898.        ((and (not pcont) cont)       'begin)
  899.        ((and pcont       (not cont)) 'end)
  900.        ((and pcont       cont)       'middle)
  901.        (t (error)))))
  902.  
  903. (defsubst f90-indent-line-no ()
  904.   (if f90-leave-line-no
  905.       ()
  906.     (if (and (not (zerop (skip-chars-forward " \t")))
  907.          (looking-at "[0-9]"))
  908.     (delete-horizontal-space)))
  909.   (skip-chars-forward " \t0-9"))
  910.  
  911. (defsubst f90-no-block-limit ()
  912.   (let ((eol (f90-get-end-of-line)))
  913.     (save-excursion
  914.       (not (or (looking-at "end")
  915.            (looking-at "\\(do\\|if\\|else\\|select[ \t]*case\\|\
  916. case\\|where\\|forall\\)\\>")
  917.            (looking-at "\\(program\\|module\\|interface\\|\
  918. block[ \t]*data\\)\\>")
  919.            (looking-at "\\(contains\\|continue\\|\\sw+[ \t]*:\\)")
  920.            (looking-at "type[ \t]+\\sw+")
  921.            (looking-at f90-p-type-re)
  922.            (re-search-forward "\\(function\\|subroutine\\)" eol t))))))
  923.  
  924. (defsubst f90-update-line ()
  925.   (let (bol eol)
  926.     (if (or f90-auto-keyword-case f90-auto-hilit19)
  927.     (progn (setq bol (f90-get-beg-of-line)
  928.              eol (f90-get-end-of-line))
  929.            (if f90-auto-keyword-case
  930.            (f90-change-keywords f90-auto-keyword-case bol eol))
  931.            (if (and f90-auto-hilit19 (fboundp 'hilit-rehighlight-region))
  932.            (hilit-rehighlight-region bol eol t))))))
  933.  
  934. (defun f90-get-correct-indent ()
  935.   "Get correct indent for a line starting with line number.
  936. Does not check type and subprogram indentation."
  937.   (let ((epnt (f90-get-end-of-line)) icol cont)
  938.     (save-excursion
  939.       (while (and (f90-previous-statement)
  940.           (or (progn
  941.             (setq cont (f90-present-statement-cont))
  942.             (or (eq cont 'end) (eq cont 'middle)))
  943.               (looking-at "[ \t]*[0-9]"))))
  944.       (setq icol (current-indentation))
  945.       (beginning-of-line)
  946.       (if (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
  947.                  (f90-get-end-of-line) t)
  948.       (progn
  949.         (beginning-of-line) (skip-chars-forward " \t")
  950.         (cond ((f90-looking-at-do)
  951.            (setq icol (+ icol f90-do-indent)))
  952.           ((or (f90-looking-at-if-then)
  953.                (f90-looking-at-where-or-forall)
  954.                (f90-looking-at-select-case))
  955.            (setq icol (+ icol f90-if-indent))))
  956.         (end-of-line)))
  957.       (while (re-search-forward
  958.           "\\(if\\|do\\|select\\|where\\|forall\\|continue\\)" epnt t)
  959.     (beginning-of-line) (skip-chars-forward " \t0-9")
  960.     (cond  ((f90-looking-at-do)
  961.         (setq icol (+ icol f90-do-indent)))
  962.            ((or (f90-looking-at-if-then)
  963.             (f90-looking-at-where-or-forall)
  964.             (f90-looking-at-select-case))
  965.         (setq icol (+ icol f90-if-indent)))
  966.            ((looking-at f90-end-if-re)
  967.         (setq icol (- icol f90-if-indent)))
  968.            ((looking-at "\\(end[ \t]*do\\|continue\\)\\>")
  969.         (setq icol (- icol f90-do-indent))))
  970.     (end-of-line))
  971.       icol)))
  972.            
  973.       
  974. (defun f90-calculate-indent ()
  975.   "Calculate the indent column based on previous statements."
  976.   (interactive)
  977.   (let (icol cont (case-fold-search t) (pnt (point)))
  978.     (save-excursion
  979.       (if (not (f90-previous-statement))
  980.       (setq icol 0)
  981.     (setq cont (f90-present-statement-cont))
  982.     (if (eq cont 'end)
  983.         (while (not (eq 'begin (f90-present-statement-cont)))
  984.           (f90-previous-statement)))
  985.     (cond ((eq cont 'begin)
  986.            (setq icol (+ (f90-current-indentation)
  987.                  f90-continuation-indent)))
  988.           ((eq cont 'middle) (setq icol(current-indentation)))
  989.           (t (setq icol (f90-current-indentation))
  990.          (skip-chars-forward " \t")
  991.          (if (looking-at "[0-9]")
  992.              (setq icol (f90-get-correct-indent))
  993.            (cond ((or (f90-looking-at-if-then)
  994.                   (f90-looking-at-where-or-forall)
  995.                   (f90-looking-at-select-case)
  996.                   (looking-at f90-else-like-re))               
  997.               (setq icol (+ icol f90-if-indent)))
  998.              ((f90-looking-at-do)
  999.               (setq icol (+ icol f90-do-indent)))
  1000.              ((f90-looking-at-type-like)
  1001.               (setq icol (+ icol f90-type-indent)))
  1002.              ((or (f90-looking-at-program-block-start)
  1003.                   (looking-at "contains[ \t]*\\($\\|!\\)"))
  1004.               (setq icol (+ icol f90-program-indent)))))
  1005.          (goto-char pnt)
  1006.          (beginning-of-line)
  1007.          (cond ((looking-at "[ \t]*$"))
  1008.                ((looking-at "[ \t]*#") ; Check for cpp directive.
  1009.             (setq icol 0))
  1010.                (t
  1011.             (skip-chars-forward " \t0-9")
  1012.             (cond ((or (looking-at f90-else-like-re)
  1013.                    (looking-at f90-end-if-re))
  1014.                    (setq icol (- icol f90-if-indent)))
  1015.                   ((looking-at "\\(end[ \t]*do\\|continue\\)\\>")
  1016.                    (setq icol (- icol f90-do-indent)))
  1017.                   ((looking-at f90-end-type-re)
  1018.                    (setq icol (- icol f90-type-indent)))
  1019.                   ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
  1020.                    (f90-looking-at-program-block-end))
  1021.                    (setq icol (- icol f90-program-indent))))))
  1022.          ))))
  1023.     icol))
  1024.  
  1025. ;; Statement = statement line, a line which is neither blank, nor a comment.
  1026. (defun f90-previous-statement ()
  1027.   "Move point to beginning of the previous F90 statement.
  1028. Return nil if no previous statement is found."
  1029.   (interactive)
  1030.   (let (not-first-statement)
  1031.     (beginning-of-line)
  1032.     (while (and (setq not-first-statement (zerop (forward-line -1)))
  1033.         (looking-at "[ \t0-9]*\\(!\\|$\\)")))
  1034.     not-first-statement))
  1035.  
  1036. (defun f90-next-statement ()
  1037.   "Move point to beginning of the next F90 statement.
  1038. Return nil if no later statement is found."
  1039.   (interactive)
  1040.   (let (not-last-statement)
  1041.     (beginning-of-line)
  1042.     (while (and (setq not-last-statement
  1043.               (and (zerop (forward-line 1))
  1044.                (not (eobp))))
  1045.          (looking-at "[ \t0-9]*\\(!\\|$\\)")))
  1046.     not-last-statement))
  1047.  
  1048. (defun f90-beginning-of-subprogram ()
  1049.   "Move point to the beginning of subprogram.
  1050. Return (type name) or nil if not found."
  1051.   (interactive)
  1052.   (let ((count 1) (case-fold-search t) matching-beg)
  1053.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1054.     (if (setq matching-beg (f90-looking-at-program-block-start)) 
  1055.     (setq count (- count 1)))
  1056.     (while (and (not (zerop count))
  1057.         (re-search-backward f90-program-block-re nil 'move))
  1058.       (beginning-of-line) (skip-chars-forward " \t0-9")
  1059.       (cond 
  1060.        ((setq matching-beg (f90-looking-at-program-block-start))
  1061.     (setq count (- count 1)))
  1062.        ((f90-looking-at-program-block-end)
  1063.     (setq count (+ count 1)))))
  1064.     (beginning-of-line)
  1065.     (if (zerop count)
  1066.     matching-beg
  1067.       (message "No beginning-found.")
  1068.       nil)))
  1069.  
  1070. (defun f90-end-of-subprogram ()
  1071.   "Move point to the end of subprogram.
  1072. Return (type name) or nil if not found."
  1073.   (interactive)
  1074.   (let ((count 1) (case-fold-search t) matching-end)
  1075.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1076.     (if (setq matching-end (f90-looking-at-program-block-end))
  1077.     (setq count (1- count)))
  1078.     (end-of-line)
  1079.     (while (and (not (zerop count))
  1080.         (re-search-forward f90-program-block-re nil 'move))
  1081.       (beginning-of-line) (skip-chars-forward " \t0-9")
  1082.       (cond ((f90-looking-at-program-block-start)
  1083.          (setq count (+ count 1)))
  1084.         ((setq matching-end (f90-looking-at-program-block-end))
  1085.          (setq count (1- count ))))
  1086.       (end-of-line))
  1087.     (forward-line 1)
  1088.     (if (zerop count)
  1089.     matching-end
  1090.       (message "No end found.")
  1091.       nil)))
  1092.  
  1093. (defun f90-mark-subprogram ()
  1094.   "Put mark at end of F90 subprogram, point at beginning.
  1095. Marks are pushed and highlight (grey shadow) is turned on."
  1096.   (interactive)
  1097.   (let ((pos (point)) program)
  1098.     (f90-end-of-subprogram)
  1099.     (push-mark (point) t)
  1100.     (goto-char pos)
  1101.     (setq program (f90-beginning-of-subprogram))
  1102.     ;; The keywords in the preceding lists assume case-insensitivity.
  1103.     (if (string-match "Lucid" emacs-version)
  1104.     (zmacs-activate-region)
  1105.       (setq mark-active t)
  1106.       (setq deactivate-mark nil))
  1107.     program))
  1108.  
  1109. (defun f90-comment-region (beg-region end-region)
  1110.   "Comment/uncomment every line in the region.
  1111. Insert f90-comment-region at the beginning of every line in the region
  1112. or, if already present, remove it."
  1113.   (interactive "*r")
  1114.   (let ((end (make-marker)))
  1115.     (set-marker end end-region)
  1116.     (goto-char beg-region)
  1117.     (beginning-of-line)
  1118.     (if (looking-at (regexp-quote f90-comment-region))
  1119.     (delete-region (point) (match-end 0))
  1120.       (insert f90-comment-region))
  1121.     (while (and  (zerop (forward-line 1))
  1122.          (< (point) (marker-position end)))
  1123.       (if (looking-at (regexp-quote f90-comment-region))
  1124.       (delete-region (point) (match-end 0))
  1125.     (insert f90-comment-region)))
  1126.     (set-marker end nil)))
  1127.  
  1128. (defun f90-indent-line (&optional no-update)
  1129.   "Indent current line as F90 code."
  1130.   (interactive)
  1131.   (let (indent (no-line-number nil) (pos (make-marker)) (case-fold-search t))
  1132.     (set-marker pos (point))
  1133.     (beginning-of-line)            ; Digits after & \n are not line-no
  1134.     (if (save-excursion (and (f90-previous-statement) (f90-line-continued)))
  1135.     (progn (setq no-line-number t) (skip-chars-forward " \t"))
  1136.       (f90-indent-line-no))
  1137.     (if (looking-at "!")
  1138.     (setq indent (f90-comment-indent))
  1139.       (if (and (looking-at "end") f90-smart-end) (f90-match-end))
  1140.       (setq indent (f90-calculate-indent)))
  1141.     (if (zerop (- indent (current-column)))
  1142.     nil
  1143.       (f90-indent-to indent no-line-number))
  1144.     ;; If initial point was within line's indentation,
  1145.     ;; position after the indentation.  Else stay at same point in text.
  1146.     (if (< (point) (marker-position pos))
  1147.     (goto-char (marker-position pos)))
  1148.     (if (not no-update) (f90-update-line))
  1149.     (if (and auto-fill-function
  1150.          (> (save-excursion (end-of-line) (current-column)) fill-column))
  1151.     (save-excursion (f90-do-auto-fill)))
  1152.     (set-marker pos nil)))
  1153.  
  1154. (defun f90-indent-new-line ()
  1155.   "Reindent the current F90 line, insert a newline and indent the newline.
  1156. An abbrev before point is expanded if `abbrev-mode' is non-nil.
  1157. If run in the middle of a line, the line is not broken."
  1158.   (interactive)
  1159.   (let (string cont (case-fold-search t))
  1160.     (if abbrev-mode (expand-abbrev))
  1161.     (beginning-of-line)            ; Reindent where likely to be needed.
  1162.     (f90-indent-line-no)
  1163.     (if (or (looking-at "\\(end\\|else\\|!\\)"))
  1164.     (f90-indent-line 'no-update))
  1165.     (end-of-line)
  1166.     (delete-horizontal-space)        ;Destroy trailing whitespace
  1167.     (setq string (f90-in-string))
  1168.     (setq cont (f90-line-continued))
  1169.     (if (and string (not cont)) (insert "&"))
  1170.     (f90-update-line)
  1171.     (newline)
  1172.     (if (or string (and cont f90-beginning-ampersand)) (insert "&"))
  1173.     (f90-indent-line 'no-update)))
  1174.  
  1175.  
  1176. (defun f90-indent-region (beg-region end-region)
  1177.   "Indent every line in region by forward parsing."
  1178.   (interactive "*r")
  1179.   (let ((end-region-mark (make-marker)) (save-point (point-marker))
  1180.     (block-list nil) ind-lev ind-curr ind-b cont
  1181.     struct beg-struct end-struct)
  1182.     (set-marker end-region-mark end-region)
  1183.     (goto-char beg-region)
  1184.     ;; first find a line which is not a continuation line or comment
  1185.     (beginning-of-line)
  1186.     (while (and (looking-at "[ \t]*[0-9]*\\(!\\|[ \t]*$\\)")
  1187.         (progn (f90-indent-line 'no-update)
  1188.                (zerop (forward-line 1)))
  1189.         (< (point) end-region-mark)))
  1190.     (setq cont (f90-present-statement-cont))
  1191.     (while (and (or (eq cont 'middle) (eq cont 'end))
  1192.         (f90-previous-statement))
  1193.       (setq cont (f90-present-statement-cont)))
  1194.     ;; process present line for beginning of block
  1195.     (setq f90-cache-position (point))
  1196.     (f90-indent-line 'no-update)
  1197.     (setq ind-lev (f90-current-indentation))
  1198.     (setq ind-curr ind-lev)
  1199.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1200.     (setq struct nil)
  1201.     (setq ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
  1202.               ((or (setq struct (f90-looking-at-if-then))
  1203.                (setq struct (f90-looking-at-select-case))
  1204.                (setq struct (f90-looking-at-where-or-forall))
  1205.                (looking-at f90-else-like-re))
  1206.                f90-if-indent)
  1207.               ((setq struct (f90-looking-at-type-like))
  1208.                f90-type-indent)
  1209.               ((or(setq struct (f90-looking-at-program-block-start))
  1210.               (looking-at "contains[ \t]*\\($\\|!\\)"))
  1211.                f90-program-indent)))
  1212.     (if ind-b (setq ind-lev (+ ind-lev ind-b)))
  1213.     (if struct (setq block-list (cons struct block-list)))
  1214.     (while (and (f90-line-continued) (zerop (forward-line 1))
  1215.         (< (point) end-region-mark))
  1216.       (if (not (zerop (- (current-indentation) 
  1217.              (+ ind-curr f90-continuation-indent))))
  1218.       (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no)))
  1219.     ;; process all following lines
  1220.     (while (and  (zerop (forward-line 1)) (< (point) end-region-mark))
  1221.       (beginning-of-line)
  1222.       (f90-indent-line-no)
  1223.       (setq f90-cache-position (point))
  1224.       (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
  1225.         ((looking-at "[ \t]*#") (setq ind-curr 0))
  1226.         ((looking-at "!") (setq ind-curr (f90-comment-indent)))
  1227.         ((f90-no-block-limit) (setq ind-curr ind-lev))
  1228.         ((looking-at f90-else-like-re) (setq ind-curr
  1229.                          (- ind-lev f90-if-indent)))
  1230.         ((looking-at "contains[ \t]*\\($\\|!\\)")
  1231.          (setq ind-curr (- ind-lev f90-program-indent)))
  1232.         ((setq ind-b
  1233.            (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
  1234.              ((or (setq struct (f90-looking-at-if-then))
  1235.                   (setq struct (f90-looking-at-select-case))
  1236.                   (setq struct (f90-looking-at-where-or-forall)))
  1237.               f90-if-indent)
  1238.              ((setq struct (f90-looking-at-type-like))
  1239.               f90-type-indent)
  1240.              ((setq struct (f90-looking-at-program-block-start))
  1241.               f90-program-indent)))
  1242.          (setq ind-curr ind-lev)
  1243.          (if ind-b (setq ind-lev (+ ind-lev ind-b)))
  1244.          (setq block-list (cons struct block-list)))
  1245.         ((setq end-struct (f90-looking-at-program-block-end))
  1246.          (setq beg-struct (car block-list)
  1247.            block-list (cdr block-list))
  1248.          (if f90-smart-end 
  1249.          (save-excursion
  1250.            (f90-block-match (car beg-struct)(car (cdr beg-struct))
  1251.                     (car end-struct)(car (cdr end-struct)))))
  1252.          (setq ind-b
  1253.            (cond ((looking-at f90-end-if-re) f90-if-indent)
  1254.              ((looking-at "end[ \t]*do\\>")  f90-do-indent)
  1255.              ((looking-at f90-end-type-re) f90-type-indent)
  1256.              ((f90-looking-at-program-block-end)
  1257.               f90-program-indent)))
  1258.          (if ind-b (setq ind-lev (- ind-lev ind-b)))
  1259.          (setq ind-curr ind-lev))
  1260.         ((looking-at "continue\\>")
  1261.          (setq ind-lev (- ind-lev f90-do-indent))
  1262.          (setq ind-curr ind-lev)
  1263.          (setq block-list (cdr block-list)))
  1264.         (t (setq ind-curr ind-lev)))
  1265.       ;; do the indentation if necessary
  1266.       (if (not (zerop (- ind-curr (current-column))))
  1267.       (f90-indent-to ind-curr))
  1268.       (while (and (f90-line-continued) (zerop (forward-line 1))
  1269.           (< (point) end-region-mark))
  1270.     (if (not (zerop (- (current-indentation) 
  1271.                (+ ind-curr f90-continuation-indent))))
  1272.         (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
  1273.     ;; restore point etc
  1274.     (setq f90-cache-position nil)
  1275.     (goto-char save-point)
  1276.     (set-marker end-region-mark nil)
  1277.     (set-marker save-point nil)
  1278.     (if (string-match "Lucid" emacs-version)
  1279.     (zmacs-deactivate-region)
  1280.       (deactivate-mark))))
  1281.  
  1282. (defun f90-indent-subprogram ()
  1283.   "Properly indent the subprogram which contains point."
  1284.   (interactive)
  1285.   (save-excursion
  1286.     (let (program)
  1287.       (setq program (f90-mark-subprogram))
  1288.       (if program
  1289.       (progn
  1290.         (message (concat "Indenting " (car program) " "
  1291.                  (car (cdr program))"."))
  1292.         (f90-indent-region (point) (mark))
  1293.         (message (concat "Indenting " (car program) " "
  1294.                  (car (cdr program)) "...done.")))
  1295.     (message "Indenting the whole file.")
  1296.     (f90-indent-region (point) (mark))
  1297.     (message (concat "Indenting the whole file...done."))))))
  1298.  
  1299. ;; autofill and break-line
  1300. (defun f90-break-line (&optional no-update)
  1301.   "Break line at point, insert continuation marker(s) and indent."
  1302.   (interactive)
  1303.   (let (ctype)
  1304.     (cond ((f90-in-string)
  1305.        (insert "&") (newline) (insert "&"))
  1306.       ((f90-in-comment)
  1307.        (delete-horizontal-space)
  1308.        (setq ctype (f90-get-present-comment-type))
  1309.        (newline) (insert (concat ctype " ")))
  1310.       (t (delete-horizontal-space)
  1311.          (insert "&")
  1312.          (if (not no-update) (f90-update-line))
  1313.          (newline)
  1314.          (if f90-beginning-ampersand (insert "& ")))))
  1315.   (if (not no-update) (f90-indent-line)))
  1316.   
  1317. (defun f90-find-breakpoint ()
  1318.   "From fill-column, search backward for break-delimiter."
  1319.   (let ((bol (f90-get-beg-of-line)))
  1320.     (re-search-backward f90-break-delimiters bol)
  1321.     (if f90-break-before-delimiters
  1322.     (progn (backward-char)
  1323.            (if (not (looking-at f90-no-break-re))
  1324.            (forward-char)))
  1325.       (if (looking-at f90-no-break-re)
  1326.       (forward-char 2)
  1327.     (forward-char)))))
  1328.  
  1329. (defun f90-auto-fill-mode (arg)
  1330.   "Toggle f90-auto-fill mode.
  1331. With ARG, turn `f90-auto-fill' mode on iff ARG is positive.
  1332. In `f90-auto-fill' mode, inserting a space at a column beyond `fill-column'
  1333. automatically breaks the line at a previous space."
  1334.   (interactive "P")
  1335.   (prog1 (setq auto-fill-function
  1336.            (if (if (null arg)
  1337.                (not auto-fill-function)
  1338.              (> (prefix-numeric-value arg) 0))
  1339.            'f90-do-auto-fill))
  1340.     ;; update mode-line
  1341.     (set-buffer-modified-p (buffer-modified-p))))
  1342.  
  1343. (defun f90-do-auto-fill ()
  1344.   "Break line if non-white characters beyond fill-column."
  1345.   (interactive)
  1346.   ;; Break the line before or after the last delimiter (non-word char).
  1347.   ;; Will not break **, //, or => (specified by f90-no-break-re).
  1348.   (move-to-column fill-column)
  1349.   (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
  1350.       (delete-horizontal-space)
  1351.     (f90-find-breakpoint)
  1352.     (f90-break-line)
  1353.     (end-of-line)))
  1354.  
  1355. (defun f90-join-lines ()
  1356.   "Join present line with next line, if this line ends with \&."
  1357.   (interactive)
  1358.   (let (pos (oldpos (point)))
  1359.     (end-of-line)
  1360.     (skip-chars-backward " \t")
  1361.     (cond ((= (preceding-char) ?&)
  1362.        (delete-char -1)
  1363.        (setq pos (point))
  1364.        (forward-line 1)
  1365.        (skip-chars-forward " \t")
  1366.        (if (looking-at "\&") (delete-char 1))
  1367.        (delete-region pos (point))
  1368.        (if (not (f90-in-string))
  1369.            (progn (delete-horizontal-space) (insert " ")))
  1370.        (if (and auto-fill-function
  1371.             (> (save-excursion (end-of-line)
  1372.                        (current-column))
  1373.                fill-column))
  1374.            (f90-do-auto-fill))
  1375.        (goto-char oldpos)
  1376.        t))))
  1377.  
  1378. (defun f90-fill-region (beg-region end-region)
  1379.   "Fill every line in region by forward parsing. Join lines if possible."
  1380.   (interactive "*r")
  1381.   (let ((end-region-mark (make-marker))
  1382.     (f90-smart-end nil) (f90-auto-keyword-case nil)
  1383.     (f90-auto-hilit19 nil) indent (go-on t)
  1384.     (af-function auto-fill-function) (auto-fill-function nil))
  1385.     (set-marker end-region-mark end-region)
  1386.     (goto-char beg-region)
  1387.     (while go-on
  1388.       ;; join as much as possible
  1389.       (while (f90-join-lines));
  1390.       (setq indent (+ (f90-current-indentation) f90-continuation-indent))
  1391.       ;; chop the line if necessary
  1392.       (while (> (save-excursion (end-of-line) (current-column))
  1393.         fill-column)
  1394.     (move-to-column fill-column)
  1395.     (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
  1396.         (delete-horizontal-space)
  1397.       (f90-find-breakpoint)
  1398.       (f90-break-line 'no-update)
  1399.       (f90-indent-to indent 'no-line-no)))
  1400.       (setq go-on (and  (< (point) (marker-position end-region-mark))
  1401.             (zerop (forward-line 1))))
  1402.       (setq f90-cache-position (point)))
  1403.     (setq auto-fill-function af-function)
  1404.     (setq f90-cache-position nil)
  1405.     (if (string-match "Lucid" emacs-version)
  1406.     (zmacs-deactivate-region)
  1407.       (deactivate-mark))))
  1408.  
  1409. (defun f90-block-match (beg-block beg-name end-block end-name)
  1410.   "Match end-struct with beg-struct and complete end-block if possible.
  1411. Leave point at the end of line."
  1412.   (search-forward "end" (f90-get-end-of-line))
  1413.   (catch 'no-match
  1414.     (if (not (f90-equal-symbols beg-block end-block))
  1415.     (if end-block
  1416.         (progn
  1417.           (message "END %s does not match %s." end-block beg-block)
  1418.           (end-of-line) 
  1419.           (throw 'no-match nil))
  1420.       (message "Inserting %s." beg-block)
  1421.       (insert (concat " " beg-block)))
  1422.       (search-forward end-block))
  1423.     (if (not (f90-equal-symbols beg-name end-name))
  1424.     (cond ((and beg-name (not end-name)) 
  1425.            (message "Inserting %s." beg-name)
  1426.            (insert (concat " " beg-name)))
  1427.           ((and beg-name end-name) 
  1428.            (message "Replacing %s with %s." end-name beg-name)
  1429.            (search-forward end-name)
  1430.            (replace-match beg-name))
  1431.           ((and (not beg-name) end-name) 
  1432.            (message "Deleting %s." end-name)
  1433.            (search-forward end-name)
  1434.            (replace-match "")))
  1435.       (if end-name (search-forward end-name)))
  1436.     (if (not (looking-at "!")) (delete-horizontal-space))))
  1437.  
  1438. (defun f90-match-end ()
  1439.   "From an end foo statement, find the corresponding foo including name."
  1440.   (interactive)
  1441.   (let ((count 1) (top-of-window (window-start)) (matching-beg nil)
  1442.     (end-point (point)) (case-fold-search t)
  1443.     beg-name end-name beg-block end-block end-struct)
  1444.     (if (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
  1445.             (setq end-struct (f90-looking-at-program-block-end)))
  1446.     (progn
  1447.       (setq end-block (car end-struct))
  1448.       (setq end-name  (car (cdr end-struct)))
  1449.       (save-excursion
  1450.         (beginning-of-line)
  1451.         (while (and (not (zerop count))
  1452.             (re-search-backward 
  1453.              (concat "\\(" f90-blocks-re "\\|continue\\)") nil t))
  1454.           (beginning-of-line) (skip-chars-forward " \t0-9")
  1455.           (cond ((setq matching-beg
  1456.                (cond
  1457.                 ((f90-looking-at-do))
  1458.                 ((f90-looking-at-if-then))
  1459.                 ((f90-looking-at-where-or-forall))
  1460.                 ((f90-looking-at-select-case))
  1461.                 ((f90-looking-at-type-like))
  1462.                 ((f90-looking-at-program-block-start))))
  1463.              (setq count (- count 1)))
  1464.             ((looking-at (concat "end[ \t]*" f90-blocks-re "\\b"))
  1465.              (setq count (+ count 1)))
  1466.             ((looking-at "continue\\>") (setq count (+ count 1)))))
  1467.         (if (not (zerop count))
  1468.         (message "No matching beginning.")
  1469.           (f90-update-line)
  1470.           (if (eq f90-smart-end 'blink)
  1471.           (if (< (point) top-of-window)
  1472.               (message (concat 
  1473.                 "Matches " (what-line) ": "
  1474.                 (buffer-substring
  1475.                  (progn (beginning-of-line) (point))
  1476.                  (progn (end-of-line) (point)))))
  1477.             (sit-for 1)))
  1478.           (setq beg-block (car matching-beg))
  1479.           (setq beg-name (car (cdr matching-beg)))
  1480.           (goto-char end-point)
  1481.           (beginning-of-line)
  1482.           (f90-block-match beg-block beg-name end-block end-name)))))))
  1483.  
  1484. (defun f90-insert-end ()
  1485.   "Inserts an complete end statement matching beginning of present block."
  1486.   (interactive)
  1487.   (let ((f90-smart-end (if f90-smart-end f90-smart-end 'blink)))
  1488.     (insert "end")
  1489.     (f90-indent-new-line)))
  1490.  
  1491. ;; abbrevs and keywords
  1492.  
  1493. (defun f90-abbrev-start ()
  1494.   "Typing `\\[help-command] or `? lists all the F90 abbrevs. 
  1495. Any other key combination is executed normally."
  1496.   (interactive)
  1497.   (let (c)
  1498.     (insert last-command-char)
  1499.     (if (or (eq (setq c (if (string-match "Lucid" emacs-version)
  1500.                 (event-to-character (next-command-event))
  1501.               (read-event)))
  1502.         ??) ;insert char if not equal to `?'
  1503.         (eq c help-char))
  1504.     (f90-abbrev-help)
  1505.       (if (string-match "Lucid" emacs-version)
  1506.       (setq unread-command-event c)
  1507.     (setq unread-command-events (list c))))))
  1508.  
  1509. (defun f90-abbrev-help ()
  1510.   "List the currently defined abbrevs in F90 mode."
  1511.   (interactive)
  1512.   (message "Listing abbrev table...")
  1513.   (display-buffer (f90-prepare-abbrev-list-buffer))
  1514.   (message "Listing abbrev table...done"))
  1515.  
  1516. (defun f90-prepare-abbrev-list-buffer ()
  1517.   (save-excursion
  1518.     (set-buffer (get-buffer-create "*Abbrevs*"))
  1519.     (erase-buffer)
  1520.     (insert-abbrev-table-description 'f90-mode-abbrev-table t)
  1521.     (goto-char (point-min))
  1522.     (set-buffer-modified-p nil)
  1523.     (edit-abbrevs-mode))
  1524.   (get-buffer-create "*Abbrevs*"))
  1525.  
  1526. (defun f90-upcase-keywords ()
  1527.   "Upcase all F90 keywords in the buffer."
  1528.   (interactive)
  1529.   (f90-change-keywords 'upcase-word))
  1530.  
  1531. (defun f90-capitalize-keywords ()
  1532.   "Capitalize all F90 keywords in the buffer."
  1533.   (interactive)
  1534.   (f90-change-keywords 'capitalize-word))
  1535.  
  1536. (defun f90-downcase-keywords ()
  1537.   "Downcase all F90 keywords in the buffer."
  1538.   (interactive)
  1539.   (f90-change-keywords 'downcase-word))
  1540.  
  1541. (defun f90-upcase-region-keywords (beg end)
  1542.   "Upcase all F90 keywords in the region."
  1543.   (interactive "*r")
  1544.   (f90-change-keywords 'upcase-word beg end))
  1545.  
  1546. (defun f90-capitalize-region-keywords (beg end)
  1547.   "Capitalize all F90 keywords in the region."
  1548.   (interactive "*r")
  1549.   (f90-change-keywords 'capitalize-word beg end))
  1550.  
  1551. (defun f90-downcase-region-keywords (beg end)
  1552.   "Downcase all F90 keywords in the region."
  1553.   (interactive "*r")
  1554.   (f90-change-keywords 'downcase-word beg end))
  1555.  
  1556. ;; Change the keywords according to argument.
  1557. (defun f90-change-keywords (change-word &optional beg end)
  1558.   (save-excursion
  1559.     (setq beg (if beg beg (point-min)))
  1560.     (setq end (if end end (point-max)))
  1561.     (let ((keyword-re 
  1562.        (concat "\\<\\("
  1563.            (mapconcat 'identity f90-keywords "\\|")            "\\|"
  1564.            (mapconcat 'identity f90-intrinsic-procedures"\\|") "\\|"
  1565.            (mapconcat 'identity f90-hpf-procedures "\\|")      "\\|"
  1566.            (mapconcat 'identity f90-hpf-directives "\\|")      "\\|"
  1567.            (mapconcat 'identity f90-hpf-keywords "\\|")
  1568.            "\\)\\>")) (ref-point (point-min)) state)
  1569.       (goto-char beg)
  1570.       (while (re-search-forward keyword-re end t)
  1571.     (if (progn
  1572.           (setq state (parse-partial-sexp ref-point (point)))
  1573.           (or (nth 3 state) (nth 4 state)
  1574.           (save-excursion    ; Check for cpp directive.
  1575.             (beginning-of-line)
  1576.             (skip-chars-forward " \t0-9")
  1577.             (looking-at "#"))))
  1578.         ()
  1579.       (setq ref-point (point))
  1580.       (funcall change-word -1))))))
  1581.  
  1582. (provide 'f90)
  1583.  
  1584. ;;; f90.el ends here
  1585.