Added support for neosnippet#annonymous (#251)
This commit is contained in:
parent
de296341a5
commit
9451bdee5d
@ -8,22 +8,130 @@ function! vimtex#imaps#init_options() " {{{1
|
||||
call vimtex#util#set_default('g:vimtex_imaps_enabled', 1)
|
||||
call vimtex#util#set_default('g:vimtex_imaps_leader', '`')
|
||||
call vimtex#util#set_default('g:vimtex_imaps_disabled', [])
|
||||
call vimtex#util#set_default('g:vimtex_imaps_list', [
|
||||
call vimtex#util#set_default('g:vimtex_imaps_snippet_engine', 'ultisnips')
|
||||
call vimtex#util#set_default('g:vimtex_imaps_list', s:default_maps())
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
function! vimtex#imaps#init_script() " {{{1
|
||||
let s:has_ultisnips = exists('*UltiSnips#Anon')
|
||||
|
||||
try
|
||||
silent! call neosnippet#annonymous('')
|
||||
endtry
|
||||
let s:has_neosnippet = exists('*neosnippet#annonymous')
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
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)
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
|
||||
function! vimtex#imaps#add_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)
|
||||
let l:wrapper = get(a:map, 'wrapper', '')
|
||||
|
||||
" Don't create map if it is disabled
|
||||
if index(g:vimtex_imaps_disabled, l:lhs) >= 0 | return | endif
|
||||
|
||||
" Don't create map if snippet feature is not active/available
|
||||
if !s:test_snippet_requirement(l:wrapper) | return | endif
|
||||
|
||||
" Escape leader if it exists
|
||||
if l:leader !=# '' && !hasmapto(l:leader, 'i')
|
||||
silent execute 'inoremap <silent><buffer>' l:leader . l:leader l:leader
|
||||
endif
|
||||
|
||||
" Apply wrapper
|
||||
if l:wrapper !=# '' && exists('*' . l:wrapper)
|
||||
let l:rhs = call(l:wrapper, [l:lhs, l:rhs])
|
||||
endif
|
||||
|
||||
" Add mapping
|
||||
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#annonymous(''' . 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#annonymous(''' . 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]$') >= 0
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
function! s:default_maps() " {{{1
|
||||
" Define snippet maps with neosnippet syntax
|
||||
let snippets = [
|
||||
\ { 'lhs_rhs' : ['__', '_\{${1}\}${0}'], 'leader' : '', 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['^^', '^\{${1}\}${0}'], 'leader' : '', 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['((', '\left(${1}\right)${0}'], 'leader' : '', 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['[[', '\left[${1}\right]${0}'], 'leader' : '', 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['{{', '\left\\{${1}\right\\}${0}'], 'leader' : '', 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['i', '\int_{${1}}^{${2}}${0}'], 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['S', '\sum_{${1}}^{${2}}${0}'], 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['/', '\frac{${1}}{${2}}${0}'], 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['~', '\tilde{${1}}${0}'], 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['^', '\hat{${1}}${0}'], 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : [':', '\dot{${1}}${0}'], 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['_', '\bar{${1}}${0}'], 'wrapper' : 's:wrap_math_snippet'},
|
||||
\ { 'lhs_rhs' : ['lim', '\lim_{${1}}${0}'], 'wrapper' : 's:wrap_math_snippet'},
|
||||
\]
|
||||
|
||||
" Convert to ultisnips syntax if desired
|
||||
if g:vimtex_imaps_snippet_engine ==# 'ultisnips'
|
||||
for snippet in snippets
|
||||
let snippet.lhs_rhs[1]
|
||||
\ = substitute(snippet.lhs_rhs[1], '\${\(\d\)}', '$\1', 'g')
|
||||
endfor
|
||||
endif
|
||||
|
||||
" Return default snippet list including list of simple maps
|
||||
return snippets + [
|
||||
\ { 'lhs_rhs' : ['...', '\dots'], 'leader' : '' },
|
||||
\ { 'lhs_rhs' : ['<m-i>', '\item '], 'leader' : '' },
|
||||
\ { 'lhs_rhs' : ['__', '_\{$1\}'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['^^', '^\{$1\}'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['((', '\left($1\right)'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['[[', '\left[$1\right]'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['{{', '\left\\{$1\right\\}'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['i', '\int_{$1}^{$2}'], 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['S', '\sum_{$1}^{$2}'], 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['/', '\frac{$1}{$2}'], 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['~', '\tilde{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['^', '\hat{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : [':', '\dot{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['_', '\bar{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['lim', '\lim_{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
|
||||
\ { 'lhs_rhs' : ['0', '\emptyset'], 'wrapper' : 's:wrap_math'},
|
||||
\ { 'lhs_rhs' : ['6', '\partial'], 'wrapper' : 's:wrap_math'},
|
||||
\ { 'lhs_rhs' : ['8', '\infty'], 'wrapper' : 's:wrap_math'},
|
||||
@ -84,78 +192,14 @@ function! vimtex#imaps#init_options() " {{{1
|
||||
\ { 'lhs_rhs' : ['W', '\Omega'], 'wrapper' : 's:wrap_math'},
|
||||
\ { 'lhs_rhs' : ['X', '\Xi'], 'wrapper' : 's:wrap_math'},
|
||||
\ { 'lhs_rhs' : ['Y', '\Psi'], 'wrapper' : 's:wrap_math'},
|
||||
\])
|
||||
\]
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
function! vimtex#imaps#init_script() " {{{1
|
||||
let s:has_ultisnips = exists('*UltiSnips#Anon')
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
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)
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
|
||||
function! vimtex#imaps#add_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)
|
||||
let l:wrapper = get(a:map, 'wrapper', '')
|
||||
if index(g:vimtex_imaps_disabled, l:lhs) >= 0 | return | endif
|
||||
if l:wrapper =~? 'ultisnips' && !s:has_ultisnips | return | endif
|
||||
|
||||
" Escape leader if it exists
|
||||
if l:leader !=# '' && !hasmapto(l:leader, 'i')
|
||||
silent execute 'inoremap <silent><buffer>' l:leader . l:leader l:leader
|
||||
endif
|
||||
|
||||
" Apply wrapper
|
||||
if l:wrapper !=# '' && exists('*' . l:wrapper)
|
||||
let l:rhs = call(l:wrapper, [l:lhs, l:rhs])
|
||||
endif
|
||||
|
||||
" Add mapping
|
||||
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_ultisnips(lhs, rhs) " {{{1
|
||||
return a:lhs . '<c-r>=UltiSnips#Anon('''
|
||||
\ . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')<cr>'
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
function! s:wrap_math_ultisnips(lhs, rhs) " {{{1
|
||||
return a:lhs . '<c-r>=<sid>is_math() ? '
|
||||
\ . 'UltiSnips#Anon(''' . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')'
|
||||
\ . ': ''''<cr>'
|
||||
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]$') >= 0
|
||||
function! s:test_snippet_requirement(func_name) " {{{1
|
||||
return (a:func_name !~# 'snippet$')
|
||||
\ || (g:vimtex_imaps_snippet_engine ==# 'neosnippet' && s:has_neosnippet)
|
||||
\ || (g:vimtex_imaps_snippet_engine ==# 'ultisnips' && s:has_ultisnips)
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
|
Loading…
Reference in New Issue
Block a user