Skip to content

Commit df9c6ca

Browse files
committed
patch 8.1.1713: highlighting cursor line only works with popup_menu()
Problem: Highlighting cursor line only works with popup_menu(). Solution: Add the "cursorline" property. (Naruhiko Nishino, closes #4671)
1 parent d6bcff4 commit df9c6ca

15 files changed

Lines changed: 339 additions & 29 deletions

runtime/doc/popup.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ popup_getoptions({id}) *popup_getoptions()*
303303
zero. When all values are one then an empty list is included.
304304

305305
"borderhighlight" is not included when all values are empty.
306-
"scrollbarhighlight" and "thumbhighlight" are onlu included
306+
"scrollbarhighlight" and "thumbhighlight" are only included
307307
when set.
308308

309309
"tabpage" will be -1 for a global popup, zero for a popup on
@@ -345,7 +345,7 @@ popup_hide({id}) *popup_hide()*
345345

346346

347347
popup_locate({row}, {col}) *popup_locate()*
348-
Return the |window-ID| of the popup at screen positoin {row}
348+
Return the |window-ID| of the popup at screen position {row}
349349
and {col}. If there are multiple popups the one with the
350350
highest zindex is returned. If there are no popups at this
351351
position then zero is returned.
@@ -362,6 +362,7 @@ popup_menu({what}, {options}) *popup_menu()*
362362
\ drag: 1,
363363
\ wrap: 0,
364364
\ border: [],
365+
\ cursorline: 1,
365366
\ padding: [0,1,0,1],
366367
\ filter: 'popup_filter_menu',
367368
\ })
@@ -429,6 +430,7 @@ popup_setoptions({id}, {options}) *popup_setoptions()*
429430
callback
430431
close
431432
drag
433+
cursorline
432434
filter
433435
firstline
434436
flip
@@ -598,6 +600,11 @@ The second argument of |popup_create()| is a dictionary with options:
598600
{start} or after {end}
599601
The popup also closes if the cursor moves to another
600602
line or to another window.
603+
cursorline non-zero: Highlight the cursor line. Also scrolls the
604+
text to show this line (only works properly
605+
when 'wrap' is off).
606+
zero: Do not highlight the cursor line.
607+
Default is zero, except for |popup_menu()|.
601608
filter A callback that can filter typed characters, see
602609
|popup-filter|.
603610
callback A callback that is called when the popup closes, e.g.
@@ -695,8 +702,8 @@ If the text does not fit in the popup a scrollbar is displayed on the right of
695702
the window. This can be disabled by setting the "scrollbar" option to zero.
696703
When the scrollbar is displayed mouse scroll events, while the mouse pointer
697704
is on the popup, will cause the text to scroll up or down as you would expect.
698-
A click in the upper halve of the scrollbar will scroll the text one line
699-
down. A click in the lower halve wil scroll the text one line up. However,
705+
A click in the upper half of the scrollbar will scroll the text one line
706+
down. A click in the lower half wil scroll the text one line up. However,
700707
this is limited so that the popup does not get smaller.
701708

702709

@@ -709,7 +716,7 @@ list has four numbers:
709716
leftmost, negative for counting from the right, -1 for
710717
rightmost
711718
endcol last column, like "col"
712-
line start line, positive for conting from the top, 1 for top,
719+
line start line, positive for counting from the top, 1 for top,
713720
negative for counting from the bottom, -1 for bottom
714721
endline end line, like "line"
715722

src/popupwin.c

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,34 @@ check_highlight(dict_T *dict, char *name, char_u **pval)
441441
}
442442
}
443443

444+
/*
445+
* Highlight the line with the cursor.
446+
* Also scrolls the text to put the cursor line in view.
447+
*/
448+
static void
449+
popup_highlight_curline(win_T *wp)
450+
{
451+
int id;
452+
char buf[100];
453+
454+
match_delete(wp, 1, FALSE);
455+
456+
if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
457+
{
458+
// Scroll to show the line with the cursor. This assumes lines don't
459+
// wrap.
460+
while (wp->w_topline + wp->w_height - 1 < wp->w_cursor.lnum)
461+
wp->w_topline++;
462+
while (wp->w_cursor.lnum < wp->w_topline)
463+
wp->w_topline--;
464+
465+
id = syn_name2id((char_u *)"PopupSelected");
466+
vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
467+
match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
468+
(char_u *)buf, 10, 1, NULL, NULL);
469+
}
470+
}
471+
444472
/*
445473
* Shared between popup_create() and f_popup_setoptions().
446474
*/
@@ -635,6 +663,20 @@ apply_general_options(win_T *wp, dict_T *dict)
635663
handle_moved_argument(wp, di, TRUE);
636664
}
637665

