Remove extra TAB in Greek entries.
[emacs.git] / lisp / progmodes / fortran.el
blobe3a29289e896327967c0cc302d6306438336e5e7
1 ;;; fortran.el --- Fortran mode for GNU Emacs
3 ;; Copyright (c) 1986, 93, 94, 95, 97, 98, 99, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Michael D. Prange <prange@erl.mit.edu>
7 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
8 ;; Keywords: languages
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This mode is documented in the Emacs manual.
31 ;; Note that it is for editing Fortran77 or Fortran90 fixed source
32 ;; form. For editing Fortran 90 free format source, use `f90-mode'
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).
37 ;;; History:
39 ;; Fortran mode was upgraded by Stephen A. Wood (saw@cebaf.gov).
41 ;; We acknowledge many contributions and valuable suggestions by
42 ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
43 ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon,
44 ;; Gary Sabot and Richard Stallman.
46 ;;; Code:
48 ;; Todo:
50 ;; * Tidy it all up (more)!
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.
54 ;; * Support any other extensions to f77 grokked by GNU Fortran I've missed.
56 (defgroup fortran nil
57 "Fortran mode for Emacs"
58 :link '(custom-manual "(emacs)Fortran")
59 :group 'languages)
61 (defgroup fortran-indent nil
62 "Indentation variables in Fortran mode"
63 :prefix "fortran-"
64 :group 'fortran)
66 (defgroup fortran-comment nil
67 "Comment-handling variables in Fortran mode"
68 :prefix "fortran-"
69 :group 'fortran)
72 ;;;###autoload
73 (defcustom fortran-tab-mode-default nil
74 "*Default tabbing/carriage control style for empty files in Fortran mode.
75 A value of t specifies tab-digit style of continuation control.
76 A value of nil specifies that continuation lines are marked
77 with a character in column 6."
78 :type 'boolean
79 :group 'fortran-indent)
81 ;; Buffer local, used to display mode line.
82 (defcustom fortran-tab-mode-string nil
83 "String to appear in mode line when TAB format mode is on."
84 :type '(choice (const nil) string)
85 :group 'fortran-indent)
86 (make-variable-buffer-local 'fortran-tab-mode-string)
88 (defcustom fortran-do-indent 3
89 "*Extra indentation applied to DO blocks."
90 :type 'integer
91 :group 'fortran-indent)
93 (defcustom fortran-if-indent 3
94 "*Extra indentation applied to IF blocks."
95 :type 'integer
96 :group 'fortran-indent)
98 (defcustom fortran-structure-indent 3
99 "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks."
100 :type 'integer
101 :group 'fortran-indent)
103 (defcustom fortran-continuation-indent 5
104 "*Extra indentation applied to Fortran continuation lines."
105 :type 'integer
106 :group 'fortran-indent)
108 (defcustom fortran-comment-indent-style 'fixed
109 "*How to indent comments.
110 nil forces comment lines not to be touched,
111 `fixed' makes fixed comment indentation to `fortran-comment-line-extra-indent'
112 columns beyond `fortran-minimum-statement-indent-fixed' (for
113 `indent-tabs-mode' of nil) or `fortran-minimum-statement-indent-tab' (for
114 `indent-tabs-mode' of t), and
115 `relative' indents to current Fortran indentation plus
116 `fortran-comment-line-extra-indent'."
117 :type '(radio (const :tag "Untouched" nil) (const fixed) (const relative))
118 :group 'fortran-indent)
120 (defcustom fortran-comment-line-extra-indent 0
121 "*Amount of extra indentation for text within full-line comments."
122 :type 'integer
123 :group 'fortran-indent
124 :group 'fortran-comment)
126 (defcustom fortran-comment-line-start "C"
127 "*Delimiter inserted to start new full-line comment.
128 You might want to change this to \"*\", for instance."
129 :version "21.1"
130 :type 'string
131 :group 'fortran-comment)
133 ;; This used to match preprocessor lines too, but that messes up
134 ;; filling and doesn't seem to be necessary.
135 (defcustom fortran-comment-line-start-skip
136 "^[CcDd*!]\\(\\([^ \t\n]\\)\\2+\\)?[ \t]*"
137 "*Regexp to match the start of a full-line comment."
138 :version "21.1"
139 :type 'regexp
140 :group 'fortran-comment)
142 (defcustom fortran-directive-re
143 "^[ \t]*#.*"
144 "*Regexp to match a directive line.
145 The matching text will be fontified with `font-lock-keyword-face'.
146 The matching line will be given zero indentation."
147 :version "21.4"
148 :type 'regexp
149 :group 'fortran-indent)
151 (defcustom fortran-minimum-statement-indent-fixed 6
152 "*Minimum statement indentation for fixed format continuation style."
153 :type 'integer
154 :group 'fortran-indent)
156 (defcustom fortran-minimum-statement-indent-tab (max tab-width 6)
157 "*Minimum statement indentation for TAB format continuation style."
158 :type 'integer
159 :group 'fortran-indent)
161 ;; Note that this is documented in the v18 manuals as being a string
162 ;; of length one rather than a single character.
163 ;; The code in this file accepts either format for compatibility.
164 (defcustom fortran-comment-indent-char " "
165 "*Single-character string inserted for Fortran comment indentation.
166 Normally a space."
167 :type 'string
168 :group 'fortran-comment)
170 (defcustom fortran-line-number-indent 1
171 "*Maximum indentation for Fortran line numbers.
172 5 means right-justify them within their five-column field."
173 :type 'integer
174 :group 'fortran-indent)
176 (defcustom fortran-check-all-num-for-matching-do nil
177 "*Non-nil causes all numbered lines to be treated as possible DO loop ends."
178 :type 'boolean
179 :group 'fortran)
181 (defcustom fortran-blink-matching-if nil
182 "*Non-nil causes \\[fortran-indent-line] on ENDIF statement to blink on matching IF.
183 Also, from an ENDDO statement blink on matching DO [WHILE] statement."
184 :type 'boolean
185 :group 'fortran)
187 (defcustom fortran-continuation-string "$"
188 "*Single-character string used for Fortran continuation lines.
189 In fixed format continuation style, this character is inserted in
190 column 6 by \\[fortran-split-line] to begin a continuation line.
191 Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
192 convert the line into a continuation line of the appropriate style.
193 Normally $."
194 :type 'string
195 :group 'fortran)
197 (defcustom fortran-comment-region "c$$$"
198 "*String inserted by \\[fortran-comment-region] at start of each \
199 line in region."
200 :type 'string
201 :group 'fortran-comment)
203 (defcustom fortran-electric-line-number t
204 "*Non-nil causes line number digits to be moved to the correct \
205 column as typed."
206 :type 'boolean
207 :group 'fortran)
209 (defvar fortran-column-ruler-fixed
210 "0 4 6 10 20 30 40 5\
211 0 60 70\n\
212 \[ ]|{ | | | | | | | | \
213 \| | | | |}\n"
214 "String displayed above current line by \\[fortran-column-ruler].
215 This variable used in fixed format mode.")
217 (defvar fortran-column-ruler-tab
218 "0 810 20 30 40 5\
219 0 60 70\n\
220 \[ ]| { | | | | | | | | \
221 \| | | | |}\n"
222 "String displayed above current line by \\[fortran-column-ruler].
223 This variable used in TAB format mode.")
225 (defvar fortran-analyze-depth 100
226 "Number of lines to scan to determine whether to use fixed or TAB \
227 format style.")
229 (defcustom fortran-break-before-delimiters t
230 "*Non-nil causes filling to break lines before delimiters."
231 :type 'boolean
232 :group 'fortran)
234 (defvar fortran-mode-syntax-table
235 (let ((table (make-syntax-table)))
236 ;; We might like `;' to be punctuation (g77 multi-statement
237 ;; lines), but that screws abbrevs.
238 (modify-syntax-entry ?\; "w" table)
239 (modify-syntax-entry ?\r " " table)
240 (modify-syntax-entry ?+ "." table)
241 (modify-syntax-entry ?- "." table)
242 (modify-syntax-entry ?= "." table)
243 (modify-syntax-entry ?* "." table)
244 (modify-syntax-entry ?/ "." table)
245 (modify-syntax-entry ?\' "\"" table)
246 (modify-syntax-entry ?\" "\"" table)
247 ;; Consistent with GNU Fortran -- see the manual.
248 (modify-syntax-entry ?\\ "\\" table)
249 ;; This might be better as punctuation, as for C, but this way you
250 ;; can treat floating-point numbers as symbols.
251 (modify-syntax-entry ?. "_" table) ; e.g. `a.ne.b'
252 (modify-syntax-entry ?_ "_" table)
253 (modify-syntax-entry ?$ "_" table) ; esp. VMSisms
254 (modify-syntax-entry ?\! "<" table)
255 (modify-syntax-entry ?\n ">" table)
256 table)
257 "Syntax table in use in Fortran mode buffers.")
259 ;; Comments are real pain in Fortran because there is no way to
260 ;; represent the standard comment syntax in an Emacs syntax table.
261 ;; (We can do so for F90-style). Therefore an unmatched quote in a
262 ;; standard comment will throw fontification off on the wrong track.
263 ;; So we do syntactic fontification with regexps.
265 (defvar fortran-font-lock-keywords-1 nil
266 "Subdued level highlighting for Fortran mode.")
268 (defvar fortran-font-lock-keywords-2 nil
269 "Medium level highlighting for Fortran mode.")
271 (defvar fortran-font-lock-keywords-3 nil
272 "Gaudy level highlighting for Fortran mode.")
274 (defvar fortran-font-lock-syntactic-keywords nil
275 "`font-lock-syntactic-keywords' for Fortran.
276 These get fixed-format comments fontified.")
278 (let ((comment-chars "cd\\*") ; `d' for `debugging' comments
279 (fortran-type-types
280 (eval-when-compile
281 (let ((re (regexp-opt
282 (let ((simple-types
283 '("character" "byte" "integer" "logical"
284 "none" "real" "complex"
285 "double precision" "double complex"))
286 (structured-types '("structure" "union" "map"))
287 (other-types '("record" "dimension"
288 "parameter" "common" "save"
289 "external" "intrinsic" "data"
290 "equivalence")))
291 (append
292 (mapcar (lambda (x) (concat "implicit " x))
293 simple-types)
294 simple-types
295 (mapcar (lambda (x) (concat "end " x))
296 structured-types)
297 structured-types
298 other-types)))))
299 ;; In the optimized regexp above, replace spaces by a
300 ;; regexp for optional whitespace; regexp-opt would have
301 ;; escaped that.
302 (mapconcat #'identity (split-string re) "[ \t]*"))))
303 (fortran-keywords
304 (eval-when-compile
305 (regexp-opt '("continue" "format" "end" "enddo" "if" "then"
306 "else" "endif" "elseif" "while" "inquire" "stop"
307 "return" "include" "open" "close" "read" "write"
308 "format" "print" "select" "case" "cycle" "exit"
309 "rewind" "backspace"))))
310 (fortran-logicals
311 (eval-when-compile
312 (regexp-opt '("and" "or" "not" "lt" "le" "eq" "ge" "gt" "ne"
313 "true" "false")))))
314 (setq fortran-font-lock-syntactic-keywords
315 ;; Fixed format comments. (!-style handled normally.)
316 (list
317 (list (concat "^[" comment-chars "]") 0 '(11))
318 (list (concat "^[^" comment-chars "\t\n]" ".\\{71\\}"
319 "\\([^\n]+\\)")
320 1 '(11))))
321 (setq fortran-font-lock-keywords-1
322 (list
323 ;; Program, subroutine and function declarations, plus calls.
324 (list (concat "\\<\\(block[ \t]*data\\|call\\|entry\\|function\\|"
325 "program\\|subroutine\\)\\>[ \t]*\\(\\sw+\\)?")
326 '(1 font-lock-keyword-face)
327 '(2 font-lock-function-name-face nil t))))
328 (setq fortran-font-lock-keywords-2
329 (append fortran-font-lock-keywords-1
330 (list
331 ;; Fontify all type specifiers (must be first; see below).
332 (cons (concat "\\<\\(" fortran-type-types "\\)\\>")
333 'font-lock-type-face)
334 ;; Fontify all builtin keywords (except logical, do
335 ;; and goto; see below).
336 (concat "\\<\\(" fortran-keywords "\\)\\>")
337 ;; Fontify all builtin operators.
338 (concat "\\.\\(" fortran-logicals "\\)\\.")
339 ;; Fontify do/goto keywords and targets, and goto tags.
340 (list "\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)?"
341 '(1 font-lock-keyword-face)
342 '(2 font-lock-constant-face nil t))
343 (cons "^ *\\([0-9]+\\)" 'font-lock-constant-face))))
344 (setq fortran-font-lock-keywords-3
345 (append
346 ;; The list `fortran-font-lock-keywords-1'.
347 fortran-font-lock-keywords-1
348 ;; Fontify all type specifiers plus their declared items.
349 (list
350 (list (concat "\\<\\(" fortran-type-types "\\)\\>[ \t(/]*\\(*\\)?")
351 ;; Fontify the type specifier.
352 '(1 font-lock-type-face)
353 ;; Fontify each declaration item (or just the /.../ block name).
354 `(font-lock-match-c-style-declaration-item-and-skip-to-next
355 ;; Start after any *(...) expression.
356 (condition-case nil
357 (and (and (match-beginning ,(+ 2 (regexp-opt-depth
358 fortran-type-types)))
359 (forward-sexp))
360 (forward-sexp))
361 (error nil))
362 ;; No need to clean up.
364 ;; Fontify as a variable name, functions are
365 ;; fontified elsewhere.
366 (1 font-lock-variable-name-face nil t))))
367 ;; Things extra to `fortran-font-lock-keywords-3'
368 ;; (must be done first).
369 (list
370 ;; Fontify goto-like `err=label'/`end=label' in read/write
371 ;; statements.
372 '(", *\\(e\\(nd\\|rr\\)\\)\\> *\\(= *\\([0-9]+\\)\\)?"
373 (1 font-lock-keyword-face) (4 font-lock-constant-face nil t))
374 ;; Highlight standard continuation character and in a
375 ;; TAB-formatted line.
376 '("^ \\([^ 0]\\)" 1 font-lock-string-face)
377 '("^\t\\([1-9]\\)" 1 font-lock-string-face))
378 (list
379 ;; cpp stuff (ugh)
380 ;;; '("^# *[a-z]+" . font-lock-keyword-face))
381 `(,fortran-directive-re (0 font-lock-keyword-face t)))
382 ;; The list `fortran-font-lock-keywords-2' less that for types
383 ;; (see above).
384 (cdr (nthcdr (length fortran-font-lock-keywords-1)
385 fortran-font-lock-keywords-2)))))
387 (defvar fortran-font-lock-keywords fortran-font-lock-keywords-1
388 "Default expressions to highlight in Fortran mode.")
390 (defvar fortran-imenu-generic-expression
391 ;; These patterns could be confused by sequence nos. in cols 72+ and
392 ;; don't allow continuations everywhere.
393 (list
394 (list
396 ;; [This will be fooled by `end function' allowed by G77. Also,
397 ;; it assumes sensible whitespace is employed.]
398 (concat
399 ;; leading whitespace:
400 "^\\s-+\\("
401 ;; function declaration with optional type, e.g. `real',
402 ;; `real*4', character(*), `double precision':
403 "\\(\\sw\\|\\s-\\|[*()+]\\)*"
404 "\\<function\\|subroutine\\|entry\\|block\\s-*data\\|program\\)"
405 ;; Possible statement continuation:
406 "[ \t" fortran-continuation-string "]+"
407 ;; Variable to index:
408 "\\(\\sw+\\)")
410 ;; Un-named block data
411 (list nil "^\\s-+\\(block\\s-*data\\)\\s-*$" 1))
412 "Imenu generic expression for `imenu-default-create-index-function'.")
414 (defvar fortran-mode-map
415 (let ((map (make-sparse-keymap)))
416 (define-key map ";" 'fortran-abbrev-start)
417 (define-key map "\C-c;" 'fortran-comment-region)
418 (define-key map "\M-;" 'fortran-indent-comment)
419 (define-key map "\M-\n" 'fortran-split-line)
420 (define-key map "\M-\C-q" 'fortran-indent-subprogram)
421 (define-key map "\C-c\C-w" 'fortran-window-create-momentarily)
422 (define-key map "\C-c\C-r" 'fortran-column-ruler)
423 (define-key map "\C-c\C-p" 'fortran-previous-statement)
424 (define-key map "\C-c\C-n" 'fortran-next-statement)
425 (define-key map "\C-c\C-d" 'fortran-join-line) ; like f90
426 (define-key map "\M-^" 'fortran-join-line) ; subvert delete-indentation
427 (define-key map "0" 'fortran-electric-line-number)
428 (define-key map "1" 'fortran-electric-line-number)
429 (define-key map "2" 'fortran-electric-line-number)
430 (define-key map "3" 'fortran-electric-line-number)
431 (define-key map "4" 'fortran-electric-line-number)
432 (define-key map "5" 'fortran-electric-line-number)
433 (define-key map "6" 'fortran-electric-line-number)
434 (define-key map "7" 'fortran-electric-line-number)
435 (define-key map "8" 'fortran-electric-line-number)
436 (define-key map "9" 'fortran-electric-line-number)
438 ;; Menu
439 (easy-menu-define
440 fortran-mode-menu map ""
441 `("Fortran"
442 ["Manual" (info "(emacs)Fortran")]
443 ("Customization"
444 ,(custom-menu-create 'fortran)
445 ["Set" Custom-set t]
446 ["Save" Custom-save t]
447 ["Reset to Current" Custom-reset-current t]
448 ["Reset to Saved" Custom-reset-saved t]
449 ["Reset to Standard Settings" Custom-reset-standard t])
450 "----"
451 ["Toggle Auto-fill" auto-fill-mode :style toggle
452 :selected auto-fill-function]
453 ["Toggle abbrev-mode" abbrev-mode :style toggle :selected abbrev-mode]
454 "----"
455 ["Comment-out Region" fortran-comment-region mark-active]
456 ["Uncomment-out region"
457 (fortran-comment-region (region-beginning) (region-end) 1)
458 mark-active]
459 ["Indent Region" indent-region mark-active]
460 ["Indent Subprogram" fortran-indent-subprogram t]
461 "----"
462 ["Beginning of Subprogram" fortran-beginning-of-subprogram t]
463 ["End of Subprogram" fortran-end-of-subprogram t]
464 ("Mark"
465 ["Subprogram" mark-defun t]
466 ["IF Block" fortran-mark-if t]
467 ["DO Block" fortran-mark-do t])
468 ["Narrow to Subprogram" narrow-to-defun t]
469 ["Widen" widen t]
470 "----"
471 ["Temporary column ruler" fortran-column-ruler t]
472 ["72-column window" fortran-window-create t]
473 ["Full Width Window"
474 (enlarge-window-horizontally (- (frame-width) (window-width)))
475 (< (window-width) (frame-width))]
476 ["Momentary 72-column window" fortran-window-create-momentarily t]
477 "----"
478 ["Break Line at Point" fortran-split-line t]
479 ["Join Line" fortran-join-line t]
480 ["Fill Statement/Comment" fill-paragraph t]
481 "----"
482 ["Add imenu menu"
483 imenu-add-menubar-index (not (and (boundp 'imenu--index-alist)
484 imenu--index-alist))]))
485 map)
486 "Keymap used in Fortran mode.")
488 (defvar fortran-mode-abbrev-table
489 (let (abbrevs-changed)
490 (define-abbrev-table 'fortran-mode-abbrev-table nil)
491 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
492 ;; Only use `apply' to quieten the byte-compiler.
493 (mapcar
494 (function (lambda (element)
495 (condition-case nil
496 (apply 'define-abbrev fortran-mode-abbrev-table
497 (append element '(nil 0 t)))
498 (wrong-number-of-arguments
499 (apply 'define-abbrev fortran-mode-abbrev-table
500 (append element '(nil 0)))))))
501 '((";au" "automatic" )
502 (";b" "byte" )
503 (";bd" "block data" )
504 (";ch" "character" )
505 (";cl" "close" )
506 (";c" "continue" )
507 (";cm" "common" )
508 (";cx" "complex" )
509 (";df" "define" )
510 (";di" "dimension" )
511 (";do" "double" )
512 (";dc" "double complex" )
513 (";dp" "double precision" )
514 (";dw" "do while" )
515 (";e" "else" )
516 (";ed" "enddo" )
517 (";el" "elseif" )
518 (";en" "endif" )
519 (";eq" "equivalence" )
520 (";ew" "endwhere" )
521 (";ex" "external" )
522 (";ey" "entry" )
523 (";f" "format" )
524 (";fa" ".false." )
525 (";fu" "function" )
526 (";g" "goto" )
527 (";im" "implicit" )
528 (";ib" "implicit byte" )
529 (";ic" "implicit complex" )
530 (";ich" "implicit character")
531 (";ii" "implicit integer" )
532 (";il" "implicit logical" )
533 (";ir" "implicit real" )
534 (";inc" "include" )
535 (";in" "integer" )
536 (";intr" "intrinsic" )
537 (";l" "logical" )
538 (";n" "namelist" )
539 (";o" "open" ) ; was ;op
540 (";pa" "parameter" )
541 (";pr" "program" )
542 (";ps" "pause" )
543 (";p" "print" )
544 (";rc" "record" )
545 (";re" "real" )
546 (";r" "read" )
547 (";rt" "return" )
548 (";rw" "rewind" )
549 (";s" "stop" )
550 (";sa" "save" )
551 (";st" "structure" )
552 (";sc" "static" )
553 (";su" "subroutine" )
554 (";tr" ".true." )
555 (";ty" "type" )
556 (";vo" "volatile" )
557 (";w" "write" )
558 (";wh" "where" )))
559 fortran-mode-abbrev-table))
561 (eval-when-compile ; silence compiler
562 (defvar imenu-case-fold-search)
563 (defvar imenu-syntax-alist))
565 (defcustom fortran-mode-hook nil
566 "Hook run by Fortran mode."
567 :type 'hook
568 :group 'fortran)
570 ;;;###autoload
571 (defun fortran-mode ()
572 "Major mode for editing Fortran code.
573 \\[fortran-indent-line] indents the current Fortran line correctly.
574 DO statements must not share a common CONTINUE.
576 Type ;? or ;\\[help-command] to display a list of built-in abbrevs for
577 Fortran keywords.
579 Key definitions:
580 \\{fortran-mode-map}
582 Variables controlling indentation style and extra features:
584 `comment-start'
585 If you want to use comments starting with `!',
586 set this to the string \"!\".
587 `fortran-do-indent'
588 Extra indentation within do blocks. (default 3)
589 `fortran-if-indent'
590 Extra indentation within if blocks. (default 3)
591 `fortran-structure-indent'
592 Extra indentation within structure, union, map and interface blocks.
593 (default 3)
594 `fortran-continuation-indent'
595 Extra indentation applied to continuation statements. (default 5)
596 `fortran-comment-line-extra-indent'
597 Amount of extra indentation for text within full-line comments. (default 0)
598 `fortran-comment-indent-style'
599 nil means don't change indentation of text in full-line comments,
600 fixed means indent that text at `fortran-comment-line-extra-indent' beyond
601 the value of `fortran-minimum-statement-indent-fixed' (for fixed
602 format continuation style) or `fortran-minimum-statement-indent-tab'
603 (for TAB format continuation style).
604 relative means indent at `fortran-comment-line-extra-indent' beyond the
605 indentation for a line of code.
606 (default 'fixed)
607 `fortran-comment-indent-char'
608 Single-character string to be inserted instead of space for
609 full-line comment indentation. (default \" \")
610 `fortran-minimum-statement-indent-fixed'
611 Minimum indentation for Fortran statements in fixed format mode. (def.6)
612 `fortran-minimum-statement-indent-tab'
613 Minimum indentation for Fortran statements in TAB format mode. (default 9)
614 `fortran-line-number-indent'
615 Maximum indentation for line numbers. A line number will get
616 less than this much indentation if necessary to avoid reaching
617 column 5. (default 1)
618 `fortran-check-all-num-for-matching-do'
619 Non-nil causes all numbered lines to be treated as possible \"continue\"
620 statements. (default nil)
621 `fortran-blink-matching-if'
622 Non-nil causes \\[fortran-indent-line] on an ENDIF statement to blink on
623 matching IF. Also, from an ENDDO statement, blink on matching DO [WHILE]
624 statement. (default nil)
625 `fortran-continuation-string'
626 Single-character string to be inserted in column 5 of a continuation
627 line. (default \"$\")
628 `fortran-comment-region'
629 String inserted by \\[fortran-comment-region] at start of each line in
630 region. (default \"c$$$\")
631 `fortran-electric-line-number'
632 Non-nil causes line number digits to be moved to the correct column
633 as typed. (default t)
634 `fortran-break-before-delimiters'
635 Non-nil causes lines to be broken before delimiters.
636 (default t)
638 Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
639 with no args, if that value is non-nil."
640 (interactive)
641 (kill-all-local-variables)
642 (setq local-abbrev-table fortran-mode-abbrev-table)
643 (set-syntax-table fortran-mode-syntax-table)
644 ;; Font Lock mode support.
645 (make-local-variable 'font-lock-defaults)
646 (setq font-lock-defaults '((fortran-font-lock-keywords
647 fortran-font-lock-keywords-1
648 fortran-font-lock-keywords-2
649 fortran-font-lock-keywords-3)
650 nil t ((?/ . "$/") ("_$" . "w"))
651 fortran-beginning-of-subprogram))
652 (set (make-local-variable 'font-lock-syntactic-keywords)
653 fortran-font-lock-syntactic-keywords)
654 (make-local-variable 'fortran-break-before-delimiters)
655 (setq fortran-break-before-delimiters t)
656 (make-local-variable 'indent-line-function)
657 (setq indent-line-function 'fortran-indent-line)
658 (make-local-variable 'comment-indent-function)
659 (setq comment-indent-function 'fortran-comment-indent)
660 (set (make-local-variable 'comment-start-skip)
661 ;; We can't reuse `fortran-comment-line-start-skip' directly because
662 ;; it contains backrefs whereas we need submatch-1 to end at the
663 ;; beginning of the comment delimiter.
664 ;; (concat "\\(\\)\\(![ \t]*\\|" fortran-comment-line-start-skip "\\)")
665 "\\(\\)\\(?:^[CcDd*]\\|!\\)\\(?:\\([^ \t\n]\\)\\2+\\)?[ \t]*")
666 (set (make-local-variable 'comment-padding) "$$$")
667 (set (make-local-variable 'comment-start) fortran-comment-line-start)
668 ;; The syntax tables don't understand the column-0 comment-markers.
669 (set (make-local-variable 'comment-use-syntax) nil)
670 (make-local-variable 'require-final-newline)
671 (setq require-final-newline t)
672 (make-local-variable 'abbrev-all-caps)
673 (setq abbrev-all-caps t)
674 (make-local-variable 'indent-tabs-mode)
675 (setq indent-tabs-mode nil)
676 ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
677 (set (make-local-variable 'fill-column) 72)
678 (use-local-map fortran-mode-map)
679 (setq mode-name "Fortran")
680 (setq major-mode 'fortran-mode)
681 (make-local-variable 'fortran-comment-line-extra-indent)
682 (make-local-variable 'fortran-minimum-statement-indent-fixed)
683 (make-local-variable 'fortran-minimum-statement-indent-tab)
684 (make-local-variable 'fortran-column-ruler-fixed)
685 (make-local-variable 'fortran-column-ruler-tab)
686 (setq fortran-tab-mode-string " TAB-format")
687 (setq indent-tabs-mode (fortran-analyze-file-format))
688 (setq imenu-case-fold-search t)
689 (setq imenu-generic-expression fortran-imenu-generic-expression)
690 (setq imenu-syntax-alist '(("_$" . "w")))
691 (set (make-local-variable 'fill-paragraph-function) 'fortran-fill-paragraph)
692 (set (make-local-variable 'normal-auto-fill-function) 'fortran-auto-fill)
693 (set (make-local-variable 'indent-line-function) 'fortran-indent-line)
694 (set (make-local-variable 'indent-region-function)
695 (lambda (start end)
696 (let (fortran-blink-matching-if ; avoid blinking delay
697 indent-region-function)
698 (indent-region start end nil))))
699 (set (make-local-variable 'beginning-of-defun-function)
700 #'fortran-beginning-of-subprogram)
701 (set (make-local-variable 'end-of-defun-function)
702 #'fortran-end-of-subprogram)
703 (set (make-local-variable 'add-log-current-defun-function)
704 #'fortran-current-defun)
705 (set (make-local-variable 'dabbrev-case-fold-search) 'case-fold-search)
706 (run-hooks 'fortran-mode-hook))
708 (defsubst fortran-comment-indent ()
709 (save-excursion
710 (if (looking-at fortran-comment-line-start-skip) 0
711 (skip-chars-backward " \t")
712 (max (+ 1 (current-column))
713 comment-column))))
715 (defun fortran-indent-comment ()
716 "Align or create comment on current line.
717 Existing comments of all types are recognized and aligned.
718 If the line has no comment, a side-by-side comment is inserted and aligned
719 if the value of `comment-start' is not nil and allows such comments.
720 Otherwise, a separate-line comment is inserted, on this line
721 or on a new line inserted before this line if this line is not blank."
722 (interactive)
723 (beginning-of-line)
724 ;; Recognize existing comments of either kind.
725 (cond ((fortran-find-comment-start-skip 'all)
726 (goto-char (match-beginning 0))
727 (if (bolp)
728 (fortran-indent-line)
729 (if (not (= (current-column)
730 (fortran-comment-indent)))
731 (progn (delete-horizontal-space)
732 (indent-to (fortran-comment-indent))))))
733 ;; No existing comment.
734 ;; If side-by-side comments are defined, insert one,
735 ;; unless line is now blank.
736 ((and comment-start (not (looking-at "[ \t]*$"))
737 (string-match comment-start-skip (concat " " comment-start)))
738 (end-of-line)
739 (delete-horizontal-space)
740 (indent-to (fortran-comment-indent))
741 (insert comment-start))
742 ;; Else insert separate-line comment, making a new line if nec.
744 (if (looking-at "^[ \t]*$")
745 (delete-horizontal-space)
746 (beginning-of-line)
747 (insert ?\n)
748 (forward-char -1))
749 (insert fortran-comment-line-start)
750 (insert-char (if (stringp fortran-comment-indent-char)
751 (aref fortran-comment-indent-char 0)
752 fortran-comment-indent-char)
753 (- (fortran-calculate-indent) (current-column))))))
755 (defun fortran-comment-region (beg-region end-region arg)
756 "Comments every line in the region.
757 Puts `fortran-comment-region' at the beginning of every line in the region.
758 BEG-REGION and END-REGION are args which specify the region boundaries.
759 With non-nil ARG, uncomments the region."
760 (interactive "*r\nP")
761 (let ((end-region-mark (copy-marker end-region))
762 (save-point (point-marker)))
763 (goto-char beg-region)
764 (beginning-of-line)
765 (if (not arg) ;comment the region
766 (progn (insert fortran-comment-region)
767 (while (and (= (forward-line 1) 0)
768 (< (point) end-region-mark))
769 (insert fortran-comment-region)))
770 (let ((com (regexp-quote fortran-comment-region))) ;uncomment the region
771 (if (looking-at com)
772 (delete-region (point) (match-end 0)))
773 (while (and (= (forward-line 1) 0)
774 (< (point) end-region-mark))
775 (if (looking-at com)
776 (delete-region (point) (match-end 0))))))
777 (goto-char save-point)
778 (set-marker end-region-mark nil)
779 (set-marker save-point nil)))
781 (defun fortran-abbrev-start ()
782 "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs.
783 Any other key combination is executed normally."
784 (interactive)
785 (let (c)
786 (insert last-command-char)
787 (if (or (eq (setq c (read-event)) ??) ;insert char if not equal to `?'
788 (eq c help-char))
789 (fortran-abbrev-help)
790 (setq unread-command-events (list c)))))
792 (defun fortran-abbrev-help ()
793 "List the currently defined abbrevs in Fortran mode."
794 (interactive)
795 (message "Listing abbrev table...")
796 (display-buffer (fortran-prepare-abbrev-list-buffer))
797 (message "Listing abbrev table...done"))
799 (defun fortran-prepare-abbrev-list-buffer ()
800 (save-excursion
801 (set-buffer (get-buffer-create "*Abbrevs*"))
802 (erase-buffer)
803 (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
804 (goto-char (point-min))
805 (set-buffer-modified-p nil)
806 (edit-abbrevs-mode))
807 (get-buffer-create "*Abbrevs*"))
809 (defun fortran-column-ruler ()
810 "Insert a column ruler momentarily above current line, till next keystroke.
811 The ruler is defined by the value of `fortran-column-ruler-fixed' when in fixed
812 format mode, and `fortran-column-ruler-tab' when in TAB format mode.
813 The key typed is executed unless it is SPC."
814 (interactive)
815 (momentary-string-display
816 (if indent-tabs-mode
817 fortran-column-ruler-tab
818 fortran-column-ruler-fixed)
819 (save-excursion
820 (beginning-of-line)
821 (if (eq (window-start (selected-window))
822 (window-point (selected-window)))
823 (line-beginning-position 2)
824 (point)))
825 nil "Type SPC or any command to erase ruler."))
827 (defun fortran-window-create ()
828 "Make the window 72 columns wide.
829 See also `fortran-window-create-momentarily'."
830 (interactive)
831 (let ((window-min-width 2))
832 (if (< (window-width) (frame-width))
833 (enlarge-window-horizontally (- (frame-width)
834 (window-width) 1)))
835 (let* ((window-edges (window-edges))
836 (scroll-bar-width (- (nth 2 window-edges)
837 (car window-edges)
838 (window-width))))
839 (split-window-horizontally (+ 72 scroll-bar-width)))
840 (other-window 1)
841 (switch-to-buffer " fortran-window-extra" t)
842 (select-window (previous-window))))
844 (defun fortran-window-create-momentarily (&optional arg)
845 "Momentarily make the window 72 columns wide.
846 Optional ARG non-nil and non-unity disables the momentary feature.
847 See also `fortran-window-create'."
848 (interactive "p")
849 (if (or (not arg)
850 (= arg 1))
851 (save-window-excursion
852 (progn
853 (condition-case nil
854 (fortran-window-create)
855 (error (error "No room for Fortran window")))
856 (message "Type SPC to continue editing.")
857 (let ((char (read-event)))
858 (or (equal char (string-to-char " "))
859 (setq unread-command-events (list char))))))
860 (fortran-window-create)))
862 (defun fortran-split-line ()
863 "Break line at point and insert continuation marker and alignment."
864 (interactive)
865 (delete-horizontal-space)
866 (if (save-excursion
867 (let ((pos (point)))
868 (beginning-of-line)
869 (and (fortran-find-comment-start-skip 'all)
870 (< (match-beginning 0) pos))))
871 (insert ?\n (match-string 0))
872 (if indent-tabs-mode
873 (insert ?\n ?\t (fortran-numerical-continuation-char))
874 (insert "\n " fortran-continuation-string))) ; Space after \n important
875 (fortran-indent-line)) ; when the cont string is C, c or *.
877 (defun fortran-remove-continuation ()
878 (if (looking-at "\\( [^ 0\n]\\|\t[1-9]\\|&\\)")
879 (progn (replace-match "")
880 (delete-indentation)
881 t)))
883 (defun fortran-join-line (arg)
884 "Join current line to the previous one and re-indent.
885 With a prefix argument, repeat this operation that many times.
886 If the prefix argument ARG is negative, join the next -ARG lines.
887 Continuation lines are correctly handled."
888 (interactive "*p")
889 (save-excursion
890 (when (> 0 arg)
891 (setq arg (- arg))
892 (forward-line arg))
893 (while (not (zerop arg))
894 (beginning-of-line)
895 (or (fortran-remove-continuation)
896 (delete-indentation))
897 (setq arg (1- arg)))
898 (fortran-indent-line)))
900 (defun fortran-numerical-continuation-char ()
901 "Return a digit for tab-digit style of continuation lines.
902 If, previous line is a tab-digit continuation line, returns that digit
903 plus one. Otherwise return 1. Zero not allowed."
904 (save-excursion
905 (forward-line -1)
906 (if (looking-at "\t[1-9]")
907 (+ ?1 (% (- (char-after (+ (point) 1)) ?0) 9))
908 ?1)))
910 (put 'fortran-electric-line-number 'delete-selection t)
911 (defun fortran-electric-line-number (arg)
912 "Self insert, but if part of a Fortran line number indent it automatically.
913 Auto-indent does not happen if a numeric ARG is used."
914 (interactive "P")
915 (if (or arg (not fortran-electric-line-number))
916 (if arg
917 (self-insert-command (prefix-numeric-value arg))
918 (self-insert-command 1))
919 (if (or (and (= 5 (current-column))
920 (save-excursion
921 (beginning-of-line)
922 (looking-at " \\{5\\}"))) ;In col 5 with only spaces to left.
923 (and (= (if indent-tabs-mode
924 fortran-minimum-statement-indent-tab
925 fortran-minimum-statement-indent-fixed) (current-column))
926 (eq ?\t (char-after (line-beginning-position))) ;In col 8
927 ; with a single tab to the left.
928 (not (or (eq last-command 'fortran-indent-line)
929 (eq last-command
930 'fortran-indent-new-line))))
931 (save-excursion
932 (re-search-backward "[^ \t0-9]"
933 (line-beginning-position)
934 t)) ;not a line number
935 (looking-at "[0-9]")) ;within a line number
936 (self-insert-command (prefix-numeric-value arg))
937 (skip-chars-backward " \t")
938 (insert last-command-char)
939 (fortran-indent-line))))
941 (defvar fortran-end-prog-re1
942 "end\
943 \\([ \t]*\\(program\\|subroutine\\|function\\|block[ \t]*data\\)\\>\
944 \\([ \t]*\\(\\sw\\|\\s_\\)+\\)?\\)?")
945 (defvar fortran-end-prog-re
946 (concat "^[ \t0-9]*" fortran-end-prog-re1)
947 "Regexp possibly marking subprogram end.")
949 (defun fortran-check-end-prog-re ()
950 "Check a preliminary match against `fortran-end-prog-re'."
951 ;; Having got a possible match for the subprogram end, we need a
952 ;; match of whitespace, avoiding possible column 73+ stuff.
953 (save-match-data
954 (string-match "^\\s-*\\(\\'\\|\\s<\\)"
955 (buffer-substring (match-end 0)
956 (min (line-end-position)
957 (+ 72 (line-beginning-position)))))))
959 ;; Note that you can't just check backwards for `subroutine' &c in
960 ;; case of un-marked main programs not at the start of the file.
961 (defun fortran-beginning-of-subprogram ()
962 "Move point to the beginning of the current Fortran subprogram."
963 (interactive)
964 (save-match-data
965 (let ((case-fold-search t))
966 (beginning-of-line -1)
967 (if (catch 'ok
968 (while (re-search-backward fortran-end-prog-re nil 'move)
969 (if (fortran-check-end-prog-re)
970 (throw 'ok t))))
971 (forward-line)))))
973 (defun fortran-end-of-subprogram ()
974 "Move point to the end of the current Fortran subprogram."
975 (interactive)
976 (save-match-data
977 (let ((case-fold-search t))
978 (if (save-excursion ; on END
979 (beginning-of-line)
980 (and (looking-at fortran-end-prog-re)
981 (fortran-check-end-prog-re)))
982 (forward-line)
983 (beginning-of-line 2)
984 (catch 'ok
985 (while (re-search-forward fortran-end-prog-re nil 'move)
986 (if (fortran-check-end-prog-re)
987 (throw 'ok t))))
988 (goto-char (match-beginning 0))
989 (forward-line)))))
991 (defun fortran-previous-statement ()
992 "Move point to beginning of the previous Fortran statement.
993 Returns 'first-statement if that statement is the first
994 non-comment Fortran statement in the file, and nil otherwise.
995 Directive lines are treated as comments."
996 (interactive)
997 (let (not-first-statement continue-test)
998 (beginning-of-line)
999 (setq continue-test
1000 (and
1001 (not (looking-at fortran-comment-line-start-skip))
1002 (not (looking-at fortran-directive-re))
1003 (or (looking-at
1004 (concat "[ \t]*"
1005 (regexp-quote fortran-continuation-string)))
1006 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))))
1007 (while (and (setq not-first-statement (= (forward-line -1) 0))
1008 (or (looking-at fortran-comment-line-start-skip)
1009 (looking-at fortran-directive-re)
1010 (looking-at "[ \t]*$\\| \\{5\\}[^ 0\n]\\|\t[1-9]")
1011 (looking-at (concat "[ \t]*" comment-start-skip)))))
1012 (cond ((and continue-test
1013 (not not-first-statement))
1014 (message "Incomplete continuation statement."))
1015 (continue-test
1016 (fortran-previous-statement))
1017 ((not not-first-statement)
1018 'first-statement))))
1020 (defun fortran-next-statement ()
1021 "Move point to beginning of the next Fortran statement.
1022 Returns 'last-statement if that statement is the last
1023 non-comment Fortran statement in the file, and nil otherwise.
1024 Directive lines are treated as comments."
1025 (interactive)
1026 (let (not-last-statement)
1027 (beginning-of-line)
1028 (while (and (setq not-last-statement
1029 (and (= (forward-line 1) 0)
1030 (not (eobp))))
1031 (or (looking-at fortran-comment-line-start-skip)
1032 (looking-at fortran-directive-re)
1033 (looking-at "[ \t]*$\\| [^ 0\n]\\|\t[1-9]")
1034 (looking-at (concat "[ \t]*" comment-start-skip)))))
1035 (if (not not-last-statement)
1036 'last-statement)))
1038 (defun fortran-blink-match (regex keyword find-begin)
1039 "From a line matching REGEX, blink matching KEYWORD statement line.
1040 Use function FIND-BEGIN to match it."
1041 (let ((top-of-window (window-start))
1042 (end-point (point))
1043 (case-fold-search t)
1044 matching
1045 message)
1046 (if (save-excursion
1047 (beginning-of-line)
1048 (skip-chars-forward " \t0-9")
1049 (looking-at regex))
1050 (progn
1051 (if (not (setq matching (funcall find-begin)))
1052 (setq message (concat "No matching " keyword "."))
1053 (if (< matching top-of-window)
1054 (save-excursion
1055 (goto-char matching)
1056 (beginning-of-line)
1057 (setq message
1058 (concat "Matches "
1059 (buffer-substring (point)
1060 (line-end-position)))))))
1061 (if message
1062 (message "%s" message)
1063 (goto-char matching)
1064 (sit-for 1)
1065 (goto-char end-point))))))
1067 (defun fortran-blink-matching-if ()
1068 "From an ENDIF or ELSE statement, blink the matching IF statement."
1069 (fortran-blink-match "e\\(nd[ \t]*if\\|lse\\([ \t]*if\\)?\\)\\b"
1070 "if" #'fortran-beginning-if))
1072 (defun fortran-blink-matching-do ()
1073 "From an ENDDO statement, blink the matching DO or DO WHILE statement."
1074 (fortran-blink-match "end[ \t]*do\\b" "do" #'fortran-beginning-do))
1076 (defun fortran-mark-do ()
1077 "Put mark at end of Fortran DO [WHILE]-ENDDO construct, point at beginning.
1078 The marks are pushed."
1079 (interactive)
1080 (let (enddo-point do-point)
1081 (if (setq enddo-point (fortran-end-do))
1082 (if (not (setq do-point (fortran-beginning-do)))
1083 (message "No matching do.")
1084 (goto-char enddo-point)
1085 (push-mark)
1086 (goto-char do-point)))))
1088 (defun fortran-end-do ()
1089 "Search forward for first unmatched ENDDO.
1090 Return point or nil."
1091 (let ((case-fold-search t))
1092 (if (save-excursion (beginning-of-line)
1093 (skip-chars-forward " \t0-9")
1094 (looking-at "end[ \t]*do\\b"))
1095 ;; Sitting on one.
1096 (match-beginning 0)
1097 ;; Search for one.
1098 (save-excursion
1099 (let ((count 1))
1100 (while (and (not (= count 0))
1101 (not (eq (fortran-next-statement) 'last-statement))
1102 ;; Keep local to subprogram
1103 (not (and (looking-at fortran-end-prog-re)
1104 (fortran-check-end-prog-re))))
1105 (skip-chars-forward " \t0-9")
1106 (cond ((looking-at "end[ \t]*do\\b")
1107 (setq count (1- count)))
1108 ((looking-at
1109 "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]")
1110 (setq count (+ count 1)))))
1111 (and (= count 0)
1112 ;; All pairs accounted for.
1113 (point)))))))
1115 (defun fortran-beginning-do ()
1116 "Search backwards for first unmatched DO [WHILE].
1117 Return point or nil. Ignores labelled DO loops (ie DO 10 ... 10 CONTINUE)."
1118 (let ((case-fold-search t)
1119 (dostart-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?do[ \t]+[^0-9]"))
1120 (if (save-excursion
1121 (beginning-of-line)
1122 (skip-chars-forward " \t0-9")
1123 (looking-at dostart-re))
1124 ;; Sitting on one.
1125 (match-beginning 0)
1126 ;; Search for one.
1127 (save-excursion
1128 (let ((count 1))
1129 (while (and (not (= count 0))
1130 (not (eq (fortran-previous-statement) 'first-statement))
1131 ;; Keep local to subprogram
1132 (not (and (looking-at fortran-end-prog-re)
1133 (fortran-check-end-prog-re))))
1134 (skip-chars-forward " \t0-9")
1135 (cond ((looking-at dostart-re)
1136 (setq count (1- count)))
1137 ;; Note labelled loop ends not considered.
1138 ((looking-at "end[ \t]*do\\b")
1139 (setq count (1+ count)))))
1140 (and (= count 0)
1141 ;; All pairs accounted for.
1142 (point)))))))
1144 (defun fortran-mark-if ()
1145 "Put mark at end of Fortran IF-ENDIF construct, point at beginning.
1146 The marks are pushed."
1147 (interactive)
1148 (let (endif-point if-point)
1149 (if (setq endif-point (fortran-end-if))
1150 (if (not (setq if-point (fortran-beginning-if)))
1151 (message "No matching if.")
1152 ;; Set mark, move point.
1153 (goto-char endif-point)
1154 (push-mark)
1155 (goto-char if-point)))))
1157 (defvar fortran-if-start-re "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1159 (defun fortran-end-if ()
1160 "Search forwards for first unmatched ENDIF.
1161 Return point or nil."
1162 (let ((case-fold-search t))
1163 (if (save-excursion (beginning-of-line)
1164 (skip-chars-forward " \t0-9")
1165 (looking-at "end[ \t]*if\\b"))
1166 ;; Sitting on one.
1167 (match-beginning 0)
1168 ;; Search for one. The point has been already been moved to first
1169 ;; letter on line but this should not cause troubles.
1170 (save-excursion
1171 (let ((count 1))
1172 (while (and (not (= count 0))
1173 (not (eq (fortran-next-statement) 'last-statement))
1174 ;; Keep local to subprogram.
1175 (not (and (looking-at fortran-end-prog-re)
1176 (fortran-check-end-prog-re))))
1177 (skip-chars-forward " \t0-9")
1178 (cond ((looking-at "end[ \t]*if\\b")
1179 (setq count (- count 1)))
1180 ((looking-at fortran-if-start-re)
1181 (save-excursion
1182 (if (or
1183 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1184 (let (then-test) ; Multi-line if-then.
1185 (while
1186 (and
1187 (= (forward-line 1) 0)
1188 ;; Search forward for then.
1189 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1190 (not
1191 (setq then-test
1192 (looking-at
1193 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1194 then-test))
1195 (setq count (+ count 1)))))))
1196 (and (= count 0)
1197 ;; All pairs accounted for.
1198 (point)))))))
1200 (defun fortran-beginning-if ()
1201 "Search backwards for first unmatched IF-THEN.
1202 Return point or nil."
1203 (let ((case-fold-search t))
1204 (if (save-excursion
1205 ;; May be sitting on multi-line if-then statement, first
1206 ;; move to beginning of current statement. Note:
1207 ;; `fortran-previous-statement' moves to previous statement
1208 ;; *unless* current statement is first one. Only move
1209 ;; forward if not first-statement.
1210 (if (not (eq (fortran-previous-statement) 'first-statement))
1211 (fortran-next-statement))
1212 (skip-chars-forward " \t0-9")
1213 (and
1214 (looking-at fortran-if-start-re)
1215 (save-match-data
1216 (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1217 ;; Multi-line if-then.
1218 (let (then-test)
1219 (while
1220 (and (= (forward-line 1) 0)
1221 ;; Search forward for then.
1222 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1223 (not
1224 (setq then-test
1225 (looking-at
1226 ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
1227 then-test)))))
1228 ;; Sitting on one.
1229 (match-beginning 0)
1230 ;; Search for one.
1231 (save-excursion
1232 (let ((count 1))
1233 (while (and (not (= count 0))
1234 (not (eq (fortran-previous-statement) 'first-statement))
1235 ;; Keep local to subprogram.
1236 (not (and (looking-at fortran-end-prog-re)
1237 (fortran-check-end-prog-re))))
1238 (skip-chars-forward " \t0-9")
1239 (cond ((looking-at fortran-if-start-re)
1240 (save-excursion
1241 (if (or
1242 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
1243 (let (then-test) ; Multi-line if-then.
1244 (while
1245 (and
1246 (= (forward-line 1) 0)
1247 ;; Search forward for then.
1248 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1249 (not
1250 (setq then-test
1251 (looking-at
1252 (concat ".*then\\b[ \t]*"
1253 "[^ \t(=a-z0-9]"))))))
1254 then-test))
1255 (setq count (- count 1)))))
1256 ((looking-at "end[ \t]*if\\b")
1257 (setq count (+ count 1)))))
1258 (and (= count 0)
1259 ;; All pairs accounted for.
1260 (point)))))))
1262 (defun fortran-indent-line ()
1263 "Indent current Fortran line based on its contents and on previous lines."
1264 (interactive)
1265 (let ((cfi (fortran-calculate-indent)))
1266 (save-excursion
1267 (beginning-of-line)
1268 (if (or (not (= cfi (fortran-current-line-indentation)))
1269 (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
1270 (not (fortran-line-number-indented-correctly-p))))
1271 (fortran-indent-to-column cfi)
1272 (beginning-of-line)
1273 (if (fortran-find-comment-start-skip)
1274 (fortran-indent-comment))))
1275 ;; Never leave point in left margin.
1276 (if (< (current-column) cfi)
1277 (move-to-column cfi))
1278 (if (and auto-fill-function
1279 (> (save-excursion (end-of-line) (current-column))
1280 fill-column))
1281 (save-excursion
1282 (end-of-line)
1283 (fortran-fill)))
1284 (if fortran-blink-matching-if
1285 (progn
1286 (fortran-blink-matching-if)
1287 (fortran-blink-matching-do)))))
1289 (defun fortran-auto-fill ()
1290 (if (> (current-column) (current-fill-column))
1291 (let ((cfi (fortran-calculate-indent)))
1292 (save-excursion
1293 (beginning-of-line)
1294 (if (or (not (= cfi (fortran-current-line-indentation)))
1295 (and (re-search-forward "^[ \t]*[0-9]+"
1296 (+ (point) 4) t)
1297 (not (fortran-line-number-indented-correctly-p))))
1298 (fortran-indent-to-column cfi)
1299 (beginning-of-line)
1300 (if (fortran-find-comment-start-skip)
1301 (fortran-indent-comment))))
1302 (fortran-fill)
1303 ;; Never leave point in left margin.
1304 (if (< (current-column) cfi)
1305 (move-to-column cfi)))))
1307 ;; Historically this was a separate function which advertised itself
1308 ;; as reindenting but only did so where `most likely to be necessary'.
1309 (defalias 'fortran-indent-new-line 'reindent-then-newline-and-indent)
1311 (defun fortran-indent-subprogram ()
1312 "Properly indent the Fortran subprogram which contains point."
1313 (interactive)
1314 (save-excursion
1315 (mark-defun)
1316 (message "Indenting subprogram...")
1317 (indent-region (point) (mark) nil))
1318 (message "Indenting subprogram...done."))
1320 (defun fortran-calculate-indent ()
1321 "Calculates the Fortran indent column based on previous lines."
1322 (let (icol first-statement (case-fold-search t)
1323 (fortran-minimum-statement-indent
1324 (if indent-tabs-mode
1325 fortran-minimum-statement-indent-tab
1326 fortran-minimum-statement-indent-fixed)))
1327 (save-excursion
1328 (setq first-statement (fortran-previous-statement))
1329 (if first-statement
1330 (setq icol fortran-minimum-statement-indent)
1331 (progn
1332 (if (= (point) (point-min))
1333 (setq icol fortran-minimum-statement-indent)
1334 (setq icol (fortran-current-line-indentation)))
1335 (skip-chars-forward " \t0-9")
1336 (cond ((looking-at "\\(\\(\\sw\\|\\s_\\)+:[ \t]*\\)?if[ \t]*(")
1337 (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
1338 (let (then-test) ;multi-line if-then
1339 (while (and (= (forward-line 1) 0)
1340 ;;search forward for then
1341 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]")
1342 (not (setq then-test
1343 (looking-at
1344 ".*then\\b[ \t]\
1345 *[^ \t_$(=a-z0-9]")))))
1346 then-test))
1347 (setq icol (+ icol fortran-if-indent))))
1348 ((looking-at "else\\(if\\)?\\b")
1349 (setq icol (+ icol fortran-if-indent)))
1350 ((looking-at "select[ \t]*case[ \t](.*)")
1351 (setq icol (+ icol fortran-if-indent)))
1352 ((looking-at "case[ \t]*(.*)")
1353 (setq icol (+ icol fortran-if-indent)))
1354 ((looking-at "case[ \t]*default\\b")
1355 (setq icol (+ icol fortran-if-indent)))
1356 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1357 (setq icol (+ icol fortran-if-indent)))
1358 ((looking-at "where[ \t]*(.*)[ \t]*\n")
1359 (setq icol (+ icol fortran-if-indent)))
1360 ((looking-at "do\\b")
1361 (setq icol (+ icol fortran-do-indent)))
1362 ((looking-at
1363 "\\(structure\\|union\\|map\\|interface\\)\
1364 \\b[ \t]*[^ \t=(a-z]")
1365 (setq icol (+ icol fortran-structure-indent)))
1366 ((and (looking-at fortran-end-prog-re1)
1367 (fortran-check-end-prog-re))
1368 ;; Previous END resets indent to minimum
1369 (setq icol fortran-minimum-statement-indent))))))
1370 (save-excursion
1371 (beginning-of-line)
1372 (cond ((looking-at "[ \t]*$"))
1373 ;; Check for directive before comment, so as not to indent.
1374 ((looking-at fortran-directive-re)
1375 (setq fortran-minimum-statement-indent 0 icol 0))
1376 ((looking-at fortran-comment-line-start-skip)
1377 (cond ((eq fortran-comment-indent-style 'relative)
1378 (setq icol (+ icol fortran-comment-line-extra-indent)))
1379 ((eq fortran-comment-indent-style 'fixed)
1380 (setq icol (+ fortran-minimum-statement-indent
1381 fortran-comment-line-extra-indent))))
1382 (setq fortran-minimum-statement-indent 0))
1383 ((or (looking-at (concat "[ \t]*"
1384 (regexp-quote
1385 fortran-continuation-string)))
1386 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1387 (setq icol (+ icol fortran-continuation-indent)))
1388 (first-statement)
1389 ((and fortran-check-all-num-for-matching-do
1390 (looking-at "[ \t]*[0-9]+")
1391 (fortran-check-for-matching-do))
1392 (setq icol (- icol fortran-do-indent)))
1394 (skip-chars-forward " \t0-9")
1395 (cond ((looking-at "end[ \t]*\\(if\\|select\\|where\\)\\b")
1396 (setq icol (- icol fortran-if-indent)))
1397 ((looking-at "else\\(if\\)?\\b")
1398 (setq icol (- icol fortran-if-indent)))
1399 ((looking-at "case[ \t]*\\((.*)\\|default\\>\\)")
1400 (setq icol (- icol fortran-if-indent)))
1401 ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
1402 (setq icol (- icol fortran-if-indent)))
1403 ((and (looking-at "continue\\b")
1404 (fortran-check-for-matching-do))
1405 (setq icol (- icol fortran-do-indent)))
1406 ((looking-at "end[ \t]*do\\b")
1407 (setq icol (- icol fortran-do-indent)))
1408 ((looking-at "end[ \t]*\
1409 \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
1410 (setq icol (- icol fortran-structure-indent)))
1411 ((and (looking-at fortran-end-prog-re1)
1412 (fortran-check-end-prog-re)
1413 (not (= icol fortran-minimum-statement-indent)))
1414 (message "Warning: `end' not in column %d. Probably\
1415 an unclosed block." fortran-minimum-statement-indent))))))
1416 (max fortran-minimum-statement-indent icol)))
1418 (defun fortran-current-line-indentation ()
1419 "Indentation of current line, ignoring Fortran line number or continuation.
1420 This is the column position of the first non-whitespace character
1421 aside from the line number and/or column 5/8 line-continuation character.
1422 For comment lines, returns indentation of the first
1423 non-indentation text within the comment."
1424 (save-excursion
1425 (beginning-of-line)
1426 (cond ((looking-at fortran-comment-line-start-skip)
1427 (goto-char (match-end 0))
1428 (skip-chars-forward
1429 (if (stringp fortran-comment-indent-char)
1430 fortran-comment-indent-char
1431 (char-to-string fortran-comment-indent-char))))
1432 ((or (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1433 (goto-char (match-end 0)))
1435 ;; Move past line number.
1436 (skip-chars-forward "[ \t0-9]")))
1437 ;; Move past whitespace.
1438 (skip-chars-forward " \t")
1439 (current-column)))
1441 (defun fortran-indent-to-column (col)
1442 "Indent current line with spaces to column COL.
1443 notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
1444 line, and this continuation character is retained on indentation;
1445 2) If `fortran-continuation-string' is the first non-whitespace
1446 character, this is a continuation line;
1447 3) A non-continuation line which has a number as the first
1448 non-whitespace character is a numbered line.
1449 4) A TAB followed by a digit indicates a continuation line."
1450 (save-excursion
1451 (beginning-of-line)
1452 (if (looking-at fortran-comment-line-start-skip)
1453 (if fortran-comment-indent-style
1454 (let* ((char (if (stringp fortran-comment-indent-char)
1455 (aref fortran-comment-indent-char 0)
1456 fortran-comment-indent-char))
1457 (chars (string ? ?\t char)))
1458 (goto-char (match-end 0))
1459 (skip-chars-backward chars)
1460 (delete-region (point) (progn (skip-chars-forward chars)
1461 (point)))
1462 (insert-char char (- col (current-column)))))
1463 (if (looking-at "\t[1-9]")
1464 (if indent-tabs-mode
1465 (goto-char (match-end 0))
1466 (delete-char 2)
1467 (insert-char ? 5)
1468 (insert fortran-continuation-string))
1469 (if (looking-at " \\{5\\}[^ 0\n]")
1470 (if indent-tabs-mode
1471 (progn (delete-char 6)
1472 (insert ?\t (fortran-numerical-continuation-char) 1))
1473 (forward-char 6))
1474 (delete-horizontal-space)
1475 ;; Put line number in columns 0-4
1476 ;; or put continuation character in column 5.
1477 (cond ((eobp))
1478 ((looking-at (regexp-quote fortran-continuation-string))
1479 (if indent-tabs-mode
1480 (progn
1481 (indent-to
1482 (if indent-tabs-mode
1483 fortran-minimum-statement-indent-tab
1484 fortran-minimum-statement-indent-fixed))
1485 (delete-char 1)
1486 (insert-char (fortran-numerical-continuation-char) 1))
1487 (indent-to 5)
1488 (forward-char 1)))
1489 ((looking-at "[0-9]+")
1490 (let ((extra-space (- 5 (- (match-end 0) (point)))))
1491 (if (< extra-space 0)
1492 (message "Warning: line number exceeds 5-digit limit.")
1493 (indent-to (min fortran-line-number-indent extra-space))))
1494 (skip-chars-forward "0-9")))))
1495 ;; Point is now after any continuation character or line number.
1496 ;; Put body of statement where specified.
1497 (delete-horizontal-space)
1498 (indent-to col)
1499 ;; Indent any comment following code on the same line.
1500 (if (fortran-find-comment-start-skip)
1501 (progn (goto-char (match-beginning 0))
1502 (if (not (= (current-column) (fortran-comment-indent)))
1503 (progn (delete-horizontal-space)
1504 (indent-to (fortran-comment-indent)))))))))
1506 (defun fortran-line-number-indented-correctly-p ()
1507 "Return t if current line's line number is correctly indented.
1508 Do not call if there is no line number."
1509 (save-excursion
1510 (beginning-of-line)
1511 (skip-chars-forward " \t")
1512 (and (<= (current-column) fortran-line-number-indent)
1513 (or (= (current-column) fortran-line-number-indent)
1514 (progn (skip-chars-forward "0-9")
1515 (= (current-column) 5))))))
1517 (defun fortran-check-for-matching-do ()
1518 "When called from a numbered statement, return t if matching DO is found.
1519 Otherwise return nil."
1520 (let ((case-fold-search t)
1521 charnum)
1522 (save-excursion
1523 (beginning-of-line)
1524 (if (looking-at "[ \t]*[0-9]+")
1525 (progn
1526 (skip-chars-forward " \t")
1527 (skip-chars-forward "0") ;skip past leading zeros
1528 (setq charnum
1529 (buffer-substring (point) (progn
1530 (skip-chars-forward "0-9")
1531 (point))))
1532 (beginning-of-line)
1533 (save-restriction
1534 (save-excursion
1535 (narrow-to-defun)
1536 (and (re-search-backward
1537 (concat
1538 "\\(^[ \t0-9]*do[ \t]*0*"
1539 charnum "\\b\\)\\|" "\\(^[ \t]*0*"
1540 charnum "\\b\\)")
1541 nil t)
1542 (looking-at
1543 (concat "^[ \t0-9]*do[ \t]*0*"
1544 charnum))))))))))
1546 (defun fortran-find-comment-start-skip (&optional all)
1547 "Move to past `comment-start-skip' found on current line.
1548 Return non-nil if `comment-start-skip' found, nil if not.
1549 If ALL is nil, only match comments that start in column > 0."
1550 (interactive)
1551 ;; Hopefully at some point we can just use the line below! -stef
1552 ;; (comment-search-forward (line-end-position) t))
1553 (when (or all comment-start-skip)
1554 (let ((pos (point))
1555 (css (if comment-start-skip
1556 (concat fortran-comment-line-start-skip
1557 "\\|" comment-start-skip)
1558 fortran-comment-line-start-skip)))
1559 (when (re-search-forward css (line-end-position) t)
1560 (if (and (or all (> (match-beginning 0) (line-beginning-position)))
1561 (or (save-match-data
1562 (not (fortran-is-in-string-p (match-beginning 0))))
1563 ;; Recurse for rest of line.
1564 (fortran-find-comment-start-skip all)))
1565 (point)
1566 (goto-char pos)
1567 nil)))))
1569 ;;From: ralf@up3aud1.gwdg.de (Ralf Fassel)
1570 ;; Test if TAB format continuation lines work.
1571 (defun fortran-is-in-string-p (where)
1572 "Return non-nil iff WHERE (a buffer position) is inside a Fortran string."
1573 (save-excursion
1574 (goto-char where)
1575 (cond
1576 ((bolp) nil) ; bol is never inside a string
1577 ((save-excursion ; comment lines too
1578 (beginning-of-line)
1579 (looking-at fortran-comment-line-start-skip)) nil)
1580 (t (let (;; ok, serious now. Init some local vars:
1581 (parse-state '(0 nil nil nil nil nil 0))
1582 (quoted-comment-start (if comment-start
1583 (regexp-quote comment-start)))
1584 (not-done t)
1585 parse-limit end-of-line)
1586 ;; move to start of current statement
1587 (fortran-next-statement)
1588 (fortran-previous-statement)
1589 ;; now parse up to WHERE
1590 (while not-done
1591 (if (or ;; skip to next line if:
1592 ;; - comment line?
1593 (looking-at fortran-comment-line-start-skip)
1594 ;; - at end of line?
1595 (eolp)
1596 ;; - not in a string and after comment-start?
1597 (and (not (nth 3 parse-state))
1598 comment-start
1599 (equal comment-start
1600 (char-to-string (preceding-char)))))
1601 (if (> (forward-line) 0)
1602 (setq not-done nil))
1603 ;; else:
1604 ;; if we are at beginning of code line, skip any
1605 ;; whitespace, labels and tab continuation markers.
1606 (if (bolp) (skip-chars-forward " \t0-9"))
1607 ;; if we are in column <= 5 now, check for continuation char
1608 (cond ((= 5 (current-column)) (forward-char 1))
1609 ((and (< (current-column) 5)
1610 (equal fortran-continuation-string
1611 (char-to-string (following-char)))
1612 (forward-char 1))))
1613 ;; find out parse-limit from here
1614 (setq end-of-line (line-end-position))
1615 (setq parse-limit (min where end-of-line))
1616 ;; parse max up to comment-start, if non-nil and in current line
1617 (if comment-start
1618 (save-excursion
1619 (if (re-search-forward quoted-comment-start end-of-line t)
1620 (setq parse-limit (min (point) parse-limit)))))
1621 ;; now parse if still in limits
1622 (if (< (point) where)
1623 (setq parse-state (parse-partial-sexp
1624 (point) parse-limit nil nil parse-state))
1625 (setq not-done nil))))
1626 ;; result is
1627 (nth 3 parse-state))))))
1629 ;; From old version.
1630 (defalias 'fortran-auto-fill-mode 'auto-fill-mode)
1632 (defun fortran-fill ()
1633 (let* ((auto-fill-function #'fortran-auto-fill)
1634 (opoint (point))
1635 (bol (line-beginning-position))
1636 (eol (line-end-position))
1637 (bos (min eol (+ bol (fortran-current-line-indentation))))
1638 (quote
1639 (save-excursion
1640 (goto-char bol)
1641 (if (looking-at fortran-comment-line-start-skip)
1642 nil ; OK to break quotes on comment lines.
1643 (move-to-column fill-column)
1644 (if (fortran-is-in-string-p (point))
1645 (save-excursion (re-search-backward "\\S\"\\s\"\\S\"" bol t)
1646 (if fortran-break-before-delimiters
1647 (point)
1648 (1+ (point))))))))
1649 ;; decide where to split the line. If a position for a quoted
1650 ;; string was found above then use that, else break the line
1651 ;; before the last delimiter.
1652 ;; Delimiters are whitespace, commas, and operators.
1653 ;; Will break before a pair of *'s.
1654 (fill-point
1655 (or quote
1656 (save-excursion
1657 (move-to-column (1+ fill-column))
1658 (skip-chars-backward "^ \t\n,'+-/*=)"
1659 ;;; (if fortran-break-before-delimiters
1660 ;;; "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
1662 (if (<= (point) (1+ bos))
1663 (progn
1664 (move-to-column (1+ fill-column))
1665 ;;what is this doing???
1666 (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t))
1667 (goto-char bol))))
1668 (if (bolp)
1669 (re-search-forward "[ \t]" opoint t)
1670 (backward-char)
1671 (if (looking-at "\\s\"")
1672 (forward-char)
1673 (skip-chars-backward " \t\*")))
1674 (if fortran-break-before-delimiters
1675 (point)
1676 (1+ (point)))))))
1677 ;; if we are in an in-line comment, don't break unless the
1678 ;; line of code is longer than it should be. Otherwise
1679 ;; break the line at the column computed above.
1681 ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
1682 ;; don't prevent a break.
1683 (when (and (save-excursion
1684 (beginning-of-line)
1685 (if (not (fortran-find-comment-start-skip))
1687 (goto-char (match-beginning 0))
1688 (>= (point) fill-point)))
1689 (save-excursion
1690 (goto-char fill-point)
1691 (not (bolp)))
1692 (> (save-excursion
1693 (goto-char opoint)
1694 (current-column))
1695 (min (1+ fill-column)
1696 (+ (fortran-calculate-indent)
1697 fortran-continuation-indent))))
1698 (goto-char fill-point)
1699 (fortran-break-line)
1700 (end-of-line))))
1702 (defun fortran-break-line ()
1703 (let ((opoint (point))
1704 (bol (line-beginning-position))
1705 (comment-string
1706 (save-excursion
1707 (if (fortran-find-comment-start-skip)
1708 (delete-and-extract-region
1709 (match-beginning 0) (line-end-position))))))
1710 ;; Forward line 1 really needs to go to next non white line
1711 (if (save-excursion (forward-line)
1712 (looking-at " \\{5\\}[^ 0\n]\\|\t[1-9]"))
1713 (progn
1714 (end-of-line)
1715 (delete-region (point) (match-end 0))
1716 (delete-horizontal-space)
1717 (fortran-fill))
1718 (fortran-split-line))
1719 (if comment-string
1720 (save-excursion
1721 (goto-char bol)
1722 (end-of-line)
1723 (delete-horizontal-space)
1724 (indent-to (fortran-comment-indent))
1725 (insert comment-string)))))
1727 (defun fortran-analyze-file-format ()
1728 "Return nil if fixed format is used, t if TAB formatting is used.
1729 Use `fortran-tab-mode-default' if no non-comment statements are found in the
1730 file before the end or the first `fortran-analyze-depth' lines."
1731 (let ((i 0))
1732 (save-excursion
1733 (goto-char (point-min))
1734 (setq i 0)
1735 (while (not (or
1736 (eobp)
1737 (eq (char-after) ?\t)
1738 (looking-at " \\{6\\}")
1739 (> i fortran-analyze-depth)))
1740 (forward-line)
1741 (setq i (1+ i)))
1742 (cond
1743 ((eq (char-after) ?\t) t)
1744 ((looking-at " \\{6\\}") nil)
1745 (fortran-tab-mode-default t)
1746 (t nil)))))
1748 (or (assq 'fortran-tab-mode-string minor-mode-alist)
1749 (setq minor-mode-alist (cons
1750 '(fortran-tab-mode-string
1751 (indent-tabs-mode fortran-tab-mode-string))
1752 minor-mode-alist)))
1754 (defun fortran-fill-paragraph (&optional justify)
1755 "Fill surrounding comment block as paragraphs, else fill statement.
1756 Intended as the value of `fill-paragraph-function'."
1757 (interactive "P")
1758 (or (fill-comment-paragraph justify)
1759 (fortran-fill-statement)
1762 (defun fortran-fill-statement ()
1763 "Fill a fortran statement up to `fill-column'."
1764 (interactive)
1765 (let ((auto-fill-function #'fortran-auto-fill))
1766 (if (not (save-excursion
1767 (beginning-of-line)
1768 (or (looking-at "[ \t]*$")
1769 (looking-at fortran-comment-line-start-skip)
1770 (and comment-start-skip
1771 (looking-at (concat "[ \t]*" comment-start-skip))))))
1772 (save-excursion
1773 ;; Find beginning of statement.
1774 (fortran-next-statement)
1775 (fortran-previous-statement)
1776 ;; Re-indent initially.
1777 (fortran-indent-line)
1778 ;; Replace newline plus continuation field plus indentation with
1779 ;; single space.
1780 (while (progn
1781 (forward-line)
1782 (fortran-remove-continuation)))
1783 (fortran-previous-statement)))
1784 (fortran-indent-line)))
1786 (defun fortran-strip-sequence-nos (&optional do-space)
1787 "Delete all text in column 72 and up (assumed to be sequence numbers).
1788 Normally also deletes trailing whitespace after stripping such text.
1789 Supplying prefix arg DO-SPACE prevents stripping the whitespace."
1790 (interactive "p")
1791 (save-excursion
1792 (goto-char (point-min))
1793 (while (re-search-forward "^.\\{72\\}\\(.*\\)" nil t)
1794 (replace-match "" nil nil nil 1)
1795 (unless do-space (delete-horizontal-space)))))
1797 ;; This code used to live in add-log.el, but this is a better place
1798 ;; for it.
1799 (defun fortran-current-defun ()
1800 "Function to use for `add-log-current-defun-function' in Fortran mode."
1801 (save-excursion
1802 ;; We must be inside function body for this to work.
1803 (fortran-beginning-of-subprogram)
1804 (let ((case-fold-search t)) ; case-insensitive
1805 ;; search for fortran subprogram start
1806 (if (re-search-forward
1807 (concat "^[ \t]*\\(program\\|subroutine\\|function"
1808 "\\|[ \ta-z0-9*()]*[ \t]+function\\|"
1809 "\\(block[ \t]*data\\)\\)")
1810 (save-excursion (fortran-end-of-subprogram)
1811 (point))
1813 (or (match-string-no-properties 2)
1814 (progn
1815 ;; move to EOL or before first left paren
1816 (if (re-search-forward "[(\n]" nil t)
1817 (progn (backward-char)
1818 (skip-chars-backward " \t"))
1819 (end-of-line))
1820 ;; Use the name preceding that.
1821 (buffer-substring-no-properties (point) (progn (backward-sexp)
1822 (point)))))
1823 "main"))))
1825 (provide 'fortran)
1827 ;;; fortran.el ends here