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