1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;; Copyright (c) 1986, 1993, 1994, 1995, 1997, 1998 Free Software Foundation, Inc.
5 ;; Author: Michael D. Prange <prange@erl.mit.edu>
6 ;; Maintainer: Dave Love <fx@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This mode is documented in the Emacs manual.
30 ;; Note that it is for editing Fortran77 or Fortran90 fixed source
31 ;; form. For editing Fortran 90 free format source, use `f90-mode'
36 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
38 ;; We acknowledge many contributions and valuable suggestions by
39 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
40 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
41 ;; Gary Sabot and Richard Stallman.
47 ;; * Tidy it all up! (including renaming non-`fortran' prefixed
49 ;; * Implement insertion and removal of statement continuations in
50 ;; mixed f77/f90 style, with the first `&' past column 72 and the
51 ;; second in column 6.
52 ;; * Support any other extensions to f77 grokked by GNU Fortran.
53 ;; * Change fontification to use font-lock-syntactic-keywords for
54 ;; fixed-form comments. (Done, but doesn't work properly with
55 ;; lazy-lock in pre-20.4.)
60 "Fortran mode for Emacs"
63 (defgroup fortran-indent nil
64 "Indentation variables in Fortran mode"
68 (defgroup fortran-comment nil
69 "Comment-handling variables in Fortran mode"
75 (defcustom fortran-tab-mode-default nil
76 "*Default tabbing/carriage control style for empty files in Fortran mode.
77 A value of t specifies tab-digit style of continuation control.
78 A value of nil specifies that continuation lines are marked
79 with a character in column 6."
81 :group
'fortran-indent
)
83 ;; Buffer local, used to display mode line.
84 (defcustom fortran-tab-mode-string nil
85 "String to appear in mode line when TAB format mode is on."
86 :type
'(choice (const nil
) string
)
87 :group
'fortran-indent
)
88 (make-variable-buffer-local 'fortran-tab-mode-string
)
90 (defcustom fortran-do-indent
3
91 "*Extra indentation applied to DO blocks."
93 :group
'fortran-indent
)
95 (defcustom fortran-if-indent
3
96 "*Extra indentation applied to IF blocks."
98 :group
'fortran-indent
)
100 (defcustom fortran-structure-indent
3
101 "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
103 :group
'fortran-indent
)
105 (defcustom fortran-continuation-indent
5
106 "*Extra indentation applied to Fortran continuation lines."
108 :group
'fortran-indent
)
110 (defcustom fortran-comment-indent-style
'fixed
111 "*How to indent comments.
112 nil forces comment lines not to be touched,
113 'fixed makes fixed comment indentation to `fortran-comment-line-extra-indent'
114 columns beyond `fortran-minimum-statement-indent-fixed' (for
115 `indent-tabs-mode' of nil) or `fortran-minimum-statement-indent-tab' (for
116 `indent-tabs-mode' of t), and 'relative indents to current
117 Fortran indentation plus `fortran-comment-line-extra-indent'."
118 :type
'(radio (const :tag
"Untouched" nil
) (const fixed
) (const relative
))
119 :group
'fortran-indent
)
121 (defcustom fortran-comment-line-extra-indent
0
122 "*Amount of extra indentation for text within full-line comments."
124 :group
'fortran-indent
125 :group
'fortran-comment
)
127 (defcustom comment-line-start nil
128 "*Delimiter inserted to start new full-line comment."
129 :type
'(choice string
(const nil
))
130 :group
'fortran-comment
)
132 (defcustom comment-line-start-skip nil
133 "*Regexp to match the start of a full-line comment."
134 :type
'(choice string
(const nil
))
135 :group
'fortran-comment
)
137 (defcustom fortran-minimum-statement-indent-fixed
6
138 "*Minimum statement indentation for fixed format continuation style."
140 :group
'fortran-indent
)
142 (defcustom fortran-minimum-statement-indent-tab
(max tab-width
6)
143 "*Minimum statement indentation for TAB format continuation style."
145 :group
'fortran-indent
)
147 ;; Note that this is documented in the v18 manuals as being a string
148 ;; of length one rather than a single character.
149 ;; The code in this file accepts either format for compatibility.
150 (defcustom fortran-comment-indent-char
" "
151 "*Single-character string inserted for Fortran comment indentation.
154 :group
'fortran-comment
)
156 (defcustom fortran-line-number-indent
1
157 "*Maximum indentation for Fortran line numbers.
158 5 means right-justify them within their five-column field."
160 :group
'fortran-indent
)
162 (defcustom fortran-check-all-num-for-matching-do nil
163 "*Non-nil causes all numbered lines to be treated as possible DO loop ends."
167 (defcustom fortran-blink-matching-if nil
168 "*Non-nil causes \\[fortran-indent-line] on ENDIF statement to blink on matching IF.
169 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
173 (defcustom fortran-continuation-string
"$"
174 "*Single-character string used for Fortran continuation lines.
175 In fixed format continuation style, this character is inserted in
176 column 6 by \\[fortran-split-line] to begin a continuation line.
177 Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
178 convert the line into a continuation line of the appropriate style.
183 (defcustom fortran-comment-region
"c$$$"
184 "*String inserted by \\[fortran-comment-region] at start of each \
187 :group
'fortran-comment
)
189 (defcustom fortran-electric-line-number t
190 "*Non-nil causes line number digits to be moved to the correct \
195 (defvar fortran-column-ruler-fixed
196 "0 4 6 10 20 30 40 5\
198 \[ ]|{ | | | | | | | | \
200 "String displayed above current line by \\[fortran-column-ruler].
201 This variable used in fixed format mode.")
203 (defvar fortran-column-ruler-tab
206 \[ ]| { | | | | | | | | \
208 "String displayed above current line by \\[fortran-column-ruler].
209 This variable used in TAB format mode.")
211 (defvar fortran-mode-syntax-table nil
212 "Syntax table in use in Fortran mode buffers.")
214 (defvar fortran-analyze-depth
100
215 "Number of lines to scan to determine whether to use fixed or TAB \
218 (defcustom fortran-break-before-delimiters t
219 "*Non-nil causes filling to break lines before delimiters."
223 (if fortran-mode-syntax-table
225 (setq fortran-mode-syntax-table
(make-syntax-table))
226 ;; We might like `;' to be punctuation (g77 multi-statement lines),
227 ;; but that screws abbrevs.
228 (modify-syntax-entry ?\
; "w" fortran-mode-syntax-table)
229 (modify-syntax-entry ?
\r " " fortran-mode-syntax-table
)
230 (modify-syntax-entry ?
+ "." fortran-mode-syntax-table
)
231 (modify-syntax-entry ?-
"." fortran-mode-syntax-table
)
232 (modify-syntax-entry ?
= "." fortran-mode-syntax-table
)
233 (modify-syntax-entry ?
* "." fortran-mode-syntax-table
)
234 (modify-syntax-entry ?
/ "." fortran-mode-syntax-table
)
235 (modify-syntax-entry ?
\' "\"" fortran-mode-syntax-table
)
236 (modify-syntax-entry ?
\" "\"" fortran-mode-syntax-table
)
237 (modify-syntax-entry ?
\\ "/" fortran-mode-syntax-table
)
238 ;; This might be better as punctuation, as for C, but this way you
239 ;; can treat floating-point numbers as symbols.
240 (modify-syntax-entry ?.
"_" fortran-mode-syntax-table
) ; e.g. `a.ne.b'
241 (modify-syntax-entry ?_
"_" fortran-mode-syntax-table
)
242 (modify-syntax-entry ?$
"_" fortran-mode-syntax-table
) ; esp. VMSisms
243 (modify-syntax-entry ?\
! "<" fortran-mode-syntax-table
)
244 (modify-syntax-entry ?
\n ">" fortran-mode-syntax-table
))
246 ;; Comments are real pain in Fortran because there is no way to represent the
247 ;; standard comment syntax in an Emacs syntax table (we can for VAX-style).
248 ;; Therefore an unmatched quote in a standard comment will throw fontification
249 ;; off on the wrong track. So we do syntactic fontification with regexps.
251 ;; Regexps done by simon@gnu with help from Ulrik Dickow <dickow@nbi.dk> and
252 ;; probably others Si's forgotten about (sorry).
254 (defconst fortran-font-lock-keywords-1 nil
255 "Subdued level highlighting for Fortran mode.")
257 (defconst fortran-font-lock-keywords-2 nil
258 "Medium level highlighting for Fortran mode.")
260 (defconst fortran-font-lock-keywords-3 nil
261 "Gaudy level highlighting for Fortran mode.")
263 (defun fortran-fontify-string (limit)
264 (let ((match (match-string 1)))
265 (cond ((string= "'" match
)
266 (re-search-forward "\\([^'\n]*'?\\)" limit
))
267 ((string= "\"" match
)
268 (re-search-forward "\\([^\"\n]*\"?\\)" limit
)))))
270 (let ((comment-chars "c!*d") ; `d' for `debugging' comments
274 ; (let ((simple-types '("character" "byte" "integer" "logical"
275 ; "none" "real" "complex"
276 ; "double[ \t]*precision" "double[ \t]*complex"))
277 ; (structured-types '("structure" "union" "map"))
278 ; (other-types '("record" "dimension" "parameter" "common" "save"
279 ; "external" "intrinsic" "data" "equivalence")))
281 ; (mapcar (lambda (x) (concat "implicit[ \t]*" x)) simple-types)
283 ; (mapcar (lambda (x) (concat "end[ \t]*" x)) structured-types)
287 ;; Derived from the above, changing the escaped `[ \t]*'s back.
288 ;; Should be done with a `replace all in string' function at compile time...
289 "byte\\|c\\(haracter\\|om\\(mon\\|plex\\)\\)\\|d\\(ata\\|imension\\|ouble[ \t]*\\(complex\\|precision\\)\\)\\|e\\(nd[ \t]*\\(map\\|structure\\|union\\)\\|quivalence\\|xternal\\)\\|i\\(mplicit[ \t]*\\(byte\\|c\\(haracter\\|omplex\\)\\|double[ \t]*\\(complex\\|precision\\)\\|integer\\|logical\\|none\\|real\\)\\|nt\\(eger\\|rinsic\\)\\)\\|logical\\|map\\|none\\|parameter\\|re\\(al\\|cord\\)\\|s\\(ave\\|tructure\\)\\|union")
292 (regexp-opt '("continue" "format" "end" "enddo" "if" "then"
293 "else" "endif" "elseif" "while" "inquire" "stop"
294 "return" "include" "open" "close" "read" "write"
295 "format" "print" "select" "case" "cycle" "exit"))))
298 (regexp-opt '("and" "or" "not" "lt" "le" "eq" "ge" "gt" "ne"
301 (setq fortran-font-lock-keywords-1
304 ;; Fontify syntactically (assuming strings cannot be quoted
306 (cons (concat "^[" comment-chars
"].*") 'font-lock-comment-face
)
307 '(fortran-match-!-comment . font-lock-comment-face
)
308 (list (concat "^[^" comment-chars
"\t\n]" (make-string 71 ?.
)
310 '(1 font-lock-comment-face
))
311 '("\\(\\s\"\\)" ; single- or double-quoted string
312 (1 font-lock-string-face
)
313 (fortran-fontify-string nil nil
(1 font-lock-string-face
)))
315 ;; Program, subroutine and function declarations, plus calls.
316 (list (concat "\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|"
317 "program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?")
318 '(1 font-lock-keyword-face
)
319 '(2 font-lock-function-name-face nil t
))))
321 (setq fortran-font-lock-keywords-2
322 (append fortran-font-lock-keywords-1
325 ;; Fontify all type specifiers (must be first; see below).
326 (cons (concat "\\<\\(" fortran-type-types
"\\)\\>")
327 'font-lock-type-face
)
329 ;; Fontify all builtin keywords (except logical, do
330 ;; and goto; see below).
331 (concat "\\<\\(" fortran-keywords
"\\)\\>")
333 ;; Fontify all builtin operators.
334 (concat "\\.\\(" fortran-logicals
"\\)\\.")
336 ;; Fontify do/goto keywords and targets, and goto tags.
337 (list "\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
338 '(1 font-lock-keyword-face
)
339 '(2 font-lock-constant-face nil t
))
340 (cons "^ *\\([0-9]+\\)" 'font-lock-constant-face
))))
342 (setq fortran-font-lock-keywords-3
345 ;; The list `fortran-font-lock-keywords-1'.
346 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.
356 (and (match-beginning 15)
360 ;; No need to clean up.
362 ;; Fontify as a variable name, functions are
363 ;; fontified elsewhere.
364 (1 font-lock-variable-name-face nil t
))))
366 ;; Things extra to `fortran-font-lock-keywords-3'
367 ;; (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
))
375 ;; Highlight standard continuation character and in a
376 ;; TAB-formatted line.
377 '("^ \\([^ 0]\\)" 1 font-lock-string-face
)
378 '("^\t\\([1-9]\\)" 1 font-lock-string-face
))
380 ;; The list `fortran-font-lock-keywords-2' less that for types
382 (cdr (nthcdr (length fortran-font-lock-keywords-1
)
383 fortran-font-lock-keywords-2
)))))
385 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
386 "Default expressions to highlight in Fortran mode.")
388 (defvar fortran-imenu-generic-expression
389 ;; These patterns could be confused by sequence nos. in cols 72+ and
390 ;; don't allow continuations everywhere.
394 ;; Lines below are: 1. leading whitespace; 2. function
395 ;; declaration with optional type, e.g. `real', `real*4',
396 ;; character(*), `double precision' and possible statement
397 ;; continuation; 3. untyped declarations; 4. the variable to
398 ;; index. [This will be fooled by `end function' allowed by G77.
399 ;; Also, it assumes sensible whitespace is employed.]
401 \\(\\sw\\|\\s-\\|[*()+]\\)*\
402 \\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)\
403 [ \t" fortran-continuation-string
"]+\
406 ;; Un-named block data
407 (list nil
"^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
408 "Imenu generic expression for `imenu-default-create-index-function'.")
410 (defvar fortran-mode-map
()
411 "Keymap used in Fortran mode.")
414 (setq fortran-mode-map
(make-sparse-keymap))
415 (define-key fortran-mode-map
";" 'fortran-abbrev-start
)
416 (define-key fortran-mode-map
"\C-c;" 'fortran-comment-region
)
417 (define-key fortran-mode-map
"\M-\C-a" 'beginning-of-fortran-subprogram
)
418 (define-key fortran-mode-map
"\M-\C-e" 'end-of-fortran-subprogram
)
419 (define-key fortran-mode-map
"\M-;" 'fortran-indent-comment
)
420 (define-key fortran-mode-map
"\M-\C-h" 'mark-fortran-subprogram
)
421 (define-key fortran-mode-map
"\M-\n" 'fortran-split-line
)
422 (define-key fortran-mode-map
"\n" 'fortran-indent-new-line
)
423 (define-key fortran-mode-map
"\M-\C-q" 'fortran-indent-subprogram
)
424 (define-key fortran-mode-map
"\C-c\C-w" 'fortran-window-create-momentarily
)
425 (define-key fortran-mode-map
"\C-c\C-r" 'fortran-column-ruler
)
426 (define-key fortran-mode-map
"\C-c\C-p" 'fortran-previous-statement
)
427 (define-key fortran-mode-map
"\C-c\C-n" 'fortran-next-statement
)
428 (define-key fortran-mode-map
"\C-c\C-d" 'fortran-join-line
) ; like f90
429 (define-key fortran-mode-map
"\M-^" 'fortran-join-line
) ; subvert delete-indentation
430 (define-key fortran-mode-map
"\C-xnd" 'fortran-narrow-to-subprogram
)
431 ;(define-key fortran-mode-map "\t" 'fortran-indent-line)
432 (define-key fortran-mode-map
"0" 'fortran-electric-line-number
)
433 (define-key fortran-mode-map
"1" 'fortran-electric-line-number
)
434 (define-key fortran-mode-map
"2" 'fortran-electric-line-number
)
435 (define-key fortran-mode-map
"3" 'fortran-electric-line-number
)
436 (define-key fortran-mode-map
"4" 'fortran-electric-line-number
)
437 (define-key fortran-mode-map
"5" 'fortran-electric-line-number
)
438 (define-key fortran-mode-map
"6" 'fortran-electric-line-number
)
439 (define-key fortran-mode-map
"7" 'fortran-electric-line-number
)
440 (define-key fortran-mode-map
"8" 'fortran-electric-line-number
)
441 (define-key fortran-mode-map
"9" 'fortran-electric-line-number
)
444 (unless (boundp 'fortran-mode-menu
)
446 fortran-mode-menu fortran-mode-map
""
448 ["Toggle Auto-fill" fortran-auto-fill-mode
:style toggle
449 :selected
(eq auto-fill-function
'fortran-do-auto-fill
)]
450 ["Toggle abbrev-mode" abbrev-mode
:style toggle
:selected abbrev-mode
]
452 ["Comment-out Region" fortran-comment-region mark-active
]
453 ["Uncomment-out region"
454 (fortran-comment-region (region-beginning) (region-end) 1)
456 ["Indent Region" indent-region mark-active
]
457 ["Indent Subprogram" fortran-indent-subprogram t
]
459 ["Beginning of Subprogram" beginning-of-fortran-subprogram t
]
460 ["End of Subprogram" end-of-fortran-subprogram t
]
462 ["Subprogram" mark-fortran-subprogram t
]
463 ["IF Block" fortran-mark-if t
]
464 ["DO Block" fortran-mark-do t
])
465 ["Narrow to Subprogram" fortran-narrow-to-subprogram t
]
468 ["Temporary column ruler" fortran-column-ruler t
]
469 ["72-column window" fortran-window-create t
]
471 (enlarge-window-horizontally (- (frame-width) (window-width)))
472 (< (window-width) (frame-width))]
473 ["Momentary 72-column window" fortran-window-create-momentarily t
]
475 ["Break Line at Point" fortran-split-line t
]
476 ["Join Line" fortran-join-line t
]
477 ["Fill Statement/Comment" fill-paragraph t
]
480 (progn (imenu-add-menubar-index)
481 ;; Prod menu bar to update -- is this the right way?
483 (not (and (boundp 'imenu--index-alist
)
484 imenu--index-alist
))]))))
486 (defvar fortran-mode-abbrev-table nil
)
487 (if fortran-mode-abbrev-table
489 (let ((ac abbrevs-changed
))
490 (define-abbrev-table 'fortran-mode-abbrev-table
())
491 (define-abbrev fortran-mode-abbrev-table
";au" "automatic" nil
)
492 (define-abbrev fortran-mode-abbrev-table
";b" "byte" nil
)
493 (define-abbrev fortran-mode-abbrev-table
";bd" "block data" nil
)
494 (define-abbrev fortran-mode-abbrev-table
";ch" "character" nil
)
495 (define-abbrev fortran-mode-abbrev-table
";cl" "close" nil
)
496 (define-abbrev fortran-mode-abbrev-table
";c" "continue" nil
)
497 (define-abbrev fortran-mode-abbrev-table
";cm" "common" nil
)
498 (define-abbrev fortran-mode-abbrev-table
";cx" "complex" nil
)
499 (define-abbrev fortran-mode-abbrev-table
";df" "define" nil
)
500 (define-abbrev fortran-mode-abbrev-table
";di" "dimension" nil
)
501 (define-abbrev fortran-mode-abbrev-table
";do" "double" nil
)
502 (define-abbrev fortran-mode-abbrev-table
";dc" "double complex" nil
)
503 (define-abbrev fortran-mode-abbrev-table
";dp" "double precision" nil
)
504 (define-abbrev fortran-mode-abbrev-table
";dw" "do while" nil
)
505 (define-abbrev fortran-mode-abbrev-table
";e" "else" nil
)
506 (define-abbrev fortran-mode-abbrev-table
";ed" "enddo" nil
)
507 (define-abbrev fortran-mode-abbrev-table
";el" "elseif" nil
)
508 (define-abbrev fortran-mode-abbrev-table
";en" "endif" nil
)
509 (define-abbrev fortran-mode-abbrev-table
";eq" "equivalence" nil
)
510 (define-abbrev fortran-mode-abbrev-table
";ew" "endwhere" nil
)
511 (define-abbrev fortran-mode-abbrev-table
";ex" "external" nil
)
512 (define-abbrev fortran-mode-abbrev-table
";ey" "entry" nil
)
513 (define-abbrev fortran-mode-abbrev-table
";f" "format" nil
)
514 (define-abbrev fortran-mode-abbrev-table
";fa" ".false." nil
)
515 (define-abbrev fortran-mode-abbrev-table
";fu" "function" nil
)
516 (define-abbrev fortran-mode-abbrev-table
";g" "goto" nil
)
517 (define-abbrev fortran-mode-abbrev-table
";im" "implicit" nil
)
518 (define-abbrev fortran-mode-abbrev-table
";ib" "implicit byte" nil
)
519 (define-abbrev fortran-mode-abbrev-table
";ic" "implicit complex" nil
)
520 (define-abbrev fortran-mode-abbrev-table
";ich" "implicit character" nil
)
521 (define-abbrev fortran-mode-abbrev-table
";ii" "implicit integer" nil
)
522 (define-abbrev fortran-mode-abbrev-table
";il" "implicit logical" nil
)
523 (define-abbrev fortran-mode-abbrev-table
";ir" "implicit real" nil
)
524 (define-abbrev fortran-mode-abbrev-table
";inc" "include" nil
)
525 (define-abbrev fortran-mode-abbrev-table
";in" "integer" nil
)
526 (define-abbrev fortran-mode-abbrev-table
";intr" "intrinsic" nil
)
527 (define-abbrev fortran-mode-abbrev-table
";l" "logical" nil
)
528 (define-abbrev fortran-mode-abbrev-table
";n" "namelist" nil
)
529 (define-abbrev fortran-mode-abbrev-table
";o" "open" nil
) ; was ;op
530 (define-abbrev fortran-mode-abbrev-table
";pa" "parameter" nil
)
531 (define-abbrev fortran-mode-abbrev-table
";pr" "program" nil
)
532 (define-abbrev fortran-mode-abbrev-table
";ps" "pause" nil
)
533 (define-abbrev fortran-mode-abbrev-table
";p" "print" nil
)
534 (define-abbrev fortran-mode-abbrev-table
";rc" "record" nil
)
535 (define-abbrev fortran-mode-abbrev-table
";re" "real" nil
)
536 (define-abbrev fortran-mode-abbrev-table
";r" "read" nil
)
537 (define-abbrev fortran-mode-abbrev-table
";rt" "return" nil
)
538 (define-abbrev fortran-mode-abbrev-table
";rw" "rewind" nil
)
539 (define-abbrev fortran-mode-abbrev-table
";s" "stop" nil
)
540 (define-abbrev fortran-mode-abbrev-table
";sa" "save" nil
)
541 (define-abbrev fortran-mode-abbrev-table
";st" "structure" nil
)
542 (define-abbrev fortran-mode-abbrev-table
";sc" "static" nil
)
543 (define-abbrev fortran-mode-abbrev-table
";su" "subroutine" nil
)
544 (define-abbrev fortran-mode-abbrev-table
";tr" ".true." nil
)
545 (define-abbrev fortran-mode-abbrev-table
";ty" "type" nil
)
546 (define-abbrev fortran-mode-abbrev-table
";vo" "volatile" nil
)
547 (define-abbrev fortran-mode-abbrev-table
";w" "write" nil
)
548 (define-abbrev fortran-mode-abbrev-table
";wh" "where" nil
)
549 (setq abbrevs-changed ac
)))
551 (eval-when-compile ; silence compiler
552 (defvar imenu-case-fold-search
)
553 (defvar imenu-syntax-alist
))
556 (defun fortran-mode ()
557 "Major mode for editing Fortran code.
558 \\[fortran-indent-line] indents the current Fortran line correctly.
559 DO statements must not share a common CONTINUE.
561 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for
567 Variables controlling indentation style and extra features:
570 Normally nil in Fortran mode. If you want to use comments
571 starting with `!', set this to the string \"!\".
573 Extra indentation within do blocks. (default 3)
575 Extra indentation within if blocks. (default 3)
576 `fortran-structure-indent'
577 Extra indentation within structure, union, map and interface blocks.
579 `fortran-continuation-indent'
580 Extra indentation applied to continuation statements. (default 5)
581 `fortran-comment-line-extra-indent'
582 Amount of extra indentation for text within full-line comments. (default 0)
583 `fortran-comment-indent-style'
584 nil means don't change indentation of text in full-line comments,
585 fixed means indent that text at `fortran-comment-line-extra-indent' beyond
586 the value of `fortran-minimum-statement-indent-fixed' (for fixed
587 format continuation style) or `fortran-minimum-statement-indent-tab'
588 (for TAB format continuation style).
589 relative means indent at `fortran-comment-line-extra-indent' beyond the
590 indentation for a line of code.
592 `fortran-comment-indent-char'
593 Single-character string to be inserted instead of space for
594 full-line comment indentation. (default \" \")
595 `fortran-minimum-statement-indent-fixed'
596 Minimum indentation for Fortran statements in fixed format mode. (def.6)
597 `fortran-minimum-statement-indent-tab'
598 Minimum indentation for Fortran statements in TAB format mode. (default 9)
599 `fortran-line-number-indent'
600 Maximum indentation for line numbers. A line number will get
601 less than this much indentation if necessary to avoid reaching
602 column 5. (default 1)
603 `fortran-check-all-num-for-matching-do'
604 Non-nil causes all numbered lines to be treated as possible \"continue\"
605 statements. (default nil)
606 `fortran-blink-matching-if'
607 Non-nil causes \\[fortran-indent-line] on an ENDIF statement to blink on
608 matching IF. Also, from an ENDDO statement, blink on matching DO [WHILE]
609 statement. (default nil)
610 `fortran-continuation-string'
611 Single-character string to be inserted in column 5 of a continuation
612 line. (default \"$\")
613 `fortran-comment-region'
614 String inserted by \\[fortran-comment-region] at start of each line in
615 region. (default \"c$$$\")
616 `fortran-electric-line-number'
617 Non-nil causes line number digits to be moved to the correct column
618 as typed. (default t)
619 `fortran-break-before-delimiters'
620 Non-nil causes `fortran-fill' to break lines before delimiters.
623 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
624 with no args, if that value is non-nil."
626 (kill-all-local-variables)
627 (setq local-abbrev-table fortran-mode-abbrev-table
)
628 (set-syntax-table fortran-mode-syntax-table
)
629 ;; Font Lock mode support.
630 (make-local-variable 'font-lock-defaults
)
631 (setq font-lock-defaults
'((fortran-font-lock-keywords
632 fortran-font-lock-keywords-1
633 fortran-font-lock-keywords-2
634 fortran-font-lock-keywords-3
)
635 t t
((?
/ .
"$/") ("_$" .
"w"))))
636 (make-local-variable 'fortran-break-before-delimiters
)
637 (setq fortran-break-before-delimiters t
)
638 (make-local-variable 'indent-line-function
)
639 (setq indent-line-function
'fortran-indent-line
)
640 (make-local-variable 'comment-indent-function
)
641 (setq comment-indent-function
'fortran-comment-hook
)
642 (make-local-variable 'comment-line-start-skip
)
643 (setq comment-line-start-skip
644 "^[Cc*]\\(\\([^ \t\n]\\)\\2\\2*\\)?[ \t]*\\|^#.*")
645 (make-local-variable 'comment-line-start
)
646 (setq comment-line-start
"c")
647 (make-local-variable 'comment-start-skip
)
648 (setq comment-start-skip
"![ \t]*")
649 (make-local-variable 'comment-start
)
650 (setq comment-start nil
)
651 (make-local-variable 'require-final-newline
)
652 (setq require-final-newline t
)
653 (make-local-variable 'abbrev-all-caps
)
654 (setq abbrev-all-caps t
)
655 (make-local-variable 'indent-tabs-mode
)
656 (setq indent-tabs-mode nil
)
657 ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
658 (set (make-local-variable 'fill-column
) 72)
659 (use-local-map fortran-mode-map
)
660 (setq mode-name
"Fortran")
661 (setq major-mode
'fortran-mode
)
662 (make-local-variable 'fortran-comment-line-extra-indent
)
663 (make-local-variable 'fortran-minimum-statement-indent-fixed
)
664 (make-local-variable 'fortran-minimum-statement-indent-tab
)
665 (make-local-variable 'fortran-column-ruler-fixed
)
666 (make-local-variable 'fortran-column-ruler-tab
)
667 (setq fortran-tab-mode-string
" TAB-format")
668 (setq indent-tabs-mode
(fortran-analyze-file-format))
669 (setq imenu-case-fold-search t
)
670 (make-local-variable 'imenu-generic-expression
)
671 (setq imenu-generic-expression fortran-imenu-generic-expression
)
672 (setq imenu-syntax-alist
'(("_$" .
"w")))
673 (set (make-local-variable 'fill-paragraph-function
) 'fortran-fill-paragraph
)
674 (set (make-local-variable 'indent-line-function
) 'fortran-indent-line
)
675 (set (make-local-variable 'indent-region-function
)
677 (let (fortran-blink-matching-if ; avoid blinking delay
678 indent-region-function
)
679 (indent-region start end nil
))))
680 (run-hooks 'fortran-mode-hook
))
682 (defun fortran-comment-hook ()
684 (skip-chars-backward " \t")
685 (max (+ 1 (current-column))
688 (defun fortran-indent-comment ()
689 "Align or create comment on current line.
690 Existing comments of all types are recognized and aligned.
691 If the line has no comment, a side-by-side comment is inserted and aligned
692 if the value of `comment-start' is not nil.
693 Otherwise, a separate-line comment is inserted, on this line
694 or on a new line inserted before this line if this line is not blank."
697 ;; Recognize existing comments of either kind.
698 (cond ((looking-at comment-line-start-skip
)
699 (fortran-indent-line))
700 ((fortran-find-comment-start-skip) ; catches any inline comment and
701 ; leaves point after comment-start-skip
702 (if comment-start-skip
703 (progn (goto-char (match-beginning 0))
704 (if (not (= (current-column) (fortran-comment-hook)))
705 (progn (delete-horizontal-space)
706 (indent-to (fortran-comment-hook)))))
707 (end-of-line))) ; otherwise goto end of line or sth else?
708 ;; No existing comment.
709 ;; If side-by-side comments are defined, insert one,
710 ;; unless line is now blank.
711 ((and comment-start
(not (looking-at "^[ \t]*$")))
713 (delete-horizontal-space)
714 (indent-to (fortran-comment-hook))
715 (insert comment-start
))
716 ;; Else insert separate-line comment, making a new line if nec.
718 (if (looking-at "^[ \t]*$")
719 (delete-horizontal-space)
723 (insert comment-line-start
)
724 (insert-char (if (stringp fortran-comment-indent-char
)
725 (aref fortran-comment-indent-char
0)
726 fortran-comment-indent-char
)
727 (- (fortran-calculate-indent) (current-column))))))
729 (defun fortran-comment-region (beg-region end-region arg
)
730 "Comments every line in the region.
731 Puts `fortran-comment-region' at the beginning of every line in the region.
732 BEG-REGION and END-REGION are args which specify the region boundaries.
733 With non-nil ARG, uncomments the region."
734 (interactive "*r\nP")
735 (let ((end-region-mark (make-marker)) (save-point (point-marker)))
736 (set-marker end-region-mark end-region
)
737 (goto-char beg-region
)
739 (if (not arg
) ;comment the region
740 (progn (insert fortran-comment-region
)
741 (while (and (= (forward-line 1) 0)
742 (< (point) end-region-mark
))
743 (insert fortran-comment-region
)))
744 (let ((com (regexp-quote fortran-comment-region
))) ;uncomment the region
746 (delete-region (point) (match-end 0)))
747 (while (and (= (forward-line 1) 0)
748 (< (point) end-region-mark
))
750 (delete-region (point) (match-end 0))))))
751 (goto-char save-point
)
752 (set-marker end-region-mark nil
)
753 (set-marker save-point nil
)))
755 (defun fortran-abbrev-start ()
756 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
757 Any other key combination is executed normally."
760 (insert last-command-char
)
761 (if (or (eq (setq c
(read-event)) ??
) ;insert char if not equal to `?'
763 (fortran-abbrev-help)
764 (setq unread-command-events
(list c
)))))
766 (defun fortran-abbrev-help ()
767 "List the currently defined abbrevs in Fortran mode."
769 (message "Listing abbrev table...")
770 (display-buffer (fortran-prepare-abbrev-list-buffer))
771 (message "Listing abbrev table...done"))
773 (defun fortran-prepare-abbrev-list-buffer ()
775 (set-buffer (get-buffer-create "*Abbrevs*"))
777 (insert-abbrev-table-description 'fortran-mode-abbrev-table t
)
778 (goto-char (point-min))
779 (set-buffer-modified-p nil
)
781 (get-buffer-create "*Abbrevs*"))
783 (defun fortran-column-ruler ()
784 "Insert a column ruler momentarily above current line, till next keystroke.
785 The ruler is defined by the value of `fortran-column-ruler-fixed' when in fixed
786 format mode, and `fortran-column-ruler-tab' when in TAB format mode.
787 The key typed is executed unless it is SPC."
789 (momentary-string-display
791 fortran-column-ruler-tab
792 fortran-column-ruler-fixed
)
795 (if (eq (window-start (selected-window))
796 (window-point (selected-window)))
797 (progn (forward-line) (point))
799 nil
"Type SPC or any command to erase ruler."))
801 (defun fortran-window-create ()
802 "Make the window 72 columns wide.
803 See also `fortran-window-create-momentarily'."
805 (condition-case error
807 (let ((window-min-width 2))
808 (if (< (window-width) (frame-width))
809 (enlarge-window-horizontally (- (frame-width)
811 (split-window-horizontally 73)
813 (switch-to-buffer " fortran-window-extra" t
)
814 (select-window (previous-window))))
815 (error (message "No room for Fortran window.")
818 (defun fortran-window-create-momentarily (&optional arg
)
819 "Momentarily make the window 72 columns wide.
820 Optional ARG non-nil and non-unity disables the momentary feature.
821 See also `fortran-window-create'."
825 (save-window-excursion
826 (if (not (equal (fortran-window-create) 'error
))
827 (progn (message "Type SPC to continue editing.")
828 (let ((char (read-event)))
829 (or (equal char
(string-to-char " "))
830 (setq unread-command-events
(list char
)))))))
831 (fortran-window-create)))
833 (defun fortran-split-line ()
834 "Break line at point and insert continuation marker and alignment."
836 (delete-horizontal-space)
837 (if (save-excursion (beginning-of-line) (looking-at comment-line-start-skip
))
838 (insert "\n" comment-line-start
" ")
840 (insert "\n\t" (fortran-numerical-continuation-char))
841 (insert "\n " fortran-continuation-string
))) ; Space after \n important
842 (fortran-indent-line)) ; when the cont string is C, c or *.
844 (defun fortran-remove-continuation ()
845 (if (looking-at "\\( [^ 0\n]\\|\t[1-9]\\|&\\)")
846 (progn (replace-match "")
850 (defun fortran-join-line (arg)
851 "Join current line to the previous one and re-indent.
852 With a prefix argument, repeat this operation that many times.
853 If the prefix argument ARG is negative, join the next -ARG lines.
854 Continuation lines are correctly handled."
860 (while (not (zerop arg
))
862 (or (fortran-remove-continuation)
863 (delete-indentation))
865 (fortran-indent-line)))
867 (defun fortran-numerical-continuation-char ()
868 "Return a digit for tab-digit style of continuation lines.
869 If, previous line is a tab-digit continuation line, returns that digit
870 plus one. Otherwise return 1. Zero not allowed."
873 (if (looking-at "\t[1-9]")
874 (+ ?
1 (%
(- (char-after (+ (point) 1)) ?
0) 9))
877 (defun delete-horizontal-regexp (chars)
878 "Delete all characters in CHARS around point.
879 CHARS is like the inside of a [...] in a regular expression
880 except that ] is never special and \ quotes ^, - or \."
882 (skip-chars-backward chars
)
883 (delete-region (point) (progn (skip-chars-forward chars
) (point))))
885 (put 'fortran-electric-line-number
'delete-selection t
)
886 (defun fortran-electric-line-number (arg)
887 "Self insert, but if part of a Fortran line number indent it automatically.
888 Auto-indent does not happen if a numeric ARG is used."
890 (if (or arg
(not fortran-electric-line-number
))
892 (self-insert-command (prefix-numeric-value arg
))
893 (self-insert-command 1))
894 (if (or (and (= 5 (current-column))
897 (looking-at " ")));In col 5 with only spaces to left.
898 (and (= (if indent-tabs-mode
899 fortran-minimum-statement-indent-tab
900 fortran-minimum-statement-indent-fixed
) (current-column))
903 (looking-at "\t"));In col 8 with a single tab to the left.
904 (not (or (eq last-command
'fortran-indent-line
)
906 'fortran-indent-new-line
))))
908 (re-search-backward "[^ \t0-9]"
912 t
)) ;not a line number
913 (looking-at "[0-9]")) ;within a line number
914 (self-insert-command (prefix-numeric-value arg
))
915 (skip-chars-backward " \t")
916 (insert last-command-char
)
917 (fortran-indent-line))))
919 (defvar fortran-end-prog-re1
921 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
922 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?")
923 (defvar fortran-end-prog-re
924 (concat "^[ \t0-9]*" fortran-end-prog-re1
)
925 "Regexp possibly marking subprogram end.")
927 (defun fortran-check-end-prog-re ()
928 "Check a preliminary match against `fortran-end-prog-re'."
929 ;; Having got a possible match for the subprogram end, we need a
930 ;; match of whitespace, avoiding possible column 73+ stuff.
932 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
933 (buffer-substring (match-end 0)
934 (min (line-end-position)
935 (+ 72 (line-beginning-position)))))))
937 ;; Note that you can't just check backwards for `subroutine' &c in
938 ;; case of un-marked main programs not at the start of the file.
939 (defun beginning-of-fortran-subprogram ()
940 "Moves point to the beginning of the current Fortran subprogram."
942 (let ((case-fold-search t
))
943 (beginning-of-line -
1)
945 (while (re-search-backward fortran-end-prog-re nil
'move
)
946 (if (fortran-check-end-prog-re)
950 (defun end-of-fortran-subprogram ()
951 "Moves point to the end of the current Fortran subprogram."
953 (let ((case-fold-search t
))
954 (if (save-excursion ; on END
956 (and (looking-at fortran-end-prog-re
)
957 (fortran-check-end-prog-re)))
959 (beginning-of-line 2)
961 (while (re-search-forward fortran-end-prog-re nil
'move
)
962 (if (fortran-check-end-prog-re)
964 (goto-char (match-beginning 0))
967 (defun mark-fortran-subprogram ()
968 "Put mark at end of Fortran subprogram, point at beginning.
969 The marks are pushed."
971 (end-of-fortran-subprogram)
973 (beginning-of-fortran-subprogram))
975 (defun fortran-previous-statement ()
976 "Moves point to beginning of the previous Fortran statement.
977 Returns `first-statement' if that statement is the first
978 non-comment Fortran statement in the file, and nil otherwise."
980 (let (not-first-statement continue-test
)
984 (not (looking-at comment-line-start-skip
))
986 (concat "[ \t]*" (regexp-quote fortran-continuation-string
)))
987 (or (looking-at " [^ 0\n]")
988 (looking-at "\t[1-9]")))))
989 (while (and (setq not-first-statement
(= (forward-line -
1) 0))
990 (or (looking-at comment-line-start-skip
)
991 (looking-at "[ \t]*$")
992 (looking-at " [^ 0\n]")
993 (looking-at "\t[1-9]")
994 (looking-at (concat "[ \t]*" comment-start-skip
)))))
995 (cond ((and continue-test
996 (not not-first-statement
))
997 (message "Incomplete continuation statement."))
999 (fortran-previous-statement))
1000 ((not not-first-statement
)
1001 'first-statement
))))
1003 (defun fortran-next-statement ()
1004 "Moves point to beginning of the next Fortran statement.
1005 Returns `last-statement' if that statement is the last
1006 non-comment Fortran statement in the file, and nil otherwise."
1008 (let (not-last-statement)
1010 (while (and (setq not-last-statement
1011 (and (= (forward-line 1) 0)
1013 (or (looking-at comment-line-start-skip
)
1014 (looking-at "[ \t]*$")
1015 (looking-at " [^ 0\n]")
1016 (looking-at "\t[1-9]")
1017 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1018 (if (not not-last-statement
)
1021 (defun fortran-narrow-to-subprogram ()
1022 "Make text outside the current subprogram invisible.
1023 The subprogram visible is the one that contains or follows point."
1026 (mark-fortran-subprogram)
1027 (narrow-to-region (region-beginning)
1030 (defmacro fortran-with-subprogram-narrowing
(&rest forms
)
1031 "Execute FORMS with buffer temporarily narrowed to current subprogram.
1032 Doesn't push a mark."
1035 (narrow-to-region (progn
1036 (beginning-of-fortran-subprogram)
1039 (end-of-fortran-subprogram)
1043 (defun fortran-blink-matching-if ()
1044 "From an ENDIF statement, blink the matching IF statement."
1045 (let ((top-of-window (window-start))
1046 (endif-point (point))
1047 (case-fold-search t
)
1050 (if (save-excursion (beginning-of-line)
1051 (skip-chars-forward " \t0-9")
1052 (looking-at "end[ \t]*if\\b"))
1054 (if (not (setq matching-if
(fortran-beginning-if)))
1055 (setq message
"No matching if.")
1056 (if (< matching-if top-of-window
)
1058 (goto-char matching-if
)
1063 (point) (progn (end-of-line) (point))))))))
1065 (message "%s" message
)
1066 (goto-char matching-if
)
1068 (goto-char endif-point
))))))
1070 (defun fortran-blink-matching-do ()
1071 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1072 ;; This is basically copied from fortran-blink-matching-if.
1073 (let ((top-of-window (window-start))
1074 (enddo-point (point))
1075 (case-fold-search t
)
1078 (if (save-excursion (beginning-of-line)
1079 (skip-chars-forward " \t0-9")
1080 (looking-at "end[ \t]*do\\b"))
1082 (if (not (setq matching-do
(fortran-beginning-do)))
1083 (setq message
"No matching do.")
1084 (if (< matching-do top-of-window
)
1086 (goto-char matching-do
)
1091 (point) (progn (end-of-line) (point))))))))
1093 (message "%s" message
)
1094 (goto-char matching-do
)
1096 (goto-char enddo-point
))))))
1098 (defun fortran-mark-do ()
1099 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1100 The marks are pushed."
1102 (let (enddo-point do-point
)
1103 (if (setq enddo-point
(fortran-end-do))
1104 (if (not (setq do-point
(fortran-beginning-do)))
1105 (message "No matching do.")
1106 ;; Set mark, move point.
1107 (goto-char enddo-point
)
1109 (goto-char do-point
)))))
1111 (defun fortran-end-do ()
1112 "Search forward for first unmatched ENDDO.
1113 Return point or nil."
1114 (let ((case-fold-search t
))
1115 (if (save-excursion (beginning-of-line)
1116 (skip-chars-forward " \t0-9")
1117 (looking-at "end[ \t]*do\\b"))
1123 (while (and (not (= count
0))
1124 (not (eq (fortran-next-statement) 'last-statement
))
1125 ;; Keep local to subprogram
1126 (not (and (looking-at fortran-end-prog-re
)
1127 (fortran-check-end-prog-re))))
1129 (skip-chars-forward " \t0-9")
1130 (cond ((looking-at "end[ \t]*do\\b")
1131 (setq count
(1- count
)))
1132 ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1133 (setq count
(+ count
1)))))
1135 ;; All pairs accounted for.
1138 (defun fortran-beginning-do ()
1139 "Search backwards for first unmatched DO [WHILE].
1140 Return point or nil."
1141 (let ((case-fold-search t
))
1142 (if (save-excursion (beginning-of-line)
1143 (skip-chars-forward " \t0-9")
1144 (looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+"))
1150 (while (and (not (= count
0))
1151 (not (eq (fortran-previous-statement) 'first-statement
))
1152 ;; Keep local to subprogram
1153 (not (and (looking-at fortran-end-prog-re
)
1154 (fortran-check-end-prog-re))))
1156 (skip-chars-forward " \t0-9")
1157 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1158 (setq count
(1- count
)))
1159 ((looking-at "end[ \t]*do\\b")
1160 (setq count
(1+ count
)))))
1163 ;; All pairs accounted for.
1166 (defun fortran-mark-if ()
1167 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1168 The marks are pushed."
1170 (let (endif-point if-point
)
1171 (if (setq endif-point
(fortran-end-if))
1172 (if (not (setq if-point
(fortran-beginning-if)))
1173 (message "No matching if.")
1174 ;; Set mark, move point.
1175 (goto-char endif-point
)
1177 (goto-char if-point
)))))
1179 (defvar fortran-if-start-re
"\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1181 (defun fortran-end-if ()
1182 "Search forwards for first unmatched ENDIF.
1183 Return point or nil."
1184 (let ((case-fold-search t
))
1185 (if (save-excursion (beginning-of-line)
1186 (skip-chars-forward " \t0-9")
1187 (looking-at "end[ \t]*if\\b"))
1190 ;; Search for one. The point has been already been moved to first
1191 ;; letter on line but this should not cause troubles.
1194 (while (and (not (= count
0))
1195 (not (eq (fortran-next-statement) 'last-statement
))
1196 ;; Keep local to subprogram.
1197 (not (and (looking-at fortran-end-prog-re
)
1198 (fortran-check-end-prog-re))))
1200 (skip-chars-forward " \t0-9")
1201 (cond ((looking-at "end[ \t]*if\\b")
1202 (setq count
(- count
1)))
1204 ((looking-at fortran-if-start-re
)
1207 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1208 (let (then-test) ; Multi-line if-then.
1210 (and (= (forward-line 1) 0)
1211 ;; Search forward for then.
1212 (or (looking-at " [^ 0\n]")
1213 (looking-at "\t[1-9]"))
1217 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1219 (setq count
(+ count
1)))))))
1222 ;; All pairs accounted for.
1225 (defun fortran-beginning-if ()
1226 "Search backwards for first unmatched IF-THEN.
1227 Return point or nil."
1228 (let ((case-fold-search t
))
1230 ;; May be sitting on multi-line if-then statement, first move to
1231 ;; beginning of current statement. Note: `fortran-previous-statement'
1232 ;; moves to previous statement *unless* current statement is first
1233 ;; one. Only move forward if not first-statement.
1234 (if (not (eq (fortran-previous-statement) 'first-statement
))
1235 (fortran-next-statement))
1236 (skip-chars-forward " \t0-9")
1238 (looking-at fortran-if-start-re
)
1240 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1241 ;; Multi-line if-then.
1244 (and (= (forward-line 1) 0)
1245 ;; Search forward for then.
1246 (or (looking-at " [^ 0\n]")
1247 (looking-at "\t[1-9]"))
1251 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1258 (while (and (not (= count
0))
1259 (not (eq (fortran-previous-statement) 'first-statement
))
1260 ;; Keep local to subprogram.
1261 (not (and (looking-at fortran-end-prog-re
)
1262 (fortran-check-end-prog-re))))
1264 (skip-chars-forward " \t0-9")
1265 (cond ((looking-at fortran-if-start-re
)
1268 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1269 (let (then-test) ; Multi-line if-then.
1271 (and (= (forward-line 1) 0)
1272 ;; Search forward for then.
1273 (or (looking-at " [^ 0\n]")
1274 (looking-at "\t[1-9]"))
1278 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1280 (setq count
(- count
1)))))
1281 ((looking-at "end[ \t]*if\\b")
1282 (setq count
(+ count
1)))))
1285 ;; All pairs accounted for.
1288 (defun fortran-indent-line ()
1289 "Indent current Fortran line based on its contents and on previous lines."
1291 (let ((cfi (fortran-calculate-indent)))
1294 (if (or (not (= cfi
(fortran-current-line-indentation)))
1295 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
1296 (not (fortran-line-number-indented-correctly-p))))
1297 (fortran-indent-to-column cfi
)
1299 (if (and (not (looking-at comment-line-start-skip
))
1300 (fortran-find-comment-start-skip))
1301 (fortran-indent-comment))))
1302 ;; Never leave point in left margin.
1303 (if (< (current-column) cfi
)
1304 (move-to-column cfi
))
1305 (if (and auto-fill-function
1306 (> (save-excursion (end-of-line) (current-column)) fill-column
))
1310 (if fortran-blink-matching-if
1312 (fortran-blink-matching-if)
1313 (fortran-blink-matching-do)))))
1315 (defun fortran-indent-new-line ()
1316 "Reindent the current Fortran line, insert a newline and indent the newline.
1317 An abbrev before point is expanded if variable `abbrev-mode' is non-nil."
1319 (if abbrev-mode
(expand-abbrev))
1322 (skip-chars-forward " \t")
1323 (let ((case-fold-search t
))
1324 (if (or (looking-at "[0-9]") ;Reindent only where it is most
1325 (looking-at "end") ;likely to be necessary
1327 (looking-at (regexp-quote fortran-continuation-string
)))
1328 (fortran-indent-line))))
1330 (fortran-indent-line))
1332 (defun fortran-indent-subprogram ()
1333 "Properly indent the Fortran subprogram which contains point."
1336 (mark-fortran-subprogram)
1337 (message "Indenting subprogram...")
1338 (indent-region (point) (mark) nil
))
1339 (message "Indenting subprogram...done."))
1341 (defun fortran-calculate-indent ()
1342 "Calculates the Fortran indent column based on previous lines."
1343 (let (icol first-statement
(case-fold-search t
)
1344 (fortran-minimum-statement-indent
1345 (if indent-tabs-mode
1346 fortran-minimum-statement-indent-tab
1347 fortran-minimum-statement-indent-fixed
)))
1349 (setq first-statement
(fortran-previous-statement))
1351 (setq icol fortran-minimum-statement-indent
)
1353 (if (= (point) (point-min))
1354 (setq icol fortran-minimum-statement-indent
)
1355 (setq icol
(fortran-current-line-indentation)))
1356 (skip-chars-forward " \t0-9")
1357 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1358 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1359 (let (then-test) ;multi-line if-then
1360 (while (and (= (forward-line 1) 0)
1361 ;;search forward for then
1362 (or (looking-at " [^ 0\n]")
1363 (looking-at "\t[1-9]"))
1364 (not (setq then-test
(looking-at
1366 *[^ \t_$(=a-z0-9]")))))
1368 (setq icol
(+ icol fortran-if-indent
))))
1369 ((looking-at "else\\(if\\)?\\b")
1370 (setq icol
(+ icol fortran-if-indent
)))
1371 ((looking-at "select[ \t]*case[ \t](.*)")
1372 (setq icol
(+ icol fortran-if-indent
)))
1373 ((looking-at "case[ \t]*(.*)")
1374 (setq icol
(+ icol fortran-if-indent
)))
1375 ((looking-at "case[ \t]*default\\b")
1376 (setq icol
(+ icol fortran-if-indent
)))
1377 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1378 (setq icol
(+ icol fortran-if-indent
)))
1379 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1380 (setq icol
(+ icol fortran-if-indent
)))
1381 ((looking-at "do\\b")
1382 (setq icol
(+ icol fortran-do-indent
)))
1384 "\\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1385 (setq icol
(+ icol fortran-structure-indent
)))
1386 ((and (looking-at fortran-end-prog-re1
)
1387 (fortran-check-end-prog-re))
1388 ;; Previous END resets indent to minimum
1389 (setq icol fortran-minimum-statement-indent
))))))
1392 (cond ((looking-at "[ \t]*$"))
1393 ((looking-at comment-line-start-skip
)
1394 (cond ((eq fortran-comment-indent-style
'relative
)
1395 (setq icol
(+ icol fortran-comment-line-extra-indent
)))
1396 ((eq fortran-comment-indent-style
'fixed
)
1397 (setq icol
(+ fortran-minimum-statement-indent
1398 fortran-comment-line-extra-indent
))))
1399 (setq fortran-minimum-statement-indent
0))
1400 ((or (looking-at (concat "[ \t]*"
1402 fortran-continuation-string
)))
1403 (looking-at " [^ 0\n]")
1404 (looking-at "\t[1-9]"))
1405 (setq icol
(+ icol fortran-continuation-indent
)))
1406 ((looking-at "[ \t]*#") ; Check for cpp directive.
1407 (setq fortran-minimum-statement-indent
0 icol
0))
1409 ((and fortran-check-all-num-for-matching-do
1410 (looking-at "[ \t]*[0-9]+")
1411 (fortran-check-for-matching-do))
1412 (setq icol
(- icol fortran-do-indent
)))
1414 (skip-chars-forward " \t0-9")
1415 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1416 (setq icol
(- icol fortran-if-indent
)))
1417 ((looking-at "else\\(if\\)?\\b")
1418 (setq icol
(- icol fortran-if-indent
)))
1419 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1420 (setq icol
(- icol fortran-if-indent
)))
1421 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1422 (setq icol
(- icol fortran-if-indent
)))
1423 ((and (looking-at "continue\\b")
1424 (fortran-check-for-matching-do))
1425 (setq icol
(- icol fortran-do-indent
)))
1426 ((looking-at "end[ \t]*do\\b")
1427 (setq icol
(- icol fortran-do-indent
)))
1428 ((looking-at "end[ \t]*\
1429 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1430 (setq icol
(- icol fortran-structure-indent
)))
1431 ((and (looking-at fortran-end-prog-re1
)
1432 (fortran-check-end-prog-re)
1433 (not (= icol fortran-minimum-statement-indent
)))
1434 (message "Warning: `end' not in column %d. Probably\
1435 an unclosed block." fortran-minimum-statement-indent
))))))
1436 (max fortran-minimum-statement-indent icol
)))
1438 (defun fortran-current-line-indentation ()
1439 "Indentation of current line, ignoring Fortran line number or continuation.
1440 This is the column position of the first non-whitespace character
1441 aside from the line number and/or column 5/8 line-continuation character.
1442 For comment lines, returns indentation of the first
1443 non-indentation text within the comment."
1446 (cond ((looking-at comment-line-start-skip
)
1447 (goto-char (match-end 0))
1449 (if (stringp fortran-comment-indent-char
)
1450 fortran-comment-indent-char
1451 (char-to-string fortran-comment-indent-char
))))
1452 ((or (looking-at " [^ 0\n]")
1453 (looking-at "\t[1-9]"))
1454 (goto-char (match-end 0)))
1456 ;; Move past line number.
1457 (skip-chars-forward "[ \t0-9]");From Uli
1459 ;; Move past whitespace.
1460 (skip-chars-forward " \t")
1463 (defun fortran-indent-to-column (col)
1464 "Indent current line with spaces to column COL.
1465 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1466 line, and this continuation character is retained on indentation;
1467 2) If `fortran-continuation-string' is the first non-whitespace
1468 character, this is a continuation line;
1469 3) A non-continuation line which has a number as the first
1470 non-whitespace character is a numbered line.
1471 4) A TAB followed by a digit indicates a continuation line."
1474 (if (looking-at comment-line-start-skip
)
1475 (if fortran-comment-indent-style
1476 (let ((char (if (stringp fortran-comment-indent-char
)
1477 (aref fortran-comment-indent-char
0)
1478 fortran-comment-indent-char
)))
1479 (goto-char (match-end 0))
1480 (delete-horizontal-regexp (concat " \t" (char-to-string char
)))
1481 (insert-char char
(- col
(current-column)))))
1482 (if (looking-at "\t[1-9]")
1483 (if indent-tabs-mode
1484 (goto-char (match-end 0))
1487 (insert fortran-continuation-string
))
1488 (if (looking-at " [^ 0\n]")
1489 (if indent-tabs-mode
1490 (progn (delete-char 6)
1492 (insert-char (fortran-numerical-continuation-char) 1))
1494 (delete-horizontal-space)
1495 ;; Put line number in columns 0-4
1496 ;; or put continuation character in column 5.
1498 ((looking-at (regexp-quote fortran-continuation-string
))
1499 (if indent-tabs-mode
1502 (if indent-tabs-mode
1503 fortran-minimum-statement-indent-tab
1504 fortran-minimum-statement-indent-fixed
))
1506 (insert-char (fortran-numerical-continuation-char) 1))
1509 ((looking-at "[0-9]+")
1510 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1511 (if (< extra-space
0)
1512 (message "Warning: line number exceeds 5-digit limit.")
1513 (indent-to (min fortran-line-number-indent extra-space
))))
1514 (skip-chars-forward "0-9")))))
1515 ;; Point is now after any continuation character or line number.
1516 ;; Put body of statement where specified.
1517 (delete-horizontal-space)
1519 ;; Indent any comment following code on the same line.
1520 (if (and comment-start-skip
1521 (fortran-find-comment-start-skip))
1522 (progn (goto-char (match-beginning 0))
1523 (if (not (= (current-column) (fortran-comment-hook)))
1524 (progn (delete-horizontal-space)
1525 (indent-to (fortran-comment-hook)))))))))
1527 (defun fortran-line-number-indented-correctly-p ()
1528 "Return t if current line's line number is correctly indented.
1529 Do not call if there is no line number."
1532 (skip-chars-forward " \t")
1533 (and (<= (current-column) fortran-line-number-indent
)
1534 (or (= (current-column) fortran-line-number-indent
)
1535 (progn (skip-chars-forward "0-9")
1536 (= (current-column) 5))))))
1538 (defun fortran-check-for-matching-do ()
1539 "When called from a numbered statement, return t if matching DO is found.
1540 Otherwise return nil."
1542 (case-fold-search t
))
1545 (if (looking-at "[ \t]*[0-9]+")
1547 (skip-chars-forward " \t")
1548 (skip-chars-forward "0") ;skip past leading zeros
1549 (setq charnum
(buffer-substring (point)
1550 (progn (skip-chars-forward "0-9")
1553 (fortran-with-subprogram-narrowing
1554 (and (re-search-backward
1555 (concat "\\(^[ \t0-9]*do[ \t]*0*" charnum
"\\b\\)\\|"
1556 "\\(^[ \t]*0*" charnum
"\\b\\)")
1558 (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum
)))))))))
1560 (defun fortran-find-comment-start-skip ()
1561 "Move to past `comment-start-skip' found on current line.
1562 Return t if `comment-start-skip' found, nil if not."
1563 ;; In order to move point only if comment-start-skip is found, this
1564 ;; one uses a lot of save-excursions. Note that re-search-forward
1565 ;; moves point even if comment-start-skip is inside a string-constant.
1566 ;; Some code expects certain values for match-beginning and end
1569 (re-search-forward comment-start-skip
1570 (save-excursion (end-of-line) (point)) t
))
1571 (let ((save-match-beginning (match-beginning 0))
1572 (save-match-end (match-end 0)))
1573 (if (fortran-is-in-string-p (match-beginning 0))
1575 (goto-char save-match-end
)
1576 (fortran-find-comment-start-skip)) ; recurse for rest of line
1577 (goto-char save-match-beginning
)
1578 (re-search-forward comment-start-skip
1579 (save-excursion (end-of-line) (point)) t
)
1580 (goto-char (match-end 0))
1584 ;;From: simon@gnu (Simon Marshall)
1585 ;; Find the next ! not in a string.
1586 (defun fortran-match-!-comment
(limit)
1588 (while (and (setq found
(search-forward "!" limit t
))
1589 (fortran-is-in-string-p (point))))
1592 ;; Cheaper than `looking-at' "!.*".
1594 (list (1- (point)) (progn (end-of-line) (min (point) limit
))))
1597 ;; The above function is about 10% faster than the below...
1598 ;;(defun fortran-match-!-comment (limit)
1600 ;; (while (and (setq found (re-search-forward "!.*" limit t))
1601 ;; (fortran-is-in-string-p (match-beginning 0))))
1604 ;;From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1605 ;; Test if TAB format continuation lines work.
1606 (defun fortran-is-in-string-p (where)
1607 "Return non-nil iff WHERE (a buffer position) is inside a Fortran string."
1611 ((bolp) nil
) ; bol is never inside a string
1612 ((save-excursion ; comment lines too
1614 (looking-at comment-line-start-skip
)) nil
)
1615 (t (let (;; ok, serious now. Init some local vars:
1616 (parse-state '(0 nil nil nil nil nil
0))
1617 (quoted-comment-start (if comment-start
1618 (regexp-quote comment-start
)))
1623 ;; move to start of current statement
1624 (fortran-next-statement)
1625 (fortran-previous-statement)
1626 ;; now parse up to WHERE
1628 (if (or ;; skip to next line if:
1630 (looking-at comment-line-start-skip
)
1631 ;; - at end of line?
1633 ;; - not in a string and after comment-start?
1634 (and (not (nth 3 parse-state
))
1636 (equal comment-start
1637 (char-to-string (preceding-char)))))
1638 (if (> (forward-line) 0)
1639 (setq not-done nil
))
1641 ;; if we are at beginning of code line, skip any
1642 ;; whitespace, labels and tab continuation markers.
1643 (if (bolp) (skip-chars-forward " \t0-9"))
1644 ;; if we are in column <= 5 now, check for continuation char
1645 (cond ((= 5 (current-column)) (forward-char 1))
1646 ((and (< (current-column) 5)
1647 (equal fortran-continuation-string
1648 (char-to-string (following-char)))
1650 ;; find out parse-limit from here
1651 (setq end-of-line
(save-excursion (end-of-line)(point)))
1652 (setq parse-limit
(min where end-of-line
))
1653 ;; parse max up to comment-start, if non-nil and in current line
1656 (if (re-search-forward quoted-comment-start end-of-line t
)
1657 (setq parse-limit
(min (point) parse-limit
)))))
1658 ;; now parse if still in limits
1659 (if (< (point) where
)
1660 (setq parse-state
(parse-partial-sexp
1661 (point) parse-limit nil nil parse-state
))
1662 (setq not-done nil
))
1665 (nth 3 parse-state
))))))
1667 (defun fortran-auto-fill-mode (arg)
1668 "Toggle fortran-auto-fill mode.
1669 With ARG, turn `fortran-auto-fill' mode on iff ARG is positive.
1670 In `fortran-auto-fill' mode, inserting a space at a column beyond `fill-column'
1671 automatically breaks the line at a previous space."
1673 (prog1 (setq auto-fill-function
1675 (not auto-fill-function
)
1676 (> (prefix-numeric-value arg
) 0))
1677 #'fortran-do-auto-fill
1679 (force-mode-line-update)))
1681 (defun fortran-do-auto-fill ()
1682 (if (> (current-column) fill-column
)
1683 (fortran-indent-line)))
1685 (defun fortran-fill ()
1687 (let* ((opoint (point))
1688 (bol (save-excursion (beginning-of-line) (point)))
1689 (eol (save-excursion (end-of-line) (point)))
1690 (bos (min eol
(+ bol
(fortran-current-line-indentation))))
1694 (if (looking-at comment-line-start-skip
)
1695 nil
; OK to break quotes on comment lines.
1696 (move-to-column fill-column
)
1697 (if (fortran-is-in-string-p (point))
1698 (save-excursion (re-search-backward "\\S\"\\s\"\\S\"" bol t
)
1699 (if fortran-break-before-delimiters
1702 ;; decide where to split the line. If a position for a quoted
1703 ;; string was found above then use that, else break the line
1704 ;; before the last delimiter.
1705 ;; Delimiters are whitespace, commas, and operators.
1706 ;; Will break before a pair of *'s.
1710 (move-to-column (1+ fill-column
))
1711 (skip-chars-backward "^ \t\n,'+-/*=)"
1712 ;;; (if fortran-break-before-delimiters
1713 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1715 (if (<= (point) (1+ bos
))
1717 (move-to-column (1+ fill-column
))
1718 ;;what is this doing???
1719 (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t
))
1722 (re-search-forward "[ \t]" opoint t
)
1724 (if (looking-at "\\s\"")
1726 (skip-chars-backward " \t\*")))
1727 (if fortran-break-before-delimiters
1730 ;; if we are in an in-line comment, don't break unless the
1731 ;; line of code is longer than it should be. Otherwise
1732 ;; break the line at the column computed above.
1734 ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
1735 ;; don't prevent a break.
1736 (if (not (or (save-excursion
1737 (if (and (re-search-backward comment-start-skip bol t
)
1738 (not (fortran-is-in-string-p (point))))
1740 (skip-chars-backward " \t")
1741 (< (current-column) (1+ fill-column
)))))
1743 (goto-char fill-point
)
1745 (if (> (save-excursion
1746 (goto-char fill-point
) (current-column))
1748 (progn (goto-char fill-point
)
1749 (fortran-break-line))
1751 (if (> (save-excursion
1752 (goto-char fill-point
)
1754 (+ (fortran-calculate-indent) fortran-continuation-indent
))
1756 (goto-char fill-point
)
1757 (fortran-break-line))))))
1759 (defun fortran-break-line ()
1760 (let ((opoint (point))
1761 (bol (save-excursion (beginning-of-line) (point)))
1762 (eol (save-excursion (end-of-line) (point)))
1763 (comment-string nil
))
1766 (if (and comment-start-skip
(fortran-find-comment-start-skip))
1768 (re-search-backward comment-start-skip bol t
)
1769 (setq comment-string
(buffer-substring (point) eol
))
1770 (delete-region (point) eol
))))
1771 ;; Forward line 1 really needs to go to next non white line
1772 (if (save-excursion (forward-line)
1773 (or (looking-at " [^ 0\n]")
1774 (looking-at "\t[1-9]")))
1777 (delete-region (point) (match-end 0))
1778 (delete-horizontal-space)
1780 (fortran-split-line))
1785 (delete-horizontal-space)
1786 (indent-to (fortran-comment-hook))
1787 (insert comment-string
)))))
1789 (defun fortran-analyze-file-format ()
1790 "Return nil if fixed format is used, t if TAB formatting is used.
1791 Use `fortran-tab-mode-default' if no non-comment statements are found in the
1792 file before the end or the first `fortran-analyze-depth' lines."
1795 (goto-char (point-min))
1801 (> i fortran-analyze-depth
)))
1805 ((looking-at "\t") t
)
1806 ((looking-at " ") nil
)
1807 (fortran-tab-mode-default t
)
1810 (or (assq 'fortran-tab-mode-string minor-mode-alist
)
1811 (setq minor-mode-alist
(cons
1812 '(fortran-tab-mode-string
1813 (indent-tabs-mode fortran-tab-mode-string
))
1816 (defun fortran-fill-paragraph (&optional justify
)
1817 "Fill surrounding comment block as paragraphs, else fill statement.
1819 Intended as the value of `fill-paragraph-function'."
1823 (if (not (looking-at "[Cc*]"))
1824 (fortran-fill-statement)
1825 ;; We're in a comment block. Find the start and end of a
1826 ;; paragraph, delimited either by non-comment lines or empty
1827 ;; comments. (Get positions as markers, since the
1828 ;; `indent-region' below can shift the block's end).
1829 (let* ((non-empty-comment (concat "\\(" comment-line-start-skip
1831 (start (save-excursion
1832 ;; Find (start of) first line.
1833 (while (and (zerop (forward-line -
1))
1834 (looking-at non-empty-comment
)))
1835 (or (looking-at non-empty-comment
)
1836 (forward-line)) ; overshot
1838 (end (save-excursion
1839 ;; Find start of first line past region to fill.
1840 (while (progn (forward-line)
1841 (looking-at non-empty-comment
)))
1843 ;; Indent the block, find the string comprising the effective
1844 ;; comment start skip and use that as a fill-prefix for
1845 ;; filling the region.
1846 (indent-region start end nil
)
1847 (let ((paragraph-ignore-fill-prefix nil
)
1848 (fill-prefix (progn (beginning-of-line)
1849 (looking-at comment-line-start-skip
)
1851 (let (fill-paragraph-function)
1852 (fill-region start end justify
))) ; with normal `fill-paragraph'
1853 (set-marker start nil
)
1854 (set-marker end nil
))))
1857 (defun fortran-fill-statement ()
1858 "Fill a fortran statement up to `fill-column'."
1860 (if (not (save-excursion
1862 (or (looking-at "[ \t]*$")
1863 (looking-at comment-line-start-skip
)
1864 (and comment-start-skip
1865 (looking-at (concat "[ \t]*" comment-start-skip
))))))
1867 ;; Find beginning of statement.
1868 (fortran-next-statement)
1869 (fortran-previous-statement)
1870 ;; Re-indent initially.
1871 (fortran-indent-line)
1872 ;; Replace newline plus continuation field plus indentation with
1876 (fortran-remove-continuation)))
1877 (fortran-previous-statement)))
1878 (fortran-indent-line))
1882 ;;; fortran.el ends here