* README: Add a note about ranges in copyright years.
[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,
5df4f04c 4;; 2006, 2007, 2008, 2009, 2010, 2011 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)
8a79905d 660 (define-key map "\C-c]" 'f90-insert-end)
70186f7f 661 (define-key map "\C-c\C-w" 'f90-insert-end)
83596348
GM
662 ;; Standard tab binding will call this, and also handle regions.
663;;; (define-key map "\t" 'f90-indent-line)
70186f7f
GM
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)
668 (define-key map "/" 'f90-electric-insert)
a729409a
GM
669
670 (easy-menu-define f90-menu map "Menu for F90 mode."
671 `("F90"
672 ("Customization"
673 ,(custom-menu-create 'f90)
68fcc3ec
GM
674 ;; FIXME useless?
675 ["Set" Custom-set :active t
676 :help "Set current value of all edited settings in the buffer"]
677 ["Save" Custom-save :active t
678 :help "Set and save all edited settings"]
679 ["Reset to Current" Custom-reset-current :active t
680 :help "Reset all edited settings to current"]
681 ["Reset to Saved" Custom-reset-saved :active t
682 :help "Reset all edited or set settings to saved"]
683 ["Reset to Standard Settings" Custom-reset-standard :active t
684 :help "Erase all cusomizations in buffer"]
a729409a
GM
685 )
686 "--"
68fcc3ec
GM
687 ["Indent Subprogram" f90-indent-subprogram t]
688 ["Mark Subprogram" f90-mark-subprogram :active t :help
689 "Mark the end of the current subprogram, move point to the start"]
690 ["Beginning of Subprogram" f90-beginning-of-subprogram :active t
691 :help "Move point to the start of the current subprogram"]
692 ["End of Subprogram" f90-end-of-subprogram :active t
693 :help "Move point to the end of the current subprogram"]
a729409a 694 "--"
68fcc3ec
GM
695 ["(Un)Comment Region" f90-comment-region :active mark-active
696 :help "Comment or uncomment the region"]
697 ["Indent Region" f90-indent-region :active mark-active]
698 ["Fill Region" f90-fill-region :active mark-active
699 :help "Fill long lines in the region"]
a729409a 700 "--"
68fcc3ec
GM
701 ["Break Line at Point" f90-break-line :active t
702 :help "Break the current line at point"]
703 ["Join with Previous Line" f90-join-lines :active t
704 :help "Join the current line to the previous one"]
705 ["Insert Block End" f90-insert-end :active t
706 :help "Insert an end statement for the current code block"]
a729409a
GM
707 "--"
708 ("Highlighting"
68fcc3ec 709 :help "Fontify this buffer to varying degrees"
a729409a 710 ["Toggle font-lock-mode" font-lock-mode :selected font-lock-mode
68fcc3ec 711 :style toggle :help "Fontify text in this buffer"]
a729409a
GM
712 "--"
713 ["Light highlighting (level 1)" f90-font-lock-1 t]
714 ["Moderate highlighting (level 2)" f90-font-lock-2 t]
715 ["Heavy highlighting (level 3)" f90-font-lock-3 t]
716 ["Maximum highlighting (level 4)" f90-font-lock-4 t]
717 )
718 ("Change Keyword Case"
68fcc3ec 719 :help "Change the case of keywords in the buffer or region"
a729409a
GM
720 ["Upcase Keywords (buffer)" f90-upcase-keywords t]
721 ["Capitalize Keywords (buffer)" f90-capitalize-keywords t]
722 ["Downcase Keywords (buffer)" f90-downcase-keywords t]
723 "--"
724 ["Upcase Keywords (region)" f90-upcase-region-keywords
725 mark-active]
726 ["Capitalize Keywords (region)" f90-capitalize-region-keywords
727 mark-active]
728 ["Downcase Keywords (region)" f90-downcase-region-keywords
729 mark-active]
730 )
731 "--"
68fcc3ec
GM
732 ["Toggle Auto Fill" auto-fill-mode :selected auto-fill-function
733 :style toggle
734 :help "Automatically fill text while typing in this buffer"]
735 ["Toggle Abbrev Mode" abbrev-mode :selected abbrev-mode
736 :style toggle :help "Expand abbreviations while typing in this buffer"]
737 ["Add Imenu Menu" f90-add-imenu-menu
a729409a 738 :active (not (lookup-key (current-local-map) [menu-bar index]))
68fcc3ec
GM
739 :included (fboundp 'imenu-add-to-menubar)
740 :help "Add an index menu to the menu-bar"
741 ]))
70186f7f 742 map)
034a9d40 743 "Keymap used in F90 mode.")
ee30478d 744
b974df0a 745
599aeab9
GM
746(defun f90-font-lock-n (n)
747 "Set `font-lock-keywords' to F90 level N keywords."
748 (font-lock-mode 1)
749 (setq font-lock-keywords
750 (symbol-value (intern-soft (format "f90-font-lock-keywords-%d" n))))
751 (font-lock-fontify-buffer))
752
1a341882
GM
753(defun f90-font-lock-1 ()
754 "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
755 (interactive)
599aeab9 756 (f90-font-lock-n 1))
1a341882
GM
757
758(defun f90-font-lock-2 ()
759 "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
760 (interactive)
599aeab9 761 (f90-font-lock-n 2))
1a341882
GM
762
763(defun f90-font-lock-3 ()
764 "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
765 (interactive)
599aeab9 766 (f90-font-lock-n 3))
1a341882
GM
767
768(defun f90-font-lock-4 ()
769 "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
770 (interactive)
599aeab9 771 (f90-font-lock-n 4))
1a341882 772\f
ee30478d 773;; Regexps for finding program structures.
69658465 774(defconst f90-blocks-re
ec2f376f
GM
775 (concat "\\(block[ \t]*data\\|"
776 (regexp-opt '("do" "if" "interface" "function" "module" "program"
5ab33946
GM
777 "select" "subroutine" "type" "where" "forall"
778 ;; F2003.
779 "enum" "associate"))
ec2f376f
GM
780 "\\)\\>")
781 "Regexp potentially indicating a \"block\" of F90 code.")
782
69658465 783(defconst f90-program-block-re
ec2f376f
GM
784 (regexp-opt '("program" "module" "subroutine" "function") 'paren)
785 "Regexp used to locate the start/end of a \"subprogram\".")
786
5ab33946 787;; "class is" is F2003.
69658465 788(defconst f90-else-like-re
5ab33946
GM
789 "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\|\
790\\(class\\|type\\)[ \t]*is[ \t]*(\\|class[ \t]*default\\)"
791 "Regexp matching an ELSE IF, ELSEWHERE, CASE, CLASS/TYPE IS statement.")
ec2f376f 792
69658465 793(defconst f90-end-if-re
ec2f376f
GM
794 (concat "end[ \t]*"
795 (regexp-opt '("if" "select" "where" "forall") 'paren)
796 "\\>")
797 "Regexp matching the end of an IF, SELECT, WHERE, FORALL block.")
798
69658465 799(defconst f90-end-type-re
5ab33946
GM
800 "end[ \t]*\\(type\\|enum\\|interface\\|block[ \t]*data\\)\\>"
801 "Regexp matching the end of a TYPE, ENUM, INTERFACE, BLOCK DATA section.")
802
803(defconst f90-end-associate-re
804 "end[ \t]*associate\\>"
805 "Regexp matching the end of an ASSOCIATE block.")
ec2f376f 806
5ab33946
GM
807;; This is for a TYPE block, not a variable of derived TYPE.
808;; Hence no need to add CLASS for F2003.
ee30478d 809(defconst f90-type-def-re
5ab33946
GM
810 ;; type word
811 ;; type :: word
812 ;; type, stuff :: word
813 ;; NOT "type ("
eb9f0295 814 "\\<\\(type\\)\\>\\(?:[^()\n]*::\\)?[ \t]*\\(\\sw+\\)"
e7272ece 815 "Regexp matching the definition of a derived type.")
ec2f376f 816
5ab33946
GM
817(defconst f90-typeis-re
818 "\\<\\(class\\|type\\)[ \t]*is[ \t]*("
819 "Regexp matching a CLASS/TYPE IS statement.")
820
ec2f376f 821(defconst f90-no-break-re
b0d5b05b 822 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=" "(/" "/)") 'paren)
3f150a60
GM
823 "Regexp specifying two-character tokens not to split when breaking lines.
824Each token has one or more of the characters from `f90-break-delimiters'.
825Note that if only one of the characters is from that variable,
826then the presence of the token here allows a line-break before or
827after the other character, where a break would not normally be
828allowed. This minor issue currently only affects \"(/\" and \"/)\".")
ec2f376f
GM
829
830(defvar f90-cache-position nil
831 "Temporary position used to speed up region operations.")
034a9d40 832(make-variable-buffer-local 'f90-cache-position)
ec2f376f 833
b974df0a 834\f
799dee7a
GM
835;; Hideshow support.
836(defconst f90-end-block-re
fcca5273 837 (concat "^[ \t0-9]*\\<end[ \t]*"
799dee7a 838 (regexp-opt '("do" "if" "forall" "function" "interface"
fcca5273 839 "module" "program" "select" "subroutine"
5ab33946
GM
840 "type" "where" "enum" "associate") t)
841 "\\>")
fcca5273 842 "Regexp matching the end of an F90 \"block\", from the line start.
799dee7a
GM
843Used in the F90 entry in `hs-special-modes-alist'.")
844
845;; Ignore the fact that FUNCTION, SUBROUTINE, WHERE, FORALL have a
fcca5273 846;; following "(". DO, CASE, IF can have labels.
799dee7a
GM
847(defconst f90-start-block-re
848 (concat
849 "^[ \t0-9]*" ; statement number
850 "\\(\\("
851 "\\(\\sw+[ \t]*:[ \t]*\\)?" ; structure label
5ab33946 852 "\\(do\\|select[ \t]*\\(case\\|type\\)\\|"
fcca5273
GM
853 ;; See comments in fortran-start-block-re for the problems of IF.
854 "if[ \t]*(\\(.*\\|"
855 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
799dee7a
GM
856 ;; Distinguish WHERE block from isolated WHERE.
857 "\\(where\\|forall\\)[ \t]*(.*)[ \t]*\\(!\\|$\\)\\)\\)"
858 "\\|"
5ab33946
GM
859 ;; Avoid F2003 "type is" in "select type",
860 ;; and also variables of derived type "type (foo)".
861 ;; "type, foo" must be a block (?).
b1ca7740
GM
862 "type[ \t,]\\("
863 "[^i(!\n\"\& \t]\\|" ; not-i(
864 "i[^s!\n\"\& \t]\\|" ; i not-s
865 "is\\sw\\)\\|"
5ab33946 866 ;; "abstract interface" is F2003.
b1ca7740 867 "program\\|\\(?:abstract[ \t]*\\)?interface\\|module\\|"
5ab33946
GM
868 ;; "enum", but not "enumerator".
869 "function\\|subroutine\\|enum[^e]\\|associate"
799dee7a
GM
870 "\\)"
871 "[ \t]*")
fcca5273 872 "Regexp matching the start of an F90 \"block\", from the line start.
799dee7a
GM
873A simple regexp cannot do this in fully correct fashion, so this
874tries to strike a compromise between complexity and flexibility.
875Used in the F90 entry in `hs-special-modes-alist'.")
876
877;; hs-special-modes-alist is autoloaded.
878(add-to-list 'hs-special-modes-alist
879 `(f90-mode ,f90-start-block-re ,f90-end-block-re
880 "!" f90-end-of-block nil))
881
882\f
ec2f376f 883;; Imenu support.
b1ca7740
GM
884;; FIXME trivial to extend this to enum. Worth it?
885(defun f90-imenu-type-matcher ()
886 "Search backward for the start of a derived type.
887Set subexpression 1 in the match-data to the name of the type."
5d16fdd7 888 (let (found)
b1ca7740
GM
889 (while (and (re-search-backward "^[ \t0-9]*type[ \t]*" nil t)
890 (not (setq found
891 (save-excursion
892 (goto-char (match-end 0))
893 (unless (looking-at "\\(is\\>\\|(\\)")
894 (or (looking-at "\\(\\sw+\\)")
895 (re-search-forward
896 "[ \t]*::[ \t]*\\(\\sw+\\)"
897 (line-end-position) t))))))))
898 found))
899
ee30478d 900(defvar f90-imenu-generic-expression
b974df0a 901 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
b1ca7740 902 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]")
5d16fdd7
SM
903 ;; (not-ib "[^i(!\n\"\& \t]") (not-s "[^s!\n\"\& \t]")
904 )
b974df0a
EN
905 (list
906 '(nil "^[ \t0-9]*program[ \t]+\\(\\sw+\\)" 1)
907 '("Modules" "^[ \t0-9]*module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
b1ca7740
GM
908 (list "Types" 'f90-imenu-type-matcher 1)
909 ;; Does not handle: "type[, stuff] :: foo".
910;;; (format "^[ \t0-9]*type[ \t]+\\(\\(%s\\|i%s\\|is\\sw\\)\\sw*\\)"
911;;; not-ib not-s)
912;;; 1)
913 ;; Can't get the subexpression numbers to match in the two branches.
914;;; (format "^[ \t0-9]*type\\([ \t]*,.*\\(::\\)[ \t]*\\(\\sw+\\)\\|[ \t]+\\(\\(%s\\|i%s\\|is\\sw\\)\\sw*\\)\\)" not-ib not-s)
915;;; 3)
b974df0a 916 (list
69658465 917 "Procedures"
b974df0a
EN
918 (concat
919 "^[ \t0-9]*"
920 "\\("
ec2f376f
GM
921 ;; At least three non-space characters before function/subroutine.
922 ;; Check that the last three non-space characters do not spell E N D.
b974df0a
EN
923 "[^!\"\&\n]*\\("
924 not-e good-char good-char "\\|"
925 good-char not-n good-char "\\|"
926 good-char good-char not-d "\\)"
927 "\\|"
ec2f376f 928 ;; Less than three non-space characters before function/subroutine.
b974df0a
EN
929 good-char "?" good-char "?"
930 "\\)"
931 "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
69658465 932 4)))
a729409a 933 "Value for `imenu-generic-expression' in F90 mode.")
ee30478d 934
b974df0a 935(defun f90-add-imenu-menu ()
b974df0a 936 "Add an imenu menu to the menubar."
87ee2359 937 (interactive)
34ba7e3d 938 (if (lookup-key (current-local-map) [menu-bar index])
5c2a80ad
GM
939 (message "%s" "F90-imenu already exists.")
940 (imenu-add-to-menubar "F90-imenu")
901e8d1d 941 (redraw-frame (selected-frame))))
b974df0a 942
034a9d40 943\f
ec2f376f 944;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
5d16fdd7
SM
945(define-abbrev-table 'f90-mode-abbrev-table
946 (mapcar (lambda (e) (list (car e) (cdr e) nil :system t))
947 '(("`al" . "allocate" )
948 ("`ab" . "allocatable" )
949 ("`ai" . "abstract interface")
950 ("`as" . "assignment" )
951 ("`asy" . "asynchronous" )
952 ("`ba" . "backspace" )
953 ("`bd" . "block data" )
954 ("`c" . "character" )
955 ("`cl" . "close" )
956 ("`cm" . "common" )
957 ("`cx" . "complex" )
958 ("`cn" . "contains" )
959 ("`cy" . "cycle" )
960 ("`de" . "deallocate" )
961 ("`df" . "define" )
962 ("`di" . "dimension" )
963 ("`dp" . "double precision")
964 ("`dw" . "do while" )
965 ("`el" . "else" )
966 ("`eli" . "else if" )
967 ("`elw" . "elsewhere" )
968 ("`em" . "elemental" )
969 ("`e" . "enumerator" )
970 ("`eq" . "equivalence" )
971 ("`ex" . "external" )
972 ("`ey" . "entry" )
973 ("`fl" . "forall" )
974 ("`fo" . "format" )
975 ("`fu" . "function" )
976 ("`fa" . ".false." )
977 ("`im" . "implicit none")
978 ("`in" . "include" )
979 ("`i" . "integer" )
980 ("`it" . "intent" )
981 ("`if" . "interface" )
982 ("`lo" . "logical" )
983 ("`mo" . "module" )
984 ("`na" . "namelist" )
985 ("`nu" . "nullify" )
986 ("`op" . "optional" )
987 ("`pa" . "parameter" )
988 ("`po" . "pointer" )
989 ("`pr" . "print" )
990 ("`pi" . "private" )
991 ("`pm" . "program" )
992 ("`pr" . "protected" )
993 ("`pu" . "public" )
994 ("`r" . "real" )
995 ("`rc" . "recursive" )
996 ("`rt" . "return" )
997 ("`rw" . "rewind" )
998 ("`se" . "select" )
999 ("`sq" . "sequence" )
1000 ("`su" . "subroutine" )
1001 ("`ta" . "target" )
1002 ("`tr" . ".true." )
1003 ("`t" . "type" )
1004 ("`vo" . "volatile" )
1005 ("`wh" . "where" )
1006 ("`wr" . "write" )))
1007 "Abbrev table for F90 mode."
1008 ;; Accept ` as the first char of an abbrev. Also allow _ in abbrevs.
1009 :regexp "\\(?:[^[:word:]_`]\\|^\\)\\(`?[[:word:]_]+\\)[^[:word:]_]*")
0761b294 1010\f
034a9d40 1011;;;###autoload
e75c1e7d 1012(define-derived-mode f90-mode prog-mode "F90"
87ee2359 1013 "Major mode for editing Fortran 90,95 code in free format.
a729409a 1014For fixed format code, use `fortran-mode'.
034a9d40 1015
a729409a 1016\\[f90-indent-line] indents the current line.
ec2f376f 1017\\[f90-indent-new-line] indents current line and creates a new\
034a9d40 1018 indented line.
87ee2359 1019\\[f90-indent-subprogram] indents the current subprogram.
034a9d40
RS
1020
1021Type `? or `\\[help-command] to display a list of built-in\
1022 abbrevs for F90 keywords.
1023
1024Key definitions:
1025\\{f90-mode-map}
1026
1027Variables controlling indentation style and extra features:
1028
ec2f376f
GM
1029`f90-do-indent'
1030 Extra indentation within do blocks (default 3).
1031`f90-if-indent'
5ab33946 1032 Extra indentation within if/select/where/forall blocks (default 3).
ec2f376f 1033`f90-type-indent'
5ab33946 1034 Extra indentation within type/enum/interface/block-data blocks (default 3).
ec2f376f
GM
1035`f90-program-indent'
1036 Extra indentation within program/module/subroutine/function blocks
1037 (default 2).
1038`f90-continuation-indent'
1039 Extra indentation applied to continuation lines (default 5).
1040`f90-comment-region'
e3f5ce56
GM
1041 String inserted by function \\[f90-comment-region] at start of each
1042 line in region (default \"!!!$\").
ec2f376f
GM
1043`f90-indented-comment-re'
1044 Regexp determining the type of comment to be intended like code
1045 (default \"!\").
1046`f90-directive-comment-re'
1047 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
1048 (default \"!hpf\\\\$\").
1049`f90-break-delimiters'
1050 Regexp holding list of delimiters at which lines may be broken
1051 (default \"[-+*/><=,% \\t]\").
1052`f90-break-before-delimiters'
1053 Non-nil causes `f90-do-auto-fill' to break lines before delimiters
1054 (default t).
1055`f90-beginning-ampersand'
1056 Automatic insertion of \& at beginning of continuation lines (default t).
1057`f90-smart-end'
1058 From an END statement, check and fill the end using matching block start.
1059 Allowed values are 'blink, 'no-blink, and nil, which determine
1060 whether to blink the matching beginning (default 'blink).
1061`f90-auto-keyword-case'
1062 Automatic change of case of keywords (default nil).
1063 The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
1064`f90-leave-line-no'
1065 Do not left-justify line numbers (default nil).
034a9d40
RS
1066
1067Turning on F90 mode calls the value of the variable `f90-mode-hook'
1068with no args, if that value is non-nil."
e75c1e7d 1069 :group 'f90
e75c1e7d 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)
e3f5ce56
GM
1073 (set (make-local-variable 'comment-start) "!")
1074 (set (make-local-variable 'comment-start-skip) "!+ *")
1075 (set (make-local-variable 'comment-indent-function) 'f90-comment-indent)
1076 (set (make-local-variable 'abbrev-all-caps) t)
1077 (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
70186f7f 1078 (setq indent-tabs-mode nil) ; auto buffer local
e3f5ce56
GM
1079 (set (make-local-variable 'font-lock-defaults)
1080 '((f90-font-lock-keywords f90-font-lock-keywords-1
1081 f90-font-lock-keywords-2
1082 f90-font-lock-keywords-3
1083 f90-font-lock-keywords-4)
1084 nil t))
45d1e4d4 1085 (set (make-local-variable 'imenu-case-fold-search) t)
e3f5ce56 1086 (set (make-local-variable 'imenu-generic-expression)
a729409a
GM
1087 f90-imenu-generic-expression)
1088 (set (make-local-variable 'beginning-of-defun-function)
1089 'f90-beginning-of-subprogram)
1090 (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
d2d15846 1091 (set (make-local-variable 'add-log-current-defun-function)
e75c1e7d 1092 #'f90-current-defun))
ec2f376f 1093
034a9d40 1094\f
ec2f376f 1095;; Inline-functions.
034a9d40 1096(defsubst f90-in-string ()
d14e6bbe 1097 "Return non-nil if point is inside a string.
ec2f376f 1098Checks from `point-min', or `f90-cache-position', if that is non-nil
d14e6bbe 1099and lies before point."
034a9d40 1100 (let ((beg-pnt
640f9e26
GM
1101 (if (and f90-cache-position (> (point) f90-cache-position))
1102 f90-cache-position
1103 (point-min))))
034a9d40 1104 (nth 3 (parse-partial-sexp beg-pnt (point)))))
69658465 1105
034a9d40 1106(defsubst f90-in-comment ()
d14e6bbe 1107 "Return non-nil if point is inside a comment.
ec2f376f 1108Checks from `point-min', or `f90-cache-position', if that is non-nil
d14e6bbe 1109and lies before point."
034a9d40 1110 (let ((beg-pnt
640f9e26
GM
1111 (if (and f90-cache-position (> (point) f90-cache-position))
1112 f90-cache-position
1113 (point-min))))
034a9d40
RS
1114 (nth 4 (parse-partial-sexp beg-pnt (point)))))
1115
1116(defsubst f90-line-continued ()
d14e6bbe
GM
1117 "Return t if the current line is a continued one.
1118This includes comment lines embedded in continued lines, but
1119not the last line of a continued statement."
034a9d40 1120 (save-excursion
6734e165
GM
1121 (beginning-of-line)
1122 (while (and (looking-at "[ \t]*\\(!\\|$\\)") (zerop (forward-line -1))))
e3f5ce56
GM
1123 (end-of-line)
1124 (while (f90-in-comment)
1125 (search-backward "!" (line-beginning-position))
1126 (skip-chars-backward "!"))
1127 (skip-chars-backward " \t")
1128 (= (preceding-char) ?&)))
034a9d40 1129
748dd5a8
GM
1130;; GM this is not right, eg a continuation line starting with a number.
1131;; Need f90-code-start-position function.
1132;; And yet, things seems to work with this...
7aee8047
GM
1133;; cf f90-indent-line
1134;; (beginning-of-line) ; digits after & \n are not line-nos
1135;; (if (not (save-excursion (and (f90-previous-statement)
1136;; (f90-line-continued))))
1137;; (f90-indent-line-no)
034a9d40
RS
1138(defsubst f90-current-indentation ()
1139 "Return indentation of current line.
1140Line-numbers are considered whitespace characters."
e3f5ce56 1141 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
034a9d40
RS
1142
1143(defsubst f90-indent-to (col &optional no-line-number)
1144 "Indent current line to column COL.
d14e6bbe
GM
1145If optional argument NO-LINE-NUMBER is nil, jump over a possible
1146line-number before indenting."
034a9d40 1147 (beginning-of-line)
748dd5a8 1148 (or no-line-number
034a9d40
RS
1149 (skip-chars-forward " \t0-9"))
1150 (delete-horizontal-space)
748dd5a8
GM
1151 ;; Leave >= 1 space after line number.
1152 (indent-to col (if (zerop (current-column)) 0 1)))
034a9d40 1153
034a9d40 1154(defsubst f90-get-present-comment-type ()
d14e6bbe 1155 "If point lies within a comment, return the string starting the comment.
718d0706
GM
1156For example, \"!\" or \"!!\", followed by the appropriate amount of
1157whitespace, if any."
1158 ;; Include the whitespace for consistent auto-filling of comment blocks.
034a9d40 1159 (save-excursion
e3f5ce56
GM
1160 (when (f90-in-comment)
1161 (beginning-of-line)
718d0706 1162 (re-search-forward "!+[ \t]*" (line-end-position))
e3f5ce56 1163 (while (f90-in-string)
718d0706
GM
1164 (re-search-forward "!+[ \t]*" (line-end-position)))
1165 (match-string-no-properties 0))))
034a9d40
RS
1166
1167(defsubst f90-equal-symbols (a b)
ec2f376f 1168 "Compare strings A and B neglecting case and allowing for nil value."
f14ca250
GM
1169 (equal (if a (downcase a) nil)
1170 (if b (downcase b) nil)))
034a9d40 1171
034a9d40 1172(defsubst f90-looking-at-do ()
d14e6bbe
GM
1173 "Return (\"do\" NAME) if a do statement starts after point.
1174NAME is nil if the statement has no label."
f14ca250 1175 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\>")
0db701f0 1176 (list (match-string 3) (match-string 2))))
ee30478d
KH
1177
1178(defsubst f90-looking-at-select-case ()
5ab33946 1179 "Return (\"select\" NAME) if a select statement starts after point.
d14e6bbe 1180NAME is nil if the statement has no label."
f14ca250 1181 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
5ab33946 1182\\(select\\)[ \t]*\\(case\\|type\\)[ \t]*(")
748dd5a8 1183 (list (match-string 3) (match-string 2))))
034a9d40
RS
1184
1185(defsubst f90-looking-at-if-then ()
d14e6bbe
GM
1186 "Return (\"if\" NAME) if an if () then statement starts after point.
1187NAME is nil if the statement has no label."
034a9d40 1188 (save-excursion
f14ca250
GM
1189 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\>")
1190 (let ((struct (match-string 3))
748dd5a8 1191 (label (match-string 2))
f14ca250
GM
1192 (pos (scan-lists (point) 1 0)))
1193 (and pos (goto-char pos))
5c2a80ad
GM
1194 (skip-chars-forward " \t")
1195 (if (or (looking-at "then\\>")
1196 (when (f90-line-continued)
1197 (f90-next-statement)
1198 (skip-chars-forward " \t0-9&")
1199 (looking-at "then\\>")))
1200 (list struct label))))))
034a9d40 1201
5ab33946
GM
1202;; FIXME label?
1203(defsubst f90-looking-at-associate ()
1204 "Return (\"associate\") if an associate block starts after point."
1205 (if (looking-at "\\<\\(associate\\)[ \t]*(")
1206 (list (match-string 1))))
1207
b32a3d99 1208(defsubst f90-looking-at-where-or-forall ()
d14e6bbe
GM
1209 "Return (KIND NAME) if a where or forall block starts after point.
1210NAME is nil if the statement has no label."
f14ca250
GM
1211 (save-excursion
1212 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
1213\\(where\\|forall\\)\\>")
1214 (let ((struct (match-string 3))
748dd5a8 1215 (label (match-string 2))
f14ca250
GM
1216 (pos (scan-lists (point) 1 0)))
1217 (and pos (goto-char pos))
1218 (skip-chars-forward " \t")
1219 (if (looking-at "\\(!\\|$\\)") (list struct label))))))
034a9d40
RS
1220
1221(defsubst f90-looking-at-type-like ()
5ab33946 1222 "Return (KIND NAME) if a type/enum/interface/block-data starts after point.
7cf3f3d6 1223NAME is non-nil only for type and certain interfaces."
69658465 1224 (cond
5ab33946 1225 ((save-excursion
5fe2902f 1226 (and (looking-at "\\<type\\>[ \t]*")
5ab33946
GM
1227 (goto-char (match-end 0))
1228 (not (looking-at "\\(is\\>\\|(\\)"))
1229 (or (looking-at "\\(\\sw+\\)")
1230 (re-search-forward "[ \t]*::[ \t]*\\(\\sw+\\)"
1231 (line-end-position) t))))
1232 (list "type" (match-string 1)))
1233;;; ((and (not (looking-at f90-typeis-re))
1234;;; (looking-at f90-type-def-re))
1235;;; (list (match-string 1) (match-string 2)))
7cf3f3d6
GM
1236 ((looking-at "\\<\\(interface\\)\\>[ \t]*")
1237 (list (match-string 1)
1238 (save-excursion
1239 (goto-char (match-end 0))
1240 (if (or (looking-at "\\(operator\\|assignment\\|read\\|\
1241write\\)[ \t]*([^)\n]*)")
1242 (looking-at "\\sw+"))
1243 (match-string 0)))))
1244 ((looking-at "\\(enum\\|block[ \t]*data\\)\\>")
5ab33946
GM
1245 (list (match-string 1) nil))
1246 ((looking-at "abstract[ \t]*\\(interface\\)\\>")
6734e165 1247 (list (match-string 1) nil))))
034a9d40
RS
1248
1249(defsubst f90-looking-at-program-block-start ()
d14e6bbe 1250 "Return (KIND NAME) if a program block with name NAME starts after point."
784d007b 1251;;;NAME is nil for an un-named main PROGRAM block."
034a9d40 1252 (cond
ee30478d 1253 ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
6734e165 1254 (list (match-string 1) (match-string 2)))
034a9d40 1255 ((and (not (looking-at "module[ \t]*procedure\\>"))
640f9e26 1256 (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
6734e165 1257 (list (match-string 1) (match-string 2)))
b974df0a 1258 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
640f9e26 1259 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
748dd5a8 1260\\(\\sw+\\)"))
6734e165 1261 (list (match-string 1) (match-string 2)))))
784d007b
GM
1262;; Following will match an un-named main program block; however
1263;; one needs to check if there is an actual PROGRAM statement after
1264;; point (and before any END program). Adding this will require
1265;; change to eg f90-calculate-indent.
1266;;; ((save-excursion
1267;;; (not (f90-previous-statement)))
1268;;; '("program" nil))))
034a9d40
RS
1269
1270(defsubst f90-looking-at-program-block-end ()
d14e6bbe 1271 "Return (KIND NAME) if a block with name NAME ends after point."
7cf3f3d6
GM
1272 (cond ((looking-at "end[ \t]*\\(interface\\)[ \t]*\\(\
1273\\(?:assignment\\|operator\\|read\\|write\\)[ \t]*([^)\n]*)\\)")
1274 (list (match-string 1) (match-string 2)))
1275 ((looking-at (concat "end[ \t]*" f90-blocks-re
1276 "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
1277 (list (match-string 1) (match-string 3)))))
034a9d40
RS
1278
1279(defsubst f90-comment-indent ()
ec2f376f
GM
1280 "Return the indentation to be used for a comment starting at point.
1281Used for `comment-indent-function' by F90 mode.
1282\"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
1283`f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
89fa1ef5 1284All others return `comment-column', leaving at least one space after code."
034a9d40 1285 (cond ((looking-at "!!!") 0)
640f9e26
GM
1286 ((and f90-directive-comment-re
1287 (looking-at f90-directive-comment-re)) 0)
1288 ((looking-at (regexp-quote f90-comment-region)) 0)
1289 ((and (looking-at f90-indented-comment-re)
1290 ;; Don't attempt to indent trailing comment as code.
1291 (save-excursion
1292 (skip-chars-backward " \t")
1293 (bolp)))
1294 (f90-calculate-indent))
1295 (t (save-excursion
b464e0ee
GM
1296 (skip-chars-backward " \t")
1297 (max (if (bolp) 0 (1+ (current-column))) comment-column)))))
034a9d40
RS
1298
1299(defsubst f90-present-statement-cont ()
d14e6bbe
GM
1300 "Return continuation properties of present statement.
1301Possible return values are:
1302single - statement is not continued.
1303begin - current line is the first in a continued statement.
1304end - current line is the last in a continued statement
1305middle - current line is neither first nor last in a continued statement.
1306Comment lines embedded amongst continued lines return 'middle."
034a9d40
RS
1307 (let (pcont cont)
1308 (save-excursion
e3f5ce56 1309 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
034a9d40
RS
1310 (setq cont (f90-line-continued))
1311 (cond ((and (not pcont) (not cont)) 'single)
640f9e26
GM
1312 ((and (not pcont) cont) 'begin)
1313 ((and pcont (not cont)) 'end)
1314 ((and pcont cont) 'middle)
1315 (t (error "The impossible occurred")))))
034a9d40
RS
1316
1317(defsubst f90-indent-line-no ()
d14e6bbe
GM
1318 "If `f90-leave-line-no' is nil, left-justify a line number.
1319Leaves point at the first non-blank character after the line number.
1320Call from beginning of line."
748dd5a8
GM
1321 (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
1322 (delete-horizontal-space))
034a9d40
RS
1323 (skip-chars-forward " \t0-9"))
1324
1325(defsubst f90-no-block-limit ()
d14e6bbe
GM
1326 "Return nil if point is at the edge of a code block.
1327Searches line forward for \"function\" or \"subroutine\",
1328if all else fails."
748dd5a8
GM
1329 (save-excursion
1330 (not (or (looking-at "end")
1331 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
5ab33946
GM
1332\\|select[ \t]*\\(case\\|type\\)\\|case\\|where\\|forall\\)\\>")
1333 (looking-at "\\(program\\|module\\|\
b1ca7740 1334\\(?:abstract[ \t]*\\)?interface\\|block[ \t]*data\\)\\>")
748dd5a8
GM
1335 (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
1336 (looking-at f90-type-def-re)
1337 (re-search-forward "\\(function\\|subroutine\\)"
1338 (line-end-position) t)))))
034a9d40
RS
1339
1340(defsubst f90-update-line ()
d14e6bbe
GM
1341 "Change case of current line as per `f90-auto-keyword-case'."
1342 (if f90-auto-keyword-case
1343 (f90-change-keywords f90-auto-keyword-case
1344 (line-beginning-position) (line-end-position))))
034a9d40 1345\f
d694ccd7 1346(defun f90-electric-insert (&optional arg)
d14e6bbe 1347 "Change keyword case and auto-fill line as operators are inserted."
d694ccd7
GM
1348 (interactive "*p")
1349 (self-insert-command arg)
d14e6bbe
GM
1350 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1351 (f90-update-line)))
1352
055e4eea
GM
1353;; Behave like self-insert-command for delete-selection-mode (bug#5593).
1354(put 'f90-electric-insert 'delete-selection t)
7cae52cf 1355
034a9d40
RS
1356(defun f90-get-correct-indent ()
1357 "Get correct indent for a line starting with line number.
1358Does not check type and subprogram indentation."
6734e165 1359 (let ((epnt (line-end-position)) icol cont)
034a9d40
RS
1360 (save-excursion
1361 (while (and (f90-previous-statement)
f38184aa
GM
1362 (or (memq (setq cont (f90-present-statement-cont))
1363 '(middle end))
640f9e26 1364 (looking-at "[ \t]*[0-9]"))))
034a9d40
RS
1365 (setq icol (current-indentation))
1366 (beginning-of-line)
5c2a80ad
GM
1367 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1368 (line-end-position) t)
e3f5ce56
GM
1369 (beginning-of-line)
1370 (skip-chars-forward " \t")
5c2a80ad
GM
1371 (cond ((f90-looking-at-do)
1372 (setq icol (+ icol f90-do-indent)))
1373 ((or (f90-looking-at-if-then)
1374 (f90-looking-at-where-or-forall)
1375 (f90-looking-at-select-case))
5ab33946
GM
1376 (setq icol (+ icol f90-if-indent)))
1377 ((f90-looking-at-associate)
1378 (setq icol (+ icol f90-associate-indent))))
5c2a80ad 1379 (end-of-line))
034a9d40 1380 (while (re-search-forward
640f9e26
GM
1381 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1382 (beginning-of-line)
e3f5ce56 1383 (skip-chars-forward " \t0-9")
640f9e26 1384 (cond ((f90-looking-at-do)
e3f5ce56
GM
1385 (setq icol (+ icol f90-do-indent)))
1386 ((or (f90-looking-at-if-then)
1387 (f90-looking-at-where-or-forall)
1388 (f90-looking-at-select-case))
1389 (setq icol (+ icol f90-if-indent)))
5ab33946
GM
1390 ((f90-looking-at-associate)
1391 (setq icol (+ icol f90-associate-indent)))
e3f5ce56
GM
1392 ((looking-at f90-end-if-re)
1393 (setq icol (- icol f90-if-indent)))
5ab33946
GM
1394 ((looking-at f90-end-associate-re)
1395 (setq icol (- icol f90-associate-indent)))
e3f5ce56
GM
1396 ((looking-at "end[ \t]*do\\>")
1397 (setq icol (- icol f90-do-indent))))
640f9e26 1398 (end-of-line))
034a9d40 1399 icol)))
69658465 1400
034a9d40
RS
1401(defun f90-calculate-indent ()
1402 "Calculate the indent column based on previous statements."
1403 (interactive)
1404 (let (icol cont (case-fold-search t) (pnt (point)))
1405 (save-excursion
1406 (if (not (f90-previous-statement))
b78cbdf7
GM
1407 ;; If f90-previous-statement returns nil, we must have been
1408 ;; called from on or before the first line of the first statement.
640f9e26 1409 (setq icol (if (save-excursion
b78cbdf7
GM
1410 ;; f90-previous-statement has moved us over
1411 ;; comment/blank lines, so we need to get
1412 ;; back to the first code statement.
1413 (when (looking-at "[ \t]*\\([!#]\\|$\\)")
1414 (f90-next-statement))
1415 (skip-chars-forward " \t0-9")
784d007b
GM
1416 (f90-looking-at-program-block-start))
1417 0
1418 ;; No explicit PROGRAM start statement.
1419 f90-program-indent))
640f9e26
GM
1420 (setq cont (f90-present-statement-cont))
1421 (if (eq cont 'end)
1422 (while (not (eq 'begin (f90-present-statement-cont)))
1423 (f90-previous-statement)))
1424 (cond ((eq cont 'begin)
1425 (setq icol (+ (f90-current-indentation)
1426 f90-continuation-indent)))
1427 ((eq cont 'middle) (setq icol (current-indentation)))
1428 (t (setq icol (f90-current-indentation))
1429 (skip-chars-forward " \t")
1430 (if (looking-at "[0-9]")
1431 (setq icol (f90-get-correct-indent))
1432 (cond ((or (f90-looking-at-if-then)
1433 (f90-looking-at-where-or-forall)
1434 (f90-looking-at-select-case)
1435 (looking-at f90-else-like-re))
1436 (setq icol (+ icol f90-if-indent)))
1437 ((f90-looking-at-do)
1438 (setq icol (+ icol f90-do-indent)))
1439 ((f90-looking-at-type-like)
1440 (setq icol (+ icol f90-type-indent)))
5ab33946
GM
1441 ((f90-looking-at-associate)
1442 (setq icol (+ icol f90-associate-indent)))
640f9e26
GM
1443 ((or (f90-looking-at-program-block-start)
1444 (looking-at "contains[ \t]*\\($\\|!\\)"))
1445 (setq icol (+ icol f90-program-indent)))))
1446 (goto-char pnt)
1447 (beginning-of-line)
1448 (cond ((looking-at "[ \t]*$"))
1449 ((looking-at "[ \t]*#") ; check for cpp directive
1450 (setq icol 0))
1451 (t
1452 (skip-chars-forward " \t0-9")
1453 (cond ((or (looking-at f90-else-like-re)
1454 (looking-at f90-end-if-re))
1455 (setq icol (- icol f90-if-indent)))
1456 ((looking-at "end[ \t]*do\\>")
1457 (setq icol (- icol f90-do-indent)))
1458 ((looking-at f90-end-type-re)
1459 (setq icol (- icol f90-type-indent)))
5ab33946
GM
1460 ((looking-at f90-end-associate-re)
1461 (setq icol (- icol f90-associate-indent)))
640f9e26
GM
1462 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1463 (f90-looking-at-program-block-end))
599aeab9 1464 (setq icol (- icol f90-program-indent))))))))))
034a9d40
RS
1465 icol))
1466\f
034a9d40
RS
1467(defun f90-previous-statement ()
1468 "Move point to beginning of the previous F90 statement.
784d007b
GM
1469If no previous statement is found (i.e. if called from the first
1470statement in the buffer), move to the start of the buffer and
1471return nil. A statement is a line which is neither blank nor a
1472comment."
034a9d40
RS
1473 (interactive)
1474 (let (not-first-statement)
1475 (beginning-of-line)
1476 (while (and (setq not-first-statement (zerop (forward-line -1)))
640f9e26 1477 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
034a9d40
RS
1478 not-first-statement))
1479
1480(defun f90-next-statement ()
1481 "Move point to beginning of the next F90 statement.
1482Return nil if no later statement is found."
1483 (interactive)
1484 (let (not-last-statement)
1485 (beginning-of-line)
1486 (while (and (setq not-last-statement
640f9e26
GM
1487 (and (zerop (forward-line 1))
1488 (not (eobp))))
1489 (looking-at "[ \t0-9]*\\(!\\|$\\)")))
034a9d40
RS
1490 not-last-statement))
1491
1492(defun f90-beginning-of-subprogram ()
76bccf35 1493 "Move point to the beginning of the current subprogram.
ec2f376f 1494Return (TYPE NAME), or nil if not found."
034a9d40
RS
1495 (interactive)
1496 (let ((count 1) (case-fold-search t) matching-beg)
e3f5ce56 1497 (beginning-of-line)
76bccf35 1498 (while (and (> count 0)
640f9e26 1499 (re-search-backward f90-program-block-re nil 'move))
e3f5ce56
GM
1500 (beginning-of-line)
1501 (skip-chars-forward " \t0-9")
1502 (cond ((setq matching-beg (f90-looking-at-program-block-start))
1503 (setq count (1- count)))
1504 ((f90-looking-at-program-block-end)
1505 (setq count (1+ count)))))
034a9d40
RS
1506 (beginning-of-line)
1507 (if (zerop count)
640f9e26 1508 matching-beg
784d007b
GM
1509 ;; Note this includes the case of an un-named main program,
1510 ;; in which case we go to (point-min).
32226619
JB
1511 (if (called-interactively-p 'interactive)
1512 (message "No beginning found"))
034a9d40
RS
1513 nil)))
1514
1515(defun f90-end-of-subprogram ()
76bccf35 1516 "Move point to the end of the current subprogram.
ec2f376f 1517Return (TYPE NAME), or nil if not found."
034a9d40 1518 (interactive)
7aee8047 1519 (let ((case-fold-search t)
575b6746 1520 (count 1)
7aee8047 1521 matching-end)
034a9d40 1522 (end-of-line)
76bccf35 1523 (while (and (> count 0)
640f9e26 1524 (re-search-forward f90-program-block-re nil 'move))
e3f5ce56
GM
1525 (beginning-of-line)
1526 (skip-chars-forward " \t0-9")
034a9d40 1527 (cond ((f90-looking-at-program-block-start)
640f9e26
GM
1528 (setq count (1+ count)))
1529 ((setq matching-end (f90-looking-at-program-block-end))
1530 (setq count (1- count))))
034a9d40 1531 (end-of-line))
6f1d50da
GM
1532 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1533 ;; has a net non-zero effect, which seems odd.
1534;;; (forward-line 1)
034a9d40 1535 (if (zerop count)
640f9e26 1536 matching-end
32226619
JB
1537 (if (called-interactively-p 'interactive)
1538 (message "No end found"))
034a9d40
RS
1539 nil)))
1540
6f1d50da
GM
1541
1542(defun f90-end-of-block (&optional num)
1543 "Move point forward to the end of the current code block.
1544With optional argument NUM, go forward that many balanced blocks.
784d007b
GM
1545If NUM is negative, go backward to the start of a block. Checks
1546for consistency of block types and labels (if present), and
1547completes outermost block if `f90-smart-end' is non-nil.
1548Interactively, pushes mark before moving point."
6f1d50da 1549 (interactive "p")
7938ab41
GM
1550 ;; Can move some distance.
1551 (if (called-interactively-p 'any) (push-mark (point) t))
784d007b
GM
1552 (and num (< num 0) (f90-beginning-of-block (- num)))
1553 (let ((f90-smart-end (if f90-smart-end 'no-blink)) ; for final match-end
6f1d50da
GM
1554 (case-fold-search t)
1555 (count (or num 1))
1556 start-list start-this start-type start-label end-type end-label)
6f1d50da
GM
1557 (end-of-line) ; probably want this
1558 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1559 (beginning-of-line)
1560 (skip-chars-forward " \t0-9")
1561 (cond ((or (f90-in-string) (f90-in-comment)))
1562 ((setq start-this
1563 (or
1564 (f90-looking-at-do)
1565 (f90-looking-at-select-case)
1566 (f90-looking-at-type-like)
5ab33946 1567 (f90-looking-at-associate)
6f1d50da
GM
1568 (f90-looking-at-program-block-start)
1569 (f90-looking-at-if-then)
1570 (f90-looking-at-where-or-forall)))
1571 (setq start-list (cons start-this start-list) ; not add-to-list!
1572 count (1+ count)))
1573 ((looking-at (concat "end[ \t]*" f90-blocks-re
1574 "[ \t]*\\(\\sw+\\)?"))
1575 (setq end-type (match-string 1)
1576 end-label (match-string 2)
1577 count (1- count))
1578 ;; Check any internal blocks.
1579 (when start-list
1580 (setq start-this (car start-list)
1581 start-list (cdr start-list)
1582 start-type (car start-this)
1583 start-label (cadr start-this))
748dd5a8 1584 (or (f90-equal-symbols start-type end-type)
6f1d50da
GM
1585 (error "End type `%s' does not match start type `%s'"
1586 end-type start-type))
748dd5a8 1587 (or (f90-equal-symbols start-label end-label)
6f1d50da
GM
1588 (error "End label `%s' does not match start label `%s'"
1589 end-label start-label)))))
1590 (end-of-line))
76bccf35 1591 (if (> count 0) (error "Missing block end"))
6f1d50da 1592 ;; Check outermost block.
784d007b
GM
1593 (when f90-smart-end
1594 (save-excursion
1595 (beginning-of-line)
1596 (skip-chars-forward " \t0-9")
1597 (f90-match-end)))))
6f1d50da
GM
1598
1599(defun f90-beginning-of-block (&optional num)
1600 "Move point backwards to the start of the current code block.
1601With optional argument NUM, go backward that many balanced blocks.
1602If NUM is negative, go forward to the end of a block.
1603Checks for consistency of block types and labels (if present).
784d007b
GM
1604Does not check the outermost block, because it may be incomplete.
1605Interactively, pushes mark before moving point."
6f1d50da 1606 (interactive "p")
7938ab41 1607 (if (called-interactively-p 'any) (push-mark (point) t))
784d007b 1608 (and num (< num 0) (f90-end-of-block (- num)))
6f1d50da
GM
1609 (let ((case-fold-search t)
1610 (count (or num 1))
748dd5a8
GM
1611 end-list end-this end-type end-label
1612 start-this start-type start-label)
6f1d50da
GM
1613 (beginning-of-line) ; probably want this
1614 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1615 (beginning-of-line)
1616 (skip-chars-forward " \t0-9")
1617 (cond ((or (f90-in-string) (f90-in-comment)))
1618 ((looking-at (concat "end[ \t]*" f90-blocks-re
1619 "[ \t]*\\(\\sw+\\)?"))
1620 (setq end-list (cons (list (match-string 1) (match-string 2))
1621 end-list)
1622 count (1+ count)))
1623 ((setq start-this
1624 (or
1625 (f90-looking-at-do)
1626 (f90-looking-at-select-case)
1627 (f90-looking-at-type-like)
5ab33946 1628 (f90-looking-at-associate)
6f1d50da
GM
1629 (f90-looking-at-program-block-start)
1630 (f90-looking-at-if-then)
1631 (f90-looking-at-where-or-forall)))
1632 (setq start-type (car start-this)
1633 start-label (cadr start-this)
1634 count (1- count))
1635 ;; Check any internal blocks.
1636 (when end-list
1637 (setq end-this (car end-list)
1638 end-list (cdr end-list)
1639 end-type (car end-this)
1640 end-label (cadr end-this))
748dd5a8 1641 (or (f90-equal-symbols start-type end-type)
6f1d50da
GM
1642 (error "Start type `%s' does not match end type `%s'"
1643 start-type end-type))
748dd5a8 1644 (or (f90-equal-symbols start-label end-label)
6f1d50da
GM
1645 (error "Start label `%s' does not match end label `%s'"
1646 start-label end-label))))))
784d007b
GM
1647 ;; Includes an un-named main program block.
1648 (if (> count 0) (error "Missing block start"))))
6f1d50da 1649
76bccf35
GM
1650(defun f90-next-block (&optional num)
1651 "Move point forward to the next end or start of a code block.
1652With optional argument NUM, go forward that many blocks.
1653If NUM is negative, go backwards.
1654A block is a subroutine, if-endif, etc."
6f1d50da 1655 (interactive "p")
76bccf35
GM
1656 (let ((case-fold-search t)
1657 (count (if num (abs num) 1)))
1658 (while (and (> count 0)
1659 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1660 (re-search-backward f90-blocks-re nil 'move)))
6f1d50da
GM
1661 (beginning-of-line)
1662 (skip-chars-forward " \t0-9")
76bccf35
GM
1663 (cond ((or (f90-in-string) (f90-in-comment)))
1664 ((or
1665 (looking-at "end[ \t]*")
1666 (f90-looking-at-do)
1667 (f90-looking-at-select-case)
1668 (f90-looking-at-type-like)
5ab33946 1669 (f90-looking-at-associate)
76bccf35
GM
1670 (f90-looking-at-program-block-start)
1671 (f90-looking-at-if-then)
1672 (f90-looking-at-where-or-forall))
1673 (setq count (1- count))))
1674 (if (> num 0) (end-of-line)
1675 (beginning-of-line)))))
1676
1677
1678(defun f90-previous-block (&optional num)
1679 "Move point backward to the previous end or start of a code block.
1680With optional argument NUM, go backward that many blocks.
1681If NUM is negative, go forwards.
1682A block is a subroutine, if-endif, etc."
6f1d50da 1683 (interactive "p")
76bccf35 1684 (f90-next-block (- (or num 1))))
6f1d50da
GM
1685
1686
034a9d40 1687(defun f90-mark-subprogram ()
a729409a 1688 "Put mark at end of F90 subprogram, point at beginning, push mark."
034a9d40
RS
1689 (interactive)
1690 (let ((pos (point)) program)
1691 (f90-end-of-subprogram)
0ee7f068 1692 (push-mark)
034a9d40
RS
1693 (goto-char pos)
1694 (setq program (f90-beginning-of-subprogram))
a445370f 1695 (if (featurep 'xemacs)
0ee7f068 1696 (zmacs-activate-region)
1bb3ae5c 1697 (setq mark-active t
0ee7f068 1698 deactivate-mark nil))
034a9d40
RS
1699 program))
1700
1701(defun f90-comment-region (beg-region end-region)
1702 "Comment/uncomment every line in the region.
d14e6bbe
GM
1703Insert the variable `f90-comment-region' at the start of every line
1704in the region, or, if already present, remove it."
034a9d40 1705 (interactive "*r")
748dd5a8 1706 (let ((end (copy-marker end-region)))
034a9d40
RS
1707 (goto-char beg-region)
1708 (beginning-of-line)
1709 (if (looking-at (regexp-quote f90-comment-region))
640f9e26 1710 (delete-region (point) (match-end 0))
034a9d40 1711 (insert f90-comment-region))
e3f5ce56 1712 (while (and (zerop (forward-line 1))
640f9e26 1713 (< (point) end))
034a9d40 1714 (if (looking-at (regexp-quote f90-comment-region))
640f9e26
GM
1715 (delete-region (point) (match-end 0))
1716 (insert f90-comment-region)))
034a9d40
RS
1717 (set-marker end nil)))
1718
1719(defun f90-indent-line (&optional no-update)
87ee2359
GM
1720 "Indent current line as F90 code.
1721Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1722after indenting."
a729409a 1723 (interactive "*P")
748dd5a8
GM
1724 (let ((case-fold-search t)
1725 (pos (point-marker))
1726 indent no-line-number)
1727 (beginning-of-line) ; digits after & \n are not line-nos
1728 (if (not (save-excursion (and (f90-previous-statement)
1729 (f90-line-continued))))
1730 (f90-indent-line-no)
1731 (setq no-line-number t)
1732 (skip-chars-forward " \t"))
034a9d40 1733 (if (looking-at "!")
640f9e26 1734 (setq indent (f90-comment-indent))
ab09adac 1735 (and f90-smart-end (looking-at "end")
748dd5a8 1736 (f90-match-end))
034a9d40 1737 (setq indent (f90-calculate-indent)))
748dd5a8 1738 (or (= indent (current-column))
e3f5ce56 1739 (f90-indent-to indent no-line-number))
034a9d40
RS
1740 ;; If initial point was within line's indentation,
1741 ;; position after the indentation. Else stay at same point in text.
748dd5a8
GM
1742 (and (< (point) pos)
1743 (goto-char pos))
69658465 1744 (if auto-fill-function
7aee8047
GM
1745 ;; GM NO-UPDATE not honoured, since this calls f90-update-line.
1746 (f90-do-auto-fill)
748dd5a8 1747 (or no-update (f90-update-line)))
034a9d40
RS
1748 (set-marker pos nil)))
1749
1750(defun f90-indent-new-line ()
a729409a 1751 "Re-indent current line, insert a newline and indent the newline.
87ee2359 1752An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
034a9d40 1753If run in the middle of a line, the line is not broken."
a729409a 1754 (interactive "*")
748dd5a8
GM
1755 (if abbrev-mode (expand-abbrev))
1756 (beginning-of-line) ; reindent where likely to be needed
7aee8047 1757 (f90-indent-line) ; calls indent-line-no, update-line
748dd5a8 1758 (end-of-line)
640f9e26 1759 (delete-horizontal-space) ; destroy trailing whitespace
748dd5a8
GM
1760 (let ((string (f90-in-string))
1761 (cont (f90-line-continued)))
1762 (and string (not cont) (insert "&"))
034a9d40 1763 (newline)
748dd5a8 1764 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
7aee8047 1765 (f90-indent-line 'no-update)) ; nothing to update
034a9d40
RS
1766
1767
784d007b
GM
1768;; TODO not add spaces to empty lines at the start.
1769;; Why is second line getting extra indent over first?
034a9d40
RS
1770(defun f90-indent-region (beg-region end-region)
1771 "Indent every line in region by forward parsing."
1772 (interactive "*r")
748dd5a8 1773 (let ((end-region-mark (copy-marker end-region))
e3f5ce56 1774 (save-point (point-marker))
cbc121c7 1775 (case-fold-search t)
640f9e26 1776 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
034a9d40 1777 (goto-char beg-region)
ec2f376f 1778 ;; First find a line which is not a continuation line or comment.
034a9d40 1779 (beginning-of-line)
ee30478d 1780 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
640f9e26
GM
1781 (progn (f90-indent-line 'no-update)
1782 (zerop (forward-line 1)))
1783 (< (point) end-region-mark)))
034a9d40 1784 (setq cont (f90-present-statement-cont))
f38184aa 1785 (while (and (memq cont '(middle end))
640f9e26 1786 (f90-previous-statement))
034a9d40 1787 (setq cont (f90-present-statement-cont)))
ec2f376f 1788 ;; Process present line for beginning of block.
034a9d40
RS
1789 (setq f90-cache-position (point))
1790 (f90-indent-line 'no-update)
e3f5ce56
GM
1791 (setq ind-lev (f90-current-indentation)
1792 ind-curr ind-lev)
1793 (beginning-of-line)
1794 (skip-chars-forward " \t0-9")
1795 (setq struct nil
1796 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
640f9e26
GM
1797 ((or (setq struct (f90-looking-at-if-then))
1798 (setq struct (f90-looking-at-select-case))
1799 (setq struct (f90-looking-at-where-or-forall))
1800 (looking-at f90-else-like-re))
1801 f90-if-indent)
1802 ((setq struct (f90-looking-at-type-like))
1803 f90-type-indent)
5ab33946
GM
1804 ((setq struct (f90-looking-at-associate))
1805 f90-associate-indent)
640f9e26 1806 ((or (setq struct (f90-looking-at-program-block-start))
34ba7e3d 1807 (looking-at "contains[ \t]*\\($\\|!\\)"))
640f9e26 1808 f90-program-indent)))
034a9d40
RS
1809 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1810 (if struct (setq block-list (cons struct block-list)))
1811 (while (and (f90-line-continued) (zerop (forward-line 1))
640f9e26 1812 (< (point) end-region-mark))
ec2f376f
GM
1813 (if (looking-at "[ \t]*!")
1814 (f90-indent-to (f90-comment-indent))
748dd5a8
GM
1815 (or (= (current-indentation)
1816 (+ ind-curr f90-continuation-indent))
ec2f376f
GM
1817 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1818 ;; Process all following lines.
d14e6bbe 1819 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
034a9d40
RS
1820 (beginning-of-line)
1821 (f90-indent-line-no)
1822 (setq f90-cache-position (point))
1823 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
640f9e26
GM
1824 ((looking-at "[ \t]*#") (setq ind-curr 0))
1825 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1826 ((f90-no-block-limit) (setq ind-curr ind-lev))
1827 ((looking-at f90-else-like-re) (setq ind-curr
1828 (- ind-lev f90-if-indent)))
1829 ((looking-at "contains[ \t]*\\($\\|!\\)")
1830 (setq ind-curr (- ind-lev f90-program-indent)))
1831 ((setq ind-b
1832 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1833 ((or (setq struct (f90-looking-at-if-then))
1834 (setq struct (f90-looking-at-select-case))
1835 (setq struct (f90-looking-at-where-or-forall)))
1836 f90-if-indent)
1837 ((setq struct (f90-looking-at-type-like))
1838 f90-type-indent)
5ab33946
GM
1839 ((setq struct (f90-looking-at-associate))
1840 f90-associate-indent)
640f9e26
GM
1841 ((setq struct (f90-looking-at-program-block-start))
1842 f90-program-indent)))
1843 (setq ind-curr ind-lev)
1844 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1845 (setq block-list (cons struct block-list)))
1846 ((setq end-struct (f90-looking-at-program-block-end))
1847 (setq beg-struct (car block-list)
1848 block-list (cdr block-list))
1849 (if f90-smart-end
1850 (save-excursion
599aeab9
GM
1851 (f90-block-match (car beg-struct) (cadr beg-struct)
1852 (car end-struct) (cadr end-struct))))
640f9e26
GM
1853 (setq ind-b
1854 (cond ((looking-at f90-end-if-re) f90-if-indent)
1855 ((looking-at "end[ \t]*do\\>") f90-do-indent)
1856 ((looking-at f90-end-type-re) f90-type-indent)
5ab33946
GM
1857 ((looking-at f90-end-associate-re)
1858 f90-associate-indent)
640f9e26
GM
1859 ((f90-looking-at-program-block-end)
1860 f90-program-indent)))
1861 (if ind-b (setq ind-lev (- ind-lev ind-b)))
1862 (setq ind-curr ind-lev))
1863 (t (setq ind-curr ind-lev)))
ec2f376f 1864 ;; Do the indentation if necessary.
748dd5a8 1865 (or (= ind-curr (current-column))
640f9e26 1866 (f90-indent-to ind-curr))
034a9d40 1867 (while (and (f90-line-continued) (zerop (forward-line 1))
640f9e26 1868 (< (point) end-region-mark))
ec2f376f
GM
1869 (if (looking-at "[ \t]*!")
1870 (f90-indent-to (f90-comment-indent))
748dd5a8
GM
1871 (or (= (current-indentation)
1872 (+ ind-curr f90-continuation-indent))
ec2f376f
GM
1873 (f90-indent-to
1874 (+ ind-curr f90-continuation-indent) 'no-line-no)))))
1875 ;; Restore point, etc.
034a9d40
RS
1876 (setq f90-cache-position nil)
1877 (goto-char save-point)
1878 (set-marker end-region-mark nil)
1879 (set-marker save-point nil)
a445370f 1880 (if (featurep 'xemacs)
640f9e26 1881 (zmacs-deactivate-region)
034a9d40
RS
1882 (deactivate-mark))))
1883
1884(defun f90-indent-subprogram ()
ec2f376f 1885 "Properly indent the subprogram containing point."
a729409a 1886 (interactive "*")
034a9d40 1887 (save-excursion
e3f5ce56 1888 (let ((program (f90-mark-subprogram)))
034a9d40 1889 (if program
640f9e26
GM
1890 (progn
1891 (message "Indenting %s %s..."
599aeab9 1892 (car program) (cadr program))
640f9e26
GM
1893 (indent-region (point) (mark) nil)
1894 (message "Indenting %s %s...done"
599aeab9 1895 (car program) (cadr program)))
640f9e26
GM
1896 (message "Indenting the whole file...")
1897 (indent-region (point) (mark) nil)
1898 (message "Indenting the whole file...done")))))
034a9d40 1899
034a9d40 1900(defun f90-break-line (&optional no-update)
87ee2359
GM
1901 "Break line at point, insert continuation marker(s) and indent.
1902Unless in a string or comment, or if the optional argument NO-UPDATE
1903is non-nil, call `f90-update-line' after inserting the continuation marker."
a729409a 1904 (interactive "*P")
89fa1ef5
GM
1905 (cond ((f90-in-string)
1906 (insert "&\n&"))
1907 ((f90-in-comment)
718d0706 1908 (delete-horizontal-space 'backwards) ; remove trailing whitespace
89fa1ef5
GM
1909 (insert "\n" (f90-get-present-comment-type)))
1910 (t (insert "&")
1911 (or no-update (f90-update-line))
1912 (newline 1)
f38184aa
GM
1913 ;; FIXME also need leading ampersand if split lexical token (eg ==).
1914 ;; Or respect f90-no-break-re.
89fa1ef5 1915 (if f90-beginning-ampersand (insert "&"))))
84021009 1916 (indent-according-to-mode))
69658465 1917
034a9d40 1918(defun f90-find-breakpoint ()
87ee2359 1919 "From `fill-column', search backward for break-delimiter."
748dd5a8
GM
1920 (re-search-backward f90-break-delimiters (line-beginning-position))
1921 (if (not f90-break-before-delimiters)
1922 (forward-char (if (looking-at f90-no-break-re) 2 1))
1923 (backward-char)
1924 (or (looking-at f90-no-break-re)
0db701f0 1925 (forward-char))))
034a9d40 1926
034a9d40 1927(defun f90-do-auto-fill ()
d14e6bbe
GM
1928 "Break line if non-white characters beyond `fill-column'.
1929Update keyword case first."
a729409a 1930 (interactive "*")
ec2f376f 1931 ;; Break line before or after last delimiter (non-word char) if
b974df0a 1932 ;; position is beyond fill-column.
ec2f376f 1933 ;; Will not break **, //, or => (as specified by f90-no-break-re).
7cae52cf 1934 (f90-update-line)
d595e95d
GM
1935 ;; Need this for `f90-electric-insert' and other f90- callers.
1936 (unless (and (boundp 'comment-auto-fill-only-comments)
1937 comment-auto-fill-only-comments
1938 (not (f90-in-comment)))
1939 (while (> (current-column) fill-column)
1940 (let ((pos-mark (point-marker)))
1941 (move-to-column fill-column)
1942 (or (f90-in-string) (f90-find-breakpoint))
1943 (f90-break-line)
1944 (goto-char pos-mark)
1945 (set-marker pos-mark nil)))))
b974df0a 1946
a259425b
GM
1947(defun f90-join-lines (&optional arg)
1948 "Join current line to previous, fix whitespace, continuation, comments.
a729409a 1949With optional argument ARG, join current line to following line.
a259425b
GM
1950Like `join-line', but handles F90 syntax."
1951 (interactive "*P")
1952 (beginning-of-line)
1953 (if arg (forward-line 1))
1954 (when (eq (preceding-char) ?\n)
1955 (skip-chars-forward " \t")
1956 (if (looking-at "\&") (delete-char 1))
1957 (beginning-of-line)
1958 (delete-region (point) (1- (point)))
034a9d40 1959 (skip-chars-backward " \t")
a259425b
GM
1960 (and (eq (preceding-char) ?&) (delete-char -1))
1961 (and (f90-in-comment)
1962 (looking-at "[ \t]*!+")
1963 (replace-match ""))
1964 (or (f90-in-string)
1965 (fixup-whitespace))))
034a9d40
RS
1966
1967(defun f90-fill-region (beg-region end-region)
d14e6bbe 1968 "Fill every line in region by forward parsing. Join lines if possible."
034a9d40 1969 (interactive "*r")
748dd5a8 1970 (let ((end-region-mark (copy-marker end-region))
e3f5ce56 1971 (go-on t)
640f9e26 1972 f90-smart-end f90-auto-keyword-case auto-fill-function)
034a9d40
RS
1973 (goto-char beg-region)
1974 (while go-on
ec2f376f 1975 ;; Join as much as possible.
a729409a 1976 (while (progn
a259425b
GM
1977 (end-of-line)
1978 (skip-chars-backward " \t")
1979 (eq (preceding-char) ?&))
1980 (f90-join-lines 'forward))
ec2f376f 1981 ;; Chop the line if necessary.
034a9d40 1982 (while (> (save-excursion (end-of-line) (current-column))
640f9e26
GM
1983 fill-column)
1984 (move-to-column fill-column)
1985 (f90-find-breakpoint)
1986 (f90-break-line 'no-update))
748dd5a8 1987 (setq go-on (and (< (point) end-region-mark)
e3f5ce56
GM
1988 (zerop (forward-line 1)))
1989 f90-cache-position (point)))
034a9d40 1990 (setq f90-cache-position nil)
748dd5a8 1991 (set-marker end-region-mark nil)
a445370f 1992 (if (featurep 'xemacs)
640f9e26 1993 (zmacs-deactivate-region)
034a9d40
RS
1994 (deactivate-mark))))
1995\f
1996(defun f90-block-match (beg-block beg-name end-block end-name)
1997 "Match end-struct with beg-struct and complete end-block if possible.
ec2f376f
GM
1998BEG-BLOCK is the type of block as indicated at the start (e.g., do).
1999BEG-NAME is the block start name (may be nil).
2000END-BLOCK is the type of block as indicated at the end (may be nil).
2001END-NAME is the block end name (may be nil).
034a9d40 2002Leave point at the end of line."
784d007b
GM
2003 ;; Hack to deal with the case when this is called from
2004 ;; f90-indent-region on a program block without an explicit PROGRAM
2005 ;; statement at the start. Should really be an error (?).
2006 (or beg-block (setq beg-block "program"))
6734e165 2007 (search-forward "end" (line-end-position))
034a9d40 2008 (catch 'no-match
784d007b 2009 (if (and end-block (f90-equal-symbols beg-block end-block))
748dd5a8
GM
2010 (search-forward end-block)
2011 (if end-block
2012 (progn
2013 (message "END %s does not match %s." end-block beg-block)
2014 (end-of-line)
2015 (throw 'no-match nil))
2016 (message "Inserting %s." beg-block)
2017 (insert (concat " " beg-block))))
2018 (if (f90-equal-symbols beg-name end-name)
2019 (and end-name (search-forward end-name))
2020 (cond ((and beg-name (not end-name))
2021 (message "Inserting %s." beg-name)
2022 (insert (concat " " beg-name)))
2023 ((and beg-name end-name)
2024 (message "Replacing %s with %s." end-name beg-name)
2025 (search-forward end-name)
2026 (replace-match beg-name))
2027 ((and (not beg-name) end-name)
2028 (message "Deleting %s." end-name)
2029 (search-forward end-name)
2030 (replace-match ""))))
2031 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
034a9d40
RS
2032
2033(defun f90-match-end ()
ec2f376f 2034 "From an end block statement, find the corresponding block and name."
034a9d40 2035 (interactive)
748dd5a8
GM
2036 (let ((count 1)
2037 (top-of-window (window-start))
640f9e26 2038 (end-point (point))
748dd5a8 2039 (case-fold-search t)
640f9e26 2040 matching-beg beg-name end-name beg-block end-block end-struct)
5c2a80ad 2041 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
ab09adac
GM
2042 (setq end-struct (f90-looking-at-program-block-end)))
2043 (setq end-block (car end-struct)
599aeab9 2044 end-name (cadr end-struct))
5c2a80ad
GM
2045 (save-excursion
2046 (beginning-of-line)
784d007b
GM
2047 (while (and (> count 0)
2048 (not (= (line-beginning-position) (point-min))))
2049 (re-search-backward f90-blocks-re nil 'move)
e3f5ce56 2050 (beginning-of-line)
748dd5a8
GM
2051 ;; GM not a line number if continued line.
2052;;; (skip-chars-forward " \t")
2053;;; (skip-chars-forward "0-9")
e3f5ce56 2054 (skip-chars-forward " \t0-9")
6dd52caf
GM
2055 (cond ((or (f90-in-string) (f90-in-comment)))
2056 ((setq matching-beg
e3f5ce56
GM
2057 (or
2058 (f90-looking-at-do)
2059 (f90-looking-at-if-then)
2060 (f90-looking-at-where-or-forall)
2061 (f90-looking-at-select-case)
2062 (f90-looking-at-type-like)
5ab33946 2063 (f90-looking-at-associate)
784d007b
GM
2064 (f90-looking-at-program-block-start)
2065 ;; Interpret a single END without a block
2066 ;; start to be the END of a program block
2067 ;; without an initial PROGRAM line.
2068 (if (= (line-beginning-position) (point-min))
2069 '("program" nil))))
e3f5ce56 2070 (setq count (1- count)))
6dd52caf 2071 ((looking-at (concat "end[ \t]*" f90-blocks-re))
e3f5ce56 2072 (setq count (1+ count)))))
6dd52caf 2073 (if (> count 0)
5c2a80ad
GM
2074 (message "No matching beginning.")
2075 (f90-update-line)
2076 (if (eq f90-smart-end 'blink)
2077 (if (< (point) top-of-window)
2078 (message "Matches %s: %s"
2079 (what-line)
2080 (buffer-substring
2081 (line-beginning-position)
2082 (line-end-position)))
c046af95 2083 (sit-for blink-matching-delay)))
e3f5ce56 2084 (setq beg-block (car matching-beg)
599aeab9 2085 beg-name (cadr matching-beg))
5c2a80ad
GM
2086 (goto-char end-point)
2087 (beginning-of-line)
ab09adac 2088 (f90-block-match beg-block beg-name end-block end-name))))))
034a9d40
RS
2089
2090(defun f90-insert-end ()
87ee2359 2091 "Insert a complete end statement matching beginning of present block."
a729409a 2092 (interactive "*")
e3f5ce56 2093 (let ((f90-smart-end (or f90-smart-end 'blink)))
034a9d40
RS
2094 (insert "end")
2095 (f90-indent-new-line)))
2096\f
ec2f376f 2097;; Abbrevs and keywords.
034a9d40
RS
2098
2099(defun f90-abbrev-start ()
69658465 2100 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
034a9d40 2101Any other key combination is executed normally."
7aee8047 2102 (interactive "*")
1ba983e8 2103 (insert last-command-event)
7aee8047
GM
2104 (let (char event)
2105 (if (fboundp 'next-command-event) ; XEmacs
2106 (setq event (next-command-event)
2c948571 2107 char (and (fboundp 'event-to-character)
640f9e26 2108 (event-to-character event)))
7aee8047
GM
2109 (setq event (read-event)
2110 char event))
be550ccc 2111 ;; Insert char if not equal to `?', or if abbrev-mode is off.
f38184aa 2112 (if (and abbrev-mode (memq char (list ?? help-char)))
640f9e26 2113 (f90-abbrev-help)
7aee8047 2114 (setq unread-command-events (list event)))))
034a9d40
RS
2115
2116(defun f90-abbrev-help ()
2117 "List the currently defined abbrevs in F90 mode."
2118 (interactive)
2119 (message "Listing abbrev table...")
2120 (display-buffer (f90-prepare-abbrev-list-buffer))
2121 (message "Listing abbrev table...done"))
2122
2123(defun f90-prepare-abbrev-list-buffer ()
ec2f376f 2124 "Create a buffer listing the F90 mode abbreviations."
5d16fdd7 2125 (with-current-buffer (get-buffer-create "*Abbrevs*")
034a9d40
RS
2126 (erase-buffer)
2127 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
2128 (goto-char (point-min))
2129 (set-buffer-modified-p nil)
2130 (edit-abbrevs-mode))
2131 (get-buffer-create "*Abbrevs*"))
2132
2133(defun f90-upcase-keywords ()
2134 "Upcase all F90 keywords in the buffer."
a729409a 2135 (interactive "*")
034a9d40
RS
2136 (f90-change-keywords 'upcase-word))
2137
2138(defun f90-capitalize-keywords ()
2139 "Capitalize all F90 keywords in the buffer."
a729409a 2140 (interactive "*")
034a9d40
RS
2141 (f90-change-keywords 'capitalize-word))
2142
2143(defun f90-downcase-keywords ()
2144 "Downcase all F90 keywords in the buffer."
a729409a 2145 (interactive "*")
034a9d40
RS
2146 (f90-change-keywords 'downcase-word))
2147
2148(defun f90-upcase-region-keywords (beg end)
2149 "Upcase all F90 keywords in the region."
2150 (interactive "*r")
2151 (f90-change-keywords 'upcase-word beg end))
2152
2153(defun f90-capitalize-region-keywords (beg end)
2154 "Capitalize all F90 keywords in the region."
2155 (interactive "*r")
2156 (f90-change-keywords 'capitalize-word beg end))
2157
2158(defun f90-downcase-region-keywords (beg end)
2159 "Downcase all F90 keywords in the region."
2160 (interactive "*r")
2161 (f90-change-keywords 'downcase-word beg end))
2162
2163;; Change the keywords according to argument.
2164(defun f90-change-keywords (change-word &optional beg end)
ec2f376f 2165 "Change the case of F90 keywords in the region (if specified) or buffer.
02f85cba 2166CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
034a9d40 2167 (save-excursion
e3f5ce56
GM
2168 (setq beg (or beg (point-min))
2169 end (or end (point-max)))
69658465 2170 (let ((keyword-re
640f9e26
GM
2171 (concat "\\("
2172 f90-keywords-re "\\|" f90-procedures-re "\\|"
2173 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
2174 (ref-point (point-min))
2175 (modified (buffer-modified-p))
e3f5ce56 2176 state saveword back-point)
034a9d40 2177 (goto-char beg)
ee30478d 2178 (unwind-protect
640f9e26
GM
2179 (while (re-search-forward keyword-re end t)
2180 (unless (progn
5c2a80ad
GM
2181 (setq state (parse-partial-sexp ref-point (point)))
2182 (or (nth 3 state) (nth 4 state)
748dd5a8 2183 ;; GM f90-directive-comment-re?
ec2f376f 2184 (save-excursion ; check for cpp directive
5c2a80ad
GM
2185 (beginning-of-line)
2186 (skip-chars-forward " \t0-9")
2187 (looking-at "#"))))
640f9e26
GM
2188 (setq ref-point (point)
2189 back-point (save-excursion (backward-word 1) (point))
e3f5ce56 2190 saveword (buffer-substring back-point ref-point))
640f9e26
GM
2191 (funcall change-word -1)
2192 (or (string= saveword (buffer-substring back-point ref-point))
2193 (setq modified t))))
5d16fdd7 2194 (or modified (restore-buffer-modified-p nil))))))
034a9d40 2195
d2d15846
DL
2196
2197(defun f90-current-defun ()
2198 "Function to use for `add-log-current-defun-function' in F90 mode."
2199 (save-excursion
2200 (nth 1 (f90-beginning-of-subprogram))))
2201
784d007b
GM
2202
2203(defun f90-backslash-not-special (&optional all)
2204 "Make the backslash character (\\) be non-special in the current buffer.
2205With optional argument ALL, change the default for all present
2206and future F90 buffers. F90 mode normally treats backslash as an
2207escape character."
175069ef 2208 (or (derived-mode-p 'f90-mode)
784d007b
GM
2209 (error "This function should only be used in F90 buffers"))
2210 (when (equal (char-syntax ?\\ ) ?\\ )
2211 (or all (set-syntax-table (copy-syntax-table (syntax-table))))
2212 (modify-syntax-entry ?\\ ".")))
2213
2214
034a9d40 2215(provide 'f90)
db97b872 2216
034a9d40 2217;;; f90.el ends here