Fix #566: Implement wrap_environment

This commit is contained in:
Karl Yngve Lervåg 2016-10-17 22:35:04 +02:00
parent 5334f6090a
commit a0152c0691
2 changed files with 42 additions and 1 deletions

View File

@ -91,6 +91,9 @@ endfunction
function! vimtex#imaps#init_buffer() " {{{1 function! vimtex#imaps#init_buffer() " {{{1
if !g:vimtex_imaps_enabled | return | endif if !g:vimtex_imaps_enabled | return | endif
" Container for wrapper contexts
let b:vimtex_context = {}
" "
" Create imaps " Create imaps
" "
@ -173,6 +176,13 @@ function! s:create_map(map) " {{{1
return return
endif 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 <expr><silent><buffer>' l:lhs silent execute 'inoremap <expr><silent><buffer>' l:lhs
\ l:wrapper . '("' . escape(l:lhs, '\') . '", ' . string(a:map.rhs) . ')' \ 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 return s:is_math() ? a:rhs : a:lhs
endfunction 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 " }}}1
" "

View File

@ -8,9 +8,15 @@ Execute (Setup):
\ 'leader' : '', \ 'leader' : '',
\ 'wrapper' : 'vimtex#imaps#wrap_trivial', \ '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 silent! VimtexReload
Given:
Given tex (Minimal tex file): Given tex (Minimal tex file):
\documentclass{minimal} \documentclass{minimal}
\begin{document} \begin{document}
@ -52,3 +58,18 @@ Expect tex (Verify):
$|f| = \vec{f}\cdot\vec{f}$ --- ;vv $|f| = \vec{f}\cdot\vec{f}$ --- ;vv
\end{document} \end{document}
Do (imaps: imaps#add_map cool -> \item):
jocool\<cr>
\begin{itemize}\<cr>
cool\<cr>
\end{itemize}
Expect tex (Verify):
\documentclass{minimal}
\begin{document}
cool
\begin{itemize}
\item
\end{itemize}
\end{document}