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