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