Mercurial > vim
annotate runtime/indent/perl.vim @ 17437:5f71f12bdb8c
Added tag v8.1.1716 for changeset e1b5c15f5fee70aaa68aa8286030cf713a403aee
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 19 Jul 2019 23:30:05 +0200 |
parents | d91cf2e26ef0 |
children | bd021eb62e73 |
rev | line source |
---|---|
7 | 1 " Vim indent file |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
2 " Language: Perl 5 |
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
3 " Maintainer: vim-perl <vim-perl@googlegroups.com> |
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
4 " Homepage: http://github.com/vim-perl/vim-perl |
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
5 " Bugs/requests: http://github.com/vim-perl/vim-perl/issues |
12499 | 6 " Last Change: 2017-01-04 |
7 | 7 |
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", | |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
15 " "bar"; |
7 | 16 " print "foo" |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
17 " if bar(); |
7 | 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") | |
2531 | 25 finish |
7 | 26 endif |
27 let b:did_indent = 1 | |
28 | |
29 " Is syntax highlighting active ? | |
508 | 30 let b:indent_use_syntax = has("syntax") |
7 | 31 |
32 setlocal indentexpr=GetPerlIndent() | |
2531 | 33 setlocal indentkeys+=0=,0),0],0=or,0=and |
7 | 34 if !b:indent_use_syntax |
2531 | 35 setlocal indentkeys+=0=EO |
7 | 36 endif |
37 | |
416 | 38 let s:cpo_save = &cpo |
39 set cpo-=C | |
40 | |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
41 function! GetPerlIndent() |
7 | 42 |
2531 | 43 " Get the line to be indented |
44 let cline = getline(v:lnum) | |
7 | 45 |
2531 | 46 " Indent POD markers to column 0 |
47 if cline =~ '^\s*=\L\@!' | |
48 return 0 | |
49 endif | |
7 | 50 |
2531 | 51 " 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") | |
55 endif | |
7 | 56 |
2531 | 57 " Don't reindent POD and heredocs |
58 if csynid == "perlPOD" || csynid == "perlHereDoc" || csynid =~ "^pod" | |
59 return indent(v:lnum) | |
60 endif | |
7 | 61 |
2531 | 62 " 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 | |
7 | 74 endif |
75 | |
2531 | 76 " Now get the indent of the previous perl line. |
7 | 77 |
2531 | 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 | |
7 | 83 endif |
2531 | 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 | |
7 | 114 |
2531 | 115 " Indent blocks enclosed by {}, (), or [] |
116 if b:indent_use_syntax | |
117 " Find a real opening brace | |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
118 " NOTE: Unlike Perl character classes, we do NOT need to escape the |
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
119 " closing brackets with a backslash. Doing so just puts a backslash |
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
120 " in the character class and causes sorrow. Instead, put the closing |
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
121 " bracket as the first character in the class. |
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
122 let braceclass = '[][(){}]' |
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
123 let bracepos = match(line, braceclass, matchend(line, '^\s*[])}]')) |
2531 | 124 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" | |
5277 | 131 \ || synid == "perlBraces" |
12499 | 132 \ || synid == "perlStatementIndirObj" |
2531 | 133 \ || synid =~ "^perlFiledescStatement" |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
134 \ || synid =~ '^perl\(Sub\|Block\|Package\)Fold' |
2531 | 135 let brace = strpart(line, bracepos, 1) |
136 if brace == '(' || brace == '{' || brace == '[' | |
11518 | 137 let ind = ind + shiftwidth() |
2531 | 138 else |
11518 | 139 let ind = ind - shiftwidth() |
2531 | 140 endif |
141 endif | |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
142 let bracepos = match(line, braceclass, bracepos + 1) |
2531 | 143 endwhile |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
144 let bracepos = matchend(cline, '^\s*[])}]') |
2531 | 145 if bracepos != -1 |
146 let synid = synIDattr(synID(v:lnum, bracepos, 0), "name") | |
147 if synid == "" | |
148 \ || synid == "perlMatchStartEnd" | |
5277 | 149 \ || synid == "perlBraces" |
12499 | 150 \ || synid == "perlStatementIndirObj" |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
151 \ || synid =~ '^perl\(Sub\|Block\|Package\)Fold' |
11518 | 152 let ind = ind - shiftwidth() |
2531 | 153 endif |
154 endif | |
155 else | |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
156 if line =~ '[{[(]\s*\(#[^])}]*\)\=$' |
11518 | 157 let ind = ind + shiftwidth() |
2531 | 158 endif |
4681
2eb30f341e8d
Updated runtime files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2531
diff
changeset
|
159 if cline =~ '^\s*[])}]' |
11518 | 160 let ind = ind - shiftwidth() |
2531 | 161 endif |
7 | 162 endif |
163 | |
2531 | 164 " Indent lines that begin with 'or' or 'and' |
165 if cline =~ '^\s*\(or\|and\)\>' | |
166 if line !~ '^\s*\(or\|and\)\>' | |
11518 | 167 let ind = ind + shiftwidth() |
2531 | 168 endif |
169 elseif line =~ '^\s*\(or\|and\)\>' | |
11518 | 170 let ind = ind - shiftwidth() |
7 | 171 endif |
172 | |
2531 | 173 return ind |
7 | 174 |
175 endfunction | |
176 | |
177 let &cpo = s:cpo_save | |
178 unlet s:cpo_save | |
179 | |
2531 | 180 " vim:ts=8:sts=4:sw=4:expandtab:ft=vim |