2015-03-07 17:02:15 -05:00
|
|
|
" vimtex - LaTeX plugin for Vim
|
2014-07-22 18:08:57 -04:00
|
|
|
"
|
|
|
|
" Maintainer: Karl Yngve Lervåg
|
|
|
|
" Email: karl.yngve@gmail.com
|
|
|
|
"
|
|
|
|
|
2015-03-19 10:12:12 -04:00
|
|
|
if exists('b:did_indent')
|
2013-10-10 08:47:08 -04:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let b:did_indent = 1
|
2016-02-23 15:03:53 -05:00
|
|
|
let b:did_vimtex_indent = 1
|
2015-01-26 09:41:40 -05:00
|
|
|
|
2015-03-07 17:02:15 -05:00
|
|
|
call vimtex#util#set_default('g:vimtex_indent_enabled', 1)
|
|
|
|
if !g:vimtex_indent_enabled | finish | endif
|
2015-01-26 09:41:40 -05:00
|
|
|
|
2013-10-10 08:47:08 -04:00
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
|
|
|
setlocal autoindent
|
2015-03-19 10:12:41 -04:00
|
|
|
setlocal indentexpr=VimtexIndent()
|
2013-10-10 08:47:08 -04:00
|
|
|
setlocal indentkeys&
|
2015-10-26 17:43:36 -04:00
|
|
|
setlocal indentkeys+=[,(,{,),},],\&,=item
|
2013-10-10 08:47:08 -04:00
|
|
|
|
2015-03-19 10:12:41 -04:00
|
|
|
function! VimtexIndent() " {{{1
|
2013-10-10 08:47:08 -04:00
|
|
|
" Find a non-blank non-comment line above the current line
|
|
|
|
let lnum = prevnonblank(v:lnum - 1)
|
2015-03-19 10:12:12 -04:00
|
|
|
while lnum != 0 && getline(lnum) =~# '^\s*%'
|
2013-10-10 08:47:08 -04:00
|
|
|
let lnum = prevnonblank(lnum - 1)
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
" Zero indent for top of file
|
|
|
|
if lnum == 0
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Get current and previous line, remove comments
|
|
|
|
let cline = substitute(getline(v:lnum), '\\\@<!%.*', '', '')
|
|
|
|
let pline = substitute(getline(lnum), '\\\@<!%.*', '', '')
|
|
|
|
|
|
|
|
" Check for verbatim modes
|
2015-03-19 10:12:12 -04:00
|
|
|
if synIDattr(synID(v:lnum, indent(v:lnum), 1), 'name') ==# 'texZone'
|
2013-10-10 08:47:08 -04:00
|
|
|
if empty(cline)
|
|
|
|
return indent(lnum)
|
|
|
|
else
|
|
|
|
return indent(v:lnum)
|
|
|
|
end
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Align on ampersands
|
2015-03-19 10:12:12 -04:00
|
|
|
if cline =~# '^\s*&' && pline =~# '\\\@<!&.*'
|
|
|
|
return indent(v:lnum) + match(pline, '\\\@<!&') - stridx(cline, '&')
|
2013-10-10 08:47:08 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Find previous non-empty non-comment non-ampersand line
|
2015-03-19 10:12:12 -04:00
|
|
|
while lnum != 0 && (match(pline, '\\\@<!&') != -1 || pline =~# '^\s*%')
|
2013-10-10 08:47:08 -04:00
|
|
|
let lnum = prevnonblank(lnum - 1)
|
|
|
|
let pline = getline(lnum)
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
" Zero indent for top of file
|
|
|
|
if lnum == 0
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Use previous indentation for comments
|
2015-03-19 10:12:12 -04:00
|
|
|
if cline =~# '^\s*%'
|
2013-10-10 08:47:08 -04:00
|
|
|
return indent(v:lnum)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let ind = indent(lnum)
|
|
|
|
|
|
|
|
" Add indent on begin environment
|
2015-03-19 10:12:12 -04:00
|
|
|
if pline =~# '\\begin{.*}' && pline !~ s:envs_noindent
|
2013-10-10 08:47:08 -04:00
|
|
|
let ind = ind + &sw
|
|
|
|
|
|
|
|
" Add extra indent for list environments
|
|
|
|
if pline =~ s:envs_lists
|
|
|
|
let ind = ind + &sw
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Subtract indent on end environment
|
2015-03-19 10:12:12 -04:00
|
|
|
if cline =~# '\\end{.*}' && cline !~ s:envs_noindent
|
2013-10-10 08:47:08 -04:00
|
|
|
let ind = ind - &sw
|
|
|
|
|
|
|
|
" Subtract extra indent for list environments
|
|
|
|
if cline =~ s:envs_lists
|
|
|
|
let ind = ind - &sw
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Indent opening and closing delimiters
|
2016-02-06 14:21:27 -05:00
|
|
|
let [l:re_open, l:re_close] = vimtex#delim#get_valid_regexps(v:lnum, col('.'))
|
2016-01-08 17:45:37 -05:00
|
|
|
let ind += &sw*(
|
2016-02-06 14:21:27 -05:00
|
|
|
\ max([s:count(pline, l:re_open) - s:count(pline, l:re_close), 0])
|
|
|
|
\ - max([s:count(cline, l:re_close) - s:count(cline, l:re_open), 0]))
|
2015-08-25 15:38:05 -04:00
|
|
|
|
2013-10-10 08:47:08 -04:00
|
|
|
" Indent list items
|
2015-03-19 10:12:12 -04:00
|
|
|
if pline =~# '^\s*\\\(bib\)\?item'
|
2013-10-10 08:47:08 -04:00
|
|
|
let ind += &sw
|
|
|
|
endif
|
2015-03-19 10:12:12 -04:00
|
|
|
if cline =~# '^\s*\\\(bib\)\?item'
|
2013-10-10 08:47:08 -04:00
|
|
|
let ind -= &sw
|
|
|
|
endif
|
|
|
|
|
2014-04-14 02:48:33 -04:00
|
|
|
" Indent tikz elements
|
2016-02-23 16:13:06 -05:00
|
|
|
if pline =~# s:tikz_commands && pline !~# ';'
|
2014-04-14 02:48:33 -04:00
|
|
|
let ind += &sw
|
2016-02-23 16:13:06 -05:00
|
|
|
elseif pline !~# s:tikz_commands && pline =~# ';'
|
2014-04-14 02:48:33 -04:00
|
|
|
let ind -= &sw
|
|
|
|
endif
|
|
|
|
|
2013-10-10 08:47:08 -04:00
|
|
|
return ind
|
|
|
|
endfunction
|
|
|
|
"}}}
|
2016-01-08 17:45:37 -05:00
|
|
|
function! s:count(line, pattern) " {{{1
|
2013-10-10 08:47:08 -04: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 17:02:15 -05:00
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
|
|
|
" {{{1 Script variables
|
|
|
|
|
|
|
|
" Define some common patterns
|
|
|
|
let s:envs_lists = 'itemize\|description\|enumerate\|thebibliography'
|
|
|
|
let s:envs_noindent = 'document\|verbatim\|lstlisting'
|
2016-02-23 16:13:06 -05:00
|
|
|
let s:tikz_commands = '\v\\%(' . join([
|
2015-03-07 17:02:15 -05:00
|
|
|
\ 'draw',
|
|
|
|
\ 'fill',
|
|
|
|
\ 'path',
|
|
|
|
\ 'node',
|
2016-02-23 16:13:06 -05:00
|
|
|
\ 'coordinate',
|
|
|
|
\ 'add%(legendentry|plot)',
|
|
|
|
\ ], '|') . ')'
|
2015-03-07 17:02:15 -05:00
|
|
|
|
2013-10-10 08:47:08 -04:00
|
|
|
" }}}1
|
|
|
|
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
2014-12-08 14:44:17 -05:00
|
|
|
" vim: fdm=marker sw=2
|