File: f90.el.html

Major mode for editing F90 programs in FREE FORMAT.

The minor language revision F95 is also supported (with font-locking). Some/many (?) aspects of F2003 are supported. Some aspects of F2008 are supported.

Knows about continuation lines, named structured statements, and other features in F90 including HPF (High Performance Fortran) structures. The basic feature provides accurate indentation of F90 programs. In addition, there are many more features like automatic matching of all end statements, an auto-fill function to break long lines, a join-lines function which joins continued lines, etc.

To facilitate typing, a fairly complete list of abbreviations is provided. All abbreviations begin with the backquote character "`" For example, `i expands to integer (if abbrev-mode is on).

There are two separate features for altering the appearance of code:
  1) Upcasing or capitalizing of all keywords.
  2) Colors/fonts using font-lock-mode.
Automatic upcase or downcase of keywords is controlled by the variable f90-auto-keyword-case.

The indentations of lines starting with ! is determined by the first of the following matches (values in the left column are the defaults):

start-string/regexp indent variable holding start-string/regexp
   !!! 0
   !hpf\\$ (re) 0 f90-directive-comment-re
   !!$ 0 f90-comment-region
   ! (re) as code f90-indented-comment-re
   default comment-column

Ex: Here is the result of 3 different settings of f90-indented-comment-re
    f90-indented-comment-re !-indentation !!-indentation
         ! as code as code
         !! comment-column as code
         ![^!] as code comment-column
Trailing comments are indented to comment-column with indent-for-comment. The function f90-comment-region toggles insertion of the variable f90-comment-region in every line of the region.

One common convention for free vs. fixed format is that free format files have the ending ".f90" or ".f95" while fixed format files have the ending ".f". Emacs automatically loads Fortran files in the appropriate mode based on extension. You can modify this by adjusting the variable auto-mode-alist. For example:
(add-to-list 'auto-mode-alist '("\\\\.f\\\\'" . f90-mode))

Once you have entered f90-mode, you can get more info by using the command describe-mode (C-h m). For help use C-h f <Name of function you want described>, or C-h v <Name of variable you want described>.

To customize f90-mode for your taste, use, for example:
(you don't have to specify values for all the parameters below)

(add-hook 'f90-mode-hook
      ;; These are the default values.
      (lambda () (setq f90-do-indent 3
                       f90-if-indent 3
                       f90-type-indent 3
                       f90-program-indent 2
                       f90-continuation-indent 5
                       f90-comment-region "!!$"
                       f90-directive-comment-re "!hpf\\\\$"
                       f90-indented-comment-re "!"
                       f90-break-delimiters "[-+\\\\*/><=,% \\t]"
                       f90-break-before-delimiters t
                       f90-beginning-ampersand t
                       f90-smart-end 'blink
                       f90-auto-keyword-case nil
                       f90-leave-line-no nil
                       indent-tabs-mode nil
                       f90-font-lock-keywords f90-font-lock-keywords-2
                 )
      ;; These are not default.
      (abbrev-mode 1) ; turn on abbreviation mode
      (f90-add-imenu-menu) ; extra menu with functions etc.
      (if f90-auto-keyword-case ; change case of all keywords on startup
          (f90-change-keywords f90-auto-keyword-case))))

in your init file. You can also customize the lists f90-font-lock-keywords, etc.

The auto-fill and abbreviation minor modes are accessible from the F90 menu, or by using M-x auto-fill-mode and M-x abbrev-mode, respectively.

Remarks
1) Line numbers are by default left-justified. If f90-leave-line-no is
   non-nil, the line numbers are never touched.
2) Multi-; statements like "do i=1,20 ; j=j+i ; end do" are not handled
   correctly, but I imagine them to be rare.
3) For FIXED FORMAT code, use fortran mode.
4) Preprocessor directives, i.e., lines starting with # are left-justified
   and are untouched by all case-changing commands. There is, at present, no
   mechanism for treating multi-line directives (continued by \ ).
5) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
   You are urged to use f90-do loops (with labels if you wish).

List of user commands
  f90-previous-statement f90-next-statement
  f90-beginning-of-subprogram f90-end-of-subprogram f90-mark-subprogram
  f90-comment-region
  f90-indent-line f90-indent-new-line
  f90-indent-region (can be called by calling indent-region)
  f90-indent-subprogram
  f90-break-line f90-join-lines
  f90-fill-region f90-fill-paragraph
  f90-insert-end
  f90-upcase-keywords f90-upcase-region-keywords
  f90-downcase-keywords f90-downcase-region-keywords
  f90-capitalize-keywords f90-capitalize-region-keywords
  f90-add-imenu-menu
  f90-font-lock-1, f90-font-lock-2, f90-font-lock-3, f90-font-lock-4

Original author's thanks Thanks to all the people who have tested the mode. Special thanks to Jens Bloch Helmers for encouraging me to write this code, for creative suggestions as well as for the lists of hpf-commands. Also thanks to the authors of the fortran and pascal modes, on which some of this code is built.

Defined variables (47)

f90-associate-indentExtra indentation applied to ASSOCIATE blocks.
f90-auto-keyword-caseAutomatic case conversion of keywords.
f90-beginning-ampersandNon-nil gives automatic insertion of ‘&’ at start of continuation line.
f90-blocks-reRegexp potentially indicating a "block" of F90 code.
f90-break-before-delimitersNon-nil causes ‘f90-do-auto-fill’ to break lines before delimiters.
f90-break-delimitersRegexp matching delimiter characters at which lines may be broken.
f90-cache-positionTemporary position used to speed up region operations.
f90-comment-regionString inserted by M-x f90-comment-region at start of each line in region.
f90-constants-reRegexp for Fortran intrinsic constants.
f90-continuation-indentExtra indentation applied to continuation lines.
f90-critical-indentExtra indentation applied to BLOCK, CRITICAL blocks.
f90-directive-comment-reRegexp of comment-like directive like "!HPF\\$", not to be indented.
f90-do-indentExtra indentation applied to DO blocks.
f90-else-like-reRegexp matching an ELSE IF, ELSEWHERE, CASE, CLASS/TYPE IS statement.
f90-end-associate-reRegexp matching the end of an ASSOCIATE block.
f90-end-block-optional-nameBlock types where including the name in the end statement is optional.
f90-end-block-reRegexp matching the end of an F90 "block", from the line start.
f90-end-if-reRegexp matching the end of an IF, SELECT, WHERE, FORALL block.
f90-end-type-reRegexp matching the end of a TYPE, ENUM, INTERFACE, BLOCK DATA section.
f90-font-lock-keywordsDefault expressions to highlight in F90 mode.
f90-font-lock-keywords-1This does fairly subdued highlighting of comments and function calls.
f90-font-lock-keywords-2Highlights declarations, do-loops and other constructs.
f90-font-lock-keywords-3Highlights all F90 keywords and intrinsic procedures.
f90-font-lock-keywords-4Highlights all F90 and HPF keywords and constants.
f90-hpf-keywords-reRegexp for all HPF keywords, procedures and directives.
f90-if-indentExtra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks.
f90-imenu-generic-expressionValue for ‘imenu-generic-expression’ in F90 mode.
f90-indented-comment-reRegexp matching comments to indent as code.
f90-keywords-level-3-reKeyword-regexp for font-lock level >= 3.
f90-keywords-reRegexp used by the function ‘f90-change-keywords’.
f90-leave-line-noIf non-nil, line numbers are not left justified.
f90-menuMenu for F90 mode.
f90-mode-abbrev-tableAbbrev table for F90 mode.
f90-mode-hookHook run when entering F90 mode.
f90-mode-mapKeymap used in F90 mode.
f90-mode-syntax-tableSyntax table used in F90 mode.
f90-no-break-reRegexp specifying two-character tokens not to split when breaking lines.
f90-operators-reRegexp matching intrinsic operators.
f90-procedures-reRegexp whose first part matches F90 intrinsic procedures.
f90-program-block-reRegexp used to locate the start/end of a "subprogram".
f90-program-indentExtra indentation applied to PROGRAM, MODULE, SUBROUTINE, FUNCTION blocks.
f90-smart-endQualification of END statements according to the matching block start.
f90-smart-end-namesWhether completion of END statements should insert optional block names.
f90-start-block-reRegexp matching the start of an F90 "block", from the line start.
f90-type-def-reRegexp matching the definition of a derived type.
f90-type-indentExtra indentation applied to TYPE, ENUM, INTERFACE and BLOCK DATA blocks.
f90-typeis-reRegexp matching a CLASS/TYPE IS statement.

Defined functions (71)

f90-abbrev-help()
f90-abbrev-start()
f90-add-imenu-menu()
f90-backslash-not-special(&optional ALL)
f90-beginning-of-block(&optional NUM)
f90-beginning-of-subprogram()
f90-block-match(BEG-BLOCK BEG-NAME END-BLOCK END-NAME)
f90-break-line(&optional NO-UPDATE)
f90-calculate-indent()
f90-capitalize-keywords()
f90-capitalize-region-keywords(BEG END)
f90-change-keywords(CHANGE-WORD &optional BEG END)
f90-comment-indent()
f90-comment-region(BEG-REGION END-REGION)
f90-current-defun()
f90-current-indentation()
f90-do-auto-fill()
f90-downcase-keywords()
f90-downcase-region-keywords(BEG END)
f90-electric-insert(&optional ARG)
f90-end-of-block(&optional NUM)
f90-end-of-subprogram()
f90-equal-symbols(A B)
f90-fill-paragraph(&optional JUSTIFY)
f90-fill-region(BEG-REGION END-REGION)
f90-find-breakpoint()
f90-font-lock-1()
f90-font-lock-2()
f90-font-lock-3()
f90-font-lock-4()
f90-font-lock-n(N)
f90-get-correct-indent()
f90-get-present-comment-type()
f90-imenu-type-matcher()
f90-in-comment()
f90-in-string()
f90-indent-line(&optional NO-UPDATE)
f90-indent-line-no()
f90-indent-new-line()
f90-indent-region(BEG-REGION END-REGION)
f90-indent-subprogram()
f90-indent-to(COL &optional NO-LINE-NUMBER)
f90-insert-end()
f90-join-lines(&optional ARG)
f90-line-continued()
f90-looking-at-associate()
f90-looking-at-critical()
f90-looking-at-do()
f90-looking-at-end-critical()
f90-looking-at-if-then()
f90-looking-at-program-block-end()
f90-looking-at-program-block-start()
f90-looking-at-select-case()
f90-looking-at-type-like()
f90-looking-at-where-or-forall()
f90-mark-subprogram()
f90-match-end()
f90-menu(ARG1)
f90-mode()
f90-next-block(&optional NUM)
f90-next-statement()
f90-no-block-limit()
f90-prepare-abbrev-list-buffer()
f90-present-statement-cont()
f90-previous-block(&optional NUM)
f90-previous-statement()
f90-typedec-matcher(LIMIT)
f90-typedef-matcher(LIMIT)
f90-upcase-keywords()
f90-upcase-region-keywords(BEG END)
f90-update-line()

Defined faces (0)