Skip to content

Commit dc2ce58

Browse files
committed
patch 8.1.1553: not easy to change the text in a popup window
Problem: Not easy to change the text in a popup window. Solution: Add popup_settext(). (Ben Jackson, closes #4549) Also display a space for an empty popup.
1 parent f85e40a commit dc2ce58

12 files changed

Lines changed: 192 additions & 34 deletions

runtime/doc/popup.txt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,18 @@ TODO:
104104
incomplete cell.
105105
- Can the buffer be re-used, to avoid using up lots of buffer numbers?
106106
- Use a popup window for the "info" item of completion instead of using a
107-
preview window.
107+
preview window. Ideas in issue #4544.
108+
How to add highlighting?
109+
- When the lines do not fit show a scrollbar (like in the popup menu).
110+
Use the mouse wheel for scrolling.
108111
- Implement:
109112
popup_filter_menu({id}, {key})
110113
popup_menu({text}, {options})
111114
popup_setoptions({id}, {options})
112-
flip option
113115
hidden option
114116
tabpage option with number
115117
title option
118+
flip option
116119
transparent text property
117120

118121

@@ -134,6 +137,7 @@ Manipulating a popup window:
134137
|popup_show()| show a previously hidden popup
135138
|popup_move()| change the position and size of a popup
136139
|popup_setoptions()| override options of a popup
140+
|popup_settext()| replace the popup buffer contents
137141

138142
Closing popup windows:
139143
|popup_close()| close one popup
@@ -330,6 +334,11 @@ popup_setoptions({id}, {options}) *popup_setoptions()*
330334
{not implemented yet}
331335
Override options in popup {id} with entries in {options}.
332336

337+
popup_settext({id}, {text}) *popup_settext()*
338+
Set the text of the buffer in poup win {id}. {text} is the
339+
same as supplied to |popup_create()|.
340+
Does not change the window size or position, other than caused
341+
by the different text.
333342

334343

335344
POPUP BUFFER AND WINDOW *popup-buffer*
@@ -365,8 +374,9 @@ need them.
365374

366375
POPUP_CREATE() ARGUMENTS *popup_create-usage*
367376

368-
The first argument of |popup_create()| specifies the text to be displayed, and
369-
optionally text properties. It is in one of three forms:
377+
The first argument of |popup_create()| (and the second argument to
378+
|popup_setttext()|) specifies the text to be displayed, and optionally text
379+
properties. It is in one of three forms:
370380
- a string
371381
- a list of strings
372382
- a list of dictionaries, where each dictionary has these entries:
@@ -404,7 +414,9 @@ The second argument of |popup_create()| is a dictionary with options:
404414
flip When TRUE (the default) and the position is relative
405415
to the cursor, flip to below or above the cursor to
406416
avoid overlap with the |popupmenu-completion| or
407-
another popup with a higher "zindex".
417+
another popup with a higher "zindex". When there is
418+
no space above/below the cursor then show the popup to
419+
the side of the popup or popup menu.
408420
{not implemented yet}
409421
maxheight Maximum height of the contents, excluding border and
410422
padding.
@@ -434,7 +446,9 @@ The second argument of |popup_create()| is a dictionary with options:
434446
wrap TRUE to make the lines wrap (default TRUE).
435447
drag TRUE to allow the popup to be dragged with the mouse
436448
by grabbing at at the border. Has no effect if the
437-
popup does not have a border.
449+
popup does not have a border. As soon as dragging
450+
starts and "pos" is "center" it is changed to
451+
"topleft".
438452
highlight Highlight group name to use for the text, stored in
439453
the 'wincolor' option.
440454
padding List with numbers, defining the padding

src/evalfunc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ static struct fst
822822
{"popup_hide", 1, 1, f_popup_hide},
823823
{"popup_move", 2, 2, f_popup_move},
824824
{"popup_notification", 2, 2, f_popup_notification},
825+
{"popup_settext", 2, 2, f_popup_settext},
825826
{"popup_show", 1, 1, f_popup_show},
826827
#endif
827828
#ifdef FEAT_FLOAT

src/popupwin.c

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,10 @@ popup_adjust_position(win_T *wp)
601601
wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
602602

603603
// Compute width based on longest text line and the 'wrap' option.
604+
// Use a minimum width of one, so that something shows when there is no
605+
// text.
604606
// TODO: more accurate wrapping
605-
wp->w_width = 0;
607+
wp->w_width = 1;
606608
for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
607609
{
608610
int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
@@ -703,6 +705,48 @@ typedef enum
703705
TYPE_DIALOG
704706
} create_type_T;
705707

708+
/*
709+
* Make "buf" empty and set the contents to "text".
710+
* Used by popup_create() and popup_settext().
711+
*/
712+
static void
713+
popup_set_buffer_text(buf_T *buf, typval_T text)
714+
{
715+
int lnum;
716+
717+
// Clear the buffer, then replace the lines.
718+
curbuf = buf;
719+
for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
720+
ml_delete(lnum, FALSE);
721+
curbuf = curwin->w_buffer;
722+
723+
// Add text to the buffer.
724+
if (text.v_type == VAR_STRING)
725+
{
726+
// just a string
727+
ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
728+
}
729+
else
730+
{
731+
list_T *l = text.vval.v_list;
732+
733+
if (l->lv_len > 0)
734+
{
735+
if (l->lv_first->li_tv.v_type == VAR_STRING)
736+
// list of strings
737+
add_popup_strings(buf, l);
738+
else
739+
// list of dictionaries
740+
add_popup_dicts(buf, l);
741+
}
742+
}
743+
744+
// delete the line that was in the empty buffer
745+
curbuf = buf;
746+
ml_delete(buf->b_ml.ml_line_count, FALSE);
747+
curbuf = curwin->w_buffer;
748+
}
749+
706750
/*
707751
* popup_create({text}, {options})
708752
* popup_atcursor({text}, {options})
@@ -789,31 +833,7 @@ popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
789833
// TODO: find tab page "nr"
790834
emsg("Not implemented yet");
791835

792-
// Add text to the buffer.
793-
if (argvars[0].v_type == VAR_STRING)
794-
{
795-
// just a string
796-
ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
797-
}
798-
else
799-
{
800-
list_T *l = argvars[0].vval.v_list;
801-
802-
if (l->lv_len > 0)
803-
{
804-
if (l->lv_first->li_tv.v_type == VAR_STRING)
805-
// list of strings
806-
add_popup_strings(buf, l);
807-
else
808-
// list of dictionaries
809-
add_popup_dicts(buf, l);
810-
}
811-
}
812-
813-
// Delete the line of the empty buffer.
814-
curbuf = buf;
815-
ml_delete(buf->b_ml.ml_line_count, FALSE);
816-
curbuf = curwin->w_buffer;
836+
popup_set_buffer_text(buf, argvars[0]);
817837

818838
if (type == TYPE_ATCURSOR)
819839
{
@@ -1112,6 +1132,22 @@ f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
11121132
}
11131133
}
11141134

1135+
/*
1136+
* popup_settext({id}, {text})
1137+
*/
1138+
void
1139+
f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1140+
{
1141+
int id = (int)tv_get_number(&argvars[0]);
1142+
win_T *wp = find_popup_win(id);
1143+
1144+
if (wp != NULL)
1145+
{
1146+
popup_set_buffer_text(wp->w_buffer, argvars[1]);
1147+
popup_adjust_position(wp);
1148+
}
1149+
}
1150+
11151151
static void
11161152
popup_free(win_T *wp)
11171153
{

src/proto/popupwin.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void f_popup_notification(typval_T *argvars, typval_T *rettv);
1414
void f_popup_close(typval_T *argvars, typval_T *rettv);
1515
void f_popup_hide(typval_T *argvars, typval_T *rettv);
1616
void f_popup_show(typval_T *argvars, typval_T *rettv);
17+
void f_popup_settext(typval_T *argvars, typval_T *rettv);
1718
void popup_close(int id);
1819
void popup_close_tabpage(tabpage_T *tp, int id);
1920
void close_all_popups(void);
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+
|~| @73
5+
|~| @28|t+0#0000001#ffd7ff255|h|i|s| |i|s| |a| |t|e|x|t| +0#4040ff13#ffffff0@30
6+
|~| @73
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+
|~| @73
5+
|~| @35| +0#0000001#ffd7ff255| +0#4040ff13#ffffff0@36
6+
|~| @73
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
|:+0#0000000&|c|a|l@1| |p|o|p|u|p|_|s|e|t@1|e|x|t|(|p|,| |'@1|)| @30|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+
|~| @35|a+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@36
5+
|~| @35|b+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@36
6+
|~| @35|c+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@36
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
|:+0#0000000&|c|a|l@1| |p|o|p|u|p|_|s|e|t@1|e|x|t|(|p|,| |[|'|a|'|,|'|b|'|,|'|c|'|]|)| @19|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+
|~| @73
5+
|~| @35|a+0#0000001#ffd7ff255| +0#4040ff13#ffffff0@36
6+
|~| @73
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
|:+0#0000000&|c|a|l@1| |p|o|p|u|p|_|s|e|t@1|e|x|t|(|p|,| |[|'|a|'|]|)| @27|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+
|~| @73
5+
|~| @35| +0#0000001#ffd7ff255| +0#4040ff13#ffffff0@36
6+
|~| @73
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
|:+0#0000000&|c|a|l@1| |p|o|p|u|p|_|s|e|t@1|e|x|t|(|p|,| |[|]|)| @30|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+
|~| @33|a+0#0000001#ffd7ff255@3| +0#4040ff13#ffffff0@35
5+
|~| @33|b+0#0000001#ffd7ff255@3| +0#4040ff13#ffffff0@35
6+
|~| @33|c+0#0000001#ffd7ff255@3| +0#4040ff13#ffffff0@35
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
| +0#0000000&@56|0|,|0|-|1| @8|A|l@1|

0 commit comments

Comments
 (0)