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