666+
di = dict_find(dict, (char_u *)"cursorline", -1);
667+
if (di != NULL)
668+
{
669+
if (di->di_tv.v_type == VAR_NUMBER)
670+
{
671+
if (di->di_tv.vval.v_number != 0)
672+
wp->w_popup_flags |= POPF_CURSORLINE;
673+
else
674+
wp->w_popup_flags &= ~POPF_CURSORLINE;
675+
}
676+
else
677+
semsg(_(e_invargval), "cursorline");
678+
}
679+
638680
di = dict_find(dict, (char_u *)"filter", -1);
639681
if (di != NULL)
640682
{
@@ -662,6 +704,7 @@ apply_general_options(win_T *wp, dict_T *dict)
662704

663705
/*
664706
* Go through the options in "dict" and apply them to popup window "wp".
707+
* Only used when creating a new popup window.
665708
*/
666709
static void
667710
apply_options(win_T *wp, dict_T *dict)
@@ -679,6 +722,7 @@ apply_options(win_T *wp, dict_T *dict)
679722
}
680723

681724
popup_mask_refresh = TRUE;
725+
popup_highlight_curline(wp);
682726
}
683727

684728
/*
@@ -1313,6 +1357,7 @@ popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
13131357
set_callback(&wp->w_filter_cb, &callback);
13141358

13151359
wp->w_p_wrap = 0;
1360+
wp->w_popup_flags |= POPF_CURSORLINE;
13161361
}
13171362

13181363
for (i = 0; i < 4; ++i)
@@ -1502,26 +1547,6 @@ filter_handle_drag(win_T *wp, int c, typval_T *rettv)
15021547
rettv->vval.v_number = 0;
15031548
}
15041549

1505-
static void
1506-
popup_highlight_curline(win_T *wp)
1507-
{
1508-
int id;
1509-
char buf[100];
1510-
1511-
match_delete(wp, 1, FALSE);
1512-
1513-
// Scroll to show the line with the cursor. This assumes lines don't wrap.
1514-
while (wp->w_topline + wp->w_height - 1 < wp->w_cursor.lnum)
1515-
wp->w_topline++;
1516-
while (wp->w_cursor.lnum < wp->w_topline)
1517-
wp->w_topline--;
1518-
1519-
id = syn_name2id((char_u *)"PopupSelected");
1520-
vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1521-
match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1522-
(char_u *)buf, 10, 1, NULL, NULL);
1523-
}
1524-
15251550
/*
15261551
* popup_filter_menu({text}, {options})
15271552
*/
@@ -1630,10 +1655,7 @@ f_popup_dialog(typval_T *argvars, typval_T *rettv)
16301655
void
16311656
f_popup_menu(typval_T *argvars, typval_T *rettv)
16321657
{
1633-
win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1634-
1635-
if (wp != NULL)
1636-
popup_highlight_curline(wp);
1658+
popup_create(argvars, rettv, TYPE_MENU);
16371659
}
16381660

