Skip to content

Commit c8f12c9

Browse files
committed
patch 8.2.1688: increment/decrement removes text property
Problem: Increment/decrement removes text property. Solution: Insert the new number before deleting the old one. (closes #6962)
1 parent 9b123d8 commit c8f12c9

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

src/ops.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,9 @@ do_addsub(
26362636
}
26372637
else
26382638
{
2639+
pos_T save_pos;
2640+
int i;
2641+
26392642
if (col > 0 && ptr[col - 1] == '-'
26402643
&& (!has_mbyte ||
26412644
!(*mb_head_off)(ptr, ptr + col - 1))
@@ -2734,7 +2737,9 @@ do_addsub(
27342737
*/
27352738
if (c == '-')
27362739
--length;
2737-
while (todel-- > 0)
2740+
2741+
save_pos = curwin->w_cursor;
2742+
for (i = 0; i < todel; ++i)
27382743
{
27392744
if (c < 0x100 && isalpha(c))
27402745
{
@@ -2743,10 +2748,10 @@ do_addsub(
27432748
else
27442749
hexupper = FALSE;
27452750
}
2746-
// del_char() will mark line needing displaying
2747-
(void)del_char(FALSE);
2751+
inc_cursor();
27482752
c = gchar_cursor();
27492753
}
2754+
curwin->w_cursor = save_pos;
27502755

27512756
/*
27522757
* Prepare the leading characters in buf1[].
@@ -2776,7 +2781,6 @@ do_addsub(
27762781
*/
27772782
if (pre == 'b' || pre == 'B')
27782783
{
2779-
int i;
27802784
int bit = 0;
27812785
int bits = sizeof(uvarnumber_T) * 8;
27822786

@@ -2809,9 +2813,33 @@ do_addsub(
28092813
while (length-- > 0)
28102814
*ptr++ = '0';
28112815
*ptr = NUL;
2816+
28122817
STRCAT(buf1, buf2);
2818+
2819+
// Insert just after the first character to be removed, so that any
2820+
// text properties will be adjusted. Then delete the old number
2821+
// afterwards.
2822+
save_pos = curwin->w_cursor;
2823+
if (todel > 0)
2824+
inc_cursor();
28132825
ins_str(buf1); // insert the new number
28142826
vim_free(buf1);
2827+
2828+
// del_char() will also mark line needing displaying
2829+
if (todel > 0)
2830+
{
2831+
int bytes_after = (int)STRLEN(ml_get_curline())
2832+
- curwin->w_cursor.col;
2833+
2834+
// Delete the one character before the insert.
2835+
curwin->w_cursor = save_pos;
2836+
(void)del_char(FALSE);
2837+
curwin->w_cursor.col = STRLEN(ml_get_curline()) - bytes_after;
2838+
--todel;
2839+
}
2840+
while (todel-- > 0)
2841+
(void)del_char(FALSE);
2842+
28152843
endpos = curwin->w_cursor;
28162844
if (did_change && curwin->w_cursor.col)
28172845
--curwin->w_cursor.col;

src/testdir/test_textprop.vim

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ func Test_prop_func_invalid_args()
12731273
call assert_fails("call prop_type_list([])", 'E715:')
12741274
endfunc
12751275

1276-
func Test_split_join()
1276+
func Test_prop_split_join()
12771277
new
12781278
call prop_type_add('test', {'highlight': 'ErrorMsg'})
12791279
call setline(1, 'just some text')
@@ -1294,4 +1294,24 @@ func Test_split_join()
12941294
call prop_type_delete('test')
12951295
endfunc
12961296

1297+
func Test_prop_increment_decrement()
1298+
new
1299+
call prop_type_add('test', {'highlight': 'ErrorMsg'})
1300+
call setline(1, 'its 998 times')
1301+
call prop_add(1, 5, {'length': 3, 'type': 'test'})
1302+
1303+
exe "normal! 0f9\<C-A>"
1304+
eval getline(1)->assert_equal('its 999 times')
1305+
eval prop_list(1)->assert_equal([
1306+
\ #{id: 0, col: 5, end: 1, type: 'test', length: 3, start: 1}])
1307+
1308+
exe "normal! 0f9\<C-A>"
1309+
eval getline(1)->assert_equal('its 1000 times')
1310+
eval prop_list(1)->assert_equal([
1311+
\ #{id: 0, col: 5, end: 1, type: 'test', length: 4, start: 1}])
1312+
1313+
bwipe!
1314+
call prop_type_delete('test')
1315+
endfunc
1316+
12971317
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
1688,
753755
/**/
754756
1687,
755757
/**/

0 commit comments

Comments
 (0)