Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 2 | " Language: Fortran 2008 (and older: Fortran 2003, 95, 90, and 77) |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 3 | " Version: 0.46 |
| 4 | " Last Change: 2016 Sep. 27 |
Bram Moolenaar | 8a94d87 | 2015-01-25 13:02:57 +0100 | [diff] [blame] | 5 | " Maintainer: Ajit J. Thakkar <ajit@unb.ca>; <http://www2.unb.ca/~ajit/> |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 6 | " Usage: For instructions, do :help fortran-indent from Vim |
Bram Moolenaar | 8a94d87 | 2015-01-25 13:02:57 +0100 | [diff] [blame] | 7 | " Credits: |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 8 | " Useful suggestions were made, in chronological order, by: |
| 9 | " Albert Oliver Serra, Takuya Fujiwara and Philipp Edelmann. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10 | |
| 11 | " Only load this indent file when no other was loaded. |
| 12 | if exists("b:did_indent") |
| 13 | finish |
| 14 | endif |
| 15 | let b:did_indent = 1 |
| 16 | |
Bram Moolenaar | b6b046b | 2011-12-30 13:11:27 +0100 | [diff] [blame] | 17 | let s:cposet=&cpoptions |
| 18 | set cpoptions&vim |
| 19 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 21 | setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif |
| 22 | setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum |
| 23 | setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum |
| 24 | if exists("b:fortran_indent_more") || exists("g:fortran_indent_more") |
| 25 | setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program |
| 26 | setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule |
| 27 | setlocal indentkeys+==~endprogram |
| 28 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | |
| 30 | " Determine whether this is a fixed or free format source file |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 31 | " if this hasn't been done yet using the priority: |
| 32 | " buffer-local value |
| 33 | " > global value |
| 34 | " > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | if !exists("b:fortran_fixed_source") |
| 36 | if exists("fortran_free_source") |
| 37 | " User guarantees free source form |
| 38 | let b:fortran_fixed_source = 0 |
| 39 | elseif exists("fortran_fixed_source") |
| 40 | " User guarantees fixed source form |
| 41 | let b:fortran_fixed_source = 1 |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 42 | elseif expand("%:e") ==? "f\<90\|95\|03\|08\>" |
| 43 | " Free-form file extension defaults as in Intel ifort, gcc(gfortran), NAG, Pathscale, and Cray compilers |
| 44 | let b:fortran_fixed_source = 0 |
| 45 | elseif expand("%:e") ==? "f\|f77\|for" |
| 46 | " Fixed-form file extension defaults |
| 47 | let b:fortran_fixed_source = 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | else |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 49 | " Modern fortran still allows both fixed and free source form |
| 50 | " Assume fixed source form unless signs of free source form |
Bram Moolenaar | 8a94d87 | 2015-01-25 13:02:57 +0100 | [diff] [blame] | 51 | " are detected in the first five columns of the first s:lmax lines. |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 52 | " Detection becomes more accurate and time-consuming if more lines |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 53 | " are checked. Increase the limit below if you keep lots of comments at |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 54 | " the very top of each file and you have a fast computer. |
Bram Moolenaar | c88ebf7 | 2010-07-22 22:30:23 +0200 | [diff] [blame] | 55 | let s:lmax = 500 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | if ( s:lmax > line("$") ) |
| 57 | let s:lmax = line("$") |
| 58 | endif |
| 59 | let b:fortran_fixed_source = 1 |
| 60 | let s:ln=1 |
| 61 | while s:ln <= s:lmax |
| 62 | let s:test = strpart(getline(s:ln),0,5) |
Bram Moolenaar | c88ebf7 | 2010-07-22 22:30:23 +0200 | [diff] [blame] | 63 | if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | let b:fortran_fixed_source = 0 |
| 65 | break |
| 66 | endif |
| 67 | let s:ln = s:ln + 1 |
| 68 | endwhile |
| 69 | endif |
| 70 | endif |
| 71 | |
| 72 | " Define the appropriate indent function but only once |
| 73 | if (b:fortran_fixed_source == 1) |
| 74 | setlocal indentexpr=FortranGetFixedIndent() |
| 75 | if exists("*FortranGetFixedIndent") |
| 76 | finish |
| 77 | endif |
| 78 | else |
| 79 | setlocal indentexpr=FortranGetFreeIndent() |
| 80 | if exists("*FortranGetFreeIndent") |
| 81 | finish |
| 82 | endif |
| 83 | endif |
| 84 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 85 | function FortranGetIndent(lnum) |
| 86 | let ind = indent(a:lnum) |
| 87 | let prevline=getline(a:lnum) |
| 88 | " Strip tail comment |
| 89 | let prevstat=substitute(prevline, '!.*$', '', '') |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 90 | let prev2line=getline(a:lnum-1) |
| 91 | let prev2stat=substitute(prev2line, '!.*$', '', '') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 92 | |
| 93 | "Indent do loops only if they are all guaranteed to be of do/end do type |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 94 | if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 96 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | endif |
| 98 | if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 99 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | endif |
| 101 | endif |
| 102 | |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 103 | "Add a shiftwidth to statements following if, else, else if, case, |
| 104 | "where, else where, forall, type, interface and associate statements |
| 105 | if prevstat =~? '^\s*\(case\|else\|else\s*if\|else\s*where\)\>' |
| 106 | \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>' |
| 107 | \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>' |
| 108 | \ ||prevstat=~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 109 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 110 | " Remove unwanted indent after logical and arithmetic ifs |
| 111 | if prevstat =~? '\<if\>' && prevstat !~? '\<then\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 112 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | endif |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 114 | " Remove unwanted indent after type( statements |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 115 | if prevstat =~? '^\s*type\s*(' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 116 | let ind = ind - shiftwidth() |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 117 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 118 | endif |
| 119 | |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 120 | "Indent program units unless instructed otherwise |
| 121 | if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less") |
| 122 | let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}' |
| 123 | let type='\(\(integer\|real\|double\s\+precision\|complex\|logical' |
| 124 | \.'\|character\|type\|class\)\s*\S*\s\+\)\=' |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 125 | if prevstat =~? '^\s*\(contains\|submodule\|program\)\>' |
| 126 | \ ||prevstat =~? '^\s*'.'module\>\(\s*\procedure\)\@!' |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 127 | \ ||prevstat =~? '^\s*'.prefix.'subroutine\>' |
| 128 | \ ||prevstat =~? '^\s*'.prefix.type.'function\>' |
| 129 | \ ||prevstat =~? '^\s*'.type.prefix.'function\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 130 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | endif |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 132 | if getline(v:lnum) =~? '^\s*contains\>' |
| 133 | \ ||getline(v:lnum)=~? '^\s*end\s*' |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 134 | \ .'\(function\|subroutine\|module\|submodule\|program\)\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 135 | let ind = ind - shiftwidth() |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 136 | endif |
| 137 | endif |
| 138 | |
| 139 | "Subtract a shiftwidth from else, else if, elsewhere, case, end if, |
| 140 | " end where, end select, end forall, end interface, end associate, |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 141 | " end enum, end type, end block and end type statements |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 142 | if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*' |
| 143 | \. '\(else\|else\s*if\|else\s*where\|case\|' |
| 144 | \. 'end\s*\(if\|where\|select\|interface\|' |
Bram Moolenaar | 8a94d87 | 2015-01-25 13:02:57 +0100 | [diff] [blame] | 145 | \. 'type\|forall\|associate\|enum\|block\)\)\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 146 | let ind = ind - shiftwidth() |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 147 | " Fix indent for case statement immediately after select |
| 148 | if prevstat =~? '\<select\s\+\(case\|type\)\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 149 | let ind = ind + shiftwidth() |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 150 | endif |
| 151 | endif |
| 152 | |
| 153 | "First continuation line |
| 154 | if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 155 | let ind = ind + shiftwidth() |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 156 | endif |
| 157 | "Line after last continuation line |
Bram Moolenaar | 8a94d87 | 2015-01-25 13:02:57 +0100 | [diff] [blame] | 158 | if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 159 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | endif |
| 161 | |
| 162 | return ind |
| 163 | endfunction |
| 164 | |
| 165 | function FortranGetFreeIndent() |
| 166 | "Find the previous non-blank line |
| 167 | let lnum = prevnonblank(v:lnum - 1) |
| 168 | |
| 169 | "Use zero indent at the top of the file |
| 170 | if lnum == 0 |
| 171 | return 0 |
| 172 | endif |
| 173 | |
| 174 | let ind=FortranGetIndent(lnum) |
| 175 | return ind |
| 176 | endfunction |
| 177 | |
| 178 | function FortranGetFixedIndent() |
| 179 | let currline=getline(v:lnum) |
| 180 | "Don't indent comments, continuation lines and labelled lines |
| 181 | if strpart(currline,0,6) =~ '[^ \t]' |
| 182 | let ind = indent(v:lnum) |
| 183 | return ind |
| 184 | endif |
| 185 | |
| 186 | "Find the previous line which is not blank, not a comment, |
| 187 | "not a continuation line, and does not have a label |
| 188 | let lnum = v:lnum - 1 |
| 189 | while lnum > 0 |
| 190 | let prevline=getline(lnum) |
| 191 | if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$") |
| 192 | \ || (strpart(prevline,5,1) !~ "[ 0]") |
| 193 | " Skip comments, blank lines and continuation lines |
| 194 | let lnum = lnum - 1 |
| 195 | else |
| 196 | let test=strpart(prevline,0,5) |
| 197 | if test =~ "[0-9]" |
| 198 | " Skip lines with statement numbers |
| 199 | let lnum = lnum - 1 |
| 200 | else |
| 201 | break |
| 202 | endif |
| 203 | endif |
| 204 | endwhile |
| 205 | |
| 206 | "First line must begin at column 7 |
| 207 | if lnum == 0 |
| 208 | return 6 |
| 209 | endif |
| 210 | |
| 211 | let ind=FortranGetIndent(lnum) |
| 212 | return ind |
| 213 | endfunction |
| 214 | |
| 215 | let &cpoptions=s:cposet |
| 216 | unlet s:cposet |
| 217 | |
| 218 | " vim:sw=2 tw=130 |