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