* jit-lock.el (jit-lock--run-functions): Do the `or' where it can be nil.
[emacs.git] / lisp / progmodes / f90.el
blob6264d3b7b828605a477a9964bf18622d578e1574
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 ;; Check if in string in case using non-standard feature where
1638 ;; continued strings do not need "&" at start of continuations.
1639 (cond ((f90-in-string))
1640 ((setq matching-beg (f90-looking-at-program-block-start))
1641 (setq count (1- count)))
1642 ((f90-looking-at-program-block-end)
1643 (setq count (1+ count)))))
1644 (beginning-of-line)
1645 (if (zerop count)
1646 matching-beg
1647 ;; Note this includes the case of an un-named main program,
1648 ;; in which case we go to (point-min).
1649 (if (called-interactively-p 'interactive)
1650 (message "No beginning found"))
1651 nil)))
1653 (defun f90-end-of-subprogram ()
1654 "Move point to the end of the current subprogram.
1655 Return (TYPE NAME), or nil if not found."
1656 (interactive)
1657 (let ((case-fold-search t)
1658 (count 1)
1659 matching-end)
1660 (end-of-line)
1661 (while (and (> count 0)
1662 (re-search-forward f90-program-block-re nil 'move))
1663 (beginning-of-line)
1664 (skip-chars-forward " \t0-9")
1665 (cond ((f90-in-string))
1666 ((f90-looking-at-program-block-start)
1667 (setq count (1+ count)))
1668 ((setq matching-end (f90-looking-at-program-block-end))
1669 (setq count (1- count))))
1670 (end-of-line))
1671 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1672 ;; has a net non-zero effect, which seems odd.
1673 ;;; (forward-line 1)
1674 (if (zerop count)
1675 matching-end
1676 (if (called-interactively-p 'interactive)
1677 (message "No end found"))
1678 nil)))
1681 (defun f90-end-of-block (&optional num)
1682 "Move point forward to the end of the current code block.
1683 With optional argument NUM, go forward that many balanced blocks.
1684 If NUM is negative, go backward to the start of a block. Checks
1685 for consistency of block types and labels (if present), and
1686 completes outermost block if `f90-smart-end' is non-nil.
1687 Interactively, pushes mark before moving point."
1688 (interactive "p")
1689 ;; Can move some distance.
1690 (if (called-interactively-p 'any) (push-mark (point) t))
1691 (and num (< num 0) (f90-beginning-of-block (- num)))
1692 (let ((f90-smart-end (if f90-smart-end 'no-blink)) ; for final match-end
1693 (case-fold-search t)
1694 (count (or num 1))
1695 start-list start-this start-type start-label end-type end-label)
1696 (end-of-line) ; probably want this
1697 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1698 (beginning-of-line)
1699 (skip-chars-forward " \t0-9")
1700 (cond ((or (f90-in-string) (f90-in-comment)))
1701 ((setq start-this
1703 (f90-looking-at-do)
1704 (f90-looking-at-select-case)
1705 (f90-looking-at-type-like)
1706 (f90-looking-at-associate)
1707 (f90-looking-at-critical)
1708 (f90-looking-at-program-block-start)
1709 (f90-looking-at-if-then)
1710 (f90-looking-at-where-or-forall)))
1711 (setq start-list (cons start-this start-list) ; not add-to-list!
1712 count (1+ count)))
1713 ((looking-at (concat "end[ \t]*" f90-blocks-re
1714 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
1715 (setq end-type (match-string 1)
1716 end-label (match-string 2)
1717 count (1- count))
1718 ;; Check any internal blocks.
1719 (when start-list
1720 (setq start-this (car start-list)
1721 start-list (cdr start-list)
1722 start-type (car start-this)
1723 start-label (cadr start-this))
1724 (or (f90-equal-symbols start-type end-type)
1725 (error "End type `%s' does not match start type `%s'"
1726 end-type start-type))
1727 (or (f90-equal-symbols start-label end-label)
1728 (error "End label `%s' does not match start label `%s'"
1729 end-label start-label)))))
1730 (end-of-line))
1731 (if (> count 0) (error "Missing block end"))
1732 ;; Check outermost block.
1733 (when f90-smart-end
1734 (save-excursion
1735 (beginning-of-line)
1736 (skip-chars-forward " \t0-9")
1737 (f90-match-end)))))
1739 (defun f90-beginning-of-block (&optional num)
1740 "Move point backwards to the start of the current code block.
1741 With optional argument NUM, go backward that many balanced blocks.
1742 If NUM is negative, go forward to the end of a block.
1743 Checks for consistency of block types and labels (if present).
1744 Does not check the outermost block, because it may be incomplete.
1745 Interactively, pushes mark before moving point."
1746 (interactive "p")
1747 (if (called-interactively-p 'any) (push-mark (point) t))
1748 (and num (< num 0) (f90-end-of-block (- num)))
1749 (let ((case-fold-search t)
1750 (count (or num 1))
1751 end-list end-this end-type end-label
1752 start-this start-type start-label)
1753 (beginning-of-line) ; probably want this
1754 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1755 (beginning-of-line)
1756 (skip-chars-forward " \t0-9")
1757 (cond ((or (f90-in-string) (f90-in-comment)))
1758 ((looking-at (concat "end[ \t]*" f90-blocks-re
1759 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
1760 (setq end-list (cons (list (match-string 1) (match-string 2))
1761 end-list)
1762 count (1+ count)))
1763 ((setq start-this
1765 (f90-looking-at-do)
1766 (f90-looking-at-select-case)
1767 (f90-looking-at-type-like)
1768 (f90-looking-at-associate)
1769 (f90-looking-at-critical)
1770 (f90-looking-at-program-block-start)
1771 (f90-looking-at-if-then)
1772 (f90-looking-at-where-or-forall)))
1773 (setq start-type (car start-this)
1774 start-label (cadr start-this)
1775 count (1- count))
1776 ;; Check any internal blocks.
1777 (when end-list
1778 (setq end-this (car end-list)
1779 end-list (cdr end-list)
1780 end-type (car end-this)
1781 end-label (cadr end-this))
1782 (or (f90-equal-symbols start-type end-type)
1783 (error "Start type `%s' does not match end type `%s'"
1784 start-type end-type))
1785 (or (f90-equal-symbols start-label end-label)
1786 (error "Start label `%s' does not match end label `%s'"
1787 start-label end-label))))))
1788 ;; Includes an un-named main program block.
1789 (if (> count 0) (error "Missing block start"))))
1791 (defun f90-next-block (&optional num)
1792 "Move point forward to the next end or start of a code block.
1793 With optional argument NUM, go forward that many blocks.
1794 If NUM is negative, go backwards.
1795 A block is a subroutine, if-endif, etc."
1796 (interactive "p")
1797 (let ((case-fold-search t)
1798 (count (if num (abs num) 1)))
1799 (while (and (> count 0)
1800 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1801 (re-search-backward f90-blocks-re nil 'move)))
1802 (beginning-of-line)
1803 (skip-chars-forward " \t0-9")
1804 (cond ((or (f90-in-string) (f90-in-comment)))
1805 ((or
1806 (looking-at "end[ \t]*")
1807 (f90-looking-at-do)
1808 (f90-looking-at-select-case)
1809 (f90-looking-at-type-like)
1810 (f90-looking-at-associate)
1811 (f90-looking-at-critical)
1812 (f90-looking-at-program-block-start)
1813 (f90-looking-at-if-then)
1814 (f90-looking-at-where-or-forall))
1815 (setq count (1- count))))
1816 (if (> num 0) (end-of-line)
1817 (beginning-of-line)))))
1820 (defun f90-previous-block (&optional num)
1821 "Move point backward to the previous end or start of a code block.
1822 With optional argument NUM, go backward that many blocks.
1823 If NUM is negative, go forwards.
1824 A block is a subroutine, if-endif, etc."
1825 (interactive "p")
1826 (f90-next-block (- (or num 1))))
1829 (defun f90-mark-subprogram ()
1830 "Put mark at end of F90 subprogram, point at beginning, push mark."
1831 (interactive)
1832 (let ((pos (point)) program)
1833 (f90-end-of-subprogram)
1834 (push-mark)
1835 (goto-char pos)
1836 (setq program (f90-beginning-of-subprogram))
1837 (if (featurep 'xemacs)
1838 (zmacs-activate-region)
1839 (setq mark-active t
1840 deactivate-mark nil))
1841 program))
1843 (defun f90-comment-region (beg-region end-region)
1844 "Comment/uncomment every line in the region.
1845 Insert the variable `f90-comment-region' at the start of every line
1846 in the region, or, if already present, remove it."
1847 (interactive "*r")
1848 (let ((end (copy-marker end-region)))
1849 (goto-char beg-region)
1850 (beginning-of-line)
1851 (if (looking-at (regexp-quote f90-comment-region))
1852 (delete-region (point) (match-end 0))
1853 (insert f90-comment-region))
1854 (while (and (zerop (forward-line 1))
1855 (< (point) end))
1856 (if (looking-at (regexp-quote f90-comment-region))
1857 (delete-region (point) (match-end 0))
1858 (insert f90-comment-region)))
1859 (set-marker end nil)))
1861 (defun f90-indent-line (&optional no-update)
1862 "Indent current line as F90 code.
1863 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1864 after indenting."
1865 (interactive "*P")
1866 (let ((case-fold-search t)
1867 (pos (point-marker))
1868 indent no-line-number)
1869 (beginning-of-line) ; digits after & \n are not line-nos
1870 (if (not (save-excursion (and (f90-previous-statement)
1871 (f90-line-continued))))
1872 (f90-indent-line-no)
1873 (setq no-line-number t)
1874 (skip-chars-forward " \t"))
1875 ;; FIXME This means f90-calculate-indent gives different answers
1876 ;; for comments and preprocessor lines to this function.
1877 ;; Better to make f90-calculate-indent return the correct answer?
1878 (cond ((looking-at "!") (setq indent (f90-comment-indent)))
1879 ((looking-at "#") (setq indent 0))
1881 (and f90-smart-end (looking-at "end")
1882 (f90-match-end))
1883 (setq indent (f90-calculate-indent))))
1884 (or (= indent (current-column))
1885 (f90-indent-to indent no-line-number))
1886 ;; If initial point was within line's indentation,
1887 ;; position after the indentation. Else stay at same point in text.
1888 (and (< (point) pos)
1889 (goto-char pos))
1890 (if auto-fill-function
1891 ;; GM NO-UPDATE not honored, since this calls f90-update-line.
1892 (f90-do-auto-fill)
1893 (or no-update (f90-update-line)))
1894 (set-marker pos nil)))
1896 (defun f90-indent-new-line ()
1897 "Re-indent current line, insert a newline and indent the newline.
1898 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1899 If run in the middle of a line, the line is not broken."
1900 (interactive "*")
1901 (if abbrev-mode (expand-abbrev))
1902 (beginning-of-line) ; reindent where likely to be needed
1903 (f90-indent-line) ; calls indent-line-no, update-line
1904 (end-of-line)
1905 (delete-horizontal-space) ; destroy trailing whitespace
1906 (let ((string (f90-in-string))
1907 (cont (f90-line-continued)))
1908 (and string (not cont) (insert "&"))
1909 (newline)
1910 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
1911 (f90-indent-line 'no-update)) ; nothing to update
1914 ;; TODO not add spaces to empty lines at the start.
1915 ;; Why is second line getting extra indent over first?
1916 (defun f90-indent-region (beg-region end-region)
1917 "Indent every line in region by forward parsing."
1918 (interactive "*r")
1919 (let ((end-region-mark (copy-marker end-region))
1920 (save-point (point-marker))
1921 (case-fold-search t)
1922 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
1923 (goto-char beg-region)
1924 ;; First find a line which is not a continuation line or comment.
1925 (beginning-of-line)
1926 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1927 (progn (f90-indent-line 'no-update)
1928 (zerop (forward-line 1)))
1929 (< (point) end-region-mark)))
1930 (setq cont (f90-present-statement-cont))
1931 (while (and (memq cont '(middle end))
1932 (f90-previous-statement))
1933 (setq cont (f90-present-statement-cont)))
1934 ;; Process present line for beginning of block.
1935 (setq f90-cache-position (point))
1936 (f90-indent-line 'no-update)
1937 (setq ind-lev (f90-current-indentation)
1938 ind-curr ind-lev)
1939 (beginning-of-line)
1940 (skip-chars-forward " \t0-9")
1941 (setq struct nil
1942 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1943 ((or (setq struct (f90-looking-at-if-then))
1944 (setq struct (f90-looking-at-select-case))
1945 (setq struct (f90-looking-at-where-or-forall))
1946 (looking-at f90-else-like-re))
1947 f90-if-indent)
1948 ((setq struct (f90-looking-at-type-like))
1949 f90-type-indent)
1950 ((setq struct (f90-looking-at-associate))
1951 f90-associate-indent)
1952 ((setq struct (f90-looking-at-critical))
1953 f90-critical-indent)
1954 ((or (setq struct (f90-looking-at-program-block-start))
1955 (looking-at "contains[ \t]*\\($\\|!\\)"))
1956 f90-program-indent)))
1957 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1958 (if struct (setq block-list (cons struct block-list)))
1959 (while (and (f90-line-continued) (zerop (forward-line 1))
1960 (< (point) end-region-mark))
1961 (if (looking-at "[ \t]*!")
1962 (f90-indent-to (f90-comment-indent))
1963 (or (= (current-indentation)
1964 (+ ind-curr f90-continuation-indent))
1965 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1966 ;; Process all following lines.
1967 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1968 (beginning-of-line)
1969 (f90-indent-line-no)
1970 (setq f90-cache-position (point))
1971 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1972 ((looking-at "[ \t]*#") (setq ind-curr 0))
1973 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1974 ((f90-no-block-limit) (setq ind-curr ind-lev))
1975 ((looking-at f90-else-like-re) (setq ind-curr
1976 (- ind-lev f90-if-indent)))
1977 ((looking-at "contains[ \t]*\\($\\|!\\)")
1978 (setq ind-curr (- ind-lev f90-program-indent)))
1979 ((setq ind-b
1980 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1981 ((or (setq struct (f90-looking-at-if-then))
1982 (setq struct (f90-looking-at-select-case))
1983 (setq struct (f90-looking-at-where-or-forall)))
1984 f90-if-indent)
1985 ((setq struct (f90-looking-at-type-like))
1986 f90-type-indent)
1987 ((setq struct (f90-looking-at-associate))
1988 f90-associate-indent)
1989 ((setq struct (f90-looking-at-critical))
1990 f90-critical-indent)
1991 ((setq struct (f90-looking-at-program-block-start))
1992 f90-program-indent)))
1993 (setq ind-curr ind-lev)
1994 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1995 (setq block-list (cons struct block-list)))
1996 ((setq end-struct (f90-looking-at-program-block-end))
1997 (setq beg-struct (car block-list)
1998 block-list (cdr block-list))
1999 (if f90-smart-end
2000 (save-excursion
2001 (f90-block-match (car beg-struct) (cadr beg-struct)
2002 (car end-struct) (cadr end-struct))))
2003 (setq ind-b
2004 (cond ((looking-at f90-end-if-re) f90-if-indent)
2005 ((looking-at "end[ \t]*do\\_>") f90-do-indent)
2006 ((looking-at f90-end-type-re) f90-type-indent)
2007 ((looking-at f90-end-associate-re)
2008 f90-associate-indent)
2009 ((f90-looking-at-end-critical) f90-critical-indent)
2010 ((f90-looking-at-program-block-end)
2011 f90-program-indent)))
2012 (if ind-b (setq ind-lev (- ind-lev ind-b)))
2013 (setq ind-curr ind-lev))
2014 (t (setq ind-curr ind-lev)))
2015 ;; Do the indentation if necessary.
2016 (or (= ind-curr (current-column))
2017 (f90-indent-to ind-curr))
2018 (while (and (f90-line-continued) (zerop (forward-line 1))
2019 (< (point) end-region-mark))
2020 (cond ((looking-at "[ \t]*#") (f90-indent-to 0))
2021 ((looking-at "[ \t]*!") (f90-indent-to (f90-comment-indent)))
2023 (or (= (current-indentation)
2024 (+ ind-curr f90-continuation-indent))
2025 (f90-indent-to
2026 (+ ind-curr f90-continuation-indent) 'no-line-no))))))
2027 ;; Restore point, etc.
2028 (setq f90-cache-position nil)
2029 (goto-char save-point)
2030 (set-marker end-region-mark nil)
2031 (set-marker save-point nil)
2032 (if (featurep 'xemacs)
2033 (zmacs-deactivate-region)
2034 (deactivate-mark))))
2036 (defun f90-indent-subprogram ()
2037 "Properly indent the subprogram containing point."
2038 (interactive "*")
2039 (save-excursion
2040 (let ((program (f90-mark-subprogram)))
2041 (if program
2042 (progn
2043 (message "Indenting %s %s..."
2044 (car program) (cadr program))
2045 (indent-region (point) (mark) nil)
2046 (message "Indenting %s %s...done"
2047 (car program) (cadr program)))
2048 (message "Indenting the whole file...")
2049 (indent-region (point) (mark) nil)
2050 (message "Indenting the whole file...done")))))
2052 (defun f90-break-line (&optional no-update)
2053 "Break line at point, insert continuation marker(s) and indent.
2054 Unless in a string or comment, or if the optional argument NO-UPDATE
2055 is non-nil, call `f90-update-line' after inserting the continuation marker."
2056 (interactive "*P")
2057 (cond ((f90-in-string)
2058 (insert "&\n&"))
2059 ((f90-in-comment)
2060 (delete-horizontal-space) ; remove trailing whitespace
2061 (insert "\n" (f90-get-present-comment-type)))
2062 (t (insert "&")
2063 (or no-update (f90-update-line))
2064 (newline 1)
2065 ;; FIXME also need leading ampersand if split lexical token (eg ==).
2066 ;; Or respect f90-no-break-re.
2067 (if f90-beginning-ampersand (insert "&"))))
2068 (indent-according-to-mode))
2070 (defun f90-find-breakpoint ()
2071 "From `fill-column', search backward for break-delimiter."
2072 ;; Commented text more likely than commented code.
2073 (if (f90-in-comment)
2074 (re-search-backward "\\s-" (line-beginning-position))
2075 (re-search-backward f90-break-delimiters (line-beginning-position))
2076 (if (not f90-break-before-delimiters)
2077 (forward-char (if (looking-at f90-no-break-re) 2 1))
2078 (backward-char)
2079 (or (looking-at f90-no-break-re)
2080 (forward-char)))))
2082 (defun f90-do-auto-fill ()
2083 "Break line if non-white characters beyond `fill-column'.
2084 Update keyword case first."
2085 (interactive "*")
2086 ;; Break line before or after last delimiter (non-word char) if
2087 ;; position is beyond fill-column.
2088 ;; Will not break **, //, or => (as specified by f90-no-break-re).
2089 (f90-update-line)
2090 ;; Need this for `f90-electric-insert' and other f90- callers.
2091 (unless (and (boundp 'comment-auto-fill-only-comments)
2092 comment-auto-fill-only-comments
2093 (not (f90-in-comment)))
2094 (while (> (current-column) fill-column)
2095 (let ((pos-mark (point-marker)))
2096 (move-to-column fill-column)
2097 (or (f90-in-string) (f90-find-breakpoint))
2098 (f90-break-line)
2099 (goto-char pos-mark)
2100 (set-marker pos-mark nil)))))
2102 (defun f90-join-lines (&optional arg)
2103 "Join current line to previous, fix whitespace, continuation, comments.
2104 With optional argument ARG, join current line to following line.
2105 Like `join-line', but handles F90 syntax."
2106 (interactive "*P")
2107 (beginning-of-line)
2108 (if arg (forward-line 1))
2109 (when (eq (preceding-char) ?\n)
2110 (skip-chars-forward " \t")
2111 (if (looking-at "\&") (delete-char 1))
2112 (beginning-of-line)
2113 (delete-region (point) (1- (point)))
2114 (skip-chars-backward " \t")
2115 (and (eq (preceding-char) ?&) (delete-char -1))
2116 (and (f90-in-comment)
2117 (looking-at "[ \t]*!+")
2118 (replace-match ""))
2119 (or (f90-in-string)
2120 (fixup-whitespace))))
2122 (defun f90-fill-region (beg-region end-region)
2123 "Fill every line in region by forward parsing. Join lines if possible."
2124 (interactive "*r")
2125 (let ((end-region-mark (copy-marker end-region))
2126 (go-on t)
2127 f90-smart-end f90-auto-keyword-case auto-fill-function)
2128 (goto-char beg-region)
2129 (while go-on
2130 ;; Join as much as possible.
2131 (while (progn
2132 (end-of-line)
2133 (skip-chars-backward " \t")
2134 (eq (preceding-char) ?&))
2135 (f90-join-lines 'forward))
2136 ;; Chop the line if necessary.
2137 (while (> (save-excursion (end-of-line) (current-column))
2138 fill-column)
2139 (move-to-column fill-column)
2140 (f90-find-breakpoint)
2141 (f90-break-line 'no-update))
2142 (setq go-on (and (< (point) end-region-mark)
2143 (zerop (forward-line 1)))
2144 f90-cache-position (point)))
2145 (setq f90-cache-position nil)
2146 (set-marker end-region-mark nil)
2147 (if (featurep 'xemacs)
2148 (zmacs-deactivate-region)
2149 (deactivate-mark))))
2151 (defconst f90-end-block-optional-name
2152 '("program" "module" "subroutine" "function" "type")
2153 "Block types where including the name in the end statement is optional.")
2155 (defun f90-block-match (beg-block beg-name end-block end-name)
2156 "Match end-struct with beg-struct and complete end-block if possible.
2157 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
2158 BEG-NAME is the block start name (may be nil).
2159 END-BLOCK is the type of block as indicated at the end (may be nil).
2160 END-NAME is the block end name (may be nil).
2161 If the block type matches `f90-end-block-optional-name', do not add
2162 an end name if `f90-smart-end-names' is nil, but always update an
2163 incorrect end name if there already was one.
2164 Leave point at the end of line."
2165 ;; Hack to deal with the case when this is called from
2166 ;; f90-indent-region on a program block without an explicit PROGRAM
2167 ;; statement at the start. Should really be an error (?).
2168 (or beg-block (setq beg-block "program"))
2169 (search-forward "end" (line-end-position))
2170 (catch 'no-match
2171 (if (and end-block (f90-equal-symbols beg-block end-block))
2172 (search-forward end-block)
2173 (if end-block
2174 (progn
2175 (message "END %s does not match %s." end-block beg-block)
2176 (end-of-line)
2177 (throw 'no-match nil))
2178 (message "Inserting %s." beg-block)
2179 (insert (concat " " beg-block))))
2180 (if (f90-equal-symbols beg-name end-name)
2181 (and end-name (search-forward end-name))
2182 (cond ((and beg-name (not end-name))
2183 (unless (and (not f90-smart-end-names)
2184 (member-ignore-case beg-block
2185 f90-end-block-optional-name))
2186 (message "Inserting %s." beg-name)
2187 (insert (concat " " beg-name))))
2188 ((and beg-name end-name)
2189 (message "Replacing %s with %s." end-name beg-name)
2190 (search-forward end-name)
2191 (replace-match beg-name))
2192 ((and (not beg-name) end-name)
2193 (message "Deleting %s." end-name)
2194 (search-forward end-name)
2195 (replace-match ""))))
2196 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
2198 (defun f90-match-end ()
2199 "From an end block statement, find the corresponding block and name."
2200 (interactive)
2201 (let ((count 1)
2202 (top-of-window (window-start))
2203 (end-point (point))
2204 (case-fold-search t)
2205 matching-beg beg-name end-name beg-block end-block end-struct)
2206 ;; Check if in string in case using non-standard feature where
2207 ;; continued strings do not need "&" at start of continuations.
2208 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
2209 (unless (f90-in-string)
2210 (setq end-struct
2211 (f90-looking-at-program-block-end))))
2212 (setq end-block (car end-struct)
2213 end-name (cadr end-struct))
2214 (save-excursion
2215 (beginning-of-line)
2216 (while (and (> count 0)
2217 (not (= (line-beginning-position) (point-min))))
2218 (re-search-backward f90-blocks-re nil 'move)
2219 (beginning-of-line)
2220 ;; GM not a line number if continued line.
2221 ;;; (skip-chars-forward " \t")
2222 ;;; (skip-chars-forward "0-9")
2223 (skip-chars-forward " \t0-9")
2224 (cond ((or (f90-in-string) (f90-in-comment)))
2225 ((setq matching-beg
2227 (f90-looking-at-do)
2228 (f90-looking-at-if-then)
2229 (f90-looking-at-where-or-forall)
2230 (f90-looking-at-select-case)
2231 (f90-looking-at-type-like)
2232 (f90-looking-at-associate)
2233 (f90-looking-at-critical)
2234 (f90-looking-at-program-block-start)
2235 ;; Interpret a single END without a block
2236 ;; start to be the END of a program block
2237 ;; without an initial PROGRAM line.
2238 (if (= (line-beginning-position) (point-min))
2239 '("program" nil))))
2240 (setq count (1- count)))
2241 ((looking-at (concat "end[ \t]*" f90-blocks-re))
2242 (setq count (1+ count)))))
2243 (if (> count 0)
2244 (message "No matching beginning.")
2245 (f90-update-line)
2246 (if (eq f90-smart-end 'blink)
2247 (if (< (point) top-of-window)
2248 (message "Matches %s: %s"
2249 (what-line)
2250 (buffer-substring
2251 (line-beginning-position)
2252 (line-end-position)))
2253 (sit-for blink-matching-delay)))
2254 (setq beg-block (car matching-beg)
2255 beg-name (cadr matching-beg))
2256 (goto-char end-point)
2257 (beginning-of-line)
2258 (f90-block-match beg-block beg-name end-block end-name))))))
2260 (defun f90-insert-end ()
2261 "Insert a complete end statement matching beginning of present block."
2262 (interactive "*")
2263 (let ((f90-smart-end (or f90-smart-end 'blink)))
2264 (insert "end")
2265 (f90-indent-new-line)))
2267 ;; Abbrevs and keywords.
2269 (defun f90-abbrev-start ()
2270 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
2271 Any other key combination is executed normally."
2272 (interactive "*")
2273 (self-insert-command 1)
2274 (when abbrev-mode
2275 (set-transient-map
2276 (let ((map (make-sparse-keymap)))
2277 (define-key map [??] 'f90-abbrev-help)
2278 (define-key map (vector help-char) 'f90-abbrev-help)
2279 map))))
2281 (defun f90-abbrev-help ()
2282 "List the currently defined abbrevs in F90 mode."
2283 (interactive)
2284 (message "Listing abbrev table...")
2285 (display-buffer (f90-prepare-abbrev-list-buffer))
2286 (message "Listing abbrev table...done"))
2288 (defun f90-prepare-abbrev-list-buffer ()
2289 "Create a buffer listing the F90 mode abbreviations."
2290 (with-current-buffer (get-buffer-create "*Abbrevs*")
2291 (erase-buffer)
2292 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
2293 (goto-char (point-min))
2294 (set-buffer-modified-p nil)
2295 (edit-abbrevs-mode))
2296 (get-buffer-create "*Abbrevs*"))
2298 (defun f90-upcase-keywords ()
2299 "Upcase all F90 keywords in the buffer."
2300 (interactive "*")
2301 (f90-change-keywords 'upcase-word))
2303 (defun f90-capitalize-keywords ()
2304 "Capitalize all F90 keywords in the buffer."
2305 (interactive "*")
2306 (f90-change-keywords 'capitalize-word))
2308 (defun f90-downcase-keywords ()
2309 "Downcase all F90 keywords in the buffer."
2310 (interactive "*")
2311 (f90-change-keywords 'downcase-word))
2313 (defun f90-upcase-region-keywords (beg end)
2314 "Upcase all F90 keywords in the region."
2315 (interactive "*r")
2316 (f90-change-keywords 'upcase-word beg end))
2318 (defun f90-capitalize-region-keywords (beg end)
2319 "Capitalize all F90 keywords in the region."
2320 (interactive "*r")
2321 (f90-change-keywords 'capitalize-word beg end))
2323 (defun f90-downcase-region-keywords (beg end)
2324 "Downcase all F90 keywords in the region."
2325 (interactive "*r")
2326 (f90-change-keywords 'downcase-word beg end))
2328 ;; Change the keywords according to argument.
2329 (defun f90-change-keywords (change-word &optional beg end)
2330 "Change the case of F90 keywords in the region (if specified) or buffer.
2331 CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
2332 (save-excursion
2333 (setq beg (or beg (point-min))
2334 end (or end (point-max)))
2335 (let ((keyword-re
2336 (concat "\\("
2337 f90-keywords-re "\\|" f90-procedures-re "\\|"
2338 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
2339 (ref-point (point-min))
2340 (modified (buffer-modified-p))
2341 state saveword back-point)
2342 (goto-char beg)
2343 (unwind-protect
2344 (while (re-search-forward keyword-re end t)
2345 (unless (progn
2346 (setq state (parse-partial-sexp ref-point (point)))
2347 (or (nth 3 state) (nth 4 state)
2348 ;; GM f90-directive-comment-re?
2349 (save-excursion ; check for cpp directive
2350 (beginning-of-line)
2351 (skip-chars-forward " \t0-9")
2352 (looking-at "#"))))
2353 (setq ref-point (point)
2354 ;; FIXME this does not work for constructs with
2355 ;; embedded space, eg "sync all".
2356 back-point (save-excursion (backward-word 1) (point))
2357 saveword (buffer-substring back-point ref-point))
2358 (funcall change-word -1)
2359 (or (string= saveword (buffer-substring back-point ref-point))
2360 (setq modified t))))
2361 (or modified (restore-buffer-modified-p nil))))))
2364 (defun f90-current-defun ()
2365 "Function to use for `add-log-current-defun-function' in F90 mode."
2366 (save-excursion
2367 (nth 1 (f90-beginning-of-subprogram))))
2369 (defun f90-backslash-not-special (&optional all)
2370 "Make the backslash character (\\) be non-special in the current buffer.
2371 With optional argument ALL, change the default for all present
2372 and future F90 buffers. F90 mode normally treats backslash as an
2373 escape character."
2374 (or (derived-mode-p 'f90-mode)
2375 (error "This function should only be used in F90 buffers"))
2376 (when (equal (char-syntax ?\\ ) ?\\ )
2377 (or all (set-syntax-table (copy-syntax-table (syntax-table))))
2378 (modify-syntax-entry ?\\ ".")))
2381 (provide 'f90)
2383 ;; Local Variables:
2384 ;; coding: utf-8
2385 ;; End:
2387 ;;; f90.el ends here