16391661
/*
@@ -1858,6 +1880,7 @@ f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
18581880
if (old_firstline != wp->w_firstline)
18591881
redraw_win_later(wp, NOT_VALID);
18601882
popup_mask_refresh = TRUE;
1883+
popup_highlight_curline(wp);
18611884
popup_adjust_position(wp);
18621885
}
18631886

@@ -2047,6 +2070,7 @@ f_popup_getoptions(typval_T *argvars, typval_T *rettv)
20472070
dict_add_string(dict, "title", wp->w_popup_title);
20482071
dict_add_number(dict, "wrap", wp->w_p_wrap);
20492072
dict_add_number(dict, "drag", wp->w_popup_drag);
2073+
dict_add_number(dict, "cursorline", (wp->w_popup_flags & POPF_CURSORLINE) != 0);
20502074
dict_add_string(dict, "highlight", wp->w_p_wcr);
20512075
if (wp->w_scrollbar_highlight != NULL)
20522076
dict_add_string(dict, "scrollbarhighlight",
@@ -2181,6 +2205,7 @@ invoke_popup_filter(win_T *wp, int c)
21812205
int dummy;
21822206
typval_T argv[3];
21832207
char_u buf[NUMBUFLEN];
2208+
linenr_T old_lnum = wp->w_cursor.lnum;
21842209

21852210
// Emergency exit: CTRL-C closes the popup.
21862211
if (c == Ctrl_C)
@@ -2205,6 +2230,9 @@ invoke_popup_filter(win_T *wp, int c)
22052230
// NOTE: The callback might close the popup, thus make "wp" invalid.
22062231
call_callback(&wp->w_filter_cb, -1,
22072232
&rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2233+
if (old_lnum != wp->w_cursor.lnum)
2234+
popup_highlight_curline(wp);
2235+
22082236
res = tv_get_number(&rettv);
22092237
vim_free(argv[1].vval.v_string);
22102238
clear_tv(&rettv);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @73
4+
|~| @34|1+0#0000001#ffd7ff255@2| +0#4040ff13#ffffff0@35
5+
|~| @34|2+0#0000001#ffd7ff255@2| +0#4040ff13#ffffff0@35
6+
|~| @34|3+0#0000001#ffd7ff255@2| +0#4040ff13#ffffff0@35
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @73
4+
|~| @34|1+0#0000001#e0e0e08@2| +0#4040ff13#ffffff0@35
5+
|~| @34|2+0#0000001#ffd7ff255@2| +0#4040ff13#ffffff0@35
6+
|~| @34|3+0#0000001#ffd7ff255@2| +0#4040ff13#ffffff0@35
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @73
4+
|~| @32|╔+0#0000001#ffd7ff255|═@5|╗| +0#4040ff13#ffffff0@32
5+
|~| @32|║+0#0000001#ffd7ff255| |1@2| | +0#0000000#0000001|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
6+
|~| @32|║+0#0000001#ffd7ff255| |2@2| | +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
7+
|~| @32|╚+0#0000001#ffd7ff255|═@5|╝| +0#4040ff13#ffffff0@32
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @73
4+
|~| @32|╔+0#0000001#ffd7ff255|═@5|╗| +0#4040ff13#ffffff0@32
5+
|~| @32|║+0#0000001#ffd7ff255| |2@2| | +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
6+
|~| @32|║+0#0000001#ffd7ff255| |3@2| | +0#0000000#0000001|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
7+
|~| @32|╚+0#0000001#ffd7ff255|═@5|╝| +0#4040ff13#ffffff0@32
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @73
4+
|~| @32|╔+0#0000001#ffd7ff255|═@5|╗| +0#4040ff13#ffffff0@32
5+
|~| @32|║+0#0000001#ffd7ff255| |1+0&#e0e0e08@2| +0&#ffd7ff255| +0#0000000#0000001|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
6+
|~| @32|║+0#0000001#ffd7ff255| |2@2| | +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
7+
|~| @32|╚+0#0000001#ffd7ff255|═@5|╝| +0#4040ff13#ffffff0@32
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @73
4+
|~| @32|╔+0#0000001#ffd7ff255|═@5|╗| +0#4040ff13#ffffff0@32
5+
|~| @32|║+0#0000001#ffd7ff255| |2@2| | +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
6+
|~| @32|║+0#0000001#ffd7ff255| |3+0&#e0e0e08@2| +0&#ffd7ff255| +0#0000000#0000001|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
7+
|~| @32|╚+0#0000001#ffd7ff255|═@5|╝| +0#4040ff13#ffffff0@32
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @32|╔+0#0000001#ffd7ff255|═@5|╗| +0#4040ff13#ffffff0@32
4+
|~| @32|║+0#0000001#ffd7ff255| |1@2| | +0#0000000#0000001|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
5+
|~| @32|║+0#0000001#ffd7ff255| |2+0&#e0e0e08@2| +0&#ffd7ff255| +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
6+
|~| @32|║+0#0000001#ffd7ff255| |3@2| | +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
7+
|~| @32|╚+0#0000001#ffd7ff255|═@5|╝| +0#4040ff13#ffffff0@32
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @32|╔+0#0000001#ffd7ff255|═@5|╗| +0#4040ff13#ffffff0@32
4+
|~| @32|║+0#0000001#ffd7ff255| |1+0&#e0e0e08@2| +0&#ffd7ff255| +0#0000000#0000001|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
5+
|~| @32|║+0#0000001#ffd7ff255| |2@2| | +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
6+
|~| @32|║+0#0000001#ffd7ff255| |3@2| | +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@32
7+
|~| @32|╚+0#0000001#ffd7ff255|═@5|╝| +0#4040ff13#ffffff0@32
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|

0 commit comments

Comments
 (0)