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