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