robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Tests for atom capture: the CmdAtom event, and dot-repeat of whole insert sessions. 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 retry =t . retry 10local clear =n . clear 11local command =n . command 12local exec =n . exec 13local feed =n . feed 14local fn =n . fn 15local eq =t . eq 16local api =n . api 17local get_lines =t_atom . get_lines 18local k =t_atom . k 19local atoms_start =t_atom . atoms_start 20local atoms =t_atom . atoms 21local atoms_tail =t_atom . atoms_tail 22local atom_last =t_atom . atom_last 23local pick =t . pick 24local subatoms =t_atom . subatoms 25 26describe ( 'dot-repeat' , function () 27before_each ( clear ) 28 29it ( 'replays insert-mode cursor-moves (the whole session)' , function () 30fn . setline ( 1 , { 'one' , 'two' } ) 31feed ( 'iab<Left>c<Esc>' ) 32eq ( { 'acbone' , 'two' } , get_lines ()) 33-- The ". register and undo still restart at the cursor-move, like Vim. 34eq ( 'c' , fn . getreg ( '.' )) 35feed ( 'u' ) 36eq ( { 'abone' , 'two' } , get_lines ()) 37feed ( '<C-r>' ) 38-- "." re-executes the whole session, cursor-move included. 39feed ( 'j0.' ) 40eq ( { 'acbone' , 'acbtwo' } , get_lines ()) 41-- An absolute jump (<C-Home>) still restarts the capture: "." replays 42-- only the post-jump insert. 43feed ( 'ggAxy<C-Home>z<Esc>' ) 44eq ( { 'zacbonexy' , 'acbtwo' } , get_lines ()) 45eq ( 'z' , fn . getreg ( '.' )) 46feed ( 'j0.' ) 47eq ( { 'zacbonexy' , 'zacbtwo' } , get_lines ()) 48-- Vim's repeat-only-the-tail behavior is a mapping away (documented in 49-- vim_diff.txt): i_CTRL-O re-entry restarts the capture. 50command ( 'inoremap <Left> <C-o><Left>' ) 51feed ( 'ggiab<Left>c<Esc>' ) 52eq ( { 'acbzacbonexy' , 'zacbtwo' } , get_lines ()) 53feed ( 'j0.' ) 54eq ( { 'acbzacbonexy' , 'czacbtwo' } , get_lines ()) 55end ) 56 57it ( 'failing mid-selection ends Visual-mode #41896' , function () 58fn . setline ( 1 , { '1' , '2' , '3' } ) 59feed ( '<C-V>jI1<Esc>j.j.' ) -- The 2nd "." fails: "j" on the last line. 60eq ( { '11' , '112' , '13' } , get_lines ()) 61eq ( 'n' , fn . mode ( 1 )) 62feed ( 'k0.' ) -- The failed "." did not replace the last change. 63eq ( { '11' , '1112' , '113' } , get_lines ()) 64-- Error (E20) instead of beep. ModeChanged fires after the error, not inside emsg(). 65command ( 'autocmd ModeChanged V:n echomsg "ModeChanged"' ) 66for _ , eb in ipairs ( { 'noerrorbells' , 'errorbells' } ) do 67api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' , 'two' } ) 68feed ( 'ggmajV`a~' ) 69command (( 'set %s | delmarks a | messages clear' ): format ( eb )) 70feed ( '.' ) 71eq ( { 'ONE' , 'TWO' } , get_lines (), eb ) 72eq ( 'n' , fn . mode ( 1 )) 73eq ( '\nE20: Mark not set\nModeChanged' , fn . execute ( 'messages' )) 74end 75-- A macro or mapping still stops at the failed motion, in Visual mode (map-error). 76api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' , 'two' , 'three' } ) 77fn . setreg ( 'q' , 'Vjd' ) 78command ( 'nnoremap Q Vjd' ) 79for _ , keys in ipairs ( { 'gg2@q' , 'Q' } ) do 80feed ( keys ) 81eq ( { 'three' } , get_lines (), keys ) 82eq ( 'V' , fn . mode ( 1 )) 83feed ( '<Esc>' ) 84end 85end ) 86 87it ( 'of a visual op does not churn showcmd' , function () 88local screen =Screen . new ( 40 , 8 , { ext_messages =true } ) 89command ( 'set showcmd' ) 90fn . setline ( 1 , { 'foo bar' , 'longword two' } ) 91feed ( 'gg0viWgU' ) 92feed ( '2gg0' ) 93screen : expect ( { any ='%^longword' } ) 94local updates ={} 95function screen : _handle_msg_showcmd ( msg ) 96local text =table . concat ( vim . tbl_map ( function ( chunk ) 97return chunk [ 2 ] 98end , msg )) 99if text ~= '' then 100updates [ # updates + 1 ] =text 101end 102self . showcmd =msg 103end 104feed ( '.' ) 105retry ( nil , nil , function () 106eq ( { 'FOO bar' , 'LONGWORD two' } , get_lines ()) 107end ) 108screen : sleep ( 50 ) 109-- Stuffed (replayed) keys must not display in 'showcmd': ext_messages UIs redraw per showcmd 110-- change, which would paint the replay's transient states (its in-progress Visual selection). 111eq ( 112{} , 113vim . tbl_filter ( function ( s ) 114return s : find ( '[UW]' ) ~= nil 115end , updates ) 116) 117end ) 118end ) 119 120describe ( 'CmdAtom' , function () 121before_each ( clear ) 122 123describe ( 'composite' , function () 124it ( 'motion mapping' , function () 125command ( 'nnoremap j gj' ) 126fn . setline ( 1 , { 'a1' , 'b2' , 'c3' , 'd4' , 'e5' } ) 127feed ( 'gg' ) 128atoms_start () 129feed ( '3j' ) 130eq ( 4 , fn . line ( '.' )) 131local ev =atom_last () 132eq ( 133{ type ='motion' , lhs ='j' , keys ='3gj' , count =3 } , 134pick ( ev , 'type' , 'lhs' , 'keys' , 'count' ) 135) 136-- The "," repeat recipe: replaying the KEYS verbatim repeats the count. 137feed ( 'gg' ) 138n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 139eq ( 4 , fn . line ( '.' )) 140-- Multi-command mapping: the pre-typed count lands in the FIRST folded 141-- command; the folded atom itself has no single count. 142command ( 'nnoremap <F6> xw' ) 143fn . setline ( 1 , { 'abcdef ghi' , 'jkl' } ) 144feed ( 'gg0' ) 145feed ( '3<F6>' ) 146ev =atom_last () 147eq ( { type ='mapping' , lhs =k ( '<F6>' ) } , pick ( ev , 'type' , 'count' , 'lhs' )) 148-- Only the composite carries the mapping's LHS: a subatom is its own input. 149eq ( { keys ='3dl' , count =3 , lhs ='3dl' } , pick ( ev . atoms [ 1 ], 'keys' , 'count' , 'lhs' )) 150-- "." repeats the mapping's EDIT as ONE atom, labeled "." (not "<F6>"). 151local before =# atoms () 152feed ( '.' ) 153eq ( before + 1 , # atoms ()) 154eq ( { type ='operator' , keys ='3dl' , lhs ='.' } , pick ( atom_last (), 'type' , 'keys' , 'lhs' )) 155end ) 156 157it ( 'Lua mapping (e.g. "]q" default)' , function () 158-- Default "]q" mapping: a Lua callback with unknown keys. Still a user action, emits an atom. 159n . exec_lua ( [[ 160vim.keymap.set('n', ']q', function() 161vim.cmd({ cmd = 'cnext', count = vim.v.count1 }) 162end, { desc = ':cnext' }) 163]] ) 164fn . setline ( 1 , { 'aaa' , 'bbb' , 'ccc' } ) 165local bufnr =api . nvim_get_current_buf () 166fn . setqflist ( { { bufnr =bufnr , lnum =1 } , { bufnr =bufnr , lnum =3 } } ) 167command ( 'cfirst' ) 168atoms_start () 169feed ( ']q' ) 170eq ( 3 , fn . line ( '.' )) -- The mapping did run (:cnext). 171eq ( 172{ type ='mapping' , lhs =']q' , changed =false } , 173pick ( atom_last (), 'type' , 'lhs' , 'keys' , 'changed' ) 174) 175-- Empty `keys` mapping that edits via API: `changed=true`. 176n . exec_lua ( [[ 177vim.keymap.set('n', ',e', function() 178vim.api.nvim_buf_set_lines(0, 0, 0, false, { 'NEW' }) 179end) 180]] ) 181feed ( ',e' ) 182eq ( { lhs =',e' , changed =true } , pick ( atom_last (), 'keys' , 'lhs' , 'changed' )) 183 184-- "<Cmd>" is opaque too, but unlike a Lua callback its command is text (like a ":" mapping). 185command ( 'nnoremap ,c <Cmd>call setline(1, "N" . v:count)<CR>' ) 186feed ( '3,c' ) 187local cmdev =atom_last () 188eq ( { 189type ='excmd' , 190lhs =',c' , 191keys =k ( '3<Cmd>call setline(1, "N" . v:count)<NL>' ), 192text ='call setline(1, "N" . v:count)' , 193count =3 , 194changed =true , 195} , pick ( cmdev , 'type' , 'lhs' , 'keys' , 'text' , 'count' , 'changed' )) 196-- Those keys replay: the count must survive, since "<Cmd>" reads v:count. 197eq ( 'N3' , fn . getline ( 1 )) 198fn . setline ( 1 , 'reset' ) 199n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( cmdev . keys )) 200eq ( 'N3' , fn . getline ( 1 )) 201-- "<Cmd>" text is raw, a trailing "0" is not CTRL-V escaped (as for a ":" cmdline). 202command ( [[nnoremap ,z <Cmd>call setline(1, 'Z') <Bar> let g:z = 10<CR>]] ) 203feed ( ',z' ) 204eq ( k ( [[<Cmd>call setline(1, 'Z') | let g:z = 10<NL>]] ), atom_last (). keys ) 205 206-- <expr> mapping that returns a "<Cmd>lua …<CR>" (dot-repeat idiom #41387) captures the same 207-- way: the constructed command is the atom. 208n . exec_lua ( [[ 209vim.keymap.set('n', ',x', function() 210return '<Cmd>call setline(1, "E" . v:count1)<CR>' 211end, { expr = true }) 212]] ) 213feed ( '2,x' ) 214cmdev =atom_last () 215eq ( { 216type ='excmd' , 217lhs =',x' , 218keys =k ( '2<Cmd>call setline(1, "E" . v:count1)<NL>' ), 219count =2 , 220} , pick ( cmdev , 'type' , 'lhs' , 'keys' , 'count' )) 221fn . setline ( 1 , 'reset' ) 222n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( cmdev . keys )) 223eq ( 'E2' , fn . getline ( 1 )) 224 225-- Opaque key that changes nothing is invisible: mid-selection it must not void the pending 226-- visual atom, which would void its per-cursor extents. 227command ( 'vnoremap ,n <Cmd>call execute("")<CR>' ) 228fn . setline ( 1 , { 'aaa bbb' } ) 229feed ( 'gg0viw,nd' ) 230eq ( ' bbb' , fn . getline ( 1 )) 231eq ( { type ='visual' , keys ='viwd' } , pick ( atom_last (), 'type' , 'keys' )) 232end ) 233 234it ( '<expr> mapping: EXPRESSION input vs EXECUTION input #41665' , function () 235atoms_start () 236for _ , reader in ipairs ( { 'getcharstr' , 'input' } ) do 237n . exec_lua ( function ( reader_ ) 238_G . _log ={} 239vim . keymap . set ( 'o' , 's' , function () 240local input =reader_ == 'input' and vim . fn . input ( '' ) or vim . fn . getcharstr () 241local data ={ input , vim . v . count1 } 242return ( '<Cmd>lua table.insert(_G._log, %s)<CR>' ): format ( vim . inspect ( data )) 243end , { expr =true } ) 244end , reader ) 245 246local input =reader == 'input' and 'e<CR>' or 'e' 247-- Placement of [count] should not change the behavior. #41665 248for _ , prefix in ipairs ( { '2d' , 'd2' } ) do 249feed (( '%ss%s' ): format ( prefix , input )) 250eq ( { 251type ='operator' , 252operator ='d' , 253count =2 , 254lhs =k (( 'ds%s' ): format ( input )), 255keys =k ( '2d<Cmd>lua table.insert(_G._log, { "e", 2 })<NL>' ), 256} , pick ( atom_last (), 'type' , 'operator' , 'count' , 'lhs' , 'keys' )) 257end 258eq ( { { 'e' , 2 } , { 'e' , 2 } } , n . exec_lua ( 'return _G._log' )) 259end 260 261-- expr EVALUATION gets "e" from getcharstr(), which the expr-mapping RETURNS, thus hardcoding 262-- it into `keys` (not APPENDED by the capture engine). 263-- Mapping EXECUTION getcharstr() consumes "q" _when executed_, thus "q" is APPENDED to `keys`. 264n . exec_lua ( [[ 265vim.keymap.set('o', 's', function() 266return ('<Cmd>let g:read_input = %q . getcharstr()<CR>'):format(vim.fn.getcharstr()) 267end, { expr = true }) 268]] ) 269for _ , keys in ipairs ( { '2dseq' , 'd2seq' } ) do 270feed ( keys ) 271local ev =atom_last () 272eq ( 273{ lhs ='dseq' , keys =k ( '2d<Cmd>let g:read_input = "e" . getcharstr()<NL>q' ) } , 274pick ( ev , 'lhs' , 'keys' ) 275) 276eq ( 'eq' , api . nvim_get_var ( 'read_input' )) 277api . nvim_set_var ( 'read_input' , '' ) 278api . nvim_feedkeys ( ev . keys , 'nx' , false ) 279eq ( 'eq' , api . nvim_get_var ( 'read_input' )) 280end 281end ) 282 283it ( 'mapping that enters :terminal mode' , function () 284-- Fake picker/fuzzy-finder: a mapping that opens a :terminal UI (like fzf-lua). 285-- Its composite ends when :terminal is entered. 286n . exec_lua ( function ( prg ) 287vim . keymap . set ( 'n' , ',f' , function () 288vim . cmd ( 'enew' ) 289vim . fn . jobstart ( { prg , 'INTERACT' } , { term =true } ) 290vim . cmd . startinsert () 291end ) 292end , n . testprg ( 'shell-test' )) 293atoms_start () 294feed ( ',f' ) 295n . poke_eventloop () 296eq ( 297{ buftype ='terminal' , mode ='t' } , 298n . exec_lua ( 'return { buftype = vim.bo.buftype, mode = vim.fn.mode(1) }' ) 299) 300feed ( 'exit<CR>' ) -- Terminal-mode input: no atoms. 301n . poke_eventloop () 302feed ( [[<C-\><C-N>]] ) 303feed ( 'gg' ) 304eq ( { 305{ type ='mapping' , lhs =',f' } , 306{ type ='jump' , lhs ='gg' , keys ='gg' } , 307} , atoms_tail ( 2 , 'type' , 'lhs' , 'keys' )) 308end ) 309 310it ( 'simple mapping (single command) "unwraps" to the underlying type' , function () 311command ( 'nnoremap ,d dw' ) 312fn . setline ( 1 , { 'one two aa bb cc dd' } ) 313feed ( 'gg0' ) 314atoms_start () 315feed ( ',d' ) 316eq ( { 'two aa bb cc dd' } , get_lines ()) 317local evs =atoms () 318eq ( 1 , # evs ) 319-- Inapplicable fields (count/reg/arg/motionforce/text/atoms) are omitted. 320eq ( { 321type ='operator' , -- Unwrapped. 322keys ='dw' , 323operator ='d' , 324cmd ='w' , 325changed =true , 326lhs =',d' , 327moved =false , -- "dw" deletes at the cursor; does not move! 328pos ={ 1 , 0 } , -- Original cursor position. 329undoseq =2 , -- Undo state after the change; decreases on undo (see |restore-undo-cursor|). 330} , evs [ # evs ]) 331-- Count/register are captured; one event per occurrence. 332feed ( '"z2dw' ) 333feed ( '"z2dw' ) 334evs =atoms () 335eq ( { keys ='"z2dw' , count =2 , reg ='z' } , pick ( evs [ # evs ], 'keys' , 'count' , 'reg' )) 336-- Identical occurrences, except `undoseq`: each edit is a new undo state. 337evs [ # evs ]. undoseq , evs [ # evs - 1 ]. undoseq =nil , nil 338eq ( evs [ # evs - 1 ], evs [ # evs ]) 339end ) 340 341it ( 'complex mapping with edits/motions emits one type=mapping atom' , function () 342-- Split the line at the cursor, ending at the EOL of the first half. 343command ( 'nnoremap gj i<c-j><esc>k$' ) 344fn . setline ( 1 , { 'aaa bbb' } ) 345feed ( 'gg04l' ) 346atoms_start () 347feed ( 'gj' ) 348eq ( { 'aaa ' , 'bbb' } , get_lines ()) 349-- The mapping commands (insert-session, k, $) accumulate and fold into one CmdAtom, labeled 350-- with the user-typed LHS; the resolved keys remain the (replayable) payload. 351local evs =atoms () 352eq ( 1 , # evs ) 353eq ( 354{ type ='mapping' , lhs ='gj' , keys =k ( '1i<NL><Esc>k$' ), changed =true } , 355pick ( evs [ 1 ], 'type' , 'lhs' , 'keys' , 'changed' ) 356) 357-- The sub-commands are exposed as subatoms. 358eq ( { 359{ type ='insert' , keys =k ( '1i<NL><Esc>' ) } , 360{ type ='motion' , keys ='k' } , 361{ type ='motion' , keys ='$' } , 362} , subatoms ( evs [ 1 ], 'type' , 'keys' )) 363eq ( { 'k' , false } , { evs [ 1 ]. atoms [ 2 ]. cmd , evs [ 1 ]. atoms [ 2 ]. changed } ) 364-- A scroll inside a mapping is not a subatom: the composite's keys must stay replayable, so 365-- the scroll is elided. 366fn . setline ( 1 , { 'l1' , 'l2' , 'l3' , 'l4' , 'l5' , 'l6' } ) 367feed ( '3G' ) 368command ( 'nnoremap gk <C-e>j$' ) 369feed ( 'gk' ) 370eq ( 4 , fn . line ( '.' )) 371eq ( { type ='mapping' , lhs ='gk' , keys ='j$' } , pick ( atom_last (), 'type' , 'lhs' , 'keys' )) 372-- A recursive mapping (:nmap gJ gj) does not nest: the inner mapping's commands flatten 373-- into ONE composite labeled with the typed LHS, with the same resolved keys. 374command ( 'nmap gJ gj' ) 375api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa bbb' } ) 376feed ( 'gg04l' ) 377local before =# atoms () 378feed ( 'gJ' ) 379eq ( { 'aaa ' , 'bbb' } , get_lines ()) 380evs =atoms () 381eq ( before + 1 , # evs ) 382eq ( 383{ type ='mapping' , lhs ='gJ' , keys =k ( '1i<NL><Esc>k$' ) } , 384pick ( evs [ # evs ], 'type' , 'lhs' , 'keys' ) 385) 386end ) 387 388it ( 'Visual-mode mapping spanning Visual/Normal/Insert is one atom' , function () 389-- Unlike Insert mode, Visual is a MODE_NORMAL mode, so atom_map_start() opens the composite 390-- up front and it spans every mode the RHS passes through. 391command ( 'xnoremap q <esc>0lix<esc>l' ) 392fn . setline ( 1 , { 'ABCD' } ) 393feed ( 'gg0' ) 394atoms_start () 395feed ( 'vl' ) 396feed ( 'q' ) 397local evs =atoms () 398eq ( 1 , # evs ) 399eq ( 400{ type ='mapping' , lhs ='q' , keys =k ( 'vl<Esc>0l1ix<Esc>l' ) } , 401pick ( evs [ 1 ], 'type' , 'lhs' , 'keys' ) 402) 403eq ( { 404{ type ='visual' , keys =k ( 'vl<Esc>' ) } , 405{ type ='motion' , keys ='0' } , 406{ type ='motion' , keys ='l' } , 407{ type ='insert' , keys =k ( '1ix<Esc>' ) } , 408{ type ='motion' , keys ='l' } , 409} , subatoms ( evs [ 1 ], 'type' , 'keys' )) 410eq ( { 'AxBCD' } , get_lines ()) 411end ) 412 413it ( 'mapping that returns to the mode it started in' , function () 414-- The composite closes where the returned-to mode resolves (insert-session end, selection use), 415-- so the keys typed after the mapping fold into its atom. 416command ( 'inoremap <F2> <Esc>lli' ) 417fn . setline ( 1 , { 'abcdefg' } ) 418feed ( 'gg0' ) 419atoms_start () 420feed ( 'iAB<F2>CD<Esc>' ) 421local evs =atoms () 422eq ( 2 , # evs ) 423eq ( { type ='insert' , keys =k ( '1iAB<Esc>' ) } , pick ( evs [ 1 ], 'type' , 'keys' )) 424-- `lhs` is the trigger plus the keys typed while the composite ran. 425eq ( 426{ type ='mapping' , lhs =k ( '<F2>CD<Esc>' ), keys =k ( 'll1iCD<Esc>' ) } , 427pick ( evs [ 2 ], 'type' , 'lhs' , 'keys' ) 428) 429eq ( { 'ABaCDbcdefg' } , get_lines ()) 430 431-- A Normal-mode mapping ending in Insert has the same shape: returning to the starting mode is 432-- not special, the composite simply spans the session. 433command ( 'nnoremap <F3> lli' ) 434api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abcdefg' } ) 435feed ( 'gg0' ) 436local before =# atoms () 437feed ( '<F3>CD<Esc>' ) 438evs =atoms () 439eq ( before + 1 , # evs ) 440eq ( 441{ type ='mapping' , lhs =k ( '<F3>CD<Esc>' ), keys =k ( 'll1iCD<Esc>' ) } , 442pick ( evs [ # evs ], 'type' , 'lhs' , 'keys' ) 443) 444eq ( { 'abCDcdefg' } , get_lines ()) 445 446-- Visual round-trip: selection is pending at mapping end, keeps the composite open. 447command ( 'xnoremap <F4> <Esc>jvl' ) 448api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abcd' , 'efgh' } ) 449feed ( 'gg0' ) 450before =# atoms () 451feed ( 'vl<F4>' ) 452eq ( before , # atoms ()) 453feed ( 'd' ) 454evs =atoms () 455eq ( before + 1 , # evs ) 456eq ( 457{ type ='mapping' , lhs =k ( '<F4>d' ), keys =k ( 'vl<Esc>jvld' ) } , 458pick ( evs [ # evs ], 'type' , 'lhs' , 'keys' ) 459) 460eq ( { 461{ type ='visual' , keys =k ( 'vl<Esc>' ) } , 462{ type ='motion' , keys ='j' } , 463{ type ='visual' , keys ='vld' } , 464} , subatoms ( evs [ # evs ], 'type' , 'keys' )) 465eq ( { 'abcd' , 'eh' } , get_lines ()) 466end ) 467 468it ( 'Cmdline mapping captures Normal-mode tail' , function () 469-- Cmdline-mode is non-MODE_NORMAL, so atom_map_start() defers the composite. 470command ( 'cnoremap <F2> <C-c>x' ) 471fn . setline ( 1 , { 'ABCD' } ) 472feed ( 'gg0' ) 473atoms_start () 474feed ( ':' ) 475feed ( '<F2>' ) 476-- "x" resolves to its builtin translation "dl"; `lhs` is still the user input. 477eq ( 478{ { type ='operator' , lhs =k ( '<F2>' ), keys ='dl' } } , 479atoms_tail ( 1 , 'type' , 'lhs' , 'keys' ) 480) 481eq ( { 'BCD' } , get_lines ()) 482 483-- A mapping that stays in the cmdline opens no composite: the accepted cmdline is the atom, 484-- labeled with the cmdline rather than the LHS. 485command ( 'cnoremap <F3> echo 1' ) 486local before =# atoms () 487feed ( ':<F3><CR>' ) 488eq ( before + 1 , # atoms ()) 489eq ( 490{ { type ='excmd' , lhs =':echo 1\n' , keys =':echo 1\n' } } , 491atoms_tail ( 1 , 'type' , 'lhs' , 'keys' ) 492) 493end ) 494 495it ( 'Terminal-mode mapping captures Normal-mode tail' , function () 496-- Terminal-mode is non-MODE_NORMAL, so atom_map_start() defers the composite. 497command ( 'terminal' ) 498command ( 'tnoremap <F2> <C-\\><C-n>gg' ) 499atoms_start () 500feed ( 'i' ) -- Enter Terminal-mode. 501feed ( '<F2>' ) 502eq ( { 503{ type ='normal' , lhs ='i' , keys ='i' } , 504{ type ='jump' , lhs =k ( '<F2>' ), keys ='gg' } , -- Labeled with the mapping. 505} , atoms_tail ( 2 , 'type' , 'lhs' , 'keys' )) 506eq ( 'nt' , api . nvim_get_mode (). mode ) 507 508-- RHS returning to Terminal mode. The trailing "i" is not captured, unlike the typed "i". 509command ( [[tnoremap <F3> <C-\><C-n>ggi]] ) 510local before =# atoms () 511feed ( 'i' ) 512feed ( '<F3>' ) 513-- Only the typed "i" and the motion: the RHS's trailing "i" is captured as nothing. 514eq ( before + 2 , # atoms ()) 515eq ( { 516{ type ='normal' , lhs ='i' , keys ='i' } , 517{ type ='jump' , lhs =k ( '<F3>' ), keys ='gg' } , 518} , atoms_tail ( 2 , 'type' , 'lhs' , 'keys' )) 519eq ( 't' , api . nvim_get_mode (). mode ) 520end ) 521 522it ( '":call" payload mapping appends payload to `keys`' , function () 523n . exec ( t_atom . delsurround_vim ) 524fn . setline ( 1 , { 'a (one)' , 'b (two)' } ) 525feed ( 'gg0f(' ) 526atoms_start () 527feed ( 'ds)' ) -- ")" is the getchar()'d payload 528eq ( { 'a one' , 'b (two)' } , get_lines ()) 529local ev =atom_last () 530-- getchar() payload is appended to `keys`, replayable. 531eq ( { lhs ='ds)' , keys =':call DelSurround()\n)' } , pick ( ev , 'lhs' , 'keys' )) 532feed ( '2G0f(' ) 533n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 534eq ( { 'a one' , 'b two' } , get_lines ()) 535 536-- input() payload is appended to `keys`, replayable. 537n . exec ( [[ 538function! Suffix() abort 539call setline('.', getline('.') .. input('suffix: ')) 540endfunction 541nnoremap ,s :<C-U>call Suffix()<CR> 542]] ) 543feed ( 'gg' ) 544feed ( ',sX<CR>' ) 545n . poke_eventloop () 546eq ( 'a oneX' , fn . getline ( 1 )) 547ev =atom_last () 548eq ( { lhs =k ( ',sX<CR>' ), keys =':call Suffix()\nX\r' } , pick ( ev , 'lhs' , 'keys' )) 549feed ( 'j' ) 550n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 551eq ( 'b twoX' , fn . getline ( 2 )) 552 553-- Operator-pending mapping (:omap custom motion, like vim-sneak "z") fully captured. 554n . exec ( t_atom . minisneak_vim ) 555api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aa (x) here' , 'bb (y) here' } ) 556feed ( 'gg0' ) 557n . poke_eventloop () 558feed ( 'dzhe' ) 559n . poke_eventloop () 560eq ( 'here' , fn . getline ( 1 )) 561ev =atom_last () 562eq ( 563{ type ='operator' , operator ='d' , lhs ='dzhe' , keys ='d:call MiniSneak()\nhe' } , 564pick ( ev , 'type' , 'operator' , 'lhs' , 'keys' ) 565) 566feed ( 'j0' ) 567n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 568eq ( 'here' , fn . getline ( 2 )) 569 570-- An :omap that abandons the operator is still one replayable atom. 571command ( 'onoremap <F2> <Esc>x' ) 572api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abcd' } ) 573feed ( 'gg0' ) 574local before =# atoms () 575feed ( 'd<F2>' ) 576-- The typed "d" is part of the composite atom. 577eq ( before + 1 , # atoms ()) 578eq ( 579{ { type ='mapping' , lhs =k ( 'd<F2>' ), keys =k ( '<Esc>dl' ) } } , 580atoms_tail ( 1 , 'type' , 'lhs' , 'keys' ) 581) 582eq ( { 'bcd' } , get_lines ()) 583 584-- Burst input ("f(" and the mapping arrive together): "f" peeks for a composing char with 585-- mappings enabled, so "ds" resolves while "f(" is still executing. The motion is still its 586-- own atom, and the K_IGNORE left by the peek stays out of `lhs`. 587api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'a (one)' } ) 588feed ( 'gg0' ) 589n . poke_eventloop () 590feed ( 'f(ds)' ) 591eq ( { 'a one' } , get_lines ()) 592eq ( { 593{ type ='motion' , keys ='f(' , lhs ='f(' } , 594{ type ='excmd' , keys =':call DelSurround()\n)' , lhs ='ds)' } , 595} , atoms_tail ( 2 , 'type' , 'keys' , 'lhs' )) 596 597-- Same, mid-op ("t(" + "ds" in one batch): the next mapping is not the pending op's operand. 598command ( 'nnoremap ,D d' ) 599api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'a x(one)' } ) 600feed ( 'gg0' ) 601n . poke_eventloop () 602feed ( ',Dt(ds)' ) 603eq ( { 'one' } , get_lines ()) 604eq ( { 605{ type ='operator' , keys ='dt(' , lhs =',Dt(' } , 606{ type ='excmd' , keys =':call DelSurround()\n)' , lhs ='ds)' } , 607} , atoms_tail ( 2 , 'type' , 'keys' , 'lhs' )) 608end ) 609 610it ( 'replay of deleted/redefined Lua mapping fails (E5117)' , function () 611n . exec_lua ( [[ 612vim.keymap.set('o', 'gt', function() 613vim.cmd('normal! viw') 614end) 615]] ) 616fn . setline ( 1 , { 'k1 k2' , 'k3 k4' } ) 617atoms_start () 618feed ( 'gg0dgt' ) 619eq ( ' k2' , fn . getline ( 1 )) 620local ev =atom_last () 621n . exec_lua ( [[ 622vim.keymap.del('o', 'gt') 623vim.keymap.set('o', 'gt', function() 624vim.cmd('normal! viw') 625end) 626]] ) 627feed ( 'j0' ) 628n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 629eq ( 'k3 k4' , fn . getline ( 2 )) 630t . matches ( 'E5117' , fn . execute ( 'messages' )) 631-- Running the (re-defined) mapping emits CmdAtom.keys with the updated id. 632feed ( 'dgt' ) 633eq ( ' k4' , fn . getline ( 2 )) 634n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( atom_last (). keys )) 635eq ( 'k4' , fn . getline ( 2 )) 636end ) 637 638it ( 'temporary mapping ("submode"), expired by the next unrelated atom' , function () 639n . exec_lua ( [==[ 640local active = false 641vim.api.nvim_create_autocmd('CmdAtom', { 642callback = function(ev) 643-- `cmd` is a key-notation name (unlike `keys`, which is raw bytes). 644local resize = ev.data.cmd == '<C-W>+' or ev.data.cmd == '<C-W>-' 645if resize then 646-- Activate. Use of "+"/"-" resolves to the same cmd => re-activates. 647vim.keymap.set('n', '+', '<C-w>+') 648vim.keymap.set('n', '-', '<C-w>-') 649active = true 650elseif active then 651vim.keymap.del('n', '+') 652vim.keymap.del('n', '-') 653active = false 654end 655end, 656}) 657-- Also works if <c-w>+ was mapped to something else: 658vim.cmd[[nnoremap <leader>+ <c-w>+]] 659]==] ) 660--- Waits for the deferred CmdAtom to (de)activate the temporary mappings. 661local function wait_active ( active ) 662eq ( 663true , 664n . exec_lua ( 665[[ 666local active = ... 667return vim.wait(1000, function() 668return (vim.fn.maparg('+', 'n') ~= '') == active 669end) 670]] , 671active 672) 673) 674end 675fn . setline ( 1 , { 'one' , 'two' } ) 676command ( 'split' ) 677local height =fn . winheight ( 0 ) 678feed ( '<C-w>+' ) 679wait_active ( true ) -- the deferred CmdAtom activated the mappings 680eq ( height + 1 , fn . winheight ( 0 )) 681feed ( '+' ) -- temporary mapping: resizes without the CTRL-W prefix 682n . poke_eventloop () 683eq ( height + 2 , fn . winheight ( 0 )) 684feed ( '-' ) -- its own use emits the same resolved keys: stays active 685n . poke_eventloop () 686eq ( height + 1 , fn . winheight ( 0 )) 687feed ( 'j' ) -- any unrelated atom expires the mappings 688wait_active ( false ) 689feed ( '-' ) -- back to the builtin: a motion (up one line), not a resize 690n . poke_eventloop () 691eq ( height + 1 , fn . winheight ( 0 )) 692eq ( 1 , fn . line ( '.' )) 693-- A mapping whose atom RESOLVES to a resize also activates the submode. 694feed ( '\\+' ) 695n . poke_eventloop () 696eq ( height + 2 , fn . winheight ( 0 )) 697wait_active ( true ) 698end ) 699end ) 700 701describe ( 'insert-mode' , function () 702it ( 'session captures its text' , function () 703fn . setline ( 1 , { 'aaa' } ) 704feed ( 'gg0' ) 705atoms_start () 706-- No event for the "i" entry, and none per keystroke: one whole-session event at <Esc>. 707feed ( 'i' ) 708eq ( 0 , # atoms ()) 709feed ( 'X' ) 710eq ( 0 , # atoms ()) 711feed ( 'Y' ) 712eq ( 0 , # atoms ()) 713feed ( '<Esc>' ) 714local evs =atoms () 715eq ( 1 , # evs ) 716-- Insert keys always embed count ("1i…"). "dw" omits its count. 717eq ( 718{ type ='insert' , count =1 , text ='XY' , keys =k ( '1iXY<Esc>' ) } , 719pick ( evs [ 1 ], 'type' , 'count' , 'text' , 'keys' ) 720) 721-- Counted insert: entry cmd + text + <Esc> keys. 722feed ( '3iZ<Esc>' ) 723eq ( { type ='insert' , count =3 , text ='Z' } , pick ( atom_last (), 'type' , 'count' , 'text' )) 724end ) 725 726it ( 'mapping captures Normal-mode tail #41864' , function () 727-- Insert-mode is non-MODE_NORMAL, so atom_map_start() defers the composite. 728 729-- "inoremap <Esc> <Esc>l": <Esc> ends the session; the trailing "l" runs in Normal mode. 730command ( 'inoremap <Esc> <Esc>l' ) 731fn . setline ( 1 , { 'echo hi' } ) 732feed ( 'W' ) -- column 5 733atoms_start () 734feed ( 'i<Esc>' ) 735-- Two atoms: (empty) insert session, then the mapping tail ("l"). 736local evs =atoms () 737eq ( 2 , # evs ) 738eq ( { type ='insert' , keys =k ( '1i<Esc>' ) } , pick ( evs [ 1 ], 'type' , 'keys' )) 739eq ( { type ='motion' , lhs =k ( '<Esc>' ), keys ='l' } , pick ( evs [ 2 ], 'type' , 'lhs' , 'keys' )) 740-- The tail moved the cursor right (column 5), overriding the default leftward "<Esc>". 741eq ( { 1 , 5 } , api . nvim_win_get_cursor ( 0 )) 742 743-- Same shape when the session is not empty: the tail is independent of the session's text. 744fn . setline ( 1 , { 'echo hi' } ) 745feed ( 'gg0W' ) -- column 5 again 746local before =# atoms () -- a second atoms_start() would double-register the collector 747feed ( 'iZZ<Esc>' ) 748evs =atoms () 749eq ( before + 2 , # evs ) 750eq ( 751{ type ='insert' , keys =k ( '1iZZ<Esc>' ), text ='ZZ' } , 752pick ( evs [ # evs - 1 ], 'type' , 'keys' , 'text' ) 753) 754eq ( { type ='motion' , lhs =k ( '<Esc>' ), keys ='l' } , pick ( evs [ # evs ], 'type' , 'lhs' , 'keys' )) 755eq ( { 'echo ZZhi' } , get_lines ()) 756eq ( { 1 , 7 } , api . nvim_win_get_cursor ( 0 )) 757end ) 758 759it ( 'crazy mapping captured as type=insert + type=mapping' , function () 760-- The RHS leaves Insert mode, then runs motions and further insert sessions. 761command ( 'inoremap x ....<esc>0laa..<esc>ab<space><space><esc>hic<esc>' ) 762fn . setline ( 1 , { 'AB' } ) 763feed ( 'gg0' ) 764atoms_start () 765feed ( 'ix' ) 766local evs =atoms () 767eq ( 2 , # evs ) 768-- The insert-session (interrupted by the mapping) is a `type=insert` atom. 769eq ( 770{ type ='insert' , keys =k ( '1i....<Esc>' ), text =( '.' ): rep ( 4 ) } , 771pick ( evs [ 1 ], 'type' , 'keys' , 'text' ) 772) 773-- The tail of the mapping (which started in insert-mode...) is a `type=mapping` atom. 774eq ( { 775type ='mapping' , 776lhs ='x' , 777keys =k ( '0l1aa..<Esc>1ab <Esc>h1ic<Esc>' ), 778} , pick ( evs [ 2 ], 'type' , 'lhs' , 'keys' )) 779-- Composite subatoms: 780eq ( { 781{ type ='motion' , keys ='0' } , 782{ type ='motion' , keys ='l' } , 783{ type ='insert' , keys =k ( '1aa..<Esc>' ) } , 784{ type ='insert' , keys =k ( '1ab <Esc>' ) } , 785{ type ='motion' , keys ='h' } , 786{ type ='insert' , keys =k ( '1ic<Esc>' ) } , 787} , subatoms ( evs [ 2 ], 'type' , 'keys' )) 788eq ( { ( '..a..bc %sAB' ): format (( '.' ): rep ( 2 )) } , get_lines ()) 789end ) 790 791it ( 'insert-session entered programmatically, still captures *user* input #41516' , function () 792atoms_start () 793n . exec_lua ( [[ 794vim.keymap.set('i', '<C-j>', function() 795vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false) 796vim.schedule(function() 797vim.api.nvim_feedkeys('i', 'n', false) 798end) 799end) 800]] ) 801local before =# atoms () 802feed ( 'i' ) 803n . exec_lua ( 'vim.wait(50)' ) 804feed ( '<C-j>' ) 805n . exec_lua ( 'vim.wait(50)' ) 806feed ( '<Esc>' ) 807local evs =atoms () 808eq ( before + 2 , # evs ) 809eq ( { k ( '1i<Esc>' ), k ( '1i<Esc>' ) } , { evs [ # evs - 1 ]. keys , evs [ # evs ]. keys } ) 810-- Typed text within such a session lands in its atom... 811n . exec_lua ( [[vim.schedule(function() vim.api.nvim_feedkeys('i', 'n', false) end)]] ) 812n . exec_lua ( 'vim.wait(50)' ) 813feed ( 'hi<Esc>' ) 814eq ( { text ='hi' , keys =k ( '1ihi<Esc>' ) } , pick ( atom_last (), 'text' , 'keys' )) 815-- ...but with NO typed input within it, the session emits nothing. 816before =# atoms () 817n . exec_lua ( [[vim.schedule(function() vim.api.nvim_feedkeys('i', 'n', false) end)]] ) 818n . exec_lua ( 'vim.wait(50)' ) 819n . exec_lua ( [[vim.api.nvim_feedkeys('\27', 'n', false)]] ) 820eq ( before , # atoms ()) 821end ) 822end ) 823 824describe ( 'visual' , function () 825it ( 'replay; fallback to equal-size if unreplayable' , function () 826-- The core use-case for a plugin: observe CmdAtom, capture a Visual-mode 827-- operation's resolved `keys`, and replay them verbatim to re-execute it. 828fn . setline ( 1 , { 'foo bar' , 'longword bar' } ) 829feed ( 'gg0' ) 830atoms_start () 831feed ( 'viwd' ) -- select the inner word and delete it 832eq ( { ' bar' , 'longword bar' } , get_lines ()) 833local ev =atom_last () 834eq ( 'visual' , ev . type ) 835 836-- Replay the captured keys at line 2. The keysequence re-executes (not equal-size reselect): 837-- "iw" selects cursor-relative word, so the delete adapts to the new context. 838feed ( 'j0' ) 839n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 840eq ( { ' bar' , ' bar' } , get_lines ()) 841 842-- A viewport scroll that drags the cursor along (edge/'scrolloff') grows the selection by 843-- a viewport-dependent amount: void, no atom. 844local lines =vim . tbl_map ( function ( i ) 845return 'l' .. i 846end , fn . range ( 1 , 30 )) 847fn . setline ( 1 , lines ) 848feed ( 'gg' ) 849local before =# atoms () 850feed ( 'V<C-e>' ) 851eq ( 2 , fn . line ( '.' )) -- Scroll dragged the cursor: selection is lines 1-2. 852feed ( 'd' ) 853-- Publishes with empty `CmdAtom.keys`; the keys that produced it are in `lhs`. 854eq ( before + 1 , # atoms ()) 855eq ( 856{ type ='visual' , keys ='' , lhs =k ( 'V<C-E>d' ), changed =true } , 857pick ( atom_last (), 'type' , 'keys' , 'lhs' , 'changed' ) 858) 859eq ( 'l3' , fn . getline ( 1 )) -- The edit itself deleted both selected lines. 860 861-- "." on the unreplayable operation falls back to equal-size reselect ("1v" + op). 862feed ( '.' ) 863eq ( 'l5' , fn . getline ( 1 )) 864 865-- But a viewport key that preserves cursor ("zz") does not move the selection: still 866-- replayable, and collected like any other subatom. 867fn . setline ( 1 , lines ) 868feed ( 'gg' ) 869before =# atoms () 870feed ( 'Vzzd' ) 871eq ( 'l2' , fn . getline ( 1 )) 872eq ( before + 1 , # atoms ()) 873-- Nothing was translated, so lhs=keys. 874eq ( { type ='visual' , keys ='Vzzd' , lhs ='Vzzd' } , pick ( atom_last (), 'type' , 'keys' , 'lhs' )) 875feed ( '.' ) 876eq ( 'l3' , fn . getline ( 1 )) 877 878-- 879-- Visual selection moved by a Lua mapping (via API): captured as the mapping cmd. #41956 880-- 881n . exec_lua ( function () 882vim . keymap . set ( 'x' , 'gh' , function () 883local cursor =vim . api . nvim_win_get_cursor ( 0 ) 884vim . api . nvim_win_set_cursor ( 0 , { cursor [ 1 ], cursor [ 2 ] + 1 } ) 885vim . cmd ( 'normal! o' ) 886vim . api . nvim_win_set_cursor ( 0 , { cursor [ 1 ], cursor [ 2 ] + 3 } ) 887end ) 888end ) 889api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaaaaaa' , 'bbb' } ) 890feed ( 'gg0vghd' ) 891eq ( 'aaaa' , fn . getline ( 1 )) 892eq ( 'visual' , atom_last (). type ) 893feed ( 'j0.' ) 894eq ( 'b' , fn . getline ( 2 )) -- The API move clamps on the short line; "1v" would have emptied it. 895-- Also when only the fed key follows the API move: "o" alone would replay a 1-char selection. 896-- Defined >=10 times: at least 1 mapping-id ends in "0" (exercises redobuf encoding...). 897for _ =1 , 10 do 898n . exec_lua ( function () 899vim . keymap . set ( 'x' , 'gH' , function () 900local cursor =vim . api . nvim_win_get_cursor ( 0 ) 901vim . api . nvim_win_set_cursor ( 0 , { cursor [ 1 ], cursor [ 2 ] + 1 } ) 902vim . cmd ( 'normal! o' ) 903end ) 904end ) 905api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaaaaaa' , 'bbbbbbb' } ) 906feed ( 'gg0vgHd' ) 907eq ( 'aaaaa' , fn . getline ( 1 )) 908feed ( 'j0.' ) 909eq ( 'bbbbb' , fn . getline ( 2 )) 910end 911-- A fed "gv" reselects marks the mapping set. 912n . exec_lua ( function () 913vim . keymap . set ( 'x' , 'gs' , function () 914local row , col =unpack ( vim . api . nvim_win_get_cursor ( 0 )) 915vim . fn . setpos ( "'<" , { 0 , row , col + 1 , 0 } ) 916vim . fn . setpos ( "'>" , { 0 , row , vim . fn . col ( '$' ) - 1 , 0 } ) 917vim . cmd . normal ( { 'gv' , bang =true } ) 918end ) 919end ) 920api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaaaaaa' , 'bbbbbbbbbb' } ) 921feed ( 'gg0vgsd' ) 922eq ( '' , fn . getline ( 1 )) 923feed ( 'j0.' ) 924eq ( '' , fn . getline ( 2 )) -- To end of line again; "1v" would have left 3 chars. 925 926-- Textobject mapping that ends selection and reselects by "norm! <linenr>GV…". #41754 927n . exec_lua ( function () 928vim . keymap . set ( 'x' , '<M-m>' , function () 929local cur_line =vim . fn . line ( '.' ) 930vim . cmd ( 'normal! \27' ) 931vim . cmd (( 'normal! %dGV%dG' ): format ( cur_line , 2 * cur_line )) 932end ) 933end ) 934api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' } ) 935feed ( 'ggv<M-m>d' ) 936eq ( { '3' , '4' , '5' , '6' , '7' , '8' } , get_lines ()) 937feed ( 'j.' ) 938eq ( { '3' , '7' , '8' } , get_lines ()) 939 940-- "gv" is replayable, but "." redoes a fixed-size region ("1v"), like Vim. 941api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa bbb ccc' } ) 942feed ( 'gg0viw<Esc>' ) 943before =# atoms () 944feed ( 'gvd' ) 945eq ( ' bbb ccc' , fn . getline ( 1 )) 946eq ( before + 1 , # atoms ()) 947eq ( { type ='visual' , keys ='gvd' , lhs ='gvd' } , pick ( atom_last (), 'type' , 'keys' , 'lhs' )) 948feed ( 'w.' ) -- "." containing "gv" replays "1v" fallback at the cursor ("bbb"). 949eq ( ' ccc' , fn . getline ( 1 )) 950-- Visual-entered Insert. The atom has "gv" (as typed), not redo's "1v" fallback. 951api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa bbb ccc' } ) 952feed ( 'gg0viw<Esc>' ) 953before =# atoms () 954feed ( 'gvcX<Esc>' ) 955eq ( 'X bbb ccc' , fn . getline ( 1 )) 956eq ( before + 1 , # atoms ()) 957eq ( { type ='visual' , keys =k ( 'gvcX<Esc>' ) } , pick ( atom_last (), 'type' , 'keys' )) 958feed ( 'w.' ) 959eq ( 'X X ccc' , fn . getline ( 1 )) 960 961-- A fed (":normal!") Visual-put preps the selection keysequence, like any fed visual 962-- operator (":normal! vjd"): "." re-executes "Vjp", not a bare "p". 963api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aa' , 'bb' , 'cc' , 'dd' , 'ee' } ) 964feed ( 'ggyy' ) 965command ( 'normal! Vjp' ) 966eq ( { 'aa' , 'cc' , 'dd' , 'ee' } , get_lines ()) 967feed ( 'j.' ) 968eq ( { 'aa' , 'aa' , 'bb' , 'ee' } , get_lines ()) 969 970-- {Visual}r<C-V><CR> replaces with a literal <CR> (REPLACE_CR_NCHAR): inexpressible as 971-- spec chars, so the literal keys compose the redo tail, which "." replays. 972api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abcd' , 'efgh' } ) 973feed ( 'gg0vlr<C-V><CR>' ) 974eq ( { '\r\rcd' , 'efgh' } , get_lines ()) 975feed ( 'j0.' ) 976eq ( { '\r\rcd' , '\r\rgh' } , get_lines ()) 977 978-- <Cmd> ":norm" mapping that extends the selection: "." replays it. #41323 979command ( 'xmap <M-m> <Cmd>normal! $<CR>' ) 980api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa' , 'bbb' } ) 981feed ( 'gg0v<M-m>d' ) -- "$" in Visual is one past EOL, so "d" swallows the newline (= plain v$d) 982eq ( { 'bbb' } , get_lines ()) 983command ( 'let v:errmsg = ""' ) 984feed ( '.' ) 985eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 986eq ( 'n' , fn . mode ()) -- Not in Visual. 987eq ( { '' } , get_lines ()) 988 989-- <Cmd> ":norm" captured as itself (not its subatoms). 990command ( 'xmap <M-w> <Cmd>normal! e<CR>' ) 991api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one two three' , 'four five six' } ) 992feed ( 'gg0v<M-w>d' ) 993eq ( { ' two three' , 'four five six' } , get_lines ()) 994eq ( { type ='visual' , keys =k ( 'v<Cmd>normal! e<NL>d' ) } , pick ( atom_last (), 'type' , 'keys' )) 995feed ( 'j0.' ) 996eq ( { ' two three' , ' five six' } , get_lines ()) 997 998-- Nested command can replace the pending selection, not just extend it. #41705 999n . exec_lua ( function () 1000vim . keymap . set ( 'x' , 'Z' , function () 1001-- Mapping ends with the selection "open". 1002vim . cmd . normal ( { vim . keycode ( '<Esc>viW' ), bang =true } ) 1003end ) 1004end ) 1005api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo.bar tail' } ) 1006feed ( 'gg0' ) 1007command ( 'normal viwZ' ) 1008feed ( 'd' ) 1009eq ( { ' tail' } , get_lines ()) 1010eq ( { type ='visual' , keys ='viWd' } , pick ( atom_last (), 'type' , 'keys' )) 1011 1012-- Buffer-editing <Cmd> is captured as itself. 1013command ( 'xmap <M-a> <Cmd>call append(1, "X")<CR>' ) 1014api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'ab' , 'cd' } ) 1015feed ( 'gg0vl<M-a>d' ) 1016eq ( { '' , 'X' , 'cd' } , get_lines ()) 1017eq ( 1018{ type ='visual' , keys =k ( 'vl<Cmd>call append(1, "X")<NL>d' ) } , 1019pick ( atom_last (), 'type' , 'keys' ) 1020) 1021feed ( '3gg0.' ) 1022eq ( { '' , 'X' , '' } , get_lines ()) 1023 1024-- Search-extended selection: the payload travels in the collected keys. 1025api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'ab META x' , 'cdef META y' } ) 1026feed ( 'gg0v/META<CR>d' ) 1027eq ( { 'ETA x' , 'cdef META y' } , get_lines ()) 1028eq ( { type ='visual' , keys =k ( 'v/META<NL>d' ) } , pick ( atom_last (), 'type' , 'keys' )) 1029feed ( 'j0.' ) 1030eq ( { 'ETA x' , 'ETA y' } , get_lines ()) 1031 1032-- Excmd that visually-selects. Replaying ":" prefills "'<,'>"; keys place <C-U> internally. 1033exec ( [[ 1034func SelectWord() 1035normal! viw 1036endfunc 1037]] ) 1038api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo bar' , 'longword bar' } ) 1039feed ( 'gg0v:<C-U>call SelectWord()<CR>d' ) 1040eq ( { ' bar' , 'longword bar' } , get_lines ()) 1041eq ( 1042{ type ='visual' , keys =k ( 'v:<C-U>call SelectWord()<NL>d' ) } , 1043pick ( atom_last (), 'type' , 'keys' ) 1044) 1045feed ( 'j0.' ) 1046eq ( { ' bar' , ' bar' } , get_lines ()) 1047 1048-- Nothing typed a <C-U> here: this ":" keeps the prefilled range. 1049api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'a xx' , 'b xx' , 'c xx' } ) 1050feed ( 'gg0Vj:s/xx/YY/<CR>' ) 1051eq ( { 'a YY' , 'b YY' , 'c xx' } , get_lines ()) 1052eq ( k ( "Vj:<C-U>'<,'>s/xx/YY/<NL>" ), atom_last (). keys ) 1053end ) 1054 1055it ( '|visual-fixed-size| example in visual.txt' , function () 1056n . exec_lua ( [[ 1057local vop ---@type string? 1058vim.api.nvim_create_autocmd('CmdAtom', { 1059callback = function(ev) 1060if ev.data.type == 'visual' then 1061local children = ev.data.atoms 1062vop = children and children[#children].keys or nil 1063elseif ev.data.changed then 1064vop = nil -- the last change is no longer the Visual one 1065end 1066end, 1067}) 1068vim.keymap.set('n', '.', function() 1069vim.schedule(function() 1070vim.api.nvim_feedkeys(vop and ('1v' .. vop) or '.', 'n', false) 1071end) 1072end) 1073]] ) 1074fn . setline ( 1 , { 'foo bar' , 'longword bar' } ) 1075feed ( 'gg0viwd' ) 1076eq ( { ' bar' , 'longword bar' } , get_lines ()) 1077-- Equal-size repeat: a 3-char region at the cursor, NOT that line's word. 1078feed ( 'j0.' ) 1079retry ( nil , 1000 , function () 1080eq ( { ' bar' , 'gword bar' } , get_lines ()) 1081end ) 1082-- A non-visual change falls back to the builtin |.|. 1083feed ( 'gg0x' ) 1084eq ( { 'bar' , 'gword bar' } , get_lines ()) 1085feed ( 'j0.' ) 1086retry ( nil , 1000 , function () 1087eq ( { 'bar' , 'word bar' } , get_lines ()) 1088end ) 1089end ) 1090end ) 1091 1092describe ( 'repeat.txt examples' , function () 1093it ( '|action-repeat|' , function () 1094n . exec_lua ( [[ 1095_G.saved = nil 1096vim.api.nvim_create_autocmd('CmdAtom', { callback = function(ev) _G.last = ev.data end }) 1097_G.save = function() _G.saved = _G.last end 1098_G.replay = function() 1099local d = _G.saved 1100vim.api.nvim_feedkeys(d.keys or d.lhs, d.keys and 'n' or 'm', false) 1101end 1102]] ) 1103n . exec ( t_atom . delsurround_vim ) 1104command ( 'nnoremap ,d x' ) 1105command ( 'nnoremap <F6> xw' ) 1106command ( 'let @q = "x"' ) 1107n . exec_lua ( [[ 1108vim.keymap.set('n', ']e', function() 1109vim.api.nvim_set_current_line(vim.api.nvim_get_current_line() .. '!') 1110end) 1111]] ) 1112 1113--- Runs `keys` on line 1, then replays that atom on line 2 (both prefixed by `pre`). Reports 1114--- the atom's input/resolution pair, whether the resolution decomposes into subatoms, and 1115--- whether the replay reproduced line 1. 1116local function both ( line , pre , keys ) 1117api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { line , line } ) 1118feed ( 'gg0' .. pre ) 1119n . poke_eventloop () 1120feed ( keys ) 1121n . poke_eventloop () 1122n . exec_lua ( '_G.save()' ) 1123feed ( '2G0' .. pre ) 1124n . poke_eventloop () 1125n . exec_lua ( '_G.replay()' ) 1126n . poke_eventloop () 1127local l =get_lines () 1128local d =n . exec_lua ( 'return _G.saved' ) 1129local subs =d . atoms 1130and table . concat ( vim . tbl_map ( function ( c ) 1131return c . keys 1132end , d . atoms )) 1133return { lhs =d . lhs , keys =d . keys , subs =subs , replayed =l [ 1 ] == l [ 2 ] } 1134end 1135 1136-- Note the symmetry: "x", a mapping to "x", and a macro of "x" all resolve to "dl". 1137eq ( { 1138{ lhs ='dw' , keys ='dw' , replayed =true } , 1139{ lhs ='x' , keys ='dl' , replayed =true } , 1140{ lhs =',d' , keys ='dl' , replayed =true } , 1141{ lhs ='@q' , keys ='dl' , replayed =true } , 1142{ lhs =k ( '<F6>' ), keys ='dlw' , subs ='dlw' , replayed =true } , 1143-- No `keys` (Lua callback): the recipe feeds `lhs` with remapping. 1144{ lhs =']e' , replayed =true } , 1145-- getchar() ")" is appended to `keys`, replayable. 1146{ lhs ='ds)' , keys =':call DelSurround()\n)' , replayed =true } , 1147} , { 1148both ( 'a one two' , '' , 'dw' ), 1149both ( 'aaa bbb' , '' , 'x' ), 1150both ( 'aaa bbb' , '' , ',d' ), 1151both ( 'aaa bbb' , '' , '@q' ), 1152both ( 'aaa bbb ccc' , '' , '<F6>' ), 1153both ( 'a b' , '' , ']e' ), 1154both ( 'a (one)' , 'f(' , 'ds)' ), 1155} ) 1156 1157-- Repeat fold command ("zf"). #9821 1158api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'a {' , ' x' , '}' , 'b {' , ' y' , '}' } ) 1159feed ( 'gg0zfa{' ) 1160n . poke_eventloop () 1161n . exec_lua ( '_G.save()' ) 1162eq ( 1163{ lhs ='zfa{' , keys ='zfa{' , changed =false , operator ='zf' } , 1164pick ( n . exec_lua ( 'return _G.saved' ), 'lhs' , 'keys' , 'changed' , 'operator' ) 1165) 1166feed ( 'zR4gg0' ) -- open the new fold, move past it 1167eq ( 0 , fn . foldlevel ( 4 )) 1168n . exec_lua ( '_G.replay()' ) 1169n . poke_eventloop () 1170eq ( { 4 , 6 } , { fn . foldclosed ( 4 ), fn . foldclosedend ( 4 ) } ) -- "zf" closes what it creates 1171 1172-- Unreplayable: a void Visual op has empty keys, so the recipe feeds nothing 1173-- rather than replaying a viewport-dependent selection. 1174api . nvim_buf_set_lines ( 11750 , 11760 , 1177- 1 , 1178true , 1179vim . tbl_map ( function ( i ) 1180return 'l' .. i 1181end , fn . range ( 1 , 30 )) 1182) 1183feed ( 'gg' ) 1184feed ( k ( 'V<C-e>d' )) 1185n . poke_eventloop () 1186n . exec_lua ( '_G.save()' ) 1187eq ( { keys ='' } , pick ( n . exec_lua ( 'return _G.saved' ), 'keys' )) 1188local before =get_lines () 1189n . exec_lua ( '_G.replay()' ) 1190n . poke_eventloop () 1191eq ( before , get_lines ()) 1192 1193-- A repeat mapping is an atom too, and absent `keys` sends the recipe back to 1194-- its own `lhs`: a recorder must skip it, or the repeat replays itself. 1195n . exec_lua ( [[vim.keymap.set('n', '<F7>', function() end)]] ) 1196feed ( '<F7>' ) 1197n . poke_eventloop () 1198eq ( 1199{ type ='mapping' , lhs =k ( '<F7>' ) } , 1200pick ( n . exec_lua ( 'return _G.last' ), 'type' , 'keys' , 'lhs' ) 1201) 1202end ) 1203 1204it ( '|edit-repeat|' , function () 1205n . exec_lua ( [[ 1206local last ---@type vim.event.cmdatom.data? 1207vim.api.nvim_create_autocmd('CmdAtom', { 1208callback = function(ev) 1209local is_redo_or_undo = ev.data.changed 1210and (ev.data.undoseq or 0) <= (vim.b[ev.buf].maxseq or 0) 1211vim.b[ev.buf].maxseq = math.max(vim.b[ev.buf].maxseq or 0, ev.data.undoseq or 0) 1212if ev.data.changed and not is_redo_or_undo and ev.data.lhs ~= '.' then 1213last = ev.data 1214end 1215end, 1216}) 1217vim.keymap.set('n', '.', function() 1218-- Multicursors: degrade to builtin ".", which repeats at each cursor. 1219local mc = vim.api.nvim_create_namespace('nvim.multicursor') 1220if #vim.api.nvim_buf_get_extmarks(0, mc, 0, -1, { limit = 1 }) > 0 then 1221vim.api.nvim_feedkeys('.', 'n', false) 1222return 1223end 1224vim.schedule(function() 1225if last then 1226vim.api.nvim_feedkeys(last.keys or last.lhs, last.keys and 'n' or 'm', false) 1227end 1228end) 1229end) 1230]] ) 1231n . exec ( t_atom . delsurround_vim ) 1232fn . setline ( 1 , { 'aa bb' , 'a (one)' , 'b (two)' } ) 1233-- "Builtin" edit. 1234feed ( 'gg0dw' ) 1235n . poke_eventloop () 1236feed ( '.' ) 1237retry ( nil , 1000 , function () 1238eq ( '' , fn . getline ( 1 )) 1239end ) 1240-- Payload mapping: builtin "." could not repeat this (without e.g. vim-repeat). 1241feed ( '2G0f(ds)' ) 1242n . poke_eventloop () 1243eq ( 'a one' , fn . getline ( 2 )) 1244feed ( '3G0f(.' ) 1245retry ( nil , 1000 , function () 1246eq ( 'b two' , fn . getline ( 3 )) 1247end ) 1248 1249-- Edit fed by nvim_feedkeys(…, "mt") #41485. 1250n . exec_lua ( [[ 1251vim.keymap.set('n', '<M-m>', function() 1252vim.api.nvim_feedkeys('dl', 'mt', false) 1253end) 1254]] ) 1255api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abcd' } ) 1256feed ( 'gg0<M-m>' ) 1257n . poke_eventloop () 1258eq ( 'bcd' , fn . getline ( 1 )) 1259feed ( '.' ) 1260retry ( nil , 1000 , function () 1261eq ( 'cd' , fn . getline ( 1 )) 1262end ) 1263 1264-- Undo is not captured: "." still repeats the edit. 1265feed ( 'u' ) 1266n . poke_eventloop () 1267eq ( 'bcd' , fn . getline ( 1 )) 1268feed ( '.' ) 1269retry ( nil , 1000 , function () 1270eq ( 'cd' , fn . getline ( 1 )) 1271end ) 1272 1273-- Visual edit: replays the resolved keys, so "iw" selects the cursor-relative word. 1274api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo bar' , 'longword bar' } ) 1275feed ( 'gg0viwd' ) 1276n . poke_eventloop () 1277feed ( 'j0.' ) 1278retry ( nil , 1000 , function () 1279eq ( { ' bar' , ' bar' } , get_lines ()) 1280end ) 1281-- Visual change (insert session): selection, operator, text and <Esc> all replay. 1282api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo bar' , 'longword bar' } ) 1283feed ( 'gg0viwcX<Esc>' ) 1284n . poke_eventloop () 1285feed ( 'j0.' ) 1286retry ( nil , 1000 , function () 1287eq ( { 'X bar' , 'X bar' } , get_lines ()) 1288end ) 1289 1290-- While multicursor is active, the "." mapping degrades to builtin "." so it cascades. 1291api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa' , 'bbb' , 'ccc' } ) 1292feed ( 'gg0QjQj' ) 1293feed ( 'x' ) 1294n . poke_eventloop () 1295eq ( { 'aa' , 'bb' , 'cc' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1296feed ( '.' ) 1297retry ( nil , 1000 , function () 1298eq ( { 'a' , 'b' , 'c' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1299end ) 1300end ) 1301 1302it ( '|motion-repeat|' , function () 1303n . exec_lua ( [[ 1304local last ---@type string? 1305vim.api.nvim_create_autocmd('CmdAtom', { 1306pattern = 'motion', 1307callback = function(ev) 1308last = ev.data.keys 1309end, 1310}) 1311vim.keymap.set('n', ',', function() 1312-- CmdAtom delivery is deferred: schedule the replay AFTER any pending 1313-- event, so `last` is fresh even when "," immediately follows a 1314-- motion. A scheduled replay is programmatic input: it emits no 1315-- CmdAtom itself (no feedback loop). 1316vim.schedule(function() 1317if last then 1318vim.api.nvim_feedkeys(last, 'n', false) -- "n": already resolved 1319end 1320end) 1321end) 1322]] ) 1323local screen =Screen . new ( 30 , 3 ) 1324fn . setline ( 1 , { 'aa bb cc dd ee' } ) 1325feed ( 'gg0' ) 1326feed ( '2w' ) -- last motion: "2w" (count included) 1327n . poke_eventloop () -- deliver the deferred CmdAtom before "," 1328screen : expect ( [[ 1329aa bb ^cc dd ee | 1330{1:~ }| 1331| 1332]] ) 1333feed ( ',' ) -- repeated: "2w" again 1334screen : expect ( [[ 1335aa bb cc dd ^ee | 1336{1:~ }| 1337| 1338]] ) 1339feed ( '0fb' ) -- last motion: "fb" (payload char included) 1340n . poke_eventloop () 1341screen : expect ( [[ 1342aa ^bb cc dd ee | 1343{1:~ }| 1344| 1345]] ) 1346feed ( ',' ) -- repeated "fb": the second "b" 1347screen : expect ( [[ 1348aa b^b cc dd ee | 1349{1:~ }| 1350| 1351]] ) 1352end ) 1353 1354it ( '|restore-undo-cursor|: `pos`, `undoseq` restore across undo' , function () 1355n . exec_lua ( [[ 1356local seen = {} -- buf -> { prev = <seq>, [seq] = <cursor before that edit> } 1357vim.api.nvim_create_autocmd('CmdAtom', { 1358callback = function(ev) 1359local seq = ev.data.undoseq 1360if not seq then 1361return 1362end 1363local s = seen[ev.buf] or {} 1364seen[ev.buf] = s 1365if s.prev and seq < s.prev and s[seq + 1] then 1366vim.api.nvim_win_set_cursor(0, s[seq + 1]) -- undid: first state left behind 1367elseif s.prev and seq > s.prev and s[seq] then 1368vim.api.nvim_win_set_cursor(0, s[seq]) -- redid: a seq we already have 1369elseif ev.data.changed and not s[seq] then 1370s[seq] = ev.data.pos -- new edit 1371end 1372s.prev = seq 1373end, 1374}) 1375]] ) 1376command ( 'nnoremap <F8> u' ) 1377--- Puts the cursor at `pre`, edits, then undoes via `undo`. Reports the 1378--- column before the edit and after the undo: they must match. 1379local function undone ( pre , edit , undo ) 1380api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'this is a test' } ) 1381n . poke_eventloop () 1382feed ( 'gg0' .. pre ) 1383n . poke_eventloop () 1384local before =fn . col ( '.' ) 1385feed ( edit ) 1386n . poke_eventloop () 1387-- Drained between steps: batched atoms would deliver the undo's restore 1388-- only after the redo already ran. 1389for _ , step in ipairs ( type ( undo ) == 'table' and undo or { undo } ) do 1390if step : sub ( 1 , 1 ) == ':' then 1391command ( step : sub ( 2 )) 1392else 1393feed ( step ) 1394end 1395n . poke_eventloop () 1396end 1397return { before , fn . col ( '.' ) } 1398end 1399-- "a"/"o" enter Insert AFTER moving the cursor, so the session's `pos` must 1400-- come from the command frame, not from where the entry command landed. 1401-- The last five need `undoseq`: they are not recognizable from the keys. 1402eq ( 1403{ { 4 , 4 } , { 4 , 4 } , { 4 , 4 } , { 7 , 7 } , { 4 , 4 } , { 4 , 4 } , { 4 , 4 } , { 4 , 4 } , { 4 , 4 } } , 1404{ 1405undone ( 'lll' , 'diw' , 'u' ), 1406undone ( 'lll' , 'aXY<Esc>' , 'u' ), 1407undone ( 'lll' , 'oXY<Esc>' , 'u' ), 1408undone ( 'llllll' , 'd^' , 'u' ), 1409undone ( 'lll' , 'diw' , '<F8>' ), -- mapping to undo 1410undone ( 'lll' , 'diwwdawdaw' , '3u' ), -- count 1411undone ( 'lll' , 'diwwdaw' , ':undo 1' ), 1412undone ( 'lll' , 'diw' , 'g-' ), 1413undone ( 'lll' , 'diw' , { 'u' , '0' , '<C-r>' } ), -- redo, after moving away 1414} 1415) 1416end ) 1417end ) 1418 1419it ( 'motions, search, excmd emit without an edit' , function () 1420-- Emission is not tied to editing: every user action emits, so 1421-- plugins can observe all activity. 1422fn . setline ( 1 , { 'alpha beta' , 'gamma delta' } ) 1423feed ( 'gg0' ) 1424atoms_start () 1425feed ( 'w' ) 1426feed ( '3l' ) 1427feed ( '/gamma<CR>' ) 1428feed ( ':nohlsearch<CR>' ) 1429eq ( { 1430{ type ='motion' , keys ='w' } , 1431{ type ='motion' , keys ='3l' } , 1432{ type ='motion' , keys =k ( '/gamma<NL>' ) } , 1433{ type ='excmd' , keys =k ( ':nohlsearch<NL>' ) } , 1434} , atoms_tail ( 4 , 'type' , 'keys' )) 1435 1436-- Cursor-relative marks ('< '. '{ …) are classified as type=motion. Other marks are type=jump. 1437feed ( 'ma' ) 1438feed ( '`.' ) 1439feed ( "'[" ) 1440feed ( '`{' ) 1441feed ( 'g`.' ) 1442feed ( '`a' ) 1443feed ( "g'a" ) 1444feed ( 'G' ) 1445eq ( { 1446{ type ='motion' , keys ='`.' } , 1447{ type ='motion' , keys ="'[" } , 1448{ type ='motion' , keys ='`{' } , 1449{ type ='motion' , keys ='g`.' } , 1450{ type ='jump' , keys ='`a' } , 1451{ type ='jump' , keys ="g'a" } , 1452{ type ='jump' , keys ='G' } , 1453} , atoms_tail ( 7 , 'type' , 'keys' )) 1454 1455-- ":" embeds its count as the range prefill, never as composed digits; 1456-- the count field carries it. 1457feed ( 'gg' ) 1458feed ( '2:<CR>' ) 1459eq ( 2 , fn . line ( '.' )) 1460eq ( 1461{ type ='excmd' , keys =k ( ':.,.+1<NL>' ), count =2 , cmd =':' } , 1462pick ( atom_last (), 'type' , 'keys' , 'count' , 'cmd' ) 1463) 1464eq ( { 'alpha beta' , 'gamma delta' } , get_lines ()) -- nothing was edited 1465-- Scrolls and mouse presses also emit (type "scroll"/"mouse"): emit-only, 1466-- never cascaded. 1467feed ( '<C-e>' ) 1468feed ( '3<C-y>' ) 1469api . nvim_input_mouse ( 'wheel' , 'up' , '' , 0 , 0 , 0 ) 1470api . nvim_input_mouse ( 'left' , 'press' , '' , 0 , 1 , 2 ) 1471eq ( { 1472{ type ='scroll' , keys =k ( '<C-E>' ) } , 1473{ type ='scroll' , keys =k ( '3<C-Y>' ) } , 1474{ type ='scroll' , keys =k ( '<ScrollWheelUp>' ) } , 1475{ type ='mouse' , keys =k ( '<LeftMouse>' ) } , 1476} , atoms_tail ( 4 , 'type' , 'keys' )) 1477eq ( 2 , fn . line ( '.' )) -- the click moved the cursor 1478-- <amatch>/match is the atom type, never path-expanded. 1479n . exec_lua ( [[ 1480vim.api.nvim_create_autocmd('CmdAtom', { 1481callback = function(ev) 1482_G.last_match = ev.match 1483end, 1484}) 1485]] ) 1486feed ( '0w' ) 1487n . poke_eventloop () 1488eq ( 'motion' , n . exec_lua ( 'return _G.last_match' )) 1489end ) 1490 1491it ( 'captures counts, payload' , function () 1492fn . setline ( 1 , { 'abcd,ef' } ) 1493feed ( 'gg0' ) 1494atoms_start () 1495feed ( 'yw' ) 1496 1497-- A yank emits but does not edit. 1498eq ( { operator ='y' , changed =false } , pick ( atom_last (), 'operator' , 'changed' )) 1499feed ( '3x' ) 1500eq ( { 'd,ef' } , get_lines ()) 1501feed ( 'vf,d' ) 1502eq ( { 'ef' } , get_lines ()) 1503eq ( { '3dl' , 'vf,d' } , atoms_tail ( 2 )) 1504 1505-- Structured decomposition: operator/motion/operand as fields, no byte-parsing. 1506local op =atoms ()[ # atoms () - 1 ] -- "3dl" 1507eq ( 1508{ operator ='d' , cmd ='l' , count =3 , changed =true } , 1509pick ( op , 'operator' , 'cmd' , 'cmdarg' , 'count' , 'motionforce' , 'changed' ) 1510) 1511 1512-- A visual atom carries the completing operator's fields, and decomposes 1513-- into its commands ("v", "f," and the operator). 1514local vis =atom_last () -- "vf,d" 1515eq ( { type ='visual' , operator ='d' } , pick ( vis , 'type' , 'operator' )) 1516eq ( { 1517{ keys ='v' , cmd ='v' , changed =false } , 1518{ keys ='f,' , cmd ='f' , cmdarg =',' , changed =false } , 1519{ keys ='d' , changed =true } , -- the completing operator did the edit 1520} , subatoms ( vis , 'keys' , 'cmd' , 'cmdarg' , 'changed' )) 1521eq ( 'd' , vis . atoms [ 3 ]. operator ) 1522 1523-- Operators with an interactively-typed search payload: the atom is the 1524-- redobuff, which includes the payload. 1525fn . setline ( 1 , { 'aa END bb' } ) 1526feed ( 'gg0' ) 1527feed ( 'd/END<CR>' ) 1528eq ( { 'END bb' } , get_lines ()) 1529eq ( { k ( 'd/END<NL>' ) } , atoms_tail ( 1 )) 1530eq ( 1531{ operator ='d' , cmd ='/' , changed =true } , 1532pick ( atom_last (), 'operator' , 'cmd' , 'changed' ) 1533) 1534 1535-- An operand (mark or register name, target char) is its own field; the second char of a 1536-- two-char command NAME ("gJ") composes into `cmd`. 1537fn . setline ( 1 , { 'one' , 'two' } ) 1538feed ( 'gg0magJ' ) 1539eq ( { 'onetwo' } , get_lines ()) -- "gJ": join without inserting a space 1540eq ( { cmd ='m' , cmdarg ='a' } , pick ( atoms ()[ # atoms () - 1 ], 'cmd' , 'cmdarg' )) 1541eq ( { cmd ='gJ' } , pick ( atom_last (), 'cmd' , 'cmdarg' )) 1542local nrec =# atoms () 1543feed ( 'qax' ) -- the recording register is an operand, not part of the name 1544feed ( 'q' ) 1545eq ( { cmd ='q' , cmdarg ='a' } , pick ( atoms ()[ nrec + 1 ], 'cmd' , 'cmdarg' )) 1546 1547-- Forced motion type ("dvj") is a field. 1548fn . setline ( 1 , { 'one' , 'two' } ) 1549feed ( 'gg0dvj' ) 1550eq ( 1551{ operator ='d' , motionforce ='v' , cmd ='j' } , 1552pick ( atom_last (), 'operator' , 'motionforce' , 'cmd' ) 1553) 1554end ) 1555 1556it ( '"!" operator captures its stuffed cmdline' , function () 1557-- ":.,.+1!" + typed "{prg}<CR>" completes the operator atom, like "d/END<CR>". #41447 1558local prg =n . testprg ( 'shell-test' ) .. ' REP 2 X' 1559fn . setline ( 1 , { 'b' , 'a' , '' , 'e' , 'c' , 'd' } ) 1560atoms_start () 1561feed ( 'gg' ) 1562feed (( '!ip%s<CR>' ): format ( prg )) 1563eq ( { '0: X' , '1: X' , '' , 'e' , 'c' , 'd' } , get_lines ()) 1564local bang =atom_last () 1565local keys =( '!ip%s\n' ): format ( prg ) 1566eq ( 1567{ type ='operator' , operator ='!' , lhs =keys , keys =keys } , 1568pick ( bang , 'type' , 'operator' , 'lhs' , 'keys' ) 1569) 1570-- Replay recomputes the range from the motion: the whole 3-line paragraph is replaced. 1571feed ( '4G' ) 1572n . exec_lua ( function ( keys ) 1573vim . api . nvim_feedkeys ( keys , 'nx' , false ) 1574end , bang . keys ) 1575eq ( { '0: X' , '1: X' , '' , '0: X' , '1: X' } , get_lines ()) 1576-- Same for "=" with 'equalprg', "gq" with 'formatprg'. 1577api . nvim_set_option_value ( 'equalprg' , prg , {} ) 1578api . nvim_set_option_value ( 'formatprg' , prg , {} ) 1579api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'b' , 'a' , '' , 'd' , 'c' } ) 1580feed ( 'gg=ip' ) 1581eq ( { '0: X' , '1: X' , '' , 'd' , 'c' } , get_lines ()) 1582eq ( 1583{ type ='operator' , operator ='=' , keys ='=ip' } , 1584pick ( atom_last (), 'type' , 'operator' , 'keys' ) 1585) 1586feed ( '4Ggqip' ) 1587eq ( { '0: X' , '1: X' , '' , '0: X' , '1: X' } , get_lines ()) 1588eq ( 1589{ type ='operator' , operator ='gq' , keys ='gqip' } , 1590pick ( atom_last (), 'type' , 'operator' , 'keys' ) 1591) 1592end ) 1593 1594it ( 'stuffed translations execute within their command (exec_stuffed)' , function () 1595-- A register prefix crosses into the "." replay; an explicit register must not touch the 1596-- small-delete register. 1597atoms_start () 1598fn . setline ( 1 , { 'aaa bbb' , 'ccc ddd' } ) 1599feed ( 'gg0dw' ) 1600eq ( 'aaa ' , fn . getreg ( '-' )) 1601feed ( 'j0"z.' ) 1602eq ( 'ccc ' , fn . getreg ( 'z' )) 1603eq ( 'aaa ' , fn . getreg ( '-' )) 1604eq ( { 'bbb' , 'ddd' } , get_lines ()) 1605-- One atom: the stuffed replay collects into the "."-labeled composite. 1606eq ( 1607{ type ='operator' , lhs =k ( '"z.' ), keys ='"zdw' , reg ='z' } , 1608pick ( atom_last (), 'type' , 'lhs' , 'keys' , 'reg' ) 1609) 1610-- i_CTRL-O inside a stuffed translation's insert session ("S" == "cc"): the session resumes 1611-- after ONE normal command. 1612api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'xxxx' , 'yyyy' } ) 1613feed ( 'ggSabc<C-o>0def<Esc>' ) 1614eq ( { 'defabc' , 'yyyy' } , get_lines ()) 1615-- i_CTRL-O resolution is lossy: keys=nil, replay the "S"-labeled `lhs` instead. 1616eq ( 1617{ type ='mapping' , lhs =k ( 'Sabc<C-o>0def<Esc>' ) } , 1618pick ( atom_last (), 'type' , 'lhs' , 'keys' ) 1619) 1620-- r<Tab> under 'expandtab'/'smarttab' ("{count}R<Tab><Esc>": edit() does the tab). 1621command ( 'set expandtab shiftwidth=4 tabstop=4' ) 1622api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abcdefgh' , 'ABCDEFGH' } ) 1623feed ( 'gg03r<Tab>' ) 1624eq ( ' defgh' , fn . getline ( 1 )) 1625eq ( 1626{ type ='insert' , lhs =k ( '3r<Tab>' ), keys =k ( '3R<Tab><Esc>' ) } , 1627pick ( atom_last (), 'type' , 'lhs' , 'keys' ) 1628) 1629feed ( 'j0.' ) 1630eq ( ' DEFGH' , fn . getline ( 2 )) 1631api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abcdefgh' } ) 1632feed ( 'gg0r<C-v><Tab>' ) 1633eq ( '\tbcdefgh' , fn . getline ( 1 )) 1634command ( 'set noexpandtab smarttab shiftwidth=4 tabstop=8' ) 1635api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'abcdefgh' } ) 1636feed ( 'gg02r<Tab>' ) 1637eq ( '\tcdefgh' , fn . getline ( 1 )) 1638command ( 'set expandtab& smarttab& shiftwidth& tabstop&' ) 1639-- "&" and "count&" (":.,.+Ns"). 1640api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'blue a' , 'blue b' , 'blue c' , 'blue d' } ) 1641command ( '1s/blue/red/' ) 1642feed ( '2G&3G2&' ) 1643eq ( { 'red a' , 'red b' , 'red c' , 'red d' } , get_lines ()) 1644eq ( { type ='excmd' , lhs ='2&' , keys =':.,.+1s\n' } , pick ( atom_last (), 'type' , 'lhs' , 'keys' )) 1645 1646-- Reg "." put re-inserts through edit(), and a mapping's composite labels it. 1647api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' } ) 1648feed ( 'ggifoo<Esc>' ) 1649feed ( '$".p' ) 1650eq ( 'fooonefoo' , fn . getline ( 1 )) 1651eq ( 1652{ type ='insert' , lhs =k ( '1afoo<Esc>' ), keys =k ( '1afoo<Esc>' ) } , 1653pick ( atom_last (), 'type' , 'lhs' , 'keys' ) 1654) 1655command ( 'nnoremap ,p ".p' ) 1656feed ( ',p' ) 1657eq ( 1658{ type ='insert' , lhs =',p' , keys =k ( '1afoo<Esc>' ) } , 1659pick ( atom_last (), 'type' , 'lhs' , 'keys' ) 1660) 1661-- Typed ":put ." emits typed cmdline payload, not the internal ":put _" translation. 1662feed ( ':put .<CR>' ) 1663eq ( 'foo' , fn . getline ( 2 )) 1664eq ( 1665{ type ='excmd' , lhs =':put .\n' , keys =':put .\n' , text ='put .' } , 1666pick ( atom_last (), 'type' , 'lhs' , 'keys' , 'text' ) 1667) 1668end ) 1669 1670it ( 'fires for user input, not programmatic sources' , function () 1671atoms_start () 1672-- Drain deferred CmdAtom events, then return + clear the collected list. 1673local function take () 1674n . poke_eventloop () 1675return n . exec_lua ( [[local a = _G.atoms; _G.atoms = {}; return a]] ) 1676end 1677-- Fresh buffer + cursor via the API (no keys, so no motion atoms). 1678local function fresh () 1679api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaaaaaaa' , 'bbbbbbbb' } ) 1680api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1681take () 1682end 1683 1684-- Typed input emits: real keys (nvim_input, via feed())... 1685fresh () 1686feed ( 'x' ) 1687eq ( 1 , # take ()) 1688-- ...and nvim_feedkeys() with the "t" (typed) flag. 1689n . exec_lua ( [[vim.api.nvim_feedkeys('x', 't', false)]] ) 1690eq ( 1 , # take ()) 1691 1692-- A typed macro folds into exactly ONE atom, labeled "@q". 1693fresh () 1694feed ( 'qqxq' ) -- recording is typed input 1695take () 1696feed ( '@q' ) 1697local evs =take () 1698eq ( 1 , # evs ) 1699eq ( '@q' , evs [ 1 ]. lhs ) 1700 1701-- Programmatic / replayed input must NOT leak any atom. 1702fresh () 1703api . nvim_buf_set_lines ( 0 , 0 , 1 , true , { 'ZZZZ' } ) -- API buffer edit 1704eq ( 0 , # take ()) 1705api . nvim_buf_set_text ( 0 , 0 , 0 , 0 , 1 , { 'Q' } ) 1706eq ( 0 , # take ()) 1707command ( 'normal! x' ) -- :normal! 1708eq ( 0 , # take ()) 1709command ( 'normal x' ) -- :normal (with mappings) 1710eq ( 0 , # take ()) 1711n . exec_lua ( [[vim.cmd('normal! x')]] ) 1712eq ( 0 , # take ()) 1713n . exec_lua ( [[vim.api.nvim_feedkeys('x', '', false)]] ) -- feedkeys without "t" 1714eq ( 0 , # take ()) 1715command ( 'nnoremap gj i<c-j><esc>k$' ) 1716n . exec_lua ( [[vim.api.nvim_feedkeys('gj', 'm', false)]] ) -- remapped feedkeys without "t" 1717eq ( {} , take ()) 1718n . exec_lua ( [[vim.api.nvim_feedkeys('ihello\27', '', false)]] ) -- insert session without "t" 1719eq ( {} , take ()) 1720command ( 'normal! @q' ) -- macro played programmatically, not typed 1721eq ( 0 , # take ()) 1722n . exec_lua ( [[vim.api.nvim_feedkeys('@q', '', false)]] ) 1723eq ( 0 , # take ()) 1724command ( 'normal! yy' ) -- prep-exempt operator (a yank builds no redo) 1725eq ( 0 , # take ()) 1726n . exec_lua ( [[vim.api.nvim_feedkeys('viwd', '', false)]] ) -- Visual sequence 1727eq ( 0 , # take ()) 1728 1729-- A timer/scheduled API edit must NOT leak. 1730n . exec_lua ( [[ 1731_G.done = false 1732vim.defer_fn(function() 1733vim.api.nvim_buf_set_lines(0, 0, 1, true, { 'TTTT' }) 1734_G.done = true 1735end, 5) 1736]] ) 1737n . exec_lua ( 'vim.wait(200, function() return _G.done end)' ) 1738eq ( 0 , # take ()) 1739 1740-- Sanity: typed input still emits after all the programmatic noise. 1741fresh () 1742feed ( 'x' ) 1743eq ( 1 , # take ()) 1744end ) 1745 1746it ( 'one event per user action' , function () 1747n . clear ( { args ={ '--clean' } , args_rm ={ '--cmd' } } ) 1748--- Feeds `keys`, asserts exactly ONE new event, with the given keys. 1749local function atom ( keys , expected , lhs , type_ ) 1750local before =# atoms () 1751feed ( keys ) 1752local evs =atoms () 1753eq ( 1754{ before + 1 , k ( expected ), k ( lhs or expected ), type_ or evs [ # evs ]. type } , 1755{ # evs , evs [ # evs ]. keys , evs [ # evs ]. lhs , evs [ # evs ]. type } 1756) 1757end 1758fn . setline ( 1 , fn [ 'repeat' ]( { 'alpha beta gamma delta epsilon zeta' } , 20 )) 1759feed ( 'gg0' ) 1760atoms_start () 1761-- "." with nothing to repeat stuffs nothing. 1762feed ( '.' ) 1763eq ( { { type ='normal' , keys ='.' , lhs ='.' } } , atoms_tail ( 1 , 'type' , 'keys' , 'lhs' )) 1764-- Operators: the atom is the redobuff (count/register included). 1765-- "x" is normalized ("translated") to the elemental command "dl". 1766atom ( 'x' , 'dl' , 'x' ) 1767-- A stuffed translation UNWRAPS: keeps the resolved "type" and has no subatoms. 1768-- Only `lhs` marks it as translated; it is not a composite. 1769eq ( { type ='operator' } , pick ( atom_last (), 'type' , 'atoms' )) 1770atom ( '3x' , '3dl' , '3x' ) 1771atom ( 'D' , 'd$' , 'D' ) 1772atom ( 'dw' , 'dw' ) 1773atom ( '"z2dw' , '"z2dw' ) 1774atom ( 'yy' , 'yy' ) 1775atom ( 'p' , 'p' ) 1776atom ( 'J' , '2J' ) 1777atom ( '3J' , '3J' ) 1778atom ( 'r?' , '1r?' ) 1779atom ( '~' , '~' ) 1780atom ( 'guiw' , 'guiw' ) 1781atom ( 'gUiw' , 'gUiw' ) 1782atom ( '>>' , '>>' ) 1783atom ( 'dfa' , 'dfa' ) 1784feed ( 'ddk' ) 1785atom ( 'P' , 'P' ) 1786-- Insert sessions: one whole-session atom (entry + text + <Esc>). 1787atom ( 'iXY<Esc>' , '1iXY<Esc>' ) 1788atom ( 'A!<Esc>' , '1A!<Esc>' ) 1789atom ( 'oNEW<Esc>' , '1oNEW<Esc>' ) 1790atom ( '3iZ<Esc>' , '3iZ<Esc>' ) 1791atom ( 'cwWORD<Esc>' , 'cwWORD<Esc>' ) 1792-- Visual: the full typed keysequence. 1793atom ( 'viwd' , 'viwd' ) 1794atom ( 'Vd' , 'Vd' ) 1795atom ( '<C-v>jd' , '<C-V>jd' ) 1796-- Motions: the target is relative to the cursor. 1797atom ( 'w' , 'w' ) 1798atom ( '3w' , '3w' ) 1799atom ( 'fb' , 'fb' ) 1800atom ( '$' , '$' ) 1801feed ( 'gg0' ) 1802atom ( ']]' , ']]' ) 1803-- "%" is cursor-relative. 1804command ( 'silent! nunmap %' ) -- the bundled matchit plugin maps it 1805fn . setline ( 1 , 'alpha (beta) gamma' ) 1806feed ( 'gg0f(' ) 1807atom ( '%' , '%' , nil , 'motion' ) 1808fn . setline ( 1 , 'alpha beta gamma delta epsilon zeta' ) 1809-- "*" is cursor-relative, reads the word per-cursor. 1810feed ( 'gg0' ) 1811atom ( '*' , '*' , nil , 'motion' ) 1812atom ( 'g#' , 'g#' , nil , 'motion' ) 1813-- Jumps: absolute, the target is independent of the cursor. 1814feed ( 'gg0' ) 1815atom ( 'G' , 'G' , nil , 'jump' ) 1816atom ( 'gg' , 'gg' , nil , 'jump' ) 1817atom ( 'go' , 'go' , nil , 'jump' ) 1818atom ( '50%' , '50%' , nil , 'jump' ) -- "[count]%" is absolute, unlike "%". 1819atom ( 'L' , 'L' , nil , 'jump' ) 1820atom ( 'H' , 'H' , nil , 'jump' ) 1821atom ( 'M' , 'M' , nil , 'jump' ) 1822atom ( 'ma' , 'ma' , nil , 'normal' ) -- "m" sets state; it does not jump 1823atom ( '`a' , '`a' , nil , 'jump' ) 1824atom ( '<C-o>' , '<C-O>' , nil , 'jump' ) 1825-- Non-redoable commands: still emitted, as type "command". 1826atom ( 'zz' , 'zz' , nil , 'normal' ) 1827atom ( 'u' , 'u' ) 1828atom ( '<C-r>' , '<C-R>' ) 1829-- "." emits its resolution (like "x" => "dl"). 1830feed ( 'gg0' ) 1831atom ( 'x' , 'dl' , 'x' ) 1832atom ( '.' , 'dl' , '.' ) 1833atom ( '3.' , '3dl' , '3.' ) -- "3.": the new count replaces the captured one 1834-- "." emits exactly ONE atom labeled ".": the repeated keys fold into its composite. 1835atom ( 'iQ<Esc>' , '1iQ<Esc>' ) 1836atom ( '.' , '1iQ<Esc>' , '.' ) 1837atom ( 'viwd' , 'viwd' ) 1838atom ( '.' , 'viwd' , '.' ) 1839atom ( 'cwZZ<Esc>' , 'cwZZ<Esc>' ) 1840atom ( '.' , 'cwZZ<Esc>' , '.' ) 1841-- Payload commands: the interactively-typed cmdline completes the 1842-- keysequence (not a bare "/" or ":" prefix). 1843atom ( '/beta<CR>' , '/beta<NL>' ) 1844eq ( { type ='motion' , text ='beta' } , pick ( atom_last (), 'type' , 'text' )) 1845atom ( '?alpha<CR>' , '?alpha<NL>' ) 1846atom ( '2/beta<CR>' , '2/beta<NL>' ) 1847-- Ex commands: their own atom kind, the cmdline is the "text" payload. 1848atom ( ':set tw=42<CR>' , ':set<Space>tw=42<NL>' ) 1849eq ( { type ='excmd' , text ='set tw=42' } , pick ( atom_last (), 'type' , 'text' )) 1850-- A nested cmdline opened by the command's own execution (":normal") 1851-- does not hijack the payload. 1852atom ( ':exe "normal! :echo 1\\r"<CR>' , ':exe<Space>"normal!<Space>:echo<Space>1<Bslash>r"<NL>' ) 1853-- A mapping that ends mid-operation captures its continuation. ",D" emits nothing until the 1854-- typed "w" finishes the operation: one atom captures both. 1855command ( 'nnoremap ,D d' ) 1856command ( 'nnoremap ,V v' ) 1857before =# atoms () 1858feed ( ',D' ) 1859eq ( before , # atoms ()) 1860atom ( 'w' , 'dw' , ',Dw' ) -- The supplied motion finishes the operation 1861-- Same for a mapping that opens Visual mode: ESC closes the selection (cursor stays on the 1862-- selection end, where it moved during the Visual sequence). 1863feed ( ',V' ) 1864eq ( before + 1 , # atoms ()) 1865feed ( '<Esc>' ) 1866eq ( 1867{ type ='visual' , lhs =k ( ',V<Esc>' ), keys =k ( 'v<Esc>' ) } , 1868pick ( atom_last (), 'type' , 'lhs' , 'keys' ) 1869) 1870-- A mapping whose trailing prefix is completed by TYPED keys ("," + "w") 1871-- ends where its own keys stop: each `lhs` owns only what it produced. 1872command ( 'set notimeout' ) 1873command ( 'nmap ,x j,' ) 1874command ( 'nnoremap ,w w' ) 1875feed ( 'gg0' ) 1876before =# atoms () 1877feed ( ',xw' ) -- no poke between: nvim awaits the pending "," under 'notimeout' 1878eq ( before + 2 , # atoms ()) 1879eq ( { 1880{ type ='motion' , lhs =',x' , keys ='j' } , 1881{ type ='motion' , lhs =',w' , keys ='w' } , 1882} , atoms_tail ( 2 , 'type' , 'lhs' , 'keys' )) 1883command ( 'set notimeout&' ) 1884command ( 'nunmap ,x' ) 1885command ( 'nunmap ,w' ) 1886-- An ABORTED mapping emits nothing: an error discards its remaining 1887-- keys, and the composite with them. 1888command ( 'nnoremap ,E :NoSuchCmd<CR>x' ) 1889local count =# atoms () 1890feed ( ',E' ) -- E492 mid-mapping: the trailing "x" never runs 1891eq ( count , # atoms ()) 1892atom ( 'x' , 'dl' , 'x' ) -- the next command is not folded into the dead composite 1893command ( 'nmap ,A ,B' ) 1894command ( 'nmap ,B ,A' ) 1895feed ( ',A' ) -- E223: recursive mapping 1896atom ( 'x' , 'dl' , 'x' ) 1897-- Macro playback ("@q") emits its commands' atoms (capture-on-replay); 1898-- "@q" itself is a translation, never an atom. 1899local total =# atoms () 1900feed ( 'qqxq' ) 1901feed ( '@q' ) 1902eq ( total + 4 , # atoms ()) 1903eq ( { 'qq' , 'dl' , 'q' , 'dl' } , atoms_tail ( 4 )) 1904-- The macro's atoms fold into one "@x"-labeled composite, like a mapping. 1905eq ( '@q' , atom_last (). lhs ) 1906-- A typed scroll emits its own (emit-only) atom kind. 1907feed ( '<C-d>' ) 1908eq ( total + 5 , # atoms ()) 1909eq ( { type ='scroll' , keys =k ( '<C-D>' ) } , pick ( atom_last (), 'type' , 'keys' )) 1910-- An 'indentexpr' that runs ":normal" opens a nested command-frame mid-operator: "gq" still 1911-- pushes its own atom, after the edit. 1912exec ( [[ 1913func Indent() 1914exe "normal! \<Ignore>" 1915return 0 1916endfunc 1917setlocal indentexpr=Indent() textwidth=20 1918]] ) 1919atom ( 'Vgq' , 'Vgq' ) 1920eq ( true , atom_last (). changed ) 1921end ) 1922 1923it ( 'captures prep-exempt operators (zfap) and fold/view commands' , function () 1924fn . setline ( 1 , { 'aa' , 'aa' , '' } ) 1925feed ( 'gg0' ) 1926atoms_start () 1927feed ( 'zfap' ) 1928eq ( { 'zfap' } , atoms_tail ( 1 )) 1929eq ( 'operator' , atom_last (). type ) 1930eq ( 1 , fn . foldclosed ( 1 )) 1931feed ( 'za' ) 1932eq ( { 'za' } , atoms_tail ( 1 )) 1933-- Neither an operator nor a motion: its own kind. 1934eq ( 'normal' , atom_last (). type ) 1935eq ( - 1 , fn . foldclosed ( 1 )) 1936end ) 1937 1938it ( 'captures "do"/"dp" (:diffget/:diffput); "." repeats them (unlike Vim)' , function () 1939atoms_start () 1940command ( 'new' ) 1941fn . setline ( 1 , { 'a' , 'b' , 'c' , 'd' } ) 1942command ( 'diffthis | vnew' ) 1943fn . setline ( 1 , { 'a' , 'XY' , 'c' , 'ZW' } ) 1944command ( 'diffthis' ) 1945feed ( '2G' ) 1946local before =# atoms () 1947feed ( 'do' ) 1948eq ( before + 1 , # atoms ()) 1949eq ( { { type ='operator' , keys ='do' } } , atoms_tail ( 1 , 'type' , 'keys' )) 1950eq ( 'b' , fn . getline ( 2 )) 1951feed ( '2j.' ) -- Repeats "do" at the next hunk. 1952eq ( 'd' , fn . getline ( 4 )) 1953feed ( '2Gx' ) 1954feed ( 'dp' ) 1955eq ( { { type ='operator' , keys ='dp' } } , atoms_tail ( 1 , 'type' , 'keys' )) 1956eq ( { '' } , fn . getbufline ( fn . bufnr ( '#' ), 2 )) 1957end ) 1958 1959it ( "operatorfunc atom includes the getchar()'d payload" , function () 1960n . exec ( t_atom . minisurround_vim ) 1961fn . setline ( 1 , { 'alpha beta' } ) 1962feed ( 'gg0' ) 1963atoms_start () 1964feed ( 'ysiw"' ) 1965-- One atom: the mapping captures the operation finished by "iw". `keys` is the redobuff plus 1966-- the getchar() payload: a replayed opfunc reads the same wrap char. 1967eq ( 1968{ type ='operator' , keys ='g@iw"' , lhs ='ysiw"' } , 1969pick ( atom_last (), 'type' , 'keys' , 'lhs' ) 1970) 1971eq ( { '"alpha" beta' } , get_lines ()) 1972end ) 1973 1974it ( 'operator completed by a Lua textobject (:omap) #41482' , function () 1975-- Lua :omap textobject (starts Visual mode, |omap-info|) captures the operator: `keys` is the 1976-- redobuff (K_LUA + mapping-id). 1977n . exec_lua ( [[ 1978vim.keymap.set('o', 'gt', function() 1979vim.cmd('normal! viw') 1980end) 1981]] ) 1982fn . setline ( 1 , { 'aaa xxx' , 'bbb yyy' , 'ccc zzz' } ) 1983atoms_start () 1984feed ( 'gg0wdgt' ) 1985eq ( 'aaa ' , fn . getline ( 1 )) 1986local ev =atom_last () 1987eq ( 1988{ type ='operator' , operator ='d' , lhs ='dgt' , changed =true } , 1989pick ( ev , 'type' , 'operator' , 'lhs' , 'cmd' , 'changed' ) 1990) 1991t . matches ( '^d\128' , ev . keys ) -- "d" + K_LUA…: the redobuf. 1992feed ( 'j0w' ) 1993n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 1994eq ( 'bbb ' , fn . getline ( 2 )) 1995-- "." repeats it, captured as one "."-labeled operator atom. 1996feed ( 'j0w.' ) 1997eq ( 'ccc ' , fn . getline ( 3 )) 1998eq ( 1999{ type ='operator' , operator ='d' , lhs ='.' } , 2000pick ( atom_last (), 'type' , 'operator' , 'lhs' ) 2001) 2002 2003-- 'operatorfunc' + Lua textobject: "ghgt". Non-edit, still emits its operator atom. 2004n . exec_lua ( [[ 2005_G.oplog = {} 2006vim.keymap.set('n', 'gh', function() 2007vim.o.operatorfunc = function(mtype) 2008table.insert(_G.oplog, mtype) 2009end 2010return 'g@' 2011end, { expr = true }) 2012]] ) 2013feed ( 'gg0ghgt' ) 2014eq ( { 'char' } , n . exec_lua ( 'return _G.oplog' )) 2015ev =atom_last () 2016eq ( 2017{ type ='operator' , operator ='g@' , lhs ='ghgt' , changed =false } , 2018pick ( ev , 'type' , 'operator' , 'lhs' , 'cmd' , 'changed' ) 2019) 2020t . matches ( '^g@\128' , ev . keys ) -- "g@" + K_LUA…. 2021n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 2022eq ( { 'char' , 'char' } , n . exec_lua ( 'return _G.oplog' )) 2023 2024-- Lua 'operatorfunc' editing via ":norm!" must not clobber the captured "g@" redo/atom. 2025n . exec_lua ( [[ 2026vim.keymap.set('n', 'gr', function() 2027vim.o.operatorfunc = function() 2028vim.cmd([=[normal! `[v`]rX]=]) 2029end 2030return 'g@' 2031end, { expr = true }) 2032]] ) 2033api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'mmm nnn' , 'ooo ppp' } ) 2034feed ( 'gg0grgt' ) 2035eq ( 'XXX nnn' , fn . getline ( 1 )) 2036ev =atom_last () 2037eq ( 2038{ type ='operator' , operator ='g@' , lhs ='grgt' , changed =true } , 2039pick ( ev , 'type' , 'operator' , 'lhs' , 'changed' ) 2040) 2041t . matches ( '^g@\128' , ev . keys ) -- "g@" + K_LUA…. 2042feed ( 'j0' ) 2043n . exec_lua (( [[vim.api.nvim_feedkeys(%q, 'nx', false)]] ): format ( ev . keys )) 2044eq ( 'XXX ppp' , fn . getline ( 2 )) 2045 2046-- Aborted operator (cpo+=E empty region) cancels its redo: no atom captured. 2047command ( 'set cpo+=E' ) 2048n . exec_lua ( [[vim.keymap.set('o', 'ge', function() end)]] ) 2049feed ( 'dge' ) 2050eq ( 'XXX ppp' , fn . getline ( 2 )) 2051eq ( 2052{ type ='mapping' , lhs ='dge' , changed =false } , 2053pick ( atom_last (), 'type' , 'lhs' , 'changed' ) 2054) 2055command ( 'set cpo-=E' ) 2056end ) 2057 2058it ( 'does not capture inputsecret() input' , function () 2059api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa' } ) 2060n . exec_lua ( function () 2061vim . keymap . set ( 'n' , 'sp' , function () 2062_G . pw =vim . fn . inputsecret ( 'Password: ' ) 2063vim . api . nvim_put ( { 'X' } , 'c' , false , true ) 2064end ) 2065end ) 2066feed ( 'gg0' ) 2067atoms_start () 2068feed ( 'sp' ) 2069feed ( 'hunter2<CR>' ) 2070feed ( '<Esc>' ) -- The composite resolves at the next clock edge. 2071eq ( 'hunter2' , n . exec_lua ( 'return _G.pw' )) -- The function gets the input. 2072eq ( { type ='mapping' , lhs ='sp' } , pick ( atoms ()[ 1 ], 'type' , 'lhs' , 'keys' )) -- Not CmdAtom. 2073end ) 2074 2075it ( 'reports the original buffer' , function () 2076n . exec_lua ( [[ 2077_G.evs = {} 2078vim.api.nvim_create_autocmd('CmdAtom', { 2079callback = function(ev) 2080table.insert(_G.evs, { buf = ev.buf, keys = ev.data.keys, changed = ev.data.changed }) 2081end, 2082}) 2083]] ) 2084command ( 'let g:abufs = []' ) 2085command ( "autocmd CmdAtom * call add(g:abufs, expand('<abuf>')->str2nr())" ) 2086local a =api . nvim_get_current_buf () 2087api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa bbb' } ) 2088command ( 'badd other' ) 2089local b =api . nvim_list_bufs ()[ 2 ] 2090command ( 'nnoremap ]b <Cmd>bprev<CR>' ) 2091 2092-- An edit reports its own buffer, and `changed`. 2093feed ( 'gg0dw' ) 2094-- Leaving the buffer reports the buffer the command ran IN, not the one it 2095-- landed in. `changed` is about that same buffer: ":bnext" edits nothing. 2096feed ( ':bnext<CR>' ) 2097eq ( b , api . nvim_get_current_buf ()) 2098-- Same, from a mapping, in the other direction. 2099feed ( ']b' ) 2100eq ( a , api . nvim_get_current_buf ()) 2101local evs =n . exec_lua ( 'return _G.evs' ) 2102eq ( { 2103{ buf =a , keys ='dw' , changed =true } , 2104{ buf =a , keys =':bnext\n' , changed =false } , 2105{ buf =b , keys =( '%sbprev\n' ): format ( k ( '<Cmd>' )), changed =false } , 2106} , { evs [ # evs - 2 ], evs [ # evs - 1 ], evs [ # evs ] } ) 2107-- <abuf> agrees with the Lua callback's `buf`. 2108local abufs =n . eval ( 'g:abufs' ) 2109eq ( { a , a , b } , { abufs [ # abufs - 2 ], abufs [ # abufs - 1 ], abufs [ # abufs ] } ) 2110end ) 2111 2112it ( 'reports `moved`/`pos` for the original buffer+window' , function () 2113api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa bbb' , 'ccc ddd' , 'eee fff' } ) 2114n . exec_lua ( [[ 2115_G.jump = function() vim.api.nvim_win_set_cursor(0, { 3, 0 }) end 2116vim.keymap.set('n', ']c', '<Cmd>lua _G.jump()<CR>') 2117]] ) 2118atoms_start () 2119 2120-- A plugin motion via "<Cmd>" is type="excmd" (its RHS implementation); `moved` 2121-- reports the same observed effect as the builtin motion below it. 2122feed ( 'gg0]c' ) 2123eq ( 3 , fn . line ( '.' )) 2124eq ( 2125{ type ='excmd' , moved =true , changed =false , pos ={ 1 , 0 } } , 2126pick ( atom_last (), 'type' , 'moved' , 'changed' , 'pos' ) 2127) 2128feed ( 'gg0w' ) 2129eq ( 2130{ type ='motion' , moved =true , changed =false } , 2131pick ( atom_last (), 'type' , 'moved' , 'changed' ) 2132) 2133-- A motion that did not move is still a motion; `moved` is the separate question. 2134eq ( 2135{ { 'motion' , false } , { 'motion' , false } , { 'motion' , false } , { 'normal' , false } } , 2136vim . tbl_map ( function ( keys ) 2137feed ( keys ) 2138local a =atom_last () 2139return { a . type , a . moved } 2140end , { 'G$w' , 'gg0fz' , 'gg0h' , 'gg0zz' } ) 2141) 2142 2143-- Register-only operator also moves without changing: a motion carries no `operator`. 2144feed ( 'gg0wyb' ) 2145eq ( 2146{ moved =true , changed =false , operator ='y' } , 2147pick ( atom_last (), 'moved' , 'changed' , 'operator' ) 2148) 2149 2150-- Switching window is not a cursor-move, even on the SAME buffer: `pos` is per-window. 2151command ( 'split' ) 2152feed ( 'gg' ) 2153command ( 'wincmd j' ) 2154feed ( '3G' ) 2155command ( 'wincmd k' ) 2156feed ( '<C-w>w' ) 2157eq ( 3 , fn . line ( '.' )) 2158eq ( { type ='normal' , moved =false } , pick ( atom_last (), 'type' , 'moved' )) 2159 2160-- Both are measured on the buffer/window the action STARTED in, so acting 2161-- and then navigating away still reports the effect. 2162command ( 'nnoremap ]x <Cmd>normal! dd<CR><Cmd>wincmd w<CR>' ) 2163command ( 'nnoremap ]y <Cmd>normal! j<CR><Cmd>wincmd w<CR>' ) 2164feed ( 'gg]x' ) 2165eq ( { changed =true , moved =false } , pick ( atom_last (), 'changed' , 'moved' )) 2166command ( 'wincmd w' ) 2167feed ( 'gg]y' ) 2168eq ( { changed =false , moved =true } , pick ( atom_last (), 'changed' , 'moved' )) 2169 2170-- Switching buffer is not a cursor-move. 2171command ( 'only' ) 2172local main =api . nvim_get_current_buf () 2173command ( 'edit other' ) 2174api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'x' , 'y' , 'z' } ) 2175feed ( '3G' ) 2176feed ( '<C-^>' ) -- back to `main`, whose cursor is on another line 2177eq ( main , api . nvim_get_current_buf ()) 2178eq ( 2179{ type ='normal' , moved =false , pos ={ 3 , 0 } } , 2180pick ( atom_last (), 'type' , 'moved' , 'pos' ) 2181) 2182end ) 2183end )