mirror of
https://github.com/neovim/neovim.git
synced 2025-10-05 09:26:30 +00:00
vim-patch:9.1.0155: can only get getregion() from current buffer (#27757)
Problem: can only call getregion() for current buffer
Solution: Allow to retrieve selections from different buffers
(Shougo Matsushita)
closes: vim/vim#14131
84bf6e658d
Co-authored-by: Shougo Matsushita <Shougo.Matsu@gmail.com>
This commit is contained in:
13
runtime/doc/builtin.txt
generated
13
runtime/doc/builtin.txt
generated
@@ -2919,11 +2919,13 @@ getreginfo([{regname}]) *getreginfo()*
|
|||||||
The returned Dictionary can be passed to |setreg()|.
|
The returned Dictionary can be passed to |setreg()|.
|
||||||
|
|
||||||
getregion({pos1}, {pos2} [, {opts}]) *getregion()*
|
getregion({pos1}, {pos2} [, {opts}]) *getregion()*
|
||||||
Returns the list of strings from {pos1} to {pos2} in current
|
Returns the list of strings from {pos1} to {pos2} from a
|
||||||
buffer.
|
buffer.
|
||||||
|
|
||||||
{pos1} and {pos2} must both be |List|s with four numbers.
|
{pos1} and {pos2} must both be |List|s with four numbers.
|
||||||
See |getpos()| for the format of the list.
|
See |getpos()| for the format of the list. It's possible
|
||||||
|
to specify positions from a different buffer, but please
|
||||||
|
note the limitations at |getregion-notes|
|
||||||
|
|
||||||
The optional argument {opts} is a Dict and supports the
|
The optional argument {opts} is a Dict and supports the
|
||||||
following items:
|
following items:
|
||||||
@@ -2944,6 +2946,7 @@ getregion({pos1}, {pos2} [, {opts}]) *getregion()*
|
|||||||
This function is useful to get text starting and ending in
|
This function is useful to get text starting and ending in
|
||||||
different columns, such as a |charwise-visual| selection.
|
different columns, such as a |charwise-visual| selection.
|
||||||
|
|
||||||
|
*getregion-notes*
|
||||||
Note that:
|
Note that:
|
||||||
- Order of {pos1} and {pos2} doesn't matter, it will always
|
- Order of {pos1} and {pos2} doesn't matter, it will always
|
||||||
return content from the upper left position to the lower
|
return content from the upper left position to the lower
|
||||||
@@ -2953,8 +2956,12 @@ getregion({pos1}, {pos2} [, {opts}]) *getregion()*
|
|||||||
- If the region is blockwise and it starts or ends in the
|
- If the region is blockwise and it starts or ends in the
|
||||||
middle of a multi-cell character, it is not included but
|
middle of a multi-cell character, it is not included but
|
||||||
its selected part is substituted with spaces.
|
its selected part is substituted with spaces.
|
||||||
- If {pos1} or {pos2} is not current in the buffer, an empty
|
- If {pos1} and {pos2} are not in the same buffer, an empty
|
||||||
list is returned.
|
list is returned.
|
||||||
|
- {pos1} and {pos2} must belong to a |bufloaded()| buffer.
|
||||||
|
- It is evaluated in current window context, this makes a
|
||||||
|
different if a buffer is displayed in a different window and
|
||||||
|
'virtualedit' or 'list' is set
|
||||||
|
|
||||||
Examples: >
|
Examples: >
|
||||||
:xnoremap <CR>
|
:xnoremap <CR>
|
||||||
|
13
runtime/lua/vim/_meta/vimfn.lua
generated
13
runtime/lua/vim/_meta/vimfn.lua
generated
@@ -3525,11 +3525,13 @@ 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} in current
|
--- Returns the list of strings from {pos1} to {pos2} from a
|
||||||
--- buffer.
|
--- buffer.
|
||||||
---
|
---
|
||||||
--- {pos1} and {pos2} must both be |List|s with four numbers.
|
--- {pos1} and {pos2} must both be |List|s with four numbers.
|
||||||
--- See |getpos()| for the format of the list.
|
--- See |getpos()| for the format of the list. It's possible
|
||||||
|
--- to specify positions from a different buffer, but please
|
||||||
|
--- note the limitations at |getregion-notes|
|
||||||
---
|
---
|
||||||
--- The optional argument {opts} is a Dict and supports the
|
--- The optional argument {opts} is a Dict and supports the
|
||||||
--- following items:
|
--- following items:
|
||||||
@@ -3550,6 +3552,7 @@ function vim.fn.getreginfo(regname) end
|
|||||||
--- This function is useful to get text starting and ending in
|
--- This function is useful to get text starting and ending in
|
||||||
--- different columns, such as a |charwise-visual| selection.
|
--- different columns, such as a |charwise-visual| selection.
|
||||||
---
|
---
|
||||||
|
--- *getregion-notes*
|
||||||
--- Note that:
|
--- Note that:
|
||||||
--- - Order of {pos1} and {pos2} doesn't matter, it will always
|
--- - Order of {pos1} and {pos2} doesn't matter, it will always
|
||||||
--- return content from the upper left position to the lower
|
--- return content from the upper left position to the lower
|
||||||
@@ -3559,8 +3562,12 @@ function vim.fn.getreginfo(regname) end
|
|||||||
--- - If the region is blockwise and it starts or ends in the
|
--- - If the region is blockwise and it starts or ends in the
|
||||||
--- middle of a multi-cell character, it is not included but
|
--- middle of a multi-cell character, it is not included but
|
||||||
--- its selected part is substituted with spaces.
|
--- its selected part is substituted with spaces.
|
||||||
--- - If {pos1} or {pos2} is not current in the buffer, an empty
|
--- - If {pos1} and {pos2} are not in the same buffer, an empty
|
||||||
--- list is returned.
|
--- list is returned.
|
||||||
|
--- - {pos1} and {pos2} must belong to a |bufloaded()| buffer.
|
||||||
|
--- - It is evaluated in current window context, this makes a
|
||||||
|
--- different if a buffer is displayed in a different window and
|
||||||
|
--- 'virtualedit' or 'list' is set
|
||||||
---
|
---
|
||||||
--- Examples: >
|
--- Examples: >
|
||||||
--- :xnoremap <CR>
|
--- :xnoremap <CR>
|
||||||
|
@@ -4359,11 +4359,13 @@ M.funcs = {
|
|||||||
args = { 2, 3 },
|
args = { 2, 3 },
|
||||||
base = 1,
|
base = 1,
|
||||||
desc = [=[
|
desc = [=[
|
||||||
Returns the list of strings from {pos1} to {pos2} in current
|
Returns the list of strings from {pos1} to {pos2} from a
|
||||||
buffer.
|
buffer.
|
||||||
|
|
||||||
{pos1} and {pos2} must both be |List|s with four numbers.
|
{pos1} and {pos2} must both be |List|s with four numbers.
|
||||||
See |getpos()| for the format of the list.
|
See |getpos()| for the format of the list. It's possible
|
||||||
|
to specify positions from a different buffer, but please
|
||||||
|
note the limitations at |getregion-notes|
|
||||||
|
|
||||||
The optional argument {opts} is a Dict and supports the
|
The optional argument {opts} is a Dict and supports the
|
||||||
following items:
|
following items:
|
||||||
@@ -4384,6 +4386,7 @@ M.funcs = {
|
|||||||
This function is useful to get text starting and ending in
|
This function is useful to get text starting and ending in
|
||||||
different columns, such as a |charwise-visual| selection.
|
different columns, such as a |charwise-visual| selection.
|
||||||
|
|
||||||
|
*getregion-notes*
|
||||||
Note that:
|
Note that:
|
||||||
- Order of {pos1} and {pos2} doesn't matter, it will always
|
- Order of {pos1} and {pos2} doesn't matter, it will always
|
||||||
return content from the upper left position to the lower
|
return content from the upper left position to the lower
|
||||||
@@ -4393,8 +4396,12 @@ M.funcs = {
|
|||||||
- If the region is blockwise and it starts or ends in the
|
- If the region is blockwise and it starts or ends in the
|
||||||
middle of a multi-cell character, it is not included but
|
middle of a multi-cell character, it is not included but
|
||||||
its selected part is substituted with spaces.
|
its selected part is substituted with spaces.
|
||||||
- If {pos1} or {pos2} is not current in the buffer, an empty
|
- If {pos1} and {pos2} are not in the same buffer, an empty
|
||||||
list is returned.
|
list is returned.
|
||||||
|
- {pos1} and {pos2} must belong to a |bufloaded()| buffer.
|
||||||
|
- It is evaluated in current window context, this makes a
|
||||||
|
different if a buffer is displayed in a different window and
|
||||||
|
'virtualedit' or 'list' is set
|
||||||
|
|
||||||
Examples: >
|
Examples: >
|
||||||
:xnoremap <CR>
|
:xnoremap <CR>
|
||||||
|
@@ -2827,16 +2827,12 @@ static void f_getregion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fnum = -1;
|
int fnum1 = -1;
|
||||||
pos_T p1;
|
int fnum2 = -1;
|
||||||
if (list2fpos(&argvars[0], &p1, &fnum, NULL, false) != OK
|
pos_T p1, p2;
|
||||||
|| (fnum >= 0 && fnum != curbuf->b_fnum)) {
|
if (list2fpos(&argvars[0], &p1, &fnum1, NULL, false) != OK
|
||||||
return;
|
|| list2fpos(&argvars[1], &p2, &fnum2, NULL, false) != OK
|
||||||
}
|
|| fnum1 != fnum2) {
|
||||||
|
|
||||||
pos_T p2;
|
|
||||||
if (list2fpos(&argvars[1], &p2, &fnum, NULL, false) != OK
|
|
||||||
|| (fnum >= 0 && fnum != curbuf->b_fnum)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2866,6 +2862,18 @@ static void f_getregion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buf_T *save_curbuf = curbuf;
|
||||||
|
|
||||||
|
if (fnum1 != 0) {
|
||||||
|
buf_T *findbuf = buflist_findnr(fnum1);
|
||||||
|
// buffer not loaded
|
||||||
|
if (findbuf == NULL || findbuf->b_ml.ml_mfp == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
save_curbuf = curbuf;
|
||||||
|
curbuf = findbuf;
|
||||||
|
}
|
||||||
|
|
||||||
const TriState save_virtual = virtual_op;
|
const TriState save_virtual = virtual_op;
|
||||||
virtual_op = virtual_active();
|
virtual_op = virtual_active();
|
||||||
|
|
||||||
@@ -2948,6 +2956,10 @@ static void f_getregion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
|||||||
tv_list_append_allocated_string(rettv->vval.v_list, akt);
|
tv_list_append_allocated_string(rettv->vval.v_list, akt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (curbuf != save_curbuf) {
|
||||||
|
curbuf = save_curbuf;
|
||||||
|
}
|
||||||
|
|
||||||
virtual_op = save_virtual;
|
virtual_op = save_virtual;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1751,7 +1751,7 @@ func Test_visual_getregion()
|
|||||||
#" using the wrong type
|
#" using the wrong type
|
||||||
call assert_fails(':echo "."->getpos()->getregion("$", [])', 'E1211:')
|
call assert_fails(':echo "."->getpos()->getregion("$", [])', 'E1211:')
|
||||||
|
|
||||||
#" using a mark in another buffer
|
#" using a mark from another buffer to current buffer
|
||||||
new
|
new
|
||||||
VAR newbuf = bufnr()
|
VAR newbuf = bufnr()
|
||||||
call setline(1, range(10))
|
call setline(1, range(10))
|
||||||
@@ -1761,6 +1761,20 @@ func Test_visual_getregion()
|
|||||||
call assert_equal([], getregion(getpos('.'), getpos("'A"), {'type': 'v' }))
|
call assert_equal([], getregion(getpos('.'), getpos("'A"), {'type': 'v' }))
|
||||||
call assert_equal([], getregion(getpos("'A"), getpos('.'), {'type': 'v' }))
|
call assert_equal([], getregion(getpos("'A"), getpos('.'), {'type': 'v' }))
|
||||||
exe $':{newbuf}bwipe!'
|
exe $':{newbuf}bwipe!'
|
||||||
|
|
||||||
|
#" using a mark from another buffer to another buffer
|
||||||
|
new
|
||||||
|
VAR anotherbuf = bufnr()
|
||||||
|
call setline(1, range(10))
|
||||||
|
normal! GmA
|
||||||
|
normal! GmB
|
||||||
|
wincmd p
|
||||||
|
call assert_equal([anotherbuf, 10, 1, 0], getpos("'A"))
|
||||||
|
call assert_equal(['9'], getregion(getpos("'B"), getpos("'A"), {'type': 'v' }))
|
||||||
|
exe $':{anotherbuf}bwipe!'
|
||||||
|
|
||||||
|
#" using invalid buffer
|
||||||
|
call assert_equal([], getregion([10000, 10, 1, 0], [10000, 10, 1, 0]))
|
||||||
END
|
END
|
||||||
call CheckLegacyAndVim9Success(lines)
|
call CheckLegacyAndVim9Success(lines)
|
||||||
|
|
||||||
@@ -1911,4 +1925,18 @@ func Test_visual_getregion()
|
|||||||
call CheckLegacyAndVim9Success(lines)
|
call CheckLegacyAndVim9Success(lines)
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_getregion_invalid_buf()
|
||||||
|
new
|
||||||
|
help
|
||||||
|
call cursor(5, 7)
|
||||||
|
norm! mA
|
||||||
|
call cursor(5, 18)
|
||||||
|
norm! mB
|
||||||
|
call assert_equal(['Move around:'], getregion(getpos("'A"), getpos("'B")))
|
||||||
|
" close the help window
|
||||||
|
q
|
||||||
|
call assert_equal([], getregion(getpos("'A"), getpos("'B")))
|
||||||
|
bwipe!
|
||||||
|
endfunc
|
||||||
|
|
||||||
" vim: shiftwidth=2 sts=2 expandtab
|
" vim: shiftwidth=2 sts=2 expandtab
|
||||||
|
Reference in New Issue
Block a user