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