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