robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear , feed , insert =n . clear , n . feed , n . insert 6local command =n . command 7local exec_lua =n . exec_lua 8local eval =n . eval 9local expect =n . expect 10local fn =n . fn 11local eq =t . eq 12 13describe ( 'meta-keys #8226 #13042' , function () 14before_each ( function () 15clear () 16end ) 17 18it ( 'ALT/META, normal-mode' , function () 19-- Unmapped ALT-chord behaves as ESC+c. 20insert ( 'hello' ) 21feed ( '0<A-x><M-x>' ) 22expect ( 'llo' ) 23-- Unmapped ALT-chord resolves isolated (non-ALT) ESC mapping. #13086 #15869 24command ( 'nnoremap <ESC> A<lt>ESC><Esc>' ) 25command ( 'nnoremap ; A;<Esc>' ) 26feed ( '<A-;><M-;>' ) 27expect ( 'llo<ESC>;<ESC>;' ) 28-- Mapped ALT-chord behaves as mapped. 29command ( 'nnoremap <M-l> Ameta-l<Esc>' ) 30command ( 'nnoremap <A-j> Aalt-j<Esc>' ) 31feed ( '<A-j><M-l>' ) 32expect ( 'llo<ESC>;<ESC>;alt-jmeta-l' ) 33-- Unmapped ALT-chord with characters containing K_SPECIAL bytes 34command ( 'nnoremap … A…<Esc>' ) 35feed ( '<A-…><M-…>' ) 36expect ( 'llo<ESC>;<ESC>;alt-jmeta-l<ESC>…<ESC>…' ) 37command ( "execute 'nnoremap' nr2char(0x40000000) 'AMAX<Esc>'" ) 38command ( "call nvim_input('<A-'.nr2char(0x40000000).'>')" ) 39command ( "call nvim_input('<M-'.nr2char(0x40000000).'>')" ) 40expect ( 'llo<ESC>;<ESC>;alt-jmeta-l<ESC>…<ESC>…<ESC>MAX<ESC>MAX' ) 41end ) 42 43it ( 'ALT/META, visual-mode' , function () 44-- Unmapped ALT-chords behave as ESC+c 45insert ( 'peaches' ) 46feed ( 'viw<A-x>viw<M-x>' ) 47expect ( 'peach' ) 48-- Unmapped ALT-chord resolves isolated (non-ALT) ESC mapping. #13086 #15869 49command ( 'vnoremap <ESC> A<lt>ESC>' ) 50feed ( 'viw<A-;><Esc>viw<M-;><Esc>' ) 51expect ( 'peach<ESC>;<ESC>;' ) 52-- Mapped ALT-chord behaves as mapped. 53command ( 'vnoremap <M-l> Ameta-l<Esc>' ) 54command ( 'vnoremap <A-j> Aalt-j<Esc>' ) 55feed ( 'viw<A-j>viw<M-l>' ) 56expect ( 'peach<ESC>;<ESC>;alt-jmeta-l' ) 57-- Unmapped ALT-chord with characters containing K_SPECIAL bytes 58feed ( 'viw<A-…><Esc>viw<M-…><Esc>' ) 59expect ( 'peach<ESC>;<ESC>;alt-jmeta-l<ESC>…<ESC>…' ) 60command ( "execute 'inoremap' nr2char(0x40000000) 'MAX'" ) 61command ( "call nvim_input('viw<A-'.nr2char(0x40000000).'><Esc>')" ) 62command ( "call nvim_input('viw<M-'.nr2char(0x40000000).'><Esc>')" ) 63expect ( 'peach<ESC>;<ESC>;alt-jmeta-l<ESC>…<ESC>…<ESC>MAX<ESC>MAX' ) 64end ) 65 66it ( 'ALT/META insert-mode' , function () 67-- Mapped ALT-chord behaves as mapped. 68command ( 'inoremap <M-l> meta-l' ) 69command ( 'inoremap <A-j> alt-j' ) 70feed ( 'i<M-l> xxx <A-j><M-h>a<A-h>' ) 71expect ( 'meta-l xxx alt-j' ) 72eq ( { 0 , 1 , 14 , 0 } , fn . getpos ( '.' )) 73-- Unmapped ALT-chord behaves as ESC+c. 74command ( 'iunmap <M-l>' ) 75feed ( '0i<M-l>' ) 76eq ( { 0 , 1 , 2 , 0 } , fn . getpos ( '.' )) 77-- Unmapped ALT-chord has same `undo` characteristics as ESC+<key> 78command ( '0,$d' ) 79feed ( 'ahello<M-.>' ) 80expect ( 'hellohello' ) 81feed ( 'u' ) 82expect ( 'hello' ) 83end ) 84 85it ( 'ALT/META terminal-mode' , function () 86exec_lua ( [[ 87_G.input_data = '' 88vim.api.nvim_open_term(0, { on_input = function(_, _, _, data) 89_G.input_data = _G.input_data .. vim.fn.strtrans(data) 90end }) 91]] ) 92-- Mapped ALT-chord behaves as mapped. 93command ( 'tnoremap <M-l> meta-l' ) 94command ( 'tnoremap <A-j> alt-j' ) 95feed ( 'i<M-l> xxx <A-j>' ) 96eq ( 'meta-l xxx alt-j' , exec_lua ( [[return _G.input_data]] )) 97-- Unmapped ALT-chord is sent to terminal as-is. #16202 #16220 98exec_lua ( [[_G.input_data = '']] ) 99command ( 'tunmap <M-l>' ) 100feed ( '<M-l>' ) 101local meta_l_seq =exec_lua ( [[return _G.input_data]] ) 102command ( 'tnoremap <Esc> <C-\\><C-N>' ) 103feed ( 'yyy<M-l><A-j>' ) 104eq ( meta_l_seq .. 'yyy' .. meta_l_seq .. 'alt-j' , exec_lua ( [[return _G.input_data]] )) 105eq ( 't' , eval ( 'mode(1)' )) 106feed ( '<Esc>j' ) 107eq ( { 0 , 2 , 1 , 0 } , fn . getpos ( '.' )) 108eq ( 'nt' , eval ( 'mode(1)' )) 109end ) 110 111it ( 'ALT/META when recording a macro #13235' , function () 112command ( 'inoremap <M-Esc> <lt>M-ESC>' ) 113feed ( 'ifoo<CR>bar<CR>baz<Esc>gg0' ) 114-- <M-"> is reinterpreted as <Esc>" 115feed ( 'qrviw"ayC// This is some text: <M-">apq' ) 116expect ( [[ 117// This is some text: foo 118bar 119baz]] ) 120-- Should not insert an extra double quote or trigger <M-Esc> when replaying 121feed ( 'j0@rj0@@' ) 122expect ( [[ 123// This is some text: foo 124// This is some text: bar 125// This is some text: baz]] ) 126command ( '%delete' ) 127end ) 128 129it ( 'ALT/META with special key when recording a macro' , function () 130command ( 'inoremap <M-Esc> <lt>M-ESC>' ) 131command ( 'noremap <S-Tab> "' ) 132command ( 'noremap! <S-Tab> "' ) 133feed ( 'ifoo<CR>bar<CR>baz<Esc>gg0' ) 134-- <M-S-Tab> is reinterpreted as <Esc><S-Tab> 135feed ( 'qrviw<S-Tab>ayC// This is some text: <M-S-Tab>apq' ) 136expect ( [[ 137// This is some text: foo 138bar 139baz]] ) 140-- Should not insert an extra double quote or trigger <M-Esc> when replaying 141feed ( 'j0@rj0@@' ) 142expect ( [[ 143// This is some text: foo 144// This is some text: bar 145// This is some text: baz]] ) 146end ) 147 148it ( 'ALT/META with vim.on_key()' , function () 149feed ( 'ifoo<CR>bar<CR>baz<Esc>gg0viw"ay' ) 150command ( 'nnoremap … "' ) 151 152exec_lua [[ 153keys = {} 154typed = {} 155 156vim.on_key(function(buf, typed_buf) 157table.insert(keys, vim.fn.keytrans(buf)) 158table.insert(typed, vim.fn.keytrans(typed_buf)) 159end) 160]] 161 162-- <M-"> and <M-…> are reinterpreted as <Esc>" and <Esc>… 163feed ( 'c$FOO.<M-">apA.<M-…>ap' ) 164expect ( [[ 165FOO.foo.foo 166bar 167baz]] ) 168 169-- vim.on_key() callback should only receive <Esc>" and <Esc>… 170eq ( 'c$FOO.<Esc>"apA.<Esc>"ap' , exec_lua [[return table.concat(keys, '')]] ) 171eq ( 'c$FOO.<Esc>"apA.<Esc>…ap' , exec_lua [[return table.concat(typed, '')]] ) 172end ) 173end )