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
|
2016-12-05 16:23:51 -05:00
|
|
|
call vimtex#util#set_default('g:vimtex_indent_ignored_envs', [
|
|
|
|
\ 'document',
|
|
|
|
\])
|
2015-01-26 09:41:40 -05:00
|
|
|
|
2017-02-01 07:07:12 -05:00
|
|
|
let s:cpo_save = &cpoptions
|
|
|
|
set cpoptions&vim
|
2013-10-10 08:47:08 -04:00
|
|
|
|
|
|
|
setlocal autoindent
|
2016-08-01 16:57:43 -04:00
|
|
|
setlocal indentexpr=VimtexIndent(v:lnum)
|
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
|
|
|
|
2016-08-01 16:57:43 -04:00
|
|
|
function! VimtexIndent(lnum) " {{{1
|
2016-10-13 03:49:28 -04:00
|
|
|
let l:prev_lnum = s:get_prev_line(prevnonblank(a:lnum - 1))
|
|
|
|
if l:prev_lnum == 0 | return indent(a:lnum) | endif
|
2013-10-10 08:47:08 -04:00
|
|
|
|
2016-03-24 07:52:45 -04:00
|
|
|
" Get current and previous line and remove comments
|
2016-10-13 03:49:28 -04:00
|
|
|
let l:line = substitute(getline(a:lnum), '\\\@<!%.*', '', '')
|
|
|
|
let l:prev_line = substitute(getline(l:prev_lnum), '\\\@<!%.*', '', '')
|
2013-10-10 08:47:08 -04:00
|
|
|
|
|
|
|
" Check for verbatim modes
|
2016-10-13 03:49:28 -04:00
|
|
|
if s:is_verbatim(l:line, a:lnum)
|
|
|
|
return empty(l:line) ? indent(l:prev_lnum) : indent(a:lnum)
|
2013-10-10 08:47:08 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Align on ampersands
|
2016-12-30 16:27:57 -05:00
|
|
|
if get(g:, 'vimtex_indent_on_ampersands', 1)
|
|
|
|
\ && l:line =~# '^\s*&' && l:prev_line =~# '\\\@<!&.*'
|
2016-10-13 03:49:28 -04:00
|
|
|
return indent(a:lnum) + match(l:prev_line, '\\\@<!&') - stridx(l:line, '&')
|
2016-03-24 07:52:45 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Use previous indentation for comments
|
2016-10-13 03:49:28 -04:00
|
|
|
if l:line =~# '^\s*%'
|
2016-08-01 16:57:43 -04:00
|
|
|
return indent(a:lnum)
|
2013-10-10 08:47:08 -04:00
|
|
|
endif
|
|
|
|
|
2016-10-13 03:49:28 -04:00
|
|
|
" Ensure previous line does not start with ampersand
|
|
|
|
let l:prev_lnum = s:get_prev_line(l:prev_lnum, 'ignore-ampersands')
|
|
|
|
if l:prev_lnum == 0 | return 0 | endif
|
|
|
|
let l:prev_line = substitute(getline(l:prev_lnum), '\\\@<!%.*', '', '')
|
2013-10-10 08:47:08 -04:00
|
|
|
|
2016-10-13 03:49:28 -04:00
|
|
|
" Indent environments, delimiters, and tikz
|
|
|
|
let l:ind = indent(l:prev_lnum)
|
|
|
|
let l:ind += s:indent_envs(l:line, l:prev_line)
|
|
|
|
let l:ind += s:indent_delims(l:line, a:lnum, l:prev_line, l:prev_lnum)
|
|
|
|
let l:ind += s:indent_tikz(l:prev_lnum, l:prev_line)
|
2016-03-24 17:25:16 -04:00
|
|
|
return l:ind
|
|
|
|
endfunction
|
|
|
|
"}}}
|
2013-10-10 08:47:08 -04:00
|
|
|
|
2016-09-17 16:24:20 -04:00
|
|
|
function! s:get_prev_line(lnum, ...) " {{{1
|
|
|
|
let l:ignore_amps = a:0 > 0
|
2016-03-27 18:22:10 -04:00
|
|
|
let l:lnum = a:lnum
|
|
|
|
let l:prev = getline(l:lnum)
|
|
|
|
|
|
|
|
while l:lnum != 0
|
2016-09-17 16:24:20 -04:00
|
|
|
\ && (l:prev =~# '^\s*%'
|
|
|
|
\ || s:is_verbatim(l:prev, l:lnum)
|
|
|
|
\ || !l:ignore_amps && match(l:prev, '^\s*&') >= 0)
|
2016-03-27 18:22:10 -04:00
|
|
|
let l:lnum = prevnonblank(l:lnum - 1)
|
|
|
|
let l:prev = getline(l:lnum)
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
return l:lnum
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
function! s:is_verbatim(line, lnum) " {{{1
|
2017-01-10 17:15:41 -05:00
|
|
|
return a:line !~# '\v\\%(begin|end)\{%(verbatim|lstlisting|minted)'
|
|
|
|
\ && vimtex#env#is_inside('\%(lstlisting\|verbatim\|minted\)')
|
2016-03-27 18:22:10 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
2016-03-24 17:25:16 -04:00
|
|
|
function! s:indent_envs(cur, prev) " {{{1
|
|
|
|
let l:ind = 0
|
2013-10-10 08:47:08 -04:00
|
|
|
|
2016-03-24 18:13:32 -04:00
|
|
|
" First for general environments
|
2016-12-05 16:23:51 -05:00
|
|
|
let l:ind += &sw*((a:prev =~# '\\begin{.*}') && (a:prev !~# s:envs_ignored))
|
|
|
|
let l:ind -= &sw*((a:cur =~# '\\end{.*}') && (a:cur !~# s:envs_ignored))
|
2013-10-10 08:47:08 -04:00
|
|
|
|
2016-03-24 18:13:32 -04: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 08:47:08 -04:00
|
|
|
|
2016-03-24 17:25:16 -04:00
|
|
|
return l:ind
|
|
|
|
endfunction
|
2015-08-25 15:38:05 -04:00
|
|
|
|
2016-12-05 16:23:51 -05:00
|
|
|
let s:envs_ignored = '\v' . join(g:vimtex_indent_ignored_envs, '|')
|
2016-03-24 17:25:16 -04:00
|
|
|
let s:envs_lists = 'itemize\|description\|enumerate\|thebibliography'
|
2016-03-24 18:13:32 -04: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 17:25:16 -04:00
|
|
|
|
|
|
|
" }}}1
|
2016-10-13 03:49:28 -04:00
|
|
|
function! s:indent_delims(line, lnum, prev_line, prev_lnum) " {{{1
|
2016-10-27 10:04:38 -04:00
|
|
|
if empty(s:re_delims) | return 0 | endif
|
|
|
|
|
2016-10-31 16:32:55 -04:00
|
|
|
let l:pre = s:split(a:prev_line, a:prev_lnum)
|
|
|
|
let l:cur = s:split(a:line, a:lnum)
|
|
|
|
|
|
|
|
return &sw*( max([ s:count(l:pre.math_post, s:re_delims[0])
|
|
|
|
\ - s:count(l:pre.math_post, s:re_delims[1])
|
|
|
|
\ + s:count(l:pre.text, s:re_delims[2])
|
|
|
|
\ - s:count(l:pre.text, s:re_delims[3]), 0])
|
|
|
|
\ - max([ s:count(l:cur.math_pre, s:re_delims[1])
|
|
|
|
\ - s:count(l:cur.math_pre, s:re_delims[0])
|
|
|
|
\ + s:count(l:cur.text, s:re_delims[3])
|
|
|
|
\ - s:count(l:cur.text, s:re_delims[2]), 0]))
|
2016-03-24 17:25:16 -04:00
|
|
|
endfunction
|
2013-10-10 08:47:08 -04:00
|
|
|
|
2016-10-13 03:49:28 -04:00
|
|
|
"
|
|
|
|
" This fetches the regexes for delimiters in text and math mode:
|
|
|
|
" s:re_delims[0] == math open
|
|
|
|
" s:re_delims[1] == math close
|
|
|
|
" s:re_delims[2] == text open
|
|
|
|
" s:re_delims[3] == text close
|
|
|
|
"
|
|
|
|
let s:re_delims = vimtex#delim#get_delim_regexes()
|
2016-10-11 16:56:50 -04:00
|
|
|
|
2016-10-31 16:13:26 -04:00
|
|
|
" }}}1
|
|
|
|
function! s:indent_tikz(lnum, prev) " {{{1
|
2017-01-10 17:14:06 -05:00
|
|
|
if !has_key(b:vimtex.packages, 'tikz') | return 0 | endif
|
|
|
|
|
2016-12-17 06:13:21 -05:00
|
|
|
let l:env_lnum = vimtex#env#is_inside('tikzpicture')
|
|
|
|
if l:env_lnum > 0 && l:env_lnum < a:lnum
|
2016-10-31 16:13:26 -04:00
|
|
|
let l:prev_starts = a:prev =~# s:tikz_commands
|
|
|
|
let l:prev_stops = a:prev =~# ';\s*$'
|
|
|
|
|
|
|
|
" Increase indent on tikz command start
|
|
|
|
if l:prev_starts && ! l:prev_stops
|
|
|
|
return &sw
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Decrease indent on tikz command end, i.e. on semicolon
|
|
|
|
if ! l:prev_starts && l:prev_stops
|
2016-12-17 06:13:21 -05:00
|
|
|
let l:context = join(getline(l:env_lnum, a:lnum-1), '')
|
2016-10-31 16:13:26 -04:00
|
|
|
return -&sw*(l:context =~# s:tikz_commands)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
return 0
|
2016-10-11 16:56:50 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-10-31 16:13:26 -04:00
|
|
|
let s:tikz_commands = '\v\\%(' . join([
|
|
|
|
\ 'draw',
|
|
|
|
\ 'fill',
|
|
|
|
\ 'path',
|
|
|
|
\ 'node',
|
|
|
|
\ 'coordinate',
|
|
|
|
\ 'add%(legendentry|plot)',
|
|
|
|
\ ], '|') . ')'
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
|
|
|
"
|
|
|
|
" Utility functions for s:indent_delims
|
|
|
|
"
|
2016-10-31 16:32:55 -04:00
|
|
|
function! s:split(line, lnum) " {{{1
|
2016-10-31 16:40:01 -04:00
|
|
|
"
|
|
|
|
" This function splits a line into regions:
|
|
|
|
"
|
|
|
|
" text is all the normal text in the given line
|
|
|
|
" math_pre is the math region at the beginning of the line (if any)
|
|
|
|
" math_post is the math region at the end of the line (if any)
|
|
|
|
"
|
|
|
|
" Any math region that is not located at the beginning or end of the line
|
|
|
|
" will be ignored.
|
|
|
|
"
|
2016-10-31 16:32:55 -04:00
|
|
|
let l:result = {}
|
|
|
|
let l:result.text = ''
|
|
|
|
let l:result.math_pre = ''
|
|
|
|
let l:result.math_post = ''
|
2016-10-11 16:56:50 -04:00
|
|
|
|
2016-10-27 17:23:39 -04:00
|
|
|
call setpos('.', [0, a:lnum, 1, 0])
|
2016-10-31 16:32:55 -04:00
|
|
|
let l:strlen = strlen(a:line)
|
|
|
|
let l:cnum = 0
|
2016-10-27 17:23:39 -04:00
|
|
|
|
2016-10-31 16:40:01 -04:00
|
|
|
"
|
|
|
|
" Handles the case where the start of the line is a math region
|
|
|
|
"
|
2016-10-31 16:32:55 -04:00
|
|
|
if vimtex#util#in_mathzone()
|
2016-10-27 17:23:39 -04:00
|
|
|
let l:open = vimtex#delim#get_prev('env_math', 'open')
|
|
|
|
if !empty(l:open) && l:open.lnum == a:lnum
|
2016-10-31 16:32:55 -04:00
|
|
|
let l:cnum = strlen(l:open.match)
|
|
|
|
let l:result.text .= strpart(a:line, 0, l:cnum)
|
2016-10-27 17:23:39 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
let l:close = vimtex#delim#get_next('env_math', 'close')
|
2016-10-31 16:32:55 -04:00
|
|
|
if empty(l:close) || l:close.lnum > a:lnum
|
|
|
|
let l:result.math_post = strpart(a:line, l:cnum)
|
|
|
|
if l:cnum == 0
|
|
|
|
let l:result.math_pre = l:result.math_post
|
|
|
|
endif
|
|
|
|
return s:strip(l:result)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if l:cnum == 0
|
|
|
|
let l:result.math_pre = strpart(a:line, l:cnum, l:close.cnum-1)
|
|
|
|
endif
|
|
|
|
let l:cnum = l:close.cnum-1
|
2016-10-27 17:23:39 -04:00
|
|
|
call setpos('.', [0, a:lnum, l:close.cnum+strlen(l:close.match), 0])
|
|
|
|
endif
|
|
|
|
|
2016-10-31 16:40:01 -04:00
|
|
|
"
|
|
|
|
" Iterate over all math regions in the line
|
|
|
|
"
|
2016-10-27 17:23:39 -04:00
|
|
|
while 1
|
|
|
|
let l:open = vimtex#delim#get_next('env_math', 'open')
|
|
|
|
if empty(l:open)
|
|
|
|
\ || l:open.lnum > a:lnum
|
2016-10-31 16:32:55 -04:00
|
|
|
\ || l:open.cnum + strlen(l:open.match) > l:strlen
|
|
|
|
let l:result.text .= strpart(a:line, l:cnum)
|
|
|
|
return s:strip(l:result)
|
|
|
|
else
|
|
|
|
let l:result.text .= strpart(a:line, l:cnum,
|
|
|
|
\ l:open.cnum + strlen(l:open.match) - l:cnum - 1)
|
|
|
|
let l:cnum = l:open.cnum+strlen(l:open.match)-1
|
|
|
|
call setpos('.', [0, a:lnum, l:open.cnum+strlen(l:open.match), 0])
|
|
|
|
endif
|
2016-10-27 17:23:39 -04:00
|
|
|
|
|
|
|
let l:close = vimtex#delim#get_matching(l:open)
|
2016-10-31 16:32:55 -04:00
|
|
|
if empty(l:close) || l:close.lnum == 0 || l:close.lnum > a:lnum
|
|
|
|
let l:result.math_post = strpart(a:line, l:cnum)
|
|
|
|
return s:strip(l:result)
|
|
|
|
else
|
|
|
|
let l:cnum = l:close.cnum-1
|
|
|
|
call setpos('.', [0, a:lnum, l:close.cnum+strlen(l:close.match), 0])
|
|
|
|
endif
|
2016-10-27 17:23:39 -04:00
|
|
|
endwhile
|
|
|
|
endfunction
|
|
|
|
|
2016-10-31 16:32:55 -04:00
|
|
|
" }}}1
|
|
|
|
function! s:strip(result) " {{{1
|
2016-10-31 16:40:01 -04:00
|
|
|
let a:result.text = substitute(a:result.text, '\\verb\(.\).\{}\1', '', 'g')
|
2016-10-31 16:32:55 -04:00
|
|
|
return a:result
|
|
|
|
endfunction
|
|
|
|
|
2016-03-24 17:25:16 -04:00
|
|
|
" }}}1
|
2016-10-31 16:13:26 -04:00
|
|
|
function! s:count(line, pattern) " {{{1
|
|
|
|
let l:sum = 0
|
|
|
|
let l:indx = match(a:line, a:pattern)
|
|
|
|
while l:indx >= 0
|
|
|
|
let l:sum += 1
|
|
|
|
let l:match = matchstr(a:line, a:pattern, l:indx)
|
|
|
|
let l:indx += len(l:match)
|
|
|
|
let l:indx = match(a:line, a:pattern, l:indx)
|
|
|
|
endwhile
|
|
|
|
return l:sum
|
2013-10-10 08:47:08 -04:00
|
|
|
endfunction
|
2016-03-24 17:25:16 -04:00
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
2017-02-01 07:07:12 -05:00
|
|
|
let &cpoptions = s:cpo_save
|
2013-10-10 08:47:08 -04:00
|
|
|
unlet s:cpo_save
|
|
|
|
|
2014-12-08 14:44:17 -05:00
|
|
|
" vim: fdm=marker sw=2
|