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