blob: 13ba6391db42fe1f597eb78e46aa03a412be15eb [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
113 vim_memset(array, 0, sizeof(int) * 4);
114 di = dict_find(dict, (char_u *)name, -1);
115 if (di != NULL)
116 {
117 if (di->di_tv.v_type != VAR_LIST)
118 emsg(_(e_listreq));
119 else
120 {
121 list_T *list = di->di_tv.vval.v_list;
122 listitem_T *li;
123 int i;
124 int nr;
125
126 for (i = 0; i < 4; ++i)
127 array[i] = 1;
128 if (list != NULL)
129 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
130 ++i, li = li->li_next)
131 {
132 nr = (int)tv_get_number(&li->li_tv);
133 if (nr >= 0)
134 array[i] = nr > max_val ? max_val : nr;
135 }
136 }
137 }
138}
139
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200140/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200141 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200142 */
143 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200144set_moved_values(win_T *wp)
145{
146 wp->w_popup_curwin = curwin;
147 wp->w_popup_lnum = curwin->w_cursor.lnum;
148 wp->w_popup_mincol = curwin->w_cursor.col;
149 wp->w_popup_maxcol = curwin->w_cursor.col;
150}
151
152/*
153 * Used when popup options contain "moved" with "word" or "WORD".
154 */
155 static void
156set_moved_columns(win_T *wp, int flags)
157{
158 char_u *ptr;
159 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
160
161 if (len > 0)
162 {
163 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
164 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
165 }
166}
167
168/*
169 * Go through the options in "dict" and apply them to buffer "buf" displayed in
170 * popup window "wp".
171 */
172 static void
173apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200174{
Bram Moolenaar402502d2019-05-30 22:07:36 +0200175 int nr;
176 char_u *str;
177 dictitem_T *di;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200178 int i;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200179
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200180 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
181 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200182 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
183 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200184
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200185 get_pos_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200186
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200187 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
Bram Moolenaar33796b32019-06-08 16:01:13 +0200188 if (wp->w_zindex < 1)
189 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
190 if (wp->w_zindex > 32000)
191 wp->w_zindex = 32000;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200192
Bram Moolenaar33796b32019-06-08 16:01:13 +0200193# if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200194 // Add timer to close the popup after some time.
195 nr = dict_get_number(dict, (char_u *)"time");
196 if (nr > 0)
197 {
198 char_u cbbuf[50];
199 char_u *ptr = cbbuf;
200 typval_T tv;
201
202 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
203 "{_ -> popup_close(%d)}", wp->w_id);
204 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
205 {
206 wp->w_popup_timer = create_timer(nr, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200207 wp->w_popup_timer->tr_callback = get_callback(&tv);
208 clear_tv(&tv);
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200209 }
210 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200211# endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200212
Bram Moolenaar402502d2019-05-30 22:07:36 +0200213 // Option values resulting in setting an option.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200214 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200215 if (str != NULL)
216 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
217 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200218
Bram Moolenaar402502d2019-05-30 22:07:36 +0200219 di = dict_find(dict, (char_u *)"wrap", -1);
220 if (di != NULL)
221 {
222 nr = dict_get_number(dict, (char_u *)"wrap");
223 wp->w_p_wrap = nr != 0;
224 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200225
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200226 di = dict_find(dict, (char_u *)"callback", -1);
227 if (di != NULL)
228 {
229 callback_T callback = get_callback(&di->di_tv);
230
231 if (callback.cb_name != NULL)
232 set_callback(&wp->w_close_cb, &callback);
233 }
234
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200235 di = dict_find(dict, (char_u *)"filter", -1);
236 if (di != NULL)
237 {
238 callback_T callback = get_callback(&di->di_tv);
239
240 if (callback.cb_name != NULL)
241 set_callback(&wp->w_filter_cb, &callback);
242 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200243
244 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
245 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200246
247 for (i = 0; i < 4; ++i)
248 VIM_CLEAR(wp->w_border_highlight[i]);
249 di = dict_find(dict, (char_u *)"borderhighlight", -1);
250 if (di != NULL)
251 {
252 if (di->di_tv.v_type != VAR_LIST)
253 emsg(_(e_listreq));
254 else
255 {
256 list_T *list = di->di_tv.vval.v_list;
257 listitem_T *li;
258
259 if (list != NULL)
260 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
261 ++i, li = li->li_next)
262 {
263 str = tv_get_string(&li->li_tv);
264 if (*str != NUL)
265 wp->w_border_highlight[i] = vim_strsave(str);
266 }
267 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
268 for (i = 1; i < 4; ++i)
269 wp->w_border_highlight[i] =
270 vim_strsave(wp->w_border_highlight[0]);
271 }
272 }
273
274 for (i = 0; i < 8; ++i)
275 wp->w_border_char[i] = 0;
276 di = dict_find(dict, (char_u *)"borderchars", -1);
277 if (di != NULL)
278 {
279 if (di->di_tv.v_type != VAR_LIST)
280 emsg(_(e_listreq));
281 else
282 {
283 list_T *list = di->di_tv.vval.v_list;
284 listitem_T *li;
285
286 if (list != NULL)
287 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
288 ++i, li = li->li_next)
289 {
290 str = tv_get_string(&li->li_tv);
291 if (*str != NUL)
292 wp->w_border_char[i] = mb_ptr2char(str);
293 }
294 if (list->lv_len == 1)
295 for (i = 1; i < 8; ++i)
296 wp->w_border_char[i] = wp->w_border_char[0];
297 if (list->lv_len == 2)
298 {
299 for (i = 4; i < 8; ++i)
300 wp->w_border_char[i] = wp->w_border_char[1];
301 for (i = 1; i < 4; ++i)
302 wp->w_border_char[i] = wp->w_border_char[0];
303 }
304 }
305 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200306
307 di = dict_find(dict, (char_u *)"moved", -1);
308 if (di != NULL)
309 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200310 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200311 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
312 {
313 char_u *s = di->di_tv.vval.v_string;
314 int flags = 0;
315
316 if (STRCMP(s, "word") == 0)
317 flags = FIND_IDENT | FIND_STRING;
318 else if (STRCMP(s, "WORD") == 0)
319 flags = FIND_STRING;
320 else if (STRCMP(s, "any") != 0)
321 semsg(_(e_invarg2), s);
322 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200323 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200324 }
325 else if (di->di_tv.v_type == VAR_LIST
326 && di->di_tv.vval.v_list != NULL
327 && di->di_tv.vval.v_list->lv_len == 2)
328 {
329 list_T *l = di->di_tv.vval.v_list;
330
331 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
332 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
333 }
334 else
335 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
336 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200337
338 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200339}
340
341/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200342 * Add lines to the popup from a list of strings.
343 */
344 static void
345add_popup_strings(buf_T *buf, list_T *l)
346{
347 listitem_T *li;
348 linenr_T lnum = 0;
349 char_u *p;
350
351 for (li = l->lv_first; li != NULL; li = li->li_next)
352 if (li->li_tv.v_type == VAR_STRING)
353 {
354 p = li->li_tv.vval.v_string;
355 ml_append_buf(buf, lnum++,
356 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
357 }
358}
359
360/*
361 * Add lines to the popup from a list of dictionaries.
362 */
363 static void
364add_popup_dicts(buf_T *buf, list_T *l)
365{
366 listitem_T *li;
367 listitem_T *pli;
368 linenr_T lnum = 0;
369 char_u *p;
370 dict_T *dict;
371
372 // first add the text lines
373 for (li = l->lv_first; li != NULL; li = li->li_next)
374 {
375 if (li->li_tv.v_type != VAR_DICT)
376 {
377 emsg(_(e_dictreq));
378 return;
379 }
380 dict = li->li_tv.vval.v_dict;
381 p = dict == NULL ? NULL
382 : dict_get_string(dict, (char_u *)"text", FALSE);
383 ml_append_buf(buf, lnum++,
384 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
385 }
386
387 // add the text properties
388 lnum = 1;
389 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
390 {
391 dictitem_T *di;
392 list_T *plist;
393
394 dict = li->li_tv.vval.v_dict;
395 di = dict_find(dict, (char_u *)"props", -1);
396 if (di != NULL)
397 {
398 if (di->di_tv.v_type != VAR_LIST)
399 {
400 emsg(_(e_listreq));
401 return;
402 }
403 plist = di->di_tv.vval.v_list;
404 if (plist != NULL)
405 {
406 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
407 {
408 if (pli->li_tv.v_type != VAR_DICT)
409 {
410 emsg(_(e_dictreq));
411 return;
412 }
413 dict = pli->li_tv.vval.v_dict;
414 if (dict != NULL)
415 {
416 int col = dict_get_number(dict, (char_u *)"col");
417
418 prop_add_common( lnum, col, dict, buf, NULL);
419 }
420 }
421 }
422 }
423 }
424}
425
426/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200427 * Adjust the position and size of the popup to fit on the screen.
428 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200429 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200430popup_adjust_position(win_T *wp)
431{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200432 linenr_T lnum;
433 int wrapped = 0;
434 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200435 int center_vert = FALSE;
436 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200437 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaar399d8982019-06-02 15:34:29 +0200438 int top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
439 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
440 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
441 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
442 int extra_height = top_extra + bot_extra;
443 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200444 int org_winrow = wp->w_winrow;
445 int org_wincol = wp->w_wincol;
446 int org_width = wp->w_width;
447 int org_height = wp->w_height;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200448
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200449 wp->w_winrow = 0;
450 wp->w_wincol = 0;
451 if (wp->w_popup_pos == POPPOS_CENTER)
452 {
453 // center after computing the size
454 center_vert = TRUE;
455 center_hor = TRUE;
456 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200457 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200458 {
459 if (wp->w_wantline == 0)
460 center_vert = TRUE;
461 else if (wp->w_popup_pos == POPPOS_TOPLEFT
462 || wp->w_popup_pos == POPPOS_TOPRIGHT)
463 {
464 wp->w_winrow = wp->w_wantline - 1;
465 if (wp->w_winrow >= Rows)
466 wp->w_winrow = Rows - 1;
467 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200468
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200469 if (wp->w_wantcol == 0)
470 center_hor = TRUE;
471 else if (wp->w_popup_pos == POPPOS_TOPLEFT
472 || wp->w_popup_pos == POPPOS_BOTLEFT)
473 {
474 wp->w_wincol = wp->w_wantcol - 1;
475 if (wp->w_wincol >= Columns - 3)
476 wp->w_wincol = Columns - 3;
477 }
478 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200479
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200480 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200481 // When left aligned use the space available, but shift to the left when we
482 // hit the right of the screen.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200483 maxwidth = Columns - wp->w_wincol;
484 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200485 {
486 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200487 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200488 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200489
490 // Compute width based on longest text line and the 'wrap' option.
491 // TODO: more accurate wrapping
492 wp->w_width = 0;
493 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
494 {
495 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
496
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200497 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200498 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200499 while (len > maxwidth)
500 {
501 ++wrapped;
502 len -= maxwidth;
503 wp->w_width = maxwidth;
504 }
505 }
506 else if (len > maxwidth
507 && allow_adjust_left
508 && (wp->w_popup_pos == POPPOS_TOPLEFT
509 || wp->w_popup_pos == POPPOS_BOTLEFT))
510 {
511 // adjust leftwise to fit text on screen
512 int shift_by = ( len - maxwidth );
513
514 if ( shift_by > wp->w_wincol )
515 {
516 int truncate_shift = shift_by - wp->w_wincol;
517 len -= truncate_shift;
518 shift_by -= truncate_shift;
519 }
520
521 wp->w_wincol -= shift_by;
522 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200523 wp->w_width = maxwidth;
524 }
525 if (wp->w_width < len)
526 wp->w_width = len;
527 }
528
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200529 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
530 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200531 if (wp->w_width > maxwidth)
532 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200533 if (center_hor)
534 wp->w_wincol = (Columns - wp->w_width) / 2;
535 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
536 || wp->w_popup_pos == POPPOS_TOPRIGHT)
537 {
538 // Right aligned: move to the right if needed.
539 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200540 if (wp->w_width + extra_width < wp->w_wantcol)
541 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200542 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200543
544 if (wp->w_height <= 1)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200545 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200546 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
547 wp->w_height = wp->w_minheight;
548 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
549 wp->w_height = wp->w_maxheight;
550 if (wp->w_height > Rows - wp->w_winrow)
551 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200552
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200553 if (center_vert)
554 wp->w_winrow = (Rows - wp->w_height) / 2;
555 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
556 || wp->w_popup_pos == POPPOS_BOTLEFT)
557 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200558 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200559 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200560 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200561 else
562 // not enough space, make top aligned
563 wp->w_winrow = wp->w_wantline + 1;
564 }
565
Bram Moolenaar17146962019-05-30 00:12:11 +0200566 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200567
568 // Need to update popup_mask if the position or size changed.
569 if (org_winrow != wp->w_winrow
570 || org_wincol != wp->w_wincol
571 || org_width != wp->w_width
572 || org_height != wp->w_height)
573 {
574 redraw_all_later(NOT_VALID);
575 popup_mask_refresh = TRUE;
576 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200577}
578
Bram Moolenaar17627312019-06-02 19:53:44 +0200579typedef enum
580{
581 TYPE_NORMAL,
582 TYPE_ATCURSOR
583} create_type_T;
584
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200585/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200586 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200587 * popup_atcursor({text}, {options})
Bram Moolenaar33796b32019-06-08 16:01:13 +0200588 * When called from f_popup_atcursor() "type" is TYPE_ATCURSOR.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200589 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200590 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200591popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200592{
593 win_T *wp;
594 buf_T *buf;
595 dict_T *d;
596 int nr;
597
598 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200599 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
600 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200601 {
602 emsg(_(e_listreq));
603 return;
604 }
605 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
606 {
607 emsg(_(e_dictreq));
608 return;
609 }
610 d = argvars[1].vval.v_dict;
611
612 // Create the window and buffer.
613 wp = win_alloc_popup_win();
614 if (wp == NULL)
615 return;
616 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200617 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200618
619 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
620 if (buf == NULL)
621 return;
622 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200623
624 win_init_popup_win(wp, buf);
625
626 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200627 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200628 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200629 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200630 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200631 buf->b_p_ul = -1; // no undo
632 buf->b_p_swf = FALSE; // no swap file
633 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200634 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200635 wp->w_p_wrap = TRUE; // 'wrap' is default on
636
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200637 // Avoid that 'buftype' is reset when this buffer is entered.
638 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200639
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200640 nr = (int)dict_get_number(d, (char_u *)"tab");
641 if (nr == 0)
642 {
643 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200644 wp->w_next = curtab->tp_first_popupwin;
645 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200646 }
647 else if (nr < 0)
648 {
649 // global popup
650 wp->w_next = first_popupwin;
651 first_popupwin = wp;
652 }
653 else
654 // TODO: find tab page "nr"
655 emsg("Not implemented yet");
656
657 // Add text to the buffer.
658 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200659 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200660 // just a string
661 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200662 }
663 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200664 {
665 list_T *l = argvars[0].vval.v_list;
666
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200667 if (l->lv_len > 0)
668 {
669 if (l->lv_first->li_tv.v_type == VAR_STRING)
670 // list of strings
671 add_popup_strings(buf, l);
672 else
673 // list of dictionaries
674 add_popup_dicts(buf, l);
675 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200676 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200677
678 // Delete the line of the empty buffer.
679 curbuf = buf;
680 ml_delete(buf->b_ml.ml_line_count, FALSE);
681 curbuf = curwin->w_buffer;
682
Bram Moolenaar17627312019-06-02 19:53:44 +0200683 if (type == TYPE_ATCURSOR)
684 {
685 wp->w_popup_pos = POPPOS_BOTLEFT;
686 setcursor_mayforce(TRUE);
687 wp->w_wantline = screen_screenrow();
688 if (wp->w_wantline == 0) // cursor in first line
689 {
690 wp->w_wantline = 2;
691 wp->w_popup_pos = POPPOS_TOPLEFT;
692 }
693 wp->w_wantcol = screen_screencol() + 1;
694 set_moved_values(wp);
695 set_moved_columns(wp, FIND_STRING);
696 }
697
Bram Moolenaar33796b32019-06-08 16:01:13 +0200698 // set default values
699 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
700
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200701 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200702 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200703
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200704 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200705
706 wp->w_vsep_width = 0;
707
708 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200709 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200710}
711
712/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200713 * popup_create({text}, {options})
714 */
715 void
716f_popup_create(typval_T *argvars, typval_T *rettv)
717{
Bram Moolenaar17627312019-06-02 19:53:44 +0200718 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200719}
720
721/*
722 * popup_atcursor({text}, {options})
723 */
724 void
725f_popup_atcursor(typval_T *argvars, typval_T *rettv)
726{
Bram Moolenaar17627312019-06-02 19:53:44 +0200727 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200728}
729
730/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200731 * Find the popup window with window-ID "id".
732 * If the popup window does not exist NULL is returned.
733 * If the window is not a popup window, and error message is given.
734 */
735 static win_T *
736find_popup_win(int id)
737{
738 win_T *wp = win_id2wp(id);
739
740 if (wp != NULL && !bt_popup(wp->w_buffer))
741 {
742 semsg(_("E993: window %d is not a popup window"), id);
743 return NULL;
744 }
745 return wp;
746}
747
748/*
749 * Return TRUE if there any popups that are not hidden.
750 */
751 int
752popup_any_visible(void)
753{
754 win_T *wp;
755
756 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200757 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200758 return TRUE;
759 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200760 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200761 return TRUE;
762 return FALSE;
763}
764
765/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200766 * Invoke the close callback for window "wp" with value "result".
767 * Careful: The callback may make "wp" invalid!
768 */
769 static void
770invoke_popup_callback(win_T *wp, typval_T *result)
771{
772 typval_T rettv;
773 int dummy;
774 typval_T argv[3];
775
776 argv[0].v_type = VAR_NUMBER;
777 argv[0].vval.v_number = (varnumber_T)wp->w_id;
778
779 if (result != NULL && result->v_type != VAR_UNKNOWN)
780 copy_tv(result, &argv[1]);
781 else
782 {
783 argv[1].v_type = VAR_NUMBER;
784 argv[1].vval.v_number = 0;
785 }
786
787 argv[2].v_type = VAR_UNKNOWN;
788
789 call_callback(&wp->w_close_cb, -1,
790 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
791 if (result != NULL)
792 clear_tv(&argv[1]);
793 clear_tv(&rettv);
794}
795
796/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200797 * Close popup "wp" and invoke any close callback for it.
798 */
799 static void
800popup_close_and_callback(win_T *wp, typval_T *arg)
801{
802 int id = wp->w_id;
803
804 if (wp->w_close_cb.cb_name != NULL)
805 // Careful: This may make "wp" invalid.
806 invoke_popup_callback(wp, arg);
807
808 popup_close(id);
809}
810
811/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200812 * popup_close({id})
813 */
814 void
815f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
816{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200817 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200818 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200819
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200820 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +0200821 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200822}
823
824/*
825 * popup_hide({id})
826 */
827 void
828f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
829{
830 int id = (int)tv_get_number(argvars);
831 win_T *wp = find_popup_win(id);
832
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200833 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200834 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200835 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200836 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200837 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200838 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200839 }
840}
841
842/*
843 * popup_show({id})
844 */
845 void
846f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
847{
848 int id = (int)tv_get_number(argvars);
849 win_T *wp = find_popup_win(id);
850
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200851 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200852 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200853 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200854 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200855 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200856 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200857 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200858}
859
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200860 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200861popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200862{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200863 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200864 if (wp->w_winrow + wp->w_height >= cmdline_row)
865 clear_cmdline = TRUE;
866 win_free_popup(wp);
867 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200868 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200869}
870
Bram Moolenaarec583842019-05-26 14:11:23 +0200871/*
872 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200873 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +0200874 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200875 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200876popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200877{
878 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200879 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200880 win_T *prev = NULL;
881
Bram Moolenaarec583842019-05-26 14:11:23 +0200882 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200883 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200884 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200885 {
886 if (prev == NULL)
887 first_popupwin = wp->w_next;
888 else
889 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200890 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200891 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200892 }
893
Bram Moolenaarec583842019-05-26 14:11:23 +0200894 // go through tab-local popups
895 FOR_ALL_TABPAGES(tp)
896 popup_close_tabpage(tp, id);
897}
898
899/*
900 * Close a popup window with Window-id "id" in tabpage "tp".
901 */
902 void
903popup_close_tabpage(tabpage_T *tp, int id)
904{
905 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200906 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200907 win_T *prev = NULL;
908
Bram Moolenaarec583842019-05-26 14:11:23 +0200909 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
910 if (wp->w_id == id)
911 {
912 if (prev == NULL)
913 *root = wp->w_next;
914 else
915 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200916 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200917 return;
918 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200919}
920
921 void
922close_all_popups(void)
923{
924 while (first_popupwin != NULL)
925 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200926 while (curtab->tp_first_popupwin != NULL)
927 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200928}
929
930 void
931ex_popupclear(exarg_T *eap UNUSED)
932{
933 close_all_popups();
934}
935
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200936/*
937 * popup_move({id}, {options})
938 */
939 void
940f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
941{
942 dict_T *d;
943 int nr;
944 int id = (int)tv_get_number(argvars);
945 win_T *wp = find_popup_win(id);
946
947 if (wp == NULL)
948 return; // invalid {id}
949
950 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
951 {
952 emsg(_(e_dictreq));
953 return;
954 }
955 d = argvars[1].vval.v_dict;
956
957 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
958 wp->w_minwidth = nr;
959 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
960 wp->w_minheight = nr;
961 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
962 wp->w_maxwidth = nr;
963 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
964 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200965 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200966
967 if (wp->w_winrow + wp->w_height >= cmdline_row)
968 clear_cmdline = TRUE;
969 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200970}
971
Bram Moolenaarbc133542019-05-29 20:26:46 +0200972/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200973 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200974 */
975 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200976f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200977{
978 dict_T *dict;
979 int id = (int)tv_get_number(argvars);
980 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200981 int top_extra;
982 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +0200983
984 if (rettv_dict_alloc(rettv) == OK)
985 {
986 if (wp == NULL)
987 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200988 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
989 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
990
Bram Moolenaarbc133542019-05-29 20:26:46 +0200991 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200992
Bram Moolenaarbc133542019-05-29 20:26:46 +0200993 dict_add_number(dict, "line", wp->w_winrow + 1);
994 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200995 dict_add_number(dict, "width", wp->w_width + left_extra + wp->w_popup_border[1] + wp->w_popup_padding[1]);
996 dict_add_number(dict, "height", wp->w_height + top_extra + wp->w_popup_border[2] + wp->w_popup_padding[2]);
997
998 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
999 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1000 dict_add_number(dict, "core_width", wp->w_width);
1001 dict_add_number(dict, "core_height", wp->w_height);
1002
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001003 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001004 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001005 }
1006}
1007
1008/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001009 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001010 */
1011 void
1012f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1013{
1014 dict_T *dict;
1015 int id = (int)tv_get_number(argvars);
1016 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001017 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001018
1019 if (rettv_dict_alloc(rettv) == OK)
1020 {
1021 if (wp == NULL)
1022 return;
1023
1024 dict = rettv->vval.v_dict;
1025 dict_add_number(dict, "line", wp->w_wantline);
1026 dict_add_number(dict, "col", wp->w_wantcol);
1027 dict_add_number(dict, "minwidth", wp->w_minwidth);
1028 dict_add_number(dict, "minheight", wp->w_minheight);
1029 dict_add_number(dict, "maxheight", wp->w_maxheight);
1030 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
1031 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001032 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001033
1034 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1035 ++i)
1036 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1037 {
1038 dict_add_string(dict, "pos",
1039 (char_u *)poppos_entries[i].pp_name);
1040 break;
1041 }
1042
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001043# if defined(FEAT_TIMERS)
1044 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1045 ? (long)wp->w_popup_timer->tr_interval : 0L);
1046# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001047 }
1048}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001049
1050 int
1051not_in_popup_window()
1052{
1053 if (bt_popup(curwin->w_buffer))
1054 {
1055 emsg(_("E994: Not allowed in a popup window"));
1056 return TRUE;
1057 }
1058 return FALSE;
1059}
1060
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001061/*
1062 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
1063 * in the current tab.
1064 */
1065 void
1066popup_reset_handled()
1067{
1068 win_T *wp;
1069
1070 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1071 wp->w_popup_flags &= ~POPF_HANDLED;
1072 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1073 wp->w_popup_flags &= ~POPF_HANDLED;
1074}
1075
1076/*
1077 * Find the next visible popup where POPF_HANDLED is not set.
1078 * Must have called popup_reset_handled() first.
1079 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1080 * popup with the highest zindex.
1081 */
1082 win_T *
1083find_next_popup(int lowest)
1084{
1085 win_T *wp;
1086 win_T *found_wp;
1087 int found_zindex;
1088
1089 found_zindex = lowest ? INT_MAX : 0;
1090 found_wp = NULL;
1091 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1092 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1093 && (lowest ? wp->w_zindex < found_zindex
1094 : wp->w_zindex > found_zindex))
1095 {
1096 found_zindex = wp->w_zindex;
1097 found_wp = wp;
1098 }
1099 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1100 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1101 && (lowest ? wp->w_zindex < found_zindex
1102 : wp->w_zindex > found_zindex))
1103 {
1104 found_zindex = wp->w_zindex;
1105 found_wp = wp;
1106 }
1107
1108 if (found_wp != NULL)
1109 found_wp->w_popup_flags |= POPF_HANDLED;
1110 return found_wp;
1111}
1112
1113/*
1114 * Invoke the filter callback for window "wp" with typed character "c".
1115 * Uses the global "mod_mask" for modifiers.
1116 * Returns the return value of the filter.
1117 * Careful: The filter may make "wp" invalid!
1118 */
1119 static int
1120invoke_popup_filter(win_T *wp, int c)
1121{
1122 int res;
1123 typval_T rettv;
1124 int dummy;
1125 typval_T argv[3];
1126 char_u buf[NUMBUFLEN];
1127
1128 argv[0].v_type = VAR_NUMBER;
1129 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1130
1131 // Convert the number to a string, so that the function can use:
1132 // if a:c == "\<F2>"
1133 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1134 argv[1].v_type = VAR_STRING;
1135 argv[1].vval.v_string = vim_strsave(buf);
1136
1137 argv[2].v_type = VAR_UNKNOWN;
1138
1139 call_callback(&wp->w_filter_cb, -1,
1140 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1141 res = tv_get_number(&rettv);
1142 vim_free(argv[1].vval.v_string);
1143 clear_tv(&rettv);
1144 return res;
1145}
1146
1147/*
1148 * Called when "c" was typed: invoke popup filter callbacks.
1149 * Returns TRUE when the character was consumed,
1150 */
1151 int
1152popup_do_filter(int c)
1153{
1154 int res = FALSE;
1155 win_T *wp;
1156
1157 popup_reset_handled();
1158
1159 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1160 if (wp->w_filter_cb.cb_name != NULL)
1161 res = invoke_popup_filter(wp, c);
1162
1163 return res;
1164}
1165
Bram Moolenaar3397f742019-06-02 18:40:06 +02001166/*
1167 * Called when the cursor moved: check if any popup needs to be closed if the
1168 * cursor moved far enough.
1169 */
1170 void
1171popup_check_cursor_pos()
1172{
1173 win_T *wp;
1174 typval_T tv;
1175
1176 popup_reset_handled();
1177 while ((wp = find_next_popup(TRUE)) != NULL)
1178 if (wp->w_popup_curwin != NULL
1179 && (curwin != wp->w_popup_curwin
1180 || curwin->w_cursor.lnum != wp->w_popup_lnum
1181 || curwin->w_cursor.col < wp->w_popup_mincol
1182 || curwin->w_cursor.col > wp->w_popup_maxcol))
1183 {
1184 tv.v_type = VAR_NUMBER;
1185 tv.vval.v_number = -1;
1186 popup_close_and_callback(wp, &tv);
1187 }
1188}
1189
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001190#endif // FEAT_TEXT_PROP