9aba93d73e06

OH MY GOD INDENTATION
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 24 Jan 2011 15:05:38 -0500
parents def83f37f73e
children 2cf19e613bd2 a0450320e4bc
branches/tags (none)
files vim/.vimrc vim/bundle/django-custom/indent/htmldjango.vim vim/snippets/htmldjango.snippets

Changes

--- a/vim/.vimrc	Mon Jan 24 12:02:34 2011 -0500
+++ b/vim/.vimrc	Mon Jan 24 15:05:38 2011 -0500
@@ -459,5 +459,3 @@
 else
     set nocursorline
 endif
-
-au Filetype html,xml,xsl,htmldjango set nosi indentexpr= autoindent shiftwidth=4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vim/bundle/django-custom/indent/htmldjango.vim	Mon Jan 24 15:05:38 2011 -0500
@@ -0,0 +1,71 @@
+" Vim indent file
+" Language:     htmldjango
+" Maintainer:   Steve Losh <steve@stevelosh.com>
+"
+" Mostly based on indent/eruby.vim
+"
+" To use: save as ~/.vim/indent/htmldjango.vim
+
+if exists("b:did_indent")
+    finish
+endif
+
+runtime! indent/html.vim
+unlet! b:did_indent
+
+if &l:indentexpr == ''
+    if &l:cindent
+        let &l:indentexpr = 'cindent(v:lnum)'
+    else
+        let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
+    endif
+endif
+let b:html_indentexpr = &l:indentexpr
+
+let b:did_indent = 1
+
+setlocal indentexpr=GetDjangoIndent()
+setlocal indentkeys=o,O,*<Return>,{,},o,O,!^F,<>>
+
+" Only define the function once.
+if exists("*GetDjangoIndent")
+    finish
+endif
+
+function! GetDjangoIndent(...)
+    if a:0 && a:1 == '.'
+        let v:lnum = line('.')
+    elseif a:0 && a:1 =~ '^\d'
+        let v:lnum = a:1
+    endif
+    let vcol = col('.')
+
+    call cursor(v:lnum,vcol)
+
+    exe "let ind = ".b:html_indentexpr
+
+    let lnum = prevnonblank(v:lnum-1)
+    let prev_non_blank_line = getline(lnum)
+    let current_line = getline(v:lnum)
+
+    let tagstart = '^\s*' . '{%\s*'
+    let tagend = '.*%}' . '\s*$'
+
+    let blocktags = '\(block\|for\|if\|with\|autoescape\|comment\|filter\|spaceless\)'
+    let midtags = '\(empty\|else\)'
+
+    if prev_non_blank_line =~# tagstart . blocktags . tagend
+        let ind = ind + &sw
+    elseif prev_non_blank_line =~# tagstart . midtags . tagend
+        let ind = ind + &sw
+    endif
+
+    if current_line =~# tagstart . 'end' . blocktags . '.*$'
+        let ind = ind - &sw
+    elseif current_line =~# tagstart . midtags . tagend
+        let ind = ind - &sw
+    endif
+
+    return ind
+endfunction
+
--- a/vim/snippets/htmldjango.snippets	Mon Jan 24 12:02:34 2011 -0500
+++ b/vim/snippets/htmldjango.snippets	Mon Jan 24 15:05:38 2011 -0500
@@ -45,14 +45,12 @@
 	{% endfor %}
 snippet empty
 	{% empty %}
-	${1}
 snippet if
 	{% if ${1} %}
 	${2}
 	{% endif %}
 snippet else
 	{% else %}
-	${1}
 snippet ifchanged
 	{% ifchanged %}${1}{% endifchanged %}
 snippet ifequal