Skip to content

Commit 8e0a8e7

Browse files
committed
patch 8.1.1967: line() only works for the current window
Problem: Line() only works for the current window. Solution: Add an optional argument for the window to use.
1 parent e677df8 commit 8e0a8e7

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/evalfunc.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ static funcentry_T global_functions[] =
634634
{"len", 1, 1, FEARG_1, f_len},
635635
{"libcall", 3, 3, FEARG_3, f_libcall},
636636
{"libcallnr", 3, 3, FEARG_3, f_libcallnr},
637-
{"line", 1, 1, FEARG_1, f_line},
637+
{"line", 1, 2, FEARG_1, f_line},
638638
{"line2byte", 1, 1, FEARG_1, f_line2byte},
639639
{"lispindent", 1, 1, FEARG_1, f_lispindent},
640640
{"list2str", 1, 2, FEARG_1, f_list2str},
@@ -1154,14 +1154,18 @@ tv_get_lnum(typval_T *argvars)
11541154
{
11551155
typval_T rettv;
11561156
linenr_T lnum;
1157+
int save_type;
11571158

11581159
lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
11591160
if (lnum == 0) /* no valid number, try using line() */
11601161
{
11611162
rettv.v_type = VAR_NUMBER;
1163+
save_type = argvars[1].v_type;
1164+
argvars[1].v_type = VAR_UNKNOWN;
11621165
f_line(argvars, &rettv);
11631166
lnum = (linenr_T)rettv.vval.v_number;
11641167
clear_tv(&rettv);
1168+
argvars[1].v_type = save_type;
11651169
}
11661170
return lnum;
11671171
}
@@ -6658,16 +6662,40 @@ f_libcallnr(typval_T *argvars, typval_T *rettv)
66586662
}
66596663

66606664
/*
6661-
* "line(string)" function
6665+
* "line(string, [winid])" function
66626666
*/
66636667
static void
66646668
f_line(typval_T *argvars, typval_T *rettv)
66656669
{
66666670
linenr_T lnum = 0;
6667-
pos_T *fp;
6671+
pos_T *fp = NULL;
66686672
int fnum;
6673+
int id;
6674+
tabpage_T *tp;
6675+
win_T *wp;
6676+
win_T *save_curwin;
6677+
tabpage_T *save_curtab;
6678+
6679+
if (argvars[1].v_type != VAR_UNKNOWN)
6680+
{
6681+
// use window specified in the second argument
6682+
id = (int)tv_get_number(&argvars[1]);
6683+
wp = win_id2wp_tp(id, &tp);
6684+
if (wp != NULL && tp != NULL)
6685+
{
6686+
if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6687+
== OK)
6688+
{
6689+
check_cursor();
6690+
fp = var2fpos(&argvars[0], TRUE, &fnum);
6691+
}
6692+
restore_win_noblock(save_curwin, save_curtab, TRUE);
6693+
}
6694+
}
6695+
else
6696+
// use current window
6697+
fp = var2fpos(&argvars[0], TRUE, &fnum);
66696698

6670-
fp = var2fpos(&argvars[0], TRUE, &fnum);
66716699
if (fp != NULL)
66726700
lnum = fp->lnum;
66736701
rettv->vval.v_number = lnum;

src/testdir/test_popupwin.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ func Test_popup_firstline()
346346
redraw
347347
call assert_equal(11, popup_getoptions(winid).firstline)
348348
call assert_equal(11, popup_getpos(winid).firstline)
349+
" check line() works with popup window
350+
call assert_equal(11, line('.', winid))
351+
call assert_equal(50, line('$', winid))
352+
call assert_equal(0, line('$', 123456))
349353

350354
" Normal command changes what is displayed but not "firstline"
351355
call win_execute(winid, "normal! \<c-y>")

src/version.c

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

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1967,
764766
/**/
765767
1966,
766768
/**/

0 commit comments

Comments
 (0)