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