blob: 5611cba6b7fb1a1ee4cbd821452485655c8ef753 [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 Moolenaar75fb0852019-06-25 05:15:58 +0200290 di = dict_find(dict, (char_u *)"scrollbar", -1);
291 if (di != NULL)
292 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
293
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200294 str = dict_get_string(dict, (char_u *)"title", FALSE);
295 if (str != NULL)
296 {
297 vim_free(wp->w_popup_title);
298 wp->w_popup_title = vim_strsave(str);
299 }
300
Bram Moolenaar402502d2019-05-30 22:07:36 +0200301 di = dict_find(dict, (char_u *)"wrap", -1);
302 if (di != NULL)
303 {
304 nr = dict_get_number(dict, (char_u *)"wrap");
305 wp->w_p_wrap = nr != 0;
306 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200307
Bram Moolenaara42d9452019-06-15 21:46:30 +0200308 di = dict_find(dict, (char_u *)"drag", -1);
309 if (di != NULL)
310 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200311
Bram Moolenaarae943152019-06-16 22:54:14 +0200312 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
313 if (str != NULL)
314 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
315 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200316
Bram Moolenaarae943152019-06-16 22:54:14 +0200317 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
318 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200319
Bram Moolenaar790498b2019-06-01 22:15:29 +0200320 di = dict_find(dict, (char_u *)"borderhighlight", -1);
321 if (di != NULL)
322 {
323 if (di->di_tv.v_type != VAR_LIST)
324 emsg(_(e_listreq));
325 else
326 {
327 list_T *list = di->di_tv.vval.v_list;
328 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200329 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200330
331 if (list != NULL)
332 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
333 ++i, li = li->li_next)
334 {
335 str = tv_get_string(&li->li_tv);
336 if (*str != NUL)
337 wp->w_border_highlight[i] = vim_strsave(str);
338 }
339 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
340 for (i = 1; i < 4; ++i)
341 wp->w_border_highlight[i] =
342 vim_strsave(wp->w_border_highlight[0]);
343 }
344 }
345
Bram Moolenaar790498b2019-06-01 22:15:29 +0200346 di = dict_find(dict, (char_u *)"borderchars", -1);
347 if (di != NULL)
348 {
349 if (di->di_tv.v_type != VAR_LIST)
350 emsg(_(e_listreq));
351 else
352 {
353 list_T *list = di->di_tv.vval.v_list;
354 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200355 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200356
357 if (list != NULL)
358 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
359 ++i, li = li->li_next)
360 {
361 str = tv_get_string(&li->li_tv);
362 if (*str != NUL)
363 wp->w_border_char[i] = mb_ptr2char(str);
364 }
365 if (list->lv_len == 1)
366 for (i = 1; i < 8; ++i)
367 wp->w_border_char[i] = wp->w_border_char[0];
368 if (list->lv_len == 2)
369 {
370 for (i = 4; i < 8; ++i)
371 wp->w_border_char[i] = wp->w_border_char[1];
372 for (i = 1; i < 4; ++i)
373 wp->w_border_char[i] = wp->w_border_char[0];
374 }
375 }
376 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200377
Bram Moolenaarae943152019-06-16 22:54:14 +0200378 di = dict_find(dict, (char_u *)"zindex", -1);
379 if (di != NULL)
380 {
381 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
382 if (wp->w_zindex < 1)
383 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
384 if (wp->w_zindex > 32000)
385 wp->w_zindex = 32000;
386 }
387
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200388 di = dict_find(dict, (char_u *)"mask", -1);
389 if (di != NULL)
390 {
391 int ok = TRUE;
392
393 if (di->di_tv.v_type != VAR_LIST)
394 ok = FALSE;
395 else if (di->di_tv.vval.v_list != NULL)
396 {
397 listitem_T *li;
398
399 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
400 li = li->li_next)
401 {
402 if (li->li_tv.v_type != VAR_LIST
403 || li->li_tv.vval.v_list == NULL
404 || li->li_tv.vval.v_list->lv_len != 4)
405 {
406 ok = FALSE;
407 break;
408 }
409 }
410 }
411 if (ok)
412 {
413 wp->w_popup_mask = di->di_tv.vval.v_list;
414 ++wp->w_popup_mask->lv_refcount;
415 }
416 else
417 semsg(_(e_invargval), "mask");
418 }
419
Bram Moolenaarae943152019-06-16 22:54:14 +0200420#if defined(FEAT_TIMERS)
421 // Add timer to close the popup after some time.
422 nr = dict_get_number(dict, (char_u *)"time");
423 if (nr > 0)
424 popup_add_timeout(wp, nr);
425#endif
426
Bram Moolenaar3397f742019-06-02 18:40:06 +0200427 di = dict_find(dict, (char_u *)"moved", -1);
428 if (di != NULL)
429 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200430 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200431 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
432 {
433 char_u *s = di->di_tv.vval.v_string;
434 int flags = 0;
435
436 if (STRCMP(s, "word") == 0)
437 flags = FIND_IDENT | FIND_STRING;
438 else if (STRCMP(s, "WORD") == 0)
439 flags = FIND_STRING;
440 else if (STRCMP(s, "any") != 0)
441 semsg(_(e_invarg2), s);
442 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200443 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200444 }
445 else if (di->di_tv.v_type == VAR_LIST
446 && di->di_tv.vval.v_list != NULL
447 && di->di_tv.vval.v_list->lv_len == 2)
448 {
449 list_T *l = di->di_tv.vval.v_list;
450
451 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
452 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
453 }
454 else
455 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
456 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200457
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 di = dict_find(dict, (char_u *)"filter", -1);
459 if (di != NULL)
460 {
461 callback_T callback = get_callback(&di->di_tv);
462
463 if (callback.cb_name != NULL)
464 {
465 free_callback(&wp->w_filter_cb);
466 set_callback(&wp->w_filter_cb, &callback);
467 }
468 }
469
470 di = dict_find(dict, (char_u *)"callback", -1);
471 if (di != NULL)
472 {
473 callback_T callback = get_callback(&di->di_tv);
474
475 if (callback.cb_name != NULL)
476 {
477 free_callback(&wp->w_close_cb);
478 set_callback(&wp->w_close_cb, &callback);
479 }
480 }
481}
482
483/*
484 * Go through the options in "dict" and apply them to popup window "wp".
485 */
486 static void
487apply_options(win_T *wp, dict_T *dict)
488{
489 int nr;
490
491 apply_move_options(wp, dict);
492 apply_general_options(wp, dict);
493
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200494 nr = dict_get_number(dict, (char_u *)"hidden");
495 if (nr > 0)
496 {
497 wp->w_popup_flags |= POPF_HIDDEN;
498 --wp->w_buffer->b_nwindows;
499 }
500
Bram Moolenaar33796b32019-06-08 16:01:13 +0200501 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200502}
503
504/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200505 * Add lines to the popup from a list of strings.
506 */
507 static void
508add_popup_strings(buf_T *buf, list_T *l)
509{
510 listitem_T *li;
511 linenr_T lnum = 0;
512 char_u *p;
513
514 for (li = l->lv_first; li != NULL; li = li->li_next)
515 if (li->li_tv.v_type == VAR_STRING)
516 {
517 p = li->li_tv.vval.v_string;
518 ml_append_buf(buf, lnum++,
519 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
520 }
521}
522
523/*
524 * Add lines to the popup from a list of dictionaries.
525 */
526 static void
527add_popup_dicts(buf_T *buf, list_T *l)
528{
529 listitem_T *li;
530 listitem_T *pli;
531 linenr_T lnum = 0;
532 char_u *p;
533 dict_T *dict;
534
535 // first add the text lines
536 for (li = l->lv_first; li != NULL; li = li->li_next)
537 {
538 if (li->li_tv.v_type != VAR_DICT)
539 {
540 emsg(_(e_dictreq));
541 return;
542 }
543 dict = li->li_tv.vval.v_dict;
544 p = dict == NULL ? NULL
545 : dict_get_string(dict, (char_u *)"text", FALSE);
546 ml_append_buf(buf, lnum++,
547 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
548 }
549
550 // add the text properties
551 lnum = 1;
552 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
553 {
554 dictitem_T *di;
555 list_T *plist;
556
557 dict = li->li_tv.vval.v_dict;
558 di = dict_find(dict, (char_u *)"props", -1);
559 if (di != NULL)
560 {
561 if (di->di_tv.v_type != VAR_LIST)
562 {
563 emsg(_(e_listreq));
564 return;
565 }
566 plist = di->di_tv.vval.v_list;
567 if (plist != NULL)
568 {
569 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
570 {
571 if (pli->li_tv.v_type != VAR_DICT)
572 {
573 emsg(_(e_dictreq));
574 return;
575 }
576 dict = pli->li_tv.vval.v_dict;
577 if (dict != NULL)
578 {
579 int col = dict_get_number(dict, (char_u *)"col");
580
581 prop_add_common( lnum, col, dict, buf, NULL);
582 }
583 }
584 }
585 }
586 }
587}
588
589/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200590 * Return the height of popup window "wp", including border and padding.
591 */
592 int
593popup_height(win_T *wp)
594{
595 return wp->w_height
596 + wp->w_popup_padding[0] + wp->w_popup_border[0]
597 + wp->w_popup_padding[2] + wp->w_popup_border[2];
598}
599
600/*
601 * Return the width of popup window "wp", including border and padding.
602 */
603 int
604popup_width(win_T *wp)
605{
606 return wp->w_width
607 + wp->w_popup_padding[3] + wp->w_popup_border[3]
608 + wp->w_popup_padding[1] + wp->w_popup_border[1];
609}
610
611/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200612 * Get the padding plus border at the top, adjusted to 1 if there is a title.
613 */
614 static int
615popup_top_extra(win_T *wp)
616{
617 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
618
619 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
620 return 1;
621 return extra;
622}
623
624/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200625 * Adjust the position and size of the popup to fit on the screen.
626 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200627 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200628popup_adjust_position(win_T *wp)
629{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200630 linenr_T lnum;
631 int wrapped = 0;
632 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200633 int center_vert = FALSE;
634 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200635 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200636 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200637 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
638 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
639 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
640 int extra_height = top_extra + bot_extra;
641 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200642 int org_winrow = wp->w_winrow;
643 int org_wincol = wp->w_wincol;
644 int org_width = wp->w_width;
645 int org_height = wp->w_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200646 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200647
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200648 wp->w_winrow = 0;
649 wp->w_wincol = 0;
650 if (wp->w_popup_pos == POPPOS_CENTER)
651 {
652 // center after computing the size
653 center_vert = TRUE;
654 center_hor = TRUE;
655 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200656 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200657 {
658 if (wp->w_wantline == 0)
659 center_vert = TRUE;
660 else if (wp->w_popup_pos == POPPOS_TOPLEFT
661 || wp->w_popup_pos == POPPOS_TOPRIGHT)
662 {
663 wp->w_winrow = wp->w_wantline - 1;
664 if (wp->w_winrow >= Rows)
665 wp->w_winrow = Rows - 1;
666 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200667
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200668 if (wp->w_wantcol == 0)
669 center_hor = TRUE;
670 else if (wp->w_popup_pos == POPPOS_TOPLEFT
671 || wp->w_popup_pos == POPPOS_BOTLEFT)
672 {
673 wp->w_wincol = wp->w_wantcol - 1;
674 if (wp->w_wincol >= Columns - 3)
675 wp->w_wincol = Columns - 3;
676 }
677 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200678
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200679 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200680 // When left aligned use the space available, but shift to the left when we
681 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200682 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200683 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200684 {
685 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200686 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200687 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200688
Bram Moolenaar8d241042019-06-12 23:40:01 +0200689 // start at the desired first line
690 wp->w_topline = wp->w_firstline;
691 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
692 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
693
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200694 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200695 // Use a minimum width of one, so that something shows when there is no
696 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200697 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200698 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200699 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200700 {
701 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
702
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200703 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200704 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200705 while (len > maxwidth)
706 {
707 ++wrapped;
708 len -= maxwidth;
709 wp->w_width = maxwidth;
710 }
711 }
712 else if (len > maxwidth
713 && allow_adjust_left
714 && (wp->w_popup_pos == POPPOS_TOPLEFT
715 || wp->w_popup_pos == POPPOS_BOTLEFT))
716 {
717 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200718 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200719
Bram Moolenaar51c31312019-06-15 22:27:23 +0200720 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200721 {
722 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200723
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200724 len -= truncate_shift;
725 shift_by -= truncate_shift;
726 }
727
728 wp->w_wincol -= shift_by;
729 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200730 wp->w_width = maxwidth;
731 }
732 if (wp->w_width < len)
733 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200734 // do not use the width of lines we're not going to show
735 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
736 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
737 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200738 }
739
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200740 wp->w_has_scrollbar = wp->w_want_scrollbar
741 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
742
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200743 minwidth = wp->w_minwidth;
744 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
745 {
746 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
747
748 if (minwidth < title_len)
749 minwidth = title_len;
750 }
751
752 if (minwidth > 0 && wp->w_width < minwidth)
753 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200754 if (wp->w_width > maxwidth)
755 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200756 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200757 {
758 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
759 if (wp->w_wincol < 0)
760 wp->w_wincol = 0;
761 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200762 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
763 || wp->w_popup_pos == POPPOS_TOPRIGHT)
764 {
765 // Right aligned: move to the right if needed.
766 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200767 if (wp->w_width + extra_width < wp->w_wantcol)
768 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200769 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200770
Bram Moolenaar8d241042019-06-12 23:40:01 +0200771 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
772 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200773 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
774 wp->w_height = wp->w_minheight;
775 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
776 wp->w_height = wp->w_maxheight;
777 if (wp->w_height > Rows - wp->w_winrow)
778 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200779
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200780 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200781 {
782 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
783 if (wp->w_winrow < 0)
784 wp->w_winrow = 0;
785 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200786 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
787 || wp->w_popup_pos == POPPOS_BOTLEFT)
788 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200789 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200790 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200791 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200792 else
793 // not enough space, make top aligned
794 wp->w_winrow = wp->w_wantline + 1;
795 }
796
Bram Moolenaar17146962019-05-30 00:12:11 +0200797 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200798
799 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200800 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200801 if (org_winrow != wp->w_winrow
802 || org_wincol != wp->w_wincol
803 || org_width != wp->w_width
804 || org_height != wp->w_height)
805 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200806 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200807 popup_mask_refresh = TRUE;
808 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200809}
810
Bram Moolenaar17627312019-06-02 19:53:44 +0200811typedef enum
812{
813 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200814 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200815 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200816 TYPE_DIALOG,
817 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200818} create_type_T;
819
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200820/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200821 * Make "buf" empty and set the contents to "text".
822 * Used by popup_create() and popup_settext().
823 */
824 static void
825popup_set_buffer_text(buf_T *buf, typval_T text)
826{
827 int lnum;
828
829 // Clear the buffer, then replace the lines.
830 curbuf = buf;
831 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
832 ml_delete(lnum, FALSE);
833 curbuf = curwin->w_buffer;
834
835 // Add text to the buffer.
836 if (text.v_type == VAR_STRING)
837 {
838 // just a string
839 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
840 }
841 else
842 {
843 list_T *l = text.vval.v_list;
844
845 if (l->lv_len > 0)
846 {
847 if (l->lv_first->li_tv.v_type == VAR_STRING)
848 // list of strings
849 add_popup_strings(buf, l);
850 else
851 // list of dictionaries
852 add_popup_dicts(buf, l);
853 }
854 }
855
856 // delete the line that was in the empty buffer
857 curbuf = buf;
858 ml_delete(buf->b_ml.ml_line_count, FALSE);
859 curbuf = curwin->w_buffer;
860}
861
862/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200863 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200864 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200865 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200866 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200867popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200868{
Bram Moolenaara3fce622019-06-20 02:31:49 +0200869 win_T *wp;
870 tabpage_T *tp = NULL;
871 int tabnr;
872 buf_T *buf;
873 dict_T *d;
874 int nr;
875 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200876
877 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200878 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
879 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200880 {
881 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200882 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200883 }
884 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
885 {
886 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200887 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200888 }
889 d = argvars[1].vval.v_dict;
890
Bram Moolenaara3fce622019-06-20 02:31:49 +0200891 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
892 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
893 else if (type == TYPE_NOTIFICATION)
894 tabnr = -1; // notifications are global by default
895 else
896 tabnr = 0;
897 if (tabnr > 0)
898 {
899 tp = find_tabpage(tabnr);
900 if (tp == NULL)
901 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +0200902 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +0200903 return NULL;
904 }
905 }
906
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200907 // Create the window and buffer.
908 wp = win_alloc_popup_win();
909 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200910 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200911 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200912 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200913
914 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
915 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200916 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200917 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200918
919 win_init_popup_win(wp, buf);
920
921 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200922 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200923 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200924 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200925 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200926 buf->b_p_ul = -1; // no undo
927 buf->b_p_swf = FALSE; // no swap file
928 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200929 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200930 wp->w_p_wrap = TRUE; // 'wrap' is default on
931
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200932 // Avoid that 'buftype' is reset when this buffer is entered.
933 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200934
Bram Moolenaara3fce622019-06-20 02:31:49 +0200935 if (tp != NULL)
936 {
937 // popup on specified tab page
938 wp->w_next = tp->tp_first_popupwin;
939 tp->tp_first_popupwin = wp;
940 }
941 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200942 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200943 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200944 wp->w_next = curtab->tp_first_popupwin;
945 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200946 }
Bram Moolenaara3fce622019-06-20 02:31:49 +0200947 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200948 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200949 win_T *prev = first_popupwin;
950
951 // Global popup: add at the end, so that it gets displayed on top of
952 // older ones with the same zindex. Matters for notifications.
953 if (first_popupwin == NULL)
954 first_popupwin = wp;
955 else
956 {
957 while (prev->w_next != NULL)
958 prev = prev->w_next;
959 prev->w_next = wp;
960 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200961 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200962
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200963 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200964
Bram Moolenaar17627312019-06-02 19:53:44 +0200965 if (type == TYPE_ATCURSOR)
966 {
967 wp->w_popup_pos = POPPOS_BOTLEFT;
968 setcursor_mayforce(TRUE);
969 wp->w_wantline = screen_screenrow();
970 if (wp->w_wantline == 0) // cursor in first line
971 {
972 wp->w_wantline = 2;
973 wp->w_popup_pos = POPPOS_TOPLEFT;
974 }
975 wp->w_wantcol = screen_screencol() + 1;
976 set_moved_values(wp);
977 set_moved_columns(wp, FIND_STRING);
978 }
979
Bram Moolenaar33796b32019-06-08 16:01:13 +0200980 // set default values
981 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
982
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200983 if (type == TYPE_NOTIFICATION)
984 {
985 win_T *twp, *nextwin;
986 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200987
988 // Try to not overlap with another global popup. Guess we need 3
989 // more screen lines than buffer lines.
990 wp->w_wantline = 1;
991 for (twp = first_popupwin; twp != NULL; twp = nextwin)
992 {
993 nextwin = twp->w_next;
994 if (twp != wp
995 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
996 && twp->w_winrow <= wp->w_wantline - 1 + height
997 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
998 {
999 // move to below this popup and restart the loop to check for
1000 // overlap with other popups
1001 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1002 nextwin = first_popupwin;
1003 }
1004 }
1005 if (wp->w_wantline + height > Rows)
1006 {
1007 // can't avoid overlap, put on top in the hope that message goes
1008 // away soon.
1009 wp->w_wantline = 1;
1010 }
1011
1012 wp->w_wantcol = 10;
1013 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001014 wp->w_minwidth = 20;
1015 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001016 for (i = 0; i < 4; ++i)
1017 wp->w_popup_border[i] = 1;
1018 wp->w_popup_padding[1] = 1;
1019 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001020
1021 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001022 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001023 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1024 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001025 }
1026
Bram Moolenaara730e552019-06-16 19:05:31 +02001027 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001028 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001029 wp->w_popup_pos = POPPOS_CENTER;
1030 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1031 wp->w_popup_drag = 1;
1032 for (i = 0; i < 4; ++i)
1033 {
1034 wp->w_popup_border[i] = 1;
1035 wp->w_popup_padding[i] = 1;
1036 }
1037 }
1038
Bram Moolenaara730e552019-06-16 19:05:31 +02001039 if (type == TYPE_MENU)
1040 {
1041 typval_T tv;
1042 callback_T callback;
1043
1044 tv.v_type = VAR_STRING;
1045 tv.vval.v_string = (char_u *)"popup_filter_menu";
1046 callback = get_callback(&tv);
1047 if (callback.cb_name != NULL)
1048 set_callback(&wp->w_filter_cb, &callback);
1049
1050 wp->w_p_wrap = 0;
1051 }
1052
Bram Moolenaarae943152019-06-16 22:54:14 +02001053 for (i = 0; i < 4; ++i)
1054 VIM_CLEAR(wp->w_border_highlight[i]);
1055 for (i = 0; i < 8; ++i)
1056 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001057 wp->w_want_scrollbar = 1;
Bram Moolenaarae943152019-06-16 22:54:14 +02001058
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001059 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001060 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001061
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001062#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001063 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1064 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001065#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001066
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001067 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001068
1069 wp->w_vsep_width = 0;
1070
1071 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001072 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001073
1074 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001075}
1076
1077/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001078 * popup_clear()
1079 */
1080 void
1081f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1082{
1083 close_all_popups();
1084}
1085
1086/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001087 * popup_create({text}, {options})
1088 */
1089 void
1090f_popup_create(typval_T *argvars, typval_T *rettv)
1091{
Bram Moolenaar17627312019-06-02 19:53:44 +02001092 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001093}
1094
1095/*
1096 * popup_atcursor({text}, {options})
1097 */
1098 void
1099f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1100{
Bram Moolenaar17627312019-06-02 19:53:44 +02001101 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001102}
1103
1104/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001105 * Invoke the close callback for window "wp" with value "result".
1106 * Careful: The callback may make "wp" invalid!
1107 */
1108 static void
1109invoke_popup_callback(win_T *wp, typval_T *result)
1110{
1111 typval_T rettv;
1112 int dummy;
1113 typval_T argv[3];
1114
1115 argv[0].v_type = VAR_NUMBER;
1116 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1117
1118 if (result != NULL && result->v_type != VAR_UNKNOWN)
1119 copy_tv(result, &argv[1]);
1120 else
1121 {
1122 argv[1].v_type = VAR_NUMBER;
1123 argv[1].vval.v_number = 0;
1124 }
1125
1126 argv[2].v_type = VAR_UNKNOWN;
1127
1128 call_callback(&wp->w_close_cb, -1,
1129 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1130 if (result != NULL)
1131 clear_tv(&argv[1]);
1132 clear_tv(&rettv);
1133}
1134
1135/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001136 * Close popup "wp" and invoke any close callback for it.
1137 */
1138 static void
1139popup_close_and_callback(win_T *wp, typval_T *arg)
1140{
1141 int id = wp->w_id;
1142
1143 if (wp->w_close_cb.cb_name != NULL)
1144 // Careful: This may make "wp" invalid.
1145 invoke_popup_callback(wp, arg);
1146
1147 popup_close(id);
1148}
1149
1150/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001151 * In a filter: check if the typed key is a mouse event that is used for
1152 * dragging the popup.
1153 */
1154 static void
1155filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1156{
1157 int row = mouse_row;
1158 int col = mouse_col;
1159
1160 if (wp->w_popup_drag
1161 && is_mouse_key(c)
1162 && (wp == popup_dragwin
1163 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1164 // do not consume the key, allow for dragging the popup
1165 rettv->vval.v_number = 0;
1166}
1167
1168 static void
1169popup_highlight_curline(win_T *wp)
1170{
1171 int id;
1172 char buf[100];
1173
1174 match_delete(wp, 1, FALSE);
1175
1176 id = syn_name2id((char_u *)"PopupSelected");
1177 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1178 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1179 (char_u *)buf, 10, 1, NULL, NULL);
1180}
1181
1182/*
1183 * popup_filter_menu({text}, {options})
1184 */
1185 void
1186f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1187{
1188 int id = tv_get_number(&argvars[0]);
1189 win_T *wp = win_id2wp(id);
1190 char_u *key = tv_get_string(&argvars[1]);
1191 typval_T res;
1192 int c;
1193 linenr_T old_lnum;
1194
1195 // If the popup has been closed do not consume the key.
1196 if (wp == NULL)
1197 return;
1198
1199 c = *key;
1200 if (c == K_SPECIAL && key[1] != NUL)
1201 c = TO_SPECIAL(key[1], key[2]);
1202
1203 // consume all keys until done
1204 rettv->vval.v_number = 1;
1205 res.v_type = VAR_NUMBER;
1206
1207 old_lnum = wp->w_cursor.lnum;
1208 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1209 --wp->w_cursor.lnum;
1210 if ((c == 'j' || c == 'J' || c == K_DOWN)
1211 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1212 ++wp->w_cursor.lnum;
1213 if (old_lnum != wp->w_cursor.lnum)
1214 {
1215 popup_highlight_curline(wp);
1216 return;
1217 }
1218
1219 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1220 {
1221 // Cancelled, invoke callback with -1
1222 res.vval.v_number = -1;
1223 popup_close_and_callback(wp, &res);
1224 return;
1225 }
1226 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1227 {
1228 // Invoke callback with current index.
1229 res.vval.v_number = wp->w_cursor.lnum;
1230 popup_close_and_callback(wp, &res);
1231 return;
1232 }
1233
1234 filter_handle_drag(wp, c, rettv);
1235}
1236
1237/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001238 * popup_filter_yesno({text}, {options})
1239 */
1240 void
1241f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1242{
1243 int id = tv_get_number(&argvars[0]);
1244 win_T *wp = win_id2wp(id);
1245 char_u *key = tv_get_string(&argvars[1]);
1246 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001247 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001248
1249 // If the popup has been closed don't consume the key.
1250 if (wp == NULL)
1251 return;
1252
Bram Moolenaara730e552019-06-16 19:05:31 +02001253 c = *key;
1254 if (c == K_SPECIAL && key[1] != NUL)
1255 c = TO_SPECIAL(key[1], key[2]);
1256
Bram Moolenaara42d9452019-06-15 21:46:30 +02001257 // consume all keys until done
1258 rettv->vval.v_number = 1;
1259
Bram Moolenaara730e552019-06-16 19:05:31 +02001260 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001261 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001262 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001263 res.vval.v_number = 0;
1264 else
1265 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001266 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001267 return;
1268 }
1269
1270 // Invoke callback
1271 res.v_type = VAR_NUMBER;
1272 popup_close_and_callback(wp, &res);
1273}
1274
1275/*
1276 * popup_dialog({text}, {options})
1277 */
1278 void
1279f_popup_dialog(typval_T *argvars, typval_T *rettv)
1280{
1281 popup_create(argvars, rettv, TYPE_DIALOG);
1282}
1283
1284/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001285 * popup_menu({text}, {options})
1286 */
1287 void
1288f_popup_menu(typval_T *argvars, typval_T *rettv)
1289{
1290 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1291
1292 if (wp != NULL)
1293 popup_highlight_curline(wp);
1294}
1295
1296/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001297 * popup_notification({text}, {options})
1298 */
1299 void
1300f_popup_notification(typval_T *argvars, typval_T *rettv)
1301{
1302 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1303}
1304
1305/*
1306 * Find the popup window with window-ID "id".
1307 * If the popup window does not exist NULL is returned.
1308 * If the window is not a popup window, and error message is given.
1309 */
1310 static win_T *
1311find_popup_win(int id)
1312{
1313 win_T *wp = win_id2wp(id);
1314
1315 if (wp != NULL && !bt_popup(wp->w_buffer))
1316 {
1317 semsg(_("E993: window %d is not a popup window"), id);
1318 return NULL;
1319 }
1320 return wp;
1321}
1322
1323/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001324 * popup_close({id})
1325 */
1326 void
1327f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1328{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001329 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001330 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001331
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001332 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001333 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001334}
1335
1336/*
1337 * popup_hide({id})
1338 */
1339 void
1340f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1341{
1342 int id = (int)tv_get_number(argvars);
1343 win_T *wp = find_popup_win(id);
1344
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001345 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001346 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001347 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001348 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001349 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001350 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001351 }
1352}
1353
1354/*
1355 * popup_show({id})
1356 */
1357 void
1358f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1359{
1360 int id = (int)tv_get_number(argvars);
1361 win_T *wp = find_popup_win(id);
1362
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001363 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001364 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001365 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001366 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001367 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001368 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001369 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001370}
1371
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001372/*
1373 * popup_settext({id}, {text})
1374 */
1375 void
1376f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1377{
1378 int id = (int)tv_get_number(&argvars[0]);
1379 win_T *wp = find_popup_win(id);
1380
1381 if (wp != NULL)
1382 {
1383 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1384 popup_adjust_position(wp);
1385 }
1386}
1387
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001388 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001389popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001390{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001391 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001392 if (wp->w_winrow + wp->w_height >= cmdline_row)
1393 clear_cmdline = TRUE;
1394 win_free_popup(wp);
1395 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001396 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001397}
1398
Bram Moolenaarec583842019-05-26 14:11:23 +02001399/*
1400 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001401 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001402 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001403 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001404popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001405{
1406 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001407 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001408 win_T *prev = NULL;
1409
Bram Moolenaarec583842019-05-26 14:11:23 +02001410 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001411 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001412 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001413 {
1414 if (prev == NULL)
1415 first_popupwin = wp->w_next;
1416 else
1417 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001418 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001419 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001420 }
1421
Bram Moolenaarec583842019-05-26 14:11:23 +02001422 // go through tab-local popups
1423 FOR_ALL_TABPAGES(tp)
1424 popup_close_tabpage(tp, id);
1425}
1426
1427/*
1428 * Close a popup window with Window-id "id" in tabpage "tp".
1429 */
1430 void
1431popup_close_tabpage(tabpage_T *tp, int id)
1432{
1433 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001434 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001435 win_T *prev = NULL;
1436
Bram Moolenaarec583842019-05-26 14:11:23 +02001437 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1438 if (wp->w_id == id)
1439 {
1440 if (prev == NULL)
1441 *root = wp->w_next;
1442 else
1443 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001444 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001445 return;
1446 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001447}
1448
1449 void
1450close_all_popups(void)
1451{
1452 while (first_popupwin != NULL)
1453 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001454 while (curtab->tp_first_popupwin != NULL)
1455 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001456}
1457
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001458/*
1459 * popup_move({id}, {options})
1460 */
1461 void
1462f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1463{
Bram Moolenaarae943152019-06-16 22:54:14 +02001464 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001465 int id = (int)tv_get_number(argvars);
1466 win_T *wp = find_popup_win(id);
1467
1468 if (wp == NULL)
1469 return; // invalid {id}
1470
1471 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1472 {
1473 emsg(_(e_dictreq));
1474 return;
1475 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001476 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001477
Bram Moolenaarae943152019-06-16 22:54:14 +02001478 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001479
1480 if (wp->w_winrow + wp->w_height >= cmdline_row)
1481 clear_cmdline = TRUE;
1482 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001483}
1484
Bram Moolenaarbc133542019-05-29 20:26:46 +02001485/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001486 * popup_setoptions({id}, {options})
1487 */
1488 void
1489f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1490{
1491 dict_T *dict;
1492 int id = (int)tv_get_number(argvars);
1493 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001494 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001495
1496 if (wp == NULL)
1497 return; // invalid {id}
1498
1499 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1500 {
1501 emsg(_(e_dictreq));
1502 return;
1503 }
1504 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001505 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001506
1507 apply_move_options(wp, dict);
1508 apply_general_options(wp, dict);
1509
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001510 if (old_firstline != wp->w_firstline)
1511 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001512 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001513 popup_adjust_position(wp);
1514}
1515
1516/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001517 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001518 */
1519 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001520f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001521{
1522 dict_T *dict;
1523 int id = (int)tv_get_number(argvars);
1524 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001525 int top_extra;
1526 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001527
1528 if (rettv_dict_alloc(rettv) == OK)
1529 {
1530 if (wp == NULL)
1531 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001532 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001533 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1534
Bram Moolenaarbc133542019-05-29 20:26:46 +02001535 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001536
Bram Moolenaarbc133542019-05-29 20:26:46 +02001537 dict_add_number(dict, "line", wp->w_winrow + 1);
1538 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001539 dict_add_number(dict, "width", wp->w_width + left_extra
1540 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1541 dict_add_number(dict, "height", wp->w_height + top_extra
1542 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001543
1544 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1545 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1546 dict_add_number(dict, "core_width", wp->w_width);
1547 dict_add_number(dict, "core_height", wp->w_height);
1548
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001549 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001550 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001551 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001552 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001553 }
1554}
1555
1556/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001557 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1558 */
1559 static void
1560get_padding_border(dict_T *dict, int *array, char *name)
1561{
1562 list_T *list;
1563 int i;
1564
1565 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1566 return;
1567
1568 list = list_alloc();
1569 if (list != NULL)
1570 {
1571 dict_add_list(dict, name, list);
1572 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1573 for (i = 0; i < 4; ++i)
1574 list_append_number(list, array[i]);
1575 }
1576}
1577
1578/*
1579 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1580 */
1581 static void
1582get_borderhighlight(dict_T *dict, win_T *wp)
1583{
1584 list_T *list;
1585 int i;
1586
1587 for (i = 0; i < 4; ++i)
1588 if (wp->w_border_highlight[i] != NULL)
1589 break;
1590 if (i == 4)
1591 return;
1592
1593 list = list_alloc();
1594 if (list != NULL)
1595 {
1596 dict_add_list(dict, "borderhighlight", list);
1597 for (i = 0; i < 4; ++i)
1598 list_append_string(list, wp->w_border_highlight[i], -1);
1599 }
1600}
1601
1602/*
1603 * For popup_getoptions(): add a "borderchars" entry to "dict".
1604 */
1605 static void
1606get_borderchars(dict_T *dict, win_T *wp)
1607{
1608 list_T *list;
1609 int i;
1610 char_u buf[NUMBUFLEN];
1611 int len;
1612
1613 for (i = 0; i < 8; ++i)
1614 if (wp->w_border_char[i] != 0)
1615 break;
1616 if (i == 8)
1617 return;
1618
1619 list = list_alloc();
1620 if (list != NULL)
1621 {
1622 dict_add_list(dict, "borderchars", list);
1623 for (i = 0; i < 8; ++i)
1624 {
1625 len = mb_char2bytes(wp->w_border_char[i], buf);
1626 list_append_string(list, buf, len);
1627 }
1628 }
1629}
1630
1631/*
1632 * For popup_getoptions(): add a "moved" entry to "dict".
1633 */
1634 static void
1635get_moved_list(dict_T *dict, win_T *wp)
1636{
1637 list_T *list;
1638
1639 list = list_alloc();
1640 if (list != NULL)
1641 {
1642 dict_add_list(dict, "moved", list);
1643 list_append_number(list, wp->w_popup_mincol);
1644 list_append_number(list, wp->w_popup_maxcol);
1645 }
1646}
1647
1648/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001649 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001650 */
1651 void
1652f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1653{
1654 dict_T *dict;
1655 int id = (int)tv_get_number(argvars);
1656 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001657 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001658 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001659
1660 if (rettv_dict_alloc(rettv) == OK)
1661 {
1662 if (wp == NULL)
1663 return;
1664
1665 dict = rettv->vval.v_dict;
1666 dict_add_number(dict, "line", wp->w_wantline);
1667 dict_add_number(dict, "col", wp->w_wantcol);
1668 dict_add_number(dict, "minwidth", wp->w_minwidth);
1669 dict_add_number(dict, "minheight", wp->w_minheight);
1670 dict_add_number(dict, "maxheight", wp->w_maxheight);
1671 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001672 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001673 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001674 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001675 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001676 dict_add_string(dict, "title", wp->w_popup_title);
1677 dict_add_number(dict, "wrap", wp->w_p_wrap);
1678 dict_add_number(dict, "drag", wp->w_popup_drag);
1679 dict_add_string(dict, "highlight", wp->w_p_wcr);
1680
Bram Moolenaara3fce622019-06-20 02:31:49 +02001681 // find the tabpage that holds this popup
1682 i = 1;
1683 FOR_ALL_TABPAGES(tp)
1684 {
1685 win_T *p;
1686
1687 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1688 if (p->w_id == id)
1689 break;
1690 if (p != NULL)
1691 break;
1692 ++i;
1693 }
1694 if (tp == NULL)
1695 i = -1; // must be global
1696 else if (tp == curtab)
1697 i = 0;
1698 dict_add_number(dict, "tabpage", i);
1699
Bram Moolenaarae943152019-06-16 22:54:14 +02001700 get_padding_border(dict, wp->w_popup_padding, "padding");
1701 get_padding_border(dict, wp->w_popup_border, "border");
1702 get_borderhighlight(dict, wp);
1703 get_borderchars(dict, wp);
1704 get_moved_list(dict, wp);
1705
1706 if (wp->w_filter_cb.cb_name != NULL)
1707 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1708 if (wp->w_close_cb.cb_name != NULL)
1709 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001710
1711 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1712 ++i)
1713 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1714 {
1715 dict_add_string(dict, "pos",
1716 (char_u *)poppos_entries[i].pp_name);
1717 break;
1718 }
1719
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001720# if defined(FEAT_TIMERS)
1721 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1722 ? (long)wp->w_popup_timer->tr_interval : 0L);
1723# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001724 }
1725}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001726
1727 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001728error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001729{
1730 if (bt_popup(curwin->w_buffer))
1731 {
1732 emsg(_("E994: Not allowed in a popup window"));
1733 return TRUE;
1734 }
1735 return FALSE;
1736}
1737
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001738/*
1739 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001740 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001741 */
1742 void
1743popup_reset_handled()
1744{
1745 win_T *wp;
1746
1747 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1748 wp->w_popup_flags &= ~POPF_HANDLED;
1749 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1750 wp->w_popup_flags &= ~POPF_HANDLED;
1751}
1752
1753/*
1754 * Find the next visible popup where POPF_HANDLED is not set.
1755 * Must have called popup_reset_handled() first.
1756 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1757 * popup with the highest zindex.
1758 */
1759 win_T *
1760find_next_popup(int lowest)
1761{
1762 win_T *wp;
1763 win_T *found_wp;
1764 int found_zindex;
1765
1766 found_zindex = lowest ? INT_MAX : 0;
1767 found_wp = NULL;
1768 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1769 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1770 && (lowest ? wp->w_zindex < found_zindex
1771 : wp->w_zindex > found_zindex))
1772 {
1773 found_zindex = wp->w_zindex;
1774 found_wp = wp;
1775 }
1776 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1777 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1778 && (lowest ? wp->w_zindex < found_zindex
1779 : wp->w_zindex > found_zindex))
1780 {
1781 found_zindex = wp->w_zindex;
1782 found_wp = wp;
1783 }
1784
1785 if (found_wp != NULL)
1786 found_wp->w_popup_flags |= POPF_HANDLED;
1787 return found_wp;
1788}
1789
1790/*
1791 * Invoke the filter callback for window "wp" with typed character "c".
1792 * Uses the global "mod_mask" for modifiers.
1793 * Returns the return value of the filter.
1794 * Careful: The filter may make "wp" invalid!
1795 */
1796 static int
1797invoke_popup_filter(win_T *wp, int c)
1798{
1799 int res;
1800 typval_T rettv;
1801 int dummy;
1802 typval_T argv[3];
1803 char_u buf[NUMBUFLEN];
1804
Bram Moolenaara42d9452019-06-15 21:46:30 +02001805 // Emergency exit: CTRL-C closes the popup.
1806 if (c == Ctrl_C)
1807 {
1808 rettv.v_type = VAR_NUMBER;
1809 rettv.vval.v_number = -1;
1810 popup_close_and_callback(wp, &rettv);
1811 return 1;
1812 }
1813
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001814 argv[0].v_type = VAR_NUMBER;
1815 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1816
1817 // Convert the number to a string, so that the function can use:
1818 // if a:c == "\<F2>"
1819 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1820 argv[1].v_type = VAR_STRING;
1821 argv[1].vval.v_string = vim_strsave(buf);
1822
1823 argv[2].v_type = VAR_UNKNOWN;
1824
Bram Moolenaara42d9452019-06-15 21:46:30 +02001825 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001826 call_callback(&wp->w_filter_cb, -1,
1827 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1828 res = tv_get_number(&rettv);
1829 vim_free(argv[1].vval.v_string);
1830 clear_tv(&rettv);
1831 return res;
1832}
1833
1834/*
1835 * Called when "c" was typed: invoke popup filter callbacks.
1836 * Returns TRUE when the character was consumed,
1837 */
1838 int
1839popup_do_filter(int c)
1840{
1841 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001842 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001843
1844 popup_reset_handled();
1845
1846 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1847 if (wp->w_filter_cb.cb_name != NULL)
1848 res = invoke_popup_filter(wp, c);
1849
1850 return res;
1851}
1852
Bram Moolenaar3397f742019-06-02 18:40:06 +02001853/*
1854 * Called when the cursor moved: check if any popup needs to be closed if the
1855 * cursor moved far enough.
1856 */
1857 void
1858popup_check_cursor_pos()
1859{
1860 win_T *wp;
1861 typval_T tv;
1862
1863 popup_reset_handled();
1864 while ((wp = find_next_popup(TRUE)) != NULL)
1865 if (wp->w_popup_curwin != NULL
1866 && (curwin != wp->w_popup_curwin
1867 || curwin->w_cursor.lnum != wp->w_popup_lnum
1868 || curwin->w_cursor.col < wp->w_popup_mincol
1869 || curwin->w_cursor.col > wp->w_popup_maxcol))
1870 {
1871 tv.v_type = VAR_NUMBER;
1872 tv.vval.v_number = -1;
1873 popup_close_and_callback(wp, &tv);
1874 }
1875}
1876
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001877/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001878 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
1879 * "col" and "line" are screen coordinates.
1880 */
1881 static int
1882popup_masked(win_T *wp, int screencol, int screenline)
1883{
1884 int col = screencol - wp->w_wincol + 1;
1885 int line = screenline - wp->w_winrow + 1;
1886 listitem_T *lio, *li;
1887 int width, height;
1888
1889 if (wp->w_popup_mask == NULL)
1890 return FALSE;
1891 width = popup_width(wp);
1892 height = popup_height(wp);
1893
1894 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1895 {
1896 int cols, cole;
1897 int lines, linee;
1898
1899 li = lio->li_tv.vval.v_list->lv_first;
1900 cols = tv_get_number(&li->li_tv);
1901 if (cols < 0)
1902 cols = width + cols + 1;
1903 if (col < cols)
1904 continue;
1905 li = li->li_next;
1906 cole = tv_get_number(&li->li_tv);
1907 if (cole < 0)
1908 cole = width + cole + 1;
1909 if (col > cole)
1910 continue;
1911 li = li->li_next;
1912 lines = tv_get_number(&li->li_tv);
1913 if (lines < 0)
1914 lines = height + lines + 1;
1915 if (line < lines)
1916 continue;
1917 li = li->li_next;
1918 linee = tv_get_number(&li->li_tv);
1919 if (linee < 0)
1920 linee = height + linee + 1;
1921 if (line > linee)
1922 continue;
1923 return TRUE;
1924 }
1925 return FALSE;
1926}
1927
1928/*
1929 * Set flags in popup_transparent[] for window "wp" to "val".
1930 */
1931 static void
1932update_popup_transparent(win_T *wp, int val)
1933{
1934 if (wp->w_popup_mask != NULL)
1935 {
1936 int width = popup_width(wp);
1937 int height = popup_height(wp);
1938 listitem_T *lio, *li;
1939 int cols, cole;
1940 int lines, linee;
1941 int col, line;
1942
1943 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1944 {
1945 li = lio->li_tv.vval.v_list->lv_first;
1946 cols = tv_get_number(&li->li_tv);
1947 if (cols < 0)
1948 cols = width + cols + 1;
1949 li = li->li_next;
1950 cole = tv_get_number(&li->li_tv);
1951 if (cole < 0)
1952 cole = width + cole + 1;
1953 li = li->li_next;
1954 lines = tv_get_number(&li->li_tv);
1955 if (lines < 0)
1956 lines = height + lines + 1;
1957 li = li->li_next;
1958 linee = tv_get_number(&li->li_tv);
1959 if (linee < 0)
1960 linee = height + linee + 1;
1961
1962 --cols;
1963 --lines;
1964 for (line = lines; line < linee && line < screen_Rows; ++line)
1965 for (col = cols; col < cole && col < screen_Columns; ++col)
1966 popup_transparent[(line + wp->w_winrow) * screen_Columns
1967 + col + wp->w_wincol] = val;
1968 }
1969 }
1970}
1971
1972/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001973 * Update "popup_mask" if needed.
1974 * Also recomputes the popup size and positions.
1975 * Also updates "popup_visible".
1976 * Also marks window lines for redrawing.
1977 */
1978 void
1979may_update_popup_mask(int type)
1980{
1981 win_T *wp;
1982 short *mask;
1983 int line, col;
1984 int redraw_all = FALSE;
1985
1986 // Need to recompute when switching tabs.
1987 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1988 // (such as the screen size) must have changed.
1989 if (popup_mask_tab != curtab || type >= NOT_VALID)
1990 {
1991 popup_mask_refresh = TRUE;
1992 redraw_all = TRUE;
1993 }
1994 if (!popup_mask_refresh)
1995 {
1996 // Check if any buffer has changed.
1997 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1998 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1999 popup_mask_refresh = TRUE;
2000 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2001 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2002 popup_mask_refresh = TRUE;
2003 if (!popup_mask_refresh)
2004 return;
2005 }
2006
2007 // Need to update the mask, something has changed.
2008 popup_mask_refresh = FALSE;
2009 popup_mask_tab = curtab;
2010 popup_visible = FALSE;
2011
2012 // If redrawing everything, just update "popup_mask".
2013 // If redrawing only what is needed, update "popup_mask_next" and then
2014 // compare with "popup_mask" to see what changed.
2015 if (type >= SOME_VALID)
2016 mask = popup_mask;
2017 else
2018 mask = popup_mask_next;
2019 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2020
2021 // Find the window with the lowest zindex that hasn't been handled yet,
2022 // so that the window with a higher zindex overwrites the value in
2023 // popup_mask.
2024 popup_reset_handled();
2025 while ((wp = find_next_popup(TRUE)) != NULL)
2026 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002027 int height = popup_height(wp);
2028 int width = popup_width(wp);
2029
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002030 popup_visible = TRUE;
2031
2032 // Recompute the position if the text changed.
2033 if (redraw_all
2034 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2035 popup_adjust_position(wp);
2036
2037 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002038 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002039 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002040 col < wp->w_wincol + width && col < screen_Columns; ++col)
2041 if (!popup_masked(wp, col, line))
2042 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002043 }
2044
2045 // Only check which lines are to be updated if not already
2046 // updating all lines.
2047 if (mask == popup_mask_next)
2048 for (line = 0; line < screen_Rows; ++line)
2049 {
2050 int col_done = 0;
2051
2052 for (col = 0; col < screen_Columns; ++col)
2053 {
2054 int off = line * screen_Columns + col;
2055
2056 if (popup_mask[off] != popup_mask_next[off])
2057 {
2058 popup_mask[off] = popup_mask_next[off];
2059
2060 if (line >= cmdline_row)
2061 {
2062 // the command line needs to be cleared if text below
2063 // the popup is now visible.
2064 if (!msg_scrolled && popup_mask_next[off] == 0)
2065 clear_cmdline = TRUE;
2066 }
2067 else if (col >= col_done)
2068 {
2069 linenr_T lnum;
2070 int line_cp = line;
2071 int col_cp = col;
2072
2073 // The screen position "line" / "col" needs to be
2074 // redrawn. Figure out what window that is and update
2075 // w_redraw_top and w_redr_bot. Only needs to be done
2076 // once for each window line.
2077 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2078 if (wp != NULL)
2079 {
2080 if (line_cp >= wp->w_height)
2081 // In (or below) status line
2082 wp->w_redr_status = TRUE;
2083 // compute the position in the buffer line from the
2084 // position on the screen
2085 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2086 &lnum))
2087 // past bottom
2088 wp->w_redr_status = TRUE;
2089 else
2090 redrawWinline(wp, lnum);
2091
2092 // This line is going to be redrawn, no need to
2093 // check until the right side of the window.
2094 col_done = wp->w_wincol + wp->w_width - 1;
2095 }
2096 }
2097 }
2098 }
2099 }
2100}
2101
2102/*
2103 * Return a string of "len" spaces in IObuff.
2104 */
2105 static char_u *
2106get_spaces(int len)
2107{
2108 vim_memset(IObuff, ' ', (size_t)len);
2109 IObuff[len] = NUL;
2110 return IObuff;
2111}
2112
2113/*
2114 * Update popup windows. They are drawn on top of normal windows.
2115 * "win_update" is called for each popup window, lowest zindex first.
2116 */
2117 void
2118update_popups(void (*win_update)(win_T *wp))
2119{
2120 win_T *wp;
2121 int top_off;
2122 int left_off;
2123 int total_width;
2124 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002125 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002126 int popup_attr;
2127 int border_attr[4];
2128 int border_char[8];
2129 char_u buf[MB_MAXBYTES];
2130 int row;
2131 int i;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002132 int sb_thumb_top;
2133 int sb_thumb_height;
2134 int attr_scroll = highlight_attr[HLF_PSB];
2135 int attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002136
2137 // Find the window with the lowest zindex that hasn't been updated yet,
2138 // so that the window with a higher zindex is drawn later, thus goes on
2139 // top.
2140 popup_reset_handled();
2141 while ((wp = find_next_popup(TRUE)) != NULL)
2142 {
2143 // This drawing uses the zindex of the popup window, so that it's on
2144 // top of the text but doesn't draw when another popup with higher
2145 // zindex is on top of the character.
2146 screen_zindex = wp->w_zindex;
2147
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002148 // Set flags in popup_transparent[] for masked cells.
2149 update_popup_transparent(wp, 1);
2150
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002151 // adjust w_winrow and w_wincol for border and padding, since
2152 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002153 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002154 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2155 wp->w_winrow += top_off;
2156 wp->w_wincol += left_off;
2157
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002158 // Draw the popup text, unless it's off screen.
2159 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2160 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002161
2162 wp->w_winrow -= top_off;
2163 wp->w_wincol -= left_off;
2164
2165 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002166 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1]
2167 + wp->w_has_scrollbar;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002168 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002169 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2170 popup_attr = get_wcr_attr(wp);
2171
2172 // We can only use these line drawing characters when 'encoding' is
2173 // "utf-8" and 'ambiwidth' is "single".
2174 if (enc_utf8 && *p_ambw == 's')
2175 {
2176 border_char[0] = border_char[2] = 0x2550;
2177 border_char[1] = border_char[3] = 0x2551;
2178 border_char[4] = 0x2554;
2179 border_char[5] = 0x2557;
2180 border_char[6] = 0x255d;
2181 border_char[7] = 0x255a;
2182 }
2183 else
2184 {
2185 border_char[0] = border_char[2] = '-';
2186 border_char[1] = border_char[3] = '|';
2187 for (i = 4; i < 8; ++i)
2188 border_char[i] = '+';
2189 }
2190 for (i = 0; i < 8; ++i)
2191 if (wp->w_border_char[i] != 0)
2192 border_char[i] = wp->w_border_char[i];
2193
2194 for (i = 0; i < 4; ++i)
2195 {
2196 border_attr[i] = popup_attr;
2197 if (wp->w_border_highlight[i] != NULL)
2198 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2199 }
2200
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002201 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002202 if (wp->w_popup_border[0] > 0)
2203 {
2204 // top border
2205 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2206 wp->w_wincol,
2207 wp->w_wincol + total_width,
2208 wp->w_popup_border[3] != 0
2209 ? border_char[4] : border_char[0],
2210 border_char[0], border_attr[0]);
2211 if (wp->w_popup_border[1] > 0)
2212 {
2213 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2214 screen_puts(buf, wp->w_winrow,
2215 wp->w_wincol + total_width - 1, border_attr[1]);
2216 }
2217 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002218 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2219 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002220
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002221 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002222 {
2223 // top padding
2224 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002225 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002226 wp->w_wincol + wp->w_popup_border[3],
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002227 wp->w_wincol + total_width - wp->w_popup_border[1]
2228 - wp->w_has_scrollbar,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002229 ' ', ' ', popup_attr);
2230 }
2231
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002232 // Title goes on top of border or padding.
2233 if (wp->w_popup_title != NULL)
2234 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2235 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2236
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002237 // Compute scrollbar thumb position and size.
2238 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002239 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002240 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2241
Bram Moolenaar68acb412019-06-26 03:40:36 +02002242 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2243 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002244 if (sb_thumb_height == 0)
2245 sb_thumb_height = 1;
Bram Moolenaar68acb412019-06-26 03:40:36 +02002246 sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2)
2247 * (wp->w_height - sb_thumb_height)
2248 / (linecount - wp->w_height);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002249 }
2250
2251 for (i = wp->w_popup_border[0];
2252 i < total_height - wp->w_popup_border[2]; ++i)
2253 {
2254 row = wp->w_winrow + i;
2255
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002256 // left border
2257 if (wp->w_popup_border[3] > 0)
2258 {
2259 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2260 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2261 }
2262 // left padding
2263 if (wp->w_popup_padding[3] > 0)
2264 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2265 wp->w_wincol + wp->w_popup_border[3], popup_attr);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002266 // scrollbar
2267 if (wp->w_has_scrollbar)
2268 {
2269 int line = i - top_off;
2270 int scroll_col = wp->w_wincol + total_width - 1
2271 - wp->w_popup_border[1];
2272
2273 if (line >= 0 && line < wp->w_height)
2274 screen_putchar(' ', row, scroll_col,
2275 line >= sb_thumb_top
2276 && line < sb_thumb_top + sb_thumb_height
2277 ? attr_thumb : attr_scroll);
2278 else
2279 screen_putchar(' ', row, scroll_col, popup_attr);
2280 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002281 // right border
2282 if (wp->w_popup_border[1] > 0)
2283 {
2284 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2285 screen_puts(buf, row,
2286 wp->w_wincol + total_width - 1, border_attr[1]);
2287 }
2288 // right padding
2289 if (wp->w_popup_padding[1] > 0)
2290 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2291 wp->w_wincol + wp->w_popup_border[3]
2292 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2293 }
2294
2295 if (wp->w_popup_padding[2] > 0)
2296 {
2297 // bottom padding
2298 row = wp->w_winrow + wp->w_popup_border[0]
2299 + wp->w_popup_padding[0] + wp->w_height;
2300 screen_fill(row, row + wp->w_popup_padding[2],
2301 wp->w_wincol + wp->w_popup_border[3],
2302 wp->w_wincol + total_width - wp->w_popup_border[1],
2303 ' ', ' ', popup_attr);
2304 }
2305
2306 if (wp->w_popup_border[2] > 0)
2307 {
2308 // bottom border
2309 row = wp->w_winrow + total_height - 1;
2310 screen_fill(row , row + 1,
2311 wp->w_wincol,
2312 wp->w_wincol + total_width,
2313 wp->w_popup_border[3] != 0
2314 ? border_char[7] : border_char[2],
2315 border_char[2], border_attr[2]);
2316 if (wp->w_popup_border[1] > 0)
2317 {
2318 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2319 screen_puts(buf, row,
2320 wp->w_wincol + total_width - 1, border_attr[2]);
2321 }
2322 }
2323
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002324 update_popup_transparent(wp, 0);
2325
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002326 // Back to the normal zindex.
2327 screen_zindex = 0;
2328 }
2329}
2330
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002331/*
2332 * Mark references in callbacks of one popup window.
2333 */
2334 static int
2335set_ref_in_one_popup(win_T *wp, int copyID)
2336{
2337 int abort = FALSE;
2338 typval_T tv;
2339
2340 if (wp->w_close_cb.cb_partial != NULL)
2341 {
2342 tv.v_type = VAR_PARTIAL;
2343 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2344 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2345 }
2346 if (wp->w_filter_cb.cb_partial != NULL)
2347 {
2348 tv.v_type = VAR_PARTIAL;
2349 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2350 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2351 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002352 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002353 return abort;
2354}
2355
2356/*
2357 * Set reference in callbacks of popup windows.
2358 */
2359 int
2360set_ref_in_popups(int copyID)
2361{
2362 int abort = FALSE;
2363 win_T *wp;
2364 tabpage_T *tp;
2365
2366 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2367 abort = abort || set_ref_in_one_popup(wp, copyID);
2368
2369 FOR_ALL_TABPAGES(tp)
2370 {
2371 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2372 abort = abort || set_ref_in_one_popup(wp, copyID);
2373 if (abort)
2374 break;
2375 }
2376 return abort;
2377}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002378#endif // FEAT_TEXT_PROP