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