blob: 5fc8b7008aae8a2356c2861dd560d3c04aab3d26 [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>
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
6" Last Change: 2020 Apr 15
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 Moolenaar9de99972010-08-09 22:33:06 +020051 " Get current syntax item at the line's first char
52 let csynid = ''
53 if b:indent_use_syntax
54 let csynid = synIDattr(synID(v:lnum,1,0),"name")
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
Bram Moolenaar9de99972010-08-09 22:33:06 +020057 " Don't reindent POD and heredocs
58 if csynid == "perlPOD" || csynid == "perlHereDoc" || csynid =~ "^pod"
59 return indent(v:lnum)
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 " Indent end-of-heredocs markers to column 0
63 if b:indent_use_syntax
64 " Assumes that an end-of-heredoc marker matches \I\i* to avoid
65 " confusion with other types of strings
66 if csynid == "perlStringStartEnd" && cline =~ '^\I\i*$'
67 return 0
68 endif
69 else
70 " Without syntax hints, assume that end-of-heredocs markers begin with EO
71 if cline =~ '^\s*EO'
72 return 0
73 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaar9de99972010-08-09 22:33:06 +020076 " Now get the indent of the previous perl line.
77
78 " Find a non-blank line above the current line.
79 let lnum = prevnonblank(v:lnum - 1)
80 " Hit the start of the file, use zero indent.
81 if lnum == 0
82 return 0
83 endif
84 let line = getline(lnum)
85 let ind = indent(lnum)
86 " Skip heredocs, POD, and comments on 1st column
87 if b:indent_use_syntax
88 let skippin = 2
89 while skippin
90 let synid = synIDattr(synID(lnum,1,0),"name")
91 if (synid == "perlStringStartEnd" && line =~ '^\I\i*$')
92 \ || (skippin != 2 && synid == "perlPOD")
93 \ || (skippin != 2 && synid == "perlHereDoc")
94 \ || synid == "perlComment"
95 \ || synid =~ "^pod"
96 let lnum = prevnonblank(lnum - 1)
97 if lnum == 0
98 return 0
99 endif
100 let line = getline(lnum)
101 let ind = indent(lnum)
102 let skippin = 1
103 else
104 let skippin = 0
105 endif
106 endwhile
107 else
108 if line =~ "^EO"
109 let lnum = search("<<[\"']\\=EO", "bW")
110 let line = getline(lnum)
111 let ind = indent(lnum)
112 endif
113 endif
114
115 " Indent blocks enclosed by {}, (), or []
116 if b:indent_use_syntax
117 " Find a real opening brace
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200118 " NOTE: Unlike Perl character classes, we do NOT need to escape the
119 " closing brackets with a backslash. Doing so just puts a backslash
120 " in the character class and causes sorrow. Instead, put the closing
121 " bracket as the first character in the class.
122 let braceclass = '[][(){}]'
123 let bracepos = match(line, braceclass, matchend(line, '^\s*[])}]'))
Bram Moolenaar9de99972010-08-09 22:33:06 +0200124 while bracepos != -1
125 let synid = synIDattr(synID(lnum, bracepos + 1, 0), "name")
126 " If the brace is highlighted in one of those groups, indent it.
127 " 'perlHereDoc' is here only to handle the case '&foo(<<EOF)'.
128 if synid == ""
129 \ || synid == "perlMatchStartEnd"
130 \ || synid == "perlHereDoc"
Bram Moolenaar9d98fe92013-08-03 18:35:36 +0200131 \ || synid == "perlBraces"
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200132 \ || synid == "perlStatementIndirObj"
Bram Moolenaar9de99972010-08-09 22:33:06 +0200133 \ || synid =~ "^perlFiledescStatement"
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200134 \ || synid =~ '^perl\(Sub\|Block\|Package\)Fold'
Bram Moolenaar9de99972010-08-09 22:33:06 +0200135 let brace = strpart(line, bracepos, 1)
136 if brace == '(' || brace == '{' || brace == '['
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200137 let ind = ind + shiftwidth()
Bram Moolenaar9de99972010-08-09 22:33:06 +0200138 else
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200139 let ind = ind - shiftwidth()
Bram Moolenaar9de99972010-08-09 22:33:06 +0200140 endif
141 endif
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200142 let bracepos = match(line, braceclass, bracepos + 1)
Bram Moolenaar9de99972010-08-09 22:33:06 +0200143 endwhile
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200144 let bracepos = matchend(cline, '^\s*[])}]')
Bram Moolenaar9de99972010-08-09 22:33:06 +0200145 if bracepos != -1
146 let synid = synIDattr(synID(v:lnum, bracepos, 0), "name")
147 if synid == ""
148 \ || synid == "perlMatchStartEnd"
Bram Moolenaar9d98fe92013-08-03 18:35:36 +0200149 \ || synid == "perlBraces"
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200150 \ || synid == "perlStatementIndirObj"
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200151 \ || synid =~ '^perl\(Sub\|Block\|Package\)Fold'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200152 let ind = ind - shiftwidth()
Bram Moolenaar9de99972010-08-09 22:33:06 +0200153 endif
154 endif
155 else
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200156 if line =~ '[{[(]\s*\(#[^])}]*\)\=$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200157 let ind = ind + shiftwidth()
Bram Moolenaar9de99972010-08-09 22:33:06 +0200158 endif
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200159 if cline =~ '^\s*[])}]'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200160 let ind = ind - shiftwidth()
Bram Moolenaar9de99972010-08-09 22:33:06 +0200161 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\)\>'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200167 let ind = ind + shiftwidth()
Bram Moolenaar9de99972010-08-09 22:33:06 +0200168 endif
169 elseif line =~ '^\s*\(or\|and\)\>'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200170 let ind = ind - shiftwidth()
Bram Moolenaar9de99972010-08-09 22:33:06 +0200171 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