blob: f6da71da82ba14ea78e027f2feadbc80d77a1613 [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 Moolenaar0fcf26b2019-06-23 01:03:51 +02001054#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001055 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1056 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001057#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001058
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001059 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001060
1061 wp->w_vsep_width = 0;
1062
1063 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001064 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001065
1066 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001067}
1068
1069/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001070 * popup_clear()
1071 */
1072 void
1073f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1074{
1075 close_all_popups();
1076}
1077
1078/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001079 * popup_create({text}, {options})
1080 */
1081 void
1082f_popup_create(typval_T *argvars, typval_T *rettv)
1083{
Bram Moolenaar17627312019-06-02 19:53:44 +02001084 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001085}
1086
1087/*
1088 * popup_atcursor({text}, {options})
1089 */
1090 void
1091f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1092{
Bram Moolenaar17627312019-06-02 19:53:44 +02001093 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001094}
1095
1096/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001097 * Invoke the close callback for window "wp" with value "result".
1098 * Careful: The callback may make "wp" invalid!
1099 */
1100 static void
1101invoke_popup_callback(win_T *wp, typval_T *result)
1102{
1103 typval_T rettv;
1104 int dummy;
1105 typval_T argv[3];
1106
1107 argv[0].v_type = VAR_NUMBER;
1108 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1109
1110 if (result != NULL && result->v_type != VAR_UNKNOWN)
1111 copy_tv(result, &argv[1]);
1112 else
1113 {
1114 argv[1].v_type = VAR_NUMBER;
1115 argv[1].vval.v_number = 0;
1116 }
1117
1118 argv[2].v_type = VAR_UNKNOWN;
1119
1120 call_callback(&wp->w_close_cb, -1,
1121 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1122 if (result != NULL)
1123 clear_tv(&argv[1]);
1124 clear_tv(&rettv);
1125}
1126
1127/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001128 * Close popup "wp" and invoke any close callback for it.
1129 */
1130 static void
1131popup_close_and_callback(win_T *wp, typval_T *arg)
1132{
1133 int id = wp->w_id;
1134
1135 if (wp->w_close_cb.cb_name != NULL)
1136 // Careful: This may make "wp" invalid.
1137 invoke_popup_callback(wp, arg);
1138
1139 popup_close(id);
1140}
1141
1142/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001143 * In a filter: check if the typed key is a mouse event that is used for
1144 * dragging the popup.
1145 */
1146 static void
1147filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1148{
1149 int row = mouse_row;
1150 int col = mouse_col;
1151
1152 if (wp->w_popup_drag
1153 && is_mouse_key(c)
1154 && (wp == popup_dragwin
1155 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1156 // do not consume the key, allow for dragging the popup
1157 rettv->vval.v_number = 0;
1158}
1159
1160 static void
1161popup_highlight_curline(win_T *wp)
1162{
1163 int id;
1164 char buf[100];
1165
1166 match_delete(wp, 1, FALSE);
1167
1168 id = syn_name2id((char_u *)"PopupSelected");
1169 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1170 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1171 (char_u *)buf, 10, 1, NULL, NULL);
1172}
1173
1174/*
1175 * popup_filter_menu({text}, {options})
1176 */
1177 void
1178f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1179{
1180 int id = tv_get_number(&argvars[0]);
1181 win_T *wp = win_id2wp(id);
1182 char_u *key = tv_get_string(&argvars[1]);
1183 typval_T res;
1184 int c;
1185 linenr_T old_lnum;
1186
1187 // If the popup has been closed do not consume the key.
1188 if (wp == NULL)
1189 return;
1190
1191 c = *key;
1192 if (c == K_SPECIAL && key[1] != NUL)
1193 c = TO_SPECIAL(key[1], key[2]);
1194
1195 // consume all keys until done
1196 rettv->vval.v_number = 1;
1197 res.v_type = VAR_NUMBER;
1198
1199 old_lnum = wp->w_cursor.lnum;
1200 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1201 --wp->w_cursor.lnum;
1202 if ((c == 'j' || c == 'J' || c == K_DOWN)
1203 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1204 ++wp->w_cursor.lnum;
1205 if (old_lnum != wp->w_cursor.lnum)
1206 {
1207 popup_highlight_curline(wp);
1208 return;
1209 }
1210
1211 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1212 {
1213 // Cancelled, invoke callback with -1
1214 res.vval.v_number = -1;
1215 popup_close_and_callback(wp, &res);
1216 return;
1217 }
1218 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1219 {
1220 // Invoke callback with current index.
1221 res.vval.v_number = wp->w_cursor.lnum;
1222 popup_close_and_callback(wp, &res);
1223 return;
1224 }
1225
1226 filter_handle_drag(wp, c, rettv);
1227}
1228
1229/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001230 * popup_filter_yesno({text}, {options})
1231 */
1232 void
1233f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1234{
1235 int id = tv_get_number(&argvars[0]);
1236 win_T *wp = win_id2wp(id);
1237 char_u *key = tv_get_string(&argvars[1]);
1238 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001239 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001240
1241 // If the popup has been closed don't consume the key.
1242 if (wp == NULL)
1243 return;
1244
Bram Moolenaara730e552019-06-16 19:05:31 +02001245 c = *key;
1246 if (c == K_SPECIAL && key[1] != NUL)
1247 c = TO_SPECIAL(key[1], key[2]);
1248
Bram Moolenaara42d9452019-06-15 21:46:30 +02001249 // consume all keys until done
1250 rettv->vval.v_number = 1;
1251
Bram Moolenaara730e552019-06-16 19:05:31 +02001252 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001253 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001254 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001255 res.vval.v_number = 0;
1256 else
1257 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001258 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001259 return;
1260 }
1261
1262 // Invoke callback
1263 res.v_type = VAR_NUMBER;
1264 popup_close_and_callback(wp, &res);
1265}
1266
1267/*
1268 * popup_dialog({text}, {options})
1269 */
1270 void
1271f_popup_dialog(typval_T *argvars, typval_T *rettv)
1272{
1273 popup_create(argvars, rettv, TYPE_DIALOG);
1274}
1275
1276/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001277 * popup_menu({text}, {options})
1278 */
1279 void
1280f_popup_menu(typval_T *argvars, typval_T *rettv)
1281{
1282 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1283
1284 if (wp != NULL)
1285 popup_highlight_curline(wp);
1286}
1287
1288/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001289 * popup_notification({text}, {options})
1290 */
1291 void
1292f_popup_notification(typval_T *argvars, typval_T *rettv)
1293{
1294 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1295}
1296
1297/*
1298 * Find the popup window with window-ID "id".
1299 * If the popup window does not exist NULL is returned.
1300 * If the window is not a popup window, and error message is given.
1301 */
1302 static win_T *
1303find_popup_win(int id)
1304{
1305 win_T *wp = win_id2wp(id);
1306
1307 if (wp != NULL && !bt_popup(wp->w_buffer))
1308 {
1309 semsg(_("E993: window %d is not a popup window"), id);
1310 return NULL;
1311 }
1312 return wp;
1313}
1314
1315/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001316 * popup_close({id})
1317 */
1318 void
1319f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1320{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001321 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001322 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001323
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001324 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001325 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001326}
1327
1328/*
1329 * popup_hide({id})
1330 */
1331 void
1332f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1333{
1334 int id = (int)tv_get_number(argvars);
1335 win_T *wp = find_popup_win(id);
1336
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001337 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001338 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001339 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001340 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001341 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001342 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001343 }
1344}
1345
1346/*
1347 * popup_show({id})
1348 */
1349 void
1350f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1351{
1352 int id = (int)tv_get_number(argvars);
1353 win_T *wp = find_popup_win(id);
1354
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001355 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001356 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001357 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001358 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001359 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001360 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001361 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001362}
1363
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001364/*
1365 * popup_settext({id}, {text})
1366 */
1367 void
1368f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1369{
1370 int id = (int)tv_get_number(&argvars[0]);
1371 win_T *wp = find_popup_win(id);
1372
1373 if (wp != NULL)
1374 {
1375 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1376 popup_adjust_position(wp);
1377 }
1378}
1379
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001380 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001381popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001382{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001383 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001384 if (wp->w_winrow + wp->w_height >= cmdline_row)
1385 clear_cmdline = TRUE;
1386 win_free_popup(wp);
1387 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001388 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001389}
1390
Bram Moolenaarec583842019-05-26 14:11:23 +02001391/*
1392 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001393 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001394 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001395 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001396popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001397{
1398 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001399 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001400 win_T *prev = NULL;
1401
Bram Moolenaarec583842019-05-26 14:11:23 +02001402 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001403 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001404 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001405 {
1406 if (prev == NULL)
1407 first_popupwin = wp->w_next;
1408 else
1409 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001410 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001411 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001412 }
1413
Bram Moolenaarec583842019-05-26 14:11:23 +02001414 // go through tab-local popups
1415 FOR_ALL_TABPAGES(tp)
1416 popup_close_tabpage(tp, id);
1417}
1418
1419/*
1420 * Close a popup window with Window-id "id" in tabpage "tp".
1421 */
1422 void
1423popup_close_tabpage(tabpage_T *tp, int id)
1424{
1425 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001426 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001427 win_T *prev = NULL;
1428
Bram Moolenaarec583842019-05-26 14:11:23 +02001429 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1430 if (wp->w_id == id)
1431 {
1432 if (prev == NULL)
1433 *root = wp->w_next;
1434 else
1435 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001436 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001437 return;
1438 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001439}
1440
1441 void
1442close_all_popups(void)
1443{
1444 while (first_popupwin != NULL)
1445 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001446 while (curtab->tp_first_popupwin != NULL)
1447 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001448}
1449
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001450/*
1451 * popup_move({id}, {options})
1452 */
1453 void
1454f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1455{
Bram Moolenaarae943152019-06-16 22:54:14 +02001456 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001457 int id = (int)tv_get_number(argvars);
1458 win_T *wp = find_popup_win(id);
1459
1460 if (wp == NULL)
1461 return; // invalid {id}
1462
1463 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1464 {
1465 emsg(_(e_dictreq));
1466 return;
1467 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001468 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001469
Bram Moolenaarae943152019-06-16 22:54:14 +02001470 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001471
1472 if (wp->w_winrow + wp->w_height >= cmdline_row)
1473 clear_cmdline = TRUE;
1474 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001475}
1476
Bram Moolenaarbc133542019-05-29 20:26:46 +02001477/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001478 * popup_setoptions({id}, {options})
1479 */
1480 void
1481f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1482{
1483 dict_T *dict;
1484 int id = (int)tv_get_number(argvars);
1485 win_T *wp = find_popup_win(id);
1486
1487 if (wp == NULL)
1488 return; // invalid {id}
1489
1490 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1491 {
1492 emsg(_(e_dictreq));
1493 return;
1494 }
1495 dict = argvars[1].vval.v_dict;
1496
1497 apply_move_options(wp, dict);
1498 apply_general_options(wp, dict);
1499
Bram Moolenaarad24a712019-06-17 20:05:45 +02001500 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001501 popup_adjust_position(wp);
1502}
1503
1504/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001505 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001506 */
1507 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001508f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001509{
1510 dict_T *dict;
1511 int id = (int)tv_get_number(argvars);
1512 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001513 int top_extra;
1514 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001515
1516 if (rettv_dict_alloc(rettv) == OK)
1517 {
1518 if (wp == NULL)
1519 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001520 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001521 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1522
Bram Moolenaarbc133542019-05-29 20:26:46 +02001523 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001524
Bram Moolenaarbc133542019-05-29 20:26:46 +02001525 dict_add_number(dict, "line", wp->w_winrow + 1);
1526 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001527 dict_add_number(dict, "width", wp->w_width + left_extra
1528 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1529 dict_add_number(dict, "height", wp->w_height + top_extra
1530 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001531
1532 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1533 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1534 dict_add_number(dict, "core_width", wp->w_width);
1535 dict_add_number(dict, "core_height", wp->w_height);
1536
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001537 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001538 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001539 }
1540}
1541
1542/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001543 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1544 */
1545 static void
1546get_padding_border(dict_T *dict, int *array, char *name)
1547{
1548 list_T *list;
1549 int i;
1550
1551 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1552 return;
1553
1554 list = list_alloc();
1555 if (list != NULL)
1556 {
1557 dict_add_list(dict, name, list);
1558 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1559 for (i = 0; i < 4; ++i)
1560 list_append_number(list, array[i]);
1561 }
1562}
1563
1564/*
1565 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1566 */
1567 static void
1568get_borderhighlight(dict_T *dict, win_T *wp)
1569{
1570 list_T *list;
1571 int i;
1572
1573 for (i = 0; i < 4; ++i)
1574 if (wp->w_border_highlight[i] != NULL)
1575 break;
1576 if (i == 4)
1577 return;
1578
1579 list = list_alloc();
1580 if (list != NULL)
1581 {
1582 dict_add_list(dict, "borderhighlight", list);
1583 for (i = 0; i < 4; ++i)
1584 list_append_string(list, wp->w_border_highlight[i], -1);
1585 }
1586}
1587
1588/*
1589 * For popup_getoptions(): add a "borderchars" entry to "dict".
1590 */
1591 static void
1592get_borderchars(dict_T *dict, win_T *wp)
1593{
1594 list_T *list;
1595 int i;
1596 char_u buf[NUMBUFLEN];
1597 int len;
1598
1599 for (i = 0; i < 8; ++i)
1600 if (wp->w_border_char[i] != 0)
1601 break;
1602 if (i == 8)
1603 return;
1604
1605 list = list_alloc();
1606 if (list != NULL)
1607 {
1608 dict_add_list(dict, "borderchars", list);
1609 for (i = 0; i < 8; ++i)
1610 {
1611 len = mb_char2bytes(wp->w_border_char[i], buf);
1612 list_append_string(list, buf, len);
1613 }
1614 }
1615}
1616
1617/*
1618 * For popup_getoptions(): add a "moved" entry to "dict".
1619 */
1620 static void
1621get_moved_list(dict_T *dict, win_T *wp)
1622{
1623 list_T *list;
1624
1625 list = list_alloc();
1626 if (list != NULL)
1627 {
1628 dict_add_list(dict, "moved", list);
1629 list_append_number(list, wp->w_popup_mincol);
1630 list_append_number(list, wp->w_popup_maxcol);
1631 }
1632}
1633
1634/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001635 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001636 */
1637 void
1638f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1639{
1640 dict_T *dict;
1641 int id = (int)tv_get_number(argvars);
1642 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001643 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001644 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001645
1646 if (rettv_dict_alloc(rettv) == OK)
1647 {
1648 if (wp == NULL)
1649 return;
1650
1651 dict = rettv->vval.v_dict;
1652 dict_add_number(dict, "line", wp->w_wantline);
1653 dict_add_number(dict, "col", wp->w_wantcol);
1654 dict_add_number(dict, "minwidth", wp->w_minwidth);
1655 dict_add_number(dict, "minheight", wp->w_minheight);
1656 dict_add_number(dict, "maxheight", wp->w_maxheight);
1657 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001658 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001659 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001660 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001661 dict_add_string(dict, "title", wp->w_popup_title);
1662 dict_add_number(dict, "wrap", wp->w_p_wrap);
1663 dict_add_number(dict, "drag", wp->w_popup_drag);
1664 dict_add_string(dict, "highlight", wp->w_p_wcr);
1665
Bram Moolenaara3fce622019-06-20 02:31:49 +02001666 // find the tabpage that holds this popup
1667 i = 1;
1668 FOR_ALL_TABPAGES(tp)
1669 {
1670 win_T *p;
1671
1672 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1673 if (p->w_id == id)
1674 break;
1675 if (p != NULL)
1676 break;
1677 ++i;
1678 }
1679 if (tp == NULL)
1680 i = -1; // must be global
1681 else if (tp == curtab)
1682 i = 0;
1683 dict_add_number(dict, "tabpage", i);
1684
Bram Moolenaarae943152019-06-16 22:54:14 +02001685 get_padding_border(dict, wp->w_popup_padding, "padding");
1686 get_padding_border(dict, wp->w_popup_border, "border");
1687 get_borderhighlight(dict, wp);
1688 get_borderchars(dict, wp);
1689 get_moved_list(dict, wp);
1690
1691 if (wp->w_filter_cb.cb_name != NULL)
1692 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1693 if (wp->w_close_cb.cb_name != NULL)
1694 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001695
1696 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1697 ++i)
1698 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1699 {
1700 dict_add_string(dict, "pos",
1701 (char_u *)poppos_entries[i].pp_name);
1702 break;
1703 }
1704
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001705# if defined(FEAT_TIMERS)
1706 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1707 ? (long)wp->w_popup_timer->tr_interval : 0L);
1708# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001709 }
1710}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001711
1712 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001713error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001714{
1715 if (bt_popup(curwin->w_buffer))
1716 {
1717 emsg(_("E994: Not allowed in a popup window"));
1718 return TRUE;
1719 }
1720 return FALSE;
1721}
1722
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001723/*
1724 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001725 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001726 */
1727 void
1728popup_reset_handled()
1729{
1730 win_T *wp;
1731
1732 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1733 wp->w_popup_flags &= ~POPF_HANDLED;
1734 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1735 wp->w_popup_flags &= ~POPF_HANDLED;
1736}
1737
1738/*
1739 * Find the next visible popup where POPF_HANDLED is not set.
1740 * Must have called popup_reset_handled() first.
1741 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1742 * popup with the highest zindex.
1743 */
1744 win_T *
1745find_next_popup(int lowest)
1746{
1747 win_T *wp;
1748 win_T *found_wp;
1749 int found_zindex;
1750
1751 found_zindex = lowest ? INT_MAX : 0;
1752 found_wp = NULL;
1753 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1754 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1755 && (lowest ? wp->w_zindex < found_zindex
1756 : wp->w_zindex > found_zindex))
1757 {
1758 found_zindex = wp->w_zindex;
1759 found_wp = wp;
1760 }
1761 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1762 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1763 && (lowest ? wp->w_zindex < found_zindex
1764 : wp->w_zindex > found_zindex))
1765 {
1766 found_zindex = wp->w_zindex;
1767 found_wp = wp;
1768 }
1769
1770 if (found_wp != NULL)
1771 found_wp->w_popup_flags |= POPF_HANDLED;
1772 return found_wp;
1773}
1774
1775/*
1776 * Invoke the filter callback for window "wp" with typed character "c".
1777 * Uses the global "mod_mask" for modifiers.
1778 * Returns the return value of the filter.
1779 * Careful: The filter may make "wp" invalid!
1780 */
1781 static int
1782invoke_popup_filter(win_T *wp, int c)
1783{
1784 int res;
1785 typval_T rettv;
1786 int dummy;
1787 typval_T argv[3];
1788 char_u buf[NUMBUFLEN];
1789
Bram Moolenaara42d9452019-06-15 21:46:30 +02001790 // Emergency exit: CTRL-C closes the popup.
1791 if (c == Ctrl_C)
1792 {
1793 rettv.v_type = VAR_NUMBER;
1794 rettv.vval.v_number = -1;
1795 popup_close_and_callback(wp, &rettv);
1796 return 1;
1797 }
1798
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001799 argv[0].v_type = VAR_NUMBER;
1800 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1801
1802 // Convert the number to a string, so that the function can use:
1803 // if a:c == "\<F2>"
1804 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1805 argv[1].v_type = VAR_STRING;
1806 argv[1].vval.v_string = vim_strsave(buf);
1807
1808 argv[2].v_type = VAR_UNKNOWN;
1809
Bram Moolenaara42d9452019-06-15 21:46:30 +02001810 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001811 call_callback(&wp->w_filter_cb, -1,
1812 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1813 res = tv_get_number(&rettv);
1814 vim_free(argv[1].vval.v_string);
1815 clear_tv(&rettv);
1816 return res;
1817}
1818
1819/*
1820 * Called when "c" was typed: invoke popup filter callbacks.
1821 * Returns TRUE when the character was consumed,
1822 */
1823 int
1824popup_do_filter(int c)
1825{
1826 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001827 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001828
1829 popup_reset_handled();
1830
1831 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1832 if (wp->w_filter_cb.cb_name != NULL)
1833 res = invoke_popup_filter(wp, c);
1834
1835 return res;
1836}
1837
Bram Moolenaar3397f742019-06-02 18:40:06 +02001838/*
1839 * Called when the cursor moved: check if any popup needs to be closed if the
1840 * cursor moved far enough.
1841 */
1842 void
1843popup_check_cursor_pos()
1844{
1845 win_T *wp;
1846 typval_T tv;
1847
1848 popup_reset_handled();
1849 while ((wp = find_next_popup(TRUE)) != NULL)
1850 if (wp->w_popup_curwin != NULL
1851 && (curwin != wp->w_popup_curwin
1852 || curwin->w_cursor.lnum != wp->w_popup_lnum
1853 || curwin->w_cursor.col < wp->w_popup_mincol
1854 || curwin->w_cursor.col > wp->w_popup_maxcol))
1855 {
1856 tv.v_type = VAR_NUMBER;
1857 tv.vval.v_number = -1;
1858 popup_close_and_callback(wp, &tv);
1859 }
1860}
1861
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001862/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001863 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
1864 * "col" and "line" are screen coordinates.
1865 */
1866 static int
1867popup_masked(win_T *wp, int screencol, int screenline)
1868{
1869 int col = screencol - wp->w_wincol + 1;
1870 int line = screenline - wp->w_winrow + 1;
1871 listitem_T *lio, *li;
1872 int width, height;
1873
1874 if (wp->w_popup_mask == NULL)
1875 return FALSE;
1876 width = popup_width(wp);
1877 height = popup_height(wp);
1878
1879 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1880 {
1881 int cols, cole;
1882 int lines, linee;
1883
1884 li = lio->li_tv.vval.v_list->lv_first;
1885 cols = tv_get_number(&li->li_tv);
1886 if (cols < 0)
1887 cols = width + cols + 1;
1888 if (col < cols)
1889 continue;
1890 li = li->li_next;
1891 cole = tv_get_number(&li->li_tv);
1892 if (cole < 0)
1893 cole = width + cole + 1;
1894 if (col > cole)
1895 continue;
1896 li = li->li_next;
1897 lines = tv_get_number(&li->li_tv);
1898 if (lines < 0)
1899 lines = height + lines + 1;
1900 if (line < lines)
1901 continue;
1902 li = li->li_next;
1903 linee = tv_get_number(&li->li_tv);
1904 if (linee < 0)
1905 linee = height + linee + 1;
1906 if (line > linee)
1907 continue;
1908 return TRUE;
1909 }
1910 return FALSE;
1911}
1912
1913/*
1914 * Set flags in popup_transparent[] for window "wp" to "val".
1915 */
1916 static void
1917update_popup_transparent(win_T *wp, int val)
1918{
1919 if (wp->w_popup_mask != NULL)
1920 {
1921 int width = popup_width(wp);
1922 int height = popup_height(wp);
1923 listitem_T *lio, *li;
1924 int cols, cole;
1925 int lines, linee;
1926 int col, line;
1927
1928 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1929 {
1930 li = lio->li_tv.vval.v_list->lv_first;
1931 cols = tv_get_number(&li->li_tv);
1932 if (cols < 0)
1933 cols = width + cols + 1;
1934 li = li->li_next;
1935 cole = tv_get_number(&li->li_tv);
1936 if (cole < 0)
1937 cole = width + cole + 1;
1938 li = li->li_next;
1939 lines = tv_get_number(&li->li_tv);
1940 if (lines < 0)
1941 lines = height + lines + 1;
1942 li = li->li_next;
1943 linee = tv_get_number(&li->li_tv);
1944 if (linee < 0)
1945 linee = height + linee + 1;
1946
1947 --cols;
1948 --lines;
1949 for (line = lines; line < linee && line < screen_Rows; ++line)
1950 for (col = cols; col < cole && col < screen_Columns; ++col)
1951 popup_transparent[(line + wp->w_winrow) * screen_Columns
1952 + col + wp->w_wincol] = val;
1953 }
1954 }
1955}
1956
1957/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001958 * Update "popup_mask" if needed.
1959 * Also recomputes the popup size and positions.
1960 * Also updates "popup_visible".
1961 * Also marks window lines for redrawing.
1962 */
1963 void
1964may_update_popup_mask(int type)
1965{
1966 win_T *wp;
1967 short *mask;
1968 int line, col;
1969 int redraw_all = FALSE;
1970
1971 // Need to recompute when switching tabs.
1972 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1973 // (such as the screen size) must have changed.
1974 if (popup_mask_tab != curtab || type >= NOT_VALID)
1975 {
1976 popup_mask_refresh = TRUE;
1977 redraw_all = TRUE;
1978 }
1979 if (!popup_mask_refresh)
1980 {
1981 // Check if any buffer has changed.
1982 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1983 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1984 popup_mask_refresh = TRUE;
1985 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1986 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1987 popup_mask_refresh = TRUE;
1988 if (!popup_mask_refresh)
1989 return;
1990 }
1991
1992 // Need to update the mask, something has changed.
1993 popup_mask_refresh = FALSE;
1994 popup_mask_tab = curtab;
1995 popup_visible = FALSE;
1996
1997 // If redrawing everything, just update "popup_mask".
1998 // If redrawing only what is needed, update "popup_mask_next" and then
1999 // compare with "popup_mask" to see what changed.
2000 if (type >= SOME_VALID)
2001 mask = popup_mask;
2002 else
2003 mask = popup_mask_next;
2004 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2005
2006 // Find the window with the lowest zindex that hasn't been handled yet,
2007 // so that the window with a higher zindex overwrites the value in
2008 // popup_mask.
2009 popup_reset_handled();
2010 while ((wp = find_next_popup(TRUE)) != NULL)
2011 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002012 int height = popup_height(wp);
2013 int width = popup_width(wp);
2014
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002015 popup_visible = TRUE;
2016
2017 // Recompute the position if the text changed.
2018 if (redraw_all
2019 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2020 popup_adjust_position(wp);
2021
2022 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002023 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002024 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002025 col < wp->w_wincol + width && col < screen_Columns; ++col)
2026 if (!popup_masked(wp, col, line))
2027 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002028 }
2029
2030 // Only check which lines are to be updated if not already
2031 // updating all lines.
2032 if (mask == popup_mask_next)
2033 for (line = 0; line < screen_Rows; ++line)
2034 {
2035 int col_done = 0;
2036
2037 for (col = 0; col < screen_Columns; ++col)
2038 {
2039 int off = line * screen_Columns + col;
2040
2041 if (popup_mask[off] != popup_mask_next[off])
2042 {
2043 popup_mask[off] = popup_mask_next[off];
2044
2045 if (line >= cmdline_row)
2046 {
2047 // the command line needs to be cleared if text below
2048 // the popup is now visible.
2049 if (!msg_scrolled && popup_mask_next[off] == 0)
2050 clear_cmdline = TRUE;
2051 }
2052 else if (col >= col_done)
2053 {
2054 linenr_T lnum;
2055 int line_cp = line;
2056 int col_cp = col;
2057
2058 // The screen position "line" / "col" needs to be
2059 // redrawn. Figure out what window that is and update
2060 // w_redraw_top and w_redr_bot. Only needs to be done
2061 // once for each window line.
2062 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2063 if (wp != NULL)
2064 {
2065 if (line_cp >= wp->w_height)
2066 // In (or below) status line
2067 wp->w_redr_status = TRUE;
2068 // compute the position in the buffer line from the
2069 // position on the screen
2070 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2071 &lnum))
2072 // past bottom
2073 wp->w_redr_status = TRUE;
2074 else
2075 redrawWinline(wp, lnum);
2076
2077 // This line is going to be redrawn, no need to
2078 // check until the right side of the window.
2079 col_done = wp->w_wincol + wp->w_width - 1;
2080 }
2081 }
2082 }
2083 }
2084 }
2085}
2086
2087/*
2088 * Return a string of "len" spaces in IObuff.
2089 */
2090 static char_u *
2091get_spaces(int len)
2092{
2093 vim_memset(IObuff, ' ', (size_t)len);
2094 IObuff[len] = NUL;
2095 return IObuff;
2096}
2097
2098/*
2099 * Update popup windows. They are drawn on top of normal windows.
2100 * "win_update" is called for each popup window, lowest zindex first.
2101 */
2102 void
2103update_popups(void (*win_update)(win_T *wp))
2104{
2105 win_T *wp;
2106 int top_off;
2107 int left_off;
2108 int total_width;
2109 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002110 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002111 int popup_attr;
2112 int border_attr[4];
2113 int border_char[8];
2114 char_u buf[MB_MAXBYTES];
2115 int row;
2116 int i;
2117
2118 // Find the window with the lowest zindex that hasn't been updated yet,
2119 // so that the window with a higher zindex is drawn later, thus goes on
2120 // top.
2121 popup_reset_handled();
2122 while ((wp = find_next_popup(TRUE)) != NULL)
2123 {
2124 // This drawing uses the zindex of the popup window, so that it's on
2125 // top of the text but doesn't draw when another popup with higher
2126 // zindex is on top of the character.
2127 screen_zindex = wp->w_zindex;
2128
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002129 // Set flags in popup_transparent[] for masked cells.
2130 update_popup_transparent(wp, 1);
2131
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002132 // adjust w_winrow and w_wincol for border and padding, since
2133 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002134 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002135 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2136 wp->w_winrow += top_off;
2137 wp->w_wincol += left_off;
2138
2139 // Draw the popup text.
2140 win_update(wp);
2141
2142 wp->w_winrow -= top_off;
2143 wp->w_wincol -= left_off;
2144
2145 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
2146 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002147 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002148 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2149 popup_attr = get_wcr_attr(wp);
2150
2151 // We can only use these line drawing characters when 'encoding' is
2152 // "utf-8" and 'ambiwidth' is "single".
2153 if (enc_utf8 && *p_ambw == 's')
2154 {
2155 border_char[0] = border_char[2] = 0x2550;
2156 border_char[1] = border_char[3] = 0x2551;
2157 border_char[4] = 0x2554;
2158 border_char[5] = 0x2557;
2159 border_char[6] = 0x255d;
2160 border_char[7] = 0x255a;
2161 }
2162 else
2163 {
2164 border_char[0] = border_char[2] = '-';
2165 border_char[1] = border_char[3] = '|';
2166 for (i = 4; i < 8; ++i)
2167 border_char[i] = '+';
2168 }
2169 for (i = 0; i < 8; ++i)
2170 if (wp->w_border_char[i] != 0)
2171 border_char[i] = wp->w_border_char[i];
2172
2173 for (i = 0; i < 4; ++i)
2174 {
2175 border_attr[i] = popup_attr;
2176 if (wp->w_border_highlight[i] != NULL)
2177 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2178 }
2179
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002180 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002181 if (wp->w_popup_border[0] > 0)
2182 {
2183 // top border
2184 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2185 wp->w_wincol,
2186 wp->w_wincol + total_width,
2187 wp->w_popup_border[3] != 0
2188 ? border_char[4] : border_char[0],
2189 border_char[0], border_attr[0]);
2190 if (wp->w_popup_border[1] > 0)
2191 {
2192 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2193 screen_puts(buf, wp->w_winrow,
2194 wp->w_wincol + total_width - 1, border_attr[1]);
2195 }
2196 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002197 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2198 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002199
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002200 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002201 {
2202 // top padding
2203 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002204 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002205 wp->w_wincol + wp->w_popup_border[3],
2206 wp->w_wincol + total_width - wp->w_popup_border[1],
2207 ' ', ' ', popup_attr);
2208 }
2209
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002210 // Title goes on top of border or padding.
2211 if (wp->w_popup_title != NULL)
2212 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2213 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2214
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002215 for (row = wp->w_winrow + wp->w_popup_border[0];
2216 row < wp->w_winrow + total_height - wp->w_popup_border[2];
2217 ++row)
2218 {
2219 // left border
2220 if (wp->w_popup_border[3] > 0)
2221 {
2222 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2223 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2224 }
2225 // left padding
2226 if (wp->w_popup_padding[3] > 0)
2227 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2228 wp->w_wincol + wp->w_popup_border[3], popup_attr);
2229 // right border
2230 if (wp->w_popup_border[1] > 0)
2231 {
2232 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2233 screen_puts(buf, row,
2234 wp->w_wincol + total_width - 1, border_attr[1]);
2235 }
2236 // right padding
2237 if (wp->w_popup_padding[1] > 0)
2238 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2239 wp->w_wincol + wp->w_popup_border[3]
2240 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2241 }
2242
2243 if (wp->w_popup_padding[2] > 0)
2244 {
2245 // bottom padding
2246 row = wp->w_winrow + wp->w_popup_border[0]
2247 + wp->w_popup_padding[0] + wp->w_height;
2248 screen_fill(row, row + wp->w_popup_padding[2],
2249 wp->w_wincol + wp->w_popup_border[3],
2250 wp->w_wincol + total_width - wp->w_popup_border[1],
2251 ' ', ' ', popup_attr);
2252 }
2253
2254 if (wp->w_popup_border[2] > 0)
2255 {
2256 // bottom border
2257 row = wp->w_winrow + total_height - 1;
2258 screen_fill(row , row + 1,
2259 wp->w_wincol,
2260 wp->w_wincol + total_width,
2261 wp->w_popup_border[3] != 0
2262 ? border_char[7] : border_char[2],
2263 border_char[2], border_attr[2]);
2264 if (wp->w_popup_border[1] > 0)
2265 {
2266 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2267 screen_puts(buf, row,
2268 wp->w_wincol + total_width - 1, border_attr[2]);
2269 }
2270 }
2271
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002272 update_popup_transparent(wp, 0);
2273
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002274 // Back to the normal zindex.
2275 screen_zindex = 0;
2276 }
2277}
2278
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002279/*
2280 * Mark references in callbacks of one popup window.
2281 */
2282 static int
2283set_ref_in_one_popup(win_T *wp, int copyID)
2284{
2285 int abort = FALSE;
2286 typval_T tv;
2287
2288 if (wp->w_close_cb.cb_partial != NULL)
2289 {
2290 tv.v_type = VAR_PARTIAL;
2291 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2292 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2293 }
2294 if (wp->w_filter_cb.cb_partial != NULL)
2295 {
2296 tv.v_type = VAR_PARTIAL;
2297 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2298 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2299 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002300 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002301 return abort;
2302}
2303
2304/*
2305 * Set reference in callbacks of popup windows.
2306 */
2307 int
2308set_ref_in_popups(int copyID)
2309{
2310 int abort = FALSE;
2311 win_T *wp;
2312 tabpage_T *tp;
2313
2314 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2315 abort = abort || set_ref_in_one_popup(wp, copyID);
2316
2317 FOR_ALL_TABPAGES(tp)
2318 {
2319 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2320 abort = abort || set_ref_in_one_popup(wp, copyID);
2321 if (abort)
2322 break;
2323 }
2324 return abort;
2325}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002326#endif // FEAT_TEXT_PROP