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 tt =require ( 'test.functional.testterm' ) 5 6local describe , it , before_each , finally =t . describe , t . it , t . before_each , t . finally 7local assert_alive =n . assert_alive 8local feed , clear =n . feed , n . clear 9local poke_eventloop =n . poke_eventloop 10local nvim_prog =n . nvim_prog 11local eval , source =n . eval , n . source 12local pcall_err =t . pcall_err 13local eq , neq =t . eq , t . neq 14local api =n . api 15local retry =t . retry 16local testprg =n . testprg 17local write_file =t . write_file 18local command =n . command 19local matches =t . matches 20local not_matches =t . not_matches 21local exec_lua =n . exec_lua 22local sleep =vim . uv . sleep 23local fn =n . fn 24local is_os =t . is_os 25local skip =t . skip 26 27describe ( ':terminal buffer' , function () 28local screen 29 30before_each ( function () 31clear () 32command ( 'set modifiable swapfile undolevels=20' ) 33screen =tt . setup_screen () 34end ) 35 36it ( 'terminal-mode forces various options' , function () 37local expr = 38'[&l:cursorlineopt, &l:cursorline, &l:cursorcolumn, &l:scrolloff, &l:sidescrolloff]' 39 40feed ( [[<C-\><C-N>]] ) 41command ( 'setlocal cursorline cursorlineopt=both cursorcolumn scrolloff=4 sidescrolloff=7' ) 42eq ( { 'both' , 1 , 1 , 4 , 7 } , eval ( expr )) 43eq ( 'nt' , eval ( 'mode(1)' )) 44 45-- Enter Terminal mode ("insert" mode in :terminal). 46feed ( 'i' ) 47eq ( 't' , eval ( 'mode(1)' )) 48eq ( { 'number' , 1 , 0 , 0 , 0 } , eval ( expr )) 49 50-- Return to Normal mode. 51feed ( [[<C-\><C-N>]] ) 52eq ( 'nt' , eval ( 'mode(1)' )) 53eq ( { 'both' , 1 , 1 , 4 , 7 } , eval ( expr )) 54 55-- Enter Terminal mode again. 56feed ( 'i' ) 57eq ( 't' , eval ( 'mode(1)' )) 58eq ( { 'number' , 1 , 0 , 0 , 0 } , eval ( expr )) 59 60-- Delete the terminal buffer and return to the previous buffer. 61command ( 'bwipe!' ) 62feed ( '<Ignore>' ) -- Add input to separate two RPC requests 63eq ( 'n' , eval ( 'mode(1)' )) 64-- Window options in the old buffer should be unchanged. #37484 65eq ( { 'both' , 0 , 0 , - 1 , - 1 } , eval ( expr )) 66end ) 67 68it ( 'terminal-mode does not change cursorlineopt if cursorline is disabled' , function () 69feed ( [[<C-\><C-N>]] ) 70command ( 'setlocal nocursorline cursorlineopt=both' ) 71feed ( 'i' ) 72eq ( { 0 , 'both' } , eval ( '[&l:cursorline, &l:cursorlineopt]' )) 73end ) 74 75it ( 'terminal-mode disables cursorline when cursorlineopt is only set to "line"' , function () 76feed ( [[<C-\><C-N>]] ) 77command ( 'setlocal cursorline cursorlineopt=line' ) 78feed ( 'i' ) 79eq ( { 0 , 'line' } , eval ( '[&l:cursorline, &l:cursorlineopt]' )) 80end ) 81 82describe ( 'swap and undo' , function () 83before_each ( function () 84feed ( '<c-\\><c-n>' ) 85screen : expect ( [[ 86tty ready | 87^ | 88|*5 89]] ) 90end ) 91 92it ( 'does not create swap files' , function () 93eq ( 'No swap file' , n . exec_capture ( 'swapname' )) 94end ) 95 96it ( 'does not create undo files' , function () 97local undofile =api . nvim_eval ( 'undofile(bufname("%"))' ) 98eq ( nil , io . open ( undofile )) 99end ) 100end ) 101 102it ( 'cannot be modified directly' , function () 103feed ( '<c-\\><c-n>dd' ) 104screen : expect ( [[ 105tty ready | 106^ | 107|*4 108{101:E21: Cannot make changes, 'modifiable' is off} | 109]] ) 110end ) 111 112it ( 'sends data to the terminal when the "put" operator is used' , function () 113feed ( '<c-\\><c-n>gg"ayj' ) 114fn . setreg ( 'a' , 'appended ' .. fn . getreg ( 'a' )) 115feed ( '"ap"ap' ) 116screen : expect ( [[ 117^tty ready | 118appended tty ready |*2 119|*4 120]] ) 121-- operator count is also taken into consideration 122feed ( '3"ap' ) 123screen : expect ( [[ 124^tty ready | 125appended tty ready |*5 126| 127]] ) 128-- pasting expression register shouldn't leak memory 129feed ( [["="abcd\nefghi"<CR>pi]] ) 130screen : expect ( [[ 131appended tty ready |*4 132abcd | 133efghi^ | 134{5:-- TERMINAL --} | 135]] ) 136end ) 137 138it ( 'sends data to the terminal when the ":put" command is used' , function () 139feed ( '<c-\\><c-n>gg"ayj' ) 140fn . setreg ( 'a' , 'appended ' .. fn . getreg ( 'a' )) 141feed ( ':put a<CR>' ) 142screen : expect ( [[ 143^tty ready | 144appended tty ready | 145|*4 146:put a | 147]] ) 148-- line argument is only used to move the cursor 149feed ( ':6put a<CR>' ) 150screen : expect ( [[ 151tty ready | 152appended tty ready |*2 153|*2 154^ | 155:6put a | 156]] ) 157end ) 158 159it ( 'can be deleted' , function () 160feed ( '<c-\\><c-n>:bd!<cr>' ) 161screen : expect ( [[ 162^ | 163{100:~ }|*5 164:bd! | 165]] ) 166feed ( ':bnext<CR>' ) 167screen : expect ( [[ 168^ | 169{100:~ }|*5 170:bnext | 171]] ) 172end ) 173 174it ( 'handles loss of focus gracefully' , function () 175-- Change the statusline to avoid printing the file name, which varies. 176api . nvim_set_option_value ( 'statusline' , '==========' , {} ) 177 178-- Save the buffer number of the terminal for later testing. 179local tbuf =eval ( 'bufnr("%")' ) 180local exitcmd =is_os ( 'win' ) and "['cmd', '/c', 'exit']" or "['sh', '-c', 'exit']" 181source ( [[ 182function! SplitWindow(id, data, event) 183new 184call feedkeys("iabc\<Esc>") 185endfunction 186 187startinsert 188call jobstart(]] .. exitcmd .. [[, {'on_exit': function("SplitWindow")}) 189call feedkeys("\<C-\>", 't') " vim will expect <C-n>, but be exited out of 190" the terminal before it can be entered. 191]] ) 192 193-- We should be in a new buffer now. 194screen : expect ( [[ 195ab^c | 196{100:~ }| 197{3:========== }| 198rows: 2, cols: 50 | 199| 200{119:========== }| 201| 202]] ) 203 204neq ( tbuf , eval ( 'bufnr("%")' )) 205command ( 'quit!' ) -- Should exit the new window, not the terminal. 206eq ( tbuf , eval ( 'bufnr("%")' )) 207end ) 208 209describe ( 'handles confirmations' , function () 210it ( 'with :confirm' , function () 211feed ( '<c-\\><c-n>' ) 212feed ( ':confirm bdelete<CR>' ) 213screen : expect { any ='Close "term://' } 214end ) 215 216it ( 'with &confirm' , function () 217feed ( '<c-\\><c-n>' ) 218feed ( ':bdelete<CR>' ) 219screen : expect { any ='E89' } 220feed ( '<cr>' ) 221eq ( 'terminal' , eval ( '&buftype' )) 222feed ( ':set confirm | bdelete<CR>' ) 223screen : expect { any ='Close "term://' } 224feed ( 'y' ) 225neq ( 'terminal' , eval ( '&buftype' )) 226end ) 227end ) 228 229it ( 'it works with set rightleft #11438' , function () 230local columns =eval ( '&columns' ) 231feed ( string . rep ( 'a' , columns )) 232command ( 'set rightleft' ) 233screen : expect ( [[ 234ydaer ytt| 235^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa| 236|*4 237{5:-- TERMINAL --} | 238]] ) 239command ( 'bdelete!' ) 240end ) 241 242it ( 'requires bang (!) to close a running job #15402' , function () 243eq ( 'Vim(wqall):E948: Job still running (add ! to end the job)' , pcall_err ( command , 'wqall' )) 244command ( 'redir => g:wqall_out | silent! wqall | redir END' ) 245matches ( 'E948:' , api . nvim_get_var ( 'wqall_out' )) 246not_matches ( 'E676:' , api . nvim_get_var ( 'wqall_out' )) 247for _ , cmd in ipairs ( { 'bdelete' , '%bdelete' , 'bwipeout' , 'bunload' } ) do 248matches ( 249'^Vim%(' 250.. cmd : gsub ( '%%' , '' ) 251.. '%):E89: term://.*tty%-test.* will be killed %(add %! to override%)$' , 252pcall_err ( command , cmd ) 253) 254end 255command ( 'call jobstop(&channel)' ) 256assert ( 0 >= eval ( 'jobwait([&channel], 1000)[0]' )) 257command ( 'bdelete' ) 258end ) 259 260it ( ':wqall! closes a running job' , function () 261n . expect_exit ( command , 'wqall!' ) 262end ) 263 264it ( 'stops running jobs with :quit' , function () 265-- Open in a new window to avoid terminating the nvim instance 266command ( 'split' ) 267command ( 'terminal' ) 268command ( 'set nohidden' ) 269command ( 'quit' ) 270end ) 271 272it ( 'does not segfault when pasting empty register #13955' , function () 273feed ( '<c-\\><c-n>' ) 274command ( 'put a' ) -- register a is empty 275n . assert_alive () 276end ) 277 278it ( [[can use temporary normal mode <c-\><c-o>]] , function () 279eq ( 't' , fn . mode ( 1 )) 280feed [[<c-\><c-o>]] 281screen : expect ( [[ 282tty ready | 283^ | 284|*4 285{5:-- (terminal) --} | 286]] ) 287eq ( 'ntT' , fn . mode ( 1 )) 288 289feed [[:let g:x = 17]] 290screen : expect ( [[ 291tty ready | 292|*5 293:let g:x = 17^ | 294]] ) 295 296feed [[<cr>]] 297screen : expect ( [[ 298tty ready | 299^ | 300|*4 301{5:-- TERMINAL --} | 302]] ) 303eq ( 't' , fn . mode ( 1 )) 304end ) 305 306it ( 'is refreshed with partial mappings in Terminal mode #9167' , function () 307command ( [[set timeoutlen=20000 | tnoremap jk <C-\><C-N>]] ) 308feed ( 'j' ) -- Won't reach the terminal until the next character is typed 309screen : expect_unchanged () 310feed ( 'j' ) -- Refresh scheduled for the first 'j' but not processed 311screen : expect_unchanged () 312for i =1 , 10 do 313eq ( { mode ='t' , blocking =true } , api . nvim_get_mode ()) 314-- Wait for the previously scheduled refresh timer to arrive. Must comfortably exceed 315-- terminal.c REFRESH_DELAY *plus* PTY echo round-trip; else the refresh isn't queued before 316-- the next key and, since a partial mapping never drains main_loop.events, screen is stale. 317vim . uv . sleep ( 50 ) 318feed ( 'j' ) -- Refresh scheduled for the last 'j' and processed for the one before 319screen : expect (( [[ 320tty ready | 321%s^%s| 322|*4 323{5:-- TERMINAL --} | 324]] ): format (( 'j' ): rep ( i ), ( ' ' ): rep ( 50 - i ))) 325end 326feed ( 'l' ) -- No partial mapping, so all pending refreshes should be processed 327screen : expect ( [[ 328tty ready | 329jjjjjjjjjjjjl^ | 330|*4 331{5:-- TERMINAL --} | 332]] ) 333end ) 334 335it ( 'is refreshed with partial mappings in Normal mode' , function () 336command ( 'set timeoutlen=20000 | nnoremap jk :' ) 337command ( 'nnoremap j <Cmd>call chansend(&channel, "j")<CR>' ) 338feed ( [[<C-\><C-N>]] ) 339screen : expect ( [[ 340tty ready | 341^ | 342|*5 343]] ) 344feed ( 'j' ) -- Won't reach the terminal until the next character is typed 345screen : expect_unchanged () 346feed ( 'j' ) -- Refresh scheduled for the first 'j' but not processed 347screen : expect_unchanged () 348for i =1 , 10 do 349eq ( { mode ='nt' , blocking =true } , api . nvim_get_mode ()) 350-- Wait for the previously scheduled refresh timer to arrive. Must comfortably exceed 351-- terminal.c REFRESH_DELAY *plus* PTY echo round-trip; else the refresh isn't queued before 352-- the next key and, since a partial mapping never drains main_loop.events, screen is stale. 353vim . uv . sleep ( 50 ) 354feed ( 'j' ) -- Refresh scheduled for the last 'j' and processed for the one before 355screen : expect (( [[ 356tty ready | 357^%s%s| 358|*5 359]] ): format (( 'j' ): rep ( i ), ( ' ' ): rep ( 50 - i ))) 360end 361feed ( 'l' ) -- No partial mapping, so all pending refreshes should be processed 362screen : expect ( [[ 363tty ready | 364j^jjjjjjjjjjj | 365|*5 366]] ) 367end ) 368 369it ( 'writing to an existing file with :w fails #13549' , function () 370eq ( 371'Vim(write):E13: File exists (add ! to override)' , 372pcall_err ( command , 'write test/functional/fixtures/tty-test.c' ) 373) 374end ) 375 376it ( 'external interrupt (got_int) does not hang #20726' , function () 377eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 378command ( 'call timer_start(0, {-> interrupt()})' ) 379feed ( '<Ignore>' ) -- Add input to separate two RPC requests 380eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 381feed ( [[<C-\><C-N>]] ) 382eq ( { mode ='nt' , blocking =false } , api . nvim_get_mode ()) 383command ( 'bd!' ) 384end ) 385 386it ( 'correct size when switching buffers' , function () 387local term_buf =api . nvim_get_current_buf () 388command ( 'file foo | enew | vsplit' ) 389api . nvim_set_current_buf ( term_buf ) 390screen : expect ( [[ 391tty ready │ | 392^rows: 5, cols: 25 │{100:~ }| 393│{100:~ }|*3 394{120:foo [-] }{2:[No Name] }| 395| 396]] ) 397 398feed ( '<C-^><C-W><C-O><C-^>' ) 399screen : expect ( [[ 400tty ready | 401^rows: 5, cols: 25 | 402rows: 6, cols: 50 | 403|*4 404]] ) 405end ) 406 407it ( 'reports focus notifications when requested' , function () 408feed ( [[<C-\><C-N>]] ) 409exec_lua ( function () 410local function new_test_term () 411local chan =vim . api . nvim_open_term ( 0 , { 412on_input =function ( _ , term , buf , data ) 413if data == '\27[I' then 414vim . b [ buf ]. term_focused =true 415vim . api . nvim_chan_send ( term , 'focused\n' ) 416elseif data == '\27[O' then 417vim . b [ buf ]. term_focused =false 418vim . api . nvim_chan_send ( term , 'unfocused\n' ) 419end 420end , 421} ) 422vim . b . term_focused =false 423vim . api . nvim_chan_send ( chan , '\27[?1004h' ) -- Enable focus reporting 424end 425 426vim . cmd 'edit bar' 427new_test_term () 428vim . cmd 'vnew foo' 429new_test_term () 430vim . cmd 'vsplit' 431end ) 432screen : expect ( [[ 433^ │ │ | 434│ │ |*4 435{120:foo [-] }{119:foo [-] bar [-] }| 436| 437]] ) 438 439-- TermEnter/Leave happens *after* entering/leaving terminal mode, so focus should've changed 440-- already by the time these events run. 441exec_lua ( function () 442_G . last_event =nil 443vim . api . nvim_create_autocmd ( { 'TermEnter' , 'TermLeave' } , { 444callback =function ( ev ) 445_G . last_event =ev . event 446.. ' ' 447.. vim . fs . basename ( ev . file ) 448.. ' ' 449.. tostring ( vim . b [ ev . buf ]. term_focused ) 450end , 451} ) 452end ) 453 454feed ( 'i' ) 455screen : expect ( [[ 456focused │focused │ | 457^ │ │ | 458│ │ |*3 459{120:foo [-] }{119:foo [-] bar [-] }| 460{5:-- TERMINAL --} | 461]] ) 462eq ( 'TermEnter foo true' , exec_lua ( 'return _G.last_event' )) 463 464-- Next window has the same terminal; no new notifications. 465command ( 'wincmd w' ) 466screen : expect ( [[ 467focused │focused │ | 468│^ │ | 469│ │ |*3 470{119:foo [-] }{120:foo [-] }{119:bar [-] }| 471{5:-- TERMINAL --} | 472]] ) 473-- Next window has a different terminal; expect new unfocus and focus notifications. 474command ( 'wincmd w' ) 475screen : expect ( [[ 476focused │focused │focused | 477unfocused │unfocuse│^ | 478│ │ |*3 479{119:foo [-] foo [-] }{120:bar [-] }| 480{5:-- TERMINAL --} | 481]] ) 482-- Leaving terminal mode; expect a new unfocus notification. 483feed ( [[<C-\><C-N>]] ) 484screen : expect ( [[ 485focused │focused │focused | 486unfocused │unfocuse│unfocused | 487│ │^ | 488│ │ |*2 489{119:foo [-] foo [-] }{120:bar [-] }| 490| 491]] ) 492eq ( 'TermLeave bar false' , exec_lua ( 'return _G.last_event' )) 493end ) 494 495it ( 'no crash with race between buffer close and OSC 2' , function () 496skip ( is_os ( 'win' ), 'tty-test cannot forward OSC 2 on Windows?' ) 497exec_lua ( function () 498vim . api . nvim_chan_send ( vim . bo . channel , '\027]2;SOME_TITLE\007' ) 499end ) 500retry ( nil , 4000 , function () 501eq ( 'SOME_TITLE' , api . nvim_buf_get_var ( 0 , 'term_title' )) 502end ) 503screen : expect_unchanged () 504--- @type string 505local title_before_del =exec_lua ( function () 506vim . wait ( 10 ) -- Ensure there are no pending events so that a write isn't queued. 507vim . api . nvim_chan_send ( vim . bo . channel , '\027]2;OTHER_TITLE\007' ) 508vim . uv . sleep ( 50 ) -- Block the event loop and wait for tty-test to forward OSC 2. 509local term_title =vim . b . term_title 510vim . api . nvim_buf_delete ( 0 , { force =true } ) 511vim . wait ( 10 , nil , nil , true ) -- Process fast events only. 512return term_title 513end ) 514-- Title isn't changed until the second vim.wait(). 515eq ( 'SOME_TITLE' , title_before_del ) 516screen : expect ( [[ 517^ | 518{100:~ }|*5 519| 520]] ) 521assert_alive () 522end ) 523 524describe ( 'handles suspended PTY process' , function () 525if skip ( is_os ( 'win' ), 'N/A for Windows' ) then 526return 527end 528 529--- @param external_resume boolean 530local function test_term_process_suspend_resume ( external_resume ) 531command ( 'set mousemodel=extend' ) 532local pid =eval ( 'jobpid(&channel)' ) 533vim . uv . kill ( pid , 'sigstop' ) 534-- In Terminal mode the cursor is at the "[Process suspended]" text to hint that 535-- pressing a key will change the suspended state. 536local s0 =[[ 537tty ready | 538|*4 539^[Process suspended] | 540{5:-- TERMINAL --} | 541]] 542screen : expect ( s0 ) 543feed ( [[<C-\><C-N>]] ) 544-- Returning to Normal mode puts the cursor at its previous position as that's 545-- closer to the terminal output, making it easier for the user to copy. 546screen : expect ( [[ 547tty ready | 548^ | 549|*3 550[Process suspended] | 551| 552]] ) 553feed ( 'i' ) 554screen : expect ( s0 ) 555command ( 'set laststatus=0 | botright vsplit' ) 556screen : expect ( [[ 557tty ready │tty ready | 558│ |*4 559[Process suspended] │^[Process suspended] | 560{5:-- TERMINAL --} | 561]] ) 562-- Resize is detected by the process on resume. 563if external_resume then 564vim . uv . kill ( pid , 'sigcont' ) 565else 566feed ( 'a' ) 567end 568screen : expect ( [[ 569tty ready │tty ready | 570rows: 6, cols: 25 │rows: 6, cols: 25 | 571│^ | 572│ |*3 573{5:-- TERMINAL --} | 574]] ) 575tt . enable_mouse () 576tt . feed_data ( 'mouse enabled\n\n\n\n' ) 577screen : expect ( [[ 578rows: 6, cols: 25 │rows: 6, cols: 25 | 579mouse enabled │mouse enabled | 580│ |*3 581│^ | 582{5:-- TERMINAL --} | 583]] ) 584api . nvim_input_mouse ( 'right' , 'press' , '' , 0 , 0 , 25 ) 585screen : expect ( { any =vim . pesc ( '"!!^' ) } ) 586api . nvim_input_mouse ( 'right' , 'release' , '' , 0 , 0 , 25 ) 587screen : expect ( { any =vim . pesc ( '#!!^' ) } ) 588vim . uv . kill ( pid , 'sigstop' ) 589local s1 =[[ 590rows: 6, cols: 25 │rows: 6, cols: 25 | 591mouse enabled │mouse enabled | 592│ |*3 593[Process suspended] │^[Process suspended] | 594{5:-- TERMINAL --} | 595]] 596screen : expect ( s1 ) 597-- Mouse isn't forwarded when process is suspended. 598api . nvim_input_mouse ( 'right' , 'press' , '' , 0 , 1 , 27 ) 599api . nvim_input_mouse ( 'right' , 'release' , '' , 0 , 1 , 27 ) 600screen : expect ( [[ 601rows: 6, cols: 25 │rows: 6, cols: 25 | 602mo{108:use enabled} │mo{108:^use enabled} | 603{108: } │{108: } |*3 604[Process suspended] │[Process suspended] | 605{5:-- VISUAL --} | 606]] ) 607feed ( '<Esc>i' ) 608screen : expect ( s1 ) 609if external_resume then 610vim . uv . kill ( pid , 'sigcont' ) 611else 612feed ( 'a' ) 613end 614screen : expect ( [[ 615rows: 6, cols: 25 │rows: 6, cols: 25 | 616mouse enabled │mouse enabled | 617│ |*3 618#!! │ #!!^ | 619{5:-- TERMINAL --} | 620]] ) 621-- Mouse is forwarded after process is resumed. 622api . nvim_input_mouse ( 'right' , 'press' , '' , 0 , 0 , 28 ) 623screen : expect ( { any =vim . pesc ( '"$!^' ) } ) 624api . nvim_input_mouse ( 'right' , 'release' , '' , 0 , 0 , 28 ) 625screen : expect ( { any =vim . pesc ( '#$!^' ) } ) 626end 627 628it ( 'resumed by an external signal' , function () 629skip ( is_os ( 'mac' ), 'FIXME: does not work on macOS' ) 630test_term_process_suspend_resume ( true ) 631end ) 632 633it ( 'resumed by pressing a key' , function () 634test_term_process_suspend_resume ( false ) 635end ) 636end ) 637end ) 638 639describe ( ':terminal buffer' , function () 640before_each ( clear ) 641 642it ( 'exit emits FileChangedShell #41759' , function () 643local path =t . tmpname () 644write_file ( path , 'foo\n' ) 645command ( 'edit ' .. path ) 646command ( 'set noautoread' ) -- Disable 'autoread' to exercise :checktime specifically. 647api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'local change' } ) 648n . exec ( 'let g:fcs = 0 | autocmd FileChangedShell * let g:fcs = 1' ) 649write_file ( path , 'external change\n' ) 650 651-- Terminal in another tab, so returning to the buffer is not what triggers the check. 652command ( 'tabnew' ) 653fn . jobstart ( { testprg ( 'shell-test' ), 'EXIT' , '0' } , { term =true } ) 654retry ( nil , 10000 , function () 655eq ( 1 , api . nvim_get_var ( 'fcs' )) 656end ) 657end ) 658 659it ( 'can resume suspended PTY process running in fish' , function () 660skip ( is_os ( 'win' ), 'N/A for Windows' ) 661skip ( fn . executable ( 'fish' ) == 0 , 'missing "fish" command' ) 662 663local screen =Screen . new ( 50 , 7 ) 664screen : add_extra_attr_ids ( { 665[ 100 ] ={ 666foreground =Screen . colors . NvimDarkGrey2 , 667background =Screen . colors . NvimLightGrey2 , 668} , 669[ 101 ] ={ 670foreground =Screen . colors . NvimLightGrey4 , 671background =Screen . colors . NvimLightGrey2 , 672} , 673[ 102 ] ={ 674foreground =Screen . colors . NvimDarkGrey2 , 675background =Screen . colors . NvimLightGrey4 , 676} , 677} ) 678command ( 'set shell=fish termguicolors' ) 679command (( 'terminal %s -u NONE -i NONE' ): format ( fn . shellescape ( nvim_prog ))) 680command ( 'startinsert' ) 681local s0 =[[ 682{100:^ }| 683{101:~ }|*3 684{102:[No Name] 0,0-1 All}| 685{100: }| 686{5:-- TERMINAL --} | 687]] 688screen : expect ( s0 ) 689feed ( '<C-Z>' ) 690screen : expect ( [[ 691|*5 692^[Process suspended] | 693{5:-- TERMINAL --} | 694]] ) 695feed ( '<Space>' ) 696screen : expect ( s0 ) 697end ) 698 699it ( 'term_close() use-after-free #4393' , function () 700command ( 'terminal yes' ) 701feed ( '<Ignore>' ) -- Add input to separate two RPC requests 702command ( 'bdelete!' ) 703end ) 704 705describe ( 'TermRequest' , function () 706it ( 'emits events #26972' , function () 707local term =api . nvim_open_term ( 0 , {} ) 708local termbuf =api . nvim_get_current_buf () 709 710-- Test that <abuf> is the terminal buffer, not the current buffer 711command ( 'au TermRequest * let g:termbuf = +expand("<abuf>")' ) 712command ( 'wincmd p' ) 713 714-- cwd will be inserted in a file URI, which cannot contain backs 715local cwd =t . fix_slashes ( fn . getcwd ()) 716local parent =cwd : match ( '^(.+/)' ) 717local expected ='\027]7;file://host' .. parent 718api . nvim_chan_send ( term , string . format ( '%s\027\\' , expected )) 719eq ( expected , eval ( 'v:termrequest' )) 720eq ( termbuf , eval ( 'g:termbuf' )) 721end ) 722 723it ( 'emits events for APC' , function () 724local term =api . nvim_open_term ( 0 , {} ) 725 726-- cwd will be inserted in a file URI, which cannot contain backs 727local cwd =t . fix_slashes ( fn . getcwd ()) 728local parent =cwd : match ( '^(.+/)' ) 729local expected ='\027_Gfile://host' .. parent 730api . nvim_chan_send ( term , string . format ( '%s\027\\' , expected )) 731eq ( expected , eval ( 'v:termrequest' )) 732end ) 733 734it ( 'synchronization #27572' , function () 735command ( 'autocmd! nvim.terminal TermRequest' ) 736local term =exec_lua ( [[ 737_G.input = {} 738local term = vim.api.nvim_open_term(0, { 739on_input = function(_, _, _, data) 740table.insert(_G.input, data) 741end, 742force_crlf = false, 743}) 744vim.api.nvim_create_autocmd('TermRequest', { 745callback = function(ev) 746if ev.data.sequence == '\027]11;?' then 747table.insert(_G.input, '\027]11;rgb:0000/0000/0000\027\\') 748end 749end 750}) 751return term 752]] ) 753api . nvim_chan_send ( term , '\027]11;?\007\027[5n\027]11;?\007\027[5n' ) 754eq ( { 755'\027]11;rgb:0000/0000/0000\027\\' , 756'\027[0n' , 757'\027]11;rgb:0000/0000/0000\027\\' , 758'\027[0n' , 759} , exec_lua ( 'return _G.input' )) 760end ) 761 762it ( 'works with vim.wait() from another autocommand #32706' , function () 763command ( 'autocmd! nvim.terminal TermRequest' ) 764exec_lua ( [[ 765local term = vim.api.nvim_open_term(0, {}) 766vim.api.nvim_create_autocmd('TermRequest', { 767buf = 0, 768callback = function(ev) 769_G.sequence = ev.data.sequence 770_G.v_termrequest = vim.v.termrequest 771end, 772}) 773vim.api.nvim_create_autocmd('TermEnter', { 774buf = 0, 775callback = function() 776vim.api.nvim_chan_send(term, '\027]11;?\027\\') 777_G.result = vim.wait(3000, function() 778local expected = '\027]11;?' 779return _G.sequence == expected and _G.v_termrequest == expected 780end) 781end, 782}) 783]] ) 784feed ( 'i' ) 785retry ( nil , 4000 , function () 786eq ( true , exec_lua ( 'return _G.result' )) 787end ) 788end ) 789 790it ( 'includes cursor position #31609' , function () 791command ( 'autocmd! nvim.terminal TermRequest' ) 792local screen =Screen . new ( 50 , 10 ) 793local term =exec_lua ( [[ 794_G.cursor = {} 795local term = vim.api.nvim_open_term(0, {}) 796vim.api.nvim_create_autocmd('TermRequest', { 797callback = function(ev) 798_G.cursor = ev.data.cursor 799end 800}) 801return term 802]] ) 803-- Enter terminal mode so that the cursor follows the output 804feed ( 'a' ) 805 806-- Put some lines into the scrollback. This tests the conversion from terminal line to buffer 807-- line. 808api . nvim_chan_send ( term , string . rep ( '>\n' , 20 )) 809screen : expect ( [[ 810> |*8 811^ | 812{5:-- TERMINAL --} | 813]] ) 814 815-- Emit an OSC escape sequence 816api . nvim_chan_send ( term , 'Hello\nworld!\027]133;D\027\\' ) 817screen : expect ( [[ 818> |*7 819Hello | 820world!^ | 821{5:-- TERMINAL --} | 822]] ) 823eq ( { 22 , 6 } , exec_lua ( 'return _G.cursor' )) 824 825api . nvim_chan_send ( term , '\nHello\027]133;D\027\\\nworld!\n' ) 826screen : expect ( [[ 827> |*4 828Hello | 829world! | 830Hello | 831world! | 832^ | 833{5:-- TERMINAL --} | 834]] ) 835eq ( { 23 , 5 } , exec_lua ( 'return _G.cursor' )) 836 837api . nvim_chan_send ( term , 'Hello\027]133;D\027\\\nworld!' .. ( '\n' ): rep ( 6 )) 838screen : expect ( [[ 839world! | 840Hello | 841world! | 842|*5 843^ | 844{5:-- TERMINAL --} | 845]] ) 846eq ( { 25 , 5 } , exec_lua ( 'return _G.cursor' )) 847 848api . nvim_set_option_value ( 'scrollback' , 10 , {} ) 849eq ( 19 , api . nvim_buf_line_count ( 0 )) 850 851api . nvim_chan_send ( term , 'Hello\nworld!\027]133;D\027\\' ) 852screen : expect ( [[ 853Hello | 854world! | 855|*5 856Hello | 857world!^ | 858{5:-- TERMINAL --} | 859]] ) 860eq ( { 19 , 6 } , exec_lua ( 'return _G.cursor' )) 861 862api . nvim_chan_send ( term , '\nHello\027]133;D\027\\\nworld!\n' ) 863screen : expect ( [[ 864|*4 865Hello | 866world! | 867Hello | 868world! | 869^ | 870{5:-- TERMINAL --} | 871]] ) 872eq ( { 17 , 5 } , exec_lua ( 'return _G.cursor' )) 873 874api . nvim_chan_send ( term , 'Hello\027]133;D\027\\\nworld!' .. ( '\n' ): rep ( 6 )) 875screen : expect ( [[ 876world! | 877Hello | 878world! | 879|*5 880^ | 881{5:-- TERMINAL --} | 882]] ) 883eq ( { 12 , 5 } , exec_lua ( 'return _G.cursor' )) 884 885api . nvim_chan_send ( term , 'Hello\027]133;D\027\\\nworld!' .. ( '\n' ): rep ( 8 )) 886screen : expect ( [[ 887world! | 888|*7 889^ | 890{5:-- TERMINAL --} | 891]] ) 892eq ( { 10 , 5 } , exec_lua ( 'return _G.cursor' )) 893 894api . nvim_chan_send ( term , 'Hello\027]133;D\027\\\nworld!' .. ( '\n' ): rep ( 20 )) 895screen : expect ( [[ 896|*8 897^ | 898{5:-- TERMINAL --} | 899]] ) 900eq ( { - 2 , 5 } , exec_lua ( 'return _G.cursor' )) 901end ) 902 903it ( 'does not cause hang in vim.wait() #32753' , function () 904local screen =Screen . new ( 50 , 10 ) 905 906exec_lua ( function () 907local term =vim . api . nvim_open_term ( 0 , {} ) 908 909-- Write OSC sequence with pending scrollback. TermRequest will 910-- reschedule itself onto an event queue until the pending scrollback is 911-- processed (i.e. the terminal is refreshed). 912vim . api . nvim_chan_send ( term , string . format ( '%s\027]133;;\007' , string . rep ( 'a\n' , 100 ))) 913 914-- vim.wait() drains the event queue. The terminal won't be refreshed 915-- until the event queue is empty. This test ensures that TermRequest 916-- does not continuously reschedule itself onto the same event queue, 917-- causing an infinite loop. 918vim . wait ( 100 ) 919end ) 920 921screen : expect ( [[ 922^a | 923a |*8 924| 925]] ) 926end ) 927 928describe ( 'no heap-use-after-free after' , function () 929local term 930 931before_each ( function () 932term =exec_lua ( function () 933vim . api . nvim_create_autocmd ( 'TermRequest' , { callback =function () end } ) 934return vim . api . nvim_open_term ( 0 , {} ) 935end ) 936end ) 937 938it ( 'wiping buffer with pending TermRequest #37226' , function () 939exec_lua ( function () 940vim . api . nvim_chan_send ( term , '\027]8;;https://example.com\027\\' ) 941vim . api . nvim_buf_delete ( 0 , { force =true } ) 942end ) 943assert_alive () 944end ) 945 946it ( 'unloading buffer with pending TermRequest #37226' , function () 947api . nvim_create_buf ( true , false ) -- Create a buffer to switch to. 948exec_lua ( function () 949vim . api . nvim_chan_send ( term , '\027]8;;https://example.com\027\\' ) 950vim . api . nvim_buf_delete ( 0 , { force =true , unload =true } ) 951end ) 952assert_alive () 953end ) 954end ) 955end ) 956 957it ( 'no heap-buffer-overflow when using jobstart("echo",{term=true}) #3161' , function () 958local testfilename ='Xtestfile-functional-terminal-buffers_spec' 959write_file ( testfilename , 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) 960finally ( function () 961os . remove ( testfilename ) 962end ) 963feed ( ':edit ' .. testfilename .. '<CR>' ) 964-- Move cursor away from the beginning of the line 965feed ( '$' ) 966-- Let jobstart(…,{term=true}) modify the buffer 967fn . jobstart ( 'echo' , { term =true } ) 968assert_alive () 969command ( 'bdelete!' ) 970end ) 971 972it ( 'no heap-buffer-overflow when sending long line with nowrap #11548' , function () 973command ( 'set nowrap | autocmd TermOpen * startinsert' ) 974feed ( ':call feedkeys("4000ai\\<esc>:terminal!\\<cr>")<CR>' ) 975assert_alive () 976end ) 977 978it ( 'truncates the size of grapheme clusters' , function () 979local chan =api . nvim_open_term ( 0 , {} ) 980local composing =( 'a̳' ): sub ( 2 ) 981api . nvim_chan_send ( chan , 'a' .. composing : rep ( 20 )) 982retry ( nil , nil , function () 983eq ( 'a' .. composing : rep ( 14 ), api . nvim_get_current_line ()) 984end ) 985end ) 986 987it ( 'handles extended grapheme clusters' , function () 988local screen =Screen . new ( 50 , 7 ) 989feed 'i' 990local chan =api . nvim_open_term ( 0 , {} ) 991api . nvim_chan_send ( chan , '🏴☠️ yarrr' ) 992screen : expect ( [[ 993🏴☠️ yarrr^ | 994|*5 995{5:-- TERMINAL --} | 996]] ) 997eq ( '🏴☠️ yarrr' , api . nvim_get_current_line ()) 998end ) 999 1000it ( 'handles split UTF-8 sequences #16245' , function () 1001local screen =Screen . new ( 50 , 10 ) 1002fn . jobstart ( { testprg ( 'shell-test' ), 'UTF-8' } , { term =true } ) 1003screen : expect ( [[ 1004^å | 1005ref: å̲ | 10061: å̲ | 10072: å̲ | 10083: å̲ | 1009| 1010[Process exited 0] | 1011|*3 1012]] ) 1013-- Test with Unicode char at right edge using a 4-wide terminal 1014command ( 'bwipe! | set laststatus=0 | 4vnew' ) 1015fn . jobstart ( { testprg ( 'shell-test' ), 'UTF-8' } , { term =true } ) 1016screen : expect ( [[ 1017^å │ | 1018ref:│{1:~ }| 1019å̲ │{1:~ }| 10201: å̲│{1:~ }| 10212: å̲│{1:~ }| 10223: å̲│{1:~ }| 1023│{1:~ }| 1024[Pro│{1:~ }| 1025│{1:~ }| 1026| 1027]] ) 1028end ) 1029 1030--- @param subcmd 'REP'|'REPFAST' 1031local function check_term_rep ( subcmd , count , scrollback ) 1032local screen =Screen . new ( 50 , 7 ) 1033api . nvim_set_option_value ( 'scrollback' , scrollback , {} ) 1034api . nvim_create_autocmd ( 'TermClose' , { command ='let g:did_termclose = 1' } ) 1035fn . jobstart ( { testprg ( 'shell-test' ), subcmd , count , 'TEST' } , { term =true } ) 1036retry ( nil , 10000 + count , function () 1037eq ( 1 , api . nvim_get_var ( 'did_termclose' )) 1038end ) 1039feed ( 'i' ) 1040screen : expect (( [[ 1041%d: TEST{MATCH: +}| 1042%d: TEST{MATCH: +}| 1043%d: TEST{MATCH: +}| 1044%d: TEST{MATCH: +}| 1045%d: TEST{MATCH: +}| 1046^[Process exited 0] | 1047{5:-- TERMINAL --} | 1048]] ): format ( count - 5 , count - 4 , count - 3 , count - 2 , count - 1 )) 1049local lines =api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 1050local start =math . max ( count + 1 - scrollback - 6 , 0 ) 1051for i =start , count - 1 do 1052eq (( '%d: TEST' ): format ( i ), lines [ i - start + 1 ]) 1053end 1054eq ( '' , lines [ # lines ]) 1055eq ( count - start + 1 , # lines ) 1056end 1057 1058it ( 'does not drop data when job exits immediately after output #3030' , function () 1059check_term_rep ( 'REPFAST' , 20000 , 30000 ) 1060end ) 1061 1062it ( 'does not drop data when autocommands poll for events #37559' , function () 1063api . nvim_create_autocmd ( 'BufFilePre' , { command ='sleep 50m' , nested =true } ) 1064api . nvim_create_autocmd ( 'BufFilePost' , { command ='sleep 50m' , nested =true } ) 1065api . nvim_create_autocmd ( 'TermOpen' , { command ='sleep 50m' , nested =true } ) 1066-- REP pauses 1 ms every 100 lines, so each autocommand processes some output. 1067check_term_rep ( 'REP' , 20000 , 30000 ) 1068end ) 1069 1070describe ( 'scrollback is correct if all output is drained by' , function () 1071for _ , event in ipairs ( { 'BufFilePre' , 'BufFilePost' , 'TermOpen' } ) do 1072describe (( '%s autocommand that lasts for' ): format ( event ), function () 1073for _ , delay in ipairs ( { 5 , 15 , 25 } ) do 1074-- Terminal refresh delay is 10 ms. 1075it (( '%.1f * terminal refresh delay' ): format ( delay / 10 ), function () 1076local cmd =( 'sleep %dm' ): format ( delay ) 1077api . nvim_create_autocmd ( event , { command =cmd , nested =true } ) 1078check_term_rep ( 'REPFAST' , 200 , 10000 ) 1079end ) 1080end 1081end ) 1082end 1083end ) 1084 1085it ( 'scrollback is correct if buffer update callbacks poll for uv events' , function () 1086-- Use vim.regex:match_str(), which may poll for uv events. 1087exec_lua ( function () 1088local regex =vim . regex ( [[^\d\+: TEST]] ) 1089_G . matched_lines ={} --- @type table<string,boolean> 1090vim . api . nvim_create_autocmd ( 'TermOpen' , { 1091callback =function ( ev ) 1092vim . api . nvim_buf_attach ( ev . buf , false , { 1093on_lines =function ( _ , buf , _ , first , _ , last , _ ) 1094local lines =vim . api . nvim_buf_get_lines ( buf , first , last , true ) 1095for _ , line in ipairs ( lines ) do 1096if regex : match_str ( line ) then 1097_G . matched_lines [ vim . trim ( line )] =true 1098end 1099end 1100end , 1101} ) 1102end , 1103} ) 1104end ) 1105-- Use a 'scrollback' value smaller than the number of printed lines. 1106-- REP pauses 1 ms every 100 lines, so this can take at least 20 refresh cycles. 1107check_term_rep ( 'REP' , 20000 , 10000 ) 1108-- Check that buffer update callbacks have seen all output lines. 1109local matched_lines =exec_lua ( 'return _G.matched_lines' ) 1110for i =0 , 19999 do 1111local line =( '%d: TEST' ): format ( i ) 1112eq ( true , matched_lines [ line ], line ) 1113end 1114end ) 1115 1116it ( 'handles unprintable chars' , function () 1117local screen =Screen . new ( 50 , 7 ) 1118feed 'i' 1119local chan =api . nvim_open_term ( 0 , {} ) 1120api . nvim_chan_send ( chan , '\239\187\191' ) -- '\xef\xbb\xbf' 1121screen : expect ( [[ 1122{18:<feff>}^ | 1123|*5 1124{5:-- TERMINAL --} | 1125]] ) 1126eq ( '\239\187\191' , api . nvim_get_current_line ()) 1127end ) 1128 1129it ( "handles bell respecting 'belloff' and 'visualbell'" , function () 1130local screen =Screen . new ( 50 , 7 ) 1131local chan =api . nvim_open_term ( 0 , {} ) 1132 1133command ( 'set belloff=' ) 1134api . nvim_chan_send ( chan , '\a' ) 1135screen : expect ( function () 1136eq ( { true , false } , { screen . bell , screen . visual_bell } ) 1137end ) 1138screen . bell =false 1139 1140command ( 'set visualbell' ) 1141api . nvim_chan_send ( chan , '\a' ) 1142screen : expect ( function () 1143eq ( { false , true } , { screen . bell , screen . visual_bell } ) 1144end ) 1145screen . visual_bell =false 1146 1147command ( 'set belloff=term' ) 1148api . nvim_chan_send ( chan , '\a' ) 1149screen : expect ( { 1150condition =function () 1151eq ( { false , false } , { screen . bell , screen . visual_bell } ) 1152end , 1153unchanged =true , 1154} ) 1155 1156command ( 'set belloff=all' ) 1157api . nvim_chan_send ( chan , '\a' ) 1158screen : expect ( { 1159condition =function () 1160eq ( { false , false } , { screen . bell , screen . visual_bell } ) 1161end , 1162unchanged =true , 1163} ) 1164end ) 1165 1166it ( 'does not wipeout unrelated buffer after channel closes' , function () 1167local screen =Screen . new ( 50 , 7 ) 1168screen : set_default_attr_ids ( { 1169[ 1 ] ={ foreground =Screen . colors . Blue1 , bold =true } , 1170[ 2 ] ={ reverse =true } , 1171[ 31 ] ={ background =Screen . colors . DarkGreen , foreground =Screen . colors . White , bold =true } , 1172} ) 1173 1174local old_buf =api . nvim_get_current_buf () 1175command ( 'new' ) 1176fn . chanclose ( api . nvim_open_term ( 0 , {} )) 1177local term_buf =api . nvim_get_current_buf () 1178screen : expect ( [[ 1179^ | 1180[Terminal closed] | 1181{31:[Scratch] [-] }| 1182| 1183{1:~ }| 1184{2:[No Name] }| 1185| 1186]] ) 1187 1188-- Autocommand should not result in the wrong buffer being wiped out. 1189command ( 'autocmd TermLeave * ++once wincmd p' ) 1190feed ( 'ii' ) 1191screen : expect ( [[ 1192^ | 1193{1:~ }|*5 1194| 1195]] ) 1196eq ( old_buf , api . nvim_get_current_buf ()) 1197eq ( false , api . nvim_buf_is_valid ( term_buf )) 1198 1199term_buf =api . nvim_get_current_buf () 1200fn . chanclose ( api . nvim_open_term ( term_buf , {} )) 1201screen : expect ( [[ 1202^ | 1203[Terminal closed] | 1204|*5 1205]] ) 1206 1207-- Autocommand should not result in a heap UAF if it frees the terminal prematurely. 1208command ( 'autocmd TermLeave * ++once bwipeout!' ) 1209feed ( 'ii' ) 1210screen : expect ( [[ 1211^ | 1212{1:~ }|*5 1213| 1214]] ) 1215eq ( false , api . nvim_buf_is_valid ( term_buf )) 1216end ) 1217 1218it ( 'no heap-use-after-free from autocmds when entering terminal mode' , function () 1219local buf =api . nvim_get_current_buf () 1220local chan =api . nvim_open_term ( 0 , {} ) 1221command ( 'autocmd TermEnter,ModeChanged * ++once bwipeout!' ) 1222feed ( 'i' ) 1223eq ( false , api . nvim_buf_is_valid ( buf )) 1224eq ( {} , api . nvim_get_chan_info ( chan )) 1225eq ( 'n' , fn . mode ()) 1226 1227-- Remain in Terminal mode if autocmds put us in a different terminal. 1228buf =api . nvim_get_current_buf () 1229chan =api . nvim_open_term ( 0 , {} ) 1230command ( 'autocmd TermEnter,ModeChanged * ++once bwipeout! | let g:chan = nvim_open_term(0, {})' ) 1231feed ( 'i' ) 1232eq ( false , api . nvim_buf_is_valid ( buf )) 1233eq ( {} , api . nvim_get_chan_info ( chan )) 1234eq ( api . nvim_get_current_buf (), api . nvim_get_chan_info ( eval ( 'g:chan' )). buffer ) 1235eq ( 't' , fn . mode ()) 1236end ) 1237 1238local enew_screen =[[ 1239^ | 1240{1:~ }|*5 1241| 1242]] 1243 1244local function test_enew_in_buf_with_running_term ( env ) 1245describe ( 'editing a new file' , function () 1246it ( 'hides terminal buffer ignoring bufhidden=wipe' , function () 1247local old_snapshot =env . screen : get_snapshot () 1248command ( 'setlocal bufhidden=wipe' ) 1249command ( 'enew' ) 1250neq ( env . buf , api . nvim_get_current_buf ()) 1251env . screen : expect ( enew_screen ) 1252feed ( '<C-^>' ) 1253eq ( env . buf , api . nvim_get_current_buf ()) 1254env . screen : expect ( old_snapshot ) 1255end ) 1256end ) 1257end 1258 1259local function test_open_term_in_buf_with_running_term ( env ) 1260describe ( 'does not allow opening another terminal' , function () 1261it ( 'with jobstart() in same buffer' , function () 1262eq ( 1263( 'Vim:Terminal already connected to buffer %d' ): format ( env . buf ), 1264pcall_err ( fn . jobstart , { testprg ( 'tty-test' ) } , { term =true } ) 1265) 1266env . screen : expect_unchanged () 1267end ) 1268 1269it ( 'with nvim_open_term() in same buffer' , function () 1270eq ( 1271( 'Terminal already connected to buffer %d' ): format ( env . buf ), 1272pcall_err ( api . nvim_open_term , env . buf , {} ) 1273) 1274env . screen : expect_unchanged () 1275end ) 1276end ) 1277end 1278 1279describe ( 'with running terminal job' , function () 1280local env ={} 1281 1282before_each ( function () 1283env . screen =Screen . new ( 50 , 7 ) 1284fn . jobstart ( { testprg ( 'tty-test' ) } , { term =true } ) 1285env . screen : expect ( [[ 1286^tty ready | 1287|*6 1288]] ) 1289env . buf =api . nvim_get_current_buf () 1290api . nvim_set_option_value ( 'modified' , false , { buf =env . buf } ) 1291end ) 1292 1293test_enew_in_buf_with_running_term ( env ) 1294test_open_term_in_buf_with_running_term ( env ) 1295end ) 1296 1297describe ( 'with open nvim_open_term() channel' , function () 1298local env ={} 1299 1300before_each ( function () 1301env . screen =Screen . new ( 50 , 7 ) 1302local chan =api . nvim_open_term ( 0 , {} ) 1303api . nvim_chan_send ( chan , 'TEST' ) 1304env . screen : expect ( [[ 1305^TEST | 1306|*6 1307]] ) 1308env . buf =api . nvim_get_current_buf () 1309api . nvim_set_option_value ( 'modified' , false , { buf =env . buf } ) 1310end ) 1311 1312test_enew_in_buf_with_running_term ( env ) 1313test_open_term_in_buf_with_running_term ( env ) 1314end ) 1315 1316local function test_enew_in_buf_with_finished_term ( env ) 1317describe ( 'editing a new file' , function () 1318it ( 'hides terminal buffer with bufhidden=hide' , function () 1319local old_snapshot =env . screen : get_snapshot () 1320command ( 'setlocal bufhidden=hide' ) 1321command ( 'enew' ) 1322neq ( env . buf , api . nvim_get_current_buf ()) 1323env . screen : expect ( enew_screen ) 1324feed ( '<C-^>' ) 1325eq ( env . buf , api . nvim_get_current_buf ()) 1326env . screen : expect ( old_snapshot ) 1327end ) 1328 1329it ( 'wipes terminal buffer with bufhidden=wipe' , function () 1330command ( 'setlocal bufhidden=wipe' ) 1331command ( 'enew' ) 1332neq ( env . buf , api . nvim_get_current_buf ()) 1333eq ( false , api . nvim_buf_is_valid ( env . buf )) 1334env . screen : expect ( enew_screen ) 1335feed ( '<C-^>' ) 1336env . screen : expect ( [[ 1337^ | 1338{1:~ }|*5 1339{9:E23: No alternate file} | 1340]] ) 1341end ) 1342end ) 1343end 1344 1345local function test_open_term_in_buf_with_finished_term ( env ) 1346describe ( 'does not leak memory when opening another terminal' , function () 1347describe ( 'with jobstart() in same buffer' , function () 1348it ( 'in Normal mode' , function () 1349fn . jobstart ( { testprg ( 'tty-test' ) } , { term =true } ) 1350env . screen : expect ( [[ 1351^tty ready | 1352|*6 1353]] ) 1354end ) 1355 1356it ( 'in Terminal mode' , function () 1357feed ( 'i' ) 1358eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 1359fn . jobstart ( { testprg ( 'tty-test' ) } , { term =true } ) 1360env . screen : expect ( [[ 1361tty ready | 1362^ | 1363|*4 1364{5:-- TERMINAL --} | 1365]] ) 1366end ) 1367end ) 1368 1369describe ( 'with nvim_open_term() in same buffer' , function () 1370it ( 'in Normal mode' , function () 1371local chan =api . nvim_open_term ( env . buf , {} ) 1372api . nvim_chan_send ( chan , 'OTHER' ) 1373env . screen : expect ( [[ 1374^OTHER | 1375|*6 1376]] ) 1377end ) 1378 1379it ( 'in Terminal mode' , function () 1380feed ( 'i' ) 1381eq ( { blocking =false , mode ='t' } , api . nvim_get_mode ()) 1382local chan =api . nvim_open_term ( env . buf , {} ) 1383api . nvim_chan_send ( chan , 'OTHER' ) 1384env . screen : expect ( [[ 1385OTHER^ | 1386|*5 1387{5:-- TERMINAL --} | 1388]] ) 1389end ) 1390end ) 1391end ) 1392end 1393 1394describe ( 'with exited terminal job' , function () 1395local env ={} 1396 1397before_each ( function () 1398env . screen =Screen . new ( 50 , 7 ) 1399fn . jobstart ( { testprg ( 'shell-test' ) } , { term =true } ) 1400env . screen : expect ( [[ 1401^ready $ | 1402[Process exited 0] | 1403|*5 1404]] ) 1405env . buf =api . nvim_get_current_buf () 1406api . nvim_set_option_value ( 'modified' , false , { buf =env . buf } ) 1407end ) 1408 1409test_enew_in_buf_with_finished_term ( env ) 1410test_open_term_in_buf_with_finished_term ( env ) 1411end ) 1412 1413describe ( 'with closed nvim_open_term() channel' , function () 1414local env ={} 1415 1416before_each ( function () 1417env . screen =Screen . new ( 50 , 7 ) 1418local chan =api . nvim_open_term ( 0 , {} ) 1419api . nvim_chan_send ( chan , 'TEST' ) 1420fn . chanclose ( chan ) 1421env . screen : expect ( [[ 1422^TEST | 1423[Terminal closed] | 1424|*5 1425]] ) 1426env . buf =api . nvim_get_current_buf () 1427api . nvim_set_option_value ( 'modified' , false , { buf =env . buf } ) 1428end ) 1429 1430test_enew_in_buf_with_finished_term ( env ) 1431test_open_term_in_buf_with_finished_term ( env ) 1432end ) 1433 1434it ( 'with nvim_open_term() channel and only 1 line is not reused by :enew' , function () 1435command ( '1new' ) 1436local oldbuf =api . nvim_get_current_buf () 1437api . nvim_open_term ( oldbuf , {} ) 1438eq ( { mode ='nt' , blocking =false } , api . nvim_get_mode ()) 1439feed ( 'i' ) 1440eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 1441feed ( [[<C-\><C-N>]] ) 1442eq ( { mode ='nt' , blocking =false } , api . nvim_get_mode ()) 1443 1444command ( 'enew' ) 1445neq ( oldbuf , api . nvim_get_current_buf ()) 1446eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 1447feed ( 'i' ) 1448eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 1449feed ( '<Esc>' ) 1450eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 1451 1452command ( 'buffer #' ) 1453eq ( oldbuf , api . nvim_get_current_buf ()) 1454eq ( { mode ='nt' , blocking =false } , api . nvim_get_mode ()) 1455feed ( 'i' ) 1456eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 1457feed ( [[<C-\><C-N>]] ) 1458eq ( { mode ='nt' , blocking =false } , api . nvim_get_mode ()) 1459end ) 1460 1461it ( 'does not allow OptionSet or b:term_title watcher to delete buffer' , function () 1462local chan =exec_lua ( [[ 1463local au = vim.api.nvim_create_autocmd('OptionSet', { command = 'bwipeout!' }) 1464local chan = vim.api.nvim_open_term(0, {}) 1465vim.api.nvim_del_autocmd(au) 1466return chan 1467]] ) 1468matches ( '^E937: ' , api . nvim_get_vvar ( 'errmsg' )) 1469api . nvim_set_vvar ( 'errmsg' , '' ) 1470api . nvim_chan_send ( chan , '\027]2;SOME_TITLE\007' ) 1471eq ( 'SOME_TITLE' , api . nvim_buf_get_var ( 0 , 'term_title' )) 1472command ( [[call dictwatcheradd(b:, 'term_title', {-> execute('bwipe!')})]] ) 1473api . nvim_chan_send ( chan , '\027]2;OTHER_TITLE\007' ) 1474eq ( 'OTHER_TITLE' , api . nvim_buf_get_var ( 0 , 'term_title' )) 1475matches ( '^E937: ' , api . nvim_get_vvar ( 'errmsg' )) 1476end ) 1477 1478it ( 'using NameBuff in BufFilePre does not interfere with buffer rename' , function () 1479local oldbuf =api . nvim_get_current_buf () 1480n . exec ( [[ 1481file Xoldfile 1482new Xotherfile 1483wincmd w 1484let g:BufFilePre_bufs = [] 1485let g:BufFilePost_bufs = [] 1486autocmd BufFilePre * call add(g:BufFilePre_bufs, [bufnr(), bufname()]) 1487autocmd BufFilePost * call add(g:BufFilePost_bufs, [bufnr(), bufname()]) 1488autocmd BufFilePre,BufFilePost * call execute('ls') 1489]] ) 1490fn . jobstart ( { testprg ( 'shell-test' ) } , { term =true } ) 1491eq ( { { oldbuf , 'Xoldfile' } } , api . nvim_get_var ( 'BufFilePre_bufs' )) 1492local buffilepost_bufs =api . nvim_get_var ( 'BufFilePost_bufs' ) 1493eq ( 1 , # buffilepost_bufs ) 1494eq ( oldbuf , buffilepost_bufs [ 1 ][ 1 ]) 1495matches ( '^term://' , buffilepost_bufs [ 1 ][ 2 ]) 1496end ) 1497 1498it ( 'double width char does not crash 1-wide terminal #41856' , function () 1499command ( 'botright vnew' ) 1500local screen =Screen . new ( 50 , 7 ) 1501local chan =api . nvim_open_term ( 0 , {} ) 1502screen : try_resize ( 25 , 7 ) 1503api . nvim_chan_send ( chan , 'キ' ) 1504assert_alive () 1505end ) 1506end ) 1507 1508describe ( 'on_lines does not emit out-of-bounds line indexes when' , function () 1509before_each ( function () 1510clear () 1511exec_lua ( [[ 1512function _G.register_callback(bufnr) 1513_G.cb_error = '' 1514vim.api.nvim_buf_attach(bufnr, false, { 1515on_lines = function(_, bufnr, _, firstline, _, _) 1516local status, msg = pcall(vim.api.nvim_buf_get_offset, bufnr, firstline) 1517if not status then 1518_G.cb_error = msg 1519end 1520end 1521}) 1522end 1523]] ) 1524end ) 1525 1526it ( 'creating a terminal buffer #16394' , function () 1527command ( 'autocmd TermOpen * ++once call v:lua.register_callback(str2nr(expand("<abuf>")))' ) 1528command ( 'terminal' ) 1529sleep ( 500 ) 1530eq ( '' , exec_lua ( [[return _G.cb_error]] )) 1531end ) 1532 1533it ( 'deleting a terminal buffer #16394' , function () 1534command ( 'terminal' ) 1535sleep ( 500 ) 1536local cb_error =exec_lua ( [[ 1537_G.register_callback(0) 1538vim.cmd('bdelete!') 1539return _G.cb_error 1540]] ) 1541eq ( '' , cb_error ) 1542end ) 1543end ) 1544 1545describe ( 'terminal input' , function () 1546local chan --- @type integer 1547 1548before_each ( function () 1549clear () 1550chan =exec_lua ( function () 1551_G . input_data ='' 1552return vim . api . nvim_open_term ( 0 , { 1553on_input =function ( _ , _ , _ , data ) 1554_G . input_data =_G . input_data .. data 1555end , 1556} ) 1557end ) 1558feed ( 'i' ) 1559poke_eventloop () 1560end ) 1561 1562it ( '<C-Space> is sent as NUL byte' , function () 1563feed ( 'aaa<C-Space>bbb' ) 1564eq ( 'aaa\0bbb' , exec_lua ( [[return _G.input_data]] )) 1565end ) 1566 1567it ( 'unknown special keys are not sent' , function () 1568feed ( 'aaa<Help>bbb' ) 1569eq ( 'aaabbb' , exec_lua ( [[return _G.input_data]] )) 1570end ) 1571 1572it ( '<Ignore> is no-op' , function () 1573feed ( 'aaa<Ignore>bbb' ) 1574eq ( 'aaabbb' , exec_lua ( [[return _G.input_data]] )) 1575eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 1576feed ( [[<C-\><Ignore><C-N>]] ) 1577eq ( { mode ='nt' , blocking =false } , api . nvim_get_mode ()) 1578feed ( 'v' ) 1579eq ( { mode ='v' , blocking =false } , api . nvim_get_mode ()) 1580feed ( '<Esc>' ) 1581eq ( { mode ='nt' , blocking =false } , api . nvim_get_mode ()) 1582feed ( 'i' ) 1583eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 1584feed ( [[<C-\><Ignore><C-O>]] ) 1585eq ( { mode ='ntT' , blocking =false } , api . nvim_get_mode ()) 1586feed ( 'v' ) 1587eq ( { mode ='v' , blocking =false } , api . nvim_get_mode ()) 1588feed ( '<Esc>' ) 1589eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 1590fn . chanclose ( chan ) 1591feed ( '<MouseMove>' ) 1592eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 1593feed ( '<Ignore>' ) 1594eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 1595eq ( 'terminal' , api . nvim_get_option_value ( 'buftype' , { buf =0 } )) 1596feed ( '<Space>' ) 1597eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 1598eq ( '' , api . nvim_get_option_value ( 'buftype' , { buf =0 } )) 1599end ) 1600end ) 1601 1602describe ( 'terminal input' , function () 1603it ( 'sends various special keys with modifiers' , function () 1604clear () 1605local screen =tt . setup_child_nvim ( { 1606'-u' , 1607'NONE' , 1608'-i' , 1609'NONE' , 1610'--cmd' , 1611'colorscheme vim' , 1612'--cmd' , 1613'set notermguicolors' , 1614'-c' , 1615'while 1 | redraw | echo keytrans(getcharstr(-1, #{simplify: 0})) | endwhile' , 1616} ) 1617screen : expect ( [[ 1618^ | 1619{100:~ }|*3 1620{3:[No Name] 0,0-1 All}| 1621| 1622{5:-- TERMINAL --} | 1623]] ) 1624local keys ={ 1625'<Tab>' , 1626'<CR>' , 1627'<Esc>' , 1628'<M-Tab>' , 1629'<M-CR>' , 1630'<M-Esc>' , 1631'<BS>' , 1632'<S-Tab>' , 1633'<Insert>' , 1634'<S-Insert>' , 1635'<Del>' , 1636'<S-Del>' , 1637'<PageUp>' , 1638'<PageDown>' , 1639'<S-Up>' , 1640'<C-Up>' , 1641'<Up>' , 1642'<S-Down>' , 1643'<C-Down>' , 1644'<Down>' , 1645'<S-Left>' , 1646'<C-Left>' , 1647'<Left>' , 1648'<S-Right>' , 1649'<C-Right>' , 1650'<Right>' , 1651'<S-Home>' , 1652'<C-Home>' , 1653'<Home>' , 1654'<S-End>' , 1655'<C-End>' , 1656'<End>' , 1657'<C-LeftMouse><0,0>' , 1658'<C-LeftDrag><0,1>' , 1659'<C-LeftRelease><0,1>' , 1660'<LeftMouse><0,1>' , 1661'<LeftRelease><0,1>' , 1662'<2-LeftMouse><0,1>' , 1663'<2-LeftDrag><0,0>' , 1664'<2-LeftRelease><0,0>' , 1665'<M-MiddleMouse><0,0>' , 1666'<M-MiddleDrag><0,1>' , 1667'<M-MiddleRelease><0,1>' , 1668'<MiddleMouse><0,1>' , 1669'<MiddleRelease><0,1>' , 1670'<2-MiddleMouse><0,1>' , 1671'<2-MiddleDrag><0,0>' , 1672'<2-MiddleRelease><0,0>' , 1673'<S-RightMouse><0,0>' , 1674'<S-RightDrag><0,1>' , 1675'<S-RightRelease><0,1>' , 1676'<RightMouse><0,1>' , 1677'<RightRelease><0,1>' , 1678'<2-RightMouse><0,1>' , 1679'<2-RightDrag><0,0>' , 1680'<2-RightRelease><0,0>' , 1681'<S-X1Mouse><0,0>' , 1682'<S-X1Drag><0,1>' , 1683'<S-X1Release><0,1>' , 1684'<X1Mouse><0,1>' , 1685'<X1Release><0,1>' , 1686'<2-X1Mouse><0,1>' , 1687'<2-X1Drag><0,0>' , 1688'<2-X1Release><0,0>' , 1689'<S-X2Mouse><0,0>' , 1690'<S-X2Drag><0,1>' , 1691'<S-X2Release><0,1>' , 1692'<X2Mouse><0,1>' , 1693'<X2Release><0,1>' , 1694'<2-X2Mouse><0,1>' , 1695'<2-X2Drag><0,0>' , 1696'<2-X2Release><0,0>' , 1697'<S-ScrollWheelUp>' , 1698'<S-ScrollWheelDown>' , 1699'<ScrollWheelUp>' , 1700'<ScrollWheelDown>' , 1701'<S-ScrollWheelLeft>' , 1702'<S-ScrollWheelRight>' , 1703'<ScrollWheelLeft>' , 1704'<ScrollWheelRight>' , 1705} 1706-- FIXME: The escape sequence to enable kitty keyboard mode doesn't work on Windows 1707if not is_os ( 'win' ) then 1708table . insert ( keys , '<S-BS>' ) 1709table . insert ( keys , '<C-I>' ) 1710table . insert ( keys , '<C-M>' ) 1711table . insert ( keys , '<C-[>' ) 1712end 1713for _ , key in ipairs ( keys ) do 1714feed ( key ) 1715screen : expect (( [[ 1716| 1717{100:~ }|*3 1718{3:[No Name] 0,0-1 All}| 1719%s^ {MATCH: *}| 1720{5:-- TERMINAL --} | 1721]] ): format ( key : gsub ( '<%d+,%d+>$' , '' ))) 1722end 1723end ) 1724end ) 1725 1726if is_os ( 'win' ) then 1727describe ( ':terminal in Windows' , function () 1728local screen 1729 1730before_each ( function () 1731clear () 1732command ( 'set modifiable swapfile undolevels=20' ) 1733poke_eventloop () 1734local cmd ={ 'cmd.exe' , '/K' , 'PROMPT=$g$s' } 1735screen =tt . setup_screen ( nil , cmd ) 1736end ) 1737 1738it ( '"put" operator sends data normally' , function () 1739feed ( '<c-\\><c-n>G' ) 1740local s =':: tty ready' 1741fn . setreg ( 'a' , s .. '\n:: appended ' .. s .. '\n\n' ) 1742feed ( '"ap"ap' ) 1743screen : expect ( [[ 1744| 1745> :: tty ready | 1746> :: appended :: tty ready | 1747> :: tty ready | 1748> :: appended :: tty ready | 1749^> | 1750| 1751]] ) 1752-- operator count is also taken into consideration 1753feed ( '3"ap' ) 1754screen : expect ( [[ 1755> :: appended :: tty ready | 1756> :: tty ready | 1757> :: appended :: tty ready | 1758> :: tty ready | 1759> :: appended :: tty ready | 1760^> | 1761| 1762]] ) 1763end ) 1764 1765it ( '":put" command sends data normally' , function () 1766feed ( '<c-\\><c-n>G' ) 1767local s =':: tty ready' 1768fn . setreg ( 'a' , s .. '\n:: appended ' .. s .. '\n\n' ) 1769feed ( ':put a<CR>' ) 1770screen : expect ( [[ 1771| 1772> :: tty ready | 1773> :: appended :: tty ready | 1774> | 1775| 1776^ | 1777:put a | 1778]] ) 1779-- line argument is only used to move the cursor 1780feed ( ':6put a<CR>' ) 1781screen : expect ( [[ 1782| 1783> :: tty ready | 1784> :: appended :: tty ready | 1785> :: tty ready | 1786> :: appended :: tty ready | 1787^> | 1788:6put a | 1789]] ) 1790end ) 1791end ) 1792end 1793 1794describe ( 'termopen() (deprecated alias to `jobstart(…,{term=true})`)' , function () 1795before_each ( clear ) 1796 1797it ( 'disallowed when textlocked' , function () 1798command ( "autocmd TextYankPost <buffer> ++once call termopen('foo')" ) 1799matches ( 1800'Vim%(call%):E565: Not allowed to change text or change window$' , 1801pcall_err ( command , 'normal! yy' ) 1802) 1803 1804feed ( 'q:' ) 1805eq ( 'Vim:jobstart(...,{term=true}) requires unmodified buffer' , pcall_err ( fn . termopen , 'bar' )) 1806end ) 1807end ) 1808 1809describe ( 'jobstart(…,{term=true})' , function () 1810before_each ( clear ) 1811 1812describe ( '$COLORTERM value' , function () 1813before_each ( function () 1814-- Outer value should never be propagated to :terminal 1815fn . setenv ( 'COLORTERM' , 'wrongvalue' ) 1816end ) 1817 1818local function test_term_colorterm ( expected , opts ) 1819local screen =Screen . new ( 50 , 4 ) 1820fn . jobstart ( { 1821nvim_prog , 1822'-u' , 1823'NONE' , 1824'-i' , 1825'NONE' , 1826'--headless' , 1827'-c' , 1828'echo $COLORTERM | quit' , 1829} , vim . tbl_extend ( 'error' , opts , { term =true } )) 1830screen : expect (( [[ 1831^%s{MATCH:%%s+}| 1832[Process exited 0] | 1833|*2 1834]] ): format ( expected )) 1835end 1836 1837describe ( "with 'notermguicolors'" , function () 1838before_each ( function () 1839command ( 'set notermguicolors' ) 1840end ) 1841it ( 'is empty by default' , function () 1842test_term_colorterm ( '' , {} ) 1843end ) 1844it ( 'can be overridden' , function () 1845test_term_colorterm ( 'expectedvalue' , { env ={ COLORTERM ='expectedvalue' } } ) 1846end ) 1847end ) 1848 1849describe ( "with 'termguicolors'" , function () 1850before_each ( function () 1851command ( 'set termguicolors' ) 1852end ) 1853it ( 'is "truecolor" by default' , function () 1854test_term_colorterm ( 'truecolor' , {} ) 1855end ) 1856it ( 'can be overridden' , function () 1857test_term_colorterm ( 'expectedvalue' , { env ={ COLORTERM ='expectedvalue' } } ) 1858end ) 1859end ) 1860end ) 1861end )