blob: d988cbaf9b6212f80dffad4d5efa0cb431b0313d [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;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001000 int new_buffer;
1001 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001002 dict_T *d;
1003 int nr;
1004 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001005
1006 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001007 if (argvars[0].v_type == VAR_NUMBER)
1008 {
1009 buf = buflist_findnr( argvars[0].vval.v_number);
1010 if (buf == NULL)
1011 {
1012 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1013 return NULL;
1014 }
1015 }
1016 else if (!(argvars[0].v_type == VAR_STRING
1017 && argvars[0].vval.v_string != NULL)
1018 && !(argvars[0].v_type == VAR_LIST
1019 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001020 {
1021 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001022 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001023 }
1024 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1025 {
1026 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001027 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001028 }
1029 d = argvars[1].vval.v_dict;
1030
Bram Moolenaara3fce622019-06-20 02:31:49 +02001031 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1032 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1033 else if (type == TYPE_NOTIFICATION)
1034 tabnr = -1; // notifications are global by default
1035 else
1036 tabnr = 0;
1037 if (tabnr > 0)
1038 {
1039 tp = find_tabpage(tabnr);
1040 if (tp == NULL)
1041 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001042 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001043 return NULL;
1044 }
1045 }
1046
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001047 // Create the window and buffer.
1048 wp = win_alloc_popup_win();
1049 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001050 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001051 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001052 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001053 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001054
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001055 if (buf != NULL)
1056 {
1057 // use existing buffer
1058 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001059 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001060 buffer_ensure_loaded(buf);
1061 }
1062 else
1063 {
1064 // create a new buffer associated with the popup
1065 new_buffer = TRUE;
1066 buf = buflist_new(NULL, NULL, (linenr_T)0,
1067 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1068 if (buf == NULL)
1069 return NULL;
1070 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001071
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001072 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001073
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001074 set_local_options_default(wp);
1075 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001076 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001077 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1078 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1079 buf->b_p_ul = -1; // no undo
1080 buf->b_p_swf = FALSE; // no swap file
1081 buf->b_p_bl = FALSE; // unlisted buffer
1082 buf->b_locked = TRUE;
1083 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001084
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001085 // Avoid that 'buftype' is reset when this buffer is entered.
1086 buf->b_p_initialized = TRUE;
1087 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001088
Bram Moolenaara3fce622019-06-20 02:31:49 +02001089 if (tp != NULL)
1090 {
1091 // popup on specified tab page
1092 wp->w_next = tp->tp_first_popupwin;
1093 tp->tp_first_popupwin = wp;
1094 }
1095 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001096 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001097 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001098 wp->w_next = curtab->tp_first_popupwin;
1099 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001100 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001101 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001102 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001103 win_T *prev = first_popupwin;
1104
1105 // Global popup: add at the end, so that it gets displayed on top of
1106 // older ones with the same zindex. Matters for notifications.
1107 if (first_popupwin == NULL)
1108 first_popupwin = wp;
1109 else
1110 {
1111 while (prev->w_next != NULL)
1112 prev = prev->w_next;
1113 prev->w_next = wp;
1114 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001115 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001116
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001117 if (new_buffer)
1118 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001119
Bram Moolenaar17627312019-06-02 19:53:44 +02001120 if (type == TYPE_ATCURSOR)
1121 {
1122 wp->w_popup_pos = POPPOS_BOTLEFT;
1123 setcursor_mayforce(TRUE);
1124 wp->w_wantline = screen_screenrow();
1125 if (wp->w_wantline == 0) // cursor in first line
1126 {
1127 wp->w_wantline = 2;
1128 wp->w_popup_pos = POPPOS_TOPLEFT;
1129 }
1130 wp->w_wantcol = screen_screencol() + 1;
1131 set_moved_values(wp);
1132 set_moved_columns(wp, FIND_STRING);
1133 }
1134
Bram Moolenaar33796b32019-06-08 16:01:13 +02001135 // set default values
1136 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001137 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001138
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001139 if (type == TYPE_NOTIFICATION)
1140 {
1141 win_T *twp, *nextwin;
1142 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001143
1144 // Try to not overlap with another global popup. Guess we need 3
1145 // more screen lines than buffer lines.
1146 wp->w_wantline = 1;
1147 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1148 {
1149 nextwin = twp->w_next;
1150 if (twp != wp
1151 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1152 && twp->w_winrow <= wp->w_wantline - 1 + height
1153 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1154 {
1155 // move to below this popup and restart the loop to check for
1156 // overlap with other popups
1157 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1158 nextwin = first_popupwin;
1159 }
1160 }
1161 if (wp->w_wantline + height > Rows)
1162 {
1163 // can't avoid overlap, put on top in the hope that message goes
1164 // away soon.
1165 wp->w_wantline = 1;
1166 }
1167
1168 wp->w_wantcol = 10;
1169 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001170 wp->w_minwidth = 20;
1171 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001172 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001173 for (i = 0; i < 4; ++i)
1174 wp->w_popup_border[i] = 1;
1175 wp->w_popup_padding[1] = 1;
1176 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001177
1178 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001179 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001180 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1181 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001182 }
1183
Bram Moolenaara730e552019-06-16 19:05:31 +02001184 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001185 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001186 wp->w_popup_pos = POPPOS_CENTER;
1187 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1188 wp->w_popup_drag = 1;
1189 for (i = 0; i < 4; ++i)
1190 {
1191 wp->w_popup_border[i] = 1;
1192 wp->w_popup_padding[i] = 1;
1193 }
1194 }
1195
Bram Moolenaara730e552019-06-16 19:05:31 +02001196 if (type == TYPE_MENU)
1197 {
1198 typval_T tv;
1199 callback_T callback;
1200
1201 tv.v_type = VAR_STRING;
1202 tv.vval.v_string = (char_u *)"popup_filter_menu";
1203 callback = get_callback(&tv);
1204 if (callback.cb_name != NULL)
1205 set_callback(&wp->w_filter_cb, &callback);
1206
1207 wp->w_p_wrap = 0;
1208 }
1209
Bram Moolenaarae943152019-06-16 22:54:14 +02001210 for (i = 0; i < 4; ++i)
1211 VIM_CLEAR(wp->w_border_highlight[i]);
1212 for (i = 0; i < 8; ++i)
1213 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001214 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001215 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001216
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001217 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001218 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001219
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001220#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001221 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1222 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001223#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001224
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001225 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001226
1227 wp->w_vsep_width = 0;
1228
1229 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001230 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001231
1232 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001233}
1234
1235/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001236 * popup_clear()
1237 */
1238 void
1239f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1240{
1241 close_all_popups();
1242}
1243
1244/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001245 * popup_create({text}, {options})
1246 */
1247 void
1248f_popup_create(typval_T *argvars, typval_T *rettv)
1249{
Bram Moolenaar17627312019-06-02 19:53:44 +02001250 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001251}
1252
1253/*
1254 * popup_atcursor({text}, {options})
1255 */
1256 void
1257f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1258{
Bram Moolenaar17627312019-06-02 19:53:44 +02001259 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001260}
1261
1262/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001263 * Invoke the close callback for window "wp" with value "result".
1264 * Careful: The callback may make "wp" invalid!
1265 */
1266 static void
1267invoke_popup_callback(win_T *wp, typval_T *result)
1268{
1269 typval_T rettv;
1270 int dummy;
1271 typval_T argv[3];
1272
1273 argv[0].v_type = VAR_NUMBER;
1274 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1275
1276 if (result != NULL && result->v_type != VAR_UNKNOWN)
1277 copy_tv(result, &argv[1]);
1278 else
1279 {
1280 argv[1].v_type = VAR_NUMBER;
1281 argv[1].vval.v_number = 0;
1282 }
1283
1284 argv[2].v_type = VAR_UNKNOWN;
1285
1286 call_callback(&wp->w_close_cb, -1,
1287 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1288 if (result != NULL)
1289 clear_tv(&argv[1]);
1290 clear_tv(&rettv);
1291}
1292
1293/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001294 * Close popup "wp" and invoke any close callback for it.
1295 */
1296 static void
1297popup_close_and_callback(win_T *wp, typval_T *arg)
1298{
1299 int id = wp->w_id;
1300
1301 if (wp->w_close_cb.cb_name != NULL)
1302 // Careful: This may make "wp" invalid.
1303 invoke_popup_callback(wp, arg);
1304
1305 popup_close(id);
1306}
1307
1308/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001309 * Close popup "wp" because of a mouse click.
1310 */
1311 void
1312popup_close_for_mouse_click(win_T *wp)
1313{
1314 typval_T res;
1315
1316 res.v_type = VAR_NUMBER;
1317 res.vval.v_number = -2;
1318 popup_close_and_callback(wp, &res);
1319}
1320
1321/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001322 * In a filter: check if the typed key is a mouse event that is used for
1323 * dragging the popup.
1324 */
1325 static void
1326filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1327{
1328 int row = mouse_row;
1329 int col = mouse_col;
1330
1331 if (wp->w_popup_drag
1332 && is_mouse_key(c)
1333 && (wp == popup_dragwin
1334 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1335 // do not consume the key, allow for dragging the popup
1336 rettv->vval.v_number = 0;
1337}
1338
1339 static void
1340popup_highlight_curline(win_T *wp)
1341{
1342 int id;
1343 char buf[100];
1344
1345 match_delete(wp, 1, FALSE);
1346
1347 id = syn_name2id((char_u *)"PopupSelected");
1348 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1349 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1350 (char_u *)buf, 10, 1, NULL, NULL);
1351}
1352
1353/*
1354 * popup_filter_menu({text}, {options})
1355 */
1356 void
1357f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1358{
1359 int id = tv_get_number(&argvars[0]);
1360 win_T *wp = win_id2wp(id);
1361 char_u *key = tv_get_string(&argvars[1]);
1362 typval_T res;
1363 int c;
1364 linenr_T old_lnum;
1365
1366 // If the popup has been closed do not consume the key.
1367 if (wp == NULL)
1368 return;
1369
1370 c = *key;
1371 if (c == K_SPECIAL && key[1] != NUL)
1372 c = TO_SPECIAL(key[1], key[2]);
1373
1374 // consume all keys until done
1375 rettv->vval.v_number = 1;
1376 res.v_type = VAR_NUMBER;
1377
1378 old_lnum = wp->w_cursor.lnum;
1379 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1380 --wp->w_cursor.lnum;
1381 if ((c == 'j' || c == 'J' || c == K_DOWN)
1382 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1383 ++wp->w_cursor.lnum;
1384 if (old_lnum != wp->w_cursor.lnum)
1385 {
1386 popup_highlight_curline(wp);
1387 return;
1388 }
1389
1390 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1391 {
1392 // Cancelled, invoke callback with -1
1393 res.vval.v_number = -1;
1394 popup_close_and_callback(wp, &res);
1395 return;
1396 }
1397 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1398 {
1399 // Invoke callback with current index.
1400 res.vval.v_number = wp->w_cursor.lnum;
1401 popup_close_and_callback(wp, &res);
1402 return;
1403 }
1404
1405 filter_handle_drag(wp, c, rettv);
1406}
1407
1408/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001409 * popup_filter_yesno({text}, {options})
1410 */
1411 void
1412f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1413{
1414 int id = tv_get_number(&argvars[0]);
1415 win_T *wp = win_id2wp(id);
1416 char_u *key = tv_get_string(&argvars[1]);
1417 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001418 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001419
1420 // If the popup has been closed don't consume the key.
1421 if (wp == NULL)
1422 return;
1423
Bram Moolenaara730e552019-06-16 19:05:31 +02001424 c = *key;
1425 if (c == K_SPECIAL && key[1] != NUL)
1426 c = TO_SPECIAL(key[1], key[2]);
1427
Bram Moolenaara42d9452019-06-15 21:46:30 +02001428 // consume all keys until done
1429 rettv->vval.v_number = 1;
1430
Bram Moolenaara730e552019-06-16 19:05:31 +02001431 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001432 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001433 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001434 res.vval.v_number = 0;
1435 else
1436 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001437 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001438 return;
1439 }
1440
1441 // Invoke callback
1442 res.v_type = VAR_NUMBER;
1443 popup_close_and_callback(wp, &res);
1444}
1445
1446/*
1447 * popup_dialog({text}, {options})
1448 */
1449 void
1450f_popup_dialog(typval_T *argvars, typval_T *rettv)
1451{
1452 popup_create(argvars, rettv, TYPE_DIALOG);
1453}
1454
1455/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001456 * popup_menu({text}, {options})
1457 */
1458 void
1459f_popup_menu(typval_T *argvars, typval_T *rettv)
1460{
1461 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1462
1463 if (wp != NULL)
1464 popup_highlight_curline(wp);
1465}
1466
1467/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001468 * popup_notification({text}, {options})
1469 */
1470 void
1471f_popup_notification(typval_T *argvars, typval_T *rettv)
1472{
1473 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1474}
1475
1476/*
1477 * Find the popup window with window-ID "id".
1478 * If the popup window does not exist NULL is returned.
1479 * If the window is not a popup window, and error message is given.
1480 */
1481 static win_T *
1482find_popup_win(int id)
1483{
1484 win_T *wp = win_id2wp(id);
1485
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001486 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001487 {
1488 semsg(_("E993: window %d is not a popup window"), id);
1489 return NULL;
1490 }
1491 return wp;
1492}
1493
1494/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001495 * popup_close({id})
1496 */
1497 void
1498f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1499{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001500 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001501 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001502
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001503 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001504 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001505}
1506
1507/*
1508 * popup_hide({id})
1509 */
1510 void
1511f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1512{
1513 int id = (int)tv_get_number(argvars);
1514 win_T *wp = find_popup_win(id);
1515
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001516 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001517 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001518 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001519 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001520 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001521 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001522 }
1523}
1524
1525/*
1526 * popup_show({id})
1527 */
1528 void
1529f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1530{
1531 int id = (int)tv_get_number(argvars);
1532 win_T *wp = find_popup_win(id);
1533
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001534 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001535 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001536 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001537 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001538 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001539 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001540 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001541}
1542
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001543/*
1544 * popup_settext({id}, {text})
1545 */
1546 void
1547f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1548{
1549 int id = (int)tv_get_number(&argvars[0]);
1550 win_T *wp = find_popup_win(id);
1551
1552 if (wp != NULL)
1553 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001554 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1555 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1556 else
1557 {
1558 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1559 popup_adjust_position(wp);
1560 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001561 }
1562}
1563
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001564 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001565popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001566{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001567 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001568 if (wp->w_winrow + wp->w_height >= cmdline_row)
1569 clear_cmdline = TRUE;
1570 win_free_popup(wp);
1571 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001572 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001573}
1574
Bram Moolenaarec583842019-05-26 14:11:23 +02001575/*
1576 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001577 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001578 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001579 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001580popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001581{
1582 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001583 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001584 win_T *prev = NULL;
1585
Bram Moolenaarec583842019-05-26 14:11:23 +02001586 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001587 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001588 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001589 {
1590 if (prev == NULL)
1591 first_popupwin = wp->w_next;
1592 else
1593 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001594 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001595 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001596 }
1597
Bram Moolenaarec583842019-05-26 14:11:23 +02001598 // go through tab-local popups
1599 FOR_ALL_TABPAGES(tp)
1600 popup_close_tabpage(tp, id);
1601}
1602
1603/*
1604 * Close a popup window with Window-id "id" in tabpage "tp".
1605 */
1606 void
1607popup_close_tabpage(tabpage_T *tp, int id)
1608{
1609 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001610 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001611 win_T *prev = NULL;
1612
Bram Moolenaarec583842019-05-26 14:11:23 +02001613 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1614 if (wp->w_id == id)
1615 {
1616 if (prev == NULL)
1617 *root = wp->w_next;
1618 else
1619 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001620 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001621 return;
1622 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001623}
1624
1625 void
1626close_all_popups(void)
1627{
1628 while (first_popupwin != NULL)
1629 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001630 while (curtab->tp_first_popupwin != NULL)
1631 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001632}
1633
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001634/*
1635 * popup_move({id}, {options})
1636 */
1637 void
1638f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1639{
Bram Moolenaarae943152019-06-16 22:54:14 +02001640 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001641 int id = (int)tv_get_number(argvars);
1642 win_T *wp = find_popup_win(id);
1643
1644 if (wp == NULL)
1645 return; // invalid {id}
1646
1647 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1648 {
1649 emsg(_(e_dictreq));
1650 return;
1651 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001652 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001653
Bram Moolenaarae943152019-06-16 22:54:14 +02001654 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001655
1656 if (wp->w_winrow + wp->w_height >= cmdline_row)
1657 clear_cmdline = TRUE;
1658 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001659}
1660
Bram Moolenaarbc133542019-05-29 20:26:46 +02001661/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001662 * popup_setoptions({id}, {options})
1663 */
1664 void
1665f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1666{
1667 dict_T *dict;
1668 int id = (int)tv_get_number(argvars);
1669 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001670 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001671
1672 if (wp == NULL)
1673 return; // invalid {id}
1674
1675 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1676 {
1677 emsg(_(e_dictreq));
1678 return;
1679 }
1680 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001681 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001682
1683 apply_move_options(wp, dict);
1684 apply_general_options(wp, dict);
1685
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001686 if (old_firstline != wp->w_firstline)
1687 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001688 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001689 popup_adjust_position(wp);
1690}
1691
1692/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001693 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001694 */
1695 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001696f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001697{
1698 dict_T *dict;
1699 int id = (int)tv_get_number(argvars);
1700 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001701 int top_extra;
1702 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001703
1704 if (rettv_dict_alloc(rettv) == OK)
1705 {
1706 if (wp == NULL)
1707 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001708 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001709 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1710
Bram Moolenaarbc133542019-05-29 20:26:46 +02001711 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001712
Bram Moolenaarbc133542019-05-29 20:26:46 +02001713 dict_add_number(dict, "line", wp->w_winrow + 1);
1714 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001715 dict_add_number(dict, "width", wp->w_width + left_extra
1716 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1717 dict_add_number(dict, "height", wp->w_height + top_extra
1718 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001719
1720 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1721 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1722 dict_add_number(dict, "core_width", wp->w_width);
1723 dict_add_number(dict, "core_height", wp->w_height);
1724
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001725 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001726 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001727 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001728 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001729 }
1730}
1731
1732/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001733 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1734 */
1735 static void
1736get_padding_border(dict_T *dict, int *array, char *name)
1737{
1738 list_T *list;
1739 int i;
1740
1741 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1742 return;
1743
1744 list = list_alloc();
1745 if (list != NULL)
1746 {
1747 dict_add_list(dict, name, list);
1748 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1749 for (i = 0; i < 4; ++i)
1750 list_append_number(list, array[i]);
1751 }
1752}
1753
1754/*
1755 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1756 */
1757 static void
1758get_borderhighlight(dict_T *dict, win_T *wp)
1759{
1760 list_T *list;
1761 int i;
1762
1763 for (i = 0; i < 4; ++i)
1764 if (wp->w_border_highlight[i] != NULL)
1765 break;
1766 if (i == 4)
1767 return;
1768
1769 list = list_alloc();
1770 if (list != NULL)
1771 {
1772 dict_add_list(dict, "borderhighlight", list);
1773 for (i = 0; i < 4; ++i)
1774 list_append_string(list, wp->w_border_highlight[i], -1);
1775 }
1776}
1777
1778/*
1779 * For popup_getoptions(): add a "borderchars" entry to "dict".
1780 */
1781 static void
1782get_borderchars(dict_T *dict, win_T *wp)
1783{
1784 list_T *list;
1785 int i;
1786 char_u buf[NUMBUFLEN];
1787 int len;
1788
1789 for (i = 0; i < 8; ++i)
1790 if (wp->w_border_char[i] != 0)
1791 break;
1792 if (i == 8)
1793 return;
1794
1795 list = list_alloc();
1796 if (list != NULL)
1797 {
1798 dict_add_list(dict, "borderchars", list);
1799 for (i = 0; i < 8; ++i)
1800 {
1801 len = mb_char2bytes(wp->w_border_char[i], buf);
1802 list_append_string(list, buf, len);
1803 }
1804 }
1805}
1806
1807/*
1808 * For popup_getoptions(): add a "moved" entry to "dict".
1809 */
1810 static void
1811get_moved_list(dict_T *dict, win_T *wp)
1812{
1813 list_T *list;
1814
1815 list = list_alloc();
1816 if (list != NULL)
1817 {
1818 dict_add_list(dict, "moved", list);
1819 list_append_number(list, wp->w_popup_mincol);
1820 list_append_number(list, wp->w_popup_maxcol);
1821 }
1822}
1823
1824/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001825 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001826 */
1827 void
1828f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1829{
1830 dict_T *dict;
1831 int id = (int)tv_get_number(argvars);
1832 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001833 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001834 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001835
1836 if (rettv_dict_alloc(rettv) == OK)
1837 {
1838 if (wp == NULL)
1839 return;
1840
1841 dict = rettv->vval.v_dict;
1842 dict_add_number(dict, "line", wp->w_wantline);
1843 dict_add_number(dict, "col", wp->w_wantcol);
1844 dict_add_number(dict, "minwidth", wp->w_minwidth);
1845 dict_add_number(dict, "minheight", wp->w_minheight);
1846 dict_add_number(dict, "maxheight", wp->w_maxheight);
1847 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001848 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001849 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001850 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001851 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001852 dict_add_string(dict, "title", wp->w_popup_title);
1853 dict_add_number(dict, "wrap", wp->w_p_wrap);
1854 dict_add_number(dict, "drag", wp->w_popup_drag);
1855 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02001856 if (wp->w_scrollbar_highlight != NULL)
1857 dict_add_string(dict, "scrollbarhighlight",
1858 wp->w_scrollbar_highlight);
1859 if (wp->w_thumb_highlight != NULL)
1860 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02001861
Bram Moolenaara3fce622019-06-20 02:31:49 +02001862 // find the tabpage that holds this popup
1863 i = 1;
1864 FOR_ALL_TABPAGES(tp)
1865 {
1866 win_T *p;
1867
1868 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1869 if (p->w_id == id)
1870 break;
1871 if (p != NULL)
1872 break;
1873 ++i;
1874 }
1875 if (tp == NULL)
1876 i = -1; // must be global
1877 else if (tp == curtab)
1878 i = 0;
1879 dict_add_number(dict, "tabpage", i);
1880
Bram Moolenaarae943152019-06-16 22:54:14 +02001881 get_padding_border(dict, wp->w_popup_padding, "padding");
1882 get_padding_border(dict, wp->w_popup_border, "border");
1883 get_borderhighlight(dict, wp);
1884 get_borderchars(dict, wp);
1885 get_moved_list(dict, wp);
1886
1887 if (wp->w_filter_cb.cb_name != NULL)
1888 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1889 if (wp->w_close_cb.cb_name != NULL)
1890 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001891
1892 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1893 ++i)
1894 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1895 {
1896 dict_add_string(dict, "pos",
1897 (char_u *)poppos_entries[i].pp_name);
1898 break;
1899 }
1900
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001901 dict_add_string(dict, "close", (char_u *)(
1902 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
1903 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
1904
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001905# if defined(FEAT_TIMERS)
1906 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1907 ? (long)wp->w_popup_timer->tr_interval : 0L);
1908# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001909 }
1910}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001911
1912 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001913error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001914{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001915 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001916 {
1917 emsg(_("E994: Not allowed in a popup window"));
1918 return TRUE;
1919 }
1920 return FALSE;
1921}
1922
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001923/*
1924 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001925 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001926 */
1927 void
1928popup_reset_handled()
1929{
1930 win_T *wp;
1931
1932 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1933 wp->w_popup_flags &= ~POPF_HANDLED;
1934 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1935 wp->w_popup_flags &= ~POPF_HANDLED;
1936}
1937
1938/*
1939 * Find the next visible popup where POPF_HANDLED is not set.
1940 * Must have called popup_reset_handled() first.
1941 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1942 * popup with the highest zindex.
1943 */
1944 win_T *
1945find_next_popup(int lowest)
1946{
1947 win_T *wp;
1948 win_T *found_wp;
1949 int found_zindex;
1950
1951 found_zindex = lowest ? INT_MAX : 0;
1952 found_wp = NULL;
1953 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1954 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1955 && (lowest ? wp->w_zindex < found_zindex
1956 : wp->w_zindex > found_zindex))
1957 {
1958 found_zindex = wp->w_zindex;
1959 found_wp = wp;
1960 }
1961 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1962 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1963 && (lowest ? wp->w_zindex < found_zindex
1964 : wp->w_zindex > found_zindex))
1965 {
1966 found_zindex = wp->w_zindex;
1967 found_wp = wp;
1968 }
1969
1970 if (found_wp != NULL)
1971 found_wp->w_popup_flags |= POPF_HANDLED;
1972 return found_wp;
1973}
1974
1975/*
1976 * Invoke the filter callback for window "wp" with typed character "c".
1977 * Uses the global "mod_mask" for modifiers.
1978 * Returns the return value of the filter.
1979 * Careful: The filter may make "wp" invalid!
1980 */
1981 static int
1982invoke_popup_filter(win_T *wp, int c)
1983{
1984 int res;
1985 typval_T rettv;
1986 int dummy;
1987 typval_T argv[3];
1988 char_u buf[NUMBUFLEN];
1989
Bram Moolenaara42d9452019-06-15 21:46:30 +02001990 // Emergency exit: CTRL-C closes the popup.
1991 if (c == Ctrl_C)
1992 {
1993 rettv.v_type = VAR_NUMBER;
1994 rettv.vval.v_number = -1;
1995 popup_close_and_callback(wp, &rettv);
1996 return 1;
1997 }
1998
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001999 argv[0].v_type = VAR_NUMBER;
2000 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2001
2002 // Convert the number to a string, so that the function can use:
2003 // if a:c == "\<F2>"
2004 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2005 argv[1].v_type = VAR_STRING;
2006 argv[1].vval.v_string = vim_strsave(buf);
2007
2008 argv[2].v_type = VAR_UNKNOWN;
2009
Bram Moolenaara42d9452019-06-15 21:46:30 +02002010 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002011 call_callback(&wp->w_filter_cb, -1,
2012 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2013 res = tv_get_number(&rettv);
2014 vim_free(argv[1].vval.v_string);
2015 clear_tv(&rettv);
2016 return res;
2017}
2018
2019/*
2020 * Called when "c" was typed: invoke popup filter callbacks.
2021 * Returns TRUE when the character was consumed,
2022 */
2023 int
2024popup_do_filter(int c)
2025{
2026 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002027 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002028
2029 popup_reset_handled();
2030
2031 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2032 if (wp->w_filter_cb.cb_name != NULL)
2033 res = invoke_popup_filter(wp, c);
2034
2035 return res;
2036}
2037
Bram Moolenaar3397f742019-06-02 18:40:06 +02002038/*
2039 * Called when the cursor moved: check if any popup needs to be closed if the
2040 * cursor moved far enough.
2041 */
2042 void
2043popup_check_cursor_pos()
2044{
2045 win_T *wp;
2046 typval_T tv;
2047
2048 popup_reset_handled();
2049 while ((wp = find_next_popup(TRUE)) != NULL)
2050 if (wp->w_popup_curwin != NULL
2051 && (curwin != wp->w_popup_curwin
2052 || curwin->w_cursor.lnum != wp->w_popup_lnum
2053 || curwin->w_cursor.col < wp->w_popup_mincol
2054 || curwin->w_cursor.col > wp->w_popup_maxcol))
2055 {
2056 tv.v_type = VAR_NUMBER;
2057 tv.vval.v_number = -1;
2058 popup_close_and_callback(wp, &tv);
2059 }
2060}
2061
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002062/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002063 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2064 * "col" and "line" are screen coordinates.
2065 */
2066 static int
2067popup_masked(win_T *wp, int screencol, int screenline)
2068{
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002069 int col = screencol - wp->w_wincol + 1 + wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002070 int line = screenline - wp->w_winrow + 1;
2071 listitem_T *lio, *li;
2072 int width, height;
2073
2074 if (wp->w_popup_mask == NULL)
2075 return FALSE;
2076 width = popup_width(wp);
2077 height = popup_height(wp);
2078
2079 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2080 {
2081 int cols, cole;
2082 int lines, linee;
2083
2084 li = lio->li_tv.vval.v_list->lv_first;
2085 cols = tv_get_number(&li->li_tv);
2086 if (cols < 0)
2087 cols = width + cols + 1;
2088 if (col < cols)
2089 continue;
2090 li = li->li_next;
2091 cole = tv_get_number(&li->li_tv);
2092 if (cole < 0)
2093 cole = width + cole + 1;
2094 if (col > cole)
2095 continue;
2096 li = li->li_next;
2097 lines = tv_get_number(&li->li_tv);
2098 if (lines < 0)
2099 lines = height + lines + 1;
2100 if (line < lines)
2101 continue;
2102 li = li->li_next;
2103 linee = tv_get_number(&li->li_tv);
2104 if (linee < 0)
2105 linee = height + linee + 1;
2106 if (line > linee)
2107 continue;
2108 return TRUE;
2109 }
2110 return FALSE;
2111}
2112
2113/*
2114 * Set flags in popup_transparent[] for window "wp" to "val".
2115 */
2116 static void
2117update_popup_transparent(win_T *wp, int val)
2118{
2119 if (wp->w_popup_mask != NULL)
2120 {
2121 int width = popup_width(wp);
2122 int height = popup_height(wp);
2123 listitem_T *lio, *li;
2124 int cols, cole;
2125 int lines, linee;
2126 int col, line;
2127
2128 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2129 {
2130 li = lio->li_tv.vval.v_list->lv_first;
2131 cols = tv_get_number(&li->li_tv);
2132 if (cols < 0)
2133 cols = width + cols + 1;
2134 li = li->li_next;
2135 cole = tv_get_number(&li->li_tv);
2136 if (cole < 0)
2137 cole = width + cole + 1;
2138 li = li->li_next;
2139 lines = tv_get_number(&li->li_tv);
2140 if (lines < 0)
2141 lines = height + lines + 1;
2142 li = li->li_next;
2143 linee = tv_get_number(&li->li_tv);
2144 if (linee < 0)
2145 linee = height + linee + 1;
2146
2147 --cols;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002148 cols -= wp->w_leftcol;
2149 if (cols < 0)
2150 cols = 0;
2151 cole -= wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002152 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002153 if (lines < 0)
2154 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002155 for (line = lines; line < linee && line < screen_Rows; ++line)
2156 for (col = cols; col < cole && col < screen_Columns; ++col)
2157 popup_transparent[(line + wp->w_winrow) * screen_Columns
2158 + col + wp->w_wincol] = val;
2159 }
2160 }
2161}
2162
2163/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002164 * Update "popup_mask" if needed.
2165 * Also recomputes the popup size and positions.
2166 * Also updates "popup_visible".
2167 * Also marks window lines for redrawing.
2168 */
2169 void
2170may_update_popup_mask(int type)
2171{
2172 win_T *wp;
2173 short *mask;
2174 int line, col;
2175 int redraw_all = FALSE;
2176
2177 // Need to recompute when switching tabs.
2178 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2179 // (such as the screen size) must have changed.
2180 if (popup_mask_tab != curtab || type >= NOT_VALID)
2181 {
2182 popup_mask_refresh = TRUE;
2183 redraw_all = TRUE;
2184 }
2185 if (!popup_mask_refresh)
2186 {
2187 // Check if any buffer has changed.
2188 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2189 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2190 popup_mask_refresh = TRUE;
2191 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2192 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2193 popup_mask_refresh = TRUE;
2194 if (!popup_mask_refresh)
2195 return;
2196 }
2197
2198 // Need to update the mask, something has changed.
2199 popup_mask_refresh = FALSE;
2200 popup_mask_tab = curtab;
2201 popup_visible = FALSE;
2202
2203 // If redrawing everything, just update "popup_mask".
2204 // If redrawing only what is needed, update "popup_mask_next" and then
2205 // compare with "popup_mask" to see what changed.
2206 if (type >= SOME_VALID)
2207 mask = popup_mask;
2208 else
2209 mask = popup_mask_next;
2210 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2211
2212 // Find the window with the lowest zindex that hasn't been handled yet,
2213 // so that the window with a higher zindex overwrites the value in
2214 // popup_mask.
2215 popup_reset_handled();
2216 while ((wp = find_next_popup(TRUE)) != NULL)
2217 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002218 int height = popup_height(wp);
2219 int width = popup_width(wp);
2220
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002221 popup_visible = TRUE;
2222
2223 // Recompute the position if the text changed.
2224 if (redraw_all
2225 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2226 popup_adjust_position(wp);
2227
2228 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002229 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002230 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002231 col < wp->w_wincol + width && col < screen_Columns; ++col)
2232 if (!popup_masked(wp, col, line))
2233 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002234 }
2235
2236 // Only check which lines are to be updated if not already
2237 // updating all lines.
2238 if (mask == popup_mask_next)
2239 for (line = 0; line < screen_Rows; ++line)
2240 {
2241 int col_done = 0;
2242
2243 for (col = 0; col < screen_Columns; ++col)
2244 {
2245 int off = line * screen_Columns + col;
2246
2247 if (popup_mask[off] != popup_mask_next[off])
2248 {
2249 popup_mask[off] = popup_mask_next[off];
2250
2251 if (line >= cmdline_row)
2252 {
2253 // the command line needs to be cleared if text below
2254 // the popup is now visible.
2255 if (!msg_scrolled && popup_mask_next[off] == 0)
2256 clear_cmdline = TRUE;
2257 }
2258 else if (col >= col_done)
2259 {
2260 linenr_T lnum;
2261 int line_cp = line;
2262 int col_cp = col;
2263
2264 // The screen position "line" / "col" needs to be
2265 // redrawn. Figure out what window that is and update
2266 // w_redraw_top and w_redr_bot. Only needs to be done
2267 // once for each window line.
2268 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2269 if (wp != NULL)
2270 {
2271 if (line_cp >= wp->w_height)
2272 // In (or below) status line
2273 wp->w_redr_status = TRUE;
2274 // compute the position in the buffer line from the
2275 // position on the screen
2276 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2277 &lnum))
2278 // past bottom
2279 wp->w_redr_status = TRUE;
2280 else
2281 redrawWinline(wp, lnum);
2282
2283 // This line is going to be redrawn, no need to
2284 // check until the right side of the window.
2285 col_done = wp->w_wincol + wp->w_width - 1;
2286 }
2287 }
2288 }
2289 }
2290 }
2291}
2292
2293/*
2294 * Return a string of "len" spaces in IObuff.
2295 */
2296 static char_u *
2297get_spaces(int len)
2298{
2299 vim_memset(IObuff, ' ', (size_t)len);
2300 IObuff[len] = NUL;
2301 return IObuff;
2302}
2303
2304/*
2305 * Update popup windows. They are drawn on top of normal windows.
2306 * "win_update" is called for each popup window, lowest zindex first.
2307 */
2308 void
2309update_popups(void (*win_update)(win_T *wp))
2310{
2311 win_T *wp;
2312 int top_off;
2313 int left_off;
2314 int total_width;
2315 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002316 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002317 int popup_attr;
2318 int border_attr[4];
2319 int border_char[8];
2320 char_u buf[MB_MAXBYTES];
2321 int row;
2322 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002323 int sb_thumb_top = 0;
2324 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002325 int attr_scroll = 0;
2326 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002327
2328 // Find the window with the lowest zindex that hasn't been updated yet,
2329 // so that the window with a higher zindex is drawn later, thus goes on
2330 // top.
2331 popup_reset_handled();
2332 while ((wp = find_next_popup(TRUE)) != NULL)
2333 {
2334 // This drawing uses the zindex of the popup window, so that it's on
2335 // top of the text but doesn't draw when another popup with higher
2336 // zindex is on top of the character.
2337 screen_zindex = wp->w_zindex;
2338
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002339 // Set flags in popup_transparent[] for masked cells.
2340 update_popup_transparent(wp, 1);
2341
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002342 // adjust w_winrow and w_wincol for border and padding, since
2343 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002344 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002345 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2346 wp->w_winrow += top_off;
2347 wp->w_wincol += left_off;
2348
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002349 // Draw the popup text, unless it's off screen.
2350 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2351 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002352
2353 wp->w_winrow -= top_off;
2354 wp->w_wincol -= left_off;
2355
2356 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002357 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1]
2358 + wp->w_has_scrollbar;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002359 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002360 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2361 popup_attr = get_wcr_attr(wp);
2362
2363 // We can only use these line drawing characters when 'encoding' is
2364 // "utf-8" and 'ambiwidth' is "single".
2365 if (enc_utf8 && *p_ambw == 's')
2366 {
2367 border_char[0] = border_char[2] = 0x2550;
2368 border_char[1] = border_char[3] = 0x2551;
2369 border_char[4] = 0x2554;
2370 border_char[5] = 0x2557;
2371 border_char[6] = 0x255d;
2372 border_char[7] = 0x255a;
2373 }
2374 else
2375 {
2376 border_char[0] = border_char[2] = '-';
2377 border_char[1] = border_char[3] = '|';
2378 for (i = 4; i < 8; ++i)
2379 border_char[i] = '+';
2380 }
2381 for (i = 0; i < 8; ++i)
2382 if (wp->w_border_char[i] != 0)
2383 border_char[i] = wp->w_border_char[i];
2384
2385 for (i = 0; i < 4; ++i)
2386 {
2387 border_attr[i] = popup_attr;
2388 if (wp->w_border_highlight[i] != NULL)
2389 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2390 }
2391
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002392 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002393 if (wp->w_popup_border[0] > 0)
2394 {
2395 // top border
2396 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2397 wp->w_wincol,
2398 wp->w_wincol + total_width,
2399 wp->w_popup_border[3] != 0
2400 ? border_char[4] : border_char[0],
2401 border_char[0], border_attr[0]);
2402 if (wp->w_popup_border[1] > 0)
2403 {
2404 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2405 screen_puts(buf, wp->w_winrow,
2406 wp->w_wincol + total_width - 1, border_attr[1]);
2407 }
2408 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002409 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2410 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002411
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002412 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002413 {
2414 // top padding
2415 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002416 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002417 wp->w_wincol + wp->w_popup_border[3],
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002418 wp->w_wincol + total_width - wp->w_popup_border[1]
2419 - wp->w_has_scrollbar,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002420 ' ', ' ', popup_attr);
2421 }
2422
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002423 // Title goes on top of border or padding.
2424 if (wp->w_popup_title != NULL)
2425 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2426 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2427
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002428 // Compute scrollbar thumb position and size.
2429 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002430 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002431 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2432
Bram Moolenaar68acb412019-06-26 03:40:36 +02002433 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2434 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002435 if (sb_thumb_height == 0)
2436 sb_thumb_height = 1;
Bram Moolenaar68acb412019-06-26 03:40:36 +02002437 sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2)
2438 * (wp->w_height - sb_thumb_height)
2439 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002440 if (wp->w_scrollbar_highlight != NULL)
2441 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2442 else
2443 attr_scroll = highlight_attr[HLF_PSB];
2444 if (wp->w_thumb_highlight != NULL)
2445 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2446 else
2447 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002448 }
2449
2450 for (i = wp->w_popup_border[0];
2451 i < total_height - wp->w_popup_border[2]; ++i)
2452 {
2453 row = wp->w_winrow + i;
2454
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002455 // left border
2456 if (wp->w_popup_border[3] > 0)
2457 {
2458 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2459 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2460 }
2461 // left padding
2462 if (wp->w_popup_padding[3] > 0)
2463 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2464 wp->w_wincol + wp->w_popup_border[3], popup_attr);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002465 // scrollbar
2466 if (wp->w_has_scrollbar)
2467 {
2468 int line = i - top_off;
2469 int scroll_col = wp->w_wincol + total_width - 1
2470 - wp->w_popup_border[1];
2471
2472 if (line >= 0 && line < wp->w_height)
2473 screen_putchar(' ', row, scroll_col,
2474 line >= sb_thumb_top
2475 && line < sb_thumb_top + sb_thumb_height
2476 ? attr_thumb : attr_scroll);
2477 else
2478 screen_putchar(' ', row, scroll_col, popup_attr);
2479 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002480 // right border
2481 if (wp->w_popup_border[1] > 0)
2482 {
2483 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2484 screen_puts(buf, row,
2485 wp->w_wincol + total_width - 1, border_attr[1]);
2486 }
2487 // right padding
2488 if (wp->w_popup_padding[1] > 0)
2489 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2490 wp->w_wincol + wp->w_popup_border[3]
2491 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2492 }
2493
2494 if (wp->w_popup_padding[2] > 0)
2495 {
2496 // bottom padding
2497 row = wp->w_winrow + wp->w_popup_border[0]
2498 + wp->w_popup_padding[0] + wp->w_height;
2499 screen_fill(row, row + wp->w_popup_padding[2],
2500 wp->w_wincol + wp->w_popup_border[3],
2501 wp->w_wincol + total_width - wp->w_popup_border[1],
2502 ' ', ' ', popup_attr);
2503 }
2504
2505 if (wp->w_popup_border[2] > 0)
2506 {
2507 // bottom border
2508 row = wp->w_winrow + total_height - 1;
2509 screen_fill(row , row + 1,
2510 wp->w_wincol,
2511 wp->w_wincol + total_width,
2512 wp->w_popup_border[3] != 0
2513 ? border_char[7] : border_char[2],
2514 border_char[2], border_attr[2]);
2515 if (wp->w_popup_border[1] > 0)
2516 {
2517 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2518 screen_puts(buf, row,
2519 wp->w_wincol + total_width - 1, border_attr[2]);
2520 }
2521 }
2522
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002523 if (wp->w_popup_close == POPCLOSE_BUTTON)
2524 {
2525 // close button goes on top of anything at the top-right corner
2526 buf[mb_char2bytes('X', buf)] = NUL;
2527 screen_puts(buf, wp->w_winrow, wp->w_wincol + total_width - 1,
2528 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2529 }
2530
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002531 update_popup_transparent(wp, 0);
2532
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002533 // Back to the normal zindex.
2534 screen_zindex = 0;
2535 }
2536}
2537
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002538/*
2539 * Mark references in callbacks of one popup window.
2540 */
2541 static int
2542set_ref_in_one_popup(win_T *wp, int copyID)
2543{
2544 int abort = FALSE;
2545 typval_T tv;
2546
2547 if (wp->w_close_cb.cb_partial != NULL)
2548 {
2549 tv.v_type = VAR_PARTIAL;
2550 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2551 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2552 }
2553 if (wp->w_filter_cb.cb_partial != NULL)
2554 {
2555 tv.v_type = VAR_PARTIAL;
2556 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2557 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2558 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002559 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002560 return abort;
2561}
2562
2563/*
2564 * Set reference in callbacks of popup windows.
2565 */
2566 int
2567set_ref_in_popups(int copyID)
2568{
2569 int abort = FALSE;
2570 win_T *wp;
2571 tabpage_T *tp;
2572
2573 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2574 abort = abort || set_ref_in_one_popup(wp, copyID);
2575
2576 FOR_ALL_TABPAGES(tp)
2577 {
2578 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2579 abort = abort || set_ref_in_one_popup(wp, copyID);
2580 if (abort)
2581 break;
2582 }
2583 return abort;
2584}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002585#endif // FEAT_TEXT_PROP