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: Dave Love <fx@gnu.org>
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 'relative indents to current
115 Fortran indentation plus `fortran-comment-line-extra-indent'."
116 :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative))
117 :group 'fortran-indent)
119 (defcustom fortran-comment-line-extra-indent 0
120 "*Amount of extra indentation for text within full-line comments."
122 :group 'fortran-indent
123 :group 'fortran-comment)
125 (defcustom fortran-comment-line-start "C"
126 "*Delimiter inserted to start new full-line comment.
127 You might want to change this to \"*\", for instance."
130 :group 'fortran-comment)
132 ;; This used to match preprocessor lines too, but that messes up
133 ;; filling and doesn't seem to be necessary.
134 (defcustom fortran-comment-line-start-skip
135 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
136 "*Regexp to match the start of a full-line comment."
139 :group 'fortran-comment)
141 (defcustom fortran-minimum-statement-indent-fixed 6
142 "*Minimum statement indentation for fixed format continuation style."
144 :group 'fortran-indent)
146 (defcustom fortran-minimum-statement-indent-tab (max tab-width 6)
147 "*Minimum statement indentation for TAB format continuation style."
149 :group 'fortran-indent)
151 ;; Note that this is documented in the v18 manuals as being a string
152 ;; of length one rather than a single character.
153 ;; The code in this file accepts either format for compatibility.
154 (defcustom fortran-comment-indent-char " "
155 "*Single-character string inserted for Fortran comment indentation.
158 :group 'fortran-comment)
160 (defcustom fortran-line-number-indent 1
161 "*Maximum indentation for Fortran line numbers.
162 5 means right-justify them within their five-column field."
164 :group 'fortran-indent)
166 (defcustom fortran-check-all-num-for-matching-do nil
167 "*Non-nil causes all numbered lines to be treated as possible DO loop ends."
171 (defcustom fortran-blink-matching-if nil
172 "*Non-nil causes \\[fortran-indent-line] on ENDIF statement to blink on matching IF.
173 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
177 (defcustom fortran-continuation-string "$"
178 "*Single-character string used for Fortran continuation lines.
179 In fixed format continuation style, this character is inserted in
180 column 6 by \\[fortran-split-line] to begin a continuation line.
181 Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
182 convert the line into a continuation line of the appropriate style.
187 (defcustom fortran-comment-region "c$$$"
188 "*String inserted by \\[fortran-comment-region] at start of each \
191 :group 'fortran-comment)
193 (defcustom fortran-electric-line-number t
194 "*Non-nil causes line number digits to be moved to the correct \
199 (defvar fortran-column-ruler-fixed
200 "0 4 6 10 20 30 40 5\
202 \[ ]|{ | | | | | | | | \
204 "String displayed above current line by \\[fortran-column-ruler].
205 This variable used in fixed format mode.")
207 (defvar fortran-column-ruler-tab
210 \[ ]| { | | | | | | | | \
212 "String displayed above current line by \\[fortran-column-ruler].
213 This variable used in TAB format mode.")
215 (defvar fortran-analyze-depth 100
216 "Number of lines to scan to determine whether to use fixed or TAB \
219 (defcustom fortran-break-before-delimiters t
220 "*Non-nil causes filling to break lines before delimiters."
224 (defvar fortran-mode-syntax-table
225 (let ((table (make-syntax-table)))
226 ;; We might like `;' to be punctuation (g77 multi-statement
227 ;; lines), but that screws abbrevs.
228 (modify-syntax-entry ?\; "w" table)
229 (modify-syntax-entry ?\r " " table)
230 (modify-syntax-entry ?+ "." table)
231 (modify-syntax-entry ?- "." table)
232 (modify-syntax-entry ?= "." table)
233 (modify-syntax-entry ?* "." table)
234 (modify-syntax-entry ?/ "." table)
235 (modify-syntax-entry ?\' "\"" table)
236 (modify-syntax-entry ?\" "\"" table)
237 ;; Consistent with GNU Fortran -- see the manual.
238 (modify-syntax-entry ?\\ "\\" table)
239 ;; This might be better as punctuation, as for C, but this way you
240 ;; can treat floating-point numbers as symbols.
241 (modify-syntax-entry ?. "_" table) ; e.g. `a.ne.b'
242 (modify-syntax-entry ?_ "_" table)
243 (modify-syntax-entry ?$ "_" table) ; esp. VMSisms
244 (modify-syntax-entry ?\! "<" table)
245 (modify-syntax-entry ?\n ">" table)
247 "Syntax table in use in Fortran mode buffers.")
249 ;; Comments are real pain in Fortran because there is no way to
250 ;; represent the standard comment syntax in an Emacs syntax table.
251 ;; (We can do so for F90-style). Therefore an unmatched quote in a
252 ;; standard comment will throw fontification off on the wrong track.
253 ;; So we do syntactic fontification with regexps.
255 (defvar fortran-font-lock-keywords-1 nil
256 "Subdued level highlighting for Fortran mode.")
258 (defvar fortran-font-lock-keywords-2 nil
259 "Medium level highlighting for Fortran mode.")
261 (defvar fortran-font-lock-keywords-3 nil
262 "Gaudy level highlighting for Fortran mode.")
264 (defvar fortran-font-lock-syntactic-keywords nil
265 "`font-lock-syntactic-keywords' for Fortran.
266 These get fixed-format comments fontified.")
268 (let ((comment-chars "cd\\*") ; `d' for `debugging' comments
271 (let ((re (regexp-opt
273 '("character" "byte" "integer" "logical"
274 "none" "real" "complex"
275 "double precision" "double complex"))
276 (structured-types '("structure" "union" "map"))
277 (other-types '("record" "dimension"
278 "parameter" "common" "save"
279 "external" "intrinsic" "data"
282 (mapcar (lambda (x) (concat "implicit " x))
285 (mapcar (lambda (x) (concat "end " x))
289 ;; In the optimized regexp above, replace spaces by a
290 ;; regexp for optional whitespace; regexp-opt would have
292 (mapconcat #'identity (split-string re) "[ \t]*"))))
295 (regexp-opt '("continue" "format" "end" "enddo" "if" "then"
296 "else" "endif" "elseif" "while" "inquire" "stop"
297 "return" "include" "open" "close" "read" "write"
298 "format" "print" "select" "case" "cycle" "exit"
299 "rewind" "backspace"))))
302 (regexp-opt '("and" "or" "not" "lt" "le" "eq" "ge" "gt" "ne"
304 (setq fortran-font-lock-syntactic-keywords
305 ;; Fixed format comments. (!-style handled normally.)
307 (list (concat "^[" comment-chars "]") 0 '(11))
308 (list (concat "^[^" comment-chars "\t\n]" ".\\{71\\}"
311 (setq fortran-font-lock-keywords-1
313 ;; Program, subroutine and function declarations, plus calls.
314 (list (concat "\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|"
315 "program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?")
316 '(1 font-lock-keyword-face)
317 '(2 font-lock-function-name-face nil t))))
318 (setq fortran-font-lock-keywords-2
319 (append fortran-font-lock-keywords-1
321 ;; Fontify all type specifiers (must be first; see below).
322 (cons (concat "\\<\\(" fortran-type-types "\\)\\>")
323 'font-lock-type-face)
324 ;; Fontify all builtin keywords (except logical, do
325 ;; and goto; see below).
326 (concat "\\<\\(" fortran-keywords "\\)\\>")
327 ;; Fontify all builtin operators.
328 (concat "\\.\\(" fortran-logicals "\\)\\.")
329 ;; Fontify do/goto keywords and targets, and goto tags.
330 (list "\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
331 '(1 font-lock-keyword-face)
332 '(2 font-lock-constant-face nil t))
333 (cons "^ *\\([0-9]+\\)" 'font-lock-constant-face))))
334 (setq fortran-font-lock-keywords-3
336 ;; The list `fortran-font-lock-keywords-1'.
337 fortran-font-lock-keywords-1
338 ;; Fontify all type specifiers plus their declared items.
340 (list (concat "\\<\\(" fortran-type-types "\\)\\>[ \t(/]*\\(*\\)?")
341 ;; Fontify the type specifier.
342 '(1 font-lock-type-face)
343 ;; Fontify each declaration item (or just the /.../ block name).
344 `(font-lock-match-c-style-declaration-item-and-skip-to-next
345 ;; Start after any *(...) expression.
347 (and (and (match-beginning ,(+ 2 (regexp-opt-depth
348 fortran-type-types)))
352 ;; No need to clean up.
354 ;; Fontify as a variable name, functions are
355 ;; fontified elsewhere.
356 (1 font-lock-variable-name-face nil t))))
357 ;; Things extra to `fortran-font-lock-keywords-3'
358 ;; (must be done first).
360 ;; Fontify goto-like `err=label'/`end=label' in read/write
362 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
363 (1 font-lock-keyword-face) (4 font-lock-constant-face nil t))
364 ;; Highlight standard continuation character and in a
365 ;; TAB-formatted line.
366 '("^ \\([^ 0]\\)" 1 font-lock-string-face)
367 '("^\t\\([1-9]\\)" 1 font-lock-string-face))
370 '("^# *[a-z]+" . font-lock-keyword-face))
371 ;; The list `fortran-font-lock-keywords-2' less that for types
373 (cdr (nthcdr (length fortran-font-lock-keywords-1)
374 fortran-font-lock-keywords-2)))))
376 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
377 "Default expressions to highlight in Fortran mode.")
379 (defvar fortran-imenu-generic-expression
380 ;; These patterns could be confused by sequence nos. in cols 72+ and
381 ;; don't allow continuations everywhere.
385 ;; [This will be fooled by `end function' allowed by G77. Also,
386 ;; it assumes sensible whitespace is employed.]
388 ;; leading whitespace:
390 ;; function declaration with optional type, e.g. `real',
391 ;; `real*4', character(*), `double precision':
392 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
393 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
394 ;; Possible statement continuation:
395 "[ \t" fortran-continuation-string "]+"
396 ;; Variable to index:
399 ;; Un-named block data
400 (list nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
401 "Imenu generic expression for `imenu-default-create-index-function'.")
403 (defvar fortran-mode-map
404 (let ((map (make-sparse-keymap)))
405 (define-key map ";" 'fortran-abbrev-start)
406 (define-key map "\C-c;" 'fortran-comment-region)
407 (define-key map "\M-;" 'fortran-indent-comment)
408 (define-key map "\M-\n" 'fortran-split-line)
409 (define-key map "\M-\C-q" 'fortran-indent-subprogram)
410 (define-key map "\C-c\C-w" 'fortran-window-create-momentarily)
411 (define-key map "\C-c\C-r" 'fortran-column-ruler)
412 (define-key map "\C-c\C-p" 'fortran-previous-statement)
413 (define-key map "\C-c\C-n" 'fortran-next-statement)
414 (define-key map "\C-c\C-d" 'fortran-join-line) ; like f90
415 (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation
416 (define-key map "0" 'fortran-electric-line-number)
417 (define-key map "1" 'fortran-electric-line-number)
418 (define-key map "2" 'fortran-electric-line-number)
419 (define-key map "3" 'fortran-electric-line-number)
420 (define-key map "4" 'fortran-electric-line-number)
421 (define-key map "5" 'fortran-electric-line-number)
422 (define-key map "6" 'fortran-electric-line-number)
423 (define-key map "7" 'fortran-electric-line-number)
424 (define-key map "8" 'fortran-electric-line-number)
425 (define-key map "9" 'fortran-electric-line-number)
429 fortran-mode-menu map ""
431 ["Manual" (info "(emacs)Fortran")]
433 ,(custom-menu-create 'fortran)
435 ["Save" Custom-save t]
436 ["Reset to Current" Custom-reset-current t]
437 ["Reset to Saved" Custom-reset-saved t]
438 ["Reset to Standard Settings" Custom-reset-standard t])
440 ["Toggle Auto-fill" auto-fill-mode :style toggle
441 :selected auto-fill-function]
442 ["Toggle abbrev-mode" abbrev-mode :style toggle :selected abbrev-mode]
444 ["Comment-out Region" fortran-comment-region mark-active]
445 ["Uncomment-out region"
446 (fortran-comment-region (region-beginning) (region-end) 1)
448 ["Indent Region" indent-region mark-active]
449 ["Indent Subprogram" fortran-indent-subprogram t]
451 ["Beginning of Subprogram" fortran-beginning-of-subprogram t]
452 ["End of Subprogram" fortran-end-of-subprogram t]
454 ["Subprogram" mark-defun t]
455 ["IF Block" fortran-mark-if t]
456 ["DO Block" fortran-mark-do t])
457 ["Narrow to Subprogram" narrow-to-defun t]
460 ["Temporary column ruler" fortran-column-ruler t]
461 ["72-column window" fortran-window-create t]
463 (enlarge-window-horizontally (- (frame-width) (window-width)))
464 (< (window-width) (frame-width))]
465 ["Momentary 72-column window" fortran-window-create-momentarily t]
467 ["Break Line at Point" fortran-split-line t]
468 ["Join Line" fortran-join-line t]
469 ["Fill Statement/Comment" fill-paragraph t]
472 imenu-add-menubar-index (not (and (boundp 'imenu--index-alist)
473 imenu--index-alist))]))
475 "Keymap used in Fortran mode.")
477 (defvar fortran-mode-abbrev-table
478 (let ((ac abbrevs-changed))
479 (define-abbrev-table 'fortran-mode-abbrev-table ())
480 (define-abbrev fortran-mode-abbrev-table ";au" "automatic" nil)
481 (define-abbrev fortran-mode-abbrev-table ";b" "byte" nil)
482 (define-abbrev fortran-mode-abbrev-table ";bd" "block data" nil)
483 (define-abbrev fortran-mode-abbrev-table ";ch" "character" nil)
484 (define-abbrev fortran-mode-abbrev-table ";cl" "close" nil)
485 (define-abbrev fortran-mode-abbrev-table ";c" "continue" nil)
486 (define-abbrev fortran-mode-abbrev-table ";cm" "common" nil)
487 (define-abbrev fortran-mode-abbrev-table ";cx" "complex" nil)
488 (define-abbrev fortran-mode-abbrev-table ";df" "define" nil)
489 (define-abbrev fortran-mode-abbrev-table ";di" "dimension" nil)
490 (define-abbrev fortran-mode-abbrev-table ";do" "double" nil)
491 (define-abbrev fortran-mode-abbrev-table ";dc" "double complex" nil)
492 (define-abbrev fortran-mode-abbrev-table ";dp" "double precision" nil)
493 (define-abbrev fortran-mode-abbrev-table ";dw" "do while" nil)
494 (define-abbrev fortran-mode-abbrev-table ";e" "else" nil)
495 (define-abbrev fortran-mode-abbrev-table ";ed" "enddo" nil)
496 (define-abbrev fortran-mode-abbrev-table ";el" "elseif" nil)
497 (define-abbrev fortran-mode-abbrev-table ";en" "endif" nil)
498 (define-abbrev fortran-mode-abbrev-table ";eq" "equivalence" nil)
499 (define-abbrev fortran-mode-abbrev-table ";ew" "endwhere" nil)
500 (define-abbrev fortran-mode-abbrev-table ";ex" "external" nil)
501 (define-abbrev fortran-mode-abbrev-table ";ey" "entry" nil)
502 (define-abbrev fortran-mode-abbrev-table ";f" "format" nil)
503 (define-abbrev fortran-mode-abbrev-table ";fa" ".false." nil)
504 (define-abbrev fortran-mode-abbrev-table ";fu" "function" nil)
505 (define-abbrev fortran-mode-abbrev-table ";g" "goto" nil)
506 (define-abbrev fortran-mode-abbrev-table ";im" "implicit" nil)
507 (define-abbrev fortran-mode-abbrev-table ";ib" "implicit byte" nil)
508 (define-abbrev fortran-mode-abbrev-table ";ic" "implicit complex" nil)
509 (define-abbrev fortran-mode-abbrev-table ";ich" "implicit character" nil)
510 (define-abbrev fortran-mode-abbrev-table ";ii" "implicit integer" nil)
511 (define-abbrev fortran-mode-abbrev-table ";il" "implicit logical" nil)
512 (define-abbrev fortran-mode-abbrev-table ";ir" "implicit real" nil)
513 (define-abbrev fortran-mode-abbrev-table ";inc" "include" nil)
514 (define-abbrev fortran-mode-abbrev-table ";in" "integer" nil)
515 (define-abbrev fortran-mode-abbrev-table ";intr" "intrinsic" nil)
516 (define-abbrev fortran-mode-abbrev-table ";l" "logical" nil)
517 (define-abbrev fortran-mode-abbrev-table ";n" "namelist" nil)
518 (define-abbrev fortran-mode-abbrev-table ";o" "open" nil) ; was ;op
519 (define-abbrev fortran-mode-abbrev-table ";pa" "parameter" nil)
520 (define-abbrev fortran-mode-abbrev-table ";pr" "program" nil)
521 (define-abbrev fortran-mode-abbrev-table ";ps" "pause" nil)
522 (define-abbrev fortran-mode-abbrev-table ";p" "print" nil)
523 (define-abbrev fortran-mode-abbrev-table ";rc" "record" nil)
524 (define-abbrev fortran-mode-abbrev-table ";re" "real" nil)
525 (define-abbrev fortran-mode-abbrev-table ";r" "read" nil)
526 (define-abbrev fortran-mode-abbrev-table ";rt" "return" nil)
527 (define-abbrev fortran-mode-abbrev-table ";rw" "rewind" nil)
528 (define-abbrev fortran-mode-abbrev-table ";s" "stop" nil)
529 (define-abbrev fortran-mode-abbrev-table ";sa" "save" nil)
530 (define-abbrev fortran-mode-abbrev-table ";st" "structure" nil)
531 (define-abbrev fortran-mode-abbrev-table ";sc" "static" nil)
532 (define-abbrev fortran-mode-abbrev-table ";su" "subroutine" nil)
533 (define-abbrev fortran-mode-abbrev-table ";tr" ".true." nil)
534 (define-abbrev fortran-mode-abbrev-table ";ty" "type" nil)
535 (define-abbrev fortran-mode-abbrev-table ";vo" "volatile" nil)
536 (define-abbrev fortran-mode-abbrev-table ";w" "write" nil)
537 (define-abbrev fortran-mode-abbrev-table ";wh" "where" nil)
538 (setq abbrevs-changed ac)
539 fortran-mode-abbrev-table))
541 (eval-when-compile ; silence compiler
542 (defvar imenu-case-fold-search)
543 (defvar imenu-syntax-alist))
545 (defcustom fortran-mode-hook nil
546 "Hook run by Fortran mode."
551 (defun fortran-mode ()
552 "Major mode for editing Fortran code.
553 \\[fortran-indent-line] indents the current Fortran line correctly.
554 DO statements must not share a common CONTINUE.
556 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for
562 Variables controlling indentation style and extra features:
565 If you want to use comments starting with `!',
566 set this to the string \"!\".
568 Extra indentation within do blocks. (default 3)
570 Extra indentation within if blocks. (default 3)
571 `fortran-structure-indent'
572 Extra indentation within structure, union, map and interface blocks.
574 `fortran-continuation-indent'
575 Extra indentation applied to continuation statements. (default 5)
576 `fortran-comment-line-extra-indent'
577 Amount of extra indentation for text within full-line comments. (default 0)
578 `fortran-comment-indent-style'
579 nil means don't change indentation of text in full-line comments,
580 fixed means indent that text at `fortran-comment-line-extra-indent' beyond
581 the value of `fortran-minimum-statement-indent-fixed' (for fixed
582 format continuation style) or `fortran-minimum-statement-indent-tab'
583 (for TAB format continuation style).
584 relative means indent at `fortran-comment-line-extra-indent' beyond the
585 indentation for a line of code.
587 `fortran-comment-indent-char'
588 Single-character string to be inserted instead of space for
589 full-line comment indentation. (default \" \")
590 `fortran-minimum-statement-indent-fixed'
591 Minimum indentation for Fortran statements in fixed format mode. (def.6)
592 `fortran-minimum-statement-indent-tab'
593 Minimum indentation for Fortran statements in TAB format mode. (default 9)
594 `fortran-line-number-indent'
595 Maximum indentation for line numbers. A line number will get
596 less than this much indentation if necessary to avoid reaching
597 column 5. (default 1)
598 `fortran-check-all-num-for-matching-do'
599 Non-nil causes all numbered lines to be treated as possible \"continue\"
600 statements. (default nil)
601 `fortran-blink-matching-if'
602 Non-nil causes \\[fortran-indent-line] on an ENDIF statement to blink on
603 matching IF. Also, from an ENDDO statement, blink on matching DO [WHILE]
604 statement. (default nil)
605 `fortran-continuation-string'
606 Single-character string to be inserted in column 5 of a continuation
607 line. (default \"$\")
608 `fortran-comment-region'
609 String inserted by \\[fortran-comment-region] at start of each line in
610 region. (default \"c$$$\")
611 `fortran-electric-line-number'
612 Non-nil causes line number digits to be moved to the correct column
613 as typed. (default t)
614 `fortran-break-before-delimiters'
615 Non-nil causes lines to be broken before delimiters.
618 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
619 with no args, if that value is non-nil."
621 (kill-all-local-variables)
622 (setq local-abbrev-table fortran-mode-abbrev-table)
623 (set-syntax-table fortran-mode-syntax-table)
624 ;; Font Lock mode support.
625 (make-local-variable 'font-lock-defaults)
626 (setq font-lock-defaults '((fortran-font-lock-keywords
627 fortran-font-lock-keywords-1
628 fortran-font-lock-keywords-2
629 fortran-font-lock-keywords-3)
630 nil t ((?/ . "$/") ("_$" . "w"))
631 fortran-beginning-of-subprogram))
632 (set (make-local-variable 'font-lock-syntactic-keywords)
633 fortran-font-lock-syntactic-keywords)
634 (make-local-variable 'fortran-break-before-delimiters)
635 (setq fortran-break-before-delimiters t)
636 (make-local-variable 'indent-line-function)
637 (setq indent-line-function 'fortran-indent-line)
638 (make-local-variable 'comment-indent-function)
639 (setq comment-indent-function 'fortran-comment-indent)
640 (make-local-variable 'comment-start-skip)
641 (setq comment-start-skip "![ \t]*")
642 (make-local-variable 'comment-start)
643 (setq comment-start fortran-comment-line-start)
644 (make-local-variable 'require-final-newline)
645 (setq require-final-newline t)
646 (make-local-variable 'abbrev-all-caps)
647 (setq abbrev-all-caps t)
648 (make-local-variable 'indent-tabs-mode)
649 (setq indent-tabs-mode nil)
650 ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
651 (set (make-local-variable 'fill-column) 72)
652 (use-local-map fortran-mode-map)
653 (setq mode-name "Fortran")
654 (setq major-mode 'fortran-mode)
655 (make-local-variable 'fortran-comment-line-extra-indent)
656 (make-local-variable 'fortran-minimum-statement-indent-fixed)
657 (make-local-variable 'fortran-minimum-statement-indent-tab)
658 (make-local-variable 'fortran-column-ruler-fixed)
659 (make-local-variable 'fortran-column-ruler-tab)
660 (setq fortran-tab-mode-string " TAB-format")
661 (setq indent-tabs-mode (fortran-analyze-file-format))
662 (setq imenu-case-fold-search t)
663 (setq imenu-generic-expression fortran-imenu-generic-expression)
664 (setq imenu-syntax-alist '(("_$" . "w")))
665 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
666 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
667 (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
668 (set (make-local-variable 'indent-region-function)
670 (let (fortran-blink-matching-if ; avoid blinking delay
671 indent-region-function)
672 (indent-region start end nil))))
673 (set (make-local-variable 'beginning-of-defun-function)
674 #'fortran-beginning-of-subprogram)
675 (set (make-local-variable 'end-of-defun-function)
676 #'fortran-end-of-subprogram)
677 (set (make-local-variable 'add-log-current-defun-function)
678 #'fortran-current-defun)
679 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
680 (run-hooks 'fortran-mode-hook))
682 (defsubst fortran-comment-indent ()
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 fortran-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)
705 (fortran-comment-indent)))
706 (progn (delete-horizontal-space)
707 (indent-to (fortran-comment-indent)))))
708 (end-of-line))) ; otherwise goto end of line or sth else?
709 ;; No existing comment.
710 ;; If side-by-side comments are defined, insert one,
711 ;; unless line is now blank.
712 ((and comment-start (not (looking-at "^[ \t]*$")))
714 (delete-horizontal-space)
715 (indent-to (fortran-comment-indent))
716 (insert comment-start))
717 ;; Else insert separate-line comment, making a new line if nec.
719 (if (looking-at "^[ \t]*$")
720 (delete-horizontal-space)
724 (insert fortran-comment-line-start)
725 (insert-char (if (stringp fortran-comment-indent-char)
726 (aref fortran-comment-indent-char 0)
727 fortran-comment-indent-char)
728 (- (fortran-calculate-indent) (current-column))))))
730 (defun fortran-comment-region (beg-region end-region arg)
731 "Comments every line in the region.
732 Puts `fortran-comment-region' at the beginning of every line in the region.
733 BEG-REGION and END-REGION are args which specify the region boundaries.
734 With non-nil ARG, uncomments the region."
735 (interactive "*r\nP")
736 (let ((end-region-mark (copy-marker end-region))
737 (save-point (point-marker)))
738 (goto-char beg-region)
740 (if (not arg) ;comment the region
741 (progn (insert fortran-comment-region)
742 (while (and (= (forward-line 1) 0)
743 (< (point) end-region-mark))
744 (insert fortran-comment-region)))
745 (let ((com (regexp-quote fortran-comment-region))) ;uncomment the region
747 (delete-region (point) (match-end 0)))
748 (while (and (= (forward-line 1) 0)
749 (< (point) end-region-mark))
751 (delete-region (point) (match-end 0))))))
752 (goto-char save-point)
753 (set-marker end-region-mark nil)
754 (set-marker save-point nil)))
756 (defun fortran-abbrev-start ()
757 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
758 Any other key combination is executed normally."
761 (insert last-command-char)
762 (if (or (eq (setq c (read-event)) ??) ;insert char if not equal to `?'
764 (fortran-abbrev-help)
765 (setq unread-command-events (list c)))))
767 (defun fortran-abbrev-help ()
768 "List the currently defined abbrevs in Fortran mode."
770 (message "Listing abbrev table...")
771 (display-buffer (fortran-prepare-abbrev-list-buffer))
772 (message "Listing abbrev table...done"))
774 (defun fortran-prepare-abbrev-list-buffer ()
776 (set-buffer (get-buffer-create "*Abbrevs*"))
778 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
779 (goto-char (point-min))
780 (set-buffer-modified-p nil)
782 (get-buffer-create "*Abbrevs*"))
784 (defun fortran-column-ruler ()
785 "Insert a column ruler momentarily above current line, till next keystroke.
786 The ruler is defined by the value of `fortran-column-ruler-fixed' when in fixed
787 format mode, and `fortran-column-ruler-tab' when in TAB format mode.
788 The key typed is executed unless it is SPC."
790 (momentary-string-display
792 fortran-column-ruler-tab
793 fortran-column-ruler-fixed)
796 (if (eq (window-start (selected-window))
797 (window-point (selected-window)))
798 (line-beginning-position 2)
800 nil "Type SPC or any command to erase ruler."))
802 (defun fortran-window-create ()
803 "Make the window 72 columns wide.
804 See also `fortran-window-create-momentarily'."
806 (let ((window-min-width 2))
807 (if (< (window-width) (frame-width))
808 (enlarge-window-horizontally (- (frame-width)
810 (let* ((window-edges (window-edges))
811 (scroll-bar-width (- (nth 2 window-edges)
814 (split-window-horizontally (+ 72 scroll-bar-width)))
816 (switch-to-buffer " fortran-window-extra" t)
817 (select-window (previous-window))))
819 (defun fortran-window-create-momentarily (&optional arg)
820 "Momentarily make the window 72 columns wide.
821 Optional ARG non-nil and non-unity disables the momentary feature.
822 See also `fortran-window-create'."
826 (save-window-excursion
829 (fortran-window-create)
830 (error (error "No room for Fortran window")))
831 (message "Type SPC to continue editing.")
832 (let ((char (read-event)))
833 (or (equal char (string-to-char " "))
834 (setq unread-command-events (list char))))))
835 (fortran-window-create)))
837 (defun fortran-split-line ()
838 "Break line at point and insert continuation marker and alignment."
840 (delete-horizontal-space)
843 (looking-at fortran-comment-line-start-skip))
844 (insert ?\n fortran-comment-line-start ? )
846 (insert ?\n ?\t (fortran-numerical-continuation-char))
847 (insert "\n " fortran-continuation-string))) ; Space after \n important
848 (fortran-indent-line)) ; when the cont string is C, c or *.
850 (defun fortran-remove-continuation ()
851 (if (looking-at "\\( [^ 0\n]\\|\t[1-9]\\|&\\)")
852 (progn (replace-match "")
856 (defun fortran-join-line (arg)
857 "Join current line to the previous one and re-indent.
858 With a prefix argument, repeat this operation that many times.
859 If the prefix argument ARG is negative, join the next -ARG lines.
860 Continuation lines are correctly handled."
866 (while (not (zerop arg))
868 (or (fortran-remove-continuation)
869 (delete-indentation))
871 (fortran-indent-line)))
873 (defun fortran-numerical-continuation-char ()
874 "Return a digit for tab-digit style of continuation lines.
875 If, previous line is a tab-digit continuation line, returns that digit
876 plus one. Otherwise return 1. Zero not allowed."
879 (if (looking-at "\t[1-9]")
880 (+ ?1 (% (- (char-after (+ (point) 1)) ?0) 9))
883 (put 'fortran-electric-line-number 'delete-selection t)
884 (defun fortran-electric-line-number (arg)
885 "Self insert, but if part of a Fortran line number indent it automatically.
886 Auto-indent does not happen if a numeric ARG is used."
888 (if (or arg (not fortran-electric-line-number))
890 (self-insert-command (prefix-numeric-value arg))
891 (self-insert-command 1))
892 (if (or (and (= 5 (current-column))
895 (looking-at " \\{5\\}"))) ;In col 5 with only spaces to left.
896 (and (= (if indent-tabs-mode
897 fortran-minimum-statement-indent-tab
898 fortran-minimum-statement-indent-fixed) (current-column))
899 (eq ?\t (char-after (line-beginning-position))) ;In col 8
900 ; with a single tab to the left.
901 (not (or (eq last-command 'fortran-indent-line)
903 'fortran-indent-new-line))))
905 (re-search-backward "[^ \t0-9]"
906 (line-beginning-position)
907 t)) ;not a line number
908 (looking-at "[0-9]")) ;within a line number
909 (self-insert-command (prefix-numeric-value arg))
910 (skip-chars-backward " \t")
911 (insert last-command-char)
912 (fortran-indent-line))))
914 (defvar fortran-end-prog-re1
916 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
917 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?")
918 (defvar fortran-end-prog-re
919 (concat "^[ \t0-9]*" fortran-end-prog-re1)
920 "Regexp possibly marking subprogram end.")
922 (defun fortran-check-end-prog-re ()
923 "Check a preliminary match against `fortran-end-prog-re'."
924 ;; Having got a possible match for the subprogram end, we need a
925 ;; match of whitespace, avoiding possible column 73+ stuff.
927 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
928 (buffer-substring (match-end 0)
929 (min (line-end-position)
930 (+ 72 (line-beginning-position)))))))
932 ;; Note that you can't just check backwards for `subroutine' &c in
933 ;; case of un-marked main programs not at the start of the file.
934 (defun fortran-beginning-of-subprogram ()
935 "Move point to the beginning of the current Fortran subprogram."
938 (let ((case-fold-search t))
939 (beginning-of-line -1)
941 (while (re-search-backward fortran-end-prog-re nil 'move)
942 (if (fortran-check-end-prog-re)
946 (defun fortran-end-of-subprogram ()
947 "Move point to the end of the current Fortran subprogram."
950 (let ((case-fold-search t))
951 (if (save-excursion ; on END
953 (and (looking-at fortran-end-prog-re)
954 (fortran-check-end-prog-re)))
956 (beginning-of-line 2)
958 (while (re-search-forward fortran-end-prog-re nil 'move)
959 (if (fortran-check-end-prog-re)
961 (goto-char (match-beginning 0))
964 (defun fortran-previous-statement ()
965 "Move point to beginning of the previous Fortran statement.
966 Returns `first-statement' if that statement is the first
967 non-comment Fortran statement in the file, and nil otherwise."
969 (let (not-first-statement continue-test)
973 (not (looking-at fortran-comment-line-start-skip))
976 (regexp-quote fortran-continuation-string)))
977 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
978 (while (and (setq not-first-statement (= (forward-line -1) 0))
979 (or (looking-at fortran-comment-line-start-skip)
980 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
981 (looking-at (concat "[ \t]*" comment-start-skip)))))
982 (cond ((and continue-test
983 (not not-first-statement))
984 (message "Incomplete continuation statement."))
986 (fortran-previous-statement))
987 ((not not-first-statement)
990 (defun fortran-next-statement ()
991 "Move point to beginning of the next Fortran statement.
992 Returns `last-statement' if that statement is the last
993 non-comment Fortran statement in the file, and nil otherwise."
995 (let (not-last-statement)
997 (while (and (setq not-last-statement
998 (and (= (forward-line 1) 0)
1000 (or (looking-at fortran-comment-line-start-skip)
1001 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1002 (looking-at (concat "[ \t]*" comment-start-skip)))))
1003 (if (not not-last-statement)
1006 (defun fortran-blink-match (regex keyword find-begin)
1007 "From a line matching REGEX, blink matching KEYWORD statement line.
1008 Use function FIND-BEGIN to match it."
1009 (let ((top-of-window (window-start))
1011 (case-fold-search t)
1016 (skip-chars-forward " \t0-9")
1019 (if (not (setq matching (funcall find-begin)))
1020 (setq message (concat "No matching " keyword "."))
1021 (if (< matching top-of-window)
1023 (goto-char matching)
1027 (buffer-substring (point)
1028 (line-end-position)))))))
1030 (message "%s" message)
1031 (goto-char matching)
1033 (goto-char end-point))))))
1035 (defun fortran-blink-matching-if ()
1036 "From an ENDIF or ELSE statement, blink the matching IF statement."
1037 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1038 "if" #'fortran-beginning-if))
1040 (defun fortran-blink-matching-do ()
1041 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1042 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
1044 (defun fortran-mark-do ()
1045 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1046 The marks are pushed."
1048 (let (enddo-point do-point)
1049 (if (setq enddo-point (fortran-end-do))
1050 (if (not (setq do-point (fortran-beginning-do)))
1051 (message "No matching do.")
1052 (goto-char enddo-point)
1054 (goto-char do-point)))))
1056 (defun fortran-end-do ()
1057 "Search forward for first unmatched ENDDO.
1058 Return point or nil."
1059 (let ((case-fold-search t))
1060 (if (save-excursion (beginning-of-line)
1061 (skip-chars-forward " \t0-9")
1062 (looking-at "end[ \t]*do\\b"))
1068 (while (and (not (= count 0))
1069 (not (eq (fortran-next-statement) 'last-statement))
1070 ;; Keep local to subprogram
1071 (not (and (looking-at fortran-end-prog-re)
1072 (fortran-check-end-prog-re))))
1073 (skip-chars-forward " \t0-9")
1074 (cond ((looking-at "end[ \t]*do\\b")
1075 (setq count (1- count)))
1077 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1078 (setq count (+ count 1)))))
1080 ;; All pairs accounted for.
1083 (defun fortran-beginning-do ()
1084 "Search backwards for first unmatched DO [WHILE].
1085 Return point or nil."
1086 (let ((case-fold-search t))
1089 (skip-chars-forward " \t0-9")
1090 (looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+"))
1096 (while (and (not (= count 0))
1097 (not (eq (fortran-previous-statement) 'first-statement))
1098 ;; Keep local to subprogram
1099 (not (and (looking-at fortran-end-prog-re)
1100 (fortran-check-end-prog-re))))
1101 (skip-chars-forward " \t0-9")
1103 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[0-9]")
1104 (setq count (1- count)))
1105 ((looking-at "end[ \t]*do\\b")
1106 (setq count (1+ count)))))
1108 ;; All pairs accounted for.
1111 (defun fortran-mark-if ()
1112 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1113 The marks are pushed."
1115 (let (endif-point if-point)
1116 (if (setq endif-point (fortran-end-if))
1117 (if (not (setq if-point (fortran-beginning-if)))
1118 (message "No matching if.")
1119 ;; Set mark, move point.
1120 (goto-char endif-point)
1122 (goto-char if-point)))))
1124 (defvar fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1126 (defun fortran-end-if ()
1127 "Search forwards for first unmatched ENDIF.
1128 Return point or nil."
1129 (let ((case-fold-search t))
1130 (if (save-excursion (beginning-of-line)
1131 (skip-chars-forward " \t0-9")
1132 (looking-at "end[ \t]*if\\b"))
1135 ;; Search for one. The point has been already been moved to first
1136 ;; letter on line but this should not cause troubles.
1139 (while (and (not (= count 0))
1140 (not (eq (fortran-next-statement) 'last-statement))
1141 ;; Keep local to subprogram.
1142 (not (and (looking-at fortran-end-prog-re)
1143 (fortran-check-end-prog-re))))
1144 (skip-chars-forward " \t0-9")
1145 (cond ((looking-at "end[ \t]*if\\b")
1146 (setq count (- count 1)))
1147 ((looking-at fortran-if-start-re)
1150 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1151 (let (then-test) ; Multi-line if-then.
1154 (= (forward-line 1) 0)
1155 ;; Search forward for then.
1156 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1160 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1162 (setq count (+ count 1)))))))
1164 ;; All pairs accounted for.
1167 (defun fortran-beginning-if ()
1168 "Search backwards for first unmatched IF-THEN.
1169 Return point or nil."
1170 (let ((case-fold-search t))
1172 ;; May be sitting on multi-line if-then statement, first
1173 ;; move to beginning of current statement. Note:
1174 ;; `fortran-previous-statement' moves to previous statement
1175 ;; *unless* current statement is first one. Only move
1176 ;; forward if not first-statement.
1177 (if (not (eq (fortran-previous-statement) 'first-statement))
1178 (fortran-next-statement))
1179 (skip-chars-forward " \t0-9")
1181 (looking-at fortran-if-start-re)
1183 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1184 ;; Multi-line if-then.
1187 (and (= (forward-line 1) 0)
1188 ;; Search forward for then.
1189 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1193 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1200 (while (and (not (= count 0))
1201 (not (eq (fortran-previous-statement) 'first-statement))
1202 ;; Keep local to subprogram.
1203 (not (and (looking-at fortran-end-prog-re)
1204 (fortran-check-end-prog-re))))
1205 (skip-chars-forward " \t0-9")
1206 (cond ((looking-at fortran-if-start-re)
1209 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1210 (let (then-test) ; Multi-line if-then.
1213 (= (forward-line 1) 0)
1214 ;; Search forward for then.
1215 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1219 (concat ".*then\\b[ \t]*"
1220 "[^ \t(=a-z0-9]"))))))
1222 (setq count (- count 1)))))
1223 ((looking-at "end[ \t]*if\\b")
1224 (setq count (+ count 1)))))
1226 ;; All pairs accounted for.
1229 (defun fortran-indent-line ()
1230 "Indent current Fortran line based on its contents and on previous lines."
1232 (let ((cfi (fortran-calculate-indent)))
1235 (if (or (not (= cfi (fortran-current-line-indentation)))
1236 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1237 (not (fortran-line-number-indented-correctly-p))))
1238 (fortran-indent-to-column cfi)
1240 (if (and (not (looking-at fortran-comment-line-start-skip))
1241 (fortran-find-comment-start-skip))
1242 (fortran-indent-comment))))
1243 ;; Never leave point in left margin.
1244 (if (< (current-column) cfi)
1245 (move-to-column cfi))
1246 (if (and auto-fill-function
1247 (> (save-excursion (end-of-line) (current-column))
1252 (if fortran-blink-matching-if
1254 (fortran-blink-matching-if)
1255 (fortran-blink-matching-do)))))
1257 (defun fortran-auto-fill ()
1258 (if (> (current-column) (current-fill-column))
1259 (let ((cfi (fortran-calculate-indent)))
1262 (if (or (not (= cfi (fortran-current-line-indentation)))
1263 (and (re-search-forward "^[ \t]*[0-9]+"
1265 (not (fortran-line-number-indented-correctly-p))))
1266 (fortran-indent-to-column cfi)
1268 (if (and (not (looking-at fortran-comment-line-start-skip))
1269 (fortran-find-comment-start-skip))
1270 (fortran-indent-comment))))
1272 ;; Never leave point in left margin.
1273 (if (< (current-column) cfi)
1274 (move-to-column cfi)))))
1276 ;; Historically this was a separate function which advertised itself
1277 ;; as reindenting but only did so where `most likely to be necessary'.
1278 (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
1280 (defun fortran-indent-subprogram ()
1281 "Properly indent the Fortran subprogram which contains point."
1285 (message "Indenting subprogram...")
1286 (indent-region (point) (mark) nil))
1287 (message "Indenting subprogram...done."))
1289 (defun fortran-calculate-indent ()
1290 "Calculates the Fortran indent column based on previous lines."
1291 (let (icol first-statement (case-fold-search t)
1292 (fortran-minimum-statement-indent
1293 (if indent-tabs-mode
1294 fortran-minimum-statement-indent-tab
1295 fortran-minimum-statement-indent-fixed)))
1297 (setq first-statement (fortran-previous-statement))
1299 (setq icol fortran-minimum-statement-indent)
1301 (if (= (point) (point-min))
1302 (setq icol fortran-minimum-statement-indent)
1303 (setq icol (fortran-current-line-indentation)))
1304 (skip-chars-forward " \t0-9")
1305 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1306 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1307 (let (then-test) ;multi-line if-then
1308 (while (and (= (forward-line 1) 0)
1309 ;;search forward for then
1310 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1311 (not (setq then-test
1314 *[^ \t_$(=a-z0-9]")))))
1316 (setq icol (+ icol fortran-if-indent))))
1317 ((looking-at "else\\(if\\)?\\b")
1318 (setq icol (+ icol fortran-if-indent)))
1319 ((looking-at "select[ \t]*case[ \t](.*)")
1320 (setq icol (+ icol fortran-if-indent)))
1321 ((looking-at "case[ \t]*(.*)")
1322 (setq icol (+ icol fortran-if-indent)))
1323 ((looking-at "case[ \t]*default\\b")
1324 (setq icol (+ icol fortran-if-indent)))
1325 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1326 (setq icol (+ icol fortran-if-indent)))
1327 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1328 (setq icol (+ icol fortran-if-indent)))
1329 ((looking-at "do\\b")
1330 (setq icol (+ icol fortran-do-indent)))
1332 "\\(structure\\|union\\|map\\|interface\\)\
1333 \\b[ \t]*[^ \t=(a-z]")
1334 (setq icol (+ icol fortran-structure-indent)))
1335 ((and (looking-at fortran-end-prog-re1)
1336 (fortran-check-end-prog-re))
1337 ;; Previous END resets indent to minimum
1338 (setq icol fortran-minimum-statement-indent))))))
1341 (cond ((looking-at "[ \t]*$"))
1342 ((looking-at fortran-comment-line-start-skip)
1343 (cond ((eq fortran-comment-indent-style 'relative)
1344 (setq icol (+ icol fortran-comment-line-extra-indent)))
1345 ((eq fortran-comment-indent-style 'fixed)
1346 (setq icol (+ fortran-minimum-statement-indent
1347 fortran-comment-line-extra-indent))))
1348 (setq fortran-minimum-statement-indent 0))
1349 ((or (looking-at (concat "[ \t]*"
1351 fortran-continuation-string)))
1352 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1353 (setq icol (+ icol fortran-continuation-indent)))
1354 ((looking-at "[ \t]*#") ; Check for cpp directive.
1355 (setq fortran-minimum-statement-indent 0 icol 0))
1357 ((and fortran-check-all-num-for-matching-do
1358 (looking-at "[ \t]*[0-9]+")
1359 (fortran-check-for-matching-do))
1360 (setq icol (- icol fortran-do-indent)))
1362 (skip-chars-forward " \t0-9")
1363 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1364 (setq icol (- icol fortran-if-indent)))
1365 ((looking-at "else\\(if\\)?\\b")
1366 (setq icol (- icol fortran-if-indent)))
1367 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1368 (setq icol (- icol fortran-if-indent)))
1369 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1370 (setq icol (- icol fortran-if-indent)))
1371 ((and (looking-at "continue\\b")
1372 (fortran-check-for-matching-do))
1373 (setq icol (- icol fortran-do-indent)))
1374 ((looking-at "end[ \t]*do\\b")
1375 (setq icol (- icol fortran-do-indent)))
1376 ((looking-at "end[ \t]*\
1377 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1378 (setq icol (- icol fortran-structure-indent)))
1379 ((and (looking-at fortran-end-prog-re1)
1380 (fortran-check-end-prog-re)
1381 (not (= icol fortran-minimum-statement-indent)))
1382 (message "Warning: `end' not in column %d. Probably\
1383 an unclosed block." fortran-minimum-statement-indent))))))
1384 (max fortran-minimum-statement-indent icol)))
1386 (defun fortran-current-line-indentation ()
1387 "Indentation of current line, ignoring Fortran line number or continuation.
1388 This is the column position of the first non-whitespace character
1389 aside from the line number and/or column 5/8 line-continuation character.
1390 For comment lines, returns indentation of the first
1391 non-indentation text within the comment."
1394 (cond ((looking-at fortran-comment-line-start-skip)
1395 (goto-char (match-end 0))
1397 (if (stringp fortran-comment-indent-char)
1398 fortran-comment-indent-char
1399 (char-to-string fortran-comment-indent-char))))
1400 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1401 (goto-char (match-end 0)))
1403 ;; Move past line number.
1404 (skip-chars-forward "[ \t0-9]")))
1405 ;; Move past whitespace.
1406 (skip-chars-forward " \t")
1409 (defun fortran-indent-to-column (col)
1410 "Indent current line with spaces to column COL.
1411 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1412 line, and this continuation character is retained on indentation;
1413 2) If `fortran-continuation-string' is the first non-whitespace
1414 character, this is a continuation line;
1415 3) A non-continuation line which has a number as the first
1416 non-whitespace character is a numbered line.
1417 4) A TAB followed by a digit indicates a continuation line."
1420 (if (looking-at fortran-comment-line-start-skip)
1421 (if fortran-comment-indent-style
1422 (let* ((char (if (stringp fortran-comment-indent-char)
1423 (aref fortran-comment-indent-char 0)
1424 fortran-comment-indent-char))
1425 (chars (string ? ?\t char)))
1426 (goto-char (match-end 0))
1427 (skip-chars-backward chars)
1428 (delete-region (point) (progn (skip-chars-forward chars)
1430 (insert-char char (- col (current-column)))))
1431 (if (looking-at "\t[1-9]")
1432 (if indent-tabs-mode
1433 (goto-char (match-end 0))
1436 (insert fortran-continuation-string))
1437 (if (looking-at " \\{5\\}[^ 0\n]")
1438 (if indent-tabs-mode
1439 (progn (delete-char 6)
1440 (insert ?\t (fortran-numerical-continuation-char) 1))
1442 (delete-horizontal-space)
1443 ;; Put line number in columns 0-4
1444 ;; or put continuation character in column 5.
1446 ((looking-at (regexp-quote fortran-continuation-string))
1447 (if indent-tabs-mode
1450 (if indent-tabs-mode
1451 fortran-minimum-statement-indent-tab
1452 fortran-minimum-statement-indent-fixed))
1454 (insert-char (fortran-numerical-continuation-char) 1))
1457 ((looking-at "[0-9]+")
1458 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1459 (if (< extra-space 0)
1460 (message "Warning: line number exceeds 5-digit limit.")
1461 (indent-to (min fortran-line-number-indent extra-space))))
1462 (skip-chars-forward "0-9")))))
1463 ;; Point is now after any continuation character or line number.
1464 ;; Put body of statement where specified.
1465 (delete-horizontal-space)
1467 ;; Indent any comment following code on the same line.
1468 (if (and comment-start-skip
1469 (fortran-find-comment-start-skip))
1470 (progn (goto-char (match-beginning 0))
1471 (if (not (= (current-column)
1472 (fortran-comment-indent)))
1473 (progn (delete-horizontal-space)
1474 (indent-to (fortran-comment-indent)))))))))
1476 (defun fortran-line-number-indented-correctly-p ()
1477 "Return t if current line's line number is correctly indented.
1478 Do not call if there is no line number."
1481 (skip-chars-forward " \t")
1482 (and (<= (current-column) fortran-line-number-indent)
1483 (or (= (current-column) fortran-line-number-indent)
1484 (progn (skip-chars-forward "0-9")
1485 (= (current-column) 5))))))
1487 (defun fortran-check-for-matching-do ()
1488 "When called from a numbered statement, return t if matching DO is found.
1489 Otherwise return nil."
1490 (let ((case-fold-search t)
1494 (if (looking-at "[ \t]*[0-9]+")
1496 (skip-chars-forward " \t")
1497 (skip-chars-forward "0") ;skip past leading zeros
1499 (buffer-substring (point) (progn
1500 (skip-chars-forward "0-9")
1506 (and (re-search-backward
1508 "\\(^[ \t0-9]*do[ \t]*0*"
1509 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1513 (concat "^[ \t0-9]*do[ \t]*0*"
1516 (defun fortran-find-comment-start-skip ()
1517 "Move to past `comment-start-skip' found on current line.
1518 Return t if `comment-start-skip' found, nil if not."
1519 ;; In order to move point only if comment-start-skip is found, this
1520 ;; one uses a lot of save-excursions. Note that re-search-forward
1521 ;; moves point even if comment-start-skip is inside a string-constant.
1522 ;; Some code expects certain values for match-beginning and end.
1524 (if (and comment-start-skip
1526 (re-search-forward comment-start-skip (line-end-position) t)))
1527 (let ((save-match-beginning (match-beginning 0))
1528 (save-match-end (match-end 0)))
1529 (if (fortran-is-in-string-p (match-beginning 0))
1531 (goto-char save-match-end)
1532 (fortran-find-comment-start-skip)) ; recurse for rest of line
1533 (goto-char save-match-beginning)
1534 (re-search-forward comment-start-skip (line-end-position) t)
1535 (goto-char (match-end 0))
1538 ;;From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1539 ;; Test if TAB format continuation lines work.
1540 (defun fortran-is-in-string-p (where)
1541 "Return non-nil iff WHERE (a buffer position) is inside a Fortran string."
1545 ((bolp) nil) ; bol is never inside a string
1546 ((save-excursion ; comment lines too
1548 (looking-at fortran-comment-line-start-skip)) nil)
1549 (t (let (;; ok, serious now. Init some local vars:
1550 (parse-state '(0 nil nil nil nil nil 0))
1551 (quoted-comment-start (if comment-start
1552 (regexp-quote comment-start)))
1554 parse-limit end-of-line)
1555 ;; move to start of current statement
1556 (fortran-next-statement)
1557 (fortran-previous-statement)
1558 ;; now parse up to WHERE
1560 (if (or ;; skip to next line if:
1562 (looking-at fortran-comment-line-start-skip)
1563 ;; - at end of line?
1565 ;; - not in a string and after comment-start?
1566 (and (not (nth 3 parse-state))
1568 (equal comment-start
1569 (char-to-string (preceding-char)))))
1570 (if (> (forward-line) 0)
1571 (setq not-done nil))
1573 ;; if we are at beginning of code line, skip any
1574 ;; whitespace, labels and tab continuation markers.
1575 (if (bolp) (skip-chars-forward " \t0-9"))
1576 ;; if we are in column <= 5 now, check for continuation char
1577 (cond ((= 5 (current-column)) (forward-char 1))
1578 ((and (< (current-column) 5)
1579 (equal fortran-continuation-string
1580 (char-to-string (following-char)))
1582 ;; find out parse-limit from here
1583 (setq end-of-line (line-end-position))
1584 (setq parse-limit (min where end-of-line))
1585 ;; parse max up to comment-start, if non-nil and in current line
1588 (if (re-search-forward quoted-comment-start end-of-line t)
1589 (setq parse-limit (min (point) parse-limit)))))
1590 ;; now parse if still in limits
1591 (if (< (point) where)
1592 (setq parse-state (parse-partial-sexp
1593 (point) parse-limit nil nil parse-state))
1594 (setq not-done nil))))
1596 (nth 3 parse-state))))))
1598 ;; From old version.
1599 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1601 (defun fortran-fill ()
1602 (let* ((auto-fill-function #'fortran-auto-fill)
1604 (bol (line-beginning-position))
1605 (eol (line-end-position))
1606 (bos (min eol (+ bol (fortran-current-line-indentation))))
1610 (if (looking-at fortran-comment-line-start-skip)
1611 nil ; OK to break quotes on comment lines.
1612 (move-to-column fill-column)
1613 (if (fortran-is-in-string-p (point))
1614 (save-excursion (re-search-backward "\\S\"\\s\"\\S\"" bol t)
1615 (if fortran-break-before-delimiters
1618 ;; decide where to split the line. If a position for a quoted
1619 ;; string was found above then use that, else break the line
1620 ;; before the last delimiter.
1621 ;; Delimiters are whitespace, commas, and operators.
1622 ;; Will break before a pair of *'s.
1626 (move-to-column (1+ fill-column))
1627 (skip-chars-backward "^ \t\n,'+-/*=)"
1628 ;;; (if fortran-break-before-delimiters
1629 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1631 (if (<= (point) (1+ bos))
1633 (move-to-column (1+ fill-column))
1634 ;;what is this doing???
1635 (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t))
1638 (re-search-forward "[ \t]" opoint t)
1640 (if (looking-at "\\s\"")
1642 (skip-chars-backward " \t\*")))
1643 (if fortran-break-before-delimiters
1646 ;; if we are in an in-line comment, don't break unless the
1647 ;; line of code is longer than it should be. Otherwise
1648 ;; break the line at the column computed above.
1650 ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
1651 ;; don't prevent a break.
1652 (if (not (or (save-excursion
1653 (if (and comment-start-skip
1654 (re-search-backward comment-start-skip bol t)
1655 (not (fortran-is-in-string-p (point))))
1657 (skip-chars-backward " \t")
1658 (< (current-column) (1+ fill-column)))))
1660 (goto-char fill-point)
1662 (when (> (save-excursion
1665 (min (1+ fill-column)
1666 (+ (fortran-calculate-indent)
1667 fortran-continuation-indent)))
1668 (goto-char fill-point)
1669 (fortran-break-line)
1672 (defun fortran-break-line ()
1673 (let ((opoint (point))
1674 (bol (line-beginning-position))
1675 (eol (line-end-position))
1676 (comment-string nil))
1678 (if (and comment-start-skip (fortran-find-comment-start-skip))
1680 (re-search-backward comment-start-skip bol t)
1681 (setq comment-string (buffer-substring (point) eol))
1682 (delete-region (point) eol))))
1683 ;; Forward line 1 really needs to go to next non white line
1684 (if (save-excursion (forward-line)
1685 (or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")))
1688 (delete-region (point) (match-end 0))
1689 (delete-horizontal-space)
1691 (fortran-split-line))
1696 (delete-horizontal-space)
1697 (indent-to (fortran-comment-indent))
1698 (insert comment-string)))))
1700 (defun fortran-analyze-file-format ()
1701 "Return nil if fixed format is used, t if TAB formatting is used.
1702 Use `fortran-tab-mode-default' if no non-comment statements are found in the
1703 file before the end or the first `fortran-analyze-depth' lines."
1706 (goto-char (point-min))
1710 (eq (char-after) ?\t)
1711 (looking-at " \\{6\\}")
1712 (> i fortran-analyze-depth)))
1716 ((eq (char-after) ?\t) t)
1717 ((looking-at " \\{6\\}") nil)
1718 (fortran-tab-mode-default t)
1721 (or (assq 'fortran-tab-mode-string minor-mode-alist)
1722 (setq minor-mode-alist (cons
1723 '(fortran-tab-mode-string
1724 (indent-tabs-mode fortran-tab-mode-string))
1727 (defun fortran-fill-paragraph (&optional justify)
1728 "Fill surrounding comment block as paragraphs, else fill statement.
1729 Intended as the value of `fill-paragraph-function'."
1733 (if (not (looking-at fortran-comment-line-start-skip))
1734 (fortran-fill-statement)
1735 ;; We're in a comment block. Find the start and end of a
1736 ;; paragraph, delimited either by non-comment lines or empty
1737 ;; comments. (Get positions as markers, since the
1738 ;; `indent-region' below can shift the block's end).
1739 (let* ((non-empty-comment
1740 (concat fortran-comment-line-start-skip "[^ \t\n]"))
1741 (start (save-excursion
1742 ;; Find (start of) first line.
1743 (while (and (zerop (forward-line -1))
1744 (looking-at non-empty-comment)))
1745 (or (looking-at non-empty-comment)
1746 (forward-line)) ; overshot
1748 (end (save-excursion
1749 ;; Find start of first line past region to fill.
1752 (looking-at non-empty-comment)))
1754 ;; Indent the block, find the string comprising the effective
1755 ;; comment start skip and use that as a fill-prefix for
1756 ;; filling the region.
1757 (indent-region start end nil)
1758 (let ((paragraph-ignore-fill-prefix nil)
1761 (looking-at fortran-comment-line-start-skip)
1763 (let (fill-paragraph-function)
1764 (fill-region start end justify))) ; with normal `fill-paragraph'
1765 (set-marker start nil)
1766 (set-marker end nil))))
1769 (defun fortran-fill-statement ()
1770 "Fill a fortran statement up to `fill-column'."
1772 (let ((auto-fill-function #'fortran-auto-fill))
1773 (if (not (save-excursion
1775 (or (looking-at "[ \t]*$")
1776 (looking-at fortran-comment-line-start-skip)
1777 (and comment-start-skip
1778 (looking-at (concat "[ \t]*"
1779 comment-start-skip))))))
1781 ;; Find beginning of statement.
1782 (fortran-next-statement)
1783 (fortran-previous-statement)
1784 ;; Re-indent initially.
1785 (fortran-indent-line)
1786 ;; Replace newline plus continuation field plus indentation with
1790 (fortran-remove-continuation)))
1791 (fortran-previous-statement)))
1792 (fortran-indent-line)))
1794 (defun fortran-strip-sqeuence-nos (&optional do-space)
1795 "Delete all text in column 72 and up (assumed to be sequence numbers).
1796 Normally also deletes trailing whitespace after stripping such text.
1797 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
1800 (goto-char (point-min))
1801 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t)
1802 (replace-match "" nil nil nil 1)
1803 (unless do-space (delete-horizontal-space)))))
1805 ;; This code used to live in add-log.el, but this is a better place
1807 (defun fortran-current-defun ()
1808 "Function to use for `add-log-current-defun-function' in Fortran mode."
1809 ;; We must be inside function body for this to work.
1810 (fortran-beginning-of-subprogram)
1811 (let ((case-fold-search t)) ; case-insensitive
1812 ;; search for fortran subprogram start
1813 (if (re-search-forward
1814 (concat "^[ \t]*\\(program\\|subroutine\\|function"
1815 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
1816 "\\(block[ \t]*data\\)\\)")
1817 (save-excursion (fortran-end-of-subprogram)
1820 (or (match-string-no-properties 2)
1822 ;; move to EOL or before first left paren
1823 (if (re-search-forward "[(\n]" nil t)
1824 (progn (backward-char)
1825 (skip-chars-backward " \t"))
1827 ;; Use the name preceding that.
1828 (buffer-substring-no-properties (point) (progn (backward-sexp)
1834 ;;; fortran.el ends here