Skip to content

Commit 630afe8

Browse files
committed
patch 8.1.0125: virtual edit replace with multi-byte fails at end of line
Problem: Virtual edit replace with multi-byte fails at end of line. (Lukas Werling) Solution: use ins_char() to add the character. (Christian Brabandt, closes #3114) Rename PCHAR() to PBYTE() to avoid mistakes like this.
1 parent d8b37a5 commit 630afe8

4 files changed

Lines changed: 50 additions & 13 deletions

File tree

‎src/macros.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
*/
1515

1616
/*
17-
* PCHAR(lp, c) - put character 'c' at position 'lp'
17+
* PBYTE(lp, c) - put byte 'c' at position 'lp'
1818
*/
19-
#define PCHAR(lp, c) (*(ml_get_buf(curbuf, (lp).lnum, TRUE) + (lp).col) = (c))
19+
#define PBYTE(lp, c) (*(ml_get_buf(curbuf, (lp).lnum, TRUE) + (lp).col) = (c))
2020

2121
/*
2222
* Position comparisons

‎src/ops.c‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,25 @@ mb_adjust_opend(oparg_T *oap)
21462146
#endif
21472147

21482148
#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2149+
2150+
# ifdef FEAT_MBYTE
2151+
/*
2152+
* Replace the character under the cursor with "c".
2153+
* This takes care of multi-byte characters.
2154+
*/
2155+
static void
2156+
replace_character(int c)
2157+
{
2158+
int n = State;
2159+
2160+
State = REPLACE;
2161+
ins_char(c);
2162+
State = n;
2163+
/* Backup to the replaced character. */
2164+
dec_cursor();
2165+
}
2166+
2167+
# endif
21492168
/*
21502169
* Replace a whole area with one character.
21512170
*/
@@ -2331,12 +2350,7 @@ op_replace(oparg_T *oap, int c)
23312350
* with a multi-byte and the other way around. */
23322351
if (curwin->w_cursor.lnum == oap->end.lnum)
23332352
oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2334-
n = State;
2335-
State = REPLACE;
2336-
ins_char(c);
2337-
State = n;
2338-
/* Backup to the replaced character. */
2339-
dec_cursor();
2353+
replace_character(c);
23402354
}
23412355
else
23422356
#endif
@@ -2358,7 +2372,7 @@ op_replace(oparg_T *oap, int c)
23582372
getvpos(&oap->end, end_vcol);
23592373
}
23602374
#endif
2361-
PCHAR(curwin->w_cursor, c);
2375+
PBYTE(curwin->w_cursor, c);
23622376
}
23632377
}
23642378
#ifdef FEAT_VIRTUALEDIT
@@ -2377,9 +2391,14 @@ op_replace(oparg_T *oap, int c)
23772391
curwin->w_cursor.col -= (virtcols + 1);
23782392
for (; virtcols >= 0; virtcols--)
23792393
{
2380-
PCHAR(curwin->w_cursor, c);
2381-
if (inc(&curwin->w_cursor) == -1)
2382-
break;
2394+
#ifdef FEAT_MBYTE
2395+
if ((*mb_char2len)(c) > 1)
2396+
replace_character(c);
2397+
else
2398+
#endif
2399+
PBYTE(curwin->w_cursor, c);
2400+
if (inc(&curwin->w_cursor) == -1)
2401+
break;
23832402
}
23842403
}
23852404
#endif
@@ -2619,7 +2638,7 @@ swapchar(int op_type, pos_T *pos)
26192638
}
26202639
else
26212640
#endif
2622-
PCHAR(*pos, nc);
2641+
PBYTE(*pos, nc);
26232642
return TRUE;
26242643
}
26252644
return FALSE;

‎src/testdir/test_virtualedit.vim‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ func Test_paste_end_of_line()
4242
set virtualedit=
4343
endfunc
4444

45+
func Test_replace_end_of_line()
46+
new
47+
set virtualedit=all
48+
call setline(1, range(20))
49+
exe "normal! gg2jv10lr-"
50+
call assert_equal(["1", "-----------", "3"], getline(2,4))
51+
if has('multi_byte')
52+
call setline(1, range(20))
53+
exe "normal! gg2jv10lr\<c-k>hh"
54+
call assert_equal(["1", "───────────", "3"], getline(2,4))
55+
endif
56+
57+
bwipe!
58+
set virtualedit=
59+
endfunc
60+
4561
func Test_edit_CTRL_G()
4662
new
4763
set virtualedit=insert

‎src/version.c‎

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

790790
static int included_patches[] =
791791
{ /* Add new patch number below this line */
792+
/**/
793+
125,
792794
/**/
793795
124,
794796
/**/

0 commit comments

Comments
 (0)