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