Mercurial > vim
annotate runtime/indent/php.vim @ 6007:ce284c205558 v7.4.344
updated for version 7.4.344
Problem: Unessecary initializations and other things related to
matchaddpos().
Solution: Code cleanup. (Alexey Radkov)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 25 Jun 2014 18:15:22 +0200 |
parents | 306caa30d83b |
children | 5d89d9b40499 |
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 |
5862 | 6 " Last Change: 2014 April 3rd |
7 " Version: 1.49 | |
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 |
51 let php_sync_method = 0 | |
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() | |
115 setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=?>,=<?,=*/ | |
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 |
131 let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\|die\|else\)' | |
132 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\>\)' | |
133 let s:functionDecl = '\<function\>\%(\s\+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\)\=\s*(.*' | |
409 | 134 let s:endline= '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$' |
5862 | 135 let s:terminated = '\%(\%(;\%(\s*\%(?>\|}\)\)\=\|<<<''\=\a\w*''\=$\|^\s*}\)'.s:endline.'\)\|^[^''"`]*[''"`]$' |
409 | 136 let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!' |
137 | |
138 | |
5862 | 139 |
140 let s:escapeDebugStops = 0 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
141 function! DebugPrintReturn(scriptLine) |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
142 |
5862 | 143 if ! s:escapeDebugStops |
144 echo "debug:" . a:scriptLine | |
145 let c = getchar() | |
146 if c == "\<Del>" | |
147 let s:escapeDebugStops = 1 | |
148 end | |
149 endif | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
150 |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
151 endfunction |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
152 |
409 | 153 function! GetLastRealCodeLNum(startline) " {{{ |
856 | 154 |
532 | 155 let lnum = a:startline |
856 | 156 |
631 | 157 if b:GetLastRealCodeLNum_ADD && b:GetLastRealCodeLNum_ADD == lnum + 1 |
158 let lnum = b:GetLastRealCodeLNum_ADD | |
159 endif | |
856 | 160 |
532 | 161 let old_lnum = lnum |
409 | 162 |
532 | 163 while lnum > 1 |
164 let lnum = prevnonblank(lnum) | |
165 let lastline = getline(lnum) | |
409 | 166 |
532 | 167 if b:InPHPcode_and_script && lastline =~ '?>\s*$' |
168 let lnum = lnum - 1 | |
169 elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$' | |
170 let lnum = lnum - 1 | |
171 elseif lastline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)' | |
172 let lnum = lnum - 1 | |
173 elseif lastline =~ '\*/\s*$' | |
174 call cursor(lnum, 1) | |
175 if lastline !~ '^\*/' | |
176 call search('\*/', 'W') | |
177 endif | |
557 | 178 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()') |
409 | 179 |
532 | 180 let lastline = getline(lnum) |
181 if lastline =~ '^\s*/\*' | |
182 let lnum = lnum - 1 | |
183 else | |
184 break | |
185 endif | |
409 | 186 |
187 | |
532 | 188 elseif lastline =~? '\%(//\s*\|?>.*\)\@<!<?\%(php\)\=\s*$\|^\s*<script\>' |
189 | |
190 while lastline !~ '\(<?.*\)\@<!?>' && lnum > 1 | |
191 let lnum = lnum - 1 | |
192 let lastline = getline(lnum) | |
193 endwhile | |
194 if lastline =~ '^\s*?>' | |
195 let lnum = lnum - 1 | |
196 else | |
197 break | |
198 endif | |
199 | |
409 | 200 |
2442 | 201 elseif lastline =~? '^\a\w*;\=$' && lastline !~? s:notPhpHereDoc |
202 let tofind=substitute( lastline, '\(\a\w*\);\=', '<<<''\\=\1''\\=$', '') | |
532 | 203 while getline(lnum) !~? tofind && lnum > 1 |
204 let lnum = lnum - 1 | |
205 endwhile | |
5862 | 206 elseif lastline =~ '^[^''"`]*[''"`][;,]'.s:endline |
207 let tofind=substitute( lastline, '^.*\([''"`]\)[;,].*$', '^[^\1]\\+[\1]$', '') | |
208 while getline(lnum) !~? tofind && lnum > 1 | |
209 let lnum = lnum - 1 | |
210 endwhile | |
532 | 211 else |
856 | 212 break |
409 | 213 endif |
532 | 214 endwhile |
215 | |
2442 | 216 if lnum==1 && getline(lnum) !~ '<?' |
532 | 217 let lnum=0 |
218 endif | |
219 | |
220 if b:InPHPcode_and_script && !b:InPHPcode | |
221 let b:InPHPcode_and_script = 0 | |
222 endif | |
1120 | 223 |
532 | 224 return lnum |
225 endfunction " }}} | |
409 | 226 |
557 | 227 function! Skippmatch2() |
228 | |
229 let line = getline(".") | |
230 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
231 if line =~ "\\([\"']\\).*/\\*.*\\1" || line =~ '\%(//\|#\).*/\*' |
5862 | 232 return 1 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
233 else |
5862 | 234 return 0 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
235 endif |
557 | 236 endfun |
237 | |
2442 | 238 function! Skippmatch() " {{{ |
532 | 239 let synname = synIDattr(synID(line("."), col("."), 0), "name") |
1668 | 240 if synname == "Delimiter" || synname == "phpRegionDelimiter" || synname =~# "^phpParent" || synname == "phpArrayParens" || synname =~# '^php\%(Block\|Brace\)' || synname == "javaScriptBraces" || synname =~# "^phpComment" && b:UserIsTypingComment |
532 | 241 return 0 |
242 else | |
243 return 1 | |
244 endif | |
245 endfun " }}} | |
409 | 246 |
5862 | 247 function! FindOpenBracket(lnum, blockStarter) " {{{ |
532 | 248 call cursor(a:lnum, 1) |
5862 | 249 let line = searchpair('{', '', '}', 'bW', 'Skippmatch()') |
250 | |
251 if a:blockStarter == 1 | |
252 while line > 1 | |
253 let linec = getline(line) | |
254 | |
255 if linec =~ s:terminated || linec =~ '^\s*\%(' . s:blockstart . '\)\|'. s:functionDecl . s:endline | |
256 break | |
257 endif | |
258 | |
259 let line = GetLastRealCodeLNum(line - 1) | |
260 endwhile | |
261 endif | |
262 | |
263 return line | |
532 | 264 endfun " }}} |
409 | 265 |
266 function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{ | |
532 | 267 |
268 if getline(a:lnum) =~# '^\s*}\s*else\%(if\)\=\>' | |
269 let beforeelse = a:lnum | |
270 else | |
271 let beforeelse = GetLastRealCodeLNum(a:lnum - 1) | |
272 endif | |
409 | 273 |
532 | 274 if !s:level |
275 let s:iftoskip = 0 | |
276 endif | |
277 | |
278 if getline(beforeelse) =~# '^\s*\%(}\s*\)\=else\%(\s*if\)\@!\>' | |
279 let s:iftoskip = s:iftoskip + 1 | |
280 endif | |
281 | |
282 if getline(beforeelse) =~ '^\s*}' | |
5862 | 283 let beforeelse = FindOpenBracket(beforeelse, 0) |
532 | 284 |
285 if getline(beforeelse) =~ '^\s*{' | |
286 let beforeelse = GetLastRealCodeLNum(beforeelse - 1) | |
409 | 287 endif |
532 | 288 endif |
409 | 289 |
290 | |
532 | 291 if !s:iftoskip && a:StopAfterFirstPrevElse && getline(beforeelse) =~# '^\s*\%([}]\s*\)\=else\%(if\)\=\>' |
292 return beforeelse | |
293 endif | |
294 | |
295 if getline(beforeelse) !~# '^\s*if\>' && beforeelse>1 || s:iftoskip && beforeelse>1 | |
296 | |
2442 | 297 if s:iftoskip && getline(beforeelse) =~# '^\s*if\>' |
532 | 298 let s:iftoskip = s:iftoskip - 1 |
409 | 299 endif |
300 | |
532 | 301 let s:level = s:level + 1 |
302 let beforeelse = FindTheIfOfAnElse(beforeelse, a:StopAfterFirstPrevElse) | |
303 endif | |
409 | 304 |
532 | 305 return beforeelse |
409 | 306 |
532 | 307 endfunction " }}} |
409 | 308 |
2442 | 309 let s:defaultORcase = '^\s*\%(default\|case\).*:' |
310 | |
311 function! FindTheSwitchIndent (lnum) " {{{ | |
312 | |
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*[''"`]' | |
356 return "" | |
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() |
2442 | 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 |
532 | 421 let b:PHP_lastindented = v:lnum |
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 | |
433 let b:PHP_lastindented = v:lnum | |
434 endif | |
409 | 435 |
436 | |
532 | 437 if !b:InPHPcode_checked " {{{ One time check |
438 let b:InPHPcode_checked = 1 | |
409 | 439 |
631 | 440 let synname = "" |
441 if cline !~ '<?.*?>' | |
442 let synname = IslinePHP (prevnonblank(v:lnum), "") | |
443 endif | |
409 | 444 |
532 | 445 if synname!="" |
1120 | 446 if synname != "phpHereDoc" && synname != "phpHereDocDelimiter" |
532 | 447 let b:InPHPcode = 1 |
448 let b:InPHPcode_tofind = "" | |
409 | 449 |
1668 | 450 if synname =~# "^phpComment" |
532 | 451 let b:UserIsTypingComment = 1 |
452 else | |
453 let b:UserIsTypingComment = 0 | |
409 | 454 endif |
455 | |
532 | 456 if synname =~? '^javaScript' |
457 let b:InPHPcode_and_script = 1 | |
458 endif | |
409 | 459 |
532 | 460 else |
461 let b:InPHPcode = 0 | |
462 let b:UserIsTypingComment = 0 | |
409 | 463 |
532 | 464 let lnum = v:lnum - 1 |
1668 | 465 while getline(lnum) !~? '<<<''\=\a\w*''\=$' && lnum > 1 |
532 | 466 let lnum = lnum - 1 |
467 endwhile | |
409 | 468 |
2442 | 469 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') |
532 | 470 endif |
471 else | |
472 let b:InPHPcode = 0 | |
473 let b:UserIsTypingComment = 0 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
474 let b:InPHPcode_tofind = s:PHP_startindenttag |
409 | 475 endif |
532 | 476 endif "!b:InPHPcode_checked }}} |
409 | 477 |
478 | |
532 | 479 " Test if we are indenting PHP code {{{ |
480 let lnum = prevnonblank(v:lnum - 1) | |
481 let last_line = getline(lnum) | |
5862 | 482 let endline= s:endline |
409 | 483 |
532 | 484 if b:InPHPcode_tofind!="" |
485 if cline =~? b:InPHPcode_tofind | |
2442 | 486 let b:InPHPcode = 1 |
532 | 487 let b:InPHPcode_tofind = "" |
488 let b:UserIsTypingComment = 0 | |
489 if cline =~ '\*/' | |
409 | 490 call cursor(v:lnum, 1) |
532 | 491 if cline !~ '^\*/' |
492 call search('\*/', 'W') | |
493 endif | |
557 | 494 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()') |
409 | 495 |
496 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
497 | |
532 | 498 let b:PHP_LastIndentedWasComment = 0 |
409 | 499 |
532 | 500 if cline =~ '^\s*\*/' |
501 return indent(lnum) + 1 | |
502 else | |
503 return indent(lnum) | |
7 | 504 endif |
532 | 505 |
506 elseif cline =~? '<script\>' | |
507 let b:InPHPcode_and_script = 1 | |
631 | 508 let b:GetLastRealCodeLNum_ADD = v:lnum |
532 | 509 endif |
7 | 510 endif |
532 | 511 endif |
409 | 512 |
532 | 513 if b:InPHPcode |
409 | 514 |
1120 | 515 if !b:InPHPcode_and_script && last_line =~ '\%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP(lnum, '?>')=~"Delimiter" |
532 | 516 if cline !~? s:PHP_startindenttag |
517 let b:InPHPcode = 0 | |
518 let b:InPHPcode_tofind = s:PHP_startindenttag | |
519 elseif cline =~? '<script\>' | |
520 let b:InPHPcode_and_script = 1 | |
521 endif | |
409 | 522 |
5862 | 523 elseif last_line =~ '^[^''"`]\+[''"`]$' " a string identifier with nothing after it and no other string identifier before |
524 let b:InPHPcode = 0 | |
525 let b:InPHPcode_tofind = substitute( last_line, '^.*\([''"`]\).*$', '^[^\1]*\1[;,]$', '') | |
2442 | 526 elseif last_line =~? '<<<''\=\a\w*''\=$' |
532 | 527 let b:InPHPcode = 0 |
2442 | 528 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') |
532 | 529 |
530 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*' | |
531 let b:InPHPcode = 0 | |
532 let b:InPHPcode_tofind = '\*/' | |
409 | 533 |
532 | 534 elseif cline =~? '^\s*</script>' |
535 let b:InPHPcode = 0 | |
536 let b:InPHPcode_tofind = s:PHP_startindenttag | |
537 endif | |
538 endif " }}} | |
539 | |
856 | 540 |
532 | 541 if !b:InPHPcode && !b:InPHPcode_and_script |
542 return -1 | |
543 endif | |
409 | 544 |
532 | 545 " 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
|
546 let addSpecial = 0 |
532 | 547 if cline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)' |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
548 let addSpecial = b:PHP_outdentSLComments |
532 | 549 if b:PHP_LastIndentedWasComment == 1 |
550 return indent(real_PHP_lastindented) | |
551 endif | |
552 let b:PHP_LastIndentedWasComment = 1 | |
553 else | |
554 let b:PHP_LastIndentedWasComment = 0 | |
555 endif " }}} | |
556 | |
557 " Indent multiline /* comments correctly {{{ | |
558 | |
559 if b:PHP_InsideMultilineComment || b:UserIsTypingComment | |
560 if cline =~ '^\s*\*\%(\/\)\@!' | |
561 if last_line =~ '^\s*/\*' | |
562 return indent(lnum) + 1 | |
563 else | |
564 return indent(lnum) | |
565 endif | |
566 else | |
567 let b:PHP_InsideMultilineComment = 0 | |
568 endif | |
569 endif | |
570 | |
631 | 571 if !b:PHP_InsideMultilineComment && cline =~ '^\s*/\*' && cline !~ '\*/\s*$' |
572 if getline(v:lnum + 1) !~ '^\s*\*' | |
573 return -1 | |
574 endif | |
532 | 575 let b:PHP_InsideMultilineComment = 1 |
576 endif " }}} | |
409 | 577 |
578 | |
532 | 579 " Things always indented at col 1 (PHP delimiter: <?, ?>, Heredoc end) {{{ |
2442 | 580 if cline =~# '^\s*<?' && cline !~ '?>' && b:PHP_outdentphpescape |
532 | 581 return 0 |
582 endif | |
409 | 583 |
2442 | 584 if cline =~ '^\s*?>' && cline !~# '<?' && b:PHP_outdentphpescape |
532 | 585 return 0 |
586 endif | |
409 | 587 |
5862 | 588 if cline =~? '^\s*\a\w*;$\|^\a\w*$\|^\s*[''"`][;,]' && cline !~? s:notPhpHereDoc |
532 | 589 return 0 |
590 endif " }}} | |
409 | 591 |
532 | 592 let s:level = 0 |
409 | 593 |
532 | 594 let lnum = GetLastRealCodeLNum(v:lnum - 1) |
631 | 595 |
532 | 596 let last_line = getline(lnum) |
597 let ind = indent(lnum) | |
409 | 598 |
532 | 599 if ind==0 && b:PHP_default_indenting |
600 let ind = b:PHP_default_indenting | |
601 endif | |
409 | 602 |
532 | 603 if lnum == 0 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
604 return b:PHP_default_indenting + addSpecial |
532 | 605 endif |
409 | 606 |
607 | |
532 | 608 if cline =~ '^\s*}\%(}}\)\@!' |
5862 | 609 let ind = indent(FindOpenBracket(v:lnum, 1)) |
532 | 610 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting |
611 return ind | |
612 endif | |
409 | 613 |
532 | 614 if cline =~ '^\s*\*/' |
615 call cursor(v:lnum, 1) | |
616 if cline !~ '^\*/' | |
617 call search('\*/', 'W') | |
618 endif | |
557 | 619 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()') |
532 | 620 |
621 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
409 | 622 |
532 | 623 if cline =~ '^\s*\*/' |
624 return indent(lnum) + 1 | |
625 else | |
626 return indent(lnum) | |
627 endif | |
628 endif | |
629 | |
630 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
631 if last_line =~ '[;}]'.endline && last_line !~ '^[)\]]' && last_line !~# s:defaultORcase |
532 | 632 if ind==b:PHP_default_indenting |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
633 return b:PHP_default_indenting + addSpecial |
532 | 634 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
|
635 return b:PHP_CurrentIndentLevel + addSpecial |
532 | 636 endif |
637 endif | |
638 | |
639 let LastLineClosed = 0 | |
640 | |
5862 | 641 let terminated = s:terminated |
532 | 642 |
643 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline | |
409 | 644 |
532 | 645 if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>' |
646 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
647 return indent(FindTheIfOfAnElse(v:lnum, 1)) | |
2442 | 648 elseif cline =~# s:defaultORcase |
649 return FindTheSwitchIndent(v:lnum) + &sw * b:PHP_vintage_case_default_indent | |
1668 | 650 elseif cline =~ '^\s*)\=\s*{' |
532 | 651 let previous_line = last_line |
652 let last_line_num = lnum | |
653 | |
654 while last_line_num > 1 | |
655 | |
5862 | 656 if previous_line =~ terminated || previous_line =~ '^\s*\%(' . s:blockstart . '\)\|'. s:functionDecl . endline |
532 | 657 |
658 let ind = indent(last_line_num) | |
659 | |
660 if b:PHP_BracesAtCodeLevel | |
661 let ind = ind + &sw | |
662 endif | |
663 | |
856 | 664 return ind |
532 | 665 endif |
666 | |
5862 | 667 let last_line_num = GetLastRealCodeLNum(last_line_num - 1) |
532 | 668 let previous_line = getline(last_line_num) |
669 endwhile | |
409 | 670 |
1668 | 671 elseif last_line =~# unstated && cline !~ '^\s*);\='.endline |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
672 let ind = ind + &sw " we indent one level further when the preceding line is not stated |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
673 return ind + addSpecial |
532 | 674 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
675 elseif (ind != b:PHP_default_indenting || last_line =~ '^[)\]]' ) && last_line =~ terminated " Added || last_line =~ '^)' on 2007-12-30 (array indenting problem broke other things) |
532 | 676 let previous_line = last_line |
677 let last_line_num = lnum | |
678 let LastLineClosed = 1 | |
679 | |
5862 | 680 let isSingleLineBlock = 0 |
532 | 681 while 1 |
5862 | 682 if ! isSingleLineBlock && previous_line =~ '^\s*}\|;\s*}'.endline " XXX |
532 | 683 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
684 call cursor(last_line_num, 1) |
5862 | 685 if previous_line !~ '^}' |
686 call search('}\|;\s*}'.endline, 'W') | |
687 end | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
688 let oldLastLine = last_line_num |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
689 let last_line_num = searchpair('{', '', '}', 'bW', 'Skippmatch()') |
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
690 |
5862 | 691 if getline(last_line_num) =~ '^\s*{' |
532 | 692 let last_line_num = GetLastRealCodeLNum(last_line_num - 1) |
5862 | 693 elseif oldLastLine == last_line_num |
694 let isSingleLineBlock = 1 | |
695 continue | |
409 | 696 endif |
532 | 697 |
698 let previous_line = getline(last_line_num) | |
7 | 699 |
532 | 700 continue |
701 else | |
5862 | 702 let isSingleLineBlock = 0 |
409 | 703 |
532 | 704 if getline(last_line_num) =~# '^\s*else\%(if\)\=\>' |
705 let last_line_num = FindTheIfOfAnElse(last_line_num, 0) | |
706 continue | |
707 endif | |
409 | 708 |
709 | |
532 | 710 let last_match = last_line_num |
409 | 711 |
532 | 712 let one_ahead_indent = indent(last_line_num) |
713 let last_line_num = GetLastRealCodeLNum(last_line_num - 1) | |
714 let two_ahead_indent = indent(last_line_num) | |
715 let after_previous_line = previous_line | |
716 let previous_line = getline(last_line_num) | |
409 | 717 |
718 | |
2442 | 719 if previous_line =~# s:defaultORcase.'\|{'.endline |
532 | 720 break |
721 endif | |
409 | 722 |
532 | 723 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline |
724 break | |
7 | 725 endif |
409 | 726 |
856 | 727 if one_ahead_indent == two_ahead_indent || last_line_num < 1 |
1668 | 728 if previous_line =~# '\%(;\|^\s*}\)'.endline || last_line_num < 1 |
532 | 729 break |
730 endif | |
731 endif | |
732 endif | |
733 endwhile | |
734 | |
735 if indent(last_match) != ind | |
736 let ind = indent(last_match) | |
737 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting | |
738 | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
739 return ind + addSpecial |
532 | 740 endif |
741 endif | |
742 | |
743 let plinnum = GetLastRealCodeLNum(lnum - 1) | |
2442 | 744 let AntepenultimateLine = getline(plinnum) |
532 | 745 |
746 let last_line = substitute(last_line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','') | |
747 | |
748 | |
749 if ind == b:PHP_default_indenting | |
750 if last_line =~ terminated | |
751 let LastLineClosed = 1 | |
752 endif | |
753 endif | |
754 | |
755 if !LastLineClosed | |
756 | |
2034 | 757 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
758 if last_line =~# '[{(\[]'.endline || last_line =~? '\h\w*\s*(.*,$' && AntepenultimateLine !~ '[,(]'.endline |
532 | 759 |
5862 | 760 let dontIndent = 0 |
761 if last_line =~ '\S\+\s*{'.endline && last_line !~ '^\s*\%(' . s:blockstart . '\)\|'. s:functionDecl . s:endline | |
762 let dontIndent = 1 | |
763 endif | |
764 | |
765 if !dontIndent && (!b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{') | |
409 | 766 let ind = ind + &sw |
532 | 767 endif |
768 | |
2442 | 769 if b:PHP_BracesAtCodeLevel || b:PHP_vintage_case_default_indent == 1 |
532 | 770 let b:PHP_CurrentIndentLevel = ind |
1668 | 771 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
772 return ind + addSpecial |
532 | 773 endif |
774 | |
775 elseif last_line =~ '\S\+\s*),'.endline | |
776 call cursor(lnum, 1) | |
5862 | 777 call search('),'.endline, 'W') " line never begins with ) so no need for 'c' flag |
532 | 778 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()') |
779 if openedparent != lnum | |
780 let ind = indent(openedparent) | |
781 endif | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
782 |
1668 | 783 elseif last_line =~ '^\s*'.s:blockstart |
784 let ind = ind + &sw | |
785 | |
532 | 786 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
787 elseif AntepenultimateLine =~ '\%(;\%(\s*\%(?>\|}\)\)\=\|<<<''\=\a\w*''\=$\|^\s*}\|{\)'.endline . '\|' . s:defaultORcase |
532 | 788 let ind = ind + &sw |
409 | 789 endif |
7 | 790 |
532 | 791 endif |
409 | 792 |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
793 if cline =~ '^\s*[)\]];\=' |
532 | 794 let ind = ind - &sw |
795 endif | |
796 | |
797 let b:PHP_CurrentIndentLevel = ind | |
4502
605c9ce57ec3
Updated runtime files, language files and translations.
Bram Moolenaar <bram@vim.org>
parents:
2442
diff
changeset
|
798 return ind + addSpecial |
7 | 799 endfunction |