Commit | Line | Data |
---|---|---|
e5167999 ER |
1 | ;;; fortran.el --- Fortran mode for GNU Emacs |
2 | ||
8cb8832f | 3 | ;; Copyright (C) 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001, |
034babe1 | 4 | ;; 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
9750e079 | 5 | |
e5167999 | 6 | ;; Author: Michael D. Prange <prange@erl.mit.edu> |
367e10df | 7 | ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk> |
5b04210c | 8 | ;; Keywords: fortran, 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 | |
b578f267 | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3a35cf56 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
e5167999 ER |
26 | |
27 | ;;; Commentary: | |
28 | ||
7977773d DL |
29 | ;; This mode is documented in the Emacs manual. |
30 | ;; | |
31 | ;; Note that it is for editing Fortran77 or Fortran90 fixed source | |
45cf60ae | 32 | ;; form. For editing Fortran 90 free format source, use `f90-mode' |
b2523604 DL |
33 | ;; (f90.el). It is meant to support the GNU Fortran language |
34 | ;; implemented by g77 (its extensions to Fortran77 and | |
35 | ;; interpretations, e.g. of blackslash in strings). | |
7977773d DL |
36 | |
37 | ;;; History: | |
38 | ||
39 | ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov). | |
23029d77 JB |
40 | |
41 | ;; We acknowledge many contributions and valuable suggestions by | |
b8cbdf43 | 42 | ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea, |
60db3594 | 43 | ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon, |
b8cbdf43 RS |
44 | ;; Gary Sabot and Richard Stallman. |
45 | ||
e5167999 ER |
46 | ;;; Code: |
47 | ||
45cf60ae | 48 | ;; Todo: |
1eb6bf70 | 49 | |
315aa1de | 50 | ;; * Tidy it all up (more)! |
1eb6bf70 DL |
51 | ;; * Implement insertion and removal of statement continuations in |
52 | ;; mixed f77/f90 style, with the first `&' past column 72 and the | |
53 | ;; second in column 6. | |
b2523604 | 54 | ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed. |
1eb6bf70 | 55 | |
5b04210c | 56 | (eval-when-compile ; silence compiler |
be35ca9f | 57 | (defvar dabbrev-case-fold-search) |
5b04210c GM |
58 | (defvar imenu-case-fold-search) |
59 | (defvar imenu-syntax-alist)) | |
60 | ||
61 | ||
fcad5199 | 62 | (defgroup fortran nil |
5b04210c GM |
63 | "Major mode for editing fixed format Fortran code." |
64 | :link '(custom-manual "(emacs)Fortran") | |
fcad5199 RS |
65 | :group 'languages) |
66 | ||
67 | (defgroup fortran-indent nil | |
5b04210c | 68 | "Indentation variables in Fortran mode." |
fcad5199 | 69 | :prefix "fortran-" |
5b04210c | 70 | :group 'fortran) |
fcad5199 RS |
71 | |
72 | (defgroup fortran-comment nil | |
5b04210c | 73 | "Comment-handling variables in Fortran mode." |
fcad5199 | 74 | :prefix "fortran-" |
5b04210c | 75 | :group 'fortran) |
fcad5199 RS |
76 | |
77 | ||
3dd63760 | 78 | ;;;###autoload |
fcad5199 | 79 | (defcustom fortran-tab-mode-default nil |
e80f2147 | 80 | "*Default tabbing/carriage control style for empty files in Fortran mode. |
5b04210c | 81 | A non-nil value specifies tab-digit style of continuation control. |
e80f2147 | 82 | A value of nil specifies that continuation lines are marked |
fcad5199 | 83 | with a character in column 6." |
5b04210c | 84 | :type 'boolean |
fcad5199 | 85 | :group 'fortran-indent) |
e80f2147 | 86 | |
a7113309 GM |
87 | (defcustom fortran-tab-mode-string "/t" |
88 | "*String to appear in mode line in TAB format buffers." | |
5b04210c | 89 | :type 'string |
fcad5199 | 90 | :group 'fortran-indent) |
5b04210c | 91 | |
fcad5199 RS |
92 | (defcustom fortran-do-indent 3 |
93 | "*Extra indentation applied to DO blocks." | |
5b04210c | 94 | :type 'integer |
fcad5199 RS |
95 | :group 'fortran-indent) |
96 | ||
97 | (defcustom fortran-if-indent 3 | |
8cb8832f | 98 | "*Extra indentation applied to IF, SELECT CASE and WHERE blocks." |
5b04210c | 99 | :type 'integer |
fcad5199 RS |
100 | :group 'fortran-indent) |
101 | ||
102 | (defcustom fortran-structure-indent 3 | |
103 | "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks." | |
5b04210c | 104 | :type 'integer |
fcad5199 RS |
105 | :group 'fortran-indent) |
106 | ||
107 | (defcustom fortran-continuation-indent 5 | |
5b04210c GM |
108 | "*Extra indentation applied to continuation lines." |
109 | :type 'integer | |
fcad5199 RS |
110 | :group 'fortran-indent) |
111 | ||
112 | (defcustom fortran-comment-indent-style 'fixed | |
7977773d | 113 | "*How to indent comments. |
5b04210c GM |
114 | nil forces comment lines not to be touched; |
115 | `fixed' indents to `fortran-comment-line-extra-indent' columns beyond | |
116 | `fortran-minimum-statement-indent-fixed' (if `indent-tabs-mode' nil), or | |
117 | `fortran-minimum-statement-indent-tab' (if `indent-tabs-mode' non-nil); | |
ff451e17 SM |
118 | `relative' indents to current Fortran indentation plus |
119 | `fortran-comment-line-extra-indent'." | |
5b04210c | 120 | :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative)) |
fcad5199 RS |
121 | :group 'fortran-indent) |
122 | ||
123 | (defcustom fortran-comment-line-extra-indent 0 | |
124 | "*Amount of extra indentation for text within full-line comments." | |
5b04210c | 125 | :type 'integer |
fcad5199 RS |
126 | :group 'fortran-indent |
127 | :group 'fortran-comment) | |
128 | ||
68ca306c DL |
129 | (defcustom fortran-comment-line-start "C" |
130 | "*Delimiter inserted to start new full-line comment. | |
131 | You might want to change this to \"*\", for instance." | |
7dae727d | 132 | :version "21.1" |
5b04210c GM |
133 | :type 'string |
134 | :group 'fortran-comment) | |
fcad5199 | 135 | |
68ca306c DL |
136 | ;; This used to match preprocessor lines too, but that messes up |
137 | ;; filling and doesn't seem to be necessary. | |
138 | (defcustom fortran-comment-line-start-skip | |
8b31236d | 139 | "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*" |
fcad5199 | 140 | "*Regexp to match the start of a full-line comment." |
7dae727d | 141 | :version "21.1" |
5b04210c GM |
142 | :type 'regexp |
143 | :group 'fortran-comment) | |
fcad5199 | 144 | |
5a73972b | 145 | (defcustom fortran-directive-re |
8edfcc7d | 146 | "^[ \t]*#.*" |
5a73972b GM |
147 | "*Regexp to match a directive line. |
148 | The matching text will be fontified with `font-lock-keyword-face'. | |
149 | The matching line will be given zero indentation." | |
bf247b6e | 150 | :version "22.1" |
5b04210c GM |
151 | :type 'regexp |
152 | :group 'fortran-indent) | |
8edfcc7d | 153 | |
fcad5199 RS |
154 | (defcustom fortran-minimum-statement-indent-fixed 6 |
155 | "*Minimum statement indentation for fixed format continuation style." | |
5b04210c | 156 | :type 'integer |
fcad5199 RS |
157 | :group 'fortran-indent) |
158 | ||
159 | (defcustom fortran-minimum-statement-indent-tab (max tab-width 6) | |
160 | "*Minimum statement indentation for TAB format continuation style." | |
5b04210c | 161 | :type 'integer |
fcad5199 | 162 | :group 'fortran-indent) |
3dd63760 JB |
163 | |
164 | ;; Note that this is documented in the v18 manuals as being a string | |
165 | ;; of length one rather than a single character. | |
166 | ;; The code in this file accepts either format for compatibility. | |
fcad5199 | 167 | (defcustom fortran-comment-indent-char " " |
3dd63760 | 168 | "*Single-character string inserted for Fortran comment indentation. |
fcad5199 | 169 | Normally a space." |
5b04210c | 170 | :type 'string |
fcad5199 | 171 | :group 'fortran-comment) |
3dd63760 | 172 | |
fcad5199 | 173 | (defcustom fortran-line-number-indent 1 |
3dd63760 | 174 | "*Maximum indentation for Fortran line numbers. |
fcad5199 | 175 | 5 means right-justify them within their five-column field." |
5b04210c | 176 | :type 'integer |
fcad5199 | 177 | :group 'fortran-indent) |
3dd63760 | 178 | |
fcad5199 RS |
179 | (defcustom fortran-check-all-num-for-matching-do nil |
180 | "*Non-nil causes all numbered lines to be treated as possible DO loop ends." | |
5b04210c | 181 | :type 'boolean |
fcad5199 | 182 | :group 'fortran) |
3dd63760 | 183 | |
fcad5199 | 184 | (defcustom fortran-blink-matching-if nil |
5b04210c | 185 | "*Non-nil causes \\[fortran-indent-line] on ENDIF to blink on matching IF. |
fcad5199 | 186 | Also, from an ENDDO statement blink on matching DO [WHILE] statement." |
5b04210c | 187 | :type 'boolean |
fcad5199 | 188 | :group 'fortran) |
3dd63760 | 189 | |
fcad5199 | 190 | (defcustom fortran-continuation-string "$" |
b8cbdf43 | 191 | "*Single-character string used for Fortran continuation lines. |
3dd63760 JB |
192 | In fixed format continuation style, this character is inserted in |
193 | column 6 by \\[fortran-split-line] to begin a continuation line. | |
5b04210c GM |
194 | Also, if \\[fortran-indent-line] finds this at the beginning of a |
195 | line, it will convert the line into a continuation line of the | |
196 | appropriate style. Normally $." | |
197 | :type 'string | |
fcad5199 | 198 | :group 'fortran) |
3dd63760 | 199 | |
fcad5199 | 200 | (defcustom fortran-comment-region "c$$$" |
1eb6bf70 DL |
201 | "*String inserted by \\[fortran-comment-region] at start of each \ |
202 | line in region." | |
5b04210c | 203 | :type 'string |
fcad5199 | 204 | :group 'fortran-comment) |
3dd63760 | 205 | |
fcad5199 | 206 | (defcustom fortran-electric-line-number t |
5b04210c GM |
207 | "*Non-nil causes line numbers to be moved to the correct column as typed." |
208 | :type 'boolean | |
fcad5199 | 209 | :group 'fortran) |
3dd63760 | 210 | |
5b04210c | 211 | (defcustom fortran-column-ruler-fixed |
23029d77 | 212 | "0 4 6 10 20 30 40 5\ |
7977773d | 213 | 0 60 70\n\ |
f022dd89 SM |
214 | \[ ]|{ | | | | | | | | \ |
215 | \| | | | |}\n" | |
dc6579ac | 216 | "String displayed above current line by \\[fortran-column-ruler]. |
5b04210c GM |
217 | This variable is used in fixed format mode. |
218 | See the variable `fortran-column-ruler-tab' for TAB format mode." | |
219 | :type 'string | |
220 | :group 'fortran) | |
23029d77 | 221 | |
5b04210c | 222 | (defcustom fortran-column-ruler-tab |
23029d77 | 223 | "0 810 20 30 40 5\ |
7977773d | 224 | 0 60 70\n\ |
f022dd89 SM |
225 | \[ ]| { | | | | | | | | \ |
226 | \| | | | |}\n" | |
dc6579ac | 227 | "String displayed above current line by \\[fortran-column-ruler]. |
5b04210c GM |
228 | This variable is used in TAB format mode. |
229 | See the variable `fortran-column-ruler-fixed' for fixed format mode." | |
230 | :type 'string | |
231 | :group 'fortran) | |
3dd63760 | 232 | |
5b04210c GM |
233 | (defcustom fortran-analyze-depth 100 |
234 | "Number of lines to scan to identify fixed or TAB format style." | |
235 | :type 'integer | |
236 | :group 'fortran) | |
3dd63760 | 237 | |
fcad5199 | 238 | (defcustom fortran-break-before-delimiters t |
5b04210c | 239 | "*Non-nil causes filling to break lines before delimiters. |
337c50a5 | 240 | Delimiters are characters matching the regexp `fortran-break-delimiters-re'." |
5b04210c | 241 | :type 'boolean |
fcad5199 | 242 | :group 'fortran) |
b8cbdf43 | 243 | |
337c50a5 GM |
244 | (defconst fortran-break-delimiters-re "[-+*/><=, \t]" |
245 | "Regexp matching delimiter characters at which lines may be broken. | |
246 | There are certain tokens comprised entirely of characters | |
247 | matching this regexp that should not be split, and these are | |
248 | specified by the constant `fortran-no-break-re'.") | |
249 | ||
250 | ;; The ">=", etc F77 extensions are supported by g77. | |
251 | (defconst fortran-no-break-re | |
252 | (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=") 'paren) | |
253 | "Regexp specifying where not to break lines when filling. | |
254 | This regexp matches certain tokens comprised entirely of | |
255 | characters matching the regexp `fortran-break-delimiters-re' that should | |
256 | not be split by filling. Each element is assumed to be two | |
257 | characters long.") | |
258 | ||
5b04210c GM |
259 | (defcustom fortran-mode-hook nil |
260 | "Hook run when entering Fortran mode." | |
261 | :type 'hook | |
262 | :group 'fortran) | |
3dd63760 | 263 | |
5b04210c GM |
264 | \f |
265 | (defvar fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(" | |
266 | "Regexp matching the start of an IF statement.") | |
267 | ||
268 | (defvar fortran-end-prog-re1 | |
269 | "end\ | |
270 | \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\ | |
271 | \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?" | |
272 | "Regexp possibly matching the end of a subprogram.") | |
563f09b5 | 273 | |
5b04210c GM |
274 | (defvar fortran-end-prog-re |
275 | (concat "^[ \t0-9]*" fortran-end-prog-re1) | |
276 | "Regexp possibly matching the end of a subprogram, from the line start. | |
277 | See also `fortran-end-prog-re1'.") | |
278 | ||
279 | (defconst fortran-type-types | |
280 | (concat "\\<" | |
281 | (mapconcat 'identity ; " " -> "[ \t]*" | |
282 | (split-string | |
283 | (regexp-opt | |
284 | (let ((simple-types | |
285 | '("character" "byte" "integer" "logical" | |
286 | "none" "real" "complex" | |
287 | "double precision" "double complex")) | |
288 | (structured-types '("structure" "union" "map")) | |
289 | (other-types '("record" "dimension" | |
290 | "parameter" "common" "save" | |
291 | "external" "intrinsic" "data" | |
292 | "equivalence"))) | |
293 | (append | |
294 | (mapcar (lambda (x) (concat "implicit " x)) | |
295 | simple-types) | |
296 | simple-types | |
297 | (mapcar (lambda (x) (concat "end " x)) | |
298 | structured-types) | |
299 | structured-types | |
300 | other-types)) 'paren)) | |
301 | "[ \t]*") "\\>") | |
302 | "Regexp matching Fortran types.") | |
303 | ||
304 | (defvar fortran-font-lock-keywords-1 | |
305 | ;; Program, subroutine and function declarations, plus calls. | |
306 | '(("\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|\ | |
307 | program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?" | |
308 | (1 font-lock-keyword-face) | |
309 | (2 font-lock-function-name-face nil t))) | |
f3d4eb7b | 310 | "Subdued level highlighting for Fortran mode.") |
563f09b5 | 311 | |
5b04210c GM |
312 | (defvar fortran-font-lock-keywords-2 |
313 | (append fortran-font-lock-keywords-1 | |
314 | (list | |
315 | ;; Fontify all type specifiers (must be first - see below). | |
316 | (cons fortran-type-types 'font-lock-type-face) | |
317 | ;; Builtin keywords (except logical, do and goto - see below). | |
318 | (concat "\\<" (regexp-opt | |
319 | '("continue" "format" "end" "enddo" | |
320 | "if" "then" "else" "endif" "elseif" | |
321 | "while" "inquire" "stop" "return" | |
322 | "include" "open" "close" "read" | |
323 | "write" "format" "print" "select" "case" | |
8cb8832f GM |
324 | "cycle" "exit" "rewind" "backspace" |
325 | "where" "elsewhere") | |
5b04210c GM |
326 | 'paren) "\\>") |
327 | ;; Builtin operators. | |
328 | (concat "\\." (regexp-opt | |
329 | '("and" "or" "not" "lt" "le" "eq" "ge" | |
330 | "gt" "ne" "true" "false") | |
331 | 'paren) "\\.") | |
332 | ;; do/goto keywords and targets, and goto tags. | |
333 | '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?" | |
334 | (1 font-lock-keyword-face) | |
335 | (2 font-lock-constant-face nil t)) | |
336 | '("^ *\\([0-9]+\\)" . font-lock-constant-face))) | |
f408b027 SM |
337 | "Medium level highlighting for Fortran mode.") |
338 | ||
5b04210c GM |
339 | (defvar fortran-font-lock-keywords-3 |
340 | (append | |
341 | fortran-font-lock-keywords-1 | |
342 | ;; All type specifiers plus their declared items. | |
343 | (list | |
344 | (list (concat fortran-type-types "[ \t(/]*\\(*\\)?") | |
345 | ;; Type specifier. | |
346 | '(1 font-lock-type-face) | |
347 | ;; Declaration item (or just /.../ block name). | |
348 | `(font-lock-match-c-style-declaration-item-and-skip-to-next | |
349 | ;; Start after any *(...) expression. | |
350 | (condition-case nil | |
351 | (and (match-beginning ,(1+ (regexp-opt-depth | |
352 | fortran-type-types))) | |
353 | (forward-sexp) | |
354 | (forward-sexp)) | |
355 | (error nil)) | |
356 | ;; No need to clean up. | |
357 | nil | |
358 | ;; Fontify as a variable name, functions fontified elsewhere. | |
359 | (1 font-lock-variable-name-face nil t)))) | |
360 | ;; Things extra to `fortran-font-lock-keywords-3' (must be done first). | |
361 | (list | |
362 | ;; Goto-like `err=label'/`end=label' in read/write statements. | |
363 | '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?" | |
364 | (1 font-lock-keyword-face) (4 font-lock-constant-face nil t)) | |
365 | ;; Standard continuation character and in a TAB-formatted line. | |
366 | '("^ \\{5\\}\\([^ 0\n]\\)" 1 font-lock-string-face) | |
367 | '("^\t\\([1-9]\\)" 1 font-lock-string-face)) | |
368 | `((,fortran-directive-re (0 font-lock-keyword-face t))) | |
369 | ;; `fortran-font-lock-keywords-2' without types (see above). | |
370 | (cdr (nthcdr (length fortran-font-lock-keywords-1) | |
371 | fortran-font-lock-keywords-2))) | |
f408b027 SM |
372 | "Gaudy level highlighting for Fortran mode.") |
373 | ||
8cb8832f GM |
374 | (defvar fortran-font-lock-keywords-4 |
375 | (append fortran-font-lock-keywords-3 | |
376 | (list (list | |
377 | (concat "\\<" | |
378 | (regexp-opt | |
379 | '("int" "ifix" "idint" "real" "float" "sngl" | |
380 | "dble" "cmplx" "ichar" "char" "aint" "dint" | |
381 | "anint" "dnint" "nint" "idnint" "iabs" "abs" | |
382 | "dabs" "cabs" "mod" "amod" "dmod" "isign" | |
383 | "sign" "dsign" "idim" "dim" "ddim" "dprod" | |
384 | "max" "max0" "amax1" "dmax1" "amax0" "max1" | |
385 | "min0" "amin1" "dmin1" "amin0" "min1" "len" | |
386 | "index" "lge" "lgt" "lle" "llt" "aimag" | |
387 | "conjg" "sqrt" "dsqrt" "csqrt" "exp" "dexp" | |
388 | "cexp" "log" "alog" "dlog" "clog" "log10" | |
389 | "alog10" "dlog10" "sin" "dsin" "csin" "cos" | |
390 | "dcos" "ccos" "tan" "dtan" "asin" "dasin" | |
391 | "acos" "dacos" "atan" "datan" "atan2" "datan2" | |
392 | "sinh" "dsinh" "cosh" "dcosh" "tanh" "dtanh") | |
393 | 'paren) "[ \t]*(") '(1 font-lock-builtin-face)))) | |
394 | "Maximum highlighting for Fortran mode. | |
395 | Consists of level 3 plus all other intrinsics not already highlighted.") | |
396 | ||
5b04210c GM |
397 | ;; Comments are real pain in Fortran because there is no way to |
398 | ;; represent the standard comment syntax in an Emacs syntax table. | |
399 | ;; (We can do so for F90-style). Therefore an unmatched quote in a | |
400 | ;; standard comment will throw fontification off on the wrong track. | |
401 | ;; So we do syntactic fontification with regexps. | |
402 | (defvar fortran-font-lock-syntactic-keywords | |
403 | '(("^[cd\\*]" 0 (11)) | |
404 | ("^[^cd\\*\t\n].\\{71\\}\\([^\n]+\\)" 1 (11))) | |
f687a879 DL |
405 | "`font-lock-syntactic-keywords' for Fortran. |
406 | These get fixed-format comments fontified.") | |
0f1057e9 | 407 | |
f3d4eb7b SM |
408 | (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1 |
409 | "Default expressions to highlight in Fortran mode.") | |
5b04210c | 410 | |
9645c179 | 411 | (defvar fortran-imenu-generic-expression |
240e9cda DL |
412 | ;; These patterns could be confused by sequence nos. in cols 72+ and |
413 | ;; don't allow continuations everywhere. | |
7977773d DL |
414 | (list |
415 | (list | |
416 | nil | |
a8189dfe DL |
417 | ;; [This will be fooled by `end function' allowed by G77. Also, |
418 | ;; it assumes sensible whitespace is employed.] | |
419 | (concat | |
420 | ;; leading whitespace: | |
421 | "^\\s-+\\(" | |
422 | ;; function declaration with optional type, e.g. `real', | |
423 | ;; `real*4', character(*), `double precision': | |
424 | "\\(\\sw\\|\\s-\\|[*()+]\\)*" | |
425 | "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)" | |
426 | ;; Possible statement continuation: | |
427 | "[ \t" fortran-continuation-string "]+" | |
428 | ;; Variable to index: | |
429 | "\\(\\sw+\\)") | |
7977773d | 430 | 3) |
5b04210c GM |
431 | ;; Un-named block data. |
432 | '(nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1)) | |
433 | "Value for `imenu-generic-expression' in Fortran mode.") | |
434 | ||
435 | \f | |
8cb8832f GM |
436 | ;; Hideshow support. |
437 | (defconst fortran-blocks-re | |
438 | (concat "block[ \t]*data\\|select[ \t]*case\\|" | |
439 | (regexp-opt '("do" "if" "interface" "function" "map" "program" | |
440 | "structure" "subroutine" "union" "where"))) | |
441 | "Regexp potentially indicating the start or end of a Fortran \"block\". | |
442 | Omits naked END statements, and DO-loops closed by anything other | |
443 | than ENDDO.") | |
444 | ||
445 | (defconst fortran-end-block-re | |
446 | ;; Do-loops terminated by things other than ENDDO cannot be handled | |
447 | ;; with a regexp. This omission does not seem to matter to hideshow... | |
448 | (concat "^[ \t0-9]*\\<end[ \t]*\\(" | |
449 | fortran-blocks-re | |
450 | ;; Naked END statement. | |
451 | "\\|!\\|$\\)") | |
452 | "Regexp matching the end of a Fortran \"block\", from the line start. | |
453 | Note that only ENDDO is handled for the end of a DO-loop. Used | |
454 | in the Fortran entry in `hs-special-modes-alist'.") | |
455 | ||
456 | (defconst fortran-start-block-re | |
457 | (concat | |
458 | "^[ \t0-9]*\\(" ; statement number | |
459 | ;; Structure label for DO, IF, SELECT, WHERE. | |
460 | "\\(\\(\\sw+[ \t]*:[ \t]*\\)?" | |
461 | ;; IF blocks are a nuisance: | |
462 | ;; IF ( ... ) foo is not a block, but a single statement. | |
463 | ;; IF ( ... ) THEN can be split over multiple lines. | |
464 | ;; [So can, eg, a DO WHILE (... ), but that is less common, I hope.] | |
465 | ;; The regexp below allows for it to be split over at most 2 lines. | |
466 | ;; That leads to the problem of not matching two consecutive IF | |
467 | ;; statements as one, eg: | |
468 | ;; IF ( ... ) foo | |
469 | ;; IF ( ... ) THEN | |
470 | ;; It simply is not possible to do this in a 100% correct fashion | |
471 | ;; using a regexp - see the functions fortran-end-if, | |
472 | ;; fortran-beginning-if for the hoops we have to go through. | |
473 | ;; An alternative is to match on THEN at a line end, eg: | |
474 | ;; ".*)[ \t]*then[ \t]*\\($\\|!\\)" | |
475 | ;; This would also match ELSE branches, though. This does not seem | |
476 | ;; right to me, because then one has neighbouring blocks that are | |
477 | ;; not nested in each other. | |
478 | "\\(if[ \t]*(\\(.*\\|" | |
479 | ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|" | |
480 | "do\\|select[ \t]*case\\|where\\)\\)\\|" | |
481 | (regexp-opt '("interface" "function" "map" "program" | |
482 | "structure" "subroutine" "union")) | |
483 | "\\|block[ \t]*data\\)[ \t]*") | |
484 | "Regexp matching the start of a Fortran \"block\", from the line start. | |
485 | A simple regexp cannot do this in fully correct fashion, so this | |
486 | tries to strike a compromise between complexity and flexibility. | |
487 | Used in the Fortran entry in `hs-special-modes-alist'.") | |
488 | ||
489 | (add-to-list 'hs-special-modes-alist | |
490 | `(fortran-mode ,fortran-start-block-re ,fortran-end-block-re | |
491 | "^[cC*!]" fortran-end-of-block nil)) | |
492 | ||
493 | \f | |
5b04210c GM |
494 | (defvar fortran-mode-syntax-table |
495 | (let ((table (make-syntax-table))) | |
496 | ;; We might like `;' to be punctuation (g77 multi-statement | |
497 | ;; lines), but that screws abbrevs. | |
498 | (modify-syntax-entry ?\; "w" table) | |
499 | (modify-syntax-entry ?\r " " table) | |
500 | (modify-syntax-entry ?+ "." table) | |
501 | (modify-syntax-entry ?- "." table) | |
502 | (modify-syntax-entry ?= "." table) | |
503 | (modify-syntax-entry ?* "." table) | |
504 | (modify-syntax-entry ?/ "." table) | |
505 | (modify-syntax-entry ?\' "\"" table) | |
506 | (modify-syntax-entry ?\" "\"" table) | |
8cb8832f GM |
507 | ;; Consistent with GNU Fortran's default -- see the manual. |
508 | ;; The F77 standard imposes no rule on this issue. | |
5b04210c GM |
509 | (modify-syntax-entry ?\\ "\\" table) |
510 | ;; This might be better as punctuation, as for C, but this way you | |
511 | ;; can treat floating-point numbers as symbols. | |
512 | (modify-syntax-entry ?. "_" table) ; e.g. `a.ne.b' | |
513 | (modify-syntax-entry ?_ "_" table) | |
514 | (modify-syntax-entry ?$ "_" table) ; esp. VMSisms | |
515 | (modify-syntax-entry ?\! "<" table) | |
516 | (modify-syntax-entry ?\n ">" table) | |
517 | table) | |
518 | "Syntax table used in Fortran mode.") | |
f408b027 | 519 | |
f6bf87c5 NR |
520 | (defvar fortran-gud-syntax-table |
521 | (let ((st (make-syntax-table fortran-mode-syntax-table))) | |
522 | (modify-syntax-entry ?\n "." st) | |
523 | st) | |
524 | "Syntax table used to parse Fortran expressions for printing in GUD.") | |
525 | ||
7a7db8e5 | 526 | (defvar fortran-mode-map |
315aa1de | 527 | (let ((map (make-sparse-keymap))) |
5b04210c GM |
528 | (define-key map ";" 'fortran-abbrev-start) |
529 | (define-key map "\C-c;" 'fortran-comment-region) | |
530 | (define-key map "\M-;" 'fortran-indent-comment) | |
531 | (define-key map "\M-\n" 'fortran-split-line) | |
8cb8832f GM |
532 | (define-key map "\M-\C-n" 'fortran-end-of-block) |
533 | (define-key map "\M-\C-p" 'fortran-beginning-of-block) | |
5b04210c | 534 | (define-key map "\M-\C-q" 'fortran-indent-subprogram) |
315aa1de DL |
535 | (define-key map "\C-c\C-w" 'fortran-window-create-momentarily) |
536 | (define-key map "\C-c\C-r" 'fortran-column-ruler) | |
537 | (define-key map "\C-c\C-p" 'fortran-previous-statement) | |
538 | (define-key map "\C-c\C-n" 'fortran-next-statement) | |
539 | (define-key map "\C-c\C-d" 'fortran-join-line) ; like f90 | |
5b04210c | 540 | (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation |
315aa1de DL |
541 | (define-key map "0" 'fortran-electric-line-number) |
542 | (define-key map "1" 'fortran-electric-line-number) | |
543 | (define-key map "2" 'fortran-electric-line-number) | |
544 | (define-key map "3" 'fortran-electric-line-number) | |
545 | (define-key map "4" 'fortran-electric-line-number) | |
546 | (define-key map "5" 'fortran-electric-line-number) | |
547 | (define-key map "6" 'fortran-electric-line-number) | |
548 | (define-key map "7" 'fortran-electric-line-number) | |
549 | (define-key map "8" 'fortran-electric-line-number) | |
550 | (define-key map "9" 'fortran-electric-line-number) | |
7a7db8e5 | 551 | |
5b04210c GM |
552 | (easy-menu-define fortran-menu map "Menu for Fortran mode." |
553 | `("Fortran" | |
554 | ["Manual" (info "(emacs)Fortran")] | |
555 | ("Customization" | |
556 | ,(custom-menu-create 'fortran) | |
557 | ["Set" Custom-set t] | |
558 | ["Save" Custom-save t] | |
559 | ["Reset to Current" Custom-reset-current t] | |
560 | ["Reset to Saved" Custom-reset-saved t] | |
561 | ["Reset to Standard Settings" Custom-reset-standard t] | |
562 | ) | |
563 | "--" | |
564 | ["Comment Region" fortran-comment-region mark-active] | |
565 | ["Uncomment Region" | |
566 | (fortran-comment-region (region-beginning) (region-end) 1) | |
567 | mark-active] | |
568 | ["Indent Region" indent-region mark-active] | |
569 | ["Indent Subprogram" fortran-indent-subprogram t] | |
570 | "--" | |
571 | ["Beginning of Subprogram" fortran-beginning-of-subprogram t] | |
572 | ["End of Subprogram" fortran-end-of-subprogram t] | |
573 | ("Mark" | |
574 | ["Subprogram" mark-defun t] | |
575 | ["IF Block" fortran-mark-if t] | |
576 | ["DO Block" fortran-mark-do t] | |
577 | ) | |
578 | ["Narrow to Subprogram" narrow-to-defun t] | |
579 | ["Widen" widen t] | |
580 | "--" | |
581 | ["Temporary column ruler" fortran-column-ruler t] | |
582 | ["72-column window" fortran-window-create t] | |
583 | ["Full Width Window" | |
584 | (enlarge-window-horizontally (- (frame-width) (window-width))) | |
585 | (< (window-width) (frame-width))] | |
586 | ["Momentary 72-column window" fortran-window-create-momentarily t] | |
587 | "--" | |
588 | ["Break Line at Point" fortran-split-line t] | |
589 | ["Join Line" fortran-join-line t] | |
590 | ["Fill Statement/Comment" fill-paragraph t] | |
591 | "--" | |
592 | ["Toggle auto-fill" auto-fill-mode :selected auto-fill-function | |
593 | :style toggle] | |
594 | ["Toggle abbrev-mode" abbrev-mode :selected abbrev-mode | |
595 | :style toggle] | |
596 | ["Add imenu Menu" imenu-add-menubar-index | |
597 | :active (not (lookup-key (current-local-map) [menu-bar index])) | |
598 | :included (fboundp 'imenu-add-to-menubar)])) | |
315aa1de | 599 | map) |
b8cbdf43 | 600 | "Keymap used in Fortran mode.") |
5b04210c | 601 | |
3dd63760 | 602 | \f |
7a7db8e5 | 603 | (defvar fortran-mode-abbrev-table |
6f9a4ce2 GM |
604 | (let (abbrevs-changed) |
605 | (define-abbrev-table 'fortran-mode-abbrev-table nil) | |
606 | ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible. | |
607 | ;; Only use `apply' to quieten the byte-compiler. | |
608 | (mapcar | |
609 | (function (lambda (element) | |
610 | (condition-case nil | |
611 | (apply 'define-abbrev fortran-mode-abbrev-table | |
612 | (append element '(nil 0 t))) | |
613 | (wrong-number-of-arguments | |
614 | (apply 'define-abbrev fortran-mode-abbrev-table | |
615 | (append element '(nil 0))))))) | |
616 | '((";au" "automatic" ) | |
617 | (";b" "byte" ) | |
618 | (";bd" "block data" ) | |
619 | (";ch" "character" ) | |
620 | (";cl" "close" ) | |
621 | (";c" "continue" ) | |
622 | (";cm" "common" ) | |
623 | (";cx" "complex" ) | |
624 | (";df" "define" ) | |
625 | (";di" "dimension" ) | |
626 | (";do" "double" ) | |
627 | (";dc" "double complex" ) | |
628 | (";dp" "double precision" ) | |
629 | (";dw" "do while" ) | |
630 | (";e" "else" ) | |
631 | (";ed" "enddo" ) | |
632 | (";el" "elseif" ) | |
633 | (";en" "endif" ) | |
634 | (";eq" "equivalence" ) | |
635 | (";ew" "endwhere" ) | |
636 | (";ex" "external" ) | |
637 | (";ey" "entry" ) | |
638 | (";f" "format" ) | |
639 | (";fa" ".false." ) | |
640 | (";fu" "function" ) | |
641 | (";g" "goto" ) | |
642 | (";im" "implicit" ) | |
643 | (";ib" "implicit byte" ) | |
644 | (";ic" "implicit complex" ) | |
645 | (";ich" "implicit character") | |
646 | (";ii" "implicit integer" ) | |
647 | (";il" "implicit logical" ) | |
648 | (";ir" "implicit real" ) | |
649 | (";inc" "include" ) | |
650 | (";in" "integer" ) | |
651 | (";intr" "intrinsic" ) | |
652 | (";l" "logical" ) | |
653 | (";n" "namelist" ) | |
654 | (";o" "open" ) ; was ;op | |
655 | (";pa" "parameter" ) | |
656 | (";pr" "program" ) | |
657 | (";ps" "pause" ) | |
658 | (";p" "print" ) | |
659 | (";rc" "record" ) | |
660 | (";re" "real" ) | |
661 | (";r" "read" ) | |
662 | (";rt" "return" ) | |
663 | (";rw" "rewind" ) | |
664 | (";s" "stop" ) | |
665 | (";sa" "save" ) | |
666 | (";st" "structure" ) | |
667 | (";sc" "static" ) | |
668 | (";su" "subroutine" ) | |
669 | (";tr" ".true." ) | |
670 | (";ty" "type" ) | |
671 | (";vo" "volatile" ) | |
672 | (";w" "write" ) | |
673 | (";wh" "where" ))) | |
7a7db8e5 | 674 | fortran-mode-abbrev-table)) |
7977773d | 675 | |
5b04210c | 676 | \f |
7dae727d | 677 | |
3dd63760 JB |
678 | ;;;###autoload |
679 | (defun fortran-mode () | |
5b04210c GM |
680 | "Major mode for editing Fortran code in fixed format. |
681 | For free format code, use `f90-mode'. | |
682 | ||
60db3594 | 683 | \\[fortran-indent-line] indents the current Fortran line correctly. |
5b04210c | 684 | Note that DO statements must not share a common CONTINUE. |
3dd63760 | 685 | |
5b04210c GM |
686 | Type ;? or ;\\[help-command] to display a list of built-in abbrevs for\ |
687 | Fortran keywords. | |
3dd63760 JB |
688 | |
689 | Key definitions: | |
690 | \\{fortran-mode-map} | |
691 | ||
692 | Variables controlling indentation style and extra features: | |
693 | ||
8cb8832f | 694 | `fortran-comment-line-start' |
5b04210c GM |
695 | To use comments starting with `!', set this to the string \"!\". |
696 | `fortran-do-indent' | |
697 | Extra indentation within DO blocks (default 3). | |
698 | `fortran-if-indent' | |
699 | Extra indentation within IF blocks (default 3). | |
700 | `fortran-structure-indent' | |
701 | Extra indentation within STRUCTURE, UNION, MAP and INTERFACE blocks. | |
702 | (default 3) | |
703 | `fortran-continuation-indent' | |
704 | Extra indentation applied to continuation statements (default 5). | |
705 | `fortran-comment-line-extra-indent' | |
706 | Amount of extra indentation for text in full-line comments (default 0). | |
707 | `fortran-comment-indent-style' | |
708 | How to indent the text in full-line comments. Allowed values are: | |
709 | nil don't change the indentation | |
710 | fixed indent to `fortran-comment-line-extra-indent' beyond the | |
711 | value of either | |
712 | `fortran-minimum-statement-indent-fixed' (fixed format) or | |
713 | `fortran-minimum-statement-indent-tab' (TAB format), | |
714 | depending on the continuation format in use. | |
715 | relative indent to `fortran-comment-line-extra-indent' beyond the | |
3dd63760 | 716 | indentation for a line of code. |
5b04210c GM |
717 | (default 'fixed) |
718 | `fortran-comment-indent-char' | |
719 | Single-character string to be inserted instead of space for | |
720 | full-line comment indentation (default \" \"). | |
721 | `fortran-minimum-statement-indent-fixed' | |
722 | Minimum indentation for statements in fixed format mode (default 6). | |
723 | `fortran-minimum-statement-indent-tab' | |
724 | Minimum indentation for statements in TAB format mode (default 9). | |
725 | `fortran-line-number-indent' | |
726 | Maximum indentation for line numbers (default 1). A line number will | |
727 | get less than this much indentation if necessary to avoid reaching | |
728 | column 5. | |
729 | `fortran-check-all-num-for-matching-do' | |
730 | Non-nil causes all numbered lines to be treated as possible \"continue\" | |
731 | statements (default nil). | |
732 | `fortran-blink-matching-if' | |
733 | Non-nil causes \\[fortran-indent-line] on an ENDIF (or ENDDO) statement | |
734 | to blink on the matching IF (or DO [WHILE]). (default nil) | |
735 | `fortran-continuation-string' | |
736 | Single-character string to be inserted in column 5 of a continuation | |
737 | line (default \"$\"). | |
738 | `fortran-comment-region' | |
739 | String inserted by \\[fortran-comment-region] at start of each line in | |
740 | the region (default \"c$$$\"). | |
741 | `fortran-electric-line-number' | |
742 | Non-nil causes line number digits to be moved to the correct column | |
743 | as typed (default t). | |
744 | `fortran-break-before-delimiters' | |
745 | Non-nil causes lines to be broken before delimiters (default t). | |
3dd63760 | 746 | |
b8cbdf43 | 747 | Turning on Fortran mode calls the value of the variable `fortran-mode-hook' |
3dd63760 JB |
748 | with no args, if that value is non-nil." |
749 | (interactive) | |
750 | (kill-all-local-variables) | |
5b04210c GM |
751 | (setq major-mode 'fortran-mode |
752 | mode-name "Fortran" | |
753 | local-abbrev-table fortran-mode-abbrev-table) | |
3dd63760 | 754 | (set-syntax-table fortran-mode-syntax-table) |
5b04210c GM |
755 | (use-local-map fortran-mode-map) |
756 | (set (make-local-variable 'indent-line-function) 'fortran-indent-line) | |
757 | (set (make-local-variable 'indent-region-function) | |
758 | (lambda (start end) | |
759 | (let (fortran-blink-matching-if ; avoid blinking delay | |
760 | indent-region-function) | |
761 | (indent-region start end nil)))) | |
fd98e23a | 762 | (set (make-local-variable 'require-final-newline) mode-require-final-newline) |
5b04210c GM |
763 | ;; The syntax tables don't understand the column-0 comment-markers. |
764 | (set (make-local-variable 'comment-use-syntax) nil) | |
765 | (set (make-local-variable 'comment-padding) "$$$") | |
766 | (set (make-local-variable 'comment-start) fortran-comment-line-start) | |
ff451e17 SM |
767 | (set (make-local-variable 'comment-start-skip) |
768 | ;; We can't reuse `fortran-comment-line-start-skip' directly because | |
769 | ;; it contains backrefs whereas we need submatch-1 to end at the | |
770 | ;; beginning of the comment delimiter. | |
771 | ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)") | |
772 | "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*") | |
5b04210c GM |
773 | (set (make-local-variable 'comment-indent-function) 'fortran-comment-indent) |
774 | (set (make-local-variable 'abbrev-all-caps) t) | |
775 | (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill) | |
5b04210c | 776 | (set (make-local-variable 'indent-tabs-mode) (fortran-analyze-file-format)) |
a7113309 | 777 | (setq mode-line-process '(indent-tabs-mode fortran-tab-mode-string)) |
1eb6bf70 | 778 | (set (make-local-variable 'fill-column) 72) |
1eb6bf70 | 779 | (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph) |
5b04210c GM |
780 | (set (make-local-variable 'font-lock-defaults) |
781 | '((fortran-font-lock-keywords | |
782 | fortran-font-lock-keywords-1 | |
783 | fortran-font-lock-keywords-2 | |
8cb8832f GM |
784 | fortran-font-lock-keywords-3 |
785 | fortran-font-lock-keywords-4) | |
5b04210c GM |
786 | nil t ((?/ . "$/") ("_$" . "w")) |
787 | fortran-beginning-of-subprogram)) | |
788 | (set (make-local-variable 'font-lock-syntactic-keywords) | |
789 | fortran-font-lock-syntactic-keywords) | |
790 | (set (make-local-variable 'imenu-case-fold-search) t) | |
791 | (set (make-local-variable 'imenu-generic-expression) | |
792 | fortran-imenu-generic-expression) | |
793 | (set (make-local-variable 'imenu-syntax-alist) '(("_$" . "w"))) | |
26ef1c87 DL |
794 | (set (make-local-variable 'beginning-of-defun-function) |
795 | #'fortran-beginning-of-subprogram) | |
796 | (set (make-local-variable 'end-of-defun-function) | |
797 | #'fortran-end-of-subprogram) | |
68ca306c DL |
798 | (set (make-local-variable 'add-log-current-defun-function) |
799 | #'fortran-current-defun) | |
315aa1de | 800 | (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search) |
5727e748 | 801 | (set (make-local-variable 'gud-find-expr-function) 'fortran-gud-find-expr) |
9a969196 | 802 | (run-mode-hooks 'fortran-mode-hook)) |
5b04210c | 803 | |
3dd63760 | 804 | \f |
5727e748 SM |
805 | (defun fortran-gud-find-expr () |
806 | ;; Consider \n as punctuation (end of expression). | |
807 | (with-syntax-table fortran-gud-syntax-table | |
808 | (gud-find-c-expr))) | |
809 | ||
315aa1de | 810 | (defsubst fortran-comment-indent () |
5b04210c GM |
811 | "Return the indentation appropriate for the current comment line. |
812 | This is 0 for a line matching `fortran-comment-line-start-skip', else | |
813 | the value of `comment-column' (leaving at least one space after code)." | |
814 | (if (looking-at fortran-comment-line-start-skip) 0 | |
815 | (save-excursion | |
ff451e17 | 816 | (skip-chars-backward " \t") |
0a39a75c | 817 | (max (1+ (current-column)) comment-column)))) |
3dd63760 JB |
818 | |
819 | (defun fortran-indent-comment () | |
820 | "Align or create comment on current line. | |
821 | Existing comments of all types are recognized and aligned. | |
5b04210c | 822 | If the line has no comment, a side-by-side comment is inserted and aligned, |
ff451e17 | 823 | if the value of `comment-start' is not nil and allows such comments. |
3dd63760 JB |
824 | Otherwise, a separate-line comment is inserted, on this line |
825 | or on a new line inserted before this line if this line is not blank." | |
5b04210c | 826 | (interactive "*") |
3dd63760 JB |
827 | (beginning-of-line) |
828 | ;; Recognize existing comments of either kind. | |
ff451e17 SM |
829 | (cond ((fortran-find-comment-start-skip 'all) |
830 | (goto-char (match-beginning 0)) | |
831 | (if (bolp) | |
832 | (fortran-indent-line) | |
5b04210c GM |
833 | (unless (= (current-column) (fortran-comment-indent)) |
834 | (delete-horizontal-space) | |
835 | (indent-to (fortran-comment-indent))))) | |
3dd63760 JB |
836 | ;; No existing comment. |
837 | ;; If side-by-side comments are defined, insert one, | |
838 | ;; unless line is now blank. | |
ff451e17 SM |
839 | ((and comment-start (not (looking-at "[ \t]*$")) |
840 | (string-match comment-start-skip (concat " " comment-start))) | |
3dd63760 JB |
841 | (end-of-line) |
842 | (delete-horizontal-space) | |
315aa1de | 843 | (indent-to (fortran-comment-indent)) |
3dd63760 JB |
844 | (insert comment-start)) |
845 | ;; Else insert separate-line comment, making a new line if nec. | |
846 | (t | |
847 | (if (looking-at "^[ \t]*$") | |
848 | (delete-horizontal-space) | |
849 | (beginning-of-line) | |
7dae727d | 850 | (insert ?\n) |
3dd63760 | 851 | (forward-char -1)) |
7dae727d | 852 | (insert fortran-comment-line-start) |
3dd63760 JB |
853 | (insert-char (if (stringp fortran-comment-indent-char) |
854 | (aref fortran-comment-indent-char 0) | |
b8cbdf43 | 855 | fortran-comment-indent-char) |
1eb6bf70 | 856 | (- (fortran-calculate-indent) (current-column)))))) |
3dd63760 JB |
857 | |
858 | (defun fortran-comment-region (beg-region end-region arg) | |
5b04210c GM |
859 | "Comment every line in the region. |
860 | Inserts the string variable `fortran-comment-region' at the beginning of | |
861 | every line in the region. | |
862 | BEG-REGION and END-REGION specify the region boundaries. | |
3dd63760 JB |
863 | With non-nil ARG, uncomments the region." |
864 | (interactive "*r\nP") | |
b937fd1e | 865 | (let ((end-region-mark (copy-marker end-region)) |
315aa1de | 866 | (save-point (point-marker))) |
3dd63760 JB |
867 | (goto-char beg-region) |
868 | (beginning-of-line) | |
5b04210c | 869 | (if arg |
0a39a75c | 870 | (let ((com (regexp-quote fortran-comment-region))) ; uncomment |
5b04210c GM |
871 | (if (looking-at com) |
872 | (delete-region (point) (match-end 0))) | |
873 | (while (and (zerop (forward-line 1)) | |
874 | (< (point) end-region-mark)) | |
875 | (if (looking-at com) | |
876 | (delete-region (point) (match-end 0))))) | |
0a39a75c | 877 | (insert fortran-comment-region) ; comment |
5b04210c GM |
878 | (while (and (zerop (forward-line 1)) |
879 | (< (point) end-region-mark)) | |
880 | (insert fortran-comment-region))) | |
3dd63760 JB |
881 | (goto-char save-point) |
882 | (set-marker end-region-mark nil) | |
883 | (set-marker save-point nil))) | |
5b04210c | 884 | |
3dd63760 JB |
885 | \f |
886 | (defun fortran-abbrev-start () | |
60db3594 | 887 | "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs. |
3dd63760 | 888 | Any other key combination is executed normally." |
5b04210c | 889 | (interactive "*") |
be35ca9f GM |
890 | (insert last-command-char) |
891 | (let (char event) | |
892 | (if (fboundp 'next-command-event) ; XEmacs | |
893 | (setq event (next-command-event) | |
894 | char (event-to-character event)) | |
895 | (setq event (read-event) | |
896 | char event)) | |
897 | ;; Insert char if not equal to `?', or if abbrev-mode is off. | |
898 | (if (and abbrev-mode (or (eq char ??) (eq char help-char))) | |
3dd63760 | 899 | (fortran-abbrev-help) |
be35ca9f | 900 | (setq unread-command-events (list event))))) |
3dd63760 JB |
901 | |
902 | (defun fortran-abbrev-help () | |
903 | "List the currently defined abbrevs in Fortran mode." | |
904 | (interactive) | |
905 | (message "Listing abbrev table...") | |
e80f2147 | 906 | (display-buffer (fortran-prepare-abbrev-list-buffer)) |
3dd63760 JB |
907 | (message "Listing abbrev table...done")) |
908 | ||
e80f2147 | 909 | (defun fortran-prepare-abbrev-list-buffer () |
5b04210c | 910 | "Create a buffer listing the Fortran mode abbreviations." |
e80f2147 RS |
911 | (save-excursion |
912 | (set-buffer (get-buffer-create "*Abbrevs*")) | |
913 | (erase-buffer) | |
4632a893 | 914 | (insert-abbrev-table-description 'fortran-mode-abbrev-table t) |
e80f2147 RS |
915 | (goto-char (point-min)) |
916 | (set-buffer-modified-p nil) | |
917 | (edit-abbrevs-mode)) | |
918 | (get-buffer-create "*Abbrevs*")) | |
919 | ||
3dd63760 | 920 | (defun fortran-column-ruler () |
7977773d | 921 | "Insert a column ruler momentarily above current line, till next keystroke. |
0a39a75c GM |
922 | The ruler is defined by the value of `fortran-column-ruler-fixed' in fixed |
923 | format mode, and `fortran-column-ruler-tab' in TAB format mode. | |
924 | The next key typed is executed unless it is SPC." | |
3dd63760 | 925 | (interactive) |
60db3594 | 926 | (momentary-string-display |
23029d77 JB |
927 | (if indent-tabs-mode |
928 | fortran-column-ruler-tab | |
929 | fortran-column-ruler-fixed) | |
930 | (save-excursion | |
60db3594 | 931 | (beginning-of-line) |
23029d77 JB |
932 | (if (eq (window-start (selected-window)) |
933 | (window-point (selected-window))) | |
7a7db8e5 | 934 | (line-beginning-position 2) |
23029d77 | 935 | (point))) |
3dd63760 JB |
936 | nil "Type SPC or any command to erase ruler.")) |
937 | ||
938 | (defun fortran-window-create () | |
7977773d | 939 | "Make the window 72 columns wide. |
fe668515 | 940 | See also `fortran-window-create-momentarily'." |
3dd63760 | 941 | (interactive) |
b2523604 DL |
942 | (let ((window-min-width 2)) |
943 | (if (< (window-width) (frame-width)) | |
944 | (enlarge-window-horizontally (- (frame-width) | |
945 | (window-width) 1))) | |
946 | (let* ((window-edges (window-edges)) | |
947 | (scroll-bar-width (- (nth 2 window-edges) | |
948 | (car window-edges) | |
949 | (window-width)))) | |
950 | (split-window-horizontally (+ 72 scroll-bar-width))) | |
951 | (other-window 1) | |
952 | (switch-to-buffer " fortran-window-extra" t) | |
953 | (select-window (previous-window)))) | |
3dd63760 JB |
954 | |
955 | (defun fortran-window-create-momentarily (&optional arg) | |
7977773d | 956 | "Momentarily make the window 72 columns wide. |
3dd63760 | 957 | Optional ARG non-nil and non-unity disables the momentary feature. |
fe668515 | 958 | See also `fortran-window-create'." |
3dd63760 JB |
959 | (interactive "p") |
960 | (if (or (not arg) | |
961 | (= arg 1)) | |
962 | (save-window-excursion | |
b2523604 DL |
963 | (progn |
964 | (condition-case nil | |
965 | (fortran-window-create) | |
966 | (error (error "No room for Fortran window"))) | |
967 | (message "Type SPC to continue editing.") | |
968 | (let ((char (read-event))) | |
969 | (or (equal char (string-to-char " ")) | |
970 | (setq unread-command-events (list char)))))) | |
3dd63760 JB |
971 | (fortran-window-create))) |
972 | ||
973 | (defun fortran-split-line () | |
974 | "Break line at point and insert continuation marker and alignment." | |
5b04210c | 975 | (interactive "*") |
3dd63760 | 976 | (delete-horizontal-space) |
7dae727d | 977 | (if (save-excursion |
894dc7e7 SM |
978 | (let ((pos (point))) |
979 | (beginning-of-line) | |
980 | (and (fortran-find-comment-start-skip 'all) | |
981 | (< (match-beginning 0) pos)))) | |
ff451e17 | 982 | (insert ?\n (match-string 0)) |
23029d77 | 983 | (if indent-tabs-mode |
7dae727d | 984 | (insert ?\n ?\t (fortran-numerical-continuation-char)) |
0a39a75c GM |
985 | (insert "\n " fortran-continuation-string))) ; space after \n important |
986 | (fortran-indent-line)) ; when cont string is C, c or * | |
1eb6bf70 DL |
987 | |
988 | (defun fortran-remove-continuation () | |
5b04210c GM |
989 | "Delete any Fortran continuation characters at point. |
990 | Returns t if anything actually deleted." | |
991 | (when (looking-at "\\( \\{5\\}[^ 0\n]\\|\t[1-9]\\|&\\)") | |
992 | (replace-match "") | |
993 | (delete-indentation) | |
994 | t)) | |
3dd63760 | 995 | |
46d4d7bf DL |
996 | (defun fortran-join-line (arg) |
997 | "Join current line to the previous one and re-indent. | |
998 | With a prefix argument, repeat this operation that many times. | |
999 | If the prefix argument ARG is negative, join the next -ARG lines. | |
1000 | Continuation lines are correctly handled." | |
1001 | (interactive "*p") | |
7977773d | 1002 | (save-excursion |
46d4d7bf DL |
1003 | (when (> 0 arg) |
1004 | (setq arg (- arg)) | |
1005 | (forward-line arg)) | |
1006 | (while (not (zerop arg)) | |
1007 | (beginning-of-line) | |
1008 | (or (fortran-remove-continuation) | |
1009 | (delete-indentation)) | |
1010 | (setq arg (1- arg))) | |
7977773d DL |
1011 | (fortran-indent-line))) |
1012 | ||
3dd63760 | 1013 | (defun fortran-numerical-continuation-char () |
b56eb7c9 | 1014 | "Return a digit for tab-digit style of continuation lines. |
5b04210c GM |
1015 | If previous line is a tab-digit continuation line, return that digit |
1016 | plus one, otherwise return 1. Zero not allowed." | |
3dd63760 JB |
1017 | (save-excursion |
1018 | (forward-line -1) | |
1019 | (if (looking-at "\t[1-9]") | |
5b04210c | 1020 | (+ ?1 (% (- (char-after (1+ (point))) ?0) 9)) |
3dd63760 JB |
1021 | ?1))) |
1022 | ||
556dd629 | 1023 | (put 'fortran-electric-line-number 'delete-selection t) |
3dd63760 JB |
1024 | (defun fortran-electric-line-number (arg) |
1025 | "Self insert, but if part of a Fortran line number indent it automatically. | |
7977773d | 1026 | Auto-indent does not happen if a numeric ARG is used." |
5b04210c | 1027 | (interactive "*P") |
3dd63760 | 1028 | (if (or arg (not fortran-electric-line-number)) |
60db3594 | 1029 | (if arg |
b8cbdf43 | 1030 | (self-insert-command (prefix-numeric-value arg)) |
3dd63760 JB |
1031 | (self-insert-command 1)) |
1032 | (if (or (and (= 5 (current-column)) | |
1033 | (save-excursion | |
1034 | (beginning-of-line) | |
5b04210c GM |
1035 | ;; In col 5 with only spaces to the left. |
1036 | (looking-at " \\{5\\}"))) | |
23029d77 | 1037 | (and (= (if indent-tabs-mode |
1eb6bf70 DL |
1038 | fortran-minimum-statement-indent-tab |
1039 | fortran-minimum-statement-indent-fixed) (current-column)) | |
5b04210c | 1040 | ;; In col 8 with a single tab to the left. |
bf247b6e | 1041 | (eq ?\t (char-after (line-beginning-position))) |
3dd63760 JB |
1042 | (not (or (eq last-command 'fortran-indent-line) |
1043 | (eq last-command | |
aa4ed68c | 1044 | 'fortran-indent-new-line)))) |
3dd63760 JB |
1045 | (save-excursion |
1046 | (re-search-backward "[^ \t0-9]" | |
7dae727d | 1047 | (line-beginning-position) |
5b04210c GM |
1048 | t)) ; not a line number |
1049 | (looking-at "[0-9]")) ; within a line number | |
b8cbdf43 | 1050 | (self-insert-command (prefix-numeric-value arg)) |
3dd63760 JB |
1051 | (skip-chars-backward " \t") |
1052 | (insert last-command-char) | |
1053 | (fortran-indent-line)))) | |
5b04210c | 1054 | |
3dd63760 | 1055 | \f |
823ab5da DL |
1056 | (defun fortran-check-end-prog-re () |
1057 | "Check a preliminary match against `fortran-end-prog-re'." | |
1058 | ;; Having got a possible match for the subprogram end, we need a | |
1059 | ;; match of whitespace, avoiding possible column 73+ stuff. | |
1060 | (save-match-data | |
bd6cabcf | 1061 | (string-match "^\\s-*\\(\\'\\|\\s<\\)" |
823ab5da DL |
1062 | (buffer-substring (match-end 0) |
1063 | (min (line-end-position) | |
1064 | (+ 72 (line-beginning-position))))))) | |
1065 | ||
1066 | ;; Note that you can't just check backwards for `subroutine' &c in | |
1067 | ;; case of un-marked main programs not at the start of the file. | |
7dae727d | 1068 | (defun fortran-beginning-of-subprogram () |
b2523604 | 1069 | "Move point to the beginning of the current Fortran subprogram." |
3dd63760 | 1070 | (interactive) |
f687a879 DL |
1071 | (save-match-data |
1072 | (let ((case-fold-search t)) | |
1073 | (beginning-of-line -1) | |
1074 | (if (catch 'ok | |
1075 | (while (re-search-backward fortran-end-prog-re nil 'move) | |
1076 | (if (fortran-check-end-prog-re) | |
1077 | (throw 'ok t)))) | |
1078 | (forward-line))))) | |
3dd63760 | 1079 | |
7dae727d | 1080 | (defun fortran-end-of-subprogram () |
b2523604 | 1081 | "Move point to the end of the current Fortran subprogram." |
3dd63760 | 1082 | (interactive) |
f687a879 DL |
1083 | (save-match-data |
1084 | (let ((case-fold-search t)) | |
1085 | (if (save-excursion ; on END | |
1086 | (beginning-of-line) | |
1087 | (and (looking-at fortran-end-prog-re) | |
1088 | (fortran-check-end-prog-re))) | |
1089 | (forward-line) | |
1090 | (beginning-of-line 2) | |
1091 | (catch 'ok | |
1092 | (while (re-search-forward fortran-end-prog-re nil 'move) | |
1093 | (if (fortran-check-end-prog-re) | |
1094 | (throw 'ok t)))) | |
1095 | (goto-char (match-beginning 0)) | |
1096 | (forward-line))))) | |
3dd63760 | 1097 | |
3dd63760 | 1098 | (defun fortran-previous-statement () |
b2523604 | 1099 | "Move point to beginning of the previous Fortran statement. |
8edfcc7d GM |
1100 | Returns 'first-statement if that statement is the first |
1101 | non-comment Fortran statement in the file, and nil otherwise. | |
5a73972b | 1102 | Directive lines are treated as comments." |
3dd63760 JB |
1103 | (interactive) |
1104 | (let (not-first-statement continue-test) | |
1105 | (beginning-of-line) | |
1106 | (setq continue-test | |
b8cbdf43 | 1107 | (and |
7dae727d | 1108 | (not (looking-at fortran-comment-line-start-skip)) |
5a73972b | 1109 | (not (looking-at fortran-directive-re)) |
b8cbdf43 | 1110 | (or (looking-at |
315aa1de DL |
1111 | (concat "[ \t]*" |
1112 | (regexp-quote fortran-continuation-string))) | |
1113 | (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")))) | |
5b04210c | 1114 | (while (and (setq not-first-statement (zerop (forward-line -1))) |
7dae727d | 1115 | (or (looking-at fortran-comment-line-start-skip) |
5a73972b | 1116 | (looking-at fortran-directive-re) |
98110b1f GM |
1117 | (looking-at |
1118 | (concat "[ \t]*" | |
1119 | (regexp-quote fortran-continuation-string))) | |
315aa1de DL |
1120 | (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]") |
1121 | (looking-at (concat "[ \t]*" comment-start-skip))))) | |
3dd63760 JB |
1122 | (cond ((and continue-test |
1123 | (not not-first-statement)) | |
1124 | (message "Incomplete continuation statement.")) | |
60db3594 | 1125 | (continue-test |
3dd63760 JB |
1126 | (fortran-previous-statement)) |
1127 | ((not not-first-statement) | |
1128 | 'first-statement)))) | |
1129 | ||
1130 | (defun fortran-next-statement () | |
b2523604 | 1131 | "Move point to beginning of the next Fortran statement. |
8edfcc7d GM |
1132 | Returns 'last-statement if that statement is the last |
1133 | non-comment Fortran statement in the file, and nil otherwise. | |
5a73972b | 1134 | Directive lines are treated as comments." |
3dd63760 JB |
1135 | (interactive) |
1136 | (let (not-last-statement) | |
1137 | (beginning-of-line) | |
b8cbdf43 | 1138 | (while (and (setq not-last-statement |
5b04210c | 1139 | (and (zerop (forward-line 1)) |
b8cbdf43 | 1140 | (not (eobp)))) |
7dae727d | 1141 | (or (looking-at fortran-comment-line-start-skip) |
5a73972b | 1142 | (looking-at fortran-directive-re) |
7dae727d | 1143 | (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]") |
315aa1de | 1144 | (looking-at (concat "[ \t]*" comment-start-skip))))) |
3dd63760 JB |
1145 | (if (not not-last-statement) |
1146 | 'last-statement))) | |
5b04210c | 1147 | |
8cb8832f GM |
1148 | (defun fortran-looking-at-if-then () |
1149 | "Return non-nil if at the start of a line with an IF ... THEN statement." | |
1150 | ;; cf f90-looking-at-if-then. | |
1151 | (let ((p (point)) | |
1152 | (i (fortran-beginning-if))) | |
1153 | (if i | |
1154 | (save-excursion | |
1155 | (goto-char i) | |
1156 | (beginning-of-line) | |
1157 | (= (point) p))))) | |
1158 | ||
1159 | ;; Used in hs-special-modes-alist. | |
1160 | (defun fortran-end-of-block (&optional num) | |
1161 | "Move point forward to the end of the current code block. | |
1162 | With optional argument NUM, go forward that many balanced blocks. | |
1163 | If NUM is negative, go backward to the start of a block. Does | |
1164 | not check for consistency of block types. Interactively, pushes | |
1165 | mark before moving point." | |
1166 | (interactive "p") | |
1167 | (if (interactive-p) (push-mark (point) t)) | |
1168 | (and num (< num 0) (fortran-beginning-of-block (- num))) | |
1169 | (let ((case-fold-search t) | |
1170 | (count (or num 1))) | |
1171 | (end-of-line) | |
1172 | (while (and (> count 0) | |
1173 | (re-search-forward | |
1174 | (concat "\\(" fortran-blocks-re | |
1175 | (if fortran-check-all-num-for-matching-do | |
1176 | "\\|^[ \t]*[0-9]+" "") | |
1177 | "\\|continue\\|end\\)\\>") | |
1178 | nil 'move)) | |
1179 | (beginning-of-line) | |
1180 | (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re)) | |
1181 | (fortran-looking-at-if-then) | |
1182 | (looking-at fortran-start-block-re)) | |
1183 | (setq count (1+ count)) | |
1184 | (if (or (looking-at fortran-end-block-re) | |
1185 | (and (or (looking-at "^[0-9 \t]*continue") | |
1186 | (and fortran-check-all-num-for-matching-do | |
1187 | (looking-at "[ \t]*[0-9]+"))) | |
1188 | (fortran-check-for-matching-do))) | |
1189 | (setq count (1- count)))) | |
1190 | (end-of-line)) | |
1191 | (if (> count 0) (error "Missing block end")))) | |
1192 | ||
1193 | (defun fortran-beginning-of-block (&optional num) | |
1194 | "Move point backwards to the start of the current code block. | |
1195 | With optional argument NUM, go backward that many balanced | |
1196 | blocks. If NUM is negative, go forward to the end of a block. | |
1197 | Does not check for consistency of block types. Interactively, | |
1198 | pushes mark before moving point." | |
1199 | (interactive "p") | |
1200 | (if (interactive-p) (push-mark (point) t)) | |
1201 | (and num (< num 0) (fortran-end-of-block (- num))) | |
1202 | (let ((case-fold-search t) | |
1203 | (count (or num 1))) | |
1204 | (beginning-of-line) | |
1205 | (while (and (> count 0) | |
1206 | (re-search-backward | |
1207 | (concat "\\(" fortran-blocks-re | |
1208 | (if fortran-check-all-num-for-matching-do | |
1209 | "\\|^[ \t]*[0-9]+" "") | |
1210 | "\\|continue\\|end\\)\\>") | |
1211 | nil 'move)) | |
1212 | (beginning-of-line) | |
1213 | (if (if (looking-at (concat "^[0-9 \t]*" fortran-if-start-re)) | |
1214 | (fortran-looking-at-if-then) | |
1215 | (looking-at fortran-start-block-re)) | |
1216 | (setq count (1- count)) | |
1217 | (if (or (looking-at fortran-end-block-re) | |
1218 | (and (or (looking-at "^[0-9 \t]*continue") | |
1219 | (and fortran-check-all-num-for-matching-do | |
1220 | (looking-at "[ \t]*[0-9]+"))) | |
1221 | (fortran-check-for-matching-do))) | |
1222 | (setq count (1+ count))))) | |
1223 | ;; Includes an un-named main program block. | |
1224 | (if (> count 0) (error "Missing block start")))) | |
1225 | ||
3dd63760 | 1226 | \f |
7dae727d DL |
1227 | (defun fortran-blink-match (regex keyword find-begin) |
1228 | "From a line matching REGEX, blink matching KEYWORD statement line. | |
1229 | Use function FIND-BEGIN to match it." | |
1eb6bf70 | 1230 | (let ((top-of-window (window-start)) |
7dae727d | 1231 | (end-point (point)) |
1eb6bf70 | 1232 | (case-fold-search t) |
7dae727d | 1233 | matching |
1eb6bf70 | 1234 | message) |
5b04210c GM |
1235 | (when (save-excursion |
1236 | (beginning-of-line) | |
1237 | (skip-chars-forward " \t0-9") | |
1238 | (looking-at regex)) | |
1239 | (if (not (setq matching (funcall find-begin))) | |
1240 | (setq message (concat "No matching " keyword ".")) | |
1241 | (if (< matching top-of-window) | |
1242 | (save-excursion | |
1243 | (goto-char matching) | |
1244 | (beginning-of-line) | |
1245 | (setq message | |
1246 | (concat "Matches " | |
1247 | (buffer-substring (point) | |
1248 | (line-end-position))))))) | |
1249 | (if message | |
1250 | (message "%s" message) | |
1251 | (goto-char matching) | |
1252 | (sit-for 1) | |
1253 | (goto-char end-point))))) | |
7dae727d DL |
1254 | |
1255 | (defun fortran-blink-matching-if () | |
1256 | "From an ENDIF or ELSE statement, blink the matching IF statement." | |
1257 | (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b" | |
1258 | "if" #'fortran-beginning-if)) | |
947388af RS |
1259 | |
1260 | (defun fortran-blink-matching-do () | |
45cf60ae | 1261 | "From an ENDDO statement, blink the matching DO or DO WHILE statement." |
7dae727d | 1262 | (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do)) |
c5af0a18 RS |
1263 | |
1264 | (defun fortran-mark-do () | |
60db3594 | 1265 | "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning. |
c5af0a18 RS |
1266 | The marks are pushed." |
1267 | (interactive) | |
1268 | (let (enddo-point do-point) | |
1269 | (if (setq enddo-point (fortran-end-do)) | |
1270 | (if (not (setq do-point (fortran-beginning-do))) | |
1271 | (message "No matching do.") | |
c5af0a18 RS |
1272 | (goto-char enddo-point) |
1273 | (push-mark) | |
1274 | (goto-char do-point))))) | |
1275 | ||
1276 | (defun fortran-end-do () | |
45cf60ae DL |
1277 | "Search forward for first unmatched ENDDO. |
1278 | Return point or nil." | |
1eb6bf70 DL |
1279 | (let ((case-fold-search t)) |
1280 | (if (save-excursion (beginning-of-line) | |
1281 | (skip-chars-forward " \t0-9") | |
1282 | (looking-at "end[ \t]*do\\b")) | |
1283 | ;; Sitting on one. | |
1284 | (match-beginning 0) | |
1285 | ;; Search for one. | |
1286 | (save-excursion | |
1287 | (let ((count 1)) | |
5b04210c | 1288 | (while (and (not (zerop count)) |
1eb6bf70 | 1289 | (not (eq (fortran-next-statement) 'last-statement)) |
0a39a75c | 1290 | ;; Keep local to subprogram. |
45cf60ae DL |
1291 | (not (and (looking-at fortran-end-prog-re) |
1292 | (fortran-check-end-prog-re)))) | |
1eb6bf70 DL |
1293 | (skip-chars-forward " \t0-9") |
1294 | (cond ((looking-at "end[ \t]*do\\b") | |
1295 | (setq count (1- count))) | |
7dae727d DL |
1296 | ((looking-at |
1297 | "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]") | |
5b04210c GM |
1298 | (setq count (1+ count))))) |
1299 | (and (zerop count) | |
1eb6bf70 DL |
1300 | ;; All pairs accounted for. |
1301 | (point))))))) | |
c5af0a18 RS |
1302 | |
1303 | (defun fortran-beginning-do () | |
45cf60ae | 1304 | "Search backwards for first unmatched DO [WHILE]. |
845d331e GM |
1305 | Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)." |
1306 | (let ((case-fold-search t) | |
1307 | (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")) | |
7dae727d DL |
1308 | (if (save-excursion |
1309 | (beginning-of-line) | |
1310 | (skip-chars-forward " \t0-9") | |
845d331e | 1311 | (looking-at dostart-re)) |
1eb6bf70 DL |
1312 | ;; Sitting on one. |
1313 | (match-beginning 0) | |
1314 | ;; Search for one. | |
1315 | (save-excursion | |
1316 | (let ((count 1)) | |
5b04210c | 1317 | (while (and (not (zerop count)) |
1eb6bf70 | 1318 | (not (eq (fortran-previous-statement) 'first-statement)) |
0a39a75c | 1319 | ;; Keep local to subprogram. |
45cf60ae DL |
1320 | (not (and (looking-at fortran-end-prog-re) |
1321 | (fortran-check-end-prog-re)))) | |
1eb6bf70 | 1322 | (skip-chars-forward " \t0-9") |
845d331e | 1323 | (cond ((looking-at dostart-re) |
1eb6bf70 | 1324 | (setq count (1- count))) |
845d331e | 1325 | ;; Note labelled loop ends not considered. |
1eb6bf70 DL |
1326 | ((looking-at "end[ \t]*do\\b") |
1327 | (setq count (1+ count))))) | |
5b04210c | 1328 | (and (zerop count) |
1eb6bf70 DL |
1329 | ;; All pairs accounted for. |
1330 | (point))))))) | |
c5af0a18 RS |
1331 | |
1332 | (defun fortran-mark-if () | |
1333 | "Put mark at end of Fortran IF-ENDIF construct, point at beginning. | |
1334 | The marks are pushed." | |
1335 | (interactive) | |
1336 | (let (endif-point if-point) | |
1337 | (if (setq endif-point (fortran-end-if)) | |
1338 | (if (not (setq if-point (fortran-beginning-if))) | |
1339 | (message "No matching if.") | |
1340 | ;; Set mark, move point. | |
1341 | (goto-char endif-point) | |
1342 | (push-mark) | |
1343 | (goto-char if-point))))) | |
1344 | ||
1345 | (defun fortran-end-if () | |
45cf60ae DL |
1346 | "Search forwards for first unmatched ENDIF. |
1347 | Return point or nil." | |
1eb6bf70 DL |
1348 | (let ((case-fold-search t)) |
1349 | (if (save-excursion (beginning-of-line) | |
1350 | (skip-chars-forward " \t0-9") | |
1351 | (looking-at "end[ \t]*if\\b")) | |
1352 | ;; Sitting on one. | |
1353 | (match-beginning 0) | |
1354 | ;; Search for one. The point has been already been moved to first | |
1355 | ;; letter on line but this should not cause troubles. | |
1356 | (save-excursion | |
1357 | (let ((count 1)) | |
5b04210c | 1358 | (while (and (not (zerop count)) |
1eb6bf70 DL |
1359 | (not (eq (fortran-next-statement) 'last-statement)) |
1360 | ;; Keep local to subprogram. | |
45cf60ae DL |
1361 | (not (and (looking-at fortran-end-prog-re) |
1362 | (fortran-check-end-prog-re)))) | |
1eb6bf70 DL |
1363 | (skip-chars-forward " \t0-9") |
1364 | (cond ((looking-at "end[ \t]*if\\b") | |
5b04210c | 1365 | (setq count (1- count))) |
1eb6bf70 DL |
1366 | ((looking-at fortran-if-start-re) |
1367 | (save-excursion | |
1368 | (if (or | |
1369 | (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]") | |
0a39a75c | 1370 | (let (then-test) ; multi-line if-then |
1eb6bf70 | 1371 | (while |
315aa1de | 1372 | (and |
5b04210c | 1373 | (zerop (forward-line 1)) |
315aa1de DL |
1374 | ;; Search forward for then. |
1375 | (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]") | |
1376 | (not | |
1377 | (setq then-test | |
1378 | (looking-at | |
1379 | ".*then\\b[ \t]*[^ \t(=a-z0-9]"))))) | |
1eb6bf70 | 1380 | then-test)) |
5b04210c GM |
1381 | (setq count (1+ count))))))) |
1382 | (and (zerop count) | |
1eb6bf70 DL |
1383 | ;; All pairs accounted for. |
1384 | (point))))))) | |
c5af0a18 RS |
1385 | |
1386 | (defun fortran-beginning-if () | |
45cf60ae DL |
1387 | "Search backwards for first unmatched IF-THEN. |
1388 | Return point or nil." | |
1eb6bf70 DL |
1389 | (let ((case-fold-search t)) |
1390 | (if (save-excursion | |
7dae727d DL |
1391 | ;; May be sitting on multi-line if-then statement, first |
1392 | ;; move to beginning of current statement. Note: | |
1393 | ;; `fortran-previous-statement' moves to previous statement | |
1394 | ;; *unless* current statement is first one. Only move | |
1395 | ;; forward if not first-statement. | |
1eb6bf70 DL |
1396 | (if (not (eq (fortran-previous-statement) 'first-statement)) |
1397 | (fortran-next-statement)) | |
1398 | (skip-chars-forward " \t0-9") | |
1399 | (and | |
1400 | (looking-at fortran-if-start-re) | |
1401 | (save-match-data | |
1402 | (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]") | |
1403 | ;; Multi-line if-then. | |
1404 | (let (then-test) | |
1405 | (while | |
5b04210c | 1406 | (and (zerop (forward-line 1)) |
1eb6bf70 | 1407 | ;; Search forward for then. |
315aa1de | 1408 | (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]") |
1eb6bf70 DL |
1409 | (not |
1410 | (setq then-test | |
1411 | (looking-at | |
1412 | ".*then\\b[ \t]*[^ \t(=a-z0-9]"))))) | |
1413 | then-test))))) | |
1414 | ;; Sitting on one. | |
1415 | (match-beginning 0) | |
1416 | ;; Search for one. | |
1417 | (save-excursion | |
1418 | (let ((count 1)) | |
5b04210c | 1419 | (while (and (not (zerop count)) |
1eb6bf70 DL |
1420 | (not (eq (fortran-previous-statement) 'first-statement)) |
1421 | ;; Keep local to subprogram. | |
45cf60ae DL |
1422 | (not (and (looking-at fortran-end-prog-re) |
1423 | (fortran-check-end-prog-re)))) | |
1eb6bf70 DL |
1424 | (skip-chars-forward " \t0-9") |
1425 | (cond ((looking-at fortran-if-start-re) | |
1426 | (save-excursion | |
1427 | (if (or | |
1428 | (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]") | |
0a39a75c | 1429 | (let (then-test) ; multi-line if-then |
1eb6bf70 | 1430 | (while |
315aa1de | 1431 | (and |
5b04210c | 1432 | (zerop (forward-line 1)) |
315aa1de DL |
1433 | ;; Search forward for then. |
1434 | (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]") | |
1435 | (not | |
1436 | (setq then-test | |
1437 | (looking-at | |
1438 | (concat ".*then\\b[ \t]*" | |
b2523604 | 1439 | "[^ \t(=a-z0-9]")))))) |
1eb6bf70 | 1440 | then-test)) |
5b04210c | 1441 | (setq count (1- count))))) |
1eb6bf70 | 1442 | ((looking-at "end[ \t]*if\\b") |
5b04210c GM |
1443 | (setq count (1+ count))))) |
1444 | (and (zerop count) | |
1eb6bf70 DL |
1445 | ;; All pairs accounted for. |
1446 | (point))))))) | |
5b04210c | 1447 | |
3dd63760 JB |
1448 | \f |
1449 | (defun fortran-indent-line () | |
7977773d | 1450 | "Indent current Fortran line based on its contents and on previous lines." |
5b04210c | 1451 | (interactive "*") |
1eb6bf70 | 1452 | (let ((cfi (fortran-calculate-indent))) |
3dd63760 JB |
1453 | (save-excursion |
1454 | (beginning-of-line) | |
1455 | (if (or (not (= cfi (fortran-current-line-indentation))) | |
1456 | (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t) | |
1457 | (not (fortran-line-number-indented-correctly-p)))) | |
1458 | (fortran-indent-to-column cfi) | |
1459 | (beginning-of-line) | |
ff451e17 | 1460 | (if (fortran-find-comment-start-skip) |
3dd63760 JB |
1461 | (fortran-indent-comment)))) |
1462 | ;; Never leave point in left margin. | |
1463 | (if (< (current-column) cfi) | |
1464 | (move-to-column cfi)) | |
5b04210c GM |
1465 | (and auto-fill-function |
1466 | (> (save-excursion (end-of-line) (current-column)) | |
1467 | fill-column) | |
1468 | (save-excursion | |
1469 | (end-of-line) | |
1470 | (fortran-fill))) | |
1471 | (when fortran-blink-matching-if | |
1472 | (fortran-blink-matching-if) | |
1473 | (fortran-blink-matching-do)))) | |
3dd63760 | 1474 | |
315aa1de | 1475 | (defun fortran-auto-fill () |
5b04210c | 1476 | "Function to use for `normal-auto-fill-function' in Fortran mode." |
315aa1de DL |
1477 | (if (> (current-column) (current-fill-column)) |
1478 | (let ((cfi (fortran-calculate-indent))) | |
1479 | (save-excursion | |
1480 | (beginning-of-line) | |
1481 | (if (or (not (= cfi (fortran-current-line-indentation))) | |
1482 | (and (re-search-forward "^[ \t]*[0-9]+" | |
1483 | (+ (point) 4) t) | |
1484 | (not (fortran-line-number-indented-correctly-p)))) | |
1485 | (fortran-indent-to-column cfi) | |
1486 | (beginning-of-line) | |
ff451e17 | 1487 | (if (fortran-find-comment-start-skip) |
315aa1de DL |
1488 | (fortran-indent-comment)))) |
1489 | (fortran-fill) | |
1490 | ;; Never leave point in left margin. | |
1491 | (if (< (current-column) cfi) | |
1492 | (move-to-column cfi))))) | |
1493 | ||
5aca2648 DL |
1494 | ;; Historically this was a separate function which advertised itself |
1495 | ;; as reindenting but only did so where `most likely to be necessary'. | |
1496 | (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent) | |
b8cbdf43 | 1497 | |
3dd63760 | 1498 | (defun fortran-indent-subprogram () |
5b04210c GM |
1499 | "Properly indent the Fortran subprogram containing point." |
1500 | (interactive "*") | |
3dd63760 | 1501 | (save-excursion |
7a7db8e5 | 1502 | (mark-defun) |
c4c42b2e DL |
1503 | (message "Indenting subprogram...") |
1504 | (indent-region (point) (mark) nil)) | |
3dd63760 JB |
1505 | (message "Indenting subprogram...done.")) |
1506 | ||
1eb6bf70 | 1507 | (defun fortran-calculate-indent () |
b8cbdf43 | 1508 | "Calculates the Fortran indent column based on previous lines." |
3dd63760 JB |
1509 | (let (icol first-statement (case-fold-search t) |
1510 | (fortran-minimum-statement-indent | |
23029d77 JB |
1511 | (if indent-tabs-mode |
1512 | fortran-minimum-statement-indent-tab | |
1513 | fortran-minimum-statement-indent-fixed))) | |
3dd63760 JB |
1514 | (save-excursion |
1515 | (setq first-statement (fortran-previous-statement)) | |
1516 | (if first-statement | |
1517 | (setq icol fortran-minimum-statement-indent) | |
98110b1f GM |
1518 | (if (= (point) (point-min)) |
1519 | (setq icol fortran-minimum-statement-indent) | |
1520 | (setq icol (fortran-current-line-indentation))) | |
1521 | (skip-chars-forward " \t0-9") | |
1522 | (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(") | |
1523 | (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]") | |
0a39a75c | 1524 | (let (then-test) ; multi-line if-then |
5b04210c | 1525 | (while (and (zerop (forward-line 1)) |
0a39a75c | 1526 | ;; Search forward for then. |
98110b1f GM |
1527 | (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]") |
1528 | (not (setq then-test | |
1529 | (looking-at | |
1530 | ".*then\\b[ \t]\ | |
b8cbdf43 | 1531 | *[^ \t_$(=a-z0-9]"))))) |
98110b1f GM |
1532 | then-test)) |
1533 | (setq icol (+ icol fortran-if-indent)))) | |
1534 | ((looking-at "else\\(if\\)?\\b") | |
1535 | (setq icol (+ icol fortran-if-indent))) | |
1536 | ((looking-at "select[ \t]*case[ \t](.*)") | |
1537 | (setq icol (+ icol fortran-if-indent))) | |
1538 | ((looking-at "case[ \t]*(.*)") | |
1539 | (setq icol (+ icol fortran-if-indent))) | |
1540 | ((looking-at "case[ \t]*default\\b") | |
1541 | (setq icol (+ icol fortran-if-indent))) | |
1542 | ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b") | |
1543 | (setq icol (+ icol fortran-if-indent))) | |
1544 | ((looking-at "where[ \t]*(.*)[ \t]*\n") | |
1545 | (setq icol (+ icol fortran-if-indent))) | |
1546 | ((looking-at "do\\b") | |
1547 | (setq icol (+ icol fortran-do-indent))) | |
1548 | ((looking-at | |
1549 | "\\(structure\\|union\\|map\\|interface\\)\ | |
7dae727d | 1550 | \\b[ \t]*[^ \t=(a-z]") |
98110b1f GM |
1551 | (setq icol (+ icol fortran-structure-indent))) |
1552 | ((and (looking-at fortran-end-prog-re1) | |
1553 | (fortran-check-end-prog-re)) | |
0a39a75c | 1554 | ;; Previous END resets indent to minimum. |
98110b1f | 1555 | (setq icol fortran-minimum-statement-indent))))) |
3dd63760 JB |
1556 | (save-excursion |
1557 | (beginning-of-line) | |
1558 | (cond ((looking-at "[ \t]*$")) | |
5a73972b GM |
1559 | ;; Check for directive before comment, so as not to indent. |
1560 | ((looking-at fortran-directive-re) | |
1561 | (setq fortran-minimum-statement-indent 0 icol 0)) | |
7dae727d | 1562 | ((looking-at fortran-comment-line-start-skip) |
3dd63760 | 1563 | (cond ((eq fortran-comment-indent-style 'relative) |
23029d77 | 1564 | (setq icol (+ icol fortran-comment-line-extra-indent))) |
3dd63760 | 1565 | ((eq fortran-comment-indent-style 'fixed) |
b8cbdf43 | 1566 | (setq icol (+ fortran-minimum-statement-indent |
23029d77 | 1567 | fortran-comment-line-extra-indent)))) |
b8cbdf43 | 1568 | (setq fortran-minimum-statement-indent 0)) |
98110b1f | 1569 | ((or (looking-at (concat "[ \t]*" |
b8cbdf43 RS |
1570 | (regexp-quote |
1571 | fortran-continuation-string))) | |
315aa1de | 1572 | (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")) |
98110b1f GM |
1573 | (skip-chars-forward " \t") |
1574 | ;; Do not introduce extra whitespace into a broken string. | |
1575 | (setq icol | |
1576 | (if (fortran-is-in-string-p (point)) | |
1577 | 6 | |
1578 | (+ icol fortran-continuation-indent)))) | |
3dd63760 JB |
1579 | (first-statement) |
1580 | ((and fortran-check-all-num-for-matching-do | |
1581 | (looking-at "[ \t]*[0-9]+") | |
1582 | (fortran-check-for-matching-do)) | |
1583 | (setq icol (- icol fortran-do-indent))) | |
1584 | (t | |
1585 | (skip-chars-forward " \t0-9") | |
1eb6bf70 | 1586 | (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b") |
3dd63760 | 1587 | (setq icol (- icol fortran-if-indent))) |
1eb6bf70 | 1588 | ((looking-at "else\\(if\\)?\\b") |
5a8d870b | 1589 | (setq icol (- icol fortran-if-indent))) |
1eb6bf70 | 1590 | ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)") |
5a8d870b | 1591 | (setq icol (- icol fortran-if-indent))) |
b8cbdf43 RS |
1592 | ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b") |
1593 | (setq icol (- icol fortran-if-indent))) | |
3dd63760 JB |
1594 | ((and (looking-at "continue\\b") |
1595 | (fortran-check-for-matching-do)) | |
1596 | (setq icol (- icol fortran-do-indent))) | |
1597 | ((looking-at "end[ \t]*do\\b") | |
1598 | (setq icol (- icol fortran-do-indent))) | |
1eb6bf70 | 1599 | ((looking-at "end[ \t]*\ |
5a8d870b | 1600 | \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]") |
b8cbdf43 | 1601 | (setq icol (- icol fortran-structure-indent))) |
1eb6bf70 | 1602 | ((and (looking-at fortran-end-prog-re1) |
45cf60ae | 1603 | (fortran-check-end-prog-re) |
3dd63760 JB |
1604 | (not (= icol fortran-minimum-statement-indent))) |
1605 | (message "Warning: `end' not in column %d. Probably\ | |
1606 | an unclosed block." fortran-minimum-statement-indent)))))) | |
1607 | (max fortran-minimum-statement-indent icol))) | |
5b04210c | 1608 | |
3dd63760 JB |
1609 | \f |
1610 | (defun fortran-current-line-indentation () | |
1611 | "Indentation of current line, ignoring Fortran line number or continuation. | |
1612 | This is the column position of the first non-whitespace character | |
1613 | aside from the line number and/or column 5/8 line-continuation character. | |
1614 | For comment lines, returns indentation of the first | |
1615 | non-indentation text within the comment." | |
1616 | (save-excursion | |
1617 | (beginning-of-line) | |
7dae727d | 1618 | (cond ((looking-at fortran-comment-line-start-skip) |
3dd63760 JB |
1619 | (goto-char (match-end 0)) |
1620 | (skip-chars-forward | |
b8cbdf43 RS |
1621 | (if (stringp fortran-comment-indent-char) |
1622 | fortran-comment-indent-char | |
1623 | (char-to-string fortran-comment-indent-char)))) | |
315aa1de | 1624 | ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")) |
3dd63760 JB |
1625 | (goto-char (match-end 0))) |
1626 | (t | |
1627 | ;; Move past line number. | |
7dae727d | 1628 | (skip-chars-forward "[ \t0-9]"))) |
3dd63760 JB |
1629 | ;; Move past whitespace. |
1630 | (skip-chars-forward " \t") | |
1631 | (current-column))) | |
1632 | ||
1633 | (defun fortran-indent-to-column (col) | |
5b04210c | 1634 | "Indent current line to column COL. |
3dd63760 JB |
1635 | notes: 1) A non-zero/non-blank character in column 5 indicates a continuation |
1636 | line, and this continuation character is retained on indentation; | |
b8cbdf43 RS |
1637 | 2) If `fortran-continuation-string' is the first non-whitespace |
1638 | character, this is a continuation line; | |
3dd63760 JB |
1639 | 3) A non-continuation line which has a number as the first |
1640 | non-whitespace character is a numbered line. | |
b8cbdf43 | 1641 | 4) A TAB followed by a digit indicates a continuation line." |
3dd63760 JB |
1642 | (save-excursion |
1643 | (beginning-of-line) | |
7dae727d | 1644 | (if (looking-at fortran-comment-line-start-skip) |
3dd63760 | 1645 | (if fortran-comment-indent-style |
7dae727d DL |
1646 | (let* ((char (if (stringp fortran-comment-indent-char) |
1647 | (aref fortran-comment-indent-char 0) | |
1648 | fortran-comment-indent-char)) | |
1649 | (chars (string ? ?\t char))) | |
3dd63760 | 1650 | (goto-char (match-end 0)) |
7dae727d DL |
1651 | (skip-chars-backward chars) |
1652 | (delete-region (point) (progn (skip-chars-forward chars) | |
1653 | (point))) | |
3dd63760 JB |
1654 | (insert-char char (- col (current-column))))) |
1655 | (if (looking-at "\t[1-9]") | |
23029d77 | 1656 | (if indent-tabs-mode |
3dd63760 JB |
1657 | (goto-char (match-end 0)) |
1658 | (delete-char 2) | |
7dae727d | 1659 | (insert-char ? 5) |
3dd63760 | 1660 | (insert fortran-continuation-string)) |
315aa1de | 1661 | (if (looking-at " \\{5\\}[^ 0\n]") |
23029d77 | 1662 | (if indent-tabs-mode |
3dd63760 | 1663 | (progn (delete-char 6) |
7dae727d | 1664 | (insert ?\t (fortran-numerical-continuation-char) 1)) |
3dd63760 JB |
1665 | (forward-char 6)) |
1666 | (delete-horizontal-space) | |
0a39a75c GM |
1667 | ;; Put line number in columns 0-4, or |
1668 | ;; continuation character in column 5. | |
3dd63760 JB |
1669 | (cond ((eobp)) |
1670 | ((looking-at (regexp-quote fortran-continuation-string)) | |
23029d77 | 1671 | (if indent-tabs-mode |
3dd63760 | 1672 | (progn |
60db3594 | 1673 | (indent-to |
23029d77 JB |
1674 | (if indent-tabs-mode |
1675 | fortran-minimum-statement-indent-tab | |
1676 | fortran-minimum-statement-indent-fixed)) | |
3dd63760 JB |
1677 | (delete-char 1) |
1678 | (insert-char (fortran-numerical-continuation-char) 1)) | |
b8cbdf43 RS |
1679 | (indent-to 5) |
1680 | (forward-char 1))) | |
3dd63760 JB |
1681 | ((looking-at "[0-9]+") |
1682 | (let ((extra-space (- 5 (- (match-end 0) (point))))) | |
1683 | (if (< extra-space 0) | |
1684 | (message "Warning: line number exceeds 5-digit limit.") | |
1685 | (indent-to (min fortran-line-number-indent extra-space)))) | |
1686 | (skip-chars-forward "0-9"))))) | |
1687 | ;; Point is now after any continuation character or line number. | |
1688 | ;; Put body of statement where specified. | |
1689 | (delete-horizontal-space) | |
1690 | (indent-to col) | |
1691 | ;; Indent any comment following code on the same line. | |
5b04210c GM |
1692 | (when (fortran-find-comment-start-skip) |
1693 | (goto-char (match-beginning 0)) | |
1694 | (unless (= (current-column) (fortran-comment-indent)) | |
1695 | (delete-horizontal-space) | |
1696 | (indent-to (fortran-comment-indent))))))) | |
3dd63760 JB |
1697 | |
1698 | (defun fortran-line-number-indented-correctly-p () | |
1699 | "Return t if current line's line number is correctly indented. | |
1700 | Do not call if there is no line number." | |
1701 | (save-excursion | |
1702 | (beginning-of-line) | |
1703 | (skip-chars-forward " \t") | |
1704 | (and (<= (current-column) fortran-line-number-indent) | |
1705 | (or (= (current-column) fortran-line-number-indent) | |
1706 | (progn (skip-chars-forward "0-9") | |
1707 | (= (current-column) 5)))))) | |
1708 | ||
1709 | (defun fortran-check-for-matching-do () | |
7977773d DL |
1710 | "When called from a numbered statement, return t if matching DO is found. |
1711 | Otherwise return nil." | |
7dae727d DL |
1712 | (let ((case-fold-search t) |
1713 | charnum) | |
3dd63760 JB |
1714 | (save-excursion |
1715 | (beginning-of-line) | |
5b04210c GM |
1716 | (when (looking-at "[ \t]*[0-9]+") |
1717 | (skip-chars-forward " \t") | |
0a39a75c | 1718 | (skip-chars-forward "0") ; skip past leading zeros |
5b04210c GM |
1719 | (setq charnum |
1720 | (buffer-substring (point) (progn | |
1721 | (skip-chars-forward "0-9") | |
1722 | (point)))) | |
1723 | (beginning-of-line) | |
1724 | (save-restriction | |
1725 | (save-excursion | |
1726 | (narrow-to-defun) | |
1727 | (and (re-search-backward | |
1728 | (concat | |
1729 | "\\(^[ \t0-9]*do[ \t]*0*" | |
1730 | charnum "\\b\\)\\|" "\\(^[ \t]*0*" | |
1731 | charnum "\\b\\)") | |
1732 | nil t) | |
1733 | (looking-at | |
1734 | (concat "^[ \t0-9]*do[ \t]*0*" | |
1735 | charnum))))))))) | |
3dd63760 | 1736 | |
ff451e17 | 1737 | (defun fortran-find-comment-start-skip (&optional all) |
b8cbdf43 | 1738 | "Move to past `comment-start-skip' found on current line. |
ff451e17 SM |
1739 | Return non-nil if `comment-start-skip' found, nil if not. |
1740 | If ALL is nil, only match comments that start in column > 0." | |
ff451e17 SM |
1741 | ;; Hopefully at some point we can just use the line below! -stef |
1742 | ;; (comment-search-forward (line-end-position) t)) | |
1743 | (when (or all comment-start-skip) | |
1744 | (let ((pos (point)) | |
1745 | (css (if comment-start-skip | |
1746 | (concat fortran-comment-line-start-skip | |
1747 | "\\|" comment-start-skip) | |
1748 | fortran-comment-line-start-skip))) | |
1749 | (when (re-search-forward css (line-end-position) t) | |
1750 | (if (and (or all (> (match-beginning 0) (line-beginning-position))) | |
1751 | (or (save-match-data | |
1752 | (not (fortran-is-in-string-p (match-beginning 0)))) | |
1753 | ;; Recurse for rest of line. | |
1754 | (fortran-find-comment-start-skip all))) | |
1755 | (point) | |
1756 | (goto-char pos) | |
1757 | nil))))) | |
f022dd89 | 1758 | |
5b04210c | 1759 | ;; From: ralf@up3aud1.gwdg.de (Ralf Fassel) |
1eb6bf70 | 1760 | ;; Test if TAB format continuation lines work. |
b56eb7c9 | 1761 | (defun fortran-is-in-string-p (where) |
7977773d | 1762 | "Return non-nil iff WHERE (a buffer position) is inside a Fortran string." |
b56eb7c9 RS |
1763 | (save-excursion |
1764 | (goto-char where) | |
1765 | (cond | |
1766 | ((bolp) nil) ; bol is never inside a string | |
1767 | ((save-excursion ; comment lines too | |
1eb6bf70 | 1768 | (beginning-of-line) |
7dae727d | 1769 | (looking-at fortran-comment-line-start-skip)) nil) |
0a39a75c | 1770 | (t (let ((parse-state '(0 nil nil nil nil nil 0)) |
b56eb7c9 RS |
1771 | (quoted-comment-start (if comment-start |
1772 | (regexp-quote comment-start))) | |
1773 | (not-done t) | |
7dae727d | 1774 | parse-limit end-of-line) |
0a39a75c | 1775 | ;; Move to start of current statement. |
b56eb7c9 RS |
1776 | (fortran-next-statement) |
1777 | (fortran-previous-statement) | |
0a39a75c | 1778 | ;; Now parse up to WHERE. |
b56eb7c9 | 1779 | (while not-done |
0a39a75c | 1780 | (if (or ;; Skip to next line if: |
b56eb7c9 | 1781 | ;; - comment line? |
7dae727d | 1782 | (looking-at fortran-comment-line-start-skip) |
b56eb7c9 RS |
1783 | ;; - at end of line? |
1784 | (eolp) | |
1785 | ;; - not in a string and after comment-start? | |
1786 | (and (not (nth 3 parse-state)) | |
1787 | comment-start | |
1788 | (equal comment-start | |
1789 | (char-to-string (preceding-char))))) | |
1eb6bf70 | 1790 | (if (> (forward-line) 0) |
b56eb7c9 RS |
1791 | (setq not-done nil)) |
1792 | ;; else: | |
0a39a75c | 1793 | ;; If we are at beginning of code line, skip any |
b56eb7c9 RS |
1794 | ;; whitespace, labels and tab continuation markers. |
1795 | (if (bolp) (skip-chars-forward " \t0-9")) | |
0a39a75c | 1796 | ;; If we are in column <= 5 now, check for continuation char. |
b56eb7c9 RS |
1797 | (cond ((= 5 (current-column)) (forward-char 1)) |
1798 | ((and (< (current-column) 5) | |
1799 | (equal fortran-continuation-string | |
1800 | (char-to-string (following-char))) | |
1801 | (forward-char 1)))) | |
0a39a75c | 1802 | ;; Find out parse-limit from here. |
7dae727d | 1803 | (setq end-of-line (line-end-position)) |
b56eb7c9 | 1804 | (setq parse-limit (min where end-of-line)) |
0a39a75c | 1805 | ;; Parse max up to comment-start, if non-nil and in current line. |
b56eb7c9 RS |
1806 | (if comment-start |
1807 | (save-excursion | |
1808 | (if (re-search-forward quoted-comment-start end-of-line t) | |
1809 | (setq parse-limit (min (point) parse-limit))))) | |
0a39a75c | 1810 | ;; Now parse if still in limits. |
b56eb7c9 RS |
1811 | (if (< (point) where) |
1812 | (setq parse-state (parse-partial-sexp | |
1813 | (point) parse-limit nil nil parse-state)) | |
7dae727d | 1814 | (setq not-done nil)))) |
0a39a75c | 1815 | ;; Result. |
b56eb7c9 | 1816 | (nth 3 parse-state)))))) |
b8cbdf43 | 1817 | |
7aabd23e DL |
1818 | ;; From old version. |
1819 | (defalias 'fortran-auto-fill-mode 'auto-fill-mode) | |
b8cbdf43 | 1820 | |
4254fe58 | 1821 | (defun fortran-fill () |
5b04210c | 1822 | "Fill the current line at an appropriate point(s)." |
315aa1de | 1823 | (let* ((auto-fill-function #'fortran-auto-fill) |
e04196d3 | 1824 | (opoint (point)) |
7dae727d DL |
1825 | (bol (line-beginning-position)) |
1826 | (eol (line-end-position)) | |
b8cbdf43 | 1827 | (bos (min eol (+ bol (fortran-current-line-indentation)))) |
337c50a5 GM |
1828 | ;; If in a string at fill-column, break it either before the |
1829 | ;; initial quote, or at fill-col (if string is too long). | |
b8cbdf43 RS |
1830 | (quote |
1831 | (save-excursion | |
1832 | (goto-char bol) | |
98110b1f GM |
1833 | ;; OK to break quotes on comment lines. |
1834 | (unless (looking-at fortran-comment-line-start-skip) | |
1835 | (let (fcpoint start) | |
337c50a5 GM |
1836 | (move-to-column fill-column) |
1837 | (when (fortran-is-in-string-p (setq fcpoint (point))) | |
1838 | (save-excursion | |
1839 | (re-search-backward "\\S\"\\s\"\\S\"?" bol t) | |
1840 | (setq start | |
1841 | (if fortran-break-before-delimiters | |
1842 | (point) | |
1843 | (1+ (point))))) | |
1844 | (if (re-search-forward "\\S\"\\s\"\\S\"" eol t) | |
1845 | (backward-char 2)) | |
8cb8832f GM |
1846 | ;; If the current string is longer than (fill-column |
1847 | ;; - 6) chars, break it at the fill column (else | |
1848 | ;; infinite loop). | |
337c50a5 GM |
1849 | (if (> (- (point) start) |
1850 | (- fill-column 6 fortran-continuation-indent)) | |
1851 | fcpoint | |
1852 | start)))))) | |
5b04210c | 1853 | ;; Decide where to split the line. If a position for a quoted |
b8cbdf43 | 1854 | ;; string was found above then use that, else break the line |
337c50a5 | 1855 | ;; before/after the last delimiter. |
b8cbdf43 RS |
1856 | (fill-point |
1857 | (or quote | |
1858 | (save-excursion | |
337c50a5 GM |
1859 | ;; If f-b-b-d is t, have an extra column to play with, |
1860 | ;; since delimiter gets shifted to new line. | |
1861 | (move-to-column (if fortran-break-before-delimiters | |
1862 | (1+ fill-column) | |
1863 | fill-column)) | |
1864 | (let ((repeat t)) | |
1865 | (while repeat | |
1866 | (setq repeat nil) | |
1867 | ;; Adapted from f90-find-breakpoint. | |
8aa7b879 | 1868 | (re-search-backward fortran-break-delimiters-re bol) |
337c50a5 GM |
1869 | (if (not fortran-break-before-delimiters) |
1870 | (if (looking-at fortran-no-break-re) | |
1871 | ;; Deal with cases such as "**" split over | |
1872 | ;; fill-col. Simpler alternative would be | |
1873 | ;; to start from (1- fill-column) above. | |
1874 | (if (> (+ 2 (current-column)) fill-column) | |
1875 | (setq repeat t) | |
1876 | (forward-char 2)) | |
1877 | (forward-char 1)) | |
1878 | (backward-char) | |
1879 | (or (looking-at fortran-no-break-re) | |
1880 | (forward-char))))) | |
1881 | ;; Line indented beyond fill-column? | |
1882 | (when (<= (point) bos) | |
98110b1f | 1883 | (move-to-column (1+ fill-column)) |
0a39a75c | 1884 | ;; What is this doing??? |
98110b1f GM |
1885 | (or (re-search-forward "[\t\n,'+-/*)=]" eol t) |
1886 | (goto-char bol))) | |
b8cbdf43 | 1887 | (if (bolp) |
337c50a5 GM |
1888 | (re-search-forward "[ \t]" opoint t)) |
1889 | (point))))) | |
0a39a75c | 1890 | ;; If we are in an in-line comment, don't break unless the |
b8cbdf43 RS |
1891 | ;; line of code is longer than it should be. Otherwise |
1892 | ;; break the line at the column computed above. | |
1893 | ;; | |
0a39a75c GM |
1894 | ;; Need to use fortran-find-comment-start-skip to make sure that |
1895 | ;; quoted !'s don't prevent a break. | |
ff451e17 SM |
1896 | (when (and (save-excursion |
1897 | (beginning-of-line) | |
0ab47edc GM |
1898 | (if (not (fortran-find-comment-start-skip)) |
1899 | t | |
ff451e17 SM |
1900 | (goto-char (match-beginning 0)) |
1901 | (>= (point) fill-point))) | |
1902 | (save-excursion | |
1903 | (goto-char fill-point) | |
1904 | (not (bolp))) | |
1905 | (> (save-excursion | |
1906 | (goto-char opoint) | |
1907 | (current-column)) | |
1908 | (min (1+ fill-column) | |
1909 | (+ (fortran-calculate-indent) | |
1910 | fortran-continuation-indent)))) | |
1911 | (goto-char fill-point) | |
1912 | (fortran-break-line) | |
1913 | (end-of-line)))) | |
7dae727d | 1914 | |
b8cbdf43 | 1915 | (defun fortran-break-line () |
5b04210c | 1916 | "Call `fortran-split-line'. Joins continuation lines first, then refills." |
b8cbdf43 | 1917 | (let ((opoint (point)) |
7dae727d | 1918 | (bol (line-beginning-position)) |
ff451e17 SM |
1919 | (comment-string |
1920 | (save-excursion | |
1921 | (if (fortran-find-comment-start-skip) | |
1922 | (delete-and-extract-region | |
1923 | (match-beginning 0) (line-end-position)))))) | |
0a39a75c | 1924 | ;; Forward line 1 really needs to go to next non white line. |
1eb6bf70 | 1925 | (if (save-excursion (forward-line) |
ff451e17 | 1926 | (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")) |
b8cbdf43 | 1927 | (progn |
b8313955 RS |
1928 | (end-of-line) |
1929 | (delete-region (point) (match-end 0)) | |
b8cbdf43 | 1930 | (delete-horizontal-space) |
4254fe58 | 1931 | (fortran-fill)) |
b8cbdf43 RS |
1932 | (fortran-split-line)) |
1933 | (if comment-string | |
1934 | (save-excursion | |
1935 | (goto-char bol) | |
1936 | (end-of-line) | |
1937 | (delete-horizontal-space) | |
315aa1de | 1938 | (indent-to (fortran-comment-indent)) |
b8cbdf43 RS |
1939 | (insert comment-string))))) |
1940 | ||
23029d77 | 1941 | (defun fortran-analyze-file-format () |
7977773d | 1942 | "Return nil if fixed format is used, t if TAB formatting is used. |
5b04210c GM |
1943 | Use `fortran-tab-mode-default' if no non-comment statements are found |
1944 | before the end or in the first `fortran-analyze-depth' lines." | |
23029d77 JB |
1945 | (let ((i 0)) |
1946 | (save-excursion | |
1947 | (goto-char (point-min)) | |
23029d77 JB |
1948 | (while (not (or |
1949 | (eobp) | |
7dae727d | 1950 | (eq (char-after) ?\t) |
315aa1de | 1951 | (looking-at " \\{6\\}") |
23029d77 JB |
1952 | (> i fortran-analyze-depth))) |
1953 | (forward-line) | |
1954 | (setq i (1+ i))) | |
1955 | (cond | |
7dae727d | 1956 | ((eq (char-after) ?\t) t) |
315aa1de | 1957 | ((looking-at " \\{6\\}") nil) |
a7113309 | 1958 | (t fortran-tab-mode-default))))) |
23029d77 | 1959 | |
1eb6bf70 DL |
1960 | (defun fortran-fill-paragraph (&optional justify) |
1961 | "Fill surrounding comment block as paragraphs, else fill statement. | |
5b04210c GM |
1962 | Intended as the value of `fill-paragraph-function'. |
1963 | A comment block is filled by calling `fill-comment-paragraph' with | |
1964 | argument JUSTIFY, otherwise `fortran-fill-statement' is called. | |
1965 | Always returns non-nil (to prevent `fill-paragraph' being called)." | |
1966 | (interactive "*P") | |
2bcfe15e SM |
1967 | (or (fill-comment-paragraph justify) |
1968 | (fortran-fill-statement) | |
1969 | t)) | |
1eb6bf70 DL |
1970 | |
1971 | (defun fortran-fill-statement () | |
5b04210c GM |
1972 | "Fill a Fortran statement up to `fill-column'." |
1973 | (interactive "*") | |
315aa1de | 1974 | (let ((auto-fill-function #'fortran-auto-fill)) |
5b04210c GM |
1975 | (unless (save-excursion |
1976 | (beginning-of-line) | |
1977 | (or (looking-at "[ \t]*$") | |
1978 | (looking-at fortran-comment-line-start-skip) | |
1979 | (and comment-start-skip | |
1980 | (looking-at (concat "[ \t]*" comment-start-skip))))) | |
1981 | (save-excursion | |
1982 | ;; Find beginning of statement. | |
1983 | (fortran-next-statement) | |
1984 | (fortran-previous-statement) | |
1985 | ;; Re-indent initially. | |
1986 | (fortran-indent-line) | |
1987 | ;; Replace newline plus continuation field plus indentation with | |
1988 | ;; single space. | |
1989 | (while (progn | |
1990 | (forward-line) | |
1991 | (fortran-remove-continuation))) | |
1992 | (fortran-previous-statement))) | |
e04196d3 | 1993 | (fortran-indent-line))) |
1eb6bf70 | 1994 | |
cfe9d0b5 | 1995 | (defun fortran-strip-sequence-nos (&optional do-space) |
b9fe3dc8 RS |
1996 | "Delete all text in column 72 and up (assumed to be sequence numbers). |
1997 | Normally also deletes trailing whitespace after stripping such text. | |
1998 | Supplying prefix arg DO-SPACE prevents stripping the whitespace." | |
5b04210c | 1999 | (interactive "*p") |
3b4613a4 DL |
2000 | (save-excursion |
2001 | (goto-char (point-min)) | |
562e00da | 2002 | (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t) |
3b4613a4 DL |
2003 | (replace-match "" nil nil nil 1) |
2004 | (unless do-space (delete-horizontal-space))))) | |
2005 | ||
68ca306c DL |
2006 | ;; This code used to live in add-log.el, but this is a better place |
2007 | ;; for it. | |
2008 | (defun fortran-current-defun () | |
2009 | "Function to use for `add-log-current-defun-function' in Fortran mode." | |
a245ece5 GM |
2010 | (save-excursion |
2011 | ;; We must be inside function body for this to work. | |
2012 | (fortran-beginning-of-subprogram) | |
2013 | (let ((case-fold-search t)) ; case-insensitive | |
0a39a75c | 2014 | ;; Search for fortran subprogram start. |
a245ece5 GM |
2015 | (if (re-search-forward |
2016 | (concat "^[ \t]*\\(program\\|subroutine\\|function" | |
2017 | "\\|[ \ta-z0-9*()]*[ \t]+function\\|" | |
2018 | "\\(block[ \t]*data\\)\\)") | |
2019 | (save-excursion (fortran-end-of-subprogram) | |
2020 | (point)) | |
2021 | t) | |
2022 | (or (match-string-no-properties 2) | |
2023 | (progn | |
0a39a75c | 2024 | ;; Move to EOL or before first left paren. |
a245ece5 GM |
2025 | (if (re-search-forward "[(\n]" nil t) |
2026 | (progn (backward-char) | |
2027 | (skip-chars-backward " \t")) | |
2028 | (end-of-line)) | |
2029 | ;; Use the name preceding that. | |
2030 | (buffer-substring-no-properties (point) (progn (backward-sexp) | |
2031 | (point))))) | |
2032 | "main")))) | |
68ca306c | 2033 | |
49116ac0 JB |
2034 | (provide 'fortran) |
2035 | ||
ab5796a9 | 2036 | ;;; arch-tag: 74935096-21c4-4cab-8ee5-6ef16090dc04 |
1a06eabd | 2037 | ;;; fortran.el ends here |