Fortran Mode Help
Name:Fortran Modes
Version:2.3
Last update:2019-02-18 17:51:44





Introduction

FORTRAN was first developed in the 1950s and still used today. Its name comes from FORmula TRANslation, revealing its early use in processing mathematics. Dialects include FORTRAN 77 and FORTRAN 90/95 or greater (2003, 2008 standards).
Alpha's Fortran modes (Fortran77 and Fortran95) provide keyword coloring, subroutine marking and indexing. They support Shift-Command-Double-Click for jumping to subroutines and the opening of include files.

Fixed-Format vs Free-Format

Fortran 77 is not a free-format language, but has a very strict set of rules for how the source code should be formatted. The most important rules are the column position rules:
Col. 1 :Blank, or one of c C ! * for comments
Col. 2-5 :Statement label (optional)
Col. 6 :Continuation of previous line (optional)
Col. 7-72 :Statements
Col. 73-80 :Sequence number (optional, rarely used today)
Most lines in a Fortran 77 program start with 6 blanks and end before column 72, i.e. only the statement field is used.
Fortran 90 and greater (95, 2003, 2008), on the other hand, allows free format.
AlphaTcl contains two modes for Fortran files, Fortran77 and Fortran95:
Yes, these are both dialects rather than separate languages, but for technical reasons Alpha needs to define a different mode for each one. Both of them share some of the features described in this file, but have distinct indentation and block-shifting routines.

Fortran77 Menu

The Fortran77 Menu is automatically inserted into the menu bar whenever you open a file in Fortran77 mode. It includes items that will open up the www G77 Home Page and the Fortran Faq sites. You can adjust your Fortran preference to change the home page location to something more useful by selecting Alpha ↣ Fortran 77 Mode Setup ↣ Mode Preferences…. Click here to open the f77 Mode Preferences.
The Shift Left/Right menu commands will preserve columns 1-6, and adjust the indentation of the statement in the current line/selection. These use the same keyboard shortcuts as the Edit ↣ Shift menu items, i.e. over-riding the behavior seen in other Alpha modes. If you need to turn off this feature to restore the Edit ↣ Shift keyboard shortcuts then toggle the F77 ↣ Fortran Mode Shifting menu item.
In Alpha, these are dynamic menu items -- press Option () to display the items and shortcuts which shift the text one space to the left/right.
The F77 ↣ Special Balance menu item will behave like Alpha's normal Edit ↣ Balance command to select the text contained in () or in {}. If there are no more bracket/brace characters to balance, then blocks of code found between IF ... END IF or DO ... CONTINUE will be selected, as well as entire MODULE, INTERFACE, PROGRAM, SUBROUTINE, and FUNCTION blocks.

Fortran95 Menu

The Fortran95 Menu is automatically inserted into the menu bar whenever you open a file in Fortran95 mode. It includes items that will open up the www GFortran Home Page, G95 Home Page and the Fortran Faq sites. You can adjust your Fortran preference to change the home page location to something more useful by selecting Alpha ↣ Fortran 95 Mode Setup ↣ Mode Preferences…. Click here to open the f95 Mode Preferences.
Unlike Fortran77 mode, there are no special Shift Left/Right menu commands, and you can make use of the Edit ↣ Shift menu items.
The F95 ↣ Special Balance menu item will behave just like Alpha's normal Edit ↣ Balance command to select the text contained in () or in {}. If there are no more bracket/brace characters to balance, then blocks of code found between IF ... END IF or DO ... CONTINUE will be selected, as well as entire MODULE, INTERFACE, PROGRAM, SUBROUTINE, and FUNCTION blocks.

Fortran Comments

Fortran77 Mode Comments

You can use the F77 ↣ Fortran Comments submenu to change the character used for all Comment menu items.
The Indent Comments preference determines if comments should be left in column 3 or advanced to column 7, or simply left alone. You can select F77 ↣ Reformat Block to properly indent commented code. Click here to open the
f77 Mode Preferences.
Unfortunately, the presence of any specially colored keywords within these comments will end the colorization on these lines. Remember, this is a known bug and this preference is an acknowledged hack, so please don't complain about its inadequate implementation.

