Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / progmodes / f90.el
blob6aee713dd8690dbb1c8692775801dfd329a51510
1 ;;; f90.el --- Fortran-90 mode (free format) -*- lexical-binding: t -*-
3 ;; Copyright (C) 1995-1997, 2000-2014 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 may get more info by using
75 ;; the command describe-mode (C-h m). For online help use
76 ;; C-h f <Name of function you want described>, or
77 ;; C-h v <Name of variable you want described>.
79 ;; To customize f90-mode for your taste, use, for example:
80 ;; (you don't have to specify values for all the parameters below)
82 ;; (add-hook 'f90-mode-hook
83 ;; ;; These are the default values.
84 ;; (lambda () (setq f90-do-indent 3
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 "contiguous" "submodule" "concurrent" "codimension"
346 "sync all" "sync memory" "critical" "image_index"
348 "\\_>")
349 "Regexp used by the function `f90-change-keywords'.")
351 (defconst f90-keywords-level-3-re
352 (concat
353 "\\_<"
354 (regexp-opt
355 '("allocatable" "allocate" "assign" "assignment" "backspace"
356 "close" "deallocate" "dimension" "endfile" "entry" "equivalence"
357 "external" "inquire" "intent" "intrinsic" "nullify" "only" "open"
358 ;; FIXME operator and assignment should be F2003 procedures?
359 "operator" "optional" "parameter" "pause" "pointer" "print" "private"
360 "public" "read" "recursive" "result" "rewind" "save" "select"
361 "sequence" "target" "write"
362 ;; F95 keywords.
363 "elemental" "pure"
364 ;; F2003. asynchronous separate.
365 "abstract" "deferred" "import" "final" "non_intrinsic" "non_overridable"
366 "nopass" "pass" "protected" "value" "volatile"
367 ;; F2008.
368 ;; "concurrent" is only in the sense of "do [,] concurrent", but given
369 ;; the [,] it's simpler to just do every instance (cf "do while").
370 "contiguous" "concurrent" "codimension" "sync all" "sync memory"
372 "\\_>")
373 "Keyword-regexp for font-lock level >= 3.")
375 (defconst f90-procedures-re
376 (concat "\\_<"
377 (regexp-opt
378 '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
379 "all" "allocated" "anint" "any" "asin" "associated"
380 "atan" "atan2" "bit_size" "btest" "ceiling" "char" "cmplx"
381 "conjg" "cos" "cosh" "count" "cshift" "date_and_time" "dble"
382 "digits" "dim" "dot_product" "dprod" "eoshift" "epsilon"
383 "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
384 "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior"
385 "ishft" "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt"
386 "lle" "llt" "log" "log10" "logical" "matmul" "max"
387 "maxexponent" "maxloc" "maxval" "merge" "min" "minexponent"
388 "minloc" "minval" "mod" "modulo" "mvbits" "nearest" "nint"
389 "not" "pack" "precision" "present" "product" "radix"
390 ;; Real is taken out here to avoid highlighting declarations.
391 "random_number" "random_seed" "range" ;; "real"
392 "repeat" "reshape" "rrspacing" "scale" "scan"
393 "selected_int_kind" "selected_real_kind" "set_exponent"
394 "shape" "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt"
395 "sum" "system_clock" "tan" "tanh" "tiny" "transfer"
396 "transpose" "trim" "ubound" "unpack" "verify"
397 ;; F95 intrinsic functions.
398 "null" "cpu_time"
399 ;; F2003.
400 "move_alloc" "command_argument_count" "get_command"
401 "get_command_argument" "get_environment_variable"
402 "selected_char_kind" "wait" "flush" "new_line"
403 "extends" "extends_type_of" "same_type_as" "bind"
404 ;; F2003 ieee_arithmetic intrinsic module.
405 "ieee_support_underflow_control" "ieee_get_underflow_mode"
406 "ieee_set_underflow_mode"
407 ;; F2003 iso_c_binding intrinsic module.
408 "c_loc" "c_funloc" "c_associated" "c_f_pointer"
409 "c_f_procpointer"
410 ;; F2008.
411 "bge" "bgt" "ble" "blt" "dshiftl" "dshiftr" "leadz" "popcnt"
412 "poppar" "trailz" "maskl" "maskr" "shifta" "shiftl" "shiftr"
413 "merge_bits" "iall" "iany" "iparity" "storage_size"
414 "bessel_j0" "bessel_j1" "bessel_jn"
415 "bessel_y0" "bessel_y1" "bessel_yn"
416 "erf" "erfc" "erfc_scaled" "gamma" "hypot" "log_gamma"
417 "norm2" "parity" "findloc" "is_contiguous"
418 "sync images" "lock" "unlock" "image_index"
419 "lcobound" "ucobound" "num_images" "this_image"
420 ;; F2008 iso_fortran_env module.
421 "compiler_options" "compiler_version"
422 ;; F2008 iso_c_binding module.
423 "c_sizeof"
424 ) t)
425 ;; A left parenthesis to avoid highlighting non-procedures.
426 "[ \t]*(")
427 "Regexp whose first part matches F90 intrinsic procedures.")
429 (defconst f90-operators-re
430 (concat "\\."
431 (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
432 "neqv" "not" "or" "true") t)
433 "\\.")
434 "Regexp matching intrinsic operators.")
436 (defconst f90-hpf-keywords-re
437 (concat
438 "\\_<"
439 (regexp-opt
440 ;; Intrinsic procedures.
441 '("all_prefix" "all_scatter" "all_suffix" "any_prefix"
442 "any_scatter" "any_suffix" "copy_prefix" "copy_scatter"
443 "copy_suffix" "count_prefix" "count_scatter" "count_suffix"
444 "grade_down" "grade_up"
445 "hpf_alignment" "hpf_distribution" "hpf_template" "iall" "iall_prefix"
446 "iall_scatter" "iall_suffix" "iany" "iany_prefix" "iany_scatter"
447 "iany_suffix" "ilen" "iparity" "iparity_prefix"
448 "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
449 "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter"
450 "minval_suffix" "number_of_processors" "parity"
451 "parity_prefix" "parity_scatter" "parity_suffix" "popcnt" "poppar"
452 "processors_shape" "product_prefix" "product_scatter"
453 "product_suffix" "sum_prefix" "sum_scatter" "sum_suffix"
454 ;; Directives.
455 "align" "distribute" "dynamic" "independent" "inherit" "processors"
456 "realign" "redistribute" "template"
457 ;; Keywords.
458 "block" "cyclic" "extrinsic" "new" "onto" "pure" "with"))
459 "\\_>")
460 "Regexp for all HPF keywords, procedures and directives.")
462 (defconst f90-constants-re
463 (concat
464 "\\_<"
465 (regexp-opt '( ;; F2003 iso_fortran_env constants.
466 "iso_fortran_env"
467 "input_unit" "output_unit" "error_unit"
468 "iostat_end" "iostat_eor"
469 "numeric_storage_size" "character_storage_size"
470 "file_storage_size"
471 ;; F2003 iso_c_binding constants.
472 "iso_c_binding"
473 "c_int" "c_short" "c_long" "c_long_long" "c_signed_char"
474 "c_size_t"
475 "c_int8_t" "c_int16_t" "c_int32_t" "c_int64_t"
476 "c_int_least8_t" "c_int_least16_t" "c_int_least32_t"
477 "c_int_least64_t"
478 "c_int_fast8_t" "c_int_fast16_t" "c_int_fast32_t"
479 "c_int_fast64_t"
480 "c_intmax_t" "c_intptr_t"
481 "c_float" "c_double" "c_long_double"
482 "c_float_complex" "c_double_complex" "c_long_double_complex"
483 "c_bool" "c_char"
484 "c_null_char" "c_alert" "c_backspace" "c_form_feed"
485 "c_new_line" "c_carriage_return" "c_horizontal_tab"
486 "c_vertical_tab"
487 "c_ptr" "c_funptr" "c_null_ptr" "c_null_funptr"
488 "ieee_exceptions"
489 "ieee_arithmetic"
490 "ieee_features"
491 ;; F2008 iso_fortran_env constants.
492 "character_kinds" "int8" "int16" "int32" "int64"
493 "integer_kinds" "iostat_inquire_internal_unit"
494 "logical_kinds" "real_kinds" "real32" "real64" "real128"
495 "lock_type" "atomic_int_kind" "atomic_logical_kind"
497 "\\_>")
498 "Regexp for Fortran intrinsic constants.")
500 ;; cf f90-looking-at-type-like.
501 (defun f90-typedef-matcher (limit)
502 "Search for the start/end of the definition of a derived type, up to LIMIT.
503 Set the match data so that subexpression 1,2 are the TYPE, and
504 type-name parts, respectively."
505 (let (found l)
506 (while (and (re-search-forward "\\_<\\(\\(?:end[ \t]*\\)?type\\)\\_>[ \t]*"
507 limit t)
508 (not (setq found
509 (progn
510 (setq l (match-data))
511 (unless (looking-at "\\(is\\_>\\|(\\)")
512 (when (if (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
513 (goto-char (match-end 0))
514 (re-search-forward
515 "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
516 (line-end-position) t))
517 ;; 0 is wrong, but we don't use it.
518 (set-match-data
519 (append l (list (match-beginning 1)
520 (match-end 1))))
521 t)))))))
522 found))
524 (defvar f90-font-lock-keywords-1
525 (list
526 ;; Special highlighting of "module procedure".
527 '("\\_<\\(module[ \t]*procedure\\)\\_>\\([^()\n]*::\\)?[ \t]*\\([^&!\n]*\\)"
528 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
529 ;; Highlight definition of derived type.
530 ;;; '("\\_<\\(\\(?:end[ \t]*\\)?type\\)\\_>\\([^()\n]*::\\)?[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
531 ;;; (1 font-lock-keyword-face) (3 font-lock-function-name-face))
532 '(f90-typedef-matcher
533 (1 font-lock-keyword-face) (2 font-lock-function-name-face))
534 ;; F2003. Prevent operators being highlighted as functions.
535 '("\\_<\\(\\(?:end[ \t]*\\)?interface[ \t]*\\(?:assignment\\|operator\\|\
536 read\\|write\\)\\)[ \t]*(" (1 font-lock-keyword-face t))
537 ;; Other functions and declarations. Named interfaces = F2003.
538 ;; F2008: end submodule submodule_name.
539 '("\\_<\\(\\(?:end[ \t]*\\)?\\(program\\|\\(?:sub\\)?module\\|\
540 function\\|associate\\|subroutine\\|interface\\)\\|use\\|call\\)\
541 \\_>[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
542 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
543 ;; F2008: submodule (parent_name) submodule_name.
544 '("\\_<\\(submodule\\)\\_>[ \t]*([^)\n]+)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
545 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
546 ;; F2003.
547 '("\\_<\\(use\\)[ \t]*,[ \t]*\\(\\(?:non_\\)?intrinsic\\)[ \t]*::[ \t]*\
548 \\(\\(?:\\sw\\|\\s_\\)+\\)"
549 (1 font-lock-keyword-face) (2 font-lock-keyword-face)
550 (3 font-lock-function-name-face))
551 "\\_<\\(\\(end[ \t]*\\)?block[ \t]*data\\|contains\\)\\_>"
552 ;; "abstract interface" is F2003.
553 '("\\_<abstract[ \t]*interface\\_>" (0 font-lock-keyword-face t)))
554 "This does fairly subdued highlighting of comments and function calls.")
556 ;; NB not explicitly handling this, yet it seems to work.
557 ;; type(...) function foo()
558 (defun f90-typedec-matcher (limit)
559 "Search for the declaration of variables of derived type, up to LIMIT.
560 Set the match data so that subexpression 1,2 are the TYPE(...),
561 and variable-name parts, respectively."
562 ;; Matcher functions must return nil only when there are no more
563 ;; matches within the search range.
564 (let (found l)
565 (while (and (re-search-forward "\\_<\\(type\\|class\\)[ \t]*(" limit t)
566 (not
567 (setq found
568 (condition-case nil
569 (progn
570 ;; Set l after this to just highlight
571 ;; the "type" part.
572 (backward-char 1)
573 ;; Needed for: type( foo(...) ) :: bar
574 (forward-sexp)
575 (setq l (list (match-beginning 0) (point)))
576 (skip-chars-forward " \t")
577 (when
578 (re-search-forward
579 ;; type (foo) bar, qux
580 (if (looking-at "\\(?:\\sw\\|\\s_\\)+")
581 "\\([^&!\n]+\\)"
582 ;; type (foo), stuff :: bar, qux
583 "::[ \t]*\\([^&!\n]+\\)")
584 (line-end-position) t)
585 (set-match-data
586 (append (list (car l) (match-end 1))
587 l (list (match-beginning 1)
588 (match-end 1))))
590 (error nil))))))
591 found))
593 (defvar f90-font-lock-keywords-2
594 (append
595 f90-font-lock-keywords-1
596 (list
597 ;; Variable declarations (avoid the real function call).
598 ;; NB by accident (?), this correctly fontifies the "integer" in:
599 ;; integer () function foo ()
600 ;; because "() function foo ()" matches \\3.
601 ;; The "pure" part does not really belong here, but was added to
602 ;; exploit that hack.
603 ;; The "function foo" bit is correctly fontified by keywords-1.
604 ;; TODO ? actually check for balanced parens in that case.
605 '("^[ \t0-9]*\\(?:pure\\|elemental\\)?[ \t]*\
606 \\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
607 enumerator\\|generic\\|procedure\\|logical\\|double[ \t]*precision\\)\
608 \\(.*::\\|[ \t]*(.*)\\)?\\([^&!\n]*\\)"
609 (1 font-lock-type-face t) (4 font-lock-variable-name-face t))
610 ;; Derived type/class variables.
611 ;; TODO ? If we just highlighted the "type" part, rather than
612 ;; "type(...)", this could be in the previous expression. And this
613 ;; would be consistent with integer( kind=8 ), etc.
614 '(f90-typedec-matcher
615 (1 font-lock-type-face) (2 font-lock-variable-name-face))
616 ;; "real function foo (args)". Must override previous. Note hack
617 ;; to get "args" unhighlighted again. Might not always be right,
618 ;; but probably better than leaving them as variables.
619 ;; NB not explicitly handling this case:
620 ;; integer( kind=1 ) function foo()
621 ;; thanks to the happy accident described above.
622 ;; Not anchored, so don't need to worry about "pure" etc.
623 '("\\_<\\(\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
624 logical\\|double[ \t]*precision\\|\
625 \\(?:type\\|class\\)[ \t]*([ \t]*\\(?:\\sw\\|\\s_\\)+[ \t]*)\\)[ \t]*\\)\
626 \\(function\\)\\_>[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*\\(([^&!\n]*)\\)"
627 (1 font-lock-type-face t) (4 font-lock-keyword-face t)
628 (5 font-lock-function-name-face t) (6 'default t))
629 ;; enum (F2003; must be followed by ", bind(C)").
630 '("\\_<\\(enum\\)[ \t]*," (1 font-lock-keyword-face))
631 ;; end do, enum (F2003), if, select, where, and forall constructs.
632 ;; block, critical (F2008).
633 ;; Note that "block data" may get somewhat mixed up with F2008 blocks,
634 ;; but since the former is obsolete I'm not going to worry about it.
635 '("\\_<\\(end[ \t]*\\(do\\|if\\|enum\\|select\\|forall\\|where\\|\
636 block\\|critical\\)\\)\\_>\
637 \\([ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?"
638 (1 font-lock-keyword-face) (3 font-lock-constant-face nil t))
639 '("^[ \t0-9]*\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|\
640 do\\([ \t]*while\\)?\\|select[ \t]*\\(?:case\\|type\\)\\|where\\|\
641 forall\\|block\\|critical\\)\\)\\_>"
642 (2 font-lock-constant-face nil t) (3 font-lock-keyword-face))
643 ;; Implicit declaration.
644 '("\\_<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\
645 \\|enumerator\\|procedure\\|\
646 logical\\|double[ \t]*precision\\|type[ \t]*(\\(?:\\sw\\|\\s_\\)+)\\|none\\)[ \t]*"
647 (1 font-lock-keyword-face) (2 font-lock-type-face))
648 '("\\_<\\(namelist\\|common\\)[ \t]*\/\\(\\(?:\\sw\\|\\s_\\)+\\)?\/"
649 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
650 "\\_<else\\([ \t]*if\\|where\\)?\\_>"
651 '("\\(&\\)[ \t]*\\(!\\|$\\)" (1 font-lock-keyword-face))
652 "\\_<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\_>"
653 '("\\_<\\(exit\\|cycle\\)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?\\_>"
654 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
655 '("\\_<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
656 ;; F2003 "class default".
657 '("\\_<\\(class\\)[ \t]*default" . 1)
658 ;; F2003 "type is" in a "select type" block.
659 '("\\_<\\(\\(type\\|class\\)[ \t]*is\\)[ \t]*(" (1 font-lock-keyword-face t))
660 '("\\_<\\(do\\|go[ \t]*to\\)\\_>[ \t]*\\([0-9]+\\)"
661 (1 font-lock-keyword-face) (2 font-lock-constant-face))
662 ;; Line numbers (lines whose first character after number is letter).
663 '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t))
664 ;; Override eg for "#include".
665 '("^#[ \t]*\\(?:\\sw\\|\\s_\\)+" (0 font-lock-preprocessor-face t)
666 ("\\_<defined\\_>" nil nil (0 font-lock-preprocessor-face)))
667 '("^#" ("\\(&&\\|||\\)" nil nil (0 font-lock-constant-face t)))
668 '("^#[ \t]*define[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)(" (1 font-lock-function-name-face))
669 '("^#[ \t]*define[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)" (1 font-lock-variable-name-face))
670 '("^#[ \t]*include[ \t]+\\(<.+>\\)" (1 font-lock-string-face))))
671 "Highlights declarations, do-loops and other constructs.")
673 (defvar f90-font-lock-keywords-3
674 (append f90-font-lock-keywords-2
675 (list
676 f90-keywords-level-3-re
677 f90-operators-re
678 ;; FIXME why isn't this font-lock-builtin-face, which
679 ;; otherwise we hardly use, as in fortran.el?
680 (list f90-procedures-re '(1 font-lock-keyword-face keep))
681 "\\_<real\\_>" ; avoid overwriting real defs
682 ;; As an attribute, but not as an optional argument.
683 '("\\_<\\(asynchronous\\)[ \t]*[^=]" . 1)))
684 "Highlights all F90 keywords and intrinsic procedures.")
686 (defvar f90-font-lock-keywords-4
687 (append f90-font-lock-keywords-3
688 (list (cons f90-constants-re 'font-lock-constant-face)
689 f90-hpf-keywords-re))
690 "Highlights all F90 and HPF keywords and constants.")
692 (defvar f90-font-lock-keywords
693 f90-font-lock-keywords-2
694 "Default expressions to highlight in F90 mode.
695 Can be overridden by the value of `font-lock-maximum-decoration'.")
698 (defvar f90-mode-syntax-table
699 (let ((table (make-syntax-table)))
700 (modify-syntax-entry ?\! "<" table) ; begin comment
701 (modify-syntax-entry ?\n ">" table) ; end comment
702 (modify-syntax-entry ?_ "_" table) ; underscore in names
703 (modify-syntax-entry ?\' "\"" table) ; string quote
704 (modify-syntax-entry ?\" "\"" table) ; string quote
705 ;; FIXME: We used to set ` to word syntax for the benefit of abbrevs, but
706 ;; we do not need it any more. Not sure if it should be "_" or "." now.
707 (modify-syntax-entry ?\` "_" table)
708 (modify-syntax-entry ?\r " " table) ; return is whitespace
709 (modify-syntax-entry ?+ "." table) ; punctuation
710 (modify-syntax-entry ?- "." table)
711 (modify-syntax-entry ?= "." table)
712 (modify-syntax-entry ?* "." table)
713 (modify-syntax-entry ?/ "." table)
714 (modify-syntax-entry ?% "." table) ; bug#8820
715 ;; I think that the f95 standard leaves the behavior of \
716 ;; unspecified, but that f2k will require it to be non-special.
717 ;; Use `f90-backslash-not-special' to change.
718 (modify-syntax-entry ?\\ "\\" table) ; escape chars
719 table)
720 "Syntax table used in F90 mode.")
722 (defvar f90-mode-map
723 (let ((map (make-sparse-keymap)))
724 (define-key map "`" 'f90-abbrev-start)
725 (define-key map "\C-c;" 'f90-comment-region)
726 (define-key map "\C-\M-a" 'f90-beginning-of-subprogram)
727 (define-key map "\C-\M-e" 'f90-end-of-subprogram)
728 (define-key map "\C-\M-h" 'f90-mark-subprogram)
729 (define-key map "\C-\M-n" 'f90-end-of-block)
730 (define-key map "\C-\M-p" 'f90-beginning-of-block)
731 (define-key map "\C-\M-q" 'f90-indent-subprogram)
732 (define-key map "\C-j" 'f90-indent-new-line) ; LFD equals C-j
733 ;;; (define-key map "\r" 'newline)
734 (define-key map "\C-c\r" 'f90-break-line)
735 ;;; (define-key map [M-return] 'f90-break-line)
736 (define-key map "\C-c\C-a" 'f90-previous-block)
737 (define-key map "\C-c\C-e" 'f90-next-block)
738 (define-key map "\C-c\C-d" 'f90-join-lines)
739 (define-key map "\C-c\C-f" 'f90-fill-region)
740 (define-key map "\C-c\C-p" 'f90-previous-statement)
741 (define-key map "\C-c\C-n" 'f90-next-statement)
742 (define-key map "\C-c]" 'f90-insert-end)
743 (define-key map "\C-c\C-w" 'f90-insert-end)
744 ;; Standard tab binding will call this, and also handle regions.
745 ;;; (define-key map "\t" 'f90-indent-line)
746 (define-key map "," 'f90-electric-insert)
747 (define-key map "+" 'f90-electric-insert)
748 (define-key map "-" 'f90-electric-insert)
749 (define-key map "*" 'f90-electric-insert)
750 (define-key map "/" 'f90-electric-insert)
752 (easy-menu-define f90-menu map "Menu for F90 mode."
753 `("F90"
754 ("Customization"
755 ,(custom-menu-create 'f90)
756 ;; FIXME useless?
757 ["Set" Custom-set :active t
758 :help "Set current value of all edited settings in the buffer"]
759 ["Save" Custom-save :active t
760 :help "Set and save all edited settings"]
761 ["Reset to Current" Custom-reset-current :active t
762 :help "Reset all edited settings to current"]
763 ["Reset to Saved" Custom-reset-saved :active t
764 :help "Reset all edited or set settings to saved"]
765 ["Reset to Standard Settings" Custom-reset-standard :active t
766 :help "Erase all customizations in buffer"]
768 "--"
769 ["Indent Subprogram" f90-indent-subprogram t]
770 ["Mark Subprogram" f90-mark-subprogram :active t :help
771 "Mark the end of the current subprogram, move point to the start"]
772 ["Beginning of Subprogram" f90-beginning-of-subprogram :active t
773 :help "Move point to the start of the current subprogram"]
774 ["End of Subprogram" f90-end-of-subprogram :active t
775 :help "Move point to the end of the current subprogram"]
776 "--"
777 ["(Un)Comment Region" f90-comment-region :active mark-active
778 :help "Comment or uncomment the region"]
779 ["Indent Region" f90-indent-region :active mark-active]
780 ["Fill Region" f90-fill-region :active mark-active
781 :help "Fill long lines in the region"]
782 "--"
783 ["Break Line at Point" f90-break-line :active t
784 :help "Break the current line at point"]
785 ["Join with Previous Line" f90-join-lines :active t
786 :help "Join the current line to the previous one"]
787 ["Insert Block End" f90-insert-end :active t
788 :help "Insert an end statement for the current code block"]
789 "--"
790 ("Highlighting"
791 :help "Fontify this buffer to varying degrees"
792 ["Toggle font-lock-mode" font-lock-mode :selected font-lock-mode
793 :style toggle :help "Fontify text in this buffer"]
794 "--"
795 ["Light highlighting (level 1)" f90-font-lock-1 t]
796 ["Moderate highlighting (level 2)" f90-font-lock-2 t]
797 ["Heavy highlighting (level 3)" f90-font-lock-3 t]
798 ["Maximum highlighting (level 4)" f90-font-lock-4 t]
800 ("Change Keyword Case"
801 :help "Change the case of keywords in the buffer or region"
802 ["Upcase Keywords (buffer)" f90-upcase-keywords t]
803 ["Capitalize Keywords (buffer)" f90-capitalize-keywords t]
804 ["Downcase Keywords (buffer)" f90-downcase-keywords t]
805 "--"
806 ["Upcase Keywords (region)" f90-upcase-region-keywords
807 mark-active]
808 ["Capitalize Keywords (region)" f90-capitalize-region-keywords
809 mark-active]
810 ["Downcase Keywords (region)" f90-downcase-region-keywords
811 mark-active]
813 "--"
814 ["Toggle Auto Fill" auto-fill-mode :selected auto-fill-function
815 :style toggle
816 :help "Automatically fill text while typing in this buffer"]
817 ["Toggle Abbrev Mode" abbrev-mode :selected abbrev-mode
818 :style toggle :help "Expand abbreviations while typing in this buffer"]
819 ["Add Imenu Menu" f90-add-imenu-menu
820 :active (not (lookup-key (current-local-map) [menu-bar index]))
821 :included (fboundp 'imenu-add-to-menubar)
822 :help "Add an index menu to the menu-bar"
824 map)
825 "Keymap used in F90 mode.")
828 (defun f90-font-lock-n (n)
829 "Set `font-lock-keywords' to F90 level N keywords."
830 (font-lock-mode 1)
831 (setq font-lock-keywords
832 (symbol-value (intern-soft (format "f90-font-lock-keywords-%d" n))))
833 (font-lock-fontify-buffer))
835 (defun f90-font-lock-1 ()
836 "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
837 (interactive)
838 (f90-font-lock-n 1))
840 (defun f90-font-lock-2 ()
841 "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
842 (interactive)
843 (f90-font-lock-n 2))
845 (defun f90-font-lock-3 ()
846 "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
847 (interactive)
848 (f90-font-lock-n 3))
850 (defun f90-font-lock-4 ()
851 "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
852 (interactive)
853 (f90-font-lock-n 4))
855 ;; Regexps for finding program structures.
856 (defconst f90-blocks-re
857 (concat "\\(\\(?:block[ \t]*data\\|"
858 (regexp-opt '("do" "if" "interface" "function" "module" "program"
859 "select" "subroutine" "type" "where" "forall"
860 ;; F2003.
861 "enum" "associate"
862 ;; F2008.
863 "submodule" "block" "critical"))
864 "\\)\\_>\\)")
865 "Regexp potentially indicating a \"block\" of F90 code.")
867 (defconst f90-program-block-re
868 (regexp-opt '("program" "module" "subroutine" "function" "submodule") 'paren)
869 "Regexp used to locate the start/end of a \"subprogram\".")
871 ;; "class is" is F2003.
872 (defconst f90-else-like-re
873 "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\|\
874 \\(class\\|type\\)[ \t]*is[ \t]*(\\|class[ \t]*default\\)"
875 "Regexp matching an ELSE IF, ELSEWHERE, CASE, CLASS/TYPE IS statement.")
877 (defconst f90-end-if-re
878 (concat "end[ \t]*"
879 (regexp-opt '("if" "select" "where" "forall") 'paren)
880 "\\_>")
881 "Regexp matching the end of an IF, SELECT, WHERE, FORALL block.")
883 (defconst f90-end-type-re
884 "end[ \t]*\\(type\\|enum\\|interface\\|block[ \t]*data\\)\\_>"
885 "Regexp matching the end of a TYPE, ENUM, INTERFACE, BLOCK DATA section.")
887 (defconst f90-end-associate-re
888 "end[ \t]*associate\\_>"
889 "Regexp matching the end of an ASSOCIATE block.")
891 ;; This is for a TYPE block, not a variable of derived TYPE.
892 ;; Hence no need to add CLASS for F2003.
893 (defconst f90-type-def-re
894 ;; type word
895 ;; type :: word
896 ;; type, stuff :: word
897 ;; type, bind(c) :: word
898 ;; NOT "type ("
899 "\\_<\\(type\\)\\_>\\(?:\\(?:[^()\n]*\\|\
900 .*,[ \t]*bind[ \t]*([ \t]*c[ \t]*)[ \t]*\\)::\\)?[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
901 "Regexp matching the definition of a derived type.")
903 (defconst f90-typeis-re
904 "\\_<\\(class\\|type\\)[ \t]*is[ \t]*("
905 "Regexp matching a CLASS/TYPE IS statement.")
907 (defconst f90-no-break-re
908 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=" "(/" "/)") 'paren)
909 "Regexp specifying two-character tokens not to split when breaking lines.
910 Each token has one or more of the characters from `f90-break-delimiters'.
911 Note that if only one of the characters is from that variable,
912 then the presence of the token here allows a line-break before or
913 after the other character, where a break would not normally be
914 allowed. This minor issue currently only affects \"(/\" and \"/)\".")
916 (defvar f90-cache-position nil
917 "Temporary position used to speed up region operations.")
918 (make-variable-buffer-local 'f90-cache-position)
921 ;; Hideshow support.
922 (defconst f90-end-block-re
923 (concat "^[ \t0-9]*\\_<end[ \t]*"
924 (regexp-opt '("do" "if" "forall" "function" "interface"
925 "module" "program" "select" "subroutine"
926 "type" "where" "enum" "associate" "submodule"
927 "block" "critical") t)
928 "\\_>")
929 "Regexp matching the end of an F90 \"block\", from the line start.
930 Used in the F90 entry in `hs-special-modes-alist'.")
932 ;; Ignore the fact that FUNCTION, SUBROUTINE, WHERE, FORALL have a
933 ;; following "(". DO, CASE, IF can have labels.
934 (defconst f90-start-block-re
935 (concat
936 "^[ \t0-9]*" ; statement number
937 "\\(\\("
938 "\\(\\(?:\\sw\\|\\s_\\)+[ \t]*:[ \t]*\\)?" ; structure label
939 "\\(do\\|select[ \t]*\\(case\\|type\\)\\|"
940 ;; See comments in fortran-start-block-re for the problems of IF.
941 "if[ \t]*(\\(.*\\|"
942 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\_>\\)\\)\\)\\_<then\\|"
943 ;; Distinguish WHERE block from isolated WHERE.
944 "\\(where\\|forall\\)[ \t]*(.*)[ \t]*\\(!\\|$\\)\\)\\)"
945 "\\|"
946 ;; Avoid F2003 "type is" in "select type",
947 ;; and also variables of derived type "type (foo)".
948 ;; "type, foo" must be a block (?).
949 "type[ \t,]\\("
950 "[^i(!\n\"\& \t]\\|" ; not-i(
951 "i[^s!\n\"\& \t]\\|" ; i not-s
952 "is\\(?:\\sw\\|\\s_\\)\\)\\|"
953 ;; "abstract interface" is F2003; "submodule" is F2008.
954 "program\\|\\(?:abstract[ \t]*\\)?interface\\|\\(?:sub\\)?module\\|"
955 ;; "enum", but not "enumerator".
956 "function\\|subroutine\\|enum[^e]\\|associate\\|block\\|critical"
957 "\\)"
958 "[ \t]*")
959 "Regexp matching the start of an F90 \"block\", from the line start.
960 A simple regexp cannot do this in fully correct fashion, so this
961 tries to strike a compromise between complexity and flexibility.
962 Used in the F90 entry in `hs-special-modes-alist'.")
964 ;; hs-special-modes-alist is autoloaded.
965 (add-to-list 'hs-special-modes-alist
966 `(f90-mode ,f90-start-block-re ,f90-end-block-re
967 "!" f90-end-of-block nil))
970 ;; Imenu support.
971 ;; FIXME trivial to extend this to enum. Worth it?
972 (defun f90-imenu-type-matcher ()
973 "Search backward for the start of a derived type.
974 Set subexpression 1 in the match-data to the name of the type."
975 (let (found)
976 (while (and (re-search-backward "^[ \t0-9]*type[ \t]*" nil t)
977 (not (setq found
978 (save-excursion
979 (goto-char (match-end 0))
980 (unless (looking-at "\\(is\\_>\\|(\\)")
981 (or (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
982 (re-search-forward
983 "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
984 (line-end-position) t))))))))
985 found))
987 (defvar f90-imenu-generic-expression
988 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
989 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]")
990 ;; (not-ib "[^i(!\n\"\& \t]") (not-s "[^s!\n\"\& \t]")
992 `((nil "^[ \t0-9]*program[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)" 1)
993 ("Submodules" "^[ \t0-9]*submodule[ \t]*([^)\n]+)[ \t]*\
994 \\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*\\(!\\|$\\)" 1)
995 ("Modules" "^[ \t0-9]*module[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*\\(!\\|$\\)" 1)
996 ("Types" f90-imenu-type-matcher 1)
997 ;; Does not handle: "type[, stuff] :: foo".
998 ;;(format "^[ \t0-9]*type[ \t]+\\(\\(%s\\|i%s\\|is\\(?:\\sw\\|\\s_\\)\\)\\(?:\\sw\\|\\s_\\)*\\)"
999 ;; not-ib not-s)
1000 ;;1)
1001 ;; Can't get the subexpression numbers to match in the two branches.
1002 ;; FIXME: Now with \(?N:..\) we can get the numbers to match!
1003 ;;(format "^[ \t0-9]*type\\([ \t]*,.*\\(::\\)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)\\|[ \t]+\\(\\(%s\\|i%s\\|is\\(?:\\sw\\|\\s_\\)\\)\\(?:\\sw\\|\\s_\\)*\\)\\)" not-ib not-s)
1004 ;;3)
1005 ("Procedures"
1006 ,(concat
1007 "^[ \t0-9]*"
1008 "\\("
1009 ;; At least three non-space characters before function/subroutine.
1010 ;; Check that the last three non-space characters do not spell E N D.
1011 "[^!\"\&\n]*\\("
1012 not-e good-char good-char "\\|"
1013 good-char not-n good-char "\\|"
1014 good-char good-char not-d "\\)"
1015 "\\|"
1016 ;; Less than three non-space characters before function/subroutine.
1017 good-char "?" good-char "?"
1018 "\\)"
1019 "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)")
1020 4)))
1021 "Value for `imenu-generic-expression' in F90 mode.")
1023 (defun f90-add-imenu-menu ()
1024 "Add an imenu menu to the menubar."
1025 (interactive)
1026 (if (lookup-key (current-local-map) [menu-bar index])
1027 (message "%s" "F90-imenu already exists.")
1028 (imenu-add-to-menubar "F90-imenu")
1029 (redraw-frame)))
1032 ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
1033 (define-abbrev-table 'f90-mode-abbrev-table
1034 (mapcar (lambda (e) (list (car e) (cdr e) nil :system t))
1035 '(("`al" . "allocate" )
1036 ("`ab" . "allocatable" )
1037 ("`ai" . "abstract interface")
1038 ("`as" . "assignment" )
1039 ("`asy" . "asynchronous" )
1040 ("`ba" . "backspace" )
1041 ("`bd" . "block data" )
1042 ("`bl" . "block" )
1043 ("`c" . "character" )
1044 ("`cl" . "close" )
1045 ("`cm" . "common" )
1046 ("`cx" . "complex" )
1047 ("`cn" . "contains" )
1048 ("`cr" . "critical" )
1049 ("`cy" . "cycle" )
1050 ("`de" . "deallocate" )
1051 ("`df" . "define" )
1052 ("`di" . "dimension" )
1053 ("`dp" . "double precision")
1054 ("`dw" . "do while" )
1055 ("`el" . "else" )
1056 ("`eli" . "else if" )
1057 ("`elw" . "elsewhere" )
1058 ("`em" . "elemental" )
1059 ("`e" . "enumerator" )
1060 ("`eq" . "equivalence" )
1061 ("`ex" . "external" )
1062 ("`ey" . "entry" )
1063 ("`fl" . "forall" )
1064 ("`fo" . "format" )
1065 ("`fu" . "function" )
1066 ("`fa" . ".false." )
1067 ("`im" . "implicit none")
1068 ("`in" . "include" )
1069 ("`i" . "integer" )
1070 ("`it" . "intent" )
1071 ("`if" . "interface" )
1072 ("`lo" . "logical" )
1073 ("`mo" . "module" )
1074 ("`na" . "namelist" )
1075 ("`nu" . "nullify" )
1076 ("`op" . "optional" )
1077 ("`pa" . "parameter" )
1078 ("`po" . "pointer" )
1079 ("`pr" . "print" )
1080 ("`pi" . "private" )
1081 ("`pm" . "program" )
1082 ("`pr" . "protected" )
1083 ("`pu" . "public" )
1084 ("`r" . "real" )
1085 ("`rc" . "recursive" )
1086 ("`rt" . "return" )
1087 ("`rw" . "rewind" )
1088 ("`se" . "select" )
1089 ("`sq" . "sequence" )
1090 ("`su" . "subroutine" )
1091 ("`ta" . "target" )
1092 ("`tr" . ".true." )
1093 ("`t" . "type" )
1094 ("`vo" . "volatile" )
1095 ("`wh" . "where" )
1096 ("`wr" . "write" )))
1097 "Abbrev table for F90 mode."
1098 ;; Accept ` as the first char of an abbrev. Also allow _ in abbrevs.
1099 :regexp "\\(?:[^[:word:]_`]\\|^\\)\\(`?[[:word:]_]+\\)[^[:word:]_]*")
1101 ;;;###autoload
1102 (define-derived-mode f90-mode prog-mode "F90"
1103 "Major mode for editing Fortran 90,95 code in free format.
1104 For fixed format code, use `fortran-mode'.
1106 \\[f90-indent-line] indents the current line.
1107 \\[f90-indent-new-line] indents current line and creates a new\
1108 indented line.
1109 \\[f90-indent-subprogram] indents the current subprogram.
1111 Type `? or `\\[help-command] to display a list of built-in\
1112 abbrevs for F90 keywords.
1114 Key definitions:
1115 \\{f90-mode-map}
1117 Variables controlling indentation style and extra features:
1119 `f90-do-indent'
1120 Extra indentation within do blocks (default 3).
1121 `f90-if-indent'
1122 Extra indentation within if/select/where/forall blocks (default 3).
1123 `f90-type-indent'
1124 Extra indentation within type/enum/interface/block-data blocks (default 3).
1125 `f90-program-indent'
1126 Extra indentation within program/module/subroutine/function blocks
1127 (default 2).
1128 `f90-associate-indent'
1129 Extra indentation within associate blocks (default 2).
1130 `f90-critical-indent'
1131 Extra indentation within critical/block blocks (default 2).
1132 `f90-continuation-indent'
1133 Extra indentation applied to continuation lines (default 5).
1134 `f90-comment-region'
1135 String inserted by function \\[f90-comment-region] at start of each
1136 line in region (default \"!!!$\").
1137 `f90-indented-comment-re'
1138 Regexp determining the type of comment to be intended like code
1139 (default \"!\").
1140 `f90-directive-comment-re'
1141 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
1142 (default \"!hpf\\\\$\").
1143 `f90-break-delimiters'
1144 Regexp holding list of delimiters at which lines may be broken
1145 (default \"[-+*/><=,% \\t]\").
1146 `f90-break-before-delimiters'
1147 Non-nil causes `f90-do-auto-fill' to break lines before delimiters
1148 (default t).
1149 `f90-beginning-ampersand'
1150 Automatic insertion of \& at beginning of continuation lines (default t).
1151 `f90-smart-end'
1152 From an END statement, check and fill the end using matching block start.
1153 Allowed values are `blink', `no-blink', and nil, which determine
1154 whether to blink the matching beginning (default `blink').
1155 `f90-auto-keyword-case'
1156 Automatic change of case of keywords (default nil).
1157 The possibilities are `downcase-word', `upcase-word', `capitalize-word'.
1158 `f90-leave-line-no'
1159 Do not left-justify line numbers (default nil).
1161 Turning on F90 mode calls the value of the variable `f90-mode-hook'
1162 with no args, if that value is non-nil."
1163 :group 'f90
1164 :abbrev-table f90-mode-abbrev-table
1165 (set (make-local-variable 'indent-line-function) 'f90-indent-line)
1166 (set (make-local-variable 'indent-region-function) 'f90-indent-region)
1167 (set (make-local-variable 'comment-start) "!")
1168 (set (make-local-variable 'comment-start-skip) "!+ *")
1169 (set (make-local-variable 'comment-indent-function) 'f90-comment-indent)
1170 (set (make-local-variable 'abbrev-all-caps) t)
1171 (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
1172 (setq indent-tabs-mode nil) ; auto buffer local
1173 (set (make-local-variable 'font-lock-defaults)
1174 '((f90-font-lock-keywords f90-font-lock-keywords-1
1175 f90-font-lock-keywords-2
1176 f90-font-lock-keywords-3
1177 f90-font-lock-keywords-4)
1178 nil t))
1179 (set (make-local-variable 'imenu-case-fold-search) t)
1180 (set (make-local-variable 'imenu-generic-expression)
1181 f90-imenu-generic-expression)
1182 (set (make-local-variable 'beginning-of-defun-function)
1183 'f90-beginning-of-subprogram)
1184 (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
1185 (set (make-local-variable 'add-log-current-defun-function)
1186 #'f90-current-defun))
1189 ;; Inline-functions.
1190 (defsubst f90-in-string ()
1191 "Return non-nil if point is inside a string.
1192 Checks from `point-min', or `f90-cache-position', if that is non-nil
1193 and lies before point."
1194 (let ((beg-pnt
1195 (if (and f90-cache-position (> (point) f90-cache-position))
1196 f90-cache-position
1197 (point-min))))
1198 (nth 3 (parse-partial-sexp beg-pnt (point)))))
1200 (defsubst f90-in-comment ()
1201 "Return non-nil if point is inside a comment.
1202 Checks from `point-min', or `f90-cache-position', if that is non-nil
1203 and lies before point."
1204 (let ((beg-pnt
1205 (if (and f90-cache-position (> (point) f90-cache-position))
1206 f90-cache-position
1207 (point-min))))
1208 (nth 4 (parse-partial-sexp beg-pnt (point)))))
1210 (defsubst f90-line-continued ()
1211 "Return t if the current line is a continued one.
1212 This includes comment or preprocessor lines embedded in continued lines,
1213 but not the last line of a continued statement."
1214 (save-excursion
1215 (beginning-of-line)
1216 (while (and (looking-at "[ \t]*\\([!#]\\|$\\)") (zerop (forward-line -1))))
1217 (end-of-line)
1218 (while (f90-in-comment)
1219 (search-backward "!" (line-beginning-position))
1220 (skip-chars-backward "!"))
1221 (skip-chars-backward " \t")
1222 (= (preceding-char) ?&)))
1224 ;; GM this is not right, eg a continuation line starting with a number.
1225 ;; Need f90-code-start-position function.
1226 ;; And yet, things seems to work with this...
1227 ;; cf f90-indent-line
1228 ;; (beginning-of-line) ; digits after & \n are not line-nos
1229 ;; (if (not (save-excursion (and (f90-previous-statement)
1230 ;; (f90-line-continued))))
1231 ;; (f90-indent-line-no)
1232 (defsubst f90-current-indentation ()
1233 "Return indentation of current line.
1234 Line-numbers are considered whitespace characters."
1235 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
1237 (defsubst f90-indent-to (col &optional no-line-number)
1238 "Indent current line to column COL.
1239 If optional argument NO-LINE-NUMBER is nil, jump over a possible
1240 line-number before indenting."
1241 (beginning-of-line)
1242 (or no-line-number
1243 (skip-chars-forward " \t0-9"))
1244 (delete-horizontal-space)
1245 ;; Leave >= 1 space after line number.
1246 (indent-to col (if (zerop (current-column)) 0 1)))
1248 (defsubst f90-get-present-comment-type ()
1249 "If point lies within a comment, return the string starting the comment.
1250 For example, \"!\" or \"!!\", followed by the appropriate amount of
1251 whitespace, if any."
1252 ;; Include the whitespace for consistent auto-filling of comment blocks.
1253 (save-excursion
1254 (when (f90-in-comment)
1255 (beginning-of-line)
1256 (re-search-forward "!+[ \t]*" (line-end-position))
1257 (while (f90-in-string)
1258 (re-search-forward "!+[ \t]*" (line-end-position)))
1259 (match-string-no-properties 0))))
1261 (defsubst f90-equal-symbols (a b)
1262 "Compare strings A and B neglecting case and allowing for nil value."
1263 (equal (if a (downcase a) nil)
1264 (if b (downcase b) nil)))
1266 (defsubst f90-looking-at-do ()
1267 "Return (\"do\" NAME) if a do statement starts after point.
1268 NAME is nil if the statement has no label."
1269 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\_>")
1270 (list (match-string 3) (match-string 2))))
1272 (defsubst f90-looking-at-select-case ()
1273 "Return (\"select\" NAME) if a select statement starts after point.
1274 NAME is nil if the statement has no label."
1275 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\
1276 \\(select\\)[ \t]*\\(case\\|type\\)[ \t]*(")
1277 (list (match-string 3) (match-string 2))))
1279 (defsubst f90-looking-at-if-then ()
1280 "Return (\"if\" NAME) if an if () then statement starts after point.
1281 NAME is nil if the statement has no label."
1282 (save-excursion
1283 (when (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\_>")
1284 (let ((struct (match-string 3))
1285 (label (match-string 2))
1286 (pos (scan-lists (point) 1 0)))
1287 (and pos (goto-char pos))
1288 (skip-chars-forward " \t")
1289 (if (or (looking-at "then\\_>")
1290 (when (f90-line-continued)
1291 (f90-next-statement)
1292 (skip-chars-forward " \t0-9&")
1293 (looking-at "then\\_>")))
1294 (list struct label))))))
1296 ;; FIXME label?
1297 (defsubst f90-looking-at-associate ()
1298 "Return (\"associate\") if an associate block starts after point."
1299 (if (looking-at "\\_<\\(associate\\)[ \t]*(")
1300 (list (match-string 1))))
1302 (defsubst f90-looking-at-critical ()
1303 "Return (KIND NAME) if a critical or block block starts after point."
1304 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(critical\\|block\\)\\_>")
1305 (let ((struct (match-string 3))
1306 (label (match-string 2)))
1307 (if (or (not (string-equal "block" struct))
1308 (save-excursion
1309 (skip-chars-forward " \t")
1310 (not (looking-at "data\\_>"))))
1311 (list struct label)))))
1313 (defsubst f90-looking-at-end-critical ()
1314 "Return non-nil if a critical or block block ends after point."
1315 (if (looking-at "end[ \t]*\\(critical\\|block\\)\\_>")
1316 (or (not (string-equal "block" (match-string 1)))
1317 (save-excursion
1318 (skip-chars-forward " \t")
1319 (not (looking-at "data\\_>"))))))
1321 (defsubst f90-looking-at-where-or-forall ()
1322 "Return (KIND NAME) if a where or forall block starts after point.
1323 NAME is nil if the statement has no label."
1324 (save-excursion
1325 (when (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\
1326 \\(where\\|forall\\)\\_>")
1327 (let ((struct (match-string 3))
1328 (label (match-string 2))
1329 (pos (scan-lists (point) 1 0)))
1330 (and pos (goto-char pos))
1331 (skip-chars-forward " \t")
1332 (if (looking-at "\\(!\\|$\\)") (list struct label))))))
1334 (defsubst f90-looking-at-type-like ()
1335 "Return (KIND NAME) if a type/enum/interface/block-data starts after point.
1336 NAME is non-nil only for type and certain interfaces."
1337 (cond
1338 ((save-excursion
1339 (and (looking-at "\\_<type\\_>[ \t]*")
1340 (goto-char (match-end 0))
1341 (not (looking-at "\\(is\\_>\\|(\\)"))
1342 (or (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
1343 (re-search-forward "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
1344 (line-end-position) t))))
1345 (list "type" (match-string 1)))
1346 ;;; ((and (not (looking-at f90-typeis-re))
1347 ;;; (looking-at f90-type-def-re))
1348 ;;; (list (match-string 1) (match-string 2)))
1349 ((looking-at "\\_<\\(interface\\)\\_>[ \t]*")
1350 (list (match-string 1)
1351 (save-excursion
1352 (goto-char (match-end 0))
1353 (if (or (looking-at "\\(operator\\|assignment\\|read\\|\
1354 write\\)[ \t]*([^)\n]*)")
1355 (looking-at "\\(?:\\sw\\|\\s_\\)+"))
1356 (match-string 0)))))
1357 ((looking-at "\\(enum\\|block[ \t]*data\\)\\_>")
1358 (list (match-string 1) nil))
1359 ((looking-at "abstract[ \t]*\\(interface\\)\\_>")
1360 (list (match-string 1) nil))))
1362 (defsubst f90-looking-at-program-block-start ()
1363 "Return (KIND NAME) if a program block with name NAME starts after point."
1364 ;;;NAME is nil for an un-named main PROGRAM block."
1365 (cond
1366 ((looking-at "\\(program\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
1367 (list (match-string 1) (match-string 2)))
1368 ((and (not (looking-at "module[ \t]*procedure\\_>"))
1369 (looking-at "\\(module\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>"))
1370 (list (match-string 1) (match-string 2)))
1371 ((looking-at "\\(submodule\\)[ \t]*([^)\n]+)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
1372 (list (match-string 1) (match-string 2)))
1373 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
1374 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
1375 \\(\\(?:\\sw\\|\\s_\\)+\\)"))
1376 (list (match-string 1) (match-string 2)))))
1377 ;; Following will match an un-named main program block; however
1378 ;; one needs to check if there is an actual PROGRAM statement after
1379 ;; point (and before any END program). Adding this will require
1380 ;; change to eg f90-calculate-indent.
1381 ;;; ((save-excursion
1382 ;;; (not (f90-previous-statement)))
1383 ;;; '("program" nil))))
1385 (defsubst f90-looking-at-program-block-end ()
1386 "Return (KIND NAME) if a block with name NAME ends after point."
1387 (cond ((looking-at "end[ \t]*\\(interface\\)[ \t]*\\(\
1388 \\(?:assignment\\|operator\\|read\\|write\\)[ \t]*([^)\n]*)\\)")
1389 (list (match-string 1) (match-string 2)))
1390 ((looking-at (concat "end[ \t]*" f90-blocks-re
1391 "?\\([ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?\\_>"))
1392 (list (match-string 1) (match-string 3)))))
1394 (defsubst f90-comment-indent ()
1395 "Return the indentation to be used for a comment starting at point.
1396 Used for `comment-indent-function' by F90 mode.
1397 \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
1398 `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
1399 All others return `comment-column', leaving at least one space after code."
1400 (cond ((looking-at "!!!") 0)
1401 ((and f90-directive-comment-re
1402 (looking-at f90-directive-comment-re)) 0)
1403 ((looking-at (regexp-quote f90-comment-region)) 0)
1404 ((and (looking-at f90-indented-comment-re)
1405 ;; Don't attempt to indent trailing comment as code.
1406 (save-excursion
1407 (skip-chars-backward " \t")
1408 (bolp)))
1409 (f90-calculate-indent))
1410 (t (save-excursion
1411 (skip-chars-backward " \t")
1412 (max (if (bolp) 0 (1+ (current-column))) comment-column)))))
1414 (defsubst f90-present-statement-cont ()
1415 "Return continuation properties of present statement.
1416 Possible return values are:
1417 single - statement is not continued.
1418 begin - current line is the first in a continued statement.
1419 end - current line is the last in a continued statement
1420 middle - current line is neither first nor last in a continued statement.
1421 Comment lines embedded amongst continued lines return 'middle."
1422 (let (pcont cont)
1423 (save-excursion
1424 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
1425 (setq cont (f90-line-continued))
1426 (cond ((and (not pcont) (not cont)) 'single)
1427 ((and (not pcont) cont) 'begin)
1428 ((and pcont (not cont)) 'end)
1429 ((and pcont cont) 'middle)
1430 (t (error "The impossible occurred")))))
1432 (defsubst f90-indent-line-no ()
1433 "If `f90-leave-line-no' is nil, left-justify a line number.
1434 Leaves point at the first non-blank character after the line number.
1435 Call from beginning of line."
1436 (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
1437 (delete-horizontal-space))
1438 (skip-chars-forward " \t0-9"))
1440 (defsubst f90-no-block-limit ()
1441 "Return nil if point is at the edge of a code block.
1442 Searches line forward for \"function\" or \"subroutine\",
1443 if all else fails."
1444 (save-excursion
1445 (not (or (looking-at "end")
1446 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
1447 \\|select[ \t]*\\(case\\|type\\)\\|case\\|where\\|forall\\|\
1448 block\\|critical\\)\\_>")
1449 (looking-at "\\(program\\|\\(?:sub\\)?module\\|\
1450 \\(?:abstract[ \t]*\\)?interface\\|block[ \t]*data\\)\\_>")
1451 (looking-at "\\(contains\\|\\(?:\\sw\\|\\s_\\)+[ \t]*:\\)")
1452 (looking-at f90-type-def-re)
1453 (re-search-forward "\\(function\\|subroutine\\)"
1454 (line-end-position) t)))))
1456 (defsubst f90-update-line ()
1457 "Change case of current line as per `f90-auto-keyword-case'."
1458 (if f90-auto-keyword-case
1459 (f90-change-keywords f90-auto-keyword-case
1460 (line-beginning-position) (line-end-position))))
1462 (defun f90-electric-insert (&optional arg)
1463 "Change keyword case and auto-fill line as operators are inserted."
1464 (interactive "*p")
1465 (self-insert-command arg)
1466 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1467 (f90-update-line)))
1469 ;; Behave like self-insert-command for delete-selection-mode (bug#5593).
1470 (put 'f90-electric-insert 'delete-selection t)
1472 (defun f90-get-correct-indent ()
1473 "Get correct indent for a line starting with line number.
1474 Does not check type and subprogram indentation."
1475 (let ((epnt (line-end-position)) icol)
1476 (save-excursion
1477 (while (and (f90-previous-statement)
1478 (or (memq (f90-present-statement-cont) '(middle end))
1479 (looking-at "[ \t]*[0-9]"))))
1480 (setq icol (current-indentation))
1481 (beginning-of-line)
1482 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1483 (line-end-position) t)
1484 (beginning-of-line)
1485 (skip-chars-forward " \t")
1486 (cond ((f90-looking-at-do)
1487 (setq icol (+ icol f90-do-indent)))
1488 ((or (f90-looking-at-if-then)
1489 (f90-looking-at-where-or-forall)
1490 (f90-looking-at-select-case))
1491 (setq icol (+ icol f90-if-indent)))
1492 ;; FIXME this makes no sense, because this section/function is
1493 ;; only for if/do/select/where/forall ?
1494 ((f90-looking-at-associate)
1495 (setq icol (+ icol f90-associate-indent))))
1496 (end-of-line))
1497 (while (re-search-forward
1498 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1499 (beginning-of-line)
1500 (skip-chars-forward " \t0-9")
1501 (cond ((f90-looking-at-do)
1502 (setq icol (+ icol f90-do-indent)))
1503 ((or (f90-looking-at-if-then)
1504 (f90-looking-at-where-or-forall)
1505 (f90-looking-at-select-case))
1506 (setq icol (+ icol f90-if-indent)))
1507 ;; FIXME this makes no sense, because this section/function is
1508 ;; only for if/do/select/where/forall ?
1509 ((f90-looking-at-associate)
1510 (setq icol (+ icol f90-associate-indent)))
1511 ((looking-at f90-end-if-re)
1512 (setq icol (- icol f90-if-indent)))
1513 ((looking-at f90-end-associate-re)
1514 (setq icol (- icol f90-associate-indent)))
1515 ((f90-looking-at-end-critical)
1516 (setq icol (- icol f90-critical-indent)))
1517 ((looking-at "end[ \t]*do\\_>")
1518 (setq icol (- icol f90-do-indent))))
1519 (end-of-line))
1520 icol)))
1522 (defun f90-calculate-indent ()
1523 "Calculate the indent column based on previous statements."
1524 (interactive)
1525 (let (icol cont (case-fold-search t) (pnt (point)))
1526 (save-excursion
1527 (if (not (f90-previous-statement))
1528 ;; If f90-previous-statement returns nil, we must have been
1529 ;; called from on or before the first line of the first statement.
1530 (setq icol (if (or (save-excursion
1531 (goto-char pnt)
1532 (beginning-of-line)
1533 ;; Preprocessor line before code statement.
1534 (looking-at "[ \t]*#"))
1535 (progn
1536 ;; f90-previous-statement has moved us over
1537 ;; comment/blank lines, so we need to get
1538 ;; back to the first code statement.
1539 (when (looking-at "[ \t]*\\([!#]\\|$\\)")
1540 (f90-next-statement))
1541 (skip-chars-forward " \t0-9")
1542 (f90-looking-at-program-block-start)))
1544 ;; No explicit PROGRAM start statement.
1545 f90-program-indent))
1546 (setq cont (f90-present-statement-cont))
1547 (if (eq cont 'end)
1548 (while (not (eq 'begin (f90-present-statement-cont)))
1549 (f90-previous-statement)))
1550 (cond ((eq cont 'begin)
1551 (setq icol (+ (f90-current-indentation)
1552 f90-continuation-indent)))
1553 ((eq cont 'middle) (setq icol (current-indentation)))
1554 (t (setq icol (f90-current-indentation))
1555 (skip-chars-forward " \t")
1556 (if (looking-at "[0-9]")
1557 (setq icol (f90-get-correct-indent))
1558 (cond ((or (f90-looking-at-if-then)
1559 (f90-looking-at-where-or-forall)
1560 (f90-looking-at-select-case)
1561 (looking-at f90-else-like-re))
1562 (setq icol (+ icol f90-if-indent)))
1563 ((f90-looking-at-do)
1564 (setq icol (+ icol f90-do-indent)))
1565 ((f90-looking-at-type-like)
1566 (setq icol (+ icol f90-type-indent)))
1567 ((f90-looking-at-associate)
1568 (setq icol (+ icol f90-associate-indent)))
1569 ((f90-looking-at-critical)
1570 (setq icol (+ icol f90-critical-indent)))
1571 ((or (f90-looking-at-program-block-start)
1572 (looking-at "contains[ \t]*\\($\\|!\\)"))
1573 (setq icol (+ icol f90-program-indent)))))
1574 (goto-char pnt)
1575 (beginning-of-line)
1576 (cond ((looking-at "[ \t]*$"))
1577 ((looking-at "[ \t]*#") ; check for cpp directive
1578 (setq icol 0))
1580 (skip-chars-forward " \t0-9")
1581 (cond ((or (looking-at f90-else-like-re)
1582 (looking-at f90-end-if-re))
1583 (setq icol (- icol f90-if-indent)))
1584 ((looking-at "end[ \t]*do\\_>")
1585 (setq icol (- icol f90-do-indent)))
1586 ((looking-at f90-end-type-re)
1587 (setq icol (- icol f90-type-indent)))
1588 ((looking-at f90-end-associate-re)
1589 (setq icol (- icol f90-associate-indent)))
1590 ((f90-looking-at-end-critical)
1591 (setq icol (- icol f90-critical-indent)))
1592 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1593 (f90-looking-at-program-block-end))
1594 (setq icol (- icol f90-program-indent))))))))))
1595 icol))
1597 (defun f90-previous-statement ()
1598 "Move point to beginning of the previous F90 statement.
1599 If no previous statement is found (i.e. if called from the first
1600 statement in the buffer), move to the start of the buffer and
1601 return nil. A statement is a line which is neither blank nor a
1602 comment."
1603 (interactive)
1604 (let (not-first-statement)
1605 (beginning-of-line)
1606 (while (and (setq not-first-statement (zerop (forward-line -1)))
1607 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1608 not-first-statement))
1610 (defun f90-next-statement ()
1611 "Move point to beginning of the next F90 statement.
1612 Return nil if no later statement is found."
1613 (interactive)
1614 (let (not-last-statement)
1615 (beginning-of-line)
1616 (while (and (setq not-last-statement
1617 (and (zerop (forward-line 1))
1618 (not (eobp))))
1619 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1620 not-last-statement))
1622 (defun f90-beginning-of-subprogram ()
1623 "Move point to the beginning of the current subprogram.
1624 Return (TYPE NAME), or nil if not found."
1625 (interactive)
1626 (let ((count 1) (case-fold-search t) matching-beg)
1627 (beginning-of-line)
1628 (while (and (> count 0)
1629 (re-search-backward f90-program-block-re nil 'move))
1630 (beginning-of-line)
1631 (skip-chars-forward " \t0-9")
1632 (cond ((setq matching-beg (f90-looking-at-program-block-start))
1633 (setq count (1- count)))
1634 ((f90-looking-at-program-block-end)
1635 (setq count (1+ count)))))
1636 (beginning-of-line)
1637 (if (zerop count)
1638 matching-beg
1639 ;; Note this includes the case of an un-named main program,
1640 ;; in which case we go to (point-min).
1641 (if (called-interactively-p 'interactive)
1642 (message "No beginning found"))
1643 nil)))
1645 (defun f90-end-of-subprogram ()
1646 "Move point to the end of the current subprogram.
1647 Return (TYPE NAME), or nil if not found."
1648 (interactive)
1649 (let ((case-fold-search t)
1650 (count 1)
1651 matching-end)
1652 (end-of-line)
1653 (while (and (> count 0)
1654 (re-search-forward f90-program-block-re nil 'move))
1655 (beginning-of-line)
1656 (skip-chars-forward " \t0-9")
1657 (cond ((f90-looking-at-program-block-start)
1658 (setq count (1+ count)))
1659 ((setq matching-end (f90-looking-at-program-block-end))
1660 (setq count (1- count))))
1661 (end-of-line))
1662 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1663 ;; has a net non-zero effect, which seems odd.
1664 ;;; (forward-line 1)
1665 (if (zerop count)
1666 matching-end
1667 (if (called-interactively-p 'interactive)
1668 (message "No end found"))
1669 nil)))
1672 (defun f90-end-of-block (&optional num)
1673 "Move point forward to the end of the current code block.
1674 With optional argument NUM, go forward that many balanced blocks.
1675 If NUM is negative, go backward to the start of a block. Checks
1676 for consistency of block types and labels (if present), and
1677 completes outermost block if `f90-smart-end' is non-nil.
1678 Interactively, pushes mark before moving point."
1679 (interactive "p")
1680 ;; Can move some distance.
1681 (if (called-interactively-p 'any) (push-mark (point) t))
1682 (and num (< num 0) (f90-beginning-of-block (- num)))
1683 (let ((f90-smart-end (if f90-smart-end 'no-blink)) ; for final match-end
1684 (case-fold-search t)
1685 (count (or num 1))
1686 start-list start-this start-type start-label end-type end-label)
1687 (end-of-line) ; probably want this
1688 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1689 (beginning-of-line)
1690 (skip-chars-forward " \t0-9")
1691 (cond ((or (f90-in-string) (f90-in-comment)))
1692 ((setq start-this
1694 (f90-looking-at-do)
1695 (f90-looking-at-select-case)
1696 (f90-looking-at-type-like)
1697 (f90-looking-at-associate)
1698 (f90-looking-at-critical)
1699 (f90-looking-at-program-block-start)
1700 (f90-looking-at-if-then)
1701 (f90-looking-at-where-or-forall)))
1702 (setq start-list (cons start-this start-list) ; not add-to-list!
1703 count (1+ count)))
1704 ((looking-at (concat "end[ \t]*" f90-blocks-re
1705 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
1706 (setq end-type (match-string 1)
1707 end-label (match-string 2)
1708 count (1- count))
1709 ;; Check any internal blocks.
1710 (when start-list
1711 (setq start-this (car start-list)
1712 start-list (cdr start-list)
1713 start-type (car start-this)
1714 start-label (cadr start-this))
1715 (or (f90-equal-symbols start-type end-type)
1716 (error "End type `%s' does not match start type `%s'"
1717 end-type start-type))
1718 (or (f90-equal-symbols start-label end-label)
1719 (error "End label `%s' does not match start label `%s'"
1720 end-label start-label)))))
1721 (end-of-line))
1722 (if (> count 0) (error "Missing block end"))
1723 ;; Check outermost block.
1724 (when f90-smart-end
1725 (save-excursion
1726 (beginning-of-line)
1727 (skip-chars-forward " \t0-9")
1728 (f90-match-end)))))
1730 (defun f90-beginning-of-block (&optional num)
1731 "Move point backwards to the start of the current code block.
1732 With optional argument NUM, go backward that many balanced blocks.
1733 If NUM is negative, go forward to the end of a block.
1734 Checks for consistency of block types and labels (if present).
1735 Does not check the outermost block, because it may be incomplete.
1736 Interactively, pushes mark before moving point."
1737 (interactive "p")
1738 (if (called-interactively-p 'any) (push-mark (point) t))
1739 (and num (< num 0) (f90-end-of-block (- num)))
1740 (let ((case-fold-search t)
1741 (count (or num 1))
1742 end-list end-this end-type end-label
1743 start-this start-type start-label)
1744 (beginning-of-line) ; probably want this
1745 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1746 (beginning-of-line)
1747 (skip-chars-forward " \t0-9")
1748 (cond ((or (f90-in-string) (f90-in-comment)))
1749 ((looking-at (concat "end[ \t]*" f90-blocks-re
1750 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
1751 (setq end-list (cons (list (match-string 1) (match-string 2))
1752 end-list)
1753 count (1+ count)))
1754 ((setq start-this
1756 (f90-looking-at-do)
1757 (f90-looking-at-select-case)
1758 (f90-looking-at-type-like)
1759 (f90-looking-at-associate)
1760 (f90-looking-at-critical)
1761 (f90-looking-at-program-block-start)
1762 (f90-looking-at-if-then)
1763 (f90-looking-at-where-or-forall)))
1764 (setq start-type (car start-this)
1765 start-label (cadr start-this)
1766 count (1- count))
1767 ;; Check any internal blocks.
1768 (when end-list
1769 (setq end-this (car end-list)
1770 end-list (cdr end-list)
1771 end-type (car end-this)
1772 end-label (cadr end-this))
1773 (or (f90-equal-symbols start-type end-type)
1774 (error "Start type `%s' does not match end type `%s'"
1775 start-type end-type))
1776 (or (f90-equal-symbols start-label end-label)
1777 (error "Start label `%s' does not match end label `%s'"
1778 start-label end-label))))))
1779 ;; Includes an un-named main program block.
1780 (if (> count 0) (error "Missing block start"))))
1782 (defun f90-next-block (&optional num)
1783 "Move point forward to the next end or start of a code block.
1784 With optional argument NUM, go forward that many blocks.
1785 If NUM is negative, go backwards.
1786 A block is a subroutine, if-endif, etc."
1787 (interactive "p")
1788 (let ((case-fold-search t)
1789 (count (if num (abs num) 1)))
1790 (while (and (> count 0)
1791 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1792 (re-search-backward f90-blocks-re nil 'move)))
1793 (beginning-of-line)
1794 (skip-chars-forward " \t0-9")
1795 (cond ((or (f90-in-string) (f90-in-comment)))
1796 ((or
1797 (looking-at "end[ \t]*")
1798 (f90-looking-at-do)
1799 (f90-looking-at-select-case)
1800 (f90-looking-at-type-like)
1801 (f90-looking-at-associate)
1802 (f90-looking-at-critical)
1803 (f90-looking-at-program-block-start)
1804 (f90-looking-at-if-then)
1805 (f90-looking-at-where-or-forall))
1806 (setq count (1- count))))
1807 (if (> num 0) (end-of-line)
1808 (beginning-of-line)))))
1811 (defun f90-previous-block (&optional num)
1812 "Move point backward to the previous end or start of a code block.
1813 With optional argument NUM, go backward that many blocks.
1814 If NUM is negative, go forwards.
1815 A block is a subroutine, if-endif, etc."
1816 (interactive "p")
1817 (f90-next-block (- (or num 1))))
1820 (defun f90-mark-subprogram ()
1821 "Put mark at end of F90 subprogram, point at beginning, push mark."
1822 (interactive)
1823 (let ((pos (point)) program)
1824 (f90-end-of-subprogram)
1825 (push-mark)
1826 (goto-char pos)
1827 (setq program (f90-beginning-of-subprogram))
1828 (if (featurep 'xemacs)
1829 (zmacs-activate-region)
1830 (setq mark-active t
1831 deactivate-mark nil))
1832 program))
1834 (defun f90-comment-region (beg-region end-region)
1835 "Comment/uncomment every line in the region.
1836 Insert the variable `f90-comment-region' at the start of every line
1837 in the region, or, if already present, remove it."
1838 (interactive "*r")
1839 (let ((end (copy-marker end-region)))
1840 (goto-char beg-region)
1841 (beginning-of-line)
1842 (if (looking-at (regexp-quote f90-comment-region))
1843 (delete-region (point) (match-end 0))
1844 (insert f90-comment-region))
1845 (while (and (zerop (forward-line 1))
1846 (< (point) end))
1847 (if (looking-at (regexp-quote f90-comment-region))
1848 (delete-region (point) (match-end 0))
1849 (insert f90-comment-region)))
1850 (set-marker end nil)))
1852 (defun f90-indent-line (&optional no-update)
1853 "Indent current line as F90 code.
1854 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1855 after indenting."
1856 (interactive "*P")
1857 (let ((case-fold-search t)
1858 (pos (point-marker))
1859 indent no-line-number)
1860 (beginning-of-line) ; digits after & \n are not line-nos
1861 (if (not (save-excursion (and (f90-previous-statement)
1862 (f90-line-continued))))
1863 (f90-indent-line-no)
1864 (setq no-line-number t)
1865 (skip-chars-forward " \t"))
1866 ;; FIXME This means f90-calculate-indent gives different answers
1867 ;; for comments and preprocessor lines to this function.
1868 ;; Better to make f90-calculate-indent return the correct answer?
1869 (cond ((looking-at "!") (setq indent (f90-comment-indent)))
1870 ((looking-at "#") (setq indent 0))
1872 (and f90-smart-end (looking-at "end")
1873 (f90-match-end))
1874 (setq indent (f90-calculate-indent))))
1875 (or (= indent (current-column))
1876 (f90-indent-to indent no-line-number))
1877 ;; If initial point was within line's indentation,
1878 ;; position after the indentation. Else stay at same point in text.
1879 (and (< (point) pos)
1880 (goto-char pos))
1881 (if auto-fill-function
1882 ;; GM NO-UPDATE not honored, since this calls f90-update-line.
1883 (f90-do-auto-fill)
1884 (or no-update (f90-update-line)))
1885 (set-marker pos nil)))
1887 (defun f90-indent-new-line ()
1888 "Re-indent current line, insert a newline and indent the newline.
1889 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1890 If run in the middle of a line, the line is not broken."
1891 (interactive "*")
1892 (if abbrev-mode (expand-abbrev))
1893 (beginning-of-line) ; reindent where likely to be needed
1894 (f90-indent-line) ; calls indent-line-no, update-line
1895 (end-of-line)
1896 (delete-horizontal-space) ; destroy trailing whitespace
1897 (let ((string (f90-in-string))
1898 (cont (f90-line-continued)))
1899 (and string (not cont) (insert "&"))
1900 (newline)
1901 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
1902 (f90-indent-line 'no-update)) ; nothing to update
1905 ;; TODO not add spaces to empty lines at the start.
1906 ;; Why is second line getting extra indent over first?
1907 (defun f90-indent-region (beg-region end-region)
1908 "Indent every line in region by forward parsing."
1909 (interactive "*r")
1910 (let ((end-region-mark (copy-marker end-region))
1911 (save-point (point-marker))
1912 (case-fold-search t)
1913 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
1914 (goto-char beg-region)
1915 ;; First find a line which is not a continuation line or comment.
1916 (beginning-of-line)
1917 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1918 (progn (f90-indent-line 'no-update)
1919 (zerop (forward-line 1)))
1920 (< (point) end-region-mark)))
1921 (setq cont (f90-present-statement-cont))
1922 (while (and (memq cont '(middle end))
1923 (f90-previous-statement))
1924 (setq cont (f90-present-statement-cont)))
1925 ;; Process present line for beginning of block.
1926 (setq f90-cache-position (point))
1927 (f90-indent-line 'no-update)
1928 (setq ind-lev (f90-current-indentation)
1929 ind-curr ind-lev)
1930 (beginning-of-line)
1931 (skip-chars-forward " \t0-9")
1932 (setq struct nil
1933 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1934 ((or (setq struct (f90-looking-at-if-then))
1935 (setq struct (f90-looking-at-select-case))
1936 (setq struct (f90-looking-at-where-or-forall))
1937 (looking-at f90-else-like-re))
1938 f90-if-indent)
1939 ((setq struct (f90-looking-at-type-like))
1940 f90-type-indent)
1941 ((setq struct (f90-looking-at-associate))
1942 f90-associate-indent)
1943 ((setq struct (f90-looking-at-critical))
1944 f90-critical-indent)
1945 ((or (setq struct (f90-looking-at-program-block-start))
1946 (looking-at "contains[ \t]*\\($\\|!\\)"))
1947 f90-program-indent)))
1948 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1949 (if struct (setq block-list (cons struct block-list)))
1950 (while (and (f90-line-continued) (zerop (forward-line 1))
1951 (< (point) end-region-mark))
1952 (if (looking-at "[ \t]*!")
1953 (f90-indent-to (f90-comment-indent))
1954 (or (= (current-indentation)
1955 (+ ind-curr f90-continuation-indent))
1956 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1957 ;; Process all following lines.
1958 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1959 (beginning-of-line)
1960 (f90-indent-line-no)
1961 (setq f90-cache-position (point))
1962 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1963 ((looking-at "[ \t]*#") (setq ind-curr 0))
1964 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1965 ((f90-no-block-limit) (setq ind-curr ind-lev))
1966 ((looking-at f90-else-like-re) (setq ind-curr
1967 (- ind-lev f90-if-indent)))
1968 ((looking-at "contains[ \t]*\\($\\|!\\)")
1969 (setq ind-curr (- ind-lev f90-program-indent)))
1970 ((setq ind-b
1971 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1972 ((or (setq struct (f90-looking-at-if-then))
1973 (setq struct (f90-looking-at-select-case))
1974 (setq struct (f90-looking-at-where-or-forall)))
1975 f90-if-indent)
1976 ((setq struct (f90-looking-at-type-like))
1977 f90-type-indent)
1978 ((setq struct (f90-looking-at-associate))
1979 f90-associate-indent)
1980 ((setq struct (f90-looking-at-critical))
1981 f90-critical-indent)
1982 ((setq struct (f90-looking-at-program-block-start))
1983 f90-program-indent)))
1984 (setq ind-curr ind-lev)
1985 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1986 (setq block-list (cons struct block-list)))
1987 ((setq end-struct (f90-looking-at-program-block-end))
1988 (setq beg-struct (car block-list)
1989 block-list (cdr block-list))
1990 (if f90-smart-end
1991 (save-excursion
1992 (f90-block-match (car beg-struct) (cadr beg-struct)
1993 (car end-struct) (cadr end-struct))))
1994 (setq ind-b
1995 (cond ((looking-at f90-end-if-re) f90-if-indent)
1996 ((looking-at "end[ \t]*do\\_>") f90-do-indent)
1997 ((looking-at f90-end-type-re) f90-type-indent)
1998 ((looking-at f90-end-associate-re)
1999 f90-associate-indent)
2000 ((f90-looking-at-end-critical) f90-critical-indent)
2001 ((f90-looking-at-program-block-end)
2002 f90-program-indent)))
2003 (if ind-b (setq ind-lev (- ind-lev ind-b)))
2004 (setq ind-curr ind-lev))
2005 (t (setq ind-curr ind-lev)))
2006 ;; Do the indentation if necessary.
2007 (or (= ind-curr (current-column))
2008 (f90-indent-to ind-curr))
2009 (while (and (f90-line-continued) (zerop (forward-line 1))
2010 (< (point) end-region-mark))
2011 (cond ((looking-at "[ \t]*#") (f90-indent-to 0))
2012 ((looking-at "[ \t]*!") (f90-indent-to (f90-comment-indent)))
2014 (or (= (current-indentation)
2015 (+ ind-curr f90-continuation-indent))
2016 (f90-indent-to
2017 (+ ind-curr f90-continuation-indent) 'no-line-no))))))
2018 ;; Restore point, etc.
2019 (setq f90-cache-position nil)
2020 (goto-char save-point)
2021 (set-marker end-region-mark nil)
2022 (set-marker save-point nil)
2023 (if (featurep 'xemacs)
2024 (zmacs-deactivate-region)
2025 (deactivate-mark))))
2027 (defun f90-indent-subprogram ()
2028 "Properly indent the subprogram containing point."
2029 (interactive "*")
2030 (save-excursion
2031 (let ((program (f90-mark-subprogram)))
2032 (if program
2033 (progn
2034 (message "Indenting %s %s..."
2035 (car program) (cadr program))
2036 (indent-region (point) (mark) nil)
2037 (message "Indenting %s %s...done"
2038 (car program) (cadr program)))
2039 (message "Indenting the whole file...")
2040 (indent-region (point) (mark) nil)
2041 (message "Indenting the whole file...done")))))
2043 (defun f90-break-line (&optional no-update)
2044 "Break line at point, insert continuation marker(s) and indent.
2045 Unless in a string or comment, or if the optional argument NO-UPDATE
2046 is non-nil, call `f90-update-line' after inserting the continuation marker."
2047 (interactive "*P")
2048 (cond ((f90-in-string)
2049 (insert "&\n&"))
2050 ((f90-in-comment)
2051 (delete-horizontal-space) ; remove trailing whitespace
2052 (insert "\n" (f90-get-present-comment-type)))
2053 (t (insert "&")
2054 (or no-update (f90-update-line))
2055 (newline 1)
2056 ;; FIXME also need leading ampersand if split lexical token (eg ==).
2057 ;; Or respect f90-no-break-re.
2058 (if f90-beginning-ampersand (insert "&"))))
2059 (indent-according-to-mode))
2061 (defun f90-find-breakpoint ()
2062 "From `fill-column', search backward for break-delimiter."
2063 ;; Commented text more likely than commented code.
2064 (if (f90-in-comment)
2065 (re-search-backward "\\s-" (line-beginning-position))
2066 (re-search-backward f90-break-delimiters (line-beginning-position))
2067 (if (not f90-break-before-delimiters)
2068 (forward-char (if (looking-at f90-no-break-re) 2 1))
2069 (backward-char)
2070 (or (looking-at f90-no-break-re)
2071 (forward-char)))))
2073 (defun f90-do-auto-fill ()
2074 "Break line if non-white characters beyond `fill-column'.
2075 Update keyword case first."
2076 (interactive "*")
2077 ;; Break line before or after last delimiter (non-word char) if
2078 ;; position is beyond fill-column.
2079 ;; Will not break **, //, or => (as specified by f90-no-break-re).
2080 (f90-update-line)
2081 ;; Need this for `f90-electric-insert' and other f90- callers.
2082 (unless (and (boundp 'comment-auto-fill-only-comments)
2083 comment-auto-fill-only-comments
2084 (not (f90-in-comment)))
2085 (while (> (current-column) fill-column)
2086 (let ((pos-mark (point-marker)))
2087 (move-to-column fill-column)
2088 (or (f90-in-string) (f90-find-breakpoint))
2089 (f90-break-line)
2090 (goto-char pos-mark)
2091 (set-marker pos-mark nil)))))
2093 (defun f90-join-lines (&optional arg)
2094 "Join current line to previous, fix whitespace, continuation, comments.
2095 With optional argument ARG, join current line to following line.
2096 Like `join-line', but handles F90 syntax."
2097 (interactive "*P")
2098 (beginning-of-line)
2099 (if arg (forward-line 1))
2100 (when (eq (preceding-char) ?\n)
2101 (skip-chars-forward " \t")
2102 (if (looking-at "\&") (delete-char 1))
2103 (beginning-of-line)
2104 (delete-region (point) (1- (point)))
2105 (skip-chars-backward " \t")
2106 (and (eq (preceding-char) ?&) (delete-char -1))
2107 (and (f90-in-comment)
2108 (looking-at "[ \t]*!+")
2109 (replace-match ""))
2110 (or (f90-in-string)
2111 (fixup-whitespace))))
2113 (defun f90-fill-region (beg-region end-region)
2114 "Fill every line in region by forward parsing. Join lines if possible."
2115 (interactive "*r")
2116 (let ((end-region-mark (copy-marker end-region))
2117 (go-on t)
2118 f90-smart-end f90-auto-keyword-case auto-fill-function)
2119 (goto-char beg-region)
2120 (while go-on
2121 ;; Join as much as possible.
2122 (while (progn
2123 (end-of-line)
2124 (skip-chars-backward " \t")
2125 (eq (preceding-char) ?&))
2126 (f90-join-lines 'forward))
2127 ;; Chop the line if necessary.
2128 (while (> (save-excursion (end-of-line) (current-column))
2129 fill-column)
2130 (move-to-column fill-column)
2131 (f90-find-breakpoint)
2132 (f90-break-line 'no-update))
2133 (setq go-on (and (< (point) end-region-mark)
2134 (zerop (forward-line 1)))
2135 f90-cache-position (point)))
2136 (setq f90-cache-position nil)
2137 (set-marker end-region-mark nil)
2138 (if (featurep 'xemacs)
2139 (zmacs-deactivate-region)
2140 (deactivate-mark))))
2142 (defconst f90-end-block-optional-name
2143 '("program" "module" "subroutine" "function" "type")
2144 "Block types where including the name in the end statement is optional.")
2146 (defun f90-block-match (beg-block beg-name end-block end-name)
2147 "Match end-struct with beg-struct and complete end-block if possible.
2148 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
2149 BEG-NAME is the block start name (may be nil).
2150 END-BLOCK is the type of block as indicated at the end (may be nil).
2151 END-NAME is the block end name (may be nil).
2152 If the block type matches `f90-end-block-optional-name', do not add
2153 an end name if `f90-smart-end-names' is nil, but always update an
2154 incorrect end name if there already was one.
2155 Leave point at the end of line."
2156 ;; Hack to deal with the case when this is called from
2157 ;; f90-indent-region on a program block without an explicit PROGRAM
2158 ;; statement at the start. Should really be an error (?).
2159 (or beg-block (setq beg-block "program"))
2160 (search-forward "end" (line-end-position))
2161 (catch 'no-match
2162 (if (and end-block (f90-equal-symbols beg-block end-block))
2163 (search-forward end-block)
2164 (if end-block
2165 (progn
2166 (message "END %s does not match %s." end-block beg-block)
2167 (end-of-line)
2168 (throw 'no-match nil))
2169 (message "Inserting %s." beg-block)
2170 (insert (concat " " beg-block))))
2171 (if (f90-equal-symbols beg-name end-name)
2172 (and end-name (search-forward end-name))
2173 (cond ((and beg-name (not end-name))
2174 (unless (and (not f90-smart-end-names)
2175 (member-ignore-case beg-block
2176 f90-end-block-optional-name))
2177 (message "Inserting %s." beg-name)
2178 (insert (concat " " beg-name))))
2179 ((and beg-name end-name)
2180 (message "Replacing %s with %s." end-name beg-name)
2181 (search-forward end-name)
2182 (replace-match beg-name))
2183 ((and (not beg-name) end-name)
2184 (message "Deleting %s." end-name)
2185 (search-forward end-name)
2186 (replace-match ""))))
2187 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
2189 (defun f90-match-end ()
2190 "From an end block statement, find the corresponding block and name."
2191 (interactive)
2192 (let ((count 1)
2193 (top-of-window (window-start))
2194 (end-point (point))
2195 (case-fold-search t)
2196 matching-beg beg-name end-name beg-block end-block end-struct)
2197 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
2198 (setq end-struct (f90-looking-at-program-block-end)))
2199 (setq end-block (car end-struct)
2200 end-name (cadr end-struct))
2201 (save-excursion
2202 (beginning-of-line)
2203 (while (and (> count 0)
2204 (not (= (line-beginning-position) (point-min))))
2205 (re-search-backward f90-blocks-re nil 'move)
2206 (beginning-of-line)
2207 ;; GM not a line number if continued line.
2208 ;;; (skip-chars-forward " \t")
2209 ;;; (skip-chars-forward "0-9")
2210 (skip-chars-forward " \t0-9")
2211 (cond ((or (f90-in-string) (f90-in-comment)))
2212 ((setq matching-beg
2214 (f90-looking-at-do)
2215 (f90-looking-at-if-then)
2216 (f90-looking-at-where-or-forall)
2217 (f90-looking-at-select-case)
2218 (f90-looking-at-type-like)
2219 (f90-looking-at-associate)
2220 (f90-looking-at-critical)
2221 (f90-looking-at-program-block-start)
2222 ;; Interpret a single END without a block
2223 ;; start to be the END of a program block
2224 ;; without an initial PROGRAM line.
2225 (if (= (line-beginning-position) (point-min))
2226 '("program" nil))))
2227 (setq count (1- count)))
2228 ((looking-at (concat "end[ \t]*" f90-blocks-re))
2229 (setq count (1+ count)))))
2230 (if (> count 0)
2231 (message "No matching beginning.")
2232 (f90-update-line)
2233 (if (eq f90-smart-end 'blink)
2234 (if (< (point) top-of-window)
2235 (message "Matches %s: %s"
2236 (what-line)
2237 (buffer-substring
2238 (line-beginning-position)
2239 (line-end-position)))
2240 (sit-for blink-matching-delay)))
2241 (setq beg-block (car matching-beg)
2242 beg-name (cadr matching-beg))
2243 (goto-char end-point)
2244 (beginning-of-line)
2245 (f90-block-match beg-block beg-name end-block end-name))))))
2247 (defun f90-insert-end ()
2248 "Insert a complete end statement matching beginning of present block."
2249 (interactive "*")
2250 (let ((f90-smart-end (or f90-smart-end 'blink)))
2251 (insert "end")
2252 (f90-indent-new-line)))
2254 ;; Abbrevs and keywords.
2256 (defun f90-abbrev-start ()
2257 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
2258 Any other key combination is executed normally."
2259 (interactive "*")
2260 (self-insert-command 1)
2261 (when abbrev-mode
2262 (set-transient-map
2263 (let ((map (make-sparse-keymap)))
2264 (define-key map [??] 'f90-abbrev-help)
2265 (define-key map (vector help-char) 'f90-abbrev-help)
2266 map))))
2268 (defun f90-abbrev-help ()
2269 "List the currently defined abbrevs in F90 mode."
2270 (interactive)
2271 (message "Listing abbrev table...")
2272 (display-buffer (f90-prepare-abbrev-list-buffer))
2273 (message "Listing abbrev table...done"))
2275 (defun f90-prepare-abbrev-list-buffer ()
2276 "Create a buffer listing the F90 mode abbreviations."
2277 (with-current-buffer (get-buffer-create "*Abbrevs*")
2278 (erase-buffer)
2279 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
2280 (goto-char (point-min))
2281 (set-buffer-modified-p nil)
2282 (edit-abbrevs-mode))
2283 (get-buffer-create "*Abbrevs*"))
2285 (defun f90-upcase-keywords ()
2286 "Upcase all F90 keywords in the buffer."
2287 (interactive "*")
2288 (f90-change-keywords 'upcase-word))
2290 (defun f90-capitalize-keywords ()
2291 "Capitalize all F90 keywords in the buffer."
2292 (interactive "*")
2293 (f90-change-keywords 'capitalize-word))
2295 (defun f90-downcase-keywords ()
2296 "Downcase all F90 keywords in the buffer."
2297 (interactive "*")
2298 (f90-change-keywords 'downcase-word))
2300 (defun f90-upcase-region-keywords (beg end)
2301 "Upcase all F90 keywords in the region."
2302 (interactive "*r")
2303 (f90-change-keywords 'upcase-word beg end))
2305 (defun f90-capitalize-region-keywords (beg end)
2306 "Capitalize all F90 keywords in the region."
2307 (interactive "*r")
2308 (f90-change-keywords 'capitalize-word beg end))
2310 (defun f90-downcase-region-keywords (beg end)
2311 "Downcase all F90 keywords in the region."
2312 (interactive "*r")
2313 (f90-change-keywords 'downcase-word beg end))
2315 ;; Change the keywords according to argument.
2316 (defun f90-change-keywords (change-word &optional beg end)
2317 "Change the case of F90 keywords in the region (if specified) or buffer.
2318 CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
2319 (save-excursion
2320 (setq beg (or beg (point-min))
2321 end (or end (point-max)))
2322 (let ((keyword-re
2323 (concat "\\("
2324 f90-keywords-re "\\|" f90-procedures-re "\\|"
2325 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
2326 (ref-point (point-min))
2327 (modified (buffer-modified-p))
2328 state saveword back-point)
2329 (goto-char beg)
2330 (unwind-protect
2331 (while (re-search-forward keyword-re end t)
2332 (unless (progn
2333 (setq state (parse-partial-sexp ref-point (point)))
2334 (or (nth 3 state) (nth 4 state)
2335 ;; GM f90-directive-comment-re?
2336 (save-excursion ; check for cpp directive
2337 (beginning-of-line)
2338 (skip-chars-forward " \t0-9")
2339 (looking-at "#"))))
2340 (setq ref-point (point)
2341 back-point (save-excursion (backward-word 1) (point))
2342 saveword (buffer-substring back-point ref-point))
2343 (funcall change-word -1)
2344 (or (string= saveword (buffer-substring back-point ref-point))
2345 (setq modified t))))
2346 (or modified (restore-buffer-modified-p nil))))))
2349 (defun f90-current-defun ()
2350 "Function to use for `add-log-current-defun-function' in F90 mode."
2351 (save-excursion
2352 (nth 1 (f90-beginning-of-subprogram))))
2354 (defun f90-backslash-not-special (&optional all)
2355 "Make the backslash character (\\) be non-special in the current buffer.
2356 With optional argument ALL, change the default for all present
2357 and future F90 buffers. F90 mode normally treats backslash as an
2358 escape character."
2359 (or (derived-mode-p 'f90-mode)
2360 (error "This function should only be used in F90 buffers"))
2361 (when (equal (char-syntax ?\\ ) ?\\ )
2362 (or all (set-syntax-table (copy-syntax-table (syntax-table))))
2363 (modify-syntax-entry ?\\ ".")))
2366 (provide 'f90)
2368 ;; Local Variables:
2369 ;; coding: utf-8
2370 ;; End:
2372 ;;; f90.el ends here