blob: 32f486181e2c595aa18f0fafc962108c04e1c4b6 [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;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001440 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001441 popup_close_and_callback(wp, &res);
1442 }
1443}
1444
1445/*
1446 * Called when the mouse moved: may close a popup with "mousemoved".
1447 */
1448 void
1449popup_handle_mouse_moved(void)
1450{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001451 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001452 win_T *mouse_wp;
1453 int row = mouse_row;
1454 int col = mouse_col;
1455
1456 // find the window where the mouse is in
1457 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1458
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001459 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1460 {
1461 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001462 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001463 }
1464 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1465 {
1466 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001467 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001468 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001469}
1470
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001471/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001472 * In a filter: check if the typed key is a mouse event that is used for
1473 * dragging the popup.
1474 */
1475 static void
1476filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1477{
1478 int row = mouse_row;
1479 int col = mouse_col;
1480
1481 if (wp->w_popup_drag
1482 && is_mouse_key(c)
1483 && (wp == popup_dragwin
1484 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1485 // do not consume the key, allow for dragging the popup
1486 rettv->vval.v_number = 0;
1487}
1488
1489 static void
1490popup_highlight_curline(win_T *wp)
1491{
1492 int id;
1493 char buf[100];
1494
1495 match_delete(wp, 1, FALSE);
1496
1497 id = syn_name2id((char_u *)"PopupSelected");
1498 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1499 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1500 (char_u *)buf, 10, 1, NULL, NULL);
1501}
1502
1503/*
1504 * popup_filter_menu({text}, {options})
1505 */
1506 void
1507f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1508{
1509 int id = tv_get_number(&argvars[0]);
1510 win_T *wp = win_id2wp(id);
1511 char_u *key = tv_get_string(&argvars[1]);
1512 typval_T res;
1513 int c;
1514 linenr_T old_lnum;
1515
1516 // If the popup has been closed do not consume the key.
1517 if (wp == NULL)
1518 return;
1519
1520 c = *key;
1521 if (c == K_SPECIAL && key[1] != NUL)
1522 c = TO_SPECIAL(key[1], key[2]);
1523
1524 // consume all keys until done
1525 rettv->vval.v_number = 1;
1526 res.v_type = VAR_NUMBER;
1527
1528 old_lnum = wp->w_cursor.lnum;
1529 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1530 --wp->w_cursor.lnum;
1531 if ((c == 'j' || c == 'J' || c == K_DOWN)
1532 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1533 ++wp->w_cursor.lnum;
1534 if (old_lnum != wp->w_cursor.lnum)
1535 {
1536 popup_highlight_curline(wp);
1537 return;
1538 }
1539
1540 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1541 {
1542 // Cancelled, invoke callback with -1
1543 res.vval.v_number = -1;
1544 popup_close_and_callback(wp, &res);
1545 return;
1546 }
1547 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1548 {
1549 // Invoke callback with current index.
1550 res.vval.v_number = wp->w_cursor.lnum;
1551 popup_close_and_callback(wp, &res);
1552 return;
1553 }
1554
1555 filter_handle_drag(wp, c, rettv);
1556}
1557
1558/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001559 * popup_filter_yesno({text}, {options})
1560 */
1561 void
1562f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1563{
1564 int id = tv_get_number(&argvars[0]);
1565 win_T *wp = win_id2wp(id);
1566 char_u *key = tv_get_string(&argvars[1]);
1567 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001568 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001569
1570 // If the popup has been closed don't consume the key.
1571 if (wp == NULL)
1572 return;
1573
Bram Moolenaara730e552019-06-16 19:05:31 +02001574 c = *key;
1575 if (c == K_SPECIAL && key[1] != NUL)
1576 c = TO_SPECIAL(key[1], key[2]);
1577
Bram Moolenaara42d9452019-06-15 21:46:30 +02001578 // consume all keys until done
1579 rettv->vval.v_number = 1;
1580
Bram Moolenaara730e552019-06-16 19:05:31 +02001581 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001582 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001583 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001584 res.vval.v_number = 0;
1585 else
1586 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001587 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001588 return;
1589 }
1590
1591 // Invoke callback
1592 res.v_type = VAR_NUMBER;
1593 popup_close_and_callback(wp, &res);
1594}
1595
1596/*
1597 * popup_dialog({text}, {options})
1598 */
1599 void
1600f_popup_dialog(typval_T *argvars, typval_T *rettv)
1601{
1602 popup_create(argvars, rettv, TYPE_DIALOG);
1603}
1604
1605/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001606 * popup_menu({text}, {options})
1607 */
1608 void
1609f_popup_menu(typval_T *argvars, typval_T *rettv)
1610{
1611 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1612
1613 if (wp != NULL)
1614 popup_highlight_curline(wp);
1615}
1616
1617/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001618 * popup_notification({text}, {options})
1619 */
1620 void
1621f_popup_notification(typval_T *argvars, typval_T *rettv)
1622{
1623 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1624}
1625
1626/*
1627 * Find the popup window with window-ID "id".
1628 * If the popup window does not exist NULL is returned.
1629 * If the window is not a popup window, and error message is given.
1630 */
1631 static win_T *
1632find_popup_win(int id)
1633{
1634 win_T *wp = win_id2wp(id);
1635
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001636 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001637 {
1638 semsg(_("E993: window %d is not a popup window"), id);
1639 return NULL;
1640 }
1641 return wp;
1642}
1643
1644/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001645 * popup_close({id})
1646 */
1647 void
1648f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1649{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001650 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001651 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001652
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001653 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001654 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001655}
1656
1657/*
1658 * popup_hide({id})
1659 */
1660 void
1661f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1662{
1663 int id = (int)tv_get_number(argvars);
1664 win_T *wp = find_popup_win(id);
1665
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001666 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001667 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001668 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001669 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001670 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001671 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001672 }
1673}
1674
1675/*
1676 * popup_show({id})
1677 */
1678 void
1679f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1680{
1681 int id = (int)tv_get_number(argvars);
1682 win_T *wp = find_popup_win(id);
1683
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001684 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001685 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001686 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001687 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001688 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001689 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001690 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001691}
1692
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001693/*
1694 * popup_settext({id}, {text})
1695 */
1696 void
1697f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1698{
1699 int id = (int)tv_get_number(&argvars[0]);
1700 win_T *wp = find_popup_win(id);
1701
1702 if (wp != NULL)
1703 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001704 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1705 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1706 else
1707 {
1708 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1709 popup_adjust_position(wp);
1710 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001711 }
1712}
1713
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001714 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001715popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001716{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001717 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001718 if (wp->w_winrow + wp->w_height >= cmdline_row)
1719 clear_cmdline = TRUE;
1720 win_free_popup(wp);
1721 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001722 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001723}
1724
Bram Moolenaarec583842019-05-26 14:11:23 +02001725/*
1726 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001727 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001728 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001729 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001730popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001731{
1732 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001733 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001734 win_T *prev = NULL;
1735
Bram Moolenaarec583842019-05-26 14:11:23 +02001736 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001737 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001738 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001739 {
1740 if (prev == NULL)
1741 first_popupwin = wp->w_next;
1742 else
1743 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001744 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001745 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001746 }
1747
Bram Moolenaarec583842019-05-26 14:11:23 +02001748 // go through tab-local popups
1749 FOR_ALL_TABPAGES(tp)
1750 popup_close_tabpage(tp, id);
1751}
1752
1753/*
1754 * Close a popup window with Window-id "id" in tabpage "tp".
1755 */
1756 void
1757popup_close_tabpage(tabpage_T *tp, int id)
1758{
1759 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001760 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001761 win_T *prev = NULL;
1762
Bram Moolenaarec583842019-05-26 14:11:23 +02001763 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1764 if (wp->w_id == id)
1765 {
1766 if (prev == NULL)
1767 *root = wp->w_next;
1768 else
1769 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001770 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001771 return;
1772 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001773}
1774
1775 void
1776close_all_popups(void)
1777{
1778 while (first_popupwin != NULL)
1779 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001780 while (curtab->tp_first_popupwin != NULL)
1781 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001782}
1783
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001784/*
1785 * popup_move({id}, {options})
1786 */
1787 void
1788f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1789{
Bram Moolenaarae943152019-06-16 22:54:14 +02001790 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001791 int id = (int)tv_get_number(argvars);
1792 win_T *wp = find_popup_win(id);
1793
1794 if (wp == NULL)
1795 return; // invalid {id}
1796
1797 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1798 {
1799 emsg(_(e_dictreq));
1800 return;
1801 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001802 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001803
Bram Moolenaarae943152019-06-16 22:54:14 +02001804 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001805
1806 if (wp->w_winrow + wp->w_height >= cmdline_row)
1807 clear_cmdline = TRUE;
1808 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001809}
1810
Bram Moolenaarbc133542019-05-29 20:26:46 +02001811/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001812 * popup_setoptions({id}, {options})
1813 */
1814 void
1815f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1816{
1817 dict_T *dict;
1818 int id = (int)tv_get_number(argvars);
1819 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001820 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001821
1822 if (wp == NULL)
1823 return; // invalid {id}
1824
1825 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1826 {
1827 emsg(_(e_dictreq));
1828 return;
1829 }
1830 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001831 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001832
1833 apply_move_options(wp, dict);
1834 apply_general_options(wp, dict);
1835
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001836 if (old_firstline != wp->w_firstline)
1837 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001838 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001839 popup_adjust_position(wp);
1840}
1841
1842/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001843 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001844 */
1845 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001846f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001847{
1848 dict_T *dict;
1849 int id = (int)tv_get_number(argvars);
1850 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001851 int top_extra;
1852 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001853
1854 if (rettv_dict_alloc(rettv) == OK)
1855 {
1856 if (wp == NULL)
1857 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001858 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001859 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1860
Bram Moolenaarbc133542019-05-29 20:26:46 +02001861 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001862
Bram Moolenaarbc133542019-05-29 20:26:46 +02001863 dict_add_number(dict, "line", wp->w_winrow + 1);
1864 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001865 dict_add_number(dict, "width", wp->w_width + left_extra
1866 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1867 dict_add_number(dict, "height", wp->w_height + top_extra
1868 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001869
1870 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1871 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1872 dict_add_number(dict, "core_width", wp->w_width);
1873 dict_add_number(dict, "core_height", wp->w_height);
1874
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001875 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001876 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001877 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001878 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001879 }
1880}
1881
1882/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001883 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1884 */
1885 static void
1886get_padding_border(dict_T *dict, int *array, char *name)
1887{
1888 list_T *list;
1889 int i;
1890
1891 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1892 return;
1893
1894 list = list_alloc();
1895 if (list != NULL)
1896 {
1897 dict_add_list(dict, name, list);
1898 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1899 for (i = 0; i < 4; ++i)
1900 list_append_number(list, array[i]);
1901 }
1902}
1903
1904/*
1905 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1906 */
1907 static void
1908get_borderhighlight(dict_T *dict, win_T *wp)
1909{
1910 list_T *list;
1911 int i;
1912
1913 for (i = 0; i < 4; ++i)
1914 if (wp->w_border_highlight[i] != NULL)
1915 break;
1916 if (i == 4)
1917 return;
1918
1919 list = list_alloc();
1920 if (list != NULL)
1921 {
1922 dict_add_list(dict, "borderhighlight", list);
1923 for (i = 0; i < 4; ++i)
1924 list_append_string(list, wp->w_border_highlight[i], -1);
1925 }
1926}
1927
1928/*
1929 * For popup_getoptions(): add a "borderchars" entry to "dict".
1930 */
1931 static void
1932get_borderchars(dict_T *dict, win_T *wp)
1933{
1934 list_T *list;
1935 int i;
1936 char_u buf[NUMBUFLEN];
1937 int len;
1938
1939 for (i = 0; i < 8; ++i)
1940 if (wp->w_border_char[i] != 0)
1941 break;
1942 if (i == 8)
1943 return;
1944
1945 list = list_alloc();
1946 if (list != NULL)
1947 {
1948 dict_add_list(dict, "borderchars", list);
1949 for (i = 0; i < 8; ++i)
1950 {
1951 len = mb_char2bytes(wp->w_border_char[i], buf);
1952 list_append_string(list, buf, len);
1953 }
1954 }
1955}
1956
1957/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001958 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001959 */
1960 static void
1961get_moved_list(dict_T *dict, win_T *wp)
1962{
1963 list_T *list;
1964
1965 list = list_alloc();
1966 if (list != NULL)
1967 {
1968 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001969 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02001970 list_append_number(list, wp->w_popup_mincol);
1971 list_append_number(list, wp->w_popup_maxcol);
1972 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001973 list = list_alloc();
1974 if (list != NULL)
1975 {
1976 dict_add_list(dict, "mousemoved", list);
1977 list_append_number(list, wp->w_popup_mouse_row);
1978 list_append_number(list, wp->w_popup_mouse_mincol);
1979 list_append_number(list, wp->w_popup_mouse_maxcol);
1980 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001981}
1982
1983/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001984 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001985 */
1986 void
1987f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1988{
1989 dict_T *dict;
1990 int id = (int)tv_get_number(argvars);
1991 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001992 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001993 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001994
1995 if (rettv_dict_alloc(rettv) == OK)
1996 {
1997 if (wp == NULL)
1998 return;
1999
2000 dict = rettv->vval.v_dict;
2001 dict_add_number(dict, "line", wp->w_wantline);
2002 dict_add_number(dict, "col", wp->w_wantcol);
2003 dict_add_number(dict, "minwidth", wp->w_minwidth);
2004 dict_add_number(dict, "minheight", wp->w_minheight);
2005 dict_add_number(dict, "maxheight", wp->w_maxheight);
2006 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002007 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002008 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002009 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002010 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002011 dict_add_string(dict, "title", wp->w_popup_title);
2012 dict_add_number(dict, "wrap", wp->w_p_wrap);
2013 dict_add_number(dict, "drag", wp->w_popup_drag);
2014 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002015 if (wp->w_scrollbar_highlight != NULL)
2016 dict_add_string(dict, "scrollbarhighlight",
2017 wp->w_scrollbar_highlight);
2018 if (wp->w_thumb_highlight != NULL)
2019 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002020
Bram Moolenaara3fce622019-06-20 02:31:49 +02002021 // find the tabpage that holds this popup
2022 i = 1;
2023 FOR_ALL_TABPAGES(tp)
2024 {
2025 win_T *p;
2026
2027 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2028 if (p->w_id == id)
2029 break;
2030 if (p != NULL)
2031 break;
2032 ++i;
2033 }
2034 if (tp == NULL)
2035 i = -1; // must be global
2036 else if (tp == curtab)
2037 i = 0;
2038 dict_add_number(dict, "tabpage", i);
2039
Bram Moolenaarae943152019-06-16 22:54:14 +02002040 get_padding_border(dict, wp->w_popup_padding, "padding");
2041 get_padding_border(dict, wp->w_popup_border, "border");
2042 get_borderhighlight(dict, wp);
2043 get_borderchars(dict, wp);
2044 get_moved_list(dict, wp);
2045
2046 if (wp->w_filter_cb.cb_name != NULL)
2047 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2048 if (wp->w_close_cb.cb_name != NULL)
2049 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002050
2051 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2052 ++i)
2053 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2054 {
2055 dict_add_string(dict, "pos",
2056 (char_u *)poppos_entries[i].pp_name);
2057 break;
2058 }
2059
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002060 dict_add_string(dict, "close", (char_u *)(
2061 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2062 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2063
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002064# if defined(FEAT_TIMERS)
2065 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2066 ? (long)wp->w_popup_timer->tr_interval : 0L);
2067# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002068 }
2069}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002070
2071 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002072error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002073{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002074 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002075 {
2076 emsg(_("E994: Not allowed in a popup window"));
2077 return TRUE;
2078 }
2079 return FALSE;
2080}
2081
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002082/*
2083 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002084 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002085 */
2086 void
2087popup_reset_handled()
2088{
2089 win_T *wp;
2090
2091 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2092 wp->w_popup_flags &= ~POPF_HANDLED;
2093 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2094 wp->w_popup_flags &= ~POPF_HANDLED;
2095}
2096
2097/*
2098 * Find the next visible popup where POPF_HANDLED is not set.
2099 * Must have called popup_reset_handled() first.
2100 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2101 * popup with the highest zindex.
2102 */
2103 win_T *
2104find_next_popup(int lowest)
2105{
2106 win_T *wp;
2107 win_T *found_wp;
2108 int found_zindex;
2109
2110 found_zindex = lowest ? INT_MAX : 0;
2111 found_wp = NULL;
2112 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2113 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2114 && (lowest ? wp->w_zindex < found_zindex
2115 : wp->w_zindex > found_zindex))
2116 {
2117 found_zindex = wp->w_zindex;
2118 found_wp = wp;
2119 }
2120 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2121 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2122 && (lowest ? wp->w_zindex < found_zindex
2123 : wp->w_zindex > found_zindex))
2124 {
2125 found_zindex = wp->w_zindex;
2126 found_wp = wp;
2127 }
2128
2129 if (found_wp != NULL)
2130 found_wp->w_popup_flags |= POPF_HANDLED;
2131 return found_wp;
2132}
2133
2134/*
2135 * Invoke the filter callback for window "wp" with typed character "c".
2136 * Uses the global "mod_mask" for modifiers.
2137 * Returns the return value of the filter.
2138 * Careful: The filter may make "wp" invalid!
2139 */
2140 static int
2141invoke_popup_filter(win_T *wp, int c)
2142{
2143 int res;
2144 typval_T rettv;
2145 int dummy;
2146 typval_T argv[3];
2147 char_u buf[NUMBUFLEN];
2148
Bram Moolenaara42d9452019-06-15 21:46:30 +02002149 // Emergency exit: CTRL-C closes the popup.
2150 if (c == Ctrl_C)
2151 {
2152 rettv.v_type = VAR_NUMBER;
2153 rettv.vval.v_number = -1;
2154 popup_close_and_callback(wp, &rettv);
2155 return 1;
2156 }
2157
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002158 argv[0].v_type = VAR_NUMBER;
2159 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2160
2161 // Convert the number to a string, so that the function can use:
2162 // if a:c == "\<F2>"
2163 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2164 argv[1].v_type = VAR_STRING;
2165 argv[1].vval.v_string = vim_strsave(buf);
2166
2167 argv[2].v_type = VAR_UNKNOWN;
2168
Bram Moolenaara42d9452019-06-15 21:46:30 +02002169 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002170 call_callback(&wp->w_filter_cb, -1,
2171 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2172 res = tv_get_number(&rettv);
2173 vim_free(argv[1].vval.v_string);
2174 clear_tv(&rettv);
2175 return res;
2176}
2177
2178/*
2179 * Called when "c" was typed: invoke popup filter callbacks.
2180 * Returns TRUE when the character was consumed,
2181 */
2182 int
2183popup_do_filter(int c)
2184{
2185 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002186 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002187
2188 popup_reset_handled();
2189
2190 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2191 if (wp->w_filter_cb.cb_name != NULL)
2192 res = invoke_popup_filter(wp, c);
2193
2194 return res;
2195}
2196
Bram Moolenaar3397f742019-06-02 18:40:06 +02002197/*
2198 * Called when the cursor moved: check if any popup needs to be closed if the
2199 * cursor moved far enough.
2200 */
2201 void
2202popup_check_cursor_pos()
2203{
2204 win_T *wp;
2205 typval_T tv;
2206
2207 popup_reset_handled();
2208 while ((wp = find_next_popup(TRUE)) != NULL)
2209 if (wp->w_popup_curwin != NULL
2210 && (curwin != wp->w_popup_curwin
2211 || curwin->w_cursor.lnum != wp->w_popup_lnum
2212 || curwin->w_cursor.col < wp->w_popup_mincol
2213 || curwin->w_cursor.col > wp->w_popup_maxcol))
2214 {
2215 tv.v_type = VAR_NUMBER;
2216 tv.vval.v_number = -1;
2217 popup_close_and_callback(wp, &tv);
2218 }
2219}
2220
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002221/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002222 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2223 * "col" and "line" are screen coordinates.
2224 */
2225 static int
2226popup_masked(win_T *wp, int screencol, int screenline)
2227{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002228 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002229 int line = screenline - wp->w_winrow + 1;
2230 listitem_T *lio, *li;
2231 int width, height;
2232
2233 if (wp->w_popup_mask == NULL)
2234 return FALSE;
2235 width = popup_width(wp);
2236 height = popup_height(wp);
2237
2238 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2239 {
2240 int cols, cole;
2241 int lines, linee;
2242
2243 li = lio->li_tv.vval.v_list->lv_first;
2244 cols = tv_get_number(&li->li_tv);
2245 if (cols < 0)
2246 cols = width + cols + 1;
2247 if (col < cols)
2248 continue;
2249 li = li->li_next;
2250 cole = tv_get_number(&li->li_tv);
2251 if (cole < 0)
2252 cole = width + cole + 1;
2253 if (col > cole)
2254 continue;
2255 li = li->li_next;
2256 lines = tv_get_number(&li->li_tv);
2257 if (lines < 0)
2258 lines = height + lines + 1;
2259 if (line < lines)
2260 continue;
2261 li = li->li_next;
2262 linee = tv_get_number(&li->li_tv);
2263 if (linee < 0)
2264 linee = height + linee + 1;
2265 if (line > linee)
2266 continue;
2267 return TRUE;
2268 }
2269 return FALSE;
2270}
2271
2272/*
2273 * Set flags in popup_transparent[] for window "wp" to "val".
2274 */
2275 static void
2276update_popup_transparent(win_T *wp, int val)
2277{
2278 if (wp->w_popup_mask != NULL)
2279 {
2280 int width = popup_width(wp);
2281 int height = popup_height(wp);
2282 listitem_T *lio, *li;
2283 int cols, cole;
2284 int lines, linee;
2285 int col, line;
2286
2287 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2288 {
2289 li = lio->li_tv.vval.v_list->lv_first;
2290 cols = tv_get_number(&li->li_tv);
2291 if (cols < 0)
2292 cols = width + cols + 1;
2293 li = li->li_next;
2294 cole = tv_get_number(&li->li_tv);
2295 if (cole < 0)
2296 cole = width + cole + 1;
2297 li = li->li_next;
2298 lines = tv_get_number(&li->li_tv);
2299 if (lines < 0)
2300 lines = height + lines + 1;
2301 li = li->li_next;
2302 linee = tv_get_number(&li->li_tv);
2303 if (linee < 0)
2304 linee = height + linee + 1;
2305
2306 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002307 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002308 if (cols < 0)
2309 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002310 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002311 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002312 if (lines < 0)
2313 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002314 for (line = lines; line < linee && line < screen_Rows; ++line)
2315 for (col = cols; col < cole && col < screen_Columns; ++col)
2316 popup_transparent[(line + wp->w_winrow) * screen_Columns
2317 + col + wp->w_wincol] = val;
2318 }
2319 }
2320}
2321
2322/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002323 * Update "popup_mask" if needed.
2324 * Also recomputes the popup size and positions.
2325 * Also updates "popup_visible".
2326 * Also marks window lines for redrawing.
2327 */
2328 void
2329may_update_popup_mask(int type)
2330{
2331 win_T *wp;
2332 short *mask;
2333 int line, col;
2334 int redraw_all = FALSE;
2335
2336 // Need to recompute when switching tabs.
2337 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2338 // (such as the screen size) must have changed.
2339 if (popup_mask_tab != curtab || type >= NOT_VALID)
2340 {
2341 popup_mask_refresh = TRUE;
2342 redraw_all = TRUE;
2343 }
2344 if (!popup_mask_refresh)
2345 {
2346 // Check if any buffer has changed.
2347 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2348 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2349 popup_mask_refresh = TRUE;
2350 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2351 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2352 popup_mask_refresh = TRUE;
2353 if (!popup_mask_refresh)
2354 return;
2355 }
2356
2357 // Need to update the mask, something has changed.
2358 popup_mask_refresh = FALSE;
2359 popup_mask_tab = curtab;
2360 popup_visible = FALSE;
2361
2362 // If redrawing everything, just update "popup_mask".
2363 // If redrawing only what is needed, update "popup_mask_next" and then
2364 // compare with "popup_mask" to see what changed.
2365 if (type >= SOME_VALID)
2366 mask = popup_mask;
2367 else
2368 mask = popup_mask_next;
2369 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2370
2371 // Find the window with the lowest zindex that hasn't been handled yet,
2372 // so that the window with a higher zindex overwrites the value in
2373 // popup_mask.
2374 popup_reset_handled();
2375 while ((wp = find_next_popup(TRUE)) != NULL)
2376 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002377 int height;
2378 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002379
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002380 popup_visible = TRUE;
2381
2382 // Recompute the position if the text changed.
2383 if (redraw_all
2384 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2385 popup_adjust_position(wp);
2386
Bram Moolenaard529ba52019-07-02 23:13:53 +02002387 height = popup_height(wp);
2388 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002389 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002390 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002391 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002392 col < wp->w_wincol + width && col < screen_Columns; ++col)
2393 if (!popup_masked(wp, col, line))
2394 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002395 }
2396
2397 // Only check which lines are to be updated if not already
2398 // updating all lines.
2399 if (mask == popup_mask_next)
2400 for (line = 0; line < screen_Rows; ++line)
2401 {
2402 int col_done = 0;
2403
2404 for (col = 0; col < screen_Columns; ++col)
2405 {
2406 int off = line * screen_Columns + col;
2407
2408 if (popup_mask[off] != popup_mask_next[off])
2409 {
2410 popup_mask[off] = popup_mask_next[off];
2411
2412 if (line >= cmdline_row)
2413 {
2414 // the command line needs to be cleared if text below
2415 // the popup is now visible.
2416 if (!msg_scrolled && popup_mask_next[off] == 0)
2417 clear_cmdline = TRUE;
2418 }
2419 else if (col >= col_done)
2420 {
2421 linenr_T lnum;
2422 int line_cp = line;
2423 int col_cp = col;
2424
2425 // The screen position "line" / "col" needs to be
2426 // redrawn. Figure out what window that is and update
2427 // w_redraw_top and w_redr_bot. Only needs to be done
2428 // once for each window line.
2429 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2430 if (wp != NULL)
2431 {
2432 if (line_cp >= wp->w_height)
2433 // In (or below) status line
2434 wp->w_redr_status = TRUE;
2435 // compute the position in the buffer line from the
2436 // position on the screen
2437 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2438 &lnum))
2439 // past bottom
2440 wp->w_redr_status = TRUE;
2441 else
2442 redrawWinline(wp, lnum);
2443
2444 // This line is going to be redrawn, no need to
2445 // check until the right side of the window.
2446 col_done = wp->w_wincol + wp->w_width - 1;
2447 }
2448 }
2449 }
2450 }
2451 }
2452}
2453
2454/*
2455 * Return a string of "len" spaces in IObuff.
2456 */
2457 static char_u *
2458get_spaces(int len)
2459{
2460 vim_memset(IObuff, ' ', (size_t)len);
2461 IObuff[len] = NUL;
2462 return IObuff;
2463}
2464
2465/*
2466 * Update popup windows. They are drawn on top of normal windows.
2467 * "win_update" is called for each popup window, lowest zindex first.
2468 */
2469 void
2470update_popups(void (*win_update)(win_T *wp))
2471{
2472 win_T *wp;
2473 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002474 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002475 int total_width;
2476 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002477 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002478 int popup_attr;
2479 int border_attr[4];
2480 int border_char[8];
2481 char_u buf[MB_MAXBYTES];
2482 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002483 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002484 int padcol = 0;
2485 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002486 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002487 int sb_thumb_top = 0;
2488 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002489 int attr_scroll = 0;
2490 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002491
2492 // Find the window with the lowest zindex that hasn't been updated yet,
2493 // so that the window with a higher zindex is drawn later, thus goes on
2494 // top.
2495 popup_reset_handled();
2496 while ((wp = find_next_popup(TRUE)) != NULL)
2497 {
2498 // This drawing uses the zindex of the popup window, so that it's on
2499 // top of the text but doesn't draw when another popup with higher
2500 // zindex is on top of the character.
2501 screen_zindex = wp->w_zindex;
2502
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002503 // Set flags in popup_transparent[] for masked cells.
2504 update_popup_transparent(wp, 1);
2505
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002506 // adjust w_winrow and w_wincol for border and padding, since
2507 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002508 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002509 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2510 - wp->w_popup_leftoff;
2511 if (wp->w_wincol + left_extra < 0)
2512 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002513 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 Moolenaarc2a43162019-06-26 01:03:53 +02002516 // Draw the popup text, unless it's off screen.
2517 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2518 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002519
2520 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002521 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002522
Bram Moolenaard529ba52019-07-02 23:13:53 +02002523 total_width = popup_width(wp);
2524 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002525 popup_attr = get_wcr_attr(wp);
2526
2527 // We can only use these line drawing characters when 'encoding' is
2528 // "utf-8" and 'ambiwidth' is "single".
2529 if (enc_utf8 && *p_ambw == 's')
2530 {
2531 border_char[0] = border_char[2] = 0x2550;
2532 border_char[1] = border_char[3] = 0x2551;
2533 border_char[4] = 0x2554;
2534 border_char[5] = 0x2557;
2535 border_char[6] = 0x255d;
2536 border_char[7] = 0x255a;
2537 }
2538 else
2539 {
2540 border_char[0] = border_char[2] = '-';
2541 border_char[1] = border_char[3] = '|';
2542 for (i = 4; i < 8; ++i)
2543 border_char[i] = '+';
2544 }
2545 for (i = 0; i < 8; ++i)
2546 if (wp->w_border_char[i] != 0)
2547 border_char[i] = wp->w_border_char[i];
2548
2549 for (i = 0; i < 4; ++i)
2550 {
2551 border_attr[i] = popup_attr;
2552 if (wp->w_border_highlight[i] != NULL)
2553 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2554 }
2555
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002556 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002557 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002558 if (wp->w_popup_border[0] > 0)
2559 {
2560 // top border
2561 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002562 wincol < 0 ? 0 : wincol, wincol + total_width,
2563 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002564 ? border_char[4] : border_char[0],
2565 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002566 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002567 {
2568 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2569 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002570 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002571 }
2572 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002573 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2574 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002575
Bram Moolenaard529ba52019-07-02 23:13:53 +02002576 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2577 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002578 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002579 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2580 - wp->w_has_scrollbar;
2581 if (padcol < 0)
2582 {
2583 padwidth += padcol;
2584 padcol = 0;
2585 }
2586 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002587 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002588 {
2589 // top padding
2590 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002591 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002592 ' ', ' ', popup_attr);
2593 }
2594
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002595 // Title goes on top of border or padding.
2596 if (wp->w_popup_title != NULL)
2597 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2598 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2599
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002600 // Compute scrollbar thumb position and size.
2601 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002602 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002603 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2604
Bram Moolenaar68acb412019-06-26 03:40:36 +02002605 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2606 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002607 if (sb_thumb_height == 0)
2608 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002609 if (linecount <= wp->w_height)
2610 // it just fits, avoid divide by zero
2611 sb_thumb_top = 0;
2612 else
2613 sb_thumb_top = (wp->w_topline - 1
2614 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002615 * (wp->w_height - sb_thumb_height)
2616 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002617 if (wp->w_scrollbar_highlight != NULL)
2618 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2619 else
2620 attr_scroll = highlight_attr[HLF_PSB];
2621 if (wp->w_thumb_highlight != NULL)
2622 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2623 else
2624 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002625 }
2626
2627 for (i = wp->w_popup_border[0];
2628 i < total_height - wp->w_popup_border[2]; ++i)
2629 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002630 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002631 // left and right padding only needed next to the body
2632 int do_padding =
2633 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2634 && i < total_height - wp->w_popup_border[2]
2635 - wp->w_popup_padding[2];
2636
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002637 row = wp->w_winrow + i;
2638
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002639 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002640 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002641 {
2642 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002643 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002644 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002645 if (do_padding && wp->w_popup_padding[3] > 0)
2646 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002647 int col = wincol + wp->w_popup_border[3];
2648
Bram Moolenaard529ba52019-07-02 23:13:53 +02002649 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002650 pad_left = wp->w_popup_padding[3];
2651 if (col < 0)
2652 {
2653 pad_left += col;
2654 col = 0;
2655 }
2656 if (pad_left > 0)
2657 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2658 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002659 // scrollbar
2660 if (wp->w_has_scrollbar)
2661 {
2662 int line = i - top_off;
2663 int scroll_col = wp->w_wincol + total_width - 1
2664 - wp->w_popup_border[1];
2665
2666 if (line >= 0 && line < wp->w_height)
2667 screen_putchar(' ', row, scroll_col,
2668 line >= sb_thumb_top
2669 && line < sb_thumb_top + sb_thumb_height
2670 ? attr_thumb : attr_scroll);
2671 else
2672 screen_putchar(' ', row, scroll_col, popup_attr);
2673 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002674 // right border
2675 if (wp->w_popup_border[1] > 0)
2676 {
2677 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002678 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002679 }
2680 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002681 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002682 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002683 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002684 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2685 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002686 }
2687
2688 if (wp->w_popup_padding[2] > 0)
2689 {
2690 // bottom padding
2691 row = wp->w_winrow + wp->w_popup_border[0]
2692 + wp->w_popup_padding[0] + wp->w_height;
2693 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002694 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002695 }
2696
2697 if (wp->w_popup_border[2] > 0)
2698 {
2699 // bottom border
2700 row = wp->w_winrow + total_height - 1;
2701 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002702 wincol < 0 ? 0 : wincol,
2703 wincol + total_width,
2704 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002705 ? border_char[7] : border_char[2],
2706 border_char[2], border_attr[2]);
2707 if (wp->w_popup_border[1] > 0)
2708 {
2709 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002710 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002711 }
2712 }
2713
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002714 if (wp->w_popup_close == POPCLOSE_BUTTON)
2715 {
2716 // close button goes on top of anything at the top-right corner
2717 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002718 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002719 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2720 }
2721
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002722 update_popup_transparent(wp, 0);
2723
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002724 // Back to the normal zindex.
2725 screen_zindex = 0;
2726 }
2727}
2728
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002729/*
2730 * Mark references in callbacks of one popup window.
2731 */
2732 static int
2733set_ref_in_one_popup(win_T *wp, int copyID)
2734{
2735 int abort = FALSE;
2736 typval_T tv;
2737
2738 if (wp->w_close_cb.cb_partial != NULL)
2739 {
2740 tv.v_type = VAR_PARTIAL;
2741 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2742 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2743 }
2744 if (wp->w_filter_cb.cb_partial != NULL)
2745 {
2746 tv.v_type = VAR_PARTIAL;
2747 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2748 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2749 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002750 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002751 return abort;
2752}
2753
2754/*
2755 * Set reference in callbacks of popup windows.
2756 */
2757 int
2758set_ref_in_popups(int copyID)
2759{
2760 int abort = FALSE;
2761 win_T *wp;
2762 tabpage_T *tp;
2763
2764 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2765 abort = abort || set_ref_in_one_popup(wp, copyID);
2766
2767 FOR_ALL_TABPAGES(tp)
2768 {
2769 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2770 abort = abort || set_ref_in_one_popup(wp, copyID);
2771 if (abort)
2772 break;
2773 }
2774 return abort;
2775}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002776#endif // FEAT_TEXT_PROP