blob: d0f106fe61bd37f1ed0f98c6ed2874b551dd61fe [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 Moolenaar68d48f42019-06-12 22:42:41 +0200167
168#if defined(FEAT_TIMERS)
169 static void
170popup_add_timeout(win_T *wp, int time)
171{
172 char_u cbbuf[50];
173 char_u *ptr = cbbuf;
174 typval_T tv;
175
176 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
177 "{_ -> popup_close(%d)}", wp->w_id);
178 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
179 {
180 wp->w_popup_timer = create_timer(time, 0);
181 wp->w_popup_timer->tr_callback = get_callback(&tv);
182 clear_tv(&tv);
183 }
184}
185#endif
186
Bram Moolenaar17627312019-06-02 19:53:44 +0200187/*
188 * Go through the options in "dict" and apply them to buffer "buf" displayed in
189 * popup window "wp".
190 */
191 static void
192apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200193{
Bram Moolenaar402502d2019-05-30 22:07:36 +0200194 int nr;
195 char_u *str;
196 dictitem_T *di;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200197 int i;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200198
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200199 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
200 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200201 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
202 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200203
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200204 get_pos_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200205
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200206 di = dict_find(dict, (char_u *)"zindex", -1);
207 if (di != NULL)
208 {
209 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
210 if (wp->w_zindex < 1)
211 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
212 if (wp->w_zindex > 32000)
213 wp->w_zindex = 32000;
214 }
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200215
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200216#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200217 // Add timer to close the popup after some time.
218 nr = dict_get_number(dict, (char_u *)"time");
219 if (nr > 0)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200220 popup_add_timeout(wp, nr);
221#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200222
Bram Moolenaar402502d2019-05-30 22:07:36 +0200223 // Option values resulting in setting an option.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200224 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200225 if (str != NULL)
226 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
227 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200228
Bram Moolenaar402502d2019-05-30 22:07:36 +0200229 di = dict_find(dict, (char_u *)"wrap", -1);
230 if (di != NULL)
231 {
232 nr = dict_get_number(dict, (char_u *)"wrap");
233 wp->w_p_wrap = nr != 0;
234 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200235
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200236 di = dict_find(dict, (char_u *)"callback", -1);
237 if (di != NULL)
238 {
239 callback_T callback = get_callback(&di->di_tv);
240
241 if (callback.cb_name != NULL)
242 set_callback(&wp->w_close_cb, &callback);
243 }
244
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200245 di = dict_find(dict, (char_u *)"filter", -1);
246 if (di != NULL)
247 {
248 callback_T callback = get_callback(&di->di_tv);
249
250 if (callback.cb_name != NULL)
251 set_callback(&wp->w_filter_cb, &callback);
252 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200253
254 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
255 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200256
257 for (i = 0; i < 4; ++i)
258 VIM_CLEAR(wp->w_border_highlight[i]);
259 di = dict_find(dict, (char_u *)"borderhighlight", -1);
260 if (di != NULL)
261 {
262 if (di->di_tv.v_type != VAR_LIST)
263 emsg(_(e_listreq));
264 else
265 {
266 list_T *list = di->di_tv.vval.v_list;
267 listitem_T *li;
268
269 if (list != NULL)
270 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
271 ++i, li = li->li_next)
272 {
273 str = tv_get_string(&li->li_tv);
274 if (*str != NUL)
275 wp->w_border_highlight[i] = vim_strsave(str);
276 }
277 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
278 for (i = 1; i < 4; ++i)
279 wp->w_border_highlight[i] =
280 vim_strsave(wp->w_border_highlight[0]);
281 }
282 }
283
284 for (i = 0; i < 8; ++i)
285 wp->w_border_char[i] = 0;
286 di = dict_find(dict, (char_u *)"borderchars", -1);
287 if (di != NULL)
288 {
289 if (di->di_tv.v_type != VAR_LIST)
290 emsg(_(e_listreq));
291 else
292 {
293 list_T *list = di->di_tv.vval.v_list;
294 listitem_T *li;
295
296 if (list != NULL)
297 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
298 ++i, li = li->li_next)
299 {
300 str = tv_get_string(&li->li_tv);
301 if (*str != NUL)
302 wp->w_border_char[i] = mb_ptr2char(str);
303 }
304 if (list->lv_len == 1)
305 for (i = 1; i < 8; ++i)
306 wp->w_border_char[i] = wp->w_border_char[0];
307 if (list->lv_len == 2)
308 {
309 for (i = 4; i < 8; ++i)
310 wp->w_border_char[i] = wp->w_border_char[1];
311 for (i = 1; i < 4; ++i)
312 wp->w_border_char[i] = wp->w_border_char[0];
313 }
314 }
315 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200316
317 di = dict_find(dict, (char_u *)"moved", -1);
318 if (di != NULL)
319 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200320 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200321 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
322 {
323 char_u *s = di->di_tv.vval.v_string;
324 int flags = 0;
325
326 if (STRCMP(s, "word") == 0)
327 flags = FIND_IDENT | FIND_STRING;
328 else if (STRCMP(s, "WORD") == 0)
329 flags = FIND_STRING;
330 else if (STRCMP(s, "any") != 0)
331 semsg(_(e_invarg2), s);
332 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200333 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200334 }
335 else if (di->di_tv.v_type == VAR_LIST
336 && di->di_tv.vval.v_list != NULL
337 && di->di_tv.vval.v_list->lv_len == 2)
338 {
339 list_T *l = di->di_tv.vval.v_list;
340
341 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
342 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
343 }
344 else
345 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
346 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200347
348 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200349}
350
351/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200352 * Add lines to the popup from a list of strings.
353 */
354 static void
355add_popup_strings(buf_T *buf, list_T *l)
356{
357 listitem_T *li;
358 linenr_T lnum = 0;
359 char_u *p;
360
361 for (li = l->lv_first; li != NULL; li = li->li_next)
362 if (li->li_tv.v_type == VAR_STRING)
363 {
364 p = li->li_tv.vval.v_string;
365 ml_append_buf(buf, lnum++,
366 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
367 }
368}
369
370/*
371 * Add lines to the popup from a list of dictionaries.
372 */
373 static void
374add_popup_dicts(buf_T *buf, list_T *l)
375{
376 listitem_T *li;
377 listitem_T *pli;
378 linenr_T lnum = 0;
379 char_u *p;
380 dict_T *dict;
381
382 // first add the text lines
383 for (li = l->lv_first; li != NULL; li = li->li_next)
384 {
385 if (li->li_tv.v_type != VAR_DICT)
386 {
387 emsg(_(e_dictreq));
388 return;
389 }
390 dict = li->li_tv.vval.v_dict;
391 p = dict == NULL ? NULL
392 : dict_get_string(dict, (char_u *)"text", FALSE);
393 ml_append_buf(buf, lnum++,
394 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
395 }
396
397 // add the text properties
398 lnum = 1;
399 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
400 {
401 dictitem_T *di;
402 list_T *plist;
403
404 dict = li->li_tv.vval.v_dict;
405 di = dict_find(dict, (char_u *)"props", -1);
406 if (di != NULL)
407 {
408 if (di->di_tv.v_type != VAR_LIST)
409 {
410 emsg(_(e_listreq));
411 return;
412 }
413 plist = di->di_tv.vval.v_list;
414 if (plist != NULL)
415 {
416 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
417 {
418 if (pli->li_tv.v_type != VAR_DICT)
419 {
420 emsg(_(e_dictreq));
421 return;
422 }
423 dict = pli->li_tv.vval.v_dict;
424 if (dict != NULL)
425 {
426 int col = dict_get_number(dict, (char_u *)"col");
427
428 prop_add_common( lnum, col, dict, buf, NULL);
429 }
430 }
431 }
432 }
433 }
434}
435
436/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200437 * Return the height of popup window "wp", including border and padding.
438 */
439 int
440popup_height(win_T *wp)
441{
442 return wp->w_height
443 + wp->w_popup_padding[0] + wp->w_popup_border[0]
444 + wp->w_popup_padding[2] + wp->w_popup_border[2];
445}
446
447/*
448 * Return the width of popup window "wp", including border and padding.
449 */
450 int
451popup_width(win_T *wp)
452{
453 return wp->w_width
454 + wp->w_popup_padding[3] + wp->w_popup_border[3]
455 + wp->w_popup_padding[1] + wp->w_popup_border[1];
456}
457
458/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200459 * Adjust the position and size of the popup to fit on the screen.
460 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200461 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200462popup_adjust_position(win_T *wp)
463{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200464 linenr_T lnum;
465 int wrapped = 0;
466 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200467 int center_vert = FALSE;
468 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200469 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaar399d8982019-06-02 15:34:29 +0200470 int top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
471 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
472 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
473 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
474 int extra_height = top_extra + bot_extra;
475 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200476 int org_winrow = wp->w_winrow;
477 int org_wincol = wp->w_wincol;
478 int org_width = wp->w_width;
479 int org_height = wp->w_height;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200480
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200481 wp->w_winrow = 0;
482 wp->w_wincol = 0;
483 if (wp->w_popup_pos == POPPOS_CENTER)
484 {
485 // center after computing the size
486 center_vert = TRUE;
487 center_hor = TRUE;
488 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200489 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200490 {
491 if (wp->w_wantline == 0)
492 center_vert = TRUE;
493 else if (wp->w_popup_pos == POPPOS_TOPLEFT
494 || wp->w_popup_pos == POPPOS_TOPRIGHT)
495 {
496 wp->w_winrow = wp->w_wantline - 1;
497 if (wp->w_winrow >= Rows)
498 wp->w_winrow = Rows - 1;
499 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200500
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200501 if (wp->w_wantcol == 0)
502 center_hor = TRUE;
503 else if (wp->w_popup_pos == POPPOS_TOPLEFT
504 || wp->w_popup_pos == POPPOS_BOTLEFT)
505 {
506 wp->w_wincol = wp->w_wantcol - 1;
507 if (wp->w_wincol >= Columns - 3)
508 wp->w_wincol = Columns - 3;
509 }
510 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200511
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200512 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200513 // When left aligned use the space available, but shift to the left when we
514 // hit the right of the screen.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200515 maxwidth = Columns - wp->w_wincol;
516 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200517 {
518 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200519 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200520 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200521
522 // Compute width based on longest text line and the 'wrap' option.
523 // TODO: more accurate wrapping
524 wp->w_width = 0;
525 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
526 {
527 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
528
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200529 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200530 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200531 while (len > maxwidth)
532 {
533 ++wrapped;
534 len -= maxwidth;
535 wp->w_width = maxwidth;
536 }
537 }
538 else if (len > maxwidth
539 && allow_adjust_left
540 && (wp->w_popup_pos == POPPOS_TOPLEFT
541 || wp->w_popup_pos == POPPOS_BOTLEFT))
542 {
543 // adjust leftwise to fit text on screen
544 int shift_by = ( len - maxwidth );
545
546 if ( shift_by > wp->w_wincol )
547 {
548 int truncate_shift = shift_by - wp->w_wincol;
549 len -= truncate_shift;
550 shift_by -= truncate_shift;
551 }
552
553 wp->w_wincol -= shift_by;
554 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200555 wp->w_width = maxwidth;
556 }
557 if (wp->w_width < len)
558 wp->w_width = len;
559 }
560
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200561 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
562 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200563 if (wp->w_width > maxwidth)
564 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200565 if (center_hor)
566 wp->w_wincol = (Columns - wp->w_width) / 2;
567 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
568 || wp->w_popup_pos == POPPOS_TOPRIGHT)
569 {
570 // Right aligned: move to the right if needed.
571 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200572 if (wp->w_width + extra_width < wp->w_wantcol)
573 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200574 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200575
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200576 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200577 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
578 wp->w_height = wp->w_minheight;
579 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
580 wp->w_height = wp->w_maxheight;
581 if (wp->w_height > Rows - wp->w_winrow)
582 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200583
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200584 if (center_vert)
585 wp->w_winrow = (Rows - wp->w_height) / 2;
586 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
587 || wp->w_popup_pos == POPPOS_BOTLEFT)
588 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200589 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200590 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200591 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200592 else
593 // not enough space, make top aligned
594 wp->w_winrow = wp->w_wantline + 1;
595 }
596
Bram Moolenaar17146962019-05-30 00:12:11 +0200597 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200598
599 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200600 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200601 if (org_winrow != wp->w_winrow
602 || org_wincol != wp->w_wincol
603 || org_width != wp->w_width
604 || org_height != wp->w_height)
605 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200606 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200607 popup_mask_refresh = TRUE;
608 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200609}
610
Bram Moolenaar17627312019-06-02 19:53:44 +0200611typedef enum
612{
613 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200614 TYPE_ATCURSOR,
615 TYPE_NOTIFICATION
Bram Moolenaar17627312019-06-02 19:53:44 +0200616} create_type_T;
617
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200618/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200619 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200620 * popup_atcursor({text}, {options})
Bram Moolenaar33796b32019-06-08 16:01:13 +0200621 * When called from f_popup_atcursor() "type" is TYPE_ATCURSOR.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200622 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200623 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200624popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200625{
626 win_T *wp;
627 buf_T *buf;
628 dict_T *d;
629 int nr;
630
631 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200632 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
633 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200634 {
635 emsg(_(e_listreq));
636 return;
637 }
638 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
639 {
640 emsg(_(e_dictreq));
641 return;
642 }
643 d = argvars[1].vval.v_dict;
644
645 // Create the window and buffer.
646 wp = win_alloc_popup_win();
647 if (wp == NULL)
648 return;
649 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200650 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200651
652 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
653 if (buf == NULL)
654 return;
655 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200656
657 win_init_popup_win(wp, buf);
658
659 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200660 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200661 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200662 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200663 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200664 buf->b_p_ul = -1; // no undo
665 buf->b_p_swf = FALSE; // no swap file
666 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200667 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200668 wp->w_p_wrap = TRUE; // 'wrap' is default on
669
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200670 // Avoid that 'buftype' is reset when this buffer is entered.
671 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200672
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200673 if (dict_find(d, (char_u *)"tab", -1) != NULL)
674 nr = (int)dict_get_number(d, (char_u *)"tab");
675 else if (type == TYPE_NOTIFICATION)
676 nr = -1; // notifications are global by default
677 else
678 nr = 0;
679
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200680 if (nr == 0)
681 {
682 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200683 wp->w_next = curtab->tp_first_popupwin;
684 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200685 }
686 else if (nr < 0)
687 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200688 win_T *prev = first_popupwin;
689
690 // Global popup: add at the end, so that it gets displayed on top of
691 // older ones with the same zindex. Matters for notifications.
692 if (first_popupwin == NULL)
693 first_popupwin = wp;
694 else
695 {
696 while (prev->w_next != NULL)
697 prev = prev->w_next;
698 prev->w_next = wp;
699 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200700 }
701 else
702 // TODO: find tab page "nr"
703 emsg("Not implemented yet");
704
705 // Add text to the buffer.
706 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200707 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200708 // just a string
709 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200710 }
711 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200712 {
713 list_T *l = argvars[0].vval.v_list;
714
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200715 if (l->lv_len > 0)
716 {
717 if (l->lv_first->li_tv.v_type == VAR_STRING)
718 // list of strings
719 add_popup_strings(buf, l);
720 else
721 // list of dictionaries
722 add_popup_dicts(buf, l);
723 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200724 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200725
726 // Delete the line of the empty buffer.
727 curbuf = buf;
728 ml_delete(buf->b_ml.ml_line_count, FALSE);
729 curbuf = curwin->w_buffer;
730
Bram Moolenaar17627312019-06-02 19:53:44 +0200731 if (type == TYPE_ATCURSOR)
732 {
733 wp->w_popup_pos = POPPOS_BOTLEFT;
734 setcursor_mayforce(TRUE);
735 wp->w_wantline = screen_screenrow();
736 if (wp->w_wantline == 0) // cursor in first line
737 {
738 wp->w_wantline = 2;
739 wp->w_popup_pos = POPPOS_TOPLEFT;
740 }
741 wp->w_wantcol = screen_screencol() + 1;
742 set_moved_values(wp);
743 set_moved_columns(wp, FIND_STRING);
744 }
745
Bram Moolenaar33796b32019-06-08 16:01:13 +0200746 // set default values
747 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
748
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200749 if (type == TYPE_NOTIFICATION)
750 {
751 win_T *twp, *nextwin;
752 int height = buf->b_ml.ml_line_count + 3;
753 int i;
754
755 // Try to not overlap with another global popup. Guess we need 3
756 // more screen lines than buffer lines.
757 wp->w_wantline = 1;
758 for (twp = first_popupwin; twp != NULL; twp = nextwin)
759 {
760 nextwin = twp->w_next;
761 if (twp != wp
762 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
763 && twp->w_winrow <= wp->w_wantline - 1 + height
764 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
765 {
766 // move to below this popup and restart the loop to check for
767 // overlap with other popups
768 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
769 nextwin = first_popupwin;
770 }
771 }
772 if (wp->w_wantline + height > Rows)
773 {
774 // can't avoid overlap, put on top in the hope that message goes
775 // away soon.
776 wp->w_wantline = 1;
777 }
778
779 wp->w_wantcol = 10;
780 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
781 for (i = 0; i < 4; ++i)
782 wp->w_popup_border[i] = 1;
783 wp->w_popup_padding[1] = 1;
784 wp->w_popup_padding[3] = 1;
785 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
786 (char_u *)"WarningMsg", OPT_FREE|OPT_LOCAL, 0);
787 }
788
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200789 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200790 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200791
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200792 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
793 popup_add_timeout(wp, 3000);
794
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200795 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200796
797 wp->w_vsep_width = 0;
798
799 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200800 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200801}
802
803/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200804 * popup_clear()
805 */
806 void
807f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
808{
809 close_all_popups();
810}
811
812/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200813 * popup_create({text}, {options})
814 */
815 void
816f_popup_create(typval_T *argvars, typval_T *rettv)
817{
Bram Moolenaar17627312019-06-02 19:53:44 +0200818 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200819}
820
821/*
822 * popup_atcursor({text}, {options})
823 */
824 void
825f_popup_atcursor(typval_T *argvars, typval_T *rettv)
826{
Bram Moolenaar17627312019-06-02 19:53:44 +0200827 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200828}
829
830/*
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200831 * popup_notification({text}, {options})
832 */
833 void
834f_popup_notification(typval_T *argvars, typval_T *rettv)
835{
836 popup_create(argvars, rettv, TYPE_NOTIFICATION);
837}
838
839/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200840 * Find the popup window with window-ID "id".
841 * If the popup window does not exist NULL is returned.
842 * If the window is not a popup window, and error message is given.
843 */
844 static win_T *
845find_popup_win(int id)
846{
847 win_T *wp = win_id2wp(id);
848
849 if (wp != NULL && !bt_popup(wp->w_buffer))
850 {
851 semsg(_("E993: window %d is not a popup window"), id);
852 return NULL;
853 }
854 return wp;
855}
856
857/*
858 * Return TRUE if there any popups that are not hidden.
859 */
860 int
861popup_any_visible(void)
862{
863 win_T *wp;
864
865 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200866 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200867 return TRUE;
868 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200869 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200870 return TRUE;
871 return FALSE;
872}
873
874/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200875 * Invoke the close callback for window "wp" with value "result".
876 * Careful: The callback may make "wp" invalid!
877 */
878 static void
879invoke_popup_callback(win_T *wp, typval_T *result)
880{
881 typval_T rettv;
882 int dummy;
883 typval_T argv[3];
884
885 argv[0].v_type = VAR_NUMBER;
886 argv[0].vval.v_number = (varnumber_T)wp->w_id;
887
888 if (result != NULL && result->v_type != VAR_UNKNOWN)
889 copy_tv(result, &argv[1]);
890 else
891 {
892 argv[1].v_type = VAR_NUMBER;
893 argv[1].vval.v_number = 0;
894 }
895
896 argv[2].v_type = VAR_UNKNOWN;
897
898 call_callback(&wp->w_close_cb, -1,
899 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
900 if (result != NULL)
901 clear_tv(&argv[1]);
902 clear_tv(&rettv);
903}
904
905/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200906 * Close popup "wp" and invoke any close callback for it.
907 */
908 static void
909popup_close_and_callback(win_T *wp, typval_T *arg)
910{
911 int id = wp->w_id;
912
913 if (wp->w_close_cb.cb_name != NULL)
914 // Careful: This may make "wp" invalid.
915 invoke_popup_callback(wp, arg);
916
917 popup_close(id);
918}
919
920/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200921 * popup_close({id})
922 */
923 void
924f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
925{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200926 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200927 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200928
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200929 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +0200930 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200931}
932
933/*
934 * popup_hide({id})
935 */
936 void
937f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
938{
939 int id = (int)tv_get_number(argvars);
940 win_T *wp = find_popup_win(id);
941
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200942 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200943 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200944 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200945 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200946 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200947 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200948 }
949}
950
951/*
952 * popup_show({id})
953 */
954 void
955f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
956{
957 int id = (int)tv_get_number(argvars);
958 win_T *wp = find_popup_win(id);
959
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200960 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200961 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200962 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200963 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200964 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200965 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200966 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200967}
968
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200969 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200970popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200971{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200972 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200973 if (wp->w_winrow + wp->w_height >= cmdline_row)
974 clear_cmdline = TRUE;
975 win_free_popup(wp);
976 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200977 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200978}
979
Bram Moolenaarec583842019-05-26 14:11:23 +0200980/*
981 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200982 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +0200983 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200984 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200985popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200986{
987 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200988 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200989 win_T *prev = NULL;
990
Bram Moolenaarec583842019-05-26 14:11:23 +0200991 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200992 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200993 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200994 {
995 if (prev == NULL)
996 first_popupwin = wp->w_next;
997 else
998 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200999 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001000 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001001 }
1002
Bram Moolenaarec583842019-05-26 14:11:23 +02001003 // go through tab-local popups
1004 FOR_ALL_TABPAGES(tp)
1005 popup_close_tabpage(tp, id);
1006}
1007
1008/*
1009 * Close a popup window with Window-id "id" in tabpage "tp".
1010 */
1011 void
1012popup_close_tabpage(tabpage_T *tp, int id)
1013{
1014 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001015 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001016 win_T *prev = NULL;
1017
Bram Moolenaarec583842019-05-26 14:11:23 +02001018 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1019 if (wp->w_id == id)
1020 {
1021 if (prev == NULL)
1022 *root = wp->w_next;
1023 else
1024 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001025 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001026 return;
1027 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001028}
1029
1030 void
1031close_all_popups(void)
1032{
1033 while (first_popupwin != NULL)
1034 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001035 while (curtab->tp_first_popupwin != NULL)
1036 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001037}
1038
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001039/*
1040 * popup_move({id}, {options})
1041 */
1042 void
1043f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1044{
1045 dict_T *d;
1046 int nr;
1047 int id = (int)tv_get_number(argvars);
1048 win_T *wp = find_popup_win(id);
1049
1050 if (wp == NULL)
1051 return; // invalid {id}
1052
1053 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1054 {
1055 emsg(_(e_dictreq));
1056 return;
1057 }
1058 d = argvars[1].vval.v_dict;
1059
1060 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1061 wp->w_minwidth = nr;
1062 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1063 wp->w_minheight = nr;
1064 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1065 wp->w_maxwidth = nr;
1066 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1067 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001068 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001069
1070 if (wp->w_winrow + wp->w_height >= cmdline_row)
1071 clear_cmdline = TRUE;
1072 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001073}
1074
Bram Moolenaarbc133542019-05-29 20:26:46 +02001075/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001076 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001077 */
1078 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001079f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001080{
1081 dict_T *dict;
1082 int id = (int)tv_get_number(argvars);
1083 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001084 int top_extra;
1085 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001086
1087 if (rettv_dict_alloc(rettv) == OK)
1088 {
1089 if (wp == NULL)
1090 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001091 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1092 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1093
Bram Moolenaarbc133542019-05-29 20:26:46 +02001094 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001095
Bram Moolenaarbc133542019-05-29 20:26:46 +02001096 dict_add_number(dict, "line", wp->w_winrow + 1);
1097 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001098 dict_add_number(dict, "width", wp->w_width + left_extra
1099 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1100 dict_add_number(dict, "height", wp->w_height + top_extra
1101 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001102
1103 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1104 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1105 dict_add_number(dict, "core_width", wp->w_width);
1106 dict_add_number(dict, "core_height", wp->w_height);
1107
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001108 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001109 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001110 }
1111}
1112
1113/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001114 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001115 */
1116 void
1117f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1118{
1119 dict_T *dict;
1120 int id = (int)tv_get_number(argvars);
1121 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001122 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001123
1124 if (rettv_dict_alloc(rettv) == OK)
1125 {
1126 if (wp == NULL)
1127 return;
1128
1129 dict = rettv->vval.v_dict;
1130 dict_add_number(dict, "line", wp->w_wantline);
1131 dict_add_number(dict, "col", wp->w_wantcol);
1132 dict_add_number(dict, "minwidth", wp->w_minwidth);
1133 dict_add_number(dict, "minheight", wp->w_minheight);
1134 dict_add_number(dict, "maxheight", wp->w_maxheight);
1135 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
1136 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001137 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001138
1139 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1140 ++i)
1141 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1142 {
1143 dict_add_string(dict, "pos",
1144 (char_u *)poppos_entries[i].pp_name);
1145 break;
1146 }
1147
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001148# if defined(FEAT_TIMERS)
1149 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1150 ? (long)wp->w_popup_timer->tr_interval : 0L);
1151# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001152 }
1153}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001154
1155 int
1156not_in_popup_window()
1157{
1158 if (bt_popup(curwin->w_buffer))
1159 {
1160 emsg(_("E994: Not allowed in a popup window"));
1161 return TRUE;
1162 }
1163 return FALSE;
1164}
1165
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001166/*
1167 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
1168 * in the current tab.
1169 */
1170 void
1171popup_reset_handled()
1172{
1173 win_T *wp;
1174
1175 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1176 wp->w_popup_flags &= ~POPF_HANDLED;
1177 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1178 wp->w_popup_flags &= ~POPF_HANDLED;
1179}
1180
1181/*
1182 * Find the next visible popup where POPF_HANDLED is not set.
1183 * Must have called popup_reset_handled() first.
1184 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1185 * popup with the highest zindex.
1186 */
1187 win_T *
1188find_next_popup(int lowest)
1189{
1190 win_T *wp;
1191 win_T *found_wp;
1192 int found_zindex;
1193
1194 found_zindex = lowest ? INT_MAX : 0;
1195 found_wp = NULL;
1196 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1197 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1198 && (lowest ? wp->w_zindex < found_zindex
1199 : wp->w_zindex > found_zindex))
1200 {
1201 found_zindex = wp->w_zindex;
1202 found_wp = wp;
1203 }
1204 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1205 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1206 && (lowest ? wp->w_zindex < found_zindex
1207 : wp->w_zindex > found_zindex))
1208 {
1209 found_zindex = wp->w_zindex;
1210 found_wp = wp;
1211 }
1212
1213 if (found_wp != NULL)
1214 found_wp->w_popup_flags |= POPF_HANDLED;
1215 return found_wp;
1216}
1217
1218/*
1219 * Invoke the filter callback for window "wp" with typed character "c".
1220 * Uses the global "mod_mask" for modifiers.
1221 * Returns the return value of the filter.
1222 * Careful: The filter may make "wp" invalid!
1223 */
1224 static int
1225invoke_popup_filter(win_T *wp, int c)
1226{
1227 int res;
1228 typval_T rettv;
1229 int dummy;
1230 typval_T argv[3];
1231 char_u buf[NUMBUFLEN];
1232
1233 argv[0].v_type = VAR_NUMBER;
1234 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1235
1236 // Convert the number to a string, so that the function can use:
1237 // if a:c == "\<F2>"
1238 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1239 argv[1].v_type = VAR_STRING;
1240 argv[1].vval.v_string = vim_strsave(buf);
1241
1242 argv[2].v_type = VAR_UNKNOWN;
1243
1244 call_callback(&wp->w_filter_cb, -1,
1245 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1246 res = tv_get_number(&rettv);
1247 vim_free(argv[1].vval.v_string);
1248 clear_tv(&rettv);
1249 return res;
1250}
1251
1252/*
1253 * Called when "c" was typed: invoke popup filter callbacks.
1254 * Returns TRUE when the character was consumed,
1255 */
1256 int
1257popup_do_filter(int c)
1258{
1259 int res = FALSE;
1260 win_T *wp;
1261
1262 popup_reset_handled();
1263
1264 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1265 if (wp->w_filter_cb.cb_name != NULL)
1266 res = invoke_popup_filter(wp, c);
1267
1268 return res;
1269}
1270
Bram Moolenaar3397f742019-06-02 18:40:06 +02001271/*
1272 * Called when the cursor moved: check if any popup needs to be closed if the
1273 * cursor moved far enough.
1274 */
1275 void
1276popup_check_cursor_pos()
1277{
1278 win_T *wp;
1279 typval_T tv;
1280
1281 popup_reset_handled();
1282 while ((wp = find_next_popup(TRUE)) != NULL)
1283 if (wp->w_popup_curwin != NULL
1284 && (curwin != wp->w_popup_curwin
1285 || curwin->w_cursor.lnum != wp->w_popup_lnum
1286 || curwin->w_cursor.col < wp->w_popup_mincol
1287 || curwin->w_cursor.col > wp->w_popup_maxcol))
1288 {
1289 tv.v_type = VAR_NUMBER;
1290 tv.vval.v_number = -1;
1291 popup_close_and_callback(wp, &tv);
1292 }
1293}
1294
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001295#endif // FEAT_TEXT_PROP