Update FSF's address.
[bpt/emacs.git] / lisp / progmodes / f90.el
CommitLineData
be010748 1;;; f90.el --- Fortran-90 mode (free format)
b578f267
EN
2
3;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
034a9d40 4
ee30478d
KH
5;; Author: Torbj\"orn Einarsson <T.Einarsson@clab.ericsson.se>
6;; Created: Sep. 21, 1995
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
034a9d40
RS
28;; Smart mode for editing F90 programs in FREE FORMAT.
29;; Knows about continuation lines, named structured statements, and other
30;; new features in F90 including HPF (High Performance Fortran) structures.
31;; The basic feature is to provide an accurate indentation of F90 programs.
32;; In addition, there are many more features like automatic matching of all
33;; end statements, an auto-fill function to break long lines, a join-lines
34;; function which joins continued lines etc etc.
35;; To facilitate typing, a fairly complete list of abbreviations is provided.
36;; For example, `i is short-hand for integer (if abbrev-mode is on).
37
38;; There are two separate features for highlighting the code.
39;; 1) Upcasing or capitalizing of all keywords.
ee30478d 40;; 2) Colors/fonts using font-lock-mode. (only when using X-windows)
034a9d40
RS
41;; Automatic upcase of downcase of keywords is controlled by the parameter
42;; f90-auto-keyword-case.
034a9d40
RS
43
44;; The indentations of lines starting with ! is determined by the first of the
ee30478d
KH
45;; following matches (the values in the left column are the default values):
46
47;; start-string/regexp indent variable holding start-string/regexp
48;; !!! 0
49;; !hpf\\$ (re) 0 f90-directive-comment-re
50;; !!$ 0 f90-comment-region
51;; ! (re) as code f90-indented-comment-re
52;; default comment-column
53
54;; Ex: Here is the result of 3 different settings of f90-indented-comment-re
55;; f90-indented-comment-re !-indentation !!-indentation
56;; ! as code as code
57;; !! comment-column as code
58;; ![^!] as code comment-column
034a9d40
RS
59;; Trailing comments are indented to comment-column with indent-for-comment M-;
60;; f90-comment-region (C-c;) toggles insertion of f90-comment-region in region.
61
62;; One common convention for free vs. fixed format is that free-format files
63;; have the ending .f90 while the fixed format files have the ending .f.
64;; To make f90-mode work, put this file in, for example, your directory
65;; ~/lisp, and be sure that you have the following in your .emacs-file
66;; (setq load-path (append load-path '("~/lisp")))
67;; (autoload 'f90-mode "f90"
68;; "Major mode for editing Fortran 90 code in free format." t)
69;; (setq auto-mode-alist (append auto-mode-alist
70;; (list '("\\.f90$" . f90-mode))))
71;; Once you have entered f90-mode, you may get more info by using
72;; the command describe-mode (C-h m). For online help describing various
73;; functions use C-h f <Name of function you want described>
74
75;; To customize the f90-mode for your taste, use, for example:
76;; (you don't have to specify values for all the parameters below)
77;;(setq f90-mode-hook
78;; '(lambda () (setq f90-do-indent 3
79;; f90-if-indent 3
80;; f90-type-indent 3
81;; f90-program-indent 2
82;; f90-continuation-indent 5
83;; f90-comment-region "!!$"
ee30478d
KH
84;; f90-directive-comment-re "!hpf\\$"
85;; f90-indented-comment-re "!"
034a9d40
RS
86;; f90-break-delimiters "[-+\\*/,><=% \t]"
87;; f90-break-before-delimiters t
88;; f90-beginning-ampersand t
89;; f90-smart-end 'blink
90;; f90-auto-keyword-case nil
034a9d40 91;; f90-leave-line-no nil
72e80cad
KH
92;; f90-startup-message t
93;; indent-tabs-mode nil
94;; )
034a9d40
RS
95;; ;;The rest is not default.
96;; (abbrev-mode 1) ; turn on abbreviation mode
97;; (f90-auto-fill-mode 1) ; turn on auto-filling
ee30478d 98;; (turn-on-font-lock) ; for highlighting
034a9d40
RS
99;; (if f90-auto-keyword-case ; change case of all keywords on startup
100;; (f90-change-keywords f90-auto-keyword-case))
101;; ))
102;; in your .emacs file (the shown values are the defaults). You can also
103;; change the values of the lists f90-keywords etc.
104;; The auto-fill and abbreviation minor modes are accessible from the menu,
105;; or by using M-x f90-auto-fill-mode and M-x abbrev-mode, respectively.
106
107;; Remarks
108;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
109;; non-nil, the line numbers are never touched.
110;; 2) Multi-; statements like > do i=1,20 ; j=j+i ; end do < are not handled
111;; correctly, but I imagine them to be rare.
ee30478d 112;; 3) Regexps for hilit19 are no longer supported.
034a9d40
RS
113;; 4) For FIXED FORMAT code, use the ordinary fortran mode.
114;; 5) This mode does not work under emacs-18.x.
72e80cad
KH
115;; 6) Preprocessor directives, i.e., lines starting with # are left-justified
116;; and are untouched by all case-changing commands. There is, at present, no
117;; mechanism for treating multi-line directives (continued by \ ).
ee30478d
KH
118;; 7) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
119;; You are urged to use f90-do loops (with labels if you wish).
034a9d40
RS
120
121;; List of user commands
122;; f90-previous-statement f90-next-statement
123;; f90-beginning-of-subprogram f90-end-of-subprogram f90-mark-subprogram
124;; f90-comment-region
125;; f90-indent-line f90-indent-new-line
126;; f90-indent-region (can be called by calling indent-region)
127;; f90-indent-subprogram
128;; f90-break-line f90-join-lines
129;; f90-auto-fill-mode
130;; f90-fill-region
131;; f90-insert-end
132;; f90-upcase-keywords f90-upcase-region-keywords
133;; f90-downcase-keywords f90-downcase-region-keywords
134;; f90-capitalize-keywords f90-capitalize-region-keywords
135
136;; Thanks to all the people who have tested the mode. Special thanks to Jens
137;; Bloch Helmers for encouraging me to write this code, for creative
138;; suggestions as well as for the lists of hpf-commands.
139;; Also thanks to the authors of the fortran and pascal modes, on which some
140;; of this code is built.
141
142;;; Code:
b578f267 143
ee30478d 144(defconst bug-f90-mode "T.Einarsson@clab.ericsson.se"
034a9d40
RS
145 "Address of mailing list for F90 mode bugs.")
146
147;; User options
148(defvar f90-do-indent 3
149 "*Extra indentation applied to DO blocks.")
150
151(defvar f90-if-indent 3
152 "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks.")
153
154(defvar f90-type-indent 3
155 "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks.")
156
157(defvar f90-program-indent 2
158 "*Extra indentation applied to PROGRAM/MODULE/SUBROUTINE/FUNCTION blocks.")
159
160(defvar f90-continuation-indent 5
161 "*Extra indentation applied to F90 continuation lines.")
162
163(defvar f90-comment-region "!!$"
164 "*String inserted by \\[f90-comment-region]\
165 at start of each line in region.")
166
ee30478d
KH
167(defvar f90-indented-comment-re "!"
168 "*Regexp saying which comments to be indented like code.")
034a9d40 169
ee30478d
KH
170(defvar f90-directive-comment-re "!hpf\\$"
171 "*Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented.")
034a9d40
RS
172
173(defvar f90-beginning-ampersand t
174 "*t makes automatic insertion of \& at beginning of continuation line.")
175
176(defvar f90-smart-end 'blink
177 "*From an END statement, check and fill the end using matching block start.
178Allowed values are 'blink, 'no-blink, and nil, which determine
179whether to blink the matching beginning.")
180
181(defvar f90-break-delimiters "[-+\\*/><=,% \t]"
182 "*Regexp holding list of delimiters at which lines may be broken.")
183
184(defvar f90-break-before-delimiters t
185 "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters.")
186
187(defvar f90-auto-keyword-case nil
188 "*Automatic case conversion of keywords.
189 The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil")
190
034a9d40
RS
191(defvar f90-leave-line-no nil
192 "*If nil, left-justify linenumbers.")
193
194(defvar f90-startup-message t
195 "*Non-nil displays a startup message when F90 mode is first called.")
196
ee30478d
KH
197(defconst f90-keywords-re
198 ;;("allocate" "allocatable" "assign" "assignment" "backspace" "block"
199 ;;"call" "case" "character" "close" "common" "complex" "contains"
200 ;;"continue" "cycle" "data" "deallocate" "dimension" "do" "double" "else"
201 ;;"elseif" "elsewhere" "end" "enddo" "endfile" "endif" "entry" "equivalence"
202 ;;"exit" "external" "forall" "format" "function" "goto" "if" "implicit"
203 ;;"include" "inquire" "integer" "intent" "interface" "intrinsic" "logical"
204 ;;"module" "namelist" "none" "nullify" "only" "open" "optional" "parameter"
205 ;;"pause" "pointer" "precision" "print" "private" "procedure" "program"
206 ;;"public" "read" "real" "recursive" "return" "rewind" "save" "select"
207 ;;"sequence" "stop" "subroutine" "target" "then" "type" "use" "where"
208 ;;"while" "write")
209 (concat
210 "\\<\\(a\\(llocat\\(able\\|e\\)\\|ssign\\(\\|ment\\)\\)\\|b\\(ackspace\\|"
211 "lock\\)\\|c\\(a\\(ll\\|se\\)\\|haracter\\|lose\\|o\\(m\\(mon\\|plex\\)\\|"
212 "nt\\(ains\\|inue\\)\\)\\|ycle\\)\\|d\\(ata\\|eallocate\\|imension\\|"
213 "o\\(\\|uble\\)\\)\\|e\\(lse\\(\\|if\\|where\\)\\|n\\(d\\(\\|do\\|file\\|"
214 "if\\)\\|try\\)\\|quivalence\\|x\\(it\\|ternal\\)\\)\\|f\\(or\\(all\\|"
215 "mat\\)\\|unction\\)\\|goto\\|i\\(f\\|mplicit\\|n\\(clude\\|quire\\|t\\("
216 "e\\(ger\\|nt\\|rface\\)\\|rinsic\\)\\)\\)\\|logical\\|module\\|n\\("
217 "amelist\\|one\\|ullify\\)\\|o\\(nly\\|p\\(en\\|tional\\)\\)\\|p\\(a\\("
218 "rameter\\|use\\)\\|ointer\\|r\\(ecision\\|i\\(nt\\|vate\\)\\|o\\("
219 "cedure\\|gram\\)\\)\\|ublic\\)\\|re\\(a[dl]\\|cursive\\|turn\\|wind\\)\\|"
220 "s\\(ave\\|e\\(lect\\|quence\\)\\|top\\|ubroutine\\)\\|t\\(arget\\|hen\\|"
221 "ype\\)\\|use\\|w\\(h\\(ere\\|ile\\)\\|rite\\)\\)\\>")
222 "Regexp for F90 keywords.")
223
224(defconst f90-keywords-level-3-re
225 ;; ("allocate" "allocatable" "assign" "assignment" "backspace" "close"
226 ;; "deallocate" "endfile" "entry" "equivalence" "external" "inquire" "intent"
227 ;; "intrinsic" "only" "open" "optional" "parameter" "pause" "pointer" "print"
228 ;; "private" "public" "read" "recursive" "rewind" "save" "select" "sequence"
229 ;; "target" "write")
230 (concat
231 "\\<\\(a\\(llocat\\(able\\|e\\)\\|ssign\\(\\|ment\\)\\)\\|backspace\\|"
232 "close\\|deallocate\\|e\\(n\\(dfile\\|try\\)\\|quivalence\\|xternal\\)\\|"
233 "in\\(quire\\|t\\(ent\\|rinsic\\)\\)\\|o\\(nly\\|p\\(en\\|tional\\)\\)\\|"
234 "p\\(a\\(rameter\\|use\\)\\|ointer\\|ri\\(nt\\|vate\\)\\|ublic\\)\\|re\\("
235 "ad\\|cursive\\|wind\\)\\|s\\(ave\\|e\\(lect\\|quence\\)\\)\\|target\\|"
236 "write\\)\\>")
237"Keyword-regexp for font-lock level >= 3.")
238
239
240(defconst f90-procedures-re
241 ;; ("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint" "all" "allocated"
242 ;; "anint" "any" "asin" "associated" "atan" "atan2" "bit_size" "btest"
243 ;; "ceiling" "char" "cmplx" "conjg" "cos" "cosh" "count" "cshift"
244 ;; "date_and_time" "dble" "digits" "dim" "dot_product" "dprod" "eoshift"
245 ;; "epsilon" "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
246 ;; "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior" "ishft"
247 ;; "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt" "lle" "llt" "log"
248 ;; "logical" "log10" "matmul" "max" "maxexponent" "maxloc" "maxval" "merge"
249 ;; "min" "minexponent" "minloc" "minval" "mod" "modulo" "mvbits" "nearest"
250 ;; "nint" "not" "pack" "precision" "present" "product" "radix"
251 ;; "random_number" "random_seed" "range" "real" "repeat" "reshape"
252 ;; "rrspacing" "scale" "scan" "selected_int_kind" "selected_real_kind"
253 ;; "set_exponent" "shape" "sign" "sin" "sinh" "size" "spacing" "spread"
254 ;; "sqrt" "sum" "system_clock" "tan" "tanh" "tiny" "transfer" "transpose"
255 ;; "trim" "ubound" "unpack" "verify")
a7acbbe4 256 ;; A left parenthesis to avoid highlighting non-procedures.
ee30478d
KH
257 ;; Real is taken out here to avoid highlighting declarations.
258 (concat
259 "\\<\\(a\\(bs\\|c\\(har\\|os\\)\\|djust[lr]\\|i\\(mag\\|nt\\)\\|ll\\(\\|"
260 "ocated\\)\\|n\\(int\\|y\\)\\|s\\(in\\|sociated\\)\\|tan2?\\)\\|b\\("
261 "it_size\\|test\\)\\|c\\(eiling\\|har\\|mplx\\|o\\(njg\\|sh?\\|unt\\)\\|"
262 "shift\\)\\|d\\(ate_and_time\\|ble\\|i\\(gits\\|m\\)\\|ot_product\\|prod"
263 "\\)\\|e\\(oshift\\|psilon\\|xp\\(\\|onent\\)\\)\\|f\\(loor\\|"
264 "raction\\)\\|huge\\|i\\(a\\(char\\|nd\\)\\|b\\(clr\\|its\\|set\\)\\|"
265 "char\\|eor\\|n\\(dex\\|t\\)\\|or\\|shftc?\\)\\|kind\\|l\\(bound\\|"
266 "en\\(\\|_trim\\)\\|g[et]\\|l[et]\\|og\\(\\|10\\|ical\\)\\)\\|m\\(a\\("
267 "tmul\\|x\\(\\|exponent\\|loc\\|val\\)\\)\\|erge\\|in\\(\\|exponent\\|"
268 "loc\\|val\\)\\|od\\(\\|ulo\\)\\|vbits\\)\\|n\\(earest\\|int\\|ot\\)\\|"
269 "p\\(ack\\|r\\(e\\(cision\\|sent\\)\\|oduct\\)\\)\\|r\\(a\\(dix\\|n\\("
270 "dom_\\(number\\|seed\\)\\|ge\\)\\)\\|e\\(peat\\|shape\\)\\|rspacing\\)\\|"
271 "s\\(ca\\(le\\|n\\)\\|e\\(lected_\\(int_kind\\|real_kind\\)\\|"
272 "t_exponent\\)\\|hape\\|i\\(gn\\|nh?\\|ze\\)\\|p\\(acing\\|read\\)\\|"
273 "qrt\\|um\\|ystem_clock\\)\\|t\\(anh?\\|iny\\|r\\(ans\\(fer\\|pose\\)\\|"
274 "im\\)\\)\\|u\\(bound\\|npack\\)\\|verify\\)[ \t]*(")
275 "Regexp whose first part matches F90 intrinsic procedures.")
276
277(defconst f90-operators-re
278;; "and" "or" "not" "eqv" "neqv" "eq" "ne" "lt" "le" "gt" "ge" "true" "false"
279 (concat
280 "\\.\\(and\\|eqv?\\|false\\|g[et]\\|l[et]\\|n\\(e\\(\\|qv\\)\\|"
281 "ot\\)\\|or\\|true\\)\\.")
282 "Regexp matching intrinsic operators.")
283
284(defconst f90-hpf-keywords-re
285 ;; Intrinsic procedures
286 ;; ("all_prefix" "all_scatter" "all_suffix" "any_prefix" "any_scatter"
287 ;; "any_suffix" "copy_prefix" "copy_scatter" "copy_suffix" "count_prefix"
288 ;; "count_scatter" "count_suffix" "grade_down" "grade_up" "hpf_alignment"
289 ;; "hpf_template" "hpf_distribution" "iall" "iall_prefix" "iall_scatter"
290 ;; "iall_suffix" "iany" "iany_prefix" "iany_scatter" "iany_suffix" "iparity"
291 ;; "iparity_prefix" "iparity_scatter" "iparity_suffix" "leadz"
292 ;; "maxval_prefix" "maxval_scatter" "maxval_suffix" "minval_prefix"
293 ;; "minval_scatter" "minval_suffix" "parity" "parity_prefix"
294 ;; "parity_scatter" "parity_suffix" "popcnt" "poppar" "product_prefix"
295 ;; "product_scatter" "product_suffix" "sum_prefix" "sum_scatter"
296 ;; "sum_suffix" "ilen" "number_of_processors" "processors_shape")
297 ;; Directives
298 ;; ("align" "distribute" "dynamic" "inherit" "template" "processors"
299 ;; "realign" "redistribute" "independent")
300 ;; Keywords
301 ;; ("pure" "extrinsic" "new" "with" "onto" "block" "cyclic")
302 (concat
303 "\\<\\(a\\(l\\(ign\\|l_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|ny_\\("
304 "prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|block\\|c\\(o\\(py_\\(prefix\\|"
305 "s\\(catter\\|uffix\\)\\)\\|unt_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|"
306 "yclic\\)\\|d\\(istribute\\|ynamic\\)\\|extrinsic\\|grade_\\(down\\|"
307 "up\\)\\|hpf_\\(alignment\\|distribution\\|template\\)\\|i\\(a\\(ll\\(\\|"
308 "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|ny\\(\\|_\\(prefix\\|s\\("
309 "catter\\|uffix\\)\\)\\)\\)\\|len\\|n\\(dependent\\|herit\\)\\|parity\\(\\|"
310 "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\)\\|leadz\\|m\\(axval_\\("
311 "prefix\\|s\\(catter\\|uffix\\)\\)\\|inval_\\(prefix\\|s\\(catter\\|"
312 "uffix\\)\\)\\)\\|n\\(ew\\|umber_of_processors\\)\\|onto\\|p\\(arity\\(\\|"
313 "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|op\\(cnt\\|par\\)\\|ro\\("
314 "cessors\\(\\|_shape\\)\\|duct_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|"
315 "ure\\)\\|re\\(align\\|distribute\\)\\|sum_\\(prefix\\|s\\(catter\\|"
316 "uffix\\)\\)\\|template\\|with\\)\\>")
317 "Regexp for all HPF keywords, procedures and directives.")
034a9d40
RS
318
319;; Highlighting patterns
320
ee30478d
KH
321(defvar f90-font-lock-keywords-1
322 (if (string-match "XEmacs" emacs-version)
323 (list ; XEmacs
324 '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>"
325 1 font-lock-keyword-face)
326 '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)"
327 3 font-lock-function-name-face)
328 '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>"
329 1 font-lock-keyword-face)
330 '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)"
331 2 font-lock-function-name-face nil t)
332 ;; Special highlighting of "module procedure foo-list"
333 '("\\<\\(module[ \t]*procedure\\)\\>" 1 font-lock-keyword-face t)
334 ;; Highlight definition of new type
335 '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
336 1 font-lock-keyword-face)
337 '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
338 3 font-lock-function-name-face)
339 "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
340 (list ; Emacs
341 '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)?"
342 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
343 '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
344 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
345 ;; Special highlighting of "module procedure foo-list"
346 '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face t))
347 ;; Highlight definition of new type
348 '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
349 (1 font-lock-keyword-face) (3 font-lock-function-name-face))
350 "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>"))
351 "This does fairly subdued highlighting of comments and function calls.")
352
353(defvar f90-font-lock-keywords-2
354 (append f90-font-lock-keywords-1
355 (if (string-match "XEmacs" emacs-version)
356 (list ; XEmacs
357 ;; Variable declarations (avoid the real function call)
358 '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)"
359 1 font-lock-type-face)
360 '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\(.*\\)"
361 4 font-lock-doc-string-face)
362 ;; do, if and select constructs
363 '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>"
364 1 font-lock-keyword-face)
365 '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)"
366 3 font-lock-doc-string-face)
367 '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
368 2 font-lock-doc-string-face)
369 '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
370 3 font-lock-keyword-face)
371 ;; implicit declaration
372 '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>"
373 1 font-lock-keyword-face)
374 '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>"
375 2 font-lock-type-face)
376 '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
377 1 font-lock-keyword-face)
378 '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)\/"
379 2 font-lock-doc-string-face nil t)
380 '("\\<\\(where\\|forall\\)[ \t]*(" . 1)
381 "\\<e\\(lse\\([ \t]*if\\|where\\)?\\|nd[ \t]*\\(where\\|forall\\)\\)\\>"
382 "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
383 '("\\<\\(exit\\|cycle\\)\\>"
384 1 font-lock-keyword-face)
385 '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)2\\>"
386 2 font-lock-doc-string-face)
387 '("\\<\\(case\\)[ \t]+\\(default\\|(\\)" . 1)
388 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
389 1 font-lock-keyword-face)
390 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
391 2 font-lock-doc-string-face)
392 '("^[ \t]*\\([0-9]+\\)" 1 font-lock-doc-string-face t))
393 (list ; Emacs
394 ;; Variable declarations (avoid the real function call)
395 '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\(.*\\)"
396 (1 font-lock-type-face) (4 font-lock-variable-name-face))
397 ;; do, if and select constructs
398 '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)?"
399 (1 font-lock-keyword-face) (3 font-lock-reference-face nil t))
400 '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
401 (2 font-lock-reference-face nil t) (3 font-lock-keyword-face))
402 ;; implicit declaration
403 '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>" (1 font-lock-keyword-face) (2 font-lock-type-face))
404 '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/" (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
405 '("\\<\\(where\\|forall\\)[ \t]*(" . 1)
406 "\\<e\\(lse\\([ \t]*if\\|where\\)?\\|nd[ \t]*\\(where\\|forall\\)\\)\\>"
407 "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
408 '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>"
409 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
410 '("\\<\\(case\\)[ \t]+\\(default\\|(\\)" . 1)
411 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
412 (1 font-lock-keyword-face) (2 font-lock-reference-face))
413 '("^[ \t]*\\([0-9]+\\)" (1 font-lock-reference-face t)))))
414 "Highlights declarations, do-loops and other constructions")
415
416(defvar f90-font-lock-keywords-3
417 (append f90-font-lock-keywords-2
034a9d40 418 (list
ee30478d
KH
419 f90-keywords-level-3-re
420 f90-operators-re
421 (if (string-match "XEmacs" emacs-version)
422 (append (list f90-procedures-re) '(1 font-lock-keyword-face t))
423 (list f90-procedures-re '(1 font-lock-keyword-face t)))
424 "\\<real\\>" ; Avoid overwriting real defs.
425 ))
426 "Highlights all F90 keywords and intrinsic procedures.")
427
428(defvar f90-font-lock-keywords-4
429 (append f90-font-lock-keywords-3
430 (list f90-hpf-keywords-re))
431 "Highlights all F90 and HPF keywords.")
432
433(defvar f90-font-lock-keywords
434 f90-font-lock-keywords-2
435 "*Default expressions to highlight in F90 mode.")
034a9d40
RS
436
437;; syntax table
438(defvar f90-mode-syntax-table nil
439 "Syntax table in use in F90 mode buffers.")
440
441(if f90-mode-syntax-table
442 ()
443 (setq f90-mode-syntax-table (make-syntax-table))
444 (modify-syntax-entry ?\! "<" f90-mode-syntax-table) ; beg. comment
445 (modify-syntax-entry ?\n ">" f90-mode-syntax-table) ; end comment
446 (modify-syntax-entry ?_ "w" f90-mode-syntax-table) ; underscore in names
447 (modify-syntax-entry ?\' "\"" f90-mode-syntax-table) ; string quote
448 (modify-syntax-entry ?\" "\"" f90-mode-syntax-table) ; string quote
449 (modify-syntax-entry ?\` "w" f90-mode-syntax-table) ; for abbrevs
450 (modify-syntax-entry ?\r " " f90-mode-syntax-table) ; return is whitespace
451 (modify-syntax-entry ?+ "." f90-mode-syntax-table)
452 (modify-syntax-entry ?- "." f90-mode-syntax-table)
453 (modify-syntax-entry ?= "." f90-mode-syntax-table)
454 (modify-syntax-entry ?* "." f90-mode-syntax-table)
455 (modify-syntax-entry ?/ "." f90-mode-syntax-table)
456 (modify-syntax-entry ?\\ "/" f90-mode-syntax-table)) ; escape chars
457
458;; keys
459(defvar f90-mode-map ()
460 "Keymap used in F90 mode.")
ee30478d 461
034a9d40
RS
462(if f90-mode-map
463 ()
464 (setq f90-mode-map (make-sparse-keymap))
465 (define-key f90-mode-map "`" 'f90-abbrev-start)
466 (define-key f90-mode-map "\C-c;" 'f90-comment-region)
467 (define-key f90-mode-map "\C-\M-a" 'f90-beginning-of-subprogram)
468 (define-key f90-mode-map "\C-\M-e" 'f90-end-of-subprogram)
469 (define-key f90-mode-map "\C-\M-h" 'f90-mark-subprogram)
470 (define-key f90-mode-map "\C-\M-q" 'f90-indent-subprogram)
471 (define-key f90-mode-map "\C-j" 'f90-indent-new-line) ; LFD equals C-j
472 (define-key f90-mode-map "\r" 'newline)
473 (define-key f90-mode-map "\C-c\r" 'f90-break-line)
474 ;; (define-key f90-mode-map [M-return] 'f90-break-line)
475 (define-key f90-mode-map "\C-c\C-d" 'f90-join-lines)
476 (define-key f90-mode-map "\C-c\C-f" 'f90-fill-region)
477 (define-key f90-mode-map "\C-c\C-p" 'f90-previous-statement)
478 (define-key f90-mode-map "\C-c\C-n" 'f90-next-statement)
479 (define-key f90-mode-map "\C-c\C-w" 'f90-insert-end)
480 (define-key f90-mode-map "\t" 'f90-indent-line))
ee30478d 481
034a9d40 482;; menus
ee30478d
KH
483(if (string-match "XEmacs" emacs-version)
484 (defvar f90-xemacs-menu
485 '("F90"
486 ["Indent Subprogram" f90-indent-subprogram t]
487 ["Mark Subprogram" f90-mark-subprogram t]
488 ["Beginning of Subprogram" f90-beginning-of-subprogram t]
489 ["End of Subprogram" f90-end-of-subprogram t]
490 "-----"
491 ["(Un)Comment Region" f90-comment-region t]
492 ["Indent Region" indent-region t]
493 ["Fill Region" f90-fill-region t]
494 "-----"
495 ["Break Line at Point" f90-break-line t]
496 ["Join with Next Line" f90-join-lines t]
497 ["Insert Newline" newline t]
498 ["Insert End" f90-insert-end t]
499 "-----"
500 ["Upcase Keywords (buffer)" f90-upcase-keywords t]
501 ["Upcase Keywords (region)" f90-upcase-region-keywords
502 t]
503 ["Capitalize Keywords (buffer)" f90-capitalize-keywords t]
504 ["Capitalize Keywords (region)"
505 f90-capitalize-region-keywords t]
506 ["Downcase Keywords (buffer)" f90-downcase-keywords t]
507 ["Downcase Keywords (region)"
508 f90-downcase-region-keywords t]
509 "-----"
510 ["Toggle abbrev-mode" abbrev-mode t]
511 ["Toggle auto-fill" f90-auto-fill-mode t])
512 "XEmacs menu for F90 mode.")
db97b872 513 ;; Emacs
034a9d40
RS
514 (define-key f90-mode-map [menu-bar] (make-sparse-keymap))
515 (define-key f90-mode-map [menu-bar f90]
516 (cons "F90" (make-sparse-keymap "f90")))
517 (define-key f90-mode-map [menu-bar f90 abbrev-mode]
518 '("Toggle abbrev-mode" . abbrev-mode))
519 (define-key f90-mode-map [menu-bar f90 f90-auto-fill-mode]
520 '("Toggle auto-fill" . f90-auto-fill-mode))
521 (define-key f90-mode-map [menu-bar f90 f90-downcase-region-keywords]
658e4ee5 522 '("Downcase Keywords (region)" . f90-downcase-region-keywords))
034a9d40 523 (define-key f90-mode-map [menu-bar f90 f90-downcase-keywords]
658e4ee5 524 '("Downcase Keywords (buffer)" . f90-downcase-keywords))
034a9d40 525 (define-key f90-mode-map [menu-bar f90 f90-capitalize-keywords]
658e4ee5 526 '("Capitalize Keywords (region)" . f90-capitalize-region-keywords))
034a9d40 527 (define-key f90-mode-map [menu-bar f90 f90-capitalize-region-keywords]
658e4ee5 528 '("Capitalize Keywords (buffer)" . f90-capitalize-keywords))
034a9d40 529 (define-key f90-mode-map [menu-bar f90 f90-upcase-region-keywords]
ee30478d 530 '("Upcase Keywords (region)" . f90-upcase-region-keywords))
034a9d40 531 (define-key f90-mode-map [menu-bar f90 f90-upcase-keywords]
ee30478d 532 '("Upcase Keywords (buffer)" . f90-upcase-keywords))
034a9d40 533 (define-key f90-mode-map [menu-bar f90 f90-insert-end]
658e4ee5 534 '("Insert end" . f90-insert-end))
034a9d40 535 (define-key f90-mode-map [menu-bar f90 f90-join-lines]
658e4ee5 536 '("Join with Next Line" . f90-join-lines))
034a9d40 537 (define-key f90-mode-map [menu-bar f90 f90-break-line]
658e4ee5 538 '("Break Line at Point" . f90-break-line))
034a9d40
RS
539 (define-key f90-mode-map [menu-bar f90 f90-fill-region]
540 '("Fill Region" . f90-fill-region))
541 (define-key f90-mode-map [menu-bar f90 indent-region]
542 '("Indent Region" . indent-region))
543 (define-key f90-mode-map [menu-bar f90 f90-comment-region]
544 '("(Un)Comment Region" . f90-comment-region))
545 (define-key f90-mode-map [menu-bar f90 f90-end-of-subprogram]
546 '("End of Subprogram" . f90-end-of-subprogram))
547 (define-key f90-mode-map [menu-bar f90 f90-beginning-of-subprogram]
548 '("Beginning of Subprogram" . f90-beginning-of-subprogram))
549 (define-key f90-mode-map [menu-bar f90 f90-mark-subprogram]
550 '("Mark Subprogram" . f90-mark-subprogram))
551 (define-key f90-mode-map [menu-bar f90 f90-indent-subprogram]
552 '("Indent Subprogram" . f90-indent-subprogram)))
553
ee30478d 554;; Regexps for finding program structures.
034a9d40
RS
555(defconst f90-blocks-re
556 "\\(block[ \t]*data\\|do\\|if\\|interface\\|function\\|module\\|\
557program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
558(defconst f90-program-block-re
559 "\\(program\\|module\\|subroutine\\|function\\)")
560(defconst f90-else-like-re
ee30478d 561 "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\)\\>")
034a9d40
RS
562(defconst f90-end-if-re
563 "end[ \t]*\\(if\\|select\\|where\\|forall\\)\\>")
564(defconst f90-end-type-re
ee30478d
KH
565 "end[ \t]*\\(type\\|interface\\|block[ \t]*data\\)")
566(defconst f90-type-def-re
567 "\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)")
034a9d40
RS
568(defconst f90-no-break-re "\\(\\*\\*\\|//\\|=>\\)")
569;; A temporary position to make region operators faster
570(defvar f90-cache-position nil)
571(make-variable-buffer-local 'f90-cache-position)
ee30478d
KH
572
573;; Imenu support
574(defvar f90-imenu-generic-expression
575 (cons
576 (concat
577 "^[ \t0-9]*\\("
578 "program[ \t]+\\(\\sw+\\)\\|"
579 "module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)\\|"
580 "\\(recursive[ \t]*\\)?subroutine[ \t]+\\(\\sw+\\)\\|"
581 ; avoid end function, but allow for most other things
582 "\\([^!]*\\([^e!].[^ \t!]\\|.[^n!][^ \t!]\\|..[^d \t!]\\)"
583 "\\|[^!]?[^!]?\\)[ \t]*function[ \t]+\\(\\sw+\\)"
584 "\\)")
585 (list 2 3 6 9))
586 "imenu generic expression for F90 mode.")
587
588;; When compiling under GNU Emacs, load imenu during compilation. If
589;; you have 19.22 or earlier, comment this out, or get imenu.
590(and (fboundp 'eval-when-compile)
591 (eval-when-compile
592 (if (not (string-match "XEmacs" emacs-version))
593 (require 'imenu))
594 ()))
595
596
034a9d40
RS
597\f
598;; abbrevs have generally two letters, except standard types `c, `i, `r, `t
599(defvar f90-mode-abbrev-table nil)
600(if f90-mode-abbrev-table
601 ()
602 (let ((ac abbrevs-changed))
603 (define-abbrev-table 'f90-mode-abbrev-table ())
604 (define-abbrev f90-mode-abbrev-table "`al" "allocate" nil)
ee30478d 605 (define-abbrev f90-mode-abbrev-table "`ab" "allocatable" nil)
034a9d40
RS
606 (define-abbrev f90-mode-abbrev-table "`as" "assignment" nil)
607 (define-abbrev f90-mode-abbrev-table "`ba" "backspace" nil)
608 (define-abbrev f90-mode-abbrev-table "`bd" "block data" nil)
609 (define-abbrev f90-mode-abbrev-table "`c" "character" nil)
610 (define-abbrev f90-mode-abbrev-table "`cl" "close" nil)
611 (define-abbrev f90-mode-abbrev-table "`cm" "common" nil)
612 (define-abbrev f90-mode-abbrev-table "`cx" "complex" nil)
613 (define-abbrev f90-mode-abbrev-table "`cn" "contains" nil)
614 (define-abbrev f90-mode-abbrev-table "`cy" "cycle" nil)
615 (define-abbrev f90-mode-abbrev-table "`de" "deallocate" nil)
616 (define-abbrev f90-mode-abbrev-table "`df" "define" nil)
617 (define-abbrev f90-mode-abbrev-table "`di" "dimension" nil)
618 (define-abbrev f90-mode-abbrev-table "`dw" "do while" nil)
619 (define-abbrev f90-mode-abbrev-table "`el" "else" nil)
620 (define-abbrev f90-mode-abbrev-table "`eli" "else if" nil)
621 (define-abbrev f90-mode-abbrev-table "`elw" "elsewhere" nil)
622 (define-abbrev f90-mode-abbrev-table "`eq" "equivalence" nil)
623 (define-abbrev f90-mode-abbrev-table "`ex" "external" nil)
624 (define-abbrev f90-mode-abbrev-table "`ey" "entry" nil)
625 (define-abbrev f90-mode-abbrev-table "`fl" "forall" nil)
626 (define-abbrev f90-mode-abbrev-table "`fo" "format" nil)
627 (define-abbrev f90-mode-abbrev-table "`fu" "function" nil)
628 (define-abbrev f90-mode-abbrev-table "`fa" ".false." nil)
629 (define-abbrev f90-mode-abbrev-table "`im" "implicit none" nil)
630 (define-abbrev f90-mode-abbrev-table "`in " "include" nil)
631 (define-abbrev f90-mode-abbrev-table "`i" "integer" nil)
632 (define-abbrev f90-mode-abbrev-table "`it" "intent" nil)
633 (define-abbrev f90-mode-abbrev-table "`if" "interface" nil)
634 (define-abbrev f90-mode-abbrev-table "`lo" "logical" nil)
635 (define-abbrev f90-mode-abbrev-table "`mo" "module" nil)
636 (define-abbrev f90-mode-abbrev-table "`na" "namelist" nil)
637 (define-abbrev f90-mode-abbrev-table "`nu" "nullify" nil)
638 (define-abbrev f90-mode-abbrev-table "`op" "optional" nil)
639 (define-abbrev f90-mode-abbrev-table "`pa" "parameter" nil)
640 (define-abbrev f90-mode-abbrev-table "`po" "pointer" nil)
641 (define-abbrev f90-mode-abbrev-table "`pr" "print" nil)
642 (define-abbrev f90-mode-abbrev-table "`pi" "private" nil)
643 (define-abbrev f90-mode-abbrev-table "`pm" "program" nil)
644 (define-abbrev f90-mode-abbrev-table "`pu" "public" nil)
645 (define-abbrev f90-mode-abbrev-table "`r" "real" nil)
646 (define-abbrev f90-mode-abbrev-table "`rc" "recursive" nil)
647 (define-abbrev f90-mode-abbrev-table "`rt" "return" nil)
648 (define-abbrev f90-mode-abbrev-table "`rw" "rewind" nil)
649 (define-abbrev f90-mode-abbrev-table "`se" "select" nil)
650 (define-abbrev f90-mode-abbrev-table "`sq" "sequence" nil)
651 (define-abbrev f90-mode-abbrev-table "`su" "subroutine" nil)
652 (define-abbrev f90-mode-abbrev-table "`ta" "target" nil)
653 (define-abbrev f90-mode-abbrev-table "`tr" ".true." nil)
654 (define-abbrev f90-mode-abbrev-table "`t" "type" nil)
655 (define-abbrev f90-mode-abbrev-table "`wh" "where" nil)
656 (define-abbrev f90-mode-abbrev-table "`wr" "write" nil)
657 (setq abbrevs-changed ac)))
658\f
659;;;###autoload
660(defun f90-mode ()
661 "Major mode for editing Fortran 90 code in free format.
662
663\\[f90-indent-new-line] corrects current indentation and creates new\
664 indented line.
665\\[f90-indent-line] indents the current line correctly.
666\\[f90-indent-subprogram] indents the current subprogram.
667
668Type `? or `\\[help-command] to display a list of built-in\
669 abbrevs for F90 keywords.
670
671Key definitions:
672\\{f90-mode-map}
673
674Variables controlling indentation style and extra features:
675
676 f90-do-indent
677 Extra indentation within do blocks. (default 3)
678 f90-if-indent
679 Extra indentation within if/select case/where/forall blocks. (default 3)
680 f90-type-indent
681 Extra indentation within type/interface/block-data blocks. (default 3)
682 f90-program-indent
683 Extra indentation within program/module/subroutine/function blocks.
684 (default 2)
685 f90-continuation-indent
686 Extra indentation applied to continuation lines. (default 5)
687 f90-comment-region
688 String inserted by \\[f90-comment-region] at start of each line in
689 region. (default \"!!!$\")
ee30478d
KH
690 f90-indented-comment-re
691 Regexp determining the type of comment to be intended like code.
692 (default \"!\")
693 f90-directive-comment-re
694 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented.
695 (default \"!hpf\\\\$\")
034a9d40
RS
696 f90-break-delimiters
697 Regexp holding list of delimiters at which lines may be broken.
698 (default \"[-+*/><=,% \\t]\")
699 f90-break-before-delimiters
700 Non-nil causes `f90-do-auto-fill' to break lines before delimiters.
701 (default t)
702 f90-beginning-ampersand
703 Automatic insertion of \& at beginning of continuation lines. (default t)
704 f90-smart-end
705 From an END statement, check and fill the end using matching block start.
706 Allowed values are 'blink, 'no-blink, and nil, which determine
707 whether to blink the matching beginning.) (default 'blink)
708 f90-auto-keyword-case
709 Automatic change of case of keywords. (default nil)
710 The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
034a9d40
RS
711 f90-leave-line-no
712 Do not left-justify line numbers. (default nil)
713 f90-startup-message
714 Set to nil to inhibit message first time F90 mode is used. (default t)
715 f90-keywords
716 List of keywords used for highlighting/upcase-keywords etc.
717
718Turning on F90 mode calls the value of the variable `f90-mode-hook'
719with no args, if that value is non-nil."
720 (interactive)
721 (kill-all-local-variables)
722 (setq major-mode 'f90-mode)
723 (setq mode-name "F90")
724 (setq local-abbrev-table f90-mode-abbrev-table)
725 (set-syntax-table f90-mode-syntax-table)
726 (use-local-map f90-mode-map)
727 (make-local-variable 'indent-line-function)
728 (setq indent-line-function 'f90-indent-line)
729 (make-local-variable 'indent-region-function)
730 (setq indent-region-function 'f90-indent-region)
731 (make-local-variable 'require-final-newline)
732 (setq require-final-newline t)
733 (make-local-variable 'comment-start)
734 (setq comment-start "!")
735 (make-local-variable 'comment-start-skip)
736 (setq comment-start-skip "!+ *")
737 (make-local-variable 'comment-indent-function)
738 (setq comment-indent-function 'f90-comment-indent)
739 (make-local-variable 'abbrev-all-caps)
740 (setq abbrev-all-caps t)
72e80cad 741 (setq indent-tabs-mode nil)
034a9d40 742 ;; Setting up things for font-lock
ee30478d
KH
743 (if (string-match "Xemacs" emacs-version)
744 (progn
745 (put 'f90-mode 'font-lock-keywords-case-fold-search t)
746 (if (and current-menubar
747 (not (assoc "F90" current-menubar)))
748 (progn
749 (set-buffer-menubar (copy-sequence current-menubar))
750 (add-submenu nil f90-xemacs-menu)))
751 (make-local-variable 'font-lock-keywords)
752 (setq font-lock-keywords f90-font-lock-keywords))
753 ;; Emacs
754 (make-local-variable 'font-lock-defaults)
755 (setq font-lock-defaults
756 '((f90-font-lock-keywords f90-font-lock-keywords-1
757 f90-font-lock-keywords-2
758 f90-font-lock-keywords-3
759 f90-font-lock-keywords-4)
760 nil t))
761 ;; Tell imenu how to handle f90.
762 (make-local-variable 'imenu-generic-expression)
763 (setq imenu-generic-expression f90-imenu-generic-expression))
034a9d40
RS
764 (run-hooks 'f90-mode-hook)
765 (if f90-startup-message
c04f7d65 766 (message "Emacs F90 mode; please report bugs to %s" bug-f90-mode))
034a9d40
RS
767 (setq f90-startup-message nil))
768\f
769;; inline-functions
770(defsubst f90-get-beg-of-line ()
771 (save-excursion (beginning-of-line) (point)))
772
773(defsubst f90-get-end-of-line ()
774 (save-excursion (end-of-line) (point)))
775
776(defsubst f90-in-string ()
777 (let ((beg-pnt
778 (if (and f90-cache-position (> (point) f90-cache-position))
779 f90-cache-position
780 (point-min))))
781 (nth 3 (parse-partial-sexp beg-pnt (point)))))
782
783(defsubst f90-in-comment ()
784 (let ((beg-pnt
785 (if (and f90-cache-position (> (point) f90-cache-position))
786 f90-cache-position
787 (point-min))))
788 (nth 4 (parse-partial-sexp beg-pnt (point)))))
789
790(defsubst f90-line-continued ()
791 (save-excursion
792 (let ((bol (f90-get-beg-of-line)))
793 (end-of-line)
794 (while (f90-in-comment)
795 (search-backward "!" bol)
796 (skip-chars-backward "!"))
797 (skip-chars-backward " \t")
798 (= (preceding-char) ?&))))
799
800(defsubst f90-current-indentation ()
801 "Return indentation of current line.
802Line-numbers are considered whitespace characters."
803 (save-excursion
804 (beginning-of-line) (skip-chars-forward " \t0-9")
805 (current-column)))
806
807(defsubst f90-indent-to (col &optional no-line-number)
808 "Indent current line to column COL.
809If no-line-number nil, jump over a possible line-number."
810 (beginning-of-line)
811 (if (not no-line-number)
812 (skip-chars-forward " \t0-9"))
813 (delete-horizontal-space)
814 (if (zerop (current-column))
815 (indent-to col)
816 (indent-to col 1)))
817
818(defsubst f90-match-piece (arg)
819 (if (match-beginning arg)
820 (buffer-substring (match-beginning arg) (match-end arg))))
821
822(defsubst f90-get-present-comment-type ()
823 (save-excursion
824 (let ((type nil) (eol (f90-get-end-of-line)))
825 (if (f90-in-comment)
826 (progn
827 (beginning-of-line)
828 (re-search-forward "[!]+" eol)
829 (while (f90-in-string)
830 (re-search-forward "[!]+" eol))
831 (setq type (buffer-substring (match-beginning 0) (match-end 0)))))
832 type)))
833
834(defsubst f90-equal-symbols (a b)
835 "Compare strings neglecting case and allowing for nil value."
836 (let ((a-local (if a (downcase a) nil))
837 (b-local (if b (downcase b) nil)))
838 (equal a-local b-local)))
839
ee30478d
KH
840;; XEmacs 19.11 & 19.12 gives back a single char when matching an empty regular
841;; expression. Therefore, the next 2 functions are longer than necessary.
034a9d40
RS
842
843(defsubst f90-looking-at-do ()
844 "Return (\"do\" name) if a do statement starts after point.
845Name is nil if the statement has no label."
ee30478d
KH
846 (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(do\\)\\>")
847 (let (label
848 (struct (f90-match-piece 3)))
849 (if (looking-at "\\(\\sw+\\)[ \t]*\:")
850 (setq label (f90-match-piece 1)))
851 (list struct label))))
852
853(defsubst f90-looking-at-select-case ()
854 "Return (\"select\" name) if a select-case statement starts after point.
855Name is nil if the statement has no label."
856 (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(select\\)[ \t]*case[ \t]*(")
857 (let (label
858 (struct (f90-match-piece 3)))
859 (if (looking-at "\\(\\sw+\\)[ \t]*\:")
860 (setq label (f90-match-piece 1)))
861 (list struct label))))
034a9d40
RS
862
863(defsubst f90-looking-at-if-then ()
864 "Return (\"if\" name) if an if () then statement starts after point.
865Name is nil if the statement has no label."
866 (save-excursion
867 (let (struct (label nil))
ee30478d 868 (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(if\\)\\>")
034a9d40
RS
869 (progn
870 (setq struct (f90-match-piece 3))
ee30478d 871 (if (looking-at "\\(\\sw+\\)[ \t]*\:")
034a9d40
RS
872 (setq label (f90-match-piece 1)))
873 (goto-char (scan-lists (point) 1 0))
874 (skip-chars-forward " \t")
ee30478d 875 (if (or (looking-at "then\\>")
034a9d40
RS
876 (if (f90-line-continued)
877 (progn
878 (f90-next-statement)
879 (skip-chars-forward " \t0-9&")
ee30478d 880 (looking-at "then\\>"))))
034a9d40
RS
881 (list struct label)))))))
882
034a9d40
RS
883(defsubst f90-looking-at-where-or-forall ()
884 "Return (kind nil) if where/forall...end starts after point."
885 (save-excursion
886 (let (command)
887 (if (looking-at "\\(where\\|forall\\)[ \t]*(")
888 (progn
889 (setq command (list (f90-match-piece 1) nil))
890 (goto-char (scan-lists (point) 1 0))
891 (skip-chars-forward " \t")
892 (if (looking-at "\\(!\\|$\\)")
893 command))))))
894
895(defsubst f90-looking-at-type-like ()
896 "Return (kind name) at the start of a type/interface/block-data block.
897Name is non-nil only for type."
898 (cond
ee30478d 899 ((looking-at f90-type-def-re)
72e80cad 900 (list (f90-match-piece 1) (f90-match-piece 3)))
ee30478d 901 ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
034a9d40
RS
902 (list (f90-match-piece 1) nil))))
903
904(defsubst f90-looking-at-program-block-start ()
905 "Return (kind name) if a program block with name name starts after point."
906 (cond
ee30478d 907 ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
034a9d40
RS
908 (list (f90-match-piece 1) (f90-match-piece 2)))
909 ((and (not (looking-at "module[ \t]*procedure\\>"))
ee30478d 910 (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
034a9d40 911 (list (f90-match-piece 1) (f90-match-piece 2)))
ee30478d 912 ((looking-at "\\(recursive[ \t]*\\)?\\(subroutine\\)[ \t]+\\(\\sw+\\)")
034a9d40 913 (list (f90-match-piece 2) (f90-match-piece 3)))
ee30478d
KH
914 ((and (not (looking-at "end[ \t]*function"))
915 (looking-at ".*\\(function\\)[ \t]+\\(\\sw+\\)"))
034a9d40
RS
916 (list (f90-match-piece 1) (f90-match-piece 2)))))
917
918(defsubst f90-looking-at-program-block-end ()
919 "Return list of type and name of end of block."
ee30478d
KH
920 (if (looking-at (concat "end[ \t]*" f90-blocks-re
921 "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
034a9d40
RS
922 (list (f90-match-piece 1) (f90-match-piece 3))))
923
924(defsubst f90-comment-indent ()
925 (cond ((looking-at "!!!") 0)
ee30478d
KH
926 ((and f90-directive-comment-re
927 (looking-at f90-directive-comment-re)) 0)
034a9d40 928 ((looking-at (regexp-quote f90-comment-region)) 0)
ee30478d 929 ((looking-at f90-indented-comment-re)
034a9d40
RS
930 (f90-calculate-indent))
931 (t (skip-chars-backward " \t")
932 (max (if (bolp) 0 (1+ (current-column))) comment-column))))
933
934(defsubst f90-present-statement-cont ()
935 "Return continuation properties of present statement."
936 (let (pcont cont)
937 (save-excursion
938 (setq pcont (if (f90-previous-statement) (f90-line-continued) nil)))
939 (setq cont (f90-line-continued))
940 (cond ((and (not pcont) (not cont)) 'single)
941 ((and (not pcont) cont) 'begin)
942 ((and pcont (not cont)) 'end)
943 ((and pcont cont) 'middle)
944 (t (error)))))
945
946(defsubst f90-indent-line-no ()
947 (if f90-leave-line-no
948 ()
949 (if (and (not (zerop (skip-chars-forward " \t")))
950 (looking-at "[0-9]"))
951 (delete-horizontal-space)))
952 (skip-chars-forward " \t0-9"))
953
954(defsubst f90-no-block-limit ()
955 (let ((eol (f90-get-end-of-line)))
956 (save-excursion
957 (not (or (looking-at "end")
958 (looking-at "\\(do\\|if\\|else\\|select[ \t]*case\\|\
959case\\|where\\|forall\\)\\>")
960 (looking-at "\\(program\\|module\\|interface\\|\
961block[ \t]*data\\)\\>")
ee30478d
KH
962 (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
963 (looking-at f90-type-def-re)
034a9d40
RS
964 (re-search-forward "\\(function\\|subroutine\\)" eol t))))))
965
966(defsubst f90-update-line ()
967 (let (bol eol)
ee30478d 968 (if f90-auto-keyword-case
034a9d40
RS
969 (progn (setq bol (f90-get-beg-of-line)
970 eol (f90-get-end-of-line))
971 (if f90-auto-keyword-case
ee30478d 972 (f90-change-keywords f90-auto-keyword-case bol eol))))))
034a9d40
RS
973\f
974(defun f90-get-correct-indent ()
975 "Get correct indent for a line starting with line number.
976Does not check type and subprogram indentation."
977 (let ((epnt (f90-get-end-of-line)) icol cont)
978 (save-excursion
979 (while (and (f90-previous-statement)
980 (or (progn
981 (setq cont (f90-present-statement-cont))
982 (or (eq cont 'end) (eq cont 'middle)))
983 (looking-at "[ \t]*[0-9]"))))
984 (setq icol (current-indentation))
985 (beginning-of-line)
986 (if (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
987 (f90-get-end-of-line) t)
988 (progn
989 (beginning-of-line) (skip-chars-forward " \t")
990 (cond ((f90-looking-at-do)
991 (setq icol (+ icol f90-do-indent)))
992 ((or (f90-looking-at-if-then)
993 (f90-looking-at-where-or-forall)
994 (f90-looking-at-select-case))
995 (setq icol (+ icol f90-if-indent))))
996 (end-of-line)))
997 (while (re-search-forward
ee30478d 998 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
034a9d40
RS
999 (beginning-of-line) (skip-chars-forward " \t0-9")
1000 (cond ((f90-looking-at-do)
1001 (setq icol (+ icol f90-do-indent)))
1002 ((or (f90-looking-at-if-then)
1003 (f90-looking-at-where-or-forall)
1004 (f90-looking-at-select-case))
1005 (setq icol (+ icol f90-if-indent)))
1006 ((looking-at f90-end-if-re)
1007 (setq icol (- icol f90-if-indent)))
ee30478d 1008 ((looking-at "end[ \t]*do\\>")
034a9d40
RS
1009 (setq icol (- icol f90-do-indent))))
1010 (end-of-line))
1011 icol)))
1012
1013
1014(defun f90-calculate-indent ()
1015 "Calculate the indent column based on previous statements."
1016 (interactive)
1017 (let (icol cont (case-fold-search t) (pnt (point)))
1018 (save-excursion
1019 (if (not (f90-previous-statement))
1020 (setq icol 0)
1021 (setq cont (f90-present-statement-cont))
1022 (if (eq cont 'end)
1023 (while (not (eq 'begin (f90-present-statement-cont)))
1024 (f90-previous-statement)))
1025 (cond ((eq cont 'begin)
1026 (setq icol (+ (f90-current-indentation)
1027 f90-continuation-indent)))
1028 ((eq cont 'middle) (setq icol(current-indentation)))
1029 (t (setq icol (f90-current-indentation))
1030 (skip-chars-forward " \t")
1031 (if (looking-at "[0-9]")
1032 (setq icol (f90-get-correct-indent))
1033 (cond ((or (f90-looking-at-if-then)
1034 (f90-looking-at-where-or-forall)
1035 (f90-looking-at-select-case)
1036 (looking-at f90-else-like-re))
1037 (setq icol (+ icol f90-if-indent)))
1038 ((f90-looking-at-do)
1039 (setq icol (+ icol f90-do-indent)))
1040 ((f90-looking-at-type-like)
1041 (setq icol (+ icol f90-type-indent)))
1042 ((or (f90-looking-at-program-block-start)
1043 (looking-at "contains[ \t]*\\($\\|!\\)"))
1044 (setq icol (+ icol f90-program-indent)))))
1045 (goto-char pnt)
1046 (beginning-of-line)
1047 (cond ((looking-at "[ \t]*$"))
1048 ((looking-at "[ \t]*#") ; Check for cpp directive.
1049 (setq icol 0))
1050 (t
1051 (skip-chars-forward " \t0-9")
1052 (cond ((or (looking-at f90-else-like-re)
1053 (looking-at f90-end-if-re))
1054 (setq icol (- icol f90-if-indent)))
ee30478d 1055 ((looking-at "end[ \t]*do\\>")
034a9d40
RS
1056 (setq icol (- icol f90-do-indent)))
1057 ((looking-at f90-end-type-re)
1058 (setq icol (- icol f90-type-indent)))
1059 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1060 (f90-looking-at-program-block-end))
1061 (setq icol (- icol f90-program-indent))))))
1062 ))))
1063 icol))
1064\f
1065;; Statement = statement line, a line which is neither blank, nor a comment.
1066(defun f90-previous-statement ()
1067 "Move point to beginning of the previous F90 statement.
1068Return nil if no previous statement is found."
1069 (interactive)
1070 (let (not-first-statement)
1071 (beginning-of-line)
1072 (while (and (setq not-first-statement (zerop (forward-line -1)))
ee30478d 1073 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
034a9d40
RS
1074 not-first-statement))
1075
1076(defun f90-next-statement ()
1077 "Move point to beginning of the next F90 statement.
1078Return nil if no later statement is found."
1079 (interactive)
1080 (let (not-last-statement)
1081 (beginning-of-line)
1082 (while (and (setq not-last-statement
1083 (and (zerop (forward-line 1))
1084 (not (eobp))))
1085 (looking-at "[ \t0-9]*\\(!\\|$\\)")))
1086 not-last-statement))
1087
1088(defun f90-beginning-of-subprogram ()
1089 "Move point to the beginning of subprogram.
1090Return (type name) or nil if not found."
1091 (interactive)
1092 (let ((count 1) (case-fold-search t) matching-beg)
1093 (beginning-of-line) (skip-chars-forward " \t0-9")
1094 (if (setq matching-beg (f90-looking-at-program-block-start))
1095 (setq count (- count 1)))
1096 (while (and (not (zerop count))
1097 (re-search-backward f90-program-block-re nil 'move))
1098 (beginning-of-line) (skip-chars-forward " \t0-9")
1099 (cond
1100 ((setq matching-beg (f90-looking-at-program-block-start))
1101 (setq count (- count 1)))
1102 ((f90-looking-at-program-block-end)
1103 (setq count (+ count 1)))))
1104 (beginning-of-line)
1105 (if (zerop count)
1106 matching-beg
1107 (message "No beginning-found.")
1108 nil)))
1109
1110(defun f90-end-of-subprogram ()
1111 "Move point to the end of subprogram.
1112Return (type name) or nil if not found."
1113 (interactive)
1114 (let ((count 1) (case-fold-search t) matching-end)
1115 (beginning-of-line) (skip-chars-forward " \t0-9")
1116 (if (setq matching-end (f90-looking-at-program-block-end))
1117 (setq count (1- count)))
1118 (end-of-line)
1119 (while (and (not (zerop count))
1120 (re-search-forward f90-program-block-re nil 'move))
1121 (beginning-of-line) (skip-chars-forward " \t0-9")
1122 (cond ((f90-looking-at-program-block-start)
1123 (setq count (+ count 1)))
1124 ((setq matching-end (f90-looking-at-program-block-end))
1125 (setq count (1- count ))))
1126 (end-of-line))
1127 (forward-line 1)
1128 (if (zerop count)
1129 matching-end
1130 (message "No end found.")
1131 nil)))
1132
1133(defun f90-mark-subprogram ()
1134 "Put mark at end of F90 subprogram, point at beginning.
1135Marks are pushed and highlight (grey shadow) is turned on."
1136 (interactive)
1137 (let ((pos (point)) program)
1138 (f90-end-of-subprogram)
1139 (push-mark (point) t)
1140 (goto-char pos)
1141 (setq program (f90-beginning-of-subprogram))
1142 ;; The keywords in the preceding lists assume case-insensitivity.
ee30478d 1143 (if (string-match "XEmacs" emacs-version)
034a9d40
RS
1144 (zmacs-activate-region)
1145 (setq mark-active t)
1146 (setq deactivate-mark nil))
1147 program))
1148
1149(defun f90-comment-region (beg-region end-region)
1150 "Comment/uncomment every line in the region.
1151Insert f90-comment-region at the beginning of every line in the region
1152or, if already present, remove it."
1153 (interactive "*r")
1154 (let ((end (make-marker)))
1155 (set-marker end end-region)
1156 (goto-char beg-region)
1157 (beginning-of-line)
1158 (if (looking-at (regexp-quote f90-comment-region))
1159 (delete-region (point) (match-end 0))
1160 (insert f90-comment-region))
1161 (while (and (zerop (forward-line 1))
1162 (< (point) (marker-position end)))
1163 (if (looking-at (regexp-quote f90-comment-region))
1164 (delete-region (point) (match-end 0))
1165 (insert f90-comment-region)))
1166 (set-marker end nil)))
1167
1168(defun f90-indent-line (&optional no-update)
1169 "Indent current line as F90 code."
1170 (interactive)
1171 (let (indent (no-line-number nil) (pos (make-marker)) (case-fold-search t))
1172 (set-marker pos (point))
1173 (beginning-of-line) ; Digits after & \n are not line-no
1174 (if (save-excursion (and (f90-previous-statement) (f90-line-continued)))
1175 (progn (setq no-line-number t) (skip-chars-forward " \t"))
1176 (f90-indent-line-no))
1177 (if (looking-at "!")
1178 (setq indent (f90-comment-indent))
ee30478d
KH
1179 (if (and (looking-at "end") f90-smart-end)
1180 (f90-match-end))
034a9d40
RS
1181 (setq indent (f90-calculate-indent)))
1182 (if (zerop (- indent (current-column)))
1183 nil
1184 (f90-indent-to indent no-line-number))
1185 ;; If initial point was within line's indentation,
1186 ;; position after the indentation. Else stay at same point in text.
1187 (if (< (point) (marker-position pos))
1188 (goto-char (marker-position pos)))
1189 (if (not no-update) (f90-update-line))
1190 (if (and auto-fill-function
1191 (> (save-excursion (end-of-line) (current-column)) fill-column))
1192 (save-excursion (f90-do-auto-fill)))
1193 (set-marker pos nil)))
1194
1195(defun f90-indent-new-line ()
1196 "Reindent the current F90 line, insert a newline and indent the newline.
1197An abbrev before point is expanded if `abbrev-mode' is non-nil.
1198If run in the middle of a line, the line is not broken."
1199 (interactive)
1200 (let (string cont (case-fold-search t))
1201 (if abbrev-mode (expand-abbrev))
1202 (beginning-of-line) ; Reindent where likely to be needed.
1203 (f90-indent-line-no)
1204 (if (or (looking-at "\\(end\\|else\\|!\\)"))
1205 (f90-indent-line 'no-update))
1206 (end-of-line)
1207 (delete-horizontal-space) ;Destroy trailing whitespace
1208 (setq string (f90-in-string))
1209 (setq cont (f90-line-continued))
1210 (if (and string (not cont)) (insert "&"))
1211 (f90-update-line)
1212 (newline)
1213 (if (or string (and cont f90-beginning-ampersand)) (insert "&"))
1214 (f90-indent-line 'no-update)))
1215
1216
1217(defun f90-indent-region (beg-region end-region)
1218 "Indent every line in region by forward parsing."
1219 (interactive "*r")
1220 (let ((end-region-mark (make-marker)) (save-point (point-marker))
1221 (block-list nil) ind-lev ind-curr ind-b cont
1222 struct beg-struct end-struct)
1223 (set-marker end-region-mark end-region)
1224 (goto-char beg-region)
1225 ;; first find a line which is not a continuation line or comment
1226 (beginning-of-line)
ee30478d 1227 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
034a9d40
RS
1228 (progn (f90-indent-line 'no-update)
1229 (zerop (forward-line 1)))
1230 (< (point) end-region-mark)))
1231 (setq cont (f90-present-statement-cont))
1232 (while (and (or (eq cont 'middle) (eq cont 'end))
1233 (f90-previous-statement))
1234 (setq cont (f90-present-statement-cont)))
1235 ;; process present line for beginning of block
1236 (setq f90-cache-position (point))
1237 (f90-indent-line 'no-update)
1238 (setq ind-lev (f90-current-indentation))
1239 (setq ind-curr ind-lev)
1240 (beginning-of-line) (skip-chars-forward " \t0-9")
1241 (setq struct nil)
1242 (setq ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1243 ((or (setq struct (f90-looking-at-if-then))
1244 (setq struct (f90-looking-at-select-case))
1245 (setq struct (f90-looking-at-where-or-forall))
1246 (looking-at f90-else-like-re))
1247 f90-if-indent)
1248 ((setq struct (f90-looking-at-type-like))
1249 f90-type-indent)
1250 ((or(setq struct (f90-looking-at-program-block-start))
1251 (looking-at "contains[ \t]*\\($\\|!\\)"))
1252 f90-program-indent)))
1253 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1254 (if struct (setq block-list (cons struct block-list)))
1255 (while (and (f90-line-continued) (zerop (forward-line 1))
1256 (< (point) end-region-mark))
1257 (if (not (zerop (- (current-indentation)
1258 (+ ind-curr f90-continuation-indent))))
1259 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no)))
1260 ;; process all following lines
1261 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1262 (beginning-of-line)
1263 (f90-indent-line-no)
1264 (setq f90-cache-position (point))
1265 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1266 ((looking-at "[ \t]*#") (setq ind-curr 0))
1267 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1268 ((f90-no-block-limit) (setq ind-curr ind-lev))
1269 ((looking-at f90-else-like-re) (setq ind-curr
1270 (- ind-lev f90-if-indent)))
1271 ((looking-at "contains[ \t]*\\($\\|!\\)")
1272 (setq ind-curr (- ind-lev f90-program-indent)))
1273 ((setq ind-b
1274 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1275 ((or (setq struct (f90-looking-at-if-then))
1276 (setq struct (f90-looking-at-select-case))
1277 (setq struct (f90-looking-at-where-or-forall)))
1278 f90-if-indent)
1279 ((setq struct (f90-looking-at-type-like))
1280 f90-type-indent)
1281 ((setq struct (f90-looking-at-program-block-start))
1282 f90-program-indent)))
1283 (setq ind-curr ind-lev)
1284 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1285 (setq block-list (cons struct block-list)))
1286 ((setq end-struct (f90-looking-at-program-block-end))
1287 (setq beg-struct (car block-list)
1288 block-list (cdr block-list))
1289 (if f90-smart-end
1290 (save-excursion
1291 (f90-block-match (car beg-struct)(car (cdr beg-struct))
1292 (car end-struct)(car (cdr end-struct)))))
1293 (setq ind-b
1294 (cond ((looking-at f90-end-if-re) f90-if-indent)
1295 ((looking-at "end[ \t]*do\\>") f90-do-indent)
1296 ((looking-at f90-end-type-re) f90-type-indent)
1297 ((f90-looking-at-program-block-end)
1298 f90-program-indent)))
1299 (if ind-b (setq ind-lev (- ind-lev ind-b)))
1300 (setq ind-curr ind-lev))
034a9d40
RS
1301 (t (setq ind-curr ind-lev)))
1302 ;; do the indentation if necessary
1303 (if (not (zerop (- ind-curr (current-column))))
1304 (f90-indent-to ind-curr))
1305 (while (and (f90-line-continued) (zerop (forward-line 1))
1306 (< (point) end-region-mark))
1307 (if (not (zerop (- (current-indentation)
1308 (+ ind-curr f90-continuation-indent))))
1309 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1310 ;; restore point etc
1311 (setq f90-cache-position nil)
1312 (goto-char save-point)
1313 (set-marker end-region-mark nil)
1314 (set-marker save-point nil)
ee30478d 1315 (if (string-match "Xemacs" emacs-version)
034a9d40
RS
1316 (zmacs-deactivate-region)
1317 (deactivate-mark))))
1318
1319(defun f90-indent-subprogram ()
1320 "Properly indent the subprogram which contains point."
1321 (interactive)
1322 (save-excursion
1323 (let (program)
1324 (setq program (f90-mark-subprogram))
1325 (if program
1326 (progn
1327 (message (concat "Indenting " (car program) " "
1328 (car (cdr program))"."))
1329 (f90-indent-region (point) (mark))
1330 (message (concat "Indenting " (car program) " "
1331 (car (cdr program)) "...done.")))
1332 (message "Indenting the whole file.")
1333 (f90-indent-region (point) (mark))
1334 (message (concat "Indenting the whole file...done."))))))
1335
1336;; autofill and break-line
1337(defun f90-break-line (&optional no-update)
1338 "Break line at point, insert continuation marker(s) and indent."
1339 (interactive)
1340 (let (ctype)
1341 (cond ((f90-in-string)
1342 (insert "&") (newline) (insert "&"))
1343 ((f90-in-comment)
1344 (delete-horizontal-space)
1345 (setq ctype (f90-get-present-comment-type))
1346 (newline) (insert (concat ctype " ")))
1347 (t (delete-horizontal-space)
1348 (insert "&")
1349 (if (not no-update) (f90-update-line))
1350 (newline)
1351 (if f90-beginning-ampersand (insert "& ")))))
1352 (if (not no-update) (f90-indent-line)))
1353
1354(defun f90-find-breakpoint ()
1355 "From fill-column, search backward for break-delimiter."
1356 (let ((bol (f90-get-beg-of-line)))
1357 (re-search-backward f90-break-delimiters bol)
1358 (if f90-break-before-delimiters
1359 (progn (backward-char)
1360 (if (not (looking-at f90-no-break-re))
1361 (forward-char)))
1362 (if (looking-at f90-no-break-re)
1363 (forward-char 2)
1364 (forward-char)))))
1365
1366(defun f90-auto-fill-mode (arg)
1367 "Toggle f90-auto-fill mode.
1368With ARG, turn `f90-auto-fill' mode on iff ARG is positive.
1369In `f90-auto-fill' mode, inserting a space at a column beyond `fill-column'
1370automatically breaks the line at a previous space."
1371 (interactive "P")
1372 (prog1 (setq auto-fill-function
1373 (if (if (null arg)
1374 (not auto-fill-function)
1375 (> (prefix-numeric-value arg) 0))
1376 'f90-do-auto-fill))
3d93ac28 1377 (force-mode-line-update)))
034a9d40
RS
1378
1379(defun f90-do-auto-fill ()
1380 "Break line if non-white characters beyond fill-column."
1381 (interactive)
1382 ;; Break the line before or after the last delimiter (non-word char).
1383 ;; Will not break **, //, or => (specified by f90-no-break-re).
ee30478d
KH
1384 ;; Start by checking that line is longer than fill-column.
1385 (if (> (save-excursion (end-of-line) (current-column)) fill-column)
1386 (progn
1387 (move-to-column fill-column)
1388 (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
1389 (delete-horizontal-space)
1390 (f90-find-breakpoint)
1391 (f90-break-line)
1392 (end-of-line)))))
034a9d40
RS
1393
1394(defun f90-join-lines ()
1395 "Join present line with next line, if this line ends with \&."
1396 (interactive)
1397 (let (pos (oldpos (point)))
1398 (end-of-line)
1399 (skip-chars-backward " \t")
1400 (cond ((= (preceding-char) ?&)
1401 (delete-char -1)
1402 (setq pos (point))
1403 (forward-line 1)
1404 (skip-chars-forward " \t")
1405 (if (looking-at "\&") (delete-char 1))
1406 (delete-region pos (point))
1407 (if (not (f90-in-string))
1408 (progn (delete-horizontal-space) (insert " ")))
1409 (if (and auto-fill-function
1410 (> (save-excursion (end-of-line)
1411 (current-column))
1412 fill-column))
1413 (f90-do-auto-fill))
1414 (goto-char oldpos)
1415 t))))
1416
1417(defun f90-fill-region (beg-region end-region)
1418 "Fill every line in region by forward parsing. Join lines if possible."
1419 (interactive "*r")
1420 (let ((end-region-mark (make-marker))
ee30478d 1421 (f90-smart-end nil) (f90-auto-keyword-case nil) indent (go-on t)
034a9d40
RS
1422 (af-function auto-fill-function) (auto-fill-function nil))
1423 (set-marker end-region-mark end-region)
1424 (goto-char beg-region)
1425 (while go-on
1426 ;; join as much as possible
1427 (while (f90-join-lines));
1428 (setq indent (+ (f90-current-indentation) f90-continuation-indent))
1429 ;; chop the line if necessary
1430 (while (> (save-excursion (end-of-line) (current-column))
1431 fill-column)
1432 (move-to-column fill-column)
1433 (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
1434 (delete-horizontal-space)
1435 (f90-find-breakpoint)
1436 (f90-break-line 'no-update)
1437 (f90-indent-to indent 'no-line-no)))
1438 (setq go-on (and (< (point) (marker-position end-region-mark))
1439 (zerop (forward-line 1))))
1440 (setq f90-cache-position (point)))
1441 (setq auto-fill-function af-function)
1442 (setq f90-cache-position nil)
ee30478d 1443 (if (string-match "XEmacs" emacs-version)
034a9d40
RS
1444 (zmacs-deactivate-region)
1445 (deactivate-mark))))
1446\f
1447(defun f90-block-match (beg-block beg-name end-block end-name)
1448 "Match end-struct with beg-struct and complete end-block if possible.
1449Leave point at the end of line."
1450 (search-forward "end" (f90-get-end-of-line))
1451 (catch 'no-match
1452 (if (not (f90-equal-symbols beg-block end-block))
1453 (if end-block
1454 (progn
1455 (message "END %s does not match %s." end-block beg-block)
1456 (end-of-line)
1457 (throw 'no-match nil))
1458 (message "Inserting %s." beg-block)
1459 (insert (concat " " beg-block)))
1460 (search-forward end-block))
1461 (if (not (f90-equal-symbols beg-name end-name))
1462 (cond ((and beg-name (not end-name))
1463 (message "Inserting %s." beg-name)
1464 (insert (concat " " beg-name)))
1465 ((and beg-name end-name)
1466 (message "Replacing %s with %s." end-name beg-name)
1467 (search-forward end-name)
1468 (replace-match beg-name))
1469 ((and (not beg-name) end-name)
1470 (message "Deleting %s." end-name)
1471 (search-forward end-name)
1472 (replace-match "")))
1473 (if end-name (search-forward end-name)))
ee30478d 1474 (if (not (looking-at "[ \t]*!")) (delete-horizontal-space))))
034a9d40
RS
1475
1476(defun f90-match-end ()
1477 "From an end foo statement, find the corresponding foo including name."
1478 (interactive)
1479 (let ((count 1) (top-of-window (window-start)) (matching-beg nil)
1480 (end-point (point)) (case-fold-search t)
1481 beg-name end-name beg-block end-block end-struct)
1482 (if (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
1483 (setq end-struct (f90-looking-at-program-block-end)))
1484 (progn
1485 (setq end-block (car end-struct))
1486 (setq end-name (car (cdr end-struct)))
1487 (save-excursion
1488 (beginning-of-line)
1489 (while (and (not (zerop count))
1490 (re-search-backward
ee30478d 1491 (concat "\\(" f90-blocks-re "\\)") nil t))
034a9d40
RS
1492 (beginning-of-line) (skip-chars-forward " \t0-9")
1493 (cond ((setq matching-beg
1494 (cond
1495 ((f90-looking-at-do))
1496 ((f90-looking-at-if-then))
1497 ((f90-looking-at-where-or-forall))
1498 ((f90-looking-at-select-case))
1499 ((f90-looking-at-type-like))
1500 ((f90-looking-at-program-block-start))))
1501 (setq count (- count 1)))
1502 ((looking-at (concat "end[ \t]*" f90-blocks-re "\\b"))
ee30478d 1503 (setq count (+ count 1)))))
034a9d40
RS
1504 (if (not (zerop count))
1505 (message "No matching beginning.")
1506 (f90-update-line)
1507 (if (eq f90-smart-end 'blink)
1508 (if (< (point) top-of-window)
1509 (message (concat
1510 "Matches " (what-line) ": "
1511 (buffer-substring
1512 (progn (beginning-of-line) (point))
1513 (progn (end-of-line) (point)))))
1514 (sit-for 1)))
1515 (setq beg-block (car matching-beg))
1516 (setq beg-name (car (cdr matching-beg)))
1517 (goto-char end-point)
1518 (beginning-of-line)
1519 (f90-block-match beg-block beg-name end-block end-name)))))))
1520
1521(defun f90-insert-end ()
1522 "Inserts an complete end statement matching beginning of present block."
1523 (interactive)
1524 (let ((f90-smart-end (if f90-smart-end f90-smart-end 'blink)))
1525 (insert "end")
1526 (f90-indent-new-line)))
1527\f
1528;; abbrevs and keywords
1529
1530(defun f90-abbrev-start ()
1531 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
1532Any other key combination is executed normally."
1533 (interactive)
ee30478d 1534 (let (e c)
034a9d40 1535 (insert last-command-char)
ee30478d
KH
1536 (setq e (next-command-event)
1537 c (if (string-match "XEmacs" emacs-version)
1538 (event-to-character e)
1539 (read-event)))
1540 ;; insert char if not equal to `?'
1541 (if (or (= c ??) (eq c help-char))
034a9d40 1542 (f90-abbrev-help)
ee30478d
KH
1543 (if (string-match "XEmacs" emacs-version)
1544 (setq unread-command-event e)
034a9d40
RS
1545 (setq unread-command-events (list c))))))
1546
1547(defun f90-abbrev-help ()
1548 "List the currently defined abbrevs in F90 mode."
1549 (interactive)
1550 (message "Listing abbrev table...")
1551 (display-buffer (f90-prepare-abbrev-list-buffer))
1552 (message "Listing abbrev table...done"))
1553
1554(defun f90-prepare-abbrev-list-buffer ()
1555 (save-excursion
1556 (set-buffer (get-buffer-create "*Abbrevs*"))
1557 (erase-buffer)
1558 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
1559 (goto-char (point-min))
1560 (set-buffer-modified-p nil)
1561 (edit-abbrevs-mode))
1562 (get-buffer-create "*Abbrevs*"))
1563
1564(defun f90-upcase-keywords ()
1565 "Upcase all F90 keywords in the buffer."
1566 (interactive)
1567 (f90-change-keywords 'upcase-word))
1568
1569(defun f90-capitalize-keywords ()
1570 "Capitalize all F90 keywords in the buffer."
1571 (interactive)
1572 (f90-change-keywords 'capitalize-word))
1573
1574(defun f90-downcase-keywords ()
1575 "Downcase all F90 keywords in the buffer."
1576 (interactive)
1577 (f90-change-keywords 'downcase-word))
1578
1579(defun f90-upcase-region-keywords (beg end)
1580 "Upcase all F90 keywords in the region."
1581 (interactive "*r")
1582 (f90-change-keywords 'upcase-word beg end))
1583
1584(defun f90-capitalize-region-keywords (beg end)
1585 "Capitalize all F90 keywords in the region."
1586 (interactive "*r")
1587 (f90-change-keywords 'capitalize-word beg end))
1588
1589(defun f90-downcase-region-keywords (beg end)
1590 "Downcase all F90 keywords in the region."
1591 (interactive "*r")
1592 (f90-change-keywords 'downcase-word beg end))
1593
1594;; Change the keywords according to argument.
1595(defun f90-change-keywords (change-word &optional beg end)
1596 (save-excursion
1597 (setq beg (if beg beg (point-min)))
1598 (setq end (if end end (point-max)))
1599 (let ((keyword-re
ee30478d
KH
1600 (concat "\\("
1601 f90-keywords-re "\\|" f90-procedures-re "\\|"
1602 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
1603 (ref-point (point-min)) state
1604 (modified (buffer-modified-p)) saveword back-point)
034a9d40 1605 (goto-char beg)
ee30478d
KH
1606 (unwind-protect
1607 (while (re-search-forward keyword-re end t)
1608 (if (progn
1609 (setq state (parse-partial-sexp ref-point (point)))
1610 (or (nth 3 state) (nth 4 state)
1611 (save-excursion ; Check for cpp directive.
1612 (beginning-of-line)
1613 (skip-chars-forward " \t0-9")
1614 (looking-at "#"))))
1615 ()
1616 (setq ref-point (point)
1617 back-point (save-excursion (backward-word 1) (point)))
1618 (setq saveword (buffer-substring back-point ref-point))
1619 (funcall change-word -1)
1620 (or (string= saveword (buffer-substring back-point ref-point))
1621 (setq modified t))))
1622 (or modified (set-buffer-modified-p nil))))))
034a9d40
RS
1623
1624(provide 'f90)
db97b872 1625
034a9d40 1626;;; f90.el ends here