Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Perl |
| 3 | " Author: Rafael Garcia-Suarez <rgarciasuarez@free.fr> |
| 4 | " URL: http://rgarciasuarez.free.fr/vim/indent/perl.vim |
Bram Moolenaar | c6485bc | 2010-07-28 17:02:55 +0200 | [diff] [blame] | 5 | " Last Change: 2010 Jul 28 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6 | |
| 7 | " Suggestions and improvements by : |
| 8 | " Aaron J. Sherman (use syntax for hints) |
| 9 | " Artem Chuprina (play nice with folding) |
Bram Moolenaar | c6485bc | 2010-07-28 17:02:55 +0200 | [diff] [blame] | 10 | " Benjamin R. Haskell (fold syntax group fix) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11 | |
| 12 | " TODO things that are not or not properly indented (yet) : |
| 13 | " - Continued statements |
| 14 | " print "foo", |
| 15 | " "bar"; |
| 16 | " print "foo" |
| 17 | " if bar(); |
| 18 | " - Multiline regular expressions (m//x) |
| 19 | " (The following probably needs modifying the perl syntax file) |
| 20 | " - qw() lists |
| 21 | " - Heredocs with terminators that don't match \I\i* |
| 22 | |
| 23 | " Only load this indent file when no other was loaded. |
| 24 | if exists("b:did_indent") |
| 25 | finish |
| 26 | endif |
| 27 | let b:did_indent = 1 |
| 28 | |
| 29 | " Is syntax highlighting active ? |
Bram Moolenaar | d1231f9 | 2005-09-07 21:12:33 +0000 | [diff] [blame] | 30 | let b:indent_use_syntax = has("syntax") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 31 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 32 | setlocal indentexpr=GetPerlIndent() |
| 33 | setlocal indentkeys+=0=,0),0=or,0=and |
| 34 | if !b:indent_use_syntax |
| 35 | setlocal indentkeys+=0=EO |
| 36 | endif |
| 37 | |
| 38 | " Only define the function once. |
| 39 | if exists("*GetPerlIndent") |
| 40 | finish |
| 41 | endif |
| 42 | |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 43 | let s:cpo_save = &cpo |
| 44 | set cpo-=C |
| 45 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | function GetPerlIndent() |
| 47 | |
| 48 | " Get the line to be indented |
| 49 | let cline = getline(v:lnum) |
| 50 | |
| 51 | " Indent POD markers to column 0 |
| 52 | if cline =~ '^\s*=\L\@!' |
| 53 | return 0 |
| 54 | endif |
| 55 | |
| 56 | " Don't reindent coments on first column |
| 57 | if cline =~ '^#.' |
| 58 | return 0 |
| 59 | endif |
| 60 | |
| 61 | " Get current syntax item at the line's first char |
| 62 | let csynid = '' |
| 63 | if b:indent_use_syntax |
| 64 | let csynid = synIDattr(synID(v:lnum,1,0),"name") |
| 65 | endif |
| 66 | |
| 67 | " Don't reindent POD and heredocs |
| 68 | if csynid == "perlPOD" || csynid == "perlHereDoc" || csynid =~ "^pod" |
| 69 | return indent(v:lnum) |
| 70 | endif |
| 71 | |
| 72 | " Indent end-of-heredocs markers to column 0 |
| 73 | if b:indent_use_syntax |
| 74 | " Assumes that an end-of-heredoc marker matches \I\i* to avoid |
| 75 | " confusion with other types of strings |
| 76 | if csynid == "perlStringStartEnd" && cline =~ '^\I\i*$' |
| 77 | return 0 |
| 78 | endif |
| 79 | else |
| 80 | " Without syntax hints, assume that end-of-heredocs markers begin with EO |
| 81 | if cline =~ '^\s*EO' |
| 82 | return 0 |
| 83 | endif |
| 84 | endif |
| 85 | |
| 86 | " Now get the indent of the previous perl line. |
| 87 | |
| 88 | " Find a non-blank line above the current line. |
| 89 | let lnum = prevnonblank(v:lnum - 1) |
| 90 | " Hit the start of the file, use zero indent. |
| 91 | if lnum == 0 |
| 92 | return 0 |
| 93 | endif |
| 94 | let line = getline(lnum) |
| 95 | let ind = indent(lnum) |
| 96 | " Skip heredocs, POD, and comments on 1st column |
| 97 | if b:indent_use_syntax |
| 98 | let skippin = 2 |
| 99 | while skippin |
| 100 | let synid = synIDattr(synID(lnum,1,0),"name") |
| 101 | if (synid == "perlStringStartEnd" && line =~ '^\I\i*$') |
| 102 | \ || (skippin != 2 && synid == "perlPOD") |
| 103 | \ || (skippin != 2 && synid == "perlHereDoc") |
| 104 | \ || synid == "perlComment" |
| 105 | \ || synid =~ "^pod" |
| 106 | let lnum = prevnonblank(lnum - 1) |
| 107 | if lnum == 0 |
| 108 | return 0 |
| 109 | endif |
| 110 | let line = getline(lnum) |
| 111 | let ind = indent(lnum) |
| 112 | let skippin = 1 |
| 113 | else |
| 114 | let skippin = 0 |
| 115 | endif |
| 116 | endwhile |
| 117 | else |
| 118 | if line =~ "^EO" |
| 119 | let lnum = search("<<[\"']\\=EO", "bW") |
| 120 | let line = getline(lnum) |
| 121 | let ind = indent(lnum) |
| 122 | endif |
| 123 | endif |
| 124 | |
| 125 | " Indent blocks enclosed by {} or () |
| 126 | if b:indent_use_syntax |
| 127 | " Find a real opening brace |
| 128 | let bracepos = match(line, '[(){}]', matchend(line, '^\s*[)}]')) |
| 129 | while bracepos != -1 |
| 130 | let synid = synIDattr(synID(lnum, bracepos + 1, 0), "name") |
| 131 | " If the brace is highlighted in one of those groups, indent it. |
| 132 | " 'perlHereDoc' is here only to handle the case '&foo(<<EOF)'. |
| 133 | if synid == "" |
| 134 | \ || synid == "perlMatchStartEnd" |
| 135 | \ || synid == "perlHereDoc" |
| 136 | \ || synid =~ "^perlFiledescStatement" |
Bram Moolenaar | c6485bc | 2010-07-28 17:02:55 +0200 | [diff] [blame] | 137 | \ || synid =~ '^perl\(Sub\|Block\)Fold' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | let brace = strpart(line, bracepos, 1) |
| 139 | if brace == '(' || brace == '{' |
| 140 | let ind = ind + &sw |
| 141 | else |
| 142 | let ind = ind - &sw |
| 143 | endif |
| 144 | endif |
| 145 | let bracepos = match(line, '[(){}]', bracepos + 1) |
| 146 | endwhile |
| 147 | let bracepos = matchend(cline, '^\s*[)}]') |
| 148 | if bracepos != -1 |
| 149 | let synid = synIDattr(synID(v:lnum, bracepos, 0), "name") |
| 150 | if synid == "" |
| 151 | \ || synid == "perlMatchStartEnd" |
Bram Moolenaar | c6485bc | 2010-07-28 17:02:55 +0200 | [diff] [blame] | 152 | \ || synid =~ '^perl\(Sub\|Block\)Fold' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 153 | let ind = ind - &sw |
| 154 | endif |
| 155 | endif |
| 156 | else |
| 157 | if line =~ '[{(]\s*\(#[^)}]*\)\=$' |
| 158 | let ind = ind + &sw |
| 159 | endif |
| 160 | if cline =~ '^\s*[)}]' |
| 161 | let ind = ind - &sw |
| 162 | endif |
| 163 | endif |
| 164 | |
| 165 | " Indent lines that begin with 'or' or 'and' |
| 166 | if cline =~ '^\s*\(or\|and\)\>' |
| 167 | if line !~ '^\s*\(or\|and\)\>' |
| 168 | let ind = ind + &sw |
| 169 | endif |
| 170 | elseif line =~ '^\s*\(or\|and\)\>' |
| 171 | let ind = ind - &sw |
| 172 | endif |
| 173 | |
| 174 | return ind |
| 175 | |
| 176 | endfunction |
| 177 | |
| 178 | let &cpo = s:cpo_save |
| 179 | unlet s:cpo_save |
| 180 | |
| 181 | " vim:ts=8:sts=2:sw=2 |