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