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