blob: adfa462299a5ae0177a9711c2f49e9c77303e297 [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 Moolenaar451d4b52019-06-12 20:22:27 +0200427 * Return the height of popup window "wp", including border and padding.
428 */
429 int
430popup_height(win_T *wp)
431{
432 return wp->w_height
433 + wp->w_popup_padding[0] + wp->w_popup_border[0]
434 + wp->w_popup_padding[2] + wp->w_popup_border[2];
435}
436
437/*
438 * Return the width of popup window "wp", including border and padding.
439 */
440 int
441popup_width(win_T *wp)
442{
443 return wp->w_width
444 + wp->w_popup_padding[3] + wp->w_popup_border[3]
445 + wp->w_popup_padding[1] + wp->w_popup_border[1];
446}
447
448/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200449 * Adjust the position and size of the popup to fit on the screen.
450 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200451 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200452popup_adjust_position(win_T *wp)
453{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200454 linenr_T lnum;
455 int wrapped = 0;
456 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200457 int center_vert = FALSE;
458 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200459 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaar399d8982019-06-02 15:34:29 +0200460 int top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
461 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
462 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
463 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
464 int extra_height = top_extra + bot_extra;
465 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200466 int org_winrow = wp->w_winrow;
467 int org_wincol = wp->w_wincol;
468 int org_width = wp->w_width;
469 int org_height = wp->w_height;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200470
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200471 wp->w_winrow = 0;
472 wp->w_wincol = 0;
473 if (wp->w_popup_pos == POPPOS_CENTER)
474 {
475 // center after computing the size
476 center_vert = TRUE;
477 center_hor = TRUE;
478 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200479 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200480 {
481 if (wp->w_wantline == 0)
482 center_vert = TRUE;
483 else if (wp->w_popup_pos == POPPOS_TOPLEFT
484 || wp->w_popup_pos == POPPOS_TOPRIGHT)
485 {
486 wp->w_winrow = wp->w_wantline - 1;
487 if (wp->w_winrow >= Rows)
488 wp->w_winrow = Rows - 1;
489 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200490
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200491 if (wp->w_wantcol == 0)
492 center_hor = TRUE;
493 else if (wp->w_popup_pos == POPPOS_TOPLEFT
494 || wp->w_popup_pos == POPPOS_BOTLEFT)
495 {
496 wp->w_wincol = wp->w_wantcol - 1;
497 if (wp->w_wincol >= Columns - 3)
498 wp->w_wincol = Columns - 3;
499 }
500 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200501
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200502 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200503 // When left aligned use the space available, but shift to the left when we
504 // hit the right of the screen.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200505 maxwidth = Columns - wp->w_wincol;
506 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200507 {
508 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200509 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200510 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200511
512 // Compute width based on longest text line and the 'wrap' option.
513 // TODO: more accurate wrapping
514 wp->w_width = 0;
515 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
516 {
517 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
518
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200519 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200520 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200521 while (len > maxwidth)
522 {
523 ++wrapped;
524 len -= maxwidth;
525 wp->w_width = maxwidth;
526 }
527 }
528 else if (len > maxwidth
529 && allow_adjust_left
530 && (wp->w_popup_pos == POPPOS_TOPLEFT
531 || wp->w_popup_pos == POPPOS_BOTLEFT))
532 {
533 // adjust leftwise to fit text on screen
534 int shift_by = ( len - maxwidth );
535
536 if ( shift_by > wp->w_wincol )
537 {
538 int truncate_shift = shift_by - wp->w_wincol;
539 len -= truncate_shift;
540 shift_by -= truncate_shift;
541 }
542
543 wp->w_wincol -= shift_by;
544 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200545 wp->w_width = maxwidth;
546 }
547 if (wp->w_width < len)
548 wp->w_width = len;
549 }
550
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200551 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
552 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200553 if (wp->w_width > maxwidth)
554 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200555 if (center_hor)
556 wp->w_wincol = (Columns - wp->w_width) / 2;
557 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
558 || wp->w_popup_pos == POPPOS_TOPRIGHT)
559 {
560 // Right aligned: move to the right if needed.
561 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200562 if (wp->w_width + extra_width < wp->w_wantcol)
563 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200564 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200565
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200566 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200567 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
568 wp->w_height = wp->w_minheight;
569 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
570 wp->w_height = wp->w_maxheight;
571 if (wp->w_height > Rows - wp->w_winrow)
572 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200573
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200574 if (center_vert)
575 wp->w_winrow = (Rows - wp->w_height) / 2;
576 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
577 || wp->w_popup_pos == POPPOS_BOTLEFT)
578 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200579 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200580 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200581 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200582 else
583 // not enough space, make top aligned
584 wp->w_winrow = wp->w_wantline + 1;
585 }
586
Bram Moolenaar17146962019-05-30 00:12:11 +0200587 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200588
589 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200590 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200591 if (org_winrow != wp->w_winrow
592 || org_wincol != wp->w_wincol
593 || org_width != wp->w_width
594 || org_height != wp->w_height)
595 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200596 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200597 popup_mask_refresh = TRUE;
598 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200599}
600
Bram Moolenaar17627312019-06-02 19:53:44 +0200601typedef enum
602{
603 TYPE_NORMAL,
604 TYPE_ATCURSOR
605} create_type_T;
606
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200607/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200608 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200609 * popup_atcursor({text}, {options})
Bram Moolenaar33796b32019-06-08 16:01:13 +0200610 * When called from f_popup_atcursor() "type" is TYPE_ATCURSOR.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200611 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200612 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200613popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200614{
615 win_T *wp;
616 buf_T *buf;
617 dict_T *d;
618 int nr;
619
620 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200621 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
622 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200623 {
624 emsg(_(e_listreq));
625 return;
626 }
627 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
628 {
629 emsg(_(e_dictreq));
630 return;
631 }
632 d = argvars[1].vval.v_dict;
633
634 // Create the window and buffer.
635 wp = win_alloc_popup_win();
636 if (wp == NULL)
637 return;
638 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200639 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200640
641 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
642 if (buf == NULL)
643 return;
644 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200645
646 win_init_popup_win(wp, buf);
647
648 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200649 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200650 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200651 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200652 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200653 buf->b_p_ul = -1; // no undo
654 buf->b_p_swf = FALSE; // no swap file
655 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200656 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200657 wp->w_p_wrap = TRUE; // 'wrap' is default on
658
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200659 // Avoid that 'buftype' is reset when this buffer is entered.
660 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200661
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200662 nr = (int)dict_get_number(d, (char_u *)"tab");
663 if (nr == 0)
664 {
665 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200666 wp->w_next = curtab->tp_first_popupwin;
667 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200668 }
669 else if (nr < 0)
670 {
671 // global popup
672 wp->w_next = first_popupwin;
673 first_popupwin = wp;
674 }
675 else
676 // TODO: find tab page "nr"
677 emsg("Not implemented yet");
678
679 // Add text to the buffer.
680 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200681 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200682 // just a string
683 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200684 }
685 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200686 {
687 list_T *l = argvars[0].vval.v_list;
688
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200689 if (l->lv_len > 0)
690 {
691 if (l->lv_first->li_tv.v_type == VAR_STRING)
692 // list of strings
693 add_popup_strings(buf, l);
694 else
695 // list of dictionaries
696 add_popup_dicts(buf, l);
697 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200698 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200699
700 // Delete the line of the empty buffer.
701 curbuf = buf;
702 ml_delete(buf->b_ml.ml_line_count, FALSE);
703 curbuf = curwin->w_buffer;
704
Bram Moolenaar17627312019-06-02 19:53:44 +0200705 if (type == TYPE_ATCURSOR)
706 {
707 wp->w_popup_pos = POPPOS_BOTLEFT;
708 setcursor_mayforce(TRUE);
709 wp->w_wantline = screen_screenrow();
710 if (wp->w_wantline == 0) // cursor in first line
711 {
712 wp->w_wantline = 2;
713 wp->w_popup_pos = POPPOS_TOPLEFT;
714 }
715 wp->w_wantcol = screen_screencol() + 1;
716 set_moved_values(wp);
717 set_moved_columns(wp, FIND_STRING);
718 }
719
Bram Moolenaar33796b32019-06-08 16:01:13 +0200720 // set default values
721 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
722
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200723 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200724 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200725
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200726 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200727
728 wp->w_vsep_width = 0;
729
730 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200731 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200732}
733
734/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200735 * popup_clear()
736 */
737 void
738f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
739{
740 close_all_popups();
741}
742
743/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200744 * popup_create({text}, {options})
745 */
746 void
747f_popup_create(typval_T *argvars, typval_T *rettv)
748{
Bram Moolenaar17627312019-06-02 19:53:44 +0200749 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200750}
751
752/*
753 * popup_atcursor({text}, {options})
754 */
755 void
756f_popup_atcursor(typval_T *argvars, typval_T *rettv)
757{
Bram Moolenaar17627312019-06-02 19:53:44 +0200758 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200759}
760
761/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200762 * Find the popup window with window-ID "id".
763 * If the popup window does not exist NULL is returned.
764 * If the window is not a popup window, and error message is given.
765 */
766 static win_T *
767find_popup_win(int id)
768{
769 win_T *wp = win_id2wp(id);
770
771 if (wp != NULL && !bt_popup(wp->w_buffer))
772 {
773 semsg(_("E993: window %d is not a popup window"), id);
774 return NULL;
775 }
776 return wp;
777}
778
779/*
780 * Return TRUE if there any popups that are not hidden.
781 */
782 int
783popup_any_visible(void)
784{
785 win_T *wp;
786
787 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200788 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200789 return TRUE;
790 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200791 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200792 return TRUE;
793 return FALSE;
794}
795
796/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200797 * Invoke the close callback for window "wp" with value "result".
798 * Careful: The callback may make "wp" invalid!
799 */
800 static void
801invoke_popup_callback(win_T *wp, typval_T *result)
802{
803 typval_T rettv;
804 int dummy;
805 typval_T argv[3];
806
807 argv[0].v_type = VAR_NUMBER;
808 argv[0].vval.v_number = (varnumber_T)wp->w_id;
809
810 if (result != NULL && result->v_type != VAR_UNKNOWN)
811 copy_tv(result, &argv[1]);
812 else
813 {
814 argv[1].v_type = VAR_NUMBER;
815 argv[1].vval.v_number = 0;
816 }
817
818 argv[2].v_type = VAR_UNKNOWN;
819
820 call_callback(&wp->w_close_cb, -1,
821 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
822 if (result != NULL)
823 clear_tv(&argv[1]);
824 clear_tv(&rettv);
825}
826
827/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200828 * Close popup "wp" and invoke any close callback for it.
829 */
830 static void
831popup_close_and_callback(win_T *wp, typval_T *arg)
832{
833 int id = wp->w_id;
834
835 if (wp->w_close_cb.cb_name != NULL)
836 // Careful: This may make "wp" invalid.
837 invoke_popup_callback(wp, arg);
838
839 popup_close(id);
840}
841
842/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200843 * popup_close({id})
844 */
845 void
846f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
847{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200848 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200849 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200850
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200851 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +0200852 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200853}
854
855/*
856 * popup_hide({id})
857 */
858 void
859f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
860{
861 int id = (int)tv_get_number(argvars);
862 win_T *wp = find_popup_win(id);
863
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200864 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200865 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200866 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200867 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200868 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200869 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200870 }
871}
872
873/*
874 * popup_show({id})
875 */
876 void
877f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
878{
879 int id = (int)tv_get_number(argvars);
880 win_T *wp = find_popup_win(id);
881
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200882 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200883 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200884 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200885 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200886 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200887 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200888 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200889}
890
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200891 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200892popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200893{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200894 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200895 if (wp->w_winrow + wp->w_height >= cmdline_row)
896 clear_cmdline = TRUE;
897 win_free_popup(wp);
898 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200899 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200900}
901
Bram Moolenaarec583842019-05-26 14:11:23 +0200902/*
903 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200904 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +0200905 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200906 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200907popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200908{
909 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200910 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200911 win_T *prev = NULL;
912
Bram Moolenaarec583842019-05-26 14:11:23 +0200913 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200914 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200915 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200916 {
917 if (prev == NULL)
918 first_popupwin = wp->w_next;
919 else
920 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200921 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200922 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200923 }
924
Bram Moolenaarec583842019-05-26 14:11:23 +0200925 // go through tab-local popups
926 FOR_ALL_TABPAGES(tp)
927 popup_close_tabpage(tp, id);
928}
929
930/*
931 * Close a popup window with Window-id "id" in tabpage "tp".
932 */
933 void
934popup_close_tabpage(tabpage_T *tp, int id)
935{
936 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200937 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200938 win_T *prev = NULL;
939
Bram Moolenaarec583842019-05-26 14:11:23 +0200940 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
941 if (wp->w_id == id)
942 {
943 if (prev == NULL)
944 *root = wp->w_next;
945 else
946 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200947 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200948 return;
949 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200950}
951
952 void
953close_all_popups(void)
954{
955 while (first_popupwin != NULL)
956 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200957 while (curtab->tp_first_popupwin != NULL)
958 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200959}
960
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200961/*
962 * popup_move({id}, {options})
963 */
964 void
965f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
966{
967 dict_T *d;
968 int nr;
969 int id = (int)tv_get_number(argvars);
970 win_T *wp = find_popup_win(id);
971
972 if (wp == NULL)
973 return; // invalid {id}
974
975 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
976 {
977 emsg(_(e_dictreq));
978 return;
979 }
980 d = argvars[1].vval.v_dict;
981
982 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
983 wp->w_minwidth = nr;
984 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
985 wp->w_minheight = nr;
986 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
987 wp->w_maxwidth = nr;
988 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
989 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200990 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200991
992 if (wp->w_winrow + wp->w_height >= cmdline_row)
993 clear_cmdline = TRUE;
994 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200995}
996
Bram Moolenaarbc133542019-05-29 20:26:46 +0200997/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200998 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200999 */
1000 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001001f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001002{
1003 dict_T *dict;
1004 int id = (int)tv_get_number(argvars);
1005 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001006 int top_extra;
1007 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001008
1009 if (rettv_dict_alloc(rettv) == OK)
1010 {
1011 if (wp == NULL)
1012 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001013 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1014 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1015
Bram Moolenaarbc133542019-05-29 20:26:46 +02001016 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001017
Bram Moolenaarbc133542019-05-29 20:26:46 +02001018 dict_add_number(dict, "line", wp->w_winrow + 1);
1019 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001020 dict_add_number(dict, "width", wp->w_width + left_extra + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1021 dict_add_number(dict, "height", wp->w_height + top_extra + wp->w_popup_border[2] + wp->w_popup_padding[2]);
1022
1023 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1024 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1025 dict_add_number(dict, "core_width", wp->w_width);
1026 dict_add_number(dict, "core_height", wp->w_height);
1027
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001028 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001029 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001030 }
1031}
1032
1033/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001034 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001035 */
1036 void
1037f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1038{
1039 dict_T *dict;
1040 int id = (int)tv_get_number(argvars);
1041 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001042 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001043
1044 if (rettv_dict_alloc(rettv) == OK)
1045 {
1046 if (wp == NULL)
1047 return;
1048
1049 dict = rettv->vval.v_dict;
1050 dict_add_number(dict, "line", wp->w_wantline);
1051 dict_add_number(dict, "col", wp->w_wantcol);
1052 dict_add_number(dict, "minwidth", wp->w_minwidth);
1053 dict_add_number(dict, "minheight", wp->w_minheight);
1054 dict_add_number(dict, "maxheight", wp->w_maxheight);
1055 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
1056 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001057 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001058
1059 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1060 ++i)
1061 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1062 {
1063 dict_add_string(dict, "pos",
1064 (char_u *)poppos_entries[i].pp_name);
1065 break;
1066 }
1067
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001068# if defined(FEAT_TIMERS)
1069 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1070 ? (long)wp->w_popup_timer->tr_interval : 0L);
1071# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001072 }
1073}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001074
1075 int
1076not_in_popup_window()
1077{
1078 if (bt_popup(curwin->w_buffer))
1079 {
1080 emsg(_("E994: Not allowed in a popup window"));
1081 return TRUE;
1082 }
1083 return FALSE;
1084}
1085
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001086/*
1087 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
1088 * in the current tab.
1089 */
1090 void
1091popup_reset_handled()
1092{
1093 win_T *wp;
1094
1095 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1096 wp->w_popup_flags &= ~POPF_HANDLED;
1097 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1098 wp->w_popup_flags &= ~POPF_HANDLED;
1099}
1100
1101/*
1102 * Find the next visible popup where POPF_HANDLED is not set.
1103 * Must have called popup_reset_handled() first.
1104 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1105 * popup with the highest zindex.
1106 */
1107 win_T *
1108find_next_popup(int lowest)
1109{
1110 win_T *wp;
1111 win_T *found_wp;
1112 int found_zindex;
1113
1114 found_zindex = lowest ? INT_MAX : 0;
1115 found_wp = NULL;
1116 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1117 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1118 && (lowest ? wp->w_zindex < found_zindex
1119 : wp->w_zindex > found_zindex))
1120 {
1121 found_zindex = wp->w_zindex;
1122 found_wp = wp;
1123 }
1124 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1125 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1126 && (lowest ? wp->w_zindex < found_zindex
1127 : wp->w_zindex > found_zindex))
1128 {
1129 found_zindex = wp->w_zindex;
1130 found_wp = wp;
1131 }
1132
1133 if (found_wp != NULL)
1134 found_wp->w_popup_flags |= POPF_HANDLED;
1135 return found_wp;
1136}
1137
1138/*
1139 * Invoke the filter callback for window "wp" with typed character "c".
1140 * Uses the global "mod_mask" for modifiers.
1141 * Returns the return value of the filter.
1142 * Careful: The filter may make "wp" invalid!
1143 */
1144 static int
1145invoke_popup_filter(win_T *wp, int c)
1146{
1147 int res;
1148 typval_T rettv;
1149 int dummy;
1150 typval_T argv[3];
1151 char_u buf[NUMBUFLEN];
1152
1153 argv[0].v_type = VAR_NUMBER;
1154 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1155
1156 // Convert the number to a string, so that the function can use:
1157 // if a:c == "\<F2>"
1158 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1159 argv[1].v_type = VAR_STRING;
1160 argv[1].vval.v_string = vim_strsave(buf);
1161
1162 argv[2].v_type = VAR_UNKNOWN;
1163
1164 call_callback(&wp->w_filter_cb, -1,
1165 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1166 res = tv_get_number(&rettv);
1167 vim_free(argv[1].vval.v_string);
1168 clear_tv(&rettv);
1169 return res;
1170}
1171
1172/*
1173 * Called when "c" was typed: invoke popup filter callbacks.
1174 * Returns TRUE when the character was consumed,
1175 */
1176 int
1177popup_do_filter(int c)
1178{
1179 int res = FALSE;
1180 win_T *wp;
1181
1182 popup_reset_handled();
1183
1184 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1185 if (wp->w_filter_cb.cb_name != NULL)
1186 res = invoke_popup_filter(wp, c);
1187
1188 return res;
1189}
1190
Bram Moolenaar3397f742019-06-02 18:40:06 +02001191/*
1192 * Called when the cursor moved: check if any popup needs to be closed if the
1193 * cursor moved far enough.
1194 */
1195 void
1196popup_check_cursor_pos()
1197{
1198 win_T *wp;
1199 typval_T tv;
1200
1201 popup_reset_handled();
1202 while ((wp = find_next_popup(TRUE)) != NULL)
1203 if (wp->w_popup_curwin != NULL
1204 && (curwin != wp->w_popup_curwin
1205 || curwin->w_cursor.lnum != wp->w_popup_lnum
1206 || curwin->w_cursor.col < wp->w_popup_mincol
1207 || curwin->w_cursor.col > wp->w_popup_maxcol))
1208 {
1209 tv.v_type = VAR_NUMBER;
1210 tv.vval.v_number = -1;
1211 popup_close_and_callback(wp, &tv);
1212 }
1213}
1214
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001215#endif // FEAT_TEXT_PROP