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
|
||||
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 = {
|
||||
\ 'miscellaneous' : {
|
||||
\ 'list' : [
|
||||
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)
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
|
||||
function! s:default_collections() " {{{1
|
||||
return [
|
||||
\ {
|
||||
\ 'title' : 'miscellaneous',
|
||||
\ 'leader' : '',
|
||||
\ 'mode' : '',
|
||||
\ 'mappings' : [
|
||||
\ ['...', '\dots'],
|
||||
\ ['<m-i>', '\item '],
|
||||
\ ],
|
||||
\ },
|
||||
\ 'math' : {
|
||||
\ 'math' : 1,
|
||||
\ 'list' : [
|
||||
\ {
|
||||
\ 'title' : 'math',
|
||||
\ 'leader' : '',
|
||||
\ 'mode' : 'm',
|
||||
\ 'mappings' : [
|
||||
\ ['__', '_\{$1\}'],
|
||||
\ ['^^', '^\{$1\}'],
|
||||
\ ['((', '\left($1\right)'],
|
||||
@ -47,10 +78,10 @@ function! vimtex#imaps#init_script() " {{{1
|
||||
\ ['qK', '\Uparrow'],
|
||||
\ ],
|
||||
\ },
|
||||
\ 'math_leader' : {
|
||||
\ 'leader' : 1,
|
||||
\ 'math' : 1,
|
||||
\ 'list' : [
|
||||
\ {
|
||||
\ 'title' : 'math_leader',
|
||||
\ 'mode' : 'm',
|
||||
\ 'mappings' : [
|
||||
\ ['i', '\int_{$1}^{$2}'],
|
||||
\ ['S', '\sum_{$1}^{$2}'],
|
||||
\ ['/', '\frac{$1}{$2}'],
|
||||
@ -69,10 +100,10 @@ function! vimtex#imaps#init_script() " {{{1
|
||||
\ ['_', '\bar{$1}'],
|
||||
\ ],
|
||||
\ },
|
||||
\ 'greek' : {
|
||||
\ 'leader' : 1,
|
||||
\ 'math' : 1,
|
||||
\ 'list' : [
|
||||
\ {
|
||||
\ 'title' : 'greek',
|
||||
\ 'mode' : 'm',
|
||||
\ 'mappings' : [
|
||||
\ ['a', '\alpha'],
|
||||
\ ['b', '\beta'],
|
||||
\ ['c', '\chi'],
|
||||
@ -107,53 +138,26 @@ function! vimtex#imaps#init_script() " {{{1
|
||||
\ ['Y', '\Psi'],
|
||||
\ ],
|
||||
\ },
|
||||
\}
|
||||
\]
|
||||
endfunction
|
||||
|
||||
" }}}1
|
||||
function! vimtex#imaps#init_buffer() " {{{1
|
||||
if !g:vimtex_imaps_enabled | return | endif
|
||||
function! s:parse_collection(collection) " {{{1
|
||||
"
|
||||
" 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)
|
||||
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
|
||||
for [lhs, rhs] in a:collection.mappings
|
||||
let l:ultisnips = match(rhs, '$1') > 0
|
||||
if l:ultisnips && !s:has_ultisnips | continue | endif
|
||||
|
||||
"
|
||||
" Generate RHS
|
||||
"
|
||||
if l:math && l:ultisnips
|
||||
let rhs = s:wrap_math_ultisnips(lhs, rhs)
|
||||
elseif l:math
|
||||
@ -162,16 +166,24 @@ function! s:create_imaps(imaps) " {{{1
|
||||
let rhs = s:wrap_ultisnips(lhs, rhs)
|
||||
endif
|
||||
|
||||
"
|
||||
" Add leader
|
||||
"
|
||||
let lhs = l:leader ? g:vimtex_imaps_leader . lhs : lhs
|
||||
|
||||
silent execute 'inoremap <silent><buffer>' lhs rhs
|
||||
silent execute 'inoremap <silent><buffer>' l:leader . lhs rhs
|
||||
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
|
||||
|
||||
" }}}1
|
||||
|
||||
"
|
||||
" Helper functions
|
||||
"
|
||||
function! s:wrap_math_ultisnips(lhs, rhs) " {{{1
|
||||
return a:lhs . '<c-r>=<sid>is_math() ? UltiSnips#Anon('''
|
||||
\ . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'') : ''''<cr>'
|
||||
|
Loading…
x
Reference in New Issue
Block a user