1 ;;; fortran.el --- Fortran mode for GNU Emacs (version 1.28.3, July 15, 1991)
3 ;;; Copyright (c) 1991 Free Software Foundation, Inc.
4 ;;; Written by Michael D. Prange (prange@erl.mit.edu)
5 ;;; Maintained (as of version 1.28) by Stephen A. Wood (saw@hallc1.cebaf.gov)
6 ;;; This version is an update of version 1.21 (Oct 1, 1985).
7 ;;; Updated by Stephen A. Wood (saw@hallc1.cebaf.gov) to use tab format
8 ;;; continuation control and indentation. (Digit after TAB to signify
9 ;;; continuation line. This version also incorporates suggestions from
10 ;;; Richard Stallman, and the ideas in two previous unpublished versions of
11 ;;; fortran .el (version 1.21.1 (from gildea@expo.lcs.mit.edu) and 1.27
12 ;;; (prange@erl.mit.edu).)
14 ;;; Notes to fortran-mode version 1.28
15 ;;; 1. Fortran mode can support either fixed format or tab format. Fixed
16 ;;; format is where statements start in column 6 (first column is 0)
17 ;;; and continuation lines are denoted by a character in column 5.
18 ;;; In tab mode, statements follow a tab character. Continuation lines
19 ;;; are where the first character on a line is a tab and the second is
20 ;;; a digit from 1 to 9.
21 ;;; 2. When fortran mode is called, the buffer is analyzed to determine what
22 ;;; kind of formating is used. Starting at the top of the file, lines
23 ;;; are scanned until a line is found that begins with a tab or 6 spaces.
24 ;;; The mode for that buffer is then set to either tab or fixed format
25 ;;; based on that line. If no line starting with a tab or 6 spaces is
26 ;;; found before the end of the buffer or in the first 100 lines, the
27 ;;; mode is set from the variable `fortran-tab-mode-default'. t is tab
28 ;;; mode, nil is fixed format mode. By default, fixed format mode is used.
29 ;;; To use tabbing mode as the default, put the following line in .emacs
30 ;;; (setq fortran-tab-mode-default t)
31 ;;; This line should not be in the hook since the hook is called only
32 ;;; after the file is analyzed.
33 ;;; To force a particular mode independent of the analysis, attach
34 ;;; (fortran-tab-mode t) or (fortran-tab-mode nil)
35 ;;; to fortran-mode-hook.
36 ;;; 3. The command `fortran-tab-mode' will toggle between fixed and tab
37 ;;; formatting modes. The file will not automatically be reformatted,
38 ;;; but either `indent-region' or `fortran-indent-subprogram' can be
39 ;;; used to reformat portions of the file.
40 ;;; 4. Several abbreviations have been added. Abbreviation mode is turned
42 ;;; 5. The routine fortran-blink-matching if has been incorporated (from
43 ;;; experimental version 1.27). If the variable of the same name is set
44 ;;; to t, the the matching if statement is blinked whenever an endif
46 ;;; 6. C-c C-w is now bound to fortran-window-create-momentarily (from
47 ;;; experimental version 1.27.)
48 ;;; 7. LFD is now bound to fortran-reindent-then-newline-and-indent.
49 ;;; 8. fortran-continuation-string (was fortran-continuation-char) is now
50 ;;; a string rather than a character.
51 ;;; 9. Fixed a bug from 1.21 that gave max-lisp-eval-depth exceeded when
52 ;;; Comments lines had !'s in them.
53 ;;; 10. DEC fortran recognizes a & in the first column as a continuation.
54 ;;; character. This mode does not recognize the & as a continuation
56 ;;; 11. fortran-blink-matching-if still is in effect when indenting a region.
57 ;;; Is this a desirable effect? (It looks kind of neat)
58 ;;; 12. If you strike a digit and there are exactly 5 characters, all spaces
59 ;;; to the left of the point, the digit will be inserted in place to
60 ;;; serve as a continuation line marker. Similarly, if the only thing to
61 ;;; the left of the point is a single tab, and the last command issued
62 ;;; was neither fortran-indent-line (TAB) or fortran-reindent-then-newline-
63 ;;; and-indent (LFD), the digit is inserted as a tab format style
64 ;;; continuation character.
65 ;;; 13. Both modes should usually work with tab-width set to other than 8.
66 ;;; However, in tab-mode, if tab-width is less than 6, the column number
67 ;;; for the minimum indentation is set to 6 so that all line numbers will
68 ;;; have have a tab after them. This may be a bit ugly, but why would
69 ;;; you want to use a tab-width other than 8 anyway?
70 ;;; 14. When in tab mode, the fortran column ruler will not be correct if
71 ;;; tab-width is not 8.
73 ;; This file is part of GNU Emacs.
75 ;; GNU Emacs is free software; you can redistribute it and/or modify
76 ;; it under the terms of the GNU General Public License as published by
77 ;; the Free Software Foundation; either version 1, or (at your option)
80 ;; GNU Emacs is distributed in the hope that it will be useful,
81 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
82 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
83 ;; GNU General Public License for more details.
85 ;; You should have received a copy of the GNU General Public License
86 ;; along with GNU Emacs; see the file COPYING. If not, write to
87 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
89 ;;; Author acknowledges help from Stephen Gildea <gildea@erl.mit.edu>
91 ;;; Bugs to bug-fortran-mode@erl.mit.edu
94 (defvar fortran-tab-mode-default nil
95 "*Default tabbing/carriage control style for empty files in fortran mode.
96 t indicates that tab-digit style of continuation control will be used.
97 nil indicates that continuation lines are marked with a character in
100 (defvar fortran-do-indent
3
101 "*Extra indentation applied to `do' blocks.")
103 (defvar fortran-if-indent
3
104 "*Extra indentation applied to `if' blocks.")
106 (defvar fortran-continuation-indent
5
107 "*Extra indentation applied to `continuation' lines.")
109 (defvar fortran-comment-indent-style
'fixed
110 "*nil forces comment lines not to be touched,
111 'fixed produces fixed comment indentation to comment-column,
112 and 'relative indents to current fortran indentation plus comment-column.")
114 (defvar fortran-comment-line-column
6
115 "*Indentation for text in comment lines.")
117 (defvar comment-line-start nil
118 "*Delimiter inserted to start new full-line comment.")
120 (defvar comment-line-start-skip nil
121 "*Regexp to match the start of a full-line comment.")
123 (defvar fortran-minimum-statement-indent
6
124 "*Minimum indentation for fortran statements.")
126 ;; Note that this is documented in the v18 manuals as being a string
127 ;; of length one rather than a single character.
128 ;; The code in this file accepts either format for compatibility.
129 (defvar fortran-comment-indent-char
" "
130 "*Single-character string inserted for Fortran comment indentation.
133 (defvar fortran-line-number-indent
1
134 "*Maximum indentation for Fortran line numbers.
135 5 means right-justify them within their five-column field.")
137 (defvar fortran-check-all-num-for-matching-do nil
138 "*Non-nil causes all numbered lines to be treated as possible do-loop ends.")
140 (defvar fortran-blink-matching-if nil
141 "*From a fortran `endif' statement, blink the matching `if' statement.")
143 (defvar fortran-continuation-string
"$"
144 "*Single-character string used for fortran continuation lines.
145 In fixed format continuation style, this character is inserted in
146 column 6 by \\[fortran-split-line] to begin a continuation line.
147 Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
148 convert the line into a continuation line of the appropriate style.
151 (defvar fortran-comment-region
"c$$$"
152 "*String inserted by \\[fortran-comment-region]\
153 at start of each line in region.")
155 (defvar fortran-electric-line-number t
156 "*Non-nil causes line number digits to be moved to the correct column as\
159 (defvar fortran-startup-message t
160 "*Non-nil displays a startup message when Fortran mode is first called.")
162 (defvar fortran-column-ruler
" "
163 "*String displayed above current line by \\[fortran-column-ruler].")
165 (defconst bug-fortran-mode
"bug-fortran-mode@erl.mit.edu"
166 "Address of mailing list for Fortran mode bugs.")
168 (defconst fortran-mode-version
"1.28.3")
170 (defvar fortran-mode-syntax-table nil
171 "Syntax table in use in Fortran mode buffers.")
173 (defvar fortran-analyze-depth
100
174 "Number of lines to scan to determine whether to use fixed or tab format\
177 (if fortran-mode-syntax-table
179 (setq fortran-mode-syntax-table
(make-syntax-table))
180 (modify-syntax-entry ?\
; "w" fortran-mode-syntax-table)
181 (modify-syntax-entry ?
\r " " fortran-mode-syntax-table
)
182 (modify-syntax-entry ?
+ "." fortran-mode-syntax-table
)
183 (modify-syntax-entry ?-
"." fortran-mode-syntax-table
)
184 (modify-syntax-entry ?
= "." fortran-mode-syntax-table
)
185 (modify-syntax-entry ?
* "." fortran-mode-syntax-table
)
186 (modify-syntax-entry ?
/ "." fortran-mode-syntax-table
)
187 (modify-syntax-entry ?
\' "\"" fortran-mode-syntax-table
)
188 (modify-syntax-entry ?
\" "\"" fortran-mode-syntax-table
)
189 (modify-syntax-entry ?
\\ "/" fortran-mode-syntax-table
)
190 (modify-syntax-entry ?.
"w" fortran-mode-syntax-table
)
191 (modify-syntax-entry ?
\n ">" fortran-mode-syntax-table
))
193 (defvar fortran-mode-map
()
194 "Keymap used in fortran mode.")
197 (setq fortran-mode-map
(make-sparse-keymap))
198 (define-key fortran-mode-map
";" 'fortran-abbrev-start
)
199 (define-key fortran-mode-map
"\C-c;" 'fortran-comment-region
)
200 (define-key fortran-mode-map
"\e\C-a" 'beginning-of-fortran-subprogram
)
201 (define-key fortran-mode-map
"\e\C-e" 'end-of-fortran-subprogram
)
202 (define-key fortran-mode-map
"\e;" 'fortran-indent-comment
)
203 (define-key fortran-mode-map
"\e\C-h" 'mark-fortran-subprogram
)
204 (define-key fortran-mode-map
"\e\n" 'fortran-split-line
)
205 (define-key fortran-mode-map
"\n" 'fortran-reindent-then-newline-and-indent
)
206 (define-key fortran-mode-map
"\e\C-q" 'fortran-indent-subprogram
)
207 (define-key fortran-mode-map
"\C-c\C-w" 'fortran-window-create-momentarily
)
208 (define-key fortran-mode-map
"\C-c\C-r" 'fortran-column-ruler
)
209 (define-key fortran-mode-map
"\C-c\C-p" 'fortran-previous-statement
)
210 (define-key fortran-mode-map
"\C-c\C-n" 'fortran-next-statement
)
211 (define-key fortran-mode-map
"\t" 'fortran-indent-line
)
212 (define-key fortran-mode-map
"0" 'fortran-electric-line-number
)
213 (define-key fortran-mode-map
"1" 'fortran-electric-line-number
)
214 (define-key fortran-mode-map
"2" 'fortran-electric-line-number
)
215 (define-key fortran-mode-map
"3" 'fortran-electric-line-number
)
216 (define-key fortran-mode-map
"4" 'fortran-electric-line-number
)
217 (define-key fortran-mode-map
"5" 'fortran-electric-line-number
)
218 (define-key fortran-mode-map
"6" 'fortran-electric-line-number
)
219 (define-key fortran-mode-map
"7" 'fortran-electric-line-number
)
220 (define-key fortran-mode-map
"8" 'fortran-electric-line-number
)
221 (define-key fortran-mode-map
"9" 'fortran-electric-line-number
))
223 (defvar fortran-mode-abbrev-table nil
)
224 (if fortran-mode-abbrev-table
226 (let ((ac abbrevs-changed
))
227 (define-abbrev-table 'fortran-mode-abbrev-table
())
228 (define-abbrev fortran-mode-abbrev-table
";au" "automatic" nil
)
229 (define-abbrev fortran-mode-abbrev-table
";b" "byte" nil
)
230 (define-abbrev fortran-mode-abbrev-table
";bl" "block data" nil
)
231 (define-abbrev fortran-mode-abbrev-table
";ch" "character" nil
)
232 (define-abbrev fortran-mode-abbrev-table
";cl" "close" nil
)
233 (define-abbrev fortran-mode-abbrev-table
";c" "continue" nil
)
234 (define-abbrev fortran-mode-abbrev-table
";cm" "common" nil
)
235 (define-abbrev fortran-mode-abbrev-table
";cx" "complex" nil
)
236 (define-abbrev fortran-mode-abbrev-table
";df" "define" nil
)
237 (define-abbrev fortran-mode-abbrev-table
";di" "dimension" nil
)
238 (define-abbrev fortran-mode-abbrev-table
";do" "double" nil
)
239 (define-abbrev fortran-mode-abbrev-table
";dc" "double complex" nil
)
240 (define-abbrev fortran-mode-abbrev-table
";dp" "double precision" nil
)
241 (define-abbrev fortran-mode-abbrev-table
";dw" "do while" nil
)
242 (define-abbrev fortran-mode-abbrev-table
";e" "else" nil
)
243 (define-abbrev fortran-mode-abbrev-table
";ed" "enddo" nil
)
244 (define-abbrev fortran-mode-abbrev-table
";el" "elseif" nil
)
245 (define-abbrev fortran-mode-abbrev-table
";en" "endif" nil
)
246 (define-abbrev fortran-mode-abbrev-table
";eq" "equivalence" nil
)
247 (define-abbrev fortran-mode-abbrev-table
";ex" "external" nil
)
248 (define-abbrev fortran-mode-abbrev-table
";ey" "entry" nil
)
249 (define-abbrev fortran-mode-abbrev-table
";f" "format" nil
)
250 (define-abbrev fortran-mode-abbrev-table
";fa" ".false." nil
)
251 (define-abbrev fortran-mode-abbrev-table
";fu" "function" nil
)
252 (define-abbrev fortran-mode-abbrev-table
";g" "goto" nil
)
253 (define-abbrev fortran-mode-abbrev-table
";im" "implicit" nil
)
254 (define-abbrev fortran-mode-abbrev-table
";ib" "implicit byte" nil
)
255 (define-abbrev fortran-mode-abbrev-table
";ic" "implicit complex" nil
)
256 (define-abbrev fortran-mode-abbrev-table
";ich" "implicit character" nil
)
257 (define-abbrev fortran-mode-abbrev-table
";ii" "implicit integer" nil
)
258 (define-abbrev fortran-mode-abbrev-table
";il" "implicit logical" nil
)
259 (define-abbrev fortran-mode-abbrev-table
";ir" "implicit real" nil
)
260 (define-abbrev fortran-mode-abbrev-table
";inc" "include" nil
)
261 (define-abbrev fortran-mode-abbrev-table
";in" "integer" nil
)
262 (define-abbrev fortran-mode-abbrev-table
";intr" "intrinsic" nil
)
263 (define-abbrev fortran-mode-abbrev-table
";l" "logical" nil
)
264 (define-abbrev fortran-mode-abbrev-table
";n" "namelist" nil
)
265 (define-abbrev fortran-mode-abbrev-table
";o" "open" nil
) ; was ;op
266 (define-abbrev fortran-mode-abbrev-table
";pa" "parameter" nil
)
267 (define-abbrev fortran-mode-abbrev-table
";pr" "program" nil
)
268 (define-abbrev fortran-mode-abbrev-table
";ps" "pause" nil
)
269 (define-abbrev fortran-mode-abbrev-table
";p" "print" nil
)
270 (define-abbrev fortran-mode-abbrev-table
";rc" "record" nil
)
271 (define-abbrev fortran-mode-abbrev-table
";re" "real" nil
)
272 (define-abbrev fortran-mode-abbrev-table
";r" "read" nil
)
273 (define-abbrev fortran-mode-abbrev-table
";rt" "return" nil
)
274 (define-abbrev fortran-mode-abbrev-table
";rw" "rewind" nil
)
275 (define-abbrev fortran-mode-abbrev-table
";s" "stop" nil
)
276 (define-abbrev fortran-mode-abbrev-table
";sa" "save" nil
)
277 (define-abbrev fortran-mode-abbrev-table
";st" "structure" nil
)
278 (define-abbrev fortran-mode-abbrev-table
";sc" "static" nil
)
279 (define-abbrev fortran-mode-abbrev-table
";su" "subroutine" nil
)
280 (define-abbrev fortran-mode-abbrev-table
";tr" ".true." nil
)
281 (define-abbrev fortran-mode-abbrev-table
";ty" "type" nil
)
282 (define-abbrev fortran-mode-abbrev-table
";vo" "volatile" nil
)
283 (define-abbrev fortran-mode-abbrev-table
";w" "write" nil
)
284 (setq abbrevs-changed ac
)))
287 (defun fortran-mode ()
288 "Major mode for editing fortran code.
289 Tab indents the current fortran line correctly.
290 `do' statements must not share a common `continue'.
292 Type `;?' or `;\\[help-command]' to display a list of built-in\
293 abbrevs for Fortran keywords.
298 Variables controlling indentation style and extra features:
301 Normally nil in Fortran mode. If you want to use comments
302 starting with `!', set this to the string \"!\".
304 Extra indentation within do blocks. (default 3)
306 Extra indentation within if blocks. (default 3)
307 fortran-continuation-indent
308 Extra indentation appled to continuation statements. (default 5)
309 fortran-comment-line-column
310 Amount of indentation for text within full-line comments. (default 6)
311 fortran-comment-indent-style
312 nil means don't change indentation of text in full-line comments,
313 fixed means indent that text at column fortran-comment-line-column
314 relative means indent at fortran-comment-line-column beyond the
315 indentation for a line of code.
317 fortran-comment-indent-char
318 Single-character string be inserted instead of space for
319 full-line comment indentation. (default \" \")
320 fortran-minimum-statement-indent
321 Minimum indentation for fortran statements. (default 6)
322 fortran-line-number-indent
323 Maximum indentation for line numbers. A line number will get
324 less than this much indentation if necessary to avoid reaching
325 column 5. (default 1)
326 fortran-check-all-num-for-matching-do
327 Non-nil causes all numbered lines to be treated as possible 'continue'
328 statements. (default nil)
329 fortran-blink-matching-if
330 From a fortran `endif' statement, blink the matching `if' statement.
332 fortran-continuation-string
333 Single-character string to be inserted in column 5 of a continuation
334 line. (default \"$\")
335 fortran-comment-region
336 String inserted by \\[fortran-comment-region] at start of each line in
337 region. (default \"c$$$\")
338 fortran-electric-line-number
339 Non-nil causes line number digits to be moved to the correct column
340 as typed. (default t)
341 fortran-startup-message
342 Set to nil to inhibit message first time Fortran mode is used.
344 Turning on Fortran mode calls the value of the variable fortran-mode-hook
345 with no args, if that value is non-nil."
347 (kill-all-local-variables)
348 (if fortran-startup-message
349 (message "Emacs Fortran mode version %s. Bugs to %s"
350 fortran-mode-version bug-fortran-mode
))
351 (setq fortran-startup-message nil
)
352 (setq local-abbrev-table fortran-mode-abbrev-table
)
353 (set-syntax-table fortran-mode-syntax-table
)
354 (make-local-variable 'indent-line-function
)
355 (setq indent-line-function
'fortran-indent-line
)
356 (make-local-variable 'comment-indent-hook
)
357 (setq comment-indent-hook
'fortran-comment-hook
)
358 (make-local-variable 'comment-line-start-skip
)
359 (setq comment-line-start-skip
360 "^[Cc*]\\(\\([^ \t\n]\\)\\1*\\)?[ \t]*") ;[^ \t\n]* handles c$$$
361 (make-local-variable 'comment-line-start
)
362 (setq comment-line-start
"c")
363 (make-local-variable 'comment-start-skip
)
364 (setq comment-start-skip
"![ \t]*")
365 (make-local-variable 'comment-start
)
366 (setq comment-start nil
)
367 (make-local-variable 'require-final-newline
)
368 (setq require-final-newline t
)
369 (make-local-variable 'abbrev-all-caps
)
370 (setq abbrev-all-caps t
)
371 (make-local-variable 'indent-tabs-mode
)
372 (setq indent-tabs-mode nil
)
373 (setq abbrev-mode t
) ; ?? (abbrev-mode 1) instead??
374 (use-local-map fortran-mode-map
)
375 (setq mode-name
"Fortran")
376 (setq major-mode
'fortran-mode
)
377 (make-local-variable 'fortran-tab-mode
)
378 (make-local-variable 'fortran-comment-line-column
)
379 (make-local-variable 'fortran-minimum-statement-indent
)
380 (make-local-variable 'fortran-column-ruler
)
381 (make-local-variable 'fortran-tab-mode-string
)
382 (fortran-tab-mode (fortran-analyze-file-format))
383 (run-hooks 'fortran-mode-hook
))
385 (defun fortran-comment-hook ()
387 (skip-chars-backward " \t")
388 (max (+ 1 (current-column))
391 (defun fortran-indent-comment ()
392 "Align or create comment on current line.
393 Existing comments of all types are recognized and aligned.
394 If the line has no comment, a side-by-side comment is inserted and aligned
395 if the value of comment-start is not nil.
396 Otherwise, a separate-line comment is inserted, on this line
397 or on a new line inserted before this line if this line is not blank."
400 ;; Recognize existing comments of either kind.
401 (cond ((looking-at comment-line-start-skip
)
402 (fortran-indent-line))
403 ((re-search-forward comment-start-skip
404 (save-excursion (end-of-line) (point)) t
)
405 (indent-for-comment))
406 ;; No existing comment.
407 ;; If side-by-side comments are defined, insert one,
408 ;; unless line is now blank.
409 ((and comment-start
(not (looking-at "^[ \t]*$")))
411 (delete-horizontal-space)
412 (indent-to (fortran-comment-hook))
413 (insert comment-start
))
414 ;; Else insert separate-line comment, making a new line if nec.
416 (if (looking-at "^[ \t]*$")
417 (delete-horizontal-space)
421 (insert comment-line-start
)
422 (insert-char (if (stringp fortran-comment-indent-char
)
423 (aref fortran-comment-indent-char
0)
424 fortran-comment-indent-char
)
425 (- (calculate-fortran-indent) (current-column))))))
427 (defun fortran-comment-region (beg-region end-region arg
)
428 "Comments every line in the region.
429 Puts fortran-comment-region at the beginning of every line in the region.
430 BEG-REGION and END-REGION are args which specify the region boundaries.
431 With non-nil ARG, uncomments the region."
432 (interactive "*r\nP")
433 (let ((end-region-mark (make-marker)) (save-point (point-marker)))
434 (set-marker end-region-mark end-region
)
435 (goto-char beg-region
)
437 (if (not arg
) ;comment the region
438 (progn (insert fortran-comment-region
)
439 (while (and (= (forward-line 1) 0)
440 (< (point) end-region-mark
))
441 (insert fortran-comment-region
)))
442 (let ((com (regexp-quote fortran-comment-region
))) ;uncomment the region
444 (delete-region (point) (match-end 0)))
445 (while (and (= (forward-line 1) 0)
446 (< (point) end-region-mark
))
448 (delete-region (point) (match-end 0))))))
449 (goto-char save-point
)
450 (set-marker end-region-mark nil
)
451 (set-marker save-point nil
)))
453 (defun fortran-abbrev-start ()
454 "Typing \";\\[help-command]\" or \";?\" lists all the fortran abbrevs.
455 Any other key combination is executed normally."
458 (insert last-command-char
)
459 (if (or (= (setq c
(read-char)) ??
) ;insert char if not equal to `?'
461 (fortran-abbrev-help)
462 (setq unread-command-char c
))))
464 (defun fortran-abbrev-help ()
465 "List the currently defined abbrevs in Fortran mode."
467 (message "Listing abbrev table...")
468 (require 'abbrevlist
)
469 (list-one-abbrev-table fortran-mode-abbrev-table
"*Help*")
470 (message "Listing abbrev table...done"))
472 (defun fortran-column-ruler ()
473 "Inserts a column ruler momentarily above current line, till next keystroke.
474 The ruler is defined by the value of fortran-column-ruler.
475 The key typed is executed unless it is SPC."
477 (momentary-string-display
478 fortran-column-ruler
(save-excursion (beginning-of-line) (point))
479 nil
"Type SPC or any command to erase ruler."))
481 (defun fortran-window-create ()
482 "Makes the window 72 columns wide.
483 See also fortran-window-create-momentarily."
485 (condition-case error
487 (let ((window-min-width 2))
488 (if (< (window-width) (screen-width))
489 (enlarge-window-horizontally (- (screen-width)
491 (split-window-horizontally 73)
493 (switch-to-buffer " fortran-window-extra" t
)
494 (select-window (previous-window))))
495 (error (message "No room for fortran window.")
498 (defun fortran-window-create-momentarily (&optional arg
)
499 "Momentarily makes the window 72 columns wide.
500 Optional ARG non-nil and non-unity disables the momentary feature.
501 See also fortran-window-create."
505 (save-window-excursion
506 (if (not (equal (fortran-window-create) 'error
))
507 (progn (message "Type SPC to continue editing.")
508 (let ((char (read-char)))
509 (or (equal char
(string-to-char " "))
510 (setq unread-command-char char
))))))
511 (fortran-window-create)))
513 (defun fortran-split-line ()
514 "Break line at point and insert continuation marker and alignment."
516 (delete-horizontal-space)
517 (if (save-excursion (beginning-of-line) (looking-at comment-line-start-skip
))
518 (insert "\n" comment-line-start
" ")
522 (insert-char (fortran-numerical-continuation-char) 1))
523 (insert "\n" fortran-continuation-string
)))
524 (fortran-indent-line))
526 (defun fortran-numerical-continuation-char ()
527 "Return a digit for tab-digit style of continution lines.
528 If, previous line is a tab-digit continuation line, returns that digit
529 plus one. Otherwise return 1. Zero not allowed."
532 (if (looking-at "\t[1-9]")
533 (+ ?
1 (%
(- (char-after (+ (point) 1)) ?
0) 9))
536 (defun delete-horizontal-regexp (chars)
537 "Delete all characters in CHARS around point.
538 CHARS is like the inside of a [...] in a regular expression
539 except that ] is never special and \ quotes ^, - or \."
541 (skip-chars-backward chars
)
542 (delete-region (point) (progn (skip-chars-forward chars
) (point))))
544 (defun fortran-electric-line-number (arg)
545 "Self insert, but if part of a Fortran line number indent it automatically.
546 Auto-indent does not happen if a numeric arg is used."
547 ;The use of arg may be superfluous here since there apears to be no way to
548 ;prefix a digit key with an argument.
550 (if (or arg
(not fortran-electric-line-number
))
552 (self-insert-command arg
)
553 (self-insert-command 1))
554 (if (or (and (= 5 (current-column))
557 (looking-at " ")));In col 5 with only spaces to left.
558 (and (= fortran-minimum-statement-indent
(current-column))
561 (looking-at "\t"));In col 8 with a single tab to the left.
562 (not (or (eq last-command
'fortran-indent-line
)
564 'fortran-reindent-then-newline-and-indent
))))
566 (re-search-backward "[^ \t0-9]"
570 t
)) ;not a line number
571 (looking-at "[0-9]") ;within a line number
573 (insert last-command-char
)
574 (skip-chars-backward " \t")
575 (insert last-command-char
)
576 (fortran-indent-line))))
578 (defun beginning-of-fortran-subprogram ()
579 "Moves point to the beginning of the current fortran subprogram."
581 (let ((case-fold-search t
))
582 (beginning-of-line -
1)
583 (re-search-backward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil
'move
)
584 (if (looking-at "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")
587 (defun end-of-fortran-subprogram ()
588 "Moves point to the end of the current fortran subprogram."
590 (let ((case-fold-search t
))
591 (beginning-of-line 2)
592 (re-search-forward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil
'move
)
593 (goto-char (match-beginning 0))
596 (defun mark-fortran-subprogram ()
597 "Put mark at end of fortran subprogram, point at beginning.
598 The marks are pushed."
600 (end-of-fortran-subprogram)
602 (beginning-of-fortran-subprogram))
604 (defun fortran-previous-statement ()
605 "Moves point to beginning of the previous fortran statement.
606 Returns 'first-statement if that statement is the first
607 non-comment Fortran statement in the file, and nil otherwise."
609 (let (not-first-statement continue-test
)
613 (concat "[ \t]*" (regexp-quote fortran-continuation-string
)))
614 (or (looking-at " [^ 0\n]")
615 (looking-at "\t[1-9]"))))
616 (while (and (setq not-first-statement
(= (forward-line -
1) 0))
617 (or (looking-at comment-line-start-skip
)
618 (looking-at "[ \t]*$")
619 (looking-at " [^ 0\n]")
620 (looking-at "\t[1-9]")
621 (looking-at (concat "[ \t]*" comment-start-skip
)))))
622 (cond ((and continue-test
623 (not not-first-statement
))
624 (message "Incomplete continuation statement."))
626 (fortran-previous-statement))
627 ((not not-first-statement
)
630 (defun fortran-next-statement ()
631 "Moves point to beginning of the next fortran statement.
632 Returns `last-statement' if that statement is the last
633 non-comment Fortran statement in the file, and nil otherwise."
635 (let (not-last-statement)
637 (while (and (setq not-last-statement
(= (forward-line 1) 0))
638 (or (looking-at comment-line-start-skip
)
639 (looking-at "[ \t]*$")
640 (looking-at " [^ 0\n]")
641 (looking-at "\t[1-9]")
642 (looking-at (concat "[ \t]*" comment-start-skip
)))))
643 (if (not not-last-statement
)
646 (defun fortran-blink-matching-if ()
647 "From a fortran `endif' statement, blink the matching `if' statement."
648 (let ((count 1) (top-of-window (window-start)) matching-if
649 (endif-point (point)) message
)
650 (if (save-excursion (beginning-of-line)
651 (skip-chars-forward " \t0-9")
652 (looking-at "end[ \t]*if\\b"))
655 (while (and (not (= count
0))
656 (not (eq (fortran-previous-statement)
659 "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")))
660 ; Keep local to subprogram
661 (skip-chars-forward " \t0-9")
662 (cond ((looking-at "if[ \t]*(")
663 (save-excursion (if (or (looking-at ".*)[
664 \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
665 (let (then-test);multi-line if-then
666 (while (and (= (forward-line 1) 0)
667 ;search forward for then
669 (looking-at " [^ 0\n]")
671 (looking-at "\t[1-9]"))
673 then-test
(looking-at
675 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
677 (setq count
(- count
1)))))
678 ((looking-at "end[ \t]*if\\b")
679 (setq count
(+ count
1)))))
680 (if (not (= count
0))
681 (setq message
"No matching if.")
682 (if (< (point) top-of-window
)
683 (setq message
(concat "Matches " (buffer-substring
684 (progn (beginning-of-line)
688 (setq matching-if
(point)))))
691 (goto-char matching-if
)
693 (goto-char endif-point
))))))
695 (defun fortran-indent-line ()
696 "Indents current fortran line based on its contents and on previous lines."
698 (let ((cfi (calculate-fortran-indent)))
701 (if (or (not (= cfi
(fortran-current-line-indentation)))
702 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
703 (not (fortran-line-number-indented-correctly-p))))
704 (fortran-indent-to-column cfi
)
706 (if (and (not (looking-at comment-line-start-skip
))
707 (re-search-forward comment-start-skip
708 (save-excursion (end-of-line) (point)) 'move
))
709 (fortran-indent-comment))))
710 ;; Never leave point in left margin.
711 (if (< (current-column) cfi
)
712 (move-to-column cfi
))
713 (if fortran-blink-matching-if
714 (fortran-blink-matching-if))))
716 (defun fortran-reindent-then-newline-and-indent ()
717 "Reindent the current fortran line, insert a newline and indent the newline.
718 An abbrev before point is expanded if abbrev-mode is non-nil."
720 (if abbrev-mode
(expand-abbrev))
723 (skip-chars-forward " \t")
724 (if (or (looking-at "[0-9]") ;Reindent only where it is most
725 (looking-at "end") ;likely to be necessary
727 (looking-at (regexp-quote fortran-continuation-string
)))
728 (fortran-indent-line)))
730 (fortran-indent-line))
732 (defun fortran-indent-subprogram ()
733 "Properly indents the Fortran subprogram which contains point."
736 (mark-fortran-subprogram)
737 (message "Indenting subprogram...")
738 (indent-region (point) (mark) nil
))
739 (message "Indenting subprogram...done."))
741 (defun calculate-fortran-indent ()
742 "Calculates the fortran indent column based on previous lines."
743 (let (icol first-statement
(case-fold-search t
)
744 (fortran-minimum-statement-indent
745 fortran-minimum-statement-indent
))
747 (setq first-statement
(fortran-previous-statement))
749 (setq icol fortran-minimum-statement-indent
)
751 (if (= (point) (point-min))
752 (setq icol fortran-minimum-statement-indent
)
753 (setq icol
(fortran-current-line-indentation)))
754 (skip-chars-forward " \t0-9")
755 (cond ((looking-at "if[ \t]*(")
756 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
757 (let (then-test) ;multi-line if-then
758 (while (and (= (forward-line 1) 0)
759 ;search forward for then
760 (or (looking-at " [^ 0\n]")
761 (looking-at "\t[1-9]"))
762 (not (setq then-test
(looking-at
763 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
765 (setq icol
(+ icol fortran-if-indent
))))
766 ((looking-at "\\(else\\|elseif\\)\\b")
767 (setq icol
(+ icol fortran-if-indent
)))
768 ((looking-at "do\\b")
769 (setq icol
(+ icol fortran-do-indent
)))
770 ((looking-at "end\\b[ \t]*[^ \t=(a-z]")
771 ; Previous END resets indent to minimum
772 (setq icol fortran-minimum-statement-indent
))))))
775 (cond ((looking-at "[ \t]*$"))
776 ((looking-at comment-line-start-skip
)
777 (setq fortran-minimum-statement-indent
0)
778 (cond ((eq fortran-comment-indent-style
'relative
)
779 (setq icol
(+ icol fortran-comment-line-column
)))
780 ((eq fortran-comment-indent-style
'fixed
)
781 (setq icol fortran-comment-line-column
))))
782 ((or (looking-at (concat "[ \t]*"
783 (regexp-quote fortran-continuation-string
)))
784 (looking-at " [^ 0\n]")
785 (looking-at "\t[1-9]"))
786 (setq icol
(+ icol fortran-continuation-indent
)))
788 ((and fortran-check-all-num-for-matching-do
789 (looking-at "[ \t]*[0-9]+")
790 (fortran-check-for-matching-do))
791 (setq icol
(- icol fortran-do-indent
)))
793 (skip-chars-forward " \t0-9")
794 (cond ((looking-at "end[ \t]*if\\b")
795 (setq icol
(- icol fortran-if-indent
)))
796 ((looking-at "\\(else\\|elseif\\)\\b")
797 (setq icol
(- icol fortran-if-indent
)))
798 ((and (looking-at "continue\\b")
799 (fortran-check-for-matching-do))
800 (setq icol
(- icol fortran-do-indent
)))
801 ((looking-at "end[ \t]*do\\b")
802 (setq icol
(- icol fortran-do-indent
)))
803 ((and (looking-at "end\\b[ \t]*[^ \t=(a-z]")
804 (not (= icol fortran-minimum-statement-indent
)))
805 (message "Warning: `end' not in column %d. Probably\
806 an unclosed block." fortran-minimum-statement-indent
))))))
807 (max fortran-minimum-statement-indent icol
)))
809 (defun fortran-current-line-indentation ()
810 "Indentation of current line, ignoring Fortran line number or continuation.
811 This is the column position of the first non-whitespace character
812 aside from the line number and/or column 5/8 line-continuation character.
813 For comment lines, returns indentation of the first
814 non-indentation text within the comment."
817 (cond ((looking-at comment-line-start-skip
)
818 (goto-char (match-end 0))
820 (if (stringp fortran-comment-indent-char
)
821 fortran-comment-indent-char
822 (char-to-string fortran-comment-indent-char
))))
823 ((or (looking-at " [^ 0\n]")
824 (looking-at "\t[1-9]"))
825 (goto-char (match-end 0)))
827 ;; Move past line number.
828 (re-search-forward "^[ \t0-9]*" (+ (point) 4) t
)))
829 ;; Move past whitespace.
830 (skip-chars-forward " \t")
833 (defun fortran-indent-to-column (col)
834 "Indents current line with spaces to column COL.
835 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
836 line, and this continuation character is retained on indentation;
837 2) If fortran-continuation-string is the first non-whitespace character,
838 this is a continuation line;
839 3) A non-continuation line which has a number as the first
840 non-whitespace character is a numbered line.
841 4) A tab followed by a digit indicates a continuation line."
844 (if (looking-at comment-line-start-skip
)
845 (if fortran-comment-indent-style
846 (let ((char (if (stringp fortran-comment-indent-char
)
847 (aref fortran-comment-indent-char
0)
848 fortran-comment-indent-char
)))
849 (goto-char (match-end 0))
850 (delete-horizontal-regexp (concat " \t" (char-to-string char
)))
851 (insert-char char
(- col
(current-column)))))
852 (if (looking-at "\t[1-9]")
854 (goto-char (match-end 0))
857 (insert fortran-continuation-string
))
858 (if (looking-at " [^ 0\n]")
860 (progn (delete-char 6)
862 (insert-char (fortran-numerical-continuation-char) 1))
864 (delete-horizontal-space)
865 ;; Put line number in columns 0-4
866 ;; or put continuation character in column 5.
868 ((looking-at (regexp-quote fortran-continuation-string
))
871 (indent-to fortran-minimum-statement-indent
)
873 (insert-char (fortran-numerical-continuation-char) 1))
876 ((looking-at "[0-9]+")
877 (let ((extra-space (- 5 (- (match-end 0) (point)))))
878 (if (< extra-space
0)
879 (message "Warning: line number exceeds 5-digit limit.")
880 (indent-to (min fortran-line-number-indent extra-space
))))
881 (skip-chars-forward "0-9")))))
882 ;; Point is now after any continuation character or line number.
883 ;; Put body of statement where specified.
884 (delete-horizontal-space)
886 ;; Indent any comment following code on the same line.
887 (if (and comment-start-skip
888 (re-search-forward comment-start-skip
889 (save-excursion (end-of-line) (point)) t
))
890 (progn (goto-char (match-beginning 0))
891 (if (not (= (current-column) (fortran-comment-hook)))
892 (progn (delete-horizontal-space)
893 (indent-to (fortran-comment-hook)))))))))
895 (defun fortran-line-number-indented-correctly-p ()
896 "Return t if current line's line number is correctly indented.
897 Do not call if there is no line number."
900 (skip-chars-forward " \t")
901 (and (<= (current-column) fortran-line-number-indent
)
902 (or (= (current-column) fortran-line-number-indent
)
903 (progn (skip-chars-forward "0-9")
904 (= (current-column) 5))))))
906 (defun fortran-check-for-matching-do ()
907 "When called from a numbered statement, returns t if matching 'do' is found.
908 Otherwise return a nil."
910 (case-fold-search t
))
913 (if (looking-at "[ \t]*[0-9]+")
915 (skip-chars-forward " \t")
916 (skip-chars-forward "0") ;skip past leading zeros
917 (setq charnum
(buffer-substring (point)
918 (progn (skip-chars-forward "0-9")
921 (and (re-search-backward
923 "\\(^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]\\)\\|\\(^[ \t0-9]*do[ \t]*0*"
924 charnum
"\\b\\)\\|\\(^[ \t]*0*" charnum
"\\b\\)")
926 (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum
))))))))
928 (defun fortran-analyze-file-format ()
929 "Return 0 if Fixed format is used, 1 if Tab formatting is used.
930 Use fortran-tab-mode-default if no non-comment statements are found in the
931 file before the end or the first fortran-analyze-depth lines."
933 (goto-char (point-min))
939 (> i fortran-analyze-depth
)))
943 ((looking-at "\t") 1)
945 (fortran-tab-mode-default 1)
948 (defun fortran-tab-mode (arg)
949 "Toggle fortran-tab-mode which indicates style of continuation lines.
950 With no argument, toggle on/off the tabbing mode of continuation lines.
951 If argument is a positive number, or non-nil if not a number, fortran-tab-mode
952 is turned on. Otherwise
953 If `fortran-tab-mode' is false"
955 (setq fortran-tab-mode
956 (if (null arg
) (not fortran-tab-mode
)
958 (> (prefix-numeric-value arg
) 0)
961 (fortran-setup-tab-format-style)
962 (fortran-setup-fixed-format-style))
963 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
965 (defun fortran-setup-tab-format-style ()
966 "Set up fortran mode to use the TAB-digit mode of continuation lines.
967 Use the command fortran-tab-mode to toggle between this and fixed format style."
968 (setq fortran-comment-line-column
(max tab-width
6))
969 (setq fortran-minimum-statement-indent
(max tab-width
6))
970 (setq indent-tabs-mode t
)
971 (setq fortran-column-ruler
973 "0 810 20 30 40 50 60 70\n"
974 "[ ]| { | | | | | | | | | | | | |}\n"))
975 (setq fortran-tab-mode-string
" TAB-format")
976 (set-buffer-modified-p (buffer-modified-p)))
978 (defun fortran-setup-fixed-format-style ()
979 "Set up fortran mode to use the column 6 mode of continuation lines.
980 Use the command fortran-tab-mode to toggle between this and tab
981 character format style."
982 (setq fortran-comment-line-column
6)
983 (setq fortran-minimum-statement-indent
6)
984 (setq indent-tabs-mode nil
)
985 (setq fortran-column-ruler
987 "0 4 6 10 20 30 40 50 60 70\n"
988 "[ ]|{ | | | | | | | | | | | | |}\n"))
989 (setq fortran-tab-mode-string
" Fixed-format")
990 (set-buffer-modified-p (buffer-modified-p)))
992 (or (assq 'fortran-tab-mode minor-mode-alist
)
993 (setq minor-mode-alist
(cons
994 '(fortran-tab-mode-string fortran-tab-mode-string
)
999 ;;; fortran.el ends here