blob: c5123697d1912d2f3a9a7df7481ac0914f004b43 [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
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200544 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200545 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
546 wp->w_height = wp->w_minheight;
547 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
548 wp->w_height = wp->w_maxheight;
549 if (wp->w_height > Rows - wp->w_winrow)
550 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200551
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200552 if (center_vert)
553 wp->w_winrow = (Rows - wp->w_height) / 2;
554 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
555 || wp->w_popup_pos == POPPOS_BOTLEFT)
556 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200557 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200558 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200559 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200560 else
561 // not enough space, make top aligned
562 wp->w_winrow = wp->w_wantline + 1;
563 }
564
Bram Moolenaar17146962019-05-30 00:12:11 +0200565 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200566
567 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200568 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200569 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 {
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200574 // TODO: redraw only windows that were below the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200575 redraw_all_later(NOT_VALID);
576 popup_mask_refresh = TRUE;
577 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200578}
579
Bram Moolenaar17627312019-06-02 19:53:44 +0200580typedef enum
581{
582 TYPE_NORMAL,
583 TYPE_ATCURSOR
584} create_type_T;
585
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200586/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200587 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200588 * popup_atcursor({text}, {options})
Bram Moolenaar33796b32019-06-08 16:01:13 +0200589 * When called from f_popup_atcursor() "type" is TYPE_ATCURSOR.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200590 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200591 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200592popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200593{
594 win_T *wp;
595 buf_T *buf;
596 dict_T *d;
597 int nr;
598
599 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200600 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
601 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200602 {
603 emsg(_(e_listreq));
604 return;
605 }
606 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
607 {
608 emsg(_(e_dictreq));
609 return;
610 }
611 d = argvars[1].vval.v_dict;
612
613 // Create the window and buffer.
614 wp = win_alloc_popup_win();
615 if (wp == NULL)
616 return;
617 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200618 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200619
620 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
621 if (buf == NULL)
622 return;
623 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200624
625 win_init_popup_win(wp, buf);
626
627 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200628 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200629 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200630 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200631 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200632 buf->b_p_ul = -1; // no undo
633 buf->b_p_swf = FALSE; // no swap file
634 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200635 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200636 wp->w_p_wrap = TRUE; // 'wrap' is default on
637
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200638 // Avoid that 'buftype' is reset when this buffer is entered.
639 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200640
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200641 nr = (int)dict_get_number(d, (char_u *)"tab");
642 if (nr == 0)
643 {
644 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200645 wp->w_next = curtab->tp_first_popupwin;
646 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200647 }
648 else if (nr < 0)
649 {
650 // global popup
651 wp->w_next = first_popupwin;
652 first_popupwin = wp;
653 }
654 else
655 // TODO: find tab page "nr"
656 emsg("Not implemented yet");
657
658 // Add text to the buffer.
659 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200660 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200661 // just a string
662 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200663 }
664 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200665 {
666 list_T *l = argvars[0].vval.v_list;
667
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200668 if (l->lv_len > 0)
669 {
670 if (l->lv_first->li_tv.v_type == VAR_STRING)
671 // list of strings
672 add_popup_strings(buf, l);
673 else
674 // list of dictionaries
675 add_popup_dicts(buf, l);
676 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200677 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200678
679 // Delete the line of the empty buffer.
680 curbuf = buf;
681 ml_delete(buf->b_ml.ml_line_count, FALSE);
682 curbuf = curwin->w_buffer;
683
Bram Moolenaar17627312019-06-02 19:53:44 +0200684 if (type == TYPE_ATCURSOR)
685 {
686 wp->w_popup_pos = POPPOS_BOTLEFT;
687 setcursor_mayforce(TRUE);
688 wp->w_wantline = screen_screenrow();
689 if (wp->w_wantline == 0) // cursor in first line
690 {
691 wp->w_wantline = 2;
692 wp->w_popup_pos = POPPOS_TOPLEFT;
693 }
694 wp->w_wantcol = screen_screencol() + 1;
695 set_moved_values(wp);
696 set_moved_columns(wp, FIND_STRING);
697 }
698
Bram Moolenaar33796b32019-06-08 16:01:13 +0200699 // set default values
700 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
701
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200702 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200703 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200704
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200705 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200706
707 wp->w_vsep_width = 0;
708
709 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200710 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200711}
712
713/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200714 * popup_clear()
715 */
716 void
717f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
718{
719 close_all_popups();
720}
721
722/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200723 * popup_create({text}, {options})
724 */
725 void
726f_popup_create(typval_T *argvars, typval_T *rettv)
727{
Bram Moolenaar17627312019-06-02 19:53:44 +0200728 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200729}
730
731/*
732 * popup_atcursor({text}, {options})
733 */
734 void
735f_popup_atcursor(typval_T *argvars, typval_T *rettv)
736{
Bram Moolenaar17627312019-06-02 19:53:44 +0200737 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200738}
739
740/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200741 * Find the popup window with window-ID "id".
742 * If the popup window does not exist NULL is returned.
743 * If the window is not a popup window, and error message is given.
744 */
745 static win_T *
746find_popup_win(int id)
747{
748 win_T *wp = win_id2wp(id);
749
750 if (wp != NULL && !bt_popup(wp->w_buffer))
751 {
752 semsg(_("E993: window %d is not a popup window"), id);
753 return NULL;
754 }
755 return wp;
756}
757
758/*
759 * Return TRUE if there any popups that are not hidden.
760 */
761 int
762popup_any_visible(void)
763{
764 win_T *wp;
765
766 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200767 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200768 return TRUE;
769 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200770 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200771 return TRUE;
772 return FALSE;
773}
774
775/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200776 * Invoke the close callback for window "wp" with value "result".
777 * Careful: The callback may make "wp" invalid!
778 */
779 static void
780invoke_popup_callback(win_T *wp, typval_T *result)
781{
782 typval_T rettv;
783 int dummy;
784 typval_T argv[3];
785
786 argv[0].v_type = VAR_NUMBER;
787 argv[0].vval.v_number = (varnumber_T)wp->w_id;
788
789 if (result != NULL && result->v_type != VAR_UNKNOWN)
790 copy_tv(result, &argv[1]);
791 else
792 {
793 argv[1].v_type = VAR_NUMBER;
794 argv[1].vval.v_number = 0;
795 }
796
797 argv[2].v_type = VAR_UNKNOWN;
798
799 call_callback(&wp->w_close_cb, -1,
800 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
801 if (result != NULL)
802 clear_tv(&argv[1]);
803 clear_tv(&rettv);
804}
805
806/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200807 * Close popup "wp" and invoke any close callback for it.
808 */
809 static void
810popup_close_and_callback(win_T *wp, typval_T *arg)
811{
812 int id = wp->w_id;
813
814 if (wp->w_close_cb.cb_name != NULL)
815 // Careful: This may make "wp" invalid.
816 invoke_popup_callback(wp, arg);
817
818 popup_close(id);
819}
820
821/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200822 * popup_close({id})
823 */
824 void
825f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
826{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200827 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200828 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200829
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200830 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +0200831 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200832}
833
834/*
835 * popup_hide({id})
836 */
837 void
838f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
839{
840 int id = (int)tv_get_number(argvars);
841 win_T *wp = find_popup_win(id);
842
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200843 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200844 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200845 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200846 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200847 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200848 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200849 }
850}
851
852/*
853 * popup_show({id})
854 */
855 void
856f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
857{
858 int id = (int)tv_get_number(argvars);
859 win_T *wp = find_popup_win(id);
860
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200861 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200862 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200863 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200864 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200865 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200866 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200867 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200868}
869
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200870 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200871popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200872{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200873 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200874 if (wp->w_winrow + wp->w_height >= cmdline_row)
875 clear_cmdline = TRUE;
876 win_free_popup(wp);
877 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200878 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200879}
880
Bram Moolenaarec583842019-05-26 14:11:23 +0200881/*
882 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200883 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +0200884 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200885 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200886popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200887{
888 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200889 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200890 win_T *prev = NULL;
891
Bram Moolenaarec583842019-05-26 14:11:23 +0200892 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200893 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200894 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200895 {
896 if (prev == NULL)
897 first_popupwin = wp->w_next;
898 else
899 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200900 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200901 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200902 }
903
Bram Moolenaarec583842019-05-26 14:11:23 +0200904 // go through tab-local popups
905 FOR_ALL_TABPAGES(tp)
906 popup_close_tabpage(tp, id);
907}
908
909/*
910 * Close a popup window with Window-id "id" in tabpage "tp".
911 */
912 void
913popup_close_tabpage(tabpage_T *tp, int id)
914{
915 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200916 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200917 win_T *prev = NULL;
918
Bram Moolenaarec583842019-05-26 14:11:23 +0200919 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
920 if (wp->w_id == id)
921 {
922 if (prev == NULL)
923 *root = wp->w_next;
924 else
925 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200926 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200927 return;
928 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200929}
930
931 void
932close_all_popups(void)
933{
934 while (first_popupwin != NULL)
935 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200936 while (curtab->tp_first_popupwin != NULL)
937 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200938}
939
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200940/*
941 * popup_move({id}, {options})
942 */
943 void
944f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
945{
946 dict_T *d;
947 int nr;
948 int id = (int)tv_get_number(argvars);
949 win_T *wp = find_popup_win(id);
950
951 if (wp == NULL)
952 return; // invalid {id}
953
954 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
955 {
956 emsg(_(e_dictreq));
957 return;
958 }
959 d = argvars[1].vval.v_dict;
960
961 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
962 wp->w_minwidth = nr;
963 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
964 wp->w_minheight = nr;
965 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
966 wp->w_maxwidth = nr;
967 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
968 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200969 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200970
971 if (wp->w_winrow + wp->w_height >= cmdline_row)
972 clear_cmdline = TRUE;
973 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200974}
975
Bram Moolenaarbc133542019-05-29 20:26:46 +0200976/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200977 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200978 */
979 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200980f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200981{
982 dict_T *dict;
983 int id = (int)tv_get_number(argvars);
984 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200985 int top_extra;
986 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +0200987
988 if (rettv_dict_alloc(rettv) == OK)
989 {
990 if (wp == NULL)
991 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200992 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
993 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
994
Bram Moolenaarbc133542019-05-29 20:26:46 +0200995 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200996
Bram Moolenaarbc133542019-05-29 20:26:46 +0200997 dict_add_number(dict, "line", wp->w_winrow + 1);
998 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200999 dict_add_number(dict, "width", wp->w_width + left_extra + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1000 dict_add_number(dict, "height", wp->w_height + top_extra + wp->w_popup_border[2] + wp->w_popup_padding[2]);
1001
1002 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1003 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1004 dict_add_number(dict, "core_width", wp->w_width);
1005 dict_add_number(dict, "core_height", wp->w_height);
1006
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001007 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001008 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001009 }
1010}
1011
1012/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001013 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001014 */
1015 void
1016f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1017{
1018 dict_T *dict;
1019 int id = (int)tv_get_number(argvars);
1020 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001021 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001022
1023 if (rettv_dict_alloc(rettv) == OK)
1024 {
1025 if (wp == NULL)
1026 return;
1027
1028 dict = rettv->vval.v_dict;
1029 dict_add_number(dict, "line", wp->w_wantline);
1030 dict_add_number(dict, "col", wp->w_wantcol);
1031 dict_add_number(dict, "minwidth", wp->w_minwidth);
1032 dict_add_number(dict, "minheight", wp->w_minheight);
1033 dict_add_number(dict, "maxheight", wp->w_maxheight);
1034 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
1035 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001036 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001037
1038 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1039 ++i)
1040 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1041 {
1042 dict_add_string(dict, "pos",
1043 (char_u *)poppos_entries[i].pp_name);
1044 break;
1045 }
1046
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001047# if defined(FEAT_TIMERS)
1048 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1049 ? (long)wp->w_popup_timer->tr_interval : 0L);
1050# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001051 }
1052}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001053
1054 int
1055not_in_popup_window()
1056{
1057 if (bt_popup(curwin->w_buffer))
1058 {
1059 emsg(_("E994: Not allowed in a popup window"));
1060 return TRUE;
1061 }
1062 return FALSE;
1063}
1064
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001065/*
1066 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
1067 * in the current tab.
1068 */
1069 void
1070popup_reset_handled()
1071{
1072 win_T *wp;
1073
1074 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1075 wp->w_popup_flags &= ~POPF_HANDLED;
1076 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1077 wp->w_popup_flags &= ~POPF_HANDLED;
1078}
1079
1080/*
1081 * Find the next visible popup where POPF_HANDLED is not set.
1082 * Must have called popup_reset_handled() first.
1083 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1084 * popup with the highest zindex.
1085 */
1086 win_T *
1087find_next_popup(int lowest)
1088{
1089 win_T *wp;
1090 win_T *found_wp;
1091 int found_zindex;
1092
1093 found_zindex = lowest ? INT_MAX : 0;
1094 found_wp = NULL;
1095 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1096 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1097 && (lowest ? wp->w_zindex < found_zindex
1098 : wp->w_zindex > found_zindex))
1099 {
1100 found_zindex = wp->w_zindex;
1101 found_wp = wp;
1102 }
1103 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1104 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1105 && (lowest ? wp->w_zindex < found_zindex
1106 : wp->w_zindex > found_zindex))
1107 {
1108 found_zindex = wp->w_zindex;
1109 found_wp = wp;
1110 }
1111
1112 if (found_wp != NULL)
1113 found_wp->w_popup_flags |= POPF_HANDLED;
1114 return found_wp;
1115}
1116
1117/*
1118 * Invoke the filter callback for window "wp" with typed character "c".
1119 * Uses the global "mod_mask" for modifiers.
1120 * Returns the return value of the filter.
1121 * Careful: The filter may make "wp" invalid!
1122 */
1123 static int
1124invoke_popup_filter(win_T *wp, int c)
1125{
1126 int res;
1127 typval_T rettv;
1128 int dummy;
1129 typval_T argv[3];
1130 char_u buf[NUMBUFLEN];
1131
1132 argv[0].v_type = VAR_NUMBER;
1133 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1134
1135 // Convert the number to a string, so that the function can use:
1136 // if a:c == "\<F2>"
1137 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1138 argv[1].v_type = VAR_STRING;
1139 argv[1].vval.v_string = vim_strsave(buf);
1140
1141 argv[2].v_type = VAR_UNKNOWN;
1142
1143 call_callback(&wp->w_filter_cb, -1,
1144 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1145 res = tv_get_number(&rettv);
1146 vim_free(argv[1].vval.v_string);
1147 clear_tv(&rettv);
1148 return res;
1149}
1150
1151/*
1152 * Called when "c" was typed: invoke popup filter callbacks.
1153 * Returns TRUE when the character was consumed,
1154 */
1155 int
1156popup_do_filter(int c)
1157{
1158 int res = FALSE;
1159 win_T *wp;
1160
1161 popup_reset_handled();
1162
1163 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1164 if (wp->w_filter_cb.cb_name != NULL)
1165 res = invoke_popup_filter(wp, c);
1166
1167 return res;
1168}
1169
Bram Moolenaar3397f742019-06-02 18:40:06 +02001170/*
1171 * Called when the cursor moved: check if any popup needs to be closed if the
1172 * cursor moved far enough.
1173 */
1174 void
1175popup_check_cursor_pos()
1176{
1177 win_T *wp;
1178 typval_T tv;
1179
1180 popup_reset_handled();
1181 while ((wp = find_next_popup(TRUE)) != NULL)
1182 if (wp->w_popup_curwin != NULL
1183 && (curwin != wp->w_popup_curwin
1184 || curwin->w_cursor.lnum != wp->w_popup_lnum
1185 || curwin->w_cursor.col < wp->w_popup_mincol
1186 || curwin->w_cursor.col > wp->w_popup_maxcol))
1187 {
1188 tv.v_type = VAR_NUMBER;
1189 tv.vval.v_number = -1;
1190 popup_close_and_callback(wp, &tv);
1191 }
1192}
1193
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001194#endif // FEAT_TEXT_PROP