Skip to content

Commit 868b7b6

Browse files
committed
patch 8.1.1418: win_execute() is not implemented yet
Problem: Win_execute() is not implemented yet. Solution: Implement it.
1 parent 1bbebab commit 868b7b6

File tree

6 files changed

+86
-15
lines changed

6 files changed

+86
-15
lines changed

runtime/doc/eval.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,8 @@ values({dict}) List values in {dict}
27392739
virtcol({expr}) Number screen column of cursor or mark
27402740
visualmode([expr]) String last visual mode used
27412741
wildmenumode() Number whether 'wildmenu' mode is active
2742+
win_execute({id}, {command} [, {silent}])
2743+
String execute {command} in window {id}
27422744
win_findbuf({bufnr}) List find windows containing {bufnr}
27432745
win_getid([{win} [, {tab}]]) Number get window ID for {win} in {tab}
27442746
win_gotoid({expr}) Number go to window with ID {expr}
@@ -4012,7 +4014,10 @@ execute({command} [, {silent}]) *execute()*
40124014
To get a list of lines use |split()| on the result: >
40134015
split(execute('args'), "\n")
40144016
4015-
< When used recursively the output of the recursive call is not
4017+
< To execute a command in another window than the current one
4018+
use `win_execute()`.
4019+
4020+
When used recursively the output of the recursive call is not
40164021
included in the output of the higher level call.
40174022

40184023
exepath({expr}) *exepath()*
@@ -10310,6 +10315,12 @@ wildmenumode() *wildmenumode()*
1031010315
<
1031110316
(Note, this needs the 'wildcharm' option set appropriately).
1031210317

10318+
win_execute({id}, {command} [, {silent}]) *win_execute()*
10319+
Like `execute()` but in the context of window {id}.
10320+
The window will temporarily be made the current window,
10321+
without triggering autocommands.
10322+
Example: >
10323+
call win_execute(winid, 'syntax enable')
1031310324
1031410325
win_findbuf({bufnr}) *win_findbuf()*
1031510326
Returns a list with |window-ID|s for windows that contain

runtime/doc/popup.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ By default the 'wrap' option is set, so that no text disappears. However, if
7070
there is not enough space, some text may be invisible.
7171

7272

73+
7374
TODO:
7475

7576
Example how to use syntax highlighting of a code snippet.
@@ -242,14 +243,6 @@ popup_getposition({id}) *popup_getposition()*
242243
positioning mechanism applied.
243244
If popup window {id} is not found an empty Dict is returned.
244245

245-
win_execute({id}, {command})
246-
{not implemented yet}
247-
Like `execute()` but in the context of window {id}.
248-
The window will temporarily be made the current window,
249-
without triggering autocommands.
250-
Example: >
251-
call win_execute(winid, 'syntax enable')
252-
<
253246

254247
*:popupclear* *:popupc*
255248
:popupc[lear] Emergency solution to a misbehaving plugin: close all popup
@@ -274,6 +267,10 @@ better leave them alone.
274267

275268
The window does have a cursor position, but the cursor is not displayed.
276269

270+
To execute a command in the context of the popup window and buffer use
271+
`win_execute()`. Example: >
272+
call win_execute(winid, 'syntax enable')
273+
277274
Options can be set on the window with `setwinvar()`, e.g.: >
278275
call setwinvar(winid, '&wrap', 0)
279276
And options can be set on the buffer with `setbufvar()`, e.g.: >

