vim-patch:9.1.0446: getregionpos() inconsistent for partly-selected multibyte char (#29032)

Problem:  getregionpos() behaves inconsistently for a partly-selected
          multibyte char.
Solution: Always use column of the first byte for a partly-selected
          multibyte char (zeertzjq).

closes: vim/vim#14851

ef73374dc3
This commit is contained in:
zeertzjq 2024-05-27 05:50:49 +08:00 committed by GitHub
parent 43a2019f09
commit 22fe04452e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 3 deletions

View File

@ -3042,6 +3042,7 @@ static void f_getregionpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr
for (linenr_T lnum = p1.lnum; lnum <= p2.lnum; lnum++) { for (linenr_T lnum = p1.lnum; lnum <= p2.lnum; lnum++) {
pos_T ret_p1, ret_p2; pos_T ret_p1, ret_p2;
char *line = ml_get(lnum);
colnr_T line_len = ml_get_len(lnum); colnr_T line_len = ml_get_len(lnum);
if (region_type == kMTLineWise) { if (region_type == kMTLineWise) {
@ -3060,7 +3061,7 @@ static void f_getregionpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr
if (bd.is_oneChar) { // selection entirely inside one char if (bd.is_oneChar) { // selection entirely inside one char
if (region_type == kMTBlockWise) { if (region_type == kMTBlockWise) {
ret_p1.col = bd.textcol; ret_p1.col = (colnr_T)(mb_prevptr(line, bd.textstart) - line) + 1;
ret_p1.coladd = bd.start_char_vcols - (bd.start_vcol - oa.start_vcol); ret_p1.coladd = bd.start_char_vcols - (bd.start_vcol - oa.start_vcol);
} else { } else {
ret_p1.col = p1.col + 1; ret_p1.col = p1.col + 1;
@ -3072,7 +3073,7 @@ static void f_getregionpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr
ret_p1.coladd = oa.start_vcol - bd.start_vcol; ret_p1.coladd = oa.start_vcol - bd.start_vcol;
bd.is_oneChar = true; bd.is_oneChar = true;
} else if (bd.startspaces > 0) { } else if (bd.startspaces > 0) {
ret_p1.col = bd.textcol; ret_p1.col = (colnr_T)(mb_prevptr(line, bd.textstart) - line) + 1;
ret_p1.coladd = bd.start_char_vcols - bd.startspaces; ret_p1.coladd = bd.start_char_vcols - bd.startspaces;
} else { } else {
ret_p1.col = bd.textcol + 1; ret_p1.col = bd.textcol + 1;

View File

@ -2077,10 +2077,12 @@ func Test_visual_getregion()
\ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" })) \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
call assert_equal([ call assert_equal([
\ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 5, 0]], \ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 5, 0]],
\ [[bufnr('%'), 2, 10, 1], [bufnr('%'), 2, 10, 2]], \ [[bufnr('%'), 2, 7, 1], [bufnr('%'), 2, 7, 2]],
\ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]], \ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
\ ], \ ],
\ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" })) \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
call assert_equal(['efghijk«', '🇦«🇧«🇨«🇩', '12345'],
\ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
call assert_equal([ call assert_equal([
\ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 13, 0]], \ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 13, 0]],
\ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 22, 0]], \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 22, 0]],
@ -2088,6 +2090,28 @@ func Test_visual_getregion()
\ ], \ ],
\ getregionpos(getpos('v'), getpos('.'), {'type': 'v' })) \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
call cursor(1, 5)
call feedkeys("\<Esc>\<C-v>5l2j", 'xt')
call assert_equal(['efghij', ' «🇨« ', '567890'],
\ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
call assert_equal([
\ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 10, 0]],
\ [[bufnr('%'), 2, 7, 1], [bufnr('%'), 2, 19, 1]],
\ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 10, 0]],
\ ],
\ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
call cursor(1, 4)
call feedkeys("\<Esc>\<C-v>02j", 'xt')
call assert_equal(['abcd', '🇦« ', '1234'],
\ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
call assert_equal([
\ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
\ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 7, 1]],
\ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 4, 0]],
\ ],
\ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
#" characterwise selection with multibyte chars #" characterwise selection with multibyte chars
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>vj", 'xt') call feedkeys("\<Esc>vj", 'xt')