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 XEmacs.
11 ;; XEmacs 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)
16 ;; XEmacs 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 XEmacs; 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 ;;; Synched up with: GNU Emacs 21.3
30 ;; Major mode for editing F90 programs in FREE FORMAT.
31 ;; The minor language revision F95 is also supported (with font-locking).
33 ;; Knows about continuation lines, named structured statements, and other
34 ;; features in F90 including HPF (High Performance Fortran) structures.
35 ;; The basic feature provides accurate indentation of F90 programs.
36 ;; In addition, there are many more features like automatic matching of all
37 ;; end statements, an auto-fill function to break long lines, a join-lines
38 ;; function which joins continued lines, etc.
40 ;; To facilitate typing, a fairly complete list of abbreviations is provided.
41 ;; All abbreviations begin with the backquote character "`"
42 ;; (this requires modification of the syntax-table).
43 ;; For example, `i expands to integer (if abbrev-mode is on).
45 ;; There are two separate features for altering the appearance of code:
46 ;; 1) Upcasing or capitalizing of all keywords.
47 ;; 2) Colors/fonts using font-lock-mode.
48 ;; Automatic upcase or downcase of keywords is controlled by the variable
49 ;; f90-auto-keyword-case.
51 ;; The indentations of lines starting with ! is determined by the first of the
52 ;; following matches (values in the left column are the defaults):
54 ;; start-string/regexp indent variable holding start-string/regexp
56 ;; !hpf\\$ (re) 0 f90-directive-comment-re
57 ;; !!$ 0 f90-comment-region
58 ;; ! (re) as code f90-indented-comment-re
59 ;; default comment-column
61 ;; Ex: Here is the result of 3 different settings of f90-indented-comment-re
62 ;; f90-indented-comment-re !-indentation !!-indentation
64 ;; !! comment-column as code
65 ;; ![^!] as code comment-column
66 ;; Trailing comments are indented to comment-column with indent-for-comment.
67 ;; The function f90-comment-region toggles insertion of
68 ;; the variable f90-comment-region in every line of the region.
70 ;; One common convention for free vs. fixed format is that free format files
71 ;; have the ending .f90 or .f95 while fixed format files have the ending .f.
72 ;; Emacs automatically loads Fortran files in the appropriate mode based
73 ;; on extension. You can modify this by adjusting the variable auto-mode-alist.
75 ;; (add-to-list 'auto-mode-alist '("\\.f\\'" . f90-mode))
77 ;; Once you have entered f90-mode, you may get more info by using
78 ;; the command describe-mode (C-h m). For online help use
79 ;; C-h f <Name of function you want described>, or
80 ;; C-h v <Name of variable you want described>.
82 ;; To customize f90-mode for your taste, use, for example:
83 ;; (you don't have to specify values for all the parameters below)
85 ;;(add-hook 'f90-mode-hook
86 ;; ;; These are the default values.
87 ;; '(lambda () (setq f90-do-indent 3
90 ;; f90-program-indent 2
91 ;; f90-continuation-indent 5
92 ;; f90-comment-region "!!$"
93 ;; f90-directive-comment-re "!hpf\\$"
94 ;; f90-indented-comment-re "!"
95 ;; f90-break-delimiters "[-+\\*/><=,% \t]"
96 ;; f90-break-before-delimiters t
97 ;; f90-beginning-ampersand t
98 ;; f90-smart-end 'blink
99 ;; f90-auto-keyword-case nil
100 ;; f90-leave-line-no nil
101 ;; indent-tabs-mode nil
102 ;; f90-font-lock-keywords f90-font-lock-keywords-2
104 ;; ;; These are not default.
105 ;; (abbrev-mode 1) ; turn on abbreviation mode
106 ;; (f90-add-imenu-menu) ; extra menu with functions etc.
107 ;; (if f90-auto-keyword-case ; change case of all keywords on startup
108 ;; (f90-change-keywords f90-auto-keyword-case))
111 ;; in your .emacs file. You can also customize the lists
112 ;; f90-font-lock-keywords, etc.
114 ;; The auto-fill and abbreviation minor modes are accessible from the F90 menu,
115 ;; or by using M-x auto-fill-mode and M-x abbrev-mode, respectively.
118 ;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
119 ;; non-nil, the line numbers are never touched.
120 ;; 2) Multi-; statements like "do i=1,20 ; j=j+i ; end do" are not handled
121 ;; correctly, but I imagine them to be rare.
122 ;; 3) Regexps for hilit19 are no longer supported.
123 ;; 4) For FIXED FORMAT code, use fortran mode.
124 ;; 5) This mode does not work under emacs-18.x.
125 ;; 6) Preprocessor directives, i.e., lines starting with # are left-justified
126 ;; and are untouched by all case-changing commands. There is, at present, no
127 ;; mechanism for treating multi-line directives (continued by \ ).
128 ;; 7) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
129 ;; You are urged to use f90-do loops (with labels if you wish).
130 ;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
132 ;; List of user commands
133 ;; f90-previous-statement f90-next-statement
134 ;; f90-beginning-of-subprogram f90-end-of-subprogram f90-mark-subprogram
135 ;; f90-comment-region
136 ;; f90-indent-line f90-indent-new-line
137 ;; f90-indent-region (can be called by calling indent-region)
138 ;; f90-indent-subprogram
139 ;; f90-break-line f90-join-lines
142 ;; f90-upcase-keywords f90-upcase-region-keywords
143 ;; f90-downcase-keywords f90-downcase-region-keywords
144 ;; f90-capitalize-keywords f90-capitalize-region-keywords
145 ;; f90-add-imenu-menu
146 ;; f90-font-lock-1, f90-font-lock-2, f90-font-lock-3, f90-font-lock-4
148 ;; Original author's thanks
149 ;; Thanks to all the people who have tested the mode. Special thanks to Jens
150 ;; Bloch Helmers for encouraging me to write this code, for creative
151 ;; suggestions as well as for the lists of hpf-commands.
152 ;; Also thanks to the authors of the fortran and pascal modes, on which some
153 ;; of this code is built.
158 ;; Support for hideshow, align.
159 ;; OpenMP, preprocessor highlighting.
161 (defvar comment-auto-fill-only-comments)
162 (defvar font-lock-keywords)
167 "Major mode for editing free format Fortran 90,95 code."
170 (defgroup f90-indent nil
171 "Indentation in free format Fortran."
176 (defcustom f90-do-indent 3
177 "*Extra indentation applied to DO blocks."
181 (defcustom f90-if-indent 3
182 "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
186 (defcustom f90-type-indent 3
187 "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks."
191 (defcustom f90-program-indent 2
192 "*Extra indentation applied to PROGRAM, MODULE, SUBROUTINE, FUNCTION blocks."
196 (defcustom f90-continuation-indent 5
197 "*Extra indentation applied to continuation lines."
201 (defcustom f90-comment-region "!!$"
202 "*String inserted by \\[f90-comment-region] at start of each line in region."
206 (defcustom f90-indented-comment-re "!"
207 "*Regexp matching comments to indent as code."
211 (defcustom f90-directive-comment-re "!hpf\\$"
212 "*Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
216 (defcustom f90-beginning-ampersand t
217 "*Non-nil gives automatic insertion of \& at start of continuation line."
221 (defcustom f90-smart-end 'blink
222 "*From an END statement, check and fill the end using matching block start.
223 Allowed values are 'blink, 'no-blink, and nil, which determine
224 whether to blink the matching beginning."
225 :type '(choice (const blink) (const no-blink) (const nil))
228 (defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
229 "*Regexp matching delimiter characters at which lines may be broken.
230 There are certain tokens comprised entirely of characters
231 matching this regexp that should not be split, and these are
232 specified by the constant `f90-no-break-re'."
236 (defcustom f90-break-before-delimiters t
237 "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
241 (defcustom f90-auto-keyword-case nil
242 "*Automatic case conversion of keywords.
243 The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil."
244 :type '(choice (const downcase-word) (const upcase-word)
245 (const capitalize-word) (const nil))
248 (defcustom f90-leave-line-no nil
249 "*If non-nil, line numbers are not left justified."
253 (defcustom f90-mode-hook nil
254 "Hook run when entering F90 mode."
256 :options '(f90-add-imenu-menu)
259 ;; User options end here.
261 (defconst f90-keywords-re
262 (regexp-opt '("allocatable" "allocate" "assign" "assignment" "backspace"
263 "block" "call" "case" "character" "close" "common" "complex"
264 "contains" "continue" "cycle" "data" "deallocate"
265 "dimension" "do" "double" "else" "elseif" "elsewhere" "end"
266 "enddo" "endfile" "endif" "entry" "equivalence" "exit"
267 "external" "forall" "format" "function" "goto" "if"
268 "implicit" "include" "inquire" "integer" "intent"
269 "interface" "intrinsic" "logical" "module" "namelist" "none"
270 "nullify" "only" "open" "operator" "optional" "parameter"
271 "pause" "pointer" "precision" "print" "private" "procedure"
272 "program" "public" "read" "real" "recursive" "result" "return"
273 "rewind" "save" "select" "sequence" "stop" "subroutine"
274 "target" "then" "type" "use" "where" "while" "write"
276 "elemental" "pure") 'words)
277 "Regexp for F90 keywords.")
279 (defconst f90-keywords-level-3-re
281 '("allocatable" "allocate" "assign" "assignment" "backspace"
282 "close" "deallocate" "dimension" "endfile" "entry" "equivalence"
283 "external" "inquire" "intent" "intrinsic" "nullify" "only" "open"
284 "operator" "optional" "parameter" "pause" "pointer" "print" "private"
285 "public" "read" "recursive" "result" "rewind" "save" "select"
286 "sequence" "target" "write"
288 "elemental" "pure") 'words)
289 "Keyword-regexp for font-lock level >= 3.")
291 (defconst f90-procedures-re
294 '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
295 "all" "allocated" "anint" "any" "asin" "associated"
296 "atan" "atan2" "bit_size" "btest" "ceiling" "char" "cmplx"
297 "conjg" "cos" "cosh" "count" "cshift" "date_and_time" "dble"
298 "digits" "dim" "dot_product" "dprod" "eoshift" "epsilon"
299 "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
300 "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior"
301 "ishft" "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt"
302 "lle" "llt" "log" "log10" "logical" "matmul" "max"
303 "maxexponent" "maxloc" "maxval" "merge" "min" "minexponent"
304 "minloc" "minval" "mod" "modulo" "mvbits" "nearest" "nint"
305 "not" "pack" "precision" "present" "product" "radix"
306 ;; Real is taken out here to avoid highlighting declarations.
307 "random_number" "random_seed" "range" ;; "real"
308 "repeat" "reshape" "rrspacing" "scale" "scan"
309 "selected_int_kind" "selected_real_kind" "set_exponent"
310 "shape" "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt"
311 "sum" "system_clock" "tan" "tanh" "tiny" "transfer"
312 "transpose" "trim" "ubound" "unpack" "verify"
313 ;; F95 intrinsic functions.
314 "null" "cpu_time") t)
315 ;; A left parenthesis to avoid highlighting non-procedures.
317 "Regexp whose first part matches F90 intrinsic procedures.")
319 (defconst f90-operators-re
321 (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
322 "neqv" "not" "or" "true") t)
324 "Regexp matching intrinsic operators.")
326 (defconst f90-hpf-keywords-re
328 ;; Intrinsic procedures.
329 '("all_prefix" "all_scatter" "all_suffix" "any_prefix"
330 "any_scatter" "any_suffix" "copy_prefix" "copy_scatter"
331 "copy_suffix" "count_prefix" "count_scatter" "count_suffix"
332 "grade_down" "grade_up"
333 "hpf_alignment" "hpf_distribution" "hpf_template" "iall" "iall_prefix"
334 "iall_scatter" "iall_suffix" "iany" "iany_prefix" "iany_scatter"
335 "iany_suffix" "ilen" "iparity" "iparity_prefix"
336 "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
337 "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter"
338 "minval_suffix" "number_of_processors" "parity"
339 "parity_prefix" "parity_scatter" "parity_suffix" "popcnt" "poppar"
340 "processors_shape" "product_prefix" "product_scatter"
341 "product_suffix" "sum_prefix" "sum_scatter" "sum_suffix"
343 "align" "distribute" "dynamic" "independent" "inherit" "processors"
344 "realign" "redistribute" "template"
346 "block" "cyclic" "extrinsic" "new" "onto" "pure" "with") 'words)
347 "Regexp for all HPF keywords, procedures and directives.")
349 ;; Highlighting patterns.
351 (defvar f90-font-lock-keywords-1
353 ;; Special highlighting of "module procedure".
354 '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face))
355 ;; Highlight definition of derived type.
356 '("\\<\\(\\(?:end[ \t]*\\)?type\\)\\>\\([^()\n]*::\\)?[ \t]*\\(\\sw+\\)"
357 (1 font-lock-keyword-face) (3 font-lock-function-name-face))
358 ;; Other functions and declarations.
359 '("\\<\\(\\(?:end[ \t]*\\)?\\(program\\|module\\|function\\|\
360 subroutine\\)\\|use\\|call\\)\\>[ \t]*\\(\\sw+\\)?"
361 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
362 "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
363 "This does fairly subdued highlighting of comments and function calls.")
365 (defvar f90-font-lock-keywords-2
367 f90-font-lock-keywords-1
369 ;; Variable declarations (avoid the real function call).
370 '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
371 logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\([^&!\n]*\\)"
372 (1 font-lock-type-face t) (4 font-lock-variable-name-face t))
373 ;; do, if, select, where, and forall constructs.
374 '("\\<\\(end[ \t]*\\(do\\|if\\|select\\|forall\\|where\\)\\)\\>\
375 \\([ \t]+\\(\\sw+\\)\\)?"
376 (1 font-lock-keyword-face) (3 font-lock-constant-face nil t))
377 '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|\
378 do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
379 (2 font-lock-constant-face nil t) (3 font-lock-keyword-face))
380 ;; Implicit declaration.
381 '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\
382 \\|logical\\|type[ \t]*(\\sw+)\\|none\\)[ \t]*"
383 (1 font-lock-keyword-face) (2 font-lock-type-face))
384 '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
385 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
386 "\\<else\\([ \t]*if\\|where\\)?\\>"
387 '("\\(&\\)[ \t]*\\(!\\|$\\)" (1 font-lock-keyword-face))
388 "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
389 '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>"
390 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
391 '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
392 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
393 (1 font-lock-keyword-face) (2 font-lock-constant-face))
394 ;; Line numbers (lines whose first character after number is letter).
395 '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t))))
396 "Highlights declarations, do-loops and other constructs.")
398 (defvar f90-font-lock-keywords-3
399 (append f90-font-lock-keywords-2
401 f90-keywords-level-3-re
403 (list f90-procedures-re '(1 font-lock-keyword-face keep))
404 "\\<real\\>" ; avoid overwriting real defs
406 "Highlights all F90 keywords and intrinsic procedures.")
408 (defvar f90-font-lock-keywords-4
409 (append f90-font-lock-keywords-3
410 (list f90-hpf-keywords-re))
411 "Highlights all F90 and HPF keywords.")
413 (defvar f90-font-lock-keywords
414 f90-font-lock-keywords-2
415 "*Default expressions to highlight in F90 mode.
416 Can be overridden by the value of `font-lock-maximum-decoration'.")
419 (defvar f90-mode-syntax-table
420 (let ((table (make-syntax-table)))
421 (modify-syntax-entry ?\! "<" table) ; begin comment
422 (modify-syntax-entry ?\n ">" table) ; end comment
423 (modify-syntax-entry ?_ "w" table) ; underscore in names
424 (modify-syntax-entry ?\' "\"" table) ; string quote
425 (modify-syntax-entry ?\" "\"" table) ; string quote
426 (modify-syntax-entry ?\` "w" table) ; for abbrevs
427 (modify-syntax-entry ?\r " " table) ; return is whitespace
428 (modify-syntax-entry ?+ "." table) ; punctuation
429 (modify-syntax-entry ?- "." table)
430 (modify-syntax-entry ?= "." table)
431 (modify-syntax-entry ?* "." table)
432 (modify-syntax-entry ?/ "." table)
433 (modify-syntax-entry ?\\ "\\" table) ; escape chars
435 "Syntax table used in F90 mode.")
438 (let ((map (make-sparse-keymap)))
439 (define-key map "`" 'f90-abbrev-start)
440 (define-key map "\C-c;" 'f90-comment-region)
441 (define-key map "\C-\M-a" 'f90-beginning-of-subprogram)
442 (define-key map "\C-\M-e" 'f90-end-of-subprogram)
443 (define-key map "\C-\M-h" 'f90-mark-subprogram)
444 (define-key map "\C-\M-n" 'f90-end-of-block)
445 (define-key map "\C-\M-p" 'f90-beginning-of-block)
446 (define-key map "\C-\M-q" 'f90-indent-subprogram)
447 (define-key map "\C-j" 'f90-indent-new-line) ; LFD equals C-j
448 (define-key map "\r" 'newline)
449 (define-key map "\C-c\r" 'f90-break-line)
450 ;;; (define-key map [M-return] 'f90-break-line)
451 (define-key map "\C-c\C-a" 'f90-previous-block)
452 (define-key map "\C-c\C-e" 'f90-next-block)
453 (define-key map "\C-c\C-d" 'f90-join-lines)
454 (define-key map "\C-c\C-f" 'f90-fill-region)
455 (define-key map "\C-c\C-p" 'f90-previous-statement)
456 (define-key map "\C-c\C-n" 'f90-next-statement)
457 (define-key map "\C-c\C-w" 'f90-insert-end)
458 (define-key map "\t" 'f90-indent-line)
459 (define-key map "," 'f90-electric-insert)
460 (define-key map "+" 'f90-electric-insert)
461 (define-key map "-" 'f90-electric-insert)
462 (define-key map "*" 'f90-electric-insert)
463 (define-key map "/" 'f90-electric-insert)
465 (easy-menu-define f90-menu map "Menu for F90 mode."
468 ,(custom-menu-create 'f90)
470 ["Save" Custom-save t]
471 ["Reset to Current" Custom-reset-current t]
472 ["Reset to Saved" Custom-reset-saved t]
473 ["Reset to Standard Settings" Custom-reset-standard t]
476 ["Indent Subprogram" f90-indent-subprogram t]
477 ["Mark Subprogram" f90-mark-subprogram t]
478 ["Beginning of Subprogram" f90-beginning-of-subprogram t]
479 ["End of Subprogram" f90-end-of-subprogram t]
481 ;; XEmacs change: mark-active --> zmacs-region-active-p
482 ["(Un)Comment Region" f90-comment-region zmacs-region-active-p]
483 ["Indent Region" f90-indent-region zmacs-region-active-p]
484 ["Fill Region" f90-fill-region zmacs-region-active-p]
486 ["Break Line at Point" f90-break-line t]
487 ["Join with Previous Line" f90-join-lines t]
488 ["Insert Block End" f90-insert-end t]
491 ["Toggle font-lock-mode" font-lock-mode :selected font-lock-mode
494 ["Light highlighting (level 1)" f90-font-lock-1 t]
495 ["Moderate highlighting (level 2)" f90-font-lock-2 t]
496 ["Heavy highlighting (level 3)" f90-font-lock-3 t]
497 ["Maximum highlighting (level 4)" f90-font-lock-4 t]
499 ("Change Keyword Case"
500 ["Upcase Keywords (buffer)" f90-upcase-keywords t]
501 ["Capitalize Keywords (buffer)" f90-capitalize-keywords t]
502 ["Downcase Keywords (buffer)" f90-downcase-keywords t]
504 ;; XEmacs change: mark-active --> zmacs-region-active-p
505 ["Upcase Keywords (region)" f90-upcase-region-keywords
506 zmacs-region-active-p]
507 ["Capitalize Keywords (region)" f90-capitalize-region-keywords
508 zmacs-region-active-p]
509 ["Downcase Keywords (region)" f90-downcase-region-keywords
510 zmacs-region-active-p]
513 ["Toggle auto-fill" auto-fill-mode :selected auto-fill-function
515 ["Toggle abbrev-mode" abbrev-mode :selected abbrev-mode
517 ["Add imenu Menu" f90-add-imenu-menu
518 :active (not (lookup-key (current-local-map) [menu-bar index]))
519 :included (fboundp 'imenu-add-to-menubar)]))
521 "Keymap used in F90 mode.")
524 (defun f90-font-lock-1 ()
525 "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
528 (setq font-lock-keywords f90-font-lock-keywords-1)
529 (font-lock-fontify-buffer))
531 (defun f90-font-lock-2 ()
532 "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
535 (setq font-lock-keywords f90-font-lock-keywords-2)
536 (font-lock-fontify-buffer))
538 (defun f90-font-lock-3 ()
539 "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
542 (setq font-lock-keywords f90-font-lock-keywords-3)
543 (font-lock-fontify-buffer))
545 (defun f90-font-lock-4 ()
546 "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
549 (setq font-lock-keywords f90-font-lock-keywords-4)
550 (font-lock-fontify-buffer))
553 ;; Regexps for finding program structures.
554 (defconst f90-blocks-re
555 (concat "\\(block[ \t]*data\\|"
556 (regexp-opt '("do" "if" "interface" "function" "module" "program"
557 "select" "subroutine" "type" "where" "forall"))
559 "Regexp potentially indicating a \"block\" of F90 code.")
561 (defconst f90-program-block-re
562 (regexp-opt '("program" "module" "subroutine" "function") 'paren)
563 "Regexp used to locate the start/end of a \"subprogram\".")
565 (defconst f90-else-like-re
566 "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\)"
567 "Regexp matching an ELSE IF, ELSEWHERE, CASE statement.")
569 (defconst f90-end-if-re
571 (regexp-opt '("if" "select" "where" "forall") 'paren)
573 "Regexp matching the end of an IF, SELECT, WHERE, FORALL block.")
575 (defconst f90-end-type-re
576 "end[ \t]*\\(type\\|interface\\|block[ \t]*data\\)\\>"
577 "Regexp matching the end of a TYPE, INTERFACE, BLOCK DATA section.")
579 (defconst f90-type-def-re
580 "\\<\\(type\\)\\>\\(?:[^()\n]*::\\)?[ \t]*\\(\\sw+\\)"
581 "Regexp matching the definition of a derived type.")
583 (defconst f90-no-break-re
584 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=") 'paren)
585 "Regexp specifying where not to break lines when filling.
586 This regexp matches certain tokens comprised entirely of
587 characters matching the regexp `f90-break-delimiters' that should
588 not be split by filling. Each element is assumed to be two
591 (defvar f90-cache-position nil
592 "Temporary position used to speed up region operations.")
593 (make-variable-buffer-local 'f90-cache-position)
597 (defvar f90-imenu-generic-expression
598 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
599 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]"))
601 '(nil "^[ \t0-9]*program[ \t]+\\(\\sw+\\)" 1)
602 '("Modules" "^[ \t0-9]*module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
603 '("Types" "^[ \t0-9]*type[ \t]+\\(\\sw+\\)" 1)
609 ;; At least three non-space characters before function/subroutine.
610 ;; Check that the last three non-space characters do not spell E N D.
612 not-e good-char good-char "\\|"
613 good-char not-n good-char "\\|"
614 good-char good-char not-d "\\)"
616 ;; Less than three non-space characters before function/subroutine.
617 good-char "?" good-char "?"
619 "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
621 "Value for `imenu-generic-expression' in F90 mode.")
623 (defun f90-add-imenu-menu ()
624 "Add an imenu menu to the menubar."
626 (if (lookup-key (current-local-map) [menu-bar index])
627 (message "%s" "F90-imenu already exists.")
628 (imenu-add-to-menubar "F90-imenu")
629 (redraw-frame (selected-frame))))
632 ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
633 (defvar f90-mode-abbrev-table
634 (let (abbrevs-changed)
635 (define-abbrev-table 'f90-mode-abbrev-table nil)
636 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
637 ;; A little baroque to quieten the byte-compiler.
639 (function (lambda (element)
641 (apply 'define-abbrev f90-mode-abbrev-table
642 (append element '(nil 0 t)))
643 (wrong-number-of-arguments
644 (apply 'define-abbrev f90-mode-abbrev-table
645 (append element '(nil 0)))))))
646 '(("`al" "allocate" )
647 ("`ab" "allocatable" )
648 ("`as" "assignment" )
650 ("`bd" "block data" )
657 ("`de" "deallocate" )
663 ("`elw" "elsewhere" )
664 ("`eq" "equivalence" )
671 ("`im" "implicit none")
693 ("`su" "subroutine" )
699 f90-mode-abbrev-table)
700 "Abbrev table for F90 mode.")
705 "Major mode for editing Fortran 90,95 code in free format.
706 For fixed format code, use `fortran-mode'.
708 \\[f90-indent-line] indents the current line.
709 \\[f90-indent-new-line] indents current line and creates a new\
711 \\[f90-indent-subprogram] indents the current subprogram.
713 Type `? or `\\[help-command] to display a list of built-in\
714 abbrevs for F90 keywords.
719 Variables controlling indentation style and extra features:
722 Extra indentation within do blocks (default 3).
724 Extra indentation within if/select case/where/forall blocks (default 3).
726 Extra indentation within type/interface/block-data blocks (default 3).
728 Extra indentation within program/module/subroutine/function blocks
730 `f90-continuation-indent'
731 Extra indentation applied to continuation lines (default 5).
733 String inserted by function \\[f90-comment-region] at start of each
734 line in region (default \"!!!$\").
735 `f90-indented-comment-re'
736 Regexp determining the type of comment to be intended like code
738 `f90-directive-comment-re'
739 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
740 (default \"!hpf\\\\$\").
741 `f90-break-delimiters'
742 Regexp holding list of delimiters at which lines may be broken
743 (default \"[-+*/><=,% \\t]\").
744 `f90-break-before-delimiters'
745 Non-nil causes `f90-do-auto-fill' to break lines before delimiters
747 `f90-beginning-ampersand'
748 Automatic insertion of \& at beginning of continuation lines (default t).
750 From an END statement, check and fill the end using matching block start.
751 Allowed values are 'blink, 'no-blink, and nil, which determine
752 whether to blink the matching beginning (default 'blink).
753 `f90-auto-keyword-case'
754 Automatic change of case of keywords (default nil).
755 The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
757 Do not left-justify line numbers (default nil).
759 List of keywords used for highlighting/upcase-keywords etc.
761 Turning on F90 mode calls the value of the variable `f90-mode-hook'
762 with no args, if that value is non-nil."
764 (kill-all-local-variables)
765 (setq major-mode 'f90-mode
767 local-abbrev-table f90-mode-abbrev-table)
768 (set-syntax-table f90-mode-syntax-table)
769 (use-local-map f90-mode-map)
770 (set (make-local-variable 'indent-line-function) 'f90-indent-line)
771 (set (make-local-variable 'indent-region-function) 'f90-indent-region)
772 (set (make-local-variable 'require-final-newline) t)
773 (set (make-local-variable 'comment-start) "!")
774 (set (make-local-variable 'comment-start-skip) "!+ *")
775 (set (make-local-variable 'comment-indent-function) 'f90-comment-indent)
776 (set (make-local-variable 'abbrev-all-caps) t)
777 (set (make-local-variable 'normal-auto-fill-function) 'f90-do-auto-fill)
778 (setq indent-tabs-mode nil) ; auto buffer local
779 ;; XEmacs addition: set up things for font-lock
781 (put 'f90-mode 'font-lock-keywords-case-fold-search t)
782 (when (and (featurep 'menubar)
784 (not (assoc "F90" current-menubar)))
785 (set-buffer-menubar (copy-sequence current-menubar))
786 (easy-menu-add f90-menu)))
787 ;; XEmacs: (Don't need a special case, since both emacsen work alike -sb)
788 (set (make-local-variable 'font-lock-defaults)
789 '((f90-font-lock-keywords f90-font-lock-keywords-1
790 f90-font-lock-keywords-2
791 f90-font-lock-keywords-3
792 f90-font-lock-keywords-4)
794 (set (make-local-variable 'imenu-case-fold-search) t)
795 (set (make-local-variable 'imenu-generic-expression)
796 f90-imenu-generic-expression)
797 (set (make-local-variable 'beginning-of-defun-function)
798 'f90-beginning-of-subprogram)
799 (set (make-local-variable 'end-of-defun-function) 'f90-end-of-subprogram)
800 (set (make-local-variable 'add-log-current-defun-function)
802 (run-hooks 'f90-mode-hook))
806 ;; XEmacs change: wrap the beginning/ending of line functions
807 (defmacro f90-get-beg-of-line ()
808 (if running-xemacs '(point-at-bol) '(line-beginning-position)))
810 (defmacro f90-get-end-of-line ()
811 (if running-xemacs '(point-at-eol) '(line-end-position)))
813 (defsubst f90-in-string ()
814 "Return non-nil if point is inside a string.
815 Checks from `point-min', or `f90-cache-position', if that is non-nil
816 and lies before point."
818 (if (and f90-cache-position (> (point) f90-cache-position))
821 (nth 3 (parse-partial-sexp beg-pnt (point)))))
823 (defsubst f90-in-comment ()
824 "Return non-nil if point is inside a comment.
825 Checks from `point-min', or `f90-cache-position', if that is non-nil
826 and lies before point."
828 (if (and f90-cache-position (> (point) f90-cache-position))
831 (nth 4 (parse-partial-sexp beg-pnt (point)))))
833 (defsubst f90-line-continued ()
834 "Return t if the current line is a continued one.
835 This includes comment lines embedded in continued lines, but
836 not the last line of a continued statement."
839 (while (and (looking-at "[ \t]*\\(!\\|$\\)") (zerop (forward-line -1))))
841 (while (f90-in-comment)
842 (search-backward "!" (f90-get-beg-of-line))
843 (skip-chars-backward "!"))
844 (skip-chars-backward " \t")
845 (= (preceding-char) ?&)))
847 ;; GM this is not right, eg a continuation line starting with a number.
848 ;; Need f90-code-start-position function.
849 ;; And yet, things seems to work with this...
850 ;; cf f90-indent-line
851 ;; (beginning-of-line) ; digits after & \n are not line-nos
852 ;; (if (not (save-excursion (and (f90-previous-statement)
853 ;; (f90-line-continued))))
854 ;; (f90-indent-line-no)
855 (defsubst f90-current-indentation ()
856 "Return indentation of current line.
857 Line-numbers are considered whitespace characters."
858 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
860 (defsubst f90-indent-to (col &optional no-line-number)
861 "Indent current line to column COL.
862 If optional argument NO-LINE-NUMBER is nil, jump over a possible
863 line-number before indenting."
866 (skip-chars-forward " \t0-9"))
867 (delete-horizontal-space)
868 ;; Leave >= 1 space after line number.
869 (indent-to col (if (zerop (current-column)) 0 1)))
871 (defsubst f90-get-present-comment-type ()
872 "If point lies within a comment, return the string starting the comment.
873 For example, \"!\" or \"!!\"."
875 (when (f90-in-comment)
877 (re-search-forward "!+" (f90-get-end-of-line))
878 (while (f90-in-string)
879 (re-search-forward "!+" (f90-get-end-of-line)))
882 (defsubst f90-equal-symbols (a b)
883 "Compare strings A and B neglecting case and allowing for nil value."
884 (equal (if a (downcase a) nil)
885 (if b (downcase b) nil)))
887 (defsubst f90-looking-at-do ()
888 "Return (\"do\" NAME) if a do statement starts after point.
889 NAME is nil if the statement has no label."
890 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\>")
891 (list (match-string 3) (match-string 2))))
893 (defsubst f90-looking-at-select-case ()
894 "Return (\"select\" NAME) if a select-case statement starts after point.
895 NAME is nil if the statement has no label."
896 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
897 \\(select\\)[ \t]*case[ \t]*(")
898 (list (match-string 3) (match-string 2))))
900 (defsubst f90-looking-at-if-then ()
901 "Return (\"if\" NAME) if an if () then statement starts after point.
902 NAME is nil if the statement has no label."
904 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\>")
905 (let ((struct (match-string 3))
906 (label (match-string 2))
907 (pos (scan-lists (point) 1 0)))
908 (and pos (goto-char pos))
909 (skip-chars-forward " \t")
910 (if (or (looking-at "then\\>")
911 (when (f90-line-continued)
913 (skip-chars-forward " \t0-9&")
914 (looking-at "then\\>")))
915 (list struct label))))))
917 (defsubst f90-looking-at-where-or-forall ()
918 "Return (KIND NAME) if a where or forall block starts after point.
919 NAME is nil if the statement has no label."
921 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
922 \\(where\\|forall\\)\\>")
923 (let ((struct (match-string 3))
924 (label (match-string 2))
925 (pos (scan-lists (point) 1 0)))
926 (and pos (goto-char pos))
927 (skip-chars-forward " \t")
928 (if (looking-at "\\(!\\|$\\)") (list struct label))))))
930 (defsubst f90-looking-at-type-like ()
931 "Return (KIND NAME) if a type/interface/block-data block starts after point.
932 NAME is non-nil only for type."
934 ((looking-at f90-type-def-re)
935 (list (match-string 1) (match-string 2)))
936 ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
937 (list (match-string 1) nil))))
939 (defsubst f90-looking-at-program-block-start ()
940 "Return (KIND NAME) if a program block with name NAME starts after point."
942 ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
943 (list (match-string 1) (match-string 2)))
944 ((and (not (looking-at "module[ \t]*procedure\\>"))
945 (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
946 (list (match-string 1) (match-string 2)))
947 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
948 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
950 (list (match-string 1) (match-string 2)))))
952 (defsubst f90-looking-at-program-block-end ()
953 "Return (KIND NAME) if a block with name NAME ends after point."
954 (if (looking-at (concat "end[ \t]*" f90-blocks-re
955 "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
956 (list (match-string 1) (match-string 3))))
958 (defsubst f90-comment-indent ()
959 "Return the indentation to be used for a comment starting at point.
960 Used for `comment-indent-function' by F90 mode.
961 \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
962 `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
963 All others return `comment-column', leaving at least one space after code."
964 (cond ((looking-at "!!!") 0)
965 ((and f90-directive-comment-re
966 (looking-at f90-directive-comment-re)) 0)
967 ((looking-at (regexp-quote f90-comment-region)) 0)
968 ((and (looking-at f90-indented-comment-re)
969 ;; Don't attempt to indent trailing comment as code.
971 (skip-chars-backward " \t")
973 (f90-calculate-indent))
974 (t (skip-chars-backward " \t")
975 (max (if (bolp) 0 (1+ (current-column))) comment-column))))
977 (defsubst f90-present-statement-cont ()
978 "Return continuation properties of present statement.
979 Possible return values are:
980 single - statement is not continued.
981 begin - current line is the first in a continued statement.
982 end - current line is the last in a continued statement
983 middle - current line is neither first nor last in a continued statement.
984 Comment lines embedded amongst continued lines return 'middle."
987 (setq pcont (if (f90-previous-statement) (f90-line-continued))))
988 (setq cont (f90-line-continued))
989 (cond ((and (not pcont) (not cont)) 'single)
990 ((and (not pcont) cont) 'begin)
991 ((and pcont (not cont)) 'end)
992 ((and pcont cont) 'middle)
993 (t (error "The impossible occurred")))))
995 (defsubst f90-indent-line-no ()
996 "If `f90-leave-line-no' is nil, left-justify a line number.
997 Leaves point at the first non-blank character after the line number.
998 Call from beginning of line."
999 (and (null f90-leave-line-no) (looking-at "[ \t]+[0-9]")
1000 (delete-horizontal-space))
1001 (skip-chars-forward " \t0-9"))
1003 (defsubst f90-no-block-limit ()
1004 "Return nil if point is at the edge of a code block.
1005 Searches line forward for \"function\" or \"subroutine\",
1008 (not (or (looking-at "end")
1009 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
1010 \\|select[ \t]*case\\|case\\|where\\|forall\\)\\>")
1011 (looking-at "\\(program\\|module\\|interface\\|\
1012 block[ \t]*data\\)\\>")
1013 (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
1014 (looking-at f90-type-def-re)
1015 (re-search-forward "\\(function\\|subroutine\\)"
1016 (f90-get-end-of-line) t)))))
1018 (defsubst f90-update-line ()
1019 "Change case of current line as per `f90-auto-keyword-case'."
1020 (if f90-auto-keyword-case
1021 (f90-change-keywords f90-auto-keyword-case
1022 (f90-get-beg-of-line) (f90-get-end-of-line))))
1024 (defun f90-electric-insert ()
1025 "Change keyword case and auto-fill line as operators are inserted."
1027 (self-insert-command 1)
1028 (if auto-fill-function (f90-do-auto-fill) ; also updates line
1032 (defun f90-get-correct-indent ()
1033 "Get correct indent for a line starting with line number.
1034 Does not check type and subprogram indentation."
1035 (let ((epnt (f90-get-end-of-line)) icol cont)
1037 (while (and (f90-previous-statement)
1039 (setq cont (f90-present-statement-cont))
1040 (or (eq cont 'end) (eq cont 'middle)))
1041 (looking-at "[ \t]*[0-9]"))))
1042 (setq icol (current-indentation))
1044 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1045 (f90-get-end-of-line) t)
1047 (skip-chars-forward " \t")
1048 (cond ((f90-looking-at-do)
1049 (setq icol (+ icol f90-do-indent)))
1050 ((or (f90-looking-at-if-then)
1051 (f90-looking-at-where-or-forall)
1052 (f90-looking-at-select-case))
1053 (setq icol (+ icol f90-if-indent))))
1055 (while (re-search-forward
1056 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
1058 (skip-chars-forward " \t0-9")
1059 (cond ((f90-looking-at-do)
1060 (setq icol (+ icol f90-do-indent)))
1061 ((or (f90-looking-at-if-then)
1062 (f90-looking-at-where-or-forall)
1063 (f90-looking-at-select-case))
1064 (setq icol (+ icol f90-if-indent)))
1065 ((looking-at f90-end-if-re)
1066 (setq icol (- icol f90-if-indent)))
1067 ((looking-at "end[ \t]*do\\>")
1068 (setq icol (- icol f90-do-indent))))
1072 (defun f90-calculate-indent ()
1073 "Calculate the indent column based on previous statements."
1075 (let (icol cont (case-fold-search t) (pnt (point)))
1077 (if (not (f90-previous-statement))
1079 (setq cont (f90-present-statement-cont))
1081 (while (not (eq 'begin (f90-present-statement-cont)))
1082 (f90-previous-statement)))
1083 (cond ((eq cont 'begin)
1084 (setq icol (+ (f90-current-indentation)
1085 f90-continuation-indent)))
1086 ((eq cont 'middle) (setq icol (current-indentation)))
1087 (t (setq icol (f90-current-indentation))
1088 (skip-chars-forward " \t")
1089 (if (looking-at "[0-9]")
1090 (setq icol (f90-get-correct-indent))
1091 (cond ((or (f90-looking-at-if-then)
1092 (f90-looking-at-where-or-forall)
1093 (f90-looking-at-select-case)
1094 (looking-at f90-else-like-re))
1095 (setq icol (+ icol f90-if-indent)))
1096 ((f90-looking-at-do)
1097 (setq icol (+ icol f90-do-indent)))
1098 ((f90-looking-at-type-like)
1099 (setq icol (+ icol f90-type-indent)))
1100 ((or (f90-looking-at-program-block-start)
1101 (looking-at "contains[ \t]*\\($\\|!\\)"))
1102 (setq icol (+ icol f90-program-indent)))))
1105 (cond ((looking-at "[ \t]*$"))
1106 ((looking-at "[ \t]*#") ; check for cpp directive
1109 (skip-chars-forward " \t0-9")
1110 (cond ((or (looking-at f90-else-like-re)
1111 (looking-at f90-end-if-re))
1112 (setq icol (- icol f90-if-indent)))
1113 ((looking-at "end[ \t]*do\\>")
1114 (setq icol (- icol f90-do-indent)))
1115 ((looking-at f90-end-type-re)
1116 (setq icol (- icol f90-type-indent)))
1117 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1118 (f90-looking-at-program-block-end))
1119 (setq icol (- icol f90-program-indent))))))
1123 (defun f90-previous-statement ()
1124 "Move point to beginning of the previous F90 statement.
1125 Return nil if no previous statement is found.
1126 A statement is a line which is neither blank nor a comment."
1128 (let (not-first-statement)
1130 (while (and (setq not-first-statement (zerop (forward-line -1)))
1131 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1132 not-first-statement))
1134 (defun f90-next-statement ()
1135 "Move point to beginning of the next F90 statement.
1136 Return nil if no later statement is found."
1138 (let (not-last-statement)
1140 (while (and (setq not-last-statement
1141 (and (zerop (forward-line 1))
1143 (looking-at "[ \t0-9]*\\(!\\|$\\)")))
1144 not-last-statement))
1146 (defun f90-beginning-of-subprogram ()
1147 "Move point to the beginning of the current subprogram.
1148 Return (TYPE NAME), or nil if not found."
1150 (let ((count 1) (case-fold-search t) matching-beg)
1152 (while (and (> count 0)
1153 (re-search-backward f90-program-block-re nil 'move))
1155 (skip-chars-forward " \t0-9")
1156 (cond ((setq matching-beg (f90-looking-at-program-block-start))
1157 (setq count (1- count)))
1158 ((f90-looking-at-program-block-end)
1159 (setq count (1+ count)))))
1163 (message "No beginning found.")
1166 (defun f90-end-of-subprogram ()
1167 "Move point to the end of the current subprogram.
1168 Return (TYPE NAME), or nil if not found."
1170 (let ((case-fold-search t)
1174 (while (and (> count 0)
1175 (re-search-forward f90-program-block-re nil 'move))
1177 (skip-chars-forward " \t0-9")
1178 (cond ((f90-looking-at-program-block-start)
1179 (setq count (1+ count)))
1180 ((setq matching-end (f90-looking-at-program-block-end))
1181 (setq count (1- count))))
1183 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1184 ;; has a net non-zero effect, which seems odd.
1185 ;;; (forward-line 1)
1188 (message "No end found.")
1192 (defun f90-end-of-block (&optional num)
1193 "Move point forward to the end of the current code block.
1194 With optional argument NUM, go forward that many balanced blocks.
1195 If NUM is negative, go backward to the start of a block.
1196 Checks for consistency of block types and labels (if present),
1197 and completes outermost block if necessary."
1199 (if (and num (< num 0)) (f90-beginning-of-block (- num)))
1200 (let ((f90-smart-end nil) ; for the final `f90-match-end'
1201 (case-fold-search t)
1203 start-list start-this start-type start-label end-type end-label)
1204 (if (interactive-p) (push-mark (point) t))
1205 (end-of-line) ; probably want this
1206 (while (and (> count 0) (re-search-forward f90-blocks-re nil 'move))
1208 (skip-chars-forward " \t0-9")
1209 (cond ((or (f90-in-string) (f90-in-comment)))
1213 (f90-looking-at-select-case)
1214 (f90-looking-at-type-like)
1215 (f90-looking-at-program-block-start)
1216 (f90-looking-at-if-then)
1217 (f90-looking-at-where-or-forall)))
1218 (setq start-list (cons start-this start-list) ; not add-to-list!
1220 ((looking-at (concat "end[ \t]*" f90-blocks-re
1221 "[ \t]*\\(\\sw+\\)?"))
1222 (setq end-type (match-string 1)
1223 end-label (match-string 2)
1225 ;; Check any internal blocks.
1227 (setq start-this (car start-list)
1228 start-list (cdr start-list)
1229 start-type (car start-this)
1230 start-label (cadr start-this))
1231 (or (f90-equal-symbols start-type end-type)
1232 (error "End type `%s' does not match start type `%s'"
1233 end-type start-type))
1234 (or (f90-equal-symbols start-label end-label)
1235 (error "End label `%s' does not match start label `%s'"
1236 end-label start-label)))))
1238 (if (> count 0) (error "Missing block end"))
1239 ;; Check outermost block.
1243 (skip-chars-forward " \t0-9")
1246 (defun f90-beginning-of-block (&optional num)
1247 "Move point backwards to the start of the current code block.
1248 With optional argument NUM, go backward that many balanced blocks.
1249 If NUM is negative, go forward to the end of a block.
1250 Checks for consistency of block types and labels (if present).
1251 Does not check the outermost block, because it may be incomplete."
1253 (if (and num (< num 0)) (f90-end-of-block (- num)))
1254 (let ((case-fold-search t)
1256 end-list end-this end-type end-label
1257 start-this start-type start-label)
1258 (if (interactive-p) (push-mark (point) t))
1259 (beginning-of-line) ; probably want this
1260 (while (and (> count 0) (re-search-backward f90-blocks-re nil 'move))
1262 (skip-chars-forward " \t0-9")
1263 (cond ((or (f90-in-string) (f90-in-comment)))
1264 ((looking-at (concat "end[ \t]*" f90-blocks-re
1265 "[ \t]*\\(\\sw+\\)?"))
1266 (setq end-list (cons (list (match-string 1) (match-string 2))
1272 (f90-looking-at-select-case)
1273 (f90-looking-at-type-like)
1274 (f90-looking-at-program-block-start)
1275 (f90-looking-at-if-then)
1276 (f90-looking-at-where-or-forall)))
1277 (setq start-type (car start-this)
1278 start-label (cadr start-this)
1280 ;; Check any internal blocks.
1282 (setq end-this (car end-list)
1283 end-list (cdr end-list)
1284 end-type (car end-this)
1285 end-label (cadr end-this))
1286 (or (f90-equal-symbols start-type end-type)
1287 (error "Start type `%s' does not match end type `%s'"
1288 start-type end-type))
1289 (or (f90-equal-symbols start-label end-label)
1290 (error "Start label `%s' does not match end label `%s'"
1291 start-label end-label))))))
1292 (if (> count 0) (error "Missing block start"))))
1294 (defun f90-next-block (&optional num)
1295 "Move point forward to the next end or start of a code block.
1296 With optional argument NUM, go forward that many blocks.
1297 If NUM is negative, go backwards.
1298 A block is a subroutine, if-endif, etc."
1300 (let ((case-fold-search t)
1301 (count (if num (abs num) 1)))
1302 (while (and (> count 0)
1303 (if (> num 0) (re-search-forward f90-blocks-re nil 'move)
1304 (re-search-backward f90-blocks-re nil 'move)))
1306 (skip-chars-forward " \t0-9")
1307 (cond ((or (f90-in-string) (f90-in-comment)))
1309 (looking-at "end[ \t]*")
1311 (f90-looking-at-select-case)
1312 (f90-looking-at-type-like)
1313 (f90-looking-at-program-block-start)
1314 (f90-looking-at-if-then)
1315 (f90-looking-at-where-or-forall))
1316 (setq count (1- count))))
1317 (if (> num 0) (end-of-line)
1318 (beginning-of-line)))))
1321 (defun f90-previous-block (&optional num)
1322 "Move point backward to the previous end or start of a code block.
1323 With optional argument NUM, go backward that many blocks.
1324 If NUM is negative, go forwards.
1325 A block is a subroutine, if-endif, etc."
1327 (f90-next-block (- (or num 1))))
1330 (defun f90-mark-subprogram ()
1331 "Put mark at end of F90 subprogram, point at beginning, push mark."
1333 (let ((pos (point)) program)
1334 (f90-end-of-subprogram)
1335 ;; XEmacs change: pass the NOMSG arg to push-mark
1336 (push-mark (point) t)
1338 (setq program (f90-beginning-of-subprogram))
1339 (if (fboundp 'zmacs-activate-region)
1340 (zmacs-activate-region)
1342 deactivate-mark nil))
1345 (defun f90-comment-region (beg-region end-region)
1346 "Comment/uncomment every line in the region.
1347 Insert the variable `f90-comment-region' at the start of every line
1348 in the region, or, if already present, remove it."
1350 (let ((end (copy-marker end-region)))
1351 (goto-char beg-region)
1353 (if (looking-at (regexp-quote f90-comment-region))
1354 (delete-region (point) (match-end 0))
1355 (insert f90-comment-region))
1356 (while (and (zerop (forward-line 1))
1358 (if (looking-at (regexp-quote f90-comment-region))
1359 (delete-region (point) (match-end 0))
1360 (insert f90-comment-region)))
1361 (set-marker end nil)))
1363 (defun f90-indent-line (&optional no-update)
1364 "Indent current line as F90 code.
1365 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1368 (let ((case-fold-search t)
1369 (pos (point-marker))
1370 indent no-line-number)
1371 (beginning-of-line) ; digits after & \n are not line-nos
1372 (if (not (save-excursion (and (f90-previous-statement)
1373 (f90-line-continued))))
1374 (f90-indent-line-no)
1375 (setq no-line-number t)
1376 (skip-chars-forward " \t"))
1377 (if (looking-at "!")
1378 (setq indent (f90-comment-indent))
1379 (and f90-smart-end (looking-at "end")
1381 (setq indent (f90-calculate-indent)))
1382 (or (= indent (current-column))
1383 (f90-indent-to indent no-line-number))
1384 ;; If initial point was within line's indentation,
1385 ;; position after the indentation. Else stay at same point in text.
1386 (and (< (point) pos)
1388 (if auto-fill-function
1389 ;; GM NO-UPDATE not honoured, since this calls f90-update-line.
1391 (or no-update (f90-update-line)))
1392 (set-marker pos nil)))
1394 (defun f90-indent-new-line ()
1395 "Re-indent current line, insert a newline and indent the newline.
1396 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1397 If run in the middle of a line, the line is not broken."
1399 (if abbrev-mode (expand-abbrev))
1400 (beginning-of-line) ; reindent where likely to be needed
1401 (f90-indent-line) ; calls indent-line-no, update-line
1403 (delete-horizontal-space) ; destroy trailing whitespace
1404 (let ((string (f90-in-string))
1405 (cont (f90-line-continued)))
1406 (and string (not cont) (insert "&"))
1408 (if (or string (and cont f90-beginning-ampersand)) (insert "&")))
1409 (f90-indent-line 'no-update)) ; nothing to update
1412 (defun f90-indent-region (beg-region end-region)
1413 "Indent every line in region by forward parsing."
1415 (let ((end-region-mark (copy-marker end-region))
1416 (save-point (point-marker))
1417 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
1418 (goto-char beg-region)
1419 ;; First find a line which is not a continuation line or comment.
1421 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1422 (progn (f90-indent-line 'no-update)
1423 (zerop (forward-line 1)))
1424 (< (point) end-region-mark)))
1425 (setq cont (f90-present-statement-cont))
1426 (while (and (or (eq cont 'middle) (eq cont 'end))
1427 (f90-previous-statement))
1428 (setq cont (f90-present-statement-cont)))
1429 ;; Process present line for beginning of block.
1430 (setq f90-cache-position (point))
1431 (f90-indent-line 'no-update)
1432 (setq ind-lev (f90-current-indentation)
1435 (skip-chars-forward " \t0-9")
1437 ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1438 ((or (setq struct (f90-looking-at-if-then))
1439 (setq struct (f90-looking-at-select-case))
1440 (setq struct (f90-looking-at-where-or-forall))
1441 (looking-at f90-else-like-re))
1443 ((setq struct (f90-looking-at-type-like))
1445 ((or (setq struct (f90-looking-at-program-block-start))
1446 (looking-at "contains[ \t]*\\($\\|!\\)"))
1447 f90-program-indent)))
1448 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1449 (if struct (setq block-list (cons struct block-list)))
1450 (while (and (f90-line-continued) (zerop (forward-line 1))
1451 (< (point) end-region-mark))
1452 (if (looking-at "[ \t]*!")
1453 (f90-indent-to (f90-comment-indent))
1454 (or (= (current-indentation)
1455 (+ ind-curr f90-continuation-indent))
1456 (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
1457 ;; Process all following lines.
1458 (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
1460 (f90-indent-line-no)
1461 (setq f90-cache-position (point))
1462 (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
1463 ((looking-at "[ \t]*#") (setq ind-curr 0))
1464 ((looking-at "!") (setq ind-curr (f90-comment-indent)))
1465 ((f90-no-block-limit) (setq ind-curr ind-lev))
1466 ((looking-at f90-else-like-re) (setq ind-curr
1467 (- ind-lev f90-if-indent)))
1468 ((looking-at "contains[ \t]*\\($\\|!\\)")
1469 (setq ind-curr (- ind-lev f90-program-indent)))
1471 (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
1472 ((or (setq struct (f90-looking-at-if-then))
1473 (setq struct (f90-looking-at-select-case))
1474 (setq struct (f90-looking-at-where-or-forall)))
1476 ((setq struct (f90-looking-at-type-like))
1478 ((setq struct (f90-looking-at-program-block-start))
1479 f90-program-indent)))
1480 (setq ind-curr ind-lev)
1481 (if ind-b (setq ind-lev (+ ind-lev ind-b)))
1482 (setq block-list (cons struct block-list)))
1483 ((setq end-struct (f90-looking-at-program-block-end))
1484 (setq beg-struct (car block-list)
1485 block-list (cdr block-list))
1488 (f90-block-match (car beg-struct) (car (cdr beg-struct))
1489 (car end-struct) (car (cdr end-struct)))))
1491 (cond ((looking-at f90-end-if-re) f90-if-indent)
1492 ((looking-at "end[ \t]*do\\>") f90-do-indent)
1493 ((looking-at f90-end-type-re) f90-type-indent)
1494 ((f90-looking-at-program-block-end)
1495 f90-program-indent)))
1496 (if ind-b (setq ind-lev (- ind-lev ind-b)))
1497 (setq ind-curr ind-lev))
1498 (t (setq ind-curr ind-lev)))
1499 ;; Do the indentation if necessary.
1500 (or (= ind-curr (current-column))
1501 (f90-indent-to ind-curr))
1502 (while (and (f90-line-continued) (zerop (forward-line 1))
1503 (< (point) end-region-mark))
1504 (if (looking-at "[ \t]*!")
1505 (f90-indent-to (f90-comment-indent))
1506 (or (= (current-indentation)
1507 (+ ind-curr f90-continuation-indent))
1509 (+ ind-curr f90-continuation-indent) 'no-line-no)))))
1510 ;; Restore point, etc.
1511 (setq f90-cache-position nil)
1512 (goto-char save-point)
1513 (set-marker end-region-mark nil)
1514 (set-marker save-point nil)
1515 (if (fboundp 'zmacs-deactivate-region)
1516 (zmacs-deactivate-region)
1517 (deactivate-mark))))
1519 (defun f90-indent-subprogram ()
1520 "Properly indent the subprogram containing point."
1523 (let ((program (f90-mark-subprogram)))
1526 (message "Indenting %s %s..."
1527 (car program) (car (cdr program)))
1528 (indent-region (point) (mark) nil)
1529 (message "Indenting %s %s...done"
1530 (car program) (car (cdr program))))
1531 (message "Indenting the whole file...")
1532 (indent-region (point) (mark) nil)
1533 (message "Indenting the whole file...done")))))
1535 (defun f90-break-line (&optional no-update)
1536 "Break line at point, insert continuation marker(s) and indent.
1537 Unless in a string or comment, or if the optional argument NO-UPDATE
1538 is non-nil, call `f90-update-line' after inserting the continuation marker."
1540 (cond ((f90-in-string)
1543 (insert "\n" (f90-get-present-comment-type)))
1545 (or no-update (f90-update-line))
1547 (if f90-beginning-ampersand (insert "&"))))
1548 (indent-according-to-mode))
1550 (defun f90-find-breakpoint ()
1551 "From `fill-column', search backward for break-delimiter."
1552 (re-search-backward f90-break-delimiters (f90-get-beg-of-line))
1553 (if (not f90-break-before-delimiters)
1554 (forward-char (if (looking-at f90-no-break-re) 2 1))
1556 (or (looking-at f90-no-break-re)
1559 (defun f90-do-auto-fill ()
1560 "Break line if non-white characters beyond `fill-column'.
1561 Update keyword case first."
1563 ;; Break line before or after last delimiter (non-word char) if
1564 ;; position is beyond fill-column.
1565 ;; Will not break **, //, or => (as specified by f90-no-break-re).
1567 ;; Need this for `f90-electric-insert' and other f90- callers.
1568 (unless (and (boundp 'comment-auto-fill-only-comments)
1569 comment-auto-fill-only-comments
1570 (not (f90-in-comment)))
1571 (while (> (current-column) fill-column)
1572 (let ((pos-mark (point-marker)))
1573 (move-to-column fill-column)
1574 (or (f90-in-string) (f90-find-breakpoint))
1576 (goto-char pos-mark)
1577 (set-marker pos-mark nil)))))
1579 (defun f90-join-lines (&optional arg)
1580 "Join current line to previous, fix whitespace, continuation, comments.
1581 With optional argument ARG, join current line to following line.
1582 Like `join-line', but handles F90 syntax."
1585 (if arg (forward-line 1))
1586 (when (eq (preceding-char) ?\n)
1587 (skip-chars-forward " \t")
1588 (if (looking-at "\&") (delete-char 1))
1590 (delete-region (point) (1- (point)))
1591 (skip-chars-backward " \t")
1592 (and (eq (preceding-char) ?&) (delete-char -1))
1593 (and (f90-in-comment)
1594 (looking-at "[ \t]*!+")
1597 (fixup-whitespace))))
1599 (defun f90-fill-region (beg-region end-region)
1600 "Fill every line in region by forward parsing. Join lines if possible."
1602 (let ((end-region-mark (copy-marker end-region))
1604 f90-smart-end f90-auto-keyword-case auto-fill-function)
1605 (goto-char beg-region)
1607 ;; Join as much as possible.
1610 (skip-chars-backward " \t")
1611 (eq (preceding-char) ?&))
1612 (f90-join-lines 'forward))
1613 ;; Chop the line if necessary.
1614 (while (> (save-excursion (end-of-line) (current-column))
1616 (move-to-column fill-column)
1617 (f90-find-breakpoint)
1618 (f90-break-line 'no-update))
1619 (setq go-on (and (< (point) end-region-mark)
1620 (zerop (forward-line 1)))
1621 f90-cache-position (point)))
1622 (setq f90-cache-position nil)
1623 (set-marker end-region-mark nil)
1624 (if (fboundp 'zmacs-deactivate-region)
1625 (zmacs-deactivate-region)
1626 (deactivate-mark))))
1628 (defun f90-block-match (beg-block beg-name end-block end-name)
1629 "Match end-struct with beg-struct and complete end-block if possible.
1630 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
1631 BEG-NAME is the block start name (may be nil).
1632 END-BLOCK is the type of block as indicated at the end (may be nil).
1633 END-NAME is the block end name (may be nil).
1634 Leave point at the end of line."
1635 (search-forward "end" (f90-get-end-of-line))
1637 (if (f90-equal-symbols beg-block end-block)
1638 (search-forward end-block)
1641 (message "END %s does not match %s." end-block beg-block)
1643 (throw 'no-match nil))
1644 (message "Inserting %s." beg-block)
1645 (insert (concat " " beg-block))))
1646 (if (f90-equal-symbols beg-name end-name)
1647 (and end-name (search-forward end-name))
1648 (cond ((and beg-name (not end-name))
1649 (message "Inserting %s." beg-name)
1650 (insert (concat " " beg-name)))
1651 ((and beg-name end-name)
1652 (message "Replacing %s with %s." end-name beg-name)
1653 (search-forward end-name)
1654 (replace-match beg-name))
1655 ((and (not beg-name) end-name)
1656 (message "Deleting %s." end-name)
1657 (search-forward end-name)
1658 (replace-match ""))))
1659 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
1661 (defun f90-match-end ()
1662 "From an end block statement, find the corresponding block and name."
1665 (top-of-window (window-start))
1667 (case-fold-search t)
1668 matching-beg beg-name end-name beg-block end-block end-struct)
1669 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
1670 (setq end-struct (f90-looking-at-program-block-end)))
1671 (setq end-block (car end-struct)
1672 end-name (car (cdr end-struct)))
1675 (while (and (> count 0) (re-search-backward f90-blocks-re nil t))
1677 ;; GM not a line number if continued line.
1678 ;;; (skip-chars-forward " \t")
1679 ;;; (skip-chars-forward "0-9")
1680 (skip-chars-forward " \t0-9")
1681 (cond ((or (f90-in-string) (f90-in-comment)))
1685 (f90-looking-at-if-then)
1686 (f90-looking-at-where-or-forall)
1687 (f90-looking-at-select-case)
1688 (f90-looking-at-type-like)
1689 (f90-looking-at-program-block-start)))
1690 (setq count (1- count)))
1691 ((looking-at (concat "end[ \t]*" f90-blocks-re))
1692 (setq count (1+ count)))))
1694 (message "No matching beginning.")
1696 (if (eq f90-smart-end 'blink)
1697 (if (< (point) top-of-window)
1698 (message "Matches %s: %s"
1701 (f90-get-beg-of-line)
1702 (f90-get-end-of-line)))
1704 (setq beg-block (car matching-beg)
1705 beg-name (car (cdr matching-beg)))
1706 (goto-char end-point)
1708 (f90-block-match beg-block beg-name end-block end-name))))))
1710 (defun f90-insert-end ()
1711 "Insert a complete end statement matching beginning of present block."
1713 (let ((f90-smart-end (or f90-smart-end 'blink)))
1715 (f90-indent-new-line)))
1717 ;; Abbrevs and keywords.
1719 (defun f90-abbrev-start ()
1720 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
1721 Any other key combination is executed normally."
1723 (insert last-command-char)
1725 (if (fboundp 'next-command-event) ; XEmacs
1726 (setq event (next-command-event)
1727 char (event-to-character event))
1728 (setq event (read-event)
1730 ;; Insert char if not equal to `?', or if abbrev-mode is off.
1731 (if (and abbrev-mode (or (eq char ??) (eq char help-char)))
1733 (setq unread-command-events (list event)))))
1735 (defun f90-abbrev-help ()
1736 "List the currently defined abbrevs in F90 mode."
1738 (message "Listing abbrev table...")
1739 (display-buffer (f90-prepare-abbrev-list-buffer))
1740 (message "Listing abbrev table...done"))
1742 (defun f90-prepare-abbrev-list-buffer ()
1743 "Create a buffer listing the F90 mode abbreviations."
1745 (set-buffer (get-buffer-create "*Abbrevs*"))
1747 (insert-abbrev-table-description 'f90-mode-abbrev-table t)
1748 (goto-char (point-min))
1749 (set-buffer-modified-p nil)
1750 (edit-abbrevs-mode))
1751 (get-buffer-create "*Abbrevs*"))
1753 (defun f90-upcase-keywords ()
1754 "Upcase all F90 keywords in the buffer."
1756 (f90-change-keywords 'upcase-word))
1758 (defun f90-capitalize-keywords ()
1759 "Capitalize all F90 keywords in the buffer."
1761 (f90-change-keywords 'capitalize-word))
1763 (defun f90-downcase-keywords ()
1764 "Downcase all F90 keywords in the buffer."
1766 (f90-change-keywords 'downcase-word))
1768 (defun f90-upcase-region-keywords (beg end)
1769 "Upcase all F90 keywords in the region."
1771 (f90-change-keywords 'upcase-word beg end))
1773 (defun f90-capitalize-region-keywords (beg end)
1774 "Capitalize all F90 keywords in the region."
1776 (f90-change-keywords 'capitalize-word beg end))
1778 (defun f90-downcase-region-keywords (beg end)
1779 "Downcase all F90 keywords in the region."
1781 (f90-change-keywords 'downcase-word beg end))
1783 ;; Change the keywords according to argument.
1784 (defun f90-change-keywords (change-word &optional beg end)
1785 "Change the case of F90 keywords in the region (if specified) or buffer.
1786 CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
1788 (setq beg (or beg (point-min))
1789 end (or end (point-max)))
1792 f90-keywords-re "\\|" f90-procedures-re "\\|"
1793 f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
1794 (ref-point (point-min))
1795 (modified (buffer-modified-p))
1796 state saveword back-point)
1799 (while (re-search-forward keyword-re end t)
1801 (setq state (parse-partial-sexp ref-point (point)))
1802 (or (nth 3 state) (nth 4 state)
1803 ;; GM f90-directive-comment-re?
1804 (save-excursion ; check for cpp directive
1806 (skip-chars-forward " \t0-9")
1808 (setq ref-point (point)
1809 back-point (save-excursion (backward-word 1) (point))
1810 saveword (buffer-substring back-point ref-point))
1811 (funcall change-word -1)
1812 (or (string= saveword (buffer-substring back-point ref-point))
1813 (setq modified t))))
1814 (or modified (set-buffer-modified-p nil))))))
1817 (defun f90-current-defun ()
1818 "Function to use for `add-log-current-defun-function' in F90 mode."
1820 (nth 1 (f90-beginning-of-subprogram))))
1823 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.[fF]90\\'" . f90-mode))
1827 ;;; arch-tag: fceac97c-c147-44bd-aec0-172d4b560ef8
1828 ;;; f90.el ends here