blob: 83d010ef6bc1d4b098e9745361c2ae775e6140fd [file] [log] [blame]
Bram Moolenaar1e015462005-09-25 22:16:38 +00001" Vim indent file
Bram Moolenaar9964e462007-05-05 17:54:07 +00002" Language: eRuby
3" Maintainer: Tim Pope <vimNOSPAM@tpope.info>
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004" Info: $Id$
5" URL: http://vim-ruby.rubyforge.org
6" Anon CVS: See above site
7" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
Bram Moolenaar1e015462005-09-25 22:16:38 +00008
Bram Moolenaar1e015462005-09-25 22:16:38 +00009if exists("b:did_indent")
10 finish
11endif
12
Bram Moolenaar9964e462007-05-05 17:54:07 +000013runtime! indent/ruby.vim
14unlet! b:did_indent
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000015set indentexpr=
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000016
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000017if exists("b:eruby_subtype")
18 exe "runtime! indent/".b:eruby_subtype.".vim"
19else
20 runtime! indent/html.vim
21endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000022unlet! b:did_indent
23
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000024if &l:indentexpr == ''
25 if &l:cindent
26 let &l:indentexpr = 'cindent(v:lnum)'
27 else
28 let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
29 endif
30endif
31let b:eruby_subtype_indentexpr = &l:indentexpr
32
Bram Moolenaar9964e462007-05-05 17:54:07 +000033let b:did_indent = 1
34
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000035setlocal indentexpr=GetErubyIndent()
Bram Moolenaar9964e462007-05-05 17:54:07 +000036setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=end,=else,=elsif,=rescue,=ensure,=when
37
38" Only define the function once.
39if exists("*GetErubyIndent")
40 finish
41endif
42
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000043function! GetErubyIndent()
Bram Moolenaar9964e462007-05-05 17:54:07 +000044 let vcol = col('.')
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000045 call cursor(v:lnum,1)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000046 let inruby = searchpair('<%','','%>','W')
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000047 call cursor(v:lnum,vcol)
48 if inruby && getline(v:lnum) !~ '^<%'
Bram Moolenaar9964e462007-05-05 17:54:07 +000049 let ind = GetRubyIndent()
50 else
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000051 exe "let ind = ".b:eruby_subtype_indentexpr
Bram Moolenaar9964e462007-05-05 17:54:07 +000052 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000053 let lnum = prevnonblank(v:lnum-1)
Bram Moolenaar9964e462007-05-05 17:54:07 +000054 let line = getline(lnum)
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000055 let cline = getline(v:lnum)
Bram Moolenaar9964e462007-05-05 17:54:07 +000056 if cline =~# '<%\s*\%(end\|else\|\%(ensure\|rescue\|elsif\|when\).\{-\}\)\s*\%(-\=%>\|$\)'
57 let ind = ind - &sw
58 endif
59 if line =~# '\<do\%(\s*|[^|]*|\)\=\s*-\=%>'
60 let ind = ind + &sw
61 elseif line =~# '<%\s*\%(module\|class\|def\|if\|for\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue\)\>.*%>'
62 let ind = ind + &sw
63 endif
64 if line =~# '^\s*<%[=#]\=\s*$' && cline !~# '^\s*end\>'
65 let ind = ind + &sw
66 endif
67 if cline =~# '^\s*-\=%>\s*$'
68 let ind = ind - &sw
69 endif
70 return ind
71endfunction
72
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000073" vim:set sw=2 sts=2 ts=8 noet: