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