blob: 8d05b03b0f94faa9db6cdd52ca11fd048dd49319 [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 Moolenaara5792f52005-11-23 21:25:05 +00005" Last Change: 2005 Nobember 21st
6" Version: 1.20
Bram Moolenaar071d4272004-06-13 20:20:40 +00007"
Bram Moolenaara5792f52005-11-23 21:25:05 +00008" The change log and all the comments have been removed from this file.
Bram Moolenaar1e015462005-09-25 22:16:38 +00009"
Bram Moolenaara5792f52005-11-23 21:25:05 +000010" For a complete change log and fully commented code, download the script on
11" 2072productions.com at the URI provided above.
12"
Bram Moolenaar9ff70112005-07-11 22:29:03 +000013" If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr
Bram Moolenaara5792f52005-11-23 21:25:05 +000014" with an example of code that break the algorithm.
15"
16"
17" Thanks a lot for using this script.
Bram Moolenaar9ff70112005-07-11 22:29:03 +000018"
19"
20" NOTE: This script must be used with PHP syntax ON and with the php syntax
Bram Moolenaar1e015462005-09-25 22:16:38 +000021" 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 +000022"
23"
24" In the case you have syntax errors in your script such as end of HereDoc
25" tags not at col 1 you'll have to indent your file 2 times (This script
26" will automatically put HereDoc end tags at col 1).
27"
Bram Moolenaara5792f52005-11-23 21:25:05 +000028"
Bram Moolenaar9ff70112005-07-11 22:29:03 +000029" NOTE: If you are editing file in Unix file format and that (by accident)
30" there are '\r' before new lines, this script won't be able to proceed
31" correctly and will make many mistakes because it won't be able to match
32" '\s*$' correctly.
33" So you have to remove those useless characters first with a command like:
34"
35" :%s /\r$//g
36"
37" or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will
38" silently remove them when VIM load this script (at each bufread).
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaara5792f52005-11-23 21:25:05 +000040
41" Options: PHP_autoformatcomment = 0 to not enable autoformating of comment by
42" default, if set to 0, this script will let the 'formatoptions' setting intact.
43"
Bram Moolenaar9ff70112005-07-11 22:29:03 +000044" Options: PHP_default_indenting = # of sw (default is 0), # of sw will be
Bram Moolenaara5792f52005-11-23 21:25:05 +000045" added to the indent of each line of PHP code.
Bram Moolenaar9ff70112005-07-11 22:29:03 +000046"
47" Options: PHP_removeCRwhenUnix = 1 to make the script automatically remove CR
Bram Moolenaara5792f52005-11-23 21:25:05 +000048" at end of lines (by default this option is unset), NOTE that you
49" MUST remove CR when the fileformat is UNIX else the indentation
50" won't be correct...
Bram Moolenaar9ff70112005-07-11 22:29:03 +000051"
52" Options: PHP_BracesAtCodeLevel = 1 to indent the '{' and '}' at the same
Bram Moolenaara5792f52005-11-23 21:25:05 +000053" level than the code they contain.
54" Exemple:
55" Instead of:
56" if ($foo)
Bram Moolenaar9ff70112005-07-11 22:29:03 +000057" {
Bram Moolenaara5792f52005-11-23 21:25:05 +000058" foo();
Bram Moolenaar9ff70112005-07-11 22:29:03 +000059" }
60"
Bram Moolenaara5792f52005-11-23 21:25:05 +000061" You will write:
62" if ($foo)
63" {
64" foo();
65" }
66"
67" NOTE: The script will be a bit slower if you use this option because
68" some optimizations won't be available.
69
Bram Moolenaar9ff70112005-07-11 22:29:03 +000070
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072if exists("b:did_indent")
Bram Moolenaar1e015462005-09-25 22:16:38 +000073 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000074endif
75let b:did_indent = 1
76
Bram Moolenaar9ff70112005-07-11 22:29:03 +000077
78let php_sync_method = 0
79
80
81if exists("PHP_default_indenting")
Bram Moolenaar1e015462005-09-25 22:16:38 +000082 let b:PHP_default_indenting = PHP_default_indenting * &sw
Bram Moolenaar9ff70112005-07-11 22:29:03 +000083else
Bram Moolenaar1e015462005-09-25 22:16:38 +000084 let b:PHP_default_indenting = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +000085endif
86
87if exists("PHP_BracesAtCodeLevel")
Bram Moolenaar1e015462005-09-25 22:16:38 +000088 let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel
Bram Moolenaar9ff70112005-07-11 22:29:03 +000089else
Bram Moolenaar1e015462005-09-25 22:16:38 +000090 let b:PHP_BracesAtCodeLevel = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +000091endif
92
Bram Moolenaara5792f52005-11-23 21:25:05 +000093if exists("PHP_autoformatcomment")
94 let b:PHP_autoformatcomment = PHP_autoformatcomment
95else
96 let b:PHP_autoformatcomment = 1
97endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +000098
99let b:PHP_lastindented = 0
100let b:PHP_indentbeforelast = 0
101let b:PHP_indentinghuge = 0
102let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
103let b:PHP_LastIndentedWasComment = 0
104let b:PHP_InsideMultilineComment = 0
105let b:InPHPcode = 0
106let b:InPHPcode_checked = 0
107let b:InPHPcode_and_script = 0
108let b:InPHPcode_tofind = ""
109let b:PHP_oldchangetick = b:changedtick
110let b:UserIsTypingComment = 0
111let b:optionsset = 0
112
113setlocal nosmartindent
114setlocal noautoindent
115setlocal nocindent
Bram Moolenaar1e015462005-09-25 22:16:38 +0000116setlocal nolisp
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000117
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118setlocal indentexpr=GetPhpIndent()
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000119setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=?>,=<?,=*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000121
Bram Moolenaar1e015462005-09-25 22:16:38 +0000122
123let s:searchpairflags = 'bWr'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000124
125if &fileformat == "unix" && exists("PHP_removeCRwhenUnix") && PHP_removeCRwhenUnix
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126 silent! %s/\r$//g
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000127endif
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129if exists("*GetPhpIndent")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000130 finish " XXX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131endif
132
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000133let s:endline= '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$'
134let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000135" setlocal debug=msg " XXX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000137
138function! GetLastRealCodeLNum(startline) " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000139 let lnum = a:startline
140 let old_lnum = lnum
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000141
Bram Moolenaar1e015462005-09-25 22:16:38 +0000142 while lnum > 1
143 let lnum = prevnonblank(lnum)
144 let lastline = getline(lnum)
145
146 if b:InPHPcode_and_script && lastline =~ '?>\s*$'
147 let lnum = lnum - 1
148 elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$'
149 let lnum = lnum - 1
150 elseif lastline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
151 let lnum = lnum - 1
152 elseif lastline =~ '\*/\s*$'
153 call cursor(lnum, 1)
154 if lastline !~ '^\*/'
155 call search('\*/', 'W')
156 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000157 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
Bram Moolenaar1e015462005-09-25 22:16:38 +0000158
159 let lastline = getline(lnum)
160 if lastline =~ '^\s*/\*'
161 let lnum = lnum - 1
162 else
163 break
164 endif
165
166
167 elseif lastline =~? '\%(//\s*\|?>.*\)\@<!<?\%(php\)\=\s*$\|^\s*<script\>'
168
169 while lastline !~ '\(<?.*\)\@<!?>' && lnum > 1
170 let lnum = lnum - 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000171 let lastline = getline(lnum)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000172 endwhile
173 if lastline =~ '^\s*?>'
174 let lnum = lnum - 1
175 else
176 break
177 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000178
179
Bram Moolenaar1e015462005-09-25 22:16:38 +0000180 elseif lastline =~? '^\a\w*;$' && lastline !~? s:notPhpHereDoc
181 let tofind=substitute( lastline, '\([^;]\+\);', '<<<\1$', '')
182 while getline(lnum) !~? tofind && lnum > 1
183 let lnum = lnum - 1
184 endwhile
185 else
186 break
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000187 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000188 endwhile
189
190 if lnum==1 && getline(lnum)!~ '<?'
191 let lnum=0
192 endif
193
194 if b:InPHPcode_and_script && !b:InPHPcode
195 let b:InPHPcode_and_script = 0
196 endif
197 return lnum
198endfunction " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000199
Bram Moolenaara5792f52005-11-23 21:25:05 +0000200function! Skippmatch2()
201
202 let line = getline(".")
203
204 if line =~ '\%(".*\)\@<=/\*\%(.*"\)\@=' || line =~ '\%(//.*\)\@<=/\*'
205 return 1
206 else
207 return 0
208 endif
209endfun
210
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000211function! Skippmatch() " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000212 let synname = synIDattr(synID(line("."), col("."), 0), "name")
213 if synname == "Delimiter" || synname == "phpParent" || synname == "javaScriptBraces" || synname == "phpComment" && b:UserIsTypingComment
214 return 0
215 else
216 return 1
217 endif
218endfun " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000219
220function! FindOpenBracket(lnum) " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000221 call cursor(a:lnum, 1)
222 return searchpair('{', '', '}', 'bW', 'Skippmatch()')
223endfun " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000224
225function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000226
227 if getline(a:lnum) =~# '^\s*}\s*else\%(if\)\=\>'
228 let beforeelse = a:lnum
229 else
230 let beforeelse = GetLastRealCodeLNum(a:lnum - 1)
231 endif
232
233 if !s:level
234 let s:iftoskip = 0
235 endif
236
237 if getline(beforeelse) =~# '^\s*\%(}\s*\)\=else\%(\s*if\)\@!\>'
238 let s:iftoskip = s:iftoskip + 1
239 endif
240
241 if getline(beforeelse) =~ '^\s*}'
242 let beforeelse = FindOpenBracket(beforeelse)
243
244 if getline(beforeelse) =~ '^\s*{'
245 let beforeelse = GetLastRealCodeLNum(beforeelse - 1)
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000246 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000247 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000248
249
Bram Moolenaar1e015462005-09-25 22:16:38 +0000250 if !s:iftoskip && a:StopAfterFirstPrevElse && getline(beforeelse) =~# '^\s*\%([}]\s*\)\=else\%(if\)\=\>'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000251 return beforeelse
Bram Moolenaar1e015462005-09-25 22:16:38 +0000252 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000253
Bram Moolenaar1e015462005-09-25 22:16:38 +0000254 if getline(beforeelse) !~# '^\s*if\>' && beforeelse>1 || s:iftoskip && beforeelse>1
255
256 if s:iftoskip && getline(beforeelse) =~# '^\s*if\>'
257 let s:iftoskip = s:iftoskip - 1
258 endif
259
260 let s:level = s:level + 1
261 let beforeelse = FindTheIfOfAnElse(beforeelse, a:StopAfterFirstPrevElse)
262 endif
263
264 return beforeelse
265
266endfunction " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000267
268function! IslinePHP (lnum, tofind) " {{{
Bram Moolenaar1e015462005-09-25 22:16:38 +0000269 let cline = getline(a:lnum)
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000270
Bram Moolenaar1e015462005-09-25 22:16:38 +0000271 if a:tofind==""
Bram Moolenaara5792f52005-11-23 21:25:05 +0000272 let tofind = "^\\s*[\"']*\\s*\\zs\\S"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000273 else
274 let tofind = a:tofind
275 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000276
Bram Moolenaar1e015462005-09-25 22:16:38 +0000277 let tofind = tofind . '\c'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000278
Bram Moolenaar1e015462005-09-25 22:16:38 +0000279 let coltotest = match (cline, tofind) + 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000280
Bram Moolenaar1e015462005-09-25 22:16:38 +0000281 let synname = synIDattr(synID(a:lnum, coltotest, 0), "name")
282
283 if synname =~ '^php' || synname=="Delimiter" || synname =~? '^javaScript'
284 return synname
285 else
286 return ""
287 endif
288endfunction " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000289
290let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\);'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000291let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|else\>\|while\>\|switch\>\|for\%(each\)\=\>\|declare\>\|class\>\|interface\>\|abstract\>\|[|&]\)'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000292
293let s:autorestoptions = 0
294if ! s:autorestoptions
Bram Moolenaara5792f52005-11-23 21:25:05 +0000295 au BufWinEnter,Syntax *.php,*.php3,*.php4,*.php5 call ResetOptions()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000296 let s:autorestoptions = 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000297endif
298
299function! ResetOptions()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000300 if ! b:optionsset
Bram Moolenaara5792f52005-11-23 21:25:05 +0000301 if b:PHP_autoformatcomment
302
303 setlocal comments=s1:/*,mb:*,ex:*/,://,:#
304
305 setlocal formatoptions-=t
306 setlocal formatoptions+=q
307 setlocal formatoptions+=r
308 setlocal formatoptions+=o
309 setlocal formatoptions+=w
310 setlocal formatoptions+=c
311 setlocal formatoptions+=b
312 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000313 let b:optionsset = 1
314 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000315endfunc
316
317function! GetPhpIndent()
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000318
Bram Moolenaar1e015462005-09-25 22:16:38 +0000319 let UserIsEditing=0
320 if b:PHP_oldchangetick != b:changedtick
321 let b:PHP_oldchangetick = b:changedtick
322 let UserIsEditing=1
323 endif
324
325 if b:PHP_default_indenting
326 let b:PHP_default_indenting = g:PHP_default_indenting * &sw
327 endif
328
329 let cline = getline(v:lnum)
330
331 if !b:PHP_indentinghuge && b:PHP_lastindented > b:PHP_indentbeforelast
332 if b:PHP_indentbeforelast
333 let b:PHP_indentinghuge = 1
334 echom 'Large indenting detected, speed optimizations engaged'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000335 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000336 let b:PHP_indentbeforelast = b:PHP_lastindented
337 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000338
Bram Moolenaar1e015462005-09-25 22:16:38 +0000339 if b:InPHPcode_checked && prevnonblank(v:lnum - 1) != b:PHP_lastindented
340 if b:PHP_indentinghuge
341 echom 'Large indenting deactivated'
342 let b:PHP_indentinghuge = 0
343 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000344 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000345 let b:PHP_lastindented = v:lnum
346 let b:PHP_LastIndentedWasComment=0
347 let b:PHP_InsideMultilineComment=0
348 let b:PHP_indentbeforelast = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000349
Bram Moolenaar1e015462005-09-25 22:16:38 +0000350 let b:InPHPcode = 0
351 let b:InPHPcode_checked = 0
352 let b:InPHPcode_and_script = 0
353 let b:InPHPcode_tofind = ""
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000354
Bram Moolenaar1e015462005-09-25 22:16:38 +0000355 elseif v:lnum > b:PHP_lastindented
356 let real_PHP_lastindented = b:PHP_lastindented
357 let b:PHP_lastindented = v:lnum
358 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000359
Bram Moolenaar1e015462005-09-25 22:16:38 +0000360
361 if !b:InPHPcode_checked " {{{ One time check
362 let b:InPHPcode_checked = 1
363
364 let synname = IslinePHP (prevnonblank(v:lnum), "")
365
366 if synname!=""
367 if synname != "phpHereDoc"
368 let b:InPHPcode = 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000369 let b:InPHPcode_tofind = ""
370
Bram Moolenaar1e015462005-09-25 22:16:38 +0000371 if synname == "phpComment"
372 let b:UserIsTypingComment = 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000373 else
Bram Moolenaar1e015462005-09-25 22:16:38 +0000374 let b:UserIsTypingComment = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000375 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000376
Bram Moolenaar1e015462005-09-25 22:16:38 +0000377 if synname =~? '^javaScript'
378 let b:InPHPcode_and_script = 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000379 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000380
Bram Moolenaar1e015462005-09-25 22:16:38 +0000381 else
382 let b:InPHPcode = 0
383 let b:UserIsTypingComment = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000384
Bram Moolenaar1e015462005-09-25 22:16:38 +0000385 let lnum = v:lnum - 1
386 while getline(lnum) !~? '<<<\a\w*$' && lnum > 1
387 let lnum = lnum - 1
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000388 endwhile
389
Bram Moolenaar1e015462005-09-25 22:16:38 +0000390 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<\(\a\w*\)\c', '^\\s*\1;$', '')
391 endif
392 else
393 let b:InPHPcode = 0
394 let b:UserIsTypingComment = 0
395 let b:InPHPcode_tofind = '<?\%(.*?>\)\@!\|<script.*>'
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000396 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000397 endif "!b:InPHPcode_checked }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000398
399
Bram Moolenaar1e015462005-09-25 22:16:38 +0000400 " Test if we are indenting PHP code {{{
401 let lnum = prevnonblank(v:lnum - 1)
402 let last_line = getline(lnum)
403
404 if b:InPHPcode_tofind!=""
405 if cline =~? b:InPHPcode_tofind
406 let b:InPHPcode = 1
407 let b:InPHPcode_tofind = ""
408 let b:UserIsTypingComment = 0
409 if cline =~ '\*/'
410 call cursor(v:lnum, 1)
411 if cline !~ '^\*/'
412 call search('\*/', 'W')
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000413 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000414 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000415
Bram Moolenaar1e015462005-09-25 22:16:38 +0000416 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000417
Bram Moolenaar1e015462005-09-25 22:16:38 +0000418 let b:PHP_LastIndentedWasComment = 0
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000419
Bram Moolenaar1e015462005-09-25 22:16:38 +0000420 if cline =~ '^\s*\*/'
421 return indent(lnum) + 1
422 else
423 return indent(lnum)
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000424 endif
425
Bram Moolenaar1e015462005-09-25 22:16:38 +0000426 elseif cline =~? '<script\>'
427 let b:InPHPcode_and_script = 1
428 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000429 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000430 endif
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000431
Bram Moolenaar1e015462005-09-25 22:16:38 +0000432 if b:InPHPcode
433
434 if !b:InPHPcode_and_script && last_line =~ '\%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP(lnum, '?>')=="Delimiter"
435 if cline !~? s:PHP_startindenttag
436 let b:InPHPcode = 0
437 let b:InPHPcode_tofind = s:PHP_startindenttag
438 elseif cline =~? '<script\>'
439 let b:InPHPcode_and_script = 1
440 endif
441
442 elseif last_line =~? '<<<\a\w*$'
443 let b:InPHPcode = 0
444 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<\(\a\w*\)\c', '^\\s*\1;$', '')
445
446 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*'
447 let b:InPHPcode = 0
448 let b:InPHPcode_tofind = '\*/'
449
450 elseif cline =~? '^\s*</script>'
451 let b:InPHPcode = 0
452 let b:InPHPcode_tofind = s:PHP_startindenttag
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000453 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000454 endif " }}}
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000455
Bram Moolenaar1e015462005-09-25 22:16:38 +0000456 if !b:InPHPcode && !b:InPHPcode_and_script
457 return -1
458 endif
459
460
461 " Indent successive // or # comment the same way the first is {{{
462 if cline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
463 if b:PHP_LastIndentedWasComment == 1
464 return indent(real_PHP_lastindented)
465 endif
466 let b:PHP_LastIndentedWasComment = 1
467 else
468 let b:PHP_LastIndentedWasComment = 0
469 endif " }}}
470
471 " Indent multiline /* comments correctly {{{
472
473 if b:PHP_InsideMultilineComment || b:UserIsTypingComment
474 if cline =~ '^\s*\*\%(\/\)\@!'
475 if last_line =~ '^\s*/\*'
476 return indent(lnum) + 1
477 else
478 return indent(lnum)
479 endif
480 else
481 let b:PHP_InsideMultilineComment = 0
482 endif
483 endif
484
485 if !b:PHP_InsideMultilineComment && cline =~ '^\s*/\*'
486 let b:PHP_InsideMultilineComment = 1
487 return -1
488 endif " }}}
489
490
491 " Things always indented at col 1 (PHP delimiter: <?, ?>, Heredoc end) {{{
492 if cline =~# '^\s*<?' && cline !~ '?>'
493 return 0
494 endif
495
496 if cline =~ '^\s*?>' && cline !~# '<?'
497 return 0
498 endif
499
500 if cline =~? '^\s*\a\w*;$' && cline !~? s:notPhpHereDoc
501 return 0
502 endif " }}}
503
504 let s:level = 0
505
506 let lnum = GetLastRealCodeLNum(v:lnum - 1)
507 let last_line = getline(lnum)
508 let ind = indent(lnum)
509 let endline= s:endline
510
511 if ind==0 && b:PHP_default_indenting
512 let ind = b:PHP_default_indenting
513 endif
514
515 if lnum == 0
516 return b:PHP_default_indenting
517 endif
518
519
520 if cline =~ '^\s*}\%(}}\)\@!'
521 let ind = indent(FindOpenBracket(v:lnum))
522 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000523 return ind
Bram Moolenaar1e015462005-09-25 22:16:38 +0000524 endif
525
526 if cline =~ '^\s*\*/'
527 call cursor(v:lnum, 1)
528 if cline !~ '^\*/'
529 call search('\*/', 'W')
530 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000531 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
Bram Moolenaar1e015462005-09-25 22:16:38 +0000532
533 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
534
535 if cline =~ '^\s*\*/'
536 return indent(lnum) + 1
537 else
538 return indent(lnum)
539 endif
540 endif
541
542 let defaultORcase = '^\s*\%(default\|case\).*:'
543
544 if last_line =~ '[;}]'.endline && last_line !~# defaultORcase
545 if ind==b:PHP_default_indenting
546 return b:PHP_default_indenting
547 elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline
548 return b:PHP_CurrentIndentLevel
549 endif
550 endif
551
552 let LastLineClosed = 0
553
554 let terminated = '\%(;\%(\s*?>\)\=\|<<<\a\w*\|}\)'.endline
555
556 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline
557
558 if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>'
559 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
560 return indent(FindTheIfOfAnElse(v:lnum, 1))
561 elseif cline =~ '^\s*{'
562 let previous_line = last_line
563 let last_line_num = lnum
564
565 while last_line_num > 1
566
567 if previous_line =~ '^\s*\%(' . s:blockstart . '\|\%([a-zA-Z]\s*\)*function\)' && previous_line !~ '^\s*[|&]'
568
569 let ind = indent(last_line_num)
570
571 if b:PHP_BracesAtCodeLevel
572 let ind = ind + &sw
573 endif
574
575 return ind
576 endif
577
578 let last_line_num = last_line_num - 1
579 let previous_line = getline(last_line_num)
580 endwhile
581
582 elseif last_line =~# unstated && cline !~ '^\s*{\|^\s*);\='.endline
583 let ind = ind + &sw
584 return ind
585
586 elseif ind != b:PHP_default_indenting && last_line =~ terminated
587 let previous_line = last_line
588 let last_line_num = lnum
589 let LastLineClosed = 1
590
591 while 1
592 if previous_line =~ '^\s*}'
593 let last_line_num = FindOpenBracket(last_line_num)
594
595 if getline(last_line_num) =~ '^\s*{'
596 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
597 endif
598
599 let previous_line = getline(last_line_num)
600
601 continue
602 else
603
604 if getline(last_line_num) =~# '^\s*else\%(if\)\=\>'
605 let last_line_num = FindTheIfOfAnElse(last_line_num, 0)
606 continue
607 endif
608
609
610 let last_match = last_line_num
611
612 let one_ahead_indent = indent(last_line_num)
613 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
614 let two_ahead_indent = indent(last_line_num)
615 let after_previous_line = previous_line
616 let previous_line = getline(last_line_num)
617
618
619 if previous_line =~# defaultORcase.'\|{'.endline
620 break
621 endif
622
623 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline
624 break
625 endif
626
627 if one_ahead_indent == two_ahead_indent || last_line_num < 1
628 if previous_line =~# '[;}]'.endline || last_line_num < 1
629 break
630 endif
631 endif
632 endif
633 endwhile
634
635 if indent(last_match) != ind
636 let ind = indent(last_match)
637 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
638
639 if cline =~# defaultORcase
640 let ind = ind - &sw
641 endif
642 return ind
643 endif
644 endif
645
646 let plinnum = GetLastRealCodeLNum(lnum - 1)
647 let pline = getline(plinnum)
648
649 let last_line = substitute(last_line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','')
650
651
652 if ind == b:PHP_default_indenting
653 if last_line =~ terminated
654 let LastLineClosed = 1
655 endif
656 endif
657
658 if !LastLineClosed
659
660 if last_line =~# '[{(]'.endline || last_line =~? '\h\w*\s*(.*,$' && pline !~ '[,(]'.endline
661
662 if !b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{'
663 let ind = ind + &sw
664 endif
665
666 if b:PHP_BracesAtCodeLevel || cline !~# defaultORcase
667 let b:PHP_CurrentIndentLevel = ind
668 return ind
669 endif
670
671 elseif last_line =~ '\S\+\s*),'.endline
672 call cursor(lnum, 1)
673 call search('),'.endline, 'W')
674 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
675 if openedparent != lnum
676 let ind = indent(openedparent)
677 endif
678
679
680 elseif cline !~ '^\s*{' && pline =~ '\%(;\%(\s*?>\)\=\|<<<\a\w*\|{\|^\s*'.s:blockstart.'\s*(.*)\)'.endline.'\|^\s*}\|'.defaultORcase
681
682 let ind = ind + &sw
683
684 endif
685
686 elseif last_line =~# defaultORcase
687 let ind = ind + &sw
688 endif
689
690 if cline =~ '^\s*);\='
691 let ind = ind - &sw
692 elseif cline =~# defaultORcase
693 let ind = ind - &sw
694
695 endif
696
697 let b:PHP_CurrentIndentLevel = ind
698 return ind
Bram Moolenaar9ff70112005-07-11 22:29:03 +0000699endfunction
700
Bram Moolenaar1e015462005-09-25 22:16:38 +0000701" vim: set ts=8 sw=4 sts=4: