neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

mapping_spec.lua (7301B)


      1 -- Test for mappings and abbreviations
      2 
      3 local t = require('test.testutil')
      4 local n = require('test.functional.testnvim')()
      5 local Screen = require('test.functional.ui.screen')
      6 
      7 local clear, feed, insert = n.clear, n.feed, n.insert
      8 local expect, poke_eventloop = n.expect, n.poke_eventloop
      9 local command, eq, eval, api = n.command, t.eq, n.eval, n.api
     10 local exec = n.exec
     11 local sleep = vim.uv.sleep
     12 
     13 describe('mapping', function()
     14  before_each(clear)
     15 
     16  it('abbreviations with р (0x80)', function()
     17    insert([[
     18      test starts here:
     19      ]])
     20 
     21    -- Abbreviations with р (0x80) should work.
     22    command('inoreab чкпр   vim')
     23    feed('GAчкпр <esc>')
     24 
     25    expect([[
     26      test starts here:
     27      vim ]])
     28  end)
     29 
     30  -- oldtest: Test_map_ctrl_c_insert()
     31  it('Ctrl-c works in Insert mode', function()
     32    -- Mapping of ctrl-c in insert mode
     33    command('set cpo-=< cpo-=k')
     34    command('inoremap <c-c> <ctrl-c>')
     35    command('cnoremap <c-c> dummy')
     36    command('cunmap <c-c>')
     37    feed('GA<cr>')
     38    -- XXX: editor must be in Insert mode before <C-C> is put into input buffer
     39    poke_eventloop()
     40    feed('TEST2: CTRL-C |<c-c>A|<cr><esc>')
     41    command('unmap! <c-c>')
     42 
     43    expect([[
     44 
     45      TEST2: CTRL-C |<ctrl-c>A|
     46      ]])
     47  end)
     48 
     49  -- oldtest: Test_map_ctrl_c_visual()
     50  it('Ctrl-c works in Visual mode', function()
     51    command([[vnoremap <c-c> :<C-u>$put ='vmap works'<cr>]])
     52    feed('GV')
     53    -- XXX: editor must be in Visual mode before <C-C> is put into input buffer
     54    poke_eventloop()
     55    feed('vV<c-c>')
     56    command('vunmap <c-c>')
     57 
     58    expect([[
     59 
     60      vmap works]])
     61  end)
     62 
     63  it('langmap', function()
     64    -- langmap should not get remapped in insert mode.
     65    command('inoremap { FAIL_ilangmap')
     66    command('set langmap=+{ langnoremap')
     67    feed('o+<esc>')
     68 
     69    -- Insert mode expr mapping with langmap.
     70    command('inoremap <expr> { "FAIL_iexplangmap"')
     71    feed('o+<esc>')
     72 
     73    -- langmap should not get remapped in cmdline mode.
     74    command('cnoremap { FAIL_clangmap')
     75    feed('o+<esc>')
     76    command('cunmap {')
     77 
     78    -- cmdline mode expr mapping with langmap.
     79    command('cnoremap <expr> { "FAIL_cexplangmap"')
     80    feed('o+<esc>')
     81    command('cunmap {')
     82 
     83    -- Assert buffer contents.
     84    expect([[
     85 
     86      +
     87      +
     88      +
     89      +]])
     90  end)
     91 
     92  -- oldtest: Test_map_feedkeys()
     93  it('feedkeys', function()
     94    insert([[
     95      a b c d
     96      a b c d
     97      ]])
     98 
     99    -- Vim's issue #212 (feedkeys insert mapping at current position)
    100    command('nnoremap . :call feedkeys(".", "in")<cr>')
    101    feed('/^a b<cr>')
    102    feed('0qqdw.ifoo<esc>qj0@q<esc>')
    103    command('unmap .')
    104    expect([[
    105      fooc d
    106      fooc d
    107      ]])
    108  end)
    109 
    110  -- oldtest: Test_map_cursor()
    111  it('i_CTRL-G_U', function()
    112    -- <c-g>U<cursor> works only within a single line
    113    command('imapclear')
    114    command('imap ( ()<c-g>U<left>')
    115    feed('G2o<esc>ki<cr>Test1: text with a (here some more text<esc>k.')
    116    -- test undo
    117    feed('G2o<esc>ki<cr>Test2: text wit a (here some more text [und undo]<c-g>u<esc>k.u')
    118    command('imapclear')
    119    command('set whichwrap=<,>,[,]')
    120    feed('G3o<esc>2k')
    121    command(
    122      [[:exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."]]
    123    )
    124 
    125    expect([[
    126 
    127 
    128      Test1: text with a (here some more text)
    129      Test1: text with a (here some more text)
    130 
    131 
    132      Test2: text wit a (here some more text [und undo])
    133      new line here
    134      Test3: text with a (parenthesis here
    135      new line here
    136      ]])
    137  end)
    138 
    139  -- oldtest: Test_mouse_drag_mapped_start_select()
    140  it('dragging starts Select mode even if coming from mapping', function()
    141    command('set mouse=a')
    142    command('set selectmode=mouse')
    143 
    144    command('nnoremap <LeftDrag> <LeftDrag><Cmd><CR>')
    145    poke_eventloop()
    146    api.nvim_input_mouse('left', 'press', '', 0, 0, 0)
    147    poke_eventloop()
    148    api.nvim_input_mouse('left', 'drag', '', 0, 0, 1)
    149    poke_eventloop()
    150    eq('s', eval('mode()'))
    151  end)
    152 
    153  -- oldtest: Test_mouse_drag_insert_map()
    154  it('<LeftDrag> mapping in Insert mode works correctly', function()
    155    command('set mouse=a')
    156 
    157    command('inoremap <LeftDrag> <LeftDrag><Cmd>let g:dragged = 1<CR>')
    158    feed('i')
    159    poke_eventloop()
    160    api.nvim_input_mouse('left', 'press', '', 0, 0, 0)
    161    poke_eventloop()
    162    api.nvim_input_mouse('left', 'drag', '', 0, 0, 1)
    163    poke_eventloop()
    164    eq(1, eval('g:dragged'))
    165    eq('v', eval('mode()'))
    166    feed([[<C-\><C-N>]])
    167 
    168    command([[inoremap <LeftDrag> <LeftDrag><C-\><C-N>]])
    169    feed('i')
    170    poke_eventloop()
    171    api.nvim_input_mouse('left', 'press', '', 0, 0, 0)
    172    poke_eventloop()
    173    api.nvim_input_mouse('left', 'drag', '', 0, 0, 1)
    174    poke_eventloop()
    175    eq('n', eval('mode()'))
    176  end)
    177 
    178  -- oldtest: Test_map_after_timed_out_nop()
    179  it('timeout works after an <Nop> mapping is triggered on timeout', function()
    180    command('set timeout timeoutlen=400')
    181    command('inoremap ab TEST')
    182    command('inoremap a <Nop>')
    183    -- Enter Insert mode
    184    feed('i')
    185    -- Wait for the "a" mapping to time out
    186    feed('a')
    187    sleep(500)
    188    -- Send "a" and wait for a period shorter than 'timeoutlen'
    189    feed('a')
    190    sleep(100)
    191    -- Send "b", should trigger the "ab" mapping
    192    feed('b')
    193    expect('TEST')
    194  end)
    195 
    196  -- oldtest: Test_showcmd_part_map()
    197  it("'showcmd' with a partial mapping", function()
    198    local screen = Screen.new(60, 6)
    199    exec([[
    200      set notimeout showcmd
    201      nnoremap ,a <Ignore>
    202      nnoremap ;a <Ignore>
    203      nnoremap Àa <Ignore>
    204      nnoremap Ëa <Ignore>
    205      nnoremap βa <Ignore>
    206      nnoremap ωa <Ignore>
    207      nnoremap a <Ignore>
    208      nnoremap <C-W>a <Ignore>
    209    ]])
    210 
    211    for _, c in ipairs({ ',', ';', 'À', 'Ë', 'β', 'ω', '…' }) do
    212      feed(c)
    213      screen:expect(([[
    214        ^                                                            |
    215        {1:~                                                           }|*4
    216                                                         %s          |
    217      ]]):format(c))
    218      feed('a')
    219      screen:expect([[
    220        ^                                                            |
    221        {1:~                                                           }|*4
    222                                                                    |
    223      ]])
    224    end
    225 
    226    feed('\23')
    227    screen:expect([[
    228      ^                                                            |
    229      {1:~                                                           }|*4
    230                                                       ^W         |
    231    ]])
    232    feed('a')
    233    screen:expect([[
    234      ^                                                            |
    235      {1:~                                                           }|*4
    236                                                                  |
    237    ]])
    238 
    239    feed('<C-W>')
    240    screen:expect([[
    241      ^                                                            |
    242      {1:~                                                           }|*4
    243                                                       ^W         |
    244    ]])
    245    feed('a')
    246    screen:expect([[
    247      ^                                                            |
    248      {1:~                                                           }|*4
    249                                                                  |
    250    ]])
    251  end)
    252 end)