robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
205a1ce4c7
master
1" Test editing line in Ex mode (see :help Q and :help gQ). 2 3source check.vim 4source shared.vim 5 6" Helper function to test editing line in Q Ex mode 7func Ex_Q ( cmd ) 8" Is there a simpler way to test editing Ex line? 9call feedkeys ( "Q" 10 \.. "let s:test_ex =<< END\<CR>" 11 \.. a: cmd .. "\<CR>" 12 \.. "END\<CR>" 13 \.. "visual\<CR>" , 'tx' ) 14return s: test_ex [ 0 ] 15endfunc 16 17" Helper function to test editing line in gQ Ex mode 18func Ex_gQ ( cmd ) 19call feedkeys ( "gQ" .. a: cmd .. "\<C-b>\"\<CR>" , 'tx' ) 20let ret = @:[ 1 :] " Remove leading quote. 21call feedkeys ( "visual\<CR>" , 'tx' ) 22return ret 23endfunc 24 25" Helper function to test editing line with both Q and gQ Ex mode. 26func Ex ( cmd ) 27return [ Ex_Q ( a: cmd ), Ex_gQ ( a: cmd )] 28endfunc 29 30" Test editing line in Ex mode (both Q and gQ) 31func Test_ex_mode () 32throw 'Skipped: Nvim only supports Vim Ex mode' 33let encoding_save = & encoding 34set sw = 2 35 36for e in [ 'utf8' , 'latin1' ] 37exe 'set encoding=' . e 38 39call assert_equal ([ 'bar' , 'bar' ], Ex ( "foo bar\<C-u>bar" ), e ) 40call assert_equal ([ "1\<C-u>2" , "1\<C-u>2" ], Ex ( "1\<C-v>\<C-u>2" ), e ) 41call assert_equal ([ "1\<C-b>2\<C-e>3" , '213' ], Ex ( "1\<C-b>2\<C-e>3" ), e ) 42call assert_equal ([ '0123' , '2013' ], Ex ( "01\<Home>2\<End>3" ), e ) 43call assert_equal ([ '0123' , '0213' ], Ex ( "01\<Left>2\<Right>3" ), e ) 44call assert_equal ([ '01234' , '0342' ], Ex ( "012\<Left>\<Left>\<Insert>3\<Insert>4" ), e ) 45call assert_equal ([ "foo bar\<C-w>" , 'foo ' ], Ex ( "foo bar\<C-w>" ), e ) 46call assert_equal ([ 'foo' , 'foo' ], Ex ( "fooba\<Del>\<Del>" ), e ) 47call assert_equal ([ "foo\tbar" , 'foobar' ], Ex ( "foo\<Tab>bar" ), e ) 48call assert_equal ([ "abbrev\t" , 'abbreviate' ], Ex ( "abbrev\<Tab>" ), e ) 49call assert_equal ([ ' 1' , "1\<C-t>\<C-t>" ], Ex ( "1\<C-t>\<C-t>" ), e ) 50call assert_equal ([ ' 1' , "1\<C-t>\<C-t>" ], Ex ( "1\<C-t>\<C-t>\<C-d>" ), e ) 51call assert_equal ([ ' foo' , ' foo' ], Ex ( " foo\<C-d>" ), e ) 52call assert_equal ([ 'foo' , ' foo0' ], Ex ( " foo0\<C-d>" ), e ) 53call assert_equal ([ 'foo' , ' foo^' ], Ex ( " foo^\<C-d>" ), e ) 54call assert_equal ([ 'foo' , 'foo' ], 55 \Ex ( "\<BS>\<C-H>\<Del>\<kDel>foo" ), e ) 56" default wildchar <Tab> interferes with this test 57set wildchar = <c-e> 58call assert_equal ([ "a\tb" , "a\tb" ], Ex ( "a\t\t\<C-H>b" ), e ) 59call assert_equal ([ "\t mn" , "\tm\<C-T>n" ], Ex ( "\tm\<C-T>n" ), e ) 60set wildchar & 61endfor 62 63set sw & 64let & encoding = encoding_save 65endfunc 66 67" Test substitute confirmation prompt :%s/pat/str/c in Ex mode 68func Test_Ex_substitute () 69CheckRunVimInTerminal 70let buf = RunVimInTerminal ( '' , { 'rows' : 6 }) 71 72call term_sendkeys ( buf , ":call setline(1, repeat(['foo foo'], 4))\<CR>" ) 73call term_sendkeys ( buf , ":set number\<CR>" ) 74call term_sendkeys ( buf , "gQ" ) 75call WaitForAssert ({ ->assert_match ( ':' , term_getline ( buf , 6 ))}, 1000 ) 76 77call term_sendkeys ( buf , "%s/foo/bar/gc\<CR>" ) 78call WaitForAssert ({ ->assert_match ( ' 1 foo foo' , term_getline ( buf , 5 ))}, 79 \1000 ) 80call WaitForAssert ({ ->assert_match ( ' ^^^' , term_getline ( buf , 6 ))}, 1000 ) 81call term_sendkeys ( buf , "N\<CR>" ) 82call term_wait ( buf ) 83call WaitForAssert ({ ->assert_match ( ' ^^^' , term_getline ( buf , 6 ))}, 1000 ) 84call term_sendkeys ( buf , "n\<CR>" ) 85call WaitForAssert ({ ->assert_match ( ' ^^^' , term_getline ( buf , 6 ))}, 86 \1000 ) 87call term_sendkeys ( buf , "y\<CR>" ) 88 89call term_sendkeys ( buf , "q\<CR>" ) 90call WaitForAssert ({ ->assert_match ( ':' , term_getline ( buf , 6 ))}, 1000 ) 91 92" Pressing enter in ex mode should print the current line 93call term_sendkeys ( buf , "\<CR>" ) 94call WaitForAssert ({ ->assert_match ( ' 3 foo foo' , term_getline ( buf , 5 ))}, 1000 ) 95call WaitForAssert ({ ->assert_match ( ':' , term_getline ( buf , 6 ))}, 1000 ) 96 97" The printed line should overwrite the colon 98call term_sendkeys ( buf , "\<CR>" ) 99call WaitForAssert ({ ->assert_match ( ' 3 foo foo' , term_getline ( buf , 4 ))}, 1000 ) 100call WaitForAssert ({ ->assert_match ( ' 4 foo foo' , term_getline ( buf , 5 ))}, 1000 ) 101call WaitForAssert ({ ->assert_match ( ':' , term_getline ( buf , 6 ))}, 1000 ) 102 103call term_sendkeys ( buf , ":vi\<CR>" ) 104call WaitForAssert ({ ->assert_match ( 'foo bar' , term_getline ( buf , 1 ))}, 1000 ) 105 106call StopVimInTerminal ( buf ) 107endfunc 108 109" Test for displaying lines from an empty buffer in Ex mode 110func Test_Ex_emptybuf () 111throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script' 112new 113call assert_fails ( 'call feedkeys("Q\<CR>", "xt")' , 'E749:' ) 114call setline ( 1 , "abc" ) 115call assert_fails ( 'call feedkeys("Q\<CR>", "xt")' , 'E501:' ) 116call assert_fails ( 'call feedkeys("Q%d\<CR>", "xt")' , 'E749:' ) 117bw ! 118endfunc 119 120" Test for the :open command 121func Test_open_command () 122throw 'Skipped: Nvim does not have :open' 123new 124call setline ( 1 , [ 'foo foo' , 'foo bar' , 'foo baz' ]) 125call feedkeys ( "Qopen\<CR>j" , 'xt' ) 126call assert_equal ( 'foo bar' , getline ( '.' )) 127call feedkeys ( "Qopen /bar/\<CR>" , 'xt' ) 128call assert_equal ( 5 , col ( '.' )) 129call assert_fails ( 'call feedkeys("Qopen /baz/\<CR>", "xt")' , 'E479:' ) 130bw ! 131endfunc 132 133func Test_open_command_flush_line () 134throw 'Skipped: Nvim does not have :open' 135" this was accessing freed memory: the regexp match uses a pointer to the 136" current line which becomes invalid when searching for the ') mark. 137new 138call setline ( 1 , [ 'one' , 'two. three' ]) 139s /one/ONE 140try 141open /\%')/ 142catch /E479/ 143endtry 144bwipe ! 145endfunc 146 147" FIXME: this doesn't fail without the fix but hangs 148func Skip_Test_open_command_state () 149" Tricky script that failed because State was not set properly 150let lines =<< trim END 151!ls 1520scìi 153so! Xsourced 154set t_û0=0 155v/-/o 156END 157call writefile ( lines , 'XopenScript' , '' ) 158 159let sourced = [ "!f\u0083\x02\<Esc>z=0" ] 160call writefile ( sourced , 'Xsourced' , 'b' ) 161 162CheckRunVimInTerminal 163let buf = RunVimInTerminal ( '-u NONE -i NONE -n -m -X -Z -e -s -S XopenScript -c qa!' , #{ rows : 6 , wait_for_ruler : 0 , no_clean : 1 }) 164sleep 3 165 166call StopVimInTerminal ( buf ) 167endfunc 168 169" Test for :g/pat/visual to run vi commands in Ex mode 170" This used to hang Vim before 8.2.0274. 171func Test_Ex_global () 172throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script' 173new 174call setline ( 1 , [ '' , 'foo' , 'bar' , 'foo' , 'bar' , 'foo' ]) 175call feedkeys ( "Q\<bs>g/bar/visual\<CR>$rxQ$ryQvisual\<CR>j" , "xt" ) 176call assert_equal ( 'bax' , getline ( 3 )) 177call assert_equal ( 'bay' , getline ( 5 )) 178bwipe ! 179 180throw 'Skipped: Nvim only supports Vim Ex mode' 181new 182call setline ( 1 , [ 'foo' , 'bar' ]) 183call feedkeys ( "Qg/./i\\\na\\\n.\\\na\\\nb\\\n." , "xt" ) 184call assert_equal ([ 'a' , 'b' , 'foo' , 'a' , 'b' , 'bar' ], getline ( 1 , '$' )) 185bwipe ! 186endfunc 187 188func Test_Ex_shell () 189throw 'Skipped: Nvim only supports Vim Ex mode' 190CheckUnix 191 192new 193call feedkeys ( "Qr !echo foo\\\necho bar\n" , 'xt' ) 194call assert_equal ([ '' , 'foo' , 'bar' ], getline ( 1 , '$' )) 195bwipe ! 196 197new 198call feedkeys ( "Qr !echo foo\\\\\nbar\n" , 'xt' ) 199call assert_equal ([ '' , 'foobar' ], getline ( 1 , '$' )) 200bwipe ! 201 202new 203call feedkeys ( "Qr !echo foo\\ \\\necho bar\n" , 'xt' ) 204call assert_equal ([ '' , 'foo ' , 'bar' ], getline ( 1 , '$' )) 205bwipe ! 206 207new 208call setline ( 1 , [ 'bar' , 'baz' ]) 209call feedkeys ( "Qg/./!echo \\\ns/b/c/" , "xt" ) 210call assert_equal ([ 'car' , 'caz' ], getline ( 1 , '$' )) 211bwipe ! 212endfunc 213 214" Test for pressing Ctrl-C in :append inside a loop in Ex mode 215" This used to hang Vim 216func Test_Ex_append_in_loop () 217CheckRunVimInTerminal 218let buf = RunVimInTerminal ( '' , { 'rows' : 6 }) 219 220call term_sendkeys ( buf , "gQ" ) 221call term_sendkeys ( buf , "for i in range(1)\<CR>" ) 222call term_sendkeys ( buf , "append\<CR>" ) 223call WaitForAssert ({ ->assert_match ( ': append' , term_getline ( buf , 5 ))}, 1000 ) 224call term_sendkeys ( buf , "\<C-C>" ) 225" Wait for input to be flushed 226call term_wait ( buf ) 227call term_sendkeys ( buf , "foo\<CR>" ) 228call WaitForAssert ({ ->assert_match ( 'foo' , term_getline ( buf , 5 ))}, 1000 ) 229call term_sendkeys ( buf , ".\<CR>" ) 230call WaitForAssert ({ ->assert_match ( '.' , term_getline ( buf , 5 ))}, 1000 ) 231call term_sendkeys ( buf , "endfor\<CR>" ) 232call term_sendkeys ( buf , "vi\<CR>" ) 233call WaitForAssert ({ ->assert_match ( 'foo' , term_getline ( buf , 1 ))}, 1000 ) 234 235call StopVimInTerminal ( buf ) 236endfunc 237 238" In Ex-mode, a backslash escapes a newline 239func Test_Ex_escape_enter () 240throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script' 241call feedkeys ( "gQlet l = \"a\\\<kEnter>b\"\<cr>vi\<cr>" , 'xt' ) 242call assert_equal ( "a\rb" , l ) 243endfunc 244 245" Test for :append! command in Ex mode 246func Test_Ex_append () 247" feedkeys('x') implies ex_normal_busy; Ex mode cannot run inside :normal. 248throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script' 249new 250call setline ( 1 , "\t abc" ) 251call feedkeys ( "Qappend!\npqr\nxyz\n.\nvisual\n" , 'xt' ) 252call assert_equal ([ "\t abc" , "\t pqr" , "\t xyz" ], getline ( 1 , '$' )) 253bw ! 254 255new 256call feedkeys ( "Qappend\na\\\n." , 'xt' ) 257call assert_equal ([ 'a\' ], getline ( 1 , '$' )) 258bw ! 259endfunc 260 261func Test_ex_mode_errors () 262" Not allowed to enter ex mode when text is locked 263au InsertCharPre <buffer> normal ! 1q:<CR> 264let caught_e565 = 0 265try 266call feedkeys ( "ix\<esc>" , 'xt' ) 267catch /^Vim\%((\a\+)\)\=:E565/ " catch E565 268let caught_e565 = 1 269endtry 270call assert_equal ( 1 , caught_e565 ) 271au ! InsertCharPre 272 273new 274au CmdLineEnter * call ExEnterFunc () 275func ExEnterFunc () 276 277endfunc 278call feedkeys ( "gQvi\r" , 'xt' ) 279 280au ! CmdLineEnter 281delfunc ExEnterFunc 282 283au CmdlineEnter * : 284call feedkeys ( "gQecho 1\r" , 'xt' ) 285 286au ! CmdlineEnter 287 288quit 289endfunc 290 291func Test_ex_mode_with_global () 292CheckNotGui 293CheckFeature timers 294 295" This will get stuck in Normal mode after the failed "J", use a timer to 296" get going again. 297let lines =<< trim END 298" call ch_logfile('logfile', 'w') 299pedit 300func FeedQ(id) 301call feedkeys('1q:', 't') 302endfunc 303call timer_start(10, 'FeedQ') 304g/^/vi|HJ 305call writefile(['done'], 'Xdidexmode') 306qall! 307END 308call writefile ( lines , 'Xexmodescript' ) 309call assert_equal ( 1 , RunVim ([], [], '-e -s -S Xexmodescript' )) 310call assert_equal ([ 'done' ], readfile ( 'Xdidexmode' )) 311 312call delete ( 'logfile' ) 313call delete ( 'Xdidexmode' ) 314call delete ( 'Xexmodescript' ) 315endfunc 316 317func Test_ex_mode_count_overflow () 318" The multiplication causes an integer overflow 319CheckNotAsan 320 321" this used to cause a crash 322let lines =<< trim END 323call feedkeys("\<Esc>1q:\<CR>") 324v9|9silent! vi|333333233333y32333333%O 325call writefile(['done'], 'Xdidexmode') 326qall! 327END 328call writefile ( lines , 'Xexmodescript' ) 329call assert_equal ( 1 , RunVim ([], [], '-e -s -S Xexmodescript -c qa' )) 330call assert_equal ([ 'done' ], readfile ( 'Xdidexmode' )) 331 332call delete ( 'Xdidexmode' ) 333call delete ( 'Xexmodescript' ) 334endfunc 335 336func Test_ex_mode_large_indent () 337new 338set ts = 500 ai 339call setline ( 1 , "\t" ) 340exe "normal gQi\<CR>." 341set ts = 8 noai 342bwipe ! 343endfunc 344 345" This was accessing illegal memory when using "+" for eap->cmd. 346func Test_empty_command_visual_mode () 347let lines =<< trim END 348r<sfile> 3490norm0V: 350:qall! 351END 352call writefile ( lines , 'Xexmodescript' ) 353call assert_equal ( 1 , RunVim ([], [], '-u NONE -e -s -S Xexmodescript' )) 354 355call delete ( 'Xexmodescript' ) 356endfunc 357 358" Test using backslash in ex-mode 359func Test_backslash_multiline () 360throw 'Skipped: Nvim only supports Vim Ex mode' 361new 362call setline ( 1 , 'enum' ) 363call feedkeys ( 'Qg/enum/i\ \ .' , "xt" ) 364call assert_equal ([ "" , "enum" ], getline ( 1 , 2 )) 365endfunc 366 367" Test using backslash in ex-mode after patch 9.1.0535 368func Test_backslash_multiline2 () 369throw 'Skipped: Nvim only supports Vim Ex mode' 370new 371call feedkeys ( 'Qa X \\ Y .' , "xt" ) 372call assert_equal ([ 'X \\' , "Y" ], getline ( 1 , 2 )) 373endfunc 374 375" Testing implicit print command 376func Test_implicit_print () 377throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script' 378new 379call setline ( 1 , [ 'one' , 'two' , 'three' ]) 380call feedkeys ( 'Q:let a=execute(":1,2")' , 'xt' ) 381call feedkeys ( 'Q:let b=execute(":3")' , 'xt' ) 382call assert_equal ( 'one two' , a ->split ( '\n' ) ->join ( ' ' )) 383call assert_equal ( 'three' , b ->split ( '\n' ) ->join ( ' ' )) 384bw ! 385endfunc 386 387" Test inserting text after the trailing bar 388func Test_insert_after_trailing_bar () 389" feedkeys('x') implies ex_normal_busy; Ex mode cannot run inside :normal. 390throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script' 391new 392call feedkeys ( "Qi|\nfoo\n.\na|bar\nbar\n.\nc|baz\n." , "xt" ) 393call assert_equal ([ '' , 'foo' , 'bar' , 'baz' ], getline ( 1 , '$' )) 394bwipe ! 395endfunc 396 397" Test global insert of a newline without terminating period 398func Test_global_insert_newline () 399throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script' 400new 401call setline ( 1 , [ 'foo' ]) 402call feedkeys ( "Qg/foo/i\\\n" , "xt" ) 403call assert_equal ([ '' , 'foo' ], getline ( 1 , '$' )) 404bwipe ! 405endfunc 406 407" An empty command followed by a newline shouldn't cause E749 in Ex mode. 408func Test_ex_empty_command_newline () 409throw 'Skipped: Nvim implements interactive Ex mode as a cmdwin script' 410let g: var = 0 411call feedkeys ( "gQexecute \"\\nlet g:var = 1\"\r" , 'xt' ) 412call assert_equal ( 1 , g: var ) 413call feedkeys ( "gQexecute \" \\nlet g:var = 2\"\r" , 'xt' ) 414call assert_equal ( 2 , g: var ) 415call feedkeys ( "gQexecute \"\\t \\nlet g:var = 3\"\r" , 'xt' ) 416call assert_equal ( 3 , g: var ) 417call feedkeys ( "gQexecute \"\\\"?!\\nlet g:var = 4\"\r" , 'xt' ) 418call assert_equal ( 4 , g: var ) 419call feedkeys ( "gQexecute \" \\\"?!\\nlet g:var = 5\"\r" , 'xt' ) 420call assert_equal ( 5 , g: var ) 421unlet g: var 422endfunc 423 424" vim: shiftwidth=2 sts=2 expandtab