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