blob: c042fe82d8608fe23f852cb311ac014030946291 [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
109get_padding_border(dict_T *dict, int *array, char *name, int max_val)
110{
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/*
254 * Go through the options in "dict" and apply them to buffer "buf" displayed in
255 * popup window "wp".
256 */
257 static void
258apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200259{
Bram Moolenaar402502d2019-05-30 22:07:36 +0200260 int nr;
261 char_u *str;
262 dictitem_T *di;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200263 int i;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200264
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200265 di = dict_find(dict, (char_u *)"minwidth", -1);
266 if (di != NULL)
267 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200268 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200269 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
270 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200271
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200272 get_pos_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200273
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200274 di = dict_find(dict, (char_u *)"zindex", -1);
275 if (di != NULL)
276 {
277 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
278 if (wp->w_zindex < 1)
279 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
280 if (wp->w_zindex > 32000)
281 wp->w_zindex = 32000;
282 }
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200283
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200284#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200285 // Add timer to close the popup after some time.
286 nr = dict_get_number(dict, (char_u *)"time");
287 if (nr > 0)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200288 popup_add_timeout(wp, nr);
289#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200290
Bram Moolenaar402502d2019-05-30 22:07:36 +0200291 // Option values resulting in setting an option.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200292 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200293 if (str != NULL)
294 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
295 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200296
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200297 str = dict_get_string(dict, (char_u *)"title", FALSE);
298 if (str != NULL)
299 {
300 vim_free(wp->w_popup_title);
301 wp->w_popup_title = vim_strsave(str);
302 }
303
Bram Moolenaar8d241042019-06-12 23:40:01 +0200304 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
305 if (wp->w_firstline < 1)
306 wp->w_firstline = 1;
307
Bram Moolenaar402502d2019-05-30 22:07:36 +0200308 di = dict_find(dict, (char_u *)"wrap", -1);
309 if (di != NULL)
310 {
311 nr = dict_get_number(dict, (char_u *)"wrap");
312 wp->w_p_wrap = nr != 0;
313 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200314
Bram Moolenaara42d9452019-06-15 21:46:30 +0200315 di = dict_find(dict, (char_u *)"drag", -1);
316 if (di != NULL)
317 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200318
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200319 di = dict_find(dict, (char_u *)"callback", -1);
320 if (di != NULL)
321 {
322 callback_T callback = get_callback(&di->di_tv);
323
324 if (callback.cb_name != NULL)
325 set_callback(&wp->w_close_cb, &callback);
326 }
327
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200328 di = dict_find(dict, (char_u *)"filter", -1);
329 if (di != NULL)
330 {
331 callback_T callback = get_callback(&di->di_tv);
332
333 if (callback.cb_name != NULL)
334 set_callback(&wp->w_filter_cb, &callback);
335 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200336
337 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
338 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200339
340 for (i = 0; i < 4; ++i)
341 VIM_CLEAR(wp->w_border_highlight[i]);
342 di = dict_find(dict, (char_u *)"borderhighlight", -1);
343 if (di != NULL)
344 {
345 if (di->di_tv.v_type != VAR_LIST)
346 emsg(_(e_listreq));
347 else
348 {
349 list_T *list = di->di_tv.vval.v_list;
350 listitem_T *li;
351
352 if (list != NULL)
353 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
354 ++i, li = li->li_next)
355 {
356 str = tv_get_string(&li->li_tv);
357 if (*str != NUL)
358 wp->w_border_highlight[i] = vim_strsave(str);
359 }
360 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
361 for (i = 1; i < 4; ++i)
362 wp->w_border_highlight[i] =
363 vim_strsave(wp->w_border_highlight[0]);
364 }
365 }
366
367 for (i = 0; i < 8; ++i)
368 wp->w_border_char[i] = 0;
369 di = dict_find(dict, (char_u *)"borderchars", -1);
370 if (di != NULL)
371 {
372 if (di->di_tv.v_type != VAR_LIST)
373 emsg(_(e_listreq));
374 else
375 {
376 list_T *list = di->di_tv.vval.v_list;
377 listitem_T *li;
378
379 if (list != NULL)
380 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
381 ++i, li = li->li_next)
382 {
383 str = tv_get_string(&li->li_tv);
384 if (*str != NUL)
385 wp->w_border_char[i] = mb_ptr2char(str);
386 }
387 if (list->lv_len == 1)
388 for (i = 1; i < 8; ++i)
389 wp->w_border_char[i] = wp->w_border_char[0];
390 if (list->lv_len == 2)
391 {
392 for (i = 4; i < 8; ++i)
393 wp->w_border_char[i] = wp->w_border_char[1];
394 for (i = 1; i < 4; ++i)
395 wp->w_border_char[i] = wp->w_border_char[0];
396 }
397 }
398 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200399
400 di = dict_find(dict, (char_u *)"moved", -1);
401 if (di != NULL)
402 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200403 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200404 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
405 {
406 char_u *s = di->di_tv.vval.v_string;
407 int flags = 0;
408
409 if (STRCMP(s, "word") == 0)
410 flags = FIND_IDENT | FIND_STRING;
411 else if (STRCMP(s, "WORD") == 0)
412 flags = FIND_STRING;
413 else if (STRCMP(s, "any") != 0)
414 semsg(_(e_invarg2), s);
415 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200416 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200417 }
418 else if (di->di_tv.v_type == VAR_LIST
419 && di->di_tv.vval.v_list != NULL
420 && di->di_tv.vval.v_list->lv_len == 2)
421 {
422 list_T *l = di->di_tv.vval.v_list;
423
424 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
425 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
426 }
427 else
428 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
429 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200430
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200431 nr = dict_get_number(dict, (char_u *)"hidden");
432 if (nr > 0)
433 {
434 wp->w_popup_flags |= POPF_HIDDEN;
435 --wp->w_buffer->b_nwindows;
436 }
437
Bram Moolenaar33796b32019-06-08 16:01:13 +0200438 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200439}
440
441/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200442 * Add lines to the popup from a list of strings.
443 */
444 static void
445add_popup_strings(buf_T *buf, list_T *l)
446{
447 listitem_T *li;
448 linenr_T lnum = 0;
449 char_u *p;
450
451 for (li = l->lv_first; li != NULL; li = li->li_next)
452 if (li->li_tv.v_type == VAR_STRING)
453 {
454 p = li->li_tv.vval.v_string;
455 ml_append_buf(buf, lnum++,
456 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
457 }
458}
459
460/*
461 * Add lines to the popup from a list of dictionaries.
462 */
463 static void
464add_popup_dicts(buf_T *buf, list_T *l)
465{
466 listitem_T *li;
467 listitem_T *pli;
468 linenr_T lnum = 0;
469 char_u *p;
470 dict_T *dict;
471
472 // first add the text lines
473 for (li = l->lv_first; li != NULL; li = li->li_next)
474 {
475 if (li->li_tv.v_type != VAR_DICT)
476 {
477 emsg(_(e_dictreq));
478 return;
479 }
480 dict = li->li_tv.vval.v_dict;
481 p = dict == NULL ? NULL
482 : dict_get_string(dict, (char_u *)"text", FALSE);
483 ml_append_buf(buf, lnum++,
484 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
485 }
486
487 // add the text properties
488 lnum = 1;
489 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
490 {
491 dictitem_T *di;
492 list_T *plist;
493
494 dict = li->li_tv.vval.v_dict;
495 di = dict_find(dict, (char_u *)"props", -1);
496 if (di != NULL)
497 {
498 if (di->di_tv.v_type != VAR_LIST)
499 {
500 emsg(_(e_listreq));
501 return;
502 }
503 plist = di->di_tv.vval.v_list;
504 if (plist != NULL)
505 {
506 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
507 {
508 if (pli->li_tv.v_type != VAR_DICT)
509 {
510 emsg(_(e_dictreq));
511 return;
512 }
513 dict = pli->li_tv.vval.v_dict;
514 if (dict != NULL)
515 {
516 int col = dict_get_number(dict, (char_u *)"col");
517
518 prop_add_common( lnum, col, dict, buf, NULL);
519 }
520 }
521 }
522 }
523 }
524}
525
526/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200527 * Return the height of popup window "wp", including border and padding.
528 */
529 int
530popup_height(win_T *wp)
531{
532 return wp->w_height
533 + wp->w_popup_padding[0] + wp->w_popup_border[0]
534 + wp->w_popup_padding[2] + wp->w_popup_border[2];
535}
536
537/*
538 * Return the width of popup window "wp", including border and padding.
539 */
540 int
541popup_width(win_T *wp)
542{
543 return wp->w_width
544 + wp->w_popup_padding[3] + wp->w_popup_border[3]
545 + wp->w_popup_padding[1] + wp->w_popup_border[1];
546}
547
548/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200549 * Get the padding plus border at the top, adjusted to 1 if there is a title.
550 */
551 static int
552popup_top_extra(win_T *wp)
553{
554 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
555
556 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
557 return 1;
558 return extra;
559}
560
561/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200562 * Adjust the position and size of the popup to fit on the screen.
563 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200564 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200565popup_adjust_position(win_T *wp)
566{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200567 linenr_T lnum;
568 int wrapped = 0;
569 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200570 int center_vert = FALSE;
571 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200572 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200573 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200574 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
575 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
576 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
577 int extra_height = top_extra + bot_extra;
578 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200579 int org_winrow = wp->w_winrow;
580 int org_wincol = wp->w_wincol;
581 int org_width = wp->w_width;
582 int org_height = wp->w_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200583 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200584
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200585 wp->w_winrow = 0;
586 wp->w_wincol = 0;
587 if (wp->w_popup_pos == POPPOS_CENTER)
588 {
589 // center after computing the size
590 center_vert = TRUE;
591 center_hor = TRUE;
592 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200593 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200594 {
595 if (wp->w_wantline == 0)
596 center_vert = TRUE;
597 else if (wp->w_popup_pos == POPPOS_TOPLEFT
598 || wp->w_popup_pos == POPPOS_TOPRIGHT)
599 {
600 wp->w_winrow = wp->w_wantline - 1;
601 if (wp->w_winrow >= Rows)
602 wp->w_winrow = Rows - 1;
603 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200604
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200605 if (wp->w_wantcol == 0)
606 center_hor = TRUE;
607 else if (wp->w_popup_pos == POPPOS_TOPLEFT
608 || wp->w_popup_pos == POPPOS_BOTLEFT)
609 {
610 wp->w_wincol = wp->w_wantcol - 1;
611 if (wp->w_wincol >= Columns - 3)
612 wp->w_wincol = Columns - 3;
613 }
614 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200615
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200616 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200617 // When left aligned use the space available, but shift to the left when we
618 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200619 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200620 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200621 {
622 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200623 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200624 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200625
Bram Moolenaar8d241042019-06-12 23:40:01 +0200626 // start at the desired first line
627 wp->w_topline = wp->w_firstline;
628 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
629 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
630
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200631 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200632 // Use a minimum width of one, so that something shows when there is no
633 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200634 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200635 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200636 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200637 {
638 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
639
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200640 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200641 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200642 while (len > maxwidth)
643 {
644 ++wrapped;
645 len -= maxwidth;
646 wp->w_width = maxwidth;
647 }
648 }
649 else if (len > maxwidth
650 && allow_adjust_left
651 && (wp->w_popup_pos == POPPOS_TOPLEFT
652 || wp->w_popup_pos == POPPOS_BOTLEFT))
653 {
654 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200655 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200656
Bram Moolenaar51c31312019-06-15 22:27:23 +0200657 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200658 {
659 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200660
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200661 len -= truncate_shift;
662 shift_by -= truncate_shift;
663 }
664
665 wp->w_wincol -= shift_by;
666 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200667 wp->w_width = maxwidth;
668 }
669 if (wp->w_width < len)
670 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200671 // do not use the width of lines we're not going to show
672 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
673 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
674 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200675 }
676
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200677 minwidth = wp->w_minwidth;
678 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
679 {
680 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
681
682 if (minwidth < title_len)
683 minwidth = title_len;
684 }
685
686 if (minwidth > 0 && wp->w_width < minwidth)
687 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200688 if (wp->w_width > maxwidth)
689 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200690 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200691 {
692 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
693 if (wp->w_wincol < 0)
694 wp->w_wincol = 0;
695 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200696 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
697 || wp->w_popup_pos == POPPOS_TOPRIGHT)
698 {
699 // Right aligned: move to the right if needed.
700 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200701 if (wp->w_width + extra_width < wp->w_wantcol)
702 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200703 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200704
Bram Moolenaar8d241042019-06-12 23:40:01 +0200705 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
706 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200707 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
708 wp->w_height = wp->w_minheight;
709 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
710 wp->w_height = wp->w_maxheight;
711 if (wp->w_height > Rows - wp->w_winrow)
712 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200713
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200714 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200715 {
716 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
717 if (wp->w_winrow < 0)
718 wp->w_winrow = 0;
719 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200720 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
721 || wp->w_popup_pos == POPPOS_BOTLEFT)
722 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200723 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200724 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200725 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200726 else
727 // not enough space, make top aligned
728 wp->w_winrow = wp->w_wantline + 1;
729 }
730
Bram Moolenaar17146962019-05-30 00:12:11 +0200731 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200732
733 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200734 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200735 if (org_winrow != wp->w_winrow
736 || org_wincol != wp->w_wincol
737 || org_width != wp->w_width
738 || org_height != wp->w_height)
739 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200740 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200741 popup_mask_refresh = TRUE;
742 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200743}
744
Bram Moolenaar17627312019-06-02 19:53:44 +0200745typedef enum
746{
747 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200748 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200749 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200750 TYPE_DIALOG,
751 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200752} create_type_T;
753
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200754/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200755 * Make "buf" empty and set the contents to "text".
756 * Used by popup_create() and popup_settext().
757 */
758 static void
759popup_set_buffer_text(buf_T *buf, typval_T text)
760{
761 int lnum;
762
763 // Clear the buffer, then replace the lines.
764 curbuf = buf;
765 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
766 ml_delete(lnum, FALSE);
767 curbuf = curwin->w_buffer;
768
769 // Add text to the buffer.
770 if (text.v_type == VAR_STRING)
771 {
772 // just a string
773 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
774 }
775 else
776 {
777 list_T *l = text.vval.v_list;
778
779 if (l->lv_len > 0)
780 {
781 if (l->lv_first->li_tv.v_type == VAR_STRING)
782 // list of strings
783 add_popup_strings(buf, l);
784 else
785 // list of dictionaries
786 add_popup_dicts(buf, l);
787 }
788 }
789
790 // delete the line that was in the empty buffer
791 curbuf = buf;
792 ml_delete(buf->b_ml.ml_line_count, FALSE);
793 curbuf = curwin->w_buffer;
794}
795
796/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200797 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200798 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200799 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200800 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200801popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200802{
803 win_T *wp;
804 buf_T *buf;
805 dict_T *d;
806 int nr;
807
808 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200809 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
810 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200811 {
812 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200813 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200814 }
815 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
816 {
817 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200818 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200819 }
820 d = argvars[1].vval.v_dict;
821
822 // Create the window and buffer.
823 wp = win_alloc_popup_win();
824 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200825 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200826 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200827 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200828
829 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
830 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200831 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200832 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200833
834 win_init_popup_win(wp, buf);
835
836 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200837 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200838 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200839 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200840 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200841 buf->b_p_ul = -1; // no undo
842 buf->b_p_swf = FALSE; // no swap file
843 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200844 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200845 wp->w_p_wrap = TRUE; // 'wrap' is default on
846
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200847 // Avoid that 'buftype' is reset when this buffer is entered.
848 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200849
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200850 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
851 nr = (int)dict_get_number(d, (char_u *)"tabpage");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200852 else if (type == TYPE_NOTIFICATION)
853 nr = -1; // notifications are global by default
854 else
855 nr = 0;
856
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200857 if (nr == 0)
858 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200859 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200860 wp->w_next = curtab->tp_first_popupwin;
861 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200862 }
863 else if (nr < 0)
864 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200865 win_T *prev = first_popupwin;
866
867 // Global popup: add at the end, so that it gets displayed on top of
868 // older ones with the same zindex. Matters for notifications.
869 if (first_popupwin == NULL)
870 first_popupwin = wp;
871 else
872 {
873 while (prev->w_next != NULL)
874 prev = prev->w_next;
875 prev->w_next = wp;
876 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200877 }
878 else
879 // TODO: find tab page "nr"
880 emsg("Not implemented yet");
881
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200882 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200883
Bram Moolenaar17627312019-06-02 19:53:44 +0200884 if (type == TYPE_ATCURSOR)
885 {
886 wp->w_popup_pos = POPPOS_BOTLEFT;
887 setcursor_mayforce(TRUE);
888 wp->w_wantline = screen_screenrow();
889 if (wp->w_wantline == 0) // cursor in first line
890 {
891 wp->w_wantline = 2;
892 wp->w_popup_pos = POPPOS_TOPLEFT;
893 }
894 wp->w_wantcol = screen_screencol() + 1;
895 set_moved_values(wp);
896 set_moved_columns(wp, FIND_STRING);
897 }
898
Bram Moolenaar33796b32019-06-08 16:01:13 +0200899 // set default values
900 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
901
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200902 if (type == TYPE_NOTIFICATION)
903 {
904 win_T *twp, *nextwin;
905 int height = buf->b_ml.ml_line_count + 3;
906 int i;
907
908 // Try to not overlap with another global popup. Guess we need 3
909 // more screen lines than buffer lines.
910 wp->w_wantline = 1;
911 for (twp = first_popupwin; twp != NULL; twp = nextwin)
912 {
913 nextwin = twp->w_next;
914 if (twp != wp
915 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
916 && twp->w_winrow <= wp->w_wantline - 1 + height
917 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
918 {
919 // move to below this popup and restart the loop to check for
920 // overlap with other popups
921 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
922 nextwin = first_popupwin;
923 }
924 }
925 if (wp->w_wantline + height > Rows)
926 {
927 // can't avoid overlap, put on top in the hope that message goes
928 // away soon.
929 wp->w_wantline = 1;
930 }
931
932 wp->w_wantcol = 10;
933 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200934 wp->w_minwidth = 20;
935 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200936 for (i = 0; i < 4; ++i)
937 wp->w_popup_border[i] = 1;
938 wp->w_popup_padding[1] = 1;
939 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200940
941 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200942 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200943 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
944 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200945 }
946
Bram Moolenaara730e552019-06-16 19:05:31 +0200947 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +0200948 {
949 int i;
950
951 wp->w_popup_pos = POPPOS_CENTER;
952 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
953 wp->w_popup_drag = 1;
954 for (i = 0; i < 4; ++i)
955 {
956 wp->w_popup_border[i] = 1;
957 wp->w_popup_padding[i] = 1;
958 }
959 }
960
Bram Moolenaara730e552019-06-16 19:05:31 +0200961 if (type == TYPE_MENU)
962 {
963 typval_T tv;
964 callback_T callback;
965
966 tv.v_type = VAR_STRING;
967 tv.vval.v_string = (char_u *)"popup_filter_menu";
968 callback = get_callback(&tv);
969 if (callback.cb_name != NULL)
970 set_callback(&wp->w_filter_cb, &callback);
971
972 wp->w_p_wrap = 0;
973 }
974
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200975 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200976 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200977
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200978 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
979 popup_add_timeout(wp, 3000);
980
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200981 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200982
983 wp->w_vsep_width = 0;
984
985 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200986 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +0200987
988 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200989}
990
991/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200992 * popup_clear()
993 */
994 void
995f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
996{
997 close_all_popups();
998}
999
1000/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001001 * popup_create({text}, {options})
1002 */
1003 void
1004f_popup_create(typval_T *argvars, typval_T *rettv)
1005{
Bram Moolenaar17627312019-06-02 19:53:44 +02001006 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001007}
1008
1009/*
1010 * popup_atcursor({text}, {options})
1011 */
1012 void
1013f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1014{
Bram Moolenaar17627312019-06-02 19:53:44 +02001015 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001016}
1017
1018/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001019 * Invoke the close callback for window "wp" with value "result".
1020 * Careful: The callback may make "wp" invalid!
1021 */
1022 static void
1023invoke_popup_callback(win_T *wp, typval_T *result)
1024{
1025 typval_T rettv;
1026 int dummy;
1027 typval_T argv[3];
1028
1029 argv[0].v_type = VAR_NUMBER;
1030 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1031
1032 if (result != NULL && result->v_type != VAR_UNKNOWN)
1033 copy_tv(result, &argv[1]);
1034 else
1035 {
1036 argv[1].v_type = VAR_NUMBER;
1037 argv[1].vval.v_number = 0;
1038 }
1039
1040 argv[2].v_type = VAR_UNKNOWN;
1041
1042 call_callback(&wp->w_close_cb, -1,
1043 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1044 if (result != NULL)
1045 clear_tv(&argv[1]);
1046 clear_tv(&rettv);
1047}
1048
1049/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001050 * Close popup "wp" and invoke any close callback for it.
1051 */
1052 static void
1053popup_close_and_callback(win_T *wp, typval_T *arg)
1054{
1055 int id = wp->w_id;
1056
1057 if (wp->w_close_cb.cb_name != NULL)
1058 // Careful: This may make "wp" invalid.
1059 invoke_popup_callback(wp, arg);
1060
1061 popup_close(id);
1062}
1063
1064/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001065 * In a filter: check if the typed key is a mouse event that is used for
1066 * dragging the popup.
1067 */
1068 static void
1069filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1070{
1071 int row = mouse_row;
1072 int col = mouse_col;
1073
1074 if (wp->w_popup_drag
1075 && is_mouse_key(c)
1076 && (wp == popup_dragwin
1077 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1078 // do not consume the key, allow for dragging the popup
1079 rettv->vval.v_number = 0;
1080}
1081
1082 static void
1083popup_highlight_curline(win_T *wp)
1084{
1085 int id;
1086 char buf[100];
1087
1088 match_delete(wp, 1, FALSE);
1089
1090 id = syn_name2id((char_u *)"PopupSelected");
1091 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1092 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1093 (char_u *)buf, 10, 1, NULL, NULL);
1094}
1095
1096/*
1097 * popup_filter_menu({text}, {options})
1098 */
1099 void
1100f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1101{
1102 int id = tv_get_number(&argvars[0]);
1103 win_T *wp = win_id2wp(id);
1104 char_u *key = tv_get_string(&argvars[1]);
1105 typval_T res;
1106 int c;
1107 linenr_T old_lnum;
1108
1109 // If the popup has been closed do not consume the key.
1110 if (wp == NULL)
1111 return;
1112
1113 c = *key;
1114 if (c == K_SPECIAL && key[1] != NUL)
1115 c = TO_SPECIAL(key[1], key[2]);
1116
1117 // consume all keys until done
1118 rettv->vval.v_number = 1;
1119 res.v_type = VAR_NUMBER;
1120
1121 old_lnum = wp->w_cursor.lnum;
1122 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1123 --wp->w_cursor.lnum;
1124 if ((c == 'j' || c == 'J' || c == K_DOWN)
1125 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1126 ++wp->w_cursor.lnum;
1127 if (old_lnum != wp->w_cursor.lnum)
1128 {
1129 popup_highlight_curline(wp);
1130 return;
1131 }
1132
1133 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1134 {
1135 // Cancelled, invoke callback with -1
1136 res.vval.v_number = -1;
1137 popup_close_and_callback(wp, &res);
1138 return;
1139 }
1140 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1141 {
1142 // Invoke callback with current index.
1143 res.vval.v_number = wp->w_cursor.lnum;
1144 popup_close_and_callback(wp, &res);
1145 return;
1146 }
1147
1148 filter_handle_drag(wp, c, rettv);
1149}
1150
1151/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001152 * popup_filter_yesno({text}, {options})
1153 */
1154 void
1155f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1156{
1157 int id = tv_get_number(&argvars[0]);
1158 win_T *wp = win_id2wp(id);
1159 char_u *key = tv_get_string(&argvars[1]);
1160 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001161 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001162
1163 // If the popup has been closed don't consume the key.
1164 if (wp == NULL)
1165 return;
1166
Bram Moolenaara730e552019-06-16 19:05:31 +02001167 c = *key;
1168 if (c == K_SPECIAL && key[1] != NUL)
1169 c = TO_SPECIAL(key[1], key[2]);
1170
Bram Moolenaara42d9452019-06-15 21:46:30 +02001171 // consume all keys until done
1172 rettv->vval.v_number = 1;
1173
Bram Moolenaara730e552019-06-16 19:05:31 +02001174 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001175 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001176 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001177 res.vval.v_number = 0;
1178 else
1179 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001180 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001181 return;
1182 }
1183
1184 // Invoke callback
1185 res.v_type = VAR_NUMBER;
1186 popup_close_and_callback(wp, &res);
1187}
1188
1189/*
1190 * popup_dialog({text}, {options})
1191 */
1192 void
1193f_popup_dialog(typval_T *argvars, typval_T *rettv)
1194{
1195 popup_create(argvars, rettv, TYPE_DIALOG);
1196}
1197
1198/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001199 * popup_menu({text}, {options})
1200 */
1201 void
1202f_popup_menu(typval_T *argvars, typval_T *rettv)
1203{
1204 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1205
1206 if (wp != NULL)
1207 popup_highlight_curline(wp);
1208}
1209
1210/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001211 * popup_notification({text}, {options})
1212 */
1213 void
1214f_popup_notification(typval_T *argvars, typval_T *rettv)
1215{
1216 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1217}
1218
1219/*
1220 * Find the popup window with window-ID "id".
1221 * If the popup window does not exist NULL is returned.
1222 * If the window is not a popup window, and error message is given.
1223 */
1224 static win_T *
1225find_popup_win(int id)
1226{
1227 win_T *wp = win_id2wp(id);
1228
1229 if (wp != NULL && !bt_popup(wp->w_buffer))
1230 {
1231 semsg(_("E993: window %d is not a popup window"), id);
1232 return NULL;
1233 }
1234 return wp;
1235}
1236
1237/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001238 * popup_close({id})
1239 */
1240 void
1241f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1242{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001243 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001244 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001245
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001246 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001247 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001248}
1249
1250/*
1251 * popup_hide({id})
1252 */
1253 void
1254f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1255{
1256 int id = (int)tv_get_number(argvars);
1257 win_T *wp = find_popup_win(id);
1258
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001259 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001260 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001261 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001262 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001263 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001264 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001265 }
1266}
1267
1268/*
1269 * popup_show({id})
1270 */
1271 void
1272f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1273{
1274 int id = (int)tv_get_number(argvars);
1275 win_T *wp = find_popup_win(id);
1276
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001277 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001278 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001279 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001280 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001281 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001282 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001283 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001284}
1285
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001286/*
1287 * popup_settext({id}, {text})
1288 */
1289 void
1290f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1291{
1292 int id = (int)tv_get_number(&argvars[0]);
1293 win_T *wp = find_popup_win(id);
1294
1295 if (wp != NULL)
1296 {
1297 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1298 popup_adjust_position(wp);
1299 }
1300}
1301
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001302 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001303popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001304{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001305 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001306 if (wp->w_winrow + wp->w_height >= cmdline_row)
1307 clear_cmdline = TRUE;
1308 win_free_popup(wp);
1309 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001310 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001311}
1312
Bram Moolenaarec583842019-05-26 14:11:23 +02001313/*
1314 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001315 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001316 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001317 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001318popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001319{
1320 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001321 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001322 win_T *prev = NULL;
1323
Bram Moolenaarec583842019-05-26 14:11:23 +02001324 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001325 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001326 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001327 {
1328 if (prev == NULL)
1329 first_popupwin = wp->w_next;
1330 else
1331 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001332 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001333 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001334 }
1335
Bram Moolenaarec583842019-05-26 14:11:23 +02001336 // go through tab-local popups
1337 FOR_ALL_TABPAGES(tp)
1338 popup_close_tabpage(tp, id);
1339}
1340
1341/*
1342 * Close a popup window with Window-id "id" in tabpage "tp".
1343 */
1344 void
1345popup_close_tabpage(tabpage_T *tp, int id)
1346{
1347 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001348 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001349 win_T *prev = NULL;
1350
Bram Moolenaarec583842019-05-26 14:11:23 +02001351 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1352 if (wp->w_id == id)
1353 {
1354 if (prev == NULL)
1355 *root = wp->w_next;
1356 else
1357 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001358 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001359 return;
1360 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001361}
1362
1363 void
1364close_all_popups(void)
1365{
1366 while (first_popupwin != NULL)
1367 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001368 while (curtab->tp_first_popupwin != NULL)
1369 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001370}
1371
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001372/*
1373 * popup_move({id}, {options})
1374 */
1375 void
1376f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1377{
1378 dict_T *d;
1379 int nr;
1380 int id = (int)tv_get_number(argvars);
1381 win_T *wp = find_popup_win(id);
1382
1383 if (wp == NULL)
1384 return; // invalid {id}
1385
1386 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1387 {
1388 emsg(_(e_dictreq));
1389 return;
1390 }
1391 d = argvars[1].vval.v_dict;
1392
1393 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1394 wp->w_minwidth = nr;
1395 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1396 wp->w_minheight = nr;
1397 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1398 wp->w_maxwidth = nr;
1399 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1400 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001401 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001402
1403 if (wp->w_winrow + wp->w_height >= cmdline_row)
1404 clear_cmdline = TRUE;
1405 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001406}
1407
Bram Moolenaarbc133542019-05-29 20:26:46 +02001408/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001409 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001410 */
1411 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001412f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001413{
1414 dict_T *dict;
1415 int id = (int)tv_get_number(argvars);
1416 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001417 int top_extra;
1418 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001419
1420 if (rettv_dict_alloc(rettv) == OK)
1421 {
1422 if (wp == NULL)
1423 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001424 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001425 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1426
Bram Moolenaarbc133542019-05-29 20:26:46 +02001427 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001428
Bram Moolenaarbc133542019-05-29 20:26:46 +02001429 dict_add_number(dict, "line", wp->w_winrow + 1);
1430 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001431 dict_add_number(dict, "width", wp->w_width + left_extra
1432 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1433 dict_add_number(dict, "height", wp->w_height + top_extra
1434 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001435
1436 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1437 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1438 dict_add_number(dict, "core_width", wp->w_width);
1439 dict_add_number(dict, "core_height", wp->w_height);
1440
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001441 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001442 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001443 }
1444}
1445
1446/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001447 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001448 */
1449 void
1450f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1451{
1452 dict_T *dict;
1453 int id = (int)tv_get_number(argvars);
1454 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001455 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001456
1457 if (rettv_dict_alloc(rettv) == OK)
1458 {
1459 if (wp == NULL)
1460 return;
1461
1462 dict = rettv->vval.v_dict;
1463 dict_add_number(dict, "line", wp->w_wantline);
1464 dict_add_number(dict, "col", wp->w_wantcol);
1465 dict_add_number(dict, "minwidth", wp->w_minwidth);
1466 dict_add_number(dict, "minheight", wp->w_minheight);
1467 dict_add_number(dict, "maxheight", wp->w_maxheight);
1468 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001469 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001470 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001471 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001472
1473 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1474 ++i)
1475 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1476 {
1477 dict_add_string(dict, "pos",
1478 (char_u *)poppos_entries[i].pp_name);
1479 break;
1480 }
1481
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001482# if defined(FEAT_TIMERS)
1483 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1484 ? (long)wp->w_popup_timer->tr_interval : 0L);
1485# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001486 }
1487}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001488
1489 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001490error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001491{
1492 if (bt_popup(curwin->w_buffer))
1493 {
1494 emsg(_("E994: Not allowed in a popup window"));
1495 return TRUE;
1496 }
1497 return FALSE;
1498}
1499
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001500/*
1501 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001502 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001503 */
1504 void
1505popup_reset_handled()
1506{
1507 win_T *wp;
1508
1509 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1510 wp->w_popup_flags &= ~POPF_HANDLED;
1511 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1512 wp->w_popup_flags &= ~POPF_HANDLED;
1513}
1514
1515/*
1516 * Find the next visible popup where POPF_HANDLED is not set.
1517 * Must have called popup_reset_handled() first.
1518 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1519 * popup with the highest zindex.
1520 */
1521 win_T *
1522find_next_popup(int lowest)
1523{
1524 win_T *wp;
1525 win_T *found_wp;
1526 int found_zindex;
1527
1528 found_zindex = lowest ? INT_MAX : 0;
1529 found_wp = NULL;
1530 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1531 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1532 && (lowest ? wp->w_zindex < found_zindex
1533 : wp->w_zindex > found_zindex))
1534 {
1535 found_zindex = wp->w_zindex;
1536 found_wp = wp;
1537 }
1538 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1539 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1540 && (lowest ? wp->w_zindex < found_zindex
1541 : wp->w_zindex > found_zindex))
1542 {
1543 found_zindex = wp->w_zindex;
1544 found_wp = wp;
1545 }
1546
1547 if (found_wp != NULL)
1548 found_wp->w_popup_flags |= POPF_HANDLED;
1549 return found_wp;
1550}
1551
1552/*
1553 * Invoke the filter callback for window "wp" with typed character "c".
1554 * Uses the global "mod_mask" for modifiers.
1555 * Returns the return value of the filter.
1556 * Careful: The filter may make "wp" invalid!
1557 */
1558 static int
1559invoke_popup_filter(win_T *wp, int c)
1560{
1561 int res;
1562 typval_T rettv;
1563 int dummy;
1564 typval_T argv[3];
1565 char_u buf[NUMBUFLEN];
1566
Bram Moolenaara42d9452019-06-15 21:46:30 +02001567 // Emergency exit: CTRL-C closes the popup.
1568 if (c == Ctrl_C)
1569 {
1570 rettv.v_type = VAR_NUMBER;
1571 rettv.vval.v_number = -1;
1572 popup_close_and_callback(wp, &rettv);
1573 return 1;
1574 }
1575
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001576 argv[0].v_type = VAR_NUMBER;
1577 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1578
1579 // Convert the number to a string, so that the function can use:
1580 // if a:c == "\<F2>"
1581 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1582 argv[1].v_type = VAR_STRING;
1583 argv[1].vval.v_string = vim_strsave(buf);
1584
1585 argv[2].v_type = VAR_UNKNOWN;
1586
Bram Moolenaara42d9452019-06-15 21:46:30 +02001587 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001588 call_callback(&wp->w_filter_cb, -1,
1589 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1590 res = tv_get_number(&rettv);
1591 vim_free(argv[1].vval.v_string);
1592 clear_tv(&rettv);
1593 return res;
1594}
1595
1596/*
1597 * Called when "c" was typed: invoke popup filter callbacks.
1598 * Returns TRUE when the character was consumed,
1599 */
1600 int
1601popup_do_filter(int c)
1602{
1603 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001604 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001605
1606 popup_reset_handled();
1607
1608 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1609 if (wp->w_filter_cb.cb_name != NULL)
1610 res = invoke_popup_filter(wp, c);
1611
1612 return res;
1613}
1614
Bram Moolenaar3397f742019-06-02 18:40:06 +02001615/*
1616 * Called when the cursor moved: check if any popup needs to be closed if the
1617 * cursor moved far enough.
1618 */
1619 void
1620popup_check_cursor_pos()
1621{
1622 win_T *wp;
1623 typval_T tv;
1624
1625 popup_reset_handled();
1626 while ((wp = find_next_popup(TRUE)) != NULL)
1627 if (wp->w_popup_curwin != NULL
1628 && (curwin != wp->w_popup_curwin
1629 || curwin->w_cursor.lnum != wp->w_popup_lnum
1630 || curwin->w_cursor.col < wp->w_popup_mincol
1631 || curwin->w_cursor.col > wp->w_popup_maxcol))
1632 {
1633 tv.v_type = VAR_NUMBER;
1634 tv.vval.v_number = -1;
1635 popup_close_and_callback(wp, &tv);
1636 }
1637}
1638
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001639/*
1640 * Update "popup_mask" if needed.
1641 * Also recomputes the popup size and positions.
1642 * Also updates "popup_visible".
1643 * Also marks window lines for redrawing.
1644 */
1645 void
1646may_update_popup_mask(int type)
1647{
1648 win_T *wp;
1649 short *mask;
1650 int line, col;
1651 int redraw_all = FALSE;
1652
1653 // Need to recompute when switching tabs.
1654 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1655 // (such as the screen size) must have changed.
1656 if (popup_mask_tab != curtab || type >= NOT_VALID)
1657 {
1658 popup_mask_refresh = TRUE;
1659 redraw_all = TRUE;
1660 }
1661 if (!popup_mask_refresh)
1662 {
1663 // Check if any buffer has changed.
1664 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1665 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1666 popup_mask_refresh = TRUE;
1667 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1668 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1669 popup_mask_refresh = TRUE;
1670 if (!popup_mask_refresh)
1671 return;
1672 }
1673
1674 // Need to update the mask, something has changed.
1675 popup_mask_refresh = FALSE;
1676 popup_mask_tab = curtab;
1677 popup_visible = FALSE;
1678
1679 // If redrawing everything, just update "popup_mask".
1680 // If redrawing only what is needed, update "popup_mask_next" and then
1681 // compare with "popup_mask" to see what changed.
1682 if (type >= SOME_VALID)
1683 mask = popup_mask;
1684 else
1685 mask = popup_mask_next;
1686 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
1687
1688 // Find the window with the lowest zindex that hasn't been handled yet,
1689 // so that the window with a higher zindex overwrites the value in
1690 // popup_mask.
1691 popup_reset_handled();
1692 while ((wp = find_next_popup(TRUE)) != NULL)
1693 {
1694 popup_visible = TRUE;
1695
1696 // Recompute the position if the text changed.
1697 if (redraw_all
1698 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1699 popup_adjust_position(wp);
1700
1701 for (line = wp->w_winrow;
1702 line < wp->w_winrow + popup_height(wp)
1703 && line < screen_Rows; ++line)
1704 for (col = wp->w_wincol;
1705 col < wp->w_wincol + popup_width(wp)
1706 && col < screen_Columns; ++col)
1707 mask[line * screen_Columns + col] = wp->w_zindex;
1708 }
1709
1710 // Only check which lines are to be updated if not already
1711 // updating all lines.
1712 if (mask == popup_mask_next)
1713 for (line = 0; line < screen_Rows; ++line)
1714 {
1715 int col_done = 0;
1716
1717 for (col = 0; col < screen_Columns; ++col)
1718 {
1719 int off = line * screen_Columns + col;
1720
1721 if (popup_mask[off] != popup_mask_next[off])
1722 {
1723 popup_mask[off] = popup_mask_next[off];
1724
1725 if (line >= cmdline_row)
1726 {
1727 // the command line needs to be cleared if text below
1728 // the popup is now visible.
1729 if (!msg_scrolled && popup_mask_next[off] == 0)
1730 clear_cmdline = TRUE;
1731 }
1732 else if (col >= col_done)
1733 {
1734 linenr_T lnum;
1735 int line_cp = line;
1736 int col_cp = col;
1737
1738 // The screen position "line" / "col" needs to be
1739 // redrawn. Figure out what window that is and update
1740 // w_redraw_top and w_redr_bot. Only needs to be done
1741 // once for each window line.
1742 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
1743 if (wp != NULL)
1744 {
1745 if (line_cp >= wp->w_height)
1746 // In (or below) status line
1747 wp->w_redr_status = TRUE;
1748 // compute the position in the buffer line from the
1749 // position on the screen
1750 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1751 &lnum))
1752 // past bottom
1753 wp->w_redr_status = TRUE;
1754 else
1755 redrawWinline(wp, lnum);
1756
1757 // This line is going to be redrawn, no need to
1758 // check until the right side of the window.
1759 col_done = wp->w_wincol + wp->w_width - 1;
1760 }
1761 }
1762 }
1763 }
1764 }
1765}
1766
1767/*
1768 * Return a string of "len" spaces in IObuff.
1769 */
1770 static char_u *
1771get_spaces(int len)
1772{
1773 vim_memset(IObuff, ' ', (size_t)len);
1774 IObuff[len] = NUL;
1775 return IObuff;
1776}
1777
1778/*
1779 * Update popup windows. They are drawn on top of normal windows.
1780 * "win_update" is called for each popup window, lowest zindex first.
1781 */
1782 void
1783update_popups(void (*win_update)(win_T *wp))
1784{
1785 win_T *wp;
1786 int top_off;
1787 int left_off;
1788 int total_width;
1789 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001790 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001791 int popup_attr;
1792 int border_attr[4];
1793 int border_char[8];
1794 char_u buf[MB_MAXBYTES];
1795 int row;
1796 int i;
1797
1798 // Find the window with the lowest zindex that hasn't been updated yet,
1799 // so that the window with a higher zindex is drawn later, thus goes on
1800 // top.
1801 popup_reset_handled();
1802 while ((wp = find_next_popup(TRUE)) != NULL)
1803 {
1804 // This drawing uses the zindex of the popup window, so that it's on
1805 // top of the text but doesn't draw when another popup with higher
1806 // zindex is on top of the character.
1807 screen_zindex = wp->w_zindex;
1808
1809 // adjust w_winrow and w_wincol for border and padding, since
1810 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001811 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001812 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1813 wp->w_winrow += top_off;
1814 wp->w_wincol += left_off;
1815
1816 // Draw the popup text.
1817 win_update(wp);
1818
1819 wp->w_winrow -= top_off;
1820 wp->w_wincol -= left_off;
1821
1822 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1823 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001824 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001825 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1826 popup_attr = get_wcr_attr(wp);
1827
1828 // We can only use these line drawing characters when 'encoding' is
1829 // "utf-8" and 'ambiwidth' is "single".
1830 if (enc_utf8 && *p_ambw == 's')
1831 {
1832 border_char[0] = border_char[2] = 0x2550;
1833 border_char[1] = border_char[3] = 0x2551;
1834 border_char[4] = 0x2554;
1835 border_char[5] = 0x2557;
1836 border_char[6] = 0x255d;
1837 border_char[7] = 0x255a;
1838 }
1839 else
1840 {
1841 border_char[0] = border_char[2] = '-';
1842 border_char[1] = border_char[3] = '|';
1843 for (i = 4; i < 8; ++i)
1844 border_char[i] = '+';
1845 }
1846 for (i = 0; i < 8; ++i)
1847 if (wp->w_border_char[i] != 0)
1848 border_char[i] = wp->w_border_char[i];
1849
1850 for (i = 0; i < 4; ++i)
1851 {
1852 border_attr[i] = popup_attr;
1853 if (wp->w_border_highlight[i] != NULL)
1854 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
1855 }
1856
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001857 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001858 if (wp->w_popup_border[0] > 0)
1859 {
1860 // top border
1861 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1862 wp->w_wincol,
1863 wp->w_wincol + total_width,
1864 wp->w_popup_border[3] != 0
1865 ? border_char[4] : border_char[0],
1866 border_char[0], border_attr[0]);
1867 if (wp->w_popup_border[1] > 0)
1868 {
1869 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1870 screen_puts(buf, wp->w_winrow,
1871 wp->w_wincol + total_width - 1, border_attr[1]);
1872 }
1873 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001874 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
1875 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001876
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001877 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001878 {
1879 // top padding
1880 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001881 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001882 wp->w_wincol + wp->w_popup_border[3],
1883 wp->w_wincol + total_width - wp->w_popup_border[1],
1884 ' ', ' ', popup_attr);
1885 }
1886
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001887 // Title goes on top of border or padding.
1888 if (wp->w_popup_title != NULL)
1889 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
1890 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
1891
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001892 for (row = wp->w_winrow + wp->w_popup_border[0];
1893 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1894 ++row)
1895 {
1896 // left border
1897 if (wp->w_popup_border[3] > 0)
1898 {
1899 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1900 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1901 }
1902 // left padding
1903 if (wp->w_popup_padding[3] > 0)
1904 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1905 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1906 // right border
1907 if (wp->w_popup_border[1] > 0)
1908 {
1909 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1910 screen_puts(buf, row,
1911 wp->w_wincol + total_width - 1, border_attr[1]);
1912 }
1913 // right padding
1914 if (wp->w_popup_padding[1] > 0)
1915 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1916 wp->w_wincol + wp->w_popup_border[3]
1917 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1918 }
1919
1920 if (wp->w_popup_padding[2] > 0)
1921 {
1922 // bottom padding
1923 row = wp->w_winrow + wp->w_popup_border[0]
1924 + wp->w_popup_padding[0] + wp->w_height;
1925 screen_fill(row, row + wp->w_popup_padding[2],
1926 wp->w_wincol + wp->w_popup_border[3],
1927 wp->w_wincol + total_width - wp->w_popup_border[1],
1928 ' ', ' ', popup_attr);
1929 }
1930
1931 if (wp->w_popup_border[2] > 0)
1932 {
1933 // bottom border
1934 row = wp->w_winrow + total_height - 1;
1935 screen_fill(row , row + 1,
1936 wp->w_wincol,
1937 wp->w_wincol + total_width,
1938 wp->w_popup_border[3] != 0
1939 ? border_char[7] : border_char[2],
1940 border_char[2], border_attr[2]);
1941 if (wp->w_popup_border[1] > 0)
1942 {
1943 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1944 screen_puts(buf, row,
1945 wp->w_wincol + total_width - 1, border_attr[2]);
1946 }
1947 }
1948
1949 // Back to the normal zindex.
1950 screen_zindex = 0;
1951 }
1952}
1953
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001954#endif // FEAT_TEXT_PROP