blob: 620ef0506231619c4aa58d9e37dde2334a290793 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* 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
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar4d784b22019-05-25 19:51:39 +020031/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020032 * Get option value for"key", which is "line" or "col".
33 * Handles "cursor+N" and "cursor-N".
34 */
35 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020036popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037{
38 dictitem_T *di;
39 char_u *val;
40 char_u *s;
41 char_u *endp;
42 int n = 0;
43
44 di = dict_find(dict, key, -1);
45 if (di == NULL)
46 return 0;
47
48 val = tv_get_string(&di->di_tv);
49 if (STRNCMP(val, "cursor", 6) != 0)
50 return dict_get_number(dict, key);
51
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
56 n = strtol((char *)s, (char **)&endp, 10);
57 if (endp != NULL && *skipwhite(endp) != NUL)
58 {
59 semsg(_(e_invexpr2), val);
60 return 0;
61 }
62 }
63
64 if (STRCMP(key, "line") == 0)
65 n = screen_screenrow() + 1 + n;
66 else // "col"
67 n = screen_screencol() + 1 + n;
68
69 if (n < 1)
70 n = 1;
71 return n;
72}
73
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020074 static void
75get_pos_options(win_T *wp, dict_T *dict)
76{
77 char_u *str;
78 int nr;
79
80 nr = popup_options_one(dict, (char_u *)"line");
81 if (nr > 0)
82 wp->w_wantline = nr;
83 nr = popup_options_one(dict, (char_u *)"col");
84 if (nr > 0)
85 wp->w_wantcol = nr;
86
87 str = dict_get_string(dict, (char_u *)"pos", FALSE);
88 if (str != NULL)
89 {
90 for (nr = 0;
91 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
92 ++nr)
93 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
94 {
95 wp->w_popup_pos = poppos_entries[nr].pp_val;
96 nr = -1;
97 break;
98 }
99 if (nr != -1)
100 semsg(_(e_invarg2), str);
101 }
102}
103
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200104/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200105 * Go through the options in "dict" and apply them to buffer "buf" displayed in
106 * popup window "wp".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200107 * When called from f_popup_atcursor() "atcursor" is TRUE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200108 */
109 static void
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200110apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict, int atcursor)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200111{
Bram Moolenaar402502d2019-05-30 22:07:36 +0200112 int nr;
113 char_u *str;
114 dictitem_T *di;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200115
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200116 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
117 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200118 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
119 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200120
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200121 if (atcursor)
122 {
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200123 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200124 setcursor_mayforce(TRUE);
125 wp->w_wantline = screen_screenrow();
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200126 if (wp->w_wantline == 0) // cursor in first line
127 {
128 wp->w_wantline = 2;
129 wp->w_popup_pos = POPPOS_TOPLEFT;
130 }
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200131 wp->w_wantcol = screen_screencol() + 1;
132 }
133
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200134 get_pos_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200135
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200136 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200137
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200138#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200139 // Add timer to close the popup after some time.
140 nr = dict_get_number(dict, (char_u *)"time");
141 if (nr > 0)
142 {
143 char_u cbbuf[50];
144 char_u *ptr = cbbuf;
145 typval_T tv;
146
147 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
148 "{_ -> popup_close(%d)}", wp->w_id);
149 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
150 {
151 wp->w_popup_timer = create_timer(nr, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200152 wp->w_popup_timer->tr_callback = get_callback(&tv);
153 clear_tv(&tv);
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200154 }
155 }
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200156#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200157
Bram Moolenaar402502d2019-05-30 22:07:36 +0200158 // Option values resulting in setting an option.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200159 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200160 if (str != NULL)
161 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
162 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200163
Bram Moolenaar402502d2019-05-30 22:07:36 +0200164 di = dict_find(dict, (char_u *)"wrap", -1);
165 if (di != NULL)
166 {
167 nr = dict_get_number(dict, (char_u *)"wrap");
168 wp->w_p_wrap = nr != 0;
169 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200170
171 di = dict_find(dict, (char_u *)"filter", -1);
172 if (di != NULL)
173 {
174 callback_T callback = get_callback(&di->di_tv);
175
176 if (callback.cb_name != NULL)
177 set_callback(&wp->w_filter_cb, &callback);
178 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200179}
180
181/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200182 * Add lines to the popup from a list of strings.
183 */
184 static void
185add_popup_strings(buf_T *buf, list_T *l)
186{
187 listitem_T *li;
188 linenr_T lnum = 0;
189 char_u *p;
190
191 for (li = l->lv_first; li != NULL; li = li->li_next)
192 if (li->li_tv.v_type == VAR_STRING)
193 {
194 p = li->li_tv.vval.v_string;
195 ml_append_buf(buf, lnum++,
196 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
197 }
198}
199
200/*
201 * Add lines to the popup from a list of dictionaries.
202 */
203 static void
204add_popup_dicts(buf_T *buf, list_T *l)
205{
206 listitem_T *li;
207 listitem_T *pli;
208 linenr_T lnum = 0;
209 char_u *p;
210 dict_T *dict;
211
212 // first add the text lines
213 for (li = l->lv_first; li != NULL; li = li->li_next)
214 {
215 if (li->li_tv.v_type != VAR_DICT)
216 {
217 emsg(_(e_dictreq));
218 return;
219 }
220 dict = li->li_tv.vval.v_dict;
221 p = dict == NULL ? NULL
222 : dict_get_string(dict, (char_u *)"text", FALSE);
223 ml_append_buf(buf, lnum++,
224 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
225 }
226
227 // add the text properties
228 lnum = 1;
229 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
230 {
231 dictitem_T *di;
232 list_T *plist;
233
234 dict = li->li_tv.vval.v_dict;
235 di = dict_find(dict, (char_u *)"props", -1);
236 if (di != NULL)
237 {
238 if (di->di_tv.v_type != VAR_LIST)
239 {
240 emsg(_(e_listreq));
241 return;
242 }
243 plist = di->di_tv.vval.v_list;
244 if (plist != NULL)
245 {
246 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
247 {
248 if (pli->li_tv.v_type != VAR_DICT)
249 {
250 emsg(_(e_dictreq));
251 return;
252 }
253 dict = pli->li_tv.vval.v_dict;
254 if (dict != NULL)
255 {
256 int col = dict_get_number(dict, (char_u *)"col");
257
258 prop_add_common( lnum, col, dict, buf, NULL);
259 }
260 }
261 }
262 }
263 }
264}
265
266/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200267 * Adjust the position and size of the popup to fit on the screen.
268 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200269 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200270popup_adjust_position(win_T *wp)
271{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200272 linenr_T lnum;
273 int wrapped = 0;
274 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200275 int center_vert = FALSE;
276 int center_hor = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200277
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200278 wp->w_winrow = 0;
279 wp->w_wincol = 0;
280 if (wp->w_popup_pos == POPPOS_CENTER)
281 {
282 // center after computing the size
283 center_vert = TRUE;
284 center_hor = TRUE;
285 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200286 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200287 {
288 if (wp->w_wantline == 0)
289 center_vert = TRUE;
290 else if (wp->w_popup_pos == POPPOS_TOPLEFT
291 || wp->w_popup_pos == POPPOS_TOPRIGHT)
292 {
293 wp->w_winrow = wp->w_wantline - 1;
294 if (wp->w_winrow >= Rows)
295 wp->w_winrow = Rows - 1;
296 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200297
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200298 if (wp->w_wantcol == 0)
299 center_hor = TRUE;
300 else if (wp->w_popup_pos == POPPOS_TOPLEFT
301 || wp->w_popup_pos == POPPOS_BOTLEFT)
302 {
303 wp->w_wincol = wp->w_wantcol - 1;
304 if (wp->w_wincol >= Columns - 3)
305 wp->w_wincol = Columns - 3;
306 }
307 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200308
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200309 // When centering or right aligned, use maximum width.
310 // When left aligned use the space available.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200311 maxwidth = Columns - wp->w_wincol;
312 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
313 maxwidth = wp->w_maxwidth;
314
315 // Compute width based on longest text line and the 'wrap' option.
316 // TODO: more accurate wrapping
317 wp->w_width = 0;
318 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
319 {
320 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
321
322 while (wp->w_p_wrap && len > maxwidth)
323 {
324 ++wrapped;
325 len -= maxwidth;
326 wp->w_width = maxwidth;
327 }
328 if (wp->w_width < len)
329 wp->w_width = len;
330 }
331
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200332 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
333 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200334 if (wp->w_width > maxwidth)
335 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200336 if (center_hor)
337 wp->w_wincol = (Columns - wp->w_width) / 2;
338 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
339 || wp->w_popup_pos == POPPOS_TOPRIGHT)
340 {
341 // Right aligned: move to the right if needed.
342 // No truncation, because that would change the height.
343 if (wp->w_width < wp->w_wantcol)
344 wp->w_wincol = wp->w_wantcol - wp->w_width;
345 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200346
347 if (wp->w_height <= 1)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200348 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200349 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
350 wp->w_height = wp->w_minheight;
351 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
352 wp->w_height = wp->w_maxheight;
353 if (wp->w_height > Rows - wp->w_winrow)
354 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200355
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200356 if (center_vert)
357 wp->w_winrow = (Rows - wp->w_height) / 2;
358 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
359 || wp->w_popup_pos == POPPOS_BOTLEFT)
360 {
361 if (wp->w_height <= wp->w_wantline)
362 // bottom aligned: may move down
363 wp->w_winrow = wp->w_wantline - wp->w_height;
364 else
365 // not enough space, make top aligned
366 wp->w_winrow = wp->w_wantline + 1;
367 }
368
Bram Moolenaar17146962019-05-30 00:12:11 +0200369 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200370}
371
372/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200373 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200374 * popup_atcursor({text}, {options})
375 * When called from f_popup_atcursor() "atcursor" is TRUE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200376 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200377 static void
378popup_create(typval_T *argvars, typval_T *rettv, int atcursor)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200379{
380 win_T *wp;
381 buf_T *buf;
382 dict_T *d;
383 int nr;
384
385 // Check arguments look OK.
386 if (!(argvars[0].v_type == VAR_STRING
387 && argvars[0].vval.v_string != NULL
388 && STRLEN(argvars[0].vval.v_string) > 0)
389 && !(argvars[0].v_type == VAR_LIST
390 && argvars[0].vval.v_list != NULL
391 && argvars[0].vval.v_list->lv_len > 0))
392 {
393 emsg(_(e_listreq));
394 return;
395 }
396 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
397 {
398 emsg(_(e_dictreq));
399 return;
400 }
401 d = argvars[1].vval.v_dict;
402
403 // Create the window and buffer.
404 wp = win_alloc_popup_win();
405 if (wp == NULL)
406 return;
407 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200408 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200409
410 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
411 if (buf == NULL)
412 return;
413 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200414
415 win_init_popup_win(wp, buf);
416
417 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200418 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200419 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200420 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200421 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200422 buf->b_p_ul = -1; // no undo
423 buf->b_p_swf = FALSE; // no swap file
424 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200425 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200426 wp->w_p_wrap = TRUE; // 'wrap' is default on
427
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200428 // Avoid that 'buftype' is reset when this buffer is entered.
429 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200430
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200431 nr = (int)dict_get_number(d, (char_u *)"tab");
432 if (nr == 0)
433 {
434 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200435 wp->w_next = curtab->tp_first_popupwin;
436 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200437 }
438 else if (nr < 0)
439 {
440 // global popup
441 wp->w_next = first_popupwin;
442 first_popupwin = wp;
443 }
444 else
445 // TODO: find tab page "nr"
446 emsg("Not implemented yet");
447
448 // Add text to the buffer.
449 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200450 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200451 // just a string
452 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200453 }
454 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200455 {
456 list_T *l = argvars[0].vval.v_list;
457
458 if (l->lv_first->li_tv.v_type == VAR_STRING)
459 // list of strings
460 add_popup_strings(buf, l);
461 else
462 // list of dictionaries
463 add_popup_dicts(buf, l);
464 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200465
466 // Delete the line of the empty buffer.
467 curbuf = buf;
468 ml_delete(buf->b_ml.ml_line_count, FALSE);
469 curbuf = curwin->w_buffer;
470
471 // Deal with options.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200472 apply_options(wp, buf, argvars[1].vval.v_dict, atcursor);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200473
474 // set default values
475 if (wp->w_zindex == 0)
476 wp->w_zindex = 50;
477
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200478 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200479
480 wp->w_vsep_width = 0;
481
482 redraw_all_later(NOT_VALID);
483}
484
485/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200486 * popup_create({text}, {options})
487 */
488 void
489f_popup_create(typval_T *argvars, typval_T *rettv)
490{
491 popup_create(argvars, rettv, FALSE);
492}
493
494/*
495 * popup_atcursor({text}, {options})
496 */
497 void
498f_popup_atcursor(typval_T *argvars, typval_T *rettv)
499{
500 popup_create(argvars, rettv, TRUE);
501}
502
503/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200504 * Find the popup window with window-ID "id".
505 * If the popup window does not exist NULL is returned.
506 * If the window is not a popup window, and error message is given.
507 */
508 static win_T *
509find_popup_win(int id)
510{
511 win_T *wp = win_id2wp(id);
512
513 if (wp != NULL && !bt_popup(wp->w_buffer))
514 {
515 semsg(_("E993: window %d is not a popup window"), id);
516 return NULL;
517 }
518 return wp;
519}
520
521/*
522 * Return TRUE if there any popups that are not hidden.
523 */
524 int
525popup_any_visible(void)
526{
527 win_T *wp;
528
529 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200530 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200531 return TRUE;
532 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200533 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200534 return TRUE;
535 return FALSE;
536}
537
538/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200539 * popup_close({id})
540 */
541 void
542f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
543{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200544 int id = (int)tv_get_number(argvars);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200545
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200546 popup_close(id);
547}
548
549/*
550 * popup_hide({id})
551 */
552 void
553f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
554{
555 int id = (int)tv_get_number(argvars);
556 win_T *wp = find_popup_win(id);
557
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200558 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200559 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200560 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200561 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200562 redraw_all_later(NOT_VALID);
563 }
564}
565
566/*
567 * popup_show({id})
568 */
569 void
570f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
571{
572 int id = (int)tv_get_number(argvars);
573 win_T *wp = find_popup_win(id);
574
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200575 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200576 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200577 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200578 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200579 redraw_all_later(NOT_VALID);
580 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200581}
582
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200583 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200584popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200585{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200586 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200587 if (wp->w_winrow + wp->w_height >= cmdline_row)
588 clear_cmdline = TRUE;
589 win_free_popup(wp);
590 redraw_all_later(NOT_VALID);
591}
592
Bram Moolenaarec583842019-05-26 14:11:23 +0200593/*
594 * Close a popup window by Window-id.
595 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200596 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200597popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200598{
599 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200600 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200601 win_T *prev = NULL;
602
Bram Moolenaarec583842019-05-26 14:11:23 +0200603 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200604 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200605 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200606 {
607 if (prev == NULL)
608 first_popupwin = wp->w_next;
609 else
610 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200611 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200612 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200613 }
614
Bram Moolenaarec583842019-05-26 14:11:23 +0200615 // go through tab-local popups
616 FOR_ALL_TABPAGES(tp)
617 popup_close_tabpage(tp, id);
618}
619
620/*
621 * Close a popup window with Window-id "id" in tabpage "tp".
622 */
623 void
624popup_close_tabpage(tabpage_T *tp, int id)
625{
626 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200627 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200628 win_T *prev = NULL;
629
Bram Moolenaarec583842019-05-26 14:11:23 +0200630 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
631 if (wp->w_id == id)
632 {
633 if (prev == NULL)
634 *root = wp->w_next;
635 else
636 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200637 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200638 return;
639 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200640}
641
642 void
643close_all_popups(void)
644{
645 while (first_popupwin != NULL)
646 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200647 while (curtab->tp_first_popupwin != NULL)
648 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200649}
650
651 void
652ex_popupclear(exarg_T *eap UNUSED)
653{
654 close_all_popups();
655}
656
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200657/*
658 * popup_move({id}, {options})
659 */
660 void
661f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
662{
663 dict_T *d;
664 int nr;
665 int id = (int)tv_get_number(argvars);
666 win_T *wp = find_popup_win(id);
667
668 if (wp == NULL)
669 return; // invalid {id}
670
671 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
672 {
673 emsg(_(e_dictreq));
674 return;
675 }
676 d = argvars[1].vval.v_dict;
677
678 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
679 wp->w_minwidth = nr;
680 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
681 wp->w_minheight = nr;
682 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
683 wp->w_maxwidth = nr;
684 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
685 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200686 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200687
688 if (wp->w_winrow + wp->w_height >= cmdline_row)
689 clear_cmdline = TRUE;
690 popup_adjust_position(wp);
691 redraw_all_later(NOT_VALID);
692}
693
Bram Moolenaarbc133542019-05-29 20:26:46 +0200694/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200695 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200696 */
697 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200698f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200699{
700 dict_T *dict;
701 int id = (int)tv_get_number(argvars);
702 win_T *wp = find_popup_win(id);
703
704 if (rettv_dict_alloc(rettv) == OK)
705 {
706 if (wp == NULL)
707 return; // invalid {id}
708 dict = rettv->vval.v_dict;
709 dict_add_number(dict, "line", wp->w_winrow + 1);
710 dict_add_number(dict, "col", wp->w_wincol + 1);
711 dict_add_number(dict, "width", wp->w_width);
712 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200713 dict_add_number(dict, "visible",
714 (wp->w_popup_flags & POPF_HIDDEN) == 0);
715 }
716}
717
718/*
719 * f_popup_getoptions({id})
720 */
721 void
722f_popup_getoptions(typval_T *argvars, typval_T *rettv)
723{
724 dict_T *dict;
725 int id = (int)tv_get_number(argvars);
726 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200727 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200728
729 if (rettv_dict_alloc(rettv) == OK)
730 {
731 if (wp == NULL)
732 return;
733
734 dict = rettv->vval.v_dict;
735 dict_add_number(dict, "line", wp->w_wantline);
736 dict_add_number(dict, "col", wp->w_wantcol);
737 dict_add_number(dict, "minwidth", wp->w_minwidth);
738 dict_add_number(dict, "minheight", wp->w_minheight);
739 dict_add_number(dict, "maxheight", wp->w_maxheight);
740 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
741 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200742
743 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
744 ++i)
745 if (wp->w_popup_pos == poppos_entries[i].pp_val)
746 {
747 dict_add_string(dict, "pos",
748 (char_u *)poppos_entries[i].pp_name);
749 break;
750 }
751
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200752# if defined(FEAT_TIMERS)
753 dict_add_number(dict, "time", wp->w_popup_timer != NULL
754 ? (long)wp->w_popup_timer->tr_interval : 0L);
755# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +0200756 }
757}
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200758
759 int
760not_in_popup_window()
761{
762 if (bt_popup(curwin->w_buffer))
763 {
764 emsg(_("E994: Not allowed in a popup window"));
765 return TRUE;
766 }
767 return FALSE;
768}
769
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200770/*
771 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
772 * in the current tab.
773 */
774 void
775popup_reset_handled()
776{
777 win_T *wp;
778
779 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
780 wp->w_popup_flags &= ~POPF_HANDLED;
781 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
782 wp->w_popup_flags &= ~POPF_HANDLED;
783}
784
785/*
786 * Find the next visible popup where POPF_HANDLED is not set.
787 * Must have called popup_reset_handled() first.
788 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
789 * popup with the highest zindex.
790 */
791 win_T *
792find_next_popup(int lowest)
793{
794 win_T *wp;
795 win_T *found_wp;
796 int found_zindex;
797
798 found_zindex = lowest ? INT_MAX : 0;
799 found_wp = NULL;
800 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
801 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
802 && (lowest ? wp->w_zindex < found_zindex
803 : wp->w_zindex > found_zindex))
804 {
805 found_zindex = wp->w_zindex;
806 found_wp = wp;
807 }
808 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
809 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
810 && (lowest ? wp->w_zindex < found_zindex
811 : wp->w_zindex > found_zindex))
812 {
813 found_zindex = wp->w_zindex;
814 found_wp = wp;
815 }
816
817 if (found_wp != NULL)
818 found_wp->w_popup_flags |= POPF_HANDLED;
819 return found_wp;
820}
821
822/*
823 * Invoke the filter callback for window "wp" with typed character "c".
824 * Uses the global "mod_mask" for modifiers.
825 * Returns the return value of the filter.
826 * Careful: The filter may make "wp" invalid!
827 */
828 static int
829invoke_popup_filter(win_T *wp, int c)
830{
831 int res;
832 typval_T rettv;
833 int dummy;
834 typval_T argv[3];
835 char_u buf[NUMBUFLEN];
836
837 argv[0].v_type = VAR_NUMBER;
838 argv[0].vval.v_number = (varnumber_T)wp->w_id;
839
840 // Convert the number to a string, so that the function can use:
841 // if a:c == "\<F2>"
842 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
843 argv[1].v_type = VAR_STRING;
844 argv[1].vval.v_string = vim_strsave(buf);
845
846 argv[2].v_type = VAR_UNKNOWN;
847
848 call_callback(&wp->w_filter_cb, -1,
849 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
850 res = tv_get_number(&rettv);
851 vim_free(argv[1].vval.v_string);
852 clear_tv(&rettv);
853 return res;
854}
855
856/*
857 * Called when "c" was typed: invoke popup filter callbacks.
858 * Returns TRUE when the character was consumed,
859 */
860 int
861popup_do_filter(int c)
862{
863 int res = FALSE;
864 win_T *wp;
865
866 popup_reset_handled();
867
868 while (!res && (wp = find_next_popup(FALSE)) != NULL)
869 if (wp->w_filter_cb.cb_name != NULL)
870 res = invoke_popup_filter(wp, c);
871
872 return res;
873}
874
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200875#endif // FEAT_TEXT_PROP