1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;; Copyright (C) 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Michael D. Prange <prange@erl.mit.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: fortran, languages
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 3, 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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, 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 (defvar dabbrev-case-fold-search
)
58 (defvar font-lock-syntactic-keywords
)
59 (defvar gud-find-expr-function
)
60 (defvar imenu-case-fold-search
)
61 (defvar imenu-syntax-alist
)
65 "Major mode for editing fixed format Fortran code."
66 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
67 :link
'(custom-manual "(emacs)Fortran")
70 (defgroup fortran-indent nil
71 "Indentation variables in Fortran mode."
75 (defgroup fortran-comment nil
76 "Comment-handling variables in Fortran mode."
82 (defcustom fortran-tab-mode-default nil
83 "*Default tabbing/carriage control style for empty files in Fortran mode.
84 A non-nil value specifies tab-digit style of continuation control.
85 A value of nil specifies that continuation lines are marked
86 with a character in column 6."
88 :group
'fortran-indent
)
89 (put 'fortran-tab-mode-default
'safe-local-variable
'booleanp
)
91 (defcustom fortran-tab-mode-string
"/t"
92 "*String to appear in mode line in TAB format buffers."
94 :group
'fortran-indent
)
95 (put 'fortran-tab-mode-string
'safe-local-variable
'stringp
)
97 (defcustom fortran-do-indent
3
98 "*Extra indentation applied to DO blocks."
100 :group
'fortran-indent
)
101 (put 'fortran-do-indent
'safe-local-variable
'integerp
)
103 (defcustom fortran-if-indent
3
104 "*Extra indentation applied to IF, SELECT CASE and WHERE blocks."
106 :group
'fortran-indent
)
107 (put 'fortran-if-indent
'safe-local-variable
'integerp
)
109 (defcustom fortran-structure-indent
3
110 "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
112 :group
'fortran-indent
)
113 (put 'fortran-structure-indent
'safe-local-variable
'integerp
)
115 (defcustom fortran-continuation-indent
5
116 "*Extra indentation applied to continuation lines."
118 :group
'fortran-indent
)
119 (put 'fortran-continuation-indent
'safe-local-variable
'integerp
)
121 (defcustom fortran-comment-indent-style
'fixed
122 "*How to indent comments.
123 nil forces comment lines not to be touched;
124 `fixed' indents to `fortran-comment-line-extra-indent' columns beyond
125 `fortran-minimum-statement-indent-fixed' (if `indent-tabs-mode' nil), or
126 `fortran-minimum-statement-indent-tab' (if `indent-tabs-mode' non-nil);
127 `relative' indents to current Fortran indentation plus
128 `fortran-comment-line-extra-indent'."
129 :type
'(radio (const :tag
"Untouched" nil
) (const fixed
) (const relative
))
130 :group
'fortran-indent
)
131 (put 'fortran-comment-indent
'safe-local-variable
132 (lambda (value) (memq value
'(nil fixed relative
))))
134 (defcustom fortran-comment-line-extra-indent
0
135 "*Amount of extra indentation for text within full-line comments."
137 :group
'fortran-indent
138 :group
'fortran-comment
)
139 (put 'fortran-comment-line-extra-indent
'safe-local-variable
'integerp
)
141 (defcustom fortran-comment-line-start
"C"
142 "*Delimiter inserted to start new full-line comment.
143 You might want to change this to \"*\", for instance."
146 :group
'fortran-comment
)
147 (put 'fortran-comment-line-start
'safe-local-variable
'stringp
)
149 ;; This used to match preprocessor lines too, but that messes up
150 ;; filling and doesn't seem to be necessary.
151 (defcustom fortran-comment-line-start-skip
152 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
153 "*Regexp to match the start of a full-line comment."
156 :group
'fortran-comment
)
157 (put 'fortran-comment-line-start-skip
'safe-local-variable
'stringp
)
159 (defcustom fortran-directive-re
161 "*Regexp to match a directive line.
162 The matching text will be fontified with `font-lock-keyword-face'.
163 The matching line will be given zero indentation."
166 :group
'fortran-indent
)
167 (put 'fortran-directive-re
'safe-local-variable
'stringp
)
169 (defcustom fortran-minimum-statement-indent-fixed
6
170 "*Minimum statement indentation for fixed format continuation style."
172 :group
'fortran-indent
)
173 (put 'fortran-minimum-statement-indent-fixed
'safe-local-variable
'integerp
)
175 (defcustom fortran-minimum-statement-indent-tab
(max tab-width
6)
176 "*Minimum statement indentation for TAB format continuation style."
178 :group
'fortran-indent
)
179 (put 'fortran-minimum-statement-indent-tab
'safe-local-variable
'integerp
)
181 ;; Note that this is documented in the v18 manuals as being a string
182 ;; of length one rather than a single character.
183 ;; The code in this file accepts either format for compatibility.
184 (defcustom fortran-comment-indent-char
" "
185 "*Single-character string inserted for Fortran comment indentation.
188 :group
'fortran-comment
)
189 (put 'fortran-comment-indent-char
'safe-local-variable
190 (lambda (value) (or (char-valid-p value
)
192 (= (length value
) 1)))))
194 (defcustom fortran-line-number-indent
1
195 "*Maximum indentation for Fortran line numbers.
196 5 means right-justify them within their five-column field."
198 :group
'fortran-indent
)
199 (put 'fortran-line-number-indent
'safe-local-variable
'integerp
)
201 (defcustom fortran-check-all-num-for-matching-do nil
202 "*Non-nil causes all numbered lines to be treated as possible DO loop ends."
205 (put 'fortran-check-all-num-for-matching-do
'safe-local-variable
'booleanp
)
207 (defcustom fortran-blink-matching-if nil
208 "*Non-nil causes \\[fortran-indent-line] on ENDIF to blink on matching IF.
209 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
212 (put 'fortran-blink-matching-if
'safe-local-variable
'booleanp
)
214 (defcustom fortran-continuation-string
"$"
215 "*Single-character string used for Fortran continuation lines.
216 In fixed format continuation style, this character is inserted in
217 column 6 by \\[fortran-split-line] to begin a continuation line.
218 Also, if \\[fortran-indent-line] finds this at the beginning of a
219 line, it will convert the line into a continuation line of the
220 appropriate style. Normally $."
223 (put 'fortran-continuation-string
'safe-local-variable
224 (lambda (value) (and (stringp value
)
225 (= (length value
) 1))))
227 (defcustom fortran-comment-region
"c$$$"
228 "*String inserted by \\[fortran-comment-region] at start of each \
231 :group
'fortran-comment
)
232 (put 'fortran-comment-region
'safe-local-variable
'stringp
)
234 (defcustom fortran-electric-line-number t
235 "*Non-nil causes line numbers to be moved to the correct column as typed."
238 (put 'fortran-electric-line-number
'safe-local-variable
'booleanp
)
240 (defcustom fortran-column-ruler-fixed
241 "0 4 6 10 20 30 40 5\
243 \[ ]|{ | | | | | | | | \
245 "String displayed above current line by \\[fortran-column-ruler].
246 This variable is used in fixed format mode.
247 See the variable `fortran-column-ruler-tab' for TAB format mode."
250 (put 'fortran-column-ruler-fixed
'safe-local-variable
'stringp
)
252 (defcustom fortran-column-ruler-tab
255 \[ ]| { | | | | | | | | \
257 "String displayed above current line by \\[fortran-column-ruler].
258 This variable is used in TAB format mode.
259 See the variable `fortran-column-ruler-fixed' for fixed format mode."
262 (put 'fortran-column-ruler-tab
'safe-local-variable
'stringp
)
264 (defcustom fortran-analyze-depth
100
265 "Number of lines to scan to identify fixed or TAB format style."
268 (put 'fortran-analyze-depth
'safe-local-variable
'integerp
)
270 (defcustom fortran-break-before-delimiters t
271 "*Non-nil causes filling to break lines before delimiters.
272 Delimiters are characters matching the regexp `fortran-break-delimiters-re'."
275 (put 'fortran-break-before-delimiters
'safe-local-variable
'booleanp
)
277 (defconst fortran-break-delimiters-re
"[-+*/><=, \t]"
278 "Regexp matching delimiter characters at which lines may be broken.
279 There are certain tokens comprised entirely of characters
280 matching this regexp that should not be split, and these are
281 specified by the constant `fortran-no-break-re'.")
283 ;; The ">=", etc F77 extensions are supported by g77.
284 (defconst fortran-no-break-re
285 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=") 'paren
)
286 "Regexp specifying where not to break lines when filling.
287 This regexp matches certain tokens comprised entirely of
288 characters matching the regexp `fortran-break-delimiters-re' that should
289 not be split by filling. Each element is assumed to be two
292 (defcustom fortran-mode-hook nil
293 "Hook run when entering Fortran mode."
298 (defvar fortran-if-start-re
"\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*("
299 "Regexp matching the start of an IF statement.")
301 (defvar fortran-end-prog-re1
303 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
304 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?"
305 "Regexp possibly matching the end of a subprogram.")
307 (defvar fortran-end-prog-re
308 (concat "^[ \t0-9]*" fortran-end-prog-re1
)
309 "Regexp possibly matching the end of a subprogram, from the line start.
310 See also `fortran-end-prog-re1'.")
312 (defconst fortran-type-types
314 (mapconcat 'identity
; " " -> "[ \t]*"
318 '("character" "byte" "integer" "logical"
319 "none" "real" "complex"
320 "double precision" "double complex"))
321 (structured-types '("structure" "union" "map"))
322 (other-types '("record" "dimension"
323 "parameter" "common" "save"
324 "external" "intrinsic" "data"
327 (mapcar (lambda (x) (concat "implicit " x
))
330 (mapcar (lambda (x) (concat "end " x
))
333 other-types
)) 'paren
))
335 "Regexp matching Fortran types.")
337 (defvar fortran-font-lock-keywords-1
338 ;; Program, subroutine and function declarations, plus calls.
339 '(("\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|\
340 program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?"
341 (1 font-lock-keyword-face
)
342 (2 font-lock-function-name-face nil t
)))
343 "Subdued level highlighting for Fortran mode.")
345 (defvar fortran-font-lock-keywords-2
346 (append fortran-font-lock-keywords-1
348 ;; Fontify all type specifiers (must be first - see below).
349 (cons fortran-type-types
'font-lock-type-face
)
350 ;; Builtin keywords (except logical, do and goto - see below).
351 (concat "\\<" (regexp-opt
352 '("continue" "format" "end" "enddo"
353 "if" "then" "else" "endif" "elseif"
354 "while" "inquire" "stop" "return"
355 "include" "open" "close" "read"
356 "write" "format" "print" "select" "case"
357 "cycle" "exit" "rewind" "backspace"
360 ;; Builtin operators.
361 (concat "\\." (regexp-opt
362 '("and" "or" "not" "lt" "le" "eq" "ge"
363 "gt" "ne" "eqv" "neqv" "true" "false")
365 ;; do/goto keywords and targets, and goto tags.
366 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
367 (1 font-lock-keyword-face
)
368 (2 font-lock-constant-face nil t
))
369 '("^ *\\([0-9]+\\)" . font-lock-constant-face
)))
370 "Medium level highlighting for Fortran mode.")
372 (defvar fortran-font-lock-keywords-3
374 fortran-font-lock-keywords-1
375 ;; All type specifiers plus their declared items.
377 (list (concat fortran-type-types
"[ \t(/]*\\(*\\)?")
379 '(1 font-lock-type-face
)
380 ;; Declaration item (or just /.../ block name).
381 `(font-lock-match-c-style-declaration-item-and-skip-to-next
382 ;; Start after any *(...) expression.
384 (and (match-beginning ,(1+ (regexp-opt-depth
385 fortran-type-types
)))
389 ;; No need to clean up.
391 ;; Fontify as a variable name, functions fontified elsewhere.
392 (1 font-lock-variable-name-face nil t
))))
393 ;; Things extra to `fortran-font-lock-keywords-3' (must be done first).
395 ;; Goto-like `err=label'/`end=label' in read/write statements.
396 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
397 (1 font-lock-keyword-face
) (4 font-lock-constant-face nil t
))
398 ;; Standard continuation character and in a TAB-formatted line.
399 '("^ \\{5\\}\\([^ 0\n]\\)" 1 font-lock-string-face
)
400 '("^\t\\([1-9]\\)" 1 font-lock-string-face
))
401 `((,fortran-directive-re
(0 font-lock-keyword-face t
)))
402 ;; `fortran-font-lock-keywords-2' without types (see above).
403 (cdr (nthcdr (length fortran-font-lock-keywords-1
)
404 fortran-font-lock-keywords-2
)))
405 "Gaudy level highlighting for Fortran mode.")
407 (defvar fortran-font-lock-keywords-4
408 (append fortran-font-lock-keywords-3
412 '("int" "ifix" "idint" "real" "float" "sngl"
413 "dble" "cmplx" "ichar" "char" "aint" "dint"
414 "anint" "dnint" "nint" "idnint" "iabs" "abs"
415 "dabs" "cabs" "mod" "amod" "dmod" "isign"
416 "sign" "dsign" "idim" "dim" "ddim" "dprod"
417 "max" "max0" "amax1" "dmax1" "amax0" "max1"
418 "min" "min0" "amin1" "dmin1" "amin0" "min1"
419 "len" "index" "lge" "lgt" "lle" "llt" "aimag"
420 "conjg" "sqrt" "dsqrt" "csqrt" "exp" "dexp"
421 "cexp" "log" "alog" "dlog" "clog" "log10"
422 "alog10" "dlog10" "sin" "dsin" "csin" "cos"
423 "dcos" "ccos" "tan" "dtan" "asin" "dasin"
424 "acos" "dacos" "atan" "datan" "atan2" "datan2"
425 "sinh" "dsinh" "cosh" "dcosh" "tanh" "dtanh")
426 'paren
) "[ \t]*(") '(1 font-lock-builtin-face
))))
427 "Maximum highlighting for Fortran mode.
428 Consists of level 3 plus all other intrinsics not already highlighted.")
430 ;; Comments are real pain in Fortran because there is no way to
431 ;; represent the standard comment syntax in an Emacs syntax table.
432 ;; (We can do so for F90-style). Therefore an unmatched quote in a
433 ;; standard comment will throw fontification off on the wrong track.
434 ;; So we do syntactic fontification with regexps.
435 (defvar fortran-font-lock-syntactic-keywords
436 '(("^[cd\\*]" 0 (11))
437 ("^[^cd\\*\t\n].\\{71\\}\\([^\n]+\\)" 1 (11)))
438 "`font-lock-syntactic-keywords' for Fortran.
439 These get fixed-format comments fontified.")
441 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
442 "Default expressions to highlight in Fortran mode.")
444 (defvar fortran-imenu-generic-expression
445 ;; These patterns could be confused by sequence nos. in cols 72+ and
446 ;; don't allow continuations everywhere.
450 ;; [This will be fooled by `end function' allowed by G77. Also,
451 ;; it assumes sensible whitespace is employed.]
453 ;; leading whitespace:
455 ;; function declaration with optional type, e.g. `real',
456 ;; `real*4', character(*), `double precision':
457 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
458 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
459 ;; Possible statement continuation:
460 "[ \t" fortran-continuation-string
"]+"
461 ;; Variable to index:
464 ;; Un-named block data.
465 '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
466 "Value for `imenu-generic-expression' in Fortran mode.")
470 (defconst fortran-blocks-re
471 (concat "block[ \t]*data\\|select[ \t]*case\\|"
472 (regexp-opt '("do" "if" "interface" "function" "map" "program"
473 "structure" "subroutine" "union" "where")))
474 "Regexp potentially indicating the start or end of a Fortran \"block\".
475 Omits naked END statements, and DO-loops closed by anything other
478 (defconst fortran-end-block-re
479 ;; Do-loops terminated by things other than ENDDO cannot be handled
480 ;; with a regexp. This omission does not seem to matter to hideshow...
481 (concat "^[ \t0-9]*\\<end[ \t]*\\("
483 ;; Naked END statement.
485 "Regexp matching the end of a Fortran \"block\", from the line start.
486 Note that only ENDDO is handled for the end of a DO-loop. Used
487 in the Fortran entry in `hs-special-modes-alist'.")
489 (defconst fortran-start-block-re
491 "^[ \t0-9]*\\(" ; statement number
492 ;; Structure label for DO, IF, SELECT, WHERE.
493 "\\(\\(\\sw+[ \t]*:[ \t]*\\)?"
494 ;; IF blocks are a nuisance:
495 ;; IF ( ... ) foo is not a block, but a single statement.
496 ;; IF ( ... ) THEN can be split over multiple lines.
497 ;; [So can, eg, a DO WHILE (... ), but that is less common, I hope.]
498 ;; The regexp below allows for it to be split over at most 2 lines.
499 ;; That leads to the problem of not matching two consecutive IF
500 ;; statements as one, eg:
503 ;; It simply is not possible to do this in a 100% correct fashion
504 ;; using a regexp - see the functions fortran-end-if,
505 ;; fortran-beginning-if for the hoops we have to go through.
506 ;; An alternative is to match on THEN at a line end, eg:
507 ;; ".*)[ \t]*then[ \t]*\\($\\|!\\)"
508 ;; This would also match ELSE branches, though. This does not seem
509 ;; right to me, because then one has neighbouring blocks that are
510 ;; not nested in each other.
511 "\\(if[ \t]*(\\(.*\\|"
512 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
513 "do\\|select[ \t]*case\\|where\\)\\)\\|"
514 (regexp-opt '("interface" "function" "map" "program"
515 "structure" "subroutine" "union"))
516 "\\|block[ \t]*data\\)[ \t]*")
517 "Regexp matching the start of a Fortran \"block\", from the line start.
518 A simple regexp cannot do this in fully correct fashion, so this
519 tries to strike a compromise between complexity and flexibility.
520 Used in the Fortran entry in `hs-special-modes-alist'.")
522 (add-to-list 'hs-special-modes-alist
523 `(fortran-mode ,fortran-start-block-re
,fortran-end-block-re
524 "^[cC*!]" fortran-end-of-block nil
))
527 (defvar fortran-mode-syntax-table
528 (let ((table (make-syntax-table)))
529 ;; We might like `;' to be punctuation (g77 multi-statement
530 ;; lines), but that screws abbrevs.
531 (modify-syntax-entry ?\
; "w" table)
532 (modify-syntax-entry ?
\r " " table
)
533 (modify-syntax-entry ?
+ "." table
)
534 (modify-syntax-entry ?-
"." table
)
535 (modify-syntax-entry ?
= "." table
)
536 (modify-syntax-entry ?
* "." table
)
537 (modify-syntax-entry ?
/ "." table
)
538 (modify-syntax-entry ?
\' "\"" table
)
539 (modify-syntax-entry ?
\" "\"" table
)
540 ;; Consistent with GNU Fortran's default -- see the manual.
541 ;; The F77 standard imposes no rule on this issue.
542 (modify-syntax-entry ?
\\ "\\" table
)
543 ;; This might be better as punctuation, as for C, but this way you
544 ;; can treat floating-point numbers as symbols.
545 (modify-syntax-entry ?.
"_" table
) ; e.g. `a.ne.b'
546 (modify-syntax-entry ?_
"_" table
)
547 (modify-syntax-entry ?$
"_" table
) ; esp. VMSisms
548 (modify-syntax-entry ?\
! "<" table
)
549 (modify-syntax-entry ?
\n ">" table
)
551 "Syntax table used in Fortran mode.")
553 (defvar fortran-gud-syntax-table
554 (let ((st (make-syntax-table fortran-mode-syntax-table
)))
555 (modify-syntax-entry ?
\n "." st
)
557 "Syntax table used to parse Fortran expressions for printing in GUD.")
559 (defvar fortran-mode-map
560 (let ((map (make-sparse-keymap)))
561 (define-key map
";" 'fortran-abbrev-start
)
562 (define-key map
"\C-c;" 'fortran-comment-region
)
563 (define-key map
"\M-;" 'fortran-indent-comment
)
564 (define-key map
"\M-\n" 'fortran-split-line
)
565 (define-key map
"\M-\C-n" 'fortran-end-of-block
)
566 (define-key map
"\M-\C-p" 'fortran-beginning-of-block
)
567 (define-key map
"\M-\C-q" 'fortran-indent-subprogram
)
568 (define-key map
"\C-c\C-w" 'fortran-window-create-momentarily
)
569 (define-key map
"\C-c\C-r" 'fortran-column-ruler
)
570 (define-key map
"\C-c\C-p" 'fortran-previous-statement
)
571 (define-key map
"\C-c\C-n" 'fortran-next-statement
)
572 (define-key map
"\C-c\C-d" 'fortran-join-line
) ; like f90
573 (define-key map
"\M-^" 'fortran-join-line
) ; subvert delete-indentation
574 (define-key map
"0" 'fortran-electric-line-number
)
575 (define-key map
"1" 'fortran-electric-line-number
)
576 (define-key map
"2" 'fortran-electric-line-number
)
577 (define-key map
"3" 'fortran-electric-line-number
)
578 (define-key map
"4" 'fortran-electric-line-number
)
579 (define-key map
"5" 'fortran-electric-line-number
)
580 (define-key map
"6" 'fortran-electric-line-number
)
581 (define-key map
"7" 'fortran-electric-line-number
)
582 (define-key map
"8" 'fortran-electric-line-number
)
583 (define-key map
"9" 'fortran-electric-line-number
)
585 (easy-menu-define fortran-menu map
"Menu for Fortran mode."
587 ["Manual" (info "(emacs)Fortran")]
589 ,(custom-menu-create 'fortran
)
591 ["Save" Custom-save t
]
592 ["Reset to Current" Custom-reset-current t
]
593 ["Reset to Saved" Custom-reset-saved t
]
594 ["Reset to Standard Settings" Custom-reset-standard t
]
597 ["Comment Region" fortran-comment-region mark-active
]
599 (fortran-comment-region (region-beginning) (region-end) 1)
601 ["Indent Region" indent-region mark-active
]
602 ["Indent Subprogram" fortran-indent-subprogram t
]
604 ["Beginning of Subprogram" fortran-beginning-of-subprogram t
]
605 ["End of Subprogram" fortran-end-of-subprogram t
]
607 ["Subprogram" mark-defun
t]
608 ["IF Block" fortran-mark-if t
]
609 ["DO Block" fortran-mark-do t
]
611 ["Narrow to Subprogram" narrow-to-defun
t]
614 ["Temporary column ruler" fortran-column-ruler t
]
615 ["72-column window" fortran-window-create t
]
617 (enlarge-window-horizontally (- (frame-width) (window-width)))
618 (not (window-full-width-p))]
619 ["Momentary 72-column window" fortran-window-create-momentarily t
]
621 ["Break Line at Point" fortran-split-line t
]
622 ["Join Line" fortran-join-line t
]
623 ["Fill Statement/Comment" fill-paragraph t
]
625 ["Toggle auto-fill" auto-fill-mode
:selected auto-fill-function
627 ["Toggle abbrev-mode" abbrev-mode
:selected abbrev-mode
629 ["Add imenu Menu" imenu-add-menubar-index
630 :active
(not (lookup-key (current-local-map) [menu-bar index
]))
631 :included
(fboundp 'imenu-add-to-menubar
)]))
633 "Keymap used in Fortran mode.")
636 (defvar fortran-mode-abbrev-table
638 (define-abbrev-table 'fortran-mode-abbrev-table nil
)
639 fortran-mode-abbrev-table
)
640 "Abbrev table for Fortran mode.")
642 (let (abbrevs-changed)
643 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
644 ;; Only use `apply' to quieten the byte-compiler.
646 (function (lambda (element)
648 (apply 'define-abbrev fortran-mode-abbrev-table
649 (append element
'(nil 0 t
)))
650 (wrong-number-of-arguments
651 (apply 'define-abbrev fortran-mode-abbrev-table
652 (append element
'(nil 0)))))))
653 '((";au" "automatic" )
655 (";bd" "block data" )
664 (";dc" "double complex" )
665 (";dp" "double precision" )
671 (";eq" "equivalence" )
680 (";ib" "implicit byte" )
681 (";ic" "implicit complex" )
682 (";ich" "implicit character")
683 (";ii" "implicit integer" )
684 (";il" "implicit logical" )
685 (";ir" "implicit real" )
688 (";intr" "intrinsic" )
691 (";o" "open" ) ; was ;op
705 (";su" "subroutine" )
714 (defun fortran-mode ()
715 "Major mode for editing Fortran code in fixed format.
716 For free format code, use `f90-mode'.
718 \\[fortran-indent-line] indents the current Fortran line correctly.
719 Note that DO statements must not share a common CONTINUE.
721 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
727 Variables controlling indentation style and extra features:
729 `fortran-comment-line-start'
730 To use comments starting with `!', set this to the string \"!\".
732 Extra indentation within DO blocks (default 3).
734 Extra indentation within IF blocks (default 3).
735 `fortran-structure-indent'
736 Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
738 `fortran-continuation-indent'
739 Extra indentation applied to continuation statements (default 5).
740 `fortran-comment-line-extra-indent'
741 Amount of extra indentation for text in full-line comments (default 0).
742 `fortran-comment-indent-style'
743 How to indent the text in full-line comments. Allowed values are:
744 nil don't change the indentation
745 fixed indent to `fortran-comment-line-extra-indent' beyond the
747 `fortran-minimum-statement-indent-fixed' (fixed format) or
748 `fortran-minimum-statement-indent-tab' (TAB format),
749 depending on the continuation format in use.
750 relative indent to `fortran-comment-line-extra-indent' beyond the
751 indentation for a line of code.
753 `fortran-comment-indent-char'
754 Single-character string to be inserted instead of space for
755 full-line comment indentation (default \" \").
756 `fortran-minimum-statement-indent-fixed'
757 Minimum indentation for statements in fixed format mode (default 6).
758 `fortran-minimum-statement-indent-tab'
759 Minimum indentation for statements in TAB format mode (default 9).
760 `fortran-line-number-indent'
761 Maximum indentation for line numbers (default 1). A line number will
762 get less than this much indentation if necessary to avoid reaching
764 `fortran-check-all-num-for-matching-do'
765 Non-nil causes all numbered lines to be treated as possible \"continue\"
766 statements (default nil).
767 `fortran-blink-matching-if'
768 Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
769 to blink on the matching IF (or DO [WHILE]). (default nil)
770 `fortran-continuation-string'
771 Single-character string to be inserted in column 5 of a continuation
772 line (default \"$\").
773 `fortran-comment-region'
774 String inserted by \\[fortran-comment-region] at start of each line in
775 the region (default \"c$$$\").
776 `fortran-electric-line-number'
777 Non-nil causes line number digits to be moved to the correct column
778 as typed (default t).
779 `fortran-break-before-delimiters'
780 Non-nil causes lines to be broken before delimiters (default t).
782 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
783 with no args, if that value is non-nil."
785 (kill-all-local-variables)
786 (setq major-mode
'fortran-mode
788 local-abbrev-table fortran-mode-abbrev-table
)
789 (set-syntax-table fortran-mode-syntax-table
)
790 (use-local-map fortran-mode-map
)
791 (set (make-local-variable 'indent-line-function
) 'fortran-indent-line
)
792 (set (make-local-variable 'indent-region-function
)
794 (let (fortran-blink-matching-if ; avoid blinking delay
795 indent-region-function
)
796 (indent-region start end nil
))))
797 (set (make-local-variable 'require-final-newline
) mode-require-final-newline
)
798 ;; The syntax tables don't understand the column-0 comment-markers.
799 (set (make-local-variable 'comment-use-syntax
) nil
)
800 (set (make-local-variable 'comment-padding
) "$$$")
801 (set (make-local-variable 'comment-start
) fortran-comment-line-start
)
802 (set (make-local-variable 'comment-start-skip
)
803 ;; We can't reuse `fortran-comment-line-start-skip' directly because
804 ;; it contains backrefs whereas we need submatch-1 to end at the
805 ;; beginning of the comment delimiter.
806 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
807 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
808 (set (make-local-variable 'comment-indent-function
) 'fortran-comment-indent
)
809 (set (make-local-variable 'abbrev-all-caps
) t
)
810 (set (make-local-variable 'normal-auto-fill-function
) 'fortran-auto-fill
)
811 (set (make-local-variable 'indent-tabs-mode
) (fortran-analyze-file-format))
812 (setq mode-line-process
'(indent-tabs-mode fortran-tab-mode-string
))
813 (set (make-local-variable 'fill-column
) 72)
814 (set (make-local-variable 'fill-paragraph-function
) 'fortran-fill-paragraph
)
815 (set (make-local-variable 'font-lock-defaults
)
816 '((fortran-font-lock-keywords
817 fortran-font-lock-keywords-1
818 fortran-font-lock-keywords-2
819 fortran-font-lock-keywords-3
820 fortran-font-lock-keywords-4
)
821 nil t
((?
/ .
"$/") ("_$" .
"w"))
822 fortran-beginning-of-subprogram
))
823 (set (make-local-variable 'font-lock-syntactic-keywords
)
824 fortran-font-lock-syntactic-keywords
)
825 (set (make-local-variable 'imenu-case-fold-search
) t
)
826 (set (make-local-variable 'imenu-generic-expression
)
827 fortran-imenu-generic-expression
)
828 (set (make-local-variable 'imenu-syntax-alist
) '(("_$" .
"w")))
829 (set (make-local-variable 'beginning-of-defun-function
)
830 #'fortran-beginning-of-subprogram
)
831 (set (make-local-variable 'end-of-defun-function
)
832 #'fortran-end-of-subprogram
)
833 (set (make-local-variable 'add-log-current-defun-function
)
834 #'fortran-current-defun
)
835 (set (make-local-variable 'dabbrev-case-fold-search
) 'case-fold-search
)
836 (set (make-local-variable 'gud-find-expr-function
) 'fortran-gud-find-expr
)
837 (run-mode-hooks 'fortran-mode-hook
))
840 (defun fortran-gud-find-expr ()
841 ;; Consider \n as punctuation (end of expression).
842 (with-syntax-table fortran-gud-syntax-table
845 (defsubst fortran-comment-indent
()
846 "Return the indentation appropriate for the current comment line.
847 This is 0 for a line matching `fortran-comment-line-start-skip', else
848 the value of `comment-column' (leaving at least one space after code)."
849 (if (looking-at fortran-comment-line-start-skip
) 0
851 (skip-chars-backward " \t")
852 (max (1+ (current-column)) comment-column
))))
854 (defun fortran-indent-comment ()
855 "Align or create comment on current line.
856 Existing comments of all types are recognized and aligned.
857 If the line has no comment, a side-by-side comment is inserted and aligned,
858 if the value of `comment-start' is not nil and allows such comments.
859 Otherwise, a separate-line comment is inserted, on this line
860 or on a new line inserted before this line if this line is not blank."
863 ;; Recognize existing comments of either kind.
864 (cond ((fortran-find-comment-start-skip 'all
)
865 (goto-char (match-beginning 0))
867 (fortran-indent-line)
868 (unless (= (current-column) (fortran-comment-indent))
869 (delete-horizontal-space)
870 (indent-to (fortran-comment-indent)))))
871 ;; No existing comment.
872 ;; If side-by-side comments are defined, insert one,
873 ;; unless line is now blank.
874 ((and comment-start
(not (looking-at "[ \t]*$"))
875 (string-match comment-start-skip
(concat " " comment-start
)))
877 (delete-horizontal-space)
878 (indent-to (fortran-comment-indent))
879 (insert comment-start
))
880 ;; Else insert separate-line comment, making a new line if nec.
882 (if (looking-at "^[ \t]*$")
883 (delete-horizontal-space)
887 (insert fortran-comment-line-start
)
888 (insert-char (if (stringp fortran-comment-indent-char
)
889 (aref fortran-comment-indent-char
0)
890 fortran-comment-indent-char
)
891 (- (fortran-calculate-indent) (current-column))))))
893 (defun fortran-comment-region (beg-region end-region arg
)
894 "Comment every line in the region.
895 Inserts the string variable `fortran-comment-region' at the beginning of
896 every line in the region.
897 BEG-REGION and END-REGION specify the region boundaries.
898 With non-nil ARG, uncomments the region."
899 (interactive "*r\nP")
900 (let ((end-region-mark (copy-marker end-region
))
901 (save-point (point-marker)))
902 (goto-char beg-region
)
905 (let ((com (regexp-quote fortran-comment-region
))) ; uncomment
907 (delete-region (point) (match-end 0)))
908 (while (and (zerop (forward-line 1))
909 (< (point) end-region-mark
))
911 (delete-region (point) (match-end 0)))))
912 (insert fortran-comment-region
) ; comment
913 (while (and (zerop (forward-line 1))
914 (< (point) end-region-mark
))
915 (insert fortran-comment-region
)))
916 (goto-char save-point
)
917 (set-marker end-region-mark nil
)
918 (set-marker save-point nil
)))
921 (defun fortran-abbrev-start ()
922 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
923 Any other key combination is executed normally."
925 (insert last-command-char
)
926 (let* ((event (if (fboundp 'next-command-event
) ; XEmacs
929 (char (if (fboundp 'event-to-character
)
930 (event-to-character event
) event
)))
931 ;; Insert char if not equal to `?', or if abbrev-mode is off.
932 (if (and abbrev-mode
(or (eq char ??
) (eq char help-char
)
933 (memq event help-event-list
)))
934 (fortran-abbrev-help)
935 (push event unread-command-events
))))
937 (defun fortran-abbrev-help ()
938 "List the currently defined abbrevs in Fortran mode."
940 (message "Listing abbrev table...")
941 (display-buffer (fortran-prepare-abbrev-list-buffer))
942 (message "Listing abbrev table...done"))
944 (defun fortran-prepare-abbrev-list-buffer ()
945 "Create a buffer listing the Fortran mode abbreviations."
946 (with-current-buffer (get-buffer-create "*Abbrevs*")
948 (insert-abbrev-table-description 'fortran-mode-abbrev-table t
)
949 (goto-char (point-min))
950 (set-buffer-modified-p nil
)
952 (get-buffer-create "*Abbrevs*"))
954 (defun fortran-column-ruler ()
955 "Insert a column ruler momentarily above current line, till next keystroke.
956 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
957 format mode, and `fortran-column-ruler-tab' in TAB format mode.
958 The next key typed is executed unless it is SPC."
960 (momentary-string-display
962 fortran-column-ruler-tab
963 fortran-column-ruler-fixed
)
966 (if (eq (window-start (selected-window))
967 (window-point (selected-window)))
968 (line-beginning-position 2)
970 nil
"Type SPC or any command to erase ruler."))
972 (defun fortran-window-create ()
973 "Make the window 72 columns wide.
974 See also `fortran-window-create-momentarily'."
976 (let ((window-min-width 2))
977 (unless (window-full-width-p)
978 (enlarge-window-horizontally (- (frame-width)
980 (let* ((window-edges (window-edges))
981 (scroll-bar-width (- (nth 2 window-edges
)
984 (split-window-horizontally (+ 72 scroll-bar-width
)))
986 (switch-to-buffer " fortran-window-extra" t
)
987 (select-window (previous-window))))
989 (defun fortran-window-create-momentarily (&optional arg
)
990 "Momentarily make the window 72 columns wide.
991 Optional ARG non-nil and non-unity disables the momentary feature.
992 See also `fortran-window-create'."
996 (save-window-excursion
999 (fortran-window-create)
1000 (error (error "No room for Fortran window")))
1001 (message "Type SPC to continue editing.")
1002 (let ((char (read-event)))
1003 (or (equal char ?\s
)
1004 (setq unread-command-events
(list char
))))))
1005 (fortran-window-create)))
1007 (defun fortran-split-line ()
1008 "Break line at point and insert continuation marker and alignment."
1010 (delete-horizontal-space)
1012 (let ((pos (point)))
1014 (and (fortran-find-comment-start-skip 'all
)
1015 (< (match-beginning 0) pos
))))
1016 (insert ?
\n (match-string 0))
1017 (if indent-tabs-mode
1018 (insert ?
\n ?
\t (fortran-numerical-continuation-char))
1019 (insert "\n " fortran-continuation-string
))) ; space after \n important
1020 (fortran-indent-line)) ; when cont string is C, c or *
1022 (defun fortran-remove-continuation ()
1023 "Delete any Fortran continuation characters at point.
1024 Returns t if anything actually deleted."
1025 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1027 (delete-indentation)
1030 (defun fortran-join-line (arg)
1031 "Join current line to the previous one and re-indent.
1032 With a prefix argument, repeat this operation that many times.
1033 If the prefix argument ARG is negative, join the next -ARG lines.
1034 Continuation lines are correctly handled."
1040 (while (not (zerop arg
))
1042 (or (fortran-remove-continuation)
1043 (delete-indentation))
1044 (setq arg
(1- arg
)))
1045 (fortran-indent-line)))
1047 (defun fortran-numerical-continuation-char ()
1048 "Return a digit for tab-digit style of continuation lines.
1049 If previous line is a tab-digit continuation line, return that digit
1050 plus one, otherwise return 1. Zero not allowed."
1053 (if (looking-at "\t[1-9]")
1054 (+ ?
1 (%
(- (char-after (1+ (point))) ?
0) 9))
1057 (put 'fortran-electric-line-number
'delete-selection t
)
1058 (defun fortran-electric-line-number (arg)
1059 "Self insert, but if part of a Fortran line number indent it automatically.
1060 Auto-indent does not happen if a numeric ARG is used."
1062 (if (or arg
(not fortran-electric-line-number
))
1064 (self-insert-command (prefix-numeric-value arg
))
1065 (self-insert-command 1))
1066 (if (or (and (= 5 (current-column))
1069 ;; In col 5 with only spaces to the left.
1070 (looking-at " \\{5\\}")))
1071 (and (= (if indent-tabs-mode
1072 fortran-minimum-statement-indent-tab
1073 fortran-minimum-statement-indent-fixed
) (current-column))
1074 ;; In col 8 with a single tab to the left.
1075 (eq ?
\t (char-after (line-beginning-position)))
1076 (not (or (eq last-command
'fortran-indent-line
)
1078 'fortran-indent-new-line
))))
1080 (re-search-backward "[^ \t0-9]"
1081 (line-beginning-position)
1082 t
)) ; not a line number
1083 (looking-at "[0-9]")) ; within a line number
1084 (self-insert-command (prefix-numeric-value arg
))
1085 (skip-chars-backward " \t")
1086 (insert last-command-char
)
1087 (fortran-indent-line))))
1090 (defun fortran-check-end-prog-re ()
1091 "Check a preliminary match against `fortran-end-prog-re'."
1092 ;; Having got a possible match for the subprogram end, we need a
1093 ;; match of whitespace, avoiding possible column 73+ stuff.
1095 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
1096 (buffer-substring (match-end 0)
1097 (min (line-end-position)
1098 (+ 72 (line-beginning-position)))))))
1100 ;; Note that you can't just check backwards for `subroutine' &c in
1101 ;; case of un-marked main programs not at the start of the file.
1102 (defun fortran-beginning-of-subprogram ()
1103 "Move point to the beginning of the current Fortran subprogram."
1106 (let ((case-fold-search t
))
1107 (beginning-of-line -
1)
1109 (while (re-search-backward fortran-end-prog-re nil
'move
)
1110 (if (fortran-check-end-prog-re)
1114 (defun fortran-end-of-subprogram ()
1115 "Move point to the end of the current Fortran subprogram."
1118 (let ((case-fold-search t
))
1119 (if (save-excursion ; on END
1121 (and (looking-at fortran-end-prog-re
)
1122 (fortran-check-end-prog-re)))
1124 (beginning-of-line 2)
1126 (while (re-search-forward fortran-end-prog-re nil
'move
)
1127 (if (fortran-check-end-prog-re)
1129 (goto-char (match-beginning 0))
1132 (defun fortran-previous-statement ()
1133 "Move point to beginning of the previous Fortran statement.
1134 Returns 'first-statement if that statement is the first
1135 non-comment Fortran statement in the file, and nil otherwise.
1136 Directive lines are treated as comments."
1138 (let (not-first-statement continue-test
)
1142 (not (looking-at fortran-comment-line-start-skip
))
1143 (not (looking-at fortran-directive-re
))
1146 (regexp-quote fortran-continuation-string
)))
1147 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1148 (while (and (setq not-first-statement
(zerop (forward-line -
1)))
1149 (or (looking-at fortran-comment-line-start-skip
)
1150 (looking-at fortran-directive-re
)
1153 (regexp-quote fortran-continuation-string
)))
1154 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1155 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1156 (cond ((and continue-test
1157 (not not-first-statement
))
1158 (message "Incomplete continuation statement."))
1160 (fortran-previous-statement))
1161 ((not not-first-statement
)
1162 'first-statement
))))
1164 (defun fortran-next-statement ()
1165 "Move point to beginning of the next Fortran statement.
1166 Returns 'last-statement if that statement is the last
1167 non-comment Fortran statement in the file, and nil otherwise.
1168 Directive lines are treated as comments."
1170 (let (not-last-statement)
1172 (while (and (setq not-last-statement
1173 (and (zerop (forward-line 1))
1175 (or (looking-at fortran-comment-line-start-skip
)
1176 (looking-at fortran-directive-re
)
1177 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1178 (looking-at (concat "[ \t]*" comment-start-skip
)))))
1179 (if (not not-last-statement
)
1182 (defun fortran-looking-at-if-then ()
1183 "Return non-nil if at the start of a line with an IF ... THEN statement."
1184 ;; cf f90-looking-at-if-then.
1186 (i (fortran-beginning-if)))
1193 ;; Used in hs-special-modes-alist.
1194 (defun fortran-end-of-block (&optional num
)
1195 "Move point forward to the end of the current code block.
1196 With optional argument NUM, go forward that many balanced blocks.
1197 If NUM is negative, go backward to the start of a block. Does
1198 not check for consistency of block types. Interactively, pushes
1199 mark before moving point."
1201 (if (interactive-p) (push-mark (point) t
))
1202 (and num
(< num
0) (fortran-beginning-of-block (- num
)))
1203 (let ((case-fold-search t
)
1206 (while (and (> count
0)
1208 (concat "\\(" fortran-blocks-re
1209 (if fortran-check-all-num-for-matching-do
1210 "\\|^[ \t]*[0-9]+" "")
1211 "\\|continue\\|end\\)\\>")
1214 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re
))
1215 (fortran-looking-at-if-then)
1216 (looking-at fortran-start-block-re
))
1217 (setq count
(1+ count
))
1218 (if (or (looking-at fortran-end-block-re
)
1219 (and (or (looking-at "^[0-9 \t]*continue")
1220 (and fortran-check-all-num-for-matching-do
1221 (looking-at "[ \t]*[0-9]+")))
1222 (fortran-check-for-matching-do)))
1223 (setq count
(1- count
))))
1225 (if (> count
0) (error "Missing block end"))))
1227 (defun fortran-beginning-of-block (&optional num
)
1228 "Move point backwards to the start of the current code block.
1229 With optional argument NUM, go backward that many balanced
1230 blocks. If NUM is negative, go forward to the end of a block.
1231 Does not check for consistency of block types. Interactively,
1232 pushes mark before moving point."
1234 (if (interactive-p) (push-mark (point) t
))
1235 (and num
(< num
0) (fortran-end-of-block (- num
)))
1236 (let ((case-fold-search t
)
1239 (while (and (> count
0)
1241 (concat "\\(" fortran-blocks-re
1242 (if fortran-check-all-num-for-matching-do
1243 "\\|^[ \t]*[0-9]+" "")
1244 "\\|continue\\|end\\)\\>")
1247 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re
))
1248 (fortran-looking-at-if-then)
1249 (looking-at fortran-start-block-re
))
1250 (setq count
(1- count
))
1251 (if (or (looking-at fortran-end-block-re
)
1252 (and (or (looking-at "^[0-9 \t]*continue")
1253 (and fortran-check-all-num-for-matching-do
1254 (looking-at "[ \t]*[0-9]+")))
1255 (fortran-check-for-matching-do)))
1256 (setq count
(1+ count
)))))
1257 ;; Includes an un-named main program block.
1258 (if (> count
0) (error "Missing block start"))))
1261 (defun fortran-blink-match (regex keyword find-begin
)
1262 "From a line matching REGEX, blink matching KEYWORD statement line.
1263 Use function FIND-BEGIN to match it."
1264 (let ((top-of-window (window-start))
1266 (case-fold-search t
)
1269 (when (save-excursion
1271 (skip-chars-forward " \t0-9")
1273 (if (not (setq matching
(funcall find-begin
)))
1274 (setq message
(concat "No matching " keyword
"."))
1275 (if (< matching top-of-window
)
1277 (goto-char matching
)
1281 (buffer-substring (point)
1282 (line-end-position)))))))
1284 (message "%s" message
)
1285 (goto-char matching
)
1286 (sit-for blink-matching-delay
)
1287 (goto-char end-point
)))))
1289 (defun fortran-blink-matching-if ()
1290 "From an ENDIF or ELSE statement, blink the matching IF statement."
1291 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1292 "if" #'fortran-beginning-if
))
1294 (defun fortran-blink-matching-do ()
1295 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1296 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do
))
1298 (defun fortran-mark-do ()
1299 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1300 The marks are pushed."
1302 (let (enddo-point do-point
)
1303 (if (setq enddo-point
(fortran-end-do))
1304 (if (not (setq do-point
(fortran-beginning-do)))
1305 (message "No matching do.")
1306 (goto-char enddo-point
)
1308 (goto-char do-point
)))))
1310 (defun fortran-end-do ()
1311 "Search forward for first unmatched ENDDO.
1312 Return point or nil."
1313 (let ((case-fold-search t
))
1314 (if (save-excursion (beginning-of-line)
1315 (skip-chars-forward " \t0-9")
1316 (looking-at "end[ \t]*do\\b"))
1322 (while (and (not (zerop count
))
1323 (not (eq (fortran-next-statement) 'last-statement
))
1324 ;; Keep local to subprogram.
1325 (not (and (looking-at fortran-end-prog-re
)
1326 (fortran-check-end-prog-re))))
1327 (skip-chars-forward " \t0-9")
1328 (cond ((looking-at "end[ \t]*do\\b")
1329 (setq count
(1- count
)))
1331 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1332 (setq count
(1+ count
)))))
1334 ;; All pairs accounted for.
1337 (defun fortran-beginning-do ()
1338 "Search backwards for first unmatched DO [WHILE].
1339 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1340 (let ((case-fold-search t
)
1341 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1344 (skip-chars-forward " \t0-9")
1345 (looking-at dostart-re
))
1351 (while (and (not (zerop count
))
1352 (not (eq (fortran-previous-statement) 'first-statement
))
1353 ;; Keep local to subprogram.
1354 (not (and (looking-at fortran-end-prog-re
)
1355 (fortran-check-end-prog-re))))
1356 (skip-chars-forward " \t0-9")
1357 (cond ((looking-at dostart-re
)
1358 (setq count
(1- count
)))
1359 ;; Note labelled loop ends not considered.
1360 ((looking-at "end[ \t]*do\\b")
1361 (setq count
(1+ count
)))))
1363 ;; All pairs accounted for.
1366 (defun fortran-mark-if ()
1367 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1368 The marks are pushed."
1370 (let (endif-point if-point
)
1371 (if (setq endif-point
(fortran-end-if))
1372 (if (not (setq if-point
(fortran-beginning-if)))
1373 (message "No matching if.")
1374 ;; Set mark, move point.
1375 (goto-char endif-point
)
1377 (goto-char if-point
)))))
1379 (defun fortran-end-if ()
1380 "Search forwards for first unmatched ENDIF.
1381 Return point or nil."
1382 (let ((case-fold-search t
))
1383 (if (save-excursion (beginning-of-line)
1384 (skip-chars-forward " \t0-9")
1385 (looking-at "end[ \t]*if\\b"))
1388 ;; Search for one. The point has been already been moved to first
1389 ;; letter on line but this should not cause troubles.
1392 (while (and (not (zerop count
))
1393 (not (eq (fortran-next-statement) 'last-statement
))
1394 ;; Keep local to subprogram.
1395 (not (and (looking-at fortran-end-prog-re
)
1396 (fortran-check-end-prog-re))))
1397 (skip-chars-forward " \t0-9")
1398 (cond ((looking-at "end[ \t]*if\\b")
1399 (setq count
(1- count
)))
1400 ((looking-at fortran-if-start-re
)
1403 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1404 (let (then-test) ; multi-line if-then
1407 (zerop (forward-line 1))
1408 ;; Search forward for then.
1409 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1413 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1415 (setq count
(1+ count
)))))))
1417 ;; All pairs accounted for.
1420 (defun fortran-beginning-if ()
1421 "Search backwards for first unmatched IF-THEN.
1422 Return point or nil."
1423 (let ((case-fold-search t
))
1425 ;; May be sitting on multi-line if-then statement, first
1426 ;; move to beginning of current statement. Note:
1427 ;; `fortran-previous-statement' moves to previous statement
1428 ;; *unless* current statement is first one. Only move
1429 ;; forward if not first-statement.
1430 (if (not (eq (fortran-previous-statement) 'first-statement
))
1431 (fortran-next-statement))
1432 (skip-chars-forward " \t0-9")
1434 (looking-at fortran-if-start-re
)
1436 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1437 ;; Multi-line if-then.
1440 (and (zerop (forward-line 1))
1441 ;; Search forward for then.
1442 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1446 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1453 (while (and (not (zerop count
))
1454 (not (eq (fortran-previous-statement) 'first-statement
))
1455 ;; Keep local to subprogram.
1456 (not (and (looking-at fortran-end-prog-re
)
1457 (fortran-check-end-prog-re))))
1458 (skip-chars-forward " \t0-9")
1459 (cond ((looking-at fortran-if-start-re
)
1462 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1463 (let (then-test) ; multi-line if-then
1466 (zerop (forward-line 1))
1467 ;; Search forward for then.
1468 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1472 (concat ".*then\\b[ \t]*"
1473 "[^ \t(=a-z0-9]"))))))
1475 (setq count
(1- count
)))))
1476 ((looking-at "end[ \t]*if\\b")
1477 (setq count
(1+ count
)))))
1479 ;; All pairs accounted for.
1483 (defun fortran-indent-line ()
1484 "Indent current Fortran line based on its contents and on previous lines."
1486 (let ((cfi (fortran-calculate-indent)))
1489 (if (or (not (= cfi
(fortran-current-line-indentation)))
1490 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
1491 (not (fortran-line-number-indented-correctly-p))))
1492 (fortran-indent-to-column cfi
)
1494 (if (fortran-find-comment-start-skip)
1495 (fortran-indent-comment))))
1496 ;; Never leave point in left margin.
1497 (if (< (current-column) cfi
)
1498 (move-to-column cfi
))
1499 (and auto-fill-function
1500 (> (save-excursion (end-of-line) (current-column))
1505 (when fortran-blink-matching-if
1506 (fortran-blink-matching-if)
1507 (fortran-blink-matching-do))))
1509 (defun fortran-auto-fill ()
1510 "Function to use for `normal-auto-fill-function' in Fortran mode."
1511 (if (> (current-column) (current-fill-column))
1512 (let ((cfi (fortran-calculate-indent)))
1515 (if (or (not (= cfi
(fortran-current-line-indentation)))
1516 (and (re-search-forward "^[ \t]*[0-9]+"
1518 (not (fortran-line-number-indented-correctly-p))))
1519 (fortran-indent-to-column cfi
)
1521 (if (fortran-find-comment-start-skip)
1522 (fortran-indent-comment))))
1524 ;; Never leave point in left margin.
1525 (if (< (current-column) cfi
)
1526 (move-to-column cfi
)))))
1528 ;; Historically this was a separate function which advertised itself
1529 ;; as reindenting but only did so where `most likely to be necessary'.
1530 (defalias 'fortran-indent-new-line
'reindent-then-newline-and-indent
)
1532 (defun fortran-indent-subprogram ()
1533 "Properly indent the Fortran subprogram containing point."
1537 (message "Indenting subprogram...")
1538 (indent-region (point) (mark) nil
))
1539 (message "Indenting subprogram...done."))
1541 (defun fortran-calculate-indent ()
1542 "Calculates the Fortran indent column based on previous lines."
1543 (let (icol first-statement
(case-fold-search t
)
1544 (fortran-minimum-statement-indent
1545 (if indent-tabs-mode
1546 fortran-minimum-statement-indent-tab
1547 fortran-minimum-statement-indent-fixed
)))
1549 (setq first-statement
(fortran-previous-statement))
1551 (setq icol fortran-minimum-statement-indent
)
1552 (if (= (point) (point-min))
1553 (setq icol fortran-minimum-statement-indent
)
1554 (setq icol
(fortran-current-line-indentation)))
1555 (skip-chars-forward " \t0-9")
1556 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1557 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1558 (let (then-test) ; multi-line if-then
1559 (while (and (zerop (forward-line 1))
1560 ;; Search forward for then.
1561 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1562 (not (setq then-test
1565 *[^ \t_$(=a-z0-9]")))))
1567 (setq icol
(+ icol fortran-if-indent
))))
1568 ((looking-at "else\\(if\\)?\\b")
1569 (setq icol
(+ icol fortran-if-indent
)))
1570 ((looking-at "select[ \t]*case[ \t](.*)")
1571 (setq icol
(+ icol fortran-if-indent
)))
1572 ((looking-at "case[ \t]*(.*)")
1573 (setq icol
(+ icol fortran-if-indent
)))
1574 ((looking-at "case[ \t]*default\\b")
1575 (setq icol
(+ icol fortran-if-indent
)))
1576 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1577 (setq icol
(+ icol fortran-if-indent
)))
1578 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1579 (setq icol
(+ icol fortran-if-indent
)))
1580 ((looking-at "do\\b")
1581 (setq icol
(+ icol fortran-do-indent
)))
1583 "\\(structure\\|union\\|map\\|interface\\)\
1584 \\b[ \t]*[^ \t=(a-z]")
1585 (setq icol
(+ icol fortran-structure-indent
)))
1586 ((and (looking-at fortran-end-prog-re1
)
1587 (fortran-check-end-prog-re))
1588 ;; Previous END resets indent to minimum.
1589 (setq icol fortran-minimum-statement-indent
)))))
1592 (cond ((looking-at "[ \t]*$"))
1593 ;; Check for directive before comment, so as not to indent.
1594 ((looking-at fortran-directive-re
)
1595 (setq fortran-minimum-statement-indent
0 icol
0))
1596 ((looking-at fortran-comment-line-start-skip
)
1597 (cond ((eq fortran-comment-indent-style
'relative
)
1598 (setq icol
(+ icol fortran-comment-line-extra-indent
)))
1599 ((eq fortran-comment-indent-style
'fixed
)
1600 (setq icol
(+ fortran-minimum-statement-indent
1601 fortran-comment-line-extra-indent
))))
1602 (setq fortran-minimum-statement-indent
0))
1603 ((or (looking-at (concat "[ \t]*"
1605 fortran-continuation-string
)))
1606 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1607 (skip-chars-forward " \t")
1608 ;; Do not introduce extra whitespace into a broken string.
1610 (if (fortran-is-in-string-p (point))
1612 (+ icol fortran-continuation-indent
))))
1614 ((and fortran-check-all-num-for-matching-do
1615 (looking-at "[ \t]*[0-9]+")
1616 (fortran-check-for-matching-do))
1617 (setq icol
(- icol fortran-do-indent
)))
1619 (skip-chars-forward " \t0-9")
1620 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1621 (setq icol
(- icol fortran-if-indent
)))
1622 ((looking-at "else\\(if\\)?\\b")
1623 (setq icol
(- icol fortran-if-indent
)))
1624 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1625 (setq icol
(- icol fortran-if-indent
)))
1626 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1627 (setq icol
(- icol fortran-if-indent
)))
1628 ((and (looking-at "continue\\b")
1629 (fortran-check-for-matching-do))
1630 (setq icol
(- icol fortran-do-indent
)))
1631 ((looking-at "end[ \t]*do\\b")
1632 (setq icol
(- icol fortran-do-indent
)))
1633 ((looking-at "end[ \t]*\
1634 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1635 (setq icol
(- icol fortran-structure-indent
)))
1636 ((and (looking-at fortran-end-prog-re1
)
1637 (fortran-check-end-prog-re)
1638 (not (= icol fortran-minimum-statement-indent
)))
1639 (message "Warning: `end' not in column %d. Probably\
1640 an unclosed block." fortran-minimum-statement-indent
))))))
1641 (max fortran-minimum-statement-indent icol
)))
1644 (defun fortran-current-line-indentation ()
1645 "Indentation of current line, ignoring Fortran line number or continuation.
1646 This is the column position of the first non-whitespace character
1647 aside from the line number and/or column 5/8 line-continuation character.
1648 For comment lines, returns indentation of the first
1649 non-indentation text within the comment."
1652 (cond ((looking-at fortran-comment-line-start-skip
)
1653 (goto-char (match-end 0))
1655 (if (stringp fortran-comment-indent-char
)
1656 fortran-comment-indent-char
1657 (char-to-string fortran-comment-indent-char
))))
1658 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1659 (goto-char (match-end 0)))
1661 ;; Move past line number.
1662 (skip-chars-forward "[ \t0-9]")))
1663 ;; Move past whitespace.
1664 (skip-chars-forward " \t")
1667 (defun fortran-indent-to-column (col)
1668 "Indent current line to column COL.
1669 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1670 line, and this continuation character is retained on indentation;
1671 2) If `fortran-continuation-string' is the first non-whitespace
1672 character, this is a continuation line;
1673 3) A non-continuation line which has a number as the first
1674 non-whitespace character is a numbered line.
1675 4) A TAB followed by a digit indicates a continuation line."
1678 (if (looking-at fortran-comment-line-start-skip
)
1679 (if fortran-comment-indent-style
1680 (let* ((char (if (stringp fortran-comment-indent-char
)
1681 (aref fortran-comment-indent-char
0)
1682 fortran-comment-indent-char
))
1683 (chars (string ?\s ?
\t char
)))
1684 (goto-char (match-end 0))
1685 (skip-chars-backward chars
)
1686 (delete-region (point) (progn (skip-chars-forward chars
)
1688 (insert-char char
(- col
(current-column)))))
1689 (if (looking-at "\t[1-9]")
1690 (if indent-tabs-mode
1691 (goto-char (match-end 0))
1694 (insert fortran-continuation-string
))
1695 (if (looking-at " \\{5\\}[^ 0\n]")
1696 (if indent-tabs-mode
1697 (progn (delete-char 6)
1698 (insert ?
\t (fortran-numerical-continuation-char) 1))
1700 (delete-horizontal-space)
1701 ;; Put line number in columns 0-4, or
1702 ;; continuation character in column 5.
1704 ((looking-at (regexp-quote fortran-continuation-string
))
1705 (if indent-tabs-mode
1708 (if indent-tabs-mode
1709 fortran-minimum-statement-indent-tab
1710 fortran-minimum-statement-indent-fixed
))
1712 (insert-char (fortran-numerical-continuation-char) 1))
1715 ((looking-at "[0-9]+")
1716 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1717 (if (< extra-space
0)
1718 (message "Warning: line number exceeds 5-digit limit.")
1719 (indent-to (min fortran-line-number-indent extra-space
))))
1720 (skip-chars-forward "0-9")))))
1721 ;; Point is now after any continuation character or line number.
1722 ;; Put body of statement where specified.
1723 (delete-horizontal-space)
1725 ;; Indent any comment following code on the same line.
1726 (when (fortran-find-comment-start-skip)
1727 (goto-char (match-beginning 0))
1728 (unless (= (current-column) (fortran-comment-indent))
1729 (delete-horizontal-space)
1730 (indent-to (fortran-comment-indent)))))))
1732 (defun fortran-line-number-indented-correctly-p ()
1733 "Return t if current line's line number is correctly indented.
1734 Do not call if there is no line number."
1737 (skip-chars-forward " \t")
1738 (and (<= (current-column) fortran-line-number-indent
)
1739 (or (= (current-column) fortran-line-number-indent
)
1740 (progn (skip-chars-forward "0-9")
1741 (= (current-column) 5))))))
1743 (defun fortran-check-for-matching-do ()
1744 "When called from a numbered statement, return t if matching DO is found.
1745 Otherwise return nil."
1746 (let ((case-fold-search t
)
1750 (when (looking-at "[ \t]*[0-9]+")
1751 (skip-chars-forward " \t")
1752 (skip-chars-forward "0") ; skip past leading zeros
1754 (buffer-substring (point) (progn
1755 (skip-chars-forward "0-9")
1761 (and (re-search-backward
1763 "\\(^[ \t0-9]*do[ \t]*0*"
1764 charnum
"\\b\\)\\|" "\\(^[ \t]*0*"
1768 (concat "^[ \t0-9]*do[ \t]*0*"
1771 (defun fortran-find-comment-start-skip (&optional all
)
1772 "Move to past `comment-start-skip' found on current line.
1773 Return non-nil if `comment-start-skip' found, nil if not.
1774 If ALL is nil, only match comments that start in column > 0."
1775 ;; Hopefully at some point we can just use the line below! -stef
1776 ;; (comment-search-forward (line-end-position) t))
1777 (when (or all comment-start-skip
)
1779 (css (if comment-start-skip
1780 (concat fortran-comment-line-start-skip
1781 "\\|" comment-start-skip
)
1782 fortran-comment-line-start-skip
)))
1783 (when (re-search-forward css
(line-end-position) t
)
1784 (if (and (or all
(> (match-beginning 0) (line-beginning-position)))
1785 (or (save-match-data
1786 (not (fortran-is-in-string-p (match-beginning 0))))
1787 ;; Recurse for rest of line.
1788 (fortran-find-comment-start-skip all
)))
1793 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1794 ;; Test if TAB format continuation lines work.
1795 (defun fortran-is-in-string-p (where)
1796 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
1800 ((bolp) nil
) ; bol is never inside a string
1801 ((save-excursion ; comment lines too
1803 (looking-at fortran-comment-line-start-skip
)) nil
)
1804 (t (let ((parse-state '(0 nil nil nil nil nil
0))
1805 (quoted-comment-start (if comment-start
1806 (regexp-quote comment-start
)))
1808 parse-limit end-of-line
)
1809 ;; Move to start of current statement.
1810 (fortran-next-statement)
1811 (fortran-previous-statement)
1812 ;; Now parse up to WHERE.
1814 (if (or ;; Skip to next line if:
1816 (looking-at fortran-comment-line-start-skip
)
1817 ;; - at end of line?
1819 ;; - not in a string and after comment-start?
1820 (and (not (nth 3 parse-state
))
1822 (equal comment-start
1823 (char-to-string (preceding-char)))))
1824 (if (> (forward-line) 0)
1825 (setq not-done nil
))
1827 ;; If we are at beginning of code line, skip any
1828 ;; whitespace, labels and tab continuation markers.
1829 (if (bolp) (skip-chars-forward " \t0-9"))
1830 ;; If we are in column <= 5 now, check for continuation char.
1831 (cond ((= 5 (current-column)) (forward-char 1))
1832 ((and (< (current-column) 5)
1833 (equal fortran-continuation-string
1834 (char-to-string (following-char)))
1836 ;; Find out parse-limit from here.
1837 (setq end-of-line
(line-end-position))
1838 (setq parse-limit
(min where end-of-line
))
1839 ;; Parse max up to comment-start, if non-nil and in current line.
1842 (if (re-search-forward quoted-comment-start end-of-line t
)
1843 (setq parse-limit
(min (point) parse-limit
)))))
1844 ;; Now parse if still in limits.
1845 (if (< (point) where
)
1846 (setq parse-state
(parse-partial-sexp
1847 (point) parse-limit nil nil parse-state
))
1848 (setq not-done nil
))))
1850 (nth 3 parse-state
))))))
1852 ;; From old version.
1853 (defalias 'fortran-auto-fill-mode
'auto-fill-mode
)
1855 (defun fortran-fill ()
1856 "Fill the current line at an appropriate point(s)."
1857 (let* ((auto-fill-function #'fortran-auto-fill
)
1859 (bol (line-beginning-position))
1860 (eol (line-end-position))
1861 (bos (min eol
(+ bol
(fortran-current-line-indentation))))
1862 ;; If in a string at fill-column, break it either before the
1863 ;; initial quote, or at fill-col (if string is too long).
1867 ;; OK to break quotes on comment lines.
1868 (unless (looking-at fortran-comment-line-start-skip
)
1869 (let (fcpoint start
)
1870 (move-to-column fill-column
)
1871 (when (fortran-is-in-string-p (setq fcpoint
(point)))
1873 (re-search-backward "\\S\"\\s\"\\S\"?" bol t
)
1875 (if fortran-break-before-delimiters
1878 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t
)
1880 ;; If the current string is longer than (fill-column
1881 ;; - 6) chars, break it at the fill column (else
1883 (if (> (- (point) start
)
1884 (- fill-column
6 fortran-continuation-indent
))
1887 ;; Decide where to split the line. If a position for a quoted
1888 ;; string was found above then use that, else break the line
1889 ;; before/after the last delimiter.
1893 ;; If f-b-b-d is t, have an extra column to play with,
1894 ;; since delimiter gets shifted to new line.
1895 (move-to-column (if fortran-break-before-delimiters
1901 ;; Adapted from f90-find-breakpoint.
1902 (re-search-backward fortran-break-delimiters-re bol
)
1903 (if (not fortran-break-before-delimiters
)
1904 (if (looking-at fortran-no-break-re
)
1905 ;; Deal with cases such as "**" split over
1906 ;; fill-col. Simpler alternative would be
1907 ;; to start from (1- fill-column) above.
1908 (if (> (+ 2 (current-column)) fill-column
)
1913 (or (looking-at fortran-no-break-re
)
1915 ;; Line indented beyond fill-column?
1916 (when (<= (point) bos
)
1917 (move-to-column (1+ fill-column
))
1918 ;; What is this doing???
1919 (or (re-search-forward "[\t\n,'+-/*)=]" eol t
)
1922 (re-search-forward "[ \t]" opoint t
))
1924 ;; If we are in an in-line comment, don't break unless the
1925 ;; line of code is longer than it should be. Otherwise
1926 ;; break the line at the column computed above.
1928 ;; Need to use fortran-find-comment-start-skip to make sure that
1929 ;; quoted !'s don't prevent a break.
1930 (when (and (save-excursion
1932 (if (not (fortran-find-comment-start-skip))
1934 (goto-char (match-beginning 0))
1935 (>= (point) fill-point
)))
1937 (goto-char fill-point
)
1942 (min (1+ fill-column
)
1943 (+ (fortran-calculate-indent)
1944 fortran-continuation-indent
))))
1945 (goto-char fill-point
)
1946 (fortran-break-line)
1949 (defun fortran-break-line ()
1950 "Call `fortran-split-line'. Joins continuation lines first, then refills."
1951 (let ((bol (line-beginning-position))
1954 (if (fortran-find-comment-start-skip)
1955 (delete-and-extract-region
1956 (match-beginning 0) (line-end-position))))))
1957 ;; Forward line 1 really needs to go to next non white line.
1958 (if (save-excursion (forward-line)
1959 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1962 (delete-region (point) (match-end 0))
1963 (delete-horizontal-space)
1965 (fortran-split-line))
1970 (delete-horizontal-space)
1971 (indent-to (fortran-comment-indent))
1972 (insert comment-string
)))))
1974 (defun fortran-analyze-file-format ()
1975 "Return nil if fixed format is used, t if TAB formatting is used.
1976 Use `fortran-tab-mode-default' if no non-comment statements are found
1977 before the end or in the first `fortran-analyze-depth' lines."
1980 (goto-char (point-min))
1983 (eq (char-after) ?
\t)
1984 (looking-at " \\{6\\}")
1985 (> i fortran-analyze-depth
)))
1989 ((eq (char-after) ?
\t) t
)
1990 ((looking-at " \\{6\\}") nil
)
1991 (t fortran-tab-mode-default
)))))
1993 (defun fortran-fill-paragraph (&optional justify
)
1994 "Fill surrounding comment block as paragraphs, else fill statement.
1995 Intended as the value of `fill-paragraph-function'.
1996 A comment block is filled by calling `fill-comment-paragraph' with
1997 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
1998 Always returns non-nil (to prevent `fill-paragraph' being called)."
2000 (or (fill-comment-paragraph justify
)
2001 (fortran-fill-statement)
2004 (defun fortran-fill-statement ()
2005 "Fill a Fortran statement up to `fill-column'."
2007 (let ((auto-fill-function #'fortran-auto-fill
))
2008 (unless (save-excursion
2010 (or (looking-at "[ \t]*$")
2011 (looking-at fortran-comment-line-start-skip
)
2012 (and comment-start-skip
2013 (looking-at (concat "[ \t]*" comment-start-skip
)))))
2015 ;; Find beginning of statement.
2016 (fortran-next-statement)
2017 (fortran-previous-statement)
2018 ;; Re-indent initially.
2019 (fortran-indent-line)
2020 ;; Replace newline plus continuation field plus indentation with
2024 (fortran-remove-continuation)))
2025 (fortran-previous-statement)))
2026 (fortran-indent-line)))
2028 (defun fortran-strip-sequence-nos (&optional do-space
)
2029 "Delete all text in column 72 and up (assumed to be sequence numbers).
2030 Normally also deletes trailing whitespace after stripping such text.
2031 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
2034 (goto-char (point-min))
2035 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t
)
2036 (replace-match "" nil nil nil
1)
2037 (unless do-space
(delete-horizontal-space)))))
2039 ;; This code used to live in add-log.el, but this is a better place
2041 (defun fortran-current-defun ()
2042 "Function to use for `add-log-current-defun-function' in Fortran mode."
2044 ;; We must be inside function body for this to work.
2045 (fortran-beginning-of-subprogram)
2046 (let ((case-fold-search t
)) ; case-insensitive
2047 ;; Search for fortran subprogram start.
2048 (if (re-search-forward
2049 (concat "^[ \t]*\\(program\\|subroutine\\|function"
2050 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
2051 "\\(block[ \t]*data\\)\\)")
2052 (save-excursion (fortran-end-of-subprogram)
2055 (or (match-string-no-properties 2)
2057 ;; Move to EOL or before first left paren.
2058 (if (re-search-forward "[(\n]" nil t
)
2059 (progn (backward-char)
2060 (skip-chars-backward " \t"))
2062 ;; Use the name preceding that.
2063 (buffer-substring-no-properties (point) (progn (backward-sexp)
2069 ;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
2070 ;;; fortran.el ends here