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
|
1668
|
5 " Last Change: 2008 June 7th
|
631
|
6 " Newsletter: http://www.2072productions.com/?to=php-indent-for-vim-newsletter.php
|
1668
|
7 " Version: 1.28
|
557
|
8 "
|
1668
|
9 " If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr
|
|
10 " with an example of code that breaks the algorithm.
|
|
11 "
|
|
12 " ---> The change log and all the comments have been removed from this file.
|
409
|
13 "
|
557
|
14 " For a complete change log and fully commented code, download the script on
|
|
15 " 2072productions.com at the URI provided above.
|
856
|
16 "
|
557
|
17 " If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr
|
631
|
18 " with an example of code that breaks the algorithm.
|
409
|
19 "
|
|
20 "
|
557
|
21 " Thanks a lot for using this script.
|
409
|
22 "
|
|
23 "
|
|
24 " NOTE: This script must be used with PHP syntax ON and with the php syntax
|
1668
|
25 " script by Lutz Eymers (http://www.isp.de/data/php.vim ) or with the
|
|
26 " script by Peter Hodge (http://www.vim.org/scripts/script.php?script_id=1571 )
|
|
27 " the later is bunbdled by default with vim 7.
|
409
|
28 "
|
|
29 "
|
1668
|
30 " In the case you have syntax errors in your script such as HereDoc end
|
|
31 " identifiers not at col 1 you'll have to indent your file 2 times (This
|
|
32 " script will automatically put HereDoc end identifiers at col 1 if
|
|
33 " they are followed by a ';').
|
856
|
34 "
|
557
|
35 "
|
409
|
36 " NOTE: If you are editing file in Unix file format and that (by accident)
|
|
37 " there are '\r' before new lines, this script won't be able to proceed
|
|
38 " correctly and will make many mistakes because it won't be able to match
|
|
39 " '\s*$' correctly.
|
|
40 " So you have to remove those useless characters first with a command like:
|
7
|
41 "
|
409
|
42 " :%s /\r$//g
|
|
43 "
|
|
44 " or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will
|
|
45 " silently remove them when VIM load this script (at each bufread).
|
1668
|
46 "
|
|
47 "
|
557
|
48 " Options: PHP_autoformatcomment = 0 to not enable autoformating of comment by
|
|
49 " default, if set to 0, this script will let the 'formatoptions' setting intact.
|
856
|
50 "
|
409
|
51 " Options: PHP_default_indenting = # of sw (default is 0), # of sw will be
|
557
|
52 " added to the indent of each line of PHP code.
|
409
|
53 "
|
|
54 " Options: PHP_removeCRwhenUnix = 1 to make the script automatically remove CR
|
557
|
55 " at end of lines (by default this option is unset), NOTE that you
|
|
56 " MUST remove CR when the fileformat is UNIX else the indentation
|
1668
|
57 " won't be correct!
|
7
|
58 "
|
409
|
59 " Options: PHP_BracesAtCodeLevel = 1 to indent the '{' and '}' at the same
|
557
|
60 " level than the code they contain.
|
|
61 " Exemple:
|
|
62 " Instead of:
|
|
63 " if ($foo)
|
409
|
64 " {
|
557
|
65 " foo();
|
409
|
66 " }
|
|
67 "
|
557
|
68 " You will write:
|
|
69 " if ($foo)
|
|
70 " {
|
|
71 " foo();
|
|
72 " }
|
|
73 "
|
|
74 " NOTE: The script will be a bit slower if you use this option because
|
|
75 " some optimizations won't be available.
|
1668
|
76 "
|
|
77 " Options: PHP_vintage_case_default_indent = 1 (defaults to 0) to add a meaningless indent
|
|
78 " befaore 'case:' and 'default":' statement in switch block
|
|
79 "
|
557
|
80
|
7
|
81 if exists("b:did_indent")
|
532
|
82 finish
|
7
|
83 endif
|
|
84 let b:did_indent = 1
|
|
85
|
409
|
86
|
|
87 let php_sync_method = 0
|
|
88
|
|
89
|
|
90 if exists("PHP_default_indenting")
|
532
|
91 let b:PHP_default_indenting = PHP_default_indenting * &sw
|
409
|
92 else
|
532
|
93 let b:PHP_default_indenting = 0
|
409
|
94 endif
|
|
95
|
|
96 if exists("PHP_BracesAtCodeLevel")
|
532
|
97 let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel
|
409
|
98 else
|
532
|
99 let b:PHP_BracesAtCodeLevel = 0
|
409
|
100 endif
|
|
101
|
1668
|
102
|
557
|
103 if exists("PHP_autoformatcomment")
|
|
104 let b:PHP_autoformatcomment = PHP_autoformatcomment
|
|
105 else
|
|
106 let b:PHP_autoformatcomment = 1
|
|
107 endif
|
7
|
108
|
1668
|
109 if exists("PHP_vintage_case_default_indent")
|
|
110 let b:PHP_vintage_case_default_indent = PHP_vintage_case_default_indent
|
|
111 else
|
|
112 let b:PHP_vintage_case_default_indent = 0
|
|
113 endif
|
|
114
|
|
115
|
|
116
|
409
|
117 let b:PHP_lastindented = 0
|
|
118 let b:PHP_indentbeforelast = 0
|
|
119 let b:PHP_indentinghuge = 0
|
|
120 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
121 let b:PHP_LastIndentedWasComment = 0
|
|
122 let b:PHP_InsideMultilineComment = 0
|
|
123 let b:InPHPcode = 0
|
|
124 let b:InPHPcode_checked = 0
|
|
125 let b:InPHPcode_and_script = 0
|
|
126 let b:InPHPcode_tofind = ""
|
|
127 let b:PHP_oldchangetick = b:changedtick
|
|
128 let b:UserIsTypingComment = 0
|
|
129 let b:optionsset = 0
|
|
130
|
|
131 setlocal nosmartindent
|
856
|
132 setlocal noautoindent
|
409
|
133 setlocal nocindent
|
532
|
134 setlocal nolisp
|
409
|
135
|
|
136 setlocal indentexpr=GetPhpIndent()
|
|
137 setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=?>,=<?,=*/
|
|
138
|
|
139
|
532
|
140
|
|
141 let s:searchpairflags = 'bWr'
|
409
|
142
|
|
143 if &fileformat == "unix" && exists("PHP_removeCRwhenUnix") && PHP_removeCRwhenUnix
|
532
|
144 silent! %s/\r$//g
|
409
|
145 endif
|
|
146
|
7
|
147 if exists("*GetPhpIndent")
|
631
|
148 finish " XXX
|
7
|
149 endif
|
|
150
|
409
|
151 let s:endline= '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$'
|
|
152 let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!'
|
1120
|
153 "setlocal debug=msg " XXX
|
409
|
154
|
|
155
|
|
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 let old_lnum = lnum
|
409
|
165
|
532
|
166 while lnum > 1
|
|
167 let lnum = prevnonblank(lnum)
|
|
168 let lastline = getline(lnum)
|
409
|
169
|
532
|
170 if b:InPHPcode_and_script && lastline =~ '?>\s*$'
|
|
171 let lnum = lnum - 1
|
|
172 elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$'
|
|
173 let lnum = lnum - 1
|
|
174 elseif lastline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
|
|
175 let lnum = lnum - 1
|
|
176 elseif lastline =~ '\*/\s*$'
|
|
177 call cursor(lnum, 1)
|
|
178 if lastline !~ '^\*/'
|
|
179 call search('\*/', 'W')
|
|
180 endif
|
557
|
181 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
|
409
|
182
|
532
|
183 let lastline = getline(lnum)
|
|
184 if lastline =~ '^\s*/\*'
|
|
185 let lnum = lnum - 1
|
|
186 else
|
|
187 break
|
|
188 endif
|
409
|
189
|
|
190
|
532
|
191 elseif lastline =~? '\%(//\s*\|?>.*\)\@<!<?\%(php\)\=\s*$\|^\s*<script\>'
|
|
192
|
|
193 while lastline !~ '\(<?.*\)\@<!?>' && lnum > 1
|
|
194 let lnum = lnum - 1
|
|
195 let lastline = getline(lnum)
|
|
196 endwhile
|
|
197 if lastline =~ '^\s*?>'
|
|
198 let lnum = lnum - 1
|
|
199 else
|
|
200 break
|
|
201 endif
|
|
202
|
409
|
203
|
1668
|
204 elseif lastline =~? '^\a\w*;\=$' && lastline !~? s:notPhpHereDoc " XXX 0607
|
|
205 let tofind=substitute( lastline, '\(\a\w*\);\=', '<<<''\\=\1''\\=$', '') " XXX 0607
|
532
|
206 while getline(lnum) !~? tofind && lnum > 1
|
|
207 let lnum = lnum - 1
|
|
208 endwhile
|
|
209 else
|
856
|
210 break
|
409
|
211 endif
|
532
|
212 endwhile
|
|
213
|
|
214 if lnum==1 && getline(lnum)!~ '<?'
|
|
215 let lnum=0
|
|
216 endif
|
|
217
|
|
218 if b:InPHPcode_and_script && !b:InPHPcode
|
|
219 let b:InPHPcode_and_script = 0
|
|
220 endif
|
1120
|
221
|
|
222
|
|
223
|
532
|
224 return lnum
|
|
225 endfunction " }}}
|
409
|
226
|
557
|
227 function! Skippmatch2()
|
|
228
|
|
229 let line = getline(".")
|
|
230
|
1668
|
231 if line =~ '\%(".*\)\@<=/\*\%(.*"\)\@=' || line =~ '\%(\%(//\|#\).*\)\@<=/\*'
|
557
|
232 return 1
|
|
233 else
|
|
234 return 0
|
|
235 endif
|
|
236 endfun
|
|
237
|
409
|
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
|
|
247 function! FindOpenBracket(lnum) " {{{
|
532
|
248 call cursor(a:lnum, 1)
|
|
249 return searchpair('{', '', '}', 'bW', 'Skippmatch()')
|
|
250 endfun " }}}
|
409
|
251
|
|
252 function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{
|
532
|
253
|
|
254 if getline(a:lnum) =~# '^\s*}\s*else\%(if\)\=\>'
|
|
255 let beforeelse = a:lnum
|
|
256 else
|
|
257 let beforeelse = GetLastRealCodeLNum(a:lnum - 1)
|
|
258 endif
|
409
|
259
|
532
|
260 if !s:level
|
|
261 let s:iftoskip = 0
|
|
262 endif
|
|
263
|
|
264 if getline(beforeelse) =~# '^\s*\%(}\s*\)\=else\%(\s*if\)\@!\>'
|
|
265 let s:iftoskip = s:iftoskip + 1
|
|
266 endif
|
|
267
|
|
268 if getline(beforeelse) =~ '^\s*}'
|
|
269 let beforeelse = FindOpenBracket(beforeelse)
|
|
270
|
|
271 if getline(beforeelse) =~ '^\s*{'
|
|
272 let beforeelse = GetLastRealCodeLNum(beforeelse - 1)
|
409
|
273 endif
|
532
|
274 endif
|
409
|
275
|
|
276
|
532
|
277 if !s:iftoskip && a:StopAfterFirstPrevElse && getline(beforeelse) =~# '^\s*\%([}]\s*\)\=else\%(if\)\=\>'
|
|
278 return beforeelse
|
|
279 endif
|
|
280
|
|
281 if getline(beforeelse) !~# '^\s*if\>' && beforeelse>1 || s:iftoskip && beforeelse>1
|
|
282
|
|
283 if s:iftoskip && getline(beforeelse) =~# '^\s*if\>'
|
|
284 let s:iftoskip = s:iftoskip - 1
|
409
|
285 endif
|
|
286
|
532
|
287 let s:level = s:level + 1
|
|
288 let beforeelse = FindTheIfOfAnElse(beforeelse, a:StopAfterFirstPrevElse)
|
|
289 endif
|
409
|
290
|
532
|
291 return beforeelse
|
409
|
292
|
532
|
293 endfunction " }}}
|
409
|
294
|
|
295 function! IslinePHP (lnum, tofind) " {{{
|
532
|
296 let cline = getline(a:lnum)
|
409
|
297
|
532
|
298 if a:tofind==""
|
557
|
299 let tofind = "^\\s*[\"']*\\s*\\zs\\S"
|
532
|
300 else
|
|
301 let tofind = a:tofind
|
|
302 endif
|
409
|
303
|
532
|
304 let tofind = tofind . '\c'
|
409
|
305
|
532
|
306 let coltotest = match (cline, tofind) + 1
|
|
307
|
|
308 let synname = synIDattr(synID(a:lnum, coltotest, 0), "name")
|
409
|
309
|
532
|
310 if synname =~ '^php' || synname=="Delimiter" || synname =~? '^javaScript'
|
|
311 return synname
|
|
312 else
|
|
313 return ""
|
|
314 endif
|
|
315 endfunction " }}}
|
409
|
316
|
1668
|
317 let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\|else\)'
|
|
318 let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|else\>\|while\>\|switch\>\|for\%(each\)\=\>\|declare\>\|class\>\|interface\>\|abstract\>\|try\>\|catch\>\)'
|
409
|
319
|
|
320 let s:autorestoptions = 0
|
|
321 if ! s:autorestoptions
|
557
|
322 au BufWinEnter,Syntax *.php,*.php3,*.php4,*.php5 call ResetOptions()
|
532
|
323 let s:autorestoptions = 1
|
7
|
324 endif
|
|
325
|
409
|
326 function! ResetOptions()
|
532
|
327 if ! b:optionsset
|
557
|
328 if b:PHP_autoformatcomment
|
|
329
|
|
330 setlocal comments=s1:/*,mb:*,ex:*/,://,:#
|
856
|
331
|
1668
|
332 setlocal formatoptions-=t
|
557
|
333 setlocal formatoptions+=q
|
|
334 setlocal formatoptions+=r
|
|
335 setlocal formatoptions+=o
|
1668
|
336 setlocal formatoptions+=w
|
557
|
337 setlocal formatoptions+=c
|
|
338 setlocal formatoptions+=b
|
|
339 endif
|
532
|
340 let b:optionsset = 1
|
|
341 endif
|
409
|
342 endfunc
|
|
343
|
|
344 function! GetPhpIndent()
|
|
345
|
631
|
346 let b:GetLastRealCodeLNum_ADD = 0
|
|
347
|
532
|
348 let UserIsEditing=0
|
|
349 if b:PHP_oldchangetick != b:changedtick
|
|
350 let b:PHP_oldchangetick = b:changedtick
|
|
351 let UserIsEditing=1
|
|
352 endif
|
409
|
353
|
532
|
354 if b:PHP_default_indenting
|
|
355 let b:PHP_default_indenting = g:PHP_default_indenting * &sw
|
|
356 endif
|
|
357
|
|
358 let cline = getline(v:lnum)
|
409
|
359
|
856
|
360 if !b:PHP_indentinghuge && b:PHP_lastindented > b:PHP_indentbeforelast
|
532
|
361 if b:PHP_indentbeforelast
|
|
362 let b:PHP_indentinghuge = 1
|
1668
|
363 echom 'Large indenting detected, speed optimizations engaged (v1.28)'
|
532
|
364 endif
|
|
365 let b:PHP_indentbeforelast = b:PHP_lastindented
|
|
366 endif
|
409
|
367
|
532
|
368 if b:InPHPcode_checked && prevnonblank(v:lnum - 1) != b:PHP_lastindented
|
|
369 if b:PHP_indentinghuge
|
|
370 echom 'Large indenting deactivated'
|
|
371 let b:PHP_indentinghuge = 0
|
|
372 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
409
|
373 endif
|
532
|
374 let b:PHP_lastindented = v:lnum
|
|
375 let b:PHP_LastIndentedWasComment=0
|
|
376 let b:PHP_InsideMultilineComment=0
|
|
377 let b:PHP_indentbeforelast = 0
|
409
|
378
|
532
|
379 let b:InPHPcode = 0
|
|
380 let b:InPHPcode_checked = 0
|
|
381 let b:InPHPcode_and_script = 0
|
|
382 let b:InPHPcode_tofind = ""
|
409
|
383
|
532
|
384 elseif v:lnum > b:PHP_lastindented
|
|
385 let real_PHP_lastindented = b:PHP_lastindented
|
|
386 let b:PHP_lastindented = v:lnum
|
|
387 endif
|
409
|
388
|
|
389
|
532
|
390 if !b:InPHPcode_checked " {{{ One time check
|
|
391 let b:InPHPcode_checked = 1
|
409
|
392
|
631
|
393 let synname = ""
|
|
394 if cline !~ '<?.*?>'
|
|
395 let synname = IslinePHP (prevnonblank(v:lnum), "")
|
|
396 endif
|
409
|
397
|
532
|
398 if synname!=""
|
1120
|
399 if synname != "phpHereDoc" && synname != "phpHereDocDelimiter"
|
532
|
400 let b:InPHPcode = 1
|
|
401 let b:InPHPcode_tofind = ""
|
409
|
402
|
1668
|
403 if synname =~# "^phpComment"
|
532
|
404 let b:UserIsTypingComment = 1
|
|
405 else
|
|
406 let b:UserIsTypingComment = 0
|
409
|
407 endif
|
|
408
|
532
|
409 if synname =~? '^javaScript'
|
|
410 let b:InPHPcode_and_script = 1
|
|
411 endif
|
409
|
412
|
532
|
413 else
|
|
414 let b:InPHPcode = 0
|
|
415 let b:UserIsTypingComment = 0
|
409
|
416
|
532
|
417 let lnum = v:lnum - 1
|
1668
|
418 while getline(lnum) !~? '<<<''\=\a\w*''\=$' && lnum > 1
|
532
|
419 let lnum = lnum - 1
|
|
420 endwhile
|
409
|
421
|
1668
|
422 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') " XXX 0607
|
532
|
423 endif
|
|
424 else
|
|
425 let b:InPHPcode = 0
|
|
426 let b:UserIsTypingComment = 0
|
|
427 let b:InPHPcode_tofind = '<?\%(.*?>\)\@!\|<script.*>'
|
409
|
428 endif
|
532
|
429 endif "!b:InPHPcode_checked }}}
|
409
|
430
|
|
431
|
532
|
432 " Test if we are indenting PHP code {{{
|
|
433 let lnum = prevnonblank(v:lnum - 1)
|
|
434 let last_line = getline(lnum)
|
409
|
435
|
532
|
436 if b:InPHPcode_tofind!=""
|
|
437 if cline =~? b:InPHPcode_tofind
|
|
438 let b:InPHPcode = 1
|
|
439 let b:InPHPcode_tofind = ""
|
|
440 let b:UserIsTypingComment = 0
|
|
441 if cline =~ '\*/'
|
409
|
442 call cursor(v:lnum, 1)
|
532
|
443 if cline !~ '^\*/'
|
|
444 call search('\*/', 'W')
|
|
445 endif
|
557
|
446 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
|
409
|
447
|
|
448 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
449
|
532
|
450 let b:PHP_LastIndentedWasComment = 0
|
409
|
451
|
532
|
452 if cline =~ '^\s*\*/'
|
|
453 return indent(lnum) + 1
|
|
454 else
|
|
455 return indent(lnum)
|
7
|
456 endif
|
532
|
457
|
|
458 elseif cline =~? '<script\>'
|
|
459 let b:InPHPcode_and_script = 1
|
631
|
460 let b:GetLastRealCodeLNum_ADD = v:lnum
|
532
|
461 endif
|
7
|
462 endif
|
532
|
463 endif
|
409
|
464
|
532
|
465 if b:InPHPcode
|
409
|
466
|
1120
|
467 if !b:InPHPcode_and_script && last_line =~ '\%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP(lnum, '?>')=~"Delimiter"
|
532
|
468 if cline !~? s:PHP_startindenttag
|
|
469 let b:InPHPcode = 0
|
|
470 let b:InPHPcode_tofind = s:PHP_startindenttag
|
|
471 elseif cline =~? '<script\>'
|
|
472 let b:InPHPcode_and_script = 1
|
|
473 endif
|
409
|
474
|
1668
|
475 elseif last_line =~? '<<<''\=\a\w*''\=$' " XXX 0607
|
532
|
476 let b:InPHPcode = 0
|
1668
|
477 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') " XXX 0607
|
532
|
478
|
|
479 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*'
|
|
480 let b:InPHPcode = 0
|
|
481 let b:InPHPcode_tofind = '\*/'
|
409
|
482
|
532
|
483 elseif cline =~? '^\s*</script>'
|
|
484 let b:InPHPcode = 0
|
|
485 let b:InPHPcode_tofind = s:PHP_startindenttag
|
|
486 endif
|
|
487 endif " }}}
|
|
488
|
856
|
489
|
532
|
490 if !b:InPHPcode && !b:InPHPcode_and_script
|
|
491 return -1
|
|
492 endif
|
409
|
493
|
532
|
494 " Indent successive // or # comment the same way the first is {{{
|
|
495 if cline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
|
|
496 if b:PHP_LastIndentedWasComment == 1
|
|
497 return indent(real_PHP_lastindented)
|
|
498 endif
|
|
499 let b:PHP_LastIndentedWasComment = 1
|
|
500 else
|
|
501 let b:PHP_LastIndentedWasComment = 0
|
|
502 endif " }}}
|
|
503
|
|
504 " Indent multiline /* comments correctly {{{
|
|
505
|
|
506 if b:PHP_InsideMultilineComment || b:UserIsTypingComment
|
|
507 if cline =~ '^\s*\*\%(\/\)\@!'
|
|
508 if last_line =~ '^\s*/\*'
|
|
509 return indent(lnum) + 1
|
|
510 else
|
|
511 return indent(lnum)
|
|
512 endif
|
|
513 else
|
|
514 let b:PHP_InsideMultilineComment = 0
|
|
515 endif
|
|
516 endif
|
|
517
|
631
|
518 if !b:PHP_InsideMultilineComment && cline =~ '^\s*/\*' && cline !~ '\*/\s*$'
|
|
519 if getline(v:lnum + 1) !~ '^\s*\*'
|
|
520 return -1
|
|
521 endif
|
532
|
522 let b:PHP_InsideMultilineComment = 1
|
|
523 endif " }}}
|
409
|
524
|
|
525
|
532
|
526 " Things always indented at col 1 (PHP delimiter: <?, ?>, Heredoc end) {{{
|
|
527 if cline =~# '^\s*<?' && cline !~ '?>'
|
|
528 return 0
|
|
529 endif
|
409
|
530
|
856
|
531 if cline =~ '^\s*?>' && cline !~# '<?'
|
532
|
532 return 0
|
|
533 endif
|
409
|
534
|
1668
|
535 if cline =~? '^\s*\a\w*;$\|^\a\w*$' && cline !~? s:notPhpHereDoc " XXX 0607
|
532
|
536 return 0
|
|
537 endif " }}}
|
409
|
538
|
532
|
539 let s:level = 0
|
409
|
540
|
532
|
541 let lnum = GetLastRealCodeLNum(v:lnum - 1)
|
631
|
542
|
532
|
543 let last_line = getline(lnum)
|
|
544 let ind = indent(lnum)
|
|
545 let endline= s:endline
|
409
|
546
|
532
|
547 if ind==0 && b:PHP_default_indenting
|
|
548 let ind = b:PHP_default_indenting
|
|
549 endif
|
409
|
550
|
532
|
551 if lnum == 0
|
|
552 return b:PHP_default_indenting
|
|
553 endif
|
409
|
554
|
|
555
|
532
|
556 if cline =~ '^\s*}\%(}}\)\@!'
|
|
557 let ind = indent(FindOpenBracket(v:lnum))
|
|
558 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
559 return ind
|
|
560 endif
|
409
|
561
|
532
|
562 if cline =~ '^\s*\*/'
|
|
563 call cursor(v:lnum, 1)
|
|
564 if cline !~ '^\*/'
|
|
565 call search('\*/', 'W')
|
|
566 endif
|
557
|
567 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
|
532
|
568
|
|
569 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
409
|
570
|
532
|
571 if cline =~ '^\s*\*/'
|
|
572 return indent(lnum) + 1
|
|
573 else
|
|
574 return indent(lnum)
|
|
575 endif
|
|
576 endif
|
|
577
|
|
578 let defaultORcase = '^\s*\%(default\|case\).*:'
|
|
579
|
1668
|
580 if last_line =~ '[;}]'.endline && last_line !~ '^)' && last_line !~# defaultORcase " Added && last_line !~ '^)' on 2007-12-30
|
532
|
581 if ind==b:PHP_default_indenting
|
|
582 return b:PHP_default_indenting
|
|
583 elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline
|
|
584 return b:PHP_CurrentIndentLevel
|
|
585 endif
|
|
586 endif
|
|
587
|
|
588 let LastLineClosed = 0
|
|
589
|
1668
|
590 let terminated = '\%(;\%(\s*?>\)\=\|<<<''\=\a\w*''\=$\|^\s*}\)'.endline " XXX 0607
|
532
|
591
|
|
592 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline
|
409
|
593
|
532
|
594 if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>'
|
|
595 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
596 return indent(FindTheIfOfAnElse(v:lnum, 1))
|
1668
|
597 elseif cline =~ '^\s*)\=\s*{'
|
532
|
598 let previous_line = last_line
|
|
599 let last_line_num = lnum
|
|
600
|
|
601 while last_line_num > 1
|
|
602
|
1668
|
603 if previous_line =~ '^\s*\%(' . s:blockstart . '\|\%([a-zA-Z]\s*\)*function\)'
|
532
|
604
|
|
605 let ind = indent(last_line_num)
|
|
606
|
|
607 if b:PHP_BracesAtCodeLevel
|
|
608 let ind = ind + &sw
|
|
609 endif
|
|
610
|
856
|
611 return ind
|
532
|
612 endif
|
|
613
|
|
614 let last_line_num = last_line_num - 1
|
|
615 let previous_line = getline(last_line_num)
|
|
616 endwhile
|
409
|
617
|
1668
|
618 elseif last_line =~# unstated && cline !~ '^\s*);\='.endline
|
|
619 let ind = ind + &sw " we indent one level further when the preceding line is not stated
|
532
|
620 return ind
|
|
621
|
1668
|
622 elseif (ind != b:PHP_default_indenting || last_line =~ '^)' ) && last_line =~ terminated " Added || last_line =~ '^)' on 2007-12-30 (array indenting [rpblem broke other things)
|
532
|
623 let previous_line = last_line
|
|
624 let last_line_num = lnum
|
|
625 let LastLineClosed = 1
|
|
626
|
|
627 while 1
|
|
628 if previous_line =~ '^\s*}'
|
|
629 let last_line_num = FindOpenBracket(last_line_num)
|
|
630
|
|
631 if getline(last_line_num) =~ '^\s*{'
|
|
632 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
|
409
|
633 endif
|
532
|
634
|
|
635 let previous_line = getline(last_line_num)
|
7
|
636
|
532
|
637 continue
|
|
638 else
|
409
|
639
|
532
|
640 if getline(last_line_num) =~# '^\s*else\%(if\)\=\>'
|
|
641 let last_line_num = FindTheIfOfAnElse(last_line_num, 0)
|
|
642 continue
|
|
643 endif
|
409
|
644
|
|
645
|
532
|
646 let last_match = last_line_num
|
409
|
647
|
532
|
648 let one_ahead_indent = indent(last_line_num)
|
|
649 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
|
|
650 let two_ahead_indent = indent(last_line_num)
|
|
651 let after_previous_line = previous_line
|
|
652 let previous_line = getline(last_line_num)
|
409
|
653
|
|
654
|
532
|
655 if previous_line =~# defaultORcase.'\|{'.endline
|
|
656 break
|
|
657 endif
|
409
|
658
|
532
|
659 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline
|
|
660 break
|
7
|
661 endif
|
409
|
662
|
856
|
663 if one_ahead_indent == two_ahead_indent || last_line_num < 1
|
1668
|
664 if previous_line =~# '\%(;\|^\s*}\)'.endline || last_line_num < 1
|
532
|
665 break
|
|
666 endif
|
|
667 endif
|
|
668 endif
|
|
669 endwhile
|
|
670
|
|
671 if indent(last_match) != ind
|
|
672 let ind = indent(last_match)
|
|
673 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
674
|
|
675 if cline =~# defaultORcase
|
|
676 let ind = ind - &sw
|
|
677 endif
|
|
678 return ind
|
|
679 endif
|
|
680 endif
|
|
681
|
|
682 let plinnum = GetLastRealCodeLNum(lnum - 1)
|
|
683 let pline = getline(plinnum)
|
|
684
|
|
685 let last_line = substitute(last_line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','')
|
|
686
|
|
687
|
|
688 if ind == b:PHP_default_indenting
|
|
689 if last_line =~ terminated
|
|
690 let LastLineClosed = 1
|
|
691 endif
|
|
692 endif
|
|
693
|
|
694 if !LastLineClosed
|
|
695
|
|
696 if last_line =~# '[{(]'.endline || last_line =~? '\h\w*\s*(.*,$' && pline !~ '[,(]'.endline
|
|
697
|
|
698 if !b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{'
|
409
|
699 let ind = ind + &sw
|
532
|
700 endif
|
|
701
|
1668
|
702 if b:PHP_BracesAtCodeLevel || b:PHP_vintage_case_default_indent == 1 || cline !~# defaultORcase
|
532
|
703 let b:PHP_CurrentIndentLevel = ind
|
1668
|
704
|
532
|
705 return ind
|
|
706 endif
|
|
707
|
|
708 elseif last_line =~ '\S\+\s*),'.endline
|
|
709 call cursor(lnum, 1)
|
|
710 call search('),'.endline, 'W')
|
|
711 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
|
|
712 if openedparent != lnum
|
|
713 let ind = indent(openedparent)
|
|
714 endif
|
1668
|
715 elseif last_line =~ '^\s*'.s:blockstart
|
|
716 let ind = ind + &sw
|
|
717
|
|
718 elseif last_line =~# defaultORcase
|
|
719 let ind = ind + &sw
|
532
|
720
|
|
721
|
1668
|
722 elseif pline =~ '\%(;\%(\s*?>\)\=\|<<<''\=\a\w*''\=$\|^\s*}\|{\)'.endline . '\|' . defaultORcase
|
532
|
723
|
|
724 let ind = ind + &sw
|
409
|
725 endif
|
7
|
726
|
532
|
727 endif
|
409
|
728
|
532
|
729 if cline =~ '^\s*);\='
|
|
730 let ind = ind - &sw
|
1668
|
731 elseif cline =~# defaultORcase && last_line !~# defaultORcase
|
532
|
732 let ind = ind - &sw
|
|
733
|
|
734 endif
|
|
735
|
|
736 let b:PHP_CurrentIndentLevel = ind
|
|
737 return ind
|
7
|
738 endfunction
|