blob: 96263bca4f6eaeacf927e4e52c8510fbf671bfd2 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_TEXT_PROP
17
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar4d784b22019-05-25 19:51:39 +020031/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020032 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020033 * Handles "cursor+N" and "cursor-N".
34 */
35 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020036popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037{
38 dictitem_T *di;
39 char_u *val;
40 char_u *s;
41 char_u *endp;
42 int n = 0;
43
44 di = dict_find(dict, key, -1);
45 if (di == NULL)
46 return 0;
47
48 val = tv_get_string(&di->di_tv);
49 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020050 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020051
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020056 endp = s;
57 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
58 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059 if (endp != NULL && *skipwhite(endp) != NUL)
60 {
61 semsg(_(e_invexpr2), val);
62 return 0;
63 }
64 }
65
66 if (STRCMP(key, "line") == 0)
67 n = screen_screenrow() + 1 + n;
68 else // "col"
69 n = screen_screencol() + 1 + n;
70
71 if (n < 1)
72 n = 1;
73 return n;
74}
75
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020076 static void
77get_pos_options(win_T *wp, dict_T *dict)
78{
79 char_u *str;
80 int nr;
Bram Moolenaar711d02c2019-06-28 04:06:50 +020081 dictitem_T *di;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020082
83 nr = popup_options_one(dict, (char_u *)"line");
84 if (nr > 0)
85 wp->w_wantline = nr;
86 nr = popup_options_one(dict, (char_u *)"col");
87 if (nr > 0)
88 wp->w_wantcol = nr;
89
Bram Moolenaar711d02c2019-06-28 04:06:50 +020090 di = dict_find(dict, (char_u *)"fixed", -1);
91 if (di != NULL)
92 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020093
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020094 str = dict_get_string(dict, (char_u *)"pos", FALSE);
95 if (str != NULL)
96 {
97 for (nr = 0;
98 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
99 ++nr)
100 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
101 {
102 wp->w_popup_pos = poppos_entries[nr].pp_val;
103 nr = -1;
104 break;
105 }
106 if (nr != -1)
107 semsg(_(e_invarg2), str);
108 }
109}
110
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200111 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200112set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113{
114 dictitem_T *di;
115
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200116 di = dict_find(dict, (char_u *)name, -1);
117 if (di != NULL)
118 {
119 if (di->di_tv.v_type != VAR_LIST)
120 emsg(_(e_listreq));
121 else
122 {
123 list_T *list = di->di_tv.vval.v_list;
124 listitem_T *li;
125 int i;
126 int nr;
127
128 for (i = 0; i < 4; ++i)
129 array[i] = 1;
130 if (list != NULL)
131 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
132 ++i, li = li->li_next)
133 {
134 nr = (int)tv_get_number(&li->li_tv);
135 if (nr >= 0)
136 array[i] = nr > max_val ? max_val : nr;
137 }
138 }
139 }
140}
141
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200142/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200143 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200144 */
145 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200146set_moved_values(win_T *wp)
147{
148 wp->w_popup_curwin = curwin;
149 wp->w_popup_lnum = curwin->w_cursor.lnum;
150 wp->w_popup_mincol = curwin->w_cursor.col;
151 wp->w_popup_maxcol = curwin->w_cursor.col;
152}
153
154/*
155 * Used when popup options contain "moved" with "word" or "WORD".
156 */
157 static void
158set_moved_columns(win_T *wp, int flags)
159{
160 char_u *ptr;
161 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
162
163 if (len > 0)
164 {
165 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
166 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
167 }
168}
169
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200170/*
171 * Return TRUE if "row"/"col" is on the border of the popup.
172 * The values are relative to the top-left corner.
173 */
174 int
175popup_on_border(win_T *wp, int row, int col)
176{
177 return (row == 0 && wp->w_popup_border[0] > 0)
178 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
179 || (col == 0 && wp->w_popup_border[3] > 0)
180 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
181}
182
183// Values set when dragging a popup window starts.
184static int drag_start_row;
185static int drag_start_col;
186static int drag_start_wantline;
187static int drag_start_wantcol;
188
189/*
190 * Mouse down on border of popup window: start dragging it.
191 * Uses mouse_col and mouse_row.
192 */
193 void
194popup_start_drag(win_T *wp)
195{
196 drag_start_row = mouse_row;
197 drag_start_col = mouse_col;
198 // TODO: handle using different corner
199 if (wp->w_wantline == 0)
200 drag_start_wantline = wp->w_winrow + 1;
201 else
202 drag_start_wantline = wp->w_wantline;
203 if (wp->w_wantcol == 0)
204 drag_start_wantcol = wp->w_wincol + 1;
205 else
206 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200207
208 // Stop centering the popup
209 if (wp->w_popup_pos == POPPOS_CENTER)
210 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200211}
212
213/*
214 * Mouse moved while dragging a popup window: adjust the window popup position.
215 */
216 void
217popup_drag(win_T *wp)
218{
219 // The popup may be closed before dragging stops.
220 if (!win_valid_popup(wp))
221 return;
222
223 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
224 if (wp->w_wantline < 1)
225 wp->w_wantline = 1;
226 if (wp->w_wantline > Rows)
227 wp->w_wantline = Rows;
228 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
229 if (wp->w_wantcol < 1)
230 wp->w_wantcol = 1;
231 if (wp->w_wantcol > Columns)
232 wp->w_wantcol = Columns;
233
234 popup_adjust_position(wp);
235}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200236
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200237/*
238 * Set w_firstline to match the current "wp->w_topline".
239 */
240 void
241popup_set_firstline(win_T *wp)
242{
243 int height = wp->w_height;
244
245 wp->w_firstline = wp->w_topline;
246 popup_adjust_position(wp);
247
248 // we don't want the popup to get smaller, decrement the first line
249 // until it doesn't
250 while (wp->w_firstline > 1 && wp->w_height < height)
251 {
252 --wp->w_firstline;
253 popup_adjust_position(wp);
254 }
255}
256
257/*
258 * Handle a click in a popup window, if it is in the scrollbar.
259 */
260 void
261popup_handle_scrollbar_click(win_T *wp, int row, int col)
262{
263 int height = popup_height(wp);
264 int old_topline = wp->w_topline;
265
266 if (wp->w_has_scrollbar == 0)
267 return;
268 if (row >= wp->w_popup_border[0]
269 && row < height - wp->w_popup_border[2]
270 && col == popup_width(wp) - 1)
271 {
272 if (row >= height / 2)
273 {
274 // Click in lower half, scroll down.
275 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
276 ++wp->w_topline;
277 }
278 else if (wp->w_topline > 1)
279 // click on upper half, scroll up.
280 --wp->w_topline;
281 if (wp->w_topline != old_topline)
282 {
283 popup_set_firstline(wp);
284 redraw_win_later(wp, NOT_VALID);
285 }
286 }
287}
288
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200289#if defined(FEAT_TIMERS)
290 static void
291popup_add_timeout(win_T *wp, int time)
292{
293 char_u cbbuf[50];
294 char_u *ptr = cbbuf;
295 typval_T tv;
296
297 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
298 "{_ -> popup_close(%d)}", wp->w_id);
299 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
300 {
301 wp->w_popup_timer = create_timer(time, 0);
302 wp->w_popup_timer->tr_callback = get_callback(&tv);
303 clear_tv(&tv);
304 }
305}
306#endif
307
Bram Moolenaar17627312019-06-02 19:53:44 +0200308/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200309 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200310 */
311 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200312apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200313{
Bram Moolenaarae943152019-06-16 22:54:14 +0200314 int nr;
315
316 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
317 wp->w_minwidth = nr;
318 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
319 wp->w_minheight = nr;
320 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
321 wp->w_maxwidth = nr;
322 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
323 wp->w_maxheight = nr;
324 get_pos_options(wp, d);
325}
326
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200327 static void
328check_highlight(dict_T *dict, char *name, char_u **pval)
329{
330 dictitem_T *di;
331 char_u *str;
332
333 di = dict_find(dict, (char_u *)name, -1);
334 if (di != NULL)
335 {
336 if (di->di_tv.v_type != VAR_STRING)
337 semsg(_(e_invargval), name);
338 else
339 {
340 str = tv_get_string(&di->di_tv);
341 if (*str != NUL)
342 *pval = vim_strsave(str);
343 }
344 }
345}
346
Bram Moolenaarae943152019-06-16 22:54:14 +0200347/*
348 * Shared between popup_create() and f_popup_setoptions().
349 */
350 static void
351apply_general_options(win_T *wp, dict_T *dict)
352{
353 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200354 int nr;
355 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200356
Bram Moolenaarae943152019-06-16 22:54:14 +0200357 // TODO: flip
358
359 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200360 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200361 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
362 if (wp->w_firstline < 1)
363 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200364
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200365 di = dict_find(dict, (char_u *)"scrollbar", -1);
366 if (di != NULL)
367 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
368
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200369 str = dict_get_string(dict, (char_u *)"title", FALSE);
370 if (str != NULL)
371 {
372 vim_free(wp->w_popup_title);
373 wp->w_popup_title = vim_strsave(str);
374 }
375
Bram Moolenaar402502d2019-05-30 22:07:36 +0200376 di = dict_find(dict, (char_u *)"wrap", -1);
377 if (di != NULL)
378 {
379 nr = dict_get_number(dict, (char_u *)"wrap");
380 wp->w_p_wrap = nr != 0;
381 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200382
Bram Moolenaara42d9452019-06-15 21:46:30 +0200383 di = dict_find(dict, (char_u *)"drag", -1);
384 if (di != NULL)
385 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200386
Bram Moolenaarae943152019-06-16 22:54:14 +0200387 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
388 if (str != NULL)
389 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
390 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200391
Bram Moolenaarae943152019-06-16 22:54:14 +0200392 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
393 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200394
Bram Moolenaar790498b2019-06-01 22:15:29 +0200395 di = dict_find(dict, (char_u *)"borderhighlight", -1);
396 if (di != NULL)
397 {
398 if (di->di_tv.v_type != VAR_LIST)
399 emsg(_(e_listreq));
400 else
401 {
402 list_T *list = di->di_tv.vval.v_list;
403 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200404 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200405
406 if (list != NULL)
407 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
408 ++i, li = li->li_next)
409 {
410 str = tv_get_string(&li->li_tv);
411 if (*str != NUL)
412 wp->w_border_highlight[i] = vim_strsave(str);
413 }
414 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
415 for (i = 1; i < 4; ++i)
416 wp->w_border_highlight[i] =
417 vim_strsave(wp->w_border_highlight[0]);
418 }
419 }
420
Bram Moolenaar790498b2019-06-01 22:15:29 +0200421 di = dict_find(dict, (char_u *)"borderchars", -1);
422 if (di != NULL)
423 {
424 if (di->di_tv.v_type != VAR_LIST)
425 emsg(_(e_listreq));
426 else
427 {
428 list_T *list = di->di_tv.vval.v_list;
429 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200430 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200431
432 if (list != NULL)
433 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
434 ++i, li = li->li_next)
435 {
436 str = tv_get_string(&li->li_tv);
437 if (*str != NUL)
438 wp->w_border_char[i] = mb_ptr2char(str);
439 }
440 if (list->lv_len == 1)
441 for (i = 1; i < 8; ++i)
442 wp->w_border_char[i] = wp->w_border_char[0];
443 if (list->lv_len == 2)
444 {
445 for (i = 4; i < 8; ++i)
446 wp->w_border_char[i] = wp->w_border_char[1];
447 for (i = 1; i < 4; ++i)
448 wp->w_border_char[i] = wp->w_border_char[0];
449 }
450 }
451 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200452
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200453 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
454 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
455
Bram Moolenaarae943152019-06-16 22:54:14 +0200456 di = dict_find(dict, (char_u *)"zindex", -1);
457 if (di != NULL)
458 {
459 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
460 if (wp->w_zindex < 1)
461 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
462 if (wp->w_zindex > 32000)
463 wp->w_zindex = 32000;
464 }
465
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200466 di = dict_find(dict, (char_u *)"mask", -1);
467 if (di != NULL)
468 {
469 int ok = TRUE;
470
471 if (di->di_tv.v_type != VAR_LIST)
472 ok = FALSE;
473 else if (di->di_tv.vval.v_list != NULL)
474 {
475 listitem_T *li;
476
477 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
478 li = li->li_next)
479 {
480 if (li->li_tv.v_type != VAR_LIST
481 || li->li_tv.vval.v_list == NULL
482 || li->li_tv.vval.v_list->lv_len != 4)
483 {
484 ok = FALSE;
485 break;
486 }
487 }
488 }
489 if (ok)
490 {
491 wp->w_popup_mask = di->di_tv.vval.v_list;
492 ++wp->w_popup_mask->lv_refcount;
493 }
494 else
495 semsg(_(e_invargval), "mask");
496 }
497
Bram Moolenaarae943152019-06-16 22:54:14 +0200498#if defined(FEAT_TIMERS)
499 // Add timer to close the popup after some time.
500 nr = dict_get_number(dict, (char_u *)"time");
501 if (nr > 0)
502 popup_add_timeout(wp, nr);
503#endif
504
Bram Moolenaar3397f742019-06-02 18:40:06 +0200505 di = dict_find(dict, (char_u *)"moved", -1);
506 if (di != NULL)
507 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200508 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200509 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
510 {
511 char_u *s = di->di_tv.vval.v_string;
512 int flags = 0;
513
514 if (STRCMP(s, "word") == 0)
515 flags = FIND_IDENT | FIND_STRING;
516 else if (STRCMP(s, "WORD") == 0)
517 flags = FIND_STRING;
518 else if (STRCMP(s, "any") != 0)
519 semsg(_(e_invarg2), s);
520 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200521 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200522 }
523 else if (di->di_tv.v_type == VAR_LIST
524 && di->di_tv.vval.v_list != NULL
525 && di->di_tv.vval.v_list->lv_len == 2)
526 {
527 list_T *l = di->di_tv.vval.v_list;
528
529 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
530 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
531 }
532 else
533 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
534 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200535
Bram Moolenaarae943152019-06-16 22:54:14 +0200536 di = dict_find(dict, (char_u *)"filter", -1);
537 if (di != NULL)
538 {
539 callback_T callback = get_callback(&di->di_tv);
540
541 if (callback.cb_name != NULL)
542 {
543 free_callback(&wp->w_filter_cb);
544 set_callback(&wp->w_filter_cb, &callback);
545 }
546 }
547
548 di = dict_find(dict, (char_u *)"callback", -1);
549 if (di != NULL)
550 {
551 callback_T callback = get_callback(&di->di_tv);
552
553 if (callback.cb_name != NULL)
554 {
555 free_callback(&wp->w_close_cb);
556 set_callback(&wp->w_close_cb, &callback);
557 }
558 }
559}
560
561/*
562 * Go through the options in "dict" and apply them to popup window "wp".
563 */
564 static void
565apply_options(win_T *wp, dict_T *dict)
566{
567 int nr;
568
569 apply_move_options(wp, dict);
570 apply_general_options(wp, dict);
571
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200572 nr = dict_get_number(dict, (char_u *)"hidden");
573 if (nr > 0)
574 {
575 wp->w_popup_flags |= POPF_HIDDEN;
576 --wp->w_buffer->b_nwindows;
577 }
578
Bram Moolenaar33796b32019-06-08 16:01:13 +0200579 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200580}
581
582/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200583 * Add lines to the popup from a list of strings.
584 */
585 static void
586add_popup_strings(buf_T *buf, list_T *l)
587{
588 listitem_T *li;
589 linenr_T lnum = 0;
590 char_u *p;
591
592 for (li = l->lv_first; li != NULL; li = li->li_next)
593 if (li->li_tv.v_type == VAR_STRING)
594 {
595 p = li->li_tv.vval.v_string;
596 ml_append_buf(buf, lnum++,
597 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
598 }
599}
600
601/*
602 * Add lines to the popup from a list of dictionaries.
603 */
604 static void
605add_popup_dicts(buf_T *buf, list_T *l)
606{
607 listitem_T *li;
608 listitem_T *pli;
609 linenr_T lnum = 0;
610 char_u *p;
611 dict_T *dict;
612
613 // first add the text lines
614 for (li = l->lv_first; li != NULL; li = li->li_next)
615 {
616 if (li->li_tv.v_type != VAR_DICT)
617 {
618 emsg(_(e_dictreq));
619 return;
620 }
621 dict = li->li_tv.vval.v_dict;
622 p = dict == NULL ? NULL
623 : dict_get_string(dict, (char_u *)"text", FALSE);
624 ml_append_buf(buf, lnum++,
625 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
626 }
627
628 // add the text properties
629 lnum = 1;
630 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
631 {
632 dictitem_T *di;
633 list_T *plist;
634
635 dict = li->li_tv.vval.v_dict;
636 di = dict_find(dict, (char_u *)"props", -1);
637 if (di != NULL)
638 {
639 if (di->di_tv.v_type != VAR_LIST)
640 {
641 emsg(_(e_listreq));
642 return;
643 }
644 plist = di->di_tv.vval.v_list;
645 if (plist != NULL)
646 {
647 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
648 {
649 if (pli->li_tv.v_type != VAR_DICT)
650 {
651 emsg(_(e_dictreq));
652 return;
653 }
654 dict = pli->li_tv.vval.v_dict;
655 if (dict != NULL)
656 {
657 int col = dict_get_number(dict, (char_u *)"col");
658
659 prop_add_common( lnum, col, dict, buf, NULL);
660 }
661 }
662 }
663 }
664 }
665}
666
667/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200668 * Return the height of popup window "wp", including border and padding.
669 */
670 int
671popup_height(win_T *wp)
672{
673 return wp->w_height
674 + wp->w_popup_padding[0] + wp->w_popup_border[0]
675 + wp->w_popup_padding[2] + wp->w_popup_border[2];
676}
677
678/*
679 * Return the width of popup window "wp", including border and padding.
680 */
681 int
682popup_width(win_T *wp)
683{
684 return wp->w_width
685 + wp->w_popup_padding[3] + wp->w_popup_border[3]
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200686 + wp->w_popup_padding[1] + wp->w_popup_border[1]
687 + wp->w_has_scrollbar;
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200688}
689
690/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200691 * Get the padding plus border at the top, adjusted to 1 if there is a title.
692 */
693 static int
694popup_top_extra(win_T *wp)
695{
696 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
697
698 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
699 return 1;
700 return extra;
701}
702
703/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200704 * Adjust the position and size of the popup to fit on the screen.
705 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200706 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200707popup_adjust_position(win_T *wp)
708{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200709 linenr_T lnum;
710 int wrapped = 0;
711 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200712 int center_vert = FALSE;
713 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200714 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200715 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200716 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
717 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
718 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
719 int extra_height = top_extra + bot_extra;
720 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200721 int org_winrow = wp->w_winrow;
722 int org_wincol = wp->w_wincol;
723 int org_width = wp->w_width;
724 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200725 int org_leftcol = wp->w_leftcol;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200726 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200727
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200728 wp->w_winrow = 0;
729 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200730 wp->w_leftcol = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200731 if (wp->w_popup_pos == POPPOS_CENTER)
732 {
733 // center after computing the size
734 center_vert = TRUE;
735 center_hor = TRUE;
736 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200737 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200738 {
739 if (wp->w_wantline == 0)
740 center_vert = TRUE;
741 else if (wp->w_popup_pos == POPPOS_TOPLEFT
742 || wp->w_popup_pos == POPPOS_TOPRIGHT)
743 {
744 wp->w_winrow = wp->w_wantline - 1;
745 if (wp->w_winrow >= Rows)
746 wp->w_winrow = Rows - 1;
747 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200748
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200749 if (wp->w_wantcol == 0)
750 center_hor = TRUE;
751 else if (wp->w_popup_pos == POPPOS_TOPLEFT
752 || wp->w_popup_pos == POPPOS_BOTLEFT)
753 {
754 wp->w_wincol = wp->w_wantcol - 1;
755 if (wp->w_wincol >= Columns - 3)
756 wp->w_wincol = Columns - 3;
757 }
758 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200759
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200760 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200761 // When left aligned use the space available, but shift to the left when we
762 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200763 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200764 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200765 {
766 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200767 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200768 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200769
Bram Moolenaar8d241042019-06-12 23:40:01 +0200770 // start at the desired first line
771 wp->w_topline = wp->w_firstline;
772 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
773 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
774
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200775 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200776 // Use a minimum width of one, so that something shows when there is no
777 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200778 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200779 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200780 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200781 {
782 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
783
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200784 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200785 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200786 while (len > maxwidth)
787 {
788 ++wrapped;
789 len -= maxwidth;
790 wp->w_width = maxwidth;
791 }
792 }
793 else if (len > maxwidth
794 && allow_adjust_left
795 && (wp->w_popup_pos == POPPOS_TOPLEFT
796 || wp->w_popup_pos == POPPOS_BOTLEFT))
797 {
798 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200799 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200800
Bram Moolenaar51c31312019-06-15 22:27:23 +0200801 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200802 {
803 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200804
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200805 len -= truncate_shift;
806 shift_by -= truncate_shift;
807 }
808
809 wp->w_wincol -= shift_by;
810 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200811 wp->w_width = maxwidth;
812 }
813 if (wp->w_width < len)
814 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200815 // do not use the width of lines we're not going to show
816 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
817 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
818 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200819 }
820
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200821 wp->w_has_scrollbar = wp->w_want_scrollbar
822 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
823
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200824 minwidth = wp->w_minwidth;
825 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
826 {
827 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
828
829 if (minwidth < title_len)
830 minwidth = title_len;
831 }
832
833 if (minwidth > 0 && wp->w_width < minwidth)
834 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200835 if (wp->w_width > maxwidth)
836 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200837 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200838 {
839 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
840 if (wp->w_wincol < 0)
841 wp->w_wincol = 0;
842 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200843 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
844 || wp->w_popup_pos == POPPOS_TOPRIGHT)
845 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200846 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
847
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200848 // Right aligned: move to the right if needed.
849 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200850 if (leftoff >= 0)
851 wp->w_wincol = leftoff;
852 else if (wp->w_popup_fixed)
853 {
854 // "col" specifies the right edge, but popup doesn't fit, skip some
855 // columns when displaying the window.
856 wp->w_leftcol = -leftoff;
857 wp->w_width += leftoff;
858 if (wp->w_width < 0)
859 wp->w_width = 0;
860 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200861 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200862
Bram Moolenaar8d241042019-06-12 23:40:01 +0200863 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
864 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200865 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
866 wp->w_height = wp->w_minheight;
867 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
868 wp->w_height = wp->w_maxheight;
869 if (wp->w_height > Rows - wp->w_winrow)
870 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200871
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200872 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200873 {
874 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
875 if (wp->w_winrow < 0)
876 wp->w_winrow = 0;
877 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200878 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
879 || wp->w_popup_pos == POPPOS_BOTLEFT)
880 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200881 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200882 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200883 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200884 else
885 // not enough space, make top aligned
886 wp->w_winrow = wp->w_wantline + 1;
887 }
888
Bram Moolenaar17146962019-05-30 00:12:11 +0200889 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200890
891 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200892 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200893 if (org_winrow != wp->w_winrow
894 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200895 || org_leftcol != wp->w_leftcol
Bram Moolenaar33796b32019-06-08 16:01:13 +0200896 || org_width != wp->w_width
897 || org_height != wp->w_height)
898 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200899 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200900 popup_mask_refresh = TRUE;
901 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200902}
903
Bram Moolenaar17627312019-06-02 19:53:44 +0200904typedef enum
905{
906 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200907 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200908 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200909 TYPE_DIALOG,
910 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200911} create_type_T;
912
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200913/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200914 * Make "buf" empty and set the contents to "text".
915 * Used by popup_create() and popup_settext().
916 */
917 static void
918popup_set_buffer_text(buf_T *buf, typval_T text)
919{
920 int lnum;
921
922 // Clear the buffer, then replace the lines.
923 curbuf = buf;
924 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
925 ml_delete(lnum, FALSE);
926 curbuf = curwin->w_buffer;
927
928 // Add text to the buffer.
929 if (text.v_type == VAR_STRING)
930 {
931 // just a string
932 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
933 }
934 else
935 {
936 list_T *l = text.vval.v_list;
937
938 if (l->lv_len > 0)
939 {
940 if (l->lv_first->li_tv.v_type == VAR_STRING)
941 // list of strings
942 add_popup_strings(buf, l);
943 else
944 // list of dictionaries
945 add_popup_dicts(buf, l);
946 }
947 }
948
949 // delete the line that was in the empty buffer
950 curbuf = buf;
951 ml_delete(buf->b_ml.ml_line_count, FALSE);
952 curbuf = curwin->w_buffer;
953}
954
955/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200956 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200957 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200958 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200959 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200960popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200961{
Bram Moolenaara3fce622019-06-20 02:31:49 +0200962 win_T *wp;
963 tabpage_T *tp = NULL;
964 int tabnr;
965 buf_T *buf;
966 dict_T *d;
967 int nr;
968 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200969
970 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200971 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
972 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200973 {
974 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200975 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200976 }
977 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
978 {
979 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200980 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200981 }
982 d = argvars[1].vval.v_dict;
983
Bram Moolenaara3fce622019-06-20 02:31:49 +0200984 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
985 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
986 else if (type == TYPE_NOTIFICATION)
987 tabnr = -1; // notifications are global by default
988 else
989 tabnr = 0;
990 if (tabnr > 0)
991 {
992 tp = find_tabpage(tabnr);
993 if (tp == NULL)
994 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +0200995 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +0200996 return NULL;
997 }
998 }
999
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001000 // Create the window and buffer.
1001 wp = win_alloc_popup_win();
1002 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001003 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001004 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001005 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001006
1007 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
1008 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001009 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001010 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001011
1012 win_init_popup_win(wp, buf);
1013
1014 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +02001015 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001016 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +02001017 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001018 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001019 buf->b_p_ul = -1; // no undo
1020 buf->b_p_swf = FALSE; // no swap file
1021 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001022 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001023 wp->w_p_wrap = TRUE; // 'wrap' is default on
1024
Bram Moolenaar54fabd42019-05-30 19:03:22 +02001025 // Avoid that 'buftype' is reset when this buffer is entered.
1026 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001027
Bram Moolenaara3fce622019-06-20 02:31:49 +02001028 if (tp != NULL)
1029 {
1030 // popup on specified tab page
1031 wp->w_next = tp->tp_first_popupwin;
1032 tp->tp_first_popupwin = wp;
1033 }
1034 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001035 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001036 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001037 wp->w_next = curtab->tp_first_popupwin;
1038 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001039 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001040 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001041 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001042 win_T *prev = first_popupwin;
1043
1044 // Global popup: add at the end, so that it gets displayed on top of
1045 // older ones with the same zindex. Matters for notifications.
1046 if (first_popupwin == NULL)
1047 first_popupwin = wp;
1048 else
1049 {
1050 while (prev->w_next != NULL)
1051 prev = prev->w_next;
1052 prev->w_next = wp;
1053 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001054 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001055
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001056 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001057
Bram Moolenaar17627312019-06-02 19:53:44 +02001058 if (type == TYPE_ATCURSOR)
1059 {
1060 wp->w_popup_pos = POPPOS_BOTLEFT;
1061 setcursor_mayforce(TRUE);
1062 wp->w_wantline = screen_screenrow();
1063 if (wp->w_wantline == 0) // cursor in first line
1064 {
1065 wp->w_wantline = 2;
1066 wp->w_popup_pos = POPPOS_TOPLEFT;
1067 }
1068 wp->w_wantcol = screen_screencol() + 1;
1069 set_moved_values(wp);
1070 set_moved_columns(wp, FIND_STRING);
1071 }
1072
Bram Moolenaar33796b32019-06-08 16:01:13 +02001073 // set default values
1074 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
1075
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001076 if (type == TYPE_NOTIFICATION)
1077 {
1078 win_T *twp, *nextwin;
1079 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001080
1081 // Try to not overlap with another global popup. Guess we need 3
1082 // more screen lines than buffer lines.
1083 wp->w_wantline = 1;
1084 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1085 {
1086 nextwin = twp->w_next;
1087 if (twp != wp
1088 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1089 && twp->w_winrow <= wp->w_wantline - 1 + height
1090 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1091 {
1092 // move to below this popup and restart the loop to check for
1093 // overlap with other popups
1094 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1095 nextwin = first_popupwin;
1096 }
1097 }
1098 if (wp->w_wantline + height > Rows)
1099 {
1100 // can't avoid overlap, put on top in the hope that message goes
1101 // away soon.
1102 wp->w_wantline = 1;
1103 }
1104
1105 wp->w_wantcol = 10;
1106 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001107 wp->w_minwidth = 20;
1108 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001109 for (i = 0; i < 4; ++i)
1110 wp->w_popup_border[i] = 1;
1111 wp->w_popup_padding[1] = 1;
1112 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001113
1114 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001115 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001116 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1117 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001118 }
1119
Bram Moolenaara730e552019-06-16 19:05:31 +02001120 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001121 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001122 wp->w_popup_pos = POPPOS_CENTER;
1123 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1124 wp->w_popup_drag = 1;
1125 for (i = 0; i < 4; ++i)
1126 {
1127 wp->w_popup_border[i] = 1;
1128 wp->w_popup_padding[i] = 1;
1129 }
1130 }
1131
Bram Moolenaara730e552019-06-16 19:05:31 +02001132 if (type == TYPE_MENU)
1133 {
1134 typval_T tv;
1135 callback_T callback;
1136
1137 tv.v_type = VAR_STRING;
1138 tv.vval.v_string = (char_u *)"popup_filter_menu";
1139 callback = get_callback(&tv);
1140 if (callback.cb_name != NULL)
1141 set_callback(&wp->w_filter_cb, &callback);
1142
1143 wp->w_p_wrap = 0;
1144 }
1145
Bram Moolenaarae943152019-06-16 22:54:14 +02001146 for (i = 0; i < 4; ++i)
1147 VIM_CLEAR(wp->w_border_highlight[i]);
1148 for (i = 0; i < 8; ++i)
1149 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001150 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001151 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001152
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001153 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001154 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001155
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001156#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001157 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1158 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001159#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001160
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001161 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001162
1163 wp->w_vsep_width = 0;
1164
1165 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001166 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001167
1168 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001169}
1170
1171/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001172 * popup_clear()
1173 */
1174 void
1175f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1176{
1177 close_all_popups();
1178}
1179
1180/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001181 * popup_create({text}, {options})
1182 */
1183 void
1184f_popup_create(typval_T *argvars, typval_T *rettv)
1185{
Bram Moolenaar17627312019-06-02 19:53:44 +02001186 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001187}
1188
1189/*
1190 * popup_atcursor({text}, {options})
1191 */
1192 void
1193f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1194{
Bram Moolenaar17627312019-06-02 19:53:44 +02001195 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001196}
1197
1198/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001199 * Invoke the close callback for window "wp" with value "result".
1200 * Careful: The callback may make "wp" invalid!
1201 */
1202 static void
1203invoke_popup_callback(win_T *wp, typval_T *result)
1204{
1205 typval_T rettv;
1206 int dummy;
1207 typval_T argv[3];
1208
1209 argv[0].v_type = VAR_NUMBER;
1210 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1211
1212 if (result != NULL && result->v_type != VAR_UNKNOWN)
1213 copy_tv(result, &argv[1]);
1214 else
1215 {
1216 argv[1].v_type = VAR_NUMBER;
1217 argv[1].vval.v_number = 0;
1218 }
1219
1220 argv[2].v_type = VAR_UNKNOWN;
1221
1222 call_callback(&wp->w_close_cb, -1,
1223 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1224 if (result != NULL)
1225 clear_tv(&argv[1]);
1226 clear_tv(&rettv);
1227}
1228
1229/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001230 * Close popup "wp" and invoke any close callback for it.
1231 */
1232 static void
1233popup_close_and_callback(win_T *wp, typval_T *arg)
1234{
1235 int id = wp->w_id;
1236
1237 if (wp->w_close_cb.cb_name != NULL)
1238 // Careful: This may make "wp" invalid.
1239 invoke_popup_callback(wp, arg);
1240
1241 popup_close(id);
1242}
1243
1244/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001245 * In a filter: check if the typed key is a mouse event that is used for
1246 * dragging the popup.
1247 */
1248 static void
1249filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1250{
1251 int row = mouse_row;
1252 int col = mouse_col;
1253
1254 if (wp->w_popup_drag
1255 && is_mouse_key(c)
1256 && (wp == popup_dragwin
1257 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1258 // do not consume the key, allow for dragging the popup
1259 rettv->vval.v_number = 0;
1260}
1261
1262 static void
1263popup_highlight_curline(win_T *wp)
1264{
1265 int id;
1266 char buf[100];
1267
1268 match_delete(wp, 1, FALSE);
1269
1270 id = syn_name2id((char_u *)"PopupSelected");
1271 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1272 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1273 (char_u *)buf, 10, 1, NULL, NULL);
1274}
1275
1276/*
1277 * popup_filter_menu({text}, {options})
1278 */
1279 void
1280f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1281{
1282 int id = tv_get_number(&argvars[0]);
1283 win_T *wp = win_id2wp(id);
1284 char_u *key = tv_get_string(&argvars[1]);
1285 typval_T res;
1286 int c;
1287 linenr_T old_lnum;
1288
1289 // If the popup has been closed do not consume the key.
1290 if (wp == NULL)
1291 return;
1292
1293 c = *key;
1294 if (c == K_SPECIAL && key[1] != NUL)
1295 c = TO_SPECIAL(key[1], key[2]);
1296
1297 // consume all keys until done
1298 rettv->vval.v_number = 1;
1299 res.v_type = VAR_NUMBER;
1300
1301 old_lnum = wp->w_cursor.lnum;
1302 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1303 --wp->w_cursor.lnum;
1304 if ((c == 'j' || c == 'J' || c == K_DOWN)
1305 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1306 ++wp->w_cursor.lnum;
1307 if (old_lnum != wp->w_cursor.lnum)
1308 {
1309 popup_highlight_curline(wp);
1310 return;
1311 }
1312
1313 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1314 {
1315 // Cancelled, invoke callback with -1
1316 res.vval.v_number = -1;
1317 popup_close_and_callback(wp, &res);
1318 return;
1319 }
1320 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1321 {
1322 // Invoke callback with current index.
1323 res.vval.v_number = wp->w_cursor.lnum;
1324 popup_close_and_callback(wp, &res);
1325 return;
1326 }
1327
1328 filter_handle_drag(wp, c, rettv);
1329}
1330
1331/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001332 * popup_filter_yesno({text}, {options})
1333 */
1334 void
1335f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1336{
1337 int id = tv_get_number(&argvars[0]);
1338 win_T *wp = win_id2wp(id);
1339 char_u *key = tv_get_string(&argvars[1]);
1340 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001341 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001342
1343 // If the popup has been closed don't consume the key.
1344 if (wp == NULL)
1345 return;
1346
Bram Moolenaara730e552019-06-16 19:05:31 +02001347 c = *key;
1348 if (c == K_SPECIAL && key[1] != NUL)
1349 c = TO_SPECIAL(key[1], key[2]);
1350
Bram Moolenaara42d9452019-06-15 21:46:30 +02001351 // consume all keys until done
1352 rettv->vval.v_number = 1;
1353
Bram Moolenaara730e552019-06-16 19:05:31 +02001354 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001355 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001356 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001357 res.vval.v_number = 0;
1358 else
1359 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001360 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001361 return;
1362 }
1363
1364 // Invoke callback
1365 res.v_type = VAR_NUMBER;
1366 popup_close_and_callback(wp, &res);
1367}
1368
1369/*
1370 * popup_dialog({text}, {options})
1371 */
1372 void
1373f_popup_dialog(typval_T *argvars, typval_T *rettv)
1374{
1375 popup_create(argvars, rettv, TYPE_DIALOG);
1376}
1377
1378/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001379 * popup_menu({text}, {options})
1380 */
1381 void
1382f_popup_menu(typval_T *argvars, typval_T *rettv)
1383{
1384 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1385
1386 if (wp != NULL)
1387 popup_highlight_curline(wp);
1388}
1389
1390/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001391 * popup_notification({text}, {options})
1392 */
1393 void
1394f_popup_notification(typval_T *argvars, typval_T *rettv)
1395{
1396 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1397}
1398
1399/*
1400 * Find the popup window with window-ID "id".
1401 * If the popup window does not exist NULL is returned.
1402 * If the window is not a popup window, and error message is given.
1403 */
1404 static win_T *
1405find_popup_win(int id)
1406{
1407 win_T *wp = win_id2wp(id);
1408
1409 if (wp != NULL && !bt_popup(wp->w_buffer))
1410 {
1411 semsg(_("E993: window %d is not a popup window"), id);
1412 return NULL;
1413 }
1414 return wp;
1415}
1416
1417/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001418 * popup_close({id})
1419 */
1420 void
1421f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1422{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001423 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001424 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001425
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001426 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001427 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001428}
1429
1430/*
1431 * popup_hide({id})
1432 */
1433 void
1434f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1435{
1436 int id = (int)tv_get_number(argvars);
1437 win_T *wp = find_popup_win(id);
1438
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001439 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001440 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001441 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001442 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001443 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001444 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001445 }
1446}
1447
1448/*
1449 * popup_show({id})
1450 */
1451 void
1452f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1453{
1454 int id = (int)tv_get_number(argvars);
1455 win_T *wp = find_popup_win(id);
1456
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001457 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001458 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001459 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001460 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001461 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001462 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001463 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001464}
1465
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001466/*
1467 * popup_settext({id}, {text})
1468 */
1469 void
1470f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1471{
1472 int id = (int)tv_get_number(&argvars[0]);
1473 win_T *wp = find_popup_win(id);
1474
1475 if (wp != NULL)
1476 {
1477 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1478 popup_adjust_position(wp);
1479 }
1480}
1481
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001482 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001483popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001484{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001485 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001486 if (wp->w_winrow + wp->w_height >= cmdline_row)
1487 clear_cmdline = TRUE;
1488 win_free_popup(wp);
1489 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001490 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001491}
1492
Bram Moolenaarec583842019-05-26 14:11:23 +02001493/*
1494 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001495 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001496 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001497 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001498popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001499{
1500 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001501 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001502 win_T *prev = NULL;
1503
Bram Moolenaarec583842019-05-26 14:11:23 +02001504 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001505 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001506 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001507 {
1508 if (prev == NULL)
1509 first_popupwin = wp->w_next;
1510 else
1511 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001512 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001513 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001514 }
1515
Bram Moolenaarec583842019-05-26 14:11:23 +02001516 // go through tab-local popups
1517 FOR_ALL_TABPAGES(tp)
1518 popup_close_tabpage(tp, id);
1519}
1520
1521/*
1522 * Close a popup window with Window-id "id" in tabpage "tp".
1523 */
1524 void
1525popup_close_tabpage(tabpage_T *tp, int id)
1526{
1527 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001528 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001529 win_T *prev = NULL;
1530
Bram Moolenaarec583842019-05-26 14:11:23 +02001531 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1532 if (wp->w_id == id)
1533 {
1534 if (prev == NULL)
1535 *root = wp->w_next;
1536 else
1537 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001538 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001539 return;
1540 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001541}
1542
1543 void
1544close_all_popups(void)
1545{
1546 while (first_popupwin != NULL)
1547 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001548 while (curtab->tp_first_popupwin != NULL)
1549 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001550}
1551
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001552/*
1553 * popup_move({id}, {options})
1554 */
1555 void
1556f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1557{
Bram Moolenaarae943152019-06-16 22:54:14 +02001558 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001559 int id = (int)tv_get_number(argvars);
1560 win_T *wp = find_popup_win(id);
1561
1562 if (wp == NULL)
1563 return; // invalid {id}
1564
1565 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1566 {
1567 emsg(_(e_dictreq));
1568 return;
1569 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001570 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001571
Bram Moolenaarae943152019-06-16 22:54:14 +02001572 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001573
1574 if (wp->w_winrow + wp->w_height >= cmdline_row)
1575 clear_cmdline = TRUE;
1576 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001577}
1578
Bram Moolenaarbc133542019-05-29 20:26:46 +02001579/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001580 * popup_setoptions({id}, {options})
1581 */
1582 void
1583f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1584{
1585 dict_T *dict;
1586 int id = (int)tv_get_number(argvars);
1587 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001588 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001589
1590 if (wp == NULL)
1591 return; // invalid {id}
1592
1593 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1594 {
1595 emsg(_(e_dictreq));
1596 return;
1597 }
1598 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001599 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001600
1601 apply_move_options(wp, dict);
1602 apply_general_options(wp, dict);
1603
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001604 if (old_firstline != wp->w_firstline)
1605 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001606 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001607 popup_adjust_position(wp);
1608}
1609
1610/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001611 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001612 */
1613 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001614f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001615{
1616 dict_T *dict;
1617 int id = (int)tv_get_number(argvars);
1618 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001619 int top_extra;
1620 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001621
1622 if (rettv_dict_alloc(rettv) == OK)
1623 {
1624 if (wp == NULL)
1625 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001626 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001627 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1628
Bram Moolenaarbc133542019-05-29 20:26:46 +02001629 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001630
Bram Moolenaarbc133542019-05-29 20:26:46 +02001631 dict_add_number(dict, "line", wp->w_winrow + 1);
1632 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001633 dict_add_number(dict, "width", wp->w_width + left_extra
1634 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1635 dict_add_number(dict, "height", wp->w_height + top_extra
1636 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001637
1638 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1639 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1640 dict_add_number(dict, "core_width", wp->w_width);
1641 dict_add_number(dict, "core_height", wp->w_height);
1642
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001643 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001644 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001645 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001646 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001647 }
1648}
1649
1650/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001651 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1652 */
1653 static void
1654get_padding_border(dict_T *dict, int *array, char *name)
1655{
1656 list_T *list;
1657 int i;
1658
1659 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1660 return;
1661
1662 list = list_alloc();
1663 if (list != NULL)
1664 {
1665 dict_add_list(dict, name, list);
1666 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1667 for (i = 0; i < 4; ++i)
1668 list_append_number(list, array[i]);
1669 }
1670}
1671
1672/*
1673 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1674 */
1675 static void
1676get_borderhighlight(dict_T *dict, win_T *wp)
1677{
1678 list_T *list;
1679 int i;
1680
1681 for (i = 0; i < 4; ++i)
1682 if (wp->w_border_highlight[i] != NULL)
1683 break;
1684 if (i == 4)
1685 return;
1686
1687 list = list_alloc();
1688 if (list != NULL)
1689 {
1690 dict_add_list(dict, "borderhighlight", list);
1691 for (i = 0; i < 4; ++i)
1692 list_append_string(list, wp->w_border_highlight[i], -1);
1693 }
1694}
1695
1696/*
1697 * For popup_getoptions(): add a "borderchars" entry to "dict".
1698 */
1699 static void
1700get_borderchars(dict_T *dict, win_T *wp)
1701{
1702 list_T *list;
1703 int i;
1704 char_u buf[NUMBUFLEN];
1705 int len;
1706
1707 for (i = 0; i < 8; ++i)
1708 if (wp->w_border_char[i] != 0)
1709 break;
1710 if (i == 8)
1711 return;
1712
1713 list = list_alloc();
1714 if (list != NULL)
1715 {
1716 dict_add_list(dict, "borderchars", list);
1717 for (i = 0; i < 8; ++i)
1718 {
1719 len = mb_char2bytes(wp->w_border_char[i], buf);
1720 list_append_string(list, buf, len);
1721 }
1722 }
1723}
1724
1725/*
1726 * For popup_getoptions(): add a "moved" entry to "dict".
1727 */
1728 static void
1729get_moved_list(dict_T *dict, win_T *wp)
1730{
1731 list_T *list;
1732
1733 list = list_alloc();
1734 if (list != NULL)
1735 {
1736 dict_add_list(dict, "moved", list);
1737 list_append_number(list, wp->w_popup_mincol);
1738 list_append_number(list, wp->w_popup_maxcol);
1739 }
1740}
1741
1742/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001743 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001744 */
1745 void
1746f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1747{
1748 dict_T *dict;
1749 int id = (int)tv_get_number(argvars);
1750 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001751 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001752 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001753
1754 if (rettv_dict_alloc(rettv) == OK)
1755 {
1756 if (wp == NULL)
1757 return;
1758
1759 dict = rettv->vval.v_dict;
1760 dict_add_number(dict, "line", wp->w_wantline);
1761 dict_add_number(dict, "col", wp->w_wantcol);
1762 dict_add_number(dict, "minwidth", wp->w_minwidth);
1763 dict_add_number(dict, "minheight", wp->w_minheight);
1764 dict_add_number(dict, "maxheight", wp->w_maxheight);
1765 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001766 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001767 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001768 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001769 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001770 dict_add_string(dict, "title", wp->w_popup_title);
1771 dict_add_number(dict, "wrap", wp->w_p_wrap);
1772 dict_add_number(dict, "drag", wp->w_popup_drag);
1773 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02001774 if (wp->w_scrollbar_highlight != NULL)
1775 dict_add_string(dict, "scrollbarhighlight",
1776 wp->w_scrollbar_highlight);
1777 if (wp->w_thumb_highlight != NULL)
1778 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02001779
Bram Moolenaara3fce622019-06-20 02:31:49 +02001780 // find the tabpage that holds this popup
1781 i = 1;
1782 FOR_ALL_TABPAGES(tp)
1783 {
1784 win_T *p;
1785
1786 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1787 if (p->w_id == id)
1788 break;
1789 if (p != NULL)
1790 break;
1791 ++i;
1792 }
1793 if (tp == NULL)
1794 i = -1; // must be global
1795 else if (tp == curtab)
1796 i = 0;
1797 dict_add_number(dict, "tabpage", i);
1798
Bram Moolenaarae943152019-06-16 22:54:14 +02001799 get_padding_border(dict, wp->w_popup_padding, "padding");
1800 get_padding_border(dict, wp->w_popup_border, "border");
1801 get_borderhighlight(dict, wp);
1802 get_borderchars(dict, wp);
1803 get_moved_list(dict, wp);
1804
1805 if (wp->w_filter_cb.cb_name != NULL)
1806 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1807 if (wp->w_close_cb.cb_name != NULL)
1808 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001809
1810 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1811 ++i)
1812 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1813 {
1814 dict_add_string(dict, "pos",
1815 (char_u *)poppos_entries[i].pp_name);
1816 break;
1817 }
1818
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001819# if defined(FEAT_TIMERS)
1820 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1821 ? (long)wp->w_popup_timer->tr_interval : 0L);
1822# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001823 }
1824}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001825
1826 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001827error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001828{
1829 if (bt_popup(curwin->w_buffer))
1830 {
1831 emsg(_("E994: Not allowed in a popup window"));
1832 return TRUE;
1833 }
1834 return FALSE;
1835}
1836
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001837/*
1838 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001839 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001840 */
1841 void
1842popup_reset_handled()
1843{
1844 win_T *wp;
1845
1846 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1847 wp->w_popup_flags &= ~POPF_HANDLED;
1848 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1849 wp->w_popup_flags &= ~POPF_HANDLED;
1850}
1851
1852/*
1853 * Find the next visible popup where POPF_HANDLED is not set.
1854 * Must have called popup_reset_handled() first.
1855 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1856 * popup with the highest zindex.
1857 */
1858 win_T *
1859find_next_popup(int lowest)
1860{
1861 win_T *wp;
1862 win_T *found_wp;
1863 int found_zindex;
1864
1865 found_zindex = lowest ? INT_MAX : 0;
1866 found_wp = NULL;
1867 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1868 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1869 && (lowest ? wp->w_zindex < found_zindex
1870 : wp->w_zindex > found_zindex))
1871 {
1872 found_zindex = wp->w_zindex;
1873 found_wp = wp;
1874 }
1875 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1876 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1877 && (lowest ? wp->w_zindex < found_zindex
1878 : wp->w_zindex > found_zindex))
1879 {
1880 found_zindex = wp->w_zindex;
1881 found_wp = wp;
1882 }
1883
1884 if (found_wp != NULL)
1885 found_wp->w_popup_flags |= POPF_HANDLED;
1886 return found_wp;
1887}
1888
1889/*
1890 * Invoke the filter callback for window "wp" with typed character "c".
1891 * Uses the global "mod_mask" for modifiers.
1892 * Returns the return value of the filter.
1893 * Careful: The filter may make "wp" invalid!
1894 */
1895 static int
1896invoke_popup_filter(win_T *wp, int c)
1897{
1898 int res;
1899 typval_T rettv;
1900 int dummy;
1901 typval_T argv[3];
1902 char_u buf[NUMBUFLEN];
1903
Bram Moolenaara42d9452019-06-15 21:46:30 +02001904 // Emergency exit: CTRL-C closes the popup.
1905 if (c == Ctrl_C)
1906 {
1907 rettv.v_type = VAR_NUMBER;
1908 rettv.vval.v_number = -1;
1909 popup_close_and_callback(wp, &rettv);
1910 return 1;
1911 }
1912
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001913 argv[0].v_type = VAR_NUMBER;
1914 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1915
1916 // Convert the number to a string, so that the function can use:
1917 // if a:c == "\<F2>"
1918 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1919 argv[1].v_type = VAR_STRING;
1920 argv[1].vval.v_string = vim_strsave(buf);
1921
1922 argv[2].v_type = VAR_UNKNOWN;
1923
Bram Moolenaara42d9452019-06-15 21:46:30 +02001924 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001925 call_callback(&wp->w_filter_cb, -1,
1926 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1927 res = tv_get_number(&rettv);
1928 vim_free(argv[1].vval.v_string);
1929 clear_tv(&rettv);
1930 return res;
1931}
1932
1933/*
1934 * Called when "c" was typed: invoke popup filter callbacks.
1935 * Returns TRUE when the character was consumed,
1936 */
1937 int
1938popup_do_filter(int c)
1939{
1940 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001941 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001942
1943 popup_reset_handled();
1944
1945 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1946 if (wp->w_filter_cb.cb_name != NULL)
1947 res = invoke_popup_filter(wp, c);
1948
1949 return res;
1950}
1951
Bram Moolenaar3397f742019-06-02 18:40:06 +02001952/*
1953 * Called when the cursor moved: check if any popup needs to be closed if the
1954 * cursor moved far enough.
1955 */
1956 void
1957popup_check_cursor_pos()
1958{
1959 win_T *wp;
1960 typval_T tv;
1961
1962 popup_reset_handled();
1963 while ((wp = find_next_popup(TRUE)) != NULL)
1964 if (wp->w_popup_curwin != NULL
1965 && (curwin != wp->w_popup_curwin
1966 || curwin->w_cursor.lnum != wp->w_popup_lnum
1967 || curwin->w_cursor.col < wp->w_popup_mincol
1968 || curwin->w_cursor.col > wp->w_popup_maxcol))
1969 {
1970 tv.v_type = VAR_NUMBER;
1971 tv.vval.v_number = -1;
1972 popup_close_and_callback(wp, &tv);
1973 }
1974}
1975
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001976/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001977 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
1978 * "col" and "line" are screen coordinates.
1979 */
1980 static int
1981popup_masked(win_T *wp, int screencol, int screenline)
1982{
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001983 int col = screencol - wp->w_wincol + 1 + wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001984 int line = screenline - wp->w_winrow + 1;
1985 listitem_T *lio, *li;
1986 int width, height;
1987
1988 if (wp->w_popup_mask == NULL)
1989 return FALSE;
1990 width = popup_width(wp);
1991 height = popup_height(wp);
1992
1993 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1994 {
1995 int cols, cole;
1996 int lines, linee;
1997
1998 li = lio->li_tv.vval.v_list->lv_first;
1999 cols = tv_get_number(&li->li_tv);
2000 if (cols < 0)
2001 cols = width + cols + 1;
2002 if (col < cols)
2003 continue;
2004 li = li->li_next;
2005 cole = tv_get_number(&li->li_tv);
2006 if (cole < 0)
2007 cole = width + cole + 1;
2008 if (col > cole)
2009 continue;
2010 li = li->li_next;
2011 lines = tv_get_number(&li->li_tv);
2012 if (lines < 0)
2013 lines = height + lines + 1;
2014 if (line < lines)
2015 continue;
2016 li = li->li_next;
2017 linee = tv_get_number(&li->li_tv);
2018 if (linee < 0)
2019 linee = height + linee + 1;
2020 if (line > linee)
2021 continue;
2022 return TRUE;
2023 }
2024 return FALSE;
2025}
2026
2027/*
2028 * Set flags in popup_transparent[] for window "wp" to "val".
2029 */
2030 static void
2031update_popup_transparent(win_T *wp, int val)
2032{
2033 if (wp->w_popup_mask != NULL)
2034 {
2035 int width = popup_width(wp);
2036 int height = popup_height(wp);
2037 listitem_T *lio, *li;
2038 int cols, cole;
2039 int lines, linee;
2040 int col, line;
2041
2042 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2043 {
2044 li = lio->li_tv.vval.v_list->lv_first;
2045 cols = tv_get_number(&li->li_tv);
2046 if (cols < 0)
2047 cols = width + cols + 1;
2048 li = li->li_next;
2049 cole = tv_get_number(&li->li_tv);
2050 if (cole < 0)
2051 cole = width + cole + 1;
2052 li = li->li_next;
2053 lines = tv_get_number(&li->li_tv);
2054 if (lines < 0)
2055 lines = height + lines + 1;
2056 li = li->li_next;
2057 linee = tv_get_number(&li->li_tv);
2058 if (linee < 0)
2059 linee = height + linee + 1;
2060
2061 --cols;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002062 cols -= wp->w_leftcol;
2063 if (cols < 0)
2064 cols = 0;
2065 cole -= wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002066 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002067 if (lines < 0)
2068 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002069 for (line = lines; line < linee && line < screen_Rows; ++line)
2070 for (col = cols; col < cole && col < screen_Columns; ++col)
2071 popup_transparent[(line + wp->w_winrow) * screen_Columns
2072 + col + wp->w_wincol] = val;
2073 }
2074 }
2075}
2076
2077/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002078 * Update "popup_mask" if needed.
2079 * Also recomputes the popup size and positions.
2080 * Also updates "popup_visible".
2081 * Also marks window lines for redrawing.
2082 */
2083 void
2084may_update_popup_mask(int type)
2085{
2086 win_T *wp;
2087 short *mask;
2088 int line, col;
2089 int redraw_all = FALSE;
2090
2091 // Need to recompute when switching tabs.
2092 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2093 // (such as the screen size) must have changed.
2094 if (popup_mask_tab != curtab || type >= NOT_VALID)
2095 {
2096 popup_mask_refresh = TRUE;
2097 redraw_all = TRUE;
2098 }
2099 if (!popup_mask_refresh)
2100 {
2101 // Check if any buffer has changed.
2102 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2103 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2104 popup_mask_refresh = TRUE;
2105 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2106 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2107 popup_mask_refresh = TRUE;
2108 if (!popup_mask_refresh)
2109 return;
2110 }
2111
2112 // Need to update the mask, something has changed.
2113 popup_mask_refresh = FALSE;
2114 popup_mask_tab = curtab;
2115 popup_visible = FALSE;
2116
2117 // If redrawing everything, just update "popup_mask".
2118 // If redrawing only what is needed, update "popup_mask_next" and then
2119 // compare with "popup_mask" to see what changed.
2120 if (type >= SOME_VALID)
2121 mask = popup_mask;
2122 else
2123 mask = popup_mask_next;
2124 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2125
2126 // Find the window with the lowest zindex that hasn't been handled yet,
2127 // so that the window with a higher zindex overwrites the value in
2128 // popup_mask.
2129 popup_reset_handled();
2130 while ((wp = find_next_popup(TRUE)) != NULL)
2131 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002132 int height = popup_height(wp);
2133 int width = popup_width(wp);
2134
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002135 popup_visible = TRUE;
2136
2137 // Recompute the position if the text changed.
2138 if (redraw_all
2139 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2140 popup_adjust_position(wp);
2141
2142 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002143 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002144 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002145 col < wp->w_wincol + width && col < screen_Columns; ++col)
2146 if (!popup_masked(wp, col, line))
2147 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002148 }
2149
2150 // Only check which lines are to be updated if not already
2151 // updating all lines.
2152 if (mask == popup_mask_next)
2153 for (line = 0; line < screen_Rows; ++line)
2154 {
2155 int col_done = 0;
2156
2157 for (col = 0; col < screen_Columns; ++col)
2158 {
2159 int off = line * screen_Columns + col;
2160
2161 if (popup_mask[off] != popup_mask_next[off])
2162 {
2163 popup_mask[off] = popup_mask_next[off];
2164
2165 if (line >= cmdline_row)
2166 {
2167 // the command line needs to be cleared if text below
2168 // the popup is now visible.
2169 if (!msg_scrolled && popup_mask_next[off] == 0)
2170 clear_cmdline = TRUE;
2171 }
2172 else if (col >= col_done)
2173 {
2174 linenr_T lnum;
2175 int line_cp = line;
2176 int col_cp = col;
2177
2178 // The screen position "line" / "col" needs to be
2179 // redrawn. Figure out what window that is and update
2180 // w_redraw_top and w_redr_bot. Only needs to be done
2181 // once for each window line.
2182 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2183 if (wp != NULL)
2184 {
2185 if (line_cp >= wp->w_height)
2186 // In (or below) status line
2187 wp->w_redr_status = TRUE;
2188 // compute the position in the buffer line from the
2189 // position on the screen
2190 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2191 &lnum))
2192 // past bottom
2193 wp->w_redr_status = TRUE;
2194 else
2195 redrawWinline(wp, lnum);
2196
2197 // This line is going to be redrawn, no need to
2198 // check until the right side of the window.
2199 col_done = wp->w_wincol + wp->w_width - 1;
2200 }
2201 }
2202 }
2203 }
2204 }
2205}
2206
2207/*
2208 * Return a string of "len" spaces in IObuff.
2209 */
2210 static char_u *
2211get_spaces(int len)
2212{
2213 vim_memset(IObuff, ' ', (size_t)len);
2214 IObuff[len] = NUL;
2215 return IObuff;
2216}
2217
2218/*
2219 * Update popup windows. They are drawn on top of normal windows.
2220 * "win_update" is called for each popup window, lowest zindex first.
2221 */
2222 void
2223update_popups(void (*win_update)(win_T *wp))
2224{
2225 win_T *wp;
2226 int top_off;
2227 int left_off;
2228 int total_width;
2229 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002230 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002231 int popup_attr;
2232 int border_attr[4];
2233 int border_char[8];
2234 char_u buf[MB_MAXBYTES];
2235 int row;
2236 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002237 int sb_thumb_top = 0;
2238 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002239 int attr_scroll = 0;
2240 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002241
2242 // Find the window with the lowest zindex that hasn't been updated yet,
2243 // so that the window with a higher zindex is drawn later, thus goes on
2244 // top.
2245 popup_reset_handled();
2246 while ((wp = find_next_popup(TRUE)) != NULL)
2247 {
2248 // This drawing uses the zindex of the popup window, so that it's on
2249 // top of the text but doesn't draw when another popup with higher
2250 // zindex is on top of the character.
2251 screen_zindex = wp->w_zindex;
2252
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002253 // Set flags in popup_transparent[] for masked cells.
2254 update_popup_transparent(wp, 1);
2255
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002256 // adjust w_winrow and w_wincol for border and padding, since
2257 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002258 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002259 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2260 wp->w_winrow += top_off;
2261 wp->w_wincol += left_off;
2262
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002263 // Draw the popup text, unless it's off screen.
2264 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2265 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002266
2267 wp->w_winrow -= top_off;
2268 wp->w_wincol -= left_off;
2269
2270 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002271 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1]
2272 + wp->w_has_scrollbar;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002273 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002274 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2275 popup_attr = get_wcr_attr(wp);
2276
2277 // We can only use these line drawing characters when 'encoding' is
2278 // "utf-8" and 'ambiwidth' is "single".
2279 if (enc_utf8 && *p_ambw == 's')
2280 {
2281 border_char[0] = border_char[2] = 0x2550;
2282 border_char[1] = border_char[3] = 0x2551;
2283 border_char[4] = 0x2554;
2284 border_char[5] = 0x2557;
2285 border_char[6] = 0x255d;
2286 border_char[7] = 0x255a;
2287 }
2288 else
2289 {
2290 border_char[0] = border_char[2] = '-';
2291 border_char[1] = border_char[3] = '|';
2292 for (i = 4; i < 8; ++i)
2293 border_char[i] = '+';
2294 }
2295 for (i = 0; i < 8; ++i)
2296 if (wp->w_border_char[i] != 0)
2297 border_char[i] = wp->w_border_char[i];
2298
2299 for (i = 0; i < 4; ++i)
2300 {
2301 border_attr[i] = popup_attr;
2302 if (wp->w_border_highlight[i] != NULL)
2303 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2304 }
2305
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002306 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002307 if (wp->w_popup_border[0] > 0)
2308 {
2309 // top border
2310 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2311 wp->w_wincol,
2312 wp->w_wincol + total_width,
2313 wp->w_popup_border[3] != 0
2314 ? border_char[4] : border_char[0],
2315 border_char[0], border_attr[0]);
2316 if (wp->w_popup_border[1] > 0)
2317 {
2318 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2319 screen_puts(buf, wp->w_winrow,
2320 wp->w_wincol + total_width - 1, border_attr[1]);
2321 }
2322 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002323 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2324 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002325
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002326 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002327 {
2328 // top padding
2329 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002330 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002331 wp->w_wincol + wp->w_popup_border[3],
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002332 wp->w_wincol + total_width - wp->w_popup_border[1]
2333 - wp->w_has_scrollbar,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002334 ' ', ' ', popup_attr);
2335 }
2336
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002337 // Title goes on top of border or padding.
2338 if (wp->w_popup_title != NULL)
2339 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2340 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2341
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002342 // Compute scrollbar thumb position and size.
2343 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002344 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002345 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2346
Bram Moolenaar68acb412019-06-26 03:40:36 +02002347 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2348 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002349 if (sb_thumb_height == 0)
2350 sb_thumb_height = 1;
Bram Moolenaar68acb412019-06-26 03:40:36 +02002351 sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2)
2352 * (wp->w_height - sb_thumb_height)
2353 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002354 if (wp->w_scrollbar_highlight != NULL)
2355 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2356 else
2357 attr_scroll = highlight_attr[HLF_PSB];
2358 if (wp->w_thumb_highlight != NULL)
2359 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2360 else
2361 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002362 }
2363
2364 for (i = wp->w_popup_border[0];
2365 i < total_height - wp->w_popup_border[2]; ++i)
2366 {
2367 row = wp->w_winrow + i;
2368
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002369 // left border
2370 if (wp->w_popup_border[3] > 0)
2371 {
2372 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2373 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2374 }
2375 // left padding
2376 if (wp->w_popup_padding[3] > 0)
2377 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2378 wp->w_wincol + wp->w_popup_border[3], popup_attr);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002379 // scrollbar
2380 if (wp->w_has_scrollbar)
2381 {
2382 int line = i - top_off;
2383 int scroll_col = wp->w_wincol + total_width - 1
2384 - wp->w_popup_border[1];
2385
2386 if (line >= 0 && line < wp->w_height)
2387 screen_putchar(' ', row, scroll_col,
2388 line >= sb_thumb_top
2389 && line < sb_thumb_top + sb_thumb_height
2390 ? attr_thumb : attr_scroll);
2391 else
2392 screen_putchar(' ', row, scroll_col, popup_attr);
2393 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002394 // right border
2395 if (wp->w_popup_border[1] > 0)
2396 {
2397 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2398 screen_puts(buf, row,
2399 wp->w_wincol + total_width - 1, border_attr[1]);
2400 }
2401 // right padding
2402 if (wp->w_popup_padding[1] > 0)
2403 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2404 wp->w_wincol + wp->w_popup_border[3]
2405 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2406 }
2407
2408 if (wp->w_popup_padding[2] > 0)
2409 {
2410 // bottom padding
2411 row = wp->w_winrow + wp->w_popup_border[0]
2412 + wp->w_popup_padding[0] + wp->w_height;
2413 screen_fill(row, row + wp->w_popup_padding[2],
2414 wp->w_wincol + wp->w_popup_border[3],
2415 wp->w_wincol + total_width - wp->w_popup_border[1],
2416 ' ', ' ', popup_attr);
2417 }
2418
2419 if (wp->w_popup_border[2] > 0)
2420 {
2421 // bottom border
2422 row = wp->w_winrow + total_height - 1;
2423 screen_fill(row , row + 1,
2424 wp->w_wincol,
2425 wp->w_wincol + total_width,
2426 wp->w_popup_border[3] != 0
2427 ? border_char[7] : border_char[2],
2428 border_char[2], border_attr[2]);
2429 if (wp->w_popup_border[1] > 0)
2430 {
2431 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2432 screen_puts(buf, row,
2433 wp->w_wincol + total_width - 1, border_attr[2]);
2434 }
2435 }
2436
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002437 update_popup_transparent(wp, 0);
2438
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002439 // Back to the normal zindex.
2440 screen_zindex = 0;
2441 }
2442}
2443
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002444/*
2445 * Mark references in callbacks of one popup window.
2446 */
2447 static int
2448set_ref_in_one_popup(win_T *wp, int copyID)
2449{
2450 int abort = FALSE;
2451 typval_T tv;
2452
2453 if (wp->w_close_cb.cb_partial != NULL)
2454 {
2455 tv.v_type = VAR_PARTIAL;
2456 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2457 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2458 }
2459 if (wp->w_filter_cb.cb_partial != NULL)
2460 {
2461 tv.v_type = VAR_PARTIAL;
2462 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2463 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2464 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002465 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002466 return abort;
2467}
2468
2469/*
2470 * Set reference in callbacks of popup windows.
2471 */
2472 int
2473set_ref_in_popups(int copyID)
2474{
2475 int abort = FALSE;
2476 win_T *wp;
2477 tabpage_T *tp;
2478
2479 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2480 abort = abort || set_ref_in_one_popup(wp, copyID);
2481
2482 FOR_ALL_TABPAGES(tp)
2483 {
2484 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2485 abort = abort || set_ref_in_one_popup(wp, copyID);
2486 if (abort)
2487 break;
2488 }
2489 return abort;
2490}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002491#endif // FEAT_TEXT_PROP