From 91942ceb25d81fee42f4e70417f2dfc35d1c705f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 30 May 2015 10:41:19 +0200 Subject: [PATCH] Improved the math mapping structure --- autoload/vimtex/mappings.vim | 43 +++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/autoload/vimtex/mappings.vim b/autoload/vimtex/mappings.vim index 517bf7d..9c8fc3b 100644 --- a/autoload/vimtex/mappings.vim +++ b/autoload/vimtex/mappings.vim @@ -79,26 +79,39 @@ function! vimtex#mappings#init(initialized) " {{{1 endif endif - let ultisnip_maps = [ - \ ['__', '_\{$1\}'], - \ ['^^', '^\{$1\}'], - \ ['((', '\left($1\right)'], - \ ['[[', '\left[$1\right]'], + let g:vimtex_mappings = [ + \ { 'lhs' : '__', 'rhs' : '_\{$1\}', 'math' : 1, 'type' : 'map'}, + \ { 'lhs' : '^^', 'rhs' : '^\{$1\}', 'math' : 1, 'type' : 'map'}, + \ { 'lhs' : '((', 'rhs' : '\left($1\right)', 'math' : 1, 'type' : 'map'}, + \ { 'lhs' : '[[', 'rhs' : '\left[$1\right]', 'math' : 1, 'type' : 'map'}, + \ { 'lhs' : '`a', 'rhs' : '\alpha', 'math' : 1, 'type' : 'abbrev'}, \] - for [lhs, rhs] in ultisnip_maps - silent execute 'inoremap ' lhs - \ lhs . '=UltiSnips#Anon(''' . rhs . ''', ''' . lhs - \ . ''', '''', ''i'')' - endfor - let abbreviations = [ - \ ['`a', '\alpha'], - \] - for [lhs, rhs] in abbreviations - silent execute 'iabbrev ' lhs rhs + call s:init_math_mappings() +endfunction + +" }}}1 + +function! s:init_math_mappings() " {{{1 + for map in g:vimtex_mappings + if map.type ==# 'map' + silent execute 'inoremap ' map.lhs + \ map.lhs . '=UltiSnips#Anon(''' . map.rhs . ''', ''' . map.lhs + \ . ''', '''', ''i'')' + elseif map.type ==# 'abbrev' + silent execute 'inoremap ' . map.lhs + \ . ' =is_math() ?' + \ string(map.rhs) ':' string(map.lhs) '' + endif endfor endfunction +" }}}1 +function! s:is_math() " {{{1 + return match(map(synstack(line('.'), col('.')), + \ 'synIDattr(v:val, ''name'')'), '^texMathZone[EX]$') >= 0 +endfunction + " }}}1 " vim: fdm=marker sw=2