blob: a997cde87dea53be60cdcf0b1f7cab0660eece9f [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 {
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200883 // count Tabs for what they are worth
884 int len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
885 (colnr_T)MAXCOL);
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200886
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200887 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200888 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200889 while (len > maxwidth)
890 {
891 ++wrapped;
892 len -= maxwidth;
893 wp->w_width = maxwidth;
894 }
895 }
896 else if (len > maxwidth
897 && allow_adjust_left
898 && (wp->w_popup_pos == POPPOS_TOPLEFT
899 || wp->w_popup_pos == POPPOS_BOTLEFT))
900 {
901 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200902 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200903
Bram Moolenaar51c31312019-06-15 22:27:23 +0200904 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200905 {
906 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200907
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200908 len -= truncate_shift;
909 shift_by -= truncate_shift;
910 }
911
912 wp->w_wincol -= shift_by;
913 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200914 wp->w_width = maxwidth;
915 }
916 if (wp->w_width < len)
917 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200918 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200919 if (wp->w_maxheight > 0
920 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200921 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200922 }
923
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200924 wp->w_has_scrollbar = wp->w_want_scrollbar
925 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
926
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200927 minwidth = wp->w_minwidth;
928 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
929 {
930 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
931
932 if (minwidth < title_len)
933 minwidth = title_len;
934 }
935
936 if (minwidth > 0 && wp->w_width < minwidth)
937 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200938 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +0200939 {
940 if (wp->w_width > maxspace)
941 // some columns cut off on the right
942 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200943 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200944 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200945 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200946 {
947 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
948 if (wp->w_wincol < 0)
949 wp->w_wincol = 0;
950 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200951 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
952 || wp->w_popup_pos == POPPOS_TOPRIGHT)
953 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200954 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
955
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200956 // Right aligned: move to the right if needed.
957 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200958 if (leftoff >= 0)
959 wp->w_wincol = leftoff;
960 else if (wp->w_popup_fixed)
961 {
962 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +0200963 // columns when displaying the window, minus left border and
964 // padding.
965 if (-leftoff > left_extra)
966 wp->w_leftcol = -leftoff - left_extra;
967 wp->w_width -= wp->w_leftcol;
968 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200969 if (wp->w_width < 0)
970 wp->w_width = 0;
971 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200972 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200973
Bram Moolenaar8d241042019-06-12 23:40:01 +0200974 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
975 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200976 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
977 wp->w_height = wp->w_minheight;
978 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
979 wp->w_height = wp->w_maxheight;
980 if (wp->w_height > Rows - wp->w_winrow)
981 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200982
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200983 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200984 {
985 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
986 if (wp->w_winrow < 0)
987 wp->w_winrow = 0;
988 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200989 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
990 || wp->w_popup_pos == POPPOS_BOTLEFT)
991 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200992 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200993 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200994 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200995 else
996 // not enough space, make top aligned
997 wp->w_winrow = wp->w_wantline + 1;
998 }
999
Bram Moolenaar17146962019-05-30 00:12:11 +02001000 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001001
1002 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001003 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001004 if (org_winrow != wp->w_winrow
1005 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001006 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001007 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001008 || org_width != wp->w_width
1009 || org_height != wp->w_height)
1010 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001011 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001012 popup_mask_refresh = TRUE;
1013 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001014}
1015
Bram Moolenaar17627312019-06-02 19:53:44 +02001016typedef enum
1017{
1018 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001019 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001020 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001021 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001022 TYPE_DIALOG,
1023 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +02001024} create_type_T;
1025
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001026/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001027 * Make "buf" empty and set the contents to "text".
1028 * Used by popup_create() and popup_settext().
1029 */
1030 static void
1031popup_set_buffer_text(buf_T *buf, typval_T text)
1032{
1033 int lnum;
1034
1035 // Clear the buffer, then replace the lines.
1036 curbuf = buf;
1037 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1038 ml_delete(lnum, FALSE);
1039 curbuf = curwin->w_buffer;
1040
1041 // Add text to the buffer.
1042 if (text.v_type == VAR_STRING)
1043 {
1044 // just a string
1045 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1046 }
1047 else
1048 {
1049 list_T *l = text.vval.v_list;
1050
1051 if (l->lv_len > 0)
1052 {
1053 if (l->lv_first->li_tv.v_type == VAR_STRING)
1054 // list of strings
1055 add_popup_strings(buf, l);
1056 else
1057 // list of dictionaries
1058 add_popup_dicts(buf, l);
1059 }
1060 }
1061
1062 // delete the line that was in the empty buffer
1063 curbuf = buf;
1064 ml_delete(buf->b_ml.ml_line_count, FALSE);
1065 curbuf = curwin->w_buffer;
1066}
1067
1068/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001069 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001070 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001071 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001072 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001073popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001074{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001075 win_T *wp;
1076 tabpage_T *tp = NULL;
1077 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001078 int new_buffer;
1079 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001080 dict_T *d;
1081 int nr;
1082 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001083
1084 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001085 if (argvars[0].v_type == VAR_NUMBER)
1086 {
1087 buf = buflist_findnr( argvars[0].vval.v_number);
1088 if (buf == NULL)
1089 {
1090 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1091 return NULL;
1092 }
1093 }
1094 else if (!(argvars[0].v_type == VAR_STRING
1095 && argvars[0].vval.v_string != NULL)
1096 && !(argvars[0].v_type == VAR_LIST
1097 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001098 {
1099 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001100 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001101 }
1102 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1103 {
1104 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001105 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001106 }
1107 d = argvars[1].vval.v_dict;
1108
Bram Moolenaara3fce622019-06-20 02:31:49 +02001109 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1110 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1111 else if (type == TYPE_NOTIFICATION)
1112 tabnr = -1; // notifications are global by default
1113 else
1114 tabnr = 0;
1115 if (tabnr > 0)
1116 {
1117 tp = find_tabpage(tabnr);
1118 if (tp == NULL)
1119 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001120 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001121 return NULL;
1122 }
1123 }
1124
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001125 // Create the window and buffer.
1126 wp = win_alloc_popup_win();
1127 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001128 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001129 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001130 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001131 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001132
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001133 if (buf != NULL)
1134 {
1135 // use existing buffer
1136 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001137 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001138 buffer_ensure_loaded(buf);
1139 }
1140 else
1141 {
1142 // create a new buffer associated with the popup
1143 new_buffer = TRUE;
1144 buf = buflist_new(NULL, NULL, (linenr_T)0,
1145 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1146 if (buf == NULL)
1147 return NULL;
1148 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001149
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001150 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001151
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001152 set_local_options_default(wp);
1153 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001154 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001155 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1156 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1157 buf->b_p_ul = -1; // no undo
1158 buf->b_p_swf = FALSE; // no swap file
1159 buf->b_p_bl = FALSE; // unlisted buffer
1160 buf->b_locked = TRUE;
1161 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001162
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001163 // Avoid that 'buftype' is reset when this buffer is entered.
1164 buf->b_p_initialized = TRUE;
1165 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001166
Bram Moolenaara3fce622019-06-20 02:31:49 +02001167 if (tp != NULL)
1168 {
1169 // popup on specified tab page
1170 wp->w_next = tp->tp_first_popupwin;
1171 tp->tp_first_popupwin = wp;
1172 }
1173 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001174 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001175 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001176 wp->w_next = curtab->tp_first_popupwin;
1177 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001178 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001179 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001180 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001181 win_T *prev = first_popupwin;
1182
1183 // Global popup: add at the end, so that it gets displayed on top of
1184 // older ones with the same zindex. Matters for notifications.
1185 if (first_popupwin == NULL)
1186 first_popupwin = wp;
1187 else
1188 {
1189 while (prev->w_next != NULL)
1190 prev = prev->w_next;
1191 prev->w_next = wp;
1192 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001193 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001194
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001195 if (new_buffer)
1196 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001197
Bram Moolenaar17627312019-06-02 19:53:44 +02001198 if (type == TYPE_ATCURSOR)
1199 {
1200 wp->w_popup_pos = POPPOS_BOTLEFT;
1201 setcursor_mayforce(TRUE);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001202 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
Bram Moolenaar17627312019-06-02 19:53:44 +02001203 if (wp->w_wantline == 0) // cursor in first line
1204 {
1205 wp->w_wantline = 2;
1206 wp->w_popup_pos = POPPOS_TOPLEFT;
1207 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001208 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar17627312019-06-02 19:53:44 +02001209 set_moved_values(wp);
1210 set_moved_columns(wp, FIND_STRING);
1211 }
1212
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001213 if (type == TYPE_BEVAL)
1214 {
1215 wp->w_popup_pos = POPPOS_BOTLEFT;
1216
1217 // by default use the mouse position
1218 wp->w_wantline = mouse_row;
1219 if (wp->w_wantline <= 0) // mouse on first line
1220 {
1221 wp->w_wantline = 2;
1222 wp->w_popup_pos = POPPOS_TOPLEFT;
1223 }
1224 wp->w_wantcol = mouse_col + 1;
1225 set_mousemoved_values(wp);
1226 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1227 }
1228
Bram Moolenaar33796b32019-06-08 16:01:13 +02001229 // set default values
1230 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001231 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001232
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001233 if (type == TYPE_NOTIFICATION)
1234 {
1235 win_T *twp, *nextwin;
1236 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001237
1238 // Try to not overlap with another global popup. Guess we need 3
1239 // more screen lines than buffer lines.
1240 wp->w_wantline = 1;
1241 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1242 {
1243 nextwin = twp->w_next;
1244 if (twp != wp
1245 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1246 && twp->w_winrow <= wp->w_wantline - 1 + height
1247 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1248 {
1249 // move to below this popup and restart the loop to check for
1250 // overlap with other popups
1251 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1252 nextwin = first_popupwin;
1253 }
1254 }
1255 if (wp->w_wantline + height > Rows)
1256 {
1257 // can't avoid overlap, put on top in the hope that message goes
1258 // away soon.
1259 wp->w_wantline = 1;
1260 }
1261
1262 wp->w_wantcol = 10;
1263 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001264 wp->w_minwidth = 20;
1265 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001266 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001267 for (i = 0; i < 4; ++i)
1268 wp->w_popup_border[i] = 1;
1269 wp->w_popup_padding[1] = 1;
1270 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001271
1272 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001273 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001274 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1275 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001276 }
1277
Bram Moolenaara730e552019-06-16 19:05:31 +02001278 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001279 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001280 wp->w_popup_pos = POPPOS_CENTER;
1281 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1282 wp->w_popup_drag = 1;
1283 for (i = 0; i < 4; ++i)
1284 {
1285 wp->w_popup_border[i] = 1;
1286 wp->w_popup_padding[i] = 1;
1287 }
1288 }
1289
Bram Moolenaara730e552019-06-16 19:05:31 +02001290 if (type == TYPE_MENU)
1291 {
1292 typval_T tv;
1293 callback_T callback;
1294
1295 tv.v_type = VAR_STRING;
1296 tv.vval.v_string = (char_u *)"popup_filter_menu";
1297 callback = get_callback(&tv);
1298 if (callback.cb_name != NULL)
1299 set_callback(&wp->w_filter_cb, &callback);
1300
1301 wp->w_p_wrap = 0;
1302 }
1303
Bram Moolenaarae943152019-06-16 22:54:14 +02001304 for (i = 0; i < 4; ++i)
1305 VIM_CLEAR(wp->w_border_highlight[i]);
1306 for (i = 0; i < 8; ++i)
1307 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001308 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001309 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001310
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001311 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001312 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001313
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001314#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001315 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1316 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001317#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001318
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001319 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001320
1321 wp->w_vsep_width = 0;
1322
1323 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001324 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001325
1326 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001327}
1328
1329/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001330 * popup_clear()
1331 */
1332 void
1333f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1334{
1335 close_all_popups();
1336}
1337
1338/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001339 * popup_create({text}, {options})
1340 */
1341 void
1342f_popup_create(typval_T *argvars, typval_T *rettv)
1343{
Bram Moolenaar17627312019-06-02 19:53:44 +02001344 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001345}
1346
1347/*
1348 * popup_atcursor({text}, {options})
1349 */
1350 void
1351f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1352{
Bram Moolenaar17627312019-06-02 19:53:44 +02001353 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001354}
1355
1356/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001357 * popup_beval({text}, {options})
1358 */
1359 void
1360f_popup_beval(typval_T *argvars, typval_T *rettv)
1361{
1362 popup_create(argvars, rettv, TYPE_BEVAL);
1363}
1364
1365/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001366 * Invoke the close callback for window "wp" with value "result".
1367 * Careful: The callback may make "wp" invalid!
1368 */
1369 static void
1370invoke_popup_callback(win_T *wp, typval_T *result)
1371{
1372 typval_T rettv;
1373 int dummy;
1374 typval_T argv[3];
1375
1376 argv[0].v_type = VAR_NUMBER;
1377 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1378
1379 if (result != NULL && result->v_type != VAR_UNKNOWN)
1380 copy_tv(result, &argv[1]);
1381 else
1382 {
1383 argv[1].v_type = VAR_NUMBER;
1384 argv[1].vval.v_number = 0;
1385 }
1386
1387 argv[2].v_type = VAR_UNKNOWN;
1388
1389 call_callback(&wp->w_close_cb, -1,
1390 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1391 if (result != NULL)
1392 clear_tv(&argv[1]);
1393 clear_tv(&rettv);
1394}
1395
1396/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001397 * Close popup "wp" and invoke any close callback for it.
1398 */
1399 static void
1400popup_close_and_callback(win_T *wp, typval_T *arg)
1401{
1402 int id = wp->w_id;
1403
1404 if (wp->w_close_cb.cb_name != NULL)
1405 // Careful: This may make "wp" invalid.
1406 invoke_popup_callback(wp, arg);
1407
1408 popup_close(id);
1409}
1410
1411/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001412 * Close popup "wp" because of a mouse click.
1413 */
1414 void
1415popup_close_for_mouse_click(win_T *wp)
1416{
1417 typval_T res;
1418
1419 res.v_type = VAR_NUMBER;
1420 res.vval.v_number = -2;
1421 popup_close_and_callback(wp, &res);
1422}
1423
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001424 static void
1425check_mouse_moved(win_T *wp, win_T *mouse_wp)
1426{
1427 // Close the popup when all if these are true:
1428 // - the mouse is not on this popup
1429 // - "mousemoved" was used
1430 // - the mouse is no longer on the same screen row or the mouse column is
1431 // outside of the relevant text
1432 if (wp != mouse_wp
1433 && wp->w_popup_mouse_row != 0
1434 && (wp->w_popup_mouse_row != mouse_row
1435 || mouse_col < wp->w_popup_mouse_mincol
1436 || mouse_col > wp->w_popup_mouse_maxcol))
1437 {
1438 typval_T res;
1439
1440 res.v_type = VAR_NUMBER;
1441 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001442 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001443 popup_close_and_callback(wp, &res);
1444 }
1445}
1446
1447/*
1448 * Called when the mouse moved: may close a popup with "mousemoved".
1449 */
1450 void
1451popup_handle_mouse_moved(void)
1452{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001453 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001454 win_T *mouse_wp;
1455 int row = mouse_row;
1456 int col = mouse_col;
1457
1458 // find the window where the mouse is in
1459 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1460
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001461 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1462 {
1463 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001464 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001465 }
1466 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1467 {
1468 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001469 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001470 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001471}
1472
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001473/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001474 * In a filter: check if the typed key is a mouse event that is used for
1475 * dragging the popup.
1476 */
1477 static void
1478filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1479{
1480 int row = mouse_row;
1481 int col = mouse_col;
1482
1483 if (wp->w_popup_drag
1484 && is_mouse_key(c)
1485 && (wp == popup_dragwin
1486 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1487 // do not consume the key, allow for dragging the popup
1488 rettv->vval.v_number = 0;
1489}
1490
1491 static void
1492popup_highlight_curline(win_T *wp)
1493{
1494 int id;
1495 char buf[100];
1496
1497 match_delete(wp, 1, FALSE);
1498
1499 id = syn_name2id((char_u *)"PopupSelected");
1500 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1501 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1502 (char_u *)buf, 10, 1, NULL, NULL);
1503}
1504
1505/*
1506 * popup_filter_menu({text}, {options})
1507 */
1508 void
1509f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1510{
1511 int id = tv_get_number(&argvars[0]);
1512 win_T *wp = win_id2wp(id);
1513 char_u *key = tv_get_string(&argvars[1]);
1514 typval_T res;
1515 int c;
1516 linenr_T old_lnum;
1517
1518 // If the popup has been closed do not consume the key.
1519 if (wp == NULL)
1520 return;
1521
1522 c = *key;
1523 if (c == K_SPECIAL && key[1] != NUL)
1524 c = TO_SPECIAL(key[1], key[2]);
1525
1526 // consume all keys until done
1527 rettv->vval.v_number = 1;
1528 res.v_type = VAR_NUMBER;
1529
1530 old_lnum = wp->w_cursor.lnum;
1531 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1532 --wp->w_cursor.lnum;
1533 if ((c == 'j' || c == 'J' || c == K_DOWN)
1534 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1535 ++wp->w_cursor.lnum;
1536 if (old_lnum != wp->w_cursor.lnum)
1537 {
1538 popup_highlight_curline(wp);
1539 return;
1540 }
1541
1542 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1543 {
1544 // Cancelled, invoke callback with -1
1545 res.vval.v_number = -1;
1546 popup_close_and_callback(wp, &res);
1547 return;
1548 }
1549 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1550 {
1551 // Invoke callback with current index.
1552 res.vval.v_number = wp->w_cursor.lnum;
1553 popup_close_and_callback(wp, &res);
1554 return;
1555 }
1556
1557 filter_handle_drag(wp, c, rettv);
1558}
1559
1560/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001561 * popup_filter_yesno({text}, {options})
1562 */
1563 void
1564f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1565{
1566 int id = tv_get_number(&argvars[0]);
1567 win_T *wp = win_id2wp(id);
1568 char_u *key = tv_get_string(&argvars[1]);
1569 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001570 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001571
1572 // If the popup has been closed don't consume the key.
1573 if (wp == NULL)
1574 return;
1575
Bram Moolenaara730e552019-06-16 19:05:31 +02001576 c = *key;
1577 if (c == K_SPECIAL && key[1] != NUL)
1578 c = TO_SPECIAL(key[1], key[2]);
1579
Bram Moolenaara42d9452019-06-15 21:46:30 +02001580 // consume all keys until done
1581 rettv->vval.v_number = 1;
1582
Bram Moolenaara730e552019-06-16 19:05:31 +02001583 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001584 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001585 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001586 res.vval.v_number = 0;
1587 else
1588 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001589 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001590 return;
1591 }
1592
1593 // Invoke callback
1594 res.v_type = VAR_NUMBER;
1595 popup_close_and_callback(wp, &res);
1596}
1597
1598/*
1599 * popup_dialog({text}, {options})
1600 */
1601 void
1602f_popup_dialog(typval_T *argvars, typval_T *rettv)
1603{
1604 popup_create(argvars, rettv, TYPE_DIALOG);
1605}
1606
1607/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001608 * popup_menu({text}, {options})
1609 */
1610 void
1611f_popup_menu(typval_T *argvars, typval_T *rettv)
1612{
1613 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1614
1615 if (wp != NULL)
1616 popup_highlight_curline(wp);
1617}
1618
1619/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001620 * popup_notification({text}, {options})
1621 */
1622 void
1623f_popup_notification(typval_T *argvars, typval_T *rettv)
1624{
1625 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1626}
1627
1628/*
1629 * Find the popup window with window-ID "id".
1630 * If the popup window does not exist NULL is returned.
1631 * If the window is not a popup window, and error message is given.
1632 */
1633 static win_T *
1634find_popup_win(int id)
1635{
1636 win_T *wp = win_id2wp(id);
1637
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001638 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001639 {
1640 semsg(_("E993: window %d is not a popup window"), id);
1641 return NULL;
1642 }
1643 return wp;
1644}
1645
1646/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001647 * popup_close({id})
1648 */
1649 void
1650f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1651{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001652 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001653 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001654
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001655 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001656 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001657}
1658
1659/*
1660 * popup_hide({id})
1661 */
1662 void
1663f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1664{
1665 int id = (int)tv_get_number(argvars);
1666 win_T *wp = find_popup_win(id);
1667
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001668 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001669 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001670 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001671 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001672 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001673 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001674 }
1675}
1676
1677/*
1678 * popup_show({id})
1679 */
1680 void
1681f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1682{
1683 int id = (int)tv_get_number(argvars);
1684 win_T *wp = find_popup_win(id);
1685
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001686 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001687 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001688 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001689 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001690 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001691 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001692 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001693}
1694
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001695/*
1696 * popup_settext({id}, {text})
1697 */
1698 void
1699f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1700{
1701 int id = (int)tv_get_number(&argvars[0]);
1702 win_T *wp = find_popup_win(id);
1703
1704 if (wp != NULL)
1705 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001706 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1707 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1708 else
1709 {
1710 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1711 popup_adjust_position(wp);
1712 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001713 }
1714}
1715
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001716 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001717popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001718{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001719 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001720 if (wp->w_winrow + wp->w_height >= cmdline_row)
1721 clear_cmdline = TRUE;
1722 win_free_popup(wp);
1723 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001724 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001725}
1726
Bram Moolenaarec583842019-05-26 14:11:23 +02001727/*
1728 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001729 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001730 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001731 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001732popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001733{
1734 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001735 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001736 win_T *prev = NULL;
1737
Bram Moolenaarec583842019-05-26 14:11:23 +02001738 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001739 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001740 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001741 {
1742 if (prev == NULL)
1743 first_popupwin = wp->w_next;
1744 else
1745 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001746 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001747 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001748 }
1749
Bram Moolenaarec583842019-05-26 14:11:23 +02001750 // go through tab-local popups
1751 FOR_ALL_TABPAGES(tp)
1752 popup_close_tabpage(tp, id);
1753}
1754
1755/*
1756 * Close a popup window with Window-id "id" in tabpage "tp".
1757 */
1758 void
1759popup_close_tabpage(tabpage_T *tp, int id)
1760{
1761 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001762 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001763 win_T *prev = NULL;
1764
Bram Moolenaarec583842019-05-26 14:11:23 +02001765 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1766 if (wp->w_id == id)
1767 {
1768 if (prev == NULL)
1769 *root = wp->w_next;
1770 else
1771 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001772 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001773 return;
1774 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001775}
1776
1777 void
1778close_all_popups(void)
1779{
1780 while (first_popupwin != NULL)
1781 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001782 while (curtab->tp_first_popupwin != NULL)
1783 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001784}
1785
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001786/*
1787 * popup_move({id}, {options})
1788 */
1789 void
1790f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1791{
Bram Moolenaarae943152019-06-16 22:54:14 +02001792 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001793 int id = (int)tv_get_number(argvars);
1794 win_T *wp = find_popup_win(id);
1795
1796 if (wp == NULL)
1797 return; // invalid {id}
1798
1799 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1800 {
1801 emsg(_(e_dictreq));
1802 return;
1803 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001804 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001805
Bram Moolenaarae943152019-06-16 22:54:14 +02001806 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001807
1808 if (wp->w_winrow + wp->w_height >= cmdline_row)
1809 clear_cmdline = TRUE;
1810 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001811}
1812
Bram Moolenaarbc133542019-05-29 20:26:46 +02001813/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001814 * popup_setoptions({id}, {options})
1815 */
1816 void
1817f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1818{
1819 dict_T *dict;
1820 int id = (int)tv_get_number(argvars);
1821 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001822 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001823
1824 if (wp == NULL)
1825 return; // invalid {id}
1826
1827 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1828 {
1829 emsg(_(e_dictreq));
1830 return;
1831 }
1832 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001833 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001834
1835 apply_move_options(wp, dict);
1836 apply_general_options(wp, dict);
1837
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001838 if (old_firstline != wp->w_firstline)
1839 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001840 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001841 popup_adjust_position(wp);
1842}
1843
1844/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001845 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001846 */
1847 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001848f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001849{
1850 dict_T *dict;
1851 int id = (int)tv_get_number(argvars);
1852 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001853 int top_extra;
1854 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001855
1856 if (rettv_dict_alloc(rettv) == OK)
1857 {
1858 if (wp == NULL)
1859 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001860 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001861 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1862
Bram Moolenaarbc133542019-05-29 20:26:46 +02001863 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001864
Bram Moolenaarbc133542019-05-29 20:26:46 +02001865 dict_add_number(dict, "line", wp->w_winrow + 1);
1866 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001867 dict_add_number(dict, "width", wp->w_width + left_extra
1868 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1869 dict_add_number(dict, "height", wp->w_height + top_extra
1870 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001871
1872 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1873 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1874 dict_add_number(dict, "core_width", wp->w_width);
1875 dict_add_number(dict, "core_height", wp->w_height);
1876
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001877 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001878 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001879 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001880 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001881 }
1882}
1883
1884/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001885 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1886 */
1887 static void
1888get_padding_border(dict_T *dict, int *array, char *name)
1889{
1890 list_T *list;
1891 int i;
1892
1893 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1894 return;
1895
1896 list = list_alloc();
1897 if (list != NULL)
1898 {
1899 dict_add_list(dict, name, list);
1900 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1901 for (i = 0; i < 4; ++i)
1902 list_append_number(list, array[i]);
1903 }
1904}
1905
1906/*
1907 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1908 */
1909 static void
1910get_borderhighlight(dict_T *dict, win_T *wp)
1911{
1912 list_T *list;
1913 int i;
1914
1915 for (i = 0; i < 4; ++i)
1916 if (wp->w_border_highlight[i] != NULL)
1917 break;
1918 if (i == 4)
1919 return;
1920
1921 list = list_alloc();
1922 if (list != NULL)
1923 {
1924 dict_add_list(dict, "borderhighlight", list);
1925 for (i = 0; i < 4; ++i)
1926 list_append_string(list, wp->w_border_highlight[i], -1);
1927 }
1928}
1929
1930/*
1931 * For popup_getoptions(): add a "borderchars" entry to "dict".
1932 */
1933 static void
1934get_borderchars(dict_T *dict, win_T *wp)
1935{
1936 list_T *list;
1937 int i;
1938 char_u buf[NUMBUFLEN];
1939 int len;
1940
1941 for (i = 0; i < 8; ++i)
1942 if (wp->w_border_char[i] != 0)
1943 break;
1944 if (i == 8)
1945 return;
1946
1947 list = list_alloc();
1948 if (list != NULL)
1949 {
1950 dict_add_list(dict, "borderchars", list);
1951 for (i = 0; i < 8; ++i)
1952 {
1953 len = mb_char2bytes(wp->w_border_char[i], buf);
1954 list_append_string(list, buf, len);
1955 }
1956 }
1957}
1958
1959/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001960 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001961 */
1962 static void
1963get_moved_list(dict_T *dict, win_T *wp)
1964{
1965 list_T *list;
1966
1967 list = list_alloc();
1968 if (list != NULL)
1969 {
1970 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001971 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02001972 list_append_number(list, wp->w_popup_mincol);
1973 list_append_number(list, wp->w_popup_maxcol);
1974 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001975 list = list_alloc();
1976 if (list != NULL)
1977 {
1978 dict_add_list(dict, "mousemoved", list);
1979 list_append_number(list, wp->w_popup_mouse_row);
1980 list_append_number(list, wp->w_popup_mouse_mincol);
1981 list_append_number(list, wp->w_popup_mouse_maxcol);
1982 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001983}
1984
1985/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001986 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001987 */
1988 void
1989f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1990{
1991 dict_T *dict;
1992 int id = (int)tv_get_number(argvars);
1993 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001994 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001995 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001996
1997 if (rettv_dict_alloc(rettv) == OK)
1998 {
1999 if (wp == NULL)
2000 return;
2001
2002 dict = rettv->vval.v_dict;
2003 dict_add_number(dict, "line", wp->w_wantline);
2004 dict_add_number(dict, "col", wp->w_wantcol);
2005 dict_add_number(dict, "minwidth", wp->w_minwidth);
2006 dict_add_number(dict, "minheight", wp->w_minheight);
2007 dict_add_number(dict, "maxheight", wp->w_maxheight);
2008 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002009 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002010 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002011 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002012 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002013 dict_add_string(dict, "title", wp->w_popup_title);
2014 dict_add_number(dict, "wrap", wp->w_p_wrap);
2015 dict_add_number(dict, "drag", wp->w_popup_drag);
2016 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002017 if (wp->w_scrollbar_highlight != NULL)
2018 dict_add_string(dict, "scrollbarhighlight",
2019 wp->w_scrollbar_highlight);
2020 if (wp->w_thumb_highlight != NULL)
2021 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002022
Bram Moolenaara3fce622019-06-20 02:31:49 +02002023 // find the tabpage that holds this popup
2024 i = 1;
2025 FOR_ALL_TABPAGES(tp)
2026 {
2027 win_T *p;
2028
2029 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2030 if (p->w_id == id)
2031 break;
2032 if (p != NULL)
2033 break;
2034 ++i;
2035 }
2036 if (tp == NULL)
2037 i = -1; // must be global
2038 else if (tp == curtab)
2039 i = 0;
2040 dict_add_number(dict, "tabpage", i);
2041
Bram Moolenaarae943152019-06-16 22:54:14 +02002042 get_padding_border(dict, wp->w_popup_padding, "padding");
2043 get_padding_border(dict, wp->w_popup_border, "border");
2044 get_borderhighlight(dict, wp);
2045 get_borderchars(dict, wp);
2046 get_moved_list(dict, wp);
2047
2048 if (wp->w_filter_cb.cb_name != NULL)
2049 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2050 if (wp->w_close_cb.cb_name != NULL)
2051 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002052
2053 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2054 ++i)
2055 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2056 {
2057 dict_add_string(dict, "pos",
2058 (char_u *)poppos_entries[i].pp_name);
2059 break;
2060 }
2061
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002062 dict_add_string(dict, "close", (char_u *)(
2063 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2064 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2065
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002066# if defined(FEAT_TIMERS)
2067 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2068 ? (long)wp->w_popup_timer->tr_interval : 0L);
2069# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002070 }
2071}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002072
2073 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002074error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002075{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002076 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002077 {
2078 emsg(_("E994: Not allowed in a popup window"));
2079 return TRUE;
2080 }
2081 return FALSE;
2082}
2083
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002084/*
2085 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002086 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002087 */
2088 void
2089popup_reset_handled()
2090{
2091 win_T *wp;
2092
2093 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2094 wp->w_popup_flags &= ~POPF_HANDLED;
2095 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2096 wp->w_popup_flags &= ~POPF_HANDLED;
2097}
2098
2099/*
2100 * Find the next visible popup where POPF_HANDLED is not set.
2101 * Must have called popup_reset_handled() first.
2102 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2103 * popup with the highest zindex.
2104 */
2105 win_T *
2106find_next_popup(int lowest)
2107{
2108 win_T *wp;
2109 win_T *found_wp;
2110 int found_zindex;
2111
2112 found_zindex = lowest ? INT_MAX : 0;
2113 found_wp = NULL;
2114 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2115 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2116 && (lowest ? wp->w_zindex < found_zindex
2117 : wp->w_zindex > found_zindex))
2118 {
2119 found_zindex = wp->w_zindex;
2120 found_wp = wp;
2121 }
2122 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2123 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2124 && (lowest ? wp->w_zindex < found_zindex
2125 : wp->w_zindex > found_zindex))
2126 {
2127 found_zindex = wp->w_zindex;
2128 found_wp = wp;
2129 }
2130
2131 if (found_wp != NULL)
2132 found_wp->w_popup_flags |= POPF_HANDLED;
2133 return found_wp;
2134}
2135
2136/*
2137 * Invoke the filter callback for window "wp" with typed character "c".
2138 * Uses the global "mod_mask" for modifiers.
2139 * Returns the return value of the filter.
2140 * Careful: The filter may make "wp" invalid!
2141 */
2142 static int
2143invoke_popup_filter(win_T *wp, int c)
2144{
2145 int res;
2146 typval_T rettv;
2147 int dummy;
2148 typval_T argv[3];
2149 char_u buf[NUMBUFLEN];
2150
Bram Moolenaara42d9452019-06-15 21:46:30 +02002151 // Emergency exit: CTRL-C closes the popup.
2152 if (c == Ctrl_C)
2153 {
2154 rettv.v_type = VAR_NUMBER;
2155 rettv.vval.v_number = -1;
2156 popup_close_and_callback(wp, &rettv);
2157 return 1;
2158 }
2159
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002160 argv[0].v_type = VAR_NUMBER;
2161 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2162
2163 // Convert the number to a string, so that the function can use:
2164 // if a:c == "\<F2>"
2165 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2166 argv[1].v_type = VAR_STRING;
2167 argv[1].vval.v_string = vim_strsave(buf);
2168
2169 argv[2].v_type = VAR_UNKNOWN;
2170
Bram Moolenaara42d9452019-06-15 21:46:30 +02002171 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002172 call_callback(&wp->w_filter_cb, -1,
2173 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2174 res = tv_get_number(&rettv);
2175 vim_free(argv[1].vval.v_string);
2176 clear_tv(&rettv);
2177 return res;
2178}
2179
2180/*
2181 * Called when "c" was typed: invoke popup filter callbacks.
2182 * Returns TRUE when the character was consumed,
2183 */
2184 int
2185popup_do_filter(int c)
2186{
2187 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002188 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002189
2190 popup_reset_handled();
2191
2192 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2193 if (wp->w_filter_cb.cb_name != NULL)
2194 res = invoke_popup_filter(wp, c);
2195
2196 return res;
2197}
2198
Bram Moolenaar3397f742019-06-02 18:40:06 +02002199/*
2200 * Called when the cursor moved: check if any popup needs to be closed if the
2201 * cursor moved far enough.
2202 */
2203 void
2204popup_check_cursor_pos()
2205{
2206 win_T *wp;
2207 typval_T tv;
2208
2209 popup_reset_handled();
2210 while ((wp = find_next_popup(TRUE)) != NULL)
2211 if (wp->w_popup_curwin != NULL
2212 && (curwin != wp->w_popup_curwin
2213 || curwin->w_cursor.lnum != wp->w_popup_lnum
2214 || curwin->w_cursor.col < wp->w_popup_mincol
2215 || curwin->w_cursor.col > wp->w_popup_maxcol))
2216 {
2217 tv.v_type = VAR_NUMBER;
2218 tv.vval.v_number = -1;
2219 popup_close_and_callback(wp, &tv);
2220 }
2221}
2222
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002223/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002224 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2225 * "col" and "line" are screen coordinates.
2226 */
2227 static int
2228popup_masked(win_T *wp, int screencol, int screenline)
2229{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002230 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002231 int line = screenline - wp->w_winrow + 1;
2232 listitem_T *lio, *li;
2233 int width, height;
2234
2235 if (wp->w_popup_mask == NULL)
2236 return FALSE;
2237 width = popup_width(wp);
2238 height = popup_height(wp);
2239
2240 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2241 {
2242 int cols, cole;
2243 int lines, linee;
2244
2245 li = lio->li_tv.vval.v_list->lv_first;
2246 cols = tv_get_number(&li->li_tv);
2247 if (cols < 0)
2248 cols = width + cols + 1;
2249 if (col < cols)
2250 continue;
2251 li = li->li_next;
2252 cole = tv_get_number(&li->li_tv);
2253 if (cole < 0)
2254 cole = width + cole + 1;
2255 if (col > cole)
2256 continue;
2257 li = li->li_next;
2258 lines = tv_get_number(&li->li_tv);
2259 if (lines < 0)
2260 lines = height + lines + 1;
2261 if (line < lines)
2262 continue;
2263 li = li->li_next;
2264 linee = tv_get_number(&li->li_tv);
2265 if (linee < 0)
2266 linee = height + linee + 1;
2267 if (line > linee)
2268 continue;
2269 return TRUE;
2270 }
2271 return FALSE;
2272}
2273
2274/*
2275 * Set flags in popup_transparent[] for window "wp" to "val".
2276 */
2277 static void
2278update_popup_transparent(win_T *wp, int val)
2279{
2280 if (wp->w_popup_mask != NULL)
2281 {
2282 int width = popup_width(wp);
2283 int height = popup_height(wp);
2284 listitem_T *lio, *li;
2285 int cols, cole;
2286 int lines, linee;
2287 int col, line;
2288
2289 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2290 {
2291 li = lio->li_tv.vval.v_list->lv_first;
2292 cols = tv_get_number(&li->li_tv);
2293 if (cols < 0)
2294 cols = width + cols + 1;
2295 li = li->li_next;
2296 cole = tv_get_number(&li->li_tv);
2297 if (cole < 0)
2298 cole = width + cole + 1;
2299 li = li->li_next;
2300 lines = tv_get_number(&li->li_tv);
2301 if (lines < 0)
2302 lines = height + lines + 1;
2303 li = li->li_next;
2304 linee = tv_get_number(&li->li_tv);
2305 if (linee < 0)
2306 linee = height + linee + 1;
2307
2308 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002309 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002310 if (cols < 0)
2311 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002312 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002313 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002314 if (lines < 0)
2315 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002316 for (line = lines; line < linee && line < screen_Rows; ++line)
2317 for (col = cols; col < cole && col < screen_Columns; ++col)
2318 popup_transparent[(line + wp->w_winrow) * screen_Columns
2319 + col + wp->w_wincol] = val;
2320 }
2321 }
2322}
2323
2324/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002325 * Update "popup_mask" if needed.
2326 * Also recomputes the popup size and positions.
2327 * Also updates "popup_visible".
2328 * Also marks window lines for redrawing.
2329 */
2330 void
2331may_update_popup_mask(int type)
2332{
2333 win_T *wp;
2334 short *mask;
2335 int line, col;
2336 int redraw_all = FALSE;
2337
2338 // Need to recompute when switching tabs.
2339 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2340 // (such as the screen size) must have changed.
2341 if (popup_mask_tab != curtab || type >= NOT_VALID)
2342 {
2343 popup_mask_refresh = TRUE;
2344 redraw_all = TRUE;
2345 }
2346 if (!popup_mask_refresh)
2347 {
2348 // Check if any buffer has changed.
2349 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2350 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2351 popup_mask_refresh = TRUE;
2352 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2353 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2354 popup_mask_refresh = TRUE;
2355 if (!popup_mask_refresh)
2356 return;
2357 }
2358
2359 // Need to update the mask, something has changed.
2360 popup_mask_refresh = FALSE;
2361 popup_mask_tab = curtab;
2362 popup_visible = FALSE;
2363
2364 // If redrawing everything, just update "popup_mask".
2365 // If redrawing only what is needed, update "popup_mask_next" and then
2366 // compare with "popup_mask" to see what changed.
2367 if (type >= SOME_VALID)
2368 mask = popup_mask;
2369 else
2370 mask = popup_mask_next;
2371 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2372
2373 // Find the window with the lowest zindex that hasn't been handled yet,
2374 // so that the window with a higher zindex overwrites the value in
2375 // popup_mask.
2376 popup_reset_handled();
2377 while ((wp = find_next_popup(TRUE)) != NULL)
2378 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002379 int height;
2380 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002381
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002382 popup_visible = TRUE;
2383
2384 // Recompute the position if the text changed.
2385 if (redraw_all
2386 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2387 popup_adjust_position(wp);
2388
Bram Moolenaard529ba52019-07-02 23:13:53 +02002389 height = popup_height(wp);
2390 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002391 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002392 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002393 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002394 col < wp->w_wincol + width && col < screen_Columns; ++col)
2395 if (!popup_masked(wp, col, line))
2396 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002397 }
2398
2399 // Only check which lines are to be updated if not already
2400 // updating all lines.
2401 if (mask == popup_mask_next)
2402 for (line = 0; line < screen_Rows; ++line)
2403 {
2404 int col_done = 0;
2405
2406 for (col = 0; col < screen_Columns; ++col)
2407 {
2408 int off = line * screen_Columns + col;
2409
2410 if (popup_mask[off] != popup_mask_next[off])
2411 {
2412 popup_mask[off] = popup_mask_next[off];
2413
2414 if (line >= cmdline_row)
2415 {
2416 // the command line needs to be cleared if text below
2417 // the popup is now visible.
2418 if (!msg_scrolled && popup_mask_next[off] == 0)
2419 clear_cmdline = TRUE;
2420 }
2421 else if (col >= col_done)
2422 {
2423 linenr_T lnum;
2424 int line_cp = line;
2425 int col_cp = col;
2426
2427 // The screen position "line" / "col" needs to be
2428 // redrawn. Figure out what window that is and update
2429 // w_redraw_top and w_redr_bot. Only needs to be done
2430 // once for each window line.
2431 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2432 if (wp != NULL)
2433 {
2434 if (line_cp >= wp->w_height)
2435 // In (or below) status line
2436 wp->w_redr_status = TRUE;
2437 // compute the position in the buffer line from the
2438 // position on the screen
2439 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2440 &lnum))
2441 // past bottom
2442 wp->w_redr_status = TRUE;
2443 else
2444 redrawWinline(wp, lnum);
2445
2446 // This line is going to be redrawn, no need to
2447 // check until the right side of the window.
2448 col_done = wp->w_wincol + wp->w_width - 1;
2449 }
2450 }
2451 }
2452 }
2453 }
2454}
2455
2456/*
2457 * Return a string of "len" spaces in IObuff.
2458 */
2459 static char_u *
2460get_spaces(int len)
2461{
2462 vim_memset(IObuff, ' ', (size_t)len);
2463 IObuff[len] = NUL;
2464 return IObuff;
2465}
2466
2467/*
2468 * Update popup windows. They are drawn on top of normal windows.
2469 * "win_update" is called for each popup window, lowest zindex first.
2470 */
2471 void
2472update_popups(void (*win_update)(win_T *wp))
2473{
2474 win_T *wp;
2475 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002476 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002477 int total_width;
2478 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002479 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002480 int popup_attr;
2481 int border_attr[4];
2482 int border_char[8];
2483 char_u buf[MB_MAXBYTES];
2484 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002485 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002486 int padcol = 0;
2487 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002488 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002489 int sb_thumb_top = 0;
2490 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002491 int attr_scroll = 0;
2492 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002493
2494 // Find the window with the lowest zindex that hasn't been updated yet,
2495 // so that the window with a higher zindex is drawn later, thus goes on
2496 // top.
2497 popup_reset_handled();
2498 while ((wp = find_next_popup(TRUE)) != NULL)
2499 {
2500 // This drawing uses the zindex of the popup window, so that it's on
2501 // top of the text but doesn't draw when another popup with higher
2502 // zindex is on top of the character.
2503 screen_zindex = wp->w_zindex;
2504
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002505 // Set flags in popup_transparent[] for masked cells.
2506 update_popup_transparent(wp, 1);
2507
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002508 // adjust w_winrow and w_wincol for border and padding, since
2509 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002510 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002511 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2512 - wp->w_popup_leftoff;
2513 if (wp->w_wincol + left_extra < 0)
2514 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002515 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002516 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002517
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002518 // Draw the popup text, unless it's off screen.
2519 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2520 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002521
2522 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002523 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002524
Bram Moolenaard529ba52019-07-02 23:13:53 +02002525 total_width = popup_width(wp);
2526 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002527 popup_attr = get_wcr_attr(wp);
2528
2529 // We can only use these line drawing characters when 'encoding' is
2530 // "utf-8" and 'ambiwidth' is "single".
2531 if (enc_utf8 && *p_ambw == 's')
2532 {
2533 border_char[0] = border_char[2] = 0x2550;
2534 border_char[1] = border_char[3] = 0x2551;
2535 border_char[4] = 0x2554;
2536 border_char[5] = 0x2557;
2537 border_char[6] = 0x255d;
2538 border_char[7] = 0x255a;
2539 }
2540 else
2541 {
2542 border_char[0] = border_char[2] = '-';
2543 border_char[1] = border_char[3] = '|';
2544 for (i = 4; i < 8; ++i)
2545 border_char[i] = '+';
2546 }
2547 for (i = 0; i < 8; ++i)
2548 if (wp->w_border_char[i] != 0)
2549 border_char[i] = wp->w_border_char[i];
2550
2551 for (i = 0; i < 4; ++i)
2552 {
2553 border_attr[i] = popup_attr;
2554 if (wp->w_border_highlight[i] != NULL)
2555 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2556 }
2557
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002558 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002559 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002560 if (wp->w_popup_border[0] > 0)
2561 {
2562 // top border
2563 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002564 wincol < 0 ? 0 : wincol, wincol + total_width,
2565 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002566 ? border_char[4] : border_char[0],
2567 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002568 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002569 {
2570 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2571 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002572 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002573 }
2574 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002575 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2576 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002577
Bram Moolenaard529ba52019-07-02 23:13:53 +02002578 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2579 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002580 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002581 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2582 - wp->w_has_scrollbar;
2583 if (padcol < 0)
2584 {
2585 padwidth += padcol;
2586 padcol = 0;
2587 }
2588 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002589 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002590 {
2591 // top padding
2592 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002593 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002594 ' ', ' ', popup_attr);
2595 }
2596
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002597 // Title goes on top of border or padding.
2598 if (wp->w_popup_title != NULL)
2599 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2600 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2601
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002602 // Compute scrollbar thumb position and size.
2603 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002604 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002605 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2606
Bram Moolenaar68acb412019-06-26 03:40:36 +02002607 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2608 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002609 if (sb_thumb_height == 0)
2610 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002611 if (linecount <= wp->w_height)
2612 // it just fits, avoid divide by zero
2613 sb_thumb_top = 0;
2614 else
2615 sb_thumb_top = (wp->w_topline - 1
2616 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002617 * (wp->w_height - sb_thumb_height)
2618 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002619 if (wp->w_scrollbar_highlight != NULL)
2620 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2621 else
2622 attr_scroll = highlight_attr[HLF_PSB];
2623 if (wp->w_thumb_highlight != NULL)
2624 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2625 else
2626 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002627 }
2628
2629 for (i = wp->w_popup_border[0];
2630 i < total_height - wp->w_popup_border[2]; ++i)
2631 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002632 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002633 // left and right padding only needed next to the body
2634 int do_padding =
2635 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2636 && i < total_height - wp->w_popup_border[2]
2637 - wp->w_popup_padding[2];
2638
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002639 row = wp->w_winrow + i;
2640
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002641 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002642 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002643 {
2644 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002645 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002646 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002647 if (do_padding && wp->w_popup_padding[3] > 0)
2648 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002649 int col = wincol + wp->w_popup_border[3];
2650
Bram Moolenaard529ba52019-07-02 23:13:53 +02002651 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002652 pad_left = wp->w_popup_padding[3];
2653 if (col < 0)
2654 {
2655 pad_left += col;
2656 col = 0;
2657 }
2658 if (pad_left > 0)
2659 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2660 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002661 // scrollbar
2662 if (wp->w_has_scrollbar)
2663 {
2664 int line = i - top_off;
2665 int scroll_col = wp->w_wincol + total_width - 1
2666 - wp->w_popup_border[1];
2667
2668 if (line >= 0 && line < wp->w_height)
2669 screen_putchar(' ', row, scroll_col,
2670 line >= sb_thumb_top
2671 && line < sb_thumb_top + sb_thumb_height
2672 ? attr_thumb : attr_scroll);
2673 else
2674 screen_putchar(' ', row, scroll_col, popup_attr);
2675 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002676 // right border
2677 if (wp->w_popup_border[1] > 0)
2678 {
2679 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002680 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002681 }
2682 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002683 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002684 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002685 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002686 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2687 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002688 }
2689
2690 if (wp->w_popup_padding[2] > 0)
2691 {
2692 // bottom padding
2693 row = wp->w_winrow + wp->w_popup_border[0]
2694 + wp->w_popup_padding[0] + wp->w_height;
2695 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002696 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002697 }
2698
2699 if (wp->w_popup_border[2] > 0)
2700 {
2701 // bottom border
2702 row = wp->w_winrow + total_height - 1;
2703 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002704 wincol < 0 ? 0 : wincol,
2705 wincol + total_width,
2706 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002707 ? border_char[7] : border_char[2],
2708 border_char[2], border_attr[2]);
2709 if (wp->w_popup_border[1] > 0)
2710 {
2711 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002712 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002713 }
2714 }
2715
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002716 if (wp->w_popup_close == POPCLOSE_BUTTON)
2717 {
2718 // close button goes on top of anything at the top-right corner
2719 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002720 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002721 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2722 }
2723
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002724 update_popup_transparent(wp, 0);
2725
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002726 // Back to the normal zindex.
2727 screen_zindex = 0;
2728 }
2729}
2730
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002731/*
2732 * Mark references in callbacks of one popup window.
2733 */
2734 static int
2735set_ref_in_one_popup(win_T *wp, int copyID)
2736{
2737 int abort = FALSE;
2738 typval_T tv;
2739
2740 if (wp->w_close_cb.cb_partial != NULL)
2741 {
2742 tv.v_type = VAR_PARTIAL;
2743 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2744 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2745 }
2746 if (wp->w_filter_cb.cb_partial != NULL)
2747 {
2748 tv.v_type = VAR_PARTIAL;
2749 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2750 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2751 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002752 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002753 return abort;
2754}
2755
2756/*
2757 * Set reference in callbacks of popup windows.
2758 */
2759 int
2760set_ref_in_popups(int copyID)
2761{
2762 int abort = FALSE;
2763 win_T *wp;
2764 tabpage_T *tp;
2765
2766 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2767 abort = abort || set_ref_in_one_popup(wp, copyID);
2768
2769 FOR_ALL_TABPAGES(tp)
2770 {
2771 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2772 abort = abort || set_ref_in_one_popup(wp, copyID);
2773 if (abort)
2774 break;
2775 }
2776 return abort;
2777}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002778#endif // FEAT_TEXT_PROP