f90.el: Add some F2008 keywords
[emacs.git] / lisp / progmodes / f90.el
blobb923819ebb3814a85dc7e1fb4a512f2c84e796c7
1 ;;; f90.el --- Fortran-90 mode (free format) -*- lexical-binding: t -*-
3 ;; Copyright (C) 1995-1997, 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Torbjörn Einarsson <Torbjorn.Einarsson@era.ericsson.se>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
7 ;; Keywords: fortran, f90, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Major mode for editing F90 programs in FREE FORMAT.
27 ;; The minor language revision F95 is also supported (with font-locking).
28 ;; Some/many (?) aspects of F2003 are supported.
29 ;; Some aspects of F2008 are supported.
31 ;; Knows about continuation lines, named structured statements, and other
32 ;; features in F90 including HPF (High Performance Fortran) structures.
33 ;; The basic feature provides accurate indentation of F90 programs.
34 ;; In addition, there are many more features like automatic matching of all
35 ;; end statements, an auto-fill function to break long lines, a join-lines
36 ;; function which joins continued lines, etc.
38 ;; To facilitate typing, a fairly complete list of abbreviations is provided.
39 ;; All abbreviations begin with the backquote character "`"
40 ;; For example, `i expands to integer (if abbrev-mode is on).
42 ;; There are two separate features for altering the appearance of code:
43 ;; 1) Upcasing or capitalizing of all keywords.
44 ;; 2) Colors/fonts using font-lock-mode.
45 ;; Automatic upcase or downcase of keywords is controlled by the variable
46 ;; f90-auto-keyword-case.
48 ;; The indentations of lines starting with ! is determined by the first of the
49 ;; following matches (values in the left column are the defaults):
51 ;; start-string/regexp indent variable holding start-string/regexp
52 ;; !!! 0
53 ;; !hpf\\$ (re) 0 f90-directive-comment-re
54 ;; !!$ 0 f90-comment-region
55 ;; ! (re) as code f90-indented-comment-re
56 ;; default comment-column
58 ;; Ex: Here is the result of 3 different settings of f90-indented-comment-re
59 ;; f90-indented-comment-re !-indentation !!-indentation
60 ;; ! as code as code
61 ;; !! comment-column as code
62 ;; ![^!] as code comment-column
63 ;; Trailing comments are indented to comment-column with indent-for-comment.
64 ;; The function f90-comment-region toggles insertion of
65 ;; the variable f90-comment-region in every line of the region.
67 ;; One common convention for free vs. fixed format is that free format files
68 ;; have the ending .f90 or .f95 while fixed format files have the ending .f.
69 ;; Emacs automatically loads Fortran files in the appropriate mode based
70 ;; on extension. You can modify this by adjusting the variable auto-mode-alist.
71 ;; For example:
72 ;; (add-to-list 'auto-mode-alist '("\\.f\\'" . f90-mode))
74 ;; Once you have entered f90-mode, you 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 (defconst f90-type-def-re
899 ;; type word
900 ;; type :: word
901 ;; type, stuff :: word
902 ;; type, bind(c) :: word
903 ;; NOT "type ("
904 "\\_<\\(type\\)\\_>\\(?:\\(?:[^()\n]*\\|\
905 .*,[ \t]*bind[ \t]*([ \t]*c[ \t]*)[ \t]*\\)::\\)?[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
906 "Regexp matching the definition of a derived type.")
908 (defconst f90-typeis-re
909 "\\_<\\(class\\|type\\)[ \t]*is[ \t]*("
910 "Regexp matching a CLASS/TYPE IS statement.")
912 (defconst f90-no-break-re
913 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=" "(/" "/)") 'paren)
914 "Regexp specifying two-character tokens not to split when breaking lines.
915 Each token has one or more of the characters from `f90-break-delimiters'.
916 Note that if only one of the characters is from that variable,
917 then the presence of the token here allows a line-break before or
918 after the other character, where a break would not normally be
919 allowed. This minor issue currently only affects \"(/\" and \"/)\".")
921 (defvar f90-cache-position nil
922 "Temporary position used to speed up region operations.")
923 (make-variable-buffer-local 'f90-cache-position)
926 ;; Hideshow support.
927 (defconst f90-end-block-re
928 (concat "^[ \t0-9]*\\_<end[ \t]*"
929 (regexp-opt '("do" "if" "forall" "function" "interface"
930 "module" "program" "select" "subroutine"
931 "type" "where" "enum" "associate" "submodule"
932 "block" "critical") t)
933 "\\_>")
934 "Regexp matching the end of an F90 \"block\", from the line start.
935 Used in the F90 entry in `hs-special-modes-alist'.")
937 ;; Ignore the fact that FUNCTION, SUBROUTINE, WHERE, FORALL have a
938 ;; following "(". DO, CASE, IF can have labels.
939 (defconst f90-start-block-re
940 (concat
941 "^[ \t0-9]*" ; statement number
942 "\\(\\("
943 "\\(\\(?:\\sw\\|\\s_\\)+[ \t]*:[ \t]*\\)?" ; structure label
944 "\\(do\\|select[ \t]*\\(case\\|type\\)\\|"
945 ;; See comments in fortran-start-block-re for the problems of IF.
946 "if[ \t]*(\\(.*\\|"
947 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\_>\\)\\)\\)\\_<then\\|"
948 ;; Distinguish WHERE block from isolated WHERE.
949 "\\(where\\|forall\\)[ \t]*(.*)[ \t]*\\(!\\|$\\)\\)\\)"
950 "\\|"
951 ;; Avoid F2003 "type is" in "select type",
952 ;; and also variables of derived type "type (foo)".
953 ;; "type, foo" must be a block (?).
954 "type[ \t,]\\("
955 "[^i(!\n\"\& \t]\\|" ; not-i(
956 "i[^s!\n\"\& \t]\\|" ; i not-s
957 "is\\(?:\\sw\\|\\s_\\)\\)\\|"
958 ;; "abstract interface" is F2003; "submodule" is F2008.
959 "program\\|\\(?:abstract[ \t]*\\)?interface\\|\\(?:sub\\)?module\\|"
960 ;; "enum", but not "enumerator".
961 "function\\|subroutine\\|enum[^e]\\|associate\\|block\\|critical"
962 "\\)"
963 "[ \t]*")
964 "Regexp matching the start of an F90 \"block\", from the line start.
965 A simple regexp cannot do this in fully correct fashion, so this
966 tries to strike a compromise between complexity and flexibility.
967 Used in the F90 entry in `hs-special-modes-alist'.")
969 ;; hs-special-modes-alist is autoloaded.
970 (add-to-list 'hs-special-modes-alist
971 `(f90-mode ,f90-start-block-re ,f90-end-block-re
972 "!" f90-end-of-block nil))
975 ;; Imenu support.
976 ;; FIXME trivial to extend this to enum. Worth it?
977 (defun f90-imenu-type-matcher ()
978 "Search backward for the start of a derived type.
979 Set subexpression 1 in the match-data to the name of the type."
980 (let (found)
981 (while (and (re-search-backward "^[ \t0-9]*type[ \t]*" nil t)
982 (not (setq found
983 (save-excursion
984 (goto-char (match-end 0))
985 (unless (looking-at "\\(is\\_>\\|(\\)")
986 (or (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
987 (re-search-forward
988 "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
989 (line-end-position) t))))))))
990 found))
992 (defvar f90-imenu-generic-expression
993 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
994 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]")
995 ;; (not-ib "[^i(!\n\"\& \t]") (not-s "[^s!\n\"\& \t]")
997 `((nil "^[ \t0-9]*program[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)" 1)
998 ("Submodules" "^[ \t0-9]*submodule[ \t]*([^)\n]+)[ \t]*\
999 \\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*\\(!\\|$\\)" 1)
1000 ("Modules" "^[ \t0-9]*module[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*\\(!\\|$\\)" 1)
1001 ("Types" f90-imenu-type-matcher 1)
1002 ;; Does not handle: "type[, stuff] :: foo".
1003 ;;(format "^[ \t0-9]*type[ \t]+\\(\\(%s\\|i%s\\|is\\(?:\\sw\\|\\s_\\)\\)\\(?:\\sw\\|\\s_\\)*\\)"
1004 ;; not-ib not-s)
1005 ;;1)
1006 ;; Can't get the subexpression numbers to match in the two branches.
1007 ;; FIXME: Now with \(?N:..\) we can get the numbers to match!
1008 ;;(format "^[ \t0-9]*type\\([ \t]*,.*\\(::\\)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)\\|[ \t]+\\(\\(%s\\|i%s\\|is\\(?:\\sw\\|\\s_\\)\\)\\(?:\\sw\\|\\s_\\)*\\)\\)" not-ib not-s)
1009 ;;3)
1010 ("Procedures"
1011 ,(concat
1012 "^[ \t0-9]*"
1013 "\\("
1014 ;; At least three non-space characters before function/subroutine.
1015 ;; Check that the last three non-space characters do not spell E N D.
1016 "[^!\"\&\n]*\\("
1017 not-e good-char good-char "\\|"
1018 good-char not-n good-char "\\|"
1019 good-char good-char not-d "\\)"
1020 "\\|"
1021 ;; Less than three non-space characters before function/subroutine.
1022 good-char "?" good-char "?"
1023 "\\)"
1024 "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)")
1025 4)))
1026 "Value for `imenu-generic-expression' in F90 mode.")
1028 (defun f90-add-imenu-menu ()
1029 "Add an imenu menu to the menubar."
1030 (interactive)
1031 (if (lookup-key (current-local-map) [menu-bar index])
1032 (message "%s" "F90-imenu already exists.")
1033 (imenu-add-to-menubar "F90-imenu")
1034 (redraw-frame)))
1037 ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
1038 (define-abbrev-table 'f90-mode-abbrev-table
1039 (mapcar (lambda (e) (list (car e) (cdr e) nil :system t))
1040 '(("`al" . "allocate" )
1041 ("`ab" . "allocatable" )
1042 ("`ai" . "abstract interface")
1043 ("`as" . "assignment" )
1044 ("`asy" . "asynchronous" )
1045 ("`ba" . "backspace" )
1046 ("`bd" . "block data" )
1047 ("`bl" . "block" )
1048 ("`c" . "character" )
1049 ("`cl" . "close" )
1050 ("`cm" . "common" )
1051 ("`cx" . "complex" )
1052 ("`cn" . "contains" )
1053 ("`cr" . "critical" )
1054 ("`cy" . "cycle" )
1055 ("`de" . "deallocate" )
1056 ("`df" . "define" )
1057 ("`di" . "dimension" )
1058 ("`dp" . "double precision")
1059 ("`dw" . "do while" )
1060 ("`el" . "else" )
1061 ("`eli" . "else if" )
1062 ("`elw" . "elsewhere" )
1063 ("`em" . "elemental" )
1064 ("`e" . "enumerator" )
1065 ("`eq" . "equivalence" )
1066 ("`ex" . "external" )
1067 ("`ey" . "entry" )
1068 ("`fl" . "forall" )
1069 ("`fo" . "format" )
1070 ("`fu" . "function" )
1071 ("`fa" . ".false." )
1072 ("`im" . "implicit none")
1073 ("`in" . "include" )
1074 ("`i" . "integer" )
1075 ("`it" . "intent" )
1076 ("`if" . "interface" )
1077 ("`lo" . "logical" )
1078 ("`mo" . "module" )
1079 ("`na" . "namelist" )
1080 ("`nu" . "nullify" )
1081 ("`op" . "optional" )
1082 ("`pa" . "parameter" )
1083 ("`po" . "pointer" )
1084 ("`pr" . "print" )
1085 ("`pi" . "private" )
1086 ("`pm" . "program" )
1087 ("`pr" . "protected" )
1088 ("`pu" . "public" )
1089 ("`r" . "real" )
1090 ("`rc" . "recursive" )
1091 ("`rt" . "return" )
1092 ("`rw" . "rewind" )
1093 ("`se" . "select" )
1094 ("`sq" . "sequence" )
1095 ("`su" . "subroutine" )
1096 ("`ta" . "target" )
1097 ("`tr" . ".true." )
1098 ("`t" . "type" )
1099 ("`vo" . "volatile" )
1100 ("`wh" . "where" )
1101 ("`wr" . "write" )))
1102 "Abbrev table for F90 mode."
1103 ;; Accept ` as the first char of an abbrev. Also allow _ in abbrevs.
1104 :regexp "\\(?:[^[:word:]_`]\\|^\\)\\(`?[[:word:]_]+\\)[^[:word:]_]*")
1106 ;;;###autoload
1107 (define-derived-mode f90-mode prog-mode "F90"
1108 "Major mode for editing Fortran 90,95 code in free format.
1109 For fixed format code, use `fortran-mode'.
1111 \\[f90-indent-line] indents the current line.
1112 \\[f90-indent-new-line] indents current line and creates a new\
1113 indented line.
1114 \\[f90-indent-subprogram] indents the current subprogram.
1116 Type `? or `\\[help-command] to display a list of built-in\
1117 abbrevs for F90 keywords.
1119 Key definitions:
1120 \\{f90-mode-map}
1122 Variables controlling indentation style and extra features:
1124 `f90-do-indent'
1125 Extra indentation within do blocks (default 3).
1126 `f90-if-indent'
1127 Extra indentation within if/select/where/forall blocks (default 3).
1128 `f90-type-indent'
1129 Extra indentation within type/enum/interface/block-data blocks (default 3).
1130 `f90-program-indent'
1131 Extra indentation within program/module/subroutine/function blocks
1132 (default 2).
1133 `f90-associate-indent'
1134 Extra indentation within associate blocks (default 2).
1135 `f90-critical-indent'
1136 Extra indentation within critical/block blocks (default 2).
1137 `f90-continuation-indent'
1138 Extra indentation applied to continuation lines (default 5).
1139 `f90-comment-region'
1140 String inserted by function \\[f90-comment-region] at start of each
1141 line in region (default \"!!!$\").
1142 `f90-indented-comment-re'
1143 Regexp determining the type of comment to be intended like code
1144 (default \"!\").
1145 `f90-directive-comment-re'
1146 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
1147 (default \"!hpf\\\\$\").
1148 `f90-break-delimiters'
1149 Regexp holding list of delimiters at which lines may be broken
1150 (default \"[-+*/><=,% \\t]\").
1151 `f90-break-before-delimiters'
1152 Non-nil causes `f90-do-auto-fill' to break lines before delimiters
1153 (default t).
1154 `f90-beginning-ampersand'
1155 Automatic insertion of \& at beginning of continuation lines (default t).
1156 `f90-smart-end'
1157 From an END statement, check and fill the end using matching block start.
1158 Allowed values are `blink', `no-blink', and nil, which determine
1159 whether to blink the matching beginning (default `blink').
1160 `f90-auto-keyword-case'
1161 Automatic change of case of keywords (default nil).
1162 The possibilities are `downcase-word', `upcase-word', `capitalize-word'.
1163 `f90-leave-line-no'
1164 Do not left-justify line numbers (default nil).
1166 Turning on F90 mode calls the value of the variable `f90-mode-hook'
1167 with no args, if that value is non-nil."
1168 :group 'f90
1169 :abbrev-table f90-mode-abbrev-table
1170 (set (make-local-variable 'indent-line-function) 'f90-indent-line)
1171 (set (make-local-variable 'indent-region-function) 'f90-indent-region)
1172 (set (make-local-variable 'comment-start) "!")
1173 (set (make-local-variable 'comment-start-skip) "!+ *")
1174 (set (make-local-variable 'comment-indent-function) 'f90-comment-indent)
1175 (set (make-local-variable 'abbrev-all-caps) t)
1176 (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
1177 (setq indent-tabs-mode nil) ; auto buffer local
1178 (set (make-local-variable 'font-lock-defaults)
1179 '((f90-font-lock-keywords f90-font-lock-keywords-1
1180 f90-font-lock-keywords-2
1181 f90-font-lock-keywords-3
1182 f90-font-lock-keywords-4)
1183 nil t))
1184 (set (make-local-variable 'imenu-case-fold-search) t)
1185 (set (make-local-variable 'imenu-generic-expression)
1186 f90-imenu-generic-expression)
1187 (set (make-local-variable 'beginning-of-defun-function)
1188 'f90-beginning-of-subprogram)
1189 (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
1190 (set (make-local-variable 'add-log-current-defun-function)
1191 #'f90-current-defun))
1194 ;; Inline-functions.
1195 (defsubst f90-in-string ()
1196 "Return non-nil if point is inside a string.
1197 Checks from `point-min', or `f90-cache-position', if that is non-nil
1198 and lies before point."
1199 (let ((beg-pnt
1200 (if (and f90-cache-position (> (point) f90-cache-position))
1201 f90-cache-position
1202 (point-min))))
1203 (nth 3 (parse-partial-sexp beg-pnt (point)))))
1205 (defsubst f90-in-comment ()
1206 "Return non-nil if point is inside a comment.
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 4 (parse-partial-sexp beg-pnt (point)))))
1215 (defsubst f90-line-continued ()
1216 "Return t if the current line is a continued one.
1217 This includes comment or preprocessor lines embedded in continued lines,
1218 but not the last line of a continued statement."
1219 (save-excursion
1220 (beginning-of-line)
1221 (while (and (looking-at "[ \t]*\\([!#]\\|$\\)") (zerop (forward-line -1))))
1222 (end-of-line)
1223 (while (f90-in-comment)
1224 (search-backward "!" (line-beginning-position))
1225 (skip-chars-backward "!"))
1226 (skip-chars-backward " \t")
1227 (= (preceding-char) ?&)))
1229 ;; GM this is not right, eg a continuation line starting with a number.
1230 ;; Need f90-code-start-position function.
1231 ;; And yet, things seems to work with this...
1232 ;; cf f90-indent-line
1233 ;; (beginning-of-line) ; digits after & \n are not line-nos
1234 ;; (if (not (save-excursion (and (f90-previous-statement)
1235 ;; (f90-line-continued))))
1236 ;; (f90-indent-line-no)
1237 (defsubst f90-current-indentation ()
1238 "Return indentation of current line.
1239 Line-numbers are considered whitespace characters."
1240 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
1242 (defsubst f90-indent-to (col &optional no-line-number)
1243 "Indent current line to column COL.
1244 If optional argument NO-LINE-NUMBER is nil, jump over a possible
1245 line-number before indenting."
1246 (beginning-of-line)
1247 (or no-line-number
1248 (skip-chars-forward " \t0-9"))
1249 (delete-horizontal-space)
1250 ;; Leave >= 1 space after line number.
1251 (indent-to col (if (zerop (current-column)) 0 1)))
1253 (defsubst f90-get-present-comment-type ()
1254 "If point lies within a comment, return the string starting the comment.
1255 For example, \"!\" or \"!!\", followed by the appropriate amount of
1256 whitespace, if any."
1257 ;; Include the whitespace for consistent auto-filling of comment blocks.
1258 (save-excursion
1259 (when (f90-in-comment)
1260 (beginning-of-line)
1261 (re-search-forward "!+[ \t]*" (line-end-position))
1262 (while (f90-in-string)
1263 (re-search-forward "!+[ \t]*" (line-end-position)))
1264 (match-string-no-properties 0))))
1266 (defsubst f90-equal-symbols (a b)
1267 "Compare strings A and B neglecting case and allowing for nil value."
1268 (equal (if a (downcase a) nil)
1269 (if b (downcase b) nil)))
1271 (defsubst f90-looking-at-do ()
1272 "Return (\"do\" NAME) if a do statement starts after point.
1273 NAME is nil if the statement has no label."
1274 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\_>")
1275 (list (match-string 3) (match-string 2))))
1277 (defsubst f90-looking-at-select-case ()
1278 "Return (\"select\" NAME) if a select statement starts after point.
1279 NAME is nil if the statement has no label."
1280 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\
1281 \\(select\\)[ \t]*\\(case\\|type\\)[ \t]*(")
1282 (list (match-string 3) (match-string 2))))
1284 (defsubst f90-looking-at-if-then ()
1285 "Return (\"if\" NAME) if an if () then statement starts after point.
1286 NAME is nil if the statement has no label."
1287 (save-excursion
1288 (when (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\_>")
1289 (let ((struct (match-string 3))
1290 (label (match-string 2))
1291 (pos (scan-lists (point) 1 0)))
1292 (and pos (goto-char pos))
1293 (skip-chars-forward " \t")
1294 (if (or (looking-at "then\\_>")
1295 (when (f90-line-continued)
1296 (f90-next-statement)
1297 (skip-chars-forward " \t0-9&")
1298 (looking-at "then\\_>")))
1299 (list struct label))))))
1301 ;; FIXME label?
1302 (defsubst f90-looking-at-associate ()
1303 "Return (\"associate\") if an associate block starts after point."
1304 (if (looking-at "\\_<\\(associate\\)[ \t]*(")
1305 (list (match-string 1))))
1307 (defsubst f90-looking-at-critical ()
1308 "Return (KIND NAME) if a critical or block block starts after point."
1309 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(critical\\|block\\)\\_>")
1310 (let ((struct (match-string 3))
1311 (label (match-string 2)))
1312 (if (or (not (string-equal "block" struct))
1313 (save-excursion
1314 (skip-chars-forward " \t")
1315 (not (looking-at "data\\_>"))))
1316 (list struct label)))))
1318 (defsubst f90-looking-at-end-critical ()
1319 "Return non-nil if a critical or block block ends after point."
1320 (if (looking-at "end[ \t]*\\(critical\\|block\\)\\_>")
1321 (or (not (string-equal "block" (match-string 1)))
1322 (save-excursion
1323 (skip-chars-forward " \t")
1324 (not (looking-at "data\\_>"))))))
1326 (defsubst f90-looking-at-where-or-forall ()
1327 "Return (KIND NAME) if a where or forall block starts after point.
1328 NAME is nil if the statement has no label."
1329 (save-excursion
1330 (when (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\
1331 \\(where\\|forall\\)\\_>")
1332 (let ((struct (match-string 3))
1333 (label (match-string 2))
1334 (pos (scan-lists (point) 1 0)))
1335 (and pos (goto-char pos))
1336 (skip-chars-forward " \t")
1337 (if (looking-at "\\(!\\|$\\)") (list struct label))))))
1339 (defsubst f90-looking-at-type-like ()
1340 "Return (KIND NAME) if a type/enum/interface/block-data starts after point.
1341 NAME is non-nil only for type and certain interfaces."
1342 (cond
1343 ((save-excursion
1344 (and (looking-at "\\_<type\\_>[ \t]*")
1345 (goto-char (match-end 0))
1346 (not (looking-at "\\(is\\_>\\|(\\)"))
1347 (or (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
1348 (re-search-forward "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
1349 (line-end-position) t))))
1350 (list "type" (match-string 1)))
1351 ;;; ((and (not (looking-at f90-typeis-re))
1352 ;;; (looking-at f90-type-def-re))
1353 ;;; (list (match-string 1) (match-string 2)))
1354 ((looking-at "\\_<\\(interface\\)\\_>[ \t]*")
1355 (list (match-string 1)
1356 (save-excursion
1357 (goto-char (match-end 0))
1358 (if (or (looking-at "\\(operator\\|assignment\\|read\\|\
1359 write\\)[ \t]*([^)\n]*)")
1360 (looking-at "\\(?:\\sw\\|\\s_\\)+"))
1361 (match-string 0)))))
1362 ((looking-at "\\(enum\\|block[ \t]*data\\)\\_>")
1363 (list (match-string 1) nil))
1364 ((looking-at "abstract[ \t]*\\(interface\\)\\_>")
1365 (list (match-string 1) nil))))
1367 (defsubst f90-looking-at-program-block-start ()
1368 "Return (KIND NAME) if a program block with name NAME starts after point."
1369 ;;;NAME is nil for an un-named main PROGRAM block."
1370 (cond
1371 ((looking-at "\\(program\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
1372 (list (match-string 1) (match-string 2)))
1373 ((and (not (looking-at "module[ \t]*procedure\\_>"))
1374 (looking-at "\\(module\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>"))
1375 (list (match-string 1) (match-string 2)))
1376 ((looking-at "\\(submodule\\)[ \t]*([^)\n]+)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
1377 (list (match-string 1) (match-string 2)))
1378 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
1379 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
1380 \\(\\(?:\\sw\\|\\s_\\)+\\)"))
1381 (list (match-string 1) (match-string 2)))))
1382 ;; Following will match an un-named main program block; however
1383 ;; one needs to check if there is an actual PROGRAM statement after
1384 ;; point (and before any END program). Adding this will require
1385 ;; change to eg f90-calculate-indent.
1386 ;;; ((save-excursion
1387 ;;; (not (f90-previous-statement)))
1388 ;;; '("program" nil))))
1390 (defsubst f90-looking-at-program-block-end ()
1391 "Return (KIND NAME) if a block with name NAME ends after point."
1392 (cond ((looking-at "end[ \t]*\\(interface\\)[ \t]*\\(\
1393 \\(?:assignment\\|operator\\|read\\|write\\)[ \t]*([^)\n]*)\\)")
1394 (list (match-string 1) (match-string 2)))
1395 ((looking-at (concat "end[ \t]*" f90-blocks-re
1396 "?\\([ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?\\_>"))
1397 (list (match-string 1) (match-string 3)))))
1399 (defsubst f90-comment-indent ()
1400 "Return the indentation to be used for a comment starting at point.
1401 Used for `comment-indent-function' by F90 mode.
1402 \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
1403 `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
1404 All others return `comment-column', leaving at least one space after code."
1405 (cond ((looking-at "!!!") 0)
1406 ((and f90-directive-comment-re
1407 (looking-at f90-directive-comment-re)) 0)
1408 ((looking-at (regexp-quote f90-comment-region)) 0)
1409 ((and (looking-at f90-indented-comment-re)
1410 ;; Don't attempt to indent trailing comment as code.
1411 (save-excursion
1412 (skip-chars-backward " \t")
1413 (bolp)))
1414 (f90-calculate-indent))
1415 (t (save-excursion
1416 (skip-chars-backward " \t")
1417 (max (if (bolp) 0 (1+ (current-column))) comment-column)))))
1419 (defsubst f90-present-statement-cont ()
1420 "Return continuation properties of present statement.
1421 Possible return values are:
1422 single - statement is not continued.
1423 begin - current line is the first in a continued statement.
1424 end - current line is the last in a continued statement
1425 middle - current line is neither first nor last in a continued statement.
1426 Comment lines embedded amongst continued lines return 'middle."
1427 (let (pcont cont)
1428 (save-excursion
1429 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
1430 (setq cont (f90-line-continued))
1431 (cond ((and (not pcont) (not cont)) 'single)
1432 ((and (not pcont) cont) 'begin)
1433 ((and pcont (not cont)) 'end)
1434 ((and pcont cont) 'middle)
1435 (t (error "The impossible occurred")))))
1437 (defsubst f90-indent-line-no ()
1438 "If `f90-leave-line-no' is nil, left-justify a line number.
1439 Leaves point at the first non-blank character after the line number.
1440 Call from beginning of line."
1441 (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
1442 (delete-horizontal-space))
1443 (skip-chars-forward " \t0-9"))
1445 (defsubst f90-no-block-limit ()
1446 "Return nil if point is at the edge of a code block.
1447 Searches line forward for \"function\" or \"subroutine\",
1448 if all else fails."
1449 (save-excursion
1450 (not (or (looking-at "end")
1451 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
1452 \\|select[ \t]*\\(case\\|type\\)\\|case\\|where\\|forall\\|\
1453 block\\|critical\\)\\_>")
1454 (looking-at "\\(program\\|\\(?:sub\\)?module\\|\
1455 \\(?:abstract[ \t]*\\)?interface\\|block[ \t]*data\\)\\_>")
1456 (looking-at "\\(contains\\|\\(?:\\sw\\|\\s_\\)+[ \t]*:\\)")
1457 (looking-at f90-type-def-re)
1458 (re-search-forward "\\(function\\|subroutine\\)"
1459 (line-end-position) t)))))
1461 (defsubst f90-update-line ()
1462 "Change case of current line as per `f90-auto-keyword-case'."
1463 (if f90-auto-keyword-case
1464 (f90-change-keywords f90-auto-keyword-case
1465 (line-beginning-position) (line-end-position))))
1467 (defun f90-electric-insert (&optional arg)
1468 "Change keyword case and auto-fill line as operators are inserted."
1469 (interactive "*p")
1470 (self-insert-command arg)
1471 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1472 (f90-update-line)))
1474 ;; Behave like self-insert-command for delete-selection-mode (bug#5593).
1475 (put 'f90-electric-insert 'delete-selection t)
1477 (defun f90-get-correct-indent ()
1478 "Get correct indent for a line starting with line number.
1479 Does not check type and subprogram indentation."
1480 (let ((epnt (line-end-position)) icol)
1481 (save-excursion
1482 (while (and (f90-previous-statement)
1483 (or (memq (f90-present-statement-cont) '(middle end))
1484 (looking-at "[ \t]*[0-9]"))))
1485 (setq icol (current-indentation))
1486 (beginning-of-line)
1487 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1488 (line-end-position) t)
1489 (beginning-of-line)
1490 (skip-chars-forward " \t")
1491 (cond ((f90-looking-at-do)
1492 (setq icol (+ icol f90-do-indent)))
1493 ((or (f90-looking-at-if-then)
1494 (f90-looking-at-where-or-forall)
1495 (f90-looking-at-select-case))
1496 (setq icol (+ icol f90-if-indent)))
1497 ;; FIXME this makes no sense, because this section/function is
1498 ;; only for if/do/select/where/forall ?
1499 ((f90-looking-at-associate)
1500 (setq icol (+ icol f90-associate-indent))))
1501 (end-of-line))
1502 (while (re-search-forward
1503 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1504 (beginning-of-line)
1505 (skip-chars-forward " \t0-9")
1506 (cond ((f90-looking-at-do)
1507 (setq icol (+ icol f90-do-indent)))
1508 ((or (f90-looking-at-if-then)
1509 (f90-looking-at-where-or-forall)
1510 (f90-looking-at-select-case))
1511 (setq icol (+ icol f90-if-indent)))
1512 ;; FIXME this makes no sense, because this section/function is
1513 ;; only for if/do/select/where/forall ?
1514 ((f90-looking-at-associate)
1515 (setq icol (+ icol f90-associate-indent)))
1516 ((looking-at f90-end-if-re)
1517 (setq icol (- icol f90-if-indent)))
1518 ((looking-at f90-end-associate-re)
1519 (setq icol (- icol f90-associate-indent)))
1520 ((f90-looking-at-end-critical)
1521 (setq icol (- icol f90-critical-indent)))
1522 ((looking-at "end[ \t]*do\\_>")
1523 (setq icol (- icol f90-do-indent))))
1524 (end-of-line))
1525 icol)))
1527 (defun f90-calculate-indent ()
1528 "Calculate the indent column based on previous statements."
1529 (interactive)
1530 (let (icol cont (case-fold-search t) (pnt (point)))
1531 (save-excursion
1532 (if (not (f90-previous-statement))
1533 ;; If f90-previous-statement returns nil, we must have been
1534 ;; called from on or before the first line of the first statement.
1535 (setq icol (if (or (save-excursion
1536 (goto-char pnt)
1537 (beginning-of-line)
1538 ;; Preprocessor line before code statement.
1539 (looking-at "[ \t]*#"))
1540 (progn
1541 ;; f90-previous-statement has moved us over
1542 ;; comment/blank lines, so we need to get
1543 ;; back to the first code statement.
1544 (when (looking-at "[ \t]*\\([!#]\\|$\\)")
1545 (f90-next-statement))
1546 (skip-chars-forward " \t0-9")
1547 (f90-looking-at-program-block-start)))
1549 ;; No explicit PROGRAM start statement.
1550 f90-program-indent))
1551 (setq cont (f90-present-statement-cont))
1552 (if (eq cont 'end)
1553 (while (not (eq 'begin (f90-present-statement-cont)))
1554 (f90-previous-statement)))
1555 (cond ((eq cont 'begin)
1556 (setq icol (+ (f90-current-indentation)
1557 f90-continuation-indent)))
1558 ((eq cont 'middle) (setq icol (current-indentation)))
1559 (t (setq icol (f90-current-indentation))
1560 (skip-chars-forward " \t")
1561 (if (looking-at "[0-9]")
1562 (setq icol (f90-get-correct-indent))
1563 (cond ((or (f90-looking-at-if-then)
1564 (f90-looking-at-where-or-forall)
1565 (f90-looking-at-select-case)
1566 (looking-at f90-else-like-re))
1567 (setq icol (+ icol f90-if-indent)))
1568 ((f90-looking-at-do)
1569 (setq icol (+ icol f90-do-indent)))
1570 ((f90-looking-at-type-like)
1571 (setq icol (+ icol f90-type-indent)))
1572 ((f90-looking-at-associate)
1573 (setq icol (+ icol f90-associate-indent)))
1574 ((f90-looking-at-critical)
1575 (setq icol (+ icol f90-critical-indent)))
1576 ((or (f90-looking-at-program-block-start)
1577 (looking-at "contains[ \t]*\\($\\|!\\)"))
1578 (setq icol (+ icol f90-program-indent)))))
1579 (goto-char pnt)
1580 (beginning-of-line)
1581 (cond ((looking-at "[ \t]*$"))
1582 ((looking-at "[ \t]*#") ; check for cpp directive
1583 (setq icol 0))
1585 (skip-chars-forward " \t0-9")
1586 (cond ((or (looking-at f90-else-like-re)
1587 (looking-at f90-end-if-re))
1588 (setq icol (- icol f90-if-indent)))
1589 ((looking-at "end[ \t]*do\\_>")
1590 (setq icol (- icol f90-do-indent)))
1591 ((looking-at f90-end-type-re)
1592 (setq icol (- icol f90-type-indent)))
1593 ((looking-at f90-end-associate-re)
1594 (setq icol (- icol f90-associate-indent)))
1595 ((f90-looking-at-end-critical)
1596 (setq icol (- icol f90-critical-indent)))
1597 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1598 (f90-looking-at-program-block-end))
1599 (setq icol (- icol f90-program-indent))))))))))
1600 icol))
1602 (defun f90-previous-statement ()
1603 "Move point to beginning of the previous F90 statement.
1604 If no previous statement is found (i.e. if called from the first
1605 statement in the buffer), move to the start of the buffer and
1606 return nil. A statement is a line which is neither blank nor a
1607 comment."
1608 (interactive)
1609 (let (not-first-statement)
1610 (beginning-of-line)
1611 (while (and (setq not-first-statement (zerop (forward-line -1)))
1612 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1613 not-first-statement))
1615 (defun f90-next-statement ()
1616 "Move point to beginning of the next F90 statement.
1617 Return nil if no later statement is found."
1618 (interactive)
1619 (let (not-last-statement)
1620 (beginning-of-line)
1621 (while (and (setq not-last-statement
1622 (and (zerop (forward-line 1))
1623 (not (eobp))))
1624 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1625 not-last-statement))
1627 (defun f90-beginning-of-subprogram ()
1628 "Move point to the beginning of the current subprogram.
1629 Return (TYPE NAME), or nil if not found."
1630 (interactive)
1631 (let ((count 1) (case-fold-search t) matching-beg)
1632 (beginning-of-line)
1633 (while (and (> count 0)
1634 (re-search-backward f90-program-block-re nil 'move))
1635 (beginning-of-line)
1636 (skip-chars-forward " \t0-9")
1637 (cond ((setq matching-beg (f90-looking-at-program-block-start))
1638 (setq count (1- count)))
1639 ((f90-looking-at-program-block-end)
1640 (setq count (1+ count)))))
1641 (beginning-of-line)
1642 (if (zerop count)
1643 matching-beg
1644 ;; Note this includes the case of an un-named main program,
1645 ;; in which case we go to (point-min).
1646 (if (called-interactively-p 'interactive)
1647 (message "No beginning found"))
1648 nil)))
1650 (defun f90-end-of-subprogram ()
1651 "Move point to the end of the current subprogram.
1652 Return (TYPE NAME), or nil if not found."
1653 (interactive)
1654 (let ((case-fold-search t)
1655 (count 1)
1656 matching-end)
1657 (end-of-line)
1658 (while (and (> count 0)
1659 (re-search-forward f90-program-block-re nil 'move))
1660 (beginning-of-line)
1661 (skip-chars-forward " \t0-9")
1662 (cond ((f90-looking-at-program-block-start)
1663 (setq count (1+ count)))
1664 ((setq matching-end (f90-looking-at-program-block-end))
1665 (setq count (1- count))))
1666 (end-of-line))
1667 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1668 ;; has a net non-zero effect, which seems odd.
1669 ;;; (forward-line 1)
1670 (if (zerop count)
1671 matching-end
1672 (if (called-interactively-p 'interactive)
1673 (message "No end found"))
1674 nil)))
1677 (defun f90-end-of-block (&optional num)
1678 "Move point forward to the end of the current code block.
1679 With optional argument NUM, go forward that many balanced blocks.
1680 If NUM is negative, go backward to the start of a block. Checks
1681 for consistency of block types and labels (if present), and
1682 completes outermost block if `f90-smart-end' is non-nil.
1683 Interactively, pushes mark before moving point."
1684 (interactive "p")
1685 ;; Can move some distance.
1686 (if (called-interactively-p 'any) (push-mark (point) t))
1687 (and num (< num 0) (f90-beginning-of-block (- num)))
1688 (let ((f90-smart-end (if f90-smart-end 'no-blink)) ; for final match-end
1689 (case-fold-search t)
1690 (count (or num 1))
1691 start-list start-this start-type start-label end-type end-label)
1692 (end-of-line) ; probably want this
1693 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1694 (beginning-of-line)
1695 (skip-chars-forward " \t0-9")
1696 (cond ((or (f90-in-string) (f90-in-comment)))
1697 ((setq start-this
1699 (f90-looking-at-do)
1700 (f90-looking-at-select-case)
1701 (f90-looking-at-type-like)
1702 (f90-looking-at-associate)
1703 (f90-looking-at-critical)
1704 (f90-looking-at-program-block-start)
1705 (f90-looking-at-if-then)
1706 (f90-looking-at-where-or-forall)))
1707 (setq start-list (cons start-this start-list) ; not add-to-list!
1708 count (1+ count)))
1709 ((looking-at (concat "end[ \t]*" f90-blocks-re
1710 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
1711 (setq end-type (match-string 1)
1712 end-label (match-string 2)
1713 count (1- count))
1714 ;; Check any internal blocks.
1715 (when start-list
1716 (setq start-this (car start-list)
1717 start-list (cdr start-list)
1718 start-type (car start-this)
1719 start-label (cadr start-this))
1720 (or (f90-equal-symbols start-type end-type)
1721 (error "End type `%s' does not match start type `%s'"
1722 end-type start-type))
1723 (or (f90-equal-symbols start-label end-label)
1724 (error "End label `%s' does not match start label `%s'"
1725 end-label start-label)))))
1726 (end-of-line))
1727 (if (> count 0) (error "Missing block end"))
1728 ;; Check outermost block.
1729 (when f90-smart-end
1730 (save-excursion
1731 (beginning-of-line)
1732 (skip-chars-forward " \t0-9")
1733 (f90-match-end)))))
1735 (defun f90-beginning-of-block (&optional num)
1736 "Move point backwards to the start of the current code block.
1737 With optional argument NUM, go backward that many balanced blocks.
1738 If NUM is negative, go forward to the end of a block.
1739 Checks for consistency of block types and labels (if present).
1740 Does not check the outermost block, because it may be incomplete.
1741 Interactively, pushes mark before moving point."
1742 (interactive "p")
1743 (if (called-interactively-p 'any) (push-mark (point) t))
1744 (and num (< num 0) (f90-end-of-block (- num)))
1745 (let ((case-fold-search t)
1746 (count (or num 1))
1747 end-list end-this end-type end-label
1748 start-this start-type start-label)
1749 (beginning-of-line) ; probably want this
1750 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1751 (beginning-of-line)
1752 (skip-chars-forward " \t0-9")
1753 (cond ((or (f90-in-string) (f90-in-comment)))
1754 ((looking-at (concat "end[ \t]*" f90-blocks-re
1755 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
1756 (setq end-list (cons (list (match-string 1) (match-string 2))
1757 end-list)
1758 count (1+ count)))
1759 ((setq start-this
1761 (f90-looking-at-do)
1762 (f90-looking-at-select-case)
1763 (f90-looking-at-type-like)
1764 (f90-looking-at-associate)
1765 (f90-looking-at-critical)
1766 (f90-looking-at-program-block-start)
1767 (f90-looking-at-if-then)
1768 (f90-looking-at-where-or-forall)))
1769 (setq start-type (car start-this)
1770 start-label (cadr start-this)
1771 count (1- count))
1772 ;; Check any internal blocks.
1773 (when end-list
1774 (setq end-this (car end-list)
1775 end-list (cdr end-list)
1776 end-type (car end-this)
1777 end-label (cadr end-this))
1778 (or (f90-equal-symbols start-type end-type)
1779 (error "Start type `%s' does not match end type `%s'"
1780 start-type end-type))
1781 (or (f90-equal-symbols start-label end-label)
1782 (error "Start label `%s' does not match end label `%s'"
1783 start-label end-label))))))
1784 ;; Includes an un-named main program block.
1785 (if (> count 0) (error "Missing block start"))))
1787 (defun f90-next-block (&optional num)
1788 "Move point forward to the next end or start of a code block.
1789 With optional argument NUM, go forward that many blocks.
1790 If NUM is negative, go backwards.
1791 A block is a subroutine, if-endif, etc."
1792 (interactive "p")
1793 (let ((case-fold-search t)
1794 (count (if num (abs num) 1)))
1795 (while (and (> count 0)
1796 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1797 (re-search-backward f90-blocks-re nil 'move)))
1798 (beginning-of-line)
1799 (skip-chars-forward " \t0-9")
1800 (cond ((or (f90-in-string) (f90-in-comment)))
1801 ((or
1802 (looking-at "end[ \t]*")
1803 (f90-looking-at-do)
1804 (f90-looking-at-select-case)
1805 (f90-looking-at-type-like)
1806 (f90-looking-at-associate)
1807 (f90-looking-at-critical)
1808 (f90-looking-at-program-block-start)
1809 (f90-looking-at-if-then)
1810 (f90-looking-at-where-or-forall))
1811 (setq count (1- count))))
1812 (if (> num 0) (end-of-line)
1813 (beginning-of-line)))))
1816 (defun f90-previous-block (&optional num)
1817 "Move point backward to the previous end or start of a code block.
1818 With optional argument NUM, go backward that many blocks.
1819 If NUM is negative, go forwards.
1820 A block is a subroutine, if-endif, etc."
1821 (interactive "p")
1822 (f90-next-block (- (or num 1))))
1825 (defun f90-mark-subprogram ()
1826 "Put mark at end of F90 subprogram, point at beginning, push mark."
1827 (interactive)
1828 (let ((pos (point)) program)
1829 (f90-end-of-subprogram)
1830 (push-mark)
1831 (goto-char pos)
1832 (setq program (f90-beginning-of-subprogram))
1833 (if (featurep 'xemacs)
1834 (zmacs-activate-region)
1835 (setq mark-active t
1836 deactivate-mark nil))
1837 program))
1839 (defun f90-comment-region (beg-region end-region)
1840 "Comment/uncomment every line in the region.
1841 Insert the variable `f90-comment-region' at the start of every line
1842 in the region, or, if already present, remove it."
1843 (interactive "*r")
1844 (let ((end (copy-marker end-region)))
1845 (goto-char beg-region)
1846 (beginning-of-line)
1847 (if (looking-at (regexp-quote f90-comment-region))
1848 (delete-region (point) (match-end 0))
1849 (insert f90-comment-region))
1850 (while (and (zerop (forward-line 1))
1851 (< (point) end))
1852 (if (looking-at (regexp-quote f90-comment-region))
1853 (delete-region (point) (match-end 0))
1854 (insert f90-comment-region)))
1855 (set-marker end nil)))
1857 (defun f90-indent-line (&optional no-update)
1858 "Indent current line as F90 code.
1859 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1860 after indenting."
1861 (interactive "*P")
1862 (let ((case-fold-search t)
1863 (pos (point-marker))
1864 indent no-line-number)
1865 (beginning-of-line) ; digits after & \n are not line-nos
1866 (if (not (save-excursion (and (f90-previous-statement)
1867 (f90-line-continued))))
1868 (f90-indent-line-no)
1869 (setq no-line-number t)
1870 (skip-chars-forward " \t"))
1871 ;; FIXME This means f90-calculate-indent gives different answers
1872 ;; for comments and preprocessor lines to this function.
1873 ;; Better to make f90-calculate-indent return the correct answer?
1874 (cond ((looking-at "!") (setq indent (f90-comment-indent)))
1875 ((looking-at "#") (setq indent 0))
1877 (and f90-smart-end (looking-at "end")
1878 (f90-match-end))
1879 (setq indent (f90-calculate-indent))))
1880 (or (= indent (current-column))
1881 (f90-indent-to indent no-line-number))
1882 ;; If initial point was within line's indentation,
1883 ;; position after the indentation. Else stay at same point in text.
1884 (and (< (point) pos)
1885 (goto-char pos))
1886 (if auto-fill-function
1887 ;; GM NO-UPDATE not honored, since this calls f90-update-line.
1888 (f90-do-auto-fill)
1889 (or no-update (f90-update-line)))
1890 (set-marker pos nil)))
1892 (defun f90-indent-new-line ()
1893 "Re-indent current line, insert a newline and indent the newline.
1894 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1895 If run in the middle of a line, the line is not broken."
1896 (interactive "*")
1897 (if abbrev-mode (expand-abbrev))
1898 (beginning-of-line) ; reindent where likely to be needed
1899 (f90-indent-line) ; calls indent-line-no, update-line
1900 (end-of-line)
1901 (delete-horizontal-space) ; destroy trailing whitespace
1902 (let ((string (f90-in-string))
1903 (cont (f90-line-continued)))
1904 (and string (not cont) (insert "&"))
1905 (newline)
1906 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
1907 (f90-indent-line 'no-update)) ; nothing to update
1910 ;; TODO not add spaces to empty lines at the start.
1911 ;; Why is second line getting extra indent over first?
1912 (defun f90-indent-region (beg-region end-region)
1913 "Indent every line in region by forward parsing."
1914 (interactive "*r")
1915 (let ((end-region-mark (copy-marker end-region))
1916 (save-point (point-marker))
1917 (case-fold-search t)
1918 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
1919 (goto-char beg-region)
1920 ;; First find a line which is not a continuation line or comment.
1921 (beginning-of-line)
1922 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1923 (progn (f90-indent-line 'no-update)
1924 (zerop (forward-line 1)))
1925 (< (point) end-region-mark)))
1926 (setq cont (f90-present-statement-cont))
1927 (while (and (memq cont '(middle end))
1928 (f90-previous-statement))
1929 (setq cont (f90-present-statement-cont)))
1930 ;; Process present line for beginning of block.
1931 (setq f90-cache-position (point))
1932 (f90-indent-line 'no-update)
1933 (setq ind-lev (f90-current-indentation)
1934 ind-curr ind-lev)
1935 (beginning-of-line)
1936 (skip-chars-forward " \t0-9")
1937 (setq struct nil
1938 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1939 ((or (setq struct (f90-looking-at-if-then))
1940 (setq struct (f90-looking-at-select-case))
1941 (setq struct (f90-looking-at-where-or-forall))
1942 (looking-at f90-else-like-re))
1943 f90-if-indent)
1944 ((setq struct (f90-looking-at-type-like))
1945 f90-type-indent)
1946 ((setq struct (f90-looking-at-associate))
1947 f90-associate-indent)
1948 ((setq struct (f90-looking-at-critical))
1949 f90-critical-indent)
1950 ((or (setq struct (f90-looking-at-program-block-start))
1951 (looking-at "contains[ \t]*\\($\\|!\\)"))
1952 f90-program-indent)))
1953 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1954 (if struct (setq block-list (cons struct block-list)))
1955 (while (and (f90-line-continued) (zerop (forward-line 1))
1956 (< (point) end-region-mark))
1957 (if (looking-at "[ \t]*!")
1958 (f90-indent-to (f90-comment-indent))
1959 (or (= (current-indentation)
1960 (+ ind-curr f90-continuation-indent))
1961 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1962 ;; Process all following lines.
1963 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1964 (beginning-of-line)
1965 (f90-indent-line-no)
1966 (setq f90-cache-position (point))
1967 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1968 ((looking-at "[ \t]*#") (setq ind-curr 0))
1969 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1970 ((f90-no-block-limit) (setq ind-curr ind-lev))
1971 ((looking-at f90-else-like-re) (setq ind-curr
1972 (- ind-lev f90-if-indent)))
1973 ((looking-at "contains[ \t]*\\($\\|!\\)")
1974 (setq ind-curr (- ind-lev f90-program-indent)))
1975 ((setq ind-b
1976 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1977 ((or (setq struct (f90-looking-at-if-then))
1978 (setq struct (f90-looking-at-select-case))
1979 (setq struct (f90-looking-at-where-or-forall)))
1980 f90-if-indent)
1981 ((setq struct (f90-looking-at-type-like))
1982 f90-type-indent)
1983 ((setq struct (f90-looking-at-associate))
1984 f90-associate-indent)
1985 ((setq struct (f90-looking-at-critical))
1986 f90-critical-indent)
1987 ((setq struct (f90-looking-at-program-block-start))
1988 f90-program-indent)))
1989 (setq ind-curr ind-lev)
1990 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1991 (setq block-list (cons struct block-list)))
1992 ((setq end-struct (f90-looking-at-program-block-end))
1993 (setq beg-struct (car block-list)
1994 block-list (cdr block-list))
1995 (if f90-smart-end
1996 (save-excursion
1997 (f90-block-match (car beg-struct) (cadr beg-struct)
1998 (car end-struct) (cadr end-struct))))
1999 (setq ind-b
2000 (cond ((looking-at f90-end-if-re) f90-if-indent)
2001 ((looking-at "end[ \t]*do\\_>") f90-do-indent)
2002 ((looking-at f90-end-type-re) f90-type-indent)
2003 ((looking-at f90-end-associate-re)
2004 f90-associate-indent)
2005 ((f90-looking-at-end-critical) f90-critical-indent)
2006 ((f90-looking-at-program-block-end)
2007 f90-program-indent)))
2008 (if ind-b (setq ind-lev (- ind-lev ind-b)))
2009 (setq ind-curr ind-lev))
2010 (t (setq ind-curr ind-lev)))
2011 ;; Do the indentation if necessary.
2012 (or (= ind-curr (current-column))
2013 (f90-indent-to ind-curr))
2014 (while (and (f90-line-continued) (zerop (forward-line 1))
2015 (< (point) end-region-mark))
2016 (cond ((looking-at "[ \t]*#") (f90-indent-to 0))
2017 ((looking-at "[ \t]*!") (f90-indent-to (f90-comment-indent)))
2019 (or (= (current-indentation)
2020 (+ ind-curr f90-continuation-indent))
2021 (f90-indent-to
2022 (+ ind-curr f90-continuation-indent) 'no-line-no))))))
2023 ;; Restore point, etc.
2024 (setq f90-cache-position nil)
2025 (goto-char save-point)
2026 (set-marker end-region-mark nil)
2027 (set-marker save-point nil)
2028 (if (featurep 'xemacs)
2029 (zmacs-deactivate-region)
2030 (deactivate-mark))))
2032 (defun f90-indent-subprogram ()
2033 "Properly indent the subprogram containing point."
2034 (interactive "*")
2035 (save-excursion
2036 (let ((program (f90-mark-subprogram)))
2037 (if program
2038 (progn
2039 (message "Indenting %s %s..."
2040 (car program) (cadr program))
2041 (indent-region (point) (mark) nil)
2042 (message "Indenting %s %s...done"
2043 (car program) (cadr program)))
2044 (message "Indenting the whole file...")
2045 (indent-region (point) (mark) nil)
2046 (message "Indenting the whole file...done")))))
2048 (defun f90-break-line (&optional no-update)
2049 "Break line at point, insert continuation marker(s) and indent.
2050 Unless in a string or comment, or if the optional argument NO-UPDATE
2051 is non-nil, call `f90-update-line' after inserting the continuation marker."
2052 (interactive "*P")
2053 (cond ((f90-in-string)
2054 (insert "&\n&"))
2055 ((f90-in-comment)
2056 (delete-horizontal-space) ; remove trailing whitespace
2057 (insert "\n" (f90-get-present-comment-type)))
2058 (t (insert "&")
2059 (or no-update (f90-update-line))
2060 (newline 1)
2061 ;; FIXME also need leading ampersand if split lexical token (eg ==).
2062 ;; Or respect f90-no-break-re.
2063 (if f90-beginning-ampersand (insert "&"))))
2064 (indent-according-to-mode))
2066 (defun f90-find-breakpoint ()
2067 "From `fill-column', search backward for break-delimiter."
2068 ;; Commented text more likely than commented code.
2069 (if (f90-in-comment)
2070 (re-search-backward "\\s-" (line-beginning-position))
2071 (re-search-backward f90-break-delimiters (line-beginning-position))
2072 (if (not f90-break-before-delimiters)
2073 (forward-char (if (looking-at f90-no-break-re) 2 1))
2074 (backward-char)
2075 (or (looking-at f90-no-break-re)
2076 (forward-char)))))
2078 (defun f90-do-auto-fill ()
2079 "Break line if non-white characters beyond `fill-column'.
2080 Update keyword case first."
2081 (interactive "*")
2082 ;; Break line before or after last delimiter (non-word char) if
2083 ;; position is beyond fill-column.
2084 ;; Will not break **, //, or => (as specified by f90-no-break-re).
2085 (f90-update-line)
2086 ;; Need this for `f90-electric-insert' and other f90- callers.
2087 (unless (and (boundp 'comment-auto-fill-only-comments)
2088 comment-auto-fill-only-comments
2089 (not (f90-in-comment)))
2090 (while (> (current-column) fill-column)
2091 (let ((pos-mark (point-marker)))
2092 (move-to-column fill-column)
2093 (or (f90-in-string) (f90-find-breakpoint))
2094 (f90-break-line)
2095 (goto-char pos-mark)
2096 (set-marker pos-mark nil)))))
2098 (defun f90-join-lines (&optional arg)
2099 "Join current line to previous, fix whitespace, continuation, comments.
2100 With optional argument ARG, join current line to following line.
2101 Like `join-line', but handles F90 syntax."
2102 (interactive "*P")
2103 (beginning-of-line)
2104 (if arg (forward-line 1))
2105 (when (eq (preceding-char) ?\n)
2106 (skip-chars-forward " \t")
2107 (if (looking-at "\&") (delete-char 1))
2108 (beginning-of-line)
2109 (delete-region (point) (1- (point)))
2110 (skip-chars-backward " \t")
2111 (and (eq (preceding-char) ?&) (delete-char -1))
2112 (and (f90-in-comment)
2113 (looking-at "[ \t]*!+")
2114 (replace-match ""))
2115 (or (f90-in-string)
2116 (fixup-whitespace))))
2118 (defun f90-fill-region (beg-region end-region)
2119 "Fill every line in region by forward parsing. Join lines if possible."
2120 (interactive "*r")
2121 (let ((end-region-mark (copy-marker end-region))
2122 (go-on t)
2123 f90-smart-end f90-auto-keyword-case auto-fill-function)
2124 (goto-char beg-region)
2125 (while go-on
2126 ;; Join as much as possible.
2127 (while (progn
2128 (end-of-line)
2129 (skip-chars-backward " \t")
2130 (eq (preceding-char) ?&))
2131 (f90-join-lines 'forward))
2132 ;; Chop the line if necessary.
2133 (while (> (save-excursion (end-of-line) (current-column))
2134 fill-column)
2135 (move-to-column fill-column)
2136 (f90-find-breakpoint)
2137 (f90-break-line 'no-update))
2138 (setq go-on (and (< (point) end-region-mark)
2139 (zerop (forward-line 1)))
2140 f90-cache-position (point)))
2141 (setq f90-cache-position nil)
2142 (set-marker end-region-mark nil)
2143 (if (featurep 'xemacs)
2144 (zmacs-deactivate-region)
2145 (deactivate-mark))))
2147 (defconst f90-end-block-optional-name
2148 '("program" "module" "subroutine" "function" "type")
2149 "Block types where including the name in the end statement is optional.")
2151 (defun f90-block-match (beg-block beg-name end-block end-name)
2152 "Match end-struct with beg-struct and complete end-block if possible.
2153 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
2154 BEG-NAME is the block start name (may be nil).
2155 END-BLOCK is the type of block as indicated at the end (may be nil).
2156 END-NAME is the block end name (may be nil).
2157 If the block type matches `f90-end-block-optional-name', do not add
2158 an end name if `f90-smart-end-names' is nil, but always update an
2159 incorrect end name if there already was one.
2160 Leave point at the end of line."
2161 ;; Hack to deal with the case when this is called from
2162 ;; f90-indent-region on a program block without an explicit PROGRAM
2163 ;; statement at the start. Should really be an error (?).
2164 (or beg-block (setq beg-block "program"))
2165 (search-forward "end" (line-end-position))
2166 (catch 'no-match
2167 (if (and end-block (f90-equal-symbols beg-block end-block))
2168 (search-forward end-block)
2169 (if end-block
2170 (progn
2171 (message "END %s does not match %s." end-block beg-block)
2172 (end-of-line)
2173 (throw 'no-match nil))
2174 (message "Inserting %s." beg-block)
2175 (insert (concat " " beg-block))))
2176 (if (f90-equal-symbols beg-name end-name)
2177 (and end-name (search-forward end-name))
2178 (cond ((and beg-name (not end-name))
2179 (unless (and (not f90-smart-end-names)
2180 (member-ignore-case beg-block
2181 f90-end-block-optional-name))
2182 (message "Inserting %s." beg-name)
2183 (insert (concat " " beg-name))))
2184 ((and beg-name end-name)
2185 (message "Replacing %s with %s." end-name beg-name)
2186 (search-forward end-name)
2187 (replace-match beg-name))
2188 ((and (not beg-name) end-name)
2189 (message "Deleting %s." end-name)
2190 (search-forward end-name)
2191 (replace-match ""))))
2192 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
2194 (defun f90-match-end ()
2195 "From an end block statement, find the corresponding block and name."
2196 (interactive)
2197 (let ((count 1)
2198 (top-of-window (window-start))
2199 (end-point (point))
2200 (case-fold-search t)
2201 matching-beg beg-name end-name beg-block end-block end-struct)
2202 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
2203 (setq end-struct (f90-looking-at-program-block-end)))
2204 (setq end-block (car end-struct)
2205 end-name (cadr end-struct))
2206 (save-excursion
2207 (beginning-of-line)
2208 (while (and (> count 0)
2209 (not (= (line-beginning-position) (point-min))))
2210 (re-search-backward f90-blocks-re nil 'move)
2211 (beginning-of-line)
2212 ;; GM not a line number if continued line.
2213 ;;; (skip-chars-forward " \t")
2214 ;;; (skip-chars-forward "0-9")
2215 (skip-chars-forward " \t0-9")
2216 (cond ((or (f90-in-string) (f90-in-comment)))
2217 ((setq matching-beg
2219 (f90-looking-at-do)
2220 (f90-looking-at-if-then)
2221 (f90-looking-at-where-or-forall)
2222 (f90-looking-at-select-case)
2223 (f90-looking-at-type-like)
2224 (f90-looking-at-associate)
2225 (f90-looking-at-critical)
2226 (f90-looking-at-program-block-start)
2227 ;; Interpret a single END without a block
2228 ;; start to be the END of a program block
2229 ;; without an initial PROGRAM line.
2230 (if (= (line-beginning-position) (point-min))
2231 '("program" nil))))
2232 (setq count (1- count)))
2233 ((looking-at (concat "end[ \t]*" f90-blocks-re))
2234 (setq count (1+ count)))))
2235 (if (> count 0)
2236 (message "No matching beginning.")
2237 (f90-update-line)
2238 (if (eq f90-smart-end 'blink)
2239 (if (< (point) top-of-window)
2240 (message "Matches %s: %s"
2241 (what-line)
2242 (buffer-substring
2243 (line-beginning-position)
2244 (line-end-position)))
2245 (sit-for blink-matching-delay)))
2246 (setq beg-block (car matching-beg)
2247 beg-name (cadr matching-beg))
2248 (goto-char end-point)
2249 (beginning-of-line)
2250 (f90-block-match beg-block beg-name end-block end-name))))))
2252 (defun f90-insert-end ()
2253 "Insert a complete end statement matching beginning of present block."
2254 (interactive "*")
2255 (let ((f90-smart-end (or f90-smart-end 'blink)))
2256 (insert "end")
2257 (f90-indent-new-line)))
2259 ;; Abbrevs and keywords.
2261 (defun f90-abbrev-start ()
2262 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
2263 Any other key combination is executed normally."
2264 (interactive "*")
2265 (self-insert-command 1)
2266 (when abbrev-mode
2267 (set-transient-map
2268 (let ((map (make-sparse-keymap)))
2269 (define-key map [??] 'f90-abbrev-help)
2270 (define-key map (vector help-char) 'f90-abbrev-help)
2271 map))))
2273 (defun f90-abbrev-help ()
2274 "List the currently defined abbrevs in F90 mode."
2275 (interactive)
2276 (message "Listing abbrev table...")
2277 (display-buffer (f90-prepare-abbrev-list-buffer))
2278 (message "Listing abbrev table...done"))
2280 (defun f90-prepare-abbrev-list-buffer ()
2281 "Create a buffer listing the F90 mode abbreviations."
2282 (with-current-buffer (get-buffer-create "*Abbrevs*")
2283 (erase-buffer)
2284 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
2285 (goto-char (point-min))
2286 (set-buffer-modified-p nil)
2287 (edit-abbrevs-mode))
2288 (get-buffer-create "*Abbrevs*"))
2290 (defun f90-upcase-keywords ()
2291 "Upcase all F90 keywords in the buffer."
2292 (interactive "*")
2293 (f90-change-keywords 'upcase-word))
2295 (defun f90-capitalize-keywords ()
2296 "Capitalize all F90 keywords in the buffer."
2297 (interactive "*")
2298 (f90-change-keywords 'capitalize-word))
2300 (defun f90-downcase-keywords ()
2301 "Downcase all F90 keywords in the buffer."
2302 (interactive "*")
2303 (f90-change-keywords 'downcase-word))
2305 (defun f90-upcase-region-keywords (beg end)
2306 "Upcase all F90 keywords in the region."
2307 (interactive "*r")
2308 (f90-change-keywords 'upcase-word beg end))
2310 (defun f90-capitalize-region-keywords (beg end)
2311 "Capitalize all F90 keywords in the region."
2312 (interactive "*r")
2313 (f90-change-keywords 'capitalize-word beg end))
2315 (defun f90-downcase-region-keywords (beg end)
2316 "Downcase all F90 keywords in the region."
2317 (interactive "*r")
2318 (f90-change-keywords 'downcase-word beg end))
2320 ;; Change the keywords according to argument.
2321 (defun f90-change-keywords (change-word &optional beg end)
2322 "Change the case of F90 keywords in the region (if specified) or buffer.
2323 CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
2324 (save-excursion
2325 (setq beg (or beg (point-min))
2326 end (or end (point-max)))
2327 (let ((keyword-re
2328 (concat "\\("
2329 f90-keywords-re "\\|" f90-procedures-re "\\|"
2330 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
2331 (ref-point (point-min))
2332 (modified (buffer-modified-p))
2333 state saveword back-point)
2334 (goto-char beg)
2335 (unwind-protect
2336 (while (re-search-forward keyword-re end t)
2337 (unless (progn
2338 (setq state (parse-partial-sexp ref-point (point)))
2339 (or (nth 3 state) (nth 4 state)
2340 ;; GM f90-directive-comment-re?
2341 (save-excursion ; check for cpp directive
2342 (beginning-of-line)
2343 (skip-chars-forward " \t0-9")
2344 (looking-at "#"))))
2345 (setq ref-point (point)
2346 ;; FIXME this does not work for constructs with
2347 ;; embedded space, eg "sync all".
2348 back-point (save-excursion (backward-word 1) (point))
2349 saveword (buffer-substring back-point ref-point))
2350 (funcall change-word -1)
2351 (or (string= saveword (buffer-substring back-point ref-point))
2352 (setq modified t))))
2353 (or modified (restore-buffer-modified-p nil))))))
2356 (defun f90-current-defun ()
2357 "Function to use for `add-log-current-defun-function' in F90 mode."
2358 (save-excursion
2359 (nth 1 (f90-beginning-of-subprogram))))
2361 (defun f90-backslash-not-special (&optional all)
2362 "Make the backslash character (\\) be non-special in the current buffer.
2363 With optional argument ALL, change the default for all present
2364 and future F90 buffers. F90 mode normally treats backslash as an
2365 escape character."
2366 (or (derived-mode-p 'f90-mode)
2367 (error "This function should only be used in F90 buffers"))
2368 (when (equal (char-syntax ?\\ ) ?\\ )
2369 (or all (set-syntax-table (copy-syntax-table (syntax-table))))
2370 (modify-syntax-entry ?\\ ".")))
2373 (provide 'f90)
2375 ;; Local Variables:
2376 ;; coding: utf-8
2377 ;; End:
2379 ;;; f90.el ends here