fix(defaults): only repeat macro for each selected line if linewise (#28289)

As mentioned in #28287, repeating a macro for each selected line doesn't
really make sense in non-linewise Visual mode.

Fix #28287
This commit is contained in:
zeertzjq 2024-04-15 03:43:33 +08:00 committed by GitHub
parent f6a3fdd684
commit aa1d0ac095
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 15 additions and 51 deletions

View File

@ -317,8 +317,8 @@ The following new APIs and features were added.
• |nvim_input_mouse()| supports mouse buttons "x1" and "x2". • |nvim_input_mouse()| supports mouse buttons "x1" and "x2".
• |v_Q-default| and |v_@-default| repeat a register for each line of a visual • |v_Q-default| and |v_@-default| repeat a register for each line of a linewise
selection. visual selection.
• |vim.diagnostic.count()| returns the number of diagnostics for a given • |vim.diagnostic.count()| returns the number of diagnostics for a given
buffer and/or namespace, by severity. This is a faster alternative to buffer and/or namespace, by severity. This is a faster alternative to

View File

@ -149,8 +149,8 @@ q Stops recording.
@@ Repeat the previous @{0-9a-z":*} [count] times. @@ Repeat the previous @{0-9a-z":*} [count] times.
*v_@-default* *v_@-default*
{Visual}@{0-9a-z".=*+} In Visual mode, execute the contents of the register {Visual}@{0-9a-z".=*+} In linewise Visual mode, execute the contents of the
{Visual}@@ but for each selected line. {Visual}@@ register for each selected line.
See |visual-repeat|, |default-mappings|. See |visual-repeat|, |default-mappings|.
*Q* *Q*
@ -158,8 +158,8 @@ Q Repeat the last recorded register [count] times.
See |reg_recorded()|. See |reg_recorded()|.
*v_Q-default* *v_Q-default*
{Visual}Q In Visual mode, repeat the last recorded register for {Visual}Q In linewise Visual mode, repeat the last recorded
each selected line. register for each selected line.
See |visual-repeat|, |default-mappings|. See |visual-repeat|, |default-mappings|.
*:@* *:@*

View File

@ -366,15 +366,15 @@ one of the last commands to extend the highlighted text, the repeating will
be applied up to the rightmost column of the longest line. Any count passed be applied up to the rightmost column of the longest line. Any count passed
to the `.` command is not used. to the `.` command is not used.
Visual mode |default-mappings| "@" and "Q" can repeat commands in a register Visual mode |default-mappings| "@" and "Q" repeat a register for all selected
for all selected lines. See |v_@-default| and |v_Q-default| for details. For lines if the selection is linewise. See |v_@-default| and |v_Q-default| for
example, given the text: details. For example, given the text:
123(hello)321 123(hello)321
456(world)654 456(world)654
456(NOT THIS)654 456(NOT THIS)654
With register "x" containing the commands `yi(VP`, Visually selecting the With register "x" containing the commands `yi(VP`, visually selecting the
first two lines and typing `@x` produces: first two lines and typing `@x` produces:
hello hello

View File

@ -78,19 +78,20 @@ do
--- See |&-default| --- See |&-default|
vim.keymap.set('n', '&', ':&&<CR>', { desc = ':help &-default' }) vim.keymap.set('n', '&', ':&&<CR>', { desc = ':help &-default' })
--- Use Q in visual mode to execute a macro on each line of the selection. #21422 --- Use Q in Visual mode to execute a macro on each line of the selection. #21422
--- This only make sense in linewise Visual mode. #28287
--- ---
--- Applies to @x and includes @@ too. --- Applies to @x and includes @@ too.
vim.keymap.set( vim.keymap.set(
'x', 'x',
'Q', 'Q',
':normal! @<C-R>=reg_recorded()<CR><CR>', "mode() == 'V' ? ':normal! @<C-R>=reg_recorded()<CR><CR>' : 'Q'",
{ silent = true, desc = ':help v_Q-default' } { silent = true, expr = true, desc = ':help v_Q-default' }
) )
vim.keymap.set( vim.keymap.set(
'x', 'x',
'@', '@',
"':normal! @'.getcharstr().'<CR>'", "mode() == 'V' ? ':normal! @'.getcharstr().'<CR>' : '@'",
{ silent = true, expr = true, desc = ':help v_@-default' } { silent = true, expr = true, desc = ':help v_@-default' }
) )

View File

@ -131,43 +131,6 @@ helloFOO123
helloFOO]] helloFOO]]
end) end)
-- XXX: does this really make sense?
it('can be replayed with @ in blockwise Visual mode', function()
insert [[
hello
hello
hello]]
feed [[gg]]
feed [[qqAFOO<esc>qu]]
expect [[
hello
hello
hello]]
feed [[qwA123<esc>qu]]
expect [[
hello
hello
hello]]
feed [[<C-v>3j@q]]
expect [[
helloFOO
helloFOO
helloFOO]]
feed [[gg<C-v>j@w]]
expect [[
helloFOO123
helloFOO123
helloFOO]]
end)
end)
describe('macros without default mappings', function()
before_each(clear)
it('can be recorded and replayed in Visual mode', function() it('can be recorded and replayed in Visual mode', function()
insert('foo BAR BAR foo BAR foo BAR BAR BAR foo BAR BAR') insert('foo BAR BAR foo BAR foo BAR BAR BAR foo BAR BAR')
feed('0vqifofRq') feed('0vqifofRq')