1 ;;; f90.el --- Fortran-90 mode (free format)
3 ;; Copyright (C) 1995-1997, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Torbjörn Einarsson <Torbjorn.Einarsson@era.ericsson.se>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
7 ;; Keywords: fortran, f90, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
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
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.
72 ;; (add-to-list 'auto-mode-alist '("\\.f\\'" . f90-mode))
74 ;; Once you have entered f90-mode, you may get more info by using
75 ;; the command describe-mode (C-h m). For online help use
76 ;; C-h f <Name of function you want described>, or
77 ;; C-h v <Name of variable you want described>.
79 ;; To customize f90-mode for your taste, use, for example:
80 ;; (you don't have to specify values for all the parameters below)
82 ;; (add-hook 'f90-mode-hook
83 ;; ;; These are the default values.
84 ;; (lambda () (setq f90-do-indent 3
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
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))
108 ;; in your .emacs file. You can also customize the lists
109 ;; f90-font-lock-keywords, etc.
111 ;; The auto-fill and abbreviation minor modes are accessible from the F90 menu,
112 ;; or by using M-x auto-fill-mode and M-x abbrev-mode, respectively.
115 ;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
116 ;; non-nil, the line numbers are never touched.
117 ;; 2) Multi-; statements like "do i=1,20 ; j=j+i ; end do" are not handled
118 ;; correctly, but I imagine them to be rare.
119 ;; 3) Regexps for hilit19 are no longer supported.
120 ;; 4) For FIXED FORMAT code, use fortran mode.
121 ;; 5) This mode does not work under emacs-18.x.
122 ;; 6) Preprocessor directives, i.e., lines starting with # are left-justified
123 ;; and are untouched by all case-changing commands. There is, at present, no
124 ;; mechanism for treating multi-line directives (continued by \ ).
125 ;; 7) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
126 ;; You are urged to use f90-do loops (with labels if you wish).
127 ;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
129 ;; List of user commands
130 ;; f90-previous-statement f90-next-statement
131 ;; f90-beginning-of-subprogram f90-end-of-subprogram f90-mark-subprogram
132 ;; f90-comment-region
133 ;; f90-indent-line f90-indent-new-line
134 ;; f90-indent-region (can be called by calling indent-region)
135 ;; f90-indent-subprogram
136 ;; f90-break-line f90-join-lines
139 ;; f90-upcase-keywords f90-upcase-region-keywords
140 ;; f90-downcase-keywords f90-downcase-region-keywords
141 ;; f90-capitalize-keywords f90-capitalize-region-keywords
142 ;; f90-add-imenu-menu
143 ;; f90-font-lock-1, f90-font-lock-2, f90-font-lock-3, f90-font-lock-4
145 ;; Original author's thanks
146 ;; Thanks to all the people who have tested the mode. Special thanks to Jens
147 ;; Bloch Helmers for encouraging me to write this code, for creative
148 ;; suggestions as well as for the lists of hpf-commands.
149 ;; Also thanks to the authors of the fortran and pascal modes, on which some
150 ;; of this code is built.
155 ;; 1. Any missing F2003 syntax?
156 ;; 2. Have "f90-mode" just recognize F90 syntax, then derived modes
157 ;; "f95-mode", "f2003-mode" for the language revisions.
158 ;; 3. Support for align.
160 ;; 1. OpenMP, OpenMPI?, preprocessor highlighting.
161 ;; 2. integer_name = 1
162 ;; 3. Labels for "else" statements (F2003)?
164 (defvar comment-auto-fill-only-comments)
165 (defvar font-lock-keywords)
170 "Major mode for editing free format Fortran 90,95 code."
171 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
174 (defgroup f90-indent nil
175 "Indentation in free format Fortran."
180 (defcustom f90-do-indent 3
181 "Extra indentation applied to DO blocks."
186 (defcustom f90-if-indent 3
187 "Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
192 (defcustom f90-type-indent 3
193 "Extra indentation applied to TYPE, ENUM, INTERFACE and BLOCK DATA blocks."
198 (defcustom f90-program-indent 2
199 "Extra indentation applied to PROGRAM, MODULE, SUBROUTINE, FUNCTION blocks."
204 (defcustom f90-associate-indent 2
205 "Extra indentation applied to ASSOCIATE blocks."
211 (defcustom f90-continuation-indent 5
212 "Extra indentation applied to continuation lines."
217 (defcustom f90-comment-region "!!$"
218 "String inserted by \\[f90-comment-region] at start of each line in region."
223 (defcustom f90-indented-comment-re "!"
224 "Regexp matching comments to indent as code."
229 (defcustom f90-directive-comment-re "!hpf\\$"
230 "Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
235 (defcustom f90-beginning-ampersand t
236 "Non-nil gives automatic insertion of \& at start of continuation line."
241 (defcustom f90-smart-end 'blink
242 "Qualification of END statements according to the matching block start.
243 For example, the END that closes an IF block is changed to END
244 IF. If the block has a label, this is added as well. Allowed
245 values are 'blink, 'no-blink, and nil. If nil, nothing is done.
246 The other two settings have the same effect, but 'blink
247 additionally blinks the cursor to the start of the block."
248 :type '(choice (const blink) (const no-blink) (const nil))
249 :safe (lambda (value) (memq value '(blink no-blink nil)))
252 (defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
253 "Regexp matching delimiter characters at which lines may be broken.
254 There are some common two-character tokens where one or more of
255 the members matches this regexp. Although Fortran allows breaks
256 within lexical tokens (provided the next line has a beginning ampersand),
257 the constant `f90-no-break-re' ensures that such tokens are not split."
262 (defcustom f90-break-before-delimiters t
263 "Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
268 (defcustom f90-auto-keyword-case nil
269 "Automatic case conversion of keywords.
270 The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil."
271 :type '(choice (const downcase-word) (const upcase-word)
272 (const capitalize-word) (const nil))
273 :safe (lambda (value) (memq value '(downcase-word
274 capitalize-word upcase-word nil)))
277 (defcustom f90-leave-line-no nil
278 "If non-nil, line numbers are not left justified."
283 (defcustom f90-mode-hook nil
284 "Hook run when entering F90 mode."
286 ;; Not the only safe options, but some common ones.
287 :safe (lambda (value) (member value '((f90-add-imenu-menu) nil)))
288 :options '(f90-add-imenu-menu)
291 ;; User options end here.
293 (defconst f90-keywords-re
294 (regexp-opt '("allocatable" "allocate" "assign" "assignment" "backspace"
295 "block" "call" "case" "character" "close" "common" "complex"
296 "contains" "continue" "cycle" "data" "deallocate"
297 "dimension" "do" "double" "else" "elseif" "elsewhere" "end"
298 "enddo" "endfile" "endif" "entry" "equivalence" "exit"
299 "external" "forall" "format" "function" "goto" "if"
300 "implicit" "include" "inquire" "integer" "intent"
301 "interface" "intrinsic" "logical" "module" "namelist" "none"
302 "nullify" "only" "open" "operator" "optional" "parameter"
303 "pause" "pointer" "precision" "print" "private" "procedure"
304 "program" "public" "read" "real" "recursive" "result" "return"
305 "rewind" "save" "select" "sequence" "stop" "subroutine"
306 "target" "then" "type" "use" "where" "while" "write"
310 "abstract" "associate" "asynchronous" "bind" "class"
311 "deferred" "enum" "enumerator" "extends" "extends_type_of"
312 "final" "generic" "import" "non_intrinsic" "non_overridable"
313 "nopass" "pass" "protected" "same_type_as" "value" "volatile"
315 "contiguous" "submodule" "concurrent"
317 "Regexp used by the function `f90-change-keywords'.")
319 (defconst f90-keywords-level-3-re
321 '("allocatable" "allocate" "assign" "assignment" "backspace"
322 "close" "deallocate" "dimension" "endfile" "entry" "equivalence"
323 "external" "inquire" "intent" "intrinsic" "nullify" "only" "open"
324 ;; FIXME operator and assignment should be F2003 procedures?
325 "operator" "optional" "parameter" "pause" "pointer" "print" "private"
326 "public" "read" "recursive" "result" "rewind" "save" "select"
327 "sequence" "target" "write"
330 ;; F2003. asynchronous separate.
331 "abstract" "deferred" "import" "final" "non_intrinsic" "non_overridable"
332 "nopass" "pass" "protected" "value" "volatile"
334 ;; "concurrent" is only in the sense of "do [,] concurrent", but given
335 ;; the [,] it's simpler to just do every instance (cf "do while").
336 "contiguous" "concurrent"
338 "Keyword-regexp for font-lock level >= 3.")
340 (defconst f90-procedures-re
343 '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
344 "all" "allocated" "anint" "any" "asin" "associated"
345 "atan" "atan2" "bit_size" "btest" "ceiling" "char" "cmplx"
346 "conjg" "cos" "cosh" "count" "cshift" "date_and_time" "dble"
347 "digits" "dim" "dot_product" "dprod" "eoshift" "epsilon"
348 "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
349 "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior"
350 "ishft" "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt"
351 "lle" "llt" "log" "log10" "logical" "matmul" "max"
352 "maxexponent" "maxloc" "maxval" "merge" "min" "minexponent"
353 "minloc" "minval" "mod" "modulo" "mvbits" "nearest" "nint"
354 "not" "pack" "precision" "present" "product" "radix"
355 ;; Real is taken out here to avoid highlighting declarations.
356 "random_number" "random_seed" "range" ;; "real"
357 "repeat" "reshape" "rrspacing" "scale" "scan"
358 "selected_int_kind" "selected_real_kind" "set_exponent"
359 "shape" "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt"
360 "sum" "system_clock" "tan" "tanh" "tiny" "transfer"
361 "transpose" "trim" "ubound" "unpack" "verify"
362 ;; F95 intrinsic functions.
365 "move_alloc" "command_argument_count" "get_command"
366 "get_command_argument" "get_environment_variable"
367 "selected_char_kind" "wait" "flush" "new_line"
368 "extends" "extends_type_of" "same_type_as" "bind"
369 ;; F2003 ieee_arithmetic intrinsic module.
370 "ieee_support_underflow_control" "ieee_get_underflow_mode"
371 "ieee_set_underflow_mode"
372 ;; F2003 iso_c_binding intrinsic module.
373 "c_loc" "c_funloc" "c_associated" "c_f_pointer"
376 "bge" "bgt" "ble" "blt" "dshiftl" "dshiftr" "leadz" "popcnt"
377 "poppar" "trailz" "maskl" "maskr" "shifta" "shiftl" "shiftr"
378 "merge_bits" "iall" "iany" "iparity" "storage_size"
379 "bessel_j0" "bessel_j1" "bessel_jn"
380 "bessel_y0" "bessel_y1" "bessel_yn"
381 "erf" "erfc" "erfc_scaled" "gamma" "hypot" "log_gamma"
382 "norm2" "parity" "findloc" "is_contiguous"
383 ;; F2008 iso_fortran_env module.
384 "compiler_options" "compiler_version"
385 ;; F2008 iso_c_binding module.
388 ;; A left parenthesis to avoid highlighting non-procedures.
390 "Regexp whose first part matches F90 intrinsic procedures.")
392 (defconst f90-operators-re
394 (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
395 "neqv" "not" "or" "true") t)
397 "Regexp matching intrinsic operators.")
399 (defconst f90-hpf-keywords-re
401 ;; Intrinsic procedures.
402 '("all_prefix" "all_scatter" "all_suffix" "any_prefix"
403 "any_scatter" "any_suffix" "copy_prefix" "copy_scatter"
404 "copy_suffix" "count_prefix" "count_scatter" "count_suffix"
405 "grade_down" "grade_up"
406 "hpf_alignment" "hpf_distribution" "hpf_template" "iall" "iall_prefix"
407 "iall_scatter" "iall_suffix" "iany" "iany_prefix" "iany_scatter"
408 "iany_suffix" "ilen" "iparity" "iparity_prefix"
409 "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
410 "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter"
411 "minval_suffix" "number_of_processors" "parity"
412 "parity_prefix" "parity_scatter" "parity_suffix" "popcnt" "poppar"
413 "processors_shape" "product_prefix" "product_scatter"
414 "product_suffix" "sum_prefix" "sum_scatter" "sum_suffix"
416 "align" "distribute" "dynamic" "independent" "inherit" "processors"
417 "realign" "redistribute" "template"
419 "block" "cyclic" "extrinsic" "new" "onto" "pure" "with") 'words)
420 "Regexp for all HPF keywords, procedures and directives.")
422 (defconst f90-constants-re
423 (regexp-opt '( ;; F2003 iso_fortran_env constants.
425 "input_unit" "output_unit" "error_unit"
426 "iostat_end" "iostat_eor"
427 "numeric_storage_size" "character_storage_size"
429 ;; F2003 iso_c_binding constants.
431 "c_int" "c_short" "c_long" "c_long_long" "c_signed_char"
433 "c_int8_t" "c_int16_t" "c_int32_t" "c_int64_t"
434 "c_int_least8_t" "c_int_least16_t" "c_int_least32_t"
436 "c_int_fast8_t" "c_int_fast16_t" "c_int_fast32_t"
438 "c_intmax_t" "c_intptr_t"
439 "c_float" "c_double" "c_long_double"
440 "c_float_complex" "c_double_complex" "c_long_double_complex"
442 "c_null_char" "c_alert" "c_backspace" "c_form_feed"
443 "c_new_line" "c_carriage_return" "c_horizontal_tab"
445 "c_ptr" "c_funptr" "c_null_ptr" "c_null_funptr"
449 ;; F2008 iso_fortran_env constants.
450 "character_kinds" "int8" "int16" "int32" "int64"
451 "integer_kinds" "iostat_inquire_internal_unit"
452 "logical_kinds" "real_kinds" "real32" "real64" "real128"
454 "Regexp for Fortran intrinsic constants.")
456 ;; cf f90-looking-at-type-like.
457 (defun f90-typedef-matcher (limit)
458 "Search for the start/end of the definition of a derived type, up to LIMIT.
459 Set the match data so that subexpression 1,2 are the TYPE, and
460 type-name parts, respectively."
462 (while (and (re-search-forward "\\<\\(\\(?:end[ \t]*\\)?type\\)\\>[ \t]*"
466 (setq l (match-data))
467 (unless (looking-at "\\(is\\>\\|(\\)")
468 (when (if (looking-at "\\(\\sw+\\)")
469 (goto-char (match-end 0))
471 "[ \t]*::[ \t]*\\(\\sw+\\)"
472 (line-end-position) t))
473 ;; 0 is wrong, but we don't use it.
475 (append l (list (match-beginning 1)
480 (defvar f90-font-lock-keywords-1
482 ;; Special highlighting of "module procedure".
483 '("\\<\\(module[ \t]*procedure\\)\\>\\([^()\n]*::\\)?[ \t]*\\([^&!\n]*\\)"
484 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
485 ;; Highlight definition of derived type.
486 ;;; '("\\<\\(\\(?:end[ \t]*\\)?type\\)\\>\\([^()\n]*::\\)?[ \t]*\\(\\sw+\\)"
487 ;;; (1 font-lock-keyword-face) (3 font-lock-function-name-face))
488 '(f90-typedef-matcher
489 (1 font-lock-keyword-face) (2 font-lock-function-name-face))
490 ;; F2003. Prevent operators being highlighted as functions.
491 '("\\<\\(\\(?:end[ \t]*\\)?interface[ \t]*\\(?:assignment\\|operator\\|\
492 read\\|write\\)\\)[ \t]*(" (1 font-lock-keyword-face t))
493 ;; Other functions and declarations. Named interfaces = F2003.
494 ;; F2008: end submodule submodule_name.
495 '("\\<\\(\\(?:end[ \t]*\\)?\\(program\\|\\(?:sub\\)?module\\|\
496 function\\|associate\\|subroutine\\|interface\\)\\|use\\|call\\)\
497 \\>[ \t]*\\(\\sw+\\)?"
498 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
499 ;; F2008: submodule (parent_name) submodule_name.
500 '("\\<\\(submodule\\)\\>[ \t]*([^)\n]+)[ \t]*\\(\\sw+\\)?"
501 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
503 '("\\<\\(use\\)[ \t]*,[ \t]*\\(\\(?:non_\\)?intrinsic\\)[ \t]*::[ \t]*\
505 (1 font-lock-keyword-face) (2 font-lock-keyword-face)
506 (3 font-lock-function-name-face))
507 "\\<\\(\\(end[ \t]*\\)?block[ \t]*data\\|contains\\)\\>"
508 ;; "abstract interface" is F2003.
509 '("\\<abstract[ \t]*interface\\>" (0 font-lock-keyword-face t)))
510 "This does fairly subdued highlighting of comments and function calls.")
512 ;; NB not explicitly handling this, yet it seems to work.
513 ;; type(...) function foo()
514 (defun f90-typedec-matcher (limit)
515 "Search for the declaration of variables of derived type, up to LIMIT.
516 Set the match data so that subexpression 1,2 are the TYPE(...),
517 and variable-name parts, respectively."
518 ;; Matcher functions must return nil only when there are no more
519 ;; matches within the search range.
521 (while (and (re-search-forward "\\<\\(type\\|class\\)[ \t]*(" limit t)
526 ;; Set l after this to just highlight
529 ;; Needed for: type( foo(...) ) :: bar
531 (setq l (list (match-beginning 0) (point)))
532 (skip-chars-forward " \t")
535 ;; type (foo) bar, qux
536 (if (looking-at "\\sw+")
538 ;; type (foo), stuff :: bar, qux
539 "::[ \t]*\\([^&!\n]+\\)")
540 (line-end-position) t)
542 (append (list (car l) (match-end 1))
543 l (list (match-beginning 1)
549 (defvar f90-font-lock-keywords-2
551 f90-font-lock-keywords-1
553 ;; Variable declarations (avoid the real function call).
554 ;; NB by accident (?), this correctly fontifies the "integer" in:
555 ;; integer () function foo ()
556 ;; because "() function foo ()" matches \\3.
557 ;; The "pure" part does not really belong here, but was added to
558 ;; exploit that hack.
559 ;; The "function foo" bit is correctly fontified by keywords-1.
560 ;; TODO ? actually check for balanced parens in that case.
561 '("^[ \t0-9]*\\(?:pure\\|elemental\\)?[ \t]*\
562 \\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
563 enumerator\\|generic\\|procedure\\|logical\\|double[ \t]*precision\\)\
564 \\(.*::\\|[ \t]*(.*)\\)?\\([^&!\n]*\\)"
565 (1 font-lock-type-face t) (4 font-lock-variable-name-face t))
566 ;; Derived type/class variables.
567 ;; TODO ? If we just highlighted the "type" part, rather than
568 ;; "type(...)", this could be in the previous expression. And this
569 ;; would be consistent with integer( kind=8 ), etc.
570 '(f90-typedec-matcher
571 (1 font-lock-type-face) (2 font-lock-variable-name-face))
572 ;; "real function foo (args)". Must override previous. Note hack
573 ;; to get "args" unhighlighted again. Might not always be right,
574 ;; but probably better than leaving them as variables.
575 ;; NB not explicitly handling this case:
576 ;; integer( kind=1 ) function foo()
577 ;; thanks to the happy accident described above.
578 ;; Not anchored, so don't need to worry about "pure" etc.
579 '("\\<\\(\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
580 logical\\|double[ \t]*precision\\|\
581 \\(?:type\\|class\\)[ \t]*([ \t]*\\sw+[ \t]*)\\)[ \t]*\\)\
582 \\(function\\)\\>[ \t]*\\(\\sw+\\)[ \t]*\\(([^&!\n]*)\\)"
583 (1 font-lock-type-face t) (4 font-lock-keyword-face t)
584 (5 font-lock-function-name-face t) (6 'default t))
585 ;; enum (F2003; must be followed by ", bind(C)").
586 '("\\<\\(enum\\)[ \t]*," (1 font-lock-keyword-face))
587 ;; end do, enum (F2003), if, select, where, and forall constructs.
588 '("\\<\\(end[ \t]*\\(do\\|if\\|enum\\|select\\|forall\\|where\\)\\)\\>\
589 \\([ \t]+\\(\\sw+\\)\\)?"
590 (1 font-lock-keyword-face) (3 font-lock-constant-face nil t))
591 '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|\
592 do\\([ \t]*while\\)?\\|select[ \t]*\\(?:case\\|type\\)\\|where\\|\
594 (2 font-lock-constant-face nil t) (3 font-lock-keyword-face))
595 ;; Implicit declaration.
596 '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\
597 \\|enumerator\\|procedure\\|\
598 logical\\|double[ \t]*precision\\|type[ \t]*(\\sw+)\\|none\\)[ \t]*"
599 (1 font-lock-keyword-face) (2 font-lock-type-face))
600 '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
601 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
602 "\\<else\\([ \t]*if\\|where\\)?\\>"
603 '("\\(&\\)[ \t]*\\(!\\|$\\)" (1 font-lock-keyword-face))
604 "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
605 '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>"
606 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
607 '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
608 ;; F2003 "class default".
609 '("\\<\\(class\\)[ \t]*default" . 1)
610 ;; F2003 "type is" in a "select type" block.
611 '("\\<\\(\\(type\\|class\\)[ \t]*is\\)[ \t]*(" (1 font-lock-keyword-face t))
612 '("\\<\\(do\\|go[ \t]*to\\)\\>[ \t]*\\([0-9]+\\)"
613 (1 font-lock-keyword-face) (2 font-lock-constant-face))
614 ;; Line numbers (lines whose first character after number is letter).
615 '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t))))
616 "Highlights declarations, do-loops and other constructs.")
618 (defvar f90-font-lock-keywords-3
619 (append f90-font-lock-keywords-2
621 f90-keywords-level-3-re
623 ;; FIXME why isn't this font-lock-builtin-face, which
624 ;; otherwise we hardly use, as in fortran.el?
625 (list f90-procedures-re '(1 font-lock-keyword-face keep))
626 "\\<real\\>" ; avoid overwriting real defs
627 ;; As an attribute, but not as an optional argument.
628 '("\\<\\(asynchronous\\)[ \t]*[^=]" . 1)))
629 "Highlights all F90 keywords and intrinsic procedures.")
631 (defvar f90-font-lock-keywords-4
632 (append f90-font-lock-keywords-3
633 (list (cons f90-constants-re 'font-lock-constant-face)
634 f90-hpf-keywords-re))
635 "Highlights all F90 and HPF keywords and constants.")
637 (defvar f90-font-lock-keywords
638 f90-font-lock-keywords-2
639 "*Default expressions to highlight in F90 mode.
640 Can be overridden by the value of `font-lock-maximum-decoration'.")
643 (defvar f90-mode-syntax-table
644 (let ((table (make-syntax-table)))
645 (modify-syntax-entry ?\! "<" table) ; begin comment
646 (modify-syntax-entry ?\n ">" table) ; end comment
647 ;; FIXME: This goes against the convention: it should be "_".
648 (modify-syntax-entry ?_ "w" table) ; underscore in names
649 (modify-syntax-entry ?\' "\"" table) ; string quote
650 (modify-syntax-entry ?\" "\"" table) ; string quote
651 ;; FIXME: We used to set ` to word syntax for the benefit of abbrevs, but
652 ;; we do not need it any more. Not sure if it should be "_" or "." now.
653 (modify-syntax-entry ?\` "_" table)
654 (modify-syntax-entry ?\r " " table) ; return is whitespace
655 (modify-syntax-entry ?+ "." table) ; punctuation
656 (modify-syntax-entry ?- "." table)
657 (modify-syntax-entry ?= "." table)
658 (modify-syntax-entry ?* "." table)
659 (modify-syntax-entry ?/ "." table)
660 (modify-syntax-entry ?% "." table) ; bug#8820
661 ;; I think that the f95 standard leaves the behavior of \
662 ;; unspecified, but that f2k will require it to be non-special.
663 ;; Use `f90-backslash-not-special' to change.
664 (modify-syntax-entry ?\\ "\\" table) ; escape chars
666 "Syntax table used in F90 mode.")
669 (let ((map (make-sparse-keymap)))
670 (define-key map "`" 'f90-abbrev-start)
671 (define-key map "\C-c;" 'f90-comment-region)
672 (define-key map "\C-\M-a" 'f90-beginning-of-subprogram)
673 (define-key map "\C-\M-e" 'f90-end-of-subprogram)
674 (define-key map "\C-\M-h" 'f90-mark-subprogram)
675 (define-key map "\C-\M-n" 'f90-end-of-block)
676 (define-key map "\C-\M-p" 'f90-beginning-of-block)
677 (define-key map "\C-\M-q" 'f90-indent-subprogram)
678 (define-key map "\C-j" 'f90-indent-new-line) ; LFD equals C-j
679 ;;; (define-key map "\r" 'newline)
680 (define-key map "\C-c\r" 'f90-break-line)
681 ;;; (define-key map [M-return] 'f90-break-line)
682 (define-key map "\C-c\C-a" 'f90-previous-block)
683 (define-key map "\C-c\C-e" 'f90-next-block)
684 (define-key map "\C-c\C-d" 'f90-join-lines)
685 (define-key map "\C-c\C-f" 'f90-fill-region)
686 (define-key map "\C-c\C-p" 'f90-previous-statement)
687 (define-key map "\C-c\C-n" 'f90-next-statement)
688 (define-key map "\C-c]" 'f90-insert-end)
689 (define-key map "\C-c\C-w" 'f90-insert-end)
690 ;; Standard tab binding will call this, and also handle regions.
691 ;;; (define-key map "\t" 'f90-indent-line)
692 (define-key map "," 'f90-electric-insert)
693 (define-key map "+" 'f90-electric-insert)
694 (define-key map "-" 'f90-electric-insert)
695 (define-key map "*" 'f90-electric-insert)
696 (define-key map "/" 'f90-electric-insert)
698 (easy-menu-define f90-menu map "Menu for F90 mode."
701 ,(custom-menu-create 'f90)
703 ["Set" Custom-set :active t
704 :help "Set current value of all edited settings in the buffer"]
705 ["Save" Custom-save :active t
706 :help "Set and save all edited settings"]
707 ["Reset to Current" Custom-reset-current :active t
708 :help "Reset all edited settings to current"]
709 ["Reset to Saved" Custom-reset-saved :active t
710 :help "Reset all edited or set settings to saved"]
711 ["Reset to Standard Settings" Custom-reset-standard :active t
712 :help "Erase all cusomizations in buffer"]
715 ["Indent Subprogram" f90-indent-subprogram t]
716 ["Mark Subprogram" f90-mark-subprogram :active t :help
717 "Mark the end of the current subprogram, move point to the start"]
718 ["Beginning of Subprogram" f90-beginning-of-subprogram :active t
719 :help "Move point to the start of the current subprogram"]
720 ["End of Subprogram" f90-end-of-subprogram :active t
721 :help "Move point to the end of the current subprogram"]
723 ["(Un)Comment Region" f90-comment-region :active mark-active
724 :help "Comment or uncomment the region"]
725 ["Indent Region" f90-indent-region :active mark-active]
726 ["Fill Region" f90-fill-region :active mark-active
727 :help "Fill long lines in the region"]
729 ["Break Line at Point" f90-break-line :active t
730 :help "Break the current line at point"]
731 ["Join with Previous Line" f90-join-lines :active t
732 :help "Join the current line to the previous one"]
733 ["Insert Block End" f90-insert-end :active t
734 :help "Insert an end statement for the current code block"]
737 :help "Fontify this buffer to varying degrees"
738 ["Toggle font-lock-mode" font-lock-mode :selected font-lock-mode
739 :style toggle :help "Fontify text in this buffer"]
741 ["Light highlighting (level 1)" f90-font-lock-1 t]
742 ["Moderate highlighting (level 2)" f90-font-lock-2 t]
743 ["Heavy highlighting (level 3)" f90-font-lock-3 t]
744 ["Maximum highlighting (level 4)" f90-font-lock-4 t]
746 ("Change Keyword Case"
747 :help "Change the case of keywords in the buffer or region"
748 ["Upcase Keywords (buffer)" f90-upcase-keywords t]
749 ["Capitalize Keywords (buffer)" f90-capitalize-keywords t]
750 ["Downcase Keywords (buffer)" f90-downcase-keywords t]
752 ["Upcase Keywords (region)" f90-upcase-region-keywords
754 ["Capitalize Keywords (region)" f90-capitalize-region-keywords
756 ["Downcase Keywords (region)" f90-downcase-region-keywords
760 ["Toggle Auto Fill" auto-fill-mode :selected auto-fill-function
762 :help "Automatically fill text while typing in this buffer"]
763 ["Toggle Abbrev Mode" abbrev-mode :selected abbrev-mode
764 :style toggle :help "Expand abbreviations while typing in this buffer"]
765 ["Add Imenu Menu" f90-add-imenu-menu
766 :active (not (lookup-key (current-local-map) [menu-bar index]))
767 :included (fboundp 'imenu-add-to-menubar)
768 :help "Add an index menu to the menu-bar"
771 "Keymap used in F90 mode.")
774 (defun f90-font-lock-n (n)
775 "Set `font-lock-keywords' to F90 level N keywords."
777 (setq font-lock-keywords
778 (symbol-value (intern-soft (format "f90-font-lock-keywords-%d" n))))
779 (font-lock-fontify-buffer))
781 (defun f90-font-lock-1 ()
782 "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
786 (defun f90-font-lock-2 ()
787 "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
791 (defun f90-font-lock-3 ()
792 "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
796 (defun f90-font-lock-4 ()
797 "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
801 ;; Regexps for finding program structures.
802 (defconst f90-blocks-re
803 (concat "\\(block[ \t]*data\\|"
804 (regexp-opt '("do" "if" "interface" "function" "module" "program"
805 "select" "subroutine" "type" "where" "forall"
811 "Regexp potentially indicating a \"block\" of F90 code.")
813 (defconst f90-program-block-re
814 (regexp-opt '("program" "module" "subroutine" "function" "submodule") 'paren)
815 "Regexp used to locate the start/end of a \"subprogram\".")
817 ;; "class is" is F2003.
818 (defconst f90-else-like-re
819 "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\|\
820 \\(class\\|type\\)[ \t]*is[ \t]*(\\|class[ \t]*default\\)"
821 "Regexp matching an ELSE IF, ELSEWHERE, CASE, CLASS/TYPE IS statement.")
823 (defconst f90-end-if-re
825 (regexp-opt '("if" "select" "where" "forall") 'paren)
827 "Regexp matching the end of an IF, SELECT, WHERE, FORALL block.")
829 (defconst f90-end-type-re
830 "end[ \t]*\\(type\\|enum\\|interface\\|block[ \t]*data\\)\\>"
831 "Regexp matching the end of a TYPE, ENUM, INTERFACE, BLOCK DATA section.")
833 (defconst f90-end-associate-re
834 "end[ \t]*associate\\>"
835 "Regexp matching the end of an ASSOCIATE block.")
837 ;; This is for a TYPE block, not a variable of derived TYPE.
838 ;; Hence no need to add CLASS for F2003.
839 (defconst f90-type-def-re
842 ;; type, stuff :: word
843 ;; type, bind(c) :: word
845 "\\<\\(type\\)\\>\\(?:\\(?:[^()\n]*\\|\
846 .*,[ \t]*bind[ \t]*([ \t]*c[ \t]*)[ \t]*\\)::\\)?[ \t]*\\(\\sw+\\)"
847 "Regexp matching the definition of a derived type.")
849 (defconst f90-typeis-re
850 "\\<\\(class\\|type\\)[ \t]*is[ \t]*("
851 "Regexp matching a CLASS/TYPE IS statement.")
853 (defconst f90-no-break-re
854 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=" "(/" "/)") 'paren)
855 "Regexp specifying two-character tokens not to split when breaking lines.
856 Each token has one or more of the characters from `f90-break-delimiters'.
857 Note that if only one of the characters is from that variable,
858 then the presence of the token here allows a line-break before or
859 after the other character, where a break would not normally be
860 allowed. This minor issue currently only affects \"(/\" and \"/)\".")
862 (defvar f90-cache-position nil
863 "Temporary position used to speed up region operations.")
864 (make-variable-buffer-local 'f90-cache-position)
868 (defconst f90-end-block-re
869 (concat "^[ \t0-9]*\\<end[ \t]*"
870 (regexp-opt '("do" "if" "forall" "function" "interface"
871 "module" "program" "select" "subroutine"
872 "type" "where" "enum" "associate" "submodule") t)
874 "Regexp matching the end of an F90 \"block\", from the line start.
875 Used in the F90 entry in `hs-special-modes-alist'.")
877 ;; Ignore the fact that FUNCTION, SUBROUTINE, WHERE, FORALL have a
878 ;; following "(". DO, CASE, IF can have labels.
879 (defconst f90-start-block-re
881 "^[ \t0-9]*" ; statement number
883 "\\(\\sw+[ \t]*:[ \t]*\\)?" ; structure label
884 "\\(do\\|select[ \t]*\\(case\\|type\\)\\|"
885 ;; See comments in fortran-start-block-re for the problems of IF.
887 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
888 ;; Distinguish WHERE block from isolated WHERE.
889 "\\(where\\|forall\\)[ \t]*(.*)[ \t]*\\(!\\|$\\)\\)\\)"
891 ;; Avoid F2003 "type is" in "select type",
892 ;; and also variables of derived type "type (foo)".
893 ;; "type, foo" must be a block (?).
895 "[^i(!\n\"\& \t]\\|" ; not-i(
896 "i[^s!\n\"\& \t]\\|" ; i not-s
898 ;; "abstract interface" is F2003; "submodule" is F2008.
899 "program\\|\\(?:abstract[ \t]*\\)?interface\\|\\(?:sub\\)?module\\|"
900 ;; "enum", but not "enumerator".
901 "function\\|subroutine\\|enum[^e]\\|associate"
904 "Regexp matching the start of an F90 \"block\", from the line start.
905 A simple regexp cannot do this in fully correct fashion, so this
906 tries to strike a compromise between complexity and flexibility.
907 Used in the F90 entry in `hs-special-modes-alist'.")
909 ;; hs-special-modes-alist is autoloaded.
910 (add-to-list 'hs-special-modes-alist
911 `(f90-mode ,f90-start-block-re ,f90-end-block-re
912 "!" f90-end-of-block nil))
916 ;; FIXME trivial to extend this to enum. Worth it?
917 (defun f90-imenu-type-matcher ()
918 "Search backward for the start of a derived type.
919 Set subexpression 1 in the match-data to the name of the type."
921 (while (and (re-search-backward "^[ \t0-9]*type[ \t]*" nil t)
924 (goto-char (match-end 0))
925 (unless (looking-at "\\(is\\>\\|(\\)")
926 (or (looking-at "\\(\\sw+\\)")
928 "[ \t]*::[ \t]*\\(\\sw+\\)"
929 (line-end-position) t))))))))
932 (defvar f90-imenu-generic-expression
933 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
934 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]")
935 ;; (not-ib "[^i(!\n\"\& \t]") (not-s "[^s!\n\"\& \t]")
938 '(nil "^[ \t0-9]*program[ \t]+\\(\\sw+\\)" 1)
939 '("Submodules" "^[ \t0-9]*submodule[ \t]*([^)\n]+)[ \t]*\
940 \\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
941 '("Modules" "^[ \t0-9]*module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
942 (list "Types" 'f90-imenu-type-matcher 1)
943 ;; Does not handle: "type[, stuff] :: foo".
944 ;;; (format "^[ \t0-9]*type[ \t]+\\(\\(%s\\|i%s\\|is\\sw\\)\\sw*\\)"
947 ;; Can't get the subexpression numbers to match in the two branches.
948 ;;; (format "^[ \t0-9]*type\\([ \t]*,.*\\(::\\)[ \t]*\\(\\sw+\\)\\|[ \t]+\\(\\(%s\\|i%s\\|is\\sw\\)\\sw*\\)\\)" not-ib not-s)
955 ;; At least three non-space characters before function/subroutine.
956 ;; Check that the last three non-space characters do not spell E N D.
958 not-e good-char good-char "\\|"
959 good-char not-n good-char "\\|"
960 good-char good-char not-d "\\)"
962 ;; Less than three non-space characters before function/subroutine.
963 good-char "?" good-char "?"
965 "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
967 "Value for `imenu-generic-expression' in F90 mode.")
969 (defun f90-add-imenu-menu ()
970 "Add an imenu menu to the menubar."
972 (if (lookup-key (current-local-map) [menu-bar index])
973 (message "%s" "F90-imenu already exists.")
974 (imenu-add-to-menubar "F90-imenu")
975 (redraw-frame (selected-frame))))
978 ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
979 (define-abbrev-table 'f90-mode-abbrev-table
980 (mapcar (lambda (e) (list (car e) (cdr e) nil :system t))
981 '(("`al" . "allocate" )
982 ("`ab" . "allocatable" )
983 ("`ai" . "abstract interface")
984 ("`as" . "assignment" )
985 ("`asy" . "asynchronous" )
986 ("`ba" . "backspace" )
987 ("`bd" . "block data" )
988 ("`c" . "character" )
992 ("`cn" . "contains" )
994 ("`de" . "deallocate" )
996 ("`di" . "dimension" )
997 ("`dp" . "double precision")
998 ("`dw" . "do while" )
1000 ("`eli" . "else if" )
1001 ("`elw" . "elsewhere" )
1002 ("`em" . "elemental" )
1003 ("`e" . "enumerator" )
1004 ("`eq" . "equivalence" )
1005 ("`ex" . "external" )
1009 ("`fu" . "function" )
1010 ("`fa" . ".false." )
1011 ("`im" . "implicit none")
1012 ("`in" . "include" )
1015 ("`if" . "interface" )
1016 ("`lo" . "logical" )
1018 ("`na" . "namelist" )
1019 ("`nu" . "nullify" )
1020 ("`op" . "optional" )
1021 ("`pa" . "parameter" )
1022 ("`po" . "pointer" )
1024 ("`pi" . "private" )
1025 ("`pm" . "program" )
1026 ("`pr" . "protected" )
1029 ("`rc" . "recursive" )
1033 ("`sq" . "sequence" )
1034 ("`su" . "subroutine" )
1038 ("`vo" . "volatile" )
1040 ("`wr" . "write" )))
1041 "Abbrev table for F90 mode."
1042 ;; Accept ` as the first char of an abbrev. Also allow _ in abbrevs.
1043 :regexp "\\(?:[^[:word:]_`]\\|^\\)\\(`?[[:word:]_]+\\)[^[:word:]_]*")
1046 (define-derived-mode f90-mode prog-mode "F90"
1047 "Major mode for editing Fortran 90,95 code in free format.
1048 For fixed format code, use `fortran-mode'.
1050 \\[f90-indent-line] indents the current line.
1051 \\[f90-indent-new-line] indents current line and creates a new\
1053 \\[f90-indent-subprogram] indents the current subprogram.
1055 Type `? or `\\[help-command] to display a list of built-in\
1056 abbrevs for F90 keywords.
1061 Variables controlling indentation style and extra features:
1064 Extra indentation within do blocks (default 3).
1066 Extra indentation within if/select/where/forall blocks (default 3).
1068 Extra indentation within type/enum/interface/block-data blocks (default 3).
1069 `f90-program-indent'
1070 Extra indentation within program/module/subroutine/function blocks
1072 `f90-continuation-indent'
1073 Extra indentation applied to continuation lines (default 5).
1074 `f90-comment-region'
1075 String inserted by function \\[f90-comment-region] at start of each
1076 line in region (default \"!!!$\").
1077 `f90-indented-comment-re'
1078 Regexp determining the type of comment to be intended like code
1080 `f90-directive-comment-re'
1081 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
1082 (default \"!hpf\\\\$\").
1083 `f90-break-delimiters'
1084 Regexp holding list of delimiters at which lines may be broken
1085 (default \"[-+*/><=,% \\t]\").
1086 `f90-break-before-delimiters'
1087 Non-nil causes `f90-do-auto-fill' to break lines before delimiters
1089 `f90-beginning-ampersand'
1090 Automatic insertion of \& at beginning of continuation lines (default t).
1092 From an END statement, check and fill the end using matching block start.
1093 Allowed values are 'blink, 'no-blink, and nil, which determine
1094 whether to blink the matching beginning (default 'blink).
1095 `f90-auto-keyword-case'
1096 Automatic change of case of keywords (default nil).
1097 The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
1099 Do not left-justify line numbers (default nil).
1101 Turning on F90 mode calls the value of the variable `f90-mode-hook'
1102 with no args, if that value is non-nil."
1104 :abbrev-table f90-mode-abbrev-table
1105 (set (make-local-variable 'indent-line-function) 'f90-indent-line)
1106 (set (make-local-variable 'indent-region-function) 'f90-indent-region)
1107 (set (make-local-variable 'comment-start) "!")
1108 (set (make-local-variable 'comment-start-skip) "!+ *")
1109 (set (make-local-variable 'comment-indent-function) 'f90-comment-indent)
1110 (set (make-local-variable 'abbrev-all-caps) t)
1111 (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
1112 (setq indent-tabs-mode nil) ; auto buffer local
1113 (set (make-local-variable 'font-lock-defaults)
1114 '((f90-font-lock-keywords f90-font-lock-keywords-1
1115 f90-font-lock-keywords-2
1116 f90-font-lock-keywords-3
1117 f90-font-lock-keywords-4)
1119 (set (make-local-variable 'imenu-case-fold-search) t)
1120 (set (make-local-variable 'imenu-generic-expression)
1121 f90-imenu-generic-expression)
1122 (set (make-local-variable 'beginning-of-defun-function)
1123 'f90-beginning-of-subprogram)
1124 (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
1125 (set (make-local-variable 'add-log-current-defun-function)
1126 #'f90-current-defun))
1129 ;; Inline-functions.
1130 (defsubst f90-in-string ()
1131 "Return non-nil if point is inside a string.
1132 Checks from `point-min', or `f90-cache-position', if that is non-nil
1133 and lies before point."
1135 (if (and f90-cache-position (> (point) f90-cache-position))
1138 (nth 3 (parse-partial-sexp beg-pnt (point)))))
1140 (defsubst f90-in-comment ()
1141 "Return non-nil if point is inside a comment.
1142 Checks from `point-min', or `f90-cache-position', if that is non-nil
1143 and lies before point."
1145 (if (and f90-cache-position (> (point) f90-cache-position))
1148 (nth 4 (parse-partial-sexp beg-pnt (point)))))
1150 (defsubst f90-line-continued ()
1151 "Return t if the current line is a continued one.
1152 This includes comment lines embedded in continued lines, but
1153 not the last line of a continued statement."
1156 (while (and (looking-at "[ \t]*\\(!\\|$\\)") (zerop (forward-line -1))))
1158 (while (f90-in-comment)
1159 (search-backward "!" (line-beginning-position))
1160 (skip-chars-backward "!"))
1161 (skip-chars-backward " \t")
1162 (= (preceding-char) ?&)))
1164 ;; GM this is not right, eg a continuation line starting with a number.
1165 ;; Need f90-code-start-position function.
1166 ;; And yet, things seems to work with this...
1167 ;; cf f90-indent-line
1168 ;; (beginning-of-line) ; digits after & \n are not line-nos
1169 ;; (if (not (save-excursion (and (f90-previous-statement)
1170 ;; (f90-line-continued))))
1171 ;; (f90-indent-line-no)
1172 (defsubst f90-current-indentation ()
1173 "Return indentation of current line.
1174 Line-numbers are considered whitespace characters."
1175 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
1177 (defsubst f90-indent-to (col &optional no-line-number)
1178 "Indent current line to column COL.
1179 If optional argument NO-LINE-NUMBER is nil, jump over a possible
1180 line-number before indenting."
1183 (skip-chars-forward " \t0-9"))
1184 (delete-horizontal-space)
1185 ;; Leave >= 1 space after line number.
1186 (indent-to col (if (zerop (current-column)) 0 1)))
1188 (defsubst f90-get-present-comment-type ()
1189 "If point lies within a comment, return the string starting the comment.
1190 For example, \"!\" or \"!!\", followed by the appropriate amount of
1191 whitespace, if any."
1192 ;; Include the whitespace for consistent auto-filling of comment blocks.
1194 (when (f90-in-comment)
1196 (re-search-forward "!+[ \t]*" (line-end-position))
1197 (while (f90-in-string)
1198 (re-search-forward "!+[ \t]*" (line-end-position)))
1199 (match-string-no-properties 0))))
1201 (defsubst f90-equal-symbols (a b)
1202 "Compare strings A and B neglecting case and allowing for nil value."
1203 (equal (if a (downcase a) nil)
1204 (if b (downcase b) nil)))
1206 (defsubst f90-looking-at-do ()
1207 "Return (\"do\" NAME) if a do statement starts after point.
1208 NAME is nil if the statement has no label."
1209 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\>")
1210 (list (match-string 3) (match-string 2))))
1212 (defsubst f90-looking-at-select-case ()
1213 "Return (\"select\" NAME) if a select statement starts after point.
1214 NAME is nil if the statement has no label."
1215 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
1216 \\(select\\)[ \t]*\\(case\\|type\\)[ \t]*(")
1217 (list (match-string 3) (match-string 2))))
1219 (defsubst f90-looking-at-if-then ()
1220 "Return (\"if\" NAME) if an if () then statement starts after point.
1221 NAME is nil if the statement has no label."
1223 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\>")
1224 (let ((struct (match-string 3))
1225 (label (match-string 2))
1226 (pos (scan-lists (point) 1 0)))
1227 (and pos (goto-char pos))
1228 (skip-chars-forward " \t")
1229 (if (or (looking-at "then\\>")
1230 (when (f90-line-continued)
1231 (f90-next-statement)
1232 (skip-chars-forward " \t0-9&")
1233 (looking-at "then\\>")))
1234 (list struct label))))))
1237 (defsubst f90-looking-at-associate ()
1238 "Return (\"associate\") if an associate block starts after point."
1239 (if (looking-at "\\<\\(associate\\)[ \t]*(")
1240 (list (match-string 1))))
1242 (defsubst f90-looking-at-where-or-forall ()
1243 "Return (KIND NAME) if a where or forall block starts after point.
1244 NAME is nil if the statement has no label."
1246 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
1247 \\(where\\|forall\\)\\>")
1248 (let ((struct (match-string 3))
1249 (label (match-string 2))
1250 (pos (scan-lists (point) 1 0)))
1251 (and pos (goto-char pos))
1252 (skip-chars-forward " \t")
1253 (if (looking-at "\\(!\\|$\\)") (list struct label))))))
1255 (defsubst f90-looking-at-type-like ()
1256 "Return (KIND NAME) if a type/enum/interface/block-data starts after point.
1257 NAME is non-nil only for type and certain interfaces."
1260 (and (looking-at "\\<type\\>[ \t]*")
1261 (goto-char (match-end 0))
1262 (not (looking-at "\\(is\\>\\|(\\)"))
1263 (or (looking-at "\\(\\sw+\\)")
1264 (re-search-forward "[ \t]*::[ \t]*\\(\\sw+\\)"
1265 (line-end-position) t))))
1266 (list "type" (match-string 1)))
1267 ;;; ((and (not (looking-at f90-typeis-re))
1268 ;;; (looking-at f90-type-def-re))
1269 ;;; (list (match-string 1) (match-string 2)))
1270 ((looking-at "\\<\\(interface\\)\\>[ \t]*")
1271 (list (match-string 1)
1273 (goto-char (match-end 0))
1274 (if (or (looking-at "\\(operator\\|assignment\\|read\\|\
1275 write\\)[ \t]*([^)\n]*)")
1276 (looking-at "\\sw+"))
1277 (match-string 0)))))
1278 ((looking-at "\\(enum\\|block[ \t]*data\\)\\>")
1279 (list (match-string 1) nil))
1280 ((looking-at "abstract[ \t]*\\(interface\\)\\>")
1281 (list (match-string 1) nil))))
1283 (defsubst f90-looking-at-program-block-start ()
1284 "Return (KIND NAME) if a program block with name NAME starts after point."
1285 ;;;NAME is nil for an un-named main PROGRAM block."
1287 ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
1288 (list (match-string 1) (match-string 2)))
1289 ((and (not (looking-at "module[ \t]*procedure\\>"))
1290 (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
1291 (list (match-string 1) (match-string 2)))
1292 ((looking-at "\\(submodule\\)[ \t]*([^)\n]+)[ \t]*\\(\\sw+\\)\\>")
1293 (list (match-string 1) (match-string 2)))
1294 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
1295 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
1297 (list (match-string 1) (match-string 2)))))
1298 ;; Following will match an un-named main program block; however
1299 ;; one needs to check if there is an actual PROGRAM statement after
1300 ;; point (and before any END program). Adding this will require
1301 ;; change to eg f90-calculate-indent.
1302 ;;; ((save-excursion
1303 ;;; (not (f90-previous-statement)))
1304 ;;; '("program" nil))))
1306 (defsubst f90-looking-at-program-block-end ()
1307 "Return (KIND NAME) if a block with name NAME ends after point."
1308 (cond ((looking-at "end[ \t]*\\(interface\\)[ \t]*\\(\
1309 \\(?:assignment\\|operator\\|read\\|write\\)[ \t]*([^)\n]*)\\)")
1310 (list (match-string 1) (match-string 2)))
1311 ((looking-at (concat "end[ \t]*" f90-blocks-re
1312 "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
1313 (list (match-string 1) (match-string 3)))))
1315 (defsubst f90-comment-indent ()
1316 "Return the indentation to be used for a comment starting at point.
1317 Used for `comment-indent-function' by F90 mode.
1318 \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
1319 `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
1320 All others return `comment-column', leaving at least one space after code."
1321 (cond ((looking-at "!!!") 0)
1322 ((and f90-directive-comment-re
1323 (looking-at f90-directive-comment-re)) 0)
1324 ((looking-at (regexp-quote f90-comment-region)) 0)
1325 ((and (looking-at f90-indented-comment-re)
1326 ;; Don't attempt to indent trailing comment as code.
1328 (skip-chars-backward " \t")
1330 (f90-calculate-indent))
1332 (skip-chars-backward " \t")
1333 (max (if (bolp) 0 (1+ (current-column))) comment-column)))))
1335 (defsubst f90-present-statement-cont ()
1336 "Return continuation properties of present statement.
1337 Possible return values are:
1338 single - statement is not continued.
1339 begin - current line is the first in a continued statement.
1340 end - current line is the last in a continued statement
1341 middle - current line is neither first nor last in a continued statement.
1342 Comment lines embedded amongst continued lines return 'middle."
1345 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
1346 (setq cont (f90-line-continued))
1347 (cond ((and (not pcont) (not cont)) 'single)
1348 ((and (not pcont) cont) 'begin)
1349 ((and pcont (not cont)) 'end)
1350 ((and pcont cont) 'middle)
1351 (t (error "The impossible occurred")))))
1353 (defsubst f90-indent-line-no ()
1354 "If `f90-leave-line-no' is nil, left-justify a line number.
1355 Leaves point at the first non-blank character after the line number.
1356 Call from beginning of line."
1357 (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
1358 (delete-horizontal-space))
1359 (skip-chars-forward " \t0-9"))
1361 (defsubst f90-no-block-limit ()
1362 "Return nil if point is at the edge of a code block.
1363 Searches line forward for \"function\" or \"subroutine\",
1366 (not (or (looking-at "end")
1367 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
1368 \\|select[ \t]*\\(case\\|type\\)\\|case\\|where\\|forall\\)\\>")
1369 (looking-at "\\(program\\|\\(?:sub\\)?module\\|\
1370 \\(?:abstract[ \t]*\\)?interface\\|block[ \t]*data\\)\\>")
1371 (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
1372 (looking-at f90-type-def-re)
1373 (re-search-forward "\\(function\\|subroutine\\)"
1374 (line-end-position) t)))))
1376 (defsubst f90-update-line ()
1377 "Change case of current line as per `f90-auto-keyword-case'."
1378 (if f90-auto-keyword-case
1379 (f90-change-keywords f90-auto-keyword-case
1380 (line-beginning-position) (line-end-position))))
1382 (defun f90-electric-insert (&optional arg)
1383 "Change keyword case and auto-fill line as operators are inserted."
1385 (self-insert-command arg)
1386 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1389 ;; Behave like self-insert-command for delete-selection-mode (bug#5593).
1390 (put 'f90-electric-insert 'delete-selection t)
1392 (defun f90-get-correct-indent ()
1393 "Get correct indent for a line starting with line number.
1394 Does not check type and subprogram indentation."
1395 (let ((epnt (line-end-position)) icol)
1397 (while (and (f90-previous-statement)
1398 (or (memq (f90-present-statement-cont) '(middle end))
1399 (looking-at "[ \t]*[0-9]"))))
1400 (setq icol (current-indentation))
1402 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1403 (line-end-position) t)
1405 (skip-chars-forward " \t")
1406 (cond ((f90-looking-at-do)
1407 (setq icol (+ icol f90-do-indent)))
1408 ((or (f90-looking-at-if-then)
1409 (f90-looking-at-where-or-forall)
1410 (f90-looking-at-select-case))
1411 (setq icol (+ icol f90-if-indent)))
1412 ((f90-looking-at-associate)
1413 (setq icol (+ icol f90-associate-indent))))
1415 (while (re-search-forward
1416 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1418 (skip-chars-forward " \t0-9")
1419 (cond ((f90-looking-at-do)
1420 (setq icol (+ icol f90-do-indent)))
1421 ((or (f90-looking-at-if-then)
1422 (f90-looking-at-where-or-forall)
1423 (f90-looking-at-select-case))
1424 (setq icol (+ icol f90-if-indent)))
1425 ((f90-looking-at-associate)
1426 (setq icol (+ icol f90-associate-indent)))
1427 ((looking-at f90-end-if-re)
1428 (setq icol (- icol f90-if-indent)))
1429 ((looking-at f90-end-associate-re)
1430 (setq icol (- icol f90-associate-indent)))
1431 ((looking-at "end[ \t]*do\\>")
1432 (setq icol (- icol f90-do-indent))))
1436 (defun f90-calculate-indent ()
1437 "Calculate the indent column based on previous statements."
1439 (let (icol cont (case-fold-search t) (pnt (point)))
1441 (if (not (f90-previous-statement))
1442 ;; If f90-previous-statement returns nil, we must have been
1443 ;; called from on or before the first line of the first statement.
1444 (setq icol (if (save-excursion
1445 ;; f90-previous-statement has moved us over
1446 ;; comment/blank lines, so we need to get
1447 ;; back to the first code statement.
1448 (when (looking-at "[ \t]*\\([!#]\\|$\\)")
1449 (f90-next-statement))
1450 (skip-chars-forward " \t0-9")
1451 (f90-looking-at-program-block-start))
1453 ;; No explicit PROGRAM start statement.
1454 f90-program-indent))
1455 (setq cont (f90-present-statement-cont))
1457 (while (not (eq 'begin (f90-present-statement-cont)))
1458 (f90-previous-statement)))
1459 (cond ((eq cont 'begin)
1460 (setq icol (+ (f90-current-indentation)
1461 f90-continuation-indent)))
1462 ((eq cont 'middle) (setq icol (current-indentation)))
1463 (t (setq icol (f90-current-indentation))
1464 (skip-chars-forward " \t")
1465 (if (looking-at "[0-9]")
1466 (setq icol (f90-get-correct-indent))
1467 (cond ((or (f90-looking-at-if-then)
1468 (f90-looking-at-where-or-forall)
1469 (f90-looking-at-select-case)
1470 (looking-at f90-else-like-re))
1471 (setq icol (+ icol f90-if-indent)))
1472 ((f90-looking-at-do)
1473 (setq icol (+ icol f90-do-indent)))
1474 ((f90-looking-at-type-like)
1475 (setq icol (+ icol f90-type-indent)))
1476 ((f90-looking-at-associate)
1477 (setq icol (+ icol f90-associate-indent)))
1478 ((or (f90-looking-at-program-block-start)
1479 (looking-at "contains[ \t]*\\($\\|!\\)"))
1480 (setq icol (+ icol f90-program-indent)))))
1483 (cond ((looking-at "[ \t]*$"))
1484 ((looking-at "[ \t]*#") ; check for cpp directive
1487 (skip-chars-forward " \t0-9")
1488 (cond ((or (looking-at f90-else-like-re)
1489 (looking-at f90-end-if-re))
1490 (setq icol (- icol f90-if-indent)))
1491 ((looking-at "end[ \t]*do\\>")
1492 (setq icol (- icol f90-do-indent)))
1493 ((looking-at f90-end-type-re)
1494 (setq icol (- icol f90-type-indent)))
1495 ((looking-at f90-end-associate-re)
1496 (setq icol (- icol f90-associate-indent)))
1497 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1498 (f90-looking-at-program-block-end))
1499 (setq icol (- icol f90-program-indent))))))))))
1502 (defun f90-previous-statement ()
1503 "Move point to beginning of the previous F90 statement.
1504 If no previous statement is found (i.e. if called from the first
1505 statement in the buffer), move to the start of the buffer and
1506 return nil. A statement is a line which is neither blank nor a
1509 (let (not-first-statement)
1511 (while (and (setq not-first-statement (zerop (forward-line -1)))
1512 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1513 not-first-statement))
1515 (defun f90-next-statement ()
1516 "Move point to beginning of the next F90 statement.
1517 Return nil if no later statement is found."
1519 (let (not-last-statement)
1521 (while (and (setq not-last-statement
1522 (and (zerop (forward-line 1))
1524 (looking-at "[ \t0-9]*\\(!\\|$\\)")))
1525 not-last-statement))
1527 (defun f90-beginning-of-subprogram ()
1528 "Move point to the beginning of the current subprogram.
1529 Return (TYPE NAME), or nil if not found."
1531 (let ((count 1) (case-fold-search t) matching-beg)
1533 (while (and (> count 0)
1534 (re-search-backward f90-program-block-re nil 'move))
1536 (skip-chars-forward " \t0-9")
1537 (cond ((setq matching-beg (f90-looking-at-program-block-start))
1538 (setq count (1- count)))
1539 ((f90-looking-at-program-block-end)
1540 (setq count (1+ count)))))
1544 ;; Note this includes the case of an un-named main program,
1545 ;; in which case we go to (point-min).
1546 (if (called-interactively-p 'interactive)
1547 (message "No beginning found"))
1550 (defun f90-end-of-subprogram ()
1551 "Move point to the end of the current subprogram.
1552 Return (TYPE NAME), or nil if not found."
1554 (let ((case-fold-search t)
1558 (while (and (> count 0)
1559 (re-search-forward f90-program-block-re nil 'move))
1561 (skip-chars-forward " \t0-9")
1562 (cond ((f90-looking-at-program-block-start)
1563 (setq count (1+ count)))
1564 ((setq matching-end (f90-looking-at-program-block-end))
1565 (setq count (1- count))))
1567 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1568 ;; has a net non-zero effect, which seems odd.
1569 ;;; (forward-line 1)
1572 (if (called-interactively-p 'interactive)
1573 (message "No end found"))
1577 (defun f90-end-of-block (&optional num)
1578 "Move point forward to the end of the current code block.
1579 With optional argument NUM, go forward that many balanced blocks.
1580 If NUM is negative, go backward to the start of a block. Checks
1581 for consistency of block types and labels (if present), and
1582 completes outermost block if `f90-smart-end' is non-nil.
1583 Interactively, pushes mark before moving point."
1585 ;; Can move some distance.
1586 (if (called-interactively-p 'any) (push-mark (point) t))
1587 (and num (< num 0) (f90-beginning-of-block (- num)))
1588 (let ((f90-smart-end (if f90-smart-end 'no-blink)) ; for final match-end
1589 (case-fold-search t)
1591 start-list start-this start-type start-label end-type end-label)
1592 (end-of-line) ; probably want this
1593 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1595 (skip-chars-forward " \t0-9")
1596 (cond ((or (f90-in-string) (f90-in-comment)))
1600 (f90-looking-at-select-case)
1601 (f90-looking-at-type-like)
1602 (f90-looking-at-associate)
1603 (f90-looking-at-program-block-start)
1604 (f90-looking-at-if-then)
1605 (f90-looking-at-where-or-forall)))
1606 (setq start-list (cons start-this start-list) ; not add-to-list!
1608 ((looking-at (concat "end[ \t]*" f90-blocks-re
1609 "[ \t]*\\(\\sw+\\)?"))
1610 (setq end-type (match-string 1)
1611 end-label (match-string 2)
1613 ;; Check any internal blocks.
1615 (setq start-this (car start-list)
1616 start-list (cdr start-list)
1617 start-type (car start-this)
1618 start-label (cadr start-this))
1619 (or (f90-equal-symbols start-type end-type)
1620 (error "End type `%s' does not match start type `%s'"
1621 end-type start-type))
1622 (or (f90-equal-symbols start-label end-label)
1623 (error "End label `%s' does not match start label `%s'"
1624 end-label start-label)))))
1626 (if (> count 0) (error "Missing block end"))
1627 ;; Check outermost block.
1631 (skip-chars-forward " \t0-9")
1634 (defun f90-beginning-of-block (&optional num)
1635 "Move point backwards to the start of the current code block.
1636 With optional argument NUM, go backward that many balanced blocks.
1637 If NUM is negative, go forward to the end of a block.
1638 Checks for consistency of block types and labels (if present).
1639 Does not check the outermost block, because it may be incomplete.
1640 Interactively, pushes mark before moving point."
1642 (if (called-interactively-p 'any) (push-mark (point) t))
1643 (and num (< num 0) (f90-end-of-block (- num)))
1644 (let ((case-fold-search t)
1646 end-list end-this end-type end-label
1647 start-this start-type start-label)
1648 (beginning-of-line) ; probably want this
1649 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1651 (skip-chars-forward " \t0-9")
1652 (cond ((or (f90-in-string) (f90-in-comment)))
1653 ((looking-at (concat "end[ \t]*" f90-blocks-re
1654 "[ \t]*\\(\\sw+\\)?"))
1655 (setq end-list (cons (list (match-string 1) (match-string 2))
1661 (f90-looking-at-select-case)
1662 (f90-looking-at-type-like)
1663 (f90-looking-at-associate)
1664 (f90-looking-at-program-block-start)
1665 (f90-looking-at-if-then)
1666 (f90-looking-at-where-or-forall)))
1667 (setq start-type (car start-this)
1668 start-label (cadr start-this)
1670 ;; Check any internal blocks.
1672 (setq end-this (car end-list)
1673 end-list (cdr end-list)
1674 end-type (car end-this)
1675 end-label (cadr end-this))
1676 (or (f90-equal-symbols start-type end-type)
1677 (error "Start type `%s' does not match end type `%s'"
1678 start-type end-type))
1679 (or (f90-equal-symbols start-label end-label)
1680 (error "Start label `%s' does not match end label `%s'"
1681 start-label end-label))))))
1682 ;; Includes an un-named main program block.
1683 (if (> count 0) (error "Missing block start"))))
1685 (defun f90-next-block (&optional num)
1686 "Move point forward to the next end or start of a code block.
1687 With optional argument NUM, go forward that many blocks.
1688 If NUM is negative, go backwards.
1689 A block is a subroutine, if-endif, etc."
1691 (let ((case-fold-search t)
1692 (count (if num (abs num) 1)))
1693 (while (and (> count 0)
1694 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1695 (re-search-backward f90-blocks-re nil 'move)))
1697 (skip-chars-forward " \t0-9")
1698 (cond ((or (f90-in-string) (f90-in-comment)))
1700 (looking-at "end[ \t]*")
1702 (f90-looking-at-select-case)
1703 (f90-looking-at-type-like)
1704 (f90-looking-at-associate)
1705 (f90-looking-at-program-block-start)
1706 (f90-looking-at-if-then)
1707 (f90-looking-at-where-or-forall))
1708 (setq count (1- count))))
1709 (if (> num 0) (end-of-line)
1710 (beginning-of-line)))))
1713 (defun f90-previous-block (&optional num)
1714 "Move point backward to the previous end or start of a code block.
1715 With optional argument NUM, go backward that many blocks.
1716 If NUM is negative, go forwards.
1717 A block is a subroutine, if-endif, etc."
1719 (f90-next-block (- (or num 1))))
1722 (defun f90-mark-subprogram ()
1723 "Put mark at end of F90 subprogram, point at beginning, push mark."
1725 (let ((pos (point)) program)
1726 (f90-end-of-subprogram)
1729 (setq program (f90-beginning-of-subprogram))
1730 (if (featurep 'xemacs)
1731 (zmacs-activate-region)
1733 deactivate-mark nil))
1736 (defun f90-comment-region (beg-region end-region)
1737 "Comment/uncomment every line in the region.
1738 Insert the variable `f90-comment-region' at the start of every line
1739 in the region, or, if already present, remove it."
1741 (let ((end (copy-marker end-region)))
1742 (goto-char beg-region)
1744 (if (looking-at (regexp-quote f90-comment-region))
1745 (delete-region (point) (match-end 0))
1746 (insert f90-comment-region))
1747 (while (and (zerop (forward-line 1))
1749 (if (looking-at (regexp-quote f90-comment-region))
1750 (delete-region (point) (match-end 0))
1751 (insert f90-comment-region)))
1752 (set-marker end nil)))
1754 (defun f90-indent-line (&optional no-update)
1755 "Indent current line as F90 code.
1756 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1759 (let ((case-fold-search t)
1760 (pos (point-marker))
1761 indent no-line-number)
1762 (beginning-of-line) ; digits after & \n are not line-nos
1763 (if (not (save-excursion (and (f90-previous-statement)
1764 (f90-line-continued))))
1765 (f90-indent-line-no)
1766 (setq no-line-number t)
1767 (skip-chars-forward " \t"))
1768 (if (looking-at "!")
1769 (setq indent (f90-comment-indent))
1770 (and f90-smart-end (looking-at "end")
1772 (setq indent (f90-calculate-indent)))
1773 (or (= indent (current-column))
1774 (f90-indent-to indent no-line-number))
1775 ;; If initial point was within line's indentation,
1776 ;; position after the indentation. Else stay at same point in text.
1777 (and (< (point) pos)
1779 (if auto-fill-function
1780 ;; GM NO-UPDATE not honoured, since this calls f90-update-line.
1782 (or no-update (f90-update-line)))
1783 (set-marker pos nil)))
1785 (defun f90-indent-new-line ()
1786 "Re-indent current line, insert a newline and indent the newline.
1787 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1788 If run in the middle of a line, the line is not broken."
1790 (if abbrev-mode (expand-abbrev))
1791 (beginning-of-line) ; reindent where likely to be needed
1792 (f90-indent-line) ; calls indent-line-no, update-line
1794 (delete-horizontal-space) ; destroy trailing whitespace
1795 (let ((string (f90-in-string))
1796 (cont (f90-line-continued)))
1797 (and string (not cont) (insert "&"))
1799 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
1800 (f90-indent-line 'no-update)) ; nothing to update
1803 ;; TODO not add spaces to empty lines at the start.
1804 ;; Why is second line getting extra indent over first?
1805 (defun f90-indent-region (beg-region end-region)
1806 "Indent every line in region by forward parsing."
1808 (let ((end-region-mark (copy-marker end-region))
1809 (save-point (point-marker))
1810 (case-fold-search t)
1811 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
1812 (goto-char beg-region)
1813 ;; First find a line which is not a continuation line or comment.
1815 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1816 (progn (f90-indent-line 'no-update)
1817 (zerop (forward-line 1)))
1818 (< (point) end-region-mark)))
1819 (setq cont (f90-present-statement-cont))
1820 (while (and (memq cont '(middle end))
1821 (f90-previous-statement))
1822 (setq cont (f90-present-statement-cont)))
1823 ;; Process present line for beginning of block.
1824 (setq f90-cache-position (point))
1825 (f90-indent-line 'no-update)
1826 (setq ind-lev (f90-current-indentation)
1829 (skip-chars-forward " \t0-9")
1831 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1832 ((or (setq struct (f90-looking-at-if-then))
1833 (setq struct (f90-looking-at-select-case))
1834 (setq struct (f90-looking-at-where-or-forall))
1835 (looking-at f90-else-like-re))
1837 ((setq struct (f90-looking-at-type-like))
1839 ((setq struct (f90-looking-at-associate))
1840 f90-associate-indent)
1841 ((or (setq struct (f90-looking-at-program-block-start))
1842 (looking-at "contains[ \t]*\\($\\|!\\)"))
1843 f90-program-indent)))
1844 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1845 (if struct (setq block-list (cons struct block-list)))
1846 (while (and (f90-line-continued) (zerop (forward-line 1))
1847 (< (point) end-region-mark))
1848 (if (looking-at "[ \t]*!")
1849 (f90-indent-to (f90-comment-indent))
1850 (or (= (current-indentation)
1851 (+ ind-curr f90-continuation-indent))
1852 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1853 ;; Process all following lines.
1854 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1856 (f90-indent-line-no)
1857 (setq f90-cache-position (point))
1858 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1859 ((looking-at "[ \t]*#") (setq ind-curr 0))
1860 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1861 ((f90-no-block-limit) (setq ind-curr ind-lev))
1862 ((looking-at f90-else-like-re) (setq ind-curr
1863 (- ind-lev f90-if-indent)))
1864 ((looking-at "contains[ \t]*\\($\\|!\\)")
1865 (setq ind-curr (- ind-lev f90-program-indent)))
1867 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1868 ((or (setq struct (f90-looking-at-if-then))
1869 (setq struct (f90-looking-at-select-case))
1870 (setq struct (f90-looking-at-where-or-forall)))
1872 ((setq struct (f90-looking-at-type-like))
1874 ((setq struct (f90-looking-at-associate))
1875 f90-associate-indent)
1876 ((setq struct (f90-looking-at-program-block-start))
1877 f90-program-indent)))
1878 (setq ind-curr ind-lev)
1879 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1880 (setq block-list (cons struct block-list)))
1881 ((setq end-struct (f90-looking-at-program-block-end))
1882 (setq beg-struct (car block-list)
1883 block-list (cdr block-list))
1886 (f90-block-match (car beg-struct) (cadr beg-struct)
1887 (car end-struct) (cadr end-struct))))
1889 (cond ((looking-at f90-end-if-re) f90-if-indent)
1890 ((looking-at "end[ \t]*do\\>") f90-do-indent)
1891 ((looking-at f90-end-type-re) f90-type-indent)
1892 ((looking-at f90-end-associate-re)
1893 f90-associate-indent)
1894 ((f90-looking-at-program-block-end)
1895 f90-program-indent)))
1896 (if ind-b (setq ind-lev (- ind-lev ind-b)))
1897 (setq ind-curr ind-lev))
1898 (t (setq ind-curr ind-lev)))
1899 ;; Do the indentation if necessary.
1900 (or (= ind-curr (current-column))
1901 (f90-indent-to ind-curr))
1902 (while (and (f90-line-continued) (zerop (forward-line 1))
1903 (< (point) end-region-mark))
1904 (if (looking-at "[ \t]*!")
1905 (f90-indent-to (f90-comment-indent))
1906 (or (= (current-indentation)
1907 (+ ind-curr f90-continuation-indent))
1909 (+ ind-curr f90-continuation-indent) 'no-line-no)))))
1910 ;; Restore point, etc.
1911 (setq f90-cache-position nil)
1912 (goto-char save-point)
1913 (set-marker end-region-mark nil)
1914 (set-marker save-point nil)
1915 (if (featurep 'xemacs)
1916 (zmacs-deactivate-region)
1917 (deactivate-mark))))
1919 (defun f90-indent-subprogram ()
1920 "Properly indent the subprogram containing point."
1923 (let ((program (f90-mark-subprogram)))
1926 (message "Indenting %s %s..."
1927 (car program) (cadr program))
1928 (indent-region (point) (mark) nil)
1929 (message "Indenting %s %s...done"
1930 (car program) (cadr program)))
1931 (message "Indenting the whole file...")
1932 (indent-region (point) (mark) nil)
1933 (message "Indenting the whole file...done")))))
1935 (defun f90-break-line (&optional no-update)
1936 "Break line at point, insert continuation marker(s) and indent.
1937 Unless in a string or comment, or if the optional argument NO-UPDATE
1938 is non-nil, call `f90-update-line' after inserting the continuation marker."
1940 (cond ((f90-in-string)
1943 (delete-horizontal-space 'backwards) ; remove trailing whitespace
1944 (insert "\n" (f90-get-present-comment-type)))
1946 (or no-update (f90-update-line))
1948 ;; FIXME also need leading ampersand if split lexical token (eg ==).
1949 ;; Or respect f90-no-break-re.
1950 (if f90-beginning-ampersand (insert "&"))))
1951 (indent-according-to-mode))
1953 (defun f90-find-breakpoint ()
1954 "From `fill-column', search backward for break-delimiter."
1955 (re-search-backward f90-break-delimiters (line-beginning-position))
1956 (if (not f90-break-before-delimiters)
1957 (forward-char (if (looking-at f90-no-break-re) 2 1))
1959 (or (looking-at f90-no-break-re)
1962 (defun f90-do-auto-fill ()
1963 "Break line if non-white characters beyond `fill-column'.
1964 Update keyword case first."
1966 ;; Break line before or after last delimiter (non-word char) if
1967 ;; position is beyond fill-column.
1968 ;; Will not break **, //, or => (as specified by f90-no-break-re).
1970 ;; Need this for `f90-electric-insert' and other f90- callers.
1971 (unless (and (boundp 'comment-auto-fill-only-comments)
1972 comment-auto-fill-only-comments
1973 (not (f90-in-comment)))
1974 (while (> (current-column) fill-column)
1975 (let ((pos-mark (point-marker)))
1976 (move-to-column fill-column)
1977 (or (f90-in-string) (f90-find-breakpoint))
1979 (goto-char pos-mark)
1980 (set-marker pos-mark nil)))))
1982 (defun f90-join-lines (&optional arg)
1983 "Join current line to previous, fix whitespace, continuation, comments.
1984 With optional argument ARG, join current line to following line.
1985 Like `join-line', but handles F90 syntax."
1988 (if arg (forward-line 1))
1989 (when (eq (preceding-char) ?\n)
1990 (skip-chars-forward " \t")
1991 (if (looking-at "\&") (delete-char 1))
1993 (delete-region (point) (1- (point)))
1994 (skip-chars-backward " \t")
1995 (and (eq (preceding-char) ?&) (delete-char -1))
1996 (and (f90-in-comment)
1997 (looking-at "[ \t]*!+")
2000 (fixup-whitespace))))
2002 (defun f90-fill-region (beg-region end-region)
2003 "Fill every line in region by forward parsing. Join lines if possible."
2005 (let ((end-region-mark (copy-marker end-region))
2007 f90-smart-end f90-auto-keyword-case auto-fill-function)
2008 (goto-char beg-region)
2010 ;; Join as much as possible.
2013 (skip-chars-backward " \t")
2014 (eq (preceding-char) ?&))
2015 (f90-join-lines 'forward))
2016 ;; Chop the line if necessary.
2017 (while (> (save-excursion (end-of-line) (current-column))
2019 (move-to-column fill-column)
2020 (f90-find-breakpoint)
2021 (f90-break-line 'no-update))
2022 (setq go-on (and (< (point) end-region-mark)
2023 (zerop (forward-line 1)))
2024 f90-cache-position (point)))
2025 (setq f90-cache-position nil)
2026 (set-marker end-region-mark nil)
2027 (if (featurep 'xemacs)
2028 (zmacs-deactivate-region)
2029 (deactivate-mark))))
2031 (defun f90-block-match (beg-block beg-name end-block end-name)
2032 "Match end-struct with beg-struct and complete end-block if possible.
2033 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
2034 BEG-NAME is the block start name (may be nil).
2035 END-BLOCK is the type of block as indicated at the end (may be nil).
2036 END-NAME is the block end name (may be nil).
2037 Leave point at the end of line."
2038 ;; Hack to deal with the case when this is called from
2039 ;; f90-indent-region on a program block without an explicit PROGRAM
2040 ;; statement at the start. Should really be an error (?).
2041 (or beg-block (setq beg-block "program"))
2042 (search-forward "end" (line-end-position))
2044 (if (and end-block (f90-equal-symbols beg-block end-block))
2045 (search-forward end-block)
2048 (message "END %s does not match %s." end-block beg-block)
2050 (throw 'no-match nil))
2051 (message "Inserting %s." beg-block)
2052 (insert (concat " " beg-block))))
2053 (if (f90-equal-symbols beg-name end-name)
2054 (and end-name (search-forward end-name))
2055 (cond ((and beg-name (not end-name))
2056 (message "Inserting %s." beg-name)
2057 (insert (concat " " beg-name)))
2058 ((and beg-name end-name)
2059 (message "Replacing %s with %s." end-name beg-name)
2060 (search-forward end-name)
2061 (replace-match beg-name))
2062 ((and (not beg-name) end-name)
2063 (message "Deleting %s." end-name)
2064 (search-forward end-name)
2065 (replace-match ""))))
2066 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
2068 (defun f90-match-end ()
2069 "From an end block statement, find the corresponding block and name."
2072 (top-of-window (window-start))
2074 (case-fold-search t)
2075 matching-beg beg-name end-name beg-block end-block end-struct)
2076 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
2077 (setq end-struct (f90-looking-at-program-block-end)))
2078 (setq end-block (car end-struct)
2079 end-name (cadr end-struct))
2082 (while (and (> count 0)
2083 (not (= (line-beginning-position) (point-min))))
2084 (re-search-backward f90-blocks-re nil 'move)
2086 ;; GM not a line number if continued line.
2087 ;;; (skip-chars-forward " \t")
2088 ;;; (skip-chars-forward "0-9")
2089 (skip-chars-forward " \t0-9")
2090 (cond ((or (f90-in-string) (f90-in-comment)))
2094 (f90-looking-at-if-then)
2095 (f90-looking-at-where-or-forall)
2096 (f90-looking-at-select-case)
2097 (f90-looking-at-type-like)
2098 (f90-looking-at-associate)
2099 (f90-looking-at-program-block-start)
2100 ;; Interpret a single END without a block
2101 ;; start to be the END of a program block
2102 ;; without an initial PROGRAM line.
2103 (if (= (line-beginning-position) (point-min))
2105 (setq count (1- count)))
2106 ((looking-at (concat "end[ \t]*" f90-blocks-re))
2107 (setq count (1+ count)))))
2109 (message "No matching beginning.")
2111 (if (eq f90-smart-end 'blink)
2112 (if (< (point) top-of-window)
2113 (message "Matches %s: %s"
2116 (line-beginning-position)
2117 (line-end-position)))
2118 (sit-for blink-matching-delay)))
2119 (setq beg-block (car matching-beg)
2120 beg-name (cadr matching-beg))
2121 (goto-char end-point)
2123 (f90-block-match beg-block beg-name end-block end-name))))))
2125 (defun f90-insert-end ()
2126 "Insert a complete end statement matching beginning of present block."
2128 (let ((f90-smart-end (or f90-smart-end 'blink)))
2130 (f90-indent-new-line)))
2132 ;; Abbrevs and keywords.
2134 (defun f90-abbrev-start ()
2135 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
2136 Any other key combination is executed normally."
2138 (insert last-command-event)
2140 (if (fboundp 'next-command-event) ; XEmacs
2141 (setq event (next-command-event)
2142 char (and (fboundp 'event-to-character)
2143 (event-to-character event)))
2144 (setq event (read-event)
2146 ;; Insert char if not equal to `?', or if abbrev-mode is off.
2147 (if (and abbrev-mode (memq char (list ?? help-char)))
2149 (setq unread-command-events (list event)))))
2151 (defun f90-abbrev-help ()
2152 "List the currently defined abbrevs in F90 mode."
2154 (message "Listing abbrev table...")
2155 (display-buffer (f90-prepare-abbrev-list-buffer))
2156 (message "Listing abbrev table...done"))
2158 (defun f90-prepare-abbrev-list-buffer ()
2159 "Create a buffer listing the F90 mode abbreviations."
2160 (with-current-buffer (get-buffer-create "*Abbrevs*")
2162 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
2163 (goto-char (point-min))
2164 (set-buffer-modified-p nil)
2165 (edit-abbrevs-mode))
2166 (get-buffer-create "*Abbrevs*"))
2168 (defun f90-upcase-keywords ()
2169 "Upcase all F90 keywords in the buffer."
2171 (f90-change-keywords 'upcase-word))
2173 (defun f90-capitalize-keywords ()
2174 "Capitalize all F90 keywords in the buffer."
2176 (f90-change-keywords 'capitalize-word))
2178 (defun f90-downcase-keywords ()
2179 "Downcase all F90 keywords in the buffer."
2181 (f90-change-keywords 'downcase-word))
2183 (defun f90-upcase-region-keywords (beg end)
2184 "Upcase all F90 keywords in the region."
2186 (f90-change-keywords 'upcase-word beg end))
2188 (defun f90-capitalize-region-keywords (beg end)
2189 "Capitalize all F90 keywords in the region."
2191 (f90-change-keywords 'capitalize-word beg end))
2193 (defun f90-downcase-region-keywords (beg end)
2194 "Downcase all F90 keywords in the region."
2196 (f90-change-keywords 'downcase-word beg end))
2198 ;; Change the keywords according to argument.
2199 (defun f90-change-keywords (change-word &optional beg end)
2200 "Change the case of F90 keywords in the region (if specified) or buffer.
2201 CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
2203 (setq beg (or beg (point-min))
2204 end (or end (point-max)))
2207 f90-keywords-re "\\|" f90-procedures-re "\\|"
2208 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
2209 (ref-point (point-min))
2210 (modified (buffer-modified-p))
2211 state saveword back-point)
2214 (while (re-search-forward keyword-re end t)
2216 (setq state (parse-partial-sexp ref-point (point)))
2217 (or (nth 3 state) (nth 4 state)
2218 ;; GM f90-directive-comment-re?
2219 (save-excursion ; check for cpp directive
2221 (skip-chars-forward " \t0-9")
2223 (setq ref-point (point)
2224 back-point (save-excursion (backward-word 1) (point))
2225 saveword (buffer-substring back-point ref-point))
2226 (funcall change-word -1)
2227 (or (string= saveword (buffer-substring back-point ref-point))
2228 (setq modified t))))
2229 (or modified (restore-buffer-modified-p nil))))))
2232 (defun f90-current-defun ()
2233 "Function to use for `add-log-current-defun-function' in F90 mode."
2235 (nth 1 (f90-beginning-of-subprogram))))
2237 (defun f90-backslash-not-special (&optional all)
2238 "Make the backslash character (\\) be non-special in the current buffer.
2239 With optional argument ALL, change the default for all present
2240 and future F90 buffers. F90 mode normally treats backslash as an
2242 (or (derived-mode-p 'f90-mode)
2243 (error "This function should only be used in F90 buffers"))
2244 (when (equal (char-syntax ?\\ ) ?\\ )
2245 (or all (set-syntax-table (copy-syntax-table (syntax-table))))
2246 (modify-syntax-entry ?\\ ".")))
2253 ;; lexical-binding: t
2256 ;;; f90.el ends here