robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Test for edit functions 2 3if exists ( "+t_kD" ) 4let & t_kD = "[3;*~" 5endif 6 7source check.vim 8source screendump.vim 9source view_util.vim 10 11" Needs to come first until the bug in getchar() is 12" fixed: https://groups.google.com/d/msg/vim_dev/fXL9yme4H4c/bOR-U6_bAQAJ 13func Test_edit_00b () 14new 15call setline ( 1 , [ 'abc ' ]) 16inoreabbr <buffer> h here some more 17call cursor ( 1 , 4 ) 18" <esc> expands the abbreviation and ends insert mode 19" call feedkeys(":set im\<cr> h\<c-l>:set noim\<cr>", 'tix') 20call feedkeys ( "i h\<esc>" , 'tix' ) 21call assert_equal ([ 'abc here some more ' ], getline ( 1 , '$' )) 22iunabbr <buffer> h 23bw ! 24endfunc 25 26func Test_edit_01 () 27" set for Travis CI? 28" set nocp noesckeys 29new 30" 1) empty buffer 31call assert_equal ([ '' ], getline ( 1 , '$' )) 32" 2) delete in an empty line 33call feedkeys ( "i\<del>\<esc>" , 'tnix' ) 34call assert_equal ([ '' ], getline ( 1 , '$' )) 35 %d 36" 3) delete one character 37call setline ( 1 , 'a' ) 38call feedkeys ( "i\<del>\<esc>" , 'tnix' ) 39call assert_equal ([ '' ], getline ( 1 , '$' )) 40 %d 41" 4) delete a multibyte character 42call setline ( 1 , "\u0401" ) 43call feedkeys ( "i\<del>\<esc>" , 'tnix' ) 44call assert_equal ([ '' ], getline ( 1 , '$' )) 45 %d 46" 5.1) delete linebreak with 'bs' option containing eol 47let _bs =& bs 48set bs = eol 49call setline ( 1 , [ "abc def" , "ghi jkl" ]) 50call cursor ( 1 , 1 ) 51call feedkeys ( "A\<del>\<esc>" , 'tnix' ) 52call assert_equal ([ 'abc defghi jkl' ], getline ( 1 , 2 )) 53 %d 54" 5.2) delete linebreak with backspace option w/out eol 55set bs = 56call setline ( 1 , [ "abc def" , "ghi jkl" ]) 57call cursor ( 1 , 1 ) 58call feedkeys ( "A\<del>\<esc>" , 'tnix' ) 59call assert_equal ([ "abc def" , "ghi jkl" ], getline ( 1 , 2 )) 60let & bs = _bs 61bw ! 62endfunc 63 64func Test_edit_02 () 65" Change cursor position in InsertCharPre command 66new 67call setline ( 1 , 'abc' ) 68call cursor ( 1 , 1 ) 69fu ! DoIt (...) 70call cursor ( 1 , 4 ) 71if len ( a: 000 ) 72let v: char = a: 1 73endif 74endfu 75au InsertCharPre <buffer> : call DoIt ( 'y' ) 76call feedkeys ( "ix\<esc>" , 'tnix' ) 77call assert_equal ([ 'abcy' ], getline ( 1 , '$' )) 78" Setting <Enter> in InsertCharPre 79au ! InsertCharPre <buffer> : call DoIt ( "\n" ) 80call setline ( 1 , 'abc' ) 81call cursor ( 1 , 1 ) 82call feedkeys ( "ix\<esc>" , 'tnix' ) 83call assert_equal ([ 'abc' , '' ], getline ( 1 , '$' )) 84 %d 85au ! InsertCharPre 86" Change cursor position in InsertEnter command 87" 1) when setting v:char, keeps changed cursor position 88au ! InsertEnter <buffer> : call DoIt ( 'y' ) 89call setline ( 1 , 'abc' ) 90call cursor ( 1 , 1 ) 91call feedkeys ( "ix\<esc>" , 'tnix' ) 92call assert_equal ([ 'abxc' ], getline ( 1 , '$' )) 93" 2) when not setting v:char, restores changed cursor position 94au ! InsertEnter <buffer> : call DoIt () 95call setline ( 1 , 'abc' ) 96call cursor ( 1 , 1 ) 97call feedkeys ( "ix\<esc>" , 'tnix' ) 98call assert_equal ([ 'xabc' ], getline ( 1 , '$' )) 99au ! InsertEnter 100delfu DoIt 101bw ! 102endfunc 103 104func Test_edit_03 () 105" Change cursor after <c-o> command to end of line 106new 107call setline ( 1 , 'abc' ) 108call cursor ( 1 , 1 ) 109call feedkeys ( "i\<c-o>$y\<esc>" , 'tnix' ) 110call assert_equal ([ 'abcy' ], getline ( 1 , '$' )) 111 %d 112call setline ( 1 , 'abc' ) 113call cursor ( 1 , 1 ) 114call feedkeys ( "i\<c-o>80|y\<esc>" , 'tnix' ) 115call assert_equal ([ 'abcy' ], getline ( 1 , '$' )) 116 %d 117call setline ( 1 , 'abc' ) 118call feedkeys ( "Ad\<c-o>:s/$/efg/\<cr>hij" , 'tnix' ) 119call assert_equal ([ 'hijabcdefg' ], getline ( 1 , '$' )) 120bw ! 121endfunc 122 123func Test_edit_04 () 124" test for :stopinsert 125new 126call setline ( 1 , 'abc' ) 127call cursor ( 1 , 1 ) 128call feedkeys ( "i\<c-o>:stopinsert\<cr>$" , 'tnix' ) 129call feedkeys ( "aX\<esc>" , 'tnix' ) 130call assert_equal ([ 'abcX' ], getline ( 1 , '$' )) 131 %d 132bw ! 133endfunc 134 135func Test_edit_05 () 136" test for folds being opened 137new 138call setline ( 1 , [ 'abcX' , 'abcX' , 'zzzZ' ]) 139call cursor ( 1 , 1 ) 140set foldmethod = manualfoldopen += insert 141" create fold for those two lines 142norm ! Vjzf 143call feedkeys ( "$ay\<esc>" , 'tnix' ) 144call assert_equal ([ 'abcXy' , 'abcX' , 'zzzZ' ], getline ( 1 , '$' )) 145 %d 146call setline ( 1 , [ 'abcX' , 'abcX' , 'zzzZ' ]) 147call cursor ( 1 , 1 ) 148set foldmethod = manualfoldopen -= insert 149" create fold for those two lines 150norm ! Vjzf 151call feedkeys ( "$ay\<esc>" , 'tnix' ) 152call assert_equal ([ 'abcXy' , 'abcX' , 'zzzZ' ], getline ( 1 , '$' )) 153 %d 154bw ! 155endfunc 156 157func Test_edit_06 () 158" Test in diff mode 159if ! has ( "diff" ) || ! executable ( "diff" ) 160return 161endif 162new 163call setline ( 1 , [ 'abc' , 'xxx' , 'yyy' ]) 164vnew 165call setline ( 1 , [ 'abc' , 'zzz' , 'xxx' , 'yyy' ]) 166wincmd p 167diffthis 168wincmd p 169diffthis 170wincmd p 171call cursor ( 2 , 1 ) 172norm ! zt 173call feedkeys ( "Ozzz\<esc>" , 'tnix' ) 174call assert_equal ([ 'abc' , 'zzz' , 'xxx' , 'yyy' ], getline ( 1 , '$' )) 175bw ! 176bw ! 177endfunc 178 179func Test_edit_07 () 180" 1) Test with completion <c-l> when popupmenu is visible 181new 182call setline ( 1 , 'J' ) 183 184func ! ListMonths () 185call complete ( col ( '.' ) - 1 , [ 'January' , 'February' , 'March' , 186 \'April' , 'May' , 'June' , 'July' , 'August' , 'September' , 187 \'October' , 'November' , 'December' ]) 188return '' 189endfunc 190inoremap <buffer> < F5 > < C-R > =ListMonths()< CR > 191 192call feedkeys ( "A\<f5>\<c-p>" . repeat ( "\<down>" , 6 ) . "\<c-l>\<down>\<c-l>\<cr>" , 'tx' ) 193call assert_equal ([ 'July' ], getline ( 1 , '$' )) 194" 1) Test completion when InsertCharPre kicks in 195 %d 196call setline ( 1 , 'J' ) 197fu ! DoIt () 198if v: char == 'u' 199let v: char = 'an' 200endif 201endfu 202au InsertCharPre <buffer> : call DoIt () 203call feedkeys ( "A\<f5>\<c-p>u\<C-Y>\<c-l>\<cr>" , 'tx' ) 204call assert_equal ([ "Jan\<c-l>" , '' ], 1 ->getline ( '$' )) 205 %d 206call setline ( 1 , 'J' ) 207call feedkeys ( "A\<f5>\<c-p>u\<down>\<c-l>\<cr>" , 'tx' ) 208call assert_equal ([ "January" ], 1 ->getline ( '$' )) 209 210delfu ListMonths 211delfu DoIt 212iunmap <buffer> <f5> 213bw ! 214endfunc 215 216func Test_edit_08 () 217throw 'skipped: moved to test/functional/legacy/edit_spec.lua' 218" reset insertmode from i_ctrl-r_= 219let g: bufnr = bufnr ( '%' ) 220new 221call setline ( 1 , [ 'abc' ]) 222call cursor ( 1 , 4 ) 223call feedkeys ( ":set im\<cr>ZZZ\<c-r>=setbufvar(g:bufnr,'&im', 0)\<cr>" , 'tnix' ) 224call assert_equal ([ 'abZZZc' ], getline ( 1 , '$' )) 225call assert_equal ([ 0 , 1 , 1 , 0 ], getpos ( '.' )) 226call assert_false ( 0 , '&im' ) 227bw ! 228unlet g: bufnr 229endfunc 230 231func Test_edit_09 () 232" test i_CTRL-\ combinations 233new 234call setline ( 1 , [ 'abc' , 'def' , 'ghi' ]) 235call cursor ( 1 , 1 ) 236" 1) CTRL-\ CTLR-N 237" call feedkeys(":set im\<cr>\<c-\>\<c-n>ccABC\<c-l>", 'txin') 238call feedkeys ( "i\<c-\>\<c-n>ccABC\<esc>" , 'txin' ) 239call assert_equal ([ 'ABC' , 'def' , 'ghi' ], getline ( 1 , '$' )) 240call setline ( 1 , [ 'ABC' , 'def' , 'ghi' ]) 241" 2) CTRL-\ CTLR-G 242" CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is set, but 243" 'insertmode' is now removed so skip this test 244" call feedkeys("j0\<c-\>\<c-g>ZZZ\<cr>\<esc>", 'txin') 245" call assert_equal(['ABC', 'ZZZ', 'def', 'ghi'], getline(1,'$')) 246" call feedkeys("I\<c-\>\<c-g>YYY\<c-l>", 'txin') 247" call assert_equal(['ABC', 'ZZZ', 'YYYdef', 'ghi'], getline(1,'$')) 248" set noinsertmode 249" 3) CTRL-\ CTRL-O 250call setline ( 1 , [ 'ABC' , 'ZZZ' , 'def' , 'ghi' ]) 251call cursor ( 1 , 1 ) 252call feedkeys ( "A\<c-o>ix" , 'txin' ) 253call assert_equal ([ 'ABxC' , 'ZZZ' , 'def' , 'ghi' ], getline ( 1 , '$' )) 254call feedkeys ( "A\<c-\>\<c-o>ix" , 'txin' ) 255call assert_equal ([ 'ABxCx' , 'ZZZ' , 'def' , 'ghi' ], getline ( 1 , '$' )) 256" 4) CTRL-\ a (should be inserted literally, not special after <c-\> 257call setline ( 1 , [ 'ABC' , 'ZZZ' , 'def' , 'ghi' ]) 258call cursor ( 1 , 1 ) 259call feedkeys ( "A\<c-\>a" , 'txin' ) 260call assert_equal ([ "ABC\<c-\>a" , 'ZZZ' , 'def' , 'ghi' ], getline ( 1 , '$' )) 261bw ! 262endfunc 263 264func Test_edit_11 () 265" Test that indenting kicks in 266new 267set cindent 268call setline ( 1 , [ '{' , '' , '' ]) 269call cursor ( 2 , 1 ) 270call feedkeys ( "i\<c-f>int c;\<esc>" , 'tnix' ) 271call cursor ( 3 , 1 ) 272call feedkeys ( "\<Insert>/* comment */" , 'tnix' ) 273call assert_equal ([ '{' , "\<tab>int c;" , "/* comment */" ], getline ( 1 , '$' )) 274" added changed cindentkeys slightly 275set cindent cinkeys += */ 276call setline ( 1 , [ '{' , '' , '' ]) 277call cursor ( 2 , 1 ) 278call feedkeys ( "i\<c-f>int c;\<esc>" , 'tnix' ) 279call cursor ( 3 , 1 ) 280call feedkeys ( "i/* comment */" , 'tnix' ) 281call assert_equal ([ '{' , "\<tab>int c;" , "\<tab>/* comment */" ], getline ( 1 , '$' )) 282set cindent cinkeys += =end 283call feedkeys ( "oend\<cr>\<esc>" , 'tnix' ) 284call assert_equal ([ '{' , "\<tab>int c;" , "\<tab>/* comment */" , "\tend" , '' ], getline ( 1 , '$' )) 285set cinkeys -= =end 286 %d 287" Use indentexpr instead of cindenting 288func ! Do_Indent () 289if v: lnum == 3 290return 3 * shiftwidth () 291else 292return 2 * shiftwidth () 293endif 294endfunc 295setl indentexpr = Do _Indent()indentkeys += */ 296call setline ( 1 , [ '{' , '' , '' ]) 297call cursor ( 2 , 1 ) 298call feedkeys ( "i\<c-f>int c;\<esc>" , 'tnix' ) 299call cursor ( 3 , 1 ) 300call feedkeys ( "i/* comment */" , 'tnix' ) 301call assert_equal ([ '{' , "\<tab>\<tab>int c;" , "\<tab>\<tab>\<tab>/* comment */" ], getline ( 1 , '$' )) 302set cinkeys & vim indentkeys & vim 303set nocindent indentexpr = 304delfu Do_Indent 305bw ! 306endfunc 307 308func Test_edit_11_indentexpr () 309" Test that indenting kicks in 310new 311" Use indentexpr instead of cindenting 312func ! Do_Indent () 313let pline = prevnonblank ( v: lnum ) 314if empty ( getline ( v: lnum )) 315if getline ( pline ) =~ 'if\|then' 316return shiftwidth () 317else 318return 0 319endif 320else 321return 0 322endif 323endfunc 324setl indentexpr = Do _Indent()indentkeys += 0=then,0=fi 325call setline ( 1 , [ 'if [ $this ]' ]) 326call cursor ( 1 , 1 ) 327call feedkeys ( "othen\<cr>that\<cr>fi" , 'tnix' ) 328call assert_equal ([ 'if [ $this ]' , "then" , "\<tab>that" , "fi" ], getline ( 1 , '$' )) 329set cinkeys & vim indentkeys & vim 330set nocindent indentexpr = 331delfu Do_Indent 332 333" Using a script-local function 334func s: NewIndentExpr () 335endfunc 336set indentexpr = s :NewIndentExpr() 337call assert_equal ( expand ( '<SID>' ) .. 'NewIndentExpr()' , & indentexpr ) 338call assert_equal ( expand ( '<SID>' ) .. 'NewIndentExpr()' , & g: indentexpr ) 339set indentexpr = <SID>NewIndentExpr() 340call assert_equal ( expand ( '<SID>' ) .. 'NewIndentExpr()' , & indentexpr ) 341call assert_equal ( expand ( '<SID>' ) .. 'NewIndentExpr()' , & g: indentexpr ) 342setlocal indentexpr = 343setglobal indentexpr=s:NewIndentExpr() 344call assert_equal ( expand ( '<SID>' ) .. 'NewIndentExpr()' , & g: indentexpr ) 345call assert_equal ( '' , & indentexpr ) 346new 347call assert_equal ( expand ( '<SID>' ) .. 'NewIndentExpr()' , & indentexpr ) 348bw ! 349setglobal indentexpr=<SID>NewIndentExpr() 350call assert_equal ( expand ( '<SID>' ) .. 'NewIndentExpr()' , & g: indentexpr ) 351call assert_equal ( '' , & indentexpr ) 352new 353call assert_equal ( expand ( '<SID>' ) .. 'NewIndentExpr()' , & indentexpr ) 354bw ! 355set indentexpr & 356 357bw ! 358endfunc 359 360" Test changing indent in replace mode 361func Test_edit_12 () 362new 363call setline ( 1 , [ "\tabc" , "\tdef" ]) 364call cursor ( 2 , 4 ) 365call feedkeys ( "R^\<c-d>" , 'tnix' ) 366call assert_equal ([ "\tabc" , "def" ], getline ( 1 , '$' )) 367call assert_equal ([ 0 , 2 , 2 , 0 ], '.' ->getpos ()) 368 %d 369call setline ( 1 , [ "\tabc" , "\t\tdef" ]) 370call cursor ( 2 , 2 ) 371call feedkeys ( "R^\<c-d>" , 'tnix' ) 372call assert_equal ([ "\tabc" , "def" ], getline ( 1 , '$' )) 373call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( '.' )) 374 %d 375call setline ( 1 , [ "\tabc" , "\t\tdef" ]) 376call cursor ( 2 , 2 ) 377call feedkeys ( "R\<c-t>" , 'tnix' ) 378call assert_equal ([ "\tabc" , "\t\t\tdef" ], getline ( 1 , '$' )) 379call assert_equal ([ 0 , 2 , 2 , 0 ], getpos ( '.' )) 380bw ! 38110 vnew 382call setline ( 1 , [ "\tabc" , "\t\tdef" ]) 383call cursor ( 2 , 2 ) 384call feedkeys ( "R\<c-t>" , 'tnix' ) 385call assert_equal ([ "\tabc" , "\t\t\tdef" ], getline ( 1 , '$' )) 386call assert_equal ([ 0 , 2 , 2 , 0 ], getpos ( '.' )) 387 %d 388set sw = 4 389call setline ( 1 , [ "\tabc" , "\t\tdef" ]) 390call cursor ( 2 , 2 ) 391call feedkeys ( "R\<c-t>\<c-t>" , 'tnix' ) 392call assert_equal ([ "\tabc" , "\t\t\tdef" ], getline ( 1 , '$' )) 393call assert_equal ([ 0 , 2 , 2 , 0 ], getpos ( '.' )) 394 %d 395call setline ( 1 , [ "\tabc" , "\t\tdef" ]) 396call cursor ( 2 , 2 ) 397call feedkeys ( "R\<c-t>\<c-t>" , 'tnix' ) 398call assert_equal ([ "\tabc" , "\t\t\tdef" ], getline ( 1 , '$' )) 399call assert_equal ([ 0 , 2 , 2 , 0 ], getpos ( '.' )) 400set sw & 401 402" In replace mode, after hitting enter in a line with tab characters, 403" pressing backspace should restore the tab characters. 404 %d 405setlocal autoindent backspace = 2 406call setline ( 1 , "\tone\t\ttwo" ) 407exe "normal ggRred\<CR>six" .. repeat ( "\<BS>" , 8 ) 408call assert_equal ([ "\tone\t\ttwo" ], getline ( 1 , '$' )) 409bw ! 410endfunc 411 412func Test_edit_13 () 413" Test smartindenting 414new 415set smartindent autoindent 416call setline ( 1 , [ "\tabc" ]) 417call feedkeys ( "A {\<cr>more\<cr>}\<esc>" , 'tnix' ) 418call assert_equal ([ "\tabc {" , "\t\tmore" , "\t}" ], getline ( 1 , '$' )) 419set smartindent & autoindent & 420bwipe ! 421 422" Test autoindent removing indent of blank line. 423new 424call setline ( 1 , ' foo bar baz' ) 425set autoindent 426exe "normal 0eea\<CR>\<CR>\<Esc>" 427call assert_equal ( " foo bar" , getline ( 1 )) 428call assert_equal ( "" , getline ( 2 )) 429call assert_equal ( " baz" , getline ( 3 )) 430set autoindent & 431 432" pressing <C-U> to erase line should keep the indent with 'autoindent' 433set backspace = 2 autoindent 434 %d 435exe "normal i\tone\<CR>three\<C-U>two" 436call assert_equal ([ "\tone" , "\ttwo" ], getline ( 1 , '$' )) 437set backspace & autoindent & 438 439bwipe ! 440endfunc 441 442" Test for autoindent removing indent when insert mode is stopped. Some parts 443" of the code is exercised only when interactive mode is used. So use Vim in a 444" terminal. 445func Test_autoindent_remove_indent () 446CheckRunVimInTerminal 447let buf = RunVimInTerminal ( '-N Xarifile' , { 'rows' : 6 , 'cols' : 20 }) 448call TermWait ( buf ) 449call term_sendkeys ( buf , ":set autoindent\n" ) 450" leaving insert mode in a new line with indent added by autoindent, should 451" remove the indent. 452call term_sendkeys ( buf , "i\<Tab>foo\<CR>\<Esc>" ) 453" Need to delay for some time, otherwise the code in getchar.c will not be 454" exercised. 455call TermWait ( buf , 50 ) 456" when a line is wrapped and the cursor is at the start of the second line, 457" leaving insert mode, should move the cursor back to the first line. 458call term_sendkeys ( buf , "o" .. repeat ( 'x' , 20 ) .. "\<Esc>" ) 459" Need to delay for some time, otherwise the code in getchar.c will not be 460" exercised. 461call TermWait ( buf , 50 ) 462call term_sendkeys ( buf , ":w\n" ) 463call TermWait ( buf ) 464call StopVimInTerminal ( buf ) 465call assert_equal ([ "\tfoo" , '' , repeat ( 'x' , 20 )], readfile ( 'Xarifile' )) 466call delete ( 'Xarifile' ) 467endfunc 468 469" Test for autoindent removing indent when insert mode is stopped and 470" autocomplete is enabled. Some parts of the code is exercised only when 471" interactive mode is used. So use Vim in a terminal. 472func Test_autoindent_remove_indent_with_autocomplete () 473CheckRunVimInTerminal 474let buf = RunVimInTerminal ( '-N Xarifile' , { 'rows' : 6 , 'cols' : 20 }) 475call TermWait ( buf ) 476call term_sendkeys ( buf , ":set autoindent autocomplete\n" ) 477" leaving insert mode in a new line with indent added by autoindent, should 478" remove the indent. 479call term_sendkeys ( buf , "i\<Tab>foo\<CR>\<Esc>" ) 480" Need to delay for some time, otherwise the code in getchar.c will not be 481" exercised. 482call TermWait ( buf , 50 ) 483" when a line is wrapped and the cursor is at the start of the second line, 484" leaving insert mode, should move the cursor back to the first line. 485call term_sendkeys ( buf , "o" .. repeat ( 'x' , 20 ) .. "\<Esc>" ) 486" Need to delay for some time, otherwise the code in getchar.c will not be 487" exercised. 488call TermWait ( buf , 50 ) 489call term_sendkeys ( buf , ":w\n" ) 490call TermWait ( buf ) 491call StopVimInTerminal ( buf ) 492call assert_equal ([ "\tfoo" , '' , repeat ( 'x' , 20 )], readfile ( 'Xarifile' )) 493call delete ( 'Xarifile' ) 494endfunc 495 496func Test_edit_esc_after_CR_autoindent () 497new 498setlocal autoindent 499autocmd InsertLeavePre * let g: prev_cursor = getpos ( '.' ) 500 501call setline ( 1 , 'foobar' ) 502exe "normal! $hi\<CR>\<Esc>" 503call assert_equal ([ 'foob' , 'ar' ], getline ( 1 , '$' )) 504call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( '.' )) 505call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( "'^" )) 506call assert_equal ([ 0 , 2 , 1 , 0 ], g: prev_cursor ) 507 %d 508 509call setline ( 1 , 'foobar' ) 510exe "normal! $i\<CR>\<Esc>" 511call assert_equal ([ 'fooba' , 'r' ], getline ( 1 , '$' )) 512call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( '.' )) 513call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( "'^" )) 514call assert_equal ([ 0 , 2 , 1 , 0 ], g: prev_cursor ) 515 %d 516 517call setline ( 1 , 'foobar' ) 518exe "normal! A\<CR>\<Esc>" 519call assert_equal ([ 'foobar' , '' ], getline ( 1 , '$' )) 520call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( '.' )) 521call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( "'^" )) 522call assert_equal ([ 0 , 2 , 1 , 0 ], g: prev_cursor ) 523 %d 524 525call setline ( 1 , ' foobar' ) 526exe "normal! $hi\<CR>\<Esc>" 527call assert_equal ([ ' foob' , ' ar' ], getline ( 1 , '$' )) 528call assert_equal ([ 0 , 2 , 2 , 0 ], getpos ( '.' )) 529call assert_equal ([ 0 , 2 , 3 , 0 ], getpos ( "'^" )) 530call assert_equal ([ 0 , 2 , 3 , 0 ], g: prev_cursor ) 531 %d 532 533call setline ( 1 , ' foobar' ) 534exe "normal! $i\<CR>\<Esc>" 535call assert_equal ([ ' fooba' , ' r' ], getline ( 1 , '$' )) 536call assert_equal ([ 0 , 2 , 2 , 0 ], getpos ( '.' )) 537call assert_equal ([ 0 , 2 , 3 , 0 ], getpos ( "'^" )) 538call assert_equal ([ 0 , 2 , 3 , 0 ], g: prev_cursor ) 539 %d 540 541call setline ( 1 , ' foobar' ) 542exe "normal! A\<CR>\<Esc>" 543call assert_equal ([ ' foobar' , '' ], getline ( 1 , '$' )) 544call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( '.' )) 545call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( "'^" )) 546call assert_equal ([ 0 , 2 , 1 , 0 ], g: prev_cursor ) 547 %d 548 549autocmd ! InsertLeavePre 550unlet g: prev_cursor 551bwipe ! 552endfunc 553 554func Test_edit_CR () 555" Test for <CR> in insert mode 556" basically only in quickfix mode it's tested, the rest 557" has been taken care of by other tests 558CheckFeature quickfix 559botright new 560call writefile ( range ( 1 , 10 ), 'Xqflist.txt' , 'D' ) 561call setqflist ([{ 'filename' : 'Xqflist.txt' , 'lnum' : 2 }]) 562copen 563set modifiable 564call feedkeys ( "A\<cr>" , 'tnix' ) 565call assert_equal ( 'Xqflist.txt' , bufname ( '' )) 566call assert_equal ( 2 , line ( '.' )) 567cclose 568botright new 569call setloclist ( 0 , [{ 'filename' : 'Xqflist.txt' , 'lnum' : 10 }]) 570lopen 571set modifiable 572call feedkeys ( "A\<cr>" , 'tnix' ) 573call assert_equal ( 'Xqflist.txt' , bufname ( '' )) 574call assert_equal ( 10 , line ( '.' )) 575call feedkeys ( "A\<Enter>" , 'tnix' ) 576call feedkeys ( "A\<kEnter>" , 'tnix' ) 577call feedkeys ( "A\n" , 'tnix' ) 578call feedkeys ( "A\r" , 'tnix' ) 579call assert_equal ( map ( range ( 1 , 10 ), 'string(v:val)' ) + [ '' , '' , '' , '' ], getline ( 1 , '$' )) 580 581bw ! 582lclose 583endfunc 584 585func Test_edit_CTRL_ () 586CheckFeature rightleft 587" disabled for Windows builds, why? 588CheckNotMSWindows 589let _encoding =& encoding 590set encoding = utf-8 591" Test for CTRL-_ 592new 593call setline ( 1 , [ 'abc' ]) 594call cursor ( 1 , 1 ) 595call feedkeys ( "i\<c-_>xyz\<esc>" , 'tnix' ) 596call assert_equal ([ "\<C-_>xyzabc" ], getline ( 1 , '$' )) 597call assert_false ( & revins ) 598set ari 599call setline ( 1 , [ 'abc' ]) 600call cursor ( 1 , 1 ) 601call feedkeys ( "i\<c-_>xyz\<esc>" , 'tnix' ) 602" call assert_equal(["æèñabc"], getline(1, '$')) 603call assert_equal ([ "zyxabc" ], getline ( 1 , '$' )) 604call assert_true ( & revins ) 605call setline ( 1 , [ 'abc' ]) 606call cursor ( 1 , 1 ) 607call feedkeys ( "i\<c-_>xyz\<esc>" , 'tnix' ) 608call assert_equal ([ "xyzabc" ], getline ( 1 , '$' )) 609call assert_false ( & revins ) 610set noari 611let & encoding = _encoding 612bw ! 613endfunc 614 615" needs to come first, to have the @. register empty 616func Test_edit_00a_CTRL_A () 617" Test pressing CTRL-A 618new 619call setline ( 1 , repeat ([ '' ], 5 )) 620call cursor ( 1 , 1 ) 621try 622call feedkeys ( "A\<NUL>" , 'tnix' ) 623catch /^Vim\%((\a\+)\)\=:E29/ 624call assert_true ( 1 , 'E29 error caught' ) 625endtry 626call cursor ( 1 , 1 ) 627call feedkeys ( "Afoobar \<esc>" , 'tnix' ) 628call cursor ( 2 , 1 ) 629call feedkeys ( "A\<c-a>more\<esc>" , 'tnix' ) 630call cursor ( 3 , 1 ) 631call feedkeys ( "A\<NUL>and more\<esc>" , 'tnix' ) 632call assert_equal ([ 'foobar ' , 'foobar more' , 'foobar morend more' , '' , '' ], getline ( 1 , '$' )) 633bw ! 634endfunc 635 636func Test_edit_CTRL_EY () 637" Ctrl-E/ Ctrl-Y in insert mode completion to scroll 63810 new 639call setline ( 1 , range ( 1 , 100 )) 640call cursor ( 30 , 1 ) 641norm ! z. 642call feedkeys ( "A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>" , 'tnix' ) 643call assert_equal ( 30 , winsaveview ()[ 'topline' ]) 644call assert_equal ([ 0 , 30 , 2 , 0 ], getpos ( '.' )) 645call feedkeys ( "A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>" , 'tnix' ) 646call feedkeys ( "A\<c-x>" . repeat ( "\<c-y>" , 10 ), 'tnix' ) 647call assert_equal ( 21 , winsaveview ()[ 'topline' ]) 648call assert_equal ([ 0 , 30 , 2 , 0 ], getpos ( '.' )) 649bw ! 650endfunc 651 652func Test_edit_CTRL_G () 653new 654call setline ( 1 , [ 'foobar' , 'foobar' , 'foobar' ]) 655call cursor ( 2 , 4 ) 656call feedkeys ( "ioooooooo\<c-g>k\<c-r>.\<esc>" , 'tnix' ) 657call assert_equal ([ 'foooooooooobar' , 'foooooooooobar' , 'foobar' ], getline ( 1 , '$' )) 658call assert_equal ([ 0 , 1 , 11 , 0 ], getpos ( '.' )) 659call feedkeys ( "i\<c-g>k\<esc>" , 'tnix' ) 660call assert_equal ([ 0 , 1 , 10 , 0 ], getpos ( '.' )) 661call cursor ( 2 , 4 ) 662call feedkeys ( "i\<c-g>jzzzz\<esc>" , 'tnix' ) 663call assert_equal ([ 'foooooooooobar' , 'foooooooooobar' , 'foozzzzbar' ], getline ( 1 , '$' )) 664call assert_equal ([ 0 , 3 , 7 , 0 ], getpos ( '.' )) 665call feedkeys ( "i\<c-g>j\<esc>" , 'tnix' ) 666call assert_equal ([ 0 , 3 , 6 , 0 ], getpos ( '.' )) 667call assert_nobeep ( "normal! i\<c-g>\<esc>" ) 668bw ! 669endfunc 670 671func Test_edit_CTRL_I () 672" Tab in completion mode 673let path = expand ( "%:p:h" ) 674new 675call setline ( 1 , [ path . "/" , '' ]) 676call feedkeys ( "Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>" , 'tnix' ) 677call assert_match ( 'runtest\.vim' , getline ( 1 )) 678 %d 679call writefile ([ 'one' , 'two' , 'three' ], 'Xinclude.txt' , 'D' ) 680let include = '#include Xinclude.txt' 681call setline ( 1 , [ include , '' ]) 682call cursor ( 2 , 1 ) 683call feedkeys ( "A\<c-x>\<tab>\<cr>\<esc>" , 'tnix' ) 684call assert_equal ([ include , 'one' , '' ], getline ( 1 , '$' )) 685call feedkeys ( "2ggC\<c-x>\<tab>\<down>\<cr>\<esc>" , 'tnix' ) 686call assert_equal ([ include , 'two' , '' ], getline ( 1 , '$' )) 687call feedkeys ( "2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>" , 'tnix' ) 688call assert_equal ([ include , 'three' , '' ], getline ( 1 , '$' )) 689call feedkeys ( "2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>" , 'tnix' ) 690call assert_equal ([ include , '' , '' ], getline ( 1 , '$' )) 691bw ! 692endfunc 693 694func Test_edit_CTRL_K () 695" Test pressing CTRL-K (basically only dictionary completion and digraphs 696" the rest is already covered 697call writefile ([ 'A' , 'AA' , 'AAA' , 'AAAA' ], 'Xdictionary.txt' , 'D' ) 698set dictionary = Xdictionary.txt 699new 700call setline ( 1 , 'A' ) 701call cursor ( 1 , 1 ) 702call feedkeys ( "A\<c-x>\<c-k>\<cr>\<esc>" , 'tnix' ) 703call assert_equal ([ 'AA' , '' ], getline ( 1 , '$' )) 704 %d 705call setline ( 1 , 'A' ) 706call cursor ( 1 , 1 ) 707call feedkeys ( "A\<c-x>\<c-k>\<down>\<cr>\<esc>" , 'tnix' ) 708call assert_equal ([ 'AAA' ], getline ( 1 , '$' )) 709 %d 710call setline ( 1 , 'A' ) 711call cursor ( 1 , 1 ) 712call feedkeys ( "A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>" , 'tnix' ) 713call assert_equal ([ 'AAAA' ], getline ( 1 , '$' )) 714 %d 715call setline ( 1 , 'A' ) 716call cursor ( 1 , 1 ) 717call feedkeys ( "A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>" , 'tnix' ) 718call assert_equal ([ 'A' ], getline ( 1 , '$' )) 719 %d 720call setline ( 1 , 'A' ) 721call cursor ( 1 , 1 ) 722call feedkeys ( "A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>" , 'tnix' ) 723call assert_equal ([ 'AA' ], getline ( 1 , '$' )) 724 725" press an unexpected key after dictionary completion 726 %d 727call setline ( 1 , 'A' ) 728call cursor ( 1 , 1 ) 729call feedkeys ( "A\<c-x>\<c-k>\<c-]>\<cr>\<esc>" , 'tnix' ) 730call assert_equal ([ 'AA' , '' ], getline ( 1 , '$' )) 731 %d 732call setline ( 1 , 'A' ) 733call cursor ( 1 , 1 ) 734call feedkeys ( "A\<c-x>\<c-k>\<c-s>\<cr>\<esc>" , 'tnix' ) 735call assert_equal ([ "AA\<c-s>" , '' ], getline ( 1 , '$' )) 736 %d 737call setline ( 1 , 'A' ) 738call cursor ( 1 , 1 ) 739call feedkeys ( "A\<c-x>\<c-k>\<c-f>\<cr>\<esc>" , 'tnix' ) 740call assert_equal ([ "AA\<c-f>" , '' ], getline ( 1 , '$' )) 741 742set dictionary = 743 %d 744call setline ( 1 , 'A' ) 745call cursor ( 1 , 1 ) 746try 747call feedkeys ( "A\<c-x>\<c-k>\<esc>" , 'tnix' ) 748catch 749endtry 750 751call Ntest_override ( "char_avail" , 1 ) 752set showcmd 753 %d 754call feedkeys ( "A\<c-k>a:\<esc>" , 'tnix' ) 755call assert_equal ([ 'ä' ], getline ( 1 , '$' )) 756call Ntest_override ( "char_avail" , 0 ) 757set noshowcmd 758 759bw ! 760endfunc 761 762func Test_edit_CTRL_L () 763" Test Ctrl-X Ctrl-L (line completion) 764new 765set complete = . 766call setline ( 1 , [ 'one' , 'two' , 'three' , '' , '' , '' , '' ]) 767call cursor ( 4 , 1 ) 768call feedkeys ( "A\<c-x>\<c-l>\<esc>" , 'tnix' ) 769call assert_equal ([ 'one' , 'two' , 'three' , 'three' , '' , '' , '' ], getline ( 1 , '$' )) 770call feedkeys ( "cct\<c-x>\<c-l>\<c-n>\<esc>" , 'tnix' ) 771call assert_equal ([ 'one' , 'two' , 'three' , 't' , '' , '' , '' ], getline ( 1 , '$' )) 772call feedkeys ( "cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>" , 'tnix' ) 773call assert_equal ([ 'one' , 'two' , 'three' , 'two' , '' , '' , '' ], getline ( 1 , '$' )) 774call feedkeys ( "cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>" , 'tnix' ) 775call assert_equal ([ 'one' , 'two' , 'three' , 'three' , '' , '' , '' ], getline ( 1 , '$' )) 776call feedkeys ( "cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>" , 'tnix' ) 777call assert_equal ([ 'one' , 'two' , 'three' , 't' , '' , '' , '' ], getline ( 1 , '$' )) 778call feedkeys ( "cct\<c-x>\<c-l>\<c-p>\<esc>" , 'tnix' ) 779call assert_equal ([ 'one' , 'two' , 'three' , 'two' , '' , '' , '' ], getline ( 1 , '$' )) 780call feedkeys ( "cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>" , 'tnix' ) 781call assert_equal ([ 'one' , 'two' , 'three' , 't' , '' , '' , '' ], getline ( 1 , '$' )) 782call feedkeys ( "cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>" , 'tnix' ) 783call assert_equal ([ 'one' , 'two' , 'three' , 'three' , '' , '' , '' ], getline ( 1 , '$' )) 784set complete = 785call cursor ( 5 , 1 ) 786call feedkeys ( "A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>" , 'tnix' ) 787call assert_equal ([ 'one' , 'two' , 'three' , 'three' , "\<c-l>\<c-p>\<c-n>" , '' , '' ], getline ( 1 , '$' )) 788set complete & 789 %d 790if has ( "conceal" ) && has ( "syntax" ) 791call setline ( 1 , [ 'foo' , 'bar' , 'foobar' ]) 792call Ntest_override ( "char_avail" , 1 ) 793set conceallevel = 2 concealcursor = n 794syn on 795syn match ErrorMsg "^bar " 796call matchadd ( "Conceal" , 'oo' , 10 , -1 , { 'conceal' : 'X' }) 797func ! DoIt () 798let g: change = 1 799endfunc 800au ! TextChangedI <buffer> : call DoIt () 801 802call cursor ( 2 , 1 ) 803call assert_false ( exists ( "g:change" )) 804call feedkeys ( "A \<esc>" , 'tnix' ) 805call assert_equal ([ 'foo' , 'bar ' , 'foobar' ], getline ( 1 , '$' )) 806call assert_equal ( 1 , g: change ) 807 808call Ntest_override ( "char_avail" , 0 ) 809call clearmatches () 810syn off 811au ! TextChangedI 812delfu DoIt 813unlet ! g: change 814endif 815bw ! 816endfunc 817 818func Test_edit_CTRL_N () 819" Check keyword completion 820" for e in ['latin1', 'utf-8'] 821for e in [ 'utf-8' ] 822exe 'set encoding=' .. e 823new 824set complete = . 825call setline ( 1 , [ 'INFER' , 'loWER' , '' , '' , ]) 826call cursor ( 3 , 1 ) 827call feedkeys ( "Ai\<c-n>\<cr>\<esc>" , "tnix" ) 828call feedkeys ( "ILO\<c-n>\<cr>\<esc>" , 'tnix' ) 829call assert_equal ([ 'INFER' , 'loWER' , 'i' , 'LO' , '' , '' ], getline ( 1 , '$' ), e ) 830 %d 831call setline ( 1 , [ 'INFER' , 'loWER' , '' , '' , ]) 832call cursor ( 3 , 1 ) 833set ignorecase infercase 834call feedkeys ( "Ii\<c-n>\<cr>\<esc>" , "tnix" ) 835call feedkeys ( "ILO\<c-n>\<cr>\<esc>" , 'tnix' ) 836call assert_equal ([ 'INFER' , 'loWER' , 'infer' , 'LOWER' , '' , '' ], getline ( 1 , '$' ), e ) 837set noignorecase noinfercase 838 %d 839call setline ( 1 , [ 'one word' , 'two word' ]) 840exe "normal! Goo\<C-P>\<C-X>\<C-P>" 841call assert_equal ( 'one word' , getline ( 3 )) 842 %d 843set complete & 844bw ! 845endfor 846endfunc 847 848func Test_edit_CTRL_O () 849" Check for CTRL-O in insert mode 850new 851inoreabbr <buffer> h here some more 852call setline ( 1 , [ 'abc' , 'def' ]) 853call cursor ( 1 , 1 ) 854" Ctrl-O after an abbreviation 855exe "norm A h\<c-o>:set nu\<cr> text" 856call assert_equal ([ 'abc here some more text' , 'def' ], getline ( 1 , '$' )) 857call assert_true ( & nu ) 858set nonu 859iunabbr <buffer> h 860" Ctrl-O at end of line with 've'=onemore 861call cursor ( 1 , 1 ) 862call feedkeys ( "A\<c-o>:let g:a=getpos('.')\<cr>\<esc>" , 'tnix' ) 863call assert_equal ([ 0 , 1 , 23 , 0 ], g: a ) 864call cursor ( 1 , 1 ) 865set ve = onemore 866call feedkeys ( "A\<c-o>:let g:a=getpos('.')\<cr>\<esc>" , 'tnix' ) 867call assert_equal ([ 0 , 1 , 24 , 0 ], g: a ) 868set ve = 869unlet ! g: a 870bw ! 871endfunc 872 873func Test_edit_CTRL_R () 874" Insert Register 875new 876" call test_override("ALL", 1) 877set showcmd 878call feedkeys ( "AFOOBAR eins zwei\<esc>" , 'tnix' ) 879call feedkeys ( "O\<c-r>." , 'tnix' ) 880call feedkeys ( "O\<c-r>=10*500\<cr>\<esc>" , 'tnix' ) 881call feedkeys ( "O\<c-r>=getreg('=', 1)\<cr>\<esc>" , 'tnix' ) 882call assert_equal ([ "getreg('=', 1)" , '5000' , "FOOBAR eins zwei" , "FOOBAR eins zwei" ], getline ( 1 , '$' )) 883" call test_override("ALL", 0) 884set noshowcmd 885bw ! 886endfunc 887 888func Test_edit_CTRL_S () 889" Test pressing CTRL-S (basically only spellfile completion) 890" the rest is already covered 891new 892if ! has ( "spell" ) 893call setline ( 1 , 'vim' ) 894call feedkeys ( "A\<c-x>ss\<cr>\<esc>" , 'tnix' ) 895call assert_equal ([ 'vims' , '' ], getline ( 1 , '$' )) 896bw ! 897return 898endif 899call setline ( 1 , 'vim' ) 900" spell option not yet set 901try 902call feedkeys ( "A\<c-x>\<c-s>\<cr>\<esc>" , 'tnix' ) 903catch /^Vim\%((\a\+)\)\=:E756/ 904call assert_true ( 1 , 'error caught' ) 905endtry 906call assert_equal ([ 'vim' , '' ], getline ( 1 , '$' )) 907 %d 908setl spell spelllang = en 909call setline ( 1 , 'vim' ) 910call cursor ( 1 , 1 ) 911call feedkeys ( "A\<c-x>\<c-s>\<cr>\<esc>" , 'tnix' ) 912call assert_equal ([ 'Vim' , '' ], getline ( 1 , '$' )) 913 %d 914call setline ( 1 , 'vim' ) 915call cursor ( 1 , 1 ) 916call feedkeys ( "A\<c-x>\<c-s>\<down>\<cr>\<esc>" , 'tnix' ) 917call assert_equal ([ 'Aim' ], getline ( 1 , '$' )) 918 %d 919call setline ( 1 , 'vim' ) 920call cursor ( 1 , 1 ) 921call feedkeys ( "A\<c-x>\<c-s>\<c-p>\<cr>\<esc>" , 'tnix' ) 922call assert_equal ([ 'vim' , '' ], getline ( 1 , '$' )) 923 %d 924" empty buffer 925call cursor ( 1 , 1 ) 926call feedkeys ( "A\<c-x>\<c-s>\<c-p>\<cr>\<esc>" , 'tnix' ) 927call assert_equal ([ '' , '' ], getline ( 1 , '$' )) 928setl nospell 929bw ! 930endfunc 931 932func Edit_CTRL_T () 933" Check for CTRL-T and CTRL-X CTRL-T in insert mode 934" 1) increase indent 935new 936call setline ( 1 , "abc" ) 937call cursor ( 1 , 1 ) 938call feedkeys ( "A\<c-t>xyz" , 'tnix' ) 939call assert_equal ([ "\<tab>abcxyz" ], getline ( 1 , '$' )) 940" 2) also when paste option is set 941set paste 942call setline ( 1 , "abc" ) 943call cursor ( 1 , 1 ) 944call feedkeys ( "A\<c-t>xyz" , 'tnix' ) 945call assert_equal ([ "\<tab>abcxyz" ], getline ( 1 , '$' )) 946set nopaste 947" CTRL-X CTRL-T (thesaurus complete) 948call writefile ([ 'angry furious mad enraged' ], 'Xthesaurus' , 'D' ) 949set thesaurus = Xthesaurus 950call setline ( 1 , 'mad' ) 951call cursor ( 1 , 1 ) 952call feedkeys ( "A\<c-x>\<c-t>\<cr>\<esc>" , 'tnix' ) 953call assert_equal ([ 'mad' , '' ], getline ( 1 , '$' )) 954 %d 955call setline ( 1 , 'mad' ) 956call cursor ( 1 , 1 ) 957call feedkeys ( "A\<c-x>\<c-t>\<c-n>\<cr>\<esc>" , 'tnix' ) 958call assert_equal ([ 'angry' , '' ], getline ( 1 , '$' )) 959 %d 960call setline ( 1 , 'mad' ) 961call cursor ( 1 , 1 ) 962call feedkeys ( "A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>" , 'tnix' ) 963call assert_equal ([ 'furious' , '' ], getline ( 1 , '$' )) 964 %d 965call setline ( 1 , 'mad' ) 966call cursor ( 1 , 1 ) 967call feedkeys ( "A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>" , 'tnix' ) 968call assert_equal ([ 'enraged' , '' ], getline ( 1 , '$' )) 969 %d 970call setline ( 1 , 'mad' ) 971call cursor ( 1 , 1 ) 972call feedkeys ( "A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>" , 'tnix' ) 973call assert_equal ([ 'mad' , '' ], getline ( 1 , '$' )) 974 %d 975call setline ( 1 , 'mad' ) 976call cursor ( 1 , 1 ) 977call feedkeys ( "A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>" , 'tnix' ) 978call assert_equal ([ 'mad' , '' ], getline ( 1 , '$' )) 979" Using <c-p> <c-n> when 'complete' is empty 980set complete = 981 %d 982call setline ( 1 , 'mad' ) 983call cursor ( 1 , 1 ) 984call feedkeys ( "A\<c-x>\<c-t>\<c-n>\<cr>\<esc>" , 'tnix' ) 985call assert_equal ([ 'angry' , '' ], getline ( 1 , '$' )) 986 %d 987call setline ( 1 , 'mad' ) 988call cursor ( 1 , 1 ) 989call feedkeys ( "A\<c-x>\<c-t>\<c-p>\<cr>\<esc>" , 'tnix' ) 990call assert_equal ([ 'mad' , '' ], getline ( 1 , '$' )) 991set complete & 992 993set thesaurus = 994 %d 995call setline ( 1 , 'mad' ) 996call cursor ( 1 , 1 ) 997try 998call feedkeys ( "A\<c-x>\<c-t>\<esc>" , 'tnix' ) 999catch 1000endtry 1001call assert_equal ([ 'mad' ], getline ( 1 , '$' )) 1002bw ! 1003endfunc 1004 1005func Test_edit_CTRL_T () 1006call Edit_CTRL_T () 1007set completeopt += fuzzy 1008call Edit_CTRL_T () 1009set completeopt & 1010endfunc 1011 1012func Test_edit_CTRL_T_longest () 1013" CTRL-X CTRL-T (thesaurus complete) with 'longest' should not insert 1014" longest match 1015set completeopt += longest 1016new 1017call writefile ([ 'angry furious mad madder maddest' ], 'Xthesaurus' , 'D' ) 1018set thesaurus = Xthesaurus 1019call setline ( 1 , 'mad' ) 1020call cursor ( 1 , 1 ) 1021call feedkeys ( "A\<c-x>\<c-t>\<cr>\<esc>" , 'tnix' ) 1022call assert_equal ([ 'mad' , '' ], getline ( 1 , '$' )) 1023bw ! 1024set thesaurus = 1025set completeopt & 1026endfunc 1027 1028" Test thesaurus completion with different encodings 1029func Test_thesaurus_complete_with_encoding () 1030call writefile ([ 'angry furious mad enraged' ], 'Xthesaurus' , 'D' ) 1031set thesaurus = Xthesaurus 1032" for e in ['latin1', 'utf-8'] 1033for e in [ 'utf-8' ] 1034exe 'set encoding=' .. e 1035new 1036call setline ( 1 , 'mad' ) 1037call cursor ( 1 , 1 ) 1038call feedkeys ( "A\<c-x>\<c-t>\<cr>\<esc>" , 'tnix' ) 1039call assert_equal ([ 'mad' , '' ], getline ( 1 , '$' )) 1040bw ! 1041endfor 1042set thesaurus = 1043endfunc 1044 1045" Test 'thesaurusfunc' 1046func MyThesaurus ( findstart , base ) 1047let mythesaurus = [ 1048 \#{ word : "happy" , 1049 \synonyms : "cheerful,blissful,flying high,looking good,peppy" }, 1050 \#{ word : "kind" , 1051 \synonyms : "amiable,bleeding-heart,heart in right place" }] 1052if a: findstart 1053" locate the start of the word 1054let line = getline ( '.' ) 1055let start = col ( '.' ) - 1 1056while start > 0 && line [ start - 1 ] =~ '\a' 1057let start -= 1 1058endwhile 1059return start 1060else 1061" find strings matching with "a:base" 1062let res = [] 1063for w in mythesaurus 1064if w . word =~ '^' . a: base 1065call add ( res , w . word ) 1066call extend ( res , split ( w . synonyms , "," )) 1067endif 1068endfor 1069return res 1070endif 1071endfunc 1072 1073func Test_thesaurus_func () 1074new 1075set thesaurus = notused 1076set thesaurusfunc = NotUsed 1077setlocal thesaurusfunc = MyThesaurus 1078call setline ( 1 , "an ki" ) 1079call cursor ( 1 , 1 ) 1080call feedkeys ( "A\<c-x>\<c-t>\<c-n>\<cr>\<esc>" , 'tnix' ) 1081call assert_equal ([ 'an amiable' , '' ], getline ( 1 , '$' )) 1082 1083setlocal thesaurusfunc = NonExistingFunc 1084call assert_fails ( "normal $a\<C-X>\<C-T>" , 'E117:' ) 1085 1086setlocal thesaurusfunc = 1087set thesaurusfunc = NonExistingFunc 1088call assert_fails ( "normal $a\<C-X>\<C-T>" , 'E117:' ) 1089 %bw ! 1090 1091set thesaurusfunc = 1092set thesaurus = 1093endfunc 1094 1095func Test_edit_CTRL_U () 1096" Test 'completefunc' 1097new 1098" -1, -2 and -3 are special return values 1099let g: special = 0 1100fun ! CompleteMonths ( findstart , base ) 1101if a: findstart 1102" locate the start of the word 1103return g: special 1104else 1105" find months matching with "a:base" 1106let res = [] 1107for m in split ( "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" ) 1108if m =~ '^\c' . a: base 1109call add ( res , { 'word' : m , 'abbr' : m . ' Month' , 'icase' : 0 }) 1110endif 1111endfor 1112return { 'words' : res , 'refresh' : 'always' } 1113endif 1114endfun 1115set completefunc = CompleteMonths 1116call setline ( 1 , [ '' , '' ]) 1117call cursor ( 1 , 1 ) 1118call feedkeys ( "AX\<c-x>\<c-u>\<cr>\<esc>" , 'tnix' ) 1119call assert_equal ([ 'X' , '' , '' ], getline ( 1 , '$' )) 1120 %d 1121let g: special = -1 1122call feedkeys ( "AX\<c-x>\<c-u>\<cr>\<esc>" , 'tnix' ) 1123call assert_equal ([ 'XJan' , '' ], getline ( 1 , '$' )) 1124 %d 1125let g: special = -2 1126call feedkeys ( "AX\<c-x>\<c-u>\<cr>\<esc>" , 'tnix' ) 1127call assert_equal ([ 'X' , '' ], getline ( 1 , '$' )) 1128 %d 1129let g: special = -3 1130call feedkeys ( "AX\<c-x>\<c-u>\<cr>\<esc>" , 'tnix' ) 1131call assert_equal ([ 'X' , '' ], getline ( 1 , '$' )) 1132 %d 1133let g: special = 0 1134call feedkeys ( "AM\<c-x>\<c-u>\<cr>\<esc>" , 'tnix' ) 1135call assert_equal ([ 'Mar' , '' ], getline ( 1 , '$' )) 1136 %d 1137call feedkeys ( "AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>" , 'tnix' ) 1138call assert_equal ([ 'May' , '' ], getline ( 1 , '$' )) 1139 %d 1140call feedkeys ( "AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>" , 'tnix' ) 1141call assert_equal ([ 'M' , '' ], getline ( 1 , '$' )) 1142delfu CompleteMonths 1143 %d 1144try 1145call feedkeys ( "A\<c-x>\<c-u>" , 'tnix' ) 1146call assert_fails ( 1 , 'unknown completion function' ) 1147catch /^Vim\%((\a\+)\)\=:E117/ 1148call assert_true ( 1 , 'E117 error caught' ) 1149endtry 1150set completefunc = 1151bw ! 1152endfunc 1153 1154func Test_edit_completefunc_delete () 1155func CompleteFunc ( findstart , base ) 1156if a: findstart == 1 1157return col ( '.' ) - 1 1158endif 1159normal dd 1160return [ 'a' , 'b' ] 1161endfunc 1162new 1163set completefunc = CompleteFunc 1164call setline ( 1 , [ '' , 'abcd' , '' ]) 11652 d 1166call assert_fails ( "normal 2G$a\<C-X>\<C-U>" , 'E565:' ) 1167bwipe ! 1168endfunc 1169 1170func Test_edit_CTRL_Z () 1171" Ctrl-Z when insertmode is not set inserts it literally 1172new 1173call setline ( 1 , 'abc' ) 1174call feedkeys ( "A\<c-z>\<esc>" , 'tnix' ) 1175call assert_equal ([ "abc\<c-z>" ], getline ( 1 , '$' )) 1176bw ! 1177" TODO: How to Test Ctrl-Z in insert mode, e.g. suspend? 1178endfunc 1179 1180func Test_edit_DROP () 1181if ! has ( "dnd" ) 1182return 1183endif 1184new 1185call setline ( 1 , [ 'abc def ghi' ]) 1186call cursor ( 1 , 1 ) 1187try 1188call feedkeys ( "i\<Drop>\<Esc>" , 'tnix' ) 1189call assert_fails ( 1 , 'Invalid register name' ) 1190catch /^Vim\%((\a\+)\)\=:E353/ 1191call assert_true ( 1 , 'error caught' ) 1192endtry 1193bw ! 1194endfunc 1195 1196func Test_edit_CTRL_V () 1197new 1198call setline ( 1 , [ 'abc' ]) 1199call cursor ( 2 , 1 ) 1200 1201" force some redraws 1202set showmode showcmd 1203call Ntest_override ( 'char_avail' , 1 ) 1204 1205call feedkeys ( "A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>" , 'tnix' ) 1206call assert_equal ([ "abc\x0e\x0c\x02" ], getline ( 1 , '$' )) 1207 1208if has ( "rightleft" ) && exists ( "+rl" ) 1209set rl 1210call setline ( 1 , [ 'abc' ]) 1211call cursor ( 2 , 1 ) 1212call feedkeys ( "A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>" , 'tnix' ) 1213call assert_equal ([ "abc\x0e\x0c\x02" ], getline ( 1 , '$' )) 1214set norl 1215endif 1216 1217set noshowmode showcmd 1218call Ntest_override ( 'char_avail' , 0 ) 1219 1220" No modifiers should be applied to the char typed using i_CTRL-V_digit. 1221call feedkeys ( ":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>" , 'tnix' ) 1222call assert_equal ( 'LcL<C-F2>πjπ<M-F3>' , getline ( 2 )) 1223 1224if has ( 'osx' ) 1225" A char with a modifier should not be a valid char for i_CTRL-V_digit. 1226call feedkeys ( "o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>" , 'tnix' ) 1227call assert_equal ( '<D-j><D-1><D-o><D-x><D-u>' , getline ( 3 )) 1228endif 1229 1230bw ! 1231endfunc 1232 1233func Test_edit_F1 () 1234CheckFeature quickfix 1235 1236" Pressing <f1> 1237new 1238" call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix') 1239call feedkeys ( "i\<f1>\<esc>" , 'tnix' ) 1240set noinsertmode 1241call assert_equal ( 'help' , & buftype ) 1242bw 1243bw 1244endfunc 1245 1246func Test_edit_F21 () 1247" Pressing <f21> 1248" sends a netbeans command 1249if has ( "netbeans_intg" ) 1250new 1251" I have no idea what this is supposed to do :) 1252call feedkeys ( "A\<F21>\<F1>\<esc>" , 'tnix' ) 1253bw 1254endif 1255endfunc 1256 1257func Test_edit_HOME_END () 1258" Test Home/End Keys 1259new 1260set foldopen += hor 1261call setline ( 1 , [ 'abc' , 'def' ]) 1262call cursor ( 1 , 1 ) 1263call feedkeys ( "AX\<Home>Y\<esc>" , 'tnix' ) 1264call cursor ( 2 , 1 ) 1265call feedkeys ( "iZ\<End>Y\<esc>" , 'tnix' ) 1266call assert_equal ([ 'YabcX' , 'ZdefY' ], getline ( 1 , '$' )) 1267 1268set foldopen -= hor 1269bw ! 1270endfunc 1271 1272func Test_edit_INS () 1273" Test for Pressing <Insert> 1274new 1275call setline ( 1 , [ 'abc' , 'def' ]) 1276call cursor ( 1 , 1 ) 1277call feedkeys ( "i\<Insert>ZYX>" , 'tnix' ) 1278call assert_equal ([ 'ZYX>' , 'def' ], getline ( 1 , '$' )) 1279call setline ( 1 , [ 'abc' , 'def' ]) 1280call cursor ( 1 , 1 ) 1281call feedkeys ( "i\<Insert>Z\<Insert>YX>" , 'tnix' ) 1282call assert_equal ([ 'ZYX>bc' , 'def' ], getline ( 1 , '$' )) 1283bw ! 1284endfunc 1285 1286func Test_edit_LEFT_RIGHT () 1287" Left, Shift-Left, Right, Shift-Right 1288new 1289call setline ( 1 , [ 'abc def ghi' , 'ABC DEF GHI' , 'ZZZ YYY XXX' ]) 1290let _ww =& ww 1291set ww = 1292call cursor ( 2 , 1 ) 1293call feedkeys ( "i\<left>\<esc>" , 'tnix' ) 1294call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( '.' )) 1295" Is this a bug, <s-left> does not respect whichwrap option 1296call feedkeys ( "i\<s-left>\<esc>" , 'tnix' ) 1297call assert_equal ([ 0 , 1 , 8 , 0 ], getpos ( '.' )) 1298call feedkeys ( "i" . repeat ( "\<s-left>" , 3 ) . "\<esc>" , 'tnix' ) 1299call assert_equal ([ 0 , 1 , 1 , 0 ], getpos ( '.' )) 1300call feedkeys ( "i\<right>\<esc>" , 'tnix' ) 1301call assert_equal ([ 0 , 1 , 1 , 0 ], getpos ( '.' )) 1302call feedkeys ( "i\<right>\<right>\<esc>" , 'tnix' ) 1303call assert_equal ([ 0 , 1 , 2 , 0 ], getpos ( '.' )) 1304call feedkeys ( "A\<right>\<esc>" , 'tnix' ) 1305call assert_equal ([ 0 , 1 , 11 , 0 ], getpos ( '.' )) 1306call feedkeys ( "A\<s-right>\<esc>" , 'tnix' ) 1307call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( '.' )) 1308call feedkeys ( "i\<s-right>\<esc>" , 'tnix' ) 1309call assert_equal ([ 0 , 2 , 4 , 0 ], getpos ( '.' )) 1310call cursor ( 3 , 11 ) 1311call feedkeys ( "A\<right>\<esc>" , 'tnix' ) 1312call feedkeys ( "A\<s-right>\<esc>" , 'tnix' ) 1313call assert_equal ([ 0 , 3 , 11 , 0 ], getpos ( '.' )) 1314call cursor ( 2 , 11 ) 1315" <S-Right> does not respect 'whichwrap' option 1316call feedkeys ( "A\<s-right>\<esc>" , 'tnix' ) 1317call assert_equal ([ 0 , 3 , 1 , 0 ], getpos ( '.' )) 1318" Check motion when 'whichwrap' contains cursor keys for insert mode 1319set ww += [,] 1320call cursor ( 2 , 1 ) 1321call feedkeys ( "i\<left>\<esc>" , 'tnix' ) 1322call assert_equal ([ 0 , 1 , 11 , 0 ], getpos ( '.' )) 1323call cursor ( 2 , 11 ) 1324call feedkeys ( "A\<right>\<esc>" , 'tnix' ) 1325call assert_equal ([ 0 , 3 , 1 , 0 ], getpos ( '.' )) 1326call cursor ( 2 , 11 ) 1327call feedkeys ( "A\<s-right>\<esc>" , 'tnix' ) 1328call assert_equal ([ 0 , 3 , 1 , 0 ], getpos ( '.' )) 1329let & ww = _ww 1330bw ! 1331endfunc 1332 1333func Test_edit_MOUSE () 1334" This is a simple test, since we're not really using the mouse here 1335CheckFeature mouse 133610 new 1337call setline ( 1 , range ( 1 , 100 )) 1338call cursor ( 1 , 1 ) 1339call assert_equal ( 1 , line ( 'w0' )) 1340call assert_equal ( 10 , line ( 'w$' )) 1341set mouse = a 1342" One scroll event moves three lines. 1343call feedkeys ( "A\<ScrollWheelDown>\<esc>" , 'tnix' ) 1344call assert_equal ( 4 , line ( 'w0' )) 1345call assert_equal ( 13 , line ( 'w$' )) 1346" This should move by one page down. 1347call feedkeys ( "A\<S-ScrollWheelDown>\<esc>" , 'tnix' ) 1348call assert_equal ( 14 , line ( 'w0' )) 1349set nostartofline 1350" Another page down. 1351call feedkeys ( "A\<C-ScrollWheelDown>\<esc>" , 'tnix' ) 1352call assert_equal ( 24 , line ( 'w0' )) 1353 1354call assert_equal ([ 0 , 24 , 2 , 0 ], getpos ( '.' )) 1355call Ntest_setmouse ( 4 , 3 ) 1356call feedkeys ( "A\<LeftMouse>\<esc>" , 'tnix' ) 1357call assert_equal ([ 0 , 27 , 2 , 0 ], getpos ( '.' )) 1358set mousemodel = extend 1359call Ntest_setmouse ( 5 , 3 ) 1360call feedkeys ( "A\<RightMouse>\<esc>\<esc>" , 'tnix' ) 1361call assert_equal ([ 0 , 28 , 2 , 0 ], getpos ( '.' )) 1362set mousemodel & 1363call cursor ( 1 , 100 ) 1364norm ! zt 1365" this should move by a screen up, but when the test 1366" is run, it moves up to the top of the buffer... 1367call feedkeys ( "A\<ScrollWheelUp>\<esc>" , 'tnix' ) 1368call assert_equal ([ 0 , 1 , 1 , 0 ], getpos ( '.' )) 1369call cursor ( 1 , 30 ) 1370norm ! zt 1371call feedkeys ( "A\<S-ScrollWheelUp>\<esc>" , 'tnix' ) 1372call assert_equal ([ 0 , 1 , 1 , 0 ], getpos ( '.' )) 1373call cursor ( 1 , 30 ) 1374norm ! zt 1375call feedkeys ( "A\<C-ScrollWheelUp>\<esc>" , 'tnix' ) 1376call assert_equal ([ 0 , 1 , 1 , 0 ], getpos ( '.' )) 1377 %d 1378call setline ( 1 , repeat ([ "12345678901234567890" ], 100 )) 1379call cursor ( 2 , 1 ) 1380call feedkeys ( "A\<ScrollWheelRight>\<esc>" , 'tnix' ) 1381call assert_equal ([ 0 , 2 , 20 , 0 ], getpos ( '.' )) 1382call feedkeys ( "A\<ScrollWheelLeft>\<esc>" , 'tnix' ) 1383call assert_equal ([ 0 , 2 , 20 , 0 ], getpos ( '.' )) 1384call feedkeys ( "A\<S-ScrollWheelRight>\<esc>" , 'tnix' ) 1385call assert_equal ([ 0 , 2 , 20 , 0 ], getpos ( '.' )) 1386call feedkeys ( "A\<S-ScrollWheelLeft>\<esc>" , 'tnix' ) 1387call assert_equal ([ 0 , 2 , 20 , 0 ], getpos ( '.' )) 1388call feedkeys ( "A\<C-ScrollWheelRight>\<esc>" , 'tnix' ) 1389call assert_equal ([ 0 , 2 , 20 , 0 ], getpos ( '.' )) 1390call feedkeys ( "A\<C-ScrollWheelLeft>\<esc>" , 'tnix' ) 1391call assert_equal ([ 0 , 2 , 20 , 0 ], getpos ( '.' )) 1392set mouse & startofline 1393bw ! 1394endfunc 1395 1396func Test_edit_PAGEUP_PAGEDOWN () 139710 new 1398call setline ( 1 , repeat ([ 'abc def ghi' ], 30 )) 1399call cursor ( 1 , 1 ) 1400call assert_nobeep ( 'call feedkeys("i\<PageDown>\<esc>", "tnix")' ) 1401call assert_equal ([ 0 , 9 , 1 , 0 ], getpos ( '.' )) 1402call assert_nobeep ( 'call feedkeys("i\<PageDown>\<esc>", "tnix")' ) 1403call assert_equal ([ 0 , 17 , 1 , 0 ], getpos ( '.' )) 1404call assert_nobeep ( 'call feedkeys("i\<PageDown>\<esc>", "tnix")' ) 1405call assert_equal ([ 0 , 25 , 1 , 0 ], getpos ( '.' )) 1406call assert_nobeep ( 'call feedkeys("i\<PageDown>\<esc>", "tnix")' ) 1407call assert_equal ([ 0 , 30 , 1 , 0 ], getpos ( '.' )) 1408call assert_beeps ( 'call feedkeys("i\<PageDown>\<esc>", "tnix")' ) 1409call assert_equal ([ 0 , 30 , 1 , 0 ], getpos ( '.' )) 1410call assert_nobeep ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1411call assert_equal ([ 0 , 29 , 1 , 0 ], getpos ( '.' )) 1412call assert_nobeep ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1413call assert_equal ([ 0 , 21 , 1 , 0 ], getpos ( '.' )) 1414call assert_nobeep ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1415call assert_equal ([ 0 , 13 , 1 , 0 ], getpos ( '.' )) 1416call assert_nobeep ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1417call assert_equal ([ 0 , 10 , 1 , 0 ], getpos ( '.' )) 1418call assert_beeps ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1419call assert_equal ([ 0 , 10 , 11 , 0 ], getpos ( '.' )) 1420" <S-Up> is the same as <PageUp> 1421" <S-Down> is the same as <PageDown> 1422call cursor ( 1 , 1 ) 1423call assert_nobeep ( 'call feedkeys("i\<S-Down>\<esc>", "tnix")' ) 1424call assert_equal ([ 0 , 9 , 1 , 0 ], getpos ( '.' )) 1425call assert_nobeep ( 'call feedkeys("i\<S-Down>\<esc>", "tnix")' ) 1426call assert_equal ([ 0 , 17 , 1 , 0 ], getpos ( '.' )) 1427call assert_nobeep ( 'call feedkeys("i\<S-Down>\<esc>", "tnix")' ) 1428call assert_equal ([ 0 , 25 , 1 , 0 ], getpos ( '.' )) 1429call assert_nobeep ( 'call feedkeys("i\<S-Down>\<esc>", "tnix")' ) 1430call assert_equal ([ 0 , 30 , 1 , 0 ], getpos ( '.' )) 1431call assert_beeps ( 'call feedkeys("i\<S-Down>\<esc>", "tnix")' ) 1432call assert_equal ([ 0 , 30 , 1 , 0 ], getpos ( '.' )) 1433call assert_nobeep ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1434call assert_equal ([ 0 , 29 , 1 , 0 ], getpos ( '.' )) 1435call assert_nobeep ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1436call assert_equal ([ 0 , 21 , 1 , 0 ], getpos ( '.' )) 1437call assert_nobeep ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1438call assert_equal ([ 0 , 13 , 1 , 0 ], getpos ( '.' )) 1439call assert_nobeep ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1440call assert_equal ([ 0 , 10 , 1 , 0 ], getpos ( '.' )) 1441call assert_beeps ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1442call assert_equal ([ 0 , 10 , 11 , 0 ], getpos ( '.' )) 1443set nostartofline 1444call cursor ( 30 , 11 ) 1445norm ! zt 1446call assert_nobeep ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1447call assert_equal ([ 0 , 29 , 11 , 0 ], getpos ( '.' )) 1448call assert_nobeep ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1449call assert_equal ([ 0 , 21 , 11 , 0 ], getpos ( '.' )) 1450call assert_nobeep ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1451call assert_equal ([ 0 , 13 , 11 , 0 ], getpos ( '.' )) 1452call assert_nobeep ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1453call assert_equal ([ 0 , 10 , 11 , 0 ], getpos ( '.' )) 1454call assert_beeps ( 'call feedkeys("A\<PageUp>\<esc>", "tnix")' ) 1455call assert_equal ([ 0 , 10 , 11 , 0 ], getpos ( '.' )) 1456call cursor ( 1 , 1 ) 1457call assert_nobeep ( 'call feedkeys("A\<PageDown>\<esc>", "tnix")' ) 1458call assert_equal ([ 0 , 9 , 11 , 0 ], getpos ( '.' )) 1459call assert_nobeep ( 'call feedkeys("A\<PageDown>\<esc>", "tnix")' ) 1460call assert_equal ([ 0 , 17 , 11 , 0 ], getpos ( '.' )) 1461call assert_nobeep ( 'call feedkeys("A\<PageDown>\<esc>", "tnix")' ) 1462call assert_equal ([ 0 , 25 , 11 , 0 ], getpos ( '.' )) 1463call assert_nobeep ( 'call feedkeys("A\<PageDown>\<esc>", "tnix")' ) 1464call assert_equal ([ 0 , 30 , 11 , 0 ], getpos ( '.' )) 1465call assert_beeps ( 'call feedkeys("A\<PageDown>\<esc>", "tnix")' ) 1466call assert_equal ([ 0 , 30 , 11 , 0 ], getpos ( '.' )) 1467" <S-Up> is the same as <PageUp> 1468" <S-Down> is the same as <PageDown> 1469call cursor ( 30 , 11 ) 1470norm ! zt 1471call assert_nobeep ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1472call assert_equal ([ 0 , 29 , 11 , 0 ], getpos ( '.' )) 1473call assert_nobeep ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1474call assert_equal ([ 0 , 21 , 11 , 0 ], getpos ( '.' )) 1475call assert_nobeep ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1476call assert_equal ([ 0 , 13 , 11 , 0 ], getpos ( '.' )) 1477call assert_nobeep ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1478call assert_equal ([ 0 , 10 , 11 , 0 ], getpos ( '.' )) 1479call assert_beeps ( 'call feedkeys("A\<S-Up>\<esc>", "tnix")' ) 1480call assert_equal ([ 0 , 10 , 11 , 0 ], getpos ( '.' )) 1481call cursor ( 1 , 1 ) 1482call assert_nobeep ( 'call feedkeys("A\<S-Down>\<esc>", "tnix")' ) 1483call assert_equal ([ 0 , 9 , 11 , 0 ], getpos ( '.' )) 1484call assert_nobeep ( 'call feedkeys("A\<S-Down>\<esc>", "tnix")' ) 1485call assert_equal ([ 0 , 17 , 11 , 0 ], getpos ( '.' )) 1486call assert_nobeep ( 'call feedkeys("A\<S-Down>\<esc>", "tnix")' ) 1487call assert_equal ([ 0 , 25 , 11 , 0 ], getpos ( '.' )) 1488call assert_nobeep ( 'call feedkeys("A\<S-Down>\<esc>", "tnix")' ) 1489call assert_equal ([ 0 , 30 , 11 , 0 ], getpos ( '.' )) 1490call assert_beeps ( 'call feedkeys("A\<S-Down>\<esc>", "tnix")' ) 1491call assert_equal ([ 0 , 30 , 11 , 0 ], getpos ( '.' )) 1492bw ! 1493endfunc 1494 1495func Test_edit_forbidden () 1496new 1497" 1) edit in the sandbox is not allowed 1498call setline ( 1 , 'a' ) 1499com ! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix') 1500call assert_fails ( ':Sandbox' , 'E48:' ) 1501com ! Sandbox :sandbox exe "norm! i\<del>" 1502call assert_fails ( ':Sandbox' , 'E48:' ) 1503delcom Sandbox 1504call assert_equal ([ 'a' ], getline ( 1 , '$' )) 1505 1506" 2) edit with textlock set 1507fu ! DoIt () 1508call feedkeys ( "i\<del>\<esc>" , 'tnix' ) 1509endfu 1510au InsertCharPre <buffer> : call DoIt () 1511try 1512call feedkeys ( "ix\<esc>" , 'tnix' ) 1513call assert_fails ( 1 , 'textlock' ) 1514catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here 1515endtry 1516" TODO: Might be a bug: should x really be inserted here 1517call assert_equal ([ 'xa' ], getline ( 1 , '$' )) 1518delfu DoIt 1519try 1520call feedkeys ( "ix\<esc>" , 'tnix' ) 1521call assert_fails ( 1 , 'unknown function' ) 1522catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function 1523endtry 1524au ! InsertCharPre 1525 1526" 3) edit when completion is shown 1527fun ! Complete ( findstart , base ) 1528if a: findstart 1529return col ( '.' ) 1530else 1531call feedkeys ( "i\<del>\<esc>" , 'tnix' ) 1532return [] 1533endif 1534endfun 1535set completefunc = Complete 1536try 1537call feedkeys ( "i\<c-x>\<c-u>\<esc>" , 'tnix' ) 1538call assert_fails ( 1 , 'change in complete function' ) 1539catch /^Vim\%((\a\+)\)\=:E565/ " catch E565 1540endtry 1541delfu Complete 1542set completefunc = 1543 1544if has ( "rightleft" ) && exists ( "+fkmap" ) 1545" 4) 'R' when 'fkmap' and 'revins' is set. 1546set revins fkmap 1547try 1548normal Ri 1549call assert_fails ( 1 , "R with 'fkmap' and 'ri' set" ) 1550catch 1551finally 1552set norevins nofkmap 1553endtry 1554endif 1555bw ! 1556endfunc 1557 1558func Test_edit_rightleft () 1559" Cursor in rightleft mode moves differently 1560CheckFeature rightleft 1561call NewWindow ( 10 , 20 ) 1562call setline ( 1 , [ 'abc' , 'def' , 'ghi' ]) 1563call cursor ( 1 , 2 ) 1564set rightleft 1565" Screen looks as expected 1566let lines = ScreenLines ([ 1 , 4 ], winwidth ( 0 )) 1567let expect = [ 1568 \" cba" , 1569 \" fed" , 1570 \" ihg" , 1571 \" ~" ] 1572call assert_equal ( join ( expect , "\n" ), join ( lines , "\n" )) 1573" 2) right moves to the left 1574call feedkeys ( "i\<right>\<esc>x" , 'txin' ) 1575call assert_equal ([ 'bc' , 'def' , 'ghi' ], getline ( 1 , '$' )) 1576call cursor ( 1 , 2 ) 1577call feedkeys ( "i\<s-right>\<esc>" , 'txin' ) 1578call cursor ( 1 , 2 ) 1579call feedkeys ( "i\<c-right>\<esc>" , 'txin' ) 1580" Screen looks as expected 1581let lines = ScreenLines ([ 1 , 4 ], winwidth ( 0 )) 1582let expect = [ 1583 \" cb" , 1584 \" fed" , 1585 \" ihg" , 1586 \" ~" ] 1587call assert_equal ( join ( expect , "\n" ), join ( lines , "\n" )) 1588" 2) left moves to the right 1589call setline ( 1 , [ 'abc' , 'def' , 'ghi' ]) 1590call cursor ( 1 , 2 ) 1591call feedkeys ( "i\<left>\<esc>x" , 'txin' ) 1592call assert_equal ([ 'ac' , 'def' , 'ghi' ], getline ( 1 , '$' )) 1593call cursor ( 1 , 2 ) 1594call feedkeys ( "i\<s-left>\<esc>" , 'txin' ) 1595call cursor ( 1 , 2 ) 1596call feedkeys ( "i\<c-left>\<esc>" , 'txin' ) 1597" Screen looks as expected 1598let lines = ScreenLines ([ 1 , 4 ], winwidth ( 0 )) 1599let expect = [ 1600 \" ca" , 1601 \" fed" , 1602 \" ihg" , 1603 \" ~" ] 1604call assert_equal ( join ( expect , "\n" ), join ( lines , "\n" )) 1605 %d _ 1606" call test_override('redraw_flag', 1) 1607" call test_override('char_avail', 1) 1608call feedkeys ( "a\<C-V>x41" , "xt" ) 1609redraw ! 1610call assert_equal ( repeat ( ' ' , 19 ) .. 'A' , Screenline ( 1 )) 1611" call test_override('ALL', 0) 1612set norightleft 1613bw ! 1614endfunc 1615 1616func Test_edit_complete_very_long_name () 1617" Long directory names only work on Unix. 1618CheckUnix 1619 1620let dirname = getcwd () . "/Xlongdir" 1621let longdirname = dirname . repeat ( '/' . repeat ( 'd' , 255 ), 4 ) 1622try 1623call mkdir ( longdirname , 'pR' ) 1624catch /E739:/ 1625" Long directory name probably not supported. 1626call delete ( dirname , 'rf' ) 1627return 1628endtry 1629 1630" Try to get the Vim window position before setting 'columns', so that we can 1631" move the window back to where it was. 1632let winposx = getwinposx () 1633let winposy = getwinposy () 1634 1635if winposx >= 0 && winposy >= 0 && ! has ( 'gui_running' ) 1636" We did get the window position, but xterm may report the wrong numbers. 1637" Move the window to the reported position and compute any offset. 1638exe 'winpos ' . winposx . ' ' . winposy 1639sleep 100m 1640let x = getwinposx () 1641if x >= 0 1642let winposx += winposx - x 1643endif 1644let y = getwinposy () 1645if y >= 0 1646let winposy += winposy - y 1647endif 1648endif 1649 1650let save_columns = & columns 1651" Need at least about 1100 columns to reproduce the problem. 1652set columns = 2000 1653set noswapfile 1654 1655let longfilename = longdirname . '/' . repeat ( 'a' , 255 ) 1656call writefile ([ 'Totum' , 'Table' ], longfilename ) 1657new 1658exe "next Xnofile " . longfilename 1659exe "normal iT\<C-N>" 1660 1661bwipe ! 1662exe 'bwipe! ' . longfilename 1663let & columns = save_columns 1664if winposx >= 0 && winposy >= 0 1665exe 'winpos ' . winposx . ' ' . winposy 1666endif 1667set swapfile & 1668endfunc 1669 1670func Test_edit_backtick () 1671next a\`b c 1672call assert_equal ( 'a`b' , expand ( '%' )) 1673next 1674call assert_equal ( 'c' , expand ( '%' )) 1675call assert_equal ( 'a\`b c' , expand ( '##' )) 1676endfunc 1677 1678func Test_edit_quit () 1679edit foo.txt 1680split 1681new 1682call setline ( 1 , 'hello' ) 16833 wincmd w 1684redraw ! 1685call assert_fails ( '1q' , 'E37:' ) 1686bwipe ! foo.txt 1687only 1688endfunc 1689 1690func Test_edit_alt () 1691" Keeping the cursor line didn't happen when the first line has indent. 1692new 1693call setline ( 1 , [ ' one' , 'two' , 'three' ]) 1694w XAltFile 1695$ 1696call assert_equal ( 3 , line ( '.' )) 1697e Xother 1698e # 1699call assert_equal ( 3 , line ( '.' )) 1700 1701bwipe XAltFile 1702call delete ( 'XAltFile' ) 1703endfunc 1704 1705func Test_edit_InsertLeave () 1706new 1707au InsertLeavePre * let g: did_au_pre = 1 1708au InsertLeave * let g: did_au = 1 1709let g: did_au_pre = 0 1710let g: did_au = 0 1711call feedkeys ( "afoo\<Esc>" , 'tx' ) 1712call assert_equal ( 1 , g: did_au_pre ) 1713call assert_equal ( 1 , g: did_au ) 1714call assert_equal ( 'foo' , getline ( 1 )) 1715 1716let g: did_au_pre = 0 1717let g: did_au = 0 1718call feedkeys ( "Sbar\<C-C>" , 'tx' ) 1719call assert_equal ( 1 , g: did_au_pre ) 1720call assert_equal ( 0 , g: did_au ) 1721call assert_equal ( 'bar' , getline ( 1 )) 1722 1723inoremap x xx< Esc > 1724let g: did_au_pre = 0 1725let g: did_au = 0 1726call feedkeys ( "Saax" , 'tx' ) 1727call assert_equal ( 1 , g: did_au_pre ) 1728call assert_equal ( 1 , g: did_au ) 1729call assert_equal ( 'aaxx' , getline ( 1 )) 1730 1731inoremap x xx< C-C > 1732let g: did_au_pre = 0 1733let g: did_au = 0 1734call feedkeys ( "Sbbx" , 'tx' ) 1735call assert_equal ( 1 , g: did_au_pre ) 1736call assert_equal ( 0 , g: did_au ) 1737call assert_equal ( 'bbxx' , getline ( 1 )) 1738 1739bwipe ! 1740au ! InsertLeave InsertLeavePre 1741iunmap x 1742endfunc 1743 1744func Test_edit_InsertLeave_undo () 1745new XtestUndo 1746set undofile 1747au InsertLeave * wall 1748exe "normal ofoo\<Esc>" 1749call assert_equal ( 2 , line ( '$' )) 1750normal u 1751call assert_equal ( 1 , line ( '$' )) 1752 1753bwipe ! 1754au ! InsertLeave 1755call delete ( 'XtestUndo' ) 1756call delete ( undofile ( 'XtestUndo' )) 1757set undofile & 1758endfunc 1759 1760" Test for inserting characters using CTRL-V followed by a number. 1761func Test_edit_special_chars () 1762new 1763 1764let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>" 1765 1766exe "normal " . t 1767call assert_equal ( "ABC !a\<C-O>g\<C-G>8" , getline ( 2 )) 1768 1769bw ! 1770endfunc 1771 1772func Test_edit_startinsert () 1773new 1774set backspace += start 1775call setline ( 1 , 'foobar' ) 1776call feedkeys ( "A\<C-U>\<Esc>" , 'xt' ) 1777call assert_equal ( '' , getline ( 1 )) 1778 1779call setline ( 1 , 'foobar' ) 1780call feedkeys ( ":startinsert!\<CR>\<C-U>\<Esc>" , 'xt' ) 1781call assert_equal ( '' , getline ( 1 )) 1782 1783call setline ( 1 , 'foobar' ) 1784setl nomodifiable 1785call assert_fails ( 'startinsert' , 'E21:' ) 1786 1787call cursor ( 1 , 1 ) 1788call assert_fails ( 'startinsert!' , 'E21:' ) 1789call assert_equal ( 1 , col ( '.' )) 1790 1791set backspace & 1792bwipe ! 1793endfunc 1794 1795" ":startinsert" is ineffective in a terminal window: it must not give an 1796" error and with "!" it must not move the cursor. 1797func Test_edit_startinsert_in_terminal () 1798CheckFeature terminal 1799 1800let buf = Run_shell_in_terminal ({}) 1801 1802" Fill the terminal with text. 1803if has ( 'win32' ) 1804call feedkeys ( "dir\<CR>" , 'xt' ) 1805else 1806call feedkeys ( "ls\<CR>" , 'xt' ) 1807endif 1808call WaitForAssert ({ ->assert_notequal ( '' , term_getline ( buf , 1 ))}) 1809 1810" Go to Terminal-Normal mode and put the cursor on a line with text. 1811call feedkeys ( "\<C-W>N" , 'xt' ) 1812call assert_notequal ( 0 , search ( '\S' , 'w' )) 1813call cursor ( line ( '.' ), 1 ) 1814 1815startinsert 1816startinsert ! 1817call assert_equal ( 1 , col ( '.' )) 1818 1819" Clear "restart_edit" in case the commands were not ignored. 1820stopinsert 1821 1822call feedkeys ( "i" , 'xt' ) 1823call StopShellInTerminal ( buf ) 1824bwipe ! 1825endfunc 1826 1827" Backspacing in Virtual Replace mode over a wide character that replaced 1828" several narrower ones must not eat the padding that follows. 1829func Test_edit_vreplace_bs_multibyte () 1830new 1831call setline ( 1 , 'aé xyz' ) 1832call feedkeys ( "gR\u4e00\<BS>\e" , 'xt' ) 1833call assert_equal ( 'aé xyz' , getline ( 1 )) 1834 1835call setline ( 1 , 'a€ xyz' ) 1836call feedkeys ( "gR\u4e00\<BS>\e" , 'xt' ) 1837call assert_equal ( 'a€ xyz' , getline ( 1 )) 1838bwipe ! 1839endfunc 1840 1841" Test for :startreplace and :startgreplace 1842func Test_edit_startreplace () 1843new 1844call setline ( 1 , 'abc' ) 1845call feedkeys ( "l:startreplace\<CR>xyz\e" , 'xt' ) 1846call assert_equal ( 'axyz' , getline ( 1 )) 1847call feedkeys ( "0:startreplace!\<CR>abc\e" , 'xt' ) 1848call assert_equal ( 'axyzabc' , getline ( 1 )) 1849call setline ( 1 , "a\tb" ) 1850call feedkeys ( "0l:startgreplace\<CR>xyz\e" , 'xt' ) 1851call assert_equal ( "axyz\tb" , getline ( 1 )) 1852call feedkeys ( "0i\<C-R>=execute('startreplace')\<CR>12\e" , 'xt' ) 1853call assert_equal ( "12axyz\tb" , getline ( 1 )) 1854 1855call setline ( 1 , 'abc' ) 1856setl nomodifiable 1857call assert_fails ( 'startreplace' , 'E21:' ) 1858call assert_fails ( 'startgreplace' , 'E21:' ) 1859 1860call cursor ( 1 , 1 ) 1861call assert_fails ( 'startreplace!' , 'E21:' ) 1862call assert_equal ( 1 , col ( '.' )) 1863call cursor ( 1 , 1 ) 1864call assert_fails ( 'startgreplace!' , 'E21:' ) 1865call assert_equal ( 1 , col ( '.' )) 1866 1867bw ! 1868endfunc 1869 1870func Test_edit_noesckeys () 1871CheckNotGui 1872new 1873 1874" <Left> moves cursor when 'esckeys' is set 1875exe "set t_kl=\<Esc>OD" 1876" set esckeys 1877call feedkeys ( "axyz\<Esc>ODX" , "xt" ) 1878" call assert_equal("xyXz", getline(1)) 1879 1880" <Left> exits Insert mode when 'esckeys' is off 1881" set noesckeys 1882call setline ( 1 , '' ) 1883call feedkeys ( "axyz\<Esc>ODX" , "xt" ) 1884call assert_equal ([ "DX" , "xyz" ], getline ( 1 , 2 )) 1885 1886bwipe ! 1887" set esckeys 1888endfunc 1889 1890" Test for running an invalid ex command in insert mode using CTRL-O 1891func Test_edit_ctrl_o_invalid_cmd () 1892new 1893set showmode showcmd 1894" Avoid a sleep of 3 seconds. Zero might have side effects. 1895" call test_override('ui_delay', 50) 1896let caught_e492 = 0 1897try 1898call feedkeys ( "i\<C-O>:invalid\<CR>abc\<Esc>" , "xt" ) 1899catch /E492:/ 1900let caught_e492 = 1 1901endtry 1902call assert_equal ( 1 , caught_e492 ) 1903call assert_equal ( 'abc' , getline ( 1 )) 1904set showmode & showcmd & 1905" call test_override('ui_delay', 0) 1906bw ! 1907endfunc 1908 1909" Test for editing a file with a very long name 1910func Test_edit_illegal_filename () 1911CheckEnglish 1912new 1913redir => msg 1914exe 'edit ' . repeat ( 'f' , 5000 ) 1915redir END 1916call assert_match ( "Illegal file name$" , split ( msg , "\n" )[ 0 ]) 1917close ! 1918endfunc 1919 1920" Test for editing a directory 1921func Test_edit_is_a_directory () 1922CheckEnglish 1923let dirname = getcwd () . "/Xeditdir" 1924call mkdir ( dirname , 'p' ) 1925 1926new 1927redir => msg 1928exe 'edit' dirname 1929redir END 1930call assert_match ( "is a directory$" , split ( msg , "\n" )[ 0 ]) 1931bwipe ! 1932 1933let dirname .= '/' 1934 1935new 1936redir => msg 1937exe 'edit' dirname 1938redir END 1939call assert_match ( "is a directory$" , split ( msg , "\n" )[ 0 ]) 1940bwipe ! 1941 1942call delete ( dirname , 'rf' ) 1943endfunc 1944 1945" Test for editing a file using invalid file encoding 1946func Test_edit_invalid_encoding () 1947CheckEnglish 1948call writefile ([], 'Xinvfile' , 'D' ) 1949redir => msg 1950new ++enc=axbyc Xinvfile 1951redir END 1952call assert_match ( '\[NOT converted\]' , msg ) 1953close ! 1954endfunc 1955 1956" Test for the "charconvert" option 1957func Test_edit_charconvert () 1958CheckEnglish 1959call writefile ([ 'one' , 'two' ], 'Xccfile' , 'D' ) 1960 1961" set 'charconvert' to a non-existing function 1962set charconvert = NonExitingFunc () 1963new 1964let caught_e117 = v: false 1965try 1966redir => msg 1967edit ++enc= axbycXccfile 1968catch /E117:/ 1969let caught_e117 = v: true 1970finally 1971redir END 1972endtry 1973call assert_true ( caught_e117 ) 1974call assert_equal ([ 'one' , 'two' ], getline ( 1 , '$' )) 1975call assert_match ( "Conversion with 'charconvert' failed" , msg ) 1976close ! 1977set charconvert & 1978 1979" 'charconvert' function doesn't create an output file 1980func Cconv1 () 1981endfunc 1982set charconvert = Cconv1 () 1983new 1984redir => msg 1985edit ++enc= axbycXccfile 1986redir END 1987call assert_equal ([ 'one' , 'two' ], getline ( 1 , '$' )) 1988call assert_match ( "can't read output of 'charconvert'" , msg ) 1989close ! 1990delfunc Cconv1 1991set charconvert & 1992 1993" 'charconvert' function to convert to upper case 1994func Cconv2 () 1995let data = readfile ( v: fname_in ) 1996call map ( data , 'toupper(v:val)' ) 1997call writefile ( data , v: fname_out ) 1998endfunc 1999set charconvert = Cconv2 () 2000new Xccfile 2001write ++enc=ucase Xccfile1 2002call assert_equal ([ 'ONE' , 'TWO' ], readfile ( 'Xccfile1' )) 2003call delete ( 'Xccfile1' ) 2004close ! 2005delfunc Cconv2 2006set charconvert & 2007 2008" 'charconvert' function removes the input file 2009func Cconv3 () 2010call delete ( v: fname_in ) 2011endfunc 2012set charconvert = Cconv3 () 2013new 2014call assert_fails ( 'edit ++enc=lcase Xccfile' , 'E202:' ) 2015call assert_equal ([ '' ], getline ( 1 , '$' )) 2016close ! 2017delfunc Cconv3 2018set charconvert & 2019endfunc 2020 2021" Test for editing a file without read permission 2022func Test_edit_file_no_read_perm () 2023CheckUnix 2024CheckNotRoot 2025 2026call writefile ([ 'one' , 'two' ], 'Xnrpfile' , 'D' ) 2027call setfperm ( 'Xnrpfile' , '-w-------' ) 2028new 2029redir => msg 2030edit Xnrpfile 2031redir END 2032call assert_equal ( 1 , & readonly ) 2033call assert_equal ([ '' ], getline ( 1 , '$' )) 2034call assert_match ( '\[Permission Denied\]' , msg ) 2035close ! 2036endfunc 2037 2038" Using :edit without leaving 'insertmode' should not cause Insert mode to be 2039" re-entered immediately after <C-L> 2040func Test_edit_insertmode_ex_edit () 2041CheckRunVimInTerminal 2042 2043let lines =<< trim END 2044set insertmode noruler 2045inoremap <C-B> <Cmd>edit Xfoo<CR> 2046END 2047call writefile ( lines , 'Xtest_edit_insertmode_ex_edit' , 'D' ) 2048 2049let buf = RunVimInTerminal ( '-S Xtest_edit_insertmode_ex_edit' , #{ rows : 6 , wait_for_ruler : 0 }) 2050" Somehow when using valgrind "INSERT" does not show up unless we send 2051" something to the terminal. 2052for i in range ( 30 ) 2053if term_getline ( buf , 6 ) =~ 'INSERT' 2054break 2055endif 2056call term_sendkeys ( buf , "-" ) 2057sleep 100m 2058endfor 2059call WaitForAssert ({ ->assert_match ( '^-- INSERT --\s*$' , term_getline ( buf , 6 ))}) 2060call term_sendkeys ( buf , "\<C-B>\<C-L>" ) 2061call WaitForAssert ({ ->assert_notmatch ( '^-- INSERT --\s*$' , term_getline ( buf , 6 ))}) 2062 2063" clean up 2064call StopVimInTerminal ( buf ) 2065endfunc 2066 2067" Pressing escape in 'insertmode' should beep 2068" FIXME: Execute this later, when using valgrind it makes the next test 2069" Test_edit_insertmode_ex_edit() fail. 2070func Test_z_edit_insertmode_esc_beeps () 2071new 2072" set insertmode 2073" call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')") 2074set insertmode & 2075" unsupported "CTRL-G l" command should beep in insert mode. 2076call assert_beeps ( "normal i\<C-G>l" ) 2077bwipe ! 2078endfunc 2079 2080" Test for 'hkmap' and 'hkmapp' 2081func Test_edit_hkmap () 2082throw "Skipped: Nvim does not support 'hkmap'" 2083CheckFeature rightleft 2084if has ( 'win32' ) && ! has ( 'gui' ) 2085" Test fails on the MS-Windows terminal version 2086return 2087endif 2088new 2089 2090set revins hkmap 2091let str = 'abcdefghijklmnopqrstuvwxyz' 2092let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 2093let str ..= '`/'',.;' 2094call feedkeys ( 'i' .. str , 'xt' ) 2095let expected = "óõú,.;" 2096let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA" 2097let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù" 2098call assert_equal ( expected , getline ( 1 )) 2099 2100 %d 2101set revins hkmap hkmapp 2102let str = 'abcdefghijklmnopqrstuvwxyz' 2103let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 2104call feedkeys ( 'i' .. str , 'xt' ) 2105let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA" 2106let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà" 2107call assert_equal ( expected , getline ( 1 )) 2108 2109set revins & hkmap & hkmapp & 2110bw ! 2111endfunc 2112 2113" Test for 'allowrevins' and using CTRL-_ in insert mode 2114func Test_edit_allowrevins () 2115CheckFeature rightleft 2116new 2117set allowrevins 2118call feedkeys ( "iABC\<C-_>DEF\<C-_>GHI" , 'xt' ) 2119call assert_equal ( 'ABCFEDGHI' , getline ( 1 )) 2120set allowrevins & 2121bw ! 2122endfunc 2123 2124" Test for inserting a register in insert mode using CTRL-R 2125func Test_edit_insert_reg () 2126throw 'Skipped: use test/functional/legacy/edit_spec.lua' 2127new 2128let g: Line = '' 2129func SaveFirstLine () 2130let g: Line = Screenline ( 1 ) 2131return 'r' 2132endfunc 2133inoremap <expr> <buffer> < F2 > SaveFirstLine () 2134call test_override ( 'redraw_flag' , 1 ) 2135call test_override ( 'char_avail' , 1 ) 2136let @r= 'sample' 2137call feedkeys ( "a\<C-R>=SaveFirstLine()\<CR>" , "xt" ) 2138call assert_equal ( '"' , g: Line ) 2139 2140" Test for inserting an null and an empty list 2141call feedkeys ( "a\<C-R>=test_null_list()\<CR>" , "xt" ) 2142call feedkeys ( "a\<C-R>=[]\<CR>" , "xt" ) 2143call assert_equal ([ 'r' ], getbufline ( '' , 1 , '$' )) 2144call test_override ( 'ALL' , 0 ) 2145bw ! 2146endfunc 2147 2148" Test for positioning cursor after CTRL-R expression failed 2149func Test_edit_ctrl_r_failed () 2150CheckScreendump 2151CheckRunVimInTerminal 2152 2153let buf = RunVimInTerminal ( '' , #{ rows : 6 , cols : 60 }) 2154 2155" trying to insert a blob produces an error 2156call term_sendkeys ( buf , "i\<C-R>=0z\<CR>" ) 2157call WaitForAssert ({ ->assert_match ( '^E976:' , term_getline ( buf , 5 ))}, 1000 ) 2158 2159" ending Insert mode should put the cursor back on the ':' 2160call term_sendkeys ( buf , ":\<Esc>" ) 2161call VerifyScreenDump ( buf , 'Test_edit_ctlr_r_failed_1' , {}) 2162 2163call StopVimInTerminal ( buf ) 2164endfunc 2165 2166" When a character is inserted at the last position of the last line in a 2167" window, the window contents should be scrolled one line up. If the top line 2168" is part of a fold, then the entire fold should be scrolled up. 2169func Test_edit_lastline_scroll () 2170if has ( 'linux' ) 2171" TODO: For unknown reasons, this test fails on CI when run in Gui mode 2172CheckNotGui 2173endif 2174new 2175let h = winheight ( 0 ) 2176let lines = [ 'one' , 'two' , 'three' ] 2177let lines += repeat ([ 'vim' ], h - 4 ) 2178call setline ( 1 , lines ) 2179call setline ( h , repeat ( 'x' , winwidth ( 0 ) - 1 )) 2180call feedkeys ( "GAx" , 'xt' ) 2181redraw ! 2182call assert_equal ( h - 1 , winline ()) 2183call assert_equal ( 2 , line ( 'w0' )) 2184 2185" scroll with a fold 21861 , 2 fold 2187normal gg 2188call setline ( h + 1 , repeat ( 'x' , winwidth ( 0 ) - 1 )) 2189call feedkeys ( "GAx" , 'xt' ) 2190redraw ! 2191call assert_equal ( h - 1 , winline ()) 2192call assert_equal ( 3 , line ( 'w0' )) 2193 2194bw ! 2195endfunc 2196 2197func Test_edit_browse () 2198" in the GUI this opens a file picker, we only test the terminal behavior 2199CheckNotGui 2200 2201" ":browse xxx" checks for the FileExplorer augroup and assumes editing "." 2202" works then. 2203augroup FileExplorer 2204au ! 2205augroup END 2206 2207" When the CASE_INSENSITIVE_FILENAME is defined this used to cause a crash. 2208 browse enew 2209bwipe ! 2210 2211 browse split 2212bwipe ! 2213endfunc 2214 2215func Test_read_invalid () 2216" set encoding=latin1 2217" This was not properly checking for going past the end. 2218call assert_fails ( 'r`=' , 'E484:' ) 2219set encoding = utf-8 2220endfunc 2221 2222" Test for the 'revins' option 2223func Test_edit_revins () 2224CheckFeature rightleft 2225new 2226set revins 2227exe "normal! ione\ttwo three" 2228call assert_equal ( "eerht owt\teno" , getline ( 1 )) 2229call setline ( 1 , "one\ttwo three" ) 2230normal ! gg$bi a 2231call assert_equal ( "one\ttwo a three" , getline ( 1 )) 2232exe "normal! $bi\<BS>\<BS>" 2233call assert_equal ( "one\ttwo a ree" , getline ( 1 )) 2234exe "normal! 0wi\<C-W>" 2235call assert_equal ( "one\t a ree" , getline ( 1 )) 2236exe "normal! 0wi\<C-U>" 2237call assert_equal ( "one\t " , getline ( 1 )) 2238" newline in insert mode starts at the end of the line 2239call setline ( 1 , 'one two three' ) 2240exe "normal! wi\nfour" 2241call assert_equal ([ 'one two three' , 'ruof' ], getline ( 1 , '$' )) 2242set backspace = indent,eol,start 2243exe "normal! ggA\<BS>:" 2244call assert_equal ([ 'one two three:ruof' ], getline ( 1 , '$' )) 2245set revins & backspace & 2246bw ! 2247endfunc 2248 2249" Test for getting the character of the line below after "p" 2250func Test_edit_put_CTRL_E () 2251" set encoding=latin1 2252new 2253let @"= '' 2254sil ! norm orggRx 2255sil ! norm pr 2256call assert_equal ([ 'r' , 'r' ], getline ( 1 , 2 )) 2257bwipe ! 2258set encoding = utf-8 2259endfunc 2260 2261" Test toggling of input method. See :help i_CTRL-^ 2262func Test_edit_CTRL_hat () 2263CheckFeature xim 2264 2265" FIXME: test fails with Motif GUI. 2266" test also fails when running in the GUI. 2267CheckFeature gui_gtk 2268CheckNotGui 2269 2270new 2271 2272call assert_equal ( 0 , & iminsert ) 2273call feedkeys ( "i\<C-^>" , 'xt' ) 2274call assert_equal ( 2 , & iminsert ) 2275call feedkeys ( "i\<C-^>" , 'xt' ) 2276call assert_equal ( 0 , & iminsert ) 2277 2278bwipe ! 2279endfunc 2280 2281" Weird long file name was going over the end of NameBuff 2282func Test_edit_overlong_file_name () 2283CheckUnix 2284 2285file 0000000000000000000000000000 2286file %%%%%%%%%%%%%%%%%%%%%%%%%% 2287file %%%%%% 2288set readonly 2289set ls = 2 2290 2291redraw ! 2292set noreadonly ls & 2293bwipe ! 2294endfunc 2295 2296func Test_edit_shift_bs () 2297CheckMSWindows 2298 2299" FIXME: this works interactively, but the test fails 2300throw 'Skipped: Shift-Backspace Test not working correctly :(' 2301 2302" Need to run this in Win32 Terminal, do not use CheckRunVimInTerminal 2303if ! has ( "terminal" ) 2304return 2305endif 2306 2307" Shift Backspace should work like Backspace in insert mode 2308let lines =<< trim END 2309call setline(1, ['abc']) 2310END 2311call writefile ( lines , 'Xtest_edit_shift_bs' , 'D' ) 2312 2313let buf = RunVimInTerminal ( '-S Xtest_edit_shift_bs' , #{ rows : 3 }) 2314call term_sendkeys ( buf , "A\<S-BS>-\<esc>" ) 2315call TermWait ( buf , 50 ) 2316call assert_equal ( 'ab-' , term_getline ( buf , 1 )) 2317 2318" clean up 2319call StopVimInTerminal ( buf ) 2320endfunc 2321 2322func Test_edit_Ctrl_RSB () 2323new 2324let g: triggered = [] 2325autocmd InsertCharPre <buffer> let g: triggered += [ v: char ] 2326 2327" i_CTRL-] should not trigger InsertCharPre 2328exe "normal! A\<C-]>" 2329call assert_equal ([], g: triggered ) 2330 2331" i_CTRL-] should expand abbreviations but not trigger InsertCharPre 2332inoreabbr <buffer> f foo 2333exe "normal! Af\<C-]>a" 2334call assert_equal ([ 'f' , 'f' , 'o' , 'o' , 'a' ], g: triggered ) 2335call assert_equal ( 'fooa' , getline ( 1 )) 2336 2337" CTRL-] followed by i_CTRL-V should not expand abbreviations 2338" i_CTRL-V doesn't trigger InsertCharPre 2339call setline ( 1 , '' ) 2340exe "normal! Af\<C-V>\<C-]>" 2341call assert_equal ( "f\<C-]>" , getline ( 1 )) 2342 2343let g: triggered = [] 2344call setline ( 1 , '' ) 2345 2346" Also test assigning to v:char 2347autocmd InsertCharPre <buffer> let v: char = 'f' 2348exe "normal! Ag\<C-]>h" 2349call assert_equal ([ 'g' , 'f' , 'o' , 'o' , 'h' ], g: triggered ) 2350call assert_equal ( 'ffff' , getline ( 1 )) 2351 2352autocmd ! InsertCharPre 2353unlet g: triggered 2354bwipe ! 2355endfunc 2356 2357func s: check_backspace ( expected ) 2358let g: actual = [] 2359inoremap <buffer> < F2 > <Cmd> let g: actual += [ getline ( '.' )] <CR> 2360set backspace = indent,eol,start 2361 2362exe "normal i" .. repeat ( "\<BS>\<F2>" , len ( a: expected )) 2363call assert_equal ( a: expected , g: actual ) 2364 2365set backspace & 2366iunmap <buffer> <F2> 2367unlet g: actual 2368endfunc 2369 2370" Test that backspace works with 'smarttab' and mixed Tabs and spaces. 2371func Test_edit_backspace_smarttab_mixed () 2372set smarttab 2373call NewWindow ( 1 , 30 ) 2374setlocal tabstop = 4 shiftwidth = 4 2375 2376call setline ( 1 , "\t \t \t a" ) 2377normal ! $ 2378call s: check_backspace ([ 2379 \"\t \t \ta" , 2380 \"\t \t a" , 2381 \"\t \t a" , 2382 \"\t \ta" , 2383 \"\t a" , 2384 \"\ta" , 2385 \"a" , 2386 \]) 2387 2388call CloseWindow () 2389set smarttab & 2390endfunc 2391 2392" Test that backspace works with 'smarttab' and 'varsofttabstop'. 2393func Test_edit_backspace_smarttab_varsofttabstop () 2394CheckFeature vartabs 2395 2396set smarttab 2397call NewWindow ( 1 , 30 ) 2398setlocal tabstop = 8 varsofttabstop = 6,2,5,3 2399 2400call setline ( 1 , "a\t \t a" ) 2401normal ! $ 2402call s: check_backspace ([ 2403 \"a\t \ta" , 2404 \"a\t a" , 2405 \"a\ta" , 2406 \"a a" , 2407 \"aa" , 2408 \"a" , 2409 \]) 2410 2411call CloseWindow () 2412set smarttab & 2413endfunc 2414 2415" Test that backspace works with 'smarttab' when a Tab is shown as "^I". 2416func Test_edit_backspace_smarttab_list () 2417set smarttab 2418call NewWindow ( 1 , 30 ) 2419setlocal tabstop = 4 shiftwidth = 4 list listchars = 2420 2421call setline ( 1 , "\t \t \t a" ) 2422normal ! $ 2423call s: check_backspace ([ 2424 \"\t \t a" , 2425 \"\t \t a" , 2426 \"\t \ta" , 2427 \"\t a" , 2428 \"a" , 2429 \]) 2430 2431call CloseWindow () 2432set smarttab & 2433endfunc 2434 2435" Test that backspace works with 'smarttab' and 'breakindent'. 2436func Test_edit_backspace_smarttab_breakindent () 2437CheckFeature linebreak 2438 2439set smarttab 2440call NewWindow ( 3 , 17 ) 2441setlocal tabstop = 4 shiftwidth = 4 breakindent breakindentopt = min:5 2442 2443call setline ( 1 , "\t \t \t a" ) 2444normal ! $ 2445call s: check_backspace ([ 2446 \"\t \t \ta" , 2447 \"\t \t a" , 2448 \"\t \t a" , 2449 \"\t \ta" , 2450 \"\t a" , 2451 \"\ta" , 2452 \"a" , 2453 \]) 2454 2455call CloseWindow () 2456set smarttab & 2457endfunc 2458 2459" Test that backspace works with 'smarttab' and virtual text. 2460func Test_edit_backspace_smarttab_virtual_text () 2461CheckFeature textprop 2462 2463set smarttab 2464call NewWindow ( 1 , 50 ) 2465setlocal tabstop = 4 shiftwidth = 4 2466 2467call setline ( 1 , "\t \t \t a" ) 2468call prop_type_add ( 'theprop' , {}) 2469call prop_add ( 1 , 3 , { 'type' : 'theprop' , 'text' : 'text' }) 2470normal ! $ 2471call s: check_backspace ([ 2472 \"\t \t \ta" , 2473 \"\t \t a" , 2474 \"\t \t a" , 2475 \"\t \ta" , 2476 \"\t a" , 2477 \"\ta" , 2478 \"a" , 2479 \]) 2480 2481call CloseWindow () 2482call prop_type_delete ( 'theprop' ) 2483set smarttab & 2484endfunc 2485 2486func Test_edit_CAR_with_completion () 2487new 2488 2489" With "noselect", behavior is the same with and without "noinsert": 2490" Enter inserts a new line when no selection is done or after selecting with 2491" Ctrl-N/P, but does not insert a new line when selecting with cursor keys. 2492for cot in [ 'menu,menuone,noselect' , 'menu,menuone,noselect,noinsert' ] 2493let & cot = cot 2494 %delete 2495call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>e\<CR>" , 'tx' ) 2496call assert_equal ([ 'hello hero' , 'he' , '' ], getline ( 1 , '$' )) 2497 %delete 2498call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<CR>" , 'tx' ) 2499call assert_equal ([ 'hello hero' , 'h' , '' ], getline ( 1 , '$' )) 2500 %delete 2501call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<C-N>\<CR>" , 'tx' ) 2502call assert_equal ([ 'hello hero' , 'hello' , '' ], getline ( 1 , '$' )) 2503 %delete 2504call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<C-N>\<C-N>\<CR>" , 'tx' ) 2505call assert_equal ([ 'hello hero' , 'hero' , '' ], getline ( 1 , '$' )) 2506 %delete 2507call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<C-N>\<C-P>\<CR>" , 'tx' ) 2508call assert_equal ([ 'hello hero' , 'h' , '' ], getline ( 1 , '$' )) 2509 %delete 2510call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<Down>\<CR>" , 'tx' ) 2511call assert_equal ([ 'hello hero' , 'hello' ], getline ( 1 , '$' )) 2512 %delete 2513call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<Down>\<Down>\<CR>" , 'tx' ) 2514call assert_equal ([ 'hello hero' , 'hero' ], getline ( 1 , '$' )) 2515 %delete 2516call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<Down>\<Up>\<CR>" , 'tx' ) 2517call assert_equal ([ 'hello hero' , 'h' ], getline ( 1 , '$' )) 2518endfor 2519 2520" With "noinsert" but not "noselect": like pressing <Down> after "noselect". 2521set cot = menu,menuone,noinsert 2522 %delete 2523call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>e\<CR>" , 'tx' ) 2524call assert_equal ([ 'hello hero' , 'hello' ], getline ( 1 , '$' )) 2525 %delete 2526call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<CR>" , 'tx' ) 2527call assert_equal ([ 'hello hero' , 'hello' ], getline ( 1 , '$' )) 2528 %delete 2529call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<Down>\<CR>" , 'tx' ) 2530call assert_equal ([ 'hello hero' , 'hero' ], getline ( 1 , '$' )) 2531 %delete 2532call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<Up>\<CR>" , 'tx' ) 2533call assert_equal ([ 'hello hero' , 'h' ], getline ( 1 , '$' )) 2534 %delete 2535call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<C-N>\<CR>" , 'tx' ) 2536call assert_equal ([ 'hello hero' , 'hero' , '' ], getline ( 1 , '$' )) 2537 %delete 2538call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>\<C-P>\<CR>" , 'tx' ) 2539call assert_equal ([ 'hello hero' , 'h' , '' ], getline ( 1 , '$' )) 2540 2541set cot & 2542bw ! 2543endfunc 2544 2545func Test_autoindent_no_strip_after_cmd_setline () 2546new 2547setlocal autoindent 2548inoremap <buffer> < F2 > <Cmd> call setline ( '.' , 'v v' ) <CR> < Cmd>call cursor(line('.'),2)<CR> 2549call feedkeys ( "Go\<F2>\<Esc>" , 'tx' ) 2550call assert_equal ( 'v v' , getline ( 2 )) 2551bwipe ! 2552endfunc 2553 2554func Test_autoindent_no_strip_after_cursorholdi () 2555CheckFeature timers 2556new 2557setlocal autoindent 2558set updatetime = 50 2559au CursorHoldI <buffer> call setline ( '.' , 'v v' ) 2560call setline ( 1 , ' x' ) 2561call cursor ( 1 , 2 ) 2562call timer_start ( 120 , { ->feedkeys ( "\<Esc>" , 't' )}) 2563call feedkeys ( "o" , 'tx!' ) 2564call assert_equal ( 'v v' , getline ( 2 )) 2565set updatetime & 2566bwipe ! 2567endfunc 2568 2569" Issue #20130: '[ must mark the start of the paste after CTRL-R CTRL-P + edit. 2570func Test_open_square_mark_after_ctrl_r_ctrl_p_paste () 2571new 2572call setline ( 1 , [ 'a' , 'b' , 'c' , 'd' ]) 2573call cursor ( 4 , 1 ) 2574 2575call feedkeys ( "Vggyjo\<C-r>\<C-p>\"\<BS>\<Esc>" , 'xt' ) 2576 2577call assert_equal ([ 'a' , 'b' , 'a' , 'b' , 'c' , 'd' , 'c' , 'd' ], 2578 \getline ( 1 , '$' )) 2579call assert_equal ([ 0 , 3 , 1 , 0 ], getpos ( "'[" )) 2580bwipe ! 2581endfunc 2582 2583func Test_autoindent_no_strip_cross_line () 2584new 2585setlocal autoindent 2586inoremap <buffer> < F3 > {}< Left > <CR >< Cmd>normal! ==< CR > <Up > <End > <CR > 2587 2588call setline ( 1 , '' ) 2589call feedkeys ( "i\<F3>\<Esc>" , 'tx' ) 2590 2591call assert_equal ( '{' , getline ( 1 )) 2592call assert_equal ( '' , getline ( 2 )) 2593call assert_equal ( '}' , getline ( 3 )) 2594call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( '.' )) 2595 2596" Overwrite @. register with simple content to avoid affecting later tests. 2597call feedkeys ( "Go\<Esc>" , 'tnix' ) 2598bwipe ! 2599endfunc 2600 2601" vim: shiftwidth=2 sts=2 expandtab