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