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