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.
diff --git a/src/popupwin.c b/src/popupwin.c
index c0b5e13..99ef290 100644
--- a/src/popupwin.c
+++ b/src/popupwin.c
@@ -601,8 +601,10 @@
wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
// Compute width based on longest text line and the 'wrap' option.
+ // Use a minimum width of one, so that something shows when there is no
+ // text.
// TODO: more accurate wrapping
- wp->w_width = 0;
+ wp->w_width = 1;
for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
{
int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
@@ -704,6 +706,48 @@
} create_type_T;
/*
+ * Make "buf" empty and set the contents to "text".
+ * Used by popup_create() and popup_settext().
+ */
+ static void
+popup_set_buffer_text(buf_T *buf, typval_T text)
+{
+ int lnum;
+
+ // Clear the buffer, then replace the lines.
+ curbuf = buf;
+ for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
+ ml_delete(lnum, FALSE);
+ curbuf = curwin->w_buffer;
+
+ // Add text to the buffer.
+ if (text.v_type == VAR_STRING)
+ {
+ // just a string
+ ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
+ }
+ else
+ {
+ list_T *l = text.vval.v_list;
+
+ if (l->lv_len > 0)
+ {
+ if (l->lv_first->li_tv.v_type == VAR_STRING)
+ // list of strings
+ add_popup_strings(buf, l);
+ else
+ // list of dictionaries
+ add_popup_dicts(buf, l);
+ }
+ }
+
+ // delete the line that was in the empty buffer
+ curbuf = buf;
+ ml_delete(buf->b_ml.ml_line_count, FALSE);
+ curbuf = curwin->w_buffer;
+}
+
+/*
* popup_create({text}, {options})
* popup_atcursor({text}, {options})
*/
@@ -789,31 +833,7 @@
// TODO: find tab page "nr"
emsg("Not implemented yet");
- // Add text to the buffer.
- if (argvars[0].v_type == VAR_STRING)
- {
- // just a string
- ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
- }
- else
- {
- list_T *l = argvars[0].vval.v_list;
-
- if (l->lv_len > 0)
- {
- if (l->lv_first->li_tv.v_type == VAR_STRING)
- // list of strings
- add_popup_strings(buf, l);
- else
- // list of dictionaries
- add_popup_dicts(buf, l);
- }
- }
-
- // Delete the line of the empty buffer.
- curbuf = buf;
- ml_delete(buf->b_ml.ml_line_count, FALSE);
- curbuf = curwin->w_buffer;
+ popup_set_buffer_text(buf, argvars[0]);
if (type == TYPE_ATCURSOR)
{
@@ -1112,6 +1132,22 @@
}
}
+/*
+ * popup_settext({id}, {text})
+ */
+ void
+f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
+{
+ int id = (int)tv_get_number(&argvars[0]);
+ win_T *wp = find_popup_win(id);
+
+ if (wp != NULL)
+ {
+ popup_set_buffer_text(wp->w_buffer, argvars[1]);
+ popup_adjust_position(wp);
+ }
+}
+
static void
popup_free(win_T *wp)
{