Some big updates
* Use collection to denote the list of mappings * Let default collections be defined through a function * Allow separate leader keys for each collection * Test for ultisnips before creating mappings
This commit is contained in:
parent
bccded6ee2
commit
3095c3ca72
@ -11,19 +11,50 @@ endfunction
|
|||||||
|
|
||||||
" }}}1
|
" }}}1
|
||||||
function! vimtex#imaps#init_script() " {{{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
|
||||||
|
|
||||||
|
let b:vimtex.imap_collections = []
|
||||||
|
|
||||||
"
|
"
|
||||||
" Define default lists of imaps
|
" Create predefined imaps
|
||||||
"
|
"
|
||||||
let s:imaps = {
|
for collection in s:default_collections()
|
||||||
\ 'miscellaneous' : {
|
if get(g:, 'vimtex_imaps_' . collection.title, 1)
|
||||||
\ 'list' : [
|
call s:parse_collection(collection)
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
|
||||||
|
"
|
||||||
|
" Create custom imaps if defined
|
||||||
|
"
|
||||||
|
for collection in get(g:, 'vimtex_imaps_custom', [])
|
||||||
|
call s:parse_collection(collection)
|
||||||
|
endfor
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" }}}1
|
||||||
|
|
||||||
|
function! s:default_collections() " {{{1
|
||||||
|
return [
|
||||||
|
\ {
|
||||||
|
\ 'title' : 'miscellaneous',
|
||||||
|
\ 'leader' : '',
|
||||||
|
\ 'mode' : '',
|
||||||
|
\ 'mappings' : [
|
||||||
\ ['...', '\dots'],
|
\ ['...', '\dots'],
|
||||||
\ ['<m-i>', '\item '],
|
\ ['<m-i>', '\item '],
|
||||||
\ ],
|
\ ],
|
||||||
\ },
|
\ },
|
||||||
\ 'math' : {
|
\ {
|
||||||
\ 'math' : 1,
|
\ 'title' : 'math',
|
||||||
\ 'list' : [
|
\ 'leader' : '',
|
||||||
|
\ 'mode' : 'm',
|
||||||
|
\ 'mappings' : [
|
||||||
\ ['__', '_\{$1\}'],
|
\ ['__', '_\{$1\}'],
|
||||||
\ ['^^', '^\{$1\}'],
|
\ ['^^', '^\{$1\}'],
|
||||||
\ ['((', '\left($1\right)'],
|
\ ['((', '\left($1\right)'],
|
||||||
@ -47,10 +78,10 @@ function! vimtex#imaps#init_script() " {{{1
|
|||||||
\ ['qK', '\Uparrow'],
|
\ ['qK', '\Uparrow'],
|
||||||
\ ],
|
\ ],
|
||||||
\ },
|
\ },
|
||||||
\ 'math_leader' : {
|
\ {
|
||||||
\ 'leader' : 1,
|
\ 'title' : 'math_leader',
|
||||||
\ 'math' : 1,
|
\ 'mode' : 'm',
|
||||||
\ 'list' : [
|
\ 'mappings' : [
|
||||||
\ ['i', '\int_{$1}^{$2}'],
|
\ ['i', '\int_{$1}^{$2}'],
|
||||||
\ ['S', '\sum_{$1}^{$2}'],
|
\ ['S', '\sum_{$1}^{$2}'],
|
||||||
\ ['/', '\frac{$1}{$2}'],
|
\ ['/', '\frac{$1}{$2}'],
|
||||||
@ -69,10 +100,10 @@ function! vimtex#imaps#init_script() " {{{1
|
|||||||
\ ['_', '\bar{$1}'],
|
\ ['_', '\bar{$1}'],
|
||||||
\ ],
|
\ ],
|
||||||
\ },
|
\ },
|
||||||
\ 'greek' : {
|
\ {
|
||||||
\ 'leader' : 1,
|
\ 'title' : 'greek',
|
||||||
\ 'math' : 1,
|
\ 'mode' : 'm',
|
||||||
\ 'list' : [
|
\ 'mappings' : [
|
||||||
\ ['a', '\alpha'],
|
\ ['a', '\alpha'],
|
||||||
\ ['b', '\beta'],
|
\ ['b', '\beta'],
|
||||||
\ ['c', '\chi'],
|
\ ['c', '\chi'],
|
||||||
@ -107,53 +138,26 @@ function! vimtex#imaps#init_script() " {{{1
|
|||||||
\ ['Y', '\Psi'],
|
\ ['Y', '\Psi'],
|
||||||
\ ],
|
\ ],
|
||||||
\ },
|
\ },
|
||||||
\}
|
\]
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" }}}1
|
" }}}1
|
||||||
function! vimtex#imaps#init_buffer() " {{{1
|
function! s:parse_collection(collection) " {{{1
|
||||||
if !g:vimtex_imaps_enabled | return | endif
|
"
|
||||||
|
" Extract some collection metadata
|
||||||
|
"
|
||||||
|
let l:leader = get(a:collection, 'leader', g:vimtex_imaps_leader)
|
||||||
|
let l:mode = get(a:collection, 'mode', '')
|
||||||
|
let l:math = l:mode =~# 'm'
|
||||||
|
|
||||||
"
|
"
|
||||||
" Create predefined imaps
|
" Create mappings
|
||||||
"
|
"
|
||||||
for [name, imaps] in items(s:imaps)
|
for [lhs, rhs] in a:collection.mappings
|
||||||
if get(g:, 'vimtex_imaps_' . name, 1)
|
|
||||||
echom 'imaps created: ' . name
|
|
||||||
call s:create_imaps(imaps)
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
|
|
||||||
"
|
|
||||||
" Create custom imaps if defined
|
|
||||||
"
|
|
||||||
for imaps in get(g:, 'vimtex_imaps_custom', [])
|
|
||||||
call s:create_imaps(imaps)
|
|
||||||
endfor
|
|
||||||
|
|
||||||
"
|
|
||||||
" Escape the leader
|
|
||||||
"
|
|
||||||
silent execute 'inoremap <silent><buffer>'
|
|
||||||
\ g:vimtex_imaps_leader . g:vimtex_imaps_leader
|
|
||||||
\ g:vimtex_imaps_leader
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
" }}}1
|
|
||||||
|
|
||||||
"
|
|
||||||
" Functions to create the imaps
|
|
||||||
"
|
|
||||||
function! s:create_imaps(imaps) " {{{1
|
|
||||||
let l:leader = get(a:imaps, 'leader', 0)
|
|
||||||
let l:math = get(a:imaps, 'math', 0)
|
|
||||||
|
|
||||||
for [lhs, rhs] in a:imaps.list
|
|
||||||
let l:ultisnips = match(rhs, '$1') > 0
|
let l:ultisnips = match(rhs, '$1') > 0
|
||||||
|
if l:ultisnips && !s:has_ultisnips | continue | endif
|
||||||
|
|
||||||
"
|
|
||||||
" Generate RHS
|
" Generate RHS
|
||||||
"
|
|
||||||
if l:math && l:ultisnips
|
if l:math && l:ultisnips
|
||||||
let rhs = s:wrap_math_ultisnips(lhs, rhs)
|
let rhs = s:wrap_math_ultisnips(lhs, rhs)
|
||||||
elseif l:math
|
elseif l:math
|
||||||
@ -162,16 +166,24 @@ function! s:create_imaps(imaps) " {{{1
|
|||||||
let rhs = s:wrap_ultisnips(lhs, rhs)
|
let rhs = s:wrap_ultisnips(lhs, rhs)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
"
|
silent execute 'inoremap <silent><buffer>' l:leader . lhs rhs
|
||||||
" Add leader
|
|
||||||
"
|
|
||||||
let lhs = l:leader ? g:vimtex_imaps_leader . lhs : lhs
|
|
||||||
|
|
||||||
silent execute 'inoremap <silent><buffer>' lhs rhs
|
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
|
"
|
||||||
|
" Escape leader if it exists
|
||||||
|
"
|
||||||
|
if l:leader !=# '' && !hasmapto(l:leader, 'i')
|
||||||
|
silent execute 'inoremap <silent><buffer>' l:leader . l:leader l:leader
|
||||||
|
endif
|
||||||
|
|
||||||
|
let b:vimtex.imap_collections += [a:collection.title]
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" }}}1
|
" }}}1
|
||||||
|
|
||||||
|
"
|
||||||
|
" Helper functions
|
||||||
|
"
|
||||||
function! s:wrap_math_ultisnips(lhs, rhs) " {{{1
|
function! s:wrap_math_ultisnips(lhs, rhs) " {{{1
|
||||||
return a:lhs . '<c-r>=<sid>is_math() ? UltiSnips#Anon('''
|
return a:lhs . '<c-r>=<sid>is_math() ? UltiSnips#Anon('''
|
||||||
\ . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'') : ''''<cr>'
|
\ . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'') : ''''<cr>'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user