robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
256a88d0ac
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 =n . clear 6local command =n . command 7local eq , neq =t . eq , t . neq 8local exec_lua =n . exec_lua 9local exec =n . exec 10local feed =n . feed 11local fn =n . fn 12local api =n . api 13local matches =t . matches 14local source =n . source 15local pcall_err =t . pcall_err 16 17local shallowcopy =t . shallowcopy 18local sleep =vim . uv . sleep 19 20local sid_api_client =- 9 21local sid_lua =- 8 22 23local mode_bits_map ={ 24[ 'n' ] =0x01 , 25[ 'x' ] =0x02 , 26[ 'o' ] =0x04 , 27[ 'c' ] =0x08 , 28[ 'i' ] =0x10 , 29[ 'l' ] =0x20 , 30[ 's' ] =0x40 , 31[ 't' ] =0x80 , 32[ ' ' ] =0x47 , 33[ 'v' ] =0x42 , 34[ '!' ] =0x18 , 35} 36 37describe ( 'nvim_get_keymap' , function () 38before_each ( clear ) 39 40-- Basic mapping and table to be used to describe results 41local foo_bar_string ='nnoremap foo bar' 42local foo_bar_map_table ={ 43lhs ='foo' , 44lhsraw ='foo' , 45script =0 , 46silent =0 , 47rhs ='bar' , 48expr =0 , 49replace_keycodes =0 , 50sid =0 , 51scriptversion =1 , 52buf =0 , 53buffer =0 , -- deprecated 54nowait =0 , 55mode ='n' , 56mode_bits =0x01 , 57abbr =0 , 58noremap =1 , 59lnum =0 , 60} 61 62it ( 'returns empty list when no map' , function () 63eq ( {} , api . nvim_get_keymap ( 'n' )) 64end ) 65 66it ( 'returns list of all applicable mappings' , function () 67command ( foo_bar_string ) 68-- Only one mapping available. Should be the same as `foo_bar_map_table`, and maparg() result 69-- (plus "buf"), since this is a global map. 70eq ( { foo_bar_map_table } , api . nvim_get_keymap ( 'n' )) 71-- Add "buf" key to maparg() result. 72local maparg_rv =vim . tbl_deep_extend ( 'force' , fn . maparg ( 'foo' , 'n' , false , true ), { buf =0 } ) 73eq ( { maparg_rv } , api . nvim_get_keymap ( 'n' )) 74 75-- Add another mapping 76command ( 'nnoremap foo_longer bar_longer' ) 77local foolong_bar_map_table =shallowcopy ( foo_bar_map_table ) 78foolong_bar_map_table [ 'lhs' ] ='foo_longer' 79foolong_bar_map_table [ 'lhsraw' ] ='foo_longer' 80foolong_bar_map_table [ 'rhs' ] ='bar_longer' 81 82eq ( { foolong_bar_map_table , foo_bar_map_table } , api . nvim_get_keymap ( 'n' )) 83 84-- Remove a mapping 85command ( 'unmap foo_longer' ) 86eq ( { foo_bar_map_table } , api . nvim_get_keymap ( 'n' )) 87end ) 88 89it ( 'works for other modes' , function () 90-- Add two mappings, one in insert and one normal 91-- We'll only check the insert mode one 92command ( 'nnoremap not_going to_check' ) 93 94command ( 'inoremap foo bar' ) 95-- The table will be the same except for the mode 96local insert_table =shallowcopy ( foo_bar_map_table ) 97insert_table [ 'mode' ] ='i' 98insert_table [ 'mode_bits' ] =0x10 99 100eq ( { insert_table } , api . nvim_get_keymap ( 'i' )) 101end ) 102 103it ( 'considers scope' , function () 104-- change the map slightly 105command ( 'nnoremap foo_longer bar_longer' ) 106local foolong_bar_map_table =shallowcopy ( foo_bar_map_table ) 107foolong_bar_map_table [ 'lhs' ] ='foo_longer' 108foolong_bar_map_table [ 'lhsraw' ] ='foo_longer' 109foolong_bar_map_table [ 'rhs' ] ='bar_longer' 110 111local buffer_table =shallowcopy ( foo_bar_map_table ) 112buffer_table [ 'buf' ] =1 113buffer_table [ 'buffer' ] =1 -- deprecated 114 115command ( 'nnoremap <buffer> foo bar' ) 116 117-- The buffer mapping should not show up 118eq ( { foolong_bar_map_table } , api . nvim_get_keymap ( 'n' )) 119eq ( { buffer_table } , api . nvim_buf_get_keymap ( 0 , 'n' )) 120end ) 121 122it ( 'considers scope for overlapping maps' , function () 123command ( 'nnoremap foo bar' ) 124 125local buffer_table =shallowcopy ( foo_bar_map_table ) 126buffer_table [ 'buf' ] =1 127buffer_table [ 'buffer' ] =1 -- deprecated 128 129command ( 'nnoremap <buffer> foo bar' ) 130 131eq ( { foo_bar_map_table } , api . nvim_get_keymap ( 'n' )) 132eq ( { buffer_table } , api . nvim_buf_get_keymap ( 0 , 'n' )) 133end ) 134 135it ( 'can retrieve mapping for different buffers' , function () 136local original_buffer =api . nvim_buf_get_number ( 0 ) 137-- Place something in each of the buffers to make sure they stick around 138-- and set hidden so we can leave them 139command ( 'set hidden' ) 140command ( 'new' ) 141command ( 'normal! ihello 2' ) 142command ( 'new' ) 143command ( 'normal! ihello 3' ) 144 145local final_buffer =api . nvim_buf_get_number ( 0 ) 146 147command ( 'nnoremap <buffer> foo bar' ) 148-- Final buffer will have buffer mappings 149local buffer_table =shallowcopy ( foo_bar_map_table ) 150buffer_table [ 'buf' ] =final_buffer 151buffer_table [ 'buffer' ] =final_buffer -- deprecated 152eq ( { buffer_table } , api . nvim_buf_get_keymap ( final_buffer , 'n' )) 153eq ( { buffer_table } , api . nvim_buf_get_keymap ( 0 , 'n' )) 154 155command ( 'buffer ' .. original_buffer ) 156eq ( original_buffer , api . nvim_buf_get_number ( 0 )) 157-- Original buffer won't have any mappings 158eq ( {} , api . nvim_get_keymap ( 'n' )) 159eq ( {} , api . nvim_buf_get_keymap ( 0 , 'n' )) 160eq ( { buffer_table } , api . nvim_buf_get_keymap ( final_buffer , 'n' )) 161end ) 162 163-- Test toggle switches for basic options 164-- @param option The key represented in the `maparg()` result dict 165local function global_and_buffer_test ( 166map , 167option , 168option_token , 169global_on_result , 170buffer_on_result , 171global_off_result , 172buffer_off_result , 173new_windows 174) 175local function make_new_windows ( number_of_windows ) 176if new_windows == nil then 177return nil 178end 179 180for _ =1 , number_of_windows do 181command ( 'new' ) 182end 183end 184 185local mode =string . sub ( map , 1 , 1 ) 186-- Don't run this for the <buffer> mapping, since it doesn't make sense 187if option_token ~= '<buffer>' then 188it ( 189string . format ( 190'returns %d for the key "%s" when %s is used globally with %s (%s)' , 191global_on_result , 192option , 193option_token , 194map , 195mode 196), 197function () 198make_new_windows ( new_windows ) 199command ( map .. ' ' .. option_token .. ' foo bar' ) 200local result =api . nvim_get_keymap ( mode )[ 1 ][ option ] 201eq ( global_on_result , result ) 202end 203) 204end 205 206it ( 207string . format ( 208'returns %d for the key "%s" when %s is used for buffers with %s (%s)' , 209buffer_on_result , 210option , 211option_token , 212map , 213mode 214), 215function () 216make_new_windows ( new_windows ) 217command ( map .. ' <buffer> ' .. option_token .. ' foo bar' ) 218local result =api . nvim_buf_get_keymap ( 0 , mode )[ 1 ][ option ] 219eq ( buffer_on_result , result ) 220end 221) 222 223-- Don't run these for the <buffer> mapping, since it doesn't make sense 224if option_token ~= '<buffer>' then 225it ( 226string . format ( 227'returns %d for the key "%s" when %s is not used globally with %s (%s)' , 228global_off_result , 229option , 230option_token , 231map , 232mode 233), 234function () 235make_new_windows ( new_windows ) 236command ( map .. ' baz bat' ) 237local result =api . nvim_get_keymap ( mode )[ 1 ][ option ] 238eq ( global_off_result , result ) 239end 240) 241 242it ( 243string . format ( 244'returns %d for the key "%s" when %s is not used for buffers with %s (%s)' , 245buffer_off_result , 246option , 247option_token , 248map , 249mode 250), 251function () 252make_new_windows ( new_windows ) 253command ( map .. ' <buffer> foo bar' ) 254 255local result =api . nvim_buf_get_keymap ( 0 , mode )[ 1 ][ option ] 256eq ( buffer_off_result , result ) 257end 258) 259end 260end 261 262-- Standard modes and returns the same values in the dictionary as maparg() 263local mode_list ={ 'nnoremap' , 'nmap' , 'imap' , 'inoremap' , 'cnoremap' } 264for mode in pairs ( mode_list ) do 265global_and_buffer_test ( mode_list [ mode ], 'silent' , '<silent>' , 1 , 1 , 0 , 0 ) 266global_and_buffer_test ( mode_list [ mode ], 'nowait' , '<nowait>' , 1 , 1 , 0 , 0 ) 267global_and_buffer_test ( mode_list [ mode ], 'expr' , '<expr>' , 1 , 1 , 0 , 0 ) 268end 269 270-- noremap will now be 2 if script was used, which is not the same as maparg() 271global_and_buffer_test ( 'nmap' , 'noremap' , '<script>' , 2 , 2 , 0 , 0 ) 272global_and_buffer_test ( 'nnoremap' , 'noremap' , '<script>' , 2 , 2 , 1 , 1 ) 273 274-- buffer will return the buffer ID, which is not the same as maparg() 275-- Three of these tests won't run 276global_and_buffer_test ( 'nnoremap' , 'buffer' , '<buffer>' , nil , 3 , nil , nil , 2 ) 277 278it ( 'returns script numbers for global maps' , function () 279source ( [[ 280function! s:maparg_test_function() abort 281return 'testing' 282endfunction 283 284nnoremap fizz :call <SID>maparg_test_function()<CR> 285]] ) 286local sid_result =api . nvim_get_keymap ( 'n' )[ 1 ][ 'sid' ] 287eq ( 1 , sid_result ) 288eq ( 'testing' , api . nvim_call_function ( '<SNR>' .. sid_result .. '_maparg_test_function' , {} )) 289end ) 290 291it ( 'returns script numbers for buffer maps' , function () 292source ( [[ 293function! s:maparg_test_function() abort 294return 'testing' 295endfunction 296 297nnoremap <buffer> fizz :call <SID>maparg_test_function()<CR> 298]] ) 299local sid_result =api . nvim_buf_get_keymap ( 0 , 'n' )[ 1 ][ 'sid' ] 300eq ( 1 , sid_result ) 301eq ( 'testing' , api . nvim_call_function ( '<SNR>' .. sid_result .. '_maparg_test_function' , {} )) 302end ) 303 304it ( 'works with <F12> and others' , function () 305command ( 'nnoremap <F12> :let g:maparg_test_var = 1<CR>' ) 306eq ( '<F12>' , api . nvim_get_keymap ( 'n' )[ 1 ][ 'lhs' ]) 307eq ( ':let g:maparg_test_var = 1<CR>' , api . nvim_get_keymap ( 'n' )[ 1 ][ 'rhs' ]) 308end ) 309 310it ( "works correctly despite 'cpoptions'" , function () 311local cpo_table ={ 312script =0 , 313silent =0 , 314expr =0 , 315replace_keycodes =0 , 316sid =0 , 317scriptversion =1 , 318buf =0 , 319buffer =0 , -- deprecated 320nowait =0 , 321abbr =0 , 322noremap =1 , 323lnum =0 , 324} 325local function cpomap ( lhs , rhs , mode ) 326local ret =shallowcopy ( cpo_table ) 327local lhsraw =api . nvim_eval (( '"%s"' ): format ( lhs : gsub ( '\\' , '\\\\' ): gsub ( '<' , '\\<*' ))) 328local lhsrawalt =api . nvim_eval (( '"%s"' ): format ( lhs : gsub ( '\\' , '\\\\' ): gsub ( '<' , '\\<' ))) 329ret . lhs =lhs 330ret . lhsraw =lhsraw 331ret . lhsrawalt =lhsrawalt ~= lhsraw and lhsrawalt or nil 332ret . rhs =rhs 333ret . mode =mode 334ret . mode_bits =mode_bits_map [ mode ] 335return ret 336end 337 338command ( 'set cpo+=B' ) 339command ( 'nnoremap \\<C-a><C-a><LT>C-a>\\ \\<C-b><C-b><LT>C-b>\\' ) 340command ( 'nnoremap <special> \\<C-c><C-c><LT>C-c>\\ \\<C-d><C-d><LT>C-d>\\' ) 341 342command ( 'set cpo+=B' ) 343command ( 'xnoremap \\<C-a><C-a><LT>C-a>\\ \\<C-b><C-b><LT>C-b>\\' ) 344command ( 'xnoremap <special> \\<C-c><C-c><LT>C-c>\\ \\<C-d><C-d><LT>C-d>\\' ) 345 346command ( 'set cpo-=B' ) 347command ( 'snoremap \\<C-a><C-a><LT>C-a>\\ \\<C-b><C-b><LT>C-b>\\' ) 348command ( 'snoremap <special> \\<C-c><C-c><LT>C-c>\\ \\<C-d><C-d><LT>C-d>\\' ) 349 350command ( 'set cpo-=B' ) 351command ( 'onoremap \\<C-a><C-a><LT>C-a>\\ \\<C-b><C-b><LT>C-b>\\' ) 352command ( 'onoremap <special> \\<C-c><C-c><LT>C-c>\\ \\<C-d><C-d><LT>C-d>\\' ) 353 354for _ , cmd in ipairs ( { 355'set cpo-=B' , 356'set cpo+=B' , 357} ) do 358command ( cmd ) 359eq ( { 360cpomap ( '\\<C-C><C-C><lt>C-c>\\' , '\\<C-D><C-D><lt>C-d>\\' , 'n' ), 361cpomap ( '\\<C-A><C-A><lt>C-a>\\' , '\\<C-B><C-B><lt>C-b>\\' , 'n' ), 362} , api . nvim_get_keymap ( 'n' )) 363eq ( { 364cpomap ( '\\<C-C><C-C><lt>C-c>\\' , '\\<C-D><C-D><lt>C-d>\\' , 'x' ), 365cpomap ( '\\<C-A><C-A><lt>C-a>\\' , '\\<C-B><C-B><lt>C-b>\\' , 'x' ), 366} , api . nvim_get_keymap ( 'x' )) 367eq ( { 368cpomap ( '<lt>C-c><C-C><lt>C-c> ' , '<lt>C-d><C-D><lt>C-d>' , 's' ), 369cpomap ( '<lt>C-a><C-A><lt>C-a> ' , '<lt>C-b><C-B><lt>C-b>' , 's' ), 370} , api . nvim_get_keymap ( 's' )) 371eq ( { 372cpomap ( '<lt>C-c><C-C><lt>C-c> ' , '<lt>C-d><C-D><lt>C-d>' , 'o' ), 373cpomap ( '<lt>C-a><C-A><lt>C-a> ' , '<lt>C-b><C-B><lt>C-b>' , 'o' ), 374} , api . nvim_get_keymap ( 'o' )) 375end 376end ) 377 378it ( 'always uses space for space and bar for bar' , function () 379local space_table ={ 380lhs ='| |' , 381lhsraw ='| |' , 382rhs ='| |' , 383mode ='n' , 384mode_bits =0x01 , 385abbr =0 , 386script =0 , 387silent =0 , 388expr =0 , 389replace_keycodes =0 , 390sid =0 , 391scriptversion =1 , 392buf =0 , 393buffer =0 , -- deprecated 394nowait =0 , 395noremap =1 , 396lnum =0 , 397} 398command ( 'nnoremap \\|<Char-0x20><Char-32><Space><Bar> \\|<Char-0x20><Char-32><Space> <Bar>' ) 399eq ( { space_table } , api . nvim_get_keymap ( 'n' )) 400end ) 401 402it ( 'can handle lua mappings' , function () 403eq ( 4040 , 405exec_lua ( [[ 406GlobalCount = 0 407vim.api.nvim_set_keymap('n', 'asdf', '', { 408callback = function() GlobalCount = GlobalCount + 1 end, 409}) 410return GlobalCount 411]] ) 412) 413 414feed ( 'asdf\n' ) 415eq ( 1 , exec_lua ( [[return GlobalCount]] )) 416 417eq ( 4182 , 419exec_lua ( [[ 420vim.api.nvim_get_keymap('n')[1].callback() 421return GlobalCount 422]] ) 423) 424 425exec ( [[ 426call nvim_get_keymap('n')[0].callback() 427]] ) 428eq ( 3 , exec_lua ( [[return GlobalCount]] )) 429 430local mapargs =api . nvim_get_keymap ( 'n' ) 431mapargs [ 1 ]. callback =nil 432eq ( { 433lhs ='asdf' , 434lhsraw ='asdf' , 435script =0 , 436silent =0 , 437expr =0 , 438replace_keycodes =0 , 439sid =sid_lua , 440scriptversion =1 , 441buf =0 , 442buffer =0 , -- deprecated 443nowait =0 , 444mode ='n' , 445mode_bits =0x01 , 446abbr =0 , 447noremap =0 , 448lnum =0 , 449} , mapargs [ 1 ]) 450end ) 451 452it ( 'can handle map descriptions' , function () 453api . nvim_set_keymap ( 'n' , 'lhs' , 'rhs' , { desc ='map description' } ) 454eq ( { 455lhs ='lhs' , 456lhsraw ='lhs' , 457rhs ='rhs' , 458script =0 , 459silent =0 , 460expr =0 , 461replace_keycodes =0 , 462sid =sid_api_client , 463scriptversion =1 , 464buf =0 , 465buffer =0 , -- deprecated 466nowait =0 , 467mode ='n' , 468mode_bits =0x01 , 469abbr =0 , 470noremap =0 , 471lnum =0 , 472desc ='map description' , 473} , api . nvim_get_keymap ( 'n' )[ 1 ]) 474end ) 475 476it ( 'can get abbreviations' , function () 477command ( 'inoreabbr foo bar' ) 478command ( 'cnoreabbr <buffer> foo baz' ) 479 480local mapargs_i ={ 481abbr =1 , 482buf =0 , 483buffer =0 , -- deprecated 484expr =0 , 485replace_keycodes =0 , 486lhs ='foo' , 487lhsraw ='foo' , 488lnum =0 , 489mode ='i' , 490mode_bits =0x10 , 491noremap =1 , 492nowait =0 , 493rhs ='bar' , 494script =0 , 495scriptversion =1 , 496sid =0 , 497silent =0 , 498} 499local mapargs_c ={ 500abbr =1 , 501buf =1 , 502buffer =1 , -- deprecated 503expr =0 , 504replace_keycodes =0 , 505lhs ='foo' , 506lhsraw ='foo' , 507lnum =0 , 508mode ='c' , 509mode_bits =0x08 , 510noremap =1 , 511nowait =0 , 512rhs ='baz' , 513script =0 , 514scriptversion =1 , 515sid =0 , 516silent =0 , 517} 518 519local curbuf =api . nvim_get_current_buf () 520 521eq ( { mapargs_i } , api . nvim_get_keymap ( 'ia' )) 522eq ( {} , api . nvim_buf_get_keymap ( curbuf , 'ia' )) 523 524eq ( {} , api . nvim_get_keymap ( 'ca' )) 525eq ( { mapargs_c } , api . nvim_buf_get_keymap ( curbuf , 'ca' )) 526 527eq ( { mapargs_i } , api . nvim_get_keymap ( '!a' )) 528eq ( { mapargs_c } , api . nvim_buf_get_keymap ( curbuf , '!a' )) 529end ) 530end ) 531 532describe ( 'nvim_set_keymap, nvim_del_keymap' , function () 533before_each ( clear ) 534 535-- `generate_expected` is truthy: for generating an expected output for 536-- maparg(), which does not accept "!" (though it returns "!" in its output 537-- if getting a mapping set with |:map!|). 538local function normalize_mapmode ( mode , generate_expected ) 539if mode : sub ( - 1 ) == 'a' then 540mode =mode : sub ( 1 , - 2 ) 541end 542if not generate_expected and mode == '!' then 543-- Cannot retrieve mapmode-ic mappings with "!", but can with "i" or "c". 544mode ='i' 545elseif mode == '' then 546mode =generate_expected and ' ' or mode 547end 548return mode 549end 550 551-- Generate a mapargs dict, for comparison against the mapping that was 552-- actually set 553local function generate_mapargs ( mode , lhs , rhs , opts ) 554if not opts then 555opts ={} 556end 557 558local to_return ={} 559local expected_mode =normalize_mapmode ( mode , true ) 560to_return . mode =expected_mode 561to_return . mode_bits =mode_bits_map [ expected_mode ] 562to_return . abbr =mode : sub ( - 1 ) == 'a' and 1 or 0 563to_return . noremap =not opts . noremap and 0 or 1 564to_return . lhs =lhs 565to_return . rhs =rhs 566to_return . script =0 567to_return . silent =not opts . silent and 0 or 1 568to_return . nowait =not opts . nowait and 0 or 1 569to_return . expr =not opts . expr and 0 or 1 570to_return . replace_keycodes =not opts . replace_keycodes and 0 or 1 571to_return . sid =not opts . sid and sid_api_client or opts . sid 572to_return . scriptversion =1 573-- to_return.buf = not opts.buffer and 0 or opts.buffer 574to_return . buffer =not opts . buffer and 0 or opts . buffer 575to_return . lnum =not opts . lnum and 0 or opts . lnum 576to_return . desc =opts . desc 577 578return to_return 579end 580 581-- Gets a maparg() dict from Nvim, if one exists. 582local function get_mapargs ( mode , lhs ) 583local mapargs =fn . maparg ( lhs , normalize_mapmode ( mode ), mode : sub ( - 1 ) == 'a' , true ) 584-- drop "lhsraw" and "lhsrawalt" which are hard to check 585mapargs . lhsraw =nil 586mapargs . lhsrawalt =nil 587return mapargs 588end 589 590it ( 'error on empty LHS' , function () 591-- escape parentheses in lua string, else comparison fails erroneously 592eq ( 'Invalid (empty) LHS' , pcall_err ( api . nvim_set_keymap , '' , '' , 'rhs' , {} )) 593eq ( 'Invalid (empty) LHS' , pcall_err ( api . nvim_set_keymap , '' , '' , '' , {} )) 594eq ( 'Invalid (empty) LHS' , pcall_err ( api . nvim_del_keymap , '' , '' )) 595end ) 596 597it ( 'del_keymap preserves {rhs} fallback by default #30258' , function () 598api . nvim_set_keymap ( 'n' , 'ge' , 'q' , {} ) 599api . nvim_del_keymap ( 'n' , 'q' ) 600eq ( {} , get_mapargs ( 'n' , 'ge' )) 601end ) 602 603it ( 'del_keymap with opts.lhs matches only {lhs}, never {rhs} #30258' , function () 604api . nvim_set_keymap ( 'n' , 'ge' , 'q' , {} ) 605eq ( 'E31: No such mapping' , pcall_err ( api . nvim_del_keymap , 'n' , 'q' , { lhs =true } )) 606eq ( generate_mapargs ( 'n' , 'ge' , 'q' ), get_mapargs ( 'n' , 'ge' )) 607 608api . nvim_del_keymap ( 'n' , 'ge' , { lhs =true } ) 609eq ( {} , get_mapargs ( 'n' , 'ge' )) 610end ) 611 612it ( 'del_keymap for abbreviations with opts.lhs matches only {lhs}, never {rhs} #30258' , function () 613api . nvim_set_keymap ( 'ia' , 'foo' , 'bar' , {} ) 614eq ( 'E31: No such mapping' , pcall_err ( api . nvim_del_keymap , 'ia' , 'bar' , { lhs =true } )) 615eq ( generate_mapargs ( 'ia' , 'foo' , 'bar' ), get_mapargs ( 'ia' , 'foo' )) 616 617api . nvim_del_keymap ( 'ia' , 'foo' , { lhs =true } ) 618eq ( {} , get_mapargs ( 'ia' , 'foo' )) 619end ) 620 621it ( 'buf_del_keymap with opts.lhs matches only {lhs}, never {rhs} #30258' , function () 622api . nvim_buf_set_keymap ( 0 , 'n' , 'ge' , 'q' , {} ) 623eq ( 'E31: No such mapping' , pcall_err ( api . nvim_buf_del_keymap , 0 , 'n' , 'q' , { lhs =true } )) 624eq ( generate_mapargs ( 'n' , 'ge' , 'q' , { buffer =1 } ), get_mapargs ( 'n' , 'ge' )) 625 626api . nvim_buf_del_keymap ( 0 , 'n' , 'ge' , { lhs =true } ) 627eq ( {} , get_mapargs ( 'n' , 'ge' )) 628end ) 629 630it ( 'error if LHS longer than MAXMAPLEN' , function () 631-- assume MAXMAPLEN of 50 chars, as declared in mapping_defs.h 632local MAXMAPLEN =50 633local lhs ='' 634for i =1 , MAXMAPLEN do 635lhs =lhs .. ( i % 10 ) 636end 637 638-- exactly 50 chars should be fine 639api . nvim_set_keymap ( '' , lhs , 'rhs' , {} ) 640 641-- del_keymap should unmap successfully 642api . nvim_del_keymap ( '' , lhs ) 643eq ( {} , get_mapargs ( '' , lhs )) 644 645-- 51 chars should produce an error 646lhs =lhs .. '1' 647eq ( 648'LHS exceeds maximum map length: ' .. lhs , 649pcall_err ( api . nvim_set_keymap , '' , lhs , 'rhs' , {} ) 650) 651eq ( 'LHS exceeds maximum map length: ' .. lhs , pcall_err ( api . nvim_del_keymap , '' , lhs )) 652end ) 653 654it ( 'does not leak callback LuaRef on too-long LHS #39351' , function () 655eq ( 6560 , 657exec_lua ( function () 658local weak =setmetatable ( {} , { __mode ='v' } ) 659for i =1 , 2 do 660local cb =function () end 661weak [ i ] =cb 662local ok =pcall ( vim . api . nvim_set_keymap , 'n' , ( 'a' ): rep ( 66 ), '' , { callback =cb } ) 663assert ( not ok ) 664end 665collectgarbage ( 'collect' ) 666return vim . tbl_count ( weak ) 667end ) 668) 669end ) 670 671it ( 'does not throw errors when rhs is longer than MAXMAPLEN' , function () 672local MAXMAPLEN =50 673local rhs ='' 674for i =1 , MAXMAPLEN do 675rhs =rhs .. ( i % 10 ) 676end 677rhs =rhs .. '1' 678api . nvim_set_keymap ( '' , 'lhs' , rhs , {} ) 679eq ( generate_mapargs ( '' , 'lhs' , rhs ), get_mapargs ( '' , 'lhs' )) 680end ) 681 682it ( 'error on invalid mode shortname' , function () 683eq ( 'Invalid mode shortname: " "' , pcall_err ( api . nvim_set_keymap , ' ' , 'lhs' , 'rhs' , {} )) 684eq ( 'Invalid mode shortname: "m"' , pcall_err ( api . nvim_set_keymap , 'm' , 'lhs' , 'rhs' , {} )) 685eq ( 'Invalid mode shortname: "?"' , pcall_err ( api . nvim_set_keymap , '?' , 'lhs' , 'rhs' , {} )) 686eq ( 'Invalid mode shortname: "y"' , pcall_err ( api . nvim_set_keymap , 'y' , 'lhs' , 'rhs' , {} )) 687eq ( 'Invalid mode shortname: "p"' , pcall_err ( api . nvim_set_keymap , 'p' , 'lhs' , 'rhs' , {} )) 688eq ( 'Invalid mode shortname: "a"' , pcall_err ( api . nvim_set_keymap , 'a' , 'lhs' , 'rhs' , {} )) 689eq ( 'Invalid mode shortname: "oa"' , pcall_err ( api . nvim_set_keymap , 'oa' , 'lhs' , 'rhs' , {} )) 690eq ( 'Invalid mode shortname: "!o"' , pcall_err ( api . nvim_set_keymap , '!o' , 'lhs' , 'rhs' , {} )) 691eq ( 'Invalid mode shortname: "!i"' , pcall_err ( api . nvim_set_keymap , '!i' , 'lhs' , 'rhs' , {} )) 692eq ( 'Invalid mode shortname: "!!"' , pcall_err ( api . nvim_set_keymap , '!!' , 'lhs' , 'rhs' , {} )) 693eq ( 'Invalid mode shortname: "map"' , pcall_err ( api . nvim_set_keymap , 'map' , 'lhs' , 'rhs' , {} )) 694eq ( 'Invalid mode shortname: "vmap"' , pcall_err ( api . nvim_set_keymap , 'vmap' , 'lhs' , 'rhs' , {} )) 695eq ( 696'Invalid mode shortname: "xnoremap"' , 697pcall_err ( api . nvim_set_keymap , 'xnoremap' , 'lhs' , 'rhs' , {} ) 698) 699eq ( 'Invalid mode shortname: " "' , pcall_err ( api . nvim_del_keymap , ' ' , 'lhs' )) 700eq ( 'Invalid mode shortname: "m"' , pcall_err ( api . nvim_del_keymap , 'm' , 'lhs' )) 701eq ( 'Invalid mode shortname: "?"' , pcall_err ( api . nvim_del_keymap , '?' , 'lhs' )) 702eq ( 'Invalid mode shortname: "y"' , pcall_err ( api . nvim_del_keymap , 'y' , 'lhs' )) 703eq ( 'Invalid mode shortname: "p"' , pcall_err ( api . nvim_del_keymap , 'p' , 'lhs' )) 704eq ( 'Invalid mode shortname: "a"' , pcall_err ( api . nvim_del_keymap , 'a' , 'lhs' )) 705eq ( 'Invalid mode shortname: "oa"' , pcall_err ( api . nvim_del_keymap , 'oa' , 'lhs' )) 706eq ( 'Invalid mode shortname: "!o"' , pcall_err ( api . nvim_del_keymap , '!o' , 'lhs' )) 707eq ( 'Invalid mode shortname: "!i"' , pcall_err ( api . nvim_del_keymap , '!i' , 'lhs' )) 708eq ( 'Invalid mode shortname: "!!"' , pcall_err ( api . nvim_del_keymap , '!!' , 'lhs' )) 709eq ( 'Invalid mode shortname: "map"' , pcall_err ( api . nvim_del_keymap , 'map' , 'lhs' )) 710eq ( 'Invalid mode shortname: "vmap"' , pcall_err ( api . nvim_del_keymap , 'vmap' , 'lhs' )) 711eq ( 'Invalid mode shortname: "xnoremap"' , pcall_err ( api . nvim_del_keymap , 'xnoremap' , 'lhs' )) 712end ) 713 714it ( 'validation' , function () 715eq ( 716"Invalid key: 'silentt'" , 717pcall_err ( api . nvim_set_keymap , 'n' , 'lhs' , 'rhs' , { silentt =true } ) 718) 719eq ( "Invalid key: 'sidd'" , pcall_err ( api . nvim_set_keymap , 'n' , 'lhs' , 'rhs' , { sidd =false } )) 720eq ( 721"Invalid key: 'nowaiT'" , 722pcall_err ( api . nvim_set_keymap , 'n' , 'lhs' , 'rhs' , { nowaiT =false } ) 723) 724 725-- <buffer> option key 726eq ( 727"Invalid key: 'buffer'" , 728pcall_err ( api . nvim_set_keymap , 'n' , 'lhs' , 'rhs' , { buffer =true } ) 729) 730 731eq ( 732'"replace_keycodes" requires "expr"' , 733pcall_err ( api . nvim_set_keymap , 'n' , 'lhs' , 'rhs' , { replace_keycodes =true } ) 734) 735end ) 736 737local optnames ={ 'nowait' , 'silent' , 'script' , 'expr' , 'unique' } 738for _ , opt in ipairs ( optnames ) do 739-- note: need '%' to escape hyphens, which have special meaning in lua 740it ( 'throws an error when given non-boolean value for ' .. opt , function () 741local opts ={} 742opts [ opt ] ='fooo' 743eq ( 744( "Invalid '%s': expected boolean" ): format ( opt ), 745pcall_err ( api . nvim_set_keymap , 'n' , 'lhs' , 'rhs' , opts ) 746) 747end ) 748end 749 750-- Perform tests of basic functionality 751it ( 'sets ordinary mappings' , function () 752api . nvim_set_keymap ( 'n' , 'lhs' , 'rhs' , {} ) 753eq ( generate_mapargs ( 'n' , 'lhs' , 'rhs' ), get_mapargs ( 'n' , 'lhs' )) 754 755api . nvim_set_keymap ( 'v' , 'lhs' , 'rhs' , {} ) 756eq ( generate_mapargs ( 'v' , 'lhs' , 'rhs' ), get_mapargs ( 'v' , 'lhs' )) 757end ) 758 759it ( 'does not throw when LHS or RHS have leading/trailing whitespace' , function () 760api . nvim_set_keymap ( 'n' , ' lhs' , 'rhs' , {} ) 761eq ( generate_mapargs ( 'n' , '<Space><Space><Space>lhs' , 'rhs' ), get_mapargs ( 'n' , ' lhs' )) 762 763api . nvim_set_keymap ( 'n' , 'lhs ' , 'rhs' , {} ) 764eq ( generate_mapargs ( 'n' , 'lhs<Space><Space><Space><Space>' , 'rhs' ), get_mapargs ( 'n' , 'lhs ' )) 765 766api . nvim_set_keymap ( 'v' , ' lhs ' , '\trhs\t\f' , {} ) 767eq ( generate_mapargs ( 'v' , '<Space>lhs<Space><Space>' , '\trhs\t\f' ), get_mapargs ( 'v' , ' lhs ' )) 768end ) 769 770it ( 'can set noremap mappings' , function () 771api . nvim_set_keymap ( 'x' , 'lhs' , 'rhs' , { noremap =true } ) 772eq ( generate_mapargs ( 'x' , 'lhs' , 'rhs' , { noremap =true } ), get_mapargs ( 'x' , 'lhs' )) 773 774api . nvim_set_keymap ( 't' , 'lhs' , 'rhs' , { noremap =true } ) 775eq ( generate_mapargs ( 't' , 'lhs' , 'rhs' , { noremap =true } ), get_mapargs ( 't' , 'lhs' )) 776end ) 777 778it ( 'can unmap mappings' , function () 779api . nvim_set_keymap ( 'v' , 'lhs' , 'rhs' , {} ) 780api . nvim_del_keymap ( 'v' , 'lhs' ) 781eq ( {} , get_mapargs ( 'v' , 'lhs' )) 782 783api . nvim_set_keymap ( 't' , 'lhs' , 'rhs' , { noremap =true } ) 784api . nvim_del_keymap ( 't' , 'lhs' ) 785eq ( {} , get_mapargs ( 't' , 'lhs' )) 786end ) 787 788-- Test some edge cases 789it ( '"!" and empty string are synonyms for mapmode-nvo' , function () 790local nvo_shortnames ={ '' , '!' } 791for _ , name in ipairs ( nvo_shortnames ) do 792api . nvim_set_keymap ( name , 'lhs' , 'rhs' , {} ) 793api . nvim_del_keymap ( name , 'lhs' ) 794eq ( {} , get_mapargs ( name , 'lhs' )) 795end 796end ) 797 798local special_chars ={ '<C-U>' , '<S-Left>' , '<F12><F2><Tab>' , '<Space><Tab>' } 799for _ , lhs in ipairs ( special_chars ) do 800for _ , rhs in ipairs ( special_chars ) do 801local mapmode ='!' 802it ( 'can set mappings with special characters, lhs: ' .. lhs .. ', rhs: ' .. rhs , function () 803api . nvim_set_keymap ( mapmode , lhs , rhs , {} ) 804eq ( generate_mapargs ( mapmode , lhs , rhs ), get_mapargs ( mapmode , lhs )) 805end ) 806end 807end 808 809it ( 'can set mappings containing C0 control codes' , function () 810api . nvim_set_keymap ( 'n' , '\n\r\n' , 'rhs' , {} ) 811local expected =generate_mapargs ( 'n' , '<NL><CR><NL>' , 'rhs' ) 812eq ( expected , get_mapargs ( 'n' , '<NL><CR><NL>' )) 813end ) 814 815it ( 'can set mappings whose RHS is a <Nop>' , function () 816api . nvim_set_keymap ( 'i' , 'lhs' , '<Nop>' , {} ) 817command ( 'normal ilhs' ) 818eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) -- imap to <Nop> does nothing 819eq ( generate_mapargs ( 'i' , 'lhs' , '<Nop>' , {} ), get_mapargs ( 'i' , 'lhs' )) 820 821-- also test for case insensitivity 822api . nvim_set_keymap ( 'i' , 'lhs' , '<nOp>' , {} ) 823command ( 'normal ilhs' ) 824eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 825-- note: RHS in returned mapargs() dict reflects the original RHS 826-- provided by the user 827eq ( generate_mapargs ( 'i' , 'lhs' , '<nOp>' , {} ), get_mapargs ( 'i' , 'lhs' )) 828 829api . nvim_set_keymap ( 'i' , 'lhs' , '<NOP>' , {} ) 830command ( 'normal ilhs' ) 831eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 832eq ( generate_mapargs ( 'i' , 'lhs' , '<NOP>' , {} ), get_mapargs ( 'i' , 'lhs' )) 833 834-- a single ^V in RHS is also <Nop> (see :h map-empty-rhs) 835api . nvim_set_keymap ( 'i' , 'lhs' , '\022' , {} ) 836command ( 'normal ilhs' ) 837eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 838eq ( generate_mapargs ( 'i' , 'lhs' , '\022' , {} ), get_mapargs ( 'i' , 'lhs' )) 839end ) 840 841it ( 'treats an empty RHS in a mapping like a <Nop>' , function () 842api . nvim_set_keymap ( 'i' , 'lhs' , '' , {} ) 843command ( 'normal ilhs' ) 844eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 845eq ( generate_mapargs ( 'i' , 'lhs' , '' , {} ), get_mapargs ( 'i' , 'lhs' )) 846end ) 847 848it ( 'can set and unset <M-">' , function () 849-- Taken from the legacy test: test_mapping.vim. Exposes a bug in which 850-- replace_termcodes changes the length of the mapping's LHS, but 851-- do_map continues to use the *old* length of LHS. 852api . nvim_set_keymap ( 'i' , '<M-">' , 'foo' , {} ) 853api . nvim_del_keymap ( 'i' , '<M-">' ) 854eq ( {} , get_mapargs ( 'i' , '<M-">' )) 855end ) 856 857it ( 858'interprets control sequences in expr-quotes correctly when called ' .. 'inside vim' , 859function () 860command ( [[call nvim_set_keymap('i', "\<space>", "\<tab>", {})]] ) 861eq ( generate_mapargs ( 'i' , '<Space>' , '\t' , { sid =0 } ), get_mapargs ( 'i' , '<Space>' )) 862feed ( 'i ' ) 863eq ( { '\t' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 864end 865) 866 867it ( 'throws appropriate error messages when setting <unique> maps' , function () 868api . nvim_set_keymap ( 'l' , 'lhs' , 'rhs' , {} ) 869eq ( 870'E227: Mapping already exists for lhs' , 871pcall_err ( api . nvim_set_keymap , 'l' , 'lhs' , 'rhs' , { unique =true } ) 872) 873-- different mapmode, no error should be thrown 874api . nvim_set_keymap ( 't' , 'lhs' , 'rhs' , { unique =true } ) 875 876api . nvim_set_keymap ( 'n' , '<tab>' , 'rhs' , {} ) 877eq ( 878'E227: Mapping already exists for <tab>' , 879pcall_err ( api . nvim_set_keymap , 'n' , '<tab>' , 'rhs' , { unique =true } ) 880) 881 882api . nvim_set_keymap ( 'ia' , 'lhs' , 'rhs' , {} ) 883eq ( 884'E226: Abbreviation already exists for lhs' , 885pcall_err ( api . nvim_set_keymap , 'ia' , 'lhs' , 'rhs' , { unique =true } ) 886) 887end ) 888 889it ( 'can set <expr> mappings whose RHS change dynamically' , function () 890exec ( [[ 891function! FlipFlop() abort 892if !exists('g:flip') | let g:flip = 0 | endif 893let g:flip = !g:flip 894return g:flip 895endfunction 896]] ) 897eq ( 1 , api . nvim_call_function ( 'FlipFlop' , {} )) 898eq ( 0 , api . nvim_call_function ( 'FlipFlop' , {} )) 899eq ( 1 , api . nvim_call_function ( 'FlipFlop' , {} )) 900eq ( 0 , api . nvim_call_function ( 'FlipFlop' , {} )) 901 902api . nvim_set_keymap ( 'i' , 'lhs' , 'FlipFlop()' , { expr =true } ) 903command ( 'normal ilhs' ) 904eq ( { '1' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 905 906command ( 'normal! ggVGd' ) 907 908command ( 'normal ilhs' ) 909eq ( { '0' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 910end ) 911 912it ( 'can set mappings that do trigger other mappings' , function () 913api . nvim_set_keymap ( 'i' , 'mhs' , 'rhs' , {} ) 914api . nvim_set_keymap ( 'i' , 'lhs' , 'mhs' , {} ) 915 916command ( 'normal imhs' ) 917eq ( { 'rhs' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 918 919command ( 'normal! ggVGd' ) 920 921command ( 'normal ilhs' ) 922eq ( { 'rhs' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 923end ) 924 925it ( "can set noremap mappings that don't trigger other mappings" , function () 926api . nvim_set_keymap ( 'i' , 'mhs' , 'rhs' , {} ) 927api . nvim_set_keymap ( 'i' , 'lhs' , 'mhs' , { noremap =true } ) 928 929command ( 'normal imhs' ) 930eq ( { 'rhs' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 931 932command ( 'normal! ggVGd' ) 933 934command ( 'normal ilhs' ) -- shouldn't trigger mhs-to-rhs mapping 935eq ( { 'mhs' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 936end ) 937 938it ( 'can set nowait mappings that fire without waiting' , function () 939api . nvim_set_keymap ( 'i' , '123456' , 'longer' , {} ) 940api . nvim_set_keymap ( 'i' , '123' , 'shorter' , { nowait =true } ) 941 942-- feed keys one at a time; if all keys arrive atomically, the longer 943-- mapping will trigger 944local keys ='i123456' 945for c in string . gmatch ( keys , '.' ) do 946feed ( c ) 947sleep ( 5 ) 948end 949eq ( { 'shorter456' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 0 )) 950end ) 951 952-- Perform exhaustive tests of basic functionality 953local mapmodes ={ 'n' , 'v' , 'x' , 's' , 'o' , '!' , 'i' , 'l' , 'c' , 't' , '' , 'ia' , 'ca' , '!a' } 954for _ , mapmode in ipairs ( mapmodes ) do 955it ( 'can set/unset normal mappings in mapmode ' .. mapmode , function () 956api . nvim_set_keymap ( mapmode , 'lhs' , 'rhs' , {} ) 957eq ( generate_mapargs ( mapmode , 'lhs' , 'rhs' ), get_mapargs ( mapmode , 'lhs' )) 958 959-- some mapmodes (like 'o') will prevent other mapmodes (like '!') from 960-- taking effect, so unmap after each mapping 961api . nvim_del_keymap ( mapmode , 'lhs' ) 962eq ( {} , get_mapargs ( mapmode , 'lhs' )) 963end ) 964end 965 966for _ , mapmode in ipairs ( mapmodes ) do 967it ( 'can set/unset noremap mappings using mapmode ' .. mapmode , function () 968api . nvim_set_keymap ( mapmode , 'lhs' , 'rhs' , { noremap =true } ) 969eq ( generate_mapargs ( mapmode , 'lhs' , 'rhs' , { noremap =true } ), get_mapargs ( mapmode , 'lhs' )) 970 971api . nvim_del_keymap ( mapmode , 'lhs' ) 972eq ( {} , get_mapargs ( mapmode , 'lhs' )) 973end ) 974end 975 976-- Test map-arguments, using optnames from above 977-- remove some map arguments that are harder to test, or were already tested 978optnames ={ 'nowait' , 'silent' , 'expr' , 'noremap' } 979for _ , mapmode in ipairs ( mapmodes ) do 980-- Test with single mappings 981for _ , maparg in ipairs ( optnames ) do 982it ( 'can set/unset ' .. mapmode .. '-mappings with maparg: ' .. maparg , function () 983api . nvim_set_keymap ( mapmode , 'lhs' , 'rhs' , { [ maparg ] =true } ) 984eq ( 985generate_mapargs ( mapmode , 'lhs' , 'rhs' , { [ maparg ] =true } ), 986get_mapargs ( mapmode , 'lhs' ) 987) 988api . nvim_del_keymap ( mapmode , 'lhs' ) 989eq ( {} , get_mapargs ( mapmode , 'lhs' )) 990end ) 991it ( 992'can set/unset ' 993.. mapmode 994.. '-mode mappings with maparg ' 995.. maparg 996.. ', whose value is false' , 997function () 998api . nvim_set_keymap ( mapmode , 'lhs' , 'rhs' , { [ maparg ] =false } ) 999eq ( generate_mapargs ( mapmode , 'lhs' , 'rhs' ), get_mapargs ( mapmode , 'lhs' )) 1000api . nvim_del_keymap ( mapmode , 'lhs' ) 1001eq ( {} , get_mapargs ( mapmode , 'lhs' )) 1002end 1003) 1004end 1005 1006-- Test with triplets of mappings, one of which is false 1007for i =1 , ( # optnames - 2 ) do 1008local opt1 , opt2 , opt3 =optnames [ i ], optnames [ i + 1 ], optnames [ i + 2 ] 1009it ( 1010'can set/unset ' 1011.. mapmode 1012.. '-mode mappings with mapargs ' 1013.. opt1 1014.. ', ' 1015.. opt2 1016.. ', ' 1017.. opt3 , 1018function () 1019local opts ={ [ opt1 ] =true , [ opt2 ] =false , [ opt3 ] =true } 1020api . nvim_set_keymap ( mapmode , 'lhs' , 'rhs' , opts ) 1021eq ( generate_mapargs ( mapmode , 'lhs' , 'rhs' , opts ), get_mapargs ( mapmode , 'lhs' )) 1022api . nvim_del_keymap ( mapmode , 'lhs' ) 1023eq ( {} , get_mapargs ( mapmode , 'lhs' )) 1024end 1025) 1026end 1027end 1028 1029it ( 'can make lua mappings' , function () 1030eq ( 10310 , 1032exec_lua [[ 1033GlobalCount = 0 1034vim.api.nvim_set_keymap('n', 'asdf', '', { 1035callback = function() GlobalCount = GlobalCount + 1 end, 1036}) 1037return GlobalCount 1038]] 1039) 1040 1041feed ( 'asdf\n' ) 1042 1043eq ( 1 , exec_lua [[return GlobalCount]] ) 1044end ) 1045 1046it ( ':map command shows lua mapping correctly' , function () 1047exec_lua [[ 1048vim.api.nvim_set_keymap('n', 'asdf', '', { 1049callback = function() print('jkl;') end, 1050}) 1051]] 1052matches ( 1053'^\nn asdf <Lua %d+>' , 1054exec_lua [[return vim.api.nvim_exec2(':nmap asdf', { output = true }).output]] 1055) 1056end ) 1057 1058it ( 'mapcheck() returns lua mapping correctly' , function () 1059exec_lua [[ 1060vim.api.nvim_set_keymap('n', 'asdf', '', { 1061callback = function() print('jkl;') end, 1062}) 1063]] 1064matches ( '^<Lua %d+>' , fn . mapcheck ( 'asdf' , 'n' )) 1065end ) 1066 1067it ( 'maparg() and maplist() return lua mapping correctly' , function () 1068eq ( 10690 , 1070exec_lua ( [[ 1071GlobalCount = 0 1072vim.api.nvim_set_keymap('n', 'asdf', '', { 1073callback = function() GlobalCount = GlobalCount + 1 end, 1074}) 1075return GlobalCount 1076]] ) 1077) 1078 1079matches ( '^<Lua %d+>' , fn . maparg ( 'asdf' , 'n' )) 1080 1081local mapargs =fn . maparg ( 'asdf' , 'n' , false , true ) 1082mapargs . callback =nil 1083mapargs . lhsraw =nil 1084mapargs . lhsrawalt =nil 1085eq ( generate_mapargs ( 'n' , 'asdf' , nil , { sid =sid_lua } ), mapargs ) 1086 1087eq ( 10881 , 1089exec_lua ( [[ 1090vim.fn.maparg('asdf', 'n', false, true).callback() 1091return GlobalCount 1092]] ) 1093) 1094 1095exec ( [[ 1096call maparg('asdf', 'n', v:false, v:true).callback() 1097]] ) 1098eq ( 2 , exec_lua ( [[return GlobalCount]] )) 1099 1100api . nvim_eval ( [[ 1101maplist()->filter({_, m -> m.lhs == 'asdf'})->foreach({_, m -> m.callback()}) 1102]] ) 1103eq ( 3 , exec_lua ( [[return GlobalCount]] )) 1104end ) 1105 1106it ( 'can make lua expr mappings replacing keycodes' , function () 1107exec_lua [[ 1108vim.api.nvim_set_keymap('n', 'aa', '', { 1109callback = function() return '<Insert>π<C-V><M-π>foo<lt><Esc>' end, 1110expr = true, 1111replace_keycodes = true, 1112}) 1113]] 1114 1115feed ( 'aa' ) 1116 1117eq ( { 'π<M-π>foo<' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 1118end ) 1119 1120it ( 'can make lua expr mappings without replacing keycodes' , function () 1121exec_lua [[ 1122vim.api.nvim_set_keymap('i', 'aa', '', { 1123callback = function() return '<space>' end, 1124expr = true, 1125}) 1126]] 1127 1128feed ( 'iaa<esc>' ) 1129 1130eq ( { '<space>' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 1131end ) 1132 1133it ( 'lua expr mapping returning nil is equivalent to returning an empty string' , function () 1134exec_lua [[ 1135vim.api.nvim_set_keymap('i', 'aa', '', { 1136callback = function() return nil end, 1137expr = true, 1138}) 1139]] 1140 1141feed ( 'iaa<esc>' ) 1142 1143eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 1144end ) 1145 1146it ( 'does not reset pum in lua mapping' , function () 1147eq ( 11480 , 1149exec_lua [[ 1150VisibleCount = 0 1151vim.api.nvim_set_keymap('i', '<F2>', '', { 1152callback = function() VisibleCount = VisibleCount + vim.fn.pumvisible() end, 1153}) 1154return VisibleCount 1155]] 1156) 1157feed ( 'i<C-X><C-V><F2><F2><esc>' ) 1158eq ( 2 , exec_lua [[return VisibleCount]] ) 1159end ) 1160 1161it ( 'redo of lua mappings in op-pending mode work' , function () 1162eq ( 11630 , 1164exec_lua [[ 1165OpCount = 0 1166vim.api.nvim_set_keymap('o', '<F2>', '', { 1167callback = function() OpCount = OpCount + 1 end, 1168}) 1169return OpCount 1170]] 1171) 1172feed ( 'd<F2>' ) 1173eq ( 1 , exec_lua [[return OpCount]] ) 1174feed ( '.' ) 1175eq ( 2 , exec_lua [[return OpCount]] ) 1176end ) 1177 1178it ( 'can overwrite lua mappings' , function () 1179eq ( 11800 , 1181exec_lua [[ 1182GlobalCount = 0 1183vim.api.nvim_set_keymap('n', 'asdf', '', { 1184callback = function() GlobalCount = GlobalCount + 1 end, 1185}) 1186return GlobalCount 1187]] 1188) 1189 1190feed ( 'asdf\n' ) 1191 1192eq ( 1 , exec_lua [[return GlobalCount]] ) 1193 1194exec_lua [[ 1195vim.api.nvim_set_keymap('n', 'asdf', '', { 1196callback = function() GlobalCount = GlobalCount - 1 end, 1197}) 1198]] 1199 1200feed ( 'asdf\n' ) 1201 1202eq ( 0 , exec_lua [[return GlobalCount]] ) 1203end ) 1204 1205it ( 'can unmap lua mappings' , function () 1206eq ( 12070 , 1208exec_lua [[ 1209GlobalCount = 0 1210vim.api.nvim_set_keymap('n', 'asdf', '', { 1211callback = function() GlobalCount = GlobalCount + 1 end, 1212}) 1213return GlobalCount 1214]] 1215) 1216 1217feed ( 'asdf\n' ) 1218 1219eq ( 1 , exec_lua [[return GlobalCount]] ) 1220 1221exec_lua [[vim.api.nvim_del_keymap('n', 'asdf' )]] 1222 1223feed ( 'asdf\n' ) 1224 1225eq ( 1 , exec_lua [[return GlobalCount]] ) 1226eq ( '\nNo mapping found' , n . exec_capture ( 'nmap asdf' )) 1227end ) 1228 1229it ( 'no double-free when unmapping simplifiable lua mappings' , function () 1230eq ( 12310 , 1232exec_lua [[ 1233GlobalCount = 0 1234vim.api.nvim_set_keymap('n', '<C-I>', '', { 1235callback = function() GlobalCount = GlobalCount + 1 end, 1236}) 1237return GlobalCount 1238]] 1239) 1240 1241feed ( '<C-I>\n' ) 1242 1243eq ( 1 , exec_lua [[return GlobalCount]] ) 1244 1245exec_lua [[vim.api.nvim_del_keymap('n', '<C-I>')]] 1246 1247feed ( '<C-I>\n' ) 1248 1249eq ( 1 , exec_lua [[return GlobalCount]] ) 1250eq ( '\nNo mapping found' , n . exec_capture ( 'nmap <C-I>' )) 1251end ) 1252 1253it ( 'can set descriptions on mappings' , function () 1254api . nvim_set_keymap ( 'n' , 'lhs' , 'rhs' , { desc ='map description' } ) 1255eq ( generate_mapargs ( 'n' , 'lhs' , 'rhs' , { desc ='map description' } ), get_mapargs ( 'n' , 'lhs' )) 1256eq ( '\nn lhs rhs\n map description' , n . exec_capture ( 'nmap lhs' )) 1257end ) 1258 1259it ( 'can define !-mode abbreviations with lua callbacks' , function () 1260exec_lua [[ 1261GlobalCount = 0 1262vim.api.nvim_set_keymap('!a', 'foo', '', { 1263expr = true, 1264callback = function() 1265GlobalCount = GlobalCount + 1 1266return tostring(GlobalCount) 1267end, 1268}) 1269]] 1270 1271feed 'iThe foo and the bar and the foo again<esc>' 1272eq ( 'The 1 and the bar and the 2 again' , api . nvim_get_current_line ()) 1273 1274feed ':let x = "The foo is the one"<cr>' 1275eq ( 'The 3 is the one' , api . nvim_eval 'x' ) 1276end ) 1277 1278it ( 'can define insert mode abbreviations with lua callbacks' , function () 1279exec_lua [[ 1280GlobalCount = 0 1281vim.api.nvim_set_keymap('ia', 'foo', '', { 1282expr = true, 1283callback = function() 1284GlobalCount = GlobalCount + 1 1285return tostring(GlobalCount) 1286end, 1287}) 1288]] 1289 1290feed 'iThe foo and the bar and the foo again<esc>' 1291eq ( 'The 1 and the bar and the 2 again' , api . nvim_get_current_line ()) 1292 1293feed ':let x = "The foo is the one"<cr>' 1294eq ( 'The foo is the one' , api . nvim_eval 'x' ) 1295end ) 1296 1297it ( 'can define cmdline mode abbreviations with lua callbacks' , function () 1298exec_lua [[ 1299GlobalCount = 0 1300vim.api.nvim_set_keymap('ca', 'foo', '', { 1301expr = true, 1302callback = function() 1303GlobalCount = GlobalCount + 1 1304return tostring(GlobalCount) 1305end, 1306}) 1307]] 1308 1309feed 'iThe foo and the bar and the foo again<esc>' 1310eq ( 'The foo and the bar and the foo again' , api . nvim_get_current_line ()) 1311 1312feed ':let x = "The foo is the one"<cr>' 1313eq ( 'The 1 is the one' , api . nvim_eval 'x' ) 1314end ) 1315end ) 1316 1317describe ( 'nvim_buf_set_keymap, nvim_buf_del_keymap' , function () 1318before_each ( clear ) 1319 1320-- nvim_set_keymap is implemented as a wrapped call to nvim_buf_set_keymap, 1321-- so its tests also effectively test nvim_buf_set_keymap 1322 1323-- here, we mainly test for buffer specificity and other special cases 1324 1325-- switch to the given buffer, abandoning any changes in the current buffer 1326local function switch_to_buf ( bufnr ) 1327command ( bufnr .. 'buffer!' ) 1328end 1329 1330-- `set hidden`, then create two buffers and return their bufnr's 1331-- If start_from_first is truthy, the first buffer will be open when 1332-- the function returns; if falsy, the second buffer will be open. 1333local function make_two_buffers ( start_from_first ) 1334command ( 'set hidden' ) 1335 1336local first_buf =api . nvim_call_function ( 'bufnr' , { '%' } ) 1337command ( 'new' ) 1338local second_buf =api . nvim_call_function ( 'bufnr' , { '%' } ) 1339neq ( second_buf , first_buf ) -- sanity check 1340 1341if start_from_first then 1342switch_to_buf ( first_buf ) 1343end 1344 1345return first_buf , second_buf 1346end 1347 1348it ( 'rejects negative bufnr values' , function () 1349eq ( 1350'Wrong type for argument 1 when calling nvim_buf_set_keymap, expecting Buffer' , 1351pcall_err ( api . nvim_buf_set_keymap , - 1 , '' , 'lhs' , 'rhs' , {} ) 1352) 1353end ) 1354 1355it ( 'can set mappings active in the current buffer but not others' , function () 1356local first , second =make_two_buffers ( true ) 1357 1358api . nvim_buf_set_keymap ( 0 , '' , 'lhs' , 'irhs<Esc>' , {} ) 1359command ( 'normal lhs' ) 1360eq ( { 'rhs' } , api . nvim_buf_get_lines ( 0 , 0 , 1 , 1 )) 1361 1362-- mapping should have no effect in new buffer 1363switch_to_buf ( second ) 1364command ( 'normal lhs' ) 1365eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , 1 , 1 )) 1366 1367-- mapping should remain active in old buffer 1368switch_to_buf ( first ) 1369command ( 'normal ^lhs' ) 1370eq ( { 'rhsrhs' } , api . nvim_buf_get_lines ( 0 , 0 , 1 , 1 )) 1371end ) 1372 1373it ( 'can set local mappings in buffer other than current' , function () 1374local first =make_two_buffers ( false ) 1375api . nvim_buf_set_keymap ( first , '' , 'lhs' , 'irhs<Esc>' , {} ) 1376 1377-- shouldn't do anything 1378command ( 'normal lhs' ) 1379eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , 1 , 1 )) 1380 1381-- should take effect 1382switch_to_buf ( first ) 1383command ( 'normal lhs' ) 1384eq ( { 'rhs' } , api . nvim_buf_get_lines ( 0 , 0 , 1 , 1 )) 1385end ) 1386 1387it ( 'can disable mappings made in another buffer, inside that buffer' , function () 1388local first =make_two_buffers ( false ) 1389api . nvim_buf_set_keymap ( first , '' , 'lhs' , 'irhs<Esc>' , {} ) 1390api . nvim_buf_del_keymap ( first , '' , 'lhs' ) 1391switch_to_buf ( first ) 1392 1393-- shouldn't do anything 1394command ( 'normal lhs' ) 1395eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , 1 , 1 )) 1396end ) 1397 1398it ( "can't disable mappings given wrong buffer handle" , function () 1399local first , second =make_two_buffers ( false ) 1400api . nvim_buf_set_keymap ( first , '' , 'lhs' , 'irhs<Esc>' , {} ) 1401eq ( 'E31: No such mapping' , pcall_err ( api . nvim_buf_del_keymap , second , '' , 'lhs' )) 1402 1403-- should still work 1404switch_to_buf ( first ) 1405command ( 'normal lhs' ) 1406eq ( { 'rhs' } , api . nvim_buf_get_lines ( 0 , 0 , 1 , 1 )) 1407end ) 1408 1409it ( 'does not crash when setting mapping in a non-existing buffer #13541' , function () 1410pcall_err ( api . nvim_buf_set_keymap , 100 , '' , 'lsh' , 'irhs<Esc>' , {} ) 1411n . assert_alive () 1412end ) 1413 1414it ( 'can make lua mappings' , function () 1415eq ( 14160 , 1417exec_lua [[ 1418GlobalCount = 0 1419vim.api.nvim_buf_set_keymap(0, 'n', 'asdf', '', { 1420callback = function() GlobalCount = GlobalCount + 1 end, 1421}) 1422return GlobalCount 1423]] 1424) 1425 1426feed ( 'asdf\n' ) 1427 1428eq ( 1 , exec_lua [[return GlobalCount]] ) 1429end ) 1430 1431it ( 'can make lua expr mappings replacing keycodes' , function () 1432exec_lua [[ 1433vim.api.nvim_buf_set_keymap(0, 'n', 'aa', '', { 1434callback = function() return '<Insert>π<C-V><M-π>foo<lt><Esc>' end, 1435expr = true, 1436replace_keycodes = true, 1437}) 1438]] 1439 1440feed ( 'aa' ) 1441 1442eq ( { 'π<M-π>foo<' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 1443end ) 1444 1445it ( 'can make lua expr mappings without replacing keycodes' , function () 1446exec_lua [[ 1447vim.api.nvim_buf_set_keymap(0, 'i', 'aa', '', { 1448callback = function() return '<space>' end, 1449expr = true, 1450}) 1451]] 1452 1453feed ( 'iaa<esc>' ) 1454 1455eq ( { '<space>' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 1456end ) 1457 1458it ( 'can overwrite lua mappings' , function () 1459eq ( 14600 , 1461exec_lua [[ 1462GlobalCount = 0 1463vim.api.nvim_buf_set_keymap(0, 'n', 'asdf', '', { 1464callback = function() GlobalCount = GlobalCount + 1 end, 1465}) 1466return GlobalCount 1467]] 1468) 1469 1470feed ( 'asdf\n' ) 1471 1472eq ( 1 , exec_lua [[return GlobalCount]] ) 1473 1474exec_lua [[ 1475vim.api.nvim_buf_set_keymap(0, 'n', 'asdf', '', { 1476callback = function() GlobalCount = GlobalCount - 1 end, 1477}) 1478]] 1479 1480feed ( 'asdf\n' ) 1481 1482eq ( 0 , exec_lua [[return GlobalCount]] ) 1483end ) 1484 1485it ( 'can unmap lua mappings' , function () 1486eq ( 14870 , 1488exec_lua [[ 1489GlobalCount = 0 1490vim.api.nvim_buf_set_keymap(0, 'n', 'asdf', '', { 1491callback = function() GlobalCount = GlobalCount + 1 end, 1492}) 1493return GlobalCount 1494]] 1495) 1496 1497feed ( 'asdf\n' ) 1498 1499eq ( 1 , exec_lua [[return GlobalCount]] ) 1500 1501exec_lua [[vim.api.nvim_buf_del_keymap(0, 'n', 'asdf' )]] 1502 1503feed ( 'asdf\n' ) 1504 1505eq ( 1 , exec_lua [[return GlobalCount]] ) 1506eq ( '\nNo mapping found' , n . exec_capture ( 'nmap asdf' )) 1507end ) 1508 1509it ( 'no double-free when unmapping simplifiable lua mappings' , function () 1510eq ( 15110 , 1512exec_lua [[ 1513GlobalCount = 0 1514vim.api.nvim_buf_set_keymap(0, 'n', '<C-I>', '', { 1515callback = function() GlobalCount = GlobalCount + 1 end, 1516}) 1517return GlobalCount 1518]] 1519) 1520 1521feed ( '<C-I>\n' ) 1522 1523eq ( 1 , exec_lua [[return GlobalCount]] ) 1524 1525exec_lua [[vim.api.nvim_buf_del_keymap(0, 'n', '<C-I>')]] 1526 1527feed ( '<C-I>\n' ) 1528 1529eq ( 1 , exec_lua [[return GlobalCount]] ) 1530eq ( '\nNo mapping found' , n . exec_capture ( 'nmap <C-I>' )) 1531end ) 1532 1533it ( 'does not overwrite in <unique> mappings' , function () 1534api . nvim_buf_set_keymap ( 0 , 'i' , 'lhs' , 'rhs' , {} ) 1535eq ( 1536'E227: Mapping already exists for lhs' , 1537pcall_err ( api . nvim_buf_set_keymap , 0 , 'i' , 'lhs' , 'rhs' , { unique =true } ) 1538) 1539 1540api . nvim_buf_set_keymap ( 0 , 'ia' , 'lhs2' , 'rhs2' , {} ) 1541eq ( 1542'E226: Abbreviation already exists for lhs2' , 1543pcall_err ( api . nvim_buf_set_keymap , 0 , 'ia' , 'lhs2' , 'rhs2' , { unique =true } ) 1544) 1545 1546api . nvim_set_keymap ( 'n' , 'lhs' , 'rhs' , {} ) 1547eq ( 1548'E225: Global mapping already exists for lhs' , 1549pcall_err ( api . nvim_buf_set_keymap , 0 , 'n' , 'lhs' , 'rhs' , { unique =true } ) 1550) 1551end ) 1552end )