My automated tests run Vim in silent batch mode (:help -s-ex), these also cover repeat of mappings via repeat.vim. Unfortunately, the combination of feedkeys() and :execute used in the repeat.vim implementation to return a potential caught error cause the line where the repeat mapping is executed to be increased, so for me the tests fail in a really strange way. The same problem can occur if someone uses repeats in silent batch mode (e.g. through a recorded macro) - though the source of the problem would be even harder to track down there - in my test run it at least was easily reproducible.
I raised this as a potential Vim bug in https://github.com/vim/vim/issues/7153, but Bram explained that it's a side effect of Ex mode (where empty lines make the cursor go to the next line, and as repeat#run() returns the empty String on the happy path, that's an empty line being executed). Recommendation from Bram is to pass the "x" flag for immediate execution to feedkeys() (which avoids the problem). However, in the context of repeat.vim this would mean that any errors resulting from the repeat mapping invocation would have to be caught inside repeat#run(), because they would then execute within the function's context, and not after it. Also, the "x" flag would not be supported by old Vim 7.3 and earlier.
Instead, I chose to avoid the problem by replacing the :execute with a more straightforward :call (actually an :if that tests a returned Boolean success flag), so instead of the clever direct execution of the returned :echoerr command, the error message instead is retrieved from a script-local variable via a new repeat#errmsg() getter. I use this idiom in all of my plugins through a set of utility functions (5bb32fd27a/autoload/ingo/err.vim (L38-L41)), too.
So by adding a little (well encapsulated) variable and getter, this problem can be avoided without touching the sensitive feedkeys() and try...catch logic within the plugin.
As with built-in commands, this allows to override the original register on repeat, e.g. "a. uses register a instead of the original one.
One limitation is that we cannot detect whether no or the default register has been given, so an override from a non-default to the default register (e.g. via "".) is not possible.
Need to :execute the :silent! call to avoid that the remainder of the
command line is aborted together with the call when repeat.vim is not
installed. Otherwise, <SID>MyFunction() won't be invoked, and the
mapping does nothing.
The current implementation prefers feedkeys(..., 'i') and only falls back to :normal if Vim 7.4 does not have the 'i' flag and if Vim 7.3 supports :normal with count. For Vim 8.0 (and later), and also for versions between 7.0 and 7.3.99, feedkeys(..., 'i') is used. Due to the prepending action of the 'i' flag, keys must be submitted in reverse order; that's why s is submitted before r . cnt. Vim 7.0...7.3.99 ignore the 'i' flag, though, and append the keys, which are now in the wrong order (this only matters if a register or count is given).
Add a separate conditional branch for Vim versions before 7.3.100 that uses feedkeys() in the correct order and omits the ignored 'i' flag.
Since we are using the `i` flag, the feedkeys calls insert the keys
instead of appending. That's why we need to insert the second part of
the command before inserting the first part in front of it.
Now mappings can be remapped in `.vimrc` by
`nnoremap <key> <Plug>(RepeatUndoLine)`
and disablabled in `.vimrc` by
`nnoremap <SID>(DisableRepeatUndoLine) <Plug>(RepeatUndoLine)`
* Fix adding trailing spaces when 've' is not empty.
* Don't increment b:changedtick, offer invalidate instead.
* Support repetition with original register.
* Don't clobber existing U map.
* Respect 'foldopen' on undo.
* Allow customizing all mappings.
f1d0fbbdf4 allowed customizing the .
mapping by making the implementation function publicly accessible. Let's
do the same to s:wrap(), so that the other mappings (u, U, <C-R>) can be
extended, too.
(Personally, I'd like to have undo / redo stop and beep at the last
saved position, to avoid that I accidentally undo beyond the saved state
even though I didn't intend to.)
Need to :execute the :silent! call to avoid that the remainder of the
command line is aborted together with the call when repeat.vim is not
installed. Otherwise, <SID>MyFunction() won't be invoked, and the
mapping does nothing.
Since there seems to be a bug with "norm" in Vim prior to 7.3.100,
better go back to using feedkeys.
The repeat call can still be remapped by using something like:
nnoremap . :call repeat#run(v:count)<bar>call feedkeys('`.', 'n')<cr>
For commands that take an optional register (like p/P), Vim uses the
same register on repetition. This enhancement allows the same for custom
mappings, which need to call repeat#setreg() before repeat#set(). (No
changes for the vast majority of mappings that don't use registers.) It
even supports repeat of the expression register, with the expression
being re-evaluated on repeat.
repeat#set() so far automatically incremented b:changedtick. Problems
with this:
1. The way that was done clobbered the expression register "=.
2. It causes the "readonly" warning and "Cannot make changes" error in
readonly/nomodifiable buffers, so mappings that don't modify anything
cannot be repeated there.
3. It's actually not needed most of the time, because many user mappings
and all repeatable Vim built-in normal mode commands I know (with the
exception of yank with cpo+=y) actually do modify the buffer
themselves.
For the exceptional case where the user has a set of related mappings,
one that repeats naturally (e.g. a custom operator, via g@), and one
that invokes repeat#set(), and both do not modify the buffer, a new
function repeat#invalidate() is offered. This should be called by the
former mapping, and all is well.
The problem appears when cursor is positioned after line end, thus
running p command (even with empty "-register) causes Vim to add spaces
from line end until cursor.