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