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