Mercurial > vim
annotate runtime/indent/php.vim @ 7690:0f193b74d989
Added tag v7.4.1143 for changeset 20dc2763a3b989a105a140894493daaa05f883aa
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 19 Jan 2016 23:45:05 +0100 |
parents | 5d89d9b40499 |
children | 78106b0f2c56 |
rev | line source |
---|---|
7 | 1 " Vim indent file |
2 " Language: PHP | |
409 | 3 " Author: John Wellesz <John.wellesz (AT) teaser (DOT) fr> |
4 " URL: http://www.2072productions.com/vim/indent/php.vim | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
5 " Home: https://github.com/2072/PHP-Indenting-for-VIm |
6421 | 6 " Last Change: 2014 November 26th |
7 " Version: 1.57 | |
557 | 8 " |
856 | 9 " |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
10 " Type :help php-indent for available options |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
11 " |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
12 " A fully commented version of this file is available on github |
409 | 13 " |
14 " | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
15 " If you find a bug, please open a ticket on github.org |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
16 " ( https://github.com/2072/PHP-Indenting-for-VIm/issues ) with an example of |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
17 " code that breaks the algorithm. |
409 | 18 " |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
19 |
409 | 20 " NOTE: This script must be used with PHP syntax ON and with the php syntax |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
21 " script by Lutz Eymers (http://www.isp.de/data/php.vim ) or with the |
1668 | 22 " script by Peter Hodge (http://www.vim.org/scripts/script.php?script_id=1571 ) |
2034 | 23 " the later is bunbdled by default with Vim 7. |
409 | 24 " |
25 " | |
1668 | 26 " In the case you have syntax errors in your script such as HereDoc end |
27 " identifiers not at col 1 you'll have to indent your file 2 times (This | |
28 " script will automatically put HereDoc end identifiers at col 1 if | |
29 " they are followed by a ';'). | |
856 | 30 " |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
31 |
2034 | 32 " NOTE: If you are editing files in Unix file format and that (by accident) |
33 " there are '\r' before new lines, this script won't be able to proceed | |
34 " correctly and will make many mistakes because it won't be able to match | |
35 " '\s*$' correctly. | |
36 " So you have to remove those useless characters first with a command like: | |
409 | 37 " |
2034 | 38 " :%s /\r$//g |
7 | 39 " |
2034 | 40 " or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will |
41 " silently remove them when VIM load this script (at each bufread). | |
5862 | 42 |
43 | |
557 | 44 |
7 | 45 if exists("b:did_indent") |
532 | 46 finish |
7 | 47 endif |
48 let b:did_indent = 1 | |
49 | |
409 | 50 |
6421 | 51 let g:php_sync_method = 0 |
409 | 52 |
53 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
54 |
409 | 55 if exists("PHP_default_indenting") |
532 | 56 let b:PHP_default_indenting = PHP_default_indenting * &sw |
409 | 57 else |
532 | 58 let b:PHP_default_indenting = 0 |
409 | 59 endif |
60 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
61 if exists("PHP_outdentSLComments") |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
62 let b:PHP_outdentSLComments = PHP_outdentSLComments * &sw |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
63 else |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
64 let b:PHP_outdentSLComments = 0 |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
65 endif |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
66 |
409 | 67 if exists("PHP_BracesAtCodeLevel") |
532 | 68 let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel |
409 | 69 else |
532 | 70 let b:PHP_BracesAtCodeLevel = 0 |
409 | 71 endif |
72 | |
1668 | 73 |
557 | 74 if exists("PHP_autoformatcomment") |
75 let b:PHP_autoformatcomment = PHP_autoformatcomment | |
76 else | |
77 let b:PHP_autoformatcomment = 1 | |
78 endif | |
7 | 79 |
2442 | 80 if exists("PHP_outdentphpescape") |
81 let b:PHP_outdentphpescape = PHP_outdentphpescape | |
82 else | |
83 let b:PHP_outdentphpescape = 1 | |
84 endif | |
85 | |
86 | |
87 if exists("PHP_vintage_case_default_indent") && PHP_vintage_case_default_indent | |
88 let b:PHP_vintage_case_default_indent = 1 | |
1668 | 89 else |
90 let b:PHP_vintage_case_default_indent = 0 | |
91 endif | |
92 | |
93 | |
94 | |
409 | 95 let b:PHP_lastindented = 0 |
96 let b:PHP_indentbeforelast = 0 | |
97 let b:PHP_indentinghuge = 0 | |
98 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
99 let b:PHP_LastIndentedWasComment = 0 | |
100 let b:PHP_InsideMultilineComment = 0 | |
101 let b:InPHPcode = 0 | |
102 let b:InPHPcode_checked = 0 | |
103 let b:InPHPcode_and_script = 0 | |
104 let b:InPHPcode_tofind = "" | |
105 let b:PHP_oldchangetick = b:changedtick | |
106 let b:UserIsTypingComment = 0 | |
107 let b:optionsset = 0 | |
108 | |
109 setlocal nosmartindent | |
856 | 110 setlocal noautoindent |
409 | 111 setlocal nocindent |
532 | 112 setlocal nolisp |
409 | 113 |
114 setlocal indentexpr=GetPhpIndent() | |
6421 | 115 setlocal indentkeys=0{,0},0),0],:,!^F,o,O,e,*<Return>,=?>,=<?,=*/ |
409 | 116 |
117 | |
532 | 118 |
119 let s:searchpairflags = 'bWr' | |
409 | 120 |
121 if &fileformat == "unix" && exists("PHP_removeCRwhenUnix") && PHP_removeCRwhenUnix | |
532 | 122 silent! %s/\r$//g |
409 | 123 endif |
124 | |
7 | 125 if exists("*GetPhpIndent") |
2034 | 126 call ResetPhpOptions() |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
127 finish " XXX -- comment this line for easy dev |
7 | 128 endif |
129 | |
5862 | 130 |
6421 | 131 let s:PHP_validVariable = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' |
5862 | 132 let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\|die\|else\)' |
133 let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|\%(}\s*\)\?else\>\|do\>\|while\>\|switch\>\|case\>\|default\>\|for\%(each\)\=\>\|declare\>\|class\>\|trait\>\|use\>\|interface\>\|abstract\>\|final\>\|try\>\|\%(}\s*\)\=catch\>\|\%(}\s*\)\=finally\>\)' | |
6421 | 134 let s:functionDecl = '\<function\>\%(\s\+'.s:PHP_validVariable.'\)\=\s*(.*' |
409 | 135 let s:endline= '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$' |
6421 | 136 |
137 | |
138 let s:terminated = '\%(\%(;\%(\s*\%(?>\|}\)\)\=\|<<<''\=\a\w*''\=$\|^\s*}\|^\s*'.s:PHP_validVariable.':\)'.s:endline.'\)\|^[^''"`]*[''"`]$' | |
409 | 139 let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!' |
140 | |
141 | |
5862 | 142 |
143 let s:escapeDebugStops = 0 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
144 function! DebugPrintReturn(scriptLine) |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
145 |
6421 | 146 if ! s:escapeDebugStops |
5862 | 147 echo "debug:" . a:scriptLine |
148 let c = getchar() | |
149 if c == "\<Del>" | |
150 let s:escapeDebugStops = 1 | |
151 end | |
152 endif | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
153 |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
154 endfunction |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
155 |
409 | 156 function! GetLastRealCodeLNum(startline) " {{{ |
856 | 157 |
532 | 158 let lnum = a:startline |
856 | 159 |
631 | 160 if b:GetLastRealCodeLNum_ADD && b:GetLastRealCodeLNum_ADD == lnum + 1 |
161 let lnum = b:GetLastRealCodeLNum_ADD | |
162 endif | |
856 | 163 |
532 | 164 while lnum > 1 |
165 let lnum = prevnonblank(lnum) | |
166 let lastline = getline(lnum) | |
409 | 167 |
532 | 168 if b:InPHPcode_and_script && lastline =~ '?>\s*$' |
169 let lnum = lnum - 1 | |
170 elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$' | |
171 let lnum = lnum - 1 | |
172 elseif lastline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)' | |
173 let lnum = lnum - 1 | |
174 elseif lastline =~ '\*/\s*$' | |
175 call cursor(lnum, 1) | |
176 if lastline !~ '^\*/' | |
177 call search('\*/', 'W') | |
178 endif | |
557 | 179 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()') |
409 | 180 |
532 | 181 let lastline = getline(lnum) |
182 if lastline =~ '^\s*/\*' | |
183 let lnum = lnum - 1 | |
184 else | |
185 break | |
186 endif | |
409 | 187 |
188 | |
532 | 189 elseif lastline =~? '\%(//\s*\|?>.*\)\@<!<?\%(php\)\=\s*$\|^\s*<script\>' |
190 | |
191 while lastline !~ '\(<?.*\)\@<!?>' && lnum > 1 | |
192 let lnum = lnum - 1 | |
193 let lastline = getline(lnum) | |
194 endwhile | |
195 if lastline =~ '^\s*?>' | |
196 let lnum = lnum - 1 | |
197 else | |
198 break | |
199 endif | |
200 | |
409 | 201 |
2442 | 202 elseif lastline =~? '^\a\w*;\=$' && lastline !~? s:notPhpHereDoc |
203 let tofind=substitute( lastline, '\(\a\w*\);\=', '<<<''\\=\1''\\=$', '') | |
532 | 204 while getline(lnum) !~? tofind && lnum > 1 |
205 let lnum = lnum - 1 | |
206 endwhile | |
5862 | 207 elseif lastline =~ '^[^''"`]*[''"`][;,]'.s:endline |
208 let tofind=substitute( lastline, '^.*\([''"`]\)[;,].*$', '^[^\1]\\+[\1]$', '') | |
209 while getline(lnum) !~? tofind && lnum > 1 | |
210 let lnum = lnum - 1 | |
211 endwhile | |
532 | 212 else |
856 | 213 break |
409 | 214 endif |
532 | 215 endwhile |
216 | |
2442 | 217 if lnum==1 && getline(lnum) !~ '<?' |
532 | 218 let lnum=0 |
219 endif | |
220 | |
6421 | 221 if b:InPHPcode_and_script && 1 > b:InPHPcode |
532 | 222 let b:InPHPcode_and_script = 0 |
223 endif | |
1120 | 224 |
532 | 225 return lnum |
226 endfunction " }}} | |
409 | 227 |
557 | 228 function! Skippmatch2() |
229 | |
230 let line = getline(".") | |
231 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
232 if line =~ "\\([\"']\\).*/\\*.*\\1" || line =~ '\%(//\|#\).*/\*' |
5862 | 233 return 1 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
234 else |
5862 | 235 return 0 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
236 endif |
557 | 237 endfun |
238 | |
2442 | 239 function! Skippmatch() " {{{ |
532 | 240 let synname = synIDattr(synID(line("."), col("."), 0), "name") |
6421 | 241 if synname == "Delimiter" || synname == "phpRegionDelimiter" || synname =~# "^phpParent" || synname == "phpArrayParens" || synname =~# '^php\%(Block\|Brace\)' || synname == "javaScriptBraces" || synname =~# '^php\%(Doc\)\?Comment' && b:UserIsTypingComment |
532 | 242 return 0 |
243 else | |
244 return 1 | |
245 endif | |
246 endfun " }}} | |
409 | 247 |
5862 | 248 function! FindOpenBracket(lnum, blockStarter) " {{{ |
532 | 249 call cursor(a:lnum, 1) |
5862 | 250 let line = searchpair('{', '', '}', 'bW', 'Skippmatch()') |
251 | |
252 if a:blockStarter == 1 | |
6421 | 253 while line > 1 |
5862 | 254 let linec = getline(line) |
255 | |
256 if linec =~ s:terminated || linec =~ '^\s*\%(' . s:blockstart . '\)\|'. s:functionDecl . s:endline | |
257 break | |
258 endif | |
259 | |
260 let line = GetLastRealCodeLNum(line - 1) | |
261 endwhile | |
262 endif | |
263 | |
264 return line | |
532 | 265 endfun " }}} |
409 | 266 |
267 function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{ | |
532 | 268 |
269 if getline(a:lnum) =~# '^\s*}\s*else\%(if\)\=\>' | |
270 let beforeelse = a:lnum | |
271 else | |
272 let beforeelse = GetLastRealCodeLNum(a:lnum - 1) | |
273 endif | |
409 | 274 |
532 | 275 if !s:level |
276 let s:iftoskip = 0 | |
277 endif | |
278 | |
279 if getline(beforeelse) =~# '^\s*\%(}\s*\)\=else\%(\s*if\)\@!\>' | |
280 let s:iftoskip = s:iftoskip + 1 | |
281 endif | |
282 | |
283 if getline(beforeelse) =~ '^\s*}' | |
5862 | 284 let beforeelse = FindOpenBracket(beforeelse, 0) |
532 | 285 |
286 if getline(beforeelse) =~ '^\s*{' | |
287 let beforeelse = GetLastRealCodeLNum(beforeelse - 1) | |
409 | 288 endif |
532 | 289 endif |
409 | 290 |
291 | |
532 | 292 if !s:iftoskip && a:StopAfterFirstPrevElse && getline(beforeelse) =~# '^\s*\%([}]\s*\)\=else\%(if\)\=\>' |
293 return beforeelse | |
294 endif | |
295 | |
296 if getline(beforeelse) !~# '^\s*if\>' && beforeelse>1 || s:iftoskip && beforeelse>1 | |
297 | |
2442 | 298 if s:iftoskip && getline(beforeelse) =~# '^\s*if\>' |
532 | 299 let s:iftoskip = s:iftoskip - 1 |
409 | 300 endif |
301 | |
532 | 302 let s:level = s:level + 1 |
303 let beforeelse = FindTheIfOfAnElse(beforeelse, a:StopAfterFirstPrevElse) | |
304 endif | |
409 | 305 |
532 | 306 return beforeelse |
409 | 307 |
532 | 308 endfunction " }}} |
409 | 309 |
2442 | 310 let s:defaultORcase = '^\s*\%(default\|case\).*:' |
311 | |
312 function! FindTheSwitchIndent (lnum) " {{{ | |
313 | |
314 let test = GetLastRealCodeLNum(a:lnum - 1) | |
315 | |
316 if test <= 1 | |
317 return indent(1) - &sw * b:PHP_vintage_case_default_indent | |
318 end | |
319 | |
5862 | 320 while getline(test) =~ '^\s*}' && test > 1 |
321 let test = GetLastRealCodeLNum(FindOpenBracket(test, 0) - 1) | |
2442 | 322 |
5862 | 323 if getline(test) =~ '^\s*switch\>' |
324 let test = GetLastRealCodeLNum(test - 1) | |
2442 | 325 endif |
5862 | 326 endwhile |
2442 | 327 |
328 if getline(test) =~# '^\s*switch\>' | |
329 return indent(test) | |
330 elseif getline(test) =~# s:defaultORcase | |
331 return indent(test) - &sw * b:PHP_vintage_case_default_indent | |
332 else | |
333 return FindTheSwitchIndent(test) | |
334 endif | |
335 | |
336 endfunction "}}} | |
337 | |
5294 | 338 let s:SynPHPMatchGroups = {'phpParent':1, 'Delimiter':1, 'Define':1, 'Storageclass':1, 'StorageClass':1, 'Structure':1, 'Exception':1} |
409 | 339 function! IslinePHP (lnum, tofind) " {{{ |
532 | 340 let cline = getline(a:lnum) |
409 | 341 |
532 | 342 if a:tofind=="" |
5862 | 343 let tofind = "^\\s*[\"'`]*\\s*\\zs\\S" |
532 | 344 else |
345 let tofind = a:tofind | |
346 endif | |
409 | 347 |
532 | 348 let tofind = tofind . '\c' |
409 | 349 |
532 | 350 let coltotest = match (cline, tofind) + 1 |
351 | |
352 let synname = synIDattr(synID(a:lnum, coltotest, 0), "name") | |
409 | 353 |
5862 | 354 if synname == 'phpStringSingle' || synname == 'phpStringDouble' || synname == 'phpBacktick' |
355 if cline !~ '^\s*[''"`]' | |
6421 | 356 return "SpecStringEntrails" |
5862 | 357 else |
358 return synname | |
359 end | |
360 end | |
361 | |
5277 | 362 if get(s:SynPHPMatchGroups, synname) || synname =~ '^php' || synname =~? '^javaScript' |
532 | 363 return synname |
364 else | |
365 return "" | |
366 endif | |
367 endfunction " }}} | |
409 | 368 |
2034 | 369 let s:autoresetoptions = 0 |
370 if ! s:autoresetoptions | |
371 let s:autoresetoptions = 1 | |
7 | 372 endif |
373 | |
2034 | 374 function! ResetPhpOptions() |
6421 | 375 if ! b:optionsset && &filetype =~ "php" |
557 | 376 if b:PHP_autoformatcomment |
377 | |
378 setlocal comments=s1:/*,mb:*,ex:*/,://,:# | |
856 | 379 |
1668 | 380 setlocal formatoptions-=t |
557 | 381 setlocal formatoptions+=q |
382 setlocal formatoptions+=r | |
383 setlocal formatoptions+=o | |
384 setlocal formatoptions+=c | |
385 setlocal formatoptions+=b | |
386 endif | |
532 | 387 let b:optionsset = 1 |
388 endif | |
409 | 389 endfunc |
390 | |
2034 | 391 call ResetPhpOptions() |
392 | |
409 | 393 function! GetPhpIndent() |
394 | |
631 | 395 let b:GetLastRealCodeLNum_ADD = 0 |
396 | |
532 | 397 let UserIsEditing=0 |
398 if b:PHP_oldchangetick != b:changedtick | |
399 let b:PHP_oldchangetick = b:changedtick | |
400 let UserIsEditing=1 | |
401 endif | |
409 | 402 |
532 | 403 if b:PHP_default_indenting |
404 let b:PHP_default_indenting = g:PHP_default_indenting * &sw | |
405 endif | |
406 | |
407 let cline = getline(v:lnum) | |
409 | 408 |
856 | 409 if !b:PHP_indentinghuge && b:PHP_lastindented > b:PHP_indentbeforelast |
532 | 410 if b:PHP_indentbeforelast |
411 let b:PHP_indentinghuge = 1 | |
412 endif | |
413 let b:PHP_indentbeforelast = b:PHP_lastindented | |
414 endif | |
409 | 415 |
532 | 416 if b:InPHPcode_checked && prevnonblank(v:lnum - 1) != b:PHP_lastindented |
417 if b:PHP_indentinghuge | |
418 let b:PHP_indentinghuge = 0 | |
419 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
409 | 420 endif |
6421 | 421 let real_PHP_lastindented = v:lnum |
532 | 422 let b:PHP_LastIndentedWasComment=0 |
423 let b:PHP_InsideMultilineComment=0 | |
424 let b:PHP_indentbeforelast = 0 | |
409 | 425 |
532 | 426 let b:InPHPcode = 0 |
427 let b:InPHPcode_checked = 0 | |
428 let b:InPHPcode_and_script = 0 | |
429 let b:InPHPcode_tofind = "" | |
409 | 430 |
532 | 431 elseif v:lnum > b:PHP_lastindented |
432 let real_PHP_lastindented = b:PHP_lastindented | |
6421 | 433 else |
434 let real_PHP_lastindented = v:lnum | |
532 | 435 endif |
409 | 436 |
6421 | 437 let b:PHP_lastindented = v:lnum |
438 | |
409 | 439 |
532 | 440 if !b:InPHPcode_checked " {{{ One time check |
441 let b:InPHPcode_checked = 1 | |
409 | 442 |
631 | 443 let synname = "" |
444 if cline !~ '<?.*?>' | |
445 let synname = IslinePHP (prevnonblank(v:lnum), "") | |
446 endif | |
409 | 447 |
532 | 448 if synname!="" |
6421 | 449 if synname == "SpecStringEntrails" |
450 let b:InPHPcode = -1 " thumb down | |
451 let b:UserIsTypingComment = 0 | |
452 let b:InPHPcode_tofind = "" | |
453 elseif synname != "phpHereDoc" && synname != "phpHereDocDelimiter" | |
532 | 454 let b:InPHPcode = 1 |
455 let b:InPHPcode_tofind = "" | |
409 | 456 |
6421 | 457 if synname =~# '^php\%(Doc\)\?Comment' |
532 | 458 let b:UserIsTypingComment = 1 |
459 else | |
460 let b:UserIsTypingComment = 0 | |
409 | 461 endif |
462 | |
532 | 463 if synname =~? '^javaScript' |
464 let b:InPHPcode_and_script = 1 | |
465 endif | |
409 | 466 |
532 | 467 else |
468 let b:InPHPcode = 0 | |
469 let b:UserIsTypingComment = 0 | |
409 | 470 |
532 | 471 let lnum = v:lnum - 1 |
1668 | 472 while getline(lnum) !~? '<<<''\=\a\w*''\=$' && lnum > 1 |
532 | 473 let lnum = lnum - 1 |
474 endwhile | |
409 | 475 |
2442 | 476 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') |
532 | 477 endif |
478 else | |
479 let b:InPHPcode = 0 | |
480 let b:UserIsTypingComment = 0 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
481 let b:InPHPcode_tofind = s:PHP_startindenttag |
409 | 482 endif |
532 | 483 endif "!b:InPHPcode_checked }}} |
409 | 484 |
485 | |
532 | 486 " Test if we are indenting PHP code {{{ |
487 let lnum = prevnonblank(v:lnum - 1) | |
488 let last_line = getline(lnum) | |
5862 | 489 let endline= s:endline |
409 | 490 |
532 | 491 if b:InPHPcode_tofind!="" |
492 if cline =~? b:InPHPcode_tofind | |
493 let b:InPHPcode_tofind = "" | |
494 let b:UserIsTypingComment = 0 | |
6421 | 495 |
496 if b:InPHPcode == -1 | |
497 let b:InPHPcode = 1 | |
498 return -1 | |
499 end | |
500 | |
501 let b:InPHPcode = 1 | |
502 | |
532 | 503 if cline =~ '\*/' |
409 | 504 call cursor(v:lnum, 1) |
532 | 505 if cline !~ '^\*/' |
506 call search('\*/', 'W') | |
507 endif | |
557 | 508 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()') |
409 | 509 |
510 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
511 | |
532 | 512 let b:PHP_LastIndentedWasComment = 0 |
409 | 513 |
532 | 514 if cline =~ '^\s*\*/' |
515 return indent(lnum) + 1 | |
516 else | |
517 return indent(lnum) | |
7 | 518 endif |
532 | 519 |
520 elseif cline =~? '<script\>' | |
521 let b:InPHPcode_and_script = 1 | |
631 | 522 let b:GetLastRealCodeLNum_ADD = v:lnum |
532 | 523 endif |
7 | 524 endif |
532 | 525 endif |
409 | 526 |
6421 | 527 if 1 == b:InPHPcode |
409 | 528 |
1120 | 529 if !b:InPHPcode_and_script && last_line =~ '\%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP(lnum, '?>')=~"Delimiter" |
532 | 530 if cline !~? s:PHP_startindenttag |
531 let b:InPHPcode = 0 | |
532 let b:InPHPcode_tofind = s:PHP_startindenttag | |
533 elseif cline =~? '<script\>' | |
534 let b:InPHPcode_and_script = 1 | |
535 endif | |
409 | 536 |
6421 | 537 elseif last_line =~ '^[^''"`]\+[''"`]$' |
538 let b:InPHPcode = -1 | |
5862 | 539 let b:InPHPcode_tofind = substitute( last_line, '^.*\([''"`]\).*$', '^[^\1]*\1[;,]$', '') |
2442 | 540 elseif last_line =~? '<<<''\=\a\w*''\=$' |
532 | 541 let b:InPHPcode = 0 |
2442 | 542 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') |
532 | 543 |
544 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*' | |
545 let b:InPHPcode = 0 | |
546 let b:InPHPcode_tofind = '\*/' | |
409 | 547 |
532 | 548 elseif cline =~? '^\s*</script>' |
549 let b:InPHPcode = 0 | |
550 let b:InPHPcode_tofind = s:PHP_startindenttag | |
551 endif | |
552 endif " }}} | |
553 | |
856 | 554 |
6421 | 555 if 1 > b:InPHPcode && !b:InPHPcode_and_script |
532 | 556 return -1 |
557 endif | |
409 | 558 |
532 | 559 " Indent successive // or # comment the same way the first is {{{ |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
560 let addSpecial = 0 |
532 | 561 if cline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)' |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
562 let addSpecial = b:PHP_outdentSLComments |
532 | 563 if b:PHP_LastIndentedWasComment == 1 |
564 return indent(real_PHP_lastindented) | |
565 endif | |
566 let b:PHP_LastIndentedWasComment = 1 | |
567 else | |
568 let b:PHP_LastIndentedWasComment = 0 | |
569 endif " }}} | |
570 | |
571 " Indent multiline /* comments correctly {{{ | |
572 | |
573 if b:PHP_InsideMultilineComment || b:UserIsTypingComment | |
574 if cline =~ '^\s*\*\%(\/\)\@!' | |
575 if last_line =~ '^\s*/\*' | |
576 return indent(lnum) + 1 | |
577 else | |
578 return indent(lnum) | |
579 endif | |
580 else | |
581 let b:PHP_InsideMultilineComment = 0 | |
582 endif | |
583 endif | |
584 | |
6421 | 585 if !b:PHP_InsideMultilineComment && cline =~ '^\s*/\*\%(.*\*/\)\@!' |
631 | 586 if getline(v:lnum + 1) !~ '^\s*\*' |
587 return -1 | |
588 endif | |
532 | 589 let b:PHP_InsideMultilineComment = 1 |
590 endif " }}} | |
409 | 591 |
592 | |
532 | 593 " Things always indented at col 1 (PHP delimiter: <?, ?>, Heredoc end) {{{ |
2442 | 594 if cline =~# '^\s*<?' && cline !~ '?>' && b:PHP_outdentphpescape |
532 | 595 return 0 |
596 endif | |
409 | 597 |
2442 | 598 if cline =~ '^\s*?>' && cline !~# '<?' && b:PHP_outdentphpescape |
532 | 599 return 0 |
600 endif | |
409 | 601 |
5862 | 602 if cline =~? '^\s*\a\w*;$\|^\a\w*$\|^\s*[''"`][;,]' && cline !~? s:notPhpHereDoc |
532 | 603 return 0 |
604 endif " }}} | |
409 | 605 |
532 | 606 let s:level = 0 |
409 | 607 |
532 | 608 let lnum = GetLastRealCodeLNum(v:lnum - 1) |
631 | 609 |
532 | 610 let last_line = getline(lnum) |
611 let ind = indent(lnum) | |
409 | 612 |
532 | 613 if ind==0 && b:PHP_default_indenting |
614 let ind = b:PHP_default_indenting | |
615 endif | |
409 | 616 |
532 | 617 if lnum == 0 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
618 return b:PHP_default_indenting + addSpecial |
532 | 619 endif |
409 | 620 |
621 | |
532 | 622 if cline =~ '^\s*}\%(}}\)\@!' |
5862 | 623 let ind = indent(FindOpenBracket(v:lnum, 1)) |
532 | 624 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting |
625 return ind | |
626 endif | |
409 | 627 |
532 | 628 if cline =~ '^\s*\*/' |
629 call cursor(v:lnum, 1) | |
630 if cline !~ '^\*/' | |
631 call search('\*/', 'W') | |
632 endif | |
557 | 633 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()') |
532 | 634 |
635 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
409 | 636 |
532 | 637 if cline =~ '^\s*\*/' |
638 return indent(lnum) + 1 | |
639 else | |
640 return indent(lnum) | |
641 endif | |
642 endif | |
643 | |
644 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
645 if last_line =~ '[;}]'.endline && last_line !~ '^[)\]]' && last_line !~# s:defaultORcase |
532 | 646 if ind==b:PHP_default_indenting |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
647 return b:PHP_default_indenting + addSpecial |
532 | 648 elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
649 return b:PHP_CurrentIndentLevel + addSpecial |
532 | 650 endif |
651 endif | |
652 | |
653 let LastLineClosed = 0 | |
654 | |
5862 | 655 let terminated = s:terminated |
532 | 656 |
657 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline | |
409 | 658 |
532 | 659 if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>' |
660 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
661 return indent(FindTheIfOfAnElse(v:lnum, 1)) | |
2442 | 662 elseif cline =~# s:defaultORcase |
663 return FindTheSwitchIndent(v:lnum) + &sw * b:PHP_vintage_case_default_indent | |
1668 | 664 elseif cline =~ '^\s*)\=\s*{' |
532 | 665 let previous_line = last_line |
666 let last_line_num = lnum | |
667 | |
668 while last_line_num > 1 | |
669 | |
5862 | 670 if previous_line =~ terminated || previous_line =~ '^\s*\%(' . s:blockstart . '\)\|'. s:functionDecl . endline |
532 | 671 |
672 let ind = indent(last_line_num) | |
673 | |
674 if b:PHP_BracesAtCodeLevel | |
675 let ind = ind + &sw | |
676 endif | |
677 | |
856 | 678 return ind |
532 | 679 endif |
680 | |
5862 | 681 let last_line_num = GetLastRealCodeLNum(last_line_num - 1) |
532 | 682 let previous_line = getline(last_line_num) |
683 endwhile | |
409 | 684 |
1668 | 685 elseif last_line =~# unstated && cline !~ '^\s*);\='.endline |
6421 | 686 let ind = ind + &sw |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
687 return ind + addSpecial |
532 | 688 |
6421 | 689 elseif (ind != b:PHP_default_indenting || last_line =~ '^[)\]]' ) && last_line =~ terminated |
532 | 690 let previous_line = last_line |
691 let last_line_num = lnum | |
692 let LastLineClosed = 1 | |
693 | |
5862 | 694 let isSingleLineBlock = 0 |
532 | 695 while 1 |
6421 | 696 if ! isSingleLineBlock && previous_line =~ '^\s*}\|;\s*}'.endline |
532 | 697 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
698 call cursor(last_line_num, 1) |
5862 | 699 if previous_line !~ '^}' |
700 call search('}\|;\s*}'.endline, 'W') | |
701 end | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
702 let oldLastLine = last_line_num |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
703 let last_line_num = searchpair('{', '', '}', 'bW', 'Skippmatch()') |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
704 |
5862 | 705 if getline(last_line_num) =~ '^\s*{' |
532 | 706 let last_line_num = GetLastRealCodeLNum(last_line_num - 1) |
5862 | 707 elseif oldLastLine == last_line_num |
708 let isSingleLineBlock = 1 | |
709 continue | |
409 | 710 endif |
532 | 711 |
712 let previous_line = getline(last_line_num) | |
7 | 713 |
532 | 714 continue |
715 else | |
5862 | 716 let isSingleLineBlock = 0 |
409 | 717 |
532 | 718 if getline(last_line_num) =~# '^\s*else\%(if\)\=\>' |
719 let last_line_num = FindTheIfOfAnElse(last_line_num, 0) | |
720 continue | |
721 endif | |
409 | 722 |
723 | |
532 | 724 let last_match = last_line_num |
409 | 725 |
532 | 726 let one_ahead_indent = indent(last_line_num) |
727 let last_line_num = GetLastRealCodeLNum(last_line_num - 1) | |
728 let two_ahead_indent = indent(last_line_num) | |
729 let after_previous_line = previous_line | |
730 let previous_line = getline(last_line_num) | |
409 | 731 |
732 | |
2442 | 733 if previous_line =~# s:defaultORcase.'\|{'.endline |
532 | 734 break |
735 endif | |
409 | 736 |
532 | 737 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline |
738 break | |
7 | 739 endif |
409 | 740 |
856 | 741 if one_ahead_indent == two_ahead_indent || last_line_num < 1 |
1668 | 742 if previous_line =~# '\%(;\|^\s*}\)'.endline || last_line_num < 1 |
532 | 743 break |
744 endif | |
745 endif | |
746 endif | |
747 endwhile | |
748 | |
749 if indent(last_match) != ind | |
750 let ind = indent(last_match) | |
751 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
752 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
753 return ind + addSpecial |
532 | 754 endif |
755 endif | |
756 | |
6421 | 757 if (last_line !~ '^\s*}\%(}}\)\@!') |
758 let plinnum = GetLastRealCodeLNum(lnum - 1) | |
759 else | |
760 let plinnum = GetLastRealCodeLNum(FindOpenBracket(lnum, 1) - 1) | |
761 endif | |
762 | |
2442 | 763 let AntepenultimateLine = getline(plinnum) |
532 | 764 |
765 let last_line = substitute(last_line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','') | |
766 | |
767 | |
768 if ind == b:PHP_default_indenting | |
6421 | 769 if last_line =~ terminated && last_line !~# s:defaultORcase |
532 | 770 let LastLineClosed = 1 |
771 endif | |
772 endif | |
773 | |
774 if !LastLineClosed | |
775 | |
2034 | 776 |
6421 | 777 if last_line =~# '[{(\[]'.endline || last_line =~? '\h\w*\s*(.*,$' && AntepenultimateLine !~ '[,(\[]'.endline |
532 | 778 |
5862 | 779 let dontIndent = 0 |
6421 | 780 if last_line =~ '\S\+\s*{'.endline && last_line !~ '^\s*)\s*{'.endline && last_line !~ '^\s*\%(' . s:blockstart . '\)\|'. s:functionDecl . s:endline |
5862 | 781 let dontIndent = 1 |
782 endif | |
783 | |
784 if !dontIndent && (!b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{') | |
409 | 785 let ind = ind + &sw |
532 | 786 endif |
787 | |
2442 | 788 if b:PHP_BracesAtCodeLevel || b:PHP_vintage_case_default_indent == 1 |
532 | 789 let b:PHP_CurrentIndentLevel = ind |
1668 | 790 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
791 return ind + addSpecial |
532 | 792 endif |
793 | |
794 elseif last_line =~ '\S\+\s*),'.endline | |
795 call cursor(lnum, 1) | |
6421 | 796 call search('),'.endline, 'W') |
532 | 797 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()') |
798 if openedparent != lnum | |
799 let ind = indent(openedparent) | |
800 endif | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
801 |
1668 | 802 elseif last_line =~ '^\s*'.s:blockstart |
803 let ind = ind + &sw | |
804 | |
532 | 805 |
6421 | 806 elseif AntepenultimateLine =~ '{'.endline || AntepenultimateLine =~ terminated || AntepenultimateLine =~# s:defaultORcase |
532 | 807 let ind = ind + &sw |
409 | 808 endif |
7 | 809 |
532 | 810 endif |
409 | 811 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
812 if cline =~ '^\s*[)\]];\=' |
532 | 813 let ind = ind - &sw |
814 endif | |
815 | |
816 let b:PHP_CurrentIndentLevel = ind | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
817 return ind + addSpecial |
7 | 818 endfunction |