(f90-font-lock-keywords-1): Fix highlighting of `type' forms.
[emacs.git] / lisp / progmodes / f90.el
blob3c73fab604eb02baf3cb7abbd6383890ea6e2f0c
1 ;;; f90.el --- Fortran-90 mode (free format)
3 ;; Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
5 ;; Author: Torbj\"orn Einarsson <Torbjorn.Einarsson@era.ericsson.se>
6 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
7 ;; Keywords: fortran, f90, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Major mode for editing F90 programs in FREE FORMAT.
29 ;; The minor language revision F95 is also supported (with font-locking).
31 ;; Knows about continuation lines, named structured statements, and other
32 ;; features in F90 including HPF (High Performance Fortran) structures.
33 ;; The basic feature provides accurate indentation of F90 programs.
34 ;; In addition, there are many more features like automatic matching of all
35 ;; end statements, an auto-fill function to break long lines, a join-lines
36 ;; function which joins continued lines, etc.
38 ;; To facilitate typing, a fairly complete list of abbreviations is provided.
39 ;; All abbreviations begin with the backquote character "`"
40 ;; (this requires modification of the syntax-table).
41 ;; For example, `i expands to integer (if abbrev-mode is on).
43 ;; There are two separate features for altering the appearance of code:
44 ;; 1) Upcasing or capitalizing of all keywords.
45 ;; 2) Colors/fonts using font-lock-mode.
46 ;; Automatic upcase or downcase of keywords is controlled by the variable
47 ;; f90-auto-keyword-case.
49 ;; The indentations of lines starting with ! is determined by the first of the
50 ;; following matches (values in the left column are the defaults):
52 ;; start-string/regexp indent variable holding start-string/regexp
53 ;; !!! 0
54 ;; !hpf\\$ (re) 0 f90-directive-comment-re
55 ;; !!$ 0 f90-comment-region
56 ;; ! (re) as code f90-indented-comment-re
57 ;; default comment-column
59 ;; Ex: Here is the result of 3 different settings of f90-indented-comment-re
60 ;; f90-indented-comment-re !-indentation !!-indentation
61 ;; ! as code as code
62 ;; !! comment-column as code
63 ;; ![^!] as code comment-column
64 ;; Trailing comments are indented to comment-column with indent-for-comment.
65 ;; The function f90-comment-region toggles insertion of
66 ;; the variable f90-comment-region in every line of the region.
68 ;; One common convention for free vs. fixed format is that free-format files
69 ;; have the ending .f90 or .f95 while fixed format files have the ending .f.
70 ;; Emacs automatically loads Fortran files in the appropriate mode based
71 ;; on extension. You can modify this by adjusting the variable auto-mode-alist.
72 ;; For example:
73 ;; (add-to-list 'auto-mode-alist '("\\.f\\'" . f90-mode))
75 ;; Once you have entered f90-mode, you may get more info by using
76 ;; the command describe-mode (C-h m). For online help use
77 ;; C-h f <Name of function you want described>, or
78 ;; C-h v <Name of variable you want described>.
80 ;; To customize f90-mode for your taste, use, for example:
81 ;; (you don't have to specify values for all the parameters below)
83 ;;(add-hook 'f90-mode-hook
84 ;; ;; These are the default values.
85 ;; '(lambda () (setq f90-do-indent 3
86 ;; f90-if-indent 3
87 ;; f90-type-indent 3
88 ;; f90-program-indent 2
89 ;; f90-continuation-indent 5
90 ;; f90-comment-region "!!$"
91 ;; f90-directive-comment-re "!hpf\\$"
92 ;; f90-indented-comment-re "!"
93 ;; f90-break-delimiters "[-+\\*/><=,% \t]"
94 ;; f90-break-before-delimiters t
95 ;; f90-beginning-ampersand t
96 ;; f90-smart-end 'blink
97 ;; f90-auto-keyword-case nil
98 ;; f90-leave-line-no nil
99 ;; indent-tabs-mode nil
100 ;; f90-font-lock-keywords f90-font-lock-keywords-2
101 ;; )
102 ;; ;; These are not default.
103 ;; (abbrev-mode 1) ; turn on abbreviation mode
104 ;; (f90-add-imenu-menu) ; extra menu with functions etc.
105 ;; (if f90-auto-keyword-case ; change case of all keywords on startup
106 ;; (f90-change-keywords f90-auto-keyword-case))
107 ;; ))
109 ;; in your .emacs file. You can also customize the lists
110 ;; f90-font-lock-keywords, etc.
112 ;; The auto-fill and abbreviation minor modes are accessible from the F90 menu,
113 ;; or by using M-x auto-fill-mode and M-x abbrev-mode, respectively.
115 ;; Remarks
116 ;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
117 ;; non-nil, the line numbers are never touched.
118 ;; 2) Multi-; statements like "do i=1,20 ; j=j+i ; end do" are not handled
119 ;; correctly, but I imagine them to be rare.
120 ;; 3) Regexps for hilit19 are no longer supported.
121 ;; 4) For FIXED FORMAT code, use fortran mode.
122 ;; 5) This mode does not work under emacs-18.x.
123 ;; 6) Preprocessor directives, i.e., lines starting with # are left-justified
124 ;; and are untouched by all case-changing commands. There is, at present, no
125 ;; mechanism for treating multi-line directives (continued by \ ).
126 ;; 7) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
127 ;; You are urged to use f90-do loops (with labels if you wish).
128 ;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
130 ;; List of user commands
131 ;; f90-previous-statement f90-next-statement
132 ;; f90-beginning-of-subprogram f90-end-of-subprogram f90-mark-subprogram
133 ;; f90-comment-region
134 ;; f90-indent-line f90-indent-new-line
135 ;; f90-indent-region (can be called by calling indent-region)
136 ;; f90-indent-subprogram
137 ;; f90-break-line f90-join-lines
138 ;; f90-fill-region
139 ;; f90-insert-end
140 ;; f90-upcase-keywords f90-upcase-region-keywords
141 ;; f90-downcase-keywords f90-downcase-region-keywords
142 ;; f90-capitalize-keywords f90-capitalize-region-keywords
143 ;; f90-add-imenu-menu
144 ;; f90-font-lock-1, f90-font-lock-2, f90-font-lock-3, f90-font-lock-4
146 ;; Original author's thanks
147 ;; Thanks to all the people who have tested the mode. Special thanks to Jens
148 ;; Bloch Helmers for encouraging me to write this code, for creative
149 ;; suggestions as well as for the lists of hpf-commands.
150 ;; Also thanks to the authors of the fortran and pascal modes, on which some
151 ;; of this code is built.
153 ;; TODO
154 ;; Support for hideshow, align.
155 ;; OpenMP, preprocessor highlighting.
157 ;;; Code:
159 ;; User options
161 (defgroup f90 nil
162 "Major mode for editing Fortran 90,95 code."
163 :group 'languages)
165 (defgroup f90-indent nil
166 "Indentation in free-format Fortran."
167 :prefix "f90-"
168 :group 'f90)
171 (defcustom f90-do-indent 3
172 "*Extra indentation applied to DO blocks."
173 :type 'integer
174 :group 'f90-indent)
176 (defcustom f90-if-indent 3
177 "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
178 :type 'integer
179 :group 'f90-indent)
181 (defcustom f90-type-indent 3
182 "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks."
183 :type 'integer
184 :group 'f90-indent)
186 (defcustom f90-program-indent 2
187 "*Extra indentation applied to PROGRAM/MODULE/SUBROUTINE/FUNCTION blocks."
188 :type 'integer
189 :group 'f90-indent)
191 (defcustom f90-continuation-indent 5
192 "*Extra indentation applied to F90 continuation lines."
193 :type 'integer
194 :group 'f90-indent)
196 (defcustom f90-comment-region "!!$"
197 "*String inserted by \\[f90-comment-region] at start of each line in region."
198 :type 'string
199 :group 'f90-indent)
201 (defcustom f90-indented-comment-re "!"
202 "*Regexp saying which comments to indent like code."
203 :type 'regexp
204 :group 'f90-indent)
206 (defcustom f90-directive-comment-re "!hpf\\$"
207 "*Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
208 :type 'regexp
209 :group 'f90-indent)
211 (defcustom f90-beginning-ampersand t
212 "*Non-nil gives automatic insertion of \& at start of continuation line."
213 :type 'boolean
214 :group 'f90)
216 (defcustom f90-smart-end 'blink
217 "*From an END statement, check and fill the end using matching block start.
218 Allowed values are 'blink, 'no-blink, and nil, which determine
219 whether to blink the matching beginning."
220 :type '(choice (const blink) (const no-blink) (const nil))
221 :group 'f90)
223 (defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
224 "*Regexp holding list of delimiters at which lines may be broken."
225 :type 'regexp
226 :group 'f90)
228 (defcustom f90-break-before-delimiters t
229 "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
230 :type 'boolean
231 :group 'f90)
233 (defcustom f90-auto-keyword-case nil
234 "*Automatic case conversion of keywords.
235 The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil."
236 :type '(choice (const downcase-word) (const upcase-word)
237 (const capitalize-word) (const nil))
238 :group 'f90)
240 (defcustom f90-leave-line-no nil
241 "*If non-nil, line numbers are not left justified."
242 :type 'boolean
243 :group 'f90)
245 (defconst f90-xemacs-flag (string-match "XEmacs\\|Lucid" emacs-version)
246 "Non-nil means F90 mode thinks it is running under XEmacs.")
248 (defconst f90-keywords-re
249 (regexp-opt '("allocatable" "allocate" "assign" "assignment" "backspace"
250 "block" "call" "case" "character" "close" "common" "complex"
251 "contains" "continue" "cycle" "data" "deallocate"
252 "dimension" "do" "double" "else" "elseif" "elsewhere" "end"
253 "enddo" "endfile" "endif" "entry" "equivalence" "exit"
254 "external" "forall" "format" "function" "goto" "if"
255 "implicit" "include" "inquire" "integer" "intent"
256 "interface" "intrinsic" "logical" "module" "namelist" "none"
257 "nullify" "only" "open" "operator" "optional" "parameter"
258 "pause" "pointer" "precision" "print" "private" "procedure"
259 "program" "public" "read" "real" "recursive" "result" "return"
260 "rewind" "save" "select" "sequence" "stop" "subroutine"
261 "target" "then" "type" "use" "where" "while" "write"
262 ;; F95 keywords.
263 "elemental" "pure") 'words)
264 "Regexp for F90 keywords.")
266 (defconst f90-keywords-level-3-re
267 (regexp-opt
268 '("allocatable" "allocate" "assign" "assignment" "backspace"
269 "close" "deallocate" "dimension" "endfile" "entry" "equivalence"
270 "external" "inquire" "intent" "intrinsic" "nullify" "only" "open"
271 "operator" "optional" "parameter" "pause" "pointer" "print" "private"
272 "public" "read" "recursive" "result" "rewind" "save" "select"
273 "sequence" "target" "write"
274 ;; F95 keywords.
275 "elemental" "pure") 'words)
276 "Keyword-regexp for font-lock level >= 3.")
278 (defconst f90-procedures-re
279 (concat "\\<"
280 (regexp-opt
281 '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
282 "all" "allocated" "anint" "any" "asin" "associated"
283 "atan" "atan2" "bit_size" "btest" "ceiling" "char" "cmplx"
284 "conjg" "cos" "cosh" "count" "cshift" "date_and_time" "dble"
285 "digits" "dim" "dot_product" "dprod" "eoshift" "epsilon"
286 "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
287 "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior"
288 "ishft" "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt"
289 "lle" "llt" "log" "log10" "logical" "matmul" "max"
290 "maxexponent" "maxloc" "maxval" "merge" "min" "minexponent"
291 "minloc" "minval" "mod" "modulo" "mvbits" "nearest" "nint"
292 "not" "pack" "precision" "present" "product" "radix"
293 ;; Real is taken out here to avoid highlighting declarations.
294 "random_number" "random_seed" "range" ;; "real"
295 "repeat" "reshape" "rrspacing" "scale" "scan"
296 "selected_int_kind" "selected_real_kind" "set_exponent"
297 "shape" "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt"
298 "sum" "system_clock" "tan" "tanh" "tiny" "transfer"
299 "transpose" "trim" "ubound" "unpack" "verify"
300 ;; F95 intrinsic functions.
301 "null" "cpu_time") t)
302 ;; A left parenthesis to avoid highlighting non-procedures.
303 "[ \t]*(")
304 "Regexp whose first part matches F90 intrinsic procedures.")
306 (defconst f90-operators-re
307 (concat "\\."
308 (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
309 "neqv" "not" "or" "true") t)
310 "\\.")
311 "Regexp matching intrinsic operators.")
313 (defconst f90-hpf-keywords-re
314 (regexp-opt
315 ;; Intrinsic procedures.
316 '("all_prefix" "all_scatter" "all_suffix" "any_prefix"
317 "any_scatter" "any_suffix" "copy_prefix" "copy_scatter"
318 "copy_suffix" "count_prefix" "count_scatter" "count_suffix"
319 "grade_down" "grade_up"
320 "hpf_alignment" "hpf_distribution" "hpf_template" "iall" "iall_prefix"
321 "iall_scatter" "iall_suffix" "iany" "iany_prefix" "iany_scatter"
322 "iany_suffix" "ilen" "iparity" "iparity_prefix"
323 "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
324 "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter"
325 "minval_suffix" "number_of_processors" "parity"
326 "parity_prefix" "parity_scatter" "parity_suffix" "popcnt" "poppar"
327 "processors_shape" "product_prefix" "product_scatter"
328 "product_suffix" "sum_prefix" "sum_scatter" "sum_suffix"
329 ;; Directives.
330 "align" "distribute" "dynamic" "independent" "inherit" "processors"
331 "realign" "redistribute" "template"
332 ;; Keywords.
333 "block" "cyclic" "extrinsic" "new" "onto" "pure" "with") 'words)
334 "Regexp for all HPF keywords, procedures and directives.")
336 ;; Highlighting patterns.
338 (defvar f90-font-lock-keywords-1
339 (list
340 ;; Special highlighting of "module procedure".
341 '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face))
342 ;; Highlight declaration of derived type.
343 '("\\<\\(\\(?:end[ \t]*\\)?type\\)\\>[ \t]*\\([^()\n]*::[ \t]*\\)?\
344 \\(\\sw+\\)"
345 (1 font-lock-keyword-face) (3 font-lock-function-name-face))
346 ;; Other functions and declarations.
347 '("\\<\\(\\(?:end[ \t]*\\)?\\(program\\|module\\|function\\|\
348 subroutine\\)\\|use\\|call\\)\\>[ \t]*\\(\\sw+\\)?"
349 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
350 "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
351 "This does fairly subdued highlighting of comments and function calls.")
353 (defvar f90-font-lock-keywords-2
354 (append
355 f90-font-lock-keywords-1
356 (list
357 ;; Variable declarations (avoid the real function call).
358 '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
359 logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\([^!\n]*\\)"
360 (1 font-lock-type-face t) (4 font-lock-variable-name-face))
361 ;; do, if, select, where, and forall constructs.
362 '("\\<\\(end[ \t]*\\(do\\|if\\|select\\|forall\\|where\\)\\)\\>\
363 \\([ \t]+\\(\\sw+\\)\\)?"
364 (1 font-lock-keyword-face) (3 font-lock-constant-face nil t))
365 '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|\
366 do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
367 (2 font-lock-constant-face nil t) (3 font-lock-keyword-face))
368 ;; Implicit declaration.
369 '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\
370 \\|logical\\|type[ \t]*(\\sw+)\\|none\\)[ \t]*"
371 (1 font-lock-keyword-face) (2 font-lock-type-face))
372 '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
373 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
374 "\\<else\\([ \t]*if\\|where\\)?\\>"
375 "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
376 '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>"
377 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
378 '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
379 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
380 (1 font-lock-keyword-face) (2 font-lock-constant-face))
381 ;; Line numbers (lines whose first character after number is letter).
382 '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t))))
383 "Highlights declarations, do-loops and other constructs.")
385 (defvar f90-font-lock-keywords-3
386 (append f90-font-lock-keywords-2
387 (list
388 f90-keywords-level-3-re
389 f90-operators-re
390 (list f90-procedures-re '(1 font-lock-keyword-face keep))
391 "\\<real\\>" ; avoid overwriting real defs
393 "Highlights all F90 keywords and intrinsic procedures.")
395 (defvar f90-font-lock-keywords-4
396 (append f90-font-lock-keywords-3
397 (list f90-hpf-keywords-re))
398 "Highlights all F90 and HPF keywords.")
400 (defvar f90-font-lock-keywords
401 f90-font-lock-keywords-2
402 "*Default expressions to highlight in F90 mode.")
405 (defvar f90-mode-syntax-table
406 (let ((table (make-syntax-table)))
407 (modify-syntax-entry ?\! "<" table) ; begin comment
408 (modify-syntax-entry ?\n ">" table) ; end comment
409 (modify-syntax-entry ?_ "w" table) ; underscore in names
410 (modify-syntax-entry ?\' "\"" table) ; string quote
411 (modify-syntax-entry ?\" "\"" table) ; string quote
412 (modify-syntax-entry ?\` "w" table) ; for abbrevs
413 (modify-syntax-entry ?\r " " table) ; return is whitespace
414 (modify-syntax-entry ?+ "." table) ; punctuation
415 (modify-syntax-entry ?- "." table)
416 (modify-syntax-entry ?= "." table)
417 (modify-syntax-entry ?* "." table)
418 (modify-syntax-entry ?/ "." table)
419 (modify-syntax-entry ?\\ "\\" table) ; escape chars
420 table)
421 "Syntax table used in F90 mode.")
423 (defvar f90-mode-map
424 (let ((map (make-sparse-keymap)))
425 (define-key map "`" 'f90-abbrev-start)
426 (define-key map "\C-c;" 'f90-comment-region)
427 (define-key map "\C-\M-a" 'f90-beginning-of-subprogram)
428 (define-key map "\C-\M-e" 'f90-end-of-subprogram)
429 (define-key map "\C-\M-h" 'f90-mark-subprogram)
430 (define-key map "\C-\M-n" 'f90-end-of-block)
431 (define-key map "\C-\M-p" 'f90-beginning-of-block)
432 (define-key map "\C-\M-q" 'f90-indent-subprogram)
433 (define-key map "\C-j" 'f90-indent-new-line) ; LFD equals C-j
434 (define-key map "\r" 'newline)
435 (define-key map "\C-c\r" 'f90-break-line)
436 ;;; (define-key map [M-return] 'f90-break-line)
437 (define-key map "\C-c\C-a" 'f90-previous-block)
438 (define-key map "\C-c\C-e" 'f90-next-block)
439 (define-key map "\C-c\C-d" 'f90-join-lines)
440 (define-key map "\C-c\C-f" 'f90-fill-region)
441 (define-key map "\C-c\C-p" 'f90-previous-statement)
442 (define-key map "\C-c\C-n" 'f90-next-statement)
443 (define-key map "\C-c\C-w" 'f90-insert-end)
444 (define-key map "\t" 'f90-indent-line)
445 (define-key map "," 'f90-electric-insert)
446 (define-key map "+" 'f90-electric-insert)
447 (define-key map "-" 'f90-electric-insert)
448 (define-key map "*" 'f90-electric-insert)
449 (define-key map "/" 'f90-electric-insert)
450 map)
451 "Keymap used in F90 mode.")
453 ;; Menu bar support.
454 (if f90-xemacs-flag
455 (defvar f90-xemacs-menu
456 '("F90"
457 ["Indent Subprogram" f90-indent-subprogram t]
458 ["Mark Subprogram" f90-mark-subprogram t]
459 ["Beginning of Subprogram" f90-beginning-of-subprogram t]
460 ["End of Subprogram" f90-end-of-subprogram t]
461 "-----"
462 ["(Un)Comment Region" f90-comment-region t]
463 ["Indent Region" indent-region t]
464 ["Fill Region" f90-fill-region t]
465 "-----"
466 ["Break Line at Point" f90-break-line t]
467 ["Join with Next Line" f90-join-lines t]
468 ["Insert Newline" newline t]
469 ["Insert Block End" f90-insert-end t]
470 "-----"
471 ["Upcase Keywords (buffer)" f90-upcase-keywords t]
472 ["Upcase Keywords (region)" f90-upcase-region-keywords t]
473 ["Capitalize Keywords (buffer)" f90-capitalize-keywords t]
474 ["Capitalize Keywords (region)" f90-capitalize-region-keywords t]
475 ["Downcase Keywords (buffer)" f90-downcase-keywords t]
476 ["Downcase Keywords (region)" f90-downcase-region-keywords t]
477 "-----"
478 ["Toggle abbrev-mode" abbrev-mode t]
479 ["Toggle auto-fill" auto-fill-mode t])
480 "XEmacs menu for F90 mode.")
482 ;; Emacs.
483 (defvar f90-menu-bar-menu
484 (let ((map (make-sparse-keymap "F90")))
485 (define-key map [f90-imenu-menu]
486 '("Add imenu Menu" . f90-add-imenu-menu))
487 (define-key map [abbrev-mode]
488 '("Toggle abbrev-mode" . abbrev-mode))
489 (define-key map [auto-fill-mode]
490 '("Toggle auto-fill" . auto-fill-mode))
491 (define-key map [line1] '("--"))
492 (define-key map [f90-change-case-menu]
493 '("Change Keyword Case" . f90-change-case-menu))
494 (define-key map [f90-font-lock-menu]
495 '("Highlighting" . f90-font-lock-menu))
496 (define-key map [line2] '("--"))
497 (define-key map [f90-insert-end]
498 '("Insert Block End" . f90-insert-end))
499 (define-key map [f90-join-lines]
500 '("Join with Next Line" . f90-join-lines))
501 (define-key map [f90-break-line]
502 '("Break Line at Point" . f90-break-line))
503 (define-key map [line3] '("--"))
504 (define-key map [f90-fill-region]
505 '("Fill Region" . f90-fill-region))
506 (put 'f90-fill-region 'menu-enable 'mark-active)
507 (define-key map [indent-region]
508 '("Indent Region" . indent-region))
509 (define-key map [f90-comment-region]
510 '("(Un)Comment Region" . f90-comment-region))
511 (put 'f90-comment-region 'menu-enable 'mark-active)
512 (define-key map [line4] '("--"))
513 (define-key map [f90-end-of-subprogram]
514 '("End of Subprogram" . f90-end-of-subprogram))
515 (define-key map [f90-beginning-of-subprogram]
516 '("Beginning of Subprogram" . f90-beginning-of-subprogram))
517 (define-key map [f90-mark-subprogram]
518 '("Mark Subprogram" . f90-mark-subprogram))
519 (define-key map [f90-indent-subprogram]
520 '("Indent Subprogram" . f90-indent-subprogram))
521 map)
522 "F90 mode top-level menu bar menu.")
524 (define-key f90-mode-map [menu-bar f90-menu]
525 (cons "F90" f90-menu-bar-menu))
527 (defvar f90-change-case-menu
528 (let ((map (make-sparse-keymap "Change Keyword Case")))
529 (define-key map [dkr]
530 '("Downcase Keywords (region)" . f90-downcase-region-keywords))
531 (put 'f90-downcase-region-keywords 'menu-enable 'mark-active)
532 (define-key map [ckr]
533 '("Capitalize Keywords (region)" . f90-capitalize-region-keywords))
534 (put 'f90-capitalize-region-keywords 'menu-enable 'mark-active)
535 (define-key map [ukr]
536 '("Upcase Keywords (region)" . f90-upcase-region-keywords))
537 (put 'f90-upcase-region-keywords 'menu-enable 'mark-active)
538 (define-key map [line] '("--"))
539 (define-key map [dkb]
540 '("Downcase Keywords (buffer)" . f90-downcase-keywords))
541 (define-key map [ckb]
542 '("Capitalize Keywords (buffer)" . f90-capitalize-keywords))
543 (define-key map [ukb]
544 '("Upcase Keywords (buffer)" . f90-upcase-keywords))
545 map)
546 "Submenu for change of case.")
548 (defalias 'f90-change-case-menu f90-change-case-menu)
550 ;; Font-lock-menu and function calls.
551 (defalias 'f90-font-lock-on 'font-lock-mode)
552 (put 'f90-font-lock-on 'menu-enable 'font-lock-mode)
553 (put 'f90-font-lock-on 'menu-alias t)
555 (defalias 'f90-font-lock-off 'font-lock-mode)
556 (put 'f90-font-lock-off 'menu-enable '(not font-lock-mode))
557 (put 'f90-font-lock-off 'menu-alias t)
559 (defun f90-font-lock-1 ()
560 "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
561 (interactive)
562 (font-lock-mode 1)
563 (setq font-lock-keywords f90-font-lock-keywords-1)
564 (font-lock-fontify-buffer))
566 (defun f90-font-lock-2 ()
567 "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
568 (interactive)
569 (font-lock-mode 1)
570 (setq font-lock-keywords f90-font-lock-keywords-2)
571 (font-lock-fontify-buffer))
573 (defun f90-font-lock-3 ()
574 "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
575 (interactive)
576 (font-lock-mode 1)
577 (setq font-lock-keywords f90-font-lock-keywords-3)
578 (font-lock-fontify-buffer))
580 (defun f90-font-lock-4 ()
581 "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
582 (interactive)
583 (font-lock-mode 1)
584 (setq font-lock-keywords f90-font-lock-keywords-4)
585 (font-lock-fontify-buffer))
587 (defvar f90-font-lock-menu
588 (let ((map (make-sparse-keymap "f90-font-lock-menu")))
589 (define-key map [h4]
590 '("Maximum highlighting (level 4)" . f90-font-lock-4))
591 (define-key map [h3]
592 '("Heavy highlighting (level 3)" . f90-font-lock-3))
593 (define-key map [h2]
594 '("Default highlighting (level 2)" . f90-font-lock-2))
595 (define-key map [h1]
596 '("Light highlighting (level 1)" . f90-font-lock-1))
597 (define-key map [line] '("--"))
598 (define-key map [floff]
599 '("Turn off font-lock-mode" . f90-font-lock-on))
600 (define-key map [flon]
601 '("Turn on font-lock-mode" . f90-font-lock-off))
602 map)
603 "Submenu for highlighting using font-lock-mode.")
605 (defalias 'f90-font-lock-menu f90-font-lock-menu)
609 ;; Regexps for finding program structures.
610 (defconst f90-blocks-re
611 (concat "\\(block[ \t]*data\\|"
612 (regexp-opt '("do" "if" "interface" "function" "module" "program"
613 "select" "subroutine" "type" "where" "forall"))
614 "\\)\\>")
615 "Regexp potentially indicating a \"block\" of F90 code.")
617 (defconst f90-program-block-re
618 (regexp-opt '("program" "module" "subroutine" "function") 'paren)
619 "Regexp used to locate the start/end of a \"subprogram\".")
621 (defconst f90-else-like-re
622 "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\)"
623 "Regexp matching an ELSE IF, ELSEWHERE, CASE statement.")
625 (defconst f90-end-if-re
626 (concat "end[ \t]*"
627 (regexp-opt '("if" "select" "where" "forall") 'paren)
628 "\\>")
629 "Regexp matching the end of an IF, SELECT, WHERE, FORALL block.")
631 (defconst f90-end-type-re
632 "end[ \t]*\\(type\\|interface\\|block[ \t]*data\\)\\>"
633 "Regexp matching the end of a TYPE, INTERFACE, BLOCK DATA section.")
635 (defconst f90-type-def-re
636 "\\<\\(type\\)\\>[ \t]*\\(?:[^()\n]*::[ \t]*\\)?\\(\\sw+\\)"
637 "Regexp matching the definition of a derived type.")
639 (defconst f90-no-break-re
640 (regexp-opt '("**" "//" "=>") 'paren)
641 "Regexp specifying where not to break lines when filling.")
643 (defvar f90-cache-position nil
644 "Temporary position used to speed up region operations.")
645 (make-variable-buffer-local 'f90-cache-position)
647 (defvar f90-imenu-flag nil
648 "Non-nil means this buffer already has an imenu.")
649 (make-variable-buffer-local 'f90-imenu-flag)
652 ;; Imenu support.
653 (defvar f90-imenu-generic-expression
654 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
655 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]"))
656 (list
657 '(nil "^[ \t0-9]*program[ \t]+\\(\\sw+\\)" 1)
658 '("Modules" "^[ \t0-9]*module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
659 '("Types" "^[ \t0-9]*type[ \t]+\\(\\sw+\\)" 1)
660 (list
661 "Procedures"
662 (concat
663 "^[ \t0-9]*"
664 "\\("
665 ;; At least three non-space characters before function/subroutine.
666 ;; Check that the last three non-space characters do not spell E N D.
667 "[^!\"\&\n]*\\("
668 not-e good-char good-char "\\|"
669 good-char not-n good-char "\\|"
670 good-char good-char not-d "\\)"
671 "\\|"
672 ;; Less than three non-space characters before function/subroutine.
673 good-char "?" good-char "?"
674 "\\)"
675 "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
676 4)))
677 "Generic imenu expression for F90 mode.")
679 (defun f90-add-imenu-menu ()
680 "Add an imenu menu to the menubar."
681 (interactive)
682 (if f90-imenu-flag
683 (message "%s" "F90-imenu already exists.")
684 (imenu-add-to-menubar "F90-imenu")
685 (redraw-frame (selected-frame))
686 (setq f90-imenu-flag t)))
688 (put 'f90-add-imenu-menu 'menu-enable '(not f90-imenu-flag))
691 ;; When compiling under GNU Emacs, load imenu during compilation.
692 ;; If you have 19.22 or earlier, comment this out, or get imenu.
693 (or f90-xemacs-flag (eval-when-compile (require 'imenu)))
696 ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
697 (defvar f90-mode-abbrev-table
698 (let (abbrevs-changed)
699 (define-abbrev-table 'f90-mode-abbrev-table
700 '(("`al" "allocate" nil 0 t)
701 ("`ab" "allocatable" nil 0 t)
702 ("`as" "assignment" nil 0 t)
703 ("`ba" "backspace" nil 0 t)
704 ("`bd" "block data" nil 0 t)
705 ("`c" "character" nil 0 t)
706 ("`cl" "close" nil 0 t)
707 ("`cm" "common" nil 0 t)
708 ("`cx" "complex" nil 0 t)
709 ("`cn" "contains" nil 0 t)
710 ("`cy" "cycle" nil 0 t)
711 ("`de" "deallocate" nil 0 t)
712 ("`df" "define" nil 0 t)
713 ("`di" "dimension" nil 0 t)
714 ("`dw" "do while" nil 0 t)
715 ("`el" "else" nil 0 t)
716 ("`eli" "else if" nil 0 t)
717 ("`elw" "elsewhere" nil 0 t)
718 ("`eq" "equivalence" nil 0 t)
719 ("`ex" "external" nil 0 t)
720 ("`ey" "entry" nil 0 t)
721 ("`fl" "forall" nil 0 t)
722 ("`fo" "format" nil 0 t)
723 ("`fu" "function" nil 0 t)
724 ("`fa" ".false." nil 0 t)
725 ("`im" "implicit none" nil 0 t)
726 ("`in " "include" nil 0 t)
727 ("`i" "integer" nil 0 t)
728 ("`it" "intent" nil 0 t)
729 ("`if" "interface" nil 0 t)
730 ("`lo" "logical" nil 0 t)
731 ("`mo" "module" nil 0 t)
732 ("`na" "namelist" nil 0 t)
733 ("`nu" "nullify" nil 0 t)
734 ("`op" "optional" nil 0 t)
735 ("`pa" "parameter" nil 0 t)
736 ("`po" "pointer" nil 0 t)
737 ("`pr" "print" nil 0 t)
738 ("`pi" "private" nil 0 t)
739 ("`pm" "program" nil 0 t)
740 ("`pu" "public" nil 0 t)
741 ("`r" "real" nil 0 t)
742 ("`rc" "recursive" nil 0 t)
743 ("`rt" "return" nil 0 t)
744 ("`rw" "rewind" nil 0 t)
745 ("`se" "select" nil 0 t)
746 ("`sq" "sequence" nil 0 t)
747 ("`su" "subroutine" nil 0 t)
748 ("`ta" "target" nil 0 t)
749 ("`tr" ".true." nil 0 t)
750 ("`t" "type" nil 0 t)
751 ("`wh" "where" nil 0 t)
752 ("`wr" "write" nil 0 t)))
753 f90-mode-abbrev-table)
754 "Abbrev table for F90 mode.")
756 (defcustom f90-mode-hook nil
757 "Hook run when entering F90 mode."
758 :type 'hook
759 :options '(f90-add-imenu-menu)
760 :group 'f90)
762 ;;;###autoload
763 (defun f90-mode ()
764 "Major mode for editing Fortran 90,95 code in free format.
766 \\[f90-indent-new-line] indents current line and creates a new\
767 indented line.
768 \\[f90-indent-line] indents the current line.
769 \\[f90-indent-subprogram] indents the current subprogram.
771 Type `? or `\\[help-command] to display a list of built-in\
772 abbrevs for F90 keywords.
774 Key definitions:
775 \\{f90-mode-map}
777 Variables controlling indentation style and extra features:
779 `f90-do-indent'
780 Extra indentation within do blocks (default 3).
781 `f90-if-indent'
782 Extra indentation within if/select case/where/forall blocks (default 3).
783 `f90-type-indent'
784 Extra indentation within type/interface/block-data blocks (default 3).
785 `f90-program-indent'
786 Extra indentation within program/module/subroutine/function blocks
787 (default 2).
788 `f90-continuation-indent'
789 Extra indentation applied to continuation lines (default 5).
790 `f90-comment-region'
791 String inserted by function \\[f90-comment-region] at start of each
792 line in region (default \"!!!$\").
793 `f90-indented-comment-re'
794 Regexp determining the type of comment to be intended like code
795 (default \"!\").
796 `f90-directive-comment-re'
797 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
798 (default \"!hpf\\\\$\").
799 `f90-break-delimiters'
800 Regexp holding list of delimiters at which lines may be broken
801 (default \"[-+*/><=,% \\t]\").
802 `f90-break-before-delimiters'
803 Non-nil causes `f90-do-auto-fill' to break lines before delimiters
804 (default t).
805 `f90-beginning-ampersand'
806 Automatic insertion of \& at beginning of continuation lines (default t).
807 `f90-smart-end'
808 From an END statement, check and fill the end using matching block start.
809 Allowed values are 'blink, 'no-blink, and nil, which determine
810 whether to blink the matching beginning (default 'blink).
811 `f90-auto-keyword-case'
812 Automatic change of case of keywords (default nil).
813 The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
814 `f90-leave-line-no'
815 Do not left-justify line numbers (default nil).
816 `f90-keywords-re'
817 List of keywords used for highlighting/upcase-keywords etc.
819 Turning on F90 mode calls the value of the variable `f90-mode-hook'
820 with no args, if that value is non-nil."
821 (interactive)
822 (kill-all-local-variables)
823 (setq major-mode 'f90-mode
824 mode-name "F90"
825 local-abbrev-table f90-mode-abbrev-table)
826 (set-syntax-table f90-mode-syntax-table)
827 (use-local-map f90-mode-map)
828 (set (make-local-variable 'indent-line-function) 'f90-indent-line)
829 (set (make-local-variable 'indent-region-function) 'f90-indent-region)
830 (set (make-local-variable 'require-final-newline) t)
831 (set (make-local-variable 'comment-start) "!")
832 (set (make-local-variable 'comment-start-skip) "!+ *")
833 (set (make-local-variable 'comment-indent-function) 'f90-comment-indent)
834 (set (make-local-variable 'abbrev-all-caps) t)
835 (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
836 (setq indent-tabs-mode nil) ; auto buffer local
837 ;; Setting up things for font-lock.
838 (when f90-xemacs-flag
839 (put 'f90-mode 'font-lock-keywords-case-fold-search t)
840 (when (and (featurep 'menubar)
841 current-menubar
842 (not (assoc "F90" current-menubar)))
843 (set-buffer-menubar (copy-sequence current-menubar))
844 (add-submenu nil f90-xemacs-menu)))
845 ;; XEmacs: Does not need a special case, since both emacsen work alike -sb.
846 (set (make-local-variable 'font-lock-defaults)
847 '((f90-font-lock-keywords f90-font-lock-keywords-1
848 f90-font-lock-keywords-2
849 f90-font-lock-keywords-3
850 f90-font-lock-keywords-4)
851 nil t))
852 ;; Tell imenu how to handle f90.
853 (set (make-local-variable 'imenu-case-fold-search) t)
854 (set (make-local-variable 'imenu-generic-expression)
855 f90-imenu-generic-expression)
856 (set (make-local-variable 'add-log-current-defun-function)
857 #'f90-current-defun)
858 (run-hooks 'f90-mode-hook))
861 ;; Inline-functions.
862 (defsubst f90-in-string ()
863 "Return non-nil if point is inside a string.
864 Checks from `point-min', or `f90-cache-position', if that is non-nil
865 and lies before point."
866 (let ((beg-pnt
867 (if (and f90-cache-position (> (point) f90-cache-position))
868 f90-cache-position
869 (point-min))))
870 (nth 3 (parse-partial-sexp beg-pnt (point)))))
872 (defsubst f90-in-comment ()
873 "Return non-nil if point is inside a comment.
874 Checks from `point-min', or `f90-cache-position', if that is non-nil
875 and lies before point."
876 (let ((beg-pnt
877 (if (and f90-cache-position (> (point) f90-cache-position))
878 f90-cache-position
879 (point-min))))
880 (nth 4 (parse-partial-sexp beg-pnt (point)))))
882 (defsubst f90-line-continued ()
883 "Return t if the current line is a continued one.
884 This includes comment lines embedded in continued lines, but
885 not the last line of a continued statement."
886 (save-excursion
887 (beginning-of-line)
888 (while (and (looking-at "[ \t]*\\(!\\|$\\)") (zerop (forward-line -1))))
889 (end-of-line)
890 (while (f90-in-comment)
891 (search-backward "!" (line-beginning-position))
892 (skip-chars-backward "!"))
893 (skip-chars-backward " \t")
894 (= (preceding-char) ?&)))
896 (defsubst f90-current-indentation ()
897 "Return indentation of current line.
898 Line-numbers are considered whitespace characters."
899 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
901 (defsubst f90-indent-to (col &optional no-line-number)
902 "Indent current line to column COL.
903 If optional argument NO-LINE-NUMBER is nil, jump over a possible
904 line-number before indenting."
905 (beginning-of-line)
906 (if (not no-line-number)
907 (skip-chars-forward " \t0-9"))
908 (delete-horizontal-space)
909 (if (zerop (current-column))
910 (indent-to col)
911 (indent-to col 1))) ; leave >= 1 space after line number
913 (defsubst f90-get-present-comment-type ()
914 "If point lies within a comment, return the string starting the comment.
915 For example, \"!\" or \"!!\"."
916 (save-excursion
917 (when (f90-in-comment)
918 (beginning-of-line)
919 (re-search-forward "!+" (line-end-position))
920 (while (f90-in-string)
921 (re-search-forward "!+" (line-end-position)))
922 (match-string 0))))
924 (defsubst f90-equal-symbols (a b)
925 "Compare strings A and B neglecting case and allowing for nil value."
926 (equal (if a (downcase a) nil)
927 (if b (downcase b) nil)))
929 ;; XEmacs 19.11 & 19.12 return a single char when matching an empty regexp.
930 ;; The next 2 functions are therefore longer than necessary.
931 (defsubst f90-looking-at-do ()
932 "Return (\"do\" NAME) if a do statement starts after point.
933 NAME is nil if the statement has no label."
934 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\>")
935 (list (match-string 3)
936 (if (looking-at "\\(\\sw+\\)[ \t]*:") (match-string 1)))))
938 (defsubst f90-looking-at-select-case ()
939 "Return (\"select\" NAME) if a select-case statement starts after point.
940 NAME is nil if the statement has no label."
941 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
942 \\(select\\)[ \t]*case[ \t]*(")
943 (list (match-string 3)
944 (if (looking-at "\\(\\sw+\\)[ \t]*:") (match-string 1)))))
946 (defsubst f90-looking-at-if-then ()
947 "Return (\"if\" NAME) if an if () then statement starts after point.
948 NAME is nil if the statement has no label."
949 (save-excursion
950 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\>")
951 (let ((struct (match-string 3))
952 (label (if (looking-at "\\(\\sw+\\)[ \t]*:") (match-string 1)))
953 (pos (scan-lists (point) 1 0)))
954 (and pos (goto-char pos))
955 (skip-chars-forward " \t")
956 (if (or (looking-at "then\\>")
957 (when (f90-line-continued)
958 (f90-next-statement)
959 (skip-chars-forward " \t0-9&")
960 (looking-at "then\\>")))
961 (list struct label))))))
963 (defsubst f90-looking-at-where-or-forall ()
964 "Return (KIND NAME) if a where or forall block starts after point.
965 NAME is nil if the statement has no label."
966 (save-excursion
967 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
968 \\(where\\|forall\\)\\>")
969 (let ((struct (match-string 3))
970 (label (if (looking-at "\\(\\sw+\\)[ \t]*:") (match-string 1)))
971 (pos (scan-lists (point) 1 0)))
972 (and pos (goto-char pos))
973 (skip-chars-forward " \t")
974 (if (looking-at "\\(!\\|$\\)") (list struct label))))))
976 (defsubst f90-looking-at-type-like ()
977 "Return (KIND NAME) if a type/interface/block-data block starts after point.
978 NAME is non-nil only for type."
979 (cond
980 ((looking-at f90-type-def-re)
981 (list (match-string 1) (match-string 2)))
982 ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
983 (list (match-string 1) nil))))
985 (defsubst f90-looking-at-program-block-start ()
986 "Return (KIND NAME) if a program block with name NAME starts after point."
987 (cond
988 ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
989 (list (match-string 1) (match-string 2)))
990 ((and (not (looking-at "module[ \t]*procedure\\>"))
991 (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
992 (list (match-string 1) (match-string 2)))
993 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
994 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)\
995 [ \t]+\\(\\sw+\\)"))
996 (list (match-string 1) (match-string 2)))))
998 (defsubst f90-looking-at-program-block-end ()
999 "Return (KIND NAME) if a block with name NAME ends after point."
1000 (if (looking-at (concat "end[ \t]*" f90-blocks-re
1001 "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
1002 (list (match-string 1) (match-string 3))))
1004 (defsubst f90-comment-indent ()
1005 "Return the indentation to be used for a comment starting at point.
1006 Used for `comment-indent-function' by F90 mode.
1007 \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
1008 `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
1009 All others return `comment-column', leaving at least one space after code."
1010 (cond ((looking-at "!!!") 0)
1011 ((and f90-directive-comment-re
1012 (looking-at f90-directive-comment-re)) 0)
1013 ((looking-at (regexp-quote f90-comment-region)) 0)
1014 ((and (looking-at f90-indented-comment-re)
1015 ;; Don't attempt to indent trailing comment as code.
1016 (save-excursion
1017 (skip-chars-backward " \t")
1018 (bolp)))
1019 (f90-calculate-indent))
1020 (t (skip-chars-backward " \t")
1021 (max (if (bolp) 0 (1+ (current-column))) comment-column))))
1023 (defsubst f90-present-statement-cont ()
1024 "Return continuation properties of present statement.
1025 Possible return values are:
1026 single - statement is not continued.
1027 begin - current line is the first in a continued statement.
1028 end - current line is the last in a continued statement
1029 middle - current line is neither first nor last in a continued statement.
1030 Comment lines embedded amongst continued lines return 'middle."
1031 (let (pcont cont)
1032 (save-excursion
1033 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
1034 (setq cont (f90-line-continued))
1035 (cond ((and (not pcont) (not cont)) 'single)
1036 ((and (not pcont) cont) 'begin)
1037 ((and pcont (not cont)) 'end)
1038 ((and pcont cont) 'middle)
1039 (t (error "The impossible occurred")))))
1041 (defsubst f90-indent-line-no ()
1042 "If `f90-leave-line-no' is nil, left-justify a line number.
1043 Leaves point at the first non-blank character after the line number.
1044 Call from beginning of line."
1045 (if (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]"))
1046 (delete-horizontal-space))
1047 (skip-chars-forward " \t0-9"))
1049 (defsubst f90-no-block-limit ()
1050 "Return nil if point is at the edge of a code block.
1051 Searches line forward for \"function\" or \"subroutine\",
1052 if all else fails."
1053 (let ((eol (line-end-position)))
1054 (save-excursion
1055 (not (or (looking-at "end")
1056 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
1057 \\|select[ \t]*case\\|case\\|where\\|forall\\)\\>")
1058 (looking-at "\\(program\\|module\\|interface\\|\
1059 block[ \t]*data\\)\\>")
1060 (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
1061 (looking-at f90-type-def-re)
1062 (re-search-forward "\\(function\\|subroutine\\)" eol t))))))
1064 (defsubst f90-update-line ()
1065 "Change case of current line as per `f90-auto-keyword-case'."
1066 (if f90-auto-keyword-case
1067 (f90-change-keywords f90-auto-keyword-case
1068 (line-beginning-position) (line-end-position))))
1070 (defun f90-electric-insert ()
1071 "Change keyword case and auto-fill line as operators are inserted."
1072 (interactive)
1073 (self-insert-command 1)
1074 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1075 (f90-update-line)))
1078 (defun f90-get-correct-indent ()
1079 "Get correct indent for a line starting with line number.
1080 Does not check type and subprogram indentation."
1081 (let ((epnt (line-end-position)) icol cont)
1082 (save-excursion
1083 (while (and (f90-previous-statement)
1084 (or (progn
1085 (setq cont (f90-present-statement-cont))
1086 (or (eq cont 'end) (eq cont 'middle)))
1087 (looking-at "[ \t]*[0-9]"))))
1088 (setq icol (current-indentation))
1089 (beginning-of-line)
1090 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1091 (line-end-position) t)
1092 (beginning-of-line)
1093 (skip-chars-forward " \t")
1094 (cond ((f90-looking-at-do)
1095 (setq icol (+ icol f90-do-indent)))
1096 ((or (f90-looking-at-if-then)
1097 (f90-looking-at-where-or-forall)
1098 (f90-looking-at-select-case))
1099 (setq icol (+ icol f90-if-indent))))
1100 (end-of-line))
1101 (while (re-search-forward
1102 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1103 (beginning-of-line)
1104 (skip-chars-forward " \t0-9")
1105 (cond ((f90-looking-at-do)
1106 (setq icol (+ icol f90-do-indent)))
1107 ((or (f90-looking-at-if-then)
1108 (f90-looking-at-where-or-forall)
1109 (f90-looking-at-select-case))
1110 (setq icol (+ icol f90-if-indent)))
1111 ((looking-at f90-end-if-re)
1112 (setq icol (- icol f90-if-indent)))
1113 ((looking-at "end[ \t]*do\\>")
1114 (setq icol (- icol f90-do-indent))))
1115 (end-of-line))
1116 icol)))
1118 (defun f90-calculate-indent ()
1119 "Calculate the indent column based on previous statements."
1120 (interactive)
1121 (let (icol cont (case-fold-search t) (pnt (point)))
1122 (save-excursion
1123 (if (not (f90-previous-statement))
1124 (setq icol 0)
1125 (setq cont (f90-present-statement-cont))
1126 (if (eq cont 'end)
1127 (while (not (eq 'begin (f90-present-statement-cont)))
1128 (f90-previous-statement)))
1129 (cond ((eq cont 'begin)
1130 (setq icol (+ (f90-current-indentation)
1131 f90-continuation-indent)))
1132 ((eq cont 'middle) (setq icol (current-indentation)))
1133 (t (setq icol (f90-current-indentation))
1134 (skip-chars-forward " \t")
1135 (if (looking-at "[0-9]")
1136 (setq icol (f90-get-correct-indent))
1137 (cond ((or (f90-looking-at-if-then)
1138 (f90-looking-at-where-or-forall)
1139 (f90-looking-at-select-case)
1140 (looking-at f90-else-like-re))
1141 (setq icol (+ icol f90-if-indent)))
1142 ((f90-looking-at-do)
1143 (setq icol (+ icol f90-do-indent)))
1144 ((f90-looking-at-type-like)
1145 (setq icol (+ icol f90-type-indent)))
1146 ((or (f90-looking-at-program-block-start)
1147 (looking-at "contains[ \t]*\\($\\|!\\)"))
1148 (setq icol (+ icol f90-program-indent)))))
1149 (goto-char pnt)
1150 (beginning-of-line)
1151 (cond ((looking-at "[ \t]*$"))
1152 ((looking-at "[ \t]*#") ; check for cpp directive
1153 (setq icol 0))
1155 (skip-chars-forward " \t0-9")
1156 (cond ((or (looking-at f90-else-like-re)
1157 (looking-at f90-end-if-re))
1158 (setq icol (- icol f90-if-indent)))
1159 ((looking-at "end[ \t]*do\\>")
1160 (setq icol (- icol f90-do-indent)))
1161 ((looking-at f90-end-type-re)
1162 (setq icol (- icol f90-type-indent)))
1163 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1164 (f90-looking-at-program-block-end))
1165 (setq icol (- icol f90-program-indent))))))
1166 ))))
1167 icol))
1169 (defun f90-previous-statement ()
1170 "Move point to beginning of the previous F90 statement.
1171 Return nil if no previous statement is found.
1172 A statement is a line which is neither blank nor a comment."
1173 (interactive)
1174 (let (not-first-statement)
1175 (beginning-of-line)
1176 (while (and (setq not-first-statement (zerop (forward-line -1)))
1177 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1178 not-first-statement))
1180 (defun f90-next-statement ()
1181 "Move point to beginning of the next F90 statement.
1182 Return nil if no later statement is found."
1183 (interactive)
1184 (let (not-last-statement)
1185 (beginning-of-line)
1186 (while (and (setq not-last-statement
1187 (and (zerop (forward-line 1))
1188 (not (eobp))))
1189 (looking-at "[ \t0-9]*\\(!\\|$\\)")))
1190 not-last-statement))
1192 (defun f90-beginning-of-subprogram ()
1193 "Move point to the beginning of the current subprogram.
1194 Return (TYPE NAME), or nil if not found."
1195 (interactive)
1196 (let ((count 1) (case-fold-search t) matching-beg)
1197 (beginning-of-line)
1198 (while (and (> count 0)
1199 (re-search-backward f90-program-block-re nil 'move))
1200 (beginning-of-line)
1201 (skip-chars-forward " \t0-9")
1202 (cond ((setq matching-beg (f90-looking-at-program-block-start))
1203 (setq count (1- count)))
1204 ((f90-looking-at-program-block-end)
1205 (setq count (1+ count)))))
1206 (beginning-of-line)
1207 (if (zerop count)
1208 matching-beg
1209 (message "No beginning found.")
1210 nil)))
1212 (defun f90-end-of-subprogram ()
1213 "Move point to the end of the current subprogram.
1214 Return (TYPE NAME), or nil if not found."
1215 (interactive)
1216 (let ((count 1) (case-fold-search t) matching-end)
1217 (end-of-line)
1218 (while (and (> count 0)
1219 (re-search-forward f90-program-block-re nil 'move))
1220 (beginning-of-line)
1221 (skip-chars-forward " \t0-9")
1222 (cond ((f90-looking-at-program-block-start)
1223 (setq count (1+ count)))
1224 ((setq matching-end (f90-looking-at-program-block-end))
1225 (setq count (1- count))))
1226 (end-of-line))
1227 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1228 ;; has a net non-zero effect, which seems odd.
1229 ;;; (forward-line 1)
1230 (if (zerop count)
1231 matching-end
1232 (message "No end found.")
1233 nil)))
1236 (defun f90-end-of-block (&optional num)
1237 "Move point forward to the end of the current code block.
1238 With optional argument NUM, go forward that many balanced blocks.
1239 If NUM is negative, go backward to the start of a block.
1240 Checks for consistency of block types and labels (if present),
1241 and completes outermost block if necessary."
1242 (interactive "p")
1243 (if (and num (< num 0)) (f90-beginning-of-block (- num)))
1244 (let ((f90-smart-end nil) ; for the final `f90-match-end'
1245 (case-fold-search t)
1246 (count (or num 1))
1247 start-list start-this start-type start-label end-type end-label)
1248 (if (interactive-p) (push-mark (point) t))
1249 (end-of-line) ; probably want this
1250 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1251 (beginning-of-line)
1252 (skip-chars-forward " \t0-9")
1253 (cond ((or (f90-in-string) (f90-in-comment)))
1254 ((setq start-this
1256 (f90-looking-at-do)
1257 (f90-looking-at-select-case)
1258 (f90-looking-at-type-like)
1259 (f90-looking-at-program-block-start)
1260 (f90-looking-at-if-then)
1261 (f90-looking-at-where-or-forall)))
1262 (setq start-list (cons start-this start-list) ; not add-to-list!
1263 count (1+ count)))
1264 ((looking-at (concat "end[ \t]*" f90-blocks-re
1265 "[ \t]*\\(\\sw+\\)?"))
1266 (setq end-type (match-string 1)
1267 end-label (match-string 2)
1268 count (1- count))
1269 ;; Check any internal blocks.
1270 (when start-list
1271 (setq start-this (car start-list)
1272 start-list (cdr start-list)
1273 start-type (car start-this)
1274 start-label (cadr start-this))
1275 (if (not (f90-equal-symbols start-type end-type))
1276 (error "End type `%s' does not match start type `%s'"
1277 end-type start-type))
1278 (if (not (f90-equal-symbols start-label end-label))
1279 (error "End label `%s' does not match start label `%s'"
1280 end-label start-label)))))
1281 (end-of-line))
1282 (if (> count 0) (error "Missing block end"))
1283 ;; Check outermost block.
1284 (if (interactive-p)
1285 (save-excursion
1286 (beginning-of-line)
1287 (skip-chars-forward " \t0-9")
1288 (f90-match-end)))))
1290 (defun f90-beginning-of-block (&optional num)
1291 "Move point backwards to the start of the current code block.
1292 With optional argument NUM, go backward that many balanced blocks.
1293 If NUM is negative, go forward to the end of a block.
1294 Checks for consistency of block types and labels (if present).
1295 Does not check the outermost block, because it may be incomplete."
1296 (interactive "p")
1297 (if (and num (< num 0)) (f90-end-of-block (- num)))
1298 (let ((case-fold-search t)
1299 (count (or num 1))
1300 end-list end-this end-type end-label start-this start-type start-label)
1301 (if (interactive-p) (push-mark (point) t))
1302 (beginning-of-line) ; probably want this
1303 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1304 (beginning-of-line)
1305 (skip-chars-forward " \t0-9")
1306 (cond ((or (f90-in-string) (f90-in-comment)))
1307 ((looking-at (concat "end[ \t]*" f90-blocks-re
1308 "[ \t]*\\(\\sw+\\)?"))
1309 (setq end-list (cons (list (match-string 1) (match-string 2))
1310 end-list)
1311 count (1+ count)))
1312 ((setq start-this
1314 (f90-looking-at-do)
1315 (f90-looking-at-select-case)
1316 (f90-looking-at-type-like)
1317 (f90-looking-at-program-block-start)
1318 (f90-looking-at-if-then)
1319 (f90-looking-at-where-or-forall)))
1320 (setq start-type (car start-this)
1321 start-label (cadr start-this)
1322 count (1- count))
1323 ;; Check any internal blocks.
1324 (when end-list
1325 (setq end-this (car end-list)
1326 end-list (cdr end-list)
1327 end-type (car end-this)
1328 end-label (cadr end-this))
1329 (if (not (f90-equal-symbols start-type end-type))
1330 (error "Start type `%s' does not match end type `%s'"
1331 start-type end-type))
1332 (if (not (f90-equal-symbols start-label end-label))
1333 (error "Start label `%s' does not match end label `%s'"
1334 start-label end-label))))))
1335 (if (> count 0) (error "Missing block start"))))
1337 (defun f90-next-block (&optional num)
1338 "Move point forward to the next end or start of a code block.
1339 With optional argument NUM, go forward that many blocks.
1340 If NUM is negative, go backwards.
1341 A block is a subroutine, if-endif, etc."
1342 (interactive "p")
1343 (let ((case-fold-search t)
1344 (count (if num (abs num) 1)))
1345 (while (and (> count 0)
1346 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1347 (re-search-backward f90-blocks-re nil 'move)))
1348 (beginning-of-line)
1349 (skip-chars-forward " \t0-9")
1350 (cond ((or (f90-in-string) (f90-in-comment)))
1351 ((or
1352 (looking-at "end[ \t]*")
1353 (f90-looking-at-do)
1354 (f90-looking-at-select-case)
1355 (f90-looking-at-type-like)
1356 (f90-looking-at-program-block-start)
1357 (f90-looking-at-if-then)
1358 (f90-looking-at-where-or-forall))
1359 (setq count (1- count))))
1360 (if (> num 0) (end-of-line)
1361 (beginning-of-line)))))
1364 (defun f90-previous-block (&optional num)
1365 "Move point backward to the previous end or start of a code block.
1366 With optional argument NUM, go backward that many blocks.
1367 If NUM is negative, go forwards.
1368 A block is a subroutine, if-endif, etc."
1369 (interactive "p")
1370 (f90-next-block (- (or num 1))))
1373 (defvar f90-mark-subprogram-overlay nil
1374 "Used internally by `f90-mark-subprogram' to highlight the subprogram.")
1375 (make-variable-buffer-local 'f90-mark-subprogram-overlay)
1377 (defun f90-mark-subprogram ()
1378 "Put mark at end of F90 subprogram, point at beginning, push marks.
1379 If called interactively, highlight the subprogram with the face `highlight'.
1380 Call again to remove the highlighting."
1381 (interactive)
1382 (let ((pos (point)) program)
1383 (f90-end-of-subprogram)
1384 (push-mark (point) t)
1385 (goto-char pos)
1386 (setq program (f90-beginning-of-subprogram))
1387 ;; The keywords in the preceding lists assume case-insensitivity.
1388 (if f90-xemacs-flag
1389 (zmacs-activate-region)
1390 (setq mark-active t
1391 deactivate-mark nil)
1392 (if (interactive-p)
1393 (if (overlayp f90-mark-subprogram-overlay)
1394 (if (overlay-buffer f90-mark-subprogram-overlay)
1395 (delete-overlay f90-mark-subprogram-overlay)
1396 (move-overlay f90-mark-subprogram-overlay (point) (mark)))
1397 (setq f90-mark-subprogram-overlay (make-overlay (point) (mark)))
1398 (overlay-put f90-mark-subprogram-overlay 'face 'highlight))))
1399 program))
1401 (defun f90-comment-region (beg-region end-region)
1402 "Comment/uncomment every line in the region.
1403 Insert the variable `f90-comment-region' at the start of every line
1404 in the region, or, if already present, remove it."
1405 (interactive "*r")
1406 (let ((end (make-marker)))
1407 (set-marker end end-region)
1408 (goto-char beg-region)
1409 (beginning-of-line)
1410 (if (looking-at (regexp-quote f90-comment-region))
1411 (delete-region (point) (match-end 0))
1412 (insert f90-comment-region))
1413 (while (and (zerop (forward-line 1))
1414 (< (point) (marker-position end)))
1415 (if (looking-at (regexp-quote f90-comment-region))
1416 (delete-region (point) (match-end 0))
1417 (insert f90-comment-region)))
1418 (set-marker end nil)))
1420 (defun f90-indent-line (&optional no-update)
1421 "Indent current line as F90 code.
1422 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1423 after indenting."
1424 (interactive)
1425 (let (indent no-line-number (pos (make-marker)) (case-fold-search t))
1426 (set-marker pos (point))
1427 (beginning-of-line) ; digits after & \n are not line-nos
1428 (if (save-excursion (and (f90-previous-statement) (f90-line-continued)))
1429 (progn (setq no-line-number t) (skip-chars-forward " \t"))
1430 (f90-indent-line-no))
1431 (if (looking-at "!")
1432 (setq indent (f90-comment-indent))
1433 (if (and (looking-at "end") f90-smart-end)
1434 (f90-match-end))
1435 (setq indent (f90-calculate-indent)))
1436 (if (not (zerop (- indent (current-column))))
1437 (f90-indent-to indent no-line-number))
1438 ;; If initial point was within line's indentation,
1439 ;; position after the indentation. Else stay at same point in text.
1440 (if (< (point) (marker-position pos))
1441 (goto-char (marker-position pos)))
1442 (if auto-fill-function
1443 (f90-do-auto-fill) ; also updates line
1444 (if (not no-update) (f90-update-line)))
1445 (set-marker pos nil)))
1447 (defun f90-indent-new-line ()
1448 "Reindent current line, insert a newline and indent the newline.
1449 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1450 If run in the middle of a line, the line is not broken."
1451 (interactive)
1452 (let (string cont (case-fold-search t))
1453 (if abbrev-mode (expand-abbrev))
1454 (beginning-of-line) ; reindent where likely to be needed
1455 (f90-indent-line-no)
1456 (f90-indent-line 'no-update)
1457 (end-of-line)
1458 (delete-horizontal-space) ; destroy trailing whitespace
1459 (setq string (f90-in-string)
1460 cont (f90-line-continued))
1461 (if (and string (not cont)) (insert "&"))
1462 (f90-update-line)
1463 (newline)
1464 (if (or string (and cont f90-beginning-ampersand)) (insert "&"))
1465 (f90-indent-line 'no-update)))
1468 (defun f90-indent-region (beg-region end-region)
1469 "Indent every line in region by forward parsing."
1470 (interactive "*r")
1471 (let ((end-region-mark (make-marker))
1472 (save-point (point-marker))
1473 block-list ind-lev ind-curr ind-b cont
1474 struct beg-struct end-struct)
1475 (set-marker end-region-mark end-region)
1476 (goto-char beg-region)
1477 ;; First find a line which is not a continuation line or comment.
1478 (beginning-of-line)
1479 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1480 (progn (f90-indent-line 'no-update)
1481 (zerop (forward-line 1)))
1482 (< (point) end-region-mark)))
1483 (setq cont (f90-present-statement-cont))
1484 (while (and (or (eq cont 'middle) (eq cont 'end))
1485 (f90-previous-statement))
1486 (setq cont (f90-present-statement-cont)))
1487 ;; Process present line for beginning of block.
1488 (setq f90-cache-position (point))
1489 (f90-indent-line 'no-update)
1490 (setq ind-lev (f90-current-indentation)
1491 ind-curr ind-lev)
1492 (beginning-of-line)
1493 (skip-chars-forward " \t0-9")
1494 (setq struct nil
1495 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1496 ((or (setq struct (f90-looking-at-if-then))
1497 (setq struct (f90-looking-at-select-case))
1498 (setq struct (f90-looking-at-where-or-forall))
1499 (looking-at f90-else-like-re))
1500 f90-if-indent)
1501 ((setq struct (f90-looking-at-type-like))
1502 f90-type-indent)
1503 ((or(setq struct (f90-looking-at-program-block-start))
1504 (looking-at "contains[ \t]*\\($\\|!\\)"))
1505 f90-program-indent)))
1506 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1507 (if struct (setq block-list (cons struct block-list)))
1508 (while (and (f90-line-continued) (zerop (forward-line 1))
1509 (< (point) end-region-mark))
1510 (if (looking-at "[ \t]*!")
1511 (f90-indent-to (f90-comment-indent))
1512 (if (not (zerop (- (current-indentation)
1513 (+ ind-curr f90-continuation-indent))))
1514 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1515 ;; Process all following lines.
1516 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1517 (beginning-of-line)
1518 (f90-indent-line-no)
1519 (setq f90-cache-position (point))
1520 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1521 ((looking-at "[ \t]*#") (setq ind-curr 0))
1522 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1523 ((f90-no-block-limit) (setq ind-curr ind-lev))
1524 ((looking-at f90-else-like-re) (setq ind-curr
1525 (- ind-lev f90-if-indent)))
1526 ((looking-at "contains[ \t]*\\($\\|!\\)")
1527 (setq ind-curr (- ind-lev f90-program-indent)))
1528 ((setq ind-b
1529 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1530 ((or (setq struct (f90-looking-at-if-then))
1531 (setq struct (f90-looking-at-select-case))
1532 (setq struct (f90-looking-at-where-or-forall)))
1533 f90-if-indent)
1534 ((setq struct (f90-looking-at-type-like))
1535 f90-type-indent)
1536 ((setq struct (f90-looking-at-program-block-start))
1537 f90-program-indent)))
1538 (setq ind-curr ind-lev)
1539 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1540 (setq block-list (cons struct block-list)))
1541 ((setq end-struct (f90-looking-at-program-block-end))
1542 (setq beg-struct (car block-list)
1543 block-list (cdr block-list))
1544 (if f90-smart-end
1545 (save-excursion
1546 (f90-block-match (car beg-struct) (car (cdr beg-struct))
1547 (car end-struct) (car (cdr end-struct)))))
1548 (setq ind-b
1549 (cond ((looking-at f90-end-if-re) f90-if-indent)
1550 ((looking-at "end[ \t]*do\\>") f90-do-indent)
1551 ((looking-at f90-end-type-re) f90-type-indent)
1552 ((f90-looking-at-program-block-end)
1553 f90-program-indent)))
1554 (if ind-b (setq ind-lev (- ind-lev ind-b)))
1555 (setq ind-curr ind-lev))
1556 (t (setq ind-curr ind-lev)))
1557 ;; Do the indentation if necessary.
1558 (if (not (zerop (- ind-curr (current-column))))
1559 (f90-indent-to ind-curr))
1560 (while (and (f90-line-continued) (zerop (forward-line 1))
1561 (< (point) end-region-mark))
1562 (if (looking-at "[ \t]*!")
1563 (f90-indent-to (f90-comment-indent))
1564 (if (not (zerop (- (current-indentation)
1565 (+ ind-curr f90-continuation-indent))))
1566 (f90-indent-to
1567 (+ ind-curr f90-continuation-indent) 'no-line-no)))))
1568 ;; Restore point, etc.
1569 (setq f90-cache-position nil)
1570 (goto-char save-point)
1571 (set-marker end-region-mark nil)
1572 (set-marker save-point nil)
1573 (if f90-xemacs-flag
1574 (zmacs-deactivate-region)
1575 (deactivate-mark))))
1577 (defun f90-indent-subprogram ()
1578 "Properly indent the subprogram containing point."
1579 (interactive)
1580 (save-excursion
1581 (let ((program (f90-mark-subprogram)))
1582 (if program
1583 (progn
1584 (message "Indenting %s %s..."
1585 (car program) (car (cdr program)))
1586 (indent-region (point) (mark) nil)
1587 (message "Indenting %s %s...done"
1588 (car program) (car (cdr program))))
1589 (message "Indenting the whole file...")
1590 (indent-region (point) (mark) nil)
1591 (message "Indenting the whole file...done")))))
1593 (defun f90-break-line (&optional no-update)
1594 "Break line at point, insert continuation marker(s) and indent.
1595 Unless in a string or comment, or if the optional argument NO-UPDATE
1596 is non-nil, call `f90-update-line' after inserting the continuation marker."
1597 (interactive)
1598 (cond ((f90-in-string)
1599 (insert "&\n&"))
1600 ((f90-in-comment)
1601 (insert "\n" (f90-get-present-comment-type)))
1602 (t (insert "&")
1603 (or no-update (f90-update-line))
1604 (newline 1)
1605 (if f90-beginning-ampersand (insert "&"))))
1606 (indent-according-to-mode))
1608 (defun f90-find-breakpoint ()
1609 "From `fill-column', search backward for break-delimiter."
1610 (let ((bol (line-beginning-position)))
1611 (re-search-backward f90-break-delimiters bol)
1612 (if (not f90-break-before-delimiters)
1613 (if (looking-at f90-no-break-re)
1614 (forward-char 2)
1615 (forward-char))
1616 (backward-char)
1617 (if (not (looking-at f90-no-break-re))
1618 (forward-char)))))
1620 (defun f90-do-auto-fill ()
1621 "Break line if non-white characters beyond `fill-column'.
1622 Update keyword case first."
1623 (interactive)
1624 ;; Break line before or after last delimiter (non-word char) if
1625 ;; position is beyond fill-column.
1626 ;; Will not break **, //, or => (as specified by f90-no-break-re).
1627 (f90-update-line)
1628 ;; Need this for `f90-electric-insert' and other f90- callers.
1629 (unless (and (boundp 'comment-auto-fill-only-comments)
1630 comment-auto-fill-only-comments
1631 (not (f90-in-comment)))
1632 (while (> (current-column) fill-column)
1633 (let ((pos-mark (point-marker)))
1634 (move-to-column fill-column)
1635 (or (f90-in-string) (f90-find-breakpoint))
1636 (f90-break-line)
1637 (goto-char pos-mark)
1638 (set-marker pos-mark nil)))))
1641 (defun f90-join-lines ()
1642 "Join present line with next line, if this line ends with \&."
1643 (interactive)
1644 (let (pos (oldpos (point)))
1645 (end-of-line)
1646 (skip-chars-backward " \t")
1647 (when (= (preceding-char) ?&)
1648 (delete-char -1)
1649 (setq pos (point))
1650 (forward-line 1)
1651 (skip-chars-forward " \t")
1652 (if (looking-at "\&") (delete-char 1))
1653 (delete-region pos (point))
1654 (unless (f90-in-string)
1655 (delete-horizontal-space)
1656 (insert " "))
1657 (if (and auto-fill-function
1658 (> (line-end-position) fill-column))
1659 (f90-do-auto-fill))
1660 (goto-char oldpos)
1661 t))) ; return t if joined something
1663 (defun f90-fill-region (beg-region end-region)
1664 "Fill every line in region by forward parsing. Join lines if possible."
1665 (interactive "*r")
1666 (let ((end-region-mark (make-marker))
1667 (go-on t)
1668 f90-smart-end f90-auto-keyword-case auto-fill-function)
1669 (set-marker end-region-mark end-region)
1670 (goto-char beg-region)
1671 (while go-on
1672 ;; Join as much as possible.
1673 (while (f90-join-lines))
1674 ;; Chop the line if necessary.
1675 (while (> (save-excursion (end-of-line) (current-column))
1676 fill-column)
1677 (move-to-column fill-column)
1678 (f90-find-breakpoint)
1679 (f90-break-line 'no-update))
1680 (setq go-on (and (< (point) (marker-position end-region-mark))
1681 (zerop (forward-line 1)))
1682 f90-cache-position (point)))
1683 (setq f90-cache-position nil)
1684 (if f90-xemacs-flag
1685 (zmacs-deactivate-region)
1686 (deactivate-mark))))
1688 (defun f90-block-match (beg-block beg-name end-block end-name)
1689 "Match end-struct with beg-struct and complete end-block if possible.
1690 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
1691 BEG-NAME is the block start name (may be nil).
1692 END-BLOCK is the type of block as indicated at the end (may be nil).
1693 END-NAME is the block end name (may be nil).
1694 Leave point at the end of line."
1695 (search-forward "end" (line-end-position))
1696 (catch 'no-match
1697 (if (not (f90-equal-symbols beg-block end-block))
1698 (if end-block
1699 (progn
1700 (message "END %s does not match %s." end-block beg-block)
1701 (end-of-line)
1702 (throw 'no-match nil))
1703 (message "Inserting %s." beg-block)
1704 (insert (concat " " beg-block)))
1705 (search-forward end-block))
1706 (if (not (f90-equal-symbols beg-name end-name))
1707 (cond ((and beg-name (not end-name))
1708 (message "Inserting %s." beg-name)
1709 (insert (concat " " beg-name)))
1710 ((and beg-name end-name)
1711 (message "Replacing %s with %s." end-name beg-name)
1712 (search-forward end-name)
1713 (replace-match beg-name))
1714 ((and (not beg-name) end-name)
1715 (message "Deleting %s." end-name)
1716 (search-forward end-name)
1717 (replace-match "")))
1718 (if end-name (search-forward end-name)))
1719 (if (not (looking-at "[ \t]*!")) (delete-horizontal-space))))
1721 (defun f90-match-end ()
1722 "From an end block statement, find the corresponding block and name."
1723 (interactive)
1724 (let ((count 1) (top-of-window (window-start))
1725 (end-point (point)) (case-fold-search t)
1726 matching-beg beg-name end-name beg-block end-block end-struct)
1727 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
1728 (setq end-struct (f90-looking-at-program-block-end)))
1729 (setq end-block (car end-struct)
1730 end-name (car (cdr end-struct)))
1731 (save-excursion
1732 (beginning-of-line)
1733 (while (and (> count 0) (re-search-backward f90-blocks-re nil t))
1734 (beginning-of-line)
1735 (skip-chars-forward " \t0-9")
1736 (cond ((or (f90-in-string) (f90-in-comment)))
1737 ((setq matching-beg
1739 (f90-looking-at-do)
1740 (f90-looking-at-if-then)
1741 (f90-looking-at-where-or-forall)
1742 (f90-looking-at-select-case)
1743 (f90-looking-at-type-like)
1744 (f90-looking-at-program-block-start)))
1745 (setq count (1- count)))
1746 ((looking-at (concat "end[ \t]*" f90-blocks-re))
1747 (setq count (1+ count)))))
1748 (if (> count 0)
1749 (message "No matching beginning.")
1750 (f90-update-line)
1751 (if (eq f90-smart-end 'blink)
1752 (if (< (point) top-of-window)
1753 (message "Matches %s: %s"
1754 (what-line)
1755 (buffer-substring
1756 (line-beginning-position)
1757 (line-end-position)))
1758 (sit-for 1)))
1759 (setq beg-block (car matching-beg)
1760 beg-name (car (cdr matching-beg)))
1761 (goto-char end-point)
1762 (beginning-of-line)
1763 (f90-block-match beg-block beg-name end-block end-name))))))
1765 (defun f90-insert-end ()
1766 "Insert a complete end statement matching beginning of present block."
1767 (interactive)
1768 (let ((f90-smart-end (or f90-smart-end 'blink)))
1769 (insert "end")
1770 (f90-indent-new-line)))
1772 ;; Abbrevs and keywords.
1774 (defun f90-abbrev-start ()
1775 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
1776 Any other key combination is executed normally."
1777 (interactive)
1778 (let (e c)
1779 (insert last-command-char)
1780 (if (not f90-xemacs-flag)
1781 (setq c (read-event))
1782 (setq e (next-command-event)
1783 c (event-to-character e)))
1784 ;; Insert char if not equal to `?'.
1785 (if (or (eq c ??) (eq c help-char))
1786 (f90-abbrev-help)
1787 (if f90-xemacs-flag
1788 (setq unread-command-event e)
1789 (setq unread-command-events (list c))))))
1791 (defun f90-abbrev-help ()
1792 "List the currently defined abbrevs in F90 mode."
1793 (interactive)
1794 (message "Listing abbrev table...")
1795 (display-buffer (f90-prepare-abbrev-list-buffer))
1796 (message "Listing abbrev table...done"))
1798 (defun f90-prepare-abbrev-list-buffer ()
1799 "Create a buffer listing the F90 mode abbreviations."
1800 (save-excursion
1801 (set-buffer (get-buffer-create "*Abbrevs*"))
1802 (erase-buffer)
1803 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
1804 (goto-char (point-min))
1805 (set-buffer-modified-p nil)
1806 (edit-abbrevs-mode))
1807 (get-buffer-create "*Abbrevs*"))
1809 (defun f90-upcase-keywords ()
1810 "Upcase all F90 keywords in the buffer."
1811 (interactive)
1812 (f90-change-keywords 'upcase-word))
1814 (defun f90-capitalize-keywords ()
1815 "Capitalize all F90 keywords in the buffer."
1816 (interactive)
1817 (f90-change-keywords 'capitalize-word))
1819 (defun f90-downcase-keywords ()
1820 "Downcase all F90 keywords in the buffer."
1821 (interactive)
1822 (f90-change-keywords 'downcase-word))
1824 (defun f90-upcase-region-keywords (beg end)
1825 "Upcase all F90 keywords in the region."
1826 (interactive "*r")
1827 (f90-change-keywords 'upcase-word beg end))
1829 (defun f90-capitalize-region-keywords (beg end)
1830 "Capitalize all F90 keywords in the region."
1831 (interactive "*r")
1832 (f90-change-keywords 'capitalize-word beg end))
1834 (defun f90-downcase-region-keywords (beg end)
1835 "Downcase all F90 keywords in the region."
1836 (interactive "*r")
1837 (f90-change-keywords 'downcase-word beg end))
1839 ;; Change the keywords according to argument.
1840 (defun f90-change-keywords (change-word &optional beg end)
1841 "Change the case of F90 keywords in the region (if specified) or buffer.
1842 CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
1843 (save-excursion
1844 (setq beg (or beg (point-min))
1845 end (or end (point-max)))
1846 (let ((keyword-re
1847 (concat "\\("
1848 f90-keywords-re "\\|" f90-procedures-re "\\|"
1849 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
1850 (ref-point (point-min))
1851 (modified (buffer-modified-p))
1852 state saveword back-point)
1853 (goto-char beg)
1854 (unwind-protect
1855 (while (re-search-forward keyword-re end t)
1856 (unless (progn
1857 (setq state (parse-partial-sexp ref-point (point)))
1858 (or (nth 3 state) (nth 4 state)
1859 (save-excursion ; check for cpp directive
1860 (beginning-of-line)
1861 (skip-chars-forward " \t0-9")
1862 (looking-at "#"))))
1863 (setq ref-point (point)
1864 back-point (save-excursion (backward-word 1) (point))
1865 saveword (buffer-substring back-point ref-point))
1866 (funcall change-word -1)
1867 (or (string= saveword (buffer-substring back-point ref-point))
1868 (setq modified t))))
1869 (or modified (set-buffer-modified-p nil))))))
1872 (defun f90-current-defun ()
1873 "Function to use for `add-log-current-defun-function' in F90 mode."
1874 (save-excursion
1875 (nth 1 (f90-beginning-of-subprogram))))
1877 (provide 'f90)
1879 ;;; f90.el ends here