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