robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Multicursor tests. 2 3local t =require ( 'test.testutil' ) 4local n =require ( 'test.functional.testnvim' )() 5local Screen =require ( 'test.functional.ui.screen' ) 6local t_atom =require ( 'test.functional.editor.atom_testutil' ) 7 8local describe , it , before_each =t . describe , t . it , t . before_each 9local pending =t . pending 10local clear =n . clear 11local command =n . command 12local feed =n . feed 13local fn =n . fn 14local eq =t . eq 15local api =n . api 16local get_lines =t_atom . get_lines 17local k =t_atom . k 18local atoms_start =t_atom . atoms_start 19local atoms =t_atom . atoms 20local atoms_tail =t_atom . atoms_tail 21local atom_last =t_atom . atom_last 22local subatoms =t_atom . subatoms 23 24--- Clears the buffer mcursors like the default CTRL-L mapping (test-harness "mapclear" removed it). 25local function clear_cursors () 26n . exec_lua ( 27[[vim.api.nvim_buf_clear_namespace(0, vim.api.nvim_create_namespace('nvim.multicursor'), 0, -1)]] 28) 29end 30 31--- Number of multicursors. 32local function ncursors () 33local ns =api . nvim_create_namespace ( 'nvim.multicursor' ) 34return # api . nvim_buf_get_extmarks ( 0 , ns , 0 , - 1 , {} ) 35end 36 37--- Positions ({row, col}, 0-based, in position order) of the mcursors. 38local function anchors () 39local ns =api . nvim_create_namespace ( 'nvim.multicursor' ) 40local positions ={} 41for _ , m in ipairs ( api . nvim_buf_get_extmarks ( 0 , ns , 0 , - 1 , {} )) do 42positions [ # positions + 1 ] ={ m [ 2 ], m [ 3 ] } 43end 44return positions 45end 46 47--- Adds a cursor at `pos` in `buf`, via extmark API. 48local function add_cursor ( buf , pos ) 49local ns =api . nvim_create_namespace ( 'nvim.multicursor' ) 50api . nvim_buf_set_extmark ( buf , ns , pos [ 1 ] - 1 , pos [ 2 ], {} ) 51end 52 53--- Sets the buffer lines, then places cursors by "Q". 54local function cursors ( lines , place ) 55api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 56feed ( 'gg0' ) 57feed ( place or 'QjQj' ) 58end 59 60--- Asserts table-driven cascade rows. 61--- - `row.place` (default: "gg0") places the primary. 62--- - `row.keys` input (the "Q" placements and the operation), then compare the whole buffer. 63--- - `row.after` optionally feeds a key after the operation. 64--- - `row.pre` runs a :command first (such rows go last: the option persists). 65local function assert_rows ( rows ) 66for _ , row in ipairs ( rows ) do 67clear_cursors () 68if row . pre then 69command ( row . pre ) 70end 71api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , row . lines ) 72feed ( row . place or 'gg0' ) 73feed ( row . keys ) 74eq ( row . expect , get_lines (), row . keys ) 75if row . after then 76feed ( row . after ) 77eq ( row . after_expect , get_lines (), ( '%s ; %s' ): format ( row . keys , row . after )) 78end 79end 80end 81 82describe ( 'multicursor' , function () 83before_each ( function () 84clear () 85command ( 'hi MCursor guifg=Black guibg=LightGrey' ) 86end ) 87 88describe ( 'Q' , function () 89it ( 'creates a cursor again after clearing all' , function () 90cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'QjQ' ) 91clear_cursors () 92clear_cursors () -- Repeated clear (nothing to remove) is a no-op. 93eq ( 0 , ncursors ()) 94eq ( false , n . exec_lua ( "return require('vim._core.mcursor').active()" )) 95feed ( 'Q' ) 96eq ( 1 , ncursors ()) 97eq ( true , n . exec_lua ( "return require('vim._core.mcursor').active()" )) 98feed ( 'gg0x' ) 99eq ( { 'aa' , 'bb' , 'ccc' } , get_lines ()) 100end ) 101 102it ( 'entering a buffer with mcursors via a nav mapping keeps them' , function () 103-- A navigation mapping ("nnoremap <C-l> <C-w>l") ending in another buffer must not count as 104-- "the mapping edited the buffer". 105command ( 'nnoremap <F5> <C-w>p' ) 106fn . setline ( 1 , { 'aaa' , 'bbb' } ) 107feed ( 'gg0Q' ) -- Cursor in buf1, at the primary position. 108eq ( 1 , ncursors ()) 109command ( 'set hidden' ) 110command ( 'vsplit | enew' ) 111fn . setline ( 1 , { 'xxx' , 'yyy' } ) 112feed ( 'gg0Q' ) -- Cursor in buf2. 113eq ( 1 , ncursors ()) 114feed ( '<F5>' ) -- Mapped switch to win1/buf1. 115eq ( 1 , ncursors ()) -- buf1 cursor still exists 116feed ( '<F5>' ) -- and back 117eq ( 1 , ncursors ()) -- buf2 cursor also 118end ) 119 120it ( '"qQ" is macro-recording, not cursor-add' , function () 121-- "q" is the recording command; a stray "q" before "Q" starts recording into reg Q. 122feed ( 'qQ' ) 123eq ( 'Q' , fn . reg_recording ()) 124eq ( 0 , ncursors ()) 125feed ( 'q' ) -- stop recording: "Q" creates cursors again 126eq ( '' , fn . reg_recording ()) 127feed ( 'Q' ) 128eq ( 1 , ncursors ()) 129end ) 130 131it ( 'gQ restores the cleared cursors (like gv)' , function () 132cursors ( { 'aaa' , 'bbb' , 'ccc' , 'ddd' } , 'QjQ' ) 133eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 134eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 135clear_cursors () 136eq ( 0 , ncursors ()) 137feed ( 'G$' ) -- Move the primary away. 138feed ( 'gQ' ) 139eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 140eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) -- Primary cursor position is restored too. 141feed ( 'Gx' ) -- The restored cursors cascade. 142eq ( { 'aa' , 'bb' , 'ccc' , 'dd' } , get_lines ()) 143-- The snapshot extmarks are shifted by intervening edits. 144clear_cursors () 145feed ( 'ggO<Esc>' ) -- New line on top shifts the snapshot down. 146feed ( 'gQ' ) 147eq ( { { 1 , 0 } , { 2 , 0 } } , anchors ()) 148-- No-op if multicursor is already active. 149feed ( 'gQ' ) 150eq ( 'gQ: multicursor session is active' , n . exec_capture ( '1messages' )) 151end ) 152 153it ( ':g//normal! Q places a cursor at each match' , function () 154fn . setline ( 1 , { 'foo a' , 'bar b' , 'foo c' , 'baz d' , 'foo e' } ) 155command ( 'g/foo/normal! Q' ) 156eq ( 3 , ncursors ()) 157feed ( 'A!<Esc>' ) -- edit applies once per line (primary ends on the last match) 158eq ( { 'foo a!' , 'bar b' , 'foo c!' , 'baz d' , 'foo e!' } , get_lines ()) 159end ) 160 161it ( 'Q in operator-pending mode aborts the operator' , function () 162-- nv_Q: checkclearop() clears the pending operator, no cursor. 163fn . setline ( 1 , { 'one two' } ) 164feed ( 'gg0' ) 165feed ( 'dQ' ) 166eq ( { 'one two' } , get_lines ()) 167eq ( 0 , ncursors ()) 168feed ( 'wx' ) -- "w" moves (the "d" is gone), then x deletes one char 169eq ( { 'one wo' } , get_lines ()) 170end ) 171 172it ( 'Q then edit applies once; primary-overlapping cursor stays synced' , function () 173fn . setline ( 1 , { 'ab' , 'cd' } ) 174feed ( 'Q' ) 175eq ( 1 , ncursors ()) 176feed ( 'x' ) -- The primary's own edit covers the cursor under it, applied only once. 177eq ( { 'b' , 'cd' } , get_lines ()) 178eq ( { { 0 , 0 } } , anchors ()) 179feed ( 'jx' ) -- Move with follow=OFF, the cursor is no longer "primary-overlapping". 180eq ( { '' , 'd' } , get_lines ()) 181 182-- Insert-session ESC moves the primary; the primary-overlapping cursor stays synced. 183for _ , keys in ipairs ( { 'i<Esc>' , 'a<Esc>' , 'aX<Esc>' , 'A<Esc>' , 'cwX<Esc>' , 'oX<Esc>' } ) do 184clear_cursors () 185cursors ( { 'abcd' , 'efgh' } , 'lQjQ' ) 186feed ( keys ) 187local cur =api . nvim_win_get_cursor ( 0 ) 188eq ( { cur [ 1 ] - 1 , cur [ 2 ] } , anchors ()[ 2 ], keys ) 189end 190end ) 191 192it ( 'Q on an existing cursor removes it (toggle)' , function () 193fn . setline ( 1 , { 'aaa' , 'bbb' } ) 194feed ( 'Q' ) 195eq ( 1 , ncursors ()) 196feed ( 'Q' ) -- toggle off: multicursor mode ends 197eq ( 0 , ncursors ()) 198feed ( 'QjQk' ) 199eq ( 2 , ncursors ()) 200feed ( 'Q' ) -- removes only the cursor under the primary 201eq ( 1 , ncursors ()) 202feed ( 'x' ) -- the remaining cursor still cascades 203eq ( { 'aa' , 'bb' } , get_lines ()) 204end ) 205 206it ( 'Q not allowed in a macro (recording or executing)' , function () 207-- |mcursor-limitations|: Q beeps (no cursor) while recording or executing a macro. 208fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' } ) 209feed ( 'gg0' ) 210feed ( 'qq' ) 211feed ( 'Q' ) -- Recording a macro: not allowed (but still recorded). 212feed ( 'zqj' ) -- Likewise "zq". 213feed ( 'q' ) 214eq ( 0 , ncursors ()) 215feed ( '@q' ) -- executing the recorded "Q": not allowed either 216eq ( 0 , ncursors ()) 217feed ( 'Q' ) -- outside a macro: works 218eq ( 1 , ncursors ()) 219end ) 220 221it ( '1Q adds, 2Q removes (no toggle); "zq" adds to existing cursors' , function () 222fn . setline ( 1 , { 'foo bar foo' } ) 223feed ( 'gg0fbQ' ) -- A cursor on "bar". 224feed ( '0*' ) -- Sets the last search pattern; the cursor lands on the second "foo". 225feed ( 'zqn' ) -- Place a cursor at each "foo". 226-- Also under the primary: "zq" does not cascade, so its dedupe does not drop that cursor. 227eq ( { { 0 , 0 } , { 0 , 4 } , { 0 , 8 } } , anchors ()) 228feed ( '0fb' ) 229feed ( '1Q1Q' ) -- No-op (idempotent). 230eq ( 3 , ncursors ()) 231feed ( '0' ) 232feed ( '2Q2Q' ) -- Removes the first "foo" cursor, then no-op. 233eq ( 2 , ncursors ()) 234feed ( '1Q' ) 235eq ( 3 , ncursors ()) 236feed ( '$' ) -- The primary edits a fourth position. 237feed ( 'x' ) 238eq ( { 'oo ar o' } , get_lines ()) 239end ) 240end ) 241 242describe ( '{Visual}Q' , function () 243it ( 'places cursor on each selected line' , function () 244fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' } ) 245feed ( 'ggvjQ' ) -- selection spans lines 1-2, cursor ends on line 2 246eq ( 'n' , fn . mode ()) -- Visual mode ended 247eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) -- primary: unmoved, where the selection ended 248eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) -- One per selected line, including under the primary. 249 250-- Cursors align by screen (not byte) column. A multibyte char before the cursor must not 251-- shift other cursors. 252clear_cursors () 253api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'é123' , 'abcdef' } ) 254feed ( 'gg0llvjQ' ) -- Visual from "2" (line 1) down; cursor ends on "c" (screen column 3) 255eq ( { 2 , 2 } , api . nvim_win_get_cursor ( 0 )) -- primary: unmoved, on "c" (screen column 3) 256feed ( 'x' ) 257eq ( { 'é13' , 'abdef' } , get_lines ()) 258 259-- Each line, also where the cursor column is outside the selection: before '< (charwise), or 260-- past a short line (blockwise). 261clear_cursors () 262api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'hello world' , 'foo' , '' } ) 263feed ( 'gg0wv}Q' ) -- From "world" to the blank line: the cursor column is 0. 264eq ( { { 0 , 0 } , { 1 , 0 } , { 2 , 0 } } , anchors ()) 265clear_cursors () 266api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abc' , 'd' , 'efg' } ) 267feed ( 'gg0ll<C-v>jjQ' ) 268eq ( { { 0 , 2 } , { 1 , 0 } , { 2 , 2 } } , anchors ()) 269end ) 270 271it ( 'V{motion}Q keeps primary at selection-end; "$": each line end' , function () 272fn . setline ( 1 , { 'aaaa' , 'cc' , 'dddd' , 'eeee' } ) 273feed ( 'gg0ll' ) -- screen column 3 on line 1 274feed ( 'Vjj' ) -- linewise down to line 3; the cursor ends on line 3 275feed ( 'Q' ) 276eq ( { 3 , 2 } , api . nvim_win_get_cursor ( 0 )) -- Primary is unmoved (line 3). 277-- One cursor per selected line at the shared screen column; a short line at its end ("j"). 278feed ( 'iX<Esc>' ) 279eq ( { 'aaXaa' , 'cXc' , 'ddXdd' , 'eeee' } , get_lines ()) 280-- {Visual}Q enabled follow-mode (q=). 281feed ( '$' ) 282eq ( { { 0 , 4 } , { 1 , 2 } , { 2 , 4 } } , anchors ()) 283-- After "$" (curswant): a cursor at each line end. Also on the last buffer line. 284clear_cursors () 285api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaaa' , 'cc' , 'dddd' } ) 286feed ( 'ggVG$Q' ) 287eq ( { { 0 , 3 } , { 1 , 1 } , { 2 , 3 } } , anchors ()) 288end ) 289 290it ( 'after an <expr> mapping does not replay the mapping keys #41857' , function () 291command ( 'xnoremap <expr> is "ip"' ) -- selects the inner paragraph 292api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'a1' , 'a2' , 'a3' , '' , 'b1' , 'b2' } ) 293feed ( 'gg0j' ) -- line 2, inside the first paragraph 294feed ( 'vis' ) -- Visual + the <expr> mapping: selects lines 1-3 295feed ( 'Q' ) -- a cursor on each selected line 296eq ( 3 , ncursors ()) 297feed ( 'iX<Esc>' ) -- the mapping keys ("s", "Q") must NOT be replayed 298eq ( { 'Xa1' , 'Xa2' , 'Xa3' , '' , 'b1' , 'b2' } , get_lines ()) 299end ) 300end ) 301 302describe ( 'zq' , function () 303it ( 'zq{search} places a cursor at each match' , function () 304fn . setline ( 1 , { 'foo bar foo' , 'baz foo qux' , 'foobar foo' } ) 305feed ( 'gg0' ) -- on the first "foo" 306feed ( '*' ) -- whole-word pattern; the cursor moves to the next match 307eq ( 1 , api . nvim_get_vvar ( 'hlsearch' )) 308feed ( 'zqn' ) 309eq ( 0 , api . nvim_get_vvar ( 'hlsearch' )) -- ":nohlsearch", workaround for #41629. 310-- 4 whole-word "foo" matches ("foobar" excluded), including the primary's. 311eq ( 4 , ncursors ()) 312-- The primary cursor does not move ("*" left it on the second match). 313eq ( { 1 , 8 } , api . nvim_win_get_cursor ( 0 )) 314feed ( 'cwXXX<Esc>' ) 315eq ( { 'XXX bar XXX' , 'baz XXX qux' , 'foobar XXX' } , get_lines ()) 316-- "zqgn" is the same; so is a backward search ("zq#"). 317clear_cursors () 318feed ( 'uzqgn' ) 319eq ( 4 , ncursors ()) 320clear_cursors () 321feed ( 'zq#' ) 322eq ( 4 , ncursors ()) 323eq ( 0 , api . nvim_get_vvar ( 'searchforward' )) -- The motion's direction is preserved. 324 325-- A "/" search likewise, also with several matches per line. 326clear_cursors () 327api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'ab ab ab' , 'xx ab' } ) 328feed ( 'gg0/ab<CR>' ) -- the cursor lands on the second "ab" 329feed ( 'zqn' ) 330eq ( 4 , ncursors ()) 331feed ( 'x' ) 332eq ( { 'b b b' , 'xx b' } , get_lines ()) 333 334-- In mappings: an operator alias ("gz"); an edit after "zq" applies at every cursor it placed. 335clear_cursors () 336n . exec_lua ( function () 337vim . keymap . set ( 'n' , 'gz' , function () 338return 'zq' 339end , { expr =true } ) 340end ) 341command ( 'nnoremap <F3> zq*cgnX<Esc>' ) 342api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo bar' , 'foo baz' , 'qux foo' } ) 343feed ( 'gg0gz*' ) 344eq ( 3 , ncursors ()) 345clear_cursors () 346feed ( '<F3>' ) 347eq ( { 'X bar' , 'X baz' , 'qux X' } , get_lines ()) 348 349-- Placement uses the real search engine, so it matches what "n" finds under the current 350-- case options. 'ignorecase': "/foo" matches all three cases. 351clear_cursors () 352command ( 'set ignorecase' ) 353api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'Foo foo FOO' } ) 354feed ( 'gg0/foo<CR>' ) 355feed ( 'zqn' ) 356eq ( 3 , ncursors ()) 357feed ( 'gUiw' ) 358eq ( { 'FOO FOO FOO' } , get_lines ()) 359 360-- 'smartcase': an uppercase letter in the pattern forces case-sensitivity. 361clear_cursors () 362command ( 'set smartcase' ) 363api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'Foo foo Foo' } ) 364feed ( 'gg0/Foo<CR>' ) -- only the two "Foo"s, not "foo" 365feed ( 'zqn' ) 366eq ( 2 , ncursors ()) 367feed ( 'x' ) 368eq ( { 'oo foo oo' } , get_lines ()) 369 370-- 'nowrapscan': from the match at (or after) primary; none if no match. 371clear_cursors () 372command ( 'set nowrapscan' ) 373api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo x' } ) 374fn . setreg ( '/' , 'foo' ) 375feed ( '$zqN' ) -- Backwards. Places at "x". 376eq ( 0 , ncursors ()) 377api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo bar foo' } ) 378feed ( '$hzq#' ) -- Mid-word on the last "foo": at its start. 379eq ( { { 0 , 8 } } , anchors ()) 380eq ( { 1 , 8 } , api . nvim_win_get_cursor ( 0 )) 381end ) 382 383it ( 'zq{motion} {Visual} bounds, per-cursor gv' , function () 384-- Motion: each step, like typing the motion again. The primary stays, and gets a cursor. 385local screen =Screen . new ( 30 , 5 ) 386cursors ( { 'aa bb cc' , 'dd ee' } , '' ) 387feed ( 'zqw' ) 388eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 389screen : expect ( { 390condition =function () 391eq ( 'normal' , screen . mode ) -- Not "operator". 392end , 393} ) 394-- ("w" on the last word stops at its end.) 395eq ( { { 0 , 0 } , { 0 , 3 } , { 0 , 6 } , { 1 , 0 } , { 1 , 3 } , { 1 , 4 } } , anchors ()) 396-- A count applies to the motion ("2zqw" is "zq2w": every 2nd word). 397clear_cursors () 398cursors ( { 'aa bb cc dd ee' } , '' ) 399feed ( '2zqw' ) 400eq ( { { 0 , 0 } , { 0 , 6 } , { 0 , 12 } , { 0 , 13 } } , anchors ()) 401-- Forward motions only: a backward motion has no step after the cursor. 402clear_cursors () 403feed ( '$zqb' ) 404eq ( 0 , ncursors ()) 405-- A step that stays ("ti" before an "i") retries from the next char. 406clear_cursors () 407cursors ( { '#include <limits.h>' } , '' ) 408feed ( 'zqti' ) 409eq ( { { 0 , 0 } , { 0 , 10 } , { 0 , 12 } } , anchors ()) 410-- A search that wraps on its first step (no match after the cursor). 411clear_cursors () 412fn . setline ( 1 , { 'foo x foo y' } ) 413feed ( '/foo<CR>$zqn' ) 414eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) -- Not on a match: moves to the next one, like "n". 415eq ( { { 0 , 0 } , { 0 , 6 } } , anchors ()) 416clear_cursors () 417feed ( 'Gzqj' ) -- "j" fails on the last line. 418eq ( 1 , ncursors ()) 419-- 3-key motion ("g`a"). 420clear_cursors () 421feed ( '0fxmagg0zqg`a' ) 422eq ( { { 0 , 0 } , { 0 , 4 } } , anchors ()) 423-- The steps do not scroll the window. 424clear_cursors () 425api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , fn [ 'repeat' ]( { 'x' } , 100 )) 426feed ( '50Gztzqj' ) 427eq ( { 50 , 50 } , { fn . line ( 'w0' ), fn . line ( '.' ) } ) 428-- The primary is considered a zq "step" only if the motion lands on it. 429clear_cursors () 430cursors ( { 'xx' , 'xbc a da' } , 'jl' ) 431feed ( 'zqfa' ) 432eq ( { 2 , 4 } , api . nvim_win_get_cursor ( 0 )) 433eq ( { { 1 , 4 } , { 1 , 7 } } , anchors ()) 434clear_cursors () 435feed ( 'zqfz' ) -- Fails from the primary (no "z"). 436eq ( { 2 , 4 } , api . nvim_win_get_cursor ( 0 )) 437eq ( 0 , ncursors ()) 438 439-- {Visual}zq{motion} limits to the selection. Primary + existing cursor (1:3) stays. 440clear_cursors () 441cursors ( { 'aa bb' , 'cc dd' , 'ee ff' , 'gg hh' } , 'j' ) 442feed ( 'gg3lQ2G0Vjzqw' ) 443eq ( { 3 , 0 } , api . nvim_win_get_cursor ( 0 )) 444eq ( { { 0 , 3 } , { 1 , 0 } , { 1 , 3 } , { 2 , 0 } , { 2 , 3 } } , anchors ()) 445-- Follow mode ("q="): the motion is the operator's ("operator" atom), not followed. 446clear_cursors () 447cursors ( { 'aa bb' , 'cc dd' , 'ee ff' , 'gg hh' } , '' ) 448feed ( '3lQ2G01q=' ) 449atoms_start () 450feed ( 'zqw' ) 451eq ( { { type ='operator' , keys ='zqw' } } , atoms_tail ( 1 , 'type' , 'keys' )) 452eq ( 453{ { 0 , 3 } , { 1 , 0 } , { 1 , 3 } , { 2 , 0 } , { 2 , 3 } , { 3 , 0 } , { 3 , 3 } , { 3 , 4 } } , 454anchors () 455) 456feed ( '2q=' ) 457-- "*" searches the word at the cursor, not at the selection start. 458clear_cursors () 459cursors ( { 'aa bb' , 'bb aa' , 'aa bb' } , '' ) 460feed ( 'Vjzq*' ) 461eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 462eq ( { { 0 , 3 } , { 1 , 0 } } , anchors ()) 463 464-- Failed motion does not place cursors. 465clear_cursors () 466feed ( 'Vkzq/zz<CR>' ) -- Failed motion: no cursors, the cursor stays. 467eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 468eq ( 0 , ncursors ()) 469feed ( 'Vjzqfx' ) -- Failed from selection start (no "x"). 470eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 471eq ( 0 , ncursors ()) 472 473-- The steps iterate from the selection-start until selection-end. 474-- The steps are internal, not captured/cascaded. 475command ( 'xnoremap j gj' ) 476command ( 'autocmd CmdAtom * let g:atoms = 1' ) 477for motion , want in pairs ( { 478[ '0' ] ={ { 1 , 0 } } , 479[ '^' ] ={ { 1 , 2 } } , 480[ '$' ] ={ { 1 , 6 } } , 481w ={ { 1 , 2 } , { 1 , 5 } , { 2 , 0 } , { 3 , 2 } } , 482j ={ { 1 , 0 } , { 2 , 0 } , { 3 , 0 } } , -- One per line, at the cursor column. 483[ '+' ] ={ { 1 , 2 } , { 2 , 0 } , { 3 , 2 } } , 484} ) do 485clear_cursors () 486cursors ( { 'x' , ' aa bb' , 'cc' , ' dd' , 'y' } , '' ) 487feed ( 'jV2jzq' .. motion ) 488eq ( want , anchors (), motion ) 489-- The primary (4:0) is a "j" step and stays; else it moves to the first step. 490local first =want [ 1 ] 491local cursor =motion == 'j' and { 4 , 0 } or { first [ 1 ] + 1 , first [ 2 ] } 492eq ( cursor , api . nvim_win_get_cursor ( 0 ), motion ) 493end 494 495-- {Visual}zq blockwise: each line, bounded by blockwise columns (or short-line end). 496clear_cursors () 497cursors ( { 'abc' , 'def' , 'g' , '' , 'hij' } , '' ) 498feed ( '<C-v>4jzql' ) 499eq ( { { 0 , 0 } , { 1 , 0 } , { 2 , 0 } , { 3 , 0 } , { 4 , 0 } } , anchors ()) 500clear_cursors () 501cursors ( { 'abc' , 'def' , 'g' , '' , 'hij' } , '' ) 502feed ( 'l<C-v>4jzql' ) 503eq ( { 5 , 1 } , api . nvim_win_get_cursor ( 0 )) 504eq ( { { 0 , 1 } , { 1 , 1 } , { 2 , 1 } , { 3 , 0 } , { 4 , 1 } } , anchors ()) 505feed ( 'A;<Esc>' ) 506eq ( { 'abc;' , 'def;' , 'g;' , ';' , 'hij;' } , get_lines ()) 507feed ( 'u' ) 508 509-- A search motion only stops at a match. 510clear_cursors () 511fn . setreg ( '/' , 'e' ) 512feed ( 'gg0l<C-v>4jzqn' ) 513eq ( { 2 , 1 } , api . nvim_win_get_cursor ( 0 )) 514eq ( { { 1 , 1 } } , anchors ()) 515clear_cursors () 516cursors ( { 'aa bb cc dd' , 'ee ff gg hh' } , '' ) 517feed ( '3l<C-v>j4lzqw' ) 518eq ( { { 0 , 3 } , { 0 , 6 } , { 1 , 3 } , { 1 , 6 } } , anchors ()) 519eq ( { 1 , 3 } , api . nvim_win_get_cursor ( 0 )) -- Not on a step: moved to the first. 520 521-- "gv" selects per-cursor visual areas defined by "zq{search}". 522clear_cursors () 523cursors ( { 'foo x foo' , 'bar foo' } , '' ) 524feed ( 'zq*gvcX<Esc>' ) 525eq ( { 'X x X' , 'bar X' } , get_lines ()) 526-- Also an existing cursor (its '< '> "fo"). 527clear_cursors () 528cursors ( { 'foo bar foo' } , 'Qwvly' ) 529feed ( '/foo<CR>zqngvcX<Esc>' ) 530eq ( { 'X bar X' } , get_lines ()) 531-- A zq step on an existing cursor sets its '< '> too. Matches "foo" (not "fo" from "vly"). 532clear_cursors () 533cursors ( { 'x foo' , 'y foo' , 'z foo' } , '' ) 534feed ( 'Vjzq/foo<CR>' ) 535eq ( { 1 , 2 } , api . nvim_win_get_cursor ( 0 )) 536feed ( 'gvcX<Esc>' ) 537eq ( { 'x X' , 'y X' , 'z foo' } , get_lines ()) 538clear_cursors () 539feed ( '<Esc>' ) 540eq ( { 'V' , 1 , 2 } , { fn . visualmode (), fn . line ( "'<" ), fn . line ( "'>" ) } ) 541 542-- Not supported: textobjects, non-motions ("zqx"), non-repeatable ("label" plugin). 543clear_cursors () 544cursors ( { 'aa bb cc' } , '' ) 545n . exec_lua ( function () 546vim . keymap . set ( 'o' , '<F2>' , function () 547vim . api . nvim_win_set_cursor ( 0 , { 1 , 6 } ) 548end ) 549end ) 550for _ , keys in ipairs ( { 'zqiw' , 'zqx' , 'zq<F2>' } ) do 551feed ( keys ) 552eq ( 0 , ncursors (), keys ) 553eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 ), keys ) 554end 555eq ( { 'aa bb cc' } , get_lines ()) 556end ) 557 558it ( 'does nothing without a previous search (E35)' , function () 559fn . setline ( 1 , { 'foo foo' } ) 560feed ( 'zqn' ) 561eq ( 0 , ncursors ()) 562feed ( 'vlzqgn' ) -- {Visual}zq likewise adds nothing. 563eq ( 0 , ncursors ()) 564end ) 565 566it ( '{Visual}zqgn limits matches to selection (linewise)' , function () 567api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo one' , 'foo foo' , 'foo y foo' , 'foo end' } ) 568feed ( 'gg0/foo<CR>' ) -- pattern; cursor lands on line 2's first "foo" 569feed ( 'Vj' ) -- linewise: lines 2-3 570feed ( 'zqgn' ) 571-- 4 matches within lines 2-3 (line 1/4 "foo" are excluded), including the primary's. 572eq ( 4 , ncursors ()) 573eq ( { 3 , 0 } , api . nvim_win_get_cursor ( 0 )) -- primary stays put (selection end), not moved 574feed ( 'x' ) -- the selection ended on a match, so the primary edits with the others 575eq ( { 'foo one' , 'oo oo' , 'oo y oo' , 'foo end' } , get_lines ()) 576 577-- Charwise selection only matches within its columns. 578clear_cursors () 579api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo foo' , 'bar' , 'foo foo' } ) 580feed ( 'gg0/foo<CR>' ) -- cursor on line 1's second "foo" 581feed ( 'vj' ) -- Charwise from line 1's second "foo" into line 2. 582feed ( 'zqgn' ) 583-- Not line 1's first "foo" (before the selection), nor line 3. 584eq ( { 1 , 4 } , api . nvim_win_get_cursor ( 0 )) 585eq ( { { 0 , 4 } } , anchors ()) 586end ) 587end ) 588 589describe ( 'mouse' , function () 590it ( '<C-LeftMouse> toggles a cursor at the click, without moving the primary' , function () 591command ( 'set mousetime=0' ) -- repeated clicks must not count as double-clicks 592fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' } ) 593feed ( 'gg0' ) 594api . nvim_input_mouse ( 'left' , 'press' , 'C' , 0 , 2 , 0 ) -- ctrl-click on line 3 595api . nvim_input_mouse ( 'left' , 'release' , 'C' , 0 , 2 , 0 ) 596-- The primary did not move; a cursor was added at left-click. 597eq ( 1 , fn . line ( '.' )) 598eq ( { { 2 , 0 } } , anchors ()) 599feed ( 'x' ) -- cascades to the clicked position 600eq ( { 'aa' , 'bbb' , 'cc' } , get_lines ()) 601-- CTRL-click on the existing cursor removes it (toggle). 602api . nvim_input_mouse ( 'left' , 'press' , 'C' , 0 , 2 , 0 ) 603api . nvim_input_mouse ( 'left' , 'release' , 'C' , 0 , 2 , 0 ) 604eq ( 0 , ncursors ()) 605 606-- Toggling another cursor keeps the cursor under the primary. 607feed ( 'Q' ) 608for _ =1 , 2 do 609api . nvim_input_mouse ( 'left' , 'press' , 'C' , 0 , 2 , 0 ) 610api . nvim_input_mouse ( 'left' , 'release' , 'C' , 0 , 2 , 0 ) 611end 612eq ( { { 0 , 0 } } , anchors ()) 613feed ( 'Q' ) 614 615-- CTRL-click keeps "q=" follow-mode (unlike Q). 616feed ( 'ggQj' ) 617feed ( 'q=' ) 618api . nvim_input_mouse ( 'left' , 'press' , 'C' , 0 , 2 , 0 ) 619api . nvim_input_mouse ( 'left' , 'release' , 'C' , 0 , 2 , 0 ) 620feed ( 'l' ) 621eq ( { { 0 , 1 } , { 2 , 1 } } , anchors ()) -- Still following: the motion cascaded. 622feed ( 'q=' ) -- Off. 623-- No-op in Insert mode. 624feed ( 'i' ) 625api . nvim_input_mouse ( 'left' , 'press' , 'C' , 0 , 0 , 1 ) 626api . nvim_input_mouse ( 'left' , 'release' , 'C' , 0 , 0 , 1 ) 627eq ( 2 , ncursors ()) 628eq ( 2 , fn . line ( '.' )) 629eq ( 'i' , fn . mode ()) 630feed ( '<Esc>' ) 631end ) 632 633it ( 'middle-click paste applies once' , function () 634-- Fake clipboard provider: "*" must not touch the real system clipboard. 635clear ( '--cmd' , 'set rtp^=test/functional/fixtures' ) 636fn . setline ( 1 , { 'abc' , 'def' , 'ghi' } ) 637fn . setreg ( '*' , 'NEW' ) -- middle-click pastes the * register 638feed ( 'gg0' ) 639api . nvim_input_mouse ( 'middle' , 'press' , '' , 0 , 2 , 0 ) 640api . nvim_input_mouse ( 'middle' , 'release' , '' , 0 , 2 , 0 ) 641feed ( '<Ignore>' ) 642-- Sanity: middle-click pastes (no cursors yet). 643local _ , pasted =table . concat ( get_lines (), '\n' ): gsub ( 'NEW' , '' ) 644eq ( 1 , pasted ) 645feed ( 'ggQjQ2j' ) -- cursors on lines 1-2, primary below 646api . nvim_input_mouse ( 'middle' , 'press' , '' , 0 , 0 , 0 ) 647api . nvim_input_mouse ( 'middle' , 'release' , '' , 0 , 0 , 0 ) 648feed ( '<Ignore>' ) 649-- One more paste at the click position; no paste at the other cursors. 650_ , pasted =table . concat ( get_lines (), '\n' ): gsub ( 'NEW' , '' ) 651eq ( 2 , pasted ) 652end ) 653end ) 654 655describe ( 'normal-mode cascade' , function () 656it ( "cursor-local marks ('[ '] '. '^)" , function () 657cursors ( { 'aa bb' , 'cc dd' } , 'Qj' ) 658feed ( 'gUiw' ) 659eq ( { 'AA bb' , 'CC dd' } , get_lines ()) 660-- Each cursor's replay set its own change marks, so a mapping reading them acts per cursor. 661command ( 'nnoremap <F2> `[v`]u' ) 662feed ( '<F2>' ) 663eq ( { 'aa bb' , 'cc dd' } , get_lines ()) 664 665-- '. and '^ are per-cursor: a cascaded mapping jumps to their cursor-local positions. 666clear_cursors () 667cursors ( { 'abord1 line' , 'abord2 line' , 'abord3 line' } ) 668command ( [[nnoremap <F4> lx$`.iZ<Esc>]] ) 669feed ( '<F4>' ) 670eq ( { 'aZord1 line' , 'aZord2 line' , 'aZord3 line' } , get_lines ()) 671eq ( { 3 , 1 } , api . nvim_buf_get_mark ( 0 , '.' )) 672eq ( { 3 , 2 } , api . nvim_buf_get_mark ( 0 , '^' )) 673 674-- The primary's marks are shifted by edits from other cursors. 675clear_cursors () 676cursors ( { 'a' , 'b' , 'c' , 'd' , 'e' } , 'Q3j' ) 677feed ( 'dd' ) -- Delete line 1. 678eq ( { 'b' , 'c' , 'e' } , get_lines ()) 679eq ( 3 , fn . getpos ( "'[" )[ 2 ]) 680 681-- A cursor without a change yet has no '., like a new editor, so "`." fails there (E20). 682clear_cursors () 683command ( 'let v:errmsg = ""' ) 684api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abc' , 'def' , 'ghi' , 'jkl' } ) 685feed ( 'Gx' ) -- The primary's change. 686feed ( 'ggQjQjq=`.q=' ) 687eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 688eq ( { 4 , 0 } , api . nvim_win_get_cursor ( 0 )) 689eq ( 'E20: Mark not set' , api . nvim_get_vvar ( 'errmsg' )) 690end ) 691 692it ( 'CTRL-C interrupts the cascade; one "u" undoes the partial edit' , function () 693local nlines =5000 694local lines ={} ---@type string[] 695for i =1 , nlines do 696lines [ i ] ='aaa' 697end 698api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 699api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 700for i =2 , nlines do 701add_cursor ( 0 , { i , 0 } ) 702end 703eq ( nlines - 1 , ncursors ()) 704 705-- Interrupt from in-process on_key handler. The cascade runs too fast for test-runner CTRL-C. 706n . exec_lua ( function () 707_G . keys_seen , _G . cascading_seen =0 , false 708vim . on_key ( function () 709_G . keys_seen =_G . keys_seen + 1 710_G . cascading_seen =_G . cascading_seen or vim . api . nvim__mcursor_cascading () 711if _G . keys_seen == 1000 then 712vim . fn . interrupt () 713end 714end ) 715end ) 716 717feed ( 'x' ) -- Primary edit + cascade over ~5000 cursors... 718n . poke_eventloop () 719eq ( true , n . exec_lua ( 'return _G.cascading_seen' )) 720local edited , unedited =0 , 0 721for _ , l in ipairs ( get_lines ()) do 722if l == 'aa' then 723edited =edited + 1 724elseif l == 'aaa' then 725unedited =unedited + 1 726end 727end 728eq ( nlines , edited + unedited ) -- no line was half-edited 729eq ( true , edited >= 1 ) -- the primary's own edit happened 730eq ( true , unedited >= 1 ) -- >=1 cursor did NOT cascade 731-- The partial cascade is still one undo block. 732feed ( 'u' ) 733eq ( lines , get_lines ()) 734end ) 735 736it ( 'line inserts shift other cursors; u restores them' , function () 737-- "o" inserts a line at each cursor; cursors below must shift. With the primary between the 738-- cursors, its own insert shifts the other cursors' extmarks BEFORE the replays. 739for _ , case in ipairs ( { { 'QjQj' , 6 } , { 'QjjQk' , 4 } } ) do 740local place , primary =case [ 1 ], case [ 2 ] 741clear_cursors () 742cursors ( { 'aaa' , 'bbb' , 'ccc' } , place ) 743feed ( 'oX<Esc>' ) 744eq ( { 'aaa' , 'X' , 'bbb' , 'X' , 'ccc' , 'X' } , get_lines (), place ) 745eq ( primary , fn . line ( '.' ), place ) -- The primary shifted too. 746-- Undo moves every cursor back (cursor extmarks restore on undo). 747feed ( 'u' ) 748eq ( { 'aaa' , 'bbb' , 'ccc' } , get_lines (), place ) 749feed ( 'x' ) 750eq ( { 'aa' , 'bb' , 'cc' } , get_lines (), place ) 751end 752end ) 753 754it ( 'per-cursor on fold contents, not the fold itself' , function () 755-- If the primary cursor operates on a closed fold it applies to the whole fold 756-- (|fold-behavior|); but *replayed* edits/motions ignore folds, and operate on the per-cursor 757-- text within folds (if there happens to be one at the given cursor). 758 759fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' , 'ddd' } ) 760feed ( '3G0Q' ) 761command ( '2,4fold' ) 762eq ( 2 , fn . foldclosed ( 3 )) 763feed ( 'gg0' ) 764feed ( 'x' ) 765eq ( { 'aa' , 'bbb' , 'cc' , 'ddd' } , get_lines ()) 766feed ( 'dd' ) 767 768-- Replayed "dd" deletes only the line (within the fold) at cursor. And the stays closed. 769eq ( { 'bbb' , 'ddd' } , get_lines ()) 770eq ( { 1 , 2 } , { fn . foldclosed ( 1 ), fn . foldclosedend ( 1 ) } ) 771eq ( { { 1 , 0 } } , anchors ()) 772 773-- Insert-cascade entry ("cc") acts on the fold contents, not the fold itself. 774clear_cursors () 775feed ( 'zE' ) -- Eliminate the fold above, it would misdirect "3j" below. 776cursors ( { 'aaa' , 'bbb' , 'ccc' , 'ddd' , 'eee' } , '3jQgg' ) 777command ( '3,5fold' ) 778eq ( 3 , fn . foldclosed ( 4 )) 779feed ( 'gg' ) 780feed ( 'ccX<Esc>' ) 781eq ( { 'X' , 'bbb' , 'ccc' , 'X' , 'eee' } , get_lines ()) 782eq ( { 3 , 5 } , { fn . foldclosed ( 4 ), fn . foldclosedend ( 4 ) } ) 783eq ( { { 3 , 0 } } , anchors ()) 784 785-- Replayed fold command ("zN") must not re-enable fold semantics mid-cascade. 786clear_cursors () 787feed ( 'zE' ) 788command ( 'nnoremap X zN:normal! dd<CR>' ) 789cursors ( { 'aaa' , 'bbb' , 'ccc' , 'ddd' , 'eee' , 'fff' } , '3jQgg' ) 790command ( '3,5fold' ) 791feed ( 'gg' ) 792feed ( 'X' ) -- Performs "dd". 793eq ( { 'bbb' , 'ccc' , 'eee' , 'fff' } , get_lines ()) 794eq ( { 2 , 3 } , { fn . foldclosed ( 2 ), fn . foldclosedend ( 2 ) } ) 795 796-- Primary cursor keeps the usual |fold-behavior|: its "dd" deletes the whole fold. 797-- But the replay does not: it only deletes the line at each cursor. 798clear_cursors () 799feed ( 'zE' ) 800cursors ( { 'aaa' , 'bbb' , 'ccc' , 'ddd' , 'eee' , 'fff' } , '4jQgg' ) 801command ( '2,3fold' ) 802feed ( '2G' ) -- Into the closed fold. 803feed ( 'dd' ) 804eq ( { 'aaa' , 'ddd' , 'fff' } , get_lines ()) 805eq ( { { 2 , 0 } } , anchors ()) 806 807-- Follow-mode motions also ignore closed folds. Replayed "j" steps into the fold. 808clear_cursors () 809feed ( 'zE' ) 810cursors ( { 'aaa' , 'bbb' , 'ccc' , 'ddd' , 'eee' , 'fff' , 'ggg' } , 'Q4j' ) 811command ( '2,4fold' ) 812feed ( 'q=' ) 813feed ( 'jj' ) 814eq ( { { 2 , 0 } } , anchors ()) 815eq ( 7 , fn . line ( '.' )) 816eq ( 2 , fn . foldclosed ( 3 )) 817end ) 818 819it ( 'CTRL-D scrolling does not affect the other cursors' , function () 820local screen =Screen . new ( 30 , 10 ) 821local l ={} 822for i =1 , 50 do 823l [ # l + 1 ] =( 'line%d' ): format ( i ) 824end 825cursors ( l ) 826-- Scrolling is viewport-dependent, not repeatable. Other cursors do not move, even in 827-- follow-mode. 828feed ( '<C-d>' ) 829eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 830feed ( 'q=' ) 831feed ( '<C-d>' ) 832feed ( 'q=' ) 833eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 834feed ( '<C-u>' ) 835-- An edit still cascades to the off-screen cursors, and the viewport stays anchored 836-- (replays scroll the window; the cascade restores it). 837feed ( 'x' ) 838eq ( { 'ine1' , 'ine2' } , { get_lines ()[ 1 ], get_lines ()[ 2 ] } ) 839screen : expect ( [[ 840line5 | 841line6 | 842^ine7 | 843line8 | 844line9 | 845line10 | 846line11 | 847line12 | 848line13 | 849| 850]] ) 851end ) 852end ) 853 854describe ( 'composite/mapping' , function () 855it ( 'Visual-mode mapping that creates cursors (Q) #41694' , function () 856command ( 'xmap I Q0i' ) 857 858-- The mapping itself creates the cursors; until then, nothing consumes atoms. 859cursors ( { 'test' , 'nvim' , '' , 'test' , 'nvim' } , '' ) 860feed ( 'gg$jjjVjI' ) 861feed ( 'bad<Esc>' ) 862eq ( { 'test' , 'nvim' , '' , 'badtest' , 'badnvim' } , get_lines ()) 863 864-- Existing cursors, and a CmdAtom consumer: 865clear_cursors () 866cursors ( { 'test' , 'nvim' , '' , 'test' , 'nvim' } , '' ) 867feed ( 'gg$Qjjj' ) 868atoms_start () 869feed ( 'VjI' ) 870feed ( 'bad<Esc>' ) 871eq ( { 'badtest' , 'nvim' , '' , 'badtest' , 'badnvim' } , get_lines ()) 872-- "Q" is excluded: it is cursor-management, not part of the edit. 873eq ( { '01i\27ibad\27' } , atoms_tail ( 1 )) 874end ) 875 876it ( 'jump (absolute motion) in a mapping is not replayed, like a mark' , function () 877-- "G" would move every cursor to the same line; only the edit replays per-cursor. 878command ( 'nnoremap X Gdd' ) 879cursors ( { 'a' , 'b' , 'c' , 'd' } , 'jQ' ) 880feed ( 'X' ) 881eq ( { 'a' , 'c' } , get_lines ()) 882end ) 883 884it ( 'global op (undo/redo, g CTRL-A) does not cascade' , function () 885-- Mapping that performs a "global op" must not cascade. 886 887-- vim-repeat maps u/U/<C-R> to undo/redo wrappers. 888fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' } ) 889feed ( 'gg0Qj0Qj0' ) -- 3 cursors 890feed ( 'x' ) -- one cascade: aa,bb,cc 891eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 892command ( 'nnoremap <silent> u :<C-U>undo<CR>' ) 893command ( 'nnoremap <silent> <C-R> :<C-U>redo<CR>' ) 894feed ( 'u' ) -- One undo of the cascade, not per-cursor. 895eq ( { 'aaa' , 'bbb' , 'ccc' } , get_lines ()) 896feed ( '<C-R>' ) -- Redo mapping does not cascade. 897eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 898-- Nested normal_execute() after the undo, in the same mapping. 899command ( 'nnoremap <silent> u :<C-U>undo <Bar> normal! l<CR>' ) 900feed ( 'u' ) 901eq ( { 'aaa' , 'bbb' , 'ccc' } , get_lines ()) 902 903-- Follow-mode, where the undo's cursor-move would attempt LHS-replay. #42037 904clear_cursors () 905command ( 'nnoremap u u<Cmd>let g:did_u = 1<CR>' ) -- Multi-command RHS. 906command ( 'nnoremap <C-R> <C-R>' ) 907command ( 'nnoremap gm yiwp' ) 908cursors ( { 'aa' , 'bb' } , 'vipQ' ) 909feed ( 'gmgm' ) 910eq ( { 'aaaaaaaa' , 'bbbbbbbb' } , get_lines ()) 911feed ( 'u' ) 912eq ( { 'aaaa' , 'bbbb' } , get_lines ()) 913feed ( 'u' ) 914eq ( { 'aa' , 'bb' } , get_lines ()) 915feed ( '<C-R>' ) 916eq ( { 'aaaa' , 'bbbb' } , get_lines ()) 917feed ( '0x' ) -- Cursors are still in place. 918eq ( { 'aaa' , 'bbb' } , get_lines ()) 919 920-- "U" (line-undo) is also treated as a "global op". 921clear_cursors () 922command ( 'nnoremap U U<Cmd>let g:did_U = 1<CR>' ) 923cursors ( { 'aaa' , 'bbb' } , 'Qj0' ) 924feed ( 'x' ) 925eq ( { 'aa' , 'bb' } , get_lines ()) 926feed ( 'U' ) 927eq ( { 'aaa' , 'bb' } , get_lines ()) 928 929-- g CTRL-A (counter) applies once, not per-cursor. Also in follow-mode. 930command ( 'nnoremap ,n g<C-A><Cmd>let g:did_n = 1<CR>' ) 931for _ , place in ipairs ( { 'Qj0Qj0' , 'VGQ' } ) do 932clear_cursors () 933cursors ( { 'a' , 'b' , 'c' } , place ) 934feed ( ',n' ) 935eq ( { '1a' , '2b' , '3c' } , get_lines (), place ) 936end 937end ) 938end ) 939 940describe ( '. (dot-repeat)' , function () 941it ( 'repeats operators, pre-cursor edits, inserts and changes at all cursors' , function () 942-- Operator. 943cursors ( { 'aaa' , 'bbb' , 'ccc' } ) 944feed ( 'x' ) 945eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 946feed ( '.' ) 947eq ( { 'a' , 'b' , 'c' } , get_lines ()) 948-- An edit made before placing cursors repeats at all of them. 949clear_cursors () 950api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa' , 'bbb' , 'ccc' } ) 951feed ( 'gg0' ) 952feed ( 'x' ) 953eq ( { 'aa' , 'bbb' , 'ccc' } , get_lines ()) 954feed ( 'QjQj' ) 955feed ( '.' ) 956eq ( { 'a' , 'bb' , 'cc' } , get_lines ()) 957-- Insert. 958clear_cursors () 959cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 960feed ( 'iZ<Esc>' ) 961eq ( { 'Zaaa' , 'Zbbb' } , get_lines ()) 962feed ( '.' ) 963eq ( { 'ZZaaa' , 'ZZbbb' } , get_lines ()) 964-- Change, with a q= move between the change and the repeat. 965clear_cursors () 966cursors ( { 'one two' , 'one two' } , 'Qj' ) 967feed ( 'cwX<Esc>' ) 968eq ( { 'X two' , 'X two' } , get_lines ()) 969feed ( 'q=' ) 970feed ( 'w' ) 971feed ( 'q=' ) 972feed ( '.' ) 973eq ( { 'X X' , 'X X' } , get_lines ()) 974end ) 975end ) 976 977describe ( 'per-cursor registers' , function () 978it ( 'yy/p roundtrips each cursor through its own register' , function () 979cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 980feed ( 'yy' ) 981feed ( 'p' ) 982eq ( { 'aaa' , 'aaa' , 'bbb' , 'bbb' } , get_lines ()) 983end ) 984 985it ( 'insert-mode <C-R>x #41933' , function () 986for _ , case in ipairs ( { 987{ '0' , 'yiw' , { 'aaa x-aaa' , 'ccc y-ccc' } } , 988{ 'a' , '"ayiw' , { 'aaa x-aaa' , 'ccc y-ccc' } } , 989{ '"' , 'yiw' , { 'aaa x-aaa' , 'ccc y-ccc' } } , 990{ '-' , 'diw' , { ' x-aaa' , ' y-ccc' } } , 991} ) do 992local reg , fill , want =case [ 1 ], case [ 2 ], case [ 3 ] 993clear_cursors () 994cursors ( { 'aaa x' , 'ccc y' } , 'Qj0' ) 995feed ( fill ) 996feed (( 'A-<C-R>%s' ): format ( reg )) 997eq ( want , get_lines (), reg ) -- Live, before <Esc>. 998feed ( 'Z<Esc>' ) 999eq ( { want [ 1 ] .. 'Z' , want [ 2 ] .. 'Z' } , get_lines (), reg ) 1000end 1001end ) 1002 1003it ( 'empty at cursor init restores as empty' , function () 1004fn . setline ( 1 , { 'one two' , 'three four' } ) 1005feed ( 'gg0' ) 1006feed ( 'Q' ) -- this cursor's snapshot: @a is EMPTY 1007feed ( 'j0' ) 1008command ( [[let @a = 'LEAK']] ) -- primary-only write (synthetic: no cascade) 1009feed ( '"ap' ) 1010-- The replay loads the other cursor's register snapshot, where @a was empty: nothing 1011-- pastes there (empty registers are omitted; the primary's @a must not leak through). 1012eq ( { 'one two' , 'tLEAKhree four' } , get_lines ()) 1013end ) 1014 1015it ( 'dW + p swaps words using each cursor register' , function () 1016cursors ( { 'one two' , 'three four' } , 'Qj' ) 1017feed ( 'q=' ) 1018feed ( 'dW' ) 1019eq ( { 'two' , 'four' } , get_lines ()) 1020feed ( 'E' ) 1021feed ( 'p' ) 1022feed ( 'q=' ) 1023eq ( { 'twoone ' , 'fourthree ' } , get_lines ()) 1024end ) 1025 1026it ( 'clearing joins/gathers the per-cursor registers (DWIM)' , function () 1027cursors ( { 'one two' , 'three four' } , 'Qj' ) 1028feed ( 'dW' ) 1029eq ( { 'two' , 'four' } , get_lines ()) 1030clear_cursors () 1031-- On exit, each cursor's delete is joined (document order) into '"'. 1032eq ( 'one \nthree \n' , fn . getreg ( '"' )) 1033feed ( 'Go<Esc>p' ) 1034eq ( { 'two' , 'four' , '' , 'one ' , 'three ' } , get_lines ()) 1035 1036-- A cursor whose replays never wrote a register contributes nothing: it has no snapshot, 1037-- so the pre-session '"' cannot be joined in as if it were that cursor's delete. 1038fn . setreg ( '"' , 'STALE' ) 1039cursors ( { '' , 'abc' } , 'Qj' ) 1040feed ( 'x' ) -- writes a register at the primary only: "x" on the cursor's empty line is a no-op 1041clear_cursors () 1042eq ( 'a' , fn . getreg ( '"' )) -- No contributions to join; primary's register is untouched. 1043eq ( 'v' , fn . getregtype ( '"' )) 1044 1045-- Partial-range clear deletes cursors without ending the session; no join/gather (yet). 1046cursors ( { 'one two' , 'three four' , 'five six' } ) 1047feed ( 'dW' ) 1048eq ( { 'two' , 'four' , 'six' } , get_lines ()) 1049n . exec_lua ( 1050[[vim.api.nvim_buf_clear_namespace(0, vim.api.nvim_create_namespace('nvim.multicursor'), 0, 1)]] 1051) 1052eq ( 'five ' , fn . getreg ( '"' )) -- Primary's own delete, unjoined. 1053clear_cursors () 1054eq ( 'three \nfive \n' , fn . getreg ( '"' )) -- All cursors cleared => yanks joined. 1055end ) 1056 1057it ( 'visual p pastes each cursor register over the selection' , function () 1058cursors ( { 'aaa X' , 'bbb Y' } , 'Qj' ) 1059feed ( 'yiw' ) 1060feed ( 'q=' ) 1061feed ( 'w' ) 1062feed ( 'q=' ) 1063feed ( 'viwp' ) 1064eq ( { 'aaa aaa' , 'bbb bbb' } , get_lines ()) 1065end ) 1066end ) 1067 1068describe ( 'operator matrix' , function () 1069it ( 'operators cascade at each cursor' , function () 1070assert_rows ( { 1071-- x deletes a char at each cursor. 1072{ lines ={ 'aaa' , 'bbb' , 'ccc' } , keys ='QjQjx' , expect ={ 'aa' , 'bb' , 'cc' } } , 1073-- dw deletes a word at each cursor. 1074{ 1075lines ={ 'hello world' , 'foo bar' , 'one two' } , 1076keys ='QjQjdw' , 1077expect ={ 'world' , 'bar' , 'two' } , 1078} , 1079-- 2x deletes 2 chars at each cursor. 1080{ lines ={ 'aaaa' , 'bbbb' , 'cccc' } , keys ='QjQj2x' , expect ={ 'aa' , 'bb' , 'cc' } } , 1081-- dd deletes a line at each cursor. 1082{ 1083lines ={ 'a1' , 'a2' , 'b1' , 'b2' , 'c1' , 'c2' } , 1084keys ='Q2jQ2jdd' , 1085expect ={ 'a2' , 'b2' , 'c2' } , 1086} , 1087-- D deletes to EOL, C changes to EOL. 1088{ 1089lines ={ 'one two' , 'three four' } , 1090place ='gg0ll' , 1091keys ='QjD' , 1092expect ={ 'on' , 'th' } , 1093} , 1094{ 1095lines ={ 'one two' , 'three four' } , 1096place ='gg0ll' , 1097keys ='QjCX<Esc>' , 1098expect ={ 'onX' , 'thX' } , 1099} , 1100-- 2cl changes 2 chars. 1101{ lines ={ 'abcd' , 'efgh' } , keys ='Qj2clXY<Esc>' , expect ={ 'XYcd' , 'XYgh' } } , 1102-- ~ toggles case and advances. 1103{ lines ={ 'abc' , 'def' } , keys ='Qj~~' , expect ={ 'ABc' , 'DEf' } } , 1104-- cT{char} changes backwards. 1105{ 1106lines ={ 'x_abcY' , 'w_defgZ' } , 1107place ='gg$' , 1108keys ='Qj$cT_M<Esc>' , 1109expect ={ 'x_MY' , 'w_MZ' } , 1110} , 1111-- d4h deletes backwards. 1112{ lines ={ 'abcdef' , 'ghijkl' } , place ='gg$' , keys ='Qj$d4h' , expect ={ 'af' , 'gl' } } , 1113-- c2aw changes counted text objects. 1114{ 1115lines ={ 'one two three' , 'foo bar baz' } , 1116keys ='Qjc2awX<Esc>' , 1117expect ={ 'Xthree' , 'Xbaz' } , 1118} , 1119-- ci( on empty parens inserts inside. 1120{ 1121lines ={ 'a()b' , 'c()d' } , 1122place ='gg0l' , 1123keys ='Qjci(X<Esc>' , 1124expect ={ 'a(X)b' , 'c(X)d' } , 1125} , 1126-- ci" seeks forward to the quotes. 1127{ 1128lines ={ 'x "aa" y' , 'z "bbb" w' } , 1129keys ='Qjci"NEW<Esc>' , 1130expect ={ 'x "NEW" y' , 'z "NEW" w' } , 1131} , 1132-- df{char} cascades with its payload char. 1133{ lines ={ 'ab,cd' , 'wxy,z' } , keys ='Qjdf,' , expect ={ 'cd' , 'z' } } , 1134-- [count]r{char} replaces. 1135{ lines ={ 'abc' , 'def' } , keys ='Qj2rZ' , expect ={ 'ZZc' , 'ZZf' } } , 1136-- r<CR> and gr{char}: self-terminating replace sessions (no trailing <Esc>) cascade. 1137{ 1138lines ={ 'abcd' , 'efgh' } , 1139place ='gg0l' , 1140keys ='Qjr<CR>' , 1141expect ={ 'a' , 'cd' , 'e' , 'gh' } , 1142} , 1143{ lines ={ 'abc' , 'def' } , keys ='QjgrZ' , expect ={ 'Zbc' , 'Zef' } } , 1144-- x deletes one multibyte char at each cursor. 1145{ lines ={ 'éàü' , '日本語' } , keys ='Qjx' , expect ={ 'àü' , '本語' } } , 1146-- diw leaves the cursor at the deleted region (the "x" probes every cursor). 1147{ 1148lines ={ 'foo bar' , 'baz qux' , 'aaa bbb' } , 1149place ='gg0l' , 1150keys ='QjQjdiw' , 1151expect ={ ' bar' , ' qux' , ' bbb' } , 1152after ='x' , 1153after_expect ={ 'bar' , 'qux' , 'bbb' } , 1154} , 1155-- guiW lowercases and leaves the cursor at the region start. 1156{ 1157lines ={ 'FOO BAR' , 'BAZ QUX' } , 1158place ='gg0l' , 1159keys ='QjguiW' , 1160expect ={ 'foo BAR' , 'baz QUX' } , 1161after ='x' , 1162after_expect ={ 'oo BAR' , 'az QUX' } , 1163} , 1164-- An operator with a search-motion payload cascades whole (the atom IS the redobuff); 1165-- each cursor finds its own match. 1166{ 1167lines ={ 'aaa find end' , 'bbb find end' } , 1168keys ='Qjd/find<CR>' , 1169expect ={ 'find end' , 'find end' } , 1170} , 1171-- cc preserves per-line indent with 'autoindent'. 1172{ 1173lines ={ ' aaa' , ' bbb' } , 1174pre ='set autoindent' , 1175keys ='QjccX<Esc>' , 1176expect ={ ' X' , ' X' } , 1177} , 1178-- >> indents the line at each cursor. 1179{ 1180lines ={ 'foo' , 'bar' } , 1181pre ='set shiftwidth=2' , 1182keys ='Qj>>' , 1183expect ={ ' foo' , ' bar' } , 1184} , 1185} ) 1186end ) 1187end ) 1188 1189describe ( 'g CTRL-A (counter)' , function () 1190it ( 'inserts an ascending number at each cursor' , function () 1191cursors ( { 'a' , 'b' , 'c' } , 'Qj0Qj0' ) 1192feed ( 'g<C-A>' ) 1193eq ( { '1a' , '2b' , '3c' } , get_lines ()) 1194-- A count sets the starting number. 1195clear_cursors () 1196cursors ( { 'a' , 'b' } , 'Qj0' ) 1197feed ( '5g<C-A>' ) 1198eq ( { '5a' , '6b' } , get_lines ()) 1199-- Number overlapping primary/multicursor positions once, without deleting the multicursor. 1200clear_cursors () 1201cursors ( { 'x' , 'y' , 'z' } , 'QjQj' ) 1202feed ( 'gg0g<C-A>' ) 1203eq ( { '1x' , '2y' , 'z' } , get_lines ()) 1204eq ( 2 , ncursors ()) 1205end ) 1206 1207it ( 'without cursors, g CTRL-A is not a command' , function () 1208fn . setline ( 1 , { 'x 5' } ) 1209feed ( 'gg0g<C-A>' ) 1210eq ( { 'x 5' } , get_lines ()) -- beeps, no counter and no increment 1211feed ( '<C-A>' ) -- plain CTRL-A increments as usual 1212eq ( { 'x 6' } , get_lines ()) 1213end ) 1214 1215it ( 'number() takes start/step/format' , function () 1216cursors ( { 'x' , 'x' , 'x' } , 'Qj0Qj0' ) 1217n . exec_lua ( [[require('vim._core.mcursor').number(10, 2, '%d) ')]] ) 1218eq ( { '10) x' , '12) x' , '14) x' } , get_lines ()) 1219end ) 1220end ) 1221 1222describe ( 'extmarks' , function () 1223it ( 'adding an extmark adds a cursor, which cascades' , function () 1224fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' } ) 1225add_cursor ( 0 , { 1 , 0 } ) 1226add_cursor ( 0 , { 2 , 0 } ) 1227eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 1228feed ( 'Gx' ) 1229eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 1230 1231-- Also in a hidden buffer. 1232command ( 'set hidden' ) 1233local other =api . nvim_create_buf ( true , false ) 1234api . nvim_buf_set_lines ( other , 0 , - 1 , true , { 'xxx' , 'yyy' } ) 1235add_cursor ( other , { 1 , 0 } ) 1236api . nvim_set_current_buf ( other ) 1237feed ( 'Gx' ) 1238eq ( { 'xx' , 'yy' } , get_lines ()) 1239 1240-- Ignored if a cursor already exists (no double-apply), unlike "Q" which toggles. 1241clear_cursors () 1242fn . setline ( 1 , { 'abcdef' , 'ghijkl' } ) 1243feed ( 'gg0' ) 1244add_cursor ( 0 , { 1 , 0 } ) 1245add_cursor ( 0 , { 1 , 0 } ) 1246eq ( { { 0 , 0 } } , anchors ()) 1247feed ( 'j0x' ) 1248eq ( { 'bcdef' , 'hijkl' } , get_lines ()) 1249end ) 1250 1251it ( 'deleting an extmark deletes its cursor' , function () 1252cursors ( { 'aaa' , 'bbb' , 'ccc' } ) 1253local ns =api . nvim_create_namespace ( 'nvim.multicursor' ) 1254local marks =api . nvim_buf_get_extmarks ( 0 , ns , 0 , - 1 , {} ) 1255eq ( 2 , # marks ) 1256api . nvim_buf_del_extmark ( 0 , ns , marks [ 1 ][ 1 ]) 1257feed ( 'x' ) -- the cursor without a mark is swept; the other cascades 1258eq ( { 'aaa' , 'bb' , 'cc' } , get_lines ()) 1259eq ( 1 , ncursors ()) 1260end ) 1261 1262it ( 'exit with cursors active in multiple buffers' , function () 1263cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 1264feed ( 'yy' ) -- Session-written register, exit path reaches mc_reg_gather. 1265command ( 'new' ) 1266fn . setline ( 1 , { 'ccc' } ) 1267add_cursor ( 0 , { 1 , 0 } ) 1268n . expect_exit ( command , 'qall!' ) 1269end ) 1270 1271it ( 'cursors are disposed with their buffer' , function () 1272local screen =Screen . new ( 30 , 5 ) 1273command ( 'set showcmd' ) 1274fn . setline ( 1 , { 'aaa' , 'bbb' } ) 1275local buf =api . nvim_get_current_buf () 1276add_cursor ( 0 , { 1 , 0 } ) 1277add_cursor ( 0 , { 2 , 0 } ) 1278 1279-- Deleting an unrelated buffer does not dedupe the cursor under the primary. #41651 1280for _ , has_cursor in ipairs ( { false , true } ) do 1281local scratch =api . nvim_create_buf ( false , true ) 1282if has_cursor then 1283add_cursor ( scratch , { 1 , 0 } ) 1284end 1285api . nvim_buf_delete ( scratch , { force =true } ) 1286eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 1287end 1288 1289command ( 'new' ) 1290fn . setreg ( '"' , 'KEEP' ) 1291command ( 'bwipeout! ' .. buf ) 1292-- Registers NOT gathered/joined, the cleared cursors are not in curbuf. 1293eq ( 'KEEP' , fn . getreg ( '"' )) 1294eq ( 'v' , fn . getregtype ( '"' )) 1295fn . setline ( 1 , { 'xxx' , 'yyy' } ) 1296add_cursor ( 0 , { 1 , 0 } ) 1297feed ( 'Gx' ) 1298eq ( { 'xx' , 'yy' } , get_lines ()) 1299-- The wiped buffer's cursors are gone: only the new one counts. 1300screen : expect ( { any =' 1×' } ) 1301end ) 1302end ) 1303 1304describe ( 'buffers and windows' , function () 1305it ( 'cursors/cascade are per-buffer' , function () 1306-- Cursors cascade only if their buffer is the current buffer. 1307command ( 'set hidden' ) 1308cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 1309local buf_a =api . nvim_get_current_buf () 1310feed ( ':enew<CR>' ) -- Typed, so the buffer switch goes through atom capture. 1311eq ( 0 , ncursors ()) 1312cursors ( { 'xxx' , 'yyy' } , 'Qj' ) 1313local buf_b =api . nvim_get_current_buf () 1314eq ( 1 , ncursors ()) 1315feed ( 'x' ) -- Cascades only in the current buffer. 1316eq ( { 'xx' , 'yy' } , get_lines ()) 1317eq ( { 'aaa' , 'bbb' } , api . nvim_buf_get_lines ( buf_a , 0 , - 1 , true )) 1318command ( 'buffer #' ) 1319eq ( 1 , ncursors ()) -- The extmark-tracked cursor survived the round-trip. 1320feed ( '2G0x' ) -- The cascade resumes. 1321eq ( { 'aa' , 'bb' } , get_lines ()) 1322eq ( { 'xx' , 'yy' } , api . nvim_buf_get_lines ( buf_b , 0 , - 1 , true )) 1323-- Likewise from another window. 1324command ( 'split | buffer ' .. buf_b ) 1325feed ( '2G0x' ) 1326eq ( { 'x' , 'y' } , get_lines ()) 1327command ( 'wincmd p' ) 1328feed ( '2G0x' ) 1329eq ( { 'a' , 'b' } , get_lines ()) 1330end ) 1331 1332it ( 'atom queued in one buffer does not cascade in another buffer' , function () 1333command ( 'set hidden' ) 1334cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 1335local buf_a =api . nvim_get_current_buf () 1336command ( 'enew' ) 1337fn . setline ( 1 , { 'ccc' , 'ddd' } ) 1338feed ( 'gg0Qj' ) 1339local buf_b =api . nvim_get_current_buf () 1340command ( 'buffer #' ) 1341command ( 'nnoremap X x:bnext<CR>' ) 1342feed ( '2G0X' ) 1343-- The "x" atom was captured in A but resolves in B. It should not cascade to B's cursors. 1344-- A's cursors are skipped, except the edit done by the primary cursor. 1345eq ( { a ={ 'aaa' , 'bb' } , b ={ 'ccc' , 'ddd' } } , { 1346a =api . nvim_buf_get_lines ( buf_a , 0 , - 1 , true ), 1347b =api . nvim_buf_get_lines ( buf_b , 0 , - 1 , true ), 1348} ) 1349-- Same-buffer edit still cascades. 1350feed ( '2G0x' ) 1351eq ( { 'cc' , 'dd' } , api . nvim_buf_get_lines ( buf_b , 0 , - 1 , true )) 1352 1353-- TEMPORARY buffer-switch is allowed. A mapping may do work in a throwaway buffer, then apply 1354-- edits to curbuf. 1355command ( 'nnoremap Y :new<CR>ihello<Esc>:bwipe!<CR>x' ) 1356feed ( '2G0Y' ) 1357eq ( { 'c' , 'd' } , api . nvim_buf_get_lines ( buf_b , 0 , - 1 , true )) 1358end ) 1359 1360it ( 'edits cascade from any window showing the buffer (:split)' , function () 1361-- Cascade is conditional on the buffer, not the window. 1362cursors ( { 'aaa' , 'bbb' } , 'Q' ) 1363command ( 'split' ) 1364feed ( 'jx' ) -- edit from the new window 1365eq ( { 'aa' , 'bb' } , get_lines ()) 1366end ) 1367 1368it ( 'clearing is per-buffer' , function () 1369command ( 'set hidden' ) 1370cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 1371command ( 'enew' ) 1372clear_cursors () 1373command ( 'buffer #' ) 1374eq ( 1 , ncursors ()) 1375clear_cursors () 1376eq ( 0 , ncursors ()) 1377feed ( 'gg0x' ) -- only the primary edits 1378eq ( { 'aa' , 'bbb' } , get_lines ()) 1379end ) 1380 1381it ( 'nvim_buf_clear_namespace("nvim.multicursor") deletes the cursors' , function () 1382cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 1383eq ( 1 , ncursors ()) 1384n . exec_lua ( [[ 1385vim.api.nvim_buf_clear_namespace(0, vim.api.nvim_create_namespace('nvim.multicursor'), 0, -1) 1386]] ) 1387eq ( 0 , ncursors ()) 1388feed ( 'gQ' ) -- the deletion snapshotted the cursors: restorable 1389eq ( 1 , ncursors ()) 1390feed ( 'x' ) -- both cursors edit again (primary still on line 2) 1391eq ( { 'aa' , 'bb' } , get_lines ()) 1392 1393-- Partially clearing the namespace does not "dedupe" the cursor under the primary. 1394feed ( 'Q' ) 1395api . nvim_buf_clear_namespace ( 0 , api . nvim_create_namespace ( 'nvim.multicursor' ), 0 , 1 ) 1396eq ( { { 1 , 0 } } , anchors ()) 1397end ) 1398 1399it ( ':edit! (reload) clears the cursors and the gQ snapshot' , function () 1400local fname =t . tmpname () 1401fn . writefile ( { 'aaa' , 'bbb' } , fname ) 1402command ( 'edit ' .. fname ) 1403feed ( 'gg0QjQ' ) 1404clear_cursors () -- Snapshots the cursors (gQ). 1405feed ( 'Q' ) 1406eq ( 1 , ncursors ()) 1407command ( 'edit!' ) 1408eq ( 0 , ncursors ()) 1409feed ( 'gQ' ) 1410eq ( 0 , ncursors ()) -- Nothing to restore: the snapshot died with the text. 1411feed ( 'Q' ) -- A new session starts cleanly. 1412eq ( 1 , ncursors ()) 1413end ) 1414end ) 1415 1416describe ( 'insert-mode' , function () 1417it ( '"cgn" cascades live; failed cursor is removed #41960' , function () 1418-- "gn" searches per-cursor. Insert-entry cascades, and typed text previews live, before ESC. 1419cursors ( { 'a x foo' , 'b foo' , 'c y z foo' } , 'QjQj0' ) 1420feed ( '/foo<CR>' ) 1421feed ( 'cgn' ) 1422eq ( { 'a x ' , 'b ' , 'c y z ' } , get_lines ()) 1423eq ( { { 0 , 4 } , { 1 , 2 } } , anchors ()) -- Each cursor moved to its match. 1424feed ( 'X' ) 1425eq ( { 'a x X' , 'b X' , 'c y z X' } , get_lines ()) 1426feed ( '<Esc>' ) 1427eq ( { 'a x X' , 'b X' , 'c y z X' } , get_lines ()) 1428feed ( 'u' ) -- One undo step. 1429eq ( { 'a x foo' , 'b foo' , 'c y z foo' } , get_lines ()) 1430 1431-- Cursor is removed when insert-entry fails at a cursor ("ct;" where there is no ";"). 1432clear_cursors () 1433cursors ( { 'a;b' , 'cd' , 'e;f' } , 'QjQj0' ) 1434eq ( 2 , ncursors ()) 1435feed ( 'ct;X<Esc>' ) 1436eq ( { 'X;b' , 'cd' , 'X;f' } , get_lines ()) 1437eq ( 1 , ncursors ()) 1438-- Same for "cgn" at a cursor with no match left (the primary's replay consumed it). 1439clear_cursors () 1440cursors ( { 'a foo' , 'b' , 'c' } , 'jQjQgg0' ) 1441feed ( '/foo<CR>' ) 1442feed ( 'cgnX<Esc>' ) 1443eq ( { 'a X' , 'b' , 'c' } , get_lines ()) 1444eq ( 0 , ncursors ()) 1445end ) 1446 1447it ( 'CTRL-U cascades before <Esc> (deletion crossing the session anchor)' , function () 1448-- Deleting typed text cascades live (the region shrinks). But CTRL-U here eats the "o" 1449-- autoindent, which precedes the tracked region, invisible to the preview diff. 1450-- Non-literal keys flush instead: the pending keys replay as a span immediately. 1451command ( 'set autoindent' ) 1452cursors ( { ' indented aa' , ' indented cc' } , 'Q' ) 1453feed ( '2gg0' ) 1454feed ( 'o' ) 1455feed ( '<C-u>' ) 1456-- Mid-session (no <Esc> yet): the cursor's line must match the 1457-- primary's (indent eaten at both). 1458eq ( { ' indented aa' , '' , ' indented cc' , '' } , get_lines ()) 1459end ) 1460 1461it ( 'o + CTRL-U does not join lines at less-indented cursors; ESC or CTRL-C ends' , function () 1462-- 'autoindent': "o" opens an indented line at the indented cursors, an empty line at the flat 1463-- cursor. The replayed CTRL-U there has nothing to delete; it must not backspace through the 1464-- line boundary ('backspace' includes "eol") and join lines. 1465command ( 'set autoindent' ) 1466 1467for _ , esc in ipairs ( { '<Esc>' , '<C-c>' } ) do 1468clear_cursors () 1469cursors ( { ' indented aa' , 'flat bb' , ' indented cc' } ) 1470feed ( 'o<C-u><Tab>yay' ) 1471-- Drain first: a CTRL-C pending in typeahead cancels the queued keys before they execute 1472-- (unrelated to multicursor). 1473n . poke_eventloop () 1474feed ( esc ) 1475-- CTRL-C ends the insert session like <Esc>; it must not also abort the commit replay (the 1476-- primary keeps its text on CTRL-C, so the cursors do too). 1477eq ( { 1478' indented aa' , 1479'\tyay' , 1480'flat bb' , 1481'\tyay' , 1482' indented cc' , 1483'\tyay' , 1484} , get_lines (), esc ) 1485-- The cursors survive: a subsequent edit still cascades to all three. 1486feed ( 'x' ) 1487eq ( { 1488' indented aa' , 1489'\tya' , 1490'flat bb' , 1491'\tya' , 1492' indented cc' , 1493'\tya' , 1494} , get_lines (), esc ) 1495end 1496end ) 1497 1498it ( 'cursor-moves cascade live at each cursor' , function () 1499cursors ( { 'alpha one' , 'beta two' , 'gamma three' } ) 1500-- A cursor-move key is a cmd, not text: it flushes pending keys as a span (like CTRL-U). 1501feed ( 'Aab<Left><Left>X' ) 1502-- Mid-session (no <Esc> yet): the cursor-move already applied everywhere. 1503eq ( { 'alpha oneXab' , 'beta twoXab' , 'gamma threeXab' } , get_lines ()) 1504feed ( '<Esc>' ) 1505feed ( 'AZ<Home>Y<Esc>' ) 1506eq ( { 'Yalpha oneXabZ' , 'Ybeta twoXabZ' , 'Ygamma threeXabZ' } , get_lines ()) 1507-- Word-wise cursor-move: per-cursor word boundaries, not a copied column offset. 1508feed ( 'A<S-Left>W<Esc>' ) 1509eq ( { 'Yalpha WoneXabZ' , 'Ybeta WtwoXabZ' , 'Ygamma WthreeXabZ' } , get_lines ()) 1510end ) 1511 1512it ( 'CTRL-G U cursor-move cascades live; one undo block' , function () 1513cursors ( { 'aa' , 'bb' } , 'Qj' ) 1514feed ( 'i12<C-g>U<Left>3' ) 1515-- Mid-session: the CTRL-G U move flushed and cascaded. 1516eq ( { '132aa' , '132bb' } , get_lines ()) 1517feed ( '<Esc>' ) 1518-- CTRL-G U does not split undo (unlike plain <Left>). 1519feed ( 'u' ) 1520eq ( { 'aa' , 'bb' } , get_lines ()) 1521end ) 1522 1523it ( 'absolute jump (<C-Home>) splits: previews stay, the live cascade re-anchors' , function () 1524fn . setline ( 1 , { 'alpha' , 'beta' , 'gamma' } ) 1525feed ( 'j0Q' ) 1526feed ( 'j0Q' ) 1527feed ( 'gg$' ) 1528feed ( 'ix<C-Home>y' ) 1529-- Mid-session: "x" stays as text; "y" (after jump) continues at the re-anchored positions. 1530eq ( { 'yalphxa' , 'xybeta' , 'xygamma' } , get_lines ()) 1531feed ( '<Esc>' ) 1532eq ( { 'yalphxa' , 'xybeta' , 'xygamma' } , get_lines ()) 1533end ) 1534 1535it ( 'EOL insertion points display as virtual cells' , function () 1536local screen =Screen . new ( 40 , 5 ) 1537command ( 'hi MCursor guifg=NONE guibg=Red' ) 1538cursors ( { 'aa' , 'bbbb' , 'cc' } ) 1539-- "A": each insertion point is past EOL (no char cell), a virtual cell overlays it. #41576 1540feed ( 'A' ) 1541screen : add_extra_attr_ids ( { 1542[ 100 ] ={ background =Screen . colors . Red } , 1543} ) 1544screen : expect ( [[ 1545aa{100: } | 1546bbbb{100: } | 1547cc^ | 1548{1:~ }| 1549{5:-- INSERT --} | 1550]] ) 1551-- Ending the session moves cursors onto last char (real text); the virtual cell disappears. 1552feed ( '!<Esc>' ) 1553screen : expect ( [[ 1554aa{100:!} | 1555bbbb{100:!} | 1556cc^! | 1557{1:~ }| 1558| 1559]] ) 1560end ) 1561 1562it ( 'typed text live-mirrors at cursors' , function () 1563command ( 'let v:oldfiles = ["/a", "/b"] | let @a = "reg"' ) 1564local screen =Screen . new ( 30 , 6 ) 1565cursors ( { 'aaa' , 'bbb' , 'ccc' } ) 1566-- Still in insert mode (no <Esc> yet): the text already cascaded; each cursor displays 1567-- at its insertion point, like the primary. 1568feed ( 'iXY' ) 1569screen : expect ( [[ 1570XY{17:a}aa | 1571XY{17:b}bb | 1572XY^ccc | 1573{1:~ }|*2 1574{5:-- INSERT --} | 1575]] ) 1576feed ( '<Esc>' ) 1577eq ( { 'XYaaa' , 'XYbbb' , 'XYccc' } , get_lines ()) 1578-- The session end shifts every cursor onto the last inserted char. 1579screen : expect ( [[ 1580X{17:Y}aaa | 1581X{17:Y}bbb | 1582X^Yccc | 1583{1:~ }|*2 1584| 1585]] ) 1586-- Cascade context-management should not clobber v:oldfiles. 1587eq ( { '/a' , '/b' } , api . nvim_get_vvar ( 'oldfiles' )) 1588 1589-- Entering insert mode ("a") displays each cursor at its insertion-point immediately. 1590feed ( 'a' ) 1591screen : expect ( [[ 1592XY{17:a}aa | 1593XY{17:b}bb | 1594XY^ccc | 1595{1:~ }|*2 1596{5:-- INSERT --} | 1597]] ) 1598feed ( '<Esc>' ) 1599-- Operator entry ("ciw") live-mirrors the per-cursor change, still in insert mode. 1600feed ( '0ciwZ' ) 1601screen : expect ( [[ 1602Z{17: } | 1603Z{17: } | 1604Z^ | 1605{1:~ }|*2 1606{5:-- INSERT --} | 1607]] ) 1608feed ( '<Esc>' ) 1609-- Visual-change entry ("viwc") live-mirrors the selection at each cursor. 1610feed ( 'viwcW' ) 1611screen : expect ( [[ 1612W{17: } | 1613W{17: } | 1614W^ | 1615{1:~ }|*2 1616{5:-- INSERT --} | 1617]] ) 1618feed ( '<Esc>' ) 1619-- Programmatic Visual selection followed by a typed change. #41705 1620command ( 'normal! viw' ) 1621feed ( 'cV' ) 1622screen : expect ( [[ 1623V{17: } | 1624V{17: } | 1625V^ | 1626{1:~ }|*2 1627{5:-- INSERT --} | 1628]] ) 1629feed ( '<Esc>' ) 1630-- Also when "c" is a Visual-mode operator mapping. #41605 1631command ( 'xnoremap c "_c' ) 1632feed ( 'viwcM' ) 1633screen : expect ( [[ 1634M{17: } | 1635M{17: } | 1636M^ | 1637{1:~ }|*2 1638{5:-- INSERT --} | 1639]] ) 1640feed ( '<Esc>' ) 1641-- Also when another key maps to "c". 1642command ( 'xnoremap - c' ) 1643feed ( 'viw-N' ) 1644screen : expect ( [[ 1645N{17: } | 1646N{17: } | 1647N^ | 1648{1:~ }|*2 1649{5:-- INSERT --} | 1650]] ) 1651feed ( '<Esc>' ) 1652 1653-- Also when a Normal-mode mapping moves before entering Insert. #41605 1654clear_cursors () 1655cursors ( { 'aaaa' , 'bbbb' , 'cccc' } , 'llQjQj' ) 1656eq ( { { 0 , 2 } , { 1 , 2 } } , anchors ()) 1657eq ( { 3 , 2 } , api . nvim_win_get_cursor ( 0 )) 1658command ( 'nnoremap i ^i' ) 1659atoms_start () 1660feed ( 'iX' ) 1661screen : expect ( [[ 1662X{17:a}aaa | 1663X{17:b}bbb | 1664X^cccc | 1665{1:~ }|*2 1666{5:-- INSERT --} | 1667]] ) 1668feed ( '<Esc>' ) 1669eq ( { 'Xaaaa' , 'Xbbbb' , 'Xcccc' } , get_lines ()) 1670local ev =atom_last () 1671eq ( 1672{ type ='mapping' , lhs =k ( 'iX<Esc>' ), changed =true } , 1673t . pick ( atom_last (), 'type' , 'lhs' , 'changed' ) 1674) 1675eq ( { 1676{ type ='motion' , keys ='^' } , 1677{ type ='insert' , keys =k ( '1i<Esc>' ) } , 1678{ type ='insert' , keys =k ( 'iX<Esc>' ) } , 1679} , subatoms ( ev , 'type' , 'keys' )) 1680end ) 1681 1682it ( 'session survives all cursors deduping away mid-session' , function () 1683-- "A" entry replay lands on the primary and dedupe removes it mid-session; the ESC commit 1684-- must not cascade into the empty set. 1685fn . setline ( 1 , { 'x' } ) 1686feed ( 'Q' ) 1687feed ( 'Ahi<Esc>' ) 1688n . assert_alive () 1689eq ( { 'xhi' } , get_lines ()) 1690end ) 1691 1692it ( 'sessions cascade at each cursor' , function () 1693assert_rows ( { 1694-- iZ: after <Esc> the cursors sit ON the last inserted char (like the primary cursor). 1695{ 1696lines ={ 'aaa' , 'bbb' , 'ccc' } , 1697keys ='QjQjiZ<Esc>' , 1698expect ={ 'Zaaa' , 'Zbbb' , 'Zccc' } , 1699after ='x' , 1700after_expect ={ 'aaa' , 'bbb' , 'ccc' } , 1701} , 1702-- A: distinct chars and line lengths, so an off-by-one anchor (e.g. a past-EOL anchor 1703-- clamped by a replay in MODE_NORMAL) inserts before the last char, which identical 1704-- chars ("!!") would mask. 1705{ 1706lines ={ 'a' , 'bb bb' , 'c cc' } , 1707keys ='QjQjA!?<Esc>' , 1708expect ={ 'a!?' , 'bb bb!?' , 'c cc!?' } , 1709after ='x' , 1710after_expect ={ 'a!' , 'bb bb!' , 'c cc!' } , 1711} , 1712-- cw changes a word at each cursor. 1713{ 1714lines ={ 'aaa one' , 'bbb two' , 'ccc three' } , 1715keys ='QjQjcwFOO<Esc>' , 1716expect ={ 'FOO one' , 'FOO two' , 'FOO three' } , 1717} , 1718-- <BS> cascades deletion. 1719{ lines ={ 'aaa' , 'bbb' } , keys ='QjiXY<BS>Z<Esc>' , expect ={ 'XZaaa' , 'XZbbb' } } , 1720-- <BS> deletes before the insert start. 1721{ 1722lines ={ 'abc' , 'def' } , 1723place ='gg0l' , 1724keys ='Qji<BS>Z<Esc>' , 1725expect ={ 'Zbc' , 'Zef' } , 1726} , 1727-- <BS> at col 0 joins with the line above. 1728{ 1729lines ={ 'aa' , 'bb' , 'cc' , 'dd' } , 1730place ='ggj0' , 1731keys ='Q2ji<BS><Esc>' , 1732expect ={ 'aabb' , 'ccdd' } , 1733} , 1734-- <CR> mid-insert splits the line. 1735{ 1736lines ={ 'aaXbb' , 'ccXdd' } , 1737place ='gg02l' , 1738keys ='QjiAB<CR>CD<Esc>' , 1739expect ={ 'aaAB' , 'CDXbb' , 'ccAB' , 'CDXdd' } , 1740} , 1741-- O opens a line above. 1742{ 1743lines ={ 'aaa' , 'bbb' , 'ccc' } , 1744keys ='Q2jOX<Esc>' , 1745expect ={ 'X' , 'aaa' , 'bbb' , 'X' , 'ccc' } , 1746} , 1747-- a appends after the cursor char. 1748{ lines ={ 'abc' , 'def' } , keys ='QjaZ<Esc>' , expect ={ 'aZbc' , 'dZef' } } , 1749-- I inserts at the first non-blank. 1750{ 1751lines ={ ' aa' , ' bb' } , 1752place ='gg$' , 1753keys ='Qj$IX<Esc>' , 1754expect ={ ' Xaa' , ' Xbb' } , 1755} , 1756-- i_CTRL-W deletes a word. 1757{ 1758lines ={ 'zz' , 'yy' } , 1759keys ='Qjifoo bar<C-w>X<Esc>' , 1760expect ={ 'foo Xzz' , 'foo Xyy' } , 1761} , 1762-- i_CTRL-V inserts a unicode char; after <Esc> the cursors sit ON it. 1763{ 1764lines ={ 'aaa' , 'bbb' } , 1765keys ='Qji<C-v>u00e9<Esc>' , 1766expect ={ 'éaaa' , 'ébbb' } , 1767after ='x' , 1768after_expect ={ 'aaa' , 'bbb' } , 1769} , 1770-- i<Esc> at col 0 keeps the cursor at col 0. 1771{ 1772lines ={ 'abc' , 'def' } , 1773keys ='Qji<Esc>' , 1774expect ={ 'abc' , 'def' } , 1775after ='x' , 1776after_expect ={ 'bc' , 'ef' } , 1777} , 1778-- 3iZ (counted insert) cascades the whole session. 1779{ lines ={ 'aaa' , 'bbb' } , keys ='Qj3iZ<Esc>' , expect ={ 'ZZZaaa' , 'ZZZbbb' } } , 1780-- R replaces; R + <BS> restores the replaced chars. 1781{ lines ={ 'abcdef' , 'ghijkl' } , keys ='QjRXY<Esc>' , expect ={ 'XYcdef' , 'XYijkl' } } , 1782{ 1783lines ={ 'abcdef' , 'ghijkl' } , 1784keys ='QjRXY<BS><BS><Esc>' , 1785expect ={ 'abcdef' , 'ghijkl' } , 1786} , 1787-- Abbreviations expand identically at each cursor. 1788{ 1789lines ={ 'aaa' , 'bbb' } , 1790pre ='iabbrev teh the' , 1791keys ='Qjiteh <Esc>' , 1792expect ={ 'the aaa' , 'the bbb' } , 1793} , 1794-- 'autoindent' applies per cursor with o. 1795{ 1796lines ={ ' aa' , ' bb' } , 1797pre ='set autoindent' , 1798keys ='QjoX<Esc>' , 1799expect ={ ' aa' , ' X' , ' bb' , ' X' } , 1800} , 1801} ) 1802end ) 1803 1804it ( 'ea (with q=)' , function () 1805cursors ( { 'one two' , 'three four' } , 'Qj' ) 1806feed ( 'q=' ) 1807feed ( 'ea!<Esc>' ) 1808feed ( 'q=' ) 1809eq ( { 'one! two' , 'three! four' } , get_lines ()) 1810end ) 1811 1812it ( 'completion: i_CTRL-N completes existing text at each cursor' , function () 1813command ( 'set completeopt=menuone' ) 1814cursors ( { 'wombat' , 'wo' , 'wo' } , 'jQj' ) 1815feed ( 'A<C-n><Esc>' ) 1816eq ( { 'wombat' , 'wombat' , 'wombat' } , get_lines ()) 1817end ) 1818 1819it ( 'completion: live-mirrors cursors on separate lines' , function () 1820command ( 'set completeopt=menuone' ) 1821cursors ( { 'wombat' , '' , '' } , 'jQj' ) 1822feed ( 'iwo<C-n>' ) 1823eq ( 1 , fn . pumvisible ()) 1824eq ( { 'wombat' , 'wombat' , 'wombat' } , get_lines ()) 1825feed ( '<Esc>' ) 1826eq ( { 'wombat' , 'wombat' , 'wombat' } , get_lines ()) 1827end ) 1828 1829it ( 'completion: ESC commits with stale previews #41719' , function () 1830command ( 'set completeopt=menuone,noselect' ) 1831cursors ( { 'aa' , 'bb cc dd' , 'ee' } , 'GQkQww' ) 1832feed ( 'ciwa<C-n>a' ) 1833eq ( 1 , fn . pumvisible ()) 1834eq ( { 'aa' , 'a cc aa' , 'a' } , get_lines ()) 1835feed ( '<Esc>' ) 1836eq ( { 'aa' , 'aa cc aa' , 'aa' } , get_lines ()) 1837end ) 1838 1839it ( 'completion: live-mirrors same-line cursor AFTER the primary' , function () 1840command ( 'set completeopt=menuone,noselect' ) 1841cursors ( { 'aa' , 'bb cc dd' } , 'jwwQ0' ) 1842feed ( 'ciw<C-n>a' ) 1843eq ( 1 , fn . pumvisible ()) 1844eq ( { 'aa' , 'a cc a' } , get_lines ()) 1845feed ( '<C-e>x<Esc>' ) 1846eq ( { 'aa' , 'ax cc ax' } , get_lines ()) 1847end ) 1848 1849it ( 'completion: defers preview of multiline edit BEFORE primary #41719' , function () 1850command ( 'set completeopt=menuone' ) 1851cursors ( { '' , '' } , 'Qj' ) 1852feed ( 'ia' ) 1853fn . complete ( 1 , { 'aa\nbb' , 'ac' } ) 1854eq ( 1 , fn . pumvisible ()) 1855eq ( { 'a' , 'aa' , 'bb' } , get_lines ()) 1856feed ( '<C-c>' ) 1857end ) 1858 1859it ( 'completion: defers preview of same-line cursor BEFORE primary #41719' , function () 1860command ( 'set completeopt=menu,noselect' ) 1861-- Place cursor1 on its own line, to exercise whole-batch deferral. 1862-- Place cursor2 on same line as the primary, to test same-line handling. 1863for _ , case in ipairs ( { 1864{ 'aa' , 'aa' , '' } , 1865{ 'a<C-e>' , 'a' , 'a' } , 1866{ 'a<C-n><C-y>' , 'aa' , 'aa' } , 1867{ 'a<BS>a' , 'a' , 'a' } , 1868} ) do 1869local keys , text , preview =unpack ( case ) 1870clear_cursors () 1871cursors ( { 'aa' , 'bb cc dd' , 'ee' } , 'GQkQww' ) 1872eq ( { { 1 , 0 } , { 2 , 0 } } , anchors ()) 1873eq ( { 2 , 6 } , api . nvim_win_get_cursor ( 0 )) 1874feed ( 'ciw<C-n>' ) 1875eq ( 1 , fn . pumvisible ()) 1876feed ( keys ) 1877eq ( { 'aa' , ( '%s cc %s' ): format ( preview , text ), preview } , get_lines ()) 1878feed ( 'aa' ) 1879eq ( 0 , fn . pumvisible ()) 1880feed ( ' ' ) 1881eq ( { 'aa' , ( '%saa cc %saa ' ): format ( text , text ), ( '%saa ' ): format ( text ) } , get_lines ()) 1882feed ( 'x<Esc>' ) 1883eq ( { 'aa' , ( '%saa x cc %saa x' ): format ( text , text ), ( '%saa x' ): format ( text ) } , get_lines ()) 1884end 1885end ) 1886 1887it ( 'mapping ending in a Normal-mode "tail", cascades #41864' , function () 1888command ( 'inoremap <Esc> <Esc>l' ) -- the "<Esc>" leaves a Normal-mode tail ("l") 1889api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'echo hi' , 'echo bye' } ) 1890feed ( 'WQjq=' ) -- cursor (1,5), primary (2,5); follow-mode ON 1891feed ( 'i<Esc>' ) 1892-- The empty insert and its "l" tail cascade: both cursors end on column 5, not 4. 1893eq ( { { 0 , 5 } } , anchors ()) 1894eq ( { 2 , 5 } , api . nvim_win_get_cursor ( 0 )) 1895feed ( 'x' ) 1896eq ( { 'echo i' , 'echo ye' } , get_lines ()) 1897 1898-- Without follow-mode, the tail "l" is a motion, so primary-only. 1899api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'echo hi' , 'echo bye' } ) 1900clear_cursors () 1901feed ( 'gg0WQj' ) -- follow-mode OFF 1902feed ( 'i<Esc>' ) 1903eq ( { { 0 , 4 } } , anchors ()) 1904eq ( { 2 , 5 } , api . nvim_win_get_cursor ( 0 )) 1905 1906-- A mapping that explicitly invokes ("1q=…2q=") can force the tail "l" to cascade. 1907command ( 'iunmap <Esc>' ) 1908command ( 'inoremap <Esc> <Esc>1q=l2q=' ) 1909api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'echo hi' , 'echo bye' } ) 1910clear_cursors () 1911feed ( 'gg0WQj' ) -- follow-mode OFF 1912feed ( 'i<Esc>' ) 1913eq ( { { 0 , 5 } } , anchors ()) 1914eq ( { 2 , 5 } , api . nvim_win_get_cursor ( 0 )) 1915-- Follow-mode is off again: a typed motion stays primary-only. 1916feed ( 'l' ) 1917eq ( { { 0 , 5 } } , anchors ()) 1918eq ( { 2 , 6 } , api . nvim_win_get_cursor ( 0 )) 1919end ) 1920end ) 1921 1922describe ( 'navigation state (primary-only)' , function () 1923it ( 'jumplist, changelist and marks follow only the primary' , function () 1924local l ={} 1925for i =1 , 30 do 1926l [ # l + 1 ] =( 'word%d line' ): format ( i ) 1927end 1928cursors ( l ) 1929-- Change marks and changelist: one edit at 3 cursors records the primary's position only. 1930local nchanges =# fn . getchangelist ()[ 1 ] 1931feed ( 'x' ) 1932eq ( { 3 , 0 } , api . nvim_buf_get_mark ( 0 , '[' )) 1933eq ( { 3 , 0 } , api . nvim_buf_get_mark ( 0 , ']' )) 1934eq ( { 3 , 0 } , api . nvim_buf_get_mark ( 0 , '.' )) 1935eq ( nchanges + 1 , # fn . getchangelist ()[ 1 ]) 1936-- Likewise an Insert-mode edit (previewed, then replayed at the cursors). 1937feed ( 'iab<Esc>' ) 1938eq ( { 3 , 0 } , api . nvim_buf_get_mark ( 0 , '.' )) 1939eq ( nchanges + 1 , # fn . getchangelist ()[ 1 ]) 1940eq ( 3 , fn . getchangelist ()[ 1 ][ nchanges + 1 ]. lnum ) 1941 1942-- Visual marks: the primary's selection. 1943feed ( 'viwy' ) 1944eq ( { 3 , 0 } , api . nvim_buf_get_mark ( 0 , '<' )) 1945eq ( { 3 , 5 } , api . nvim_buf_get_mark ( 0 , '>' )) 1946 1947-- Jumplist: a followed jump records one entry (the primary's). 1948-- (Last: all cursors land on line 30 and merge.) 1949command ( 'clearjumps' ) 1950feed ( 'q=' ) 1951feed ( 'G' ) 1952feed ( 'q=' ) 1953eq ( 1 , # fn . getjumplist ()[ 1 ]) 1954end ) 1955end ) 1956 1957describe ( 'completion' , function () 1958-- While a completion is active, the cascade pauses: redobuff is frozen, and spans cannot replay 1959-- into a busy completion (edit() refuses nesting). The cursors catch up when completion ends. 1960 1961--- Three empty lines under "foo*" completion candidates; cursors on lines 4-5, primary on 6. 1962local function ac_setup () 1963command ( 'setlocal autocomplete' ) 1964cursors ( { 'foo' , 'foobar' , 'foobarbaz' , '' , '' , '' } , '3jQjQj' ) 1965end 1966 1967it ( "'autocomplete' popup: cursors catch up when the completion ends" , function () 1968local screen =Screen . new ( 40 , 12 ) 1969ac_setup () 1970feed ( 'if' ) 1971feed ( 'o' ) 1972screen : expect ( { any ='INSERT' } ) 1973-- The preview updates live (even during completion); cursors intact. 1974eq ( 2 , ncursors ()) 1975feed ( '<Esc>' ) 1976eq ( { 'foo' , 'foobar' , 'foobarbaz' , 'fo' , 'fo' , 'fo' } , get_lines ()) 1977eq ( 2 , ncursors ()) 1978end ) 1979 1980it ( "'autocomplete': accepting a completion applies at all cursors" , function () 1981ac_setup () 1982feed ( 'if' ) 1983feed ( '<C-n>' ) -- select the first popup entry 1984feed ( '<C-y>' ) -- accept 1985feed ( ' tail<Esc>' ) -- and keep typing 1986local l =get_lines () 1987eq ( { l [ 4 ], l [ 4 ] } , { l [ 5 ], l [ 6 ] } ) 1988end ) 1989 1990it ( "'autocomplete': <BS>, <C-e>" , function () 1991-- BS/C-e cancel propagation. 1992ac_setup () 1993feed ( 'ifoo<BS>x<Esc>' ) 1994eq ( { 'fox' , 'fox' , 'fox' } , { get_lines ()[ 4 ], get_lines ()[ 5 ], get_lines ()[ 6 ] } ) 1995feed ( 'cc' ) 1996feed ( 'f<C-n><C-n><C-e><Esc>' ) -- cycle, then cancel back to the leader 1997eq ( { 'f' , 'f' , 'f' } , { get_lines ()[ 4 ], get_lines ()[ 5 ], get_lines ()[ 6 ] } ) 1998 1999-- No candidates, {Visual}Q cursors: <BS> mid-completion. #41602 2000clear_cursors () 2001api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '' , '' , '' } ) 2002command ( 'let v:errmsg = ""' ) 2003feed ( 'VggQ' ) 2004feed ( 'i a<BS>' ) 2005eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 2006feed ( '<Esc>' ) 2007eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 2008eq ( { ' ' , ' ' , ' ' } , get_lines ()) 2009 2010-- Live-mirrors after <BS> ends autocompletion; popup shows on new input. 2011clear_cursors () 2012ac_setup () 2013feed ( 'ifo' ) 2014eq ( 1 , fn . pumvisible ()) 2015feed ( '<BS>' ) 2016eq ( 1 , fn . pumvisible ()) 2017feed ( '<BS>' ) 2018eq ( 0 , fn . pumvisible ()) 2019feed ( 'fo' ) 2020eq ( 1 , fn . pumvisible ()) 2021eq ( { 'fo' , 'fo' , 'fo' } , { get_lines ()[ 4 ], get_lines ()[ 5 ], get_lines ()[ 6 ] } ) 2022feed ( '<Esc>' ) 2023eq ( { 'fo' , 'fo' , 'fo' } , { get_lines ()[ 4 ], get_lines ()[ 5 ], get_lines ()[ 6 ] } ) 2024 2025-- Live-mirrors if <BS> ends autocompletion then restarts it immediately ('autocomplete' with 2026-- printable char before the cursor). #41605 2027clear_cursors () 2028cursors ( { 'foo' , 'foobar' , 'foobarbaz' } ) 2029feed ( 'A f' ) 2030eq ( 1 , fn . pumvisible ()) 2031feed ( '<BS>' ) 2032eq ( { 'foo ' , 'foobar ' , 'foobarbaz ' } , get_lines ()) 2033feed ( ' fo' ) 2034eq ( 1 , fn . pumvisible ()) 2035eq ( { 'foo fo' , 'foobar fo' , 'foobarbaz fo' } , get_lines ()) 2036feed ( '<Esc>' ) 2037eq ( { 'foo fo' , 'foobar fo' , 'foobarbaz fo' } , get_lines ()) 2038end ) 2039 2040it ( 'InsertCharPre-driven complete() plugin (cmp-style)' , function () 2041local screen =Screen . new ( 40 , 12 ) 2042-- Typical third-party completion plugin: an InsertCharPre handler 2043-- that schedules complete() (textlock forbids calling it directly). 2044n . exec_lua ( [[ 2045vim.api.nvim_create_autocmd('InsertCharPre', { 2046callback = function() 2047vim.schedule(function() 2048if vim.fn.mode():find('i') and vim.fn.pumvisible() == 0 then 2049vim.fn.complete(1, { 'foobar', 'foolish' }) 2050end 2051end) 2052end, 2053}) 2054]] ) 2055cursors ( { '' , '' , '' } ) 2056feed ( 'if' ) 2057screen : expect ( { any ='foolish' } ) -- wait for the popup 2058feed ( '<C-y>' ) -- accept the (pre-inserted) first candidate 2059feed ( '<Esc>' ) 2060eq ( { 'foobar' , 'foobar' , 'foobar' } , get_lines ()) 2061screen : expect ( { none ='foolish' } ) -- popup closed 2062-- <Esc> with the popup still visible keeps all cursors equal too. 2063feed ( 'cc' ) 2064feed ( 'x' ) 2065screen : expect ( { any ='foolish' } ) -- wait for the new popup 2066feed ( '<Esc>' ) 2067local l =get_lines () 2068eq ( { l [ 3 ], l [ 3 ] } , { l [ 1 ], l [ 2 ] } ) 2069end ) 2070end ) 2071 2072describe ( 'visual-mode cascade' , function () 2073it ( 'failed motion mid-replay does not eat the keys after it' , function () 2074fn . setline ( 1 , { 'alpha bravo' , 'golf hotel' , 'mike november' } ) 2075feed ( 'ggVjjQ' ) -- cursor on each line; enables "q=" follow-mode 2076feed ( 'gg0' ) 2077-- The abandoned selection replays "vloh<Esc>" at each cursor ("q=" follow). The "h" fails 2078-- (col 0 after "o" swap); Visual mode must not leak into next cursor replay. 2079feed ( 'vloh<Esc>' ) 2080eq ( { 'alpha bravo' , 'golf hotel' , 'mike november' } , get_lines ()) 2081eq ( 'n' , api . nvim_get_mode (). mode ) 2082 2083-- Per-cursor behavior: "t;" fails if cursor-line has no ";". The op still applies to the 2084-- existing Visual selection. 2085clear_cursors () 2086cursors ( { 'a;b' , 'cd' , 'e;f' } , 'jQjQgg0' ) 2087feed ( 'vt;d' ) 2088eq ( { ';b' , 'd' , ';f' } , get_lines ()) 2089eq ( 2 , ncursors ()) -- The cursor where "t;" failed is kept. 2090clear_cursors () 2091cursors ( { 'a;b' , 'cd' , 'e;f' } , 'jQjQgg0' ) 2092feed ( 'vt;cX<Esc>' ) 2093eq ( { 'X;b' , 'Xd' , 'X;f' } , get_lines ()) 2094end ) 2095 2096it ( 'per-cursor selection' , function () 2097local screen =Screen . new ( 30 , 6 ) 2098cursors ( { 'longword x' , 'ab y' , 'medium z' } ) 2099-- Each cursor shows its own selection ("iw" = that cursor's word), previewed live. 2100feed ( 'viw' ) 2101screen : expect ( [[ 2102{17:longword} x | 2103{17:ab} y | 2104{17:mediu^m} z | 2105{1:~ }|*2 2106{5:-- VISUAL --} | 2107]] ) 2108eq ( 'visual' , screen . mode ) -- The UI mode ('guicursor'). #41631 2109feed ( 'e' ) 2110screen : expect ( [[ 2111{17:longword x} | 2112{17:ab y} | 2113{17:medium ^z} | 2114{1:~ }|*2 2115{5:-- VISUAL --} | 2116]] ) 2117-- <Esc>: cursors move to the selection-end, like the primary. 2118feed ( '<Esc>' ) 2119screen : expect ( [[ 2120longword {17:x} | 2121ab {17:y} | 2122medium ^z | 2123{1:~ }|*2 2124| 2125]] ) 2126eq ( 'normal' , screen . mode ) 2127end ) 2128 2129it ( 'linewise/blockwise selections' , function () 2130local screen =Screen . new ( 30 , 6 ) 2131cursors ( { 'aaaa' , 'bbbb' , 'cccc' , 'dddd' } , 'Q2j' ) 2132feed ( 'Vj' ) -- linewise: primary lines 3-4, fake lines 1-2 2133screen : expect ( [[ 2134{17:aaaa} | 2135{17:bbbb} | 2136{17:cccc} | 2137{17:^dddd} | 2138{1:~ }| 2139{5:-- VISUAL LINE --} | 2140]] ) 2141feed ( '<Esc>' ) 2142clear_cursors () 2143cursors ( { 'aaaa' , 'bbbb' , 'cccc' , 'dddd' } , 'Q2jl' ) 2144feed ( '<C-v>jl' ) -- blockwise: primary (3,1)-(4,2), fake (1,0)-(2,1) 2145screen : expect ( [[ 2146{17:aa}aa | 2147{17:bb}bb | 2148c{17:cc}c | 2149d{17:d^d}d | 2150{1:~ }| 2151{5:-- VISUAL BLOCK --} | 2152]] ) 2153end ) 2154 2155it ( 'gv reselects per-cursor #41606' , function () 2156local screen =Screen . new ( 30 , 6 ) 2157cursors ( { 'aa bb cc dd' , 'ee ff gg hh' , 'ii jj kk ll' } ) 2158feed ( 'v3l<Esc>' ) 2159-- Per-cursor '< '> marks: "gv" reselects that (absolute) area. 2160feed ( 'gv' ) 2161screen : expect ( [[ 2162{17:aa b}b cc dd | 2163{17:ee f}f gg hh | 2164{17:ii ^j}j kk ll | 2165{1:~ }|*2 2166{5:-- VISUAL --} | 2167]] ) 2168feed ( 'd' ) 2169eq ( { 'b cc dd' , 'f gg hh' , 'j kk ll' } , get_lines ()) 2170-- "." redoes a same-size region ("1v"), like Vim: not "gv" (the collapsed area). 2171feed ( '.' ) 2172eq ( { ' dd' , ' hh' , ' ll' } , get_lines ()) 2173 2174-- The per-cursor "gv" regions are shifted by edits. 2175clear_cursors () 2176cursors ( { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' } , '2jQgg' ) 2177feed ( 'Vj<Esc>' ) -- Primary: lines 1-2. Cursor: lines 3-4. 2178feed ( 'gvd' ) 2179eq ( { 'e' , 'f' } , get_lines ()) 2180-- ...and a cursor's delete shifts the primary's area. 2181clear_cursors () 2182cursors ( { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' } , 'Q3j' ) 2183feed ( 'Vj<Esc>' ) -- Cursor: lines 1-2. Primary: lines 4-5. 2184feed ( 'gvd' ) 2185eq ( { 'c' , 'f' } , get_lines ()) 2186feed ( 'gv' ) 2187screen : expect ( [[ 2188{17:c} | 2189{17:^f} | 2190{1:~ }|*3 2191{5:-- VISUAL LINE --} | 2192]] ) 2193feed ( '<Esc>' ) 2194 2195-- A cursor without a previous area (added after the selection) has nothing to reselect. 2196clear_cursors () 2197cursors ( { 'aa bb' , 'cc dd' , 'ee ff' } , 'Qj' ) 2198feed ( 'viw<Esc>' ) 2199feed ( 'jQk' ) -- Cursor on line 3: no area. 2200feed ( 'gvd' ) 2201eq ( { ' bb' , ' dd' , 'ee ff' } , get_lines ()) 2202eq ( 2 , ncursors ()) 2203clear_cursors () 2204cursors ( { 'aa bb' , 'cc dd' , 'ee ff' } , 'Qj' ) 2205feed ( 'viw<Esc>' ) 2206feed ( 'jQk' ) 2207feed ( 'gvrZ' ) -- Not Normal-mode "rZ". 2208eq ( { 'ZZ bb' , 'ZZ dd' , 'ee ff' } , get_lines ()) 2209 2210-- Per-cursor "gv" + Insert-entering cmd ("gvc…"). 2211clear_cursors () 2212cursors ( { 'aa bb' , 'cc dd' , 'ee ff' } ) 2213feed ( 'viw<Esc>' ) 2214feed ( 'gvcX<Esc>' ) 2215eq ( { 'X bb' , 'X dd' , 'X ff' } , get_lines ()) 2216-- Cursor without a previous area is skipped. 2217clear_cursors () 2218cursors ( { 'aa bb' , 'cc dd' , 'ee ff' } , 'Qj' ) 2219feed ( '<C-v>l<Esc>' ) 2220feed ( 'jQk' ) -- Cursor on line 3: no area. 2221feed ( 'gvIX<Esc>' ) 2222eq ( 'ee ff' , fn . getline ( 3 )) 2223end ) 2224 2225it ( 'selection moved by mapping via API/Lua #41956' , function () 2226local screen =Screen . new ( 30 , 6 ) 2227n . exec_lua ( function () 2228-- Extends the selection by 2 via the API, around a fed "o". 2229vim . keymap . set ( 'x' , 'gh' , function () 2230local cursor =vim . api . nvim_win_get_cursor ( 0 ) 2231vim . api . nvim_win_set_cursor ( 0 , { cursor [ 1 ], cursor [ 2 ] + 1 } ) 2232vim . cmd ( 'normal! o' ) 2233vim . api . nvim_win_set_cursor ( 0 , { cursor [ 1 ], cursor [ 2 ] + 3 } ) 2234end ) 2235-- Starts a selection with a fed "v", extends it via the API. 2236vim . keymap . set ( 'n' , 'gL' , function () 2237local cursor =vim . api . nvim_win_get_cursor ( 0 ) 2238vim . cmd ( 'normal! v' ) 2239vim . api . nvim_win_set_cursor ( 0 , { cursor [ 1 ], cursor [ 2 ] + 2 } ) 2240end ) 2241end ) 2242cursors ( { 'aaaaaaa' , 'bbbbbbb' , 'ccccccc' } ) 2243atoms_start () 2244-- Fed "o" does not describe the resulting selection, so the mapping itself is the subatom. 2245feed ( 'vgh' ) 2246screen : expect ( [[ 2247a{17:aaa}aaa | 2248b{17:bbb}bbb | 2249c{17:cc^c}ccc | 2250{1:~ }|*2 2251{5:-- VISUAL --} | 2252]] ) 2253feed ( 'd' ) 2254eq ( { 'aaaa' , 'bbbb' , 'cccc' } , get_lines ()) 2255eq ( { { type ='visual' } } , atoms_tail ( 1 , 'type' )) 2256-- Mapping that starts the selection: same. 2257feed ( 'gL' ) 2258screen : expect ( [[ 2259a{17:aaa} | 2260b{17:bbb} | 2261c{17:cc^c} | 2262{1:~ }|*2 2263{5:-- VISUAL --} | 2264]] ) 2265feed ( 'd' ) 2266eq ( { 'a' , 'b' , 'c' } , get_lines ()) 2267 2268-- Fed "gv" reselects marks set by the mapping. 2269n . exec_lua ( function () 2270vim . keymap . set ( 'x' , 'gs' , function () 2271local row , col =unpack ( vim . api . nvim_win_get_cursor ( 0 )) 2272vim . fn . setpos ( "'<" , { 0 , row , col + 1 , 0 } ) 2273vim . fn . setpos ( "'>" , { 0 , row , col + 3 , 0 } ) 2274vim . cmd . normal ( { 'gv' , bang =true } ) 2275end ) 2276end ) 2277clear_cursors () 2278cursors ( { 'aaaaaaa' , 'bbbbbbb' , 'ccccccc' } ) 2279feed ( 'vgs' ) 2280screen : expect ( [[ 2281{17:aaa}aaaa | 2282{17:bbb}bbbb | 2283{17:cc^c}cccc | 2284{1:~ }|*2 2285{5:-- VISUAL --} | 2286]] ) 2287feed ( 'd' ) 2288eq ( { 'aaaa' , 'bbbb' , 'cccc' } , get_lines ()) 2289 2290-- Mapping that ends the selection by operator and starts a new one: session is kept across 2291-- the operator. 2292n . exec_lua ( function () 2293vim . keymap . set ( 'x' , 'gl' , function () 2294vim . cmd ( 'normal! "_y' ) 2295vim . cmd ( 'normal! 2l' ) 2296vim . cmd ( 'normal! 1v' ) 2297end ) 2298end ) 2299clear_cursors () 2300cursors ( { 'aaaaaaa' , 'bbbbbbb' , 'ccccccc' } ) 2301feed ( 'vlgl' ) 2302screen : expect ( [[ 2303aa{17:aa}aaa | 2304bb{17:bb}bbb | 2305cc{17:c^c}ccc | 2306{1:~ }|*2 2307{5:-- VISUAL --} | 2308]] ) 2309feed ( 'd' ) 2310eq ( { 'aaaaa' , 'bbbbb' , 'ccccc' } , get_lines ()) 2311end ) 2312 2313it ( 'mapping that edits during the selection #42005' , function () 2314local screen =Screen . new ( 30 , 6 ) 2315-- "Shift right": the mapping edits, then reselects the moved text. 2316command ( [[xnoremap gl <Cmd>normal! xp`[1v<CR>]] ) 2317cursors ( { 'abcd' , 'efgh' , 'ijkl' } ) 2318feed ( 'vgl' ) 2319screen : expect ( [[ 2320b{17:a}cd | 2321f{17:e}gh | 2322j{17:^i}kl | 2323{1:~ }|*2 2324{5:-- VISUAL --} | 2325]] ) 2326eq ( { 'bacd' , 'fegh' , 'jikl' } , get_lines ()) 2327feed ( 'gl' ) -- span 2 continues from each cursor's own selection. 2328eq ( { 'bcad' , 'fgeh' , 'jkil' } , get_lines ()) 2329feed ( 'd' ) -- Operator completes the session as the last span. 2330eq ( { 'bcd' , 'fgh' , 'jkl' } , get_lines ()) 2331 2332-- Mapping that acts only in Visual mode ("mini.move"). 2333n . exec_lua ( function () 2334vim . keymap . set ( 'x' , 'gm' , function () 2335if vim . fn . mode () ~= 'v' then 2336return 2337end 2338vim . cmd ( 'normal! xp`[1v' ) 2339end ) 2340end ) 2341clear_cursors () 2342cursors ( { 'abcd' , 'efgh' , 'ijkl' } ) 2343feed ( 'vgm' ) 2344eq ( { 'bacd' , 'fegh' , 'jikl' } , get_lines ()) 2345end ) 2346 2347it ( 'primary-overlapping cursor is not replayed; stays synced #42025' , function () 2348-- The command may move the primary before the cascade ("yiwp")! 2349command ( 'nnoremap gm yiwp' ) -- "Multiply" the word at cursor. 2350cursors ( { 'aa' , 'bb' } , 'QjQ' ) -- Primary-overlapping cursor. 2351feed ( 'gm' ) 2352eq ( { 'aaaa' , 'bbbb' } , get_lines ()) 2353-- Primary-overlapping cursor settles on the primary's new position (the paste moved it). 2354local cur =api . nvim_win_get_cursor ( 0 ) 2355eq ( { { 0 , 2 } , { cur [ 1 ] - 1 , cur [ 2 ] } } , anchors ()) 2356 2357-- Also when the edit displaces the overlapping cursor's (right-gravity) mark. 2358for _ , place in ipairs ( { 'QjQ' , 'vipQ' } ) do 2359for _ , case in ipairs ( { 2360{ 'rx' , { 'xa' , 'xb' } } , 2361{ '~' , { 'Aa' , 'Bb' } } , 2362{ 'g~~' , { 'AA' , 'BB' } } , 2363} ) do 2364clear_cursors () 2365cursors ( { 'aa' , 'bb' } , place ) 2366feed ( case [ 1 ]) 2367eq ( case [ 2 ], get_lines (), place .. ' ' .. case [ 1 ]) 2368local cur =api . nvim_win_get_cursor ( 0 ) 2369eq ( { cur [ 1 ] - 1 , cur [ 2 ] } , anchors ()[ 2 ], place .. ' ' .. case [ 1 ]) -- Re-synced. 2370end 2371end 2372 2373-- Also for LHS-replayed mapping. 2374n . exec_lua ( function () 2375-- "rx" via API, which also samples the cursors when replayed at the other cursor. 2376_G . peek =function () 2377local row , col =unpack ( vim . api . nvim_win_get_cursor ( 0 )) 2378vim . api . nvim_buf_set_text ( 0 , row - 1 , col , row - 1 , col + 1 , { 'x' } ) 2379if row == 1 then 2380local ns =vim . api . nvim_create_namespace ( 'nvim.multicursor' ) 2381_G . seen =vim . api . nvim_buf_get_extmarks ( 0 , ns , 0 , - 1 , {} )[ 2 ] 2382end 2383end 2384end ) 2385command ( 'nnoremap gr <Cmd>lua _G.peek()<CR>' ) 2386clear_cursors () 2387cursors ( { 'aa' , 'bb' } , 'QjQ' ) 2388feed ( 'gr' ) 2389eq ( { 'xa' , 'xb' } , get_lines ()) 2390eq ( { 1 , 0 } , n . exec_lua ( 'return { _G.seen[2], _G.seen[3] }' )) 2391 2392-- Same for a Visual span: the primary sits at selection-end. 2393command ( [[xnoremap gl <Cmd>normal! xp`[1v<CR>]] ) 2394clear_cursors () 2395cursors ( { 'abcd' , 'efgh' , 'ijkl' } , 'QjQjQ' ) 2396feed ( '0vgl' ) 2397eq ( { 'bacd' , 'fegh' , 'jikl' } , get_lines ()) 2398-- Also when Visual selection moves the primary first. 2399for _ , keys in ipairs ( { 'va)d' , 'va)cX<Esc>' , 'ca)X<Esc>' } ) do 2400feed ( '<Esc>' ) 2401clear_cursors () 2402cursors ( { 'aa (bb) (cc)' , 'aa (bb) (cc)' } , 'vipQ' ) 2403feed ( keys ) 2404local x =keys : find ( 'X' ) and 'X' or '' 2405eq ( { ( 'aa %s (cc)' ): format ( x ), ( 'aa %s (cc)' ): format ( x ) } , get_lines (), keys ) 2406local cur =api . nvim_win_get_cursor ( 0 ) 2407eq ( { cur [ 1 ] - 1 , cur [ 2 ] } , anchors ()[ 2 ], keys ) -- Primary-overlapping cursor stays synced. 2408end 2409-- Motion does not cascade, so it must not dedupe the cursor under the primary. 2410clear_cursors () 2411cursors ( { 'abc' , 'def' } , 'Q' ) 2412feed ( 'l' ) 2413eq ( 1 , ncursors ()) 2414end ) 2415 2416it ( 'replays the full visual keysequence' , function () 2417cursors ( { 'one two three x' , 'aa bb cc d' } , 'Qj' ) 2418-- Select word, extend twice, delete: selection re-executes at each cursor, so the extents are 2419-- per-cursor (not a fixed-size reselect). 2420atoms_start () 2421feed ( 'viweex' ) 2422eq ( { ' x' , ' d' } , get_lines ()) 2423-- The operator is normalized ("translated"): visual "x" == "d". 2424eq ( { 'viweed' } , atoms_tail ( 1 )) 2425 2426-- A motion that fails (beeps) at primary is skipped. E.g. "j" at EOB. 2427clear_cursors () 2428cursors ( { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' } , 'QjQ4j' ) -- Cursors at lines 1-2, primary on the last. 2429feed ( 'Vjd' ) 2430eq ( { 'c' , 'd' , 'e' } , get_lines ()) 2431eq ( { 'Vd' } , atoms_tail ( 1 )) 2432end ) 2433 2434it ( 'selections opened by :normal #41705' , function () 2435local screen =Screen . new ( 30 , 6 ) 2436command ( 'nnoremap <F2> <Cmd>normal! viw<CR>' ) 2437n . exec_lua ( function () 2438vim . keymap . set ( 'x' , 'Z' , '<Cmd>normal! iw<CR>' ) 2439vim . keymap . set ( 'n' , '<F3>' , function () 2440vim . cmd . normal ( 'vZ' ) 2441end ) 2442end ) 2443atoms_start () 2444-- Each entry opens the selection from a different enclosing frame: typed cmdline, <Cmd> 2445-- mapping, Lua mapping (nested x-mapping), RPC. Result does not depend on follow-mode. 2446for i , keys in ipairs ( { ':normal! viw<CR>' , '<F2>' , '<F3>' , 'api' } ) do 2447clear_cursors () 2448cursors ( { 'longword x' , 'ab y' , 'medium z' } ) 2449feed ( i % 2 == 0 and '2q=' or '1q=' ) 2450if keys == 'api' then 2451command ( 'normal! viw' ) 2452else 2453feed ( keys ) 2454end 2455screen : expect ( [[ 2456{17:longword} x | 2457{17:ab} y | 2458{17:mediu^m} z | 2459{1:~ }|*2 2460{5:-- VISUAL --} | 2461]] ) 2462-- Preview does not advance the anchors, even when a mapping opens the selection. 2463eq ( { { 0 , 0 } , { 1 , 0 } } , anchors (), keys ) 2464feed ( 'd' ) 2465eq ( { ' x' , ' y' , ' z' } , get_lines (), keys ) 2466 2467-- The atom is the literal input: a command is captured as itself, not any keys it "feeds". 2468-- But fed input with no enclosing command (RPC) collects its own keys. 2469local expected =( { 2470[ ':normal! viw<CR>' ] =k ( ':normal! viw<NL>d' ), 2471[ '<F2>' ] =k ( '<Cmd>normal! viw<NL>d' ), 2472[ 'api' ] ='viwd' , 2473} )[ keys ] 2474if expected then 2475eq ( { expected } , atoms_tail ( 1 ), keys ) 2476else 2477t . matches ( '^\128\253g%d+\nd$' , atoms_tail ( 1 )[ 1 ]) -- "<F3>": K_LUA + mapping id. 2478end 2479 2480screen : expect ( { 2481condition =function () 2482eq ( 'normal' , screen . mode ) 2483end , 2484} ) 2485end 2486end ) 2487 2488it ( 'selections after mapping motions' , function () 2489local screen =Screen . new ( 30 , 6 ) 2490cursors ( { 'a longword x' , 'bbbb ab y' , 'cc medium z' } ) 2491command ( 'nnoremap <F2> w<Cmd>normal! viw<CR>' ) 2492atoms_start () 2493feed ( '<F2>' ) 2494-- The mapping's "w" places the selection anchors, even without follow-mode. 2495eq ( { { 0 , 2 } , { 1 , 5 } } , anchors ()) 2496screen : expect ( [[ 2497a {17:longword} x | 2498bbbb {17:ab} y | 2499cc {17:mediu^m} z | 2500{1:~ }|*2 2501{5:-- VISUAL --} | 2502]] ) 2503feed ( 'd' ) 2504eq ( { 'a x' , 'bbbb y' , 'cc z' } , get_lines ()) 2505eq ( { k ( 'w<Cmd>normal! viw<NL>d' ) } , atoms_tail ( 1 )) 2506end ) 2507 2508it ( 'refreshes a nested selection even if the primary selection is unchanged' , function () 2509local screen =Screen . new ( 30 , 5 ) 2510cursors ( { 'foo.bar tail' , 'word tail' } , 'Qj' ) 2511n . exec_lua ( function () 2512vim . keymap . set ( 'x' , 'Z' , function () 2513vim . cmd . normal ( { vim . keycode ( '<Esc>viW' ), bang =true } ) 2514end ) 2515end ) 2516feed ( 'viw' ) 2517screen : expect ( [[ 2518{17:foo}.bar tail | 2519{17:wor^d} tail | 2520{1:~ }|*2 2521{5:-- VISUAL --} | 2522]] ) 2523feed ( 'Z' ) 2524screen : expect ( [[ 2525{17:foo.bar} tail | 2526{17:wor^d} tail | 2527{1:~ }|*2 2528{5:-- VISUAL --} | 2529]] ) 2530feed ( 'd' ) 2531eq ( { ' tail' , ' tail' } , get_lines ()) 2532end ) 2533 2534it ( 'operators cascade at each cursor' , function () 2535assert_rows ( { 2536-- Vjd deletes two lines. 2537{ lines ={ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' } , keys ='Q4jVjd' , expect ={ 'c' , 'd' } } , 2538-- r replaces the selection. 2539{ lines ={ 'one two' , 'ab cd' } , keys ='QjviwrX' , expect ={ 'XXX two' , 'XX cd' } } , 2540-- Blockwise c changes the block, I inserts before it, A appends after it. 2541{ 2542lines ={ 'ab' , 'cd' , 'ef' , 'gh' } , 2543keys ='Q2j<C-v>jcX<Esc>' , 2544expect ={ 'Xb' , 'Xd' , 'Xf' , 'Xh' } , 2545} , 2546{ 2547lines ={ 'ab' , 'cd' , 'ef' , 'gh' } , 2548keys ='Q2j<C-v>jIX<Esc>' , 2549expect ={ 'Xab' , 'Xcd' , 'Xef' , 'Xgh' } , 2550} , 2551{ 2552lines ={ 'ab' , 'cd' , 'ef' , 'gh' } , 2553keys ='Q2j<C-v>jA!<Esc>' , 2554expect ={ 'a!b' , 'c!d' , 'e!f' , 'g!h' } , 2555} , 2556} ) 2557end ) 2558 2559it ( 'ESC moves cursors to selection-end' , function () 2560cursors ( { 'abc' , 'def' } , 'Qj' ) 2561feed ( 'viw<Esc>' ) 2562feed ( 'x' ) -- cascades normally; no stray visual replay 2563eq ( { 'ab' , 'de' } , get_lines ()) 2564-- Without follow-mode a plain motion ("0") stays primary-only. 2565-- But the selection itself moves every cursor to its selection-end. 2566clear_cursors () 2567cursors ( { 'aaa bbb ccc' , 'ddd eee fff' , 'ggg hhh iii' } , '4lQjQj' ) 2568feed ( 'viw<Esc>' ) 2569eq ( { { 0 , 6 } , { 1 , 6 } } , anchors ()) 2570feed ( '0viwe<Esc>' ) 2571eq ( { { 0 , 10 } , { 1 , 10 } } , anchors ()) 2572end ) 2573 2574it ( 'cursor displays at each selection end; o swaps it' , function () 2575local screen =Screen . new ( 40 , 6 ) 2576command ( 'hi MCursor guifg=NONE guibg=Red' ) 2577command ( 'hi MCursorVisual guibg=Blue' ) 2578cursors ( { 'aaa bbb ccc' , 'ddd eee fff' , 'ggg hhh iii' } , '4lQjQj' ) 2579feed ( 'vl' ) 2580screen : add_extra_attr_ids ( { 2581[ 100 ] ={ background =Screen . colors . Blue1 } , 2582} ) 2583-- The (red) display cursor sits at each selection end; the (blue) 2584-- anchor cell shows only the selection. 2585screen : expect ( [[ 2586aaa {100:b}{30:b}b ccc | 2587ddd {100:e}{30:e}e fff | 2588ggg {17:h^h}h iii | 2589{1:~ }|*2 2590{5:-- VISUAL --} | 2591]] ) 2592feed ( 'o' ) 2593screen : expect ( [[ 2594aaa {30:b}{100:b}b ccc | 2595ddd {30:e}{100:e}e fff | 2596ggg {17:^hh}h iii | 2597{1:~ }|*2 2598{5:-- VISUAL --} | 2599]] ) 2600feed ( '<Esc>' ) 2601-- Back to normal mode: the cursor highlight returns to the anchors. 2602screen : expect ( [[ 2603aaa {30:b}bb ccc | 2604ddd {30:e}ee fff | 2605ggg ^hhh iii | 2606{1:~ }|*2 2607| 2608]] ) 2609end ) 2610 2611it ( 'payload motions: f{char} and search' , function () 2612cursors ( { 'abcd,ef' , 'wxyz,gh' } , 'Qj' ) 2613feed ( 'vf,d' ) -- f-operand: replayable, cascades. 2614eq ( { 'ef' , 'gh' } , get_lines ()) 2615clear_cursors () 2616cursors ( { 'one two' , 'one two' } , 'Qj' ) 2617feed ( 'v/two<CR>d' ) -- Search payload travels in the collected keys, cascades. 2618eq ( { 'wo' , 'wo' } , get_lines ()) 2619clear_cursors () 2620-- Viewport scroll (C-E) that drags the cursor (viewport edge, 'scrolloff') changes the 2621-- selection by a viewport-dependent amount: not replayable, edits primary only. 2622local lines ={} 2623for i =1 , 30 do 2624lines [ i ] ='x' .. i 2625end 2626fn . setline ( 1 , lines ) 2627feed ( '10ggQgg' ) 2628feed ( 'V<C-e>' ) -- Drags the primary onto line 2: selection is lines 1-2. 2629feed ( 'd' ) 2630eq ( 28 , fn . line ( '$' )) -- Primary deleted 2 lines; no cascade. 2631eq ( { 'x3' , 'x4' } , { fn . getline ( 1 ), fn . getline ( 2 ) } ) 2632end ) 2633end ) 2634 2635describe ( 'q= (follow-mode)' , function () 2636it ( "'follow' option" , function () 2637-- :setlocal is the same as "q=". 2638cursors ( { 'abcd' , 'efgh' } , 'Qj' ) 2639command ( 'setlocal follow' ) 2640feed ( 'lx' ) 2641eq ( { 'acd' , 'egh' } , get_lines ()) 2642-- Buffer-local. 2643command ( 'new' ) 2644eq ( false , api . nvim_get_option_value ( 'follow' , {} )) 2645command ( 'bwipe!' ) 2646eq ( true , api . nvim_get_option_value ( 'follow' , {} )) 2647-- LHS-replay does not re-run "q=" (toggle) at each cursor. 2648clear_cursors () 2649command ( 'setlocal nofollow' ) 2650command ( 'let g:optionset = 0 | autocmd OptionSet follow let g:optionset += 1' ) 2651command ( [[nnoremap gx <Cmd>exe 'normal! q='<Bar>call setline('.', 'X'..getline('.'))<CR>]] ) 2652cursors ( { 'aa' , 'bb' } , 'Qj' ) 2653feed ( 'gx' ) 2654eq ( { 'Xaa' , 'Xbb' } , get_lines ()) 2655eq ( true , api . nvim_get_option_value ( 'follow' , {} )) 2656eq ( 1 , n . eval ( 'g:optionset' )) 2657end ) 2658 2659it ( 'cursors follow primary-cursor motions' , function () 2660cursors ( { 'abcd' , 'efgh' } , 'Q' ) 2661feed ( 'j' ) -- No cascade/follow. 2662feed ( 'q=' ) 2663feed ( 'll' ) -- Cascade, both cursors move 2 chars rightwards. 2664feed ( 'x' ) 2665eq ( { 'abd' , 'efh' } , get_lines ()) 2666-- Toggle off: "h" moves only primary, the other cursor still deletes at its unmoved position. 2667feed ( 'q=' ) 2668feed ( 'h' ) 2669feed ( 'x' ) 2670eq ( { 'ab' , 'eh' } , get_lines ()) 2671-- [count]q= forces the mode instead of toggling. 2672feed ( '1q=' ) -- Force "on". 2673feed ( '1q=' ) 2674feed ( 'h' ) 2675feed ( 'x' ) 2676eq ( { 'b' , 'h' } , get_lines ()) 2677feed ( '2q=' ) -- Force "off". 2678feed ( '2q=' ) 2679feed ( 'A!<Esc>' ) 2680feed ( 'h' ) 2681feed ( 'x' ) 2682eq ( { 'b' , '!' } , get_lines ()) 2683end ) 2684 2685it ( "ending the mc-session keeps 'follow'; Q disables it" , function () 2686-- Explicit clear. 2687cursors ( { 'aaa' , 'bbb' , 'ccc' } ) 2688feed ( 'q=' ) 2689clear_cursors () 2690eq ( true , api . nvim_get_option_value ( 'follow' , {} )) 2691 2692-- Implicit exit (cursors deduped) clear. 2693cursors ( { 'aaa' , 'bbb' , 'ccc' } ) 2694feed ( 'q=' ) 2695feed ( '9k' ) -- Every cursor clamps to line 1, onto the primary: all deduped. 2696eq ( 0 , ncursors ()) 2697eq ( true , api . nvim_get_option_value ( 'follow' , {} )) 2698feed ( 'Q' ) -- Disables follow-mode, else it would dedupe on "j". 2699eq ( false , api . nvim_get_option_value ( 'follow' , {} )) 2700feed ( 'j' ) 2701eq ( 1 , ncursors ()) 2702 2703-- During a session: Q adds a cursor at the primary, and exits follow-mode. 2704clear_cursors () 2705cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'Qj' ) 2706feed ( 'q=' ) 2707feed ( 'l' ) -- Sanity: cascades. 2708eq ( { { 0 , 1 } } , anchors ()) 2709feed ( 'Q' ) 2710eq ( false , api . nvim_get_option_value ( 'follow' , {} )) 2711feed ( 'j' ) -- No follow: nothing converges or dedupes. 2712eq ( { { 0 , 1 } , { 1 , 1 } } , anchors ()) 2713-- Partial-range clear does not end the mc-session; the remaining cursors keep "q=". 2714clear_cursors () 2715cursors ( { 'aaa' , 'bbb' , 'ccc' } ) -- Cursors on lines 1-2, primary on line 3. 2716feed ( 'q=' ) 2717n . exec_lua ( 2718[[vim.api.nvim_buf_clear_namespace(0, vim.api.nvim_create_namespace('nvim.multicursor'), 0, 1)]] 2719) 2720eq ( 1 , ncursors ()) -- Cursor 1 is gone, cursor 2 still exists. 2721feed ( 'l' ) 2722eq ( { { 1 , 1 } } , anchors ()) -- Still in follow-mode. 2723end ) 2724 2725it ( 'follows a mapping that moves the cursor via API (no motion key) #41646' , function () 2726command ( 2727'nnoremap gm <Cmd>lua local p = vim.api.nvim_win_get_cursor(0); ' 2728.. 'p[2] = p[2] + 1; vim.api.nvim_win_set_cursor(0, p)<CR>' 2729) 2730cursors ( { 'abcd' , 'efgh' , 'ijkl' } , 'QjQj' ) -- A cursor on each line. 2731feed ( 'q=' ) 2732feed ( 'gm' ) -- Every cursor moves one column right. 2733feed ( 'x' ) 2734eq ( { 'acd' , 'egh' , 'ikl' } , get_lines ()) 2735 2736-- <Cmd> mapping uses nested ":normal!" to move the cursor. #41653 2737clear_cursors () 2738command ( 'nnoremap gh <Cmd>normal! $<CR>' ) 2739cursors ( { 'a b' , 'c d' , 'e f' } , 'QjQj' ) 2740feed ( 'q=' ) 2741feed ( 'gh' ) -- Every cursor moves to EOL. 2742feed ( 'x' ) 2743eq ( { 'a ' , 'c ' , 'e ' } , get_lines ()) 2744 2745-- matchit "%" mapping (uses ":call" to move the cursor). 2746command ( 'packadd matchit' ) 2747clear_cursors () 2748cursors ( { '(aa)' , '(bb)' , '(cc)' } , 'QjQj' ) -- All cursors on "(". 2749feed ( 'q=' ) 2750feed ( '%' ) -- Every cursor jumps to its ")". 2751feed ( 'x' ) 2752eq ( { '(aa' , '(bb' , '(cc' } , get_lines ()) 2753end ) 2754 2755it ( "is decided at a mapping's first move #41836" , function () 2756-- Cursors on lines 2-4, primary line 5, follow=ON. 2757cursors ( { 'l1' , 'l2' , 'l3' , 'l4' , 'l5' } , 'jQjQjQj1q=' ) 2758-- Mapping toggles follow OFF, moves, then toggles ON: the move should NOT cascade. 2759n . exec_lua ( [[vim.keymap.set('n', '<F1>', function() vim.cmd('norm! 2q=gg1q=') end)]] ) 2760feed ( '<F1>' ) 2761eq ( { { 1 , 0 } , { 2 , 0 } , { 3 , 0 } } , anchors ()) -- cursors stay on lines 2-4 (not cascaded) 2762eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) -- primary moved to line 1 2763-- Follow is ON again (trailing "1q="): a plain motion now cascades. 2764feed ( 'jx' ) 2765eq ( { 'l1' , '2' , '3' , '4' , '5' } , get_lines ()) 2766eq ( { { 2 , 0 } , { 3 , 0 } , { 4 , 0 } } , anchors ()) -- Cursors followed to lines 3-5. 2767 2768-- Toggles as separate mapping keys (not ":norm!"): ON around the move, it cascades. 2769feed ( '2q=' ) -- follow OFF; primary line 2, cursors on lines 3-5 2770command ( 'nnoremap <F2> 1q=k2q=' ) 2771feed ( '<F2>' ) 2772eq ( { { 1 , 0 } , { 2 , 0 } , { 3 , 0 } } , anchors ()) -- Cursors followed to lines 2-4. 2773eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 2774-- OFF at the move (toggled after it): no cascade. 2775command ( 'nnoremap <F3> j1q=2q=' ) 2776feed ( '<F3>' ) 2777eq ( { { 1 , 0 } , { 2 , 0 } , { 3 , 0 } } , anchors ()) -- Cursors did not move. 2778eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 2779 2780-- Cursor-move by API in-between the toggles is not followed, even with follow=ON initially. 2781clear_cursors () 2782n . exec_lua ( [[ 2783vim.keymap.set('n', '<Up>', function() 2784local next_pos = vim.pos.cursor(0) 2785next_pos.row = math.max(next_pos.row - 1, 1) 2786vim.cmd('norm! Q') 2787vim.cmd('norm! 2q=') 2788vim.api.nvim_win_set_cursor(0, next_pos:to_cursor()) 2789vim.cmd('norm! 1q=') 2790end) 2791]] ) 2792-- Follow=OFF: cursor on line 5, primary line 4. 2793cursors ( { 'l1' , 'l2' , 'l3' , 'l4' , 'l5' } , 'G0<Up>' ) 2794eq ( { { 4 , 0 } } , anchors ()) 2795feed ( '<Up>' ) -- follow=ON ("1q="): cursor on line 4, primary line 3 2796eq ( { { 3 , 0 } , { 4 , 0 } } , anchors ()) 2797feed ( 'k' ) -- follow=ON again: cursors follow the motion 2798eq ( { { 2 , 0 } , { 3 , 0 } } , anchors ()) 2799eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 2800end ) 2801 2802it ( 'jumps (absolute motions) are not followed' , function () 2803cursors ( { 'abcd' , 'efgh' , 'ijkl' } , 'Q' ) 2804feed ( '3G' ) -- Jumps fill the jumplist; no cascade. 2805feed ( '2G' ) 2806feed ( 'q=' ) -- Follow-mode. 2807feed ( 'l' ) -- Sanity: motions cascade. 2808eq ( { { 0 , 1 } } , anchors ()) 2809feed ( '<C-o>' ) -- Jump. 2810feed ( '``' ) -- Jump. 2811feed ( 'G' ) 2812feed ( 'gg' ) 2813feed ( 'L' ) 2814eq ( { { 0 , 1 } } , anchors ()) 2815 2816-- Jump from a mapping (no LHS-replay fallback). #41995 2817command ( 'nnoremap <Down> ]C' ) 2818clear_cursors () 2819cursors ( { 'a' , 'b' , 'c' , 'd' } , 'QjQjQj' ) 2820feed ( '1q=' ) 2821eq ( { { 0 , 0 } , { 1 , 0 } , { 2 , 0 } } , anchors ()) 2822eq ( 4 , fn . line ( '.' )) 2823for _ , line in ipairs ( { 1 , 2 , 3 , 1 } ) do 2824feed ( '<Down>' ) 2825eq ( line , fn . line ( '.' )) -- The primary jumps to the next cursor (wraps)... 2826eq ( { { 0 , 0 } , { 1 , 0 } , { 2 , 0 } } , anchors ()) -- ...the cursors stay put. 2827end 2828end ) 2829 2830it ( '"*" follows per-cursor (its own word); keeps the primary search pattern' , function () 2831cursors ( { 'foo bar' , 'bar baz' , 'foo' , 'bar' , 'baz' } , 'Qj0' ) 2832feed ( 'q=' ) 2833feed ( '*' ) -- Each cursor goes to the next occurrence of its own word. 2834eq ( { { 2 , 0 } } , anchors ()) 2835eq ( { 4 , 1 } , { fn . line ( '.' ), fn . col ( '.' ) } ) 2836eq ( [[\<bar\>]] , fn . getreg ( '/' )) 2837feed ( 'n' ) -- The primary's pattern, at each cursor. 2838eq ( { { 3 , 0 } } , anchors ()) 2839eq ( { 1 , 5 } , { fn . line ( '.' ), fn . col ( '.' ) } ) 2840end ) 2841 2842it ( 'cursors follow j/k, gj/gk, arrow keys, and $' , function () 2843cursors ( { 'a1' , 'a2' , 'a3' , 'b1' , 'b2' , 'b3' } , 'Q' ) 2844feed ( '3j' ) -- Cursors at "a1" and "b1". 2845feed ( 'q=' ) 2846feed ( 'j' ) -- Both cursors move down. 2847feed ( 'x' ) 2848eq ( { 'a1' , '2' , 'a3' , 'b1' , '2' , 'b3' } , get_lines ()) 2849feed ( 'k' ) -- Both cursors move back up. 2850feed ( 'x' ) 2851eq ( { '1' , '2' , 'a3' , '1' , '2' , 'b3' } , get_lines ()) 2852feed ( 'gj' ) -- Display-line motions follow too. 2853feed ( 'x' ) 2854eq ( { '1' , '' , 'a3' , '1' , '' , 'b3' } , get_lines ()) 2855feed ( 'gk' ) 2856feed ( 'x' ) 2857eq ( { '' , '' , 'a3' , '' , '' , 'b3' } , get_lines ()) 2858-- Arrow keys follow. 2859clear_cursors () 2860cursors ( { 'abcd' , 'efgh' , 'ijkl' , 'mnop' } , 'Q2j' ) -- Cursors at "abcd" and "ijkl". 2861feed ( 'q=' ) 2862feed ( '<Down>' ) 2863feed ( '<Right>' ) 2864feed ( 'x' ) 2865eq ( { 'abcd' , 'egh' , 'ijkl' , 'mop' } , get_lines ()) 2866feed ( '<Up>' ) 2867feed ( '<Left>' ) 2868feed ( 'x' ) 2869eq ( { 'bcd' , 'egh' , 'jkl' , 'mop' } , get_lines ()) 2870feed ( 'q=' ) 2871-- $ moves each cursor to its own EOL. 2872clear_cursors () 2873cursors ( { 'abc' , 'defgh' } , 'Qj' ) 2874feed ( 'q=' ) 2875feed ( '$' ) 2876feed ( 'x' ) 2877eq ( { 'ab' , 'defg' } , get_lines ()) 2878 2879-- A motion that fails (beeps) at primary is not replayed. E.g. "j" at EOB. 2880clear_cursors () 2881cursors ( { 'a' , 'b' , 'c' } , 'QjQj' ) -- Cursors at lines 1-2, primary on the last line. 2882feed ( 'q=' ) 2883feed ( 'j' ) 2884feed ( 'x' ) 2885eq ( { '' , '' , '' } , get_lines ()) 2886-- "0" at col 0 does not move the primary, but also does not fail/beep, so it cascades. 2887clear_cursors () 2888cursors ( { 'abc' , 'def' } , 'llQj0' ) -- Cursor at column 2, primary at column 0. 2889feed ( 'q=' ) 2890feed ( '0' ) 2891feed ( 'x' ) 2892eq ( { 'bc' , 'ef' } , get_lines ()) 2893end ) 2894 2895it ( 'cursors follow mapped motions (nnoremap j gj)' , function () 2896command ( 'nnoremap j gj' ) 2897cursors ( { 'a1' , 'a2' , 'b1' , 'b2' } , 'Q2j' ) 2898feed ( 'q=' ) 2899feed ( 'j' ) 2900feed ( 'x' ) 2901eq ( { 'a1' , '2' , 'b1' , '2' } , get_lines ()) 2902-- Expr-mapping results follow the same way. 2903command ( [[nnoremap <expr> j v:count == 0 ? 'gk' : 'k']] ) 2904feed ( 'j' ) 2905feed ( 'x' ) 2906eq ( { '1' , '2' , '1' , '2' } , get_lines ()) 2907-- No follow: a motion mapping does not cascade; the "x" does. 2908feed ( 'q=' ) 2909feed ( 'j' ) -- gk: moves only the primary. 2910feed ( 'x' ) 2911eq ( { '' , '' , '1' , '2' } , get_lines ()) 2912end ) 2913 2914it ( 'q= while macro-recording does not toggle' , function () 2915fn . setline ( 1 , { 'a1' , 'a2' , 'b1' , 'b2' } ) 2916feed ( 'gg0' ) 2917feed ( 'Q' ) -- Cursor placed before recording starts. 2918feed ( 'qa' ) -- Start recording. 2919feed ( '2j' ) 2920feed ( 'q=' ) -- q stops recording; = is a pending operator... 2921feed ( '<Esc>' ) 2922feed ( 'j' ) 2923feed ( 'x' ) 2924-- Follow did not toggle. 2925eq ( { '1' , 'a2' , 'b1' , '2' } , get_lines ()) 2926end ) 2927 2928it ( 'per-cursor curswant is kept over short lines' , function () 2929fn . setline ( 1 , { 'ABCDEF' , 'xy' , 'GHIJKL' , 'MNOPQR' , 'zw' , 'STUVWX' } ) 2930feed ( 'gg04l' ) -- Cursor at (1,4). 2931feed ( 'Q' ) 2932feed ( '3jhh' ) -- Primary at (4,2), a different column. 2933feed ( 'q=' ) 2934feed ( 'jj' ) -- Over the short lines: each cursor keeps its column. 2935feed ( 'q=' ) 2936feed ( 'x' ) 2937eq ( { 'ABCDEF' , 'xy' , 'GHIJL' , 'MNOPQR' , 'zw' , 'STVWX' } , get_lines ()) 2938end ) 2939end ) 2940 2941describe ( 'parity' , function () 2942it ( 'mapped command cascades its resolved atom' , function () 2943command ( 'nnoremap ,d dw' ) 2944cursors ( { 'one two aa bb' , 'three four cc dd' } , 'Qj' ) 2945atoms_start () 2946feed ( ',d' ) 2947eq ( { 'two aa bb' , 'four cc dd' } , get_lines ()) 2948-- With mcursors: still exactly one CmdAtom event; cascade replays do not emit CmdAtoms. 2949eq ( 1 , # atoms ()) 2950-- Operator + insert mapping: also one unit. 2951command ( 'nnoremap ,x dwiFOO <esc>' ) 2952feed ( ',x' ) 2953eq ( { 'FOO aa bb' , 'FOO cc dd' } , get_lines ()) 2954end ) 2955 2956it ( 'mapping with edits and motions cascades as one unit' , function () 2957-- Split the line at the cursor, ending at the EOL of the first half. 2958command ( 'nnoremap gj i<c-j><esc>k$' ) 2959cursors ( { 'aaa bbb' , 'ccc ddd' , 'eee fff' } , '4lQjQj' ) 2960atoms_start () 2961feed ( 'gj' ) 2962eq ( { 'aaa ' , 'bbb' , 'ccc ' , 'ddd' , 'eee ' , 'fff' } , get_lines ()) 2963-- The mapping is the atom: exactly one CmdAtom event. Never re-resolved. 2964local evs =atoms () 2965eq ( 1 , # evs ) 2966eq ( { type ='mapping' , lhs ='gj' , keys =k ( '1i<Esc>i<NL><Esc>k$' ), changed =true } , { 2967type =evs [ 1 ]. type , 2968lhs =evs [ 1 ]. lhs , 2969keys =evs [ 1 ]. keys , 2970changed =evs [ 1 ]. changed , 2971} ) 2972-- `atoms` is non-empty iff the atom is a composite of more than one command; 2973eq ( { 2974-- Spans display as "insert" (cascade-internal type). 2975{ type ='insert' , keys =k ( '1i<Esc>' ) } , 2976{ type ='insert' , keys =k ( 'i<NL><Esc>' ) } , 2977{ type ='motion' , keys ='k' } , 2978{ type ='motion' , keys ='$' } , 2979} , subatoms ( evs [ 1 ], 'type' , 'keys' )) 2980eq ( { 'k' , false } , { evs [ 1 ]. atoms [ 3 ]. cmd , evs [ 1 ]. atoms [ 3 ]. changed } ) 2981-- The mapping's motions (k$) cascade too, even without "q=", because the mapping edits. 2982feed ( 'x' ) 2983eq ( { 'aaa' , 'bbb' , 'ccc' , 'ddd' , 'eee' , 'fff' } , get_lines ()) 2984end ) 2985 2986it ( 'live insert cascade emits one whole-session atom' , function () 2987cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 2988atoms_start () 2989-- Typed key-by-key (event-loop pulses between keys): cascades span-by-span, yet emits one 2990-- insert-session atom. Spans are cascade-internal, never CmdAtom events. 2991feed ( 'i' ) 2992n . poke_eventloop () 2993feed ( 'X' ) 2994n . poke_eventloop () 2995feed ( 'Y' ) 2996n . poke_eventloop () 2997feed ( '<Esc>' ) 2998local evs =atoms () 2999eq ( 1 , # evs ) 3000eq ( { type ='insert' , text ='XY' , keys =k ( '1iXY<Esc>' ) } , { 3001type =evs [ # evs ]. type , 3002text =evs [ # evs ]. text , 3003keys =evs [ # evs ]. keys , 3004} ) 3005-- Session marks (preview regions, trackers) do not leak: a second session adds none. 3006local function session_marks () 3007return n . exec_lua ( [[ 3008local ns = vim.api.nvim_get_namespaces()['nvim.multicursor._session'] 3009return ns and #vim.api.nvim_buf_get_extmarks(0, ns, 0, -1, {}) or 0 3010]] ) 3011end 3012local marks =session_marks () 3013feed ( 'i' ) 3014n . poke_eventloop () 3015feed ( 'Z' ) 3016n . poke_eventloop () 3017feed ( '<Esc>' ) 3018eq ( marks , session_marks ()) 3019-- A Visual-entered session keeps its "visual" type: each nested span-replay bracket owns 3020-- its own InsSession, so it cannot clobber the primary session's `vis`. 3021clear_cursors () 3022cursors ( { 'alpha one' , 'beta two' } , 'Qj' ) 3023feed ( 'viwcX<Esc>' ) 3024eq ( { 'X one' , 'X two' } , get_lines ()) 3025eq ( 'visual' , atom_last (). type ) 3026end ) 3027 3028it ( 'InsertEnter/InsertLeave/TextChanged(I) fire once per action' , function () 3029command ( 'let [g:ie, g:il, g:tci, g:tc] = [0, 0, 0, 0]' ) 3030command ( 'autocmd InsertEnter * let g:ie += 1' ) 3031command ( 'autocmd InsertLeave * let g:il += 1' ) 3032command ( 'autocmd TextChangedI * let g:tci += 1' ) 3033command ( 'autocmd TextChanged * let g:tc += 1' ) 3034cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 3035-- Drain input between keys: TextChanged(I) fires only at idle. 3036-- The expected counts below are exactly what this sequence produces WITHOUT multicursors. 3037feed ( 'i' ) 3038n . poke_eventloop () 3039feed ( 'X' ) 3040n . poke_eventloop () 3041feed ( 'Y' ) 3042n . poke_eventloop () 3043feed ( '<Esc>' ) 3044n . poke_eventloop () 3045eq ( { 'XYaaa' , 'XYbbb' } , get_lines ()) 3046eq ( 1 , api . nvim_get_var ( 'ie' )) 3047eq ( 1 , api . nvim_get_var ( 'il' )) 3048eq ( 2 , api . nvim_get_var ( 'tci' )) -- Once per typed char, not per cursor. 3049eq ( 1 , api . nvim_get_var ( 'tc' )) -- Once for the whole session. 3050end ) 3051 3052it ( 'InsertCharPre fires once per typed char, result applies everywhere' , function () 3053command ( 'let g:icp = 0' ) 3054command ( 'autocmd InsertCharPre * let g:icp += 1 | let v:char = toupper(v:char)' ) 3055cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 3056feed ( 'ixy<Esc>' ) 3057eq ( { 'XYaaa' , 'XYbbb' } , get_lines ()) 3058eq ( 2 , api . nvim_get_var ( 'icp' )) 3059end ) 3060 3061it ( 'TextYankPost fires per cursor with per-cursor contents' , function () 3062command ( 'let g:yanks = []' ) 3063command ( 3064'autocmd TextYankPost * let g:yanks += [[v:event.regcontents, luaeval("vim.api.nvim__mcursor_cascading()")]]' 3065) 3066cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 3067feed ( 'yy' ) 3068-- The primary's own yank fires first and is not a replay. 3069eq ( { { { 'bbb' } , false } , { { 'aaa' } , true } } , api . nvim_get_var ( 'yanks' )) 3070-- The primary's registers win, it is the effective yank. 3071eq ( 'bbb\n' , fn . getreg ( '"' )) 3072end ) 3073end ) 3074 3075describe ( 'yank DWIM (concat on exit)' , function () 3076it ( 'joins per-cursor yanks (document order) into the register on exit' , function () 3077cursors ( { 'foo x' , 'bar y' , 'baz z' } , 'Qj0Q' ) 3078feed ( 'j0' ) -- Cursors on lines 1,2; primary on line 3 (yanked first). 3079feed ( 'yiw' ) 3080-- During multicursor, the register is the primary's own yank. 3081eq ( 'baz' , fn . getreg ( '"' )) 3082clear_cursors () -- Exit: concat, in document-order (not yank-order). 3083eq ( 'foo\nbar\nbaz\n' , fn . getreg ( '"' )) 3084eq ( 'V' , fn . getregtype ( '"' )) -- Linewise. 3085end ) 3086 3087it ( 'a named register concatenates, an untouched one is preserved' , function () 3088fn . setreg ( 'z' , 'PRESET' ) -- 'z' is untouched during multicursor. 3089cursors ( { 'foo' , 'bar' } , 'Qj0' ) 3090feed ( '"ayiw' ) 3091clear_cursors () 3092eq ( 'foo\nbar\n' , fn . getreg ( 'a' )) -- The used register is joined. 3093eq ( 'PRESET' , fn . getreg ( 'z' )) -- An untouched register is not tripled. 3094end ) 3095 3096it ( 'last-write-wins: delete after yank yields the deletes' , function () 3097cursors ( { 'aa' , 'bb' } , 'Qj0' ) 3098feed ( 'yl' ) -- Yank a char into '"'. 3099feed ( 'x' ) -- Delete a char into '"' (overwrites, per register semantics). 3100clear_cursors () 3101eq ( 'a\nb\n' , fn . getreg ( '"' )) -- The deletes, not the yanks. 3102end ) 3103end ) 3104 3105describe ( 'options' , function () 3106it ( "'textwidth' auto-wrap applies at each cursor" , function () 3107api . nvim_set_option_value ( 'textwidth' , 10 , {} ) 3108cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 3109feed ( 'Awww xxx yyy zzz<Esc>' ) 3110local lines =get_lines () 3111-- The text of both cursors wrapped the same way (same number of lines each). 3112eq ( 0 , # lines % 2 ) 3113local half =# lines / 2 3114for i =1 , half do 3115local a =lines [ i ]: gsub ( '^aaa' , '' ) 3116local b =lines [ half + i ]: gsub ( '^bbb' , '' ) 3117eq ( a , b ) 3118end 3119-- The primary's line actually wrapped. 3120eq ( true , # lines > 2 ) 3121end ) 3122end ) 3123 3124describe ( 'undo' , function () 3125it ( 'redo (CTRL-R) restores the cursors to their post-edit positions' , function () 3126cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'QjQj$' ) 3127feed ( 'IX <Esc>' ) 3128eq ( { 'X aaa' , 'X bbb' , 'X ccc' } , get_lines ()) 3129eq ( { { 0 , 1 } , { 1 , 1 } } , anchors ()) 3130feed ( 'u' ) 3131eq ( { 'aaa' , 'bbb' , 'ccc' } , get_lines ()) 3132eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 3133feed ( '<C-r>' ) 3134eq ( { 'X aaa' , 'X bbb' , 'X ccc' } , get_lines ()) 3135-- Splice adjustment alone would drift the marks to col 2: the 3136-- explicit post-session positions must be restored. 3137eq ( { { 0 , 1 } , { 1 , 1 } } , anchors ()) 3138-- The primary restores to its recorded post-edit position too (the undo header's cursor is 3139-- the PRE-change position, it serves undo), in sync with the mcursors. 3140eq ( { 3 , 1 } , api . nvim_win_get_cursor ( 0 )) 3141feed ( 'x' ) 3142eq ( { 'Xaaa' , 'Xbbb' , 'Xccc' } , get_lines ()) 3143end ) 3144 3145it ( 'uu undoes two cascaded edits step by step' , function () 3146cursors ( { 'abc' , 'def' } , 'Q' ) 3147feed ( 'jx' ) 3148eq ( { 'bc' , 'ef' } , get_lines ()) 3149feed ( 'x' ) 3150eq ( { 'c' , 'f' } , get_lines ()) 3151feed ( 'u' ) 3152eq ( { 'bc' , 'ef' } , get_lines ()) 3153feed ( 'u' ) 3154eq ( { 'abc' , 'def' } , get_lines ()) 3155feed ( '<C-r><C-r>' ) 3156eq ( { 'c' , 'f' } , get_lines ()) 3157-- Counted undo (2u) likewise treats each cascade as one change. 3158feed ( '2u' ) 3159eq ( { 'abc' , 'def' } , get_lines ()) 3160end ) 3161 3162it ( 'bulk undo across a live insert then a cascaded delete (one step each)' , function () 3163cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'QjQj0' ) 3164feed ( 'IX<Esc>' ) -- live insert at all cursors 3165eq ( { 'Xaaa' , 'Xbbb' , 'Xccc' } , get_lines ()) 3166feed ( 'x' ) -- cascaded delete at all cursors 3167eq ( { 'aaa' , 'bbb' , 'ccc' } , get_lines ()) 3168feed ( 'u' ) -- revert the delete everywhere 3169eq ( { 'Xaaa' , 'Xbbb' , 'Xccc' } , get_lines ()) 3170feed ( 'u' ) -- revert the insert everywhere 3171eq ( { 'aaa' , 'bbb' , 'ccc' } , get_lines ()) 3172eq ( 3 , fn . line ( '.' )) -- primary placement after undoing a live insert 3173end ) 3174 3175it ( 'one undo reverts a per-cursor newline-insert #41822' , function () 3176-- A newline shifts the line count between the per-cursor replays; single "u" restores it. 3177cursors ( { 'aa' , 'bb' , 'cc' } , 'Qjj' ) 3178feed ( 'A{<CR><Esc>' ) -- Append + newline at cursor (line 1) and primary (line 3). 3179eq ( { 'aa{' , '' , 'bb' , 'cc{' , '' } , get_lines ()) 3180feed ( 'u' ) 3181eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 3182feed ( '<C-r>' ) 3183eq ( { 'aa{' , '' , 'bb' , 'cc{' , '' } , get_lines ()) 3184-- "o" opens the line before Insert starts: the range still covers it. 3185clear_cursors () 3186cursors ( { 'aa' , 'bb' , 'cc' } , 'Qjj' ) 3187feed ( 'ox<Esc>' ) 3188eq ( { 'aa' , 'x' , 'bb' , 'cc' , 'x' } , get_lines ()) 3189feed ( 'u' ) 3190eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 3191end ) 3192 3193it ( 'empty insert-session does not add undo state #41883' , function () 3194cursors ( { 'abc' , 'def' , 'ghi' } , 'Qj' ) 3195local seq =fn . undotree (). seq_last 3196feed ( 'i<Esc>' ) 3197eq ( seq , fn . undotree (). seq_last ) 3198feed ( 'a<Esc>' ) -- Move the cursors (records extmark undo), but don't edit. 3199eq ( seq , fn . undotree (). seq_last ) 3200-- In a mapping the session shares the mapping's undo state: only its own entry is dropped. 3201command ( 'nnoremap <F5> xi<Esc>' ) 3202feed ( '<F5>' ) 3203eq ( { 'bc' , 'ef' , 'ghi' } , get_lines ()) 3204eq ( seq + 1 , fn . undotree (). seq_last ) 3205feed ( 'u' ) 3206eq ( { 'abc' , 'def' , 'ghi' } , get_lines ()) 3207-- After an undo, the redo branch survives. 3208feed ( 'i<Esc>' ) 3209eq ( seq + 1 , fn . undotree (). seq_last ) 3210feed ( '<C-r>' ) 3211eq ( { 'bc' , 'ef' , 'ghi' } , get_lines ()) 3212end ) 3213 3214it ( 'u/CTRL-R ping-pong toggles without drift' , function () 3215-- Repeated undo/redo of one cascade must stabilize (no extmark drift). 3216cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'QjQ' ) 3217feed ( 'jx' ) 3218eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 3219for _ =1 , 2 do 3220feed ( 'u' ) 3221eq ( { 'aaa' , 'bbb' , 'ccc' } , get_lines ()) 3222eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 3223feed ( '<C-r>' ) 3224eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 3225eq ( { { 0 , 0 } , { 1 , 0 } } , anchors ()) 3226end 3227end ) 3228 3229it ( 'undo steps across the mc-enter boundary' , function () 3230-- Q is placement, not an edit: the undo tree spans pre-mc and in-mc 3231-- edits with no extra step in between. 3232fn . setline ( 1 , { 'xxx' , 'yyy' } ) 3233feed ( 'gg0x' ) 3234eq ( { 'xx' , 'yyy' } , get_lines ()) 3235feed ( 'Q' ) 3236feed ( 'jx' ) 3237eq ( { 'x' , 'yy' } , get_lines ()) 3238feed ( 'u' ) 3239eq ( { 'xx' , 'yyy' } , get_lines ()) 3240feed ( 'u' ) 3241eq ( { 'xxx' , 'yyy' } , get_lines ()) 3242end ) 3243 3244it ( 'one u/CTRL-R reverts a whole macro (@q) cascade' , function () 3245-- "@x" cascades as ONE unit, so it is one undo block. 3246fn . setreg ( 'q' , 'iX\027' ) 3247cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 3248feed ( '@q' ) 3249eq ( { 'Xaaa' , 'Xbbb' } , get_lines ()) 3250feed ( 'u' ) 3251eq ( { 'aaa' , 'bbb' } , get_lines ()) 3252feed ( '<C-r>' ) 3253eq ( { 'Xaaa' , 'Xbbb' } , get_lines ()) 3254end ) 3255 3256it ( 'u undoes a visual-mode cascade at all cursors' , function () 3257cursors ( { 'abc def' , 'ghi jkl' } , 'Qj' ) 3258feed ( 'viwd' ) 3259eq ( { ' def' , ' jkl' } , get_lines ()) 3260feed ( 'u' ) 3261eq ( { 'abc def' , 'ghi jkl' } , get_lines ()) 3262-- The cursors are back at their pre-edit positions. 3263feed ( 'x' ) 3264eq ( { 'bc def' , 'hi jkl' } , get_lines ()) 3265end ) 3266 3267it ( 'g-/g+ (time-travel) exits multicursor mode' , function () 3268-- Out of scope forever (mcursor.md): time-travel jumps across cascade 3269-- boundaries, where per-cursor state is meaningless: all cursors are 3270-- removed instead. 3271cursors ( { 'aaa' , 'bbb' } , 'Q' ) 3272feed ( 'jx' ) 3273eq ( { 'aa' , 'bb' } , get_lines ()) 3274eq ( 1 , ncursors ()) 3275feed ( 'g-' ) 3276eq ( 0 , ncursors ()) 3277eq ( { 'aaa' , 'bbb' } , get_lines ()) -- one step back: the whole cascade 3278feed ( 'g+' ) 3279eq ( 0 , ncursors ()) -- still out of multicursor mode 3280eq ( { 'aa' , 'bb' } , get_lines ()) 3281feed ( 'Q' ) -- a new session starts cleanly 3282eq ( 1 , ncursors ()) 3283end ) 3284 3285it ( 'u restores the buffer but NOT the registers (Vim parity)' , function () 3286-- Undo never restores registers. 3287cursors ( { 'foo x' , 'bar y' } , 'Qj0' ) 3288feed ( 'diw' ) -- each cursor deletes its word into its own register 3289eq ( { ' x' , ' y' } , get_lines ()) 3290feed ( 'u' ) 3291eq ( { 'foo x' , 'bar y' } , get_lines ()) 3292-- The primary's register still holds its deletion... 3293eq ( 'bar' , fn . getreg ( '"' )) 3294-- ...and the per-cursor values persist too: exiting concatenates them 3295-- (yank DWIM) as if the undo never happened. 3296clear_cursors () 3297eq ( 'foo\nbar\n' , fn . getreg ( '"' )) 3298end ) 3299end ) 3300 3301describe ( 'same-line cursors' , function () 3302it ( 'two cursors on one line edit at their own columns' , function () 3303cursors ( { 'abcdef' } , 'Q4l' ) 3304feed ( 'x' ) 3305eq ( { 'bcdf' } , get_lines ()) 3306feed ( 'x' ) 3307eq ( { 'cd' } , get_lines ()) 3308end ) 3309 3310it ( 'linewise op with two cursors on one line applies once' , function () 3311pending ( 'policy: dedup linewise ops for same-line cursors (mcursor.md)' ) 3312end ) 3313 3314it ( 'coincident cursors merge' , function () 3315cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'QjQ' ) 3316feed ( 'q=' ) 3317feed ( '9j' ) -- All cursors to the last line. 3318feed ( 'q=' ) 3319feed ( 'x' ) -- One deletion, not three. 3320eq ( { 'aaa' , 'bbb' , 'cc' } , get_lines ()) 3321end ) 3322end ) 3323 3324describe ( 'multibyte' , function () 3325it ( 'cursor highlight covers a double-width char' , function () 3326local screen =Screen . new ( 20 , 4 ) 3327cursors ( { '日本語' , '中文字' } , 'Qj' ) 3328screen : expect ( [[ 3329{17:日}本語 | 3330^中文字 | 3331{1:~ }| 3332| 3333]] ) 3334end ) 3335end ) 3336 3337describe ( 'terminal multiple-cursors protocol' , function () 3338local exec_lua =n . exec_lua 3339 3340before_each ( function () 3341-- Stub the UI channel: record emitted sequences instead of sending. 3342exec_lua ( [[ 3343_G.sent = {} 3344vim.api.nvim_ui_send = function(s) 3345table.insert(_G.sent, s) 3346end 3347]] ) 3348end ) 3349 3350--- Runs detect() and replies to its support query with the given TermResponse `sequence`. 3351local function detect ( sequence ) 3352exec_lua (( [[ 3353require('vim._core.mcursor').detect({ chan = 1 }) 3354vim.api.nvim_exec_autocmds('TermResponse', { 3355data = { sequence = %q, chan = 1 }, 3356}) 3357]] ): format ( sequence )) 3358end 3359 3360it ( 'enabled only if the terminal reply lists shape 29' , function () 3361detect ( '\027[>1;2;3 q' ) -- no shape 29 3362-- The support query was sent, but no cursor updates follow. 3363eq ( { '\027[> q' } , exec_lua ( 'return _G.sent' )) 3364feed ( 'Q' ) 3365exec_lua ( 'vim.wait(10)' ) 3366eq ( { '\027[> q' } , exec_lua ( 'return _G.sent' )) 3367end ) 3368 3369it ( 'displays multicursors as terminal cursors' , function () 3370local screen =Screen . new ( 30 , 6 ) 3371detect ( '\027[>1;2;3;29;30;40;100;101 q' ) 3372 3373cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'QjQ' ) 3374exec_lua ( 'vim.wait(10)' ) -- drain the scheduled refresh 3375-- Clear-all, text color (30), cursor color (40), then positions (29). 3376local sent =exec_lua ( 'return _G.sent' ) 3377t . matches ( 3378'^\027%[>0;4 q\027%[>30;2:%d+:%d+:%d+ q\027%[>40;2:%d+:%d+:%d+ q\027%[>29;2:1:1;2:2:1 q$' , 3379sent [ # sent ] 3380) 3381 3382-- The cell-highlight fallback is suppressed (no {17:} on line 1). 3383screen : expect ( [[ 3384aaa | 3385^bbb | 3386ccc | 3387{1:~ }|*2 3388| 3389]] ) 3390 3391-- Removing all cursors clears the terminal cursors. 3392clear_cursors () 3393exec_lua ( 'vim.wait(10)' ) 3394sent =exec_lua ( 'return _G.sent' ) 3395eq ( '\027[>0;4 q' , sent [ # sent ]) 3396 3397-- tty_cursors(false) restores the cell-highlight fallback. 3398feed ( 'Q' ) 3399exec_lua ( "require('vim._core.mcursor').tty_cursors(false)" ) 3400screen : expect ( [[ 3401aaa | 3402{17:^b}bb | 3403ccc | 3404{1:~ }|*2 3405| 3406]] ) 3407end ) 3408 3409it ( 'displays the cursors of every visible window (splits)' , function () 3410local _ =Screen . new ( 30 , 9 ) 3411detect ( '\027[>1;2;3;29;30;40;100;101 q' ) 3412fn . setline ( 1 , { 'aaa' , 'bbb' } ) 3413feed ( 'gg0Q' ) -- cursor in buffer 1 3414command ( 'split | enew' ) -- top window: a second buffer 3415fn . setline ( 1 , { 'xxx' , 'yyy' } ) 3416feed ( 'gg0Q' ) -- cursor in buffer 2 3417exec_lua ( 'vim.wait(10)' ) 3418-- The last sequence draws BOTH cursors: buffer 2's in the focused top 3419-- window, and buffer 1's in the (unfocused) bottom window. 3420local sent =exec_lua ( 'return _G.sent' ) ---@type string[] 3421local seq =sent [ # sent ] 3422local win1 =fn . win_getid ( fn . winnr ( 'j' )) 3423local pos1 =fn . screenpos ( win1 , 1 , 1 ) 3424t . ok ( 3425seq : find (( '2:%d:%d' ): format ( pos1 . row , pos1 . col ), 1 , true ) ~= nil , 3426'buf1 cursor drawn' , 3427seq 3428) 3429local pos2 =fn . screenpos ( 0 , 1 , 1 ) 3430t . ok ( 3431seq : find (( '2:%d:%d' ): format ( pos2 . row , pos2 . col ), 1 , true ) ~= nil , 3432'buf2 cursor drawn' , 3433seq 3434) 3435end ) 3436end ) 3437 3438describe ( "'operatorfunc' (g@)" , function () 3439it ( 'VISUAL-mode surround ("S") cascades' , function () 3440pending ( 'visual ":" LHS-replay: the visual selection differs per cursor; TODO' ) 3441-- Minimal vim-surround "S": VSurround is an Ex command plus a getchar() 3442-- payload: `:<C-U>call <SID>opfunc(visualmode(),...)<CR>` + the wrap char. 3443n . exec ( [=[ 3444function! VisualSurround() abort 3445let c = nr2char(getchar()) 3446let [l1, c1] = getpos("'<")[1:2] 3447let [l2, c2] = getpos("'>")[1:2] 3448call setpos('.', [0, l2, c2, 0]) 3449exe "normal! a" . c 3450call setpos('.', [0, l1, c1, 0]) 3451exe "normal! i" . c 3452endfunction 3453xnoremap <silent> S :<C-U>call VisualSurround()<CR> 3454]=] ) 3455cursors ( { 'foo one' , 'bar two' } , 'Qj0' ) 3456feed ( 'viwS"' ) 3457-- Each cursor's own word is wrapped (per-cursor extents, like "viwd"). 3458eq ( { '"foo" one' , '"bar" two' } , get_lines ()) 3459end ) 3460 3461it ( 'surround plugin (w/ getchar() payload) cascades; one u/CTRL-R reverts' , function () 3462-- The op edits each cursor's region through :normal + register juggling (a full 3463-- exec_normal() per cursor). 3464n . exec ( t_atom . minisurround_vim ) 3465cursors ( { 'alpha beta' , 'gamma delta' , 'epsilon zeta' } , 'Qj0Qj0' ) 3466atoms_start () 3467feed ( 'ysiw"' ) 3468-- The atom is the redobuff plus the getchar()'d payload: the replayed 3469-- opfunc reads the same wrap char. 3470eq ( { 'g@iw"' } , atoms_tail ( 1 )) 3471eq ( { '"alpha" beta' , '"gamma" delta' , '"epsilon" zeta' } , get_lines ()) 3472-- The whole cascade (primary + replays) is ONE undo step: a single 3473-- u/CTRL-R reverts or reapplies it at every cursor. 3474feed ( 'u' ) 3475eq ( { 'alpha beta' , 'gamma delta' , 'epsilon zeta' } , get_lines ()) 3476feed ( '<C-r>' ) 3477eq ( { '"alpha" beta' , '"gamma" delta' , '"epsilon" zeta' } , get_lines ()) 3478end ) 3479 3480it ( 'no-effect operator (aborted "ysa[") does not cascade' , function () 3481-- vim-surround "ysa[" whose surround char is <Esc>/CTRL-C: a redoable g@ whose opfunc does 3482-- nothing. Its "a[" textobject jumps EVERY cursor to the same "[", so a cascade would 3483-- collapse them (dedupe). No edit and no register write = no per-cursor effect: must not 3484-- cascade. 3485n . exec ( [[ 3486function! Noop(type) abort 3487call getchar() 3488endfunction 3489function! NoopSetup() abort 3490set operatorfunc=Noop 3491return 'g@' 3492endfunction 3493nnoremap <expr> ,s NoopSetup() 3494]] ) 3495fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' , 'x [y] z' } ) -- only line 4 has brackets 3496feed ( 'gg0Qj0' ) -- cursor on line 1, primary on line 2 (neither has brackets) 3497feed ( ',sa[z' ) -- g@ + a[ jumps primary to line 4's "["; opfunc getchar()'s "z", does nothing 3498eq ( 1 , ncursors ()) -- the cursor survives 3499end ) 3500 3501it ( 'motion-only operator cascades during follow-mode #41612' , function () 3502-- The operator does not change text/registers; it only moves the cursor. 3503n . exec_lua ( [[ 3504local function test_op(motion) 3505if motion == nil then 3506vim.o.operatorfunc = test_op 3507return 'g@' 3508end 3509local pos = vim.pos.cursor() 3510local line = vim.api.nvim_buf_get_lines(pos.buf, pos.row, pos.row + 1, false)[1] 3511pos.col = #line 3512vim.api.nvim_win_set_cursor(0, pos:to_cursor()) 3513end 3514vim.keymap.set('n', 'gm', test_op, { expr = true }) 3515]] ) 3516cursors ( { 'aaaa' , 'bbbbb' , 'cccccc' } , 'QjQj' ) 3517feed ( '1q=' ) 3518atoms_start () 3519feed ( 'gmil' ) 3520eq ( { { 0 , 3 } , { 1 , 4 } } , anchors ()) 3521eq ( 6 , fn . col ( '.' )) 3522-- Exactly one type=operator atom. 3523eq ( 1 , # atoms ()) 3524eq ( { 3525type ='operator' , 3526keys ='g@il' , 3527lhs ='gmil' , 3528operator ='g@' , 3529changed =false , 3530moved =true , 3531} , t . pick ( atom_last (), 'type' , 'keys' , 'lhs' , 'operator' , 'changed' , 'moved' )) 3532end ) 3533 3534it ( 'cursors placed inside the opfunc are live for the next typed cascade' , function () 3535-- Occurrence-operator pattern (vim-mode-plus "co{motion}", issue #21334): the 3536-- 'operatorfunc' places a cursor at each occurrence of the word within the motion, then 3537-- a following typed edit cascades to all of them. Rule: extmarks added from WITHIN 3538-- an opfunc yield cursors the next command cascades to (the g@ itself has no 3539-- effect, so it does not cascade and the placed cursors survive, like |v_Q| placement). 3540n . exec_lua ( [==[ 3541_G.occur_opfunc = function() 3542local ms = vim.fn.matchbufline('%', _G.occur_pat, vim.fn.line("'["), vim.fn.line("']")) 3543vim.api.nvim_win_set_cursor(0, { ms[1].lnum, ms[1].byteidx }) 3544local ns = vim.api.nvim_create_namespace('nvim.multicursor') 3545for i = 2, #ms do 3546vim.api.nvim_buf_set_extmark(0, ns, ms[i].lnum - 1, ms[i].byteidx, {}) 3547end 3548end 3549vim.keymap.set('n', 'co', function() 3550_G.occur_pat = ([[\<%s\>]]):format(vim.fn.expand('<cword>')) -- before g@ moves the cursor 3551vim.o.operatorfunc = 'v:lua.occur_opfunc' 3552return 'g@' 3553end, { expr = true }) 3554]==] ) 3555fn . setline ( 1 , { 'text a text' , 'b text c' } ) 3556api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) -- on the first "text" 3557feed ( 'coip' ) -- place a cursor at every "text" in the paragraph 3558eq ( 2 , ncursors ()) -- 3 matches: primary + 2 multicursors 3559feed ( 'ciwWORD<Esc>' ) -- a typed edit cascades to the opfunc-placed cursors 3560eq ( { 'WORD a WORD' , 'b WORD c' } , get_lines ()) 3561end ) 3562 3563it ( 3564'payload mapping (":call" + getchar, like vim-surround "ds") cascades via LHS-replay' , 3565function () 3566-- A mapping whose edit is done through :normal is invisible to atom 3567-- capture (decide-once sees nothing). It cascades by re-running the 3568-- mapping (LHS + the getchar()'d target) at each cursor: LHS-replay. 3569n . exec ( t_atom . delsurround_vim ) 3570fn . setline ( 1 , { 'a (one)' , 'b (two)' , 'c (three)' } ) 3571feed ( 'gg0f(Qj0f(Qj0f(' ) 3572atoms_start () 3573feed ( 'ds)' ) -- ")" is the getchar()'d payload 3574eq ( { 'a one' , 'b two' , 'c three' } , get_lines ()) 3575-- The emitted atom carries the resolution plus the getchar()'d payload; the cascade 3576-- itself re-runs `lhs` (the edit is invisible, so nothing was queued for it). 3577local ev =atoms ()[ # atoms ()] 3578eq ( { lhs ='ds)' , keys =':call DelSurround()\n)' } , { lhs =ev . lhs , keys =ev . keys } ) 3579 3580-- Same for a mapping that produces NO capturable keys at all (kKeyOpaque): 3581-- "<Cmd>" (K_COMMAND) and a Lua callback (K_LUA). Both are real user 3582-- keystrokes, so their edit is a mapping edit and cascades by LHS-replay. 3583command ( 'nnoremap <F3> <Cmd>normal! x<CR>' ) 3584n . exec_lua ( [[vim.keymap.set('n', '<F4>', function() vim.cmd('normal! x') end)]] ) 3585for _ , lhs in ipairs ( { '<F3>' , '<F4>' } ) do 3586clear_cursors () 3587fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' } ) 3588feed ( 'gg0QjQj' ) 3589feed ( lhs ) 3590eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 3591end 3592 3593-- Op-pending payload mapping (vim-sneak :omap) cascades by `keys`. 3594n . exec ( t_atom . minisneak_vim ) 3595clear_cursors () 3596fn . setline ( 1 , { 'aa (x) here' , 'bb (y) here' , 'cc (z) here' } ) 3597feed ( 'gg0QjQj' ) 3598feed ( 'dzhe' ) 3599eq ( { 'here' , 'here' , 'here' } , get_lines ()) 3600 3601-- Lua :omap textobject (starts Visual mode, |omap-info|) cascades by `keys` too. #41482 3602n . exec_lua ( [[ 3603vim.keymap.set('o', 'gt', function() 3604vim.cmd('normal! viw') 3605end) 3606]] ) 3607clear_cursors () 3608fn . setline ( 1 , { 'aaa xxx' , 'bbb yyy' , 'ccc zzz' } ) 3609feed ( 'gg0wQj0wQj0w' ) 3610feed ( 'dgt' ) 3611eq ( { 'aaa ' , 'bbb ' , 'ccc ' } , get_lines ()) 3612end 3613) 3614end ) 3615 3616describe ( ']C and [C' , function () 3617local function cur () 3618return { fn . line ( '.' ), fn . col ( '.' ) - 1 } 3619end 3620 3621-- The default mappings require the standard startup. 3622before_each ( function () 3623n . clear ( { args_rm ={ '--cmd' } } ) 3624end ) 3625 3626it ( ']C scrolls the viewport to an off-screen cursor' , function () 3627local screen =Screen . new ( 30 , 6 ) 3628local lines ={} ---@type string[] 3629for i =1 , 40 do 3630lines [ i ] =( 'line %d' ): format ( i ) 3631end 3632fn . setline ( 1 , lines ) 3633feed ( 'gg0Q' ) 3634add_cursor ( 0 , { 30 , 0 } ) 3635feed ( ']C' ) -- jump to line 30: the viewport must follow 3636eq ( 30 , fn . line ( '.' )) 3637screen : expect ( { any ='ine 30' } ) -- ("l" is under the painted cursor cell) 3638end ) 3639 3640it ( 'cycle through the cursors, wrapping' , function () 3641cursors ( { 'aaa' , 'bbb' , 'ccc' , 'ddd' } , 'Q2jllQ' ) 3642feed ( 'gg0j' ) 3643feed ( ']C' ) 3644eq ( { 3 , 2 } , cur ()) 3645eq ( { { 0 , 0 } , { 2 , 2 } } , anchors ()) 3646feed ( ']C' ) -- wraps 3647eq ( { 1 , 0 } , cur ()) 3648feed ( '2]C' ) -- count 3649eq ( { 1 , 0 } , cur ()) 3650feed ( '[C' ) 3651eq ( { 3 , 2 } , cur ()) 3652feed ( '[C' ) 3653eq ( { 1 , 0 } , cur ()) 3654feed ( ']CQ' ) -- "]CQ" removes the cursor it lands on. 3655eq ( { { 0 , 0 } } , anchors ()) 3656-- |mcursor-examples| mapping: ]C lays an egg before jumping. 3657n . exec_lua ( [[ 3658local ns = vim.api.nvim_create_namespace('nvim.multicursor') 3659vim.keymap.set('n', ']C', function() 3660local row, col = vim.pos.cursor(0):to_extmark() 3661vim.api.nvim_buf_set_extmark(0, ns, row, col, {}) 3662vim.cmd('normal! ]C') 3663end) 3664]] ) 3665feed ( ']C' ) 3666eq ( { 1 , 0 } , cur ()) 3667eq ( { { 0 , 0 } , { 2 , 2 } } , anchors ()) 3668feed ( ']C' ) 3669eq ( { 3 , 2 } , cur ()) 3670eq ( { { 0 , 0 } , { 2 , 2 } } , anchors ()) 3671end ) 3672 3673it ( 'does not move the other cursors in q= mode' , function () 3674cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'Qj' ) 3675feed ( 'q=' ) 3676feed ( ']C' ) 3677eq ( { 1 , 0 } , cur ()) 3678eq ( { { 0 , 0 } } , anchors ()) 3679feed ( 'q=' ) 3680end ) 3681 3682it ( 'beeps and does not move without cursors' , function () 3683fn . setline ( 1 , { 'aaa' } ) 3684feed ( 'gg0' ) 3685eq ( 0 , fn . assert_beeps ( 'normal! ]C' )) 3686eq ( { 1 , 0 } , cur ()) 3687end ) 3688end ) 3689 3690describe ( 'treesitter' , function () 3691it ( 'incremental selections at each cursor #41716' , function () 3692-- The default ts "an"/"in" mappings: setpos("'<") + "gv" in Lua. 3693clear ( { args_rm ={ '--cmd' } } ) 3694command ( n . nvim_set ) 3695command ( 'colorscheme vim' ) 3696command ( 'hi MCursor guifg=Black guibg=LightGrey' ) 3697local screen =Screen . new ( 30 , 6 ) 3698command ( 'set filetype=lua' ) 3699cursors ( { 'foo(one, two)' , 'bar(three, four)' , 'baz(five, six)' } , 'ftQj0ffQj0fs' ) 3700eq ( { { 0 , 9 } , { 1 , 11 } } , anchors ()) 3701eq ( { 3 , 10 } , api . nvim_win_get_cursor ( 0 )) 3702feed ( 'van' ) 3703screen : expect ( [[ 3704foo(one, {17:two}) | 3705bar(three, {17:four}) | 3706baz(five, {17:si^x}) | 3707{1:~ }|*2 3708{5:-- VISUAL --} | 3709]] ) 3710feed ( 'an' ) 3711screen : expect ( [[ 3712foo{17:(one, two)} | 3713bar{17:(three, four)} | 3714baz{17:(five, six^)} | 3715{1:~ }|*2 3716{5:-- VISUAL --} | 3717]] ) 3718-- TODO: "in" after "an": the primary selects the first child. #42000 3719-- feed('in') 3720-- screen:expect([[ 3721-- foo(one, {17:two}) | 3722-- bar(three, {17:four}) | 3723-- baz(five, {17:si}^x) | 3724-- {1:~ }|*2 3725-- {5:-- VISUAL --} | 3726-- ]]) 3727feed ( 'cX' ) 3728eq ( { 'fooX' , 'barX' , 'bazX' } , get_lines ()) 3729eq ( 'i' , fn . mode ()) 3730feed ( '<Esc>' ) 3731eq ( { 'fooX' , 'barX' , 'bazX' } , get_lines ()) 3732 3733clear_cursors () 3734cursors ( { 'foo(one, two)' , 'bar(three, four)' , 'baz(five, six)' } , 'ftQj0ffQj0fs' ) 3735feed ( 'canX' ) 3736eq ( { 'foo(one, X)' , 'bar(three, X)' , 'baz(five, X)' } , get_lines ()) 3737eq ( 'i' , fn . mode ()) 3738feed ( '<Esc>' ) 3739eq ( { 'foo(one, X)' , 'bar(three, X)' , 'baz(five, X)' } , get_lines ()) 3740end ) 3741 3742it ( 'markdown highlighting survives a live insert' , function () 3743n . exec_lua ( [[ 3744-- Large, injection-heavy buffer: multi-slice ASYNC parses (the 3745-- crash lived in a resumed parse's external scanner). 3746local lines = {} 3747for i = 1, 800 do 3748vim.list_extend(lines, { 3749('# Section %d'):format(i), 3750'', 3751'- item with `code` and *emphasis*', 3752' - nested [link](http://x)', 3753'', 3754'```lua', 3755'local x = ' .. i, 3756'```', 3757'', 3758}) 3759end 3760vim.api.nvim_buf_set_lines(0, 0, -1, true, lines) 3761vim.treesitter.start(0, 'markdown') 3762]] ) 3763feed ( 'gg0Q2jQ2jQ4j' ) 3764feed ( 'A' ) 3765for c in ( 'hello world' ): gmatch ( '.' ) do 3766feed ( c ) 3767n . poke_eventloop () 3768end 3769feed ( '<Esc>' ) 3770feed ( 'u' ) 3771feed ( '<C-r>' ) 3772n . assert_alive () 3773end ) 3774end ) 3775 3776describe ( 'clipboard' , function () 3777it ( "does not crash after jumping to an empty line with 'clipboard'" , function () 3778n . exec_lua ( [[ 3779_G.content = {} 3780vim.g.clipboard = { 3781name = 'test', 3782copy = { 3783['+'] = function(lines) 3784_G.content = lines 3785end, 3786}, 3787paste = { 3788['+'] = function() 3789return _G.content 3790end, 3791}, 3792} 3793vim.o.clipboard = 'unnamedplus' 3794]] ) 3795cursors ( { '' , 'aa' } , 'QjQ' ) -- Cursors on both lines, primary on the non-empty line. 3796feed ( ']C' ) -- Make the empty-line cursor primary. 3797feed ( 'C' ) 3798n . assert_alive () 3799feed ( '<Esc>' ) 3800eq ( { '' , '' } , get_lines ()) 3801end ) 3802 3803it ( 'clipboard: implicit clipboard=unnamed[plus], explicit "+' , function () 3804n . exec_lua ( [[ 3805_G.copies, _G.pastes = 0, 0 3806_G.content = {} 3807vim.g.clipboard = { 3808name = 'test', 3809copy = { 3810['+'] = function(lines) 3811_G.copies = _G.copies + 1 3812_G.content = lines 3813end, 3814}, 3815paste = { 3816['+'] = function() 3817_G.pastes = _G.pastes + 1 3818return _G.content 3819end, 3820}, 3821} 3822]] ) 3823local function provider () 3824return n . exec_lua ( 'return { _G.copies, _G.pastes, _G.content }' ) 3825end 3826 3827-- Implicit clipboard (clipboard=unnamed[plus]) is ignored during cascade. 3828-- On multicursor exit, the unnamed (") join is written to the clipboard. 3829command ( 'set clipboard=unnamedplus' ) 3830cursors ( { 'aa bb' , 'cc dd' , 'ee ff' } ) 3831feed ( 'dw' ) 3832eq ( { 'bb' , 'dd' , 'ff' } , get_lines ()) 3833-- Clipboard provider was updated only by the primary's "dw". Not cascaded. 3834eq ( { 1 , 0 , { 'ee ' } } , provider ()) 3835feed ( 'p' ) -- Pastes unnamed reg (not clipboard) per-cursor. 3836eq ( { 'baa b' , 'dcc d' , 'fee f' } , get_lines ()) 3837eq ( { 1 , 1 , { 'ee ' } } , provider ()) 3838clear_cursors () -- Exit: the joined yank is written to the clipboard. 3839eq ( { 2 , 1 , { 'aa ' , 'cc ' , 'ee ' , '' } } , provider ()) 3840 3841-- Explicit clipboard "+". 3842command ( 'set clipboard=' ) 3843cursors ( { 'aa' , 'bb' } , 'Qj0' ) 3844feed ( '"+yy' ) -- Explicit "+: writes the primary's yank to the clipboard. 3845eq ( { 3 , 1 , { 'bb' , '' } } , provider ()) 3846feed ( '"+p' ) -- Explicit "+: pastes the primary's clipboard at every cursor. 3847eq ( { 'aa' , 'bb' , 'bb' , 'bb' } , get_lines ()) 3848eq ( { 3 , 2 , { 'bb' , '' } } , provider ()) 3849clear_cursors () 3850eq ( { 3 , 2 , { 'bb' , '' } } , provider ()) 3851end ) 3852end ) 3853 3854describe ( 'UI integration' , function () 3855it ( 'cursor positions are pushed to UIs (win_extmark)' , function () 3856local screen =Screen . new ( 30 , 5 ) 3857cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'QjlQj' ) 3858local ns =api . nvim_create_namespace ( 'nvim.multicursor' ) 3859local marks =api . nvim_buf_get_extmarks ( 0 , ns , 0 , - 1 , {} ) 3860eq ( 2 , # marks ) 3861screen : expect ( { 3862grid =[[ 3863{17:a}aa | 3864b{17:b}b | 3865c^cc | 3866{1:~ }| 3867| 3868]] , 3869extmarks ={ 3870[ 2 ] ={ 3871{ 1000 , ns , marks [ 1 ][ 1 ], 0 , 0 } , 3872{ 1000 , ns , marks [ 2 ][ 1 ], 1 , 1 } , 3873} , 3874} , 3875} ) 3876end ) 3877 3878it ( 'showcmd area shows the cursor count ("N×"), also with ui2' , function () 3879local screen ---@type test.functional.ui.screen 3880for _ , ui2 in ipairs ( { false , true } ) do 3881clear () 3882command ( 'hi MCursor guifg=Black guibg=LightGrey' ) 3883screen =Screen . new ( 30 , 5 ) 3884command ( 'set showcmd' ) -- the test env defaults to 'noshowcmd' 3885if ui2 then 3886n . exec_lua ( [[require('vim._core.ui2').enable({})]] ) 3887end 3888cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'Q' ) 3889screen : expect ( { any ='1×' } ) 3890feed ( 'jQ' ) 3891screen : expect ( { any ='2×' } ) 3892feed ( 'q=' ) -- Follow mode: "=" prefix. 3893feed ( 'l' ) -- Tickle showcmd redraw. 3894screen : expect ( { any ='=2×' } ) 3895feed ( 'q=' ) -- Follow mode off. 3896feed ( 'h' ) -- Tickle showcmd redraw. 3897clear_cursors () -- cleared with the cursors 3898feed ( '<Esc>' ) -- the indicator refreshes on the next command 3899screen : expect ( { none ='×' } ) 3900end 3901 3902-- ui2 redraws on 'showcmd', which should only update AFTER cascade, not during. #42081 #41659 3903n . exec_lua ( function () 3904_G . seen ={} 3905local ns =vim . api . nvim_create_namespace ( 'nvim.multicursor.cursor' ) 3906vim . ui_attach ( vim . api . nvim_create_namespace ( 'test' ), { ext_messages =true } , function ( ev ) 3907if ev == 'msg_showcmd' then 3908-- Buffer text, cursor column, and selection-end columns. 3909local ends =vim . tbl_map ( function ( m ) 3910return m [ 3 ] 3911end , vim . api . nvim_buf_get_extmarks ( 0 , ns , 0 , - 1 , {} )) 3912_G . seen [ # _G . seen + 1 ] =( '%s %d %s' ): format ( 3913table . concat ( vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ), ',' ), 3914vim . fn . col ( '.' ) - 1 , 3915table . concat ( ends , ',' ) 3916) 3917end 3918end ) 3919end ) 3920clear_cursors () 3921cursors ( { 'aaa' , 'bbb' , 'ccc' } , 'QjQjQ' ) 3922screen : expect ( { any ='3×' } ) -- Input processed, before resetting `seen`. 3923n . exec_lua ( '_G.seen = {}' ) 3924feed ( 'rx' ) 3925screen : expect ( [[ 3926{17:x}aa | 3927{17:x}bb | 3928{17:^x}cc | 3929{1:~ }| 39303× | 3931]] ) 3932eq ( { 'aaa,bbb,ccc 0 ' , 'xaa,xbb,xcc 0 ' } , n . exec_lua ( 'return _G.seen' )) 3933n . exec_lua ( '_G.seen = {}' ) 3934feed ( 'vl' ) 3935screen : expect ( [[ 3936{17:xa}a | 3937{17:xb}b | 3938{17:x^c}c | 3939{1:~ }| 3940{5:-- VISUAL --} 3× 2 | 3941]] ) 3942eq ( 3943{ 'xaa,xbb,xcc 0 0,0,0' , 'xaa,xbb,xcc 1 1,1,1' , 'xaa,xbb,xcc 1 1,1,1' } , 3944n . exec_lua ( 'return _G.seen' ) 3945) 3946end ) 3947end ) 3948 3949describe ( 'workflows' , function () 3950it ( 'follow-once mapping' , function () 3951n . exec_lua ( [[ 3952vim.keymap.set('n', 'q-', function() 3953vim.cmd('normal! 1q=') 3954vim.api.nvim_create_autocmd('CmdAtom', { 3955callback = function(ev) 3956if ev.data.lhs == 'q-' then 3957return 3958end 3959vim.cmd('normal! 2q=') 3960return true 3961end, 3962}) 3963end) 3964]] ) 3965cursors ( { ' aaa' , ' bbb' , ' ccc' } ) 3966feed ( 'q-' ) 3967feed ( '^' ) -- Cascades: every cursor moves to the first non-blank. 3968eq ( { { 0 , 2 } , { 1 , 2 } } , anchors ()) 3969feed ( 'l' ) -- Follow already ended: primary only. 3970eq ( { { 0 , 2 } , { 1 , 2 } } , anchors ()) 3971eq ( 4 , fn . col ( '.' )) 3972end ) 3973 3974it ( 'place a cursor at a range of quickfix items: :cdo normal! Q' , function () 3975fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' , 'ddd' } ) 3976fn . setqflist ( { 3977{ bufnr =fn . bufnr ( '' ), lnum =1 , col =1 } , 3978{ bufnr =fn . bufnr ( '' ), lnum =2 , col =1 } , 3979{ bufnr =fn . bufnr ( '' ), lnum =4 , col =1 } , 3980} ) 3981command ( 'cdo normal! Q' ) 3982eq ( 3 , ncursors ()) 3983feed ( '3G0' ) -- Move the primary off the last item's cursor (it would double-apply). 3984feed ( 'x' ) 3985eq ( { 'aa' , 'bb' , 'cc' , 'dd' } , get_lines ()) 3986-- A range addresses quickfix items: cursors on items 2-3 only. 3987clear_cursors () 3988command ( '2,3cdo normal! Q' ) 3989eq ( 2 , ncursors ()) 3990end ) 3991 3992it ( 'CTRL-X/CTRL-A apply at each cursor; g CTRL-A is the counter' , function () 3993cursors ( { 'x = 5' , 'y = 5' } , 'Qj' ) 3994feed ( '<C-x>' ) 3995eq ( { 'x = 4' , 'y = 4' } , get_lines ()) 3996feed ( '<C-a>' ) 3997eq ( { 'x = 5' , 'y = 5' } , get_lines ()) 3998-- g_CTRL-A inserts counter at the cursor positions. 3999feed ( 'g<C-a>' ) 4000eq ( { 'x = 15' , 'y = 25' } , get_lines ()) 4001end ) 4002 4003it ( 'typeahead behind the cascade-triggering key is not consumed by replays' , function () 4004-- Batch input: "x" cascades; queued "yy" must survive the replays (save_current_state), then 4005-- cascade (per-cursor registers prove the yank ran at both cursors). 4006cursors ( { 'abc' , 'def' } , 'Qj' ) 4007feed ( 'xyy' ) 4008eq ( { 'bc' , 'ef' } , get_lines ()) 4009feed ( 'p' ) 4010eq ( { 'bc' , 'bc' , 'ef' , 'ef' } , get_lines ()) 4011end ) 4012 4013it ( 'x then p at EOL on lines of different lengths' , function () 4014-- Jagged EOL: each cursor deletes last char into its own register, then pastes at its own 4015-- (shorter) EOL. 4016fn . setline ( 1 , { 'abc' , 'de' } ) 4017feed ( 'gg$Q' ) 4018feed ( 'j$' ) 4019feed ( 'x' ) 4020eq ( { 'ab' , 'd' } , get_lines ()) 4021feed ( 'p' ) 4022eq ( { 'abc' , 'de' } , get_lines ()) 4023end ) 4024 4025it ( 'type=excmd CmdAtoms are emit-only, ":s" does not cascade' , function () 4026-- cmdline payloads are captured but never cascaded: only the primary's ":s" runs. 4027cursors ( { 'foo' , 'foo' } , 'Qj' ) 4028atoms_start () 4029feed ( ':s/o/O/<CR>' ) 4030eq ( { 'foo' , 'fOo' } , get_lines ()) 4031local ev =atoms ()[ # atoms ()] 4032eq ( 'excmd' , ev . type ) 4033eq ( 's/o/O/' , ev . text ) 4034end ) 4035end ) 4036 4037describe ( '@ (macro replay)' , function () 4038it ( 'cascades at each cursor' , function () 4039fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' } ) 4040feed ( 'gg0qqxq' ) -- record "x"; line 1 becomes "aa" 4041feed ( 'jQ' ) 4042feed ( 'j0' ) 4043feed ( '@q' ) 4044eq ( { 'aa' , 'bb' , 'cc' } , get_lines ()) 4045-- "@@" repeats, still cascading. 4046feed ( '@@' ) 4047eq ( { 'aa' , 'b' , 'c' } , get_lines ()) 4048-- A macro with an insert session cascades too. 4049clear_cursors () 4050api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa' , 'bbb' , 'ccc' } ) 4051feed ( 'gg0qwA!<Esc>q' ) -- record "A!<Esc>"; line 1 becomes "aaa!" 4052feed ( 'jQ' ) 4053feed ( 'j0' ) 4054feed ( '@w' ) 4055eq ( { 'aaa!' , 'bbb!' , 'ccc!' } , get_lines ()) 4056-- A count applies at each cursor. 4057clear_cursors () 4058api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaaa' , 'bbbb' , 'cccc' } ) 4059feed ( 'gg0' ) 4060feed ( 'jQ' ) 4061feed ( 'j0' ) 4062feed ( '2@q' ) -- the "x" macro, twice per cursor 4063eq ( { 'aaaa' , 'bb' , 'cc' } , get_lines ()) 4064end ) 4065 4066it ( 'from a cursorless buffer, cascades in the buffer it enters' , function () 4067-- Capture starts before the macro runs (buffer unknown). A macro typed where there are no 4068-- cursors, cascades in the buffer it navigates into, like a mapping. 4069command ( 'set hidden' ) 4070cursors ( { 'aaa' , 'bbb' } , 'jQk' ) -- buf1: primary on line 1, cursor on line 2. 4071command ( 'vsplit | enew' ) -- buf2: no cursors. 4072fn . setreg ( 'q' , k ( '<C-w>px' )) 4073feed ( '@q' ) 4074eq ( { 'aa' , 'bb' } , get_lines ()) 4075end ) 4076end ) 4077 4078describe ( 'CmdAtom' , function () 4079it ( 'fires for a cascaded operation' , function () 4080cursors ( { 'aaa' , 'bbb' } , 'Q' ) 4081atoms_start () 4082feed ( 'jx' ) 4083-- Atom shape: see cmdatom_spec. With cursors: no extra atoms from the cascade replays. 4084eq ( { 'j' , 'dl' } , atoms_tail ( 2 )) 4085-- A yank cascades (per-cursor registers) but does not edit. 4086feed ( 'yy' ) 4087eq ( false , atoms ()[ # atoms ()]. changed ) 4088end ) 4089 4090it ( 'non-edit operator (zfap) cascades; fold toggles (za) do not' , function () 4091fn . setline ( 1 , { 'aa' , 'aa' , '' , 'bb' , 'bb' , '' } ) 4092feed ( '4G0Q' ) 4093feed ( 'gg0' ) 4094atoms_start () 4095feed ( 'zfap' ) 4096-- With cursors: the cascade adds no atoms. (Atom shape is covered in cmdatom_spec.) 4097eq ( { 'zfap' } , atoms_tail ( 1 )) 4098-- The fold operator cascades: each cursor folds its own paragraph. 4099eq ( { { 1 , 3 } , { 4 , 6 } } , { 4100{ fn . foldclosed ( 1 ), fn . foldclosedend ( 1 ) } , 4101{ fn . foldclosed ( 4 ), fn . foldclosedend ( 4 ) } , 4102} ) 4103feed ( 'za' ) 4104eq ( { 'za' } , atoms_tail ( 1 )) 4105-- Fold toggles are view state: emitted, not cascaded. 4106eq ( { - 1 , 4 } , { fn . foldclosed ( 1 ), fn . foldclosed ( 4 ) } ) 4107end ) 4108 4109it ( 'records q= motions' , function () 4110cursors ( { 'abcd' , 'efgh' } , 'Qj' ) 4111atoms_start () 4112feed ( 'q=' ) 4113feed ( 'l' ) 4114feed ( 'l' ) 4115feed ( 'q=' ) 4116eq ( { 'q=' , 'l' , 'l' , 'q=' } , atoms_tail ( 4 )) 4117eq ( 'motion' , atoms ()[ # atoms () - 1 ]. type ) 4118end ) 4119end ) 4120 4121describe ( 'programmatic edits' , function () 4122it ( 'API edits do not cascade; they shift the cursors, which then track' , function () 4123-- Extmarks are authoritative: the cascade re-queries positions, so an interleaved API edit 4124-- just shifts where the replays land. 4125cursors ( { 'aaa' , 'bbb' , 'ccc' } ) 4126n . exec_lua ( "vim.api.nvim_buf_set_lines(0, 0, 0, true, { 'zzz' })" ) 4127eq ( { 'zzz' , 'aaa' , 'bbb' , 'ccc' } , get_lines ()) 4128feed ( 'x' ) 4129eq ( { 'zzz' , 'aa' , 'bb' , 'cc' } , get_lines ()) 4130end ) 4131 4132it ( 'TextChanged autocmd editing the buffer does not re-cascade' , function () 4133fn . setline ( 1 , { 'top' , 'aaa' , 'bbb' } ) 4134feed ( '2G0Q' ) 4135feed ( 'j' ) 4136n . exec_lua ( [[ 4137_G.fired = 0 4138vim.api.nvim_create_autocmd('TextChanged', { 4139callback = function() 4140_G.fired = _G.fired + 1 4141if _G.fired == 1 then 4142vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { 'X' }) 4143end 4144end, 4145}) 4146]] ) 4147feed ( 'x' ) 4148n . poke_eventloop () 4149eq ( { 'Xtop' , 'aa' , 'bb' } , get_lines ()) 4150-- Once for the whole cascade + once for the autocmd's own API edit. 4151-- Not once per cursor, and the autocmd edit did not cascade. 4152eq ( 2 , n . exec_lua ( 'return _G.fired' )) 4153end ) 4154 4155it ( 'replacing the whole buffer via API does not crash the cascade' , function () 4156cursors ( { 'aaa' , 'bbb' , 'ccc' } ) 4157n . exec_lua ( "vim.api.nvim_buf_set_lines(0, 0, -1, true, { 'fresh', 'stuff' })" ) 4158-- The cursor lines are gone: both marks collapse to the edit boundary (past the last line), 4159-- where they merge/dedupe into one cursor; its replay clamps onto the last line. 4160feed ( 'x' ) 4161eq ( { 'fresh' , 'uff' } , get_lines ()) 4162eq ( 0 , ncursors ()) 4163end ) 4164 4165it ( ':normal! never cascades (programmatic input)' , function () 4166cursors ( { 'aaa' , 'bbb' } , 'Qj' ) 4167atoms_start () 4168command ( 'normal! x' ) -- Programmatic, no cascade (primary only). 4169eq ( { 'aaa' , 'bb' } , get_lines ()) 4170command ( 'normal! viwd' ) -- The entire Visual op is programmatic, not just selection. 4171eq ( { 'aaa' , '' } , get_lines ()) 4172eq ( {} , atoms ()) 4173feed ( 'u' ) 4174feed ( 'x' ) -- User input, cascades. 4175eq ( { 'aa' , 'b' } , get_lines ()) 4176end ) 4177end ) 4178end )