Skip to content

Commit 3b59be4

Browse files
gpanderschrisbra
authored andcommitted
patch 9.1.0677: :keepp does not retain the substitute pattern
Problem: :keeppatterns does not retain the substitute pattern for a :s command Solution: preserve the last substitute pattern when used with the :keeppatterns command modifier (Gregory Anders) closes: #15497 Signed-off-by: Gregory Anders <greg@gpanders.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 12cb1d1 commit 3b59be4

5 files changed

Lines changed: 19 additions & 10 deletions

File tree

runtime/doc/cmdline.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*cmdline.txt* For Vim version 9.1. Last change: 2024 Apr 27
1+
*cmdline.txt* For Vim version 9.1. Last change: 2024 Aug 15
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -379,7 +379,7 @@ terminals)
379379
380380
:keepp[atterns] {command} *:keepp* *:keeppatterns*
381381
Execute {command}, without adding anything to the search
382-
history
382+
history or modifying the last substitute pattern.
383383

384384
==============================================================================
385385
2. Command-line completion *cmdline-completion*

runtime/doc/version9.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2024 Jul 30
1+
*version9.txt* For Vim version 9.1. Last change: 2024 Aug 15
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41592,6 +41592,7 @@ Changed~
4159241592
mark deprecated attributes from LSP server) |complete-items|
4159341593
- the regex engines match correctly case-insensitive multi-byte characters
4159441594
(and apply proper case folding)
41595+
- |:keeppatterns| preserves the last substitute pattern when used with |:s|
4159541596

4159641597
*added-9.2*
4159741598
Added ~

src/ex_cmds.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,6 +3777,7 @@ ex_substitute(exarg_T *eap)
37773777
int endcolumn = FALSE; // cursor in last column when done
37783778
pos_T old_cursor = curwin->w_cursor;
37793779
int start_nsubs;
3780+
int keeppatterns = cmdmod.cmod_flags & CMOD_KEEPPATTERNS;
37803781
#ifdef FEAT_EVAL
37813782
int save_ma = 0;
37823783
int save_sandbox = 0;
@@ -3876,7 +3877,7 @@ ex_substitute(exarg_T *eap)
38763877
// out of memory
38773878
return;
38783879
}
3879-
else
3880+
else if (!keeppatterns)
38803881
{
38813882
vim_free(old_sub);
38823883
old_sub = vim_strsave(sub);
@@ -3940,7 +3941,7 @@ ex_substitute(exarg_T *eap)
39403941
ex_may_print(eap);
39413942
}
39423943

3943-
if ((cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0)
3944+
if (!keeppatterns)
39443945
save_re_pat(RE_SUBST, pat, patlen, magic_isset());
39453946
// put pattern in history
39463947
add_to_history(HIST_SEARCH, pat, patlen, TRUE, NUL);

src/testdir/test_substitute.vim

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ func Test_replace_keeppatterns()
806806
a
807807
foobar
808808

809-
substitute foo asdf
809+
substitute foo asdf foo
810810

811811
one two
812812
.
@@ -815,21 +815,26 @@ one two
815815
/^substitute
816816
s/foo/bar/
817817
call assert_equal('foo', @/)
818-
call assert_equal('substitute bar asdf', getline('.'))
818+
call assert_equal('substitute bar asdf foo', getline('.'))
819819

820820
/^substitute
821821
keeppatterns s/asdf/xyz/
822822
call assert_equal('^substitute', @/)
823-
call assert_equal('substitute bar xyz', getline('.'))
823+
call assert_equal('substitute bar xyz foo', getline('.'))
824+
825+
/^substitute
826+
&
827+
call assert_equal('^substitute', @/)
828+
call assert_equal('substitute bar xyz bar', getline('.'))
824829

825830
exe "normal /bar /e\<CR>"
826831
call assert_equal(15, col('.'))
827832
normal -
828833
keeppatterns /xyz
829834
call assert_equal('bar ', @/)
830-
call assert_equal('substitute bar xyz', getline('.'))
835+
call assert_equal('substitute bar xyz bar', getline('.'))
831836
exe "normal 0dn"
832-
call assert_equal('xyz', getline('.'))
837+
call assert_equal('xyz bar', getline('.'))
833838

834839
close!
835840
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
677,
707709
/**/
708710
676,
709711
/**/

0 commit comments

Comments
 (0)