diff --git a/autoload/vimtex/imaps.vim b/autoload/vimtex/imaps.vim index c8b4919..1751743 100644 --- a/autoload/vimtex/imaps.vim +++ b/autoload/vimtex/imaps.vim @@ -91,6 +91,9 @@ endfunction function! vimtex#imaps#init_buffer() " {{{1 if !g:vimtex_imaps_enabled | return | endif + " Container for wrapper contexts + let b:vimtex_context = {} + " " Create imaps " @@ -173,6 +176,13 @@ function! s:create_map(map) " {{{1 return endif + " Some wrappers use a context which must be made available to the wrapper + " function in run time. + if has_key(a:map, 'context') + execute 'let l:key = "' . escape(l:lhs, '<') . '"' + let b:vimtex_context[l:key] = a:map.context + endif + silent execute 'inoremap ' l:lhs \ l:wrapper . '("' . escape(l:lhs, '\') . '", ' . string(a:map.rhs) . ')' @@ -193,6 +203,16 @@ function! vimtex#imaps#wrap_math(lhs, rhs) " {{{1 return s:is_math() ? a:rhs : a:lhs endfunction +" }}}1 +function! vimtex#imaps#wrap_environment(lhs, rhs) " {{{1 + for l:env in b:vimtex_context[a:lhs] + if vimtex#env#is_inside(l:env) + return a:rhs + endif + endfor + return a:lhs +endfunction + " }}}1 " diff --git a/test/vader/imaps.vader b/test/vader/imaps.vader index 1eee598..ff8d73f 100644 --- a/test/vader/imaps.vader +++ b/test/vader/imaps.vader @@ -8,9 +8,15 @@ Execute (Setup): \ 'leader' : '', \ 'wrapper' : 'vimtex#imaps#wrap_trivial', \}) + call vimtex#imaps#add_map({ + \ 'lhs' : 'cool', + \ 'rhs' : '\item', + \ 'leader' : '', + \ 'wrapper' : 'vimtex#imaps#wrap_environment', + \ 'context' : ["itemize", "enumerate"], + \}) silent! VimtexReload -Given: Given tex (Minimal tex file): \documentclass{minimal} \begin{document} @@ -52,3 +58,18 @@ Expect tex (Verify): $|f| = \vec{f}\cdot\vec{f}$ --- ;vv \end{document} +Do (imaps: imaps#add_map cool -> \item): + jocool\ + \begin{itemize}\ + cool\ + \end{itemize} + +Expect tex (Verify): + \documentclass{minimal} + \begin{document} + cool + \begin{itemize} + \item + \end{itemize} + \end{document} +