Skip to content

Commit d589986

Browse files
committed
L2U: save and reinstate previous mapping
instead of remapping over and over.
1 parent aa29918 commit d589986

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

autoload/LaTeXtoUnicode.vim

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,18 @@ endfunction
346346

347347
" Function which saves the current insert-mode mapping of a key sequence `s`
348348
" and associates it with another key sequence `k` (e.g. stores the current
349-
" <Tab> mapping into the Fallback trigger)
349+
" <Tab> mapping into the Fallback trigger).
350+
" It returns the previous maparg dictionary, so that the previous mapping can
351+
" be reinstated if needed.
350352
function! s:L2U_SetFallbackMapping(s, k)
351353
let mmdict = maparg(a:s, 'i', 0, 1)
352354
if empty(mmdict)
353355
exe 'inoremap <buffer> ' . a:k . ' ' . a:s
354-
return
356+
return mmdict
355357
endif
356358
let rhs = mmdict["rhs"]
357359
if rhs =~# '^<Plug>L2U'
358-
return
360+
return mmdict
359361
endif
360362
let pre = '<buffer>'
361363
let pre = pre . (mmdict["silent"] ? '<silent>' : '')
@@ -382,6 +384,25 @@ function! s:L2U_SetFallbackMapping(s, k)
382384
endif
383385
endif
384386
exe cmd . pre . ' ' . a:k . ' ' . rhs
387+
return mmdict
388+
endfunction
389+
390+
" Reinstate a mapping from the maparg dict returned by SetFallbackMapping
391+
" (only if buffer-local, since otherwise it should still be available)
392+
function! s:L2U_ReinstateMapping(mmdict)
393+
if empty(a:mmdict) || !a:mmdict["buffer"]
394+
return ''
395+
endif
396+
let lhs = a:mmdict["lhs"]
397+
let rhs = a:mmdict["rhs"]
398+
if rhs =~# '^<Plug>L2U'
399+
return ''
400+
endif
401+
let pre = '<buffer>'
402+
let pre = pre . (a:mmdict["silent"] ? '<silent>' : '')
403+
let pre = pre . (a:mmdict["expr"] ? '<expr>' : '')
404+
let cmd = a:mmdict["noremap"] ? 'inoremap ' : 'imap '
405+
exe cmd . pre . ' ' . lhs . ' ' . rhs
385406
endfunction
386407

387408
" This is the function which is mapped to <Tab>
@@ -564,7 +585,7 @@ function! s:L2U_SetTab(wait_insert_enter)
564585
endif
565586
setlocal completefunc=LaTeXtoUnicode#completefunc
566587

567-
call s:L2U_SetFallbackMapping('<Tab>', s:l2u_fallback_trigger)
588+
let b:l2u_prev_map_tab = s:L2U_SetFallbackMapping('<Tab>', s:l2u_fallback_trigger)
568589
imap <buffer> <Tab> <Plug>L2UTab
569590
inoremap <buffer><expr> <Plug>L2UTab LaTeXtoUnicode#Tab()
570591
@@ -585,7 +606,7 @@ function! s:L2U_UnsetTab()
585606
exec "setlocal completefunc=" . get(b:, "l2u_prev_completefunc", "")
586607
iunmap <buffer> <Tab>
587608
if empty(maparg("<Tab>", "i"))
588-
call s:L2U_SetFallbackMapping(s:l2u_fallback_trigger, '<Tab>')
609+
call s:L2U_ReinstateMapping(b:l2u_prev_map_tab)
589610
endif
590611
iunmap <buffer> <Plug>L2UTab
591612
exe 'iunmap <buffer> ' . s:l2u_fallback_trigger
@@ -675,7 +696,7 @@ function! s:L2U_SetAutoSub(wait_insert_enter)
675696
" autocmd InsertCharPre. The <Enter> key does not seem to be catched in
676697
" this way though, so we use a mapping for that case.
677698

678-
call s:L2U_SetFallbackMapping('<CR>', s:l2u_fallback_trigger_cr)
699+
let b:l2u_prev_map_cr = s:L2U_SetFallbackMapping('<CR>', s:l2u_fallback_trigger_cr)
679700
imap <buffer> <CR> <Plug>L2UAutoSub
680701
exec 'inoremap <buffer><expr> <Plug>L2UAutoSub LaTeXtoUnicode#AutoSub("\n", "' . s:l2u_fallback_trigger_cr . '")'
681702

@@ -695,7 +716,7 @@ function! s:L2U_UnsetAutoSub()
695716

696717
iunmap <buffer> <CR>
697718
if empty(maparg("<CR>", "i"))
698-
exec 'call s:L2U_SetFallbackMapping("' . s:l2u_fallback_trigger_cr . '", "\<CR>")'
719+
call s:L2U_ReinstateMapping(b:l2u_prev_map_cr)
699720
endif
700721
iunmap <buffer> <Plug>L2UAutoSub
701722
exe 'iunmap <buffer> ' . s:l2u_fallback_trigger_cr

0 commit comments

Comments
 (0)