Updated how mappings are added

Removed the collections feature, instead add metadata to each mapping. This
makes it easy to add just a single new mappings.
This commit is contained in:
Karl Yngve Lervåg 2015-10-19 20:53:43 +02:00
parent 94af494e9c
commit 8bf2661ee7

View File

@ -7,6 +7,78 @@
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_default', [
\ { 'map' : ['...', '\dots'], 'leader' : '' },
\ { 'map' : ['<m-i>', '\item '], 'leader' : '' },
\ { 'map' : ['__', '_\{$1\}'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['^^', '^\{$1\}'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['((', '\left($1\right)'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['[[', '\left[$1\right]'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['{{', '\left\\{$1\right\\}'], 'leader' : '', 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['i', '\int_{$1}^{$2}'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['S', '\sum_{$1}^{$2}'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['/', '\frac{$1}{$2}'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['0', '\emptyset'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['6', '\partial'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['8', '\infty'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['=', '\equiv'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['\', '\setminus'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['.', '\cdot'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['*', '\times'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['<', '\leq'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['>', '\geq'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['~', '\tilde{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['^', '\hat{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : [':', '\dot{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['_', '\bar{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['lim', '\lim_{$1}'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['qj', '\downarrow'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['ql', '\leftarrow'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['qh', '\rightarrow'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['qk', '\uparrow'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['qJ', '\Downarrow'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['qL', '\Leftarrow'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['qH', '\Rightarrow'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['qK', '\Uparrow'], 'wrapper' : 's:wrap_math_ultisnips'},
\ { 'map' : ['a', '\alpha'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['b', '\beta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['c', '\chi'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['d', '\delta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['e', '\varepsilon'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['f', '\varphi'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['g', '\gamma'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['h', '\eta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['k', '\kappa'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['l', '\lambda'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['m', '\mu'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['n', '\nu'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['p', '\pi'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['q', '\theta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['r', '\rho'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['s', '\sigma'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['t', '\tau'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['u', '\upsilon'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['w', '\omega'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['z', '\zeta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['A', '\Alpha'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['B', '\Beta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['G', '\Gamma'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['D', '\Delta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['E', '\Epsilon'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['Z', '\Zeta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['Y', '\Eta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['F', '\Phi'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['G', '\Gamma'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['L', '\Lambda'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['N', '\Nabla'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['Q', '\Theta'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['R', '\varrho'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['T', '\Tau'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['U', '\Upsilon'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['W', '\Omega'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['X', '\Xi'], 'wrapper' : 's:wrap_math'},
\ { 'map' : ['Y', '\Psi'], 'wrapper' : 's:wrap_math'},
\])
endfunction
" }}}1
@ -18,149 +90,33 @@ endfunction
function! vimtex#imaps#init_buffer() " {{{1
if !g:vimtex_imaps_enabled | return | endif
let b:vimtex.imap_collections = []
"
" Create predefined imaps
"
for collection in s:default_collections()
if get(g:, 'vimtex_imaps_' . collection.title, 1)
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)
for l:map in g:vimtex_imaps_default
call vimtex#imaps#add_map(l:map)
endfor
endfunction
" }}}1
function! s:default_collections() " {{{1
return [
\ {
\ 'title' : 'miscellaneous',
\ 'leader' : '',
\ 'mappings' : [
\ ['...', '\dots'],
\ ['<m-i>', '\item '],
\ ],
\ },
\ {
\ 'title' : 'math',
\ 'leader' : '',
\ 'wrapper' : 's:wrap_math_ultisnips',
\ 'mappings' : [
\ ['__', '_\{$1\}'],
\ ['^^', '^\{$1\}'],
\ ['((', '\left($1\right)'],
\ ['[[', '\left[$1\right]'],
\ ['{{', '\left\\{$1\right\\}'],
\ ],
\ },
\ {
\ 'title' : 'math_leader',
\ 'wrapper' : 's:wrap_math_ultisnips',
\ 'mappings' : [
\ ['i', '\int_{$1}^{$2}'],
\ ['S', '\sum_{$1}^{$2}'],
\ ['/', '\frac{$1}{$2}'],
\ ['0', '\emptyset'],
\ ['6', '\partial'],
\ ['8', '\infty'],
\ ['=', '\equiv'],
\ ['\', '\setminus'],
\ ['.', '\cdot'],
\ ['*', '\times'],
\ ['<', '\leq'],
\ ['>', '\geq'],
\ ['~', '\tilde{$1}'],
\ ['^', '\hat{$1}'],
\ [';', '\dot{$1}'],
\ ['_', '\bar{$1}'],
\ ['lim', '\lim_{$1}'],
\ ['qj', '\downarrow'],
\ ['ql', '\leftarrow'],
\ ['qh', '\rightarrow'],
\ ['qk', '\uparrow'],
\ ['qJ', '\Downarrow'],
\ ['qL', '\Leftarrow'],
\ ['qH', '\Rightarrow'],
\ ['qK', '\Uparrow'],
\ ],
\ },
\ {
\ 'title' : 'math_greek',
\ 'wrapper' : 's:wrap_math',
\ 'mappings' : [
\ ['a', '\alpha'],
\ ['b', '\beta'],
\ ['c', '\chi'],
\ ['d', '\delta'],
\ ['e', '\varepsilon'],
\ ['f', '\varphi'],
\ ['g', '\gamma'],
\ ['h', '\eta'],
\ ['k', '\kappa'],
\ ['l', '\lambda'],
\ ['m', '\mu'],
\ ['n', '\nu'],
\ ['p', '\pi'],
\ ['q', '\theta'],
\ ['r', '\rho'],
\ ['s', '\sigma'],
\ ['t', '\tau'],
\ ['u', '\upsilon'],
\ ['w', '\omega'],
\ ['z', '\zeta'],
\ ['A', '\Alpha'],
\ ['B', '\Beta'],
\ ['G', '\Gamma'],
\ ['D', '\Delta'],
\ ['E', '\Epsilon'],
\ ['Z', '\Zeta'],
\ ['Y', '\Eta'],
\ ['F', '\Phi'],
\ ['G', '\Gamma'],
\ ['L', '\Lambda'],
\ ['N', '\Nabla'],
\ ['Q', '\Theta'],
\ ['R', '\varrho'],
\ ['T', '\Tau'],
\ ['U', '\Upsilon'],
\ ['W', '\Omega'],
\ ['X', '\Xi'],
\ ['Y', '\Psi'],
\ ],
\ },
\]
endfunction
function! vimtex#imaps#add_map(map) " {{{1
let l:lhs = a:map.map[0]
let l:rhs = a:map.map[1]
if match(l:rhs, '$1') > 0 && !s:has_ultisnips | return | endif
" }}}1
function! s:parse_collection(collection) " {{{1
let l:leader = get(a:collection, 'leader', g:vimtex_imaps_leader)
let l:wrapper = get(a:collection, 'wrapper', '')
" Create mappings
for [lhs, rhs] in a:collection.mappings
if match(rhs, '$1') > 0 && !s:has_ultisnips | continue | endif
if l:wrapper !=# '' && exists('*' . l:wrapper)
let rhs = call(l:wrapper, [lhs, rhs])
endif
silent execute 'inoremap <silent><buffer>' l:leader . lhs rhs
endfor
let l:leader = get(a:map, 'leader', g:vimtex_imaps_leader)
let l:wrapper = get(a:map, 'wrapper', '')
" 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]
" 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