home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / fortran.el < prev    next >
Encoding:
Text File  |  1992-10-28  |  51.4 KB  |  1,287 lines

  1. ;;; fortran.el --- Fortran mode for GNU Emacs
  2.  
  3. ;;; Copyright (c) 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Michael D. Prange <prange@erl.mit.edu>
  6. ;; Maintainer: bug-fortran-mode@erl.mit.edu
  7. ;; Version 1.28.7
  8. ;; Keywords: languages
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  24. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; fortran.el version 1.28.7, October 20, 1992
  29. ;; Many contributions and valuable suggestions by
  30. ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
  31. ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon, 
  32. ;; Gary Sabot and Richard Stallman.
  33.  
  34. ;; Maintained (as of version 1.28) by Stephen A. Wood (saw@cebaf.gov)
  35.  
  36. ;;; This version is an update of version 1.21 (Oct 1, 1985).
  37. ;;;  Updated by Stephen A. Wood (saw@cebaf.gov) to use tab format
  38. ;;;  continuation control and indentation.  (Digit after TAB to signify 
  39. ;;;  continuation line.
  40.  
  41. ;;; Notes to fortran-mode version 1.28
  42. ;;;  1. Fortran mode can support either fixed format or tab format.  Fixed
  43. ;;;     format is where statements start in column 6 (first column is 0)
  44. ;;;     and continuation lines are denoted by a character in column 5.
  45. ;;;     In tab mode, statements follow a tab character.  Continuation lines
  46. ;;;     are where the first character on a line is a tab and the second is
  47. ;;;     a digit from 1 to 9.
  48. ;;;  2. When fortran mode is called, the buffer is analyzed to determine what
  49. ;;;     kind of formating is used.  Starting at the top of the file, lines
  50. ;;;     are scanned until a line is found that begins with a tab or 6 spaces.
  51. ;;;     The mode for that buffer is then set to either tab or fixed format
  52. ;;;     based on that line.  If no line starting with a tab or 6 spaces is
  53. ;;;     found before the end  of the buffer or in the first 100 lines, the
  54. ;;;     mode is set from the variable `fortran-tab-mode-default'.  t is tab
  55. ;;;     mode, nil is fixed format mode.  By default, fixed format mode is used.
  56. ;;;     To use tabbing mode as the default, put the following line in .emacs
  57. ;;;           (setq fortran-tab-mode-default t)
  58. ;;;     This line should not be in the hook since the hook is called only
  59. ;;;     after the file is analyzed.
  60. ;;;     To force a particular mode independent of the analysis, attach
  61. ;;;           (fortran-tab-mode t) or (fortran-tab-mode nil)
  62. ;;;     to fortran-mode-hook.
  63. ;;;  3. The command `fortran-tab-mode' will toggle between fixed and tab
  64. ;;;     formatting modes.  The file will not automatically be reformatted,
  65. ;;;     but either `indent-region' or `fortran-indent-subprogram' can be
  66. ;;;     used to reformat portions of the file.
  67. ;;;  4. Several abbreviations have been added.  Abbreviation mode is turned
  68. ;;;     on by default.
  69. ;;;  5. The routine fortran-blink-matching if has been incorporated (from
  70. ;;;     experimental version 1.27).  If the variable of the same name is set
  71. ;;;     to t, the the matching if statement is blinked whenever an endif
  72. ;;;     line is indented.
  73. ;;;  6. C-c C-w is now bound to fortran-window-create-momentarily (from
  74. ;;;     experimental version 1.27.)
  75. ;;;  7. LFD is now bound to fortran-reindent-then-newline-and-indent.
  76. ;;;  8. fortran-continuation-string (was fortran-continuation-char) is now
  77. ;;;     a string rather than a character.
  78. ;;;  9. Fixed a bug from 1.21 that gave max-lisp-eval-depth exceeded when
  79. ;;;     Comments lines had !'s in them.
  80. ;;; 10. DEC fortran recognizes a & in the first column as a continuation.
  81. ;;;     character.  This mode does not recognize the & as a continuation
  82. ;;;     character.
  83. ;;; 11. fortran-blink-matching-if still is in effect when indenting a region.
  84. ;;;     Is this a desirable effect?  (It looks kind of neat)
  85. ;;; 12. If you strike a digit and there are exactly 5 characters, all spaces
  86. ;;;     to the left of the point, the digit will be inserted in place to
  87. ;;;     serve as a continuation line marker.  Similarly, if the only thing to
  88. ;;;     the left of the point is a single tab, and the last command issued
  89. ;;;     was neither fortran-indent-line (TAB) or fortran-reindent-then-newline-
  90. ;;;     and-indent (LFD), the digit is inserted as a tab format style
  91. ;;;     continuation character.
  92. ;;; 13. Both modes should usually work with tab-width set to other than 8.
  93. ;;;     However, in tab-mode, if tab-width is less than 6, the column number
  94. ;;;     for the minimum indentation is set to 6 so that all line numbers will
  95. ;;;     have have a tab after them.  This may be a bit ugly, but why would
  96. ;;;     you want to use a tab-width other than 8 anyway?
  97. ;;; 14. When in tab mode, the fortran column ruler will not be correct if
  98. ;;;     tab-width is not 8.
  99. ;;; 15. Fortran-electic-line-number will work properly in overwrite-mode.
  100. ;;;     Thanks to Mark Neale (mjn@jet.uk)
  101. ;;; 16. Fixed bug in fortran-previous-statement that gives "Incomplete
  102. ;;;     continuation statement." when used on the first statement which
  103. ;;;     happens to be a comment that begins with the same character as
  104. ;;;     `fortran-continuation-string'
  105. ;;; 17. If `comment-start-skip' is found in a fortran string, no indenting is
  106. ;;;     done.  Thanks to Ralf Fassel (ralf@up3aud1.gwdg.de) for patches.
  107. ;;;     This awaits a hopeful future multimode solution in which
  108. ;;;     indentation/spacing inside of constants doesn't get touched when
  109. ;;;     comment delimeter characters happen to be inside the constant.
  110. ;;; 18. Changed meaning of `fortran-comment-line-column'.  If
  111. ;;;     If `fortran-comment-indent-style' is 'fixed, then, comments are
  112. ;;;     indented to `fortran-minimum-statement-indent' plus
  113. ;;;     `fortran-comment-line-column'.  If the style is `relative', the
  114. ;;;     meaning remains the same in that the line-column value is added to
  115. ;;;     the current indentation level.  The default value is now zero.
  116. ;;;     (Thanks to Ulrich Mueller (ulm@vsnhd1.cern.ch).
  117. ;;; 19. Fixed infinite loop in fortran-next-statement that occurs with emacs
  118. ;;;     versions 18.55 and before because of a difference in the behavior
  119. ;;;     of (forward-line 1) on a line that is the last in the buffer which
  120. ;;;     doesn't have a newline.  (Thanks to Ulrich Mueller)
  121. ;;; 20. Added indentation for structure, union and map blocks (Fortran 90
  122. ;;;     and other post f77 fortrans.) at the suggestion of Dr. Anil Gokhale
  123. ;;;     (avg@dynsim1.litwin.com).
  124. ;;; 21. The command fortran-auto-fill-mode toggles on and off fortran-auto-fill
  125. ;;;     mode.  By default it is off, and the fill column is 72.  (Thanks to
  126. ;;;     (Mark Neale for the code for auto fill.)  Will split line before
  127. ;;;     whitespace, commas, or operators.  Won't break stuff betweek quotes,
  128. ;;;     unless it is a comment line.  Put (fortran-auto-fill-mode 1) into
  129. ;;;     fortran-mode-hook to have auto fill mode automatically on.
  130. ;;; 22. If auto-fill-mode is on, fortran-indent-line will call the auto fill
  131. ;;;     code to make sure that lines are not to long after indentation.  This
  132. ;;;     suggestion and improvements to auto filling provided by Eric Prestemon
  133. ;;;     (ecprest@pocorvares.er.usgs.gov.
  134. ;;; 23. comment-line-start-skip treats cpp directives (beginning with #) as
  135. ;;;     unindentable comments.
  136. ;;; 24. where, elsewhere indenting now supported for F90.
  137. ;;; 
  138.  
  139. ;;; Bugs to bug-fortran-mode@erl.mit.edu
  140.  
  141. (defconst fortran-mode-version "version 1.28.7")
  142.  
  143. ;;; Code:
  144.  
  145. ;;;###autoload
  146. (defvar fortran-tab-mode-default nil
  147.   "*Default tabbing/carriage control style for empty files in fortran mode.
  148. t indicates that tab-digit style of continuation control will be used.
  149. nil indicates that continuation lines are marked with a character in
  150. column 6.")
  151.  
  152. (defvar fortran-do-indent 3
  153.   "*Extra indentation applied to `do' blocks.")
  154.  
  155. (defvar fortran-if-indent 3
  156.   "*Extra indentation applied to `if' blocks.")
  157.  
  158. (defvar fortran-structure-indent 3
  159.   "*Extra indentation applied to `structure', `union' and `map' blocks.")
  160.  
  161. (defvar fortran-continuation-indent 5
  162.   "*Extra indentation applied to `continuation' lines.")
  163.  
  164. (defvar fortran-comment-indent-style 'fixed
  165.   "*nil forces comment lines not to be touched,
  166. 'fixed produces fixed comment indentation to fortran-comment-line-column
  167. beyond fortran-minimum-statement-indent, and 'relative indents to current
  168. fortran indentation plus fortran-comment-line-column.")
  169.  
  170. (defvar fortran-comment-line-column 0
  171.   "*Amount of extra indentation for text within full-line comments.")
  172.  
  173. (defvar comment-line-start nil
  174.   "*Delimiter inserted to start new full-line comment.")
  175.  
  176. (defvar comment-line-start-skip nil
  177.   "*Regexp to match the start of a full-line comment.")
  178.  
  179. (defvar fortran-minimum-statement-indent 6
  180.   "*Minimum indentation for fortran statements.")
  181.  
  182. ;; Note that this is documented in the v18 manuals as being a string
  183. ;; of length one rather than a single character.
  184. ;; The code in this file accepts either format for compatibility.
  185. (defvar fortran-comment-indent-char " "
  186.   "*Single-character string inserted for Fortran comment indentation.
  187. Normally a space.")
  188.  
  189. (defvar fortran-line-number-indent 1
  190.   "*Maximum indentation for Fortran line numbers.
  191. 5 means right-justify them within their five-column field.")
  192.  
  193. (defvar fortran-check-all-num-for-matching-do nil
  194.   "*Non-nil causes all numbered lines to be treated as possible do-loop ends.")
  195.  
  196. (defvar fortran-blink-matching-if nil
  197.   "*From a fortran `endif' statement, blink the matching `if' statement.")
  198.  
  199. (defvar fortran-continuation-string "$"
  200.   "*Single-character string used for fortran continuation lines.
  201. In fixed format continuation style, this character is inserted in
  202. column 6 by \\[fortran-split-line] to begin a continuation line.
  203. Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
  204. convert the line into a continuation line of the appropriate style.
  205. Normally $.")
  206.  
  207. (defvar fortran-comment-region "c$$$"
  208.   "*String inserted by \\[fortran-comment-region]\
  209.  at start of each line in region.")
  210.  
  211. (defvar fortran-electric-line-number t
  212.   "*Non-nil causes line number digits to be moved to the correct column as\
  213.  typed.")
  214.  
  215. (defvar fortran-startup-message t
  216.   "*Non-nil displays a startup message when Fortran mode is first called.")
  217.  
  218. (defvar fortran-column-ruler " "
  219.   "*String displayed above current line by \\[fortran-column-ruler].")
  220.  
  221. (defconst bug-fortran-mode "bug-fortran-mode@erl.mit.edu"
  222.   "Address of mailing list for Fortran mode bugs.")
  223.  
  224. (defvar fortran-mode-syntax-table nil
  225.   "Syntax table in use in Fortran mode buffers.")
  226.  
  227. (defvar fortran-analyze-depth 100
  228.   "Number of lines to scan to determine whether to use fixed or tab format\
  229.  style.")
  230.  
  231. (defvar fortran-break-before-delimiters t
  232.   "Non-nil causes fortran-do-auto-fill breaks lines before delimeters.")
  233.  
  234. (if fortran-mode-syntax-table
  235.     ()
  236.   (setq fortran-mode-syntax-table (make-syntax-table))
  237.   (modify-syntax-entry ?\; "w" fortran-mode-syntax-table)
  238.   (modify-syntax-entry ?\r " " fortran-mode-syntax-table)
  239.   (modify-syntax-entry ?+ "." fortran-mode-syntax-table)
  240.   (modify-syntax-entry ?- "." fortran-mode-syntax-table)
  241.   (modify-syntax-entry ?= "." fortran-mode-syntax-table)
  242.   (modify-syntax-entry ?* "." fortran-mode-syntax-table)
  243.   (modify-syntax-entry ?/ "." fortran-mode-syntax-table)
  244.   (modify-syntax-entry ?\' "\"" fortran-mode-syntax-table)
  245.   (modify-syntax-entry ?\" "\"" fortran-mode-syntax-table)
  246.   (modify-syntax-entry ?\\ "/" fortran-mode-syntax-table)
  247.   (modify-syntax-entry ?. "w" fortran-mode-syntax-table)
  248.   (modify-syntax-entry ?_ "w" fortran-mode-syntax-table)
  249.   (modify-syntax-entry ?\n ">" fortran-mode-syntax-table))
  250.  
  251. (defvar fortran-mode-map () 
  252.   "Keymap used in fortran mode.")
  253. (if fortran-mode-map
  254.     ()
  255.   (setq fortran-mode-map (make-sparse-keymap))
  256.   (define-key fortran-mode-map ";" 'fortran-abbrev-start)
  257.   (define-key fortran-mode-map "\C-c;" 'fortran-comment-region)
  258.   (define-key fortran-mode-map "\e\C-a" 'beginning-of-fortran-subprogram)
  259.   (define-key fortran-mode-map "\e\C-e" 'end-of-fortran-subprogram)
  260.   (define-key fortran-mode-map "\e;" 'fortran-indent-comment)
  261.   (define-key fortran-mode-map "\e\C-h" 'mark-fortran-subprogram)
  262.   (define-key fortran-mode-map "\e\n" 'fortran-split-line)
  263.   (define-key fortran-mode-map "\n" 'fortran-reindent-then-newline-and-indent)
  264.   (define-key fortran-mode-map "\e\C-q" 'fortran-indent-subprogram)
  265.   (define-key fortran-mode-map "\C-c\C-w" 'fortran-window-create-momentarily)
  266.   (define-key fortran-mode-map "\C-c\C-r" 'fortran-column-ruler)
  267.   (define-key fortran-mode-map "\C-c\C-p" 'fortran-previous-statement)
  268.   (define-key fortran-mode-map "\C-c\C-n" 'fortran-next-statement)
  269.   (define-key fortran-mode-map "\t" 'fortran-indent-line)
  270.   (define-key fortran-mode-map "0" 'fortran-electric-line-number)
  271.   (define-key fortran-mode-map "1" 'fortran-electric-line-number)
  272.   (define-key fortran-mode-map "2" 'fortran-electric-line-number)
  273.   (define-key fortran-mode-map "3" 'fortran-electric-line-number)
  274.   (define-key fortran-mode-map "4" 'fortran-electric-line-number)
  275.   (define-key fortran-mode-map "5" 'fortran-electric-line-number)
  276.   (define-key fortran-mode-map "6" 'fortran-electric-line-number)
  277.   (define-key fortran-mode-map "7" 'fortran-electric-line-number)
  278.   (define-key fortran-mode-map "8" 'fortran-electric-line-number)
  279.   (define-key fortran-mode-map "9" 'fortran-electric-line-number))
  280.  
  281. (defvar fortran-mode-abbrev-table nil)
  282. (if fortran-mode-abbrev-table
  283.     ()
  284.   (let ((ac abbrevs-changed))
  285.     (define-abbrev-table 'fortran-mode-abbrev-table ())
  286.     (define-abbrev fortran-mode-abbrev-table  ";au"  "automatic" nil)
  287.     (define-abbrev fortran-mode-abbrev-table  ";b"   "byte" nil)
  288.     (define-abbrev fortran-mode-abbrev-table  ";bD"  "block data" nil)
  289.     (define-abbrev fortran-mode-abbrev-table  ";ch"  "character" nil)
  290.     (define-abbrev fortran-mode-abbrev-table  ";cl"  "close" nil)
  291.     (define-abbrev fortran-mode-abbrev-table  ";c"   "continue" nil)
  292.     (define-abbrev fortran-mode-abbrev-table  ";cm"  "common" nil)
  293.     (define-abbrev fortran-mode-abbrev-table  ";cx"  "complex" nil)
  294.     (define-abbrev fortran-mode-abbrev-table  ";df"  "define" nil)
  295.     (define-abbrev fortran-mode-abbrev-table  ";di"  "dimension" nil)
  296.     (define-abbrev fortran-mode-abbrev-table  ";do"  "double" nil)
  297.     (define-abbrev fortran-mode-abbrev-table  ";dc"  "double complex" nil)
  298.     (define-abbrev fortran-mode-abbrev-table  ";dp"  "double precision" nil)
  299.     (define-abbrev fortran-mode-abbrev-table  ";dw"  "do while" nil)
  300.     (define-abbrev fortran-mode-abbrev-table  ";e"   "else" nil)
  301.     (define-abbrev fortran-mode-abbrev-table  ";ed"  "enddo" nil)
  302.     (define-abbrev fortran-mode-abbrev-table  ";el"  "elseif" nil)
  303.     (define-abbrev fortran-mode-abbrev-table  ";en"  "endif" nil)
  304.     (define-abbrev fortran-mode-abbrev-table  ";eq"  "equivalence" nil)
  305.     (define-abbrev fortran-mode-abbrev-table  ";eW"  "endwhere" nil)
  306.     (define-abbrev fortran-mode-abbrev-table  ";ex"  "external" nil)
  307.     (define-abbrev fortran-mode-abbrev-table  ";ey"  "entry" nil)
  308.     (define-abbrev fortran-mode-abbrev-table  ";f"   "format" nil)
  309.     (define-abbrev fortran-mode-abbrev-table  ";fa"  ".false." nil)
  310.     (define-abbrev fortran-mode-abbrev-table  ";fu"  "function" nil)
  311.     (define-abbrev fortran-mode-abbrev-table  ";g"   "goto" nil)
  312.     (define-abbrev fortran-mode-abbrev-table  ";im"  "implicit" nil)
  313.     (define-abbrev fortran-mode-abbrev-table  ";ib"  "implicit byte" nil)
  314.     (define-abbrev fortran-mode-abbrev-table  ";ic"  "implicit complex" nil)
  315.     (define-abbrev fortran-mode-abbrev-table  ";ich" "implicit character" nil)
  316.     (define-abbrev fortran-mode-abbrev-table  ";ii"  "implicit integer" nil)
  317.     (define-abbrev fortran-mode-abbrev-table  ";il"  "implicit logical" nil)
  318.     (define-abbrev fortran-mode-abbrev-table  ";ir"  "implicit real" nil)
  319.     (define-abbrev fortran-mode-abbrev-table  ";inc" "include" nil)
  320.     (define-abbrev fortran-mode-abbrev-table  ";in"  "integer" nil)
  321.     (define-abbrev fortran-mode-abbrev-table  ";intr" "intrinsic" nil)
  322.     (define-abbrev fortran-mode-abbrev-table  ";l"   "logical" nil)
  323.     (define-abbrev fortran-mode-abbrev-table  ";n"   "namelist" nil)
  324.     (define-abbrev fortran-mode-abbrev-table  ";o"   "open" nil) ; was ;op
  325.     (define-abbrev fortran-mode-abbrev-table  ";pa"  "parameter" nil)
  326.     (define-abbrev fortran-mode-abbrev-table  ";pr"  "program" nil)
  327.     (define-abbrev fortran-mode-abbrev-table  ";ps"  "pause" nil)
  328.     (define-abbrev fortran-mode-abbrev-table  ";p"   "print" nil)
  329.     (define-abbrev fortran-mode-abbrev-table  ";rc"  "record" nil)
  330.     (define-abbrev fortran-mode-abbrev-table  ";re"  "real" nil)
  331.     (define-abbrev fortran-mode-abbrev-table  ";r"   "read" nil)
  332.     (define-abbrev fortran-mode-abbrev-table  ";rt"  "return" nil)
  333.     (define-abbrev fortran-mode-abbrev-table  ";rw"  "rewind" nil)
  334.     (define-abbrev fortran-mode-abbrev-table  ";s"   "stop" nil)
  335.     (define-abbrev fortran-mode-abbrev-table  ";sa"  "save" nil)
  336.     (define-abbrev fortran-mode-abbrev-table  ";st"  "structure" nil)
  337.     (define-abbrev fortran-mode-abbrev-table  ";sc"  "static" nil)
  338.     (define-abbrev fortran-mode-abbrev-table  ";su"  "subroutine" nil)
  339.     (define-abbrev fortran-mode-abbrev-table  ";tr"  ".true." nil)
  340.     (define-abbrev fortran-mode-abbrev-table  ";ty"  "type" nil)
  341.     (define-abbrev fortran-mode-abbrev-table  ";vo"  "volatile" nil)
  342.     (define-abbrev fortran-mode-abbrev-table  ";w"   "write" nil)
  343.     (define-abbrev fortran-mode-abbrev-table  ";wh"  "where" nil)
  344.     (setq abbrevs-changed ac)))
  345.  
  346. ;;;###autoload
  347. (defun fortran-mode ()
  348.   "Major mode for editing fortran code.
  349. Tab indents the current fortran line correctly. 
  350. `do' statements must not share a common `continue'.
  351.  
  352. Type `;?' or `;\\[help-command]' to display a list of built-in\
  353.  abbrevs for Fortran keywords.
  354.  
  355. Key definitions:
  356. \\{fortran-mode-map}
  357.  
  358. Variables controlling indentation style and extra features:
  359.  
  360.  comment-start
  361.     Normally nil in Fortran mode.  If you want to use comments
  362.     starting with `!', set this to the string \"!\".
  363.  fortran-do-indent
  364.     Extra indentation within do blocks.  (default 3)
  365.  fortran-if-indent
  366.     Extra indentation within if blocks.  (default 3)
  367.  fortran-structure-indent
  368.     Extra indentation within structure, union and map blocks.  (default 3)
  369.  fortran-continuation-indent
  370.     Extra indentation applied to continuation statements.  (default 5)
  371.  fortran-comment-line-column
  372.     Amount of extra indentation for text within full-line comments. (default 0)
  373.  fortran-comment-indent-style
  374.     nil    means don't change indentation of text in full-line comments,
  375.     fixed  means indent that text at fortran-comment-line-column beyond
  376.            the value of fortran-minimum-statement-indent,
  377.     relative  means indent at fortran-comment-line-column beyond the
  378.            indentation for a line of code.
  379.     (default 'fixed)
  380.  fortran-comment-indent-char
  381.     Single-character string be inserted instead of space for
  382.     full-line comment indentation.  (default \" \")
  383.  fortran-minimum-statement-indent
  384.     Minimum indentation for fortran statements. (default 6)
  385.  fortran-line-number-indent
  386.     Maximum indentation for line numbers.  A line number will get
  387.     less than this much indentation if necessary to avoid reaching
  388.     column 5.  (default 1)
  389.  fortran-check-all-num-for-matching-do
  390.     Non-nil causes all numbered lines to be treated as possible 'continue'
  391.     statements.  (default nil)
  392.  fortran-blink-matching-if 
  393.     From a fortran `endif' statement, blink the matching `if' statement.
  394.     (default nil)
  395.  fortran-continuation-string
  396.     Single-character string to be inserted in column 5 of a continuation
  397.     line.  (default \"$\")
  398.  fortran-comment-region
  399.     String inserted by \\[fortran-comment-region] at start of each line in 
  400.     region.  (default \"c$$$\")
  401.  fortran-electric-line-number
  402.     Non-nil causes line number digits to be moved to the correct column 
  403.     as typed.  (default t)
  404.  fortran-break-before-delimiters
  405.     Non-nil causes fortran-do-auto-fill breaks lines before delimeters.
  406.     (default t)
  407.  fortran-startup-message
  408.     Set to nil to inhibit message first time Fortran mode is used.
  409.  
  410. Turning on Fortran mode calls the value of the variable fortran-mode-hook 
  411. with no args, if that value is non-nil."
  412.   (interactive)
  413.   (kill-all-local-variables)
  414.   (if fortran-startup-message
  415.       (message "Emacs Fortran mode %s.  Bugs to %s"
  416.            fortran-mode-version bug-fortran-mode))
  417.   (setq fortran-startup-message nil)
  418.   (setq local-abbrev-table fortran-mode-abbrev-table)
  419.   (set-syntax-table fortran-mode-syntax-table)
  420.   (make-local-variable 'fortran-break-before-delimiters)
  421.   (setq fortran-break-before-delimiters t)
  422.   (make-local-variable 'indent-line-function)
  423.   (setq indent-line-function 'fortran-indent-line)
  424.   (make-local-variable 'comment-indent-hook)
  425.   (setq comment-indent-hook 'fortran-comment-hook)
  426.   (make-local-variable 'comment-line-start-skip)
  427.   (setq comment-line-start-skip
  428.     "^[Cc*]\\(\\([^ \t\n]\\)\\2\\2*\\)?[ \t]*\\|^#.*")
  429.   (make-local-variable 'comment-line-start)
  430.   (setq comment-line-start "c")
  431.   (make-local-variable 'comment-start-skip)
  432.   (setq comment-start-skip "![ \t]*")
  433.   (make-local-variable 'comment-start)
  434.   (setq comment-start nil)
  435.   (make-local-variable 'require-final-newline)
  436.   (setq require-final-newline t)
  437.   (make-local-variable 'abbrev-all-caps)
  438.   (setq abbrev-all-caps t)
  439.   (make-local-variable 'indent-tabs-mode)
  440.   (setq indent-tabs-mode nil)
  441.   (setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
  442.   (setq fill-column 72) ; Already local?
  443.   (use-local-map fortran-mode-map)
  444.   (setq mode-name "Fortran")
  445.   (setq major-mode 'fortran-mode)
  446.   (make-local-variable 'fortran-tab-mode)
  447.   (make-local-variable 'fortran-comment-line-column)
  448.   (make-local-variable 'fortran-minimum-statement-indent)
  449.   (make-local-variable 'fortran-column-ruler)
  450.   (make-local-variable 'fortran-tab-mode-string)
  451.   (fortran-tab-mode (fortran-analyze-file-format))
  452.   (run-hooks 'fortran-mode-hook))
  453.  
  454. (defun fortran-comment-hook ()
  455.   (save-excursion
  456.     (skip-chars-backward " \t")
  457.     (max (+ 1 (current-column))
  458.      comment-column)))
  459.  
  460. (defun fortran-indent-comment ()
  461.   "Align or create comment on current line.
  462. Existing comments of all types are recognized and aligned.
  463. If the line has no comment, a side-by-side comment is inserted and aligned
  464. if the value of  comment-start  is not nil.
  465. Otherwise, a separate-line comment is inserted, on this line
  466. or on a new line inserted before this line if this line is not blank."
  467.   (interactive)
  468.   (beginning-of-line)
  469.   ;; Recognize existing comments of either kind.
  470.   (cond ((looking-at comment-line-start-skip)
  471.      (fortran-indent-line))
  472.     ((find-comment-start-skip)      ; this catches any inline comment and
  473.                     ; leaves point after comment-start-skip
  474.      (if comment-start-skip
  475.          (progn (goto-char (match-beginning 0))
  476.             (if (not (= (current-column) (fortran-comment-hook)))
  477.             (progn (delete-horizontal-space)
  478.                    (indent-to (fortran-comment-hook)))))
  479.        (end-of-line)))        ; otherwise goto end of line or sth else?
  480.     ;; No existing comment.
  481.     ;; If side-by-side comments are defined, insert one,
  482.     ;; unless line is now blank.
  483.     ((and comment-start (not (looking-at "^[ \t]*$")))
  484.      (end-of-line)
  485.      (delete-horizontal-space)
  486.      (indent-to (fortran-comment-hook))
  487.      (insert comment-start))
  488.     ;; Else insert separate-line comment, making a new line if nec.
  489.     (t
  490.      (if (looking-at "^[ \t]*$")
  491.          (delete-horizontal-space)
  492.        (beginning-of-line)
  493.        (insert "\n")
  494.        (forward-char -1))
  495.      (insert comment-line-start)
  496.      (insert-char (if (stringp fortran-comment-indent-char)
  497.               (aref fortran-comment-indent-char 0)
  498.             fortran-comment-indent-char)
  499.               (- (calculate-fortran-indent) (current-column))))))
  500.  
  501. (defun fortran-comment-region (beg-region end-region arg)
  502.   "Comments every line in the region.
  503. Puts fortran-comment-region at the beginning of every line in the region. 
  504. BEG-REGION and END-REGION are args which specify the region boundaries. 
  505. With non-nil ARG, uncomments the region."
  506.   (interactive "*r\nP")
  507.   (let ((end-region-mark (make-marker)) (save-point (point-marker)))
  508.     (set-marker end-region-mark end-region)
  509.     (goto-char beg-region)
  510.     (beginning-of-line)
  511.     (if (not arg)            ;comment the region
  512.     (progn (insert fortran-comment-region)
  513.            (while (and  (= (forward-line 1) 0)
  514.                 (< (point) end-region-mark))
  515.          (insert fortran-comment-region)))
  516.       (let ((com (regexp-quote fortran-comment-region))) ;uncomment the region
  517.     (if (looking-at com)
  518.         (delete-region (point) (match-end 0)))
  519.     (while (and  (= (forward-line 1) 0)
  520.              (< (point) end-region-mark))
  521.       (if (looking-at com)
  522.           (delete-region (point) (match-end 0))))))
  523.     (goto-char save-point)
  524.     (set-marker end-region-mark nil)
  525.     (set-marker save-point nil)))
  526.  
  527. (defun fortran-abbrev-start ()
  528.   "Typing \";\\[help-command]\" or \";?\" lists all the fortran abbrevs. 
  529. Any other key combination is executed normally."
  530.   (interactive)
  531.   (let (c)
  532.     (insert last-command-char)
  533.     (if (or (= (setq c (read-char)) ??)    ;insert char if not equal to `?'
  534.         (= c help-char))
  535.     (fortran-abbrev-help)
  536.       (setq unread-command-char c))))
  537.  
  538. (defun fortran-abbrev-help ()
  539.   "List the currently defined abbrevs in Fortran mode."
  540.   (interactive)
  541.   (message "Listing abbrev table...")
  542.   (require 'abbrevlist)
  543.   (list-one-abbrev-table fortran-mode-abbrev-table "*Help*")
  544.   (message "Listing abbrev table...done"))
  545.  
  546. (defun fortran-column-ruler ()
  547.   "Inserts a column ruler momentarily above current line, till next keystroke.
  548. The ruler is defined by the value of fortran-column-ruler.
  549. The key typed is executed unless it is SPC."
  550.   (interactive)
  551.   (momentary-string-display 
  552.    fortran-column-ruler (save-excursion
  553.               (beginning-of-line) 
  554.               (if (eq (window-start (selected-window))
  555.                   (window-point (selected-window)))
  556.                   (progn (forward-line) (point))
  557.                 (point)))
  558.    nil "Type SPC or any command to erase ruler."))
  559.  
  560. (defun fortran-window-create ()
  561.   "Makes the window 72 columns wide.
  562. See also fortran-window-create-momentarily."
  563.   (interactive)
  564.   (condition-case error
  565.       (progn
  566.     (let ((window-min-width 2))
  567.       (if (< (window-width) (screen-width))
  568.           (enlarge-window-horizontally (- (screen-width)
  569.                           (window-width) 1)))
  570.       (split-window-horizontally 73)
  571.       (other-window 1)
  572.       (switch-to-buffer " fortran-window-extra" t)
  573.       (select-window (previous-window))))
  574.     (error (message "No room for fortran window.")
  575.        'error)))
  576.  
  577. (defun fortran-window-create-momentarily (&optional arg)
  578.   "Momentarily makes the window 72 columns wide.
  579. Optional ARG non-nil and non-unity disables the momentary feature.
  580. See also fortran-window-create."
  581.   (interactive "p")
  582.   (if (or (not arg)
  583.       (= arg 1))
  584.       (save-window-excursion
  585.     (if (not (equal (fortran-window-create) 'error))
  586.         (progn (message "Type SPC to continue editing.")
  587.            (let ((char (read-char)))
  588.              (or (equal char (string-to-char " "))
  589.              (setq unread-command-char char))))))
  590.     (fortran-window-create)))
  591.  
  592. (defun fortran-split-line ()
  593.   "Break line at point and insert continuation marker and alignment."
  594.   (interactive)
  595.   (delete-horizontal-space)
  596.   (if (save-excursion (beginning-of-line) (looking-at comment-line-start-skip))
  597.       (insert "\n" comment-line-start " ")
  598.     (if fortran-tab-mode
  599.     (progn 
  600.       (insert "\n\t")
  601.       (insert-char (fortran-numerical-continuation-char) 1))
  602.       (insert "\n " fortran-continuation-string)));Space after \n important
  603.   (fortran-indent-line))        ;when the cont string is C, c or *.
  604.  
  605. (defun fortran-numerical-continuation-char ()
  606.   "Return a digit for tab-digit style of continution lines.
  607. If, previous line is a tab-digit continuation line, returns that digit
  608. plus one.  Otherwise return 1.  Zero not allowed."
  609.   (save-excursion
  610.     (forward-line -1)
  611.     (if (looking-at "\t[1-9]")
  612.     (+ ?1 (% (- (char-after (+ (point) 1)) ?0) 9))
  613.       ?1)))
  614.  
  615. (defun delete-horizontal-regexp (chars)
  616.   "Delete all characters in CHARS around point.
  617. CHARS is like the inside of a [...] in a regular expression
  618. except that ] is never special and \ quotes ^, - or \."
  619.   (interactive "*s")
  620.   (skip-chars-backward chars)
  621.   (delete-region (point) (progn (skip-chars-forward chars) (point))))
  622.  
  623. (defun fortran-electric-line-number (arg)
  624.   "Self insert, but if part of a Fortran line number indent it automatically.
  625. Auto-indent does not happen if a numeric arg is used."
  626.   ;;The use of arg may be superfluous here since there apears to be no way to
  627.   ;;prefix a digit key with an argument.
  628.   (interactive "P")
  629.   (if (or arg (not fortran-electric-line-number))
  630.       (if arg 
  631.       (self-insert-command (prefix-numeric-value arg))
  632.     (self-insert-command 1))
  633.     (if (or (and (= 5 (current-column))
  634.          (save-excursion
  635.            (beginning-of-line)
  636.            (looking-at "     ")));In col 5 with only spaces to left.
  637.         (and (= fortran-minimum-statement-indent (current-column))
  638.          (save-excursion
  639.            (beginning-of-line)
  640.            (looking-at "\t"));In col 8 with a single tab to the left.
  641.          (not (or (eq last-command 'fortran-indent-line)
  642.               (eq last-command
  643.                   'fortran-reindent-then-newline-and-indent))))
  644.         (save-excursion
  645.           (re-search-backward "[^ \t0-9]"
  646.                   (save-excursion
  647.                     (beginning-of-line)
  648.                     (point))
  649.                   t)) ;not a line number
  650.         (looking-at "[0-9]")    ;within a line number
  651.         )
  652.     (self-insert-command (prefix-numeric-value arg))
  653.       (skip-chars-backward " \t")
  654.       (insert last-command-char)
  655.       (fortran-indent-line))))
  656.  
  657. (defun beginning-of-fortran-subprogram ()
  658.   "Moves point to the beginning of the current fortran subprogram."
  659.   (interactive)
  660.   (let ((case-fold-search t))
  661.     (beginning-of-line -1)
  662.     (re-search-backward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
  663.     (if (looking-at "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")
  664.     (forward-line 1))))
  665.  
  666. (defun end-of-fortran-subprogram ()
  667.   "Moves point to the end of the current fortran subprogram."
  668.   (interactive)
  669.   (let ((case-fold-search t))
  670.     (beginning-of-line 2)
  671.     (re-search-forward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
  672.     (goto-char (match-beginning 0))
  673.     (forward-line 1)))
  674.  
  675. (defun mark-fortran-subprogram ()
  676.   "Put mark at end of fortran subprogram, point at beginning. 
  677. The marks are pushed."
  678.   (interactive)
  679.   (end-of-fortran-subprogram)
  680.   (push-mark (point))
  681.   (beginning-of-fortran-subprogram))
  682.  
  683. (defun fortran-previous-statement ()
  684.   "Moves point to beginning of the previous fortran statement.
  685. Returns 'first-statement if that statement is the first
  686. non-comment Fortran statement in the file, and nil otherwise."
  687.   (interactive)
  688.   (let (not-first-statement continue-test)
  689.     (beginning-of-line)
  690.     (setq continue-test
  691.       (and
  692.        (not (looking-at comment-line-start-skip))
  693.        (or (looking-at
  694.             (concat "[ \t]*" (regexp-quote fortran-continuation-string)))
  695.            (or (looking-at "     [^ 0\n]")
  696.            (looking-at "\t[1-9]")))))
  697.     (while (and (setq not-first-statement (= (forward-line -1) 0))
  698.         (or (looking-at comment-line-start-skip)
  699.             (looking-at "[ \t]*$")
  700.             (looking-at "     [^ 0\n]")
  701.             (looking-at "\t[1-9]")
  702.             (looking-at (concat "[ \t]*"  comment-start-skip)))))
  703.     (cond ((and continue-test
  704.         (not not-first-statement))
  705.        (message "Incomplete continuation statement."))
  706.       (continue-test    
  707.        (fortran-previous-statement))
  708.       ((not not-first-statement)
  709.        'first-statement))))
  710.  
  711. (defun fortran-next-statement ()
  712.   "Moves point to beginning of the next fortran statement.
  713. Returns `last-statement' if that statement is the last
  714. non-comment Fortran statement in the file, and nil otherwise."
  715.   (interactive)
  716.   (let (not-last-statement)
  717.     (beginning-of-line)
  718.     (while (and (setq not-last-statement
  719.               (and (= (forward-line 1) 0)
  720.                (not (eobp))))
  721.          (or (looking-at comment-line-start-skip)
  722.              (looking-at "[ \t]*$")
  723.              (looking-at "     [^ 0\n]")
  724.              (looking-at "\t[1-9]")
  725.              (looking-at (concat "[ \t]*"  comment-start-skip)))))
  726.     (if (not not-last-statement)
  727.      'last-statement)))
  728.  
  729. (defun fortran-blink-matching-if ()
  730.   "From a fortran `endif' statement, blink the matching `if' statement."
  731.   (let ((count 1) (top-of-window (window-start)) matching-if
  732.     (endif-point (point)) message)
  733.     (if (save-excursion (beginning-of-line)
  734.             (skip-chars-forward " \t0-9")
  735.             (looking-at "end[ \t]*if\\b"))
  736.     (progn
  737.       (save-excursion
  738.         (while (and (not (= count 0))
  739.             (not (eq (fortran-previous-statement)
  740.                  'first-statement))
  741.             (not (looking-at
  742.                   "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")))
  743.                     ; Keep local to subprogram
  744.           (skip-chars-forward " \t0-9")
  745.           (cond ((looking-at "if[ \t]*(")
  746.              (save-excursion
  747.                (if (or
  748.                 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
  749.                 (let (then-test);multi-line if-then
  750.                   (while
  751.                   (and (= (forward-line 1) 0)
  752.                     ;search forward for then
  753.                        (or (looking-at "     [^ 0\n]")
  754.                        (looking-at "\t[1-9]"))
  755.                        (not
  756.                     (setq
  757.                      then-test
  758.                      (looking-at
  759.                       ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
  760.                   then-test))
  761.                (setq count (- count 1)))))
  762.             ((looking-at "end[ \t]*if\\b")
  763.              (setq count (+ count 1)))))
  764.         (if (not (= count 0))
  765.         (setq message "No matching if.")
  766.           (if (< (point) top-of-window)
  767.           (setq message (concat "Matches " (buffer-substring
  768.                             (progn (beginning-of-line)
  769.                                (point))
  770.                             (progn (end-of-line)
  771.                                (point)))))
  772.         (setq matching-if (point)))))
  773.       (if message
  774.           (message "%s" message)
  775.         (goto-char matching-if)
  776.         (sit-for 1)
  777.         (goto-char endif-point))))))
  778.  
  779. (defun fortran-indent-line ()
  780.   "Indents current fortran line based on its contents and on previous lines."
  781.   (interactive)
  782.   (let ((cfi (calculate-fortran-indent)))
  783.     (save-excursion
  784.       (beginning-of-line)
  785.       (if (or (not (= cfi (fortran-current-line-indentation)))
  786.           (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
  787.            (not (fortran-line-number-indented-correctly-p))))
  788.       (fortran-indent-to-column cfi)
  789.     (beginning-of-line)
  790.     (if (and (not (looking-at comment-line-start-skip))
  791.          (find-comment-start-skip))
  792.         (fortran-indent-comment))))
  793.     ;; Never leave point in left margin.
  794.     (if (< (current-column) cfi)
  795.     (move-to-column cfi))
  796.     (if (and auto-fill-hook
  797.          (> (save-excursion (end-of-line) (current-column)) fill-column))
  798.     (save-excursion
  799.       (end-of-line)
  800.       (fortran-do-auto-fill)))
  801.     (if fortran-blink-matching-if
  802.     (fortran-blink-matching-if))))
  803.  
  804. (defun fortran-reindent-then-newline-and-indent ()
  805.   "Reindent the current fortran line, insert a newline and indent the newline.
  806. An abbrev before point is expanded if abbrev-mode is non-nil."
  807.   (interactive)
  808.   (if abbrev-mode (expand-abbrev))
  809.   (save-excursion
  810.     (beginning-of-line)
  811.     (skip-chars-forward " \t")
  812.     (if (or (looking-at "[0-9]")    ;Reindent only where it is most
  813.         (looking-at "end")        ;likely to be necessary
  814.         (looking-at "else")
  815.         (looking-at (regexp-quote fortran-continuation-string)))
  816.     (fortran-indent-line)))
  817.   (newline)
  818.   (fortran-indent-line))
  819.  
  820. (defun fortran-indent-subprogram ()
  821.   "Properly indents the Fortran subprogram which contains point."
  822.   (interactive)
  823.   (save-excursion
  824.     (mark-fortran-subprogram)
  825.     (message "Indenting subprogram...")
  826.     (indent-region (point) (mark) nil))
  827.   (message "Indenting subprogram...done."))
  828.  
  829. (defun calculate-fortran-indent ()
  830.   "Calculates the fortran indent column based on previous lines."
  831.   (let (icol first-statement (case-fold-search t)
  832.          (fortran-minimum-statement-indent
  833.           fortran-minimum-statement-indent))
  834.     (save-excursion
  835.       (setq first-statement (fortran-previous-statement))
  836.       (if first-statement
  837.       (setq icol fortran-minimum-statement-indent)
  838.     (progn
  839.       (if (= (point) (point-min))
  840.           (setq icol fortran-minimum-statement-indent)
  841.         (setq icol (fortran-current-line-indentation)))
  842.       (skip-chars-forward " \t0-9")
  843.       (cond ((looking-at "if[ \t]*(")
  844.          (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
  845.              (let (then-test)    ;multi-line if-then
  846.                (while (and (= (forward-line 1) 0)
  847.                        ;;search forward for then
  848.                        (or (looking-at "     [^ 0\n]")
  849.                        (looking-at "\t[1-9]"))
  850.                        (not (setq then-test (looking-at
  851.                                  ".*then\\b[ \t]\
  852. *[^ \t_$(=a-z0-9]")))))
  853.                then-test))
  854.              (setq icol (+ icol fortran-if-indent))))
  855.         ((looking-at "\\(else\\|elseif\\)\\b")
  856.          (setq icol (+ icol fortran-if-indent)))
  857.         ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
  858.          (setq icol (+ icol fortran-if-indent)))
  859.         ((looking-at "where.*(.*)[ \t]*\n")
  860.          (setq icol (+ icol fortran-if-indent)))
  861.         ((looking-at "do\\b")
  862.          (setq icol (+ icol fortran-do-indent)))
  863.         ((looking-at
  864.           "\\(structure\\|union\\|map\\)\\b[ \t]*[^ \t=(a-z]")
  865.          (setq icol (+ icol fortran-structure-indent)))
  866.         ((looking-at "end\\b[ \t]*[^ \t=(a-z]")
  867.          ;; Previous END resets indent to minimum
  868.          (setq icol fortran-minimum-statement-indent))))))
  869.     (save-excursion
  870.       (beginning-of-line)
  871.       (cond ((looking-at "[ \t]*$"))
  872.         ((looking-at comment-line-start-skip)
  873.          (cond ((eq fortran-comment-indent-style 'relative)
  874.             (setq icol (+ icol fortran-comment-line-column)))
  875.            ((eq fortran-comment-indent-style 'fixed)
  876.             (setq icol (+ fortran-minimum-statement-indent
  877.                   fortran-comment-line-column))))
  878.          (setq fortran-minimum-statement-indent 0))
  879.         ((or (looking-at (concat "[ \t]*"
  880.                      (regexp-quote
  881.                       fortran-continuation-string)))
  882.          (looking-at "     [^ 0\n]")
  883.          (looking-at "\t[1-9]"))
  884.          (setq icol (+ icol fortran-continuation-indent)))
  885.         (first-statement)
  886.         ((and fortran-check-all-num-for-matching-do
  887.           (looking-at "[ \t]*[0-9]+")
  888.           (fortran-check-for-matching-do))
  889.          (setq icol (- icol fortran-do-indent)))
  890.         (t
  891.          (skip-chars-forward " \t0-9")
  892.          (cond ((looking-at "end[ \t]*if\\b")
  893.             (setq icol (- icol fortran-if-indent)))
  894.            ((looking-at "\\(else\\|elseif\\)\\b")
  895.             (setq icol (- icol fortran-if-indent)))
  896.            ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
  897.             (setq icol (- icol fortran-if-indent)))
  898.            ((looking-at "end[ \t]*where\\b")
  899.             (setq icol (- icol fortran-if-indent)))
  900.            ((and (looking-at "continue\\b")
  901.              (fortran-check-for-matching-do))
  902.             (setq icol (- icol fortran-do-indent)))
  903.            ((looking-at "end[ \t]*do\\b")
  904.             (setq icol (- icol fortran-do-indent)))
  905.            ((looking-at
  906.              "end[ \t]*\
  907. \\(structure\\|union\\|map\\)\\b[ \t]*[^ \t=(a-z]")
  908.             (setq icol (- icol fortran-structure-indent)))
  909.            ((and (looking-at "end\\b[ \t]*[^ \t=(a-z]")
  910.              (not (= icol fortran-minimum-statement-indent)))
  911.              (message "Warning: `end' not in column %d.  Probably\
  912.  an unclosed block." fortran-minimum-statement-indent))))))
  913.     (max fortran-minimum-statement-indent icol)))
  914.  
  915. (defun fortran-current-line-indentation ()
  916.   "Indentation of current line, ignoring Fortran line number or continuation.
  917. This is the column position of the first non-whitespace character
  918. aside from the line number and/or column 5/8 line-continuation character.
  919. For comment lines, returns indentation of the first
  920. non-indentation text within the comment."
  921.   (save-excursion
  922.     (beginning-of-line)
  923.     (cond ((looking-at comment-line-start-skip)
  924.        (goto-char (match-end 0))
  925.        (skip-chars-forward
  926.         (if (stringp fortran-comment-indent-char)
  927.         fortran-comment-indent-char
  928.           (char-to-string fortran-comment-indent-char))))
  929.       ((or (looking-at "     [^ 0\n]")
  930.            (looking-at "\t[1-9]"))
  931.        (goto-char (match-end 0)))
  932.       (t
  933.        ;; Move past line number.
  934.        (skip-chars-forward "[ \t0-9]");From Uli
  935.        ))
  936.     ;; Move past whitespace.
  937.     (skip-chars-forward " \t")
  938.     (current-column)))
  939.  
  940. (defun fortran-indent-to-column (col)
  941.   "Indents current line with spaces to column COL.
  942. notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
  943.           line, and this continuation character is retained on indentation;
  944.        2) If fortran-continuation-string is the first non-whitespace character,
  945.           this is a continuation line;
  946.        3) A non-continuation line which has a number as the first
  947.           non-whitespace character is a numbered line.
  948.        4) A tab followed by a digit indicates a continuation line."
  949.   (save-excursion
  950.     (beginning-of-line)
  951.     (if (looking-at comment-line-start-skip)
  952.     (if fortran-comment-indent-style
  953.         (let ((char (if (stringp fortran-comment-indent-char)
  954.                 (aref fortran-comment-indent-char 0)
  955.               fortran-comment-indent-char)))
  956.           (goto-char (match-end 0))
  957.           (delete-horizontal-regexp (concat " \t" (char-to-string char)))
  958.           (insert-char char (- col (current-column)))))
  959.       (if (looking-at "\t[1-9]")
  960.       (if fortran-tab-mode
  961.           (goto-char (match-end 0))
  962.         (delete-char 2)
  963.         (insert "     ")
  964.         (insert fortran-continuation-string))
  965.     (if (looking-at "     [^ 0\n]")
  966.         (if fortran-tab-mode
  967.         (progn (delete-char 6)
  968.                (insert "\t")
  969.                (insert-char (fortran-numerical-continuation-char) 1))
  970.           (forward-char 6))
  971.       (delete-horizontal-space)
  972.       ;; Put line number in columns 0-4
  973.       ;; or put continuation character in column 5.
  974.       (cond ((eobp))
  975.         ((looking-at (regexp-quote fortran-continuation-string))
  976.          (if fortran-tab-mode
  977.              (progn
  978.                (indent-to fortran-minimum-statement-indent)
  979.                (delete-char 1)
  980.                (insert-char (fortran-numerical-continuation-char) 1))
  981.            (indent-to 5))
  982.          (forward-char 1))
  983.         ((looking-at "[0-9]+")
  984.          (let ((extra-space (- 5 (- (match-end 0) (point)))))
  985.            (if (< extra-space 0)
  986.                (message "Warning: line number exceeds 5-digit limit.")
  987.              (indent-to (min fortran-line-number-indent extra-space))))
  988.          (skip-chars-forward "0-9")))))
  989.       ;; Point is now after any continuation character or line number.
  990.       ;; Put body of statement where specified.
  991.       (delete-horizontal-space)
  992.       (indent-to col)
  993.       ;; Indent any comment following code on the same line.
  994.       (if (and comment-start-skip
  995.            (find-comment-start-skip))
  996.       (progn (goto-char (match-beginning 0))
  997.          (if (not (= (current-column) (fortran-comment-hook)))
  998.              (progn (delete-horizontal-space)
  999.                 (indent-to (fortran-comment-hook)))))))))
  1000.  
  1001. (defun fortran-line-number-indented-correctly-p ()
  1002.   "Return t if current line's line number is correctly indented.
  1003. Do not call if there is no line number."
  1004.   (save-excursion
  1005.     (beginning-of-line)
  1006.     (skip-chars-forward " \t")
  1007.     (and (<= (current-column) fortran-line-number-indent)
  1008.      (or (= (current-column) fortran-line-number-indent)
  1009.          (progn (skip-chars-forward "0-9")
  1010.             (= (current-column) 5))))))
  1011.  
  1012. (defun fortran-check-for-matching-do ()
  1013.   "When called from a numbered statement, returns t if matching 'do' is found.
  1014. Otherwise return a nil."
  1015.   (let (charnum
  1016.     (case-fold-search t))
  1017.     (save-excursion
  1018.       (beginning-of-line)
  1019.       (if (looking-at "[ \t]*[0-9]+")
  1020.       (progn
  1021.         (skip-chars-forward " \t")
  1022.         (skip-chars-forward "0") ;skip past leading zeros
  1023.         (setq charnum (buffer-substring (point)
  1024.                         (progn (skip-chars-forward "0-9")
  1025.                            (point))))
  1026.         (beginning-of-line)
  1027.         (and (re-search-backward
  1028.           (concat
  1029.            "\\(^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]\\)\\|\\(^[ \t0-9]*do\
  1030. [ \t]*0*"
  1031.            charnum "\\b\\)\\|\\(^[ \t]*0*" charnum "\\b\\)")
  1032.           nil t)
  1033.          (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum))))))))
  1034.  
  1035. (defun fortran-analyze-file-format ()
  1036.   "Return 0 if Fixed format is used, 1 if Tab formatting is used.
  1037. Use fortran-tab-mode-default if no non-comment statements are found in the
  1038. file before the end or the first fortran-analyze-depth lines."
  1039.   (save-excursion
  1040.     (goto-char (point-min))
  1041.     (setq i 0)
  1042.     (while (not (or
  1043.          (eobp)
  1044.          (looking-at "\t")
  1045.          (looking-at "      ")
  1046.          (> i fortran-analyze-depth)))
  1047.       (forward-line)
  1048.       (setq i (1+ i)))
  1049.     (cond
  1050.      ((looking-at "\t") 1)
  1051.      ((looking-at "      ") 0)
  1052.      (fortran-tab-mode-default 1)
  1053.      (t 0))))
  1054.  
  1055. (defun fortran-tab-mode (arg)
  1056.   "Toggle fortran-tab-mode which indicates style of continuation lines.
  1057. With no argument, toggle on/off the tabbing mode of continuation lines.
  1058. If argument is a positive number, or non-nil if not a number, fortran-tab-mode
  1059. is turned on.  Otherwise 
  1060. If `fortran-tab-mode' is false"
  1061.   (interactive "P")
  1062.   (setq fortran-tab-mode
  1063.     (if (null arg) (not fortran-tab-mode)
  1064.       (if (numberp arg)
  1065.           (> (prefix-numeric-value arg) 0)
  1066.         (arg))))
  1067.   (if fortran-tab-mode
  1068.       (fortran-setup-tab-format-style)
  1069.     (fortran-setup-fixed-format-style))
  1070.   (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
  1071.  
  1072. (defun fortran-setup-tab-format-style ()
  1073.   "Set up fortran mode to use the TAB-digit mode of continuation lines.
  1074. Use the command \\[fortran-tab-mode] to toggle between this and fixed
  1075. format style."
  1076.   (setq fortran-minimum-statement-indent (max tab-width 6))
  1077.   (setq indent-tabs-mode t)
  1078.   (setq fortran-column-ruler
  1079.     (concat
  1080.      "0       810        20        30        40        5\
  1081. 0        60        70\n"
  1082.      "[   ]|  { |    |    |    |    |    |    |    |    \
  1083. |    |    |    |    |}\n"))
  1084.   (setq fortran-tab-mode-string " TAB-format")
  1085.   (set-buffer-modified-p (buffer-modified-p)))
  1086.  
  1087. (defun fortran-setup-fixed-format-style ()
  1088.   "Set up fortran mode to use the column 6 mode of continuation lines.
  1089. Use the command \\[fortran-tab-mode] to toggle between this and tab
  1090. character format style."
  1091.   (setq fortran-minimum-statement-indent 6)
  1092.   (setq indent-tabs-mode nil)
  1093.   (setq fortran-column-ruler
  1094.     (concat
  1095.      "0   4 6  10        20        30        40        5\
  1096. 0        60        70\n"
  1097.      "[   ]|{   |    |    |    |    |    |    |    |    \
  1098. |    |    |    |    |}\n"))
  1099.   (setq fortran-tab-mode-string " Fixed-format")
  1100.   (set-buffer-modified-p (buffer-modified-p)))
  1101.  
  1102. (or (assq 'fortran-tab-mode-string minor-mode-alist)
  1103.     (setq minor-mode-alist (cons
  1104.                 '(fortran-tab-mode-string fortran-tab-mode-string)
  1105.                 minor-mode-alist)))
  1106.  
  1107. (defun find-comment-start-skip ()
  1108.   "Move to past `comment-start-skip' found on current line.
  1109. Return t if `comment-start-skip' found, nil if not."
  1110. ;;; In order to move point only if comment-start-skip is found,
  1111. ;;; this one uses a lot of save-excursions.  Note that re-search-forward
  1112. ;;; moves point even if comment-start-skip is inside a string-constant.
  1113. ;;; Some code expects certain values for match-beginning and end
  1114.   (interactive)
  1115.   (let ((save-match-beginning) (save-match-end))
  1116.     (if (save-excursion 
  1117.       (re-search-forward comment-start-skip
  1118.                  (save-excursion (end-of-line) (point)) t))
  1119.     (progn
  1120.       (setq save-match-beginning (match-beginning 0))
  1121.       (setq save-match-end (match-end 0))
  1122.       (if (is-in-fortran-string-p (match-beginning 0))
  1123.           (progn
  1124.         (save-excursion
  1125.           (goto-char save-match-end)
  1126.           (find-comment-start-skip))    ; recurse for rest of line
  1127.         )
  1128.         (goto-char save-match-beginning)
  1129.         (re-search-forward comment-start-skip
  1130.                    (save-excursion (end-of-line) (point)) t)
  1131.         (goto-char (match-end 0))
  1132.         t))
  1133.       nil)))
  1134.  
  1135. (defun is-in-fortran-string-p (pos)
  1136.   "t if POS (a buffer position) is inside a standard Fortran string.
  1137. Fortran strings are delimeted by apostrophes (\').  Quote-Escape-sequences
  1138. (\\'), strings delimited by \" and detection of syntax-errors
  1139. (unbalanced quotes) are NOT supported."
  1140. ;;; The algorithm is simple: start at point with value nil
  1141. ;;; and toggle value at each quote found until end of line.
  1142. ;;; The quote skip is hard-coded, maybe it's possible to change this
  1143. ;;; and use something like 'string-constant-delimiter' (which
  1144. ;;; doesn't exist yet) so this function can be used by other modes,
  1145. ;;; but then one must pay attention to escape sequences, multi-line-constants
  1146. ;;; and such things.
  1147.   (let ((is-in-fortran-string nil))
  1148.     (save-excursion   
  1149.       (goto-char pos)
  1150.       (fortran-previous-statement)
  1151.       (fortran-next-statement)
  1152.       (while (< (point) pos)
  1153.     ;; Make sure we don't count quotes in continuation column.
  1154.     (if (looking-at "^     ")
  1155.         (goto-char (+ 1 (match-end 0)))
  1156.       (if (and (not is-in-fortran-string)
  1157.            (looking-at comment-start-skip))
  1158.           (beginning-of-line 2)
  1159.         (if (looking-at "'")
  1160.         (setq is-in-fortran-string (not is-in-fortran-string)))
  1161.         (forward-char 1)))))
  1162.     is-in-fortran-string))
  1163.  
  1164. (defun fortran-auto-fill-mode (arg)
  1165.   "Toggle fortran-auto-fill mode.
  1166. With arg, turn fortran-auto-fill mode on iff arg is positive.
  1167. In fortran-auto-fill mode, inserting a space at a column beyond  fill-column
  1168. automatically breaks the line at a previous space."
  1169.   (interactive "P")
  1170.   (prog1 (setq auto-fill-hook
  1171.            (if (if (null arg)
  1172.                (not auto-fill-hook)
  1173.              (> (prefix-numeric-value arg) 0))
  1174.            'fortran-indent-line
  1175.          nil))
  1176.     ;; update mode-line
  1177.     (set-buffer-modified-p (buffer-modified-p))))
  1178.  
  1179. (defun fortran-do-auto-fill ()
  1180.   (interactive)
  1181.   (let* ((opoint (point))
  1182.      (bol (save-excursion (beginning-of-line) (point)))
  1183.      (eol (save-excursion (end-of-line) (point)))
  1184.      (bos (min eol (+ bol (fortran-current-line-indentation))))
  1185.      (quote
  1186.       (save-excursion
  1187.         (goto-char bol)
  1188.         (if (looking-at comment-line-start-skip)
  1189.         nil            ; OK to break quotes on comment lines.
  1190.           (move-to-column fill-column)
  1191.           (cond ((is-in-fortran-string-p (point))
  1192.              (save-excursion (re-search-backward "[^']'[^']" bol t)
  1193.                      (if fortran-break-before-delimiters
  1194.                      (point)
  1195.                        (1+ (point)))))
  1196.             (t nil)))))
  1197.      ;;
  1198.      ;; decide where to split the line. If a position for a quoted
  1199.      ;; string was found above then use that, else break the line
  1200.      ;; before the last delimiter.
  1201.      ;; Delimeters are whitespace, commas, and operators.
  1202.      ;; Will break before a pair of *'s.
  1203.      ;;
  1204.      (fill-point
  1205.       (or quote
  1206.           (save-excursion
  1207.         (move-to-column (1+ fill-column))
  1208.         (skip-chars-backward "^ \t\n,'+-/*)=")
  1209.         (if (<= (point) (1+ bos))
  1210.             (progn
  1211.               (move-to-column (1+ fill-column))
  1212.               (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t))
  1213.               (goto-char bol))))
  1214.         (if (bolp)
  1215.             (re-search-forward "[ \t]" opoint t)
  1216.           (forward-char -1)
  1217.           (if (looking-at "'")
  1218.               (forward-char 1)
  1219.             (skip-chars-backward " \t\*")))
  1220.         (if fortran-break-before-delimiters
  1221.             (point)
  1222.           (1+ (point))))))
  1223.      )
  1224.     ;; if we are in an in-line comment, don't break unless the
  1225.     ;; line of code is longer than it should be. Otherwise
  1226.     ;; break the line at the column computed above.
  1227.     ;;
  1228.     ;; Need to use find-comment-start-skip to make sure that quoted !'s
  1229.     ;; don't prevent a break.
  1230.     (if (not (or (save-excursion
  1231.            (if (and (re-search-backward comment-start-skip bol t)
  1232.                 (not (is-in-fortran-string-p (point))))
  1233.                (progn
  1234.              (skip-chars-backward " \t")
  1235.              (< (current-column) (1+ fill-column)))))
  1236.          (save-excursion
  1237.            (goto-char fill-point)
  1238.            (bolp))))
  1239.     (if (> (save-excursion
  1240.          (goto-char fill-point) (current-column))
  1241.            (1+ fill-column))
  1242.         (progn (goto-char fill-point)
  1243.            (fortran-break-line))
  1244.       (save-excursion
  1245.         (if (> (save-excursion
  1246.              (goto-char fill-point) 
  1247.              (current-column))
  1248.            (+ (calculate-fortran-indent) fortran-continuation-indent))
  1249.         (progn
  1250.           (goto-char fill-point)
  1251.           (fortran-break-line))))))
  1252.     ))
  1253. (defun fortran-break-line ()
  1254.   (let ((opoint (point))
  1255.     (bol (save-excursion (beginning-of-line) (point)))
  1256.     (eol (save-excursion (end-of-line) (point)))
  1257.     (comment-string nil))
  1258.     
  1259.     (save-excursion
  1260.       (if (and comment-start-skip (find-comment-start-skip))
  1261.       (progn
  1262.         (re-search-backward comment-start-skip bol t)
  1263.         (setq comment-string (buffer-substring (point) eol))
  1264.         (delete-region (point) eol))))
  1265. ;;; Forward line 1 really needs to go to next non white line
  1266.     (if (save-excursion (forward-line 1)
  1267.             (or (looking-at "     [^ 0\n]")
  1268.                 (looking-at "\t[1-9]")))
  1269.     (progn
  1270.       (forward-line 1)
  1271.       (delete-indentation)
  1272.       (delete-char 2)
  1273.       (delete-horizontal-space)
  1274.       (fortran-do-auto-fill))
  1275.       (fortran-split-line))
  1276.     (if comment-string
  1277.     (save-excursion
  1278.       (goto-char bol)
  1279.       (end-of-line)
  1280.       (delete-horizontal-space)
  1281.       (indent-to (fortran-comment-hook))
  1282.       (insert comment-string)))))
  1283.  
  1284. (provide 'fortran)
  1285.  
  1286. ;;; fortran.el ends here
  1287.