src/evalfunc.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ static void f_values(typval_T *argvars, typval_T *rettv);
492492
static void f_virtcol(typval_T *argvars, typval_T *rettv);
493493
static void f_visualmode(typval_T *argvars, typval_T *rettv);
494494
static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
495+
static void f_win_execute(typval_T *argvars, typval_T *rettv);
495496
static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
496497
static void f_win_getid(typval_T *argvars, typval_T *rettv);
497498
static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
@@ -1045,6 +1046,7 @@ static struct fst
10451046
{"virtcol", 1, 1, f_virtcol},
10461047
{"visualmode", 0, 1, f_visualmode},
10471048
{"wildmenumode", 0, 0, f_wildmenumode},
1049+
{"win_execute", 2, 3, f_win_execute},
10481050
{"win_findbuf", 1, 1, f_win_findbuf},
10491051
{"win_getid", 0, 2, f_win_getid},
10501052
{"win_gotoid", 1, 1, f_win_gotoid},
@@ -3519,7 +3521,7 @@ get_list_line(
35193521
* "execute()" function
35203522
*/
35213523
static void
3522-
f_execute(typval_T *argvars, typval_T *rettv)
3524+
execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
35233525
{
35243526
char_u *cmd = NULL;
35253527
list_T *list = NULL;
@@ -3535,25 +3537,25 @@ f_execute(typval_T *argvars, typval_T *rettv)
35353537
rettv->vval.v_string = NULL;
35363538
rettv->v_type = VAR_STRING;
35373539

3538-
if (argvars[0].v_type == VAR_LIST)
3540+
if (argvars[arg_off].v_type == VAR_LIST)
35393541
{
3540-
list = argvars[0].vval.v_list;
3542+
list = argvars[arg_off].vval.v_list;
35413543
if (list == NULL || list->lv_first == NULL)
35423544
/* empty list, no commands, empty output */
35433545
return;
35443546
++list->lv_refcount;
35453547
}
35463548
else
35473549
{
3548-
cmd = tv_get_string_chk(&argvars[0]);
3550+
cmd = tv_get_string_chk(&argvars[arg_off]);
35493551
if (cmd == NULL)
35503552
return;
35513553
}
35523554

3553-
if (argvars[1].v_type != VAR_UNKNOWN)
3555+
if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
35543556
{
35553557
char_u buf[NUMBUFLEN];
3556-
char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
3558+
char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
35573559

35583560
if (s == NULL)
35593561
return;
@@ -3620,6 +3622,15 @@ f_execute(typval_T *argvars, typval_T *rettv)
36203622
msg_col = save_msg_col;
36213623
}
36223624

3625+
/*
3626+
* "execute()" function
3627+
*/
3628+
static void
3629+
f_execute(typval_T *argvars, typval_T *rettv)
3630+
{
3631+
execute_common(argvars, rettv, 0);
3632+
}
3633+
36233634
/*
36243635
* "exepath()" function
36253636
*/
@@ -6096,6 +6107,30 @@ f_getwininfo(typval_T *argvars, typval_T *rettv)
60966107
}
60976108
}
60986109

6110+
/*
6111+
* "win_execute()" function
6112+
*/
6113+
static void
6114+
f_win_execute(typval_T *argvars, typval_T *rettv)
6115+
{
6116+
int id = (int)tv_get_number(argvars);
6117+
win_T *wp = win_id2wp(id);
6118+
win_T *save_curwin = curwin;
6119+
6120+
if (wp != NULL)
6121+
{
6122+
curwin = wp;
6123+
curbuf = curwin->w_buffer;
6124+
check_cursor();
6125+
execute_common(argvars, rettv, 1);
6126+
if (win_valid(save_curwin))
6127+
{
6128+
curwin = save_curwin;
6129+
curbuf = curwin->w_buffer;
6130+
}
6131+
}
6132+
}
6133+
60996134
/*
61006135
* "win_findbuf()" function
61016136
*/

src/popupwin.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ f_popup_create(typval_T *argvars, typval_T *rettv)
238238
buf->b_p_ul = -1; // no undo
239239
buf->b_p_swf = FALSE; // no swap file
240240
buf->b_p_bl = FALSE; // unlisted buffer
241+
buf->b_locked = TRUE;
241242

242243
win_init_popup_win(wp, buf);
243244

@@ -376,6 +377,7 @@ f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
376377
static void
377378
popup_free(win_T *wp)
378379
{
380+
wp->w_buffer->b_locked = FALSE;
379381
if (wp->w_winrow + wp->w_height >= cmdline_row)
380382
clear_cmdline = TRUE;
381383
win_free_popup(wp);

src/testdir/test_execute_func.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,27 @@ func Test_execute_not_silent()
7878
endfor
7979
call assert_equal('xyz ', text2)
8080
endfunc
81+
82+
func Test_win_execute()
83+
let thiswin = win_getid()
84+
new
85+
let otherwin = win_getid()
86+
call setline(1, 'the new window')
87+
call win_gotoid(thiswin)
88+
let line = win_execute(otherwin, 'echo getline(1)')
89+
call assert_match('the new window', line)
90+
91+
if has('textprop')
92+
let popupwin = popup_create('the popup win', {'line': 2, 'col': 3})
93+
redraw
94+
let line = win_execute(popupwin, 'echo getline(1)')
95+
call assert_match('the popup win', line)
96+
97+
call assert_fails('call win_execute(popupwin, "bwipe!")', 'E937:')
98+
99+
call popup_close(popupwin)
100+
endif
101+
102+
call win_gotoid(otherwin)
103+
bwipe!
104+
endfunc

src/version.c

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

768768
static int included_patches[] =
769769
{ /* Add new patch number below this line */
770+
/**/
771+
1418,
770772
/**/
771773
1417,
772774
/**/

0 commit comments

Comments
 (0)