blob: c6c5910a3043dec4f01d805bcc6a489c59804073 [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
Bram Moolenaara901a372019-07-13 16:38:50 +02001515 // Scroll to show the line with the cursor. This assumes lines don't wrap.
1516 while (wp->w_topline + wp->w_height - 1 < wp->w_cursor.lnum)
1517 wp->w_topline++;
1518 while (wp->w_cursor.lnum < wp->w_topline)
1519 wp->w_topline--;
1520
Bram Moolenaara730e552019-06-16 19:05:31 +02001521 id = syn_name2id((char_u *)"PopupSelected");
1522 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1523 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1524 (char_u *)buf, 10, 1, NULL, NULL);
1525}
1526
1527/*
1528 * popup_filter_menu({text}, {options})
1529 */
1530 void
1531f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1532{
1533 int id = tv_get_number(&argvars[0]);
1534 win_T *wp = win_id2wp(id);
1535 char_u *key = tv_get_string(&argvars[1]);
1536 typval_T res;
1537 int c;
1538 linenr_T old_lnum;
1539
1540 // If the popup has been closed do not consume the key.
1541 if (wp == NULL)
1542 return;
1543
1544 c = *key;
1545 if (c == K_SPECIAL && key[1] != NUL)
1546 c = TO_SPECIAL(key[1], key[2]);
1547
1548 // consume all keys until done
1549 rettv->vval.v_number = 1;
1550 res.v_type = VAR_NUMBER;
1551
1552 old_lnum = wp->w_cursor.lnum;
1553 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1554 --wp->w_cursor.lnum;
1555 if ((c == 'j' || c == 'J' || c == K_DOWN)
1556 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1557 ++wp->w_cursor.lnum;
1558 if (old_lnum != wp->w_cursor.lnum)
1559 {
1560 popup_highlight_curline(wp);
1561 return;
1562 }
1563
1564 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1565 {
1566 // Cancelled, invoke callback with -1
1567 res.vval.v_number = -1;
1568 popup_close_and_callback(wp, &res);
1569 return;
1570 }
1571 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1572 {
1573 // Invoke callback with current index.
1574 res.vval.v_number = wp->w_cursor.lnum;
1575 popup_close_and_callback(wp, &res);
1576 return;
1577 }
1578
1579 filter_handle_drag(wp, c, rettv);
1580}
1581
1582/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001583 * popup_filter_yesno({text}, {options})
1584 */
1585 void
1586f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1587{
1588 int id = tv_get_number(&argvars[0]);
1589 win_T *wp = win_id2wp(id);
1590 char_u *key = tv_get_string(&argvars[1]);
1591 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001592 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001593
1594 // If the popup has been closed don't consume the key.
1595 if (wp == NULL)
1596 return;
1597
Bram Moolenaara730e552019-06-16 19:05:31 +02001598 c = *key;
1599 if (c == K_SPECIAL && key[1] != NUL)
1600 c = TO_SPECIAL(key[1], key[2]);
1601
Bram Moolenaara42d9452019-06-15 21:46:30 +02001602 // consume all keys until done
1603 rettv->vval.v_number = 1;
1604
Bram Moolenaara730e552019-06-16 19:05:31 +02001605 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001606 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001607 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001608 res.vval.v_number = 0;
1609 else
1610 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001611 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001612 return;
1613 }
1614
1615 // Invoke callback
1616 res.v_type = VAR_NUMBER;
1617 popup_close_and_callback(wp, &res);
1618}
1619
1620/*
1621 * popup_dialog({text}, {options})
1622 */
1623 void
1624f_popup_dialog(typval_T *argvars, typval_T *rettv)
1625{
1626 popup_create(argvars, rettv, TYPE_DIALOG);
1627}
1628
1629/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001630 * popup_menu({text}, {options})
1631 */
1632 void
1633f_popup_menu(typval_T *argvars, typval_T *rettv)
1634{
1635 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1636
1637 if (wp != NULL)
1638 popup_highlight_curline(wp);
1639}
1640
1641/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001642 * popup_notification({text}, {options})
1643 */
1644 void
1645f_popup_notification(typval_T *argvars, typval_T *rettv)
1646{
1647 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1648}
1649
1650/*
1651 * Find the popup window with window-ID "id".
1652 * If the popup window does not exist NULL is returned.
1653 * If the window is not a popup window, and error message is given.
1654 */
1655 static win_T *
1656find_popup_win(int id)
1657{
1658 win_T *wp = win_id2wp(id);
1659
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001660 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001661 {
1662 semsg(_("E993: window %d is not a popup window"), id);
1663 return NULL;
1664 }
1665 return wp;
1666}
1667
1668/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001669 * popup_close({id})
1670 */
1671 void
1672f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1673{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001674 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001675 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001676
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001677 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001678 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001679}
1680
1681/*
1682 * popup_hide({id})
1683 */
1684 void
1685f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1686{
1687 int id = (int)tv_get_number(argvars);
1688 win_T *wp = find_popup_win(id);
1689
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001690 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001691 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001692 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001693 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001694 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001695 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001696 }
1697}
1698
1699/*
1700 * popup_show({id})
1701 */
1702 void
1703f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1704{
1705 int id = (int)tv_get_number(argvars);
1706 win_T *wp = find_popup_win(id);
1707
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001708 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001709 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001710 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001711 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001712 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001713 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001714 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001715}
1716
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001717/*
1718 * popup_settext({id}, {text})
1719 */
1720 void
1721f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1722{
1723 int id = (int)tv_get_number(&argvars[0]);
1724 win_T *wp = find_popup_win(id);
1725
1726 if (wp != NULL)
1727 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001728 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1729 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1730 else
1731 {
1732 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1733 popup_adjust_position(wp);
1734 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001735 }
1736}
1737
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001738 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001739popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001740{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001741 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001742 if (wp->w_winrow + wp->w_height >= cmdline_row)
1743 clear_cmdline = TRUE;
1744 win_free_popup(wp);
1745 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001746 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001747}
1748
Bram Moolenaarec583842019-05-26 14:11:23 +02001749/*
1750 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001751 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001752 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001753 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001754popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001755{
1756 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001757 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001758 win_T *prev = NULL;
1759
Bram Moolenaarec583842019-05-26 14:11:23 +02001760 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001761 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001762 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001763 {
1764 if (prev == NULL)
1765 first_popupwin = wp->w_next;
1766 else
1767 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001768 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001769 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001770 }
1771
Bram Moolenaarec583842019-05-26 14:11:23 +02001772 // go through tab-local popups
1773 FOR_ALL_TABPAGES(tp)
1774 popup_close_tabpage(tp, id);
1775}
1776
1777/*
1778 * Close a popup window with Window-id "id" in tabpage "tp".
1779 */
1780 void
1781popup_close_tabpage(tabpage_T *tp, int id)
1782{
1783 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001784 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001785 win_T *prev = NULL;
1786
Bram Moolenaarec583842019-05-26 14:11:23 +02001787 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1788 if (wp->w_id == id)
1789 {
1790 if (prev == NULL)
1791 *root = wp->w_next;
1792 else
1793 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001794 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001795 return;
1796 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001797}
1798
1799 void
1800close_all_popups(void)
1801{
1802 while (first_popupwin != NULL)
1803 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001804 while (curtab->tp_first_popupwin != NULL)
1805 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001806}
1807
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001808/*
1809 * popup_move({id}, {options})
1810 */
1811 void
1812f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1813{
Bram Moolenaarae943152019-06-16 22:54:14 +02001814 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001815 int id = (int)tv_get_number(argvars);
1816 win_T *wp = find_popup_win(id);
1817
1818 if (wp == NULL)
1819 return; // invalid {id}
1820
1821 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1822 {
1823 emsg(_(e_dictreq));
1824 return;
1825 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001826 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001827
Bram Moolenaarae943152019-06-16 22:54:14 +02001828 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001829
1830 if (wp->w_winrow + wp->w_height >= cmdline_row)
1831 clear_cmdline = TRUE;
1832 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001833}
1834
Bram Moolenaarbc133542019-05-29 20:26:46 +02001835/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001836 * popup_setoptions({id}, {options})
1837 */
1838 void
1839f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1840{
1841 dict_T *dict;
1842 int id = (int)tv_get_number(argvars);
1843 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001844 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001845
1846 if (wp == NULL)
1847 return; // invalid {id}
1848
1849 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1850 {
1851 emsg(_(e_dictreq));
1852 return;
1853 }
1854 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001855 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001856
1857 apply_move_options(wp, dict);
1858 apply_general_options(wp, dict);
1859
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001860 if (old_firstline != wp->w_firstline)
1861 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001862 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001863 popup_adjust_position(wp);
1864}
1865
1866/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001867 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001868 */
1869 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001870f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001871{
1872 dict_T *dict;
1873 int id = (int)tv_get_number(argvars);
1874 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001875 int top_extra;
1876 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001877
1878 if (rettv_dict_alloc(rettv) == OK)
1879 {
1880 if (wp == NULL)
1881 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001882 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001883 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1884
Bram Moolenaarbc133542019-05-29 20:26:46 +02001885 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001886
Bram Moolenaarbc133542019-05-29 20:26:46 +02001887 dict_add_number(dict, "line", wp->w_winrow + 1);
1888 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001889 dict_add_number(dict, "width", wp->w_width + left_extra
1890 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1891 dict_add_number(dict, "height", wp->w_height + top_extra
1892 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001893
1894 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1895 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1896 dict_add_number(dict, "core_width", wp->w_width);
1897 dict_add_number(dict, "core_height", wp->w_height);
1898
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001899 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001900 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001901 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001902 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001903 }
1904}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001905/*
1906 * popup_locate({row}, {col})
1907 */
1908 void
1909f_popup_locate(typval_T *argvars, typval_T *rettv)
1910{
1911 int row = tv_get_number(&argvars[0]) - 1;
1912 int col = tv_get_number(&argvars[1]) - 1;
1913 win_T *wp;
1914
1915 wp = mouse_find_win(&row, &col, FIND_POPUP);
1916 if (WIN_IS_POPUP(wp))
1917 rettv->vval.v_number = wp->w_id;
1918}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001919
1920/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001921 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1922 */
1923 static void
1924get_padding_border(dict_T *dict, int *array, char *name)
1925{
1926 list_T *list;
1927 int i;
1928
1929 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1930 return;
1931
1932 list = list_alloc();
1933 if (list != NULL)
1934 {
1935 dict_add_list(dict, name, list);
1936 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1937 for (i = 0; i < 4; ++i)
1938 list_append_number(list, array[i]);
1939 }
1940}
1941
1942/*
1943 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1944 */
1945 static void
1946get_borderhighlight(dict_T *dict, win_T *wp)
1947{
1948 list_T *list;
1949 int i;
1950
1951 for (i = 0; i < 4; ++i)
1952 if (wp->w_border_highlight[i] != NULL)
1953 break;
1954 if (i == 4)
1955 return;
1956
1957 list = list_alloc();
1958 if (list != NULL)
1959 {
1960 dict_add_list(dict, "borderhighlight", list);
1961 for (i = 0; i < 4; ++i)
1962 list_append_string(list, wp->w_border_highlight[i], -1);
1963 }
1964}
1965
1966/*
1967 * For popup_getoptions(): add a "borderchars" entry to "dict".
1968 */
1969 static void
1970get_borderchars(dict_T *dict, win_T *wp)
1971{
1972 list_T *list;
1973 int i;
1974 char_u buf[NUMBUFLEN];
1975 int len;
1976
1977 for (i = 0; i < 8; ++i)
1978 if (wp->w_border_char[i] != 0)
1979 break;
1980 if (i == 8)
1981 return;
1982
1983 list = list_alloc();
1984 if (list != NULL)
1985 {
1986 dict_add_list(dict, "borderchars", list);
1987 for (i = 0; i < 8; ++i)
1988 {
1989 len = mb_char2bytes(wp->w_border_char[i], buf);
1990 list_append_string(list, buf, len);
1991 }
1992 }
1993}
1994
1995/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001996 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001997 */
1998 static void
1999get_moved_list(dict_T *dict, win_T *wp)
2000{
2001 list_T *list;
2002
2003 list = list_alloc();
2004 if (list != NULL)
2005 {
2006 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002007 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002008 list_append_number(list, wp->w_popup_mincol);
2009 list_append_number(list, wp->w_popup_maxcol);
2010 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002011 list = list_alloc();
2012 if (list != NULL)
2013 {
2014 dict_add_list(dict, "mousemoved", list);
2015 list_append_number(list, wp->w_popup_mouse_row);
2016 list_append_number(list, wp->w_popup_mouse_mincol);
2017 list_append_number(list, wp->w_popup_mouse_maxcol);
2018 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002019}
2020
2021/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002022 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002023 */
2024 void
2025f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2026{
2027 dict_T *dict;
2028 int id = (int)tv_get_number(argvars);
2029 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002030 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002031 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002032
2033 if (rettv_dict_alloc(rettv) == OK)
2034 {
2035 if (wp == NULL)
2036 return;
2037
2038 dict = rettv->vval.v_dict;
2039 dict_add_number(dict, "line", wp->w_wantline);
2040 dict_add_number(dict, "col", wp->w_wantcol);
2041 dict_add_number(dict, "minwidth", wp->w_minwidth);
2042 dict_add_number(dict, "minheight", wp->w_minheight);
2043 dict_add_number(dict, "maxheight", wp->w_maxheight);
2044 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002045 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002046 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002047 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002048 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002049 dict_add_string(dict, "title", wp->w_popup_title);
2050 dict_add_number(dict, "wrap", wp->w_p_wrap);
2051 dict_add_number(dict, "drag", wp->w_popup_drag);
2052 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002053 if (wp->w_scrollbar_highlight != NULL)
2054 dict_add_string(dict, "scrollbarhighlight",
2055 wp->w_scrollbar_highlight);
2056 if (wp->w_thumb_highlight != NULL)
2057 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002058
Bram Moolenaara3fce622019-06-20 02:31:49 +02002059 // find the tabpage that holds this popup
2060 i = 1;
2061 FOR_ALL_TABPAGES(tp)
2062 {
2063 win_T *p;
2064
2065 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2066 if (p->w_id == id)
2067 break;
2068 if (p != NULL)
2069 break;
2070 ++i;
2071 }
2072 if (tp == NULL)
2073 i = -1; // must be global
2074 else if (tp == curtab)
2075 i = 0;
2076 dict_add_number(dict, "tabpage", i);
2077
Bram Moolenaarae943152019-06-16 22:54:14 +02002078 get_padding_border(dict, wp->w_popup_padding, "padding");
2079 get_padding_border(dict, wp->w_popup_border, "border");
2080 get_borderhighlight(dict, wp);
2081 get_borderchars(dict, wp);
2082 get_moved_list(dict, wp);
2083
2084 if (wp->w_filter_cb.cb_name != NULL)
2085 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2086 if (wp->w_close_cb.cb_name != NULL)
2087 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002088
2089 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2090 ++i)
2091 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2092 {
2093 dict_add_string(dict, "pos",
2094 (char_u *)poppos_entries[i].pp_name);
2095 break;
2096 }
2097
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002098 dict_add_string(dict, "close", (char_u *)(
2099 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2100 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2101
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002102# if defined(FEAT_TIMERS)
2103 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2104 ? (long)wp->w_popup_timer->tr_interval : 0L);
2105# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002106 }
2107}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002108
2109 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002110error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002111{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002112 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002113 {
2114 emsg(_("E994: Not allowed in a popup window"));
2115 return TRUE;
2116 }
2117 return FALSE;
2118}
2119
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002120/*
2121 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002122 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002123 */
2124 void
2125popup_reset_handled()
2126{
2127 win_T *wp;
2128
2129 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2130 wp->w_popup_flags &= ~POPF_HANDLED;
2131 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2132 wp->w_popup_flags &= ~POPF_HANDLED;
2133}
2134
2135/*
2136 * Find the next visible popup where POPF_HANDLED is not set.
2137 * Must have called popup_reset_handled() first.
2138 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2139 * popup with the highest zindex.
2140 */
2141 win_T *
2142find_next_popup(int lowest)
2143{
2144 win_T *wp;
2145 win_T *found_wp;
2146 int found_zindex;
2147
2148 found_zindex = lowest ? INT_MAX : 0;
2149 found_wp = NULL;
2150 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2151 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2152 && (lowest ? wp->w_zindex < found_zindex
2153 : wp->w_zindex > found_zindex))
2154 {
2155 found_zindex = wp->w_zindex;
2156 found_wp = wp;
2157 }
2158 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2159 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2160 && (lowest ? wp->w_zindex < found_zindex
2161 : wp->w_zindex > found_zindex))
2162 {
2163 found_zindex = wp->w_zindex;
2164 found_wp = wp;
2165 }
2166
2167 if (found_wp != NULL)
2168 found_wp->w_popup_flags |= POPF_HANDLED;
2169 return found_wp;
2170}
2171
2172/*
2173 * Invoke the filter callback for window "wp" with typed character "c".
2174 * Uses the global "mod_mask" for modifiers.
2175 * Returns the return value of the filter.
2176 * Careful: The filter may make "wp" invalid!
2177 */
2178 static int
2179invoke_popup_filter(win_T *wp, int c)
2180{
2181 int res;
2182 typval_T rettv;
2183 int dummy;
2184 typval_T argv[3];
2185 char_u buf[NUMBUFLEN];
2186
Bram Moolenaara42d9452019-06-15 21:46:30 +02002187 // Emergency exit: CTRL-C closes the popup.
2188 if (c == Ctrl_C)
2189 {
2190 rettv.v_type = VAR_NUMBER;
2191 rettv.vval.v_number = -1;
2192 popup_close_and_callback(wp, &rettv);
2193 return 1;
2194 }
2195
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002196 argv[0].v_type = VAR_NUMBER;
2197 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2198
2199 // Convert the number to a string, so that the function can use:
2200 // if a:c == "\<F2>"
2201 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2202 argv[1].v_type = VAR_STRING;
2203 argv[1].vval.v_string = vim_strsave(buf);
2204
2205 argv[2].v_type = VAR_UNKNOWN;
2206
Bram Moolenaara42d9452019-06-15 21:46:30 +02002207 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002208 call_callback(&wp->w_filter_cb, -1,
2209 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2210 res = tv_get_number(&rettv);
2211 vim_free(argv[1].vval.v_string);
2212 clear_tv(&rettv);
2213 return res;
2214}
2215
2216/*
2217 * Called when "c" was typed: invoke popup filter callbacks.
2218 * Returns TRUE when the character was consumed,
2219 */
2220 int
2221popup_do_filter(int c)
2222{
2223 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002224 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002225
2226 popup_reset_handled();
2227
2228 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2229 if (wp->w_filter_cb.cb_name != NULL)
2230 res = invoke_popup_filter(wp, c);
2231
2232 return res;
2233}
2234
Bram Moolenaar3397f742019-06-02 18:40:06 +02002235/*
2236 * Called when the cursor moved: check if any popup needs to be closed if the
2237 * cursor moved far enough.
2238 */
2239 void
2240popup_check_cursor_pos()
2241{
2242 win_T *wp;
2243 typval_T tv;
2244
2245 popup_reset_handled();
2246 while ((wp = find_next_popup(TRUE)) != NULL)
2247 if (wp->w_popup_curwin != NULL
2248 && (curwin != wp->w_popup_curwin
2249 || curwin->w_cursor.lnum != wp->w_popup_lnum
2250 || curwin->w_cursor.col < wp->w_popup_mincol
2251 || curwin->w_cursor.col > wp->w_popup_maxcol))
2252 {
2253 tv.v_type = VAR_NUMBER;
2254 tv.vval.v_number = -1;
2255 popup_close_and_callback(wp, &tv);
2256 }
2257}
2258
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002259/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002260 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2261 * "col" and "line" are screen coordinates.
2262 */
2263 static int
2264popup_masked(win_T *wp, int screencol, int screenline)
2265{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002266 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002267 int line = screenline - wp->w_winrow + 1;
2268 listitem_T *lio, *li;
2269 int width, height;
2270
2271 if (wp->w_popup_mask == NULL)
2272 return FALSE;
2273 width = popup_width(wp);
2274 height = popup_height(wp);
2275
2276 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2277 {
2278 int cols, cole;
2279 int lines, linee;
2280
2281 li = lio->li_tv.vval.v_list->lv_first;
2282 cols = tv_get_number(&li->li_tv);
2283 if (cols < 0)
2284 cols = width + cols + 1;
2285 if (col < cols)
2286 continue;
2287 li = li->li_next;
2288 cole = tv_get_number(&li->li_tv);
2289 if (cole < 0)
2290 cole = width + cole + 1;
2291 if (col > cole)
2292 continue;
2293 li = li->li_next;
2294 lines = tv_get_number(&li->li_tv);
2295 if (lines < 0)
2296 lines = height + lines + 1;
2297 if (line < lines)
2298 continue;
2299 li = li->li_next;
2300 linee = tv_get_number(&li->li_tv);
2301 if (linee < 0)
2302 linee = height + linee + 1;
2303 if (line > linee)
2304 continue;
2305 return TRUE;
2306 }
2307 return FALSE;
2308}
2309
2310/*
2311 * Set flags in popup_transparent[] for window "wp" to "val".
2312 */
2313 static void
2314update_popup_transparent(win_T *wp, int val)
2315{
2316 if (wp->w_popup_mask != NULL)
2317 {
2318 int width = popup_width(wp);
2319 int height = popup_height(wp);
2320 listitem_T *lio, *li;
2321 int cols, cole;
2322 int lines, linee;
2323 int col, line;
2324
2325 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2326 {
2327 li = lio->li_tv.vval.v_list->lv_first;
2328 cols = tv_get_number(&li->li_tv);
2329 if (cols < 0)
2330 cols = width + cols + 1;
2331 li = li->li_next;
2332 cole = tv_get_number(&li->li_tv);
2333 if (cole < 0)
2334 cole = width + cole + 1;
2335 li = li->li_next;
2336 lines = tv_get_number(&li->li_tv);
2337 if (lines < 0)
2338 lines = height + lines + 1;
2339 li = li->li_next;
2340 linee = tv_get_number(&li->li_tv);
2341 if (linee < 0)
2342 linee = height + linee + 1;
2343
2344 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002345 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002346 if (cols < 0)
2347 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002348 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002349 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002350 if (lines < 0)
2351 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002352 for (line = lines; line < linee
2353 && line + wp->w_winrow < screen_Rows; ++line)
2354 for (col = cols; col < cole
2355 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002356 popup_transparent[(line + wp->w_winrow) * screen_Columns
2357 + col + wp->w_wincol] = val;
2358 }
2359 }
2360}
2361
2362/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002363 * Update "popup_mask" if needed.
2364 * Also recomputes the popup size and positions.
2365 * Also updates "popup_visible".
2366 * Also marks window lines for redrawing.
2367 */
2368 void
2369may_update_popup_mask(int type)
2370{
2371 win_T *wp;
2372 short *mask;
2373 int line, col;
2374 int redraw_all = FALSE;
2375
2376 // Need to recompute when switching tabs.
2377 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2378 // (such as the screen size) must have changed.
2379 if (popup_mask_tab != curtab || type >= NOT_VALID)
2380 {
2381 popup_mask_refresh = TRUE;
2382 redraw_all = TRUE;
2383 }
2384 if (!popup_mask_refresh)
2385 {
2386 // Check if any buffer has changed.
2387 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2388 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2389 popup_mask_refresh = TRUE;
2390 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2391 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2392 popup_mask_refresh = TRUE;
2393 if (!popup_mask_refresh)
2394 return;
2395 }
2396
2397 // Need to update the mask, something has changed.
2398 popup_mask_refresh = FALSE;
2399 popup_mask_tab = curtab;
2400 popup_visible = FALSE;
2401
2402 // If redrawing everything, just update "popup_mask".
2403 // If redrawing only what is needed, update "popup_mask_next" and then
2404 // compare with "popup_mask" to see what changed.
2405 if (type >= SOME_VALID)
2406 mask = popup_mask;
2407 else
2408 mask = popup_mask_next;
2409 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2410
2411 // Find the window with the lowest zindex that hasn't been handled yet,
2412 // so that the window with a higher zindex overwrites the value in
2413 // popup_mask.
2414 popup_reset_handled();
2415 while ((wp = find_next_popup(TRUE)) != NULL)
2416 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002417 int height;
2418 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002419
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002420 popup_visible = TRUE;
2421
2422 // Recompute the position if the text changed.
2423 if (redraw_all
2424 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2425 popup_adjust_position(wp);
2426
Bram Moolenaard529ba52019-07-02 23:13:53 +02002427 height = popup_height(wp);
2428 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002429 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002430 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002431 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002432 col < wp->w_wincol + width && col < screen_Columns; ++col)
2433 if (!popup_masked(wp, col, line))
2434 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002435 }
2436
2437 // Only check which lines are to be updated if not already
2438 // updating all lines.
2439 if (mask == popup_mask_next)
2440 for (line = 0; line < screen_Rows; ++line)
2441 {
2442 int col_done = 0;
2443
2444 for (col = 0; col < screen_Columns; ++col)
2445 {
2446 int off = line * screen_Columns + col;
2447
2448 if (popup_mask[off] != popup_mask_next[off])
2449 {
2450 popup_mask[off] = popup_mask_next[off];
2451
2452 if (line >= cmdline_row)
2453 {
2454 // the command line needs to be cleared if text below
2455 // the popup is now visible.
2456 if (!msg_scrolled && popup_mask_next[off] == 0)
2457 clear_cmdline = TRUE;
2458 }
2459 else if (col >= col_done)
2460 {
2461 linenr_T lnum;
2462 int line_cp = line;
2463 int col_cp = col;
2464
2465 // The screen position "line" / "col" needs to be
2466 // redrawn. Figure out what window that is and update
2467 // w_redraw_top and w_redr_bot. Only needs to be done
2468 // once for each window line.
2469 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2470 if (wp != NULL)
2471 {
2472 if (line_cp >= wp->w_height)
2473 // In (or below) status line
2474 wp->w_redr_status = TRUE;
2475 // compute the position in the buffer line from the
2476 // position on the screen
2477 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2478 &lnum))
2479 // past bottom
2480 wp->w_redr_status = TRUE;
2481 else
2482 redrawWinline(wp, lnum);
2483
2484 // This line is going to be redrawn, no need to
2485 // check until the right side of the window.
2486 col_done = wp->w_wincol + wp->w_width - 1;
2487 }
2488 }
2489 }
2490 }
2491 }
2492}
2493
2494/*
2495 * Return a string of "len" spaces in IObuff.
2496 */
2497 static char_u *
2498get_spaces(int len)
2499{
2500 vim_memset(IObuff, ' ', (size_t)len);
2501 IObuff[len] = NUL;
2502 return IObuff;
2503}
2504
2505/*
2506 * Update popup windows. They are drawn on top of normal windows.
2507 * "win_update" is called for each popup window, lowest zindex first.
2508 */
2509 void
2510update_popups(void (*win_update)(win_T *wp))
2511{
2512 win_T *wp;
2513 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002514 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002515 int total_width;
2516 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002517 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002518 int popup_attr;
2519 int border_attr[4];
2520 int border_char[8];
2521 char_u buf[MB_MAXBYTES];
2522 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002523 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002524 int padcol = 0;
2525 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002526 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002527 int sb_thumb_top = 0;
2528 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002529 int attr_scroll = 0;
2530 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002531
2532 // Find the window with the lowest zindex that hasn't been updated yet,
2533 // so that the window with a higher zindex is drawn later, thus goes on
2534 // top.
2535 popup_reset_handled();
2536 while ((wp = find_next_popup(TRUE)) != NULL)
2537 {
2538 // This drawing uses the zindex of the popup window, so that it's on
2539 // top of the text but doesn't draw when another popup with higher
2540 // zindex is on top of the character.
2541 screen_zindex = wp->w_zindex;
2542
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002543 // Set flags in popup_transparent[] for masked cells.
2544 update_popup_transparent(wp, 1);
2545
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002546 // adjust w_winrow and w_wincol for border and padding, since
2547 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002548 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002549 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2550 - wp->w_popup_leftoff;
2551 if (wp->w_wincol + left_extra < 0)
2552 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002553 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002554 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002555
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002556 // Draw the popup text, unless it's off screen.
2557 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2558 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002559
2560 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002561 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002562
Bram Moolenaard529ba52019-07-02 23:13:53 +02002563 total_width = popup_width(wp);
2564 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002565 popup_attr = get_wcr_attr(wp);
2566
2567 // We can only use these line drawing characters when 'encoding' is
2568 // "utf-8" and 'ambiwidth' is "single".
2569 if (enc_utf8 && *p_ambw == 's')
2570 {
2571 border_char[0] = border_char[2] = 0x2550;
2572 border_char[1] = border_char[3] = 0x2551;
2573 border_char[4] = 0x2554;
2574 border_char[5] = 0x2557;
2575 border_char[6] = 0x255d;
2576 border_char[7] = 0x255a;
2577 }
2578 else
2579 {
2580 border_char[0] = border_char[2] = '-';
2581 border_char[1] = border_char[3] = '|';
2582 for (i = 4; i < 8; ++i)
2583 border_char[i] = '+';
2584 }
2585 for (i = 0; i < 8; ++i)
2586 if (wp->w_border_char[i] != 0)
2587 border_char[i] = wp->w_border_char[i];
2588
2589 for (i = 0; i < 4; ++i)
2590 {
2591 border_attr[i] = popup_attr;
2592 if (wp->w_border_highlight[i] != NULL)
2593 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2594 }
2595
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002596 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002597 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002598 if (wp->w_popup_border[0] > 0)
2599 {
2600 // top border
2601 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002602 wincol < 0 ? 0 : wincol, wincol + total_width,
2603 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002604 ? border_char[4] : border_char[0],
2605 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002606 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002607 {
2608 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2609 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002610 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002611 }
2612 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002613 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2614 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002615
Bram Moolenaard529ba52019-07-02 23:13:53 +02002616 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2617 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002618 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002619 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2620 - wp->w_has_scrollbar;
2621 if (padcol < 0)
2622 {
2623 padwidth += padcol;
2624 padcol = 0;
2625 }
2626 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002627 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002628 {
2629 // top padding
2630 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002631 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002632 ' ', ' ', popup_attr);
2633 }
2634
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002635 // Title goes on top of border or padding.
2636 if (wp->w_popup_title != NULL)
2637 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2638 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2639
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002640 // Compute scrollbar thumb position and size.
2641 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002642 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002643 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2644
Bram Moolenaar68acb412019-06-26 03:40:36 +02002645 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2646 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002647 if (sb_thumb_height == 0)
2648 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002649 if (linecount <= wp->w_height)
2650 // it just fits, avoid divide by zero
2651 sb_thumb_top = 0;
2652 else
2653 sb_thumb_top = (wp->w_topline - 1
2654 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002655 * (wp->w_height - sb_thumb_height)
2656 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002657 if (wp->w_scrollbar_highlight != NULL)
2658 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2659 else
2660 attr_scroll = highlight_attr[HLF_PSB];
2661 if (wp->w_thumb_highlight != NULL)
2662 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2663 else
2664 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002665 }
2666
2667 for (i = wp->w_popup_border[0];
2668 i < total_height - wp->w_popup_border[2]; ++i)
2669 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002670 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002671 // left and right padding only needed next to the body
2672 int do_padding =
2673 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2674 && i < total_height - wp->w_popup_border[2]
2675 - wp->w_popup_padding[2];
2676
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002677 row = wp->w_winrow + i;
2678
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002679 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002680 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002681 {
2682 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002683 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002684 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002685 if (do_padding && wp->w_popup_padding[3] > 0)
2686 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002687 int col = wincol + wp->w_popup_border[3];
2688
Bram Moolenaard529ba52019-07-02 23:13:53 +02002689 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002690 pad_left = wp->w_popup_padding[3];
2691 if (col < 0)
2692 {
2693 pad_left += col;
2694 col = 0;
2695 }
2696 if (pad_left > 0)
2697 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2698 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002699 // scrollbar
2700 if (wp->w_has_scrollbar)
2701 {
2702 int line = i - top_off;
2703 int scroll_col = wp->w_wincol + total_width - 1
2704 - wp->w_popup_border[1];
2705
2706 if (line >= 0 && line < wp->w_height)
2707 screen_putchar(' ', row, scroll_col,
2708 line >= sb_thumb_top
2709 && line < sb_thumb_top + sb_thumb_height
2710 ? attr_thumb : attr_scroll);
2711 else
2712 screen_putchar(' ', row, scroll_col, popup_attr);
2713 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002714 // right border
2715 if (wp->w_popup_border[1] > 0)
2716 {
2717 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002718 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002719 }
2720 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002721 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002722 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002723 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002724 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2725 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002726 }
2727
2728 if (wp->w_popup_padding[2] > 0)
2729 {
2730 // bottom padding
2731 row = wp->w_winrow + wp->w_popup_border[0]
2732 + wp->w_popup_padding[0] + wp->w_height;
2733 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002734 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002735 }
2736
2737 if (wp->w_popup_border[2] > 0)
2738 {
2739 // bottom border
2740 row = wp->w_winrow + total_height - 1;
2741 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002742 wincol < 0 ? 0 : wincol,
2743 wincol + total_width,
2744 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002745 ? border_char[7] : border_char[2],
2746 border_char[2], border_attr[2]);
2747 if (wp->w_popup_border[1] > 0)
2748 {
2749 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002750 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002751 }
2752 }
2753
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002754 if (wp->w_popup_close == POPCLOSE_BUTTON)
2755 {
2756 // close button goes on top of anything at the top-right corner
2757 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002758 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002759 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2760 }
2761
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002762 update_popup_transparent(wp, 0);
2763
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002764 // Back to the normal zindex.
2765 screen_zindex = 0;
2766 }
2767}
2768
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002769/*
2770 * Mark references in callbacks of one popup window.
2771 */
2772 static int
2773set_ref_in_one_popup(win_T *wp, int copyID)
2774{
2775 int abort = FALSE;
2776 typval_T tv;
2777
2778 if (wp->w_close_cb.cb_partial != NULL)
2779 {
2780 tv.v_type = VAR_PARTIAL;
2781 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2782 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2783 }
2784 if (wp->w_filter_cb.cb_partial != NULL)
2785 {
2786 tv.v_type = VAR_PARTIAL;
2787 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2788 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2789 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002790 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002791 return abort;
2792}
2793
2794/*
2795 * Set reference in callbacks of popup windows.
2796 */
2797 int
2798set_ref_in_popups(int copyID)
2799{
2800 int abort = FALSE;
2801 win_T *wp;
2802 tabpage_T *tp;
2803
2804 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2805 abort = abort || set_ref_in_one_popup(wp, copyID);
2806
2807 FOR_ALL_TABPAGES(tp)
2808 {
2809 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2810 abort = abort || set_ref_in_one_popup(wp, copyID);
2811 if (abort)
2812 break;
2813 }
2814 return abort;
2815}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002816#endif // FEAT_TEXT_PROP