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