Merge pull request #30066 from zeertzjq/vim-9.1.0677

vim-patch: :keepp does not retain last substitute string
This commit is contained in:
zeertzjq 2024-08-17 07:04:10 +08:00 committed by GitHub
commit d698b3acca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 10 deletions

View File

@ -348,7 +348,7 @@ terminals)
:keepp[atterns] {command} *:keepp* *:keeppatterns*
Execute {command}, without adding anything to the search
history
history or modifying the last substitute string for |:&|.
==============================================================================
2. Command-line completion *cmdline-completion*

View File

@ -3085,7 +3085,7 @@ void sub_set_replacement(SubReplacementString sub)
///
/// @returns true if :substitute can be replaced with a join command
static bool sub_joining_lines(exarg_T *eap, char *pat, size_t patlen, const char *sub,
const char *cmd, bool save)
const char *cmd, bool save, bool keeppatterns)
FUNC_ATTR_NONNULL_ARG(1, 4, 5)
{
// TODO(vim): find a generic solution to make line-joining operations more
@ -3123,7 +3123,7 @@ static bool sub_joining_lines(exarg_T *eap, char *pat, size_t patlen, const char
}
if (save) {
if ((cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0) {
if (!keeppatterns) {
save_re_pat(RE_SUBST, pat, patlen, magic_isset());
}
// put pattern in history
@ -3331,6 +3331,7 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
linenr_T old_line_count = curbuf->b_ml.ml_line_count;
char *sub_firstline; // allocated copy of first sub line
bool endcolumn = false; // cursor in last column when done
const bool keeppatterns = cmdmod.cmod_flags & CMOD_KEEPPATTERNS;
PreviewLines preview_lines = { KV_INITIAL_VALUE, 0 };
static int pre_hl_id = 0;
pos_T old_cursor = curwin->w_cursor;
@ -3391,7 +3392,7 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
cmd = skip_substitute(cmd, delimiter);
sub = xstrdup(p);
if (!eap->skip && cmdpreview_ns <= 0) {
if (!eap->skip && !keeppatterns && cmdpreview_ns <= 0) {
sub_set_replacement((SubReplacementString) {
.sub = xstrdup(sub),
.timestamp = os_time(),
@ -3412,7 +3413,8 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n
endcolumn = (curwin->w_curswant == MAXCOL);
}
if (sub != NULL && sub_joining_lines(eap, pat, patlen, sub, cmd, cmdpreview_ns <= 0)) {
if (sub != NULL && sub_joining_lines(eap, pat, patlen, sub, cmd, cmdpreview_ns <= 0,
keeppatterns)) {
xfree(sub);
return 0;
}

View File

@ -806,7 +806,7 @@ func Test_replace_keeppatterns()
a
foobar
substitute foo asdf
substitute foo asdf foo
one two
.
@ -815,21 +815,26 @@ one two
/^substitute
s/foo/bar/
call assert_equal('foo', @/)
call assert_equal('substitute bar asdf', getline('.'))
call assert_equal('substitute bar asdf foo', getline('.'))
/^substitute
keeppatterns s/asdf/xyz/
call assert_equal('^substitute', @/)
call assert_equal('substitute bar xyz', getline('.'))
call assert_equal('substitute bar xyz foo', getline('.'))
/^substitute
&
call assert_equal('^substitute', @/)
call assert_equal('substitute bar xyz bar', getline('.'))
exe "normal /bar /e\<CR>"
call assert_equal(15, col('.'))
normal -
keeppatterns /xyz
call assert_equal('bar ', @/)
call assert_equal('substitute bar xyz', getline('.'))
call assert_equal('substitute bar xyz bar', getline('.'))
exe "normal 0dn"
call assert_equal('xyz', getline('.'))
call assert_equal('xyz bar', getline('.'))
close!
endfunc