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