2013-09-16 19:43:28 -04:00
|
|
|
" LaTeX indent file (part of LaTeX Box)
|
|
|
|
" Maintainer: David Munger (mungerd@gmail.com)
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-22 17:24:04 -04:00
|
|
|
if exists("g:LatexBox_custom_indent") && ! g:LatexBox_custom_indent
|
|
|
|
finish
|
|
|
|
endif
|
2013-09-13 11:28:18 -04:00
|
|
|
if exists("b:did_indent")
|
2013-09-16 19:43:28 -04:00
|
|
|
finish
|
2013-09-13 11:28:18 -04:00
|
|
|
endif
|
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
let b:did_indent = 1
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
setlocal indentexpr=LatexBox_TexIndent()
|
|
|
|
setlocal indentkeys=0=\\end,0=\\end{enumerate},0=\\end{itemize},0=\\end{description},0=\\right,0=\\item,0=\\),0=\\],0},o,O,0\\
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
let s:list_envs = ['itemize', 'enumerate', 'description']
|
|
|
|
" indent on \left( and on \(, but not on (
|
|
|
|
" indent on \left[ and on \[, but not on [
|
|
|
|
" indent on \left\{ and on {, but not on \{
|
2015-03-09 00:32:50 -04:00
|
|
|
let s:open_pat = '\\\@<!\%(\\begin\|\\left\a\@!\|\\(\|\\\[\|{\)'
|
|
|
|
let s:close_pat = '\\\@<!\%(\\end\|\\right\a\@!\|\\)\|\\\]\|}\)'
|
2013-09-16 19:43:28 -04:00
|
|
|
let s:list_open_pat = '\\\@<!\\begin{\%(' . join(s:list_envs, '\|') . '\)}'
|
|
|
|
let s:list_close_pat = '\\\@<!\\end{\%(' . join(s:list_envs, '\|') . '\)}'
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
function! s:CountMatches(str, pat)
|
|
|
|
return len(substitute(substitute(a:str, a:pat, "\n", 'g'), "[^\n]", '', 'g'))
|
|
|
|
endfunction
|
2013-09-13 11:28:18 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" TexIndent {{{
|
|
|
|
function! LatexBox_TexIndent()
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
let lnum_curr = v:lnum
|
|
|
|
let lnum_prev = prevnonblank(lnum_curr - 1)
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
if lnum_prev == 0
|
|
|
|
return 0
|
|
|
|
endif
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
let line_curr = getline(lnum_curr)
|
|
|
|
let line_prev = getline(lnum_prev)
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" remove \\
|
|
|
|
let line_curr = substitute(line_curr, '\\\\', '', 'g')
|
|
|
|
let line_prev = substitute(line_prev, '\\\\', '', 'g')
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" strip comments
|
|
|
|
let line_curr = substitute(line_curr, '\\\@<!%.*$', '', 'g')
|
|
|
|
let line_prev = substitute(line_prev, '\\\@<!%.*$', '', 'g')
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" find unmatched opening patterns on previous line
|
|
|
|
let n = s:CountMatches(line_prev, s:open_pat)-s:CountMatches(line_prev, s:close_pat)
|
|
|
|
let n += s:CountMatches(line_prev, s:list_open_pat)-s:CountMatches(line_prev, s:list_close_pat)
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" reduce indentation if current line starts with a closing pattern
|
|
|
|
if line_curr =~ '^\s*\%(' . s:close_pat . '\)'
|
|
|
|
let n -= 1
|
|
|
|
endif
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" compensate indentation if previous line starts with a closing pattern
|
|
|
|
if line_prev =~ '^\s*\%(' . s:close_pat . '\)'
|
|
|
|
let n += 1
|
|
|
|
endif
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" reduce indentation if current line starts with a closing list
|
|
|
|
if line_curr =~ '^\s*\%(' . s:list_close_pat . '\)'
|
|
|
|
let n -= 1
|
|
|
|
endif
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" compensate indentation if previous line starts with a closing list
|
|
|
|
if line_prev =~ '^\s*\%(' . s:list_close_pat . '\)'
|
|
|
|
let n += 1
|
|
|
|
endif
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" reduce indentation if previous line is \begin{document}
|
|
|
|
if line_prev =~ '\\begin\s*{document}'
|
|
|
|
let n -= 1
|
|
|
|
endif
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" less shift for lines starting with \item
|
|
|
|
let item_here = line_curr =~ '^\s*\\item'
|
|
|
|
let item_above = line_prev =~ '^\s*\\item'
|
|
|
|
if !item_here && item_above
|
|
|
|
let n += 1
|
|
|
|
elseif item_here && !item_above
|
|
|
|
let n -= 1
|
|
|
|
endif
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
return indent(lnum_prev) + n * &sw
|
2013-09-13 11:28:18 -04:00
|
|
|
endfunction
|
2013-09-16 19:43:28 -04:00
|
|
|
" }}}
|
2013-09-13 11:28:18 -04:00
|
|
|
|
2014-11-10 20:37:21 -05:00
|
|
|
" Restore cursor position, window position, and last search after running a
|
|
|
|
" command.
|
|
|
|
function! Latexbox_CallIndent()
|
|
|
|
" Save the current cursor position.
|
|
|
|
let cursor = getpos('.')
|
|
|
|
|
|
|
|
" Save the current window position.
|
|
|
|
normal! H
|
|
|
|
let window = getpos('.')
|
|
|
|
call setpos('.', cursor)
|
|
|
|
|
2015-05-11 09:05:13 -04:00
|
|
|
" Get first non-whitespace character of current line.
|
|
|
|
let line_start_char = matchstr(getline('.'), '\S')
|
|
|
|
|
|
|
|
" Get initial tab position.
|
|
|
|
let initial_tab = stridx(getline('.'), line_start_char)
|
|
|
|
|
2014-11-10 20:37:21 -05:00
|
|
|
" Execute the command.
|
|
|
|
execute 'normal! =='
|
|
|
|
|
2015-05-11 09:05:13 -04:00
|
|
|
" Get tab position difference.
|
|
|
|
let difference = stridx(getline('.'), line_start_char) - initial_tab
|
|
|
|
|
|
|
|
" Set new cursor Y position based on calculated difference.
|
|
|
|
let cursor[2] = cursor[2] + difference
|
|
|
|
|
2014-11-10 20:37:21 -05:00
|
|
|
" Restore the previous window position.
|
|
|
|
call setpos('.', window)
|
|
|
|
normal! zt
|
|
|
|
|
|
|
|
" Restore the previous cursor position.
|
|
|
|
call setpos('.', cursor)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" autocmd to call indent after completion
|
|
|
|
" 7.3.598
|
|
|
|
if v:version > 703 || (v:version == 703 && has('patch598'))
|
|
|
|
augroup LatexBox_Completion
|
|
|
|
autocmd!
|
|
|
|
autocmd CompleteDone <buffer> call Latexbox_CallIndent()
|
|
|
|
augroup END
|
|
|
|
endif
|
|
|
|
|
2013-09-16 19:43:28 -04:00
|
|
|
" vim:fdm=marker:ff=unix:noet:ts=4:sw=4
|