(c-offsets-alist): Fix typo.
[bpt/emacs.git] / lisp / progmodes / fortran.el
1 ;;; fortran.el --- Fortran mode for GNU Emacs
2
3 ;; Copyright (c) 1986, 93, 94, 95, 97, 98, 99, 2000, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Michael D. Prange <prange@erl.mit.edu>
7 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
8 ;; Keywords: languages
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This mode is documented in the Emacs manual.
30 ;;
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).
36
37 ;;; History:
38
39 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
40
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.
45
46 ;;; Code:
47
48 ;; Todo:
49
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.
55
56 (defgroup fortran nil
57 "Fortran mode for Emacs"
58 :link '(custom-manual "(emacs)Fortran")
59 :group 'languages)
60
61 (defgroup fortran-indent nil
62 "Indentation variables in Fortran mode"
63 :prefix "fortran-"
64 :group 'fortran)
65
66 (defgroup fortran-comment nil
67 "Comment-handling variables in Fortran mode"
68 :prefix "fortran-"
69 :group 'fortran)
70
71
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)
80
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)
87
88 (defcustom fortran-do-indent 3
89 "*Extra indentation applied to DO blocks."
90 :type 'integer
91 :group 'fortran-indent)
92
93 (defcustom fortran-if-indent 3
94 "*Extra indentation applied to IF blocks."
95 :type 'integer
96 :group 'fortran-indent)
97
98 (defcustom fortran-structure-indent 3
99 "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
100 :type 'integer
101 :group 'fortran-indent)
102
103 (defcustom fortran-continuation-indent 5
104 "*Extra indentation applied to Fortran continuation lines."
105 :type 'integer
106 :group 'fortran-indent)
107
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)
119
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)
125
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)
132
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)
141
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)
150
151 (defcustom fortran-minimum-statement-indent-fixed 6
152 "*Minimum statement indentation for fixed format continuation style."
153 :type 'integer
154 :group 'fortran-indent)
155
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)
160
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)
169
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)
175
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)
180
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)
186
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)
196
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)
202
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)
208
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.")
216
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.")
224
225 (defvar fortran-analyze-depth 100
226 "Number of lines to scan to determine whether to use fixed or TAB \
227 format style.")
228
229 (defcustom fortran-break-before-delimiters t
230 "*Non-nil causes filling to break lines before delimiters."
231 :type 'boolean
232 :group 'fortran)
233
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.")
258
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.
264
265 (defvar fortran-font-lock-keywords-1 nil
266 "Subdued level highlighting for Fortran mode.")
267
268 (defvar fortran-font-lock-keywords-2 nil
269 "Medium level highlighting for Fortran mode.")
270
271 (defvar fortran-font-lock-keywords-3 nil
272 "Gaudy level highlighting for Fortran mode.")
273
274 (defvar fortran-font-lock-syntactic-keywords nil
275 "`font-lock-syntactic-keywords' for Fortran.
276 These get fixed-format comments fontified.")
277
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.
363 nil
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)))))
386
387 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
388 "Default expressions to highlight in Fortran mode.")
389 \f
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
395 nil
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+\\)")
409 3)
410 ;; Un-named block data
411 (list nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
412 "Imenu generic expression for `imenu-default-create-index-function'.")
413
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)
437
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.")
487 \f
488 (defvar fortran-mode-abbrev-table
489 (let (abbrevs-changed)
490 (define-abbrev-table 'fortran-mode-abbrev-table nil)
491 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
492 ;; Only use `apply' to quieten the byte-compiler.
493 (mapcar
494 (function (lambda (element)
495 (condition-case nil
496 (apply 'define-abbrev fortran-mode-abbrev-table
497 (append element '(nil 0 t)))
498 (wrong-number-of-arguments
499 (apply 'define-abbrev fortran-mode-abbrev-table
500 (append element '(nil 0)))))))
501 '((";au" "automatic" )
502 (";b" "byte" )
503 (";bd" "block data" )
504 (";ch" "character" )
505 (";cl" "close" )
506 (";c" "continue" )
507 (";cm" "common" )
508 (";cx" "complex" )
509 (";df" "define" )
510 (";di" "dimension" )
511 (";do" "double" )
512 (";dc" "double complex" )
513 (";dp" "double precision" )
514 (";dw" "do while" )
515 (";e" "else" )
516 (";ed" "enddo" )
517 (";el" "elseif" )
518 (";en" "endif" )
519 (";eq" "equivalence" )
520 (";ew" "endwhere" )
521 (";ex" "external" )
522 (";ey" "entry" )
523 (";f" "format" )
524 (";fa" ".false." )
525 (";fu" "function" )
526 (";g" "goto" )
527 (";im" "implicit" )
528 (";ib" "implicit byte" )
529 (";ic" "implicit complex" )
530 (";ich" "implicit character")
531 (";ii" "implicit integer" )
532 (";il" "implicit logical" )
533 (";ir" "implicit real" )
534 (";inc" "include" )
535 (";in" "integer" )
536 (";intr" "intrinsic" )
537 (";l" "logical" )
538 (";n" "namelist" )
539 (";o" "open" ) ; was ;op
540 (";pa" "parameter" )
541 (";pr" "program" )
542 (";ps" "pause" )
543 (";p" "print" )
544 (";rc" "record" )
545 (";re" "real" )
546 (";r" "read" )
547 (";rt" "return" )
548 (";rw" "rewind" )
549 (";s" "stop" )
550 (";sa" "save" )
551 (";st" "structure" )
552 (";sc" "static" )
553 (";su" "subroutine" )
554 (";tr" ".true." )
555 (";ty" "type" )
556 (";vo" "volatile" )
557 (";w" "write" )
558 (";wh" "where" )))
559 fortran-mode-abbrev-table))
560 \f
561 (eval-when-compile ; silence compiler
562 (defvar imenu-case-fold-search)
563 (defvar imenu-syntax-alist))
564
565 (defcustom fortran-mode-hook nil
566 "Hook run by Fortran mode."
567 :type 'hook
568 :group 'fortran)
569
570 ;;;###autoload
571 (defun fortran-mode ()
572 "Major mode for editing Fortran code.
573 \\[fortran-indent-line] indents the current Fortran line correctly.
574 DO statements must not share a common CONTINUE.
575
576 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for
577 Fortran keywords.
578
579 Key definitions:
580 \\{fortran-mode-map}
581
582 Variables controlling indentation style and extra features:
583
584 `comment-start'
585 If you want to use comments starting with `!',
586 set this to the string \"!\".
587 `fortran-do-indent'
588 Extra indentation within do blocks. (default 3)
589 `fortran-if-indent'
590 Extra indentation within if blocks. (default 3)
591 `fortran-structure-indent'
592 Extra indentation within structure, union, map and interface blocks.
593 (default 3)
594 `fortran-continuation-indent'
595 Extra indentation applied to continuation statements. (default 5)
596 `fortran-comment-line-extra-indent'
597 Amount of extra indentation for text within full-line comments. (default 0)
598 `fortran-comment-indent-style'
599 nil means don't change indentation of text in full-line comments,
600 fixed means indent that text at `fortran-comment-line-extra-indent' beyond
601 the value of `fortran-minimum-statement-indent-fixed' (for fixed
602 format continuation style) or `fortran-minimum-statement-indent-tab'
603 (for TAB format continuation style).
604 relative means indent at `fortran-comment-line-extra-indent' beyond the
605 indentation for a line of code.
606 (default 'fixed)
607 `fortran-comment-indent-char'
608 Single-character string to be inserted instead of space for
609 full-line comment indentation. (default \" \")
610 `fortran-minimum-statement-indent-fixed'
611 Minimum indentation for Fortran statements in fixed format mode. (def.6)
612 `fortran-minimum-statement-indent-tab'
613 Minimum indentation for Fortran statements in TAB format mode. (default 9)
614 `fortran-line-number-indent'
615 Maximum indentation for line numbers. A line number will get
616 less than this much indentation if necessary to avoid reaching
617 column 5. (default 1)
618 `fortran-check-all-num-for-matching-do'
619 Non-nil causes all numbered lines to be treated as possible \"continue\"
620 statements. (default nil)
621 `fortran-blink-matching-if'
622 Non-nil causes \\[fortran-indent-line] on an ENDIF statement to blink on
623 matching IF. Also, from an ENDDO statement, blink on matching DO [WHILE]
624 statement. (default nil)
625 `fortran-continuation-string'
626 Single-character string to be inserted in column 5 of a continuation
627 line. (default \"$\")
628 `fortran-comment-region'
629 String inserted by \\[fortran-comment-region] at start of each line in
630 region. (default \"c$$$\")
631 `fortran-electric-line-number'
632 Non-nil causes line number digits to be moved to the correct column
633 as typed. (default t)
634 `fortran-break-before-delimiters'
635 Non-nil causes lines to be broken before delimiters.
636 (default t)
637
638 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
639 with no args, if that value is non-nil."
640 (interactive)
641 (kill-all-local-variables)
642 (setq local-abbrev-table fortran-mode-abbrev-table)
643 (set-syntax-table fortran-mode-syntax-table)
644 ;; Font Lock mode support.
645 (make-local-variable 'font-lock-defaults)
646 (setq font-lock-defaults '((fortran-font-lock-keywords
647 fortran-font-lock-keywords-1
648 fortran-font-lock-keywords-2
649 fortran-font-lock-keywords-3)
650 nil t ((?/ . "$/") ("_$" . "w"))
651 fortran-beginning-of-subprogram))
652 (set (make-local-variable 'font-lock-syntactic-keywords)
653 fortran-font-lock-syntactic-keywords)
654 (make-local-variable 'fortran-break-before-delimiters)
655 (setq fortran-break-before-delimiters t)
656 (make-local-variable 'indent-line-function)
657 (setq indent-line-function 'fortran-indent-line)
658 (make-local-variable 'comment-indent-function)
659 (setq comment-indent-function 'fortran-comment-indent)
660 (set (make-local-variable 'comment-start-skip)
661 ;; We can't reuse `fortran-comment-line-start-skip' directly because
662 ;; it contains backrefs whereas we need submatch-1 to end at the
663 ;; beginning of the comment delimiter.
664 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
665 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
666 (set (make-local-variable 'comment-padding) "$$$")
667 (make-local-variable 'comment-start)
668 (setq comment-start fortran-comment-line-start)
669 (make-local-variable 'require-final-newline)
670 (setq require-final-newline t)
671 (make-local-variable 'abbrev-all-caps)
672 (setq abbrev-all-caps t)
673 (make-local-variable 'indent-tabs-mode)
674 (setq indent-tabs-mode nil)
675 ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
676 (set (make-local-variable 'fill-column) 72)
677 (use-local-map fortran-mode-map)
678 (setq mode-name "Fortran")
679 (setq major-mode 'fortran-mode)
680 (make-local-variable 'fortran-comment-line-extra-indent)
681 (make-local-variable 'fortran-minimum-statement-indent-fixed)
682 (make-local-variable 'fortran-minimum-statement-indent-tab)
683 (make-local-variable 'fortran-column-ruler-fixed)
684 (make-local-variable 'fortran-column-ruler-tab)
685 (setq fortran-tab-mode-string " TAB-format")
686 (setq indent-tabs-mode (fortran-analyze-file-format))
687 (setq imenu-case-fold-search t)
688 (setq imenu-generic-expression fortran-imenu-generic-expression)
689 (setq imenu-syntax-alist '(("_$" . "w")))
690 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
691 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
692 (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
693 (set (make-local-variable 'indent-region-function)
694 (lambda (start end)
695 (let (fortran-blink-matching-if ; avoid blinking delay
696 indent-region-function)
697 (indent-region start end nil))))
698 (set (make-local-variable 'beginning-of-defun-function)
699 #'fortran-beginning-of-subprogram)
700 (set (make-local-variable 'end-of-defun-function)
701 #'fortran-end-of-subprogram)
702 (set (make-local-variable 'add-log-current-defun-function)
703 #'fortran-current-defun)
704 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
705 (run-hooks 'fortran-mode-hook))
706 \f
707 (defsubst fortran-comment-indent ()
708 (save-excursion
709 (if (looking-at fortran-comment-line-start-skip) 0
710 (skip-chars-backward " \t")
711 (max (+ 1 (current-column))
712 comment-column))))
713
714 (defun fortran-indent-comment ()
715 "Align or create comment on current line.
716 Existing comments of all types are recognized and aligned.
717 If the line has no comment, a side-by-side comment is inserted and aligned
718 if the value of `comment-start' is not nil and allows such comments.
719 Otherwise, a separate-line comment is inserted, on this line
720 or on a new line inserted before this line if this line is not blank."
721 (interactive)
722 (beginning-of-line)
723 ;; Recognize existing comments of either kind.
724 (cond ((fortran-find-comment-start-skip 'all)
725 (goto-char (match-beginning 0))
726 (if (bolp)
727 (fortran-indent-line)
728 (if (not (= (current-column)
729 (fortran-comment-indent)))
730 (progn (delete-horizontal-space)
731 (indent-to (fortran-comment-indent))))))
732 ;; No existing comment.
733 ;; If side-by-side comments are defined, insert one,
734 ;; unless line is now blank.
735 ((and comment-start (not (looking-at "[ \t]*$"))
736 (string-match comment-start-skip (concat " " comment-start)))
737 (end-of-line)
738 (delete-horizontal-space)
739 (indent-to (fortran-comment-indent))
740 (insert comment-start))
741 ;; Else insert separate-line comment, making a new line if nec.
742 (t
743 (if (looking-at "^[ \t]*$")
744 (delete-horizontal-space)
745 (beginning-of-line)
746 (insert ?\n)
747 (forward-char -1))
748 (insert fortran-comment-line-start)
749 (insert-char (if (stringp fortran-comment-indent-char)
750 (aref fortran-comment-indent-char 0)
751 fortran-comment-indent-char)
752 (- (fortran-calculate-indent) (current-column))))))
753
754 (defun fortran-comment-region (beg-region end-region arg)
755 "Comments every line in the region.
756 Puts `fortran-comment-region' at the beginning of every line in the region.
757 BEG-REGION and END-REGION are args which specify the region boundaries.
758 With non-nil ARG, uncomments the region."
759 (interactive "*r\nP")
760 (let ((end-region-mark (copy-marker end-region))
761 (save-point (point-marker)))
762 (goto-char beg-region)
763 (beginning-of-line)
764 (if (not arg) ;comment the region
765 (progn (insert fortran-comment-region)
766 (while (and (= (forward-line 1) 0)
767 (< (point) end-region-mark))
768 (insert fortran-comment-region)))
769 (let ((com (regexp-quote fortran-comment-region))) ;uncomment the region
770 (if (looking-at com)
771 (delete-region (point) (match-end 0)))
772 (while (and (= (forward-line 1) 0)
773 (< (point) end-region-mark))
774 (if (looking-at com)
775 (delete-region (point) (match-end 0))))))
776 (goto-char save-point)
777 (set-marker end-region-mark nil)
778 (set-marker save-point nil)))
779 \f
780 (defun fortran-abbrev-start ()
781 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
782 Any other key combination is executed normally."
783 (interactive)
784 (let (c)
785 (insert last-command-char)
786 (if (or (eq (setq c (read-event)) ??) ;insert char if not equal to `?'
787 (eq c help-char))
788 (fortran-abbrev-help)
789 (setq unread-command-events (list c)))))
790
791 (defun fortran-abbrev-help ()
792 "List the currently defined abbrevs in Fortran mode."
793 (interactive)
794 (message "Listing abbrev table...")
795 (display-buffer (fortran-prepare-abbrev-list-buffer))
796 (message "Listing abbrev table...done"))
797
798 (defun fortran-prepare-abbrev-list-buffer ()
799 (save-excursion
800 (set-buffer (get-buffer-create "*Abbrevs*"))
801 (erase-buffer)
802 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
803 (goto-char (point-min))
804 (set-buffer-modified-p nil)
805 (edit-abbrevs-mode))
806 (get-buffer-create "*Abbrevs*"))
807
808 (defun fortran-column-ruler ()
809 "Insert a column ruler momentarily above current line, till next keystroke.
810 The ruler is defined by the value of `fortran-column-ruler-fixed' when in fixed
811 format mode, and `fortran-column-ruler-tab' when in TAB format mode.
812 The key typed is executed unless it is SPC."
813 (interactive)
814 (momentary-string-display
815 (if indent-tabs-mode
816 fortran-column-ruler-tab
817 fortran-column-ruler-fixed)
818 (save-excursion
819 (beginning-of-line)
820 (if (eq (window-start (selected-window))
821 (window-point (selected-window)))
822 (line-beginning-position 2)
823 (point)))
824 nil "Type SPC or any command to erase ruler."))
825
826 (defun fortran-window-create ()
827 "Make the window 72 columns wide.
828 See also `fortran-window-create-momentarily'."
829 (interactive)
830 (let ((window-min-width 2))
831 (if (< (window-width) (frame-width))
832 (enlarge-window-horizontally (- (frame-width)
833 (window-width) 1)))
834 (let* ((window-edges (window-edges))
835 (scroll-bar-width (- (nth 2 window-edges)
836 (car window-edges)
837 (window-width))))
838 (split-window-horizontally (+ 72 scroll-bar-width)))
839 (other-window 1)
840 (switch-to-buffer " fortran-window-extra" t)
841 (select-window (previous-window))))
842
843 (defun fortran-window-create-momentarily (&optional arg)
844 "Momentarily make the window 72 columns wide.
845 Optional ARG non-nil and non-unity disables the momentary feature.
846 See also `fortran-window-create'."
847 (interactive "p")
848 (if (or (not arg)
849 (= arg 1))
850 (save-window-excursion
851 (progn
852 (condition-case nil
853 (fortran-window-create)
854 (error (error "No room for Fortran window")))
855 (message "Type SPC to continue editing.")
856 (let ((char (read-event)))
857 (or (equal char (string-to-char " "))
858 (setq unread-command-events (list char))))))
859 (fortran-window-create)))
860
861 (defun fortran-split-line ()
862 "Break line at point and insert continuation marker and alignment."
863 (interactive)
864 (delete-horizontal-space)
865 (if (save-excursion
866 (let ((pos (point)))
867 (beginning-of-line)
868 (and (fortran-find-comment-start-skip 'all)
869 (< (match-beginning 0) pos))))
870 (insert ?\n (match-string 0))
871 (if indent-tabs-mode
872 (insert ?\n ?\t (fortran-numerical-continuation-char))
873 (insert "\n " fortran-continuation-string))) ; Space after \n important
874 (fortran-indent-line)) ; when the cont string is C, c or *.
875
876 (defun fortran-remove-continuation ()
877 (if (looking-at "\\( [^ 0\n]\\|\t[1-9]\\|&\\)")
878 (progn (replace-match "")
879 (delete-indentation)
880 t)))
881
882 (defun fortran-join-line (arg)
883 "Join current line to the previous one and re-indent.
884 With a prefix argument, repeat this operation that many times.
885 If the prefix argument ARG is negative, join the next -ARG lines.
886 Continuation lines are correctly handled."
887 (interactive "*p")
888 (save-excursion
889 (when (> 0 arg)
890 (setq arg (- arg))
891 (forward-line arg))
892 (while (not (zerop arg))
893 (beginning-of-line)
894 (or (fortran-remove-continuation)
895 (delete-indentation))
896 (setq arg (1- arg)))
897 (fortran-indent-line)))
898
899 (defun fortran-numerical-continuation-char ()
900 "Return a digit for tab-digit style of continuation lines.
901 If, previous line is a tab-digit continuation line, returns that digit
902 plus one. Otherwise return 1. Zero not allowed."
903 (save-excursion
904 (forward-line -1)
905 (if (looking-at "\t[1-9]")
906 (+ ?1 (% (- (char-after (+ (point) 1)) ?0) 9))
907 ?1)))
908
909 (put 'fortran-electric-line-number 'delete-selection t)
910 (defun fortran-electric-line-number (arg)
911 "Self insert, but if part of a Fortran line number indent it automatically.
912 Auto-indent does not happen if a numeric ARG is used."
913 (interactive "P")
914 (if (or arg (not fortran-electric-line-number))
915 (if arg
916 (self-insert-command (prefix-numeric-value arg))
917 (self-insert-command 1))
918 (if (or (and (= 5 (current-column))
919 (save-excursion
920 (beginning-of-line)
921 (looking-at " \\{5\\}"))) ;In col 5 with only spaces to left.
922 (and (= (if indent-tabs-mode
923 fortran-minimum-statement-indent-tab
924 fortran-minimum-statement-indent-fixed) (current-column))
925 (eq ?\t (char-after (line-beginning-position))) ;In col 8
926 ; with a single tab to the left.
927 (not (or (eq last-command 'fortran-indent-line)
928 (eq last-command
929 'fortran-indent-new-line))))
930 (save-excursion
931 (re-search-backward "[^ \t0-9]"
932 (line-beginning-position)
933 t)) ;not a line number
934 (looking-at "[0-9]")) ;within a line number
935 (self-insert-command (prefix-numeric-value arg))
936 (skip-chars-backward " \t")
937 (insert last-command-char)
938 (fortran-indent-line))))
939 \f
940 (defvar fortran-end-prog-re1
941 "end\
942 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
943 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?")
944 (defvar fortran-end-prog-re
945 (concat "^[ \t0-9]*" fortran-end-prog-re1)
946 "Regexp possibly marking subprogram end.")
947
948 (defun fortran-check-end-prog-re ()
949 "Check a preliminary match against `fortran-end-prog-re'."
950 ;; Having got a possible match for the subprogram end, we need a
951 ;; match of whitespace, avoiding possible column 73+ stuff.
952 (save-match-data
953 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
954 (buffer-substring (match-end 0)
955 (min (line-end-position)
956 (+ 72 (line-beginning-position)))))))
957
958 ;; Note that you can't just check backwards for `subroutine' &c in
959 ;; case of un-marked main programs not at the start of the file.
960 (defun fortran-beginning-of-subprogram ()
961 "Move point to the beginning of the current Fortran subprogram."
962 (interactive)
963 (save-match-data
964 (let ((case-fold-search t))
965 (beginning-of-line -1)
966 (if (catch 'ok
967 (while (re-search-backward fortran-end-prog-re nil 'move)
968 (if (fortran-check-end-prog-re)
969 (throw 'ok t))))
970 (forward-line)))))
971
972 (defun fortran-end-of-subprogram ()
973 "Move point to the end of the current Fortran subprogram."
974 (interactive)
975 (save-match-data
976 (let ((case-fold-search t))
977 (if (save-excursion ; on END
978 (beginning-of-line)
979 (and (looking-at fortran-end-prog-re)
980 (fortran-check-end-prog-re)))
981 (forward-line)
982 (beginning-of-line 2)
983 (catch 'ok
984 (while (re-search-forward fortran-end-prog-re nil 'move)
985 (if (fortran-check-end-prog-re)
986 (throw 'ok t))))
987 (goto-char (match-beginning 0))
988 (forward-line)))))
989
990 (defun fortran-previous-statement ()
991 "Move point to beginning of the previous Fortran statement.
992 Returns 'first-statement if that statement is the first
993 non-comment Fortran statement in the file, and nil otherwise.
994 Directive lines are treated as comments."
995 (interactive)
996 (let (not-first-statement continue-test)
997 (beginning-of-line)
998 (setq continue-test
999 (and
1000 (not (looking-at fortran-comment-line-start-skip))
1001 (not (looking-at fortran-directive-re))
1002 (or (looking-at
1003 (concat "[ \t]*"
1004 (regexp-quote fortran-continuation-string)))
1005 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1006 (while (and (setq not-first-statement (= (forward-line -1) 0))
1007 (or (looking-at fortran-comment-line-start-skip)
1008 (looking-at fortran-directive-re)
1009 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1010 (looking-at (concat "[ \t]*" comment-start-skip)))))
1011 (cond ((and continue-test
1012 (not not-first-statement))
1013 (message "Incomplete continuation statement."))
1014 (continue-test
1015 (fortran-previous-statement))
1016 ((not not-first-statement)
1017 'first-statement))))
1018
1019 (defun fortran-next-statement ()
1020 "Move point to beginning of the next Fortran statement.
1021 Returns 'last-statement if that statement is the last
1022 non-comment Fortran statement in the file, and nil otherwise.
1023 Directive lines are treated as comments."
1024 (interactive)
1025 (let (not-last-statement)
1026 (beginning-of-line)
1027 (while (and (setq not-last-statement
1028 (and (= (forward-line 1) 0)
1029 (not (eobp))))
1030 (or (looking-at fortran-comment-line-start-skip)
1031 (looking-at fortran-directive-re)
1032 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1033 (looking-at (concat "[ \t]*" comment-start-skip)))))
1034 (if (not not-last-statement)
1035 'last-statement)))
1036 \f
1037 (defun fortran-blink-match (regex keyword find-begin)
1038 "From a line matching REGEX, blink matching KEYWORD statement line.
1039 Use function FIND-BEGIN to match it."
1040 (let ((top-of-window (window-start))
1041 (end-point (point))
1042 (case-fold-search t)
1043 matching
1044 message)
1045 (if (save-excursion
1046 (beginning-of-line)
1047 (skip-chars-forward " \t0-9")
1048 (looking-at regex))
1049 (progn
1050 (if (not (setq matching (funcall find-begin)))
1051 (setq message (concat "No matching " keyword "."))
1052 (if (< matching top-of-window)
1053 (save-excursion
1054 (goto-char matching)
1055 (beginning-of-line)
1056 (setq message
1057 (concat "Matches "
1058 (buffer-substring (point)
1059 (line-end-position)))))))
1060 (if message
1061 (message "%s" message)
1062 (goto-char matching)
1063 (sit-for 1)
1064 (goto-char end-point))))))
1065
1066 (defun fortran-blink-matching-if ()
1067 "From an ENDIF or ELSE statement, blink the matching IF statement."
1068 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1069 "if" #'fortran-beginning-if))
1070
1071 (defun fortran-blink-matching-do ()
1072 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1073 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
1074
1075 (defun fortran-mark-do ()
1076 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1077 The marks are pushed."
1078 (interactive)
1079 (let (enddo-point do-point)
1080 (if (setq enddo-point (fortran-end-do))
1081 (if (not (setq do-point (fortran-beginning-do)))
1082 (message "No matching do.")
1083 (goto-char enddo-point)
1084 (push-mark)
1085 (goto-char do-point)))))
1086
1087 (defun fortran-end-do ()
1088 "Search forward for first unmatched ENDDO.
1089 Return point or nil."
1090 (let ((case-fold-search t))
1091 (if (save-excursion (beginning-of-line)
1092 (skip-chars-forward " \t0-9")
1093 (looking-at "end[ \t]*do\\b"))
1094 ;; Sitting on one.
1095 (match-beginning 0)
1096 ;; Search for one.
1097 (save-excursion
1098 (let ((count 1))
1099 (while (and (not (= count 0))
1100 (not (eq (fortran-next-statement) 'last-statement))
1101 ;; Keep local to subprogram
1102 (not (and (looking-at fortran-end-prog-re)
1103 (fortran-check-end-prog-re))))
1104 (skip-chars-forward " \t0-9")
1105 (cond ((looking-at "end[ \t]*do\\b")
1106 (setq count (1- count)))
1107 ((looking-at
1108 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1109 (setq count (+ count 1)))))
1110 (and (= count 0)
1111 ;; All pairs accounted for.
1112 (point)))))))
1113
1114 (defun fortran-beginning-do ()
1115 "Search backwards for first unmatched DO [WHILE].
1116 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1117 (let ((case-fold-search t)
1118 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1119 (if (save-excursion
1120 (beginning-of-line)
1121 (skip-chars-forward " \t0-9")
1122 (looking-at dostart-re))
1123 ;; Sitting on one.
1124 (match-beginning 0)
1125 ;; Search for one.
1126 (save-excursion
1127 (let ((count 1))
1128 (while (and (not (= count 0))
1129 (not (eq (fortran-previous-statement) 'first-statement))
1130 ;; Keep local to subprogram
1131 (not (and (looking-at fortran-end-prog-re)
1132 (fortran-check-end-prog-re))))
1133 (skip-chars-forward " \t0-9")
1134 (cond ((looking-at dostart-re)
1135 (setq count (1- count)))
1136 ;; Note labelled loop ends not considered.
1137 ((looking-at "end[ \t]*do\\b")
1138 (setq count (1+ count)))))
1139 (and (= count 0)
1140 ;; All pairs accounted for.
1141 (point)))))))
1142
1143 (defun fortran-mark-if ()
1144 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1145 The marks are pushed."
1146 (interactive)
1147 (let (endif-point if-point)
1148 (if (setq endif-point (fortran-end-if))
1149 (if (not (setq if-point (fortran-beginning-if)))
1150 (message "No matching if.")
1151 ;; Set mark, move point.
1152 (goto-char endif-point)
1153 (push-mark)
1154 (goto-char if-point)))))
1155
1156 (defvar fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1157
1158 (defun fortran-end-if ()
1159 "Search forwards for first unmatched ENDIF.
1160 Return point or nil."
1161 (let ((case-fold-search t))
1162 (if (save-excursion (beginning-of-line)
1163 (skip-chars-forward " \t0-9")
1164 (looking-at "end[ \t]*if\\b"))
1165 ;; Sitting on one.
1166 (match-beginning 0)
1167 ;; Search for one. The point has been already been moved to first
1168 ;; letter on line but this should not cause troubles.
1169 (save-excursion
1170 (let ((count 1))
1171 (while (and (not (= count 0))
1172 (not (eq (fortran-next-statement) 'last-statement))
1173 ;; Keep local to subprogram.
1174 (not (and (looking-at fortran-end-prog-re)
1175 (fortran-check-end-prog-re))))
1176 (skip-chars-forward " \t0-9")
1177 (cond ((looking-at "end[ \t]*if\\b")
1178 (setq count (- count 1)))
1179 ((looking-at fortran-if-start-re)
1180 (save-excursion
1181 (if (or
1182 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1183 (let (then-test) ; Multi-line if-then.
1184 (while
1185 (and
1186 (= (forward-line 1) 0)
1187 ;; Search forward for then.
1188 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1189 (not
1190 (setq then-test
1191 (looking-at
1192 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1193 then-test))
1194 (setq count (+ count 1)))))))
1195 (and (= count 0)
1196 ;; All pairs accounted for.
1197 (point)))))))
1198
1199 (defun fortran-beginning-if ()
1200 "Search backwards for first unmatched IF-THEN.
1201 Return point or nil."
1202 (let ((case-fold-search t))
1203 (if (save-excursion
1204 ;; May be sitting on multi-line if-then statement, first
1205 ;; move to beginning of current statement. Note:
1206 ;; `fortran-previous-statement' moves to previous statement
1207 ;; *unless* current statement is first one. Only move
1208 ;; forward if not first-statement.
1209 (if (not (eq (fortran-previous-statement) 'first-statement))
1210 (fortran-next-statement))
1211 (skip-chars-forward " \t0-9")
1212 (and
1213 (looking-at fortran-if-start-re)
1214 (save-match-data
1215 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1216 ;; Multi-line if-then.
1217 (let (then-test)
1218 (while
1219 (and (= (forward-line 1) 0)
1220 ;; Search forward for then.
1221 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1222 (not
1223 (setq then-test
1224 (looking-at
1225 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1226 then-test)))))
1227 ;; Sitting on one.
1228 (match-beginning 0)
1229 ;; Search for one.
1230 (save-excursion
1231 (let ((count 1))
1232 (while (and (not (= count 0))
1233 (not (eq (fortran-previous-statement) 'first-statement))
1234 ;; Keep local to subprogram.
1235 (not (and (looking-at fortran-end-prog-re)
1236 (fortran-check-end-prog-re))))
1237 (skip-chars-forward " \t0-9")
1238 (cond ((looking-at fortran-if-start-re)
1239 (save-excursion
1240 (if (or
1241 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1242 (let (then-test) ; Multi-line if-then.
1243 (while
1244 (and
1245 (= (forward-line 1) 0)
1246 ;; Search forward for then.
1247 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1248 (not
1249 (setq then-test
1250 (looking-at
1251 (concat ".*then\\b[ \t]*"
1252 "[^ \t(=a-z0-9]"))))))
1253 then-test))
1254 (setq count (- count 1)))))
1255 ((looking-at "end[ \t]*if\\b")
1256 (setq count (+ count 1)))))
1257 (and (= count 0)
1258 ;; All pairs accounted for.
1259 (point)))))))
1260 \f
1261 (defun fortran-indent-line ()
1262 "Indent current Fortran line based on its contents and on previous lines."
1263 (interactive)
1264 (let ((cfi (fortran-calculate-indent)))
1265 (save-excursion
1266 (beginning-of-line)
1267 (if (or (not (= cfi (fortran-current-line-indentation)))
1268 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1269 (not (fortran-line-number-indented-correctly-p))))
1270 (fortran-indent-to-column cfi)
1271 (beginning-of-line)
1272 (if (fortran-find-comment-start-skip)
1273 (fortran-indent-comment))))
1274 ;; Never leave point in left margin.
1275 (if (< (current-column) cfi)
1276 (move-to-column cfi))
1277 (if (and auto-fill-function
1278 (> (save-excursion (end-of-line) (current-column))
1279 fill-column))
1280 (save-excursion
1281 (end-of-line)
1282 (fortran-fill)))
1283 (if fortran-blink-matching-if
1284 (progn
1285 (fortran-blink-matching-if)
1286 (fortran-blink-matching-do)))))
1287
1288 (defun fortran-auto-fill ()
1289 (if (> (current-column) (current-fill-column))
1290 (let ((cfi (fortran-calculate-indent)))
1291 (save-excursion
1292 (beginning-of-line)
1293 (if (or (not (= cfi (fortran-current-line-indentation)))
1294 (and (re-search-forward "^[ \t]*[0-9]+"
1295 (+ (point) 4) t)
1296 (not (fortran-line-number-indented-correctly-p))))
1297 (fortran-indent-to-column cfi)
1298 (beginning-of-line)
1299 (if (fortran-find-comment-start-skip)
1300 (fortran-indent-comment))))
1301 (fortran-fill)
1302 ;; Never leave point in left margin.
1303 (if (< (current-column) cfi)
1304 (move-to-column cfi)))))
1305
1306 ;; Historically this was a separate function which advertised itself
1307 ;; as reindenting but only did so where `most likely to be necessary'.
1308 (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
1309
1310 (defun fortran-indent-subprogram ()
1311 "Properly indent the Fortran subprogram which contains point."
1312 (interactive)
1313 (save-excursion
1314 (mark-defun)
1315 (message "Indenting subprogram...")
1316 (indent-region (point) (mark) nil))
1317 (message "Indenting subprogram...done."))
1318
1319 (defun fortran-calculate-indent ()
1320 "Calculates the Fortran indent column based on previous lines."
1321 (let (icol first-statement (case-fold-search t)
1322 (fortran-minimum-statement-indent
1323 (if indent-tabs-mode
1324 fortran-minimum-statement-indent-tab
1325 fortran-minimum-statement-indent-fixed)))
1326 (save-excursion
1327 (setq first-statement (fortran-previous-statement))
1328 (if first-statement
1329 (setq icol fortran-minimum-statement-indent)
1330 (progn
1331 (if (= (point) (point-min))
1332 (setq icol fortran-minimum-statement-indent)
1333 (setq icol (fortran-current-line-indentation)))
1334 (skip-chars-forward " \t0-9")
1335 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1336 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1337 (let (then-test) ;multi-line if-then
1338 (while (and (= (forward-line 1) 0)
1339 ;;search forward for then
1340 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1341 (not (setq then-test
1342 (looking-at
1343 ".*then\\b[ \t]\
1344 *[^ \t_$(=a-z0-9]")))))
1345 then-test))
1346 (setq icol (+ icol fortran-if-indent))))
1347 ((looking-at "else\\(if\\)?\\b")
1348 (setq icol (+ icol fortran-if-indent)))
1349 ((looking-at "select[ \t]*case[ \t](.*)")
1350 (setq icol (+ icol fortran-if-indent)))
1351 ((looking-at "case[ \t]*(.*)")
1352 (setq icol (+ icol fortran-if-indent)))
1353 ((looking-at "case[ \t]*default\\b")
1354 (setq icol (+ icol fortran-if-indent)))
1355 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1356 (setq icol (+ icol fortran-if-indent)))
1357 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1358 (setq icol (+ icol fortran-if-indent)))
1359 ((looking-at "do\\b")
1360 (setq icol (+ icol fortran-do-indent)))
1361 ((looking-at
1362 "\\(structure\\|union\\|map\\|interface\\)\
1363 \\b[ \t]*[^ \t=(a-z]")
1364 (setq icol (+ icol fortran-structure-indent)))
1365 ((and (looking-at fortran-end-prog-re1)
1366 (fortran-check-end-prog-re))
1367 ;; Previous END resets indent to minimum
1368 (setq icol fortran-minimum-statement-indent))))))
1369 (save-excursion
1370 (beginning-of-line)
1371 (cond ((looking-at "[ \t]*$"))
1372 ;; Check for directive before comment, so as not to indent.
1373 ((looking-at fortran-directive-re)
1374 (setq fortran-minimum-statement-indent 0 icol 0))
1375 ((looking-at fortran-comment-line-start-skip)
1376 (cond ((eq fortran-comment-indent-style 'relative)
1377 (setq icol (+ icol fortran-comment-line-extra-indent)))
1378 ((eq fortran-comment-indent-style 'fixed)
1379 (setq icol (+ fortran-minimum-statement-indent
1380 fortran-comment-line-extra-indent))))
1381 (setq fortran-minimum-statement-indent 0))
1382 ((or (looking-at (concat "[ \t]*"
1383 (regexp-quote
1384 fortran-continuation-string)))
1385 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1386 (setq icol (+ icol fortran-continuation-indent)))
1387 (first-statement)
1388 ((and fortran-check-all-num-for-matching-do
1389 (looking-at "[ \t]*[0-9]+")
1390 (fortran-check-for-matching-do))
1391 (setq icol (- icol fortran-do-indent)))
1392 (t
1393 (skip-chars-forward " \t0-9")
1394 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1395 (setq icol (- icol fortran-if-indent)))
1396 ((looking-at "else\\(if\\)?\\b")
1397 (setq icol (- icol fortran-if-indent)))
1398 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1399 (setq icol (- icol fortran-if-indent)))
1400 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1401 (setq icol (- icol fortran-if-indent)))
1402 ((and (looking-at "continue\\b")
1403 (fortran-check-for-matching-do))
1404 (setq icol (- icol fortran-do-indent)))
1405 ((looking-at "end[ \t]*do\\b")
1406 (setq icol (- icol fortran-do-indent)))
1407 ((looking-at "end[ \t]*\
1408 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1409 (setq icol (- icol fortran-structure-indent)))
1410 ((and (looking-at fortran-end-prog-re1)
1411 (fortran-check-end-prog-re)
1412 (not (= icol fortran-minimum-statement-indent)))
1413 (message "Warning: `end' not in column %d. Probably\
1414 an unclosed block." fortran-minimum-statement-indent))))))
1415 (max fortran-minimum-statement-indent icol)))
1416 \f
1417 (defun fortran-current-line-indentation ()
1418 "Indentation of current line, ignoring Fortran line number or continuation.
1419 This is the column position of the first non-whitespace character
1420 aside from the line number and/or column 5/8 line-continuation character.
1421 For comment lines, returns indentation of the first
1422 non-indentation text within the comment."
1423 (save-excursion
1424 (beginning-of-line)
1425 (cond ((looking-at fortran-comment-line-start-skip)
1426 (goto-char (match-end 0))
1427 (skip-chars-forward
1428 (if (stringp fortran-comment-indent-char)
1429 fortran-comment-indent-char
1430 (char-to-string fortran-comment-indent-char))))
1431 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1432 (goto-char (match-end 0)))
1433 (t
1434 ;; Move past line number.
1435 (skip-chars-forward "[ \t0-9]")))
1436 ;; Move past whitespace.
1437 (skip-chars-forward " \t")
1438 (current-column)))
1439
1440 (defun fortran-indent-to-column (col)
1441 "Indent current line with spaces to column COL.
1442 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1443 line, and this continuation character is retained on indentation;
1444 2) If `fortran-continuation-string' is the first non-whitespace
1445 character, this is a continuation line;
1446 3) A non-continuation line which has a number as the first
1447 non-whitespace character is a numbered line.
1448 4) A TAB followed by a digit indicates a continuation line."
1449 (save-excursion
1450 (beginning-of-line)
1451 (if (looking-at fortran-comment-line-start-skip)
1452 (if fortran-comment-indent-style
1453 (let* ((char (if (stringp fortran-comment-indent-char)
1454 (aref fortran-comment-indent-char 0)
1455 fortran-comment-indent-char))
1456 (chars (string ? ?\t char)))
1457 (goto-char (match-end 0))
1458 (skip-chars-backward chars)
1459 (delete-region (point) (progn (skip-chars-forward chars)
1460 (point)))
1461 (insert-char char (- col (current-column)))))
1462 (if (looking-at "\t[1-9]")
1463 (if indent-tabs-mode
1464 (goto-char (match-end 0))
1465 (delete-char 2)
1466 (insert-char ? 5)
1467 (insert fortran-continuation-string))
1468 (if (looking-at " \\{5\\}[^ 0\n]")
1469 (if indent-tabs-mode
1470 (progn (delete-char 6)
1471 (insert ?\t (fortran-numerical-continuation-char) 1))
1472 (forward-char 6))
1473 (delete-horizontal-space)
1474 ;; Put line number in columns 0-4
1475 ;; or put continuation character in column 5.
1476 (cond ((eobp))
1477 ((looking-at (regexp-quote fortran-continuation-string))
1478 (if indent-tabs-mode
1479 (progn
1480 (indent-to
1481 (if indent-tabs-mode
1482 fortran-minimum-statement-indent-tab
1483 fortran-minimum-statement-indent-fixed))
1484 (delete-char 1)
1485 (insert-char (fortran-numerical-continuation-char) 1))
1486 (indent-to 5)
1487 (forward-char 1)))
1488 ((looking-at "[0-9]+")
1489 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1490 (if (< extra-space 0)
1491 (message "Warning: line number exceeds 5-digit limit.")
1492 (indent-to (min fortran-line-number-indent extra-space))))
1493 (skip-chars-forward "0-9")))))
1494 ;; Point is now after any continuation character or line number.
1495 ;; Put body of statement where specified.
1496 (delete-horizontal-space)
1497 (indent-to col)
1498 ;; Indent any comment following code on the same line.
1499 (if (fortran-find-comment-start-skip)
1500 (progn (goto-char (match-beginning 0))
1501 (if (not (= (current-column) (fortran-comment-indent)))
1502 (progn (delete-horizontal-space)
1503 (indent-to (fortran-comment-indent)))))))))
1504
1505 (defun fortran-line-number-indented-correctly-p ()
1506 "Return t if current line's line number is correctly indented.
1507 Do not call if there is no line number."
1508 (save-excursion
1509 (beginning-of-line)
1510 (skip-chars-forward " \t")
1511 (and (<= (current-column) fortran-line-number-indent)
1512 (or (= (current-column) fortran-line-number-indent)
1513 (progn (skip-chars-forward "0-9")
1514 (= (current-column) 5))))))
1515
1516 (defun fortran-check-for-matching-do ()
1517 "When called from a numbered statement, return t if matching DO is found.
1518 Otherwise return nil."
1519 (let ((case-fold-search t)
1520 charnum)
1521 (save-excursion
1522 (beginning-of-line)
1523 (if (looking-at "[ \t]*[0-9]+")
1524 (progn
1525 (skip-chars-forward " \t")
1526 (skip-chars-forward "0") ;skip past leading zeros
1527 (setq charnum
1528 (buffer-substring (point) (progn
1529 (skip-chars-forward "0-9")
1530 (point))))
1531 (beginning-of-line)
1532 (save-restriction
1533 (save-excursion
1534 (narrow-to-defun)
1535 (and (re-search-backward
1536 (concat
1537 "\\(^[ \t0-9]*do[ \t]*0*"
1538 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1539 charnum "\\b\\)")
1540 nil t)
1541 (looking-at
1542 (concat "^[ \t0-9]*do[ \t]*0*"
1543 charnum))))))))))
1544
1545 (defun fortran-find-comment-start-skip (&optional all)
1546 "Move to past `comment-start-skip' found on current line.
1547 Return non-nil if `comment-start-skip' found, nil if not.
1548 If ALL is nil, only match comments that start in column > 0."
1549 (interactive)
1550 ;; Hopefully at some point we can just use the line below! -stef
1551 ;; (comment-search-forward (line-end-position) t))
1552 (when (or all comment-start-skip)
1553 (let ((pos (point))
1554 (css (if comment-start-skip
1555 (concat fortran-comment-line-start-skip
1556 "\\|" comment-start-skip)
1557 fortran-comment-line-start-skip)))
1558 (when (re-search-forward css (line-end-position) t)
1559 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1560 (or (save-match-data
1561 (not (fortran-is-in-string-p (match-beginning 0))))
1562 ;; Recurse for rest of line.
1563 (fortran-find-comment-start-skip all)))
1564 (point)
1565 (goto-char pos)
1566 nil)))))
1567
1568 ;;From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1569 ;; Test if TAB format continuation lines work.
1570 (defun fortran-is-in-string-p (where)
1571 "Return non-nil iff WHERE (a buffer position) is inside a Fortran string."
1572 (save-excursion
1573 (goto-char where)
1574 (cond
1575 ((bolp) nil) ; bol is never inside a string
1576 ((save-excursion ; comment lines too
1577 (beginning-of-line)
1578 (looking-at fortran-comment-line-start-skip)) nil)
1579 (t (let (;; ok, serious now. Init some local vars:
1580 (parse-state '(0 nil nil nil nil nil 0))
1581 (quoted-comment-start (if comment-start
1582 (regexp-quote comment-start)))
1583 (not-done t)
1584 parse-limit end-of-line)
1585 ;; move to start of current statement
1586 (fortran-next-statement)
1587 (fortran-previous-statement)
1588 ;; now parse up to WHERE
1589 (while not-done
1590 (if (or ;; skip to next line if:
1591 ;; - comment line?
1592 (looking-at fortran-comment-line-start-skip)
1593 ;; - at end of line?
1594 (eolp)
1595 ;; - not in a string and after comment-start?
1596 (and (not (nth 3 parse-state))
1597 comment-start
1598 (equal comment-start
1599 (char-to-string (preceding-char)))))
1600 (if (> (forward-line) 0)
1601 (setq not-done nil))
1602 ;; else:
1603 ;; if we are at beginning of code line, skip any
1604 ;; whitespace, labels and tab continuation markers.
1605 (if (bolp) (skip-chars-forward " \t0-9"))
1606 ;; if we are in column <= 5 now, check for continuation char
1607 (cond ((= 5 (current-column)) (forward-char 1))
1608 ((and (< (current-column) 5)
1609 (equal fortran-continuation-string
1610 (char-to-string (following-char)))
1611 (forward-char 1))))
1612 ;; find out parse-limit from here
1613 (setq end-of-line (line-end-position))
1614 (setq parse-limit (min where end-of-line))
1615 ;; parse max up to comment-start, if non-nil and in current line
1616 (if comment-start
1617 (save-excursion
1618 (if (re-search-forward quoted-comment-start end-of-line t)
1619 (setq parse-limit (min (point) parse-limit)))))
1620 ;; now parse if still in limits
1621 (if (< (point) where)
1622 (setq parse-state (parse-partial-sexp
1623 (point) parse-limit nil nil parse-state))
1624 (setq not-done nil))))
1625 ;; result is
1626 (nth 3 parse-state))))))
1627
1628 ;; From old version.
1629 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1630
1631 (defun fortran-fill ()
1632 (let* ((auto-fill-function #'fortran-auto-fill)
1633 (opoint (point))
1634 (bol (line-beginning-position))
1635 (eol (line-end-position))
1636 (bos (min eol (+ bol (fortran-current-line-indentation))))
1637 (quote
1638 (save-excursion
1639 (goto-char bol)
1640 (if (looking-at fortran-comment-line-start-skip)
1641 nil ; OK to break quotes on comment lines.
1642 (move-to-column fill-column)
1643 (if (fortran-is-in-string-p (point))
1644 (save-excursion (re-search-backward "\\S\"\\s\"\\S\"" bol t)
1645 (if fortran-break-before-delimiters
1646 (point)
1647 (1+ (point))))))))
1648 ;; decide where to split the line. If a position for a quoted
1649 ;; string was found above then use that, else break the line
1650 ;; before the last delimiter.
1651 ;; Delimiters are whitespace, commas, and operators.
1652 ;; Will break before a pair of *'s.
1653 (fill-point
1654 (or quote
1655 (save-excursion
1656 (move-to-column (1+ fill-column))
1657 (skip-chars-backward "^ \t\n,'+-/*=)"
1658 ;;; (if fortran-break-before-delimiters
1659 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1660 )
1661 (if (<= (point) (1+ bos))
1662 (progn
1663 (move-to-column (1+ fill-column))
1664 ;;what is this doing???
1665 (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t))
1666 (goto-char bol))))
1667 (if (bolp)
1668 (re-search-forward "[ \t]" opoint t)
1669 (backward-char)
1670 (if (looking-at "\\s\"")
1671 (forward-char)
1672 (skip-chars-backward " \t\*")))
1673 (if fortran-break-before-delimiters
1674 (point)
1675 (1+ (point)))))))
1676 ;; if we are in an in-line comment, don't break unless the
1677 ;; line of code is longer than it should be. Otherwise
1678 ;; break the line at the column computed above.
1679 ;;
1680 ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
1681 ;; don't prevent a break.
1682 (when (and (save-excursion
1683 (beginning-of-line)
1684 (when (fortran-find-comment-start-skip)
1685 (goto-char (match-beginning 0))
1686 (>= (point) fill-point)))
1687 (save-excursion
1688 (goto-char fill-point)
1689 (not (bolp)))
1690 (> (save-excursion
1691 (goto-char opoint)
1692 (current-column))
1693 (min (1+ fill-column)
1694 (+ (fortran-calculate-indent)
1695 fortran-continuation-indent))))
1696 (goto-char fill-point)
1697 (fortran-break-line)
1698 (end-of-line))))
1699
1700 (defun fortran-break-line ()
1701 (let ((opoint (point))
1702 (bol (line-beginning-position))
1703 (comment-string
1704 (save-excursion
1705 (if (fortran-find-comment-start-skip)
1706 (delete-and-extract-region
1707 (match-beginning 0) (line-end-position))))))
1708 ;; Forward line 1 really needs to go to next non white line
1709 (if (save-excursion (forward-line)
1710 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1711 (progn
1712 (end-of-line)
1713 (delete-region (point) (match-end 0))
1714 (delete-horizontal-space)
1715 (fortran-fill))
1716 (fortran-split-line))
1717 (if comment-string
1718 (save-excursion
1719 (goto-char bol)
1720 (end-of-line)
1721 (delete-horizontal-space)
1722 (indent-to (fortran-comment-indent))
1723 (insert comment-string)))))
1724
1725 (defun fortran-analyze-file-format ()
1726 "Return nil if fixed format is used, t if TAB formatting is used.
1727 Use `fortran-tab-mode-default' if no non-comment statements are found in the
1728 file before the end or the first `fortran-analyze-depth' lines."
1729 (let ((i 0))
1730 (save-excursion
1731 (goto-char (point-min))
1732 (setq i 0)
1733 (while (not (or
1734 (eobp)
1735 (eq (char-after) ?\t)
1736 (looking-at " \\{6\\}")
1737 (> i fortran-analyze-depth)))
1738 (forward-line)
1739 (setq i (1+ i)))
1740 (cond
1741 ((eq (char-after) ?\t) t)
1742 ((looking-at " \\{6\\}") nil)
1743 (fortran-tab-mode-default t)
1744 (t nil)))))
1745
1746 (or (assq 'fortran-tab-mode-string minor-mode-alist)
1747 (setq minor-mode-alist (cons
1748 '(fortran-tab-mode-string
1749 (indent-tabs-mode fortran-tab-mode-string))
1750 minor-mode-alist)))
1751
1752 (defun fortran-fill-paragraph (&optional justify)
1753 "Fill surrounding comment block as paragraphs, else fill statement.
1754 Intended as the value of `fill-paragraph-function'."
1755 (interactive "P")
1756 (save-excursion
1757 (beginning-of-line)
1758 (if (not (looking-at fortran-comment-line-start-skip))
1759 (fortran-fill-statement)
1760 ;; We're in a comment block. Find the start and end of a
1761 ;; paragraph, delimited either by non-comment lines or empty
1762 ;; comments. (Get positions as markers, since the
1763 ;; `indent-region' below can shift the block's end).
1764 (let* ((non-empty-comment
1765 (concat fortran-comment-line-start-skip "[^ \t\n]"))
1766 (start (save-excursion
1767 ;; Find (start of) first line.
1768 (while (and (zerop (forward-line -1))
1769 (looking-at non-empty-comment)))
1770 (or (looking-at non-empty-comment)
1771 (forward-line)) ; overshot
1772 (point-marker)))
1773 (end (save-excursion
1774 ;; Find start of first line past region to fill.
1775 (while (progn
1776 (forward-line)
1777 (looking-at non-empty-comment)))
1778 (point-marker))))
1779 ;; Indent the block, find the string comprising the effective
1780 ;; comment start skip and use that as a fill-prefix for
1781 ;; filling the region.
1782 (indent-region start end nil)
1783 (let ((paragraph-ignore-fill-prefix nil)
1784 (fill-prefix (progn
1785 (beginning-of-line)
1786 (looking-at fortran-comment-line-start-skip)
1787 (match-string 0))))
1788 (let (fill-paragraph-function)
1789 (fill-region start end justify))) ; with normal `fill-paragraph'
1790 (set-marker start nil)
1791 (set-marker end nil))))
1792 t)
1793
1794 (defun fortran-fill-statement ()
1795 "Fill a fortran statement up to `fill-column'."
1796 (interactive)
1797 (let ((auto-fill-function #'fortran-auto-fill))
1798 (if (not (save-excursion
1799 (beginning-of-line)
1800 (or (looking-at "[ \t]*$")
1801 (looking-at fortran-comment-line-start-skip)
1802 (and comment-start-skip
1803 (looking-at (concat "[ \t]*" comment-start-skip))))))
1804 (save-excursion
1805 ;; Find beginning of statement.
1806 (fortran-next-statement)
1807 (fortran-previous-statement)
1808 ;; Re-indent initially.
1809 (fortran-indent-line)
1810 ;; Replace newline plus continuation field plus indentation with
1811 ;; single space.
1812 (while (progn
1813 (forward-line)
1814 (fortran-remove-continuation)))
1815 (fortran-previous-statement)))
1816 (fortran-indent-line)))
1817
1818 (defun fortran-strip-sequence-nos (&optional do-space)
1819 "Delete all text in column 72 and up (assumed to be sequence numbers).
1820 Normally also deletes trailing whitespace after stripping such text.
1821 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
1822 (interactive "p")
1823 (save-excursion
1824 (goto-char (point-min))
1825 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t)
1826 (replace-match "" nil nil nil 1)
1827 (unless do-space (delete-horizontal-space)))))
1828
1829 ;; This code used to live in add-log.el, but this is a better place
1830 ;; for it.
1831 (defun fortran-current-defun ()
1832 "Function to use for `add-log-current-defun-function' in Fortran mode."
1833 (save-excursion
1834 ;; We must be inside function body for this to work.
1835 (fortran-beginning-of-subprogram)
1836 (let ((case-fold-search t)) ; case-insensitive
1837 ;; search for fortran subprogram start
1838 (if (re-search-forward
1839 (concat "^[ \t]*\\(program\\|subroutine\\|function"
1840 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
1841 "\\(block[ \t]*data\\)\\)")
1842 (save-excursion (fortran-end-of-subprogram)
1843 (point))
1844 t)
1845 (or (match-string-no-properties 2)
1846 (progn
1847 ;; move to EOL or before first left paren
1848 (if (re-search-forward "[(\n]" nil t)
1849 (progn (backward-char)
1850 (skip-chars-backward " \t"))
1851 (end-of-line))
1852 ;; Use the name preceding that.
1853 (buffer-substring-no-properties (point) (progn (backward-sexp)
1854 (point)))))
1855 "main"))))
1856
1857 (provide 'fortran)
1858
1859 ;;; fortran.el ends here