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