1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;; Copyright (c) 1986, 93, 94, 95, 97, 98, 99, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Michael D. Prange <prange@erl.mit.edu>
7 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
10 ;; This file is part of GNU Emacs.
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)
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.
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 the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This mode is documented in the Emacs manual.
31 ;; Note that it is for editing Fortran77 or Fortran90 fixed source
32 ;; form. For editing Fortran 90 free format source, use `f90-mode'
33 ;; (f90.el). It is meant to support the GNU Fortran language
34 ;; implemented by g77 (its extensions to Fortran77 and
35 ;; interpretations, e.g. of blackslash in strings).
39 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
41 ;; We acknowledge many contributions and valuable suggestions by
42 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
43 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
44 ;; Gary Sabot and Richard Stallman.
50 ;; * Tidy it all up (more)!
51 ;; * Implement insertion and removal of statement continuations in
52 ;; mixed f77/f90 style, with the first `&' past column 72 and the
53 ;; second in column 6.
54 ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
57 "Fortran mode for Emacs"
58 :link
'(custom-manual "(emacs)Fortran")
61 (defgroup fortran-indent nil
62 "Indentation variables in Fortran mode"
66 (defgroup fortran-comment nil
67 "Comment-handling variables in Fortran mode"
73 (defcustom fortran-tab-mode-default nil
74 "*Default tabbing/carriage control style for empty files in Fortran mode.
75 A value of t specifies tab-digit style of continuation control.
76 A value of nil specifies that continuation lines are marked
77 with a character in column 6."
79 :group
'fortran-indent
)
81 ;; Buffer local, used to display mode line.
82 (defcustom fortran-tab-mode-string nil
83 "String to appear in mode line when TAB format mode is on."
84 :type
'(choice (const nil
) string
)
85 :group
'fortran-indent
)
86 (make-variable-buffer-local 'fortran-tab-mode-string
)
88 (defcustom fortran-do-indent
3
89 "*Extra indentation applied to DO blocks."
91 :group
'fortran-indent
)
93 (defcustom fortran-if-indent
3
94 "*Extra indentation applied to IF blocks."
96 :group
'fortran-indent
)
98 (defcustom fortran-structure-indent
3
99 "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
101 :group
'fortran-indent
)
103 (defcustom fortran-continuation-indent
5
104 "*Extra indentation applied to Fortran continuation lines."
106 :group
'fortran-indent
)
108 (defcustom fortran-comment-indent-style
'fixed
109 "*How to indent comments.
110 nil forces comment lines not to be touched,
111 `fixed' makes fixed comment indentation to `fortran-comment-line-extra-indent'
112 columns beyond `fortran-minimum-statement-indent-fixed' (for
113 `indent-tabs-mode' of nil) or `fortran-minimum-statement-indent-tab' (for
114 `indent-tabs-mode' of t), and
115 `relative' indents to current Fortran indentation plus
116 `fortran-comment-line-extra-indent'."
117 :type
'(radio (const :tag
"Untouched" nil
) (const fixed
) (const relative
))
118 :group
'fortran-indent
)
120 (defcustom fortran-comment-line-extra-indent
0
121 "*Amount of extra indentation for text within full-line comments."
123 :group
'fortran-indent
124 :group
'fortran-comment
)
126 (defcustom fortran-comment-line-start
"C"
127 "*Delimiter inserted to start new full-line comment.
128 You might want to change this to \"*\", for instance."
131 :group
'fortran-comment
)
133 ;; This used to match preprocessor lines too, but that messes up
134 ;; filling and doesn't seem to be necessary.
135 (defcustom fortran-comment-line-start-skip
136 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
137 "*Regexp to match the start of a full-line comment."
140 :group
'fortran-comment
)
142 (defcustom fortran-directive-re
144 "*Regexp to match a directive line.
145 The matching text will be fontified with `font-lock-keyword-face'.
146 The matching line will be given zero indentation."
149 :group
'fortran-indent
)
151 (defcustom fortran-minimum-statement-indent-fixed
6
152 "*Minimum statement indentation for fixed format continuation style."
154 :group
'fortran-indent
)
156 (defcustom fortran-minimum-statement-indent-tab
(max tab-width
6)
157 "*Minimum statement indentation for TAB format continuation style."
159 :group
'fortran-indent
)
161 ;; Note that this is documented in the v18 manuals as being a string
162 ;; of length one rather than a single character.
163 ;; The code in this file accepts either format for compatibility.
164 (defcustom fortran-comment-indent-char
" "
165 "*Single-character string inserted for Fortran comment indentation.
168 :group
'fortran-comment
)
170 (defcustom fortran-line-number-indent
1
171 "*Maximum indentation for Fortran line numbers.
172 5 means right-justify them within their five-column field."
174 :group
'fortran-indent
)
176 (defcustom fortran-check-all-num-for-matching-do nil
177 "*Non-nil causes all numbered lines to be treated as possible DO loop ends."
181 (defcustom fortran-blink-matching-if nil
182 "*Non-nil causes \\[fortran-indent-line] on ENDIF statement to blink on matching IF.
183 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
187 (defcustom fortran-continuation-string
"$"
188 "*Single-character string used for Fortran continuation lines.
189 In fixed format continuation style, this character is inserted in
190 column 6 by \\[fortran-split-line] to begin a continuation line.
191 Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
192 convert the line into a continuation line of the appropriate style.
197 (defcustom fortran-comment-region
"c$$$"
198 "*String inserted by \\[fortran-comment-region] at start of each \
201 :group
'fortran-comment
)
203 (defcustom fortran-electric-line-number t
204 "*Non-nil causes line number digits to be moved to the correct \
209 (defvar fortran-column-ruler-fixed
210 "0 4 6 10 20 30 40 5\
212 \[ ]|{ | | | | | | | | \
214 "String displayed above current line by \\[fortran-column-ruler].
215 This variable used in fixed format mode.")
217 (defvar fortran-column-ruler-tab
220 \[ ]| { | | | | | | | | \
222 "String displayed above current line by \\[fortran-column-ruler].
223 This variable used in TAB format mode.")
225 (defvar fortran-analyze-depth
100
226 "Number of lines to scan to determine whether to use fixed or TAB \
229 (defcustom fortran-break-before-delimiters t
230 "*Non-nil causes filling to break lines before delimiters."
234 (defvar fortran-mode-syntax-table
235 (let ((table (make-syntax-table)))
236 ;; We might like `;' to be punctuation (g77 multi-statement
237 ;; lines), but that screws abbrevs.
238 (modify-syntax-entry ?\
; "w" table)
239 (modify-syntax-entry ?
\r " " table
)
240 (modify-syntax-entry ?
+ "." table
)
241 (modify-syntax-entry ?-
"." table
)
242 (modify-syntax-entry ?
= "." table
)
243 (modify-syntax-entry ?
* "." table
)
244 (modify-syntax-entry ?
/ "." table
)
245 (modify-syntax-entry ?
\' "\"" table
)
246 (modify-syntax-entry ?
\" "\"" table
)
247 ;; Consistent with GNU Fortran -- see the manual.
248 (modify-syntax-entry ?
\\ "\\" table
)
249 ;; This might be better as punctuation, as for C, but this way you
250 ;; can treat floating-point numbers as symbols.
251 (modify-syntax-entry ?.
"_" table
) ; e.g. `a.ne.b'
252 (modify-syntax-entry ?_
"_" table
)
253 (modify-syntax-entry ?$
"_" table
) ; esp. VMSisms
254 (modify-syntax-entry ?\
! "<" table
)
255 (modify-syntax-entry ?
\n ">" table
)
257 "Syntax table in use in Fortran mode buffers.")
259 ;; Comments are real pain in Fortran because there is no way to
260 ;; represent the standard comment syntax in an Emacs syntax table.
261 ;; (We can do so for F90-style). Therefore an unmatched quote in a
262 ;; standard comment will throw fontification off on the wrong track.
263 ;; So we do syntactic fontification with regexps.
265 (defvar fortran-font-lock-keywords-1 nil
266 "Subdued level highlighting for Fortran mode.")
268 (defvar fortran-font-lock-keywords-2 nil
269 "Medium level highlighting for Fortran mode.")
271 (defvar fortran-font-lock-keywords-3 nil
272 "Gaudy level highlighting for Fortran mode.")
274 (defvar fortran-font-lock-syntactic-keywords nil
275 "`font-lock-syntactic-keywords' for Fortran.
276 These get fixed-format comments fontified.")
278 (let ((comment-chars "cd\\*") ; `d' for `debugging' comments
281 (let ((re (regexp-opt
283 '("character" "byte" "integer" "logical"
284 "none" "real" "complex"
285 "double precision" "double complex"))
286 (structured-types '("structure" "union" "map"))
287 (other-types '("record" "dimension"
288 "parameter" "common" "save"
289 "external" "intrinsic" "data"
292 (mapcar (lambda (x) (concat "implicit " x
))
295 (mapcar (lambda (x) (concat "end " x
))
299 ;; In the optimized regexp above, replace spaces by a
300 ;; regexp for optional whitespace; regexp-opt would have
302 (mapconcat #'identity
(split-string re
) "[ \t]*"))))
305 (regexp-opt '("continue" "format" "end" "enddo" "if" "then"
306 "else" "endif" "elseif" "while" "inquire" "stop"
307 "return" "include" "open" "close" "read" "write"
308 "format" "print" "select" "case" "cycle" "exit"
309 "rewind" "backspace"))))
312 (regexp-opt '("and" "or" "not" "lt" "le" "eq" "ge" "gt" "ne"
314 (setq fortran-font-lock-syntactic-keywords
315 ;; Fixed format comments. (!-style handled normally.)
317 (list (concat "^[" comment-chars
"]") 0 '(11))
318 (list (concat "^[^" comment-chars
"\t\n]" ".\\{71\\}"
321 (setq fortran-font-lock-keywords-1
323 ;; Program, subroutine and function declarations, plus calls.
324 (list (concat "\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|"
325 "program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?")
326 '(1 font-lock-keyword-face
)
327 '(2 font-lock-function-name-face nil t
))))
328 (setq fortran-font-lock-keywords-2
329 (append fortran-font-lock-keywords-1
331 ;; Fontify all type specifiers (must be first; see below).
332 (cons (concat "\\<\\(" fortran-type-types
"\\)\\>")
333 'font-lock-type-face
)
334 ;; Fontify all builtin keywords (except logical, do
335 ;; and goto; see below).
336 (concat "\\<\\(" fortran-keywords
"\\)\\>")
337 ;; Fontify all builtin operators.
338 (concat "\\.\\(" fortran-logicals
"\\)\\.")
339 ;; Fontify do/goto keywords and targets, and goto tags.
340 (list "\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
341 '(1 font-lock-keyword-face
)
342 '(2 font-lock-constant-face nil t
))
343 (cons "^ *\\([0-9]+\\)" 'font-lock-constant-face
))))
344 (setq fortran-font-lock-keywords-3
346 ;; The list `fortran-font-lock-keywords-1'.
347 fortran-font-lock-keywords-1
348 ;; Fontify all type specifiers plus their declared items.
350 (list (concat "\\<\\(" fortran-type-types
"\\)\\>[ \t(/]*\\(*\\)?")
351 ;; Fontify the type specifier.
352 '(1 font-lock-type-face
)
353 ;; Fontify each declaration item (or just the /.../ block name).
354 `(font-lock-match-c-style-declaration-item-and-skip-to-next
355 ;; Start after any *(...) expression.
357 (and (and (match-beginning ,(+ 2 (regexp-opt-depth
358 fortran-type-types
)))
362 ;; No need to clean up.
364 ;; Fontify as a variable name, functions are
365 ;; fontified elsewhere.
366 (1 font-lock-variable-name-face nil t
))))
367 ;; Things extra to `fortran-font-lock-keywords-3'
368 ;; (must be done first).
370 ;; Fontify goto-like `err=label'/`end=label' in read/write
372 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
373 (1 font-lock-keyword-face
) (4 font-lock-constant-face nil t
))
374 ;; Highlight standard continuation character and in a
375 ;; TAB-formatted line.
376 '("^ \\([^ 0]\\)" 1 font-lock-string-face
)
377 '("^\t\\([1-9]\\)" 1 font-lock-string-face
))
380 ;;; '("^# *[a-z]+" . font-lock-keyword-face))
381 `(,fortran-directive-re
(0 font-lock-keyword-face t
)))
382 ;; The list `fortran-font-lock-keywords-2' less that for types
384 (cdr (nthcdr (length fortran-font-lock-keywords-1
)
385 fortran-font-lock-keywords-2
)))))
387 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
388 "Default expressions to highlight in Fortran mode.")
390 (defvar fortran-imenu-generic-expression
391 ;; These patterns could be confused by sequence nos. in cols 72+ and
392 ;; don't allow continuations everywhere.
396 ;; [This will be fooled by `end function' allowed by G77. Also,
397 ;; it assumes sensible whitespace is employed.]
399 ;; leading whitespace:
401 ;; function declaration with optional type, e.g. `real',
402 ;; `real*4', character(*), `double precision':
403 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
404 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
405 ;; Possible statement continuation:
406 "[ \t" fortran-continuation-string
"]+"
407 ;; Variable to index:
410 ;; Un-named block data
411 (list nil
"^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
412 "Imenu generic expression for `imenu-default-create-index-function'.")
414 (defvar fortran-mode-map
415 (let ((map (make-sparse-keymap)))
416 (define-key map
";" 'fortran-abbrev-start
)
417 (define-key map
"\C-c;" 'fortran-comment-region
)
418 (define-key map
"\M-;" 'fortran-indent-comment
)
419 (define-key map
"\M-\n" 'fortran-split-line
)
420 (define-key map
"\M-\C-q" 'fortran-indent-subprogram
)
421 (define-key map
"\C-c\C-w" 'fortran-window-create-momentarily
)
422 (define-key map
"\C-c\C-r" 'fortran-column-ruler
)
423 (define-key map
"\C-c\C-p" 'fortran-previous-statement
)
424 (define-key map
"\C-c\C-n" 'fortran-next-statement
)
425 (define-key map
"\C-c\C-d" 'fortran-join-line
) ; like f90
426 (define-key map
"\M-^" 'fortran-join-line
) ; subvert delete-indentation
427 (define-key map
"0" 'fortran-electric-line-number
)
428 (define-key map
"1" 'fortran-electric-line-number
)
429 (define-key map
"2" 'fortran-electric-line-number
)
430 (define-key map
"3" 'fortran-electric-line-number
)
431 (define-key map
"4" 'fortran-electric-line-number
)
432 (define-key map
"5" 'fortran-electric-line-number
)
433 (define-key map
"6" 'fortran-electric-line-number
)
434 (define-key map
"7" 'fortran-electric-line-number
)
435 (define-key map
"8" 'fortran-electric-line-number
)
436 (define-key map
"9" 'fortran-electric-line-number
)
440 fortran-mode-menu map
""
442 ["Manual" (info "(emacs)Fortran")]
444 ,(custom-menu-create 'fortran
)
446 ["Save" Custom-save t
]
447 ["Reset to Current" Custom-reset-current t
]
448 ["Reset to Saved" Custom-reset-saved t
]
449 ["Reset to Standard Settings" Custom-reset-standard t
])
451 ["Toggle Auto-fill" auto-fill-mode
:style toggle
452 :selected auto-fill-function
]
453 ["Toggle abbrev-mode" abbrev-mode
:style toggle
:selected abbrev-mode
]
455 ["Comment-out Region" fortran-comment-region mark-active
]
456 ["Uncomment-out region"
457 (fortran-comment-region (region-beginning) (region-end) 1)
459 ["Indent Region" indent-region mark-active
]
460 ["Indent Subprogram" fortran-indent-subprogram t
]
462 ["Beginning of Subprogram" fortran-beginning-of-subprogram t
]
463 ["End of Subprogram" fortran-end-of-subprogram t
]
465 ["Subprogram" mark-defun
t]
466 ["IF Block" fortran-mark-if t
]
467 ["DO Block" fortran-mark-do t
])
468 ["Narrow to Subprogram" narrow-to-defun
t]
471 ["Temporary column ruler" fortran-column-ruler t
]
472 ["72-column window" fortran-window-create t
]
474 (enlarge-window-horizontally (- (frame-width) (window-width)))
475 (< (window-width) (frame-width))]
476 ["Momentary 72-column window" fortran-window-create-momentarily t
]
478 ["Break Line at Point" fortran-split-line t
]
479 ["Join Line" fortran-join-line t
]
480 ["Fill Statement/Comment" fill-paragraph t
]
483 imenu-add-menubar-index
(not (and (boundp 'imenu--index-alist
)
484 imenu--index-alist
))]))
486 "Keymap used in Fortran mode.")
488 (defvar fortran-mode-abbrev-table
489 (let (abbrevs-changed)
490 (define-abbrev-table 'fortran-mode-abbrev-table nil
)
491 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
492 ;; Only use `apply' to quieten the byte-compiler.
494 (function (lambda (element)
496 (apply 'define-abbrev fortran-mode-abbrev-table
497 (append element
'(nil 0 t
)))
498 (wrong-number-of-arguments
499 (apply 'define-abbrev fortran-mode-abbrev-table
500 (append element
'(nil 0)))))))
501 '((";au" "automatic" )
503 (";bd" "block data" )
512 (";dc" "double complex" )
513 (";dp" "double precision" )
519 (";eq" "equivalence" )
528 (";ib" "implicit byte" )
529 (";ic" "implicit complex" )
530 (";ich" "implicit character")
531 (";ii" "implicit integer" )
532 (";il" "implicit logical" )
533 (";ir" "implicit real" )
536 (";intr" "intrinsic" )
539 (";o" "open" ) ; was ;op
553 (";su" "subroutine" )
559 fortran-mode-abbrev-table
))
561 (eval-when-compile ; silence compiler
562 (defvar imenu-case-fold-search
)
563 (defvar imenu-syntax-alist
))
565 (defcustom fortran-mode-hook nil
566 "Hook run by Fortran mode."
571 (defun fortran-mode ()
572 "Major mode for editing Fortran code.
573 \\[fortran-indent-line] indents the current Fortran line correctly.
574 DO statements must not share a common CONTINUE.
576 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for
582 Variables controlling indentation style and extra features:
585 If you want to use comments starting with `!',
586 set this to the string \"!\".
588 Extra indentation within do blocks. (default 3)
590 Extra indentation within if blocks. (default 3)
591 `fortran-structure-indent'
592 Extra indentation within structure, union, map and interface blocks.
594 `fortran-continuation-indent'
595 Extra indentation applied to continuation statements. (default 5)
596 `fortran-comment-line-extra-indent'
597 Amount of extra indentation for text within full-line comments. (default 0)
598 `fortran-comment-indent-style'
599 nil means don't change indentation of text in full-line comments,
600 fixed means indent that text at `fortran-comment-line-extra-indent' beyond
601 the value of `fortran-minimum-statement-indent-fixed' (for fixed
602 format continuation style) or `fortran-minimum-statement-indent-tab'
603 (for TAB format continuation style).
604 relative means indent at `fortran-comment-line-extra-indent' beyond the
605 indentation for a line of code.
607 `fortran-comment-indent-char'
608 Single-character string to be inserted instead of space for
609 full-line comment indentation. (default \" \")
610 `fortran-minimum-statement-indent-fixed'
611 Minimum indentation for Fortran statements in fixed format mode. (def.6)
612 `fortran-minimum-statement-indent-tab'
613 Minimum indentation for Fortran statements in TAB format mode. (default 9)
614 `fortran-line-number-indent'
615 Maximum indentation for line numbers. A line number will get
616 less than this much indentation if necessary to avoid reaching
617 column 5. (default 1)
618 `fortran-check-all-num-for-matching-do'
619 Non-nil causes all numbered lines to be treated as possible \"continue\"
620 statements. (default nil)
621 `fortran-blink-matching-if'
622 Non-nil causes \\[fortran-indent-line] on an ENDIF statement to blink on
623 matching IF. Also, from an ENDDO statement, blink on matching DO [WHILE]
624 statement. (default nil)
625 `fortran-continuation-string'
626 Single-character string to be inserted in column 5 of a continuation
627 line. (default \"$\")
628 `fortran-comment-region'
629 String inserted by \\[fortran-comment-region] at start of each line in
630 region. (default \"c$$$\")
631 `fortran-electric-line-number'
632 Non-nil causes line number digits to be moved to the correct column
633 as typed. (default t)
634 `fortran-break-before-delimiters'
635 Non-nil causes lines to be broken before delimiters.
638 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
639 with no args, if that value is non-nil."
641 (kill-all-local-variables)
642 (setq local-abbrev-table fortran-mode-abbrev-table
)
643 (set-syntax-table fortran-mode-syntax-table
)
644 ;; Font Lock mode support.
645 (make-local-variable 'font-lock-defaults
)
646 (setq font-lock-defaults
'((fortran-font-lock-keywords
647 fortran-font-lock-keywords-1
648 fortran-font-lock-keywords-2
649 fortran-font-lock-keywords-3
)
650 nil t
((?
/ .
"$/") ("_$" .
"w"))
651 fortran-beginning-of-subprogram
))
652 (set (make-local-variable 'font-lock-syntactic-keywords
)
653 fortran-font-lock-syntactic-keywords
)
654 (make-local-variable 'fortran-break-before-delimiters
)
655 (setq fortran-break-before-delimiters t
)
656 (make-local-variable 'indent-line-function
)
657 (setq indent-line-function
'fortran-indent-line
)
658 (make-local-variable 'comment-indent-function
)
659 (setq comment-indent-function
'fortran-comment-indent
)
660 (set (make-local-variable 'comment-start-skip
)
661 ;; We can't reuse `fortran-comment-line-start-skip' directly because
662 ;; it contains backrefs whereas we need submatch-1 to end at the
663 ;; beginning of the comment delimiter.
664 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
665 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
666 (set (make-local-variable 'comment-padding
) "$$$")
667 (make-local-variable 'comment-start
)
668 (setq comment-start fortran-comment-line-start
)
669 (make-local-variable 'require-final-newline
)
670 (setq require-final-newline t
)
671 (make-local-variable 'abbrev-all-caps
)
672 (setq abbrev-all-caps t
)
673 (make-local-variable 'indent-tabs-mode
)
674 (setq indent-tabs-mode nil
)
675 ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
676 (set (make-local-variable 'fill-column
) 72)
677 (use-local-map fortran-mode-map
)
678 (setq mode-name
"Fortran")
679 (setq major-mode
'fortran-mode
)
680 (make-local-variable 'fortran-comment-line-extra-indent
)
681 (make-local-variable 'fortran-minimum-statement-indent-fixed
)
682 (make-local-variable 'fortran-minimum-statement-indent-tab
)
683 (make-local-variable 'fortran-column-ruler-fixed
)
684 (make-local-variable 'fortran-column-ruler-tab
)
685 (setq fortran-tab-mode-string
" TAB-format")
686 (setq indent-tabs-mode
(fortran-analyze-file-format))
687 (setq imenu-case-fold-search t
)
688 (setq imenu-generic-expression fortran-imenu-generic-expression
)
689 (setq imenu-syntax-alist
'(("_$" .
"w")))
690 (set (make-local-variable 'fill-paragraph-function
) 'fortran-fill-paragraph
)
691 (set (make-local-variable 'normal-auto-fill-function
) 'fortran-auto-fill
)
692 (set (make-local-variable 'indent-line-function
) 'fortran-indent-line
)
693 (set (make-local-variable 'indent-region-function
)
695 (let (fortran-blink-matching-if ; avoid blinking delay
696 indent-region-function
)
697 (indent-region start end nil
))))
698 (set (make-local-variable 'beginning-of-defun-function
)
699 #'fortran-beginning-of-subprogram
)
700 (set (make-local-variable 'end-of-defun-function
)
701 #'fortran-end-of-subprogram
)
702 (set (make-local-variable 'add-log-current-defun-function
)
703 #'fortran-current-defun
)
704 (set (make-local-variable 'dabbrev-case-fold-search
) 'case-fold-search
)
705 (run-hooks 'fortran-mode-hook
))
707 (defsubst fortran-comment-indent
()
709 (if (looking-at fortran-comment-line-start-skip
) 0
710 (skip-chars-backward " \t")
711 (max (+ 1 (current-column))
714 (defun fortran-indent-comment ()
715 "Align or create comment on current line.
716 Existing comments of all types are recognized and aligned.
717 If the line has no comment, a side-by-side comment is inserted and aligned
718 if the value of `comment-start' is not nil and allows such comments.
719 Otherwise, a separate-line comment is inserted, on this line
720 or on a new line inserted before this line if this line is not blank."
723 ;; Recognize existing comments of either kind.
724 (cond ((fortran-find-comment-start-skip 'all
)
725 (goto-char (match-beginning 0))
727 (fortran-indent-line)
728 (if (not (= (current-column)
729 (fortran-comment-indent)))
730 (progn (delete-horizontal-space)
731 (indent-to (fortran-comment-indent))))))
732 ;; No existing comment.
733 ;; If side-by-side comments are defined, insert one,
734 ;; unless line is now blank.
735 ((and comment-start
(not (looking-at "[ \t]*$"))
736 (string-match comment-start-skip
(concat " " comment-start
)))
738 (delete-horizontal-space)
739 (indent-to (fortran-comment-indent))
740 (insert comment-start
))
741 ;; Else insert separate-line comment, making a new line if nec.
743 (if (looking-at "^[ \t]*$")
744 (delete-horizontal-space)
748 (insert fortran-comment-line-start
)
749 (insert-char (if (stringp fortran-comment-indent-char
)
750 (aref fortran-comment-indent-char
0)
751 fortran-comment-indent-char
)
752 (- (fortran-calculate-indent) (current-column))))))
754 (defun fortran-comment-region (beg-region end-region arg
)
755 "Comments every line in the region.
756 Puts `fortran-comment-region' at the beginning of every line in the region.
757 BEG-REGION and END-REGION are args which specify the region boundaries.
758 With non-nil ARG, uncomments the region."
759 (interactive "*r\nP")
760 (let ((end-region-mark (copy-marker end-region
))
761 (save-point (point-marker)))
762 (goto-char beg-region
)
764 (if (not arg
) ;comment the region
765 (progn (insert fortran-comment-region
)
766 (while (and (= (forward-line 1) 0)
767 (< (point) end-region-mark
))
768 (insert fortran-comment-region
)))
769 (let ((com (regexp-quote fortran-comment-region
))) ;uncomment the region
771 (delete-region (point) (match-end 0)))
772 (while (and (= (forward-line 1) 0)
773 (< (point) end-region-mark
))
775 (delete-region (point) (match-end 0))))))
776 (goto-char save-point
)
777 (set-marker end-region-mark nil
)
778 (set-marker save-point nil
)))
780 (defun fortran-abbrev-start ()
781 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
782 Any other key combination is executed normally."
785 (insert last-command-char
)
786 (if (or (eq (setq c
(read-event)) ??
) ;insert char if not equal to `?'
788 (fortran-abbrev-help)
789 (setq unread-command-events
(list c
)))))
791 (defun fortran-abbrev-help ()
792 "List the currently defined abbrevs in Fortran mode."
794 (message "Listing abbrev table...")
795 (display-buffer (fortran-prepare-abbrev-list-buffer))
796 (message "Listing abbrev table...done"))
798 (defun fortran-prepare-abbrev-list-buffer ()
800 (set-buffer (get-buffer-create "*Abbrevs*"))
802 (insert-abbrev-table-description 'fortran-mode-abbrev-table t
)
803 (goto-char (point-min))
804 (set-buffer-modified-p nil
)
806 (get-buffer-create "*Abbrevs*"))
808 (defun fortran-column-ruler ()
809 "Insert a column ruler momentarily above current line, till next keystroke.
810 The ruler is defined by the value of `fortran-column-ruler-fixed' when in fixed
811 format mode, and `fortran-column-ruler-tab' when in TAB format mode.
812 The key typed is executed unless it is SPC."
814 (momentary-string-display
816 fortran-column-ruler-tab
817 fortran-column-ruler-fixed
)
820 (if (eq (window-start (selected-window))
821 (window-point (selected-window)))
822 (line-beginning-position 2)
824 nil
"Type SPC or any command to erase ruler."))
826 (defun fortran-window-create ()
827 "Make the window 72 columns wide.
828 See also `fortran-window-create-momentarily'."
830 (let ((window-min-width 2))
831 (if (< (window-width) (frame-width))
832 (enlarge-window-horizontally (- (frame-width)
834 (let* ((window-edges (window-edges))
835 (scroll-bar-width (- (nth 2 window-edges
)
838 (split-window-horizontally (+ 72 scroll-bar-width
)))
840 (switch-to-buffer " fortran-window-extra" t
)
841 (select-window (previous-window))))
843 (defun fortran-window-create-momentarily (&optional arg
)
844 "Momentarily make the window 72 columns wide.
845 Optional ARG non-nil and non-unity disables the momentary feature.
846 See also `fortran-window-create'."
850 (save-window-excursion
853 (fortran-window-create)
854 (error (error "No room for Fortran window")))
855 (message "Type SPC to continue editing.")
856 (let ((char (read-event)))
857 (or (equal char
(string-to-char " "))
858 (setq unread-command-events
(list char
))))))
859 (fortran-window-create)))
861 (defun fortran-split-line ()
862 "Break line at point and insert continuation marker and alignment."
864 (delete-horizontal-space)
868 (and (fortran-find-comment-start-skip 'all
)
869 (< (match-beginning 0) pos
))))
870 (insert ?
\n (match-string 0))
872 (insert ?
\n ?
\t (fortran-numerical-continuation-char))
873 (insert "\n " fortran-continuation-string
))) ; Space after \n important
874 (fortran-indent-line)) ; when the cont string is C, c or *.
876 (defun fortran-remove-continuation ()
877 (if (looking-at "\\( [^ 0\n]\\|\t[1-9]\\|&\\)")
878 (progn (replace-match "")
882 (defun fortran-join-line (arg)
883 "Join current line to the previous one and re-indent.
884 With a prefix argument, repeat this operation that many times.
885 If the prefix argument ARG is negative, join the next -ARG lines.
886 Continuation lines are correctly handled."
892 (while (not (zerop arg
))
894 (or (fortran-remove-continuation)
895 (delete-indentation))
897 (fortran-indent-line)))
899 (defun fortran-numerical-continuation-char ()
900 "Return a digit for tab-digit style of continuation lines.
901 If, previous line is a tab-digit continuation line, returns that digit
902 plus one. Otherwise return 1. Zero not allowed."
905 (if (looking-at "\t[1-9]")
906 (+ ?
1 (%
(- (char-after (+ (point) 1)) ?
0) 9))
909 (put 'fortran-electric-line-number
'delete-selection t
)
910 (defun fortran-electric-line-number (arg)
911 "Self insert, but if part of a Fortran line number indent it automatically.
912 Auto-indent does not happen if a numeric ARG is used."
914 (if (or arg
(not fortran-electric-line-number
))
916 (self-insert-command (prefix-numeric-value arg
))
917 (self-insert-command 1))
918 (if (or (and (= 5 (current-column))
921 (looking-at " \\{5\\}"))) ;In col 5 with only spaces to left.
922 (and (= (if indent-tabs-mode
923 fortran-minimum-statement-indent-tab
924 fortran-minimum-statement-indent-fixed
) (current-column))
925 (eq ?
\t (char-after (line-beginning-position))) ;In col 8
926 ; with a single tab to the left.
927 (not (or (eq last-command
'fortran-indent-line
)
929 'fortran-indent-new-line
))))
931 (re-search-backward "[^ \t0-9]"
932 (line-beginning-position)
933 t
)) ;not a line number
934 (looking-at "[0-9]")) ;within a line number
935 (self-insert-command (prefix-numeric-value arg
))
936 (skip-chars-backward " \t")
937 (insert last-command-char
)
938 (fortran-indent-line))))
940 (defvar fortran-end-prog-re1
942 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
943 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?")
944 (defvar fortran-end-prog-re
945 (concat "^[ \t0-9]*" fortran-end-prog-re1
)
946 "Regexp possibly marking subprogram end.")
948 (defun fortran-check-end-prog-re ()
949 "Check a preliminary match against `fortran-end-prog-re'."
950 ;; Having got a possible match for the subprogram end, we need a
951 ;; match of whitespace, avoiding possible column 73+ stuff.
953 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
954 (buffer-substring (match-end 0)
955 (min (line-end-position)
956 (+ 72 (line-beginning-position)))))))
958 ;; Note that you can't just check backwards for `subroutine' &c in
959 ;; case of un-marked main programs not at the start of the file.
960 (defun fortran-beginning-of-subprogram ()
961 "Move point to the beginning of the current Fortran subprogram."
964 (let ((case-fold-search t
))
965 (beginning-of-line -
1)
967 (while (re-search-backward fortran-end-prog-re nil
'move
)
968 (if (fortran-check-end-prog-re)
972 (defun fortran-end-of-subprogram ()
973 "Move point to the end of the current Fortran subprogram."
976 (let ((case-fold-search t
))
977 (if (save-excursion ; on END
979 (and (looking-at fortran-end-prog-re
)
980 (fortran-check-end-prog-re)))
982 (beginning-of-line 2)
984 (while (re-search-forward fortran-end-prog-re nil
'move
)
985 (if (fortran-check-end-prog-re)
987 (goto-char (match-beginning 0))
990 (defun fortran-previous-statement ()
991 "Move point to beginning of the previous Fortran statement.
992 Returns 'first-statement if that statement is the first
993 non-comment Fortran statement in the file, and nil otherwise.
994 Directive lines are treated as comments."
996 (let (not-first-statement continue-test
)
1000 (not (looking-at fortran-comment-line-start-skip
))
1001 (not (looking-at fortran-directive-re
))
1004 (regexp-quote fortran-continuation-string
)))
1005 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1006 (while (and (setq not-first-statement
(= (forward-line -
1) 0))
1007 (or (looking-at fortran-comment-line-start-skip
)
1008 (looking-at fortran-directive-re
)
1009 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1010 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1011 (cond ((and continue-test
1012 (not not-first-statement
))
1013 (message "Incomplete continuation statement."))
1015 (fortran-previous-statement))
1016 ((not not-first-statement
)
1017 'first-statement
))))
1019 (defun fortran-next-statement ()
1020 "Move point to beginning of the next Fortran statement.
1021 Returns 'last-statement if that statement is the last
1022 non-comment Fortran statement in the file, and nil otherwise.
1023 Directive lines are treated as comments."
1025 (let (not-last-statement)
1027 (while (and (setq not-last-statement
1028 (and (= (forward-line 1) 0)
1030 (or (looking-at fortran-comment-line-start-skip
)
1031 (looking-at fortran-directive-re
)
1032 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1033 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1034 (if (not not-last-statement
)
1037 (defun fortran-blink-match (regex keyword find-begin
)
1038 "From a line matching REGEX, blink matching KEYWORD statement line.
1039 Use function FIND-BEGIN to match it."
1040 (let ((top-of-window (window-start))
1042 (case-fold-search t
)
1047 (skip-chars-forward " \t0-9")
1050 (if (not (setq matching
(funcall find-begin
)))
1051 (setq message
(concat "No matching " keyword
"."))
1052 (if (< matching top-of-window
)
1054 (goto-char matching
)
1058 (buffer-substring (point)
1059 (line-end-position)))))))
1061 (message "%s" message
)
1062 (goto-char matching
)
1064 (goto-char end-point
))))))
1066 (defun fortran-blink-matching-if ()
1067 "From an ENDIF or ELSE statement, blink the matching IF statement."
1068 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1069 "if" #'fortran-beginning-if
))
1071 (defun fortran-blink-matching-do ()
1072 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1073 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do
))
1075 (defun fortran-mark-do ()
1076 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1077 The marks are pushed."
1079 (let (enddo-point do-point
)
1080 (if (setq enddo-point
(fortran-end-do))
1081 (if (not (setq do-point
(fortran-beginning-do)))
1082 (message "No matching do.")
1083 (goto-char enddo-point
)
1085 (goto-char do-point
)))))
1087 (defun fortran-end-do ()
1088 "Search forward for first unmatched ENDDO.
1089 Return point or nil."
1090 (let ((case-fold-search t
))
1091 (if (save-excursion (beginning-of-line)
1092 (skip-chars-forward " \t0-9")
1093 (looking-at "end[ \t]*do\\b"))
1099 (while (and (not (= count
0))
1100 (not (eq (fortran-next-statement) 'last-statement
))
1101 ;; Keep local to subprogram
1102 (not (and (looking-at fortran-end-prog-re
)
1103 (fortran-check-end-prog-re))))
1104 (skip-chars-forward " \t0-9")
1105 (cond ((looking-at "end[ \t]*do\\b")
1106 (setq count
(1- count
)))
1108 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1109 (setq count
(+ count
1)))))
1111 ;; All pairs accounted for.
1114 (defun fortran-beginning-do ()
1115 "Search backwards for first unmatched DO [WHILE].
1116 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1117 (let ((case-fold-search t
)
1118 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1121 (skip-chars-forward " \t0-9")
1122 (looking-at dostart-re
))
1128 (while (and (not (= count
0))
1129 (not (eq (fortran-previous-statement) 'first-statement
))
1130 ;; Keep local to subprogram
1131 (not (and (looking-at fortran-end-prog-re
)
1132 (fortran-check-end-prog-re))))
1133 (skip-chars-forward " \t0-9")
1134 (cond ((looking-at dostart-re
)
1135 (setq count
(1- count
)))
1136 ;; Note labelled loop ends not considered.
1137 ((looking-at "end[ \t]*do\\b")
1138 (setq count
(1+ count
)))))
1140 ;; All pairs accounted for.
1143 (defun fortran-mark-if ()
1144 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1145 The marks are pushed."
1147 (let (endif-point if-point
)
1148 (if (setq endif-point
(fortran-end-if))
1149 (if (not (setq if-point
(fortran-beginning-if)))
1150 (message "No matching if.")
1151 ;; Set mark, move point.
1152 (goto-char endif-point
)
1154 (goto-char if-point
)))))
1156 (defvar fortran-if-start-re
"\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1158 (defun fortran-end-if ()
1159 "Search forwards for first unmatched ENDIF.
1160 Return point or nil."
1161 (let ((case-fold-search t
))
1162 (if (save-excursion (beginning-of-line)
1163 (skip-chars-forward " \t0-9")
1164 (looking-at "end[ \t]*if\\b"))
1167 ;; Search for one. The point has been already been moved to first
1168 ;; letter on line but this should not cause troubles.
1171 (while (and (not (= count
0))
1172 (not (eq (fortran-next-statement) 'last-statement
))
1173 ;; Keep local to subprogram.
1174 (not (and (looking-at fortran-end-prog-re
)
1175 (fortran-check-end-prog-re))))
1176 (skip-chars-forward " \t0-9")
1177 (cond ((looking-at "end[ \t]*if\\b")
1178 (setq count
(- count
1)))
1179 ((looking-at fortran-if-start-re
)
1182 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1183 (let (then-test) ; Multi-line if-then.
1186 (= (forward-line 1) 0)
1187 ;; Search forward for then.
1188 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1192 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1194 (setq count
(+ count
1)))))))
1196 ;; All pairs accounted for.
1199 (defun fortran-beginning-if ()
1200 "Search backwards for first unmatched IF-THEN.
1201 Return point or nil."
1202 (let ((case-fold-search t
))
1204 ;; May be sitting on multi-line if-then statement, first
1205 ;; move to beginning of current statement. Note:
1206 ;; `fortran-previous-statement' moves to previous statement
1207 ;; *unless* current statement is first one. Only move
1208 ;; forward if not first-statement.
1209 (if (not (eq (fortran-previous-statement) 'first-statement
))
1210 (fortran-next-statement))
1211 (skip-chars-forward " \t0-9")
1213 (looking-at fortran-if-start-re
)
1215 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1216 ;; Multi-line if-then.
1219 (and (= (forward-line 1) 0)
1220 ;; Search forward for then.
1221 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1225 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1232 (while (and (not (= count
0))
1233 (not (eq (fortran-previous-statement) 'first-statement
))
1234 ;; Keep local to subprogram.
1235 (not (and (looking-at fortran-end-prog-re
)
1236 (fortran-check-end-prog-re))))
1237 (skip-chars-forward " \t0-9")
1238 (cond ((looking-at fortran-if-start-re
)
1241 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1242 (let (then-test) ; Multi-line if-then.
1245 (= (forward-line 1) 0)
1246 ;; Search forward for then.
1247 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1251 (concat ".*then\\b[ \t]*"
1252 "[^ \t(=a-z0-9]"))))))
1254 (setq count
(- count
1)))))
1255 ((looking-at "end[ \t]*if\\b")
1256 (setq count
(+ count
1)))))
1258 ;; All pairs accounted for.
1261 (defun fortran-indent-line ()
1262 "Indent current Fortran line based on its contents and on previous lines."
1264 (let ((cfi (fortran-calculate-indent)))
1267 (if (or (not (= cfi
(fortran-current-line-indentation)))
1268 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
1269 (not (fortran-line-number-indented-correctly-p))))
1270 (fortran-indent-to-column cfi
)
1272 (if (fortran-find-comment-start-skip)
1273 (fortran-indent-comment))))
1274 ;; Never leave point in left margin.
1275 (if (< (current-column) cfi
)
1276 (move-to-column cfi
))
1277 (if (and auto-fill-function
1278 (> (save-excursion (end-of-line) (current-column))
1283 (if fortran-blink-matching-if
1285 (fortran-blink-matching-if)
1286 (fortran-blink-matching-do)))))
1288 (defun fortran-auto-fill ()
1289 (if (> (current-column) (current-fill-column))
1290 (let ((cfi (fortran-calculate-indent)))
1293 (if (or (not (= cfi
(fortran-current-line-indentation)))
1294 (and (re-search-forward "^[ \t]*[0-9]+"
1296 (not (fortran-line-number-indented-correctly-p))))
1297 (fortran-indent-to-column cfi
)
1299 (if (fortran-find-comment-start-skip)
1300 (fortran-indent-comment))))
1302 ;; Never leave point in left margin.
1303 (if (< (current-column) cfi
)
1304 (move-to-column cfi
)))))
1306 ;; Historically this was a separate function which advertised itself
1307 ;; as reindenting but only did so where `most likely to be necessary'.
1308 (defalias 'fortran-indent-new-line
'reindent-then-newline-and-indent
)
1310 (defun fortran-indent-subprogram ()
1311 "Properly indent the Fortran subprogram which contains point."
1315 (message "Indenting subprogram...")
1316 (indent-region (point) (mark) nil
))
1317 (message "Indenting subprogram...done."))
1319 (defun fortran-calculate-indent ()
1320 "Calculates the Fortran indent column based on previous lines."
1321 (let (icol first-statement
(case-fold-search t
)
1322 (fortran-minimum-statement-indent
1323 (if indent-tabs-mode
1324 fortran-minimum-statement-indent-tab
1325 fortran-minimum-statement-indent-fixed
)))
1327 (setq first-statement
(fortran-previous-statement))
1329 (setq icol fortran-minimum-statement-indent
)
1331 (if (= (point) (point-min))
1332 (setq icol fortran-minimum-statement-indent
)
1333 (setq icol
(fortran-current-line-indentation)))
1334 (skip-chars-forward " \t0-9")
1335 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1336 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1337 (let (then-test) ;multi-line if-then
1338 (while (and (= (forward-line 1) 0)
1339 ;;search forward for then
1340 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1341 (not (setq then-test
1344 *[^ \t_$(=a-z0-9]")))))
1346 (setq icol
(+ icol fortran-if-indent
))))
1347 ((looking-at "else\\(if\\)?\\b")
1348 (setq icol
(+ icol fortran-if-indent
)))
1349 ((looking-at "select[ \t]*case[ \t](.*)")
1350 (setq icol
(+ icol fortran-if-indent
)))
1351 ((looking-at "case[ \t]*(.*)")
1352 (setq icol
(+ icol fortran-if-indent
)))
1353 ((looking-at "case[ \t]*default\\b")
1354 (setq icol
(+ icol fortran-if-indent
)))
1355 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1356 (setq icol
(+ icol fortran-if-indent
)))
1357 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1358 (setq icol
(+ icol fortran-if-indent
)))
1359 ((looking-at "do\\b")
1360 (setq icol
(+ icol fortran-do-indent
)))
1362 "\\(structure\\|union\\|map\\|interface\\)\
1363 \\b[ \t]*[^ \t=(a-z]")
1364 (setq icol
(+ icol fortran-structure-indent
)))
1365 ((and (looking-at fortran-end-prog-re1
)
1366 (fortran-check-end-prog-re))
1367 ;; Previous END resets indent to minimum
1368 (setq icol fortran-minimum-statement-indent
))))))
1371 (cond ((looking-at "[ \t]*$"))
1372 ;; Check for directive before comment, so as not to indent.
1373 ((looking-at fortran-directive-re
)
1374 (setq fortran-minimum-statement-indent
0 icol
0))
1375 ((looking-at fortran-comment-line-start-skip
)
1376 (cond ((eq fortran-comment-indent-style
'relative
)
1377 (setq icol
(+ icol fortran-comment-line-extra-indent
)))
1378 ((eq fortran-comment-indent-style
'fixed
)
1379 (setq icol
(+ fortran-minimum-statement-indent
1380 fortran-comment-line-extra-indent
))))
1381 (setq fortran-minimum-statement-indent
0))
1382 ((or (looking-at (concat "[ \t]*"
1384 fortran-continuation-string
)))
1385 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1386 (setq icol
(+ icol fortran-continuation-indent
)))
1388 ((and fortran-check-all-num-for-matching-do
1389 (looking-at "[ \t]*[0-9]+")
1390 (fortran-check-for-matching-do))
1391 (setq icol
(- icol fortran-do-indent
)))
1393 (skip-chars-forward " \t0-9")
1394 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1395 (setq icol
(- icol fortran-if-indent
)))
1396 ((looking-at "else\\(if\\)?\\b")
1397 (setq icol
(- icol fortran-if-indent
)))
1398 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1399 (setq icol
(- icol fortran-if-indent
)))
1400 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1401 (setq icol
(- icol fortran-if-indent
)))
1402 ((and (looking-at "continue\\b")
1403 (fortran-check-for-matching-do))
1404 (setq icol
(- icol fortran-do-indent
)))
1405 ((looking-at "end[ \t]*do\\b")
1406 (setq icol
(- icol fortran-do-indent
)))
1407 ((looking-at "end[ \t]*\
1408 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1409 (setq icol
(- icol fortran-structure-indent
)))
1410 ((and (looking-at fortran-end-prog-re1
)
1411 (fortran-check-end-prog-re)
1412 (not (= icol fortran-minimum-statement-indent
)))
1413 (message "Warning: `end' not in column %d. Probably\
1414 an unclosed block." fortran-minimum-statement-indent
))))))
1415 (max fortran-minimum-statement-indent icol
)))
1417 (defun fortran-current-line-indentation ()
1418 "Indentation of current line, ignoring Fortran line number or continuation.
1419 This is the column position of the first non-whitespace character
1420 aside from the line number and/or column 5/8 line-continuation character.
1421 For comment lines, returns indentation of the first
1422 non-indentation text within the comment."
1425 (cond ((looking-at fortran-comment-line-start-skip
)
1426 (goto-char (match-end 0))
1428 (if (stringp fortran-comment-indent-char
)
1429 fortran-comment-indent-char
1430 (char-to-string fortran-comment-indent-char
))))
1431 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1432 (goto-char (match-end 0)))
1434 ;; Move past line number.
1435 (skip-chars-forward "[ \t0-9]")))
1436 ;; Move past whitespace.
1437 (skip-chars-forward " \t")
1440 (defun fortran-indent-to-column (col)
1441 "Indent current line with spaces to column COL.
1442 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1443 line, and this continuation character is retained on indentation;
1444 2) If `fortran-continuation-string' is the first non-whitespace
1445 character, this is a continuation line;
1446 3) A non-continuation line which has a number as the first
1447 non-whitespace character is a numbered line.
1448 4) A TAB followed by a digit indicates a continuation line."
1451 (if (looking-at fortran-comment-line-start-skip
)
1452 (if fortran-comment-indent-style
1453 (let* ((char (if (stringp fortran-comment-indent-char
)
1454 (aref fortran-comment-indent-char
0)
1455 fortran-comment-indent-char
))
1456 (chars (string ? ?
\t char
)))
1457 (goto-char (match-end 0))
1458 (skip-chars-backward chars
)
1459 (delete-region (point) (progn (skip-chars-forward chars
)
1461 (insert-char char
(- col
(current-column)))))
1462 (if (looking-at "\t[1-9]")
1463 (if indent-tabs-mode
1464 (goto-char (match-end 0))
1467 (insert fortran-continuation-string
))
1468 (if (looking-at " \\{5\\}[^ 0\n]")
1469 (if indent-tabs-mode
1470 (progn (delete-char 6)
1471 (insert ?
\t (fortran-numerical-continuation-char) 1))
1473 (delete-horizontal-space)
1474 ;; Put line number in columns 0-4
1475 ;; or put continuation character in column 5.
1477 ((looking-at (regexp-quote fortran-continuation-string
))
1478 (if indent-tabs-mode
1481 (if indent-tabs-mode
1482 fortran-minimum-statement-indent-tab
1483 fortran-minimum-statement-indent-fixed
))
1485 (insert-char (fortran-numerical-continuation-char) 1))
1488 ((looking-at "[0-9]+")
1489 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1490 (if (< extra-space
0)
1491 (message "Warning: line number exceeds 5-digit limit.")
1492 (indent-to (min fortran-line-number-indent extra-space
))))
1493 (skip-chars-forward "0-9")))))
1494 ;; Point is now after any continuation character or line number.
1495 ;; Put body of statement where specified.
1496 (delete-horizontal-space)
1498 ;; Indent any comment following code on the same line.
1499 (if (fortran-find-comment-start-skip)
1500 (progn (goto-char (match-beginning 0))
1501 (if (not (= (current-column) (fortran-comment-indent)))
1502 (progn (delete-horizontal-space)
1503 (indent-to (fortran-comment-indent)))))))))
1505 (defun fortran-line-number-indented-correctly-p ()
1506 "Return t if current line's line number is correctly indented.
1507 Do not call if there is no line number."
1510 (skip-chars-forward " \t")
1511 (and (<= (current-column) fortran-line-number-indent
)
1512 (or (= (current-column) fortran-line-number-indent
)
1513 (progn (skip-chars-forward "0-9")
1514 (= (current-column) 5))))))
1516 (defun fortran-check-for-matching-do ()
1517 "When called from a numbered statement, return t if matching DO is found.
1518 Otherwise return nil."
1519 (let ((case-fold-search t
)
1523 (if (looking-at "[ \t]*[0-9]+")
1525 (skip-chars-forward " \t")
1526 (skip-chars-forward "0") ;skip past leading zeros
1528 (buffer-substring (point) (progn
1529 (skip-chars-forward "0-9")
1535 (and (re-search-backward
1537 "\\(^[ \t0-9]*do[ \t]*0*"
1538 charnum
"\\b\\)\\|" "\\(^[ \t]*0*"
1542 (concat "^[ \t0-9]*do[ \t]*0*"
1545 (defun fortran-find-comment-start-skip (&optional all
)
1546 "Move to past `comment-start-skip' found on current line.
1547 Return non-nil if `comment-start-skip' found, nil if not.
1548 If ALL is nil, only match comments that start in column > 0."
1550 ;; Hopefully at some point we can just use the line below! -stef
1551 ;; (comment-search-forward (line-end-position) t))
1552 (when (or all comment-start-skip
)
1554 (css (if comment-start-skip
1555 (concat fortran-comment-line-start-skip
1556 "\\|" comment-start-skip
)
1557 fortran-comment-line-start-skip
)))
1558 (when (re-search-forward css
(line-end-position) t
)
1559 (if (and (or all
(> (match-beginning 0) (line-beginning-position)))
1560 (or (save-match-data
1561 (not (fortran-is-in-string-p (match-beginning 0))))
1562 ;; Recurse for rest of line.
1563 (fortran-find-comment-start-skip all
)))
1568 ;;From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1569 ;; Test if TAB format continuation lines work.
1570 (defun fortran-is-in-string-p (where)
1571 "Return non-nil iff WHERE (a buffer position) is inside a Fortran string."
1575 ((bolp) nil
) ; bol is never inside a string
1576 ((save-excursion ; comment lines too
1578 (looking-at fortran-comment-line-start-skip
)) nil
)
1579 (t (let (;; ok, serious now. Init some local vars:
1580 (parse-state '(0 nil nil nil nil nil
0))
1581 (quoted-comment-start (if comment-start
1582 (regexp-quote comment-start
)))
1584 parse-limit end-of-line
)
1585 ;; move to start of current statement
1586 (fortran-next-statement)
1587 (fortran-previous-statement)
1588 ;; now parse up to WHERE
1590 (if (or ;; skip to next line if:
1592 (looking-at fortran-comment-line-start-skip
)
1593 ;; - at end of line?
1595 ;; - not in a string and after comment-start?
1596 (and (not (nth 3 parse-state
))
1598 (equal comment-start
1599 (char-to-string (preceding-char)))))
1600 (if (> (forward-line) 0)
1601 (setq not-done nil
))
1603 ;; if we are at beginning of code line, skip any
1604 ;; whitespace, labels and tab continuation markers.
1605 (if (bolp) (skip-chars-forward " \t0-9"))
1606 ;; if we are in column <= 5 now, check for continuation char
1607 (cond ((= 5 (current-column)) (forward-char 1))
1608 ((and (< (current-column) 5)
1609 (equal fortran-continuation-string
1610 (char-to-string (following-char)))
1612 ;; find out parse-limit from here
1613 (setq end-of-line
(line-end-position))
1614 (setq parse-limit
(min where end-of-line
))
1615 ;; parse max up to comment-start, if non-nil and in current line
1618 (if (re-search-forward quoted-comment-start end-of-line t
)
1619 (setq parse-limit
(min (point) parse-limit
)))))
1620 ;; now parse if still in limits
1621 (if (< (point) where
)
1622 (setq parse-state
(parse-partial-sexp
1623 (point) parse-limit nil nil parse-state
))
1624 (setq not-done nil
))))
1626 (nth 3 parse-state
))))))
1628 ;; From old version.
1629 (defalias 'fortran-auto-fill-mode
'auto-fill-mode
)
1631 (defun fortran-fill ()
1632 (let* ((auto-fill-function #'fortran-auto-fill
)
1634 (bol (line-beginning-position))
1635 (eol (line-end-position))
1636 (bos (min eol
(+ bol
(fortran-current-line-indentation))))
1640 (if (looking-at fortran-comment-line-start-skip
)
1641 nil
; OK to break quotes on comment lines.
1642 (move-to-column fill-column
)
1643 (if (fortran-is-in-string-p (point))
1644 (save-excursion (re-search-backward "\\S\"\\s\"\\S\"" bol t
)
1645 (if fortran-break-before-delimiters
1648 ;; decide where to split the line. If a position for a quoted
1649 ;; string was found above then use that, else break the line
1650 ;; before the last delimiter.
1651 ;; Delimiters are whitespace, commas, and operators.
1652 ;; Will break before a pair of *'s.
1656 (move-to-column (1+ fill-column
))
1657 (skip-chars-backward "^ \t\n,'+-/*=)"
1658 ;;; (if fortran-break-before-delimiters
1659 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1661 (if (<= (point) (1+ bos
))
1663 (move-to-column (1+ fill-column
))
1664 ;;what is this doing???
1665 (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t
))
1668 (re-search-forward "[ \t]" opoint t
)
1670 (if (looking-at "\\s\"")
1672 (skip-chars-backward " \t\*")))
1673 (if fortran-break-before-delimiters
1676 ;; if we are in an in-line comment, don't break unless the
1677 ;; line of code is longer than it should be. Otherwise
1678 ;; break the line at the column computed above.
1680 ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
1681 ;; don't prevent a break.
1682 (when (and (save-excursion
1684 (when (fortran-find-comment-start-skip)
1685 (goto-char (match-beginning 0))
1686 (>= (point) fill-point
)))
1688 (goto-char fill-point
)
1693 (min (1+ fill-column
)
1694 (+ (fortran-calculate-indent)
1695 fortran-continuation-indent
))))
1696 (goto-char fill-point
)
1697 (fortran-break-line)
1700 (defun fortran-break-line ()
1701 (let ((opoint (point))
1702 (bol (line-beginning-position))
1705 (if (fortran-find-comment-start-skip)
1706 (delete-and-extract-region
1707 (match-beginning 0) (line-end-position))))))
1708 ;; Forward line 1 really needs to go to next non white line
1709 (if (save-excursion (forward-line)
1710 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1713 (delete-region (point) (match-end 0))
1714 (delete-horizontal-space)
1716 (fortran-split-line))
1721 (delete-horizontal-space)
1722 (indent-to (fortran-comment-indent))
1723 (insert comment-string
)))))
1725 (defun fortran-analyze-file-format ()
1726 "Return nil if fixed format is used, t if TAB formatting is used.
1727 Use `fortran-tab-mode-default' if no non-comment statements are found in the
1728 file before the end or the first `fortran-analyze-depth' lines."
1731 (goto-char (point-min))
1735 (eq (char-after) ?
\t)
1736 (looking-at " \\{6\\}")
1737 (> i fortran-analyze-depth
)))
1741 ((eq (char-after) ?
\t) t
)
1742 ((looking-at " \\{6\\}") nil
)
1743 (fortran-tab-mode-default t
)
1746 (or (assq 'fortran-tab-mode-string minor-mode-alist
)
1747 (setq minor-mode-alist
(cons
1748 '(fortran-tab-mode-string
1749 (indent-tabs-mode fortran-tab-mode-string
))
1752 (defun fortran-fill-paragraph (&optional justify
)
1753 "Fill surrounding comment block as paragraphs, else fill statement.
1754 Intended as the value of `fill-paragraph-function'."
1758 (if (not (looking-at fortran-comment-line-start-skip
))
1759 (fortran-fill-statement)
1760 ;; We're in a comment block. Find the start and end of a
1761 ;; paragraph, delimited either by non-comment lines or empty
1762 ;; comments. (Get positions as markers, since the
1763 ;; `indent-region' below can shift the block's end).
1764 (let* ((non-empty-comment
1765 (concat fortran-comment-line-start-skip
"[^ \t\n]"))
1766 (start (save-excursion
1767 ;; Find (start of) first line.
1768 (while (and (zerop (forward-line -
1))
1769 (looking-at non-empty-comment
)))
1770 (or (looking-at non-empty-comment
)
1771 (forward-line)) ; overshot
1773 (end (save-excursion
1774 ;; Find start of first line past region to fill.
1777 (looking-at non-empty-comment
)))
1779 ;; Indent the block, find the string comprising the effective
1780 ;; comment start skip and use that as a fill-prefix for
1781 ;; filling the region.
1782 (indent-region start end nil
)
1783 (let ((paragraph-ignore-fill-prefix nil
)
1786 (looking-at fortran-comment-line-start-skip
)
1788 (let (fill-paragraph-function)
1789 (fill-region start end justify
))) ; with normal `fill-paragraph'
1790 (set-marker start nil
)
1791 (set-marker end nil
))))
1794 (defun fortran-fill-statement ()
1795 "Fill a fortran statement up to `fill-column'."
1797 (let ((auto-fill-function #'fortran-auto-fill
))
1798 (if (not (save-excursion
1800 (or (looking-at "[ \t]*$")
1801 (looking-at fortran-comment-line-start-skip
)
1802 (and comment-start-skip
1803 (looking-at (concat "[ \t]*" comment-start-skip
))))))
1805 ;; Find beginning of statement.
1806 (fortran-next-statement)
1807 (fortran-previous-statement)
1808 ;; Re-indent initially.
1809 (fortran-indent-line)
1810 ;; Replace newline plus continuation field plus indentation with
1814 (fortran-remove-continuation)))
1815 (fortran-previous-statement)))
1816 (fortran-indent-line)))
1818 (defun fortran-strip-sequence-nos (&optional do-space
)
1819 "Delete all text in column 72 and up (assumed to be sequence numbers).
1820 Normally also deletes trailing whitespace after stripping such text.
1821 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
1824 (goto-char (point-min))
1825 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t
)
1826 (replace-match "" nil nil nil
1)
1827 (unless do-space
(delete-horizontal-space)))))
1829 ;; This code used to live in add-log.el, but this is a better place
1831 (defun fortran-current-defun ()
1832 "Function to use for `add-log-current-defun-function' in Fortran mode."
1834 ;; We must be inside function body for this to work.
1835 (fortran-beginning-of-subprogram)
1836 (let ((case-fold-search t
)) ; case-insensitive
1837 ;; search for fortran subprogram start
1838 (if (re-search-forward
1839 (concat "^[ \t]*\\(program\\|subroutine\\|function"
1840 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
1841 "\\(block[ \t]*data\\)\\)")
1842 (save-excursion (fortran-end-of-subprogram)
1845 (or (match-string-no-properties 2)
1847 ;; move to EOL or before first left paren
1848 (if (re-search-forward "[(\n]" nil t
)
1849 (progn (backward-char)
1850 (skip-chars-backward " \t"))
1852 ;; Use the name preceding that.
1853 (buffer-substring-no-properties (point) (progn (backward-sexp)
1859 ;;; fortran.el ends here