Fortran95 Mode Comments

The comment character "!" can appear anywhere in a line to indicate a comment and all text following a "!" will be colored. This does not include a "!" that appears within a statement, as in
    write(unit=*, fmt=*) "Can't divide by zero!!", sqrt(rnorm/ynorm)
so the sqrt… statement will be colored correctly as a keyword.

Continuing Statement Lines

Fortran77 mode

Use the F77 ↣ Continue Line command to create a new line with your Continuation Character in column 6, properly indenting the line with respect to the most recent one. You can toggle the current line as continued or not with the F77 ↣ Toggle Continuation command.
You can select the Fortran 77 Mode Setup ↣ Preferences dialog to change the character inserted to indicate continuations. There is a separate preference named Continuation Characters which determines the characters that appear in the pop-up menu of options. This simply ensures that you always select a valid option. Click here to open the
f77 Mode Preferences.
Previous versions of Fortran mode used Control-Option-J (⌃⌥J) to continue lines. This shortcut has been retained for those who are used to it.

Fortran95 mode

Use the F95 ↣ Continue Line command to insert a "&" at the current cursor position, and then create a new properly indented line, as in
    private :: solve, mv_prod, prod0, prod1, Preco, |
to this
    private :: solve, mv_prod, prod0, prod1, Preco, &
    |
Continued lines are always indented twice the normal indentation amount. Some Fortran users like to also insert an "&" in column 6 in .f90 and .f95 files even though this is unnecessary. Fortran95 mode does not support this, and this syntax will result in errant formatting of subsequent lines.

Fortran Electrics

Both Fortran77 and Fortran95 modes support Electric Completions. You can open the f77 Mode Tutorial and f95 Mode Tutorial windows for a little demonstration.
All the reserved keywords and intrinsic function names can be completed. Some of them (like if, do, while, program, interface, function, subroutine, module, etc.) expand to a complete template.
There are also predefined contractions (i-e abbreviations of the form x'y which are expanded when the completion key is pressed). Available contractions are :
ContractionSignification
a'tassign to
b'dblockdata
d'cdo continue
d'edo end do
d'pdouble precision
d'wdo while
e'ielse if
i'eif else
i'nimplicit none
i'tif then
s'cselect
t'etype
w'dwhile
If the mode preference named Indent On Return is turned on, pressing Return will automatically indent the current line if necessary and then indent the new line that is created.
Click here to open the f77 Mode Preferences or the f95 Mode Preferences.
Even when this preference is turned on, pressing Control-Return creates a new line without any automatic indentation or insertion of comment characters.

Window Navigation

The F77|F95 ↣ Next/Prev Block commands will advance the cursor in the active window. A block is simply delineated by empty lines. If there is a current selection when you call these items, the selection is extended to the end/beginning of the next/previous block of code.
These functions are also available by pressing Control-Shift-Arrow keys. For example, Control-Shift-Down will advance to (or select) the next block of code. Control-Shift-Right will do this but also move the new cursor location to the top of the window.
F77|F95 ↣ Reformat Block will properly indent the statements in the block of code surrounding the cursor, and then position the cursor at the start of the next block of code.
File marking (see the Marks popup in the window's control bar) will collect all new PROGRAM statements in the active window. There is an additional Mark Tag preference that can be set -- all occurrences of this string in a commented line (i.e. starting in column 2 for Fortran77 mode or following a "!" in Fortran95 mode) will also be marked. For example, if the value of this preference is "!!!" then
    !!!! This is a special comment
    ! !!! This is another one !!!
    !    !!! And this is a third.
will be specially marked. In Fortran77 mode, this tag can appear after any valid column 1 comment character, as in
    !!!! This is a special comment
    c !!! This is another one !!!
    C    !!! And this is a third.

Known problems

Please report any problem or bug you encounter in Alpha's Bug Tracker.

License and Disclaimer

Original Author: Tom Pollard.
Copyright (c) 1994-2019, Tom Pollard and contributors.
Contributors: Craig Barton Upright, Bernard Desgraupes.
All rights reserved.
The Fortran Modes package is free software and distributed under the terms of the new BSD license:
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TOM POLLARD OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.