blob: 3185686dee5d7b148eab33e07467ceb62a512845 [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{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
223 * Return TRUE if "row"/"col" is on the "X" button of the popup.
224 * The values are relative to the top-left corner.
225 * Caller should check w_popup_close is POPCLOSE_BUTTON.
226 */
227 int
228popup_on_X_button(win_T *wp, int row, int col)
229{
230 return row == 0 && col == popup_width(wp) - 1;
231}
232
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200233// Values set when dragging a popup window starts.
234static int drag_start_row;
235static int drag_start_col;
236static int drag_start_wantline;
237static int drag_start_wantcol;
238
239/*
240 * Mouse down on border of popup window: start dragging it.
241 * Uses mouse_col and mouse_row.
242 */
243 void
244popup_start_drag(win_T *wp)
245{
246 drag_start_row = mouse_row;
247 drag_start_col = mouse_col;
248 // TODO: handle using different corner
249 if (wp->w_wantline == 0)
250 drag_start_wantline = wp->w_winrow + 1;
251 else
252 drag_start_wantline = wp->w_wantline;
253 if (wp->w_wantcol == 0)
254 drag_start_wantcol = wp->w_wincol + 1;
255 else
256 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200257
258 // Stop centering the popup
259 if (wp->w_popup_pos == POPPOS_CENTER)
260 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261}
262
263/*
264 * Mouse moved while dragging a popup window: adjust the window popup position.
265 */
266 void
267popup_drag(win_T *wp)
268{
269 // The popup may be closed before dragging stops.
270 if (!win_valid_popup(wp))
271 return;
272
273 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
274 if (wp->w_wantline < 1)
275 wp->w_wantline = 1;
276 if (wp->w_wantline > Rows)
277 wp->w_wantline = Rows;
278 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
279 if (wp->w_wantcol < 1)
280 wp->w_wantcol = 1;
281 if (wp->w_wantcol > Columns)
282 wp->w_wantcol = Columns;
283
284 popup_adjust_position(wp);
285}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200286
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200287/*
288 * Set w_firstline to match the current "wp->w_topline".
289 */
290 void
291popup_set_firstline(win_T *wp)
292{
293 int height = wp->w_height;
294
295 wp->w_firstline = wp->w_topline;
296 popup_adjust_position(wp);
297
298 // we don't want the popup to get smaller, decrement the first line
299 // until it doesn't
300 while (wp->w_firstline > 1 && wp->w_height < height)
301 {
302 --wp->w_firstline;
303 popup_adjust_position(wp);
304 }
305}
306
307/*
308 * Handle a click in a popup window, if it is in the scrollbar.
309 */
310 void
311popup_handle_scrollbar_click(win_T *wp, int row, int col)
312{
313 int height = popup_height(wp);
314 int old_topline = wp->w_topline;
315
316 if (wp->w_has_scrollbar == 0)
317 return;
318 if (row >= wp->w_popup_border[0]
319 && row < height - wp->w_popup_border[2]
Bram Moolenaarbd42b312019-07-12 16:35:34 +0200320 && col == popup_width(wp) - wp->w_popup_border[1] - 1)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200321 {
322 if (row >= height / 2)
323 {
324 // Click in lower half, scroll down.
325 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
326 ++wp->w_topline;
327 }
328 else if (wp->w_topline > 1)
329 // click on upper half, scroll up.
330 --wp->w_topline;
331 if (wp->w_topline != old_topline)
332 {
333 popup_set_firstline(wp);
334 redraw_win_later(wp, NOT_VALID);
335 }
336 }
337}
338
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200339#if defined(FEAT_TIMERS)
340 static void
341popup_add_timeout(win_T *wp, int time)
342{
343 char_u cbbuf[50];
344 char_u *ptr = cbbuf;
345 typval_T tv;
346
347 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
348 "{_ -> popup_close(%d)}", wp->w_id);
349 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
350 {
351 wp->w_popup_timer = create_timer(time, 0);
352 wp->w_popup_timer->tr_callback = get_callback(&tv);
353 clear_tv(&tv);
354 }
355}
356#endif
357
Bram Moolenaar17627312019-06-02 19:53:44 +0200358/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200359 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200360 */
361 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200362apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200363{
Bram Moolenaarae943152019-06-16 22:54:14 +0200364 int nr;
365
366 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
367 wp->w_minwidth = nr;
368 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
369 wp->w_minheight = nr;
370 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
371 wp->w_maxwidth = nr;
372 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
373 wp->w_maxheight = nr;
374 get_pos_options(wp, d);
375}
376
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200377 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200378handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
379{
380 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
381 {
382 char_u *s = di->di_tv.vval.v_string;
383 int flags = 0;
384
385 if (STRCMP(s, "word") == 0)
386 flags = FIND_IDENT | FIND_STRING;
387 else if (STRCMP(s, "WORD") == 0)
388 flags = FIND_STRING;
389 else if (STRCMP(s, "expr") == 0)
390 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
391 else if (STRCMP(s, "any") != 0)
392 semsg(_(e_invarg2), s);
393 if (flags != 0)
394 {
395 if (mousemoved)
396 set_mousemoved_columns(wp, flags);
397 else
398 set_moved_columns(wp, flags);
399 }
400 }
401 else if (di->di_tv.v_type == VAR_LIST
402 && di->di_tv.vval.v_list != NULL
403 && di->di_tv.vval.v_list->lv_len == 2)
404 {
405 list_T *l = di->di_tv.vval.v_list;
406 int mincol = tv_get_number(&l->lv_first->li_tv);
407 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
408
409 if (mousemoved)
410 {
411 wp->w_popup_mouse_mincol = mincol;
412 wp->w_popup_mouse_maxcol = maxcol;
413 }
414 else
415 {
416 wp->w_popup_mincol = mincol;
417 wp->w_popup_maxcol = maxcol;
418 }
419 }
420 else
421 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
422}
423
424 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200425check_highlight(dict_T *dict, char *name, char_u **pval)
426{
427 dictitem_T *di;
428 char_u *str;
429
430 di = dict_find(dict, (char_u *)name, -1);
431 if (di != NULL)
432 {
433 if (di->di_tv.v_type != VAR_STRING)
434 semsg(_(e_invargval), name);
435 else
436 {
437 str = tv_get_string(&di->di_tv);
438 if (*str != NUL)
439 *pval = vim_strsave(str);
440 }
441 }
442}
443
Bram Moolenaarae943152019-06-16 22:54:14 +0200444/*
445 * Shared between popup_create() and f_popup_setoptions().
446 */
447 static void
448apply_general_options(win_T *wp, dict_T *dict)
449{
450 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200451 int nr;
452 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200453
Bram Moolenaarae943152019-06-16 22:54:14 +0200454 // TODO: flip
455
456 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200457 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
459 if (wp->w_firstline < 1)
460 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200461
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200462 di = dict_find(dict, (char_u *)"scrollbar", -1);
463 if (di != NULL)
464 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
465
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200466 str = dict_get_string(dict, (char_u *)"title", FALSE);
467 if (str != NULL)
468 {
469 vim_free(wp->w_popup_title);
470 wp->w_popup_title = vim_strsave(str);
471 }
472
Bram Moolenaar402502d2019-05-30 22:07:36 +0200473 di = dict_find(dict, (char_u *)"wrap", -1);
474 if (di != NULL)
475 {
476 nr = dict_get_number(dict, (char_u *)"wrap");
477 wp->w_p_wrap = nr != 0;
478 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200479
Bram Moolenaara42d9452019-06-15 21:46:30 +0200480 di = dict_find(dict, (char_u *)"drag", -1);
481 if (di != NULL)
482 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200483
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200484 di = dict_find(dict, (char_u *)"close", -1);
485 if (di != NULL)
486 {
487 int ok = TRUE;
488
489 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
490 {
491 char_u *s = di->di_tv.vval.v_string;
492
493 if (STRCMP(s, "none") == 0)
494 wp->w_popup_close = POPCLOSE_NONE;
495 else if (STRCMP(s, "button") == 0)
496 wp->w_popup_close = POPCLOSE_BUTTON;
497 else if (STRCMP(s, "click") == 0)
498 wp->w_popup_close = POPCLOSE_CLICK;
499 else
500 ok = FALSE;
501 }
502 else
503 ok = FALSE;
504 if (!ok)
505 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
506 }
507
Bram Moolenaarae943152019-06-16 22:54:14 +0200508 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
509 if (str != NULL)
510 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
511 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200512
Bram Moolenaarae943152019-06-16 22:54:14 +0200513 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
514 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200515
Bram Moolenaar790498b2019-06-01 22:15:29 +0200516 di = dict_find(dict, (char_u *)"borderhighlight", -1);
517 if (di != NULL)
518 {
519 if (di->di_tv.v_type != VAR_LIST)
520 emsg(_(e_listreq));
521 else
522 {
523 list_T *list = di->di_tv.vval.v_list;
524 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200525 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200526
527 if (list != NULL)
528 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
529 ++i, li = li->li_next)
530 {
531 str = tv_get_string(&li->li_tv);
532 if (*str != NUL)
533 wp->w_border_highlight[i] = vim_strsave(str);
534 }
535 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
536 for (i = 1; i < 4; ++i)
537 wp->w_border_highlight[i] =
538 vim_strsave(wp->w_border_highlight[0]);
539 }
540 }
541
Bram Moolenaar790498b2019-06-01 22:15:29 +0200542 di = dict_find(dict, (char_u *)"borderchars", -1);
543 if (di != NULL)
544 {
545 if (di->di_tv.v_type != VAR_LIST)
546 emsg(_(e_listreq));
547 else
548 {
549 list_T *list = di->di_tv.vval.v_list;
550 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200551 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200552
553 if (list != NULL)
554 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
555 ++i, li = li->li_next)
556 {
557 str = tv_get_string(&li->li_tv);
558 if (*str != NUL)
559 wp->w_border_char[i] = mb_ptr2char(str);
560 }
561 if (list->lv_len == 1)
562 for (i = 1; i < 8; ++i)
563 wp->w_border_char[i] = wp->w_border_char[0];
564 if (list->lv_len == 2)
565 {
566 for (i = 4; i < 8; ++i)
567 wp->w_border_char[i] = wp->w_border_char[1];
568 for (i = 1; i < 4; ++i)
569 wp->w_border_char[i] = wp->w_border_char[0];
570 }
571 }
572 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200573
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200574 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
575 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
576
Bram Moolenaarae943152019-06-16 22:54:14 +0200577 di = dict_find(dict, (char_u *)"zindex", -1);
578 if (di != NULL)
579 {
580 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
581 if (wp->w_zindex < 1)
582 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
583 if (wp->w_zindex > 32000)
584 wp->w_zindex = 32000;
585 }
586
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200587 di = dict_find(dict, (char_u *)"mask", -1);
588 if (di != NULL)
589 {
590 int ok = TRUE;
591
592 if (di->di_tv.v_type != VAR_LIST)
593 ok = FALSE;
594 else if (di->di_tv.vval.v_list != NULL)
595 {
596 listitem_T *li;
597
598 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
599 li = li->li_next)
600 {
601 if (li->li_tv.v_type != VAR_LIST
602 || li->li_tv.vval.v_list == NULL
603 || li->li_tv.vval.v_list->lv_len != 4)
604 {
605 ok = FALSE;
606 break;
607 }
608 }
609 }
610 if (ok)
611 {
612 wp->w_popup_mask = di->di_tv.vval.v_list;
613 ++wp->w_popup_mask->lv_refcount;
614 }
615 else
616 semsg(_(e_invargval), "mask");
617 }
618
Bram Moolenaarae943152019-06-16 22:54:14 +0200619#if defined(FEAT_TIMERS)
620 // Add timer to close the popup after some time.
621 nr = dict_get_number(dict, (char_u *)"time");
622 if (nr > 0)
623 popup_add_timeout(wp, nr);
624#endif
625
Bram Moolenaar3397f742019-06-02 18:40:06 +0200626 di = dict_find(dict, (char_u *)"moved", -1);
627 if (di != NULL)
628 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200629 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200630 handle_moved_argument(wp, di, FALSE);
631 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200632
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200633 di = dict_find(dict, (char_u *)"mousemoved", -1);
634 if (di != NULL)
635 {
636 set_mousemoved_values(wp);
637 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200638 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200639
Bram Moolenaarae943152019-06-16 22:54:14 +0200640 di = dict_find(dict, (char_u *)"filter", -1);
641 if (di != NULL)
642 {
643 callback_T callback = get_callback(&di->di_tv);
644
645 if (callback.cb_name != NULL)
646 {
647 free_callback(&wp->w_filter_cb);
648 set_callback(&wp->w_filter_cb, &callback);
649 }
650 }
651
652 di = dict_find(dict, (char_u *)"callback", -1);
653 if (di != NULL)
654 {
655 callback_T callback = get_callback(&di->di_tv);
656
657 if (callback.cb_name != NULL)
658 {
659 free_callback(&wp->w_close_cb);
660 set_callback(&wp->w_close_cb, &callback);
661 }
662 }
663}
664
665/*
666 * Go through the options in "dict" and apply them to popup window "wp".
667 */
668 static void
669apply_options(win_T *wp, dict_T *dict)
670{
671 int nr;
672
673 apply_move_options(wp, dict);
674 apply_general_options(wp, dict);
675
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200676 nr = dict_get_number(dict, (char_u *)"hidden");
677 if (nr > 0)
678 {
679 wp->w_popup_flags |= POPF_HIDDEN;
680 --wp->w_buffer->b_nwindows;
681 }
682
Bram Moolenaar33796b32019-06-08 16:01:13 +0200683 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200684}
685
686/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200687 * Add lines to the popup from a list of strings.
688 */
689 static void
690add_popup_strings(buf_T *buf, list_T *l)
691{
692 listitem_T *li;
693 linenr_T lnum = 0;
694 char_u *p;
695
696 for (li = l->lv_first; li != NULL; li = li->li_next)
697 if (li->li_tv.v_type == VAR_STRING)
698 {
699 p = li->li_tv.vval.v_string;
700 ml_append_buf(buf, lnum++,
701 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
702 }
703}
704
705/*
706 * Add lines to the popup from a list of dictionaries.
707 */
708 static void
709add_popup_dicts(buf_T *buf, list_T *l)
710{
711 listitem_T *li;
712 listitem_T *pli;
713 linenr_T lnum = 0;
714 char_u *p;
715 dict_T *dict;
716
717 // first add the text lines
718 for (li = l->lv_first; li != NULL; li = li->li_next)
719 {
720 if (li->li_tv.v_type != VAR_DICT)
721 {
722 emsg(_(e_dictreq));
723 return;
724 }
725 dict = li->li_tv.vval.v_dict;
726 p = dict == NULL ? NULL
727 : dict_get_string(dict, (char_u *)"text", FALSE);
728 ml_append_buf(buf, lnum++,
729 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
730 }
731
732 // add the text properties
733 lnum = 1;
734 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
735 {
736 dictitem_T *di;
737 list_T *plist;
738
739 dict = li->li_tv.vval.v_dict;
740 di = dict_find(dict, (char_u *)"props", -1);
741 if (di != NULL)
742 {
743 if (di->di_tv.v_type != VAR_LIST)
744 {
745 emsg(_(e_listreq));
746 return;
747 }
748 plist = di->di_tv.vval.v_list;
749 if (plist != NULL)
750 {
751 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
752 {
753 if (pli->li_tv.v_type != VAR_DICT)
754 {
755 emsg(_(e_dictreq));
756 return;
757 }
758 dict = pli->li_tv.vval.v_dict;
759 if (dict != NULL)
760 {
761 int col = dict_get_number(dict, (char_u *)"col");
762
763 prop_add_common( lnum, col, dict, buf, NULL);
764 }
765 }
766 }
767 }
768 }
769}
770
771/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200772 * Get the padding plus border at the top, adjusted to 1 if there is a title.
773 */
774 static int
775popup_top_extra(win_T *wp)
776{
777 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
778
779 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
780 return 1;
781 return extra;
782}
783
784/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200785 * Return the height of popup window "wp", including border and padding.
786 */
787 int
788popup_height(win_T *wp)
789{
790 return wp->w_height
791 + popup_top_extra(wp)
792 + wp->w_popup_padding[2] + wp->w_popup_border[2];
793}
794
795/*
796 * Return the width of popup window "wp", including border, padding and
797 * scrollbar.
798 */
799 int
800popup_width(win_T *wp)
801{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200802 // w_leftcol is how many columns of the core are left of the screen
803 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200804 return wp->w_width + wp->w_leftcol
805 + wp->w_popup_padding[3] + wp->w_popup_border[3]
806 + wp->w_popup_padding[1] + wp->w_popup_border[1]
807 + wp->w_has_scrollbar
808 + wp->w_popup_rightoff;
809}
810
811/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200812 * Adjust the position and size of the popup to fit on the screen.
813 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200814 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200815popup_adjust_position(win_T *wp)
816{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200817 linenr_T lnum;
818 int wrapped = 0;
819 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200820 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200821 int center_vert = FALSE;
822 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200823 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200824 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200825 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
826 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
827 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
828 int extra_height = top_extra + bot_extra;
829 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200830 int org_winrow = wp->w_winrow;
831 int org_wincol = wp->w_wincol;
832 int org_width = wp->w_width;
833 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200834 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200835 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200836 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200837
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200838 wp->w_winrow = 0;
839 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200840 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200841 wp->w_popup_leftoff = 0;
842 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200843 if (wp->w_popup_pos == POPPOS_CENTER)
844 {
845 // center after computing the size
846 center_vert = TRUE;
847 center_hor = TRUE;
848 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200849 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200850 {
851 if (wp->w_wantline == 0)
852 center_vert = TRUE;
853 else if (wp->w_popup_pos == POPPOS_TOPLEFT
854 || wp->w_popup_pos == POPPOS_TOPRIGHT)
855 {
856 wp->w_winrow = wp->w_wantline - 1;
857 if (wp->w_winrow >= Rows)
858 wp->w_winrow = Rows - 1;
859 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200860
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200861 if (wp->w_wantcol == 0)
862 center_hor = TRUE;
863 else if (wp->w_popup_pos == POPPOS_TOPLEFT
864 || wp->w_popup_pos == POPPOS_BOTLEFT)
865 {
866 wp->w_wincol = wp->w_wantcol - 1;
867 if (wp->w_wincol >= Columns - 3)
868 wp->w_wincol = Columns - 3;
869 }
870 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200871
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200872 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200873 // When left aligned use the space available, but shift to the left when we
874 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200875 maxspace = Columns - wp->w_wincol - left_extra;
876 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200877 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200878 {
879 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200880 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200881 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200882
Bram Moolenaar8d241042019-06-12 23:40:01 +0200883 // start at the desired first line
884 wp->w_topline = wp->w_firstline;
885 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
886 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
887
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200888 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200889 // Use a minimum width of one, so that something shows when there is no
890 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200891 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200892 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200893 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200894 {
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200895 // count Tabs for what they are worth
896 int len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
897 (colnr_T)MAXCOL);
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200898
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200899 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200900 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200901 while (len > maxwidth)
902 {
903 ++wrapped;
904 len -= maxwidth;
905 wp->w_width = maxwidth;
906 }
907 }
908 else if (len > maxwidth
909 && allow_adjust_left
910 && (wp->w_popup_pos == POPPOS_TOPLEFT
911 || wp->w_popup_pos == POPPOS_BOTLEFT))
912 {
913 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200914 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200915
Bram Moolenaar51c31312019-06-15 22:27:23 +0200916 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200917 {
918 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200919
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200920 len -= truncate_shift;
921 shift_by -= truncate_shift;
922 }
923
924 wp->w_wincol -= shift_by;
925 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200926 wp->w_width = maxwidth;
927 }
928 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +0200929 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200930 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +0200931 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
932 wp->w_width = wp->w_maxwidth;
933 }
Bram Moolenaar8d241042019-06-12 23:40:01 +0200934 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200935 if (wp->w_maxheight > 0
936 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200937 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200938 }
939
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200940 wp->w_has_scrollbar = wp->w_want_scrollbar
941 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
942
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200943 minwidth = wp->w_minwidth;
944 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
945 {
946 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
947
948 if (minwidth < title_len)
949 minwidth = title_len;
950 }
951
952 if (minwidth > 0 && wp->w_width < minwidth)
953 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200954 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +0200955 {
956 if (wp->w_width > maxspace)
957 // some columns cut off on the right
958 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200959 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200960 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200961 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200962 {
963 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
964 if (wp->w_wincol < 0)
965 wp->w_wincol = 0;
966 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200967 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
968 || wp->w_popup_pos == POPPOS_TOPRIGHT)
969 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200970 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
971
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200972 // Right aligned: move to the right if needed.
973 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200974 if (leftoff >= 0)
975 wp->w_wincol = leftoff;
976 else if (wp->w_popup_fixed)
977 {
978 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +0200979 // columns when displaying the window, minus left border and
980 // padding.
981 if (-leftoff > left_extra)
982 wp->w_leftcol = -leftoff - left_extra;
983 wp->w_width -= wp->w_leftcol;
984 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200985 if (wp->w_width < 0)
986 wp->w_width = 0;
987 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200988 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200989
Bram Moolenaar8d241042019-06-12 23:40:01 +0200990 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
991 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200992 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
993 wp->w_height = wp->w_minheight;
994 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
995 wp->w_height = wp->w_maxheight;
996 if (wp->w_height > Rows - wp->w_winrow)
997 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200998
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200999 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001000 {
1001 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1002 if (wp->w_winrow < 0)
1003 wp->w_winrow = 0;
1004 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001005 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1006 || wp->w_popup_pos == POPPOS_BOTLEFT)
1007 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001008 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001009 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001010 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001011 else
1012 // not enough space, make top aligned
1013 wp->w_winrow = wp->w_wantline + 1;
1014 }
1015
Bram Moolenaar17146962019-05-30 00:12:11 +02001016 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001017
1018 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001019 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001020 if (org_winrow != wp->w_winrow
1021 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001022 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001023 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001024 || org_width != wp->w_width
1025 || org_height != wp->w_height)
1026 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001027 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001028 popup_mask_refresh = TRUE;
1029 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001030}
1031
Bram Moolenaar17627312019-06-02 19:53:44 +02001032typedef enum
1033{
1034 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001035 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001036 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001037 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001038 TYPE_DIALOG,
1039 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +02001040} create_type_T;
1041
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001042/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001043 * Make "buf" empty and set the contents to "text".
1044 * Used by popup_create() and popup_settext().
1045 */
1046 static void
1047popup_set_buffer_text(buf_T *buf, typval_T text)
1048{
1049 int lnum;
1050
1051 // Clear the buffer, then replace the lines.
1052 curbuf = buf;
1053 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1054 ml_delete(lnum, FALSE);
1055 curbuf = curwin->w_buffer;
1056
1057 // Add text to the buffer.
1058 if (text.v_type == VAR_STRING)
1059 {
1060 // just a string
1061 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1062 }
1063 else
1064 {
1065 list_T *l = text.vval.v_list;
1066
1067 if (l->lv_len > 0)
1068 {
1069 if (l->lv_first->li_tv.v_type == VAR_STRING)
1070 // list of strings
1071 add_popup_strings(buf, l);
1072 else
1073 // list of dictionaries
1074 add_popup_dicts(buf, l);
1075 }
1076 }
1077
1078 // delete the line that was in the empty buffer
1079 curbuf = buf;
1080 ml_delete(buf->b_ml.ml_line_count, FALSE);
1081 curbuf = curwin->w_buffer;
1082}
1083
1084/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001085 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001086 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001087 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001088 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001089popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001090{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001091 win_T *wp;
1092 tabpage_T *tp = NULL;
1093 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001094 int new_buffer;
1095 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001096 dict_T *d;
1097 int nr;
1098 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001099
1100 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001101 if (argvars[0].v_type == VAR_NUMBER)
1102 {
1103 buf = buflist_findnr( argvars[0].vval.v_number);
1104 if (buf == NULL)
1105 {
1106 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1107 return NULL;
1108 }
1109 }
1110 else if (!(argvars[0].v_type == VAR_STRING
1111 && argvars[0].vval.v_string != NULL)
1112 && !(argvars[0].v_type == VAR_LIST
1113 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001114 {
1115 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001116 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001117 }
1118 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1119 {
1120 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001121 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001122 }
1123 d = argvars[1].vval.v_dict;
1124
Bram Moolenaara3fce622019-06-20 02:31:49 +02001125 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1126 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1127 else if (type == TYPE_NOTIFICATION)
1128 tabnr = -1; // notifications are global by default
1129 else
1130 tabnr = 0;
1131 if (tabnr > 0)
1132 {
1133 tp = find_tabpage(tabnr);
1134 if (tp == NULL)
1135 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001136 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001137 return NULL;
1138 }
1139 }
1140
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001141 // Create the window and buffer.
1142 wp = win_alloc_popup_win();
1143 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001144 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001145 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001146 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001147 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001148
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001149 if (buf != NULL)
1150 {
1151 // use existing buffer
1152 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001153 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001154 buffer_ensure_loaded(buf);
1155 }
1156 else
1157 {
1158 // create a new buffer associated with the popup
1159 new_buffer = TRUE;
1160 buf = buflist_new(NULL, NULL, (linenr_T)0,
1161 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1162 if (buf == NULL)
1163 return NULL;
1164 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001165
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001166 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001167
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001168 set_local_options_default(wp);
1169 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001170 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001171 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1172 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1173 buf->b_p_ul = -1; // no undo
1174 buf->b_p_swf = FALSE; // no swap file
1175 buf->b_p_bl = FALSE; // unlisted buffer
1176 buf->b_locked = TRUE;
1177 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001178
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001179 // Avoid that 'buftype' is reset when this buffer is entered.
1180 buf->b_p_initialized = TRUE;
1181 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001182
Bram Moolenaara3fce622019-06-20 02:31:49 +02001183 if (tp != NULL)
1184 {
1185 // popup on specified tab page
1186 wp->w_next = tp->tp_first_popupwin;
1187 tp->tp_first_popupwin = wp;
1188 }
1189 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001190 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001191 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001192 wp->w_next = curtab->tp_first_popupwin;
1193 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001194 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001195 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001196 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001197 win_T *prev = first_popupwin;
1198
1199 // Global popup: add at the end, so that it gets displayed on top of
1200 // older ones with the same zindex. Matters for notifications.
1201 if (first_popupwin == NULL)
1202 first_popupwin = wp;
1203 else
1204 {
1205 while (prev->w_next != NULL)
1206 prev = prev->w_next;
1207 prev->w_next = wp;
1208 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001209 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001210
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001211 if (new_buffer)
1212 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001213
Bram Moolenaar17627312019-06-02 19:53:44 +02001214 if (type == TYPE_ATCURSOR)
1215 {
1216 wp->w_popup_pos = POPPOS_BOTLEFT;
1217 setcursor_mayforce(TRUE);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001218 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
Bram Moolenaar17627312019-06-02 19:53:44 +02001219 if (wp->w_wantline == 0) // cursor in first line
1220 {
1221 wp->w_wantline = 2;
1222 wp->w_popup_pos = POPPOS_TOPLEFT;
1223 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001224 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar17627312019-06-02 19:53:44 +02001225 set_moved_values(wp);
1226 set_moved_columns(wp, FIND_STRING);
1227 }
1228
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001229 if (type == TYPE_BEVAL)
1230 {
1231 wp->w_popup_pos = POPPOS_BOTLEFT;
1232
1233 // by default use the mouse position
1234 wp->w_wantline = mouse_row;
1235 if (wp->w_wantline <= 0) // mouse on first line
1236 {
1237 wp->w_wantline = 2;
1238 wp->w_popup_pos = POPPOS_TOPLEFT;
1239 }
1240 wp->w_wantcol = mouse_col + 1;
1241 set_mousemoved_values(wp);
1242 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1243 }
1244
Bram Moolenaar33796b32019-06-08 16:01:13 +02001245 // set default values
1246 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001247 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001248
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001249 if (type == TYPE_NOTIFICATION)
1250 {
1251 win_T *twp, *nextwin;
1252 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001253
1254 // Try to not overlap with another global popup. Guess we need 3
1255 // more screen lines than buffer lines.
1256 wp->w_wantline = 1;
1257 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1258 {
1259 nextwin = twp->w_next;
1260 if (twp != wp
1261 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1262 && twp->w_winrow <= wp->w_wantline - 1 + height
1263 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1264 {
1265 // move to below this popup and restart the loop to check for
1266 // overlap with other popups
1267 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1268 nextwin = first_popupwin;
1269 }
1270 }
1271 if (wp->w_wantline + height > Rows)
1272 {
1273 // can't avoid overlap, put on top in the hope that message goes
1274 // away soon.
1275 wp->w_wantline = 1;
1276 }
1277
1278 wp->w_wantcol = 10;
1279 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001280 wp->w_minwidth = 20;
1281 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001282 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001283 for (i = 0; i < 4; ++i)
1284 wp->w_popup_border[i] = 1;
1285 wp->w_popup_padding[1] = 1;
1286 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001287
1288 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001289 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001290 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1291 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001292 }
1293
Bram Moolenaara730e552019-06-16 19:05:31 +02001294 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001295 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001296 wp->w_popup_pos = POPPOS_CENTER;
1297 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1298 wp->w_popup_drag = 1;
1299 for (i = 0; i < 4; ++i)
1300 {
1301 wp->w_popup_border[i] = 1;
1302 wp->w_popup_padding[i] = 1;
1303 }
1304 }
1305
Bram Moolenaara730e552019-06-16 19:05:31 +02001306 if (type == TYPE_MENU)
1307 {
1308 typval_T tv;
1309 callback_T callback;
1310
1311 tv.v_type = VAR_STRING;
1312 tv.vval.v_string = (char_u *)"popup_filter_menu";
1313 callback = get_callback(&tv);
1314 if (callback.cb_name != NULL)
1315 set_callback(&wp->w_filter_cb, &callback);
1316
1317 wp->w_p_wrap = 0;
1318 }
1319
Bram Moolenaarae943152019-06-16 22:54:14 +02001320 for (i = 0; i < 4; ++i)
1321 VIM_CLEAR(wp->w_border_highlight[i]);
1322 for (i = 0; i < 8; ++i)
1323 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001324 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001325 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001326
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001327 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001328 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001329
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001330#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001331 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1332 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001333#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001334
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001335 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001336
1337 wp->w_vsep_width = 0;
1338
1339 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001340 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001341
1342 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001343}
1344
1345/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001346 * popup_clear()
1347 */
1348 void
1349f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1350{
1351 close_all_popups();
1352}
1353
1354/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001355 * popup_create({text}, {options})
1356 */
1357 void
1358f_popup_create(typval_T *argvars, typval_T *rettv)
1359{
Bram Moolenaar17627312019-06-02 19:53:44 +02001360 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001361}
1362
1363/*
1364 * popup_atcursor({text}, {options})
1365 */
1366 void
1367f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1368{
Bram Moolenaar17627312019-06-02 19:53:44 +02001369 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001370}
1371
1372/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001373 * popup_beval({text}, {options})
1374 */
1375 void
1376f_popup_beval(typval_T *argvars, typval_T *rettv)
1377{
1378 popup_create(argvars, rettv, TYPE_BEVAL);
1379}
1380
1381/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001382 * Invoke the close callback for window "wp" with value "result".
1383 * Careful: The callback may make "wp" invalid!
1384 */
1385 static void
1386invoke_popup_callback(win_T *wp, typval_T *result)
1387{
1388 typval_T rettv;
1389 int dummy;
1390 typval_T argv[3];
1391
1392 argv[0].v_type = VAR_NUMBER;
1393 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1394
1395 if (result != NULL && result->v_type != VAR_UNKNOWN)
1396 copy_tv(result, &argv[1]);
1397 else
1398 {
1399 argv[1].v_type = VAR_NUMBER;
1400 argv[1].vval.v_number = 0;
1401 }
1402
1403 argv[2].v_type = VAR_UNKNOWN;
1404
1405 call_callback(&wp->w_close_cb, -1,
1406 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1407 if (result != NULL)
1408 clear_tv(&argv[1]);
1409 clear_tv(&rettv);
1410}
1411
1412/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001413 * Close popup "wp" and invoke any close callback for it.
1414 */
1415 static void
1416popup_close_and_callback(win_T *wp, typval_T *arg)
1417{
1418 int id = wp->w_id;
1419
1420 if (wp->w_close_cb.cb_name != NULL)
1421 // Careful: This may make "wp" invalid.
1422 invoke_popup_callback(wp, arg);
1423
1424 popup_close(id);
1425}
1426
1427/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001428 * Close popup "wp" because of a mouse click.
1429 */
1430 void
1431popup_close_for_mouse_click(win_T *wp)
1432{
1433 typval_T res;
1434
1435 res.v_type = VAR_NUMBER;
1436 res.vval.v_number = -2;
1437 popup_close_and_callback(wp, &res);
1438}
1439
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001440 static void
1441check_mouse_moved(win_T *wp, win_T *mouse_wp)
1442{
1443 // Close the popup when all if these are true:
1444 // - the mouse is not on this popup
1445 // - "mousemoved" was used
1446 // - the mouse is no longer on the same screen row or the mouse column is
1447 // outside of the relevant text
1448 if (wp != mouse_wp
1449 && wp->w_popup_mouse_row != 0
1450 && (wp->w_popup_mouse_row != mouse_row
1451 || mouse_col < wp->w_popup_mouse_mincol
1452 || mouse_col > wp->w_popup_mouse_maxcol))
1453 {
1454 typval_T res;
1455
1456 res.v_type = VAR_NUMBER;
1457 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001458 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001459 popup_close_and_callback(wp, &res);
1460 }
1461}
1462
1463/*
1464 * Called when the mouse moved: may close a popup with "mousemoved".
1465 */
1466 void
1467popup_handle_mouse_moved(void)
1468{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001469 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001470 win_T *mouse_wp;
1471 int row = mouse_row;
1472 int col = mouse_col;
1473
1474 // find the window where the mouse is in
1475 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1476
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001477 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1478 {
1479 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001480 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001481 }
1482 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1483 {
1484 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001485 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001486 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001487}
1488
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001489/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001490 * In a filter: check if the typed key is a mouse event that is used for
1491 * dragging the popup.
1492 */
1493 static void
1494filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1495{
1496 int row = mouse_row;
1497 int col = mouse_col;
1498
1499 if (wp->w_popup_drag
1500 && is_mouse_key(c)
1501 && (wp == popup_dragwin
1502 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1503 // do not consume the key, allow for dragging the popup
1504 rettv->vval.v_number = 0;
1505}
1506
1507 static void
1508popup_highlight_curline(win_T *wp)
1509{
1510 int id;
1511 char buf[100];
1512
1513 match_delete(wp, 1, FALSE);
1514
1515 id = syn_name2id((char_u *)"PopupSelected");
1516 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1517 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1518 (char_u *)buf, 10, 1, NULL, NULL);
1519}
1520
1521/*
1522 * popup_filter_menu({text}, {options})
1523 */
1524 void
1525f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1526{
1527 int id = tv_get_number(&argvars[0]);
1528 win_T *wp = win_id2wp(id);
1529 char_u *key = tv_get_string(&argvars[1]);
1530 typval_T res;
1531 int c;
1532 linenr_T old_lnum;
1533
1534 // If the popup has been closed do not consume the key.
1535 if (wp == NULL)
1536 return;
1537
1538 c = *key;
1539 if (c == K_SPECIAL && key[1] != NUL)
1540 c = TO_SPECIAL(key[1], key[2]);
1541
1542 // consume all keys until done
1543 rettv->vval.v_number = 1;
1544 res.v_type = VAR_NUMBER;
1545
1546 old_lnum = wp->w_cursor.lnum;
1547 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1548 --wp->w_cursor.lnum;
1549 if ((c == 'j' || c == 'J' || c == K_DOWN)
1550 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1551 ++wp->w_cursor.lnum;
1552 if (old_lnum != wp->w_cursor.lnum)
1553 {
1554 popup_highlight_curline(wp);
1555 return;
1556 }
1557
1558 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1559 {
1560 // Cancelled, invoke callback with -1
1561 res.vval.v_number = -1;
1562 popup_close_and_callback(wp, &res);
1563 return;
1564 }
1565 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1566 {
1567 // Invoke callback with current index.
1568 res.vval.v_number = wp->w_cursor.lnum;
1569 popup_close_and_callback(wp, &res);
1570 return;
1571 }
1572
1573 filter_handle_drag(wp, c, rettv);
1574}
1575
1576/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001577 * popup_filter_yesno({text}, {options})
1578 */
1579 void
1580f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1581{
1582 int id = tv_get_number(&argvars[0]);
1583 win_T *wp = win_id2wp(id);
1584 char_u *key = tv_get_string(&argvars[1]);
1585 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001586 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001587
1588 // If the popup has been closed don't consume the key.
1589 if (wp == NULL)
1590 return;
1591
Bram Moolenaara730e552019-06-16 19:05:31 +02001592 c = *key;
1593 if (c == K_SPECIAL && key[1] != NUL)
1594 c = TO_SPECIAL(key[1], key[2]);
1595
Bram Moolenaara42d9452019-06-15 21:46:30 +02001596 // consume all keys until done
1597 rettv->vval.v_number = 1;
1598
Bram Moolenaara730e552019-06-16 19:05:31 +02001599 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001600 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001601 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001602 res.vval.v_number = 0;
1603 else
1604 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001605 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001606 return;
1607 }
1608
1609 // Invoke callback
1610 res.v_type = VAR_NUMBER;
1611 popup_close_and_callback(wp, &res);
1612}
1613
1614/*
1615 * popup_dialog({text}, {options})
1616 */
1617 void
1618f_popup_dialog(typval_T *argvars, typval_T *rettv)
1619{
1620 popup_create(argvars, rettv, TYPE_DIALOG);
1621}
1622
1623/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001624 * popup_menu({text}, {options})
1625 */
1626 void
1627f_popup_menu(typval_T *argvars, typval_T *rettv)
1628{
1629 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1630
1631 if (wp != NULL)
1632 popup_highlight_curline(wp);
1633}
1634
1635/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001636 * popup_notification({text}, {options})
1637 */
1638 void
1639f_popup_notification(typval_T *argvars, typval_T *rettv)
1640{
1641 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1642}
1643
1644/*
1645 * Find the popup window with window-ID "id".
1646 * If the popup window does not exist NULL is returned.
1647 * If the window is not a popup window, and error message is given.
1648 */
1649 static win_T *
1650find_popup_win(int id)
1651{
1652 win_T *wp = win_id2wp(id);
1653
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001654 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001655 {
1656 semsg(_("E993: window %d is not a popup window"), id);
1657 return NULL;
1658 }
1659 return wp;
1660}
1661
1662/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001663 * popup_close({id})
1664 */
1665 void
1666f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1667{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001668 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001669 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001670
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001671 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001672 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001673}
1674
1675/*
1676 * popup_hide({id})
1677 */
1678 void
1679f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1680{
1681 int id = (int)tv_get_number(argvars);
1682 win_T *wp = find_popup_win(id);
1683
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001684 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001685 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001686 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001687 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001688 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001689 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001690 }
1691}
1692
1693/*
1694 * popup_show({id})
1695 */
1696 void
1697f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1698{
1699 int id = (int)tv_get_number(argvars);
1700 win_T *wp = find_popup_win(id);
1701
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001702 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001703 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001704 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001705 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001706 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001707 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001708 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001709}
1710
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001711/*
1712 * popup_settext({id}, {text})
1713 */
1714 void
1715f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1716{
1717 int id = (int)tv_get_number(&argvars[0]);
1718 win_T *wp = find_popup_win(id);
1719
1720 if (wp != NULL)
1721 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001722 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1723 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1724 else
1725 {
1726 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1727 popup_adjust_position(wp);
1728 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001729 }
1730}
1731
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001732 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001733popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001734{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001735 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001736 if (wp->w_winrow + wp->w_height >= cmdline_row)
1737 clear_cmdline = TRUE;
1738 win_free_popup(wp);
1739 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001740 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001741}
1742
Bram Moolenaarec583842019-05-26 14:11:23 +02001743/*
1744 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001745 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001746 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001747 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001748popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001749{
1750 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001751 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001752 win_T *prev = NULL;
1753
Bram Moolenaarec583842019-05-26 14:11:23 +02001754 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001755 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001756 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001757 {
1758 if (prev == NULL)
1759 first_popupwin = wp->w_next;
1760 else
1761 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001762 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001763 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001764 }
1765
Bram Moolenaarec583842019-05-26 14:11:23 +02001766 // go through tab-local popups
1767 FOR_ALL_TABPAGES(tp)
1768 popup_close_tabpage(tp, id);
1769}
1770
1771/*
1772 * Close a popup window with Window-id "id" in tabpage "tp".
1773 */
1774 void
1775popup_close_tabpage(tabpage_T *tp, int id)
1776{
1777 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001778 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001779 win_T *prev = NULL;
1780
Bram Moolenaarec583842019-05-26 14:11:23 +02001781 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1782 if (wp->w_id == id)
1783 {
1784 if (prev == NULL)
1785 *root = wp->w_next;
1786 else
1787 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001788 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001789 return;
1790 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001791}
1792
1793 void
1794close_all_popups(void)
1795{
1796 while (first_popupwin != NULL)
1797 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001798 while (curtab->tp_first_popupwin != NULL)
1799 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001800}
1801
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001802/*
1803 * popup_move({id}, {options})
1804 */
1805 void
1806f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1807{
Bram Moolenaarae943152019-06-16 22:54:14 +02001808 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001809 int id = (int)tv_get_number(argvars);
1810 win_T *wp = find_popup_win(id);
1811
1812 if (wp == NULL)
1813 return; // invalid {id}
1814
1815 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1816 {
1817 emsg(_(e_dictreq));
1818 return;
1819 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001820 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001821
Bram Moolenaarae943152019-06-16 22:54:14 +02001822 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001823
1824 if (wp->w_winrow + wp->w_height >= cmdline_row)
1825 clear_cmdline = TRUE;
1826 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001827}
1828
Bram Moolenaarbc133542019-05-29 20:26:46 +02001829/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001830 * popup_setoptions({id}, {options})
1831 */
1832 void
1833f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1834{
1835 dict_T *dict;
1836 int id = (int)tv_get_number(argvars);
1837 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001838 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001839
1840 if (wp == NULL)
1841 return; // invalid {id}
1842
1843 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1844 {
1845 emsg(_(e_dictreq));
1846 return;
1847 }
1848 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001849 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001850
1851 apply_move_options(wp, dict);
1852 apply_general_options(wp, dict);
1853
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001854 if (old_firstline != wp->w_firstline)
1855 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001856 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001857 popup_adjust_position(wp);
1858}
1859
1860/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001861 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001862 */
1863 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001864f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001865{
1866 dict_T *dict;
1867 int id = (int)tv_get_number(argvars);
1868 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001869 int top_extra;
1870 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001871
1872 if (rettv_dict_alloc(rettv) == OK)
1873 {
1874 if (wp == NULL)
1875 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001876 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001877 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1878
Bram Moolenaarbc133542019-05-29 20:26:46 +02001879 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001880
Bram Moolenaarbc133542019-05-29 20:26:46 +02001881 dict_add_number(dict, "line", wp->w_winrow + 1);
1882 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001883 dict_add_number(dict, "width", wp->w_width + left_extra
1884 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1885 dict_add_number(dict, "height", wp->w_height + top_extra
1886 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001887
1888 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1889 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1890 dict_add_number(dict, "core_width", wp->w_width);
1891 dict_add_number(dict, "core_height", wp->w_height);
1892
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001893 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001894 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001895 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001896 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001897 }
1898}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001899/*
1900 * popup_locate({row}, {col})
1901 */
1902 void
1903f_popup_locate(typval_T *argvars, typval_T *rettv)
1904{
1905 int row = tv_get_number(&argvars[0]) - 1;
1906 int col = tv_get_number(&argvars[1]) - 1;
1907 win_T *wp;
1908
1909 wp = mouse_find_win(&row, &col, FIND_POPUP);
1910 if (WIN_IS_POPUP(wp))
1911 rettv->vval.v_number = wp->w_id;
1912}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001913
1914/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001915 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1916 */
1917 static void
1918get_padding_border(dict_T *dict, int *array, char *name)
1919{
1920 list_T *list;
1921 int i;
1922
1923 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1924 return;
1925
1926 list = list_alloc();
1927 if (list != NULL)
1928 {
1929 dict_add_list(dict, name, list);
1930 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1931 for (i = 0; i < 4; ++i)
1932 list_append_number(list, array[i]);
1933 }
1934}
1935
1936/*
1937 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1938 */
1939 static void
1940get_borderhighlight(dict_T *dict, win_T *wp)
1941{
1942 list_T *list;
1943 int i;
1944
1945 for (i = 0; i < 4; ++i)
1946 if (wp->w_border_highlight[i] != NULL)
1947 break;
1948 if (i == 4)
1949 return;
1950
1951 list = list_alloc();
1952 if (list != NULL)
1953 {
1954 dict_add_list(dict, "borderhighlight", list);
1955 for (i = 0; i < 4; ++i)
1956 list_append_string(list, wp->w_border_highlight[i], -1);
1957 }
1958}
1959
1960/*
1961 * For popup_getoptions(): add a "borderchars" entry to "dict".
1962 */
1963 static void
1964get_borderchars(dict_T *dict, win_T *wp)
1965{
1966 list_T *list;
1967 int i;
1968 char_u buf[NUMBUFLEN];
1969 int len;
1970
1971 for (i = 0; i < 8; ++i)
1972 if (wp->w_border_char[i] != 0)
1973 break;
1974 if (i == 8)
1975 return;
1976
1977 list = list_alloc();
1978 if (list != NULL)
1979 {
1980 dict_add_list(dict, "borderchars", list);
1981 for (i = 0; i < 8; ++i)
1982 {
1983 len = mb_char2bytes(wp->w_border_char[i], buf);
1984 list_append_string(list, buf, len);
1985 }
1986 }
1987}
1988
1989/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001990 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001991 */
1992 static void
1993get_moved_list(dict_T *dict, win_T *wp)
1994{
1995 list_T *list;
1996
1997 list = list_alloc();
1998 if (list != NULL)
1999 {
2000 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002001 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002002 list_append_number(list, wp->w_popup_mincol);
2003 list_append_number(list, wp->w_popup_maxcol);
2004 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002005 list = list_alloc();
2006 if (list != NULL)
2007 {
2008 dict_add_list(dict, "mousemoved", list);
2009 list_append_number(list, wp->w_popup_mouse_row);
2010 list_append_number(list, wp->w_popup_mouse_mincol);
2011 list_append_number(list, wp->w_popup_mouse_maxcol);
2012 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002013}
2014
2015/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002016 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002017 */
2018 void
2019f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2020{
2021 dict_T *dict;
2022 int id = (int)tv_get_number(argvars);
2023 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002024 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002025 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002026
2027 if (rettv_dict_alloc(rettv) == OK)
2028 {
2029 if (wp == NULL)
2030 return;
2031
2032 dict = rettv->vval.v_dict;
2033 dict_add_number(dict, "line", wp->w_wantline);
2034 dict_add_number(dict, "col", wp->w_wantcol);
2035 dict_add_number(dict, "minwidth", wp->w_minwidth);
2036 dict_add_number(dict, "minheight", wp->w_minheight);
2037 dict_add_number(dict, "maxheight", wp->w_maxheight);
2038 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002039 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002040 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002041 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002042 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002043 dict_add_string(dict, "title", wp->w_popup_title);
2044 dict_add_number(dict, "wrap", wp->w_p_wrap);
2045 dict_add_number(dict, "drag", wp->w_popup_drag);
2046 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002047 if (wp->w_scrollbar_highlight != NULL)
2048 dict_add_string(dict, "scrollbarhighlight",
2049 wp->w_scrollbar_highlight);
2050 if (wp->w_thumb_highlight != NULL)
2051 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002052
Bram Moolenaara3fce622019-06-20 02:31:49 +02002053 // find the tabpage that holds this popup
2054 i = 1;
2055 FOR_ALL_TABPAGES(tp)
2056 {
2057 win_T *p;
2058
2059 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2060 if (p->w_id == id)
2061 break;
2062 if (p != NULL)
2063 break;
2064 ++i;
2065 }
2066 if (tp == NULL)
2067 i = -1; // must be global
2068 else if (tp == curtab)
2069 i = 0;
2070 dict_add_number(dict, "tabpage", i);
2071
Bram Moolenaarae943152019-06-16 22:54:14 +02002072 get_padding_border(dict, wp->w_popup_padding, "padding");
2073 get_padding_border(dict, wp->w_popup_border, "border");
2074 get_borderhighlight(dict, wp);
2075 get_borderchars(dict, wp);
2076 get_moved_list(dict, wp);
2077
2078 if (wp->w_filter_cb.cb_name != NULL)
2079 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2080 if (wp->w_close_cb.cb_name != NULL)
2081 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002082
2083 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2084 ++i)
2085 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2086 {
2087 dict_add_string(dict, "pos",
2088 (char_u *)poppos_entries[i].pp_name);
2089 break;
2090 }
2091
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002092 dict_add_string(dict, "close", (char_u *)(
2093 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2094 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2095
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002096# if defined(FEAT_TIMERS)
2097 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2098 ? (long)wp->w_popup_timer->tr_interval : 0L);
2099# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002100 }
2101}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002102
2103 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002104error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002105{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002106 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002107 {
2108 emsg(_("E994: Not allowed in a popup window"));
2109 return TRUE;
2110 }
2111 return FALSE;
2112}
2113
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002114/*
2115 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002116 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002117 */
2118 void
2119popup_reset_handled()
2120{
2121 win_T *wp;
2122
2123 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2124 wp->w_popup_flags &= ~POPF_HANDLED;
2125 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2126 wp->w_popup_flags &= ~POPF_HANDLED;
2127}
2128
2129/*
2130 * Find the next visible popup where POPF_HANDLED is not set.
2131 * Must have called popup_reset_handled() first.
2132 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2133 * popup with the highest zindex.
2134 */
2135 win_T *
2136find_next_popup(int lowest)
2137{
2138 win_T *wp;
2139 win_T *found_wp;
2140 int found_zindex;
2141
2142 found_zindex = lowest ? INT_MAX : 0;
2143 found_wp = NULL;
2144 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2145 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2146 && (lowest ? wp->w_zindex < found_zindex
2147 : wp->w_zindex > found_zindex))
2148 {
2149 found_zindex = wp->w_zindex;
2150 found_wp = wp;
2151 }
2152 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2153 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2154 && (lowest ? wp->w_zindex < found_zindex
2155 : wp->w_zindex > found_zindex))
2156 {
2157 found_zindex = wp->w_zindex;
2158 found_wp = wp;
2159 }
2160
2161 if (found_wp != NULL)
2162 found_wp->w_popup_flags |= POPF_HANDLED;
2163 return found_wp;
2164}
2165
2166/*
2167 * Invoke the filter callback for window "wp" with typed character "c".
2168 * Uses the global "mod_mask" for modifiers.
2169 * Returns the return value of the filter.
2170 * Careful: The filter may make "wp" invalid!
2171 */
2172 static int
2173invoke_popup_filter(win_T *wp, int c)
2174{
2175 int res;
2176 typval_T rettv;
2177 int dummy;
2178 typval_T argv[3];
2179 char_u buf[NUMBUFLEN];
2180
Bram Moolenaara42d9452019-06-15 21:46:30 +02002181 // Emergency exit: CTRL-C closes the popup.
2182 if (c == Ctrl_C)
2183 {
2184 rettv.v_type = VAR_NUMBER;
2185 rettv.vval.v_number = -1;
2186 popup_close_and_callback(wp, &rettv);
2187 return 1;
2188 }
2189
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002190 argv[0].v_type = VAR_NUMBER;
2191 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2192
2193 // Convert the number to a string, so that the function can use:
2194 // if a:c == "\<F2>"
2195 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2196 argv[1].v_type = VAR_STRING;
2197 argv[1].vval.v_string = vim_strsave(buf);
2198
2199 argv[2].v_type = VAR_UNKNOWN;
2200
Bram Moolenaara42d9452019-06-15 21:46:30 +02002201 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002202 call_callback(&wp->w_filter_cb, -1,
2203 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2204 res = tv_get_number(&rettv);
2205 vim_free(argv[1].vval.v_string);
2206 clear_tv(&rettv);
2207 return res;
2208}
2209
2210/*
2211 * Called when "c" was typed: invoke popup filter callbacks.
2212 * Returns TRUE when the character was consumed,
2213 */
2214 int
2215popup_do_filter(int c)
2216{
2217 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002218 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002219
2220 popup_reset_handled();
2221
2222 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2223 if (wp->w_filter_cb.cb_name != NULL)
2224 res = invoke_popup_filter(wp, c);
2225
2226 return res;
2227}
2228
Bram Moolenaar3397f742019-06-02 18:40:06 +02002229/*
2230 * Called when the cursor moved: check if any popup needs to be closed if the
2231 * cursor moved far enough.
2232 */
2233 void
2234popup_check_cursor_pos()
2235{
2236 win_T *wp;
2237 typval_T tv;
2238
2239 popup_reset_handled();
2240 while ((wp = find_next_popup(TRUE)) != NULL)
2241 if (wp->w_popup_curwin != NULL
2242 && (curwin != wp->w_popup_curwin
2243 || curwin->w_cursor.lnum != wp->w_popup_lnum
2244 || curwin->w_cursor.col < wp->w_popup_mincol
2245 || curwin->w_cursor.col > wp->w_popup_maxcol))
2246 {
2247 tv.v_type = VAR_NUMBER;
2248 tv.vval.v_number = -1;
2249 popup_close_and_callback(wp, &tv);
2250 }
2251}
2252
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002253/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002254 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2255 * "col" and "line" are screen coordinates.
2256 */
2257 static int
2258popup_masked(win_T *wp, int screencol, int screenline)
2259{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002260 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002261 int line = screenline - wp->w_winrow + 1;
2262 listitem_T *lio, *li;
2263 int width, height;
2264
2265 if (wp->w_popup_mask == NULL)
2266 return FALSE;
2267 width = popup_width(wp);
2268 height = popup_height(wp);
2269
2270 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2271 {
2272 int cols, cole;
2273 int lines, linee;
2274
2275 li = lio->li_tv.vval.v_list->lv_first;
2276 cols = tv_get_number(&li->li_tv);
2277 if (cols < 0)
2278 cols = width + cols + 1;
2279 if (col < cols)
2280 continue;
2281 li = li->li_next;
2282 cole = tv_get_number(&li->li_tv);
2283 if (cole < 0)
2284 cole = width + cole + 1;
2285 if (col > cole)
2286 continue;
2287 li = li->li_next;
2288 lines = tv_get_number(&li->li_tv);
2289 if (lines < 0)
2290 lines = height + lines + 1;
2291 if (line < lines)
2292 continue;
2293 li = li->li_next;
2294 linee = tv_get_number(&li->li_tv);
2295 if (linee < 0)
2296 linee = height + linee + 1;
2297 if (line > linee)
2298 continue;
2299 return TRUE;
2300 }
2301 return FALSE;
2302}
2303
2304/*
2305 * Set flags in popup_transparent[] for window "wp" to "val".
2306 */
2307 static void
2308update_popup_transparent(win_T *wp, int val)
2309{
2310 if (wp->w_popup_mask != NULL)
2311 {
2312 int width = popup_width(wp);
2313 int height = popup_height(wp);
2314 listitem_T *lio, *li;
2315 int cols, cole;
2316 int lines, linee;
2317 int col, line;
2318
2319 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2320 {
2321 li = lio->li_tv.vval.v_list->lv_first;
2322 cols = tv_get_number(&li->li_tv);
2323 if (cols < 0)
2324 cols = width + cols + 1;
2325 li = li->li_next;
2326 cole = tv_get_number(&li->li_tv);
2327 if (cole < 0)
2328 cole = width + cole + 1;
2329 li = li->li_next;
2330 lines = tv_get_number(&li->li_tv);
2331 if (lines < 0)
2332 lines = height + lines + 1;
2333 li = li->li_next;
2334 linee = tv_get_number(&li->li_tv);
2335 if (linee < 0)
2336 linee = height + linee + 1;
2337
2338 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002339 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002340 if (cols < 0)
2341 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002342 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002343 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002344 if (lines < 0)
2345 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002346 for (line = lines; line < linee
2347 && line + wp->w_winrow < screen_Rows; ++line)
2348 for (col = cols; col < cole
2349 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002350 popup_transparent[(line + wp->w_winrow) * screen_Columns
2351 + col + wp->w_wincol] = val;
2352 }
2353 }
2354}
2355
2356/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002357 * Update "popup_mask" if needed.
2358 * Also recomputes the popup size and positions.
2359 * Also updates "popup_visible".
2360 * Also marks window lines for redrawing.
2361 */
2362 void
2363may_update_popup_mask(int type)
2364{
2365 win_T *wp;
2366 short *mask;
2367 int line, col;
2368 int redraw_all = FALSE;
2369
2370 // Need to recompute when switching tabs.
2371 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2372 // (such as the screen size) must have changed.
2373 if (popup_mask_tab != curtab || type >= NOT_VALID)
2374 {
2375 popup_mask_refresh = TRUE;
2376 redraw_all = TRUE;
2377 }
2378 if (!popup_mask_refresh)
2379 {
2380 // Check if any buffer has changed.
2381 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2382 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2383 popup_mask_refresh = TRUE;
2384 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2385 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2386 popup_mask_refresh = TRUE;
2387 if (!popup_mask_refresh)
2388 return;
2389 }
2390
2391 // Need to update the mask, something has changed.
2392 popup_mask_refresh = FALSE;
2393 popup_mask_tab = curtab;
2394 popup_visible = FALSE;
2395
2396 // If redrawing everything, just update "popup_mask".
2397 // If redrawing only what is needed, update "popup_mask_next" and then
2398 // compare with "popup_mask" to see what changed.
2399 if (type >= SOME_VALID)
2400 mask = popup_mask;
2401 else
2402 mask = popup_mask_next;
2403 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2404
2405 // Find the window with the lowest zindex that hasn't been handled yet,
2406 // so that the window with a higher zindex overwrites the value in
2407 // popup_mask.
2408 popup_reset_handled();
2409 while ((wp = find_next_popup(TRUE)) != NULL)
2410 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002411 int height;
2412 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002413
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002414 popup_visible = TRUE;
2415
2416 // Recompute the position if the text changed.
2417 if (redraw_all
2418 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2419 popup_adjust_position(wp);
2420
Bram Moolenaard529ba52019-07-02 23:13:53 +02002421 height = popup_height(wp);
2422 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002423 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002424 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002425 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002426 col < wp->w_wincol + width && col < screen_Columns; ++col)
2427 if (!popup_masked(wp, col, line))
2428 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002429 }
2430
2431 // Only check which lines are to be updated if not already
2432 // updating all lines.
2433 if (mask == popup_mask_next)
2434 for (line = 0; line < screen_Rows; ++line)
2435 {
2436 int col_done = 0;
2437
2438 for (col = 0; col < screen_Columns; ++col)
2439 {
2440 int off = line * screen_Columns + col;
2441
2442 if (popup_mask[off] != popup_mask_next[off])
2443 {
2444 popup_mask[off] = popup_mask_next[off];
2445
2446 if (line >= cmdline_row)
2447 {
2448 // the command line needs to be cleared if text below
2449 // the popup is now visible.
2450 if (!msg_scrolled && popup_mask_next[off] == 0)
2451 clear_cmdline = TRUE;
2452 }
2453 else if (col >= col_done)
2454 {
2455 linenr_T lnum;
2456 int line_cp = line;
2457 int col_cp = col;
2458
2459 // The screen position "line" / "col" needs to be
2460 // redrawn. Figure out what window that is and update
2461 // w_redraw_top and w_redr_bot. Only needs to be done
2462 // once for each window line.
2463 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2464 if (wp != NULL)
2465 {
2466 if (line_cp >= wp->w_height)
2467 // In (or below) status line
2468 wp->w_redr_status = TRUE;
2469 // compute the position in the buffer line from the
2470 // position on the screen
2471 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2472 &lnum))
2473 // past bottom
2474 wp->w_redr_status = TRUE;
2475 else
2476 redrawWinline(wp, lnum);
2477
2478 // This line is going to be redrawn, no need to
2479 // check until the right side of the window.
2480 col_done = wp->w_wincol + wp->w_width - 1;
2481 }
2482 }
2483 }
2484 }
2485 }
2486}
2487
2488/*
2489 * Return a string of "len" spaces in IObuff.
2490 */
2491 static char_u *
2492get_spaces(int len)
2493{
2494 vim_memset(IObuff, ' ', (size_t)len);
2495 IObuff[len] = NUL;
2496 return IObuff;
2497}
2498
2499/*
2500 * Update popup windows. They are drawn on top of normal windows.
2501 * "win_update" is called for each popup window, lowest zindex first.
2502 */
2503 void
2504update_popups(void (*win_update)(win_T *wp))
2505{
2506 win_T *wp;
2507 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002508 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002509 int total_width;
2510 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002511 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002512 int popup_attr;
2513 int border_attr[4];
2514 int border_char[8];
2515 char_u buf[MB_MAXBYTES];
2516 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002517 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002518 int padcol = 0;
2519 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002520 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002521 int sb_thumb_top = 0;
2522 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002523 int attr_scroll = 0;
2524 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002525
2526 // Find the window with the lowest zindex that hasn't been updated yet,
2527 // so that the window with a higher zindex is drawn later, thus goes on
2528 // top.
2529 popup_reset_handled();
2530 while ((wp = find_next_popup(TRUE)) != NULL)
2531 {
2532 // This drawing uses the zindex of the popup window, so that it's on
2533 // top of the text but doesn't draw when another popup with higher
2534 // zindex is on top of the character.
2535 screen_zindex = wp->w_zindex;
2536
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002537 // Set flags in popup_transparent[] for masked cells.
2538 update_popup_transparent(wp, 1);
2539
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002540 // adjust w_winrow and w_wincol for border and padding, since
2541 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002542 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002543 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2544 - wp->w_popup_leftoff;
2545 if (wp->w_wincol + left_extra < 0)
2546 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002547 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002548 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002549
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002550 // Draw the popup text, unless it's off screen.
2551 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2552 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002553
2554 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002555 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002556
Bram Moolenaard529ba52019-07-02 23:13:53 +02002557 total_width = popup_width(wp);
2558 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002559 popup_attr = get_wcr_attr(wp);
2560
2561 // We can only use these line drawing characters when 'encoding' is
2562 // "utf-8" and 'ambiwidth' is "single".
2563 if (enc_utf8 && *p_ambw == 's')
2564 {
2565 border_char[0] = border_char[2] = 0x2550;
2566 border_char[1] = border_char[3] = 0x2551;
2567 border_char[4] = 0x2554;
2568 border_char[5] = 0x2557;
2569 border_char[6] = 0x255d;
2570 border_char[7] = 0x255a;
2571 }
2572 else
2573 {
2574 border_char[0] = border_char[2] = '-';
2575 border_char[1] = border_char[3] = '|';
2576 for (i = 4; i < 8; ++i)
2577 border_char[i] = '+';
2578 }
2579 for (i = 0; i < 8; ++i)
2580 if (wp->w_border_char[i] != 0)
2581 border_char[i] = wp->w_border_char[i];
2582
2583 for (i = 0; i < 4; ++i)
2584 {
2585 border_attr[i] = popup_attr;
2586 if (wp->w_border_highlight[i] != NULL)
2587 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2588 }
2589
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002590 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002591 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002592 if (wp->w_popup_border[0] > 0)
2593 {
2594 // top border
2595 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002596 wincol < 0 ? 0 : wincol, wincol + total_width,
2597 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002598 ? border_char[4] : border_char[0],
2599 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002600 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002601 {
2602 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2603 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002604 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002605 }
2606 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002607 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2608 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002609
Bram Moolenaard529ba52019-07-02 23:13:53 +02002610 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2611 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002612 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002613 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2614 - wp->w_has_scrollbar;
2615 if (padcol < 0)
2616 {
2617 padwidth += padcol;
2618 padcol = 0;
2619 }
2620 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002621 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002622 {
2623 // top padding
2624 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002625 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002626 ' ', ' ', popup_attr);
2627 }
2628
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002629 // Title goes on top of border or padding.
2630 if (wp->w_popup_title != NULL)
2631 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2632 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2633
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002634 // Compute scrollbar thumb position and size.
2635 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002636 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002637 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2638
Bram Moolenaar68acb412019-06-26 03:40:36 +02002639 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2640 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002641 if (sb_thumb_height == 0)
2642 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002643 if (linecount <= wp->w_height)
2644 // it just fits, avoid divide by zero
2645 sb_thumb_top = 0;
2646 else
2647 sb_thumb_top = (wp->w_topline - 1
2648 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002649 * (wp->w_height - sb_thumb_height)
2650 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002651 if (wp->w_scrollbar_highlight != NULL)
2652 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2653 else
2654 attr_scroll = highlight_attr[HLF_PSB];
2655 if (wp->w_thumb_highlight != NULL)
2656 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2657 else
2658 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002659 }
2660
2661 for (i = wp->w_popup_border[0];
2662 i < total_height - wp->w_popup_border[2]; ++i)
2663 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002664 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002665 // left and right padding only needed next to the body
2666 int do_padding =
2667 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2668 && i < total_height - wp->w_popup_border[2]
2669 - wp->w_popup_padding[2];
2670
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002671 row = wp->w_winrow + i;
2672
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002673 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002674 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002675 {
2676 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002677 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002678 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002679 if (do_padding && wp->w_popup_padding[3] > 0)
2680 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002681 int col = wincol + wp->w_popup_border[3];
2682
Bram Moolenaard529ba52019-07-02 23:13:53 +02002683 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002684 pad_left = wp->w_popup_padding[3];
2685 if (col < 0)
2686 {
2687 pad_left += col;
2688 col = 0;
2689 }
2690 if (pad_left > 0)
2691 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2692 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002693 // scrollbar
2694 if (wp->w_has_scrollbar)
2695 {
2696 int line = i - top_off;
2697 int scroll_col = wp->w_wincol + total_width - 1
2698 - wp->w_popup_border[1];
2699
2700 if (line >= 0 && line < wp->w_height)
2701 screen_putchar(' ', row, scroll_col,
2702 line >= sb_thumb_top
2703 && line < sb_thumb_top + sb_thumb_height
2704 ? attr_thumb : attr_scroll);
2705 else
2706 screen_putchar(' ', row, scroll_col, popup_attr);
2707 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002708 // right border
2709 if (wp->w_popup_border[1] > 0)
2710 {
2711 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002712 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002713 }
2714 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002715 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002716 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002717 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002718 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2719 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002720 }
2721
2722 if (wp->w_popup_padding[2] > 0)
2723 {
2724 // bottom padding
2725 row = wp->w_winrow + wp->w_popup_border[0]
2726 + wp->w_popup_padding[0] + wp->w_height;
2727 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002728 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002729 }
2730
2731 if (wp->w_popup_border[2] > 0)
2732 {
2733 // bottom border
2734 row = wp->w_winrow + total_height - 1;
2735 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002736 wincol < 0 ? 0 : wincol,
2737 wincol + total_width,
2738 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002739 ? border_char[7] : border_char[2],
2740 border_char[2], border_attr[2]);
2741 if (wp->w_popup_border[1] > 0)
2742 {
2743 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002744 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002745 }
2746 }
2747
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002748 if (wp->w_popup_close == POPCLOSE_BUTTON)
2749 {
2750 // close button goes on top of anything at the top-right corner
2751 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002752 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002753 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2754 }
2755
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002756 update_popup_transparent(wp, 0);
2757
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002758 // Back to the normal zindex.
2759 screen_zindex = 0;
2760 }
2761}
2762
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002763/*
2764 * Mark references in callbacks of one popup window.
2765 */
2766 static int
2767set_ref_in_one_popup(win_T *wp, int copyID)
2768{
2769 int abort = FALSE;
2770 typval_T tv;
2771
2772 if (wp->w_close_cb.cb_partial != NULL)
2773 {
2774 tv.v_type = VAR_PARTIAL;
2775 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2776 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2777 }
2778 if (wp->w_filter_cb.cb_partial != NULL)
2779 {
2780 tv.v_type = VAR_PARTIAL;
2781 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2782 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2783 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002784 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002785 return abort;
2786}
2787
2788/*
2789 * Set reference in callbacks of popup windows.
2790 */
2791 int
2792set_ref_in_popups(int copyID)
2793{
2794 int abort = FALSE;
2795 win_T *wp;
2796 tabpage_T *tp;
2797
2798 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2799 abort = abort || set_ref_in_one_popup(wp, copyID);
2800
2801 FOR_ALL_TABPAGES(tp)
2802 {
2803 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2804 abort = abort || set_ref_in_one_popup(wp, copyID);
2805 if (abort)
2806 break;
2807 }
2808 return abort;
2809}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002810#endif // FEAT_TEXT_PROP