blob: fc4b6a30d3ef2f8e0e9214f676f0013548bbf870 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar543b7ef2013-06-01 14:50:56 +02002" Language: Perl 5
3" Maintainer: vim-perl <vim-perl@googlegroups.com>
4" Homepage: http://github.com/vim-perl/vim-perl
5" Bugs/requests: http://github.com/vim-perl/vim-perl/issues
Bram Moolenaar9d98fe92013-08-03 18:35:36 +02006" Last Change: 2013-07-24
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
8" Suggestions and improvements by :
9" Aaron J. Sherman (use syntax for hints)
10" Artem Chuprina (play nice with folding)
11
12" TODO things that are not or not properly indented (yet) :
13" - Continued statements
14" print "foo",
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020015" "bar";
Bram Moolenaar071d4272004-06-13 20:20:40 +000016" print "foo"
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020017" if bar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018" - 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.
24if exists("b:did_indent")
Bram Moolenaar9de99972010-08-09 22:33:06 +020025 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000026endif
27let b:did_indent = 1
28
29" Is syntax highlighting active ?
Bram Moolenaard1231f92005-09-07 21:12:33 +000030let b:indent_use_syntax = has("syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032setlocal indentexpr=GetPerlIndent()
Bram Moolenaar9de99972010-08-09 22:33:06 +020033setlocal indentkeys+=0=,0),0],0=or,0=and
Bram Moolenaar071d4272004-06-13 20:20:40 +000034if !b:indent_use_syntax
Bram Moolenaar9de99972010-08-09 22:33:06 +020035 setlocal indentkeys+=0=EO
Bram Moolenaar071d4272004-06-13 20:20:40 +000036endif
37
Bram Moolenaarb6356332005-07-18 21:40:44 +000038let s:cpo_save = &cpo
39set cpo-=C
40
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020041function! GetPerlIndent()
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar9de99972010-08-09 22:33:06 +020043 " Get the line to be indented
44 let cline = getline(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000045
Bram Moolenaar9de99972010-08-09 22:33:06 +020046 " Indent POD markers to column 0
47 if cline =~ '^\s*=\L\@!'
48 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020051 " Don't reindent comments on first column
Bram Moolenaar9de99972010-08-09 22:33:06 +020052 if cline =~ '^#.'
53 return 0
54 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar9de99972010-08-09 22:33:06 +020056 " Get current syntax item at the line's first char
57 let csynid = ''
58 if b:indent_use_syntax
59 let csynid = synIDattr(synID(v:lnum,1,0),"name")
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaar9de99972010-08-09 22:33:06 +020062 " Don't reindent POD and heredocs
63 if csynid == "perlPOD" || csynid == "perlHereDoc" || csynid =~ "^pod"
64 return indent(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaar9de99972010-08-09 22:33:06 +020067 " Indent end-of-heredocs markers to column 0
68 if b:indent_use_syntax
69 " Assumes that an end-of-heredoc marker matches \I\i* to avoid
70 " confusion with other types of strings
71 if csynid == "perlStringStartEnd" && cline =~ '^\I\i*$'
72 return 0
73 endif
74 else
75 " Without syntax hints, assume that end-of-heredocs markers begin with EO
76 if cline =~ '^\s*EO'
77 return 0
78 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar9de99972010-08-09 22:33:06 +020081 " Now get the indent of the previous perl line.
82
83 " Find a non-blank line above the current line.
84 let lnum = prevnonblank(v:lnum - 1)
85 " Hit the start of the file, use zero indent.
86 if lnum == 0
87 return 0
88 endif
89 let line = getline(lnum)
90 let ind = indent(lnum)
91 " Skip heredocs, POD, and comments on 1st column
92 if b:indent_use_syntax
93 let skippin = 2
94 while skippin
95 let synid = synIDattr(synID(lnum,1,0),"name")
96 if (synid == "perlStringStartEnd" && line =~ '^\I\i*$')
97 \ || (skippin != 2 && synid == "perlPOD")
98 \ || (skippin != 2 && synid == "perlHereDoc")
99 \ || synid == "perlComment"
100 \ || synid =~ "^pod"
101 let lnum = prevnonblank(lnum - 1)
102 if lnum == 0
103 return 0
104 endif
105 let line = getline(lnum)
106 let ind = indent(lnum)
107 let skippin = 1
108 else
109 let skippin = 0
110 endif
111 endwhile
112 else
113 if line =~ "^EO"
114 let lnum = search("<<[\"']\\=EO", "bW")
115 let line = getline(lnum)
116 let ind = indent(lnum)
117 endif
118 endif
119
120 " Indent blocks enclosed by {}, (), or []
121 if b:indent_use_syntax
122 " Find a real opening brace
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200123 " NOTE: Unlike Perl character classes, we do NOT need to escape the
124 " closing brackets with a backslash. Doing so just puts a backslash
125 " in the character class and causes sorrow. Instead, put the closing
126 " bracket as the first character in the class.
127 let braceclass = '[][(){}]'
128 let bracepos = match(line, braceclass, matchend(line, '^\s*[])}]'))
Bram Moolenaar9de99972010-08-09 22:33:06 +0200129 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"
Bram Moolenaar9d98fe92013-08-03 18:35:36 +0200136 \ || synid == "perlBraces"
Bram Moolenaar9de99972010-08-09 22:33:06 +0200137 \ || synid =~ "^perlFiledescStatement"
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200138 \ || synid =~ '^perl\(Sub\|Block\|Package\)Fold'
Bram Moolenaar9de99972010-08-09 22:33:06 +0200139 let brace = strpart(line, bracepos, 1)
140 if brace == '(' || brace == '{' || brace == '['
141 let ind = ind + &sw
142 else
143 let ind = ind - &sw
144 endif
145 endif
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200146 let bracepos = match(line, braceclass, bracepos + 1)
Bram Moolenaar9de99972010-08-09 22:33:06 +0200147 endwhile
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200148 let bracepos = matchend(cline, '^\s*[])}]')
Bram Moolenaar9de99972010-08-09 22:33:06 +0200149 if bracepos != -1
150 let synid = synIDattr(synID(v:lnum, bracepos, 0), "name")
151 if synid == ""
152 \ || synid == "perlMatchStartEnd"
Bram Moolenaar9d98fe92013-08-03 18:35:36 +0200153 \ || synid == "perlBraces"
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200154 \ || synid =~ '^perl\(Sub\|Block\|Package\)Fold'
Bram Moolenaar9de99972010-08-09 22:33:06 +0200155 let ind = ind - &sw
156 endif
157 endif
158 else
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200159 if line =~ '[{[(]\s*\(#[^])}]*\)\=$'
Bram Moolenaar9de99972010-08-09 22:33:06 +0200160 let ind = ind + &sw
161 endif
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200162 if cline =~ '^\s*[])}]'
Bram Moolenaar9de99972010-08-09 22:33:06 +0200163 let ind = ind - &sw
164 endif
165 endif
166
167 " Indent lines that begin with 'or' or 'and'
168 if cline =~ '^\s*\(or\|and\)\>'
169 if line !~ '^\s*\(or\|and\)\>'
170 let ind = ind + &sw
171 endif
172 elseif line =~ '^\s*\(or\|and\)\>'
173 let ind = ind - &sw
174 endif
175
176 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
178endfunction
179
180let &cpo = s:cpo_save
181unlet s:cpo_save
182
Bram Moolenaar9de99972010-08-09 22:33:06 +0200183" vim:ts=8:sts=4:sw=4:expandtab:ft=vim