robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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