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