aboutsummaryrefslogtreecommitdiff
path: root/test/functional/ex_cmds/map_spec.lua
blob: 954dae9335484588389a129a24d1a666519bcec8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')

local eq = t.eq
local exec = n.exec
local exec_capture = n.exec_capture
local feed = n.feed
local api = n.api
local clear = n.clear
local command = n.command
local expect = n.expect
local insert = n.insert
local eval = n.eval

describe(':*map', function()
  before_each(clear)

  it('are not affected by &isident', function()
    api.nvim_set_var('counter', 0)
    command('nnoremap <C-x> :let counter+=1<CR>')
    api.nvim_set_option_value('isident', ('%u'):format(('>'):byte()), {})
    command('nnoremap <C-y> :let counter+=1<CR>')
    -- &isident used to disable keycode parsing here as well
    feed('\24\25<C-x><C-y>')
    eq(4, api.nvim_get_var('counter'))
  end)

  it(':imap <M-">', function()
    command('imap <M-"> foo')
    feed('i-<M-">-')
    expect('-foo-')
  end)

  it('shows <Nop> as mapping rhs', function()
    command('nmap asdf <Nop>')
    eq(
      [[

n  asdf          <Nop>]],
      exec_capture('nmap asdf')
    )
  end)

  it('mappings with description can be filtered', function()
    api.nvim_set_keymap('n', 'asdf1', 'qwert', { desc = 'do the one thing' })
    api.nvim_set_keymap('n', 'asdf2', 'qwert', { desc = 'doesnot really do anything' })
    api.nvim_set_keymap('n', 'asdf3', 'qwert', { desc = 'do the other thing' })
    eq(
      [[

n  asdf3         qwert
                 do the other thing
n  asdf1         qwert
                 do the one thing]],
      exec_capture('filter the nmap')
    )
  end)

  it('<Plug> mappings ignore nore', function()
    command('let x = 0')
    eq(0, api.nvim_eval('x'))
    command [[
      nnoremap <Plug>(Increase_x) <cmd>let x+=1<cr>
      nmap increase_x_remap <Plug>(Increase_x)
      nnoremap increase_x_noremap <Plug>(Increase_x)
    ]]
    feed('increase_x_remap')
    eq(1, api.nvim_eval('x'))
    feed('increase_x_noremap')
    eq(2, api.nvim_eval('x'))
  end)

  it("Doesn't auto ignore nore for keys before or after <Plug> mapping", function()
    command('let x = 0')
    eq(0, api.nvim_eval('x'))
    command [[
      nnoremap x <nop>
      nnoremap <Plug>(Increase_x) <cmd>let x+=1<cr>
      nmap increase_x_remap x<Plug>(Increase_x)x
      nnoremap increase_x_noremap x<Plug>(Increase_x)x
    ]]
    insert('Some text')
    eq('Some text', eval("getline('.')"))

    feed('increase_x_remap')
    eq(1, api.nvim_eval('x'))
    eq('Some text', eval("getline('.')"))
    feed('increase_x_noremap')
    eq(2, api.nvim_eval('x'))
    eq('Some te', eval("getline('.')"))
  end)

  it(':unmap with rhs works when lhs is in another bucket #21530', function()
    command('map F <Plug>Foo')
    command('unmap <Plug>Foo')
    eq('\nNo mapping found', exec_capture('map F'))
  end)
end)

describe('Screen', function()
  local screen
  before_each(function()
    clear()
    screen = Screen.new(20, 5)
  end)

  it('cursor is restored after :map <expr> which calls input()', function()
    command('map <expr> x input("> ")')
    screen:expect([[
      ^                    |
      {1:~                   }|*3
                          |
    ]])
    feed('x')
    screen:expect([[
                          |
      {1:~                   }|*3
      > ^                  |
    ]])
    feed('\n')
    screen:expect([[
      ^                    |
      {1:~                   }|*3
      >                   |
    ]])
  end)

  it('cursor is restored after :imap <expr> which calls input()', function()
    command('imap <expr> x input("> ")')
    feed('i')
    screen:expect([[
      ^                    |
      {1:~                   }|*3
      {5:-- INSERT --}        |
    ]])
    feed('x')
    screen:expect([[
                          |
      {1:~                   }|*3
      > ^                  |
    ]])
    feed('\n')
    screen:expect([[
      ^                    |
      {1:~                   }|*3
      {5:-- INSERT --}        |
    ]])
  end)

  it('cursor position does not move after empty-string :cmap <expr> #19046', function()
    command([[cnoremap <expr> <F2> '']])
    feed(':<F2>')
    screen:expect([[
                          |
      {1:~                   }|*3
      :^                   |
    ]])
  end)

  -- oldtest: Test_expr_map_restore_cursor()
  it('cursor is restored after :map <expr> which redraws statusline vim-patch:8.1.2336', function()
    exec([[
      call setline(1, ['one', 'two', 'three'])
      2
      set ls=2
      hi! link StatusLine ErrorMsg
      noremap <expr> <C-B> Func()
      func Func()
          let g:on = !get(g:, 'on', 0)
          redraws
          return ''
      endfunc
      func Status()
          return get(g:, 'on', 0) ? '[on]' : ''
      endfunc
      set stl=%{Status()}
    ]])
    feed('<C-B>')
    screen:expect([[
      one                 |
      ^two                 |
      three               |
      {9:[on]                }|
                          |
    ]])
  end)

  it('error in :nmap <expr> does not mess up display vim-patch:4.2.4338', function()
    screen:try_resize(40, 5)
    command('nmap <expr> <F2> execute("throw 42")')
    feed('<F2>')
    screen:expect([[
                                              |
      {3:                                        }|
      {9:Error detected while processing :}       |
      {9:E605: Exception not caught: 42}          |
      {6:Press ENTER or type command to continue}^ |
    ]])
    feed('<CR>')
    screen:expect([[
      ^                                        |
      {1:~                                       }|*3
                                              |
    ]])
  end)

  it('error in :cmap <expr> handled correctly vim-patch:4.2.4338', function()
    screen:try_resize(40, 5)
    command('cmap <expr> <F2> execute("throw 42")')
    feed(':echo "foo')
    screen:expect([[
                                              |
      {1:~                                       }|*3
      :echo "foo^                              |
    ]])
    feed('<F2>')
    screen:expect([[
      {3:                                        }|
      :echo "foo                              |
      {9:Error detected while processing :}       |
      {9:E605: Exception not caught: 42}          |
      :echo "foo^                              |
    ]])
    feed('"')
    screen:expect([[
      {3:                                        }|
      :echo "foo                              |
      {9:Error detected while processing :}       |
      {9:E605: Exception not caught: 42}          |
      :echo "foo"^                             |
    ]])
    feed('\n')
    screen:expect([[
      :echo "foo                              |
      {9:Error detected while processing :}       |
      {9:E605: Exception not caught: 42}          |
      foo                                     |
      {6:Press ENTER or type command to continue}^ |
    ]])
  end)

  -- oldtest: Test_map_listing()
  it('listing mappings clears command line vim-patch:8.2.4401', function()
    screen:try_resize(40, 5)
    command('nmap a b')
    feed(':                      nmap a<CR>')
    screen:expect([[
      ^                                        |
      {1:~                                       }|*3
      n  a             b                      |
    ]])
  end)
end)