(calendar-other-dates): New function.
[emacs.git] / lisp / progmodes / fortran.el
blobfdd8ff75a27b5da9362fe80725907010cabdb10e
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
5 ;; Free Software Foundation, Inc.
7 ;; Author: Michael D. Prange <prange@erl.mit.edu>
8 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Keywords: fortran, languages
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This mode is documented in the Emacs manual.
32 ;; Note that it is for editing Fortran77 or Fortran90 fixed source
33 ;; form. For editing Fortran 90 free format source, use `f90-mode'
34 ;; (f90.el). It is meant to support the GNU Fortran language
35 ;; implemented by g77 (its extensions to Fortran77 and
36 ;; interpretations, e.g. of blackslash in strings).
38 ;;; History:
40 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
42 ;; We acknowledge many contributions and valuable suggestions by
43 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
44 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
45 ;; Gary Sabot and Richard Stallman.
47 ;;; Code:
49 ;; Todo:
51 ;; * Tidy it all up (more)!
52 ;; * Implement insertion and removal of statement continuations in
53 ;; mixed f77/f90 style, with the first `&' past column 72 and the
54 ;; second in column 6.
55 ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
57 ;; silence compiler
58 (defvar dabbrev-case-fold-search)
59 (defvar gud-find-expr-function)
60 (defvar imenu-case-fold-search)
61 (defvar imenu-syntax-alist)
62 (defvar comment-region-function)
63 (defvar uncomment-region-function)
65 (defgroup fortran nil
66 "Major mode for editing fixed format Fortran code."
67 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
68 :link '(custom-manual "(emacs)Fortran")
69 :group 'languages)
71 (defgroup fortran-indent nil
72 "Indentation variables in Fortran mode."
73 :prefix "fortran-"
74 :group 'fortran)
76 (defgroup fortran-comment nil
77 "Comment-handling variables in Fortran mode."
78 :prefix "fortran-"
79 :group 'fortran)
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."
87 :type 'boolean
88 :group 'fortran-indent)
89 (put 'fortran-tab-mode-default 'safe-local-variable 'booleanp)
91 ;; TODO add more detail of what tab mode is to doc string.
92 (defcustom fortran-tab-mode-string
93 (propertize "/t" 'help-echo "This buffer is in Fortran TAB mode"
94 'mouse-face 'mode-line-highlight
95 'local-map
96 (make-mode-line-mouse-map 'mouse-1
97 (lambda ()
98 (interactive)
99 (describe-variable
100 'fortran-tab-mode-string))))
101 "String to appear in mode line in TAB format buffers."
102 :type 'string
103 :group 'fortran-indent)
104 (put 'fortran-tab-mode-string 'risky-local-variable t)
106 (defcustom fortran-do-indent 3
107 "Extra indentation applied to DO blocks."
108 :type 'integer
109 :group 'fortran-indent)
110 (put 'fortran-do-indent 'safe-local-variable 'integerp)
112 (defcustom fortran-if-indent 3
113 "Extra indentation applied to IF, SELECT CASE and WHERE blocks."
114 :type 'integer
115 :group 'fortran-indent)
116 (put 'fortran-if-indent 'safe-local-variable 'integerp)
118 (defcustom fortran-structure-indent 3
119 "Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
120 :type 'integer
121 :group 'fortran-indent)
122 (put 'fortran-structure-indent 'safe-local-variable 'integerp)
124 (defcustom fortran-continuation-indent 5
125 "Extra indentation applied to continuation lines."
126 :type 'integer
127 :group 'fortran-indent)
128 (put 'fortran-continuation-indent 'safe-local-variable 'integerp)
130 (defcustom fortran-comment-indent-style 'fixed
131 "How to indent comments.
132 nil forces comment lines not to be touched;
133 `fixed' indents to `fortran-comment-line-extra-indent' columns beyond
134 `fortran-minimum-statement-indent-fixed' (if `indent-tabs-mode' nil), or
135 `fortran-minimum-statement-indent-tab' (if `indent-tabs-mode' non-nil);
136 `relative' indents to current Fortran indentation plus
137 `fortran-comment-line-extra-indent'."
138 :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative))
139 :group 'fortran-indent)
140 (put 'fortran-comment-indent 'safe-local-variable
141 (lambda (value) (memq value '(nil fixed relative))))
143 (defcustom fortran-comment-line-extra-indent 0
144 "Amount of extra indentation for text within full-line comments."
145 :type 'integer
146 :group 'fortran-indent
147 :group 'fortran-comment)
148 (put 'fortran-comment-line-extra-indent 'safe-local-variable 'integerp)
150 (defcustom fortran-comment-line-start "C"
151 "Delimiter inserted to start new full-line comment.
152 You might want to change this to \"*\", for instance."
153 :version "21.1"
154 :type 'string
155 :group 'fortran-comment)
156 (put 'fortran-comment-line-start 'safe-local-variable 'stringp)
158 ;; This used to match preprocessor lines too, but that messes up
159 ;; filling and doesn't seem to be necessary.
160 (defcustom fortran-comment-line-start-skip
161 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
162 "Regexp to match the start of a full-line comment."
163 :version "21.1"
164 :type 'regexp
165 :group 'fortran-comment)
166 (put 'fortran-comment-line-start-skip 'safe-local-variable 'stringp)
168 (defcustom fortran-directive-re
169 "^[ \t]*#.*"
170 "Regexp to match a directive line.
171 The matching text will be fontified with `font-lock-keyword-face'.
172 The matching line will be given zero indentation."
173 :version "22.1"
174 :type 'regexp
175 :group 'fortran-indent)
176 (put 'fortran-directive-re 'safe-local-variable 'stringp)
178 (defcustom fortran-minimum-statement-indent-fixed 6
179 "Minimum statement indentation for fixed format continuation style."
180 :type 'integer
181 :group 'fortran-indent)
182 (put 'fortran-minimum-statement-indent-fixed 'safe-local-variable 'integerp)
184 (defcustom fortran-minimum-statement-indent-tab (max tab-width 6)
185 "Minimum statement indentation for TAB format continuation style."
186 :type 'integer
187 :group 'fortran-indent)
188 (put 'fortran-minimum-statement-indent-tab 'safe-local-variable 'integerp)
190 ;; Note that this is documented in the v18 manuals as being a string
191 ;; of length one rather than a single character.
192 ;; The code in this file accepts either format for compatibility.
193 (defcustom fortran-comment-indent-char " "
194 "Single-character string inserted for Fortran comment indentation.
195 Normally a space."
196 :type 'string
197 :group 'fortran-comment)
198 (put 'fortran-comment-indent-char 'safe-local-variable
199 (lambda (value) (or (characterp value)
200 (and (stringp value)
201 (= (length value) 1)))))
203 (defcustom fortran-line-number-indent 1
204 "Maximum indentation for Fortran line numbers.
205 5 means right-justify them within their five-column field."
206 :type 'integer
207 :group 'fortran-indent)
208 (put 'fortran-line-number-indent 'safe-local-variable 'integerp)
210 (defcustom fortran-check-all-num-for-matching-do nil
211 "Non-nil causes all numbered lines to be treated as possible DO loop ends."
212 :type 'boolean
213 :group 'fortran)
214 (put 'fortran-check-all-num-for-matching-do 'safe-local-variable 'booleanp)
216 (defcustom fortran-blink-matching-if nil
217 "Non-nil causes \\[fortran-indent-line] on ENDIF to blink on matching IF.
218 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
219 :type 'boolean
220 :group 'fortran)
221 (put 'fortran-blink-matching-if 'safe-local-variable 'booleanp)
223 (defcustom fortran-continuation-string "$"
224 "Single-character string used for Fortran continuation lines.
225 In fixed format continuation style, this character is inserted in
226 column 6 by \\[fortran-split-line] to begin a continuation line.
227 Also, if \\[fortran-indent-line] finds this at the beginning of a
228 line, it will convert the line into a continuation line of the
229 appropriate style. Normally $."
230 :type 'string
231 :group 'fortran)
232 (put 'fortran-continuation-string 'safe-local-variable
233 (lambda (value) (and (stringp value)
234 (= (length value) 1))))
236 (defcustom fortran-comment-region "c$$$"
237 "String inserted by \\[fortran-comment-region] at start of each \
238 line in region."
239 :type 'string
240 :group 'fortran-comment)
241 (put 'fortran-comment-region 'safe-local-variable 'stringp)
243 (defcustom fortran-electric-line-number t
244 "Non-nil causes line numbers to be moved to the correct column as typed."
245 :type 'boolean
246 :group 'fortran)
247 (put 'fortran-electric-line-number 'safe-local-variable 'booleanp)
249 ;; TODO use fortran-line-length, somehow.
250 (defcustom fortran-column-ruler-fixed
251 "0 4 6 10 20 30 40 5\
252 0 60 70\n\
253 \[ ]|{ | | | | | | | | \
254 \| | | | |}\n"
255 "String displayed above current line by \\[fortran-column-ruler].
256 This variable is used in fixed format mode.
257 See the variable `fortran-column-ruler-tab' for TAB format mode."
258 :type 'string
259 :group 'fortran)
260 (put 'fortran-column-ruler-fixed 'safe-local-variable 'stringp)
262 ;; TODO use fortran-line-length, somehow.
263 (defcustom fortran-column-ruler-tab
264 "0 810 20 30 40 5\
265 0 60 70\n\
266 \[ ]| { | | | | | | | | \
267 \| | | | |}\n"
268 "String displayed above current line by \\[fortran-column-ruler].
269 This variable is used in TAB format mode.
270 See the variable `fortran-column-ruler-fixed' for fixed format mode."
271 :type 'string
272 :group 'fortran)
273 (put 'fortran-column-ruler-tab 'safe-local-variable 'stringp)
275 (defcustom fortran-analyze-depth 100
276 "Number of lines to scan to identify fixed or TAB format style."
277 :type 'integer
278 :group 'fortran)
279 (put 'fortran-analyze-depth 'safe-local-variable 'integerp)
281 (defcustom fortran-break-before-delimiters t
282 "Non-nil causes filling to break lines before delimiters.
283 Delimiters are characters matching the regexp `fortran-break-delimiters-re'."
284 :type 'boolean
285 :group 'fortran)
286 (put 'fortran-break-before-delimiters 'safe-local-variable 'booleanp)
288 ;; TODO 0 as no-limit, as per g77.
289 (defcustom fortran-line-length 72
290 "Maximum number of characters in a line of fixed-form Fortran code.
291 Characters beyond this point are treated as comments. Setting
292 this variable directly (after fortran mode is loaded) does not
293 take effect. Use either \\[customize] (which affects all Fortran
294 buffers and the default) or the function
295 `fortran-line-length' (which can also operate on just the current
296 buffer). This corresponds to the g77 compiler option
297 `-ffixed-line-length-N'."
298 :type 'integer
299 :initialize 'custom-initialize-default
300 :set (lambda (symbol value)
301 ;; Do all fortran buffers, and the default.
302 (fortran-line-length value t))
303 :version "23.1"
304 :group 'fortran)
306 (put 'fortran-line-length 'safe-local-variable 'integerp)
307 (make-variable-buffer-local 'fortran-line-length)
309 (defcustom fortran-mode-hook nil
310 "Hook run when entering Fortran mode."
311 :type 'hook
312 :group 'fortran)
315 (defconst fortran-break-delimiters-re "[-+*/><=, \t]"
316 "Regexp matching delimiter characters at which lines may be broken.
317 There are certain tokens comprised entirely of characters
318 matching this regexp that should not be split, and these are
319 specified by the constant `fortran-no-break-re'.")
321 ;; The ">=", etc F77 extensions are supported by g77.
322 (defconst fortran-no-break-re
323 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=") 'paren)
324 "Regexp specifying where not to break lines when filling.
325 This regexp matches certain tokens comprised entirely of
326 characters matching the regexp `fortran-break-delimiters-re' that should
327 not be split by filling. Each element is assumed to be two
328 characters long.")
330 (defconst fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*("
331 "Regexp matching the start of an IF statement.")
333 (defconst fortran-end-prog-re1
334 "end\
335 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
336 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?"
337 "Regexp possibly matching the end of a subprogram.")
339 (defconst fortran-end-prog-re
340 (concat "^[ \t0-9]*" fortran-end-prog-re1)
341 "Regexp possibly matching the end of a subprogram, from the line start.
342 See also `fortran-end-prog-re1'.")
344 (defconst fortran-type-types
345 (concat "\\<"
346 (mapconcat 'identity ; " " -> "[ \t]*"
347 (split-string
348 (regexp-opt
349 (let ((simple-types
350 '("character" "byte" "integer" "logical"
351 "none" "real" "complex"
352 "double precision" "double complex"))
353 (structured-types '("structure" "union" "map"))
354 (other-types '("record" "dimension"
355 "parameter" "common" "save"
356 "external" "intrinsic" "data"
357 "equivalence")))
358 (append
359 (mapcar (lambda (x) (concat "implicit " x))
360 simple-types)
361 simple-types
362 (mapcar (lambda (x) (concat "end " x))
363 structured-types)
364 structured-types
365 other-types)) 'paren))
366 "[ \t]*") "\\>")
367 "Regexp matching Fortran types.")
369 (defvar fortran-font-lock-keywords-1
370 ;; Program, subroutine and function declarations, plus calls.
371 '(("\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|\
372 program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?"
373 (1 font-lock-keyword-face)
374 (2 font-lock-function-name-face nil t)))
375 "Subdued level highlighting for Fortran mode.")
377 (defvar fortran-font-lock-keywords-2
378 (append fortran-font-lock-keywords-1
379 (list
380 ;; Fontify all type specifiers (must be first - see below).
381 (cons fortran-type-types 'font-lock-type-face)
382 ;; Builtin keywords (except logical, do and goto - see below).
383 (concat "\\<" (regexp-opt
384 '("continue" "format" "end" "enddo"
385 "if" "then" "else" "endif" "elseif"
386 "while" "inquire" "stop" "return"
387 "include" "open" "close" "read"
388 "write" "format" "print" "select" "case"
389 "cycle" "exit" "rewind" "backspace"
390 "where" "elsewhere")
391 'paren) "\\>")
392 ;; Builtin operators.
393 (concat "\\." (regexp-opt
394 '("and" "or" "not" "lt" "le" "eq" "ge"
395 "gt" "ne" "true" "false")
396 'paren) "\\.")
397 ;; do/goto keywords and targets, and goto tags.
398 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
399 (1 font-lock-keyword-face)
400 (2 font-lock-constant-face nil t))
401 '("^ *\\([0-9]+\\)" . font-lock-constant-face)))
402 "Medium level highlighting for Fortran mode.")
404 (defvar fortran-font-lock-keywords-3
405 (append
406 fortran-font-lock-keywords-1
407 ;; All type specifiers plus their declared items.
408 (list
409 (list (concat fortran-type-types "[ \t(/]*\\(*\\)?")
410 ;; Type specifier.
411 '(1 font-lock-type-face)
412 ;; Declaration item (or just /.../ block name).
413 `(font-lock-match-c-style-declaration-item-and-skip-to-next
414 ;; Start after any *(...) expression.
415 (condition-case nil
416 (and (match-beginning ,(1+ (regexp-opt-depth
417 fortran-type-types)))
418 (forward-sexp)
419 (forward-sexp))
420 (error nil))
421 ;; No need to clean up.
423 ;; Fontify as a variable name, functions fontified elsewhere.
424 (1 font-lock-variable-name-face nil t))))
425 ;; Things extra to `fortran-font-lock-keywords-3' (must be done first).
426 (list
427 ;; Goto-like `err=label'/`end=label' in read/write statements.
428 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
429 (1 font-lock-keyword-face) (4 font-lock-constant-face nil t))
430 ;; Standard continuation character and in a TAB-formatted line.
431 '("^ \\{5\\}\\([^ 0\n]\\)" 1 font-lock-string-face)
432 '("^\t\\([1-9]\\)" 1 font-lock-string-face))
433 `((,fortran-directive-re (0 font-lock-keyword-face t)))
434 ;; `fortran-font-lock-keywords-2' without types (see above).
435 (cdr (nthcdr (length fortran-font-lock-keywords-1)
436 fortran-font-lock-keywords-2)))
437 "Gaudy level highlighting for Fortran mode.")
439 (defvar fortran-font-lock-keywords-4
440 (append fortran-font-lock-keywords-3
441 (list (list
442 (concat "\\<"
443 (regexp-opt
444 '("int" "ifix" "idint" "real" "float" "sngl"
445 "dble" "cmplx" "ichar" "char" "aint" "dint"
446 "anint" "dnint" "nint" "idnint" "iabs" "abs"
447 "dabs" "cabs" "mod" "amod" "dmod" "isign"
448 "sign" "dsign" "idim" "dim" "ddim" "dprod"
449 "max" "max0" "amax1" "dmax1" "amax0" "max1"
450 "min" "min0" "amin1" "dmin1" "amin0" "min1"
451 "len" "index" "lge" "lgt" "lle" "llt" "aimag"
452 "conjg" "sqrt" "dsqrt" "csqrt" "exp" "dexp"
453 "cexp" "log" "alog" "dlog" "clog" "log10"
454 "alog10" "dlog10" "sin" "dsin" "csin" "cos"
455 "dcos" "ccos" "tan" "dtan" "asin" "dasin"
456 "acos" "dacos" "atan" "datan" "atan2" "datan2"
457 "sinh" "dsinh" "cosh" "dcosh" "tanh" "dtanh")
458 'paren) "[ \t]*(") '(1 font-lock-builtin-face))))
459 "Maximum highlighting for Fortran mode.
460 Consists of level 3 plus all other intrinsics not already highlighted.")
462 ;; Comments are real pain in Fortran because there is no way to
463 ;; represent the standard comment syntax in an Emacs syntax table.
464 ;; (We can do so for F90-style). Therefore an unmatched quote in a
465 ;; standard comment will throw fontification off on the wrong track.
466 ;; So we do syntactic fontification with regexps.
467 (defun fortran-font-lock-syntactic-keywords ()
468 "Return a value for `font-lock-syntactic-keywords' in Fortran mode.
469 This varies according to the value of `fortran-line-length'.
470 This is used to fontify fixed-format Fortran comments."
471 `(("^[cd\\*]" 0 (11))
472 (,(format "^[^cd\\*\t\n].\\{%d\\}\\([^\n]+\\)" (1- fortran-line-length))
473 1 (11))))
475 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
476 "Default expressions to highlight in Fortran mode.")
478 (defvar fortran-imenu-generic-expression
479 ;; These patterns could be confused by sequence nos. in cols 72+ and
480 ;; don't allow continuations everywhere.
481 (list
482 (list
484 ;; [This will be fooled by `end function' allowed by G77. Also,
485 ;; it assumes sensible whitespace is employed.]
486 (concat
487 ;; leading whitespace:
488 "^\\s-+\\("
489 ;; function declaration with optional type, e.g. `real',
490 ;; `real*4', character(*), `double precision':
491 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
492 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
493 ;; Possible statement continuation:
494 "[ \t" fortran-continuation-string "]+"
495 ;; Variable to index:
496 "\\(\\sw+\\)")
498 ;; Un-named block data.
499 '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
500 "Value for `imenu-generic-expression' in Fortran mode.")
503 ;; Hideshow support.
504 (defconst fortran-blocks-re
505 (concat "block[ \t]*data\\|select[ \t]*case\\|"
506 (regexp-opt '("do" "if" "interface" "function" "map" "program"
507 "structure" "subroutine" "union" "where")))
508 "Regexp potentially indicating the start or end of a Fortran \"block\".
509 Omits naked END statements, and DO-loops closed by anything other
510 than ENDDO.")
512 (defconst fortran-end-block-re
513 ;; Do-loops terminated by things other than ENDDO cannot be handled
514 ;; with a regexp. This omission does not seem to matter to hideshow...
515 (concat "^[ \t0-9]*\\<end[ \t]*\\("
516 fortran-blocks-re
517 ;; Naked END statement.
518 "\\|!\\|$\\)")
519 "Regexp matching the end of a Fortran \"block\", from the line start.
520 Note that only ENDDO is handled for the end of a DO-loop. Used
521 in the Fortran entry in `hs-special-modes-alist'.")
523 (defconst fortran-start-block-re
524 (concat
525 "^[ \t0-9]*\\(" ; statement number
526 ;; Structure label for DO, IF, SELECT, WHERE.
527 "\\(\\(\\sw+[ \t]*:[ \t]*\\)?"
528 ;; IF blocks are a nuisance:
529 ;; IF ( ... ) foo is not a block, but a single statement.
530 ;; IF ( ... ) THEN can be split over multiple lines.
531 ;; [So can, eg, a DO WHILE (... ), but that is less common, I hope.]
532 ;; The regexp below allows for it to be split over at most 2 lines.
533 ;; That leads to the problem of not matching two consecutive IF
534 ;; statements as one, eg:
535 ;; IF ( ... ) foo
536 ;; IF ( ... ) THEN
537 ;; It simply is not possible to do this in a 100% correct fashion
538 ;; using a regexp - see the functions fortran-end-if,
539 ;; fortran-beginning-if for the hoops we have to go through.
540 ;; An alternative is to match on THEN at a line end, eg:
541 ;; ".*)[ \t]*then[ \t]*\\($\\|!\\)"
542 ;; This would also match ELSE branches, though. This does not seem
543 ;; right to me, because then one has neighbouring blocks that are
544 ;; not nested in each other.
545 "\\(if[ \t]*(\\(.*\\|"
546 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
547 "do\\|select[ \t]*case\\|where\\)\\)\\|"
548 (regexp-opt '("interface" "function" "map" "program"
549 "structure" "subroutine" "union"))
550 "\\|block[ \t]*data\\)[ \t]*")
551 "Regexp matching the start of a Fortran \"block\", from the line start.
552 A simple regexp cannot do this in fully correct fashion, so this
553 tries to strike a compromise between complexity and flexibility.
554 Used in the Fortran entry in `hs-special-modes-alist'.")
556 (add-to-list 'hs-special-modes-alist
557 `(fortran-mode ,fortran-start-block-re ,fortran-end-block-re
558 "^[cC*!]" fortran-end-of-block nil))
561 (defvar fortran-mode-syntax-table
562 (let ((table (make-syntax-table)))
563 ;; We might like `;' to be punctuation (g77 multi-statement
564 ;; lines), but that screws abbrevs.
565 (modify-syntax-entry ?\; "w" table)
566 (modify-syntax-entry ?\r " " table)
567 (modify-syntax-entry ?+ "." table)
568 (modify-syntax-entry ?- "." table)
569 (modify-syntax-entry ?= "." table)
570 (modify-syntax-entry ?* "." table)
571 (modify-syntax-entry ?/ "." table)
572 (modify-syntax-entry ?\' "\"" table)
573 (modify-syntax-entry ?\" "\"" table)
574 ;; Consistent with GNU Fortran's default -- see the manual.
575 ;; The F77 standard imposes no rule on this issue.
576 (modify-syntax-entry ?\\ "\\" table)
577 ;; This might be better as punctuation, as for C, but this way you
578 ;; can treat floating-point numbers as symbols.
579 (modify-syntax-entry ?. "_" table) ; e.g. `a.ne.b'
580 (modify-syntax-entry ?_ "_" table)
581 (modify-syntax-entry ?$ "_" table) ; esp. VMSisms
582 (modify-syntax-entry ?\! "<" table)
583 (modify-syntax-entry ?\n ">" table)
584 table)
585 "Syntax table used in Fortran mode.")
587 (defvar fortran-gud-syntax-table
588 (let ((st (make-syntax-table fortran-mode-syntax-table)))
589 (modify-syntax-entry ?\n "." st)
591 "Syntax table used to parse Fortran expressions for printing in GUD.")
593 (defvar fortran-mode-map
594 (let ((map (make-sparse-keymap)))
595 (define-key map ";" 'fortran-abbrev-start)
596 (define-key map "\C-c;" 'fortran-comment-region)
597 ;; The default comment-dwim does at least as much as this.
598 ;;; (define-key map "\M-;" 'fortran-indent-comment)
599 (define-key map "\M-\n" 'fortran-split-line)
600 (define-key map "\M-\C-n" 'fortran-end-of-block)
601 (define-key map "\M-\C-p" 'fortran-beginning-of-block)
602 (define-key map "\M-\C-q" 'fortran-indent-subprogram)
603 (define-key map "\C-c\C-w" 'fortran-window-create-momentarily)
604 (define-key map "\C-c\C-r" 'fortran-column-ruler)
605 (define-key map "\C-c\C-p" 'fortran-previous-statement)
606 (define-key map "\C-c\C-n" 'fortran-next-statement)
607 (define-key map "\C-c\C-d" 'fortran-join-line) ; like f90
608 (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation
609 (define-key map "0" 'fortran-electric-line-number)
610 (define-key map "1" 'fortran-electric-line-number)
611 (define-key map "2" 'fortran-electric-line-number)
612 (define-key map "3" 'fortran-electric-line-number)
613 (define-key map "4" 'fortran-electric-line-number)
614 (define-key map "5" 'fortran-electric-line-number)
615 (define-key map "6" 'fortran-electric-line-number)
616 (define-key map "7" 'fortran-electric-line-number)
617 (define-key map "8" 'fortran-electric-line-number)
618 (define-key map "9" 'fortran-electric-line-number)
620 (easy-menu-define fortran-menu map "Menu for Fortran mode."
621 `("Fortran"
622 ["Manual" (info "(emacs)Fortran")]
623 ("Customization"
624 ,(custom-menu-create 'fortran)
625 ["Set" Custom-set t]
626 ["Save" Custom-save t]
627 ["Reset to Current" Custom-reset-current t]
628 ["Reset to Saved" Custom-reset-saved t]
629 ["Reset to Standard Settings" Custom-reset-standard t]
631 "--"
632 ["Comment Region" fortran-comment-region mark-active]
633 ["Uncomment Region"
634 (fortran-comment-region (region-beginning) (region-end) 1)
635 mark-active]
636 ["Indent Region" indent-region mark-active]
637 ["Indent Subprogram" fortran-indent-subprogram t]
638 "--"
639 ["Beginning of Subprogram" fortran-beginning-of-subprogram t]
640 ["End of Subprogram" fortran-end-of-subprogram t]
641 ("Mark"
642 ["Subprogram" mark-defun t]
643 ["IF Block" fortran-mark-if t]
644 ["DO Block" fortran-mark-do t]
646 ["Narrow to Subprogram" narrow-to-defun t]
647 ["Widen" widen t]
648 "--"
649 ["Temporary column ruler" fortran-column-ruler t]
650 ;; May not be '72', depending on fortran-line-length, but this
651 ;; seems ok for a menu item.
652 ["72-column window" fortran-window-create t]
653 ["Full Width Window"
654 (enlarge-window-horizontally (- (frame-width) (window-width)))
655 (not (window-full-width-p))]
656 ["Momentary 72-column window" fortran-window-create-momentarily t]
657 "--"
658 ["Break Line at Point" fortran-split-line t]
659 ["Join Line" fortran-join-line t]
660 ["Fill Statement/Comment" fill-paragraph t]
661 "--"
662 ["Toggle auto-fill" auto-fill-mode :selected auto-fill-function
663 :style toggle]
664 ["Toggle abbrev-mode" abbrev-mode :selected abbrev-mode
665 :style toggle]
666 ["Add imenu Menu" imenu-add-menubar-index
667 :active (not (lookup-key (current-local-map) [menu-bar index]))
668 :included (fboundp 'imenu-add-to-menubar)]))
669 map)
670 "Keymap used in Fortran mode.")
673 (defvar fortran-mode-abbrev-table
674 (progn
675 (define-abbrev-table 'fortran-mode-abbrev-table nil)
676 fortran-mode-abbrev-table)
677 "Abbrev table for Fortran mode.")
679 ;; Not in defvar because user abbrevs may be restored before this file loads.
680 (mapc
681 (lambda (e)
682 (condition-case nil
683 (define-abbrev fortran-mode-abbrev-table (car e) (cdr e) nil :count 0
684 :system t)
685 (wrong-number-of-arguments ; Emacs 22
686 (define-abbrev fortran-mode-abbrev-table (car e) (cdr e) nil 0 t))))
687 '((";au" . "automatic" )
688 (";b" . "byte" )
689 (";bd" . "block data" )
690 (";ch" . "character" )
691 (";cl" . "close" )
692 (";c" . "continue" )
693 (";cm" . "common" )
694 (";cx" . "complex" )
695 (";df" . "define" )
696 (";di" . "dimension" )
697 (";do" . "double" )
698 (";dc" . "double complex" )
699 (";dp" . "double precision" )
700 (";dw" . "do while" )
701 (";e" . "else" )
702 (";ed" . "enddo" )
703 (";el" . "elseif" )
704 (";en" . "endif" )
705 (";eq" . "equivalence" )
706 (";ew" . "endwhere" )
707 (";ex" . "external" )
708 (";ey" . "entry" )
709 (";f" . "format" )
710 (";fa" . ".false." )
711 (";fu" . "function" )
712 (";g" . "goto" )
713 (";im" . "implicit" )
714 (";ib" . "implicit byte" )
715 (";ic" . "implicit complex" )
716 (";ich" . "implicit character")
717 (";ii" . "implicit integer" )
718 (";il" . "implicit logical" )
719 (";ir" . "implicit real" )
720 (";inc" . "include" )
721 (";in" . "integer" )
722 (";intr" . "intrinsic" )
723 (";l" . "logical" )
724 (";n" . "namelist" )
725 (";o" . "open" ) ; was ;op
726 (";pa" . "parameter" )
727 (";pr" . "program" )
728 (";ps" . "pause" )
729 (";p" . "print" )
730 (";rc" . "record" )
731 (";re" . "real" )
732 (";r" . "read" )
733 (";rt" . "return" )
734 (";rw" . "rewind" )
735 (";s" . "stop" )
736 (";sa" . "save" )
737 (";st" . "structure" )
738 (";sc" . "static" )
739 (";su" . "subroutine" )
740 (";tr" . ".true." )
741 (";ty" . "type" )
742 (";vo" . "volatile" )
743 (";w" . "write" )
744 (";wh" . "where" )))
747 ;;;###autoload
748 (defun fortran-mode ()
749 "Major mode for editing Fortran code in fixed format.
750 For free format code, use `f90-mode'.
752 \\[fortran-indent-line] indents the current Fortran line correctly.
753 Note that DO statements must not share a common CONTINUE.
755 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\
756 Fortran keywords.
758 Key definitions:
759 \\{fortran-mode-map}
761 Variables controlling indentation style and extra features:
763 `fortran-comment-line-start'
764 To use comments starting with `!', set this to the string \"!\".
765 `fortran-do-indent'
766 Extra indentation within DO blocks (default 3).
767 `fortran-if-indent'
768 Extra indentation within IF blocks (default 3).
769 `fortran-structure-indent'
770 Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks.
771 (default 3)
772 `fortran-continuation-indent'
773 Extra indentation applied to continuation statements (default 5).
774 `fortran-comment-line-extra-indent'
775 Amount of extra indentation for text in full-line comments (default 0).
776 `fortran-comment-indent-style'
777 How to indent the text in full-line comments. Allowed values are:
778 nil don't change the indentation
779 fixed indent to `fortran-comment-line-extra-indent' beyond the
780 value of either
781 `fortran-minimum-statement-indent-fixed' (fixed format) or
782 `fortran-minimum-statement-indent-tab' (TAB format),
783 depending on the continuation format in use.
784 relative indent to `fortran-comment-line-extra-indent' beyond the
785 indentation for a line of code.
786 (default 'fixed)
787 `fortran-comment-indent-char'
788 Single-character string to be inserted instead of space for
789 full-line comment indentation (default \" \").
790 `fortran-minimum-statement-indent-fixed'
791 Minimum indentation for statements in fixed format mode (default 6).
792 `fortran-minimum-statement-indent-tab'
793 Minimum indentation for statements in TAB format mode (default 9).
794 `fortran-line-number-indent'
795 Maximum indentation for line numbers (default 1). A line number will
796 get less than this much indentation if necessary to avoid reaching
797 column 5.
798 `fortran-check-all-num-for-matching-do'
799 Non-nil causes all numbered lines to be treated as possible \"continue\"
800 statements (default nil).
801 `fortran-blink-matching-if'
802 Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement
803 to blink on the matching IF (or DO [WHILE]). (default nil)
804 `fortran-continuation-string'
805 Single-character string to be inserted in column 5 of a continuation
806 line (default \"$\").
807 `fortran-comment-region'
808 String inserted by \\[fortran-comment-region] at start of each line in
809 the region (default \"c$$$\").
810 `fortran-electric-line-number'
811 Non-nil causes line number digits to be moved to the correct column
812 as typed (default t).
813 `fortran-break-before-delimiters'
814 Non-nil causes lines to be broken before delimiters (default t).
816 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
817 with no args, if that value is non-nil."
818 (interactive)
819 (kill-all-local-variables)
820 (setq major-mode 'fortran-mode
821 mode-name "Fortran"
822 local-abbrev-table fortran-mode-abbrev-table)
823 (set-syntax-table fortran-mode-syntax-table)
824 (use-local-map fortran-mode-map)
825 (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
826 (set (make-local-variable 'indent-region-function)
827 (lambda (start end)
828 (let (fortran-blink-matching-if ; avoid blinking delay
829 indent-region-function)
830 (indent-region start end nil))))
831 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
832 ;; The syntax tables don't understand the column-0 comment-markers.
833 (set (make-local-variable 'comment-use-syntax) nil)
834 (set (make-local-variable 'comment-padding) "$$$")
835 (set (make-local-variable 'comment-start) fortran-comment-line-start)
836 (set (make-local-variable 'comment-start-skip)
837 ;; We can't reuse `fortran-comment-line-start-skip' directly because
838 ;; it contains backrefs whereas we need submatch-1 to end at the
839 ;; beginning of the comment delimiter.
840 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
841 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
842 (set (make-local-variable 'comment-indent-function) 'fortran-comment-indent)
843 (set (make-local-variable 'comment-region-function) 'fortran-comment-region)
844 (set (make-local-variable 'uncomment-region-function)
845 'fortran-uncomment-region)
846 (set (make-local-variable 'comment-insert-comment-function)
847 'fortran-indent-comment)
848 (set (make-local-variable 'abbrev-all-caps) t)
849 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
850 (set (make-local-variable 'indent-tabs-mode) (fortran-analyze-file-format))
851 (setq mode-line-process '(indent-tabs-mode fortran-tab-mode-string))
852 (set (make-local-variable 'fill-column) fortran-line-length)
853 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
854 (set (make-local-variable 'font-lock-defaults)
855 '((fortran-font-lock-keywords
856 fortran-font-lock-keywords-1
857 fortran-font-lock-keywords-2
858 fortran-font-lock-keywords-3
859 fortran-font-lock-keywords-4)
860 nil t ((?/ . "$/") ("_$" . "w"))
861 fortran-beginning-of-subprogram
862 (font-lock-syntactic-keywords
863 . (fortran-font-lock-syntactic-keywords))))
864 (set (make-local-variable 'imenu-case-fold-search) t)
865 (set (make-local-variable 'imenu-generic-expression)
866 fortran-imenu-generic-expression)
867 (set (make-local-variable 'imenu-syntax-alist) '(("_$" . "w")))
868 (set (make-local-variable 'beginning-of-defun-function)
869 #'fortran-beginning-of-subprogram)
870 (set (make-local-variable 'end-of-defun-function)
871 #'fortran-end-of-subprogram)
872 (set (make-local-variable 'add-log-current-defun-function)
873 #'fortran-current-defun)
874 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
875 (set (make-local-variable 'gud-find-expr-function) 'fortran-gud-find-expr)
876 (add-hook 'hack-local-variables-hook 'fortran-hack-local-variables nil t)
877 (run-mode-hooks 'fortran-mode-hook))
880 (defun fortran-line-length (nchars &optional global)
881 "Set the length of fixed-form Fortran lines to NCHARS.
882 This normally only affects the current buffer, which must be in
883 Fortran mode. If the optional argument GLOBAL is non-nil, it
884 affects all Fortran buffers, and also the default."
885 (interactive "p")
886 (let (new)
887 (mapc (lambda (buff)
888 (with-current-buffer buff
889 (when (eq major-mode 'fortran-mode)
890 (setq fortran-line-length nchars
891 fill-column fortran-line-length
892 new (fortran-font-lock-syntactic-keywords))
893 ;; Refontify only if necessary.
894 (unless (equal new font-lock-syntactic-keywords)
895 (setq font-lock-syntactic-keywords
896 (fortran-font-lock-syntactic-keywords))
897 (if font-lock-mode (font-lock-mode 1))))))
898 (if global
899 (buffer-list)
900 (list (current-buffer))))
901 (if global
902 (setq-default fortran-line-length nchars))))
904 (defun fortran-hack-local-variables ()
905 "Fortran mode adds this to `hack-local-variables-hook'."
906 (fortran-line-length fortran-line-length))
908 (declare-function gud-find-c-expr "gud.el" nil)
910 (defun fortran-gud-find-expr ()
911 ;; Consider \n as punctuation (end of expression).
912 (with-syntax-table fortran-gud-syntax-table
913 (gud-find-c-expr)))
915 (defsubst fortran-comment-indent ()
916 "Return the indentation appropriate for the current comment line.
917 This is 0 for a line matching `fortran-comment-line-start-skip', else
918 the value of `comment-column' (leaving at least one space after code)."
919 (if (looking-at fortran-comment-line-start-skip) 0
920 (save-excursion
921 (skip-chars-backward " \t")
922 (max (1+ (current-column)) comment-column))))
924 (defun fortran-indent-comment ()
925 "Align or create comment on current line.
926 Existing comments of all types are recognized and aligned.
927 If the line has no comment, a side-by-side comment is inserted and aligned,
928 if the value of `comment-start' is not nil and allows such comments.
929 Otherwise, a separate-line comment is inserted, on this line
930 or on a new line inserted before this line if this line is not blank."
931 (interactive "*")
932 (beginning-of-line)
933 ;; Recognize existing comments of either kind.
934 (cond ((fortran-find-comment-start-skip 'all)
935 (goto-char (match-beginning 0))
936 (if (bolp)
937 (fortran-indent-line)
938 (unless (= (current-column) (fortran-comment-indent))
939 (delete-horizontal-space)
940 (indent-to (fortran-comment-indent)))))
941 ;; No existing comment.
942 ;; If side-by-side comments are defined, insert one,
943 ;; unless line is now blank.
944 ((and comment-start (not (looking-at "[ \t]*$"))
945 (string-match comment-start-skip (concat " " comment-start)))
946 (end-of-line)
947 (delete-horizontal-space)
948 (indent-to (fortran-comment-indent))
949 (insert comment-start))
950 ;; Else insert separate-line comment, making a new line if nec.
952 (if (looking-at "^[ \t]*$")
953 (delete-horizontal-space)
954 (beginning-of-line)
955 (insert ?\n)
956 (forward-char -1))
957 (insert fortran-comment-line-start)
958 (insert-char (if (stringp fortran-comment-indent-char)
959 (aref fortran-comment-indent-char 0)
960 fortran-comment-indent-char)
961 (- (fortran-calculate-indent) (current-column))))))
963 (defun fortran-comment-region (beg-region end-region arg)
964 "Comment every line in the region.
965 Inserts the string variable `fortran-comment-region' at the beginning of
966 every line in the region.
967 BEG-REGION and END-REGION specify the region boundaries.
968 With non-nil ARG, uncomments the region."
969 (interactive "*r\nP")
970 (let ((end-region-mark (copy-marker end-region))
971 (save-point (point-marker)))
972 (goto-char beg-region)
973 (beginning-of-line)
974 (if arg
975 (let ((com (regexp-quote fortran-comment-region))) ; uncomment
976 (if (looking-at com)
977 (delete-region (point) (match-end 0)))
978 (while (and (zerop (forward-line 1))
979 (< (point) end-region-mark))
980 (if (looking-at com)
981 (delete-region (point) (match-end 0)))))
982 (insert fortran-comment-region) ; comment
983 (while (and (zerop (forward-line 1))
984 (< (point) end-region-mark))
985 (insert fortran-comment-region)))
986 (goto-char save-point)
987 (set-marker end-region-mark nil)
988 (set-marker save-point nil)))
990 ;; uncomment-region calls this with 3 args.
991 (defun fortran-uncomment-region (start end &optional ignored)
992 "Uncomment every line in the region."
993 (fortran-comment-region start end t))
996 (defun fortran-abbrev-start ()
997 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
998 Any other key combination is executed normally."
999 (interactive "*")
1000 (insert last-command-char)
1001 (let* ((event (if (fboundp 'next-command-event) ; XEmacs
1002 (next-command-event)
1003 (read-event)))
1004 (char (if (fboundp 'event-to-character)
1005 (event-to-character event) event)))
1006 ;; Insert char if not equal to `?', or if abbrev-mode is off.
1007 (if (and abbrev-mode (or (eq char ??) (eq char help-char)
1008 (memq event help-event-list)))
1009 (fortran-abbrev-help)
1010 (push event unread-command-events))))
1012 (defun fortran-abbrev-help ()
1013 "List the currently defined abbrevs in Fortran mode."
1014 (interactive)
1015 (message "Listing abbrev table...")
1016 (display-buffer (fortran-prepare-abbrev-list-buffer))
1017 (message "Listing abbrev table...done"))
1019 (defun fortran-prepare-abbrev-list-buffer ()
1020 "Create a buffer listing the Fortran mode abbreviations."
1021 (with-current-buffer (get-buffer-create "*Abbrevs*")
1022 (erase-buffer)
1023 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
1024 (goto-char (point-min))
1025 (set-buffer-modified-p nil)
1026 (edit-abbrevs-mode))
1027 (get-buffer-create "*Abbrevs*"))
1029 (defun fortran-column-ruler ()
1030 "Insert a column ruler momentarily above current line, till next keystroke.
1031 The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed
1032 format mode, and `fortran-column-ruler-tab' in TAB format mode.
1033 The next key typed is executed unless it is SPC."
1034 (interactive)
1035 (momentary-string-display
1036 (if indent-tabs-mode
1037 fortran-column-ruler-tab
1038 fortran-column-ruler-fixed)
1039 (save-excursion
1040 (beginning-of-line)
1041 (if (eq (window-start (selected-window))
1042 (window-point (selected-window)))
1043 (line-beginning-position 2)
1044 (point)))
1045 nil "Type SPC or any command to erase ruler."))
1047 (defun fortran-window-create ()
1048 "Make the window `fortran-line-length' (default 72) columns wide.
1049 See also `fortran-window-create-momentarily'."
1050 (interactive)
1051 (let ((window-min-width 2))
1052 (unless (window-full-width-p)
1053 (enlarge-window-horizontally (- (frame-width)
1054 (window-width) 1)))
1055 (let* ((window-edges (window-edges))
1056 (scroll-bar-width (- (nth 2 window-edges)
1057 (car window-edges)
1058 (window-width))))
1059 (split-window-horizontally (+ fortran-line-length scroll-bar-width)))
1060 (other-window 1)
1061 (switch-to-buffer " fortran-window-extra" t)
1062 (select-window (previous-window))))
1064 (defun fortran-window-create-momentarily (&optional arg)
1065 "Momentarily make the window `fortran-line-length' (default 72) columns wide.
1066 Optional ARG non-nil and non-unity disables the momentary feature.
1067 See also `fortran-window-create'."
1068 (interactive "p")
1069 (if (or (not arg)
1070 (= arg 1))
1071 (save-window-excursion
1072 (progn
1073 (condition-case nil
1074 (fortran-window-create)
1075 (error (error "No room for Fortran window")))
1076 (message "Type SPC to continue editing.")
1077 (let ((char (read-event)))
1078 (or (equal char ?\s)
1079 (setq unread-command-events (list char))))))
1080 (fortran-window-create)))
1082 (defun fortran-split-line ()
1083 "Break line at point and insert continuation marker and alignment."
1084 (interactive "*")
1085 (delete-horizontal-space)
1086 (if (save-excursion
1087 (let ((pos (point)))
1088 (beginning-of-line)
1089 (and (fortran-find-comment-start-skip 'all)
1090 (< (match-beginning 0) pos))))
1091 (insert ?\n (match-string 0))
1092 (if indent-tabs-mode
1093 (insert ?\n ?\t (fortran-numerical-continuation-char))
1094 (insert "\n " fortran-continuation-string))) ; space after \n important
1095 (fortran-indent-line)) ; when cont string is C, c or *
1097 (defun fortran-remove-continuation ()
1098 "Delete any Fortran continuation characters at point.
1099 Returns t if anything actually deleted."
1100 (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)")
1101 (replace-match "")
1102 (delete-indentation)
1105 (defun fortran-join-line (arg)
1106 "Join current line to the previous one and re-indent.
1107 With a prefix argument, repeat this operation that many times.
1108 If the prefix argument ARG is negative, join the next -ARG lines.
1109 Continuation lines are correctly handled."
1110 (interactive "*p")
1111 (save-excursion
1112 (when (> 0 arg)
1113 (setq arg (- arg))
1114 (forward-line arg))
1115 (while (not (zerop arg))
1116 (beginning-of-line)
1117 (or (fortran-remove-continuation)
1118 (delete-indentation))
1119 (setq arg (1- arg)))
1120 (fortran-indent-line)))
1122 (defun fortran-numerical-continuation-char ()
1123 "Return a digit for tab-digit style of continuation lines.
1124 If previous line is a tab-digit continuation line, return that digit
1125 plus one, otherwise return 1. Zero not allowed."
1126 (save-excursion
1127 (forward-line -1)
1128 (if (looking-at "\t[1-9]")
1129 (+ ?1 (% (- (char-after (1+ (point))) ?0) 9))
1130 ?1)))
1132 (put 'fortran-electric-line-number 'delete-selection t)
1133 (defun fortran-electric-line-number (arg)
1134 "Self insert, but if part of a Fortran line number indent it automatically.
1135 Auto-indent does not happen if a numeric ARG is used."
1136 (interactive "*P")
1137 (if (or arg (not fortran-electric-line-number))
1138 (if arg
1139 (self-insert-command (prefix-numeric-value arg))
1140 (self-insert-command 1))
1141 (if (or (and (= 5 (current-column))
1142 (save-excursion
1143 (beginning-of-line)
1144 ;; In col 5 with only spaces to the left.
1145 (looking-at " \\{5\\}")))
1146 (and (= (if indent-tabs-mode
1147 fortran-minimum-statement-indent-tab
1148 fortran-minimum-statement-indent-fixed) (current-column))
1149 ;; In col 8 with a single tab to the left.
1150 (eq ?\t (char-after (line-beginning-position)))
1151 (not (or (eq last-command 'fortran-indent-line)
1152 (eq last-command
1153 'fortran-indent-new-line))))
1154 (save-excursion
1155 (re-search-backward "[^ \t0-9]"
1156 (line-beginning-position)
1157 t)) ; not a line number
1158 (looking-at "[0-9]")) ; within a line number
1159 (self-insert-command (prefix-numeric-value arg))
1160 (skip-chars-backward " \t")
1161 (insert last-command-char)
1162 (fortran-indent-line))))
1165 (defun fortran-check-end-prog-re ()
1166 "Check a preliminary match against `fortran-end-prog-re'."
1167 ;; Having got a possible match for the subprogram end, we need a
1168 ;; match of whitespace, avoiding possible column 73+ stuff.
1169 (save-match-data
1170 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
1171 (buffer-substring (match-end 0)
1172 (min (line-end-position)
1173 (+ fortran-line-length
1174 (line-beginning-position)))))))
1176 ;; Note that you can't just check backwards for `subroutine' &c in
1177 ;; case of un-marked main programs not at the start of the file.
1178 (defun fortran-beginning-of-subprogram ()
1179 "Move point to the beginning of the current Fortran subprogram."
1180 (interactive)
1181 (save-match-data
1182 (let ((case-fold-search t))
1183 (beginning-of-line -1)
1184 (if (catch 'ok
1185 (while (re-search-backward fortran-end-prog-re nil 'move)
1186 (if (fortran-check-end-prog-re)
1187 (throw 'ok t))))
1188 (forward-line)))))
1190 (defun fortran-end-of-subprogram ()
1191 "Move point to the end of the current Fortran subprogram."
1192 (interactive)
1193 (save-match-data
1194 (let ((case-fold-search t))
1195 (if (save-excursion ; on END
1196 (beginning-of-line)
1197 (and (looking-at fortran-end-prog-re)
1198 (fortran-check-end-prog-re)))
1199 (forward-line)
1200 (beginning-of-line 2)
1201 (catch 'ok
1202 (while (re-search-forward fortran-end-prog-re nil 'move)
1203 (if (fortran-check-end-prog-re)
1204 (throw 'ok t))))
1205 (goto-char (match-beginning 0))
1206 (forward-line)))))
1208 (defun fortran-previous-statement ()
1209 "Move point to beginning of the previous Fortran statement.
1210 Returns 'first-statement if that statement is the first
1211 non-comment Fortran statement in the file, and nil otherwise.
1212 Directive lines are treated as comments."
1213 (interactive)
1214 (let (not-first-statement continue-test)
1215 (beginning-of-line)
1216 (setq continue-test
1217 (and
1218 (not (looking-at fortran-comment-line-start-skip))
1219 (not (looking-at fortran-directive-re))
1220 (or (looking-at
1221 (concat "[ \t]*"
1222 (regexp-quote fortran-continuation-string)))
1223 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1224 (while (and (setq not-first-statement (zerop (forward-line -1)))
1225 (or (looking-at fortran-comment-line-start-skip)
1226 (looking-at fortran-directive-re)
1227 (looking-at
1228 (concat "[ \t]*"
1229 (regexp-quote fortran-continuation-string)))
1230 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1231 (looking-at (concat "[ \t]*" comment-start-skip)))))
1232 (cond ((and continue-test
1233 (not not-first-statement))
1234 (message "Incomplete continuation statement."))
1235 (continue-test
1236 (fortran-previous-statement))
1237 ((not not-first-statement)
1238 'first-statement))))
1240 (defun fortran-next-statement ()
1241 "Move point to beginning of the next Fortran statement.
1242 Returns 'last-statement if that statement is the last
1243 non-comment Fortran statement in the file, and nil otherwise.
1244 Directive lines are treated as comments."
1245 (interactive)
1246 (let (not-last-statement)
1247 (beginning-of-line)
1248 (while (and (setq not-last-statement
1249 (and (zerop (forward-line 1))
1250 (not (eobp))))
1251 (or (looking-at fortran-comment-line-start-skip)
1252 (looking-at fortran-directive-re)
1253 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1254 (looking-at (concat "[ \t]*" comment-start-skip)))))
1255 (if (not not-last-statement)
1256 'last-statement)))
1258 (defun fortran-looking-at-if-then ()
1259 "Return non-nil if at the start of a line with an IF ... THEN statement."
1260 ;; cf f90-looking-at-if-then.
1261 (let ((p (point))
1262 (i (fortran-beginning-if)))
1263 (if i
1264 (save-excursion
1265 (goto-char i)
1266 (beginning-of-line)
1267 (= (point) p)))))
1269 ;; Used in hs-special-modes-alist.
1270 (defun fortran-end-of-block (&optional num)
1271 "Move point forward to the end of the current code block.
1272 With optional argument NUM, go forward that many balanced blocks.
1273 If NUM is negative, go backward to the start of a block. Does
1274 not check for consistency of block types. Interactively, pushes
1275 mark before moving point."
1276 (interactive "p")
1277 (if (interactive-p) (push-mark (point) t))
1278 (and num (< num 0) (fortran-beginning-of-block (- num)))
1279 (let ((case-fold-search t)
1280 (count (or num 1)))
1281 (end-of-line)
1282 (while (and (> count 0)
1283 (re-search-forward
1284 (concat "\\(" fortran-blocks-re
1285 (if fortran-check-all-num-for-matching-do
1286 "\\|^[ \t]*[0-9]+" "")
1287 "\\|continue\\|end\\)\\>")
1288 nil 'move))
1289 (beginning-of-line)
1290 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1291 (fortran-looking-at-if-then)
1292 (looking-at fortran-start-block-re))
1293 (setq count (1+ count))
1294 (if (or (looking-at fortran-end-block-re)
1295 (and (or (looking-at "^[0-9 \t]*continue")
1296 (and fortran-check-all-num-for-matching-do
1297 (looking-at "[ \t]*[0-9]+")))
1298 (fortran-check-for-matching-do)))
1299 (setq count (1- count))))
1300 (end-of-line))
1301 (if (> count 0) (error "Missing block end"))))
1303 (defun fortran-beginning-of-block (&optional num)
1304 "Move point backwards to the start of the current code block.
1305 With optional argument NUM, go backward that many balanced
1306 blocks. If NUM is negative, go forward to the end of a block.
1307 Does not check for consistency of block types. Interactively,
1308 pushes mark before moving point."
1309 (interactive "p")
1310 (if (interactive-p) (push-mark (point) t))
1311 (and num (< num 0) (fortran-end-of-block (- num)))
1312 (let ((case-fold-search t)
1313 (count (or num 1)))
1314 (beginning-of-line)
1315 (while (and (> count 0)
1316 (re-search-backward
1317 (concat "\\(" fortran-blocks-re
1318 (if fortran-check-all-num-for-matching-do
1319 "\\|^[ \t]*[0-9]+" "")
1320 "\\|continue\\|end\\)\\>")
1321 nil 'move))
1322 (beginning-of-line)
1323 (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re))
1324 (fortran-looking-at-if-then)
1325 (looking-at fortran-start-block-re))
1326 (setq count (1- count))
1327 (if (or (looking-at fortran-end-block-re)
1328 (and (or (looking-at "^[0-9 \t]*continue")
1329 (and fortran-check-all-num-for-matching-do
1330 (looking-at "[ \t]*[0-9]+")))
1331 (fortran-check-for-matching-do)))
1332 (setq count (1+ count)))))
1333 ;; Includes an un-named main program block.
1334 (if (> count 0) (error "Missing block start"))))
1337 (defun fortran-blink-match (regex keyword find-begin)
1338 "From a line matching REGEX, blink matching KEYWORD statement line.
1339 Use function FIND-BEGIN to match it."
1340 (let ((top-of-window (window-start))
1341 (end-point (point))
1342 (case-fold-search t)
1343 matching
1344 message)
1345 (when (save-excursion
1346 (beginning-of-line)
1347 (skip-chars-forward " \t0-9")
1348 (looking-at regex))
1349 (if (not (setq matching (funcall find-begin)))
1350 (setq message (concat "No matching " keyword "."))
1351 (if (< matching top-of-window)
1352 (save-excursion
1353 (goto-char matching)
1354 (beginning-of-line)
1355 (setq message
1356 (concat "Matches "
1357 (buffer-substring (point)
1358 (line-end-position)))))))
1359 (if message
1360 (message "%s" message)
1361 (goto-char matching)
1362 (sit-for blink-matching-delay)
1363 (goto-char end-point)))))
1365 (defun fortran-blink-matching-if ()
1366 "From an ENDIF or ELSE statement, blink the matching IF statement."
1367 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1368 "if" #'fortran-beginning-if))
1370 (defun fortran-blink-matching-do ()
1371 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1372 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
1374 (defun fortran-mark-do ()
1375 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1376 The marks are pushed."
1377 (interactive)
1378 (let (enddo-point do-point)
1379 (if (setq enddo-point (fortran-end-do))
1380 (if (not (setq do-point (fortran-beginning-do)))
1381 (message "No matching do.")
1382 (goto-char enddo-point)
1383 (push-mark)
1384 (goto-char do-point)))))
1386 (defun fortran-end-do ()
1387 "Search forward for first unmatched ENDDO.
1388 Return point or nil."
1389 (let ((case-fold-search t))
1390 (if (save-excursion (beginning-of-line)
1391 (skip-chars-forward " \t0-9")
1392 (looking-at "end[ \t]*do\\b"))
1393 ;; Sitting on one.
1394 (match-beginning 0)
1395 ;; Search for one.
1396 (save-excursion
1397 (let ((count 1))
1398 (while (and (not (zerop count))
1399 (not (eq (fortran-next-statement) 'last-statement))
1400 ;; Keep local to subprogram.
1401 (not (and (looking-at fortran-end-prog-re)
1402 (fortran-check-end-prog-re))))
1403 (skip-chars-forward " \t0-9")
1404 (cond ((looking-at "end[ \t]*do\\b")
1405 (setq count (1- count)))
1406 ((looking-at
1407 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1408 (setq count (1+ count)))))
1409 (and (zerop count)
1410 ;; All pairs accounted for.
1411 (point)))))))
1413 (defun fortran-beginning-do ()
1414 "Search backwards for first unmatched DO [WHILE].
1415 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1416 (let ((case-fold-search t)
1417 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1418 (if (save-excursion
1419 (beginning-of-line)
1420 (skip-chars-forward " \t0-9")
1421 (looking-at dostart-re))
1422 ;; Sitting on one.
1423 (match-beginning 0)
1424 ;; Search for one.
1425 (save-excursion
1426 (let ((count 1))
1427 (while (and (not (zerop count))
1428 (not (eq (fortran-previous-statement) 'first-statement))
1429 ;; Keep local to subprogram.
1430 (not (and (looking-at fortran-end-prog-re)
1431 (fortran-check-end-prog-re))))
1432 (skip-chars-forward " \t0-9")
1433 (cond ((looking-at dostart-re)
1434 (setq count (1- count)))
1435 ;; Note labelled loop ends not considered.
1436 ((looking-at "end[ \t]*do\\b")
1437 (setq count (1+ count)))))
1438 (and (zerop count)
1439 ;; All pairs accounted for.
1440 (point)))))))
1442 (defun fortran-mark-if ()
1443 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1444 The marks are pushed."
1445 (interactive)
1446 (let (endif-point if-point)
1447 (if (setq endif-point (fortran-end-if))
1448 (if (not (setq if-point (fortran-beginning-if)))
1449 (message "No matching if.")
1450 ;; Set mark, move point.
1451 (goto-char endif-point)
1452 (push-mark)
1453 (goto-char if-point)))))
1455 (defun fortran-end-if ()
1456 "Search forwards for first unmatched ENDIF.
1457 Return point or nil."
1458 (let ((case-fold-search t))
1459 (if (save-excursion (beginning-of-line)
1460 (skip-chars-forward " \t0-9")
1461 (looking-at "end[ \t]*if\\b"))
1462 ;; Sitting on one.
1463 (match-beginning 0)
1464 ;; Search for one. The point has been already been moved to first
1465 ;; letter on line but this should not cause troubles.
1466 (save-excursion
1467 (let ((count 1))
1468 (while (and (not (zerop count))
1469 (not (eq (fortran-next-statement) 'last-statement))
1470 ;; Keep local to subprogram.
1471 (not (and (looking-at fortran-end-prog-re)
1472 (fortran-check-end-prog-re))))
1473 (skip-chars-forward " \t0-9")
1474 (cond ((looking-at "end[ \t]*if\\b")
1475 (setq count (1- count)))
1476 ((looking-at fortran-if-start-re)
1477 (save-excursion
1478 (if (or
1479 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1480 (let (then-test) ; multi-line if-then
1481 (while
1482 (and
1483 (zerop (forward-line 1))
1484 ;; Search forward for then.
1485 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1486 (not
1487 (setq then-test
1488 (looking-at
1489 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1490 then-test))
1491 (setq count (1+ count)))))))
1492 (and (zerop count)
1493 ;; All pairs accounted for.
1494 (point)))))))
1496 (defun fortran-beginning-if ()
1497 "Search backwards for first unmatched IF-THEN.
1498 Return point or nil."
1499 (let ((case-fold-search t))
1500 (if (save-excursion
1501 ;; May be sitting on multi-line if-then statement, first
1502 ;; move to beginning of current statement. Note:
1503 ;; `fortran-previous-statement' moves to previous statement
1504 ;; *unless* current statement is first one. Only move
1505 ;; forward if not first-statement.
1506 (if (not (eq (fortran-previous-statement) 'first-statement))
1507 (fortran-next-statement))
1508 (skip-chars-forward " \t0-9")
1509 (and
1510 (looking-at fortran-if-start-re)
1511 (save-match-data
1512 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1513 ;; Multi-line if-then.
1514 (let (then-test)
1515 (while
1516 (and (zerop (forward-line 1))
1517 ;; Search forward for then.
1518 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1519 (not
1520 (setq then-test
1521 (looking-at
1522 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1523 then-test)))))
1524 ;; Sitting on one.
1525 (match-beginning 0)
1526 ;; Search for one.
1527 (save-excursion
1528 (let ((count 1))
1529 (while (and (not (zerop count))
1530 (not (eq (fortran-previous-statement) 'first-statement))
1531 ;; Keep local to subprogram.
1532 (not (and (looking-at fortran-end-prog-re)
1533 (fortran-check-end-prog-re))))
1534 (skip-chars-forward " \t0-9")
1535 (cond ((looking-at fortran-if-start-re)
1536 (save-excursion
1537 (if (or
1538 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1539 (let (then-test) ; multi-line if-then
1540 (while
1541 (and
1542 (zerop (forward-line 1))
1543 ;; Search forward for then.
1544 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1545 (not
1546 (setq then-test
1547 (looking-at
1548 (concat ".*then\\b[ \t]*"
1549 "[^ \t(=a-z0-9]"))))))
1550 then-test))
1551 (setq count (1- count)))))
1552 ((looking-at "end[ \t]*if\\b")
1553 (setq count (1+ count)))))
1554 (and (zerop count)
1555 ;; All pairs accounted for.
1556 (point)))))))
1559 (defun fortran-indent-line ()
1560 "Indent current Fortran line based on its contents and on previous lines."
1561 (interactive "*")
1562 (let ((cfi (fortran-calculate-indent)))
1563 (save-excursion
1564 (beginning-of-line)
1565 (if (or (not (= cfi (fortran-current-line-indentation)))
1566 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1567 (not (fortran-line-number-indented-correctly-p))))
1568 (fortran-indent-to-column cfi)
1569 (beginning-of-line)
1570 (if (fortran-find-comment-start-skip)
1571 (fortran-indent-comment))))
1572 ;; Never leave point in left margin.
1573 (if (< (current-column) cfi)
1574 (move-to-column cfi))
1575 (and auto-fill-function
1576 (> (save-excursion (end-of-line) (current-column))
1577 fill-column)
1578 (save-excursion
1579 (end-of-line)
1580 (fortran-fill)))
1581 (when fortran-blink-matching-if
1582 (fortran-blink-matching-if)
1583 (fortran-blink-matching-do))))
1585 (defun fortran-auto-fill ()
1586 "Function to use for `normal-auto-fill-function' in Fortran mode."
1587 (if (> (current-column) (current-fill-column))
1588 (let ((cfi (fortran-calculate-indent)))
1589 (save-excursion
1590 (beginning-of-line)
1591 (if (or (not (= cfi (fortran-current-line-indentation)))
1592 (and (re-search-forward "^[ \t]*[0-9]+"
1593 (+ (point) 4) t)
1594 (not (fortran-line-number-indented-correctly-p))))
1595 (fortran-indent-to-column cfi)
1596 (beginning-of-line)
1597 (if (fortran-find-comment-start-skip)
1598 (fortran-indent-comment))))
1599 (fortran-fill)
1600 ;; Never leave point in left margin.
1601 (if (< (current-column) cfi)
1602 (move-to-column cfi)))))
1604 ;; Historically this was a separate function which advertised itself
1605 ;; as reindenting but only did so where `most likely to be necessary'.
1606 (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
1608 (defun fortran-indent-subprogram ()
1609 "Properly indent the Fortran subprogram containing point."
1610 (interactive "*")
1611 (save-excursion
1612 (mark-defun)
1613 (message "Indenting subprogram...")
1614 (indent-region (point) (mark) nil))
1615 (message "Indenting subprogram...done."))
1617 (defun fortran-calculate-indent ()
1618 "Calculates the Fortran indent column based on previous lines."
1619 (let (icol first-statement (case-fold-search t)
1620 (fortran-minimum-statement-indent
1621 (if indent-tabs-mode
1622 fortran-minimum-statement-indent-tab
1623 fortran-minimum-statement-indent-fixed)))
1624 (save-excursion
1625 (setq first-statement (fortran-previous-statement))
1626 (if first-statement
1627 (setq icol fortran-minimum-statement-indent)
1628 (if (= (point) (point-min))
1629 (setq icol fortran-minimum-statement-indent)
1630 (setq icol (fortran-current-line-indentation)))
1631 (skip-chars-forward " \t0-9")
1632 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1633 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1634 (let (then-test) ; multi-line if-then
1635 (while (and (zerop (forward-line 1))
1636 ;; Search forward for then.
1637 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1638 (not (setq then-test
1639 (looking-at
1640 ".*then\\b[ \t]\
1641 *[^ \t_$(=a-z0-9]")))))
1642 then-test))
1643 (setq icol (+ icol fortran-if-indent))))
1644 ((looking-at "else\\(if\\)?\\b")
1645 (setq icol (+ icol fortran-if-indent)))
1646 ((looking-at "select[ \t]*case[ \t](.*)")
1647 (setq icol (+ icol fortran-if-indent)))
1648 ((looking-at "case[ \t]*(.*)")
1649 (setq icol (+ icol fortran-if-indent)))
1650 ((looking-at "case[ \t]*default\\b")
1651 (setq icol (+ icol fortran-if-indent)))
1652 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1653 (setq icol (+ icol fortran-if-indent)))
1654 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1655 (setq icol (+ icol fortran-if-indent)))
1656 ((looking-at "do\\b")
1657 (setq icol (+ icol fortran-do-indent)))
1658 ((looking-at
1659 "\\(structure\\|union\\|map\\|interface\\)\
1660 \\b[ \t]*[^ \t=(a-z]")
1661 (setq icol (+ icol fortran-structure-indent)))
1662 ((and (looking-at fortran-end-prog-re1)
1663 (fortran-check-end-prog-re))
1664 ;; Previous END resets indent to minimum.
1665 (setq icol fortran-minimum-statement-indent)))))
1666 (save-excursion
1667 (beginning-of-line)
1668 (cond ((looking-at "[ \t]*$"))
1669 ;; Check for directive before comment, so as not to indent.
1670 ((looking-at fortran-directive-re)
1671 (setq fortran-minimum-statement-indent 0 icol 0))
1672 ((looking-at fortran-comment-line-start-skip)
1673 (cond ((eq fortran-comment-indent-style 'relative)
1674 (setq icol (+ icol fortran-comment-line-extra-indent)))
1675 ((eq fortran-comment-indent-style 'fixed)
1676 (setq icol (+ fortran-minimum-statement-indent
1677 fortran-comment-line-extra-indent))))
1678 (setq fortran-minimum-statement-indent 0))
1679 ((or (looking-at (concat "[ \t]*"
1680 (regexp-quote
1681 fortran-continuation-string)))
1682 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1683 (skip-chars-forward " \t")
1684 ;; Do not introduce extra whitespace into a broken string.
1685 (setq icol
1686 (if (fortran-is-in-string-p (point))
1688 (+ icol fortran-continuation-indent))))
1689 (first-statement)
1690 ((and fortran-check-all-num-for-matching-do
1691 (looking-at "[ \t]*[0-9]+")
1692 (fortran-check-for-matching-do))
1693 (setq icol (- icol fortran-do-indent)))
1695 (skip-chars-forward " \t0-9")
1696 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1697 (setq icol (- icol fortran-if-indent)))
1698 ((looking-at "else\\(if\\)?\\b")
1699 (setq icol (- icol fortran-if-indent)))
1700 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1701 (setq icol (- icol fortran-if-indent)))
1702 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1703 (setq icol (- icol fortran-if-indent)))
1704 ((and (looking-at "continue\\b")
1705 (fortran-check-for-matching-do))
1706 (setq icol (- icol fortran-do-indent)))
1707 ((looking-at "end[ \t]*do\\b")
1708 (setq icol (- icol fortran-do-indent)))
1709 ((looking-at "end[ \t]*\
1710 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1711 (setq icol (- icol fortran-structure-indent)))
1712 ((and (looking-at fortran-end-prog-re1)
1713 (fortran-check-end-prog-re)
1714 (not (= icol fortran-minimum-statement-indent)))
1715 (message "Warning: `end' not in column %d. Probably\
1716 an unclosed block." fortran-minimum-statement-indent))))))
1717 (max fortran-minimum-statement-indent icol)))
1720 (defun fortran-current-line-indentation ()
1721 "Indentation of current line, ignoring Fortran line number or continuation.
1722 This is the column position of the first non-whitespace character
1723 aside from the line number and/or column 5/8 line-continuation character.
1724 For comment lines, returns indentation of the first
1725 non-indentation text within the comment."
1726 (save-excursion
1727 (beginning-of-line)
1728 (cond ((looking-at fortran-comment-line-start-skip)
1729 (goto-char (match-end 0))
1730 (skip-chars-forward
1731 (if (stringp fortran-comment-indent-char)
1732 fortran-comment-indent-char
1733 (char-to-string fortran-comment-indent-char))))
1734 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1735 (goto-char (match-end 0)))
1737 ;; Move past line number.
1738 (skip-chars-forward "[ \t0-9]")))
1739 ;; Move past whitespace.
1740 (skip-chars-forward " \t")
1741 (current-column)))
1743 (defun fortran-indent-to-column (col)
1744 "Indent current line to column COL.
1745 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1746 line, and this continuation character is retained on indentation;
1747 2) If `fortran-continuation-string' is the first non-whitespace
1748 character, this is a continuation line;
1749 3) A non-continuation line which has a number as the first
1750 non-whitespace character is a numbered line.
1751 4) A TAB followed by a digit indicates a continuation line."
1752 (save-excursion
1753 (beginning-of-line)
1754 (if (looking-at fortran-comment-line-start-skip)
1755 (if fortran-comment-indent-style
1756 (let* ((char (if (stringp fortran-comment-indent-char)
1757 (aref fortran-comment-indent-char 0)
1758 fortran-comment-indent-char))
1759 (chars (string ?\s ?\t char)))
1760 (goto-char (match-end 0))
1761 (skip-chars-backward chars)
1762 (delete-region (point) (progn (skip-chars-forward chars)
1763 (point)))
1764 (insert-char char (- col (current-column)))))
1765 (if (looking-at "\t[1-9]")
1766 (if indent-tabs-mode
1767 (goto-char (match-end 0))
1768 (delete-char 2)
1769 (insert-char ?\s 5)
1770 (insert fortran-continuation-string))
1771 (if (looking-at " \\{5\\}[^ 0\n]")
1772 (if indent-tabs-mode
1773 (progn (delete-char 6)
1774 (insert ?\t (fortran-numerical-continuation-char) 1))
1775 (forward-char 6))
1776 (delete-horizontal-space)
1777 ;; Put line number in columns 0-4, or
1778 ;; continuation character in column 5.
1779 (cond ((eobp))
1780 ((looking-at (regexp-quote fortran-continuation-string))
1781 (if indent-tabs-mode
1782 (progn
1783 (indent-to
1784 (if indent-tabs-mode
1785 fortran-minimum-statement-indent-tab
1786 fortran-minimum-statement-indent-fixed))
1787 (delete-char 1)
1788 (insert-char (fortran-numerical-continuation-char) 1))
1789 (indent-to 5)
1790 (forward-char 1)))
1791 ((looking-at "[0-9]+")
1792 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1793 (if (< extra-space 0)
1794 (message "Warning: line number exceeds 5-digit limit.")
1795 (indent-to (min fortran-line-number-indent extra-space))))
1796 (skip-chars-forward "0-9")))))
1797 ;; Point is now after any continuation character or line number.
1798 ;; Put body of statement where specified.
1799 (delete-horizontal-space)
1800 (indent-to col)
1801 ;; Indent any comment following code on the same line.
1802 (when (fortran-find-comment-start-skip)
1803 (goto-char (match-beginning 0))
1804 (unless (= (current-column) (fortran-comment-indent))
1805 (delete-horizontal-space)
1806 (indent-to (fortran-comment-indent)))))))
1808 (defun fortran-line-number-indented-correctly-p ()
1809 "Return t if current line's line number is correctly indented.
1810 Do not call if there is no line number."
1811 (save-excursion
1812 (beginning-of-line)
1813 (skip-chars-forward " \t")
1814 (and (<= (current-column) fortran-line-number-indent)
1815 (or (= (current-column) fortran-line-number-indent)
1816 (progn (skip-chars-forward "0-9")
1817 (= (current-column) 5))))))
1819 (defun fortran-check-for-matching-do ()
1820 "When called from a numbered statement, return t if matching DO is found.
1821 Otherwise return nil."
1822 (let ((case-fold-search t)
1823 charnum)
1824 (save-excursion
1825 (beginning-of-line)
1826 (when (looking-at "[ \t]*[0-9]+")
1827 (skip-chars-forward " \t")
1828 (skip-chars-forward "0") ; skip past leading zeros
1829 (setq charnum
1830 (buffer-substring (point) (progn
1831 (skip-chars-forward "0-9")
1832 (point))))
1833 (beginning-of-line)
1834 (save-restriction
1835 (save-excursion
1836 (narrow-to-defun)
1837 (and (re-search-backward
1838 (concat
1839 "\\(^[ \t0-9]*do[ \t]*0*"
1840 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1841 charnum "\\b\\)")
1842 nil t)
1843 (looking-at
1844 (concat "^[ \t0-9]*do[ \t]*0*"
1845 charnum)))))))))
1847 (defun fortran-find-comment-start-skip (&optional all)
1848 "Move to past `comment-start-skip' found on current line.
1849 Return non-nil if `comment-start-skip' found, nil if not.
1850 If ALL is nil, only match comments that start in column > 0."
1851 ;; Hopefully at some point we can just use the line below! -stef
1852 ;; (comment-search-forward (line-end-position) t))
1853 (when (or all comment-start-skip)
1854 (let ((pos (point))
1855 (css (if comment-start-skip
1856 (concat fortran-comment-line-start-skip
1857 "\\|" comment-start-skip)
1858 fortran-comment-line-start-skip)))
1859 (when (re-search-forward css (line-end-position) t)
1860 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1861 (or (save-match-data
1862 (not (fortran-is-in-string-p (match-beginning 0))))
1863 ;; Recurse for rest of line.
1864 (fortran-find-comment-start-skip all)))
1865 (point)
1866 (goto-char pos)
1867 nil)))))
1869 ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1870 ;; Test if TAB format continuation lines work.
1871 (defun fortran-is-in-string-p (where)
1872 "Return non-nil if WHERE (a buffer position) is inside a Fortran string."
1873 (save-excursion
1874 (goto-char where)
1875 (cond
1876 ((bolp) nil) ; bol is never inside a string
1877 ((save-excursion ; comment lines too
1878 (beginning-of-line)
1879 (looking-at fortran-comment-line-start-skip)) nil)
1880 (t (let ((parse-state '(0 nil nil nil nil nil 0))
1881 (quoted-comment-start (if comment-start
1882 (regexp-quote comment-start)))
1883 (not-done t)
1884 parse-limit end-of-line)
1885 ;; Move to start of current statement.
1886 (fortran-next-statement)
1887 (fortran-previous-statement)
1888 ;; Now parse up to WHERE.
1889 (while not-done
1890 (if (or ;; Skip to next line if:
1891 ;; - comment line?
1892 (looking-at fortran-comment-line-start-skip)
1893 ;; - at end of line?
1894 (eolp)
1895 ;; - not in a string and after comment-start?
1896 (and (not (nth 3 parse-state))
1897 comment-start
1898 (equal comment-start
1899 (char-to-string (preceding-char)))))
1900 (if (> (forward-line) 0)
1901 (setq not-done nil))
1902 ;; else:
1903 ;; If we are at beginning of code line, skip any
1904 ;; whitespace, labels and tab continuation markers.
1905 (if (bolp) (skip-chars-forward " \t0-9"))
1906 ;; If we are in column <= 5 now, check for continuation char.
1907 (cond ((= 5 (current-column)) (forward-char 1))
1908 ((and (< (current-column) 5)
1909 (equal fortran-continuation-string
1910 (char-to-string (following-char)))
1911 (forward-char 1))))
1912 ;; Find out parse-limit from here.
1913 (setq end-of-line (line-end-position))
1914 (setq parse-limit (min where end-of-line))
1915 ;; Parse max up to comment-start, if non-nil and in current line.
1916 (if comment-start
1917 (save-excursion
1918 (if (re-search-forward quoted-comment-start end-of-line t)
1919 (setq parse-limit (min (point) parse-limit)))))
1920 ;; Now parse if still in limits.
1921 (if (< (point) where)
1922 (setq parse-state (parse-partial-sexp
1923 (point) parse-limit nil nil parse-state))
1924 (setq not-done nil))))
1925 ;; Result.
1926 (nth 3 parse-state))))))
1928 ;; From old version.
1929 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1931 (defun fortran-fill ()
1932 "Fill the current line at an appropriate point(s)."
1933 (let* ((auto-fill-function #'fortran-auto-fill)
1934 (opoint (point))
1935 (bol (line-beginning-position))
1936 (eol (line-end-position))
1937 (bos (min eol (+ bol (fortran-current-line-indentation))))
1938 ;; If in a string at fill-column, break it either before the
1939 ;; initial quote, or at fill-col (if string is too long).
1940 (quote
1941 (save-excursion
1942 (goto-char bol)
1943 ;; OK to break quotes on comment lines.
1944 (unless (looking-at fortran-comment-line-start-skip)
1945 (let (fcpoint start)
1946 (move-to-column fill-column)
1947 (when (fortran-is-in-string-p (setq fcpoint (point)))
1948 (save-excursion
1949 (re-search-backward "\\S\"\\s\"\\S\"?" bol t)
1950 (setq start
1951 (if fortran-break-before-delimiters
1952 (point)
1953 (1+ (point)))))
1954 (if (re-search-forward "\\S\"\\s\"\\S\"" eol t)
1955 (backward-char 2))
1956 ;; If the current string is longer than (fill-column
1957 ;; - 6) chars, break it at the fill column (else
1958 ;; infinite loop).
1959 (if (> (- (point) start)
1960 (- fill-column 6 fortran-continuation-indent))
1961 fcpoint
1962 start))))))
1963 ;; Decide where to split the line. If a position for a quoted
1964 ;; string was found above then use that, else break the line
1965 ;; before/after the last delimiter.
1966 (fill-point
1967 (or quote
1968 (save-excursion
1969 ;; If f-b-b-d is t, have an extra column to play with,
1970 ;; since delimiter gets shifted to new line.
1971 (move-to-column (if fortran-break-before-delimiters
1972 (1+ fill-column)
1973 fill-column))
1974 (let ((repeat t))
1975 (while repeat
1976 (setq repeat nil)
1977 ;; Adapted from f90-find-breakpoint.
1978 (re-search-backward fortran-break-delimiters-re bol)
1979 (if (not fortran-break-before-delimiters)
1980 (if (looking-at fortran-no-break-re)
1981 ;; Deal with cases such as "**" split over
1982 ;; fill-col. Simpler alternative would be
1983 ;; to start from (1- fill-column) above.
1984 (if (> (+ 2 (current-column)) fill-column)
1985 (setq repeat t)
1986 (forward-char 2))
1987 (forward-char 1))
1988 (backward-char)
1989 (or (looking-at fortran-no-break-re)
1990 (forward-char)))))
1991 ;; Line indented beyond fill-column?
1992 (when (<= (point) bos)
1993 (move-to-column (1+ fill-column))
1994 ;; What is this doing???
1995 (or (re-search-forward "[\t\n,'+-/*)=]" eol t)
1996 (goto-char bol)))
1997 (if (bolp)
1998 (re-search-forward "[ \t]" opoint t))
1999 (point)))))
2000 ;; If we are in an in-line comment, don't break unless the
2001 ;; line of code is longer than it should be. Otherwise
2002 ;; break the line at the column computed above.
2004 ;; Need to use fortran-find-comment-start-skip to make sure that
2005 ;; quoted !'s don't prevent a break.
2006 (when (and (save-excursion
2007 (beginning-of-line)
2008 (if (not (fortran-find-comment-start-skip))
2010 (goto-char (match-beginning 0))
2011 (>= (point) fill-point)))
2012 (save-excursion
2013 (goto-char fill-point)
2014 (not (bolp)))
2015 (> (save-excursion
2016 (goto-char opoint)
2017 (current-column))
2018 (min (1+ fill-column)
2019 (+ (fortran-calculate-indent)
2020 fortran-continuation-indent))))
2021 (goto-char fill-point)
2022 (fortran-break-line)
2023 (end-of-line))))
2025 (defun fortran-break-line ()
2026 "Call `fortran-split-line'. Joins continuation lines first, then refills."
2027 (let ((bol (line-beginning-position))
2028 (comment-string
2029 (save-excursion
2030 (if (fortran-find-comment-start-skip)
2031 (delete-and-extract-region
2032 (match-beginning 0) (line-end-position))))))
2033 ;; Forward line 1 really needs to go to next non white line.
2034 (if (save-excursion (forward-line)
2035 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
2036 (progn
2037 (end-of-line)
2038 (delete-region (point) (match-end 0))
2039 (delete-horizontal-space)
2040 (fortran-fill))
2041 (fortran-split-line))
2042 (if comment-string
2043 (save-excursion
2044 (goto-char bol)
2045 (end-of-line)
2046 (delete-horizontal-space)
2047 (indent-to (fortran-comment-indent))
2048 (insert comment-string)))))
2050 (defun fortran-analyze-file-format ()
2051 "Return nil if fixed format is used, t if TAB formatting is used.
2052 Use `fortran-tab-mode-default' if no non-comment statements are found
2053 before the end or in the first `fortran-analyze-depth' lines."
2054 (let ((i 0))
2055 (save-excursion
2056 (goto-char (point-min))
2057 (while (not (or
2058 (eobp)
2059 (eq (char-after) ?\t)
2060 (looking-at " \\{6\\}")
2061 (> i fortran-analyze-depth)))
2062 (forward-line)
2063 (setq i (1+ i)))
2064 (cond
2065 ((eq (char-after) ?\t) t)
2066 ((looking-at " \\{6\\}") nil)
2067 (t fortran-tab-mode-default)))))
2069 (defun fortran-fill-paragraph (&optional justify)
2070 "Fill surrounding comment block as paragraphs, else fill statement.
2071 Intended as the value of `fill-paragraph-function'.
2072 A comment block is filled by calling `fill-comment-paragraph' with
2073 argument JUSTIFY, otherwise `fortran-fill-statement' is called.
2074 Always returns non-nil (to prevent `fill-paragraph' being called)."
2075 (interactive "*P")
2076 (or (fill-comment-paragraph justify)
2077 (fortran-fill-statement)
2080 (defun fortran-fill-statement ()
2081 "Fill a Fortran statement up to `fill-column'."
2082 (interactive "*")
2083 (let ((auto-fill-function #'fortran-auto-fill))
2084 (unless (save-excursion
2085 (beginning-of-line)
2086 (or (looking-at "[ \t]*$")
2087 (looking-at fortran-comment-line-start-skip)
2088 (and comment-start-skip
2089 (looking-at (concat "[ \t]*" comment-start-skip)))))
2090 (save-excursion
2091 ;; Find beginning of statement.
2092 (fortran-next-statement)
2093 (fortran-previous-statement)
2094 ;; Re-indent initially.
2095 (fortran-indent-line)
2096 ;; Replace newline plus continuation field plus indentation with
2097 ;; single space.
2098 (while (progn
2099 (forward-line)
2100 (fortran-remove-continuation)))
2101 (fortran-previous-statement)))
2102 (fortran-indent-line)))
2104 (defun fortran-strip-sequence-nos (&optional do-space)
2105 "Delete all text in column `fortran-line-length' (default 72) and up.
2106 This is assumed to be sequence numbers. Normally also deletes
2107 trailing whitespace after stripping such text. Supplying prefix
2108 arg DO-SPACE prevents stripping the whitespace."
2109 (interactive "*p")
2110 (save-excursion
2111 (goto-char (point-min))
2112 (while (re-search-forward (format "^.\\{%d\\}\\(.*\\)" fortran-line-length)
2113 nil t)
2114 (replace-match "" nil nil nil 1)
2115 (unless do-space (delete-horizontal-space)))))
2117 ;; This code used to live in add-log.el, but this is a better place
2118 ;; for it.
2119 (defun fortran-current-defun ()
2120 "Function to use for `add-log-current-defun-function' in Fortran mode."
2121 (save-excursion
2122 ;; We must be inside function body for this to work.
2123 (fortran-beginning-of-subprogram)
2124 (let ((case-fold-search t)) ; case-insensitive
2125 ;; Search for fortran subprogram start.
2126 (if (re-search-forward
2127 (concat "^[ \t]*\\(program\\|subroutine\\|function"
2128 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
2129 "\\(block[ \t]*data\\)\\)")
2130 (save-excursion (fortran-end-of-subprogram)
2131 (point))
2133 (or (match-string-no-properties 2)
2134 (progn
2135 ;; Move to EOL or before first left paren.
2136 (if (re-search-forward "[(\n]" nil t)
2137 (progn (backward-char)
2138 (skip-chars-backward " \t"))
2139 (end-of-line))
2140 ;; Use the name preceding that.
2141 (buffer-substring-no-properties (point) (progn (backward-sexp)
2142 (point)))))
2143 "main"))))
2145 (provide 'fortran)
2147 ;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04
2148 ;;; fortran.el ends here