blob: ccf1f6685c6a4fbf8916f1824aba9aec7bff3541 [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 Moolenaar8d241042019-06-12 23:40:01 +0200297 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
298 if (wp->w_firstline < 1)
299 wp->w_firstline = 1;
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 Moolenaar9eaac892019-06-01 22:49:29 +0200312 di = dict_find(dict, (char_u *)"callback", -1);
313 if (di != NULL)
314 {
315 callback_T callback = get_callback(&di->di_tv);
316
317 if (callback.cb_name != NULL)
318 set_callback(&wp->w_close_cb, &callback);
319 }
320
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200321 di = dict_find(dict, (char_u *)"filter", -1);
322 if (di != NULL)
323 {
324 callback_T callback = get_callback(&di->di_tv);
325
326 if (callback.cb_name != NULL)
327 set_callback(&wp->w_filter_cb, &callback);
328 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200329
330 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
331 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200332
333 for (i = 0; i < 4; ++i)
334 VIM_CLEAR(wp->w_border_highlight[i]);
335 di = dict_find(dict, (char_u *)"borderhighlight", -1);
336 if (di != NULL)
337 {
338 if (di->di_tv.v_type != VAR_LIST)
339 emsg(_(e_listreq));
340 else
341 {
342 list_T *list = di->di_tv.vval.v_list;
343 listitem_T *li;
344
345 if (list != NULL)
346 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
347 ++i, li = li->li_next)
348 {
349 str = tv_get_string(&li->li_tv);
350 if (*str != NUL)
351 wp->w_border_highlight[i] = vim_strsave(str);
352 }
353 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
354 for (i = 1; i < 4; ++i)
355 wp->w_border_highlight[i] =
356 vim_strsave(wp->w_border_highlight[0]);
357 }
358 }
359
360 for (i = 0; i < 8; ++i)
361 wp->w_border_char[i] = 0;
362 di = dict_find(dict, (char_u *)"borderchars", -1);
363 if (di != NULL)
364 {
365 if (di->di_tv.v_type != VAR_LIST)
366 emsg(_(e_listreq));
367 else
368 {
369 list_T *list = di->di_tv.vval.v_list;
370 listitem_T *li;
371
372 if (list != NULL)
373 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
374 ++i, li = li->li_next)
375 {
376 str = tv_get_string(&li->li_tv);
377 if (*str != NUL)
378 wp->w_border_char[i] = mb_ptr2char(str);
379 }
380 if (list->lv_len == 1)
381 for (i = 1; i < 8; ++i)
382 wp->w_border_char[i] = wp->w_border_char[0];
383 if (list->lv_len == 2)
384 {
385 for (i = 4; i < 8; ++i)
386 wp->w_border_char[i] = wp->w_border_char[1];
387 for (i = 1; i < 4; ++i)
388 wp->w_border_char[i] = wp->w_border_char[0];
389 }
390 }
391 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200392
393 di = dict_find(dict, (char_u *)"moved", -1);
394 if (di != NULL)
395 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200396 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200397 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
398 {
399 char_u *s = di->di_tv.vval.v_string;
400 int flags = 0;
401
402 if (STRCMP(s, "word") == 0)
403 flags = FIND_IDENT | FIND_STRING;
404 else if (STRCMP(s, "WORD") == 0)
405 flags = FIND_STRING;
406 else if (STRCMP(s, "any") != 0)
407 semsg(_(e_invarg2), s);
408 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200409 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200410 }
411 else if (di->di_tv.v_type == VAR_LIST
412 && di->di_tv.vval.v_list != NULL
413 && di->di_tv.vval.v_list->lv_len == 2)
414 {
415 list_T *l = di->di_tv.vval.v_list;
416
417 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
418 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
419 }
420 else
421 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
422 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200423
424 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200425}
426
427/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200428 * Add lines to the popup from a list of strings.
429 */
430 static void
431add_popup_strings(buf_T *buf, list_T *l)
432{
433 listitem_T *li;
434 linenr_T lnum = 0;
435 char_u *p;
436
437 for (li = l->lv_first; li != NULL; li = li->li_next)
438 if (li->li_tv.v_type == VAR_STRING)
439 {
440 p = li->li_tv.vval.v_string;
441 ml_append_buf(buf, lnum++,
442 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
443 }
444}
445
446/*
447 * Add lines to the popup from a list of dictionaries.
448 */
449 static void
450add_popup_dicts(buf_T *buf, list_T *l)
451{
452 listitem_T *li;
453 listitem_T *pli;
454 linenr_T lnum = 0;
455 char_u *p;
456 dict_T *dict;
457
458 // first add the text lines
459 for (li = l->lv_first; li != NULL; li = li->li_next)
460 {
461 if (li->li_tv.v_type != VAR_DICT)
462 {
463 emsg(_(e_dictreq));
464 return;
465 }
466 dict = li->li_tv.vval.v_dict;
467 p = dict == NULL ? NULL
468 : dict_get_string(dict, (char_u *)"text", FALSE);
469 ml_append_buf(buf, lnum++,
470 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
471 }
472
473 // add the text properties
474 lnum = 1;
475 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
476 {
477 dictitem_T *di;
478 list_T *plist;
479
480 dict = li->li_tv.vval.v_dict;
481 di = dict_find(dict, (char_u *)"props", -1);
482 if (di != NULL)
483 {
484 if (di->di_tv.v_type != VAR_LIST)
485 {
486 emsg(_(e_listreq));
487 return;
488 }
489 plist = di->di_tv.vval.v_list;
490 if (plist != NULL)
491 {
492 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
493 {
494 if (pli->li_tv.v_type != VAR_DICT)
495 {
496 emsg(_(e_dictreq));
497 return;
498 }
499 dict = pli->li_tv.vval.v_dict;
500 if (dict != NULL)
501 {
502 int col = dict_get_number(dict, (char_u *)"col");
503
504 prop_add_common( lnum, col, dict, buf, NULL);
505 }
506 }
507 }
508 }
509 }
510}
511
512/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200513 * Return the height of popup window "wp", including border and padding.
514 */
515 int
516popup_height(win_T *wp)
517{
518 return wp->w_height
519 + wp->w_popup_padding[0] + wp->w_popup_border[0]
520 + wp->w_popup_padding[2] + wp->w_popup_border[2];
521}
522
523/*
524 * Return the width of popup window "wp", including border and padding.
525 */
526 int
527popup_width(win_T *wp)
528{
529 return wp->w_width
530 + wp->w_popup_padding[3] + wp->w_popup_border[3]
531 + wp->w_popup_padding[1] + wp->w_popup_border[1];
532}
533
534/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200535 * Adjust the position and size of the popup to fit on the screen.
536 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200537 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200538popup_adjust_position(win_T *wp)
539{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200540 linenr_T lnum;
541 int wrapped = 0;
542 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200543 int center_vert = FALSE;
544 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200545 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaar399d8982019-06-02 15:34:29 +0200546 int top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
547 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
548 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
549 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
550 int extra_height = top_extra + bot_extra;
551 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200552 int org_winrow = wp->w_winrow;
553 int org_wincol = wp->w_wincol;
554 int org_width = wp->w_width;
555 int org_height = wp->w_height;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200556
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200557 wp->w_winrow = 0;
558 wp->w_wincol = 0;
559 if (wp->w_popup_pos == POPPOS_CENTER)
560 {
561 // center after computing the size
562 center_vert = TRUE;
563 center_hor = TRUE;
564 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200565 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200566 {
567 if (wp->w_wantline == 0)
568 center_vert = TRUE;
569 else if (wp->w_popup_pos == POPPOS_TOPLEFT
570 || wp->w_popup_pos == POPPOS_TOPRIGHT)
571 {
572 wp->w_winrow = wp->w_wantline - 1;
573 if (wp->w_winrow >= Rows)
574 wp->w_winrow = Rows - 1;
575 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200576
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200577 if (wp->w_wantcol == 0)
578 center_hor = TRUE;
579 else if (wp->w_popup_pos == POPPOS_TOPLEFT
580 || wp->w_popup_pos == POPPOS_BOTLEFT)
581 {
582 wp->w_wincol = wp->w_wantcol - 1;
583 if (wp->w_wincol >= Columns - 3)
584 wp->w_wincol = Columns - 3;
585 }
586 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200587
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200588 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200589 // When left aligned use the space available, but shift to the left when we
590 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200591 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200592 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200593 {
594 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200595 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200596 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200597
Bram Moolenaar8d241042019-06-12 23:40:01 +0200598 // start at the desired first line
599 wp->w_topline = wp->w_firstline;
600 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
601 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
602
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200603 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200604 // Use a minimum width of one, so that something shows when there is no
605 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200606 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200607 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200608 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200609 {
610 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
611
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200612 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200613 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200614 while (len > maxwidth)
615 {
616 ++wrapped;
617 len -= maxwidth;
618 wp->w_width = maxwidth;
619 }
620 }
621 else if (len > maxwidth
622 && allow_adjust_left
623 && (wp->w_popup_pos == POPPOS_TOPLEFT
624 || wp->w_popup_pos == POPPOS_BOTLEFT))
625 {
626 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200627 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200628
Bram Moolenaar51c31312019-06-15 22:27:23 +0200629 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200630 {
631 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200632
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200633 len -= truncate_shift;
634 shift_by -= truncate_shift;
635 }
636
637 wp->w_wincol -= shift_by;
638 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200639 wp->w_width = maxwidth;
640 }
641 if (wp->w_width < len)
642 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200643 // do not use the width of lines we're not going to show
644 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
645 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
646 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200647 }
648
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200649 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
650 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200651 if (wp->w_width > maxwidth)
652 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200653 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200654 {
655 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
656 if (wp->w_wincol < 0)
657 wp->w_wincol = 0;
658 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200659 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
660 || wp->w_popup_pos == POPPOS_TOPRIGHT)
661 {
662 // Right aligned: move to the right if needed.
663 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200664 if (wp->w_width + extra_width < wp->w_wantcol)
665 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200666 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200667
Bram Moolenaar8d241042019-06-12 23:40:01 +0200668 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
669 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200670 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
671 wp->w_height = wp->w_minheight;
672 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
673 wp->w_height = wp->w_maxheight;
674 if (wp->w_height > Rows - wp->w_winrow)
675 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200676
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200677 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200678 {
679 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
680 if (wp->w_winrow < 0)
681 wp->w_winrow = 0;
682 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200683 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
684 || wp->w_popup_pos == POPPOS_BOTLEFT)
685 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200686 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200687 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200688 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200689 else
690 // not enough space, make top aligned
691 wp->w_winrow = wp->w_wantline + 1;
692 }
693
Bram Moolenaar17146962019-05-30 00:12:11 +0200694 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200695
696 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200697 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200698 if (org_winrow != wp->w_winrow
699 || org_wincol != wp->w_wincol
700 || org_width != wp->w_width
701 || org_height != wp->w_height)
702 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200703 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200704 popup_mask_refresh = TRUE;
705 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200706}
707
Bram Moolenaar17627312019-06-02 19:53:44 +0200708typedef enum
709{
710 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200711 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200712 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200713 TYPE_DIALOG,
714 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200715} create_type_T;
716
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200717/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200718 * Make "buf" empty and set the contents to "text".
719 * Used by popup_create() and popup_settext().
720 */
721 static void
722popup_set_buffer_text(buf_T *buf, typval_T text)
723{
724 int lnum;
725
726 // Clear the buffer, then replace the lines.
727 curbuf = buf;
728 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
729 ml_delete(lnum, FALSE);
730 curbuf = curwin->w_buffer;
731
732 // Add text to the buffer.
733 if (text.v_type == VAR_STRING)
734 {
735 // just a string
736 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
737 }
738 else
739 {
740 list_T *l = text.vval.v_list;
741
742 if (l->lv_len > 0)
743 {
744 if (l->lv_first->li_tv.v_type == VAR_STRING)
745 // list of strings
746 add_popup_strings(buf, l);
747 else
748 // list of dictionaries
749 add_popup_dicts(buf, l);
750 }
751 }
752
753 // delete the line that was in the empty buffer
754 curbuf = buf;
755 ml_delete(buf->b_ml.ml_line_count, FALSE);
756 curbuf = curwin->w_buffer;
757}
758
759/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200760 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200761 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200762 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200763 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200764popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200765{
766 win_T *wp;
767 buf_T *buf;
768 dict_T *d;
769 int nr;
770
771 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200772 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
773 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200774 {
775 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200776 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200777 }
778 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
779 {
780 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200781 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200782 }
783 d = argvars[1].vval.v_dict;
784
785 // Create the window and buffer.
786 wp = win_alloc_popup_win();
787 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200788 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200789 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200790 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200791
792 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
793 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200794 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200795 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200796
797 win_init_popup_win(wp, buf);
798
799 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200800 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200801 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200802 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200803 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200804 buf->b_p_ul = -1; // no undo
805 buf->b_p_swf = FALSE; // no swap file
806 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200807 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200808 wp->w_p_wrap = TRUE; // 'wrap' is default on
809
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200810 // Avoid that 'buftype' is reset when this buffer is entered.
811 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200812
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200813 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
814 nr = (int)dict_get_number(d, (char_u *)"tabpage");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200815 else if (type == TYPE_NOTIFICATION)
816 nr = -1; // notifications are global by default
817 else
818 nr = 0;
819
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200820 if (nr == 0)
821 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200822 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200823 wp->w_next = curtab->tp_first_popupwin;
824 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200825 }
826 else if (nr < 0)
827 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200828 win_T *prev = first_popupwin;
829
830 // Global popup: add at the end, so that it gets displayed on top of
831 // older ones with the same zindex. Matters for notifications.
832 if (first_popupwin == NULL)
833 first_popupwin = wp;
834 else
835 {
836 while (prev->w_next != NULL)
837 prev = prev->w_next;
838 prev->w_next = wp;
839 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200840 }
841 else
842 // TODO: find tab page "nr"
843 emsg("Not implemented yet");
844
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200845 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200846
Bram Moolenaar17627312019-06-02 19:53:44 +0200847 if (type == TYPE_ATCURSOR)
848 {
849 wp->w_popup_pos = POPPOS_BOTLEFT;
850 setcursor_mayforce(TRUE);
851 wp->w_wantline = screen_screenrow();
852 if (wp->w_wantline == 0) // cursor in first line
853 {
854 wp->w_wantline = 2;
855 wp->w_popup_pos = POPPOS_TOPLEFT;
856 }
857 wp->w_wantcol = screen_screencol() + 1;
858 set_moved_values(wp);
859 set_moved_columns(wp, FIND_STRING);
860 }
861
Bram Moolenaar33796b32019-06-08 16:01:13 +0200862 // set default values
863 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
864
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200865 if (type == TYPE_NOTIFICATION)
866 {
867 win_T *twp, *nextwin;
868 int height = buf->b_ml.ml_line_count + 3;
869 int i;
870
871 // Try to not overlap with another global popup. Guess we need 3
872 // more screen lines than buffer lines.
873 wp->w_wantline = 1;
874 for (twp = first_popupwin; twp != NULL; twp = nextwin)
875 {
876 nextwin = twp->w_next;
877 if (twp != wp
878 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
879 && twp->w_winrow <= wp->w_wantline - 1 + height
880 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
881 {
882 // move to below this popup and restart the loop to check for
883 // overlap with other popups
884 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
885 nextwin = first_popupwin;
886 }
887 }
888 if (wp->w_wantline + height > Rows)
889 {
890 // can't avoid overlap, put on top in the hope that message goes
891 // away soon.
892 wp->w_wantline = 1;
893 }
894
895 wp->w_wantcol = 10;
896 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200897 wp->w_minwidth = 20;
898 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200899 for (i = 0; i < 4; ++i)
900 wp->w_popup_border[i] = 1;
901 wp->w_popup_padding[1] = 1;
902 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200903
904 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200905 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200906 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
907 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200908 }
909
Bram Moolenaara730e552019-06-16 19:05:31 +0200910 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +0200911 {
912 int i;
913
914 wp->w_popup_pos = POPPOS_CENTER;
915 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
916 wp->w_popup_drag = 1;
917 for (i = 0; i < 4; ++i)
918 {
919 wp->w_popup_border[i] = 1;
920 wp->w_popup_padding[i] = 1;
921 }
922 }
923
Bram Moolenaara730e552019-06-16 19:05:31 +0200924 if (type == TYPE_MENU)
925 {
926 typval_T tv;
927 callback_T callback;
928
929 tv.v_type = VAR_STRING;
930 tv.vval.v_string = (char_u *)"popup_filter_menu";
931 callback = get_callback(&tv);
932 if (callback.cb_name != NULL)
933 set_callback(&wp->w_filter_cb, &callback);
934
935 wp->w_p_wrap = 0;
936 }
937
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200938 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200939 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200940
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200941 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
942 popup_add_timeout(wp, 3000);
943
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200944 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200945
946 wp->w_vsep_width = 0;
947
948 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200949 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +0200950
951 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200952}
953
954/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200955 * popup_clear()
956 */
957 void
958f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
959{
960 close_all_popups();
961}
962
963/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200964 * popup_create({text}, {options})
965 */
966 void
967f_popup_create(typval_T *argvars, typval_T *rettv)
968{
Bram Moolenaar17627312019-06-02 19:53:44 +0200969 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200970}
971
972/*
973 * popup_atcursor({text}, {options})
974 */
975 void
976f_popup_atcursor(typval_T *argvars, typval_T *rettv)
977{
Bram Moolenaar17627312019-06-02 19:53:44 +0200978 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200979}
980
981/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200982 * Invoke the close callback for window "wp" with value "result".
983 * Careful: The callback may make "wp" invalid!
984 */
985 static void
986invoke_popup_callback(win_T *wp, typval_T *result)
987{
988 typval_T rettv;
989 int dummy;
990 typval_T argv[3];
991
992 argv[0].v_type = VAR_NUMBER;
993 argv[0].vval.v_number = (varnumber_T)wp->w_id;
994
995 if (result != NULL && result->v_type != VAR_UNKNOWN)
996 copy_tv(result, &argv[1]);
997 else
998 {
999 argv[1].v_type = VAR_NUMBER;
1000 argv[1].vval.v_number = 0;
1001 }
1002
1003 argv[2].v_type = VAR_UNKNOWN;
1004
1005 call_callback(&wp->w_close_cb, -1,
1006 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1007 if (result != NULL)
1008 clear_tv(&argv[1]);
1009 clear_tv(&rettv);
1010}
1011
1012/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001013 * Close popup "wp" and invoke any close callback for it.
1014 */
1015 static void
1016popup_close_and_callback(win_T *wp, typval_T *arg)
1017{
1018 int id = wp->w_id;
1019
1020 if (wp->w_close_cb.cb_name != NULL)
1021 // Careful: This may make "wp" invalid.
1022 invoke_popup_callback(wp, arg);
1023
1024 popup_close(id);
1025}
1026
1027/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001028 * In a filter: check if the typed key is a mouse event that is used for
1029 * dragging the popup.
1030 */
1031 static void
1032filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1033{
1034 int row = mouse_row;
1035 int col = mouse_col;
1036
1037 if (wp->w_popup_drag
1038 && is_mouse_key(c)
1039 && (wp == popup_dragwin
1040 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1041 // do not consume the key, allow for dragging the popup
1042 rettv->vval.v_number = 0;
1043}
1044
1045 static void
1046popup_highlight_curline(win_T *wp)
1047{
1048 int id;
1049 char buf[100];
1050
1051 match_delete(wp, 1, FALSE);
1052
1053 id = syn_name2id((char_u *)"PopupSelected");
1054 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1055 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1056 (char_u *)buf, 10, 1, NULL, NULL);
1057}
1058
1059/*
1060 * popup_filter_menu({text}, {options})
1061 */
1062 void
1063f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1064{
1065 int id = tv_get_number(&argvars[0]);
1066 win_T *wp = win_id2wp(id);
1067 char_u *key = tv_get_string(&argvars[1]);
1068 typval_T res;
1069 int c;
1070 linenr_T old_lnum;
1071
1072 // If the popup has been closed do not consume the key.
1073 if (wp == NULL)
1074 return;
1075
1076 c = *key;
1077 if (c == K_SPECIAL && key[1] != NUL)
1078 c = TO_SPECIAL(key[1], key[2]);
1079
1080 // consume all keys until done
1081 rettv->vval.v_number = 1;
1082 res.v_type = VAR_NUMBER;
1083
1084 old_lnum = wp->w_cursor.lnum;
1085 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1086 --wp->w_cursor.lnum;
1087 if ((c == 'j' || c == 'J' || c == K_DOWN)
1088 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1089 ++wp->w_cursor.lnum;
1090 if (old_lnum != wp->w_cursor.lnum)
1091 {
1092 popup_highlight_curline(wp);
1093 return;
1094 }
1095
1096 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1097 {
1098 // Cancelled, invoke callback with -1
1099 res.vval.v_number = -1;
1100 popup_close_and_callback(wp, &res);
1101 return;
1102 }
1103 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1104 {
1105 // Invoke callback with current index.
1106 res.vval.v_number = wp->w_cursor.lnum;
1107 popup_close_and_callback(wp, &res);
1108 return;
1109 }
1110
1111 filter_handle_drag(wp, c, rettv);
1112}
1113
1114/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001115 * popup_filter_yesno({text}, {options})
1116 */
1117 void
1118f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1119{
1120 int id = tv_get_number(&argvars[0]);
1121 win_T *wp = win_id2wp(id);
1122 char_u *key = tv_get_string(&argvars[1]);
1123 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001124 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001125
1126 // If the popup has been closed don't consume the key.
1127 if (wp == NULL)
1128 return;
1129
Bram Moolenaara730e552019-06-16 19:05:31 +02001130 c = *key;
1131 if (c == K_SPECIAL && key[1] != NUL)
1132 c = TO_SPECIAL(key[1], key[2]);
1133
Bram Moolenaara42d9452019-06-15 21:46:30 +02001134 // consume all keys until done
1135 rettv->vval.v_number = 1;
1136
Bram Moolenaara730e552019-06-16 19:05:31 +02001137 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001138 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001139 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001140 res.vval.v_number = 0;
1141 else
1142 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001143 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001144 return;
1145 }
1146
1147 // Invoke callback
1148 res.v_type = VAR_NUMBER;
1149 popup_close_and_callback(wp, &res);
1150}
1151
1152/*
1153 * popup_dialog({text}, {options})
1154 */
1155 void
1156f_popup_dialog(typval_T *argvars, typval_T *rettv)
1157{
1158 popup_create(argvars, rettv, TYPE_DIALOG);
1159}
1160
1161/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001162 * popup_menu({text}, {options})
1163 */
1164 void
1165f_popup_menu(typval_T *argvars, typval_T *rettv)
1166{
1167 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1168
1169 if (wp != NULL)
1170 popup_highlight_curline(wp);
1171}
1172
1173/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001174 * popup_notification({text}, {options})
1175 */
1176 void
1177f_popup_notification(typval_T *argvars, typval_T *rettv)
1178{
1179 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1180}
1181
1182/*
1183 * Find the popup window with window-ID "id".
1184 * If the popup window does not exist NULL is returned.
1185 * If the window is not a popup window, and error message is given.
1186 */
1187 static win_T *
1188find_popup_win(int id)
1189{
1190 win_T *wp = win_id2wp(id);
1191
1192 if (wp != NULL && !bt_popup(wp->w_buffer))
1193 {
1194 semsg(_("E993: window %d is not a popup window"), id);
1195 return NULL;
1196 }
1197 return wp;
1198}
1199
1200/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001201 * popup_close({id})
1202 */
1203 void
1204f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1205{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001206 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001207 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001208
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001209 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001210 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001211}
1212
1213/*
1214 * popup_hide({id})
1215 */
1216 void
1217f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1218{
1219 int id = (int)tv_get_number(argvars);
1220 win_T *wp = find_popup_win(id);
1221
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001222 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001223 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001224 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001225 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001226 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001227 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001228 }
1229}
1230
1231/*
1232 * popup_show({id})
1233 */
1234 void
1235f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1236{
1237 int id = (int)tv_get_number(argvars);
1238 win_T *wp = find_popup_win(id);
1239
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001240 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001241 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001242 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001243 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001244 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001245 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001246 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001247}
1248
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001249/*
1250 * popup_settext({id}, {text})
1251 */
1252 void
1253f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1254{
1255 int id = (int)tv_get_number(&argvars[0]);
1256 win_T *wp = find_popup_win(id);
1257
1258 if (wp != NULL)
1259 {
1260 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1261 popup_adjust_position(wp);
1262 }
1263}
1264
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001265 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001266popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001267{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001268 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001269 if (wp->w_winrow + wp->w_height >= cmdline_row)
1270 clear_cmdline = TRUE;
1271 win_free_popup(wp);
1272 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001273 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001274}
1275
Bram Moolenaarec583842019-05-26 14:11:23 +02001276/*
1277 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001278 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001279 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001280 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001281popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001282{
1283 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001284 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001285 win_T *prev = NULL;
1286
Bram Moolenaarec583842019-05-26 14:11:23 +02001287 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001288 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001289 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001290 {
1291 if (prev == NULL)
1292 first_popupwin = wp->w_next;
1293 else
1294 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001295 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001296 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001297 }
1298
Bram Moolenaarec583842019-05-26 14:11:23 +02001299 // go through tab-local popups
1300 FOR_ALL_TABPAGES(tp)
1301 popup_close_tabpage(tp, id);
1302}
1303
1304/*
1305 * Close a popup window with Window-id "id" in tabpage "tp".
1306 */
1307 void
1308popup_close_tabpage(tabpage_T *tp, int id)
1309{
1310 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001311 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001312 win_T *prev = NULL;
1313
Bram Moolenaarec583842019-05-26 14:11:23 +02001314 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1315 if (wp->w_id == id)
1316 {
1317 if (prev == NULL)
1318 *root = wp->w_next;
1319 else
1320 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001321 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001322 return;
1323 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001324}
1325
1326 void
1327close_all_popups(void)
1328{
1329 while (first_popupwin != NULL)
1330 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001331 while (curtab->tp_first_popupwin != NULL)
1332 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001333}
1334
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001335/*
1336 * popup_move({id}, {options})
1337 */
1338 void
1339f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1340{
1341 dict_T *d;
1342 int nr;
1343 int id = (int)tv_get_number(argvars);
1344 win_T *wp = find_popup_win(id);
1345
1346 if (wp == NULL)
1347 return; // invalid {id}
1348
1349 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1350 {
1351 emsg(_(e_dictreq));
1352 return;
1353 }
1354 d = argvars[1].vval.v_dict;
1355
1356 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1357 wp->w_minwidth = nr;
1358 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1359 wp->w_minheight = nr;
1360 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1361 wp->w_maxwidth = nr;
1362 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1363 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001364 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001365
1366 if (wp->w_winrow + wp->w_height >= cmdline_row)
1367 clear_cmdline = TRUE;
1368 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001369}
1370
Bram Moolenaarbc133542019-05-29 20:26:46 +02001371/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001372 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001373 */
1374 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001375f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001376{
1377 dict_T *dict;
1378 int id = (int)tv_get_number(argvars);
1379 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001380 int top_extra;
1381 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001382
1383 if (rettv_dict_alloc(rettv) == OK)
1384 {
1385 if (wp == NULL)
1386 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001387 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1388 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1389
Bram Moolenaarbc133542019-05-29 20:26:46 +02001390 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001391
Bram Moolenaarbc133542019-05-29 20:26:46 +02001392 dict_add_number(dict, "line", wp->w_winrow + 1);
1393 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001394 dict_add_number(dict, "width", wp->w_width + left_extra
1395 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1396 dict_add_number(dict, "height", wp->w_height + top_extra
1397 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001398
1399 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1400 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1401 dict_add_number(dict, "core_width", wp->w_width);
1402 dict_add_number(dict, "core_height", wp->w_height);
1403
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001404 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001405 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001406 }
1407}
1408
1409/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001410 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001411 */
1412 void
1413f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1414{
1415 dict_T *dict;
1416 int id = (int)tv_get_number(argvars);
1417 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001418 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001419
1420 if (rettv_dict_alloc(rettv) == OK)
1421 {
1422 if (wp == NULL)
1423 return;
1424
1425 dict = rettv->vval.v_dict;
1426 dict_add_number(dict, "line", wp->w_wantline);
1427 dict_add_number(dict, "col", wp->w_wantcol);
1428 dict_add_number(dict, "minwidth", wp->w_minwidth);
1429 dict_add_number(dict, "minheight", wp->w_minheight);
1430 dict_add_number(dict, "maxheight", wp->w_maxheight);
1431 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001432 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001433 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001434 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001435
1436 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1437 ++i)
1438 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1439 {
1440 dict_add_string(dict, "pos",
1441 (char_u *)poppos_entries[i].pp_name);
1442 break;
1443 }
1444
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001445# if defined(FEAT_TIMERS)
1446 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1447 ? (long)wp->w_popup_timer->tr_interval : 0L);
1448# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001449 }
1450}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001451
1452 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001453error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001454{
1455 if (bt_popup(curwin->w_buffer))
1456 {
1457 emsg(_("E994: Not allowed in a popup window"));
1458 return TRUE;
1459 }
1460 return FALSE;
1461}
1462
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001463/*
1464 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001465 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001466 */
1467 void
1468popup_reset_handled()
1469{
1470 win_T *wp;
1471
1472 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1473 wp->w_popup_flags &= ~POPF_HANDLED;
1474 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1475 wp->w_popup_flags &= ~POPF_HANDLED;
1476}
1477
1478/*
1479 * Find the next visible popup where POPF_HANDLED is not set.
1480 * Must have called popup_reset_handled() first.
1481 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1482 * popup with the highest zindex.
1483 */
1484 win_T *
1485find_next_popup(int lowest)
1486{
1487 win_T *wp;
1488 win_T *found_wp;
1489 int found_zindex;
1490
1491 found_zindex = lowest ? INT_MAX : 0;
1492 found_wp = NULL;
1493 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1494 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1495 && (lowest ? wp->w_zindex < found_zindex
1496 : wp->w_zindex > found_zindex))
1497 {
1498 found_zindex = wp->w_zindex;
1499 found_wp = wp;
1500 }
1501 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1502 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1503 && (lowest ? wp->w_zindex < found_zindex
1504 : wp->w_zindex > found_zindex))
1505 {
1506 found_zindex = wp->w_zindex;
1507 found_wp = wp;
1508 }
1509
1510 if (found_wp != NULL)
1511 found_wp->w_popup_flags |= POPF_HANDLED;
1512 return found_wp;
1513}
1514
1515/*
1516 * Invoke the filter callback for window "wp" with typed character "c".
1517 * Uses the global "mod_mask" for modifiers.
1518 * Returns the return value of the filter.
1519 * Careful: The filter may make "wp" invalid!
1520 */
1521 static int
1522invoke_popup_filter(win_T *wp, int c)
1523{
1524 int res;
1525 typval_T rettv;
1526 int dummy;
1527 typval_T argv[3];
1528 char_u buf[NUMBUFLEN];
1529
Bram Moolenaara42d9452019-06-15 21:46:30 +02001530 // Emergency exit: CTRL-C closes the popup.
1531 if (c == Ctrl_C)
1532 {
1533 rettv.v_type = VAR_NUMBER;
1534 rettv.vval.v_number = -1;
1535 popup_close_and_callback(wp, &rettv);
1536 return 1;
1537 }
1538
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001539 argv[0].v_type = VAR_NUMBER;
1540 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1541
1542 // Convert the number to a string, so that the function can use:
1543 // if a:c == "\<F2>"
1544 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1545 argv[1].v_type = VAR_STRING;
1546 argv[1].vval.v_string = vim_strsave(buf);
1547
1548 argv[2].v_type = VAR_UNKNOWN;
1549
Bram Moolenaara42d9452019-06-15 21:46:30 +02001550 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001551 call_callback(&wp->w_filter_cb, -1,
1552 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1553 res = tv_get_number(&rettv);
1554 vim_free(argv[1].vval.v_string);
1555 clear_tv(&rettv);
1556 return res;
1557}
1558
1559/*
1560 * Called when "c" was typed: invoke popup filter callbacks.
1561 * Returns TRUE when the character was consumed,
1562 */
1563 int
1564popup_do_filter(int c)
1565{
1566 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001567 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001568
1569 popup_reset_handled();
1570
1571 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1572 if (wp->w_filter_cb.cb_name != NULL)
1573 res = invoke_popup_filter(wp, c);
1574
1575 return res;
1576}
1577
Bram Moolenaar3397f742019-06-02 18:40:06 +02001578/*
1579 * Called when the cursor moved: check if any popup needs to be closed if the
1580 * cursor moved far enough.
1581 */
1582 void
1583popup_check_cursor_pos()
1584{
1585 win_T *wp;
1586 typval_T tv;
1587
1588 popup_reset_handled();
1589 while ((wp = find_next_popup(TRUE)) != NULL)
1590 if (wp->w_popup_curwin != NULL
1591 && (curwin != wp->w_popup_curwin
1592 || curwin->w_cursor.lnum != wp->w_popup_lnum
1593 || curwin->w_cursor.col < wp->w_popup_mincol
1594 || curwin->w_cursor.col > wp->w_popup_maxcol))
1595 {
1596 tv.v_type = VAR_NUMBER;
1597 tv.vval.v_number = -1;
1598 popup_close_and_callback(wp, &tv);
1599 }
1600}
1601
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001602/*
1603 * Update "popup_mask" if needed.
1604 * Also recomputes the popup size and positions.
1605 * Also updates "popup_visible".
1606 * Also marks window lines for redrawing.
1607 */
1608 void
1609may_update_popup_mask(int type)
1610{
1611 win_T *wp;
1612 short *mask;
1613 int line, col;
1614 int redraw_all = FALSE;
1615
1616 // Need to recompute when switching tabs.
1617 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1618 // (such as the screen size) must have changed.
1619 if (popup_mask_tab != curtab || type >= NOT_VALID)
1620 {
1621 popup_mask_refresh = TRUE;
1622 redraw_all = TRUE;
1623 }
1624 if (!popup_mask_refresh)
1625 {
1626 // Check if any buffer has changed.
1627 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1628 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1629 popup_mask_refresh = TRUE;
1630 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1631 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1632 popup_mask_refresh = TRUE;
1633 if (!popup_mask_refresh)
1634 return;
1635 }
1636
1637 // Need to update the mask, something has changed.
1638 popup_mask_refresh = FALSE;
1639 popup_mask_tab = curtab;
1640 popup_visible = FALSE;
1641
1642 // If redrawing everything, just update "popup_mask".
1643 // If redrawing only what is needed, update "popup_mask_next" and then
1644 // compare with "popup_mask" to see what changed.
1645 if (type >= SOME_VALID)
1646 mask = popup_mask;
1647 else
1648 mask = popup_mask_next;
1649 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
1650
1651 // Find the window with the lowest zindex that hasn't been handled yet,
1652 // so that the window with a higher zindex overwrites the value in
1653 // popup_mask.
1654 popup_reset_handled();
1655 while ((wp = find_next_popup(TRUE)) != NULL)
1656 {
1657 popup_visible = TRUE;
1658
1659 // Recompute the position if the text changed.
1660 if (redraw_all
1661 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1662 popup_adjust_position(wp);
1663
1664 for (line = wp->w_winrow;
1665 line < wp->w_winrow + popup_height(wp)
1666 && line < screen_Rows; ++line)
1667 for (col = wp->w_wincol;
1668 col < wp->w_wincol + popup_width(wp)
1669 && col < screen_Columns; ++col)
1670 mask[line * screen_Columns + col] = wp->w_zindex;
1671 }
1672
1673 // Only check which lines are to be updated if not already
1674 // updating all lines.
1675 if (mask == popup_mask_next)
1676 for (line = 0; line < screen_Rows; ++line)
1677 {
1678 int col_done = 0;
1679
1680 for (col = 0; col < screen_Columns; ++col)
1681 {
1682 int off = line * screen_Columns + col;
1683
1684 if (popup_mask[off] != popup_mask_next[off])
1685 {
1686 popup_mask[off] = popup_mask_next[off];
1687
1688 if (line >= cmdline_row)
1689 {
1690 // the command line needs to be cleared if text below
1691 // the popup is now visible.
1692 if (!msg_scrolled && popup_mask_next[off] == 0)
1693 clear_cmdline = TRUE;
1694 }
1695 else if (col >= col_done)
1696 {
1697 linenr_T lnum;
1698 int line_cp = line;
1699 int col_cp = col;
1700
1701 // The screen position "line" / "col" needs to be
1702 // redrawn. Figure out what window that is and update
1703 // w_redraw_top and w_redr_bot. Only needs to be done
1704 // once for each window line.
1705 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
1706 if (wp != NULL)
1707 {
1708 if (line_cp >= wp->w_height)
1709 // In (or below) status line
1710 wp->w_redr_status = TRUE;
1711 // compute the position in the buffer line from the
1712 // position on the screen
1713 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1714 &lnum))
1715 // past bottom
1716 wp->w_redr_status = TRUE;
1717 else
1718 redrawWinline(wp, lnum);
1719
1720 // This line is going to be redrawn, no need to
1721 // check until the right side of the window.
1722 col_done = wp->w_wincol + wp->w_width - 1;
1723 }
1724 }
1725 }
1726 }
1727 }
1728}
1729
1730/*
1731 * Return a string of "len" spaces in IObuff.
1732 */
1733 static char_u *
1734get_spaces(int len)
1735{
1736 vim_memset(IObuff, ' ', (size_t)len);
1737 IObuff[len] = NUL;
1738 return IObuff;
1739}
1740
1741/*
1742 * Update popup windows. They are drawn on top of normal windows.
1743 * "win_update" is called for each popup window, lowest zindex first.
1744 */
1745 void
1746update_popups(void (*win_update)(win_T *wp))
1747{
1748 win_T *wp;
1749 int top_off;
1750 int left_off;
1751 int total_width;
1752 int total_height;
1753 int popup_attr;
1754 int border_attr[4];
1755 int border_char[8];
1756 char_u buf[MB_MAXBYTES];
1757 int row;
1758 int i;
1759
1760 // Find the window with the lowest zindex that hasn't been updated yet,
1761 // so that the window with a higher zindex is drawn later, thus goes on
1762 // top.
1763 popup_reset_handled();
1764 while ((wp = find_next_popup(TRUE)) != NULL)
1765 {
1766 // This drawing uses the zindex of the popup window, so that it's on
1767 // top of the text but doesn't draw when another popup with higher
1768 // zindex is on top of the character.
1769 screen_zindex = wp->w_zindex;
1770
1771 // adjust w_winrow and w_wincol for border and padding, since
1772 // win_update() doesn't handle them.
1773 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1774 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1775 wp->w_winrow += top_off;
1776 wp->w_wincol += left_off;
1777
1778 // Draw the popup text.
1779 win_update(wp);
1780
1781 wp->w_winrow -= top_off;
1782 wp->w_wincol -= left_off;
1783
1784 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1785 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1786 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1787 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1788 popup_attr = get_wcr_attr(wp);
1789
1790 // We can only use these line drawing characters when 'encoding' is
1791 // "utf-8" and 'ambiwidth' is "single".
1792 if (enc_utf8 && *p_ambw == 's')
1793 {
1794 border_char[0] = border_char[2] = 0x2550;
1795 border_char[1] = border_char[3] = 0x2551;
1796 border_char[4] = 0x2554;
1797 border_char[5] = 0x2557;
1798 border_char[6] = 0x255d;
1799 border_char[7] = 0x255a;
1800 }
1801 else
1802 {
1803 border_char[0] = border_char[2] = '-';
1804 border_char[1] = border_char[3] = '|';
1805 for (i = 4; i < 8; ++i)
1806 border_char[i] = '+';
1807 }
1808 for (i = 0; i < 8; ++i)
1809 if (wp->w_border_char[i] != 0)
1810 border_char[i] = wp->w_border_char[i];
1811
1812 for (i = 0; i < 4; ++i)
1813 {
1814 border_attr[i] = popup_attr;
1815 if (wp->w_border_highlight[i] != NULL)
1816 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
1817 }
1818
1819 if (wp->w_popup_border[0] > 0)
1820 {
1821 // top border
1822 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1823 wp->w_wincol,
1824 wp->w_wincol + total_width,
1825 wp->w_popup_border[3] != 0
1826 ? border_char[4] : border_char[0],
1827 border_char[0], border_attr[0]);
1828 if (wp->w_popup_border[1] > 0)
1829 {
1830 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1831 screen_puts(buf, wp->w_winrow,
1832 wp->w_wincol + total_width - 1, border_attr[1]);
1833 }
1834 }
1835
1836 if (wp->w_popup_padding[0] > 0)
1837 {
1838 // top padding
1839 row = wp->w_winrow + wp->w_popup_border[0];
1840 screen_fill(row, row + wp->w_popup_padding[0],
1841 wp->w_wincol + wp->w_popup_border[3],
1842 wp->w_wincol + total_width - wp->w_popup_border[1],
1843 ' ', ' ', popup_attr);
1844 }
1845
1846 for (row = wp->w_winrow + wp->w_popup_border[0];
1847 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1848 ++row)
1849 {
1850 // left border
1851 if (wp->w_popup_border[3] > 0)
1852 {
1853 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1854 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1855 }
1856 // left padding
1857 if (wp->w_popup_padding[3] > 0)
1858 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1859 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1860 // right border
1861 if (wp->w_popup_border[1] > 0)
1862 {
1863 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1864 screen_puts(buf, row,
1865 wp->w_wincol + total_width - 1, border_attr[1]);
1866 }
1867 // right padding
1868 if (wp->w_popup_padding[1] > 0)
1869 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1870 wp->w_wincol + wp->w_popup_border[3]
1871 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1872 }
1873
1874 if (wp->w_popup_padding[2] > 0)
1875 {
1876 // bottom padding
1877 row = wp->w_winrow + wp->w_popup_border[0]
1878 + wp->w_popup_padding[0] + wp->w_height;
1879 screen_fill(row, row + wp->w_popup_padding[2],
1880 wp->w_wincol + wp->w_popup_border[3],
1881 wp->w_wincol + total_width - wp->w_popup_border[1],
1882 ' ', ' ', popup_attr);
1883 }
1884
1885 if (wp->w_popup_border[2] > 0)
1886 {
1887 // bottom border
1888 row = wp->w_winrow + total_height - 1;
1889 screen_fill(row , row + 1,
1890 wp->w_wincol,
1891 wp->w_wincol + total_width,
1892 wp->w_popup_border[3] != 0
1893 ? border_char[7] : border_char[2],
1894 border_char[2], border_attr[2]);
1895 if (wp->w_popup_border[1] > 0)
1896 {
1897 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1898 screen_puts(buf, row,
1899 wp->w_wincol + total_width - 1, border_attr[2]);
1900 }
1901 }
1902
1903 // Back to the normal zindex.
1904 screen_zindex = 0;
1905 }
1906}
1907
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001908#endif // FEAT_TEXT_PROP