blob: a10d4e0a2f61611d981a316ffb6726a4ebb50871 [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 Moolenaar451d4b52019-06-12 20:22:27 +0200703 * Return the height of popup window "wp", including border and padding.
704 */
705 int
706popup_height(win_T *wp)
707{
708 return wp->w_height
709 + wp->w_popup_padding[0] + wp->w_popup_border[0]
710 + wp->w_popup_padding[2] + wp->w_popup_border[2];
711}
712
713/*
714 * Return the width of popup window "wp", including border and padding.
715 */
716 int
717popup_width(win_T *wp)
718{
719 return wp->w_width
720 + wp->w_popup_padding[3] + wp->w_popup_border[3]
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200721 + wp->w_popup_padding[1] + wp->w_popup_border[1]
722 + wp->w_has_scrollbar;
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200723}
724
725/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200726 * Get the padding plus border at the top, adjusted to 1 if there is a title.
727 */
728 static int
729popup_top_extra(win_T *wp)
730{
731 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
732
733 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
734 return 1;
735 return extra;
736}
737
738/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200739 * Adjust the position and size of the popup to fit on the screen.
740 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200741 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200742popup_adjust_position(win_T *wp)
743{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200744 linenr_T lnum;
745 int wrapped = 0;
746 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200747 int center_vert = FALSE;
748 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200749 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200750 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200751 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
752 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
753 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
754 int extra_height = top_extra + bot_extra;
755 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200756 int org_winrow = wp->w_winrow;
757 int org_wincol = wp->w_wincol;
758 int org_width = wp->w_width;
759 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200760 int org_leftcol = wp->w_leftcol;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200761 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200762
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200763 wp->w_winrow = 0;
764 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200765 wp->w_leftcol = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200766 if (wp->w_popup_pos == POPPOS_CENTER)
767 {
768 // center after computing the size
769 center_vert = TRUE;
770 center_hor = TRUE;
771 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200772 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200773 {
774 if (wp->w_wantline == 0)
775 center_vert = TRUE;
776 else if (wp->w_popup_pos == POPPOS_TOPLEFT
777 || wp->w_popup_pos == POPPOS_TOPRIGHT)
778 {
779 wp->w_winrow = wp->w_wantline - 1;
780 if (wp->w_winrow >= Rows)
781 wp->w_winrow = Rows - 1;
782 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200783
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200784 if (wp->w_wantcol == 0)
785 center_hor = TRUE;
786 else if (wp->w_popup_pos == POPPOS_TOPLEFT
787 || wp->w_popup_pos == POPPOS_BOTLEFT)
788 {
789 wp->w_wincol = wp->w_wantcol - 1;
790 if (wp->w_wincol >= Columns - 3)
791 wp->w_wincol = Columns - 3;
792 }
793 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200794
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200795 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200796 // When left aligned use the space available, but shift to the left when we
797 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200798 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200799 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200800 {
801 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200802 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200803 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200804
Bram Moolenaar8d241042019-06-12 23:40:01 +0200805 // start at the desired first line
806 wp->w_topline = wp->w_firstline;
807 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
808 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
809
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200810 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200811 // Use a minimum width of one, so that something shows when there is no
812 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200813 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200814 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200815 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200816 {
817 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
818
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200819 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200820 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200821 while (len > maxwidth)
822 {
823 ++wrapped;
824 len -= maxwidth;
825 wp->w_width = maxwidth;
826 }
827 }
828 else if (len > maxwidth
829 && allow_adjust_left
830 && (wp->w_popup_pos == POPPOS_TOPLEFT
831 || wp->w_popup_pos == POPPOS_BOTLEFT))
832 {
833 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200834 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200835
Bram Moolenaar51c31312019-06-15 22:27:23 +0200836 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200837 {
838 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200839
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200840 len -= truncate_shift;
841 shift_by -= truncate_shift;
842 }
843
844 wp->w_wincol -= shift_by;
845 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200846 wp->w_width = maxwidth;
847 }
848 if (wp->w_width < len)
849 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200850 // do not use the width of lines we're not going to show
851 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
852 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
853 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200854 }
855
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200856 wp->w_has_scrollbar = wp->w_want_scrollbar
857 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
858
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200859 minwidth = wp->w_minwidth;
860 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
861 {
862 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
863
864 if (minwidth < title_len)
865 minwidth = title_len;
866 }
867
868 if (minwidth > 0 && wp->w_width < minwidth)
869 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200870 if (wp->w_width > maxwidth)
871 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200872 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200873 {
874 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
875 if (wp->w_wincol < 0)
876 wp->w_wincol = 0;
877 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200878 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
879 || wp->w_popup_pos == POPPOS_TOPRIGHT)
880 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200881 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
882
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200883 // Right aligned: move to the right if needed.
884 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200885 if (leftoff >= 0)
886 wp->w_wincol = leftoff;
887 else if (wp->w_popup_fixed)
888 {
889 // "col" specifies the right edge, but popup doesn't fit, skip some
890 // columns when displaying the window.
891 wp->w_leftcol = -leftoff;
892 wp->w_width += leftoff;
893 if (wp->w_width < 0)
894 wp->w_width = 0;
895 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200896 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200897
Bram Moolenaar8d241042019-06-12 23:40:01 +0200898 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
899 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200900 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
901 wp->w_height = wp->w_minheight;
902 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
903 wp->w_height = wp->w_maxheight;
904 if (wp->w_height > Rows - wp->w_winrow)
905 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200906
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200907 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200908 {
909 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
910 if (wp->w_winrow < 0)
911 wp->w_winrow = 0;
912 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200913 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
914 || wp->w_popup_pos == POPPOS_BOTLEFT)
915 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200916 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200917 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200918 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200919 else
920 // not enough space, make top aligned
921 wp->w_winrow = wp->w_wantline + 1;
922 }
923
Bram Moolenaar17146962019-05-30 00:12:11 +0200924 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200925
926 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200927 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200928 if (org_winrow != wp->w_winrow
929 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200930 || org_leftcol != wp->w_leftcol
Bram Moolenaar33796b32019-06-08 16:01:13 +0200931 || org_width != wp->w_width
932 || org_height != wp->w_height)
933 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200934 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200935 popup_mask_refresh = TRUE;
936 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200937}
938
Bram Moolenaar17627312019-06-02 19:53:44 +0200939typedef enum
940{
941 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200942 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200943 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200944 TYPE_DIALOG,
945 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200946} create_type_T;
947
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200948/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200949 * Make "buf" empty and set the contents to "text".
950 * Used by popup_create() and popup_settext().
951 */
952 static void
953popup_set_buffer_text(buf_T *buf, typval_T text)
954{
955 int lnum;
956
957 // Clear the buffer, then replace the lines.
958 curbuf = buf;
959 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
960 ml_delete(lnum, FALSE);
961 curbuf = curwin->w_buffer;
962
963 // Add text to the buffer.
964 if (text.v_type == VAR_STRING)
965 {
966 // just a string
967 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
968 }
969 else
970 {
971 list_T *l = text.vval.v_list;
972
973 if (l->lv_len > 0)
974 {
975 if (l->lv_first->li_tv.v_type == VAR_STRING)
976 // list of strings
977 add_popup_strings(buf, l);
978 else
979 // list of dictionaries
980 add_popup_dicts(buf, l);
981 }
982 }
983
984 // delete the line that was in the empty buffer
985 curbuf = buf;
986 ml_delete(buf->b_ml.ml_line_count, FALSE);
987 curbuf = curwin->w_buffer;
988}
989
990/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200991 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200992 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200993 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200994 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200995popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200996{
Bram Moolenaara3fce622019-06-20 02:31:49 +0200997 win_T *wp;
998 tabpage_T *tp = NULL;
999 int tabnr;
1000 buf_T *buf;
1001 dict_T *d;
1002 int nr;
1003 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001004
1005 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001006 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
1007 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001008 {
1009 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001010 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001011 }
1012 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1013 {
1014 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001015 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001016 }
1017 d = argvars[1].vval.v_dict;
1018
Bram Moolenaara3fce622019-06-20 02:31:49 +02001019 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1020 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1021 else if (type == TYPE_NOTIFICATION)
1022 tabnr = -1; // notifications are global by default
1023 else
1024 tabnr = 0;
1025 if (tabnr > 0)
1026 {
1027 tp = find_tabpage(tabnr);
1028 if (tp == NULL)
1029 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001030 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001031 return NULL;
1032 }
1033 }
1034
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001035 // Create the window and buffer.
1036 wp = win_alloc_popup_win();
1037 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001038 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001039 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001040 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001041
1042 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
1043 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001044 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001045 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001046
1047 win_init_popup_win(wp, buf);
1048
1049 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +02001050 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001051 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +02001052 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001053 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001054 buf->b_p_ul = -1; // no undo
1055 buf->b_p_swf = FALSE; // no swap file
1056 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001057 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001058 wp->w_p_wrap = TRUE; // 'wrap' is default on
1059
Bram Moolenaar54fabd42019-05-30 19:03:22 +02001060 // Avoid that 'buftype' is reset when this buffer is entered.
1061 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001062
Bram Moolenaara3fce622019-06-20 02:31:49 +02001063 if (tp != NULL)
1064 {
1065 // popup on specified tab page
1066 wp->w_next = tp->tp_first_popupwin;
1067 tp->tp_first_popupwin = wp;
1068 }
1069 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001070 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001071 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001072 wp->w_next = curtab->tp_first_popupwin;
1073 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001074 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001075 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001076 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001077 win_T *prev = first_popupwin;
1078
1079 // Global popup: add at the end, so that it gets displayed on top of
1080 // older ones with the same zindex. Matters for notifications.
1081 if (first_popupwin == NULL)
1082 first_popupwin = wp;
1083 else
1084 {
1085 while (prev->w_next != NULL)
1086 prev = prev->w_next;
1087 prev->w_next = wp;
1088 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001089 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001090
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001091 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001092
Bram Moolenaar17627312019-06-02 19:53:44 +02001093 if (type == TYPE_ATCURSOR)
1094 {
1095 wp->w_popup_pos = POPPOS_BOTLEFT;
1096 setcursor_mayforce(TRUE);
1097 wp->w_wantline = screen_screenrow();
1098 if (wp->w_wantline == 0) // cursor in first line
1099 {
1100 wp->w_wantline = 2;
1101 wp->w_popup_pos = POPPOS_TOPLEFT;
1102 }
1103 wp->w_wantcol = screen_screencol() + 1;
1104 set_moved_values(wp);
1105 set_moved_columns(wp, FIND_STRING);
1106 }
1107
Bram Moolenaar33796b32019-06-08 16:01:13 +02001108 // set default values
1109 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001110 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001111
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001112 if (type == TYPE_NOTIFICATION)
1113 {
1114 win_T *twp, *nextwin;
1115 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001116
1117 // Try to not overlap with another global popup. Guess we need 3
1118 // more screen lines than buffer lines.
1119 wp->w_wantline = 1;
1120 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1121 {
1122 nextwin = twp->w_next;
1123 if (twp != wp
1124 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1125 && twp->w_winrow <= wp->w_wantline - 1 + height
1126 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1127 {
1128 // move to below this popup and restart the loop to check for
1129 // overlap with other popups
1130 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1131 nextwin = first_popupwin;
1132 }
1133 }
1134 if (wp->w_wantline + height > Rows)
1135 {
1136 // can't avoid overlap, put on top in the hope that message goes
1137 // away soon.
1138 wp->w_wantline = 1;
1139 }
1140
1141 wp->w_wantcol = 10;
1142 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001143 wp->w_minwidth = 20;
1144 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001145 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001146 for (i = 0; i < 4; ++i)
1147 wp->w_popup_border[i] = 1;
1148 wp->w_popup_padding[1] = 1;
1149 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001150
1151 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001152 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001153 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1154 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001155 }
1156
Bram Moolenaara730e552019-06-16 19:05:31 +02001157 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001158 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001159 wp->w_popup_pos = POPPOS_CENTER;
1160 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1161 wp->w_popup_drag = 1;
1162 for (i = 0; i < 4; ++i)
1163 {
1164 wp->w_popup_border[i] = 1;
1165 wp->w_popup_padding[i] = 1;
1166 }
1167 }
1168
Bram Moolenaara730e552019-06-16 19:05:31 +02001169 if (type == TYPE_MENU)
1170 {
1171 typval_T tv;
1172 callback_T callback;
1173
1174 tv.v_type = VAR_STRING;
1175 tv.vval.v_string = (char_u *)"popup_filter_menu";
1176 callback = get_callback(&tv);
1177 if (callback.cb_name != NULL)
1178 set_callback(&wp->w_filter_cb, &callback);
1179
1180 wp->w_p_wrap = 0;
1181 }
1182
Bram Moolenaarae943152019-06-16 22:54:14 +02001183 for (i = 0; i < 4; ++i)
1184 VIM_CLEAR(wp->w_border_highlight[i]);
1185 for (i = 0; i < 8; ++i)
1186 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001187 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001188 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001189
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001190 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001191 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001192
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001193#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001194 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1195 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001196#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001197
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001198 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001199
1200 wp->w_vsep_width = 0;
1201
1202 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001203 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001204
1205 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001206}
1207
1208/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001209 * popup_clear()
1210 */
1211 void
1212f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1213{
1214 close_all_popups();
1215}
1216
1217/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001218 * popup_create({text}, {options})
1219 */
1220 void
1221f_popup_create(typval_T *argvars, typval_T *rettv)
1222{
Bram Moolenaar17627312019-06-02 19:53:44 +02001223 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001224}
1225
1226/*
1227 * popup_atcursor({text}, {options})
1228 */
1229 void
1230f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1231{
Bram Moolenaar17627312019-06-02 19:53:44 +02001232 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001233}
1234
1235/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001236 * Invoke the close callback for window "wp" with value "result".
1237 * Careful: The callback may make "wp" invalid!
1238 */
1239 static void
1240invoke_popup_callback(win_T *wp, typval_T *result)
1241{
1242 typval_T rettv;
1243 int dummy;
1244 typval_T argv[3];
1245
1246 argv[0].v_type = VAR_NUMBER;
1247 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1248
1249 if (result != NULL && result->v_type != VAR_UNKNOWN)
1250 copy_tv(result, &argv[1]);
1251 else
1252 {
1253 argv[1].v_type = VAR_NUMBER;
1254 argv[1].vval.v_number = 0;
1255 }
1256
1257 argv[2].v_type = VAR_UNKNOWN;
1258
1259 call_callback(&wp->w_close_cb, -1,
1260 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1261 if (result != NULL)
1262 clear_tv(&argv[1]);
1263 clear_tv(&rettv);
1264}
1265
1266/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001267 * Close popup "wp" and invoke any close callback for it.
1268 */
1269 static void
1270popup_close_and_callback(win_T *wp, typval_T *arg)
1271{
1272 int id = wp->w_id;
1273
1274 if (wp->w_close_cb.cb_name != NULL)
1275 // Careful: This may make "wp" invalid.
1276 invoke_popup_callback(wp, arg);
1277
1278 popup_close(id);
1279}
1280
1281/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001282 * Close popup "wp" because of a mouse click.
1283 */
1284 void
1285popup_close_for_mouse_click(win_T *wp)
1286{
1287 typval_T res;
1288
1289 res.v_type = VAR_NUMBER;
1290 res.vval.v_number = -2;
1291 popup_close_and_callback(wp, &res);
1292}
1293
1294/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001295 * In a filter: check if the typed key is a mouse event that is used for
1296 * dragging the popup.
1297 */
1298 static void
1299filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1300{
1301 int row = mouse_row;
1302 int col = mouse_col;
1303
1304 if (wp->w_popup_drag
1305 && is_mouse_key(c)
1306 && (wp == popup_dragwin
1307 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1308 // do not consume the key, allow for dragging the popup
1309 rettv->vval.v_number = 0;
1310}
1311
1312 static void
1313popup_highlight_curline(win_T *wp)
1314{
1315 int id;
1316 char buf[100];
1317
1318 match_delete(wp, 1, FALSE);
1319
1320 id = syn_name2id((char_u *)"PopupSelected");
1321 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1322 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1323 (char_u *)buf, 10, 1, NULL, NULL);
1324}
1325
1326/*
1327 * popup_filter_menu({text}, {options})
1328 */
1329 void
1330f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1331{
1332 int id = tv_get_number(&argvars[0]);
1333 win_T *wp = win_id2wp(id);
1334 char_u *key = tv_get_string(&argvars[1]);
1335 typval_T res;
1336 int c;
1337 linenr_T old_lnum;
1338
1339 // If the popup has been closed do not consume the key.
1340 if (wp == NULL)
1341 return;
1342
1343 c = *key;
1344 if (c == K_SPECIAL && key[1] != NUL)
1345 c = TO_SPECIAL(key[1], key[2]);
1346
1347 // consume all keys until done
1348 rettv->vval.v_number = 1;
1349 res.v_type = VAR_NUMBER;
1350
1351 old_lnum = wp->w_cursor.lnum;
1352 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1353 --wp->w_cursor.lnum;
1354 if ((c == 'j' || c == 'J' || c == K_DOWN)
1355 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1356 ++wp->w_cursor.lnum;
1357 if (old_lnum != wp->w_cursor.lnum)
1358 {
1359 popup_highlight_curline(wp);
1360 return;
1361 }
1362
1363 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1364 {
1365 // Cancelled, invoke callback with -1
1366 res.vval.v_number = -1;
1367 popup_close_and_callback(wp, &res);
1368 return;
1369 }
1370 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1371 {
1372 // Invoke callback with current index.
1373 res.vval.v_number = wp->w_cursor.lnum;
1374 popup_close_and_callback(wp, &res);
1375 return;
1376 }
1377
1378 filter_handle_drag(wp, c, rettv);
1379}
1380
1381/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001382 * popup_filter_yesno({text}, {options})
1383 */
1384 void
1385f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1386{
1387 int id = tv_get_number(&argvars[0]);
1388 win_T *wp = win_id2wp(id);
1389 char_u *key = tv_get_string(&argvars[1]);
1390 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001391 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001392
1393 // If the popup has been closed don't consume the key.
1394 if (wp == NULL)
1395 return;
1396
Bram Moolenaara730e552019-06-16 19:05:31 +02001397 c = *key;
1398 if (c == K_SPECIAL && key[1] != NUL)
1399 c = TO_SPECIAL(key[1], key[2]);
1400
Bram Moolenaara42d9452019-06-15 21:46:30 +02001401 // consume all keys until done
1402 rettv->vval.v_number = 1;
1403
Bram Moolenaara730e552019-06-16 19:05:31 +02001404 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001405 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001406 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001407 res.vval.v_number = 0;
1408 else
1409 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001410 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001411 return;
1412 }
1413
1414 // Invoke callback
1415 res.v_type = VAR_NUMBER;
1416 popup_close_and_callback(wp, &res);
1417}
1418
1419/*
1420 * popup_dialog({text}, {options})
1421 */
1422 void
1423f_popup_dialog(typval_T *argvars, typval_T *rettv)
1424{
1425 popup_create(argvars, rettv, TYPE_DIALOG);
1426}
1427
1428/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001429 * popup_menu({text}, {options})
1430 */
1431 void
1432f_popup_menu(typval_T *argvars, typval_T *rettv)
1433{
1434 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1435
1436 if (wp != NULL)
1437 popup_highlight_curline(wp);
1438}
1439
1440/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001441 * popup_notification({text}, {options})
1442 */
1443 void
1444f_popup_notification(typval_T *argvars, typval_T *rettv)
1445{
1446 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1447}
1448
1449/*
1450 * Find the popup window with window-ID "id".
1451 * If the popup window does not exist NULL is returned.
1452 * If the window is not a popup window, and error message is given.
1453 */
1454 static win_T *
1455find_popup_win(int id)
1456{
1457 win_T *wp = win_id2wp(id);
1458
1459 if (wp != NULL && !bt_popup(wp->w_buffer))
1460 {
1461 semsg(_("E993: window %d is not a popup window"), id);
1462 return NULL;
1463 }
1464 return wp;
1465}
1466
1467/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001468 * popup_close({id})
1469 */
1470 void
1471f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1472{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001473 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001474 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001475
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001476 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001477 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001478}
1479
1480/*
1481 * popup_hide({id})
1482 */
1483 void
1484f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1485{
1486 int id = (int)tv_get_number(argvars);
1487 win_T *wp = find_popup_win(id);
1488
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001489 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001490 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001491 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001492 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001493 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001494 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001495 }
1496}
1497
1498/*
1499 * popup_show({id})
1500 */
1501 void
1502f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1503{
1504 int id = (int)tv_get_number(argvars);
1505 win_T *wp = find_popup_win(id);
1506
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001507 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001508 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001509 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001510 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001511 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001512 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001513 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001514}
1515
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001516/*
1517 * popup_settext({id}, {text})
1518 */
1519 void
1520f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1521{
1522 int id = (int)tv_get_number(&argvars[0]);
1523 win_T *wp = find_popup_win(id);
1524
1525 if (wp != NULL)
1526 {
1527 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1528 popup_adjust_position(wp);
1529 }
1530}
1531
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001532 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001533popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001534{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001535 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001536 if (wp->w_winrow + wp->w_height >= cmdline_row)
1537 clear_cmdline = TRUE;
1538 win_free_popup(wp);
1539 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001540 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001541}
1542
Bram Moolenaarec583842019-05-26 14:11:23 +02001543/*
1544 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001545 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001546 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001547 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001548popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001549{
1550 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001551 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001552 win_T *prev = NULL;
1553
Bram Moolenaarec583842019-05-26 14:11:23 +02001554 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001555 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001556 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001557 {
1558 if (prev == NULL)
1559 first_popupwin = wp->w_next;
1560 else
1561 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001562 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001563 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001564 }
1565
Bram Moolenaarec583842019-05-26 14:11:23 +02001566 // go through tab-local popups
1567 FOR_ALL_TABPAGES(tp)
1568 popup_close_tabpage(tp, id);
1569}
1570
1571/*
1572 * Close a popup window with Window-id "id" in tabpage "tp".
1573 */
1574 void
1575popup_close_tabpage(tabpage_T *tp, int id)
1576{
1577 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001578 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001579 win_T *prev = NULL;
1580
Bram Moolenaarec583842019-05-26 14:11:23 +02001581 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1582 if (wp->w_id == id)
1583 {
1584 if (prev == NULL)
1585 *root = wp->w_next;
1586 else
1587 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001588 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001589 return;
1590 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001591}
1592
1593 void
1594close_all_popups(void)
1595{
1596 while (first_popupwin != NULL)
1597 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001598 while (curtab->tp_first_popupwin != NULL)
1599 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001600}
1601
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001602/*
1603 * popup_move({id}, {options})
1604 */
1605 void
1606f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1607{
Bram Moolenaarae943152019-06-16 22:54:14 +02001608 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001609 int id = (int)tv_get_number(argvars);
1610 win_T *wp = find_popup_win(id);
1611
1612 if (wp == NULL)
1613 return; // invalid {id}
1614
1615 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1616 {
1617 emsg(_(e_dictreq));
1618 return;
1619 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001620 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001621
Bram Moolenaarae943152019-06-16 22:54:14 +02001622 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001623
1624 if (wp->w_winrow + wp->w_height >= cmdline_row)
1625 clear_cmdline = TRUE;
1626 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001627}
1628
Bram Moolenaarbc133542019-05-29 20:26:46 +02001629/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001630 * popup_setoptions({id}, {options})
1631 */
1632 void
1633f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1634{
1635 dict_T *dict;
1636 int id = (int)tv_get_number(argvars);
1637 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001638 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001639
1640 if (wp == NULL)
1641 return; // invalid {id}
1642
1643 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1644 {
1645 emsg(_(e_dictreq));
1646 return;
1647 }
1648 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001649 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001650
1651 apply_move_options(wp, dict);
1652 apply_general_options(wp, dict);
1653
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001654 if (old_firstline != wp->w_firstline)
1655 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001656 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001657 popup_adjust_position(wp);
1658}
1659
1660/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001661 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001662 */
1663 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001664f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001665{
1666 dict_T *dict;
1667 int id = (int)tv_get_number(argvars);
1668 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001669 int top_extra;
1670 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001671
1672 if (rettv_dict_alloc(rettv) == OK)
1673 {
1674 if (wp == NULL)
1675 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001676 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001677 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1678
Bram Moolenaarbc133542019-05-29 20:26:46 +02001679 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001680
Bram Moolenaarbc133542019-05-29 20:26:46 +02001681 dict_add_number(dict, "line", wp->w_winrow + 1);
1682 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001683 dict_add_number(dict, "width", wp->w_width + left_extra
1684 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1685 dict_add_number(dict, "height", wp->w_height + top_extra
1686 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001687
1688 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1689 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1690 dict_add_number(dict, "core_width", wp->w_width);
1691 dict_add_number(dict, "core_height", wp->w_height);
1692
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001693 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001694 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001695 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001696 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001697 }
1698}
1699
1700/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001701 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1702 */
1703 static void
1704get_padding_border(dict_T *dict, int *array, char *name)
1705{
1706 list_T *list;
1707 int i;
1708
1709 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1710 return;
1711
1712 list = list_alloc();
1713 if (list != NULL)
1714 {
1715 dict_add_list(dict, name, list);
1716 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1717 for (i = 0; i < 4; ++i)
1718 list_append_number(list, array[i]);
1719 }
1720}
1721
1722/*
1723 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1724 */
1725 static void
1726get_borderhighlight(dict_T *dict, win_T *wp)
1727{
1728 list_T *list;
1729 int i;
1730
1731 for (i = 0; i < 4; ++i)
1732 if (wp->w_border_highlight[i] != NULL)
1733 break;
1734 if (i == 4)
1735 return;
1736
1737 list = list_alloc();
1738 if (list != NULL)
1739 {
1740 dict_add_list(dict, "borderhighlight", list);
1741 for (i = 0; i < 4; ++i)
1742 list_append_string(list, wp->w_border_highlight[i], -1);
1743 }
1744}
1745
1746/*
1747 * For popup_getoptions(): add a "borderchars" entry to "dict".
1748 */
1749 static void
1750get_borderchars(dict_T *dict, win_T *wp)
1751{
1752 list_T *list;
1753 int i;
1754 char_u buf[NUMBUFLEN];
1755 int len;
1756
1757 for (i = 0; i < 8; ++i)
1758 if (wp->w_border_char[i] != 0)
1759 break;
1760 if (i == 8)
1761 return;
1762
1763 list = list_alloc();
1764 if (list != NULL)
1765 {
1766 dict_add_list(dict, "borderchars", list);
1767 for (i = 0; i < 8; ++i)
1768 {
1769 len = mb_char2bytes(wp->w_border_char[i], buf);
1770 list_append_string(list, buf, len);
1771 }
1772 }
1773}
1774
1775/*
1776 * For popup_getoptions(): add a "moved" entry to "dict".
1777 */
1778 static void
1779get_moved_list(dict_T *dict, win_T *wp)
1780{
1781 list_T *list;
1782
1783 list = list_alloc();
1784 if (list != NULL)
1785 {
1786 dict_add_list(dict, "moved", list);
1787 list_append_number(list, wp->w_popup_mincol);
1788 list_append_number(list, wp->w_popup_maxcol);
1789 }
1790}
1791
1792/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001793 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001794 */
1795 void
1796f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1797{
1798 dict_T *dict;
1799 int id = (int)tv_get_number(argvars);
1800 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001801 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001802 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001803
1804 if (rettv_dict_alloc(rettv) == OK)
1805 {
1806 if (wp == NULL)
1807 return;
1808
1809 dict = rettv->vval.v_dict;
1810 dict_add_number(dict, "line", wp->w_wantline);
1811 dict_add_number(dict, "col", wp->w_wantcol);
1812 dict_add_number(dict, "minwidth", wp->w_minwidth);
1813 dict_add_number(dict, "minheight", wp->w_minheight);
1814 dict_add_number(dict, "maxheight", wp->w_maxheight);
1815 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001816 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001817 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001818 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001819 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001820 dict_add_string(dict, "title", wp->w_popup_title);
1821 dict_add_number(dict, "wrap", wp->w_p_wrap);
1822 dict_add_number(dict, "drag", wp->w_popup_drag);
1823 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02001824 if (wp->w_scrollbar_highlight != NULL)
1825 dict_add_string(dict, "scrollbarhighlight",
1826 wp->w_scrollbar_highlight);
1827 if (wp->w_thumb_highlight != NULL)
1828 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02001829
Bram Moolenaara3fce622019-06-20 02:31:49 +02001830 // find the tabpage that holds this popup
1831 i = 1;
1832 FOR_ALL_TABPAGES(tp)
1833 {
1834 win_T *p;
1835
1836 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1837 if (p->w_id == id)
1838 break;
1839 if (p != NULL)
1840 break;
1841 ++i;
1842 }
1843 if (tp == NULL)
1844 i = -1; // must be global
1845 else if (tp == curtab)
1846 i = 0;
1847 dict_add_number(dict, "tabpage", i);
1848
Bram Moolenaarae943152019-06-16 22:54:14 +02001849 get_padding_border(dict, wp->w_popup_padding, "padding");
1850 get_padding_border(dict, wp->w_popup_border, "border");
1851 get_borderhighlight(dict, wp);
1852 get_borderchars(dict, wp);
1853 get_moved_list(dict, wp);
1854
1855 if (wp->w_filter_cb.cb_name != NULL)
1856 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1857 if (wp->w_close_cb.cb_name != NULL)
1858 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001859
1860 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1861 ++i)
1862 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1863 {
1864 dict_add_string(dict, "pos",
1865 (char_u *)poppos_entries[i].pp_name);
1866 break;
1867 }
1868
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001869 dict_add_string(dict, "close", (char_u *)(
1870 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
1871 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
1872
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001873# if defined(FEAT_TIMERS)
1874 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1875 ? (long)wp->w_popup_timer->tr_interval : 0L);
1876# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001877 }
1878}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001879
1880 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001881error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001882{
1883 if (bt_popup(curwin->w_buffer))
1884 {
1885 emsg(_("E994: Not allowed in a popup window"));
1886 return TRUE;
1887 }
1888 return FALSE;
1889}
1890
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001891/*
1892 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001893 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001894 */
1895 void
1896popup_reset_handled()
1897{
1898 win_T *wp;
1899
1900 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1901 wp->w_popup_flags &= ~POPF_HANDLED;
1902 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1903 wp->w_popup_flags &= ~POPF_HANDLED;
1904}
1905
1906/*
1907 * Find the next visible popup where POPF_HANDLED is not set.
1908 * Must have called popup_reset_handled() first.
1909 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1910 * popup with the highest zindex.
1911 */
1912 win_T *
1913find_next_popup(int lowest)
1914{
1915 win_T *wp;
1916 win_T *found_wp;
1917 int found_zindex;
1918
1919 found_zindex = lowest ? INT_MAX : 0;
1920 found_wp = NULL;
1921 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1922 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1923 && (lowest ? wp->w_zindex < found_zindex
1924 : wp->w_zindex > found_zindex))
1925 {
1926 found_zindex = wp->w_zindex;
1927 found_wp = wp;
1928 }
1929 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1930 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1931 && (lowest ? wp->w_zindex < found_zindex
1932 : wp->w_zindex > found_zindex))
1933 {
1934 found_zindex = wp->w_zindex;
1935 found_wp = wp;
1936 }
1937
1938 if (found_wp != NULL)
1939 found_wp->w_popup_flags |= POPF_HANDLED;
1940 return found_wp;
1941}
1942
1943/*
1944 * Invoke the filter callback for window "wp" with typed character "c".
1945 * Uses the global "mod_mask" for modifiers.
1946 * Returns the return value of the filter.
1947 * Careful: The filter may make "wp" invalid!
1948 */
1949 static int
1950invoke_popup_filter(win_T *wp, int c)
1951{
1952 int res;
1953 typval_T rettv;
1954 int dummy;
1955 typval_T argv[3];
1956 char_u buf[NUMBUFLEN];
1957
Bram Moolenaara42d9452019-06-15 21:46:30 +02001958 // Emergency exit: CTRL-C closes the popup.
1959 if (c == Ctrl_C)
1960 {
1961 rettv.v_type = VAR_NUMBER;
1962 rettv.vval.v_number = -1;
1963 popup_close_and_callback(wp, &rettv);
1964 return 1;
1965 }
1966
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001967 argv[0].v_type = VAR_NUMBER;
1968 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1969
1970 // Convert the number to a string, so that the function can use:
1971 // if a:c == "\<F2>"
1972 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1973 argv[1].v_type = VAR_STRING;
1974 argv[1].vval.v_string = vim_strsave(buf);
1975
1976 argv[2].v_type = VAR_UNKNOWN;
1977
Bram Moolenaara42d9452019-06-15 21:46:30 +02001978 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001979 call_callback(&wp->w_filter_cb, -1,
1980 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1981 res = tv_get_number(&rettv);
1982 vim_free(argv[1].vval.v_string);
1983 clear_tv(&rettv);
1984 return res;
1985}
1986
1987/*
1988 * Called when "c" was typed: invoke popup filter callbacks.
1989 * Returns TRUE when the character was consumed,
1990 */
1991 int
1992popup_do_filter(int c)
1993{
1994 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001995 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001996
1997 popup_reset_handled();
1998
1999 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2000 if (wp->w_filter_cb.cb_name != NULL)
2001 res = invoke_popup_filter(wp, c);
2002
2003 return res;
2004}
2005
Bram Moolenaar3397f742019-06-02 18:40:06 +02002006/*
2007 * Called when the cursor moved: check if any popup needs to be closed if the
2008 * cursor moved far enough.
2009 */
2010 void
2011popup_check_cursor_pos()
2012{
2013 win_T *wp;
2014 typval_T tv;
2015
2016 popup_reset_handled();
2017 while ((wp = find_next_popup(TRUE)) != NULL)
2018 if (wp->w_popup_curwin != NULL
2019 && (curwin != wp->w_popup_curwin
2020 || curwin->w_cursor.lnum != wp->w_popup_lnum
2021 || curwin->w_cursor.col < wp->w_popup_mincol
2022 || curwin->w_cursor.col > wp->w_popup_maxcol))
2023 {
2024 tv.v_type = VAR_NUMBER;
2025 tv.vval.v_number = -1;
2026 popup_close_and_callback(wp, &tv);
2027 }
2028}
2029
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002030/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002031 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2032 * "col" and "line" are screen coordinates.
2033 */
2034 static int
2035popup_masked(win_T *wp, int screencol, int screenline)
2036{
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002037 int col = screencol - wp->w_wincol + 1 + wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002038 int line = screenline - wp->w_winrow + 1;
2039 listitem_T *lio, *li;
2040 int width, height;
2041
2042 if (wp->w_popup_mask == NULL)
2043 return FALSE;
2044 width = popup_width(wp);
2045 height = popup_height(wp);
2046
2047 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2048 {
2049 int cols, cole;
2050 int lines, linee;
2051
2052 li = lio->li_tv.vval.v_list->lv_first;
2053 cols = tv_get_number(&li->li_tv);
2054 if (cols < 0)
2055 cols = width + cols + 1;
2056 if (col < cols)
2057 continue;
2058 li = li->li_next;
2059 cole = tv_get_number(&li->li_tv);
2060 if (cole < 0)
2061 cole = width + cole + 1;
2062 if (col > cole)
2063 continue;
2064 li = li->li_next;
2065 lines = tv_get_number(&li->li_tv);
2066 if (lines < 0)
2067 lines = height + lines + 1;
2068 if (line < lines)
2069 continue;
2070 li = li->li_next;
2071 linee = tv_get_number(&li->li_tv);
2072 if (linee < 0)
2073 linee = height + linee + 1;
2074 if (line > linee)
2075 continue;
2076 return TRUE;
2077 }
2078 return FALSE;
2079}
2080
2081/*
2082 * Set flags in popup_transparent[] for window "wp" to "val".
2083 */
2084 static void
2085update_popup_transparent(win_T *wp, int val)
2086{
2087 if (wp->w_popup_mask != NULL)
2088 {
2089 int width = popup_width(wp);
2090 int height = popup_height(wp);
2091 listitem_T *lio, *li;
2092 int cols, cole;
2093 int lines, linee;
2094 int col, line;
2095
2096 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2097 {
2098 li = lio->li_tv.vval.v_list->lv_first;
2099 cols = tv_get_number(&li->li_tv);
2100 if (cols < 0)
2101 cols = width + cols + 1;
2102 li = li->li_next;
2103 cole = tv_get_number(&li->li_tv);
2104 if (cole < 0)
2105 cole = width + cole + 1;
2106 li = li->li_next;
2107 lines = tv_get_number(&li->li_tv);
2108 if (lines < 0)
2109 lines = height + lines + 1;
2110 li = li->li_next;
2111 linee = tv_get_number(&li->li_tv);
2112 if (linee < 0)
2113 linee = height + linee + 1;
2114
2115 --cols;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002116 cols -= wp->w_leftcol;
2117 if (cols < 0)
2118 cols = 0;
2119 cole -= wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002120 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002121 if (lines < 0)
2122 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002123 for (line = lines; line < linee && line < screen_Rows; ++line)
2124 for (col = cols; col < cole && col < screen_Columns; ++col)
2125 popup_transparent[(line + wp->w_winrow) * screen_Columns
2126 + col + wp->w_wincol] = val;
2127 }
2128 }
2129}
2130
2131/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002132 * Update "popup_mask" if needed.
2133 * Also recomputes the popup size and positions.
2134 * Also updates "popup_visible".
2135 * Also marks window lines for redrawing.
2136 */
2137 void
2138may_update_popup_mask(int type)
2139{
2140 win_T *wp;
2141 short *mask;
2142 int line, col;
2143 int redraw_all = FALSE;
2144
2145 // Need to recompute when switching tabs.
2146 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2147 // (such as the screen size) must have changed.
2148 if (popup_mask_tab != curtab || type >= NOT_VALID)
2149 {
2150 popup_mask_refresh = TRUE;
2151 redraw_all = TRUE;
2152 }
2153 if (!popup_mask_refresh)
2154 {
2155 // Check if any buffer has changed.
2156 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2157 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2158 popup_mask_refresh = TRUE;
2159 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2160 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2161 popup_mask_refresh = TRUE;
2162 if (!popup_mask_refresh)
2163 return;
2164 }
2165
2166 // Need to update the mask, something has changed.
2167 popup_mask_refresh = FALSE;
2168 popup_mask_tab = curtab;
2169 popup_visible = FALSE;
2170
2171 // If redrawing everything, just update "popup_mask".
2172 // If redrawing only what is needed, update "popup_mask_next" and then
2173 // compare with "popup_mask" to see what changed.
2174 if (type >= SOME_VALID)
2175 mask = popup_mask;
2176 else
2177 mask = popup_mask_next;
2178 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2179
2180 // Find the window with the lowest zindex that hasn't been handled yet,
2181 // so that the window with a higher zindex overwrites the value in
2182 // popup_mask.
2183 popup_reset_handled();
2184 while ((wp = find_next_popup(TRUE)) != NULL)
2185 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002186 int height = popup_height(wp);
2187 int width = popup_width(wp);
2188
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002189 popup_visible = TRUE;
2190
2191 // Recompute the position if the text changed.
2192 if (redraw_all
2193 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2194 popup_adjust_position(wp);
2195
2196 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002197 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002198 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002199 col < wp->w_wincol + width && col < screen_Columns; ++col)
2200 if (!popup_masked(wp, col, line))
2201 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002202 }
2203
2204 // Only check which lines are to be updated if not already
2205 // updating all lines.
2206 if (mask == popup_mask_next)
2207 for (line = 0; line < screen_Rows; ++line)
2208 {
2209 int col_done = 0;
2210
2211 for (col = 0; col < screen_Columns; ++col)
2212 {
2213 int off = line * screen_Columns + col;
2214
2215 if (popup_mask[off] != popup_mask_next[off])
2216 {
2217 popup_mask[off] = popup_mask_next[off];
2218
2219 if (line >= cmdline_row)
2220 {
2221 // the command line needs to be cleared if text below
2222 // the popup is now visible.
2223 if (!msg_scrolled && popup_mask_next[off] == 0)
2224 clear_cmdline = TRUE;
2225 }
2226 else if (col >= col_done)
2227 {
2228 linenr_T lnum;
2229 int line_cp = line;
2230 int col_cp = col;
2231
2232 // The screen position "line" / "col" needs to be
2233 // redrawn. Figure out what window that is and update
2234 // w_redraw_top and w_redr_bot. Only needs to be done
2235 // once for each window line.
2236 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2237 if (wp != NULL)
2238 {
2239 if (line_cp >= wp->w_height)
2240 // In (or below) status line
2241 wp->w_redr_status = TRUE;
2242 // compute the position in the buffer line from the
2243 // position on the screen
2244 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2245 &lnum))
2246 // past bottom
2247 wp->w_redr_status = TRUE;
2248 else
2249 redrawWinline(wp, lnum);
2250
2251 // This line is going to be redrawn, no need to
2252 // check until the right side of the window.
2253 col_done = wp->w_wincol + wp->w_width - 1;
2254 }
2255 }
2256 }
2257 }
2258 }
2259}
2260
2261/*
2262 * Return a string of "len" spaces in IObuff.
2263 */
2264 static char_u *
2265get_spaces(int len)
2266{
2267 vim_memset(IObuff, ' ', (size_t)len);
2268 IObuff[len] = NUL;
2269 return IObuff;
2270}
2271
2272/*
2273 * Update popup windows. They are drawn on top of normal windows.
2274 * "win_update" is called for each popup window, lowest zindex first.
2275 */
2276 void
2277update_popups(void (*win_update)(win_T *wp))
2278{
2279 win_T *wp;
2280 int top_off;
2281 int left_off;
2282 int total_width;
2283 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002284 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002285 int popup_attr;
2286 int border_attr[4];
2287 int border_char[8];
2288 char_u buf[MB_MAXBYTES];
2289 int row;
2290 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002291 int sb_thumb_top = 0;
2292 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002293 int attr_scroll = 0;
2294 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002295
2296 // Find the window with the lowest zindex that hasn't been updated yet,
2297 // so that the window with a higher zindex is drawn later, thus goes on
2298 // top.
2299 popup_reset_handled();
2300 while ((wp = find_next_popup(TRUE)) != NULL)
2301 {
2302 // This drawing uses the zindex of the popup window, so that it's on
2303 // top of the text but doesn't draw when another popup with higher
2304 // zindex is on top of the character.
2305 screen_zindex = wp->w_zindex;
2306
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002307 // Set flags in popup_transparent[] for masked cells.
2308 update_popup_transparent(wp, 1);
2309
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002310 // adjust w_winrow and w_wincol for border and padding, since
2311 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002312 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002313 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2314 wp->w_winrow += top_off;
2315 wp->w_wincol += left_off;
2316
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002317 // Draw the popup text, unless it's off screen.
2318 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2319 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002320
2321 wp->w_winrow -= top_off;
2322 wp->w_wincol -= left_off;
2323
2324 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002325 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1]
2326 + wp->w_has_scrollbar;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002327 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002328 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2329 popup_attr = get_wcr_attr(wp);
2330
2331 // We can only use these line drawing characters when 'encoding' is
2332 // "utf-8" and 'ambiwidth' is "single".
2333 if (enc_utf8 && *p_ambw == 's')
2334 {
2335 border_char[0] = border_char[2] = 0x2550;
2336 border_char[1] = border_char[3] = 0x2551;
2337 border_char[4] = 0x2554;
2338 border_char[5] = 0x2557;
2339 border_char[6] = 0x255d;
2340 border_char[7] = 0x255a;
2341 }
2342 else
2343 {
2344 border_char[0] = border_char[2] = '-';
2345 border_char[1] = border_char[3] = '|';
2346 for (i = 4; i < 8; ++i)
2347 border_char[i] = '+';
2348 }
2349 for (i = 0; i < 8; ++i)
2350 if (wp->w_border_char[i] != 0)
2351 border_char[i] = wp->w_border_char[i];
2352
2353 for (i = 0; i < 4; ++i)
2354 {
2355 border_attr[i] = popup_attr;
2356 if (wp->w_border_highlight[i] != NULL)
2357 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2358 }
2359
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002360 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002361 if (wp->w_popup_border[0] > 0)
2362 {
2363 // top border
2364 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2365 wp->w_wincol,
2366 wp->w_wincol + total_width,
2367 wp->w_popup_border[3] != 0
2368 ? border_char[4] : border_char[0],
2369 border_char[0], border_attr[0]);
2370 if (wp->w_popup_border[1] > 0)
2371 {
2372 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2373 screen_puts(buf, wp->w_winrow,
2374 wp->w_wincol + total_width - 1, border_attr[1]);
2375 }
2376 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002377 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2378 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002379
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002380 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002381 {
2382 // top padding
2383 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002384 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002385 wp->w_wincol + wp->w_popup_border[3],
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002386 wp->w_wincol + total_width - wp->w_popup_border[1]
2387 - wp->w_has_scrollbar,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002388 ' ', ' ', popup_attr);
2389 }
2390
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002391 // Title goes on top of border or padding.
2392 if (wp->w_popup_title != NULL)
2393 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2394 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2395
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002396 // Compute scrollbar thumb position and size.
2397 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002398 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002399 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2400
Bram Moolenaar68acb412019-06-26 03:40:36 +02002401 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2402 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002403 if (sb_thumb_height == 0)
2404 sb_thumb_height = 1;
Bram Moolenaar68acb412019-06-26 03:40:36 +02002405 sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2)
2406 * (wp->w_height - sb_thumb_height)
2407 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002408 if (wp->w_scrollbar_highlight != NULL)
2409 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2410 else
2411 attr_scroll = highlight_attr[HLF_PSB];
2412 if (wp->w_thumb_highlight != NULL)
2413 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2414 else
2415 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002416 }
2417
2418 for (i = wp->w_popup_border[0];
2419 i < total_height - wp->w_popup_border[2]; ++i)
2420 {
2421 row = wp->w_winrow + i;
2422
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002423 // left border
2424 if (wp->w_popup_border[3] > 0)
2425 {
2426 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2427 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2428 }
2429 // left padding
2430 if (wp->w_popup_padding[3] > 0)
2431 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2432 wp->w_wincol + wp->w_popup_border[3], popup_attr);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002433 // scrollbar
2434 if (wp->w_has_scrollbar)
2435 {
2436 int line = i - top_off;
2437 int scroll_col = wp->w_wincol + total_width - 1
2438 - wp->w_popup_border[1];
2439
2440 if (line >= 0 && line < wp->w_height)
2441 screen_putchar(' ', row, scroll_col,
2442 line >= sb_thumb_top
2443 && line < sb_thumb_top + sb_thumb_height
2444 ? attr_thumb : attr_scroll);
2445 else
2446 screen_putchar(' ', row, scroll_col, popup_attr);
2447 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002448 // right border
2449 if (wp->w_popup_border[1] > 0)
2450 {
2451 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2452 screen_puts(buf, row,
2453 wp->w_wincol + total_width - 1, border_attr[1]);
2454 }
2455 // right padding
2456 if (wp->w_popup_padding[1] > 0)
2457 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2458 wp->w_wincol + wp->w_popup_border[3]
2459 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2460 }
2461
2462 if (wp->w_popup_padding[2] > 0)
2463 {
2464 // bottom padding
2465 row = wp->w_winrow + wp->w_popup_border[0]
2466 + wp->w_popup_padding[0] + wp->w_height;
2467 screen_fill(row, row + wp->w_popup_padding[2],
2468 wp->w_wincol + wp->w_popup_border[3],
2469 wp->w_wincol + total_width - wp->w_popup_border[1],
2470 ' ', ' ', popup_attr);
2471 }
2472
2473 if (wp->w_popup_border[2] > 0)
2474 {
2475 // bottom border
2476 row = wp->w_winrow + total_height - 1;
2477 screen_fill(row , row + 1,
2478 wp->w_wincol,
2479 wp->w_wincol + total_width,
2480 wp->w_popup_border[3] != 0
2481 ? border_char[7] : border_char[2],
2482 border_char[2], border_attr[2]);
2483 if (wp->w_popup_border[1] > 0)
2484 {
2485 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2486 screen_puts(buf, row,
2487 wp->w_wincol + total_width - 1, border_attr[2]);
2488 }
2489 }
2490
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002491 if (wp->w_popup_close == POPCLOSE_BUTTON)
2492 {
2493 // close button goes on top of anything at the top-right corner
2494 buf[mb_char2bytes('X', buf)] = NUL;
2495 screen_puts(buf, wp->w_winrow, wp->w_wincol + total_width - 1,
2496 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2497 }
2498
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002499 update_popup_transparent(wp, 0);
2500
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002501 // Back to the normal zindex.
2502 screen_zindex = 0;
2503 }
2504}
2505
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002506/*
2507 * Mark references in callbacks of one popup window.
2508 */
2509 static int
2510set_ref_in_one_popup(win_T *wp, int copyID)
2511{
2512 int abort = FALSE;
2513 typval_T tv;
2514
2515 if (wp->w_close_cb.cb_partial != NULL)
2516 {
2517 tv.v_type = VAR_PARTIAL;
2518 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2519 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2520 }
2521 if (wp->w_filter_cb.cb_partial != NULL)
2522 {
2523 tv.v_type = VAR_PARTIAL;
2524 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2525 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2526 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002527 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002528 return abort;
2529}
2530
2531/*
2532 * Set reference in callbacks of popup windows.
2533 */
2534 int
2535set_ref_in_popups(int copyID)
2536{
2537 int abort = FALSE;
2538 win_T *wp;
2539 tabpage_T *tp;
2540
2541 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2542 abort = abort || set_ref_in_one_popup(wp, copyID);
2543
2544 FOR_ALL_TABPAGES(tp)
2545 {
2546 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2547 abort = abort || set_ref_in_one_popup(wp, copyID);
2548 if (abort)
2549 break;
2550 }
2551 return abort;
2552}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002553#endif // FEAT_TEXT_PROP