robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
64a301184e
master
1" Tests for mappings and abbreviations 2 3source shared.vim 4source check.vim 5source screendump.vim 6source term_util.vim 7 8func Test_abbreviation () 9new 10 11" abbreviation with 0x80 (full-id) 12inoreab чкпр vim 13call feedkeys ( "Goчкпр \<Esc>" , "xt" ) 14call assert_equal ( 'vim ' , getline ( '$' )) 15iunab чкпр 16 17" abbreviation with 0x80 (non-id) 18inoreab abc⁀ abc^ 19inoreab ⁀ ^ 20call feedkeys ( "Goabc⁀ def⁀ ⁀ \<Esc>" , "xt" ) 21call assert_equal ( 'abc^ def⁀ ^ ' , getline ( '$' )) 22iunab abc⁀ 23iunab ⁀ 24 25" abbreviation with 0x9b (non-id) 26inoreab abc; abc; 27inoreab ; ; 28call feedkeys ( "Goabc; def; ; \<Esc>" , "xt" ) 29call assert_equal ( 'abc; def; ; ' , getline ( '$' )) 30iunab abc; 31iunab ; 32 33" abbreviation with composing chars (end-id) 34inoreab ..ã a^~ 35inoreab ..β̃ β^~ 36inoreab ..π̃ π^~ 37inoreab ..Λ̃ Λ^~ 38call feedkeys ( "Go..ã ..β̃ ..π̃ ..Λ̃ \<Esc>" , "xt" ) 39call assert_equal ( 'a^~ β^~ π^~ Λ^~ ' , getline ( '$' )) 40iunab ..ã 41iunab ..β̃ 42iunab ..π̃ 43iunab ..Λ̃ 44 45bwipe ! 46endfunc 47 48func Test_abbreviation_with_noremap () 49nnoremap < F2 > :echo "cheese" 50cabbr cheese xxx 51call feedkeys ( ":echo \"cheese\"\<C-B>\"\<CR>" , 'tx' ) 52call assert_equal ( '"echo "xxx"' , @:) 53call feedkeys ( "\<F2>\<C-B>\"\<CR>" , 'tx' ) 54call assert_equal ( '"echo "cheese"' , @:) 55nnoremap < F2 > :echo "cheese< C-] > " 56call feedkeys ( "\<F2>\<C-B>\"\<CR>" , 'tx' ) 57call assert_equal ( '"echo "xxx"' , @:) 58nunmap <F2> 59cunabbr cheese 60 61new 62inoremap <buffer> (< C-] > () 63iabbr <buffer> fnu fun 64call feedkeys ( "ifnu(" , 'tx' ) 65call assert_equal ( 'fun()' , getline ( 1 )) 66bwipe ! 67endfunc 68 69func Test_abclear () 70abbrev foo foobar 71iabbrev fooi foobari 72cabbrev fooc foobarc 73call assert_equal ( "\n\n" 74 \.. "c fooc foobarc\n" 75 \.. "i fooi foobari\n" 76 \.. "! foo foobar" , execute ( 'abbrev' )) 77 78iabclear 79call assert_equal ( "\n\n" 80 \.. "c fooc foobarc\n" 81 \.. "c foo foobar" , execute ( 'abbrev' )) 82abbrev foo foobar 83iabbrev fooi foobari 84 85cabclear 86call assert_equal ( "\n\n" 87 \.. "i fooi foobari\n" 88 \.. "i foo foobar" , execute ( 'abbrev' )) 89abbrev foo foobar 90cabbrev fooc foobarc 91 92abclear 93call assert_equal ( "\n\nNo abbreviation found" , execute ( 'abbrev' )) 94call assert_fails ( '%abclear' , 'E481:' ) 95endfunc 96 97func Test_abclear_buffer () 98abbrev foo foobar 99new X1 100abbrev <buffer> foo1 foobar1 101new X2 102abbrev <buffer> foo2 foobar2 103 104call assert_equal ( "\n\n" 105 \.. "! foo2 @foobar2\n" 106 \.. "! foo foobar" , execute ( 'abbrev' )) 107 108abclear <buffer> 109call assert_equal ( "\n\n" 110 \.. "! foo foobar" , execute ( 'abbrev' )) 111 112b X1 113call assert_equal ( "\n\n" 114 \.. "! foo1 @foobar1\n" 115 \.. "! foo foobar" , execute ( 'abbrev' )) 116abclear <buffer> 117call assert_equal ( "\n\n" 118 \.. "! foo foobar" , execute ( 'abbrev' )) 119 120abclear 121call assert_equal ( "\n\nNo abbreviation found" , execute ( 'abbrev' )) 122 123 %bwipe 124endfunc 125 126func Test_map_ctrl_c_insert () 127" mapping of ctrl-c in Insert mode 128set cpo -= <cpo -= k 129inoremap < c-c > < ctrl-c> 130cnoremap < c-c > dummy 131cunmap <c-c> 132call feedkeys ( "GoTEST2: CTRL-C |\<*C-C>A|\<Esc>" , "xt" ) 133call assert_equal ( 'TEST2: CTRL-C |<ctrl-c>A|' , getline ( '$' )) 134unmap ! <c-c> 135set nomodified 136endfunc 137 138func Test_map_ctrl_c_visual () 139" mapping of ctrl-c in Visual mode 140vnoremap < c-c > :< C-u > $put ='vmap works' 141call feedkeys ( "GV\<*C-C>\<CR>" , "xt" ) 142call assert_equal ( 'vmap works' , getline ( '$' )) 143vunmap <c-c> 144set nomodified 145endfunc 146 147func Test_map_langmap () 148if ! has ( 'langmap' ) 149return 150endif 151 152" check langmap applies in normal mode 153set langmap = +-nolangremap 154new 155call setline ( 1 , [ 'a' , 'b' , 'c' ]) 1562 157call assert_equal ( 'b' , getline ( '.' )) 158call feedkeys ( "+" , "xt" ) 159call assert_equal ( 'a' , getline ( '.' )) 160 161" check no remapping 162map x + 1632 164call feedkeys ( "x" , "xt" ) 165call assert_equal ( 'c' , getline ( '.' )) 166 167" check with remapping 168set langremap 1692 170call feedkeys ( "x" , "xt" ) 171call assert_equal ( 'a' , getline ( '.' )) 172 173unmap x 174bwipe ! 175 176" 'langnoremap' follows 'langremap' and vice versa 177set langremap 178set langnoremap 179call assert_equal ( 0 , & langremap ) 180set langremap 181call assert_equal ( 0 , & langnoremap ) 182set nolangremap 183call assert_equal ( 1 , & langnoremap ) 184 185" check default values 186set langnoremap & 187call assert_equal ( 1 , & langnoremap ) 188call assert_equal ( 0 , & langremap ) 189set langremap & 190call assert_equal ( 1 , & langnoremap ) 191call assert_equal ( 0 , & langremap ) 192 193" langmap should not apply in insert mode, 'langremap' doesn't matter 194set langmap = +{nolangremap 195call feedkeys ( "Go+\<Esc>" , "xt" ) 196call assert_equal ( '+' , getline ( '$' )) 197set langmap = +{langremap 198call feedkeys ( "Go+\<Esc>" , "xt" ) 199call assert_equal ( '+' , getline ( '$' )) 200 201" langmap used for register name in insert mode. 202call setreg ( 'a' , 'aaaa' ) 203call setreg ( 'b' , 'bbbb' ) 204call setreg ( 'c' , 'cccc' ) 205set langmap = ablangremap 206call feedkeys ( "Go\<C-R>a\<Esc>" , "xt" ) 207call assert_equal ( 'bbbb' , getline ( '$' )) 208call feedkeys ( "Go\<C-R>\<C-R>a\<Esc>" , "xt" ) 209call assert_equal ( 'bbbb' , getline ( '$' )) 210" mapping does not apply 211imap c a 212call feedkeys ( "Go\<C-R>c\<Esc>" , "xt" ) 213call assert_equal ( 'cccc' , getline ( '$' )) 214imap a c 215call feedkeys ( "Go\<C-R>a\<Esc>" , "xt" ) 216call assert_equal ( 'bbbb' , getline ( '$' )) 217 218" langmap should not apply in Command-line mode 219set langmap = +{nolangremap 220call feedkeys ( ":call append(line('$'), '+')\<CR>" , "xt" ) 221call assert_equal ( '+' , getline ( '$' )) 222 223iunmap a 224iunmap c 225set nomodified 226endfunc 227 228func Test_map_feedkeys () 229" issue #212 (feedkeys insert mapping at current position) 230nnoremap . :call feedkeys(".", "in")< cr > 231call setline ( '$' , [ 'a b c d' , 'a b c d' ]) 232$ -1 233call feedkeys ( "0qqdw.ifoo\<Esc>qj0@q\<Esc>" , "xt" ) 234call assert_equal ([ 'fooc d' , 'fooc d' ], getline ( line ( '$' ) - 1 , line ( '$' ))) 235nunmap . 236set nomodified 237endfunc 238 239func Test_map_cursor () 240" <c-g>U<cursor> works only within a single line 241imapclear 242imap ( ()< c-g > U< left > 243call feedkeys ( "G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k." , "xt" ) 244call assert_equal ( 'Test1: text with a (here some more text)' , getline ( line ( '$' ) - 2 )) 245call assert_equal ( 'Test1: text with a (here some more text)' , getline ( line ( '$' ) - 1 )) 246 247" test undo 248call feedkeys ( "G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u" , "xt" ) 249call assert_equal ( '' , getline ( line ( '$' ) - 2 )) 250call assert_equal ( 'Test2: text wit a (here some more text [und undo])' , getline ( line ( '$' ) - 1 )) 251set nomodified 252imapclear 253endfunc 254 255func Test_map_cursor_ctrl_gU () 256" <c-g>U<cursor> works only within a single line 257nnoremap c< * *Ncgn< C-r > "< C-G > U< S-Left > 258call setline ( 1 , [ 'foo' , 'foobar' , '' , 'foo' ]) 259call cursor ( 1 , 2 ) 260call feedkeys ( "c<*PREFIX\<esc>." , 'xt' ) 261call assert_equal ([ 'PREFIXfoo' , 'foobar' , '' , 'PREFIXfoo' ], getline ( 1 , '$' )) 262" break undo manually 263set ul = 1000 264exe ":norm! uu" 265call assert_equal ([ 'foo' , 'foobar' , '' , 'foo' ], getline ( 1 , '$' )) 266 267" Test that it does not work if the cursor moves to the previous line 268" 2 times <S-Left> move to the previous line 269nnoremap c< * *Ncgn< C-r > "< C-G > U< S-Left > <C-G > U< S-Left > 270call setline ( 1 , [ '' , ' foo' , 'foobar' , '' , 'foo' ]) 271call cursor ( 2 , 3 ) 272call feedkeys ( "c<*PREFIX\<esc>." , 'xt' ) 273call assert_equal ([ 'PREFIXPREFIX' , ' foo' , 'foobar' , '' , 'foo' ], getline ( 1 , '$' )) 274nmapclear 275endfunc 276 277 278" This isn't actually testing a mapping, but similar use of CTRL-G U as above. 279func Test_break_undo () 280set whichwrap = <,>,[,] 281call feedkeys ( "G4o2k" , "xt" ) 282" Nvim: "." re-executes the whole insert session, including the (wrapping) 283" CTRL-G U <Right>, instead of only the text typed after it. 284exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>." 285call assert_equal ( 'Test3: text with a (parenthesis here' , getline ( line ( '$' ) - 3 )) 286call assert_equal ( 'new line hereTest3: text with a (parenthesis here' , getline ( line ( '$' ) - 2 )) 287call assert_equal ( 'new line here' , getline ( line ( '$' ) - 1 )) 288set nomodified 289endfunc 290 291func Test_map_meta_quotes () 292imap < M-" > foo 293call feedkeys ( "Go-\<*M-\">-\<Esc>" , "xt" ) 294call assert_equal ( "-foo-" , getline ( '$' )) 295set nomodified 296iunmap <M-"> 297endfunc 298 299func Test_map_meta_multibyte () 300imap < M-á > foo 301call assert_match ( 'i <M-á>\s*foo' , execute ( 'imap' )) 302iunmap <M-á> 303endfunc 304 305func Test_map_super_quotes () 306if "\<D-j>" [ -1 :] == '>' 307throw 'Skipped: <D- modifier not supported' 308endif 309 310imap < D-" > foo 311call feedkeys ( "Go-\<*D-\">-\<Esc>" , "xt" ) 312call assert_equal ( "-foo-" , getline ( '$' )) 313set nomodified 314iunmap <D-"> 315endfunc 316 317func Test_map_super_multibyte () 318if "\<D-j>" [ -1 :] == '>' 319throw 'Skipped: <D- modifier not supported' 320endif 321 322imap < D-á > foo 323call assert_match ( 'i <D-á>\s*foo' , execute ( 'imap' )) 324iunmap <D-á> 325endfunc 326 327func Test_abbr_after_line_join () 328new 329abbr foo bar 330set backspace = indent,eol,start 331exe "normal o\<BS>foo " 332call assert_equal ( "bar " , getline ( 1 )) 333bwipe ! 334unabbr foo 335set backspace & 336endfunc 337 338func Test_map_timeout () 339if ! has ( 'timers' ) 340return 341endif 342nnoremap aaaa :let got_aaaa = 1< CR > 343nnoremap bb :let got_bb = 1< CR > 344nmap b aaa 345new 346func ExitInsert ( timer ) 347let g: line = getline ( 1 ) 348call feedkeys ( "\<Esc>" , "t" ) 349endfunc 350set timeout timeoutlen = 200 351let timer = timer_start ( 300 , 'ExitInsert' ) 352" After the 'b' Vim waits for another character to see if it matches 'bb'. 353" When it times out it is expanded to "aaa", but there is no wait for 354" "aaaa". Can't check that reliably though. 355call feedkeys ( "b" , "xt!" ) 356call assert_equal ( "aa" , g: line ) 357call assert_false ( exists ( 'got_aaa' )) 358call assert_false ( exists ( 'got_bb' )) 359 360bwipe ! 361nunmap aaaa 362nunmap bb 363nunmap b 364set timeoutlen & 365delfunc ExitInsert 366call timer_stop ( timer ) 367endfunc 368 369func Test_map_timeout_with_timer_interrupt () 370CheckFeature job 371CheckFeature timers 372let g: test_is_flaky = 1 373 374" Confirm the timer invoked in exit_cb of the job doesn't disturb mapped key 375" sequence. 376new 377let g: val = 0 378nnoremap \12 :let g:val = 1< CR > 379nnoremap \123 :let g:val = 2< CR > 380set timeout timeoutlen = 200 381 382func ExitCb ( job , status ) 383let g: timer = timer_start ( 1 , { ->feedkeys ( "3\<Esc>" , 't' )}) 384endfunc 385 386call job_start ([ & shell , & shellcmdflag , 'echo' ], { 'exit_cb' : 'ExitCb' }) 387call feedkeys ( '\12' , 'xt!' ) 388call assert_equal ( 2 , g: val ) 389 390bwipe ! 391nunmap \12 392nunmap \123 393set timeoutlen & 394call WaitFor ({ ->exists ( 'g:timer' )}) 395call timer_stop ( g: timer ) 396unlet g: timer 397unlet g: val 398delfunc ExitCb 399endfunc 400 401func Test_cabbr_visual_mode () 402cabbr s su 403call feedkeys ( ":s \<c-B>\"\<CR>" , 'itx' ) 404call assert_equal ( '"su ' , getreg ( ':' )) 405call feedkeys ( ":'<,'>s \<c-B>\"\<CR>" , 'itx' ) 406let expected = '"' . "'<,'>su " 407call assert_equal ( expected , getreg ( ':' )) 408call feedkeys ( ": '<,'>s \<c-B>\"\<CR>" , 'itx' ) 409let expected = '" ' . "'<,'>su " 410call assert_equal ( expected , getreg ( ':' )) 411call feedkeys ( ":'a,'bs \<c-B>\"\<CR>" , 'itx' ) 412let expected = '"' . "'a,'bsu " 413call assert_equal ( expected , getreg ( ':' )) 414cunabbr s 415endfunc 416 417func Test_abbreviation_CR () 418new 419func Eatchar ( pat ) 420let c = nr2char ( getchar ( 0 )) 421return ( c =~ a: pat ) ? '' : c 422endfunc 423iabbrev <buffer><silent> ~~7 <c-r>=repeat('~', 7)<CR><c-r>=Eatchar('\s')<cr> 424call feedkeys ( "GA~~7 \<esc>" , 'xt' ) 425call assert_equal ( '~~~~~~~' , getline ( '$' )) 426 %d 427call feedkeys ( "GA~~7\<cr>\<esc>" , 'xt' ) 428call assert_equal ([ '~~~~~~~' , '' ], getline ( 1 , '$' )) 429delfunc Eatchar 430bw ! 431endfunc 432 433func Test_motionforce_omap () 434func GetCommand () 435let g: m = mode ( 1 ) 436let [ g: lnum1 , g: col1 ] = searchpos ( '-' , 'Wb' ) 437if g: lnum1 == 0 438return "\<Esc>" 439endif 440let [ g: lnum2 , g: col2 ] = searchpos ( '-' , 'W' ) 441if g: lnum2 == 0 442return "\<Esc>" 443endif 444return ":call Select()\<CR>" 445endfunc 446func Select () 447call cursor ([ g: lnum1 , g: col1 ]) 448exe "normal! 1 " . ( strlen ( g: m ) == 2 ? 'v' : g: m [ 2 ]) 449call cursor ([ g: lnum2 , g: col2 ]) 450execute "normal! \<BS>" 451endfunc 452new 453onoremap <buffer><expr> i- GetCommand () 454" 1) default omap mapping 455 %d _ 456call setline ( 1 , [ 'aaa - bbb' , 'x' , 'ddd - eee' ]) 457call cursor ( 2 , 1 ) 458norm di- 459call assert_equal ( 'no' , g: m ) 460call assert_equal ([ 'aaa -- eee' ], getline ( 1 , '$' )) 461" 2) forced characterwise operation 462 %d _ 463call setline ( 1 , [ 'aaa - bbb' , 'x' , 'ddd - eee' ]) 464call cursor ( 2 , 1 ) 465norm dvi- 466call assert_equal ( 'nov' , g: m ) 467call assert_equal ([ 'aaa -- eee' ], getline ( 1 , '$' )) 468" 3) forced linewise operation 469 %d _ 470call setline ( 1 , [ 'aaa - bbb' , 'x' , 'ddd - eee' ]) 471call cursor ( 2 , 1 ) 472norm dVi- 473call assert_equal ( 'noV' , g: m ) 474call assert_equal ([ '' ], getline ( 1 , '$' )) 475" 4) forced blockwise operation 476 %d _ 477call setline ( 1 , [ 'aaa - bbb' , 'x' , 'ddd - eee' ]) 478call cursor ( 2 , 1 ) 479exe "norm d\<C-V>i-" 480call assert_equal ( "no\<C-V>" , g: m ) 481call assert_equal ([ 'aaabbb' , 'x' , 'dddeee' ], getline ( 1 , '$' )) 482bwipe ! 483delfunc Select 484delfunc GetCommand 485endfunc 486 487func Test_error_in_map_expr () 488" Unlike CheckRunVimInTerminal this does work in a win32 console 489CheckFeature terminal 490if has ( 'win32' ) && has ( 'gui_running' ) 491throw 'Skipped: cannot run Vim in a terminal window' 492endif 493 494let lines =<< trim [CODE] 495func Func() 496" fail to create list 497let x = [ 498endfunc 499nmap <expr> ! Func() 500set updatetime=50 501[CODE] 502call writefile ( lines , 'Xtest.vim' ) 503 504let buf = term_start ( GetVimCommandCleanTerm () .. ' -S Xtest.vim' , { 'term_rows' : 8 }) 505let job = term_getjob ( buf ) 506call WaitForAssert ({ ->assert_notequal ( '' , term_getline ( buf , 8 ))}) 507 508" GC must not run during map-expr processing, which can make Vim crash. 509call term_sendkeys ( buf , '!' ) 510call TermWait ( buf , 50 ) 511call term_sendkeys ( buf , "\<CR>" ) 512call TermWait ( buf , 50 ) 513call assert_equal ( 'run' , job_status ( job )) 514 515call term_sendkeys ( buf , ":qall!\<CR>" ) 516call WaitFor ({ ->job_status ( job ) ==# 'dead' }) 517if has ( 'unix' ) 518call assert_equal ( '' , job_info ( job ). termsig ) 519endif 520 521call delete ( 'Xtest.vim' ) 522exe buf .. 'bwipe!' 523endfunc 524 525func Test_list_mappings () 526" Remove default mappings 527imapclear 528 529" reset 'isident' to check it isn't used 530set isident = 531inoremap < C-m > CtrlM 532inoremap < A-S > AltS 533inoremap < S-/ > ShiftSlash 534set isident & 535call assert_equal ([ 536 \'i <S-/> * ShiftSlash' , 537 \'i <M-S> * AltS' , 538 \'i <C-M> * CtrlM' , 539 \], execute ( 'imap' ) ->trim () ->split ( "\n" )) 540iunmap <C-M> 541iunmap <A-S> 542call assert_equal ([ 'i <S-/> * ShiftSlash' ], execute ( 'imap' ) ->trim () ->split ( "\n" )) 543iunmap <S-/> 544call assert_equal ([ 'No mapping found' ], execute ( 'imap' ) ->trim () ->split ( "\n" )) 545 546" List global, buffer local and script local mappings 547nmap ,f /^\k\+ (< CR > 548nmap <buffer> ,f /^\k\+ (< CR > 549nmap <script> ,fs /^\k\+ (< CR > 550call assert_equal ([ 'n ,f @/^\k\+ (<CR>' , 551 \'n ,fs & /^\k\+ (<CR>' , 552 \'n ,f /^\k\+ (<CR>' ], 553 \execute ( 'nmap ,f' ) ->trim () ->split ( "\n" )) 554 555" List <Nop> mapping 556nmap ,n< Nop > 557call assert_equal ([ 'n ,n <Nop>' ], 558 \execute ( 'nmap ,n' ) ->trim () ->split ( "\n" )) 559 560" verbose map 561call assert_match ( "\tLast set from .*/test_mapping.vim line \\d\\+$" , 562 \execute ( 'verbose map ,n' ) ->trim () ->split ( "\n" )[ 1 ]) 563 564" character with K_SPECIAL byte in rhs 565nmap foo … 566call assert_equal ([ 'n foo …' ], 567 \execute ( 'nmap foo' ) ->trim () ->split ( "\n" )) 568 569" modified character with K_SPECIAL byte in rhs 570nmap foo< M-… > 571call assert_equal ([ 'n foo <M-…>' ], 572 \execute ( 'nmap foo' ) ->trim () ->split ( "\n" )) 573 574" character with K_SPECIAL byte in lhs 575nmap … foo 576call assert_equal ([ 'n … foo' ], 577 \execute ( 'nmap …' ) ->trim () ->split ( "\n" )) 578 579" modified character with K_SPECIAL byte in lhs 580nmap < M-… > foo 581call assert_equal ([ 'n <M-…> foo' ], 582 \execute ( 'nmap <M-…>' ) ->trim () ->split ( "\n" )) 583 584" illegal bytes 585let str = ":\x7f:\x80:\x90:\xd0:" 586exe 'nmap foo ' .. str 587call assert_equal ([ 'n foo ' .. strtrans ( str )], 588 \execute ( 'nmap foo' ) ->trim () ->split ( "\n" )) 589unlet str 590 591" map to CTRL-V 592exe "nmap ,k \<C-V>" 593call assert_equal ([ 'n ,k <Nop>' ], 594 \execute ( 'nmap ,k' ) ->trim () ->split ( "\n" )) 595 596" map with space at the beginning 597exe "nmap \<C-V> w <Nop>" 598call assert_equal ([ 'n <Space>w <Nop>' ], 599 \execute ( "nmap \<C-V> w" ) ->trim () ->split ( "\n" )) 600 601nmapclear 602endfunc 603 604func Test_expr_map_gets_cursor () 605new 606call setline ( 1 , [ 'one' , 'some w!rd' ]) 607func StoreColumn () 608let g: exprLine = line ( '.' ) 609let g: exprCol = col ( '.' ) 610return 'x' 611endfunc 612nnoremap <expr> xStoreColumn () 6132 614nmap ! f!< Ignore > x 615call feedkeys ( "!" , 'xt' ) 616call assert_equal ( 'some wrd' , getline ( 2 )) 617call assert_equal ( 2 , g: exprLine ) 618call assert_equal ( 7 , g: exprCol ) 619 620bwipe ! 621unlet g: exprLine 622unlet g: exprCol 623delfunc StoreColumn 624nunmap x 625nunmap ! 626endfunc 627 628func Test_expr_map_restore_cursor () 629CheckScreendump 630 631let lines =<< trim END 632call setline(1, ['one', 'two', 'three']) 6332 634set ls=2 635hi! link StatusLine ErrorMsg 636noremap <expr> <C-B> Func() 637func Func() 638let g:on = !get(g:, 'on', 0) 639redraws 640return '' 641endfunc 642func Status() 643return get(g:, 'on', 0) ? '[on]' : '' 644endfunc 645set stl=%{Status()} 646END 647call writefile ( lines , 'XtestExprMap' ) 648let buf = RunVimInTerminal ( '-S XtestExprMap' , #{ rows : 10 }) 649call term_sendkeys ( buf , "\<C-B>" ) 650call VerifyScreenDump ( buf , 'Test_map_expr_1' , {}) 651 652" clean up 653call StopVimInTerminal ( buf ) 654call delete ( 'XtestExprMap' ) 655endfunc 656 657func Test_map_listing () 658CheckScreendump 659 660let lines =<< trim END 661nmap a b 662END 663call writefile ( lines , 'XtestMapList' ) 664let buf = RunVimInTerminal ( '-S XtestMapList' , #{ rows : 6 }) 665call term_sendkeys ( buf , ": nmap a\<CR>" ) 666call VerifyScreenDump ( buf , 'Test_map_list_1' , {}) 667 668" clean up 669call StopVimInTerminal ( buf ) 670call delete ( 'XtestMapList' ) 671endfunc 672 673func Test_expr_map_error () 674CheckScreendump 675 676let lines =<< trim END 677func Func() 678throw 'test' 679return '' 680endfunc 681 682nnoremap <expr> <F2> Func() 683cnoremap <expr> <F2> Func() 684 685call test_override('ui_delay', 10) 686END 687call writefile ( lines , 'XtestExprMap' ) 688let buf = RunVimInTerminal ( '-S XtestExprMap' , #{ rows : 10 }) 689call term_sendkeys ( buf , "\<F2>" ) 690call TermWait ( buf ) 691call term_sendkeys ( buf , "\<CR>" ) 692call VerifyScreenDump ( buf , 'Test_map_expr_2' , {}) 693 694call term_sendkeys ( buf , ":abc\<F2>" ) 695call VerifyScreenDump ( buf , 'Test_map_expr_3' , {}) 696call term_sendkeys ( buf , "\<Esc>0" ) 697call VerifyScreenDump ( buf , 'Test_map_expr_4' , {}) 698 699" clean up 700call StopVimInTerminal ( buf ) 701call delete ( 'XtestExprMap' ) 702endfunc 703 704" Test for mapping errors 705func Test_map_error () 706call assert_fails ( 'unmap' , 'E474:' ) 707call assert_fails ( "exe 'map ' .. repeat('a', 51) .. ' :ls'" , 'E474:' ) 708call assert_fails ( 'unmap abc' , 'E31:' ) 709call assert_fails ( 'unabbr abc' , 'E24:' ) 710call assert_equal ( '' , maparg ( '' )) 711call assert_fails ( 'echo maparg("abc", [])' , 'E730:' ) 712 713" unique map 714map ,w /[#&!]< CR > 715call assert_fails ( "map <unique> ,w /[#&!]<CR>" , 'E227:' ) 716" unique buffer-local map 717call assert_fails ( "map <buffer> <unique> ,w /[.,;]<CR>" , 'E225:' ) 718unmap ,w 719 720" unique abbreviation 721abbr SP special 722call assert_fails ( "abbr <unique> SP special" , 'E226:' ) 723" unique buffer-local map 724call assert_fails ( "abbr <buffer> <unique> SP special" , 'E224:' ) 725unabbr SP 726 727call assert_fails ( 'mapclear abc' , 'E474:' ) 728call assert_fails ( 'abclear abc' , 'E474:' ) 729call assert_fails ( 'abbr $xyz abc' , 'E474:' ) 730 731" space character in an abbreviation 732call assert_fails ( 'abbr ab<space> ABC' , 'E474:' ) 733 734" invalid <expr> map 735map <expr> ,fabc 736call assert_fails ( 'normal ,f' , 'E121:' ) 737unmap <expr> ,f 738 739" Recursive use of :normal in a map 740set maxmapdepth = 100 741map gq :normal gq< CR > 742call assert_fails ( 'normal gq' , 'E192:' ) 743unmap gq 744set maxmapdepth & 745endfunc 746 747" Test for <special> key mapping 748func Test_map_special () 749throw 'skipped: Nvim does not support cpoptions flag "<"' 750new 751let old_cpo = & cpo 752set cpo += < 753imap < F12 > Blue 754call feedkeys ( "i\<F12>" , "x" ) 755call assert_equal ( "<F12>" , getline ( 1 )) 756call feedkeys ( "ddi<F12>" , "x" ) 757call assert_equal ( "Blue" , getline ( 1 )) 758iunmap <F12> 759imap < special>< F12 > Green 760call feedkeys ( "ddi\<F12>" , "x" ) 761call assert_equal ( "Green" , getline ( 1 )) 762call feedkeys ( "ddi<F12>" , "x" ) 763call assert_equal ( "<F12>" , getline ( 1 )) 764iunmap <special> <F12> 765let & cpo = old_cpo 766 %bwipe ! 767endfunc 768 769" Test for hasmapto() 770func Test_hasmapto () 771call assert_equal ( 0 , hasmapto ( '/^\k\+ (' )) 772map ,f /^\k\+ (< CR > 773call assert_equal ( 1 , hasmapto ( '/^\k\+ (' )) 774unmap ,f 775 776" Insert mode mapping 777call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'i' )) 778imap ,f /^\k\+ (< CR > 779call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'i' )) 780iunmap ,f 781 782" Normal mode mapping 783call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'n' )) 784nmap ,f /^\k\+ (< CR > 785call assert_equal ( 1 , hasmapto ( '/^\k\+ (' )) 786call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'n' )) 787nunmap ,f 788 789" Visual and Select mode mapping 790call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'v' )) 791call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'x' )) 792call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 's' )) 793vmap ,f /^\k\+ (< CR > 794call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'v' )) 795call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'x' )) 796call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 's' )) 797vunmap ,f 798 799" Visual mode mapping 800call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'x' )) 801xmap ,f /^\k\+ (< CR > 802call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'v' )) 803call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'x' )) 804call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 's' )) 805xunmap ,f 806 807" Select mode mapping 808call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 's' )) 809smap ,f /^\k\+ (< CR > 810call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'v' )) 811call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'x' )) 812call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 's' )) 813sunmap ,f 814 815" Operator-pending mode mapping 816call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'o' )) 817omap ,f /^\k\+ (< CR > 818call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'o' )) 819ounmap ,f 820 821" Language mapping 822call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'l' )) 823lmap ,f /^\k\+ (< CR > 824call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'l' )) 825lunmap ,f 826 827" Cmdline mode mapping 828call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'c' )) 829cmap ,f /^\k\+ (< CR > 830call assert_equal ( 1 , hasmapto ( '/^\k\+ (' , 'c' )) 831cunmap ,f 832 833call assert_equal ( 0 , hasmapto ( '/^\k\+ (' , 'n' , 1 )) 834endfunc 835 836" Test for command-line completion of maps 837func Test_mapcomplete () 838call assert_equal ([ '<buffer>' , '<expr>' , '<nowait>' , '<script>' , 839 \'<silent>' , '<special>' , '<unique>' ], 840 \getcompletion ( '' , 'mapping' )) 841call assert_equal ([], getcompletion ( ',d' , 'mapping' )) 842 843call feedkeys ( ":unmap <buf\<C-A>\<C-B>\"\<CR>" , 'tx' ) 844call assert_equal ( '"unmap <buffer>' , @:) 845 846call feedkeys ( ":unabbr <buf\<C-A>\<C-B>\"\<CR>" , 'tx' ) 847call assert_equal ( '"unabbr <buffer>' , @:) 848 849call feedkeys ( ":abbr! \<C-A>\<C-B>\"\<CR>" , 'tx' ) 850call assert_equal ( "\"abbr! \x01" , @:) 851 852" When multiple matches have the same {lhs}, it should only appear once. 853" The simplified form should also not be included. 854nmap ,< C-F > /H< CR > 855omap ,< C-F > /H< CR > 856call feedkeys ( ":map ,\<C-A>\<C-B>\"\<CR>" , 'tx' ) 857call assert_equal ( '"map ,<C-F>' , @:) 858mapclear 859endfunc 860 861func GetAbbrText () 862unabbr hola 863return 'hello' 864endfunc 865 866" Test for <expr> in abbreviation 867func Test_expr_abbr () 868new 869iabbr <expr> teh"the" 870call feedkeys ( "iteh " , "tx" ) 871call assert_equal ( 'the ' , getline ( 1 )) 872iabclear 873call setline ( 1 , '' ) 874 875" invalid <expr> abbreviation 876abbr <expr> hte GetAbbr() 877call assert_fails ( 'normal ihte ' , 'E117:' ) 878call assert_equal ( '' , getline ( 1 )) 879unabbr <expr> hte 880 881" evaluating the expression deletes the abbreviation 882abbr <expr> hola GetAbbrText() 883call assert_equal ( 'GetAbbrText()' , maparg ( 'hola' , 'i' , '1' )) 884call feedkeys ( "ahola \<Esc>" , 'xt' ) 885call assert_equal ( 'hello ' , getline ( '.' )) 886call assert_equal ( '' , maparg ( 'hola' , 'i' , '1' )) 887 888bwipe ! 889endfunc 890 891" Test for storing mappings in different modes in a vimrc file 892func Test_mkvimrc_mapmodes () 893map a1 /a1 894nmap a2 /a2 895vmap a3 /a3 896smap a4 /a4 897xmap a5 /a5 898omap a6 /a6 899map ! a7 /a7 900imap a8 /a8 901lmap a9 /a9 902cmap a10 /a10 903tmap a11 /a11 904" Normal + Visual map 905map a12 /a12 906sunmap a12 907ounmap a12 908" Normal + Selectmode map 909map a13 /a13 910xunmap a13 911ounmap a13 912" Normal + OpPending map 913map a14 /a14 914vunmap a14 915" Visual + Selectmode map 916map a15 /a15 917nunmap a15 918ounmap a15 919" Visual + OpPending map 920map a16 /a16 921nunmap a16 922sunmap a16 923" Selectmode + OpPending map 924map a17 /a17 925nunmap a17 926xunmap a17 927" Normal + Visual + Selectmode map 928map a18 /a18 929ounmap a18 930" Normal + Visual + OpPending map 931map a19 /a19 932sunmap a19 933" Normal + Selectmode + OpPending map 934map a20 /a20 935xunmap a20 936" Visual + Selectmode + OpPending map 937map a21 /a21 938nunmap a21 939" Mapping to Nop 940map a22< Nop > 941" Script local mapping 942map <script> a23 /a23 943 944" Newline in {lhs} and {rhs} of a map 945exe "map a24\<C-V>\<C-J> ia24\<C-V>\<C-J><Esc>" 946 947" Abbreviation 948abbr a25 A25 949cabbr a26 A26 950iabbr a27 A27 951 952mkvimrc ! Xvimrc 953let l = readfile ( 'Xvimrc' ) 954call assert_equal ([ 'map a1 /a1' ], filter ( copy ( l ), 'v:val =~ " a1 "' )) 955call assert_equal ([ 'nmap a2 /a2' ], filter ( copy ( l ), 'v:val =~ " a2 "' )) 956call assert_equal ([ 'vmap a3 /a3' ], filter ( copy ( l ), 'v:val =~ " a3 "' )) 957call assert_equal ([ 'smap a4 /a4' ], filter ( copy ( l ), 'v:val =~ " a4 "' )) 958call assert_equal ([ 'xmap a5 /a5' ], filter ( copy ( l ), 'v:val =~ " a5 "' )) 959call assert_equal ([ 'omap a6 /a6' ], filter ( copy ( l ), 'v:val =~ " a6 "' )) 960call assert_equal ([ 'map! a7 /a7' ], filter ( copy ( l ), 'v:val =~ " a7 "' )) 961call assert_equal ([ 'imap a8 /a8' ], filter ( copy ( l ), 'v:val =~ " a8 "' )) 962call assert_equal ([ 'lmap a9 /a9' ], filter ( copy ( l ), 'v:val =~ " a9 "' )) 963call assert_equal ([ 'cmap a10 /a10' ], filter ( copy ( l ), 'v:val =~ " a10 "' )) 964call assert_equal ([ 'tmap a11 /a11' ], filter ( copy ( l ), 'v:val =~ " a11 "' )) 965call assert_equal ([ 'nmap a12 /a12' , 'xmap a12 /a12' ], 966 \filter ( copy ( l ), 'v:val =~ " a12 "' )) 967call assert_equal ([ 'nmap a13 /a13' , 'smap a13 /a13' ], 968 \filter ( copy ( l ), 'v:val =~ " a13 "' )) 969call assert_equal ([ 'nmap a14 /a14' , 'omap a14 /a14' ], 970 \filter ( copy ( l ), 'v:val =~ " a14 "' )) 971call assert_equal ([ 'vmap a15 /a15' ], filter ( copy ( l ), 'v:val =~ " a15 "' )) 972call assert_equal ([ 'xmap a16 /a16' , 'omap a16 /a16' ], 973 \filter ( copy ( l ), 'v:val =~ " a16 "' )) 974call assert_equal ([ 'smap a17 /a17' , 'omap a17 /a17' ], 975 \filter ( copy ( l ), 'v:val =~ " a17 "' )) 976call assert_equal ([ 'nmap a18 /a18' , 'vmap a18 /a18' ], 977 \filter ( copy ( l ), 'v:val =~ " a18 "' )) 978call assert_equal ([ 'nmap a19 /a19' , 'xmap a19 /a19' , 'omap a19 /a19' ], 979 \filter ( copy ( l ), 'v:val =~ " a19 "' )) 980call assert_equal ([ 'nmap a20 /a20' , 'smap a20 /a20' , 'omap a20 /a20' ], 981 \filter ( copy ( l ), 'v:val =~ " a20 "' )) 982call assert_equal ([ 'vmap a21 /a21' , 'omap a21 /a21' ], 983 \filter ( copy ( l ), 'v:val =~ " a21 "' )) 984call assert_equal ([ 'map a22 <Nop>' ], filter ( copy ( l ), 'v:val =~ " a22 "' )) 985call assert_equal ([], filter ( copy ( l ), 'v:val =~ " a23 "' )) 986call assert_equal ([ "map a24<NL> ia24<NL>\x16\e" ], 987 \filter ( copy ( l ), 'v:val =~ " a24"' )) 988 989call assert_equal ([ 'abbr a25 A25' ], filter ( copy ( l ), 'v:val =~ " a25 "' )) 990call assert_equal ([ 'cabbr a26 A26' ], filter ( copy ( l ), 'v:val =~ " a26 "' )) 991call assert_equal ([ 'iabbr a27 A27' ], filter ( copy ( l ), 'v:val =~ " a27 "' )) 992call delete ( 'Xvimrc' ) 993 994mapclear 995nmapclear 996vmapclear 997xmapclear 998smapclear 999omapclear 1000imapclear 1001lmapclear 1002cmapclear 1003tmapclear 1004endfunc 1005 1006" Test for recursive mapping ('maxmapdepth') 1007func Test_map_recursive () 1008map x y 1009map y x 1010call assert_fails ( 'normal x' , 'E223:' ) 1011unmap x 1012unmap y 1013endfunc 1014 1015" Test for removing an abbreviation using {rhs} and with space after {lhs} 1016func Test_abbr_remove () 1017abbr foo bar 1018let d = maparg ( 'foo' , 'i' , 1 , 1 ) 1019call assert_equal ([ 'foo' , 'bar' , '!' ], [ d . lhs , d . rhs , d . mode ]) 1020unabbr bar 1021call assert_equal ({}, maparg ( 'foo' , 'i' , 1 , 1 )) 1022 1023abbr foo bar 1024unabbr foo<space><tab> 1025call assert_equal ({}, maparg ( 'foo' , 'i' , 1 , 1 )) 1026endfunc 1027 1028" Trigger an abbreviation using a special key 1029func Test_abbr_trigger_special () 1030new 1031iabbr teh the 1032call feedkeys ( "iteh\<F2>\<Esc>" , 'xt' ) 1033call assert_equal ( 'the<F2>' , getline ( 1 )) 1034iunab teh 1035close ! 1036endfunc 1037 1038" Test for '<' in 'cpoptions' 1039func Test_map_cpo_special_keycode () 1040set cpo -= < 1041imap x< Bslash > k Test 1042let d = maparg ( 'x<Bslash>k' , 'i' , 0 , 1 ) 1043call assert_equal ([ 'x\k' , 'Test' , 'i' ], [ d . lhs , d . rhs , d . mode ]) 1044call feedkeys ( ":imap x\<C-A>\<C-B>\"\<CR>" , 'tx' ) 1045call assert_equal ( '"imap x\k' , @:) 1046iunmap x<Bslash>k 1047" Nvim: no "<" flag in 'cpoptions'. 1048" set cpo+=< 1049" imap x<Bslash>k Test 1050" let d = maparg('x<Bslash>k', 'i', 0, 1) 1051" call assert_equal(['x<Bslash>k', 'Test', 'i'], [d.lhs, d.rhs, d.mode]) 1052" call feedkeys(":imap x\<C-A>\<C-B>\"\<CR>", 'tx') 1053" call assert_equal('"imap x<Bslash>k', @:) 1054" iunmap x<Bslash>k 1055set cpo -= < 1056" Modifying 'cpo' above adds some default mappings, remove them 1057mapclear 1058mapclear ! 1059endfunc 1060 1061" Test for <Cmd> key in maps to execute commands 1062func Test_map_cmdkey () 1063new 1064 1065" Error cases 1066let x = 0 1067 noremap< F3 > <Cmd> <Cmd>let x =1<CR> 1068call assert_fails ( 'call feedkeys("\<F3>", "xt")' , 'E1136:' ) 1069call assert_equal ( 0 , x ) 1070 1071noremap < F3 > <Cmd> let x = 3 1072call assert_fails ( 'call feedkeys("\<F3>", "xt!")' , 'E1255:' ) 1073call assert_equal ( 0 , x ) 1074 1075" works in various modes and sees the correct mode() 1076noremap < F3 > <Cmd> let m = mode ( 1 ) <CR> 1077noremap ! < F3 > < Cmd>let m = mode(1) < CR > 1078 1079" normal mode 1080call feedkeys ( "\<F3>" , 'xt' ) 1081call assert_equal ( 'n' , m ) 1082 1083" visual mode 1084call feedkeys ( "v\<F3>" , 'xt!' ) 1085call assert_equal ( 'v' , m ) 1086" shouldn't leave the visual mode 1087call assert_equal ( 'v' , mode ( 1 )) 1088call feedkeys ( "\<Esc>" , 'xt' ) 1089call assert_equal ( 'n' , mode ( 1 )) 1090 1091" visual mapping in select mode 1092call feedkeys ( "gh\<F3>" , 'xt!' ) 1093call assert_equal ( 'v' , m ) 1094" shouldn't leave select mode 1095call assert_equal ( 's' , mode ( 1 )) 1096call feedkeys ( "\<Esc>" , 'xt' ) 1097call assert_equal ( 'n' , mode ( 1 )) 1098 1099" select mode mapping 1100snoremap < F3 > <Cmd> let m = mode ( 1 ) <cr> 1101call feedkeys ( "gh\<F3>" , 'xt!' ) 1102call assert_equal ( 's' , m ) 1103" shouldn't leave select mode 1104call assert_equal ( 's' , mode ( 1 )) 1105call feedkeys ( "\<Esc>" , 'xt' ) 1106call assert_equal ( 'n' , mode ( 1 )) 1107 1108" operator-pending mode 1109call feedkeys ( "d\<F3>" , 'xt!' ) 1110call assert_equal ( 'no' , m ) 1111" leaves the operator-pending mode 1112call assert_equal ( 'n' , mode ( 1 )) 1113 1114" insert mode 1115call feedkeys ( "i\<F3>abc" , 'xt' ) 1116call assert_equal ( 'i' , m ) 1117call assert_equal ( 'abc' , getline ( '.' )) 1118 1119" replace mode 1120call feedkeys ( "0R\<F3>two" , 'xt' ) 1121call assert_equal ( 'R' , m ) 1122call assert_equal ( 'two' , getline ( '.' )) 1123 1124" virtual replace mode 1125call setline ( '.' , "one\ttwo" ) 1126call feedkeys ( "4|gR\<F3>xxx" , 'xt' ) 1127call assert_equal ( 'Rv' , m ) 1128call assert_equal ( "onexxx\ttwo" , getline ( '.' )) 1129 1130" cmdline mode 1131call feedkeys ( ":\<F3>\"xxx\<CR>" , 'xt!' ) 1132call assert_equal ( 'c' , m ) 1133call assert_equal ( '"xxx' , @: ) 1134 1135" terminal mode 1136if CanRunVimInTerminal () 1137tnoremap < F3 > <Cmd> let m = mode ( 1 ) <CR> 1138let buf = Run_shell_in_terminal ({}) 1139call feedkeys ( "\<F3>" , 'xt' ) 1140call assert_equal ( 't' , m ) 1141call assert_equal ( 't' , mode ( 1 )) 1142call StopShellInTerminal ( buf ) 1143close ! 1144tunmap <F3> 1145endif 1146 1147" invoke cmdline mode recursively 1148noremap ! < F2 > < Cmd>norm! :foo < CR > 1149% d 1150call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1151call feedkeys ( ":bar\<F2>x\<C-B>\"\r" , 'xt' ) 1152call assert_equal ( '"barx' , @: ) 1153 1154" test for chars with 0x80 or 0x9b bytes 1155map < F2 > <Cmd> let x = '洛固四最倒倀'<CR> 1156call feedkeys ( "\<F2>" , 'xt' ) 1157call assert_equal ( '洛固四最倒倀' , x ) 1158 1159" test for calling a <SID> function 1160let lines =<< trim END 1161map <F2> <Cmd>call <SID>do_it()<CR> 1162func s:do_it() 1163let g:x = 32 1164endfunc 1165END 1166call writefile ( lines , 'Xscript' , 'D' ) 1167source Xscript 1168call feedkeys ( "\<F2>" , 'xt' ) 1169call assert_equal ( 32 , g: x ) 1170unlet g: x 1171 1172unmap <F2> 1173unmap ! <F2> 1174unmap <F3> 1175unmap ! <F3> 1176% bw ! 1177endfunc 1178 1179" text object enters visual mode 1180func TextObj () 1181if mode () !=# "v" 1182normal ! v 1183end 1184call cursor ( 1 , 3 ) 1185normal ! o 1186call cursor ( 2 , 4 ) 1187endfunc 1188 1189func s: cmdmap ( lhs , rhs ) 1190exe 'noremap ' .. a: lhs .. ' <Cmd>' .. a: rhs .. '<CR>' 1191exe 'noremap! ' .. a: lhs .. ' <Cmd>' .. a: rhs .. '<CR>' 1192endfunc 1193 1194func s: cmdunmap ( lhs ) 1195exe 'unmap ' .. a: lhs 1196exe 'unmap! ' .. a: lhs 1197endfunc 1198 1199" Map various <Fx> keys used by the <Cmd> key tests 1200func s: setupMaps () 1201call s: cmdmap ( '<F3>' , 'let m = mode(1)' ) 1202call s: cmdmap ( '<F4>' , 'normal! ww' ) 1203call s: cmdmap ( '<F5>' , 'normal! "ay' ) 1204call s: cmdmap ( '<F6>' , 'throw "very error"' ) 1205call s: cmdmap ( '<F7>' , 'call TextObj()' ) 1206call s: cmdmap ( '<F8>' , 'startinsert' ) 1207call s: cmdmap ( '<F9>' , 'stopinsert' ) 1208endfunc 1209 1210" Remove the mappings setup by setupMaps() 1211func s: cleanupMaps () 1212call s: cmdunmap ( '<F3>' ) 1213call s: cmdunmap ( '<F4>' ) 1214call s: cmdunmap ( '<F5>' ) 1215call s: cmdunmap ( '<F6>' ) 1216call s: cmdunmap ( '<F7>' ) 1217call s: cmdunmap ( '<F8>' ) 1218call s: cmdunmap ( '<F9>' ) 1219endfunc 1220 1221" Test for <Cmd> mapping in normal mode 1222func Test_map_cmdkey_normal_mode () 1223new 1224call s: setupMaps () 1225 1226" check v:count and v:register works 1227call s: cmdmap ( '<F2>' , 'let s = [mode(1), v:count, v:register]' ) 1228call feedkeys ( "\<F2>" , 'xt' ) 1229call assert_equal ([ 'n' , 0 , '"' ], s ) 1230call feedkeys ( "7\<F2>" , 'xt' ) 1231call assert_equal ([ 'n' , 7 , '"' ], s ) 1232call feedkeys ( "\"e\<F2>" , 'xt' ) 1233call assert_equal ([ 'n' , 0 , 'e' ], s ) 1234call feedkeys ( "5\"k\<F2>" , 'xt' ) 1235call assert_equal ([ 'n' , 5 , 'k' ], s ) 1236call s: cmdunmap ( '<F2>' ) 1237 1238call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1239call feedkeys ( "\<F7>y" , 'xt' ) 1240call assert_equal ( "me short lines\nof t" , @" ) 1241call assert_equal ( 'v' , getregtype ( '"' )) 1242call assert_equal ([ 0 , 1 , 3 , 0 ], getpos ( "'<" )) 1243call assert_equal ([ 0 , 2 , 4 , 0 ], getpos ( "'>" )) 1244 1245" startinsert 1246% d 1247call feedkeys ( "\<F8>abc" , 'xt' ) 1248call assert_equal ( 'abc' , getline ( 1 )) 1249 1250" feedkeys are not executed immediately 1251noremap ,a <Cmd> call feedkeys ( "aalpha" ) \| let g: a = getline ( 2 ) <CR> 1252% d 1253call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1254call cursor ( 2 , 3 ) 1255call feedkeys ( ",a\<F3>" , 'xt' ) 1256call assert_equal ( 'of test text' , g: a ) 1257call assert_equal ( 'n' , m ) 1258call assert_equal ([ 'some short lines' , 'of alphatest text' ], getline ( 1 , '$' )) 1259nunmap ,a 1260 1261" feedkeys(..., 'x') is executed immediately, but insert mode is aborted 1262noremap ,b <Cmd> call feedkeys ( "abeta" , 'x' ) \| let g: b = getline ( 2 ) <CR> 1263call feedkeys ( ",b\<F3>" , 'xt' ) 1264call assert_equal ( 'n' , m ) 1265call assert_equal ( 'of alphabetatest text' , g: b ) 1266nunmap ,b 1267 1268call s: cleanupMaps () 1269% bw ! 1270endfunc 1271 1272" Test for <Cmd> mapping with the :normal command 1273func Test_map_cmdkey_normal_cmd () 1274new 1275noremap ,x <Cmd> call append ( 1 , "xx" ) \| call append ( 1 , "aa" ) <CR> 1276noremap ,f <Cmd> nosuchcommand <CR> 1277noremap ,e <Cmd> throw "very error" \| call append ( 1 , "yy" ) <CR> 1278noremap ,m <Cmd> echoerr "The message." \| call append ( 1 , "zz" ) <CR> 1279noremap ,w <Cmd> for i in range ( 5 ) \| if i== 1 \| echoerr " Err" \| endif \| call append( 1, i) \| endfor< CR> 1280 1281call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1282exe "norm ,x\r" 1283call assert_equal ([ 'some short lines' , 'aa' , 'xx' , 'of test text' ], getline ( 1 , '$' )) 1284 1285call assert_fails ( 'norm ,f' , 'E492:' ) 1286call assert_fails ( 'norm ,e' , 'very error' ) 1287call assert_fails ( 'norm ,m' , 'The message.' ) 1288call assert_equal ([ 'some short lines' , 'aa' , 'xx' , 'of test text' ], getline ( 1 , '$' )) 1289 1290% d 1291let caught_err = 0 1292try 1293exe "normal ,w" 1294catch /Vim(echoerr):Err/ 1295let caught_err = 1 1296endtry 1297call assert_equal ( 1 , caught_err ) 1298call assert_equal ([ '' , '0' ], getline ( 1 , '$' )) 1299 1300% d 1301call assert_fails ( 'normal ,w' , 'Err' ) 1302call assert_equal ([ '' , '4' , '3' , '2' , '1' , '0' ], getline ( 1 , '$' )) 1303call assert_equal ( 1 , line ( '.' )) 1304 1305nunmap ,x 1306nunmap ,f 1307nunmap ,e 1308nunmap ,m 1309nunmap ,w 1310% bw ! 1311endfunc 1312 1313" Test for <Cmd> mapping in visual mode 1314func Test_map_cmdkey_visual_mode () 1315new 1316set showmode 1317call s: setupMaps () 1318 1319call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1320call feedkeys ( "v\<F4>" , 'xt!' ) 1321call assert_equal ([ 'v' , 1 , 12 ], [ mode ( 1 ), col ( 'v' ), col ( '.' )]) 1322 1323" can invoke an operator, ending the visual mode 1324let @a = '' 1325call feedkeys ( "\<F5>" , 'xt!' ) 1326call assert_equal ( 'n' , mode ( 1 )) 1327call assert_equal ( 'some short l' , @a ) 1328 1329" error doesn't interrupt visual mode 1330call assert_fails ( 'call feedkeys("ggvw\<F6>", "xt!")' , 'E605:' ) 1331call assert_equal ([ 'v' , 1 , 6 ], [ mode ( 1 ), col ( 'v' ), col ( '.' )]) 1332call feedkeys ( "\<F7>" , 'xt!' ) 1333call assert_equal ([ 'v' , 1 , 3 , 2 , 4 ], [ mode ( 1 ), line ( 'v' ), col ( 'v' ), line ( '.' ), col ( '.' )]) 1334 1335" startinsert gives "-- (insert) VISUAL --" mode 1336call feedkeys ( "\<F8>" , 'xt!' ) 1337call assert_equal ([ 'v' , 1 , 3 , 2 , 4 ], [ mode ( 1 ), line ( 'v' ), col ( 'v' ), line ( '.' ), col ( '.' )]) 1338redraw ! 1339call assert_match ( '^-- (insert) VISUAL --' , Screenline ( & lines )) 1340call feedkeys ( "\<Esc>new " , 'x' ) 1341call assert_equal ([ 'some short lines' , 'of new test text' ], getline ( 1 , '$' )) 1342 1343call s: cleanupMaps () 1344set showmode & 1345% bw ! 1346endfunc 1347 1348" Test for <Cmd> mapping in select mode 1349func Test_map_cmdkey_select_mode () 1350new 1351set showmode 1352call s: setupMaps () 1353 1354snoremap < F1 > <cmd> throw "very error"<CR> 1355snoremap < F2 > <cmd> normal ! <c-g>"by<CR> 1356call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1357 1358call feedkeys ( "gh\<F4>" , "xt!" ) 1359call assert_equal ([ 's' , 1 , 12 ], [ mode ( 1 ), col ( 'v' ), col ( '.' )]) 1360redraw ! 1361call assert_match ( '^-- SELECT --' , Screenline ( & lines )) 1362 1363" visual mapping in select mode restarts select mode after operator 1364let @a = '' 1365call feedkeys ( "\<F5>" , 'xt!' ) 1366call assert_equal ( 's' , mode ( 1 )) 1367call assert_equal ( 'some short l' , @a ) 1368 1369" select mode mapping works, and does not restart select mode 1370let @b = '' 1371call feedkeys ( "\<F2>" , 'xt!' ) 1372call assert_equal ( 'n' , mode ( 1 )) 1373call assert_equal ( 'some short l' , @b ) 1374 1375" error doesn't interrupt temporary visual mode 1376call assert_fails ( 'call feedkeys("\<Esc>ggvw\<C-G>\<F6>", "xt!")' , 'E605:' ) 1377redraw ! 1378call assert_match ( '^-- VISUAL --' , Screenline ( & lines )) 1379" quirk: restoration of select mode is not performed 1380call assert_equal ([ 'v' , 1 , 6 ], [ mode ( 1 ), col ( 'v' ), col ( '.' )]) 1381 1382" error doesn't interrupt select mode 1383call assert_fails ( 'call feedkeys("\<Esc>ggvw\<C-G>\<F1>", "xt!")' , 'E605:' ) 1384redraw ! 1385call assert_match ( '^-- SELECT --' , Screenline ( & lines )) 1386call assert_equal ([ 's' , 1 , 6 ], [ mode ( 1 ), col ( 'v' ), col ( '.' )]) 1387 1388call feedkeys ( "\<F7>" , 'xt!' ) 1389redraw ! 1390call assert_match ( '^-- SELECT --' , Screenline ( & lines )) 1391call assert_equal ([ 's' , 1 , 3 , 2 , 4 ], [ mode ( 1 ), line ( 'v' ), col ( 'v' ), line ( '.' ), col ( '.' )]) 1392 1393" startinsert gives "-- SELECT (insert) --" mode 1394call feedkeys ( "\<F8>" , 'xt!' ) 1395redraw ! 1396call assert_match ( '^-- (insert) SELECT --' , Screenline ( & lines )) 1397call assert_equal ([ 's' , 1 , 3 , 2 , 4 ], [ mode ( 1 ), line ( 'v' ), col ( 'v' ), line ( '.' ), col ( '.' )]) 1398call feedkeys ( "\<Esc>new " , 'x' ) 1399call assert_equal ([ 'some short lines' , 'of new test text' ], getline ( 1 , '$' )) 1400 1401sunmap <F1> 1402sunmap <F2> 1403call s: cleanupMaps () 1404set showmode & 1405% bw ! 1406endfunc 1407 1408" Test for <Cmd> mapping in operator-pending mode 1409func Test_map_cmdkey_op_pending_mode () 1410new 1411call s: setupMaps () 1412 1413call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1414call feedkeys ( "d\<F4>" , 'xt' ) 1415call assert_equal ([ 'lines' , 'of test text' ], getline ( 1 , '$' )) 1416call assert_equal ([ 'some short ' ], getreg ( '"' , 1 , 1 )) 1417" create a new undo point 1418let & g: undolevels = & g: undolevels 1419 1420call feedkeys ( "." , 'xt' ) 1421call assert_equal ([ 'test text' ], getline ( 1 , '$' )) 1422call assert_equal ([ 'lines' , 'of ' ], getreg ( '"' , 1 , 1 )) 1423" create a new undo point 1424let & g: undolevels = & g: undolevels 1425 1426call feedkeys ( "uu" , 'xt' ) 1427call assert_equal ([ 'some short lines' , 'of test text' ], getline ( 1 , '$' )) 1428 1429" error aborts operator-pending, operator not performed 1430call assert_fails ( 'call feedkeys("d\<F6>", "xt")' , 'E605:' ) 1431call assert_equal ([ 'some short lines' , 'of test text' ], getline ( 1 , '$' )) 1432 1433call feedkeys ( "\"bd\<F7>" , 'xt' ) 1434call assert_equal ([ 'soest text' ], getline ( 1 , '$' )) 1435call assert_equal ([ 'me short lines' , 'of t' ], getreg ( 'b' , 1 , 1 )) 1436 1437" startinsert aborts operator 1438call feedkeys ( "d\<F8>cc" , 'xt' ) 1439call assert_equal ([ 'soccest text' ], getline ( 1 , '$' )) 1440 1441call s: cleanupMaps () 1442% bw ! 1443endfunc 1444 1445" Test for <Cmd> mapping in insert mode 1446func Test_map_cmdkey_insert_mode () 1447new 1448call s: setupMaps () 1449 1450call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1451" works the same as <C-O>w<C-O>w 1452call feedkeys ( "iindeed \<F4>little " , 'xt' ) 1453call assert_equal ([ 'indeed some short little lines' , 'of test text' ], getline ( 1 , '$' )) 1454call assert_fails ( 'call feedkeys("i\<F6> 2", "xt")' , 'E605:' ) 1455call assert_equal ([ 'indeed some short little 2 lines' , 'of test text' ], getline ( 1 , '$' )) 1456 1457" Note when entering visual mode from InsertEnter autocmd, an async event, 1458" or a <Cmd> mapping, vim ends up in undocumented "INSERT VISUAL" mode. 1459call feedkeys ( "i\<F7>stuff " , 'xt' ) 1460call assert_equal ([ 'indeed some short little 2 lines' , 'of stuff test text' ], getline ( 1 , '$' )) 1461call assert_equal ([ 'v' , 1 , 3 , 2 , 9 ], [ mode ( 1 ), line ( 'v' ), col ( 'v' ), line ( '.' ), col ( '.' )]) 1462 1463call feedkeys ( "\<F5>" , 'xt' ) 1464call assert_equal ([ 'deed some short little 2 lines' , 'of stuff ' ], getreg ( 'a' , 1 , 1 )) 1465 1466" also works as part of abbreviation 1467abbr foo <Cmd>let g:y = 17<CR>bar 1468exe "normal i\<space>foo " 1469call assert_equal ( 17 , g: y ) 1470call assert_equal ( 'in bar deed some short little 2 lines' , getline ( 1 )) 1471unabbr foo 1472 1473" :startinsert does nothing 1474call setline ( 1 , 'foo bar' ) 1475call feedkeys ( "ggi\<F8>vim" , 'xt' ) 1476call assert_equal ( 'vimfoo bar' , getline ( 1 )) 1477 1478" :stopinsert works 1479call feedkeys ( "ggi\<F9>Abc" , 'xt' ) 1480call assert_equal ( 'vimfoo barbc' , getline ( 1 )) 1481 1482call s: cleanupMaps () 1483% bw ! 1484endfunc 1485 1486" Test for <Cmd> mapping in insert-completion mode 1487func Test_map_cmdkey_insert_complete_mode () 1488new 1489call s: setupMaps () 1490 1491call setline ( 1 , 'some short lines' ) 1492call feedkeys ( "os\<C-X>\<C-N>\<F3>\<C-N> " , 'xt' ) 1493call assert_equal ( 'ic' , m ) 1494call assert_equal ([ 'some short lines' , 'short ' ], getline ( 1 , '$' )) 1495 1496call s: cleanupMaps () 1497% bw ! 1498endfunc 1499 1500" Test for <Cmd> mapping in cmdline mode 1501func Test_map_cmdkey_cmdline_mode () 1502new 1503call s: setupMaps () 1504 1505call setline ( 1 , [ 'some short lines' , 'of test text' ]) 1506let x = 0 1507call feedkeys ( ":let x\<F3>= 10\r" , 'xt' ) 1508call assert_equal ( 'c' , m ) 1509call assert_equal ( 10 , x ) 1510 1511" exception doesn't leave cmdline mode 1512call assert_fails ( 'call feedkeys(":let x\<F6>= 20\r", "xt")' , 'E605:' ) 1513call assert_equal ( 20 , x ) 1514 1515" move cursor in the buffer from cmdline mode 1516call feedkeys ( ":let x\<F4>= 30\r" , 'xt' ) 1517call assert_equal ( 30 , x ) 1518call assert_equal ( 12 , col ( '.' )) 1519 1520" :startinsert takes effect after leaving cmdline mode 1521call feedkeys ( ":let x\<F8>= 40\rnew " , 'xt' ) 1522call assert_equal ( 40 , x ) 1523call assert_equal ( 'some short new lines' , getline ( 1 )) 1524 1525call s: cleanupMaps () 1526% bw ! 1527endfunc 1528 1529func Test_map_cmdkey_redo () 1530func SelectDash () 1531call search ( '^---\n\zs' , 'bcW' ) 1532norm ! V 1533call search ( '\n\ze---$' , 'W' ) 1534endfunc 1535 1536let text =<< trim END 1537--- 1538aaa 1539--- 1540bbb 1541bbb 1542--- 1543ccc 1544ccc 1545ccc 1546--- 1547END 1548new Xcmdtext 1549call setline ( 1 , text ) 1550 1551onoremap <silent> i- <Cmd> call SelectDash () <CR> 1552call feedkeys ( '2Gdi-' , 'xt' ) 1553call assert_equal ([ '---' , '---' ], getline ( 1 , 2 )) 1554call feedkeys ( 'j.' , 'xt' ) 1555call assert_equal ([ '---' , '---' , '---' ], getline ( 1 , 3 )) 1556call feedkeys ( 'j.' , 'xt' ) 1557call assert_equal ([ '---' , '---' , '---' , '---' ], getline ( 1 , 4 )) 1558 1559bwipe ! 1560call delete ( 'Xcmdtext' ) 1561delfunc SelectDash 1562ounmap i- 1563 1564new 1565call setline ( 1 , 'aaa bbb ccc ddd' ) 1566 1567" command can contain special keys 1568onoremap ix <Cmd> let g: foo ..= '…'<Bar> normal ! <C-Right><CR> 1569let g: foo = '' 1570call feedkeys ( '0dix.' , 'xt' ) 1571call assert_equal ( '……' , g: foo ) 1572call assert_equal ( 'ccc ddd' , getline ( 1 )) 1573unlet g: foo 1574 1575" command line ending in "0" is handled without errors 1576onoremap ix <Cmd> eval 0 <CR> 1577call feedkeys ( 'dix.' , 'xt' ) 1578 1579ounmap ix 1580bwipe ! 1581endfunc 1582 1583" Test for using <script> with a map to remap characters in rhs 1584func Test_script_local_remap () 1585new 1586inoremap <buffer> < SID > xyz mno 1587inoremap <buffer> <script> abc st < SID > xyzre 1588normal iabc 1589call assert_equal ( 'stmnore' , getline ( 1 )) 1590bwipe ! 1591endfunc 1592 1593func Test_abbreviate_multi_byte () 1594new 1595iabbrev foo bar 1596call feedkeys ( "ifoo…\<Esc>" , 'xt' ) 1597call assert_equal ( "bar…" , getline ( 1 )) 1598iunabbrev foo 1599bwipe ! 1600endfunc 1601 1602" Test for abbreviations with 'latin1' encoding 1603func Test_abbreviate_latin1_encoding () 1604" set encoding=latin1 1605call assert_fails ( 'abbr ab#$c ABC' , 'E474:' ) 1606new 1607iabbr <buffer> #i #include 1608iabbr <buffer> ## #enddef 1609exe "normal i#i\<C-]>" 1610call assert_equal ( '#include' , getline ( 1 )) 1611exe "normal 0Di##\<C-]>" 1612call assert_equal ( '#enddef' , getline ( 1 )) 1613% bw ! 1614set encoding = utf-8 1615endfunc 1616+ 1617" Test for <Plug> always being mapped, even when used with "noremap". 1618func Test_plug_remap () 1619let g: foo = 0 1620nnoremap < Plug > (Increase_x) <Cmd> let g: foo += 1 <CR> 1621nmap < F2 > < Plug > (Increase_x) 1622nnoremap < F3 > < Plug > (Increase_x) 1623call feedkeys ( "\<F2>" , 'xt' ) 1624call assert_equal ( 1 , g: foo ) 1625call feedkeys ( "\<F3>" , 'xt' ) 1626call assert_equal ( 2 , g: foo ) 1627nnoremap x < Nop > 1628nmap < F4 > x < Plug > (Increase_x)x 1629nnoremap < F5 > x < Plug > (Increase_x)x 1630call setline ( 1 , 'Some text' ) 1631normal ! gg$ 1632call feedkeys ( "\<F4>" , 'xt' ) 1633call assert_equal ( 3 , g: foo ) 1634call assert_equal ( 'Some text' , getline ( 1 )) 1635call feedkeys ( "\<F5>" , 'xt' ) 1636call assert_equal ( 4 , g: foo ) 1637call assert_equal ( 'Some te' , getline ( 1 )) 1638nunmap <Plug>(Increase_x) 1639nunmap <F2> 1640nunmap <F3> 1641nunmap <F4> 1642nunmap <F5> 1643unlet g: foo 1644% bw ! 1645endfunc 1646 1647func Test_mouse_drag_mapped_start_select () 1648set mouse = a 1649set selectmode = key,mouse 1650func ClickExpr () 1651call Ntest_setmouse ( 1 , 1 ) 1652return "\<LeftMouse>" 1653endfunc 1654func DragExpr () 1655call Ntest_setmouse ( 1 , 2 ) 1656return "\<LeftDrag>" 1657endfunc 1658nnoremap <expr> < F2 > ClickExpr () 1659nmap <expr> < F3 > DragExpr () 1660 1661nnoremap < Left Drag > <LeftDrag><Cmd><CR> 1662exe "normal \<F2>\<F3>" 1663call assert_equal ( 's' , mode ()) 1664exe "normal! \<C-\>\<C-N>" 1665 1666nunmap <LeftDrag> 1667nunmap <F2> 1668nunmap <F3> 1669delfunc ClickExpr 1670delfunc DragExpr 1671set selectmode & 1672set mouse & 1673endfunc 1674 1675func Test_mouse_drag_statusline () 1676set laststatus = 2 1677set mouse = a 1678func ClickExpr () 1679call Ntest_setmouse ( & lines - 1 , 1 ) 1680return "\<LeftMouse>" 1681endfunc 1682func DragExpr () 1683call Ntest_setmouse ( & lines - 2 , 1 ) 1684return "\<LeftDrag>" 1685endfunc 1686nnoremap <expr> < F2 > ClickExpr () 1687nnoremap <expr> < F3 > DragExpr () 1688 1689" this was causing a crash in win_drag_status_line() 1690call feedkeys ( "\<F2>:tabnew\<CR>\<F3>" , 'tx' ) 1691 1692nunmap <F2> 1693nunmap <F3> 1694delfunc ClickExpr 1695delfunc DragExpr 1696set laststatus & mouse & 1697endfunc 1698 1699" Test for mapping <LeftDrag> in Insert mode 1700func Test_mouse_drag_insert_map () 1701set mouse = a 1702func ClickExpr () 1703call Ntest_setmouse ( 1 , 1 ) 1704return "\<LeftMouse>" 1705endfunc 1706func DragExpr () 1707call Ntest_setmouse ( 1 , 2 ) 1708return "\<LeftDrag>" 1709endfunc 1710inoremap <expr> < F2 > ClickExpr () 1711imap <expr> < F3 > DragExpr () 1712 1713inoremap < Left Drag > <LeftDrag><Cmd>let g:dragged = 1<CR> 1714exe "normal i\<F2>\<F3>" 1715call assert_equal ( 1 , g: dragged ) 1716call assert_equal ( 'v' , mode ()) 1717exe "normal! \<C-\>\<C-N>" 1718unlet g: dragged 1719 1720inoremap < Left Drag > <LeftDrag><C-\><C-N> 1721exe "normal i\<F2>\<F3>" 1722call assert_equal ( 'n' , mode ()) 1723 1724iunmap <LeftDrag> 1725iunmap <F2> 1726iunmap <F3> 1727delfunc ClickExpr 1728delfunc DragExpr 1729set mouse & 1730endfunc 1731 1732func Test_unmap_simplifiable () 1733map < C-I > foo 1734map < Tab > bar 1735call assert_equal ( 'foo' , maparg ( '<C-I>' )) 1736call assert_equal ( 'bar' , maparg ( '<Tab>' )) 1737unmap <C-I> 1738call assert_equal ( '' , maparg ( '<C-I>' )) 1739call assert_equal ( 'bar' , maparg ( '<Tab>' )) 1740unmap <Tab> 1741 1742map < C-I > foo 1743unmap <Tab> 1744" This should not error 1745unmap <C-I> 1746endfunc 1747 1748" Test that the first byte of rhs is not remapped if rhs starts with lhs. 1749func Test_map_rhs_starts_with_lhs () 1750new 1751func MapExpr () 1752return "\<C-R>\<C-P>" 1753endfunc 1754 1755for expr in [ v: false , v: true ] 1756if expr 1757imap <buffer><expr> < C-R > MapExpr () 1758else 1759imap <buffer> < C-R > < C-R > <C-P > 1760endif 1761 1762for restore in [ v: false , v: true ] 1763if restore 1764let saved = maparg ( '<C-R>' , 'i' , v: false , v: true ) 1765iunmap <buffer> <C-R> 1766call mapset ( saved ) 1767endif 1768 1769let @a = 'foo' 1770call assert_nobeep ( 'call feedkeys("S\<C-R>a", "tx")' ) 1771call assert_equal ( 'foo' , getline ( '.' )) 1772 1773let @a = 'bar' 1774call assert_nobeep ( 'call feedkeys("S\<*C-R>a", "tx")' ) 1775call assert_equal ( 'bar' , getline ( '.' )) 1776endfor 1777endfor 1778 1779" When two mappings are used for <C-I> and <Tab>, remapping should work. 1780imap <buffer> < C-I > < Tab > bar 1781imap <buffer> < Tab > foo 1782call feedkeys ( "S\<Tab>" , 'xt' ) 1783call assert_equal ( 'foo' , getline ( '.' )) 1784call feedkeys ( "S\<*C-I>" , 'xt' ) 1785call assert_equal ( 'foobar' , getline ( '.' )) 1786 1787delfunc MapExpr 1788bwipe ! 1789endfunc 1790 1791func Test_expr_map_escape_special () 1792nnoremap … <Cmd> let g: got_ellipsis += 1 <CR> 1793func Func () 1794return '…' 1795endfunc 1796nmap <expr> < F2 > Func () 1797let g: got_ellipsis = 0 1798call feedkeys ( "\<F2>" , 'xt' ) 1799call assert_equal ( 1 , g: got_ellipsis ) 1800delfunc Func 1801nunmap <F2> 1802unlet g: got_ellipsis 1803nunmap … 1804endfunc 1805 1806" Testing for mapping after an <Nop> mapping is triggered on timeout. 1807" Test for what patch 8.1.0052 fixes. 1808func Test_map_after_timed_out_nop () 1809CheckRunVimInTerminal 1810 1811let lines =<< trim END 1812set timeout timeoutlen=400 1813inoremap ab TEST 1814inoremap a <Nop> 1815END 1816call writefile ( lines , 'Xtest_map_after_timed_out_nop' , 'D' ) 1817let buf = RunVimInTerminal ( '-S Xtest_map_after_timed_out_nop' , #{ rows : 6 }) 1818 1819" Enter Insert mode 1820call term_sendkeys ( buf , 'i' ) 1821" Wait for the "a" mapping to timeout 1822call term_sendkeys ( buf , 'a' ) 1823call term_wait ( buf , 500 ) 1824" Send "a" and wait for a period shorter than 'timeoutlen' 1825call term_sendkeys ( buf , 'a' ) 1826call term_wait ( buf , 100 ) 1827" Send "b", should trigger the "ab" mapping 1828call term_sendkeys ( buf , 'b' ) 1829call WaitForAssert ({ -> assert_equal ( "TEST" , term_getline ( buf , 1 ))}) 1830 1831" clean up 1832call StopVimInTerminal ( buf ) 1833endfunc 1834 1835" Test 'showcmd' behavior with a partial mapping 1836func Test_showcmd_part_map () 1837CheckRunVimInTerminal 1838 1839let lines =<< trim END 1840set notimeout showcmd 1841nnoremap ,a <Ignore> 1842nnoremap ;a <Ignore> 1843nnoremap Àa <Ignore> 1844nnoremap Ëa <Ignore> 1845nnoremap βa <Ignore> 1846nnoremap ωa <Ignore> 1847nnoremap …a <Ignore> 1848nnoremap <C-W>a <Ignore> 1849END 1850call writefile ( lines , 'Xtest_showcmd_part_map' , 'D' ) 1851let buf = RunVimInTerminal ( '-S Xtest_showcmd_part_map' , #{ rows : 6 }) 1852 1853call term_sendkeys ( buf , ":set noruler | echo\<CR>" ) 1854call WaitForAssert ({ -> assert_equal ( '' , term_getline ( buf , 6 ))}) 1855 1856for c in [ ',' , ';' , 'À' , 'Ë' , 'β' , 'ω' , '…' ] 1857call term_sendkeys ( buf , c ) 1858call WaitForAssert ({ -> assert_equal ( c , trim ( term_getline ( buf , 6 )))}) 1859call term_sendkeys ( buf , 'a' ) 1860call WaitForAssert ({ -> assert_equal ( '' , trim ( term_getline ( buf , 6 )))}) 1861endfor 1862 1863call term_sendkeys ( buf , "\<C-W>" ) 1864call WaitForAssert ({ -> assert_equal ( '^W' , trim ( term_getline ( buf , 6 )))}) 1865call term_sendkeys ( buf , 'a' ) 1866call WaitForAssert ({ -> assert_equal ( '' , trim ( term_getline ( buf , 6 )))}) 1867 1868" Use feedkeys() as terminal buffer cannot forward unsimplified Ctrl-W. 1869" This is like typing Ctrl-W with modifyOtherKeys enabled. 1870call term_sendkeys ( buf , ':call feedkeys("\<*C-W>", "m")' .. " | echo\<CR>" ) 1871call WaitForAssert ({ -> assert_equal ( '^W' , trim ( term_getline ( buf , 6 )))}) 1872call term_sendkeys ( buf , 'a' ) 1873call WaitForAssert ({ -> assert_equal ( '' , trim ( term_getline ( buf , 6 )))}) 1874 1875call StopVimInTerminal ( buf ) 1876endfunc 1877 1878func Test_using_past_typeahead () 1879nnoremap :00 0 1880exe "norm :set \x80\xfb0=0\<CR>" 1881exe "sil norm :0\x0f\<C-U>\<CR>" 1882 1883exe "norm :set \x80\xfb0=\<CR>" 1884nunmap :00 1885endfunc 1886 1887 1888" vim: shiftwidth=2 sts=2 expandtab