blob: 4c91fa1b333694ff86d6e5b4ff885aa17259e967 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar8c1b8cb2022-06-14 17:41:28 +01002" Language: Perl
Bram Moolenaar543b7ef2013-06-01 14:50:56 +02003" Maintainer: vim-perl <vim-perl@googlegroups.com>
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +02004" Homepage: https://github.com/vim-perl/vim-perl
5" Bugs/requests: https://github.com/vim-perl/vim-perl/issues
Bram Moolenaar8c1b8cb2022-06-14 17:41:28 +01006" License: Vim License (see :help license)
7" Last Change: 2021 Sep 24
Bram Moolenaar071d4272004-06-13 20:20:40 +00008
Bram Moolenaard592deb2022-06-17 15:42:40 +01009" Suggestions and improvements by :
10" Aaron J. Sherman (use syntax for hints)
11" Artem Chuprina (play nice with folding)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012
Bram Moolenaard592deb2022-06-17 15:42:40 +010013" TODO things that are not or not properly indented (yet) :
14" - Continued statements
15" print "foo",
16" "bar";
17" print "foo"
18" if bar();
19" - Multiline regular expressions (m//x)
20" (The following probably needs modifying the perl syntax file)
21" - qw() lists
22" - Heredocs with terminators that don't match \I\i*
23
24" Only load this indent file when no other was loaded.
25if exists("b:did_indent")
26 finish
27endif
28let b:did_indent = 1
29
30" Is syntax highlighting active ?
31let b:indent_use_syntax = has("syntax")
32
33setlocal indentexpr=GetPerlIndent()
34setlocal indentkeys+=0=,0),0],0=or,0=and
35if !b:indent_use_syntax
36 setlocal indentkeys+=0=EO
37endif
38
39let b:undo_indent = "setl inde< indk<"
40
41let s:cpo_save = &cpo
Bram Moolenaarb6356332005-07-18 21:40:44 +000042set cpo-=C
43
Bram Moolenaard592deb2022-06-17 15:42:40 +010044function! GetPerlIndent()
Bram Moolenaar071d4272004-06-13 20:20:40 +000045
Bram Moolenaard592deb2022-06-17 15:42:40 +010046 " Get the line to be indented
47 let cline = getline(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
Bram Moolenaard592deb2022-06-17 15:42:40 +010049 " Indent POD markers to column 0
50 if cline =~ '^\s*=\L\@!'
51 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
Bram Moolenaard592deb2022-06-17 15:42:40 +010054 " Get current syntax item at the line's first char
55 let csynid = ''
56 if b:indent_use_syntax
57 let csynid = synIDattr(synID(v:lnum,1,0),"name")
Bram Moolenaar9de99972010-08-09 22:33:06 +020058 endif
59
Bram Moolenaard592deb2022-06-17 15:42:40 +010060 " Don't reindent POD and heredocs
61 if csynid == "perlPOD" || csynid == "perlHereDoc" || csynid =~ "^pod"
62 return indent(v:lnum)
63 endif
Bram Moolenaar9de99972010-08-09 22:33:06 +020064
Bram Moolenaard592deb2022-06-17 15:42:40 +010065 " Indent end-of-heredocs markers to column 0
66 if b:indent_use_syntax
67 " Assumes that an end-of-heredoc marker matches \I\i* to avoid
68 " confusion with other types of strings
69 if csynid == "perlStringStartEnd" && cline =~ '^\I\i*$'
70 return 0
71 endif
72 else
73 " Without syntax hints, assume that end-of-heredocs markers begin with EO
74 if cline =~ '^\s*EO'
75 return 0
76 endif
77 endif
Bram Moolenaar9de99972010-08-09 22:33:06 +020078
Bram Moolenaard592deb2022-06-17 15:42:40 +010079 " Now get the indent of the previous perl line.
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaard592deb2022-06-17 15:42:40 +010081 " Find a non-blank line above the current line.
82 let lnum = prevnonblank(v:lnum - 1)
83 " Hit the start of the file, use zero indent.
84 if lnum == 0
85 return 0
86 endif
87 let line = getline(lnum)
88 let ind = indent(lnum)
89 " Skip heredocs, POD, and comments on 1st column
90 if b:indent_use_syntax
91 let skippin = 2
92 while skippin
93 let synid = synIDattr(synID(lnum,1,0),"name")
94 if (synid == "perlStringStartEnd" && line =~ '^\I\i*$')
95 \ || (skippin != 2 && synid == "perlPOD")
96 \ || (skippin != 2 && synid == "perlHereDoc")
97 \ || synid == "perlComment"
98 \ || synid =~ "^pod"
99 let lnum = prevnonblank(lnum - 1)
100 if lnum == 0
101 return 0
102 endif
103 let line = getline(lnum)
104 let ind = indent(lnum)
105 let skippin = 1
106 else
107 let skippin = 0
108 endif
109 endwhile
110 else
111 if line =~ "^EO"
112 let lnum = search("<<[\"']\\=EO", "bW")
113 let line = getline(lnum)
114 let ind = indent(lnum)
115 endif
116 endif
117
118 " Indent blocks enclosed by {}, (), or []
119 if b:indent_use_syntax
120 " Find a real opening brace
121 " NOTE: Unlike Perl character classes, we do NOT need to escape the
122 " closing brackets with a backslash. Doing so just puts a backslash
123 " in the character class and causes sorrow. Instead, put the closing
124 " bracket as the first character in the class.
125 let braceclass = '[][(){}]'
126 let bracepos = match(line, braceclass, matchend(line, '^\s*[])}]'))
127 while bracepos != -1
128 let synid = synIDattr(synID(lnum, bracepos + 1, 0), "name")
129 " If the brace is highlighted in one of those groups, indent it.
130 " 'perlHereDoc' is here only to handle the case '&foo(<<EOF)'.
131 if synid == ""
132 \ || synid == "perlMatchStartEnd"
133 \ || synid == "perlHereDoc"
134 \ || synid == "perlBraces"
135 \ || synid == "perlStatementIndirObj"
136 \ || synid =~ "^perlFiledescStatement"
137 \ || synid =~ '^perl\(Sub\|Block\|Package\)Fold'
138 let brace = strpart(line, bracepos, 1)
139 if brace == '(' || brace == '{' || brace == '['
140 let ind = ind + shiftwidth()
141 else
142 let ind = ind - shiftwidth()
143 endif
144 endif
145 let bracepos = match(line, braceclass, 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"
152 \ || synid == "perlBraces"
153 \ || synid == "perlStatementIndirObj"
154 \ || synid =~ '^perl\(Sub\|Block\|Package\)Fold'
155 let ind = ind - shiftwidth()
156 endif
157 endif
158 else
159 if line =~ '[{[(]\s*\(#[^])}]*\)\=$'
160 let ind = ind + shiftwidth()
161 endif
162 if cline =~ '^\s*[])}]'
163 let ind = ind - shiftwidth()
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 + shiftwidth()
171 endif
172 elseif line =~ '^\s*\(or\|and\)\>'
173 let ind = ind - shiftwidth()
174 endif
175
176 return ind
177
178endfunction
179
180let &cpo = s:cpo_save
181unlet s:cpo_save
182
183" vim:ts=8:sts=4:sw=4:expandtab:ft=vim