blob: 1fbc4ded9e1786f0fa61a18b0c297acca2a66f07 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar9de99972010-08-09 22:33:06 +02002" Language: Perl 5
3" Author: Andy Lester <andy@petdance.com>
4" URL: http://github.com/petdance/vim-perl/tree/master
5" Last Change: June 3, 2009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
7" Suggestions and improvements by :
8" Aaron J. Sherman (use syntax for hints)
9" Artem Chuprina (play nice with folding)
10
11" TODO things that are not or not properly indented (yet) :
12" - Continued statements
13" print "foo",
14" "bar";
15" print "foo"
16" if bar();
17" - Multiline regular expressions (m//x)
18" (The following probably needs modifying the perl syntax file)
19" - qw() lists
20" - Heredocs with terminators that don't match \I\i*
21
22" Only load this indent file when no other was loaded.
23if exists("b:did_indent")
Bram Moolenaar9de99972010-08-09 22:33:06 +020024 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000025endif
26let b:did_indent = 1
27
28" Is syntax highlighting active ?
Bram Moolenaard1231f92005-09-07 21:12:33 +000029let b:indent_use_syntax = has("syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031setlocal indentexpr=GetPerlIndent()
Bram Moolenaar9de99972010-08-09 22:33:06 +020032setlocal indentkeys+=0=,0),0],0=or,0=and
Bram Moolenaar071d4272004-06-13 20:20:40 +000033if !b:indent_use_syntax
Bram Moolenaar9de99972010-08-09 22:33:06 +020034 setlocal indentkeys+=0=EO
Bram Moolenaar071d4272004-06-13 20:20:40 +000035endif
36
37" Only define the function once.
38if exists("*GetPerlIndent")
Bram Moolenaar9de99972010-08-09 22:33:06 +020039 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000040endif
41
Bram Moolenaarb6356332005-07-18 21:40:44 +000042let s:cpo_save = &cpo
43set cpo-=C
44
Bram Moolenaar071d4272004-06-13 20:20:40 +000045function GetPerlIndent()
46
Bram Moolenaar9de99972010-08-09 22:33:06 +020047 " Get the line to be indented
48 let cline = getline(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000049
Bram Moolenaar9de99972010-08-09 22:33:06 +020050 " Indent POD markers to column 0
51 if cline =~ '^\s*=\L\@!'
52 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000053 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaar9de99972010-08-09 22:33:06 +020055 " Don't reindent coments on first column
56 if cline =~ '^#.'
57 return 0
58 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaar9de99972010-08-09 22:33:06 +020060 " Get current syntax item at the line's first char
61 let csynid = ''
62 if b:indent_use_syntax
63 let csynid = synIDattr(synID(v:lnum,1,0),"name")
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaar9de99972010-08-09 22:33:06 +020066 " Don't reindent POD and heredocs
67 if csynid == "perlPOD" || csynid == "perlHereDoc" || csynid =~ "^pod"
68 return indent(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000069 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaar9de99972010-08-09 22:33:06 +020071 " Indent end-of-heredocs markers to column 0
72 if b:indent_use_syntax
73 " Assumes that an end-of-heredoc marker matches \I\i* to avoid
74 " confusion with other types of strings
75 if csynid == "perlStringStartEnd" && cline =~ '^\I\i*$'
76 return 0
77 endif
78 else
79 " Without syntax hints, assume that end-of-heredocs markers begin with EO
80 if cline =~ '^\s*EO'
81 return 0
82 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar9de99972010-08-09 22:33:06 +020085 " Now get the indent of the previous perl line.
86
87 " Find a non-blank line above the current line.
88 let lnum = prevnonblank(v:lnum - 1)
89 " Hit the start of the file, use zero indent.
90 if lnum == 0
91 return 0
92 endif
93 let line = getline(lnum)
94 let ind = indent(lnum)
95 " Skip heredocs, POD, and comments on 1st column
96 if b:indent_use_syntax
97 let skippin = 2
98 while skippin
99 let synid = synIDattr(synID(lnum,1,0),"name")
100 if (synid == "perlStringStartEnd" && line =~ '^\I\i*$')
101 \ || (skippin != 2 && synid == "perlPOD")
102 \ || (skippin != 2 && synid == "perlHereDoc")
103 \ || synid == "perlComment"
104 \ || synid =~ "^pod"
105 let lnum = prevnonblank(lnum - 1)
106 if lnum == 0
107 return 0
108 endif
109 let line = getline(lnum)
110 let ind = indent(lnum)
111 let skippin = 1
112 else
113 let skippin = 0
114 endif
115 endwhile
116 else
117 if line =~ "^EO"
118 let lnum = search("<<[\"']\\=EO", "bW")
119 let line = getline(lnum)
120 let ind = indent(lnum)
121 endif
122 endif
123
124 " Indent blocks enclosed by {}, (), or []
125 if b:indent_use_syntax
126 " Find a real opening brace
127 let bracepos = match(line, '[(){}\[\]]', matchend(line, '^\s*[)}\]]'))
128 while bracepos != -1
129 let synid = synIDattr(synID(lnum, bracepos + 1, 0), "name")
130 " If the brace is highlighted in one of those groups, indent it.
131 " 'perlHereDoc' is here only to handle the case '&foo(<<EOF)'.
132 if synid == ""
133 \ || synid == "perlMatchStartEnd"
134 \ || synid == "perlHereDoc"
135 \ || synid =~ "^perlFiledescStatement"
136 \ || synid =~ '^perl\(Sub\|Block\)Fold'
137 let brace = strpart(line, bracepos, 1)
138 if brace == '(' || brace == '{' || brace == '['
139 let ind = ind + &sw
140 else
141 let ind = ind - &sw
142 endif
143 endif
144 let bracepos = match(line, '[(){}\[\]]', bracepos + 1)
145 endwhile
146 let bracepos = matchend(cline, '^\s*[)}\]]')
147 if bracepos != -1
148 let synid = synIDattr(synID(v:lnum, bracepos, 0), "name")
149 if synid == ""
150 \ || synid == "perlMatchStartEnd"
151 \ || synid =~ '^perl\(Sub\|Block\)Fold'
152 let ind = ind - &sw
153 endif
154 endif
155 else
156 if line =~ '[{\[(]\s*\(#[^)}\]]*\)\=$'
157 let ind = ind + &sw
158 endif
159 if cline =~ '^\s*[)}\]]'
160 let ind = ind - &sw
161 endif
162 endif
163
164 " Indent lines that begin with 'or' or 'and'
165 if cline =~ '^\s*\(or\|and\)\>'
166 if line !~ '^\s*\(or\|and\)\>'
167 let ind = ind + &sw
168 endif
169 elseif line =~ '^\s*\(or\|and\)\>'
170 let ind = ind - &sw
171 endif
172
173 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
175endfunction
176
177let &cpo = s:cpo_save
178unlet s:cpo_save
179
Bram Moolenaar9de99972010-08-09 22:33:06 +0200180" vim:ts=8:sts=4:sw=4:expandtab:ft=vim