robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local describe , it , before_each , after_each , setup , teardown , finally = 5t . describe , t . it , t . before_each , t . after_each , t . setup , t . teardown , t . finally 6local uv =vim . uv 7 8local dedent =t . dedent 9local assert_alive =n . assert_alive 10local NIL =vim . NIL 11local clear , eq , neq =n . clear , t . eq , t . neq 12local command =n . command 13local command_output =n . api . nvim_command_output 14local exec =n . exec 15local exec_capture =n . exec_capture 16local eval =n . eval 17local expect =n . expect 18local fn =n . fn 19local api =n . api 20local matches =t . matches 21local pesc =vim . pesc 22local mkdir_p =n . mkdir_p 23local ok , nvim_async , feed =t . ok , n . nvim_async , n . feed 24local async_meths =n . async_meths 25local is_os =t . is_os 26local parse_context =n . parse_context 27local request =n . request 28local rmdir =n . rmdir 29local source =n . source 30local next_msg =n . next_msg 31local tmpname =t . tmpname 32local write_file =t . write_file 33local exec_lua =n . exec_lua 34local insert =n . insert 35local skip =t . skip 36 37local pcall_err =t . pcall_err 38local format_string =require ( 'test.format_string' ). format_string 39local intchar2lua =t . intchar2lua 40local mergedicts_copy =t . mergedicts_copy 41local endswith =vim . endswith 42 43describe ( 'API' , function () 44before_each ( clear ) 45 46it ( 'validates requests' , function () 47-- RPC 48matches ( 'Invalid method: bogus$' , pcall_err ( request , 'bogus' )) 49matches ( 'Invalid method: … の り 。…$' , pcall_err ( request , '… の り 。…' )) 50matches ( 'Invalid method: <empty>$' , pcall_err ( request , '' )) 51 52-- Non-RPC: rpcrequest(v:servername) uses internal channel. 53matches ( 54'Invalid method: … の り 。…$' , 55pcall_err ( 56request , 57'nvim_eval' , 58[=[rpcrequest(sockconnect('pipe', v:servername, {'rpc':1}), '… の り 。…')]=] 59) 60) 61matches ( 62'Invalid method: bogus$' , 63pcall_err ( 64request , 65'nvim_eval' , 66[=[rpcrequest(sockconnect('pipe', v:servername, {'rpc':1}), 'bogus')]=] 67) 68) 69 70-- XXX: This must be the last one, else next one will fail: 71-- "Packer instance already working. Use another Packer ..." 72matches ( "can't serialize object of type .$" , pcall_err ( request , nil )) 73end ) 74 75it ( 'handles errors in async requests' , function () 76local error_types =api . nvim_get_api_info ()[ 2 ]. error_types 77nvim_async ( 'bogus' ) 78eq ( { 79'notification' , 80'nvim_error_event' , 81{ error_types . Exception . id , 'Invalid method: bogus' } , 82} , next_msg ()) 83-- error didn't close channel. 84assert_alive () 85end ) 86 87it ( 'failed async request emits nvim_error_event' , function () 88local error_types =api . nvim_get_api_info ()[ 2 ]. error_types 89async_meths . nvim_command ( 'bogus' ) 90eq ( { 91'notification' , 92'nvim_error_event' , 93{ error_types . Exception . id , 'Vim:E492: Not an editor command: bogus' } , 94} , next_msg ()) 95-- error didn't close channel. 96assert_alive () 97end ) 98 99it ( 'input is processed first if followed immediately by non-fast events' , function () 100api . nvim_set_current_line ( 'ab' ) 101async_meths . nvim_input ( 'x' ) 102async_meths . nvim_exec_lua ( '_G.res1 = vim.api.nvim_get_current_line()' , {} ) 103async_meths . nvim_exec_lua ( '_G.res2 = vim.api.nvim_get_current_line()' , {} ) 104eq ( { 'b' , 'b' } , exec_lua ( 'return { _G.res1, _G.res2 }' )) 105-- Also test with getchar() 106async_meths . nvim_command ( 'let g:getchar = 1 | call getchar() | let g:getchar = 0' ) 107eq ( 1 , api . nvim_get_var ( 'getchar' )) 108async_meths . nvim_input ( 'x' ) 109async_meths . nvim_exec_lua ( '_G.res1 = vim.g.getchar' , {} ) 110async_meths . nvim_exec_lua ( '_G.res2 = vim.g.getchar' , {} ) 111eq ( { 0 , 0 } , exec_lua ( 'return { _G.res1, _G.res2 }' )) 112end ) 113 114it ( 'does not set CA_COMMAND_BUSY #7254' , function () 115command ( 'split' ) 116command ( 'autocmd WinEnter * startinsert' ) 117command ( 'wincmd w' ) 118eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 119end ) 120 121describe ( 'nvim_exec2' , function () 122it ( 'always returns table' , function () 123-- In built version this results into `vim.empty_dict()` 124eq ( {} , api . nvim_exec2 ( 'echo "Hello"' , {} )) 125eq ( {} , api . nvim_exec2 ( 'echo "Hello"' , { output =false } )) 126eq ( { output ='Hello' } , api . nvim_exec2 ( 'echo "Hello"' , { output =true } )) 127end ) 128 129it ( 'default options' , function () 130-- Should be equivalent to { output = false } 131api . nvim_exec2 ( "let x0 = 'a'" , {} ) 132eq ( 'a' , api . nvim_get_var ( 'x0' )) 133end ) 134 135it ( 'one-line input' , function () 136api . nvim_exec2 ( "let x1 = 'a'" , { output =false } ) 137eq ( 'a' , api . nvim_get_var ( 'x1' )) 138end ) 139 140it ( ':verbose set {option}?' , function () 141api . nvim_exec2 ( 'set nowrap' , { output =false } ) 142eq ( 143{ output ='nowrap\n\tLast set from anonymous :source line 1' } , 144api . nvim_exec2 ( 'verbose set wrap?' , { output =true } ) 145) 146 147-- Using script var to force creation of a script item 148api . nvim_exec2 ( 149[[ 150let s:a = 1 151set nowrap 152]] , 153{ output =false } 154) 155eq ( 156{ output ='nowrap\n\tLast set from anonymous :source (script id 1) line 2' } , 157api . nvim_exec2 ( 'verbose set wrap?' , { output =true } ) 158) 159end ) 160 161it ( 'multiline input' , function () 162-- Heredoc + empty lines. 163api . nvim_exec2 ( "let x2 = 'a'\n" , { output =false } ) 164eq ( 'a' , api . nvim_get_var ( 'x2' )) 165api . nvim_exec2 ( 'lua <<EOF\n\n\n\ny=3\n\n\nEOF' , { output =false } ) 166eq ( 3 , api . nvim_eval ( "luaeval('y')" )) 167 168eq ( {} , api . nvim_exec2 ( 'lua <<EOF\ny=3\nEOF' , { output =false } )) 169eq ( 3 , api . nvim_eval ( "luaeval('y')" )) 170 171-- Multiple statements 172api . nvim_exec2 ( 'let x1=1\nlet x2=2\nlet x3=3\n' , { output =false } ) 173eq ( 1 , api . nvim_eval ( 'x1' )) 174eq ( 2 , api . nvim_eval ( 'x2' )) 175eq ( 3 , api . nvim_eval ( 'x3' )) 176 177-- Functions 178api . nvim_exec2 ( 'function Foo()\ncall setline(1,["xxx"])\nendfunction' , { output =false } ) 179eq ( '' , api . nvim_get_current_line ()) 180api . nvim_exec2 ( 'call Foo()' , { output =false } ) 181eq ( 'xxx' , api . nvim_get_current_line ()) 182 183-- Autocmds 184api . nvim_exec2 ( 'autocmd BufAdd * :let x1 = "Hello"' , { output =false } ) 185command ( 'new foo' ) 186eq ( 'Hello' , request ( 'nvim_eval' , 'g:x1' )) 187 188-- Line continuations 189api . nvim_exec2 ( 190[[ 191let abc = #{ 192\ a: 1, 193"\ b: 2, 194\ c: 3 195\ }]] , 196{ output =false } 197) 198eq ( { a =1 , c =3 } , request ( 'nvim_eval' , 'g:abc' )) 199 200-- try no spaces before continuations to catch off-by-one error 201api . nvim_exec2 ( 'let ab = #{\n\\a: 98,\n"\\ b: 2\n\\}' , { output =false } ) 202eq ( { a =98 } , request ( 'nvim_eval' , 'g:ab' )) 203 204-- Script scope (s:) 205eq ( 206{ output ='ahoy! script-scoped varrrrr' } , 207api . nvim_exec2 ( 208[[ 209let s:pirate = 'script-scoped varrrrr' 210function! s:avast_ye_hades(s) abort 211return a:s .. ' ' .. s:pirate 212endfunction 213echo <sid>avast_ye_hades('ahoy!') 214]] , 215{ output =true } 216) 217) 218 219eq ( 220{ output ="{'output': 'ahoy! script-scoped varrrrr'}" } , 221api . nvim_exec2 ( 222[[ 223let s:pirate = 'script-scoped varrrrr' 224function! Avast_ye_hades(s) abort 225return a:s .. ' ' .. s:pirate 226endfunction 227echo nvim_exec2('echo Avast_ye_hades(''ahoy!'')', {'output': v:true}) 228]] , 229{ output =true } 230) 231) 232 233matches ( 234'Vim%(echo%):E121: Undefined variable: s:pirate$' , 235pcall_err ( 236request , 237'nvim_exec2' , 238[[ 239let s:pirate = 'script-scoped varrrrr' 240call nvim_exec2('echo s:pirate', {'output': v:true}) 241]] , 242{ output =false } 243) 244) 245 246-- Script items are created only on script var access 247eq ( 248{ output ='1\n0' } , 249api . nvim_exec2 ( 250[[ 251echo expand("<SID>")->empty() 252let s:a = 123 253echo expand("<SID>")->empty() 254]] , 255{ output =true } 256) 257) 258 259eq ( 260{ output ='1\n0' } , 261api . nvim_exec2 ( 262[[ 263echo expand("<SID>")->empty() 264function s:a() abort 265endfunction 266echo expand("<SID>")->empty() 267]] , 268{ output =true } 269) 270) 271end ) 272 273it ( 'non-ASCII input' , function () 274api . nvim_exec2 ( 275[=[ 276new 277exe "normal! i ax \n Ax " 278:%s/ax/--a1234--/g | :%s/Ax/--A1234--/g 279]=] , 280{ output =false } 281) 282command ( '1' ) 283eq ( ' --a1234-- ' , api . nvim_get_current_line ()) 284command ( '2' ) 285eq ( ' --A1234-- ' , api . nvim_get_current_line ()) 286 287api . nvim_exec2 ( 288[[ 289new 290call setline(1,['xxx']) 291call feedkeys('r') 292call feedkeys('ñ', 'xt') 293]] , 294{ output =false } 295) 296eq ( 'ñxx' , api . nvim_get_current_line ()) 297end ) 298 299it ( 'can use :finish' , function () 300api . nvim_exec2 ( 'let g:var = 123\nfinish\nlet g:var = 456' , {} ) 301eq ( 123 , api . nvim_get_var ( 'var' )) 302end ) 303 304it ( 'execution error' , function () 305eq ( 306'nvim_exec2(), line 1: Vim:E492: Not an editor command: bogus_command' , 307pcall_err ( request , 'nvim_exec2' , 'bogus_command' , {} ) 308) 309eq ( '' , api . nvim_eval ( 'v:errmsg' )) -- v:errmsg was not updated. 310eq ( '' , eval ( 'v:exception' )) 311 312eq ( 313'nvim_exec2(), line 1: Vim(buffer):E86: Buffer 23487 does not exist' , 314pcall_err ( request , 'nvim_exec2' , 'buffer 23487' , {} ) 315) 316eq ( '' , eval ( 'v:errmsg' )) -- v:errmsg was not updated. 317eq ( '' , eval ( 'v:exception' )) 318end ) 319 320it ( 'recursion' , function () 321local fname =tmpname () 322write_file ( fname , 'let x1 = "set from :source file"\n' ) 323-- nvim_exec2 324-- :source 325-- nvim_exec2 326request ( 'nvim_exec2' , [[ 327let x2 = substitute('foo','o','X','g') 328let x4 = 'should be overwritten' 329call nvim_exec2("source ]] .. fname .. [[\nlet x3 = substitute('foo','foo','set by recursive nvim_exec2','g')\nlet x5='overwritten'\nlet x4=x5\n", {'output': v:false}) 330]] , { output =false } ) 331eq ( 'set from :source file' , request ( 'nvim_get_var' , 'x1' )) 332eq ( 'fXX' , request ( 'nvim_get_var' , 'x2' )) 333eq ( 'set by recursive nvim_exec2' , request ( 'nvim_get_var' , 'x3' )) 334eq ( 'overwritten' , request ( 'nvim_get_var' , 'x4' )) 335eq ( 'overwritten' , request ( 'nvim_get_var' , 'x5' )) 336os . remove ( fname ) 337end ) 338 339it ( 'traceback' , function () 340local fname =tmpname () 341write_file ( fname , 'echo "hello"\n' ) 342local sourcing_fname =tmpname () 343write_file ( sourcing_fname , 'call nvim_exec2("source ' .. fname .. '", {"output": v:false})\n' ) 344api . nvim_exec2 ( 'set verbose=2' , { output =false } ) 345local traceback_output =dedent ( [[ 346sourcing "nvim_exec2()" 347line 1: sourcing "nvim_exec2() called at nvim_exec2():1" 348line 1: sourcing "%s" 349line 1: sourcing "nvim_exec2() called at %s:1" 350line 1: sourcing "%s" 351hello 352finished sourcing %s 353continuing in nvim_exec2() called at %s:1 354finished sourcing nvim_exec2() called at %s:1 355continuing in %s 356finished sourcing %s 357continuing in nvim_exec2() called at nvim_exec2():1 358finished sourcing nvim_exec2() called at nvim_exec2():1 359continuing in nvim_exec2() 360finished sourcing nvim_exec2()]] ): format ( 361sourcing_fname , 362sourcing_fname , 363fname , 364fname , 365sourcing_fname , 366sourcing_fname , 367sourcing_fname , 368sourcing_fname 369) 370eq ( 371{ output =traceback_output } , 372api . nvim_exec2 ( 373'call nvim_exec2("source ' .. sourcing_fname .. '", {"output": v:false})' , 374{ output =true } 375) 376) 377os . remove ( fname ) 378os . remove ( sourcing_fname ) 379end ) 380 381it ( 'returns output' , function () 382eq ( 383{ output ='this is spinal tap' } , 384api . nvim_exec2 ( 'lua <<EOF\n\n\nprint("this is spinal tap")\n\n\nEOF' , { output =true } ) 385) 386eq ( { output ='' } , api . nvim_exec2 ( 'echo' , { output =true } )) 387eq ( { output ='foo 42' } , api . nvim_exec2 ( 'echo "foo" 42' , { output =true } )) 388-- Returns output in cmdline mode #35321 389feed ( ':' ) 390eq ( { output ='foo 42' } , api . nvim_exec2 ( 'echo "foo" 42' , { output =true } )) 391end ) 392 393it ( 'displays messages when opts.output=false' , function () 394local screen =Screen . new ( 40 , 8 ) 395api . nvim_exec2 ( "echo 'hello'" , { output =false } ) 396screen : expect { 397grid =[[ 398^ | 399{1:~ }|*6 400hello | 401]] , 402} 403end ) 404 405it ( "doesn't display messages when output=true" , function () 406local screen =Screen . new ( 40 , 6 ) 407api . nvim_exec2 ( "echo 'hello'" , { output =true } ) 408screen : expect { 409grid =[[ 410^ | 411{1:~ }|*4 412| 413]] , 414} 415exec ( [[ 416func Print() 417call nvim_exec2('echo "hello"', { 'output': v:true }) 418endfunc 419]] ) 420feed ( [[:echon 1 | call Print() | echon 5<CR>]] ) 421screen : expect { 422grid =[[ 423^ | 424{1:~ }|*4 42515 | 426]] , 427} 428end ) 429 430it ( 'errors properly when command too recursive' , function () 431exec_lua ( [[ 432_G.success = false 433vim.api.nvim_create_user_command('Test', function() 434vim.api.nvim_exec2('Test', {}) 435_G.success = true 436end, {}) 437]] ) 438pcall_err ( command , 'Test' ) 439assert_alive () 440eq ( false , exec_lua ( 'return _G.success' )) 441end ) 442 443it ( 'redir_write() message column is reset with ext_messages' , function () 444exec_lua ( 'vim.ui_attach(1, { ext_messages = true }, function() end)' ) 445api . nvim_exec2 ( 'hi VisualNC' , { output =true } ) 446eq ( 'VisualNC xxx cleared' , api . nvim_exec2 ( 'hi VisualNC' , { output =true } ). output ) 447api . nvim_exec2 ( 'echon 1234567' , { output =true } ) 448eq ( 'VisualNC xxx cleared' , api . nvim_exec2 ( 'hi VisualNC' , { output =true } ). output ) 449end ) 450 451it ( 'captures multi-chunk err nvim_echo() #36883' , function () 452eq ( 453'nvim_exec2(), line 1: Vim(call):abc' , 454pcall_err ( request , 'nvim_exec2' , 'call nvim_echo([["a"],["b"],["c"] ], 0, #{err:1})' , {} ) 455) 456end ) 457end ) 458 459describe ( 'nvim_command' , function () 460it ( 'works' , function () 461local fname =tmpname () 462command ( 'new' ) 463command ( 'edit ' .. fname ) 464command ( 'normal itesting\napi' ) 465command ( 'w' ) 466local f =assert ( io . open ( fname )) 467if is_os ( 'win' ) then 468eq ( 'testing\r\napi\r\n' , f : read ( '*a' )) 469else 470eq ( 'testing\napi\n' , f : read ( '*a' )) 471end 472f : close () 473os . remove ( fname ) 474end ) 475 476it ( 'Vimscript validation error: fails with specific error' , function () 477local status , rv =pcall ( command , 'bogus_command' ) 478eq ( false , status ) -- nvim_command() failed. 479eq ( 'E492:' , string . match ( rv , 'E%d*:' )) -- Vimscript error was returned. 480eq ( '' , api . nvim_eval ( 'v:errmsg' )) -- v:errmsg was not updated. 481eq ( '' , eval ( 'v:exception' )) 482end ) 483 484it ( 'Vimscript execution error: fails with specific error' , function () 485local status , rv =pcall ( command , 'buffer 23487' ) 486eq ( false , status ) -- nvim_command() failed. 487eq ( 'E86: Buffer 23487 does not exist' , string . match ( rv , 'E%d*:.*' )) 488eq ( '' , eval ( 'v:errmsg' )) -- v:errmsg was not updated. 489eq ( '' , eval ( 'v:exception' )) 490end ) 491 492it ( 'gives E493 instead of prompting on backwards range' , function () 493command ( 'split' ) 494eq ( 495'Vim(windo):E493: Backwards range given: 2,1windo echo' , 496pcall_err ( command , '2,1windo echo' ) 497) 498end ) 499end ) 500 501describe ( 'nvim_command_output' , function () 502it ( 'does not induce hit-enter prompt' , function () 503api . nvim_ui_attach ( 80 , 20 , {} ) 504-- Induce a hit-enter prompt use nvim_input (non-blocking). 505command ( 'set cmdheight=1' ) 506api . nvim_input ( [[:echo "hi\nhi2"<CR>]] ) 507 508-- Verify hit-enter prompt. 509eq ( { mode ='r' , blocking =true } , api . nvim_get_mode ()) 510api . nvim_input ( [[<C-c>]] ) 511 512-- Verify NO hit-enter prompt. 513command_output ( [[echo "hi\nhi2"]] ) 514eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 515end ) 516 517it ( 'captures command output' , function () 518eq ( 'this is\nspinal tap' , command_output ( [[echo "this is\nspinal tap"]] )) 519eq ( 'no line ending!' , command_output ( [[echon "no line ending!"]] )) 520end ) 521 522it ( 'captures empty command output' , function () 523eq ( '' , command_output ( 'echo' )) 524end ) 525 526it ( 'captures single-char command output' , function () 527eq ( 'x' , command_output ( 'echo "x"' )) 528end ) 529 530it ( 'captures multiple commands' , function () 531eq ( 'foo\n 1 %a "[No Name]" line 1' , command_output ( 'echo "foo" | ls' )) 532end ) 533 534it ( 'captures nested execute()' , function () 535eq ( 536'\nnested1\nnested2\n 1 %a "[No Name]" line 1' , 537command_output ( [[echo execute('echo "nested1\nnested2"') | ls]] ) 538) 539end ) 540 541it ( 'captures nested nvim_command_output()' , function () 542eq ( 543'nested1\nnested2\n 1 %a "[No Name]" line 1' , 544command_output ( [[echo nvim_command_output('echo "nested1\nnested2"') | ls]] ) 545) 546end ) 547 548it ( 'returns shell |:!| output' , function () 549local win_lf =is_os ( 'win' ) and '\r' or '' 550eq ( ':!echo foo\r\n\nfoo' .. win_lf .. '\n' , command_output ( [[!echo foo]] )) 551end ) 552 553it ( 'Vimscript validation error: fails with specific error' , function () 554local status , rv =pcall ( command_output , 'bogus commannnd' ) 555eq ( false , status ) -- nvim_command_output() failed. 556eq ( 'E492: Not an editor command: bogus commannnd' , string . match ( rv , 'E%d*:.*' )) 557eq ( '' , eval ( 'v:errmsg' )) -- v:errmsg was not updated. 558-- Verify NO hit-enter prompt. 559eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 560end ) 561 562it ( 'Vimscript execution error: fails with specific error' , function () 563local status , rv =pcall ( command_output , 'buffer 42' ) 564eq ( false , status ) -- nvim_command_output() failed. 565eq ( 'E86: Buffer 42 does not exist' , string . match ( rv , 'E%d*:.*' )) 566eq ( '' , eval ( 'v:errmsg' )) -- v:errmsg was not updated. 567-- Verify NO hit-enter prompt. 568eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 569end ) 570 571it ( 'does not cause heap buffer overflow with large output' , function () 572eq ( eval ( 'string(range(1000000))' ), command_output ( 'echo range(1000000)' )) 573end ) 574end ) 575 576describe ( 'nvim_eval' , function () 577it ( 'works' , function () 578command ( 'let g:v1 = "a"' ) 579command ( 'let g:v2 = [1, 2, {"v3": 3}]' ) 580eq ( { v1 ='a' , v2 ={ 1 , 2 , { v3 =3 } } } , api . nvim_eval ( 'g:' )) 581end ) 582 583it ( 'handles NULL-initialized strings correctly' , function () 584eq ( 1 , api . nvim_eval ( "matcharg(1) == ['', '']" )) 585eq ( { '' , '' } , api . nvim_eval ( 'matcharg(1)' )) 586end ) 587 588it ( 'works under deprecated name' , function () 589eq ( 2 , request ( 'vim_eval' , '1+1' )) 590end ) 591 592it ( 'Vimscript error: returns error details, does NOT update v:errmsg' , function () 593eq ( 'Vim:E121: Undefined variable: bogus' , pcall_err ( request , 'nvim_eval' , 'bogus expression' )) 594eq ( '' , eval ( 'v:errmsg' )) -- v:errmsg was not updated. 595end ) 596 597it ( 'can return Lua function to Lua code' , function () 598eq ( 599[["a string with \"double quotes\" and 'single quotes'"]] , 600exec_lua ( [=[ 601local fun = vim.api.nvim_eval([[luaeval('string.format')]]) 602return fun('%q', [[a string with "double quotes" and 'single quotes']]) 603]=] ) 604) 605end ) 606end ) 607 608describe ( 'nvim_call_function' , function () 609it ( 'works' , function () 610api . nvim_call_function ( 'setqflist' , { { { filename ='something' , lnum =17 } } , 'r' } ) 611eq ( 17 , api . nvim_call_function ( 'getqflist' , {} )[ 1 ]. lnum ) 612eq ( 17 , api . nvim_call_function ( 'eval' , { 17 } )) 613eq ( 'foo' , api . nvim_call_function ( 'simplify' , { 'this/./is//redundant/../../../foo' } )) 614end ) 615 616it ( 'Vimscript validation error: returns specific error, does NOT update v:errmsg' , function () 617eq ( 618'Vim:E117: Unknown function: bogus function' , 619pcall_err ( request , 'nvim_call_function' , 'bogus function' , { 'arg1' } ) 620) 621eq ( 622'Vim:E119: Not enough arguments for function: atan' , 623pcall_err ( request , 'nvim_call_function' , 'atan' , {} ) 624) 625eq ( '' , eval ( 'v:exception' )) 626eq ( '' , eval ( 'v:errmsg' )) -- v:errmsg was not updated. 627end ) 628 629it ( 'Vimscript error: returns error details, does NOT update v:errmsg' , function () 630eq ( 631'Vim:E808: Number or Float required' , 632pcall_err ( request , 'nvim_call_function' , 'atan' , { 'foo' } ) 633) 634eq ( 635'Vim:Invalid channel stream "xxx"' , 636pcall_err ( request , 'nvim_call_function' , 'chanclose' , { 999 , 'xxx' } ) 637) 638eq ( 639'Vim:E900: Invalid channel id' , 640pcall_err ( request , 'nvim_call_function' , 'chansend' , { 999 , 'foo' } ) 641) 642eq ( '' , eval ( 'v:exception' )) 643eq ( '' , eval ( 'v:errmsg' )) -- v:errmsg was not updated. 644end ) 645 646it ( 'Vimscript exception: returns exception details, does NOT update v:errmsg' , function () 647source ( [[ 648function! Foo() abort 649throw 'wtf' 650endfunction 651]] ) 652eq ( 'function Foo, line 1: wtf' , pcall_err ( request , 'nvim_call_function' , 'Foo' , {} )) 653eq ( '' , eval ( 'v:exception' )) 654eq ( '' , eval ( 'v:errmsg' )) -- v:errmsg was not updated. 655end ) 656 657it ( 'validation' , function () 658-- stylua: ignore 659local too_many_args ={ 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' , 'x' } 660source ( [[ 661function! Foo(...) abort 662echo a:000 663endfunction 664]] ) 665-- E740 666eq ( 667'Function called with too many arguments' , 668pcall_err ( request , 'nvim_call_function' , 'Foo' , too_many_args ) 669) 670end ) 671 672it ( 'can return Lua function to Lua code' , function () 673eq ( 674[["a string with \"double quotes\" and 'single quotes'"]] , 675exec_lua ( [=[ 676local fun = vim.api.nvim_call_function('luaeval', { 'string.format' }) 677return fun('%q', [[a string with "double quotes" and 'single quotes']]) 678]=] ) 679) 680end ) 681end ) 682 683describe ( 'nvim_call_dict_function' , function () 684it ( 'invokes Vimscript dict function' , function () 685source ( [[ 686function! F(name) dict 687return self.greeting.', '.a:name.'!' 688endfunction 689let g:test_dict_fn = { 'greeting':'Hello', 'F':function('F') } 690 691let g:test_dict_fn2 = { 'greeting':'Hi' } 692function g:test_dict_fn2.F2(name) 693return self.greeting.', '.a:name.' ...' 694endfunction 695]] ) 696 697-- :help Dictionary-function 698eq ( 'Hello, World!' , api . nvim_call_dict_function ( 'g:test_dict_fn' , 'F' , { 'World' } )) 699-- Funcref is sent as NIL over RPC. 700eq ( { greeting ='Hello' , F =NIL } , api . nvim_get_var ( 'test_dict_fn' )) 701 702-- :help numbered-function 703eq ( 'Hi, Moon ...' , api . nvim_call_dict_function ( 'g:test_dict_fn2' , 'F2' , { 'Moon' } )) 704-- Funcref is sent as NIL over RPC. 705eq ( { greeting ='Hi' , F2 =NIL } , api . nvim_get_var ( 'test_dict_fn2' )) 706 707-- Function specified via RPC dict. 708source ( 'function! G() dict\n return "@".(self.result)."@"\nendfunction' ) 709eq ( '@it works@' , api . nvim_call_dict_function ( { result ='it works' , G ='G' } , 'G' , {} )) 710end ) 711 712it ( 'validation' , function () 713command ( 'let g:d={"baz":"zub","meep":[]}' ) 714eq ( 715'Not found: bogus' , 716pcall_err ( request , 'nvim_call_dict_function' , 'g:d' , 'bogus' , { 1 , 2 } ) 717) 718eq ( 719'Not a function: baz' , 720pcall_err ( request , 'nvim_call_dict_function' , 'g:d' , 'baz' , { 1 , 2 } ) 721) 722eq ( 723'Not a function: meep' , 724pcall_err ( request , 'nvim_call_dict_function' , 'g:d' , 'meep' , { 1 , 2 } ) 725) 726eq ( 727'Vim:E117: Unknown function: f' , 728pcall_err ( request , 'nvim_call_dict_function' , { f ='' } , 'f' , { 1 , 2 } ) 729) 730eq ( 731'Not a function: f' , 732pcall_err ( request , 'nvim_call_dict_function' , "{ 'f': '' }" , 'f' , { 1 , 2 } ) 733) 734eq ( 735'Invalid dict argument: expected String or Dict' , 736pcall_err ( request , 'nvim_call_dict_function' , 42 , 'f' , { 1 , 2 } ) 737) 738eq ( 739'Vim:E121: Undefined variable: foo' , 740pcall_err ( request , 'nvim_call_dict_function' , 'foo' , 'f' , { 1 , 2 } ) 741) 742eq ( 'dict not found' , pcall_err ( request , 'nvim_call_dict_function' , '42' , 'f' , { 1 , 2 } )) 743eq ( 744'Invalid function name: (empty)' , 745pcall_err ( request , 'nvim_call_dict_function' , "{ 'f': '' }" , '' , { 1 , 2 } ) 746) 747end ) 748end ) 749 750describe ( 'nvim_set_current_dir' , function () 751local start_dir 752local test_dir ='Xtest_set_current_dir' 753 754before_each ( function () 755fn . mkdir ( test_dir ) 756start_dir =fn . getcwd () 757end ) 758 759after_each ( function () 760n . rmdir ( test_dir ) 761end ) 762 763it ( 'works' , function () 764api . nvim_set_current_dir ( test_dir ) 765eq ( start_dir .. n . get_pathsep () .. test_dir , fn . getcwd ()) 766end ) 767 768it ( 'sets previous directory' , function () 769api . nvim_set_current_dir ( test_dir ) 770command ( 'cd -' ) 771eq ( start_dir , fn . getcwd ()) 772end ) 773end ) 774 775describe ( 'nvim_exec_lua' , function () 776it ( 'works' , function () 777api . nvim_exec_lua ( 'vim.api.nvim_set_var("test", 3)' , {} ) 778eq ( 3 , api . nvim_get_var ( 'test' )) 779 780eq ( 17 , api . nvim_exec_lua ( 'a, b = ...\nreturn a + b' , { 10 , 7 } )) 781 782eq ( NIL , api . nvim_exec_lua ( 'function xx(a,b)\nreturn a..b\nend' , {} )) 783eq ( 'xy' , api . nvim_exec_lua ( 'return xx(...)' , { 'x' , 'y' } )) 784 785-- Deprecated name: nvim_execute_lua. 786eq ( 'xy' , api . nvim_execute_lua ( 'return xx(...)' , { 'x' , 'y' } )) 787end ) 788 789it ( 'reports errors' , function () 790eq ( [['=' expected near '+']] , pcall_err ( api . nvim_exec_lua , 'a+*b' , {} )) 791eq ( [[unexpected symbol near '1']] , pcall_err ( api . nvim_exec_lua , '1+2' , {} )) 792eq ( [[unexpected symbol]] , pcall_err ( api . nvim_exec_lua , 'aa=bb\0' , {} )) 793eq ( 794[[attempt to call global 'bork' (a nil value)]] , 795pcall_err ( api . nvim_exec_lua , 'bork()' , {} ) 796) 797eq ( 'did\nthe\nfail' , pcall_err ( api . nvim_exec_lua , 'error("did\\nthe\\nfail")' , {} )) 798end ) 799 800it ( 'uses native float values' , function () 801eq ( 2.5 , api . nvim_exec_lua ( 'return select(1, ...)' , { 2.5 } )) 802eq ( '2.5' , api . nvim_exec_lua ( 'return vim.inspect(...)' , { 2.5 } )) 803 804-- "special" float values are still accepted as return values. 805eq ( 2.5 , api . nvim_exec_lua ( "return vim.api.nvim_eval('2.5')" , {} )) 806eq ( 807'{\n [false] = 2.5,\n [true] = 3\n}' , 808api . nvim_exec_lua ( "return vim.inspect(vim.api.nvim_eval('2.5'))" , {} ) 809) 810end ) 811end ) 812 813describe ( 'nvim_input' , function () 814it ( 'Vimscript error: does NOT fail, updates v:errmsg' , function () 815local status , _ =pcall ( api . nvim_input , ':call bogus_fn()<CR>' ) 816local v_errnum =string . match ( api . nvim_eval ( 'v:errmsg' ), 'E%d*:' ) 817eq ( true , status ) -- nvim_input() did not fail. 818eq ( 'E117:' , v_errnum ) -- v:errmsg was updated. 819end ) 820 821it ( 'does not crash even if trans_special result is largest #11788, #12287' , function () 822command ( "call nvim_input('<M-'.nr2char(0x40000000).'>')" ) 823eq ( 1 , eval ( '1' )) 824end ) 825 826it ( 'keeps accepting input after the input buffer fills and drains' , function () 827-- Regression test: the fixed input buffer (INPUT_BUFFER_SIZE == 16386) is 828-- compacted left, not a ring. Filling it to within <19 bytes of the top 829-- and then draining it used to leave the read/write cursors pinned, so 830-- input_enqueue() could never reclaim space and silently dropped all 831-- further input, freezing the editor. One oversized burst of a Normal-mode 832-- no-op (CTRL-L) saturates the buffer in a single nvim_input() call; once it 833-- has drained, input must still register. poke_eventloop() waits for the 834-- burst to be fully consumed before the probe: the fix only reclaims space 835-- once the buffer is empty, so the probe must arrive after the drain. 836api . nvim_input (( '\12' ): rep ( 20000 )) 837n . poke_eventloop () 838api . nvim_input ( 'ix<Esc>' ) 839n . poke_eventloop () 840eq ( 'x' , api . nvim_get_current_line ()) 841end ) 842end ) 843 844describe ( 'nvim_paste' , function () 845it ( 'validation' , function () 846eq ( "Invalid 'phase': -2" , pcall_err ( request , 'nvim_paste' , 'foo' , true , - 2 )) 847eq ( "Invalid 'phase': 4" , pcall_err ( request , 'nvim_paste' , 'foo' , true , 4 )) 848end ) 849local function run_streamed_paste_tests () 850it ( 'stream: multiple chunks form one undo-block' , function () 851api . nvim_paste ( '1/chunk 1 (start)\n' , true , 1 ) 852api . nvim_paste ( '1/chunk 2 (end)\n' , true , 3 ) 853local expected1 =[[ 8541/chunk 1 (start) 8551/chunk 2 (end) 856]] 857expect ( expected1 ) 858api . nvim_paste ( '2/chunk 1 (start)\n' , true , 1 ) 859api . nvim_paste ( '2/chunk 2\n' , true , 2 ) 860expect ( [[ 8611/chunk 1 (start) 8621/chunk 2 (end) 8632/chunk 1 (start) 8642/chunk 2 865]] ) 866api . nvim_paste ( '2/chunk 3\n' , true , 2 ) 867api . nvim_paste ( '2/chunk 4 (end)\n' , true , 3 ) 868expect ( [[ 8691/chunk 1 (start) 8701/chunk 2 (end) 8712/chunk 1 (start) 8722/chunk 2 8732/chunk 3 8742/chunk 4 (end) 875]] ) 876feed ( 'u' ) -- Undo. 877expect ( expected1 ) 878end ) 879it ( "stream: multiple chunks sets correct '[ mark" , function () 880-- Pastes single chunk 881api . nvim_paste ( 'aaaaaa\n' , true , - 1 ) 882eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( "'[" )) 883-- Pastes an empty chunk 884api . nvim_paste ( '' , true , - 1 ) 885eq ( { 0 , 2 , 1 , 0 } , fn . getpos ( "'[" )) 886-- Pastes some chunks on empty line 887api . nvim_paste ( '1/chunk 1 (start)\n' , true , 1 ) 888eq ( { 0 , 2 , 1 , 0 } , fn . getpos ( "'[" )) 889api . nvim_paste ( '1/chunk 2\n' , true , 2 ) 890eq ( { 0 , 2 , 1 , 0 } , fn . getpos ( "'[" )) 891api . nvim_paste ( '1/chunk 3 (end)\n' , true , 3 ) 892eq ( { 0 , 2 , 1 , 0 } , fn . getpos ( "'[" )) 893-- Pastes some chunks on non-empty line 894api . nvim_paste ( 'aaaaaa' , true , - 1 ) 895eq ( { 0 , 5 , 1 , 0 } , fn . getpos ( "'[" )) 896api . nvim_paste ( 'bbbbbb' , true , 1 ) 897eq ( { 0 , 5 , 7 , 0 } , fn . getpos ( "'[" )) 898api . nvim_paste ( 'cccccc' , true , 2 ) 899eq ( { 0 , 5 , 7 , 0 } , fn . getpos ( "'[" )) 900api . nvim_paste ( 'dddddd\n' , true , 3 ) 901eq ( { 0 , 5 , 7 , 0 } , fn . getpos ( "'[" )) 902-- Pastes some empty chunks between non-empty chunks 903api . nvim_paste ( '' , true , 1 ) 904eq ( { 0 , 5 , 7 , 0 } , fn . getpos ( "'[" )) 905api . nvim_paste ( 'a' , true , 2 ) 906eq ( { 0 , 6 , 1 , 0 } , fn . getpos ( "'[" )) 907api . nvim_paste ( '' , true , 2 ) 908eq ( { 0 , 6 , 1 , 0 } , fn . getpos ( "'[" )) 909api . nvim_paste ( 'a' , true , 3 ) 910eq ( { 0 , 6 , 1 , 0 } , fn . getpos ( "'[" )) 911end ) 912it ( 'stream: Insert mode' , function () 913-- If nvim_paste() calls :undojoin without making any changes, this makes it an error. 914feed ( 'afoo<Esc>u' ) 915feed ( 'i' ) 916api . nvim_paste ( 'aaaaaa' , false , 1 ) 917api . nvim_paste ( 'bbbbbb' , false , 2 ) 918api . nvim_paste ( 'cccccc' , false , 2 ) 919api . nvim_paste ( 'dddddd' , false , 3 ) 920expect ( 'aaaaaabbbbbbccccccdddddd' ) 921feed ( '<Esc>u' ) 922expect ( '' ) 923end ) 924describe ( 'stream: Normal mode' , function () 925describe ( 'on empty line' , function () 926before_each ( function () 927-- If nvim_paste() calls :undojoin without making any changes, this makes it an error. 928feed ( 'afoo<Esc>u' ) 929end ) 930after_each ( function () 931feed ( 'u' ) 932expect ( '' ) 933end ) 934it ( 'pasting one line' , function () 935api . nvim_paste ( 'aaaaaa' , false , 1 ) 936api . nvim_paste ( 'bbbbbb' , false , 2 ) 937api . nvim_paste ( 'cccccc' , false , 2 ) 938api . nvim_paste ( 'dddddd' , false , 3 ) 939expect ( 'aaaaaabbbbbbccccccdddddd' ) 940end ) 941it ( 'pasting multiple lines' , function () 942api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 943api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 944api . nvim_paste ( 'cccccc\n' , false , 2 ) 945api . nvim_paste ( 'dddddd' , false , 3 ) 946expect ( [[ 947aaaaaa 948bbbbbb 949cccccc 950dddddd]] ) 951end ) 952end ) 953describe ( 'not at the end of a line' , function () 954before_each ( function () 955feed ( 'i||<Esc>' ) 956-- If nvim_paste() calls :undojoin without making any changes, this makes it an error. 957feed ( 'afoo<Esc>u' ) 958feed ( '0' ) 959end ) 960after_each ( function () 961feed ( 'u' ) 962expect ( '||' ) 963end ) 964it ( 'pasting one line' , function () 965api . nvim_paste ( 'aaaaaa' , false , 1 ) 966api . nvim_paste ( 'bbbbbb' , false , 2 ) 967api . nvim_paste ( 'cccccc' , false , 2 ) 968api . nvim_paste ( 'dddddd' , false , 3 ) 969expect ( '|aaaaaabbbbbbccccccdddddd|' ) 970end ) 971it ( 'pasting multiple lines' , function () 972api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 973api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 974api . nvim_paste ( 'cccccc\n' , false , 2 ) 975api . nvim_paste ( 'dddddd' , false , 3 ) 976expect ( [[ 977|aaaaaa 978bbbbbb 979cccccc 980dddddd|]] ) 981end ) 982end ) 983describe ( 'at the end of a line' , function () 984before_each ( function () 985feed ( 'i||<Esc>' ) 986-- If nvim_paste() calls :undojoin without making any changes, this makes it an error. 987feed ( 'afoo<Esc>u' ) 988feed ( '2|' ) 989end ) 990after_each ( function () 991feed ( 'u' ) 992expect ( '||' ) 993end ) 994it ( 'pasting one line' , function () 995api . nvim_paste ( 'aaaaaa' , false , 1 ) 996api . nvim_paste ( 'bbbbbb' , false , 2 ) 997api . nvim_paste ( 'cccccc' , false , 2 ) 998api . nvim_paste ( 'dddddd' , false , 3 ) 999expect ( '||aaaaaabbbbbbccccccdddddd' ) 1000end ) 1001it ( 'pasting multiple lines' , function () 1002api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1003api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1004api . nvim_paste ( 'cccccc\n' , false , 2 ) 1005api . nvim_paste ( 'dddddd' , false , 3 ) 1006expect ( [[ 1007||aaaaaa 1008bbbbbb 1009cccccc 1010dddddd]] ) 1011end ) 1012end ) 1013end ) 1014describe ( 'stream: Visual mode' , function () 1015describe ( 'neither end at the end of a line' , function () 1016before_each ( function () 1017feed ( 'i|xxx<CR>xxx|<Esc>' ) 1018-- If nvim_paste() calls :undojoin without making any changes, this makes it an error. 1019feed ( 'afoo<Esc>u' ) 1020feed ( '3|vhk' ) 1021end ) 1022after_each ( function () 1023feed ( 'u' ) 1024expect ( [[ 1025|xxx 1026xxx|]] ) 1027end ) 1028it ( 'with non-empty chunks' , function () 1029api . nvim_paste ( 'aaaaaa' , false , 1 ) 1030api . nvim_paste ( 'bbbbbb' , false , 2 ) 1031api . nvim_paste ( 'cccccc' , false , 2 ) 1032api . nvim_paste ( 'dddddd' , false , 3 ) 1033expect ( '|aaaaaabbbbbbccccccdddddd|' ) 1034end ) 1035it ( 'with empty first chunk' , function () 1036api . nvim_paste ( '' , false , 1 ) 1037api . nvim_paste ( 'bbbbbb' , false , 2 ) 1038api . nvim_paste ( 'cccccc' , false , 2 ) 1039api . nvim_paste ( 'dddddd' , false , 3 ) 1040expect ( '|bbbbbbccccccdddddd|' ) 1041end ) 1042it ( 'with all chunks empty' , function () 1043api . nvim_paste ( '' , false , 1 ) 1044api . nvim_paste ( '' , false , 2 ) 1045api . nvim_paste ( '' , false , 2 ) 1046api . nvim_paste ( '' , false , 3 ) 1047expect ( '||' ) 1048end ) 1049end ) 1050describe ( 'cursor at the end of a line' , function () 1051before_each ( function () 1052feed ( 'i||xxx<CR>xxx<Esc>' ) 1053-- If nvim_paste() calls :undojoin without making any changes, this makes it an error. 1054feed ( 'afoo<Esc>u' ) 1055feed ( '3|vko' ) 1056end ) 1057after_each ( function () 1058feed ( 'u' ) 1059expect ( [[ 1060||xxx 1061xxx]] ) 1062end ) 1063it ( 'with non-empty chunks' , function () 1064api . nvim_paste ( 'aaaaaa' , false , 1 ) 1065api . nvim_paste ( 'bbbbbb' , false , 2 ) 1066api . nvim_paste ( 'cccccc' , false , 2 ) 1067api . nvim_paste ( 'dddddd' , false , 3 ) 1068expect ( '||aaaaaabbbbbbccccccdddddd' ) 1069end ) 1070it ( 'with empty first chunk' , function () 1071api . nvim_paste ( '' , false , 1 ) 1072api . nvim_paste ( 'bbbbbb' , false , 2 ) 1073api . nvim_paste ( 'cccccc' , false , 2 ) 1074api . nvim_paste ( 'dddddd' , false , 3 ) 1075expect ( '||bbbbbbccccccdddddd' ) 1076end ) 1077end ) 1078describe ( 'other end at the end of a line' , function () 1079before_each ( function () 1080feed ( 'i||xxx<CR>xxx<Esc>' ) 1081-- If nvim_paste() calls :undojoin without making any changes, this makes it an error. 1082feed ( 'afoo<Esc>u' ) 1083feed ( '3|vk' ) 1084end ) 1085after_each ( function () 1086feed ( 'u' ) 1087expect ( [[ 1088||xxx 1089xxx]] ) 1090end ) 1091it ( 'with non-empty chunks' , function () 1092api . nvim_paste ( 'aaaaaa' , false , 1 ) 1093api . nvim_paste ( 'bbbbbb' , false , 2 ) 1094api . nvim_paste ( 'cccccc' , false , 2 ) 1095api . nvim_paste ( 'dddddd' , false , 3 ) 1096expect ( '||aaaaaabbbbbbccccccdddddd' ) 1097end ) 1098it ( 'with empty first chunk' , function () 1099api . nvim_paste ( '' , false , 1 ) 1100api . nvim_paste ( 'bbbbbb' , false , 2 ) 1101api . nvim_paste ( 'cccccc' , false , 2 ) 1102api . nvim_paste ( 'dddddd' , false , 3 ) 1103expect ( '||bbbbbbccccccdddddd' ) 1104end ) 1105end ) 1106end ) 1107describe ( 'stream: linewise Visual mode' , function () 1108before_each ( function () 1109feed ( 'i123456789<CR>987654321<CR>123456789<Esc>' ) 1110-- If nvim_paste() calls :undojoin without making any changes, this makes it an error. 1111feed ( 'afoo<Esc>u' ) 1112end ) 1113after_each ( function () 1114feed ( 'u' ) 1115expect ( [[ 1116123456789 1117987654321 1118123456789]] ) 1119end ) 1120describe ( 'selecting the start of a file' , function () 1121before_each ( function () 1122feed ( 'ggV' ) 1123end ) 1124it ( 'pasting text without final new line' , function () 1125api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1126api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1127api . nvim_paste ( 'cccccc\n' , false , 2 ) 1128api . nvim_paste ( 'dddddd' , false , 3 ) 1129expect ( [[ 1130aaaaaa 1131bbbbbb 1132cccccc 1133dddddd987654321 1134123456789]] ) 1135end ) 1136it ( 'pasting text with final new line' , function () 1137api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1138api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1139api . nvim_paste ( 'cccccc\n' , false , 2 ) 1140api . nvim_paste ( 'dddddd\n' , false , 3 ) 1141expect ( [[ 1142aaaaaa 1143bbbbbb 1144cccccc 1145dddddd 1146987654321 1147123456789]] ) 1148end ) 1149end ) 1150describe ( 'selecting the middle of a file' , function () 1151before_each ( function () 1152feed ( '2ggV' ) 1153end ) 1154it ( 'pasting text without final new line' , function () 1155api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1156api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1157api . nvim_paste ( 'cccccc\n' , false , 2 ) 1158api . nvim_paste ( 'dddddd' , false , 3 ) 1159expect ( [[ 1160123456789 1161aaaaaa 1162bbbbbb 1163cccccc 1164dddddd123456789]] ) 1165end ) 1166it ( 'pasting text with final new line' , function () 1167api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1168api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1169api . nvim_paste ( 'cccccc\n' , false , 2 ) 1170api . nvim_paste ( 'dddddd\n' , false , 3 ) 1171expect ( [[ 1172123456789 1173aaaaaa 1174bbbbbb 1175cccccc 1176dddddd 1177123456789]] ) 1178end ) 1179end ) 1180describe ( 'selecting the end of a file' , function () 1181before_each ( function () 1182feed ( '3ggV' ) 1183end ) 1184it ( 'pasting text without final new line' , function () 1185api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1186api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1187api . nvim_paste ( 'cccccc\n' , false , 2 ) 1188api . nvim_paste ( 'dddddd' , false , 3 ) 1189expect ( [[ 1190123456789 1191987654321 1192aaaaaa 1193bbbbbb 1194cccccc 1195dddddd]] ) 1196end ) 1197it ( 'pasting text with final new line' , function () 1198api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1199api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1200api . nvim_paste ( 'cccccc\n' , false , 2 ) 1201api . nvim_paste ( 'dddddd\n' , false , 3 ) 1202expect ( [[ 1203123456789 1204987654321 1205aaaaaa 1206bbbbbb 1207cccccc 1208dddddd 1209]] ) 1210end ) 1211end ) 1212describe ( 'selecting the whole file' , function () 1213before_each ( function () 1214feed ( 'ggVG' ) 1215end ) 1216it ( 'pasting text without final new line' , function () 1217api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1218api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1219api . nvim_paste ( 'cccccc\n' , false , 2 ) 1220api . nvim_paste ( 'dddddd' , false , 3 ) 1221expect ( [[ 1222aaaaaa 1223bbbbbb 1224cccccc 1225dddddd]] ) 1226end ) 1227it ( 'pasting text with final new line' , function () 1228api . nvim_paste ( 'aaaaaa\n' , false , 1 ) 1229api . nvim_paste ( 'bbbbbb\n' , false , 2 ) 1230api . nvim_paste ( 'cccccc\n' , false , 2 ) 1231api . nvim_paste ( 'dddddd\n' , false , 3 ) 1232expect ( [[ 1233aaaaaa 1234bbbbbb 1235cccccc 1236dddddd 1237]] ) 1238end ) 1239end ) 1240end ) 1241end 1242describe ( 'without virtualedit,' , function () 1243run_streamed_paste_tests () 1244end ) 1245describe ( 'with virtualedit=onemore,' , function () 1246before_each ( function () 1247command ( 'set virtualedit=onemore' ) 1248end ) 1249run_streamed_paste_tests () 1250end ) 1251describe ( 'stream: terminal buffer' , function () 1252local eol =is_os ( 'win' ) and '\r\n' or '\n' 1253before_each ( function () 1254exec_lua ( function () 1255_G . input ={} 1256_G . chan =vim . api . nvim_open_term ( 0 , { 1257on_input =function ( _ , _ , _ , data ) 1258if # data > 0 then 1259table . insert ( _G . input , data ) 1260end 1261end , 1262force_crlf =false , 1263} ) 1264end ) 1265end ) 1266local function run_terminal_streamed_paste_tests ( check_dot_repeat ) 1267it ( 'without bracketed paste mode in terminal' , function () 1268api . nvim_paste ( 'AA\nBB\n' , false , 1 ) 1269eq ( { 'AA' , eol , 'BB' , eol } , exec_lua ( 'return _G.input' )) 1270api . nvim_paste ( 'CC' , false , 2 ) 1271eq ( { 'AA' , eol , 'BB' , eol , 'CC' } , exec_lua ( 'return _G.input' )) 1272api . nvim_paste ( '\nDD' , false , 3 ) 1273eq ( { 'AA' , eol , 'BB' , eol , 'CC' , eol , 'DD' } , exec_lua ( 'return _G.input' )) 1274if check_dot_repeat then 1275exec_lua ( '_G.input = {}' ) 1276feed ( '.' ) 1277eq ( { 'AA' , eol , 'BB' , eol , 'CC' , eol , 'DD' } , exec_lua ( 'return _G.input' )) 1278end 1279end ) 1280it ( 'with bracketed paste mode in terminal' , function () 1281exec_lua ( [[vim.api.nvim_chan_send(_G.chan, '\027[?2004h')]] ) 1282api . nvim_paste ( 'AA\nBB\n' , false , 1 ) 1283eq ( { '\027[200~' , 'AA' , eol , 'BB' , eol } , exec_lua ( 'return _G.input' )) 1284api . nvim_paste ( 'CC' , false , 2 ) 1285eq ( { '\027[200~' , 'AA' , eol , 'BB' , eol , 'CC' } , exec_lua ( 'return _G.input' )) 1286api . nvim_paste ( '\nDD' , false , 3 ) 1287eq ( 1288{ '\027[200~' , 'AA' , eol , 'BB' , eol , 'CC' , eol , 'DD' , '\027[201~' } , 1289exec_lua ( 'return _G.input' ) 1290) 1291if check_dot_repeat then 1292exec_lua ( '_G.input = {}' ) 1293feed ( '.' ) 1294eq ( 1295{ '\027[200~' , 'AA' , eol , 'BB' , eol , 'CC' , eol , 'DD' , '\027[201~' } , 1296exec_lua ( 'return _G.input' ) 1297) 1298end 1299end ) 1300end 1301describe ( 'in Normal mode' , function () 1302run_terminal_streamed_paste_tests ( true ) 1303end ) 1304describe ( 'in Terminal mode' , function () 1305before_each ( function () 1306feed ( 'i' ) 1307eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 1308end ) 1309run_terminal_streamed_paste_tests ( false ) 1310end ) 1311end ) 1312it ( 'non-streaming' , function () 1313-- With final "\n". 1314api . nvim_paste ( 'line 1\nline 2\nline 3\n' , true , - 1 ) 1315expect ( [[ 1316line 1 1317line 2 1318line 3 1319]] ) 1320eq ( { 0 , 4 , 1 , 0 } , fn . getpos ( '.' )) -- Cursor follows the paste. 1321eq ( false , api . nvim_get_option_value ( 'paste' , {} )) 1322command ( '%delete _' ) 1323-- Without final "\n". 1324api . nvim_paste ( 'line 1\nline 2\nline 3' , true , - 1 ) 1325expect ( [[ 1326line 1 1327line 2 1328line 3]] ) 1329eq ( { 0 , 3 , 6 , 0 } , fn . getpos ( '.' )) 1330command ( '%delete _' ) 1331-- CRLF #10872 1332api . nvim_paste ( 'line 1\r\nline 2\r\nline 3\r\n' , true , - 1 ) 1333expect ( [[ 1334line 1 1335line 2 1336line 3 1337]] ) 1338eq ( { 0 , 4 , 1 , 0 } , fn . getpos ( '.' )) 1339command ( '%delete _' ) 1340-- CRLF without final "\n". 1341api . nvim_paste ( 'line 1\r\nline 2\r\nline 3\r' , true , - 1 ) 1342expect ( [[ 1343line 1 1344line 2 1345line 3 1346]] ) 1347eq ( { 0 , 4 , 1 , 0 } , fn . getpos ( '.' )) 1348command ( '%delete _' ) 1349-- CRLF without final "\r\n". 1350api . nvim_paste ( 'line 1\r\nline 2\r\nline 3' , true , - 1 ) 1351expect ( [[ 1352line 1 1353line 2 1354line 3]] ) 1355eq ( { 0 , 3 , 6 , 0 } , fn . getpos ( '.' )) 1356command ( '%delete _' ) 1357-- Various other junk. 1358api . nvim_paste ( 'line 1\r\n\r\rline 2\nline 3\rline 4\r' , true , - 1 ) 1359expect ( 'line 1\n\n\nline 2\nline 3\nline 4\n' ) 1360eq ( { 0 , 7 , 1 , 0 } , fn . getpos ( '.' )) 1361eq ( false , api . nvim_get_option_value ( 'paste' , {} )) 1362end ) 1363it ( 'Replace-mode' , function () 1364-- Within single line 1365api . nvim_put ( { 'aabbccdd' , 'eeffgghh' , 'iijjkkll' } , 'c' , true , false ) 1366command ( 'normal l' ) 1367command ( 'startreplace' ) 1368api . nvim_paste ( '123456' , true , - 1 ) 1369expect ( [[ 1370a123456d 1371eeffgghh 1372iijjkkll]] ) 1373command ( '%delete _' ) 1374-- Across lines 1375api . nvim_put ( { 'aabbccdd' , 'eeffgghh' , 'iijjkkll' } , 'c' , true , false ) 1376command ( 'normal l' ) 1377command ( 'startreplace' ) 1378api . nvim_paste ( '123\n456' , true , - 1 ) 1379expect ( [[ 1380a123 1381456d 1382eeffgghh 1383iijjkkll]] ) 1384end ) 1385it ( 'when searching in Visual mode' , function () 1386feed ( 'v/' ) 1387api . nvim_paste ( 'aabbccdd' , true , - 1 ) 1388eq ( 'aabbccdd' , fn . getcmdline ()) 1389expect ( '' ) 1390end ) 1391it ( 'mappings are disabled in Cmdline mode' , function () 1392command ( 'cnoremap a b' ) 1393feed ( ':' ) 1394api . nvim_paste ( 'a' , true , - 1 ) 1395eq ( 'a' , fn . getcmdline ()) 1396end ) 1397it ( 'pasted text is saved in cmdline history when <CR> comes from mapping #20957' , function () 1398command ( 'cnoremap <CR> <CR>' ) 1399feed ( ':' ) 1400api . nvim_paste ( 'echo' , true , - 1 ) 1401eq ( '' , fn . histget ( ':' )) 1402feed ( '<CR>' ) 1403eq ( 'echo' , fn . histget ( ':' )) 1404end ) 1405it ( 'pasting with empty last chunk in Cmdline mode' , function () 1406local screen =Screen . new ( 20 , 4 ) 1407feed ( ':' ) 1408api . nvim_paste ( 'Foo' , true , 1 ) 1409api . nvim_paste ( '' , true , 3 ) 1410screen : expect ( [[ 1411| 1412{1:~ }|*2 1413:Foo^ | 1414]] ) 1415end ) 1416it ( 'pasting text with control characters in Cmdline mode' , function () 1417local screen =Screen . new ( 20 , 4 ) 1418feed ( ':' ) 1419api . nvim_paste ( 'normal! \023\022\006\027' , true , - 1 ) 1420screen : expect ( [[ 1421| 1422{1:~ }|*2 1423:normal! {18:^W^V^F^[}^ | 1424]] ) 1425end ) 1426it ( 'crlf=false does not break lines at CR, CRLF' , function () 1427api . nvim_paste ( 'line 1\r\n\r\rline 2\nline 3\rline 4\r' , false , - 1 ) 1428local expected ='line 1\r\n\r\rline 2\nline 3\rline 4\r' 1429expect ( expected ) 1430eq ( { 0 , 3 , 14 , 0 } , fn . getpos ( '.' )) 1431feed ( 'u' ) -- Undo. 1432expect ( '' ) 1433feed ( '.' ) -- Dot-repeat. 1434expect ( expected ) 1435end ) 1436describe ( 'repeating a paste via redo/recording' , function () 1437-- Test with indent and control chars and multibyte chars containing 0x80 bytes 1438local text =dedent (( [[ 1439foo 1440bar 1441baz 1442!!!%s!!!%s!!!%s!!! 1443最…倒…倀… 1444]] ): format ( '\0' , '\2\3\6\21\22\23\24\27' , '\127' )) 1445before_each ( function () 1446api . nvim_set_option_value ( 'autoindent' , true , {} ) 1447end ) 1448local function test_paste_repeat_normal_insert ( is_insert ) 1449feed ( 'qr' .. ( is_insert and 'i' or '' )) 1450eq ( 'r' , fn . reg_recording ()) 1451api . nvim_paste ( text , true , - 1 ) 1452feed ( is_insert and '<Esc>' or '' ) 1453expect ( text ) 1454feed ( '.' ) 1455expect ( text : rep ( 2 )) 1456feed ( 'q' ) 1457eq ( '' , fn . reg_recording ()) 1458feed ( '3.' ) 1459expect ( text : rep ( 5 )) 1460feed ( '2@r' ) 1461expect ( text : rep ( 9 )) 1462end 1463it ( 'works in Normal mode' , function () 1464test_paste_repeat_normal_insert ( false ) 1465end ) 1466it ( 'works in Insert mode' , function () 1467test_paste_repeat_normal_insert ( true ) 1468end ) 1469local function test_paste_repeat_visual_select ( is_select ) 1470insert (( 'xxx\n' ): rep ( 5 )) 1471feed ( 'ggqr' .. ( is_select and 'gH' or 'V' )) 1472api . nvim_paste ( text , true , - 1 ) 1473feed ( 'q' ) 1474expect ( text .. ( 'xxx\n' ): rep ( 4 )) 1475feed ( '2@r' ) 1476expect ( text : rep ( 3 ) .. ( 'xxx\n' ): rep ( 2 )) 1477end 1478it ( 'works in Visual mode (recording only)' , function () 1479test_paste_repeat_visual_select ( false ) 1480end ) 1481it ( 'works in Select mode (recording only)' , function () 1482test_paste_repeat_visual_select ( true ) 1483end ) 1484end ) 1485it ( 'in a mapping recorded in a macro' , function () 1486command ( [[nnoremap <F2> <Cmd>call nvim_paste('foo', v:false, -1)<CR>]] ) 1487feed ( 'qr<F2>$q' ) 1488expect ( 'foo' ) 1489feed ( '@r' ) -- repeating a macro containing the mapping should only paste once 1490expect ( 'foofoo' ) 1491end ) 1492local function test_paste_cancel_error ( is_error ) 1493before_each ( function () 1494exec_lua (( [[ 1495vim.paste = (function(overridden) 1496return function(lines, phase) 1497for i, line in ipairs(lines) do 1498if line == 'CANCEL' then 1499%s 1500end 1501end 1502return overridden(lines, phase) 1503end 1504end)(vim.paste) 1505]] ): format ( is_error and 'error("fake fail")' or 'return false' )) 1506end ) 1507local function check_paste_cancel_error ( data , crlf , phase ) 1508if is_error then 1509eq ( 'fake fail' , pcall_err ( api . nvim_paste , data , crlf , phase )) 1510else 1511eq ( false , api . nvim_paste ( data , crlf , phase )) 1512end 1513end 1514it ( 'in phase -1' , function () 1515feed ( 'A' ) 1516check_paste_cancel_error ( 'CANCEL' , true , - 1 ) 1517feed ( '<Esc>' ) 1518expect ( '' ) 1519feed ( '.' ) 1520expect ( '' ) 1521end ) 1522it ( 'in phase 1' , function () 1523feed ( 'A' ) 1524check_paste_cancel_error ( 'CANCEL' , true , 1 ) 1525feed ( '<Esc>' ) 1526expect ( '' ) 1527feed ( '.' ) 1528expect ( '' ) 1529end ) 1530it ( 'in phase 2' , function () 1531feed ( 'A' ) 1532eq ( true , api . nvim_paste ( 'aaa' , true , 1 )) 1533expect ( 'aaa' ) 1534check_paste_cancel_error ( 'CANCEL' , true , 2 ) 1535feed ( '<Esc>' ) 1536expect ( 'aaa' ) 1537feed ( '.' ) 1538expect ( 'aaaaaa' ) 1539end ) 1540it ( 'in phase 3' , function () 1541feed ( 'A' ) 1542eq ( true , api . nvim_paste ( 'aaa' , true , 1 )) 1543expect ( 'aaa' ) 1544eq ( true , api . nvim_paste ( 'bbb' , true , 2 )) 1545expect ( 'aaabbb' ) 1546check_paste_cancel_error ( 'CANCEL' , true , 3 ) 1547feed ( '<Esc>' ) 1548expect ( 'aaabbb' ) 1549feed ( '.' ) 1550expect ( 'aaabbbaaabbb' ) 1551end ) 1552end 1553describe ( 'vim.paste() cancel' , function () 1554test_paste_cancel_error ( false ) 1555end ) 1556describe ( 'vim.paste() error' , function () 1557test_paste_cancel_error ( true ) 1558end ) 1559end ) 1560 1561describe ( 'nvim_put' , function () 1562it ( 'validation' , function () 1563eq ( 1564"Invalid 'line': expected String, got Integer" , 1565pcall_err ( request , 'nvim_put' , { 42 } , 'l' , false , false ) 1566) 1567eq ( "Invalid 'type': 'x'" , pcall_err ( request , 'nvim_put' , { 'foo' } , 'x' , false , false )) 1568end ) 1569it ( "fails if 'nomodifiable'" , function () 1570command ( 'set nomodifiable' ) 1571eq ( 1572[[Vim:E21: Cannot make changes, 'modifiable' is off]] , 1573pcall_err ( request , 'nvim_put' , { 'a' , 'b' } , 'l' , true , true ) 1574) 1575end ) 1576it ( 'inserts text' , function () 1577exec ( [[ 1578let g:pre_event = [] 1579let g:post_event = [] 1580au TextPutPre * let g:pre_event = copy(v:event) 1581au TextPutPost * let g:post_event = copy(v:event) 1582]] ) 1583-- linewise 1584local lines ={ 'line 1' , 'line 2' , 'line 3' } 1585local expected_event ={ 1586regcontents =lines , 1587regname ='_' , 1588operator ='p' , 1589regtype ='V' , 1590visual =false , 1591} 1592api . nvim_put ( lines , 'l' , true , true ) 1593expect ( [[ 1594 1595line 1 1596line 2 1597line 3]] ) 1598eq ( { 0 , 4 , 1 , 0 } , fn . getpos ( '.' )) 1599eq ( expected_event , api . nvim_get_var ( 'pre_event' )) 1600eq ( expected_event , api . nvim_get_var ( 'post_event' )) 1601command ( '%delete _' ) 1602-- charwise 1603expected_event . regtype ='v' 1604api . nvim_put ( { 'line 1' , 'line 2' , 'line 3' } , 'c' , true , false ) 1605expect ( [[ 1606line 1 1607line 2 1608line 3]] ) 1609eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( '.' )) -- follow=false 1610eq ( expected_event , api . nvim_get_var ( 'pre_event' )) 1611eq ( expected_event , api . nvim_get_var ( 'post_event' )) 1612-- blockwise 1613lines ={ 'AA' , 'BB' } 1614expected_event . regcontents =lines 1615expected_event . regtype ='\0222' 1616api . nvim_put ( lines , 'b' , true , true ) 1617expect ( [[ 1618lAAine 1 1619lBBine 2 1620line 3]] ) 1621eq ( { 0 , 2 , 4 , 0 } , fn . getpos ( '.' )) 1622eq ( expected_event , api . nvim_get_var ( 'pre_event' )) 1623eq ( expected_event , api . nvim_get_var ( 'post_event' )) 1624command ( '%delete _' ) 1625-- Empty lines list. 1626api . nvim_put ( {} , 'c' , true , true ) 1627eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( '.' )) 1628expect ( [[]] ) 1629-- Single empty line. 1630api . nvim_put ( { '' } , 'c' , true , true ) 1631eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( '.' )) 1632expect ( [[ 1633]] ) 1634api . nvim_put ( { 'AB' } , 'c' , true , true ) 1635-- after=false, follow=true 1636lines ={ 'line 1' , 'line 2' } 1637expected_event . regcontents =lines 1638expected_event . regtype ='v' 1639expected_event . operator ='P' 1640api . nvim_put ( lines , 'c' , false , true ) 1641expect ( [[ 1642Aline 1 1643line 2B]] ) 1644eq ( { 0 , 2 , 7 , 0 } , fn . getpos ( '.' )) 1645eq ( expected_event , api . nvim_get_var ( 'pre_event' )) 1646eq ( expected_event , api . nvim_get_var ( 'post_event' )) 1647command ( '%delete _' ) 1648api . nvim_put ( { 'AB' } , 'c' , true , true ) 1649-- after=false, follow=false 1650api . nvim_put ( lines , 'c' , false , false ) 1651expect ( [[ 1652Aline 1 1653line 2B]] ) 1654eq ( { 0 , 1 , 2 , 0 } , fn . getpos ( '.' )) 1655eq ( expected_event , api . nvim_get_var ( 'pre_event' )) 1656eq ( expected_event , api . nvim_get_var ( 'post_event' )) 1657eq ( '' , api . nvim_eval ( 'v:errmsg' )) 1658end ) 1659 1660it ( 'detects charwise/linewise text (empty {type})' , function () 1661-- linewise (final item is empty string) 1662api . nvim_put ( { 'line 1' , 'line 2' , 'line 3' , '' } , '' , true , true ) 1663expect ( [[ 1664 1665line 1 1666line 2 1667line 3]] ) 1668eq ( { 0 , 4 , 1 , 0 } , fn . getpos ( '.' )) 1669command ( '%delete _' ) 1670-- charwise (final item is non-empty) 1671api . nvim_put ( { 'line 1' , 'line 2' , 'line 3' } , '' , true , true ) 1672expect ( [[ 1673line 1 1674line 2 1675line 3]] ) 1676eq ( { 0 , 3 , 6 , 0 } , fn . getpos ( '.' )) 1677end ) 1678 1679it ( 'allows block width' , function () 1680-- behave consistently with setreg(); support "\022{NUM}" return by getregtype() 1681api . nvim_put ( { 'line 1' , 'line 2' , 'line 3' } , 'l' , false , false ) 1682expect ( [[ 1683line 1 1684line 2 1685line 3 1686]] ) 1687 1688-- larger width create spaces 1689api . nvim_put ( { 'a' , 'bc' } , 'b3' , false , false ) 1690expect ( [[ 1691a line 1 1692bc line 2 1693line 3 1694]] ) 1695-- smaller width is ignored 1696api . nvim_put ( { 'xxx' , 'yyy' } , '\0221' , false , true ) 1697expect ( [[ 1698xxxa line 1 1699yyybc line 2 1700line 3 1701]] ) 1702eq ( "Invalid 'type': 'bx'" , pcall_err ( api . nvim_put , { 'xxx' , 'yyy' } , 'bx' , false , true )) 1703eq ( "Invalid 'type': 'b3x'" , pcall_err ( api . nvim_put , { 'xxx' , 'yyy' } , 'b3x' , false , true )) 1704end ) 1705 1706it ( 'computes block width correctly when not specified #35034' , function () 1707api . nvim_put ( { 'line 1' , 'line 2' , 'line 3' } , 'l' , false , false ) 1708-- block width should be 4 1709api . nvim_put ( { 'あい' , 'xxx' , 'xx' } , 'b' , false , false ) 1710expect ( [[ 1711あいline 1 1712xxx line 2 1713xx line 3 1714]] ) 1715end ) 1716end ) 1717 1718describe ( 'nvim_strwidth' , function () 1719it ( 'works' , function () 1720eq ( 3 , api . nvim_strwidth ( 'abc' )) 1721-- 6 + (neovim) 1722-- 19 * 2 (each japanese character occupies two cells) 1723eq ( 44 , api . nvim_strwidth ( 'neovimのデザインかなりまともなのになってる。' )) 1724end ) 1725 1726it ( 'cannot handle NULs' , function () 1727eq ( 0 , api . nvim_strwidth ( '\0abc' )) 1728end ) 1729 1730it ( 'can handle emoji with variant selectors and ZWJ' , function () 1731local selector ='❤️' 1732eq ( 2 , fn . strchars ( selector )) 1733eq ( 1 , fn . strcharlen ( selector )) 1734eq ( 2 , api . nvim_strwidth ( selector )) 1735 1736local no_selector ='❤' 1737eq ( 1 , fn . strchars ( no_selector )) 1738eq ( 1 , fn . strcharlen ( no_selector )) 1739eq ( 1 , api . nvim_strwidth ( no_selector )) 1740 1741local selector_zwj_selector ='🏳️⚧️' 1742eq ( 5 , fn . strchars ( selector_zwj_selector )) 1743eq ( 1 , fn . strcharlen ( selector_zwj_selector )) 1744eq ( 2 , api . nvim_strwidth ( selector_zwj_selector )) 1745 1746local emoji_zwj_emoji ='🧑🌾' 1747eq ( 3 , fn . strchars ( emoji_zwj_emoji )) 1748eq ( 1 , fn . strcharlen ( emoji_zwj_emoji )) 1749eq ( 2 , api . nvim_strwidth ( emoji_zwj_emoji )) 1750end ) 1751end ) 1752 1753describe ( 'nvim_get_current_line, nvim_set_current_line' , function () 1754it ( 'works' , function () 1755eq ( '' , api . nvim_get_current_line ()) 1756api . nvim_set_current_line ( 'abc' ) 1757eq ( 'abc' , api . nvim_get_current_line ()) 1758end ) 1759end ) 1760 1761describe ( 'set/get/del variables' , function () 1762it ( 'validation' , function () 1763eq ( 'Key not found: bogus' , pcall_err ( api . nvim_get_var , 'bogus' )) 1764eq ( 'Key not found: bogus' , pcall_err ( api . nvim_del_var , 'bogus' )) 1765end ) 1766 1767it ( 'nvim_get_var, nvim_set_var, nvim_del_var' , function () 1768api . nvim_set_var ( 'lua' , { 1 , 2 , { [ '3' ] =1 } } ) 1769eq ( { 1 , 2 , { [ '3' ] =1 } } , api . nvim_get_var ( 'lua' )) 1770eq ( { 1 , 2 , { [ '3' ] =1 } } , api . nvim_eval ( 'g:lua' )) 1771eq ( 1 , fn . exists ( 'g:lua' )) 1772api . nvim_del_var ( 'lua' ) 1773eq ( 0 , fn . exists ( 'g:lua' )) 1774eq ( 'Key not found: lua' , pcall_err ( api . nvim_del_var , 'lua' )) 1775api . nvim_set_var ( 'lua' , 1 ) 1776 1777-- Empty keys are allowed in Vim dicts (and msgpack). 1778api . nvim_set_var ( 'dict_empty_key' , { [ '' ] ='empty key' } ) 1779eq ( { [ '' ] ='empty key' } , api . nvim_get_var ( 'dict_empty_key' )) 1780 1781-- Set locked g: var. 1782command ( 'lockvar lua' ) 1783eq ( 'Key is locked: lua' , pcall_err ( api . nvim_del_var , 'lua' )) 1784eq ( 'Key is locked: lua' , pcall_err ( api . nvim_set_var , 'lua' , 1 )) 1785 1786exec ( [[ 1787function Test() 1788endfunction 1789function s:Test() 1790endfunction 1791let g:Unknown_func = function('Test') 1792let g:Unknown_script_func = function('s:Test') 1793]] ) 1794eq ( NIL , api . nvim_get_var ( 'Unknown_func' )) 1795eq ( NIL , api . nvim_get_var ( 'Unknown_script_func' )) 1796 1797-- Check if autoload works properly 1798local xhome ='Xhome_api' 1799local xconfig =( '%s/Xconfig' ): format ( xhome ) 1800local xdata =( '%s/Xdata' ): format ( xhome ) 1801local autoload_folder =( '%s/nvim/autoload' ): format ( xconfig ) 1802local autoload_file =( '%s/testload.vim' ): format ( autoload_folder ) 1803mkdir_p ( autoload_folder ) 1804write_file ( autoload_file , [[let testload#value = 2]] ) 1805 1806clear { args_rm ={ '-u' } , env ={ XDG_CONFIG_HOME =xconfig , XDG_DATA_HOME =xdata } } 1807eq ( 2 , api . nvim_get_var ( 'testload#value' )) 1808rmdir ( xhome ) 1809end ) 1810 1811it ( 'nvim_get_vvar, nvim_set_vvar' , function () 1812eq ( 'Key is read-only: count' , pcall_err ( request , 'nvim_set_vvar' , 'count' , 42 )) 1813eq ( 'Dict is locked' , pcall_err ( request , 'nvim_set_vvar' , 'nosuchvar' , 42 )) 1814api . nvim_set_vvar ( 'errmsg' , 'set by API' ) 1815eq ( 'set by API' , api . nvim_get_vvar ( 'errmsg' )) 1816api . nvim_set_vvar ( 'completed_item' , { word ='a' , user_data =vim . empty_dict () } ) 1817eq ( {} , api . nvim_get_vvar ( 'completed_item' )[ 'user_data' ]) 1818api . nvim_set_vvar ( 'errmsg' , 42 ) 1819eq ( '42' , eval ( 'v:errmsg' )) 1820api . nvim_set_vvar ( 'oldfiles' , { 'one' , 'two' } ) 1821eq ( { 'one' , 'two' } , eval ( 'v:oldfiles' )) 1822api . nvim_set_vvar ( 'oldfiles' , {} ) 1823eq ( {} , eval ( 'v:oldfiles' )) 1824eq ( 1825'Setting v:oldfiles to value with wrong type' , 1826pcall_err ( api . nvim_set_vvar , 'oldfiles' , 'a' ) 1827) 1828eq ( {} , eval ( 'v:oldfiles' )) 1829 1830feed ( 'i foo foo foo<Esc>0/foo<CR>' ) 1831eq ( { 1 , 1 } , api . nvim_win_get_cursor ( 0 )) 1832eq ( 1 , eval ( 'v:searchforward' )) 1833feed ( 'n' ) 1834eq ( { 1 , 5 } , api . nvim_win_get_cursor ( 0 )) 1835api . nvim_set_vvar ( 'searchforward' , 0 ) 1836eq ( 0 , eval ( 'v:searchforward' )) 1837feed ( 'n' ) 1838eq ( { 1 , 1 } , api . nvim_win_get_cursor ( 0 )) 1839api . nvim_set_vvar ( 'searchforward' , 1 ) 1840eq ( 1 , eval ( 'v:searchforward' )) 1841feed ( 'n' ) 1842eq ( { 1 , 5 } , api . nvim_win_get_cursor ( 0 )) 1843 1844local screen =Screen . new ( 60 , 3 ) 1845eq ( 1 , eval ( 'v:hlsearch' )) 1846screen : expect { 1847grid =[[ 1848{10:foo} {10:^foo} {10:foo} | 1849{1:~ }| 1850| 1851]] , 1852} 1853api . nvim_set_vvar ( 'hlsearch' , 0 ) 1854eq ( 0 , eval ( 'v:hlsearch' )) 1855screen : expect { 1856grid =[[ 1857foo ^foo foo | 1858{1:~ }| 1859| 1860]] , 1861} 1862api . nvim_set_vvar ( 'hlsearch' , 1 ) 1863eq ( 1 , eval ( 'v:hlsearch' )) 1864screen : expect { 1865grid =[[ 1866{10:foo} {10:^foo} {10:foo} | 1867{1:~ }| 1868| 1869]] , 1870} 1871end ) 1872 1873it ( 'vim_set_var returns the old value' , function () 1874local val1 ={ 1 , 2 , { [ '3' ] =1 } } 1875local val2 ={ 4 , 7 } 1876eq ( NIL , request ( 'vim_set_var' , 'lua' , val1 )) 1877eq ( val1 , request ( 'vim_set_var' , 'lua' , val2 )) 1878end ) 1879 1880it ( 'vim_del_var returns the old value' , function () 1881local val1 ={ 1 , 2 , { [ '3' ] =1 } } 1882local val2 ={ 4 , 7 } 1883eq ( NIL , request ( 'vim_set_var' , 'lua' , val1 )) 1884eq ( val1 , request ( 'vim_set_var' , 'lua' , val2 )) 1885eq ( val2 , request ( 'vim_del_var' , 'lua' )) 1886end ) 1887 1888it ( 'preserves values with NULs in them' , function () 1889api . nvim_set_var ( 'xxx' , 'ab\0cd' ) 1890eq ( 'ab\000cd' , api . nvim_get_var ( 'xxx' )) 1891end ) 1892end ) 1893 1894describe ( 'nvim_get_option_value, nvim_set_option_value' , function () 1895it ( 'works' , function () 1896ok ( api . nvim_get_option_value ( 'equalalways' , {} )) 1897api . nvim_set_option_value ( 'equalalways' , false , {} ) 1898ok ( not api . nvim_get_option_value ( 'equalalways' , {} )) 1899end ) 1900 1901it ( 'Lua funcref RPC value is a `:map`-style "<Lua …>" string' , function () 1902exec_lua ( 'vim.o.operatorfunc = function() end' ) 1903matches ( '^<Lua %d+.*>$' , api . nvim_get_option_value ( 'operatorfunc' , {} )) 1904end ) 1905 1906it ( 'works to get global value of local options' , function () 1907eq ( false , api . nvim_get_option_value ( 'lisp' , {} )) 1908eq ( 8 , api . nvim_get_option_value ( 'shiftwidth' , {} )) 1909end ) 1910 1911it ( 'works to set global value of local options' , function () 1912api . nvim_set_option_value ( 'lisp' , true , { scope ='global' } ) 1913eq ( true , api . nvim_get_option_value ( 'lisp' , { scope ='global' } )) 1914eq ( false , api . nvim_get_option_value ( 'lisp' , {} )) 1915eq ( nil , command_output ( 'setglobal lisp?' ): match ( 'nolisp' )) 1916eq ( 'nolisp' , command_output ( 'setlocal lisp?' ): match ( 'nolisp' )) 1917api . nvim_set_option_value ( 'shiftwidth' , 20 , { scope ='global' } ) 1918eq ( '20' , command_output ( 'setglobal shiftwidth?' ): match ( '%d+' )) 1919eq ( '8' , command_output ( 'setlocal shiftwidth?' ): match ( '%d+' )) 1920end ) 1921 1922it ( 'updates where the option was last set from' , function () 1923api . nvim_set_option_value ( 'equalalways' , false , {} ) 1924local status , rv =pcall ( command_output , 'verbose set equalalways?' ) 1925eq ( true , status ) 1926matches ( 'noequalalways\n' .. '\tLast set from API client %(channel id %d+%)' , rv ) 1927 1928api . nvim_exec_lua ( 'vim.api.nvim_set_option_value("equalalways", true, {})' , {} ) 1929status , rv =pcall ( command_output , 'verbose set equalalways?' ) 1930eq ( true , status ) 1931eq ( ' equalalways\n\tLast set from Lua (run Nvim with -V1 for more details)' , rv ) 1932end ) 1933 1934it ( 'updates whether the option has ever been set #25025' , function () 1935eq ( false , api . nvim_get_option_info2 ( 'autochdir' , {} ). was_set ) 1936api . nvim_set_option_value ( 'autochdir' , true , {} ) 1937eq ( true , api . nvim_get_option_info2 ( 'autochdir' , {} ). was_set ) 1938 1939eq ( false , api . nvim_get_option_info2 ( 'cmdwinheight' , {} ). was_set ) 1940api . nvim_set_option_value ( 'cmdwinheight' , 10 , {} ) 1941eq ( true , api . nvim_get_option_info2 ( 'cmdwinheight' , {} ). was_set ) 1942 1943eq ( false , api . nvim_get_option_info2 ( 'debug' , {} ). was_set ) 1944api . nvim_set_option_value ( 'debug' , 'beep' , {} ) 1945eq ( true , api . nvim_get_option_info2 ( 'debug' , {} ). was_set ) 1946end ) 1947 1948it ( 'validation' , function () 1949eq ( "Unknown option 'foobar'" , pcall_err ( api . nvim_set_option_value , 'foobar' , 'baz' , {} )) 1950eq ( 1951"Unknown option 'foobar'" , 1952pcall_err ( api . nvim_set_option_value , 'foobar' , 'baz' , { win =api . nvim_get_current_win () } ) 1953) 1954eq ( 1955"Invalid 'scope': expected 'local' or 'global'" , 1956pcall_err ( api . nvim_get_option_value , 'scrolloff' , { scope ='bogus' } ) 1957) 1958eq ( 1959"Invalid 'scope': expected 'local' or 'global'" , 1960pcall_err ( api . nvim_set_option_value , 'scrolloff' , 1 , { scope ='bogus' } ) 1961) 1962eq ( 1963"Invalid 'scope': expected String, got Integer" , 1964pcall_err ( api . nvim_get_option_value , 'scrolloff' , { scope =42 } ) 1965) 1966matches ( 1967"Invalid 'scrolloff': expected a valid type, got Array" , 1968pcall_err ( api . nvim_set_option_value , 'scrolloff' , {} , {} ) 1969) 1970matches ( 1971"Invalid 'scrolloff': expected a valid type, got Boolean" , 1972pcall_err ( api . nvim_set_option_value , 'scrolloff' , true , {} ) 1973) 1974matches ( 1975"Invalid 'scrolloff': expected a valid type, got String" , 1976pcall_err ( api . nvim_set_option_value , 'scrolloff' , 'wrong' , {} ) 1977) 1978local tab1 =api . nvim_get_current_tabpage () 1979eq ( 1980"Conflict: 'tab' not allowed with 'win', 'buf', 'filetype' or 'scope'" , 1981pcall_err ( 1982api . nvim_get_option_value , 1983'cmdheight' , 1984{ tab =tab1 , win =api . nvim_get_current_win () } 1985) 1986) 1987eq ( 1988"Conflict: 'tab' not allowed with 'win', 'buf', 'filetype' or 'scope'" , 1989pcall_err ( api . nvim_get_option_value , 'cmdheight' , { 1990tab =tab1 , 1991scope ='local' , 1992} ) 1993) 1994eq ( 1995"Conflict: 'tab' not allowed with 'shiftwidth'" , 1996pcall_err ( api . nvim_get_option_value , 'shiftwidth' , { tab =tab1 } ) 1997) 1998eq ( 1999"Conflict: 'tab' not allowed with 'shiftwidth'" , 2000pcall_err ( api . nvim_set_option_value , 'shiftwidth' , 2 , { tab =tab1 } ) 2001) 2002eq ( 2003"Conflict: 'tab' not allowed with this function" , 2004pcall_err ( api . nvim_get_option_info2 , 'cmdheight' , { tab =tab1 } ) 2005) 2006eq ( 2007"Conflict: 'filetype' not allowed with 'scope', 'buf', 'win' or 'tab'" , 2008pcall_err ( api . nvim_get_option_value , 'cmdheight' , { filetype ='c' , tab =0 } ) 2009) 2010eq ( 2011"Invalid 'operation': expected 'set', 'append', 'prepend', or 'remove'" , 2012pcall_err ( api . nvim_set_option_value , 'wildignore' , { 'x' } , { operation ='bogus' } ) 2013) 2014eq ( 2015"Conflict: 'append' not allowed with boolean options" , 2016pcall_err ( api . nvim_set_option_value , 'number' , true , { operation ='append' } ) 2017) 2018end ) 2019 2020it ( "tabpage-local option ('cmdheight') #31140" , function () 2021api . nvim_set_option_value ( 'cmdheight' , 1 , {} ) 2022local tab1 =api . nvim_get_current_tabpage () 2023eq ( 1 , api . nvim_get_option_value ( 'cmdheight' , { tab =0 } )) 2024eq ( 1 , api . nvim_get_option_value ( 'cmdheight' , { tab =tab1 } )) 2025eq ( 1 , api . nvim_get_option_value ( 'cmdheight' , {} )) 2026command ( 'tabnew' ) 2027local tab2 =api . nvim_get_current_tabpage () 2028api . nvim_set_option_value ( 'cmdheight' , 4 , {} ) 2029eq ( 4 , api . nvim_get_option_value ( 'cmdheight' , { tab =tab2 } )) 2030eq ( 1 , api . nvim_get_option_value ( 'cmdheight' , { tab =tab1 } )) 2031eq ( 4 , api . nvim_get_option_value ( 'cmdheight' , {} )) 2032 2033-- Set non-current tab option. 2034api . nvim_set_option_value ( 'cmdheight' , 3 , { tab =tab1 } ) 2035eq ( 3 , api . nvim_get_option_value ( 'cmdheight' , { tab =tab1 } )) 2036eq ( 4 , api . nvim_get_option_value ( 'cmdheight' , { tab =tab2 } )) 2037eq ( 4 , api . nvim_get_option_value ( 'cmdheight' , {} )) 2038command ( 'tabnext' ) 2039eq ( 3 , api . nvim_get_option_value ( 'cmdheight' , {} )) 2040end ) 2041 2042it ( 'can get local values when global value is set' , function () 2043eq ( 0 , api . nvim_get_option_value ( 'scrolloff' , {} )) 2044eq ( - 1 , api . nvim_get_option_value ( 'scrolloff' , { scope ='local' } )) 2045end ) 2046 2047it ( 'can set global and local values' , function () 2048api . nvim_set_option_value ( 'makeprg' , 'hello' , {} ) 2049eq ( 'hello' , api . nvim_get_option_value ( 'makeprg' , {} )) 2050eq ( '' , api . nvim_get_option_value ( 'makeprg' , { scope ='local' } )) 2051api . nvim_set_option_value ( 'makeprg' , 'world' , { scope ='local' } ) 2052eq ( 'world' , api . nvim_get_option_value ( 'makeprg' , { scope ='local' } )) 2053api . nvim_set_option_value ( 'makeprg' , 'goodbye' , { scope ='global' } ) 2054eq ( 'goodbye' , api . nvim_get_option_value ( 'makeprg' , { scope ='global' } )) 2055api . nvim_set_option_value ( 'makeprg' , 'hello' , {} ) 2056eq ( 'hello' , api . nvim_get_option_value ( 'makeprg' , { scope ='global' } )) 2057eq ( 'hello' , api . nvim_get_option_value ( 'makeprg' , {} )) 2058eq ( '' , api . nvim_get_option_value ( 'makeprg' , { scope ='local' } )) 2059end ) 2060 2061it ( 'clears the local value of an option with nil' , function () 2062-- Set global value 2063api . nvim_set_option_value ( 'shiftwidth' , 42 , {} ) 2064eq ( 42 , api . nvim_get_option_value ( 'shiftwidth' , {} )) 2065 2066-- Set local value 2067api . nvim_set_option_value ( 'shiftwidth' , 8 , { scope ='local' } ) 2068eq ( 8 , api . nvim_get_option_value ( 'shiftwidth' , {} )) 2069eq ( 8 , api . nvim_get_option_value ( 'shiftwidth' , { scope ='local' } )) 2070eq ( 42 , api . nvim_get_option_value ( 'shiftwidth' , { scope ='global' } )) 2071 2072-- Clear value without scope 2073api . nvim_set_option_value ( 'shiftwidth' , NIL , {} ) 2074eq ( 42 , api . nvim_get_option_value ( 'shiftwidth' , {} )) 2075eq ( 42 , api . nvim_get_option_value ( 'shiftwidth' , { scope ='local' } )) 2076 2077-- Clear value with explicit scope 2078api . nvim_set_option_value ( 'shiftwidth' , 8 , { scope ='local' } ) 2079api . nvim_set_option_value ( 'shiftwidth' , NIL , { scope ='local' } ) 2080eq ( 42 , api . nvim_get_option_value ( 'shiftwidth' , {} )) 2081eq ( 42 , api . nvim_get_option_value ( 'shiftwidth' , { scope ='local' } )) 2082 2083-- Now try with options with a special "local is unset" value (e.g. 'undolevels') 2084api . nvim_set_option_value ( 'undolevels' , 1000 , {} ) 2085api . nvim_set_option_value ( 'undolevels' , 1200 , { scope ='local' } ) 2086eq ( 1200 , api . nvim_get_option_value ( 'undolevels' , { scope ='local' } )) 2087api . nvim_set_option_value ( 'undolevels' , NIL , { scope ='local' } ) 2088eq ( - 123456 , api . nvim_get_option_value ( 'undolevels' , { scope ='local' } )) 2089eq ( 1000 , api . nvim_get_option_value ( 'undolevels' , {} )) 2090 2091api . nvim_set_option_value ( 'autoread' , true , {} ) 2092api . nvim_set_option_value ( 'autoread' , false , { scope ='local' } ) 2093eq ( false , api . nvim_get_option_value ( 'autoread' , { scope ='local' } )) 2094api . nvim_set_option_value ( 'autoread' , NIL , { scope ='local' } ) 2095eq ( NIL , api . nvim_get_option_value ( 'autoread' , { scope ='local' } )) 2096eq ( true , api . nvim_get_option_value ( 'autoread' , {} )) 2097end ) 2098 2099it ( 'set window options' , function () 2100api . nvim_set_option_value ( 'colorcolumn' , '4,3' , {} ) 2101eq ( '4,3' , api . nvim_get_option_value ( 'colorcolumn' , { scope ='local' } )) 2102command ( 'set modified hidden' ) 2103command ( 'enew' ) -- edit new buffer, window option is preserved 2104eq ( '4,3' , api . nvim_get_option_value ( 'colorcolumn' , { scope ='local' } )) 2105end ) 2106 2107it ( 'set local window options' , function () 2108api . nvim_set_option_value ( 'colorcolumn' , '4,3' , { win =0 , scope ='local' } ) 2109eq ( '4,3' , api . nvim_get_option_value ( 'colorcolumn' , { win =0 , scope ='local' } )) 2110command ( 'set modified hidden' ) 2111command ( 'enew' ) -- edit new buffer, window option is reset 2112eq ( '' , api . nvim_get_option_value ( 'colorcolumn' , { win =0 , scope ='local' } )) 2113end ) 2114 2115it ( 'get buffer or window-local options' , function () 2116command ( 'new' ) 2117local buf =api . nvim_get_current_buf () 2118api . nvim_set_option_value ( 'tagfunc' , 'foobar' , { buf =buf } ) 2119eq ( 'foobar' , api . nvim_get_option_value ( 'tagfunc' , { buf =buf } )) 2120 2121local win =api . nvim_get_current_win () 2122api . nvim_set_option_value ( 'number' , true , { win =win } ) 2123eq ( true , api . nvim_get_option_value ( 'number' , { win =win } )) 2124end ) 2125 2126it ( 'getting current buffer option does not adjust cursor #19381' , function () 2127command ( 'new' ) 2128local buf =api . nvim_get_current_buf () 2129local win =api . nvim_get_current_win () 2130insert ( 'some text' ) 2131feed ( '0v$' ) 2132eq ( { 1 , 9 } , api . nvim_win_get_cursor ( win )) 2133api . nvim_get_option_value ( 'filetype' , { buf =buf } ) 2134eq ( { 1 , 9 } , api . nvim_win_get_cursor ( win )) 2135end ) 2136 2137it ( 'can get default option values for filetypes' , function () 2138command ( 'filetype plugin on' ) 2139for ft , opts in pairs { 2140lua ={ commentstring ='-- %s' } , 2141vim ={ commentstring ='"%s' } , 2142man ={ tagfunc ="v:lua.require'man'.goto_tag" } , 2143xml ={ formatexpr ='xmlformat#Format()' } , 2144} do 2145for option , value in pairs ( opts ) do 2146eq ( value , api . nvim_get_option_value ( option , { filetype =ft } )) 2147end 2148end 2149 2150command 'au FileType lua ++once setlocal commentstring=NEW\\ %s' 2151eq ( 'NEW %s' , api . nvim_get_option_value ( 'commentstring' , { filetype ='lua' } )) 2152 2153-- Works from within a FileType autocommand fired from setting the &filetype. 2154exec [[ 2155au FileType * ++once let g:value = nvim_get_option_value('commentstring', #{filetype: 'vim'}) 2156set commentstring= ft=lua 2157]] 2158eq ( '"%s' , eval ( 'g:value' )) 2159-- Check it didn't somehow mess up the &commentstring from setting the &filetype. 2160eq ( '-- %s' , eval ( '&commentstring' )) 2161 2162-- Not possible to recurse endlessly, of course. 2163exec [[ 2164au FileType foobar call nvim_get_option_value('commentstring', #{filetype: 'foobar'}) 2165]] 2166matches ( -- Watch out - this error is large! 2167[[E5555: API call: Vim:E218: Autocommand nesting too deep$]] , 2168pcall_err ( command , 'set ft=foobar' ) 2169) 2170command ( 'au! FileType foobar' ) 2171 2172eq ( 2173[[Vim(call):E5555: API call: Could not execute FileType autocommands]] , 2174pcall_err ( command , "noautocmd call nvim_get_option_value('tagfunc', #{filetype: 'man'})" ) 2175) 2176 2177-- No error if executed with no FileType autocommands defined. 2178-- Returning the copied global value will continue to suffice, I guess. 2179command ( [[filetype plugin off | setglobal commentstring=<><\ %s\ ><>]] ) 2180eq ( {} , api . nvim_get_autocmds { event ='FileType' } ) 2181eq ( '<>< %s ><>' , api . nvim_get_option_value ( 'commentstring' , { filetype ='lua' } )) 2182end ) 2183 2184it ( 'errors for bad FileType autocmds' , function () 2185command 'au FileType lua setlocal commentstring=BAD' 2186eq ( 2187[[FileType Autocommands for "lua": Vim(setlocal):E537: 'commentstring' must be empty or contain %s: commentstring=BAD]] , 2188pcall_err ( api . nvim_get_option_value , 'commentstring' , { filetype ='lua' } ) 2189) 2190end ) 2191 2192it ( "value of 'modified' is always false for scratch buffers" , function () 2193api . nvim_set_current_buf ( api . nvim_create_buf ( true , true )) 2194insert ( [[ 2195foo 2196bar 2197baz 2198]] ) 2199eq ( false , api . nvim_get_option_value ( 'modified' , {} )) 2200end ) 2201 2202it ( 'errors if autocmds wipe the dummy buffer' , function () 2203-- Wipe the dummy buffer. This will throw E813, but the buffer will still be wiped; check that 2204-- such errors from setting the filetype have priority. 2205command 'autocmd FileType * ++once bwipeout!' 2206eq ( 2207'FileType Autocommands for "*": Vim(bwipeout):E813: Cannot close autocmd window' , 2208pcall_err ( api . nvim_get_option_value , 'formatexpr' , { filetype ='lua' } ) 2209) 2210 2211-- Silence E813 to check that the error for wiping the dummy buffer is set. 2212command 'autocmd FileType * ++once silent! bwipeout!' 2213eq ( 2214'Internal buffer was deleted' , 2215pcall_err ( api . nvim_get_option_value , 'formatexpr' , { filetype ='lua' } ) 2216) 2217end ) 2218 2219it ( 'does not crash if autocmds open dummy buffer in other windows' , function () 2220exec [[ 2221autocmd FileType * ++once let g:dummy_buf = bufnr() | split 2222 2223" Autocommands should be blocked while Nvim attempts to wipe the buffer. 2224let g:wipe_events = [] 2225autocmd WinClosed * if winbufnr(expand('<amatch>')) == g:dummy_buf 2226\| let g:wipe_events += ['WinClosed'] 2227\| endif 2228autocmd BufWipeout * if expand('<abuf>') == g:dummy_buf 2229\| let g:wipe_events += ['BufWipeout'] 2230\| endif 2231]] 2232api . nvim_get_option_value ( 'formatexpr' , { filetype ='lua' } ) 2233eq ( 0 , eval ( 'bufexists(g:dummy_buf)' )) 2234eq ( {} , eval ( 'win_findbuf(g:dummy_buf)' )) 2235eq ( {} , eval ( 'g:wipe_events' )) 2236 2237-- Be an ABSOLUTE nuisance and make it the only window to prevent it from wiping. 2238-- Do it this way to avoid E813 from :only trying to close the autocmd window. 2239command ( 'autocmd FileType * ++once let g:dummy_buf = bufnr() | split | wincmd w | quit' ) 2240api . nvim_get_option_value ( 'formatexpr' , { filetype ='lua' } ) 2241eq ( 1 , eval ( 'bufexists(g:dummy_buf)' )) 2242 2243-- Ensure the buffer does not remain as a dummy by checking that we can switch to it. 2244local old_win =api . nvim_get_current_win () 2245command ( 'execute g:dummy_buf "sbuffer"' ) 2246eq ( eval ( 'g:dummy_buf' ), api . nvim_get_current_buf ()) 2247neq ( old_win , api . nvim_get_current_win ()) 2248eq ( {} , eval ( 'g:wipe_events' )) 2249end ) 2250 2251it ( 'does not crash if dummy buffer wiped after autocommands' , function () 2252-- Autocommands are blocked while Nvim attempts to wipe the buffer, but check something like 2253-- &bufhidden = "wipe" causing a premature wipe doesn't crash. 2254command ( 'autocmd FileType * ++once setlocal bufhidden=wipe | split' ) 2255api . nvim_get_option_value ( 'formatexpr' , { filetype ='lua' } ) 2256assert_alive () 2257end ) 2258 2259it ( 'sets dummy buffer options without side-effects' , function () 2260exec [[ 2261let g:events = [] 2262autocmd OptionSet * let g:events += [expand("<amatch>")] 2263autocmd FileType * ++once let g:bufhidden = &l:bufhidden 2264\| let g:buftype = &l:buftype 2265\| let g:swapfile = &l:swapfile 2266\| let g:modeline = &l:modeline 2267\| let g:bufloaded = bufloaded(bufnr()) 2268]] 2269api . nvim_get_option_value ( 'formatexpr' , { filetype ='lua' } ) 2270eq ( {} , eval ( 'g:events' )) 2271eq ( 'hide' , eval ( 'g:bufhidden' )) 2272eq ( 'nofile' , eval ( 'g:buftype' )) 2273eq ( 0 , eval ( 'g:swapfile' )) 2274eq ( 0 , eval ( 'g:modeline' )) 2275eq ( 1 , eval ( 'g:bufloaded' )) 2276end ) 2277 2278it ( 'expands env vars' , function () 2279-- I don't know why setting vim.env.INCLUDE doesn't work here. 2280clear { env ={ INCLUDE ='/dev/null' } } 2281api . nvim_set_option_value ( 'path' , '$INCLUDE' , {} ) 2282eq ( '/dev/null' , api . nvim_get_option_value ( 'path' , {} )) 2283end ) 2284 2285it ( 'expands ~' , function () 2286clear { env ={ HOME ='/dev/null' } } 2287api . nvim_set_option_value ( 'rtp' , '~' , {} ) 2288eq ( '/dev/null' , api . nvim_get_option_value ( 'rtp' , {} )) 2289end ) 2290 2291it ( 'allows setting, appending, prepending, and removing lists' , function () 2292api . nvim_set_option_value ( 'wildignore' , { '*.o' , '*.obj' } , {} ) 2293eq ( '*.o,*.obj' , api . nvim_get_option_value ( 'wildignore' , {} )) 2294 2295api . nvim_set_option_value ( 'wildignore' , { '*.a' , '*.b' , '*.c' } , { operation ='append' } ) 2296eq ( '*.o,*.obj,*.a,*.b,*.c' , api . nvim_get_option_value ( 'wildignore' , {} )) 2297 2298api . nvim_set_option_value ( 'wildignore' , { '1' , '2' , '3' } , { operation ='prepend' } ) 2299eq ( '1,2,3,*.o,*.obj,*.a,*.b,*.c' , api . nvim_get_option_value ( 'wildignore' , {} )) 2300 2301-- NOTE: you can't remove something like { '1', '*.obj' } because lists 2302-- only support removing items in order. Behavior matches :set-= 2303api . nvim_set_option_value ( 'wildignore' , { '1' , '2' , '3' } , { operation ='remove' } ) 2304eq ( '*.o,*.obj,*.a,*.b,*.c' , api . nvim_get_option_value ( 'wildignore' , {} )) 2305 2306api . nvim_set_option_value ( 'wildignore' , {} , {} ) 2307eq ( '' , api . nvim_get_option_value ( 'wildignore' , {} )) 2308end ) 2309 2310it ( 'preserves :setlocal trust semantics when merging options' , function () 2311local path =tmpname ( false ) 2312finally ( function () 2313os . remove ( path ) 2314end ) 2315 2316local expr ="writefile(['foldexpr'], " .. fn . string ( path ) .. ')' 2317api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'one' , 'two' } ) 2318 2319-- Match :setlocal +=, ^=, and -=: retained expression text must stay sandboxed. 2320for _ , case in ipairs ( { 2321{ 'append' , '+0' } , 2322{ 'prepend' , '0+' } , 2323{ 'remove' , '+0' } , 2324} ) do 2325local operation , value =unpack ( case ) 2326 2327command ( 'setlocal foldmethod=manual' ) 2328command ( 'sandbox let &l:foldexpr = ' .. fn . string ( expr .. '+0' )) 2329api . nvim_set_option_value ( 'foldexpr' , value , { scope ='local' , operation =operation } ) 2330 2331command ( 'setlocal foldmethod=expr' ) 2332command ( 'normal! zx' ) 2333eq ( 0 , fn . filereadable ( path )) 2334end 2335 2336-- As with :setlocal =, a full replacement from trusted code must allow the write. 2337api . nvim_set_option_value ( 'foldexpr' , expr , { scope ='local' } ) 2338command ( 'normal! zx' ) 2339eq ( { 'foldexpr' } , fn . readfile ( path )) 2340end ) 2341 2342it ( 'allows setting, appending, prepending, removing dicts' , function () 2343-- NOTE: order is dependent on lua's hash map implementation. I don't 2344-- *think* order matters for the map style options 2345api . nvim_set_option_value ( 'listchars' , { eol ='~' , space ='-' } , {} ) 2346eq ( 'eol:~,space:-' , api . nvim_get_option_value ( 'listchars' , {} )) 2347 2348api . nvim_set_option_value ( 2349'listchars' , 2350{ multispace ='---+' , tab ='>-' } , 2351{ operation ='append' } 2352) 2353eq ( 'eol:~,space:-,multispace:---+,tab:>-' , api . nvim_get_option_value ( 'listchars' , {} )) 2354 2355api . nvim_set_option_value ( 'listchars' , { lead ='.' } , { operation ='prepend' } ) 2356eq ( 'lead:.,eol:~,space:-,multispace:---+,tab:>-' , api . nvim_get_option_value ( 'listchars' , {} )) 2357 2358api . nvim_set_option_value ( 2359'listchars' , 2360{ lead ='' , multispace ='' } , 2361{ operation ='remove' } 2362) 2363eq ( 'eol:~,space:-,tab:>-' , api . nvim_get_option_value ( 'listchars' , {} )) 2364 2365-- A bare key (string or 1-item list) removes the matching "key:value" item by key. 2366api . nvim_set_option_value ( 'listchars' , { 'space' } , { operation ='remove' } ) 2367eq ( 'eol:~,tab:>-' , api . nvim_get_option_value ( 'listchars' , {} )) 2368 2369api . nvim_set_option_value ( 'listchars' , vim . empty_dict (), {} ) 2370eq ( '' , api . nvim_get_option_value ( 'listchars' , {} )) 2371end ) 2372 2373it ( 'returns the new option value' , function () 2374eq ( 2375{ eol ='~' , space ='-' } , 2376api . nvim_set_option_value ( 'listchars' , { eol ='~' , space ='-' } , {} ) 2377) 2378eq ( 'eol:~,space:-' , api . nvim_get_option_value ( 'listchars' , {} )) 2379eq ( 2380{ eol ='~' , space ='-' , tab ='>-' } , 2381api . nvim_set_option_value ( 'listchars' , { tab ='>-' } , { operation ='append' } ) 2382) 2383eq ( 'eol:~,space:-,tab:>-' , api . nvim_get_option_value ( 'listchars' , {} )) 2384 2385-- Scalar option types return their plain value. 2386eq ( 7 , api . nvim_set_option_value ( 'scrolloff' , 7 , {} )) 2387eq ( true , api . nvim_set_option_value ( 'number' , true , {} )) 2388eq ( false , api . nvim_set_option_value ( 'number' , false , {} )) 2389eq ( 'foo' , api . nvim_set_option_value ( 'makeprg' , 'foo' , {} )) 2390end ) 2391 2392it ( "dry_run doesn't update option value" , function () 2393local listchars =api . nvim_get_option_value ( 'listchars' , {} ) 2394eq ( 2395{ eol ='~' , space ='-' } , 2396api . nvim_set_option_value ( 'listchars' , { eol ='~' , space ='-' } , { dry_run =true } ) 2397) 2398eq ( listchars , api . nvim_get_option_value ( 'listchars' , {} )) 2399 2400-- dry_run also applies to operations: returns the merged result (5 + 10) without setting it. 2401api . nvim_set_option_value ( 'scrolloff' , 5 , {} ) 2402eq ( 15 , api . nvim_set_option_value ( 'scrolloff' , 10 , { operation ='append' , dry_run =true } )) 2403eq ( 5 , api . nvim_get_option_value ( 'scrolloff' , {} )) 2404end ) 2405 2406it ( 'dry runs reject invalid values with the same errors as assignments' , function () 2407api . nvim_set_option_value ( 'foldlevel' , 2 , {} ) 2408api . nvim_set_option_value ( 'backupext' , '.bak' , {} ) 2409api . nvim_set_option_value ( 'patchmode' , '.orig' , {} ) 2410for _ , case in ipairs ( { 2411{ 'foldmethod' , 'nonsense' , 'E474:' } , 2412{ 'foldcolumn' , 'auto:0' , 'E474:' } , 2413{ 'foldmarker' , 'broken' , 'E536:' } , 2414{ 'foldmarker' , ',}}}' , 'E474:' } , 2415{ 'foldmarker' , '{{{,' , 'E474:' } , 2416{ 'background' , 'nonsense' , 'E474:' } , 2417{ 'fileformat' , 'nonsense' , 'E474:' } , 2418{ 'fileformats' , 'unix,nonsense' , 'E474:' } , 2419{ 'comments' , 'b' , 'E524:' } , 2420{ 'comments' , 'b:' , 'E525:' } , 2421{ 'commentstring' , 'hello' , 'E537:' } , 2422{ 'eventignore' , 'NotAnEvent' , 'E474:' } , 2423{ 'eventignorewin' , 'NotAnEvent' , 'E474:' } , 2424{ 'eventignorewin' , 'VimEnter' , 'E474:' } , 2425{ 'helplang' , 'eng' , 'E474:' } , 2426{ 'matchpairs' , '(:' , 'E474:' } , 2427{ 'shellpipe' , '%x' , 'E1577:' } , 2428{ 'shellredir' , '%s %s' , 'E1577:' } , 2429{ 'showbreak' , '界' , 'E595:' } , 2430{ 'winborder' , 'nonsense' , 'E474:' } , 2431{ 'pumborder' , 'nonsense' , 'E474:' } , 2432-- Leading dots are ignored when comparing backup extensions. 2433{ 'backupext' , 'orig' , 'E589:' } , 2434{ 'patchmode' , 'bak' , 'E589:' } , 2435{ 'highlight' , '' , 'E519:' } , 2436{ 'filetype' , 'bad/name' , 'E474:' } , 2437{ 'syntax' , 'bad/name' , 'E474:' } , 2438{ 'keymap' , 'bad/name' , 'E474:' } , 2439{ 'isident' , '256' , 'E474:' } , 2440{ 'iskeyword' , '256' , 'E474:' } , 2441{ 'isfname' , '256' , 'E474:' } , 2442{ 'isprint' , '256' , 'E519:' } , 2443{ 'spelllang' , 'en/gb' , 'E474:' } , 2444{ 'spellfile' , 'words.txt' , 'E474:' } , 2445{ 'complete' , 'x' , 'E539:' } , 2446{ 'complete' , '.^x' , 'E535:' } , 2447{ 'mkspellmem' , '1,2,3' , 'E474:' } , 2448{ 'buftype' , 'terminal' , 'E474:' } , 2449{ 'shada' , ':' , 'E526:' } , 2450{ 'shada' , ':1' , 'E528:' } , 2451{ 'shada' , "nfile,'100" , 'E528:' } , -- The apostrophe is part of the filename. 2452{ 'wildchar' , 3 , 'E474:' } , -- CTRL-C. 2453{ 'wildcharm' , 13 , 'E474:' } , -- Enter. 2454{ 'colorcolumn' , 'bad' , 'E474:' } , 2455{ 'statusline' , '%^' , 'E539:' } , 2456{ 'statusline' , '%(' , 'E542:' } , 2457{ 'statuscolumn' , '%(' , 'E542:' } , 2458{ 'winbar' , '%(' , 'E542:' } , 2459{ 'tabline' , '%(' , 'E542:' } , 2460{ 'rulerformat' , '%(' , 'E542:' } , 2461{ 'listchars' , 'eol:xx' , 'E1511:' } , 2462{ 'fillchars' , 'fold:xx' , 'E1511:' } , 2463{ 'mouse' , '?' , 'E539:' } , 2464{ 'backupcopy' , 'yes,no' , 'E474:' } , 2465{ 'sessionoptions' , 'curdir,sesdir' , 'E474:' } , 2466{ 'compatible' , true , 'E519:' } , 2467{ 'foldnestmax' , 2147483648 , 'E474:' } , 2468-- Schemas apply to strings and structured input; validate after merging. 2469{ 'breakindentopt' , 'shift:x' , "E474: 'shift' requires a number" } , 2470{ 'diffopt' , { algorithm ='bogus' } , "E474: 'algorithm' must be one of:" } , 2471{ 'messagesopt' , 'history:x' , "E474: 'history' requires a number" } , 2472{ 'mousescroll' , '' , 'E474:' } , 2473{ 'mousescroll' , 'ver:x' , "E474: 'ver' requires a number" } , 2474{ 'mousescroll' , { bogus =1 } , "E474: Unknown item 'bogus'" , 'append' } , 2475{ 'previewpopup' , 'height:0' , 'E474:' } , 2476{ 'previewpopup' , { width =0 } , 'E474:' } , 2477{ 'foldopen' , 'nonsense' , 'E474:' , 'append' } , 2478{ 'foldmethod' , 'manual' , 'E474:' , 'remove' } , 2479{ 'foldlevel' , 3 , 'E487:' , 'remove' } , 2480} ) do 2481local name , value , error , operation =unpack ( case ) 2482local opts ={ operation =operation or 'set' , dry_run =true } 2483local before =api . nvim_get_option_value ( name , {} ) 2484local dry_error =pcall_err ( api . nvim_set_option_value , name , value , opts ) 2485matches ( error , dry_error ) 2486opts . dry_run =false 2487eq ( dry_error , pcall_err ( api . nvim_set_option_value , name , value , opts )) 2488eq ( before , api . nvim_get_option_value ( name , {} )) 2489end 2490end ) 2491 2492it ( 'dry runs validate the result of removal, not the removed item' , function () 2493api . nvim_set_option_value ( 'mousescroll' , { hor =3 , ver =2 } , {} ) 2494eq ( 2495{ hor ='3' , ver ='2' } , 2496api . nvim_set_option_value ( 2497'mousescroll' , 2498{ bogus =1 } , 2499{ operation ='remove' , dry_run =true } 2500) 2501) 2502end ) 2503 2504it ( 'dry runs and assignments accept valid edge cases' , function () 2505for _ , case in ipairs ( { 2506{ 'backspace' , '2' } , -- Legacy numeric spelling. 2507{ 'eventignore' , 'VimEnter' } , -- Allowed globally, but not in eventignorewin. 2508{ 'iskeyword' , '' } , -- An empty character list is allowed. 2509{ 'winborder' , '+,-,+,|,+,-,+,|' } , -- Custom border characters. 2510{ 'complete' , '.^2,w' } , -- A source can have a completion limit. 2511{ 'mkspellmem' , '1000,50,10' } , -- Memory and word-count limits. 2512{ 'shada' , "'0" } , -- Zero disables file marks. 2513{ 'lispoptions' , '' } , -- Empty is allowed. 2514{ 'signcolumn' , 'auto:1-3' } , -- Range syntax is allowed. 2515{ 'mousescroll' , 'ver:0' } , -- Zero disables vertical scrolling. 2516{ 'previewpopup' , 'height:1,width:1' } , -- Minimum dimensions. 2517} ) do 2518local name , value =unpack ( case ) 2519local dry_value =api . nvim_set_option_value ( name , value , { dry_run =true } ) 2520eq ( dry_value , api . nvim_set_option_value ( name , value , {} )) 2521end 2522end ) 2523 2524it ( 'dry runs do not change which characters belong to words' , function () 2525api . nvim_set_option_value ( 'iskeyword' , '@' , { buf =0 } ) 2526eq ( 'one' , fn . matchstr ( 'one-two' , [[\k\+]] )) 2527api . nvim_set_option_value ( 'iskeyword' , '@,-' , { buf =0 , dry_run =true } ) 2528eq ( 'one' , fn . matchstr ( 'one-two' , [[\k\+]] )) 2529api . nvim_set_option_value ( 'iskeyword' , '@,-' , { buf =0 } ) 2530eq ( 'one-two' , fn . matchstr ( 'one-two' , [[\k\+]] )) 2531end ) 2532 2533it ( 'only successful assignments change the spell suggestion limit' , function () 2534exec_lua ( [[ 2535vim.ui.select = function(items) _G.suggestion_count = #items end 2536]] ) 2537api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'helo' } ) 2538api . nvim_set_option_value ( 'spellsuggest' , 'fast,2' , {} ) 2539command ( 'normal! z=' ) 2540eq ( 2 , exec_lua ( 'return _G.suggestion_count' )) 2541 2542matches ( 'E474:' , pcall_err ( api . nvim_set_option_value , 'spellsuggest' , 'best,fast' , {} )) 2543command ( 'normal! z=' ) 2544eq ( 2 , exec_lua ( 'return _G.suggestion_count' )) 2545 2546api . nvim_set_option_value ( 'spellsuggest' , 'fast,1' , {} ) 2547command ( 'normal! z=' ) 2548eq ( 1 , exec_lua ( 'return _G.suggestion_count' )) 2549end ) 2550 2551it ( 'validates spellfile paths with Windows separators' , function () 2552skip ( not is_os ( 'win' )) 2553-- Backslashes become forward slashes before checking filename characters. 2554api . nvim_set_option_value ( 'isfname' , '@,/,.' , {} ) 2555api . nvim_set_option_value ( 'spellfile' , [[dir\words.add]] , { dry_run =true } ) 2556api . nvim_set_option_value ( 'spellfile' , [[dir\words.add]] , {} ) 2557eq ( 'dir/words.add' , api . nvim_get_option_value ( 'spellfile' , {} )) 2558end ) 2559 2560it ( 'dry runs leave listchars rendering unchanged' , function () 2561local screen =Screen . new ( 20 , 5 ) 2562api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' } ) 2563command ( 'setlocal list listchars=eol:X | redraw' ) 2564eq ( 'X' , fn . screenstring ( 1 , 4 )) 2565eq ( { eol ='Y' } , api . nvim_set_option_value ( 'listchars' , 'eol:Y' , { dry_run =true } )) 2566command ( 'redraw' ) 2567eq ( 'X' , fn . screenstring ( 1 , 4 )) 2568api . nvim_set_option_value ( 'listchars' , 'eol:Y' , {} ) 2569command ( 'redraw' ) 2570eq ( 'Y' , fn . screenstring ( 1 , 4 )) 2571screen : detach () 2572end ) 2573 2574it ( 'dry runs validate against the target buffer and tab' , function () 2575local buf =api . nvim_get_current_buf () 2576command ( 'setlocal nomodifiable previewwindow | new' ) 2577local win =api . nvim_get_current_win () 2578command ( 'tabnew' ) 2579local current =api . nvim_get_current_win () 2580for _ , case in ipairs ( { 2581{ 'fileformat' , 'dos' , { buf =buf } , 'E21:' } , 2582{ 'previewwindow' , true , { win =win } , 'E590:' } , 2583} ) do 2584local name , value , opts , error =unpack ( case ) 2585opts . dry_run =true 2586local dry_error =pcall_err ( api . nvim_set_option_value , name , value , opts ) 2587matches ( error , dry_error ) 2588eq ( current , api . nvim_get_current_win ()) 2589opts . dry_run =false 2590eq ( dry_error , pcall_err ( api . nvim_set_option_value , name , value , opts )) 2591end 2592eq ( false , api . nvim_get_option_value ( 'previewwindow' , { win =win } )) 2593-- A different tab can have its own preview window; the existing one can be reassigned. 2594eq ( true , api . nvim_set_option_value ( 'previewwindow' , true , { win =current , dry_run =true } )) 2595api . nvim_set_option_value ( 'previewwindow' , true , { win =current } ) 2596eq ( true , api . nvim_set_option_value ( 'previewwindow' , true , { win =current , dry_run =true } )) 2597end ) 2598 2599it ( 'buftype checks terminal buffers only for local assignments' , function () 2600local terminal =api . nvim_create_buf ( false , true ) 2601api . nvim_open_term ( terminal , {} ) 2602for _ , dry_run in ipairs ( { true , false } ) do 2603local opts ={ buf =terminal , dry_run =dry_run } 2604matches ( 'E474:' , pcall_err ( api . nvim_set_option_value , 'buftype' , 'nofile' , opts )) 2605eq ( 'terminal' , api . nvim_set_option_value ( 'buftype' , 'terminal' , opts )) 2606eq ( 2607'terminal' , 2608api . nvim_set_option_value ( 'buftype' , 'terminal' , { 2609scope ='global' , 2610dry_run =dry_run , 2611} ) 2612) 2613end 2614end ) 2615 2616it ( 'dry runs return clamped numeric values without storing them' , function () 2617api . nvim_set_option_value ( 'pumblend' , 10 , {} ) 2618eq ( 100 , api . nvim_set_option_value ( 'pumblend' , 150 , { dry_run =true } )) 2619eq ( 10 , api . nvim_get_option_value ( 'pumblend' , {} )) 2620api . nvim_set_option_value ( 'pumblend' , 150 , {} ) 2621eq ( 100 , api . nvim_get_option_value ( 'pumblend' , {} )) 2622end ) 2623 2624it ( 'dry runs leave other windows and their folds unchanged' , function () 2625api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' , 'two' , 'three' } ) 2626command ( '1,3fold' ) 2627exec_lua ( [[ 2628_G.foldexpr_calls = 0 2629vim.wo.foldexpr = function() 2630_G.foldexpr_calls = _G.foldexpr_calls + 1 2631return 0 2632end 2633]] ) 2634local target =api . nvim_get_current_win () 2635command ( 'new' ) 2636local current =api . nvim_get_current_win () 2637exec ( [[ 2638let g:events = 0 2639autocmd OptionSet,WinEnter,WinLeave,BufEnter,BufLeave * let g:events += 1 2640]] ) 2641 2642eq ( 'expr' , api . nvim_set_option_value ( 'foldmethod' , 'expr' , { win =target , dry_run =true } )) 2643eq ( current , api . nvim_get_current_win ()) 2644eq ( 0 , eval ( 'g:events' )) 2645api . nvim_set_current_win ( target ) 2646eq ( 'manual' , api . nvim_get_option_value ( 'foldmethod' , {} )) 2647eq ( 1 , fn . foldclosed ( 1 )) 2648eq ( 0 , exec_lua ( 'return _G.foldexpr_calls' )) 2649end ) 2650 2651it ( 'foldmarker dry runs preserve folds until assignment' , function () 2652api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '{{{' , 'one' , '}}}' , '[[[' , 'two' , ']]]' } ) 2653api . nvim_set_option_value ( 'foldmethod' , 'marker' , { win =0 } ) 2654eq ( 1 , fn . foldlevel ( 2 )) 2655eq ( 0 , fn . foldlevel ( 5 )) 2656eq ( 2657{ '[[[' , ']]]' } , 2658api . nvim_set_option_value ( 'foldmarker' , '[[[,]]]' , { win =0 , dry_run =true } ) 2659) 2660eq ( 1 , fn . foldlevel ( 2 )) 2661eq ( 0 , fn . foldlevel ( 5 )) 2662api . nvim_set_option_value ( 'foldmarker' , '[[[,]]]' , { win =0 } ) 2663eq ( 0 , fn . foldlevel ( 2 )) 2664eq ( 1 , fn . foldlevel ( 5 )) 2665end ) 2666 2667it ( 'dry runs do not change when motions open folds' , function () 2668api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' , 'two' , 'three' } ) 2669command ( '1,3fold' ) 2670api . nvim_set_option_value ( 'foldopen' , '' , {} ) 2671eq ( { 'hor' } , api . nvim_set_option_value ( 'foldopen' , 'hor' , { dry_run =true } )) 2672feed ( 'l' ) 2673eq ( 1 , fn . foldclosed ( 1 )) 2674api . nvim_set_option_value ( 'foldopen' , 'hor' , {} ) 2675feed ( 'l' ) 2676eq ( - 1 , fn . foldclosed ( 1 )) 2677end ) 2678 2679it ( 'dry runs use target window bounds and defaults without switching windows' , function () 2680local target =api . nvim_get_current_win () 2681api . nvim_set_option_value ( 'foldmethod' , 'marker' , { win =target , scope ='global' } ) 2682command ( 'new' ) 2683local current =api . nvim_get_current_win () 2684api . nvim_set_option_value ( 'foldmethod' , 'indent' , { win =current , scope ='global' } ) 2685api . nvim_win_set_height ( target , 4 ) 2686local before =api . nvim_get_option_value ( 'scroll' , { win =target } ) 2687eq ( 8 , api . nvim_set_option_value ( 'scroll' , 8 , { win =current , dry_run =true } )) 2688matches ( 2689'E49:' , 2690pcall_err ( api . nvim_set_option_value , 'scroll' , 8 , { win =target , dry_run =true } ) 2691) 2692eq ( 2 , api . nvim_set_option_value ( 'scroll' , 0 , { win =target , dry_run =true } )) 2693eq ( before , api . nvim_get_option_value ( 'scroll' , { win =target } )) 2694eq ( 'marker' , api . nvim_set_option_value ( 'foldmethod' , NIL , { win =target , dry_run =true } )) 2695eq ( 'manual' , api . nvim_get_option_value ( 'foldmethod' , { win =target } )) 2696eq ( current , api . nvim_get_current_win ()) 2697end ) 2698 2699it ( 'dry runs do not evaluate callback strings' , function () 2700local value ="function('tr', [execute('let g:called = 1')])" 2701eq ( value , api . nvim_set_option_value ( 'operatorfunc' , value , { dry_run =true } )) 2702local complete =api . nvim_get_option_value ( 'complete' , {} ) 2703value ='F' .. value : gsub ( ',' , '\\,' ) 2704api . nvim_set_option_value ( 'complete' , value , { dry_run =true } ) 2705eq ( complete , api . nvim_get_option_value ( 'complete' , {} )) 2706eq ( 0 , fn . exists ( 'g:called' )) 2707eq ( '' , api . nvim_get_option_value ( 'operatorfunc' , {} )) 2708api . nvim_set_option_value ( 'complete' , value , {} ) 2709eq ( 1 , eval ( 'g:called' )) 2710end ) 2711 2712it ( '2zF includes the existing fold and the next line' , function () 2713api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' , 'two' , 'three' , 'four' , 'five' } ) 2714command ( '2,4fold' ) 2715command ( 'setlocal nofoldenable' ) 2716api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 2717 2718-- 2zF turns folding back on. The fold on lines 2-4 counts as the first 2719-- line, and line 5 counts as the second. 2720command ( 'normal! 2zF' ) 2721eq ( 5 , fn . foldclosedend ( 2 )) 2722end ) 2723 2724it ( 'zM, zx and zX reapply an unchanged foldlevel' , function () 2725api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '{{{' , 'one' , '}}}' , 'last' } ) 2726api . nvim_set_option_value ( 'foldmethod' , 'marker' , { win =0 } ) 2727command ( 'normal! zo' ) 2728eq ( - 1 , fn . foldclosed ( 2 )) 2729command ( 'normal! zM' ) 2730eq ( 1 , fn . foldclosed ( 2 )) 2731command ( 'normal! zn' ) 2732eq ( false , api . nvim_get_option_value ( 'foldenable' , { win =0 } )) 2733command ( 'normal! zx' ) 2734eq ( true , api . nvim_get_option_value ( 'foldenable' , { win =0 } )) 2735eq ( - 1 , fn . foldclosed ( 2 )) 2736command ( 'normal! zX' ) 2737eq ( 1 , fn . foldclosed ( 2 )) 2738end ) 2739 2740it ( 'folding commands report option changes through OptionSet' , function () 2741api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' , 'two' , 'three' } ) 2742command ( '1,3fold' ) 2743exec ( [[ 2744let g:options = [] 2745autocmd OptionSet foldlevel,foldenable 2746\ call add(g:options, [expand('<amatch>'), v:option_old, v:option_new]) 2747]] ) 2748command ( 'normal! zr' ) 2749command ( 'normal! zm' ) 2750command ( 'normal! zi' ) 2751eq ( 2752{ { 'foldlevel' , 0 , 1 } , { 'foldlevel' , 1 , 0 } , { 'foldenable' , true , false } } , 2753eval ( 'g:options' ) 2754) 2755end ) 2756 2757it ( 'foldenable setters synchronize diff windows only on assignment' , function () 2758command ( 'setlocal foldmethod=diff scrollbind | split' ) 2759local target =api . nvim_get_current_win () 2760local peer =api . nvim_list_wins ()[ 2 ] 2761command ( 'new' ) 2762local unrelated =api . nvim_get_current_win () 2763command ( 'setlocal foldmethod=manual noscrollbind' ) 2764 2765api . nvim_set_option_value ( 'foldenable' , false , { win =target , dry_run =true } ) 2766eq ( true , api . nvim_get_option_value ( 'foldenable' , { win =target } )) 2767eq ( true , api . nvim_get_option_value ( 'foldenable' , { win =peer } )) 2768 2769api . nvim_set_option_value ( 'foldenable' , false , { win =target } ) 2770eq ( false , api . nvim_get_option_value ( 'foldenable' , { win =peer } )) 2771eq ( true , api . nvim_get_option_value ( 'foldenable' , { win =unrelated } )) 2772 2773api . nvim_set_current_win ( target ) 2774command ( 'normal! zN' ) 2775eq ( true , api . nvim_get_option_value ( 'foldenable' , { win =peer } )) 2776end ) 2777 2778it ( 'cancels a pending fold if OptionSet closes its window' , function () 2779local other =api . nvim_get_current_buf () 2780local lines ={ 'one' , 'two' , 'three' } 2781api . nvim_buf_set_lines ( other , 0 , - 1 , true , lines ) 2782command ( 'setlocal foldmethod=marker | new | setlocal nofoldenable' ) 2783command ( 'autocmd OptionSet foldenable close' ) 2784command ( 'normal! zfj' ) 2785eq ( 1 , # api . nvim_list_wins ()) 2786eq ( lines , api . nvim_buf_get_lines ( other , 0 , - 1 , true )) 2787end ) 2788 2789it ( 'folding commands stop if OptionSet changes their buffer' , function () 2790local other =api . nvim_get_current_buf () 2791api . nvim_set_option_value ( 'foldlevel' , 4 , { win =0 } ) 2792api . nvim_set_current_buf ( api . nvim_create_buf ( true , false )) 2793command ( 'setlocal nofoldenable' ) 2794command ( 'autocmd OptionSet foldenable buffer ' .. other ) 2795command ( 'normal! zM' ) 2796eq ( other , api . nvim_get_current_buf ()) 2797eq ( 4 , api . nvim_get_option_value ( 'foldlevel' , { win =0 } )) 2798end ) 2799 2800it ( 'zx does not open folds in a buffer entered by OptionSet' , function () 2801local other =api . nvim_get_current_buf () 2802api . nvim_buf_set_lines ( other , 0 , - 1 , true , { 'one' , 'two' , 'three' } ) 2803command ( '1,3fold' ) 2804api . nvim_set_current_buf ( api . nvim_create_buf ( true , false )) 2805command ( 'autocmd OptionSet foldlevel buffer ' .. other ) 2806command ( 'normal! zx' ) 2807eq ( other , api . nvim_get_current_buf ()) 2808eq ( 1 , fn . foldclosed ( 1 )) 2809end ) 2810 2811it ( 'merges options against non-current window' , function () 2812api . nvim_set_option_value ( 'listchars' , {} , { operation ='set' } ) 2813 2814local oldwin =fn . win_getid () 2815command ( 'split' ) 2816local curwin =fn . win_getid () 2817neq ( oldwin , curwin ) 2818 2819eq ( 2820{ eol ='~' } , 2821api . nvim_set_option_value ( 2822'listchars' , 2823{ eol ='~' } , 2824{ operation ='append' , win =oldwin } 2825) 2826) 2827eq ( 2828{ tab ='>-' } , 2829api . nvim_set_option_value ( 2830'listchars' , 2831{ tab ='>-' } , 2832{ operation ='append' , win =curwin } 2833) 2834) 2835eq ( 2836{ eol ='~' , space ='-' } , 2837api . nvim_set_option_value ( 2838'listchars' , 2839{ space ='-' } , 2840{ operation ='append' , win =oldwin } 2841) 2842) 2843eq ( 2844{ lead ='.' , tab ='>-' } , 2845api . nvim_set_option_value ( 2846'listchars' , 2847{ lead ='.' } , 2848{ operation ='append' , win =curwin } 2849) 2850) 2851end ) 2852 2853it ( 'merges options against non-current buffer' , function () 2854eq ( {} , api . nvim_set_option_value ( 'completeopt' , {} , { operation ='set' } )) 2855 2856local oldbuf =fn . bufnr () 2857command ( 'new' ) 2858local curbuf =fn . bufnr () 2859neq ( oldbuf , curbuf ) 2860 2861eq ( 2862{ 'fuzzy' } , 2863api . nvim_set_option_value ( 2864'completeopt' , 2865{ 'fuzzy' } , 2866{ operation ='append' , buf =oldbuf } 2867) 2868) 2869eq ( 2870{ 'longest' } , 2871api . nvim_set_option_value ( 2872'completeopt' , 2873{ 'longest' } , 2874{ operation ='append' , buf =curbuf } 2875) 2876) 2877eq ( 2878{ 'fuzzy' , 'menu' } , 2879api . nvim_set_option_value ( 'completeopt' , { 'menu' } , { operation ='append' , buf =oldbuf } ) 2880) 2881eq ( 2882{ 'longest' , 'noinsert' } , 2883api . nvim_set_option_value ( 2884'completeopt' , 2885{ 'noinsert' } , 2886{ operation ='append' , buf =curbuf } 2887) 2888) 2889end ) 2890 2891it ( 'applies operations to number options' , function () 2892-- For number options the operations are arithmetic. `:set {number}+= ^= -=` 2893api . nvim_set_option_value ( 'scrolloff' , 5 , {} ) 2894eq ( 8 , api . nvim_set_option_value ( 'scrolloff' , 3 , { operation ='append' } )) 2895eq ( 8 , api . nvim_get_option_value ( 'scrolloff' , {} )) 2896 2897api . nvim_set_option_value ( 'scrolloff' , 5 , {} ) 2898eq ( 15 , api . nvim_set_option_value ( 'scrolloff' , 3 , { operation ='prepend' } )) 2899eq ( 15 , api . nvim_get_option_value ( 'scrolloff' , {} )) 2900 2901api . nvim_set_option_value ( 'scrolloff' , 5 , {} ) 2902eq ( 3 , api . nvim_set_option_value ( 'scrolloff' , 2 , { operation ='remove' } )) 2903eq ( 3 , api . nvim_get_option_value ( 'scrolloff' , {} )) 2904end ) 2905 2906it ( 'applies operations to flag-list and plain string options' , function () 2907-- Flag-list option (no separator); the return value is the Lua map form. 2908api . nvim_set_option_value ( 'formatoptions' , 'tc' , {} ) 2909eq ( 2910{ t =true , c =true , q =true } , 2911api . nvim_set_option_value ( 'formatoptions' , 'q' , { operation ='append' } ) 2912) 2913eq ( 'tcq' , api . nvim_get_option_value ( 'formatoptions' , {} )) 2914eq ( 2915{ c =true , q =true } , 2916api . nvim_set_option_value ( 'formatoptions' , 't' , { operation ='remove' } ) 2917) 2918eq ( 'cq' , api . nvim_get_option_value ( 'formatoptions' , {} )) 2919 2920-- Plain (non-list) string option: append concatenates with no separator. 2921api . nvim_set_option_value ( 'makeprg' , 'make' , {} ) 2922eq ( 'make!' , api . nvim_set_option_value ( 'makeprg' , '!' , { operation ='append' } )) 2923eq ( 'make!' , api . nvim_get_option_value ( 'makeprg' , {} )) 2924end ) 2925end ) 2926 2927describe ( 'nvim_{get,set}_current_buf, nvim_list_bufs' , function () 2928it ( 'works' , function () 2929eq ( 1 , # api . nvim_list_bufs ()) 2930eq ( api . nvim_list_bufs ()[ 1 ], api . nvim_get_current_buf ()) 2931command ( 'new' ) 2932eq ( 2 , # api . nvim_list_bufs ()) 2933eq ( api . nvim_list_bufs ()[ 2 ], api . nvim_get_current_buf ()) 2934api . nvim_set_current_buf ( api . nvim_list_bufs ()[ 1 ]) 2935eq ( api . nvim_list_bufs ()[ 1 ], api . nvim_get_current_buf ()) 2936end ) 2937end ) 2938 2939describe ( 'nvim_{get,set}_current_win, nvim_list_wins' , function () 2940it ( 'works' , function () 2941eq ( 1 , # api . nvim_list_wins ()) 2942eq ( api . nvim_list_wins ()[ 1 ], api . nvim_get_current_win ()) 2943command ( 'vsplit' ) 2944command ( 'split' ) 2945eq ( 3 , # api . nvim_list_wins ()) 2946eq ( api . nvim_list_wins ()[ 1 ], api . nvim_get_current_win ()) 2947api . nvim_set_current_win ( api . nvim_list_wins ()[ 2 ]) 2948eq ( api . nvim_list_wins ()[ 2 ], api . nvim_get_current_win ()) 2949end ) 2950 2951it ( 'resets Visual mode when switching to different buffer #37072' , function () 2952command ( 'new | wincmd w' ) 2953api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'a' , 'b' } ) 2954api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 2955feed ( '<C-q>' ) 2956eq ( { mode ='\022' , blocking =false } , api . nvim_get_mode ()) 2957api . nvim_set_current_win ( fn . win_getid ( fn . winnr ( '#' ))) 2958eq ( true , pcall ( command , 'redraw' )) 2959end ) 2960 2961it ( 'failure modes' , function () 2962n . command ( 'split' ) 2963 2964eq ( 'Invalid window id: 9999' , pcall_err ( api . nvim_set_current_win , 9999 )) 2965 2966-- XXX: force nvim_set_current_win to fail somehow. 2967n . command ( "au WinLeave * throw 'foo'" ) 2968eq ( 'WinLeave Autocommands for "*": foo' , pcall_err ( api . nvim_set_current_win , 1000 )) 2969end ) 2970end ) 2971 2972describe ( 'nvim_{get,set}_current_tabpage, nvim_list_tabpages' , function () 2973it ( 'works' , function () 2974eq ( 1 , # api . nvim_list_tabpages ()) 2975eq ( api . nvim_list_tabpages ()[ 1 ], api . nvim_get_current_tabpage ()) 2976command ( 'tabnew' ) 2977eq ( 2 , # api . nvim_list_tabpages ()) 2978eq ( 2 , # api . nvim_list_wins ()) 2979eq ( api . nvim_list_wins ()[ 2 ], api . nvim_get_current_win ()) 2980eq ( api . nvim_list_tabpages ()[ 2 ], api . nvim_get_current_tabpage ()) 2981api . nvim_set_current_win ( api . nvim_list_wins ()[ 1 ]) 2982-- Switching window also switches tabpages if necessary 2983eq ( api . nvim_list_tabpages ()[ 1 ], api . nvim_get_current_tabpage ()) 2984eq ( api . nvim_list_wins ()[ 1 ], api . nvim_get_current_win ()) 2985api . nvim_set_current_tabpage ( api . nvim_list_tabpages ()[ 2 ]) 2986eq ( api . nvim_list_tabpages ()[ 2 ], api . nvim_get_current_tabpage ()) 2987eq ( api . nvim_list_wins ()[ 2 ], api . nvim_get_current_win ()) 2988end ) 2989 2990it ( 'failure modes' , function () 2991n . command ( 'tabnew' ) 2992 2993eq ( 'Invalid tabpage id: 999' , pcall_err ( api . nvim_set_current_tabpage , 999 )) 2994 2995-- XXX: force nvim_set_current_tabpage to fail somehow. 2996n . command ( "au TabLeave * throw 'foo'" ) 2997eq ( 'TabLeave Autocommands for "*": foo' , pcall_err ( api . nvim_set_current_tabpage , 1 )) 2998end ) 2999end ) 3000 3001describe ( 'nvim_get_mode' , function () 3002it ( 'during normal-mode `g` returns blocking=true' , function () 3003api . nvim_input ( 'o' ) -- add a line 3004eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 3005api . nvim_input ( [[<C-\><C-N>]] ) 3006eq ( 2 , api . nvim_eval ( "line('.')" )) 3007eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3008 3009api . nvim_input ( 'g' ) 3010eq ( { mode ='n' , blocking =true } , api . nvim_get_mode ()) 3011 3012api . nvim_input ( 'k' ) -- complete the operator 3013eq ( 1 , api . nvim_eval ( "line('.')" )) -- verify the completed operator 3014eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3015end ) 3016 3017it ( 'returns the correct result multiple consecutive times' , function () 3018for _ =1 , 5 do 3019eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3020end 3021api . nvim_input ( 'g' ) 3022for _ =1 , 4 do 3023eq ( { mode ='n' , blocking =true } , api . nvim_get_mode ()) 3024end 3025api . nvim_input ( 'g' ) 3026for _ =1 , 7 do 3027eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3028end 3029end ) 3030 3031it ( 'during normal-mode CTRL-W, returns blocking=true' , function () 3032api . nvim_input ( '<C-W>' ) 3033eq ( { mode ='n' , blocking =true } , api . nvim_get_mode ()) 3034 3035api . nvim_input ( 's' ) -- complete the operator 3036eq ( 2 , api . nvim_eval ( "winnr('$')" )) -- verify the completed operator 3037eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3038end ) 3039 3040it ( 'during hit-enter prompt without UI returns blocking=false' , function () 3041eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3042command ( "echom 'msg1'" ) 3043command ( "echom 'msg2'" ) 3044command ( "echom 'msg3'" ) 3045command ( "echom 'msg4'" ) 3046command ( "echom 'msg5'" ) 3047eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3048api . nvim_input ( ':messages<CR>' ) 3049eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3050end ) 3051 3052it ( 'during hit-enter prompt returns blocking=true' , function () 3053api . nvim_ui_attach ( 80 , 20 , {} ) 3054eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3055command ( "echom 'msg1'" ) 3056command ( "echom 'msg2'" ) 3057command ( "echom 'msg3'" ) 3058command ( "echom 'msg4'" ) 3059command ( "echom 'msg5'" ) 3060eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3061api . nvim_input ( ':messages<CR>' ) 3062eq ( { mode ='r' , blocking =true } , api . nvim_get_mode ()) 3063end ) 3064 3065it ( 'during getchar() returns blocking=false' , function () 3066api . nvim_input ( ':let g:test_input = nr2char(getchar())<CR>' ) 3067-- Events are enabled during getchar(), RPC calls are *not* blocked. #5384 3068eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3069eq ( 0 , api . nvim_eval ( "exists('g:test_input')" )) 3070api . nvim_input ( 'J' ) 3071eq ( 'J' , api . nvim_eval ( 'g:test_input' )) 3072eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3073end ) 3074 3075-- TODO: bug #6247#issuecomment-286403810 3076it ( 'batched with input' , function () 3077api . nvim_ui_attach ( 80 , 20 , {} ) 3078eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3079command ( "echom 'msg1'" ) 3080command ( "echom 'msg2'" ) 3081command ( "echom 'msg3'" ) 3082command ( "echom 'msg4'" ) 3083command ( "echom 'msg5'" ) 3084 3085local req ={ 3086{ 'nvim_get_mode' , {} } , 3087{ 'nvim_input' , { ':messages<CR>' } } , 3088{ 'nvim_get_mode' , {} } , 3089{ 'nvim_eval' , { '1' } } , 3090} 3091eq ( { 3092{ 3093{ mode ='n' , blocking =false } , 309413 , 3095{ mode ='n' , blocking =false } , -- TODO: should be blocked=true ? 30961 , 3097} , 3098NIL , 3099} , api . nvim_call_atomic ( req )) 3100eq ( { mode ='r' , blocking =true } , api . nvim_get_mode ()) 3101end ) 3102it ( 'during insert-mode map-pending, returns blocking=true #6166' , function () 3103command ( 'inoremap xx foo' ) 3104api . nvim_input ( 'ix' ) 3105eq ( { mode ='i' , blocking =true } , api . nvim_get_mode ()) 3106end ) 3107it ( 'during normal-mode gU, returns blocking=false #6166' , function () 3108api . nvim_input ( 'gu' ) 3109eq ( { mode ='no' , blocking =false } , api . nvim_get_mode ()) 3110end ) 3111 3112it ( "at '-- More --' prompt returns blocking=true #11899" , function () 3113command ( 'set more' ) 3114feed ( ':digraphs<cr>' ) 3115eq ( { mode ='rm' , blocking =true } , api . nvim_get_mode ()) 3116end ) 3117 3118it ( 'after <Nop> mapping returns blocking=false #17257' , function () 3119command ( 'nnoremap <F2> <Nop>' ) 3120feed ( '<F2>' ) 3121eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3122end ) 3123 3124it ( 'after empty string <expr> mapping returns blocking=false #17257' , function () 3125command ( 'nnoremap <expr> <F2> ""' ) 3126feed ( '<F2>' ) 3127eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 3128end ) 3129 3130it ( 'returns "c" during number prompt' , function () 3131feed ( 'ifoo<Esc>z=' ) 3132eq ( { mode ='c' , blocking =false } , api . nvim_get_mode ()) 3133end ) 3134end ) 3135 3136describe ( 'RPC (K_EVENT)' , function () 3137it ( 'does not complete ("interrupt") normal-mode operator-pending #6166' , function () 3138n . insert ( [[ 3139FIRST LINE 3140SECOND LINE]] ) 3141api . nvim_input ( 'gg' ) 3142api . nvim_input ( 'gu' ) 3143-- Make any RPC request (can be non-async: op-pending does not block). 3144api . nvim_get_current_buf () 3145-- Buffer should not change. 3146expect ( [[ 3147FIRST LINE 3148SECOND LINE]] ) 3149-- Now send input to complete the operator. 3150api . nvim_input ( 'j' ) 3151expect ( [[ 3152first line 3153second line]] ) 3154end ) 3155 3156it ( 'does not complete ("interrupt") `d` #3732' , function () 3157local screen =Screen . new ( 20 , 4 ) 3158command ( 'set listchars=eol:$' ) 3159command ( 'set list' ) 3160feed ( 'ia<cr>b<cr>c<cr><Esc>kkk' ) 3161feed ( 'd' ) 3162-- Make any RPC request (can be non-async: op-pending does not block). 3163api . nvim_get_current_buf () 3164screen : expect ( [[ 3165^a{1:$} | 3166b{1:$} | 3167c{1:$} | 3168| 3169]] ) 3170end ) 3171 3172it ( 'does not complete ("interrupt") normal-mode map-pending #6166' , function () 3173command ( "nnoremap dd :let g:foo='it worked...'<CR>" ) 3174n . insert ( [[ 3175FIRST LINE 3176SECOND LINE]] ) 3177api . nvim_input ( 'gg' ) 3178api . nvim_input ( 'd' ) 3179-- Make any RPC request (must be async, because map-pending blocks). 3180api . nvim_get_api_info () 3181-- Send input to complete the mapping. 3182api . nvim_input ( 'd' ) 3183expect ( [[ 3184FIRST LINE 3185SECOND LINE]] ) 3186eq ( 'it worked...' , n . eval ( 'g:foo' )) 3187end ) 3188 3189it ( 'does not complete ("interrupt") insert-mode map-pending #6166' , function () 3190command ( 'inoremap xx foo' ) 3191command ( 'set timeoutlen=9999' ) 3192n . insert ( [[ 3193FIRST LINE 3194SECOND LINE]] ) 3195api . nvim_input ( 'ix' ) 3196-- Make any RPC request (must be async, because map-pending blocks). 3197api . nvim_get_api_info () 3198-- Send input to complete the mapping. 3199api . nvim_input ( 'x' ) 3200expect ( [[ 3201FIRST LINE 3202SECOND LINfooE]] ) 3203end ) 3204 3205it ( 'does not interrupt Insert mode i_CTRL-O #10035' , function () 3206feed ( 'iHello World<c-o>' ) 3207eq ( { mode ='niI' , blocking =false } , api . nvim_get_mode ()) -- fast event 3208eq ( 2 , eval ( '1+1' )) -- causes K_EVENT key 3209eq ( { mode ='niI' , blocking =false } , api . nvim_get_mode ()) -- still in ctrl-o mode 3210feed ( 'dd' ) 3211eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) -- left ctrl-o mode 3212expect ( '' ) -- executed the command 3213end ) 3214 3215it ( 'does not interrupt Select mode v_CTRL-O #15688' , function () 3216feed ( 'iHello World<esc>gh<c-o>' ) 3217eq ( { mode ='vs' , blocking =false } , api . nvim_get_mode ()) -- fast event 3218eq ( { mode ='vs' , blocking =false } , api . nvim_get_mode ()) -- again #15288 3219eq ( 2 , eval ( '1+1' )) -- causes K_EVENT key 3220eq ( { mode ='vs' , blocking =false } , api . nvim_get_mode ()) -- still in ctrl-o mode 3221feed ( '^' ) 3222eq ( { mode ='s' , blocking =false } , api . nvim_get_mode ()) -- left ctrl-o mode 3223feed ( 'h' ) 3224eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) -- entered insert mode 3225expect ( 'h' ) -- selection is the whole line and is replaced 3226end ) 3227 3228it ( 'does not interrupt Insert mode i_0_CTRL-D #13997' , function () 3229command ( 'set timeoutlen=9999' ) 3230feed ( 'i<Tab><Tab>a0' ) 3231eq ( 2 , eval ( '1+1' )) -- causes K_EVENT key 3232feed ( '<C-D>' ) 3233expect ( 'a' ) -- recognized i_0_CTRL-D 3234end ) 3235 3236it ( "does not interrupt with 'digraph'" , function () 3237command ( 'set digraph' ) 3238feed ( 'i,' ) 3239eq ( 2 , eval ( '1+1' )) -- causes K_EVENT key 3240feed ( '<BS>' ) 3241eq ( 2 , eval ( '1+1' )) -- causes K_EVENT key 3242feed ( '.' ) 3243expect ( '…' ) -- digraph ",." worked 3244feed ( '<Esc>' ) 3245feed ( ':,' ) 3246eq ( 2 , eval ( '1+1' )) -- causes K_EVENT key 3247feed ( '<BS>' ) 3248eq ( 2 , eval ( '1+1' )) -- causes K_EVENT key 3249feed ( '.' ) 3250eq ( '…' , fn . getcmdline ()) -- digraph ",." worked 3251end ) 3252end ) 3253 3254describe ( 'nvim_get_context' , function () 3255it ( 'validation' , function () 3256eq ( "Invalid key: 'blah'" , pcall_err ( api . nvim_get_context , { blah ={} } )) 3257eq ( 3258"Invalid 'types': expected Array, got Integer" , 3259pcall_err ( api . nvim_get_context , { types =42 } ) 3260) 3261eq ( 3262"Invalid 'type': 'zub'" , 3263pcall_err ( api . nvim_get_context , { types ={ 'jumps' , 'zub' , 'zam' } } ) 3264) 3265end ) 3266it ( 'returns map of current editor state' , function () 3267local opts ={ types ={ 'regs' , 'jumps' , 'bufs' , 'gvars' } } 3268eq ( {} , parse_context ( api . nvim_get_context ( {} ))) 3269 3270feed ( 'i1<cr>2<cr>3<c-[>ddddddqahjklquuu' ) 3271feed ( 'gg' ) 3272feed ( 'G' ) 3273command ( 'edit! BUF1' ) 3274command ( 'edit BUF2' ) 3275api . nvim_set_var ( 'one' , 1 ) 3276api . nvim_set_var ( 'Two' , 2 ) 3277api . nvim_set_var ( 'THREE' , 3 ) 3278 3279local expected_ctx ={ 3280[ 'regs' ] ={ 3281{ [ 'rt' ] =1 , [ 'rc' ] ={ '1' } , [ 'n' ] =49 , [ 'ru' ] =true } , 3282{ [ 'rt' ] =1 , [ 'rc' ] ={ '2' } , [ 'n' ] =50 } , 3283{ [ 'rt' ] =1 , [ 'rc' ] ={ '3' } , [ 'n' ] =51 } , 3284{ [ 'rc' ] ={ 'hjkl' } , [ 'n' ] =97 } , 3285} , 3286 3287[ 'jumps' ] =eval ((( [[ 3288filter(map(add( 3289getjumplist()[0], { 'bufnr': bufnr('%'), 'lnum': getcurpos()[1] }), 3290'filter( 3291{ "f": expand("#".v:val.bufnr.":p:gs?\\?/?"), "l": v:val.lnum }, 3292{ k, v -> k != "l" || v != 1 })'), '!empty(v:val.f)') 3293]] ): gsub ( '\n' , '' ))), 3294 3295[ 'bufs' ] =eval ( [[ 3296filter(map(getbufinfo(), '{ "f": v:val.name }'), '!empty(v:val.f)') 3297]] ), 3298 3299[ 'gvars' ] ={ { 'one' , 1 } , { 'Two' , 2 } , { 'THREE' , 3 } } , 3300} 3301 3302eq ( expected_ctx , parse_context ( api . nvim_get_context ( opts ))) 3303eq ( expected_ctx , parse_context ( api . nvim_get_context ( {} ))) 3304eq ( expected_ctx , parse_context ( api . nvim_get_context ( { types ={} } ))) 3305end ) 3306end ) 3307 3308describe ( 'nvim_load_context' , function () 3309it ( 'sets current editor state to given context dict' , function () 3310local opts ={ types ={ 'regs' , 'jumps' , 'bufs' , 'gvars' } } 3311eq ( {} , parse_context ( api . nvim_get_context ( opts ))) 3312 3313api . nvim_set_var ( 'one' , 1 ) 3314api . nvim_set_var ( 'Two' , 2 ) 3315api . nvim_set_var ( 'THREE' , 3 ) 3316local ctx =api . nvim_get_context ( opts ) 3317api . nvim_set_var ( 'one' , 'a' ) 3318api . nvim_set_var ( 'Two' , 'b' ) 3319api . nvim_set_var ( 'THREE' , 'c' ) 3320eq ( { 'a' , 'b' , 'c' } , eval ( '[g:one, g:Two, g:THREE]' )) 3321api . nvim_load_context ( ctx ) 3322eq ( { 1 , 2 , 3 } , eval ( '[g:one, g:Two, g:THREE]' )) 3323 3324-- Context restores what it saved, irrespective of 'shada'. 3325command ( 'set shada=' ) 3326command ( 'autocmd OptionSet shada let g:optionset = 1' ) 3327api . nvim_set_var ( 'one' , 'a' ) 3328api . nvim_load_context ( ctx ) 3329eq ( 1 , eval ( 'g:one' )) 3330eq ( '' , eval ( '&shada' )) 3331eq ( 0 , eval ( "get(g:, 'optionset', 0)" )) 3332 3333-- Does not touch v:oldfiles (only ":rshada!" rebuilds it). 3334command ( 'let v:oldfiles = ["/a", "/b"]' ) 3335api . nvim_load_context ( ctx ) 3336eq ( { '/a' , '/b' } , eval ( 'v:oldfiles' )) 3337end ) 3338 3339it ( 'errors when context dict is invalid' , function () 3340eq ( 3341'E474: Failed to convert list to msgpack string buffer' , 3342pcall_err ( api . nvim_load_context , { regs ={ {} } , jumps ={ {} } } ) 3343) 3344eq ( 3345'E474: Failed to convert list to msgpack string buffer' , 3346pcall_err ( api . nvim_load_context , { regs ={ { [ '' ] ='' } } } ) 3347) 3348end ) 3349end ) 3350 3351describe ( 'nvim_feedkeys' , function () 3352it ( 'K_SPECIAL escaping' , function () 3353local function on_setup () 3354-- notice the special char(…) \xe2\80\xa6 3355api . nvim_feedkeys ( ':let x1="…"\n' , '' , true ) 3356 3357-- Both nvim_replace_termcodes and nvim_feedkeys escape \x80 3358local inp =n . api . nvim_replace_termcodes ( ':let x2="…"<CR>' , true , true , true ) 3359api . nvim_feedkeys ( inp , '' , true ) -- escape_ks=true 3360 3361-- nvim_feedkeys with K_SPECIAL escaping disabled 3362inp =n . api . nvim_replace_termcodes ( ':let x3="…"<CR>' , true , true , true ) 3363api . nvim_feedkeys ( inp , '' , false ) -- escape_ks=false 3364 3365n . stop () 3366end 3367 3368-- spin the loop a bit 3369n . run ( nil , nil , on_setup ) 3370 3371eq ( '…' , api . nvim_get_var ( 'x1' )) 3372-- Because of the double escaping this is neq 3373neq ( '…' , api . nvim_get_var ( 'x2' )) 3374eq ( '…' , api . nvim_get_var ( 'x3' )) 3375end ) 3376end ) 3377 3378describe ( 'nvim_out_write' , function () 3379local screen 3380 3381before_each ( function () 3382screen =Screen . new ( 40 , 8 ) 3383end ) 3384 3385it ( 'prints long messages correctly #20534' , function () 3386exec ( [[ 3387set more 3388redir => g:out 3389silent! call nvim_out_write('a') 3390silent! call nvim_out_write('a') 3391silent! call nvim_out_write('a') 3392silent! call nvim_out_write("\n") 3393silent! call nvim_out_write('a') 3394silent! call nvim_out_write('a') 3395silent! call nvim_out_write(repeat('a', 5000) .. "\n") 3396silent! call nvim_out_write('a') 3397silent! call nvim_out_write('a') 3398silent! call nvim_out_write('a') 3399silent! call nvim_out_write("\n") 3400redir END 3401]] ) 3402eq ( '\naaa\n' .. ( 'a' ): rep ( 5002 ) .. '\naaa' , api . nvim_get_var ( 'out' )) 3403end ) 3404 3405it ( 'blank line in message' , function () 3406feed ( [[:call nvim_out_write("\na\n")<CR>]] ) 3407screen : expect { 3408grid =[[ 3409| 3410{1:~ }|*3 3411{3: }| 3412| 3413a | 3414{6:Press ENTER or type command to continue}^ | 3415]] , 3416} 3417feed ( '<CR>' ) 3418feed ( [[:call nvim_out_write("b\n\nc\n")<CR>]] ) 3419screen : expect { 3420grid =[[ 3421| 3422{1:~ }|*2 3423{3: }| 3424b | 3425| 3426c | 3427{6:Press ENTER or type command to continue}^ | 3428]] , 3429} 3430end ) 3431 3432it ( 'NUL bytes in message' , function () 3433feed ( [[:lua vim.api.nvim_out_write('aaa\0bbb\0\0ccc\nddd\0\0\0eee\n')<CR>]] ) 3434screen : expect { 3435grid =[[ 3436| 3437{1:~ }|*3 3438{3: }| 3439aaa{18:^@}bbb{18:^@^@}ccc | 3440ddd{18:^@^@^@}eee | 3441{6:Press ENTER or type command to continue}^ | 3442]] , 3443} 3444end ) 3445end ) 3446 3447describe ( 'nvim_err_write' , function () 3448local screen 3449 3450before_each ( function () 3451screen =Screen . new ( 40 , 8 ) 3452end ) 3453 3454it ( 'can show one line' , function () 3455async_meths . nvim_err_write ( 'has bork\n' ) 3456screen : expect ( [[ 3457^ | 3458{1:~ }|*6 3459{9:has bork} | 3460]] ) 3461end ) 3462 3463it ( 'shows return prompt when more than &cmdheight lines' , function () 3464async_meths . nvim_err_write ( 'something happened\nvery bad\n' ) 3465screen : expect ( [[ 3466| 3467{1:~ }|*3 3468{3: }| 3469{9:something happened} | 3470{9:very bad} | 3471{6:Press ENTER or type command to continue}^ | 3472]] ) 3473end ) 3474 3475it ( 'shows return prompt after all lines are shown' , function () 3476async_meths . nvim_err_write ( 'FAILURE\nERROR\nEXCEPTION\nTRACEBACK\n' ) 3477screen : expect ( [[ 3478| 3479{1:~ }| 3480{3: }| 3481{9:FAILURE} | 3482{9:ERROR} | 3483{9:EXCEPTION} | 3484{9:TRACEBACK} | 3485{6:Press ENTER or type command to continue}^ | 3486]] ) 3487end ) 3488 3489it ( 'handles multiple calls' , function () 3490-- without linebreak text is joined to one line 3491async_meths . nvim_err_write ( 'very ' ) 3492async_meths . nvim_err_write ( 'fail\n' ) 3493screen : expect ( [[ 3494^ | 3495{1:~ }|*6 3496{9:very fail} | 3497]] ) 3498n . poke_eventloop () 3499 3500-- shows up to &cmdheight lines 3501async_meths . nvim_err_write ( 'more fail\ntoo fail\n' ) 3502screen : expect ( [[ 3503| 3504{1:~ }|*3 3505{3: }| 3506{9:more fail} | 3507{9:too fail} | 3508{6:Press ENTER or type command to continue}^ | 3509]] ) 3510feed ( '<cr>' ) -- exit the press ENTER screen 3511end ) 3512 3513it ( 'NUL bytes in message' , function () 3514async_meths . nvim_err_write ( 'aaa\0bbb\0\0ccc\nddd\0\0\0eee\n' ) 3515screen : expect { 3516grid =[[ 3517| 3518{1:~ }|*3 3519{3: }| 3520{9:aaa^@bbb^@^@ccc} | 3521{9:ddd^@^@^@eee} | 3522{6:Press ENTER or type command to continue}^ | 3523]] , 3524} 3525end ) 3526end ) 3527 3528describe ( 'nvim_err_writeln' , function () 3529local screen 3530 3531before_each ( function () 3532screen =Screen . new ( 40 , 8 ) 3533end ) 3534 3535it ( 'shows only one return prompt after all lines are shown' , function () 3536async_meths . nvim_err_writeln ( 'FAILURE\nERROR\nEXCEPTION\nTRACEBACK' ) 3537screen : expect ( [[ 3538| 3539{1:~ }| 3540{3: }| 3541{9:FAILURE} | 3542{9:ERROR} | 3543{9:EXCEPTION} | 3544{9:TRACEBACK} | 3545{6:Press ENTER or type command to continue}^ | 3546]] ) 3547feed ( '<CR>' ) 3548screen : expect ( [[ 3549^ | 3550{1:~ }|*6 3551| 3552]] ) 3553end ) 3554end ) 3555 3556describe ( 'nvim_list_chans, nvim_get_chan_info' , function () 3557before_each ( function () 3558command ( 'autocmd ChanOpen * let g:opened_event = deepcopy(v:event)' ) 3559command ( 'autocmd ChanInfo * let g:info_event = deepcopy(v:event)' ) 3560end ) 3561local testinfo ={ 3562stream ='stdio' , 3563id =1 , 3564mode ='rpc' , 3565client ={} , 3566} 3567local stderr ={ 3568stream ='stderr' , 3569id =2 , 3570mode ='bytes' , 3571} 3572 3573it ( 'returns {} for invalid channel' , function () 3574eq ( {} , api . nvim_get_chan_info ( - 1 )) 3575-- more preallocated numbers might be added, try something high 3576eq ( {} , api . nvim_get_chan_info ( 10 )) 3577end ) 3578 3579it ( 'stream=stdio channel' , function () 3580eq ( { [ 1 ] =testinfo , [ 2 ] =stderr } , api . nvim_list_chans ()) 3581-- 0 should return current channel 3582eq ( testinfo , api . nvim_get_chan_info ( 0 )) 3583eq ( testinfo , api . nvim_get_chan_info ( 1 )) 3584eq ( stderr , api . nvim_get_chan_info ( 2 )) 3585 3586api . nvim_set_client_info ( 3587'functionaltests' , 3588{ major =0 , minor =3 , patch =17 } , 3589'ui' , 3590{ do_stuff ={ n_args ={ 2 , 3 } } } , 3591{ license ='Apache2' } 3592) 3593local info ={ 3594stream ='stdio' , 3595id =1 , 3596mode ='rpc' , 3597client ={ 3598name ='functionaltests' , 3599version ={ major =0 , minor =3 , patch =17 } , 3600type ='ui' , 3601methods ={ do_stuff ={ n_args ={ 2 , 3 } } } , 3602attributes ={ license ='Apache2' } , 3603} , 3604} 3605eq ( { info =info } , api . nvim_get_var ( 'info_event' )) 3606eq ( { [ 1 ] =info , [ 2 ] =stderr } , api . nvim_list_chans ()) 3607eq ( info , api . nvim_get_chan_info ( 1 )) 3608end ) 3609 3610it ( 'stream=job channel' , function () 3611eq ( 3 , eval ( "jobstart(['cat'], {'rpc': v:true})" )) 3612local catpath =vim . fs . normalize ( eval ( 'exepath("cat")' )) 3613local info ={ 3614stream ='job' , 3615id =3 , 3616argv ={ catpath } , 3617mode ='rpc' , 3618client ={} , 3619} 3620eq ( { info =info } , api . nvim_get_var ( 'opened_event' )) 3621eq ( { [ 1 ] =testinfo , [ 2 ] =stderr , [ 3 ] =info } , api . nvim_list_chans ()) 3622eq ( info , api . nvim_get_chan_info ( 3 )) 3623eval ( 3624'rpcrequest(3, "nvim_set_client_info", "amazing-cat", {}, "remote",' 3625.. '{"nvim_command":{"n_args":1}},' -- and so on 3626.. '{"description":"The Amazing Cat"})' 3627) 3628info ={ 3629stream ='job' , 3630id =3 , 3631argv ={ catpath } , 3632mode ='rpc' , 3633client ={ 3634name ='amazing-cat' , 3635version ={ major =0 } , 3636type ='remote' , 3637methods ={ nvim_command ={ n_args =1 } } , 3638attributes ={ description ='The Amazing Cat' } , 3639} , 3640} 3641eq ( { info =info } , api . nvim_get_var ( 'info_event' )) 3642eq ( { [ 1 ] =testinfo , [ 2 ] =stderr , [ 3 ] =info } , api . nvim_list_chans ()) 3643 3644eq ( 3645"Vim:Invoking 'nvim_set_current_buf' on channel 3 (amazing-cat):\nWrong type for argument 1 when calling nvim_set_current_buf, expecting Buffer" , 3646pcall_err ( eval , 'rpcrequest(3, "nvim_set_current_buf", -1)' ) 3647) 3648eq ( info , eval ( 'rpcrequest(3, "nvim_get_chan_info", 0)' )) 3649end ) 3650 3651local function term_channel_info ( id , buffer , argv ) 3652return { 3653stream ='job' , 3654id =id , 3655argv =argv , 3656mode ='terminal' , 3657buf =buffer , 3658buffer =buffer , -- deprecated 3659pty ='?' , 3660exitcode =- 1 , 3661} 3662end 3663 3664it ( 'stream=job :terminal channel' , function () 3665Screen . new ( 80 , 24 ) 3666 3667command ( ':terminal' ) 3668eq ( 1 , api . nvim_get_current_buf ()) 3669eq ( 3 , api . nvim_get_option_value ( 'channel' , { buf =1 } )) 3670 3671local info =term_channel_info ( 3 , 1 , { vim . fs . normalize ( eval ( 'exepath(&shell)' )) } ) 3672local event =api . nvim_get_var ( 'opened_event' ) 3673if not is_os ( 'win' ) then 3674info . pty =event . info . pty 3675neq ( nil , string . match ( info . pty , '^/dev/' )) 3676end 3677eq ( { info =info } , event ) 3678info . buf =1 3679info . buffer =1 -- deprecated 3680eq ( { [ 1 ] =testinfo , [ 2 ] =stderr , [ 3 ] =info } , api . nvim_list_chans ()) 3681eq ( info , api . nvim_get_chan_info ( 3 )) 3682 3683-- :terminal with args + running process (Nvim TUI). 3684-- Don't use a shell here, so that SIGHUP handling doesn't depend on the shell. 3685command ( 'enew' ) 3686local argv ={ n . nvim_prog , '-u' , 'NONE' , '-i' , 'NONE' } 3687fn . jobstart ( argv , { 3688term =true , 3689env ={ VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ) } , 3690} ) 3691eq ( - 1 , eval ( 'jobwait([&channel], 0)[0]' )) -- Running? 3692local expected2 =term_channel_info ( 4 , 2 , argv ) 3693local actual2 =eval ( 'nvim_get_chan_info(&channel)' ) 3694expected2 . pty =actual2 . pty 3695eq ( expected2 , actual2 ) 3696 3697-- Make sure Nvim TUI is started (which is after registering SIGHUP handler). 3698t . retry ( nil , nil , function () 3699matches ( 'Nvim is open source and freely distributable' , n . curbuf_contents ()) 3700end ) 3701 3702-- :terminal with args + stopped process (Nvim TUI). 3703eq ( 1 , eval ( 'jobstop(&channel)' )) 3704eval ( 'jobwait([&channel], 1000)' ) -- Wait. 3705expected2 . pty =( is_os ( 'win' ) and '?' or '' ) -- pty stream was closed. 3706-- On Unix, SIGHUP is handled by Nvim TUI, so exit code is 1. 3707-- On Windows, even though Nvim TUI handles SIGHUP, it's not possible for the 3708-- parent process to know that, so exit code reflects SIGHUP. 3709expected2 . exitcode =( is_os ( 'win' ) and 129 or 1 ) 3710eq ( expected2 , eval ( 'nvim_get_chan_info(&channel)' )) 3711 3712-- :terminal with args + stopped process (shell-test). 3713command ( 'enew' ) 3714-- Use a process that doesn't read stdin, so PTY EOF can't race SIGHUP. 3715argv ={ n . testprg ( 'shell-test' ), 'HOLD' } 3716fn . jobstart ( argv , { term =true } ) 3717t . retry ( nil , nil , function () 3718matches ( 'holding %$' , n . curbuf_contents ()) 3719end ) 3720eq ( 1 , eval ( 'jobstop(&channel)' )) 3721eval ( 'jobwait([&channel], 1000)' ) -- Wait. 3722local expected3 =term_channel_info ( 5 , 3 , argv ) 3723expected3 . pty =( is_os ( 'win' ) and '?' or '' ) -- pty stream was closed. 3724-- Exit code should reflect SIGHUP as shell-test doesn't handle it. 3725expected3 . exitcode =129 3726eq ( expected3 , eval ( 'nvim_get_chan_info(&channel)' )) 3727end ) 3728end ) 3729 3730describe ( 'nvim_call_atomic' , function () 3731it ( 'works' , function () 3732api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'first' } ) 3733local req ={ 3734{ 'nvim_get_current_line' , {} } , 3735{ 'nvim_set_current_line' , { 'second' } } , 3736} 3737eq ( { { 'first' , NIL } , NIL } , api . nvim_call_atomic ( req )) 3738eq ( { 'second' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 3739end ) 3740 3741it ( 'allows multiple return values' , function () 3742local req ={ 3743{ 'nvim_set_var' , { 'avar' , true } } , 3744{ 'nvim_set_var' , { 'bvar' , 'string' } } , 3745{ 'nvim_get_var' , { 'avar' } } , 3746{ 'nvim_get_var' , { 'bvar' } } , 3747} 3748eq ( { { NIL , NIL , true , 'string' } , NIL } , api . nvim_call_atomic ( req )) 3749end ) 3750 3751it ( 'is aborted by errors in call' , function () 3752local error_types =api . nvim_get_api_info ()[ 2 ]. error_types 3753local req ={ 3754{ 'nvim_set_var' , { 'one' , 1 } } , 3755{ 'nvim_buf_set_lines' , {} } , 3756{ 'nvim_set_var' , { 'two' , 2 } } , 3757} 3758eq ( { 3759{ NIL } , 3760{ 37611 , 3762error_types . Exception . id , 3763'Wrong number of arguments: expecting 5 but got 0' , 3764} , 3765} , api . nvim_call_atomic ( req )) 3766eq ( 1 , api . nvim_get_var ( 'one' )) 3767eq ( false , pcall ( api . nvim_get_var , 'two' )) 3768 3769-- still returns all previous successful calls 3770req ={ 3771{ 'nvim_set_var' , { 'avar' , 5 } } , 3772{ 'nvim_set_var' , { 'bvar' , 'string' } } , 3773{ 'nvim_get_var' , { 'avar' } } , 3774{ 'nvim_buf_get_lines' , { 0 , 10 , 20 , true } } , 3775{ 'nvim_get_var' , { 'bvar' } } , 3776} 3777eq ( 3778{ { NIL , NIL , 5 } , { 3 , error_types . Validation . id , 'Index out of bounds' } } , 3779api . nvim_call_atomic ( req ) 3780) 3781 3782req ={ 3783{ 'i_am_not_a_method' , { 'xx' } } , 3784{ 'nvim_set_var' , { 'avar' , 10 } } , 3785} 3786eq ( 3787{ {} , { 0 , error_types . Exception . id , 'Invalid method: i_am_not_a_method' } } , 3788api . nvim_call_atomic ( req ) 3789) 3790eq ( 5 , api . nvim_get_var ( 'avar' )) 3791end ) 3792 3793it ( 'validation' , function () 3794local req ={ 3795{ 'nvim_set_var' , { 'avar' , 1 } } , 3796{ 'nvim_set_var' } , 3797{ 'nvim_set_var' , { 'avar' , 2 } } , 3798} 3799eq ( "Invalid 'calls' item: expected 2-item Array" , pcall_err ( api . nvim_call_atomic , req )) 3800-- call before was done, but not after 3801eq ( 1 , api . nvim_get_var ( 'avar' )) 3802 3803req ={ 3804{ 'nvim_set_var' , { 'bvar' , { 2 , 3 } } } , 380512 , 3806} 3807eq ( "Invalid 'calls' item: expected Array, got Integer" , pcall_err ( api . nvim_call_atomic , req )) 3808eq ( { 2 , 3 } , api . nvim_get_var ( 'bvar' )) 3809 3810req ={ 3811{ 'nvim_set_current_line' , 'little line' } , 3812{ 'nvim_set_var' , { 'avar' , 3 } } , 3813} 3814eq ( 'Invalid call args: expected Array, got String' , pcall_err ( api . nvim_call_atomic , req )) 3815-- call before was done, but not after 3816eq ( 1 , api . nvim_get_var ( 'avar' )) 3817eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 3818end ) 3819end ) 3820 3821describe ( 'nvim_list_runtime_paths' , function () 3822local test_dir ='Xtest_list_runtime_paths' 3823 3824setup ( function () 3825mkdir_p (( '%s/a' ): format ( test_dir )) 3826mkdir_p (( '%s/b' ): format ( test_dir )) 3827end ) 3828teardown ( function () 3829rmdir ( test_dir ) 3830end ) 3831before_each ( function () 3832api . nvim_set_current_dir ( test_dir ) 3833end ) 3834 3835it ( 'returns nothing with empty &runtimepath' , function () 3836api . nvim_set_option_value ( 'runtimepath' , '' , {} ) 3837eq ( {} , api . nvim_list_runtime_paths ()) 3838end ) 3839it ( 'returns single runtimepath' , function () 3840api . nvim_set_option_value ( 'runtimepath' , 'a' , {} ) 3841eq ( { 'a' } , api . nvim_list_runtime_paths ()) 3842end ) 3843it ( 'returns two runtimepaths' , function () 3844api . nvim_set_option_value ( 'runtimepath' , 'a,b' , {} ) 3845eq ( { 'a' , 'b' } , api . nvim_list_runtime_paths ()) 3846end ) 3847it ( 'returns empty strings when appropriate' , function () 3848api . nvim_set_option_value ( 'runtimepath' , 'a,,b' , {} ) 3849eq ( { 'a' , '' , 'b' } , api . nvim_list_runtime_paths ()) 3850api . nvim_set_option_value ( 'runtimepath' , ',a,b' , {} ) 3851eq ( { '' , 'a' , 'b' } , api . nvim_list_runtime_paths ()) 3852-- Trailing "," is ignored. Use ",," if you really really want CWD. 3853api . nvim_set_option_value ( 'runtimepath' , 'a,b,' , {} ) 3854eq ( { 'a' , 'b' } , api . nvim_list_runtime_paths ()) 3855api . nvim_set_option_value ( 'runtimepath' , 'a,b,,' , {} ) 3856eq ( { 'a' , 'b' , '' } , api . nvim_list_runtime_paths ()) 3857end ) 3858it ( 'truncates too long paths' , function () 3859local long_path =( '/a' ): rep ( 8192 ) 3860api . nvim_set_option_value ( 'runtimepath' , long_path , {} ) 3861local paths_list =api . nvim_list_runtime_paths () 3862eq ( {} , paths_list ) 3863end ) 3864end ) 3865 3866it ( 'can throw exceptions' , function () 3867local status , err =pcall ( api . nvim_get_option_value , 'invalid-option' , {} ) 3868eq ( false , status ) 3869matches ( "Unknown option 'invalid%-option'" , err ) 3870end ) 3871 3872it ( 'does not truncate error message <1 MB #5984' , function () 3873local very_long_name ='A' .. ( 'x' ): rep ( 10000 ) .. 'Z' 3874local status , err =pcall ( api . nvim_get_option_value , very_long_name , {} ) 3875eq ( false , status ) 3876eq ( very_long_name , err : match ( 'Ax+Z?' )) 3877end ) 3878 3879it ( 'does not leak memory on incorrect argument types' , function () 3880local status , err =pcall ( api . nvim_set_current_dir , { 'not' , 'a' , 'dir' } ) 3881eq ( false , status ) 3882matches ( ': Wrong type for argument 1 when calling nvim_set_current_dir, expecting String' , err ) 3883end ) 3884 3885describe ( 'nvim_parse_expression' , function () 3886before_each ( function () 3887api . nvim_set_option_value ( 'isident' , '' , {} ) 3888end ) 3889 3890local function simplify_east_api_node ( line , east_api_node ) 3891if east_api_node == NIL then 3892return nil 3893end 3894if east_api_node . children then 3895for k , v in pairs ( east_api_node . children ) do 3896east_api_node . children [ k ] =simplify_east_api_node ( line , v ) 3897end 3898end 3899local typ =east_api_node . type 3900if typ == 'Register' then 3901typ =typ .. ( '(name=%s)' ): format ( tostring ( intchar2lua ( east_api_node . name ))) 3902east_api_node . name =nil 3903elseif typ == 'PlainIdentifier' then 3904typ =typ 3905.. ( '(scope=%s,ident=%s)' ): format ( 3906tostring ( intchar2lua ( east_api_node . scope )), 3907east_api_node . ident 3908) 3909east_api_node . scope =nil 3910east_api_node . ident =nil 3911elseif typ == 'PlainKey' then 3912typ =typ .. ( '(key=%s)' ): format ( east_api_node . ident ) 3913east_api_node . ident =nil 3914elseif typ == 'Comparison' then 3915typ =typ 3916.. ( '(type=%s,inv=%u,ccs=%s)' ): format ( 3917east_api_node . cmp_type , 3918east_api_node . invert and 1 or 0 , 3919east_api_node . ccs_strategy 3920) 3921east_api_node . ccs_strategy =nil 3922east_api_node . cmp_type =nil 3923east_api_node . invert =nil 3924elseif typ == 'Integer' then 3925typ =typ .. ( '(val=%u)' ): format ( east_api_node . ivalue ) 3926east_api_node . ivalue =nil 3927elseif typ == 'Float' then 3928typ =typ .. format_string ( '(val=%e)' , east_api_node . fvalue ) 3929east_api_node . fvalue =nil 3930elseif typ == 'SingleQuotedString' or typ == 'DoubleQuotedString' then 3931typ =format_string ( '%s(val=%q)' , typ , east_api_node . svalue ) 3932east_api_node . svalue =nil 3933elseif typ == 'Option' then 3934typ =( '%s(scope=%s,ident=%s)' ): format ( 3935typ , 3936tostring ( intchar2lua ( east_api_node . scope )), 3937east_api_node . ident 3938) 3939east_api_node . ident =nil 3940east_api_node . scope =nil 3941elseif typ == 'Environment' then 3942typ =( '%s(ident=%s)' ): format ( typ , east_api_node . ident ) 3943east_api_node . ident =nil 3944elseif typ == 'Assignment' then 3945local aug =east_api_node . augmentation 3946if aug == '' then 3947aug ='Plain' 3948end 3949typ =( '%s(%s)' ): format ( typ , aug ) 3950east_api_node . augmentation =nil 3951end 3952typ =( '%s:%u:%u:%s' ): format ( 3953typ , 3954east_api_node . start [ 1 ], 3955east_api_node . start [ 2 ], 3956line : sub ( east_api_node . start [ 2 ] + 1 , east_api_node . start [ 2 ] + 1 + east_api_node . len - 1 ) 3957) 3958assert ( east_api_node . start [ 2 ] + east_api_node . len - 1 <= # line ) 3959for k , _ in pairs ( east_api_node . start ) do 3960assert (( { true , true } )[ k ]) 3961end 3962east_api_node . start =nil 3963east_api_node . type =nil 3964east_api_node . len =nil 3965local can_simplify =true 3966for _ , _ in pairs ( east_api_node ) do 3967if can_simplify then 3968can_simplify =false 3969end 3970end 3971if can_simplify then 3972return typ 3973else 3974east_api_node [ 1 ] =typ 3975return east_api_node 3976end 3977end 3978local function simplify_east_api ( line , east_api ) 3979if east_api . error then 3980east_api . err =east_api . error 3981east_api . error =nil 3982east_api . err . msg =east_api . err . message 3983east_api . err . message =nil 3984end 3985if east_api . ast then 3986east_api . ast ={ simplify_east_api_node ( line , east_api . ast ) } 3987if # east_api . ast == 0 then 3988east_api . ast =nil 3989end 3990end 3991if east_api . len == # line then 3992east_api . len =nil 3993end 3994return east_api 3995end 3996local function simplify_east_hl ( line , east_hl ) 3997for i , v in ipairs ( east_hl ) do 3998east_hl [ i ] =( '%s:%u:%u:%s' ): format ( v [ 4 ], v [ 1 ], v [ 2 ], line : sub ( v [ 2 ] + 1 , v [ 3 ])) 3999end 4000return east_hl 4001end 4002local FLAGS_TO_STR ={ 4003[ 0 ] ='' , 4004[ 1 ] ='m' , 4005[ 2 ] ='E' , 4006[ 3 ] ='mE' , 4007[ 4 ] ='l' , 4008[ 5 ] ='lm' , 4009[ 6 ] ='lE' , 4010[ 7 ] ='lmE' , 4011} 4012local function _check_parsing ( opts , str , exp_ast , exp_highlighting_fs , nz_flags_exps ) 4013if type ( str ) ~= 'string' then 4014return 4015end 4016local zflags =opts . flags [ 1 ] 4017nz_flags_exps =nz_flags_exps or {} 4018for _ , flags in ipairs ( opts . flags ) do 4019local err , msg =pcall ( function () 4020local east_api =api . nvim_parse_expression ( str , FLAGS_TO_STR [ flags ], true ) 4021local east_hl =east_api . highlight 4022east_api . highlight =nil 4023local ast =simplify_east_api ( str , east_api ) 4024local hls =simplify_east_hl ( str , east_hl ) 4025local exps ={ 4026ast =exp_ast , 4027hl_fs =exp_highlighting_fs , 4028} 4029local add_exps =nz_flags_exps [ flags ] 4030if not add_exps and flags == 3 + zflags then 4031add_exps =nz_flags_exps [ 1 + zflags ] or nz_flags_exps [ 2 + zflags ] 4032end 4033if add_exps then 4034if add_exps . ast then 4035exps . ast =mergedicts_copy ( exps . ast , add_exps . ast ) 4036end 4037if add_exps . hl_fs then 4038exps . hl_fs =mergedicts_copy ( exps . hl_fs , add_exps . hl_fs ) 4039end 4040end 4041eq ( exps . ast , ast ) 4042if exp_highlighting_fs then 4043local exp_highlighting ={} 4044local next_col =0 4045for i , h in ipairs ( exps . hl_fs ) do 4046exp_highlighting [ i ], next_col =h ( next_col ) 4047end 4048eq ( exp_highlighting , hls ) 4049end 4050end ) 4051if not err then 4052if type ( msg ) == 'table' then 4053local merr , new_msg =pcall ( format_string , 'table error:\n%s\n\n(%r)' , msg . message , msg ) 4054if merr then 4055msg =new_msg 4056else 4057msg =format_string ( 'table error without .message:\n(%r)' , msg ) 4058end 4059elseif type ( msg ) ~= 'string' then 4060msg =format_string ( 'non-string non-table error:\n%r' , msg ) 4061end 4062error ( 4063format_string ( 4064'Error while processing test (%r, %s):\n%s' , 4065str , 4066FLAGS_TO_STR [ flags ], 4067msg 4068) 4069) 4070end 4071end 4072end 4073local function hl ( group , str , shift ) 4074return function ( next_col ) 4075local col =next_col + ( shift or 0 ) 4076return (( '%s:%u:%u:%s' ): format ( 'Nvim' .. group , 0 , col , str )), ( col + # str ) 4077end 4078end 4079local function fmtn ( typ , args , rest ) 4080if 4081typ == 'UnknownFigure' 4082or typ == 'DictLiteral' 4083or typ == 'CurlyBracesIdentifier' 4084or typ == 'Lambda' 4085then 4086return ( '%s%s' ): format ( typ , rest ) 4087elseif typ == 'DoubleQuotedString' or typ == 'SingleQuotedString' then 4088if args : sub ( - 4 ) == 'NULL' then 4089args =args : sub ( 1 , - 5 ) .. '""' 4090end 4091return ( '%s(%s)%s' ): format ( typ , args , rest ) 4092end 4093end 4094 4095it ( 'does not crash parsing invalid VimL expression' , function () 4096api . nvim_input ( ':<C-r>=' ) 4097api . nvim_input ( '1bork/' ) -- #29648 4098assert_alive () 4099api . nvim_input ( '<C-u>];' ) 4100assert_alive () 4101api . nvim_parse_expression ( 'a{b}' , '' , false ) 4102assert_alive () 4103end ) 4104 4105require ( 'test.unit.viml.expressions.parser_tests' )( it , _check_parsing , hl , fmtn ) 4106end ) 4107 4108describe ( 'nvim_list_uis' , function () 4109it ( 'returns empty if --headless' , function () 4110-- Test runner defaults to --headless. 4111eq ( {} , api . nvim_list_uis ()) 4112end ) 4113it ( 'returns attached UIs' , function () 4114local screen =Screen . new ( 20 , 4 , { override =true } ) 4115local expected ={ 4116{ 4117chan =1 , 4118ext_cmdline =false , 4119ext_hlstate =false , 4120ext_linegrid =screen . _options . ext_linegrid or false , 4121ext_messages =false , 4122ext_multigrid =false , 4123ext_popupmenu =false , 4124ext_tabline =false , 4125ext_termcolors =false , 4126ext_wildmenu =false , 4127height =4 , 4128override =true , 4129rgb =true , 4130stdin_tty =false , 4131stdout_tty =false , 4132term_background ='' , 4133term_colors =0 , 4134term_name ='' , 4135width =20 , 4136} , 4137} 4138 4139eq ( expected , api . nvim_list_uis ()) 4140 4141screen : detach () 4142screen =Screen . new ( 44 , 99 , { rgb =false } ) -- luacheck: ignore 4143expected [ 1 ]. rgb =false 4144expected [ 1 ]. override =false 4145expected [ 1 ]. width =44 4146expected [ 1 ]. height =99 4147eq ( expected , api . nvim_list_uis ()) 4148end ) 4149end ) 4150 4151describe ( 'nvim_create_namespace' , function () 4152it ( 'works' , function () 4153local orig =api . nvim_get_namespaces () 4154local base =vim . iter ( orig ): fold ( 0 , function ( acc , _ , v ) 4155return math . max ( acc , v ) 4156end ) 4157eq ( base + 1 , api . nvim_create_namespace ( 'ns-1' )) 4158eq ( base + 2 , api . nvim_create_namespace ( 'ns-2' )) 4159eq ( base + 1 , api . nvim_create_namespace ( 'ns-1' )) 4160 4161local expected =vim . tbl_extend ( 'error' , orig , { 4162[ 'ns-1' ] =base + 1 , 4163[ 'ns-2' ] =base + 2 , 4164} ) 4165 4166eq ( expected , api . nvim_get_namespaces ()) 4167eq ( base + 3 , api . nvim_create_namespace ( '' )) 4168eq ( base + 4 , api . nvim_create_namespace ( '' )) 4169eq ( expected , api . nvim_get_namespaces ()) 4170end ) 4171end ) 4172 4173describe ( 'nvim_create_buf' , function () 4174it ( 'works' , function () 4175eq ( 2 , api . nvim_create_buf ( true , false )) 4176eq ( 3 , api . nvim_create_buf ( false , false )) 4177eq ( 4178' 1 %a "[No Name]" line 1\n' 4179.. ' 2 h "[No Name]" line 0' , 4180command_output ( 'ls' ) 4181) 4182-- current buffer didn't change 4183eq ( 1 , api . nvim_get_current_buf ()) 4184 4185local screen =Screen . new ( 20 , 4 ) 4186api . nvim_buf_set_lines ( 2 , 0 , - 1 , true , { 'some text' } ) 4187api . nvim_set_current_buf ( 2 ) 4188screen : expect ( 4189[[ 4190^some text | 4191{1:~ }|*2 4192| 4193]] , 4194{ 4195[ 1 ] ={ bold =true , foreground =Screen . colors . Blue1 } , 4196} 4197) 4198end ) 4199 4200it ( 'can change buftype before visiting' , function () 4201api . nvim_set_option_value ( 'hidden' , false , {} ) 4202eq ( 2 , api . nvim_create_buf ( true , false )) 4203api . nvim_set_option_value ( 'buftype' , 'nofile' , { buf =2 } ) 4204api . nvim_buf_set_lines ( 2 , 0 , - 1 , true , { 'test text' } ) 4205command ( 'split | buffer 2' ) 4206eq ( 2 , api . nvim_get_current_buf ()) 4207-- if the buf_set_option("buftype") didn't work, this would error out. 4208command ( 'close' ) 4209eq ( 1 , api . nvim_get_current_buf ()) 4210end ) 4211 4212it ( 'does not trigger BufEnter, BufWinEnter' , function () 4213command ( 'let g:fired = v:false' ) 4214command ( 'au BufEnter,BufWinEnter * let g:fired = v:true' ) 4215 4216eq ( 2 , api . nvim_create_buf ( true , false )) 4217api . nvim_buf_set_lines ( 2 , 0 , - 1 , true , { 'test' , 'text' } ) 4218 4219eq ( false , eval ( 'g:fired' )) 4220end ) 4221 4222it ( 'TextChanged and TextChangedI do not trigger without changes' , function () 4223local buf =api . nvim_create_buf ( true , false ) 4224command ( [[let g:changed = '']] ) 4225api . nvim_create_autocmd ( { 'TextChanged' , 'TextChangedI' } , { 4226buffer =buf , 4227command ='let g:changed ..= mode()' , 4228} ) 4229api . nvim_set_current_buf ( buf ) 4230feed ( 'i' ) 4231eq ( '' , api . nvim_get_var ( 'changed' )) 4232end ) 4233 4234it ( 'scratch-buffer' , function () 4235eq ( 2 , api . nvim_create_buf ( false , true )) 4236eq ( 3 , api . nvim_create_buf ( true , true )) 4237eq ( 4 , api . nvim_create_buf ( true , true )) 4238local scratch_bufs ={ 2 , 3 , 4 } 4239eq ( 4240' 1 %a "[No Name]" line 1\n' 4241.. ' 3 h "[Scratch]" line 0\n' 4242.. ' 4 h "[Scratch]" line 0' , 4243exec_capture ( 'ls' ) 4244) 4245-- current buffer didn't change 4246eq ( 1 , api . nvim_get_current_buf ()) 4247 4248local screen =Screen . new ( 20 , 4 ) 4249 4250-- 4251-- Editing a scratch-buffer does NOT change its properties. 4252-- 4253local edited_buf =2 4254api . nvim_buf_set_lines ( edited_buf , 0 , - 1 , true , { 'some text' } ) 4255for _ , b in ipairs ( scratch_bufs ) do 4256eq ( 'nofile' , api . nvim_get_option_value ( 'buftype' , { buf =b } )) 4257eq ( 'hide' , api . nvim_get_option_value ( 'bufhidden' , { buf =b } )) 4258eq ( false , api . nvim_get_option_value ( 'swapfile' , { buf =b } )) 4259eq ( false , api . nvim_get_option_value ( 'modeline' , { buf =b } )) 4260end 4261 4262-- 4263-- Visiting a scratch-buffer DOES NOT change its properties. 4264-- 4265api . nvim_set_current_buf ( edited_buf ) 4266screen : expect ( [[ 4267^some text | 4268{1:~ }|*2 4269| 4270]] ) 4271eq ( 'nofile' , api . nvim_get_option_value ( 'buftype' , { buf =edited_buf } )) 4272eq ( 'hide' , api . nvim_get_option_value ( 'bufhidden' , { buf =edited_buf } )) 4273eq ( false , api . nvim_get_option_value ( 'swapfile' , { buf =edited_buf } )) 4274eq ( false , api . nvim_get_option_value ( 'modeline' , { buf =edited_buf } )) 4275 4276-- Scratch buffer can be wiped without error. 4277command ( 'bwipe' ) 4278screen : expect ( [[ 4279^ | 4280{1:~ }|*2 4281| 4282]] ) 4283end ) 4284 4285it ( 'does not cause heap-use-after-free on exit while setting options' , function () 4286command ( 'au OptionSet * q' ) 4287command ( 'silent! call nvim_create_buf(0, 1)' ) 4288-- nowadays this works because we don't execute any spurious autocmds at all #24824 4289assert_alive () 4290end ) 4291 4292it ( 'no memory leak when autocommands load the buffer immediately' , function () 4293exec ( [[ 4294autocmd BufNew * ++once call bufload(expand("<abuf>")->str2nr()) 4295\| let loaded = bufloaded(expand("<abuf>")->str2nr()) 4296]] ) 4297api . nvim_create_buf ( false , true ) 4298eq ( 1 , eval ( 'g:loaded' )) 4299end ) 4300 4301it ( 'creating scratch buffer where autocommands set &swapfile works' , function () 4302exec ( [[ 4303autocmd BufNew * ++once execute expand("<abuf>") "buffer" 4304\| file foobar 4305\| setlocal swapfile 4306]] ) 4307local new_buf =api . nvim_create_buf ( false , true ) 4308neq ( '' , fn . swapname ( new_buf )) 4309end ) 4310 4311it ( 'fires expected autocommands' , function () 4312exec ( [=[ 4313" Append the &buftype to check autocommands trigger *after* the buffer was configured to be 4314" scratch, if applicable. 4315autocmd BufNew * let fired += [["BufNew", expand("<abuf>")->str2nr(), 4316\ getbufvar(expand("<abuf>")->str2nr(), "&buftype")]] 4317autocmd BufAdd * let fired += [["BufAdd", expand("<abuf>")->str2nr(), 4318\ getbufvar(expand("<abuf>")->str2nr(), "&buftype")]] 4319 4320" Don't want to see OptionSet; buffer options set from passing true for "scratch", etc. 4321" should be configured invisibly, and before autocommands. 4322autocmd OptionSet * let fired += [["OptionSet", expand("<amatch>")]] 4323 4324let fired = [] 4325]=] ) 4326local new_buf =api . nvim_create_buf ( false , false ) 4327eq ( { { 'BufNew' , new_buf , '' } } , eval ( 'g:fired' )) 4328 4329command ( 'let fired = []' ) 4330new_buf =api . nvim_create_buf ( false , true ) 4331eq ( { { 'BufNew' , new_buf , 'nofile' } } , eval ( 'g:fired' )) 4332 4333command ( 'let fired = []' ) 4334new_buf =api . nvim_create_buf ( true , false ) 4335eq ( { { 'BufNew' , new_buf , '' } , { 'BufAdd' , new_buf , '' } } , eval ( 'g:fired' )) 4336 4337command ( 'let fired = []' ) 4338new_buf =api . nvim_create_buf ( true , true ) 4339eq ( { { 'BufNew' , new_buf , 'nofile' } , { 'BufAdd' , new_buf , 'nofile' } } , eval ( 'g:fired' )) 4340end ) 4341end ) 4342 4343describe ( 'nvim_get_runtime_file' , function () 4344it ( 'can find files' , function () 4345eq ( {} , api . nvim_get_runtime_file ( 'bork.borkbork' , false )) 4346eq ( {} , api . nvim_get_runtime_file ( 'bork.borkbork' , true )) 4347eq ( 1 , # api . nvim_get_runtime_file ( 'autoload/msgpack.vim' , false )) 4348eq ( 1 , # api . nvim_get_runtime_file ( 'autoload/msgpack.vim' , true )) 4349local val =api . nvim_get_runtime_file ( 'autoload/remote/*.vim' , true ) 4350eq ( 2 , # val ) 4351if endswith ( val [ 1 ], 'define.vim' ) then 4352ok ( endswith ( val [ 1 ], 'autoload/remote/define.vim' )) 4353ok ( endswith ( val [ 2 ], 'autoload/remote/host.vim' )) 4354else 4355ok ( endswith ( val [ 1 ], 'autoload/remote/host.vim' )) 4356ok ( endswith ( val [ 2 ], 'autoload/remote/define.vim' )) 4357end 4358val =api . nvim_get_runtime_file ( 'autoload/remote/*.vim' , false ) 4359eq ( 1 , # val ) 4360ok ( 4361endswith ( val [ 1 ], 'autoload/remote/define.vim' ) 4362or endswith ( val [ 1 ], 'autoload/remote/host.vim' ) 4363) 4364 4365val =api . nvim_get_runtime_file ( 'lua' , true ) 4366eq ( 1 , # val ) 4367ok ( endswith ( val [ 1 ], 'lua' )) 4368 4369val =api . nvim_get_runtime_file ( 'lua/vim' , true ) 4370eq ( 1 , # val ) 4371ok ( endswith ( val [ 1 ], 'lua/vim' )) 4372end ) 4373 4374it ( 'can find directories' , function () 4375local val =api . nvim_get_runtime_file ( 'lua/' , true ) 4376eq ( 1 , # val ) 4377ok ( endswith ( val [ 1 ], 'lua/' )) 4378 4379val =api . nvim_get_runtime_file ( 'lua/vim/' , true ) 4380eq ( 1 , # val ) 4381ok ( endswith ( val [ 1 ], 'lua/vim/' )) 4382 4383eq ( {} , api . nvim_get_runtime_file ( 'foobarlang/' , true )) 4384end ) 4385it ( 'can handle bad patterns' , function () 4386skip ( is_os ( 'win' )) 4387 4388eq ( 'Vim:E220: Missing }.' , pcall_err ( api . nvim_get_runtime_file , '{' , false )) 4389 4390eq ( 4391'Vim(echo):E5555: API call: Vim:E220: Missing }.' , 4392pcall_err ( command , "echo nvim_get_runtime_file('{', v:false)" ) 4393) 4394end ) 4395it ( 'preserves order of runtimepath' , function () 4396local vimruntime =fn . getenv ( 'VIMRUNTIME' ) 4397local rtp =string . format ( '%s/syntax,%s/ftplugin' , vimruntime , vimruntime ) 4398api . nvim_set_option_value ( 'runtimepath' , rtp , {} ) 4399 4400local val =api . nvim_get_runtime_file ( 'vim.vim' , true ) 4401eq ( 2 , # val ) 4402eq ( val [ 1 ], ( '%s/syntax/vim.vim' ): format ( vimruntime )) 4403eq ( val [ 2 ], ( '%s/ftplugin/vim.vim' ): format ( vimruntime )) 4404end ) 4405 4406it ( 'finds files via an 8.3 filename path #25019' , function () 4407skip ( not is_os ( 'win' ), 'N/A: 8.3 filenames are only available on Windows' ) 4408fn . system (( 'fsutil 8dot3name set %s 0' ): format ( n . nvim_dir : sub ( 1 , 2 ))) 4409local path ='Xtest_runtime_path' 4410mkdir_p (( '%s/subdir/lua' ): format ( path )) 4411write_file (( '%s/subdir/lua/foo.lua' ): format ( path ), '' ) 4412finally ( function () 4413rmdir ( path ) 4414end ) 4415local path_with_shortname =fn . system (( 'for %%I in ("%s") do @echo %%~sI' ): format ( path ), '' ) 4416path_with_shortname =vim . fs . normalize ( vim . trim ( path_with_shortname )) 4417eq ( 'XTEST_~1' , vim . fs . basename ( path_with_shortname )) 4418exec_lua (( 'vim.opt.rtp:prepend("%s/*")' ): format ( path_with_shortname )) 4419local val =api . nvim_get_runtime_file ( 'lua/foo.lua' , true ) 4420eq ( 1 , # val ) 4421eq (( '%s/subdir/lua/foo.lua' ): format ( path_with_shortname ), val [ 1 ]) 4422end ) 4423end ) 4424 4425describe ( 'nvim_get_all_options_info' , function () 4426it ( 'should have key value pairs of option names' , function () 4427local options_info =api . nvim_get_all_options_info () 4428neq ( nil , options_info . listchars ) 4429neq ( nil , options_info . tabstop ) 4430 4431eq ( api . nvim_get_option_info 'winhighlight' , options_info . winhighlight ) 4432end ) 4433 4434it ( 'should not crash when echoed' , function () 4435api . nvim_exec2 ( 'echo nvim_get_all_options_info()' , { output =true } ) 4436end ) 4437end ) 4438 4439describe ( 'nvim_get_option_info' , function () 4440it ( 'should error for unknown options' , function () 4441eq ( "Invalid option (not found): 'bogus'" , pcall_err ( api . nvim_get_option_info , 'bogus' )) 4442end ) 4443 4444it ( 'should return the same options for short and long name' , function () 4445eq ( api . nvim_get_option_info 'winhl' , api . nvim_get_option_info 'winhighlight' ) 4446end ) 4447 4448it ( 'should have information about window options' , function () 4449eq ( { 4450allows_duplicates =false , 4451commalist =true , 4452default ='' , 4453flaglist =false , 4454global_local =false , 4455last_set_chan =0 , 4456last_set_linenr =0 , 4457last_set_sid =0 , 4458name ='winhighlight' , 4459scope ='win' , 4460shortname ='winhl' , 4461type ='string' , 4462was_set =false , 4463} , api . nvim_get_option_info 'winhl' ) 4464end ) 4465 4466it ( 'should have information about buffer options' , function () 4467eq ( { 4468allows_duplicates =true , 4469commalist =false , 4470default ='' , 4471flaglist =false , 4472global_local =false , 4473last_set_chan =0 , 4474last_set_linenr =0 , 4475last_set_sid =0 , 4476name ='filetype' , 4477scope ='buf' , 4478shortname ='ft' , 4479type ='string' , 4480was_set =false , 4481} , api . nvim_get_option_info 'filetype' ) 4482end ) 4483 4484it ( 'should have information about global options' , function () 4485-- precondition: the option was changed from its default 4486-- in test setup. 4487eq ( false , api . nvim_get_option_value ( 'showcmd' , {} )) 4488 4489eq ( { 4490allows_duplicates =true , 4491commalist =false , 4492default =true , 4493flaglist =false , 4494global_local =false , 4495last_set_chan =0 , 4496last_set_linenr =0 , 4497last_set_sid =- 2 , 4498name ='showcmd' , 4499scope ='global' , 4500shortname ='sc' , 4501type ='boolean' , 4502was_set =true , 4503} , api . nvim_get_option_info 'showcmd' ) 4504 4505api . nvim_set_option_value ( 'showcmd' , true , {} ) 4506 4507eq ( { 4508allows_duplicates =true , 4509commalist =false , 4510default =true , 4511flaglist =false , 4512global_local =false , 4513last_set_chan =1 , 4514last_set_linenr =0 , 4515last_set_sid =- 9 , 4516name ='showcmd' , 4517scope ='global' , 4518shortname ='sc' , 4519type ='boolean' , 4520was_set =true , 4521} , api . nvim_get_option_info 'showcmd' ) 4522end ) 4523end ) 4524 4525describe ( 'nvim_get_option_info2' , function () 4526local fname 4527local bufs 4528local wins 4529 4530before_each ( function () 4531fname =tmpname () 4532write_file ( 4533fname , 4534[[ 4535setglobal dictionary=mydict " 1, global-local (buffer) 4536setlocal formatprg=myprg " 2, global-local (buffer) 4537setglobal equalprg=prg1 " 3, global-local (buffer) 4538setlocal equalprg=prg2 " 4, global-local (buffer) 4539setglobal fillchars=stl:x " 5, global-local (window) 4540setlocal listchars=eol:c " 6, global-local (window) 4541setglobal showbreak=aaa " 7, global-local (window) 4542setlocal showbreak=bbb " 8, global-local (window) 4543setglobal completeopt=menu " 9, global 4544]] 4545) 4546 4547exec_lua 'vim.cmd.vsplit()' 4548api . nvim_create_buf ( false , false ) 4549 4550bufs =api . nvim_list_bufs () 4551wins =api . nvim_list_wins () 4552 4553api . nvim_win_set_buf ( wins [ 1 ], bufs [ 1 ]) 4554api . nvim_win_set_buf ( wins [ 2 ], bufs [ 2 ]) 4555 4556api . nvim_set_current_win ( wins [ 2 ]) 4557api . nvim_exec ( 'source ' .. fname , false ) 4558 4559api . nvim_set_current_win ( wins [ 1 ]) 4560end ) 4561 4562after_each ( function () 4563os . remove ( fname ) 4564end ) 4565 4566it ( 'gets option info' , function () 4567eq ( api . nvim_get_option_info ( 'dictionary' ), api . nvim_get_option_info2 ( 'dictionary' , {} )) -- buffer 4568eq ( api . nvim_get_option_info ( 'fillchars' ), api . nvim_get_option_info2 ( 'fillchars' , {} )) -- window 4569eq ( api . nvim_get_option_info ( 'completeopt' ), api . nvim_get_option_info2 ( 'completeopt' , {} )) -- global 4570eq ( 'tab' , api . nvim_get_option_info2 ( 'cmdheight' , {} ). scope ) -- tab #31140 4571end ) 4572 4573describe ( 'last set' , function () 4574-- stylua: ignore 4575local tests ={ 4576{ desc ="(buf option, global requested, global set) points to global" , linenr =1 , sid =1 , args ={ 'dictionary' , { scope ='global' }}} , 4577{ desc ="(buf option, global requested, local set) is not set" , linenr =0 , sid =0 , args ={ 'formatprg' , { scope ='global' }}} , 4578{ desc ="(buf option, global requested, both set) points to global" , linenr =3 , sid =1 , args ={ 'equalprg' , { scope ='global' }}} , 4579{ desc ="(buf option, local requested, global set) is not set" , linenr =0 , sid =0 , args ={ 'dictionary' , { scope ='local' }}} , 4580{ desc ="(buf option, local requested, local set) points to local" , linenr =2 , sid =1 , args ={ 'formatprg' , { scope ='local' }}} , 4581{ desc ="(buf option, local requested, both set) points to local" , linenr =4 , sid =1 , args ={ 'equalprg' , { scope ='local' }}} , 4582{ desc ="(buf option, fallback requested, global set) points to global" , linenr =1 , sid =1 , args ={ 'dictionary' , {}}} , 4583{ desc ="(buf option, fallback requested, local set) points to local" , linenr =2 , sid =1 , args ={ 'formatprg' , {}}} , 4584{ desc ="(buf option, fallback requested, both set) points to local" , linenr =4 , sid =1 , args ={ 'equalprg' , {}}} , 4585{ desc ="(win option, global requested, global set) points to global" , linenr =5 , sid =1 , args ={ 'fillchars' , { scope ='global' }}} , 4586{ desc ="(win option, global requested, local set) is not set" , linenr =0 , sid =0 , args ={ 'listchars' , { scope ='global' }}} , 4587{ desc ="(win option, global requested, both set) points to global" , linenr =7 , sid =1 , args ={ 'showbreak' , { scope ='global' }}} , 4588{ desc ="(win option, local requested, global set) is not set" , linenr =0 , sid =0 , args ={ 'fillchars' , { scope ='local' }}} , 4589{ desc ="(win option, local requested, local set) points to local" , linenr =6 , sid =1 , args ={ 'listchars' , { scope ='local' }}} , 4590{ desc ="(win option, local requested, both set) points to local" , linenr =8 , sid =1 , args ={ 'showbreak' , { scope ='local' }}} , 4591{ desc ="(win option, fallback requested, global set) points to global" , linenr =5 , sid =1 , args ={ 'fillchars' , {}}} , 4592{ desc ="(win option, fallback requested, local set) points to local" , linenr =6 , sid =1 , args ={ 'listchars' , {}}} , 4593{ desc ="(win option, fallback requested, both set) points to local" , linenr =8 , sid =1 , args ={ 'showbreak' , {}}} , 4594{ desc ="(global option, global requested) points to global" , linenr =9 , sid =1 , args ={ 'completeopt' , { scope ='global' }}} , 4595{ desc ="(global option, local requested) is not set" , linenr =0 , sid =0 , args ={ 'completeopt' , { scope ='local' }}} , 4596{ desc ="(global option, fallback requested) points to global" , linenr =9 , sid =1 , args ={ 'completeopt' , {}}} , 4597} 4598 4599for _ , test in pairs ( tests ) do 4600it ( test . desc , function () 4601-- Switch to the target buffer/window so that curbuf/curwin are used. 4602api . nvim_set_current_win ( wins [ 2 ]) 4603local info =api . nvim_get_option_info2 ( unpack ( test . args )) 4604eq ( test . linenr , info . last_set_linenr ) 4605eq ( test . sid , info . last_set_sid ) 4606end ) 4607end 4608 4609it ( 'cross-buffer' , function () 4610local info =api . nvim_get_option_info2 ( 'formatprg' , { buf =bufs [ 2 ] } ) 4611eq ( 2 , info . last_set_linenr ) 4612eq ( 1 , info . last_set_sid ) 4613end ) 4614 4615it ( 'cross-window' , function () 4616local info =api . nvim_get_option_info2 ( 'listchars' , { win =wins [ 2 ] } ) 4617eq ( 6 , info . last_set_linenr ) 4618eq ( 1 , info . last_set_sid ) 4619end ) 4620end ) 4621end ) 4622 4623describe ( 'nvim_echo' , function () 4624local screen 4625 4626before_each ( function () 4627screen =Screen . new ( 40 , 8 ) 4628command ( 'highlight Statement gui=bold guifg=Brown' ) 4629command ( 'highlight Special guifg=SlateBlue' ) 4630end ) 4631 4632it ( 'validation' , function () 4633eq ( "Invalid 'chunk': expected Array, got String" , pcall_err ( api . nvim_echo , { 'msg' } , 1 , {} )) 4634eq ( 4635'Invalid chunk: expected Array with 1 or 2 Strings' , 4636pcall_err ( api . nvim_echo , { { '' , '' , '' } } , 1 , {} ) 4637) 4638eq ( "Invalid 'hl_group': 'text highlight'" , pcall_err ( api . nvim_echo , { { '' , false } } , 1 , {} )) 4639eq ( "Invalid 'id': 4" , pcall_err ( api . nvim_echo , { { 'foo' } } , false , { id =4 } )) 4640eq ( "Invalid 'id': 0" , pcall_err ( api . nvim_echo , { { 'foo' } } , false , { id =0 } )) 4641eq ( "Invalid 'id': -1" , pcall_err ( api . nvim_echo , { { 'foo' } } , false , { id =- 1 } )) 4642-- String ids are always allowed (user-defined). 4643eq ( 'my.msg.id' , api . nvim_echo ( { { 'foo' } } , false , { id ='my.msg.id' } )) 4644local opts ={ kind ='progress' , source ='nvim' , status ='success' } 4645eq ( "Invalid 'source': 'nvim'" , pcall_err ( api . nvim_echo , { { '' } } , 1 , opts )) 4646end ) 4647 4648it ( 'should clear cmdline message before echo' , function () 4649feed ( ':call nvim_echo([["msg"]], v:false, {})<CR>' ) 4650screen : expect { 4651grid =[[ 4652^ | 4653{1:~ }|*6 4654msg | 4655]] , 4656} 4657end ) 4658 4659it ( 'can show highlighted line' , function () 4660async_meths . nvim_echo ( 4661{ { 'msg_a' } , { 'msg_b' , 'Statement' } , { 'msg_c' , 'Special' } } , 4662true , 4663{} 4664) 4665screen : expect { 4666grid =[[ 4667^ | 4668{1:~ }|*6 4669msg_a{15:msg_b}{16:msg_c} | 4670]] , 4671} 4672async_meths . nvim_echo ( { 4673{ 'msg_d' } , 4674{ 'msg_e' , api . nvim_get_hl_id_by_name ( 'Statement' ) } , 4675{ 'msg_f' , api . nvim_get_hl_id_by_name ( 'Special' ) } , 4676} , true , {} ) 4677screen : expect { 4678grid =[[ 4679^ | 4680{1:~ }|*6 4681msg_d{15:msg_e}{16:msg_f} | 4682]] , 4683} 4684end ) 4685 4686it ( 'can show highlighted multiline' , function () 4687async_meths . nvim_echo ( { { 'msg_a\nmsg_a' , 'Statement' } , { 'msg_b' , 'Special' } } , true , {} ) 4688screen : expect { 4689grid =[[ 4690| 4691{1:~ }|*3 4692{3: }| 4693{15:msg_a} | 4694{15:msg_a}{16:msg_b} | 4695{6:Press ENTER or type command to continue}^ | 4696]] , 4697} 4698end ) 4699 4700it ( 'can save message history' , function () 4701command ( 'set cmdheight=2' ) -- suppress Press ENTER 4702api . nvim_echo ( { { 'msg\nmsg' } , { 'msg' } } , true , {} ) 4703eq ( 'msg\nmsgmsg' , exec_capture ( 'messages' )) 4704end ) 4705 4706it ( 'can disable saving message history' , function () 4707command ( 'set cmdheight=2' ) -- suppress Press ENTER 4708async_meths . nvim_echo ( { { 'msg\nmsg' } , { 'msg' } } , false , {} ) 4709eq ( '' , exec_capture ( 'messages' )) 4710end ) 4711 4712it ( 'can print error message' , function () 4713async_meths . nvim_echo ( { { 'Error\nMessage' } } , false , { err =true } ) 4714screen : expect ( [[ 4715| 4716{1:~ }|*3 4717{3: }| 4718{9:Error} | 4719{9:Message} | 4720{6:Press ENTER or type command to continue}^ | 4721]] ) 4722feed ( ':messages<CR>' ) 4723screen : expect ( [[ 4724^ | 4725{1:~ }|*6 4726| 4727]] ) 4728async_meths . nvim_echo ( { { 'Error' } , { 'Message' , 'Special' } } , false , { err =true } ) 4729screen : expect ( [[ 4730^ | 4731{1:~ }|*6 4732{9:Error}{16:Message} | 4733]] ) 4734end ) 4735 4736it ( 'increments message ID' , function () 4737eq ( 1 , api . nvim_echo ( { { 'foo' } } , false , {} )) 4738eq ( 1 , api . nvim_echo ( { { 'foo' } } , false , { id =1 } )) -- User may pass existing id. 4739eq ( 2 , api . nvim_echo ( { { 'foo' } } , false , {} )) 4740end ) 4741 4742it ( 'no use-after-free for custom kind with :messages #38289' , function () 4743exec_lua ( function () 4744vim . api . nvim_echo ( { { 'a' } } , true , { kind ='foo' } ) 4745vim . o . guicursor ='' -- pending mode update go brrr 4746vim . api . nvim__redraw ( { flush =true } ) -- ui_flush -> arena_mem_free go brrr 4747vim . cmd . messages () 4748end ) 4749end ) 4750end ) 4751 4752describe ( 'nvim_open_term' , function () 4753local screen 4754 4755before_each ( function () 4756screen =Screen . new ( 100 , 35 ) 4757screen : add_extra_attr_ids { 4758[ 100 ] ={ background =tonumber ( '0xffff40' ), bg_indexed =true } , 4759[ 101 ] ={ 4760background =Screen . colors . LightMagenta , 4761foreground =tonumber ( '0x00e000' ), 4762fg_indexed =true , 4763} , 4764[ 102 ] ={ background =Screen . colors . LightMagenta , reverse =true } , 4765[ 103 ] ={ background =Screen . colors . LightMagenta , bold =true , reverse =true } , 4766[ 104 ] ={ fg_indexed =true , foreground =tonumber ( '0xe00000' ) } , 4767[ 105 ] ={ fg_indexed =true , foreground =tonumber ( '0xe0e000' ) } , 4768} 4769end ) 4770 4771it ( 'can batch process sequences' , function () 4772local b =api . nvim_create_buf ( true , true ) 4773api . nvim_open_win ( 4774b , 4775false , 4776{ width =79 , height =31 , row =1 , col =1 , relative ='editor' } 4777) 4778local term =api . nvim_open_term ( b , {} ) 4779 4780api . nvim_chan_send ( term , io . open ( 'test/functional/fixtures/smile2.cat' , 'r' ): read ( '*a' )) 4781screen : expect { 4782grid =[[ 4783^ | 4784{1:~}{4::smile }{1: }| 4785{1:~}{4: }{100:oooo$$$$$$$$$$$$oooo}{4: }{1: }| 4786{1:~}{4: }{100:oo$$$$$$$$$$$$$$$$$$$$$$$$o}{4: }{1: }| 4787{1:~}{4: }{100:oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o}{4: }{100:o$}{4: }{100:$$}{4: }{100:o$}{4: }{1: }| 4788{1:~}{4: }{100:o}{4: }{100:$}{4: }{100:oo}{4: }{100:o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o}{4: }{100:$$}{4: }{100:$$}{4: }{100:$$o$}{4: }{1: }| 4789{1:~}{4: }{100:oo}{4: }{100:$}{4: }{100:$}{4: "}{100:$}{4: }{100:o$$$$$$$$$}{4: }{100:$$$$$$$$$$$$$}{4: }{100:$$$$$$$$$o}{4: }{100:$$$o$$o$}{4: }{1: }| 4790{1:~}{4: "}{100:$$$$$$o$}{4: }{100:o$$$$$$$$$}{4: }{100:$$$$$$$$$$$}{4: }{100:$$$$$$$$$$o}{4: }{100:$$$$$$$$}{4: }{1: }| 4791{1:~}{4: }{100:$$$$$$$}{4: }{100:$$$$$$$$$$$}{4: }{100:$$$$$$$$$$$}{4: }{100:$$$$$$$$$$$$$$$$$$$$$$$}{4: }{1: }| 4792{1:~}{4: }{100:$$$$$$$$$$$$$$$$$$$$$$$}{4: }{100:$$$$$$$$$$$$$}{4: }{100:$$$$$$$$$$$$$$}{4: """}{100:$$$}{4: }{1: }| 4793{1:~}{4: "}{100:$$$}{4:""""}{100:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}{4: "}{100:$$$}{4: }{1: }| 4794{1:~}{4: }{100:$$$}{4: }{100:o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}{4: "}{100:$$$o}{4: }{1: }| 4795{1:~}{4: }{100:o$$}{4:" }{100:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}{4: }{100:$$$o}{4: }{1: }| 4796{1:~}{4: }{100:$$$}{4: }{100:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}{4:" "}{100:$$$$$$ooooo$$$$o}{4: }{1: }| 4797{1:~}{4: }{100:o$$$oooo$$$$$}{4: }{100:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}{4: }{100:o$$$$$$$$$$$$$$$$$}{4: }{1: }| 4798{1:~}{4: }{100:$$$$$$$$}{4:"}{100:$$$$}{4: }{100:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}{4: }{100:$$$$}{4:"""""""" }{1: }| 4799{1:~}{4: """" }{100:$$$$}{4: "}{100:$$$$$$$$$$$$$$$$$$$$$$$$$$$$}{4:" }{100:o$$$}{4: }{1: }| 4800{1:~}{4: "}{100:$$$o}{4: """}{100:$$$$$$$$$$$$$$$$$$}{4:"}{100:$$}{4:" }{100:$$$}{4: }{1: }| 4801{1:~}{4: }{100:$$$o}{4: "}{100:$$}{4:""}{100:$$$$$$}{4:"""" }{100:o$$$}{4: }{1: }| 4802{1:~}{4: }{100:$$$$o}{4: }{100:o$$$}{4:" }{1: }| 4803{1:~}{4: "}{100:$$$$o}{4: }{100:o$$$$$$o}{4:"}{100:$$$$o}{4: }{100:o$$$$}{4: }{1: }| 4804{1:~}{4: "}{100:$$$$$oo}{4: ""}{100:$$$$o$$$$$o}{4: }{100:o$$$$}{4:"" }{1: }| 4805{1:~}{4: ""}{100:$$$$$oooo}{4: "}{100:$$$o$$$$$$$$$}{4:""" }{1: }| 4806{1:~}{4: ""}{100:$$$$$$$oo}{4: }{100:$$$$$$$$$$}{4: }{1: }| 4807{1:~}{4: """"}{100:$$$$$$$$$$$}{4: }{1: }| 4808{1:~}{4: }{100:$$$$$$$$$$$$}{4: }{1: }| 4809{1:~}{4: }{100:$$$$$$$$$$}{4:" }{1: }| 4810{1:~}{4: "}{100:$$$}{4:"""" }{1: }| 4811{1:~}{4: }{1: }| 4812{1:~}{101:Press ENTER or type command to continue}{4: }{1: }| 4813{1:~}{103:term://~/config2/docs/pres//32693:vim --clean +smile 29,39 All}{1: }| 4814{1:~}{4::call nvim__screenshot("smile2.cat") }{1: }| 4815{1:~ }|*2 4816| 4817]] , 4818} 4819end ) 4820 4821it ( 'can handle input' , function () 4822screen : try_resize ( 50 , 10 ) 4823eq ( 4824{ 3 , 2 } , 4825exec_lua [[ 4826buf = vim.api.nvim_create_buf(1,1) 4827 4828stream = '' 4829do_the_echo = false 4830function input(_,t1,b1,data) 4831stream = stream .. data 4832_G.vals = {t1, b1} 4833if do_the_echo then 4834vim.api.nvim_chan_send(t1, data) 4835end 4836end 4837 4838term = vim.api.nvim_open_term(buf, {on_input=input}) 4839vim.api.nvim_open_win(buf, true, {width=40, height=5, row=1, col=1, relative='editor'}) 4840return {term, buf} 4841]] 4842) 4843 4844screen : expect { 4845grid =[[ 4846| 4847{1:~}{4:^ }{1: }| 4848{1:~}{4: }{1: }|*4 4849{1:~ }|*3 4850| 4851]] , 4852} 4853 4854feed 'iba<c-x>bla' 4855screen : expect { 4856grid =[[ 4857| 4858{1:~}{4:^ }{1: }| 4859{1:~}{4: }{1: }|*4 4860{1:~ }|*3 4861{5:-- TERMINAL --} | 4862]] , 4863} 4864 4865eq ( 'ba\024bla' , exec_lua [[ return stream ]] ) 4866eq ( { 3 , 2 } , exec_lua [[ return vals ]] ) 4867 4868exec_lua [[ do_the_echo = true ]] 4869feed 'herrejösses!' 4870 4871screen : expect { 4872grid =[[ 4873| 4874{1:~}{4:herrejösses!^ }{1: }| 4875{1:~}{4: }{1: }|*4 4876{1:~ }|*3 4877{5:-- TERMINAL --} | 4878]] , 4879} 4880eq ( 'ba\024blaherrejösses!' , exec_lua [[ return stream ]] ) 4881end ) 4882 4883it ( 'parses text from the current buffer' , function () 4884local b =api . nvim_create_buf ( true , true ) 4885api . nvim_buf_set_lines ( b , 0 , - 1 , true , { '\027[31mHello\000\027[0m' , '\027[33mworld\027[0m' } ) 4886api . nvim_set_current_buf ( b ) 4887screen : expect ( [[ 4888{18:^^[}[31mHello{18:^@^[}[0m | 4889{18:^[}[33mworld{18:^[}[0m | 4890{1:~ }|*32 4891| 4892]] ) 4893api . nvim_open_term ( b , {} ) 4894screen : expect ( [[ 4895{104:^Hello} | 4896{105:world} | 4897|*33 4898]] ) 4899end ) 4900end ) 4901 4902describe ( 'nvim_del_mark' , function () 4903it ( 'works' , function () 4904local buf =api . nvim_create_buf ( false , true ) 4905api . nvim_buf_set_lines ( buf , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 4906eq ( true , api . nvim_buf_set_mark ( buf , 'F' , 2 , 2 , {} )) 4907eq ( true , api . nvim_del_mark ( 'F' )) 4908eq ( { 0 , 0 } , api . nvim_buf_get_mark ( buf , 'F' )) 4909end ) 4910it ( 'validation' , function () 4911eq ( "Invalid mark name (must be file/uppercase): 'f'" , pcall_err ( api . nvim_del_mark , 'f' )) 4912eq ( "Invalid mark name (must be file/uppercase): '!'" , pcall_err ( api . nvim_del_mark , '!' )) 4913eq ( "Invalid mark name (must be a single char): 'fail'" , pcall_err ( api . nvim_del_mark , 'fail' )) 4914end ) 4915end ) 4916describe ( 'nvim_get_mark' , function () 4917it ( 'works' , function () 4918local buf =api . nvim_create_buf ( false , true ) 4919api . nvim_buf_set_lines ( buf , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 4920api . nvim_buf_set_mark ( buf , 'F' , 2 , 2 , {} ) 4921api . nvim_buf_set_name ( buf , 'mybuf' ) 4922local mark =api . nvim_get_mark ( 'F' , {} ) 4923-- Compare the path tail only 4924matches ( 'mybuf$' , mark [ 4 ]) 4925eq ( { 2 , 2 , buf , mark [ 4 ] } , mark ) 4926end ) 4927it ( 'validation' , function () 4928eq ( "Invalid mark name (must be file/uppercase): 'f'" , pcall_err ( api . nvim_get_mark , 'f' , {} )) 4929eq ( "Invalid mark name (must be file/uppercase): '!'" , pcall_err ( api . nvim_get_mark , '!' , {} )) 4930eq ( 4931"Invalid mark name (must be a single char): 'fail'" , 4932pcall_err ( api . nvim_get_mark , 'fail' , {} ) 4933) 4934end ) 4935it ( 'returns the expected when mark is not set' , function () 4936eq ( true , api . nvim_del_mark ( 'A' )) 4937eq ( { 0 , 0 , 0 , '' } , api . nvim_get_mark ( 'A' , {} )) 4938end ) 4939it ( 'works with deleted buffers' , function () 4940local fname =tmpname () 4941write_file ( fname , 'a\nbit of\text' ) 4942command ( 'edit ' .. fname ) 4943local buf =api . nvim_get_current_buf () 4944 4945api . nvim_buf_set_mark ( buf , 'F' , 2 , 2 , {} ) 4946command ( 'new' ) -- Create new buf to avoid :bd failing 4947command ( 'bd! ' .. buf ) 4948os . remove ( fname ) 4949 4950local mark =api . nvim_get_mark ( 'F' , {} ) 4951-- To avoid comparing relative vs absolute path 4952local mfname =mark [ 4 ] 4953local tail_patt =[[[\/][^\/]*$]] 4954-- tail of paths should be equals 4955eq ( fname : match ( tail_patt ), mfname : match ( tail_patt )) 4956eq ( { 2 , 2 , buf , mark [ 4 ] } , mark ) 4957end ) 4958end ) 4959 4960describe ( 'nvim_eval_statusline' , function () 4961it ( 'works' , function () 4962eq ( { 4963str ='%StatusLineStringWithHighlights' , 4964width =31 , 4965} , api . nvim_eval_statusline ( '%%StatusLineString%#WarningMsg#WithHighlights' , {} )) 4966end ) 4967 4968it ( 'rejects invalid format characters' , function () 4969eq ( 'E539: Illegal character <^>' , pcall_err ( api . nvim_eval_statusline , '%^' , {} )) 4970eq ( 'E539: Illegal character <}>' , pcall_err ( api . nvim_eval_statusline , '%{%}' , {} )) 4971end ) 4972 4973it ( 'reports an invalid window once' , function () 4974-- find_window_by_handle() already sets the error, so a second 4975-- api_set_error() here would allocate a message over that one and leak it. 4976eq ( 'Invalid window id: 23' , pcall_err ( api . nvim_eval_statusline , 'a' , { winid =23 } )) 4977end ) 4978 4979it ( "doesn't exceed maxwidth" , function () 4980eq ( { 4981str ='Should be trun>' , 4982width =15 , 4983} , api . nvim_eval_statusline ( 'Should be truncated%<' , { maxwidth =15 } )) 4984end ) 4985 4986it ( 'does not take a zero item width literally' , function () 4987command ( 'file some/dir/testfile.txt' ) 4988eq ( { str ='abc' , width =3 } , api . nvim_eval_statusline ( '%.0(abc%)' , {} )) 4989eq ( api . nvim_eval_statusline ( '%.50f' , {} ), api . nvim_eval_statusline ( '%.0f' , {} )) 4990eq ( api . nvim_eval_statusline ( '%.50l' , {} ), api . nvim_eval_statusline ( '%.0l' , {} )) 4991end ) 4992 4993it ( 'has correct default fillchar' , function () 4994local oldwin =api . nvim_get_current_win () 4995command ( 'set fillchars=stl:#,stlnc:$,wbr:%' ) 4996command ( 'new' ) 4997eq ( { str ='a###b' , width =5 } , api . nvim_eval_statusline ( 'a%=b' , { maxwidth =5 } )) 4998eq ( 4999{ str ='a$$$b' , width =5 } , 5000api . nvim_eval_statusline ( 'a%=b' , { winid =oldwin , maxwidth =5 } ) 5001) 5002eq ( 5003{ str ='a%%%b' , width =5 } , 5004api . nvim_eval_statusline ( 'a%=b' , { use_winbar =true , maxwidth =5 } ) 5005) 5006eq ( 5007{ str ='a b' , width =5 } , 5008api . nvim_eval_statusline ( 'a%=b' , { use_tabline =true , maxwidth =5 } ) 5009) 5010eq ( 5011{ str ='a b' , width =5 } , 5012api . nvim_eval_statusline ( 'a%=b' , { use_statuscol_lnum =1 , maxwidth =5 } ) 5013) 5014end ) 5015 5016for fc , desc in pairs ( { 5017[ '~' ] ='supports ASCII fillchar' , 5018[ '━' ] ='supports single-width multibyte fillchar' , 5019[ 'c̳' ] ='supports single-width fillchar with composing' , 5020[ '哦' ] ='treats double-width fillchar as single-width' , 5021[ '\031' ] ='treats control character fillchar as single-width' , 5022} ) do 5023it ( desc , function () 5024eq ( 5025{ str ='a' .. fc : rep ( 3 ) .. 'b' , width =5 } , 5026api . nvim_eval_statusline ( 'a%=b' , { fillchar =fc , maxwidth =5 } ) 5027) 5028eq ( 5029{ str ='a' .. fc : rep ( 3 ) .. 'b' , width =5 } , 5030api . nvim_eval_statusline ( 'a%=b' , { fillchar =fc , use_winbar =true , maxwidth =5 } ) 5031) 5032eq ( 5033{ str ='a' .. fc : rep ( 3 ) .. 'b' , width =5 } , 5034api . nvim_eval_statusline ( 'a%=b' , { fillchar =fc , use_tabline =true , maxwidth =5 } ) 5035) 5036eq ( 5037{ str ='a' .. fc : rep ( 3 ) .. 'b' , width =5 } , 5038api . nvim_eval_statusline ( 'a%=b' , { fillchar =fc , use_statuscol_lnum =1 , maxwidth =5 } ) 5039) 5040end ) 5041end 5042 5043it ( 'rejects multiple-character fillchar' , function () 5044eq ( 5045"Invalid 'fillchar': expected single character" , 5046pcall_err ( api . nvim_eval_statusline , '' , { fillchar ='aa' } ) 5047) 5048end ) 5049 5050it ( 'rejects empty string fillchar' , function () 5051eq ( 5052"Invalid 'fillchar': expected single character" , 5053pcall_err ( api . nvim_eval_statusline , '' , { fillchar ='' } ) 5054) 5055end ) 5056 5057it ( 'rejects non-string fillchar' , function () 5058eq ( 5059"Invalid 'fillchar': expected String, got Integer" , 5060pcall_err ( api . nvim_eval_statusline , '' , { fillchar =1 } ) 5061) 5062end ) 5063 5064it ( 'rejects invalid string' , function () 5065eq ( 'E539: Illegal character <}>' , pcall_err ( api . nvim_eval_statusline , '%{%}' , {} )) 5066end ) 5067 5068it ( 'supports various items' , function () 5069eq ( { str ='0' , width =1 } , api . nvim_eval_statusline ( '%l' , { maxwidth =5 } )) 5070command ( 'set readonly' ) 5071eq ( { str ='[RO]' , width =4 } , api . nvim_eval_statusline ( '%r' , { maxwidth =5 } )) 5072local screen =Screen . new ( 80 , 24 ) 5073command ( 'set showcmd' ) 5074feed ( '1234' ) 5075screen : expect ( { any ='1234' } ) 5076eq ( { str ='1234' , width =4 } , api . nvim_eval_statusline ( '%S' , { maxwidth =5 } )) 5077feed ( '56' ) 5078screen : expect ( { any ='123456' } ) 5079eq ( { str ='<3456' , width =5 } , api . nvim_eval_statusline ( '%S' , { maxwidth =5 } )) 5080end ) 5081 5082it ( 'does not include internal mapping keys in %S' , function () 5083command ( 'set showcmd' ) 5084command ( 5085[[nnoremap <F3> <Cmd>let g:showcmd_statusline = nvim_eval_statusline('AAA%SBBB', {}).str<CR>]] 5086) 5087exec_lua ( [[vim.api.nvim_feedkeys(vim.keycode('<F3>'), 'xt', false)]] ) 5088eq ( 'AAABBB' , api . nvim_get_var ( 'showcmd_statusline' )) 5089command ( 'nunmap <F3>' ) 5090 5091exec_lua [[ 5092vim.g.showcmd_statusline = '' 5093vim.keymap.set('n', '<F3>', function() 5094vim.g.showcmd_statusline = vim.api.nvim_eval_statusline('AAA%SBBB', {}).str 5095end) 5096vim.api.nvim_feedkeys(vim.keycode('<F3>'), 'xt', false) 5097]] 5098eq ( 'AAABBB' , api . nvim_get_var ( 'showcmd_statusline' )) 5099end ) 5100 5101describe ( 'highlight parsing' , function () 5102it ( 'works' , function () 5103eq ( 5104{ 5105str ='TextWithWarningHighlightTextWithUserHighlight' , 5106width =45 , 5107highlights ={ 5108{ start =0 , group ='WarningMsg' , groups ={ 'StatusLine' , 'WarningMsg' } } , 5109{ start =24 , group ='User1' , groups ={ 'StatusLine' , 'User1' } } , 5110} , 5111} , 5112api . nvim_eval_statusline ( 5113'%#WarningMsg#TextWithWarningHighlight%1*TextWithUserHighlight' , 5114{ highlights =true } 5115) 5116) 5117end ) 5118 5119it ( 'works with no highlight' , function () 5120eq ( { 5121str ='TextWithNoHighlight' , 5122width =19 , 5123highlights ={ 5124{ start =0 , group ='StatusLine' , groups ={ 'StatusLine' } } , 5125} , 5126} , api . nvim_eval_statusline ( 'TextWithNoHighlight' , { highlights =true } )) 5127end ) 5128 5129it ( 'works with inactive statusline' , function () 5130command ( 'split' ) 5131eq ( 5132{ 5133str ='TextWithNoHighlightTextWithWarningHighlight' , 5134width =43 , 5135highlights ={ 5136{ start =0 , group ='StatusLineNC' , groups ={ 'StatusLineNC' } } , 5137{ start =19 , group ='WarningMsg' , groups ={ 'StatusLineNC' , 'WarningMsg' } } , 5138} , 5139} , 5140api . nvim_eval_statusline ( 5141'TextWithNoHighlight%#WarningMsg#TextWithWarningHighlight' , 5142{ winid =api . nvim_list_wins ()[ 2 ], highlights =true } 5143) 5144) 5145end ) 5146 5147it ( 'works with tabline' , function () 5148eq ( 5149{ 5150str ='TextWithNoHighlightTextWithWarningHighlight' , 5151width =43 , 5152highlights ={ 5153{ start =0 , group ='TabLineFill' , groups ={ 'TabLineFill' } } , 5154{ start =19 , group ='WarningMsg' , groups ={ 'TabLineFill' , 'WarningMsg' } } , 5155} , 5156} , 5157api . nvim_eval_statusline ( 5158'TextWithNoHighlight%#WarningMsg#TextWithWarningHighlight' , 5159{ use_tabline =true , highlights =true } 5160) 5161) 5162end ) 5163 5164it ( 'works with winbar' , function () 5165eq ( 5166{ 5167str ='TextWithNoHighlightTextWithWarningHighlight' , 5168width =43 , 5169highlights ={ 5170{ start =0 , group ='WinBar' , groups ={ 'WinBar' } } , 5171{ start =19 , group ='WarningMsg' , groups ={ 'WinBar' , 'WarningMsg' } } , 5172} , 5173} , 5174api . nvim_eval_statusline ( 5175'TextWithNoHighlight%#WarningMsg#TextWithWarningHighlight' , 5176{ use_winbar =true , highlights =true } 5177) 5178) 5179end ) 5180 5181it ( 'works with statuscolumn' , function () 5182exec ( [[ 5183let &stc='%C%s%=%l ' 5184" should not use "stl" from 'fillchars' 5185set cul nu nuw=3 scl=yes:2 fdc=2 fillchars=stl:# 5186call setline(1, repeat(['aaaaa'], 5)) 5187let g:ns = nvim_create_namespace('') 5188call sign_define('a', {'text':'aa', 'texthl':'IncSearch', 'numhl':'Normal'}) 5189call sign_place(2, 1, 'a', bufnr(), {'lnum':4}) 5190call nvim_buf_set_extmark(0, g:ns, 3, 1, { 'sign_text':'bb', 'sign_hl_group':'ErrorMsg' }) 51911,5fold | 1,5 fold | foldopen! 5192norm 4G 5193]] ) 5194eq ( { 5195str ='││bbaa 4 ' , 5196width =9 , 5197highlights ={ 5198{ group ='CursorLineFold' , start =0 , groups ={ 'CursorLineFold' } } , 5199{ group ='Normal' , start =6 , groups ={ 'Normal' } } , 5200{ group ='ErrorMsg' , start =6 , groups ={ 'CursorLineSign' , 'ErrorMsg' } } , 5201{ group ='IncSearch' , start =8 , groups ={ 'CursorLineSign' , 'IncSearch' } } , 5202{ group ='Normal' , start =10 , groups ={ 'Normal' } } , 5203} , 5204} , api . nvim_eval_statusline ( 5205'%C%s%=%l ' , 5206{ use_statuscol_lnum =4 , highlights =true } 5207)) 5208eq ( 5209{ 5210str =' 3 ' , 5211width =9 , 5212highlights ={ 5213{ group ='LineNr' , start =0 , groups ={ 'LineNr' } } , 5214{ group ='ErrorMsg' , start =8 , groups ={ 'LineNr' , 'ErrorMsg' } } , 5215} , 5216} , 5217api . nvim_eval_statusline ( '%l%#ErrorMsg# ' , { use_statuscol_lnum =3 , highlights =true } ) 5218) 5219end ) 5220 5221it ( 'no memory leak with click functions' , function () 5222api . nvim_eval_statusline ( '%@ClickFunc@StatusLineStringWithClickFunc%T' , {} ) 5223eq ( { 5224str ='StatusLineStringWithClickFunc' , 5225width =29 , 5226} , api . nvim_eval_statusline ( '%@ClickFunc@StatusLineStringWithClickFunc%T' , {} )) 5227end ) 5228end ) 5229end ) 5230 5231describe ( 'nvim_parse_cmd' , function () 5232it ( 'works' , function () 5233eq ( { 5234cmd ='echo' , 5235args ={ 'foo' } , 5236bang =false , 5237addr ='none' , 5238magic ={ 5239file =false , 5240bar =false , 5241} , 5242nargs ='*' , 5243nextcmd ='' , 5244mods ={ 5245browse =false , 5246confirm =false , 5247emsg_silent =false , 5248filter ={ 5249pattern ='' , 5250force =false , 5251} , 5252hide =false , 5253horizontal =false , 5254keepalt =false , 5255keepjumps =false , 5256keepmarks =false , 5257keeppatterns =false , 5258lockmarks =false , 5259noautocmd =false , 5260noswapfile =false , 5261sandbox =false , 5262silent =false , 5263split ='' , 5264tab =- 1 , 5265unsilent =false , 5266verbose =- 1 , 5267vertical =false , 5268} , 5269} , api . nvim_parse_cmd ( 'echo foo' , {} )) 5270end ) 5271it ( 'works with ranges' , function () 5272eq ( { 5273cmd ='substitute' , 5274args ={ '/math.random/math.max/' } , 5275bang =false , 5276range ={ 4 , 6 } , 5277addr ='line' , 5278magic ={ 5279file =false , 5280bar =false , 5281} , 5282nargs ='*' , 5283nextcmd ='' , 5284mods ={ 5285browse =false , 5286confirm =false , 5287emsg_silent =false , 5288filter ={ 5289pattern ='' , 5290force =false , 5291} , 5292hide =false , 5293horizontal =false , 5294keepalt =false , 5295keepjumps =false , 5296keepmarks =false , 5297keeppatterns =false , 5298lockmarks =false , 5299noautocmd =false , 5300noswapfile =false , 5301sandbox =false , 5302silent =false , 5303split ='' , 5304tab =- 1 , 5305unsilent =false , 5306verbose =- 1 , 5307vertical =false , 5308} , 5309} , api . nvim_parse_cmd ( '4,6s/math.random/math.max/' , {} )) 5310end ) 5311it ( 'works with count' , function () 5312eq ( { 5313cmd ='buffer' , 5314args ={} , 5315bang =false , 5316range ={ 1 } , 5317count =1 , 5318addr ='buf' , 5319magic ={ 5320file =false , 5321bar =true , 5322} , 5323nargs ='*' , 5324nextcmd ='' , 5325mods ={ 5326browse =false , 5327confirm =false , 5328emsg_silent =false , 5329filter ={ 5330pattern ='' , 5331force =false , 5332} , 5333hide =false , 5334horizontal =false , 5335keepalt =false , 5336keepjumps =false , 5337keepmarks =false , 5338keeppatterns =false , 5339lockmarks =false , 5340noautocmd =false , 5341noswapfile =false , 5342sandbox =false , 5343silent =false , 5344split ='' , 5345tab =- 1 , 5346unsilent =false , 5347verbose =- 1 , 5348vertical =false , 5349} , 5350} , api . nvim_parse_cmd ( 'buffer 1' , {} )) 5351end ) 5352it ( 'works with register' , function () 5353eq ( { 5354cmd ='put' , 5355args ={} , 5356bang =false , 5357reg ='+' , 5358addr ='line' , 5359magic ={ 5360file =false , 5361bar =true , 5362} , 5363nargs ='0' , 5364nextcmd ='' , 5365mods ={ 5366browse =false , 5367confirm =false , 5368emsg_silent =false , 5369filter ={ 5370pattern ='' , 5371force =false , 5372} , 5373hide =false , 5374horizontal =false , 5375keepalt =false , 5376keepjumps =false , 5377keepmarks =false , 5378keeppatterns =false , 5379lockmarks =false , 5380noautocmd =false , 5381noswapfile =false , 5382sandbox =false , 5383silent =false , 5384split ='' , 5385tab =- 1 , 5386unsilent =false , 5387verbose =- 1 , 5388vertical =false , 5389} , 5390} , api . nvim_parse_cmd ( 'put +' , {} )) 5391eq ( { 5392cmd ='put' , 5393args ={} , 5394bang =false , 5395reg ='' , 5396addr ='line' , 5397magic ={ 5398file =false , 5399bar =true , 5400} , 5401nargs ='0' , 5402nextcmd ='' , 5403mods ={ 5404browse =false , 5405confirm =false , 5406emsg_silent =false , 5407filter ={ 5408pattern ='' , 5409force =false , 5410} , 5411hide =false , 5412horizontal =false , 5413keepalt =false , 5414keepjumps =false , 5415keepmarks =false , 5416keeppatterns =false , 5417lockmarks =false , 5418noautocmd =false , 5419noswapfile =false , 5420sandbox =false , 5421silent =false , 5422split ='' , 5423tab =- 1 , 5424unsilent =false , 5425verbose =- 1 , 5426vertical =false , 5427} , 5428} , api . nvim_parse_cmd ( 'put' , {} )) 5429end ) 5430it ( 'works with range, count and register' , function () 5431eq ( { 5432cmd ='delete' , 5433args ={} , 5434bang =false , 5435range ={ 3 , 7 } , 5436count =7 , 5437reg ='*' , 5438addr ='line' , 5439magic ={ 5440file =false , 5441bar =true , 5442} , 5443nargs ='0' , 5444nextcmd ='' , 5445mods ={ 5446browse =false , 5447confirm =false , 5448emsg_silent =false , 5449filter ={ 5450pattern ='' , 5451force =false , 5452} , 5453hide =false , 5454horizontal =false , 5455keepalt =false , 5456keepjumps =false , 5457keepmarks =false , 5458keeppatterns =false , 5459lockmarks =false , 5460noautocmd =false , 5461noswapfile =false , 5462sandbox =false , 5463silent =false , 5464split ='' , 5465tab =- 1 , 5466unsilent =false , 5467verbose =- 1 , 5468vertical =false , 5469} , 5470} , api . nvim_parse_cmd ( '1,3delete * 5' , {} )) 5471end ) 5472it ( 'works with bang' , function () 5473eq ( { 5474cmd ='write' , 5475args ={} , 5476bang =true , 5477addr ='line' , 5478magic ={ 5479file =true , 5480bar =true , 5481} , 5482nargs ='?' , 5483nextcmd ='' , 5484mods ={ 5485browse =false , 5486confirm =false , 5487emsg_silent =false , 5488filter ={ 5489pattern ='' , 5490force =false , 5491} , 5492hide =false , 5493horizontal =false , 5494keepalt =false , 5495keepjumps =false , 5496keepmarks =false , 5497keeppatterns =false , 5498lockmarks =false , 5499noautocmd =false , 5500noswapfile =false , 5501sandbox =false , 5502silent =false , 5503split ='' , 5504tab =- 1 , 5505unsilent =false , 5506verbose =- 1 , 5507vertical =false , 5508} , 5509} , api . nvim_parse_cmd ( 'w!' , {} )) 5510end ) 5511it ( 'works with modifiers' , function () 5512eq ( 5513{ 5514cmd ='split' , 5515args ={ 'foo.txt' } , 5516bang =false , 5517addr ='?' , 5518magic ={ 5519file =true , 5520bar =true , 5521} , 5522nargs ='?' , 5523nextcmd ='' , 5524mods ={ 5525browse =false , 5526confirm =false , 5527emsg_silent =true , 5528filter ={ 5529pattern ='foo' , 5530force =false , 5531} , 5532hide =false , 5533horizontal =true , 5534keepalt =false , 5535keepjumps =false , 5536keepmarks =false , 5537keeppatterns =false , 5538lockmarks =false , 5539noautocmd =false , 5540noswapfile =false , 5541sandbox =false , 5542silent =true , 5543split ='topleft' , 5544tab =1 , 5545unsilent =false , 5546verbose =15 , 5547vertical =false , 5548} , 5549} , 5550api . nvim_parse_cmd ( 5551'15verbose silent! horizontal topleft tab filter /foo/ split foo.txt' , 5552{} 5553) 5554) 5555eq ( 5556{ 5557cmd ='split' , 5558args ={ 'foo.txt' } , 5559bang =false , 5560addr ='?' , 5561magic ={ 5562file =true , 5563bar =true , 5564} , 5565nargs ='?' , 5566nextcmd ='' , 5567mods ={ 5568browse =false , 5569confirm =true , 5570emsg_silent =false , 5571filter ={ 5572pattern ='foo' , 5573force =true , 5574} , 5575hide =false , 5576horizontal =false , 5577keepalt =false , 5578keepjumps =false , 5579keepmarks =false , 5580keeppatterns =false , 5581lockmarks =false , 5582noautocmd =false , 5583noswapfile =false , 5584sandbox =false , 5585silent =false , 5586split ='botright' , 5587tab =0 , 5588unsilent =true , 5589verbose =0 , 5590vertical =false , 5591} , 5592} , 5593api . nvim_parse_cmd ( 5594'0verbose unsilent botright 0tab confirm filter! /foo/ split foo.txt' , 5595{} 5596) 5597) 5598end ) 5599it ( 'works with user commands' , function () 5600command ( 'command -bang -nargs=+ -range -addr=lines MyCommand echo foo' ) 5601eq ( { 5602cmd ='MyCommand' , 5603args ={ 'test' , 'it' } , 5604bang =true , 5605range ={ 4 , 6 } , 5606addr ='line' , 5607magic ={ 5608file =false , 5609bar =false , 5610} , 5611nargs ='+' , 5612nextcmd ='' , 5613mods ={ 5614browse =false , 5615confirm =false , 5616emsg_silent =false , 5617filter ={ 5618pattern ='' , 5619force =false , 5620} , 5621hide =false , 5622horizontal =false , 5623keepalt =false , 5624keepjumps =false , 5625keepmarks =false , 5626keeppatterns =false , 5627lockmarks =false , 5628noautocmd =false , 5629noswapfile =false , 5630sandbox =false , 5631silent =false , 5632split ='' , 5633tab =- 1 , 5634unsilent =false , 5635verbose =- 1 , 5636vertical =false , 5637} , 5638} , api . nvim_parse_cmd ( '4,6MyCommand! test it' , {} )) 5639end ) 5640it ( 'sets nextcmd for bar-separated commands' , function () 5641eq ( { 5642cmd ='argadd' , 5643args ={ 'a.txt' } , 5644bang =false , 5645addr ='arg' , 5646magic ={ 5647file =true , 5648bar =true , 5649} , 5650nargs ='*' , 5651nextcmd ='argadd b.txt' , 5652mods ={ 5653browse =false , 5654confirm =false , 5655emsg_silent =false , 5656filter ={ 5657pattern ='' , 5658force =false , 5659} , 5660hide =false , 5661horizontal =false , 5662keepalt =false , 5663keepjumps =false , 5664keepmarks =false , 5665keeppatterns =false , 5666lockmarks =false , 5667noautocmd =false , 5668noswapfile =false , 5669sandbox =false , 5670silent =false , 5671split ='' , 5672tab =- 1 , 5673unsilent =false , 5674verbose =- 1 , 5675vertical =false , 5676} , 5677} , api . nvim_parse_cmd ( 'argadd a.txt | argadd b.txt' , {} )) 5678end ) 5679it ( 'sets nextcmd after expr-arg commands #36029' , function () 5680local result =api . nvim_parse_cmd ( 'exe "ls"|edit foo' , {} ) 5681eq ( { '"ls"' } , result . args ) 5682eq ( 'execute' , result . cmd ) 5683eq ( 'edit foo' , result . nextcmd ) 5684end ) 5685it ( 'parses expr-arg commands with invalid expr #38689' , function () 5686for _ , arg in ipairs ( { '&' , '[' , '{' , '"' , "'" } ) do 5687local result =api . nvim_parse_cmd ( 'echo ' .. arg , {} ) 5688eq ( { arg } , result . args ) 5689eq ( 'echo' , result . cmd ) 5690eq ( '' , result . nextcmd ) 5691end 5692-- v:errmsg shouldn't be set 5693eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 5694end ) 5695it ( 'parses :map commands with space in RHS' , function () 5696eq ( { 5697addr ='none' , 5698args ={ 'a' , 'b c' } , 5699bang =false , 5700cmd ='map' , 5701magic ={ 5702bar =true , 5703file =false , 5704} , 5705mods ={ 5706browse =false , 5707confirm =false , 5708emsg_silent =false , 5709filter ={ 5710force =false , 5711pattern ='' , 5712} , 5713hide =false , 5714horizontal =false , 5715keepalt =false , 5716keepjumps =false , 5717keepmarks =false , 5718keeppatterns =false , 5719lockmarks =false , 5720noautocmd =false , 5721noswapfile =false , 5722sandbox =false , 5723silent =false , 5724split ='' , 5725tab =- 1 , 5726unsilent =false , 5727verbose =- 1 , 5728vertical =false , 5729} , 5730nargs ='*' , 5731nextcmd ='' , 5732} , api . nvim_parse_cmd ( 'map a b c' , {} )) 5733end ) 5734it ( 'works for nargs=1' , function () 5735command ( 'command -nargs=1 MyCommand echo <q-args>' ) 5736eq ( { 5737cmd ='MyCommand' , 5738args ={ 'test it' } , 5739bang =false , 5740addr ='none' , 5741magic ={ 5742file =false , 5743bar =false , 5744} , 5745nargs ='1' , 5746nextcmd ='' , 5747mods ={ 5748browse =false , 5749confirm =false , 5750emsg_silent =false , 5751filter ={ 5752pattern ='' , 5753force =false , 5754} , 5755hide =false , 5756horizontal =false , 5757keepalt =false , 5758keepjumps =false , 5759keepmarks =false , 5760keeppatterns =false , 5761lockmarks =false , 5762noautocmd =false , 5763noswapfile =false , 5764sandbox =false , 5765silent =false , 5766split ='' , 5767tab =- 1 , 5768unsilent =false , 5769verbose =- 1 , 5770vertical =false , 5771} , 5772} , api . nvim_parse_cmd ( 'MyCommand test it' , {} )) 5773end ) 5774it ( 'validates command' , function () 5775eq ( 'Parsing command-line' , pcall_err ( api . nvim_parse_cmd , '' , {} )) 5776eq ( 'Parsing command-line' , pcall_err ( api . nvim_parse_cmd , '" foo' , {} )) 5777eq ( 5778'Parsing command-line: E492: Not an editor command: Fubar' , 5779pcall_err ( api . nvim_parse_cmd , 'Fubar' , {} ) 5780) 5781command ( 'command! Fubar echo foo' ) 5782eq ( 'Parsing command-line: E477: No ! allowed' , pcall_err ( api . nvim_parse_cmd , 'Fubar!' , {} )) 5783eq ( 5784'Parsing command-line: E481: No range allowed' , 5785pcall_err ( api . nvim_parse_cmd , '4,6Fubar' , {} ) 5786) 5787command ( 'command! Foobar echo foo' ) 5788eq ( 5789'Parsing command-line: E464: Ambiguous use of user-defined command' , 5790pcall_err ( api . nvim_parse_cmd , 'F' , {} ) 5791) 5792end ) 5793it ( 'does not interfere with printing line in Ex mode #19400' , function () 5794local screen =Screen . new ( 60 , 12 ) 5795insert ( [[ 5796foo 5797bar]] ) 5798feed ( '1q:1' ) 5799screen : expect ( { any =vim . pesc ( '{1::}1^' ) } ) 5800eq ( 'Parsing command-line' , pcall_err ( api . nvim_parse_cmd , '' , {} )) 5801-- Executing the line still auto-prints it. 5802feed ( '<CR>' ) 5803screen : expect ( { any =vim . pesc ( '" foo' ) } ) 5804end ) 5805it ( 'does not move cursor or change search history/pattern #19878 #19890' , function () 5806api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo' , 'bar' , 'foo' , 'bar' } ) 5807eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 5808eq ( '' , fn . getreg ( '/' )) 5809eq ( '' , fn . histget ( 'search' )) 5810feed ( ':' ) -- call the API in cmdline mode to test whether it changes search history 5811eq ( { 5812cmd ='normal' , 5813args ={ 'x' } , 5814bang =true , 5815range ={ 3 , 4 } , 5816addr ='line' , 5817magic ={ 5818file =false , 5819bar =false , 5820} , 5821nargs ='+' , 5822nextcmd ='' , 5823mods ={ 5824browse =false , 5825confirm =false , 5826emsg_silent =false , 5827filter ={ 5828pattern ='' , 5829force =false , 5830} , 5831hide =false , 5832horizontal =false , 5833keepalt =false , 5834keepjumps =false , 5835keepmarks =false , 5836keeppatterns =false , 5837lockmarks =false , 5838noautocmd =false , 5839noswapfile =false , 5840sandbox =false , 5841silent =false , 5842split ='' , 5843tab =- 1 , 5844unsilent =false , 5845verbose =- 1 , 5846vertical =false , 5847} , 5848} , api . nvim_parse_cmd ( '+2;/bar/normal! x' , {} )) 5849eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 5850eq ( '' , fn . getreg ( '/' )) 5851eq ( '' , fn . histget ( 'search' )) 5852end ) 5853it ( 'result can be used directly by nvim_cmd #20051' , function () 5854eq ( 'foo' , api . nvim_cmd ( api . nvim_parse_cmd ( 'echo "foo"' , {} ), { output =true } )) 5855api . nvim_cmd ( api . nvim_parse_cmd ( 'set cursorline' , {} ), {} ) 5856eq ( true , api . nvim_get_option_value ( 'cursorline' , {} )) 5857-- Roundtrip on :bdelete which does not accept "range". #33394 5858api . nvim_cmd ( api . nvim_parse_cmd ( 'bdelete' , {} ), {} ) 5859end ) 5860it ( 'no side-effects (error messages) in pcall() #20339' , function () 5861eq ( 5862{ false , 'Parsing command-line: E16: Invalid range' } , 5863exec_lua ( [=[return {pcall(vim.api.nvim_parse_cmd, "'<,'>n", {})}]=] ) 5864) 5865eq ( '' , eval ( 'v:errmsg' )) 5866end ) 5867it ( 'does not include count field when no count provided for builtin commands' , function () 5868local result =api . nvim_parse_cmd ( 'copen' , {} ) 5869eq ( nil , result . count ) 5870api . nvim_cmd ( result , {} ) 5871eq ( 10 , api . nvim_win_get_height ( 0 )) 5872result =api . nvim_parse_cmd ( 'copen 5' , {} ) 5873eq ( 5 , result . count ) 5874end ) 5875it ( 'parses range-only cmdline (:1)' , function () 5876insert [[ 5877line1 5878line2 5879line3 5880line4 5881]] 5882api . nvim_win_set_cursor ( 0 , { 4 , 4 } ) 5883local res =api . nvim_parse_cmd ( '1' , {} ) 5884eq ( { 5885addr ='line' , 5886args ={} , 5887bang =false , 5888cmd ='' , 5889magic ={ 5890bar =false , 5891file =false , 5892} , 5893mods ={ 5894browse =false , 5895confirm =false , 5896emsg_silent =false , 5897filter ={ 5898force =false , 5899pattern ='' , 5900} , 5901hide =false , 5902horizontal =false , 5903keepalt =false , 5904keepjumps =false , 5905keepmarks =false , 5906keeppatterns =false , 5907lockmarks =false , 5908noautocmd =false , 5909noswapfile =false , 5910sandbox =false , 5911silent =false , 5912split ='' , 5913tab =- 1 , 5914unsilent =false , 5915verbose =- 1 , 5916vertical =false , 5917} , 5918nargs ='0' , 5919nextcmd ='' , 5920range ={ 1 } , 5921} , res ) 5922api . nvim_cmd ( res , {} ) 5923eq ( 1 , api . nvim_win_get_cursor ( 0 )[ 1 ]) 5924feed ( 'VG:' ) 5925n . poke_eventloop () 5926res =api . nvim_parse_cmd ( "'<,'>" , {} ) 5927eq ( { 1 , 5 } , res . range ) 5928end ) 5929it ( 'parses modifier-only cmdline (:aboveleft)' , function () 5930local res =api . nvim_parse_cmd ( 'aboveleft' , {} ) 5931eq ( '' , res . cmd ) 5932eq ( 'aboveleft' , res . mods . split ) 5933eq ( 'none' , res . addr ) 5934end ) 5935end ) 5936 5937describe ( 'nvim_cmd' , function () 5938it ( 'works' , function () 5939api . nvim_cmd ( { cmd ='set' , args ={ 'cursorline' } } , {} ) 5940eq ( true , api . nvim_get_option_value ( 'cursorline' , {} )) 5941end ) 5942 5943it ( 'validation' , function () 5944eq ( "Invalid 'cmd': expected non-empty String" , pcall_err ( api . nvim_cmd , { cmd ='' } , {} )) 5945eq ( "Invalid 'cmd': expected String, got Array" , pcall_err ( api . nvim_cmd , { cmd ={} } , {} )) 5946eq ( 5947"Invalid 'args': expected Array, got Boolean" , 5948pcall_err ( api . nvim_cmd , { cmd ='set' , args =true } , {} ) 5949) 5950eq ( 5951'Invalid command arg: expected non-whitespace' , 5952pcall_err ( api . nvim_cmd , { cmd ='set' , args ={ ' ' } } , {} ) 5953) 5954eq ( 5955'Invalid command arg: expected valid type, got Array' , 5956pcall_err ( api . nvim_cmd , { cmd ='set' , args ={ {} } } , {} ) 5957) 5958eq ( 'Wrong number of arguments' , pcall_err ( api . nvim_cmd , { cmd ='aboveleft' , args ={} } , {} )) 5959eq ( 5960'Command cannot accept bang: print' , 5961pcall_err ( api . nvim_cmd , { cmd ='print' , args ={} , bang =true } , {} ) 5962) 5963 5964eq ( 5965'Command cannot accept range: set' , 5966pcall_err ( api . nvim_cmd , { cmd ='set' , args ={} , range ={ 1 } } , {} ) 5967) 5968eq ( 5969"Invalid 'range': expected Array, got Boolean" , 5970pcall_err ( api . nvim_cmd , { cmd ='print' , args ={} , range =true } , {} ) 5971) 5972eq ( 5973"Invalid 'range': expected <=2 elements" , 5974pcall_err ( api . nvim_cmd , { cmd ='print' , args ={} , range ={ 1 , 2 , 3 , 4 } } , {} ) 5975) 5976eq ( 5977'Invalid range element: expected non-negative Integer' , 5978pcall_err ( api . nvim_cmd , { cmd ='print' , args ={} , range ={ - 1 } } , {} ) 5979) 5980eq ( 5981"Invalid 'range'" , 5982pcall_err ( api . nvim_cmd , { cmd ='print' , args ={} , range ={ 99 } } , {} ) 5983) 5984-- Not ":1x" (:xit). 5985eq ( 5986'Wrong number of arguments' , 5987pcall_err ( api . nvim_cmd , { cmd ='' , range ={ 1 } , args ={ 'x' } } , {} ) 5988) 5989 5990eq ( 5991'Command cannot accept count: set' , 5992pcall_err ( api . nvim_cmd , { cmd ='set' , args ={} , count =1 } , {} ) 5993) 5994eq ( 5995"Invalid 'count': expected Integer, got Boolean" , 5996pcall_err ( api . nvim_cmd , { cmd ='print' , args ={} , count =true } , {} ) 5997) 5998eq ( 5999"Invalid 'count': expected non-negative Integer" , 6000pcall_err ( api . nvim_cmd , { cmd ='print' , args ={} , count =- 1 } , {} ) 6001) 6002 6003eq ( 6004'Command cannot accept register: set' , 6005pcall_err ( api . nvim_cmd , { cmd ='set' , args ={} , reg ='x' } , {} ) 6006) 6007eq ( 6008'Cannot use register "=' , 6009pcall_err ( api . nvim_cmd , { cmd ='put' , args ={} , reg ='=' } , {} ) 6010) 6011eq ( 6012"Invalid 'reg': expected single character, got xx" , 6013pcall_err ( api . nvim_cmd , { cmd ='put' , args ={} , reg ='xx' } , {} ) 6014) 6015 6016-- #20681 6017eq ( 'Invalid command: "win_getid"' , pcall_err ( api . nvim_cmd , { cmd ='win_getid' } , {} )) 6018eq ( 'Invalid command: "echo "hi""' , pcall_err ( api . nvim_cmd , { cmd ='echo "hi"' } , {} )) 6019matches ( 'Invalid command: "win_getid"$' , pcall_err ( exec_lua , [[return vim.cmd.win_getid{}]] )) 6020 6021-- Lua call allows empty {} for dict item. 6022eq ( '' , exec_lua ( [[return vim.cmd{ cmd = "set", args = {}, magic = {} }]] )) 6023eq ( '' , exec_lua ( [[return vim.cmd{ cmd = "set", args = {}, mods = {} }]] )) 6024eq ( '' , api . nvim_cmd ( { cmd ='set' , args ={} , magic ={} } , {} )) 6025 6026-- Lua call does not allow non-empty list-like {} for dict item. 6027matches ( 6028"Invalid 'magic': Expected Dict%-like Lua table$" , 6029pcall_err ( exec_lua , [[return vim.cmd{ cmd = "set", args = {}, magic = { 'a' } }]] ) 6030) 6031matches ( 6032"Invalid key: 'bogus'$" , 6033pcall_err ( exec_lua , [[return vim.cmd{ cmd = "set", args = {}, magic = { bogus = true } }]] ) 6034) 6035matches ( 6036"Invalid key: 'bogus'$" , 6037pcall_err ( exec_lua , [[return vim.cmd{ cmd = "set", args = {}, mods = { bogus = true } }]] ) 6038) 6039end ) 6040 6041it ( 'captures output' , function () 6042eq ( 'foo' , api . nvim_cmd ( { cmd ='echo' , args ={ '"foo"' } } , { output =true } )) 6043-- Returns output in cmdline mode #35321 6044feed ( ':' ) 6045eq ( 'foo' , api . nvim_cmd ( { cmd ='echo' , args ={ '"foo"' } } , { output =true } )) 6046end ) 6047 6048it ( 'sets correct script context' , function () 6049api . nvim_cmd ( { cmd ='set' , args ={ 'cursorline' } } , {} ) 6050local str =exec_capture ( [[verbose set cursorline?]] ) 6051neq ( nil , str : find ( 'cursorline\n\tLast set from API client %(channel id %d+%)' )) 6052end ) 6053 6054it ( 'works with range' , function () 6055insert [[ 6056line1 6057line2 6058line3 6059line4 6060you didn't expect this 6061line5 6062line6 6063]] 6064api . nvim_cmd ( { cmd ='del' , range ={ 2 , 4 } } , {} ) 6065expect [[ 6066line1 6067you didn't expect this 6068line5 6069line6 6070]] 6071end ) 6072 6073it ( 'uses the same default range as Ex' , function () 6074api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'a' , 'b' , 'c' } ) 6075api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 6076command ( 'command -range -addr=other Other let g:r = [<line1>, <line2>]' ) 6077command ( 'command -range=% -addr=other OtherAll let g:r = [<line1>, <line2>]' ) 6078for _ , name in ipairs ( { 'Other' , 'OtherAll' } ) do 6079command ( name ) 6080local ex =api . nvim_get_var ( 'r' ) 6081api . nvim_cmd ( { cmd =name } , {} ) 6082eq ( ex , api . nvim_get_var ( 'r' )) 6083end 6084end ) 6085 6086it ( 'works with count' , function () 6087insert [[ 6088line1 6089line2 6090line3 6091line4 6092you didn't expect this 6093line5 6094line6 6095]] 6096api . nvim_cmd ( { cmd ='del' , range ={ 2 } , count =4 } , {} ) 6097expect [[ 6098line1 6099line5 6100line6 6101]] 6102end ) 6103 6104it ( 'works with register' , function () 6105insert [[ 6106line1 6107line2 6108line3 6109line4 6110you didn't expect this 6111line5 6112line6 6113]] 6114api . nvim_cmd ( { cmd ='del' , range ={ 2 , 4 } , reg ='a' } , {} ) 6115command ( '1put a' ) 6116expect [[ 6117line1 6118line2 6119line3 6120line4 6121you didn't expect this 6122line5 6123line6 6124]] 6125end ) 6126 6127it ( 'works with bang' , function () 6128api . nvim_create_user_command ( 'Foo' , 'echo "<bang>"' , { bang =true } ) 6129eq ( '!' , api . nvim_cmd ( { cmd ='Foo' , bang =true } , { output =true } )) 6130eq ( '' , api . nvim_cmd ( { cmd ='Foo' , bang =false } , { output =true } )) 6131end ) 6132 6133it ( 'works with modifiers' , function () 6134-- with silent = true output is still captured 6135eq ( 6136'1' , 6137api . nvim_cmd ( 6138{ cmd ='echomsg' , args ={ '1' } , mods ={ silent =true } } , 6139{ output =true } 6140) 6141) 6142-- but message isn't added to message history 6143eq ( '' , api . nvim_cmd ( { cmd ='messages' } , { output =true } )) 6144 6145api . nvim_create_user_command ( 'Foo' , 'set verbose' , {} ) 6146eq ( ' verbose=1' , api . nvim_cmd ( { cmd ='Foo' , mods ={ verbose =1 } } , { output =true } )) 6147 6148api . nvim_create_user_command ( 'Mods' , "echo '<mods>'" , {} ) 6149eq ( 6150'keepmarks keeppatterns silent 3verbose aboveleft horizontal' , 6151api . nvim_cmd ( { 6152cmd ='Mods' , 6153mods ={ 6154horizontal =true , 6155keepmarks =true , 6156keeppatterns =true , 6157silent =true , 6158split ='aboveleft' , 6159verbose =3 , 6160} , 6161} , { output =true } ) 6162) 6163eq ( 0 , api . nvim_get_option_value ( 'verbose' , {} )) 6164 6165command ( 'edit foo.txt | edit bar.txt' ) 6166eq ( 6167' 1 #h "foo.txt" line 1' , 6168api . nvim_cmd ( 6169{ cmd ='buffers' , mods ={ filter ={ pattern ='foo' , force =false } } } , 6170{ output =true } 6171) 6172) 6173eq ( 6174' 2 %a "bar.txt" line 1' , 6175api . nvim_cmd ( 6176{ cmd ='buffers' , mods ={ filter ={ pattern ='foo' , force =true } } } , 6177{ output =true } 6178) 6179) 6180 6181-- with emsg_silent = true error is suppressed 6182feed ( [[:lua vim.api.nvim_cmd({ cmd = 'call', mods = { emsg_silent = true } }, {})<CR>]] ) 6183eq ( '' , api . nvim_cmd ( { cmd ='messages' } , { output =true } )) 6184-- error from the next command typed is not suppressed #21420 6185feed ( ':call<CR><CR>' ) 6186eq ( 'E471: Argument required' , api . nvim_cmd ( { cmd ='messages' } , { output =true } )) 6187 6188-- "modifier-only" command (e.g. :noautocmd). 6189eq ( '' , api . nvim_cmd ( { cmd ='' , mods ={ noautocmd =true } } , {} )) 6190end ) 6191 6192it ( 'works with magic.file' , function () 6193exec_lua ( [[ 6194vim.api.nvim_create_user_command("Foo", function(opts) 6195vim.api.nvim_echo({{ opts.fargs[1] }}, false, {}) 6196end, { nargs = 1 }) 6197]] ) 6198eq ( 6199t . fix_slashes ( assert ( uv . cwd ())), 6200api . nvim_cmd ( 6201{ cmd ='Foo' , args ={ '%:p:h' } , magic ={ file =true } } , 6202{ output =true } 6203) 6204) 6205end ) 6206 6207it ( 'splits arguments correctly' , function () 6208exec ( [[ 6209function! FooFunc(...) 6210echo a:000 6211endfunction 6212]] ) 6213api . nvim_create_user_command ( 'Foo' , 'call FooFunc(<f-args>)' , { nargs ='+' } ) 6214eq ( 6215[=[['a quick', 'brown fox', 'jumps over the', 'lazy dog']]=] , 6216api . nvim_cmd ( 6217{ cmd ='Foo' , args ={ 'a quick' , 'brown fox' , 'jumps over the' , 'lazy dog' } } , 6218{ output =true } 6219) 6220) 6221eq ( 6222[=[['test \ \\ \"""\', 'more\ tests\" ']]=] , 6223api . nvim_cmd ( 6224{ cmd ='Foo' , args ={ [[test \ \\ \"""\]] , [[more\ tests\" ]] } } , 6225{ output =true } 6226) 6227) 6228end ) 6229 6230it ( 'splits arguments correctly for Lua callback' , function () 6231api . nvim_exec_lua ( 6232[[ 6233local function FooFunc(opts) 6234vim.print(opts.fargs) 6235end 6236 6237vim.api.nvim_create_user_command("Foo", FooFunc, { nargs = '+', bar = true }) 6238]] , 6239{} 6240) 6241eq ( 6242[[{ "a quick", "brown fox", "jumps over the", "lazy dog" }]] , 6243api . nvim_cmd ( 6244{ cmd ='Foo' , args ={ 'a quick' , 'brown fox' , 'jumps over the' , 'lazy dog' } } , 6245{ output =true } 6246) 6247) 6248eq ( 6249[[{ 'test \\ \\\\ \\"""\\', 'more\\ tests\\" ' }]] , 6250api . nvim_cmd ( 6251{ cmd ='Foo' , args ={ [[test \ \\ \"""\]] , [[more\ tests\" ]] } } , 6252{ output =true } 6253) 6254) 6255eq ( [[{ " a|b" }]] , api . nvim_cmd ( { cmd ='Foo' , args ={ ' a|b' } } , { output =true } )) 6256end ) 6257 6258it ( 'keeps leading white space of the first argument' , function () 6259api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'ab' } ) 6260api . nvim_cmd ( { cmd ='normal' , args ={ ' x' } } , {} ) 6261eq ( { 'a' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 6262end ) 6263 6264it ( 'works with buffer names' , function () 6265command ( 'edit foo.txt | edit bar.txt' ) 6266api . nvim_cmd ( { cmd ='buffer' , args ={ 'foo.txt' } } , {} ) 6267eq ( 'foo.txt' , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 6268api . nvim_cmd ( { cmd ='buffer' , args ={ 'bar.txt' } } , {} ) 6269eq ( 'bar.txt' , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 6270end ) 6271 6272it ( 'triggers CmdUndefined event if command is not found' , function () 6273api . nvim_exec_lua ( 6274[[ 6275vim.api.nvim_create_autocmd("CmdUndefined", 6276{ pattern = "Foo", 6277callback = function() 6278vim.api.nvim_create_user_command("Foo", "echo 'foo'", {}) 6279end 6280}) 6281]] , 6282{} 6283) 6284eq ( 'foo' , api . nvim_cmd ( { cmd ='Foo' } , { output =true } )) 6285end ) 6286 6287it ( 'errors if command is not implemented' , function () 6288eq ( 'Command not implemented: winpos' , pcall_err ( api . nvim_cmd , { cmd ='winpos' } , {} )) 6289end ) 6290 6291it ( 'works with empty arguments list' , function () 6292api . nvim_cmd ( { cmd ='update' } , {} ) 6293api . nvim_cmd ( { cmd ='buffer' , count =0 } , {} ) 6294end ) 6295 6296it ( "doesn't suppress errors when used in keymapping" , function () 6297api . nvim_exec_lua ( 6298[[ 6299vim.keymap.set("n", "[l", 6300function() vim.api.nvim_cmd({ cmd = "echo", args = {"foo"} }, {}) end) 6301]] , 6302{} 6303) 6304feed ( '[l' ) 6305neq ( nil , string . find ( eval ( 'v:errmsg' ), 'E5108:' )) 6306end ) 6307 6308it ( 'handles 0 range #19608' , function () 6309api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'aa' } ) 6310api . nvim_cmd ( { cmd ='delete' , range ={ 0 } } , {} ) 6311command ( 'undo' ) 6312eq ( { 'aa' } , api . nvim_buf_get_lines ( 0 , 0 , 1 , false )) 6313assert_alive () 6314end ) 6315 6316it ( 'supports filename expansion' , function () 6317api . nvim_cmd ( { cmd ='argadd' , args ={ '%:p:h:t' , '%:p:h:t' } } , {} ) 6318local arg =fn . expand ( '%:p:h:t' ) 6319eq ( { arg , arg } , fn . argv ()) 6320end ) 6321 6322it ( ":make command works when argument count isn't 1 #19696" , function () 6323command ( 'set makeprg=echo' ) 6324command ( 'set shellquote=' ) 6325matches ( '^:!echo ' , api . nvim_cmd ( { cmd ='make' } , { output =true } )) 6326assert_alive () 6327matches ( 6328'^:!echo foo bar' , 6329api . nvim_cmd ( { cmd ='make' , args ={ 'foo' , 'bar' } } , { output =true } ) 6330) 6331assert_alive () 6332local arg_pesc =pesc ( fn . expand ( '%:p:h:t' )) 6333matches ( 6334( '^:!echo %s %s' ): format ( arg_pesc , arg_pesc ), 6335api . nvim_cmd ( { cmd ='make' , args ={ '%:p:h:t' , '%:p:h:t' } } , { output =true } ) 6336) 6337assert_alive () 6338end ) 6339 6340it ( "doesn't display messages when output=true" , function () 6341local screen =Screen . new ( 40 , 6 ) 6342api . nvim_cmd ( { cmd ='echo' , args ={ [['hello']] } } , { output =true } ) 6343screen : expect { 6344grid =[[ 6345^ | 6346{1:~ }|*4 6347| 6348]] , 6349} 6350exec ( [[ 6351func Print() 6352call nvim_cmd(#{cmd: 'echo', args: ['"hello"']}, #{output: v:true}) 6353endfunc 6354]] ) 6355feed ( [[:echon 1 | call Print() | echon 5<CR>]] ) 6356screen : expect { 6357grid =[[ 6358^ | 6359{1:~ }|*4 636015 | 6361]] , 6362} 6363end ) 6364 6365it ( 'works with non-String args' , function () 6366eq ( '2' , api . nvim_cmd ( { cmd ='echo' , args ={ 2 } } , { output =true } )) 6367eq ( '1' , api . nvim_cmd ( { cmd ='echo' , args ={ true } } , { output =true } )) 6368end ) 6369 6370describe ( 'first argument as count' , function () 6371it ( 'works' , function () 6372command ( 'vsplit | enew' ) 6373api . nvim_cmd ( { cmd ='bdelete' , args ={ api . nvim_get_current_buf () } } , {} ) 6374eq ( 1 , api . nvim_get_current_buf ()) 6375end ) 6376 6377it ( 'works with :sleep using milliseconds' , function () 6378local start =uv . now () 6379api . nvim_cmd ( { cmd ='sleep' , args ={ '100m' } } , {} ) 6380ok ( uv . now () - start <= 300 ) 6381end ) 6382end ) 6383 6384it ( ':call with unknown function does not crash #26289' , function () 6385eq ( 6386'Vim:E117: Unknown function: UnknownFunc' , 6387pcall_err ( api . nvim_cmd , { cmd ='call' , args ={ 'UnknownFunc()' } } , {} ) 6388) 6389end ) 6390 6391it ( ':throw does not crash #24556' , function () 6392eq ( '42' , pcall_err ( api . nvim_cmd , { cmd ='throw' , args ={ '42' } } , {} )) 6393end ) 6394 6395it ( 'can use :return #24556' , function () 6396exec ( [[ 6397func Foo() 6398let g:pos = 'before' 6399call nvim_cmd({'cmd': 'return', 'args': ['[1, 2, 3]']}, {}) 6400let g:pos = 'after' 6401endfunc 6402let g:result = Foo() 6403]] ) 6404eq ( 'before' , api . nvim_get_var ( 'pos' )) 6405eq ( { 1 , 2 , 3 } , api . nvim_get_var ( 'result' )) 6406end ) 6407 6408it ( 'errors properly when command too recursive #27210' , function () 6409exec_lua ( [[ 6410_G.success = false 6411vim.api.nvim_create_user_command('Test', function() 6412vim.api.nvim_cmd({ cmd = 'Test' }, {}) 6413_G.success = true 6414end, {}) 6415]] ) 6416pcall_err ( command , 'Test' ) 6417assert_alive () 6418eq ( false , exec_lua ( 'return _G.success' )) 6419end ) 6420 6421it ( 'handles +flags correctly' , function () 6422-- Write a file for testing +flags 6423t . write_file ( 'testfile' , 'Line 1\nLine 2\nLine 3' , false , false ) 6424 6425-- Test + command (go to the last line) 6426local result =exec_lua ( [[ 6427local parsed = vim.api.nvim_parse_cmd('edit + testfile', {}) 6428vim.cmd(parsed) 6429return { vim.fn.line('.'), parsed.args, parsed.cmd } 6430]] ) 6431eq ( { 3 , { '+ testfile' } , 'edit' } , result ) 6432 6433-- Test +{num} command (go to line number) 6434result =exec_lua ( [[ 6435vim.cmd(vim.api.nvim_parse_cmd('edit +1 testfile', {})) 6436return vim.fn.line('.') 6437]] ) 6438eq ( 1 , result ) 6439 6440-- Test +/{pattern} command (go to line with pattern) 6441result =exec_lua ( [[ 6442local parsed = vim.api.nvim_parse_cmd('edit +/Line\\ 2 testfile', {}) 6443vim.cmd(parsed) 6444return {vim.fn.line('.'), parsed.args} 6445]] ) 6446eq ( { 2 , { '+/Line\\ 2 testfile' } } , result ) 6447 6448-- Test +{command} command (execute a command after opening the file) 6449result =exec_lua ( [[ 6450vim.cmd(vim.api.nvim_parse_cmd('edit +set\\ nomodifiable testfile', {})) 6451return vim.bo.modifiable 6452]] ) 6453eq ( false , result ) 6454 6455-- Test ++ flags structure in parsed command 6456result =exec_lua ( [[ 6457local parsed = vim.api.nvim_parse_cmd('botright edit + testfile', {}) 6458vim.cmd(parsed) 6459return { vim.fn.line('.'), parsed.cmd, parsed.args, parsed.mods.split } 6460]] ) 6461eq ( { 3 , 'edit' , { '+ testfile' } , 'botright' } , result ) 6462 6463-- Clean up 6464os . remove ( 'testfile' ) 6465end ) 6466 6467it ( 'handles various ++ flags correctly' , function () 6468-- Test ++ff flag 6469local result =exec_lua [[ 6470local parsed = vim.api.nvim_parse_cmd('edit ++ff=mac test_ff_mac.txt', {}) 6471vim.cmd(parsed) 6472return parsed.args 6473]] 6474eq ( { '++ff=mac test_ff_mac.txt' } , result ) 6475eq ( 'mac' , api . nvim_get_option_value ( 'fileformat' , {} )) 6476eq ( 'test_ff_mac.txt' , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 6477 6478exec_lua [[ 6479vim.cmd(vim.api.nvim_parse_cmd('edit ++fileformat=unix test_ff_unix.txt', {})) 6480]] 6481eq ( 'unix' , api . nvim_get_option_value ( 'fileformat' , {} )) 6482eq ( 'test_ff_unix.txt' , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 6483 6484-- Test ++enc flag 6485exec_lua [[ 6486vim.cmd(vim.api.nvim_parse_cmd('edit ++enc=utf-32 test_enc.txt', {})) 6487]] 6488eq ( 'ucs-4' , api . nvim_get_option_value ( 'fileencoding' , {} )) 6489eq ( 'test_enc.txt' , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 6490 6491-- Test ++bin and ++nobin flags 6492exec_lua [[ 6493vim.cmd(vim.api.nvim_parse_cmd('edit ++bin test_bin.txt', {})) 6494]] 6495eq ( true , api . nvim_get_option_value ( 'binary' , {} )) 6496eq ( 'test_bin.txt' , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 6497 6498exec_lua [[ 6499vim.cmd(vim.api.nvim_parse_cmd('edit ++nobin test_nobin.txt', {})) 6500]] 6501eq ( false , api . nvim_get_option_value ( 'binary' , {} )) 6502eq ( 'test_nobin.txt' , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 6503 6504-- Test multiple flags together 6505exec_lua [[ 6506vim.cmd(vim.api.nvim_parse_cmd('edit ++ff=mac ++enc=utf-32 ++bin test_multi.txt', {})) 6507]] 6508eq ( true , api . nvim_get_option_value ( 'binary' , {} )) 6509eq ( 'mac' , api . nvim_get_option_value ( 'fileformat' , {} )) 6510eq ( 'ucs-4' , api . nvim_get_option_value ( 'fileencoding' , {} )) 6511eq ( 'test_multi.txt' , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 6512end ) 6513 6514it ( 'handles invalid and incorrect ++ flags gracefully' , function () 6515-- Test invalid ++ff flag 6516local result =exec_lua [[ 6517local cmd = vim.api.nvim_parse_cmd('edit ++ff=invalid test_invalid_ff.txt', {}) 6518local _, err = pcall(vim.cmd, cmd) 6519return err 6520]] 6521matches ( "Invalid argument : '%+%+ff=invalid'$" , result ) 6522 6523-- Test incorrect ++ syntax 6524result =exec_lua [[ 6525local cmd = vim.api.nvim_parse_cmd('edit ++unknown=test_unknown.txt', {}) 6526local _, err = pcall(vim.cmd, cmd) 6527return err 6528]] 6529matches ( "Invalid argument : '%+%+unknown=test_unknown.txt'$" , result ) 6530 6531-- Test invalid ++bin flag 6532result =exec_lua [[ 6533local cmd = vim.api.nvim_parse_cmd('edit ++binabc test_invalid_bin.txt', {}) 6534local _, err = pcall(vim.cmd, cmd) 6535return err 6536]] 6537matches ( "Invalid argument : '%+%+binabc test_invalid_bin.txt'$" , result ) 6538end ) 6539 6540it ( 'handles ++p for creating parent directory' , function () 6541exec_lua [[ 6542vim.cmd('edit flags_dir/test_create.txt') 6543vim.cmd(vim.api.nvim_parse_cmd('write! ++p', {})) 6544]] 6545eq ( true , fn . isdirectory ( 'flags_dir' ) == 1 ) 6546fn . delete ( 'flags_dir' , 'rf' ) 6547end ) 6548 6549it ( 'tests editing files with bad utf8 sequences' , function () 6550-- Write a file with bad utf8 sequences 6551local file =io . open ( 'Xfile' , 'wb' ) 6552file : write ( '[\255][\192][\226\137\240][\194\194]' ) 6553file : close () 6554 6555exec_lua ( [[ 6556vim.cmd(vim.api.nvim_parse_cmd('edit! ++enc=utf8 Xfile', {})) 6557]] ) 6558eq ( '[?][?][???][??]' , api . nvim_get_current_line ()) 6559 6560exec_lua ( [[ 6561vim.cmd(vim.api.nvim_parse_cmd('edit! ++enc=utf8 ++bad=_ Xfile', {})) 6562]] ) 6563eq ( '[_][_][___][__]' , api . nvim_get_current_line ()) 6564 6565exec_lua ( [[ 6566vim.cmd(vim.api.nvim_parse_cmd('edit! ++enc=utf8 ++bad=drop Xfile', {})) 6567]] ) 6568eq ( '[][][][]' , api . nvim_get_current_line ()) 6569 6570exec_lua ( [[ 6571vim.cmd(vim.api.nvim_parse_cmd('edit! ++enc=utf8 ++bad=keep Xfile', {})) 6572]] ) 6573eq ( '[\255][\192][\226\137\240][\194\194]' , api . nvim_get_current_line ()) 6574 6575local result =exec_lua ( [[ 6576local _, err = pcall(vim.cmd, vim.api.nvim_parse_cmd('edit ++enc=utf8 ++bad=foo Xfile', {})) 6577return err 6578]] ) 6579matches ( "Invalid argument : '%+%+bad=foo'$" , result ) 6580-- Clean up 6581os . remove ( 'Xfile' ) 6582end ) 6583it ( 'interprets numeric args as count for count-only commands' , function () 6584api . nvim_cmd ( { cmd ='copen' , args ={ 8 } } , {} ) 6585local height1 =api . nvim_win_get_height ( 0 ) 6586command ( 'cclose' ) 6587api . nvim_cmd ( { cmd ='copen' , count =8 } , {} ) 6588local height2 =api . nvim_win_get_height ( 0 ) 6589command ( 'cclose' ) 6590eq ( height1 , height2 ) 6591 6592exec_lua 'vim.cmd.copen(5)' 6593height2 =api . nvim_win_get_height ( 0 ) 6594command ( 'cclose' ) 6595eq ( 5 , height2 ) 6596 6597-- should reject both count and numeric arg 6598eq ( 6599"Cannot specify both 'count' and numeric argument" , 6600pcall_err ( api . nvim_cmd , { cmd ='copen' , args ={ 5 } , count =10 } , {} ) 6601) 6602end ) 6603it ( 'handles string numeric arguments correctly' , function () 6604-- Valid string numbers should work 6605api . nvim_cmd ( { cmd ='copen' , args ={ '6' } } , {} ) 6606eq ( 6 , api . nvim_win_get_height ( 0 )) 6607command ( 'cclose' ) 6608-- Invalid strings should be rejected 6609eq ( 6610'Wrong number of arguments' , 6611pcall_err ( api . nvim_cmd , { cmd ='copen' , args ={ 'abc' } } , {} ) 6612) 6613-- Partial numbers should be rejected 6614eq ( 6615'Wrong number of arguments' , 6616pcall_err ( api . nvim_cmd , { cmd ='copen' , args ={ '8abc' } } , {} ) 6617) 6618-- Empty string should be rejected 6619eq ( 6620'Invalid command arg: expected non-whitespace' , 6621pcall_err ( api . nvim_cmd , { cmd ='copen' , args ={ '' } } , {} ) 6622) 6623-- Negative string numbers should be rejected 6624eq ( 6625'Wrong number of arguments' , 6626pcall_err ( api . nvim_cmd , { cmd ='copen' , args ={ '-5' } } , {} ) 6627) 6628-- Leading/trailing spaces should be rejected 6629eq ( 6630'Wrong number of arguments' , 6631pcall_err ( api . nvim_cmd , { cmd ='copen' , args ={ ' 5 ' } } , {} ) 6632) 6633end ) 6634end ) 6635 6636it ( 'nvim__redraw' , function () 6637local screen =Screen . new ( 60 , 5 ) 6638eq ( 'at least one action required' , pcall_err ( api . nvim__redraw , {} )) 6639eq ( 'at least one action required' , pcall_err ( api . nvim__redraw , { buf =0 } )) 6640eq ( 'at least one action required' , pcall_err ( api . nvim__redraw , { win =0 } )) 6641eq ( "cannot use both 'buf' and 'win'" , pcall_err ( api . nvim__redraw , { buf =0 , win =0 } )) 6642local win =api . nvim_get_current_win () 6643-- Can move cursor to recently opened window and window is flushed #28868 6644feed ( ':echo getchar()<CR>' ) 6645local newwin =api . nvim_open_win ( 0 , false , { 6646relative ='editor' , 6647width =1 , 6648height =1 , 6649row =1 , 6650col =10 , 6651} ) 6652api . nvim__redraw ( { win =newwin , cursor =true } ) 6653screen : expect ( { 6654grid =[[ 6655| 6656{1:~ }{4:^ }{1: }| 6657{1:~ }|*2 6658:echo getchar() | 6659]] , 6660} ) 6661fn . setline ( 1 , 'foobar' ) 6662command ( 'vnew' ) 6663fn . setline ( 1 , 'foobaz' ) 6664-- Can flush pending screen updates 6665api . nvim__redraw ( { flush =true } ) 6666screen : expect ( { 6667grid =[[ 6668foobaz │foobar | 6669{1:~ }{4:^f}{1: }│{1:~ }| 6670{1:~ }│{1:~ }| 6671{3:[No Name] [+] }{2:[No Name] [+] }| 6672:echo getchar() | 6673]] , 6674} ) 6675api . nvim_win_close ( newwin , true ) 6676-- Can update the grid cursor position #20793 6677api . nvim__redraw ( { cursor =true } ) 6678screen : expect ( { 6679grid =[[ 6680^foobaz │foobar | 6681{1:~ }│{1:~ }|*2 6682{3:[No Name] [+] }{2:[No Name] [+] }| 6683:echo getchar() | 6684]] , 6685} ) 6686-- Also in non-current window 6687api . nvim__redraw ( { cursor =true , win =win } ) 6688screen : expect ( { 6689grid =[[ 6690foobaz │^foobar | 6691{1:~ }│{1:~ }|*2 6692{3:[No Name] [+] }{2:[No Name] [+] }| 6693:echo getchar() | 6694]] , 6695} ) 6696-- Can update the 'statusline' in a single window 6697api . nvim_set_option_value ( 'statusline' , 'statusline1' , { win =0 } ) 6698api . nvim_set_option_value ( 'statusline' , 'statusline2' , { win =win } ) 6699api . nvim__redraw ( { cursor =true , win =0 , statusline =true } ) 6700screen : expect ( { 6701grid =[[ 6702^foobaz │foobar | 6703{1:~ }│{1:~ }|*2 6704{3:statusline1 }{2:[No Name] [+] }| 6705:echo getchar() | 6706]] , 6707} ) 6708api . nvim__redraw ( { win =win , statusline =true } ) 6709screen : expect ( { 6710grid =[[ 6711^foobaz │foobar | 6712{1:~ }│{1:~ }|*2 6713{3:statusline1 }{2:statusline2 }| 6714:echo getchar() | 6715]] , 6716} ) 6717-- Can update the 'statusline' in all windows 6718api . nvim_set_option_value ( 'statusline' , '' , { win =win } ) 6719api . nvim_set_option_value ( 'statusline' , 'statusline3' , {} ) 6720api . nvim__redraw ( { statusline =true } ) 6721screen : expect ( { 6722grid =[[ 6723^foobaz │foobar | 6724{1:~ }│{1:~ }|*2 6725{3:statusline3 }{2:statusline3 }| 6726:echo getchar() | 6727]] , 6728} ) 6729-- Can update the 'statuscolumn' 6730api . nvim_set_option_value ( 'statuscolumn' , 'statuscolumn' , { win =win } ) 6731api . nvim__redraw ( { statuscolumn =true } ) 6732screen : expect ( { 6733grid =[[ 6734^foobaz │{8:statuscolumn}foobar | 6735{1:~ }│{1:~ }|*2 6736{3:statusline3 }{2:statusline3 }| 6737:echo getchar() | 6738]] , 6739} ) 6740-- Can update the 'winbar' 6741api . nvim_set_option_value ( 'winbar' , 'winbar' , { win =0 } ) 6742api . nvim__redraw ( { win =0 , winbar =true } ) 6743screen : expect ( { 6744grid =[[ 6745{5:^winbar }│{8:statuscolumn}foobar | 6746foobaz │{1:~ }| 6747{1:~ }│{1:~ }| 6748{3:statusline3 }{2:statusline3 }| 6749:echo getchar() | 6750]] , 6751} ) 6752-- Can update the 'tabline' 6753api . nvim_set_option_value ( 'showtabline' , 2 , {} ) 6754api . nvim_set_option_value ( 'tabline' , 'tabline' , {} ) 6755api . nvim__redraw ( { tabline =true } ) 6756screen : expect ( { 6757grid =[[ 6758{2:^tabline }| 6759{5:winbar }│{8:statuscolumn}foobar | 6760foobaz │{1:~ }| 6761{3:statusline3 }{2:statusline3 }| 6762:echo getchar() | 6763]] , 6764} ) 6765-- Can update multiple status widgets 6766api . nvim_set_option_value ( 'tabline' , 'tabline2' , {} ) 6767api . nvim_set_option_value ( 'statusline' , 'statusline4' , {} ) 6768api . nvim__redraw ( { statusline =true , tabline =true } ) 6769screen : expect ( { 6770grid =[[ 6771{2:^tabline2 }| 6772{5:winbar }│{8:statuscolumn}foobar | 6773foobaz │{1:~ }| 6774{3:statusline4 }{2:statusline4 }| 6775:echo getchar() | 6776]] , 6777} ) 6778-- Can update all status widgets 6779api . nvim_set_option_value ( 'tabline' , 'tabline3' , {} ) 6780api . nvim_set_option_value ( 'statusline' , 'statusline5' , {} ) 6781api . nvim_set_option_value ( 'statuscolumn' , 'statuscolumn2' , {} ) 6782api . nvim_set_option_value ( 'winbar' , 'winbar2' , {} ) 6783api . nvim__redraw ( { statuscolumn =true , statusline =true , tabline =true , winbar =true } ) 6784screen : expect ( { 6785grid =[[ 6786{2:^tabline3 }| 6787{5:winbar2 }│{5:winbar2 }| 6788{8:statuscolumn2}foobaz │{8:statuscolumn}foobar | 6789{3:statusline5 }{2:statusline5 }| 6790:echo getchar() | 6791]] , 6792} ) 6793-- Can update status widget for a specific window 6794feed ( '<CR><CR>' ) 6795command ( 'let g:status=0' ) 6796api . nvim_set_option_value ( 'statusline' , '%{%g:status%}' , { win =0 } ) 6797command ( 'vsplit' ) 6798screen : expect ( { 6799grid =[[ 6800{2:tabline3 }| 6801{5:winbar2 }│{5:winbar2 }│{5:winbar2 }| 6802{8:statuscolumn2}^foobaz │{8:statuscolumn2}foobaz│{8:statuscolumn}foobar | 6803{3:0 }{2:0 statusline5 }| 680413 | 6805]] , 6806} ) 6807command ( 'let g:status=1' ) 6808api . nvim__redraw ( { win =0 , statusline =true } ) 6809screen : expect ( { 6810grid =[[ 6811{2:tabline3 }| 6812{5:winbar2 }│{5:winbar2 }│{5:winbar2 }| 6813{8:statuscolumn2}^foobaz │{8:statuscolumn2}foobaz│{8:statuscolumn}foobar | 6814{3:1 }{2:0 statusline5 }| 681513 | 6816]] , 6817} ) 6818-- Can update status widget for a specific buffer 6819command ( 'let g:status=2' ) 6820api . nvim__redraw ( { buf =0 , statusline =true } ) 6821screen : expect ( { 6822grid =[[ 6823{2:tabline3 }| 6824{5:winbar2 }│{5:winbar2 }│{5:winbar2 }| 6825{8:statuscolumn2}^foobaz │{8:statuscolumn2}foobaz│{8:statuscolumn}foobar | 6826{3:2 }{2:2 statusline5 }| 682713 | 6828]] , 6829} ) 6830-- valid = true does not draw any lines on its own 6831exec_lua ( [[ 6832_G.lines = 0 6833ns = vim.api.nvim_create_namespace('') 6834vim.api.nvim_set_decoration_provider(ns, { 6835on_win = function() 6836if _G.do_win then 6837vim.api.nvim_buf_set_extmark(0, ns, 0, 0, { hl_group = 'IncSearch', end_col = 6 }) 6838end 6839end, 6840on_line = function() 6841_G.lines = _G.lines + 1 6842end, 6843}) 6844]] ) 6845local lines =exec_lua ( 'return lines' ) 6846api . nvim__redraw ( { buf =0 , valid =true , flush =true } ) 6847eq ( lines , exec_lua ( 'return _G.lines' )) 6848-- valid = false does 6849api . nvim__redraw ( { buf =0 , valid =false , flush =true } ) 6850neq ( lines , exec_lua ( 'return _G.lines' )) 6851-- valid = true does redraw lines if affected by on_win callback 6852exec_lua ( '_G.do_win = true' ) 6853api . nvim__redraw ( { buf =0 , valid =true , flush =true } ) 6854screen : expect ( { 6855grid =[[ 6856{2:tabline3 }| 6857{5:winbar2 }│{5:winbar2 }│{5:winbar2 }| 6858{8:statuscolumn2}{2:^foobaz} │{8:statuscolumn2}{2:foobaz}│{8:statuscolumn}foobar | 6859{3:2 }{2:2 statusline5 }| 686013 | 6861]] , 6862} ) 6863end ) 6864 6865it ( 'nvim__redraw range parameter' , function () 6866Screen . new ( 10 , 5 ) 6867fn . setline ( 1 , fn . range ( 4 )) 6868 6869exec_lua ( [[ 6870_G.lines_list = {} 6871ns = vim.api.nvim_create_namespace('') 6872vim.api.nvim_set_decoration_provider(ns, { 6873on_win = function() 6874end, 6875on_line = function(_, _, _, line) 6876table.insert(_G.lines_list, line) 6877end, 6878}) 6879function _G.get_lines() 6880local lines = _G.lines_list 6881_G.lines_list = {} 6882return lines 6883end 6884]] ) 6885 6886api . nvim__redraw ( { flush =true , valid =false } ) 6887exec_lua ( '_G.get_lines()' ) 6888 6889local actual_lines ={} 6890local function test ( range ) 6891api . nvim__redraw ( { win =0 , range =range } ) 6892table . insert ( actual_lines , exec_lua ( 'return _G.get_lines()' )) 6893end 6894 6895test ( { 0 , - 1 } ) 6896test ( { 2 , 2 ^ 31 } ) 6897test ( { 2 , 2 ^ 32 } ) 6898test ( { 2 ^ 31 - 1 , 2 } ) 6899test ( { 2 ^ 32 - 1 , 2 } ) 6900 6901local expected_lines ={ 6902{ 0 , 1 , 2 , 3 } , 6903{ 2 , 3 } , 6904{ 2 , 3 } , 6905{} , 6906{} , 6907} 6908eq ( expected_lines , actual_lines ) 6909 6910n . assert_alive () 6911end ) 6912 6913it ( 'nvim__redraw updates topline' , function () 6914local screen =Screen . new ( 40 , 8 ) 6915fn . setline ( 1 , fn . range ( 100 )) 6916feed ( ':call getchar()<CR>' ) 6917fn . cursor ( 50 , 1 ) 6918screen : expect ( [[ 69190 | 69201 | 69212 | 69223 | 69234 | 69245 | 69256 | 6926^:call getchar() | 6927]] ) 6928api . nvim__redraw ( { flush =true } ) 6929screen : expect ( [[ 693046 | 693147 | 693248 | 693349 | 693450 | 693551 | 693652 | 6937^:call getchar() | 6938]] ) 6939end ) 6940end )