Fix #279: Create custom maps only in tex files

This commit is contained in:
Karl Yngve Lervåg 2015-12-06 21:46:08 +01:00
parent ddf1850b7c
commit 9878c66b34
4 changed files with 77 additions and 47 deletions

View File

@ -23,14 +23,20 @@ endfunction
function! vimtex#imaps#init_buffer() " {{{1
if !g:vimtex_imaps_enabled | return | endif
for l:map in g:vimtex_imaps_list
call vimtex#imaps#add_map(l:map)
for l:map in g:vimtex_imaps_list + get(s:, 'custom_maps', [])
call s:create_map(l:map)
endfor
endfunction
" }}}1
function! vimtex#imaps#add_map(map) " {{{1
let s:custom_maps = get(s:, 'custom_maps', []) + [a:map]
endfunction
" }}}1
function! s:create_map(map) " {{{1
let l:lhs = a:map.lhs_rhs[0]
let l:rhs = a:map.lhs_rhs[1]
let l:leader = get(a:map, 'leader', g:vimtex_imaps_leader)
@ -56,48 +62,6 @@ function! vimtex#imaps#add_map(map) " {{{1
silent execute 'inoremap <silent><buffer>' l:leader . l:lhs l:rhs
endfunction
" }}}1
"
" Wrappers
"
function! s:wrap_math(lhs, rhs) " {{{1
return '<c-r>=<sid>is_math() ? ' . string(a:rhs)
\ . ' : ' . string(a:lhs) . '<cr>'
endfunction
" }}}1
function! s:wrap_snippet(lhs, rhs) " {{{1
if g:vimtex_imaps_snippet_engine ==# 'neosnippet'
return '<c-r>=neosnippet#anonymous(''' . a:rhs . ''')<cr>'
else
return a:lhs . '<c-r>=UltiSnips#Anon('''
\ . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')<cr>'
endif
endfunction
" }}}1
function! s:wrap_math_snippet(lhs, rhs) " {{{1
if g:vimtex_imaps_snippet_engine ==# 'neosnippet'
return '<c-r>=<sid>is_math() ? neosnippet#anonymous(''' . a:rhs . ''')'
\ . ' : ' . string(a:lhs) . '<cr>'
else
return a:lhs . '<c-r>=<sid>is_math() ? '
\ . 'UltiSnips#Anon(''' . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')'
\ . ': ''''<cr>'
endif
endfunction
" }}}1
"
" Helper functions
"
function! s:is_math() " {{{1
return match(map(synstack(line('.'), max([col('.') - 1, 1])),
\ 'synIDattr(v:val, ''name'')'), '^texMathZone[A-Z]S\?$') >= 0
endfunction
" }}}1
function! s:default_maps() " {{{1
" Define snippet maps with neosnippet syntax
@ -193,6 +157,48 @@ function! s:default_maps() " {{{1
\]
endfunction
" }}}1
"
" Wrappers
"
function! s:wrap_math(lhs, rhs) " {{{1
return '<c-r>=<sid>is_math() ? ' . string(a:rhs)
\ . ' : ' . string(a:lhs) . '<cr>'
endfunction
" }}}1
function! s:wrap_snippet(lhs, rhs) " {{{1
if g:vimtex_imaps_snippet_engine ==# 'neosnippet'
return '<c-r>=neosnippet#anonymous(''' . a:rhs . ''')<cr>'
else
return a:lhs . '<c-r>=UltiSnips#Anon('''
\ . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')<cr>'
endif
endfunction
" }}}1
function! s:wrap_math_snippet(lhs, rhs) " {{{1
if g:vimtex_imaps_snippet_engine ==# 'neosnippet'
return '<c-r>=<sid>is_math() ? neosnippet#anonymous(''' . a:rhs . ''')'
\ . ' : ' . string(a:lhs) . '<cr>'
else
return a:lhs . '<c-r>=<sid>is_math() ? '
\ . 'UltiSnips#Anon(''' . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')'
\ . ': ''''<cr>'
endif
endfunction
" }}}1
"
" Helper functions
"
function! s:is_math() " {{{1
return match(map(synstack(line('.'), max([col('.') - 1, 1])),
\ 'synIDattr(v:val, ''name'')'), '^texMathZone[A-Z]S\?$') >= 0
endfunction
" }}}1
function! s:test_snippet_requirement(func_name) " {{{1
return (a:func_name !~# 'snippet$')

View File

@ -1158,13 +1158,13 @@ mappings: >
let g:vimtex_imaps_list = [
\ { 'lhs_rhs' : ['__', '_\{$1\}'],
\'leader' : '',
\'wrapper' : 's:wrap_math_ultisnips'},
\'wrapper' : 's:wrap_math_snippet'},
\ { 'lhs_rhs' : ['^^', '^\{$1\}'],
\'leader' : '',
\'wrapper' : 's:wrap_math_ultisnips'},
\'wrapper' : 's:wrap_math_snippet'},
\ { 'lhs_rhs' : ['((', '\left($1\right)'],
\'leader' : '',
\'wrapper' : 's:wrap_math_ultisnips'},
\'wrapper' : 's:wrap_math_snippet'},
\]
" Add custom mapping through vimtex#imap#add_map

View File

@ -0,0 +1,4 @@
\documentclass{minimal}
\begin{document}
Hello world!
\end{document}

20
test/feature/imaps/vimrc Normal file
View File

@ -0,0 +1,20 @@
set nocompatible
let &rtp = '~/.vim/bundle/neosnippet.vim/,' . &rtp
let &rtp = '~/.vim/bundle/vimtex,' . &rtp
let &rtp .= ',~/.vim/bundle/vimtex/after'
filetype plugin indent on
syntax enable
let g:vimtex_imaps_snippet_engine = 'neosnippet'
let g:vimtex_imaps_list = [
\ { 'lhs_rhs' : ['((', '\left($1\right)'],
\ 'leader' : '',
\ 'wrapper' : 's:wrap_math_snippet'},
\]
" Add custom mapping through vimtex#imap#add_map
call vimtex#imaps#add_map({
\ 'lhs_rhs' : [ 'v', '\vec{${1}}${0}'],
\ 'wrapper' : 's:wrap_math_snippet'})