blob: 22210cee7a1938bc4b837ce8b37ec7fcc558d6ca [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: PHP
Bram Moolenaar9ff70112005-07-11 22:29:03 +00003" Author: John Wellesz <John.wellesz (AT) teaser (DOT) fr>
4" URL: http://www.2072productions.com/vim/indent/php.vim
Bram Moolenaar1e015462005-09-25 22:16:38 +00005" Last Change: 2005 September 22th
6" Version: 1.181
Bram Moolenaar071d4272004-06-13 20:20:40 +00007"
Bram Moolenaar1e015462005-09-25 22:16:38 +00008" The change log and all the comments have been removed from this file.
9"
10" For a complete change log and fully commented code, download the script on
Bram Moolenaar9ff70112005-07-11 22:29:03 +000011" 2072productions.com at the URI provided above.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012"
Bram Moolenaar1e015462005-09-25 22:16:38 +000013"
Bram Moolenaar9ff70112005-07-11 22:29:03 +000014" If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr
Bram Moolenaar1e015462005-09-25 22:16:38 +000015" with an example of code that breaks the algorithm.
Bram Moolenaar9ff70112005-07-11 22:29:03 +000016"
17"
18" NOTE: This script must be used with PHP syntax ON and with the php syntax
Bram Moolenaar1e015462005-09-25 22:16:38 +000019" script by Lutz Eymers (http://www.isp.de/data/php.vim ) that's the script bundled with Gvim.
Bram Moolenaar9ff70112005-07-11 22:29:03 +000020"
21"
22" In the case you have syntax errors in your script such as end of HereDoc
23" tags not at col 1 you'll have to indent your file 2 times (This script
24" will automatically put HereDoc end tags at col 1).
25"
Bram Moolenaar9ff70112005-07-11 22:29:03 +000026" NOTE: If you are editing file in Unix file format and that (by accident)
27" there are '\r' before new lines, this script won't be able to proceed
28" correctly and will make many mistakes because it won't be able to match
29" '\s*$' correctly.
30" So you have to remove those useless characters first with a command like:
31"
32" :%s /\r$//g
33"
34" or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will
35" silently remove them when VIM load this script (at each bufread).
Bram Moolenaar071d4272004-06-13 20:20:40 +000036
Bram Moolenaar9ff70112005-07-11 22:29:03 +000037" Options: PHP_default_indenting = # of sw (default is 0), # of sw will be
Bram Moolenaar1e015462005-09-25 22:16:38 +000038" added to the indent of each line of PHP code.
Bram Moolenaar9ff70112005-07-11 22:29:03 +000039"
40" Options: PHP_removeCRwhenUnix = 1 to make the script automatically remove CR
Bram Moolenaar1e015462005-09-25 22:16:38 +000041" at end of lines (by default this option is unset), NOTE that you
42" MUST remove CR when the fileformat is UNIX else the indentation
43" won't be correct...
Bram Moolenaar9ff70112005-07-11 22:29:03 +000044"
45" Options: PHP_BracesAtCodeLevel = 1 to indent the '{' and '}' at the same
Bram Moolenaar1e015462005-09-25 22:16:38 +000046" level than the code they contain.
47" Exemple:
48" Instead of:
49" if ($foo)
50" {
51" foo();
52" }
53"
54" You will write:
55" if ($foo)
Bram Moolenaar9ff70112005-07-11 22:29:03 +000056" {
Bram Moolenaar1e015462005-09-25 22:16:38 +000057" foo();
Bram Moolenaar9ff70112005-07-11 22:29:03 +000058" }
59"
Bram Moolenaar1e015462005-09-25 22:16:38 +000060" NOTE: The script will be a bit slower if you use this option because
61" some optimizations won't be available.
Bram Moolenaar9ff70112005-07-11 22:29:03 +000062
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064if exists("b:did_indent")
Bram Moolenaar1e015462005-09-25 22:16:38 +000065 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000066endif
67let b:did_indent = 1
68
Bram Moolenaar9ff70112005-07-11 22:29:03 +000069
70let php_sync_method = 0
71
72
73if exists("PHP_default_indenting")
Bram Moolenaar1e015462005-09-25 22:16:38 +000074 let b:PHP_default_indenting = PHP_default_indenting * &sw
Bram Moolenaar9ff70112005-07-11 22:29:03 +000075else
Bram Moolenaar1e015462005-09-25 22:16:38 +000076 let b:PHP_default_indenting = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +000077endif
78
79if exists("PHP_BracesAtCodeLevel")
Bram Moolenaar1e015462005-09-25 22:16:38 +000080 let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel
Bram Moolenaar9ff70112005-07-11 22:29:03 +000081else
Bram Moolenaar1e015462005-09-25 22:16:38 +000082 let b:PHP_BracesAtCodeLevel = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +000083endif
84
85
86let b:PHP_lastindented = 0
87let b:PHP_indentbeforelast = 0
88let b:PHP_indentinghuge = 0
89let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
90let b:PHP_LastIndentedWasComment = 0
91let b:PHP_InsideMultilineComment = 0
92let b:InPHPcode = 0
93let b:InPHPcode_checked = 0
94let b:InPHPcode_and_script = 0
95let b:InPHPcode_tofind = ""
96let b:PHP_oldchangetick = b:changedtick
97let b:UserIsTypingComment = 0
98let b:optionsset = 0
99
100setlocal nosmartindent
101setlocal noautoindent
102setlocal nocindent
Bram Moolenaar1e015462005-09-25 22:16:38 +0000103setlocal nolisp
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000104
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105setlocal indentexpr=GetPhpIndent()
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000106setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=?>,=<?,=*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000108
Bram Moolenaar1e015462005-09-25 22:16:38 +0000109
110let s:searchpairflags = 'bWr'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000111
112if &fileformat == "unix" && exists("PHP_removeCRwhenUnix") && PHP_removeCRwhenUnix
Bram Moolenaar1e015462005-09-25 22:16:38 +0000113 silent! %s/\r$//g
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000114endif
115
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116if exists("*GetPhpIndent")
Bram Moolenaar1e015462005-09-25 22:16:38 +0000117 finish " XXX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118endif
119
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000120let s:endline= '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$'
121let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!'
122"setlocal debug=msg " XXX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000124
125function! GetLastRealCodeLNum(startline) " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126 let lnum = a:startline
127 let old_lnum = lnum
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000128
Bram Moolenaar1e015462005-09-25 22:16:38 +0000129 while lnum > 1
130 let lnum = prevnonblank(lnum)
131 let lastline = getline(lnum)
132
133 if b:InPHPcode_and_script && lastline =~ '?>\s*$'
134 let lnum = lnum - 1
135 elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$'
136 let lnum = lnum - 1
137 elseif lastline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
138 let lnum = lnum - 1
139 elseif lastline =~ '\*/\s*$'
140 call cursor(lnum, 1)
141 if lastline !~ '^\*/'
142 call search('\*/', 'W')
143 endif
144 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags)
145
146 let lastline = getline(lnum)
147 if lastline =~ '^\s*/\*'
148 let lnum = lnum - 1
149 else
150 break
151 endif
152
153
154 elseif lastline =~? '\%(//\s*\|?>.*\)\@<!<?\%(php\)\=\s*$\|^\s*<script\>'
155
156 while lastline !~ '\(<?.*\)\@<!?>' && lnum > 1
157 let lnum = lnum - 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000158 let lastline = getline(lnum)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000159 endwhile
160 if lastline =~ '^\s*?>'
161 let lnum = lnum - 1
162 else
163 break
164 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000165
166
Bram Moolenaar1e015462005-09-25 22:16:38 +0000167 elseif lastline =~? '^\a\w*;$' && lastline !~? s:notPhpHereDoc
168 let tofind=substitute( lastline, '\([^;]\+\);', '<<<\1$', '')
169 while getline(lnum) !~? tofind && lnum > 1
170 let lnum = lnum - 1
171 endwhile
172 else
173 break
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000174 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000175 endwhile
176
177 if lnum==1 && getline(lnum)!~ '<?'
178 let lnum=0
179 endif
180
181 if b:InPHPcode_and_script && !b:InPHPcode
182 let b:InPHPcode_and_script = 0
183 endif
184 return lnum
185endfunction " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000186
187function! Skippmatch() " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000188 let synname = synIDattr(synID(line("."), col("."), 0), "name")
189 if synname == "Delimiter" || synname == "phpParent" || synname == "javaScriptBraces" || synname == "phpComment" && b:UserIsTypingComment
190 return 0
191 else
192 return 1
193 endif
194endfun " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000195
196function! FindOpenBracket(lnum) " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000197 call cursor(a:lnum, 1)
198 return searchpair('{', '', '}', 'bW', 'Skippmatch()')
199endfun " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000200
201function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000202
203 if getline(a:lnum) =~# '^\s*}\s*else\%(if\)\=\>'
204 let beforeelse = a:lnum
205 else
206 let beforeelse = GetLastRealCodeLNum(a:lnum - 1)
207 endif
208
209 if !s:level
210 let s:iftoskip = 0
211 endif
212
213 if getline(beforeelse) =~# '^\s*\%(}\s*\)\=else\%(\s*if\)\@!\>'
214 let s:iftoskip = s:iftoskip + 1
215 endif
216
217 if getline(beforeelse) =~ '^\s*}'
218 let beforeelse = FindOpenBracket(beforeelse)
219
220 if getline(beforeelse) =~ '^\s*{'
221 let beforeelse = GetLastRealCodeLNum(beforeelse - 1)
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000222 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000223 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000224
225
Bram Moolenaar1e015462005-09-25 22:16:38 +0000226 if !s:iftoskip && a:StopAfterFirstPrevElse && getline(beforeelse) =~# '^\s*\%([}]\s*\)\=else\%(if\)\=\>'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000227 return beforeelse
Bram Moolenaar1e015462005-09-25 22:16:38 +0000228 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000229
Bram Moolenaar1e015462005-09-25 22:16:38 +0000230 if getline(beforeelse) !~# '^\s*if\>' && beforeelse>1 || s:iftoskip && beforeelse>1
231
232 if s:iftoskip && getline(beforeelse) =~# '^\s*if\>'
233 let s:iftoskip = s:iftoskip - 1
234 endif
235
236 let s:level = s:level + 1
237 let beforeelse = FindTheIfOfAnElse(beforeelse, a:StopAfterFirstPrevElse)
238 endif
239
240 return beforeelse
241
242endfunction " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000243
244function! IslinePHP (lnum, tofind) " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000245 let cline = getline(a:lnum)
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000246
Bram Moolenaar1e015462005-09-25 22:16:38 +0000247 if a:tofind==""
248 let tofind = "^\\s*[\"']*\s*\\zs\\S"
249 else
250 let tofind = a:tofind
251 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000252
Bram Moolenaar1e015462005-09-25 22:16:38 +0000253 let tofind = tofind . '\c'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000254
Bram Moolenaar1e015462005-09-25 22:16:38 +0000255 let coltotest = match (cline, tofind) + 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000256
Bram Moolenaar1e015462005-09-25 22:16:38 +0000257 let synname = synIDattr(synID(a:lnum, coltotest, 0), "name")
258
259 if synname =~ '^php' || synname=="Delimiter" || synname =~? '^javaScript'
260 return synname
261 else
262 return ""
263 endif
264endfunction " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000265
266let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\);'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000267let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|else\>\|while\>\|switch\>\|for\%(each\)\=\>\|declare\>\|class\>\|[|&]\)'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000268
269let s:autorestoptions = 0
270if ! s:autorestoptions
Bram Moolenaar1e015462005-09-25 22:16:38 +0000271 au BufWinEnter,Syntax *.php,*.php3,*.php4,*.php5 call ResetOptions()
272 let s:autorestoptions = 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000273endif
274
275function! ResetOptions()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000276 if ! b:optionsset
277 setlocal formatoptions=qroc
278 let b:optionsset = 1
279 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000280endfunc
281
282function! GetPhpIndent()
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000283
Bram Moolenaar1e015462005-09-25 22:16:38 +0000284 let UserIsEditing=0
285 if b:PHP_oldchangetick != b:changedtick
286 let b:PHP_oldchangetick = b:changedtick
287 let UserIsEditing=1
288 endif
289
290 if b:PHP_default_indenting
291 let b:PHP_default_indenting = g:PHP_default_indenting * &sw
292 endif
293
294 let cline = getline(v:lnum)
295
296 if !b:PHP_indentinghuge && b:PHP_lastindented > b:PHP_indentbeforelast
297 if b:PHP_indentbeforelast
298 let b:PHP_indentinghuge = 1
299 echom 'Large indenting detected, speed optimizations engaged'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000300 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000301 let b:PHP_indentbeforelast = b:PHP_lastindented
302 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000303
Bram Moolenaar1e015462005-09-25 22:16:38 +0000304 if b:InPHPcode_checked && prevnonblank(v:lnum - 1) != b:PHP_lastindented
305 if b:PHP_indentinghuge
306 echom 'Large indenting deactivated'
307 let b:PHP_indentinghuge = 0
308 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000309 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000310 let b:PHP_lastindented = v:lnum
311 let b:PHP_LastIndentedWasComment=0
312 let b:PHP_InsideMultilineComment=0
313 let b:PHP_indentbeforelast = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000314
Bram Moolenaar1e015462005-09-25 22:16:38 +0000315 let b:InPHPcode = 0
316 let b:InPHPcode_checked = 0
317 let b:InPHPcode_and_script = 0
318 let b:InPHPcode_tofind = ""
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000319
Bram Moolenaar1e015462005-09-25 22:16:38 +0000320 elseif v:lnum > b:PHP_lastindented
321 let real_PHP_lastindented = b:PHP_lastindented
322 let b:PHP_lastindented = v:lnum
323 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000324
Bram Moolenaar1e015462005-09-25 22:16:38 +0000325
326 if !b:InPHPcode_checked " {{{ One time check
327 let b:InPHPcode_checked = 1
328
329 let synname = IslinePHP (prevnonblank(v:lnum), "")
330
331 if synname!=""
332 if synname != "phpHereDoc"
333 let b:InPHPcode = 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000334 let b:InPHPcode_tofind = ""
335
Bram Moolenaar1e015462005-09-25 22:16:38 +0000336 if synname == "phpComment"
337 let b:UserIsTypingComment = 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000338 else
Bram Moolenaar1e015462005-09-25 22:16:38 +0000339 let b:UserIsTypingComment = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000340 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000341
Bram Moolenaar1e015462005-09-25 22:16:38 +0000342 if synname =~? '^javaScript'
343 let b:InPHPcode_and_script = 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000344 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000345
Bram Moolenaar1e015462005-09-25 22:16:38 +0000346 else
347 let b:InPHPcode = 0
348 let b:UserIsTypingComment = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000349
Bram Moolenaar1e015462005-09-25 22:16:38 +0000350 let lnum = v:lnum - 1
351 while getline(lnum) !~? '<<<\a\w*$' && lnum > 1
352 let lnum = lnum - 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000353 endwhile
354
Bram Moolenaar1e015462005-09-25 22:16:38 +0000355 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<\(\a\w*\)\c', '^\\s*\1;$', '')
356 endif
357 else
358 let b:InPHPcode = 0
359 let b:UserIsTypingComment = 0
360 let b:InPHPcode_tofind = '<?\%(.*?>\)\@!\|<script.*>'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000361 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000362 endif "!b:InPHPcode_checked }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000363
364
Bram Moolenaar1e015462005-09-25 22:16:38 +0000365 " Test if we are indenting PHP code {{{
366 let lnum = prevnonblank(v:lnum - 1)
367 let last_line = getline(lnum)
368
369 if b:InPHPcode_tofind!=""
370 if cline =~? b:InPHPcode_tofind
371 let b:InPHPcode = 1
372 let b:InPHPcode_tofind = ""
373 let b:UserIsTypingComment = 0
374 if cline =~ '\*/'
375 call cursor(v:lnum, 1)
376 if cline !~ '^\*/'
377 call search('\*/', 'W')
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000378 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000379 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags)
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000380
Bram Moolenaar1e015462005-09-25 22:16:38 +0000381 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000382
Bram Moolenaar1e015462005-09-25 22:16:38 +0000383 let b:PHP_LastIndentedWasComment = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000384
Bram Moolenaar1e015462005-09-25 22:16:38 +0000385 if cline =~ '^\s*\*/'
386 return indent(lnum) + 1
387 else
388 return indent(lnum)
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000389 endif
390
Bram Moolenaar1e015462005-09-25 22:16:38 +0000391 elseif cline =~? '<script\>'
392 let b:InPHPcode_and_script = 1
393 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000394 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000395 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000396
Bram Moolenaar1e015462005-09-25 22:16:38 +0000397 if b:InPHPcode
398
399 if !b:InPHPcode_and_script && last_line =~ '\%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP(lnum, '?>')=="Delimiter"
400 if cline !~? s:PHP_startindenttag
401 let b:InPHPcode = 0
402 let b:InPHPcode_tofind = s:PHP_startindenttag
403 elseif cline =~? '<script\>'
404 let b:InPHPcode_and_script = 1
405 endif
406
407 elseif last_line =~? '<<<\a\w*$'
408 let b:InPHPcode = 0
409 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<\(\a\w*\)\c', '^\\s*\1;$', '')
410
411 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*'
412 let b:InPHPcode = 0
413 let b:InPHPcode_tofind = '\*/'
414
415 elseif cline =~? '^\s*</script>'
416 let b:InPHPcode = 0
417 let b:InPHPcode_tofind = s:PHP_startindenttag
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000418 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000419 endif " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000420
Bram Moolenaar1e015462005-09-25 22:16:38 +0000421 if !b:InPHPcode && !b:InPHPcode_and_script
422 return -1
423 endif
424
425
426 " Indent successive // or # comment the same way the first is {{{
427 if cline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
428 if b:PHP_LastIndentedWasComment == 1
429 return indent(real_PHP_lastindented)
430 endif
431 let b:PHP_LastIndentedWasComment = 1
432 else
433 let b:PHP_LastIndentedWasComment = 0
434 endif " }}}
435
436 " Indent multiline /* comments correctly {{{
437
438 if b:PHP_InsideMultilineComment || b:UserIsTypingComment
439 if cline =~ '^\s*\*\%(\/\)\@!'
440 if last_line =~ '^\s*/\*'
441 return indent(lnum) + 1
442 else
443 return indent(lnum)
444 endif
445 else
446 let b:PHP_InsideMultilineComment = 0
447 endif
448 endif
449
450 if !b:PHP_InsideMultilineComment && cline =~ '^\s*/\*'
451 let b:PHP_InsideMultilineComment = 1
452 return -1
453 endif " }}}
454
455
456 " Things always indented at col 1 (PHP delimiter: <?, ?>, Heredoc end) {{{
457 if cline =~# '^\s*<?' && cline !~ '?>'
458 return 0
459 endif
460
461 if cline =~ '^\s*?>' && cline !~# '<?'
462 return 0
463 endif
464
465 if cline =~? '^\s*\a\w*;$' && cline !~? s:notPhpHereDoc
466 return 0
467 endif " }}}
468
469 let s:level = 0
470
471 let lnum = GetLastRealCodeLNum(v:lnum - 1)
472 let last_line = getline(lnum)
473 let ind = indent(lnum)
474 let endline= s:endline
475
476 if ind==0 && b:PHP_default_indenting
477 let ind = b:PHP_default_indenting
478 endif
479
480 if lnum == 0
481 return b:PHP_default_indenting
482 endif
483
484
485 if cline =~ '^\s*}\%(}}\)\@!'
486 let ind = indent(FindOpenBracket(v:lnum))
487 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000488 return ind
Bram Moolenaar1e015462005-09-25 22:16:38 +0000489 endif
490
491 if cline =~ '^\s*\*/'
492 call cursor(v:lnum, 1)
493 if cline !~ '^\*/'
494 call search('\*/', 'W')
495 endif
496 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags)
497
498 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
499
500 if cline =~ '^\s*\*/'
501 return indent(lnum) + 1
502 else
503 return indent(lnum)
504 endif
505 endif
506
507 let defaultORcase = '^\s*\%(default\|case\).*:'
508
509 if last_line =~ '[;}]'.endline && last_line !~# defaultORcase
510 if ind==b:PHP_default_indenting
511 return b:PHP_default_indenting
512 elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline
513 return b:PHP_CurrentIndentLevel
514 endif
515 endif
516
517 let LastLineClosed = 0
518
519 let terminated = '\%(;\%(\s*?>\)\=\|<<<\a\w*\|}\)'.endline
520
521 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline
522
523 if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>'
524 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
525 return indent(FindTheIfOfAnElse(v:lnum, 1))
526 elseif cline =~ '^\s*{'
527 let previous_line = last_line
528 let last_line_num = lnum
529
530 while last_line_num > 1
531
532 if previous_line =~ '^\s*\%(' . s:blockstart . '\|\%([a-zA-Z]\s*\)*function\)' && previous_line !~ '^\s*[|&]'
533
534 let ind = indent(last_line_num)
535
536 if b:PHP_BracesAtCodeLevel
537 let ind = ind + &sw
538 endif
539
540 return ind
541 endif
542
543 let last_line_num = last_line_num - 1
544 let previous_line = getline(last_line_num)
545 endwhile
546
547 elseif last_line =~# unstated && cline !~ '^\s*{\|^\s*);\='.endline
548 let ind = ind + &sw
549 return ind
550
551 elseif ind != b:PHP_default_indenting && last_line =~ terminated
552 let previous_line = last_line
553 let last_line_num = lnum
554 let LastLineClosed = 1
555
556 while 1
557 if previous_line =~ '^\s*}'
558 let last_line_num = FindOpenBracket(last_line_num)
559
560 if getline(last_line_num) =~ '^\s*{'
561 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
562 endif
563
564 let previous_line = getline(last_line_num)
565
566 continue
567 else
568
569 if getline(last_line_num) =~# '^\s*else\%(if\)\=\>'
570 let last_line_num = FindTheIfOfAnElse(last_line_num, 0)
571 continue
572 endif
573
574
575 let last_match = last_line_num
576
577 let one_ahead_indent = indent(last_line_num)
578 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
579 let two_ahead_indent = indent(last_line_num)
580 let after_previous_line = previous_line
581 let previous_line = getline(last_line_num)
582
583
584 if previous_line =~# defaultORcase.'\|{'.endline
585 break
586 endif
587
588 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline
589 break
590 endif
591
592 if one_ahead_indent == two_ahead_indent || last_line_num < 1
593 if previous_line =~# '[;}]'.endline || last_line_num < 1
594 break
595 endif
596 endif
597 endif
598 endwhile
599
600 if indent(last_match) != ind
601 let ind = indent(last_match)
602 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
603
604 if cline =~# defaultORcase
605 let ind = ind - &sw
606 endif
607 return ind
608 endif
609 endif
610
611 let plinnum = GetLastRealCodeLNum(lnum - 1)
612 let pline = getline(plinnum)
613
614 let last_line = substitute(last_line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','')
615
616
617 if ind == b:PHP_default_indenting
618 if last_line =~ terminated
619 let LastLineClosed = 1
620 endif
621 endif
622
623 if !LastLineClosed
624
625 if last_line =~# '[{(]'.endline || last_line =~? '\h\w*\s*(.*,$' && pline !~ '[,(]'.endline
626
627 if !b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{'
628 let ind = ind + &sw
629 endif
630
631 if b:PHP_BracesAtCodeLevel || cline !~# defaultORcase
632 let b:PHP_CurrentIndentLevel = ind
633 return ind
634 endif
635
636 elseif last_line =~ '\S\+\s*),'.endline
637 call cursor(lnum, 1)
638 call search('),'.endline, 'W')
639 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
640 if openedparent != lnum
641 let ind = indent(openedparent)
642 endif
643
644
645 elseif cline !~ '^\s*{' && pline =~ '\%(;\%(\s*?>\)\=\|<<<\a\w*\|{\|^\s*'.s:blockstart.'\s*(.*)\)'.endline.'\|^\s*}\|'.defaultORcase
646
647 let ind = ind + &sw
648
649 endif
650
651 elseif last_line =~# defaultORcase
652 let ind = ind + &sw
653 endif
654
655 if cline =~ '^\s*);\='
656 let ind = ind - &sw
657 elseif cline =~# defaultORcase
658 let ind = ind - &sw
659
660 endif
661
662 let b:PHP_CurrentIndentLevel = ind
663 return ind
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000664endfunction
665
Bram Moolenaar1e015462005-09-25 22:16:38 +0000666" vim: set ts=8 sw=4 sts=4: