Mercurial > vim
diff src/popupwin.c @ 17107:0001d10a7661 v8.1.1553
patch 8.1.1553: not easy to change the text in a popup window
commit https://github.com/vim/vim/commit/dc2ce58b5ac72e2af765385eb426660104816344
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jun 16 15:32:14 2019 +0200
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 https://github.com/vim/vim/issues/4549)
Also display a space for an empty popup.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 16 Jun 2019 15:45:05 +0200 |
parents | 1a23650f8da5 |
children | af861fccc309 |
line wrap: on
line diff
--- a/src/popupwin.c +++ b/src/popupwin.c @@ -601,8 +601,10 @@ popup_adjust_position(win_T *wp) 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 @@ typedef enum } 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 @@ popup_create(typval_T *argvars, typval_T // 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 @@ f_popup_show(typval_T *argvars, typval_T } } +/* + * 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) {