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)
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.
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
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
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.
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
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
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))
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.
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
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.
156 ;; Support for hideshow, align.
157 ;; OpenMP, preprocessor highlighting.
159 (defvar comment-auto-fill-only-comments
)
160 (defvar font-lock-keywords
)
165 "Major mode for editing free format Fortran 90,95 code."
168 (defgroup f90-indent nil
169 "Indentation in free format Fortran."
174 (defcustom f90-do-indent
3
175 "*Extra indentation applied to DO blocks."
179 (defcustom f90-if-indent
3
180 "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
184 (defcustom f90-type-indent
3
185 "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks."
189 (defcustom f90-program-indent
2
190 "*Extra indentation applied to PROGRAM, MODULE, SUBROUTINE, FUNCTION blocks."
194 (defcustom f90-continuation-indent
5
195 "*Extra indentation applied to continuation lines."
199 (defcustom f90-comment-region
"!!$"
200 "*String inserted by \\[f90-comment-region] at start of each line in region."
204 (defcustom f90-indented-comment-re
"!"
205 "*Regexp matching comments to indent as code."
209 (defcustom f90-directive-comment-re
"!hpf\\$"
210 "*Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
214 (defcustom f90-beginning-ampersand t
215 "*Non-nil gives automatic insertion of \& at start of continuation line."
219 (defcustom f90-smart-end
'blink
220 "*From an END statement, check and fill the end using matching block start.
221 Allowed values are 'blink, 'no-blink, and nil, which determine
222 whether to blink the matching beginning."
223 :type
'(choice (const blink
) (const no-blink
) (const nil
))
226 (defcustom f90-break-delimiters
"[-+\\*/><=,% \t]"
227 "*Regexp matching delimiter characters at which lines may be broken.
228 There are certain tokens comprised entirely of characters
229 matching this regexp that should not be split, and these are
230 specified by the constant `f90-no-break-re'."
234 (defcustom f90-break-before-delimiters t
235 "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
239 (defcustom f90-auto-keyword-case nil
240 "*Automatic case conversion of keywords.
241 The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil."
242 :type
'(choice (const downcase-word
) (const upcase-word
)
243 (const capitalize-word
) (const nil
))
246 (defcustom f90-leave-line-no nil
247 "*If non-nil, line numbers are not left justified."
251 (defcustom f90-mode-hook nil
252 "Hook run when entering F90 mode."
254 :options
'(f90-add-imenu-menu)
257 ;; User options end here.
259 (defconst f90-keywords-re
260 (regexp-opt '("allocatable" "allocate" "assign" "assignment" "backspace"
261 "block" "call" "case" "character" "close" "common" "complex"
262 "contains" "continue" "cycle" "data" "deallocate"
263 "dimension" "do" "double" "else" "elseif" "elsewhere" "end"
264 "enddo" "endfile" "endif" "entry" "equivalence" "exit"
265 "external" "forall" "format" "function" "goto" "if"
266 "implicit" "include" "inquire" "integer" "intent"
267 "interface" "intrinsic" "logical" "module" "namelist" "none"
268 "nullify" "only" "open" "operator" "optional" "parameter"
269 "pause" "pointer" "precision" "print" "private" "procedure"
270 "program" "public" "read" "real" "recursive" "result" "return"
271 "rewind" "save" "select" "sequence" "stop" "subroutine"
272 "target" "then" "type" "use" "where" "while" "write"
274 "elemental" "pure") 'words
)
275 "Regexp for F90 keywords.")
277 (defconst f90-keywords-level-3-re
279 '("allocatable" "allocate" "assign" "assignment" "backspace"
280 "close" "deallocate" "dimension" "endfile" "entry" "equivalence"
281 "external" "inquire" "intent" "intrinsic" "nullify" "only" "open"
282 "operator" "optional" "parameter" "pause" "pointer" "print" "private"
283 "public" "read" "recursive" "result" "rewind" "save" "select"
284 "sequence" "target" "write"
286 "elemental" "pure") 'words
)
287 "Keyword-regexp for font-lock level >= 3.")
289 (defconst f90-procedures-re
292 '("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint"
293 "all" "allocated" "anint" "any" "asin" "associated"
294 "atan" "atan2" "bit_size" "btest" "ceiling" "char" "cmplx"
295 "conjg" "cos" "cosh" "count" "cshift" "date_and_time" "dble"
296 "digits" "dim" "dot_product" "dprod" "eoshift" "epsilon"
297 "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
298 "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior"
299 "ishft" "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt"
300 "lle" "llt" "log" "log10" "logical" "matmul" "max"
301 "maxexponent" "maxloc" "maxval" "merge" "min" "minexponent"
302 "minloc" "minval" "mod" "modulo" "mvbits" "nearest" "nint"
303 "not" "pack" "precision" "present" "product" "radix"
304 ;; Real is taken out here to avoid highlighting declarations.
305 "random_number" "random_seed" "range" ;; "real"
306 "repeat" "reshape" "rrspacing" "scale" "scan"
307 "selected_int_kind" "selected_real_kind" "set_exponent"
308 "shape" "sign" "sin" "sinh" "size" "spacing" "spread" "sqrt"
309 "sum" "system_clock" "tan" "tanh" "tiny" "transfer"
310 "transpose" "trim" "ubound" "unpack" "verify"
311 ;; F95 intrinsic functions.
312 "null" "cpu_time") t
)
313 ;; A left parenthesis to avoid highlighting non-procedures.
315 "Regexp whose first part matches F90 intrinsic procedures.")
317 (defconst f90-operators-re
319 (regexp-opt '("and" "eq" "eqv" "false" "ge" "gt" "le" "lt" "ne"
320 "neqv" "not" "or" "true") t
)
322 "Regexp matching intrinsic operators.")
324 (defconst f90-hpf-keywords-re
326 ;; Intrinsic procedures.
327 '("all_prefix" "all_scatter" "all_suffix" "any_prefix"
328 "any_scatter" "any_suffix" "copy_prefix" "copy_scatter"
329 "copy_suffix" "count_prefix" "count_scatter" "count_suffix"
330 "grade_down" "grade_up"
331 "hpf_alignment" "hpf_distribution" "hpf_template" "iall" "iall_prefix"
332 "iall_scatter" "iall_suffix" "iany" "iany_prefix" "iany_scatter"
333 "iany_suffix" "ilen" "iparity" "iparity_prefix"
334 "iparity_scatter" "iparity_suffix" "leadz" "maxval_prefix"
335 "maxval_scatter" "maxval_suffix" "minval_prefix" "minval_scatter"
336 "minval_suffix" "number_of_processors" "parity"
337 "parity_prefix" "parity_scatter" "parity_suffix" "popcnt" "poppar"
338 "processors_shape" "product_prefix" "product_scatter"
339 "product_suffix" "sum_prefix" "sum_scatter" "sum_suffix"
341 "align" "distribute" "dynamic" "independent" "inherit" "processors"
342 "realign" "redistribute" "template"
344 "block" "cyclic" "extrinsic" "new" "onto" "pure" "with") 'words
)
345 "Regexp for all HPF keywords, procedures and directives.")
347 ;; Highlighting patterns.
349 (defvar f90-font-lock-keywords-1
351 ;; Special highlighting of "module procedure".
352 '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face
))
353 ;; Highlight definition of derived type.
354 '("\\<\\(\\(?:end[ \t]*\\)?type\\)\\>\\([^()\n]*::\\)?[ \t]*\\(\\sw+\\)"
355 (1 font-lock-keyword-face
) (3 font-lock-function-name-face
))
356 ;; Other functions and declarations.
357 '("\\<\\(\\(?:end[ \t]*\\)?\\(program\\|module\\|function\\|\
358 subroutine\\)\\|use\\|call\\)\\>[ \t]*\\(\\sw+\\)?"
359 (1 font-lock-keyword-face
) (3 font-lock-function-name-face nil t
))
360 "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
361 "This does fairly subdued highlighting of comments and function calls.")
363 (defvar f90-font-lock-keywords-2
365 f90-font-lock-keywords-1
367 ;; Variable declarations (avoid the real function call).
368 '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|\
369 logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\([^&!\n]*\\)"
370 (1 font-lock-type-face t
) (4 font-lock-variable-name-face t
))
371 ;; do, if, select, where, and forall constructs.
372 '("\\<\\(end[ \t]*\\(do\\|if\\|select\\|forall\\|where\\)\\)\\>\
373 \\([ \t]+\\(\\sw+\\)\\)?"
374 (1 font-lock-keyword-face
) (3 font-lock-constant-face nil t
))
375 '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|\
376 do\\([ \t]*while\\)?\\|select[ \t]*case\\|where\\|forall\\)\\)\\>"
377 (2 font-lock-constant-face nil t
) (3 font-lock-keyword-face
))
378 ;; Implicit declaration.
379 '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\
380 \\|logical\\|type[ \t]*(\\sw+)\\|none\\)[ \t]*"
381 (1 font-lock-keyword-face
) (2 font-lock-type-face
))
382 '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
383 (1 font-lock-keyword-face
) (2 font-lock-constant-face nil t
))
384 "\\<else\\([ \t]*if\\|where\\)?\\>"
385 '("\\(&\\)[ \t]*\\(!\\|$\\)" (1 font-lock-keyword-face
))
386 "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
387 '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>"
388 (1 font-lock-keyword-face
) (2 font-lock-constant-face nil t
))
389 '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" .
1)
390 '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
391 (1 font-lock-keyword-face
) (2 font-lock-constant-face
))
392 ;; Line numbers (lines whose first character after number is letter).
393 '("^[ \t]*\\([0-9]+\\)[ \t]*[a-z]+" (1 font-lock-constant-face t
))))
394 "Highlights declarations, do-loops and other constructs.")
396 (defvar f90-font-lock-keywords-3
397 (append f90-font-lock-keywords-2
399 f90-keywords-level-3-re
401 (list f90-procedures-re
'(1 font-lock-keyword-face keep
))
402 "\\<real\\>" ; avoid overwriting real defs
404 "Highlights all F90 keywords and intrinsic procedures.")
406 (defvar f90-font-lock-keywords-4
407 (append f90-font-lock-keywords-3
408 (list f90-hpf-keywords-re
))
409 "Highlights all F90 and HPF keywords.")
411 (defvar f90-font-lock-keywords
412 f90-font-lock-keywords-2
413 "*Default expressions to highlight in F90 mode.
414 Can be overridden by the value of `font-lock-maximum-decoration'.")
417 (defvar f90-mode-syntax-table
418 (let ((table (make-syntax-table)))
419 (modify-syntax-entry ?\
! "<" table
) ; begin comment
420 (modify-syntax-entry ?
\n ">" table
) ; end comment
421 (modify-syntax-entry ?_
"w" table
) ; underscore in names
422 (modify-syntax-entry ?
\' "\"" table
) ; string quote
423 (modify-syntax-entry ?
\" "\"" table
) ; string quote
424 (modify-syntax-entry ?\
` "w" table
) ; for abbrevs
425 (modify-syntax-entry ?
\r " " table
) ; return is whitespace
426 (modify-syntax-entry ?
+ "." table
) ; punctuation
427 (modify-syntax-entry ?-
"." table
)
428 (modify-syntax-entry ?
= "." table
)
429 (modify-syntax-entry ?
* "." table
)
430 (modify-syntax-entry ?
/ "." table
)
431 (modify-syntax-entry ?
\\ "\\" table
) ; escape chars
433 "Syntax table used in F90 mode.")
436 (let ((map (make-sparse-keymap)))
437 (define-key map
"`" 'f90-abbrev-start
)
438 (define-key map
"\C-c;" 'f90-comment-region
)
439 (define-key map
"\C-\M-a" 'f90-beginning-of-subprogram
)
440 (define-key map
"\C-\M-e" 'f90-end-of-subprogram
)
441 (define-key map
"\C-\M-h" 'f90-mark-subprogram
)
442 (define-key map
"\C-\M-n" 'f90-end-of-block
)
443 (define-key map
"\C-\M-p" 'f90-beginning-of-block
)
444 (define-key map
"\C-\M-q" 'f90-indent-subprogram
)
445 (define-key map
"\C-j" 'f90-indent-new-line
) ; LFD equals C-j
446 (define-key map
"\r" 'newline
)
447 (define-key map
"\C-c\r" 'f90-break-line
)
448 ;;; (define-key map [M-return] 'f90-break-line)
449 (define-key map
"\C-c\C-a" 'f90-previous-block
)
450 (define-key map
"\C-c\C-e" 'f90-next-block
)
451 (define-key map
"\C-c\C-d" 'f90-join-lines
)
452 (define-key map
"\C-c\C-f" 'f90-fill-region
)
453 (define-key map
"\C-c\C-p" 'f90-previous-statement
)
454 (define-key map
"\C-c\C-n" 'f90-next-statement
)
455 (define-key map
"\C-c\C-w" 'f90-insert-end
)
456 (define-key map
"\t" 'f90-indent-line
)
457 (define-key map
"," 'f90-electric-insert
)
458 (define-key map
"+" 'f90-electric-insert
)
459 (define-key map
"-" 'f90-electric-insert
)
460 (define-key map
"*" 'f90-electric-insert
)
461 (define-key map
"/" 'f90-electric-insert
)
463 (easy-menu-define f90-menu map
"Menu for F90 mode."
466 ,(custom-menu-create 'f90
)
468 ["Save" Custom-save t
]
469 ["Reset to Current" Custom-reset-current t
]
470 ["Reset to Saved" Custom-reset-saved t
]
471 ["Reset to Standard Settings" Custom-reset-standard t
]
474 ["Indent Subprogram" f90-indent-subprogram t
]
475 ["Mark Subprogram" f90-mark-subprogram t
]
476 ["Beginning of Subprogram" f90-beginning-of-subprogram t
]
477 ["End of Subprogram" f90-end-of-subprogram t
]
479 ["(Un)Comment Region" f90-comment-region mark-active
]
480 ["Indent Region" f90-indent-region mark-active
]
481 ["Fill Region" f90-fill-region mark-active
]
483 ["Break Line at Point" f90-break-line t
]
484 ["Join with Previous Line" f90-join-lines t
]
485 ["Insert Block End" f90-insert-end t
]
488 ["Toggle font-lock-mode" font-lock-mode
:selected font-lock-mode
491 ["Light highlighting (level 1)" f90-font-lock-1 t
]
492 ["Moderate highlighting (level 2)" f90-font-lock-2 t
]
493 ["Heavy highlighting (level 3)" f90-font-lock-3 t
]
494 ["Maximum highlighting (level 4)" f90-font-lock-4 t
]
496 ("Change Keyword Case"
497 ["Upcase Keywords (buffer)" f90-upcase-keywords t
]
498 ["Capitalize Keywords (buffer)" f90-capitalize-keywords t
]
499 ["Downcase Keywords (buffer)" f90-downcase-keywords t
]
501 ["Upcase Keywords (region)" f90-upcase-region-keywords
503 ["Capitalize Keywords (region)" f90-capitalize-region-keywords
505 ["Downcase Keywords (region)" f90-downcase-region-keywords
509 ["Toggle auto-fill" auto-fill-mode
:selected auto-fill-function
511 ["Toggle abbrev-mode" abbrev-mode
:selected abbrev-mode
513 ["Add imenu Menu" f90-add-imenu-menu
514 :active
(not (lookup-key (current-local-map) [menu-bar index
]))
515 :included
(fboundp 'imenu-add-to-menubar
)]))
517 "Keymap used in F90 mode.")
520 (defun f90-font-lock-1 ()
521 "Set `font-lock-keywords' to `f90-font-lock-keywords-1'."
524 (setq font-lock-keywords f90-font-lock-keywords-1
)
525 (font-lock-fontify-buffer))
527 (defun f90-font-lock-2 ()
528 "Set `font-lock-keywords' to `f90-font-lock-keywords-2'."
531 (setq font-lock-keywords f90-font-lock-keywords-2
)
532 (font-lock-fontify-buffer))
534 (defun f90-font-lock-3 ()
535 "Set `font-lock-keywords' to `f90-font-lock-keywords-3'."
538 (setq font-lock-keywords f90-font-lock-keywords-3
)
539 (font-lock-fontify-buffer))
541 (defun f90-font-lock-4 ()
542 "Set `font-lock-keywords' to `f90-font-lock-keywords-4'."
545 (setq font-lock-keywords f90-font-lock-keywords-4
)
546 (font-lock-fontify-buffer))
549 ;; Regexps for finding program structures.
550 (defconst f90-blocks-re
551 (concat "\\(block[ \t]*data\\|"
552 (regexp-opt '("do" "if" "interface" "function" "module" "program"
553 "select" "subroutine" "type" "where" "forall"))
555 "Regexp potentially indicating a \"block\" of F90 code.")
557 (defconst f90-program-block-re
558 (regexp-opt '("program" "module" "subroutine" "function") 'paren
)
559 "Regexp used to locate the start/end of a \"subprogram\".")
561 (defconst f90-else-like-re
562 "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\)"
563 "Regexp matching an ELSE IF, ELSEWHERE, CASE statement.")
565 (defconst f90-end-if-re
567 (regexp-opt '("if" "select" "where" "forall") 'paren
)
569 "Regexp matching the end of an IF, SELECT, WHERE, FORALL block.")
571 (defconst f90-end-type-re
572 "end[ \t]*\\(type\\|interface\\|block[ \t]*data\\)\\>"
573 "Regexp matching the end of a TYPE, INTERFACE, BLOCK DATA section.")
575 (defconst f90-type-def-re
576 "\\<\\(type\\)\\>\\(?:[^()\n]*::\\)?[ \t]*\\(\\sw+\\)"
577 "Regexp matching the definition of a derived type.")
579 (defconst f90-no-break-re
580 (regexp-opt '("**" "//" "=>" ">=" "<=" "==" "/=") 'paren
)
581 "Regexp specifying where not to break lines when filling.
582 This regexp matches certain tokens comprised entirely of
583 characters matching the regexp `f90-break-delimiters' that should
584 not be split by filling. Each element is assumed to be two
587 (defvar f90-cache-position nil
588 "Temporary position used to speed up region operations.")
589 (make-variable-buffer-local 'f90-cache-position
)
593 (defvar f90-imenu-generic-expression
594 (let ((good-char "[^!\"\&\n \t]") (not-e "[^e!\n\"\& \t]")
595 (not-n "[^n!\n\"\& \t]") (not-d "[^d!\n\"\& \t]"))
597 '(nil "^[ \t0-9]*program[ \t]+\\(\\sw+\\)" 1)
598 '("Modules" "^[ \t0-9]*module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)" 1)
599 '("Types" "^[ \t0-9]*type[ \t]+\\(\\sw+\\)" 1)
605 ;; At least three non-space characters before function/subroutine.
606 ;; Check that the last three non-space characters do not spell E N D.
608 not-e good-char good-char
"\\|"
609 good-char not-n good-char
"\\|"
610 good-char good-char not-d
"\\)"
612 ;; Less than three non-space characters before function/subroutine.
613 good-char
"?" good-char
"?"
615 "[ \t]*\\(function\\|subroutine\\)[ \t]+\\(\\sw+\\)")
617 "Value for `imenu-generic-expression' in F90 mode.")
619 (defun f90-add-imenu-menu ()
620 "Add an imenu menu to the menubar."
622 (if (lookup-key (current-local-map) [menu-bar index
])
623 (message "%s" "F90-imenu already exists.")
624 (imenu-add-to-menubar "F90-imenu")
625 (redraw-frame (selected-frame))))
628 ;; Abbrevs have generally two letters, except standard types `c, `i, `r, `t.
629 (defvar f90-mode-abbrev-table
630 (let (abbrevs-changed)
631 (define-abbrev-table 'f90-mode-abbrev-table nil
)
632 ;; Use the 6th arg (SYSTEM-FLAG) of define-abbrev if possible.
633 ;; A little baroque to quieten the byte-compiler.
635 (function (lambda (element)
637 (apply 'define-abbrev f90-mode-abbrev-table
638 (append element
'(nil 0 t
)))
639 (wrong-number-of-arguments
640 (apply 'define-abbrev f90-mode-abbrev-table
641 (append element
'(nil 0)))))))
642 '(("`al" "allocate" )
643 ("`ab" "allocatable" )
644 ("`as" "assignment" )
646 ("`bd" "block data" )
653 ("`de" "deallocate" )
659 ("`elw" "elsewhere" )
660 ("`eq" "equivalence" )
667 ("`im" "implicit none")
689 ("`su" "subroutine" )
695 f90-mode-abbrev-table
)
696 "Abbrev table for F90 mode.")
701 "Major mode for editing Fortran 90,95 code in free format.
702 For fixed format code, use `fortran-mode'.
704 \\[f90-indent-line] indents the current line.
705 \\[f90-indent-new-line] indents current line and creates a new\
707 \\[f90-indent-subprogram] indents the current subprogram.
709 Type `? or `\\[help-command] to display a list of built-in\
710 abbrevs for F90 keywords.
715 Variables controlling indentation style and extra features:
718 Extra indentation within do blocks (default 3).
720 Extra indentation within if/select case/where/forall blocks (default 3).
722 Extra indentation within type/interface/block-data blocks (default 3).
724 Extra indentation within program/module/subroutine/function blocks
726 `f90-continuation-indent'
727 Extra indentation applied to continuation lines (default 5).
729 String inserted by function \\[f90-comment-region] at start of each
730 line in region (default \"!!!$\").
731 `f90-indented-comment-re'
732 Regexp determining the type of comment to be intended like code
734 `f90-directive-comment-re'
735 Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented
736 (default \"!hpf\\\\$\").
737 `f90-break-delimiters'
738 Regexp holding list of delimiters at which lines may be broken
739 (default \"[-+*/><=,% \\t]\").
740 `f90-break-before-delimiters'
741 Non-nil causes `f90-do-auto-fill' to break lines before delimiters
743 `f90-beginning-ampersand'
744 Automatic insertion of \& at beginning of continuation lines (default t).
746 From an END statement, check and fill the end using matching block start.
747 Allowed values are 'blink, 'no-blink, and nil, which determine
748 whether to blink the matching beginning (default 'blink).
749 `f90-auto-keyword-case'
750 Automatic change of case of keywords (default nil).
751 The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
753 Do not left-justify line numbers (default nil).
755 List of keywords used for highlighting/upcase-keywords etc.
757 Turning on F90 mode calls the value of the variable `f90-mode-hook'
758 with no args, if that value is non-nil."
760 (kill-all-local-variables)
761 (setq major-mode
'f90-mode
763 local-abbrev-table f90-mode-abbrev-table
)
764 (set-syntax-table f90-mode-syntax-table
)
765 (use-local-map f90-mode-map
)
766 (set (make-local-variable 'indent-line-function
) 'f90-indent-line
)
767 (set (make-local-variable 'indent-region-function
) 'f90-indent-region
)
768 (set (make-local-variable 'require-final-newline
) t
)
769 (set (make-local-variable 'comment-start
) "!")
770 (set (make-local-variable 'comment-start-skip
) "!+ *")
771 (set (make-local-variable 'comment-indent-function
) 'f90-comment-indent
)
772 (set (make-local-variable 'abbrev-all-caps
) t
)
773 (set (make-local-variable 'normal-auto-fill-function
) 'f90-do-auto-fill
)
774 (setq indent-tabs-mode nil
) ; auto buffer local
775 (set (make-local-variable 'font-lock-defaults
)
776 '((f90-font-lock-keywords f90-font-lock-keywords-1
777 f90-font-lock-keywords-2
778 f90-font-lock-keywords-3
779 f90-font-lock-keywords-4
)
781 (set (make-local-variable 'imenu-case-fold-search
) t
)
782 (set (make-local-variable 'imenu-generic-expression
)
783 f90-imenu-generic-expression
)
784 (set (make-local-variable 'beginning-of-defun-function
)
785 'f90-beginning-of-subprogram
)
786 (set (make-local-variable 'end-of-defun-function
) 'f90-end-of-subprogram
)
787 (set (make-local-variable 'add-log-current-defun-function
)
789 (run-hooks 'f90-mode-hook
))
793 (defsubst f90-in-string
()
794 "Return non-nil if point is inside a string.
795 Checks from `point-min', or `f90-cache-position', if that is non-nil
796 and lies before point."
798 (if (and f90-cache-position
(> (point) f90-cache-position
))
801 (nth 3 (parse-partial-sexp beg-pnt
(point)))))
803 (defsubst f90-in-comment
()
804 "Return non-nil if point is inside a comment.
805 Checks from `point-min', or `f90-cache-position', if that is non-nil
806 and lies before point."
808 (if (and f90-cache-position
(> (point) f90-cache-position
))
811 (nth 4 (parse-partial-sexp beg-pnt
(point)))))
813 (defsubst f90-line-continued
()
814 "Return t if the current line is a continued one.
815 This includes comment lines embedded in continued lines, but
816 not the last line of a continued statement."
819 (while (and (looking-at "[ \t]*\\(!\\|$\\)") (zerop (forward-line -
1))))
821 (while (f90-in-comment)
822 (search-backward "!" (line-beginning-position))
823 (skip-chars-backward "!"))
824 (skip-chars-backward " \t")
825 (= (preceding-char) ?
&)))
827 ;; GM this is not right, eg a continuation line starting with a number.
828 ;; Need f90-code-start-position function.
829 ;; And yet, things seems to work with this...
830 ;; cf f90-indent-line
831 ;; (beginning-of-line) ; digits after & \n are not line-nos
832 ;; (if (not (save-excursion (and (f90-previous-statement)
833 ;; (f90-line-continued))))
834 ;; (f90-indent-line-no)
835 (defsubst f90-current-indentation
()
836 "Return indentation of current line.
837 Line-numbers are considered whitespace characters."
838 (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")))
840 (defsubst f90-indent-to
(col &optional no-line-number
)
841 "Indent current line to column COL.
842 If optional argument NO-LINE-NUMBER is nil, jump over a possible
843 line-number before indenting."
846 (skip-chars-forward " \t0-9"))
847 (delete-horizontal-space)
848 ;; Leave >= 1 space after line number.
849 (indent-to col
(if (zerop (current-column)) 0 1)))
851 (defsubst f90-get-present-comment-type
()
852 "If point lies within a comment, return the string starting the comment.
853 For example, \"!\" or \"!!\"."
855 (when (f90-in-comment)
857 (re-search-forward "!+" (line-end-position))
858 (while (f90-in-string)
859 (re-search-forward "!+" (line-end-position)))
862 (defsubst f90-equal-symbols
(a b
)
863 "Compare strings A and B neglecting case and allowing for nil value."
864 (equal (if a
(downcase a
) nil
)
865 (if b
(downcase b
) nil
)))
867 (defsubst f90-looking-at-do
()
868 "Return (\"do\" NAME) if a do statement starts after point.
869 NAME is nil if the statement has no label."
870 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(do\\)\\>")
871 (list (match-string 3) (match-string 2))))
873 (defsubst f90-looking-at-select-case
()
874 "Return (\"select\" NAME) if a select-case statement starts after point.
875 NAME is nil if the statement has no label."
876 (if (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
877 \\(select\\)[ \t]*case[ \t]*(")
878 (list (match-string 3) (match-string 2))))
880 (defsubst f90-looking-at-if-then
()
881 "Return (\"if\" NAME) if an if () then statement starts after point.
882 NAME is nil if the statement has no label."
884 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\\(if\\)\\>")
885 (let ((struct (match-string 3))
886 (label (match-string 2))
887 (pos (scan-lists (point) 1 0)))
888 (and pos
(goto-char pos
))
889 (skip-chars-forward " \t")
890 (if (or (looking-at "then\\>")
891 (when (f90-line-continued)
893 (skip-chars-forward " \t0-9&")
894 (looking-at "then\\>")))
895 (list struct label
))))))
897 (defsubst f90-looking-at-where-or-forall
()
898 "Return (KIND NAME) if a where or forall block starts after point.
899 NAME is nil if the statement has no label."
901 (when (looking-at "\\(\\(\\sw+\\)[ \t]*:\\)?[ \t]*\
902 \\(where\\|forall\\)\\>")
903 (let ((struct (match-string 3))
904 (label (match-string 2))
905 (pos (scan-lists (point) 1 0)))
906 (and pos
(goto-char pos
))
907 (skip-chars-forward " \t")
908 (if (looking-at "\\(!\\|$\\)") (list struct label
))))))
910 (defsubst f90-looking-at-type-like
()
911 "Return (KIND NAME) if a type/interface/block-data block starts after point.
912 NAME is non-nil only for type."
914 ((looking-at f90-type-def-re
)
915 (list (match-string 1) (match-string 2)))
916 ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
917 (list (match-string 1) nil
))))
919 (defsubst f90-looking-at-program-block-start
()
920 "Return (KIND NAME) if a program block with name NAME starts after point."
922 ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
923 (list (match-string 1) (match-string 2)))
924 ((and (not (looking-at "module[ \t]*procedure\\>"))
925 (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
926 (list (match-string 1) (match-string 2)))
927 ((and (not (looking-at "end[ \t]*\\(function\\|subroutine\\)"))
928 (looking-at "[^!'\"\&\n]*\\(function\\|subroutine\\)[ \t]+\
930 (list (match-string 1) (match-string 2)))))
932 (defsubst f90-looking-at-program-block-end
()
933 "Return (KIND NAME) if a block with name NAME ends after point."
934 (if (looking-at (concat "end[ \t]*" f90-blocks-re
935 "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
936 (list (match-string 1) (match-string 3))))
938 (defsubst f90-comment-indent
()
939 "Return the indentation to be used for a comment starting at point.
940 Used for `comment-indent-function' by F90 mode.
941 \"!!!\", `f90-directive-comment-re', variable `f90-comment-region' return 0.
942 `f90-indented-comment-re' (if not trailing code) calls `f90-calculate-indent'.
943 All others return `comment-column', leaving at least one space after code."
944 (cond ((looking-at "!!!") 0)
945 ((and f90-directive-comment-re
946 (looking-at f90-directive-comment-re
)) 0)
947 ((looking-at (regexp-quote f90-comment-region
)) 0)
948 ((and (looking-at f90-indented-comment-re
)
949 ;; Don't attempt to indent trailing comment as code.
951 (skip-chars-backward " \t")
953 (f90-calculate-indent))
954 (t (skip-chars-backward " \t")
955 (max (if (bolp) 0 (1+ (current-column))) comment-column
))))
957 (defsubst f90-present-statement-cont
()
958 "Return continuation properties of present statement.
959 Possible return values are:
960 single - statement is not continued.
961 begin - current line is the first in a continued statement.
962 end - current line is the last in a continued statement
963 middle - current line is neither first nor last in a continued statement.
964 Comment lines embedded amongst continued lines return 'middle."
967 (setq pcont
(if (f90-previous-statement) (f90-line-continued))))
968 (setq cont
(f90-line-continued))
969 (cond ((and (not pcont
) (not cont
)) 'single
)
970 ((and (not pcont
) cont
) 'begin
)
971 ((and pcont
(not cont
)) 'end
)
972 ((and pcont cont
) 'middle
)
973 (t (error "The impossible occurred")))))
975 (defsubst f90-indent-line-no
()
976 "If `f90-leave-line-no' is nil, left-justify a line number.
977 Leaves point at the first non-blank character after the line number.
978 Call from beginning of line."
979 (and (null f90-leave-line-no
) (looking-at "[ \t]+[0-9]")
980 (delete-horizontal-space))
981 (skip-chars-forward " \t0-9"))
983 (defsubst f90-no-block-limit
()
984 "Return nil if point is at the edge of a code block.
985 Searches line forward for \"function\" or \"subroutine\",
988 (not (or (looking-at "end")
989 (looking-at "\\(do\\|if\\|else\\(if\\|where\\)?\
990 \\|select[ \t]*case\\|case\\|where\\|forall\\)\\>")
991 (looking-at "\\(program\\|module\\|interface\\|\
992 block[ \t]*data\\)\\>")
993 (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
994 (looking-at f90-type-def-re
)
995 (re-search-forward "\\(function\\|subroutine\\)"
996 (line-end-position) t
)))))
998 (defsubst f90-update-line
()
999 "Change case of current line as per `f90-auto-keyword-case'."
1000 (if f90-auto-keyword-case
1001 (f90-change-keywords f90-auto-keyword-case
1002 (line-beginning-position) (line-end-position))))
1004 (defun f90-electric-insert ()
1005 "Change keyword case and auto-fill line as operators are inserted."
1007 (self-insert-command 1)
1008 (if auto-fill-function
(f90-do-auto-fill) ; also updates line
1012 (defun f90-get-correct-indent ()
1013 "Get correct indent for a line starting with line number.
1014 Does not check type and subprogram indentation."
1015 (let ((epnt (line-end-position)) icol cont
)
1017 (while (and (f90-previous-statement)
1019 (setq cont
(f90-present-statement-cont))
1020 (or (eq cont
'end
) (eq cont
'middle
)))
1021 (looking-at "[ \t]*[0-9]"))))
1022 (setq icol
(current-indentation))
1024 (when (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
1025 (line-end-position) t
)
1027 (skip-chars-forward " \t")
1028 (cond ((f90-looking-at-do)
1029 (setq icol
(+ icol f90-do-indent
)))
1030 ((or (f90-looking-at-if-then)
1031 (f90-looking-at-where-or-forall)
1032 (f90-looking-at-select-case))
1033 (setq icol
(+ icol f90-if-indent
))))
1035 (while (re-search-forward
1036 "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t
)
1038 (skip-chars-forward " \t0-9")
1039 (cond ((f90-looking-at-do)
1040 (setq icol
(+ icol f90-do-indent
)))
1041 ((or (f90-looking-at-if-then)
1042 (f90-looking-at-where-or-forall)
1043 (f90-looking-at-select-case))
1044 (setq icol
(+ icol f90-if-indent
)))
1045 ((looking-at f90-end-if-re
)
1046 (setq icol
(- icol f90-if-indent
)))
1047 ((looking-at "end[ \t]*do\\>")
1048 (setq icol
(- icol f90-do-indent
))))
1052 (defun f90-calculate-indent ()
1053 "Calculate the indent column based on previous statements."
1055 (let (icol cont
(case-fold-search t
) (pnt (point)))
1057 (if (not (f90-previous-statement))
1059 (setq cont
(f90-present-statement-cont))
1061 (while (not (eq 'begin
(f90-present-statement-cont)))
1062 (f90-previous-statement)))
1063 (cond ((eq cont
'begin
)
1064 (setq icol
(+ (f90-current-indentation)
1065 f90-continuation-indent
)))
1066 ((eq cont
'middle
) (setq icol
(current-indentation)))
1067 (t (setq icol
(f90-current-indentation))
1068 (skip-chars-forward " \t")
1069 (if (looking-at "[0-9]")
1070 (setq icol
(f90-get-correct-indent))
1071 (cond ((or (f90-looking-at-if-then)
1072 (f90-looking-at-where-or-forall)
1073 (f90-looking-at-select-case)
1074 (looking-at f90-else-like-re
))
1075 (setq icol
(+ icol f90-if-indent
)))
1076 ((f90-looking-at-do)
1077 (setq icol
(+ icol f90-do-indent
)))
1078 ((f90-looking-at-type-like)
1079 (setq icol
(+ icol f90-type-indent
)))
1080 ((or (f90-looking-at-program-block-start)
1081 (looking-at "contains[ \t]*\\($\\|!\\)"))
1082 (setq icol
(+ icol f90-program-indent
)))))
1085 (cond ((looking-at "[ \t]*$"))
1086 ((looking-at "[ \t]*#") ; check for cpp directive
1089 (skip-chars-forward " \t0-9")
1090 (cond ((or (looking-at f90-else-like-re
)
1091 (looking-at f90-end-if-re
))
1092 (setq icol
(- icol f90-if-indent
)))
1093 ((looking-at "end[ \t]*do\\>")
1094 (setq icol
(- icol f90-do-indent
)))
1095 ((looking-at f90-end-type-re
)
1096 (setq icol
(- icol f90-type-indent
)))
1097 ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
1098 (f90-looking-at-program-block-end))
1099 (setq icol
(- icol f90-program-indent
))))))
1103 (defun f90-previous-statement ()
1104 "Move point to beginning of the previous F90 statement.
1105 Return nil if no previous statement is found.
1106 A statement is a line which is neither blank nor a comment."
1108 (let (not-first-statement)
1110 (while (and (setq not-first-statement
(zerop (forward-line -
1)))
1111 (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
1112 not-first-statement
))
1114 (defun f90-next-statement ()
1115 "Move point to beginning of the next F90 statement.
1116 Return nil if no later statement is found."
1118 (let (not-last-statement)
1120 (while (and (setq not-last-statement
1121 (and (zerop (forward-line 1))
1123 (looking-at "[ \t0-9]*\\(!\\|$\\)")))
1124 not-last-statement
))
1126 (defun f90-beginning-of-subprogram ()
1127 "Move point to the beginning of the current subprogram.
1128 Return (TYPE NAME), or nil if not found."
1130 (let ((count 1) (case-fold-search t
) matching-beg
)
1132 (while (and (> count
0)
1133 (re-search-backward f90-program-block-re nil
'move
))
1135 (skip-chars-forward " \t0-9")
1136 (cond ((setq matching-beg
(f90-looking-at-program-block-start))
1137 (setq count
(1- count
)))
1138 ((f90-looking-at-program-block-end)
1139 (setq count
(1+ count
)))))
1143 (message "No beginning found.")
1146 (defun f90-end-of-subprogram ()
1147 "Move point to the end of the current subprogram.
1148 Return (TYPE NAME), or nil if not found."
1150 (let ((case-fold-search t
)
1154 (while (and (> count
0)
1155 (re-search-forward f90-program-block-re nil
'move
))
1157 (skip-chars-forward " \t0-9")
1158 (cond ((f90-looking-at-program-block-start)
1159 (setq count
(1+ count
)))
1160 ((setq matching-end
(f90-looking-at-program-block-end))
1161 (setq count
(1- count
))))
1163 ;; This means f90-end-of-subprogram followed by f90-start-of-subprogram
1164 ;; has a net non-zero effect, which seems odd.
1165 ;;; (forward-line 1)
1168 (message "No end found.")
1172 (defun f90-end-of-block (&optional num
)
1173 "Move point forward to the end of the current code block.
1174 With optional argument NUM, go forward that many balanced blocks.
1175 If NUM is negative, go backward to the start of a block.
1176 Checks for consistency of block types and labels (if present),
1177 and completes outermost block if necessary."
1179 (if (and num
(< num
0)) (f90-beginning-of-block (- num
)))
1180 (let ((f90-smart-end nil
) ; for the final `f90-match-end'
1181 (case-fold-search t
)
1183 start-list start-this start-type start-label end-type end-label
)
1184 (if (interactive-p) (push-mark (point) t
))
1185 (end-of-line) ; probably want this
1186 (while (and (> count
0) (re-search-forward f90-blocks-re nil
'move
))
1188 (skip-chars-forward " \t0-9")
1189 (cond ((or (f90-in-string) (f90-in-comment)))
1193 (f90-looking-at-select-case)
1194 (f90-looking-at-type-like)
1195 (f90-looking-at-program-block-start)
1196 (f90-looking-at-if-then)
1197 (f90-looking-at-where-or-forall)))
1198 (setq start-list
(cons start-this start-list
) ; not add-to-list!
1200 ((looking-at (concat "end[ \t]*" f90-blocks-re
1201 "[ \t]*\\(\\sw+\\)?"))
1202 (setq end-type
(match-string 1)
1203 end-label
(match-string 2)
1205 ;; Check any internal blocks.
1207 (setq start-this
(car start-list
)
1208 start-list
(cdr start-list
)
1209 start-type
(car start-this
)
1210 start-label
(cadr start-this
))
1211 (or (f90-equal-symbols start-type end-type
)
1212 (error "End type `%s' does not match start type `%s'"
1213 end-type start-type
))
1214 (or (f90-equal-symbols start-label end-label
)
1215 (error "End label `%s' does not match start label `%s'"
1216 end-label start-label
)))))
1218 (if (> count
0) (error "Missing block end"))
1219 ;; Check outermost block.
1223 (skip-chars-forward " \t0-9")
1226 (defun f90-beginning-of-block (&optional num
)
1227 "Move point backwards to the start of the current code block.
1228 With optional argument NUM, go backward that many balanced blocks.
1229 If NUM is negative, go forward to the end of a block.
1230 Checks for consistency of block types and labels (if present).
1231 Does not check the outermost block, because it may be incomplete."
1233 (if (and num
(< num
0)) (f90-end-of-block (- num
)))
1234 (let ((case-fold-search t
)
1236 end-list end-this end-type end-label
1237 start-this start-type start-label
)
1238 (if (interactive-p) (push-mark (point) t
))
1239 (beginning-of-line) ; probably want this
1240 (while (and (> count
0) (re-search-backward f90-blocks-re nil
'move
))
1242 (skip-chars-forward " \t0-9")
1243 (cond ((or (f90-in-string) (f90-in-comment)))
1244 ((looking-at (concat "end[ \t]*" f90-blocks-re
1245 "[ \t]*\\(\\sw+\\)?"))
1246 (setq end-list
(cons (list (match-string 1) (match-string 2))
1252 (f90-looking-at-select-case)
1253 (f90-looking-at-type-like)
1254 (f90-looking-at-program-block-start)
1255 (f90-looking-at-if-then)
1256 (f90-looking-at-where-or-forall)))
1257 (setq start-type
(car start-this
)
1258 start-label
(cadr start-this
)
1260 ;; Check any internal blocks.
1262 (setq end-this
(car end-list
)
1263 end-list
(cdr end-list
)
1264 end-type
(car end-this
)
1265 end-label
(cadr end-this
))
1266 (or (f90-equal-symbols start-type end-type
)
1267 (error "Start type `%s' does not match end type `%s'"
1268 start-type end-type
))
1269 (or (f90-equal-symbols start-label end-label
)
1270 (error "Start label `%s' does not match end label `%s'"
1271 start-label end-label
))))))
1272 (if (> count
0) (error "Missing block start"))))
1274 (defun f90-next-block (&optional num
)
1275 "Move point forward to the next end or start of a code block.
1276 With optional argument NUM, go forward that many blocks.
1277 If NUM is negative, go backwards.
1278 A block is a subroutine, if-endif, etc."
1280 (let ((case-fold-search t
)
1281 (count (if num
(abs num
) 1)))
1282 (while (and (> count
0)
1283 (if (> num
0) (re-search-forward f90-blocks-re nil
'move
)
1284 (re-search-backward f90-blocks-re nil
'move
)))
1286 (skip-chars-forward " \t0-9")
1287 (cond ((or (f90-in-string) (f90-in-comment)))
1289 (looking-at "end[ \t]*")
1291 (f90-looking-at-select-case)
1292 (f90-looking-at-type-like)
1293 (f90-looking-at-program-block-start)
1294 (f90-looking-at-if-then)
1295 (f90-looking-at-where-or-forall))
1296 (setq count
(1- count
))))
1297 (if (> num
0) (end-of-line)
1298 (beginning-of-line)))))
1301 (defun f90-previous-block (&optional num
)
1302 "Move point backward to the previous end or start of a code block.
1303 With optional argument NUM, go backward that many blocks.
1304 If NUM is negative, go forwards.
1305 A block is a subroutine, if-endif, etc."
1307 (f90-next-block (- (or num
1))))
1310 (defun f90-mark-subprogram ()
1311 "Put mark at end of F90 subprogram, point at beginning, push mark."
1313 (let ((pos (point)) program
)
1314 (f90-end-of-subprogram)
1317 (setq program
(f90-beginning-of-subprogram))
1318 (if (fboundp 'zmacs-activate-region
)
1319 (zmacs-activate-region)
1321 deactivate-mark nil
))
1324 (defun f90-comment-region (beg-region end-region
)
1325 "Comment/uncomment every line in the region.
1326 Insert the variable `f90-comment-region' at the start of every line
1327 in the region, or, if already present, remove it."
1329 (let ((end (copy-marker end-region
)))
1330 (goto-char beg-region
)
1332 (if (looking-at (regexp-quote f90-comment-region
))
1333 (delete-region (point) (match-end 0))
1334 (insert f90-comment-region
))
1335 (while (and (zerop (forward-line 1))
1337 (if (looking-at (regexp-quote f90-comment-region
))
1338 (delete-region (point) (match-end 0))
1339 (insert f90-comment-region
)))
1340 (set-marker end nil
)))
1342 (defun f90-indent-line (&optional no-update
)
1343 "Indent current line as F90 code.
1344 Unless optional argument NO-UPDATE is non-nil, call `f90-update-line'
1347 (let ((case-fold-search t
)
1348 (pos (point-marker))
1349 indent no-line-number
)
1350 (beginning-of-line) ; digits after & \n are not line-nos
1351 (if (not (save-excursion (and (f90-previous-statement)
1352 (f90-line-continued))))
1353 (f90-indent-line-no)
1354 (setq no-line-number t
)
1355 (skip-chars-forward " \t"))
1356 (if (looking-at "!")
1357 (setq indent
(f90-comment-indent))
1358 (and f90-smart-end
(looking-at "end")
1360 (setq indent
(f90-calculate-indent)))
1361 (or (= indent
(current-column))
1362 (f90-indent-to indent no-line-number
))
1363 ;; If initial point was within line's indentation,
1364 ;; position after the indentation. Else stay at same point in text.
1365 (and (< (point) pos
)
1367 (if auto-fill-function
1368 ;; GM NO-UPDATE not honoured, since this calls f90-update-line.
1370 (or no-update
(f90-update-line)))
1371 (set-marker pos nil
)))
1373 (defun f90-indent-new-line ()
1374 "Re-indent current line, insert a newline and indent the newline.
1375 An abbrev before point is expanded if the variable `abbrev-mode' is non-nil.
1376 If run in the middle of a line, the line is not broken."
1378 (if abbrev-mode
(expand-abbrev))
1379 (beginning-of-line) ; reindent where likely to be needed
1380 (f90-indent-line) ; calls indent-line-no, update-line
1382 (delete-horizontal-space) ; destroy trailing whitespace
1383 (let ((string (f90-in-string))
1384 (cont (f90-line-continued)))
1385 (and string
(not cont
) (insert "&"))
1387 (if (or string
(and cont f90-beginning-ampersand
)) (insert "&")))
1388 (f90-indent-line 'no-update
)) ; nothing to update
1391 (defun f90-indent-region (beg-region end-region
)
1392 "Indent every line in region by forward parsing."
1394 (let ((end-region-mark (copy-marker end-region
))
1395 (save-point (point-marker))
1396 block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct
)
1397 (goto-char beg-region
)
1398 ;; First find a line which is not a continuation line or comment.
1400 (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
1401 (progn (f90-indent-line 'no-update
)
1402 (zerop (forward-line 1)))
1403 (< (point) end-region-mark
)))
1404 (setq cont
(f90-present-statement-cont))
1405 (while (and (or (eq cont
'middle
) (eq cont
'end
))
1406 (f90-previous-statement))
1407 (setq cont
(f90-present-statement-cont)))
1408 ;; Process present line for beginning of block.
1409 (setq f90-cache-position
(point))
1410 (f90-indent-line 'no-update
)
1411 (setq ind-lev
(f90-current-indentation)
1414 (skip-chars-forward " \t0-9")
1416 ind-b
(cond ((setq struct
(f90-looking-at-do)) f90-do-indent
)
1417 ((or (setq struct
(f90-looking-at-if-then))
1418 (setq struct
(f90-looking-at-select-case))
1419 (setq struct
(f90-looking-at-where-or-forall))
1420 (looking-at f90-else-like-re
))
1422 ((setq struct
(f90-looking-at-type-like))
1424 ((or (setq struct
(f90-looking-at-program-block-start))
1425 (looking-at "contains[ \t]*\\($\\|!\\)"))
1426 f90-program-indent
)))
1427 (if ind-b
(setq ind-lev
(+ ind-lev ind-b
)))
1428 (if struct
(setq block-list
(cons struct block-list
)))
1429 (while (and (f90-line-continued) (zerop (forward-line 1))
1430 (< (point) end-region-mark
))
1431 (if (looking-at "[ \t]*!")
1432 (f90-indent-to (f90-comment-indent))
1433 (or (= (current-indentation)
1434 (+ ind-curr f90-continuation-indent
))
1435 (f90-indent-to (+ ind-curr f90-continuation-indent
) 'no-line-no
))))
1436 ;; Process all following lines.
1437 (while (and (zerop (forward-line 1)) (< (point) end-region-mark
))
1439 (f90-indent-line-no)
1440 (setq f90-cache-position
(point))
1441 (cond ((looking-at "[ \t]*$") (setq ind-curr
0))
1442 ((looking-at "[ \t]*#") (setq ind-curr
0))
1443 ((looking-at "!") (setq ind-curr
(f90-comment-indent)))
1444 ((f90-no-block-limit) (setq ind-curr ind-lev
))
1445 ((looking-at f90-else-like-re
) (setq ind-curr
1446 (- ind-lev f90-if-indent
)))
1447 ((looking-at "contains[ \t]*\\($\\|!\\)")
1448 (setq ind-curr
(- ind-lev f90-program-indent
)))
1450 (cond ((setq struct
(f90-looking-at-do)) f90-do-indent
)
1451 ((or (setq struct
(f90-looking-at-if-then))
1452 (setq struct
(f90-looking-at-select-case))
1453 (setq struct
(f90-looking-at-where-or-forall)))
1455 ((setq struct
(f90-looking-at-type-like))
1457 ((setq struct
(f90-looking-at-program-block-start))
1458 f90-program-indent
)))
1459 (setq ind-curr ind-lev
)
1460 (if ind-b
(setq ind-lev
(+ ind-lev ind-b
)))
1461 (setq block-list
(cons struct block-list
)))
1462 ((setq end-struct
(f90-looking-at-program-block-end))
1463 (setq beg-struct
(car block-list
)
1464 block-list
(cdr block-list
))
1467 (f90-block-match (car beg-struct
) (car (cdr beg-struct
))
1468 (car end-struct
) (car (cdr end-struct
)))))
1470 (cond ((looking-at f90-end-if-re
) f90-if-indent
)
1471 ((looking-at "end[ \t]*do\\>") f90-do-indent
)
1472 ((looking-at f90-end-type-re
) f90-type-indent
)
1473 ((f90-looking-at-program-block-end)
1474 f90-program-indent
)))
1475 (if ind-b
(setq ind-lev
(- ind-lev ind-b
)))
1476 (setq ind-curr ind-lev
))
1477 (t (setq ind-curr ind-lev
)))
1478 ;; Do the indentation if necessary.
1479 (or (= ind-curr
(current-column))
1480 (f90-indent-to ind-curr
))
1481 (while (and (f90-line-continued) (zerop (forward-line 1))
1482 (< (point) end-region-mark
))
1483 (if (looking-at "[ \t]*!")
1484 (f90-indent-to (f90-comment-indent))
1485 (or (= (current-indentation)
1486 (+ ind-curr f90-continuation-indent
))
1488 (+ ind-curr f90-continuation-indent
) 'no-line-no
)))))
1489 ;; Restore point, etc.
1490 (setq f90-cache-position nil
)
1491 (goto-char save-point
)
1492 (set-marker end-region-mark nil
)
1493 (set-marker save-point nil
)
1494 (if (fboundp 'zmacs-deactivate-region
)
1495 (zmacs-deactivate-region)
1496 (deactivate-mark))))
1498 (defun f90-indent-subprogram ()
1499 "Properly indent the subprogram containing point."
1502 (let ((program (f90-mark-subprogram)))
1505 (message "Indenting %s %s..."
1506 (car program
) (car (cdr program
)))
1507 (indent-region (point) (mark) nil
)
1508 (message "Indenting %s %s...done"
1509 (car program
) (car (cdr program
))))
1510 (message "Indenting the whole file...")
1511 (indent-region (point) (mark) nil
)
1512 (message "Indenting the whole file...done")))))
1514 (defun f90-break-line (&optional no-update
)
1515 "Break line at point, insert continuation marker(s) and indent.
1516 Unless in a string or comment, or if the optional argument NO-UPDATE
1517 is non-nil, call `f90-update-line' after inserting the continuation marker."
1519 (cond ((f90-in-string)
1522 (insert "\n" (f90-get-present-comment-type)))
1524 (or no-update
(f90-update-line))
1526 (if f90-beginning-ampersand
(insert "&"))))
1527 (indent-according-to-mode))
1529 (defun f90-find-breakpoint ()
1530 "From `fill-column', search backward for break-delimiter."
1531 (re-search-backward f90-break-delimiters
(line-beginning-position))
1532 (if (not f90-break-before-delimiters
)
1533 (forward-char (if (looking-at f90-no-break-re
) 2 1))
1535 (or (looking-at f90-no-break-re
)
1538 (defun f90-do-auto-fill ()
1539 "Break line if non-white characters beyond `fill-column'.
1540 Update keyword case first."
1542 ;; Break line before or after last delimiter (non-word char) if
1543 ;; position is beyond fill-column.
1544 ;; Will not break **, //, or => (as specified by f90-no-break-re).
1546 ;; Need this for `f90-electric-insert' and other f90- callers.
1547 (unless (and (boundp 'comment-auto-fill-only-comments
)
1548 comment-auto-fill-only-comments
1549 (not (f90-in-comment)))
1550 (while (> (current-column) fill-column
)
1551 (let ((pos-mark (point-marker)))
1552 (move-to-column fill-column
)
1553 (or (f90-in-string) (f90-find-breakpoint))
1555 (goto-char pos-mark
)
1556 (set-marker pos-mark nil
)))))
1558 (defun f90-join-lines (&optional arg
)
1559 "Join current line to previous, fix whitespace, continuation, comments.
1560 With optional argument ARG, join current line to following line.
1561 Like `join-line', but handles F90 syntax."
1564 (if arg
(forward-line 1))
1565 (when (eq (preceding-char) ?
\n)
1566 (skip-chars-forward " \t")
1567 (if (looking-at "\&") (delete-char 1))
1569 (delete-region (point) (1- (point)))
1570 (skip-chars-backward " \t")
1571 (and (eq (preceding-char) ?
&) (delete-char -
1))
1572 (and (f90-in-comment)
1573 (looking-at "[ \t]*!+")
1576 (fixup-whitespace))))
1578 (defun f90-fill-region (beg-region end-region
)
1579 "Fill every line in region by forward parsing. Join lines if possible."
1581 (let ((end-region-mark (copy-marker end-region
))
1583 f90-smart-end f90-auto-keyword-case auto-fill-function
)
1584 (goto-char beg-region
)
1586 ;; Join as much as possible.
1589 (skip-chars-backward " \t")
1590 (eq (preceding-char) ?
&))
1591 (f90-join-lines 'forward
))
1592 ;; Chop the line if necessary.
1593 (while (> (save-excursion (end-of-line) (current-column))
1595 (move-to-column fill-column
)
1596 (f90-find-breakpoint)
1597 (f90-break-line 'no-update
))
1598 (setq go-on
(and (< (point) end-region-mark
)
1599 (zerop (forward-line 1)))
1600 f90-cache-position
(point)))
1601 (setq f90-cache-position nil
)
1602 (set-marker end-region-mark nil
)
1603 (if (fboundp 'zmacs-deactivate-region
)
1604 (zmacs-deactivate-region)
1605 (deactivate-mark))))
1607 (defun f90-block-match (beg-block beg-name end-block end-name
)
1608 "Match end-struct with beg-struct and complete end-block if possible.
1609 BEG-BLOCK is the type of block as indicated at the start (e.g., do).
1610 BEG-NAME is the block start name (may be nil).
1611 END-BLOCK is the type of block as indicated at the end (may be nil).
1612 END-NAME is the block end name (may be nil).
1613 Leave point at the end of line."
1614 (search-forward "end" (line-end-position))
1616 (if (f90-equal-symbols beg-block end-block
)
1617 (search-forward end-block
)
1620 (message "END %s does not match %s." end-block beg-block
)
1622 (throw 'no-match nil
))
1623 (message "Inserting %s." beg-block
)
1624 (insert (concat " " beg-block
))))
1625 (if (f90-equal-symbols beg-name end-name
)
1626 (and end-name
(search-forward end-name
))
1627 (cond ((and beg-name
(not end-name
))
1628 (message "Inserting %s." beg-name
)
1629 (insert (concat " " beg-name
)))
1630 ((and beg-name end-name
)
1631 (message "Replacing %s with %s." end-name beg-name
)
1632 (search-forward end-name
)
1633 (replace-match beg-name
))
1634 ((and (not beg-name
) end-name
)
1635 (message "Deleting %s." end-name
)
1636 (search-forward end-name
)
1637 (replace-match ""))))
1638 (or (looking-at "[ \t]*!") (delete-horizontal-space))))
1640 (defun f90-match-end ()
1641 "From an end block statement, find the corresponding block and name."
1644 (top-of-window (window-start))
1646 (case-fold-search t
)
1647 matching-beg beg-name end-name beg-block end-block end-struct
)
1648 (when (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
1649 (setq end-struct
(f90-looking-at-program-block-end)))
1650 (setq end-block
(car end-struct
)
1651 end-name
(car (cdr end-struct
)))
1654 (while (and (> count
0) (re-search-backward f90-blocks-re nil t
))
1656 ;; GM not a line number if continued line.
1657 ;;; (skip-chars-forward " \t")
1658 ;;; (skip-chars-forward "0-9")
1659 (skip-chars-forward " \t0-9")
1660 (cond ((or (f90-in-string) (f90-in-comment)))
1664 (f90-looking-at-if-then)
1665 (f90-looking-at-where-or-forall)
1666 (f90-looking-at-select-case)
1667 (f90-looking-at-type-like)
1668 (f90-looking-at-program-block-start)))
1669 (setq count
(1- count
)))
1670 ((looking-at (concat "end[ \t]*" f90-blocks-re
))
1671 (setq count
(1+ count
)))))
1673 (message "No matching beginning.")
1675 (if (eq f90-smart-end
'blink
)
1676 (if (< (point) top-of-window
)
1677 (message "Matches %s: %s"
1680 (line-beginning-position)
1681 (line-end-position)))
1683 (setq beg-block
(car matching-beg
)
1684 beg-name
(car (cdr matching-beg
)))
1685 (goto-char end-point
)
1687 (f90-block-match beg-block beg-name end-block end-name
))))))
1689 (defun f90-insert-end ()
1690 "Insert a complete end statement matching beginning of present block."
1692 (let ((f90-smart-end (or f90-smart-end
'blink
)))
1694 (f90-indent-new-line)))
1696 ;; Abbrevs and keywords.
1698 (defun f90-abbrev-start ()
1699 "Typing `\\[help-command] or `? lists all the F90 abbrevs.
1700 Any other key combination is executed normally."
1702 (insert last-command-char
)
1704 (if (fboundp 'next-command-event
) ; XEmacs
1705 (setq event
(next-command-event)
1706 char
(event-to-character event
))
1707 (setq event
(read-event)
1709 ;; Insert char if not equal to `?', or if abbrev-mode is off.
1710 (if (and abbrev-mode
(or (eq char ??
) (eq char help-char
)))
1712 (setq unread-command-events
(list event
)))))
1714 (defun f90-abbrev-help ()
1715 "List the currently defined abbrevs in F90 mode."
1717 (message "Listing abbrev table...")
1718 (display-buffer (f90-prepare-abbrev-list-buffer))
1719 (message "Listing abbrev table...done"))
1721 (defun f90-prepare-abbrev-list-buffer ()
1722 "Create a buffer listing the F90 mode abbreviations."
1724 (set-buffer (get-buffer-create "*Abbrevs*"))
1726 (insert-abbrev-table-description 'f90-mode-abbrev-table t
)
1727 (goto-char (point-min))
1728 (set-buffer-modified-p nil
)
1729 (edit-abbrevs-mode))
1730 (get-buffer-create "*Abbrevs*"))
1732 (defun f90-upcase-keywords ()
1733 "Upcase all F90 keywords in the buffer."
1735 (f90-change-keywords 'upcase-word
))
1737 (defun f90-capitalize-keywords ()
1738 "Capitalize all F90 keywords in the buffer."
1740 (f90-change-keywords 'capitalize-word
))
1742 (defun f90-downcase-keywords ()
1743 "Downcase all F90 keywords in the buffer."
1745 (f90-change-keywords 'downcase-word
))
1747 (defun f90-upcase-region-keywords (beg end
)
1748 "Upcase all F90 keywords in the region."
1750 (f90-change-keywords 'upcase-word beg end
))
1752 (defun f90-capitalize-region-keywords (beg end
)
1753 "Capitalize all F90 keywords in the region."
1755 (f90-change-keywords 'capitalize-word beg end
))
1757 (defun f90-downcase-region-keywords (beg end
)
1758 "Downcase all F90 keywords in the region."
1760 (f90-change-keywords 'downcase-word beg end
))
1762 ;; Change the keywords according to argument.
1763 (defun f90-change-keywords (change-word &optional beg end
)
1764 "Change the case of F90 keywords in the region (if specified) or buffer.
1765 CHANGE-WORD should be one of 'upcase-word, 'downcase-word, 'capitalize-word."
1767 (setq beg
(or beg
(point-min))
1768 end
(or end
(point-max)))
1771 f90-keywords-re
"\\|" f90-procedures-re
"\\|"
1772 f90-hpf-keywords-re
"\\|" f90-operators-re
"\\)"))
1773 (ref-point (point-min))
1774 (modified (buffer-modified-p))
1775 state saveword back-point
)
1778 (while (re-search-forward keyword-re end t
)
1780 (setq state
(parse-partial-sexp ref-point
(point)))
1781 (or (nth 3 state
) (nth 4 state
)
1782 ;; GM f90-directive-comment-re?
1783 (save-excursion ; check for cpp directive
1785 (skip-chars-forward " \t0-9")
1787 (setq ref-point
(point)
1788 back-point
(save-excursion (backward-word 1) (point))
1789 saveword
(buffer-substring back-point ref-point
))
1790 (funcall change-word -
1)
1791 (or (string= saveword
(buffer-substring back-point ref-point
))
1792 (setq modified t
))))
1793 (or modified
(set-buffer-modified-p nil
))))))
1796 (defun f90-current-defun ()
1797 "Function to use for `add-log-current-defun-function' in F90 mode."
1799 (nth 1 (f90-beginning-of-subprogram))))
1803 ;;; arch-tag: fceac97c-c147-44bd-aec0-172d4b560ef8
1804 ;;; f90.el ends here