+(defvar fortran-imenu-generic-expression
+ ;; These patterns could be confused by sequence nos. in cols 72+ and
+ ;; don't allow continuations everywhere.
+ (list
+ (list
+ nil
+ ;; [This will be fooled by `end function' allowed by G77. Also,
+ ;; it assumes sensible whitespace is employed.]
+ (concat
+ ;; leading whitespace:
+ "^\\s-+\\("
+ ;; function declaration with optional type, e.g. `real',
+ ;; `real*4', character(*), `double precision':
+ "\\(\\sw\\|\\s-\\|[*()+]\\)*"
+ "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
+ ;; Possible statement continuation:
+ "[ \t" fortran-continuation-string "]+"
+ ;; Variable to index:
+ "\\(\\sw+\\)")
+ 3)
+ ;; Un-named block data.
+ '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
+ "Value for `imenu-generic-expression' in Fortran mode.")
+
+\f
+;; Hideshow support.
+(defconst fortran-blocks-re
+ (concat "block[ \t]*data\\|select[ \t]*case\\|"
+ (regexp-opt '("do" "if" "interface" "function" "map" "program"
+ "structure" "subroutine" "union" "where")))
+ "Regexp potentially indicating the start or end of a Fortran \"block\".
+Omits naked END statements, and DO-loops closed by anything other
+than ENDDO.")
+
+(defconst fortran-end-block-re
+ ;; Do-loops terminated by things other than ENDDO cannot be handled
+ ;; with a regexp. This omission does not seem to matter to hideshow...
+ (concat "^[ \t0-9]*\\<end[ \t]*\\("
+ fortran-blocks-re
+ ;; Naked END statement.
+ "\\|!\\|$\\)")
+ "Regexp matching the end of a Fortran \"block\", from the line start.
+Note that only ENDDO is handled for the end of a DO-loop. Used
+in the Fortran entry in `hs-special-modes-alist'.")
+
+(defconst fortran-start-block-re
+ (concat
+ "^[ \t0-9]*\\(" ; statement number
+ ;; Structure label for DO, IF, SELECT, WHERE.
+ "\\(\\(\\sw+[ \t]*:[ \t]*\\)?"
+ ;; IF blocks are a nuisance:
+ ;; IF ( ... ) foo is not a block, but a single statement.
+ ;; IF ( ... ) THEN can be split over multiple lines.
+ ;; [So can, eg, a DO WHILE (... ), but that is less common, I hope.]
+ ;; The regexp below allows for it to be split over at most 2 lines.
+ ;; That leads to the problem of not matching two consecutive IF
+ ;; statements as one, eg:
+ ;; IF ( ... ) foo
+ ;; IF ( ... ) THEN
+ ;; It simply is not possible to do this in a 100% correct fashion
+ ;; using a regexp - see the functions fortran-end-if,
+ ;; fortran-beginning-if for the hoops we have to go through.
+ ;; An alternative is to match on THEN at a line end, eg:
+ ;; ".*)[ \t]*then[ \t]*\\($\\|!\\)"
+ ;; This would also match ELSE branches, though. This does not seem
+ ;; right to me, because then one has neighbouring blocks that are
+ ;; not nested in each other.
+ "\\(if[ \t]*(\\(.*\\|"
+ ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
+ "do\\|select[ \t]*case\\|where\\)\\)\\|"
+ (regexp-opt '("interface" "function" "map" "program"
+ "structure" "subroutine" "union"))
+ "\\|block[ \t]*data\\)[ \t]*")
+ "Regexp matching the start of a Fortran \"block\", from the line start.
+A simple regexp cannot do this in fully correct fashion, so this
+tries to strike a compromise between complexity and flexibility.
+Used in the Fortran entry in `hs-special-modes-alist'.")
+
+(add-to-list 'hs-special-modes-alist
+ `(fortran-mode ,fortran-start-block-re ,fortran-end-block-re
+ "^[cC*!]" fortran-end-of-block nil))
+
+\f
+(defvar fortran-mode-syntax-table
+ (let ((table (make-syntax-table)))
+ ;; We might like `;' to be punctuation (g77 multi-statement
+ ;; lines), but that screws abbrevs.
+ (modify-syntax-entry ?\; "w" table)
+ (modify-syntax-entry ?\r " " table)
+ (modify-syntax-entry ?+ "." table)
+ (modify-syntax-entry ?- "." table)
+ (modify-syntax-entry ?= "." table)
+ (modify-syntax-entry ?* "." table)
+ (modify-syntax-entry ?/ "." table)
+ (modify-syntax-entry ?\' "\"" table)
+ (modify-syntax-entry ?\" "\"" table)
+ ;; Consistent with GNU Fortran's default -- see the manual.
+ ;; The F77 standard imposes no rule on this issue.
+ (modify-syntax-entry ?\\ "\\" table)
+ ;; This might be better as punctuation, as for C, but this way you
+ ;; can treat floating-point numbers as symbols.
+ (modify-syntax-entry ?. "_" table) ; e.g. `a.ne.b'
+ (modify-syntax-entry ?_ "_" table)
+ (modify-syntax-entry ?$ "_" table) ; esp. VMSisms
+ (modify-syntax-entry ?\! "<" table)
+ (modify-syntax-entry ?\n ">" table)
+ table)
+ "Syntax table used in Fortran mode.")
+
+(defvar fortran-gud-syntax-table
+ (let ((st (make-syntax-table fortran-mode-syntax-table)))
+ (modify-syntax-entry ?\n "." st)
+ st)
+ "Syntax table used to parse Fortran expressions for printing in GUD.")
+
+(defvar fortran-mode-map
+ (let ((map (make-sparse-keymap)))
+ (define-key map ";" 'fortran-abbrev-start)
+ (define-key map "\C-c;" 'fortran-comment-region)
+ (define-key map "\M-;" 'fortran-indent-comment)
+ (define-key map "\M-\n" 'fortran-split-line)
+ (define-key map "\M-\C-n" 'fortran-end-of-block)
+ (define-key map "\M-\C-p" 'fortran-beginning-of-block)
+ (define-key map "\M-\C-q" 'fortran-indent-subprogram)
+ (define-key map "\C-c\C-w" 'fortran-window-create-momentarily)
+ (define-key map "\C-c\C-r" 'fortran-column-ruler)
+ (define-key map "\C-c\C-p" 'fortran-previous-statement)
+ (define-key map "\C-c\C-n" 'fortran-next-statement)
+ (define-key map "\C-c\C-d" 'fortran-join-line) ; like f90
+ (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation
+ (define-key map "0" 'fortran-electric-line-number)
+ (define-key map "1" 'fortran-electric-line-number)
+ (define-key map "2" 'fortran-electric-line-number)
+ (define-key map "3" 'fortran-electric-line-number)
+ (define-key map "4" 'fortran-electric-line-number)
+ (define-key map "5" 'fortran-electric-line-number)
+ (define-key map "6" 'fortran-electric-line-number)
+ (define-key map "7" 'fortran-electric-line-number)
+ (define-key map "8" 'fortran-electric-line-number)
+ (define-key map "9" 'fortran-electric-line-number)
+
+ (easy-menu-define fortran-menu map "Menu for Fortran mode."
+ `("Fortran"
+ ["Manual" (info "(emacs)Fortran")]
+ ("Customization"
+ ,(custom-menu-create 'fortran)
+ ["Set" Custom-set t]
+ ["Save" Custom-save t]
+ ["Reset to Current" Custom-reset-current t]
+ ["Reset to Saved" Custom-reset-saved t]
+ ["Reset to Standard Settings" Custom-reset-standard t]
+ )
+ "--"
+ ["Comment Region" fortran-comment-region mark-active]
+ ["Uncomment Region"
+ (fortran-comment-region (region-beginning) (region-end) 1)
+ mark-active]
+ ["Indent Region" indent-region mark-active]
+ ["Indent Subprogram" fortran-indent-subprogram t]
+ "--"
+ ["Beginning of Subprogram" fortran-beginning-of-subprogram t]
+ ["End of Subprogram" fortran-end-of-subprogram t]
+ ("Mark"
+ ["Subprogram" mark-defun t]
+ ["IF Block" fortran-mark-if t]
+ ["DO Block" fortran-mark-do t]
+ )
+ ["Narrow to Subprogram" narrow-to-defun t]
+ ["Widen" widen t]
+ "--"
+ ["Temporary column ruler" fortran-column-ruler t]
+ ["72-column window" fortran-window-create t]
+ ["Full Width Window"
+ (enlarge-window-horizontally (- (frame-width) (window-width)))
+ (< (window-width) (frame-width))]
+ ["Momentary 72-column window" fortran-window-create-momentarily t]
+ "--"
+ ["Break Line at Point" fortran-split-line t]
+ ["Join Line" fortran-join-line t]
+ ["Fill Statement/Comment" fill-paragraph t]
+ "--"
+ ["Toggle auto-fill" auto-fill-mode :selected auto-fill-function
+ :style toggle]
+ ["Toggle abbrev-mode" abbrev-mode :selected abbrev-mode
+ :style toggle]
+ ["Add imenu Menu" imenu-add-menubar-index
+ :active (not (lookup-key (current-local-map) [menu-bar index]))
+ :included (fboundp 'imenu-add-to-menubar)]))
+ map)