blob: 000bed9b70dcb67388546603f722d8d80f4fd6c2 [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 Moolenaarcc31ad92019-05-30 19:25:06 +0200714 * popup_create({text}, {options})
715 */
716 void
717f_popup_create(typval_T *argvars, typval_T *rettv)
718{
Bram Moolenaar17627312019-06-02 19:53:44 +0200719 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200720}
721
722/*
723 * popup_atcursor({text}, {options})
724 */
725 void
726f_popup_atcursor(typval_T *argvars, typval_T *rettv)
727{
Bram Moolenaar17627312019-06-02 19:53:44 +0200728 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200729}
730
731/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200732 * Find the popup window with window-ID "id".
733 * If the popup window does not exist NULL is returned.
734 * If the window is not a popup window, and error message is given.
735 */
736 static win_T *
737find_popup_win(int id)
738{
739 win_T *wp = win_id2wp(id);
740
741 if (wp != NULL && !bt_popup(wp->w_buffer))
742 {
743 semsg(_("E993: window %d is not a popup window"), id);
744 return NULL;
745 }
746 return wp;
747}
748
749/*
750 * Return TRUE if there any popups that are not hidden.
751 */
752 int
753popup_any_visible(void)
754{
755 win_T *wp;
756
757 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200758 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200759 return TRUE;
760 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200761 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200762 return TRUE;
763 return FALSE;
764}
765
766/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200767 * Invoke the close callback for window "wp" with value "result".
768 * Careful: The callback may make "wp" invalid!
769 */
770 static void
771invoke_popup_callback(win_T *wp, typval_T *result)
772{
773 typval_T rettv;
774 int dummy;
775 typval_T argv[3];
776
777 argv[0].v_type = VAR_NUMBER;
778 argv[0].vval.v_number = (varnumber_T)wp->w_id;
779
780 if (result != NULL && result->v_type != VAR_UNKNOWN)
781 copy_tv(result, &argv[1]);
782 else
783 {
784 argv[1].v_type = VAR_NUMBER;
785 argv[1].vval.v_number = 0;
786 }
787
788 argv[2].v_type = VAR_UNKNOWN;
789
790 call_callback(&wp->w_close_cb, -1,
791 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
792 if (result != NULL)
793 clear_tv(&argv[1]);
794 clear_tv(&rettv);
795}
796
797/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200798 * Close popup "wp" and invoke any close callback for it.
799 */
800 static void
801popup_close_and_callback(win_T *wp, typval_T *arg)
802{
803 int id = wp->w_id;
804
805 if (wp->w_close_cb.cb_name != NULL)
806 // Careful: This may make "wp" invalid.
807 invoke_popup_callback(wp, arg);
808
809 popup_close(id);
810}
811
812/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813 * popup_close({id})
814 */
815 void
816f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
817{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200818 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200819 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200820
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200821 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +0200822 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200823}
824
825/*
826 * popup_hide({id})
827 */
828 void
829f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
830{
831 int id = (int)tv_get_number(argvars);
832 win_T *wp = find_popup_win(id);
833
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200834 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200835 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200836 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200837 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200838 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200839 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200840 }
841}
842
843/*
844 * popup_show({id})
845 */
846 void
847f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
848{
849 int id = (int)tv_get_number(argvars);
850 win_T *wp = find_popup_win(id);
851
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200852 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200853 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200854 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200855 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200856 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200857 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200858 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200859}
860
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200861 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200862popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200863{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200864 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200865 if (wp->w_winrow + wp->w_height >= cmdline_row)
866 clear_cmdline = TRUE;
867 win_free_popup(wp);
868 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200869 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200870}
871
Bram Moolenaarec583842019-05-26 14:11:23 +0200872/*
873 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200874 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +0200875 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200876 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200877popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200878{
879 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200880 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200881 win_T *prev = NULL;
882
Bram Moolenaarec583842019-05-26 14:11:23 +0200883 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200884 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200885 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200886 {
887 if (prev == NULL)
888 first_popupwin = wp->w_next;
889 else
890 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200891 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200892 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200893 }
894
Bram Moolenaarec583842019-05-26 14:11:23 +0200895 // go through tab-local popups
896 FOR_ALL_TABPAGES(tp)
897 popup_close_tabpage(tp, id);
898}
899
900/*
901 * Close a popup window with Window-id "id" in tabpage "tp".
902 */
903 void
904popup_close_tabpage(tabpage_T *tp, int id)
905{
906 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200907 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200908 win_T *prev = NULL;
909
Bram Moolenaarec583842019-05-26 14:11:23 +0200910 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
911 if (wp->w_id == id)
912 {
913 if (prev == NULL)
914 *root = wp->w_next;
915 else
916 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200917 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200918 return;
919 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200920}
921
922 void
923close_all_popups(void)
924{
925 while (first_popupwin != NULL)
926 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200927 while (curtab->tp_first_popupwin != NULL)
928 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200929}
930
931 void
932ex_popupclear(exarg_T *eap UNUSED)
933{
934 close_all_popups();
935}
936
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200937/*
938 * popup_move({id}, {options})
939 */
940 void
941f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
942{
943 dict_T *d;
944 int nr;
945 int id = (int)tv_get_number(argvars);
946 win_T *wp = find_popup_win(id);
947
948 if (wp == NULL)
949 return; // invalid {id}
950
951 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
952 {
953 emsg(_(e_dictreq));
954 return;
955 }
956 d = argvars[1].vval.v_dict;
957
958 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
959 wp->w_minwidth = nr;
960 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
961 wp->w_minheight = nr;
962 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
963 wp->w_maxwidth = nr;
964 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
965 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200966 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200967
968 if (wp->w_winrow + wp->w_height >= cmdline_row)
969 clear_cmdline = TRUE;
970 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200971}
972
Bram Moolenaarbc133542019-05-29 20:26:46 +0200973/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200974 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200975 */
976 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200977f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200978{
979 dict_T *dict;
980 int id = (int)tv_get_number(argvars);
981 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200982 int top_extra;
983 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +0200984
985 if (rettv_dict_alloc(rettv) == OK)
986 {
987 if (wp == NULL)
988 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200989 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
990 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
991
Bram Moolenaarbc133542019-05-29 20:26:46 +0200992 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200993
Bram Moolenaarbc133542019-05-29 20:26:46 +0200994 dict_add_number(dict, "line", wp->w_winrow + 1);
995 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200996 dict_add_number(dict, "width", wp->w_width + left_extra + wp->w_popup_border[1] + wp->w_popup_padding[1]);
997 dict_add_number(dict, "height", wp->w_height + top_extra + wp->w_popup_border[2] + wp->w_popup_padding[2]);
998
999 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1000 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1001 dict_add_number(dict, "core_width", wp->w_width);
1002 dict_add_number(dict, "core_height", wp->w_height);
1003
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001004 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001005 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001006 }
1007}
1008
1009/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001010 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001011 */
1012 void
1013f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1014{
1015 dict_T *dict;
1016 int id = (int)tv_get_number(argvars);
1017 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001018 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001019
1020 if (rettv_dict_alloc(rettv) == OK)
1021 {
1022 if (wp == NULL)
1023 return;
1024
1025 dict = rettv->vval.v_dict;
1026 dict_add_number(dict, "line", wp->w_wantline);
1027 dict_add_number(dict, "col", wp->w_wantcol);
1028 dict_add_number(dict, "minwidth", wp->w_minwidth);
1029 dict_add_number(dict, "minheight", wp->w_minheight);
1030 dict_add_number(dict, "maxheight", wp->w_maxheight);
1031 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
1032 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001033 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001034
1035 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1036 ++i)
1037 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1038 {
1039 dict_add_string(dict, "pos",
1040 (char_u *)poppos_entries[i].pp_name);
1041 break;
1042 }
1043
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001044# if defined(FEAT_TIMERS)
1045 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1046 ? (long)wp->w_popup_timer->tr_interval : 0L);
1047# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001048 }
1049}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001050
1051 int
1052not_in_popup_window()
1053{
1054 if (bt_popup(curwin->w_buffer))
1055 {
1056 emsg(_("E994: Not allowed in a popup window"));
1057 return TRUE;
1058 }
1059 return FALSE;
1060}
1061
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001062/*
1063 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
1064 * in the current tab.
1065 */
1066 void
1067popup_reset_handled()
1068{
1069 win_T *wp;
1070
1071 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1072 wp->w_popup_flags &= ~POPF_HANDLED;
1073 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1074 wp->w_popup_flags &= ~POPF_HANDLED;
1075}
1076
1077/*
1078 * Find the next visible popup where POPF_HANDLED is not set.
1079 * Must have called popup_reset_handled() first.
1080 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1081 * popup with the highest zindex.
1082 */
1083 win_T *
1084find_next_popup(int lowest)
1085{
1086 win_T *wp;
1087 win_T *found_wp;
1088 int found_zindex;
1089
1090 found_zindex = lowest ? INT_MAX : 0;
1091 found_wp = NULL;
1092 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1093 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1094 && (lowest ? wp->w_zindex < found_zindex
1095 : wp->w_zindex > found_zindex))
1096 {
1097 found_zindex = wp->w_zindex;
1098 found_wp = wp;
1099 }
1100 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1101 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1102 && (lowest ? wp->w_zindex < found_zindex
1103 : wp->w_zindex > found_zindex))
1104 {
1105 found_zindex = wp->w_zindex;
1106 found_wp = wp;
1107 }
1108
1109 if (found_wp != NULL)
1110 found_wp->w_popup_flags |= POPF_HANDLED;
1111 return found_wp;
1112}
1113
1114/*
1115 * Invoke the filter callback for window "wp" with typed character "c".
1116 * Uses the global "mod_mask" for modifiers.
1117 * Returns the return value of the filter.
1118 * Careful: The filter may make "wp" invalid!
1119 */
1120 static int
1121invoke_popup_filter(win_T *wp, int c)
1122{
1123 int res;
1124 typval_T rettv;
1125 int dummy;
1126 typval_T argv[3];
1127 char_u buf[NUMBUFLEN];
1128
1129 argv[0].v_type = VAR_NUMBER;
1130 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1131
1132 // Convert the number to a string, so that the function can use:
1133 // if a:c == "\<F2>"
1134 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1135 argv[1].v_type = VAR_STRING;
1136 argv[1].vval.v_string = vim_strsave(buf);
1137
1138 argv[2].v_type = VAR_UNKNOWN;
1139
1140 call_callback(&wp->w_filter_cb, -1,
1141 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1142 res = tv_get_number(&rettv);
1143 vim_free(argv[1].vval.v_string);
1144 clear_tv(&rettv);
1145 return res;
1146}
1147
1148/*
1149 * Called when "c" was typed: invoke popup filter callbacks.
1150 * Returns TRUE when the character was consumed,
1151 */
1152 int
1153popup_do_filter(int c)
1154{
1155 int res = FALSE;
1156 win_T *wp;
1157
1158 popup_reset_handled();
1159
1160 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1161 if (wp->w_filter_cb.cb_name != NULL)
1162 res = invoke_popup_filter(wp, c);
1163
1164 return res;
1165}
1166
Bram Moolenaar3397f742019-06-02 18:40:06 +02001167/*
1168 * Called when the cursor moved: check if any popup needs to be closed if the
1169 * cursor moved far enough.
1170 */
1171 void
1172popup_check_cursor_pos()
1173{
1174 win_T *wp;
1175 typval_T tv;
1176
1177 popup_reset_handled();
1178 while ((wp = find_next_popup(TRUE)) != NULL)
1179 if (wp->w_popup_curwin != NULL
1180 && (curwin != wp->w_popup_curwin
1181 || curwin->w_cursor.lnum != wp->w_popup_lnum
1182 || curwin->w_cursor.col < wp->w_popup_mincol
1183 || curwin->w_cursor.col > wp->w_popup_maxcol))
1184 {
1185 tv.v_type = VAR_NUMBER;
1186 tv.vval.v_number = -1;
1187 popup_close_and_callback(wp, &tv);
1188 }
1189}
1190
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001191#endif // FEAT_TEXT_PROP