@@ -13,9 +13,11 @@ function! s:L2U_Setup()
1313
1414 " Did we install the L2U tab/as-you-type/keymap... mappings?
1515 let b: l2u_tab_set = get (b: , " l2u_tab_set" , 0 )
16+ let b: l2u_stab_set = get (b: , " l2u_stab_set" , 0 )
1617 let b: l2u_cmdtab_set = get (b: , " l2u_cmdtab_set" , 0 )
1718 let b: l2u_autosub_set = get (b: , " l2u_autosub_set" , 0 )
1819 let b: l2u_keymap_set = get (b: , " l2u_keymap_set" , 0 )
20+ let b: l2u_cr_set = get (b: , " l2u_cr_set" , 0 )
1921
2022 " Following are some flags used to pass information between the function which
2123 " attempts the LaTeX-to-Unicode completion and the fallback function
@@ -28,6 +30,8 @@ function! s:L2U_Setup()
2830 let b: l2u_tab_completing = 0
2931 " Are we calling the tab fallback?
3032 let b: l2u_in_fallback = 0
33+ " Are we forcing acceptance?
34+ let b: l2u_accept_choice = 0
3135
3236endfunction
3337
@@ -63,8 +67,9 @@ function! s:L2U_SetupGlobal()
6367 " this string with feedkeys(s:l2u_esc_sequence, 'n')
6468 let s: l2u_esc_sequence = " \b "
6569
66- " Trigger for the previous mapping of <Tab>
70+ " Trigger for the previous mapping of <Tab>/<S-Tab>
6771 let s: l2u_fallback_trigger = " \u0091 L2UFallbackTab"
72+ let s: l2u_fallback_trigger_stab = " \u0091 L2UFallbackSTab"
6873
6974 " Trigger for the previous mapping of <CR>
7075 let s: l2u_fallback_trigger_cr = " \u0091 L2UFallbackCR"
@@ -283,7 +288,8 @@ function! LaTeXtoUnicode#completefunc(findstart, base)
283288 else
284289 " read settings (eager mode is implicit when suggestions are disabled)
285290 let suggestions = get (g: , " latex_to_unicode_suggestions" , 1 )
286- let eager = get (g: , " latex_to_unicode_eager" , 1 ) || ! suggestions
291+ let eager = get (g: , " latex_to_unicode_eager" , 1 ) || ! suggestions || b: l2u_accept_choice
292+ let b: l2u_accept_choice = 0
287293 " search for matches
288294 let partmatches = []
289295 let exact_match = 0
@@ -403,6 +409,19 @@ endfunction
403409
404410" This is the function which is mapped to <Tab>
405411function ! LaTeXtoUnicode#Tab ()
412+ if get (g: , " latex_to_unicode_smart_tab" , 0 ) && pumvisible () && b: l2u_tab_completing
413+ let do_nextitem = 1
414+ if exists (' *complete_info' ) && exists (' *pum_getpos' )
415+ let info = complete_info ()
416+ if info[' mode' ] == " function" && pum_getpos ()[' size' ] == 1 && info[' selected' ] != -1 && s: L2U_ismatch ()
417+ let do_nextitem = 0
418+ endif
419+ endif
420+ if do_nextitem
421+ call feedkeys (" \<C-N> " , ' n' )
422+ return ' '
423+ endif
424+ endif
406425 " the <Tab> is passed through to the fallback mapping if the completion
407426 " menu is present, and it hasn't been raised by the L2U tab, and there
408427 " isn't an exact match before the cursor
@@ -421,6 +440,15 @@ function! LaTeXtoUnicode#Tab()
421440 return " "
422441endfunction
423442
443+ function ! LaTeXtoUnicode#STab ()
444+ if get (g: , " latex_to_unicode_smart_tab" , 0 ) && pumvisible () && b: l2u_tab_completing
445+ call feedkeys (" \<C-P> " , ' n' )
446+ else
447+ call feedkeys (s: l2u_fallback_trigger_stab )
448+ endif
449+ return ' '
450+ endfunction
451+
424452" This function is called at every CompleteDone event, and is meant to handle
425453" the failures of LaTeX-to-Unicode completion by calling a fallback
426454function ! LaTeXtoUnicode#FallbackCallback ()
@@ -585,6 +613,13 @@ function! s:L2U_SetTab(wait_insert_enter)
585613 imap <buffer> <Tab> <Plug> L2UTab
586614 inoremap <buffer> <expr> <Plug> L2UTab LaTeXtoUnicode#Tab()
587615
616+ if get (g: , " latex_to_unicode_smart_tab" , 0 )
617+ let b: l2u_stab_set = 1
618+ let b: l2u_prev_map_stab = s: L2U_SetFallbackMapping (' <S-Tab>' , s: l2u_fallback_trigger_stab )
619+ imap <buffer> <S-Tab> <Plug> L2USTab
620+ inoremap <buffer> <expr> <Plug> L2USTab LaTeXtoUnicode#STab()
621+ endif
622+
588623 let b: l2u_tab_set = 1
589624endfunction
590625
@@ -606,9 +641,70 @@ function! s:L2U_UnsetTab()
606641 endif
607642 iunmap <buffer> <Plug> L2UTab
608643 exe ' iunmap <buffer> ' . s: l2u_fallback_trigger
644+
645+ if b: l2u_stab_set
646+ iunmap <buffer> <S-Tab>
647+ if empty (maparg (" <S-Tab>" , " i" ))
648+ call s: L2U_ReinstateMapping (b: l2u_prev_map_stab )
649+ endif
650+ iunmap <buffer> <Plug> L2USTab
651+ exe ' iunmap <buffer> ' . s: l2u_fallback_trigger_stab
652+ let b: l2u_stab_set = 0
653+ endif
654+
609655 let b: l2u_tab_set = 0
610656endfunction
611657
658+ function ! s: L2U_SetCR (wait_insert_enter)
659+ if b: l2u_cr_set
660+ return
661+ endif
662+ if ! (b: l2u_enabled && ((b: l2u_tab_set && get (g: , " latex_to_unicode_smart_tab" , 0 )) || b: l2u_autosub_set ))
663+ return
664+ endif
665+ " g:did_insert_enter is set from an autocommand in ftdetect
666+ if a: wait_insert_enter && ! get (g: , " did_insert_enter" , 0 )
667+ return
668+ endif
669+
670+ let b: l2u_prev_map_cr = s: L2U_SetFallbackMapping (' <CR>' , s: l2u_fallback_trigger_cr )
671+ imap <buffer> <CR> <Plug> L2UCR
672+ inoremap <buffer> <expr> <Plug> L2UCR LaTeXtoUnicode#CR()
673+
674+ let b: l2u_cr_set = 1
675+ endfunction
676+
677+ function ! s: L2U_UnsetCR ()
678+ if ! b: l2u_cr_set
679+ return
680+ endif
681+ iunmap <buffer> <CR>
682+ if empty (maparg (" <CR>" , " i" ))
683+ call s: L2U_ReinstateMapping (b: l2u_prev_map_cr )
684+ endif
685+ iunmap <buffer> <Plug> L2UCR
686+ exe ' iunmap <buffer> ' . s: l2u_fallback_trigger_cr
687+ let b: l2u_cr_set = 0
688+ endfunction
689+
690+ function ! LaTeXtoUnicode#CR (... )
691+ if b: l2u_tab_set && get (g: , " latex_to_unicode_smart_tab" , 0 ) && pumvisible () && b: l2u_tab_completing
692+ " accept the selection if any
693+ call feedkeys (" \<C-Y> " , ' n' )
694+ if s: L2U_ismatch ()
695+ let b: l2u_accept_choice = 1
696+ call feedkeys (" \<Tab> " )
697+ endif
698+ return ' '
699+ elseif b: l2u_autosub_set
700+ exec ' call LaTeXtoUnicode#AutoSub("\n", "' . s: l2u_fallback_trigger_cr . ' ")'
701+ return ' '
702+ else
703+ call feedkeys (s: l2u_fallback_trigger_cr )
704+ return ' '
705+ endif
706+ endfunction
707+
612708" Function which looks for viable LaTeX-to-Unicode supstitutions as you type
613709function ! LaTeXtoUnicode#AutoSub (... )
614710 " avoid recursive calls
@@ -690,11 +786,8 @@ function! s:L2U_SetAutoSub(wait_insert_enter)
690786 endif
691787 " Viable substitutions are searched at every character insertion via the
692788 " autocmd InsertCharPre. The <Enter> key does not seem to be catched in
693- " this way though, so we use a mapping for that case.
694-
695- let b: l2u_prev_map_cr = s: L2U_SetFallbackMapping (' <CR>' , s: l2u_fallback_trigger_cr )
696- imap <buffer> <CR> <Plug> L2UAutoSub
697- exec ' inoremap <buffer><expr> <Plug>L2UAutoSub LaTeXtoUnicode#AutoSub("\n", "' . s: l2u_fallback_trigger_cr . ' ")'
789+ " this way though, so we use a mapping for that case, which is handled
790+ " by the L2U_SetCR function
698791
699792 augroup L2UAutoSub
700793 autocmd ! * <buffer>
@@ -710,12 +803,6 @@ function! s:L2U_UnsetAutoSub()
710803 return
711804 endif
712805
713- iunmap <buffer> <CR>
714- if empty (maparg (" <CR>" , " i" ))
715- call s: L2U_ReinstateMapping (b: l2u_prev_map_cr )
716- endif
717- iunmap <buffer> <Plug> L2UAutoSub
718- exe ' iunmap <buffer> ' . s: l2u_fallback_trigger_cr
719806 augroup L2UAutoSub
720807 autocmd ! * <buffer>
721808 augroup END
@@ -750,9 +837,11 @@ function! LaTeXtoUnicode#Init(...)
750837 call s: L2U_UnsetTab ()
751838 call s: L2U_UnsetAutoSub ()
752839 call s: L2U_UnsetKeymap ()
840+ call s: L2U_UnsetCR ()
753841
754842 call s: L2U_SetTab (wait_insert_enter)
755843 call s: L2U_SetAutoSub (wait_insert_enter)
844+ call s: L2U_SetCR (wait_insert_enter)
756845 call s: L2U_SetKeymap ()
757846 return ' '
758847endfunction
0 commit comments