blob: b0b67b1919b613229770d641152076a048d54492 [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;
81
82 nr = popup_options_one(dict, (char_u *)"line");
83 if (nr > 0)
84 wp->w_wantline = nr;
85 nr = popup_options_one(dict, (char_u *)"col");
86 if (nr > 0)
87 wp->w_wantcol = nr;
88
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020089 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
90
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020091 str = dict_get_string(dict, (char_u *)"pos", FALSE);
92 if (str != NULL)
93 {
94 for (nr = 0;
95 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
96 ++nr)
97 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
98 {
99 wp->w_popup_pos = poppos_entries[nr].pp_val;
100 nr = -1;
101 break;
102 }
103 if (nr != -1)
104 semsg(_(e_invarg2), str);
105 }
106}
107
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200108 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200109set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110{
111 dictitem_T *di;
112
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113 di = dict_find(dict, (char_u *)name, -1);
114 if (di != NULL)
115 {
116 if (di->di_tv.v_type != VAR_LIST)
117 emsg(_(e_listreq));
118 else
119 {
120 list_T *list = di->di_tv.vval.v_list;
121 listitem_T *li;
122 int i;
123 int nr;
124
125 for (i = 0; i < 4; ++i)
126 array[i] = 1;
127 if (list != NULL)
128 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
129 ++i, li = li->li_next)
130 {
131 nr = (int)tv_get_number(&li->li_tv);
132 if (nr >= 0)
133 array[i] = nr > max_val ? max_val : nr;
134 }
135 }
136 }
137}
138
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200139/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200140 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200141 */
142 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200143set_moved_values(win_T *wp)
144{
145 wp->w_popup_curwin = curwin;
146 wp->w_popup_lnum = curwin->w_cursor.lnum;
147 wp->w_popup_mincol = curwin->w_cursor.col;
148 wp->w_popup_maxcol = curwin->w_cursor.col;
149}
150
151/*
152 * Used when popup options contain "moved" with "word" or "WORD".
153 */
154 static void
155set_moved_columns(win_T *wp, int flags)
156{
157 char_u *ptr;
158 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
159
160 if (len > 0)
161 {
162 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
163 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
164 }
165}
166
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200167/*
168 * Return TRUE if "row"/"col" is on the border of the popup.
169 * The values are relative to the top-left corner.
170 */
171 int
172popup_on_border(win_T *wp, int row, int col)
173{
174 return (row == 0 && wp->w_popup_border[0] > 0)
175 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
176 || (col == 0 && wp->w_popup_border[3] > 0)
177 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
178}
179
180// Values set when dragging a popup window starts.
181static int drag_start_row;
182static int drag_start_col;
183static int drag_start_wantline;
184static int drag_start_wantcol;
185
186/*
187 * Mouse down on border of popup window: start dragging it.
188 * Uses mouse_col and mouse_row.
189 */
190 void
191popup_start_drag(win_T *wp)
192{
193 drag_start_row = mouse_row;
194 drag_start_col = mouse_col;
195 // TODO: handle using different corner
196 if (wp->w_wantline == 0)
197 drag_start_wantline = wp->w_winrow + 1;
198 else
199 drag_start_wantline = wp->w_wantline;
200 if (wp->w_wantcol == 0)
201 drag_start_wantcol = wp->w_wincol + 1;
202 else
203 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200204
205 // Stop centering the popup
206 if (wp->w_popup_pos == POPPOS_CENTER)
207 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200208}
209
210/*
211 * Mouse moved while dragging a popup window: adjust the window popup position.
212 */
213 void
214popup_drag(win_T *wp)
215{
216 // The popup may be closed before dragging stops.
217 if (!win_valid_popup(wp))
218 return;
219
220 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
221 if (wp->w_wantline < 1)
222 wp->w_wantline = 1;
223 if (wp->w_wantline > Rows)
224 wp->w_wantline = Rows;
225 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
226 if (wp->w_wantcol < 1)
227 wp->w_wantcol = 1;
228 if (wp->w_wantcol > Columns)
229 wp->w_wantcol = Columns;
230
231 popup_adjust_position(wp);
232}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200233
234#if defined(FEAT_TIMERS)
235 static void
236popup_add_timeout(win_T *wp, int time)
237{
238 char_u cbbuf[50];
239 char_u *ptr = cbbuf;
240 typval_T tv;
241
242 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
243 "{_ -> popup_close(%d)}", wp->w_id);
244 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
245 {
246 wp->w_popup_timer = create_timer(time, 0);
247 wp->w_popup_timer->tr_callback = get_callback(&tv);
248 clear_tv(&tv);
249 }
250}
251#endif
252
Bram Moolenaar17627312019-06-02 19:53:44 +0200253/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200254 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200255 */
256 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200257apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200258{
Bram Moolenaarae943152019-06-16 22:54:14 +0200259 int nr;
260
261 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
262 wp->w_minwidth = nr;
263 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
264 wp->w_minheight = nr;
265 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
266 wp->w_maxwidth = nr;
267 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
268 wp->w_maxheight = nr;
269 get_pos_options(wp, d);
270}
271
272/*
273 * Shared between popup_create() and f_popup_setoptions().
274 */
275 static void
276apply_general_options(win_T *wp, dict_T *dict)
277{
278 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200279 int nr;
280 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200281
Bram Moolenaarae943152019-06-16 22:54:14 +0200282 // TODO: flip
283
284 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200285 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200286 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
287 if (wp->w_firstline < 1)
288 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200289
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200290 str = dict_get_string(dict, (char_u *)"title", FALSE);
291 if (str != NULL)
292 {
293 vim_free(wp->w_popup_title);
294 wp->w_popup_title = vim_strsave(str);
295 }
296
Bram Moolenaar402502d2019-05-30 22:07:36 +0200297 di = dict_find(dict, (char_u *)"wrap", -1);
298 if (di != NULL)
299 {
300 nr = dict_get_number(dict, (char_u *)"wrap");
301 wp->w_p_wrap = nr != 0;
302 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200303
Bram Moolenaara42d9452019-06-15 21:46:30 +0200304 di = dict_find(dict, (char_u *)"drag", -1);
305 if (di != NULL)
306 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200307
Bram Moolenaarae943152019-06-16 22:54:14 +0200308 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
309 if (str != NULL)
310 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
311 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200312
Bram Moolenaarae943152019-06-16 22:54:14 +0200313 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
314 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200315
Bram Moolenaar790498b2019-06-01 22:15:29 +0200316 di = dict_find(dict, (char_u *)"borderhighlight", -1);
317 if (di != NULL)
318 {
319 if (di->di_tv.v_type != VAR_LIST)
320 emsg(_(e_listreq));
321 else
322 {
323 list_T *list = di->di_tv.vval.v_list;
324 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200325 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200326
327 if (list != NULL)
328 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
329 ++i, li = li->li_next)
330 {
331 str = tv_get_string(&li->li_tv);
332 if (*str != NUL)
333 wp->w_border_highlight[i] = vim_strsave(str);
334 }
335 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
336 for (i = 1; i < 4; ++i)
337 wp->w_border_highlight[i] =
338 vim_strsave(wp->w_border_highlight[0]);
339 }
340 }
341
Bram Moolenaar790498b2019-06-01 22:15:29 +0200342 di = dict_find(dict, (char_u *)"borderchars", -1);
343 if (di != NULL)
344 {
345 if (di->di_tv.v_type != VAR_LIST)
346 emsg(_(e_listreq));
347 else
348 {
349 list_T *list = di->di_tv.vval.v_list;
350 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200351 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200352
353 if (list != NULL)
354 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
355 ++i, li = li->li_next)
356 {
357 str = tv_get_string(&li->li_tv);
358 if (*str != NUL)
359 wp->w_border_char[i] = mb_ptr2char(str);
360 }
361 if (list->lv_len == 1)
362 for (i = 1; i < 8; ++i)
363 wp->w_border_char[i] = wp->w_border_char[0];
364 if (list->lv_len == 2)
365 {
366 for (i = 4; i < 8; ++i)
367 wp->w_border_char[i] = wp->w_border_char[1];
368 for (i = 1; i < 4; ++i)
369 wp->w_border_char[i] = wp->w_border_char[0];
370 }
371 }
372 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200373
Bram Moolenaarae943152019-06-16 22:54:14 +0200374 di = dict_find(dict, (char_u *)"zindex", -1);
375 if (di != NULL)
376 {
377 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
378 if (wp->w_zindex < 1)
379 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
380 if (wp->w_zindex > 32000)
381 wp->w_zindex = 32000;
382 }
383
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200384 di = dict_find(dict, (char_u *)"mask", -1);
385 if (di != NULL)
386 {
387 int ok = TRUE;
388
389 if (di->di_tv.v_type != VAR_LIST)
390 ok = FALSE;
391 else if (di->di_tv.vval.v_list != NULL)
392 {
393 listitem_T *li;
394
395 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
396 li = li->li_next)
397 {
398 if (li->li_tv.v_type != VAR_LIST
399 || li->li_tv.vval.v_list == NULL
400 || li->li_tv.vval.v_list->lv_len != 4)
401 {
402 ok = FALSE;
403 break;
404 }
405 }
406 }
407 if (ok)
408 {
409 wp->w_popup_mask = di->di_tv.vval.v_list;
410 ++wp->w_popup_mask->lv_refcount;
411 }
412 else
413 semsg(_(e_invargval), "mask");
414 }
415
Bram Moolenaarae943152019-06-16 22:54:14 +0200416#if defined(FEAT_TIMERS)
417 // Add timer to close the popup after some time.
418 nr = dict_get_number(dict, (char_u *)"time");
419 if (nr > 0)
420 popup_add_timeout(wp, nr);
421#endif
422
Bram Moolenaar3397f742019-06-02 18:40:06 +0200423 di = dict_find(dict, (char_u *)"moved", -1);
424 if (di != NULL)
425 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200426 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200427 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
428 {
429 char_u *s = di->di_tv.vval.v_string;
430 int flags = 0;
431
432 if (STRCMP(s, "word") == 0)
433 flags = FIND_IDENT | FIND_STRING;
434 else if (STRCMP(s, "WORD") == 0)
435 flags = FIND_STRING;
436 else if (STRCMP(s, "any") != 0)
437 semsg(_(e_invarg2), s);
438 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200439 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200440 }
441 else if (di->di_tv.v_type == VAR_LIST
442 && di->di_tv.vval.v_list != NULL
443 && di->di_tv.vval.v_list->lv_len == 2)
444 {
445 list_T *l = di->di_tv.vval.v_list;
446
447 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
448 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
449 }
450 else
451 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
452 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200453
Bram Moolenaarae943152019-06-16 22:54:14 +0200454 di = dict_find(dict, (char_u *)"filter", -1);
455 if (di != NULL)
456 {
457 callback_T callback = get_callback(&di->di_tv);
458
459 if (callback.cb_name != NULL)
460 {
461 free_callback(&wp->w_filter_cb);
462 set_callback(&wp->w_filter_cb, &callback);
463 }
464 }
465
466 di = dict_find(dict, (char_u *)"callback", -1);
467 if (di != NULL)
468 {
469 callback_T callback = get_callback(&di->di_tv);
470
471 if (callback.cb_name != NULL)
472 {
473 free_callback(&wp->w_close_cb);
474 set_callback(&wp->w_close_cb, &callback);
475 }
476 }
477}
478
479/*
480 * Go through the options in "dict" and apply them to popup window "wp".
481 */
482 static void
483apply_options(win_T *wp, dict_T *dict)
484{
485 int nr;
486
487 apply_move_options(wp, dict);
488 apply_general_options(wp, dict);
489
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200490 nr = dict_get_number(dict, (char_u *)"hidden");
491 if (nr > 0)
492 {
493 wp->w_popup_flags |= POPF_HIDDEN;
494 --wp->w_buffer->b_nwindows;
495 }
496
Bram Moolenaar33796b32019-06-08 16:01:13 +0200497 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200498}
499
500/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200501 * Add lines to the popup from a list of strings.
502 */
503 static void
504add_popup_strings(buf_T *buf, list_T *l)
505{
506 listitem_T *li;
507 linenr_T lnum = 0;
508 char_u *p;
509
510 for (li = l->lv_first; li != NULL; li = li->li_next)
511 if (li->li_tv.v_type == VAR_STRING)
512 {
513 p = li->li_tv.vval.v_string;
514 ml_append_buf(buf, lnum++,
515 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
516 }
517}
518
519/*
520 * Add lines to the popup from a list of dictionaries.
521 */
522 static void
523add_popup_dicts(buf_T *buf, list_T *l)
524{
525 listitem_T *li;
526 listitem_T *pli;
527 linenr_T lnum = 0;
528 char_u *p;
529 dict_T *dict;
530
531 // first add the text lines
532 for (li = l->lv_first; li != NULL; li = li->li_next)
533 {
534 if (li->li_tv.v_type != VAR_DICT)
535 {
536 emsg(_(e_dictreq));
537 return;
538 }
539 dict = li->li_tv.vval.v_dict;
540 p = dict == NULL ? NULL
541 : dict_get_string(dict, (char_u *)"text", FALSE);
542 ml_append_buf(buf, lnum++,
543 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
544 }
545
546 // add the text properties
547 lnum = 1;
548 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
549 {
550 dictitem_T *di;
551 list_T *plist;
552
553 dict = li->li_tv.vval.v_dict;
554 di = dict_find(dict, (char_u *)"props", -1);
555 if (di != NULL)
556 {
557 if (di->di_tv.v_type != VAR_LIST)
558 {
559 emsg(_(e_listreq));
560 return;
561 }
562 plist = di->di_tv.vval.v_list;
563 if (plist != NULL)
564 {
565 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
566 {
567 if (pli->li_tv.v_type != VAR_DICT)
568 {
569 emsg(_(e_dictreq));
570 return;
571 }
572 dict = pli->li_tv.vval.v_dict;
573 if (dict != NULL)
574 {
575 int col = dict_get_number(dict, (char_u *)"col");
576
577 prop_add_common( lnum, col, dict, buf, NULL);
578 }
579 }
580 }
581 }
582 }
583}
584
585/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200586 * Return the height of popup window "wp", including border and padding.
587 */
588 int
589popup_height(win_T *wp)
590{
591 return wp->w_height
592 + wp->w_popup_padding[0] + wp->w_popup_border[0]
593 + wp->w_popup_padding[2] + wp->w_popup_border[2];
594}
595
596/*
597 * Return the width of popup window "wp", including border and padding.
598 */
599 int
600popup_width(win_T *wp)
601{
602 return wp->w_width
603 + wp->w_popup_padding[3] + wp->w_popup_border[3]
604 + wp->w_popup_padding[1] + wp->w_popup_border[1];
605}
606
607/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200608 * Get the padding plus border at the top, adjusted to 1 if there is a title.
609 */
610 static int
611popup_top_extra(win_T *wp)
612{
613 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
614
615 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
616 return 1;
617 return extra;
618}
619
620/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200621 * Adjust the position and size of the popup to fit on the screen.
622 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200623 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200624popup_adjust_position(win_T *wp)
625{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200626 linenr_T lnum;
627 int wrapped = 0;
628 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200629 int center_vert = FALSE;
630 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200631 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200632 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200633 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
634 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
635 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
636 int extra_height = top_extra + bot_extra;
637 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200638 int org_winrow = wp->w_winrow;
639 int org_wincol = wp->w_wincol;
640 int org_width = wp->w_width;
641 int org_height = wp->w_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200642 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200643
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200644 wp->w_winrow = 0;
645 wp->w_wincol = 0;
646 if (wp->w_popup_pos == POPPOS_CENTER)
647 {
648 // center after computing the size
649 center_vert = TRUE;
650 center_hor = TRUE;
651 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200652 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200653 {
654 if (wp->w_wantline == 0)
655 center_vert = TRUE;
656 else if (wp->w_popup_pos == POPPOS_TOPLEFT
657 || wp->w_popup_pos == POPPOS_TOPRIGHT)
658 {
659 wp->w_winrow = wp->w_wantline - 1;
660 if (wp->w_winrow >= Rows)
661 wp->w_winrow = Rows - 1;
662 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200663
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200664 if (wp->w_wantcol == 0)
665 center_hor = TRUE;
666 else if (wp->w_popup_pos == POPPOS_TOPLEFT
667 || wp->w_popup_pos == POPPOS_BOTLEFT)
668 {
669 wp->w_wincol = wp->w_wantcol - 1;
670 if (wp->w_wincol >= Columns - 3)
671 wp->w_wincol = Columns - 3;
672 }
673 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200674
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200675 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200676 // When left aligned use the space available, but shift to the left when we
677 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200678 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200679 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200680 {
681 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200682 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200683 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200684
Bram Moolenaar8d241042019-06-12 23:40:01 +0200685 // start at the desired first line
686 wp->w_topline = wp->w_firstline;
687 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
688 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
689
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200690 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200691 // Use a minimum width of one, so that something shows when there is no
692 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200693 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200694 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200695 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200696 {
697 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
698
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200699 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200700 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200701 while (len > maxwidth)
702 {
703 ++wrapped;
704 len -= maxwidth;
705 wp->w_width = maxwidth;
706 }
707 }
708 else if (len > maxwidth
709 && allow_adjust_left
710 && (wp->w_popup_pos == POPPOS_TOPLEFT
711 || wp->w_popup_pos == POPPOS_BOTLEFT))
712 {
713 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200714 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200715
Bram Moolenaar51c31312019-06-15 22:27:23 +0200716 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200717 {
718 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200719
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200720 len -= truncate_shift;
721 shift_by -= truncate_shift;
722 }
723
724 wp->w_wincol -= shift_by;
725 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200726 wp->w_width = maxwidth;
727 }
728 if (wp->w_width < len)
729 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200730 // do not use the width of lines we're not going to show
731 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
732 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
733 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200734 }
735
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200736 minwidth = wp->w_minwidth;
737 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
738 {
739 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
740
741 if (minwidth < title_len)
742 minwidth = title_len;
743 }
744
745 if (minwidth > 0 && wp->w_width < minwidth)
746 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200747 if (wp->w_width > maxwidth)
748 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200749 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200750 {
751 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
752 if (wp->w_wincol < 0)
753 wp->w_wincol = 0;
754 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200755 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
756 || wp->w_popup_pos == POPPOS_TOPRIGHT)
757 {
758 // Right aligned: move to the right if needed.
759 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200760 if (wp->w_width + extra_width < wp->w_wantcol)
761 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200762 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200763
Bram Moolenaar8d241042019-06-12 23:40:01 +0200764 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
765 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200766 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
767 wp->w_height = wp->w_minheight;
768 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
769 wp->w_height = wp->w_maxheight;
770 if (wp->w_height > Rows - wp->w_winrow)
771 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200772
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200773 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200774 {
775 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
776 if (wp->w_winrow < 0)
777 wp->w_winrow = 0;
778 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200779 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
780 || wp->w_popup_pos == POPPOS_BOTLEFT)
781 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200782 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200783 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200784 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200785 else
786 // not enough space, make top aligned
787 wp->w_winrow = wp->w_wantline + 1;
788 }
789
Bram Moolenaar17146962019-05-30 00:12:11 +0200790 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200791
792 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200793 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200794 if (org_winrow != wp->w_winrow
795 || org_wincol != wp->w_wincol
796 || org_width != wp->w_width
797 || org_height != wp->w_height)
798 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200799 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200800 popup_mask_refresh = TRUE;
801 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200802}
803
Bram Moolenaar17627312019-06-02 19:53:44 +0200804typedef enum
805{
806 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200807 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200808 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200809 TYPE_DIALOG,
810 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200811} create_type_T;
812
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200813/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200814 * Make "buf" empty and set the contents to "text".
815 * Used by popup_create() and popup_settext().
816 */
817 static void
818popup_set_buffer_text(buf_T *buf, typval_T text)
819{
820 int lnum;
821
822 // Clear the buffer, then replace the lines.
823 curbuf = buf;
824 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
825 ml_delete(lnum, FALSE);
826 curbuf = curwin->w_buffer;
827
828 // Add text to the buffer.
829 if (text.v_type == VAR_STRING)
830 {
831 // just a string
832 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
833 }
834 else
835 {
836 list_T *l = text.vval.v_list;
837
838 if (l->lv_len > 0)
839 {
840 if (l->lv_first->li_tv.v_type == VAR_STRING)
841 // list of strings
842 add_popup_strings(buf, l);
843 else
844 // list of dictionaries
845 add_popup_dicts(buf, l);
846 }
847 }
848
849 // delete the line that was in the empty buffer
850 curbuf = buf;
851 ml_delete(buf->b_ml.ml_line_count, FALSE);
852 curbuf = curwin->w_buffer;
853}
854
855/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200856 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200857 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200858 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200859 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200860popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200861{
Bram Moolenaara3fce622019-06-20 02:31:49 +0200862 win_T *wp;
863 tabpage_T *tp = NULL;
864 int tabnr;
865 buf_T *buf;
866 dict_T *d;
867 int nr;
868 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200869
870 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200871 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
872 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200873 {
874 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200875 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200876 }
877 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
878 {
879 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200880 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200881 }
882 d = argvars[1].vval.v_dict;
883
Bram Moolenaara3fce622019-06-20 02:31:49 +0200884 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
885 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
886 else if (type == TYPE_NOTIFICATION)
887 tabnr = -1; // notifications are global by default
888 else
889 tabnr = 0;
890 if (tabnr > 0)
891 {
892 tp = find_tabpage(tabnr);
893 if (tp == NULL)
894 {
895 semsg(_("E996: Tabpage not found: %d"), tabnr);
896 return NULL;
897 }
898 }
899
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200900 // Create the window and buffer.
901 wp = win_alloc_popup_win();
902 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200903 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200904 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200905 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200906
907 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
908 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200909 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200910 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200911
912 win_init_popup_win(wp, buf);
913
914 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200915 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200916 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200917 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200918 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200919 buf->b_p_ul = -1; // no undo
920 buf->b_p_swf = FALSE; // no swap file
921 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200922 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200923 wp->w_p_wrap = TRUE; // 'wrap' is default on
924
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200925 // Avoid that 'buftype' is reset when this buffer is entered.
926 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200927
Bram Moolenaara3fce622019-06-20 02:31:49 +0200928 if (tp != NULL)
929 {
930 // popup on specified tab page
931 wp->w_next = tp->tp_first_popupwin;
932 tp->tp_first_popupwin = wp;
933 }
934 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200935 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200936 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200937 wp->w_next = curtab->tp_first_popupwin;
938 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200939 }
Bram Moolenaara3fce622019-06-20 02:31:49 +0200940 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200941 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200942 win_T *prev = first_popupwin;
943
944 // Global popup: add at the end, so that it gets displayed on top of
945 // older ones with the same zindex. Matters for notifications.
946 if (first_popupwin == NULL)
947 first_popupwin = wp;
948 else
949 {
950 while (prev->w_next != NULL)
951 prev = prev->w_next;
952 prev->w_next = wp;
953 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200954 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200955
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200956 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200957
Bram Moolenaar17627312019-06-02 19:53:44 +0200958 if (type == TYPE_ATCURSOR)
959 {
960 wp->w_popup_pos = POPPOS_BOTLEFT;
961 setcursor_mayforce(TRUE);
962 wp->w_wantline = screen_screenrow();
963 if (wp->w_wantline == 0) // cursor in first line
964 {
965 wp->w_wantline = 2;
966 wp->w_popup_pos = POPPOS_TOPLEFT;
967 }
968 wp->w_wantcol = screen_screencol() + 1;
969 set_moved_values(wp);
970 set_moved_columns(wp, FIND_STRING);
971 }
972
Bram Moolenaar33796b32019-06-08 16:01:13 +0200973 // set default values
974 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
975
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200976 if (type == TYPE_NOTIFICATION)
977 {
978 win_T *twp, *nextwin;
979 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200980
981 // Try to not overlap with another global popup. Guess we need 3
982 // more screen lines than buffer lines.
983 wp->w_wantline = 1;
984 for (twp = first_popupwin; twp != NULL; twp = nextwin)
985 {
986 nextwin = twp->w_next;
987 if (twp != wp
988 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
989 && twp->w_winrow <= wp->w_wantline - 1 + height
990 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
991 {
992 // move to below this popup and restart the loop to check for
993 // overlap with other popups
994 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
995 nextwin = first_popupwin;
996 }
997 }
998 if (wp->w_wantline + height > Rows)
999 {
1000 // can't avoid overlap, put on top in the hope that message goes
1001 // away soon.
1002 wp->w_wantline = 1;
1003 }
1004
1005 wp->w_wantcol = 10;
1006 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001007 wp->w_minwidth = 20;
1008 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001009 for (i = 0; i < 4; ++i)
1010 wp->w_popup_border[i] = 1;
1011 wp->w_popup_padding[1] = 1;
1012 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001013
1014 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001015 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001016 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1017 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001018 }
1019
Bram Moolenaara730e552019-06-16 19:05:31 +02001020 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001021 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001022 wp->w_popup_pos = POPPOS_CENTER;
1023 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1024 wp->w_popup_drag = 1;
1025 for (i = 0; i < 4; ++i)
1026 {
1027 wp->w_popup_border[i] = 1;
1028 wp->w_popup_padding[i] = 1;
1029 }
1030 }
1031
Bram Moolenaara730e552019-06-16 19:05:31 +02001032 if (type == TYPE_MENU)
1033 {
1034 typval_T tv;
1035 callback_T callback;
1036
1037 tv.v_type = VAR_STRING;
1038 tv.vval.v_string = (char_u *)"popup_filter_menu";
1039 callback = get_callback(&tv);
1040 if (callback.cb_name != NULL)
1041 set_callback(&wp->w_filter_cb, &callback);
1042
1043 wp->w_p_wrap = 0;
1044 }
1045
Bram Moolenaarae943152019-06-16 22:54:14 +02001046 for (i = 0; i < 4; ++i)
1047 VIM_CLEAR(wp->w_border_highlight[i]);
1048 for (i = 0; i < 8; ++i)
1049 wp->w_border_char[i] = 0;
1050
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001051 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001052 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001053
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001054 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1055 popup_add_timeout(wp, 3000);
1056
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001057 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001058
1059 wp->w_vsep_width = 0;
1060
1061 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001062 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001063
1064 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001065}
1066
1067/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001068 * popup_clear()
1069 */
1070 void
1071f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1072{
1073 close_all_popups();
1074}
1075
1076/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001077 * popup_create({text}, {options})
1078 */
1079 void
1080f_popup_create(typval_T *argvars, typval_T *rettv)
1081{
Bram Moolenaar17627312019-06-02 19:53:44 +02001082 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001083}
1084
1085/*
1086 * popup_atcursor({text}, {options})
1087 */
1088 void
1089f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1090{
Bram Moolenaar17627312019-06-02 19:53:44 +02001091 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001092}
1093
1094/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001095 * Invoke the close callback for window "wp" with value "result".
1096 * Careful: The callback may make "wp" invalid!
1097 */
1098 static void
1099invoke_popup_callback(win_T *wp, typval_T *result)
1100{
1101 typval_T rettv;
1102 int dummy;
1103 typval_T argv[3];
1104
1105 argv[0].v_type = VAR_NUMBER;
1106 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1107
1108 if (result != NULL && result->v_type != VAR_UNKNOWN)
1109 copy_tv(result, &argv[1]);
1110 else
1111 {
1112 argv[1].v_type = VAR_NUMBER;
1113 argv[1].vval.v_number = 0;
1114 }
1115
1116 argv[2].v_type = VAR_UNKNOWN;
1117
1118 call_callback(&wp->w_close_cb, -1,
1119 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1120 if (result != NULL)
1121 clear_tv(&argv[1]);
1122 clear_tv(&rettv);
1123}
1124
1125/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001126 * Close popup "wp" and invoke any close callback for it.
1127 */
1128 static void
1129popup_close_and_callback(win_T *wp, typval_T *arg)
1130{
1131 int id = wp->w_id;
1132
1133 if (wp->w_close_cb.cb_name != NULL)
1134 // Careful: This may make "wp" invalid.
1135 invoke_popup_callback(wp, arg);
1136
1137 popup_close(id);
1138}
1139
1140/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001141 * In a filter: check if the typed key is a mouse event that is used for
1142 * dragging the popup.
1143 */
1144 static void
1145filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1146{
1147 int row = mouse_row;
1148 int col = mouse_col;
1149
1150 if (wp->w_popup_drag
1151 && is_mouse_key(c)
1152 && (wp == popup_dragwin
1153 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1154 // do not consume the key, allow for dragging the popup
1155 rettv->vval.v_number = 0;
1156}
1157
1158 static void
1159popup_highlight_curline(win_T *wp)
1160{
1161 int id;
1162 char buf[100];
1163
1164 match_delete(wp, 1, FALSE);
1165
1166 id = syn_name2id((char_u *)"PopupSelected");
1167 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1168 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1169 (char_u *)buf, 10, 1, NULL, NULL);
1170}
1171
1172/*
1173 * popup_filter_menu({text}, {options})
1174 */
1175 void
1176f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1177{
1178 int id = tv_get_number(&argvars[0]);
1179 win_T *wp = win_id2wp(id);
1180 char_u *key = tv_get_string(&argvars[1]);
1181 typval_T res;
1182 int c;
1183 linenr_T old_lnum;
1184
1185 // If the popup has been closed do not consume the key.
1186 if (wp == NULL)
1187 return;
1188
1189 c = *key;
1190 if (c == K_SPECIAL && key[1] != NUL)
1191 c = TO_SPECIAL(key[1], key[2]);
1192
1193 // consume all keys until done
1194 rettv->vval.v_number = 1;
1195 res.v_type = VAR_NUMBER;
1196
1197 old_lnum = wp->w_cursor.lnum;
1198 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1199 --wp->w_cursor.lnum;
1200 if ((c == 'j' || c == 'J' || c == K_DOWN)
1201 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1202 ++wp->w_cursor.lnum;
1203 if (old_lnum != wp->w_cursor.lnum)
1204 {
1205 popup_highlight_curline(wp);
1206 return;
1207 }
1208
1209 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1210 {
1211 // Cancelled, invoke callback with -1
1212 res.vval.v_number = -1;
1213 popup_close_and_callback(wp, &res);
1214 return;
1215 }
1216 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1217 {
1218 // Invoke callback with current index.
1219 res.vval.v_number = wp->w_cursor.lnum;
1220 popup_close_and_callback(wp, &res);
1221 return;
1222 }
1223
1224 filter_handle_drag(wp, c, rettv);
1225}
1226
1227/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001228 * popup_filter_yesno({text}, {options})
1229 */
1230 void
1231f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1232{
1233 int id = tv_get_number(&argvars[0]);
1234 win_T *wp = win_id2wp(id);
1235 char_u *key = tv_get_string(&argvars[1]);
1236 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001237 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001238
1239 // If the popup has been closed don't consume the key.
1240 if (wp == NULL)
1241 return;
1242
Bram Moolenaara730e552019-06-16 19:05:31 +02001243 c = *key;
1244 if (c == K_SPECIAL && key[1] != NUL)
1245 c = TO_SPECIAL(key[1], key[2]);
1246
Bram Moolenaara42d9452019-06-15 21:46:30 +02001247 // consume all keys until done
1248 rettv->vval.v_number = 1;
1249
Bram Moolenaara730e552019-06-16 19:05:31 +02001250 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001251 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001252 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001253 res.vval.v_number = 0;
1254 else
1255 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001256 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001257 return;
1258 }
1259
1260 // Invoke callback
1261 res.v_type = VAR_NUMBER;
1262 popup_close_and_callback(wp, &res);
1263}
1264
1265/*
1266 * popup_dialog({text}, {options})
1267 */
1268 void
1269f_popup_dialog(typval_T *argvars, typval_T *rettv)
1270{
1271 popup_create(argvars, rettv, TYPE_DIALOG);
1272}
1273
1274/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001275 * popup_menu({text}, {options})
1276 */
1277 void
1278f_popup_menu(typval_T *argvars, typval_T *rettv)
1279{
1280 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1281
1282 if (wp != NULL)
1283 popup_highlight_curline(wp);
1284}
1285
1286/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001287 * popup_notification({text}, {options})
1288 */
1289 void
1290f_popup_notification(typval_T *argvars, typval_T *rettv)
1291{
1292 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1293}
1294
1295/*
1296 * Find the popup window with window-ID "id".
1297 * If the popup window does not exist NULL is returned.
1298 * If the window is not a popup window, and error message is given.
1299 */
1300 static win_T *
1301find_popup_win(int id)
1302{
1303 win_T *wp = win_id2wp(id);
1304
1305 if (wp != NULL && !bt_popup(wp->w_buffer))
1306 {
1307 semsg(_("E993: window %d is not a popup window"), id);
1308 return NULL;
1309 }
1310 return wp;
1311}
1312
1313/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001314 * popup_close({id})
1315 */
1316 void
1317f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1318{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001319 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001320 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001321
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001322 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001323 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001324}
1325
1326/*
1327 * popup_hide({id})
1328 */
1329 void
1330f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1331{
1332 int id = (int)tv_get_number(argvars);
1333 win_T *wp = find_popup_win(id);
1334
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001335 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001336 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001337 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001338 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001339 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001340 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001341 }
1342}
1343
1344/*
1345 * popup_show({id})
1346 */
1347 void
1348f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1349{
1350 int id = (int)tv_get_number(argvars);
1351 win_T *wp = find_popup_win(id);
1352
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001353 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001354 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001355 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001356 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001357 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001358 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001359 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001360}
1361
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001362/*
1363 * popup_settext({id}, {text})
1364 */
1365 void
1366f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1367{
1368 int id = (int)tv_get_number(&argvars[0]);
1369 win_T *wp = find_popup_win(id);
1370
1371 if (wp != NULL)
1372 {
1373 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1374 popup_adjust_position(wp);
1375 }
1376}
1377
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001378 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001379popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001380{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001381 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001382 if (wp->w_winrow + wp->w_height >= cmdline_row)
1383 clear_cmdline = TRUE;
1384 win_free_popup(wp);
1385 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001386 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001387}
1388
Bram Moolenaarec583842019-05-26 14:11:23 +02001389/*
1390 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001391 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001392 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001393 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001394popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001395{
1396 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001397 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001398 win_T *prev = NULL;
1399
Bram Moolenaarec583842019-05-26 14:11:23 +02001400 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001401 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001402 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001403 {
1404 if (prev == NULL)
1405 first_popupwin = wp->w_next;
1406 else
1407 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001408 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001409 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001410 }
1411
Bram Moolenaarec583842019-05-26 14:11:23 +02001412 // go through tab-local popups
1413 FOR_ALL_TABPAGES(tp)
1414 popup_close_tabpage(tp, id);
1415}
1416
1417/*
1418 * Close a popup window with Window-id "id" in tabpage "tp".
1419 */
1420 void
1421popup_close_tabpage(tabpage_T *tp, int id)
1422{
1423 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001424 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001425 win_T *prev = NULL;
1426
Bram Moolenaarec583842019-05-26 14:11:23 +02001427 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1428 if (wp->w_id == id)
1429 {
1430 if (prev == NULL)
1431 *root = wp->w_next;
1432 else
1433 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001434 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001435 return;
1436 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001437}
1438
1439 void
1440close_all_popups(void)
1441{
1442 while (first_popupwin != NULL)
1443 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001444 while (curtab->tp_first_popupwin != NULL)
1445 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001446}
1447
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001448/*
1449 * popup_move({id}, {options})
1450 */
1451 void
1452f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1453{
Bram Moolenaarae943152019-06-16 22:54:14 +02001454 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001455 int id = (int)tv_get_number(argvars);
1456 win_T *wp = find_popup_win(id);
1457
1458 if (wp == NULL)
1459 return; // invalid {id}
1460
1461 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1462 {
1463 emsg(_(e_dictreq));
1464 return;
1465 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001466 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001467
Bram Moolenaarae943152019-06-16 22:54:14 +02001468 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001469
1470 if (wp->w_winrow + wp->w_height >= cmdline_row)
1471 clear_cmdline = TRUE;
1472 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001473}
1474
Bram Moolenaarbc133542019-05-29 20:26:46 +02001475/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001476 * popup_setoptions({id}, {options})
1477 */
1478 void
1479f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1480{
1481 dict_T *dict;
1482 int id = (int)tv_get_number(argvars);
1483 win_T *wp = find_popup_win(id);
1484
1485 if (wp == NULL)
1486 return; // invalid {id}
1487
1488 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1489 {
1490 emsg(_(e_dictreq));
1491 return;
1492 }
1493 dict = argvars[1].vval.v_dict;
1494
1495 apply_move_options(wp, dict);
1496 apply_general_options(wp, dict);
1497
Bram Moolenaarad24a712019-06-17 20:05:45 +02001498 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001499 popup_adjust_position(wp);
1500}
1501
1502/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001503 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001504 */
1505 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001506f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001507{
1508 dict_T *dict;
1509 int id = (int)tv_get_number(argvars);
1510 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001511 int top_extra;
1512 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001513
1514 if (rettv_dict_alloc(rettv) == OK)
1515 {
1516 if (wp == NULL)
1517 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001518 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001519 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1520
Bram Moolenaarbc133542019-05-29 20:26:46 +02001521 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001522
Bram Moolenaarbc133542019-05-29 20:26:46 +02001523 dict_add_number(dict, "line", wp->w_winrow + 1);
1524 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001525 dict_add_number(dict, "width", wp->w_width + left_extra
1526 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1527 dict_add_number(dict, "height", wp->w_height + top_extra
1528 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001529
1530 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1531 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1532 dict_add_number(dict, "core_width", wp->w_width);
1533 dict_add_number(dict, "core_height", wp->w_height);
1534
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001535 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001536 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001537 }
1538}
1539
1540/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001541 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1542 */
1543 static void
1544get_padding_border(dict_T *dict, int *array, char *name)
1545{
1546 list_T *list;
1547 int i;
1548
1549 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1550 return;
1551
1552 list = list_alloc();
1553 if (list != NULL)
1554 {
1555 dict_add_list(dict, name, list);
1556 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1557 for (i = 0; i < 4; ++i)
1558 list_append_number(list, array[i]);
1559 }
1560}
1561
1562/*
1563 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1564 */
1565 static void
1566get_borderhighlight(dict_T *dict, win_T *wp)
1567{
1568 list_T *list;
1569 int i;
1570
1571 for (i = 0; i < 4; ++i)
1572 if (wp->w_border_highlight[i] != NULL)
1573 break;
1574 if (i == 4)
1575 return;
1576
1577 list = list_alloc();
1578 if (list != NULL)
1579 {
1580 dict_add_list(dict, "borderhighlight", list);
1581 for (i = 0; i < 4; ++i)
1582 list_append_string(list, wp->w_border_highlight[i], -1);
1583 }
1584}
1585
1586/*
1587 * For popup_getoptions(): add a "borderchars" entry to "dict".
1588 */
1589 static void
1590get_borderchars(dict_T *dict, win_T *wp)
1591{
1592 list_T *list;
1593 int i;
1594 char_u buf[NUMBUFLEN];
1595 int len;
1596
1597 for (i = 0; i < 8; ++i)
1598 if (wp->w_border_char[i] != 0)
1599 break;
1600 if (i == 8)
1601 return;
1602
1603 list = list_alloc();
1604 if (list != NULL)
1605 {
1606 dict_add_list(dict, "borderchars", list);
1607 for (i = 0; i < 8; ++i)
1608 {
1609 len = mb_char2bytes(wp->w_border_char[i], buf);
1610 list_append_string(list, buf, len);
1611 }
1612 }
1613}
1614
1615/*
1616 * For popup_getoptions(): add a "moved" entry to "dict".
1617 */
1618 static void
1619get_moved_list(dict_T *dict, win_T *wp)
1620{
1621 list_T *list;
1622
1623 list = list_alloc();
1624 if (list != NULL)
1625 {
1626 dict_add_list(dict, "moved", list);
1627 list_append_number(list, wp->w_popup_mincol);
1628 list_append_number(list, wp->w_popup_maxcol);
1629 }
1630}
1631
1632/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001633 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001634 */
1635 void
1636f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1637{
1638 dict_T *dict;
1639 int id = (int)tv_get_number(argvars);
1640 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001641 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001642 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001643
1644 if (rettv_dict_alloc(rettv) == OK)
1645 {
1646 if (wp == NULL)
1647 return;
1648
1649 dict = rettv->vval.v_dict;
1650 dict_add_number(dict, "line", wp->w_wantline);
1651 dict_add_number(dict, "col", wp->w_wantcol);
1652 dict_add_number(dict, "minwidth", wp->w_minwidth);
1653 dict_add_number(dict, "minheight", wp->w_minheight);
1654 dict_add_number(dict, "maxheight", wp->w_maxheight);
1655 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001656 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001657 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001658 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001659 dict_add_string(dict, "title", wp->w_popup_title);
1660 dict_add_number(dict, "wrap", wp->w_p_wrap);
1661 dict_add_number(dict, "drag", wp->w_popup_drag);
1662 dict_add_string(dict, "highlight", wp->w_p_wcr);
1663
Bram Moolenaara3fce622019-06-20 02:31:49 +02001664 // find the tabpage that holds this popup
1665 i = 1;
1666 FOR_ALL_TABPAGES(tp)
1667 {
1668 win_T *p;
1669
1670 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1671 if (p->w_id == id)
1672 break;
1673 if (p != NULL)
1674 break;
1675 ++i;
1676 }
1677 if (tp == NULL)
1678 i = -1; // must be global
1679 else if (tp == curtab)
1680 i = 0;
1681 dict_add_number(dict, "tabpage", i);
1682
Bram Moolenaarae943152019-06-16 22:54:14 +02001683 get_padding_border(dict, wp->w_popup_padding, "padding");
1684 get_padding_border(dict, wp->w_popup_border, "border");
1685 get_borderhighlight(dict, wp);
1686 get_borderchars(dict, wp);
1687 get_moved_list(dict, wp);
1688
1689 if (wp->w_filter_cb.cb_name != NULL)
1690 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1691 if (wp->w_close_cb.cb_name != NULL)
1692 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001693
1694 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1695 ++i)
1696 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1697 {
1698 dict_add_string(dict, "pos",
1699 (char_u *)poppos_entries[i].pp_name);
1700 break;
1701 }
1702
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001703# if defined(FEAT_TIMERS)
1704 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1705 ? (long)wp->w_popup_timer->tr_interval : 0L);
1706# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001707 }
1708}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001709
1710 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001711error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001712{
1713 if (bt_popup(curwin->w_buffer))
1714 {
1715 emsg(_("E994: Not allowed in a popup window"));
1716 return TRUE;
1717 }
1718 return FALSE;
1719}
1720
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001721/*
1722 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001723 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001724 */
1725 void
1726popup_reset_handled()
1727{
1728 win_T *wp;
1729
1730 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1731 wp->w_popup_flags &= ~POPF_HANDLED;
1732 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1733 wp->w_popup_flags &= ~POPF_HANDLED;
1734}
1735
1736/*
1737 * Find the next visible popup where POPF_HANDLED is not set.
1738 * Must have called popup_reset_handled() first.
1739 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1740 * popup with the highest zindex.
1741 */
1742 win_T *
1743find_next_popup(int lowest)
1744{
1745 win_T *wp;
1746 win_T *found_wp;
1747 int found_zindex;
1748
1749 found_zindex = lowest ? INT_MAX : 0;
1750 found_wp = NULL;
1751 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1752 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1753 && (lowest ? wp->w_zindex < found_zindex
1754 : wp->w_zindex > found_zindex))
1755 {
1756 found_zindex = wp->w_zindex;
1757 found_wp = wp;
1758 }
1759 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1760 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1761 && (lowest ? wp->w_zindex < found_zindex
1762 : wp->w_zindex > found_zindex))
1763 {
1764 found_zindex = wp->w_zindex;
1765 found_wp = wp;
1766 }
1767
1768 if (found_wp != NULL)
1769 found_wp->w_popup_flags |= POPF_HANDLED;
1770 return found_wp;
1771}
1772
1773/*
1774 * Invoke the filter callback for window "wp" with typed character "c".
1775 * Uses the global "mod_mask" for modifiers.
1776 * Returns the return value of the filter.
1777 * Careful: The filter may make "wp" invalid!
1778 */
1779 static int
1780invoke_popup_filter(win_T *wp, int c)
1781{
1782 int res;
1783 typval_T rettv;
1784 int dummy;
1785 typval_T argv[3];
1786 char_u buf[NUMBUFLEN];
1787
Bram Moolenaara42d9452019-06-15 21:46:30 +02001788 // Emergency exit: CTRL-C closes the popup.
1789 if (c == Ctrl_C)
1790 {
1791 rettv.v_type = VAR_NUMBER;
1792 rettv.vval.v_number = -1;
1793 popup_close_and_callback(wp, &rettv);
1794 return 1;
1795 }
1796
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001797 argv[0].v_type = VAR_NUMBER;
1798 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1799
1800 // Convert the number to a string, so that the function can use:
1801 // if a:c == "\<F2>"
1802 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1803 argv[1].v_type = VAR_STRING;
1804 argv[1].vval.v_string = vim_strsave(buf);
1805
1806 argv[2].v_type = VAR_UNKNOWN;
1807
Bram Moolenaara42d9452019-06-15 21:46:30 +02001808 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001809 call_callback(&wp->w_filter_cb, -1,
1810 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1811 res = tv_get_number(&rettv);
1812 vim_free(argv[1].vval.v_string);
1813 clear_tv(&rettv);
1814 return res;
1815}
1816
1817/*
1818 * Called when "c" was typed: invoke popup filter callbacks.
1819 * Returns TRUE when the character was consumed,
1820 */
1821 int
1822popup_do_filter(int c)
1823{
1824 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001825 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001826
1827 popup_reset_handled();
1828
1829 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1830 if (wp->w_filter_cb.cb_name != NULL)
1831 res = invoke_popup_filter(wp, c);
1832
1833 return res;
1834}
1835
Bram Moolenaar3397f742019-06-02 18:40:06 +02001836/*
1837 * Called when the cursor moved: check if any popup needs to be closed if the
1838 * cursor moved far enough.
1839 */
1840 void
1841popup_check_cursor_pos()
1842{
1843 win_T *wp;
1844 typval_T tv;
1845
1846 popup_reset_handled();
1847 while ((wp = find_next_popup(TRUE)) != NULL)
1848 if (wp->w_popup_curwin != NULL
1849 && (curwin != wp->w_popup_curwin
1850 || curwin->w_cursor.lnum != wp->w_popup_lnum
1851 || curwin->w_cursor.col < wp->w_popup_mincol
1852 || curwin->w_cursor.col > wp->w_popup_maxcol))
1853 {
1854 tv.v_type = VAR_NUMBER;
1855 tv.vval.v_number = -1;
1856 popup_close_and_callback(wp, &tv);
1857 }
1858}
1859
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001860/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001861 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
1862 * "col" and "line" are screen coordinates.
1863 */
1864 static int
1865popup_masked(win_T *wp, int screencol, int screenline)
1866{
1867 int col = screencol - wp->w_wincol + 1;
1868 int line = screenline - wp->w_winrow + 1;
1869 listitem_T *lio, *li;
1870 int width, height;
1871
1872 if (wp->w_popup_mask == NULL)
1873 return FALSE;
1874 width = popup_width(wp);
1875 height = popup_height(wp);
1876
1877 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1878 {
1879 int cols, cole;
1880 int lines, linee;
1881
1882 li = lio->li_tv.vval.v_list->lv_first;
1883 cols = tv_get_number(&li->li_tv);
1884 if (cols < 0)
1885 cols = width + cols + 1;
1886 if (col < cols)
1887 continue;
1888 li = li->li_next;
1889 cole = tv_get_number(&li->li_tv);
1890 if (cole < 0)
1891 cole = width + cole + 1;
1892 if (col > cole)
1893 continue;
1894 li = li->li_next;
1895 lines = tv_get_number(&li->li_tv);
1896 if (lines < 0)
1897 lines = height + lines + 1;
1898 if (line < lines)
1899 continue;
1900 li = li->li_next;
1901 linee = tv_get_number(&li->li_tv);
1902 if (linee < 0)
1903 linee = height + linee + 1;
1904 if (line > linee)
1905 continue;
1906 return TRUE;
1907 }
1908 return FALSE;
1909}
1910
1911/*
1912 * Set flags in popup_transparent[] for window "wp" to "val".
1913 */
1914 static void
1915update_popup_transparent(win_T *wp, int val)
1916{
1917 if (wp->w_popup_mask != NULL)
1918 {
1919 int width = popup_width(wp);
1920 int height = popup_height(wp);
1921 listitem_T *lio, *li;
1922 int cols, cole;
1923 int lines, linee;
1924 int col, line;
1925
1926 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1927 {
1928 li = lio->li_tv.vval.v_list->lv_first;
1929 cols = tv_get_number(&li->li_tv);
1930 if (cols < 0)
1931 cols = width + cols + 1;
1932 li = li->li_next;
1933 cole = tv_get_number(&li->li_tv);
1934 if (cole < 0)
1935 cole = width + cole + 1;
1936 li = li->li_next;
1937 lines = tv_get_number(&li->li_tv);
1938 if (lines < 0)
1939 lines = height + lines + 1;
1940 li = li->li_next;
1941 linee = tv_get_number(&li->li_tv);
1942 if (linee < 0)
1943 linee = height + linee + 1;
1944
1945 --cols;
1946 --lines;
1947 for (line = lines; line < linee && line < screen_Rows; ++line)
1948 for (col = cols; col < cole && col < screen_Columns; ++col)
1949 popup_transparent[(line + wp->w_winrow) * screen_Columns
1950 + col + wp->w_wincol] = val;
1951 }
1952 }
1953}
1954
1955/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001956 * Update "popup_mask" if needed.
1957 * Also recomputes the popup size and positions.
1958 * Also updates "popup_visible".
1959 * Also marks window lines for redrawing.
1960 */
1961 void
1962may_update_popup_mask(int type)
1963{
1964 win_T *wp;
1965 short *mask;
1966 int line, col;
1967 int redraw_all = FALSE;
1968
1969 // Need to recompute when switching tabs.
1970 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1971 // (such as the screen size) must have changed.
1972 if (popup_mask_tab != curtab || type >= NOT_VALID)
1973 {
1974 popup_mask_refresh = TRUE;
1975 redraw_all = TRUE;
1976 }
1977 if (!popup_mask_refresh)
1978 {
1979 // Check if any buffer has changed.
1980 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1981 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1982 popup_mask_refresh = TRUE;
1983 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1984 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1985 popup_mask_refresh = TRUE;
1986 if (!popup_mask_refresh)
1987 return;
1988 }
1989
1990 // Need to update the mask, something has changed.
1991 popup_mask_refresh = FALSE;
1992 popup_mask_tab = curtab;
1993 popup_visible = FALSE;
1994
1995 // If redrawing everything, just update "popup_mask".
1996 // If redrawing only what is needed, update "popup_mask_next" and then
1997 // compare with "popup_mask" to see what changed.
1998 if (type >= SOME_VALID)
1999 mask = popup_mask;
2000 else
2001 mask = popup_mask_next;
2002 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2003
2004 // Find the window with the lowest zindex that hasn't been handled yet,
2005 // so that the window with a higher zindex overwrites the value in
2006 // popup_mask.
2007 popup_reset_handled();
2008 while ((wp = find_next_popup(TRUE)) != NULL)
2009 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002010 int height = popup_height(wp);
2011 int width = popup_width(wp);
2012
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002013 popup_visible = TRUE;
2014
2015 // Recompute the position if the text changed.
2016 if (redraw_all
2017 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2018 popup_adjust_position(wp);
2019
2020 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002021 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002022 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002023 col < wp->w_wincol + width && col < screen_Columns; ++col)
2024 if (!popup_masked(wp, col, line))
2025 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002026 }
2027
2028 // Only check which lines are to be updated if not already
2029 // updating all lines.
2030 if (mask == popup_mask_next)
2031 for (line = 0; line < screen_Rows; ++line)
2032 {
2033 int col_done = 0;
2034
2035 for (col = 0; col < screen_Columns; ++col)
2036 {
2037 int off = line * screen_Columns + col;
2038
2039 if (popup_mask[off] != popup_mask_next[off])
2040 {
2041 popup_mask[off] = popup_mask_next[off];
2042
2043 if (line >= cmdline_row)
2044 {
2045 // the command line needs to be cleared if text below
2046 // the popup is now visible.
2047 if (!msg_scrolled && popup_mask_next[off] == 0)
2048 clear_cmdline = TRUE;
2049 }
2050 else if (col >= col_done)
2051 {
2052 linenr_T lnum;
2053 int line_cp = line;
2054 int col_cp = col;
2055
2056 // The screen position "line" / "col" needs to be
2057 // redrawn. Figure out what window that is and update
2058 // w_redraw_top and w_redr_bot. Only needs to be done
2059 // once for each window line.
2060 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2061 if (wp != NULL)
2062 {
2063 if (line_cp >= wp->w_height)
2064 // In (or below) status line
2065 wp->w_redr_status = TRUE;
2066 // compute the position in the buffer line from the
2067 // position on the screen
2068 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2069 &lnum))
2070 // past bottom
2071 wp->w_redr_status = TRUE;
2072 else
2073 redrawWinline(wp, lnum);
2074
2075 // This line is going to be redrawn, no need to
2076 // check until the right side of the window.
2077 col_done = wp->w_wincol + wp->w_width - 1;
2078 }
2079 }
2080 }
2081 }
2082 }
2083}
2084
2085/*
2086 * Return a string of "len" spaces in IObuff.
2087 */
2088 static char_u *
2089get_spaces(int len)
2090{
2091 vim_memset(IObuff, ' ', (size_t)len);
2092 IObuff[len] = NUL;
2093 return IObuff;
2094}
2095
2096/*
2097 * Update popup windows. They are drawn on top of normal windows.
2098 * "win_update" is called for each popup window, lowest zindex first.
2099 */
2100 void
2101update_popups(void (*win_update)(win_T *wp))
2102{
2103 win_T *wp;
2104 int top_off;
2105 int left_off;
2106 int total_width;
2107 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002108 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002109 int popup_attr;
2110 int border_attr[4];
2111 int border_char[8];
2112 char_u buf[MB_MAXBYTES];
2113 int row;
2114 int i;
2115
2116 // Find the window with the lowest zindex that hasn't been updated yet,
2117 // so that the window with a higher zindex is drawn later, thus goes on
2118 // top.
2119 popup_reset_handled();
2120 while ((wp = find_next_popup(TRUE)) != NULL)
2121 {
2122 // This drawing uses the zindex of the popup window, so that it's on
2123 // top of the text but doesn't draw when another popup with higher
2124 // zindex is on top of the character.
2125 screen_zindex = wp->w_zindex;
2126
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002127 // Set flags in popup_transparent[] for masked cells.
2128 update_popup_transparent(wp, 1);
2129
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002130 // adjust w_winrow and w_wincol for border and padding, since
2131 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002132 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002133 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2134 wp->w_winrow += top_off;
2135 wp->w_wincol += left_off;
2136
2137 // Draw the popup text.
2138 win_update(wp);
2139
2140 wp->w_winrow -= top_off;
2141 wp->w_wincol -= left_off;
2142
2143 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
2144 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002145 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002146 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2147 popup_attr = get_wcr_attr(wp);
2148
2149 // We can only use these line drawing characters when 'encoding' is
2150 // "utf-8" and 'ambiwidth' is "single".
2151 if (enc_utf8 && *p_ambw == 's')
2152 {
2153 border_char[0] = border_char[2] = 0x2550;
2154 border_char[1] = border_char[3] = 0x2551;
2155 border_char[4] = 0x2554;
2156 border_char[5] = 0x2557;
2157 border_char[6] = 0x255d;
2158 border_char[7] = 0x255a;
2159 }
2160 else
2161 {
2162 border_char[0] = border_char[2] = '-';
2163 border_char[1] = border_char[3] = '|';
2164 for (i = 4; i < 8; ++i)
2165 border_char[i] = '+';
2166 }
2167 for (i = 0; i < 8; ++i)
2168 if (wp->w_border_char[i] != 0)
2169 border_char[i] = wp->w_border_char[i];
2170
2171 for (i = 0; i < 4; ++i)
2172 {
2173 border_attr[i] = popup_attr;
2174 if (wp->w_border_highlight[i] != NULL)
2175 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2176 }
2177
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002178 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002179 if (wp->w_popup_border[0] > 0)
2180 {
2181 // top border
2182 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2183 wp->w_wincol,
2184 wp->w_wincol + total_width,
2185 wp->w_popup_border[3] != 0
2186 ? border_char[4] : border_char[0],
2187 border_char[0], border_attr[0]);
2188 if (wp->w_popup_border[1] > 0)
2189 {
2190 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2191 screen_puts(buf, wp->w_winrow,
2192 wp->w_wincol + total_width - 1, border_attr[1]);
2193 }
2194 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002195 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2196 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002197
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002198 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002199 {
2200 // top padding
2201 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002202 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002203 wp->w_wincol + wp->w_popup_border[3],
2204 wp->w_wincol + total_width - wp->w_popup_border[1],
2205 ' ', ' ', popup_attr);
2206 }
2207
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002208 // Title goes on top of border or padding.
2209 if (wp->w_popup_title != NULL)
2210 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2211 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2212
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002213 for (row = wp->w_winrow + wp->w_popup_border[0];
2214 row < wp->w_winrow + total_height - wp->w_popup_border[2];
2215 ++row)
2216 {
2217 // left border
2218 if (wp->w_popup_border[3] > 0)
2219 {
2220 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2221 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2222 }
2223 // left padding
2224 if (wp->w_popup_padding[3] > 0)
2225 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2226 wp->w_wincol + wp->w_popup_border[3], popup_attr);
2227 // right border
2228 if (wp->w_popup_border[1] > 0)
2229 {
2230 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2231 screen_puts(buf, row,
2232 wp->w_wincol + total_width - 1, border_attr[1]);
2233 }
2234 // right padding
2235 if (wp->w_popup_padding[1] > 0)
2236 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2237 wp->w_wincol + wp->w_popup_border[3]
2238 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2239 }
2240
2241 if (wp->w_popup_padding[2] > 0)
2242 {
2243 // bottom padding
2244 row = wp->w_winrow + wp->w_popup_border[0]
2245 + wp->w_popup_padding[0] + wp->w_height;
2246 screen_fill(row, row + wp->w_popup_padding[2],
2247 wp->w_wincol + wp->w_popup_border[3],
2248 wp->w_wincol + total_width - wp->w_popup_border[1],
2249 ' ', ' ', popup_attr);
2250 }
2251
2252 if (wp->w_popup_border[2] > 0)
2253 {
2254 // bottom border
2255 row = wp->w_winrow + total_height - 1;
2256 screen_fill(row , row + 1,
2257 wp->w_wincol,
2258 wp->w_wincol + total_width,
2259 wp->w_popup_border[3] != 0
2260 ? border_char[7] : border_char[2],
2261 border_char[2], border_attr[2]);
2262 if (wp->w_popup_border[1] > 0)
2263 {
2264 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2265 screen_puts(buf, row,
2266 wp->w_wincol + total_width - 1, border_attr[2]);
2267 }
2268 }
2269
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002270 update_popup_transparent(wp, 0);
2271
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002272 // Back to the normal zindex.
2273 screen_zindex = 0;
2274 }
2275}
2276
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002277/*
2278 * Mark references in callbacks of one popup window.
2279 */
2280 static int
2281set_ref_in_one_popup(win_T *wp, int copyID)
2282{
2283 int abort = FALSE;
2284 typval_T tv;
2285
2286 if (wp->w_close_cb.cb_partial != NULL)
2287 {
2288 tv.v_type = VAR_PARTIAL;
2289 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2290 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2291 }
2292 if (wp->w_filter_cb.cb_partial != NULL)
2293 {
2294 tv.v_type = VAR_PARTIAL;
2295 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2296 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2297 }
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002298 if (wp->w_popup_mask != NULL && wp->w_popup_mask->lv_copyID != copyID)
2299 {
2300 wp->w_popup_mask->lv_copyID = copyID;
2301 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID, NULL);
2302 }
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002303 return abort;
2304}
2305
2306/*
2307 * Set reference in callbacks of popup windows.
2308 */
2309 int
2310set_ref_in_popups(int copyID)
2311{
2312 int abort = FALSE;
2313 win_T *wp;
2314 tabpage_T *tp;
2315
2316 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2317 abort = abort || set_ref_in_one_popup(wp, copyID);
2318
2319 FOR_ALL_TABPAGES(tp)
2320 {
2321 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2322 abort = abort || set_ref_in_one_popup(wp, copyID);
2323 if (abort)
2324 break;
2325 }
2326 return abort;
2327}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002328#endif // FEAT_TEXT_PROP