blob: 401f1fbea9d62c5c031399d90a5447009bbf2ef5 [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/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200171 * Used when popup options contain "mousemoved": set default moved values.
172 */
173 static void
174set_mousemoved_values(win_T *wp)
175{
176 wp->w_popup_mouse_row = mouse_row;
177 wp->w_popup_mouse_mincol = mouse_col;
178 wp->w_popup_mouse_maxcol = mouse_col;
179}
180
181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
187 char_u *text;
188 int col;
189
190 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
191 NULL, NULL, &text, &col) == OK)
192 {
193 wp->w_popup_mouse_mincol = col;
194 wp->w_popup_mouse_maxcol = col + STRLEN(text) - 1;
195 vim_free(text);
196 }
197}
198
199/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200200 * Return TRUE if "row"/"col" is on the border of the popup.
201 * The values are relative to the top-left corner.
202 */
203 int
204popup_on_border(win_T *wp, int row, int col)
205{
206 return (row == 0 && wp->w_popup_border[0] > 0)
207 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
208 || (col == 0 && wp->w_popup_border[3] > 0)
209 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
210}
211
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200212/*
213 * Return TRUE if "row"/"col" is on the "X" button of the popup.
214 * The values are relative to the top-left corner.
215 * Caller should check w_popup_close is POPCLOSE_BUTTON.
216 */
217 int
218popup_on_X_button(win_T *wp, int row, int col)
219{
220 return row == 0 && col == popup_width(wp) - 1;
221}
222
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200223// Values set when dragging a popup window starts.
224static int drag_start_row;
225static int drag_start_col;
226static int drag_start_wantline;
227static int drag_start_wantcol;
228
229/*
230 * Mouse down on border of popup window: start dragging it.
231 * Uses mouse_col and mouse_row.
232 */
233 void
234popup_start_drag(win_T *wp)
235{
236 drag_start_row = mouse_row;
237 drag_start_col = mouse_col;
238 // TODO: handle using different corner
239 if (wp->w_wantline == 0)
240 drag_start_wantline = wp->w_winrow + 1;
241 else
242 drag_start_wantline = wp->w_wantline;
243 if (wp->w_wantcol == 0)
244 drag_start_wantcol = wp->w_wincol + 1;
245 else
246 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200247
248 // Stop centering the popup
249 if (wp->w_popup_pos == POPPOS_CENTER)
250 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200251}
252
253/*
254 * Mouse moved while dragging a popup window: adjust the window popup position.
255 */
256 void
257popup_drag(win_T *wp)
258{
259 // The popup may be closed before dragging stops.
260 if (!win_valid_popup(wp))
261 return;
262
263 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
264 if (wp->w_wantline < 1)
265 wp->w_wantline = 1;
266 if (wp->w_wantline > Rows)
267 wp->w_wantline = Rows;
268 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
269 if (wp->w_wantcol < 1)
270 wp->w_wantcol = 1;
271 if (wp->w_wantcol > Columns)
272 wp->w_wantcol = Columns;
273
274 popup_adjust_position(wp);
275}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200276
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200277/*
278 * Set w_firstline to match the current "wp->w_topline".
279 */
280 void
281popup_set_firstline(win_T *wp)
282{
283 int height = wp->w_height;
284
285 wp->w_firstline = wp->w_topline;
286 popup_adjust_position(wp);
287
288 // we don't want the popup to get smaller, decrement the first line
289 // until it doesn't
290 while (wp->w_firstline > 1 && wp->w_height < height)
291 {
292 --wp->w_firstline;
293 popup_adjust_position(wp);
294 }
295}
296
297/*
298 * Handle a click in a popup window, if it is in the scrollbar.
299 */
300 void
301popup_handle_scrollbar_click(win_T *wp, int row, int col)
302{
303 int height = popup_height(wp);
304 int old_topline = wp->w_topline;
305
306 if (wp->w_has_scrollbar == 0)
307 return;
308 if (row >= wp->w_popup_border[0]
309 && row < height - wp->w_popup_border[2]
310 && col == popup_width(wp) - 1)
311 {
312 if (row >= height / 2)
313 {
314 // Click in lower half, scroll down.
315 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
316 ++wp->w_topline;
317 }
318 else if (wp->w_topline > 1)
319 // click on upper half, scroll up.
320 --wp->w_topline;
321 if (wp->w_topline != old_topline)
322 {
323 popup_set_firstline(wp);
324 redraw_win_later(wp, NOT_VALID);
325 }
326 }
327}
328
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200329#if defined(FEAT_TIMERS)
330 static void
331popup_add_timeout(win_T *wp, int time)
332{
333 char_u cbbuf[50];
334 char_u *ptr = cbbuf;
335 typval_T tv;
336
337 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
338 "{_ -> popup_close(%d)}", wp->w_id);
339 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
340 {
341 wp->w_popup_timer = create_timer(time, 0);
342 wp->w_popup_timer->tr_callback = get_callback(&tv);
343 clear_tv(&tv);
344 }
345}
346#endif
347
Bram Moolenaar17627312019-06-02 19:53:44 +0200348/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200349 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200350 */
351 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200352apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200353{
Bram Moolenaarae943152019-06-16 22:54:14 +0200354 int nr;
355
356 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
357 wp->w_minwidth = nr;
358 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
359 wp->w_minheight = nr;
360 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
361 wp->w_maxwidth = nr;
362 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
363 wp->w_maxheight = nr;
364 get_pos_options(wp, d);
365}
366
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200367 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200368handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
369{
370 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
371 {
372 char_u *s = di->di_tv.vval.v_string;
373 int flags = 0;
374
375 if (STRCMP(s, "word") == 0)
376 flags = FIND_IDENT | FIND_STRING;
377 else if (STRCMP(s, "WORD") == 0)
378 flags = FIND_STRING;
379 else if (STRCMP(s, "expr") == 0)
380 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
381 else if (STRCMP(s, "any") != 0)
382 semsg(_(e_invarg2), s);
383 if (flags != 0)
384 {
385 if (mousemoved)
386 set_mousemoved_columns(wp, flags);
387 else
388 set_moved_columns(wp, flags);
389 }
390 }
391 else if (di->di_tv.v_type == VAR_LIST
392 && di->di_tv.vval.v_list != NULL
393 && di->di_tv.vval.v_list->lv_len == 2)
394 {
395 list_T *l = di->di_tv.vval.v_list;
396 int mincol = tv_get_number(&l->lv_first->li_tv);
397 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
398
399 if (mousemoved)
400 {
401 wp->w_popup_mouse_mincol = mincol;
402 wp->w_popup_mouse_maxcol = maxcol;
403 }
404 else
405 {
406 wp->w_popup_mincol = mincol;
407 wp->w_popup_maxcol = maxcol;
408 }
409 }
410 else
411 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
412}
413
414 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200415check_highlight(dict_T *dict, char *name, char_u **pval)
416{
417 dictitem_T *di;
418 char_u *str;
419
420 di = dict_find(dict, (char_u *)name, -1);
421 if (di != NULL)
422 {
423 if (di->di_tv.v_type != VAR_STRING)
424 semsg(_(e_invargval), name);
425 else
426 {
427 str = tv_get_string(&di->di_tv);
428 if (*str != NUL)
429 *pval = vim_strsave(str);
430 }
431 }
432}
433
Bram Moolenaarae943152019-06-16 22:54:14 +0200434/*
435 * Shared between popup_create() and f_popup_setoptions().
436 */
437 static void
438apply_general_options(win_T *wp, dict_T *dict)
439{
440 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200441 int nr;
442 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200443
Bram Moolenaarae943152019-06-16 22:54:14 +0200444 // TODO: flip
445
446 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200447 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200448 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
449 if (wp->w_firstline < 1)
450 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200451
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200452 di = dict_find(dict, (char_u *)"scrollbar", -1);
453 if (di != NULL)
454 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
455
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200456 str = dict_get_string(dict, (char_u *)"title", FALSE);
457 if (str != NULL)
458 {
459 vim_free(wp->w_popup_title);
460 wp->w_popup_title = vim_strsave(str);
461 }
462
Bram Moolenaar402502d2019-05-30 22:07:36 +0200463 di = dict_find(dict, (char_u *)"wrap", -1);
464 if (di != NULL)
465 {
466 nr = dict_get_number(dict, (char_u *)"wrap");
467 wp->w_p_wrap = nr != 0;
468 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200469
Bram Moolenaara42d9452019-06-15 21:46:30 +0200470 di = dict_find(dict, (char_u *)"drag", -1);
471 if (di != NULL)
472 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200473
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200474 di = dict_find(dict, (char_u *)"close", -1);
475 if (di != NULL)
476 {
477 int ok = TRUE;
478
479 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
480 {
481 char_u *s = di->di_tv.vval.v_string;
482
483 if (STRCMP(s, "none") == 0)
484 wp->w_popup_close = POPCLOSE_NONE;
485 else if (STRCMP(s, "button") == 0)
486 wp->w_popup_close = POPCLOSE_BUTTON;
487 else if (STRCMP(s, "click") == 0)
488 wp->w_popup_close = POPCLOSE_CLICK;
489 else
490 ok = FALSE;
491 }
492 else
493 ok = FALSE;
494 if (!ok)
495 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
496 }
497
Bram Moolenaarae943152019-06-16 22:54:14 +0200498 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
499 if (str != NULL)
500 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
501 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200502
Bram Moolenaarae943152019-06-16 22:54:14 +0200503 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
504 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200505
Bram Moolenaar790498b2019-06-01 22:15:29 +0200506 di = dict_find(dict, (char_u *)"borderhighlight", -1);
507 if (di != NULL)
508 {
509 if (di->di_tv.v_type != VAR_LIST)
510 emsg(_(e_listreq));
511 else
512 {
513 list_T *list = di->di_tv.vval.v_list;
514 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200515 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200516
517 if (list != NULL)
518 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
519 ++i, li = li->li_next)
520 {
521 str = tv_get_string(&li->li_tv);
522 if (*str != NUL)
523 wp->w_border_highlight[i] = vim_strsave(str);
524 }
525 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
526 for (i = 1; i < 4; ++i)
527 wp->w_border_highlight[i] =
528 vim_strsave(wp->w_border_highlight[0]);
529 }
530 }
531
Bram Moolenaar790498b2019-06-01 22:15:29 +0200532 di = dict_find(dict, (char_u *)"borderchars", -1);
533 if (di != NULL)
534 {
535 if (di->di_tv.v_type != VAR_LIST)
536 emsg(_(e_listreq));
537 else
538 {
539 list_T *list = di->di_tv.vval.v_list;
540 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200541 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200542
543 if (list != NULL)
544 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
545 ++i, li = li->li_next)
546 {
547 str = tv_get_string(&li->li_tv);
548 if (*str != NUL)
549 wp->w_border_char[i] = mb_ptr2char(str);
550 }
551 if (list->lv_len == 1)
552 for (i = 1; i < 8; ++i)
553 wp->w_border_char[i] = wp->w_border_char[0];
554 if (list->lv_len == 2)
555 {
556 for (i = 4; i < 8; ++i)
557 wp->w_border_char[i] = wp->w_border_char[1];
558 for (i = 1; i < 4; ++i)
559 wp->w_border_char[i] = wp->w_border_char[0];
560 }
561 }
562 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200563
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200564 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
565 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
566
Bram Moolenaarae943152019-06-16 22:54:14 +0200567 di = dict_find(dict, (char_u *)"zindex", -1);
568 if (di != NULL)
569 {
570 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
571 if (wp->w_zindex < 1)
572 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
573 if (wp->w_zindex > 32000)
574 wp->w_zindex = 32000;
575 }
576
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200577 di = dict_find(dict, (char_u *)"mask", -1);
578 if (di != NULL)
579 {
580 int ok = TRUE;
581
582 if (di->di_tv.v_type != VAR_LIST)
583 ok = FALSE;
584 else if (di->di_tv.vval.v_list != NULL)
585 {
586 listitem_T *li;
587
588 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
589 li = li->li_next)
590 {
591 if (li->li_tv.v_type != VAR_LIST
592 || li->li_tv.vval.v_list == NULL
593 || li->li_tv.vval.v_list->lv_len != 4)
594 {
595 ok = FALSE;
596 break;
597 }
598 }
599 }
600 if (ok)
601 {
602 wp->w_popup_mask = di->di_tv.vval.v_list;
603 ++wp->w_popup_mask->lv_refcount;
604 }
605 else
606 semsg(_(e_invargval), "mask");
607 }
608
Bram Moolenaarae943152019-06-16 22:54:14 +0200609#if defined(FEAT_TIMERS)
610 // Add timer to close the popup after some time.
611 nr = dict_get_number(dict, (char_u *)"time");
612 if (nr > 0)
613 popup_add_timeout(wp, nr);
614#endif
615
Bram Moolenaar3397f742019-06-02 18:40:06 +0200616 di = dict_find(dict, (char_u *)"moved", -1);
617 if (di != NULL)
618 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200619 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200620 handle_moved_argument(wp, di, FALSE);
621 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200622
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200623 di = dict_find(dict, (char_u *)"mousemoved", -1);
624 if (di != NULL)
625 {
626 set_mousemoved_values(wp);
627 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200628 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200629
Bram Moolenaarae943152019-06-16 22:54:14 +0200630 di = dict_find(dict, (char_u *)"filter", -1);
631 if (di != NULL)
632 {
633 callback_T callback = get_callback(&di->di_tv);
634
635 if (callback.cb_name != NULL)
636 {
637 free_callback(&wp->w_filter_cb);
638 set_callback(&wp->w_filter_cb, &callback);
639 }
640 }
641
642 di = dict_find(dict, (char_u *)"callback", -1);
643 if (di != NULL)
644 {
645 callback_T callback = get_callback(&di->di_tv);
646
647 if (callback.cb_name != NULL)
648 {
649 free_callback(&wp->w_close_cb);
650 set_callback(&wp->w_close_cb, &callback);
651 }
652 }
653}
654
655/*
656 * Go through the options in "dict" and apply them to popup window "wp".
657 */
658 static void
659apply_options(win_T *wp, dict_T *dict)
660{
661 int nr;
662
663 apply_move_options(wp, dict);
664 apply_general_options(wp, dict);
665
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200666 nr = dict_get_number(dict, (char_u *)"hidden");
667 if (nr > 0)
668 {
669 wp->w_popup_flags |= POPF_HIDDEN;
670 --wp->w_buffer->b_nwindows;
671 }
672
Bram Moolenaar33796b32019-06-08 16:01:13 +0200673 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200674}
675
676/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200677 * Add lines to the popup from a list of strings.
678 */
679 static void
680add_popup_strings(buf_T *buf, list_T *l)
681{
682 listitem_T *li;
683 linenr_T lnum = 0;
684 char_u *p;
685
686 for (li = l->lv_first; li != NULL; li = li->li_next)
687 if (li->li_tv.v_type == VAR_STRING)
688 {
689 p = li->li_tv.vval.v_string;
690 ml_append_buf(buf, lnum++,
691 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
692 }
693}
694
695/*
696 * Add lines to the popup from a list of dictionaries.
697 */
698 static void
699add_popup_dicts(buf_T *buf, list_T *l)
700{
701 listitem_T *li;
702 listitem_T *pli;
703 linenr_T lnum = 0;
704 char_u *p;
705 dict_T *dict;
706
707 // first add the text lines
708 for (li = l->lv_first; li != NULL; li = li->li_next)
709 {
710 if (li->li_tv.v_type != VAR_DICT)
711 {
712 emsg(_(e_dictreq));
713 return;
714 }
715 dict = li->li_tv.vval.v_dict;
716 p = dict == NULL ? NULL
717 : dict_get_string(dict, (char_u *)"text", FALSE);
718 ml_append_buf(buf, lnum++,
719 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
720 }
721
722 // add the text properties
723 lnum = 1;
724 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
725 {
726 dictitem_T *di;
727 list_T *plist;
728
729 dict = li->li_tv.vval.v_dict;
730 di = dict_find(dict, (char_u *)"props", -1);
731 if (di != NULL)
732 {
733 if (di->di_tv.v_type != VAR_LIST)
734 {
735 emsg(_(e_listreq));
736 return;
737 }
738 plist = di->di_tv.vval.v_list;
739 if (plist != NULL)
740 {
741 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
742 {
743 if (pli->li_tv.v_type != VAR_DICT)
744 {
745 emsg(_(e_dictreq));
746 return;
747 }
748 dict = pli->li_tv.vval.v_dict;
749 if (dict != NULL)
750 {
751 int col = dict_get_number(dict, (char_u *)"col");
752
753 prop_add_common( lnum, col, dict, buf, NULL);
754 }
755 }
756 }
757 }
758 }
759}
760
761/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200762 * Get the padding plus border at the top, adjusted to 1 if there is a title.
763 */
764 static int
765popup_top_extra(win_T *wp)
766{
767 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
768
769 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
770 return 1;
771 return extra;
772}
773
774/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200775 * Return the height of popup window "wp", including border and padding.
776 */
777 int
778popup_height(win_T *wp)
779{
780 return wp->w_height
781 + popup_top_extra(wp)
782 + wp->w_popup_padding[2] + wp->w_popup_border[2];
783}
784
785/*
786 * Return the width of popup window "wp", including border, padding and
787 * scrollbar.
788 */
789 int
790popup_width(win_T *wp)
791{
792 return wp->w_width + wp->w_leftcol
793 + wp->w_popup_padding[3] + wp->w_popup_border[3]
794 + wp->w_popup_padding[1] + wp->w_popup_border[1]
795 + wp->w_has_scrollbar
796 + wp->w_popup_rightoff;
797}
798
799/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200800 * Adjust the position and size of the popup to fit on the screen.
801 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200802 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200803popup_adjust_position(win_T *wp)
804{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200805 linenr_T lnum;
806 int wrapped = 0;
807 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200808 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200809 int center_vert = FALSE;
810 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200811 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200812 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200813 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
814 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
815 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
816 int extra_height = top_extra + bot_extra;
817 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200818 int org_winrow = wp->w_winrow;
819 int org_wincol = wp->w_wincol;
820 int org_width = wp->w_width;
821 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200822 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200823 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200824 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200825
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200826 wp->w_winrow = 0;
827 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200828 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200829 wp->w_popup_leftoff = 0;
830 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200831 if (wp->w_popup_pos == POPPOS_CENTER)
832 {
833 // center after computing the size
834 center_vert = TRUE;
835 center_hor = TRUE;
836 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200837 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200838 {
839 if (wp->w_wantline == 0)
840 center_vert = TRUE;
841 else if (wp->w_popup_pos == POPPOS_TOPLEFT
842 || wp->w_popup_pos == POPPOS_TOPRIGHT)
843 {
844 wp->w_winrow = wp->w_wantline - 1;
845 if (wp->w_winrow >= Rows)
846 wp->w_winrow = Rows - 1;
847 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200848
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200849 if (wp->w_wantcol == 0)
850 center_hor = TRUE;
851 else if (wp->w_popup_pos == POPPOS_TOPLEFT
852 || wp->w_popup_pos == POPPOS_BOTLEFT)
853 {
854 wp->w_wincol = wp->w_wantcol - 1;
855 if (wp->w_wincol >= Columns - 3)
856 wp->w_wincol = Columns - 3;
857 }
858 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200859
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200860 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200861 // When left aligned use the space available, but shift to the left when we
862 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200863 maxspace = Columns - wp->w_wincol - left_extra;
864 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200865 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200866 {
867 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200868 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200869 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200870
Bram Moolenaar8d241042019-06-12 23:40:01 +0200871 // start at the desired first line
872 wp->w_topline = wp->w_firstline;
873 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
874 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
875
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200876 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200877 // Use a minimum width of one, so that something shows when there is no
878 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200879 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200880 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200881 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200882 {
883 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
884
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200885 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200886 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200887 while (len > maxwidth)
888 {
889 ++wrapped;
890 len -= maxwidth;
891 wp->w_width = maxwidth;
892 }
893 }
894 else if (len > maxwidth
895 && allow_adjust_left
896 && (wp->w_popup_pos == POPPOS_TOPLEFT
897 || wp->w_popup_pos == POPPOS_BOTLEFT))
898 {
899 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200900 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200901
Bram Moolenaar51c31312019-06-15 22:27:23 +0200902 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200903 {
904 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200905
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200906 len -= truncate_shift;
907 shift_by -= truncate_shift;
908 }
909
910 wp->w_wincol -= shift_by;
911 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200912 wp->w_width = maxwidth;
913 }
914 if (wp->w_width < len)
915 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200916 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200917 if (wp->w_maxheight > 0
918 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200919 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200920 }
921
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200922 wp->w_has_scrollbar = wp->w_want_scrollbar
923 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
924
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200925 minwidth = wp->w_minwidth;
926 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
927 {
928 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
929
930 if (minwidth < title_len)
931 minwidth = title_len;
932 }
933
934 if (minwidth > 0 && wp->w_width < minwidth)
935 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200936 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +0200937 {
938 if (wp->w_width > maxspace)
939 // some columns cut off on the right
940 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200941 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200942 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200943 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200944 {
945 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
946 if (wp->w_wincol < 0)
947 wp->w_wincol = 0;
948 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200949 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
950 || wp->w_popup_pos == POPPOS_TOPRIGHT)
951 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200952 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
953
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200954 // Right aligned: move to the right if needed.
955 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200956 if (leftoff >= 0)
957 wp->w_wincol = leftoff;
958 else if (wp->w_popup_fixed)
959 {
960 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +0200961 // columns when displaying the window, minus left border and
962 // padding.
963 if (-leftoff > left_extra)
964 wp->w_leftcol = -leftoff - left_extra;
965 wp->w_width -= wp->w_leftcol;
966 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200967 if (wp->w_width < 0)
968 wp->w_width = 0;
969 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200970 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200971
Bram Moolenaar8d241042019-06-12 23:40:01 +0200972 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
973 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200974 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
975 wp->w_height = wp->w_minheight;
976 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
977 wp->w_height = wp->w_maxheight;
978 if (wp->w_height > Rows - wp->w_winrow)
979 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200980
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200981 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200982 {
983 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
984 if (wp->w_winrow < 0)
985 wp->w_winrow = 0;
986 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200987 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
988 || wp->w_popup_pos == POPPOS_BOTLEFT)
989 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200990 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200991 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200992 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200993 else
994 // not enough space, make top aligned
995 wp->w_winrow = wp->w_wantline + 1;
996 }
997
Bram Moolenaar17146962019-05-30 00:12:11 +0200998 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200999
1000 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001001 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001002 if (org_winrow != wp->w_winrow
1003 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001004 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001005 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001006 || org_width != wp->w_width
1007 || org_height != wp->w_height)
1008 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001009 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001010 popup_mask_refresh = TRUE;
1011 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001012}
1013
Bram Moolenaar17627312019-06-02 19:53:44 +02001014typedef enum
1015{
1016 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001017 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001018 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001019 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001020 TYPE_DIALOG,
1021 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +02001022} create_type_T;
1023
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001024/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001025 * Make "buf" empty and set the contents to "text".
1026 * Used by popup_create() and popup_settext().
1027 */
1028 static void
1029popup_set_buffer_text(buf_T *buf, typval_T text)
1030{
1031 int lnum;
1032
1033 // Clear the buffer, then replace the lines.
1034 curbuf = buf;
1035 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1036 ml_delete(lnum, FALSE);
1037 curbuf = curwin->w_buffer;
1038
1039 // Add text to the buffer.
1040 if (text.v_type == VAR_STRING)
1041 {
1042 // just a string
1043 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1044 }
1045 else
1046 {
1047 list_T *l = text.vval.v_list;
1048
1049 if (l->lv_len > 0)
1050 {
1051 if (l->lv_first->li_tv.v_type == VAR_STRING)
1052 // list of strings
1053 add_popup_strings(buf, l);
1054 else
1055 // list of dictionaries
1056 add_popup_dicts(buf, l);
1057 }
1058 }
1059
1060 // delete the line that was in the empty buffer
1061 curbuf = buf;
1062 ml_delete(buf->b_ml.ml_line_count, FALSE);
1063 curbuf = curwin->w_buffer;
1064}
1065
1066/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001067 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001068 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001069 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001070 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001071popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001072{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001073 win_T *wp;
1074 tabpage_T *tp = NULL;
1075 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001076 int new_buffer;
1077 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001078 dict_T *d;
1079 int nr;
1080 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001081
1082 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001083 if (argvars[0].v_type == VAR_NUMBER)
1084 {
1085 buf = buflist_findnr( argvars[0].vval.v_number);
1086 if (buf == NULL)
1087 {
1088 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1089 return NULL;
1090 }
1091 }
1092 else if (!(argvars[0].v_type == VAR_STRING
1093 && argvars[0].vval.v_string != NULL)
1094 && !(argvars[0].v_type == VAR_LIST
1095 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001096 {
1097 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001098 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001099 }
1100 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1101 {
1102 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001103 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001104 }
1105 d = argvars[1].vval.v_dict;
1106
Bram Moolenaara3fce622019-06-20 02:31:49 +02001107 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1108 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1109 else if (type == TYPE_NOTIFICATION)
1110 tabnr = -1; // notifications are global by default
1111 else
1112 tabnr = 0;
1113 if (tabnr > 0)
1114 {
1115 tp = find_tabpage(tabnr);
1116 if (tp == NULL)
1117 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001118 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001119 return NULL;
1120 }
1121 }
1122
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001123 // Create the window and buffer.
1124 wp = win_alloc_popup_win();
1125 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001126 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001127 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001128 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001129 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001130
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001131 if (buf != NULL)
1132 {
1133 // use existing buffer
1134 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001135 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001136 buffer_ensure_loaded(buf);
1137 }
1138 else
1139 {
1140 // create a new buffer associated with the popup
1141 new_buffer = TRUE;
1142 buf = buflist_new(NULL, NULL, (linenr_T)0,
1143 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1144 if (buf == NULL)
1145 return NULL;
1146 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001147
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001148 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001149
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001150 set_local_options_default(wp);
1151 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001152 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001153 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1154 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1155 buf->b_p_ul = -1; // no undo
1156 buf->b_p_swf = FALSE; // no swap file
1157 buf->b_p_bl = FALSE; // unlisted buffer
1158 buf->b_locked = TRUE;
1159 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001160
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001161 // Avoid that 'buftype' is reset when this buffer is entered.
1162 buf->b_p_initialized = TRUE;
1163 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001164
Bram Moolenaara3fce622019-06-20 02:31:49 +02001165 if (tp != NULL)
1166 {
1167 // popup on specified tab page
1168 wp->w_next = tp->tp_first_popupwin;
1169 tp->tp_first_popupwin = wp;
1170 }
1171 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001172 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001173 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001174 wp->w_next = curtab->tp_first_popupwin;
1175 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001176 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001177 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001178 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001179 win_T *prev = first_popupwin;
1180
1181 // Global popup: add at the end, so that it gets displayed on top of
1182 // older ones with the same zindex. Matters for notifications.
1183 if (first_popupwin == NULL)
1184 first_popupwin = wp;
1185 else
1186 {
1187 while (prev->w_next != NULL)
1188 prev = prev->w_next;
1189 prev->w_next = wp;
1190 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001191 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001192
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001193 if (new_buffer)
1194 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001195
Bram Moolenaar17627312019-06-02 19:53:44 +02001196 if (type == TYPE_ATCURSOR)
1197 {
1198 wp->w_popup_pos = POPPOS_BOTLEFT;
1199 setcursor_mayforce(TRUE);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001200 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
Bram Moolenaar17627312019-06-02 19:53:44 +02001201 if (wp->w_wantline == 0) // cursor in first line
1202 {
1203 wp->w_wantline = 2;
1204 wp->w_popup_pos = POPPOS_TOPLEFT;
1205 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001206 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar17627312019-06-02 19:53:44 +02001207 set_moved_values(wp);
1208 set_moved_columns(wp, FIND_STRING);
1209 }
1210
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001211 if (type == TYPE_BEVAL)
1212 {
1213 wp->w_popup_pos = POPPOS_BOTLEFT;
1214
1215 // by default use the mouse position
1216 wp->w_wantline = mouse_row;
1217 if (wp->w_wantline <= 0) // mouse on first line
1218 {
1219 wp->w_wantline = 2;
1220 wp->w_popup_pos = POPPOS_TOPLEFT;
1221 }
1222 wp->w_wantcol = mouse_col + 1;
1223 set_mousemoved_values(wp);
1224 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1225 }
1226
Bram Moolenaar33796b32019-06-08 16:01:13 +02001227 // set default values
1228 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001229 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001230
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001231 if (type == TYPE_NOTIFICATION)
1232 {
1233 win_T *twp, *nextwin;
1234 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001235
1236 // Try to not overlap with another global popup. Guess we need 3
1237 // more screen lines than buffer lines.
1238 wp->w_wantline = 1;
1239 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1240 {
1241 nextwin = twp->w_next;
1242 if (twp != wp
1243 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1244 && twp->w_winrow <= wp->w_wantline - 1 + height
1245 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1246 {
1247 // move to below this popup and restart the loop to check for
1248 // overlap with other popups
1249 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1250 nextwin = first_popupwin;
1251 }
1252 }
1253 if (wp->w_wantline + height > Rows)
1254 {
1255 // can't avoid overlap, put on top in the hope that message goes
1256 // away soon.
1257 wp->w_wantline = 1;
1258 }
1259
1260 wp->w_wantcol = 10;
1261 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001262 wp->w_minwidth = 20;
1263 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001264 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001265 for (i = 0; i < 4; ++i)
1266 wp->w_popup_border[i] = 1;
1267 wp->w_popup_padding[1] = 1;
1268 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001269
1270 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001271 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001272 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1273 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001274 }
1275
Bram Moolenaara730e552019-06-16 19:05:31 +02001276 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001277 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001278 wp->w_popup_pos = POPPOS_CENTER;
1279 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1280 wp->w_popup_drag = 1;
1281 for (i = 0; i < 4; ++i)
1282 {
1283 wp->w_popup_border[i] = 1;
1284 wp->w_popup_padding[i] = 1;
1285 }
1286 }
1287
Bram Moolenaara730e552019-06-16 19:05:31 +02001288 if (type == TYPE_MENU)
1289 {
1290 typval_T tv;
1291 callback_T callback;
1292
1293 tv.v_type = VAR_STRING;
1294 tv.vval.v_string = (char_u *)"popup_filter_menu";
1295 callback = get_callback(&tv);
1296 if (callback.cb_name != NULL)
1297 set_callback(&wp->w_filter_cb, &callback);
1298
1299 wp->w_p_wrap = 0;
1300 }
1301
Bram Moolenaarae943152019-06-16 22:54:14 +02001302 for (i = 0; i < 4; ++i)
1303 VIM_CLEAR(wp->w_border_highlight[i]);
1304 for (i = 0; i < 8; ++i)
1305 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001306 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001307 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001308
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001309 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001310 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001311
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001312#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001313 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1314 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001315#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001316
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001317 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001318
1319 wp->w_vsep_width = 0;
1320
1321 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001322 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001323
1324 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001325}
1326
1327/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001328 * popup_clear()
1329 */
1330 void
1331f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1332{
1333 close_all_popups();
1334}
1335
1336/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001337 * popup_create({text}, {options})
1338 */
1339 void
1340f_popup_create(typval_T *argvars, typval_T *rettv)
1341{
Bram Moolenaar17627312019-06-02 19:53:44 +02001342 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001343}
1344
1345/*
1346 * popup_atcursor({text}, {options})
1347 */
1348 void
1349f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1350{
Bram Moolenaar17627312019-06-02 19:53:44 +02001351 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001352}
1353
1354/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001355 * popup_beval({text}, {options})
1356 */
1357 void
1358f_popup_beval(typval_T *argvars, typval_T *rettv)
1359{
1360 popup_create(argvars, rettv, TYPE_BEVAL);
1361}
1362
1363/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001364 * Invoke the close callback for window "wp" with value "result".
1365 * Careful: The callback may make "wp" invalid!
1366 */
1367 static void
1368invoke_popup_callback(win_T *wp, typval_T *result)
1369{
1370 typval_T rettv;
1371 int dummy;
1372 typval_T argv[3];
1373
1374 argv[0].v_type = VAR_NUMBER;
1375 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1376
1377 if (result != NULL && result->v_type != VAR_UNKNOWN)
1378 copy_tv(result, &argv[1]);
1379 else
1380 {
1381 argv[1].v_type = VAR_NUMBER;
1382 argv[1].vval.v_number = 0;
1383 }
1384
1385 argv[2].v_type = VAR_UNKNOWN;
1386
1387 call_callback(&wp->w_close_cb, -1,
1388 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1389 if (result != NULL)
1390 clear_tv(&argv[1]);
1391 clear_tv(&rettv);
1392}
1393
1394/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001395 * Close popup "wp" and invoke any close callback for it.
1396 */
1397 static void
1398popup_close_and_callback(win_T *wp, typval_T *arg)
1399{
1400 int id = wp->w_id;
1401
1402 if (wp->w_close_cb.cb_name != NULL)
1403 // Careful: This may make "wp" invalid.
1404 invoke_popup_callback(wp, arg);
1405
1406 popup_close(id);
1407}
1408
1409/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001410 * Close popup "wp" because of a mouse click.
1411 */
1412 void
1413popup_close_for_mouse_click(win_T *wp)
1414{
1415 typval_T res;
1416
1417 res.v_type = VAR_NUMBER;
1418 res.vval.v_number = -2;
1419 popup_close_and_callback(wp, &res);
1420}
1421
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001422 static void
1423check_mouse_moved(win_T *wp, win_T *mouse_wp)
1424{
1425 // Close the popup when all if these are true:
1426 // - the mouse is not on this popup
1427 // - "mousemoved" was used
1428 // - the mouse is no longer on the same screen row or the mouse column is
1429 // outside of the relevant text
1430 if (wp != mouse_wp
1431 && wp->w_popup_mouse_row != 0
1432 && (wp->w_popup_mouse_row != mouse_row
1433 || mouse_col < wp->w_popup_mouse_mincol
1434 || mouse_col > wp->w_popup_mouse_maxcol))
1435 {
1436 typval_T res;
1437
1438 res.v_type = VAR_NUMBER;
1439 res.vval.v_number = -2;
1440 popup_close_and_callback(wp, &res);
1441 }
1442}
1443
1444/*
1445 * Called when the mouse moved: may close a popup with "mousemoved".
1446 */
1447 void
1448popup_handle_mouse_moved(void)
1449{
1450 win_T *wp;
1451 win_T *mouse_wp;
1452 int row = mouse_row;
1453 int col = mouse_col;
1454
1455 // find the window where the mouse is in
1456 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1457
1458 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1459 check_mouse_moved(wp, mouse_wp);
1460 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1461 check_mouse_moved(wp, mouse_wp);
1462}
1463
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001464/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001465 * In a filter: check if the typed key is a mouse event that is used for
1466 * dragging the popup.
1467 */
1468 static void
1469filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1470{
1471 int row = mouse_row;
1472 int col = mouse_col;
1473
1474 if (wp->w_popup_drag
1475 && is_mouse_key(c)
1476 && (wp == popup_dragwin
1477 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1478 // do not consume the key, allow for dragging the popup
1479 rettv->vval.v_number = 0;
1480}
1481
1482 static void
1483popup_highlight_curline(win_T *wp)
1484{
1485 int id;
1486 char buf[100];
1487
1488 match_delete(wp, 1, FALSE);
1489
1490 id = syn_name2id((char_u *)"PopupSelected");
1491 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1492 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1493 (char_u *)buf, 10, 1, NULL, NULL);
1494}
1495
1496/*
1497 * popup_filter_menu({text}, {options})
1498 */
1499 void
1500f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1501{
1502 int id = tv_get_number(&argvars[0]);
1503 win_T *wp = win_id2wp(id);
1504 char_u *key = tv_get_string(&argvars[1]);
1505 typval_T res;
1506 int c;
1507 linenr_T old_lnum;
1508
1509 // If the popup has been closed do not consume the key.
1510 if (wp == NULL)
1511 return;
1512
1513 c = *key;
1514 if (c == K_SPECIAL && key[1] != NUL)
1515 c = TO_SPECIAL(key[1], key[2]);
1516
1517 // consume all keys until done
1518 rettv->vval.v_number = 1;
1519 res.v_type = VAR_NUMBER;
1520
1521 old_lnum = wp->w_cursor.lnum;
1522 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1523 --wp->w_cursor.lnum;
1524 if ((c == 'j' || c == 'J' || c == K_DOWN)
1525 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1526 ++wp->w_cursor.lnum;
1527 if (old_lnum != wp->w_cursor.lnum)
1528 {
1529 popup_highlight_curline(wp);
1530 return;
1531 }
1532
1533 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1534 {
1535 // Cancelled, invoke callback with -1
1536 res.vval.v_number = -1;
1537 popup_close_and_callback(wp, &res);
1538 return;
1539 }
1540 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1541 {
1542 // Invoke callback with current index.
1543 res.vval.v_number = wp->w_cursor.lnum;
1544 popup_close_and_callback(wp, &res);
1545 return;
1546 }
1547
1548 filter_handle_drag(wp, c, rettv);
1549}
1550
1551/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001552 * popup_filter_yesno({text}, {options})
1553 */
1554 void
1555f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1556{
1557 int id = tv_get_number(&argvars[0]);
1558 win_T *wp = win_id2wp(id);
1559 char_u *key = tv_get_string(&argvars[1]);
1560 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001561 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001562
1563 // If the popup has been closed don't consume the key.
1564 if (wp == NULL)
1565 return;
1566
Bram Moolenaara730e552019-06-16 19:05:31 +02001567 c = *key;
1568 if (c == K_SPECIAL && key[1] != NUL)
1569 c = TO_SPECIAL(key[1], key[2]);
1570
Bram Moolenaara42d9452019-06-15 21:46:30 +02001571 // consume all keys until done
1572 rettv->vval.v_number = 1;
1573
Bram Moolenaara730e552019-06-16 19:05:31 +02001574 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001575 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001576 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001577 res.vval.v_number = 0;
1578 else
1579 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001580 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001581 return;
1582 }
1583
1584 // Invoke callback
1585 res.v_type = VAR_NUMBER;
1586 popup_close_and_callback(wp, &res);
1587}
1588
1589/*
1590 * popup_dialog({text}, {options})
1591 */
1592 void
1593f_popup_dialog(typval_T *argvars, typval_T *rettv)
1594{
1595 popup_create(argvars, rettv, TYPE_DIALOG);
1596}
1597
1598/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001599 * popup_menu({text}, {options})
1600 */
1601 void
1602f_popup_menu(typval_T *argvars, typval_T *rettv)
1603{
1604 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1605
1606 if (wp != NULL)
1607 popup_highlight_curline(wp);
1608}
1609
1610/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001611 * popup_notification({text}, {options})
1612 */
1613 void
1614f_popup_notification(typval_T *argvars, typval_T *rettv)
1615{
1616 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1617}
1618
1619/*
1620 * Find the popup window with window-ID "id".
1621 * If the popup window does not exist NULL is returned.
1622 * If the window is not a popup window, and error message is given.
1623 */
1624 static win_T *
1625find_popup_win(int id)
1626{
1627 win_T *wp = win_id2wp(id);
1628
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001629 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001630 {
1631 semsg(_("E993: window %d is not a popup window"), id);
1632 return NULL;
1633 }
1634 return wp;
1635}
1636
1637/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001638 * popup_close({id})
1639 */
1640 void
1641f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1642{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001643 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001644 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001645
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001646 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001647 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001648}
1649
1650/*
1651 * popup_hide({id})
1652 */
1653 void
1654f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1655{
1656 int id = (int)tv_get_number(argvars);
1657 win_T *wp = find_popup_win(id);
1658
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001659 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001660 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001661 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001662 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001663 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001664 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001665 }
1666}
1667
1668/*
1669 * popup_show({id})
1670 */
1671 void
1672f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1673{
1674 int id = (int)tv_get_number(argvars);
1675 win_T *wp = find_popup_win(id);
1676
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001677 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001678 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001679 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001680 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001681 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001682 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001683 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001684}
1685
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001686/*
1687 * popup_settext({id}, {text})
1688 */
1689 void
1690f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1691{
1692 int id = (int)tv_get_number(&argvars[0]);
1693 win_T *wp = find_popup_win(id);
1694
1695 if (wp != NULL)
1696 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001697 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1698 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1699 else
1700 {
1701 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1702 popup_adjust_position(wp);
1703 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001704 }
1705}
1706
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001707 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001708popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001709{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001710 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001711 if (wp->w_winrow + wp->w_height >= cmdline_row)
1712 clear_cmdline = TRUE;
1713 win_free_popup(wp);
1714 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001715 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001716}
1717
Bram Moolenaarec583842019-05-26 14:11:23 +02001718/*
1719 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001720 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001721 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001722 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001723popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001724{
1725 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001726 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001727 win_T *prev = NULL;
1728
Bram Moolenaarec583842019-05-26 14:11:23 +02001729 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001730 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001731 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001732 {
1733 if (prev == NULL)
1734 first_popupwin = wp->w_next;
1735 else
1736 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001737 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001738 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001739 }
1740
Bram Moolenaarec583842019-05-26 14:11:23 +02001741 // go through tab-local popups
1742 FOR_ALL_TABPAGES(tp)
1743 popup_close_tabpage(tp, id);
1744}
1745
1746/*
1747 * Close a popup window with Window-id "id" in tabpage "tp".
1748 */
1749 void
1750popup_close_tabpage(tabpage_T *tp, int id)
1751{
1752 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001753 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001754 win_T *prev = NULL;
1755
Bram Moolenaarec583842019-05-26 14:11:23 +02001756 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1757 if (wp->w_id == id)
1758 {
1759 if (prev == NULL)
1760 *root = wp->w_next;
1761 else
1762 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001763 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001764 return;
1765 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001766}
1767
1768 void
1769close_all_popups(void)
1770{
1771 while (first_popupwin != NULL)
1772 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001773 while (curtab->tp_first_popupwin != NULL)
1774 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001775}
1776
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001777/*
1778 * popup_move({id}, {options})
1779 */
1780 void
1781f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1782{
Bram Moolenaarae943152019-06-16 22:54:14 +02001783 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001784 int id = (int)tv_get_number(argvars);
1785 win_T *wp = find_popup_win(id);
1786
1787 if (wp == NULL)
1788 return; // invalid {id}
1789
1790 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1791 {
1792 emsg(_(e_dictreq));
1793 return;
1794 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001795 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001796
Bram Moolenaarae943152019-06-16 22:54:14 +02001797 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001798
1799 if (wp->w_winrow + wp->w_height >= cmdline_row)
1800 clear_cmdline = TRUE;
1801 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001802}
1803
Bram Moolenaarbc133542019-05-29 20:26:46 +02001804/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001805 * popup_setoptions({id}, {options})
1806 */
1807 void
1808f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1809{
1810 dict_T *dict;
1811 int id = (int)tv_get_number(argvars);
1812 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001813 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001814
1815 if (wp == NULL)
1816 return; // invalid {id}
1817
1818 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1819 {
1820 emsg(_(e_dictreq));
1821 return;
1822 }
1823 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001824 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001825
1826 apply_move_options(wp, dict);
1827 apply_general_options(wp, dict);
1828
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001829 if (old_firstline != wp->w_firstline)
1830 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001831 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001832 popup_adjust_position(wp);
1833}
1834
1835/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001836 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001837 */
1838 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001839f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001840{
1841 dict_T *dict;
1842 int id = (int)tv_get_number(argvars);
1843 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001844 int top_extra;
1845 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001846
1847 if (rettv_dict_alloc(rettv) == OK)
1848 {
1849 if (wp == NULL)
1850 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001851 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001852 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1853
Bram Moolenaarbc133542019-05-29 20:26:46 +02001854 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001855
Bram Moolenaarbc133542019-05-29 20:26:46 +02001856 dict_add_number(dict, "line", wp->w_winrow + 1);
1857 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001858 dict_add_number(dict, "width", wp->w_width + left_extra
1859 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1860 dict_add_number(dict, "height", wp->w_height + top_extra
1861 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001862
1863 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1864 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1865 dict_add_number(dict, "core_width", wp->w_width);
1866 dict_add_number(dict, "core_height", wp->w_height);
1867
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001868 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001869 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001870 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001871 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001872 }
1873}
1874
1875/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001876 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1877 */
1878 static void
1879get_padding_border(dict_T *dict, int *array, char *name)
1880{
1881 list_T *list;
1882 int i;
1883
1884 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1885 return;
1886
1887 list = list_alloc();
1888 if (list != NULL)
1889 {
1890 dict_add_list(dict, name, list);
1891 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1892 for (i = 0; i < 4; ++i)
1893 list_append_number(list, array[i]);
1894 }
1895}
1896
1897/*
1898 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1899 */
1900 static void
1901get_borderhighlight(dict_T *dict, win_T *wp)
1902{
1903 list_T *list;
1904 int i;
1905
1906 for (i = 0; i < 4; ++i)
1907 if (wp->w_border_highlight[i] != NULL)
1908 break;
1909 if (i == 4)
1910 return;
1911
1912 list = list_alloc();
1913 if (list != NULL)
1914 {
1915 dict_add_list(dict, "borderhighlight", list);
1916 for (i = 0; i < 4; ++i)
1917 list_append_string(list, wp->w_border_highlight[i], -1);
1918 }
1919}
1920
1921/*
1922 * For popup_getoptions(): add a "borderchars" entry to "dict".
1923 */
1924 static void
1925get_borderchars(dict_T *dict, win_T *wp)
1926{
1927 list_T *list;
1928 int i;
1929 char_u buf[NUMBUFLEN];
1930 int len;
1931
1932 for (i = 0; i < 8; ++i)
1933 if (wp->w_border_char[i] != 0)
1934 break;
1935 if (i == 8)
1936 return;
1937
1938 list = list_alloc();
1939 if (list != NULL)
1940 {
1941 dict_add_list(dict, "borderchars", list);
1942 for (i = 0; i < 8; ++i)
1943 {
1944 len = mb_char2bytes(wp->w_border_char[i], buf);
1945 list_append_string(list, buf, len);
1946 }
1947 }
1948}
1949
1950/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001951 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001952 */
1953 static void
1954get_moved_list(dict_T *dict, win_T *wp)
1955{
1956 list_T *list;
1957
1958 list = list_alloc();
1959 if (list != NULL)
1960 {
1961 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001962 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02001963 list_append_number(list, wp->w_popup_mincol);
1964 list_append_number(list, wp->w_popup_maxcol);
1965 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001966 list = list_alloc();
1967 if (list != NULL)
1968 {
1969 dict_add_list(dict, "mousemoved", list);
1970 list_append_number(list, wp->w_popup_mouse_row);
1971 list_append_number(list, wp->w_popup_mouse_mincol);
1972 list_append_number(list, wp->w_popup_mouse_maxcol);
1973 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001974}
1975
1976/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001977 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001978 */
1979 void
1980f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1981{
1982 dict_T *dict;
1983 int id = (int)tv_get_number(argvars);
1984 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001985 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001986 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001987
1988 if (rettv_dict_alloc(rettv) == OK)
1989 {
1990 if (wp == NULL)
1991 return;
1992
1993 dict = rettv->vval.v_dict;
1994 dict_add_number(dict, "line", wp->w_wantline);
1995 dict_add_number(dict, "col", wp->w_wantcol);
1996 dict_add_number(dict, "minwidth", wp->w_minwidth);
1997 dict_add_number(dict, "minheight", wp->w_minheight);
1998 dict_add_number(dict, "maxheight", wp->w_maxheight);
1999 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002000 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002001 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002002 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002003 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002004 dict_add_string(dict, "title", wp->w_popup_title);
2005 dict_add_number(dict, "wrap", wp->w_p_wrap);
2006 dict_add_number(dict, "drag", wp->w_popup_drag);
2007 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002008 if (wp->w_scrollbar_highlight != NULL)
2009 dict_add_string(dict, "scrollbarhighlight",
2010 wp->w_scrollbar_highlight);
2011 if (wp->w_thumb_highlight != NULL)
2012 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002013
Bram Moolenaara3fce622019-06-20 02:31:49 +02002014 // find the tabpage that holds this popup
2015 i = 1;
2016 FOR_ALL_TABPAGES(tp)
2017 {
2018 win_T *p;
2019
2020 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2021 if (p->w_id == id)
2022 break;
2023 if (p != NULL)
2024 break;
2025 ++i;
2026 }
2027 if (tp == NULL)
2028 i = -1; // must be global
2029 else if (tp == curtab)
2030 i = 0;
2031 dict_add_number(dict, "tabpage", i);
2032
Bram Moolenaarae943152019-06-16 22:54:14 +02002033 get_padding_border(dict, wp->w_popup_padding, "padding");
2034 get_padding_border(dict, wp->w_popup_border, "border");
2035 get_borderhighlight(dict, wp);
2036 get_borderchars(dict, wp);
2037 get_moved_list(dict, wp);
2038
2039 if (wp->w_filter_cb.cb_name != NULL)
2040 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2041 if (wp->w_close_cb.cb_name != NULL)
2042 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002043
2044 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2045 ++i)
2046 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2047 {
2048 dict_add_string(dict, "pos",
2049 (char_u *)poppos_entries[i].pp_name);
2050 break;
2051 }
2052
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002053 dict_add_string(dict, "close", (char_u *)(
2054 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2055 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2056
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002057# if defined(FEAT_TIMERS)
2058 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2059 ? (long)wp->w_popup_timer->tr_interval : 0L);
2060# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002061 }
2062}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002063
2064 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002065error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002066{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002067 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002068 {
2069 emsg(_("E994: Not allowed in a popup window"));
2070 return TRUE;
2071 }
2072 return FALSE;
2073}
2074
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002075/*
2076 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002077 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002078 */
2079 void
2080popup_reset_handled()
2081{
2082 win_T *wp;
2083
2084 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2085 wp->w_popup_flags &= ~POPF_HANDLED;
2086 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2087 wp->w_popup_flags &= ~POPF_HANDLED;
2088}
2089
2090/*
2091 * Find the next visible popup where POPF_HANDLED is not set.
2092 * Must have called popup_reset_handled() first.
2093 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2094 * popup with the highest zindex.
2095 */
2096 win_T *
2097find_next_popup(int lowest)
2098{
2099 win_T *wp;
2100 win_T *found_wp;
2101 int found_zindex;
2102
2103 found_zindex = lowest ? INT_MAX : 0;
2104 found_wp = NULL;
2105 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2106 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2107 && (lowest ? wp->w_zindex < found_zindex
2108 : wp->w_zindex > found_zindex))
2109 {
2110 found_zindex = wp->w_zindex;
2111 found_wp = wp;
2112 }
2113 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2114 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2115 && (lowest ? wp->w_zindex < found_zindex
2116 : wp->w_zindex > found_zindex))
2117 {
2118 found_zindex = wp->w_zindex;
2119 found_wp = wp;
2120 }
2121
2122 if (found_wp != NULL)
2123 found_wp->w_popup_flags |= POPF_HANDLED;
2124 return found_wp;
2125}
2126
2127/*
2128 * Invoke the filter callback for window "wp" with typed character "c".
2129 * Uses the global "mod_mask" for modifiers.
2130 * Returns the return value of the filter.
2131 * Careful: The filter may make "wp" invalid!
2132 */
2133 static int
2134invoke_popup_filter(win_T *wp, int c)
2135{
2136 int res;
2137 typval_T rettv;
2138 int dummy;
2139 typval_T argv[3];
2140 char_u buf[NUMBUFLEN];
2141
Bram Moolenaara42d9452019-06-15 21:46:30 +02002142 // Emergency exit: CTRL-C closes the popup.
2143 if (c == Ctrl_C)
2144 {
2145 rettv.v_type = VAR_NUMBER;
2146 rettv.vval.v_number = -1;
2147 popup_close_and_callback(wp, &rettv);
2148 return 1;
2149 }
2150
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002151 argv[0].v_type = VAR_NUMBER;
2152 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2153
2154 // Convert the number to a string, so that the function can use:
2155 // if a:c == "\<F2>"
2156 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2157 argv[1].v_type = VAR_STRING;
2158 argv[1].vval.v_string = vim_strsave(buf);
2159
2160 argv[2].v_type = VAR_UNKNOWN;
2161
Bram Moolenaara42d9452019-06-15 21:46:30 +02002162 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002163 call_callback(&wp->w_filter_cb, -1,
2164 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2165 res = tv_get_number(&rettv);
2166 vim_free(argv[1].vval.v_string);
2167 clear_tv(&rettv);
2168 return res;
2169}
2170
2171/*
2172 * Called when "c" was typed: invoke popup filter callbacks.
2173 * Returns TRUE when the character was consumed,
2174 */
2175 int
2176popup_do_filter(int c)
2177{
2178 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002179 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002180
2181 popup_reset_handled();
2182
2183 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2184 if (wp->w_filter_cb.cb_name != NULL)
2185 res = invoke_popup_filter(wp, c);
2186
2187 return res;
2188}
2189
Bram Moolenaar3397f742019-06-02 18:40:06 +02002190/*
2191 * Called when the cursor moved: check if any popup needs to be closed if the
2192 * cursor moved far enough.
2193 */
2194 void
2195popup_check_cursor_pos()
2196{
2197 win_T *wp;
2198 typval_T tv;
2199
2200 popup_reset_handled();
2201 while ((wp = find_next_popup(TRUE)) != NULL)
2202 if (wp->w_popup_curwin != NULL
2203 && (curwin != wp->w_popup_curwin
2204 || curwin->w_cursor.lnum != wp->w_popup_lnum
2205 || curwin->w_cursor.col < wp->w_popup_mincol
2206 || curwin->w_cursor.col > wp->w_popup_maxcol))
2207 {
2208 tv.v_type = VAR_NUMBER;
2209 tv.vval.v_number = -1;
2210 popup_close_and_callback(wp, &tv);
2211 }
2212}
2213
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002214/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002215 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2216 * "col" and "line" are screen coordinates.
2217 */
2218 static int
2219popup_masked(win_T *wp, int screencol, int screenline)
2220{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002221 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002222 int line = screenline - wp->w_winrow + 1;
2223 listitem_T *lio, *li;
2224 int width, height;
2225
2226 if (wp->w_popup_mask == NULL)
2227 return FALSE;
2228 width = popup_width(wp);
2229 height = popup_height(wp);
2230
2231 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2232 {
2233 int cols, cole;
2234 int lines, linee;
2235
2236 li = lio->li_tv.vval.v_list->lv_first;
2237 cols = tv_get_number(&li->li_tv);
2238 if (cols < 0)
2239 cols = width + cols + 1;
2240 if (col < cols)
2241 continue;
2242 li = li->li_next;
2243 cole = tv_get_number(&li->li_tv);
2244 if (cole < 0)
2245 cole = width + cole + 1;
2246 if (col > cole)
2247 continue;
2248 li = li->li_next;
2249 lines = tv_get_number(&li->li_tv);
2250 if (lines < 0)
2251 lines = height + lines + 1;
2252 if (line < lines)
2253 continue;
2254 li = li->li_next;
2255 linee = tv_get_number(&li->li_tv);
2256 if (linee < 0)
2257 linee = height + linee + 1;
2258 if (line > linee)
2259 continue;
2260 return TRUE;
2261 }
2262 return FALSE;
2263}
2264
2265/*
2266 * Set flags in popup_transparent[] for window "wp" to "val".
2267 */
2268 static void
2269update_popup_transparent(win_T *wp, int val)
2270{
2271 if (wp->w_popup_mask != NULL)
2272 {
2273 int width = popup_width(wp);
2274 int height = popup_height(wp);
2275 listitem_T *lio, *li;
2276 int cols, cole;
2277 int lines, linee;
2278 int col, line;
2279
2280 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2281 {
2282 li = lio->li_tv.vval.v_list->lv_first;
2283 cols = tv_get_number(&li->li_tv);
2284 if (cols < 0)
2285 cols = width + cols + 1;
2286 li = li->li_next;
2287 cole = tv_get_number(&li->li_tv);
2288 if (cole < 0)
2289 cole = width + cole + 1;
2290 li = li->li_next;
2291 lines = tv_get_number(&li->li_tv);
2292 if (lines < 0)
2293 lines = height + lines + 1;
2294 li = li->li_next;
2295 linee = tv_get_number(&li->li_tv);
2296 if (linee < 0)
2297 linee = height + linee + 1;
2298
2299 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002300 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002301 if (cols < 0)
2302 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002303 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002304 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002305 if (lines < 0)
2306 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002307 for (line = lines; line < linee && line < screen_Rows; ++line)
2308 for (col = cols; col < cole && col < screen_Columns; ++col)
2309 popup_transparent[(line + wp->w_winrow) * screen_Columns
2310 + col + wp->w_wincol] = val;
2311 }
2312 }
2313}
2314
2315/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002316 * Update "popup_mask" if needed.
2317 * Also recomputes the popup size and positions.
2318 * Also updates "popup_visible".
2319 * Also marks window lines for redrawing.
2320 */
2321 void
2322may_update_popup_mask(int type)
2323{
2324 win_T *wp;
2325 short *mask;
2326 int line, col;
2327 int redraw_all = FALSE;
2328
2329 // Need to recompute when switching tabs.
2330 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2331 // (such as the screen size) must have changed.
2332 if (popup_mask_tab != curtab || type >= NOT_VALID)
2333 {
2334 popup_mask_refresh = TRUE;
2335 redraw_all = TRUE;
2336 }
2337 if (!popup_mask_refresh)
2338 {
2339 // Check if any buffer has changed.
2340 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2341 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2342 popup_mask_refresh = TRUE;
2343 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2344 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2345 popup_mask_refresh = TRUE;
2346 if (!popup_mask_refresh)
2347 return;
2348 }
2349
2350 // Need to update the mask, something has changed.
2351 popup_mask_refresh = FALSE;
2352 popup_mask_tab = curtab;
2353 popup_visible = FALSE;
2354
2355 // If redrawing everything, just update "popup_mask".
2356 // If redrawing only what is needed, update "popup_mask_next" and then
2357 // compare with "popup_mask" to see what changed.
2358 if (type >= SOME_VALID)
2359 mask = popup_mask;
2360 else
2361 mask = popup_mask_next;
2362 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2363
2364 // Find the window with the lowest zindex that hasn't been handled yet,
2365 // so that the window with a higher zindex overwrites the value in
2366 // popup_mask.
2367 popup_reset_handled();
2368 while ((wp = find_next_popup(TRUE)) != NULL)
2369 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002370 int height;
2371 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002372
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002373 popup_visible = TRUE;
2374
2375 // Recompute the position if the text changed.
2376 if (redraw_all
2377 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2378 popup_adjust_position(wp);
2379
Bram Moolenaard529ba52019-07-02 23:13:53 +02002380 height = popup_height(wp);
2381 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002382 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002383 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002384 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002385 col < wp->w_wincol + width && col < screen_Columns; ++col)
2386 if (!popup_masked(wp, col, line))
2387 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002388 }
2389
2390 // Only check which lines are to be updated if not already
2391 // updating all lines.
2392 if (mask == popup_mask_next)
2393 for (line = 0; line < screen_Rows; ++line)
2394 {
2395 int col_done = 0;
2396
2397 for (col = 0; col < screen_Columns; ++col)
2398 {
2399 int off = line * screen_Columns + col;
2400
2401 if (popup_mask[off] != popup_mask_next[off])
2402 {
2403 popup_mask[off] = popup_mask_next[off];
2404
2405 if (line >= cmdline_row)
2406 {
2407 // the command line needs to be cleared if text below
2408 // the popup is now visible.
2409 if (!msg_scrolled && popup_mask_next[off] == 0)
2410 clear_cmdline = TRUE;
2411 }
2412 else if (col >= col_done)
2413 {
2414 linenr_T lnum;
2415 int line_cp = line;
2416 int col_cp = col;
2417
2418 // The screen position "line" / "col" needs to be
2419 // redrawn. Figure out what window that is and update
2420 // w_redraw_top and w_redr_bot. Only needs to be done
2421 // once for each window line.
2422 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2423 if (wp != NULL)
2424 {
2425 if (line_cp >= wp->w_height)
2426 // In (or below) status line
2427 wp->w_redr_status = TRUE;
2428 // compute the position in the buffer line from the
2429 // position on the screen
2430 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2431 &lnum))
2432 // past bottom
2433 wp->w_redr_status = TRUE;
2434 else
2435 redrawWinline(wp, lnum);
2436
2437 // This line is going to be redrawn, no need to
2438 // check until the right side of the window.
2439 col_done = wp->w_wincol + wp->w_width - 1;
2440 }
2441 }
2442 }
2443 }
2444 }
2445}
2446
2447/*
2448 * Return a string of "len" spaces in IObuff.
2449 */
2450 static char_u *
2451get_spaces(int len)
2452{
2453 vim_memset(IObuff, ' ', (size_t)len);
2454 IObuff[len] = NUL;
2455 return IObuff;
2456}
2457
2458/*
2459 * Update popup windows. They are drawn on top of normal windows.
2460 * "win_update" is called for each popup window, lowest zindex first.
2461 */
2462 void
2463update_popups(void (*win_update)(win_T *wp))
2464{
2465 win_T *wp;
2466 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002467 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002468 int total_width;
2469 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002470 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002471 int popup_attr;
2472 int border_attr[4];
2473 int border_char[8];
2474 char_u buf[MB_MAXBYTES];
2475 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002476 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002477 int padcol = 0;
2478 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002479 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002480 int sb_thumb_top = 0;
2481 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002482 int attr_scroll = 0;
2483 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002484
2485 // Find the window with the lowest zindex that hasn't been updated yet,
2486 // so that the window with a higher zindex is drawn later, thus goes on
2487 // top.
2488 popup_reset_handled();
2489 while ((wp = find_next_popup(TRUE)) != NULL)
2490 {
2491 // This drawing uses the zindex of the popup window, so that it's on
2492 // top of the text but doesn't draw when another popup with higher
2493 // zindex is on top of the character.
2494 screen_zindex = wp->w_zindex;
2495
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002496 // Set flags in popup_transparent[] for masked cells.
2497 update_popup_transparent(wp, 1);
2498
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002499 // adjust w_winrow and w_wincol for border and padding, since
2500 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002501 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002502 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2503 - wp->w_popup_leftoff;
2504 if (wp->w_wincol + left_extra < 0)
2505 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002506 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002507 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002508
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002509 // Draw the popup text, unless it's off screen.
2510 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2511 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002512
2513 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002514 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002515
Bram Moolenaard529ba52019-07-02 23:13:53 +02002516 total_width = popup_width(wp);
2517 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002518 popup_attr = get_wcr_attr(wp);
2519
2520 // We can only use these line drawing characters when 'encoding' is
2521 // "utf-8" and 'ambiwidth' is "single".
2522 if (enc_utf8 && *p_ambw == 's')
2523 {
2524 border_char[0] = border_char[2] = 0x2550;
2525 border_char[1] = border_char[3] = 0x2551;
2526 border_char[4] = 0x2554;
2527 border_char[5] = 0x2557;
2528 border_char[6] = 0x255d;
2529 border_char[7] = 0x255a;
2530 }
2531 else
2532 {
2533 border_char[0] = border_char[2] = '-';
2534 border_char[1] = border_char[3] = '|';
2535 for (i = 4; i < 8; ++i)
2536 border_char[i] = '+';
2537 }
2538 for (i = 0; i < 8; ++i)
2539 if (wp->w_border_char[i] != 0)
2540 border_char[i] = wp->w_border_char[i];
2541
2542 for (i = 0; i < 4; ++i)
2543 {
2544 border_attr[i] = popup_attr;
2545 if (wp->w_border_highlight[i] != NULL)
2546 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2547 }
2548
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002549 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002550 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002551 if (wp->w_popup_border[0] > 0)
2552 {
2553 // top border
2554 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002555 wincol < 0 ? 0 : wincol, wincol + total_width,
2556 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002557 ? border_char[4] : border_char[0],
2558 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002559 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002560 {
2561 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2562 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002563 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002564 }
2565 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002566 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2567 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002568
Bram Moolenaard529ba52019-07-02 23:13:53 +02002569 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2570 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002571 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002572 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2573 - wp->w_has_scrollbar;
2574 if (padcol < 0)
2575 {
2576 padwidth += padcol;
2577 padcol = 0;
2578 }
2579 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002580 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002581 {
2582 // top padding
2583 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002584 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002585 ' ', ' ', popup_attr);
2586 }
2587
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002588 // Title goes on top of border or padding.
2589 if (wp->w_popup_title != NULL)
2590 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2591 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2592
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002593 // Compute scrollbar thumb position and size.
2594 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002595 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002596 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2597
Bram Moolenaar68acb412019-06-26 03:40:36 +02002598 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2599 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002600 if (sb_thumb_height == 0)
2601 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002602 if (linecount <= wp->w_height)
2603 // it just fits, avoid divide by zero
2604 sb_thumb_top = 0;
2605 else
2606 sb_thumb_top = (wp->w_topline - 1
2607 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002608 * (wp->w_height - sb_thumb_height)
2609 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002610 if (wp->w_scrollbar_highlight != NULL)
2611 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2612 else
2613 attr_scroll = highlight_attr[HLF_PSB];
2614 if (wp->w_thumb_highlight != NULL)
2615 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2616 else
2617 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002618 }
2619
2620 for (i = wp->w_popup_border[0];
2621 i < total_height - wp->w_popup_border[2]; ++i)
2622 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002623 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002624 // left and right padding only needed next to the body
2625 int do_padding =
2626 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2627 && i < total_height - wp->w_popup_border[2]
2628 - wp->w_popup_padding[2];
2629
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002630 row = wp->w_winrow + i;
2631
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002632 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002633 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002634 {
2635 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002636 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002637 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002638 if (do_padding && wp->w_popup_padding[3] > 0)
2639 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002640 int col = wincol + wp->w_popup_border[3];
2641
Bram Moolenaard529ba52019-07-02 23:13:53 +02002642 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002643 pad_left = wp->w_popup_padding[3];
2644 if (col < 0)
2645 {
2646 pad_left += col;
2647 col = 0;
2648 }
2649 if (pad_left > 0)
2650 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2651 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002652 // scrollbar
2653 if (wp->w_has_scrollbar)
2654 {
2655 int line = i - top_off;
2656 int scroll_col = wp->w_wincol + total_width - 1
2657 - wp->w_popup_border[1];
2658
2659 if (line >= 0 && line < wp->w_height)
2660 screen_putchar(' ', row, scroll_col,
2661 line >= sb_thumb_top
2662 && line < sb_thumb_top + sb_thumb_height
2663 ? attr_thumb : attr_scroll);
2664 else
2665 screen_putchar(' ', row, scroll_col, popup_attr);
2666 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002667 // right border
2668 if (wp->w_popup_border[1] > 0)
2669 {
2670 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002671 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002672 }
2673 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002674 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002675 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002676 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002677 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2678 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002679 }
2680
2681 if (wp->w_popup_padding[2] > 0)
2682 {
2683 // bottom padding
2684 row = wp->w_winrow + wp->w_popup_border[0]
2685 + wp->w_popup_padding[0] + wp->w_height;
2686 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002687 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002688 }
2689
2690 if (wp->w_popup_border[2] > 0)
2691 {
2692 // bottom border
2693 row = wp->w_winrow + total_height - 1;
2694 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002695 wincol < 0 ? 0 : wincol,
2696 wincol + total_width,
2697 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002698 ? border_char[7] : border_char[2],
2699 border_char[2], border_attr[2]);
2700 if (wp->w_popup_border[1] > 0)
2701 {
2702 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002703 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002704 }
2705 }
2706
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002707 if (wp->w_popup_close == POPCLOSE_BUTTON)
2708 {
2709 // close button goes on top of anything at the top-right corner
2710 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002711 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002712 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2713 }
2714
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002715 update_popup_transparent(wp, 0);
2716
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002717 // Back to the normal zindex.
2718 screen_zindex = 0;
2719 }
2720}
2721
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002722/*
2723 * Mark references in callbacks of one popup window.
2724 */
2725 static int
2726set_ref_in_one_popup(win_T *wp, int copyID)
2727{
2728 int abort = FALSE;
2729 typval_T tv;
2730
2731 if (wp->w_close_cb.cb_partial != NULL)
2732 {
2733 tv.v_type = VAR_PARTIAL;
2734 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2735 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2736 }
2737 if (wp->w_filter_cb.cb_partial != NULL)
2738 {
2739 tv.v_type = VAR_PARTIAL;
2740 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2741 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2742 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002743 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002744 return abort;
2745}
2746
2747/*
2748 * Set reference in callbacks of popup windows.
2749 */
2750 int
2751set_ref_in_popups(int copyID)
2752{
2753 int abort = FALSE;
2754 win_T *wp;
2755 tabpage_T *tp;
2756
2757 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2758 abort = abort || set_ref_in_one_popup(wp, copyID);
2759
2760 FOR_ALL_TABPAGES(tp)
2761 {
2762 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2763 abort = abort || set_ref_in_one_popup(wp, copyID);
2764 if (abort)
2765 break;
2766 }
2767 return abort;
2768}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002769#endif // FEAT_TEXT_PROP