vim-patch:9.1.0142: getregion() can be improved (#27662)

Problem:  getregion() can be improved (after v9.1.120)
Solution: change getregion() implementation to use pos as lists and
          one optional {opt} dictionary (Shougo Matsushita)

Note: The following is a breaking change!

Currently, the getregion() function (included as of patch v9.1.120) takes
3 arguments: the first 2 arguments are strings, describing a position,
arg3 is the type string.

However, that is slightly inflexible, there is no way to specify
additional arguments. So let's instead change the function signature to:

getregion(pos1, pos2 [, {Dict}]) where both pos1 and pos2 are lists.
This is slightly cleaner, and gives us the flexibility to specify
additional arguments as key/value pairs to the optional Dict arg.

Now it supports the "type" key to specify the selection type
(characterwise, blockwise or linewise) and now in addition one can also
define the selection type, independently of what the 'selection' option
actually is.

Technically, this is a breaking change, but since the getregion()
Vimscript function is still quite new, this should be fine.

closes: vim/vim#14090

19b718828d

Co-authored-by: Shougo Matsushita <Shougo.Matsu@gmail.com>
This commit is contained in:
zeertzjq 2024-02-29 07:19:26 +08:00 committed by GitHub
parent e592657df8
commit ce7c51a1a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 250 additions and 124 deletions

View File

@ -2918,14 +2918,25 @@ getreginfo([{regname}]) *getreginfo()*
If {regname} is not specified, |v:register| is used. If {regname} is not specified, |v:register| is used.
The returned Dictionary can be passed to |setreg()|. The returned Dictionary can be passed to |setreg()|.
getregion({pos1}, {pos2}, {type}) *getregion()* getregion({pos1}, {pos2} [, {opts}]) *getregion()*
Returns the list of strings from {pos1} to {pos2} as if it's Returns the list of strings from {pos1} to {pos2} in current
selected in visual mode of {type}. buffer.
For possible values of {pos1} and {pos2} see |line()|.
{type} is the selection type: {pos1} and {pos2} must both be |List|s with four numbers.
"v" for |charwise| mode See |getpos()| for the format of the list.
"V" for |linewise| mode
"<CTRL-V>" for |blockwise-visual| mode The optional argument {opts} is a Dict and supports the
following items:
type Specify the selection type
(default: "v"):
"v" for |charwise| mode
"V" for |linewise| mode
"<CTRL-V>" for |blockwise-visual| mode
exclusive If |TRUE|, use exclusive selection
for the end position 'selection'.
You can get the last selection type by |visualmode()|. You can get the last selection type by |visualmode()|.
If Visual mode is active, use |mode()| to get the Visual mode If Visual mode is active, use |mode()| to get the Visual mode
(e.g., in a |:vmap|). (e.g., in a |:vmap|).
@ -2943,16 +2954,13 @@ getregion({pos1}, {pos2}, {type}) *getregion()*
- If the selection starts or ends in the middle of a multibyte - If the selection starts or ends in the middle of a multibyte
character, it is not included but its selected part is character, it is not included but its selected part is
substituted with spaces. substituted with spaces.
- If {pos1} or {pos2} equals "v" (see |line()|) and it is not in - If {pos1} or {pos2} is not current in the buffer, an empty
|visual-mode|, an empty list is returned.
- If {pos1}, {pos2} or {type} is an invalid string, an empty
list is returned.
- If {pos1} or {pos2} is a mark in different buffer, an empty
list is returned. list is returned.
Examples: > Examples: >
:xnoremap <CR> :xnoremap <CR>
\ <Cmd>echom getregion('v', '.', mode())<CR> \ <Cmd>echom getregion(
\ getpos('v'), getpos('.'), #{ type: mode() })<CR>
< <
getregtype([{regname}]) *getregtype()* getregtype([{regname}]) *getregtype()*

View File

@ -6,7 +6,8 @@ do
do do
local function _visual_search(cmd) local function _visual_search(cmd)
assert(cmd == '/' or cmd == '?') assert(cmd == '/' or cmd == '?')
local chunks = vim.fn.getregion('.', 'v', vim.fn.mode()) local chunks =
vim.fn.getregion(vim.fn.getpos('.'), vim.fn.getpos('v'), { type = vim.fn.mode() })
local esc_chunks = vim local esc_chunks = vim
.iter(chunks) .iter(chunks)
:map(function(v) :map(function(v)

View File

@ -3525,13 +3525,24 @@ function vim.fn.getreg(regname, list) end
--- @return table --- @return table
function vim.fn.getreginfo(regname) end function vim.fn.getreginfo(regname) end
--- Returns the list of strings from {pos1} to {pos2} as if it's --- Returns the list of strings from {pos1} to {pos2} in current
--- selected in visual mode of {type}. --- buffer.
--- For possible values of {pos1} and {pos2} see |line()|. ---
--- {type} is the selection type: --- {pos1} and {pos2} must both be |List|s with four numbers.
--- "v" for |charwise| mode --- See |getpos()| for the format of the list.
--- "V" for |linewise| mode ---
--- "<CTRL-V>" for |blockwise-visual| mode --- The optional argument {opts} is a Dict and supports the
--- following items:
---
--- type Specify the selection type
--- (default: "v"):
--- "v" for |charwise| mode
--- "V" for |linewise| mode
--- "<CTRL-V>" for |blockwise-visual| mode
---
--- exclusive If |TRUE|, use exclusive selection
--- for the end position 'selection'.
---
--- You can get the last selection type by |visualmode()|. --- You can get the last selection type by |visualmode()|.
--- If Visual mode is active, use |mode()| to get the Visual mode --- If Visual mode is active, use |mode()| to get the Visual mode
--- (e.g., in a |:vmap|). --- (e.g., in a |:vmap|).
@ -3549,23 +3560,20 @@ function vim.fn.getreginfo(regname) end
--- - If the selection starts or ends in the middle of a multibyte --- - If the selection starts or ends in the middle of a multibyte
--- character, it is not included but its selected part is --- character, it is not included but its selected part is
--- substituted with spaces. --- substituted with spaces.
--- - If {pos1} or {pos2} equals "v" (see |line()|) and it is not in --- - If {pos1} or {pos2} is not current in the buffer, an empty
--- |visual-mode|, an empty list is returned.
--- - If {pos1}, {pos2} or {type} is an invalid string, an empty
--- list is returned.
--- - If {pos1} or {pos2} is a mark in different buffer, an empty
--- list is returned. --- list is returned.
--- ---
--- Examples: > --- Examples: >
--- :xnoremap <CR> --- :xnoremap <CR>
--- \ <Cmd>echom getregion('v', '.', mode())<CR> --- \ <Cmd>echom getregion(
--- \ getpos('v'), getpos('.'), #{ type: mode() })<CR>
--- < --- <
--- ---
--- @param pos1 string --- @param pos1 table
--- @param pos2 string --- @param pos2 table
--- @param type string --- @param opts? table
--- @return string[] --- @return string[]
function vim.fn.getregion(pos1, pos2, type) end function vim.fn.getregion(pos1, pos2, opts) end
--- The result is a String, which is type of register {regname}. --- The result is a String, which is type of register {regname}.
--- The value will be one of: --- The value will be one of:

View File

@ -4356,16 +4356,27 @@ M.funcs = {
signature = 'getreginfo([{regname}])', signature = 'getreginfo([{regname}])',
}, },
getregion = { getregion = {
args = 3, args = { 2, 3 },
base = 1, base = 1,
desc = [=[ desc = [=[
Returns the list of strings from {pos1} to {pos2} as if it's Returns the list of strings from {pos1} to {pos2} in current
selected in visual mode of {type}. buffer.
For possible values of {pos1} and {pos2} see |line()|.
{type} is the selection type: {pos1} and {pos2} must both be |List|s with four numbers.
"v" for |charwise| mode See |getpos()| for the format of the list.
"V" for |linewise| mode
"<CTRL-V>" for |blockwise-visual| mode The optional argument {opts} is a Dict and supports the
following items:
type Specify the selection type
(default: "v"):
"v" for |charwise| mode
"V" for |linewise| mode
"<CTRL-V>" for |blockwise-visual| mode
exclusive If |TRUE|, use exclusive selection
for the end position 'selection'.
You can get the last selection type by |visualmode()|. You can get the last selection type by |visualmode()|.
If Visual mode is active, use |mode()| to get the Visual mode If Visual mode is active, use |mode()| to get the Visual mode
(e.g., in a |:vmap|). (e.g., in a |:vmap|).
@ -4383,22 +4394,19 @@ M.funcs = {
- If the selection starts or ends in the middle of a multibyte - If the selection starts or ends in the middle of a multibyte
character, it is not included but its selected part is character, it is not included but its selected part is
substituted with spaces. substituted with spaces.
- If {pos1} or {pos2} equals "v" (see |line()|) and it is not in - If {pos1} or {pos2} is not current in the buffer, an empty
|visual-mode|, an empty list is returned.
- If {pos1}, {pos2} or {type} is an invalid string, an empty
list is returned.
- If {pos1} or {pos2} is a mark in different buffer, an empty
list is returned. list is returned.
Examples: > Examples: >
:xnoremap <CR> :xnoremap <CR>
\ <Cmd>echom getregion('v', '.', mode())<CR> \ <Cmd>echom getregion(
\ getpos('v'), getpos('.'), #{ type: mode() })<CR>
< <
]=], ]=],
name = 'getregion', name = 'getregion',
params = { { 'pos1', 'string' }, { 'pos2', 'string' }, { 'type', 'string' } }, params = { { 'pos1', 'table' }, { 'pos2', 'table' }, { 'opts', 'table' } },
returns = 'string[]', returns = 'string[]',
signature = 'getregion({pos1}, {pos2}, {type})', signature = 'getregion({pos1}, {pos2} [, {opts}])',
}, },
getregtype = { getregtype = {
args = { 0, 1 }, args = { 0, 1 },

View File

@ -2821,35 +2821,38 @@ static void f_getregion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{ {
tv_list_alloc_ret(rettv, kListLenMayKnow); tv_list_alloc_ret(rettv, kListLenMayKnow);
if (tv_check_for_string_arg(argvars, 0) == FAIL if (tv_check_for_list_arg(argvars, 0) == FAIL
|| tv_check_for_string_arg(argvars, 1) == FAIL || tv_check_for_list_arg(argvars, 1) == FAIL
|| tv_check_for_string_arg(argvars, 2) == FAIL) { || tv_check_for_opt_dict_arg(argvars, 2) == FAIL) {
return; return;
} }
int fnum = -1; int fnum = -1;
// NOTE: var2fpos() returns static pointer. pos_T p1;
pos_T *fp = var2fpos(&argvars[0], true, &fnum, false); if (list2fpos(&argvars[0], &p1, &fnum, NULL, false) != OK
if (fp == NULL || (fnum >= 0 && fnum != curbuf->b_fnum)) { || (fnum >= 0 && fnum != curbuf->b_fnum)) {
return; return;
} }
pos_T p1 = *fp;
fp = var2fpos(&argvars[1], true, &fnum, false); pos_T p2;
if (fp == NULL || (fnum >= 0 && fnum != curbuf->b_fnum)) { if (list2fpos(&argvars[1], &p2, &fnum, NULL, false) != OK
|| (fnum >= 0 && fnum != curbuf->b_fnum)) {
return; return;
} }
pos_T p2 = *fp;
const char *pos1 = tv_get_string(&argvars[0]); bool is_select_exclusive;
const char *pos2 = tv_get_string(&argvars[1]); char *type;
const char *type = tv_get_string(&argvars[2]); char default_type[] = "v";
if (argvars[2].v_type == VAR_DICT) {
const bool is_visual is_select_exclusive = tv_dict_get_bool(argvars[2].vval.v_dict, "exclusive",
= (pos1[0] == 'v' && pos1[1] == NUL) || (pos2[0] == 'v' && pos2[1] == NUL); *p_sel == 'e');
type = tv_dict_get_string(argvars[2].vval.v_dict, "type", false);
if (is_visual && !VIsual_active) { if (type == NULL) {
return; type = default_type;
}
} else {
is_select_exclusive = *p_sel == 'e';
type = default_type;
} }
MotionType region_type = kMTUnknown; MotionType region_type = kMTUnknown;
@ -2866,6 +2869,10 @@ static void f_getregion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
const TriState save_virtual = virtual_op; const TriState save_virtual = virtual_op;
virtual_op = virtual_active(); virtual_op = virtual_active();
// NOTE: Adjust is needed.
p1.col--;
p2.col--;
if (!lt(p1, p2)) { if (!lt(p1, p2)) {
// swap position // swap position
pos_T p = p1; pos_T p = p1;
@ -2878,7 +2885,7 @@ static void f_getregion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
if (region_type == kMTCharWise) { if (region_type == kMTCharWise) {
// handle 'selection' == "exclusive" // handle 'selection' == "exclusive"
if (*p_sel == 'e' && !equalpos(p1, p2)) { if (is_select_exclusive && !equalpos(p1, p2)) {
if (p2.coladd > 0) { if (p2.coladd > 0) {
p2.coladd--; p2.coladd--;
} else if (p2.col > 0) { } else if (p2.col > 0) {
@ -2907,7 +2914,7 @@ static void f_getregion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
oa.start = p1; oa.start = p1;
oa.end = p2; oa.end = p2;
oa.start_vcol = MIN(sc1, sc2); oa.start_vcol = MIN(sc1, sc2);
if (*p_sel == 'e' && ec1 < sc2 && 0 < sc2 && ec2 > ec1) { if (is_select_exclusive && ec1 < sc2 && 0 < sc2 && ec2 > ec1) {
oa.end_vcol = sc2 - 1; oa.end_vcol = sc2 - 1;
} else { } else {
oa.end_vcol = MAX(ec1, ec2); oa.end_vcol = MAX(ec1, ec2);

View File

@ -1642,80 +1642,113 @@ func Test_visual_getregion()
" Visual mode " Visual mode
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<ESC>vjl", 'tx') call feedkeys("\<ESC>vjl", 'tx')
call assert_equal(['one', 'tw'], 'v'->getregion('.', 'v')) call assert_equal(['one', 'tw'],
call assert_equal(['one', 'tw'], '.'->getregion('v', 'v')) \ 'v'->getpos()->getregion(getpos('.')))
call assert_equal(['o'], 'v'->getregion('v', 'v')) call assert_equal(['one', 'tw'],
call assert_equal(['w'], '.'->getregion('.', 'v')) \ '.'->getpos()->getregion(getpos('v')))
call assert_equal(['one', 'two'], '.'->getregion('v', 'V')) call assert_equal(['o'],
call assert_equal(['on', 'tw'], '.'->getregion('v', "\<C-v>")) \ 'v'->getpos()->getregion(getpos('v')))
call assert_equal(['w'],
\ '.'->getpos()->getregion(getpos('.'), #{ type: 'v' }))
call assert_equal(['one', 'two'],
\ getpos('.')->getregion(getpos('v'), #{ type: 'V' }))
call assert_equal(['on', 'tw'],
\ getpos('.')->getregion(getpos('v'), #{ type: "\<C-v>" }))
" Line visual mode " Line visual mode
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<ESC>Vl", 'tx') call feedkeys("\<ESC>Vl", 'tx')
call assert_equal(['one'], getregion('v', '.', 'V')) call assert_equal(['one'],
call assert_equal(['one'], getregion('.', 'v', 'V')) \ getregion(getpos('v'), getpos('.'), #{ type: 'V' }))
call assert_equal(['one'], getregion('v', 'v', 'V')) call assert_equal(['one'],
call assert_equal(['one'], getregion('.', '.', 'V')) \ getregion(getpos('.'), getpos('v'), #{ type: 'V' }))
call assert_equal(['on'], '.'->getregion('v', 'v')) call assert_equal(['one'],
call assert_equal(['on'], '.'->getregion('v', "\<C-v>")) \ getregion(getpos('v'), getpos('v'), #{ type: 'V' }))
call assert_equal(['one'],
\ getregion(getpos('.'), getpos('.'), #{ type: 'V' }))
call assert_equal(['on'],
\ getpos('.')->getregion(getpos('v'), #{ type: 'v' }))
call assert_equal(['on'],
\ getpos('.')->getregion(getpos('v'), #{ type: "\<C-v>" }))
" Block visual mode " Block visual mode
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<ESC>\<C-v>ll", 'tx') call feedkeys("\<ESC>\<C-v>ll", 'tx')
call assert_equal(['one'], getregion('v', '.', "\<C-v>")) call assert_equal(['one'],
call assert_equal(['one'], getregion('.', 'v', "\<C-v>")) \ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
call assert_equal(['o'], getregion('v', 'v', "\<C-v>")) call assert_equal(['one'],
call assert_equal(['e'], getregion('.', '.', "\<C-v>")) \ getregion(getpos('.'), getpos('v'), #{ type: "\<C-v>" }))
call assert_equal(['one'], '.'->getregion('v', 'V')) call assert_equal(['o'],
call assert_equal(['one'], '.'->getregion('v', 'v')) \ getregion(getpos('v'), getpos('v'), #{ type: "\<C-v>" }))
call assert_equal(['e'],
\ getregion(getpos('.'), getpos('.'), #{ type: "\<C-v>" }))
call assert_equal(['one'],
\ '.'->getpos()->getregion(getpos('v'), #{ type: 'V' }))
call assert_equal(['one'],
\ '.'->getpos()->getregion(getpos('v'), #{ type: 'v' }))
" Using Marks " Using Marks
call setpos("'a", [0, 2, 3, 0]) call setpos("'a", [0, 2, 3, 0])
call cursor(1, 1) call cursor(1, 1)
call assert_equal(['one', 'two'], "'a"->getregion('.', 'v')) call assert_equal(['one', 'two'],
call assert_equal(['one', 'two'], "."->getregion("'a", 'v')) \ "'a"->getpos()->getregion(getpos('.'), #{ type: 'v' }))
call assert_equal(['one', 'two'], "."->getregion("'a", 'V')) call assert_equal(['one', 'two'],
call assert_equal(['two'], "'a"->getregion("'a", 'V')) \ "."->getpos()->getregion(getpos("'a"), #{ type: 'v' }))
call assert_equal(['one', 'two'], "."->getregion("'a", "\<c-v>")) call assert_equal(['one', 'two'],
\ "."->getpos()->getregion(getpos("'a"), #{ type: 'V' }))
call assert_equal(['two'],
\ "'a"->getpos()->getregion(getpos("'a"), #{ type: 'V' }))
call assert_equal(['one', 'two'],
\ "."->getpos()->getregion(getpos("'a"), #{ type: "\<c-v>" }))
" Using List
call cursor(1, 1)
call assert_equal(['one', 'two'],
\ [0, 2, 3, 0]->getregion(getpos('.'), #{ type: 'v' }))
call assert_equal(['one', 'two'],
\ '.'->getpos()->getregion([0, 2, 3, 0], #{ type: 'v' }))
call assert_equal(['one', 'two'],
\ '.'->getpos()->getregion([0, 2, 3, 0], #{ type: 'V' }))
call assert_equal(['two'],
\ [0, 2, 3, 0]->getregion([0, 2, 3, 0], #{ type: 'V' }))
call assert_equal(['one', 'two'],
\ '.'->getpos()->getregion([0, 2, 3, 0], #{ type: "\<c-v>" }))
" Multiline with line visual mode " Multiline with line visual mode
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<ESC>Vjj", 'tx') call feedkeys("\<ESC>Vjj", 'tx')
call assert_equal(['one', 'two', 'three'], getregion('v', '.', 'V')) call assert_equal(['one', 'two', 'three'],
\ getregion(getpos('v'), getpos('.'), #{ type: 'V' }))
" Multiline with block visual mode " Multiline with block visual mode
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<ESC>\<C-v>jj", 'tx') call feedkeys("\<ESC>\<C-v>jj", 'tx')
call assert_equal(['o', 't', 't'], getregion('v', '.', "\<C-v>")) call assert_equal(['o', 't', 't'],
\ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<ESC>\<C-v>jj$", 'tx') call feedkeys("\<ESC>\<C-v>jj$", 'tx')
call assert_equal(['one', 'two', 'three'], getregion('v', '.', "\<C-v>")) call assert_equal(['one', 'two', 'three'],
\ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
" 'virtualedit' " 'virtualedit'
set virtualedit=all set virtualedit=all
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<ESC>\<C-v>10ljj$", 'tx') call feedkeys("\<ESC>\<C-v>10ljj$", 'tx')
call assert_equal(['one ', 'two ', 'three '], call assert_equal(['one ', 'two ', 'three '],
\ getregion('v', '.', "\<C-v>")) \ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
set virtualedit& set virtualedit&
" Invalid position " Invalid position
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<ESC>vjj$", 'tx') call feedkeys("\<ESC>vjj$", 'tx')
call assert_fails("call getregion(1, 2, 'v')", 'E1174:') call assert_fails("call getregion(1, 2)", 'E1211:')
call assert_fails("call getregion('.', {}, 'v')", 'E1174:') call assert_fails("call getregion(getpos('.'), {})", 'E1211:')
call assert_equal([], getregion('', '.', 'v')) call assert_equal([], getregion(getpos('.'), getpos('.'), #{ type: '' }))
call assert_equal([], getregion('.', '.', ''))
call feedkeys("\<ESC>", 'tx')
call assert_equal([], getregion('v', '.', 'v'))
" using an unset mark
call assert_equal([], "'z"->getregion(".", 'V'))
" using the wrong type " using the wrong type
call assert_fails(':echo "."->getregion([],"V")', 'E1174:') call assert_fails(':echo "."->getpos()->getregion("$", [])', 'E1211:')
call assert_fails(':echo "."->getregion("$", {})', 'E1174:')
call assert_fails(':echo [0, 1, 1, 0]->getregion("$", "v")', 'E1174:')
" using a mark in another buffer " using a mark in another buffer
new new
let newbuf = bufnr() let newbuf = bufnr()
@ -1723,8 +1756,8 @@ func Test_visual_getregion()
normal! GmA normal! GmA
wincmd p wincmd p
call assert_equal([newbuf, 10, 1, 0], getpos("'A")) call assert_equal([newbuf, 10, 1, 0], getpos("'A"))
call assert_equal([], getregion(".", "'A", 'v')) call assert_equal([], getregion(getpos('.'), getpos("'A"), #{ type: 'v' }))
call assert_equal([], getregion("'A", ".", 'v')) call assert_equal([], getregion(getpos("'A"), getpos('.'), #{ type: 'v' }))
exe newbuf .. 'bwipe!' exe newbuf .. 'bwipe!'
bwipe! bwipe!
@ -1738,26 +1771,47 @@ func Test_visual_getregion()
call cursor(1, 3) call cursor(1, 3)
call feedkeys("\<Esc>\<C-v>ljj", 'xt') call feedkeys("\<Esc>\<C-v>ljj", 'xt')
call assert_equal(['cd', "\u00ab ", '34'], call assert_equal(['cd', "\u00ab ", '34'],
\ getregion('v', '.', "\<C-v>")) \ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
call cursor(1, 4) call cursor(1, 4)
call feedkeys("\<Esc>\<C-v>ljj", 'xt') call feedkeys("\<Esc>\<C-v>ljj", 'xt')
call assert_equal(['de', "\U0001f1e7", '45'], call assert_equal(['de', "\U0001f1e7", '45'],
\ getregion('v', '.', "\<C-v>")) \ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
call cursor(1, 5) call cursor(1, 5)
call feedkeys("\<Esc>\<C-v>jj", 'xt') call feedkeys("\<Esc>\<C-v>jj", 'xt')
call assert_equal(['e', ' ', '5'], getregion('v', '.', "\<C-v>")) call assert_equal(['e', ' ', '5'],
\ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>vj", 'xt') call feedkeys("\<Esc>vj", 'xt')
call assert_equal(['abcdefghijk«', "\U0001f1e6"], getregion('v', '.', "v")) call assert_equal(['abcdefghijk«', "\U0001f1e6"],
\ getregion(getpos('v'), getpos('.'), #{ type: 'v' }))
" marks on multibyte chars " marks on multibyte chars
set selection=exclusive set selection=exclusive
call setpos("'a", [0, 1, 11, 0]) call setpos("'a", [0, 1, 11, 0])
call setpos("'b", [0, 2, 16, 0]) call setpos("'b", [0, 2, 16, 0])
call setpos("'c", [0, 2, 0, 0]) call setpos("'c", [0, 2, 0, 0])
call cursor(1, 1) call cursor(1, 1)
call assert_equal(['ghijk', '🇨«🇩'], getregion("'a", "'b", "\<c-v>")) call assert_equal(['ghijk', '🇨«🇩'],
call assert_equal(['k«', '🇦«🇧«🇨'], getregion("'a", "'b", "v")) \ getregion(getpos("'a"), getpos("'b"), #{ type: "\<c-v>" }))
call assert_equal(['k«'], getregion("'a", "'c", "v")) call assert_equal(['k«', '🇦«🇧«🇨'],
\ getregion(getpos("'a"), getpos("'b"), #{ type: 'v' }))
call assert_equal(['k«'],
\ getregion(getpos("'a"), getpos("'c"), #{ type: 'v' }))
" use inclusive selection, although 'selection' is exclusive
call setpos("'a", [0, 1, 11, 0])
call setpos("'b", [0, 1, 1, 0])
call assert_equal(['abcdefghijk'],
\ getregion(getpos("'a"), getpos("'b"), #{ type: "\<c-v>", exclusive: v:false }))
call assert_equal(['abcdefghij'],
\ getregion(getpos("'a"), getpos("'b"), #{ type: "\<c-v>", exclusive: v:true }))
call assert_equal(['abcdefghijk'],
\ getregion(getpos("'a"), getpos("'b"), #{ type: 'v', exclusive: 0 }))
call assert_equal(['abcdefghij'],
\ getregion(getpos("'a"), getpos("'b"), #{ type: 'v', exclusive: 1 }))
call assert_equal(['abcdefghijk«'],
\ getregion(getpos("'a"), getpos("'b"), #{ type: 'V', exclusive: 0 }))
call assert_equal(['abcdefghijk«'],
\ getregion(getpos("'a"), getpos("'b"), #{ type: 'V', exclusive: 1 }))
bwipe! bwipe!
@ -1767,31 +1821,71 @@ func Test_visual_getregion()
call setline(1, ["a\tc", "x\tz", '', '']) call setline(1, ["a\tc", "x\tz", '', ''])
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>v2l", 'xt') call feedkeys("\<Esc>v2l", 'xt')
call assert_equal(["a\t"], getregion('v', '.', 'v')) call assert_equal(["a\t"],
\ getregion(getpos('v'), getpos('.'), #{ type: 'v' }))
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>v$G", 'xt') call feedkeys("\<Esc>v$G", 'xt')
call assert_equal(["a\tc", "x\tz", ''], getregion('v', '.', 'v')) call assert_equal(["a\tc", "x\tz", ''],
\ getregion(getpos('v'), getpos('.'), #{ type: 'v' }))
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>v$j", 'xt') call feedkeys("\<Esc>v$j", 'xt')
call assert_equal(["a\tc", "x\tz"], getregion('v', '.', 'v')) call assert_equal(["a\tc", "x\tz"],
\ getregion(getpos('v'), getpos('.'), #{ type: 'v' }))
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>\<C-v>$j", 'xt') call feedkeys("\<Esc>\<C-v>$j", 'xt')
call assert_equal(["a\tc", "x\tz"], getregion('v', '.', "\<C-v>")) call assert_equal(["a\tc", "x\tz"],
\ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>\<C-v>$G", 'xt') call feedkeys("\<Esc>\<C-v>$G", 'xt')
call assert_equal(["a", "x", '', ''], getregion('v', '.', "\<C-v>")) call assert_equal(["a", "x", '', ''],
\ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>wv2j", 'xt') call feedkeys("\<Esc>wv2j", 'xt')
call assert_equal(["c", "x\tz"], getregion('v', '.', 'v')) call assert_equal(["c", "x\tz"],
\ getregion(getpos('v'), getpos('.'), #{ type: 'v' }))
set selection&
" Exclusive selection 2
new
call setline(1, ["a\tc", "x\tz", '', ''])
call cursor(1, 1)
call feedkeys("\<Esc>v2l", 'xt')
call assert_equal(["a\t"],
\ getregion(getpos('v'), getpos('.'), #{ exclusive: v:true }))
call cursor(1, 1)
call feedkeys("\<Esc>v$G", 'xt')
call assert_equal(["a\tc", "x\tz", ''],
\ getregion(getpos('v'), getpos('.'), #{ exclusive: v:true }))
call cursor(1, 1)
call feedkeys("\<Esc>v$j", 'xt')
call assert_equal(["a\tc", "x\tz"],
\ getregion(getpos('v'), getpos('.'), #{ exclusive: v:true }))
call cursor(1, 1)
call feedkeys("\<Esc>\<C-v>$j", 'xt')
call assert_equal(["a\tc", "x\tz"],
\ getregion(getpos('v'), getpos('.'),
\ #{ exclusive: v:true, type: "\<C-v>" }))
call cursor(1, 1)
call feedkeys("\<Esc>\<C-v>$G", 'xt')
call assert_equal(["a", "x", '', ''],
\ getregion(getpos('v'), getpos('.'),
\ #{ exclusive: v:true, type: "\<C-v>" }))
call cursor(1, 1)
call feedkeys("\<Esc>wv2j", 'xt')
call assert_equal(["c", "x\tz"],
\ getregion(getpos('v'), getpos('.'), #{ exclusive: v:true }))
" virtualedit " virtualedit
set selection=exclusive
set virtualedit=all set virtualedit=all
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>2lv2lj", 'xt') call feedkeys("\<Esc>2lv2lj", 'xt')
call assert_equal([' c', 'x '], getregion('v', '.', 'v')) call assert_equal([' c', 'x '],
\ getregion(getpos('v'), getpos('.'), #{ type: 'v' }))
call cursor(1, 1) call cursor(1, 1)
call feedkeys("\<Esc>2l\<C-v>2l2j", 'xt') call feedkeys("\<Esc>2l\<C-v>2l2j", 'xt')
call assert_equal([' ', ' ', ' '], getregion('v', '.', "\<C-v>")) call assert_equal([' ', ' ', ' '],
\ getregion(getpos('v'), getpos('.'), #{ type: "\<C-v>" }))
set virtualedit& set virtualedit&
set selection& set selection&