1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;;; Copyright (c) 1986, 1992 Free Software Foundation, Inc.
5 ;; Author: Michael D. Prange <prange@erl.mit.edu>
6 ;; Maintainer: bug-fortran-mode@erl.mit.edu
7 ;; Version 1.30 (February 2, 1993)
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)
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.
28 ;; Fortran mode has been upgraded and is now maintained by Stephen A. Wood
29 ;; (saw@cebaf.gov). It now will use either fixed format continuation line
30 ;; markers (character in 6th column), or tab format coninuation line style
31 ;; (digit after a TAB character.) A auto-fill mode has been added to
32 ;; automatically wrap fortran lines that get too long.
34 ;; We acknowledge many contributions and valuable suggestions by
35 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
36 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
37 ;; Gary Sabot and Richard Stallman.
39 ;;; This file may be used with GNU Emacs version 18.xx if the following
40 ;;; variable and function substutions are made.
42 ;;; frame-width with screen-width
43 ;;; auto-fill-function with auto-fill-hook
44 ;;; (setq unread-command-events (list c)) with (setq unread-command-char c)
46 ;;; Bugs to bug-fortran-mode@erl.mit.edu
48 (defconst fortran-mode-version
"version 1.30")
53 (defvar fortran-tab-mode-default nil
54 "*Default tabbing/carriage control style for empty files in Fortran mode.
55 A value of t specifies tab-digit style of continuation control.
56 A value of nil specifies that continuation lines are marked
57 with a character in column 6.")
59 ;; Buffer local, used to display mode line.
60 (defvar fortran-tab-mode-string nil
61 "String to appear in mode line when TAB-format mode is on.")
63 (defvar fortran-do-indent
3
64 "*Extra indentation applied to DO blocks.")
66 (defvar fortran-if-indent
3
67 "*Extra indentation applied to IF blocks.")
69 (defvar fortran-structure-indent
3
70 "*Extra indentation applied to STRUCTURE, UNION and MAP blocks.")
72 (defvar fortran-continuation-indent
5
73 "*Extra indentation applied to Fortran continuation lines.")
75 (defvar fortran-comment-indent-style
'fixed
76 "*nil forces comment lines not to be touched,
77 'fixed makes fixed comment indentation to `fortran-comment-line-extra-indent'
78 columns beyond `fortran-minimum-statement-indent-fixed' (for
79 `indent-tabs-mode' of nil) or `fortran-minimum-statement-indent-tab' (for
80 `indent-tabs-mode' of t), and 'relative indents to current
81 Fortran indentation plus `fortran-comment-line-extra-indent'.")
83 (defvar fortran-comment-line-extra-indent
0
84 "*Amount of extra indentation for text within full-line comments.")
86 (defvar comment-line-start nil
87 "*Delimiter inserted to start new full-line comment.")
89 (defvar comment-line-start-skip nil
90 "*Regexp to match the start of a full-line comment.")
92 (defvar fortran-minimum-statement-indent-fixed
6
93 "*Minimum statement indentation for fixed format continuation style.")
95 (defvar fortran-minimum-statement-indent-tab
(max tab-width
6)
96 "*Minimum statement indentation for TAB format continuation style.")
98 ;; Note that this is documented in the v18 manuals as being a string
99 ;; of length one rather than a single character.
100 ;; The code in this file accepts either format for compatibility.
101 (defvar fortran-comment-indent-char
" "
102 "*Single-character string inserted for Fortran comment indentation.
105 (defvar fortran-line-number-indent
1
106 "*Maximum indentation for Fortran line numbers.
107 5 means right-justify them within their five-column field.")
109 (defvar fortran-check-all-num-for-matching-do nil
110 "*Non-nil causes all numbered lines to be treated as possible DO loop ends.")
112 (defvar fortran-blink-matching-if nil
113 "*From a Fortran ENDIF statement, blink the matching IF statement.")
115 (defvar fortran-continuation-string
"$"
116 "*Single-character string used for Fortran continuation lines.
117 In fixed format continuation style, this character is inserted in
118 column 6 by \\[fortran-split-line] to begin a continuation line.
119 Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
120 convert the line into a continuation line of the appropriate style.
123 (defvar fortran-comment-region
"c$$$"
124 "*String inserted by \\[fortran-comment-region]\
125 at start of each line in region.")
127 (defvar fortran-electric-line-number t
128 "*Non-nil causes line number digits to be moved to the correct column as\
131 (defvar fortran-startup-message t
132 "*Non-nil displays a startup message when Fortran mode is first called.")
134 (defvar fortran-column-ruler-fixed
135 "0 4 6 10 20 30 40 5\
137 [ ]|{ | | | | | | | | \
139 "*String displayed above current line by \\[fortran-column-ruler].
140 This variable used in fixed-format mode.")
142 (defvar fortran-column-ruler-tab
145 [ ]| { | | | | | | | | \
147 "*String displayed above current line by \\[fortran-column-ruler].
148 This variable used in TAB-format mode.")
150 (defconst bug-fortran-mode
"bug-fortran-mode@erl.mit.edu"
151 "Address of mailing list for Fortran mode bugs.")
153 (defvar fortran-mode-syntax-table nil
154 "Syntax table in use in Fortran mode buffers.")
156 (defvar fortran-analyze-depth
100
157 "Number of lines to scan to determine whether to use fixed or TAB format\
160 (defvar fortran-break-before-delimiters t
161 "*Non-nil causes `fortran-do-auto-fill' to break lines before delimeters.")
163 (if fortran-mode-syntax-table
165 (setq fortran-mode-syntax-table
(make-syntax-table))
166 (modify-syntax-entry ?\
; "w" fortran-mode-syntax-table)
167 (modify-syntax-entry ?
\r " " fortran-mode-syntax-table
)
168 (modify-syntax-entry ?
+ "." fortran-mode-syntax-table
)
169 (modify-syntax-entry ?-
"." fortran-mode-syntax-table
)
170 (modify-syntax-entry ?
= "." fortran-mode-syntax-table
)
171 (modify-syntax-entry ?
* "." fortran-mode-syntax-table
)
172 (modify-syntax-entry ?
/ "." fortran-mode-syntax-table
)
173 (modify-syntax-entry ?
\' "\"" fortran-mode-syntax-table
)
174 (modify-syntax-entry ?
\" "\"" fortran-mode-syntax-table
)
175 (modify-syntax-entry ?
\\ "/" fortran-mode-syntax-table
)
176 (modify-syntax-entry ?.
"w" fortran-mode-syntax-table
)
177 (modify-syntax-entry ?_
"w" fortran-mode-syntax-table
)
178 (modify-syntax-entry ?
\n ">" fortran-mode-syntax-table
))
180 (defvar fortran-mode-map
()
181 "Keymap used in Fortran mode.")
184 (setq fortran-mode-map
(make-sparse-keymap))
185 (define-key fortran-mode-map
";" 'fortran-abbrev-start
)
186 (define-key fortran-mode-map
"\C-c;" 'fortran-comment-region
)
187 (define-key fortran-mode-map
"\e\C-a" 'beginning-of-fortran-subprogram
)
188 (define-key fortran-mode-map
"\e\C-e" 'end-of-fortran-subprogram
)
189 (define-key fortran-mode-map
"\e;" 'fortran-indent-comment
)
190 (define-key fortran-mode-map
"\e\C-h" 'mark-fortran-subprogram
)
191 (define-key fortran-mode-map
"\e\n" 'fortran-split-line
)
192 (define-key fortran-mode-map
"\n" 'fortran-reindent-then-newline-and-indent
)
193 (define-key fortran-mode-map
"\e\C-q" 'fortran-indent-subprogram
)
194 (define-key fortran-mode-map
"\C-c\C-w" 'fortran-window-create-momentarily
)
195 (define-key fortran-mode-map
"\C-c\C-r" 'fortran-column-ruler
)
196 (define-key fortran-mode-map
"\C-c\C-p" 'fortran-previous-statement
)
197 (define-key fortran-mode-map
"\C-c\C-n" 'fortran-next-statement
)
198 (define-key fortran-mode-map
"\t" 'fortran-indent-line
)
199 (define-key fortran-mode-map
"0" 'fortran-electric-line-number
)
200 (define-key fortran-mode-map
"1" 'fortran-electric-line-number
)
201 (define-key fortran-mode-map
"2" 'fortran-electric-line-number
)
202 (define-key fortran-mode-map
"3" 'fortran-electric-line-number
)
203 (define-key fortran-mode-map
"4" 'fortran-electric-line-number
)
204 (define-key fortran-mode-map
"5" 'fortran-electric-line-number
)
205 (define-key fortran-mode-map
"6" 'fortran-electric-line-number
)
206 (define-key fortran-mode-map
"7" 'fortran-electric-line-number
)
207 (define-key fortran-mode-map
"8" 'fortran-electric-line-number
)
208 (define-key fortran-mode-map
"9" 'fortran-electric-line-number
))
210 (defvar fortran-mode-abbrev-table nil
)
211 (if fortran-mode-abbrev-table
213 (let ((ac abbrevs-changed
))
214 (define-abbrev-table 'fortran-mode-abbrev-table
())
215 (define-abbrev fortran-mode-abbrev-table
";au" "automatic" nil
)
216 (define-abbrev fortran-mode-abbrev-table
";b" "byte" nil
)
217 (define-abbrev fortran-mode-abbrev-table
";bd" "block data" nil
)
218 (define-abbrev fortran-mode-abbrev-table
";ch" "character" nil
)
219 (define-abbrev fortran-mode-abbrev-table
";cl" "close" nil
)
220 (define-abbrev fortran-mode-abbrev-table
";c" "continue" nil
)
221 (define-abbrev fortran-mode-abbrev-table
";cm" "common" nil
)
222 (define-abbrev fortran-mode-abbrev-table
";cx" "complex" nil
)
223 (define-abbrev fortran-mode-abbrev-table
";df" "define" nil
)
224 (define-abbrev fortran-mode-abbrev-table
";di" "dimension" nil
)
225 (define-abbrev fortran-mode-abbrev-table
";do" "double" nil
)
226 (define-abbrev fortran-mode-abbrev-table
";dc" "double complex" nil
)
227 (define-abbrev fortran-mode-abbrev-table
";dp" "double precision" nil
)
228 (define-abbrev fortran-mode-abbrev-table
";dw" "do while" nil
)
229 (define-abbrev fortran-mode-abbrev-table
";e" "else" nil
)
230 (define-abbrev fortran-mode-abbrev-table
";ed" "enddo" nil
)
231 (define-abbrev fortran-mode-abbrev-table
";el" "elseif" nil
)
232 (define-abbrev fortran-mode-abbrev-table
";en" "endif" nil
)
233 (define-abbrev fortran-mode-abbrev-table
";eq" "equivalence" nil
)
234 (define-abbrev fortran-mode-abbrev-table
";ew" "endwhere" nil
)
235 (define-abbrev fortran-mode-abbrev-table
";ex" "external" nil
)
236 (define-abbrev fortran-mode-abbrev-table
";ey" "entry" nil
)
237 (define-abbrev fortran-mode-abbrev-table
";f" "format" nil
)
238 (define-abbrev fortran-mode-abbrev-table
";fa" ".false." nil
)
239 (define-abbrev fortran-mode-abbrev-table
";fu" "function" nil
)
240 (define-abbrev fortran-mode-abbrev-table
";g" "goto" nil
)
241 (define-abbrev fortran-mode-abbrev-table
";im" "implicit" nil
)
242 (define-abbrev fortran-mode-abbrev-table
";ib" "implicit byte" nil
)
243 (define-abbrev fortran-mode-abbrev-table
";ic" "implicit complex" nil
)
244 (define-abbrev fortran-mode-abbrev-table
";ich" "implicit character" nil
)
245 (define-abbrev fortran-mode-abbrev-table
";ii" "implicit integer" nil
)
246 (define-abbrev fortran-mode-abbrev-table
";il" "implicit logical" nil
)
247 (define-abbrev fortran-mode-abbrev-table
";ir" "implicit real" nil
)
248 (define-abbrev fortran-mode-abbrev-table
";inc" "include" nil
)
249 (define-abbrev fortran-mode-abbrev-table
";in" "integer" nil
)
250 (define-abbrev fortran-mode-abbrev-table
";intr" "intrinsic" nil
)
251 (define-abbrev fortran-mode-abbrev-table
";l" "logical" nil
)
252 (define-abbrev fortran-mode-abbrev-table
";n" "namelist" nil
)
253 (define-abbrev fortran-mode-abbrev-table
";o" "open" nil
) ; was ;op
254 (define-abbrev fortran-mode-abbrev-table
";pa" "parameter" nil
)
255 (define-abbrev fortran-mode-abbrev-table
";pr" "program" nil
)
256 (define-abbrev fortran-mode-abbrev-table
";ps" "pause" nil
)
257 (define-abbrev fortran-mode-abbrev-table
";p" "print" nil
)
258 (define-abbrev fortran-mode-abbrev-table
";rc" "record" nil
)
259 (define-abbrev fortran-mode-abbrev-table
";re" "real" nil
)
260 (define-abbrev fortran-mode-abbrev-table
";r" "read" nil
)
261 (define-abbrev fortran-mode-abbrev-table
";rt" "return" nil
)
262 (define-abbrev fortran-mode-abbrev-table
";rw" "rewind" nil
)
263 (define-abbrev fortran-mode-abbrev-table
";s" "stop" nil
)
264 (define-abbrev fortran-mode-abbrev-table
";sa" "save" nil
)
265 (define-abbrev fortran-mode-abbrev-table
";st" "structure" nil
)
266 (define-abbrev fortran-mode-abbrev-table
";sc" "static" nil
)
267 (define-abbrev fortran-mode-abbrev-table
";su" "subroutine" nil
)
268 (define-abbrev fortran-mode-abbrev-table
";tr" ".true." nil
)
269 (define-abbrev fortran-mode-abbrev-table
";ty" "type" nil
)
270 (define-abbrev fortran-mode-abbrev-table
";vo" "volatile" nil
)
271 (define-abbrev fortran-mode-abbrev-table
";w" "write" nil
)
272 (define-abbrev fortran-mode-abbrev-table
";wh" "where" nil
)
273 (setq abbrevs-changed ac
)))
276 (defun fortran-mode ()
277 "Major mode for editing Fortran code.
278 \\[fortran-indent-line] indents the current Fortran line correctly.
279 DO statements must not share a common CONTINUE.
281 Type ;? or ;\\[help-command] to display a list of built-in\
282 abbrevs for Fortran keywords.
287 Variables controlling indentation style and extra features:
290 Normally nil in Fortran mode. If you want to use comments
291 starting with `!', set this to the string \"!\".
293 Extra indentation within do blocks. (default 3)
295 Extra indentation within if blocks. (default 3)
296 fortran-structure-indent
297 Extra indentation within structure, union and map blocks. (default 3)
298 fortran-continuation-indent
299 Extra indentation applied to continuation statements. (default 5)
300 fortran-comment-line-extra-indent
301 Amount of extra indentation for text within full-line comments. (default 0)
302 fortran-comment-indent-style
303 nil means don't change indentation of text in full-line comments,
304 fixed means indent that text at `fortran-comment-line-extra-indent' beyond
305 the value of `fortran-minimum-statement-indent-fixed' (for fixed
306 format continuation style) or `fortran-minimum-statement-indent-tab'
307 (for TAB format continuation style).
308 relative means indent at `fortran-comment-line-extra-indent' beyond the
309 indentation for a line of code.
311 fortran-comment-indent-char
312 Single-character string to be inserted instead of space for
313 full-line comment indentation. (default \" \")
314 fortran-minimum-statement-indent-fixed
315 Minimum indentation for Fortran statements in fixed format mode. (def.6)
316 fortran-minimum-statement-indent-tab
317 Minimum indentation for Fortran statements in TAB format mode. (default 9)
318 fortran-line-number-indent
319 Maximum indentation for line numbers. A line number will get
320 less than this much indentation if necessary to avoid reaching
321 column 5. (default 1)
322 fortran-check-all-num-for-matching-do
323 Non-nil causes all numbered lines to be treated as possible \"continue\"
324 statements. (default nil)
325 fortran-blink-matching-if
326 From a Fortran ENDIF statement, blink the matching IF statement.
328 fortran-continuation-string
329 Single-character string to be inserted in column 5 of a continuation
330 line. (default \"$\")
331 fortran-comment-region
332 String inserted by \\[fortran-comment-region] at start of each line in
333 region. (default \"c$$$\")
334 fortran-electric-line-number
335 Non-nil causes line number digits to be moved to the correct column
336 as typed. (default t)
337 fortran-break-before-delimiters
338 Non-nil causes `fortran-do-auto-fill' breaks lines before delimeters.
340 fortran-startup-message
341 Set to nil to inhibit message first time Fortran mode is used.
343 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
344 with no args, if that value is non-nil."
346 (kill-all-local-variables)
347 (if fortran-startup-message
348 (message "Emacs Fortran mode %s. Bugs to %s"
349 fortran-mode-version bug-fortran-mode
))
350 (setq fortran-startup-message nil
)
351 (setq local-abbrev-table fortran-mode-abbrev-table
)
352 (set-syntax-table fortran-mode-syntax-table
)
353 (make-local-variable 'fortran-break-before-delimiters
)
354 (setq fortran-break-before-delimiters t
)
355 (make-local-variable 'indent-line-function
)
356 (setq indent-line-function
'fortran-indent-line
)
357 (make-local-variable 'comment-indent-function
)
358 (setq comment-indent-function
'fortran-comment-hook
)
359 (make-local-variable 'comment-line-start-skip
)
360 (setq comment-line-start-skip
361 "^[Cc*]\\(\\([^ \t\n]\\)\\2\\2*\\)?[ \t]*\\|^#.*")
362 (make-local-variable 'comment-line-start
)
363 (setq comment-line-start
"c")
364 (make-local-variable 'comment-start-skip
)
365 (setq comment-start-skip
"![ \t]*")
366 (make-local-variable 'comment-start
)
367 (setq comment-start nil
)
368 (make-local-variable 'require-final-newline
)
369 (setq require-final-newline t
)
370 (make-local-variable 'abbrev-all-caps
)
371 (setq abbrev-all-caps t
)
372 (make-local-variable 'indent-tabs-mode
)
373 (setq indent-tabs-mode nil
)
374 ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
375 (setq fill-column
72) ; Already local?
376 (use-local-map fortran-mode-map
)
377 (setq mode-name
"Fortran")
378 (setq major-mode
'fortran-mode
)
379 ;;;(make-local-variable 'fortran-tab-mode)
380 (make-local-variable 'fortran-comment-line-extra-indent
)
381 (make-local-variable 'fortran-minimum-statement-indent-fixed
)
382 (make-local-variable 'fortran-minimum-statement-indent-tab
)
383 (make-local-variable 'fortran-column-ruler-fixed
)
384 (make-local-variable 'fortran-column-ruler-tab
)
385 (make-local-variable 'fortran-tab-mode-string
)
386 (setq fortran-tab-mode-string
" TAB-format")
387 (setq indent-tabs-mode
(fortran-analyze-file-format))
388 (run-hooks 'fortran-mode-hook
))
390 (defun fortran-comment-hook ()
392 (skip-chars-backward " \t")
393 (max (+ 1 (current-column))
396 (defun fortran-indent-comment ()
397 "Align or create comment on current line.
398 Existing comments of all types are recognized and aligned.
399 If the line has no comment, a side-by-side comment is inserted and aligned
400 if the value of comment-start is not nil.
401 Otherwise, a separate-line comment is inserted, on this line
402 or on a new line inserted before this line if this line is not blank."
405 ;; Recognize existing comments of either kind.
406 (cond ((looking-at comment-line-start-skip
)
407 (fortran-indent-line))
408 ((fortran-find-comment-start-skip) ; catches any inline comment and
409 ; leaves point after comment-start-skip
410 (if comment-start-skip
411 (progn (goto-char (match-beginning 0))
412 (if (not (= (current-column) (fortran-comment-hook)))
413 (progn (delete-horizontal-space)
414 (indent-to (fortran-comment-hook)))))
415 (end-of-line))) ; otherwise goto end of line or sth else?
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]*$")))
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)
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
)
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
454 (delete-region (point) (match-end 0)))
455 (while (and (= (forward-line 1) 0)
456 (< (point) end-region-mark
))
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."
468 (insert last-command-char
)
469 (if (or (eq (setq c
(read-event)) ??
) ;insert char if not equal to `?'
471 (fortran-abbrev-help)
472 (setq unread-command-events
(list c
)))))
474 (defun fortran-abbrev-help ()
475 "List the currently defined abbrevs in Fortran mode."
477 (message "Listing abbrev table...")
478 (display-buffer (fortran-prepare-abbrev-list-buffer))
479 (message "Listing abbrev table...done"))
481 (defun fortran-prepare-abbrev-list-buffer ()
483 (set-buffer (get-buffer-create "*Abbrevs*"))
485 (insert-abbrev-table-description 'fortran-mode-abbrev-table t
)
486 (goto-char (point-min))
487 (set-buffer-modified-p nil
)
489 (get-buffer-create "*Abbrevs*"))
491 (defun fortran-column-ruler ()
492 "Inserts a column ruler momentarily above current line, till next keystroke.
493 The ruler is defined by the value of `fortran-column-ruler'.
494 The key typed is executed unless it is SPC."
496 (momentary-string-display
498 fortran-column-ruler-tab
499 fortran-column-ruler-fixed
)
502 (if (eq (window-start (selected-window))
503 (window-point (selected-window)))
504 (progn (forward-line) (point))
506 nil
"Type SPC or any command to erase ruler."))
508 (defun fortran-window-create ()
509 "Makes the window 72 columns wide.
510 See also `fortran-window-create-momentarily'."
512 (condition-case error
514 (let ((window-min-width 2))
515 (if (< (window-width) (frame-width))
516 (enlarge-window-horizontally (- (frame-width)
518 (split-window-horizontally 73)
520 (switch-to-buffer " fortran-window-extra" t
)
521 (select-window (previous-window))))
522 (error (message "No room for Fortran window.")
525 (defun fortran-window-create-momentarily (&optional arg
)
526 "Momentarily makes the window 72 columns wide.
527 Optional ARG non-nil and non-unity disables the momentary feature.
528 See also `fortran-window-create'."
532 (save-window-excursion
533 (if (not (equal (fortran-window-create) 'error
))
534 (progn (message "Type SPC to continue editing.")
535 (let ((char (read-event)))
536 (or (equal char
(string-to-char " "))
537 (setq unread-command-events
(list char
)))))))
538 (fortran-window-create)))
540 (defun fortran-split-line ()
541 "Break line at point and insert continuation marker and alignment."
543 (delete-horizontal-space)
544 (if (save-excursion (beginning-of-line) (looking-at comment-line-start-skip
))
545 (insert "\n" comment-line-start
" ")
549 (insert-char (fortran-numerical-continuation-char) 1))
550 (insert "\n " fortran-continuation-string
)));Space after \n important
551 (fortran-indent-line)) ;when the cont string is C, c or *.
553 (defun fortran-numerical-continuation-char ()
554 "Return a digit for tab-digit style of continution lines.
555 If, previous line is a tab-digit continuation line, returns that digit
556 plus one. Otherwise return 1. Zero not allowed."
559 (if (looking-at "\t[1-9]")
560 (+ ?
1 (%
(- (char-after (+ (point) 1)) ?
0) 9))
563 (defun delete-horizontal-regexp (chars)
564 "Delete all characters in CHARS around point.
565 CHARS is like the inside of a [...] in a regular expression
566 except that ] is never special and \ quotes ^, - or \."
568 (skip-chars-backward chars
)
569 (delete-region (point) (progn (skip-chars-forward chars
) (point))))
571 (defun fortran-electric-line-number (arg)
572 "Self insert, but if part of a Fortran line number indent it automatically.
573 Auto-indent does not happen if a numeric arg is used."
575 (if (or arg
(not fortran-electric-line-number
))
577 (self-insert-command (prefix-numeric-value arg
))
578 (self-insert-command 1))
579 (if (or (and (= 5 (current-column))
582 (looking-at " ")));In col 5 with only spaces to left.
583 (and (= (if indent-tabs-mode
584 fortran-minimum-statement-indent-tab
585 fortran-minimum-statement-indent-fixed
) (current-column))
588 (looking-at "\t"));In col 8 with a single tab to the left.
589 (not (or (eq last-command
'fortran-indent-line
)
591 'fortran-reindent-then-newline-and-indent
))))
593 (re-search-backward "[^ \t0-9]"
597 t
)) ;not a line number
598 (looking-at "[0-9]") ;within a line number
600 (self-insert-command (prefix-numeric-value arg
))
601 (skip-chars-backward " \t")
602 (insert last-command-char
)
603 (fortran-indent-line))))
605 (defun beginning-of-fortran-subprogram ()
606 "Moves point to the beginning of the current Fortran subprogram."
608 (let ((case-fold-search t
))
609 (beginning-of-line -
1)
610 (re-search-backward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil
'move
)
611 (if (looking-at "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")
614 (defun end-of-fortran-subprogram ()
615 "Moves point to the end of the current Fortran subprogram."
617 (let ((case-fold-search t
))
618 (beginning-of-line 2)
619 (re-search-forward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil
'move
)
620 (goto-char (match-beginning 0))
623 (defun mark-fortran-subprogram ()
624 "Put mark at end of Fortran subprogram, point at beginning.
625 The marks are pushed."
627 (end-of-fortran-subprogram)
629 (beginning-of-fortran-subprogram))
631 (defun fortran-previous-statement ()
632 "Moves point to beginning of the previous Fortran statement.
633 Returns `first-statement' if that statement is the first
634 non-comment Fortran statement in the file, and nil otherwise."
636 (let (not-first-statement continue-test
)
640 (not (looking-at comment-line-start-skip
))
642 (concat "[ \t]*" (regexp-quote fortran-continuation-string
)))
643 (or (looking-at " [^ 0\n]")
644 (looking-at "\t[1-9]")))))
645 (while (and (setq not-first-statement
(= (forward-line -
1) 0))
646 (or (looking-at comment-line-start-skip
)
647 (looking-at "[ \t]*$")
648 (looking-at " [^ 0\n]")
649 (looking-at "\t[1-9]")
650 (looking-at (concat "[ \t]*" comment-start-skip
)))))
651 (cond ((and continue-test
652 (not not-first-statement
))
653 (message "Incomplete continuation statement."))
655 (fortran-previous-statement))
656 ((not not-first-statement
)
659 (defun fortran-next-statement ()
660 "Moves point to beginning of the next Fortran statement.
661 Returns `last-statement' if that statement is the last
662 non-comment Fortran statement in the file, and nil otherwise."
664 (let (not-last-statement)
666 (while (and (setq not-last-statement
667 (and (= (forward-line 1) 0)
669 (or (looking-at comment-line-start-skip
)
670 (looking-at "[ \t]*$")
671 (looking-at " [^ 0\n]")
672 (looking-at "\t[1-9]")
673 (looking-at (concat "[ \t]*" comment-start-skip
)))))
674 (if (not not-last-statement
)
677 (defun fortran-blink-matching-if ()
678 "From a Fortran ENDIF statement, blink the matching IF statement."
679 (let ((count 1) (top-of-window (window-start)) matching-if
680 (endif-point (point)) message
)
681 (if (save-excursion (beginning-of-line)
682 (skip-chars-forward " \t0-9")
683 (looking-at "end[ \t]*if\\b"))
686 (while (and (not (= count
0))
687 (not (eq (fortran-previous-statement)
690 "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")))
691 ; Keep local to subprogram
692 (skip-chars-forward " \t0-9")
693 (cond ((looking-at "if[ \t]*(")
696 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
697 (let (then-test);multi-line if-then
699 (and (= (forward-line 1) 0)
700 ;search forward for then
701 (or (looking-at " [^ 0\n]")
702 (looking-at "\t[1-9]"))
707 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
709 (setq count
(- count
1)))))
710 ((looking-at "end[ \t]*if\\b")
711 (setq count
(+ count
1)))))
712 (if (not (= count
0))
713 (setq message
"No matching if.")
714 (if (< (point) top-of-window
)
715 (setq message
(concat "Matches " (buffer-substring
716 (progn (beginning-of-line)
720 (setq matching-if
(point)))))
722 (message "%s" message
)
723 (goto-char matching-if
)
725 (goto-char endif-point
))))))
727 (defun fortran-indent-line ()
728 "Indents current Fortran line based on its contents and on previous lines."
730 (let ((cfi (calculate-fortran-indent)))
733 (if (or (not (= cfi
(fortran-current-line-indentation)))
734 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t
)
735 (not (fortran-line-number-indented-correctly-p))))
736 (fortran-indent-to-column cfi
)
738 (if (and (not (looking-at comment-line-start-skip
))
739 (fortran-find-comment-start-skip))
740 (fortran-indent-comment))))
741 ;; Never leave point in left margin.
742 (if (< (current-column) cfi
)
743 (move-to-column cfi
))
744 (if (and auto-fill-function
745 (> (save-excursion (end-of-line) (current-column)) fill-column
))
748 (fortran-do-auto-fill)))
749 (if fortran-blink-matching-if
750 (fortran-blink-matching-if))))
752 (defun fortran-reindent-then-newline-and-indent ()
753 "Reindent the current Fortran line, insert a newline and indent the newline.
754 An abbrev before point is expanded if `abbrev-mode' is non-nil."
756 (if abbrev-mode
(expand-abbrev))
759 (skip-chars-forward " \t")
760 (if (or (looking-at "[0-9]") ;Reindent only where it is most
761 (looking-at "end") ;likely to be necessary
763 (looking-at (regexp-quote fortran-continuation-string
)))
764 (fortran-indent-line)))
766 (fortran-indent-line))
768 (defun fortran-indent-subprogram ()
769 "Properly indents the Fortran subprogram which contains point."
772 (mark-fortran-subprogram)
773 (message "Indenting subprogram...")
774 (indent-region (point) (mark) nil
))
775 (message "Indenting subprogram...done."))
777 (defun calculate-fortran-indent ()
778 "Calculates the Fortran indent column based on previous lines."
779 (let (icol first-statement
(case-fold-search t
)
780 (fortran-minimum-statement-indent
782 fortran-minimum-statement-indent-tab
783 fortran-minimum-statement-indent-fixed
)))
785 (setq first-statement
(fortran-previous-statement))
787 (setq icol fortran-minimum-statement-indent
)
789 (if (= (point) (point-min))
790 (setq icol fortran-minimum-statement-indent
)
791 (setq icol
(fortran-current-line-indentation)))
792 (skip-chars-forward " \t0-9")
793 (cond ((looking-at "if[ \t]*(")
794 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
795 (let (then-test) ;multi-line if-then
796 (while (and (= (forward-line 1) 0)
797 ;;search forward for then
798 (or (looking-at " [^ 0\n]")
799 (looking-at "\t[1-9]"))
800 (not (setq then-test
(looking-at
802 *[^ \t_$(=a-z0-9]")))))
804 (setq icol
(+ icol fortran-if-indent
))))
805 ((looking-at "\\(else\\|elseif\\)\\b")
806 (setq icol
(+ icol fortran-if-indent
)))
807 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
808 (setq icol
(+ icol fortran-if-indent
)))
809 ((looking-at "where.*(.*)[ \t]*\n")
810 (setq icol
(+ icol fortran-if-indent
)))
811 ((looking-at "do\\b")
812 (setq icol
(+ icol fortran-do-indent
)))
814 "\\(structure\\|union\\|map\\)\\b[ \t]*[^ \t=(a-z]")
815 (setq icol
(+ icol fortran-structure-indent
)))
816 ((looking-at "end\\b[ \t]*[^ \t=(a-z]")
817 ;; Previous END resets indent to minimum
818 (setq icol fortran-minimum-statement-indent
))))))
821 (cond ((looking-at "[ \t]*$"))
822 ((looking-at comment-line-start-skip
)
823 (cond ((eq fortran-comment-indent-style
'relative
)
824 (setq icol
(+ icol fortran-comment-line-extra-indent
)))
825 ((eq fortran-comment-indent-style
'fixed
)
826 (setq icol
(+ fortran-minimum-statement-indent
827 fortran-comment-line-extra-indent
))))
828 (setq fortran-minimum-statement-indent
0))
829 ((or (looking-at (concat "[ \t]*"
831 fortran-continuation-string
)))
832 (looking-at " [^ 0\n]")
833 (looking-at "\t[1-9]"))
834 (setq icol
(+ icol fortran-continuation-indent
)))
836 ((and fortran-check-all-num-for-matching-do
837 (looking-at "[ \t]*[0-9]+")
838 (fortran-check-for-matching-do))
839 (setq icol
(- icol fortran-do-indent
)))
841 (skip-chars-forward " \t0-9")
842 (cond ((looking-at "end[ \t]*if\\b")
843 (setq icol
(- icol fortran-if-indent
)))
844 ((looking-at "\\(else\\|elseif\\)\\b")
845 (setq icol
(- icol fortran-if-indent
)))
846 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
847 (setq icol
(- icol fortran-if-indent
)))
848 ((looking-at "end[ \t]*where\\b")
849 (setq icol
(- icol fortran-if-indent
)))
850 ((and (looking-at "continue\\b")
851 (fortran-check-for-matching-do))
852 (setq icol
(- icol fortran-do-indent
)))
853 ((looking-at "end[ \t]*do\\b")
854 (setq icol
(- icol fortran-do-indent
)))
857 \\(structure\\|union\\|map\\)\\b[ \t]*[^ \t=(a-z]")
858 (setq icol
(- icol fortran-structure-indent
)))
859 ((and (looking-at "end\\b[ \t]*[^ \t=(a-z]")
860 (not (= icol fortran-minimum-statement-indent
)))
861 (message "Warning: `end' not in column %d. Probably\
862 an unclosed block." fortran-minimum-statement-indent
))))))
863 (max fortran-minimum-statement-indent icol
)))
865 (defun fortran-current-line-indentation ()
866 "Indentation of current line, ignoring Fortran line number or continuation.
867 This is the column position of the first non-whitespace character
868 aside from the line number and/or column 5/8 line-continuation character.
869 For comment lines, returns indentation of the first
870 non-indentation text within the comment."
873 (cond ((looking-at comment-line-start-skip
)
874 (goto-char (match-end 0))
876 (if (stringp fortran-comment-indent-char
)
877 fortran-comment-indent-char
878 (char-to-string fortran-comment-indent-char
))))
879 ((or (looking-at " [^ 0\n]")
880 (looking-at "\t[1-9]"))
881 (goto-char (match-end 0)))
883 ;; Move past line number.
884 (skip-chars-forward "[ \t0-9]");From Uli
886 ;; Move past whitespace.
887 (skip-chars-forward " \t")
890 (defun fortran-indent-to-column (col)
891 "Indents current line with spaces to column COL.
892 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
893 line, and this continuation character is retained on indentation;
894 2) If `fortran-continuation-string' is the first non-whitespace
895 character, this is a continuation line;
896 3) A non-continuation line which has a number as the first
897 non-whitespace character is a numbered line.
898 4) A TAB followed by a digit indicates a continuation line."
901 (if (looking-at comment-line-start-skip
)
902 (if fortran-comment-indent-style
903 (let ((char (if (stringp fortran-comment-indent-char
)
904 (aref fortran-comment-indent-char
0)
905 fortran-comment-indent-char
)))
906 (goto-char (match-end 0))
907 (delete-horizontal-regexp (concat " \t" (char-to-string char
)))
908 (insert-char char
(- col
(current-column)))))
909 (if (looking-at "\t[1-9]")
911 (goto-char (match-end 0))
914 (insert fortran-continuation-string
))
915 (if (looking-at " [^ 0\n]")
917 (progn (delete-char 6)
919 (insert-char (fortran-numerical-continuation-char) 1))
921 (delete-horizontal-space)
922 ;; Put line number in columns 0-4
923 ;; or put continuation character in column 5.
925 ((looking-at (regexp-quote fortran-continuation-string
))
930 fortran-minimum-statement-indent-tab
931 fortran-minimum-statement-indent-fixed
))
933 (insert-char (fortran-numerical-continuation-char) 1))
936 ((looking-at "[0-9]+")
937 (let ((extra-space (- 5 (- (match-end 0) (point)))))
938 (if (< extra-space
0)
939 (message "Warning: line number exceeds 5-digit limit.")
940 (indent-to (min fortran-line-number-indent extra-space
))))
941 (skip-chars-forward "0-9")))))
942 ;; Point is now after any continuation character or line number.
943 ;; Put body of statement where specified.
944 (delete-horizontal-space)
946 ;; Indent any comment following code on the same line.
947 (if (and comment-start-skip
948 (fortran-find-comment-start-skip))
949 (progn (goto-char (match-beginning 0))
950 (if (not (= (current-column) (fortran-comment-hook)))
951 (progn (delete-horizontal-space)
952 (indent-to (fortran-comment-hook)))))))))
954 (defun fortran-line-number-indented-correctly-p ()
955 "Return t if current line's line number is correctly indented.
956 Do not call if there is no line number."
959 (skip-chars-forward " \t")
960 (and (<= (current-column) fortran-line-number-indent
)
961 (or (= (current-column) fortran-line-number-indent
)
962 (progn (skip-chars-forward "0-9")
963 (= (current-column) 5))))))
965 (defun fortran-check-for-matching-do ()
966 "When called from a numbered statement, returns t if matching DO is found.
967 Otherwise return a nil."
969 (case-fold-search t
))
972 (if (looking-at "[ \t]*[0-9]+")
974 (skip-chars-forward " \t")
975 (skip-chars-forward "0") ;skip past leading zeros
976 (setq charnum
(buffer-substring (point)
977 (progn (skip-chars-forward "0-9")
980 (and (re-search-backward
982 "\\(^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]\\)\\|\\(^[ \t0-9]*do\
984 charnum
"\\b\\)\\|\\(^[ \t]*0*" charnum
"\\b\\)")
986 (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum
))))))))
988 (defun fortran-find-comment-start-skip ()
989 "Move to past `comment-start-skip' found on current line.
990 Return t if `comment-start-skip' found, nil if not."
991 ;;; In order to move point only if comment-start-skip is found,
992 ;;; this one uses a lot of save-excursions. Note that re-search-forward
993 ;;; moves point even if comment-start-skip is inside a string-constant.
994 ;;; Some code expects certain values for match-beginning and end
996 (let ((save-match-beginning) (save-match-end))
998 (re-search-forward comment-start-skip
999 (save-excursion (end-of-line) (point)) t
))
1001 (setq save-match-beginning
(match-beginning 0))
1002 (setq save-match-end
(match-end 0))
1003 (if (fortran-is-in-string-p (match-beginning 0))
1006 (goto-char save-match-end
)
1007 (fortran-find-comment-start-skip)) ; recurse for rest of line
1009 (goto-char save-match-beginning
)
1010 (re-search-forward comment-start-skip
1011 (save-excursion (end-of-line) (point)) t
)
1012 (goto-char (match-end 0))
1016 (defun fortran-is-in-string-p (pos)
1017 "Return t if POS (a buffer position) is inside a standard Fortran string.
1018 Fortran strings are delimeted by apostrophes (\'). Quote-Escape-sequences
1019 (\\'), strings delimited by \" and detection of syntax-errors
1020 (unbalanced quotes) are NOT supported."
1021 ;;; The algorithm is simple: start at point with value nil
1022 ;;; and toggle value at each quote found until end of line.
1023 ;;; The quote skip is hard-coded, maybe it's possible to change this
1024 ;;; and use something like 'string-constant-delimiter' (which
1025 ;;; doesn't exist yet) so this function can be used by other modes,
1026 ;;; but then one must pay attention to escape sequences, multi-line-constants
1027 ;;; and such things.
1028 (let ((is-in-fortran-string nil
))
1031 (if (not (fortran-previous-statement))
1032 (fortran-next-statement))
1033 (while (< (point) pos
)
1034 ;; Make sure we don't count quotes in continuation column.
1035 (if (looking-at "^ ")
1036 (goto-char (+ 1 (match-end 0)))
1037 (if (and (not is-in-fortran-string
)
1038 (looking-at comment-start-skip
))
1039 (beginning-of-line 2)
1040 (if (looking-at "'")
1041 (setq is-in-fortran-string
(not is-in-fortran-string
)))
1042 (forward-char 1)))))
1043 is-in-fortran-string
))
1045 (defun fortran-auto-fill-mode (arg)
1046 "Toggle fortran-auto-fill mode.
1047 With ARG, turn `fortran-auto-fill' mode on iff ARG is positive.
1048 In `fortran-auto-fill' mode, inserting a space at a column beyond `fill-column'
1049 automatically breaks the line at a previous space."
1051 (prog1 (setq auto-fill-function
1053 (not auto-fill-function
)
1054 (> (prefix-numeric-value arg
) 0))
1055 'fortran-indent-line
1058 (set-buffer-modified-p (buffer-modified-p))))
1060 (defun fortran-do-auto-fill ()
1062 (let* ((opoint (point))
1063 (bol (save-excursion (beginning-of-line) (point)))
1064 (eol (save-excursion (end-of-line) (point)))
1065 (bos (min eol
(+ bol
(fortran-current-line-indentation))))
1069 (if (looking-at comment-line-start-skip
)
1070 nil
; OK to break quotes on comment lines.
1071 (move-to-column fill-column
)
1072 (cond ((fortran-is-in-string-p (point))
1073 (save-excursion (re-search-backward "[^']'[^']" bol t
)
1074 (if fortran-break-before-delimiters
1079 ;; decide where to split the line. If a position for a quoted
1080 ;; string was found above then use that, else break the line
1081 ;; before the last delimiter.
1082 ;; Delimeters are whitespace, commas, and operators.
1083 ;; Will break before a pair of *'s.
1088 (move-to-column (1+ fill-column
))
1089 (skip-chars-backward "^ \t\n,'+-/*=)"
1090 ;;; (if fortran-break-before-delimiters
1091 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1093 (if (<= (point) (1+ bos
))
1095 (move-to-column (1+ fill-column
))
1096 (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t
))
1099 (re-search-forward "[ \t]" opoint t
)
1101 (if (looking-at "'")
1103 (skip-chars-backward " \t\*")))
1104 (if fortran-break-before-delimiters
1108 ;; if we are in an in-line comment, don't break unless the
1109 ;; line of code is longer than it should be. Otherwise
1110 ;; break the line at the column computed above.
1112 ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
1113 ;; don't prevent a break.
1114 (if (not (or (save-excursion
1115 (if (and (re-search-backward comment-start-skip bol t
)
1116 (not (fortran-is-in-string-p (point))))
1118 (skip-chars-backward " \t")
1119 (< (current-column) (1+ fill-column
)))))
1121 (goto-char fill-point
)
1123 (if (> (save-excursion
1124 (goto-char fill-point
) (current-column))
1126 (progn (goto-char fill-point
)
1127 (fortran-break-line))
1129 (if (> (save-excursion
1130 (goto-char fill-point
)
1132 (+ (calculate-fortran-indent) fortran-continuation-indent
))
1134 (goto-char fill-point
)
1135 (fortran-break-line))))))
1137 (defun fortran-break-line ()
1138 (let ((opoint (point))
1139 (bol (save-excursion (beginning-of-line) (point)))
1140 (eol (save-excursion (end-of-line) (point)))
1141 (comment-string nil
))
1144 (if (and comment-start-skip
(fortran-find-comment-start-skip))
1146 (re-search-backward comment-start-skip bol t
)
1147 (setq comment-string
(buffer-substring (point) eol
))
1148 (delete-region (point) eol
))))
1149 ;;; Forward line 1 really needs to go to next non white line
1150 (if (save-excursion (forward-line 1)
1151 (or (looking-at " [^ 0\n]")
1152 (looking-at "\t[1-9]")))
1155 (delete-indentation)
1157 (delete-horizontal-space)
1158 (fortran-do-auto-fill))
1159 (fortran-split-line))
1164 (delete-horizontal-space)
1165 (indent-to (fortran-comment-hook))
1166 (insert comment-string
)))))
1168 (defun fortran-analyze-file-format ()
1169 "Returns nil if Fixed format is used, t if TAB formatting is used.
1170 Use `fortran-tab-mode-default' if no non-comment statements are found in the
1171 file before the end or the first `fortran-analyze-depth' lines."
1174 (goto-char (point-min))
1180 (> i fortran-analyze-depth
)))
1184 ((looking-at "\t") t
)
1185 ((looking-at " ") nil
)
1186 (fortran-tab-mode-default t
)
1189 (or (assq 'fortran-tab-mode-string minor-mode-alist
)
1190 (setq minor-mode-alist
(cons
1191 '(fortran-tab-mode-string
1192 (indent-tabs-mode fortran-tab-mode-string
))
1197 ;;; fortran.el ends here