Remove some function declarations, no longer needed or correct
[bpt/emacs.git] / lisp / progmodes / f90.el
CommitLineData
48da7392 1;;; f90.el --- Fortran-90 mode (free format) -*- lexical-binding: t -*-
b578f267 2
ba318903 3;; Copyright (C) 1995-1997, 2000-2014 Free Software Foundation, Inc.
034a9d40 4
239da61d 5;; Author: Torbjörn Einarsson <Torbjorn.Einarsson@era.ericsson.se>
9877fcf1 6;; Maintainer: Glenn Morris <rgm@gnu.org>
034a9d40
RS
7;; Keywords: fortran, f90, languages
8
b578f267
EN
9;; This file is part of GNU Emacs.
10
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
034a9d40 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
034a9d40 15
b578f267 16;; GNU Emacs is distributed in the hope that it will be useful,
034a9d40
RS
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.
20
21;; You should have received a copy of the GNU General Public License
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
034a9d40
RS
23
24;;; Commentary:
b578f267 25
87ee2359
GM
26;; Major mode for editing F90 programs in FREE FORMAT.
27;; The minor language revision F95 is also supported (with font-locking).
b1ca7740 28;; Some/many (?) aspects of F2003 are supported.
5d8e0d43 29;; Some aspects of F2008 are supported.
87ee2359 30
034a9d40 31;; Knows about continuation lines, named structured statements, and other
87ee2359
GM
32;; features in F90 including HPF (High Performance Fortran) structures.
33;; The basic feature provides accurate indentation of F90 programs.
034a9d40
RS
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
87ee2359
GM
36;; function which joins continued lines, etc.
37
38;; To facilitate typing, a fairly complete list of abbreviations is provided.
39;; All abbreviations begin with the backquote character "`"
87ee2359 40;; For example, `i expands to integer (if abbrev-mode is on).
034a9d40 41
87ee2359 42;; There are two separate features for altering the appearance of code:
034a9d40 43;; 1) Upcasing or capitalizing of all keywords.
87ee2359
GM
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.
034a9d40
RS
47
48;; The indentations of lines starting with ! is determined by the first of the
87ee2359 49;; following matches (values in the left column are the defaults):
ee30478d
KH
50
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
57
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
87ee2359
GM
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.
034a9d40 66
a729409a 67;; One common convention for free vs. fixed format is that free format files
69658465 68;; have the ending .f90 or .f95 while fixed format files have the ending .f.
87ee2359
GM
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))
73
034a9d40 74;; Once you have entered f90-mode, you may get more info by using
69658465 75;; the command describe-mode (C-h m). For online help use
87ee2359
GM
76;; C-h f <Name of function you want described>, or
77;; C-h v <Name of variable you want described>.
034a9d40 78
87ee2359
GM
79;; To customize f90-mode for your taste, use, for example:
80;; (you don't have to specify values for all the parameters below)
81;;
4f91a816
SM
82;; (add-hook 'f90-mode-hook
83;; ;; These are the default values.
84;; (lambda () (setq f90-do-indent 3
034a9d40
RS
85;; f90-if-indent 3
86;; f90-type-indent 3
87;; f90-program-indent 2
88;; f90-continuation-indent 5
89;; f90-comment-region "!!$"
ee30478d
KH
90;; f90-directive-comment-re "!hpf\\$"
91;; f90-indented-comment-re "!"
87ee2359 92;; f90-break-delimiters "[-+\\*/><=,% \t]"
034a9d40
RS
93;; f90-break-before-delimiters t
94;; f90-beginning-ampersand t
95;; f90-smart-end 'blink
96;; f90-auto-keyword-case nil
87ee2359 97;; f90-leave-line-no nil
72e80cad 98;; indent-tabs-mode nil
b974df0a 99;; f90-font-lock-keywords f90-font-lock-keywords-2
72e80cad 100;; )
87ee2359 101;; ;; These are not default.
034a9d40 102;; (abbrev-mode 1) ; turn on abbreviation mode
b974df0a 103;; (f90-add-imenu-menu) ; extra menu with functions etc.
034a9d40 104;; (if f90-auto-keyword-case ; change case of all keywords on startup
865fe16f 105;; (f90-change-keywords f90-auto-keyword-case))))
87ee2359 106;;
865fe16f 107;; in your init file. You can also customize the lists
87ee2359
GM
108;; f90-font-lock-keywords, etc.
109;;
110;; The auto-fill and abbreviation minor modes are accessible from the F90 menu,
b974df0a 111;; or by using M-x auto-fill-mode and M-x abbrev-mode, respectively.
034a9d40
RS
112
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.
87ee2359 116;; 2) Multi-; statements like "do i=1,20 ; j=j+i ; end do" are not handled
034a9d40 117;; correctly, but I imagine them to be rare.
ee30478d 118;; 3) Regexps for hilit19 are no longer supported.
87ee2359 119;; 4) For FIXED FORMAT code, use fortran mode.
034a9d40 120;; 5) This mode does not work under emacs-18.x.
72e80cad
KH
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 \ ).
ee30478d
KH
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).
c80718cc 126;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
034a9d40
RS
127
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
034a9d40
RS
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
b974df0a
EN
141;; f90-add-imenu-menu
142;; f90-font-lock-1, f90-font-lock-2, f90-font-lock-3, f90-font-lock-4
034a9d40 143
87ee2359 144;; Original author's thanks
034a9d40
RS
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.
150
a729409a
GM
151;;; Code:
152
87ee2359 153;; TODO
b1ca7740
GM
154;; 1. Any missing F2003 syntax?
155;; 2. Have "f90-mode" just recognize F90 syntax, then derived modes
5ab33946 156;; "f95-mode", "f2003-mode" for the language revisions.
b1ca7740
GM
157;; 3. Support for align.
158;; Font-locking:
159;; 1. OpenMP, OpenMPI?, preprocessor highlighting.
50e4518b
GM
160;; 2. integer_name = 1
161;; 3. Labels for "else" statements (F2003)?
87ee2359 162
34ba7e3d
GM
163(defvar comment-auto-fill-only-comments)
164(defvar font-lock-keywords)
0ee7f068 165
034a9d40 166;; User options
034a9d40 167
fcad5199 168(defgroup f90 nil
a729409a 169 "Major mode for editing free format Fortran 90,95 code."
8ec3bce0 170 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
d2d15846 171 :group 'languages)
034a9d40 172
fcad5199 173(defgroup f90-indent nil
a729409a 174 "Indentation in free format Fortran."
fcad5199 175 :prefix "f90-"
a729409a 176 :group 'f90)
034a9d40 177
034a9d40 178
fcad5199 179(defcustom f90-do-indent 3
d4654cff 180 "Extra indentation applied to DO blocks."
a729409a 181 :type 'integer
f38184aa 182 :safe 'integerp
fcad5199 183 :group 'f90-indent)
034a9d40 184
fcad5199 185(defcustom f90-if-indent 3
d4654cff 186 "Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
a729409a 187 :type 'integer
f38184aa 188 :safe 'integerp
fcad5199 189 :group 'f90-indent)
034a9d40 190
fcad5199 191(defcustom f90-type-indent 3
5ab33946 192 "Extra indentation applied to TYPE, ENUM, INTERFACE and BLOCK DATA blocks."
a729409a 193 :type 'integer
f38184aa 194 :safe 'integerp
fcad5199 195 :group 'f90-indent)
034a9d40 196
fcad5199 197(defcustom f90-program-indent 2
d4654cff 198 "Extra indentation applied to PROGRAM, MODULE, SUBROUTINE, FUNCTION blocks."
a729409a 199 :type 'integer
f38184aa 200 :safe 'integerp
fcad5199 201 :group 'f90-indent)
034a9d40 202
5ab33946
GM
203(defcustom f90-associate-indent 2
204 "Extra indentation applied to ASSOCIATE blocks."
205 :type 'integer
f38184aa 206 :safe 'integerp
5ab33946
GM
207 :group 'f90-indent
208 :version "23.1")
209
e93db24a
GM
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")
216
fcad5199 217(defcustom f90-continuation-indent 5
d4654cff 218 "Extra indentation applied to continuation lines."
a729409a 219 :type 'integer
f38184aa 220 :safe 'integerp
fcad5199 221 :group 'f90-indent)
034a9d40 222
fcad5199 223(defcustom f90-comment-region "!!$"
d4654cff 224 "String inserted by \\[f90-comment-region] at start of each line in region."
a729409a 225 :type 'string
f38184aa 226 :safe 'stringp
fcad5199
RS
227 :group 'f90-indent)
228
229(defcustom f90-indented-comment-re "!"
d4654cff 230 "Regexp matching comments to indent as code."
a729409a 231 :type 'regexp
f38184aa 232 :safe 'stringp
fcad5199
RS
233 :group 'f90-indent)
234
9e29ca4f 235;; Should we add ^# to this? That's not really a comment.
fcad5199 236(defcustom f90-directive-comment-re "!hpf\\$"
d4654cff 237 "Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
a729409a 238 :type 'regexp
f38184aa 239 :safe 'stringp
fcad5199
RS
240 :group 'f90-indent)
241
242(defcustom f90-beginning-ampersand t
d4654cff 243 "Non-nil gives automatic insertion of \& at start of continuation line."
a729409a 244 :type 'boolean
f38184aa 245 :safe 'booleanp
fcad5199
RS
246 :group 'f90)
247
248(defcustom f90-smart-end 'blink
d4654cff 249 "Qualification of END statements according to the matching block start.
7cc8ae06
GM
250For example, change the END that closes an IF block to END IF.
251If the block has a label, add it as well (unless `f90-smart-end-names'
5ac2eb34
SM
252says not to). Allowed values are `blink', `no-blink', and nil. If nil,
253nothing is done. The other two settings have the same effect, but `blink'
784d007b 254additionally blinks the cursor to the start of the block."
a729409a 255 :type '(choice (const blink) (const no-blink) (const nil))
f38184aa 256 :safe (lambda (value) (memq value '(blink no-blink nil)))
fcad5199 257 :group 'f90)
034a9d40 258
7cc8ae06
GM
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.
265For example, when closing a \"PROGRAM PROGNAME\" block, \"PROGNAME\" is
266optional in the \"END PROGRAM\" statement. The same is true for modules,
267functions, subroutines, and types. Some people prefer to omit the name
268from the END statement, since it makes it easier to change the name.
269
270This does not apply to named DO, IF, etc. blocks. If such blocks
271start with a label, they must end with one.
272
273If an end statement has a name that does not match the start, it is always
274corrected, regardless of the value of this variable."
275 :type 'boolean
276 :safe 'booleanp
277 :group 'f90
278 :version "24.4")
279
fcad5199 280(defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
d4654cff 281 "Regexp matching delimiter characters at which lines may be broken.
3f150a60
GM
282There are some common two-character tokens where one or more of
283the members matches this regexp. Although Fortran allows breaks
284within lexical tokens (provided the next line has a beginning ampersand),
285the constant `f90-no-break-re' ensures that such tokens are not split."
f38184aa
GM
286 :type 'regexp
287 :safe 'stringp
fcad5199 288 :group 'f90)
034a9d40 289
fcad5199 290(defcustom f90-break-before-delimiters t
d4654cff 291 "Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
f38184aa
GM
292 :type 'boolean
293 :safe 'booleanp
fcad5199 294 :group 'f90)
034a9d40 295
fcad5199 296(defcustom f90-auto-keyword-case nil
d4654cff 297 "Automatic case conversion of keywords.
87ee2359 298The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil."
a729409a
GM
299 :type '(choice (const downcase-word) (const upcase-word)
300 (const capitalize-word) (const nil))
f38184aa
GM
301 :safe (lambda (value) (memq value '(downcase-word
302 capitalize-word upcase-word nil)))
fcad5199
RS
303 :group 'f90)
304
305(defcustom f90-leave-line-no nil
d4654cff 306 "If non-nil, line numbers are not left justified."
a729409a 307 :type 'boolean
f38184aa 308 :safe 'booleanp
fcad5199
RS
309 :group 'f90)
310
0ee7f068
GM
311(defcustom f90-mode-hook nil
312 "Hook run when entering F90 mode."
a729409a 313 :type 'hook
f38184aa
GM
314 ;; Not the only safe options, but some common ones.
315 :safe (lambda (value) (member value '((f90-add-imenu-menu) nil)))
0ee7f068 316 :options '(f90-add-imenu-menu)
a729409a 317 :group 'f90)
0ee7f068
GM
318
319;; User options end here.
1bb3ae5c 320
ee30478d 321(defconst f90-keywords-re
f678b18a
SM
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"
347 ))
348 "\\_>")
9877fcf1 349 "Regexp used by the function `f90-change-keywords'.")
ee30478d
KH
350
351(defconst f90-keywords-level-3-re
f678b18a
SM
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"
371 ))
372 "\\_>")
84021009 373 "Keyword-regexp for font-lock level >= 3.")
ee30478d 374
ee30478d 375(defconst f90-procedures-re
1d5963cc 376 (concat "\\_<"
69658465
GM
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.
5ab33946
GM
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"
89826559
GM
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"
1a5db1f4 417 "norm2" "parity" "findloc" "is_contiguous"
d4f259f7
GM
418 "sync images" "lock" "unlock" "image_index"
419 "lcobound" "ucobound" "num_images" "this_image"
89826559
GM
420 ;; F2008 iso_fortran_env module.
421 "compiler_options" "compiler_version"
422 ;; F2008 iso_c_binding module.
423 "c_sizeof"
5ab33946 424 ) t)
69658465
GM
425 ;; A left parenthesis to avoid highlighting non-procedures.
426 "[ \t]*(")
ee30478d
KH
427 "Regexp whose first part matches F90 intrinsic procedures.")
428
429(defconst f90-operators-re
69658465
GM
430 (concat "\\."
431 (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
432 "neqv" "not" "or" "true") t)
433 "\\.")
ee30478d
KH
434 "Regexp matching intrinsic operators.")
435
436(defconst f90-hpf-keywords-re
f678b18a
SM
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 "\\_>")
ee30478d 460 "Regexp for all HPF keywords, procedures and directives.")
034a9d40 461
5ab33946 462(defconst f90-constants-re
f678b18a
SM
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"
496 ))
497 "\\_>")
5ab33946 498 "Regexp for Fortran intrinsic constants.")
034a9d40 499
b1ca7740
GM
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.
503Set the match data so that subexpression 1,2 are the TYPE, and
504type-name parts, respectively."
505 (let (found l)
1d5963cc 506 (while (and (re-search-forward "\\_<\\(\\(?:end[ \t]*\\)?type\\)\\_>[ \t]*"
b1ca7740
GM
507 limit t)
508 (not (setq found
509 (progn
510 (setq l (match-data))
1d5963cc
SM
511 (unless (looking-at "\\(is\\_>\\|(\\)")
512 (when (if (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
b1ca7740
GM
513 (goto-char (match-end 0))
514 (re-search-forward
1d5963cc 515 "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
b1ca7740
GM
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))
523
ee30478d 524(defvar f90-font-lock-keywords-1
45d1e4d4 525 (list
ec2f376f 526 ;; Special highlighting of "module procedure".
1d5963cc 527 '("\\_<\\(module[ \t]*procedure\\)\\_>\\([^()\n]*::\\)?[ \t]*\\([^&!\n]*\\)"
b1ca7740 528 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
eb9f0295 529 ;; Highlight definition of derived type.
1d5963cc 530;;; '("\\_<\\(\\(?:end[ \t]*\\)?type\\)\\_>\\([^()\n]*::\\)?[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
b1ca7740
GM
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))
5d8e0d43 534 ;; F2003. Prevent operators being highlighted as functions.
1d5963cc 535 '("\\_<\\(\\(?:end[ \t]*\\)?interface[ \t]*\\(?:assignment\\|operator\\|\
50e4518b
GM
536read\\|write\\)\\)[ \t]*(" (1 font-lock-keyword-face t))
537 ;; Other functions and declarations. Named interfaces = F2003.
5d8e0d43 538 ;; F2008: end submodule submodule_name.
1d5963cc 539 '("\\_<\\(\\(?:end[ \t]*\\)?\\(program\\|\\(?:sub\\)?module\\|\
5d8e0d43 540function\\|associate\\|subroutine\\|interface\\)\\|use\\|call\\)\
1d5963cc 541\\_>[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
84021009 542 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
5d8e0d43 543 ;; F2008: submodule (parent_name) submodule_name.
1d5963cc 544 '("\\_<\\(submodule\\)\\_>[ \t]*([^)\n]+)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
5d8e0d43 545 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
b1ca7740 546 ;; F2003.
1d5963cc
SM
547 '("\\_<\\(use\\)[ \t]*,[ \t]*\\(\\(?:non_\\)?intrinsic\\)[ \t]*::[ \t]*\
548\\(\\(?:\\sw\\|\\s_\\)+\\)"
b1ca7740
GM
549 (1 font-lock-keyword-face) (2 font-lock-keyword-face)
550 (3 font-lock-function-name-face))
1d5963cc 551 "\\_<\\(\\(end[ \t]*\\)?block[ \t]*data\\|contains\\)\\_>"
50e4518b 552 ;; "abstract interface" is F2003.
1d5963cc 553 '("\\_<abstract[ \t]*interface\\_>" (0 font-lock-keyword-face t)))
ee30478d
KH
554 "This does fairly subdued highlighting of comments and function calls.")
555
b1ca7740
GM
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.
560Set the match data so that subexpression 1,2 are the TYPE(...),
561and 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)
1d5963cc 565 (while (and (re-search-forward "\\_<\\(type\\|class\\)[ \t]*(" limit t)
b1ca7740
GM
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
1d5963cc 580 (if (looking-at "\\(?:\\sw\\|\\s_\\)+")
b1ca7740
GM
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))))
589 t))
590 (error nil))))))
591 found))
592
ee30478d 593(defvar f90-font-lock-keywords-2
69658465
GM
594 (append
595 f90-font-lock-keywords-1
596 (list
ec2f376f 597 ;; Variable declarations (avoid the real function call).
b1ca7740
GM
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\\)\\|\
e2bd4dd4 607enumerator\\|generic\\|procedure\\|logical\\|double[ \t]*precision\\)\
9877fcf1 608\\(.*::\\|[ \t]*(.*)\\)?\\([^&!\n]*\\)"
7aee8047 609 (1 font-lock-type-face t) (4 font-lock-variable-name-face t))
b1ca7740
GM
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))
5ab33946
GM
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.
b1ca7740 619 ;; NB not explicitly handling this case:
5ab33946 620 ;; integer( kind=1 ) function foo()
b1ca7740
GM
621 ;; thanks to the happy accident described above.
622 ;; Not anchored, so don't need to worry about "pure" etc.
1d5963cc 623 '("\\_<\\(\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
5ab33946 624logical\\|double[ \t]*precision\\|\
1d5963cc
SM
625\\(?:type\\|class\\)[ \t]*([ \t]*\\(?:\\sw\\|\\s_\\)+[ \t]*)\\)[ \t]*\\)\
626\\(function\\)\\_>[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*\\(([^&!\n]*)\\)"
5ab33946
GM
627 (1 font-lock-type-face t) (4 font-lock-keyword-face t)
628 (5 font-lock-function-name-face t) (6 'default t))
f43d8ecc 629 ;; enum (F2003; must be followed by ", bind(C)").
1d5963cc 630 '("\\_<\\(enum\\)[ \t]*," (1 font-lock-keyword-face))
b1ca7740 631 ;; end do, enum (F2003), if, select, where, and forall constructs.
e93db24a
GM
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.
1d5963cc
SM
635 '("\\_<\\(end[ \t]*\\(do\\|if\\|enum\\|select\\|forall\\|where\\|\
636block\\|critical\\)\\)\\_>\
637\\([ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?"
69658465 638 (1 font-lock-keyword-face) (3 font-lock-constant-face nil t))
1d5963cc 639 '("^[ \t0-9]*\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|\
5ab33946 640do\\([ \t]*while\\)?\\|select[ \t]*\\(?:case\\|type\\)\\|where\\|\
1d5963cc 641forall\\|block\\|critical\\)\\)\\_>"
69658465 642 (2 font-lock-constant-face nil t) (3 font-lock-keyword-face))
ec2f376f 643 ;; Implicit declaration.
1d5963cc 644 '("\\_<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\
5ab33946 645\\|enumerator\\|procedure\\|\
1d5963cc 646logical\\|double[ \t]*precision\\|type[ \t]*(\\(?:\\sw\\|\\s_\\)+)\\|none\\)[ \t]*"
69658465 647 (1 font-lock-keyword-face) (2 font-lock-type-face))
1d5963cc 648 '("\\_<\\(namelist\\|common\\)[ \t]*\/\\(\\(?:\\sw\\|\\s_\\)+\\)?\/"
69658465 649 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
1d5963cc 650 "\\_<else\\([ \t]*if\\|where\\)?\\_>"
7aee8047 651 '("\\(&\\)[ \t]*\\(!\\|$\\)" (1 font-lock-keyword-face))
1d5963cc
SM
652 "\\_<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\_>"
653 '("\\_<\\(exit\\|cycle\\)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?\\_>"
69658465 654 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
1d5963cc 655 '("\\_<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
5ab33946 656 ;; F2003 "class default".
1d5963cc 657 '("\\_<\\(class\\)[ \t]*default" . 1)
5ab33946 658 ;; F2003 "type is" in a "select type" block.
1d5963cc
SM
659 '("\\_<\\(\\(type\\|class\\)[ \t]*is\\)[ \t]*(" (1 font-lock-keyword-face t))
660 '("\\_<\\(do\\|go[ \t]*to\\)\\_>[ \t]*\\([0-9]+\\)"
69658465 661 (1 font-lock-keyword-face) (2 font-lock-constant-face))
f14ca250 662 ;; Line numbers (lines whose first character after number is letter).
9e29ca4f
GM
663 '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t))
664 ;; Override eg for "#include".
1d5963cc
SM
665 '("^#[ \t]*\\(?:\\sw\\|\\s_\\)+" (0 font-lock-preprocessor-face t)
666 ("\\_<defined\\_>" nil nil (0 font-lock-preprocessor-face)))
9e29ca4f 667 '("^#" ("\\(&&\\|||\\)" nil nil (0 font-lock-constant-face t)))
1d5963cc
SM
668 '("^#[ \t]*define[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)(" (1 font-lock-function-name-face))
669 '("^#[ \t]*define[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)" (1 font-lock-variable-name-face))
9e29ca4f 670 '("^#[ \t]*include[ \t]+\\(<.+>\\)" (1 font-lock-string-face))))
87ee2359 671 "Highlights declarations, do-loops and other constructs.")
ee30478d
KH
672
673(defvar f90-font-lock-keywords-3
674 (append f90-font-lock-keywords-2
69658465
GM
675 (list
676 f90-keywords-level-3-re
677 f90-operators-re
1ea193a2
GM
678 ;; FIXME why isn't this font-lock-builtin-face, which
679 ;; otherwise we hardly use, as in fortran.el?
69658465 680 (list f90-procedures-re '(1 font-lock-keyword-face keep))
1d5963cc 681 "\\_<real\\_>" ; avoid overwriting real defs
5ab33946 682 ;; As an attribute, but not as an optional argument.
1d5963cc 683 '("\\_<\\(asynchronous\\)[ \t]*[^=]" . 1)))
ee30478d
KH
684 "Highlights all F90 keywords and intrinsic procedures.")
685
686(defvar f90-font-lock-keywords-4
687 (append f90-font-lock-keywords-3
5ab33946
GM
688 (list (cons f90-constants-re 'font-lock-constant-face)
689 f90-hpf-keywords-re))
690 "Highlights all F90 and HPF keywords and constants.")
ee30478d
KH
691
692(defvar f90-font-lock-keywords
87ee2359 693 f90-font-lock-keywords-2
fb7ada5f 694 "Default expressions to highlight in F90 mode.
0ee7f068 695Can be overridden by the value of `font-lock-maximum-decoration'.")
034a9d40 696
ec2f376f 697
70186f7f
GM
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
1d5963cc 702 (modify-syntax-entry ?_ "_" table) ; underscore in names
70186f7f
GM
703 (modify-syntax-entry ?\' "\"" table) ; string quote
704 (modify-syntax-entry ?\" "\"" table) ; string quote
5d16fdd7
SM
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)
70186f7f
GM
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)
a8955be7 714 (modify-syntax-entry ?% "." table) ; bug#8820
fffa137c 715 ;; I think that the f95 standard leaves the behavior of \
784d007b
GM
716 ;; unspecified, but that f2k will require it to be non-special.
717 ;; Use `f90-backslash-not-special' to change.
70186f7f
GM
718 (modify-syntax-entry ?\\ "\\" table) ; escape chars
719 table)
720 "Syntax table used in F90 mode.")
721
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)
6f1d50da
GM
729 (define-key map "\C-\M-n" 'f90-end-of-block)
730 (define-key map "\C-\M-p" 'f90-beginning-of-block)
70186f7f
GM
731 (define-key map "\C-\M-q" 'f90-indent-subprogram)
732 (define-key map "\C-j" 'f90-indent-new-line) ; LFD equals C-j
83596348 733;;; (define-key map "\r" 'newline)
70186f7f
GM
734 (define-key map "\C-c\r" 'f90-break-line)
735;;; (define-key map [M-return] 'f90-break-line)
76bccf35
GM
736 (define-key map "\C-c\C-a" 'f90-previous-block)
737 (define-key map "\C-c\C-e" 'f90-next-block)
70186f7f
GM
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)
8a79905d 742 (define-key map "\C-c]" 'f90-insert-end)
70186f7f 743 (define-key map "\C-c\C-w" 'f90-insert-end)
83596348
GM
744 ;; Standard tab binding will call this, and also handle regions.
745;;; (define-key map "\t" 'f90-indent-line)
70186f7f
GM
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)
a729409a
GM
751
752 (easy-menu-define f90-menu map "Menu for F90 mode."
753 `("F90"
754 ("Customization"
755 ,(custom-menu-create 'f90)
68fcc3ec
GM
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
e4769531 766 :help "Erase all customizations in buffer"]
a729409a
GM
767 )
768 "--"
68fcc3ec
GM
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"]
a729409a 776 "--"
68fcc3ec
GM
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"]
a729409a 782 "--"
68fcc3ec
GM
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"]
a729409a
GM
789 "--"
790 ("Highlighting"
68fcc3ec 791 :help "Fontify this buffer to varying degrees"
a729409a 792 ["Toggle font-lock-mode" font-lock-mode :selected font-lock-mode
68fcc3ec 793 :style toggle :help "Fontify text in this buffer"]
a729409a
GM
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]
799 )
800 ("Change Keyword Case"
68fcc3ec 801 :help "Change the case of keywords in the buffer or region"
a729409a
GM
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]
812 )
813 "--"
68fcc3ec
GM
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
a729409a 820 :active (not (lookup-key (current-local-map) [menu-bar index]))
68fcc3ec
GM
821 :included (fboundp 'imenu-add-to-menubar)
822 :help "Add an index menu to the menu-bar"
823 ]))
70186f7f 824 map)
034a9d40 825 "Keymap used in F90 mode.")
ee30478d 826
b974df0a 827
599aeab9
GM
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))
834
1a341882
GM
835(defun f90-font-lock-1 ()
836 "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
837 (interactive)
599aeab9 838 (f90-font-lock-n 1))
1a341882
GM
839
840(defun f90-font-lock-2 ()
841 "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
842 (interactive)
599aeab9 843 (f90-font-lock-n 2))
1a341882
GM
844
845(defun f90-font-lock-3 ()
846 "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
847 (interactive)
599aeab9 848 (f90-font-lock-n 3))
1a341882
GM
849
850(defun f90-font-lock-4 ()
851 "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
852 (interactive)
599aeab9 853 (f90-font-lock-n 4))
1a341882 854\f
ee30478d 855;; Regexps for finding program structures.
69658465 856(defconst f90-blocks-re
5ac2eb34 857 (concat "\\(\\(?:block[ \t]*data\\|"
ec2f376f 858 (regexp-opt '("do" "if" "interface" "function" "module" "program"
5ab33946
GM
859 "select" "subroutine" "type" "where" "forall"
860 ;; F2003.
5d8e0d43
GM
861 "enum" "associate"
862 ;; F2008.
e93db24a 863 "submodule" "block" "critical"))
5ac2eb34 864 "\\)\\_>\\)")
ec2f376f
GM
865 "Regexp potentially indicating a \"block\" of F90 code.")
866
69658465 867(defconst f90-program-block-re
5d8e0d43 868 (regexp-opt '("program" "module" "subroutine" "function" "submodule") 'paren)
ec2f376f
GM
869 "Regexp used to locate the start/end of a \"subprogram\".")
870
5ab33946 871;; "class is" is F2003.
69658465 872(defconst f90-else-like-re
5ab33946
GM
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.")
ec2f376f 876
69658465 877(defconst f90-end-if-re
ec2f376f
GM
878 (concat "end[ \t]*"
879 (regexp-opt '("if" "select" "where" "forall") 'paren)
1d5963cc 880 "\\_>")
ec2f376f
GM
881 "Regexp matching the end of an IF, SELECT, WHERE, FORALL block.")
882
69658465 883(defconst f90-end-type-re
1d5963cc 884 "end[ \t]*\\(type\\|enum\\|interface\\|block[ \t]*data\\)\\_>"
5ab33946
GM
885 "Regexp matching the end of a TYPE, ENUM, INTERFACE, BLOCK DATA section.")
886
887(defconst f90-end-associate-re
1d5963cc 888 "end[ \t]*associate\\_>"
5ab33946 889 "Regexp matching the end of an ASSOCIATE block.")
ec2f376f 890
5ab33946
GM
891;; This is for a TYPE block, not a variable of derived TYPE.
892;; Hence no need to add CLASS for F2003.
ee30478d 893(defconst f90-type-def-re
5ab33946
GM
894 ;; type word
895 ;; type :: word
896 ;; type, stuff :: word
d1f21341 897 ;; type, bind(c) :: word
5ab33946 898 ;; NOT "type ("
1d5963cc
SM
899 "\\_<\\(type\\)\\_>\\(?:\\(?:[^()\n]*\\|\
900.*,[ \t]*bind[ \t]*([ \t]*c[ \t]*)[ \t]*\\)::\\)?[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
e7272ece 901 "Regexp matching the definition of a derived type.")
ec2f376f 902
5ab33946 903(defconst f90-typeis-re
1d5963cc 904 "\\_<\\(class\\|type\\)[ \t]*is[ \t]*("
5ab33946
GM
905 "Regexp matching a CLASS/TYPE IS statement.")
906
ec2f376f 907(defconst f90-no-break-re
b0d5b05b 908 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=" "(/" "/)") 'paren)
3f150a60
GM
909 "Regexp specifying two-character tokens not to split when breaking lines.
910Each token has one or more of the characters from `f90-break-delimiters'.
911Note that if only one of the characters is from that variable,
912then the presence of the token here allows a line-break before or
913after the other character, where a break would not normally be
914allowed. This minor issue currently only affects \"(/\" and \"/)\".")
ec2f376f
GM
915
916(defvar f90-cache-position nil
917 "Temporary position used to speed up region operations.")
034a9d40 918(make-variable-buffer-local 'f90-cache-position)
ec2f376f 919
b974df0a 920\f
799dee7a
GM
921;; Hideshow support.
922(defconst f90-end-block-re
1d5963cc 923 (concat "^[ \t0-9]*\\_<end[ \t]*"
799dee7a 924 (regexp-opt '("do" "if" "forall" "function" "interface"
fcca5273 925 "module" "program" "select" "subroutine"
e93db24a
GM
926 "type" "where" "enum" "associate" "submodule"
927 "block" "critical") t)
1d5963cc 928 "\\_>")
fcca5273 929 "Regexp matching the end of an F90 \"block\", from the line start.
799dee7a
GM
930Used in the F90 entry in `hs-special-modes-alist'.")
931
932;; Ignore the fact that FUNCTION, SUBROUTINE, WHERE, FORALL have a
fcca5273 933;; following "(". DO, CASE, IF can have labels.
799dee7a
GM
934(defconst f90-start-block-re
935 (concat
936 "^[ \t0-9]*" ; statement number
937 "\\(\\("
1d5963cc 938 "\\(\\(?:\\sw\\|\\s_\\)+[ \t]*:[ \t]*\\)?" ; structure label
5ab33946 939 "\\(do\\|select[ \t]*\\(case\\|type\\)\\|"
fcca5273
GM
940 ;; See comments in fortran-start-block-re for the problems of IF.
941 "if[ \t]*(\\(.*\\|"
1d5963cc 942 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\_>\\)\\)\\)\\_<then\\|"
799dee7a
GM
943 ;; Distinguish WHERE block from isolated WHERE.
944 "\\(where\\|forall\\)[ \t]*(.*)[ \t]*\\(!\\|$\\)\\)\\)"
945 "\\|"
5ab33946
GM
946 ;; Avoid F2003 "type is" in "select type",
947 ;; and also variables of derived type "type (foo)".
948 ;; "type, foo" must be a block (?).
b1ca7740
GM
949 "type[ \t,]\\("
950 "[^i(!\n\"\& \t]\\|" ; not-i(
951 "i[^s!\n\"\& \t]\\|" ; i not-s
1d5963cc 952 "is\\(?:\\sw\\|\\s_\\)\\)\\|"
5d8e0d43
GM
953 ;; "abstract interface" is F2003; "submodule" is F2008.
954 "program\\|\\(?:abstract[ \t]*\\)?interface\\|\\(?:sub\\)?module\\|"
5ab33946 955 ;; "enum", but not "enumerator".
e93db24a 956 "function\\|subroutine\\|enum[^e]\\|associate\\|block\\|critical"
799dee7a
GM
957 "\\)"
958 "[ \t]*")
fcca5273 959 "Regexp matching the start of an F90 \"block\", from the line start.
799dee7a
GM
960A simple regexp cannot do this in fully correct fashion, so this
961tries to strike a compromise between complexity and flexibility.
962Used in the F90 entry in `hs-special-modes-alist'.")
963
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))
968
969\f
ec2f376f 970;; Imenu support.
b1ca7740
GM
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.
974Set subexpression 1 in the match-data to the name of the type."
5d16fdd7 975 (let (found)
b1ca7740
GM
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))
1d5963cc
SM
980 (unless (looking-at "\\(is\\_>\\|(\\)")
981 (or (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
b1ca7740 982 (re-search-forward
1d5963cc 983 "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
b1ca7740
GM
984 (line-end-position) t))))))))
985 found))
986
ee30478d 987(defvar f90-imenu-generic-expression
b974df0a 988 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
b1ca7740 989 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]")
5d16fdd7
SM
990 ;; (not-ib "[^i(!\n\"\& \t]") (not-s "[^s!\n\"\& \t]")
991 )
1d5963cc
SM
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)
0ac0fecb 996 ("Types" f90-imenu-type-matcher 1)
1d5963cc
SM
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)))
a729409a 1021 "Value for `imenu-generic-expression' in F90 mode.")
ee30478d 1022
b974df0a 1023(defun f90-add-imenu-menu ()
b974df0a 1024 "Add an imenu menu to the menubar."
87ee2359 1025 (interactive)
34ba7e3d 1026 (if (lookup-key (current-local-map) [menu-bar index])
5c2a80ad
GM
1027 (message "%s" "F90-imenu already exists.")
1028 (imenu-add-to-menubar "F90-imenu")
12b4c0ea 1029 (redraw-frame)))
b974df0a 1030
034a9d40 1031\f
ec2f376f 1032;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
5d16fdd7
SM
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" )
e93db24a 1042 ("`bl" . "block" )
5d16fdd7
SM
1043 ("`c" . "character" )
1044 ("`cl" . "close" )
1045 ("`cm" . "common" )
1046 ("`cx" . "complex" )
1047 ("`cn" . "contains" )
e93db24a 1048 ("`cr" . "critical" )
5d16fdd7
SM
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:]_]*")
0761b294 1100\f
034a9d40 1101;;;###autoload
e75c1e7d 1102(define-derived-mode f90-mode prog-mode "F90"
87ee2359 1103 "Major mode for editing Fortran 90,95 code in free format.
a729409a 1104For fixed format code, use `fortran-mode'.
034a9d40 1105
a729409a 1106\\[f90-indent-line] indents the current line.
ec2f376f 1107\\[f90-indent-new-line] indents current line and creates a new\
034a9d40 1108 indented line.
87ee2359 1109\\[f90-indent-subprogram] indents the current subprogram.
034a9d40
RS
1110
1111Type `? or `\\[help-command] to display a list of built-in\
1112 abbrevs for F90 keywords.
1113
1114Key definitions:
1115\\{f90-mode-map}
1116
1117Variables controlling indentation style and extra features:
1118
ec2f376f
GM
1119`f90-do-indent'
1120 Extra indentation within do blocks (default 3).
1121`f90-if-indent'
5ab33946 1122 Extra indentation within if/select/where/forall blocks (default 3).
ec2f376f 1123`f90-type-indent'
5ab33946 1124 Extra indentation within type/enum/interface/block-data blocks (default 3).
ec2f376f
GM
1125`f90-program-indent'
1126 Extra indentation within program/module/subroutine/function blocks
1127 (default 2).
e93db24a
GM
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).
ec2f376f
GM
1132`f90-continuation-indent'
1133 Extra indentation applied to continuation lines (default 5).
1134`f90-comment-region'
e3f5ce56
GM
1135 String inserted by function \\[f90-comment-region] at start of each
1136 line in region (default \"!!!$\").
ec2f376f
GM
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.
5ac2eb34
SM
1153 Allowed values are `blink', `no-blink', and nil, which determine
1154 whether to blink the matching beginning (default `blink').
ec2f376f
GM
1155`f90-auto-keyword-case'
1156 Automatic change of case of keywords (default nil).
1d5963cc 1157 The possibilities are `downcase-word', `upcase-word', `capitalize-word'.
ec2f376f
GM
1158`f90-leave-line-no'
1159 Do not left-justify line numbers (default nil).
034a9d40
RS
1160
1161Turning on F90 mode calls the value of the variable `f90-mode-hook'
1162with no args, if that value is non-nil."
e75c1e7d 1163 :group 'f90
e75c1e7d 1164 :abbrev-table f90-mode-abbrev-table
e3f5ce56
GM
1165 (set (make-local-variable 'indent-line-function) 'f90-indent-line)
1166 (set (make-local-variable 'indent-region-function) 'f90-indent-region)
e3f5ce56
GM
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)
70186f7f 1172 (setq indent-tabs-mode nil) ; auto buffer local
e3f5ce56
GM
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))
45d1e4d4 1179 (set (make-local-variable 'imenu-case-fold-search) t)
e3f5ce56 1180 (set (make-local-variable 'imenu-generic-expression)
a729409a
GM
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)
d2d15846 1185 (set (make-local-variable 'add-log-current-defun-function)
e75c1e7d 1186 #'f90-current-defun))
ec2f376f 1187
034a9d40 1188\f
ec2f376f 1189;; Inline-functions.
034a9d40 1190(defsubst f90-in-string ()
d14e6bbe 1191 "Return non-nil if point is inside a string.
ec2f376f 1192Checks from `point-min', or `f90-cache-position', if that is non-nil
d14e6bbe 1193and lies before point."
034a9d40 1194 (let ((beg-pnt
640f9e26
GM
1195 (if (and f90-cache-position (> (point) f90-cache-position))
1196 f90-cache-position
1197 (point-min))))
034a9d40 1198 (nth 3 (parse-partial-sexp beg-pnt (point)))))
69658465 1199
034a9d40 1200(defsubst f90-in-comment ()
d14e6bbe 1201 "Return non-nil if point is inside a comment.
ec2f376f 1202Checks from `point-min', or `f90-cache-position', if that is non-nil
d14e6bbe 1203and lies before point."
034a9d40 1204 (let ((beg-pnt
640f9e26
GM
1205 (if (and f90-cache-position (> (point) f90-cache-position))
1206 f90-cache-position
1207 (point-min))))
034a9d40
RS
1208 (nth 4 (parse-partial-sexp beg-pnt (point)))))
1209
1210(defsubst f90-line-continued ()
d14e6bbe 1211 "Return t if the current line is a continued one.
a0099d31
GM
1212This includes comment or preprocessor lines embedded in continued lines,
1213but not the last line of a continued statement."
034a9d40 1214 (save-excursion
6734e165 1215 (beginning-of-line)
a0099d31 1216 (while (and (looking-at "[ \t]*\\([!#]\\|$\\)") (zerop (forward-line -1))))
e3f5ce56
GM
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) ?&)))
034a9d40 1223
748dd5a8
GM
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...
7aee8047
GM
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)
034a9d40
RS
1232(defsubst f90-current-indentation ()
1233 "Return indentation of current line.
1234Line-numbers are considered whitespace characters."
e3f5ce56 1235 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
034a9d40
RS
1236
1237(defsubst f90-indent-to (col &optional no-line-number)
1238 "Indent current line to column COL.
d14e6bbe
GM
1239If optional argument NO-LINE-NUMBER is nil, jump over a possible
1240line-number before indenting."
034a9d40 1241 (beginning-of-line)
748dd5a8 1242 (or no-line-number
034a9d40
RS
1243 (skip-chars-forward " \t0-9"))
1244 (delete-horizontal-space)
748dd5a8
GM
1245 ;; Leave >= 1 space after line number.
1246 (indent-to col (if (zerop (current-column)) 0 1)))
034a9d40 1247
034a9d40 1248(defsubst f90-get-present-comment-type ()
d14e6bbe 1249 "If point lies within a comment, return the string starting the comment.
718d0706
GM
1250For example, \"!\" or \"!!\", followed by the appropriate amount of
1251whitespace, if any."
1252 ;; Include the whitespace for consistent auto-filling of comment blocks.
034a9d40 1253 (save-excursion
e3f5ce56
GM
1254 (when (f90-in-comment)
1255 (beginning-of-line)
718d0706 1256 (re-search-forward "!+[ \t]*" (line-end-position))
e3f5ce56 1257 (while (f90-in-string)
718d0706
GM
1258 (re-search-forward "!+[ \t]*" (line-end-position)))
1259 (match-string-no-properties 0))))
034a9d40
RS
1260
1261(defsubst f90-equal-symbols (a b)
ec2f376f 1262 "Compare strings A and B neglecting case and allowing for nil value."
f14ca250
GM
1263 (equal (if a (downcase a) nil)
1264 (if b (downcase b) nil)))
034a9d40 1265
034a9d40 1266(defsubst f90-looking-at-do ()
d14e6bbe
GM
1267 "Return (\"do\" NAME) if a do statement starts after point.
1268NAME is nil if the statement has no label."
1d5963cc 1269 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\_>")
0db701f0 1270 (list (match-string 3) (match-string 2))))
ee30478d
KH
1271
1272(defsubst f90-looking-at-select-case ()
5ab33946 1273 "Return (\"select\" NAME) if a select statement starts after point.
d14e6bbe 1274NAME is nil if the statement has no label."
1d5963cc 1275 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\
5ab33946 1276\\(select\\)[ \t]*\\(case\\|type\\)[ \t]*(")
748dd5a8 1277 (list (match-string 3) (match-string 2))))
034a9d40
RS
1278
1279(defsubst f90-looking-at-if-then ()
d14e6bbe
GM
1280 "Return (\"if\" NAME) if an if () then statement starts after point.
1281NAME is nil if the statement has no label."
034a9d40 1282 (save-excursion
1d5963cc 1283 (when (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\_>")
f14ca250 1284 (let ((struct (match-string 3))
748dd5a8 1285 (label (match-string 2))
f14ca250
GM
1286 (pos (scan-lists (point) 1 0)))
1287 (and pos (goto-char pos))
5c2a80ad 1288 (skip-chars-forward " \t")
1d5963cc 1289 (if (or (looking-at "then\\_>")
5c2a80ad
GM
1290 (when (f90-line-continued)
1291 (f90-next-statement)
1292 (skip-chars-forward " \t0-9&")
1d5963cc 1293 (looking-at "then\\_>")))
5c2a80ad 1294 (list struct label))))))
034a9d40 1295
5ab33946
GM
1296;; FIXME label?
1297(defsubst f90-looking-at-associate ()
1298 "Return (\"associate\") if an associate block starts after point."
1d5963cc 1299 (if (looking-at "\\_<\\(associate\\)[ \t]*(")
5ab33946
GM
1300 (list (match-string 1))))
1301
e93db24a 1302(defsubst f90-looking-at-critical ()
ee584d02 1303 "Return (KIND NAME) if a critical or block block starts after point."
1d5963cc 1304 (if (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\\(critical\\|block\\)\\_>")
e93db24a
GM
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")
1d5963cc 1310 (not (looking-at "data\\_>"))))
e93db24a
GM
1311 (list struct label)))))
1312
1313(defsubst f90-looking-at-end-critical ()
1314 "Return non-nil if a critical or block block ends after point."
1d5963cc 1315 (if (looking-at "end[ \t]*\\(critical\\|block\\)\\_>")
e93db24a
GM
1316 (or (not (string-equal "block" (match-string 1)))
1317 (save-excursion
1318 (skip-chars-forward " \t")
1d5963cc 1319 (not (looking-at "data\\_>"))))))
e93db24a 1320
b32a3d99 1321(defsubst f90-looking-at-where-or-forall ()
d14e6bbe
GM
1322 "Return (KIND NAME) if a where or forall block starts after point.
1323NAME is nil if the statement has no label."
f14ca250 1324 (save-excursion
1d5963cc
SM
1325 (when (looking-at "\\(\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:\\)?[ \t]*\
1326\\(where\\|forall\\)\\_>")
f14ca250 1327 (let ((struct (match-string 3))
748dd5a8 1328 (label (match-string 2))
f14ca250
GM
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))))))
034a9d40
RS
1333
1334(defsubst f90-looking-at-type-like ()
5ab33946 1335 "Return (KIND NAME) if a type/enum/interface/block-data starts after point.
7cf3f3d6 1336NAME is non-nil only for type and certain interfaces."
69658465 1337 (cond
5ab33946 1338 ((save-excursion
1d5963cc 1339 (and (looking-at "\\_<type\\_>[ \t]*")
5ab33946 1340 (goto-char (match-end 0))
1d5963cc
SM
1341 (not (looking-at "\\(is\\_>\\|(\\)"))
1342 (or (looking-at "\\(\\(?:\\sw\\|\\s_\\)+\\)")
1343 (re-search-forward "[ \t]*::[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)"
5ab33946
GM
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)))
1d5963cc 1349 ((looking-at "\\_<\\(interface\\)\\_>[ \t]*")
7cf3f3d6
GM
1350 (list (match-string 1)
1351 (save-excursion
1352 (goto-char (match-end 0))
1353 (if (or (looking-at "\\(operator\\|assignment\\|read\\|\
1354write\\)[ \t]*([^)\n]*)")
1d5963cc 1355 (looking-at "\\(?:\\sw\\|\\s_\\)+"))
7cf3f3d6 1356 (match-string 0)))))
1d5963cc 1357 ((looking-at "\\(enum\\|block[ \t]*data\\)\\_>")
5ab33946 1358 (list (match-string 1) nil))
1d5963cc 1359 ((looking-at "abstract[ \t]*\\(interface\\)\\_>")
6734e165 1360 (list (match-string 1) nil))))
034a9d40
RS
1361
1362(defsubst f90-looking-at-program-block-start ()
d14e6bbe 1363 "Return (KIND NAME) if a program block with name NAME starts after point."
784d007b 1364;;;NAME is nil for an un-named main PROGRAM block."
034a9d40 1365 (cond
1d5963cc 1366 ((looking-at "\\(program\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
6734e165 1367 (list (match-string 1) (match-string 2)))
1d5963cc
SM
1368 ((and (not (looking-at "module[ \t]*procedure\\_>"))
1369 (looking-at "\\(module\\)[ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>"))
6734e165 1370 (list (match-string 1) (match-string 2)))
1d5963cc 1371 ((looking-at "\\(submodule\\)[ \t]*([^)\n]+)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)\\_>")
5d8e0d43 1372 (list (match-string 1) (match-string 2)))
b974df0a 1373 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
640f9e26 1374 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
1d5963cc 1375\\(\\(?:\\sw\\|\\s_\\)+\\)"))
6734e165 1376 (list (match-string 1) (match-string 2)))))
784d007b
GM
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))))
034a9d40
RS
1384
1385(defsubst f90-looking-at-program-block-end ()
d14e6bbe 1386 "Return (KIND NAME) if a block with name NAME ends after point."
7cf3f3d6
GM
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
1d5963cc 1391 "?\\([ \t]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?\\_>"))
7cf3f3d6 1392 (list (match-string 1) (match-string 3)))))
034a9d40
RS
1393
1394(defsubst f90-comment-indent ()
ec2f376f
GM
1395 "Return the indentation to be used for a comment starting at point.
1396Used 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'.
89fa1ef5 1399All others return `comment-column', leaving at least one space after code."
034a9d40 1400 (cond ((looking-at "!!!") 0)
640f9e26
GM
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
b464e0ee
GM
1411 (skip-chars-backward " \t")
1412 (max (if (bolp) 0 (1+ (current-column))) comment-column)))))
034a9d40
RS
1413
1414(defsubst f90-present-statement-cont ()
d14e6bbe
GM
1415 "Return continuation properties of present statement.
1416Possible return values are:
1417single - statement is not continued.
1418begin - current line is the first in a continued statement.
1419end - current line is the last in a continued statement
1420middle - current line is neither first nor last in a continued statement.
1421Comment lines embedded amongst continued lines return 'middle."
034a9d40
RS
1422 (let (pcont cont)
1423 (save-excursion
e3f5ce56 1424 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
034a9d40
RS
1425 (setq cont (f90-line-continued))
1426 (cond ((and (not pcont) (not cont)) 'single)
640f9e26
GM
1427 ((and (not pcont) cont) 'begin)
1428 ((and pcont (not cont)) 'end)
1429 ((and pcont cont) 'middle)
1430 (t (error "The impossible occurred")))))
034a9d40
RS
1431
1432(defsubst f90-indent-line-no ()
d14e6bbe
GM
1433 "If `f90-leave-line-no' is nil, left-justify a line number.
1434Leaves point at the first non-blank character after the line number.
1435Call from beginning of line."
748dd5a8
GM
1436 (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
1437 (delete-horizontal-space))
034a9d40
RS
1438 (skip-chars-forward " \t0-9"))
1439
1440(defsubst f90-no-block-limit ()
d14e6bbe
GM
1441 "Return nil if point is at the edge of a code block.
1442Searches line forward for \"function\" or \"subroutine\",
1443if all else fails."
748dd5a8
GM
1444 (save-excursion
1445 (not (or (looking-at "end")
1446 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
e93db24a 1447\\|select[ \t]*\\(case\\|type\\)\\|case\\|where\\|forall\\|\
1d5963cc 1448block\\|critical\\)\\_>")
5d8e0d43 1449 (looking-at "\\(program\\|\\(?:sub\\)?module\\|\
1d5963cc
SM
1450\\(?:abstract[ \t]*\\)?interface\\|block[ \t]*data\\)\\_>")
1451 (looking-at "\\(contains\\|\\(?:\\sw\\|\\s_\\)+[ \t]*:\\)")
748dd5a8
GM
1452 (looking-at f90-type-def-re)
1453 (re-search-forward "\\(function\\|subroutine\\)"
1454 (line-end-position) t)))))
034a9d40
RS
1455
1456(defsubst f90-update-line ()
d14e6bbe
GM
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))))
034a9d40 1461\f
d694ccd7 1462(defun f90-electric-insert (&optional arg)
d14e6bbe 1463 "Change keyword case and auto-fill line as operators are inserted."
d694ccd7
GM
1464 (interactive "*p")
1465 (self-insert-command arg)
d14e6bbe
GM
1466 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1467 (f90-update-line)))
1468
055e4eea
GM
1469;; Behave like self-insert-command for delete-selection-mode (bug#5593).
1470(put 'f90-electric-insert 'delete-selection t)
7cae52cf 1471
034a9d40
RS
1472(defun f90-get-correct-indent ()
1473 "Get correct indent for a line starting with line number.
1474Does not check type and subprogram indentation."
239da61d 1475 (let ((epnt (line-end-position)) icol)
034a9d40
RS
1476 (save-excursion
1477 (while (and (f90-previous-statement)
239da61d 1478 (or (memq (f90-present-statement-cont) '(middle end))
640f9e26 1479 (looking-at "[ \t]*[0-9]"))))
034a9d40
RS
1480 (setq icol (current-indentation))
1481 (beginning-of-line)
5c2a80ad
GM
1482 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1483 (line-end-position) t)
e3f5ce56
GM
1484 (beginning-of-line)
1485 (skip-chars-forward " \t")
5c2a80ad
GM
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))
5ab33946 1491 (setq icol (+ icol f90-if-indent)))
e93db24a
GM
1492 ;; FIXME this makes no sense, because this section/function is
1493 ;; only for if/do/select/where/forall ?
5ab33946
GM
1494 ((f90-looking-at-associate)
1495 (setq icol (+ icol f90-associate-indent))))
5c2a80ad 1496 (end-of-line))
034a9d40 1497 (while (re-search-forward
640f9e26
GM
1498 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1499 (beginning-of-line)
e3f5ce56 1500 (skip-chars-forward " \t0-9")
640f9e26 1501 (cond ((f90-looking-at-do)
e3f5ce56
GM
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)))
e93db24a
GM
1507 ;; FIXME this makes no sense, because this section/function is
1508 ;; only for if/do/select/where/forall ?
5ab33946
GM
1509 ((f90-looking-at-associate)
1510 (setq icol (+ icol f90-associate-indent)))
e3f5ce56
GM
1511 ((looking-at f90-end-if-re)
1512 (setq icol (- icol f90-if-indent)))
5ab33946
GM
1513 ((looking-at f90-end-associate-re)
1514 (setq icol (- icol f90-associate-indent)))
e93db24a
GM
1515 ((f90-looking-at-end-critical)
1516 (setq icol (- icol f90-critical-indent)))
1d5963cc 1517 ((looking-at "end[ \t]*do\\_>")
e3f5ce56 1518 (setq icol (- icol f90-do-indent))))
640f9e26 1519 (end-of-line))
034a9d40 1520 icol)))
69658465 1521
034a9d40
RS
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))
b78cbdf7
GM
1528 ;; If f90-previous-statement returns nil, we must have been
1529 ;; called from on or before the first line of the first statement.
0812589b
GM
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)))
784d007b
GM
1543 0
1544 ;; No explicit PROGRAM start statement.
1545 f90-program-indent))
640f9e26
GM
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)))
5ab33946
GM
1567 ((f90-looking-at-associate)
1568 (setq icol (+ icol f90-associate-indent)))
e93db24a
GM
1569 ((f90-looking-at-critical)
1570 (setq icol (+ icol f90-critical-indent)))
640f9e26
GM
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))
1579 (t
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)))
1d5963cc 1584 ((looking-at "end[ \t]*do\\_>")
640f9e26
GM
1585 (setq icol (- icol f90-do-indent)))
1586 ((looking-at f90-end-type-re)
1587 (setq icol (- icol f90-type-indent)))
5ab33946
GM
1588 ((looking-at f90-end-associate-re)
1589 (setq icol (- icol f90-associate-indent)))
e93db24a
GM
1590 ((f90-looking-at-end-critical)
1591 (setq icol (- icol f90-critical-indent)))
640f9e26
GM
1592 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1593 (f90-looking-at-program-block-end))
599aeab9 1594 (setq icol (- icol f90-program-indent))))))))))
034a9d40
RS
1595 icol))
1596\f
034a9d40
RS
1597(defun f90-previous-statement ()
1598 "Move point to beginning of the previous F90 statement.
784d007b
GM
1599If no previous statement is found (i.e. if called from the first
1600statement in the buffer), move to the start of the buffer and
1601return nil. A statement is a line which is neither blank nor a
1602comment."
034a9d40
RS
1603 (interactive)
1604 (let (not-first-statement)
1605 (beginning-of-line)
1606 (while (and (setq not-first-statement (zerop (forward-line -1)))
640f9e26 1607 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
034a9d40
RS
1608 not-first-statement))
1609
1610(defun f90-next-statement ()
1611 "Move point to beginning of the next F90 statement.
1612Return 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
640f9e26
GM
1617 (and (zerop (forward-line 1))
1618 (not (eobp))))
c02ee9d6 1619 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
034a9d40
RS
1620 not-last-statement))
1621
1622(defun f90-beginning-of-subprogram ()
76bccf35 1623 "Move point to the beginning of the current subprogram.
ec2f376f 1624Return (TYPE NAME), or nil if not found."
034a9d40
RS
1625 (interactive)
1626 (let ((count 1) (case-fold-search t) matching-beg)
e3f5ce56 1627 (beginning-of-line)
76bccf35 1628 (while (and (> count 0)
640f9e26 1629 (re-search-backward f90-program-block-re nil 'move))
e3f5ce56
GM
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)))))
034a9d40
RS
1636 (beginning-of-line)
1637 (if (zerop count)
640f9e26 1638 matching-beg
784d007b
GM
1639 ;; Note this includes the case of an un-named main program,
1640 ;; in which case we go to (point-min).
32226619
JB
1641 (if (called-interactively-p 'interactive)
1642 (message "No beginning found"))
034a9d40
RS
1643 nil)))
1644
1645(defun f90-end-of-subprogram ()
76bccf35 1646 "Move point to the end of the current subprogram.
ec2f376f 1647Return (TYPE NAME), or nil if not found."
034a9d40 1648 (interactive)
7aee8047 1649 (let ((case-fold-search t)
575b6746 1650 (count 1)
7aee8047 1651 matching-end)
034a9d40 1652 (end-of-line)
76bccf35 1653 (while (and (> count 0)
640f9e26 1654 (re-search-forward f90-program-block-re nil 'move))
e3f5ce56
GM
1655 (beginning-of-line)
1656 (skip-chars-forward " \t0-9")
034a9d40 1657 (cond ((f90-looking-at-program-block-start)
640f9e26
GM
1658 (setq count (1+ count)))
1659 ((setq matching-end (f90-looking-at-program-block-end))
1660 (setq count (1- count))))
034a9d40 1661 (end-of-line))
6f1d50da
GM
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)
034a9d40 1665 (if (zerop count)
640f9e26 1666 matching-end
32226619
JB
1667 (if (called-interactively-p 'interactive)
1668 (message "No end found"))
034a9d40
RS
1669 nil)))
1670
6f1d50da
GM
1671
1672(defun f90-end-of-block (&optional num)
1673 "Move point forward to the end of the current code block.
1674With optional argument NUM, go forward that many balanced blocks.
784d007b
GM
1675If NUM is negative, go backward to the start of a block. Checks
1676for consistency of block types and labels (if present), and
1677completes outermost block if `f90-smart-end' is non-nil.
1678Interactively, pushes mark before moving point."
6f1d50da 1679 (interactive "p")
7938ab41
GM
1680 ;; Can move some distance.
1681 (if (called-interactively-p 'any) (push-mark (point) t))
784d007b
GM
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
6f1d50da
GM
1684 (case-fold-search t)
1685 (count (or num 1))
1686 start-list start-this start-type start-label end-type end-label)
6f1d50da
GM
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
1693 (or
1694 (f90-looking-at-do)
1695 (f90-looking-at-select-case)
1696 (f90-looking-at-type-like)
5ab33946 1697 (f90-looking-at-associate)
e93db24a 1698 (f90-looking-at-critical)
6f1d50da
GM
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
1d5963cc 1705 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
6f1d50da
GM
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))
748dd5a8 1715 (or (f90-equal-symbols start-type end-type)
6f1d50da
GM
1716 (error "End type `%s' does not match start type `%s'"
1717 end-type start-type))
748dd5a8 1718 (or (f90-equal-symbols start-label end-label)
6f1d50da
GM
1719 (error "End label `%s' does not match start label `%s'"
1720 end-label start-label)))))
1721 (end-of-line))
76bccf35 1722 (if (> count 0) (error "Missing block end"))
6f1d50da 1723 ;; Check outermost block.
784d007b
GM
1724 (when f90-smart-end
1725 (save-excursion
1726 (beginning-of-line)
1727 (skip-chars-forward " \t0-9")
1728 (f90-match-end)))))
6f1d50da
GM
1729
1730(defun f90-beginning-of-block (&optional num)
1731 "Move point backwards to the start of the current code block.
1732With optional argument NUM, go backward that many balanced blocks.
1733If NUM is negative, go forward to the end of a block.
1734Checks for consistency of block types and labels (if present).
784d007b
GM
1735Does not check the outermost block, because it may be incomplete.
1736Interactively, pushes mark before moving point."
6f1d50da 1737 (interactive "p")
7938ab41 1738 (if (called-interactively-p 'any) (push-mark (point) t))
784d007b 1739 (and num (< num 0) (f90-end-of-block (- num)))
6f1d50da
GM
1740 (let ((case-fold-search t)
1741 (count (or num 1))
748dd5a8
GM
1742 end-list end-this end-type end-label
1743 start-this start-type start-label)
6f1d50da
GM
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
1d5963cc 1750 "[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"))
6f1d50da
GM
1751 (setq end-list (cons (list (match-string 1) (match-string 2))
1752 end-list)
1753 count (1+ count)))
1754 ((setq start-this
1755 (or
1756 (f90-looking-at-do)
1757 (f90-looking-at-select-case)
1758 (f90-looking-at-type-like)
5ab33946 1759 (f90-looking-at-associate)
e93db24a 1760 (f90-looking-at-critical)
6f1d50da
GM
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))
748dd5a8 1773 (or (f90-equal-symbols start-type end-type)
6f1d50da
GM
1774 (error "Start type `%s' does not match end type `%s'"
1775 start-type end-type))
748dd5a8 1776 (or (f90-equal-symbols start-label end-label)
6f1d50da
GM
1777 (error "Start label `%s' does not match end label `%s'"
1778 start-label end-label))))))
784d007b
GM
1779 ;; Includes an un-named main program block.
1780 (if (> count 0) (error "Missing block start"))))
6f1d50da 1781
76bccf35
GM
1782(defun f90-next-block (&optional num)
1783 "Move point forward to the next end or start of a code block.
1784With optional argument NUM, go forward that many blocks.
1785If NUM is negative, go backwards.
1786A block is a subroutine, if-endif, etc."
6f1d50da 1787 (interactive "p")
76bccf35
GM
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)))
6f1d50da
GM
1793 (beginning-of-line)
1794 (skip-chars-forward " \t0-9")
76bccf35
GM
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)
5ab33946 1801 (f90-looking-at-associate)
e93db24a 1802 (f90-looking-at-critical)
76bccf35
GM
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)))))
1809
1810
1811(defun f90-previous-block (&optional num)
1812 "Move point backward to the previous end or start of a code block.
1813With optional argument NUM, go backward that many blocks.
1814If NUM is negative, go forwards.
1815A block is a subroutine, if-endif, etc."
6f1d50da 1816 (interactive "p")
76bccf35 1817 (f90-next-block (- (or num 1))))
6f1d50da
GM
1818
1819
034a9d40 1820(defun f90-mark-subprogram ()
a729409a 1821 "Put mark at end of F90 subprogram, point at beginning, push mark."
034a9d40
RS
1822 (interactive)
1823 (let ((pos (point)) program)
1824 (f90-end-of-subprogram)
0ee7f068 1825 (push-mark)
034a9d40
RS
1826 (goto-char pos)
1827 (setq program (f90-beginning-of-subprogram))
a445370f 1828 (if (featurep 'xemacs)
0ee7f068 1829 (zmacs-activate-region)
1bb3ae5c 1830 (setq mark-active t
0ee7f068 1831 deactivate-mark nil))
034a9d40
RS
1832 program))
1833
1834(defun f90-comment-region (beg-region end-region)
1835 "Comment/uncomment every line in the region.
d14e6bbe
GM
1836Insert the variable `f90-comment-region' at the start of every line
1837in the region, or, if already present, remove it."
034a9d40 1838 (interactive "*r")
748dd5a8 1839 (let ((end (copy-marker end-region)))
034a9d40
RS
1840 (goto-char beg-region)
1841 (beginning-of-line)
1842 (if (looking-at (regexp-quote f90-comment-region))
640f9e26 1843 (delete-region (point) (match-end 0))
034a9d40 1844 (insert f90-comment-region))
e3f5ce56 1845 (while (and (zerop (forward-line 1))
640f9e26 1846 (< (point) end))
034a9d40 1847 (if (looking-at (regexp-quote f90-comment-region))
640f9e26
GM
1848 (delete-region (point) (match-end 0))
1849 (insert f90-comment-region)))
034a9d40
RS
1850 (set-marker end nil)))
1851
1852(defun f90-indent-line (&optional no-update)
87ee2359
GM
1853 "Indent current line as F90 code.
1854Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1855after indenting."
a729409a 1856 (interactive "*P")
748dd5a8
GM
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"))
a0099d31
GM
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))
1871 (t
1872 (and f90-smart-end (looking-at "end")
1873 (f90-match-end))
1874 (setq indent (f90-calculate-indent))))
748dd5a8 1875 (or (= indent (current-column))
e3f5ce56 1876 (f90-indent-to indent no-line-number))
034a9d40
RS
1877 ;; If initial point was within line's indentation,
1878 ;; position after the indentation. Else stay at same point in text.
748dd5a8
GM
1879 (and (< (point) pos)
1880 (goto-char pos))
69658465 1881 (if auto-fill-function
c5e87d10 1882 ;; GM NO-UPDATE not honored, since this calls f90-update-line.
7aee8047 1883 (f90-do-auto-fill)
748dd5a8 1884 (or no-update (f90-update-line)))
034a9d40
RS
1885 (set-marker pos nil)))
1886
1887(defun f90-indent-new-line ()
a729409a 1888 "Re-indent current line, insert a newline and indent the newline.
87ee2359 1889An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
034a9d40 1890If run in the middle of a line, the line is not broken."
a729409a 1891 (interactive "*")
748dd5a8
GM
1892 (if abbrev-mode (expand-abbrev))
1893 (beginning-of-line) ; reindent where likely to be needed
7aee8047 1894 (f90-indent-line) ; calls indent-line-no, update-line
748dd5a8 1895 (end-of-line)
640f9e26 1896 (delete-horizontal-space) ; destroy trailing whitespace
748dd5a8
GM
1897 (let ((string (f90-in-string))
1898 (cont (f90-line-continued)))
1899 (and string (not cont) (insert "&"))
034a9d40 1900 (newline)
748dd5a8 1901 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
7aee8047 1902 (f90-indent-line 'no-update)) ; nothing to update
034a9d40
RS
1903
1904
784d007b
GM
1905;; TODO not add spaces to empty lines at the start.
1906;; Why is second line getting extra indent over first?
034a9d40
RS
1907(defun f90-indent-region (beg-region end-region)
1908 "Indent every line in region by forward parsing."
1909 (interactive "*r")
748dd5a8 1910 (let ((end-region-mark (copy-marker end-region))
e3f5ce56 1911 (save-point (point-marker))
cbc121c7 1912 (case-fold-search t)
640f9e26 1913 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
034a9d40 1914 (goto-char beg-region)
ec2f376f 1915 ;; First find a line which is not a continuation line or comment.
034a9d40 1916 (beginning-of-line)
ee30478d 1917 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
640f9e26
GM
1918 (progn (f90-indent-line 'no-update)
1919 (zerop (forward-line 1)))
1920 (< (point) end-region-mark)))
034a9d40 1921 (setq cont (f90-present-statement-cont))
f38184aa 1922 (while (and (memq cont '(middle end))
640f9e26 1923 (f90-previous-statement))
034a9d40 1924 (setq cont (f90-present-statement-cont)))
ec2f376f 1925 ;; Process present line for beginning of block.
034a9d40
RS
1926 (setq f90-cache-position (point))
1927 (f90-indent-line 'no-update)
e3f5ce56
GM
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)
640f9e26
GM
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)
5ab33946
GM
1941 ((setq struct (f90-looking-at-associate))
1942 f90-associate-indent)
e93db24a
GM
1943 ((setq struct (f90-looking-at-critical))
1944 f90-critical-indent)
640f9e26 1945 ((or (setq struct (f90-looking-at-program-block-start))
34ba7e3d 1946 (looking-at "contains[ \t]*\\($\\|!\\)"))
640f9e26 1947 f90-program-indent)))
034a9d40
RS
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))
640f9e26 1951 (< (point) end-region-mark))
ec2f376f
GM
1952 (if (looking-at "[ \t]*!")
1953 (f90-indent-to (f90-comment-indent))
748dd5a8
GM
1954 (or (= (current-indentation)
1955 (+ ind-curr f90-continuation-indent))
ec2f376f
GM
1956 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1957 ;; Process all following lines.
d14e6bbe 1958 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
034a9d40
RS
1959 (beginning-of-line)
1960 (f90-indent-line-no)
1961 (setq f90-cache-position (point))
1962 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
640f9e26
GM
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)
5ab33946
GM
1978 ((setq struct (f90-looking-at-associate))
1979 f90-associate-indent)
e93db24a
GM
1980 ((setq struct (f90-looking-at-critical))
1981 f90-critical-indent)
640f9e26
GM
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
599aeab9
GM
1992 (f90-block-match (car beg-struct) (cadr beg-struct)
1993 (car end-struct) (cadr end-struct))))
640f9e26
GM
1994 (setq ind-b
1995 (cond ((looking-at f90-end-if-re) f90-if-indent)
1d5963cc 1996 ((looking-at "end[ \t]*do\\_>") f90-do-indent)
640f9e26 1997 ((looking-at f90-end-type-re) f90-type-indent)
5ab33946
GM
1998 ((looking-at f90-end-associate-re)
1999 f90-associate-indent)
e93db24a 2000 ((f90-looking-at-end-critical) f90-critical-indent)
640f9e26
GM
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)))
ec2f376f 2006 ;; Do the indentation if necessary.
748dd5a8 2007 (or (= ind-curr (current-column))
640f9e26 2008 (f90-indent-to ind-curr))
034a9d40 2009 (while (and (f90-line-continued) (zerop (forward-line 1))
640f9e26 2010 (< (point) end-region-mark))
a0099d31
GM
2011 (cond ((looking-at "[ \t]*#") (f90-indent-to 0))
2012 ((looking-at "[ \t]*!") (f90-indent-to (f90-comment-indent)))
2013 (t
2014 (or (= (current-indentation)
2015 (+ ind-curr f90-continuation-indent))
2016 (f90-indent-to
2017 (+ ind-curr f90-continuation-indent) 'no-line-no))))))
ec2f376f 2018 ;; Restore point, etc.
034a9d40
RS
2019 (setq f90-cache-position nil)
2020 (goto-char save-point)
2021 (set-marker end-region-mark nil)
2022 (set-marker save-point nil)
a445370f 2023 (if (featurep 'xemacs)
640f9e26 2024 (zmacs-deactivate-region)
034a9d40
RS
2025 (deactivate-mark))))
2026
2027(defun f90-indent-subprogram ()
ec2f376f 2028 "Properly indent the subprogram containing point."
a729409a 2029 (interactive "*")
034a9d40 2030 (save-excursion
e3f5ce56 2031 (let ((program (f90-mark-subprogram)))
034a9d40 2032 (if program
640f9e26
GM
2033 (progn
2034 (message "Indenting %s %s..."
599aeab9 2035 (car program) (cadr program))
640f9e26
GM
2036 (indent-region (point) (mark) nil)
2037 (message "Indenting %s %s...done"
599aeab9 2038 (car program) (cadr program)))
640f9e26
GM
2039 (message "Indenting the whole file...")
2040 (indent-region (point) (mark) nil)
2041 (message "Indenting the whole file...done")))))
034a9d40 2042
034a9d40 2043(defun f90-break-line (&optional no-update)
87ee2359
GM
2044 "Break line at point, insert continuation marker(s) and indent.
2045Unless in a string or comment, or if the optional argument NO-UPDATE
2046is non-nil, call `f90-update-line' after inserting the continuation marker."
a729409a 2047 (interactive "*P")
89fa1ef5
GM
2048 (cond ((f90-in-string)
2049 (insert "&\n&"))
2050 ((f90-in-comment)
f84e2fe2 2051 (delete-horizontal-space) ; remove trailing whitespace
89fa1ef5
GM
2052 (insert "\n" (f90-get-present-comment-type)))
2053 (t (insert "&")
2054 (or no-update (f90-update-line))
2055 (newline 1)
f38184aa
GM
2056 ;; FIXME also need leading ampersand if split lexical token (eg ==).
2057 ;; Or respect f90-no-break-re.
89fa1ef5 2058 (if f90-beginning-ampersand (insert "&"))))
84021009 2059 (indent-according-to-mode))
69658465 2060
034a9d40 2061(defun f90-find-breakpoint ()
87ee2359 2062 "From `fill-column', search backward for break-delimiter."
9d4433a2 2063 ;; Commented text more likely than commented code.
f84e2fe2
LM
2064 (if (f90-in-comment)
2065 (re-search-backward "\\s-" (line-beginning-position))
9d4433a2
GM
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)))))
034a9d40 2072
034a9d40 2073(defun f90-do-auto-fill ()
d14e6bbe
GM
2074 "Break line if non-white characters beyond `fill-column'.
2075Update keyword case first."
a729409a 2076 (interactive "*")
ec2f376f 2077 ;; Break line before or after last delimiter (non-word char) if
b974df0a 2078 ;; position is beyond fill-column.
ec2f376f 2079 ;; Will not break **, //, or => (as specified by f90-no-break-re).
7cae52cf 2080 (f90-update-line)
d595e95d
GM
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)))))
b974df0a 2092
a259425b
GM
2093(defun f90-join-lines (&optional arg)
2094 "Join current line to previous, fix whitespace, continuation, comments.
a729409a 2095With optional argument ARG, join current line to following line.
a259425b
GM
2096Like `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)))
034a9d40 2105 (skip-chars-backward " \t")
a259425b
GM
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))))
034a9d40
RS
2112
2113(defun f90-fill-region (beg-region end-region)
d14e6bbe 2114 "Fill every line in region by forward parsing. Join lines if possible."
034a9d40 2115 (interactive "*r")
748dd5a8 2116 (let ((end-region-mark (copy-marker end-region))
e3f5ce56 2117 (go-on t)
640f9e26 2118 f90-smart-end f90-auto-keyword-case auto-fill-function)
034a9d40
RS
2119 (goto-char beg-region)
2120 (while go-on
ec2f376f 2121 ;; Join as much as possible.
a729409a 2122 (while (progn
a259425b
GM
2123 (end-of-line)
2124 (skip-chars-backward " \t")
2125 (eq (preceding-char) ?&))
2126 (f90-join-lines 'forward))
ec2f376f 2127 ;; Chop the line if necessary.
034a9d40 2128 (while (> (save-excursion (end-of-line) (current-column))
640f9e26
GM
2129 fill-column)
2130 (move-to-column fill-column)
2131 (f90-find-breakpoint)
2132 (f90-break-line 'no-update))
748dd5a8 2133 (setq go-on (and (< (point) end-region-mark)
e3f5ce56
GM
2134 (zerop (forward-line 1)))
2135 f90-cache-position (point)))
034a9d40 2136 (setq f90-cache-position nil)
748dd5a8 2137 (set-marker end-region-mark nil)
a445370f 2138 (if (featurep 'xemacs)
640f9e26 2139 (zmacs-deactivate-region)
034a9d40
RS
2140 (deactivate-mark))))
2141\f
7cc8ae06
GM
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.")
2145
034a9d40
RS
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.
ec2f376f
GM
2148BEG-BLOCK is the type of block as indicated at the start (e.g., do).
2149BEG-NAME is the block start name (may be nil).
2150END-BLOCK is the type of block as indicated at the end (may be nil).
2151END-NAME is the block end name (may be nil).
7cc8ae06
GM
2152If the block type matches `f90-end-block-optional-name', do not add
2153an end name if `f90-smart-end-names' is nil, but always update an
2154incorrect end name if there already was one.
034a9d40 2155Leave point at the end of line."
784d007b
GM
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"))
6734e165 2160 (search-forward "end" (line-end-position))
034a9d40 2161 (catch 'no-match
784d007b 2162 (if (and end-block (f90-equal-symbols beg-block end-block))
748dd5a8
GM
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))
7cc8ae06
GM
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))))
748dd5a8
GM
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))))
034a9d40
RS
2188
2189(defun f90-match-end ()
ec2f376f 2190 "From an end block statement, find the corresponding block and name."
034a9d40 2191 (interactive)
748dd5a8
GM
2192 (let ((count 1)
2193 (top-of-window (window-start))
640f9e26 2194 (end-point (point))
748dd5a8 2195 (case-fold-search t)
640f9e26 2196 matching-beg beg-name end-name beg-block end-block end-struct)
5c2a80ad 2197 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
ab09adac
GM
2198 (setq end-struct (f90-looking-at-program-block-end)))
2199 (setq end-block (car end-struct)
599aeab9 2200 end-name (cadr end-struct))
5c2a80ad
GM
2201 (save-excursion
2202 (beginning-of-line)
784d007b
GM
2203 (while (and (> count 0)
2204 (not (= (line-beginning-position) (point-min))))
2205 (re-search-backward f90-blocks-re nil 'move)
e3f5ce56 2206 (beginning-of-line)
748dd5a8
GM
2207 ;; GM not a line number if continued line.
2208;;; (skip-chars-forward " \t")
2209;;; (skip-chars-forward "0-9")
e3f5ce56 2210 (skip-chars-forward " \t0-9")
6dd52caf
GM
2211 (cond ((or (f90-in-string) (f90-in-comment)))
2212 ((setq matching-beg
e3f5ce56
GM
2213 (or
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)
5ab33946 2219 (f90-looking-at-associate)
e93db24a 2220 (f90-looking-at-critical)
784d007b
GM
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))))
e3f5ce56 2227 (setq count (1- count)))
6dd52caf 2228 ((looking-at (concat "end[ \t]*" f90-blocks-re))
e3f5ce56 2229 (setq count (1+ count)))))
6dd52caf 2230 (if (> count 0)
5c2a80ad
GM
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)))
c046af95 2240 (sit-for blink-matching-delay)))
e3f5ce56 2241 (setq beg-block (car matching-beg)
599aeab9 2242 beg-name (cadr matching-beg))
5c2a80ad
GM
2243 (goto-char end-point)
2244 (beginning-of-line)
ab09adac 2245 (f90-block-match beg-block beg-name end-block end-name))))))
034a9d40
RS
2246
2247(defun f90-insert-end ()
87ee2359 2248 "Insert a complete end statement matching beginning of present block."
a729409a 2249 (interactive "*")
e3f5ce56 2250 (let ((f90-smart-end (or f90-smart-end 'blink)))
034a9d40
RS
2251 (insert "end")
2252 (f90-indent-new-line)))
2253\f
ec2f376f 2254;; Abbrevs and keywords.
034a9d40
RS
2255
2256(defun f90-abbrev-start ()
69658465 2257 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
034a9d40 2258Any other key combination is executed normally."
7aee8047 2259 (interactive "*")
df96ab1e
SM
2260 (self-insert-command 1)
2261 (when abbrev-mode
8cd22a08 2262 (set-transient-map
df96ab1e
SM
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))))
034a9d40
RS
2267
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"))
2274
2275(defun f90-prepare-abbrev-list-buffer ()
ec2f376f 2276 "Create a buffer listing the F90 mode abbreviations."
5d16fdd7 2277 (with-current-buffer (get-buffer-create "*Abbrevs*")
034a9d40
RS
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*"))
2284
2285(defun f90-upcase-keywords ()
2286 "Upcase all F90 keywords in the buffer."
a729409a 2287 (interactive "*")
034a9d40
RS
2288 (f90-change-keywords 'upcase-word))
2289
2290(defun f90-capitalize-keywords ()
2291 "Capitalize all F90 keywords in the buffer."
a729409a 2292 (interactive "*")
034a9d40
RS
2293 (f90-change-keywords 'capitalize-word))
2294
2295(defun f90-downcase-keywords ()
2296 "Downcase all F90 keywords in the buffer."
a729409a 2297 (interactive "*")
034a9d40
RS
2298 (f90-change-keywords 'downcase-word))
2299
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))
2304
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))
2309
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))
2314
2315;; Change the keywords according to argument.
2316(defun f90-change-keywords (change-word &optional beg end)
ec2f376f 2317 "Change the case of F90 keywords in the region (if specified) or buffer.
02f85cba 2318CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
034a9d40 2319 (save-excursion
e3f5ce56
GM
2320 (setq beg (or beg (point-min))
2321 end (or end (point-max)))
69658465 2322 (let ((keyword-re
640f9e26
GM
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))
e3f5ce56 2328 state saveword back-point)
034a9d40 2329 (goto-char beg)
ee30478d 2330 (unwind-protect
640f9e26
GM
2331 (while (re-search-forward keyword-re end t)
2332 (unless (progn
5c2a80ad
GM
2333 (setq state (parse-partial-sexp ref-point (point)))
2334 (or (nth 3 state) (nth 4 state)
748dd5a8 2335 ;; GM f90-directive-comment-re?
ec2f376f 2336 (save-excursion ; check for cpp directive
5c2a80ad
GM
2337 (beginning-of-line)
2338 (skip-chars-forward " \t0-9")
2339 (looking-at "#"))))
640f9e26
GM
2340 (setq ref-point (point)
2341 back-point (save-excursion (backward-word 1) (point))
e3f5ce56 2342 saveword (buffer-substring back-point ref-point))
640f9e26
GM
2343 (funcall change-word -1)
2344 (or (string= saveword (buffer-substring back-point ref-point))
2345 (setq modified t))))
5d16fdd7 2346 (or modified (restore-buffer-modified-p nil))))))
034a9d40 2347
d2d15846
DL
2348
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))))
2353
784d007b
GM
2354(defun f90-backslash-not-special (&optional all)
2355 "Make the backslash character (\\) be non-special in the current buffer.
2356With optional argument ALL, change the default for all present
2357and future F90 buffers. F90 mode normally treats backslash as an
2358escape character."
175069ef 2359 (or (derived-mode-p 'f90-mode)
784d007b
GM
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 ?\\ ".")))
2364
2365
034a9d40 2366(provide 'f90)
db97b872 2367
239da61d
GM
2368;; Local Variables:
2369;; coding: utf-8
239da61d
GM
2370;; End:
2371
034a9d40 2372;;; f90.el ends here