blob: 5aab84a4aa5e0e1b383d9db5ff82c94380f8fe6e [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_TEXT_PROP
17
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar4d784b22019-05-25 19:51:39 +020031/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020032 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020033 * Handles "cursor+N" and "cursor-N".
34 */
35 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020036popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037{
38 dictitem_T *di;
39 char_u *val;
40 char_u *s;
41 char_u *endp;
42 int n = 0;
43
44 di = dict_find(dict, key, -1);
45 if (di == NULL)
46 return 0;
47
48 val = tv_get_string(&di->di_tv);
49 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020050 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020051
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020056 endp = s;
57 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
58 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059 if (endp != NULL && *skipwhite(endp) != NUL)
60 {
61 semsg(_(e_invexpr2), val);
62 return 0;
63 }
64 }
65
66 if (STRCMP(key, "line") == 0)
67 n = screen_screenrow() + 1 + n;
68 else // "col"
69 n = screen_screencol() + 1 + n;
70
71 if (n < 1)
72 n = 1;
73 return n;
74}
75
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020076 static void
77get_pos_options(win_T *wp, dict_T *dict)
78{
79 char_u *str;
80 int nr;
81
82 nr = popup_options_one(dict, (char_u *)"line");
83 if (nr > 0)
84 wp->w_wantline = nr;
85 nr = popup_options_one(dict, (char_u *)"col");
86 if (nr > 0)
87 wp->w_wantcol = nr;
88
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020089 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
90
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020091 str = dict_get_string(dict, (char_u *)"pos", FALSE);
92 if (str != NULL)
93 {
94 for (nr = 0;
95 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
96 ++nr)
97 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
98 {
99 wp->w_popup_pos = poppos_entries[nr].pp_val;
100 nr = -1;
101 break;
102 }
103 if (nr != -1)
104 semsg(_(e_invarg2), str);
105 }
106}
107
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200108 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200109set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110{
111 dictitem_T *di;
112
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113 di = dict_find(dict, (char_u *)name, -1);
114 if (di != NULL)
115 {
116 if (di->di_tv.v_type != VAR_LIST)
117 emsg(_(e_listreq));
118 else
119 {
120 list_T *list = di->di_tv.vval.v_list;
121 listitem_T *li;
122 int i;
123 int nr;
124
125 for (i = 0; i < 4; ++i)
126 array[i] = 1;
127 if (list != NULL)
128 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
129 ++i, li = li->li_next)
130 {
131 nr = (int)tv_get_number(&li->li_tv);
132 if (nr >= 0)
133 array[i] = nr > max_val ? max_val : nr;
134 }
135 }
136 }
137}
138
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200139/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200140 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200141 */
142 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200143set_moved_values(win_T *wp)
144{
145 wp->w_popup_curwin = curwin;
146 wp->w_popup_lnum = curwin->w_cursor.lnum;
147 wp->w_popup_mincol = curwin->w_cursor.col;
148 wp->w_popup_maxcol = curwin->w_cursor.col;
149}
150
151/*
152 * Used when popup options contain "moved" with "word" or "WORD".
153 */
154 static void
155set_moved_columns(win_T *wp, int flags)
156{
157 char_u *ptr;
158 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
159
160 if (len > 0)
161 {
162 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
163 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
164 }
165}
166
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200167/*
168 * Return TRUE if "row"/"col" is on the border of the popup.
169 * The values are relative to the top-left corner.
170 */
171 int
172popup_on_border(win_T *wp, int row, int col)
173{
174 return (row == 0 && wp->w_popup_border[0] > 0)
175 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
176 || (col == 0 && wp->w_popup_border[3] > 0)
177 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
178}
179
180// Values set when dragging a popup window starts.
181static int drag_start_row;
182static int drag_start_col;
183static int drag_start_wantline;
184static int drag_start_wantcol;
185
186/*
187 * Mouse down on border of popup window: start dragging it.
188 * Uses mouse_col and mouse_row.
189 */
190 void
191popup_start_drag(win_T *wp)
192{
193 drag_start_row = mouse_row;
194 drag_start_col = mouse_col;
195 // TODO: handle using different corner
196 if (wp->w_wantline == 0)
197 drag_start_wantline = wp->w_winrow + 1;
198 else
199 drag_start_wantline = wp->w_wantline;
200 if (wp->w_wantcol == 0)
201 drag_start_wantcol = wp->w_wincol + 1;
202 else
203 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200204
205 // Stop centering the popup
206 if (wp->w_popup_pos == POPPOS_CENTER)
207 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200208}
209
210/*
211 * Mouse moved while dragging a popup window: adjust the window popup position.
212 */
213 void
214popup_drag(win_T *wp)
215{
216 // The popup may be closed before dragging stops.
217 if (!win_valid_popup(wp))
218 return;
219
220 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
221 if (wp->w_wantline < 1)
222 wp->w_wantline = 1;
223 if (wp->w_wantline > Rows)
224 wp->w_wantline = Rows;
225 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
226 if (wp->w_wantcol < 1)
227 wp->w_wantcol = 1;
228 if (wp->w_wantcol > Columns)
229 wp->w_wantcol = Columns;
230
231 popup_adjust_position(wp);
232}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200233
234#if defined(FEAT_TIMERS)
235 static void
236popup_add_timeout(win_T *wp, int time)
237{
238 char_u cbbuf[50];
239 char_u *ptr = cbbuf;
240 typval_T tv;
241
242 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
243 "{_ -> popup_close(%d)}", wp->w_id);
244 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
245 {
246 wp->w_popup_timer = create_timer(time, 0);
247 wp->w_popup_timer->tr_callback = get_callback(&tv);
248 clear_tv(&tv);
249 }
250}
251#endif
252
Bram Moolenaar17627312019-06-02 19:53:44 +0200253/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200254 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200255 */
256 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200257apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200258{
Bram Moolenaarae943152019-06-16 22:54:14 +0200259 int nr;
260
261 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
262 wp->w_minwidth = nr;
263 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
264 wp->w_minheight = nr;
265 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
266 wp->w_maxwidth = nr;
267 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
268 wp->w_maxheight = nr;
269 get_pos_options(wp, d);
270}
271
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200272 static void
273check_highlight(dict_T *dict, char *name, char_u **pval)
274{
275 dictitem_T *di;
276 char_u *str;
277
278 di = dict_find(dict, (char_u *)name, -1);
279 if (di != NULL)
280 {
281 if (di->di_tv.v_type != VAR_STRING)
282 semsg(_(e_invargval), name);
283 else
284 {
285 str = tv_get_string(&di->di_tv);
286 if (*str != NUL)
287 *pval = vim_strsave(str);
288 }
289 }
290}
291
Bram Moolenaarae943152019-06-16 22:54:14 +0200292/*
293 * Shared between popup_create() and f_popup_setoptions().
294 */
295 static void
296apply_general_options(win_T *wp, dict_T *dict)
297{
298 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200299 int nr;
300 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200301
Bram Moolenaarae943152019-06-16 22:54:14 +0200302 // TODO: flip
303
304 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200305 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200306 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
307 if (wp->w_firstline < 1)
308 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200309
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200310 di = dict_find(dict, (char_u *)"scrollbar", -1);
311 if (di != NULL)
312 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
313
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200314 str = dict_get_string(dict, (char_u *)"title", FALSE);
315 if (str != NULL)
316 {
317 vim_free(wp->w_popup_title);
318 wp->w_popup_title = vim_strsave(str);
319 }
320
Bram Moolenaar402502d2019-05-30 22:07:36 +0200321 di = dict_find(dict, (char_u *)"wrap", -1);
322 if (di != NULL)
323 {
324 nr = dict_get_number(dict, (char_u *)"wrap");
325 wp->w_p_wrap = nr != 0;
326 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200327
Bram Moolenaara42d9452019-06-15 21:46:30 +0200328 di = dict_find(dict, (char_u *)"drag", -1);
329 if (di != NULL)
330 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200331
Bram Moolenaarae943152019-06-16 22:54:14 +0200332 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
333 if (str != NULL)
334 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
335 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200336
Bram Moolenaarae943152019-06-16 22:54:14 +0200337 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
338 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200339
Bram Moolenaar790498b2019-06-01 22:15:29 +0200340 di = dict_find(dict, (char_u *)"borderhighlight", -1);
341 if (di != NULL)
342 {
343 if (di->di_tv.v_type != VAR_LIST)
344 emsg(_(e_listreq));
345 else
346 {
347 list_T *list = di->di_tv.vval.v_list;
348 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200349 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200350
351 if (list != NULL)
352 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
353 ++i, li = li->li_next)
354 {
355 str = tv_get_string(&li->li_tv);
356 if (*str != NUL)
357 wp->w_border_highlight[i] = vim_strsave(str);
358 }
359 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
360 for (i = 1; i < 4; ++i)
361 wp->w_border_highlight[i] =
362 vim_strsave(wp->w_border_highlight[0]);
363 }
364 }
365
Bram Moolenaar790498b2019-06-01 22:15:29 +0200366 di = dict_find(dict, (char_u *)"borderchars", -1);
367 if (di != NULL)
368 {
369 if (di->di_tv.v_type != VAR_LIST)
370 emsg(_(e_listreq));
371 else
372 {
373 list_T *list = di->di_tv.vval.v_list;
374 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200375 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200376
377 if (list != NULL)
378 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
379 ++i, li = li->li_next)
380 {
381 str = tv_get_string(&li->li_tv);
382 if (*str != NUL)
383 wp->w_border_char[i] = mb_ptr2char(str);
384 }
385 if (list->lv_len == 1)
386 for (i = 1; i < 8; ++i)
387 wp->w_border_char[i] = wp->w_border_char[0];
388 if (list->lv_len == 2)
389 {
390 for (i = 4; i < 8; ++i)
391 wp->w_border_char[i] = wp->w_border_char[1];
392 for (i = 1; i < 4; ++i)
393 wp->w_border_char[i] = wp->w_border_char[0];
394 }
395 }
396 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200397
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200398 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
399 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
400
Bram Moolenaarae943152019-06-16 22:54:14 +0200401 di = dict_find(dict, (char_u *)"zindex", -1);
402 if (di != NULL)
403 {
404 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
405 if (wp->w_zindex < 1)
406 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
407 if (wp->w_zindex > 32000)
408 wp->w_zindex = 32000;
409 }
410
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200411 di = dict_find(dict, (char_u *)"mask", -1);
412 if (di != NULL)
413 {
414 int ok = TRUE;
415
416 if (di->di_tv.v_type != VAR_LIST)
417 ok = FALSE;
418 else if (di->di_tv.vval.v_list != NULL)
419 {
420 listitem_T *li;
421
422 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
423 li = li->li_next)
424 {
425 if (li->li_tv.v_type != VAR_LIST
426 || li->li_tv.vval.v_list == NULL
427 || li->li_tv.vval.v_list->lv_len != 4)
428 {
429 ok = FALSE;
430 break;
431 }
432 }
433 }
434 if (ok)
435 {
436 wp->w_popup_mask = di->di_tv.vval.v_list;
437 ++wp->w_popup_mask->lv_refcount;
438 }
439 else
440 semsg(_(e_invargval), "mask");
441 }
442
Bram Moolenaarae943152019-06-16 22:54:14 +0200443#if defined(FEAT_TIMERS)
444 // Add timer to close the popup after some time.
445 nr = dict_get_number(dict, (char_u *)"time");
446 if (nr > 0)
447 popup_add_timeout(wp, nr);
448#endif
449
Bram Moolenaar3397f742019-06-02 18:40:06 +0200450 di = dict_find(dict, (char_u *)"moved", -1);
451 if (di != NULL)
452 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200453 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200454 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
455 {
456 char_u *s = di->di_tv.vval.v_string;
457 int flags = 0;
458
459 if (STRCMP(s, "word") == 0)
460 flags = FIND_IDENT | FIND_STRING;
461 else if (STRCMP(s, "WORD") == 0)
462 flags = FIND_STRING;
463 else if (STRCMP(s, "any") != 0)
464 semsg(_(e_invarg2), s);
465 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200466 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200467 }
468 else if (di->di_tv.v_type == VAR_LIST
469 && di->di_tv.vval.v_list != NULL
470 && di->di_tv.vval.v_list->lv_len == 2)
471 {
472 list_T *l = di->di_tv.vval.v_list;
473
474 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
475 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
476 }
477 else
478 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
479 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200480
Bram Moolenaarae943152019-06-16 22:54:14 +0200481 di = dict_find(dict, (char_u *)"filter", -1);
482 if (di != NULL)
483 {
484 callback_T callback = get_callback(&di->di_tv);
485
486 if (callback.cb_name != NULL)
487 {
488 free_callback(&wp->w_filter_cb);
489 set_callback(&wp->w_filter_cb, &callback);
490 }
491 }
492
493 di = dict_find(dict, (char_u *)"callback", -1);
494 if (di != NULL)
495 {
496 callback_T callback = get_callback(&di->di_tv);
497
498 if (callback.cb_name != NULL)
499 {
500 free_callback(&wp->w_close_cb);
501 set_callback(&wp->w_close_cb, &callback);
502 }
503 }
504}
505
506/*
507 * Go through the options in "dict" and apply them to popup window "wp".
508 */
509 static void
510apply_options(win_T *wp, dict_T *dict)
511{
512 int nr;
513
514 apply_move_options(wp, dict);
515 apply_general_options(wp, dict);
516
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200517 nr = dict_get_number(dict, (char_u *)"hidden");
518 if (nr > 0)
519 {
520 wp->w_popup_flags |= POPF_HIDDEN;
521 --wp->w_buffer->b_nwindows;
522 }
523
Bram Moolenaar33796b32019-06-08 16:01:13 +0200524 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200525}
526
527/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200528 * Add lines to the popup from a list of strings.
529 */
530 static void
531add_popup_strings(buf_T *buf, list_T *l)
532{
533 listitem_T *li;
534 linenr_T lnum = 0;
535 char_u *p;
536
537 for (li = l->lv_first; li != NULL; li = li->li_next)
538 if (li->li_tv.v_type == VAR_STRING)
539 {
540 p = li->li_tv.vval.v_string;
541 ml_append_buf(buf, lnum++,
542 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
543 }
544}
545
546/*
547 * Add lines to the popup from a list of dictionaries.
548 */
549 static void
550add_popup_dicts(buf_T *buf, list_T *l)
551{
552 listitem_T *li;
553 listitem_T *pli;
554 linenr_T lnum = 0;
555 char_u *p;
556 dict_T *dict;
557
558 // first add the text lines
559 for (li = l->lv_first; li != NULL; li = li->li_next)
560 {
561 if (li->li_tv.v_type != VAR_DICT)
562 {
563 emsg(_(e_dictreq));
564 return;
565 }
566 dict = li->li_tv.vval.v_dict;
567 p = dict == NULL ? NULL
568 : dict_get_string(dict, (char_u *)"text", FALSE);
569 ml_append_buf(buf, lnum++,
570 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
571 }
572
573 // add the text properties
574 lnum = 1;
575 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
576 {
577 dictitem_T *di;
578 list_T *plist;
579
580 dict = li->li_tv.vval.v_dict;
581 di = dict_find(dict, (char_u *)"props", -1);
582 if (di != NULL)
583 {
584 if (di->di_tv.v_type != VAR_LIST)
585 {
586 emsg(_(e_listreq));
587 return;
588 }
589 plist = di->di_tv.vval.v_list;
590 if (plist != NULL)
591 {
592 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
593 {
594 if (pli->li_tv.v_type != VAR_DICT)
595 {
596 emsg(_(e_dictreq));
597 return;
598 }
599 dict = pli->li_tv.vval.v_dict;
600 if (dict != NULL)
601 {
602 int col = dict_get_number(dict, (char_u *)"col");
603
604 prop_add_common( lnum, col, dict, buf, NULL);
605 }
606 }
607 }
608 }
609 }
610}
611
612/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200613 * Return the height of popup window "wp", including border and padding.
614 */
615 int
616popup_height(win_T *wp)
617{
618 return wp->w_height
619 + wp->w_popup_padding[0] + wp->w_popup_border[0]
620 + wp->w_popup_padding[2] + wp->w_popup_border[2];
621}
622
623/*
624 * Return the width of popup window "wp", including border and padding.
625 */
626 int
627popup_width(win_T *wp)
628{
629 return wp->w_width
630 + wp->w_popup_padding[3] + wp->w_popup_border[3]
631 + wp->w_popup_padding[1] + wp->w_popup_border[1];
632}
633
634/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200635 * Get the padding plus border at the top, adjusted to 1 if there is a title.
636 */
637 static int
638popup_top_extra(win_T *wp)
639{
640 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
641
642 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
643 return 1;
644 return extra;
645}
646
647/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200648 * Adjust the position and size of the popup to fit on the screen.
649 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200650 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200651popup_adjust_position(win_T *wp)
652{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200653 linenr_T lnum;
654 int wrapped = 0;
655 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200656 int center_vert = FALSE;
657 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200658 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200659 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200660 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
661 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
662 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
663 int extra_height = top_extra + bot_extra;
664 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200665 int org_winrow = wp->w_winrow;
666 int org_wincol = wp->w_wincol;
667 int org_width = wp->w_width;
668 int org_height = wp->w_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200669 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200670
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200671 wp->w_winrow = 0;
672 wp->w_wincol = 0;
673 if (wp->w_popup_pos == POPPOS_CENTER)
674 {
675 // center after computing the size
676 center_vert = TRUE;
677 center_hor = TRUE;
678 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200679 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200680 {
681 if (wp->w_wantline == 0)
682 center_vert = TRUE;
683 else if (wp->w_popup_pos == POPPOS_TOPLEFT
684 || wp->w_popup_pos == POPPOS_TOPRIGHT)
685 {
686 wp->w_winrow = wp->w_wantline - 1;
687 if (wp->w_winrow >= Rows)
688 wp->w_winrow = Rows - 1;
689 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200690
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200691 if (wp->w_wantcol == 0)
692 center_hor = TRUE;
693 else if (wp->w_popup_pos == POPPOS_TOPLEFT
694 || wp->w_popup_pos == POPPOS_BOTLEFT)
695 {
696 wp->w_wincol = wp->w_wantcol - 1;
697 if (wp->w_wincol >= Columns - 3)
698 wp->w_wincol = Columns - 3;
699 }
700 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200701
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200702 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200703 // When left aligned use the space available, but shift to the left when we
704 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200705 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200706 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200707 {
708 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200709 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200710 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200711
Bram Moolenaar8d241042019-06-12 23:40:01 +0200712 // start at the desired first line
713 wp->w_topline = wp->w_firstline;
714 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
715 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
716
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200717 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200718 // Use a minimum width of one, so that something shows when there is no
719 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200720 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200721 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200722 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200723 {
724 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
725
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200726 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200727 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200728 while (len > maxwidth)
729 {
730 ++wrapped;
731 len -= maxwidth;
732 wp->w_width = maxwidth;
733 }
734 }
735 else if (len > maxwidth
736 && allow_adjust_left
737 && (wp->w_popup_pos == POPPOS_TOPLEFT
738 || wp->w_popup_pos == POPPOS_BOTLEFT))
739 {
740 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200741 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200742
Bram Moolenaar51c31312019-06-15 22:27:23 +0200743 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200744 {
745 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200746
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200747 len -= truncate_shift;
748 shift_by -= truncate_shift;
749 }
750
751 wp->w_wincol -= shift_by;
752 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200753 wp->w_width = maxwidth;
754 }
755 if (wp->w_width < len)
756 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200757 // do not use the width of lines we're not going to show
758 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
759 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
760 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200761 }
762
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200763 wp->w_has_scrollbar = wp->w_want_scrollbar
764 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
765
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200766 minwidth = wp->w_minwidth;
767 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
768 {
769 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
770
771 if (minwidth < title_len)
772 minwidth = title_len;
773 }
774
775 if (minwidth > 0 && wp->w_width < minwidth)
776 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200777 if (wp->w_width > maxwidth)
778 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200779 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200780 {
781 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
782 if (wp->w_wincol < 0)
783 wp->w_wincol = 0;
784 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200785 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
786 || wp->w_popup_pos == POPPOS_TOPRIGHT)
787 {
788 // Right aligned: move to the right if needed.
789 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200790 if (wp->w_width + extra_width < wp->w_wantcol)
791 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200792 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200793
Bram Moolenaar8d241042019-06-12 23:40:01 +0200794 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
795 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200796 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
797 wp->w_height = wp->w_minheight;
798 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
799 wp->w_height = wp->w_maxheight;
800 if (wp->w_height > Rows - wp->w_winrow)
801 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200802
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200803 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200804 {
805 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
806 if (wp->w_winrow < 0)
807 wp->w_winrow = 0;
808 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200809 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
810 || wp->w_popup_pos == POPPOS_BOTLEFT)
811 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200812 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200813 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200814 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200815 else
816 // not enough space, make top aligned
817 wp->w_winrow = wp->w_wantline + 1;
818 }
819
Bram Moolenaar17146962019-05-30 00:12:11 +0200820 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200821
822 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200823 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200824 if (org_winrow != wp->w_winrow
825 || org_wincol != wp->w_wincol
826 || org_width != wp->w_width
827 || org_height != wp->w_height)
828 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200829 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200830 popup_mask_refresh = TRUE;
831 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200832}
833
Bram Moolenaar17627312019-06-02 19:53:44 +0200834typedef enum
835{
836 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200837 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200838 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200839 TYPE_DIALOG,
840 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200841} create_type_T;
842
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200843/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200844 * Make "buf" empty and set the contents to "text".
845 * Used by popup_create() and popup_settext().
846 */
847 static void
848popup_set_buffer_text(buf_T *buf, typval_T text)
849{
850 int lnum;
851
852 // Clear the buffer, then replace the lines.
853 curbuf = buf;
854 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
855 ml_delete(lnum, FALSE);
856 curbuf = curwin->w_buffer;
857
858 // Add text to the buffer.
859 if (text.v_type == VAR_STRING)
860 {
861 // just a string
862 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
863 }
864 else
865 {
866 list_T *l = text.vval.v_list;
867
868 if (l->lv_len > 0)
869 {
870 if (l->lv_first->li_tv.v_type == VAR_STRING)
871 // list of strings
872 add_popup_strings(buf, l);
873 else
874 // list of dictionaries
875 add_popup_dicts(buf, l);
876 }
877 }
878
879 // delete the line that was in the empty buffer
880 curbuf = buf;
881 ml_delete(buf->b_ml.ml_line_count, FALSE);
882 curbuf = curwin->w_buffer;
883}
884
885/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200886 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200887 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200888 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200889 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200890popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200891{
Bram Moolenaara3fce622019-06-20 02:31:49 +0200892 win_T *wp;
893 tabpage_T *tp = NULL;
894 int tabnr;
895 buf_T *buf;
896 dict_T *d;
897 int nr;
898 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200899
900 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200901 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
902 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200903 {
904 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200905 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200906 }
907 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
908 {
909 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200910 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200911 }
912 d = argvars[1].vval.v_dict;
913
Bram Moolenaara3fce622019-06-20 02:31:49 +0200914 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
915 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
916 else if (type == TYPE_NOTIFICATION)
917 tabnr = -1; // notifications are global by default
918 else
919 tabnr = 0;
920 if (tabnr > 0)
921 {
922 tp = find_tabpage(tabnr);
923 if (tp == NULL)
924 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +0200925 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +0200926 return NULL;
927 }
928 }
929
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200930 // Create the window and buffer.
931 wp = win_alloc_popup_win();
932 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200933 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200934 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200935 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200936
937 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
938 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200939 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200940 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200941
942 win_init_popup_win(wp, buf);
943
944 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200945 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200946 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200947 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200948 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200949 buf->b_p_ul = -1; // no undo
950 buf->b_p_swf = FALSE; // no swap file
951 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200952 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200953 wp->w_p_wrap = TRUE; // 'wrap' is default on
954
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200955 // Avoid that 'buftype' is reset when this buffer is entered.
956 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200957
Bram Moolenaara3fce622019-06-20 02:31:49 +0200958 if (tp != NULL)
959 {
960 // popup on specified tab page
961 wp->w_next = tp->tp_first_popupwin;
962 tp->tp_first_popupwin = wp;
963 }
964 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200965 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200966 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200967 wp->w_next = curtab->tp_first_popupwin;
968 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200969 }
Bram Moolenaara3fce622019-06-20 02:31:49 +0200970 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200971 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200972 win_T *prev = first_popupwin;
973
974 // Global popup: add at the end, so that it gets displayed on top of
975 // older ones with the same zindex. Matters for notifications.
976 if (first_popupwin == NULL)
977 first_popupwin = wp;
978 else
979 {
980 while (prev->w_next != NULL)
981 prev = prev->w_next;
982 prev->w_next = wp;
983 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200984 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200985
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200986 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200987
Bram Moolenaar17627312019-06-02 19:53:44 +0200988 if (type == TYPE_ATCURSOR)
989 {
990 wp->w_popup_pos = POPPOS_BOTLEFT;
991 setcursor_mayforce(TRUE);
992 wp->w_wantline = screen_screenrow();
993 if (wp->w_wantline == 0) // cursor in first line
994 {
995 wp->w_wantline = 2;
996 wp->w_popup_pos = POPPOS_TOPLEFT;
997 }
998 wp->w_wantcol = screen_screencol() + 1;
999 set_moved_values(wp);
1000 set_moved_columns(wp, FIND_STRING);
1001 }
1002
Bram Moolenaar33796b32019-06-08 16:01:13 +02001003 // set default values
1004 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
1005
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001006 if (type == TYPE_NOTIFICATION)
1007 {
1008 win_T *twp, *nextwin;
1009 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001010
1011 // Try to not overlap with another global popup. Guess we need 3
1012 // more screen lines than buffer lines.
1013 wp->w_wantline = 1;
1014 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1015 {
1016 nextwin = twp->w_next;
1017 if (twp != wp
1018 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1019 && twp->w_winrow <= wp->w_wantline - 1 + height
1020 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1021 {
1022 // move to below this popup and restart the loop to check for
1023 // overlap with other popups
1024 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1025 nextwin = first_popupwin;
1026 }
1027 }
1028 if (wp->w_wantline + height > Rows)
1029 {
1030 // can't avoid overlap, put on top in the hope that message goes
1031 // away soon.
1032 wp->w_wantline = 1;
1033 }
1034
1035 wp->w_wantcol = 10;
1036 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001037 wp->w_minwidth = 20;
1038 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001039 for (i = 0; i < 4; ++i)
1040 wp->w_popup_border[i] = 1;
1041 wp->w_popup_padding[1] = 1;
1042 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001043
1044 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001045 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001046 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1047 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001048 }
1049
Bram Moolenaara730e552019-06-16 19:05:31 +02001050 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001051 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001052 wp->w_popup_pos = POPPOS_CENTER;
1053 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1054 wp->w_popup_drag = 1;
1055 for (i = 0; i < 4; ++i)
1056 {
1057 wp->w_popup_border[i] = 1;
1058 wp->w_popup_padding[i] = 1;
1059 }
1060 }
1061
Bram Moolenaara730e552019-06-16 19:05:31 +02001062 if (type == TYPE_MENU)
1063 {
1064 typval_T tv;
1065 callback_T callback;
1066
1067 tv.v_type = VAR_STRING;
1068 tv.vval.v_string = (char_u *)"popup_filter_menu";
1069 callback = get_callback(&tv);
1070 if (callback.cb_name != NULL)
1071 set_callback(&wp->w_filter_cb, &callback);
1072
1073 wp->w_p_wrap = 0;
1074 }
1075
Bram Moolenaarae943152019-06-16 22:54:14 +02001076 for (i = 0; i < 4; ++i)
1077 VIM_CLEAR(wp->w_border_highlight[i]);
1078 for (i = 0; i < 8; ++i)
1079 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001080 wp->w_want_scrollbar = 1;
Bram Moolenaarae943152019-06-16 22:54:14 +02001081
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001082 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001083 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001084
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001085#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001086 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1087 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001088#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001089
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001090 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001091
1092 wp->w_vsep_width = 0;
1093
1094 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001095 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001096
1097 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001098}
1099
1100/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001101 * popup_clear()
1102 */
1103 void
1104f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1105{
1106 close_all_popups();
1107}
1108
1109/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001110 * popup_create({text}, {options})
1111 */
1112 void
1113f_popup_create(typval_T *argvars, typval_T *rettv)
1114{
Bram Moolenaar17627312019-06-02 19:53:44 +02001115 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001116}
1117
1118/*
1119 * popup_atcursor({text}, {options})
1120 */
1121 void
1122f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1123{
Bram Moolenaar17627312019-06-02 19:53:44 +02001124 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001125}
1126
1127/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001128 * Invoke the close callback for window "wp" with value "result".
1129 * Careful: The callback may make "wp" invalid!
1130 */
1131 static void
1132invoke_popup_callback(win_T *wp, typval_T *result)
1133{
1134 typval_T rettv;
1135 int dummy;
1136 typval_T argv[3];
1137
1138 argv[0].v_type = VAR_NUMBER;
1139 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1140
1141 if (result != NULL && result->v_type != VAR_UNKNOWN)
1142 copy_tv(result, &argv[1]);
1143 else
1144 {
1145 argv[1].v_type = VAR_NUMBER;
1146 argv[1].vval.v_number = 0;
1147 }
1148
1149 argv[2].v_type = VAR_UNKNOWN;
1150
1151 call_callback(&wp->w_close_cb, -1,
1152 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1153 if (result != NULL)
1154 clear_tv(&argv[1]);
1155 clear_tv(&rettv);
1156}
1157
1158/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001159 * Close popup "wp" and invoke any close callback for it.
1160 */
1161 static void
1162popup_close_and_callback(win_T *wp, typval_T *arg)
1163{
1164 int id = wp->w_id;
1165
1166 if (wp->w_close_cb.cb_name != NULL)
1167 // Careful: This may make "wp" invalid.
1168 invoke_popup_callback(wp, arg);
1169
1170 popup_close(id);
1171}
1172
1173/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001174 * In a filter: check if the typed key is a mouse event that is used for
1175 * dragging the popup.
1176 */
1177 static void
1178filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1179{
1180 int row = mouse_row;
1181 int col = mouse_col;
1182
1183 if (wp->w_popup_drag
1184 && is_mouse_key(c)
1185 && (wp == popup_dragwin
1186 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1187 // do not consume the key, allow for dragging the popup
1188 rettv->vval.v_number = 0;
1189}
1190
1191 static void
1192popup_highlight_curline(win_T *wp)
1193{
1194 int id;
1195 char buf[100];
1196
1197 match_delete(wp, 1, FALSE);
1198
1199 id = syn_name2id((char_u *)"PopupSelected");
1200 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1201 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1202 (char_u *)buf, 10, 1, NULL, NULL);
1203}
1204
1205/*
1206 * popup_filter_menu({text}, {options})
1207 */
1208 void
1209f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1210{
1211 int id = tv_get_number(&argvars[0]);
1212 win_T *wp = win_id2wp(id);
1213 char_u *key = tv_get_string(&argvars[1]);
1214 typval_T res;
1215 int c;
1216 linenr_T old_lnum;
1217
1218 // If the popup has been closed do not consume the key.
1219 if (wp == NULL)
1220 return;
1221
1222 c = *key;
1223 if (c == K_SPECIAL && key[1] != NUL)
1224 c = TO_SPECIAL(key[1], key[2]);
1225
1226 // consume all keys until done
1227 rettv->vval.v_number = 1;
1228 res.v_type = VAR_NUMBER;
1229
1230 old_lnum = wp->w_cursor.lnum;
1231 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1232 --wp->w_cursor.lnum;
1233 if ((c == 'j' || c == 'J' || c == K_DOWN)
1234 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1235 ++wp->w_cursor.lnum;
1236 if (old_lnum != wp->w_cursor.lnum)
1237 {
1238 popup_highlight_curline(wp);
1239 return;
1240 }
1241
1242 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1243 {
1244 // Cancelled, invoke callback with -1
1245 res.vval.v_number = -1;
1246 popup_close_and_callback(wp, &res);
1247 return;
1248 }
1249 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1250 {
1251 // Invoke callback with current index.
1252 res.vval.v_number = wp->w_cursor.lnum;
1253 popup_close_and_callback(wp, &res);
1254 return;
1255 }
1256
1257 filter_handle_drag(wp, c, rettv);
1258}
1259
1260/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001261 * popup_filter_yesno({text}, {options})
1262 */
1263 void
1264f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1265{
1266 int id = tv_get_number(&argvars[0]);
1267 win_T *wp = win_id2wp(id);
1268 char_u *key = tv_get_string(&argvars[1]);
1269 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001270 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001271
1272 // If the popup has been closed don't consume the key.
1273 if (wp == NULL)
1274 return;
1275
Bram Moolenaara730e552019-06-16 19:05:31 +02001276 c = *key;
1277 if (c == K_SPECIAL && key[1] != NUL)
1278 c = TO_SPECIAL(key[1], key[2]);
1279
Bram Moolenaara42d9452019-06-15 21:46:30 +02001280 // consume all keys until done
1281 rettv->vval.v_number = 1;
1282
Bram Moolenaara730e552019-06-16 19:05:31 +02001283 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001284 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001285 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001286 res.vval.v_number = 0;
1287 else
1288 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001289 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001290 return;
1291 }
1292
1293 // Invoke callback
1294 res.v_type = VAR_NUMBER;
1295 popup_close_and_callback(wp, &res);
1296}
1297
1298/*
1299 * popup_dialog({text}, {options})
1300 */
1301 void
1302f_popup_dialog(typval_T *argvars, typval_T *rettv)
1303{
1304 popup_create(argvars, rettv, TYPE_DIALOG);
1305}
1306
1307/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001308 * popup_menu({text}, {options})
1309 */
1310 void
1311f_popup_menu(typval_T *argvars, typval_T *rettv)
1312{
1313 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1314
1315 if (wp != NULL)
1316 popup_highlight_curline(wp);
1317}
1318
1319/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001320 * popup_notification({text}, {options})
1321 */
1322 void
1323f_popup_notification(typval_T *argvars, typval_T *rettv)
1324{
1325 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1326}
1327
1328/*
1329 * Find the popup window with window-ID "id".
1330 * If the popup window does not exist NULL is returned.
1331 * If the window is not a popup window, and error message is given.
1332 */
1333 static win_T *
1334find_popup_win(int id)
1335{
1336 win_T *wp = win_id2wp(id);
1337
1338 if (wp != NULL && !bt_popup(wp->w_buffer))
1339 {
1340 semsg(_("E993: window %d is not a popup window"), id);
1341 return NULL;
1342 }
1343 return wp;
1344}
1345
1346/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001347 * popup_close({id})
1348 */
1349 void
1350f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1351{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001352 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001353 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001354
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001355 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001356 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001357}
1358
1359/*
1360 * popup_hide({id})
1361 */
1362 void
1363f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1364{
1365 int id = (int)tv_get_number(argvars);
1366 win_T *wp = find_popup_win(id);
1367
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001368 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001369 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001370 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001371 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001372 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001373 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001374 }
1375}
1376
1377/*
1378 * popup_show({id})
1379 */
1380 void
1381f_popup_show(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 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001393}
1394
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001395/*
1396 * popup_settext({id}, {text})
1397 */
1398 void
1399f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1400{
1401 int id = (int)tv_get_number(&argvars[0]);
1402 win_T *wp = find_popup_win(id);
1403
1404 if (wp != NULL)
1405 {
1406 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1407 popup_adjust_position(wp);
1408 }
1409}
1410
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001411 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001412popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001413{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001414 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001415 if (wp->w_winrow + wp->w_height >= cmdline_row)
1416 clear_cmdline = TRUE;
1417 win_free_popup(wp);
1418 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001419 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001420}
1421
Bram Moolenaarec583842019-05-26 14:11:23 +02001422/*
1423 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001424 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001425 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001426 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001427popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001428{
1429 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001430 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001431 win_T *prev = NULL;
1432
Bram Moolenaarec583842019-05-26 14:11:23 +02001433 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001434 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001435 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001436 {
1437 if (prev == NULL)
1438 first_popupwin = wp->w_next;
1439 else
1440 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001441 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001442 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001443 }
1444
Bram Moolenaarec583842019-05-26 14:11:23 +02001445 // go through tab-local popups
1446 FOR_ALL_TABPAGES(tp)
1447 popup_close_tabpage(tp, id);
1448}
1449
1450/*
1451 * Close a popup window with Window-id "id" in tabpage "tp".
1452 */
1453 void
1454popup_close_tabpage(tabpage_T *tp, int id)
1455{
1456 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001457 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001458 win_T *prev = NULL;
1459
Bram Moolenaarec583842019-05-26 14:11:23 +02001460 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1461 if (wp->w_id == id)
1462 {
1463 if (prev == NULL)
1464 *root = wp->w_next;
1465 else
1466 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001467 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001468 return;
1469 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001470}
1471
1472 void
1473close_all_popups(void)
1474{
1475 while (first_popupwin != NULL)
1476 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001477 while (curtab->tp_first_popupwin != NULL)
1478 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001479}
1480
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001481/*
1482 * popup_move({id}, {options})
1483 */
1484 void
1485f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1486{
Bram Moolenaarae943152019-06-16 22:54:14 +02001487 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001488 int id = (int)tv_get_number(argvars);
1489 win_T *wp = find_popup_win(id);
1490
1491 if (wp == NULL)
1492 return; // invalid {id}
1493
1494 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1495 {
1496 emsg(_(e_dictreq));
1497 return;
1498 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001499 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001500
Bram Moolenaarae943152019-06-16 22:54:14 +02001501 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001502
1503 if (wp->w_winrow + wp->w_height >= cmdline_row)
1504 clear_cmdline = TRUE;
1505 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001506}
1507
Bram Moolenaarbc133542019-05-29 20:26:46 +02001508/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001509 * popup_setoptions({id}, {options})
1510 */
1511 void
1512f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1513{
1514 dict_T *dict;
1515 int id = (int)tv_get_number(argvars);
1516 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001517 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001518
1519 if (wp == NULL)
1520 return; // invalid {id}
1521
1522 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1523 {
1524 emsg(_(e_dictreq));
1525 return;
1526 }
1527 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001528 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001529
1530 apply_move_options(wp, dict);
1531 apply_general_options(wp, dict);
1532
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001533 if (old_firstline != wp->w_firstline)
1534 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001535 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001536 popup_adjust_position(wp);
1537}
1538
1539/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001540 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001541 */
1542 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001543f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001544{
1545 dict_T *dict;
1546 int id = (int)tv_get_number(argvars);
1547 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001548 int top_extra;
1549 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001550
1551 if (rettv_dict_alloc(rettv) == OK)
1552 {
1553 if (wp == NULL)
1554 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001555 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001556 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1557
Bram Moolenaarbc133542019-05-29 20:26:46 +02001558 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001559
Bram Moolenaarbc133542019-05-29 20:26:46 +02001560 dict_add_number(dict, "line", wp->w_winrow + 1);
1561 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001562 dict_add_number(dict, "width", wp->w_width + left_extra
1563 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1564 dict_add_number(dict, "height", wp->w_height + top_extra
1565 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001566
1567 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1568 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1569 dict_add_number(dict, "core_width", wp->w_width);
1570 dict_add_number(dict, "core_height", wp->w_height);
1571
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001572 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001573 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001574 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001575 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001576 }
1577}
1578
1579/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001580 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1581 */
1582 static void
1583get_padding_border(dict_T *dict, int *array, char *name)
1584{
1585 list_T *list;
1586 int i;
1587
1588 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1589 return;
1590
1591 list = list_alloc();
1592 if (list != NULL)
1593 {
1594 dict_add_list(dict, name, list);
1595 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1596 for (i = 0; i < 4; ++i)
1597 list_append_number(list, array[i]);
1598 }
1599}
1600
1601/*
1602 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1603 */
1604 static void
1605get_borderhighlight(dict_T *dict, win_T *wp)
1606{
1607 list_T *list;
1608 int i;
1609
1610 for (i = 0; i < 4; ++i)
1611 if (wp->w_border_highlight[i] != NULL)
1612 break;
1613 if (i == 4)
1614 return;
1615
1616 list = list_alloc();
1617 if (list != NULL)
1618 {
1619 dict_add_list(dict, "borderhighlight", list);
1620 for (i = 0; i < 4; ++i)
1621 list_append_string(list, wp->w_border_highlight[i], -1);
1622 }
1623}
1624
1625/*
1626 * For popup_getoptions(): add a "borderchars" entry to "dict".
1627 */
1628 static void
1629get_borderchars(dict_T *dict, win_T *wp)
1630{
1631 list_T *list;
1632 int i;
1633 char_u buf[NUMBUFLEN];
1634 int len;
1635
1636 for (i = 0; i < 8; ++i)
1637 if (wp->w_border_char[i] != 0)
1638 break;
1639 if (i == 8)
1640 return;
1641
1642 list = list_alloc();
1643 if (list != NULL)
1644 {
1645 dict_add_list(dict, "borderchars", list);
1646 for (i = 0; i < 8; ++i)
1647 {
1648 len = mb_char2bytes(wp->w_border_char[i], buf);
1649 list_append_string(list, buf, len);
1650 }
1651 }
1652}
1653
1654/*
1655 * For popup_getoptions(): add a "moved" entry to "dict".
1656 */
1657 static void
1658get_moved_list(dict_T *dict, win_T *wp)
1659{
1660 list_T *list;
1661
1662 list = list_alloc();
1663 if (list != NULL)
1664 {
1665 dict_add_list(dict, "moved", list);
1666 list_append_number(list, wp->w_popup_mincol);
1667 list_append_number(list, wp->w_popup_maxcol);
1668 }
1669}
1670
1671/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001672 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001673 */
1674 void
1675f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1676{
1677 dict_T *dict;
1678 int id = (int)tv_get_number(argvars);
1679 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001680 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001681 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001682
1683 if (rettv_dict_alloc(rettv) == OK)
1684 {
1685 if (wp == NULL)
1686 return;
1687
1688 dict = rettv->vval.v_dict;
1689 dict_add_number(dict, "line", wp->w_wantline);
1690 dict_add_number(dict, "col", wp->w_wantcol);
1691 dict_add_number(dict, "minwidth", wp->w_minwidth);
1692 dict_add_number(dict, "minheight", wp->w_minheight);
1693 dict_add_number(dict, "maxheight", wp->w_maxheight);
1694 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001695 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001696 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001697 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001698 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001699 dict_add_string(dict, "title", wp->w_popup_title);
1700 dict_add_number(dict, "wrap", wp->w_p_wrap);
1701 dict_add_number(dict, "drag", wp->w_popup_drag);
1702 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02001703 if (wp->w_scrollbar_highlight != NULL)
1704 dict_add_string(dict, "scrollbarhighlight",
1705 wp->w_scrollbar_highlight);
1706 if (wp->w_thumb_highlight != NULL)
1707 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02001708
Bram Moolenaara3fce622019-06-20 02:31:49 +02001709 // find the tabpage that holds this popup
1710 i = 1;
1711 FOR_ALL_TABPAGES(tp)
1712 {
1713 win_T *p;
1714
1715 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1716 if (p->w_id == id)
1717 break;
1718 if (p != NULL)
1719 break;
1720 ++i;
1721 }
1722 if (tp == NULL)
1723 i = -1; // must be global
1724 else if (tp == curtab)
1725 i = 0;
1726 dict_add_number(dict, "tabpage", i);
1727
Bram Moolenaarae943152019-06-16 22:54:14 +02001728 get_padding_border(dict, wp->w_popup_padding, "padding");
1729 get_padding_border(dict, wp->w_popup_border, "border");
1730 get_borderhighlight(dict, wp);
1731 get_borderchars(dict, wp);
1732 get_moved_list(dict, wp);
1733
1734 if (wp->w_filter_cb.cb_name != NULL)
1735 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1736 if (wp->w_close_cb.cb_name != NULL)
1737 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001738
1739 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1740 ++i)
1741 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1742 {
1743 dict_add_string(dict, "pos",
1744 (char_u *)poppos_entries[i].pp_name);
1745 break;
1746 }
1747
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001748# if defined(FEAT_TIMERS)
1749 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1750 ? (long)wp->w_popup_timer->tr_interval : 0L);
1751# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001752 }
1753}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001754
1755 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001756error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001757{
1758 if (bt_popup(curwin->w_buffer))
1759 {
1760 emsg(_("E994: Not allowed in a popup window"));
1761 return TRUE;
1762 }
1763 return FALSE;
1764}
1765
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001766/*
1767 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001768 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001769 */
1770 void
1771popup_reset_handled()
1772{
1773 win_T *wp;
1774
1775 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1776 wp->w_popup_flags &= ~POPF_HANDLED;
1777 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1778 wp->w_popup_flags &= ~POPF_HANDLED;
1779}
1780
1781/*
1782 * Find the next visible popup where POPF_HANDLED is not set.
1783 * Must have called popup_reset_handled() first.
1784 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1785 * popup with the highest zindex.
1786 */
1787 win_T *
1788find_next_popup(int lowest)
1789{
1790 win_T *wp;
1791 win_T *found_wp;
1792 int found_zindex;
1793
1794 found_zindex = lowest ? INT_MAX : 0;
1795 found_wp = NULL;
1796 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1797 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1798 && (lowest ? wp->w_zindex < found_zindex
1799 : wp->w_zindex > found_zindex))
1800 {
1801 found_zindex = wp->w_zindex;
1802 found_wp = wp;
1803 }
1804 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1805 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1806 && (lowest ? wp->w_zindex < found_zindex
1807 : wp->w_zindex > found_zindex))
1808 {
1809 found_zindex = wp->w_zindex;
1810 found_wp = wp;
1811 }
1812
1813 if (found_wp != NULL)
1814 found_wp->w_popup_flags |= POPF_HANDLED;
1815 return found_wp;
1816}
1817
1818/*
1819 * Invoke the filter callback for window "wp" with typed character "c".
1820 * Uses the global "mod_mask" for modifiers.
1821 * Returns the return value of the filter.
1822 * Careful: The filter may make "wp" invalid!
1823 */
1824 static int
1825invoke_popup_filter(win_T *wp, int c)
1826{
1827 int res;
1828 typval_T rettv;
1829 int dummy;
1830 typval_T argv[3];
1831 char_u buf[NUMBUFLEN];
1832
Bram Moolenaara42d9452019-06-15 21:46:30 +02001833 // Emergency exit: CTRL-C closes the popup.
1834 if (c == Ctrl_C)
1835 {
1836 rettv.v_type = VAR_NUMBER;
1837 rettv.vval.v_number = -1;
1838 popup_close_and_callback(wp, &rettv);
1839 return 1;
1840 }
1841
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001842 argv[0].v_type = VAR_NUMBER;
1843 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1844
1845 // Convert the number to a string, so that the function can use:
1846 // if a:c == "\<F2>"
1847 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1848 argv[1].v_type = VAR_STRING;
1849 argv[1].vval.v_string = vim_strsave(buf);
1850
1851 argv[2].v_type = VAR_UNKNOWN;
1852
Bram Moolenaara42d9452019-06-15 21:46:30 +02001853 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001854 call_callback(&wp->w_filter_cb, -1,
1855 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1856 res = tv_get_number(&rettv);
1857 vim_free(argv[1].vval.v_string);
1858 clear_tv(&rettv);
1859 return res;
1860}
1861
1862/*
1863 * Called when "c" was typed: invoke popup filter callbacks.
1864 * Returns TRUE when the character was consumed,
1865 */
1866 int
1867popup_do_filter(int c)
1868{
1869 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001870 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001871
1872 popup_reset_handled();
1873
1874 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1875 if (wp->w_filter_cb.cb_name != NULL)
1876 res = invoke_popup_filter(wp, c);
1877
1878 return res;
1879}
1880
Bram Moolenaar3397f742019-06-02 18:40:06 +02001881/*
1882 * Called when the cursor moved: check if any popup needs to be closed if the
1883 * cursor moved far enough.
1884 */
1885 void
1886popup_check_cursor_pos()
1887{
1888 win_T *wp;
1889 typval_T tv;
1890
1891 popup_reset_handled();
1892 while ((wp = find_next_popup(TRUE)) != NULL)
1893 if (wp->w_popup_curwin != NULL
1894 && (curwin != wp->w_popup_curwin
1895 || curwin->w_cursor.lnum != wp->w_popup_lnum
1896 || curwin->w_cursor.col < wp->w_popup_mincol
1897 || curwin->w_cursor.col > wp->w_popup_maxcol))
1898 {
1899 tv.v_type = VAR_NUMBER;
1900 tv.vval.v_number = -1;
1901 popup_close_and_callback(wp, &tv);
1902 }
1903}
1904
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001905/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001906 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
1907 * "col" and "line" are screen coordinates.
1908 */
1909 static int
1910popup_masked(win_T *wp, int screencol, int screenline)
1911{
1912 int col = screencol - wp->w_wincol + 1;
1913 int line = screenline - wp->w_winrow + 1;
1914 listitem_T *lio, *li;
1915 int width, height;
1916
1917 if (wp->w_popup_mask == NULL)
1918 return FALSE;
1919 width = popup_width(wp);
1920 height = popup_height(wp);
1921
1922 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1923 {
1924 int cols, cole;
1925 int lines, linee;
1926
1927 li = lio->li_tv.vval.v_list->lv_first;
1928 cols = tv_get_number(&li->li_tv);
1929 if (cols < 0)
1930 cols = width + cols + 1;
1931 if (col < cols)
1932 continue;
1933 li = li->li_next;
1934 cole = tv_get_number(&li->li_tv);
1935 if (cole < 0)
1936 cole = width + cole + 1;
1937 if (col > cole)
1938 continue;
1939 li = li->li_next;
1940 lines = tv_get_number(&li->li_tv);
1941 if (lines < 0)
1942 lines = height + lines + 1;
1943 if (line < lines)
1944 continue;
1945 li = li->li_next;
1946 linee = tv_get_number(&li->li_tv);
1947 if (linee < 0)
1948 linee = height + linee + 1;
1949 if (line > linee)
1950 continue;
1951 return TRUE;
1952 }
1953 return FALSE;
1954}
1955
1956/*
1957 * Set flags in popup_transparent[] for window "wp" to "val".
1958 */
1959 static void
1960update_popup_transparent(win_T *wp, int val)
1961{
1962 if (wp->w_popup_mask != NULL)
1963 {
1964 int width = popup_width(wp);
1965 int height = popup_height(wp);
1966 listitem_T *lio, *li;
1967 int cols, cole;
1968 int lines, linee;
1969 int col, line;
1970
1971 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
1972 {
1973 li = lio->li_tv.vval.v_list->lv_first;
1974 cols = tv_get_number(&li->li_tv);
1975 if (cols < 0)
1976 cols = width + cols + 1;
1977 li = li->li_next;
1978 cole = tv_get_number(&li->li_tv);
1979 if (cole < 0)
1980 cole = width + cole + 1;
1981 li = li->li_next;
1982 lines = tv_get_number(&li->li_tv);
1983 if (lines < 0)
1984 lines = height + lines + 1;
1985 li = li->li_next;
1986 linee = tv_get_number(&li->li_tv);
1987 if (linee < 0)
1988 linee = height + linee + 1;
1989
1990 --cols;
1991 --lines;
1992 for (line = lines; line < linee && line < screen_Rows; ++line)
1993 for (col = cols; col < cole && col < screen_Columns; ++col)
1994 popup_transparent[(line + wp->w_winrow) * screen_Columns
1995 + col + wp->w_wincol] = val;
1996 }
1997 }
1998}
1999
2000/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002001 * Update "popup_mask" if needed.
2002 * Also recomputes the popup size and positions.
2003 * Also updates "popup_visible".
2004 * Also marks window lines for redrawing.
2005 */
2006 void
2007may_update_popup_mask(int type)
2008{
2009 win_T *wp;
2010 short *mask;
2011 int line, col;
2012 int redraw_all = FALSE;
2013
2014 // Need to recompute when switching tabs.
2015 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2016 // (such as the screen size) must have changed.
2017 if (popup_mask_tab != curtab || type >= NOT_VALID)
2018 {
2019 popup_mask_refresh = TRUE;
2020 redraw_all = TRUE;
2021 }
2022 if (!popup_mask_refresh)
2023 {
2024 // Check if any buffer has changed.
2025 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2026 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2027 popup_mask_refresh = TRUE;
2028 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2029 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2030 popup_mask_refresh = TRUE;
2031 if (!popup_mask_refresh)
2032 return;
2033 }
2034
2035 // Need to update the mask, something has changed.
2036 popup_mask_refresh = FALSE;
2037 popup_mask_tab = curtab;
2038 popup_visible = FALSE;
2039
2040 // If redrawing everything, just update "popup_mask".
2041 // If redrawing only what is needed, update "popup_mask_next" and then
2042 // compare with "popup_mask" to see what changed.
2043 if (type >= SOME_VALID)
2044 mask = popup_mask;
2045 else
2046 mask = popup_mask_next;
2047 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2048
2049 // Find the window with the lowest zindex that hasn't been handled yet,
2050 // so that the window with a higher zindex overwrites the value in
2051 // popup_mask.
2052 popup_reset_handled();
2053 while ((wp = find_next_popup(TRUE)) != NULL)
2054 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002055 int height = popup_height(wp);
2056 int width = popup_width(wp);
2057
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002058 popup_visible = TRUE;
2059
2060 // Recompute the position if the text changed.
2061 if (redraw_all
2062 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2063 popup_adjust_position(wp);
2064
2065 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002066 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002067 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002068 col < wp->w_wincol + width && col < screen_Columns; ++col)
2069 if (!popup_masked(wp, col, line))
2070 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002071 }
2072
2073 // Only check which lines are to be updated if not already
2074 // updating all lines.
2075 if (mask == popup_mask_next)
2076 for (line = 0; line < screen_Rows; ++line)
2077 {
2078 int col_done = 0;
2079
2080 for (col = 0; col < screen_Columns; ++col)
2081 {
2082 int off = line * screen_Columns + col;
2083
2084 if (popup_mask[off] != popup_mask_next[off])
2085 {
2086 popup_mask[off] = popup_mask_next[off];
2087
2088 if (line >= cmdline_row)
2089 {
2090 // the command line needs to be cleared if text below
2091 // the popup is now visible.
2092 if (!msg_scrolled && popup_mask_next[off] == 0)
2093 clear_cmdline = TRUE;
2094 }
2095 else if (col >= col_done)
2096 {
2097 linenr_T lnum;
2098 int line_cp = line;
2099 int col_cp = col;
2100
2101 // The screen position "line" / "col" needs to be
2102 // redrawn. Figure out what window that is and update
2103 // w_redraw_top and w_redr_bot. Only needs to be done
2104 // once for each window line.
2105 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2106 if (wp != NULL)
2107 {
2108 if (line_cp >= wp->w_height)
2109 // In (or below) status line
2110 wp->w_redr_status = TRUE;
2111 // compute the position in the buffer line from the
2112 // position on the screen
2113 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2114 &lnum))
2115 // past bottom
2116 wp->w_redr_status = TRUE;
2117 else
2118 redrawWinline(wp, lnum);
2119
2120 // This line is going to be redrawn, no need to
2121 // check until the right side of the window.
2122 col_done = wp->w_wincol + wp->w_width - 1;
2123 }
2124 }
2125 }
2126 }
2127 }
2128}
2129
2130/*
2131 * Return a string of "len" spaces in IObuff.
2132 */
2133 static char_u *
2134get_spaces(int len)
2135{
2136 vim_memset(IObuff, ' ', (size_t)len);
2137 IObuff[len] = NUL;
2138 return IObuff;
2139}
2140
2141/*
2142 * Update popup windows. They are drawn on top of normal windows.
2143 * "win_update" is called for each popup window, lowest zindex first.
2144 */
2145 void
2146update_popups(void (*win_update)(win_T *wp))
2147{
2148 win_T *wp;
2149 int top_off;
2150 int left_off;
2151 int total_width;
2152 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002153 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002154 int popup_attr;
2155 int border_attr[4];
2156 int border_char[8];
2157 char_u buf[MB_MAXBYTES];
2158 int row;
2159 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002160 int sb_thumb_top = 0;
2161 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002162 int attr_scroll = 0;
2163 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002164
2165 // Find the window with the lowest zindex that hasn't been updated yet,
2166 // so that the window with a higher zindex is drawn later, thus goes on
2167 // top.
2168 popup_reset_handled();
2169 while ((wp = find_next_popup(TRUE)) != NULL)
2170 {
2171 // This drawing uses the zindex of the popup window, so that it's on
2172 // top of the text but doesn't draw when another popup with higher
2173 // zindex is on top of the character.
2174 screen_zindex = wp->w_zindex;
2175
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002176 // Set flags in popup_transparent[] for masked cells.
2177 update_popup_transparent(wp, 1);
2178
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002179 // adjust w_winrow and w_wincol for border and padding, since
2180 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002181 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002182 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2183 wp->w_winrow += top_off;
2184 wp->w_wincol += left_off;
2185
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002186 // Draw the popup text, unless it's off screen.
2187 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2188 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002189
2190 wp->w_winrow -= top_off;
2191 wp->w_wincol -= left_off;
2192
2193 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002194 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1]
2195 + wp->w_has_scrollbar;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002196 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002197 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2198 popup_attr = get_wcr_attr(wp);
2199
2200 // We can only use these line drawing characters when 'encoding' is
2201 // "utf-8" and 'ambiwidth' is "single".
2202 if (enc_utf8 && *p_ambw == 's')
2203 {
2204 border_char[0] = border_char[2] = 0x2550;
2205 border_char[1] = border_char[3] = 0x2551;
2206 border_char[4] = 0x2554;
2207 border_char[5] = 0x2557;
2208 border_char[6] = 0x255d;
2209 border_char[7] = 0x255a;
2210 }
2211 else
2212 {
2213 border_char[0] = border_char[2] = '-';
2214 border_char[1] = border_char[3] = '|';
2215 for (i = 4; i < 8; ++i)
2216 border_char[i] = '+';
2217 }
2218 for (i = 0; i < 8; ++i)
2219 if (wp->w_border_char[i] != 0)
2220 border_char[i] = wp->w_border_char[i];
2221
2222 for (i = 0; i < 4; ++i)
2223 {
2224 border_attr[i] = popup_attr;
2225 if (wp->w_border_highlight[i] != NULL)
2226 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2227 }
2228
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002229 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002230 if (wp->w_popup_border[0] > 0)
2231 {
2232 // top border
2233 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2234 wp->w_wincol,
2235 wp->w_wincol + total_width,
2236 wp->w_popup_border[3] != 0
2237 ? border_char[4] : border_char[0],
2238 border_char[0], border_attr[0]);
2239 if (wp->w_popup_border[1] > 0)
2240 {
2241 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2242 screen_puts(buf, wp->w_winrow,
2243 wp->w_wincol + total_width - 1, border_attr[1]);
2244 }
2245 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002246 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2247 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002248
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002249 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002250 {
2251 // top padding
2252 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002253 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002254 wp->w_wincol + wp->w_popup_border[3],
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002255 wp->w_wincol + total_width - wp->w_popup_border[1]
2256 - wp->w_has_scrollbar,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002257 ' ', ' ', popup_attr);
2258 }
2259
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002260 // Title goes on top of border or padding.
2261 if (wp->w_popup_title != NULL)
2262 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2263 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2264
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002265 // Compute scrollbar thumb position and size.
2266 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002267 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002268 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2269
Bram Moolenaar68acb412019-06-26 03:40:36 +02002270 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2271 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002272 if (sb_thumb_height == 0)
2273 sb_thumb_height = 1;
Bram Moolenaar68acb412019-06-26 03:40:36 +02002274 sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2)
2275 * (wp->w_height - sb_thumb_height)
2276 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002277 if (wp->w_scrollbar_highlight != NULL)
2278 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2279 else
2280 attr_scroll = highlight_attr[HLF_PSB];
2281 if (wp->w_thumb_highlight != NULL)
2282 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2283 else
2284 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002285 }
2286
2287 for (i = wp->w_popup_border[0];
2288 i < total_height - wp->w_popup_border[2]; ++i)
2289 {
2290 row = wp->w_winrow + i;
2291
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002292 // left border
2293 if (wp->w_popup_border[3] > 0)
2294 {
2295 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2296 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2297 }
2298 // left padding
2299 if (wp->w_popup_padding[3] > 0)
2300 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2301 wp->w_wincol + wp->w_popup_border[3], popup_attr);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002302 // scrollbar
2303 if (wp->w_has_scrollbar)
2304 {
2305 int line = i - top_off;
2306 int scroll_col = wp->w_wincol + total_width - 1
2307 - wp->w_popup_border[1];
2308
2309 if (line >= 0 && line < wp->w_height)
2310 screen_putchar(' ', row, scroll_col,
2311 line >= sb_thumb_top
2312 && line < sb_thumb_top + sb_thumb_height
2313 ? attr_thumb : attr_scroll);
2314 else
2315 screen_putchar(' ', row, scroll_col, popup_attr);
2316 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002317 // right border
2318 if (wp->w_popup_border[1] > 0)
2319 {
2320 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2321 screen_puts(buf, row,
2322 wp->w_wincol + total_width - 1, border_attr[1]);
2323 }
2324 // right padding
2325 if (wp->w_popup_padding[1] > 0)
2326 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2327 wp->w_wincol + wp->w_popup_border[3]
2328 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2329 }
2330
2331 if (wp->w_popup_padding[2] > 0)
2332 {
2333 // bottom padding
2334 row = wp->w_winrow + wp->w_popup_border[0]
2335 + wp->w_popup_padding[0] + wp->w_height;
2336 screen_fill(row, row + wp->w_popup_padding[2],
2337 wp->w_wincol + wp->w_popup_border[3],
2338 wp->w_wincol + total_width - wp->w_popup_border[1],
2339 ' ', ' ', popup_attr);
2340 }
2341
2342 if (wp->w_popup_border[2] > 0)
2343 {
2344 // bottom border
2345 row = wp->w_winrow + total_height - 1;
2346 screen_fill(row , row + 1,
2347 wp->w_wincol,
2348 wp->w_wincol + total_width,
2349 wp->w_popup_border[3] != 0
2350 ? border_char[7] : border_char[2],
2351 border_char[2], border_attr[2]);
2352 if (wp->w_popup_border[1] > 0)
2353 {
2354 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2355 screen_puts(buf, row,
2356 wp->w_wincol + total_width - 1, border_attr[2]);
2357 }
2358 }
2359
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002360 update_popup_transparent(wp, 0);
2361
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002362 // Back to the normal zindex.
2363 screen_zindex = 0;
2364 }
2365}
2366
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002367/*
2368 * Mark references in callbacks of one popup window.
2369 */
2370 static int
2371set_ref_in_one_popup(win_T *wp, int copyID)
2372{
2373 int abort = FALSE;
2374 typval_T tv;
2375
2376 if (wp->w_close_cb.cb_partial != NULL)
2377 {
2378 tv.v_type = VAR_PARTIAL;
2379 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2380 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2381 }
2382 if (wp->w_filter_cb.cb_partial != NULL)
2383 {
2384 tv.v_type = VAR_PARTIAL;
2385 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2386 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2387 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002388 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002389 return abort;
2390}
2391
2392/*
2393 * Set reference in callbacks of popup windows.
2394 */
2395 int
2396set_ref_in_popups(int copyID)
2397{
2398 int abort = FALSE;
2399 win_T *wp;
2400 tabpage_T *tp;
2401
2402 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2403 abort = abort || set_ref_in_one_popup(wp, copyID);
2404
2405 FOR_ALL_TABPAGES(tp)
2406 {
2407 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2408 abort = abort || set_ref_in_one_popup(wp, copyID);
2409 if (abort)
2410 break;
2411 }
2412 return abort;
2413}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002414#endif // FEAT_TEXT_PROP