blob: aa0bda3c96a2b277942b2760a86ed4160116d50d [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
237#if defined(FEAT_TIMERS)
238 static void
239popup_add_timeout(win_T *wp, int time)
240{
241 char_u cbbuf[50];
242 char_u *ptr = cbbuf;
243 typval_T tv;
244
245 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
246 "{_ -> popup_close(%d)}", wp->w_id);
247 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
248 {
249 wp->w_popup_timer = create_timer(time, 0);
250 wp->w_popup_timer->tr_callback = get_callback(&tv);
251 clear_tv(&tv);
252 }
253}
254#endif
255
Bram Moolenaar17627312019-06-02 19:53:44 +0200256/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200257 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200258 */
259 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200260apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200261{
Bram Moolenaarae943152019-06-16 22:54:14 +0200262 int nr;
263
264 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
265 wp->w_minwidth = nr;
266 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
267 wp->w_minheight = nr;
268 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
269 wp->w_maxwidth = nr;
270 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
271 wp->w_maxheight = nr;
272 get_pos_options(wp, d);
273}
274
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200275 static void
276check_highlight(dict_T *dict, char *name, char_u **pval)
277{
278 dictitem_T *di;
279 char_u *str;
280
281 di = dict_find(dict, (char_u *)name, -1);
282 if (di != NULL)
283 {
284 if (di->di_tv.v_type != VAR_STRING)
285 semsg(_(e_invargval), name);
286 else
287 {
288 str = tv_get_string(&di->di_tv);
289 if (*str != NUL)
290 *pval = vim_strsave(str);
291 }
292 }
293}
294
Bram Moolenaarae943152019-06-16 22:54:14 +0200295/*
296 * Shared between popup_create() and f_popup_setoptions().
297 */
298 static void
299apply_general_options(win_T *wp, dict_T *dict)
300{
301 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200302 int nr;
303 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200304
Bram Moolenaarae943152019-06-16 22:54:14 +0200305 // TODO: flip
306
307 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200308 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200309 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
310 if (wp->w_firstline < 1)
311 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200312
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200313 di = dict_find(dict, (char_u *)"scrollbar", -1);
314 if (di != NULL)
315 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
316
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200317 str = dict_get_string(dict, (char_u *)"title", FALSE);
318 if (str != NULL)
319 {
320 vim_free(wp->w_popup_title);
321 wp->w_popup_title = vim_strsave(str);
322 }
323
Bram Moolenaar402502d2019-05-30 22:07:36 +0200324 di = dict_find(dict, (char_u *)"wrap", -1);
325 if (di != NULL)
326 {
327 nr = dict_get_number(dict, (char_u *)"wrap");
328 wp->w_p_wrap = nr != 0;
329 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200330
Bram Moolenaara42d9452019-06-15 21:46:30 +0200331 di = dict_find(dict, (char_u *)"drag", -1);
332 if (di != NULL)
333 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200334
Bram Moolenaarae943152019-06-16 22:54:14 +0200335 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
336 if (str != NULL)
337 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
338 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200339
Bram Moolenaarae943152019-06-16 22:54:14 +0200340 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
341 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200342
Bram Moolenaar790498b2019-06-01 22:15:29 +0200343 di = dict_find(dict, (char_u *)"borderhighlight", -1);
344 if (di != NULL)
345 {
346 if (di->di_tv.v_type != VAR_LIST)
347 emsg(_(e_listreq));
348 else
349 {
350 list_T *list = di->di_tv.vval.v_list;
351 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200352 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200353
354 if (list != NULL)
355 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
356 ++i, li = li->li_next)
357 {
358 str = tv_get_string(&li->li_tv);
359 if (*str != NUL)
360 wp->w_border_highlight[i] = vim_strsave(str);
361 }
362 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
363 for (i = 1; i < 4; ++i)
364 wp->w_border_highlight[i] =
365 vim_strsave(wp->w_border_highlight[0]);
366 }
367 }
368
Bram Moolenaar790498b2019-06-01 22:15:29 +0200369 di = dict_find(dict, (char_u *)"borderchars", -1);
370 if (di != NULL)
371 {
372 if (di->di_tv.v_type != VAR_LIST)
373 emsg(_(e_listreq));
374 else
375 {
376 list_T *list = di->di_tv.vval.v_list;
377 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200378 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200379
380 if (list != NULL)
381 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
382 ++i, li = li->li_next)
383 {
384 str = tv_get_string(&li->li_tv);
385 if (*str != NUL)
386 wp->w_border_char[i] = mb_ptr2char(str);
387 }
388 if (list->lv_len == 1)
389 for (i = 1; i < 8; ++i)
390 wp->w_border_char[i] = wp->w_border_char[0];
391 if (list->lv_len == 2)
392 {
393 for (i = 4; i < 8; ++i)
394 wp->w_border_char[i] = wp->w_border_char[1];
395 for (i = 1; i < 4; ++i)
396 wp->w_border_char[i] = wp->w_border_char[0];
397 }
398 }
399 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200400
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200401 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
402 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
403
Bram Moolenaarae943152019-06-16 22:54:14 +0200404 di = dict_find(dict, (char_u *)"zindex", -1);
405 if (di != NULL)
406 {
407 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
408 if (wp->w_zindex < 1)
409 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
410 if (wp->w_zindex > 32000)
411 wp->w_zindex = 32000;
412 }
413
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200414 di = dict_find(dict, (char_u *)"mask", -1);
415 if (di != NULL)
416 {
417 int ok = TRUE;
418
419 if (di->di_tv.v_type != VAR_LIST)
420 ok = FALSE;
421 else if (di->di_tv.vval.v_list != NULL)
422 {
423 listitem_T *li;
424
425 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
426 li = li->li_next)
427 {
428 if (li->li_tv.v_type != VAR_LIST
429 || li->li_tv.vval.v_list == NULL
430 || li->li_tv.vval.v_list->lv_len != 4)
431 {
432 ok = FALSE;
433 break;
434 }
435 }
436 }
437 if (ok)
438 {
439 wp->w_popup_mask = di->di_tv.vval.v_list;
440 ++wp->w_popup_mask->lv_refcount;
441 }
442 else
443 semsg(_(e_invargval), "mask");
444 }
445
Bram Moolenaarae943152019-06-16 22:54:14 +0200446#if defined(FEAT_TIMERS)
447 // Add timer to close the popup after some time.
448 nr = dict_get_number(dict, (char_u *)"time");
449 if (nr > 0)
450 popup_add_timeout(wp, nr);
451#endif
452
Bram Moolenaar3397f742019-06-02 18:40:06 +0200453 di = dict_find(dict, (char_u *)"moved", -1);
454 if (di != NULL)
455 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200456 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200457 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
458 {
459 char_u *s = di->di_tv.vval.v_string;
460 int flags = 0;
461
462 if (STRCMP(s, "word") == 0)
463 flags = FIND_IDENT | FIND_STRING;
464 else if (STRCMP(s, "WORD") == 0)
465 flags = FIND_STRING;
466 else if (STRCMP(s, "any") != 0)
467 semsg(_(e_invarg2), s);
468 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200469 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200470 }
471 else if (di->di_tv.v_type == VAR_LIST
472 && di->di_tv.vval.v_list != NULL
473 && di->di_tv.vval.v_list->lv_len == 2)
474 {
475 list_T *l = di->di_tv.vval.v_list;
476
477 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
478 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
479 }
480 else
481 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
482 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200483
Bram Moolenaarae943152019-06-16 22:54:14 +0200484 di = dict_find(dict, (char_u *)"filter", -1);
485 if (di != NULL)
486 {
487 callback_T callback = get_callback(&di->di_tv);
488
489 if (callback.cb_name != NULL)
490 {
491 free_callback(&wp->w_filter_cb);
492 set_callback(&wp->w_filter_cb, &callback);
493 }
494 }
495
496 di = dict_find(dict, (char_u *)"callback", -1);
497 if (di != NULL)
498 {
499 callback_T callback = get_callback(&di->di_tv);
500
501 if (callback.cb_name != NULL)
502 {
503 free_callback(&wp->w_close_cb);
504 set_callback(&wp->w_close_cb, &callback);
505 }
506 }
507}
508
509/*
510 * Go through the options in "dict" and apply them to popup window "wp".
511 */
512 static void
513apply_options(win_T *wp, dict_T *dict)
514{
515 int nr;
516
517 apply_move_options(wp, dict);
518 apply_general_options(wp, dict);
519
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200520 nr = dict_get_number(dict, (char_u *)"hidden");
521 if (nr > 0)
522 {
523 wp->w_popup_flags |= POPF_HIDDEN;
524 --wp->w_buffer->b_nwindows;
525 }
526
Bram Moolenaar33796b32019-06-08 16:01:13 +0200527 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200528}
529
530/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200531 * Add lines to the popup from a list of strings.
532 */
533 static void
534add_popup_strings(buf_T *buf, list_T *l)
535{
536 listitem_T *li;
537 linenr_T lnum = 0;
538 char_u *p;
539
540 for (li = l->lv_first; li != NULL; li = li->li_next)
541 if (li->li_tv.v_type == VAR_STRING)
542 {
543 p = li->li_tv.vval.v_string;
544 ml_append_buf(buf, lnum++,
545 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
546 }
547}
548
549/*
550 * Add lines to the popup from a list of dictionaries.
551 */
552 static void
553add_popup_dicts(buf_T *buf, list_T *l)
554{
555 listitem_T *li;
556 listitem_T *pli;
557 linenr_T lnum = 0;
558 char_u *p;
559 dict_T *dict;
560
561 // first add the text lines
562 for (li = l->lv_first; li != NULL; li = li->li_next)
563 {
564 if (li->li_tv.v_type != VAR_DICT)
565 {
566 emsg(_(e_dictreq));
567 return;
568 }
569 dict = li->li_tv.vval.v_dict;
570 p = dict == NULL ? NULL
571 : dict_get_string(dict, (char_u *)"text", FALSE);
572 ml_append_buf(buf, lnum++,
573 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
574 }
575
576 // add the text properties
577 lnum = 1;
578 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
579 {
580 dictitem_T *di;
581 list_T *plist;
582
583 dict = li->li_tv.vval.v_dict;
584 di = dict_find(dict, (char_u *)"props", -1);
585 if (di != NULL)
586 {
587 if (di->di_tv.v_type != VAR_LIST)
588 {
589 emsg(_(e_listreq));
590 return;
591 }
592 plist = di->di_tv.vval.v_list;
593 if (plist != NULL)
594 {
595 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
596 {
597 if (pli->li_tv.v_type != VAR_DICT)
598 {
599 emsg(_(e_dictreq));
600 return;
601 }
602 dict = pli->li_tv.vval.v_dict;
603 if (dict != NULL)
604 {
605 int col = dict_get_number(dict, (char_u *)"col");
606
607 prop_add_common( lnum, col, dict, buf, NULL);
608 }
609 }
610 }
611 }
612 }
613}
614
615/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200616 * Return the height of popup window "wp", including border and padding.
617 */
618 int
619popup_height(win_T *wp)
620{
621 return wp->w_height
622 + wp->w_popup_padding[0] + wp->w_popup_border[0]
623 + wp->w_popup_padding[2] + wp->w_popup_border[2];
624}
625
626/*
627 * Return the width of popup window "wp", including border and padding.
628 */
629 int
630popup_width(win_T *wp)
631{
632 return wp->w_width
633 + wp->w_popup_padding[3] + wp->w_popup_border[3]
634 + wp->w_popup_padding[1] + wp->w_popup_border[1];
635}
636
637/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200638 * Get the padding plus border at the top, adjusted to 1 if there is a title.
639 */
640 static int
641popup_top_extra(win_T *wp)
642{
643 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
644
645 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
646 return 1;
647 return extra;
648}
649
650/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200651 * Adjust the position and size of the popup to fit on the screen.
652 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200653 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200654popup_adjust_position(win_T *wp)
655{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200656 linenr_T lnum;
657 int wrapped = 0;
658 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200659 int center_vert = FALSE;
660 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200661 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200662 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200663 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
664 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
665 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
666 int extra_height = top_extra + bot_extra;
667 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200668 int org_winrow = wp->w_winrow;
669 int org_wincol = wp->w_wincol;
670 int org_width = wp->w_width;
671 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200672 int org_leftcol = wp->w_leftcol;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200673 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200674
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200675 wp->w_winrow = 0;
676 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200677 wp->w_leftcol = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200678 if (wp->w_popup_pos == POPPOS_CENTER)
679 {
680 // center after computing the size
681 center_vert = TRUE;
682 center_hor = TRUE;
683 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200684 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200685 {
686 if (wp->w_wantline == 0)
687 center_vert = TRUE;
688 else if (wp->w_popup_pos == POPPOS_TOPLEFT
689 || wp->w_popup_pos == POPPOS_TOPRIGHT)
690 {
691 wp->w_winrow = wp->w_wantline - 1;
692 if (wp->w_winrow >= Rows)
693 wp->w_winrow = Rows - 1;
694 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200695
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200696 if (wp->w_wantcol == 0)
697 center_hor = TRUE;
698 else if (wp->w_popup_pos == POPPOS_TOPLEFT
699 || wp->w_popup_pos == POPPOS_BOTLEFT)
700 {
701 wp->w_wincol = wp->w_wantcol - 1;
702 if (wp->w_wincol >= Columns - 3)
703 wp->w_wincol = Columns - 3;
704 }
705 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200706
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200707 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200708 // When left aligned use the space available, but shift to the left when we
709 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200710 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200711 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200712 {
713 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200714 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200715 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200716
Bram Moolenaar8d241042019-06-12 23:40:01 +0200717 // start at the desired first line
718 wp->w_topline = wp->w_firstline;
719 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
720 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
721
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200722 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200723 // Use a minimum width of one, so that something shows when there is no
724 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200725 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200726 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200727 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200728 {
729 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
730
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200731 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200732 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200733 while (len > maxwidth)
734 {
735 ++wrapped;
736 len -= maxwidth;
737 wp->w_width = maxwidth;
738 }
739 }
740 else if (len > maxwidth
741 && allow_adjust_left
742 && (wp->w_popup_pos == POPPOS_TOPLEFT
743 || wp->w_popup_pos == POPPOS_BOTLEFT))
744 {
745 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200746 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200747
Bram Moolenaar51c31312019-06-15 22:27:23 +0200748 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200749 {
750 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200751
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200752 len -= truncate_shift;
753 shift_by -= truncate_shift;
754 }
755
756 wp->w_wincol -= shift_by;
757 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200758 wp->w_width = maxwidth;
759 }
760 if (wp->w_width < len)
761 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200762 // do not use the width of lines we're not going to show
763 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
764 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
765 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200766 }
767
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200768 wp->w_has_scrollbar = wp->w_want_scrollbar
769 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
770
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200771 minwidth = wp->w_minwidth;
772 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
773 {
774 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
775
776 if (minwidth < title_len)
777 minwidth = title_len;
778 }
779
780 if (minwidth > 0 && wp->w_width < minwidth)
781 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200782 if (wp->w_width > maxwidth)
783 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200784 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200785 {
786 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
787 if (wp->w_wincol < 0)
788 wp->w_wincol = 0;
789 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200790 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
791 || wp->w_popup_pos == POPPOS_TOPRIGHT)
792 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200793 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
794
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200795 // Right aligned: move to the right if needed.
796 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200797 if (leftoff >= 0)
798 wp->w_wincol = leftoff;
799 else if (wp->w_popup_fixed)
800 {
801 // "col" specifies the right edge, but popup doesn't fit, skip some
802 // columns when displaying the window.
803 wp->w_leftcol = -leftoff;
804 wp->w_width += leftoff;
805 if (wp->w_width < 0)
806 wp->w_width = 0;
807 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200808 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200809
Bram Moolenaar8d241042019-06-12 23:40:01 +0200810 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
811 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200812 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
813 wp->w_height = wp->w_minheight;
814 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
815 wp->w_height = wp->w_maxheight;
816 if (wp->w_height > Rows - wp->w_winrow)
817 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200818
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200819 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200820 {
821 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
822 if (wp->w_winrow < 0)
823 wp->w_winrow = 0;
824 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200825 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
826 || wp->w_popup_pos == POPPOS_BOTLEFT)
827 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200828 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200829 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200830 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200831 else
832 // not enough space, make top aligned
833 wp->w_winrow = wp->w_wantline + 1;
834 }
835
Bram Moolenaar17146962019-05-30 00:12:11 +0200836 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200837
838 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200839 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200840 if (org_winrow != wp->w_winrow
841 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200842 || org_leftcol != wp->w_leftcol
Bram Moolenaar33796b32019-06-08 16:01:13 +0200843 || org_width != wp->w_width
844 || org_height != wp->w_height)
845 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200846 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200847 popup_mask_refresh = TRUE;
848 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200849}
850
Bram Moolenaar17627312019-06-02 19:53:44 +0200851typedef enum
852{
853 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200854 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200855 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200856 TYPE_DIALOG,
857 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200858} create_type_T;
859
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200860/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200861 * Make "buf" empty and set the contents to "text".
862 * Used by popup_create() and popup_settext().
863 */
864 static void
865popup_set_buffer_text(buf_T *buf, typval_T text)
866{
867 int lnum;
868
869 // Clear the buffer, then replace the lines.
870 curbuf = buf;
871 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
872 ml_delete(lnum, FALSE);
873 curbuf = curwin->w_buffer;
874
875 // Add text to the buffer.
876 if (text.v_type == VAR_STRING)
877 {
878 // just a string
879 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
880 }
881 else
882 {
883 list_T *l = text.vval.v_list;
884
885 if (l->lv_len > 0)
886 {
887 if (l->lv_first->li_tv.v_type == VAR_STRING)
888 // list of strings
889 add_popup_strings(buf, l);
890 else
891 // list of dictionaries
892 add_popup_dicts(buf, l);
893 }
894 }
895
896 // delete the line that was in the empty buffer
897 curbuf = buf;
898 ml_delete(buf->b_ml.ml_line_count, FALSE);
899 curbuf = curwin->w_buffer;
900}
901
902/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200903 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200904 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200905 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200906 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200907popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200908{
Bram Moolenaara3fce622019-06-20 02:31:49 +0200909 win_T *wp;
910 tabpage_T *tp = NULL;
911 int tabnr;
912 buf_T *buf;
913 dict_T *d;
914 int nr;
915 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200916
917 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200918 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
919 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200920 {
921 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200922 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200923 }
924 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
925 {
926 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200927 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200928 }
929 d = argvars[1].vval.v_dict;
930
Bram Moolenaara3fce622019-06-20 02:31:49 +0200931 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
932 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
933 else if (type == TYPE_NOTIFICATION)
934 tabnr = -1; // notifications are global by default
935 else
936 tabnr = 0;
937 if (tabnr > 0)
938 {
939 tp = find_tabpage(tabnr);
940 if (tp == NULL)
941 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +0200942 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +0200943 return NULL;
944 }
945 }
946
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200947 // Create the window and buffer.
948 wp = win_alloc_popup_win();
949 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200950 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200951 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200952 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200953
954 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
955 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200956 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200957 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200958
959 win_init_popup_win(wp, buf);
960
961 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200962 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200963 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200964 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200965 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200966 buf->b_p_ul = -1; // no undo
967 buf->b_p_swf = FALSE; // no swap file
968 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200969 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200970 wp->w_p_wrap = TRUE; // 'wrap' is default on
971
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200972 // Avoid that 'buftype' is reset when this buffer is entered.
973 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200974
Bram Moolenaara3fce622019-06-20 02:31:49 +0200975 if (tp != NULL)
976 {
977 // popup on specified tab page
978 wp->w_next = tp->tp_first_popupwin;
979 tp->tp_first_popupwin = wp;
980 }
981 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200982 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200983 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200984 wp->w_next = curtab->tp_first_popupwin;
985 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200986 }
Bram Moolenaara3fce622019-06-20 02:31:49 +0200987 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200988 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200989 win_T *prev = first_popupwin;
990
991 // Global popup: add at the end, so that it gets displayed on top of
992 // older ones with the same zindex. Matters for notifications.
993 if (first_popupwin == NULL)
994 first_popupwin = wp;
995 else
996 {
997 while (prev->w_next != NULL)
998 prev = prev->w_next;
999 prev->w_next = wp;
1000 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001001 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001002
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001003 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001004
Bram Moolenaar17627312019-06-02 19:53:44 +02001005 if (type == TYPE_ATCURSOR)
1006 {
1007 wp->w_popup_pos = POPPOS_BOTLEFT;
1008 setcursor_mayforce(TRUE);
1009 wp->w_wantline = screen_screenrow();
1010 if (wp->w_wantline == 0) // cursor in first line
1011 {
1012 wp->w_wantline = 2;
1013 wp->w_popup_pos = POPPOS_TOPLEFT;
1014 }
1015 wp->w_wantcol = screen_screencol() + 1;
1016 set_moved_values(wp);
1017 set_moved_columns(wp, FIND_STRING);
1018 }
1019
Bram Moolenaar33796b32019-06-08 16:01:13 +02001020 // set default values
1021 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
1022
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001023 if (type == TYPE_NOTIFICATION)
1024 {
1025 win_T *twp, *nextwin;
1026 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001027
1028 // Try to not overlap with another global popup. Guess we need 3
1029 // more screen lines than buffer lines.
1030 wp->w_wantline = 1;
1031 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1032 {
1033 nextwin = twp->w_next;
1034 if (twp != wp
1035 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1036 && twp->w_winrow <= wp->w_wantline - 1 + height
1037 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1038 {
1039 // move to below this popup and restart the loop to check for
1040 // overlap with other popups
1041 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1042 nextwin = first_popupwin;
1043 }
1044 }
1045 if (wp->w_wantline + height > Rows)
1046 {
1047 // can't avoid overlap, put on top in the hope that message goes
1048 // away soon.
1049 wp->w_wantline = 1;
1050 }
1051
1052 wp->w_wantcol = 10;
1053 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001054 wp->w_minwidth = 20;
1055 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001056 for (i = 0; i < 4; ++i)
1057 wp->w_popup_border[i] = 1;
1058 wp->w_popup_padding[1] = 1;
1059 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001060
1061 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001062 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001063 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1064 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001065 }
1066
Bram Moolenaara730e552019-06-16 19:05:31 +02001067 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001068 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001069 wp->w_popup_pos = POPPOS_CENTER;
1070 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1071 wp->w_popup_drag = 1;
1072 for (i = 0; i < 4; ++i)
1073 {
1074 wp->w_popup_border[i] = 1;
1075 wp->w_popup_padding[i] = 1;
1076 }
1077 }
1078
Bram Moolenaara730e552019-06-16 19:05:31 +02001079 if (type == TYPE_MENU)
1080 {
1081 typval_T tv;
1082 callback_T callback;
1083
1084 tv.v_type = VAR_STRING;
1085 tv.vval.v_string = (char_u *)"popup_filter_menu";
1086 callback = get_callback(&tv);
1087 if (callback.cb_name != NULL)
1088 set_callback(&wp->w_filter_cb, &callback);
1089
1090 wp->w_p_wrap = 0;
1091 }
1092
Bram Moolenaarae943152019-06-16 22:54:14 +02001093 for (i = 0; i < 4; ++i)
1094 VIM_CLEAR(wp->w_border_highlight[i]);
1095 for (i = 0; i < 8; ++i)
1096 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001097 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001098 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001099
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001100 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001101 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001102
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001103#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001104 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1105 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001106#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001107
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001108 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001109
1110 wp->w_vsep_width = 0;
1111
1112 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001113 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001114
1115 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001116}
1117
1118/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001119 * popup_clear()
1120 */
1121 void
1122f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1123{
1124 close_all_popups();
1125}
1126
1127/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001128 * popup_create({text}, {options})
1129 */
1130 void
1131f_popup_create(typval_T *argvars, typval_T *rettv)
1132{
Bram Moolenaar17627312019-06-02 19:53:44 +02001133 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001134}
1135
1136/*
1137 * popup_atcursor({text}, {options})
1138 */
1139 void
1140f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1141{
Bram Moolenaar17627312019-06-02 19:53:44 +02001142 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001143}
1144
1145/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001146 * Invoke the close callback for window "wp" with value "result".
1147 * Careful: The callback may make "wp" invalid!
1148 */
1149 static void
1150invoke_popup_callback(win_T *wp, typval_T *result)
1151{
1152 typval_T rettv;
1153 int dummy;
1154 typval_T argv[3];
1155
1156 argv[0].v_type = VAR_NUMBER;
1157 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1158
1159 if (result != NULL && result->v_type != VAR_UNKNOWN)
1160 copy_tv(result, &argv[1]);
1161 else
1162 {
1163 argv[1].v_type = VAR_NUMBER;
1164 argv[1].vval.v_number = 0;
1165 }
1166
1167 argv[2].v_type = VAR_UNKNOWN;
1168
1169 call_callback(&wp->w_close_cb, -1,
1170 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1171 if (result != NULL)
1172 clear_tv(&argv[1]);
1173 clear_tv(&rettv);
1174}
1175
1176/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001177 * Close popup "wp" and invoke any close callback for it.
1178 */
1179 static void
1180popup_close_and_callback(win_T *wp, typval_T *arg)
1181{
1182 int id = wp->w_id;
1183
1184 if (wp->w_close_cb.cb_name != NULL)
1185 // Careful: This may make "wp" invalid.
1186 invoke_popup_callback(wp, arg);
1187
1188 popup_close(id);
1189}
1190
1191/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001192 * In a filter: check if the typed key is a mouse event that is used for
1193 * dragging the popup.
1194 */
1195 static void
1196filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1197{
1198 int row = mouse_row;
1199 int col = mouse_col;
1200
1201 if (wp->w_popup_drag
1202 && is_mouse_key(c)
1203 && (wp == popup_dragwin
1204 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1205 // do not consume the key, allow for dragging the popup
1206 rettv->vval.v_number = 0;
1207}
1208
1209 static void
1210popup_highlight_curline(win_T *wp)
1211{
1212 int id;
1213 char buf[100];
1214
1215 match_delete(wp, 1, FALSE);
1216
1217 id = syn_name2id((char_u *)"PopupSelected");
1218 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1219 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1220 (char_u *)buf, 10, 1, NULL, NULL);
1221}
1222
1223/*
1224 * popup_filter_menu({text}, {options})
1225 */
1226 void
1227f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1228{
1229 int id = tv_get_number(&argvars[0]);
1230 win_T *wp = win_id2wp(id);
1231 char_u *key = tv_get_string(&argvars[1]);
1232 typval_T res;
1233 int c;
1234 linenr_T old_lnum;
1235
1236 // If the popup has been closed do not consume the key.
1237 if (wp == NULL)
1238 return;
1239
1240 c = *key;
1241 if (c == K_SPECIAL && key[1] != NUL)
1242 c = TO_SPECIAL(key[1], key[2]);
1243
1244 // consume all keys until done
1245 rettv->vval.v_number = 1;
1246 res.v_type = VAR_NUMBER;
1247
1248 old_lnum = wp->w_cursor.lnum;
1249 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1250 --wp->w_cursor.lnum;
1251 if ((c == 'j' || c == 'J' || c == K_DOWN)
1252 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1253 ++wp->w_cursor.lnum;
1254 if (old_lnum != wp->w_cursor.lnum)
1255 {
1256 popup_highlight_curline(wp);
1257 return;
1258 }
1259
1260 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1261 {
1262 // Cancelled, invoke callback with -1
1263 res.vval.v_number = -1;
1264 popup_close_and_callback(wp, &res);
1265 return;
1266 }
1267 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1268 {
1269 // Invoke callback with current index.
1270 res.vval.v_number = wp->w_cursor.lnum;
1271 popup_close_and_callback(wp, &res);
1272 return;
1273 }
1274
1275 filter_handle_drag(wp, c, rettv);
1276}
1277
1278/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001279 * popup_filter_yesno({text}, {options})
1280 */
1281 void
1282f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1283{
1284 int id = tv_get_number(&argvars[0]);
1285 win_T *wp = win_id2wp(id);
1286 char_u *key = tv_get_string(&argvars[1]);
1287 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001288 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001289
1290 // If the popup has been closed don't consume the key.
1291 if (wp == NULL)
1292 return;
1293
Bram Moolenaara730e552019-06-16 19:05:31 +02001294 c = *key;
1295 if (c == K_SPECIAL && key[1] != NUL)
1296 c = TO_SPECIAL(key[1], key[2]);
1297
Bram Moolenaara42d9452019-06-15 21:46:30 +02001298 // consume all keys until done
1299 rettv->vval.v_number = 1;
1300
Bram Moolenaara730e552019-06-16 19:05:31 +02001301 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001302 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001303 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001304 res.vval.v_number = 0;
1305 else
1306 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001307 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001308 return;
1309 }
1310
1311 // Invoke callback
1312 res.v_type = VAR_NUMBER;
1313 popup_close_and_callback(wp, &res);
1314}
1315
1316/*
1317 * popup_dialog({text}, {options})
1318 */
1319 void
1320f_popup_dialog(typval_T *argvars, typval_T *rettv)
1321{
1322 popup_create(argvars, rettv, TYPE_DIALOG);
1323}
1324
1325/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001326 * popup_menu({text}, {options})
1327 */
1328 void
1329f_popup_menu(typval_T *argvars, typval_T *rettv)
1330{
1331 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1332
1333 if (wp != NULL)
1334 popup_highlight_curline(wp);
1335}
1336
1337/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001338 * popup_notification({text}, {options})
1339 */
1340 void
1341f_popup_notification(typval_T *argvars, typval_T *rettv)
1342{
1343 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1344}
1345
1346/*
1347 * Find the popup window with window-ID "id".
1348 * If the popup window does not exist NULL is returned.
1349 * If the window is not a popup window, and error message is given.
1350 */
1351 static win_T *
1352find_popup_win(int id)
1353{
1354 win_T *wp = win_id2wp(id);
1355
1356 if (wp != NULL && !bt_popup(wp->w_buffer))
1357 {
1358 semsg(_("E993: window %d is not a popup window"), id);
1359 return NULL;
1360 }
1361 return wp;
1362}
1363
1364/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001365 * popup_close({id})
1366 */
1367 void
1368f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1369{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001370 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001371 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001372
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001373 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001374 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001375}
1376
1377/*
1378 * popup_hide({id})
1379 */
1380 void
1381f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1382{
1383 int id = (int)tv_get_number(argvars);
1384 win_T *wp = find_popup_win(id);
1385
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001386 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001387 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001388 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001389 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001390 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001391 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001392 }
1393}
1394
1395/*
1396 * popup_show({id})
1397 */
1398 void
1399f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1400{
1401 int id = (int)tv_get_number(argvars);
1402 win_T *wp = find_popup_win(id);
1403
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001404 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001405 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001406 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001407 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001408 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001409 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001410 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001411}
1412
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001413/*
1414 * popup_settext({id}, {text})
1415 */
1416 void
1417f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1418{
1419 int id = (int)tv_get_number(&argvars[0]);
1420 win_T *wp = find_popup_win(id);
1421
1422 if (wp != NULL)
1423 {
1424 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1425 popup_adjust_position(wp);
1426 }
1427}
1428
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001429 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001430popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001431{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001432 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001433 if (wp->w_winrow + wp->w_height >= cmdline_row)
1434 clear_cmdline = TRUE;
1435 win_free_popup(wp);
1436 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001437 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001438}
1439
Bram Moolenaarec583842019-05-26 14:11:23 +02001440/*
1441 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001442 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001443 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001444 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001445popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001446{
1447 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001448 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001449 win_T *prev = NULL;
1450
Bram Moolenaarec583842019-05-26 14:11:23 +02001451 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001452 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001453 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001454 {
1455 if (prev == NULL)
1456 first_popupwin = wp->w_next;
1457 else
1458 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001459 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001460 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001461 }
1462
Bram Moolenaarec583842019-05-26 14:11:23 +02001463 // go through tab-local popups
1464 FOR_ALL_TABPAGES(tp)
1465 popup_close_tabpage(tp, id);
1466}
1467
1468/*
1469 * Close a popup window with Window-id "id" in tabpage "tp".
1470 */
1471 void
1472popup_close_tabpage(tabpage_T *tp, int id)
1473{
1474 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001475 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001476 win_T *prev = NULL;
1477
Bram Moolenaarec583842019-05-26 14:11:23 +02001478 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1479 if (wp->w_id == id)
1480 {
1481 if (prev == NULL)
1482 *root = wp->w_next;
1483 else
1484 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001485 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001486 return;
1487 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001488}
1489
1490 void
1491close_all_popups(void)
1492{
1493 while (first_popupwin != NULL)
1494 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001495 while (curtab->tp_first_popupwin != NULL)
1496 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001497}
1498
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001499/*
1500 * popup_move({id}, {options})
1501 */
1502 void
1503f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1504{
Bram Moolenaarae943152019-06-16 22:54:14 +02001505 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001506 int id = (int)tv_get_number(argvars);
1507 win_T *wp = find_popup_win(id);
1508
1509 if (wp == NULL)
1510 return; // invalid {id}
1511
1512 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1513 {
1514 emsg(_(e_dictreq));
1515 return;
1516 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001517 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001518
Bram Moolenaarae943152019-06-16 22:54:14 +02001519 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001520
1521 if (wp->w_winrow + wp->w_height >= cmdline_row)
1522 clear_cmdline = TRUE;
1523 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001524}
1525
Bram Moolenaarbc133542019-05-29 20:26:46 +02001526/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001527 * popup_setoptions({id}, {options})
1528 */
1529 void
1530f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1531{
1532 dict_T *dict;
1533 int id = (int)tv_get_number(argvars);
1534 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001535 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001536
1537 if (wp == NULL)
1538 return; // invalid {id}
1539
1540 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1541 {
1542 emsg(_(e_dictreq));
1543 return;
1544 }
1545 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001546 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001547
1548 apply_move_options(wp, dict);
1549 apply_general_options(wp, dict);
1550
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001551 if (old_firstline != wp->w_firstline)
1552 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001553 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001554 popup_adjust_position(wp);
1555}
1556
1557/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001558 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001559 */
1560 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001561f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001562{
1563 dict_T *dict;
1564 int id = (int)tv_get_number(argvars);
1565 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001566 int top_extra;
1567 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001568
1569 if (rettv_dict_alloc(rettv) == OK)
1570 {
1571 if (wp == NULL)
1572 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001573 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001574 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1575
Bram Moolenaarbc133542019-05-29 20:26:46 +02001576 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001577
Bram Moolenaarbc133542019-05-29 20:26:46 +02001578 dict_add_number(dict, "line", wp->w_winrow + 1);
1579 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001580 dict_add_number(dict, "width", wp->w_width + left_extra
1581 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1582 dict_add_number(dict, "height", wp->w_height + top_extra
1583 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001584
1585 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1586 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1587 dict_add_number(dict, "core_width", wp->w_width);
1588 dict_add_number(dict, "core_height", wp->w_height);
1589
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001590 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001591 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001592 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001593 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001594 }
1595}
1596
1597/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001598 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1599 */
1600 static void
1601get_padding_border(dict_T *dict, int *array, char *name)
1602{
1603 list_T *list;
1604 int i;
1605
1606 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1607 return;
1608
1609 list = list_alloc();
1610 if (list != NULL)
1611 {
1612 dict_add_list(dict, name, list);
1613 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1614 for (i = 0; i < 4; ++i)
1615 list_append_number(list, array[i]);
1616 }
1617}
1618
1619/*
1620 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1621 */
1622 static void
1623get_borderhighlight(dict_T *dict, win_T *wp)
1624{
1625 list_T *list;
1626 int i;
1627
1628 for (i = 0; i < 4; ++i)
1629 if (wp->w_border_highlight[i] != NULL)
1630 break;
1631 if (i == 4)
1632 return;
1633
1634 list = list_alloc();
1635 if (list != NULL)
1636 {
1637 dict_add_list(dict, "borderhighlight", list);
1638 for (i = 0; i < 4; ++i)
1639 list_append_string(list, wp->w_border_highlight[i], -1);
1640 }
1641}
1642
1643/*
1644 * For popup_getoptions(): add a "borderchars" entry to "dict".
1645 */
1646 static void
1647get_borderchars(dict_T *dict, win_T *wp)
1648{
1649 list_T *list;
1650 int i;
1651 char_u buf[NUMBUFLEN];
1652 int len;
1653
1654 for (i = 0; i < 8; ++i)
1655 if (wp->w_border_char[i] != 0)
1656 break;
1657 if (i == 8)
1658 return;
1659
1660 list = list_alloc();
1661 if (list != NULL)
1662 {
1663 dict_add_list(dict, "borderchars", list);
1664 for (i = 0; i < 8; ++i)
1665 {
1666 len = mb_char2bytes(wp->w_border_char[i], buf);
1667 list_append_string(list, buf, len);
1668 }
1669 }
1670}
1671
1672/*
1673 * For popup_getoptions(): add a "moved" entry to "dict".
1674 */
1675 static void
1676get_moved_list(dict_T *dict, win_T *wp)
1677{
1678 list_T *list;
1679
1680 list = list_alloc();
1681 if (list != NULL)
1682 {
1683 dict_add_list(dict, "moved", list);
1684 list_append_number(list, wp->w_popup_mincol);
1685 list_append_number(list, wp->w_popup_maxcol);
1686 }
1687}
1688
1689/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001690 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001691 */
1692 void
1693f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1694{
1695 dict_T *dict;
1696 int id = (int)tv_get_number(argvars);
1697 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001698 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001699 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001700
1701 if (rettv_dict_alloc(rettv) == OK)
1702 {
1703 if (wp == NULL)
1704 return;
1705
1706 dict = rettv->vval.v_dict;
1707 dict_add_number(dict, "line", wp->w_wantline);
1708 dict_add_number(dict, "col", wp->w_wantcol);
1709 dict_add_number(dict, "minwidth", wp->w_minwidth);
1710 dict_add_number(dict, "minheight", wp->w_minheight);
1711 dict_add_number(dict, "maxheight", wp->w_maxheight);
1712 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001713 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001714 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001715 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001716 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001717 dict_add_string(dict, "title", wp->w_popup_title);
1718 dict_add_number(dict, "wrap", wp->w_p_wrap);
1719 dict_add_number(dict, "drag", wp->w_popup_drag);
1720 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02001721 if (wp->w_scrollbar_highlight != NULL)
1722 dict_add_string(dict, "scrollbarhighlight",
1723 wp->w_scrollbar_highlight);
1724 if (wp->w_thumb_highlight != NULL)
1725 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02001726
Bram Moolenaara3fce622019-06-20 02:31:49 +02001727 // find the tabpage that holds this popup
1728 i = 1;
1729 FOR_ALL_TABPAGES(tp)
1730 {
1731 win_T *p;
1732
1733 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1734 if (p->w_id == id)
1735 break;
1736 if (p != NULL)
1737 break;
1738 ++i;
1739 }
1740 if (tp == NULL)
1741 i = -1; // must be global
1742 else if (tp == curtab)
1743 i = 0;
1744 dict_add_number(dict, "tabpage", i);
1745
Bram Moolenaarae943152019-06-16 22:54:14 +02001746 get_padding_border(dict, wp->w_popup_padding, "padding");
1747 get_padding_border(dict, wp->w_popup_border, "border");
1748 get_borderhighlight(dict, wp);
1749 get_borderchars(dict, wp);
1750 get_moved_list(dict, wp);
1751
1752 if (wp->w_filter_cb.cb_name != NULL)
1753 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1754 if (wp->w_close_cb.cb_name != NULL)
1755 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001756
1757 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1758 ++i)
1759 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1760 {
1761 dict_add_string(dict, "pos",
1762 (char_u *)poppos_entries[i].pp_name);
1763 break;
1764 }
1765
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001766# if defined(FEAT_TIMERS)
1767 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1768 ? (long)wp->w_popup_timer->tr_interval : 0L);
1769# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001770 }
1771}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001772
1773 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001774error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001775{
1776 if (bt_popup(curwin->w_buffer))
1777 {
1778 emsg(_("E994: Not allowed in a popup window"));
1779 return TRUE;
1780 }
1781 return FALSE;
1782}
1783
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001784/*
1785 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001786 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001787 */
1788 void
1789popup_reset_handled()
1790{
1791 win_T *wp;
1792
1793 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1794 wp->w_popup_flags &= ~POPF_HANDLED;
1795 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1796 wp->w_popup_flags &= ~POPF_HANDLED;
1797}
1798
1799/*
1800 * Find the next visible popup where POPF_HANDLED is not set.
1801 * Must have called popup_reset_handled() first.
1802 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1803 * popup with the highest zindex.
1804 */
1805 win_T *
1806find_next_popup(int lowest)
1807{
1808 win_T *wp;
1809 win_T *found_wp;
1810 int found_zindex;
1811
1812 found_zindex = lowest ? INT_MAX : 0;
1813 found_wp = NULL;
1814 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1815 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1816 && (lowest ? wp->w_zindex < found_zindex
1817 : wp->w_zindex > found_zindex))
1818 {
1819 found_zindex = wp->w_zindex;
1820 found_wp = wp;
1821 }
1822 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1823 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1824 && (lowest ? wp->w_zindex < found_zindex
1825 : wp->w_zindex > found_zindex))
1826 {
1827 found_zindex = wp->w_zindex;
1828 found_wp = wp;
1829 }
1830
1831 if (found_wp != NULL)
1832 found_wp->w_popup_flags |= POPF_HANDLED;
1833 return found_wp;
1834}
1835
1836/*
1837 * Invoke the filter callback for window "wp" with typed character "c".
1838 * Uses the global "mod_mask" for modifiers.
1839 * Returns the return value of the filter.
1840 * Careful: The filter may make "wp" invalid!
1841 */
1842 static int
1843invoke_popup_filter(win_T *wp, int c)
1844{
1845 int res;
1846 typval_T rettv;
1847 int dummy;
1848 typval_T argv[3];
1849 char_u buf[NUMBUFLEN];
1850
Bram Moolenaara42d9452019-06-15 21:46:30 +02001851 // Emergency exit: CTRL-C closes the popup.
1852 if (c == Ctrl_C)
1853 {
1854 rettv.v_type = VAR_NUMBER;
1855 rettv.vval.v_number = -1;
1856 popup_close_and_callback(wp, &rettv);
1857 return 1;
1858 }
1859
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001860 argv[0].v_type = VAR_NUMBER;
1861 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1862
1863 // Convert the number to a string, so that the function can use:
1864 // if a:c == "\<F2>"
1865 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1866 argv[1].v_type = VAR_STRING;
1867 argv[1].vval.v_string = vim_strsave(buf);
1868
1869 argv[2].v_type = VAR_UNKNOWN;
1870
Bram Moolenaara42d9452019-06-15 21:46:30 +02001871 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001872 call_callback(&wp->w_filter_cb, -1,
1873 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1874 res = tv_get_number(&rettv);
1875 vim_free(argv[1].vval.v_string);
1876 clear_tv(&rettv);
1877 return res;
1878}
1879
1880/*
1881 * Called when "c" was typed: invoke popup filter callbacks.
1882 * Returns TRUE when the character was consumed,
1883 */
1884 int
1885popup_do_filter(int c)
1886{
1887 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001888 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001889
1890 popup_reset_handled();
1891
1892 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1893 if (wp->w_filter_cb.cb_name != NULL)
1894 res = invoke_popup_filter(wp, c);
1895
1896 return res;
1897}
1898
Bram Moolenaar3397f742019-06-02 18:40:06 +02001899/*
1900 * Called when the cursor moved: check if any popup needs to be closed if the
1901 * cursor moved far enough.
1902 */
1903 void
1904popup_check_cursor_pos()
1905{
1906 win_T *wp;
1907 typval_T tv;
1908
1909 popup_reset_handled();
1910 while ((wp = find_next_popup(TRUE)) != NULL)
1911 if (wp->w_popup_curwin != NULL
1912 && (curwin != wp->w_popup_curwin
1913 || curwin->w_cursor.lnum != wp->w_popup_lnum
1914 || curwin->w_cursor.col < wp->w_popup_mincol
1915 || curwin->w_cursor.col > wp->w_popup_maxcol))
1916 {
1917 tv.v_type = VAR_NUMBER;
1918 tv.vval.v_number = -1;
1919 popup_close_and_callback(wp, &tv);
1920 }
1921}
1922
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001923/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001924 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
1925 * "col" and "line" are screen coordinates.
1926 */
1927 static int
1928popup_masked(win_T *wp, int screencol, int screenline)
1929{
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001930 int col = screencol - wp->w_wincol + 1 + wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001931 int line = screenline - wp->w_winrow + 1;
1932 listitem_T *lio, *li;
1933 int width, height;
1934
1935 if (wp->w_popup_mask == NULL)
1936 return FALSE;
1937 width = popup_width(wp);
1938 height = popup_height(wp);
1939
1940 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1941 {
1942 int cols, cole;
1943 int lines, linee;
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 if (col < cols)
1950 continue;
1951 li = li->li_next;
1952 cole = tv_get_number(&li->li_tv);
1953 if (cole < 0)
1954 cole = width + cole + 1;
1955 if (col > cole)
1956 continue;
1957 li = li->li_next;
1958 lines = tv_get_number(&li->li_tv);
1959 if (lines < 0)
1960 lines = height + lines + 1;
1961 if (line < lines)
1962 continue;
1963 li = li->li_next;
1964 linee = tv_get_number(&li->li_tv);
1965 if (linee < 0)
1966 linee = height + linee + 1;
1967 if (line > linee)
1968 continue;
1969 return TRUE;
1970 }
1971 return FALSE;
1972}
1973
1974/*
1975 * Set flags in popup_transparent[] for window "wp" to "val".
1976 */
1977 static void
1978update_popup_transparent(win_T *wp, int val)
1979{
1980 if (wp->w_popup_mask != NULL)
1981 {
1982 int width = popup_width(wp);
1983 int height = popup_height(wp);
1984 listitem_T *lio, *li;
1985 int cols, cole;
1986 int lines, linee;
1987 int col, line;
1988
1989 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1990 {
1991 li = lio->li_tv.vval.v_list->lv_first;
1992 cols = tv_get_number(&li->li_tv);
1993 if (cols < 0)
1994 cols = width + cols + 1;
1995 li = li->li_next;
1996 cole = tv_get_number(&li->li_tv);
1997 if (cole < 0)
1998 cole = width + cole + 1;
1999 li = li->li_next;
2000 lines = tv_get_number(&li->li_tv);
2001 if (lines < 0)
2002 lines = height + lines + 1;
2003 li = li->li_next;
2004 linee = tv_get_number(&li->li_tv);
2005 if (linee < 0)
2006 linee = height + linee + 1;
2007
2008 --cols;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002009 cols -= wp->w_leftcol;
2010 if (cols < 0)
2011 cols = 0;
2012 cole -= wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002013 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002014 if (lines < 0)
2015 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002016 for (line = lines; line < linee && line < screen_Rows; ++line)
2017 for (col = cols; col < cole && col < screen_Columns; ++col)
2018 popup_transparent[(line + wp->w_winrow) * screen_Columns
2019 + col + wp->w_wincol] = val;
2020 }
2021 }
2022}
2023
2024/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002025 * Update "popup_mask" if needed.
2026 * Also recomputes the popup size and positions.
2027 * Also updates "popup_visible".
2028 * Also marks window lines for redrawing.
2029 */
2030 void
2031may_update_popup_mask(int type)
2032{
2033 win_T *wp;
2034 short *mask;
2035 int line, col;
2036 int redraw_all = FALSE;
2037
2038 // Need to recompute when switching tabs.
2039 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2040 // (such as the screen size) must have changed.
2041 if (popup_mask_tab != curtab || type >= NOT_VALID)
2042 {
2043 popup_mask_refresh = TRUE;
2044 redraw_all = TRUE;
2045 }
2046 if (!popup_mask_refresh)
2047 {
2048 // Check if any buffer has changed.
2049 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2050 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2051 popup_mask_refresh = TRUE;
2052 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2053 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2054 popup_mask_refresh = TRUE;
2055 if (!popup_mask_refresh)
2056 return;
2057 }
2058
2059 // Need to update the mask, something has changed.
2060 popup_mask_refresh = FALSE;
2061 popup_mask_tab = curtab;
2062 popup_visible = FALSE;
2063
2064 // If redrawing everything, just update "popup_mask".
2065 // If redrawing only what is needed, update "popup_mask_next" and then
2066 // compare with "popup_mask" to see what changed.
2067 if (type >= SOME_VALID)
2068 mask = popup_mask;
2069 else
2070 mask = popup_mask_next;
2071 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2072
2073 // Find the window with the lowest zindex that hasn't been handled yet,
2074 // so that the window with a higher zindex overwrites the value in
2075 // popup_mask.
2076 popup_reset_handled();
2077 while ((wp = find_next_popup(TRUE)) != NULL)
2078 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002079 int height = popup_height(wp);
2080 int width = popup_width(wp);
2081
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002082 popup_visible = TRUE;
2083
2084 // Recompute the position if the text changed.
2085 if (redraw_all
2086 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2087 popup_adjust_position(wp);
2088
2089 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002090 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002091 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002092 col < wp->w_wincol + width && col < screen_Columns; ++col)
2093 if (!popup_masked(wp, col, line))
2094 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002095 }
2096
2097 // Only check which lines are to be updated if not already
2098 // updating all lines.
2099 if (mask == popup_mask_next)
2100 for (line = 0; line < screen_Rows; ++line)
2101 {
2102 int col_done = 0;
2103
2104 for (col = 0; col < screen_Columns; ++col)
2105 {
2106 int off = line * screen_Columns + col;
2107
2108 if (popup_mask[off] != popup_mask_next[off])
2109 {
2110 popup_mask[off] = popup_mask_next[off];
2111
2112 if (line >= cmdline_row)
2113 {
2114 // the command line needs to be cleared if text below
2115 // the popup is now visible.
2116 if (!msg_scrolled && popup_mask_next[off] == 0)
2117 clear_cmdline = TRUE;
2118 }
2119 else if (col >= col_done)
2120 {
2121 linenr_T lnum;
2122 int line_cp = line;
2123 int col_cp = col;
2124
2125 // The screen position "line" / "col" needs to be
2126 // redrawn. Figure out what window that is and update
2127 // w_redraw_top and w_redr_bot. Only needs to be done
2128 // once for each window line.
2129 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2130 if (wp != NULL)
2131 {
2132 if (line_cp >= wp->w_height)
2133 // In (or below) status line
2134 wp->w_redr_status = TRUE;
2135 // compute the position in the buffer line from the
2136 // position on the screen
2137 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2138 &lnum))
2139 // past bottom
2140 wp->w_redr_status = TRUE;
2141 else
2142 redrawWinline(wp, lnum);
2143
2144 // This line is going to be redrawn, no need to
2145 // check until the right side of the window.
2146 col_done = wp->w_wincol + wp->w_width - 1;
2147 }
2148 }
2149 }
2150 }
2151 }
2152}
2153
2154/*
2155 * Return a string of "len" spaces in IObuff.
2156 */
2157 static char_u *
2158get_spaces(int len)
2159{
2160 vim_memset(IObuff, ' ', (size_t)len);
2161 IObuff[len] = NUL;
2162 return IObuff;
2163}
2164
2165/*
2166 * Update popup windows. They are drawn on top of normal windows.
2167 * "win_update" is called for each popup window, lowest zindex first.
2168 */
2169 void
2170update_popups(void (*win_update)(win_T *wp))
2171{
2172 win_T *wp;
2173 int top_off;
2174 int left_off;
2175 int total_width;
2176 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002177 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002178 int popup_attr;
2179 int border_attr[4];
2180 int border_char[8];
2181 char_u buf[MB_MAXBYTES];
2182 int row;
2183 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002184 int sb_thumb_top = 0;
2185 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002186 int attr_scroll = 0;
2187 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002188
2189 // Find the window with the lowest zindex that hasn't been updated yet,
2190 // so that the window with a higher zindex is drawn later, thus goes on
2191 // top.
2192 popup_reset_handled();
2193 while ((wp = find_next_popup(TRUE)) != NULL)
2194 {
2195 // This drawing uses the zindex of the popup window, so that it's on
2196 // top of the text but doesn't draw when another popup with higher
2197 // zindex is on top of the character.
2198 screen_zindex = wp->w_zindex;
2199
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002200 // Set flags in popup_transparent[] for masked cells.
2201 update_popup_transparent(wp, 1);
2202
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002203 // adjust w_winrow and w_wincol for border and padding, since
2204 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002205 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002206 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2207 wp->w_winrow += top_off;
2208 wp->w_wincol += left_off;
2209
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002210 // Draw the popup text, unless it's off screen.
2211 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2212 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002213
2214 wp->w_winrow -= top_off;
2215 wp->w_wincol -= left_off;
2216
2217 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002218 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1]
2219 + wp->w_has_scrollbar;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002220 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002221 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2222 popup_attr = get_wcr_attr(wp);
2223
2224 // We can only use these line drawing characters when 'encoding' is
2225 // "utf-8" and 'ambiwidth' is "single".
2226 if (enc_utf8 && *p_ambw == 's')
2227 {
2228 border_char[0] = border_char[2] = 0x2550;
2229 border_char[1] = border_char[3] = 0x2551;
2230 border_char[4] = 0x2554;
2231 border_char[5] = 0x2557;
2232 border_char[6] = 0x255d;
2233 border_char[7] = 0x255a;
2234 }
2235 else
2236 {
2237 border_char[0] = border_char[2] = '-';
2238 border_char[1] = border_char[3] = '|';
2239 for (i = 4; i < 8; ++i)
2240 border_char[i] = '+';
2241 }
2242 for (i = 0; i < 8; ++i)
2243 if (wp->w_border_char[i] != 0)
2244 border_char[i] = wp->w_border_char[i];
2245
2246 for (i = 0; i < 4; ++i)
2247 {
2248 border_attr[i] = popup_attr;
2249 if (wp->w_border_highlight[i] != NULL)
2250 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2251 }
2252
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002253 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002254 if (wp->w_popup_border[0] > 0)
2255 {
2256 // top border
2257 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2258 wp->w_wincol,
2259 wp->w_wincol + total_width,
2260 wp->w_popup_border[3] != 0
2261 ? border_char[4] : border_char[0],
2262 border_char[0], border_attr[0]);
2263 if (wp->w_popup_border[1] > 0)
2264 {
2265 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2266 screen_puts(buf, wp->w_winrow,
2267 wp->w_wincol + total_width - 1, border_attr[1]);
2268 }
2269 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002270 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2271 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002272
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002273 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002274 {
2275 // top padding
2276 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002277 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002278 wp->w_wincol + wp->w_popup_border[3],
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002279 wp->w_wincol + total_width - wp->w_popup_border[1]
2280 - wp->w_has_scrollbar,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002281 ' ', ' ', popup_attr);
2282 }
2283
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002284 // Title goes on top of border or padding.
2285 if (wp->w_popup_title != NULL)
2286 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2287 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2288
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002289 // Compute scrollbar thumb position and size.
2290 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002291 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002292 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2293
Bram Moolenaar68acb412019-06-26 03:40:36 +02002294 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2295 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002296 if (sb_thumb_height == 0)
2297 sb_thumb_height = 1;
Bram Moolenaar68acb412019-06-26 03:40:36 +02002298 sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2)
2299 * (wp->w_height - sb_thumb_height)
2300 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002301 if (wp->w_scrollbar_highlight != NULL)
2302 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2303 else
2304 attr_scroll = highlight_attr[HLF_PSB];
2305 if (wp->w_thumb_highlight != NULL)
2306 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2307 else
2308 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002309 }
2310
2311 for (i = wp->w_popup_border[0];
2312 i < total_height - wp->w_popup_border[2]; ++i)
2313 {
2314 row = wp->w_winrow + i;
2315
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002316 // left border
2317 if (wp->w_popup_border[3] > 0)
2318 {
2319 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2320 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2321 }
2322 // left padding
2323 if (wp->w_popup_padding[3] > 0)
2324 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2325 wp->w_wincol + wp->w_popup_border[3], popup_attr);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002326 // scrollbar
2327 if (wp->w_has_scrollbar)
2328 {
2329 int line = i - top_off;
2330 int scroll_col = wp->w_wincol + total_width - 1
2331 - wp->w_popup_border[1];
2332
2333 if (line >= 0 && line < wp->w_height)
2334 screen_putchar(' ', row, scroll_col,
2335 line >= sb_thumb_top
2336 && line < sb_thumb_top + sb_thumb_height
2337 ? attr_thumb : attr_scroll);
2338 else
2339 screen_putchar(' ', row, scroll_col, popup_attr);
2340 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002341 // right border
2342 if (wp->w_popup_border[1] > 0)
2343 {
2344 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2345 screen_puts(buf, row,
2346 wp->w_wincol + total_width - 1, border_attr[1]);
2347 }
2348 // right padding
2349 if (wp->w_popup_padding[1] > 0)
2350 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2351 wp->w_wincol + wp->w_popup_border[3]
2352 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2353 }
2354
2355 if (wp->w_popup_padding[2] > 0)
2356 {
2357 // bottom padding
2358 row = wp->w_winrow + wp->w_popup_border[0]
2359 + wp->w_popup_padding[0] + wp->w_height;
2360 screen_fill(row, row + wp->w_popup_padding[2],
2361 wp->w_wincol + wp->w_popup_border[3],
2362 wp->w_wincol + total_width - wp->w_popup_border[1],
2363 ' ', ' ', popup_attr);
2364 }
2365
2366 if (wp->w_popup_border[2] > 0)
2367 {
2368 // bottom border
2369 row = wp->w_winrow + total_height - 1;
2370 screen_fill(row , row + 1,
2371 wp->w_wincol,
2372 wp->w_wincol + total_width,
2373 wp->w_popup_border[3] != 0
2374 ? border_char[7] : border_char[2],
2375 border_char[2], border_attr[2]);
2376 if (wp->w_popup_border[1] > 0)
2377 {
2378 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2379 screen_puts(buf, row,
2380 wp->w_wincol + total_width - 1, border_attr[2]);
2381 }
2382 }
2383
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002384 update_popup_transparent(wp, 0);
2385
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002386 // Back to the normal zindex.
2387 screen_zindex = 0;
2388 }
2389}
2390
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002391/*
2392 * Mark references in callbacks of one popup window.
2393 */
2394 static int
2395set_ref_in_one_popup(win_T *wp, int copyID)
2396{
2397 int abort = FALSE;
2398 typval_T tv;
2399
2400 if (wp->w_close_cb.cb_partial != NULL)
2401 {
2402 tv.v_type = VAR_PARTIAL;
2403 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2404 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2405 }
2406 if (wp->w_filter_cb.cb_partial != NULL)
2407 {
2408 tv.v_type = VAR_PARTIAL;
2409 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2410 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2411 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002412 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002413 return abort;
2414}
2415
2416/*
2417 * Set reference in callbacks of popup windows.
2418 */
2419 int
2420set_ref_in_popups(int copyID)
2421{
2422 int abort = FALSE;
2423 win_T *wp;
2424 tabpage_T *tp;
2425
2426 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2427 abort = abort || set_ref_in_one_popup(wp, copyID);
2428
2429 FOR_ALL_TABPAGES(tp)
2430 {
2431 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2432 abort = abort || set_ref_in_one_popup(wp, copyID);
2433 if (abort)
2434 break;
2435 }
2436 return abort;
2437}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002438#endif // FEAT_TEXT_PROP