Improve treatment of Fortran's "class default"
[emacs.git] / lisp / progmodes / f90.el
blob72156288ebac45c738b47eced8b36e10bcffb6be
1 ;;; f90.el --- Fortran-90 mode (free format) -*- lexical-binding: t -*-
3 ;; Copyright (C) 1995-1997, 2000-2017 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).
126 ;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
128 ;; List of user commands
129 ;; f90-previous-statement f90-next-statement
130 ;; f90-beginning-of-subprogram f90-end-of-subprogram f90-mark-subprogram
131 ;; f90-comment-region
132 ;; f90-indent-line f90-indent-new-line
133 ;; f90-indent-region (can be called by calling indent-region)
134 ;; f90-indent-subprogram
135 ;; f90-break-line f90-join-lines
136 ;; f90-fill-region
137 ;; f90-insert-end
138 ;; f90-upcase-keywords f90-upcase-region-keywords
139 ;; f90-downcase-keywords f90-downcase-region-keywords
140 ;; f90-capitalize-keywords f90-capitalize-region-keywords
141 ;; f90-add-imenu-menu
142 ;; f90-font-lock-1, f90-font-lock-2, f90-font-lock-3, f90-font-lock-4
144 ;; Original author's thanks
145 ;; Thanks to all the people who have tested the mode. Special thanks to Jens
146 ;; Bloch Helmers for encouraging me to write this code, for creative
147 ;; suggestions as well as for the lists of hpf-commands.
148 ;; Also thanks to the authors of the fortran and pascal modes, on which some
149 ;; of this code is built.
151 ;;; Code:
153 ;; TODO
154 ;; 1. Any missing F2003 syntax?
155 ;; 2. Have "f90-mode" just recognize F90 syntax, then derived modes
156 ;; "f95-mode", "f2003-mode" for the language revisions.
157 ;; 3. Support for align.
158 ;; Font-locking:
159 ;; 1. OpenMP, OpenMPI?, preprocessor highlighting.
160 ;; 2. integer_name = 1
161 ;; 3. Labels for "else" statements (F2003)?
163 (defvar comment-auto-fill-only-comments)
164 (defvar font-lock-keywords)
166 ;; User options
168 (defgroup f90 nil
169 "Major mode for editing free format Fortran 90,95 code."
170 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
171 :group 'languages)
173 (defgroup f90-indent nil
174 "Indentation in free format Fortran."
175 :prefix "f90-"
176 :group 'f90)
179 (defcustom f90-do-indent 3
180 "Extra indentation applied to DO blocks."
181 :type 'integer
182 :safe 'integerp
183 :group 'f90-indent)
185 (defcustom f90-if-indent 3
186 "Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
187 :type 'integer
188 :safe 'integerp
189 :group 'f90-indent)
191 (defcustom f90-type-indent 3
192 "Extra indentation applied to TYPE, ENUM, INTERFACE and BLOCK DATA blocks."
193 :type 'integer
194 :safe 'integerp
195 :group 'f90-indent)
197 (defcustom f90-program-indent 2
198 "Extra indentation applied to PROGRAM, MODULE, SUBROUTINE, FUNCTION blocks."
199 :type 'integer
200 :safe 'integerp
201 :group 'f90-indent)
203 (defcustom f90-associate-indent 2
204 "Extra indentation applied to ASSOCIATE blocks."
205 :type 'integer
206 :safe 'integerp
207 :group 'f90-indent
208 :version "23.1")
210 (defcustom f90-critical-indent 2
211 "Extra indentation applied to BLOCK, CRITICAL blocks."
212 :type 'integer
213 :safe 'integerp
214 :group 'f90-indent
215 :version "24.1")
217 (defcustom f90-continuation-indent 5
218 "Extra indentation applied to continuation lines."
219 :type 'integer
220 :safe 'integerp
221 :group 'f90-indent)
223 (defcustom f90-comment-region "!!$"
224 "String inserted by \\[f90-comment-region] at start of each line in region."
225 :type 'string
226 :safe 'stringp
227 :group 'f90-indent)
229 (defcustom f90-indented-comment-re "!"
230 "Regexp matching comments to indent as code."
231 :type 'regexp
232 :safe 'stringp
233 :group 'f90-indent)
235 ;; Should we add ^# to this? That's not really a comment.
236 (defcustom f90-directive-comment-re "!hpf\\$"
237 "Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
238 :type 'regexp
239 :safe 'stringp
240 :group 'f90-indent)
242 (defcustom f90-beginning-ampersand t
243 "Non-nil gives automatic insertion of `&' at start of continuation line."
244 :type 'boolean
245 :safe 'booleanp
246 :group 'f90)
248 (defcustom f90-smart-end 'blink
249 "Qualification of END statements according to the matching block start.
250 For example, change the END that closes an IF block to END IF.
251 If the block has a label, add it as well (unless `f90-smart-end-names'
252 says not to). Allowed values are `blink', `no-blink', and nil. If nil,
253 nothing is done. The other two settings have the same effect, but `blink'
254 additionally blinks the cursor to the start of the block."
255 :type '(choice (const blink) (const no-blink) (const nil))
256 :safe (lambda (value) (memq value '(blink no-blink nil)))
257 :group 'f90)
259 ;; Optional: program, module, type, function, subroutine
260 ;; Not optional: block data?, forall, if, select case/type, associate, do,
261 ;; where, interface, critical
262 ;; No labels: enum
263 (defcustom f90-smart-end-names t
264 "Whether completion of END statements should insert optional block names.
265 For example, when closing a \"PROGRAM PROGNAME\" block, \"PROGNAME\" is
266 optional in the \"END PROGRAM\" statement. The same is true for modules,
267 functions, subroutines, and types. Some people prefer to omit the name
268 from the END statement, since it makes it easier to change the name.
270 This does not apply to named DO, IF, etc. blocks. If such blocks
271 start with a label, they must end with one.
273 If an end statement has a name that does not match the start, it is always
274 corrected, regardless of the value of this variable."
275 :type 'boolean
276 :safe 'booleanp
277 :group 'f90
278 :version "24.4")
280 (defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
281 "Regexp matching delimiter characters at which lines may be broken.
282 There are some common two-character tokens where one or more of
283 the members matches this regexp. Although Fortran allows breaks
284 within lexical tokens (provided the next line has a beginning ampersand),
285 the constant `f90-no-break-re' ensures that such tokens are not split."
286 :type 'regexp
287 :safe 'stringp
288 :group 'f90)
290 (defcustom f90-break-before-delimiters t
291 "Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
292 :type 'boolean
293 :safe 'booleanp
294 :group 'f90)
296 (defcustom f90-auto-keyword-case nil
297 "Automatic case conversion of keywords.
298 The options are `downcase-word', `upcase-word', `capitalize-word' and nil."
299 :type '(choice (const downcase-word) (const upcase-word)
300 (const capitalize-word) (const nil))
301 :safe (lambda (value) (memq value '(downcase-word
302 capitalize-word upcase-word nil)))
303 :group 'f90)
305 (defcustom f90-leave-line-no nil
306 "If non-nil, line numbers are not left justified."
307 :type 'boolean
308 :safe 'booleanp
309 :group 'f90)
311 (defcustom f90-mode-hook nil
312 "Hook run when entering F90 mode."
313 :type 'hook
314 ;; Not the only safe options, but some common ones.
315 :safe (lambda (value) (member value '((f90-add-imenu-menu) nil)))
316 :options '(f90-add-imenu-menu)
317 :group 'f90)
319 ;; User options end here.
321 (defconst f90-keywords-re
322 (concat
323 "\\_<"
324 (regexp-opt '("allocatable" "allocate" "assign" "assignment" "backspace"
325 "block" "call" "case" "character" "close" "common" "complex"
326 "contains" "continue" "cycle" "data" "deallocate"
327 "dimension" "do" "double" "else" "elseif" "elsewhere" "end"
328 "enddo" "endfile" "endif" "entry" "equivalence" "exit"
329 "external" "forall" "format" "function" "goto" "if"
330 "implicit" "include" "inquire" "integer" "intent"
331 "interface" "intrinsic" "logical" "module" "namelist" "none"
332 "nullify" "only" "open" "operator" "optional" "parameter"
333 "pause" "pointer" "precision" "print" "private" "procedure"
334 "program" "public" "read" "real" "recursive" "result" "return"
335 "rewind" "save" "select" "sequence" "stop" "subroutine"
336 "target" "then" "type" "use" "where" "while" "write"
337 ;; F95 keywords.
338 "elemental" "pure"
339 ;; F2003
340 "abstract" "associate" "asynchronous" "bind" "class"
341 "deferred" "enum" "enumerator" "extends" "extends_type_of"
342 "final" "generic" "import" "non_intrinsic" "non_overridable"
343 "nopass" "pass" "protected" "same_type_as" "value" "volatile"
344 ;; F2008.
345 ;; FIXME f90-change-keywords does not work right if
346 ;; there are spaces.
347 "contiguous" "submodule" "concurrent" "codimension"
348 "sync all" "sync memory" "critical" "image_index" "error stop"
350 "\\_>")
351 "Regexp used by the function `f90-change-keywords'.")
353 (defconst f90-keywords-level-3-re
354 (concat
355 "\\_<"
356 (regexp-opt
357 '("allocatable" "allocate" "assign" "assignment" "backspace"
358 "close" "deallocate" "dimension" "endfile" "entry" "equivalence"
359 "external" "inquire" "intent" "intrinsic" "nullify" "only" "open"
360 ;; FIXME operator and assignment should be F2003 procedures?
361 "operator" "optional" "parameter" "pause" "pointer" "print" "private"
362 "public" "read" "recursive" "result" "rewind" "save" "select"
363 "sequence" "target" "write"
364 ;; F95 keywords.
365 "elemental" "pure"
366 ;; F2003. asynchronous separate.
367 "abstract" "deferred" "import" "final" "non_intrinsic" "non_overridable"
368 "nopass" "pass" "protected" "value" "volatile"
369 ;; F2008.
370 ;; "concurrent" is only in the sense of "do [,] concurrent", but given
371 ;; the [,] it's simpler to just do every instance (cf "do while").
372 "contiguous" "concurrent" "codimension" "sync all" "sync memory"
374 "\\_>")
375 "Keyword-regexp for font-lock level >= 3.")
377 (defconst f90-procedures-re
378 (concat "\\_<"
379 (regexp-opt
380 '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
381 "all" "allocated" "anint" "any" "asin" "associated"
382 "atan" "atan2" "bit_size" "btest" "ceiling" "char" "cmplx"
383 "conjg" "cos" "cosh" "count" "cshift" "date_and_time" "dble"
384 "digits" "dim" "dot_product" "dprod" "eoshift" "epsilon"
385 "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
386 "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior"
387 "ishft" "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt"
388 "lle" "llt" "log" "log10" "logical" "matmul" "max"
389 "maxexponent" "maxloc" "maxval" "merge" "min" "minexponent"
390 "minloc" "minval" "mod" "modulo" "mvbits" "nearest" "nint"
391 "not" "pack" "precision" "present" "product" "radix"
392 ;; Real is taken out here to avoid highlighting declarations.
393 "random_number" "random_seed" "range" ;; "real"
394 "repeat" "reshape" "rrspacing" "scale" "scan"
395 "selected_int_kind" "selected_real_kind" "set_exponent"
396 "shape" "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt"
397 "sum" "system_clock" "tan" "tanh" "tiny" "transfer"
398 "transpose" "trim" "ubound" "unpack" "verify"
399 ;; F95 intrinsic functions.
400 "null" "cpu_time"
401 ;; F2003.
402 "move_alloc" "command_argument_count" "get_command"
403 "get_command_argument" "get_environment_variable"
404 "selected_char_kind" "wait" "flush" "new_line"
405 "extends" "extends_type_of" "same_type_as" "bind"
406 ;; F2003 ieee_arithmetic intrinsic module.
407 "ieee_support_underflow_control" "ieee_get_underflow_mode"
408 "ieee_set_underflow_mode"
409 ;; F2003 iso_c_binding intrinsic module.
410 "c_loc" "c_funloc" "c_associated" "c_f_pointer"
411 "c_f_procpointer"
412 ;; F2008.
413 "bge" "bgt" "ble" "blt" "dshiftl" "dshiftr" "leadz" "popcnt"
414 "poppar" "trailz" "maskl" "maskr" "shifta" "shiftl" "shiftr"
415 "merge_bits" "iall" "iany" "iparity" "storage_size"
416 "bessel_j0" "bessel_j1" "bessel_jn"
417 "bessel_y0" "bessel_y1" "bessel_yn"
418 "erf" "erfc" "erfc_scaled" "gamma" "hypot" "log_gamma"
419 "norm2" "parity" "findloc" "is_contiguous"
420 "sync images" "lock" "unlock" "image_index"
421 "lcobound" "ucobound" "num_images" "this_image"
422 "acosh" "asinh" "atanh"
423 "atomic_define" "atomic_ref" "execute_command_line"
424 ;; F2008 iso_fortran_env module.
425 "compiler_options" "compiler_version"
426 ;; F2008 iso_c_binding module.
427 "c_sizeof"
428 ) t)
429 ;; A left parenthesis to avoid highlighting non-procedures.
430 "[ \t]*(")
431 "Regexp whose first part matches F90 intrinsic procedures.")
433 (defconst f90-operators-re
434 (concat "\\."
435 (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
436 "neqv" "not" "or" "true") t)
437 "\\.")
438 "Regexp matching intrinsic operators.")
440 (defconst f90-hpf-keywords-re
441 (concat
442 "\\_<"
443 (regexp-opt
444 ;; Intrinsic procedures.
445 '("all_prefix" "all_scatter" "all_suffix" "any_prefix"
446 "any_scatter" "any_suffix" "copy_prefix" "copy_scatter"
447 "copy_suffix" "count_prefix" "count_scatter" "count_suffix"
448 "grade_down" "grade_up"
449 "hpf_alignment" "hpf_distribution" "hpf_template" "iall" "iall_prefix"
450 "iall_scatter" "iall_suffix" "iany" "iany_prefix" "iany_scatter"
451 "iany_suffix" "ilen" "iparity" "iparity_prefix"
452 "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
453 "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter"
454 "minval_suffix" "number_of_processors" "parity"
455 "parity_prefix" "parity_scatter" "parity_suffix" "popcnt" "poppar"
456 "processors_shape" "product_prefix" "product_scatter"
457 "product_suffix" "sum_prefix" "sum_scatter" "sum_suffix"
458 ;; Directives.
459 "align" "distribute" "dynamic" "independent" "inherit" "processors"
460 "realign" "redistribute" "template"
461 ;; Keywords.
462 "block" "cyclic" "extrinsic" "new" "onto" "pure" "with"))
463 "\\_>")
464 "Regexp for all HPF keywords, procedures and directives.")
466 (defconst f90-constants-re
467 (concat
468 "\\_<"
469 (regexp-opt '( ;; F2003 iso_fortran_env constants.
470 "iso_fortran_env"
471 "input_unit" "output_unit" "error_unit"
472 "iostat_end" "iostat_eor"
473 "numeric_storage_size" "character_storage_size"
474 "file_storage_size"
475 ;; F2003 iso_c_binding constants.
476 "iso_c_binding"
477 "c_int" "c_short" "c_long" "c_long_long" "c_signed_char"
478 "c_size_t"
479 "c_int8_t" "c_int16_t" "c_int32_t" "c_int64_t"
480 "c_int_least8_t" "c_int_least16_t" "c_int_least32_t"
481 "c_int_least64_t"
482 "c_int_fast8_t" "c_int_fast16_t" "c_int_fast32_t"
483 "c_int_fast64_t"
484 "c_intmax_t" "c_intptr_t"
485 "c_float" "c_double" "c_long_double"
486 "c_float_complex" "c_double_complex" "c_long_double_complex"
487 "c_bool" "c_char"
488 "c_null_char" "c_alert" "c_backspace" "c_form_feed"
489 "c_new_line" "c_carriage_return" "c_horizontal_tab"
490 "c_vertical_tab"
491 "c_ptr" "c_funptr" "c_null_ptr" "c_null_funptr"
492 "ieee_exceptions"
493 "ieee_arithmetic"
494 "ieee_features"
495 ;; F2008 iso_fortran_env constants.
496 "character_kinds" "int8" "int16" "int32" "int64"
497 "integer_kinds" "iostat_inquire_internal_unit"
498 "logical_kinds" "real_kinds" "real32" "real64" "real128"
499 "lock_type" "atomic_int_kind" "atomic_logical_kind"
501 "\\_>")
502 "Regexp for Fortran intrinsic constants.")
504 ;; cf f90-looking-at-type-like.
505 (defun f90-typedef-matcher (limit)
506 "Search for the start/end of the definition of a derived type, up to LIMIT.
507 Set the match data so that subexpression 1,2 are the TYPE, and
508 type-name parts, respectively."
509 (let (found l)
510 (while (and (re-search-forward "\\_<\\(\\(?:end[ \t]*\\)?type\\)\\_>[ \t]*"
511 limit t)
512 (not (setq found
513 (progn
514 (setq l (match-data))
515 (unless (looking-at "\\(is\\_>\\|(\\)")
516 (when (if (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
517 (goto-char (match-end 0))
518 (re-search-forward
519 "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
520 (line-end-position) t))
521 ;; 0 is wrong, but we don't use it.
522 (set-match-data
523 (append l (list (match-beginning 1)
524 (match-end 1))))
525 t)))))))
526 found))
528 (defvar f90-font-lock-keywords-1
529 (list
530 ;; Special highlighting of "module procedure".
531 '("\\_<\\(module[ \t]*procedure\\)\\_>\\([^()\n]*::\\)?[ \t]*\\([^&!\n]*\\)"
532 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
533 ;; Highlight definition of derived type.
534 ;;; '("\\_<\\(\\(?:end[ \t]*\\)?type\\)\\_>\\([^()\n]*::\\)?[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
535 ;;; (1 font-lock-keyword-face) (3 font-lock-function-name-face))
536 '(f90-typedef-matcher
537 (1 font-lock-keyword-face) (2 font-lock-function-name-face))
538 ;; F2003. Prevent operators being highlighted as functions.
539 '("\\_<\\(\\(?:end[ \t]*\\)?interface[ \t]*\\(?:assignment\\|operator\\|\
540 read\\|write\\)\\)[ \t]*(" (1 font-lock-keyword-face t))
541 ;; Other functions and declarations. Named interfaces = F2003.
542 ;; F2008: end submodule submodule_name.
543 '("\\_<\\(\\(?:end[ \t]*\\)?\\(program\\|\\(?:sub\\)?module\\|\
544 function\\|associate\\|subroutine\\|interface\\)\\|use\\|call\\)\
545 \\_>[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
546 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
547 ;; F2008: submodule (parent_name) submodule_name.
548 '("\\_<\\(submodule\\)\\_>[ \t]*([^)\n]+)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
549 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
550 ;; F2003.
551 '("\\_<\\(use\\)[ \t]*,[ \t]*\\(\\(?:non_\\)?intrinsic\\)[ \t]*::[ \t]*\
552 \\(\\(?:\\sw\\|\\s_\\)+\\)"
553 (1 font-lock-keyword-face) (2 font-lock-keyword-face)
554 (3 font-lock-function-name-face))
555 "\\_<\\(\\(end[ \t]*\\)?block[ \t]*data\\|contains\\)\\_>"
556 ;; "abstract interface" is F2003.
557 '("\\_<abstract[ \t]*interface\\_>" (0 font-lock-keyword-face t)))
558 "This does fairly subdued highlighting of comments and function calls.")
560 ;; NB not explicitly handling this, yet it seems to work.
561 ;; type(...) function foo()
562 (defun f90-typedec-matcher (limit)
563 "Search for the declaration of variables of derived type, up to LIMIT.
564 Set the match data so that subexpression 1,2 are the TYPE(...),
565 and variable-name parts, respectively."
566 ;; Matcher functions must return nil only when there are no more
567 ;; matches within the search range.
568 (let (found l)
569 (while (and (re-search-forward "\\_<\\(type\\|class\\)[ \t]*(" limit t)
570 (not
571 (setq found
572 (condition-case nil
573 (progn
574 ;; Set l after this to just highlight
575 ;; the "type" part.
576 (backward-char 1)
577 ;; Needed for: type( foo(...) ) :: bar
578 (forward-sexp)
579 (setq l (list (match-beginning 0) (point)))
580 (skip-chars-forward " \t")
581 (when
582 (re-search-forward
583 ;; type (foo) bar, qux
584 (if (looking-at "\\(?:\\sw\\|\\s_\\)+")
585 "\\([^&!\n]+\\)"
586 ;; type (foo), stuff :: bar, qux
587 "::[ \t]*\\([^&!\n]+\\)")
588 (line-end-position) t)
589 (set-match-data
590 (append (list (car l) (match-end 1))
591 l (list (match-beginning 1)
592 (match-end 1))))
594 (error nil))))))
595 found))
597 (defvar f90-font-lock-keywords-2
598 (append
599 f90-font-lock-keywords-1
600 (list
601 ;; Variable declarations (avoid the real function call).
602 ;; NB by accident (?), this correctly fontifies the "integer" in:
603 ;; integer () function foo ()
604 ;; because "() function foo ()" matches \\3.
605 ;; The "pure" part does not really belong here, but was added to
606 ;; exploit that hack.
607 ;; The "function foo" bit is correctly fontified by keywords-1.
608 ;; TODO ? actually check for balanced parens in that case.
609 '("^[ \t0-9]*\\(?:pure\\|elemental\\)?[ \t]*\
610 \\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
611 enumerator\\|generic\\|procedure\\|logical\\|double[ \t]*precision\\)\
612 \\(.*::\\|[ \t]*(.*)\\)?\\([^&!\n]*\\)"
613 (1 font-lock-type-face t) (4 font-lock-variable-name-face t))
614 ;; Derived type/class variables.
615 ;; TODO ? If we just highlighted the "type" part, rather than
616 ;; "type(...)", this could be in the previous expression. And this
617 ;; would be consistent with integer( kind=8 ), etc.
618 '(f90-typedec-matcher
619 (1 font-lock-type-face) (2 font-lock-variable-name-face))
620 ;; "real function foo (args)". Must override previous. Note hack
621 ;; to get "args" unhighlighted again. Might not always be right,
622 ;; but probably better than leaving them as variables.
623 ;; NB not explicitly handling this case:
624 ;; integer( kind=1 ) function foo()
625 ;; thanks to the happy accident described above.
626 ;; Not anchored, so don't need to worry about "pure" etc.
627 '("\\_<\\(\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
628 logical\\|double[ \t]*precision\\|\
629 \\(?:type\\|class\\)[ \t]*([ \t]*\\(?:\\sw\\|\\s_\\)+[ \t]*)\\)[ \t]*\\)\
630 \\(function\\)\\_>[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*\\(([^&!\n]*)\\)"
631 (1 font-lock-type-face t) (4 font-lock-keyword-face t)
632 (5 font-lock-function-name-face t) (6 'default t))
633 ;; enum (F2003; must be followed by ", bind(C)").
634 '("\\_<\\(enum\\)[ \t]*," (1 font-lock-keyword-face))
635 ;; end do, enum (F2003), if, select, where, and forall constructs.
636 ;; block, critical (F2008).
637 ;; Note that "block data" may get somewhat mixed up with F2008 blocks,
638 ;; but since the former is obsolete I'm not going to worry about it.
639 '("\\_<\\(end[ \t]*\\(do\\|if\\|enum\\|select\\|forall\\|where\\|\
640 block\\|critical\\)\\)\\_>\
641 \\([ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?"
642 (1 font-lock-keyword-face) (3 font-lock-constant-face nil t))
643 '("^[ \t0-9]*\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|\
644 do\\([ \t]*while\\)?\\|select[ \t]*\\(?:case\\|type\\)\\|where\\|\
645 forall\\|block\\|critical\\)\\)\\_>"
646 (2 font-lock-constant-face nil t) (3 font-lock-keyword-face))
647 ;; Implicit declaration.
648 '("\\_<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\
649 \\|enumerator\\|procedure\\|\
650 logical\\|double[ \t]*precision\\|type[ \t]*(\\(?:\\sw\\|\\s_\\)+)\\|none\\)[ \t]*"
651 (1 font-lock-keyword-face) (2 font-lock-type-face))
652 '("\\_<\\(namelist\\|common\\)[ \t]*/\\(\\(?:\\sw\\|\\s_\\)+\\)?\/"
653 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
654 "\\_<else\\([ \t]*if\\|where\\)?\\_>"
655 '("\\(&\\)[ \t]*\\(!\\|$\\)" (1 font-lock-keyword-face))
656 "\\_<\\(then\\|continue\\|format\\|include\\|\\(?:error[ \t]+\\)?stop\\|\
657 return\\)\\_>"
658 '("\\_<\\(exit\\|cycle\\)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?\\_>"
659 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
660 '("\\_<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
661 ;; F2003 "class default".
662 '("\\_<\\(class\\)[ \t]*default" . 1)
663 ;; F2003 "type is" in a "select type" block.
664 '("\\_<\\(\\(type\\|class\\)[ \t]*is\\)[ \t]*(" (1 font-lock-keyword-face t))
665 '("\\_<\\(do\\|go[ \t]*to\\)\\_>[ \t]*\\([0-9]+\\)"
666 (1 font-lock-keyword-face) (2 font-lock-constant-face))
667 ;; Line numbers (lines whose first character after number is letter).
668 '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t))
669 ;; Override eg for "#include".
670 '("^#[ \t]*\\(?:\\sw\\|\\s_\\)+" (0 font-lock-preprocessor-face t)
671 ("\\_<defined\\_>" nil nil (0 font-lock-preprocessor-face)))
672 '("^#" ("\\(&&\\|||\\)" nil nil (0 font-lock-constant-face t)))
673 '("^#[ \t]*define[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)(" (1 font-lock-function-name-face))
674 '("^#[ \t]*define[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)" (1 font-lock-variable-name-face))
675 '("^#[ \t]*include[ \t]+\\(<.+>\\)" (1 font-lock-string-face))))
676 "Highlights declarations, do-loops and other constructs.")
678 (defvar f90-font-lock-keywords-3
679 (append f90-font-lock-keywords-2
680 (list
681 f90-keywords-level-3-re
682 f90-operators-re
683 ;; FIXME why isn't this font-lock-builtin-face, which
684 ;; otherwise we hardly use, as in fortran.el?
685 (list f90-procedures-re '(1 font-lock-keyword-face keep))
686 "\\_<real\\_>" ; avoid overwriting real defs
687 ;; As an attribute, but not as an optional argument.
688 '("\\_<\\(asynchronous\\)[ \t]*[^=]" . 1)))
689 "Highlights all F90 keywords and intrinsic procedures.")
691 (defvar f90-font-lock-keywords-4
692 (append f90-font-lock-keywords-3
693 (list (cons f90-constants-re 'font-lock-constant-face)
694 f90-hpf-keywords-re))
695 "Highlights all F90 and HPF keywords and constants.")
697 (defvar f90-font-lock-keywords
698 f90-font-lock-keywords-2
699 "Default expressions to highlight in F90 mode.
700 Can be overridden by the value of `font-lock-maximum-decoration'.")
703 (defvar f90-mode-syntax-table
704 (let ((table (make-syntax-table)))
705 (modify-syntax-entry ?\! "<" table) ; begin comment
706 (modify-syntax-entry ?\n ">" table) ; end comment
707 (modify-syntax-entry ?_ "_" table) ; underscore in names
708 (modify-syntax-entry ?\' "\"" table) ; string quote
709 (modify-syntax-entry ?\" "\"" table) ; string quote
710 ;; FIXME: We used to set ` to word syntax for the benefit of abbrevs, but
711 ;; we do not need it any more. Not sure if it should be "_" or "." now.
712 (modify-syntax-entry ?\` "_" table)
713 (modify-syntax-entry ?\r " " table) ; return is whitespace
714 (modify-syntax-entry ?+ "." table) ; punctuation
715 (modify-syntax-entry ?- "." table)
716 (modify-syntax-entry ?= "." table)
717 (modify-syntax-entry ?* "." table)
718 (modify-syntax-entry ?/ "." table)
719 (modify-syntax-entry ?% "." table) ; bug#8820
720 ;; I think that the f95 standard leaves the behavior of \
721 ;; unspecified, but that f2k will require it to be non-special.
722 ;; Use `f90-backslash-not-special' to change.
723 (modify-syntax-entry ?\\ "\\" table) ; escape chars
724 table)
725 "Syntax table used in F90 mode.")
727 (defvar f90-mode-map
728 (let ((map (make-sparse-keymap)))
729 (define-key map "`" 'f90-abbrev-start)
730 (define-key map "\C-c;" 'f90-comment-region)
731 (define-key map "\C-\M-a" 'f90-beginning-of-subprogram)
732 (define-key map "\C-\M-e" 'f90-end-of-subprogram)
733 (define-key map "\C-\M-h" 'f90-mark-subprogram)
734 (define-key map "\C-\M-n" 'f90-end-of-block)
735 (define-key map "\C-\M-p" 'f90-beginning-of-block)
736 (define-key map "\C-\M-q" 'f90-indent-subprogram)
737 (define-key map "\C-j" 'f90-indent-new-line) ; LFD equals C-j
738 ;;; (define-key map "\r" 'newline)
739 (define-key map "\C-c\r" 'f90-break-line)
740 ;;; (define-key map [M-return] 'f90-break-line)
741 (define-key map "\C-c\C-a" 'f90-previous-block)
742 (define-key map "\C-c\C-e" 'f90-next-block)
743 (define-key map "\C-c\C-d" 'f90-join-lines)
744 (define-key map "\C-c\C-f" 'f90-fill-region)
745 (define-key map "\C-c\C-p" 'f90-previous-statement)
746 (define-key map "\C-c\C-n" 'f90-next-statement)
747 (define-key map "\C-c]" 'f90-insert-end)
748 (define-key map "\C-c\C-w" 'f90-insert-end)
749 ;; Standard tab binding will call this, and also handle regions.
750 ;;; (define-key map "\t" 'f90-indent-line)
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)
755 (define-key map "/" 'f90-electric-insert)
757 (easy-menu-define f90-menu map "Menu for F90 mode."
758 `("F90"
759 ("Customization"
760 ,(custom-menu-create 'f90)
761 ;; FIXME useless?
762 ["Set" Custom-set :active t
763 :help "Set current value of all edited settings in the buffer"]
764 ["Save" Custom-save :active t
765 :help "Set and save all edited settings"]
766 ["Reset to Current" Custom-reset-current :active t
767 :help "Reset all edited settings to current"]
768 ["Reset to Saved" Custom-reset-saved :active t
769 :help "Reset all edited or set settings to saved"]
770 ["Reset to Standard Settings" Custom-reset-standard :active t
771 :help "Erase all customizations in buffer"]
773 "--"
774 ["Indent Subprogram" f90-indent-subprogram t]
775 ["Mark Subprogram" f90-mark-subprogram :active t :help
776 "Mark the end of the current subprogram, move point to the start"]
777 ["Beginning of Subprogram" f90-beginning-of-subprogram :active t
778 :help "Move point to the start of the current subprogram"]
779 ["End of Subprogram" f90-end-of-subprogram :active t
780 :help "Move point to the end of the current subprogram"]
781 "--"
782 ["(Un)Comment Region" f90-comment-region :active mark-active
783 :help "Comment or uncomment the region"]
784 ["Indent Region" f90-indent-region :active mark-active]
785 ["Fill Region" f90-fill-region :active mark-active
786 :help "Fill long lines in the region"]
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 'font-lock-defaults)
1189 '((f90-font-lock-keywords f90-font-lock-keywords-1
1190 f90-font-lock-keywords-2
1191 f90-font-lock-keywords-3
1192 f90-font-lock-keywords-4)
1193 nil t))
1194 (set (make-local-variable 'imenu-case-fold-search) t)
1195 (set (make-local-variable 'imenu-generic-expression)
1196 f90-imenu-generic-expression)
1197 (set (make-local-variable 'beginning-of-defun-function)
1198 'f90-beginning-of-subprogram)
1199 (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
1200 (set (make-local-variable 'add-log-current-defun-function)
1201 #'f90-current-defun))
1204 ;; Inline-functions.
1205 (defsubst f90-in-string ()
1206 "Return non-nil if point is inside a string.
1207 Checks from `point-min', or `f90-cache-position', if that is non-nil
1208 and lies before point."
1209 (let ((beg-pnt
1210 (if (and f90-cache-position (> (point) f90-cache-position))
1211 f90-cache-position
1212 (point-min))))
1213 (nth 3 (parse-partial-sexp beg-pnt (point)))))
1215 (defsubst f90-in-comment ()
1216 "Return non-nil if point is inside a comment.
1217 Checks from `point-min', or `f90-cache-position', if that is non-nil
1218 and lies before point."
1219 (let ((beg-pnt
1220 (if (and f90-cache-position (> (point) f90-cache-position))
1221 f90-cache-position
1222 (point-min))))
1223 (nth 4 (parse-partial-sexp beg-pnt (point)))))
1225 (defsubst f90-line-continued ()
1226 "Return t if the current line is a continued one.
1227 This includes comment or preprocessor lines embedded in continued lines,
1228 but not the last line of a continued statement."
1229 (save-excursion
1230 (beginning-of-line)
1231 (while (and (looking-at "[ \t]*\\([!#]\\|$\\)") (zerop (forward-line -1))))
1232 (end-of-line)
1233 (while (f90-in-comment)
1234 (search-backward "!" (line-beginning-position))
1235 (skip-chars-backward "!"))
1236 (skip-chars-backward " \t")
1237 (= (preceding-char) ?&)))
1239 ;; GM this is not right, eg a continuation line starting with a number.
1240 ;; Need f90-code-start-position function.
1241 ;; And yet, things seems to work with this...
1242 ;; cf f90-indent-line
1243 ;; (beginning-of-line) ; digits after & \n are not line-nos
1244 ;; (if (not (save-excursion (and (f90-previous-statement)
1245 ;; (f90-line-continued))))
1246 ;; (f90-indent-line-no)
1247 (defsubst f90-current-indentation ()
1248 "Return indentation of current line.
1249 Line-numbers are considered whitespace characters."
1250 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
1252 (defsubst f90-indent-to (col &optional no-line-number)
1253 "Indent current line to column COL.
1254 If optional argument NO-LINE-NUMBER is nil, jump over a possible
1255 line-number before indenting."
1256 (beginning-of-line)
1257 (or no-line-number
1258 (skip-chars-forward " \t0-9"))
1259 (delete-horizontal-space)
1260 ;; Leave >= 1 space after line number.
1261 (indent-to col (if (zerop (current-column)) 0 1)))
1263 (defsubst f90-get-present-comment-type ()
1264 "If point lies within a comment, return the string starting the comment.
1265 For example, \"!\" or \"!!\", followed by the appropriate amount of
1266 whitespace, if any."
1267 ;; Include the whitespace for consistent auto-filling of comment blocks.
1268 (save-excursion
1269 (when (f90-in-comment)
1270 (beginning-of-line)
1271 (re-search-forward "!+[ \t]*" (line-end-position))
1272 (while (f90-in-string)
1273 (re-search-forward "!+[ \t]*" (line-end-position)))
1274 (match-string-no-properties 0))))
1276 (defsubst f90-equal-symbols (a b)
1277 "Compare strings A and B neglecting case and allowing for nil value."
1278 (equal (if a (downcase a) nil)
1279 (if b (downcase b) nil)))
1281 (defsubst f90-looking-at-do ()
1282 "Return (\"do\" NAME) if a do statement starts after point.
1283 NAME is nil if the statement has no label."
1284 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\_>")
1285 (list (match-string 3) (match-string 2))))
1287 (defsubst f90-looking-at-select-case ()
1288 "Return (\"select\" NAME) if a select statement starts after point.
1289 NAME is nil if the statement has no label."
1290 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\
1291 \\(select\\)[ \t]*\\(case\\|type\\)[ \t]*(")
1292 (list (match-string 3) (match-string 2))))
1294 (defsubst f90-looking-at-if-then ()
1295 "Return (\"if\" NAME) if an if () then statement starts after point.
1296 NAME is nil if the statement has no label."
1297 (save-excursion
1298 (when (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\_>")
1299 (let ((struct (match-string 3))
1300 (label (match-string 2))
1301 (pos (scan-lists (point) 1 0)))
1302 (and pos (goto-char pos))
1303 (skip-chars-forward " \t")
1304 (if (or (looking-at "then\\_>")
1305 (when (f90-line-continued)
1306 (f90-next-statement)
1307 (skip-chars-forward " \t0-9&")
1308 (looking-at "then\\_>")))
1309 (list struct label))))))
1311 ;; FIXME label?
1312 (defsubst f90-looking-at-associate ()
1313 "Return (\"associate\") if an associate block starts after point."
1314 (if (looking-at "\\_<\\(associate\\)[ \t]*(")
1315 (list (match-string 1))))
1317 (defsubst f90-looking-at-critical ()
1318 "Return (KIND NAME) if a critical or block block starts after point."
1319 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(critical\\|block\\)\\_>")
1320 (let ((struct (match-string 3))
1321 (label (match-string 2)))
1322 (if (or (not (string-equal "block" struct))
1323 (save-excursion
1324 (skip-chars-forward " \t")
1325 (not (looking-at "data\\_>"))))
1326 (list struct label)))))
1328 (defsubst f90-looking-at-end-critical ()
1329 "Return non-nil if a critical or block block ends after point."
1330 (if (looking-at "end[ \t]*\\(critical\\|block\\)\\_>")
1331 (or (not (string-equal "block" (match-string 1)))
1332 (save-excursion
1333 (skip-chars-forward " \t")
1334 (not (looking-at "data\\_>"))))))
1336 (defsubst f90-looking-at-where-or-forall ()
1337 "Return (KIND NAME) if a where or forall block starts after point.
1338 NAME is nil if the statement has no label."
1339 (save-excursion
1340 (when (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\
1341 \\(where\\|forall\\)\\_>")
1342 (let ((struct (match-string 3))
1343 (label (match-string 2))
1344 (pos (scan-lists (point) 1 0)))
1345 (and pos (goto-char pos))
1346 (skip-chars-forward " \t")
1347 (if (looking-at "\\(!\\|$\\)") (list struct label))))))
1349 (defsubst f90-looking-at-type-like ()
1350 "Return (KIND NAME) if a type/enum/interface/block-data starts after point.
1351 NAME is non-nil only for type and certain interfaces."
1352 (cond
1353 ((save-excursion
1354 (and (looking-at "\\_<type\\_>[ \t]*")
1355 (goto-char (match-end 0))
1356 (not (looking-at "\\(is\\_>\\|(\\)"))
1357 (or (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
1358 (re-search-forward "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
1359 (line-end-position) t))))
1360 (list "type" (match-string 1)))
1361 ;;; ((and (not (looking-at f90-typeis-re))
1362 ;;; (looking-at f90-type-def-re))
1363 ;;; (list (match-string 1) (match-string 2)))
1364 ((looking-at "\\_<\\(interface\\)\\_>[ \t]*")
1365 (list (match-string 1)
1366 (save-excursion
1367 (goto-char (match-end 0))
1368 (if (or (looking-at "\\(operator\\|assignment\\|read\\|\
1369 write\\)[ \t]*([^)\n]*)")
1370 (looking-at "\\(?:\\sw\\|\\s_\\)+"))
1371 (match-string 0)))))
1372 ((looking-at "\\(enum\\|block[ \t]*data\\)\\_>")
1373 (list (match-string 1) nil))
1374 ((looking-at "abstract[ \t]*\\(interface\\)\\_>")
1375 (list (match-string 1) nil))))
1377 (defsubst f90-looking-at-program-block-start ()
1378 "Return (KIND NAME) if a program block with name NAME starts after point."
1379 ;;;NAME is nil for an un-named main PROGRAM block."
1380 (cond
1381 ((looking-at "\\(program\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
1382 (list (match-string 1) (match-string 2)))
1383 ((and (not (looking-at "module[ \t]*procedure\\_>"))
1384 (looking-at "\\(module\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>"))
1385 (list (match-string 1) (match-string 2)))
1386 ((looking-at "\\(submodule\\)[ \t]*([^)\n]+)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
1387 (list (match-string 1) (match-string 2)))
1388 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
1389 (looking-at "[^!'\"&\n]*\\(function\\|subroutine\\)[ \t]+\
1390 \\(\\(?:\\sw\\|\\s_\\)+\\)"))
1391 (list (match-string 1) (match-string 2)))))
1392 ;; Following will match an un-named main program block; however
1393 ;; one needs to check if there is an actual PROGRAM statement after
1394 ;; point (and before any END program). Adding this will require
1395 ;; change to eg f90-calculate-indent.
1396 ;;; ((save-excursion
1397 ;;; (not (f90-previous-statement)))
1398 ;;; '("program" nil))))
1400 (defsubst f90-looking-at-program-block-end ()
1401 "Return (KIND NAME) if a block with name NAME ends after point."
1402 (cond ((looking-at "end[ \t]*\\(interface\\)[ \t]*\\(\
1403 \\(?:assignment\\|operator\\|read\\|write\\)[ \t]*([^)\n]*)\\)")
1404 (list (match-string 1) (match-string 2)))
1405 ((looking-at (concat "end[ \t]*" f90-blocks-re
1406 "?\\([ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?\\_>"))
1407 (list (match-string 1) (match-string 3)))))
1409 (defsubst f90-comment-indent ()
1410 "Return the indentation to be used for a comment starting at point.
1411 Used for `comment-indent-function' by F90 mode.
1412 \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
1413 `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
1414 All others return `comment-column', leaving at least one space after code."
1415 (cond ((looking-at "!!!") 0)
1416 ((and f90-directive-comment-re
1417 (looking-at f90-directive-comment-re)) 0)
1418 ((looking-at (regexp-quote f90-comment-region)) 0)
1419 ((and (looking-at f90-indented-comment-re)
1420 ;; Don't attempt to indent trailing comment as code.
1421 (save-excursion
1422 (skip-chars-backward " \t")
1423 (bolp)))
1424 (f90-calculate-indent))
1425 (t (save-excursion
1426 (skip-chars-backward " \t")
1427 (max (if (bolp) 0 (1+ (current-column))) comment-column)))))
1429 (defsubst f90-present-statement-cont ()
1430 "Return continuation properties of present statement.
1431 Possible return values are:
1432 single - statement is not continued.
1433 begin - current line is the first in a continued statement.
1434 end - current line is the last in a continued statement
1435 middle - current line is neither first nor last in a continued statement.
1436 Comment lines embedded amongst continued lines return `middle'."
1437 (let (pcont cont)
1438 (save-excursion
1439 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
1440 (setq cont (f90-line-continued))
1441 (cond ((and (not pcont) (not cont)) 'single)
1442 ((and (not pcont) cont) 'begin)
1443 ((and pcont (not cont)) 'end)
1444 ((and pcont cont) 'middle)
1445 (t (error "The impossible occurred")))))
1447 (defsubst f90-indent-line-no ()
1448 "If `f90-leave-line-no' is nil, left-justify a line number.
1449 Leaves point at the first non-blank character after the line number.
1450 Call from beginning of line."
1451 (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
1452 (delete-horizontal-space))
1453 (skip-chars-forward " \t0-9"))
1455 (defsubst f90-no-block-limit ()
1456 "Return nil if point is at the edge of a code block.
1457 Searches line forward for \"function\" or \"subroutine\",
1458 if all else fails."
1459 (save-excursion
1460 (not (or (looking-at "end")
1461 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
1462 \\|select[ \t]*\\(case\\|type\\)\\|case\\|where\\|forall\\|\
1463 \\(?:class\\|type\\)[ \t]*is\\|class[ \t]*default\\|\
1464 block\\|critical\\|enum\\|associate\\)\\_>")
1465 (looking-at "\\(program\\|\\(?:sub\\)?module\\|\
1466 \\(?:abstract[ \t]*\\)?interface\\|block[ \t]*data\\)\\_>")
1467 (looking-at "\\(contains\\|\\(?:\\sw\\|\\s_\\)+[ \t]*:\\)")
1468 (looking-at f90-type-def-re)
1469 (re-search-forward "\\(function\\|subroutine\\)"
1470 (line-end-position) t)))))
1472 (defsubst f90-update-line ()
1473 "Change case of current line as per `f90-auto-keyword-case'."
1474 (if f90-auto-keyword-case
1475 (f90-change-keywords f90-auto-keyword-case
1476 (line-beginning-position) (line-end-position))))
1478 (defun f90-electric-insert (&optional arg)
1479 "Change keyword case and auto-fill line as operators are inserted."
1480 (interactive "*p")
1481 (self-insert-command arg)
1482 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1483 (f90-update-line)))
1485 ;; Behave like self-insert-command for delete-selection-mode (bug#5593).
1486 (put 'f90-electric-insert 'delete-selection t)
1488 (defun f90-get-correct-indent ()
1489 "Get correct indent for a line starting with line number.
1490 Does not check type and subprogram indentation."
1491 (let ((epnt (line-end-position)) icol)
1492 (save-excursion
1493 (while (and (f90-previous-statement)
1494 (or (memq (f90-present-statement-cont) '(middle end))
1495 (looking-at "[ \t]*[0-9]"))))
1496 (setq icol (current-indentation))
1497 (beginning-of-line)
1498 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1499 (line-end-position) t)
1500 (beginning-of-line)
1501 (skip-chars-forward " \t")
1502 (cond ((f90-looking-at-do)
1503 (setq icol (+ icol f90-do-indent)))
1504 ((or (f90-looking-at-if-then)
1505 (f90-looking-at-where-or-forall)
1506 (f90-looking-at-select-case))
1507 (setq icol (+ icol f90-if-indent)))
1508 ;; FIXME this makes no sense, because this section/function is
1509 ;; only for if/do/select/where/forall ?
1510 ((f90-looking-at-associate)
1511 (setq icol (+ icol f90-associate-indent))))
1512 (end-of-line))
1513 (while (re-search-forward
1514 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1515 (beginning-of-line)
1516 (skip-chars-forward " \t0-9")
1517 (cond ((f90-looking-at-do)
1518 (setq icol (+ icol f90-do-indent)))
1519 ((or (f90-looking-at-if-then)
1520 (f90-looking-at-where-or-forall)
1521 (f90-looking-at-select-case))
1522 (setq icol (+ icol f90-if-indent)))
1523 ;; FIXME this makes no sense, because this section/function is
1524 ;; only for if/do/select/where/forall ?
1525 ((f90-looking-at-associate)
1526 (setq icol (+ icol f90-associate-indent)))
1527 ((looking-at f90-end-if-re)
1528 (setq icol (- icol f90-if-indent)))
1529 ((looking-at f90-end-associate-re)
1530 (setq icol (- icol f90-associate-indent)))
1531 ((f90-looking-at-end-critical)
1532 (setq icol (- icol f90-critical-indent)))
1533 ((looking-at "end[ \t]*do\\_>")
1534 (setq icol (- icol f90-do-indent))))
1535 (end-of-line))
1536 icol)))
1538 (defun f90-calculate-indent ()
1539 "Calculate the indent column based on previous statements."
1540 (interactive)
1541 (let (icol cont (case-fold-search t) (pnt (point)))
1542 (save-excursion
1543 (if (not (f90-previous-statement))
1544 ;; If f90-previous-statement returns nil, we must have been
1545 ;; called from on or before the first line of the first statement.
1546 (setq icol (if (or (save-excursion
1547 (goto-char pnt)
1548 (beginning-of-line)
1549 ;; Preprocessor line before code statement.
1550 (looking-at "[ \t]*#"))
1551 (progn
1552 ;; f90-previous-statement has moved us over
1553 ;; comment/blank lines, so we need to get
1554 ;; back to the first code statement.
1555 (when (looking-at "[ \t]*\\([!#]\\|$\\)")
1556 (f90-next-statement))
1557 (skip-chars-forward " \t0-9")
1558 (f90-looking-at-program-block-start)))
1560 ;; No explicit PROGRAM start statement.
1561 f90-program-indent))
1562 (setq cont (f90-present-statement-cont))
1563 (if (eq cont 'end)
1564 (while (not (eq 'begin (f90-present-statement-cont)))
1565 (f90-previous-statement)))
1566 (cond ((eq cont 'begin)
1567 (setq icol (+ (f90-current-indentation)
1568 f90-continuation-indent)))
1569 ((eq cont 'middle) (setq icol (current-indentation)))
1570 (t (setq icol (f90-current-indentation))
1571 (skip-chars-forward " \t")
1572 (if (looking-at "[0-9]")
1573 (setq icol (f90-get-correct-indent))
1574 (cond ((or (f90-looking-at-if-then)
1575 (f90-looking-at-where-or-forall)
1576 (f90-looking-at-select-case)
1577 (looking-at f90-else-like-re))
1578 (setq icol (+ icol f90-if-indent)))
1579 ((f90-looking-at-do)
1580 (setq icol (+ icol f90-do-indent)))
1581 ((f90-looking-at-type-like)
1582 (setq icol (+ icol f90-type-indent)))
1583 ((f90-looking-at-associate)
1584 (setq icol (+ icol f90-associate-indent)))
1585 ((f90-looking-at-critical)
1586 (setq icol (+ icol f90-critical-indent)))
1587 ((or (f90-looking-at-program-block-start)
1588 (looking-at "contains[ \t]*\\($\\|!\\)"))
1589 (setq icol (+ icol f90-program-indent)))))
1590 (goto-char pnt)
1591 (beginning-of-line)
1592 (cond ((looking-at "[ \t]*$"))
1593 ((looking-at "[ \t]*#") ; check for cpp directive
1594 (setq icol 0))
1596 (skip-chars-forward " \t0-9")
1597 (cond ((or (looking-at f90-else-like-re)
1598 (looking-at f90-end-if-re))
1599 (setq icol (- icol f90-if-indent)))
1600 ((looking-at "end[ \t]*do\\_>")
1601 (setq icol (- icol f90-do-indent)))
1602 ((looking-at f90-end-type-re)
1603 (setq icol (- icol f90-type-indent)))
1604 ((looking-at f90-end-associate-re)
1605 (setq icol (- icol f90-associate-indent)))
1606 ((f90-looking-at-end-critical)
1607 (setq icol (- icol f90-critical-indent)))
1608 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1609 (f90-looking-at-program-block-end))
1610 (setq icol (- icol f90-program-indent))))))))))
1611 icol))
1613 (defun f90-previous-statement ()
1614 "Move point to beginning of the previous F90 statement.
1615 If no previous statement is found (i.e. if called from the first
1616 statement in the buffer), move to the start of the buffer and
1617 return nil. A statement is a line which is neither blank nor a
1618 comment."
1619 (interactive)
1620 (let (not-first-statement)
1621 (beginning-of-line)
1622 (while (and (setq not-first-statement (zerop (forward-line -1)))
1623 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1624 not-first-statement))
1626 (defun f90-next-statement ()
1627 "Move point to beginning of the next F90 statement.
1628 Return nil if no later statement is found."
1629 (interactive)
1630 (let (not-last-statement)
1631 (beginning-of-line)
1632 (while (and (setq not-last-statement
1633 (and (zerop (forward-line 1))
1634 (not (eobp))))
1635 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1636 not-last-statement))
1638 (defun f90-beginning-of-subprogram ()
1639 "Move point to the beginning of the current subprogram.
1640 Return (TYPE NAME), or nil if not found."
1641 (interactive)
1642 (let ((count 1) (case-fold-search t) matching-beg)
1643 (beginning-of-line)
1644 (while (and (> count 0)
1645 (re-search-backward f90-program-block-re nil 'move))
1646 (beginning-of-line)
1647 (skip-chars-forward " \t0-9")
1648 ;; Check if in string in case using non-standard feature where
1649 ;; continued strings do not need "&" at start of continuations.
1650 (cond ((f90-in-string))
1651 ((setq matching-beg (f90-looking-at-program-block-start))
1652 (setq count (1- count)))
1653 ((f90-looking-at-program-block-end)
1654 (setq count (1+ count)))))
1655 (beginning-of-line)
1656 (if (zerop count)
1657 matching-beg
1658 ;; Note this includes the case of an un-named main program,
1659 ;; in which case we go to (point-min).
1660 (if (called-interactively-p 'interactive)
1661 (message "No beginning found"))
1662 nil)))
1664 (defun f90-end-of-subprogram ()
1665 "Move point to the end of the current subprogram.
1666 Return (TYPE NAME), or nil if not found."
1667 (interactive)
1668 (let ((case-fold-search t)
1669 (count 1)
1670 matching-end)
1671 (end-of-line)
1672 (while (and (> count 0)
1673 (re-search-forward f90-program-block-re nil 'move))
1674 (beginning-of-line)
1675 (skip-chars-forward " \t0-9")
1676 (cond ((f90-in-string))
1677 ((f90-looking-at-program-block-start)
1678 (setq count (1+ count)))
1679 ((setq matching-end (f90-looking-at-program-block-end))
1680 (setq count (1- count))))
1681 (end-of-line))
1682 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1683 ;; has a net non-zero effect, which seems odd.
1684 ;;; (forward-line 1)
1685 (if (zerop count)
1686 matching-end
1687 (if (called-interactively-p 'interactive)
1688 (message "No end found"))
1689 nil)))
1692 (defun f90-end-of-block (&optional num)
1693 "Move point forward to the end of the current code block.
1694 With optional argument NUM, go forward that many balanced blocks.
1695 If NUM is negative, go backward to the start of a block. Checks
1696 for consistency of block types and labels (if present), and
1697 completes outermost block if `f90-smart-end' is non-nil.
1698 Interactively, pushes mark before moving point."
1699 (interactive "p")
1700 ;; Can move some distance.
1701 (if (called-interactively-p 'any) (push-mark (point) t))
1702 (and num (< num 0) (f90-beginning-of-block (- num)))
1703 (let ((f90-smart-end (if f90-smart-end 'no-blink)) ; for final match-end
1704 (case-fold-search t)
1705 (count (or num 1))
1706 start-list start-this start-type start-label end-type end-label)
1707 (end-of-line) ; probably want this
1708 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1709 (beginning-of-line)
1710 (skip-chars-forward " \t0-9")
1711 (cond ((or (f90-in-string) (f90-in-comment)))
1712 ((setq start-this
1714 (f90-looking-at-do)
1715 (f90-looking-at-select-case)
1716 (f90-looking-at-type-like)
1717 (f90-looking-at-associate)
1718 (f90-looking-at-critical)
1719 (f90-looking-at-program-block-start)
1720 (f90-looking-at-if-then)
1721 (f90-looking-at-where-or-forall)))
1722 (setq start-list (cons start-this start-list) ; not add-to-list!
1723 count (1+ count)))
1724 ((looking-at (concat "end[ \t]*" f90-blocks-re
1725 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
1726 (setq end-type (match-string 1)
1727 end-label (match-string 2)
1728 count (1- count))
1729 ;; Check any internal blocks.
1730 (when start-list
1731 (setq start-this (car start-list)
1732 start-list (cdr start-list)
1733 start-type (car start-this)
1734 start-label (cadr start-this))
1735 (or (f90-equal-symbols start-type end-type)
1736 (error "End type `%s' does not match start type `%s'"
1737 end-type start-type))
1738 (or (f90-equal-symbols start-label end-label)
1739 (error "End label `%s' does not match start label `%s'"
1740 end-label start-label)))))
1741 (end-of-line))
1742 (if (> count 0) (error "Missing block end"))
1743 ;; Check outermost block.
1744 (when f90-smart-end
1745 (save-excursion
1746 (beginning-of-line)
1747 (skip-chars-forward " \t0-9")
1748 (f90-match-end)))))
1750 (defun f90-beginning-of-block (&optional num)
1751 "Move point backwards to the start of the current code block.
1752 With optional argument NUM, go backward that many balanced blocks.
1753 If NUM is negative, go forward to the end of a block.
1754 Checks for consistency of block types and labels (if present).
1755 Does not check the outermost block, because it may be incomplete.
1756 Interactively, pushes mark before moving point."
1757 (interactive "p")
1758 (if (called-interactively-p 'any) (push-mark (point) t))
1759 (and num (< num 0) (f90-end-of-block (- num)))
1760 (let ((case-fold-search t)
1761 (count (or num 1))
1762 end-list end-this end-type end-label
1763 start-this start-type start-label)
1764 (beginning-of-line) ; probably want this
1765 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1766 (beginning-of-line)
1767 (skip-chars-forward " \t0-9")
1768 (cond ((or (f90-in-string) (f90-in-comment)))
1769 ((looking-at (concat "end[ \t]*" f90-blocks-re
1770 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
1771 (setq end-list (cons (list (match-string 1) (match-string 2))
1772 end-list)
1773 count (1+ count)))
1774 ((setq start-this
1776 (f90-looking-at-do)
1777 (f90-looking-at-select-case)
1778 (f90-looking-at-type-like)
1779 (f90-looking-at-associate)
1780 (f90-looking-at-critical)
1781 (f90-looking-at-program-block-start)
1782 (f90-looking-at-if-then)
1783 (f90-looking-at-where-or-forall)))
1784 (setq start-type (car start-this)
1785 start-label (cadr start-this)
1786 count (1- count))
1787 ;; Check any internal blocks.
1788 (when end-list
1789 (setq end-this (car end-list)
1790 end-list (cdr end-list)
1791 end-type (car end-this)
1792 end-label (cadr end-this))
1793 (or (f90-equal-symbols start-type end-type)
1794 (error "Start type `%s' does not match end type `%s'"
1795 start-type end-type))
1796 (or (f90-equal-symbols start-label end-label)
1797 (error "Start label `%s' does not match end label `%s'"
1798 start-label end-label))))))
1799 ;; Includes an un-named main program block.
1800 (if (> count 0) (error "Missing block start"))))
1802 (defun f90-next-block (&optional num)
1803 "Move point forward to the next end or start of a code block.
1804 With optional argument NUM, go forward that many blocks.
1805 If NUM is negative, go backwards.
1806 A block is a subroutine, if-endif, etc."
1807 (interactive "p")
1808 (let ((case-fold-search t)
1809 (count (if num (abs num) 1)))
1810 (while (and (> count 0)
1811 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1812 (re-search-backward f90-blocks-re nil 'move)))
1813 (beginning-of-line)
1814 (skip-chars-forward " \t0-9")
1815 (cond ((or (f90-in-string) (f90-in-comment)))
1816 ((or
1817 (looking-at "end[ \t]*")
1818 (f90-looking-at-do)
1819 (f90-looking-at-select-case)
1820 (f90-looking-at-type-like)
1821 (f90-looking-at-associate)
1822 (f90-looking-at-critical)
1823 (f90-looking-at-program-block-start)
1824 (f90-looking-at-if-then)
1825 (f90-looking-at-where-or-forall))
1826 (setq count (1- count))))
1827 (if (> num 0) (end-of-line)
1828 (beginning-of-line)))))
1831 (defun f90-previous-block (&optional num)
1832 "Move point backward to the previous end or start of a code block.
1833 With optional argument NUM, go backward that many blocks.
1834 If NUM is negative, go forwards.
1835 A block is a subroutine, if-endif, etc."
1836 (interactive "p")
1837 (f90-next-block (- (or num 1))))
1840 (defun f90-mark-subprogram ()
1841 "Put mark at end of F90 subprogram, point at beginning, push mark."
1842 (interactive)
1843 (let ((pos (point)) program)
1844 (f90-end-of-subprogram)
1845 (push-mark)
1846 (goto-char pos)
1847 (setq program (f90-beginning-of-subprogram))
1848 (if (featurep 'xemacs)
1849 (zmacs-activate-region)
1850 (setq mark-active t
1851 deactivate-mark nil))
1852 program))
1854 (defun f90-comment-region (beg-region end-region)
1855 "Comment/uncomment every line in the region.
1856 Insert the variable `f90-comment-region' at the start of every line
1857 in the region, or, if already present, remove it."
1858 (interactive "*r")
1859 (let ((end (copy-marker end-region)))
1860 (goto-char beg-region)
1861 (beginning-of-line)
1862 (if (looking-at (regexp-quote f90-comment-region))
1863 (delete-region (point) (match-end 0))
1864 (insert f90-comment-region))
1865 (while (and (zerop (forward-line 1))
1866 (< (point) end))
1867 (if (looking-at (regexp-quote f90-comment-region))
1868 (delete-region (point) (match-end 0))
1869 (insert f90-comment-region)))
1870 (set-marker end nil)))
1872 (defun f90-indent-line (&optional no-update)
1873 "Indent current line as F90 code.
1874 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1875 after indenting."
1876 (interactive "*P")
1877 (let ((case-fold-search t)
1878 (pos (point-marker))
1879 indent no-line-number)
1880 (beginning-of-line) ; digits after & \n are not line-nos
1881 (if (not (save-excursion (and (f90-previous-statement)
1882 (f90-line-continued))))
1883 (f90-indent-line-no)
1884 (setq no-line-number t)
1885 (skip-chars-forward " \t"))
1886 ;; FIXME This means f90-calculate-indent gives different answers
1887 ;; for comments and preprocessor lines to this function.
1888 ;; Better to make f90-calculate-indent return the correct answer?
1889 (cond ((= (following-char) ?!) (setq indent (f90-comment-indent)))
1890 ((= (following-char) ?#) (setq indent 0))
1892 (and f90-smart-end (looking-at "end")
1893 (f90-match-end))
1894 (setq indent (f90-calculate-indent))))
1895 (or (= indent (current-column))
1896 (f90-indent-to indent no-line-number))
1897 ;; If initial point was within line's indentation,
1898 ;; position after the indentation. Else stay at same point in text.
1899 (and (< (point) pos)
1900 (goto-char pos))
1901 (if auto-fill-function
1902 ;; GM NO-UPDATE not honored, since this calls f90-update-line.
1903 (f90-do-auto-fill)
1904 (or no-update (f90-update-line)))
1905 (set-marker pos nil)))
1907 (defun f90-indent-new-line ()
1908 "Re-indent current line, insert a newline and indent the newline.
1909 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1910 If run in the middle of a line, the line is not broken."
1911 (interactive "*")
1912 (if abbrev-mode (expand-abbrev))
1913 (beginning-of-line) ; reindent where likely to be needed
1914 (f90-indent-line) ; calls indent-line-no, update-line
1915 (end-of-line)
1916 (delete-horizontal-space) ; destroy trailing whitespace
1917 (let ((string (f90-in-string))
1918 (cont (f90-line-continued)))
1919 (and string (not cont) (insert "&"))
1920 (newline)
1921 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
1922 (f90-indent-line 'no-update)) ; nothing to update
1925 ;; TODO not add spaces to empty lines at the start.
1926 ;; Why is second line getting extra indent over first?
1927 (defun f90-indent-region (beg-region end-region)
1928 "Indent every line in region by forward parsing."
1929 (interactive "*r")
1930 (let ((end-region-mark (copy-marker end-region))
1931 (save-point (point-marker))
1932 (case-fold-search t)
1933 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
1934 (goto-char beg-region)
1935 ;; First find a line which is not a continuation line or comment.
1936 (beginning-of-line)
1937 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1938 (progn (f90-indent-line 'no-update)
1939 (zerop (forward-line 1)))
1940 (< (point) end-region-mark)))
1941 (setq cont (f90-present-statement-cont))
1942 (while (and (memq cont '(middle end))
1943 (f90-previous-statement))
1944 (setq cont (f90-present-statement-cont)))
1945 ;; Process present line for beginning of block.
1946 (setq f90-cache-position (point))
1947 (f90-indent-line 'no-update)
1948 (setq ind-lev (f90-current-indentation)
1949 ind-curr ind-lev)
1950 (beginning-of-line)
1951 (skip-chars-forward " \t0-9")
1952 (setq struct nil
1953 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1954 ((or (setq struct (f90-looking-at-if-then))
1955 (setq struct (f90-looking-at-select-case))
1956 (setq struct (f90-looking-at-where-or-forall))
1957 (looking-at f90-else-like-re))
1958 f90-if-indent)
1959 ((setq struct (f90-looking-at-type-like))
1960 f90-type-indent)
1961 ((setq struct (f90-looking-at-associate))
1962 f90-associate-indent)
1963 ((setq struct (f90-looking-at-critical))
1964 f90-critical-indent)
1965 ((or (setq struct (f90-looking-at-program-block-start))
1966 (looking-at "contains[ \t]*\\($\\|!\\)"))
1967 f90-program-indent)))
1968 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1969 (if struct (setq block-list (cons struct block-list)))
1970 (while (and (f90-line-continued) (zerop (forward-line 1))
1971 (< (point) end-region-mark))
1972 (if (looking-at "[ \t]*!")
1973 (f90-indent-to (f90-comment-indent))
1974 (or (= (current-indentation)
1975 (+ ind-curr f90-continuation-indent))
1976 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1977 ;; Process all following lines.
1978 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1979 (beginning-of-line)
1980 (f90-indent-line-no)
1981 (setq f90-cache-position (point))
1982 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1983 ((looking-at "[ \t]*#") (setq ind-curr 0))
1984 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1985 ((f90-no-block-limit) (setq ind-curr ind-lev))
1986 ((looking-at f90-else-like-re) (setq ind-curr
1987 (- ind-lev f90-if-indent)))
1988 ((looking-at "contains[ \t]*\\($\\|!\\)")
1989 (setq ind-curr (- ind-lev f90-program-indent)))
1990 ((setq ind-b
1991 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1992 ((or (setq struct (f90-looking-at-if-then))
1993 (setq struct (f90-looking-at-select-case))
1994 (setq struct (f90-looking-at-where-or-forall)))
1995 f90-if-indent)
1996 ((setq struct (f90-looking-at-type-like))
1997 f90-type-indent)
1998 ((setq struct (f90-looking-at-associate))
1999 f90-associate-indent)
2000 ((setq struct (f90-looking-at-critical))
2001 f90-critical-indent)
2002 ((setq struct (f90-looking-at-program-block-start))
2003 f90-program-indent)))
2004 (setq ind-curr ind-lev)
2005 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
2006 (setq block-list (cons struct block-list)))
2007 ((setq end-struct (f90-looking-at-program-block-end))
2008 (setq beg-struct (car block-list)
2009 block-list (cdr block-list))
2010 (if f90-smart-end
2011 (save-excursion
2012 (f90-block-match (car beg-struct) (cadr beg-struct)
2013 (car end-struct) (cadr end-struct))))
2014 (setq ind-b
2015 (cond ((looking-at f90-end-if-re) f90-if-indent)
2016 ((looking-at "end[ \t]*do\\_>") f90-do-indent)
2017 ((looking-at f90-end-type-re) f90-type-indent)
2018 ((looking-at f90-end-associate-re)
2019 f90-associate-indent)
2020 ((f90-looking-at-end-critical) f90-critical-indent)
2021 ((f90-looking-at-program-block-end)
2022 f90-program-indent)))
2023 (if ind-b (setq ind-lev (- ind-lev ind-b)))
2024 (setq ind-curr ind-lev))
2025 (t (setq ind-curr ind-lev)))
2026 ;; Do the indentation if necessary.
2027 (or (= ind-curr (current-column))
2028 (f90-indent-to ind-curr))
2029 (while (and (f90-line-continued) (zerop (forward-line 1))
2030 (< (point) end-region-mark))
2031 (cond ((looking-at "[ \t]*#") (f90-indent-to 0))
2032 ((looking-at "[ \t]*!") (f90-indent-to (f90-comment-indent)))
2034 (or (= (current-indentation)
2035 (+ ind-curr f90-continuation-indent))
2036 (f90-indent-to
2037 (+ ind-curr f90-continuation-indent) 'no-line-no))))))
2038 ;; Restore point, etc.
2039 (setq f90-cache-position nil)
2040 (goto-char save-point)
2041 (set-marker end-region-mark nil)
2042 (set-marker save-point nil)
2043 (if (featurep 'xemacs)
2044 (zmacs-deactivate-region)
2045 (deactivate-mark))))
2047 (defun f90-indent-subprogram ()
2048 "Properly indent the subprogram containing point."
2049 (interactive "*")
2050 (save-excursion
2051 (let ((program (f90-mark-subprogram)))
2052 (if program
2053 (progn
2054 (message "Indenting %s %s..."
2055 (car program) (cadr program))
2056 (indent-region (point) (mark) nil)
2057 (message "Indenting %s %s...done"
2058 (car program) (cadr program)))
2059 (message "Indenting the whole file...")
2060 (indent-region (point) (mark) nil)
2061 (message "Indenting the whole file...done")))))
2063 (defun f90-break-line (&optional no-update)
2064 "Break line at point, insert continuation marker(s) and indent.
2065 Unless in a string or comment, or if the optional argument NO-UPDATE
2066 is non-nil, call `f90-update-line' after inserting the continuation marker."
2067 (interactive "*P")
2068 (cond ((f90-in-string)
2069 (insert "&\n&"))
2070 ((f90-in-comment)
2071 (delete-horizontal-space) ; remove trailing whitespace
2072 (insert "\n" (f90-get-present-comment-type)))
2073 (t (insert "&")
2074 (or no-update (f90-update-line))
2075 (newline 1)
2076 ;; FIXME also need leading ampersand if split lexical token (eg ==).
2077 ;; Or respect f90-no-break-re.
2078 (if f90-beginning-ampersand (insert "&"))))
2079 (indent-according-to-mode))
2081 (defun f90-find-breakpoint ()
2082 "From `fill-column', search backward for break-delimiter."
2083 ;; Commented text more likely than commented code.
2084 (if (f90-in-comment)
2085 (re-search-backward "\\s-" (line-beginning-position))
2086 (re-search-backward f90-break-delimiters (line-beginning-position))
2087 (if (not f90-break-before-delimiters)
2088 (forward-char (if (looking-at f90-no-break-re) 2 1))
2089 (backward-char)
2090 (or (looking-at f90-no-break-re)
2091 (forward-char)))))
2093 (defun f90-do-auto-fill ()
2094 "Break line if non-white characters beyond `fill-column'.
2095 Update keyword case first."
2096 (interactive "*")
2097 ;; Break line before or after last delimiter (non-word char) if
2098 ;; position is beyond fill-column.
2099 ;; Will not break **, //, or => (as specified by f90-no-break-re).
2100 (f90-update-line)
2101 ;; Need this for `f90-electric-insert' and other f90- callers.
2102 (unless (and (boundp 'comment-auto-fill-only-comments)
2103 comment-auto-fill-only-comments
2104 (not (f90-in-comment)))
2105 (while (> (current-column) fill-column)
2106 (let ((pos-mark (point-marker)))
2107 (move-to-column fill-column)
2108 (or (f90-in-string) (f90-find-breakpoint))
2109 (f90-break-line)
2110 (goto-char pos-mark)
2111 (set-marker pos-mark nil)))))
2113 (defun f90-join-lines (&optional arg)
2114 "Join current line to previous, fix whitespace, continuation, comments.
2115 With optional argument ARG, join current line to following line.
2116 Like `join-line', but handles F90 syntax."
2117 (interactive "*P")
2118 (beginning-of-line)
2119 (if arg (forward-line 1))
2120 (when (eq (preceding-char) ?\n)
2121 (skip-chars-forward " \t")
2122 (if (looking-at "&") (delete-char 1))
2123 (beginning-of-line)
2124 (delete-region (point) (1- (point)))
2125 (skip-chars-backward " \t")
2126 (and (eq (preceding-char) ?&) (delete-char -1))
2127 (and (f90-in-comment)
2128 (looking-at "[ \t]*!+")
2129 (replace-match ""))
2130 (or (f90-in-string)
2131 (fixup-whitespace))))
2133 (defun f90-fill-region (beg-region end-region)
2134 "Fill every line in region by forward parsing. Join lines if possible."
2135 (interactive "*r")
2136 (let ((end-region-mark (copy-marker end-region))
2137 (go-on t)
2138 f90-smart-end f90-auto-keyword-case auto-fill-function)
2139 (goto-char beg-region)
2140 (while go-on
2141 ;; Join as much as possible.
2142 (while (progn
2143 (end-of-line)
2144 (skip-chars-backward " \t")
2145 (eq (preceding-char) ?&))
2146 (f90-join-lines 'forward))
2147 ;; Chop the line if necessary.
2148 (while (> (save-excursion (end-of-line) (current-column))
2149 fill-column)
2150 (move-to-column fill-column)
2151 (f90-find-breakpoint)
2152 (f90-break-line 'no-update))
2153 (setq go-on (and (< (point) end-region-mark)
2154 (zerop (forward-line 1)))
2155 f90-cache-position (point)))
2156 (setq f90-cache-position nil)
2157 (set-marker end-region-mark nil)
2158 (if (featurep 'xemacs)
2159 (zmacs-deactivate-region)
2160 (deactivate-mark))))
2162 (defconst f90-end-block-optional-name
2163 '("program" "module" "subroutine" "function" "type")
2164 "Block types where including the name in the end statement is optional.")
2166 (defun f90-block-match (beg-block beg-name end-block end-name)
2167 "Match end-struct with beg-struct and complete end-block if possible.
2168 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
2169 BEG-NAME is the block start name (may be nil).
2170 END-BLOCK is the type of block as indicated at the end (may be nil).
2171 END-NAME is the block end name (may be nil).
2172 If the block type matches `f90-end-block-optional-name', do not add
2173 an end name if `f90-smart-end-names' is nil, but always update an
2174 incorrect end name if there already was one.
2175 Leave point at the end of line."
2176 ;; Hack to deal with the case when this is called from
2177 ;; f90-indent-region on a program block without an explicit PROGRAM
2178 ;; statement at the start. Should really be an error (?).
2179 (or beg-block (setq beg-block "program"))
2180 (search-forward "end" (line-end-position))
2181 (catch 'no-match
2182 (if (and end-block (f90-equal-symbols beg-block end-block))
2183 (search-forward end-block)
2184 (if end-block
2185 (progn
2186 (message "END %s does not match %s." end-block beg-block)
2187 (end-of-line)
2188 (throw 'no-match nil))
2189 (message "Inserting %s." beg-block)
2190 (insert (concat " " beg-block))))
2191 (if (f90-equal-symbols beg-name end-name)
2192 (and end-name (search-forward end-name))
2193 (cond ((and beg-name (not end-name))
2194 (unless (and (not f90-smart-end-names)
2195 (member-ignore-case beg-block
2196 f90-end-block-optional-name))
2197 (message "Inserting %s." beg-name)
2198 (insert (concat " " beg-name))))
2199 ((and beg-name end-name)
2200 (message "Replacing %s with %s." end-name beg-name)
2201 (search-forward end-name)
2202 (replace-match beg-name))
2203 ((and (not beg-name) end-name)
2204 (message "Deleting %s." end-name)
2205 (search-forward end-name)
2206 (replace-match ""))))
2207 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
2209 (defun f90-match-end ()
2210 "From an end block statement, find the corresponding block and name."
2211 (interactive)
2212 (let ((count 1)
2213 (top-of-window (window-start))
2214 (end-point (point))
2215 (case-fold-search t)
2216 matching-beg beg-name end-name beg-block end-block end-struct)
2217 ;; Check if in string in case using non-standard feature where
2218 ;; continued strings do not need "&" at start of continuations.
2219 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
2220 (unless (f90-in-string)
2221 (setq end-struct
2222 (f90-looking-at-program-block-end))))
2223 (setq end-block (car end-struct)
2224 end-name (cadr end-struct))
2225 (save-excursion
2226 (beginning-of-line)
2227 (while (and (> count 0)
2228 (not (= (line-beginning-position) (point-min))))
2229 (re-search-backward f90-blocks-re nil 'move)
2230 (beginning-of-line)
2231 ;; GM not a line number if continued line.
2232 ;;; (skip-chars-forward " \t")
2233 ;;; (skip-chars-forward "0-9")
2234 (skip-chars-forward " \t0-9")
2235 (cond ((or (f90-in-string) (f90-in-comment)))
2236 ((setq matching-beg
2238 (f90-looking-at-do)
2239 (f90-looking-at-if-then)
2240 (f90-looking-at-where-or-forall)
2241 (f90-looking-at-select-case)
2242 (f90-looking-at-type-like)
2243 (f90-looking-at-associate)
2244 (f90-looking-at-critical)
2245 (f90-looking-at-program-block-start)
2246 ;; Interpret a single END without a block
2247 ;; start to be the END of a program block
2248 ;; without an initial PROGRAM line.
2249 (if (= (line-beginning-position) (point-min))
2250 '("program" nil))))
2251 (setq count (1- count)))
2252 ((looking-at (concat "end[ \t]*" f90-blocks-re))
2253 (setq count (1+ count)))))
2254 (if (> count 0)
2255 (message "No matching beginning.")
2256 (f90-update-line)
2257 (if (eq f90-smart-end 'blink)
2258 (if (< (point) top-of-window)
2259 (message "Matches %s: %s"
2260 (what-line)
2261 (buffer-substring
2262 (line-beginning-position)
2263 (line-end-position)))
2264 (sit-for blink-matching-delay)))
2265 (setq beg-block (car matching-beg)
2266 beg-name (cadr matching-beg))
2267 (goto-char end-point)
2268 (beginning-of-line)
2269 (f90-block-match beg-block beg-name end-block end-name))))))
2271 (defun f90-insert-end ()
2272 "Insert a complete end statement matching beginning of present block."
2273 (interactive "*")
2274 (let ((f90-smart-end (or f90-smart-end 'blink)))
2275 (insert "end")
2276 (f90-indent-new-line)))
2278 ;; Abbrevs and keywords.
2280 (defun f90-abbrev-start ()
2281 "Typing \\=`\\[help-command] or \\=`? lists all the F90 abbrevs.
2282 Any other key combination is executed normally."
2283 (interactive "*")
2284 (self-insert-command 1)
2285 (when abbrev-mode
2286 (set-transient-map
2287 (let ((map (make-sparse-keymap)))
2288 (define-key map [??] 'f90-abbrev-help)
2289 (define-key map (vector help-char) 'f90-abbrev-help)
2290 map))))
2292 (defun f90-abbrev-help ()
2293 "List the currently defined abbrevs in F90 mode."
2294 (interactive)
2295 (message "Listing abbrev table...")
2296 (display-buffer (f90-prepare-abbrev-list-buffer))
2297 (message "Listing abbrev table...done"))
2299 (defun f90-prepare-abbrev-list-buffer ()
2300 "Create a buffer listing the F90 mode abbreviations."
2301 (with-current-buffer (get-buffer-create "*Abbrevs*")
2302 (erase-buffer)
2303 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
2304 (goto-char (point-min))
2305 (set-buffer-modified-p nil)
2306 (edit-abbrevs-mode))
2307 (get-buffer-create "*Abbrevs*"))
2309 (defun f90-upcase-keywords ()
2310 "Upcase all F90 keywords in the buffer."
2311 (interactive "*")
2312 (f90-change-keywords 'upcase-word))
2314 (defun f90-capitalize-keywords ()
2315 "Capitalize all F90 keywords in the buffer."
2316 (interactive "*")
2317 (f90-change-keywords 'capitalize-word))
2319 (defun f90-downcase-keywords ()
2320 "Downcase all F90 keywords in the buffer."
2321 (interactive "*")
2322 (f90-change-keywords 'downcase-word))
2324 (defun f90-upcase-region-keywords (beg end)
2325 "Upcase all F90 keywords in the region."
2326 (interactive "*r")
2327 (f90-change-keywords 'upcase-word beg end))
2329 (defun f90-capitalize-region-keywords (beg end)
2330 "Capitalize all F90 keywords in the region."
2331 (interactive "*r")
2332 (f90-change-keywords 'capitalize-word beg end))
2334 (defun f90-downcase-region-keywords (beg end)
2335 "Downcase all F90 keywords in the region."
2336 (interactive "*r")
2337 (f90-change-keywords 'downcase-word beg end))
2339 ;; Change the keywords according to argument.
2340 (defun f90-change-keywords (change-word &optional beg end)
2341 "Change the case of F90 keywords in the region (if specified) or buffer.
2342 CHANGE-WORD should be one of `upcase-word', `downcase-word', `capitalize-word'."
2343 (save-excursion
2344 (setq beg (or beg (point-min))
2345 end (or end (point-max)))
2346 (let ((keyword-re
2347 (concat "\\("
2348 f90-keywords-re "\\|" f90-procedures-re "\\|"
2349 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
2350 (ref-point (point-min))
2351 (modified (buffer-modified-p))
2352 state saveword back-point)
2353 (goto-char beg)
2354 (unwind-protect
2355 (while (re-search-forward keyword-re end t)
2356 (unless (progn
2357 (setq state (parse-partial-sexp ref-point (point)))
2358 (or (nth 3 state) (nth 4 state)
2359 ;; GM f90-directive-comment-re?
2360 (save-excursion ; check for cpp directive
2361 (beginning-of-line)
2362 (skip-chars-forward " \t0-9")
2363 (looking-at "#"))))
2364 (setq ref-point (point)
2365 ;; FIXME this does not work for constructs with
2366 ;; embedded space, eg "sync all".
2367 back-point (save-excursion (backward-word-strictly 1)
2368 (point))
2369 saveword (buffer-substring back-point ref-point))
2370 (funcall change-word -1)
2371 (or (string= saveword (buffer-substring back-point ref-point))
2372 (setq modified t))))
2373 (or modified (restore-buffer-modified-p nil))))))
2376 (defun f90-current-defun ()
2377 "Function to use for `add-log-current-defun-function' in F90 mode."
2378 (save-excursion
2379 (nth 1 (f90-beginning-of-subprogram))))
2381 (defun f90-backslash-not-special (&optional all)
2382 "Make the backslash character (\\) be non-special in the current buffer.
2383 With optional argument ALL, change the default for all present
2384 and future F90 buffers. F90 mode normally treats backslash as an
2385 escape character."
2386 (or (derived-mode-p 'f90-mode)
2387 (user-error "This function should only be used in F90 buffers"))
2388 (when (equal (char-syntax ?\\ ) ?\\ )
2389 (or all (set-syntax-table (copy-syntax-table (syntax-table))))
2390 (modify-syntax-entry ?\\ ".")))
2393 (provide 'f90)
2395 ;;; f90.el ends here