Commit: patch 9.1.0155: can only get getregion() from current buffer

22 views
Skip to first unread message

Christian Brabandt

unread,
Mar 6, 2024, 3:45:10 PM3/6/24
to vim...@googlegroups.com
patch 9.1.0155: can only get getregion() from current buffer

Commit: https://github.com/vim/vim/commit/84bf6e658da51126bdd2e50af1f40cabd149343f
Author: Shougo Matsushita <Shougo...@gmail.com>
Date: Wed Mar 6 21:10:18 2024 +0100

patch 9.1.0155: can only get getregion() from current buffer

Problem: can only call getregion() for current buffer
Solution: Allow to retrieve selections from different buffers
(Shougo Matsushita)

closes: #14131

Co-authored-by: zeertzjq <zeer...@outlook.com>
Signed-off-by: Shougo Matsushita <Shougo...@gmail.com>
Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index 0d47e3653..09b4dd068 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -1,4 +1,4 @@
-*builtin.txt* For Vim version 9.1. Last change: 2024 Mar 03
+*builtin.txt* For Vim version 9.1. Last change: 2024 Mar 06


VIM REFERENCE MANUAL by Bram Moolenaar
@@ -4274,11 +4274,13 @@ getreginfo([{regname}]) *getreginfo()*
GetRegname()->getreginfo()

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.

{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
following items:
@@ -4299,6 +4301,7 @@ getregion({pos1}, {pos2} [, {opts}]) *getregion()*
This function is useful to get text starting and ending in
different columns, such as a |characterwise-visual| selection.

+ *getregion-notes*
Note that:
- Order of {pos1} and {pos2} doesn't matter, it will always
return content from the upper left position to the lower
@@ -4308,8 +4311,12 @@ getregion({pos1}, {pos2} [, {opts}]) *getregion()*
- If the region is blockwise and it starts or ends in the
middle of a multi-cell character, it is not included but
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.
+ - {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: >
:xnoremap <CR>
diff --git a/runtime/doc/tags b/runtime/doc/tags
index d6ed03a31..8dd4db241 100644
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -7767,6 +7767,7 @@ getqflist-examples quickfix.txt /*getqflist-examples*
getreg() builtin.txt /*getreg()*
getreginfo() builtin.txt /*getreginfo()*
getregion() builtin.txt /*getregion()*
+getregion-notes builtin.txt /*getregion-notes*
getregtype() builtin.txt /*getregtype()*
getscript pi_getscript.txt /*getscript*
getscript-autoinstall pi_getscript.txt /*getscript-autoinstall*
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 5d6664c9a..23d5dd386 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -5491,9 +5491,10 @@ f_getregion(typval_T *argvars, typval_T *rettv)
struct block_def bd;
char_u *akt = NULL;
int inclusive = TRUE;
- int fnum = -1;
+ int fnum1 = -1, fnum2 = -1;
pos_T p1, p2;
char_u *type;
+ buf_T *save_curbuf = curbuf;
char_u default_type[] = "v";
int save_virtual = -1;
int l;
@@ -5508,12 +5509,9 @@ f_getregion(typval_T *argvars, typval_T *rettv)
|| check_for_opt_dict_arg(argvars, 2) == FAIL)
return;

- if (list2fpos(&argvars[0], &p1, &fnum, NULL, FALSE) != OK
- || (fnum >= 0 && fnum != curbuf->b_fnum))
- return;
-
- if (list2fpos(&argvars[1], &p2, &fnum, NULL, FALSE) != OK
- || (fnum >= 0 && fnum != curbuf->b_fnum))
+ if (list2fpos(&argvars[0], &p1, &fnum1, NULL, FALSE) != OK
+ || list2fpos(&argvars[1], &p2, &fnum2, NULL, FALSE) != OK
+ || fnum1 != fnum2)
return;

if (argvars[2].v_type == VAR_DICT)
@@ -5540,6 +5538,18 @@ f_getregion(typval_T *argvars, typval_T *rettv)
else
return;

+ if (fnum1 != 0)
+ {
+ buf_T *findbuf;
+
+ findbuf = buflist_findnr(fnum1);
+ // buffer not loaded
+ if (findbuf == NULL || findbuf->b_ml.ml_mfp == NULL)
+ return;
+ save_curbuf = curbuf;
+ curbuf = findbuf;
+ }
+
save_virtual = virtual_op;
virtual_op = virtual_active();

@@ -5642,6 +5652,9 @@ f_getregion(typval_T *argvars, typval_T *rettv)
}
}

+ if (curbuf != save_curbuf)
+ curbuf = save_curbuf;
+
virtual_op = save_virtual;
}

diff --git a/src/testdir/test_visual.vim b/src/testdir/test_visual.vim
index 795f63e49..b80f43eed 100644
--- a/src/testdir/test_visual.vim
+++ b/src/testdir/test_visual.vim
@@ -1747,7 +1747,7 @@ func Test_visual_getregion()
#" using the wrong type
call assert_fails(':echo "."->getpos()->getregion("$", [])', 'E1211:')

- #" using a mark in another buffer
+ #" using a mark from another buffer to current buffer
new
VAR newbuf = bufnr()
call setline(1, range(10))
@@ -1757,6 +1757,20 @@ func Test_visual_getregion()
call assert_equal([], getregion(getpos('.'), getpos("'A"), {'type': 'v' }))
call assert_equal([], getregion(getpos("'A"), getpos('.'), {'type': 'v' }))
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
call v9.CheckLegacyAndVim9Success(lines)

@@ -1907,4 +1921,18 @@ func Test_visual_getregion()
call v9.CheckLegacyAndVim9Success(lines)
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
diff --git a/src/version.c b/src/version.c
index 0e02ea2aa..f1af6cbb9 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 155,
/**/
154,
/**/

Christ van Willegen

unread,
Mar 7, 2024, 6:39:50 AM3/7/24
to vim...@googlegroups.com
Typo comment:

+ - {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

*this makes a difference

Christ van Willegen

Christian Brabandt

unread,
Mar 7, 2024, 7:52:43 AM3/7/24
to vim...@googlegroups.com
Yes thanks. I think this is part of PR: 14154

Thanks,
Christian
--
Life is a gamble at terrible odds, if it was a bet you wouldn't take it.
-- Tom Stoppard, "Rosencrantz and Guildenstern are Dead"
Reply all
Reply to author
Forward
0 new messages