(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / progmodes / f90.el
blob6eb39051984afb753aa089df14a81745d97df201
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 an F90 \"block\", from the line start.
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.
610 (defconst f90-start-block-re
611 (concat
612 "^[ \t0-9]*" ; statement number
613 "\\(\\("
614 "\\(\\sw+[ \t]*:[ \t]*\\)?" ; structure label
615 "\\(do\\|select[ \t]*case\\|"
616 ;; See comments in fortran-start-block-re for the problems of IF.
617 "if[ \t]*(\\(.*\\|"
618 ".*\n\\([^if]*\\([^i].\\|.[^f]\\|.\\>\\)\\)\\)\\<then\\|"
619 ;; Distinguish WHERE block from isolated WHERE.
620 "\\(where\\|forall\\)[ \t]*(.*)[ \t]*\\(!\\|$\\)\\)\\)"
621 "\\|"
622 "program\\|interface\\|module\\|type\\|function\\|subroutine"
623 "\\)"
624 "[ \t]*")
625 "Regexp matching the start of an F90 \"block\", from the line start.
626 A simple regexp cannot do this in fully correct fashion, so this
627 tries to strike a compromise between complexity and flexibility.
628 Used in the F90 entry in `hs-special-modes-alist'.")
630 ;; hs-special-modes-alist is autoloaded.
631 (add-to-list 'hs-special-modes-alist
632 `(f90-mode ,f90-start-block-re ,f90-end-block-re
633 "!" f90-end-of-block nil))
636 ;; Imenu support.
637 (defvar f90-imenu-generic-expression
638 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
639 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]"))
640 (list
641 '(nil "^[ \t0-9]*program[ \t]+\\(\\sw+\\)" 1)
642 '("Modules" "^[ \t0-9]*module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
643 '("Types" "^[ \t0-9]*type[ \t]+\\(\\sw+\\)" 1)
644 (list
645 "Procedures"
646 (concat
647 "^[ \t0-9]*"
648 "\\("
649 ;; At least three non-space characters before function/subroutine.
650 ;; Check that the last three non-space characters do not spell E N D.
651 "[^!\"\&\n]*\\("
652 not-e good-char good-char "\\|"
653 good-char not-n good-char "\\|"
654 good-char good-char not-d "\\)"
655 "\\|"
656 ;; Less than three non-space characters before function/subroutine.
657 good-char "?" good-char "?"
658 "\\)"
659 "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
660 4)))
661 "Value for `imenu-generic-expression' in F90 mode.")
663 (defun f90-add-imenu-menu ()
664 "Add an imenu menu to the menubar."
665 (interactive)
666 (if (lookup-key (current-local-map) [menu-bar index])
667 (message "%s" "F90-imenu already exists.")
668 (imenu-add-to-menubar "F90-imenu")
669 (redraw-frame (selected-frame))))
672 ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
673 (defvar f90-mode-abbrev-table
674 (let (abbrevs-changed)
675 (define-abbrev-table 'f90-mode-abbrev-table nil)
676 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
677 ;; A little baroque to quieten the byte-compiler.
678 (mapcar
679 (function (lambda (element)
680 (condition-case nil
681 (apply 'define-abbrev f90-mode-abbrev-table
682 (append element '(nil 0 t)))
683 (wrong-number-of-arguments
684 (apply 'define-abbrev f90-mode-abbrev-table
685 (append element '(nil 0)))))))
686 '(("`al" "allocate" )
687 ("`ab" "allocatable" )
688 ("`as" "assignment" )
689 ("`ba" "backspace" )
690 ("`bd" "block data" )
691 ("`c" "character" )
692 ("`cl" "close" )
693 ("`cm" "common" )
694 ("`cx" "complex" )
695 ("`cn" "contains" )
696 ("`cy" "cycle" )
697 ("`de" "deallocate" )
698 ("`df" "define" )
699 ("`di" "dimension" )
700 ("`dw" "do while" )
701 ("`el" "else" )
702 ("`eli" "else if" )
703 ("`elw" "elsewhere" )
704 ("`eq" "equivalence" )
705 ("`ex" "external" )
706 ("`ey" "entry" )
707 ("`fl" "forall" )
708 ("`fo" "format" )
709 ("`fu" "function" )
710 ("`fa" ".false." )
711 ("`im" "implicit none")
712 ("`in" "include" )
713 ("`i" "integer" )
714 ("`it" "intent" )
715 ("`if" "interface" )
716 ("`lo" "logical" )
717 ("`mo" "module" )
718 ("`na" "namelist" )
719 ("`nu" "nullify" )
720 ("`op" "optional" )
721 ("`pa" "parameter" )
722 ("`po" "pointer" )
723 ("`pr" "print" )
724 ("`pi" "private" )
725 ("`pm" "program" )
726 ("`pu" "public" )
727 ("`r" "real" )
728 ("`rc" "recursive" )
729 ("`rt" "return" )
730 ("`rw" "rewind" )
731 ("`se" "select" )
732 ("`sq" "sequence" )
733 ("`su" "subroutine" )
734 ("`ta" "target" )
735 ("`tr" ".true." )
736 ("`t" "type" )
737 ("`wh" "where" )
738 ("`wr" "write" )))
739 f90-mode-abbrev-table)
740 "Abbrev table for F90 mode.")
743 ;;;###autoload
744 (defun f90-mode ()
745 "Major mode for editing Fortran 90,95 code in free format.
746 For fixed format code, use `fortran-mode'.
748 \\[f90-indent-line] indents the current line.
749 \\[f90-indent-new-line] indents current line and creates a new\
750 indented line.
751 \\[f90-indent-subprogram] indents the current subprogram.
753 Type `? or `\\[help-command] to display a list of built-in\
754 abbrevs for F90 keywords.
756 Key definitions:
757 \\{f90-mode-map}
759 Variables controlling indentation style and extra features:
761 `f90-do-indent'
762 Extra indentation within do blocks (default 3).
763 `f90-if-indent'
764 Extra indentation within if/select case/where/forall blocks (default 3).
765 `f90-type-indent'
766 Extra indentation within type/interface/block-data blocks (default 3).
767 `f90-program-indent'
768 Extra indentation within program/module/subroutine/function blocks
769 (default 2).
770 `f90-continuation-indent'
771 Extra indentation applied to continuation lines (default 5).
772 `f90-comment-region'
773 String inserted by function \\[f90-comment-region] at start of each
774 line in region (default \"!!!$\").
775 `f90-indented-comment-re'
776 Regexp determining the type of comment to be intended like code
777 (default \"!\").
778 `f90-directive-comment-re'
779 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
780 (default \"!hpf\\\\$\").
781 `f90-break-delimiters'
782 Regexp holding list of delimiters at which lines may be broken
783 (default \"[-+*/><=,% \\t]\").
784 `f90-break-before-delimiters'
785 Non-nil causes `f90-do-auto-fill' to break lines before delimiters
786 (default t).
787 `f90-beginning-ampersand'
788 Automatic insertion of \& at beginning of continuation lines (default t).
789 `f90-smart-end'
790 From an END statement, check and fill the end using matching block start.
791 Allowed values are 'blink, 'no-blink, and nil, which determine
792 whether to blink the matching beginning (default 'blink).
793 `f90-auto-keyword-case'
794 Automatic change of case of keywords (default nil).
795 The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
796 `f90-leave-line-no'
797 Do not left-justify line numbers (default nil).
798 `f90-keywords-re'
799 List of keywords used for highlighting/upcase-keywords etc.
801 Turning on F90 mode calls the value of the variable `f90-mode-hook'
802 with no args, if that value is non-nil."
803 (interactive)
804 (kill-all-local-variables)
805 (setq major-mode 'f90-mode
806 mode-name "F90"
807 local-abbrev-table f90-mode-abbrev-table)
808 (set-syntax-table f90-mode-syntax-table)
809 (use-local-map f90-mode-map)
810 (set (make-local-variable 'indent-line-function) 'f90-indent-line)
811 (set (make-local-variable 'indent-region-function) 'f90-indent-region)
812 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
813 (set (make-local-variable 'comment-start) "!")
814 (set (make-local-variable 'comment-start-skip) "!+ *")
815 (set (make-local-variable 'comment-indent-function) 'f90-comment-indent)
816 (set (make-local-variable 'abbrev-all-caps) t)
817 (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
818 (setq indent-tabs-mode nil) ; auto buffer local
819 (set (make-local-variable 'font-lock-defaults)
820 '((f90-font-lock-keywords f90-font-lock-keywords-1
821 f90-font-lock-keywords-2
822 f90-font-lock-keywords-3
823 f90-font-lock-keywords-4)
824 nil t))
825 (set (make-local-variable 'imenu-case-fold-search) t)
826 (set (make-local-variable 'imenu-generic-expression)
827 f90-imenu-generic-expression)
828 (set (make-local-variable 'beginning-of-defun-function)
829 'f90-beginning-of-subprogram)
830 (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
831 (set (make-local-variable 'add-log-current-defun-function)
832 #'f90-current-defun)
833 (run-mode-hooks 'f90-mode-hook))
836 ;; Inline-functions.
837 (defsubst f90-in-string ()
838 "Return non-nil if point is inside a string.
839 Checks from `point-min', or `f90-cache-position', if that is non-nil
840 and lies before point."
841 (let ((beg-pnt
842 (if (and f90-cache-position (> (point) f90-cache-position))
843 f90-cache-position
844 (point-min))))
845 (nth 3 (parse-partial-sexp beg-pnt (point)))))
847 (defsubst f90-in-comment ()
848 "Return non-nil if point is inside a comment.
849 Checks from `point-min', or `f90-cache-position', if that is non-nil
850 and lies before point."
851 (let ((beg-pnt
852 (if (and f90-cache-position (> (point) f90-cache-position))
853 f90-cache-position
854 (point-min))))
855 (nth 4 (parse-partial-sexp beg-pnt (point)))))
857 (defsubst f90-line-continued ()
858 "Return t if the current line is a continued one.
859 This includes comment lines embedded in continued lines, but
860 not the last line of a continued statement."
861 (save-excursion
862 (beginning-of-line)
863 (while (and (looking-at "[ \t]*\\(!\\|$\\)") (zerop (forward-line -1))))
864 (end-of-line)
865 (while (f90-in-comment)
866 (search-backward "!" (line-beginning-position))
867 (skip-chars-backward "!"))
868 (skip-chars-backward " \t")
869 (= (preceding-char) ?&)))
871 ;; GM this is not right, eg a continuation line starting with a number.
872 ;; Need f90-code-start-position function.
873 ;; And yet, things seems to work with this...
874 ;; cf f90-indent-line
875 ;; (beginning-of-line) ; digits after & \n are not line-nos
876 ;; (if (not (save-excursion (and (f90-previous-statement)
877 ;; (f90-line-continued))))
878 ;; (f90-indent-line-no)
879 (defsubst f90-current-indentation ()
880 "Return indentation of current line.
881 Line-numbers are considered whitespace characters."
882 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
884 (defsubst f90-indent-to (col &optional no-line-number)
885 "Indent current line to column COL.
886 If optional argument NO-LINE-NUMBER is nil, jump over a possible
887 line-number before indenting."
888 (beginning-of-line)
889 (or no-line-number
890 (skip-chars-forward " \t0-9"))
891 (delete-horizontal-space)
892 ;; Leave >= 1 space after line number.
893 (indent-to col (if (zerop (current-column)) 0 1)))
895 (defsubst f90-get-present-comment-type ()
896 "If point lies within a comment, return the string starting the comment.
897 For example, \"!\" or \"!!\", followed by the appropriate amount of
898 whitespace, if any."
899 ;; Include the whitespace for consistent auto-filling of comment blocks.
900 (save-excursion
901 (when (f90-in-comment)
902 (beginning-of-line)
903 (re-search-forward "!+[ \t]*" (line-end-position))
904 (while (f90-in-string)
905 (re-search-forward "!+[ \t]*" (line-end-position)))
906 (match-string-no-properties 0))))
908 (defsubst f90-equal-symbols (a b)
909 "Compare strings A and B neglecting case and allowing for nil value."
910 (equal (if a (downcase a) nil)
911 (if b (downcase b) nil)))
913 (defsubst f90-looking-at-do ()
914 "Return (\"do\" NAME) if a do statement starts after point.
915 NAME is nil if the statement has no label."
916 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\>")
917 (list (match-string 3) (match-string 2))))
919 (defsubst f90-looking-at-select-case ()
920 "Return (\"select\" NAME) if a select-case statement starts after point.
921 NAME is nil if the statement has no label."
922 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
923 \\(select\\)[ \t]*case[ \t]*(")
924 (list (match-string 3) (match-string 2))))
926 (defsubst f90-looking-at-if-then ()
927 "Return (\"if\" NAME) if an if () then statement starts after point.
928 NAME is nil if the statement has no label."
929 (save-excursion
930 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\>")
931 (let ((struct (match-string 3))
932 (label (match-string 2))
933 (pos (scan-lists (point) 1 0)))
934 (and pos (goto-char pos))
935 (skip-chars-forward " \t")
936 (if (or (looking-at "then\\>")
937 (when (f90-line-continued)
938 (f90-next-statement)
939 (skip-chars-forward " \t0-9&")
940 (looking-at "then\\>")))
941 (list struct label))))))
943 (defsubst f90-looking-at-where-or-forall ()
944 "Return (KIND NAME) if a where or forall block starts after point.
945 NAME is nil if the statement has no label."
946 (save-excursion
947 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
948 \\(where\\|forall\\)\\>")
949 (let ((struct (match-string 3))
950 (label (match-string 2))
951 (pos (scan-lists (point) 1 0)))
952 (and pos (goto-char pos))
953 (skip-chars-forward " \t")
954 (if (looking-at "\\(!\\|$\\)") (list struct label))))))
956 (defsubst f90-looking-at-type-like ()
957 "Return (KIND NAME) if a type/interface/block-data block starts after point.
958 NAME is non-nil only for type."
959 (cond
960 ((looking-at f90-type-def-re)
961 (list (match-string 1) (match-string 2)))
962 ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
963 (list (match-string 1) nil))))
965 (defsubst f90-looking-at-program-block-start ()
966 "Return (KIND NAME) if a program block with name NAME starts after point."
967 ;;;NAME is nil for an un-named main PROGRAM block."
968 (cond
969 ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
970 (list (match-string 1) (match-string 2)))
971 ((and (not (looking-at "module[ \t]*procedure\\>"))
972 (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
973 (list (match-string 1) (match-string 2)))
974 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
975 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
976 \\(\\sw+\\)"))
977 (list (match-string 1) (match-string 2)))))
978 ;; Following will match an un-named main program block; however
979 ;; one needs to check if there is an actual PROGRAM statement after
980 ;; point (and before any END program). Adding this will require
981 ;; change to eg f90-calculate-indent.
982 ;;; ((save-excursion
983 ;;; (not (f90-previous-statement)))
984 ;;; '("program" nil))))
986 (defsubst f90-looking-at-program-block-end ()
987 "Return (KIND NAME) if a block with name NAME ends after point."
988 (if (looking-at (concat "end[ \t]*" f90-blocks-re
989 "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
990 (list (match-string 1) (match-string 3))))
992 (defsubst f90-comment-indent ()
993 "Return the indentation to be used for a comment starting at point.
994 Used for `comment-indent-function' by F90 mode.
995 \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
996 `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
997 All others return `comment-column', leaving at least one space after code."
998 (cond ((looking-at "!!!") 0)
999 ((and f90-directive-comment-re
1000 (looking-at f90-directive-comment-re)) 0)
1001 ((looking-at (regexp-quote f90-comment-region)) 0)
1002 ((and (looking-at f90-indented-comment-re)
1003 ;; Don't attempt to indent trailing comment as code.
1004 (save-excursion
1005 (skip-chars-backward " \t")
1006 (bolp)))
1007 (f90-calculate-indent))
1008 (t (skip-chars-backward " \t")
1009 (max (if (bolp) 0 (1+ (current-column))) comment-column))))
1011 (defsubst f90-present-statement-cont ()
1012 "Return continuation properties of present statement.
1013 Possible return values are:
1014 single - statement is not continued.
1015 begin - current line is the first in a continued statement.
1016 end - current line is the last in a continued statement
1017 middle - current line is neither first nor last in a continued statement.
1018 Comment lines embedded amongst continued lines return 'middle."
1019 (let (pcont cont)
1020 (save-excursion
1021 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
1022 (setq cont (f90-line-continued))
1023 (cond ((and (not pcont) (not cont)) 'single)
1024 ((and (not pcont) cont) 'begin)
1025 ((and pcont (not cont)) 'end)
1026 ((and pcont cont) 'middle)
1027 (t (error "The impossible occurred")))))
1029 (defsubst f90-indent-line-no ()
1030 "If `f90-leave-line-no' is nil, left-justify a line number.
1031 Leaves point at the first non-blank character after the line number.
1032 Call from beginning of line."
1033 (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
1034 (delete-horizontal-space))
1035 (skip-chars-forward " \t0-9"))
1037 (defsubst f90-no-block-limit ()
1038 "Return nil if point is at the edge of a code block.
1039 Searches line forward for \"function\" or \"subroutine\",
1040 if all else fails."
1041 (save-excursion
1042 (not (or (looking-at "end")
1043 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
1044 \\|select[ \t]*case\\|case\\|where\\|forall\\)\\>")
1045 (looking-at "\\(program\\|module\\|interface\\|\
1046 block[ \t]*data\\)\\>")
1047 (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
1048 (looking-at f90-type-def-re)
1049 (re-search-forward "\\(function\\|subroutine\\)"
1050 (line-end-position) t)))))
1052 (defsubst f90-update-line ()
1053 "Change case of current line as per `f90-auto-keyword-case'."
1054 (if f90-auto-keyword-case
1055 (f90-change-keywords f90-auto-keyword-case
1056 (line-beginning-position) (line-end-position))))
1058 (defun f90-electric-insert (&optional arg)
1059 "Change keyword case and auto-fill line as operators are inserted."
1060 (interactive "*p")
1061 (self-insert-command arg)
1062 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1063 (f90-update-line)))
1066 (defun f90-get-correct-indent ()
1067 "Get correct indent for a line starting with line number.
1068 Does not check type and subprogram indentation."
1069 (let ((epnt (line-end-position)) icol cont)
1070 (save-excursion
1071 (while (and (f90-previous-statement)
1072 (or (progn
1073 (setq cont (f90-present-statement-cont))
1074 (or (eq cont 'end) (eq cont 'middle)))
1075 (looking-at "[ \t]*[0-9]"))))
1076 (setq icol (current-indentation))
1077 (beginning-of-line)
1078 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1079 (line-end-position) t)
1080 (beginning-of-line)
1081 (skip-chars-forward " \t")
1082 (cond ((f90-looking-at-do)
1083 (setq icol (+ icol f90-do-indent)))
1084 ((or (f90-looking-at-if-then)
1085 (f90-looking-at-where-or-forall)
1086 (f90-looking-at-select-case))
1087 (setq icol (+ icol f90-if-indent))))
1088 (end-of-line))
1089 (while (re-search-forward
1090 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1091 (beginning-of-line)
1092 (skip-chars-forward " \t0-9")
1093 (cond ((f90-looking-at-do)
1094 (setq icol (+ icol f90-do-indent)))
1095 ((or (f90-looking-at-if-then)
1096 (f90-looking-at-where-or-forall)
1097 (f90-looking-at-select-case))
1098 (setq icol (+ icol f90-if-indent)))
1099 ((looking-at f90-end-if-re)
1100 (setq icol (- icol f90-if-indent)))
1101 ((looking-at "end[ \t]*do\\>")
1102 (setq icol (- icol f90-do-indent))))
1103 (end-of-line))
1104 icol)))
1106 (defun f90-calculate-indent ()
1107 "Calculate the indent column based on previous statements."
1108 (interactive)
1109 (let (icol cont (case-fold-search t) (pnt (point)))
1110 (save-excursion
1111 (if (not (f90-previous-statement))
1112 ;; If f90-previous-statement returns nil, we must have been
1113 ;; called from on or before the first line of the first statement.
1114 (setq icol (if (save-excursion
1115 ;; f90-previous-statement has moved us over
1116 ;; comment/blank lines, so we need to get
1117 ;; back to the first code statement.
1118 (when (looking-at "[ \t]*\\([!#]\\|$\\)")
1119 (f90-next-statement))
1120 (skip-chars-forward " \t0-9")
1121 (f90-looking-at-program-block-start))
1123 ;; No explicit PROGRAM start statement.
1124 f90-program-indent))
1125 (setq cont (f90-present-statement-cont))
1126 (if (eq cont 'end)
1127 (while (not (eq 'begin (f90-present-statement-cont)))
1128 (f90-previous-statement)))
1129 (cond ((eq cont 'begin)
1130 (setq icol (+ (f90-current-indentation)
1131 f90-continuation-indent)))
1132 ((eq cont 'middle) (setq icol (current-indentation)))
1133 (t (setq icol (f90-current-indentation))
1134 (skip-chars-forward " \t")
1135 (if (looking-at "[0-9]")
1136 (setq icol (f90-get-correct-indent))
1137 (cond ((or (f90-looking-at-if-then)
1138 (f90-looking-at-where-or-forall)
1139 (f90-looking-at-select-case)
1140 (looking-at f90-else-like-re))
1141 (setq icol (+ icol f90-if-indent)))
1142 ((f90-looking-at-do)
1143 (setq icol (+ icol f90-do-indent)))
1144 ((f90-looking-at-type-like)
1145 (setq icol (+ icol f90-type-indent)))
1146 ((or (f90-looking-at-program-block-start)
1147 (looking-at "contains[ \t]*\\($\\|!\\)"))
1148 (setq icol (+ icol f90-program-indent)))))
1149 (goto-char pnt)
1150 (beginning-of-line)
1151 (cond ((looking-at "[ \t]*$"))
1152 ((looking-at "[ \t]*#") ; check for cpp directive
1153 (setq icol 0))
1155 (skip-chars-forward " \t0-9")
1156 (cond ((or (looking-at f90-else-like-re)
1157 (looking-at f90-end-if-re))
1158 (setq icol (- icol f90-if-indent)))
1159 ((looking-at "end[ \t]*do\\>")
1160 (setq icol (- icol f90-do-indent)))
1161 ((looking-at f90-end-type-re)
1162 (setq icol (- icol f90-type-indent)))
1163 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1164 (f90-looking-at-program-block-end))
1165 (setq icol (- icol f90-program-indent))))))
1166 ))))
1167 icol))
1169 (defun f90-previous-statement ()
1170 "Move point to beginning of the previous F90 statement.
1171 If no previous statement is found (i.e. if called from the first
1172 statement in the buffer), move to the start of the buffer and
1173 return nil. A statement is a line which is neither blank nor a
1174 comment."
1175 (interactive)
1176 (let (not-first-statement)
1177 (beginning-of-line)
1178 (while (and (setq not-first-statement (zerop (forward-line -1)))
1179 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1180 not-first-statement))
1182 (defun f90-next-statement ()
1183 "Move point to beginning of the next F90 statement.
1184 Return nil if no later statement is found."
1185 (interactive)
1186 (let (not-last-statement)
1187 (beginning-of-line)
1188 (while (and (setq not-last-statement
1189 (and (zerop (forward-line 1))
1190 (not (eobp))))
1191 (looking-at "[ \t0-9]*\\(!\\|$\\)")))
1192 not-last-statement))
1194 (defun f90-beginning-of-subprogram ()
1195 "Move point to the beginning of the current subprogram.
1196 Return (TYPE NAME), or nil if not found."
1197 (interactive)
1198 (let ((count 1) (case-fold-search t) matching-beg)
1199 (beginning-of-line)
1200 (while (and (> count 0)
1201 (re-search-backward f90-program-block-re nil 'move))
1202 (beginning-of-line)
1203 (skip-chars-forward " \t0-9")
1204 (cond ((setq matching-beg (f90-looking-at-program-block-start))
1205 (setq count (1- count)))
1206 ((f90-looking-at-program-block-end)
1207 (setq count (1+ count)))))
1208 (beginning-of-line)
1209 (if (zerop count)
1210 matching-beg
1211 ;; Note this includes the case of an un-named main program,
1212 ;; in which case we go to (point-min).
1213 (message "No beginning found.")
1214 nil)))
1216 (defun f90-end-of-subprogram ()
1217 "Move point to the end of the current subprogram.
1218 Return (TYPE NAME), or nil if not found."
1219 (interactive)
1220 (let ((case-fold-search t)
1221 (count 1)
1222 matching-end)
1223 (end-of-line)
1224 (while (and (> count 0)
1225 (re-search-forward f90-program-block-re nil 'move))
1226 (beginning-of-line)
1227 (skip-chars-forward " \t0-9")
1228 (cond ((f90-looking-at-program-block-start)
1229 (setq count (1+ count)))
1230 ((setq matching-end (f90-looking-at-program-block-end))
1231 (setq count (1- count))))
1232 (end-of-line))
1233 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1234 ;; has a net non-zero effect, which seems odd.
1235 ;;; (forward-line 1)
1236 (if (zerop count)
1237 matching-end
1238 (message "No end found.")
1239 nil)))
1242 (defun f90-end-of-block (&optional num)
1243 "Move point forward to the end of the current code block.
1244 With optional argument NUM, go forward that many balanced blocks.
1245 If NUM is negative, go backward to the start of a block. Checks
1246 for consistency of block types and labels (if present), and
1247 completes outermost block if `f90-smart-end' is non-nil.
1248 Interactively, pushes mark before moving point."
1249 (interactive "p")
1250 (if (interactive-p) (push-mark (point) t)) ; can move some distance
1251 (and num (< num 0) (f90-beginning-of-block (- num)))
1252 (let ((f90-smart-end (if f90-smart-end 'no-blink)) ; for final match-end
1253 (case-fold-search t)
1254 (count (or num 1))
1255 start-list start-this start-type start-label end-type end-label)
1256 (end-of-line) ; probably want this
1257 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1258 (beginning-of-line)
1259 (skip-chars-forward " \t0-9")
1260 (cond ((or (f90-in-string) (f90-in-comment)))
1261 ((setq start-this
1263 (f90-looking-at-do)
1264 (f90-looking-at-select-case)
1265 (f90-looking-at-type-like)
1266 (f90-looking-at-program-block-start)
1267 (f90-looking-at-if-then)
1268 (f90-looking-at-where-or-forall)))
1269 (setq start-list (cons start-this start-list) ; not add-to-list!
1270 count (1+ count)))
1271 ((looking-at (concat "end[ \t]*" f90-blocks-re
1272 "[ \t]*\\(\\sw+\\)?"))
1273 (setq end-type (match-string 1)
1274 end-label (match-string 2)
1275 count (1- count))
1276 ;; Check any internal blocks.
1277 (when start-list
1278 (setq start-this (car start-list)
1279 start-list (cdr start-list)
1280 start-type (car start-this)
1281 start-label (cadr start-this))
1282 (or (f90-equal-symbols start-type end-type)
1283 (error "End type `%s' does not match start type `%s'"
1284 end-type start-type))
1285 (or (f90-equal-symbols start-label end-label)
1286 (error "End label `%s' does not match start label `%s'"
1287 end-label start-label)))))
1288 (end-of-line))
1289 (if (> count 0) (error "Missing block end"))
1290 ;; Check outermost block.
1291 (when f90-smart-end
1292 (save-excursion
1293 (beginning-of-line)
1294 (skip-chars-forward " \t0-9")
1295 (f90-match-end)))))
1297 (defun f90-beginning-of-block (&optional num)
1298 "Move point backwards to the start of the current code block.
1299 With optional argument NUM, go backward that many balanced blocks.
1300 If NUM is negative, go forward to the end of a block.
1301 Checks for consistency of block types and labels (if present).
1302 Does not check the outermost block, because it may be incomplete.
1303 Interactively, pushes mark before moving point."
1304 (interactive "p")
1305 (if (interactive-p) (push-mark (point) t))
1306 (and num (< num 0) (f90-end-of-block (- num)))
1307 (let ((case-fold-search t)
1308 (count (or num 1))
1309 end-list end-this end-type end-label
1310 start-this start-type start-label)
1311 (beginning-of-line) ; probably want this
1312 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1313 (beginning-of-line)
1314 (skip-chars-forward " \t0-9")
1315 (cond ((or (f90-in-string) (f90-in-comment)))
1316 ((looking-at (concat "end[ \t]*" f90-blocks-re
1317 "[ \t]*\\(\\sw+\\)?"))
1318 (setq end-list (cons (list (match-string 1) (match-string 2))
1319 end-list)
1320 count (1+ count)))
1321 ((setq start-this
1323 (f90-looking-at-do)
1324 (f90-looking-at-select-case)
1325 (f90-looking-at-type-like)
1326 (f90-looking-at-program-block-start)
1327 (f90-looking-at-if-then)
1328 (f90-looking-at-where-or-forall)))
1329 (setq start-type (car start-this)
1330 start-label (cadr start-this)
1331 count (1- count))
1332 ;; Check any internal blocks.
1333 (when end-list
1334 (setq end-this (car end-list)
1335 end-list (cdr end-list)
1336 end-type (car end-this)
1337 end-label (cadr end-this))
1338 (or (f90-equal-symbols start-type end-type)
1339 (error "Start type `%s' does not match end type `%s'"
1340 start-type end-type))
1341 (or (f90-equal-symbols start-label end-label)
1342 (error "Start label `%s' does not match end label `%s'"
1343 start-label end-label))))))
1344 ;; Includes an un-named main program block.
1345 (if (> count 0) (error "Missing block start"))))
1347 (defun f90-next-block (&optional num)
1348 "Move point forward to the next end or start of a code block.
1349 With optional argument NUM, go forward that many blocks.
1350 If NUM is negative, go backwards.
1351 A block is a subroutine, if-endif, etc."
1352 (interactive "p")
1353 (let ((case-fold-search t)
1354 (count (if num (abs num) 1)))
1355 (while (and (> count 0)
1356 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1357 (re-search-backward f90-blocks-re nil 'move)))
1358 (beginning-of-line)
1359 (skip-chars-forward " \t0-9")
1360 (cond ((or (f90-in-string) (f90-in-comment)))
1361 ((or
1362 (looking-at "end[ \t]*")
1363 (f90-looking-at-do)
1364 (f90-looking-at-select-case)
1365 (f90-looking-at-type-like)
1366 (f90-looking-at-program-block-start)
1367 (f90-looking-at-if-then)
1368 (f90-looking-at-where-or-forall))
1369 (setq count (1- count))))
1370 (if (> num 0) (end-of-line)
1371 (beginning-of-line)))))
1374 (defun f90-previous-block (&optional num)
1375 "Move point backward to the previous end or start of a code block.
1376 With optional argument NUM, go backward that many blocks.
1377 If NUM is negative, go forwards.
1378 A block is a subroutine, if-endif, etc."
1379 (interactive "p")
1380 (f90-next-block (- (or num 1))))
1383 (defun f90-mark-subprogram ()
1384 "Put mark at end of F90 subprogram, point at beginning, push mark."
1385 (interactive)
1386 (let ((pos (point)) program)
1387 (f90-end-of-subprogram)
1388 (push-mark)
1389 (goto-char pos)
1390 (setq program (f90-beginning-of-subprogram))
1391 (if (fboundp 'zmacs-activate-region)
1392 (zmacs-activate-region)
1393 (setq mark-active t
1394 deactivate-mark nil))
1395 program))
1397 (defun f90-comment-region (beg-region end-region)
1398 "Comment/uncomment every line in the region.
1399 Insert the variable `f90-comment-region' at the start of every line
1400 in the region, or, if already present, remove it."
1401 (interactive "*r")
1402 (let ((end (copy-marker end-region)))
1403 (goto-char beg-region)
1404 (beginning-of-line)
1405 (if (looking-at (regexp-quote f90-comment-region))
1406 (delete-region (point) (match-end 0))
1407 (insert f90-comment-region))
1408 (while (and (zerop (forward-line 1))
1409 (< (point) end))
1410 (if (looking-at (regexp-quote f90-comment-region))
1411 (delete-region (point) (match-end 0))
1412 (insert f90-comment-region)))
1413 (set-marker end nil)))
1415 (defun f90-indent-line (&optional no-update)
1416 "Indent current line as F90 code.
1417 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1418 after indenting."
1419 (interactive "*P")
1420 (let ((case-fold-search t)
1421 (pos (point-marker))
1422 indent no-line-number)
1423 (beginning-of-line) ; digits after & \n are not line-nos
1424 (if (not (save-excursion (and (f90-previous-statement)
1425 (f90-line-continued))))
1426 (f90-indent-line-no)
1427 (setq no-line-number t)
1428 (skip-chars-forward " \t"))
1429 (if (looking-at "!")
1430 (setq indent (f90-comment-indent))
1431 (and f90-smart-end (looking-at "end")
1432 (f90-match-end))
1433 (setq indent (f90-calculate-indent)))
1434 (or (= indent (current-column))
1435 (f90-indent-to indent no-line-number))
1436 ;; If initial point was within line's indentation,
1437 ;; position after the indentation. Else stay at same point in text.
1438 (and (< (point) pos)
1439 (goto-char pos))
1440 (if auto-fill-function
1441 ;; GM NO-UPDATE not honoured, since this calls f90-update-line.
1442 (f90-do-auto-fill)
1443 (or no-update (f90-update-line)))
1444 (set-marker pos nil)))
1446 (defun f90-indent-new-line ()
1447 "Re-indent current line, insert a newline and indent the newline.
1448 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1449 If run in the middle of a line, the line is not broken."
1450 (interactive "*")
1451 (if abbrev-mode (expand-abbrev))
1452 (beginning-of-line) ; reindent where likely to be needed
1453 (f90-indent-line) ; calls indent-line-no, update-line
1454 (end-of-line)
1455 (delete-horizontal-space) ; destroy trailing whitespace
1456 (let ((string (f90-in-string))
1457 (cont (f90-line-continued)))
1458 (and string (not cont) (insert "&"))
1459 (newline)
1460 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
1461 (f90-indent-line 'no-update)) ; nothing to update
1464 ;; TODO not add spaces to empty lines at the start.
1465 ;; Why is second line getting extra indent over first?
1466 (defun f90-indent-region (beg-region end-region)
1467 "Indent every line in region by forward parsing."
1468 (interactive "*r")
1469 (let ((end-region-mark (copy-marker end-region))
1470 (save-point (point-marker))
1471 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
1472 (goto-char beg-region)
1473 ;; First find a line which is not a continuation line or comment.
1474 (beginning-of-line)
1475 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1476 (progn (f90-indent-line 'no-update)
1477 (zerop (forward-line 1)))
1478 (< (point) end-region-mark)))
1479 (setq cont (f90-present-statement-cont))
1480 (while (and (or (eq cont 'middle) (eq cont 'end))
1481 (f90-previous-statement))
1482 (setq cont (f90-present-statement-cont)))
1483 ;; Process present line for beginning of block.
1484 (setq f90-cache-position (point))
1485 (f90-indent-line 'no-update)
1486 (setq ind-lev (f90-current-indentation)
1487 ind-curr ind-lev)
1488 (beginning-of-line)
1489 (skip-chars-forward " \t0-9")
1490 (setq struct nil
1491 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1492 ((or (setq struct (f90-looking-at-if-then))
1493 (setq struct (f90-looking-at-select-case))
1494 (setq struct (f90-looking-at-where-or-forall))
1495 (looking-at f90-else-like-re))
1496 f90-if-indent)
1497 ((setq struct (f90-looking-at-type-like))
1498 f90-type-indent)
1499 ((or (setq struct (f90-looking-at-program-block-start))
1500 (looking-at "contains[ \t]*\\($\\|!\\)"))
1501 f90-program-indent)))
1502 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1503 (if struct (setq block-list (cons struct block-list)))
1504 (while (and (f90-line-continued) (zerop (forward-line 1))
1505 (< (point) end-region-mark))
1506 (if (looking-at "[ \t]*!")
1507 (f90-indent-to (f90-comment-indent))
1508 (or (= (current-indentation)
1509 (+ ind-curr f90-continuation-indent))
1510 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1511 ;; Process all following lines.
1512 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1513 (beginning-of-line)
1514 (f90-indent-line-no)
1515 (setq f90-cache-position (point))
1516 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1517 ((looking-at "[ \t]*#") (setq ind-curr 0))
1518 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1519 ((f90-no-block-limit) (setq ind-curr ind-lev))
1520 ((looking-at f90-else-like-re) (setq ind-curr
1521 (- ind-lev f90-if-indent)))
1522 ((looking-at "contains[ \t]*\\($\\|!\\)")
1523 (setq ind-curr (- ind-lev f90-program-indent)))
1524 ((setq ind-b
1525 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1526 ((or (setq struct (f90-looking-at-if-then))
1527 (setq struct (f90-looking-at-select-case))
1528 (setq struct (f90-looking-at-where-or-forall)))
1529 f90-if-indent)
1530 ((setq struct (f90-looking-at-type-like))
1531 f90-type-indent)
1532 ((setq struct (f90-looking-at-program-block-start))
1533 f90-program-indent)))
1534 (setq ind-curr ind-lev)
1535 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1536 (setq block-list (cons struct block-list)))
1537 ((setq end-struct (f90-looking-at-program-block-end))
1538 (setq beg-struct (car block-list)
1539 block-list (cdr block-list))
1540 (if f90-smart-end
1541 (save-excursion
1542 (f90-block-match (car beg-struct) (car (cdr beg-struct))
1543 (car end-struct) (car (cdr end-struct)))))
1544 (setq ind-b
1545 (cond ((looking-at f90-end-if-re) f90-if-indent)
1546 ((looking-at "end[ \t]*do\\>") f90-do-indent)
1547 ((looking-at f90-end-type-re) f90-type-indent)
1548 ((f90-looking-at-program-block-end)
1549 f90-program-indent)))
1550 (if ind-b (setq ind-lev (- ind-lev ind-b)))
1551 (setq ind-curr ind-lev))
1552 (t (setq ind-curr ind-lev)))
1553 ;; Do the indentation if necessary.
1554 (or (= ind-curr (current-column))
1555 (f90-indent-to ind-curr))
1556 (while (and (f90-line-continued) (zerop (forward-line 1))
1557 (< (point) end-region-mark))
1558 (if (looking-at "[ \t]*!")
1559 (f90-indent-to (f90-comment-indent))
1560 (or (= (current-indentation)
1561 (+ ind-curr f90-continuation-indent))
1562 (f90-indent-to
1563 (+ ind-curr f90-continuation-indent) 'no-line-no)))))
1564 ;; Restore point, etc.
1565 (setq f90-cache-position nil)
1566 (goto-char save-point)
1567 (set-marker end-region-mark nil)
1568 (set-marker save-point nil)
1569 (if (fboundp 'zmacs-deactivate-region)
1570 (zmacs-deactivate-region)
1571 (deactivate-mark))))
1573 (defun f90-indent-subprogram ()
1574 "Properly indent the subprogram containing point."
1575 (interactive "*")
1576 (save-excursion
1577 (let ((program (f90-mark-subprogram)))
1578 (if program
1579 (progn
1580 (message "Indenting %s %s..."
1581 (car program) (car (cdr program)))
1582 (indent-region (point) (mark) nil)
1583 (message "Indenting %s %s...done"
1584 (car program) (car (cdr program))))
1585 (message "Indenting the whole file...")
1586 (indent-region (point) (mark) nil)
1587 (message "Indenting the whole file...done")))))
1589 (defun f90-break-line (&optional no-update)
1590 "Break line at point, insert continuation marker(s) and indent.
1591 Unless in a string or comment, or if the optional argument NO-UPDATE
1592 is non-nil, call `f90-update-line' after inserting the continuation marker."
1593 (interactive "*P")
1594 (cond ((f90-in-string)
1595 (insert "&\n&"))
1596 ((f90-in-comment)
1597 (delete-horizontal-space 'backwards) ; remove trailing whitespace
1598 (insert "\n" (f90-get-present-comment-type)))
1599 (t (insert "&")
1600 (or no-update (f90-update-line))
1601 (newline 1)
1602 (if f90-beginning-ampersand (insert "&"))))
1603 (indent-according-to-mode))
1605 (defun f90-find-breakpoint ()
1606 "From `fill-column', search backward for break-delimiter."
1607 (re-search-backward f90-break-delimiters (line-beginning-position))
1608 (if (not f90-break-before-delimiters)
1609 (forward-char (if (looking-at f90-no-break-re) 2 1))
1610 (backward-char)
1611 (or (looking-at f90-no-break-re)
1612 (forward-char))))
1614 (defun f90-do-auto-fill ()
1615 "Break line if non-white characters beyond `fill-column'.
1616 Update keyword case first."
1617 (interactive "*")
1618 ;; Break line before or after last delimiter (non-word char) if
1619 ;; position is beyond fill-column.
1620 ;; Will not break **, //, or => (as specified by f90-no-break-re).
1621 (f90-update-line)
1622 ;; Need this for `f90-electric-insert' and other f90- callers.
1623 (unless (and (boundp 'comment-auto-fill-only-comments)
1624 comment-auto-fill-only-comments
1625 (not (f90-in-comment)))
1626 (while (> (current-column) fill-column)
1627 (let ((pos-mark (point-marker)))
1628 (move-to-column fill-column)
1629 (or (f90-in-string) (f90-find-breakpoint))
1630 (f90-break-line)
1631 (goto-char pos-mark)
1632 (set-marker pos-mark nil)))))
1634 (defun f90-join-lines (&optional arg)
1635 "Join current line to previous, fix whitespace, continuation, comments.
1636 With optional argument ARG, join current line to following line.
1637 Like `join-line', but handles F90 syntax."
1638 (interactive "*P")
1639 (beginning-of-line)
1640 (if arg (forward-line 1))
1641 (when (eq (preceding-char) ?\n)
1642 (skip-chars-forward " \t")
1643 (if (looking-at "\&") (delete-char 1))
1644 (beginning-of-line)
1645 (delete-region (point) (1- (point)))
1646 (skip-chars-backward " \t")
1647 (and (eq (preceding-char) ?&) (delete-char -1))
1648 (and (f90-in-comment)
1649 (looking-at "[ \t]*!+")
1650 (replace-match ""))
1651 (or (f90-in-string)
1652 (fixup-whitespace))))
1654 (defun f90-fill-region (beg-region end-region)
1655 "Fill every line in region by forward parsing. Join lines if possible."
1656 (interactive "*r")
1657 (let ((end-region-mark (copy-marker end-region))
1658 (go-on t)
1659 f90-smart-end f90-auto-keyword-case auto-fill-function)
1660 (goto-char beg-region)
1661 (while go-on
1662 ;; Join as much as possible.
1663 (while (progn
1664 (end-of-line)
1665 (skip-chars-backward " \t")
1666 (eq (preceding-char) ?&))
1667 (f90-join-lines 'forward))
1668 ;; Chop the line if necessary.
1669 (while (> (save-excursion (end-of-line) (current-column))
1670 fill-column)
1671 (move-to-column fill-column)
1672 (f90-find-breakpoint)
1673 (f90-break-line 'no-update))
1674 (setq go-on (and (< (point) end-region-mark)
1675 (zerop (forward-line 1)))
1676 f90-cache-position (point)))
1677 (setq f90-cache-position nil)
1678 (set-marker end-region-mark nil)
1679 (if (fboundp 'zmacs-deactivate-region)
1680 (zmacs-deactivate-region)
1681 (deactivate-mark))))
1683 (defun f90-block-match (beg-block beg-name end-block end-name)
1684 "Match end-struct with beg-struct and complete end-block if possible.
1685 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
1686 BEG-NAME is the block start name (may be nil).
1687 END-BLOCK is the type of block as indicated at the end (may be nil).
1688 END-NAME is the block end name (may be nil).
1689 Leave point at the end of line."
1690 ;; Hack to deal with the case when this is called from
1691 ;; f90-indent-region on a program block without an explicit PROGRAM
1692 ;; statement at the start. Should really be an error (?).
1693 (or beg-block (setq beg-block "program"))
1694 (search-forward "end" (line-end-position))
1695 (catch 'no-match
1696 (if (and end-block (f90-equal-symbols beg-block end-block))
1697 (search-forward end-block)
1698 (if end-block
1699 (progn
1700 (message "END %s does not match %s." end-block beg-block)
1701 (end-of-line)
1702 (throw 'no-match nil))
1703 (message "Inserting %s." beg-block)
1704 (insert (concat " " beg-block))))
1705 (if (f90-equal-symbols beg-name end-name)
1706 (and end-name (search-forward end-name))
1707 (cond ((and beg-name (not end-name))
1708 (message "Inserting %s." beg-name)
1709 (insert (concat " " beg-name)))
1710 ((and beg-name end-name)
1711 (message "Replacing %s with %s." end-name beg-name)
1712 (search-forward end-name)
1713 (replace-match beg-name))
1714 ((and (not beg-name) end-name)
1715 (message "Deleting %s." end-name)
1716 (search-forward end-name)
1717 (replace-match ""))))
1718 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
1720 (defun f90-match-end ()
1721 "From an end block statement, find the corresponding block and name."
1722 (interactive)
1723 (let ((count 1)
1724 (top-of-window (window-start))
1725 (end-point (point))
1726 (case-fold-search t)
1727 matching-beg beg-name end-name beg-block end-block end-struct)
1728 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
1729 (setq end-struct (f90-looking-at-program-block-end)))
1730 (setq end-block (car end-struct)
1731 end-name (car (cdr end-struct)))
1732 (save-excursion
1733 (beginning-of-line)
1734 (while (and (> count 0)
1735 (not (= (line-beginning-position) (point-min))))
1736 (re-search-backward f90-blocks-re nil 'move)
1737 (beginning-of-line)
1738 ;; GM not a line number if continued line.
1739 ;;; (skip-chars-forward " \t")
1740 ;;; (skip-chars-forward "0-9")
1741 (skip-chars-forward " \t0-9")
1742 (cond ((or (f90-in-string) (f90-in-comment)))
1743 ((setq matching-beg
1745 (f90-looking-at-do)
1746 (f90-looking-at-if-then)
1747 (f90-looking-at-where-or-forall)
1748 (f90-looking-at-select-case)
1749 (f90-looking-at-type-like)
1750 (f90-looking-at-program-block-start)
1751 ;; Interpret a single END without a block
1752 ;; start to be the END of a program block
1753 ;; without an initial PROGRAM line.
1754 (if (= (line-beginning-position) (point-min))
1755 '("program" nil))))
1756 (setq count (1- count)))
1757 ((looking-at (concat "end[ \t]*" f90-blocks-re))
1758 (setq count (1+ count)))))
1759 (if (> count 0)
1760 (message "No matching beginning.")
1761 (f90-update-line)
1762 (if (eq f90-smart-end 'blink)
1763 (if (< (point) top-of-window)
1764 (message "Matches %s: %s"
1765 (what-line)
1766 (buffer-substring
1767 (line-beginning-position)
1768 (line-end-position)))
1769 (sit-for 1)))
1770 (setq beg-block (car matching-beg)
1771 beg-name (car (cdr matching-beg)))
1772 (goto-char end-point)
1773 (beginning-of-line)
1774 (f90-block-match beg-block beg-name end-block end-name))))))
1776 (defun f90-insert-end ()
1777 "Insert a complete end statement matching beginning of present block."
1778 (interactive "*")
1779 (let ((f90-smart-end (or f90-smart-end 'blink)))
1780 (insert "end")
1781 (f90-indent-new-line)))
1783 ;; Abbrevs and keywords.
1785 (defun f90-abbrev-start ()
1786 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
1787 Any other key combination is executed normally."
1788 (interactive "*")
1789 (insert last-command-char)
1790 (let (char event)
1791 (if (fboundp 'next-command-event) ; XEmacs
1792 (setq event (next-command-event)
1793 char (event-to-character event))
1794 (setq event (read-event)
1795 char event))
1796 ;; Insert char if not equal to `?', or if abbrev-mode is off.
1797 (if (and abbrev-mode (or (eq char ??) (eq char help-char)))
1798 (f90-abbrev-help)
1799 (setq unread-command-events (list event)))))
1801 (defun f90-abbrev-help ()
1802 "List the currently defined abbrevs in F90 mode."
1803 (interactive)
1804 (message "Listing abbrev table...")
1805 (display-buffer (f90-prepare-abbrev-list-buffer))
1806 (message "Listing abbrev table...done"))
1808 (defun f90-prepare-abbrev-list-buffer ()
1809 "Create a buffer listing the F90 mode abbreviations."
1810 (save-excursion
1811 (set-buffer (get-buffer-create "*Abbrevs*"))
1812 (erase-buffer)
1813 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
1814 (goto-char (point-min))
1815 (set-buffer-modified-p nil)
1816 (edit-abbrevs-mode))
1817 (get-buffer-create "*Abbrevs*"))
1819 (defun f90-upcase-keywords ()
1820 "Upcase all F90 keywords in the buffer."
1821 (interactive "*")
1822 (f90-change-keywords 'upcase-word))
1824 (defun f90-capitalize-keywords ()
1825 "Capitalize all F90 keywords in the buffer."
1826 (interactive "*")
1827 (f90-change-keywords 'capitalize-word))
1829 (defun f90-downcase-keywords ()
1830 "Downcase all F90 keywords in the buffer."
1831 (interactive "*")
1832 (f90-change-keywords 'downcase-word))
1834 (defun f90-upcase-region-keywords (beg end)
1835 "Upcase all F90 keywords in the region."
1836 (interactive "*r")
1837 (f90-change-keywords 'upcase-word beg end))
1839 (defun f90-capitalize-region-keywords (beg end)
1840 "Capitalize all F90 keywords in the region."
1841 (interactive "*r")
1842 (f90-change-keywords 'capitalize-word beg end))
1844 (defun f90-downcase-region-keywords (beg end)
1845 "Downcase all F90 keywords in the region."
1846 (interactive "*r")
1847 (f90-change-keywords 'downcase-word beg end))
1849 ;; Change the keywords according to argument.
1850 (defun f90-change-keywords (change-word &optional beg end)
1851 "Change the case of F90 keywords in the region (if specified) or buffer.
1852 CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
1853 (save-excursion
1854 (setq beg (or beg (point-min))
1855 end (or end (point-max)))
1856 (let ((keyword-re
1857 (concat "\\("
1858 f90-keywords-re "\\|" f90-procedures-re "\\|"
1859 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
1860 (ref-point (point-min))
1861 (modified (buffer-modified-p))
1862 state saveword back-point)
1863 (goto-char beg)
1864 (unwind-protect
1865 (while (re-search-forward keyword-re end t)
1866 (unless (progn
1867 (setq state (parse-partial-sexp ref-point (point)))
1868 (or (nth 3 state) (nth 4 state)
1869 ;; GM f90-directive-comment-re?
1870 (save-excursion ; check for cpp directive
1871 (beginning-of-line)
1872 (skip-chars-forward " \t0-9")
1873 (looking-at "#"))))
1874 (setq ref-point (point)
1875 back-point (save-excursion (backward-word 1) (point))
1876 saveword (buffer-substring back-point ref-point))
1877 (funcall change-word -1)
1878 (or (string= saveword (buffer-substring back-point ref-point))
1879 (setq modified t))))
1880 (or modified (set-buffer-modified-p nil))))))
1883 (defun f90-current-defun ()
1884 "Function to use for `add-log-current-defun-function' in F90 mode."
1885 (save-excursion
1886 (nth 1 (f90-beginning-of-subprogram))))
1889 (defun f90-backslash-not-special (&optional all)
1890 "Make the backslash character (\\) be non-special in the current buffer.
1891 With optional argument ALL, change the default for all present
1892 and future F90 buffers. F90 mode normally treats backslash as an
1893 escape character."
1894 (or (eq major-mode 'f90-mode)
1895 (error "This function should only be used in F90 buffers"))
1896 (when (equal (char-syntax ?\\ ) ?\\ )
1897 (or all (set-syntax-table (copy-syntax-table (syntax-table))))
1898 (modify-syntax-entry ?\\ ".")))
1901 (provide 'f90)
1903 ;;; arch-tag: fceac97c-c147-44bd-aec0-172d4b560ef8
1904 ;;; f90.el ends here