Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read a list of people who contributed. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Implementation of popup windows. See ":help popup". |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #ifdef FEAT_TEXT_PROP |
| 17 | |
| 18 | /* |
| 19 | * Go through the options in "dict" and apply them to buffer "buf" displayed in |
| 20 | * popup window "wp". |
| 21 | */ |
| 22 | static void |
| 23 | apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict) |
| 24 | { |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame^] | 25 | int nr; |
| 26 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 27 | wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth"); |
| 28 | wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight"); |
| 29 | wp->w_winrow = dict_get_number(dict, (char_u *)"line"); |
| 30 | wp->w_wincol = dict_get_number(dict, (char_u *)"col"); |
| 31 | wp->w_zindex = dict_get_number(dict, (char_u *)"zindex"); |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame^] | 32 | |
| 33 | // Add timer to close the popup after some time. |
| 34 | nr = dict_get_number(dict, (char_u *)"time"); |
| 35 | if (nr > 0) |
| 36 | { |
| 37 | char_u cbbuf[50]; |
| 38 | char_u *ptr = cbbuf; |
| 39 | typval_T tv; |
| 40 | |
| 41 | vim_snprintf((char *)cbbuf, sizeof(cbbuf), |
| 42 | "{_ -> popup_close(%d)}", wp->w_id); |
| 43 | if (get_lambda_tv(&ptr, &tv, TRUE) == OK) |
| 44 | { |
| 45 | wp->w_popup_timer = create_timer(nr, 0); |
| 46 | wp->w_popup_timer->tr_callback = |
| 47 | vim_strsave(partial_name(tv.vval.v_partial)); |
| 48 | func_ref(wp->w_popup_timer->tr_callback); |
| 49 | wp->w_popup_timer->tr_partial = tv.vval.v_partial; |
| 50 | } |
| 51 | } |
| 52 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
| 56 | * popup_create({text}, {options}) |
| 57 | */ |
| 58 | void |
| 59 | f_popup_create(typval_T *argvars, typval_T *rettv) |
| 60 | { |
| 61 | win_T *wp; |
| 62 | buf_T *buf; |
| 63 | dict_T *d; |
| 64 | int nr; |
| 65 | |
| 66 | // Check arguments look OK. |
| 67 | if (!(argvars[0].v_type == VAR_STRING |
| 68 | && argvars[0].vval.v_string != NULL |
| 69 | && STRLEN(argvars[0].vval.v_string) > 0) |
| 70 | && !(argvars[0].v_type == VAR_LIST |
| 71 | && argvars[0].vval.v_list != NULL |
| 72 | && argvars[0].vval.v_list->lv_len > 0)) |
| 73 | { |
| 74 | emsg(_(e_listreq)); |
| 75 | return; |
| 76 | } |
| 77 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 78 | { |
| 79 | emsg(_(e_dictreq)); |
| 80 | return; |
| 81 | } |
| 82 | d = argvars[1].vval.v_dict; |
| 83 | |
| 84 | // Create the window and buffer. |
| 85 | wp = win_alloc_popup_win(); |
| 86 | if (wp == NULL) |
| 87 | return; |
| 88 | rettv->vval.v_number = wp->w_id; |
| 89 | wp->w_p_wrap = TRUE; // 'wrap' is default on |
| 90 | |
| 91 | buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY); |
| 92 | if (buf == NULL) |
| 93 | return; |
| 94 | ml_open(buf); |
| 95 | curbuf = buf; |
| 96 | set_string_option_direct((char_u *)"buftype", -1, |
| 97 | (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0); |
| 98 | set_string_option_direct((char_u *)"bufhidden", -1, |
| 99 | (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0); |
| 100 | curbuf = curwin->w_buffer; |
| 101 | buf->b_p_ul = -1; // no undo |
| 102 | buf->b_p_swf = FALSE; // no swap file |
| 103 | buf->b_p_bl = FALSE; // unlisted buffer |
| 104 | |
| 105 | win_init_popup_win(wp, buf); |
| 106 | |
| 107 | nr = (int)dict_get_number(d, (char_u *)"tab"); |
| 108 | if (nr == 0) |
| 109 | { |
| 110 | // popup on current tab |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 111 | wp->w_next = curtab->tp_first_popupwin; |
| 112 | curtab->tp_first_popupwin = wp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 113 | } |
| 114 | else if (nr < 0) |
| 115 | { |
| 116 | // global popup |
| 117 | wp->w_next = first_popupwin; |
| 118 | first_popupwin = wp; |
| 119 | } |
| 120 | else |
| 121 | // TODO: find tab page "nr" |
| 122 | emsg("Not implemented yet"); |
| 123 | |
| 124 | // Add text to the buffer. |
| 125 | if (argvars[0].v_type == VAR_STRING) |
| 126 | // just a string |
| 127 | ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE); |
| 128 | else if (argvars[0].vval.v_list->lv_first->li_tv.v_type == VAR_STRING) |
| 129 | { |
| 130 | listitem_T *li; |
| 131 | linenr_T lnum = 0; |
| 132 | char_u *p; |
| 133 | |
| 134 | // list of strings |
| 135 | for (li = argvars[0].vval.v_list->lv_first; li != NULL; |
| 136 | li = li->li_next) |
| 137 | if (li->li_tv.v_type == VAR_STRING) |
| 138 | { |
| 139 | p = li->li_tv.vval.v_string; |
| 140 | ml_append_buf(buf, lnum++, |
| 141 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 142 | } |
| 143 | } |
| 144 | else |
| 145 | // TODO: handle a list of dictionaries |
| 146 | emsg("Not implemented yet"); |
| 147 | |
| 148 | // Delete the line of the empty buffer. |
| 149 | curbuf = buf; |
| 150 | ml_delete(buf->b_ml.ml_line_count, FALSE); |
| 151 | curbuf = curwin->w_buffer; |
| 152 | |
| 153 | // Deal with options. |
| 154 | apply_options(wp, buf, argvars[1].vval.v_dict); |
| 155 | |
| 156 | // set default values |
| 157 | if (wp->w_zindex == 0) |
| 158 | wp->w_zindex = 50; |
| 159 | |
| 160 | // TODO: Compute the size and position properly. |
| 161 | |
| 162 | // Default position is in middle of the screen, assuming a small popup |
| 163 | if (wp->w_winrow == 0) |
| 164 | wp->w_winrow = Rows > 5 ? Rows / 2 - 2 : 0; |
| 165 | else |
| 166 | --wp->w_winrow; // option value is one-based |
| 167 | if (wp->w_wincol == 0) |
| 168 | wp->w_wincol = Columns > 20 ? Columns / 2 - 10 : 0; |
| 169 | else |
| 170 | --wp->w_wincol; // option value is one-based |
| 171 | |
| 172 | |
| 173 | // TODO: set width based on longest text line and the 'wrap' option |
| 174 | wp->w_width = wp->w_maxwidth == 0 ? 20 : wp->w_maxwidth; |
| 175 | if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth) |
| 176 | wp->w_width = wp->w_maxwidth; |
| 177 | if (wp->w_width > Columns - wp->w_wincol) |
| 178 | wp->w_width = Columns - wp->w_wincol; |
| 179 | |
| 180 | // TODO: adjust height for wrapped lines |
| 181 | wp->w_height = buf->b_ml.ml_line_count; |
| 182 | if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight) |
| 183 | wp->w_height = wp->w_maxheight; |
| 184 | if (wp->w_height > Rows - wp->w_winrow) |
| 185 | wp->w_height = Rows - wp->w_winrow; |
| 186 | |
| 187 | wp->w_vsep_width = 0; |
| 188 | |
| 189 | redraw_all_later(NOT_VALID); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * popup_close({id}) |
| 194 | */ |
| 195 | void |
| 196 | f_popup_close(typval_T *argvars, typval_T *rettv UNUSED) |
| 197 | { |
| 198 | int nr = (int)tv_get_number(argvars); |
| 199 | |
| 200 | popup_close(nr); |
| 201 | } |
| 202 | |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame^] | 203 | static void |
| 204 | popup_undisplay(win_T *wp) |
| 205 | { |
| 206 | if (wp->w_winrow + wp->w_height >= cmdline_row) |
| 207 | clear_cmdline = TRUE; |
| 208 | win_free_popup(wp); |
| 209 | redraw_all_later(NOT_VALID); |
| 210 | } |
| 211 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 212 | /* |
| 213 | * Close a popup window by Window-id. |
| 214 | */ |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 215 | void |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 216 | popup_close(int id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 217 | { |
| 218 | win_T *wp; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 219 | tabpage_T *tp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 220 | win_T *prev = NULL; |
| 221 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 222 | // go through global popups |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 223 | for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next) |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 224 | if (wp->w_id == id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 225 | { |
| 226 | if (prev == NULL) |
| 227 | first_popupwin = wp->w_next; |
| 228 | else |
| 229 | prev->w_next = wp->w_next; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame^] | 230 | popup_undisplay(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 231 | return; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 234 | // go through tab-local popups |
| 235 | FOR_ALL_TABPAGES(tp) |
| 236 | popup_close_tabpage(tp, id); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Close a popup window with Window-id "id" in tabpage "tp". |
| 241 | */ |
| 242 | void |
| 243 | popup_close_tabpage(tabpage_T *tp, int id) |
| 244 | { |
| 245 | win_T *wp; |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 246 | win_T **root = &tp->tp_first_popupwin; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 247 | win_T *prev = NULL; |
| 248 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 249 | for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next) |
| 250 | if (wp->w_id == id) |
| 251 | { |
| 252 | if (prev == NULL) |
| 253 | *root = wp->w_next; |
| 254 | else |
| 255 | prev->w_next = wp->w_next; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame^] | 256 | popup_undisplay(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 257 | return; |
| 258 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void |
| 262 | close_all_popups(void) |
| 263 | { |
| 264 | while (first_popupwin != NULL) |
| 265 | popup_close(first_popupwin->w_id); |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 266 | while (curtab->tp_first_popupwin != NULL) |
| 267 | popup_close(curtab->tp_first_popupwin->w_id); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void |
| 271 | ex_popupclear(exarg_T *eap UNUSED) |
| 272 | { |
| 273 | close_all_popups(); |
| 274 | } |
| 275 | |
| 276 | #endif // FEAT_TEXT_PROP |