blob: 5e444aee038e387240c85026b2b5612cca639b00 [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 Moolenaar8d241042019-06-12 23:40:01 +0200229 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
230 if (wp->w_firstline < 1)
231 wp->w_firstline = 1;
232
Bram Moolenaar402502d2019-05-30 22:07:36 +0200233 di = dict_find(dict, (char_u *)"wrap", -1);
234 if (di != NULL)
235 {
236 nr = dict_get_number(dict, (char_u *)"wrap");
237 wp->w_p_wrap = nr != 0;
238 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200239
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200240 di = dict_find(dict, (char_u *)"callback", -1);
241 if (di != NULL)
242 {
243 callback_T callback = get_callback(&di->di_tv);
244
245 if (callback.cb_name != NULL)
246 set_callback(&wp->w_close_cb, &callback);
247 }
248
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200249 di = dict_find(dict, (char_u *)"filter", -1);
250 if (di != NULL)
251 {
252 callback_T callback = get_callback(&di->di_tv);
253
254 if (callback.cb_name != NULL)
255 set_callback(&wp->w_filter_cb, &callback);
256 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200257
258 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
259 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200260
261 for (i = 0; i < 4; ++i)
262 VIM_CLEAR(wp->w_border_highlight[i]);
263 di = dict_find(dict, (char_u *)"borderhighlight", -1);
264 if (di != NULL)
265 {
266 if (di->di_tv.v_type != VAR_LIST)
267 emsg(_(e_listreq));
268 else
269 {
270 list_T *list = di->di_tv.vval.v_list;
271 listitem_T *li;
272
273 if (list != NULL)
274 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
275 ++i, li = li->li_next)
276 {
277 str = tv_get_string(&li->li_tv);
278 if (*str != NUL)
279 wp->w_border_highlight[i] = vim_strsave(str);
280 }
281 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
282 for (i = 1; i < 4; ++i)
283 wp->w_border_highlight[i] =
284 vim_strsave(wp->w_border_highlight[0]);
285 }
286 }
287
288 for (i = 0; i < 8; ++i)
289 wp->w_border_char[i] = 0;
290 di = dict_find(dict, (char_u *)"borderchars", -1);
291 if (di != NULL)
292 {
293 if (di->di_tv.v_type != VAR_LIST)
294 emsg(_(e_listreq));
295 else
296 {
297 list_T *list = di->di_tv.vval.v_list;
298 listitem_T *li;
299
300 if (list != NULL)
301 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
302 ++i, li = li->li_next)
303 {
304 str = tv_get_string(&li->li_tv);
305 if (*str != NUL)
306 wp->w_border_char[i] = mb_ptr2char(str);
307 }
308 if (list->lv_len == 1)
309 for (i = 1; i < 8; ++i)
310 wp->w_border_char[i] = wp->w_border_char[0];
311 if (list->lv_len == 2)
312 {
313 for (i = 4; i < 8; ++i)
314 wp->w_border_char[i] = wp->w_border_char[1];
315 for (i = 1; i < 4; ++i)
316 wp->w_border_char[i] = wp->w_border_char[0];
317 }
318 }
319 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200320
321 di = dict_find(dict, (char_u *)"moved", -1);
322 if (di != NULL)
323 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200324 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200325 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
326 {
327 char_u *s = di->di_tv.vval.v_string;
328 int flags = 0;
329
330 if (STRCMP(s, "word") == 0)
331 flags = FIND_IDENT | FIND_STRING;
332 else if (STRCMP(s, "WORD") == 0)
333 flags = FIND_STRING;
334 else if (STRCMP(s, "any") != 0)
335 semsg(_(e_invarg2), s);
336 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200337 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200338 }
339 else if (di->di_tv.v_type == VAR_LIST
340 && di->di_tv.vval.v_list != NULL
341 && di->di_tv.vval.v_list->lv_len == 2)
342 {
343 list_T *l = di->di_tv.vval.v_list;
344
345 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
346 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
347 }
348 else
349 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
350 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200351
352 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200353}
354
355/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200356 * Add lines to the popup from a list of strings.
357 */
358 static void
359add_popup_strings(buf_T *buf, list_T *l)
360{
361 listitem_T *li;
362 linenr_T lnum = 0;
363 char_u *p;
364
365 for (li = l->lv_first; li != NULL; li = li->li_next)
366 if (li->li_tv.v_type == VAR_STRING)
367 {
368 p = li->li_tv.vval.v_string;
369 ml_append_buf(buf, lnum++,
370 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
371 }
372}
373
374/*
375 * Add lines to the popup from a list of dictionaries.
376 */
377 static void
378add_popup_dicts(buf_T *buf, list_T *l)
379{
380 listitem_T *li;
381 listitem_T *pli;
382 linenr_T lnum = 0;
383 char_u *p;
384 dict_T *dict;
385
386 // first add the text lines
387 for (li = l->lv_first; li != NULL; li = li->li_next)
388 {
389 if (li->li_tv.v_type != VAR_DICT)
390 {
391 emsg(_(e_dictreq));
392 return;
393 }
394 dict = li->li_tv.vval.v_dict;
395 p = dict == NULL ? NULL
396 : dict_get_string(dict, (char_u *)"text", FALSE);
397 ml_append_buf(buf, lnum++,
398 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
399 }
400
401 // add the text properties
402 lnum = 1;
403 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
404 {
405 dictitem_T *di;
406 list_T *plist;
407
408 dict = li->li_tv.vval.v_dict;
409 di = dict_find(dict, (char_u *)"props", -1);
410 if (di != NULL)
411 {
412 if (di->di_tv.v_type != VAR_LIST)
413 {
414 emsg(_(e_listreq));
415 return;
416 }
417 plist = di->di_tv.vval.v_list;
418 if (plist != NULL)
419 {
420 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
421 {
422 if (pli->li_tv.v_type != VAR_DICT)
423 {
424 emsg(_(e_dictreq));
425 return;
426 }
427 dict = pli->li_tv.vval.v_dict;
428 if (dict != NULL)
429 {
430 int col = dict_get_number(dict, (char_u *)"col");
431
432 prop_add_common( lnum, col, dict, buf, NULL);
433 }
434 }
435 }
436 }
437 }
438}
439
440/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200441 * Return the height of popup window "wp", including border and padding.
442 */
443 int
444popup_height(win_T *wp)
445{
446 return wp->w_height
447 + wp->w_popup_padding[0] + wp->w_popup_border[0]
448 + wp->w_popup_padding[2] + wp->w_popup_border[2];
449}
450
451/*
452 * Return the width of popup window "wp", including border and padding.
453 */
454 int
455popup_width(win_T *wp)
456{
457 return wp->w_width
458 + wp->w_popup_padding[3] + wp->w_popup_border[3]
459 + wp->w_popup_padding[1] + wp->w_popup_border[1];
460}
461
462/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200463 * Adjust the position and size of the popup to fit on the screen.
464 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200465 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200466popup_adjust_position(win_T *wp)
467{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200468 linenr_T lnum;
469 int wrapped = 0;
470 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200471 int center_vert = FALSE;
472 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200473 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaar399d8982019-06-02 15:34:29 +0200474 int top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
475 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
476 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
477 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
478 int extra_height = top_extra + bot_extra;
479 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200480 int org_winrow = wp->w_winrow;
481 int org_wincol = wp->w_wincol;
482 int org_width = wp->w_width;
483 int org_height = wp->w_height;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200484
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200485 wp->w_winrow = 0;
486 wp->w_wincol = 0;
487 if (wp->w_popup_pos == POPPOS_CENTER)
488 {
489 // center after computing the size
490 center_vert = TRUE;
491 center_hor = TRUE;
492 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200493 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200494 {
495 if (wp->w_wantline == 0)
496 center_vert = TRUE;
497 else if (wp->w_popup_pos == POPPOS_TOPLEFT
498 || wp->w_popup_pos == POPPOS_TOPRIGHT)
499 {
500 wp->w_winrow = wp->w_wantline - 1;
501 if (wp->w_winrow >= Rows)
502 wp->w_winrow = Rows - 1;
503 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200504
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200505 if (wp->w_wantcol == 0)
506 center_hor = TRUE;
507 else if (wp->w_popup_pos == POPPOS_TOPLEFT
508 || wp->w_popup_pos == POPPOS_BOTLEFT)
509 {
510 wp->w_wincol = wp->w_wantcol - 1;
511 if (wp->w_wincol >= Columns - 3)
512 wp->w_wincol = Columns - 3;
513 }
514 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200515
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200516 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200517 // When left aligned use the space available, but shift to the left when we
518 // hit the right of the screen.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200519 maxwidth = Columns - wp->w_wincol;
520 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200521 {
522 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200523 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200524 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200525
Bram Moolenaar8d241042019-06-12 23:40:01 +0200526 // start at the desired first line
527 wp->w_topline = wp->w_firstline;
528 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
529 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
530
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200531 // Compute width based on longest text line and the 'wrap' option.
532 // TODO: more accurate wrapping
533 wp->w_width = 0;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200534 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200535 {
536 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
537
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200538 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200539 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200540 while (len > maxwidth)
541 {
542 ++wrapped;
543 len -= maxwidth;
544 wp->w_width = maxwidth;
545 }
546 }
547 else if (len > maxwidth
548 && allow_adjust_left
549 && (wp->w_popup_pos == POPPOS_TOPLEFT
550 || wp->w_popup_pos == POPPOS_BOTLEFT))
551 {
552 // adjust leftwise to fit text on screen
553 int shift_by = ( len - maxwidth );
554
555 if ( shift_by > wp->w_wincol )
556 {
557 int truncate_shift = shift_by - wp->w_wincol;
558 len -= truncate_shift;
559 shift_by -= truncate_shift;
560 }
561
562 wp->w_wincol -= shift_by;
563 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200564 wp->w_width = maxwidth;
565 }
566 if (wp->w_width < len)
567 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200568 // do not use the width of lines we're not going to show
569 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
570 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
571 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200572 }
573
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200574 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
575 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200576 if (wp->w_width > maxwidth)
577 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200578 if (center_hor)
579 wp->w_wincol = (Columns - wp->w_width) / 2;
580 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
581 || wp->w_popup_pos == POPPOS_TOPRIGHT)
582 {
583 // Right aligned: move to the right if needed.
584 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200585 if (wp->w_width + extra_width < wp->w_wantcol)
586 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200587 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200588
Bram Moolenaar8d241042019-06-12 23:40:01 +0200589 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
590 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200591 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
592 wp->w_height = wp->w_minheight;
593 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
594 wp->w_height = wp->w_maxheight;
595 if (wp->w_height > Rows - wp->w_winrow)
596 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200597
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200598 if (center_vert)
599 wp->w_winrow = (Rows - wp->w_height) / 2;
600 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
601 || wp->w_popup_pos == POPPOS_BOTLEFT)
602 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200603 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200604 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200605 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200606 else
607 // not enough space, make top aligned
608 wp->w_winrow = wp->w_wantline + 1;
609 }
610
Bram Moolenaar17146962019-05-30 00:12:11 +0200611 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200612
613 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200614 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200615 if (org_winrow != wp->w_winrow
616 || org_wincol != wp->w_wincol
617 || org_width != wp->w_width
618 || org_height != wp->w_height)
619 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200620 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200621 popup_mask_refresh = TRUE;
622 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200623}
624
Bram Moolenaar17627312019-06-02 19:53:44 +0200625typedef enum
626{
627 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200628 TYPE_ATCURSOR,
629 TYPE_NOTIFICATION
Bram Moolenaar17627312019-06-02 19:53:44 +0200630} create_type_T;
631
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200632/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200633 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200634 * popup_atcursor({text}, {options})
Bram Moolenaar33796b32019-06-08 16:01:13 +0200635 * When called from f_popup_atcursor() "type" is TYPE_ATCURSOR.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200636 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200637 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200638popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200639{
640 win_T *wp;
641 buf_T *buf;
642 dict_T *d;
643 int nr;
644
645 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200646 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
647 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200648 {
649 emsg(_(e_listreq));
650 return;
651 }
652 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
653 {
654 emsg(_(e_dictreq));
655 return;
656 }
657 d = argvars[1].vval.v_dict;
658
659 // Create the window and buffer.
660 wp = win_alloc_popup_win();
661 if (wp == NULL)
662 return;
663 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200664 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200665
666 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
667 if (buf == NULL)
668 return;
669 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200670
671 win_init_popup_win(wp, buf);
672
673 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200674 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200675 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200676 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200677 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200678 buf->b_p_ul = -1; // no undo
679 buf->b_p_swf = FALSE; // no swap file
680 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200681 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200682 wp->w_p_wrap = TRUE; // 'wrap' is default on
683
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200684 // Avoid that 'buftype' is reset when this buffer is entered.
685 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200686
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200687 if (dict_find(d, (char_u *)"tab", -1) != NULL)
688 nr = (int)dict_get_number(d, (char_u *)"tab");
689 else if (type == TYPE_NOTIFICATION)
690 nr = -1; // notifications are global by default
691 else
692 nr = 0;
693
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200694 if (nr == 0)
695 {
696 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200697 wp->w_next = curtab->tp_first_popupwin;
698 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200699 }
700 else if (nr < 0)
701 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200702 win_T *prev = first_popupwin;
703
704 // Global popup: add at the end, so that it gets displayed on top of
705 // older ones with the same zindex. Matters for notifications.
706 if (first_popupwin == NULL)
707 first_popupwin = wp;
708 else
709 {
710 while (prev->w_next != NULL)
711 prev = prev->w_next;
712 prev->w_next = wp;
713 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200714 }
715 else
716 // TODO: find tab page "nr"
717 emsg("Not implemented yet");
718
719 // Add text to the buffer.
720 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200721 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200722 // just a string
723 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200724 }
725 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200726 {
727 list_T *l = argvars[0].vval.v_list;
728
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200729 if (l->lv_len > 0)
730 {
731 if (l->lv_first->li_tv.v_type == VAR_STRING)
732 // list of strings
733 add_popup_strings(buf, l);
734 else
735 // list of dictionaries
736 add_popup_dicts(buf, l);
737 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200738 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200739
740 // Delete the line of the empty buffer.
741 curbuf = buf;
742 ml_delete(buf->b_ml.ml_line_count, FALSE);
743 curbuf = curwin->w_buffer;
744
Bram Moolenaar17627312019-06-02 19:53:44 +0200745 if (type == TYPE_ATCURSOR)
746 {
747 wp->w_popup_pos = POPPOS_BOTLEFT;
748 setcursor_mayforce(TRUE);
749 wp->w_wantline = screen_screenrow();
750 if (wp->w_wantline == 0) // cursor in first line
751 {
752 wp->w_wantline = 2;
753 wp->w_popup_pos = POPPOS_TOPLEFT;
754 }
755 wp->w_wantcol = screen_screencol() + 1;
756 set_moved_values(wp);
757 set_moved_columns(wp, FIND_STRING);
758 }
759
Bram Moolenaar33796b32019-06-08 16:01:13 +0200760 // set default values
761 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
762
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200763 if (type == TYPE_NOTIFICATION)
764 {
765 win_T *twp, *nextwin;
766 int height = buf->b_ml.ml_line_count + 3;
767 int i;
768
769 // Try to not overlap with another global popup. Guess we need 3
770 // more screen lines than buffer lines.
771 wp->w_wantline = 1;
772 for (twp = first_popupwin; twp != NULL; twp = nextwin)
773 {
774 nextwin = twp->w_next;
775 if (twp != wp
776 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
777 && twp->w_winrow <= wp->w_wantline - 1 + height
778 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
779 {
780 // move to below this popup and restart the loop to check for
781 // overlap with other popups
782 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
783 nextwin = first_popupwin;
784 }
785 }
786 if (wp->w_wantline + height > Rows)
787 {
788 // can't avoid overlap, put on top in the hope that message goes
789 // away soon.
790 wp->w_wantline = 1;
791 }
792
793 wp->w_wantcol = 10;
794 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
795 for (i = 0; i < 4; ++i)
796 wp->w_popup_border[i] = 1;
797 wp->w_popup_padding[1] = 1;
798 wp->w_popup_padding[3] = 1;
799 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
800 (char_u *)"WarningMsg", OPT_FREE|OPT_LOCAL, 0);
801 }
802
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200803 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200804 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200805
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200806 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
807 popup_add_timeout(wp, 3000);
808
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200809 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200810
811 wp->w_vsep_width = 0;
812
813 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200814 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200815}
816
817/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200818 * popup_clear()
819 */
820 void
821f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
822{
823 close_all_popups();
824}
825
826/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200827 * popup_create({text}, {options})
828 */
829 void
830f_popup_create(typval_T *argvars, typval_T *rettv)
831{
Bram Moolenaar17627312019-06-02 19:53:44 +0200832 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200833}
834
835/*
836 * popup_atcursor({text}, {options})
837 */
838 void
839f_popup_atcursor(typval_T *argvars, typval_T *rettv)
840{
Bram Moolenaar17627312019-06-02 19:53:44 +0200841 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200842}
843
844/*
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200845 * popup_notification({text}, {options})
846 */
847 void
848f_popup_notification(typval_T *argvars, typval_T *rettv)
849{
850 popup_create(argvars, rettv, TYPE_NOTIFICATION);
851}
852
853/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200854 * Find the popup window with window-ID "id".
855 * If the popup window does not exist NULL is returned.
856 * If the window is not a popup window, and error message is given.
857 */
858 static win_T *
859find_popup_win(int id)
860{
861 win_T *wp = win_id2wp(id);
862
863 if (wp != NULL && !bt_popup(wp->w_buffer))
864 {
865 semsg(_("E993: window %d is not a popup window"), id);
866 return NULL;
867 }
868 return wp;
869}
870
871/*
872 * Return TRUE if there any popups that are not hidden.
873 */
874 int
875popup_any_visible(void)
876{
877 win_T *wp;
878
879 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200880 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200881 return TRUE;
882 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200883 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200884 return TRUE;
885 return FALSE;
886}
887
888/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200889 * Invoke the close callback for window "wp" with value "result".
890 * Careful: The callback may make "wp" invalid!
891 */
892 static void
893invoke_popup_callback(win_T *wp, typval_T *result)
894{
895 typval_T rettv;
896 int dummy;
897 typval_T argv[3];
898
899 argv[0].v_type = VAR_NUMBER;
900 argv[0].vval.v_number = (varnumber_T)wp->w_id;
901
902 if (result != NULL && result->v_type != VAR_UNKNOWN)
903 copy_tv(result, &argv[1]);
904 else
905 {
906 argv[1].v_type = VAR_NUMBER;
907 argv[1].vval.v_number = 0;
908 }
909
910 argv[2].v_type = VAR_UNKNOWN;
911
912 call_callback(&wp->w_close_cb, -1,
913 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
914 if (result != NULL)
915 clear_tv(&argv[1]);
916 clear_tv(&rettv);
917}
918
919/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200920 * Close popup "wp" and invoke any close callback for it.
921 */
922 static void
923popup_close_and_callback(win_T *wp, typval_T *arg)
924{
925 int id = wp->w_id;
926
927 if (wp->w_close_cb.cb_name != NULL)
928 // Careful: This may make "wp" invalid.
929 invoke_popup_callback(wp, arg);
930
931 popup_close(id);
932}
933
934/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200935 * popup_close({id})
936 */
937 void
938f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
939{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200940 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200941 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200942
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200943 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +0200944 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200945}
946
947/*
948 * popup_hide({id})
949 */
950 void
951f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
952{
953 int id = (int)tv_get_number(argvars);
954 win_T *wp = find_popup_win(id);
955
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200956 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200957 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200958 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200959 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200960 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200961 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200962 }
963}
964
965/*
966 * popup_show({id})
967 */
968 void
969f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
970{
971 int id = (int)tv_get_number(argvars);
972 win_T *wp = find_popup_win(id);
973
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200974 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200975 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200976 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200977 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200978 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200979 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200980 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200981}
982
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200983 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200984popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200985{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200986 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200987 if (wp->w_winrow + wp->w_height >= cmdline_row)
988 clear_cmdline = TRUE;
989 win_free_popup(wp);
990 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200991 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200992}
993
Bram Moolenaarec583842019-05-26 14:11:23 +0200994/*
995 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200996 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +0200997 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200998 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200999popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001000{
1001 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001002 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001003 win_T *prev = NULL;
1004
Bram Moolenaarec583842019-05-26 14:11:23 +02001005 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001006 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001007 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001008 {
1009 if (prev == NULL)
1010 first_popupwin = wp->w_next;
1011 else
1012 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001013 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001014 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001015 }
1016
Bram Moolenaarec583842019-05-26 14:11:23 +02001017 // go through tab-local popups
1018 FOR_ALL_TABPAGES(tp)
1019 popup_close_tabpage(tp, id);
1020}
1021
1022/*
1023 * Close a popup window with Window-id "id" in tabpage "tp".
1024 */
1025 void
1026popup_close_tabpage(tabpage_T *tp, int id)
1027{
1028 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001029 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001030 win_T *prev = NULL;
1031
Bram Moolenaarec583842019-05-26 14:11:23 +02001032 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1033 if (wp->w_id == id)
1034 {
1035 if (prev == NULL)
1036 *root = wp->w_next;
1037 else
1038 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001039 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001040 return;
1041 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001042}
1043
1044 void
1045close_all_popups(void)
1046{
1047 while (first_popupwin != NULL)
1048 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001049 while (curtab->tp_first_popupwin != NULL)
1050 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001051}
1052
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001053/*
1054 * popup_move({id}, {options})
1055 */
1056 void
1057f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1058{
1059 dict_T *d;
1060 int nr;
1061 int id = (int)tv_get_number(argvars);
1062 win_T *wp = find_popup_win(id);
1063
1064 if (wp == NULL)
1065 return; // invalid {id}
1066
1067 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1068 {
1069 emsg(_(e_dictreq));
1070 return;
1071 }
1072 d = argvars[1].vval.v_dict;
1073
1074 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1075 wp->w_minwidth = nr;
1076 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1077 wp->w_minheight = nr;
1078 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1079 wp->w_maxwidth = nr;
1080 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1081 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001082 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001083
1084 if (wp->w_winrow + wp->w_height >= cmdline_row)
1085 clear_cmdline = TRUE;
1086 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001087}
1088
Bram Moolenaarbc133542019-05-29 20:26:46 +02001089/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001090 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001091 */
1092 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001093f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001094{
1095 dict_T *dict;
1096 int id = (int)tv_get_number(argvars);
1097 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001098 int top_extra;
1099 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001100
1101 if (rettv_dict_alloc(rettv) == OK)
1102 {
1103 if (wp == NULL)
1104 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001105 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1106 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1107
Bram Moolenaarbc133542019-05-29 20:26:46 +02001108 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001109
Bram Moolenaarbc133542019-05-29 20:26:46 +02001110 dict_add_number(dict, "line", wp->w_winrow + 1);
1111 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001112 dict_add_number(dict, "width", wp->w_width + left_extra
1113 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1114 dict_add_number(dict, "height", wp->w_height + top_extra
1115 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001116
1117 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1118 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1119 dict_add_number(dict, "core_width", wp->w_width);
1120 dict_add_number(dict, "core_height", wp->w_height);
1121
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001122 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001123 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001124 }
1125}
1126
1127/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001128 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001129 */
1130 void
1131f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1132{
1133 dict_T *dict;
1134 int id = (int)tv_get_number(argvars);
1135 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001136 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001137
1138 if (rettv_dict_alloc(rettv) == OK)
1139 {
1140 if (wp == NULL)
1141 return;
1142
1143 dict = rettv->vval.v_dict;
1144 dict_add_number(dict, "line", wp->w_wantline);
1145 dict_add_number(dict, "col", wp->w_wantcol);
1146 dict_add_number(dict, "minwidth", wp->w_minwidth);
1147 dict_add_number(dict, "minheight", wp->w_minheight);
1148 dict_add_number(dict, "maxheight", wp->w_maxheight);
1149 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001150 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001151 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001152 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001153
1154 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1155 ++i)
1156 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1157 {
1158 dict_add_string(dict, "pos",
1159 (char_u *)poppos_entries[i].pp_name);
1160 break;
1161 }
1162
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001163# if defined(FEAT_TIMERS)
1164 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1165 ? (long)wp->w_popup_timer->tr_interval : 0L);
1166# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001167 }
1168}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001169
1170 int
1171not_in_popup_window()
1172{
1173 if (bt_popup(curwin->w_buffer))
1174 {
1175 emsg(_("E994: Not allowed in a popup window"));
1176 return TRUE;
1177 }
1178 return FALSE;
1179}
1180
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001181/*
1182 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
1183 * in the current tab.
1184 */
1185 void
1186popup_reset_handled()
1187{
1188 win_T *wp;
1189
1190 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1191 wp->w_popup_flags &= ~POPF_HANDLED;
1192 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1193 wp->w_popup_flags &= ~POPF_HANDLED;
1194}
1195
1196/*
1197 * Find the next visible popup where POPF_HANDLED is not set.
1198 * Must have called popup_reset_handled() first.
1199 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1200 * popup with the highest zindex.
1201 */
1202 win_T *
1203find_next_popup(int lowest)
1204{
1205 win_T *wp;
1206 win_T *found_wp;
1207 int found_zindex;
1208
1209 found_zindex = lowest ? INT_MAX : 0;
1210 found_wp = NULL;
1211 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1212 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1213 && (lowest ? wp->w_zindex < found_zindex
1214 : wp->w_zindex > found_zindex))
1215 {
1216 found_zindex = wp->w_zindex;
1217 found_wp = wp;
1218 }
1219 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1220 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1221 && (lowest ? wp->w_zindex < found_zindex
1222 : wp->w_zindex > found_zindex))
1223 {
1224 found_zindex = wp->w_zindex;
1225 found_wp = wp;
1226 }
1227
1228 if (found_wp != NULL)
1229 found_wp->w_popup_flags |= POPF_HANDLED;
1230 return found_wp;
1231}
1232
1233/*
1234 * Invoke the filter callback for window "wp" with typed character "c".
1235 * Uses the global "mod_mask" for modifiers.
1236 * Returns the return value of the filter.
1237 * Careful: The filter may make "wp" invalid!
1238 */
1239 static int
1240invoke_popup_filter(win_T *wp, int c)
1241{
1242 int res;
1243 typval_T rettv;
1244 int dummy;
1245 typval_T argv[3];
1246 char_u buf[NUMBUFLEN];
1247
1248 argv[0].v_type = VAR_NUMBER;
1249 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1250
1251 // Convert the number to a string, so that the function can use:
1252 // if a:c == "\<F2>"
1253 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1254 argv[1].v_type = VAR_STRING;
1255 argv[1].vval.v_string = vim_strsave(buf);
1256
1257 argv[2].v_type = VAR_UNKNOWN;
1258
1259 call_callback(&wp->w_filter_cb, -1,
1260 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1261 res = tv_get_number(&rettv);
1262 vim_free(argv[1].vval.v_string);
1263 clear_tv(&rettv);
1264 return res;
1265}
1266
1267/*
1268 * Called when "c" was typed: invoke popup filter callbacks.
1269 * Returns TRUE when the character was consumed,
1270 */
1271 int
1272popup_do_filter(int c)
1273{
1274 int res = FALSE;
1275 win_T *wp;
1276
1277 popup_reset_handled();
1278
1279 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1280 if (wp->w_filter_cb.cb_name != NULL)
1281 res = invoke_popup_filter(wp, c);
1282
1283 return res;
1284}
1285
Bram Moolenaar3397f742019-06-02 18:40:06 +02001286/*
1287 * Called when the cursor moved: check if any popup needs to be closed if the
1288 * cursor moved far enough.
1289 */
1290 void
1291popup_check_cursor_pos()
1292{
1293 win_T *wp;
1294 typval_T tv;
1295
1296 popup_reset_handled();
1297 while ((wp = find_next_popup(TRUE)) != NULL)
1298 if (wp->w_popup_curwin != NULL
1299 && (curwin != wp->w_popup_curwin
1300 || curwin->w_cursor.lnum != wp->w_popup_lnum
1301 || curwin->w_cursor.col < wp->w_popup_mincol
1302 || curwin->w_cursor.col > wp->w_popup_maxcol))
1303 {
1304 tv.v_type = VAR_NUMBER;
1305 tv.vval.v_number = -1;
1306 popup_close_and_callback(wp, &tv);
1307 }
1308}
1309
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001310#endif // FEAT_TEXT_PROP