Fix #279: Create custom maps only in tex files
This commit is contained in:
parent
ddf1850b7c
commit
9878c66b34
@ -23,14 +23,20 @@ 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
|
||||||
|
|
||||||
for l:map in g:vimtex_imaps_list
|
for l:map in g:vimtex_imaps_list + get(s:, 'custom_maps', [])
|
||||||
call vimtex#imaps#add_map(l:map)
|
call s:create_map(l:map)
|
||||||
endfor
|
endfor
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" }}}1
|
" }}}1
|
||||||
|
|
||||||
function! vimtex#imaps#add_map(map) " {{{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:lhs = a:map.lhs_rhs[0]
|
||||||
let l:rhs = a:map.lhs_rhs[1]
|
let l:rhs = a:map.lhs_rhs[1]
|
||||||
let l:leader = get(a:map, 'leader', g:vimtex_imaps_leader)
|
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
|
silent execute 'inoremap <silent><buffer>' l:leader . l:lhs l:rhs
|
||||||
endfunction
|
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
|
" }}}1
|
||||||
function! s:default_maps() " {{{1
|
function! s:default_maps() " {{{1
|
||||||
" Define snippet maps with neosnippet syntax
|
" Define snippet maps with neosnippet syntax
|
||||||
@ -193,6 +157,48 @@ function! s:default_maps() " {{{1
|
|||||||
\]
|
\]
|
||||||
endfunction
|
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
|
" }}}1
|
||||||
function! s:test_snippet_requirement(func_name) " {{{1
|
function! s:test_snippet_requirement(func_name) " {{{1
|
||||||
return (a:func_name !~# 'snippet$')
|
return (a:func_name !~# 'snippet$')
|
||||||
|
@ -1158,13 +1158,13 @@ mappings: >
|
|||||||
let g:vimtex_imaps_list = [
|
let g:vimtex_imaps_list = [
|
||||||
\ { 'lhs_rhs' : ['__', '_\{$1\}'],
|
\ { 'lhs_rhs' : ['__', '_\{$1\}'],
|
||||||
\'leader' : '',
|
\'leader' : '',
|
||||||
\'wrapper' : 's:wrap_math_ultisnips'},
|
\'wrapper' : 's:wrap_math_snippet'},
|
||||||
\ { 'lhs_rhs' : ['^^', '^\{$1\}'],
|
\ { 'lhs_rhs' : ['^^', '^\{$1\}'],
|
||||||
\'leader' : '',
|
\'leader' : '',
|
||||||
\'wrapper' : 's:wrap_math_ultisnips'},
|
\'wrapper' : 's:wrap_math_snippet'},
|
||||||
\ { 'lhs_rhs' : ['((', '\left($1\right)'],
|
\ { 'lhs_rhs' : ['((', '\left($1\right)'],
|
||||||
\'leader' : '',
|
\'leader' : '',
|
||||||
\'wrapper' : 's:wrap_math_ultisnips'},
|
\'wrapper' : 's:wrap_math_snippet'},
|
||||||
\]
|
\]
|
||||||
|
|
||||||
" Add custom mapping through vimtex#imap#add_map
|
" Add custom mapping through vimtex#imap#add_map
|
||||||
|
4
test/feature/imaps/main.tex
Normal file
4
test/feature/imaps/main.tex
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
\documentclass{minimal}
|
||||||
|
\begin{document}
|
||||||
|
Hello world!
|
||||||
|
\end{document}
|
20
test/feature/imaps/vimrc
Normal file
20
test/feature/imaps/vimrc
Normal 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'})
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user