2015-03-07 23:02:15 +01:00
|
|
|
" vimtex - LaTeX plugin for Vim
|
2014-07-23 00:08:57 +02:00
|
|
|
"
|
|
|
|
" Maintainer: Karl Yngve Lervåg
|
|
|
|
" Email: karl.yngve@gmail.com
|
|
|
|
"
|
|
|
|
|
2015-03-19 15:12:12 +01:00
|
|
|
if exists('b:did_indent')
|
2013-10-10 14:47:08 +02:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let b:did_indent = 1
|
2016-02-23 21:03:53 +01:00
|
|
|
let b:did_vimtex_indent = 1
|
2015-01-26 15:41:40 +01:00
|
|
|
|
2015-03-07 23:02:15 +01:00
|
|
|
call vimtex#util#set_default('g:vimtex_indent_enabled', 1)
|
|
|
|
if !g:vimtex_indent_enabled | finish | endif
|
2015-01-26 15:41:40 +01:00
|
|
|
|
2013-10-10 14:47:08 +02:00
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
|
|
|
setlocal autoindent
|
2015-03-19 15:12:41 +01:00
|
|
|
setlocal indentexpr=VimtexIndent()
|
2013-10-10 14:47:08 +02:00
|
|
|
setlocal indentkeys&
|
2015-10-26 22:43:36 +01:00
|
|
|
setlocal indentkeys+=[,(,{,),},],\&,=item
|
2013-10-10 14:47:08 +02:00
|
|
|
|
2015-03-19 15:12:41 +01:00
|
|
|
function! VimtexIndent() " {{{1
|
2013-10-10 14:47:08 +02:00
|
|
|
" Find a non-blank non-comment line above the current line
|
2016-03-24 12:52:45 +01:00
|
|
|
let l:nprev = prevnonblank(v:lnum - 1)
|
|
|
|
while l:nprev != 0 && getline(l:nprev) =~# '^\s*%'
|
|
|
|
let l:nprev = prevnonblank(l:nprev - 1)
|
2013-10-10 14:47:08 +02:00
|
|
|
endwhile
|
2016-03-24 12:52:45 +01:00
|
|
|
if l:nprev == 0
|
2013-10-10 14:47:08 +02:00
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
2016-03-24 12:52:45 +01:00
|
|
|
" Get current and previous line and remove comments
|
|
|
|
let l:cur = substitute(getline(v:lnum), '\\\@<!%.*', '', '')
|
|
|
|
let l:prev = substitute(getline(l:nprev), '\\\@<!%.*', '', '')
|
2013-10-10 14:47:08 +02:00
|
|
|
|
|
|
|
" Check for verbatim modes
|
2015-03-19 15:12:12 +01:00
|
|
|
if synIDattr(synID(v:lnum, indent(v:lnum), 1), 'name') ==# 'texZone'
|
2016-03-24 12:52:45 +01:00
|
|
|
return empty(l:cur) ? indent(l:nprev) : indent(v:lnum)
|
2013-10-10 14:47:08 +02:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Align on ampersands
|
2016-03-24 12:52:45 +01:00
|
|
|
if l:cur =~# '^\s*&' && l:prev =~# '\\\@<!&.*'
|
|
|
|
return indent(v:lnum) + match(l:prev, '\\\@<!&') - stridx(l:cur, '&')
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Use previous indentation for comments
|
|
|
|
if l:cur =~# '^\s*%'
|
|
|
|
return indent(v:lnum)
|
2013-10-10 14:47:08 +02:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Find previous non-empty non-comment non-ampersand line
|
2016-03-24 12:52:45 +01:00
|
|
|
while l:nprev != 0 && (match(l:prev, '\\\@<!&') != -1 || l:prev =~# '^\s*%')
|
|
|
|
let l:nprev = prevnonblank(l:nprev - 1)
|
|
|
|
let l:prev = getline(l:nprev)
|
2013-10-10 14:47:08 +02:00
|
|
|
endwhile
|
2016-03-24 12:52:45 +01:00
|
|
|
if l:nprev == 0
|
2013-10-10 14:47:08 +02:00
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
2016-03-24 12:52:45 +01:00
|
|
|
let l:ind = indent(l:nprev)
|
2016-03-24 22:25:16 +01:00
|
|
|
let l:ind += s:indent_envs(l:cur, l:prev)
|
|
|
|
let l:ind += s:indent_delims(l:cur, l:prev)
|
|
|
|
let l:ind += s:indent_tikz(l:prev)
|
|
|
|
return l:ind
|
|
|
|
endfunction
|
|
|
|
"}}}
|
2013-10-10 14:47:08 +02:00
|
|
|
|
2016-03-24 22:25:16 +01:00
|
|
|
function! s:indent_envs(cur, prev) " {{{1
|
|
|
|
let l:ind = 0
|
2013-10-10 14:47:08 +02:00
|
|
|
|
2016-03-24 23:13:32 +01:00
|
|
|
" First for general environments
|
|
|
|
let l:ind += &sw*((a:prev =~# '\\begin{.*}') && (a:prev !~# s:envs_ignore))
|
|
|
|
let l:ind -= &sw*((a:cur =~# '\\end{.*}') && (a:cur !~# s:envs_ignore))
|
2013-10-10 14:47:08 +02:00
|
|
|
|
2016-03-24 23:13:32 +01:00
|
|
|
" Indentation for prolonged items in lists
|
|
|
|
let l:ind += &sw*((a:prev =~# s:envs_item) && (a:cur !~# s:envs_enditem))
|
|
|
|
let l:ind -= &sw*((a:cur =~# s:envs_item) && (a:prev !~# s:envs_begitem))
|
|
|
|
let l:ind -= &sw*((a:cur =~# s:envs_endlist) && (a:prev !~# s:envs_begitem))
|
2013-10-10 14:47:08 +02:00
|
|
|
|
2016-03-24 22:25:16 +01:00
|
|
|
return l:ind
|
|
|
|
endfunction
|
2015-08-25 21:38:05 +02:00
|
|
|
|
2016-03-24 23:13:32 +01:00
|
|
|
let s:envs_ignore = 'document\|verbatim\|lstlisting'
|
2016-03-24 22:25:16 +01:00
|
|
|
let s:envs_lists = 'itemize\|description\|enumerate\|thebibliography'
|
2016-03-24 23:13:32 +01:00
|
|
|
let s:envs_item = '^\s*\\item'
|
|
|
|
let s:envs_beglist = '\\begin{\%(' . s:envs_lists . '\)'
|
|
|
|
let s:envs_endlist = '\\end{\%(' . s:envs_lists . '\)'
|
|
|
|
let s:envs_begitem = s:envs_item . '\|' . s:envs_beglist
|
|
|
|
let s:envs_enditem = s:envs_item . '\|' . s:envs_endlist
|
2016-03-24 22:25:16 +01:00
|
|
|
|
|
|
|
" }}}1
|
|
|
|
function! s:indent_delims(cur, prev) " {{{1
|
|
|
|
let [l:open, l:close] = vimtex#delim#get_valid_regexps(v:lnum, col('.'))
|
|
|
|
return &sw*( max([s:count(a:prev, l:open) - s:count(a:prev, l:close), 0])
|
|
|
|
\ - max([s:count(a:cur, l:close) - s:count(a:cur, l:open), 0]))
|
|
|
|
endfunction
|
2013-10-10 14:47:08 +02:00
|
|
|
|
2016-03-24 22:25:16 +01:00
|
|
|
" }}}1
|
|
|
|
function! s:indent_tikz(cur, prev) " {{{1
|
|
|
|
if a:prev =~# s:tikz_commands && a:prev !~# ';'
|
2016-03-24 12:52:45 +01:00
|
|
|
let l:ind += &sw
|
2016-03-24 22:25:16 +01:00
|
|
|
elseif a:prev !~# s:tikz_commands && a:prev =~# ';'
|
2016-03-24 12:52:45 +01:00
|
|
|
let l:ind -= &sw
|
2014-04-14 08:48:33 +02:00
|
|
|
endif
|
|
|
|
|
2016-03-24 12:52:45 +01:00
|
|
|
return l:ind
|
2013-10-10 14:47:08 +02:00
|
|
|
endfunction
|
2016-03-24 22:25:16 +01:00
|
|
|
|
|
|
|
let s:tikz_commands = '\v\\%(' . join([
|
|
|
|
\ 'draw',
|
|
|
|
\ 'fill',
|
|
|
|
\ 'path',
|
|
|
|
\ 'node',
|
|
|
|
\ 'coordinate',
|
|
|
|
\ 'add%(legendentry|plot)',
|
|
|
|
\ ], '|') . ')'
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
2016-01-08 23:45:37 +01:00
|
|
|
function! s:count(line, pattern) " {{{1
|
2013-10-10 14:47:08 +02:00
|
|
|
let sum = 0
|
|
|
|
let indx = match(a:line, a:pattern)
|
|
|
|
while indx >= 0
|
|
|
|
let sum += 1
|
|
|
|
let match = matchstr(a:line, a:pattern, indx)
|
|
|
|
let indx += len(match)
|
|
|
|
let indx = match(a:line, a:pattern, indx)
|
|
|
|
endwhile
|
|
|
|
return sum
|
|
|
|
endfunction
|
2015-03-07 23:02:15 +01:00
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
2013-10-10 14:47:08 +02:00
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
2014-12-08 20:44:17 +01:00
|
|
|
" vim: fdm=marker sw=2
|