JimB's changes since January 18th
[bpt/emacs.git] / lisp / progmodes / fortran.el
CommitLineData
e5167999
ER
1;;; fortran.el --- Fortran mode for GNU Emacs
2
23029d77 3;;; Copyright (c) 1986, 1992 Free Software Foundation, Inc.
9750e079 4
e5167999
ER
5;; Author: Michael D. Prange <prange@erl.mit.edu>
6;; Maintainer: bug-fortran-mode@erl.mit.edu
23029d77 7;; Version 1.30 (November 18, 1992)
fd7fa35a 8;; Keywords: languages
1a06eabd 9
e5167999
ER
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to
24;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26;;; Commentary:
27
23029d77
JB
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.
33
34;; We acknowledge many contributions and valuable suggestions by
b8cbdf43
RS
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.
38
23029d77
JB
39;;; This file may be used with GNU Emacs version 18.xx if the following
40;;; variable and function substutions are made.
41;;; Replace:
dbc4e1c1 42;;; unread-command-events with unread-command-char
23029d77
JB
43;;; frame-width with screen-width
44;;; auto-fill-function with auto-fill-hook
3dd63760
JB
45
46;;; Bugs to bug-fortran-mode@erl.mit.edu
47
23029d77 48(defconst fortran-mode-version "version 1.30")
b8cbdf43 49
e5167999
ER
50;;; Code:
51
3dd63760
JB
52;;;###autoload
53(defvar fortran-tab-mode-default nil
e80f2147
RS
54 "*Default tabbing/carriage control style for empty files in Fortran mode.
55A value of t specifies tab-digit style of continuation control.
56A value of nil specifies that continuation lines are marked
57with a character in column 6.")
58
e80f2147 59;; Buffer local, used to display mode line.
23029d77
JB
60(defvar fortran-tab-mode-string nil
61 "String to appear in mode line when TAB-format mode is on.")
3dd63760
JB
62
63(defvar fortran-do-indent 3
b8cbdf43 64 "*Extra indentation applied to DO blocks.")
3dd63760
JB
65
66(defvar fortran-if-indent 3
b8cbdf43
RS
67 "*Extra indentation applied to IF blocks.")
68
69(defvar fortran-structure-indent 3
70 "*Extra indentation applied to STRUCTURE, UNION and MAP blocks.")
3dd63760
JB
71
72(defvar fortran-continuation-indent 5
b8cbdf43 73 "*Extra indentation applied to Fortran continuation lines.")
3dd63760
JB
74
75(defvar fortran-comment-indent-style 'fixed
76 "*nil forces comment lines not to be touched,
23029d77
JB
77'fixed makes fixed comment indentation to `fortran-comment-line-extra-indent'
78columns 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
81Fortran indentation plus `fortran-comment-line-extra-indent'.")
3dd63760 82
23029d77 83(defvar fortran-comment-line-extra-indent 0
b8cbdf43 84 "*Amount of extra indentation for text within full-line comments.")
3dd63760
JB
85
86(defvar comment-line-start nil
87 "*Delimiter inserted to start new full-line comment.")
88
89(defvar comment-line-start-skip nil
90 "*Regexp to match the start of a full-line comment.")
91
23029d77
JB
92(defvar fortran-minimum-statement-indent-fixed 6
93 "*Minimum statement indentation for fixed format continuation style.")
94
95(defvar fortran-minimum-statement-indent-tab (max tab-width 6)
96 "*Minimum statement indentation for TAB format continuation style.")
3dd63760
JB
97
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.
103Normally a space.")
104
105(defvar fortran-line-number-indent 1
106 "*Maximum indentation for Fortran line numbers.
1075 means right-justify them within their five-column field.")
108
109(defvar fortran-check-all-num-for-matching-do nil
b8cbdf43 110 "*Non-nil causes all numbered lines to be treated as possible DO loop ends.")
3dd63760
JB
111
112(defvar fortran-blink-matching-if nil
b8cbdf43 113 "*From a Fortran ENDIF statement, blink the matching IF statement.")
3dd63760
JB
114
115(defvar fortran-continuation-string "$"
b8cbdf43 116 "*Single-character string used for Fortran continuation lines.
3dd63760
JB
117In fixed format continuation style, this character is inserted in
118column 6 by \\[fortran-split-line] to begin a continuation line.
119Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
120convert the line into a continuation line of the appropriate style.
121Normally $.")
122
123(defvar fortran-comment-region "c$$$"
124 "*String inserted by \\[fortran-comment-region]\
125 at start of each line in region.")
126
127(defvar fortran-electric-line-number t
128 "*Non-nil causes line number digits to be moved to the correct column as\
129 typed.")
130
131(defvar fortran-startup-message t
132 "*Non-nil displays a startup message when Fortran mode is first called.")
133
23029d77
JB
134(defvar fortran-column-ruler-fixed
135 "0 4 6 10 20 30 40 5\
1360 60 70\n\
137[ ]|{ | | | | | | | | \
138| | | | |}\n"
139 "*String displayed above current line by \\[fortran-column-ruler].
140This variable used in fixed-format mode.")
141
142(defvar fortran-column-ruler-tab
143 "0 810 20 30 40 5\
1440 60 70\n\
145[ ]| { | | | | | | | | \
146| | | | |}\n"
147 "*String displayed above current line by \\[fortran-column-ruler].
148This variable used in TAB-format mode.")
3dd63760
JB
149
150(defconst bug-fortran-mode "bug-fortran-mode@erl.mit.edu"
151 "Address of mailing list for Fortran mode bugs.")
152
3dd63760
JB
153(defvar fortran-mode-syntax-table nil
154 "Syntax table in use in Fortran mode buffers.")
155
156(defvar fortran-analyze-depth 100
23029d77 157 "Number of lines to scan to determine whether to use fixed or TAB format\
3dd63760
JB
158 style.")
159
b8cbdf43
RS
160(defvar fortran-break-before-delimiters t
161 "*Non-nil causes `fortran-do-auto-fill' to break lines before delimeters.")
162
3dd63760
JB
163(if fortran-mode-syntax-table
164 ()
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)
b8cbdf43 177 (modify-syntax-entry ?_ "w" fortran-mode-syntax-table)
3dd63760
JB
178 (modify-syntax-entry ?\n ">" fortran-mode-syntax-table))
179
180(defvar fortran-mode-map ()
b8cbdf43 181 "Keymap used in Fortran mode.")
3dd63760
JB
182(if fortran-mode-map
183 ()
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))
209\f
210(defvar fortran-mode-abbrev-table nil)
211(if fortran-mode-abbrev-table
212 ()
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)
b8cbdf43 217 (define-abbrev fortran-mode-abbrev-table ";bd" "block data" nil)
3dd63760
JB
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)
b8cbdf43 234 (define-abbrev fortran-mode-abbrev-table ";ew" "endwhere" nil)
3dd63760
JB
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)
b8cbdf43 272 (define-abbrev fortran-mode-abbrev-table ";wh" "where" nil)
3dd63760
JB
273 (setq abbrevs-changed ac)))
274\f
275;;;###autoload
276(defun fortran-mode ()
b8cbdf43
RS
277 "Major mode for editing Fortran code.
278\\[fortran-indent-line] indents the current Fortran line correctly.
279DO statements must not share a common CONTINUE.
3dd63760 280
b8cbdf43 281Type ;? or ;\\[help-command] to display a list of built-in\
3dd63760
JB
282 abbrevs for Fortran keywords.
283
284Key definitions:
285\\{fortran-mode-map}
286
287Variables controlling indentation style and extra features:
288
289 comment-start
290 Normally nil in Fortran mode. If you want to use comments
291 starting with `!', set this to the string \"!\".
292 fortran-do-indent
293 Extra indentation within do blocks. (default 3)
294 fortran-if-indent
295 Extra indentation within if blocks. (default 3)
b8cbdf43
RS
296 fortran-structure-indent
297 Extra indentation within structure, union and map blocks. (default 3)
3dd63760 298 fortran-continuation-indent
b8cbdf43 299 Extra indentation applied to continuation statements. (default 5)
23029d77 300 fortran-comment-line-extra-indent
b8cbdf43 301 Amount of extra indentation for text within full-line comments. (default 0)
3dd63760
JB
302 fortran-comment-indent-style
303 nil means don't change indentation of text in full-line comments,
23029d77
JB
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
3dd63760
JB
309 indentation for a line of code.
310 (default 'fixed)
311 fortran-comment-indent-char
b8cbdf43 312 Single-character string to be inserted instead of space for
3dd63760 313 full-line comment indentation. (default \" \")
23029d77
JB
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)
3dd63760
JB
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
b8cbdf43 323 Non-nil causes all numbered lines to be treated as possible \"continue\"
3dd63760
JB
324 statements. (default nil)
325 fortran-blink-matching-if
b8cbdf43 326 From a Fortran ENDIF statement, blink the matching IF statement.
3dd63760
JB
327 (default nil)
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)
b8cbdf43
RS
337 fortran-break-before-delimiters
338 Non-nil causes `fortran-do-auto-fill' breaks lines before delimeters.
339 (default t)
3dd63760
JB
340 fortran-startup-message
341 Set to nil to inhibit message first time Fortran mode is used.
342
b8cbdf43 343Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
3dd63760
JB
344with no args, if that value is non-nil."
345 (interactive)
346 (kill-all-local-variables)
347 (if fortran-startup-message
b8cbdf43 348 (message "Emacs Fortran mode %s. Bugs to %s"
3dd63760
JB
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)
b8cbdf43
RS
353 (make-local-variable 'fortran-break-before-delimiters)
354 (setq fortran-break-before-delimiters t)
3dd63760
JB
355 (make-local-variable 'indent-line-function)
356 (setq indent-line-function 'fortran-indent-line)
357 (make-local-variable 'comment-indent-hook)
358 (setq comment-indent-hook 'fortran-comment-hook)
359 (make-local-variable 'comment-line-start-skip)
360 (setq comment-line-start-skip
b8cbdf43 361 "^[Cc*]\\(\\([^ \t\n]\\)\\2\\2*\\)?[ \t]*\\|^#.*")
3dd63760
JB
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)
23029d77 374;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
b8cbdf43 375 (setq fill-column 72) ; Already local?
3dd63760
JB
376 (use-local-map fortran-mode-map)
377 (setq mode-name "Fortran")
378 (setq major-mode 'fortran-mode)
23029d77
JB
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)
3dd63760 385 (make-local-variable 'fortran-tab-mode-string)
23029d77
JB
386 (setq fortran-tab-mode-string " TAB-format")
387 (setq indent-tabs-mode (fortran-analyze-file-format))
3dd63760
JB
388 (run-hooks 'fortran-mode-hook))
389\f
390(defun fortran-comment-hook ()
391 (save-excursion
392 (skip-chars-backward " \t")
393 (max (+ 1 (current-column))
394 comment-column)))
395
396(defun fortran-indent-comment ()
397 "Align or create comment on current line.
398Existing comments of all types are recognized and aligned.
399If the line has no comment, a side-by-side comment is inserted and aligned
400if the value of comment-start is not nil.
401Otherwise, a separate-line comment is inserted, on this line
402or on a new line inserted before this line if this line is not blank."
403 (interactive)
404 (beginning-of-line)
405 ;; Recognize existing comments of either kind.
406 (cond ((looking-at comment-line-start-skip)
407 (fortran-indent-line))
23029d77 408 ((fortran-find-comment-start-skip) ; catches any inline comment and
b8cbdf43
RS
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?
3dd63760
JB
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.
425 (t
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)
b8cbdf43 434 fortran-comment-indent-char)
3dd63760
JB
435 (- (calculate-fortran-indent) (current-column))))))
436
437(defun fortran-comment-region (beg-region end-region arg)
438 "Comments every line in the region.
439Puts fortran-comment-region at the beginning of every line in the region.
440BEG-REGION and END-REGION are args which specify the region boundaries.
441With 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)))
462\f
463(defun fortran-abbrev-start ()
b8cbdf43 464 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
3dd63760
JB
465Any 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)
dbc4e1c1 472 (setq unread-command-events (list c)))))
3dd63760
JB
473
474(defun fortran-abbrev-help ()
475 "List the currently defined abbrevs in Fortran mode."
476 (interactive)
477 (message "Listing abbrev table...")
478 (require 'abbrevlist)
e80f2147 479 (display-buffer (fortran-prepare-abbrev-list-buffer))
3dd63760
JB
480 (message "Listing abbrev table...done"))
481
e80f2147
RS
482(defun fortran-prepare-abbrev-list-buffer ()
483 (save-excursion
484 (set-buffer (get-buffer-create "*Abbrevs*"))
485 (erase-buffer)
b8cbdf43 486 (insert-abbrev-table-description fortran-mode-abbrev-table t)
e80f2147
RS
487 (goto-char (point-min))
488 (set-buffer-modified-p nil)
489 (edit-abbrevs-mode))
490 (get-buffer-create "*Abbrevs*"))
491
3dd63760
JB
492(defun fortran-column-ruler ()
493 "Inserts a column ruler momentarily above current line, till next keystroke.
fe668515 494The ruler is defined by the value of `fortran-column-ruler'.
3dd63760
JB
495The key typed is executed unless it is SPC."
496 (interactive)
497 (momentary-string-display
23029d77
JB
498 (if indent-tabs-mode
499 fortran-column-ruler-tab
500 fortran-column-ruler-fixed)
501 (save-excursion
502 (beginning-of-line)
503 (if (eq (window-start (selected-window))
504 (window-point (selected-window)))
505 (progn (forward-line) (point))
506 (point)))
3dd63760
JB
507 nil "Type SPC or any command to erase ruler."))
508
509(defun fortran-window-create ()
510 "Makes the window 72 columns wide.
fe668515 511See also `fortran-window-create-momentarily'."
3dd63760
JB
512 (interactive)
513 (condition-case error
514 (progn
515 (let ((window-min-width 2))
23029d77
JB
516 (if (< (window-width) (frame-width))
517 (enlarge-window-horizontally (- (frame-width)
3dd63760
JB
518 (window-width) 1)))
519 (split-window-horizontally 73)
520 (other-window 1)
521 (switch-to-buffer " fortran-window-extra" t)
522 (select-window (previous-window))))
b8cbdf43 523 (error (message "No room for Fortran window.")
3dd63760
JB
524 'error)))
525
526(defun fortran-window-create-momentarily (&optional arg)
527 "Momentarily makes the window 72 columns wide.
528Optional ARG non-nil and non-unity disables the momentary feature.
fe668515 529See also `fortran-window-create'."
3dd63760
JB
530 (interactive "p")
531 (if (or (not arg)
532 (= arg 1))
533 (save-window-excursion
534 (if (not (equal (fortran-window-create) 'error))
535 (progn (message "Type SPC to continue editing.")
536 (let ((char (read-char)))
537 (or (equal char (string-to-char " "))
dbc4e1c1 538 (setq unread-command-events (list char)))))))
3dd63760
JB
539 (fortran-window-create)))
540
541(defun fortran-split-line ()
542 "Break line at point and insert continuation marker and alignment."
543 (interactive)
544 (delete-horizontal-space)
545 (if (save-excursion (beginning-of-line) (looking-at comment-line-start-skip))
546 (insert "\n" comment-line-start " ")
23029d77 547 (if indent-tabs-mode
3dd63760
JB
548 (progn
549 (insert "\n\t")
550 (insert-char (fortran-numerical-continuation-char) 1))
b8cbdf43
RS
551 (insert "\n " fortran-continuation-string)));Space after \n important
552 (fortran-indent-line)) ;when the cont string is C, c or *.
3dd63760
JB
553
554(defun fortran-numerical-continuation-char ()
555 "Return a digit for tab-digit style of continution lines.
556If, previous line is a tab-digit continuation line, returns that digit
557plus one. Otherwise return 1. Zero not allowed."
558 (save-excursion
559 (forward-line -1)
560 (if (looking-at "\t[1-9]")
561 (+ ?1 (% (- (char-after (+ (point) 1)) ?0) 9))
562 ?1)))
563
564(defun delete-horizontal-regexp (chars)
565 "Delete all characters in CHARS around point.
566CHARS is like the inside of a [...] in a regular expression
567except that ] is never special and \ quotes ^, - or \."
568 (interactive "*s")
569 (skip-chars-backward chars)
570 (delete-region (point) (progn (skip-chars-forward chars) (point))))
571
572(defun fortran-electric-line-number (arg)
573 "Self insert, but if part of a Fortran line number indent it automatically.
574Auto-indent does not happen if a numeric arg is used."
3dd63760
JB
575 (interactive "P")
576 (if (or arg (not fortran-electric-line-number))
577 (if arg
b8cbdf43 578 (self-insert-command (prefix-numeric-value arg))
3dd63760
JB
579 (self-insert-command 1))
580 (if (or (and (= 5 (current-column))
581 (save-excursion
582 (beginning-of-line)
583 (looking-at " ")));In col 5 with only spaces to left.
23029d77
JB
584 (and (= (if indent-tabs-mode
585 fortran-minimum-statement-indent-tab
586 fortran-minimum-statement-indent-fixed) (current-column))
3dd63760
JB
587 (save-excursion
588 (beginning-of-line)
589 (looking-at "\t"));In col 8 with a single tab to the left.
590 (not (or (eq last-command 'fortran-indent-line)
591 (eq last-command
592 'fortran-reindent-then-newline-and-indent))))
593 (save-excursion
594 (re-search-backward "[^ \t0-9]"
595 (save-excursion
596 (beginning-of-line)
597 (point))
598 t)) ;not a line number
599 (looking-at "[0-9]") ;within a line number
600 )
b8cbdf43 601 (self-insert-command (prefix-numeric-value arg))
3dd63760
JB
602 (skip-chars-backward " \t")
603 (insert last-command-char)
604 (fortran-indent-line))))
605\f
606(defun beginning-of-fortran-subprogram ()
b8cbdf43 607 "Moves point to the beginning of the current Fortran subprogram."
3dd63760
JB
608 (interactive)
609 (let ((case-fold-search t))
610 (beginning-of-line -1)
611 (re-search-backward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
612 (if (looking-at "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")
613 (forward-line 1))))
614
615(defun end-of-fortran-subprogram ()
b8cbdf43 616 "Moves point to the end of the current Fortran subprogram."
3dd63760
JB
617 (interactive)
618 (let ((case-fold-search t))
619 (beginning-of-line 2)
620 (re-search-forward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
621 (goto-char (match-beginning 0))
622 (forward-line 1)))
623
624(defun mark-fortran-subprogram ()
b8cbdf43 625 "Put mark at end of Fortran subprogram, point at beginning.
3dd63760
JB
626The marks are pushed."
627 (interactive)
628 (end-of-fortran-subprogram)
629 (push-mark (point))
630 (beginning-of-fortran-subprogram))
b8cbdf43 631
3dd63760 632(defun fortran-previous-statement ()
b8cbdf43
RS
633 "Moves point to beginning of the previous Fortran statement.
634Returns `first-statement' if that statement is the first
3dd63760
JB
635non-comment Fortran statement in the file, and nil otherwise."
636 (interactive)
637 (let (not-first-statement continue-test)
638 (beginning-of-line)
639 (setq continue-test
b8cbdf43
RS
640 (and
641 (not (looking-at comment-line-start-skip))
642 (or (looking-at
3dd63760 643 (concat "[ \t]*" (regexp-quote fortran-continuation-string)))
b8cbdf43
RS
644 (or (looking-at " [^ 0\n]")
645 (looking-at "\t[1-9]")))))
3dd63760
JB
646 (while (and (setq not-first-statement (= (forward-line -1) 0))
647 (or (looking-at comment-line-start-skip)
648 (looking-at "[ \t]*$")
649 (looking-at " [^ 0\n]")
650 (looking-at "\t[1-9]")
651 (looking-at (concat "[ \t]*" comment-start-skip)))))
652 (cond ((and continue-test
653 (not not-first-statement))
654 (message "Incomplete continuation statement."))
655 (continue-test
656 (fortran-previous-statement))
657 ((not not-first-statement)
658 'first-statement))))
659
660(defun fortran-next-statement ()
b8cbdf43 661 "Moves point to beginning of the next Fortran statement.
3dd63760
JB
662Returns `last-statement' if that statement is the last
663non-comment Fortran statement in the file, and nil otherwise."
664 (interactive)
665 (let (not-last-statement)
666 (beginning-of-line)
b8cbdf43
RS
667 (while (and (setq not-last-statement
668 (and (= (forward-line 1) 0)
669 (not (eobp))))
3dd63760
JB
670 (or (looking-at comment-line-start-skip)
671 (looking-at "[ \t]*$")
672 (looking-at " [^ 0\n]")
673 (looking-at "\t[1-9]")
674 (looking-at (concat "[ \t]*" comment-start-skip)))))
675 (if (not not-last-statement)
676 'last-statement)))
677\f
678(defun fortran-blink-matching-if ()
b8cbdf43 679 "From a Fortran ENDIF statement, blink the matching IF statement."
3dd63760
JB
680 (let ((count 1) (top-of-window (window-start)) matching-if
681 (endif-point (point)) message)
682 (if (save-excursion (beginning-of-line)
683 (skip-chars-forward " \t0-9")
684 (looking-at "end[ \t]*if\\b"))
685 (progn
686 (save-excursion
687 (while (and (not (= count 0))
688 (not (eq (fortran-previous-statement)
689 'first-statement))
690 (not (looking-at
691 "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")))
692 ; Keep local to subprogram
693 (skip-chars-forward " \t0-9")
694 (cond ((looking-at "if[ \t]*(")
b8cbdf43
RS
695 (save-excursion
696 (if (or
697 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
698 (let (then-test);multi-line if-then
699 (while
700 (and (= (forward-line 1) 0)
3dd63760 701 ;search forward for then
b8cbdf43
RS
702 (or (looking-at " [^ 0\n]")
703 (looking-at "\t[1-9]"))
704 (not
705 (setq
706 then-test
707 (looking-at
708 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
709 then-test))
710 (setq count (- count 1)))))
3dd63760
JB
711 ((looking-at "end[ \t]*if\\b")
712 (setq count (+ count 1)))))
713 (if (not (= count 0))
714 (setq message "No matching if.")
715 (if (< (point) top-of-window)
716 (setq message (concat "Matches " (buffer-substring
717 (progn (beginning-of-line)
718 (point))
719 (progn (end-of-line)
720 (point)))))
721 (setq matching-if (point)))))
722 (if message
b8cbdf43 723 (message "%s" message)
3dd63760
JB
724 (goto-char matching-if)
725 (sit-for 1)
726 (goto-char endif-point))))))
727\f
728(defun fortran-indent-line ()
b8cbdf43 729 "Indents current Fortran line based on its contents and on previous lines."
3dd63760
JB
730 (interactive)
731 (let ((cfi (calculate-fortran-indent)))
732 (save-excursion
733 (beginning-of-line)
734 (if (or (not (= cfi (fortran-current-line-indentation)))
735 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
736 (not (fortran-line-number-indented-correctly-p))))
737 (fortran-indent-to-column cfi)
738 (beginning-of-line)
739 (if (and (not (looking-at comment-line-start-skip))
23029d77 740 (fortran-find-comment-start-skip))
3dd63760
JB
741 (fortran-indent-comment))))
742 ;; Never leave point in left margin.
743 (if (< (current-column) cfi)
744 (move-to-column cfi))
23029d77 745 (if (and auto-fill-function
b8cbdf43
RS
746 (> (save-excursion (end-of-line) (current-column)) fill-column))
747 (save-excursion
748 (end-of-line)
749 (fortran-do-auto-fill)))
3dd63760
JB
750 (if fortran-blink-matching-if
751 (fortran-blink-matching-if))))
752
753(defun fortran-reindent-then-newline-and-indent ()
b8cbdf43
RS
754 "Reindent the current Fortran line, insert a newline and indent the newline.
755An abbrev before point is expanded if `abbrev-mode' is non-nil."
3dd63760
JB
756 (interactive)
757 (if abbrev-mode (expand-abbrev))
758 (save-excursion
759 (beginning-of-line)
760 (skip-chars-forward " \t")
761 (if (or (looking-at "[0-9]") ;Reindent only where it is most
762 (looking-at "end") ;likely to be necessary
763 (looking-at "else")
764 (looking-at (regexp-quote fortran-continuation-string)))
765 (fortran-indent-line)))
b8cbdf43 766 (newline)
3dd63760 767 (fortran-indent-line))
b8cbdf43 768
3dd63760
JB
769(defun fortran-indent-subprogram ()
770 "Properly indents the Fortran subprogram which contains point."
771 (interactive)
772 (save-excursion
773 (mark-fortran-subprogram)
774 (message "Indenting subprogram...")
775 (indent-region (point) (mark) nil))
776 (message "Indenting subprogram...done."))
777
778(defun calculate-fortran-indent ()
b8cbdf43 779 "Calculates the Fortran indent column based on previous lines."
3dd63760
JB
780 (let (icol first-statement (case-fold-search t)
781 (fortran-minimum-statement-indent
23029d77
JB
782 (if indent-tabs-mode
783 fortran-minimum-statement-indent-tab
784 fortran-minimum-statement-indent-fixed)))
3dd63760
JB
785 (save-excursion
786 (setq first-statement (fortran-previous-statement))
787 (if first-statement
788 (setq icol fortran-minimum-statement-indent)
789 (progn
790 (if (= (point) (point-min))
791 (setq icol fortran-minimum-statement-indent)
792 (setq icol (fortran-current-line-indentation)))
793 (skip-chars-forward " \t0-9")
794 (cond ((looking-at "if[ \t]*(")
b8cbdf43 795 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
3dd63760
JB
796 (let (then-test) ;multi-line if-then
797 (while (and (= (forward-line 1) 0)
b8cbdf43 798 ;;search forward for then
3dd63760
JB
799 (or (looking-at " [^ 0\n]")
800 (looking-at "\t[1-9]"))
801 (not (setq then-test (looking-at
b8cbdf43
RS
802 ".*then\\b[ \t]\
803*[^ \t_$(=a-z0-9]")))))
3dd63760
JB
804 then-test))
805 (setq icol (+ icol fortran-if-indent))))
806 ((looking-at "\\(else\\|elseif\\)\\b")
807 (setq icol (+ icol fortran-if-indent)))
b8cbdf43
RS
808 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
809 (setq icol (+ icol fortran-if-indent)))
810 ((looking-at "where.*(.*)[ \t]*\n")
811 (setq icol (+ icol fortran-if-indent)))
3dd63760
JB
812 ((looking-at "do\\b")
813 (setq icol (+ icol fortran-do-indent)))
b8cbdf43
RS
814 ((looking-at
815 "\\(structure\\|union\\|map\\)\\b[ \t]*[^ \t=(a-z]")
816 (setq icol (+ icol fortran-structure-indent)))
3dd63760 817 ((looking-at "end\\b[ \t]*[^ \t=(a-z]")
b8cbdf43 818 ;; Previous END resets indent to minimum
3dd63760
JB
819 (setq icol fortran-minimum-statement-indent))))))
820 (save-excursion
821 (beginning-of-line)
822 (cond ((looking-at "[ \t]*$"))
823 ((looking-at comment-line-start-skip)
3dd63760 824 (cond ((eq fortran-comment-indent-style 'relative)
23029d77 825 (setq icol (+ icol fortran-comment-line-extra-indent)))
3dd63760 826 ((eq fortran-comment-indent-style 'fixed)
b8cbdf43 827 (setq icol (+ fortran-minimum-statement-indent
23029d77 828 fortran-comment-line-extra-indent))))
b8cbdf43 829 (setq fortran-minimum-statement-indent 0))
3dd63760 830 ((or (looking-at (concat "[ \t]*"
b8cbdf43
RS
831 (regexp-quote
832 fortran-continuation-string)))
3dd63760
JB
833 (looking-at " [^ 0\n]")
834 (looking-at "\t[1-9]"))
835 (setq icol (+ icol fortran-continuation-indent)))
836 (first-statement)
837 ((and fortran-check-all-num-for-matching-do
838 (looking-at "[ \t]*[0-9]+")
839 (fortran-check-for-matching-do))
840 (setq icol (- icol fortran-do-indent)))
841 (t
842 (skip-chars-forward " \t0-9")
843 (cond ((looking-at "end[ \t]*if\\b")
844 (setq icol (- icol fortran-if-indent)))
845 ((looking-at "\\(else\\|elseif\\)\\b")
846 (setq icol (- icol fortran-if-indent)))
b8cbdf43
RS
847 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
848 (setq icol (- icol fortran-if-indent)))
849 ((looking-at "end[ \t]*where\\b")
850 (setq icol (- icol fortran-if-indent)))
3dd63760
JB
851 ((and (looking-at "continue\\b")
852 (fortran-check-for-matching-do))
853 (setq icol (- icol fortran-do-indent)))
854 ((looking-at "end[ \t]*do\\b")
855 (setq icol (- icol fortran-do-indent)))
b8cbdf43
RS
856 ((looking-at
857 "end[ \t]*\
858\\(structure\\|union\\|map\\)\\b[ \t]*[^ \t=(a-z]")
859 (setq icol (- icol fortran-structure-indent)))
3dd63760
JB
860 ((and (looking-at "end\\b[ \t]*[^ \t=(a-z]")
861 (not (= icol fortran-minimum-statement-indent)))
862 (message "Warning: `end' not in column %d. Probably\
863 an unclosed block." fortran-minimum-statement-indent))))))
864 (max fortran-minimum-statement-indent icol)))
865\f
866(defun fortran-current-line-indentation ()
867 "Indentation of current line, ignoring Fortran line number or continuation.
868This is the column position of the first non-whitespace character
869aside from the line number and/or column 5/8 line-continuation character.
870For comment lines, returns indentation of the first
871non-indentation text within the comment."
872 (save-excursion
873 (beginning-of-line)
874 (cond ((looking-at comment-line-start-skip)
875 (goto-char (match-end 0))
876 (skip-chars-forward
b8cbdf43
RS
877 (if (stringp fortran-comment-indent-char)
878 fortran-comment-indent-char
879 (char-to-string fortran-comment-indent-char))))
3dd63760 880 ((or (looking-at " [^ 0\n]")
b8cbdf43 881 (looking-at "\t[1-9]"))
3dd63760
JB
882 (goto-char (match-end 0)))
883 (t
884 ;; Move past line number.
b8cbdf43
RS
885 (skip-chars-forward "[ \t0-9]");From Uli
886 ))
3dd63760
JB
887 ;; Move past whitespace.
888 (skip-chars-forward " \t")
889 (current-column)))
890
891(defun fortran-indent-to-column (col)
892 "Indents current line with spaces to column COL.
893notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
894 line, and this continuation character is retained on indentation;
b8cbdf43
RS
895 2) If `fortran-continuation-string' is the first non-whitespace
896 character, this is a continuation line;
3dd63760
JB
897 3) A non-continuation line which has a number as the first
898 non-whitespace character is a numbered line.
b8cbdf43 899 4) A TAB followed by a digit indicates a continuation line."
3dd63760
JB
900 (save-excursion
901 (beginning-of-line)
902 (if (looking-at comment-line-start-skip)
903 (if fortran-comment-indent-style
904 (let ((char (if (stringp fortran-comment-indent-char)
905 (aref fortran-comment-indent-char 0)
b8cbdf43 906 fortran-comment-indent-char)))
3dd63760
JB
907 (goto-char (match-end 0))
908 (delete-horizontal-regexp (concat " \t" (char-to-string char)))
909 (insert-char char (- col (current-column)))))
910 (if (looking-at "\t[1-9]")
23029d77 911 (if indent-tabs-mode
3dd63760
JB
912 (goto-char (match-end 0))
913 (delete-char 2)
914 (insert " ")
915 (insert fortran-continuation-string))
916 (if (looking-at " [^ 0\n]")
23029d77 917 (if indent-tabs-mode
3dd63760
JB
918 (progn (delete-char 6)
919 (insert "\t")
920 (insert-char (fortran-numerical-continuation-char) 1))
921 (forward-char 6))
922 (delete-horizontal-space)
b8cbdf43
RS
923 ;; Put line number in columns 0-4
924 ;; or put continuation character in column 5.
3dd63760
JB
925 (cond ((eobp))
926 ((looking-at (regexp-quote fortran-continuation-string))
23029d77 927 (if indent-tabs-mode
3dd63760 928 (progn
23029d77
JB
929 (indent-to
930 (if indent-tabs-mode
931 fortran-minimum-statement-indent-tab
932 fortran-minimum-statement-indent-fixed))
3dd63760
JB
933 (delete-char 1)
934 (insert-char (fortran-numerical-continuation-char) 1))
b8cbdf43
RS
935 (indent-to 5)
936 (forward-char 1)))
3dd63760
JB
937 ((looking-at "[0-9]+")
938 (let ((extra-space (- 5 (- (match-end 0) (point)))))
939 (if (< extra-space 0)
940 (message "Warning: line number exceeds 5-digit limit.")
941 (indent-to (min fortran-line-number-indent extra-space))))
942 (skip-chars-forward "0-9")))))
943 ;; Point is now after any continuation character or line number.
944 ;; Put body of statement where specified.
945 (delete-horizontal-space)
946 (indent-to col)
947 ;; Indent any comment following code on the same line.
948 (if (and comment-start-skip
23029d77 949 (fortran-find-comment-start-skip))
3dd63760
JB
950 (progn (goto-char (match-beginning 0))
951 (if (not (= (current-column) (fortran-comment-hook)))
952 (progn (delete-horizontal-space)
953 (indent-to (fortran-comment-hook)))))))))
954
955(defun fortran-line-number-indented-correctly-p ()
956 "Return t if current line's line number is correctly indented.
957Do not call if there is no line number."
958 (save-excursion
959 (beginning-of-line)
960 (skip-chars-forward " \t")
961 (and (<= (current-column) fortran-line-number-indent)
962 (or (= (current-column) fortran-line-number-indent)
963 (progn (skip-chars-forward "0-9")
964 (= (current-column) 5))))))
965
966(defun fortran-check-for-matching-do ()
b8cbdf43 967 "When called from a numbered statement, returns t if matching DO is found.
3dd63760
JB
968Otherwise return a nil."
969 (let (charnum
970 (case-fold-search t))
971 (save-excursion
972 (beginning-of-line)
973 (if (looking-at "[ \t]*[0-9]+")
974 (progn
975 (skip-chars-forward " \t")
976 (skip-chars-forward "0") ;skip past leading zeros
977 (setq charnum (buffer-substring (point)
978 (progn (skip-chars-forward "0-9")
979 (point))))
980 (beginning-of-line)
981 (and (re-search-backward
982 (concat
b8cbdf43
RS
983 "\\(^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]\\)\\|\\(^[ \t0-9]*do\
984[ \t]*0*"
985 charnum "\\b\\)\\|\\(^[ \t]*0*" charnum "\\b\\)")
3dd63760
JB
986 nil t)
987 (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum))))))))
988
23029d77 989(defun fortran-find-comment-start-skip ()
b8cbdf43
RS
990 "Move to past `comment-start-skip' found on current line.
991Return t if `comment-start-skip' found, nil if not."
992;;; In order to move point only if comment-start-skip is found,
993;;; this one uses a lot of save-excursions. Note that re-search-forward
994;;; moves point even if comment-start-skip is inside a string-constant.
995;;; Some code expects certain values for match-beginning and end
996 (interactive)
997 (let ((save-match-beginning) (save-match-end))
998 (if (save-excursion
999 (re-search-forward comment-start-skip
1000 (save-excursion (end-of-line) (point)) t))
1001 (progn
1002 (setq save-match-beginning (match-beginning 0))
1003 (setq save-match-end (match-end 0))
23029d77 1004 (if (fortran-is-in-string-p (match-beginning 0))
b8cbdf43
RS
1005 (progn
1006 (save-excursion
1007 (goto-char save-match-end)
23029d77 1008 (fortran-find-comment-start-skip)) ; recurse for rest of line
b8cbdf43
RS
1009 )
1010 (goto-char save-match-beginning)
1011 (re-search-forward comment-start-skip
1012 (save-excursion (end-of-line) (point)) t)
1013 (goto-char (match-end 0))
1014 t))
1015 nil)))
1016
23029d77 1017(defun fortran-is-in-string-p (pos)
b8cbdf43
RS
1018 "Return t if POS (a buffer position) is inside a standard Fortran string.
1019Fortran strings are delimeted by apostrophes (\'). Quote-Escape-sequences
1020(\\'), strings delimited by \" and detection of syntax-errors
1021(unbalanced quotes) are NOT supported."
1022;;; The algorithm is simple: start at point with value nil
1023;;; and toggle value at each quote found until end of line.
1024;;; The quote skip is hard-coded, maybe it's possible to change this
1025;;; and use something like 'string-constant-delimiter' (which
1026;;; doesn't exist yet) so this function can be used by other modes,
1027;;; but then one must pay attention to escape sequences, multi-line-constants
1028;;; and such things.
1029 (let ((is-in-fortran-string nil))
1030 (save-excursion
1031 (goto-char pos)
1032 (fortran-previous-statement)
1033 (fortran-next-statement)
1034 (while (< (point) pos)
1035 ;; Make sure we don't count quotes in continuation column.
1036 (if (looking-at "^ ")
1037 (goto-char (+ 1 (match-end 0)))
1038 (if (and (not is-in-fortran-string)
1039 (looking-at comment-start-skip))
1040 (beginning-of-line 2)
1041 (if (looking-at "'")
1042 (setq is-in-fortran-string (not is-in-fortran-string)))
1043 (forward-char 1)))))
1044 is-in-fortran-string))
1045
1046(defun fortran-auto-fill-mode (arg)
1047 "Toggle fortran-auto-fill mode.
1048With ARG, turn `fortran-auto-fill' mode on iff ARG is positive.
1049In `fortran-auto-fill' mode, inserting a space at a column beyond `fill-column'
1050automatically breaks the line at a previous space."
1051 (interactive "P")
23029d77 1052 (prog1 (setq auto-fill-function
b8cbdf43 1053 (if (if (null arg)
23029d77 1054 (not auto-fill-function)
b8cbdf43
RS
1055 (> (prefix-numeric-value arg) 0))
1056 'fortran-indent-line
1057 nil))
1058 ;; update mode-line
1059 (set-buffer-modified-p (buffer-modified-p))))
1060
1061(defun fortran-do-auto-fill ()
1062 (interactive)
1063 (let* ((opoint (point))
1064 (bol (save-excursion (beginning-of-line) (point)))
1065 (eol (save-excursion (end-of-line) (point)))
1066 (bos (min eol (+ bol (fortran-current-line-indentation))))
1067 (quote
1068 (save-excursion
1069 (goto-char bol)
1070 (if (looking-at comment-line-start-skip)
1071 nil ; OK to break quotes on comment lines.
1072 (move-to-column fill-column)
23029d77 1073 (cond ((fortran-is-in-string-p (point))
b8cbdf43
RS
1074 (save-excursion (re-search-backward "[^']'[^']" bol t)
1075 (if fortran-break-before-delimiters
1076 (point)
1077 (1+ (point)))))
1078 (t nil)))))
1079 ;;
1080 ;; decide where to split the line. If a position for a quoted
1081 ;; string was found above then use that, else break the line
1082 ;; before the last delimiter.
1083 ;; Delimeters are whitespace, commas, and operators.
1084 ;; Will break before a pair of *'s.
1085 ;;
1086 (fill-point
1087 (or quote
1088 (save-excursion
1089 (move-to-column (1+ fill-column))
1090 (skip-chars-backward "^ \t\n,'+-/*=)"
1091;;; (if fortran-break-before-delimiters
1092;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1093 )
1094 (if (<= (point) (1+ bos))
1095 (progn
1096 (move-to-column (1+ fill-column))
1097 (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t))
1098 (goto-char bol))))
1099 (if (bolp)
1100 (re-search-forward "[ \t]" opoint t)
1101 (forward-char -1)
1102 (if (looking-at "'")
1103 (forward-char 1)
1104 (skip-chars-backward " \t\*")))
1105 (if fortran-break-before-delimiters
1106 (point)
1107 (1+ (point))))))
1108 )
1109 ;; if we are in an in-line comment, don't break unless the
1110 ;; line of code is longer than it should be. Otherwise
1111 ;; break the line at the column computed above.
1112 ;;
23029d77 1113 ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
b8cbdf43
RS
1114 ;; don't prevent a break.
1115 (if (not (or (save-excursion
1116 (if (and (re-search-backward comment-start-skip bol t)
23029d77 1117 (not (fortran-is-in-string-p (point))))
b8cbdf43
RS
1118 (progn
1119 (skip-chars-backward " \t")
1120 (< (current-column) (1+ fill-column)))))
1121 (save-excursion
1122 (goto-char fill-point)
1123 (bolp))))
1124 (if (> (save-excursion
1125 (goto-char fill-point) (current-column))
1126 (1+ fill-column))
1127 (progn (goto-char fill-point)
1128 (fortran-break-line))
1129 (save-excursion
1130 (if (> (save-excursion
1131 (goto-char fill-point)
1132 (current-column))
1133 (+ (calculate-fortran-indent) fortran-continuation-indent))
1134 (progn
1135 (goto-char fill-point)
1136 (fortran-break-line))))))
1137 ))
1138(defun fortran-break-line ()
1139 (let ((opoint (point))
1140 (bol (save-excursion (beginning-of-line) (point)))
1141 (eol (save-excursion (end-of-line) (point)))
1142 (comment-string nil))
1143
1144 (save-excursion
23029d77 1145 (if (and comment-start-skip (fortran-find-comment-start-skip))
b8cbdf43
RS
1146 (progn
1147 (re-search-backward comment-start-skip bol t)
1148 (setq comment-string (buffer-substring (point) eol))
1149 (delete-region (point) eol))))
1150;;; Forward line 1 really needs to go to next non white line
1151 (if (save-excursion (forward-line 1)
1152 (or (looking-at " [^ 0\n]")
1153 (looking-at "\t[1-9]")))
1154 (progn
1155 (forward-line 1)
1156 (delete-indentation)
1157 (delete-char 2)
1158 (delete-horizontal-space)
1159 (fortran-do-auto-fill))
1160 (fortran-split-line))
1161 (if comment-string
1162 (save-excursion
1163 (goto-char bol)
1164 (end-of-line)
1165 (delete-horizontal-space)
1166 (indent-to (fortran-comment-hook))
1167 (insert comment-string)))))
1168
23029d77
JB
1169(defun fortran-analyze-file-format ()
1170 "Returns nil if Fixed format is used, t if TAB formatting is used.
1171Use `fortran-tab-mode-default' if no non-comment statements are found in the
1172file before the end or the first `fortran-analyze-depth' lines."
1173 (let ((i 0))
1174 (save-excursion
1175 (goto-char (point-min))
1176 (setq i 0)
1177 (while (not (or
1178 (eobp)
1179 (looking-at "\t")
1180 (looking-at " ")
1181 (> i fortran-analyze-depth)))
1182 (forward-line)
1183 (setq i (1+ i)))
1184 (cond
1185 ((looking-at "\t") t)
1186 ((looking-at " ") nil)
1187 (fortran-tab-mode-default t)
1188 (t nil)))))
1189
1190(or (assq 'fortran-tab-mode-string minor-mode-alist)
1191 (setq minor-mode-alist (cons
1192 '(fortran-tab-mode-string
1193 (indent-tabs-mode fortran-tab-mode-string))
1194 minor-mode-alist)))
1195
49116ac0
JB
1196(provide 'fortran)
1197
1a06eabd 1198;;; fortran.el ends here