blob: 099a0c26a339c9a4d18201707244ef1495d26e30 [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 Moolenaarb0ebbda2019-06-02 16:51:21 +020032 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020033 * 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)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020050 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020051
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020056 endp = s;
57 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
58 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059 if (endp != NULL && *skipwhite(endp) != NUL)
60 {
61 semsg(_(e_invexpr2), val);
62 return 0;
63 }
64 }
65
66 if (STRCMP(key, "line") == 0)
67 n = screen_screenrow() + 1 + n;
68 else // "col"
69 n = screen_screencol() + 1 + n;
70
71 if (n < 1)
72 n = 1;
73 return n;
74}
75
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020076 static void
77get_pos_options(win_T *wp, dict_T *dict)
78{
79 char_u *str;
80 int nr;
Bram Moolenaar711d02c2019-06-28 04:06:50 +020081 dictitem_T *di;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020082
83 nr = popup_options_one(dict, (char_u *)"line");
84 if (nr > 0)
85 wp->w_wantline = nr;
86 nr = popup_options_one(dict, (char_u *)"col");
87 if (nr > 0)
88 wp->w_wantcol = nr;
89
Bram Moolenaar711d02c2019-06-28 04:06:50 +020090 di = dict_find(dict, (char_u *)"fixed", -1);
91 if (di != NULL)
92 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020093
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020094 str = dict_get_string(dict, (char_u *)"pos", FALSE);
95 if (str != NULL)
96 {
97 for (nr = 0;
98 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
99 ++nr)
100 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
101 {
102 wp->w_popup_pos = poppos_entries[nr].pp_val;
103 nr = -1;
104 break;
105 }
106 if (nr != -1)
107 semsg(_(e_invarg2), str);
108 }
109}
110
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200111 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200112set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113{
114 dictitem_T *di;
115
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200116 di = dict_find(dict, (char_u *)name, -1);
117 if (di != NULL)
118 {
119 if (di->di_tv.v_type != VAR_LIST)
120 emsg(_(e_listreq));
121 else
122 {
123 list_T *list = di->di_tv.vval.v_list;
124 listitem_T *li;
125 int i;
126 int nr;
127
128 for (i = 0; i < 4; ++i)
129 array[i] = 1;
130 if (list != NULL)
131 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
132 ++i, li = li->li_next)
133 {
134 nr = (int)tv_get_number(&li->li_tv);
135 if (nr >= 0)
136 array[i] = nr > max_val ? max_val : nr;
137 }
138 }
139 }
140}
141
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200142/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200143 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200144 */
145 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200146set_moved_values(win_T *wp)
147{
148 wp->w_popup_curwin = curwin;
149 wp->w_popup_lnum = curwin->w_cursor.lnum;
150 wp->w_popup_mincol = curwin->w_cursor.col;
151 wp->w_popup_maxcol = curwin->w_cursor.col;
152}
153
154/*
155 * Used when popup options contain "moved" with "word" or "WORD".
156 */
157 static void
158set_moved_columns(win_T *wp, int flags)
159{
160 char_u *ptr;
161 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
162
163 if (len > 0)
164 {
165 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
166 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
167 }
168}
169
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200170/*
171 * Return TRUE if "row"/"col" is on the border of the popup.
172 * The values are relative to the top-left corner.
173 */
174 int
175popup_on_border(win_T *wp, int row, int col)
176{
177 return (row == 0 && wp->w_popup_border[0] > 0)
178 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
179 || (col == 0 && wp->w_popup_border[3] > 0)
180 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
181}
182
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200183/*
184 * Return TRUE if "row"/"col" is on the "X" button of the popup.
185 * The values are relative to the top-left corner.
186 * Caller should check w_popup_close is POPCLOSE_BUTTON.
187 */
188 int
189popup_on_X_button(win_T *wp, int row, int col)
190{
191 return row == 0 && col == popup_width(wp) - 1;
192}
193
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200194// Values set when dragging a popup window starts.
195static int drag_start_row;
196static int drag_start_col;
197static int drag_start_wantline;
198static int drag_start_wantcol;
199
200/*
201 * Mouse down on border of popup window: start dragging it.
202 * Uses mouse_col and mouse_row.
203 */
204 void
205popup_start_drag(win_T *wp)
206{
207 drag_start_row = mouse_row;
208 drag_start_col = mouse_col;
209 // TODO: handle using different corner
210 if (wp->w_wantline == 0)
211 drag_start_wantline = wp->w_winrow + 1;
212 else
213 drag_start_wantline = wp->w_wantline;
214 if (wp->w_wantcol == 0)
215 drag_start_wantcol = wp->w_wincol + 1;
216 else
217 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200218
219 // Stop centering the popup
220 if (wp->w_popup_pos == POPPOS_CENTER)
221 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200222}
223
224/*
225 * Mouse moved while dragging a popup window: adjust the window popup position.
226 */
227 void
228popup_drag(win_T *wp)
229{
230 // The popup may be closed before dragging stops.
231 if (!win_valid_popup(wp))
232 return;
233
234 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
235 if (wp->w_wantline < 1)
236 wp->w_wantline = 1;
237 if (wp->w_wantline > Rows)
238 wp->w_wantline = Rows;
239 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
240 if (wp->w_wantcol < 1)
241 wp->w_wantcol = 1;
242 if (wp->w_wantcol > Columns)
243 wp->w_wantcol = Columns;
244
245 popup_adjust_position(wp);
246}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200247
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200248/*
249 * Set w_firstline to match the current "wp->w_topline".
250 */
251 void
252popup_set_firstline(win_T *wp)
253{
254 int height = wp->w_height;
255
256 wp->w_firstline = wp->w_topline;
257 popup_adjust_position(wp);
258
259 // we don't want the popup to get smaller, decrement the first line
260 // until it doesn't
261 while (wp->w_firstline > 1 && wp->w_height < height)
262 {
263 --wp->w_firstline;
264 popup_adjust_position(wp);
265 }
266}
267
268/*
269 * Handle a click in a popup window, if it is in the scrollbar.
270 */
271 void
272popup_handle_scrollbar_click(win_T *wp, int row, int col)
273{
274 int height = popup_height(wp);
275 int old_topline = wp->w_topline;
276
277 if (wp->w_has_scrollbar == 0)
278 return;
279 if (row >= wp->w_popup_border[0]
280 && row < height - wp->w_popup_border[2]
281 && col == popup_width(wp) - 1)
282 {
283 if (row >= height / 2)
284 {
285 // Click in lower half, scroll down.
286 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
287 ++wp->w_topline;
288 }
289 else if (wp->w_topline > 1)
290 // click on upper half, scroll up.
291 --wp->w_topline;
292 if (wp->w_topline != old_topline)
293 {
294 popup_set_firstline(wp);
295 redraw_win_later(wp, NOT_VALID);
296 }
297 }
298}
299
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200300#if defined(FEAT_TIMERS)
301 static void
302popup_add_timeout(win_T *wp, int time)
303{
304 char_u cbbuf[50];
305 char_u *ptr = cbbuf;
306 typval_T tv;
307
308 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
309 "{_ -> popup_close(%d)}", wp->w_id);
310 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
311 {
312 wp->w_popup_timer = create_timer(time, 0);
313 wp->w_popup_timer->tr_callback = get_callback(&tv);
314 clear_tv(&tv);
315 }
316}
317#endif
318
Bram Moolenaar17627312019-06-02 19:53:44 +0200319/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200320 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200321 */
322 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200323apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200324{
Bram Moolenaarae943152019-06-16 22:54:14 +0200325 int nr;
326
327 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
328 wp->w_minwidth = nr;
329 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
330 wp->w_minheight = nr;
331 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
332 wp->w_maxwidth = nr;
333 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
334 wp->w_maxheight = nr;
335 get_pos_options(wp, d);
336}
337
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200338 static void
339check_highlight(dict_T *dict, char *name, char_u **pval)
340{
341 dictitem_T *di;
342 char_u *str;
343
344 di = dict_find(dict, (char_u *)name, -1);
345 if (di != NULL)
346 {
347 if (di->di_tv.v_type != VAR_STRING)
348 semsg(_(e_invargval), name);
349 else
350 {
351 str = tv_get_string(&di->di_tv);
352 if (*str != NUL)
353 *pval = vim_strsave(str);
354 }
355 }
356}
357
Bram Moolenaarae943152019-06-16 22:54:14 +0200358/*
359 * Shared between popup_create() and f_popup_setoptions().
360 */
361 static void
362apply_general_options(win_T *wp, dict_T *dict)
363{
364 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200365 int nr;
366 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200367
Bram Moolenaarae943152019-06-16 22:54:14 +0200368 // TODO: flip
369
370 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200371 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200372 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
373 if (wp->w_firstline < 1)
374 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200375
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200376 di = dict_find(dict, (char_u *)"scrollbar", -1);
377 if (di != NULL)
378 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
379
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200380 str = dict_get_string(dict, (char_u *)"title", FALSE);
381 if (str != NULL)
382 {
383 vim_free(wp->w_popup_title);
384 wp->w_popup_title = vim_strsave(str);
385 }
386
Bram Moolenaar402502d2019-05-30 22:07:36 +0200387 di = dict_find(dict, (char_u *)"wrap", -1);
388 if (di != NULL)
389 {
390 nr = dict_get_number(dict, (char_u *)"wrap");
391 wp->w_p_wrap = nr != 0;
392 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200393
Bram Moolenaara42d9452019-06-15 21:46:30 +0200394 di = dict_find(dict, (char_u *)"drag", -1);
395 if (di != NULL)
396 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200397
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200398 di = dict_find(dict, (char_u *)"close", -1);
399 if (di != NULL)
400 {
401 int ok = TRUE;
402
403 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
404 {
405 char_u *s = di->di_tv.vval.v_string;
406
407 if (STRCMP(s, "none") == 0)
408 wp->w_popup_close = POPCLOSE_NONE;
409 else if (STRCMP(s, "button") == 0)
410 wp->w_popup_close = POPCLOSE_BUTTON;
411 else if (STRCMP(s, "click") == 0)
412 wp->w_popup_close = POPCLOSE_CLICK;
413 else
414 ok = FALSE;
415 }
416 else
417 ok = FALSE;
418 if (!ok)
419 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
420 }
421
Bram Moolenaarae943152019-06-16 22:54:14 +0200422 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
423 if (str != NULL)
424 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
425 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200426
Bram Moolenaarae943152019-06-16 22:54:14 +0200427 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
428 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200429
Bram Moolenaar790498b2019-06-01 22:15:29 +0200430 di = dict_find(dict, (char_u *)"borderhighlight", -1);
431 if (di != NULL)
432 {
433 if (di->di_tv.v_type != VAR_LIST)
434 emsg(_(e_listreq));
435 else
436 {
437 list_T *list = di->di_tv.vval.v_list;
438 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200439 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200440
441 if (list != NULL)
442 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
443 ++i, li = li->li_next)
444 {
445 str = tv_get_string(&li->li_tv);
446 if (*str != NUL)
447 wp->w_border_highlight[i] = vim_strsave(str);
448 }
449 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
450 for (i = 1; i < 4; ++i)
451 wp->w_border_highlight[i] =
452 vim_strsave(wp->w_border_highlight[0]);
453 }
454 }
455
Bram Moolenaar790498b2019-06-01 22:15:29 +0200456 di = dict_find(dict, (char_u *)"borderchars", -1);
457 if (di != NULL)
458 {
459 if (di->di_tv.v_type != VAR_LIST)
460 emsg(_(e_listreq));
461 else
462 {
463 list_T *list = di->di_tv.vval.v_list;
464 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200465 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200466
467 if (list != NULL)
468 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
469 ++i, li = li->li_next)
470 {
471 str = tv_get_string(&li->li_tv);
472 if (*str != NUL)
473 wp->w_border_char[i] = mb_ptr2char(str);
474 }
475 if (list->lv_len == 1)
476 for (i = 1; i < 8; ++i)
477 wp->w_border_char[i] = wp->w_border_char[0];
478 if (list->lv_len == 2)
479 {
480 for (i = 4; i < 8; ++i)
481 wp->w_border_char[i] = wp->w_border_char[1];
482 for (i = 1; i < 4; ++i)
483 wp->w_border_char[i] = wp->w_border_char[0];
484 }
485 }
486 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200487
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200488 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
489 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
490
Bram Moolenaarae943152019-06-16 22:54:14 +0200491 di = dict_find(dict, (char_u *)"zindex", -1);
492 if (di != NULL)
493 {
494 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
495 if (wp->w_zindex < 1)
496 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
497 if (wp->w_zindex > 32000)
498 wp->w_zindex = 32000;
499 }
500
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200501 di = dict_find(dict, (char_u *)"mask", -1);
502 if (di != NULL)
503 {
504 int ok = TRUE;
505
506 if (di->di_tv.v_type != VAR_LIST)
507 ok = FALSE;
508 else if (di->di_tv.vval.v_list != NULL)
509 {
510 listitem_T *li;
511
512 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
513 li = li->li_next)
514 {
515 if (li->li_tv.v_type != VAR_LIST
516 || li->li_tv.vval.v_list == NULL
517 || li->li_tv.vval.v_list->lv_len != 4)
518 {
519 ok = FALSE;
520 break;
521 }
522 }
523 }
524 if (ok)
525 {
526 wp->w_popup_mask = di->di_tv.vval.v_list;
527 ++wp->w_popup_mask->lv_refcount;
528 }
529 else
530 semsg(_(e_invargval), "mask");
531 }
532
Bram Moolenaarae943152019-06-16 22:54:14 +0200533#if defined(FEAT_TIMERS)
534 // Add timer to close the popup after some time.
535 nr = dict_get_number(dict, (char_u *)"time");
536 if (nr > 0)
537 popup_add_timeout(wp, nr);
538#endif
539
Bram Moolenaar3397f742019-06-02 18:40:06 +0200540 di = dict_find(dict, (char_u *)"moved", -1);
541 if (di != NULL)
542 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200543 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200544 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
545 {
546 char_u *s = di->di_tv.vval.v_string;
547 int flags = 0;
548
549 if (STRCMP(s, "word") == 0)
550 flags = FIND_IDENT | FIND_STRING;
551 else if (STRCMP(s, "WORD") == 0)
552 flags = FIND_STRING;
553 else if (STRCMP(s, "any") != 0)
554 semsg(_(e_invarg2), s);
555 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200556 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200557 }
558 else if (di->di_tv.v_type == VAR_LIST
559 && di->di_tv.vval.v_list != NULL
560 && di->di_tv.vval.v_list->lv_len == 2)
561 {
562 list_T *l = di->di_tv.vval.v_list;
563
564 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
565 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
566 }
567 else
568 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
569 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200570
Bram Moolenaarae943152019-06-16 22:54:14 +0200571 di = dict_find(dict, (char_u *)"filter", -1);
572 if (di != NULL)
573 {
574 callback_T callback = get_callback(&di->di_tv);
575
576 if (callback.cb_name != NULL)
577 {
578 free_callback(&wp->w_filter_cb);
579 set_callback(&wp->w_filter_cb, &callback);
580 }
581 }
582
583 di = dict_find(dict, (char_u *)"callback", -1);
584 if (di != NULL)
585 {
586 callback_T callback = get_callback(&di->di_tv);
587
588 if (callback.cb_name != NULL)
589 {
590 free_callback(&wp->w_close_cb);
591 set_callback(&wp->w_close_cb, &callback);
592 }
593 }
594}
595
596/*
597 * Go through the options in "dict" and apply them to popup window "wp".
598 */
599 static void
600apply_options(win_T *wp, dict_T *dict)
601{
602 int nr;
603
604 apply_move_options(wp, dict);
605 apply_general_options(wp, dict);
606
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200607 nr = dict_get_number(dict, (char_u *)"hidden");
608 if (nr > 0)
609 {
610 wp->w_popup_flags |= POPF_HIDDEN;
611 --wp->w_buffer->b_nwindows;
612 }
613
Bram Moolenaar33796b32019-06-08 16:01:13 +0200614 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200615}
616
617/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200618 * Add lines to the popup from a list of strings.
619 */
620 static void
621add_popup_strings(buf_T *buf, list_T *l)
622{
623 listitem_T *li;
624 linenr_T lnum = 0;
625 char_u *p;
626
627 for (li = l->lv_first; li != NULL; li = li->li_next)
628 if (li->li_tv.v_type == VAR_STRING)
629 {
630 p = li->li_tv.vval.v_string;
631 ml_append_buf(buf, lnum++,
632 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
633 }
634}
635
636/*
637 * Add lines to the popup from a list of dictionaries.
638 */
639 static void
640add_popup_dicts(buf_T *buf, list_T *l)
641{
642 listitem_T *li;
643 listitem_T *pli;
644 linenr_T lnum = 0;
645 char_u *p;
646 dict_T *dict;
647
648 // first add the text lines
649 for (li = l->lv_first; li != NULL; li = li->li_next)
650 {
651 if (li->li_tv.v_type != VAR_DICT)
652 {
653 emsg(_(e_dictreq));
654 return;
655 }
656 dict = li->li_tv.vval.v_dict;
657 p = dict == NULL ? NULL
658 : dict_get_string(dict, (char_u *)"text", FALSE);
659 ml_append_buf(buf, lnum++,
660 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
661 }
662
663 // add the text properties
664 lnum = 1;
665 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
666 {
667 dictitem_T *di;
668 list_T *plist;
669
670 dict = li->li_tv.vval.v_dict;
671 di = dict_find(dict, (char_u *)"props", -1);
672 if (di != NULL)
673 {
674 if (di->di_tv.v_type != VAR_LIST)
675 {
676 emsg(_(e_listreq));
677 return;
678 }
679 plist = di->di_tv.vval.v_list;
680 if (plist != NULL)
681 {
682 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
683 {
684 if (pli->li_tv.v_type != VAR_DICT)
685 {
686 emsg(_(e_dictreq));
687 return;
688 }
689 dict = pli->li_tv.vval.v_dict;
690 if (dict != NULL)
691 {
692 int col = dict_get_number(dict, (char_u *)"col");
693
694 prop_add_common( lnum, col, dict, buf, NULL);
695 }
696 }
697 }
698 }
699 }
700}
701
702/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200703 * Get the padding plus border at the top, adjusted to 1 if there is a title.
704 */
705 static int
706popup_top_extra(win_T *wp)
707{
708 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
709
710 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
711 return 1;
712 return extra;
713}
714
715/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200716 * Return the height of popup window "wp", including border and padding.
717 */
718 int
719popup_height(win_T *wp)
720{
721 return wp->w_height
722 + popup_top_extra(wp)
723 + wp->w_popup_padding[2] + wp->w_popup_border[2];
724}
725
726/*
727 * Return the width of popup window "wp", including border, padding and
728 * scrollbar.
729 */
730 int
731popup_width(win_T *wp)
732{
733 return wp->w_width + wp->w_leftcol
734 + wp->w_popup_padding[3] + wp->w_popup_border[3]
735 + wp->w_popup_padding[1] + wp->w_popup_border[1]
736 + wp->w_has_scrollbar
737 + wp->w_popup_rightoff;
738}
739
740/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200741 * Adjust the position and size of the popup to fit on the screen.
742 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200743 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200744popup_adjust_position(win_T *wp)
745{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200746 linenr_T lnum;
747 int wrapped = 0;
748 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200749 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200750 int center_vert = FALSE;
751 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200752 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200753 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200754 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
755 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
756 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
757 int extra_height = top_extra + bot_extra;
758 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200759 int org_winrow = wp->w_winrow;
760 int org_wincol = wp->w_wincol;
761 int org_width = wp->w_width;
762 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200763 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200764 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200765 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200766
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200767 wp->w_winrow = 0;
768 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200769 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200770 wp->w_popup_leftoff = 0;
771 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200772 if (wp->w_popup_pos == POPPOS_CENTER)
773 {
774 // center after computing the size
775 center_vert = TRUE;
776 center_hor = TRUE;
777 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200778 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200779 {
780 if (wp->w_wantline == 0)
781 center_vert = TRUE;
782 else if (wp->w_popup_pos == POPPOS_TOPLEFT
783 || wp->w_popup_pos == POPPOS_TOPRIGHT)
784 {
785 wp->w_winrow = wp->w_wantline - 1;
786 if (wp->w_winrow >= Rows)
787 wp->w_winrow = Rows - 1;
788 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200789
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200790 if (wp->w_wantcol == 0)
791 center_hor = TRUE;
792 else if (wp->w_popup_pos == POPPOS_TOPLEFT
793 || wp->w_popup_pos == POPPOS_BOTLEFT)
794 {
795 wp->w_wincol = wp->w_wantcol - 1;
796 if (wp->w_wincol >= Columns - 3)
797 wp->w_wincol = Columns - 3;
798 }
799 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200800
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200801 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200802 // When left aligned use the space available, but shift to the left when we
803 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200804 maxspace = Columns - wp->w_wincol - left_extra;
805 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200806 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200807 {
808 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200809 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200810 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200811
Bram Moolenaar8d241042019-06-12 23:40:01 +0200812 // start at the desired first line
813 wp->w_topline = wp->w_firstline;
814 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
815 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
816
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200817 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200818 // Use a minimum width of one, so that something shows when there is no
819 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200820 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200821 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200822 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200823 {
824 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
825
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200826 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200827 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200828 while (len > maxwidth)
829 {
830 ++wrapped;
831 len -= maxwidth;
832 wp->w_width = maxwidth;
833 }
834 }
835 else if (len > maxwidth
836 && allow_adjust_left
837 && (wp->w_popup_pos == POPPOS_TOPLEFT
838 || wp->w_popup_pos == POPPOS_BOTLEFT))
839 {
840 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200841 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200842
Bram Moolenaar51c31312019-06-15 22:27:23 +0200843 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200844 {
845 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200846
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200847 len -= truncate_shift;
848 shift_by -= truncate_shift;
849 }
850
851 wp->w_wincol -= shift_by;
852 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200853 wp->w_width = maxwidth;
854 }
855 if (wp->w_width < len)
856 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200857 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200858 if (wp->w_maxheight > 0
859 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200860 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200861 }
862
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200863 wp->w_has_scrollbar = wp->w_want_scrollbar
864 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
865
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200866 minwidth = wp->w_minwidth;
867 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
868 {
869 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
870
871 if (minwidth < title_len)
872 minwidth = title_len;
873 }
874
875 if (minwidth > 0 && wp->w_width < minwidth)
876 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200877 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +0200878 {
879 if (wp->w_width > maxspace)
880 // some columns cut off on the right
881 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200882 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200883 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200884 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200885 {
886 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
887 if (wp->w_wincol < 0)
888 wp->w_wincol = 0;
889 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200890 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
891 || wp->w_popup_pos == POPPOS_TOPRIGHT)
892 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200893 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
894
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200895 // Right aligned: move to the right if needed.
896 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200897 if (leftoff >= 0)
898 wp->w_wincol = leftoff;
899 else if (wp->w_popup_fixed)
900 {
901 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +0200902 // columns when displaying the window, minus left border and
903 // padding.
904 if (-leftoff > left_extra)
905 wp->w_leftcol = -leftoff - left_extra;
906 wp->w_width -= wp->w_leftcol;
907 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200908 if (wp->w_width < 0)
909 wp->w_width = 0;
910 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200911 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200912
Bram Moolenaar8d241042019-06-12 23:40:01 +0200913 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
914 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200915 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
916 wp->w_height = wp->w_minheight;
917 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
918 wp->w_height = wp->w_maxheight;
919 if (wp->w_height > Rows - wp->w_winrow)
920 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200921
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200922 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200923 {
924 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
925 if (wp->w_winrow < 0)
926 wp->w_winrow = 0;
927 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200928 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
929 || wp->w_popup_pos == POPPOS_BOTLEFT)
930 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200931 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200932 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200933 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200934 else
935 // not enough space, make top aligned
936 wp->w_winrow = wp->w_wantline + 1;
937 }
938
Bram Moolenaar17146962019-05-30 00:12:11 +0200939 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200940
941 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200942 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200943 if (org_winrow != wp->w_winrow
944 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200945 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +0200946 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +0200947 || org_width != wp->w_width
948 || org_height != wp->w_height)
949 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200950 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200951 popup_mask_refresh = TRUE;
952 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200953}
954
Bram Moolenaar17627312019-06-02 19:53:44 +0200955typedef enum
956{
957 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200958 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200959 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200960 TYPE_DIALOG,
961 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200962} create_type_T;
963
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200964/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200965 * Make "buf" empty and set the contents to "text".
966 * Used by popup_create() and popup_settext().
967 */
968 static void
969popup_set_buffer_text(buf_T *buf, typval_T text)
970{
971 int lnum;
972
973 // Clear the buffer, then replace the lines.
974 curbuf = buf;
975 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
976 ml_delete(lnum, FALSE);
977 curbuf = curwin->w_buffer;
978
979 // Add text to the buffer.
980 if (text.v_type == VAR_STRING)
981 {
982 // just a string
983 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
984 }
985 else
986 {
987 list_T *l = text.vval.v_list;
988
989 if (l->lv_len > 0)
990 {
991 if (l->lv_first->li_tv.v_type == VAR_STRING)
992 // list of strings
993 add_popup_strings(buf, l);
994 else
995 // list of dictionaries
996 add_popup_dicts(buf, l);
997 }
998 }
999
1000 // delete the line that was in the empty buffer
1001 curbuf = buf;
1002 ml_delete(buf->b_ml.ml_line_count, FALSE);
1003 curbuf = curwin->w_buffer;
1004}
1005
1006/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001007 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001008 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001009 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001010 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001011popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001012{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001013 win_T *wp;
1014 tabpage_T *tp = NULL;
1015 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001016 int new_buffer;
1017 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001018 dict_T *d;
1019 int nr;
1020 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001021
1022 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001023 if (argvars[0].v_type == VAR_NUMBER)
1024 {
1025 buf = buflist_findnr( argvars[0].vval.v_number);
1026 if (buf == NULL)
1027 {
1028 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1029 return NULL;
1030 }
1031 }
1032 else if (!(argvars[0].v_type == VAR_STRING
1033 && argvars[0].vval.v_string != NULL)
1034 && !(argvars[0].v_type == VAR_LIST
1035 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001036 {
1037 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001038 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001039 }
1040 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1041 {
1042 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001043 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001044 }
1045 d = argvars[1].vval.v_dict;
1046
Bram Moolenaara3fce622019-06-20 02:31:49 +02001047 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1048 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1049 else if (type == TYPE_NOTIFICATION)
1050 tabnr = -1; // notifications are global by default
1051 else
1052 tabnr = 0;
1053 if (tabnr > 0)
1054 {
1055 tp = find_tabpage(tabnr);
1056 if (tp == NULL)
1057 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001058 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001059 return NULL;
1060 }
1061 }
1062
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001063 // Create the window and buffer.
1064 wp = win_alloc_popup_win();
1065 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001066 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001067 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001068 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001069 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001070
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001071 if (buf != NULL)
1072 {
1073 // use existing buffer
1074 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001075 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001076 buffer_ensure_loaded(buf);
1077 }
1078 else
1079 {
1080 // create a new buffer associated with the popup
1081 new_buffer = TRUE;
1082 buf = buflist_new(NULL, NULL, (linenr_T)0,
1083 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1084 if (buf == NULL)
1085 return NULL;
1086 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001087
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001088 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001089
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001090 set_local_options_default(wp);
1091 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001092 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001093 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1094 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1095 buf->b_p_ul = -1; // no undo
1096 buf->b_p_swf = FALSE; // no swap file
1097 buf->b_p_bl = FALSE; // unlisted buffer
1098 buf->b_locked = TRUE;
1099 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001100
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001101 // Avoid that 'buftype' is reset when this buffer is entered.
1102 buf->b_p_initialized = TRUE;
1103 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001104
Bram Moolenaara3fce622019-06-20 02:31:49 +02001105 if (tp != NULL)
1106 {
1107 // popup on specified tab page
1108 wp->w_next = tp->tp_first_popupwin;
1109 tp->tp_first_popupwin = wp;
1110 }
1111 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001112 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001113 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001114 wp->w_next = curtab->tp_first_popupwin;
1115 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001116 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001117 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001118 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001119 win_T *prev = first_popupwin;
1120
1121 // Global popup: add at the end, so that it gets displayed on top of
1122 // older ones with the same zindex. Matters for notifications.
1123 if (first_popupwin == NULL)
1124 first_popupwin = wp;
1125 else
1126 {
1127 while (prev->w_next != NULL)
1128 prev = prev->w_next;
1129 prev->w_next = wp;
1130 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001131 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001132
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001133 if (new_buffer)
1134 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001135
Bram Moolenaar17627312019-06-02 19:53:44 +02001136 if (type == TYPE_ATCURSOR)
1137 {
1138 wp->w_popup_pos = POPPOS_BOTLEFT;
1139 setcursor_mayforce(TRUE);
1140 wp->w_wantline = screen_screenrow();
1141 if (wp->w_wantline == 0) // cursor in first line
1142 {
1143 wp->w_wantline = 2;
1144 wp->w_popup_pos = POPPOS_TOPLEFT;
1145 }
1146 wp->w_wantcol = screen_screencol() + 1;
1147 set_moved_values(wp);
1148 set_moved_columns(wp, FIND_STRING);
1149 }
1150
Bram Moolenaar33796b32019-06-08 16:01:13 +02001151 // set default values
1152 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001153 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001154
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001155 if (type == TYPE_NOTIFICATION)
1156 {
1157 win_T *twp, *nextwin;
1158 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001159
1160 // Try to not overlap with another global popup. Guess we need 3
1161 // more screen lines than buffer lines.
1162 wp->w_wantline = 1;
1163 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1164 {
1165 nextwin = twp->w_next;
1166 if (twp != wp
1167 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1168 && twp->w_winrow <= wp->w_wantline - 1 + height
1169 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1170 {
1171 // move to below this popup and restart the loop to check for
1172 // overlap with other popups
1173 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1174 nextwin = first_popupwin;
1175 }
1176 }
1177 if (wp->w_wantline + height > Rows)
1178 {
1179 // can't avoid overlap, put on top in the hope that message goes
1180 // away soon.
1181 wp->w_wantline = 1;
1182 }
1183
1184 wp->w_wantcol = 10;
1185 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001186 wp->w_minwidth = 20;
1187 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001188 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001189 for (i = 0; i < 4; ++i)
1190 wp->w_popup_border[i] = 1;
1191 wp->w_popup_padding[1] = 1;
1192 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001193
1194 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001195 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001196 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1197 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001198 }
1199
Bram Moolenaara730e552019-06-16 19:05:31 +02001200 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001201 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001202 wp->w_popup_pos = POPPOS_CENTER;
1203 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1204 wp->w_popup_drag = 1;
1205 for (i = 0; i < 4; ++i)
1206 {
1207 wp->w_popup_border[i] = 1;
1208 wp->w_popup_padding[i] = 1;
1209 }
1210 }
1211
Bram Moolenaara730e552019-06-16 19:05:31 +02001212 if (type == TYPE_MENU)
1213 {
1214 typval_T tv;
1215 callback_T callback;
1216
1217 tv.v_type = VAR_STRING;
1218 tv.vval.v_string = (char_u *)"popup_filter_menu";
1219 callback = get_callback(&tv);
1220 if (callback.cb_name != NULL)
1221 set_callback(&wp->w_filter_cb, &callback);
1222
1223 wp->w_p_wrap = 0;
1224 }
1225
Bram Moolenaarae943152019-06-16 22:54:14 +02001226 for (i = 0; i < 4; ++i)
1227 VIM_CLEAR(wp->w_border_highlight[i]);
1228 for (i = 0; i < 8; ++i)
1229 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001230 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001231 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001232
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001233 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001234 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001235
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001236#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001237 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1238 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001239#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001240
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001241 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001242
1243 wp->w_vsep_width = 0;
1244
1245 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001246 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001247
1248 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001249}
1250
1251/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001252 * popup_clear()
1253 */
1254 void
1255f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1256{
1257 close_all_popups();
1258}
1259
1260/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001261 * popup_create({text}, {options})
1262 */
1263 void
1264f_popup_create(typval_T *argvars, typval_T *rettv)
1265{
Bram Moolenaar17627312019-06-02 19:53:44 +02001266 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001267}
1268
1269/*
1270 * popup_atcursor({text}, {options})
1271 */
1272 void
1273f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1274{
Bram Moolenaar17627312019-06-02 19:53:44 +02001275 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001276}
1277
1278/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001279 * Invoke the close callback for window "wp" with value "result".
1280 * Careful: The callback may make "wp" invalid!
1281 */
1282 static void
1283invoke_popup_callback(win_T *wp, typval_T *result)
1284{
1285 typval_T rettv;
1286 int dummy;
1287 typval_T argv[3];
1288
1289 argv[0].v_type = VAR_NUMBER;
1290 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1291
1292 if (result != NULL && result->v_type != VAR_UNKNOWN)
1293 copy_tv(result, &argv[1]);
1294 else
1295 {
1296 argv[1].v_type = VAR_NUMBER;
1297 argv[1].vval.v_number = 0;
1298 }
1299
1300 argv[2].v_type = VAR_UNKNOWN;
1301
1302 call_callback(&wp->w_close_cb, -1,
1303 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1304 if (result != NULL)
1305 clear_tv(&argv[1]);
1306 clear_tv(&rettv);
1307}
1308
1309/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001310 * Close popup "wp" and invoke any close callback for it.
1311 */
1312 static void
1313popup_close_and_callback(win_T *wp, typval_T *arg)
1314{
1315 int id = wp->w_id;
1316
1317 if (wp->w_close_cb.cb_name != NULL)
1318 // Careful: This may make "wp" invalid.
1319 invoke_popup_callback(wp, arg);
1320
1321 popup_close(id);
1322}
1323
1324/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001325 * Close popup "wp" because of a mouse click.
1326 */
1327 void
1328popup_close_for_mouse_click(win_T *wp)
1329{
1330 typval_T res;
1331
1332 res.v_type = VAR_NUMBER;
1333 res.vval.v_number = -2;
1334 popup_close_and_callback(wp, &res);
1335}
1336
1337/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001338 * In a filter: check if the typed key is a mouse event that is used for
1339 * dragging the popup.
1340 */
1341 static void
1342filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1343{
1344 int row = mouse_row;
1345 int col = mouse_col;
1346
1347 if (wp->w_popup_drag
1348 && is_mouse_key(c)
1349 && (wp == popup_dragwin
1350 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1351 // do not consume the key, allow for dragging the popup
1352 rettv->vval.v_number = 0;
1353}
1354
1355 static void
1356popup_highlight_curline(win_T *wp)
1357{
1358 int id;
1359 char buf[100];
1360
1361 match_delete(wp, 1, FALSE);
1362
1363 id = syn_name2id((char_u *)"PopupSelected");
1364 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1365 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1366 (char_u *)buf, 10, 1, NULL, NULL);
1367}
1368
1369/*
1370 * popup_filter_menu({text}, {options})
1371 */
1372 void
1373f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1374{
1375 int id = tv_get_number(&argvars[0]);
1376 win_T *wp = win_id2wp(id);
1377 char_u *key = tv_get_string(&argvars[1]);
1378 typval_T res;
1379 int c;
1380 linenr_T old_lnum;
1381
1382 // If the popup has been closed do not consume the key.
1383 if (wp == NULL)
1384 return;
1385
1386 c = *key;
1387 if (c == K_SPECIAL && key[1] != NUL)
1388 c = TO_SPECIAL(key[1], key[2]);
1389
1390 // consume all keys until done
1391 rettv->vval.v_number = 1;
1392 res.v_type = VAR_NUMBER;
1393
1394 old_lnum = wp->w_cursor.lnum;
1395 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1396 --wp->w_cursor.lnum;
1397 if ((c == 'j' || c == 'J' || c == K_DOWN)
1398 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1399 ++wp->w_cursor.lnum;
1400 if (old_lnum != wp->w_cursor.lnum)
1401 {
1402 popup_highlight_curline(wp);
1403 return;
1404 }
1405
1406 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1407 {
1408 // Cancelled, invoke callback with -1
1409 res.vval.v_number = -1;
1410 popup_close_and_callback(wp, &res);
1411 return;
1412 }
1413 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1414 {
1415 // Invoke callback with current index.
1416 res.vval.v_number = wp->w_cursor.lnum;
1417 popup_close_and_callback(wp, &res);
1418 return;
1419 }
1420
1421 filter_handle_drag(wp, c, rettv);
1422}
1423
1424/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001425 * popup_filter_yesno({text}, {options})
1426 */
1427 void
1428f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1429{
1430 int id = tv_get_number(&argvars[0]);
1431 win_T *wp = win_id2wp(id);
1432 char_u *key = tv_get_string(&argvars[1]);
1433 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001434 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001435
1436 // If the popup has been closed don't consume the key.
1437 if (wp == NULL)
1438 return;
1439
Bram Moolenaara730e552019-06-16 19:05:31 +02001440 c = *key;
1441 if (c == K_SPECIAL && key[1] != NUL)
1442 c = TO_SPECIAL(key[1], key[2]);
1443
Bram Moolenaara42d9452019-06-15 21:46:30 +02001444 // consume all keys until done
1445 rettv->vval.v_number = 1;
1446
Bram Moolenaara730e552019-06-16 19:05:31 +02001447 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001448 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001449 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001450 res.vval.v_number = 0;
1451 else
1452 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001453 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001454 return;
1455 }
1456
1457 // Invoke callback
1458 res.v_type = VAR_NUMBER;
1459 popup_close_and_callback(wp, &res);
1460}
1461
1462/*
1463 * popup_dialog({text}, {options})
1464 */
1465 void
1466f_popup_dialog(typval_T *argvars, typval_T *rettv)
1467{
1468 popup_create(argvars, rettv, TYPE_DIALOG);
1469}
1470
1471/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001472 * popup_menu({text}, {options})
1473 */
1474 void
1475f_popup_menu(typval_T *argvars, typval_T *rettv)
1476{
1477 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1478
1479 if (wp != NULL)
1480 popup_highlight_curline(wp);
1481}
1482
1483/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001484 * popup_notification({text}, {options})
1485 */
1486 void
1487f_popup_notification(typval_T *argvars, typval_T *rettv)
1488{
1489 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1490}
1491
1492/*
1493 * Find the popup window with window-ID "id".
1494 * If the popup window does not exist NULL is returned.
1495 * If the window is not a popup window, and error message is given.
1496 */
1497 static win_T *
1498find_popup_win(int id)
1499{
1500 win_T *wp = win_id2wp(id);
1501
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001502 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001503 {
1504 semsg(_("E993: window %d is not a popup window"), id);
1505 return NULL;
1506 }
1507 return wp;
1508}
1509
1510/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001511 * popup_close({id})
1512 */
1513 void
1514f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1515{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001516 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001517 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001518
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001519 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001520 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001521}
1522
1523/*
1524 * popup_hide({id})
1525 */
1526 void
1527f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1528{
1529 int id = (int)tv_get_number(argvars);
1530 win_T *wp = find_popup_win(id);
1531
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001532 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001533 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001534 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001535 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001536 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001537 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001538 }
1539}
1540
1541/*
1542 * popup_show({id})
1543 */
1544 void
1545f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1546{
1547 int id = (int)tv_get_number(argvars);
1548 win_T *wp = find_popup_win(id);
1549
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001550 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001551 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001552 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001553 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001554 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001555 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001556 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001557}
1558
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001559/*
1560 * popup_settext({id}, {text})
1561 */
1562 void
1563f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1564{
1565 int id = (int)tv_get_number(&argvars[0]);
1566 win_T *wp = find_popup_win(id);
1567
1568 if (wp != NULL)
1569 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001570 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1571 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1572 else
1573 {
1574 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1575 popup_adjust_position(wp);
1576 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001577 }
1578}
1579
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001580 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001581popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001582{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001583 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001584 if (wp->w_winrow + wp->w_height >= cmdline_row)
1585 clear_cmdline = TRUE;
1586 win_free_popup(wp);
1587 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001588 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001589}
1590
Bram Moolenaarec583842019-05-26 14:11:23 +02001591/*
1592 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001593 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001594 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001595 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001596popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001597{
1598 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001599 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001600 win_T *prev = NULL;
1601
Bram Moolenaarec583842019-05-26 14:11:23 +02001602 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001603 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001604 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001605 {
1606 if (prev == NULL)
1607 first_popupwin = wp->w_next;
1608 else
1609 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001610 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001611 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001612 }
1613
Bram Moolenaarec583842019-05-26 14:11:23 +02001614 // go through tab-local popups
1615 FOR_ALL_TABPAGES(tp)
1616 popup_close_tabpage(tp, id);
1617}
1618
1619/*
1620 * Close a popup window with Window-id "id" in tabpage "tp".
1621 */
1622 void
1623popup_close_tabpage(tabpage_T *tp, int id)
1624{
1625 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001626 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001627 win_T *prev = NULL;
1628
Bram Moolenaarec583842019-05-26 14:11:23 +02001629 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1630 if (wp->w_id == id)
1631 {
1632 if (prev == NULL)
1633 *root = wp->w_next;
1634 else
1635 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001636 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001637 return;
1638 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001639}
1640
1641 void
1642close_all_popups(void)
1643{
1644 while (first_popupwin != NULL)
1645 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001646 while (curtab->tp_first_popupwin != NULL)
1647 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001648}
1649
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001650/*
1651 * popup_move({id}, {options})
1652 */
1653 void
1654f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1655{
Bram Moolenaarae943152019-06-16 22:54:14 +02001656 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001657 int id = (int)tv_get_number(argvars);
1658 win_T *wp = find_popup_win(id);
1659
1660 if (wp == NULL)
1661 return; // invalid {id}
1662
1663 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1664 {
1665 emsg(_(e_dictreq));
1666 return;
1667 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001668 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001669
Bram Moolenaarae943152019-06-16 22:54:14 +02001670 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001671
1672 if (wp->w_winrow + wp->w_height >= cmdline_row)
1673 clear_cmdline = TRUE;
1674 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001675}
1676
Bram Moolenaarbc133542019-05-29 20:26:46 +02001677/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001678 * popup_setoptions({id}, {options})
1679 */
1680 void
1681f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1682{
1683 dict_T *dict;
1684 int id = (int)tv_get_number(argvars);
1685 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001686 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001687
1688 if (wp == NULL)
1689 return; // invalid {id}
1690
1691 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1692 {
1693 emsg(_(e_dictreq));
1694 return;
1695 }
1696 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001697 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001698
1699 apply_move_options(wp, dict);
1700 apply_general_options(wp, dict);
1701
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001702 if (old_firstline != wp->w_firstline)
1703 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001704 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001705 popup_adjust_position(wp);
1706}
1707
1708/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001709 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001710 */
1711 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001712f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001713{
1714 dict_T *dict;
1715 int id = (int)tv_get_number(argvars);
1716 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001717 int top_extra;
1718 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001719
1720 if (rettv_dict_alloc(rettv) == OK)
1721 {
1722 if (wp == NULL)
1723 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001724 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001725 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1726
Bram Moolenaarbc133542019-05-29 20:26:46 +02001727 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001728
Bram Moolenaarbc133542019-05-29 20:26:46 +02001729 dict_add_number(dict, "line", wp->w_winrow + 1);
1730 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001731 dict_add_number(dict, "width", wp->w_width + left_extra
1732 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1733 dict_add_number(dict, "height", wp->w_height + top_extra
1734 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001735
1736 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1737 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1738 dict_add_number(dict, "core_width", wp->w_width);
1739 dict_add_number(dict, "core_height", wp->w_height);
1740
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001741 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001742 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001743 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001744 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001745 }
1746}
1747
1748/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001749 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1750 */
1751 static void
1752get_padding_border(dict_T *dict, int *array, char *name)
1753{
1754 list_T *list;
1755 int i;
1756
1757 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1758 return;
1759
1760 list = list_alloc();
1761 if (list != NULL)
1762 {
1763 dict_add_list(dict, name, list);
1764 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1765 for (i = 0; i < 4; ++i)
1766 list_append_number(list, array[i]);
1767 }
1768}
1769
1770/*
1771 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1772 */
1773 static void
1774get_borderhighlight(dict_T *dict, win_T *wp)
1775{
1776 list_T *list;
1777 int i;
1778
1779 for (i = 0; i < 4; ++i)
1780 if (wp->w_border_highlight[i] != NULL)
1781 break;
1782 if (i == 4)
1783 return;
1784
1785 list = list_alloc();
1786 if (list != NULL)
1787 {
1788 dict_add_list(dict, "borderhighlight", list);
1789 for (i = 0; i < 4; ++i)
1790 list_append_string(list, wp->w_border_highlight[i], -1);
1791 }
1792}
1793
1794/*
1795 * For popup_getoptions(): add a "borderchars" entry to "dict".
1796 */
1797 static void
1798get_borderchars(dict_T *dict, win_T *wp)
1799{
1800 list_T *list;
1801 int i;
1802 char_u buf[NUMBUFLEN];
1803 int len;
1804
1805 for (i = 0; i < 8; ++i)
1806 if (wp->w_border_char[i] != 0)
1807 break;
1808 if (i == 8)
1809 return;
1810
1811 list = list_alloc();
1812 if (list != NULL)
1813 {
1814 dict_add_list(dict, "borderchars", list);
1815 for (i = 0; i < 8; ++i)
1816 {
1817 len = mb_char2bytes(wp->w_border_char[i], buf);
1818 list_append_string(list, buf, len);
1819 }
1820 }
1821}
1822
1823/*
1824 * For popup_getoptions(): add a "moved" entry to "dict".
1825 */
1826 static void
1827get_moved_list(dict_T *dict, win_T *wp)
1828{
1829 list_T *list;
1830
1831 list = list_alloc();
1832 if (list != NULL)
1833 {
1834 dict_add_list(dict, "moved", list);
1835 list_append_number(list, wp->w_popup_mincol);
1836 list_append_number(list, wp->w_popup_maxcol);
1837 }
1838}
1839
1840/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001841 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001842 */
1843 void
1844f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1845{
1846 dict_T *dict;
1847 int id = (int)tv_get_number(argvars);
1848 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001849 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001850 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001851
1852 if (rettv_dict_alloc(rettv) == OK)
1853 {
1854 if (wp == NULL)
1855 return;
1856
1857 dict = rettv->vval.v_dict;
1858 dict_add_number(dict, "line", wp->w_wantline);
1859 dict_add_number(dict, "col", wp->w_wantcol);
1860 dict_add_number(dict, "minwidth", wp->w_minwidth);
1861 dict_add_number(dict, "minheight", wp->w_minheight);
1862 dict_add_number(dict, "maxheight", wp->w_maxheight);
1863 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001864 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001865 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001866 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001867 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001868 dict_add_string(dict, "title", wp->w_popup_title);
1869 dict_add_number(dict, "wrap", wp->w_p_wrap);
1870 dict_add_number(dict, "drag", wp->w_popup_drag);
1871 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02001872 if (wp->w_scrollbar_highlight != NULL)
1873 dict_add_string(dict, "scrollbarhighlight",
1874 wp->w_scrollbar_highlight);
1875 if (wp->w_thumb_highlight != NULL)
1876 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02001877
Bram Moolenaara3fce622019-06-20 02:31:49 +02001878 // find the tabpage that holds this popup
1879 i = 1;
1880 FOR_ALL_TABPAGES(tp)
1881 {
1882 win_T *p;
1883
1884 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1885 if (p->w_id == id)
1886 break;
1887 if (p != NULL)
1888 break;
1889 ++i;
1890 }
1891 if (tp == NULL)
1892 i = -1; // must be global
1893 else if (tp == curtab)
1894 i = 0;
1895 dict_add_number(dict, "tabpage", i);
1896
Bram Moolenaarae943152019-06-16 22:54:14 +02001897 get_padding_border(dict, wp->w_popup_padding, "padding");
1898 get_padding_border(dict, wp->w_popup_border, "border");
1899 get_borderhighlight(dict, wp);
1900 get_borderchars(dict, wp);
1901 get_moved_list(dict, wp);
1902
1903 if (wp->w_filter_cb.cb_name != NULL)
1904 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1905 if (wp->w_close_cb.cb_name != NULL)
1906 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001907
1908 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1909 ++i)
1910 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1911 {
1912 dict_add_string(dict, "pos",
1913 (char_u *)poppos_entries[i].pp_name);
1914 break;
1915 }
1916
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001917 dict_add_string(dict, "close", (char_u *)(
1918 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
1919 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
1920
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001921# if defined(FEAT_TIMERS)
1922 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1923 ? (long)wp->w_popup_timer->tr_interval : 0L);
1924# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001925 }
1926}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001927
1928 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001929error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001930{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001931 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001932 {
1933 emsg(_("E994: Not allowed in a popup window"));
1934 return TRUE;
1935 }
1936 return FALSE;
1937}
1938
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001939/*
1940 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001941 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001942 */
1943 void
1944popup_reset_handled()
1945{
1946 win_T *wp;
1947
1948 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1949 wp->w_popup_flags &= ~POPF_HANDLED;
1950 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1951 wp->w_popup_flags &= ~POPF_HANDLED;
1952}
1953
1954/*
1955 * Find the next visible popup where POPF_HANDLED is not set.
1956 * Must have called popup_reset_handled() first.
1957 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1958 * popup with the highest zindex.
1959 */
1960 win_T *
1961find_next_popup(int lowest)
1962{
1963 win_T *wp;
1964 win_T *found_wp;
1965 int found_zindex;
1966
1967 found_zindex = lowest ? INT_MAX : 0;
1968 found_wp = NULL;
1969 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1970 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1971 && (lowest ? wp->w_zindex < found_zindex
1972 : wp->w_zindex > found_zindex))
1973 {
1974 found_zindex = wp->w_zindex;
1975 found_wp = wp;
1976 }
1977 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1978 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1979 && (lowest ? wp->w_zindex < found_zindex
1980 : wp->w_zindex > found_zindex))
1981 {
1982 found_zindex = wp->w_zindex;
1983 found_wp = wp;
1984 }
1985
1986 if (found_wp != NULL)
1987 found_wp->w_popup_flags |= POPF_HANDLED;
1988 return found_wp;
1989}
1990
1991/*
1992 * Invoke the filter callback for window "wp" with typed character "c".
1993 * Uses the global "mod_mask" for modifiers.
1994 * Returns the return value of the filter.
1995 * Careful: The filter may make "wp" invalid!
1996 */
1997 static int
1998invoke_popup_filter(win_T *wp, int c)
1999{
2000 int res;
2001 typval_T rettv;
2002 int dummy;
2003 typval_T argv[3];
2004 char_u buf[NUMBUFLEN];
2005
Bram Moolenaara42d9452019-06-15 21:46:30 +02002006 // Emergency exit: CTRL-C closes the popup.
2007 if (c == Ctrl_C)
2008 {
2009 rettv.v_type = VAR_NUMBER;
2010 rettv.vval.v_number = -1;
2011 popup_close_and_callback(wp, &rettv);
2012 return 1;
2013 }
2014
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002015 argv[0].v_type = VAR_NUMBER;
2016 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2017
2018 // Convert the number to a string, so that the function can use:
2019 // if a:c == "\<F2>"
2020 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2021 argv[1].v_type = VAR_STRING;
2022 argv[1].vval.v_string = vim_strsave(buf);
2023
2024 argv[2].v_type = VAR_UNKNOWN;
2025
Bram Moolenaara42d9452019-06-15 21:46:30 +02002026 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002027 call_callback(&wp->w_filter_cb, -1,
2028 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2029 res = tv_get_number(&rettv);
2030 vim_free(argv[1].vval.v_string);
2031 clear_tv(&rettv);
2032 return res;
2033}
2034
2035/*
2036 * Called when "c" was typed: invoke popup filter callbacks.
2037 * Returns TRUE when the character was consumed,
2038 */
2039 int
2040popup_do_filter(int c)
2041{
2042 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002043 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002044
2045 popup_reset_handled();
2046
2047 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2048 if (wp->w_filter_cb.cb_name != NULL)
2049 res = invoke_popup_filter(wp, c);
2050
2051 return res;
2052}
2053
Bram Moolenaar3397f742019-06-02 18:40:06 +02002054/*
2055 * Called when the cursor moved: check if any popup needs to be closed if the
2056 * cursor moved far enough.
2057 */
2058 void
2059popup_check_cursor_pos()
2060{
2061 win_T *wp;
2062 typval_T tv;
2063
2064 popup_reset_handled();
2065 while ((wp = find_next_popup(TRUE)) != NULL)
2066 if (wp->w_popup_curwin != NULL
2067 && (curwin != wp->w_popup_curwin
2068 || curwin->w_cursor.lnum != wp->w_popup_lnum
2069 || curwin->w_cursor.col < wp->w_popup_mincol
2070 || curwin->w_cursor.col > wp->w_popup_maxcol))
2071 {
2072 tv.v_type = VAR_NUMBER;
2073 tv.vval.v_number = -1;
2074 popup_close_and_callback(wp, &tv);
2075 }
2076}
2077
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002078/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002079 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2080 * "col" and "line" are screen coordinates.
2081 */
2082 static int
2083popup_masked(win_T *wp, int screencol, int screenline)
2084{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002085 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002086 int line = screenline - wp->w_winrow + 1;
2087 listitem_T *lio, *li;
2088 int width, height;
2089
2090 if (wp->w_popup_mask == NULL)
2091 return FALSE;
2092 width = popup_width(wp);
2093 height = popup_height(wp);
2094
2095 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2096 {
2097 int cols, cole;
2098 int lines, linee;
2099
2100 li = lio->li_tv.vval.v_list->lv_first;
2101 cols = tv_get_number(&li->li_tv);
2102 if (cols < 0)
2103 cols = width + cols + 1;
2104 if (col < cols)
2105 continue;
2106 li = li->li_next;
2107 cole = tv_get_number(&li->li_tv);
2108 if (cole < 0)
2109 cole = width + cole + 1;
2110 if (col > cole)
2111 continue;
2112 li = li->li_next;
2113 lines = tv_get_number(&li->li_tv);
2114 if (lines < 0)
2115 lines = height + lines + 1;
2116 if (line < lines)
2117 continue;
2118 li = li->li_next;
2119 linee = tv_get_number(&li->li_tv);
2120 if (linee < 0)
2121 linee = height + linee + 1;
2122 if (line > linee)
2123 continue;
2124 return TRUE;
2125 }
2126 return FALSE;
2127}
2128
2129/*
2130 * Set flags in popup_transparent[] for window "wp" to "val".
2131 */
2132 static void
2133update_popup_transparent(win_T *wp, int val)
2134{
2135 if (wp->w_popup_mask != NULL)
2136 {
2137 int width = popup_width(wp);
2138 int height = popup_height(wp);
2139 listitem_T *lio, *li;
2140 int cols, cole;
2141 int lines, linee;
2142 int col, line;
2143
2144 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2145 {
2146 li = lio->li_tv.vval.v_list->lv_first;
2147 cols = tv_get_number(&li->li_tv);
2148 if (cols < 0)
2149 cols = width + cols + 1;
2150 li = li->li_next;
2151 cole = tv_get_number(&li->li_tv);
2152 if (cole < 0)
2153 cole = width + cole + 1;
2154 li = li->li_next;
2155 lines = tv_get_number(&li->li_tv);
2156 if (lines < 0)
2157 lines = height + lines + 1;
2158 li = li->li_next;
2159 linee = tv_get_number(&li->li_tv);
2160 if (linee < 0)
2161 linee = height + linee + 1;
2162
2163 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002164 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002165 if (cols < 0)
2166 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002167 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002168 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002169 if (lines < 0)
2170 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002171 for (line = lines; line < linee && line < screen_Rows; ++line)
2172 for (col = cols; col < cole && col < screen_Columns; ++col)
2173 popup_transparent[(line + wp->w_winrow) * screen_Columns
2174 + col + wp->w_wincol] = val;
2175 }
2176 }
2177}
2178
2179/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002180 * Update "popup_mask" if needed.
2181 * Also recomputes the popup size and positions.
2182 * Also updates "popup_visible".
2183 * Also marks window lines for redrawing.
2184 */
2185 void
2186may_update_popup_mask(int type)
2187{
2188 win_T *wp;
2189 short *mask;
2190 int line, col;
2191 int redraw_all = FALSE;
2192
2193 // Need to recompute when switching tabs.
2194 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2195 // (such as the screen size) must have changed.
2196 if (popup_mask_tab != curtab || type >= NOT_VALID)
2197 {
2198 popup_mask_refresh = TRUE;
2199 redraw_all = TRUE;
2200 }
2201 if (!popup_mask_refresh)
2202 {
2203 // Check if any buffer has changed.
2204 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2205 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2206 popup_mask_refresh = TRUE;
2207 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2208 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2209 popup_mask_refresh = TRUE;
2210 if (!popup_mask_refresh)
2211 return;
2212 }
2213
2214 // Need to update the mask, something has changed.
2215 popup_mask_refresh = FALSE;
2216 popup_mask_tab = curtab;
2217 popup_visible = FALSE;
2218
2219 // If redrawing everything, just update "popup_mask".
2220 // If redrawing only what is needed, update "popup_mask_next" and then
2221 // compare with "popup_mask" to see what changed.
2222 if (type >= SOME_VALID)
2223 mask = popup_mask;
2224 else
2225 mask = popup_mask_next;
2226 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2227
2228 // Find the window with the lowest zindex that hasn't been handled yet,
2229 // so that the window with a higher zindex overwrites the value in
2230 // popup_mask.
2231 popup_reset_handled();
2232 while ((wp = find_next_popup(TRUE)) != NULL)
2233 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002234 int height;
2235 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002236
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002237 popup_visible = TRUE;
2238
2239 // Recompute the position if the text changed.
2240 if (redraw_all
2241 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2242 popup_adjust_position(wp);
2243
Bram Moolenaard529ba52019-07-02 23:13:53 +02002244 height = popup_height(wp);
2245 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002246 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002247 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002248 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002249 col < wp->w_wincol + width && col < screen_Columns; ++col)
2250 if (!popup_masked(wp, col, line))
2251 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002252 }
2253
2254 // Only check which lines are to be updated if not already
2255 // updating all lines.
2256 if (mask == popup_mask_next)
2257 for (line = 0; line < screen_Rows; ++line)
2258 {
2259 int col_done = 0;
2260
2261 for (col = 0; col < screen_Columns; ++col)
2262 {
2263 int off = line * screen_Columns + col;
2264
2265 if (popup_mask[off] != popup_mask_next[off])
2266 {
2267 popup_mask[off] = popup_mask_next[off];
2268
2269 if (line >= cmdline_row)
2270 {
2271 // the command line needs to be cleared if text below
2272 // the popup is now visible.
2273 if (!msg_scrolled && popup_mask_next[off] == 0)
2274 clear_cmdline = TRUE;
2275 }
2276 else if (col >= col_done)
2277 {
2278 linenr_T lnum;
2279 int line_cp = line;
2280 int col_cp = col;
2281
2282 // The screen position "line" / "col" needs to be
2283 // redrawn. Figure out what window that is and update
2284 // w_redraw_top and w_redr_bot. Only needs to be done
2285 // once for each window line.
2286 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2287 if (wp != NULL)
2288 {
2289 if (line_cp >= wp->w_height)
2290 // In (or below) status line
2291 wp->w_redr_status = TRUE;
2292 // compute the position in the buffer line from the
2293 // position on the screen
2294 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2295 &lnum))
2296 // past bottom
2297 wp->w_redr_status = TRUE;
2298 else
2299 redrawWinline(wp, lnum);
2300
2301 // This line is going to be redrawn, no need to
2302 // check until the right side of the window.
2303 col_done = wp->w_wincol + wp->w_width - 1;
2304 }
2305 }
2306 }
2307 }
2308 }
2309}
2310
2311/*
2312 * Return a string of "len" spaces in IObuff.
2313 */
2314 static char_u *
2315get_spaces(int len)
2316{
2317 vim_memset(IObuff, ' ', (size_t)len);
2318 IObuff[len] = NUL;
2319 return IObuff;
2320}
2321
2322/*
2323 * Update popup windows. They are drawn on top of normal windows.
2324 * "win_update" is called for each popup window, lowest zindex first.
2325 */
2326 void
2327update_popups(void (*win_update)(win_T *wp))
2328{
2329 win_T *wp;
2330 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002331 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002332 int total_width;
2333 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002334 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002335 int popup_attr;
2336 int border_attr[4];
2337 int border_char[8];
2338 char_u buf[MB_MAXBYTES];
2339 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002340 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002341 int padcol = 0;
2342 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002343 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002344 int sb_thumb_top = 0;
2345 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002346 int attr_scroll = 0;
2347 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002348
2349 // Find the window with the lowest zindex that hasn't been updated yet,
2350 // so that the window with a higher zindex is drawn later, thus goes on
2351 // top.
2352 popup_reset_handled();
2353 while ((wp = find_next_popup(TRUE)) != NULL)
2354 {
2355 // This drawing uses the zindex of the popup window, so that it's on
2356 // top of the text but doesn't draw when another popup with higher
2357 // zindex is on top of the character.
2358 screen_zindex = wp->w_zindex;
2359
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002360 // Set flags in popup_transparent[] for masked cells.
2361 update_popup_transparent(wp, 1);
2362
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002363 // adjust w_winrow and w_wincol for border and padding, since
2364 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002365 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002366 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2367 - wp->w_popup_leftoff;
2368 if (wp->w_wincol + left_extra < 0)
2369 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002370 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002371 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002372
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002373 // Draw the popup text, unless it's off screen.
2374 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2375 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002376
2377 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002378 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002379
Bram Moolenaard529ba52019-07-02 23:13:53 +02002380 total_width = popup_width(wp);
2381 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002382 popup_attr = get_wcr_attr(wp);
2383
2384 // We can only use these line drawing characters when 'encoding' is
2385 // "utf-8" and 'ambiwidth' is "single".
2386 if (enc_utf8 && *p_ambw == 's')
2387 {
2388 border_char[0] = border_char[2] = 0x2550;
2389 border_char[1] = border_char[3] = 0x2551;
2390 border_char[4] = 0x2554;
2391 border_char[5] = 0x2557;
2392 border_char[6] = 0x255d;
2393 border_char[7] = 0x255a;
2394 }
2395 else
2396 {
2397 border_char[0] = border_char[2] = '-';
2398 border_char[1] = border_char[3] = '|';
2399 for (i = 4; i < 8; ++i)
2400 border_char[i] = '+';
2401 }
2402 for (i = 0; i < 8; ++i)
2403 if (wp->w_border_char[i] != 0)
2404 border_char[i] = wp->w_border_char[i];
2405
2406 for (i = 0; i < 4; ++i)
2407 {
2408 border_attr[i] = popup_attr;
2409 if (wp->w_border_highlight[i] != NULL)
2410 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2411 }
2412
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002413 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002414 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002415 if (wp->w_popup_border[0] > 0)
2416 {
2417 // top border
2418 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002419 wincol < 0 ? 0 : wincol, wincol + total_width,
2420 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002421 ? border_char[4] : border_char[0],
2422 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002423 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002424 {
2425 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2426 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002427 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002428 }
2429 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002430 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2431 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002432
Bram Moolenaard529ba52019-07-02 23:13:53 +02002433 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2434 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002435 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002436 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2437 - wp->w_has_scrollbar;
2438 if (padcol < 0)
2439 {
2440 padwidth += padcol;
2441 padcol = 0;
2442 }
2443 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002444 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002445 {
2446 // top padding
2447 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002448 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002449 ' ', ' ', popup_attr);
2450 }
2451
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002452 // Title goes on top of border or padding.
2453 if (wp->w_popup_title != NULL)
2454 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2455 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2456
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002457 // Compute scrollbar thumb position and size.
2458 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002459 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002460 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2461
Bram Moolenaar68acb412019-06-26 03:40:36 +02002462 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2463 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002464 if (sb_thumb_height == 0)
2465 sb_thumb_height = 1;
Bram Moolenaar68acb412019-06-26 03:40:36 +02002466 sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2)
2467 * (wp->w_height - sb_thumb_height)
2468 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002469 if (wp->w_scrollbar_highlight != NULL)
2470 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2471 else
2472 attr_scroll = highlight_attr[HLF_PSB];
2473 if (wp->w_thumb_highlight != NULL)
2474 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2475 else
2476 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002477 }
2478
2479 for (i = wp->w_popup_border[0];
2480 i < total_height - wp->w_popup_border[2]; ++i)
2481 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002482 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002483 // left and right padding only needed next to the body
2484 int do_padding =
2485 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2486 && i < total_height - wp->w_popup_border[2]
2487 - wp->w_popup_padding[2];
2488
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002489 row = wp->w_winrow + i;
2490
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002491 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002492 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002493 {
2494 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002495 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002496 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002497 if (do_padding && wp->w_popup_padding[3] > 0)
2498 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002499 int col = wincol + wp->w_popup_border[3];
2500
Bram Moolenaard529ba52019-07-02 23:13:53 +02002501 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002502 pad_left = wp->w_popup_padding[3];
2503 if (col < 0)
2504 {
2505 pad_left += col;
2506 col = 0;
2507 }
2508 if (pad_left > 0)
2509 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2510 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002511 // scrollbar
2512 if (wp->w_has_scrollbar)
2513 {
2514 int line = i - top_off;
2515 int scroll_col = wp->w_wincol + total_width - 1
2516 - wp->w_popup_border[1];
2517
2518 if (line >= 0 && line < wp->w_height)
2519 screen_putchar(' ', row, scroll_col,
2520 line >= sb_thumb_top
2521 && line < sb_thumb_top + sb_thumb_height
2522 ? attr_thumb : attr_scroll);
2523 else
2524 screen_putchar(' ', row, scroll_col, popup_attr);
2525 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002526 // right border
2527 if (wp->w_popup_border[1] > 0)
2528 {
2529 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002530 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002531 }
2532 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002533 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002534 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002535 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002536 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2537 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002538 }
2539
2540 if (wp->w_popup_padding[2] > 0)
2541 {
2542 // bottom padding
2543 row = wp->w_winrow + wp->w_popup_border[0]
2544 + wp->w_popup_padding[0] + wp->w_height;
2545 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002546 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002547 }
2548
2549 if (wp->w_popup_border[2] > 0)
2550 {
2551 // bottom border
2552 row = wp->w_winrow + total_height - 1;
2553 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002554 wincol < 0 ? 0 : wincol,
2555 wincol + total_width,
2556 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002557 ? border_char[7] : border_char[2],
2558 border_char[2], border_attr[2]);
2559 if (wp->w_popup_border[1] > 0)
2560 {
2561 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002562 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002563 }
2564 }
2565
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002566 if (wp->w_popup_close == POPCLOSE_BUTTON)
2567 {
2568 // close button goes on top of anything at the top-right corner
2569 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002570 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002571 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2572 }
2573
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002574 update_popup_transparent(wp, 0);
2575
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002576 // Back to the normal zindex.
2577 screen_zindex = 0;
2578 }
2579}
2580
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002581/*
2582 * Mark references in callbacks of one popup window.
2583 */
2584 static int
2585set_ref_in_one_popup(win_T *wp, int copyID)
2586{
2587 int abort = FALSE;
2588 typval_T tv;
2589
2590 if (wp->w_close_cb.cb_partial != NULL)
2591 {
2592 tv.v_type = VAR_PARTIAL;
2593 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2594 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2595 }
2596 if (wp->w_filter_cb.cb_partial != NULL)
2597 {
2598 tv.v_type = VAR_PARTIAL;
2599 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2600 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2601 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002602 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002603 return abort;
2604}
2605
2606/*
2607 * Set reference in callbacks of popup windows.
2608 */
2609 int
2610set_ref_in_popups(int copyID)
2611{
2612 int abort = FALSE;
2613 win_T *wp;
2614 tabpage_T *tp;
2615
2616 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2617 abort = abort || set_ref_in_one_popup(wp, copyID);
2618
2619 FOR_ALL_TABPAGES(tp)
2620 {
2621 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2622 abort = abort || set_ref_in_one_popup(wp, copyID);
2623 if (abort)
2624 break;
2625 }
2626 return abort;
2627}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002628#endif // FEAT_TEXT_PROP