blob: 59aaa6617bbf6473343b683b22951211cafee437 [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,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +0200191 NULL, NULL, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192 {
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
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001440ch_log(NULL, "closing popup %d", wp->w_id);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001441 res.v_type = VAR_NUMBER;
1442 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001443 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001444 popup_close_and_callback(wp, &res);
1445 }
1446}
1447
1448/*
1449 * Called when the mouse moved: may close a popup with "mousemoved".
1450 */
1451 void
1452popup_handle_mouse_moved(void)
1453{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001454 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001455 win_T *mouse_wp;
1456 int row = mouse_row;
1457 int col = mouse_col;
1458
1459 // find the window where the mouse is in
1460 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1461
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001462 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1463 {
1464 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001465 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001466 }
1467 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1468 {
1469 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001470 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001471 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001472}
1473
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001474/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001475 * In a filter: check if the typed key is a mouse event that is used for
1476 * dragging the popup.
1477 */
1478 static void
1479filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1480{
1481 int row = mouse_row;
1482 int col = mouse_col;
1483
1484 if (wp->w_popup_drag
1485 && is_mouse_key(c)
1486 && (wp == popup_dragwin
1487 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1488 // do not consume the key, allow for dragging the popup
1489 rettv->vval.v_number = 0;
1490}
1491
1492 static void
1493popup_highlight_curline(win_T *wp)
1494{
1495 int id;
1496 char buf[100];
1497
1498 match_delete(wp, 1, FALSE);
1499
1500 id = syn_name2id((char_u *)"PopupSelected");
1501 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1502 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1503 (char_u *)buf, 10, 1, NULL, NULL);
1504}
1505
1506/*
1507 * popup_filter_menu({text}, {options})
1508 */
1509 void
1510f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1511{
1512 int id = tv_get_number(&argvars[0]);
1513 win_T *wp = win_id2wp(id);
1514 char_u *key = tv_get_string(&argvars[1]);
1515 typval_T res;
1516 int c;
1517 linenr_T old_lnum;
1518
1519 // If the popup has been closed do not consume the key.
1520 if (wp == NULL)
1521 return;
1522
1523 c = *key;
1524 if (c == K_SPECIAL && key[1] != NUL)
1525 c = TO_SPECIAL(key[1], key[2]);
1526
1527 // consume all keys until done
1528 rettv->vval.v_number = 1;
1529 res.v_type = VAR_NUMBER;
1530
1531 old_lnum = wp->w_cursor.lnum;
1532 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1533 --wp->w_cursor.lnum;
1534 if ((c == 'j' || c == 'J' || c == K_DOWN)
1535 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1536 ++wp->w_cursor.lnum;
1537 if (old_lnum != wp->w_cursor.lnum)
1538 {
1539 popup_highlight_curline(wp);
1540 return;
1541 }
1542
1543 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1544 {
1545 // Cancelled, invoke callback with -1
1546 res.vval.v_number = -1;
1547 popup_close_and_callback(wp, &res);
1548 return;
1549 }
1550 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1551 {
1552 // Invoke callback with current index.
1553 res.vval.v_number = wp->w_cursor.lnum;
1554 popup_close_and_callback(wp, &res);
1555 return;
1556 }
1557
1558 filter_handle_drag(wp, c, rettv);
1559}
1560
1561/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001562 * popup_filter_yesno({text}, {options})
1563 */
1564 void
1565f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1566{
1567 int id = tv_get_number(&argvars[0]);
1568 win_T *wp = win_id2wp(id);
1569 char_u *key = tv_get_string(&argvars[1]);
1570 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001571 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001572
1573 // If the popup has been closed don't consume the key.
1574 if (wp == NULL)
1575 return;
1576
Bram Moolenaara730e552019-06-16 19:05:31 +02001577 c = *key;
1578 if (c == K_SPECIAL && key[1] != NUL)
1579 c = TO_SPECIAL(key[1], key[2]);
1580
Bram Moolenaara42d9452019-06-15 21:46:30 +02001581 // consume all keys until done
1582 rettv->vval.v_number = 1;
1583
Bram Moolenaara730e552019-06-16 19:05:31 +02001584 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001585 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001586 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001587 res.vval.v_number = 0;
1588 else
1589 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001590 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001591 return;
1592 }
1593
1594 // Invoke callback
1595 res.v_type = VAR_NUMBER;
1596 popup_close_and_callback(wp, &res);
1597}
1598
1599/*
1600 * popup_dialog({text}, {options})
1601 */
1602 void
1603f_popup_dialog(typval_T *argvars, typval_T *rettv)
1604{
1605 popup_create(argvars, rettv, TYPE_DIALOG);
1606}
1607
1608/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001609 * popup_menu({text}, {options})
1610 */
1611 void
1612f_popup_menu(typval_T *argvars, typval_T *rettv)
1613{
1614 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1615
1616 if (wp != NULL)
1617 popup_highlight_curline(wp);
1618}
1619
1620/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001621 * popup_notification({text}, {options})
1622 */
1623 void
1624f_popup_notification(typval_T *argvars, typval_T *rettv)
1625{
1626 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1627}
1628
1629/*
1630 * Find the popup window with window-ID "id".
1631 * If the popup window does not exist NULL is returned.
1632 * If the window is not a popup window, and error message is given.
1633 */
1634 static win_T *
1635find_popup_win(int id)
1636{
1637 win_T *wp = win_id2wp(id);
1638
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001639 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001640 {
1641 semsg(_("E993: window %d is not a popup window"), id);
1642 return NULL;
1643 }
1644 return wp;
1645}
1646
1647/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001648 * popup_close({id})
1649 */
1650 void
1651f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1652{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001653 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001654 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001655
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001656 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001657 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001658}
1659
1660/*
1661 * popup_hide({id})
1662 */
1663 void
1664f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1665{
1666 int id = (int)tv_get_number(argvars);
1667 win_T *wp = find_popup_win(id);
1668
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001669 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001670 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001671 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001672 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001673 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001674 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001675 }
1676}
1677
1678/*
1679 * popup_show({id})
1680 */
1681 void
1682f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1683{
1684 int id = (int)tv_get_number(argvars);
1685 win_T *wp = find_popup_win(id);
1686
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001687 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001688 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001689 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001690 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001691 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001692 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001693 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001694}
1695
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001696/*
1697 * popup_settext({id}, {text})
1698 */
1699 void
1700f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1701{
1702 int id = (int)tv_get_number(&argvars[0]);
1703 win_T *wp = find_popup_win(id);
1704
1705 if (wp != NULL)
1706 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001707 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1708 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1709 else
1710 {
1711 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1712 popup_adjust_position(wp);
1713 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001714 }
1715}
1716
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001717 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001718popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001719{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001720 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001721 if (wp->w_winrow + wp->w_height >= cmdline_row)
1722 clear_cmdline = TRUE;
1723 win_free_popup(wp);
1724 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001725 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001726}
1727
Bram Moolenaarec583842019-05-26 14:11:23 +02001728/*
1729 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001730 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001731 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001732 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001733popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001734{
1735 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001736 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001737 win_T *prev = NULL;
1738
Bram Moolenaarec583842019-05-26 14:11:23 +02001739 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001740 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001741 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001742 {
1743 if (prev == NULL)
1744 first_popupwin = wp->w_next;
1745 else
1746 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001747 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001748 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001749 }
1750
Bram Moolenaarec583842019-05-26 14:11:23 +02001751 // go through tab-local popups
1752 FOR_ALL_TABPAGES(tp)
1753 popup_close_tabpage(tp, id);
1754}
1755
1756/*
1757 * Close a popup window with Window-id "id" in tabpage "tp".
1758 */
1759 void
1760popup_close_tabpage(tabpage_T *tp, int id)
1761{
1762 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001763 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001764 win_T *prev = NULL;
1765
Bram Moolenaarec583842019-05-26 14:11:23 +02001766 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1767 if (wp->w_id == id)
1768 {
1769 if (prev == NULL)
1770 *root = wp->w_next;
1771 else
1772 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001773 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001774 return;
1775 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001776}
1777
1778 void
1779close_all_popups(void)
1780{
1781 while (first_popupwin != NULL)
1782 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001783 while (curtab->tp_first_popupwin != NULL)
1784 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001785}
1786
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001787/*
1788 * popup_move({id}, {options})
1789 */
1790 void
1791f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1792{
Bram Moolenaarae943152019-06-16 22:54:14 +02001793 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001794 int id = (int)tv_get_number(argvars);
1795 win_T *wp = find_popup_win(id);
1796
1797 if (wp == NULL)
1798 return; // invalid {id}
1799
1800 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1801 {
1802 emsg(_(e_dictreq));
1803 return;
1804 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001805 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001806
Bram Moolenaarae943152019-06-16 22:54:14 +02001807 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001808
1809 if (wp->w_winrow + wp->w_height >= cmdline_row)
1810 clear_cmdline = TRUE;
1811 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001812}
1813
Bram Moolenaarbc133542019-05-29 20:26:46 +02001814/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001815 * popup_setoptions({id}, {options})
1816 */
1817 void
1818f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1819{
1820 dict_T *dict;
1821 int id = (int)tv_get_number(argvars);
1822 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001823 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001824
1825 if (wp == NULL)
1826 return; // invalid {id}
1827
1828 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1829 {
1830 emsg(_(e_dictreq));
1831 return;
1832 }
1833 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001834 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001835
1836 apply_move_options(wp, dict);
1837 apply_general_options(wp, dict);
1838
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001839 if (old_firstline != wp->w_firstline)
1840 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001841 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001842 popup_adjust_position(wp);
1843}
1844
1845/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001846 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001847 */
1848 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001849f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001850{
1851 dict_T *dict;
1852 int id = (int)tv_get_number(argvars);
1853 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001854 int top_extra;
1855 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001856
1857 if (rettv_dict_alloc(rettv) == OK)
1858 {
1859 if (wp == NULL)
1860 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001861 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001862 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1863
Bram Moolenaarbc133542019-05-29 20:26:46 +02001864 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001865
Bram Moolenaarbc133542019-05-29 20:26:46 +02001866 dict_add_number(dict, "line", wp->w_winrow + 1);
1867 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001868 dict_add_number(dict, "width", wp->w_width + left_extra
1869 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1870 dict_add_number(dict, "height", wp->w_height + top_extra
1871 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001872
1873 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1874 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1875 dict_add_number(dict, "core_width", wp->w_width);
1876 dict_add_number(dict, "core_height", wp->w_height);
1877
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001878 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001879 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001880 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001881 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001882 }
1883}
1884
1885/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001886 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1887 */
1888 static void
1889get_padding_border(dict_T *dict, int *array, char *name)
1890{
1891 list_T *list;
1892 int i;
1893
1894 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1895 return;
1896
1897 list = list_alloc();
1898 if (list != NULL)
1899 {
1900 dict_add_list(dict, name, list);
1901 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1902 for (i = 0; i < 4; ++i)
1903 list_append_number(list, array[i]);
1904 }
1905}
1906
1907/*
1908 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1909 */
1910 static void
1911get_borderhighlight(dict_T *dict, win_T *wp)
1912{
1913 list_T *list;
1914 int i;
1915
1916 for (i = 0; i < 4; ++i)
1917 if (wp->w_border_highlight[i] != NULL)
1918 break;
1919 if (i == 4)
1920 return;
1921
1922 list = list_alloc();
1923 if (list != NULL)
1924 {
1925 dict_add_list(dict, "borderhighlight", list);
1926 for (i = 0; i < 4; ++i)
1927 list_append_string(list, wp->w_border_highlight[i], -1);
1928 }
1929}
1930
1931/*
1932 * For popup_getoptions(): add a "borderchars" entry to "dict".
1933 */
1934 static void
1935get_borderchars(dict_T *dict, win_T *wp)
1936{
1937 list_T *list;
1938 int i;
1939 char_u buf[NUMBUFLEN];
1940 int len;
1941
1942 for (i = 0; i < 8; ++i)
1943 if (wp->w_border_char[i] != 0)
1944 break;
1945 if (i == 8)
1946 return;
1947
1948 list = list_alloc();
1949 if (list != NULL)
1950 {
1951 dict_add_list(dict, "borderchars", list);
1952 for (i = 0; i < 8; ++i)
1953 {
1954 len = mb_char2bytes(wp->w_border_char[i], buf);
1955 list_append_string(list, buf, len);
1956 }
1957 }
1958}
1959
1960/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001961 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001962 */
1963 static void
1964get_moved_list(dict_T *dict, win_T *wp)
1965{
1966 list_T *list;
1967
1968 list = list_alloc();
1969 if (list != NULL)
1970 {
1971 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001972 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02001973 list_append_number(list, wp->w_popup_mincol);
1974 list_append_number(list, wp->w_popup_maxcol);
1975 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001976 list = list_alloc();
1977 if (list != NULL)
1978 {
1979 dict_add_list(dict, "mousemoved", list);
1980 list_append_number(list, wp->w_popup_mouse_row);
1981 list_append_number(list, wp->w_popup_mouse_mincol);
1982 list_append_number(list, wp->w_popup_mouse_maxcol);
1983 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001984}
1985
1986/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001987 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001988 */
1989 void
1990f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1991{
1992 dict_T *dict;
1993 int id = (int)tv_get_number(argvars);
1994 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001995 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001996 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001997
1998 if (rettv_dict_alloc(rettv) == OK)
1999 {
2000 if (wp == NULL)
2001 return;
2002
2003 dict = rettv->vval.v_dict;
2004 dict_add_number(dict, "line", wp->w_wantline);
2005 dict_add_number(dict, "col", wp->w_wantcol);
2006 dict_add_number(dict, "minwidth", wp->w_minwidth);
2007 dict_add_number(dict, "minheight", wp->w_minheight);
2008 dict_add_number(dict, "maxheight", wp->w_maxheight);
2009 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002010 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002011 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002012 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002013 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002014 dict_add_string(dict, "title", wp->w_popup_title);
2015 dict_add_number(dict, "wrap", wp->w_p_wrap);
2016 dict_add_number(dict, "drag", wp->w_popup_drag);
2017 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002018 if (wp->w_scrollbar_highlight != NULL)
2019 dict_add_string(dict, "scrollbarhighlight",
2020 wp->w_scrollbar_highlight);
2021 if (wp->w_thumb_highlight != NULL)
2022 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002023
Bram Moolenaara3fce622019-06-20 02:31:49 +02002024 // find the tabpage that holds this popup
2025 i = 1;
2026 FOR_ALL_TABPAGES(tp)
2027 {
2028 win_T *p;
2029
2030 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2031 if (p->w_id == id)
2032 break;
2033 if (p != NULL)
2034 break;
2035 ++i;
2036 }
2037 if (tp == NULL)
2038 i = -1; // must be global
2039 else if (tp == curtab)
2040 i = 0;
2041 dict_add_number(dict, "tabpage", i);
2042
Bram Moolenaarae943152019-06-16 22:54:14 +02002043 get_padding_border(dict, wp->w_popup_padding, "padding");
2044 get_padding_border(dict, wp->w_popup_border, "border");
2045 get_borderhighlight(dict, wp);
2046 get_borderchars(dict, wp);
2047 get_moved_list(dict, wp);
2048
2049 if (wp->w_filter_cb.cb_name != NULL)
2050 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2051 if (wp->w_close_cb.cb_name != NULL)
2052 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002053
2054 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2055 ++i)
2056 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2057 {
2058 dict_add_string(dict, "pos",
2059 (char_u *)poppos_entries[i].pp_name);
2060 break;
2061 }
2062
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002063 dict_add_string(dict, "close", (char_u *)(
2064 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2065 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2066
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002067# if defined(FEAT_TIMERS)
2068 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2069 ? (long)wp->w_popup_timer->tr_interval : 0L);
2070# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002071 }
2072}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002073
2074 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002075error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002076{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002077 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002078 {
2079 emsg(_("E994: Not allowed in a popup window"));
2080 return TRUE;
2081 }
2082 return FALSE;
2083}
2084
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002085/*
2086 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002087 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002088 */
2089 void
2090popup_reset_handled()
2091{
2092 win_T *wp;
2093
2094 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2095 wp->w_popup_flags &= ~POPF_HANDLED;
2096 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2097 wp->w_popup_flags &= ~POPF_HANDLED;
2098}
2099
2100/*
2101 * Find the next visible popup where POPF_HANDLED is not set.
2102 * Must have called popup_reset_handled() first.
2103 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2104 * popup with the highest zindex.
2105 */
2106 win_T *
2107find_next_popup(int lowest)
2108{
2109 win_T *wp;
2110 win_T *found_wp;
2111 int found_zindex;
2112
2113 found_zindex = lowest ? INT_MAX : 0;
2114 found_wp = NULL;
2115 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2116 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2117 && (lowest ? wp->w_zindex < found_zindex
2118 : wp->w_zindex > found_zindex))
2119 {
2120 found_zindex = wp->w_zindex;
2121 found_wp = wp;
2122 }
2123 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2124 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2125 && (lowest ? wp->w_zindex < found_zindex
2126 : wp->w_zindex > found_zindex))
2127 {
2128 found_zindex = wp->w_zindex;
2129 found_wp = wp;
2130 }
2131
2132 if (found_wp != NULL)
2133 found_wp->w_popup_flags |= POPF_HANDLED;
2134 return found_wp;
2135}
2136
2137/*
2138 * Invoke the filter callback for window "wp" with typed character "c".
2139 * Uses the global "mod_mask" for modifiers.
2140 * Returns the return value of the filter.
2141 * Careful: The filter may make "wp" invalid!
2142 */
2143 static int
2144invoke_popup_filter(win_T *wp, int c)
2145{
2146 int res;
2147 typval_T rettv;
2148 int dummy;
2149 typval_T argv[3];
2150 char_u buf[NUMBUFLEN];
2151
Bram Moolenaara42d9452019-06-15 21:46:30 +02002152 // Emergency exit: CTRL-C closes the popup.
2153 if (c == Ctrl_C)
2154 {
2155 rettv.v_type = VAR_NUMBER;
2156 rettv.vval.v_number = -1;
2157 popup_close_and_callback(wp, &rettv);
2158 return 1;
2159 }
2160
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002161 argv[0].v_type = VAR_NUMBER;
2162 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2163
2164 // Convert the number to a string, so that the function can use:
2165 // if a:c == "\<F2>"
2166 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2167 argv[1].v_type = VAR_STRING;
2168 argv[1].vval.v_string = vim_strsave(buf);
2169
2170 argv[2].v_type = VAR_UNKNOWN;
2171
Bram Moolenaara42d9452019-06-15 21:46:30 +02002172 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002173 call_callback(&wp->w_filter_cb, -1,
2174 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2175 res = tv_get_number(&rettv);
2176 vim_free(argv[1].vval.v_string);
2177 clear_tv(&rettv);
2178 return res;
2179}
2180
2181/*
2182 * Called when "c" was typed: invoke popup filter callbacks.
2183 * Returns TRUE when the character was consumed,
2184 */
2185 int
2186popup_do_filter(int c)
2187{
2188 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002189 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002190
2191 popup_reset_handled();
2192
2193 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2194 if (wp->w_filter_cb.cb_name != NULL)
2195 res = invoke_popup_filter(wp, c);
2196
2197 return res;
2198}
2199
Bram Moolenaar3397f742019-06-02 18:40:06 +02002200/*
2201 * Called when the cursor moved: check if any popup needs to be closed if the
2202 * cursor moved far enough.
2203 */
2204 void
2205popup_check_cursor_pos()
2206{
2207 win_T *wp;
2208 typval_T tv;
2209
2210 popup_reset_handled();
2211 while ((wp = find_next_popup(TRUE)) != NULL)
2212 if (wp->w_popup_curwin != NULL
2213 && (curwin != wp->w_popup_curwin
2214 || curwin->w_cursor.lnum != wp->w_popup_lnum
2215 || curwin->w_cursor.col < wp->w_popup_mincol
2216 || curwin->w_cursor.col > wp->w_popup_maxcol))
2217 {
2218 tv.v_type = VAR_NUMBER;
2219 tv.vval.v_number = -1;
2220 popup_close_and_callback(wp, &tv);
2221 }
2222}
2223
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002224/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002225 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2226 * "col" and "line" are screen coordinates.
2227 */
2228 static int
2229popup_masked(win_T *wp, int screencol, int screenline)
2230{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002231 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002232 int line = screenline - wp->w_winrow + 1;
2233 listitem_T *lio, *li;
2234 int width, height;
2235
2236 if (wp->w_popup_mask == NULL)
2237 return FALSE;
2238 width = popup_width(wp);
2239 height = popup_height(wp);
2240
2241 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2242 {
2243 int cols, cole;
2244 int lines, linee;
2245
2246 li = lio->li_tv.vval.v_list->lv_first;
2247 cols = tv_get_number(&li->li_tv);
2248 if (cols < 0)
2249 cols = width + cols + 1;
2250 if (col < cols)
2251 continue;
2252 li = li->li_next;
2253 cole = tv_get_number(&li->li_tv);
2254 if (cole < 0)
2255 cole = width + cole + 1;
2256 if (col > cole)
2257 continue;
2258 li = li->li_next;
2259 lines = tv_get_number(&li->li_tv);
2260 if (lines < 0)
2261 lines = height + lines + 1;
2262 if (line < lines)
2263 continue;
2264 li = li->li_next;
2265 linee = tv_get_number(&li->li_tv);
2266 if (linee < 0)
2267 linee = height + linee + 1;
2268 if (line > linee)
2269 continue;
2270 return TRUE;
2271 }
2272 return FALSE;
2273}
2274
2275/*
2276 * Set flags in popup_transparent[] for window "wp" to "val".
2277 */
2278 static void
2279update_popup_transparent(win_T *wp, int val)
2280{
2281 if (wp->w_popup_mask != NULL)
2282 {
2283 int width = popup_width(wp);
2284 int height = popup_height(wp);
2285 listitem_T *lio, *li;
2286 int cols, cole;
2287 int lines, linee;
2288 int col, line;
2289
2290 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2291 {
2292 li = lio->li_tv.vval.v_list->lv_first;
2293 cols = tv_get_number(&li->li_tv);
2294 if (cols < 0)
2295 cols = width + cols + 1;
2296 li = li->li_next;
2297 cole = tv_get_number(&li->li_tv);
2298 if (cole < 0)
2299 cole = width + cole + 1;
2300 li = li->li_next;
2301 lines = tv_get_number(&li->li_tv);
2302 if (lines < 0)
2303 lines = height + lines + 1;
2304 li = li->li_next;
2305 linee = tv_get_number(&li->li_tv);
2306 if (linee < 0)
2307 linee = height + linee + 1;
2308
2309 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002310 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002311 if (cols < 0)
2312 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002313 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002314 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002315 if (lines < 0)
2316 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002317 for (line = lines; line < linee && line < screen_Rows; ++line)
2318 for (col = cols; col < cole && col < screen_Columns; ++col)
2319 popup_transparent[(line + wp->w_winrow) * screen_Columns
2320 + col + wp->w_wincol] = val;
2321 }
2322 }
2323}
2324
2325/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002326 * Update "popup_mask" if needed.
2327 * Also recomputes the popup size and positions.
2328 * Also updates "popup_visible".
2329 * Also marks window lines for redrawing.
2330 */
2331 void
2332may_update_popup_mask(int type)
2333{
2334 win_T *wp;
2335 short *mask;
2336 int line, col;
2337 int redraw_all = FALSE;
2338
2339 // Need to recompute when switching tabs.
2340 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2341 // (such as the screen size) must have changed.
2342 if (popup_mask_tab != curtab || type >= NOT_VALID)
2343 {
2344 popup_mask_refresh = TRUE;
2345 redraw_all = TRUE;
2346 }
2347 if (!popup_mask_refresh)
2348 {
2349 // Check if any buffer has changed.
2350 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2351 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2352 popup_mask_refresh = TRUE;
2353 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2354 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2355 popup_mask_refresh = TRUE;
2356 if (!popup_mask_refresh)
2357 return;
2358 }
2359
2360 // Need to update the mask, something has changed.
2361 popup_mask_refresh = FALSE;
2362 popup_mask_tab = curtab;
2363 popup_visible = FALSE;
2364
2365 // If redrawing everything, just update "popup_mask".
2366 // If redrawing only what is needed, update "popup_mask_next" and then
2367 // compare with "popup_mask" to see what changed.
2368 if (type >= SOME_VALID)
2369 mask = popup_mask;
2370 else
2371 mask = popup_mask_next;
2372 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2373
2374 // Find the window with the lowest zindex that hasn't been handled yet,
2375 // so that the window with a higher zindex overwrites the value in
2376 // popup_mask.
2377 popup_reset_handled();
2378 while ((wp = find_next_popup(TRUE)) != NULL)
2379 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002380 int height;
2381 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002382
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002383 popup_visible = TRUE;
2384
2385 // Recompute the position if the text changed.
2386 if (redraw_all
2387 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2388 popup_adjust_position(wp);
2389
Bram Moolenaard529ba52019-07-02 23:13:53 +02002390 height = popup_height(wp);
2391 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002392 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002393 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002394 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002395 col < wp->w_wincol + width && col < screen_Columns; ++col)
2396 if (!popup_masked(wp, col, line))
2397 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002398 }
2399
2400 // Only check which lines are to be updated if not already
2401 // updating all lines.
2402 if (mask == popup_mask_next)
2403 for (line = 0; line < screen_Rows; ++line)
2404 {
2405 int col_done = 0;
2406
2407 for (col = 0; col < screen_Columns; ++col)
2408 {
2409 int off = line * screen_Columns + col;
2410
2411 if (popup_mask[off] != popup_mask_next[off])
2412 {
2413 popup_mask[off] = popup_mask_next[off];
2414
2415 if (line >= cmdline_row)
2416 {
2417 // the command line needs to be cleared if text below
2418 // the popup is now visible.
2419 if (!msg_scrolled && popup_mask_next[off] == 0)
2420 clear_cmdline = TRUE;
2421 }
2422 else if (col >= col_done)
2423 {
2424 linenr_T lnum;
2425 int line_cp = line;
2426 int col_cp = col;
2427
2428 // The screen position "line" / "col" needs to be
2429 // redrawn. Figure out what window that is and update
2430 // w_redraw_top and w_redr_bot. Only needs to be done
2431 // once for each window line.
2432 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2433 if (wp != NULL)
2434 {
2435 if (line_cp >= wp->w_height)
2436 // In (or below) status line
2437 wp->w_redr_status = TRUE;
2438 // compute the position in the buffer line from the
2439 // position on the screen
2440 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2441 &lnum))
2442 // past bottom
2443 wp->w_redr_status = TRUE;
2444 else
2445 redrawWinline(wp, lnum);
2446
2447 // This line is going to be redrawn, no need to
2448 // check until the right side of the window.
2449 col_done = wp->w_wincol + wp->w_width - 1;
2450 }
2451 }
2452 }
2453 }
2454 }
2455}
2456
2457/*
2458 * Return a string of "len" spaces in IObuff.
2459 */
2460 static char_u *
2461get_spaces(int len)
2462{
2463 vim_memset(IObuff, ' ', (size_t)len);
2464 IObuff[len] = NUL;
2465 return IObuff;
2466}
2467
2468/*
2469 * Update popup windows. They are drawn on top of normal windows.
2470 * "win_update" is called for each popup window, lowest zindex first.
2471 */
2472 void
2473update_popups(void (*win_update)(win_T *wp))
2474{
2475 win_T *wp;
2476 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002477 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002478 int total_width;
2479 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002480 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002481 int popup_attr;
2482 int border_attr[4];
2483 int border_char[8];
2484 char_u buf[MB_MAXBYTES];
2485 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002486 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002487 int padcol = 0;
2488 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002489 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002490 int sb_thumb_top = 0;
2491 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002492 int attr_scroll = 0;
2493 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002494
2495 // Find the window with the lowest zindex that hasn't been updated yet,
2496 // so that the window with a higher zindex is drawn later, thus goes on
2497 // top.
2498 popup_reset_handled();
2499 while ((wp = find_next_popup(TRUE)) != NULL)
2500 {
2501 // This drawing uses the zindex of the popup window, so that it's on
2502 // top of the text but doesn't draw when another popup with higher
2503 // zindex is on top of the character.
2504 screen_zindex = wp->w_zindex;
2505
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002506 // Set flags in popup_transparent[] for masked cells.
2507 update_popup_transparent(wp, 1);
2508
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002509 // adjust w_winrow and w_wincol for border and padding, since
2510 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002511 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002512 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2513 - wp->w_popup_leftoff;
2514 if (wp->w_wincol + left_extra < 0)
2515 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002516 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002517 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002518
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002519 // Draw the popup text, unless it's off screen.
2520 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2521 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002522
2523 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002524 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002525
Bram Moolenaard529ba52019-07-02 23:13:53 +02002526 total_width = popup_width(wp);
2527 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002528 popup_attr = get_wcr_attr(wp);
2529
2530 // We can only use these line drawing characters when 'encoding' is
2531 // "utf-8" and 'ambiwidth' is "single".
2532 if (enc_utf8 && *p_ambw == 's')
2533 {
2534 border_char[0] = border_char[2] = 0x2550;
2535 border_char[1] = border_char[3] = 0x2551;
2536 border_char[4] = 0x2554;
2537 border_char[5] = 0x2557;
2538 border_char[6] = 0x255d;
2539 border_char[7] = 0x255a;
2540 }
2541 else
2542 {
2543 border_char[0] = border_char[2] = '-';
2544 border_char[1] = border_char[3] = '|';
2545 for (i = 4; i < 8; ++i)
2546 border_char[i] = '+';
2547 }
2548 for (i = 0; i < 8; ++i)
2549 if (wp->w_border_char[i] != 0)
2550 border_char[i] = wp->w_border_char[i];
2551
2552 for (i = 0; i < 4; ++i)
2553 {
2554 border_attr[i] = popup_attr;
2555 if (wp->w_border_highlight[i] != NULL)
2556 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2557 }
2558
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002559 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002560 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002561 if (wp->w_popup_border[0] > 0)
2562 {
2563 // top border
2564 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002565 wincol < 0 ? 0 : wincol, wincol + total_width,
2566 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002567 ? border_char[4] : border_char[0],
2568 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002569 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002570 {
2571 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2572 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002573 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002574 }
2575 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002576 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2577 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002578
Bram Moolenaard529ba52019-07-02 23:13:53 +02002579 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2580 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002581 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002582 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2583 - wp->w_has_scrollbar;
2584 if (padcol < 0)
2585 {
2586 padwidth += padcol;
2587 padcol = 0;
2588 }
2589 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002590 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002591 {
2592 // top padding
2593 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002594 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002595 ' ', ' ', popup_attr);
2596 }
2597
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002598 // Title goes on top of border or padding.
2599 if (wp->w_popup_title != NULL)
2600 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2601 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2602
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002603 // Compute scrollbar thumb position and size.
2604 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002605 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002606 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2607
Bram Moolenaar68acb412019-06-26 03:40:36 +02002608 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2609 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002610 if (sb_thumb_height == 0)
2611 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002612 if (linecount <= wp->w_height)
2613 // it just fits, avoid divide by zero
2614 sb_thumb_top = 0;
2615 else
2616 sb_thumb_top = (wp->w_topline - 1
2617 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002618 * (wp->w_height - sb_thumb_height)
2619 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002620 if (wp->w_scrollbar_highlight != NULL)
2621 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2622 else
2623 attr_scroll = highlight_attr[HLF_PSB];
2624 if (wp->w_thumb_highlight != NULL)
2625 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2626 else
2627 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002628 }
2629
2630 for (i = wp->w_popup_border[0];
2631 i < total_height - wp->w_popup_border[2]; ++i)
2632 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002633 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002634 // left and right padding only needed next to the body
2635 int do_padding =
2636 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2637 && i < total_height - wp->w_popup_border[2]
2638 - wp->w_popup_padding[2];
2639
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002640 row = wp->w_winrow + i;
2641
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002642 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002643 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002644 {
2645 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002646 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002647 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002648 if (do_padding && wp->w_popup_padding[3] > 0)
2649 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002650 int col = wincol + wp->w_popup_border[3];
2651
Bram Moolenaard529ba52019-07-02 23:13:53 +02002652 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002653 pad_left = wp->w_popup_padding[3];
2654 if (col < 0)
2655 {
2656 pad_left += col;
2657 col = 0;
2658 }
2659 if (pad_left > 0)
2660 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2661 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002662 // scrollbar
2663 if (wp->w_has_scrollbar)
2664 {
2665 int line = i - top_off;
2666 int scroll_col = wp->w_wincol + total_width - 1
2667 - wp->w_popup_border[1];
2668
2669 if (line >= 0 && line < wp->w_height)
2670 screen_putchar(' ', row, scroll_col,
2671 line >= sb_thumb_top
2672 && line < sb_thumb_top + sb_thumb_height
2673 ? attr_thumb : attr_scroll);
2674 else
2675 screen_putchar(' ', row, scroll_col, popup_attr);
2676 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002677 // right border
2678 if (wp->w_popup_border[1] > 0)
2679 {
2680 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002681 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002682 }
2683 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002684 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002685 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002686 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002687 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2688 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002689 }
2690
2691 if (wp->w_popup_padding[2] > 0)
2692 {
2693 // bottom padding
2694 row = wp->w_winrow + wp->w_popup_border[0]
2695 + wp->w_popup_padding[0] + wp->w_height;
2696 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002697 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002698 }
2699
2700 if (wp->w_popup_border[2] > 0)
2701 {
2702 // bottom border
2703 row = wp->w_winrow + total_height - 1;
2704 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002705 wincol < 0 ? 0 : wincol,
2706 wincol + total_width,
2707 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002708 ? border_char[7] : border_char[2],
2709 border_char[2], border_attr[2]);
2710 if (wp->w_popup_border[1] > 0)
2711 {
2712 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002713 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002714 }
2715 }
2716
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002717 if (wp->w_popup_close == POPCLOSE_BUTTON)
2718 {
2719 // close button goes on top of anything at the top-right corner
2720 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002721 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002722 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2723 }
2724
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002725 update_popup_transparent(wp, 0);
2726
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002727 // Back to the normal zindex.
2728 screen_zindex = 0;
2729 }
2730}
2731
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002732/*
2733 * Mark references in callbacks of one popup window.
2734 */
2735 static int
2736set_ref_in_one_popup(win_T *wp, int copyID)
2737{
2738 int abort = FALSE;
2739 typval_T tv;
2740
2741 if (wp->w_close_cb.cb_partial != NULL)
2742 {
2743 tv.v_type = VAR_PARTIAL;
2744 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2745 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2746 }
2747 if (wp->w_filter_cb.cb_partial != NULL)
2748 {
2749 tv.v_type = VAR_PARTIAL;
2750 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2751 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2752 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002753 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002754 return abort;
2755}
2756
2757/*
2758 * Set reference in callbacks of popup windows.
2759 */
2760 int
2761set_ref_in_popups(int copyID)
2762{
2763 int abort = FALSE;
2764 win_T *wp;
2765 tabpage_T *tp;
2766
2767 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2768 abort = abort || set_ref_in_one_popup(wp, copyID);
2769
2770 FOR_ALL_TABPAGES(tp)
2771 {
2772 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2773 abort = abort || set_ref_in_one_popup(wp, copyID);
2774 if (abort)
2775 break;
2776 }
2777 return abort;
2778}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002779#endif // FEAT_TEXT_PROP