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