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