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