Skip to content

Commit 3f54fd3

Browse files
committed
patch 8.0.1563: timeout of getwinposx() can be too short
Problem: Timeout of getwinposx() can be too short. (lilydjwg) Solution: Add getwinpos(). (closes #2689)
1 parent 71137fe commit 3f54fd3

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

runtime/doc/eval.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,8 +2192,9 @@ gettabvar({nr}, {varname} [, {def}])
21922192
gettabwinvar({tabnr}, {winnr}, {name} [, {def}])
21932193
any {name} in {winnr} in tab page {tabnr}
21942194
getwininfo([{winid}]) List list of windows
2195-
getwinposx() Number X coord in pixels of GUI Vim window
2196-
getwinposy() Number Y coord in pixels of GUI Vim window
2195+
getwinpos([{tmeout}]) List X and Y coord in pixels of the Vim window
2196+
getwinposx() Number X coord in pixels of the Vim window
2197+
getwinposy() Number Y coord in pixels of the Vim window
21972198
getwinvar({nr}, {varname} [, {def}])
21982199
any variable {varname} in window {nr}
21992200
glob({expr} [, {nosuf} [, {list} [, {alllinks}]]])
@@ -4878,16 +4879,24 @@ gettabwinvar({tabnr}, {winnr}, {varname} [, {def}]) *gettabwinvar()*
48784879
:let list_is_on = gettabwinvar(1, 2, '&list')
48794880
:echo "myvar = " . gettabwinvar(3, 1, 'myvar')
48804881
<
4882+
getwinpos([{timeout}]) *getwinpos()*
4883+
The result is a list with two numbers, the result of
4884+
getwinposx() and getwinposy() combined:
4885+
[x-pos, y-pos]
4886+
{timeout} can be used to specify how long to wait in msec for
4887+
a response from the terminal. When omitted 100 msec is used.
4888+
48814889
*getwinposx()*
48824890
getwinposx() The result is a Number, which is the X coordinate in pixels of
48834891
the left hand side of the GUI Vim window. Also works for an
4884-
xterm.
4892+
xterm (uses a timeout of 100 msec).
48854893
The result will be -1 if the information is not available.
48864894
The value can be used with `:winpos`.
48874895

48884896
*getwinposy()*
48894897
getwinposy() The result is a Number, which is the Y coordinate in pixels of
4890-
the top of the GUI Vim window. Also works for an xterm.
4898+
the top of the GUI Vim window. Also works for an xterm (uses
4899+
a timeout of 100 msec).
48914900
The result will be -1 if the information is not available.
48924901
The value can be used with `:winpos`.
48934902

src/evalfunc.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
197197
static void f_gettabvar(typval_T *argvars, typval_T *rettv);
198198
static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
199199
static void f_getwininfo(typval_T *argvars, typval_T *rettv);
200+
static void f_getwinpos(typval_T *argvars, typval_T *rettv);
200201
static void f_getwinposx(typval_T *argvars, typval_T *rettv);
201202
static void f_getwinposy(typval_T *argvars, typval_T *rettv);
202203
static void f_getwinvar(typval_T *argvars, typval_T *rettv);
@@ -641,6 +642,7 @@ static struct fst
641642
{"gettabvar", 2, 3, f_gettabvar},
642643
{"gettabwinvar", 3, 4, f_gettabwinvar},
643644
{"getwininfo", 0, 1, f_getwininfo},
645+
{"getwinpos", 0, 1, f_getwinpos},
644646
{"getwinposx", 0, 0, f_getwinposx},
645647
{"getwinposy", 0, 0, f_getwinposy},
646648
{"getwinvar", 2, 3, f_getwinvar},
@@ -5526,6 +5528,38 @@ f_win_screenpos(typval_T *argvars, typval_T *rettv)
55265528
list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
55275529
}
55285530

5531+
/*
5532+
* "getwinpos({timeout})" function
5533+
*/
5534+
static void
5535+
f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5536+
{
5537+
int x = -1;
5538+
int y = -1;
5539+
5540+
if (rettv_list_alloc(rettv) == FAIL)
5541+
return;
5542+
#ifdef FEAT_GUI
5543+
if (gui.in_use)
5544+
gui_mch_get_winpos(&x, &y);
5545+
# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5546+
else
5547+
# endif
5548+
#endif
5549+
#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5550+
{
5551+
varnumber_T timeout = 100;
5552+
5553+
if (argvars[0].v_type != VAR_UNKNOWN)
5554+
timeout = get_tv_number(&argvars[0]);
5555+
term_get_winpos(&x, &y, timeout);
5556+
}
5557+
#endif
5558+
list_append_number(rettv->vval.v_list, (varnumber_T)x);
5559+
list_append_number(rettv->vval.v_list, (varnumber_T)y);
5560+
}
5561+
5562+
55295563
/*
55305564
* "getwinposx()" function
55315565
*/
@@ -5547,7 +5581,7 @@ f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
55475581
{
55485582
int x, y;
55495583

5550-
if (term_get_winpos(&x, &y) == OK)
5584+
if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
55515585
rettv->vval.v_number = x;
55525586
}
55535587
#endif
@@ -5574,7 +5608,7 @@ f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
55745608
{
55755609
int x, y;
55765610

5577-
if (term_get_winpos(&x, &y) == OK)
5611+
if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
55785612
rettv->vval.v_number = y;
55795613
}
55805614
#endif

src/proto/term.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void term_cursor_right(int i);
2424
void term_append_lines(int line_count);
2525
void term_delete_lines(int line_count);
2626
void term_set_winpos(int x, int y);
27-
int term_get_winpos(int *x, int *y);
27+
int term_get_winpos(int *x, int *y, varnumber_T timeout);
2828
void term_set_winsize(int height, int width);
2929
void term_fg_color(int n);
3030
void term_bg_color(int n);

src/term.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,7 +2789,7 @@ static int waiting_for_winpos = FALSE;
27892789
* Returns OK or FAIL.
27902790
*/
27912791
int
2792-
term_get_winpos(int *x, int *y)
2792+
term_get_winpos(int *x, int *y, varnumber_T timeout)
27932793
{
27942794
int count = 0;
27952795

@@ -2801,8 +2801,8 @@ term_get_winpos(int *x, int *y)
28012801
OUT_STR(T_CGP);
28022802
out_flush();
28032803

2804-
/* Try reading the result for 100 msec. */
2805-
while (count++ < 10)
2804+
/* Try reading the result for "timeout" msec. */
2805+
while (count++ < timeout / 10)
28062806
{
28072807
(void)vpeekc_nomap();
28082808
if (winpos_x >= 0 && winpos_y >= 0)

src/version.c

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

779779
static int included_patches[] =
780780
{ /* Add new patch number below this line */
781+
/**/
782+
1563,
781783
/**/
782784
1562,
783785
/**/

0 commit comments

Comments
 (0)