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