(fortran-preprocessor-re): New variable. Use it for font-locking.
[emacs.git] / lisp / progmodes / fortran.el
blob4e5e6144e6894ea73719c9cb680d4559eafe5fdf
1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;; Copyright (c) 1986, 93, 94, 95, 97, 98, 99, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Michael D. Prange <prange@erl.mit.edu>
7 ;; Maintainer: Dave Love <fx@gnu.org>
8 ;; Keywords: 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 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
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).
37 ;;; History:
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.
46 ;;; Code:
48 ;; Todo:
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.
56 (defgroup fortran nil
57 "Fortran mode for Emacs"
58 :link '(custom-manual "(emacs)Fortran")
59 :group 'languages)
61 (defgroup fortran-indent nil
62 "Indentation variables in Fortran mode"
63 :prefix "fortran-"
64 :group 'fortran)
66 (defgroup fortran-comment nil
67 "Comment-handling variables in Fortran mode"
68 :prefix "fortran-"
69 :group 'fortran)
72 ;;;###autoload
73 (defcustom fortran-tab-mode-default nil
74 "*Default tabbing/carriage control style for empty files in Fortran mode.
75 A value of t specifies tab-digit style of continuation control.
76 A value of nil specifies that continuation lines are marked
77 with a character in column 6."
78 :type 'boolean
79 :group 'fortran-indent)
81 ;; Buffer local, used to display mode line.
82 (defcustom fortran-tab-mode-string nil
83 "String to appear in mode line when TAB format mode is on."
84 :type '(choice (const nil) string)
85 :group 'fortran-indent)
86 (make-variable-buffer-local 'fortran-tab-mode-string)
88 (defcustom fortran-do-indent 3
89 "*Extra indentation applied to DO blocks."
90 :type 'integer
91 :group 'fortran-indent)
93 (defcustom fortran-if-indent 3
94 "*Extra indentation applied to IF blocks."
95 :type 'integer
96 :group 'fortran-indent)
98 (defcustom fortran-structure-indent 3
99 "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
100 :type 'integer
101 :group 'fortran-indent)
103 (defcustom fortran-continuation-indent 5
104 "*Extra indentation applied to Fortran continuation lines."
105 :type 'integer
106 :group 'fortran-indent)
108 (defcustom fortran-comment-indent-style 'fixed
109 "*How to indent comments.
110 nil forces comment lines not to be touched,
111 `fixed' makes fixed comment indentation to `fortran-comment-line-extra-indent'
112 columns beyond `fortran-minimum-statement-indent-fixed' (for
113 `indent-tabs-mode' of nil) or `fortran-minimum-statement-indent-tab' (for
114 `indent-tabs-mode' of t), and
115 `relative' indents to current Fortran indentation plus
116 `fortran-comment-line-extra-indent'."
117 :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative))
118 :group 'fortran-indent)
120 (defcustom fortran-comment-line-extra-indent 0
121 "*Amount of extra indentation for text within full-line comments."
122 :type 'integer
123 :group 'fortran-indent
124 :group 'fortran-comment)
126 (defcustom fortran-comment-line-start "C"
127 "*Delimiter inserted to start new full-line comment.
128 You might want to change this to \"*\", for instance."
129 :version "21.1"
130 :type 'string
131 :group 'fortran-comment)
133 ;; This used to match preprocessor lines too, but that messes up
134 ;; filling and doesn't seem to be necessary.
135 (defcustom fortran-comment-line-start-skip
136 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
137 "*Regexp to match the start of a full-line comment."
138 :version "21.1"
139 :type 'regexp
140 :group 'fortran-comment)
142 (defcustom fortran-preprocessor-re
143 "^[ \t]*#.*"
144 "*Regexp to match the whole of a preprocessor line."
145 :version "21.3"
146 :type 'regexp
147 :group 'fortran-indent)
149 (defcustom fortran-minimum-statement-indent-fixed 6
150 "*Minimum statement indentation for fixed format continuation style."
151 :type 'integer
152 :group 'fortran-indent)
154 (defcustom fortran-minimum-statement-indent-tab (max tab-width 6)
155 "*Minimum statement indentation for TAB format continuation style."
156 :type 'integer
157 :group 'fortran-indent)
159 ;; Note that this is documented in the v18 manuals as being a string
160 ;; of length one rather than a single character.
161 ;; The code in this file accepts either format for compatibility.
162 (defcustom fortran-comment-indent-char " "
163 "*Single-character string inserted for Fortran comment indentation.
164 Normally a space."
165 :type 'string
166 :group 'fortran-comment)
168 (defcustom fortran-line-number-indent 1
169 "*Maximum indentation for Fortran line numbers.
170 5 means right-justify them within their five-column field."
171 :type 'integer
172 :group 'fortran-indent)
174 (defcustom fortran-check-all-num-for-matching-do nil
175 "*Non-nil causes all numbered lines to be treated as possible DO loop ends."
176 :type 'boolean
177 :group 'fortran)
179 (defcustom fortran-blink-matching-if nil
180 "*Non-nil causes \\[fortran-indent-line] on ENDIF statement to blink on matching IF.
181 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
182 :type 'boolean
183 :group 'fortran)
185 (defcustom fortran-continuation-string "$"
186 "*Single-character string used for Fortran continuation lines.
187 In fixed format continuation style, this character is inserted in
188 column 6 by \\[fortran-split-line] to begin a continuation line.
189 Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
190 convert the line into a continuation line of the appropriate style.
191 Normally $."
192 :type 'string
193 :group 'fortran)
195 (defcustom fortran-comment-region "c$$$"
196 "*String inserted by \\[fortran-comment-region] at start of each \
197 line in region."
198 :type 'string
199 :group 'fortran-comment)
201 (defcustom fortran-electric-line-number t
202 "*Non-nil causes line number digits to be moved to the correct \
203 column as typed."
204 :type 'boolean
205 :group 'fortran)
207 (defvar fortran-column-ruler-fixed
208 "0 4 6 10 20 30 40 5\
209 0 60 70\n\
210 \[ ]|{ | | | | | | | | \
211 \| | | | |}\n"
212 "String displayed above current line by \\[fortran-column-ruler].
213 This variable used in fixed format mode.")
215 (defvar fortran-column-ruler-tab
216 "0 810 20 30 40 5\
217 0 60 70\n\
218 \[ ]| { | | | | | | | | \
219 \| | | | |}\n"
220 "String displayed above current line by \\[fortran-column-ruler].
221 This variable used in TAB format mode.")
223 (defvar fortran-analyze-depth 100
224 "Number of lines to scan to determine whether to use fixed or TAB \
225 format style.")
227 (defcustom fortran-break-before-delimiters t
228 "*Non-nil causes filling to break lines before delimiters."
229 :type 'boolean
230 :group 'fortran)
232 (defvar fortran-mode-syntax-table
233 (let ((table (make-syntax-table)))
234 ;; We might like `;' to be punctuation (g77 multi-statement
235 ;; lines), but that screws abbrevs.
236 (modify-syntax-entry ?\; "w" table)
237 (modify-syntax-entry ?\r " " table)
238 (modify-syntax-entry ?+ "." table)
239 (modify-syntax-entry ?- "." table)
240 (modify-syntax-entry ?= "." table)
241 (modify-syntax-entry ?* "." table)
242 (modify-syntax-entry ?/ "." table)
243 (modify-syntax-entry ?\' "\"" table)
244 (modify-syntax-entry ?\" "\"" table)
245 ;; Consistent with GNU Fortran -- see the manual.
246 (modify-syntax-entry ?\\ "\\" table)
247 ;; This might be better as punctuation, as for C, but this way you
248 ;; can treat floating-point numbers as symbols.
249 (modify-syntax-entry ?. "_" table) ; e.g. `a.ne.b'
250 (modify-syntax-entry ?_ "_" table)
251 (modify-syntax-entry ?$ "_" table) ; esp. VMSisms
252 (modify-syntax-entry ?\! "<" table)
253 (modify-syntax-entry ?\n ">" table)
254 table)
255 "Syntax table in use in Fortran mode buffers.")
257 ;; Comments are real pain in Fortran because there is no way to
258 ;; represent the standard comment syntax in an Emacs syntax table.
259 ;; (We can do so for F90-style). Therefore an unmatched quote in a
260 ;; standard comment will throw fontification off on the wrong track.
261 ;; So we do syntactic fontification with regexps.
263 (defvar fortran-font-lock-keywords-1 nil
264 "Subdued level highlighting for Fortran mode.")
266 (defvar fortran-font-lock-keywords-2 nil
267 "Medium level highlighting for Fortran mode.")
269 (defvar fortran-font-lock-keywords-3 nil
270 "Gaudy level highlighting for Fortran mode.")
272 (defvar fortran-font-lock-syntactic-keywords nil
273 "`font-lock-syntactic-keywords' for Fortran.
274 These get fixed-format comments fontified.")
276 (let ((comment-chars "cd\\*") ; `d' for `debugging' comments
277 (fortran-type-types
278 (eval-when-compile
279 (let ((re (regexp-opt
280 (let ((simple-types
281 '("character" "byte" "integer" "logical"
282 "none" "real" "complex"
283 "double precision" "double complex"))
284 (structured-types '("structure" "union" "map"))
285 (other-types '("record" "dimension"
286 "parameter" "common" "save"
287 "external" "intrinsic" "data"
288 "equivalence")))
289 (append
290 (mapcar (lambda (x) (concat "implicit " x))
291 simple-types)
292 simple-types
293 (mapcar (lambda (x) (concat "end " x))
294 structured-types)
295 structured-types
296 other-types)))))
297 ;; In the optimized regexp above, replace spaces by a
298 ;; regexp for optional whitespace; regexp-opt would have
299 ;; escaped that.
300 (mapconcat #'identity (split-string re) "[ \t]*"))))
301 (fortran-keywords
302 (eval-when-compile
303 (regexp-opt '("continue" "format" "end" "enddo" "if" "then"
304 "else" "endif" "elseif" "while" "inquire" "stop"
305 "return" "include" "open" "close" "read" "write"
306 "format" "print" "select" "case" "cycle" "exit"
307 "rewind" "backspace"))))
308 (fortran-logicals
309 (eval-when-compile
310 (regexp-opt '("and" "or" "not" "lt" "le" "eq" "ge" "gt" "ne"
311 "true" "false")))))
312 (setq fortran-font-lock-syntactic-keywords
313 ;; Fixed format comments. (!-style handled normally.)
314 (list
315 (list (concat "^[" comment-chars "]") 0 '(11))
316 (list (concat "^[^" comment-chars "\t\n]" ".\\{71\\}"
317 "\\([^\n]+\\)")
318 1 '(11))))
319 (setq fortran-font-lock-keywords-1
320 (list
321 ;; Program, subroutine and function declarations, plus calls.
322 (list (concat "\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|"
323 "program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?")
324 '(1 font-lock-keyword-face)
325 '(2 font-lock-function-name-face nil t))))
326 (setq fortran-font-lock-keywords-2
327 (append fortran-font-lock-keywords-1
328 (list
329 ;; Fontify all type specifiers (must be first; see below).
330 (cons (concat "\\<\\(" fortran-type-types "\\)\\>")
331 'font-lock-type-face)
332 ;; Fontify all builtin keywords (except logical, do
333 ;; and goto; see below).
334 (concat "\\<\\(" fortran-keywords "\\)\\>")
335 ;; Fontify all builtin operators.
336 (concat "\\.\\(" fortran-logicals "\\)\\.")
337 ;; Fontify do/goto keywords and targets, and goto tags.
338 (list "\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
339 '(1 font-lock-keyword-face)
340 '(2 font-lock-constant-face nil t))
341 (cons "^ *\\([0-9]+\\)" 'font-lock-constant-face))))
342 (setq fortran-font-lock-keywords-3
343 (append
344 ;; The list `fortran-font-lock-keywords-1'.
345 fortran-font-lock-keywords-1
346 ;; Fontify all type specifiers plus their declared items.
347 (list
348 (list (concat "\\<\\(" fortran-type-types "\\)\\>[ \t(/]*\\(*\\)?")
349 ;; Fontify the type specifier.
350 '(1 font-lock-type-face)
351 ;; Fontify each declaration item (or just the /.../ block name).
352 `(font-lock-match-c-style-declaration-item-and-skip-to-next
353 ;; Start after any *(...) expression.
354 (condition-case nil
355 (and (and (match-beginning ,(+ 2 (regexp-opt-depth
356 fortran-type-types)))
357 (forward-sexp))
358 (forward-sexp))
359 (error nil))
360 ;; No need to clean up.
362 ;; Fontify as a variable name, functions are
363 ;; fontified elsewhere.
364 (1 font-lock-variable-name-face nil t))))
365 ;; Things extra to `fortran-font-lock-keywords-3'
366 ;; (must be done first).
367 (list
368 ;; Fontify goto-like `err=label'/`end=label' in read/write
369 ;; statements.
370 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
371 (1 font-lock-keyword-face) (4 font-lock-constant-face nil t))
372 ;; Highlight standard continuation character and in a
373 ;; TAB-formatted line.
374 '("^ \\([^ 0]\\)" 1 font-lock-string-face)
375 '("^\t\\([1-9]\\)" 1 font-lock-string-face))
376 (list
377 ;; cpp stuff (ugh)
378 ;;; '("^# *[a-z]+" . font-lock-keyword-face))
379 `(,fortran-preprocessor-re (0 font-lock-keyword-face t)))
380 ;; The list `fortran-font-lock-keywords-2' less that for types
381 ;; (see above).
382 (cdr (nthcdr (length fortran-font-lock-keywords-1)
383 fortran-font-lock-keywords-2)))))
385 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
386 "Default expressions to highlight in Fortran mode.")
388 (defvar fortran-imenu-generic-expression
389 ;; These patterns could be confused by sequence nos. in cols 72+ and
390 ;; don't allow continuations everywhere.
391 (list
392 (list
394 ;; [This will be fooled by `end function' allowed by G77. Also,
395 ;; it assumes sensible whitespace is employed.]
396 (concat
397 ;; leading whitespace:
398 "^\\s-+\\("
399 ;; function declaration with optional type, e.g. `real',
400 ;; `real*4', character(*), `double precision':
401 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
402 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
403 ;; Possible statement continuation:
404 "[ \t" fortran-continuation-string "]+"
405 ;; Variable to index:
406 "\\(\\sw+\\)")
408 ;; Un-named block data
409 (list nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
410 "Imenu generic expression for `imenu-default-create-index-function'.")
412 (defvar fortran-mode-map
413 (let ((map (make-sparse-keymap)))
414 (define-key map ";" 'fortran-abbrev-start)
415 (define-key map "\C-c;" 'fortran-comment-region)
416 (define-key map "\M-;" 'fortran-indent-comment)
417 (define-key map "\M-\n" 'fortran-split-line)
418 (define-key map "\M-\C-q" 'fortran-indent-subprogram)
419 (define-key map "\C-c\C-w" 'fortran-window-create-momentarily)
420 (define-key map "\C-c\C-r" 'fortran-column-ruler)
421 (define-key map "\C-c\C-p" 'fortran-previous-statement)
422 (define-key map "\C-c\C-n" 'fortran-next-statement)
423 (define-key map "\C-c\C-d" 'fortran-join-line) ; like f90
424 (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation
425 (define-key map "0" 'fortran-electric-line-number)
426 (define-key map "1" 'fortran-electric-line-number)
427 (define-key map "2" 'fortran-electric-line-number)
428 (define-key map "3" 'fortran-electric-line-number)
429 (define-key map "4" 'fortran-electric-line-number)
430 (define-key map "5" 'fortran-electric-line-number)
431 (define-key map "6" 'fortran-electric-line-number)
432 (define-key map "7" 'fortran-electric-line-number)
433 (define-key map "8" 'fortran-electric-line-number)
434 (define-key map "9" 'fortran-electric-line-number)
436 ;; Menu
437 (easy-menu-define
438 fortran-mode-menu map ""
439 `("Fortran"
440 ["Manual" (info "(emacs)Fortran")]
441 ("Customization"
442 ,(custom-menu-create 'fortran)
443 ["Set" Custom-set t]
444 ["Save" Custom-save t]
445 ["Reset to Current" Custom-reset-current t]
446 ["Reset to Saved" Custom-reset-saved t]
447 ["Reset to Standard Settings" Custom-reset-standard t])
448 "----"
449 ["Toggle Auto-fill" auto-fill-mode :style toggle
450 :selected auto-fill-function]
451 ["Toggle abbrev-mode" abbrev-mode :style toggle :selected abbrev-mode]
452 "----"
453 ["Comment-out Region" fortran-comment-region mark-active]
454 ["Uncomment-out region"
455 (fortran-comment-region (region-beginning) (region-end) 1)
456 mark-active]
457 ["Indent Region" indent-region mark-active]
458 ["Indent Subprogram" fortran-indent-subprogram t]
459 "----"
460 ["Beginning of Subprogram" fortran-beginning-of-subprogram t]
461 ["End of Subprogram" fortran-end-of-subprogram t]
462 ("Mark"
463 ["Subprogram" mark-defun t]
464 ["IF Block" fortran-mark-if t]
465 ["DO Block" fortran-mark-do t])
466 ["Narrow to Subprogram" narrow-to-defun t]
467 ["Widen" widen t]
468 "----"
469 ["Temporary column ruler" fortran-column-ruler t]
470 ["72-column window" fortran-window-create t]
471 ["Full Width Window"
472 (enlarge-window-horizontally (- (frame-width) (window-width)))
473 (< (window-width) (frame-width))]
474 ["Momentary 72-column window" fortran-window-create-momentarily t]
475 "----"
476 ["Break Line at Point" fortran-split-line t]
477 ["Join Line" fortran-join-line t]
478 ["Fill Statement/Comment" fill-paragraph t]
479 "----"
480 ["Add imenu menu"
481 imenu-add-menubar-index (not (and (boundp 'imenu--index-alist)
482 imenu--index-alist))]))
483 map)
484 "Keymap used in Fortran mode.")
486 (defvar fortran-mode-abbrev-table
487 (let ((ac abbrevs-changed))
488 (define-abbrev-table 'fortran-mode-abbrev-table ())
489 (define-abbrev fortran-mode-abbrev-table ";au" "automatic" nil 0 t)
490 (define-abbrev fortran-mode-abbrev-table ";b" "byte" nil 0 t)
491 (define-abbrev fortran-mode-abbrev-table ";bd" "block data" nil 0 t)
492 (define-abbrev fortran-mode-abbrev-table ";ch" "character" nil 0 t)
493 (define-abbrev fortran-mode-abbrev-table ";cl" "close" nil 0 t)
494 (define-abbrev fortran-mode-abbrev-table ";c" "continue" nil 0 t)
495 (define-abbrev fortran-mode-abbrev-table ";cm" "common" nil 0 t)
496 (define-abbrev fortran-mode-abbrev-table ";cx" "complex" nil 0 t)
497 (define-abbrev fortran-mode-abbrev-table ";df" "define" nil 0 t)
498 (define-abbrev fortran-mode-abbrev-table ";di" "dimension" nil 0 t)
499 (define-abbrev fortran-mode-abbrev-table ";do" "double" nil 0 t)
500 (define-abbrev fortran-mode-abbrev-table ";dc" "double complex" nil 0 t)
501 (define-abbrev fortran-mode-abbrev-table ";dp" "double precision" nil 0 t)
502 (define-abbrev fortran-mode-abbrev-table ";dw" "do while" nil 0 t)
503 (define-abbrev fortran-mode-abbrev-table ";e" "else" nil 0 t)
504 (define-abbrev fortran-mode-abbrev-table ";ed" "enddo" nil 0 t)
505 (define-abbrev fortran-mode-abbrev-table ";el" "elseif" nil 0 t)
506 (define-abbrev fortran-mode-abbrev-table ";en" "endif" nil 0 t)
507 (define-abbrev fortran-mode-abbrev-table ";eq" "equivalence" nil 0 t)
508 (define-abbrev fortran-mode-abbrev-table ";ew" "endwhere" nil 0 t)
509 (define-abbrev fortran-mode-abbrev-table ";ex" "external" nil 0 t)
510 (define-abbrev fortran-mode-abbrev-table ";ey" "entry" nil 0 t)
511 (define-abbrev fortran-mode-abbrev-table ";f" "format" nil 0 t)
512 (define-abbrev fortran-mode-abbrev-table ";fa" ".false." nil 0 t)
513 (define-abbrev fortran-mode-abbrev-table ";fu" "function" nil 0 t)
514 (define-abbrev fortran-mode-abbrev-table ";g" "goto" nil 0 t)
515 (define-abbrev fortran-mode-abbrev-table ";im" "implicit" nil 0 t)
516 (define-abbrev fortran-mode-abbrev-table ";ib" "implicit byte" nil 0 t)
517 (define-abbrev fortran-mode-abbrev-table ";ic" "implicit complex" nil 0 t)
518 (define-abbrev fortran-mode-abbrev-table ";ich" "implicit character" nil 0 t)
519 (define-abbrev fortran-mode-abbrev-table ";ii" "implicit integer" nil 0 t)
520 (define-abbrev fortran-mode-abbrev-table ";il" "implicit logical" nil 0 t)
521 (define-abbrev fortran-mode-abbrev-table ";ir" "implicit real" nil 0 t)
522 (define-abbrev fortran-mode-abbrev-table ";inc" "include" nil 0 t)
523 (define-abbrev fortran-mode-abbrev-table ";in" "integer" nil 0 t)
524 (define-abbrev fortran-mode-abbrev-table ";intr" "intrinsic" nil 0 t)
525 (define-abbrev fortran-mode-abbrev-table ";l" "logical" nil 0 t)
526 (define-abbrev fortran-mode-abbrev-table ";n" "namelist" nil 0 t)
527 (define-abbrev fortran-mode-abbrev-table ";o" "open" nil 0 t) ; was ;op
528 (define-abbrev fortran-mode-abbrev-table ";pa" "parameter" nil 0 t)
529 (define-abbrev fortran-mode-abbrev-table ";pr" "program" nil 0 t)
530 (define-abbrev fortran-mode-abbrev-table ";ps" "pause" nil 0 t)
531 (define-abbrev fortran-mode-abbrev-table ";p" "print" nil 0 t)
532 (define-abbrev fortran-mode-abbrev-table ";rc" "record" nil 0 t)
533 (define-abbrev fortran-mode-abbrev-table ";re" "real" nil 0 t)
534 (define-abbrev fortran-mode-abbrev-table ";r" "read" nil 0 t)
535 (define-abbrev fortran-mode-abbrev-table ";rt" "return" nil 0 t)
536 (define-abbrev fortran-mode-abbrev-table ";rw" "rewind" nil 0 t)
537 (define-abbrev fortran-mode-abbrev-table ";s" "stop" nil 0 t)
538 (define-abbrev fortran-mode-abbrev-table ";sa" "save" nil 0 t)
539 (define-abbrev fortran-mode-abbrev-table ";st" "structure" nil 0 t)
540 (define-abbrev fortran-mode-abbrev-table ";sc" "static" nil 0 t)
541 (define-abbrev fortran-mode-abbrev-table ";su" "subroutine" nil 0 t)
542 (define-abbrev fortran-mode-abbrev-table ";tr" ".true." nil 0 t)
543 (define-abbrev fortran-mode-abbrev-table ";ty" "type" nil 0 t)
544 (define-abbrev fortran-mode-abbrev-table ";vo" "volatile" nil 0 t)
545 (define-abbrev fortran-mode-abbrev-table ";w" "write" nil 0 t)
546 (define-abbrev fortran-mode-abbrev-table ";wh" "where" nil 0 t)
547 (setq abbrevs-changed ac)
548 fortran-mode-abbrev-table))
550 (eval-when-compile ; silence compiler
551 (defvar imenu-case-fold-search)
552 (defvar imenu-syntax-alist))
554 (defcustom fortran-mode-hook nil
555 "Hook run by Fortran mode."
556 :type 'hook
557 :group 'fortran)
559 ;;;###autoload
560 (defun fortran-mode ()
561 "Major mode for editing Fortran code.
562 \\[fortran-indent-line] indents the current Fortran line correctly.
563 DO statements must not share a common CONTINUE.
565 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for
566 Fortran keywords.
568 Key definitions:
569 \\{fortran-mode-map}
571 Variables controlling indentation style and extra features:
573 `comment-start'
574 If you want to use comments starting with `!',
575 set this to the string \"!\".
576 `fortran-do-indent'
577 Extra indentation within do blocks. (default 3)
578 `fortran-if-indent'
579 Extra indentation within if blocks. (default 3)
580 `fortran-structure-indent'
581 Extra indentation within structure, union, map and interface blocks.
582 (default 3)
583 `fortran-continuation-indent'
584 Extra indentation applied to continuation statements. (default 5)
585 `fortran-comment-line-extra-indent'
586 Amount of extra indentation for text within full-line comments. (default 0)
587 `fortran-comment-indent-style'
588 nil means don't change indentation of text in full-line comments,
589 fixed means indent that text at `fortran-comment-line-extra-indent' beyond
590 the value of `fortran-minimum-statement-indent-fixed' (for fixed
591 format continuation style) or `fortran-minimum-statement-indent-tab'
592 (for TAB format continuation style).
593 relative means indent at `fortran-comment-line-extra-indent' beyond the
594 indentation for a line of code.
595 (default 'fixed)
596 `fortran-comment-indent-char'
597 Single-character string to be inserted instead of space for
598 full-line comment indentation. (default \" \")
599 `fortran-minimum-statement-indent-fixed'
600 Minimum indentation for Fortran statements in fixed format mode. (def.6)
601 `fortran-minimum-statement-indent-tab'
602 Minimum indentation for Fortran statements in TAB format mode. (default 9)
603 `fortran-line-number-indent'
604 Maximum indentation for line numbers. A line number will get
605 less than this much indentation if necessary to avoid reaching
606 column 5. (default 1)
607 `fortran-check-all-num-for-matching-do'
608 Non-nil causes all numbered lines to be treated as possible \"continue\"
609 statements. (default nil)
610 `fortran-blink-matching-if'
611 Non-nil causes \\[fortran-indent-line] on an ENDIF statement to blink on
612 matching IF. Also, from an ENDDO statement, blink on matching DO [WHILE]
613 statement. (default nil)
614 `fortran-continuation-string'
615 Single-character string to be inserted in column 5 of a continuation
616 line. (default \"$\")
617 `fortran-comment-region'
618 String inserted by \\[fortran-comment-region] at start of each line in
619 region. (default \"c$$$\")
620 `fortran-electric-line-number'
621 Non-nil causes line number digits to be moved to the correct column
622 as typed. (default t)
623 `fortran-break-before-delimiters'
624 Non-nil causes lines to be broken before delimiters.
625 (default t)
627 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
628 with no args, if that value is non-nil."
629 (interactive)
630 (kill-all-local-variables)
631 (setq local-abbrev-table fortran-mode-abbrev-table)
632 (set-syntax-table fortran-mode-syntax-table)
633 ;; Font Lock mode support.
634 (make-local-variable 'font-lock-defaults)
635 (setq font-lock-defaults '((fortran-font-lock-keywords
636 fortran-font-lock-keywords-1
637 fortran-font-lock-keywords-2
638 fortran-font-lock-keywords-3)
639 nil t ((?/ . "$/") ("_$" . "w"))
640 fortran-beginning-of-subprogram))
641 (set (make-local-variable 'font-lock-syntactic-keywords)
642 fortran-font-lock-syntactic-keywords)
643 (make-local-variable 'fortran-break-before-delimiters)
644 (setq fortran-break-before-delimiters t)
645 (make-local-variable 'indent-line-function)
646 (setq indent-line-function 'fortran-indent-line)
647 (make-local-variable 'comment-indent-function)
648 (setq comment-indent-function 'fortran-comment-indent)
649 (set (make-local-variable 'comment-start-skip)
650 ;; We can't reuse `fortran-comment-line-start-skip' directly because
651 ;; it contains backrefs whereas we need submatch-1 to end at the
652 ;; beginning of the comment delimiter.
653 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
654 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
655 (set (make-local-variable 'comment-padding) "$$$")
656 (make-local-variable 'comment-start)
657 (setq comment-start fortran-comment-line-start)
658 (make-local-variable 'require-final-newline)
659 (setq require-final-newline t)
660 (make-local-variable 'abbrev-all-caps)
661 (setq abbrev-all-caps t)
662 (make-local-variable 'indent-tabs-mode)
663 (setq indent-tabs-mode nil)
664 ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
665 (set (make-local-variable 'fill-column) 72)
666 (use-local-map fortran-mode-map)
667 (setq mode-name "Fortran")
668 (setq major-mode 'fortran-mode)
669 (make-local-variable 'fortran-comment-line-extra-indent)
670 (make-local-variable 'fortran-minimum-statement-indent-fixed)
671 (make-local-variable 'fortran-minimum-statement-indent-tab)
672 (make-local-variable 'fortran-column-ruler-fixed)
673 (make-local-variable 'fortran-column-ruler-tab)
674 (setq fortran-tab-mode-string " TAB-format")
675 (setq indent-tabs-mode (fortran-analyze-file-format))
676 (setq imenu-case-fold-search t)
677 (setq imenu-generic-expression fortran-imenu-generic-expression)
678 (setq imenu-syntax-alist '(("_$" . "w")))
679 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
680 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
681 (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
682 (set (make-local-variable 'indent-region-function)
683 (lambda (start end)
684 (let (fortran-blink-matching-if ; avoid blinking delay
685 indent-region-function)
686 (indent-region start end nil))))
687 (set (make-local-variable 'beginning-of-defun-function)
688 #'fortran-beginning-of-subprogram)
689 (set (make-local-variable 'end-of-defun-function)
690 #'fortran-end-of-subprogram)
691 (set (make-local-variable 'add-log-current-defun-function)
692 #'fortran-current-defun)
693 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
694 (run-hooks 'fortran-mode-hook))
696 (defsubst fortran-comment-indent ()
697 (save-excursion
698 (if (looking-at fortran-comment-line-start-skip) 0
699 (skip-chars-backward " \t")
700 (max (+ 1 (current-column))
701 comment-column))))
703 (defun fortran-indent-comment ()
704 "Align or create comment on current line.
705 Existing comments of all types are recognized and aligned.
706 If the line has no comment, a side-by-side comment is inserted and aligned
707 if the value of `comment-start' is not nil and allows such comments.
708 Otherwise, a separate-line comment is inserted, on this line
709 or on a new line inserted before this line if this line is not blank."
710 (interactive)
711 (beginning-of-line)
712 ;; Recognize existing comments of either kind.
713 (cond ((fortran-find-comment-start-skip 'all)
714 (goto-char (match-beginning 0))
715 (if (bolp)
716 (fortran-indent-line)
717 (if (not (= (current-column)
718 (fortran-comment-indent)))
719 (progn (delete-horizontal-space)
720 (indent-to (fortran-comment-indent))))))
721 ;; No existing comment.
722 ;; If side-by-side comments are defined, insert one,
723 ;; unless line is now blank.
724 ((and comment-start (not (looking-at "[ \t]*$"))
725 (string-match comment-start-skip (concat " " comment-start)))
726 (end-of-line)
727 (delete-horizontal-space)
728 (indent-to (fortran-comment-indent))
729 (insert comment-start))
730 ;; Else insert separate-line comment, making a new line if nec.
732 (if (looking-at "^[ \t]*$")
733 (delete-horizontal-space)
734 (beginning-of-line)
735 (insert ?\n)
736 (forward-char -1))
737 (insert fortran-comment-line-start)
738 (insert-char (if (stringp fortran-comment-indent-char)
739 (aref fortran-comment-indent-char 0)
740 fortran-comment-indent-char)
741 (- (fortran-calculate-indent) (current-column))))))
743 (defun fortran-comment-region (beg-region end-region arg)
744 "Comments every line in the region.
745 Puts `fortran-comment-region' at the beginning of every line in the region.
746 BEG-REGION and END-REGION are args which specify the region boundaries.
747 With non-nil ARG, uncomments the region."
748 (interactive "*r\nP")
749 (let ((end-region-mark (copy-marker end-region))
750 (save-point (point-marker)))
751 (goto-char beg-region)
752 (beginning-of-line)
753 (if (not arg) ;comment the region
754 (progn (insert fortran-comment-region)
755 (while (and (= (forward-line 1) 0)
756 (< (point) end-region-mark))
757 (insert fortran-comment-region)))
758 (let ((com (regexp-quote fortran-comment-region))) ;uncomment the region
759 (if (looking-at com)
760 (delete-region (point) (match-end 0)))
761 (while (and (= (forward-line 1) 0)
762 (< (point) end-region-mark))
763 (if (looking-at com)
764 (delete-region (point) (match-end 0))))))
765 (goto-char save-point)
766 (set-marker end-region-mark nil)
767 (set-marker save-point nil)))
769 (defun fortran-abbrev-start ()
770 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
771 Any other key combination is executed normally."
772 (interactive)
773 (let (c)
774 (insert last-command-char)
775 (if (or (eq (setq c (read-event)) ??) ;insert char if not equal to `?'
776 (eq c help-char))
777 (fortran-abbrev-help)
778 (setq unread-command-events (list c)))))
780 (defun fortran-abbrev-help ()
781 "List the currently defined abbrevs in Fortran mode."
782 (interactive)
783 (message "Listing abbrev table...")
784 (display-buffer (fortran-prepare-abbrev-list-buffer))
785 (message "Listing abbrev table...done"))
787 (defun fortran-prepare-abbrev-list-buffer ()
788 (save-excursion
789 (set-buffer (get-buffer-create "*Abbrevs*"))
790 (erase-buffer)
791 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
792 (goto-char (point-min))
793 (set-buffer-modified-p nil)
794 (edit-abbrevs-mode))
795 (get-buffer-create "*Abbrevs*"))
797 (defun fortran-column-ruler ()
798 "Insert a column ruler momentarily above current line, till next keystroke.
799 The ruler is defined by the value of `fortran-column-ruler-fixed' when in fixed
800 format mode, and `fortran-column-ruler-tab' when in TAB format mode.
801 The key typed is executed unless it is SPC."
802 (interactive)
803 (momentary-string-display
804 (if indent-tabs-mode
805 fortran-column-ruler-tab
806 fortran-column-ruler-fixed)
807 (save-excursion
808 (beginning-of-line)
809 (if (eq (window-start (selected-window))
810 (window-point (selected-window)))
811 (line-beginning-position 2)
812 (point)))
813 nil "Type SPC or any command to erase ruler."))
815 (defun fortran-window-create ()
816 "Make the window 72 columns wide.
817 See also `fortran-window-create-momentarily'."
818 (interactive)
819 (let ((window-min-width 2))
820 (if (< (window-width) (frame-width))
821 (enlarge-window-horizontally (- (frame-width)
822 (window-width) 1)))
823 (let* ((window-edges (window-edges))
824 (scroll-bar-width (- (nth 2 window-edges)
825 (car window-edges)
826 (window-width))))
827 (split-window-horizontally (+ 72 scroll-bar-width)))
828 (other-window 1)
829 (switch-to-buffer " fortran-window-extra" t)
830 (select-window (previous-window))))
832 (defun fortran-window-create-momentarily (&optional arg)
833 "Momentarily make the window 72 columns wide.
834 Optional ARG non-nil and non-unity disables the momentary feature.
835 See also `fortran-window-create'."
836 (interactive "p")
837 (if (or (not arg)
838 (= arg 1))
839 (save-window-excursion
840 (progn
841 (condition-case nil
842 (fortran-window-create)
843 (error (error "No room for Fortran window")))
844 (message "Type SPC to continue editing.")
845 (let ((char (read-event)))
846 (or (equal char (string-to-char " "))
847 (setq unread-command-events (list char))))))
848 (fortran-window-create)))
850 (defun fortran-split-line ()
851 "Break line at point and insert continuation marker and alignment."
852 (interactive)
853 (delete-horizontal-space)
854 (if (save-excursion
855 (let ((pos (point)))
856 (beginning-of-line)
857 (and (fortran-find-comment-start-skip 'all)
858 (< (match-beginning 0) pos))))
859 (insert ?\n (match-string 0))
860 (if indent-tabs-mode
861 (insert ?\n ?\t (fortran-numerical-continuation-char))
862 (insert "\n " fortran-continuation-string))) ; Space after \n important
863 (fortran-indent-line)) ; when the cont string is C, c or *.
865 (defun fortran-remove-continuation ()
866 (if (looking-at "\\( [^ 0\n]\\|\t[1-9]\\|&\\)")
867 (progn (replace-match "")
868 (delete-indentation)
869 t)))
871 (defun fortran-join-line (arg)
872 "Join current line to the previous one and re-indent.
873 With a prefix argument, repeat this operation that many times.
874 If the prefix argument ARG is negative, join the next -ARG lines.
875 Continuation lines are correctly handled."
876 (interactive "*p")
877 (save-excursion
878 (when (> 0 arg)
879 (setq arg (- arg))
880 (forward-line arg))
881 (while (not (zerop arg))
882 (beginning-of-line)
883 (or (fortran-remove-continuation)
884 (delete-indentation))
885 (setq arg (1- arg)))
886 (fortran-indent-line)))
888 (defun fortran-numerical-continuation-char ()
889 "Return a digit for tab-digit style of continuation lines.
890 If, previous line is a tab-digit continuation line, returns that digit
891 plus one. Otherwise return 1. Zero not allowed."
892 (save-excursion
893 (forward-line -1)
894 (if (looking-at "\t[1-9]")
895 (+ ?1 (% (- (char-after (+ (point) 1)) ?0) 9))
896 ?1)))
898 (put 'fortran-electric-line-number 'delete-selection t)
899 (defun fortran-electric-line-number (arg)
900 "Self insert, but if part of a Fortran line number indent it automatically.
901 Auto-indent does not happen if a numeric ARG is used."
902 (interactive "P")
903 (if (or arg (not fortran-electric-line-number))
904 (if arg
905 (self-insert-command (prefix-numeric-value arg))
906 (self-insert-command 1))
907 (if (or (and (= 5 (current-column))
908 (save-excursion
909 (beginning-of-line)
910 (looking-at " \\{5\\}"))) ;In col 5 with only spaces to left.
911 (and (= (if indent-tabs-mode
912 fortran-minimum-statement-indent-tab
913 fortran-minimum-statement-indent-fixed) (current-column))
914 (eq ?\t (char-after (line-beginning-position))) ;In col 8
915 ; with a single tab to the left.
916 (not (or (eq last-command 'fortran-indent-line)
917 (eq last-command
918 'fortran-indent-new-line))))
919 (save-excursion
920 (re-search-backward "[^ \t0-9]"
921 (line-beginning-position)
922 t)) ;not a line number
923 (looking-at "[0-9]")) ;within a line number
924 (self-insert-command (prefix-numeric-value arg))
925 (skip-chars-backward " \t")
926 (insert last-command-char)
927 (fortran-indent-line))))
929 (defvar fortran-end-prog-re1
930 "end\
931 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
932 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?")
933 (defvar fortran-end-prog-re
934 (concat "^[ \t0-9]*" fortran-end-prog-re1)
935 "Regexp possibly marking subprogram end.")
937 (defun fortran-check-end-prog-re ()
938 "Check a preliminary match against `fortran-end-prog-re'."
939 ;; Having got a possible match for the subprogram end, we need a
940 ;; match of whitespace, avoiding possible column 73+ stuff.
941 (save-match-data
942 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
943 (buffer-substring (match-end 0)
944 (min (line-end-position)
945 (+ 72 (line-beginning-position)))))))
947 ;; Note that you can't just check backwards for `subroutine' &c in
948 ;; case of un-marked main programs not at the start of the file.
949 (defun fortran-beginning-of-subprogram ()
950 "Move point to the beginning of the current Fortran subprogram."
951 (interactive)
952 (save-match-data
953 (let ((case-fold-search t))
954 (beginning-of-line -1)
955 (if (catch 'ok
956 (while (re-search-backward fortran-end-prog-re nil 'move)
957 (if (fortran-check-end-prog-re)
958 (throw 'ok t))))
959 (forward-line)))))
961 (defun fortran-end-of-subprogram ()
962 "Move point to the end of the current Fortran subprogram."
963 (interactive)
964 (save-match-data
965 (let ((case-fold-search t))
966 (if (save-excursion ; on END
967 (beginning-of-line)
968 (and (looking-at fortran-end-prog-re)
969 (fortran-check-end-prog-re)))
970 (forward-line)
971 (beginning-of-line 2)
972 (catch 'ok
973 (while (re-search-forward fortran-end-prog-re nil 'move)
974 (if (fortran-check-end-prog-re)
975 (throw 'ok t))))
976 (goto-char (match-beginning 0))
977 (forward-line)))))
979 (defun fortran-previous-statement ()
980 "Move point to beginning of the previous Fortran statement.
981 Returns 'first-statement if that statement is the first
982 non-comment Fortran statement in the file, and nil otherwise.
983 Preprocessor lines are treated as comments."
984 (interactive)
985 (let (not-first-statement continue-test)
986 (beginning-of-line)
987 (setq continue-test
988 (and
989 (not (looking-at fortran-comment-line-start-skip))
990 (not (looking-at fortran-preprocessor-re))
991 (or (looking-at
992 (concat "[ \t]*"
993 (regexp-quote fortran-continuation-string)))
994 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
995 (while (and (setq not-first-statement (= (forward-line -1) 0))
996 (or (looking-at fortran-comment-line-start-skip)
997 (looking-at fortran-preprocessor-re)
998 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
999 (looking-at (concat "[ \t]*" comment-start-skip)))))
1000 (cond ((and continue-test
1001 (not not-first-statement))
1002 (message "Incomplete continuation statement."))
1003 (continue-test
1004 (fortran-previous-statement))
1005 ((not not-first-statement)
1006 'first-statement))))
1008 (defun fortran-next-statement ()
1009 "Move point to beginning of the next Fortran statement.
1010 Returns 'last-statement if that statement is the last
1011 non-comment Fortran statement in the file, and nil otherwise.
1012 Preprocessor lines are treated as comments."
1013 (interactive)
1014 (let (not-last-statement)
1015 (beginning-of-line)
1016 (while (and (setq not-last-statement
1017 (and (= (forward-line 1) 0)
1018 (not (eobp))))
1019 (or (looking-at fortran-comment-line-start-skip)
1020 (looking-at fortran-preprocessor-re)
1021 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1022 (looking-at (concat "[ \t]*" comment-start-skip)))))
1023 (if (not not-last-statement)
1024 'last-statement)))
1026 (defun fortran-blink-match (regex keyword find-begin)
1027 "From a line matching REGEX, blink matching KEYWORD statement line.
1028 Use function FIND-BEGIN to match it."
1029 (let ((top-of-window (window-start))
1030 (end-point (point))
1031 (case-fold-search t)
1032 matching
1033 message)
1034 (if (save-excursion
1035 (beginning-of-line)
1036 (skip-chars-forward " \t0-9")
1037 (looking-at regex))
1038 (progn
1039 (if (not (setq matching (funcall find-begin)))
1040 (setq message (concat "No matching " keyword "."))
1041 (if (< matching top-of-window)
1042 (save-excursion
1043 (goto-char matching)
1044 (beginning-of-line)
1045 (setq message
1046 (concat "Matches "
1047 (buffer-substring (point)
1048 (line-end-position)))))))
1049 (if message
1050 (message "%s" message)
1051 (goto-char matching)
1052 (sit-for 1)
1053 (goto-char end-point))))))
1055 (defun fortran-blink-matching-if ()
1056 "From an ENDIF or ELSE statement, blink the matching IF statement."
1057 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1058 "if" #'fortran-beginning-if))
1060 (defun fortran-blink-matching-do ()
1061 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1062 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
1064 (defun fortran-mark-do ()
1065 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1066 The marks are pushed."
1067 (interactive)
1068 (let (enddo-point do-point)
1069 (if (setq enddo-point (fortran-end-do))
1070 (if (not (setq do-point (fortran-beginning-do)))
1071 (message "No matching do.")
1072 (goto-char enddo-point)
1073 (push-mark)
1074 (goto-char do-point)))))
1076 (defun fortran-end-do ()
1077 "Search forward for first unmatched ENDDO.
1078 Return point or nil."
1079 (let ((case-fold-search t))
1080 (if (save-excursion (beginning-of-line)
1081 (skip-chars-forward " \t0-9")
1082 (looking-at "end[ \t]*do\\b"))
1083 ;; Sitting on one.
1084 (match-beginning 0)
1085 ;; Search for one.
1086 (save-excursion
1087 (let ((count 1))
1088 (while (and (not (= count 0))
1089 (not (eq (fortran-next-statement) 'last-statement))
1090 ;; Keep local to subprogram
1091 (not (and (looking-at fortran-end-prog-re)
1092 (fortran-check-end-prog-re))))
1093 (skip-chars-forward " \t0-9")
1094 (cond ((looking-at "end[ \t]*do\\b")
1095 (setq count (1- count)))
1096 ((looking-at
1097 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1098 (setq count (+ count 1)))))
1099 (and (= count 0)
1100 ;; All pairs accounted for.
1101 (point)))))))
1103 (defun fortran-beginning-do ()
1104 "Search backwards for first unmatched DO [WHILE].
1105 Return point or nil."
1106 (let ((case-fold-search t))
1107 (if (save-excursion
1108 (beginning-of-line)
1109 (skip-chars-forward " \t0-9")
1110 (looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+"))
1111 ;; Sitting on one.
1112 (match-beginning 0)
1113 ;; Search for one.
1114 (save-excursion
1115 (let ((count 1))
1116 (while (and (not (= count 0))
1117 (not (eq (fortran-previous-statement) 'first-statement))
1118 ;; Keep local to subprogram
1119 (not (and (looking-at fortran-end-prog-re)
1120 (fortran-check-end-prog-re))))
1121 (skip-chars-forward " \t0-9")
1122 (cond ((looking-at
1123 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+")
1124 (setq count (1- count)))
1125 ((looking-at "end[ \t]*do\\b")
1126 (setq count (1+ count)))))
1127 (and (= count 0)
1128 ;; All pairs accounted for.
1129 (point)))))))
1131 (defun fortran-mark-if ()
1132 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1133 The marks are pushed."
1134 (interactive)
1135 (let (endif-point if-point)
1136 (if (setq endif-point (fortran-end-if))
1137 (if (not (setq if-point (fortran-beginning-if)))
1138 (message "No matching if.")
1139 ;; Set mark, move point.
1140 (goto-char endif-point)
1141 (push-mark)
1142 (goto-char if-point)))))
1144 (defvar fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1146 (defun fortran-end-if ()
1147 "Search forwards for first unmatched ENDIF.
1148 Return point or nil."
1149 (let ((case-fold-search t))
1150 (if (save-excursion (beginning-of-line)
1151 (skip-chars-forward " \t0-9")
1152 (looking-at "end[ \t]*if\\b"))
1153 ;; Sitting on one.
1154 (match-beginning 0)
1155 ;; Search for one. The point has been already been moved to first
1156 ;; letter on line but this should not cause troubles.
1157 (save-excursion
1158 (let ((count 1))
1159 (while (and (not (= count 0))
1160 (not (eq (fortran-next-statement) 'last-statement))
1161 ;; Keep local to subprogram.
1162 (not (and (looking-at fortran-end-prog-re)
1163 (fortran-check-end-prog-re))))
1164 (skip-chars-forward " \t0-9")
1165 (cond ((looking-at "end[ \t]*if\\b")
1166 (setq count (- count 1)))
1167 ((looking-at fortran-if-start-re)
1168 (save-excursion
1169 (if (or
1170 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1171 (let (then-test) ; Multi-line if-then.
1172 (while
1173 (and
1174 (= (forward-line 1) 0)
1175 ;; Search forward for then.
1176 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1177 (not
1178 (setq then-test
1179 (looking-at
1180 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1181 then-test))
1182 (setq count (+ count 1)))))))
1183 (and (= count 0)
1184 ;; All pairs accounted for.
1185 (point)))))))
1187 (defun fortran-beginning-if ()
1188 "Search backwards for first unmatched IF-THEN.
1189 Return point or nil."
1190 (let ((case-fold-search t))
1191 (if (save-excursion
1192 ;; May be sitting on multi-line if-then statement, first
1193 ;; move to beginning of current statement. Note:
1194 ;; `fortran-previous-statement' moves to previous statement
1195 ;; *unless* current statement is first one. Only move
1196 ;; forward if not first-statement.
1197 (if (not (eq (fortran-previous-statement) 'first-statement))
1198 (fortran-next-statement))
1199 (skip-chars-forward " \t0-9")
1200 (and
1201 (looking-at fortran-if-start-re)
1202 (save-match-data
1203 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1204 ;; Multi-line if-then.
1205 (let (then-test)
1206 (while
1207 (and (= (forward-line 1) 0)
1208 ;; Search forward for then.
1209 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1210 (not
1211 (setq then-test
1212 (looking-at
1213 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1214 then-test)))))
1215 ;; Sitting on one.
1216 (match-beginning 0)
1217 ;; Search for one.
1218 (save-excursion
1219 (let ((count 1))
1220 (while (and (not (= count 0))
1221 (not (eq (fortran-previous-statement) 'first-statement))
1222 ;; Keep local to subprogram.
1223 (not (and (looking-at fortran-end-prog-re)
1224 (fortran-check-end-prog-re))))
1225 (skip-chars-forward " \t0-9")
1226 (cond ((looking-at fortran-if-start-re)
1227 (save-excursion
1228 (if (or
1229 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1230 (let (then-test) ; Multi-line if-then.
1231 (while
1232 (and
1233 (= (forward-line 1) 0)
1234 ;; Search forward for then.
1235 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1236 (not
1237 (setq then-test
1238 (looking-at
1239 (concat ".*then\\b[ \t]*"
1240 "[^ \t(=a-z0-9]"))))))
1241 then-test))
1242 (setq count (- count 1)))))
1243 ((looking-at "end[ \t]*if\\b")
1244 (setq count (+ count 1)))))
1245 (and (= count 0)
1246 ;; All pairs accounted for.
1247 (point)))))))
1249 (defun fortran-indent-line ()
1250 "Indent current Fortran line based on its contents and on previous lines."
1251 (interactive)
1252 (let ((cfi (fortran-calculate-indent)))
1253 (save-excursion
1254 (beginning-of-line)
1255 (if (or (not (= cfi (fortran-current-line-indentation)))
1256 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1257 (not (fortran-line-number-indented-correctly-p))))
1258 (fortran-indent-to-column cfi)
1259 (beginning-of-line)
1260 (if (fortran-find-comment-start-skip)
1261 (fortran-indent-comment))))
1262 ;; Never leave point in left margin.
1263 (if (< (current-column) cfi)
1264 (move-to-column cfi))
1265 (if (and auto-fill-function
1266 (> (save-excursion (end-of-line) (current-column))
1267 fill-column))
1268 (save-excursion
1269 (end-of-line)
1270 (fortran-fill)))
1271 (if fortran-blink-matching-if
1272 (progn
1273 (fortran-blink-matching-if)
1274 (fortran-blink-matching-do)))))
1276 (defun fortran-auto-fill ()
1277 (if (> (current-column) (current-fill-column))
1278 (let ((cfi (fortran-calculate-indent)))
1279 (save-excursion
1280 (beginning-of-line)
1281 (if (or (not (= cfi (fortran-current-line-indentation)))
1282 (and (re-search-forward "^[ \t]*[0-9]+"
1283 (+ (point) 4) t)
1284 (not (fortran-line-number-indented-correctly-p))))
1285 (fortran-indent-to-column cfi)
1286 (beginning-of-line)
1287 (if (fortran-find-comment-start-skip)
1288 (fortran-indent-comment))))
1289 (fortran-fill)
1290 ;; Never leave point in left margin.
1291 (if (< (current-column) cfi)
1292 (move-to-column cfi)))))
1294 ;; Historically this was a separate function which advertised itself
1295 ;; as reindenting but only did so where `most likely to be necessary'.
1296 (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
1298 (defun fortran-indent-subprogram ()
1299 "Properly indent the Fortran subprogram which contains point."
1300 (interactive)
1301 (save-excursion
1302 (mark-defun)
1303 (message "Indenting subprogram...")
1304 (indent-region (point) (mark) nil))
1305 (message "Indenting subprogram...done."))
1307 (defun fortran-calculate-indent ()
1308 "Calculates the Fortran indent column based on previous lines."
1309 (let (icol first-statement (case-fold-search t)
1310 (fortran-minimum-statement-indent
1311 (if indent-tabs-mode
1312 fortran-minimum-statement-indent-tab
1313 fortran-minimum-statement-indent-fixed)))
1314 (save-excursion
1315 (setq first-statement (fortran-previous-statement))
1316 (if first-statement
1317 (setq icol fortran-minimum-statement-indent)
1318 (progn
1319 (if (= (point) (point-min))
1320 (setq icol fortran-minimum-statement-indent)
1321 (setq icol (fortran-current-line-indentation)))
1322 (skip-chars-forward " \t0-9")
1323 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1324 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1325 (let (then-test) ;multi-line if-then
1326 (while (and (= (forward-line 1) 0)
1327 ;;search forward for then
1328 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1329 (not (setq then-test
1330 (looking-at
1331 ".*then\\b[ \t]\
1332 *[^ \t_$(=a-z0-9]")))))
1333 then-test))
1334 (setq icol (+ icol fortran-if-indent))))
1335 ((looking-at "else\\(if\\)?\\b")
1336 (setq icol (+ icol fortran-if-indent)))
1337 ((looking-at "select[ \t]*case[ \t](.*)")
1338 (setq icol (+ icol fortran-if-indent)))
1339 ((looking-at "case[ \t]*(.*)")
1340 (setq icol (+ icol fortran-if-indent)))
1341 ((looking-at "case[ \t]*default\\b")
1342 (setq icol (+ icol fortran-if-indent)))
1343 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1344 (setq icol (+ icol fortran-if-indent)))
1345 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1346 (setq icol (+ icol fortran-if-indent)))
1347 ((looking-at "do\\b")
1348 (setq icol (+ icol fortran-do-indent)))
1349 ((looking-at
1350 "\\(structure\\|union\\|map\\|interface\\)\
1351 \\b[ \t]*[^ \t=(a-z]")
1352 (setq icol (+ icol fortran-structure-indent)))
1353 ((and (looking-at fortran-end-prog-re1)
1354 (fortran-check-end-prog-re))
1355 ;; Previous END resets indent to minimum
1356 (setq icol fortran-minimum-statement-indent))))))
1357 (save-excursion
1358 (beginning-of-line)
1359 (cond ((looking-at "[ \t]*$"))
1360 ((looking-at fortran-comment-line-start-skip)
1361 (cond ((eq fortran-comment-indent-style 'relative)
1362 (setq icol (+ icol fortran-comment-line-extra-indent)))
1363 ((eq fortran-comment-indent-style 'fixed)
1364 (setq icol (+ fortran-minimum-statement-indent
1365 fortran-comment-line-extra-indent))))
1366 (setq fortran-minimum-statement-indent 0))
1367 ((or (looking-at (concat "[ \t]*"
1368 (regexp-quote
1369 fortran-continuation-string)))
1370 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1371 (setq icol (+ icol fortran-continuation-indent)))
1372 ((looking-at fortran-preprocessor-re) ; Check for cpp directive.
1373 (setq fortran-minimum-statement-indent 0 icol 0))
1374 (first-statement)
1375 ((and fortran-check-all-num-for-matching-do
1376 (looking-at "[ \t]*[0-9]+")
1377 (fortran-check-for-matching-do))
1378 (setq icol (- icol fortran-do-indent)))
1380 (skip-chars-forward " \t0-9")
1381 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1382 (setq icol (- icol fortran-if-indent)))
1383 ((looking-at "else\\(if\\)?\\b")
1384 (setq icol (- icol fortran-if-indent)))
1385 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1386 (setq icol (- icol fortran-if-indent)))
1387 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1388 (setq icol (- icol fortran-if-indent)))
1389 ((and (looking-at "continue\\b")
1390 (fortran-check-for-matching-do))
1391 (setq icol (- icol fortran-do-indent)))
1392 ((looking-at "end[ \t]*do\\b")
1393 (setq icol (- icol fortran-do-indent)))
1394 ((looking-at "end[ \t]*\
1395 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1396 (setq icol (- icol fortran-structure-indent)))
1397 ((and (looking-at fortran-end-prog-re1)
1398 (fortran-check-end-prog-re)
1399 (not (= icol fortran-minimum-statement-indent)))
1400 (message "Warning: `end' not in column %d. Probably\
1401 an unclosed block." fortran-minimum-statement-indent))))))
1402 (max fortran-minimum-statement-indent icol)))
1404 (defun fortran-current-line-indentation ()
1405 "Indentation of current line, ignoring Fortran line number or continuation.
1406 This is the column position of the first non-whitespace character
1407 aside from the line number and/or column 5/8 line-continuation character.
1408 For comment lines, returns indentation of the first
1409 non-indentation text within the comment."
1410 (save-excursion
1411 (beginning-of-line)
1412 (cond ((looking-at fortran-comment-line-start-skip)
1413 (goto-char (match-end 0))
1414 (skip-chars-forward
1415 (if (stringp fortran-comment-indent-char)
1416 fortran-comment-indent-char
1417 (char-to-string fortran-comment-indent-char))))
1418 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1419 (goto-char (match-end 0)))
1421 ;; Move past line number.
1422 (skip-chars-forward "[ \t0-9]")))
1423 ;; Move past whitespace.
1424 (skip-chars-forward " \t")
1425 (current-column)))
1427 (defun fortran-indent-to-column (col)
1428 "Indent current line with spaces to column COL.
1429 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1430 line, and this continuation character is retained on indentation;
1431 2) If `fortran-continuation-string' is the first non-whitespace
1432 character, this is a continuation line;
1433 3) A non-continuation line which has a number as the first
1434 non-whitespace character is a numbered line.
1435 4) A TAB followed by a digit indicates a continuation line."
1436 (save-excursion
1437 (beginning-of-line)
1438 (if (looking-at fortran-comment-line-start-skip)
1439 (if fortran-comment-indent-style
1440 (let* ((char (if (stringp fortran-comment-indent-char)
1441 (aref fortran-comment-indent-char 0)
1442 fortran-comment-indent-char))
1443 (chars (string ? ?\t char)))
1444 (goto-char (match-end 0))
1445 (skip-chars-backward chars)
1446 (delete-region (point) (progn (skip-chars-forward chars)
1447 (point)))
1448 (insert-char char (- col (current-column)))))
1449 (if (looking-at "\t[1-9]")
1450 (if indent-tabs-mode
1451 (goto-char (match-end 0))
1452 (delete-char 2)
1453 (insert-char ? 5)
1454 (insert fortran-continuation-string))
1455 (if (looking-at " \\{5\\}[^ 0\n]")
1456 (if indent-tabs-mode
1457 (progn (delete-char 6)
1458 (insert ?\t (fortran-numerical-continuation-char) 1))
1459 (forward-char 6))
1460 (delete-horizontal-space)
1461 ;; Put line number in columns 0-4
1462 ;; or put continuation character in column 5.
1463 (cond ((eobp))
1464 ((looking-at (regexp-quote fortran-continuation-string))
1465 (if indent-tabs-mode
1466 (progn
1467 (indent-to
1468 (if indent-tabs-mode
1469 fortran-minimum-statement-indent-tab
1470 fortran-minimum-statement-indent-fixed))
1471 (delete-char 1)
1472 (insert-char (fortran-numerical-continuation-char) 1))
1473 (indent-to 5)
1474 (forward-char 1)))
1475 ((looking-at "[0-9]+")
1476 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1477 (if (< extra-space 0)
1478 (message "Warning: line number exceeds 5-digit limit.")
1479 (indent-to (min fortran-line-number-indent extra-space))))
1480 (skip-chars-forward "0-9")))))
1481 ;; Point is now after any continuation character or line number.
1482 ;; Put body of statement where specified.
1483 (delete-horizontal-space)
1484 (indent-to col)
1485 ;; Indent any comment following code on the same line.
1486 (if (fortran-find-comment-start-skip)
1487 (progn (goto-char (match-beginning 0))
1488 (if (not (= (current-column) (fortran-comment-indent)))
1489 (progn (delete-horizontal-space)
1490 (indent-to (fortran-comment-indent)))))))))
1492 (defun fortran-line-number-indented-correctly-p ()
1493 "Return t if current line's line number is correctly indented.
1494 Do not call if there is no line number."
1495 (save-excursion
1496 (beginning-of-line)
1497 (skip-chars-forward " \t")
1498 (and (<= (current-column) fortran-line-number-indent)
1499 (or (= (current-column) fortran-line-number-indent)
1500 (progn (skip-chars-forward "0-9")
1501 (= (current-column) 5))))))
1503 (defun fortran-check-for-matching-do ()
1504 "When called from a numbered statement, return t if matching DO is found.
1505 Otherwise return nil."
1506 (let ((case-fold-search t)
1507 charnum)
1508 (save-excursion
1509 (beginning-of-line)
1510 (if (looking-at "[ \t]*[0-9]+")
1511 (progn
1512 (skip-chars-forward " \t")
1513 (skip-chars-forward "0") ;skip past leading zeros
1514 (setq charnum
1515 (buffer-substring (point) (progn
1516 (skip-chars-forward "0-9")
1517 (point))))
1518 (beginning-of-line)
1519 (save-restriction
1520 (save-excursion
1521 (narrow-to-defun)
1522 (and (re-search-backward
1523 (concat
1524 "\\(^[ \t0-9]*do[ \t]*0*"
1525 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1526 charnum "\\b\\)")
1527 nil t)
1528 (looking-at
1529 (concat "^[ \t0-9]*do[ \t]*0*"
1530 charnum))))))))))
1532 (defun fortran-find-comment-start-skip (&optional all)
1533 "Move to past `comment-start-skip' found on current line.
1534 Return non-nil if `comment-start-skip' found, nil if not.
1535 If ALL is nil, only match comments that start in column > 0."
1536 (interactive)
1537 ;; Hopefully at some point we can just use the line below! -stef
1538 ;; (comment-search-forward (line-end-position) t))
1539 (when (or all comment-start-skip)
1540 (let ((pos (point))
1541 (css (if comment-start-skip
1542 (concat fortran-comment-line-start-skip
1543 "\\|" comment-start-skip)
1544 fortran-comment-line-start-skip)))
1545 (when (re-search-forward css (line-end-position) t)
1546 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1547 (or (save-match-data
1548 (not (fortran-is-in-string-p (match-beginning 0))))
1549 ;; Recurse for rest of line.
1550 (fortran-find-comment-start-skip all)))
1551 (point)
1552 (goto-char pos)
1553 nil)))))
1555 ;;From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1556 ;; Test if TAB format continuation lines work.
1557 (defun fortran-is-in-string-p (where)
1558 "Return non-nil iff WHERE (a buffer position) is inside a Fortran string."
1559 (save-excursion
1560 (goto-char where)
1561 (cond
1562 ((bolp) nil) ; bol is never inside a string
1563 ((save-excursion ; comment lines too
1564 (beginning-of-line)
1565 (looking-at fortran-comment-line-start-skip)) nil)
1566 (t (let (;; ok, serious now. Init some local vars:
1567 (parse-state '(0 nil nil nil nil nil 0))
1568 (quoted-comment-start (if comment-start
1569 (regexp-quote comment-start)))
1570 (not-done t)
1571 parse-limit end-of-line)
1572 ;; move to start of current statement
1573 (fortran-next-statement)
1574 (fortran-previous-statement)
1575 ;; now parse up to WHERE
1576 (while not-done
1577 (if (or ;; skip to next line if:
1578 ;; - comment line?
1579 (looking-at fortran-comment-line-start-skip)
1580 ;; - at end of line?
1581 (eolp)
1582 ;; - not in a string and after comment-start?
1583 (and (not (nth 3 parse-state))
1584 comment-start
1585 (equal comment-start
1586 (char-to-string (preceding-char)))))
1587 (if (> (forward-line) 0)
1588 (setq not-done nil))
1589 ;; else:
1590 ;; if we are at beginning of code line, skip any
1591 ;; whitespace, labels and tab continuation markers.
1592 (if (bolp) (skip-chars-forward " \t0-9"))
1593 ;; if we are in column <= 5 now, check for continuation char
1594 (cond ((= 5 (current-column)) (forward-char 1))
1595 ((and (< (current-column) 5)
1596 (equal fortran-continuation-string
1597 (char-to-string (following-char)))
1598 (forward-char 1))))
1599 ;; find out parse-limit from here
1600 (setq end-of-line (line-end-position))
1601 (setq parse-limit (min where end-of-line))
1602 ;; parse max up to comment-start, if non-nil and in current line
1603 (if comment-start
1604 (save-excursion
1605 (if (re-search-forward quoted-comment-start end-of-line t)
1606 (setq parse-limit (min (point) parse-limit)))))
1607 ;; now parse if still in limits
1608 (if (< (point) where)
1609 (setq parse-state (parse-partial-sexp
1610 (point) parse-limit nil nil parse-state))
1611 (setq not-done nil))))
1612 ;; result is
1613 (nth 3 parse-state))))))
1615 ;; From old version.
1616 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1618 (defun fortran-fill ()
1619 (let* ((auto-fill-function #'fortran-auto-fill)
1620 (opoint (point))
1621 (bol (line-beginning-position))
1622 (eol (line-end-position))
1623 (bos (min eol (+ bol (fortran-current-line-indentation))))
1624 (quote
1625 (save-excursion
1626 (goto-char bol)
1627 (if (looking-at fortran-comment-line-start-skip)
1628 nil ; OK to break quotes on comment lines.
1629 (move-to-column fill-column)
1630 (if (fortran-is-in-string-p (point))
1631 (save-excursion (re-search-backward "\\S\"\\s\"\\S\"" bol t)
1632 (if fortran-break-before-delimiters
1633 (point)
1634 (1+ (point))))))))
1635 ;; decide where to split the line. If a position for a quoted
1636 ;; string was found above then use that, else break the line
1637 ;; before the last delimiter.
1638 ;; Delimiters are whitespace, commas, and operators.
1639 ;; Will break before a pair of *'s.
1640 (fill-point
1641 (or quote
1642 (save-excursion
1643 (move-to-column (1+ fill-column))
1644 (skip-chars-backward "^ \t\n,'+-/*=)"
1645 ;;; (if fortran-break-before-delimiters
1646 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1648 (if (<= (point) (1+ bos))
1649 (progn
1650 (move-to-column (1+ fill-column))
1651 ;;what is this doing???
1652 (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t))
1653 (goto-char bol))))
1654 (if (bolp)
1655 (re-search-forward "[ \t]" opoint t)
1656 (backward-char)
1657 (if (looking-at "\\s\"")
1658 (forward-char)
1659 (skip-chars-backward " \t\*")))
1660 (if fortran-break-before-delimiters
1661 (point)
1662 (1+ (point)))))))
1663 ;; if we are in an in-line comment, don't break unless the
1664 ;; line of code is longer than it should be. Otherwise
1665 ;; break the line at the column computed above.
1667 ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
1668 ;; don't prevent a break.
1669 (when (and (save-excursion
1670 (beginning-of-line)
1671 (when (fortran-find-comment-start-skip)
1672 (goto-char (match-beginning 0))
1673 (>= (point) fill-point)))
1674 (save-excursion
1675 (goto-char fill-point)
1676 (not (bolp)))
1677 (> (save-excursion
1678 (goto-char opoint)
1679 (current-column))
1680 (min (1+ fill-column)
1681 (+ (fortran-calculate-indent)
1682 fortran-continuation-indent))))
1683 (goto-char fill-point)
1684 (fortran-break-line)
1685 (end-of-line))))
1687 (defun fortran-break-line ()
1688 (let ((opoint (point))
1689 (bol (line-beginning-position))
1690 (comment-string
1691 (save-excursion
1692 (if (fortran-find-comment-start-skip)
1693 (delete-and-extract-region
1694 (match-beginning 0) (line-end-position))))))
1695 ;; Forward line 1 really needs to go to next non white line
1696 (if (save-excursion (forward-line)
1697 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1698 (progn
1699 (end-of-line)
1700 (delete-region (point) (match-end 0))
1701 (delete-horizontal-space)
1702 (fortran-fill))
1703 (fortran-split-line))
1704 (if comment-string
1705 (save-excursion
1706 (goto-char bol)
1707 (end-of-line)
1708 (delete-horizontal-space)
1709 (indent-to (fortran-comment-indent))
1710 (insert comment-string)))))
1712 (defun fortran-analyze-file-format ()
1713 "Return nil if fixed format is used, t if TAB formatting is used.
1714 Use `fortran-tab-mode-default' if no non-comment statements are found in the
1715 file before the end or the first `fortran-analyze-depth' lines."
1716 (let ((i 0))
1717 (save-excursion
1718 (goto-char (point-min))
1719 (setq i 0)
1720 (while (not (or
1721 (eobp)
1722 (eq (char-after) ?\t)
1723 (looking-at " \\{6\\}")
1724 (> i fortran-analyze-depth)))
1725 (forward-line)
1726 (setq i (1+ i)))
1727 (cond
1728 ((eq (char-after) ?\t) t)
1729 ((looking-at " \\{6\\}") nil)
1730 (fortran-tab-mode-default t)
1731 (t nil)))))
1733 (or (assq 'fortran-tab-mode-string minor-mode-alist)
1734 (setq minor-mode-alist (cons
1735 '(fortran-tab-mode-string
1736 (indent-tabs-mode fortran-tab-mode-string))
1737 minor-mode-alist)))
1739 (defun fortran-fill-paragraph (&optional justify)
1740 "Fill surrounding comment block as paragraphs, else fill statement.
1741 Intended as the value of `fill-paragraph-function'."
1742 (interactive "P")
1743 (save-excursion
1744 (beginning-of-line)
1745 (if (not (looking-at fortran-comment-line-start-skip))
1746 (fortran-fill-statement)
1747 ;; We're in a comment block. Find the start and end of a
1748 ;; paragraph, delimited either by non-comment lines or empty
1749 ;; comments. (Get positions as markers, since the
1750 ;; `indent-region' below can shift the block's end).
1751 (let* ((non-empty-comment
1752 (concat fortran-comment-line-start-skip "[^ \t\n]"))
1753 (start (save-excursion
1754 ;; Find (start of) first line.
1755 (while (and (zerop (forward-line -1))
1756 (looking-at non-empty-comment)))
1757 (or (looking-at non-empty-comment)
1758 (forward-line)) ; overshot
1759 (point-marker)))
1760 (end (save-excursion
1761 ;; Find start of first line past region to fill.
1762 (while (progn
1763 (forward-line)
1764 (looking-at non-empty-comment)))
1765 (point-marker))))
1766 ;; Indent the block, find the string comprising the effective
1767 ;; comment start skip and use that as a fill-prefix for
1768 ;; filling the region.
1769 (indent-region start end nil)
1770 (let ((paragraph-ignore-fill-prefix nil)
1771 (fill-prefix (progn
1772 (beginning-of-line)
1773 (looking-at fortran-comment-line-start-skip)
1774 (match-string 0))))
1775 (let (fill-paragraph-function)
1776 (fill-region start end justify))) ; with normal `fill-paragraph'
1777 (set-marker start nil)
1778 (set-marker end nil))))
1781 (defun fortran-fill-statement ()
1782 "Fill a fortran statement up to `fill-column'."
1783 (interactive)
1784 (let ((auto-fill-function #'fortran-auto-fill))
1785 (if (not (save-excursion
1786 (beginning-of-line)
1787 (or (looking-at "[ \t]*$")
1788 (looking-at fortran-comment-line-start-skip)
1789 (and comment-start-skip
1790 (looking-at (concat "[ \t]*" comment-start-skip))))))
1791 (save-excursion
1792 ;; Find beginning of statement.
1793 (fortran-next-statement)
1794 (fortran-previous-statement)
1795 ;; Re-indent initially.
1796 (fortran-indent-line)
1797 ;; Replace newline plus continuation field plus indentation with
1798 ;; single space.
1799 (while (progn
1800 (forward-line)
1801 (fortran-remove-continuation)))
1802 (fortran-previous-statement)))
1803 (fortran-indent-line)))
1805 (defun fortran-strip-sequence-nos (&optional do-space)
1806 "Delete all text in column 72 and up (assumed to be sequence numbers).
1807 Normally also deletes trailing whitespace after stripping such text.
1808 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
1809 (interactive "p")
1810 (save-excursion
1811 (goto-char (point-min))
1812 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t)
1813 (replace-match "" nil nil nil 1)
1814 (unless do-space (delete-horizontal-space)))))
1816 ;; This code used to live in add-log.el, but this is a better place
1817 ;; for it.
1818 (defun fortran-current-defun ()
1819 "Function to use for `add-log-current-defun-function' in Fortran mode."
1820 ;; We must be inside function body for this to work.
1821 (fortran-beginning-of-subprogram)
1822 (let ((case-fold-search t)) ; case-insensitive
1823 ;; search for fortran subprogram start
1824 (if (re-search-forward
1825 (concat "^[ \t]*\\(program\\|subroutine\\|function"
1826 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
1827 "\\(block[ \t]*data\\)\\)")
1828 (save-excursion (fortran-end-of-subprogram)
1829 (point))
1831 (or (match-string-no-properties 2)
1832 (progn
1833 ;; move to EOL or before first left paren
1834 (if (re-search-forward "[(\n]" nil t)
1835 (progn (backward-char)
1836 (skip-chars-backward " \t"))
1837 (end-of-line))
1838 ;; Use the name preceding that.
1839 (buffer-substring-no-properties (point) (progn (backward-sexp)
1840 (point)))))
1841 "main")))
1843 (provide 'fortran)
1845 ;;; fortran.el ends here