blob: 8052ca50ce9f32ac137c5a6a389f21c33e524b7c [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 Moolenaarcc31ad92019-05-30 19:25:06 +020032 * Get option value for"key", which is "line" or "col".
33 * 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)
50 return dict_get_number(dict, key);
51
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
56 n = strtol((char *)s, (char **)&endp, 10);
57 if (endp != NULL && *skipwhite(endp) != NUL)
58 {
59 semsg(_(e_invexpr2), val);
60 return 0;
61 }
62 }
63
64 if (STRCMP(key, "line") == 0)
65 n = screen_screenrow() + 1 + n;
66 else // "col"
67 n = screen_screencol() + 1 + n;
68
69 if (n < 1)
70 n = 1;
71 return n;
72}
73
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020074 static void
75get_pos_options(win_T *wp, dict_T *dict)
76{
77 char_u *str;
78 int nr;
79
80 nr = popup_options_one(dict, (char_u *)"line");
81 if (nr > 0)
82 wp->w_wantline = nr;
83 nr = popup_options_one(dict, (char_u *)"col");
84 if (nr > 0)
85 wp->w_wantcol = nr;
86
87 str = dict_get_string(dict, (char_u *)"pos", FALSE);
88 if (str != NULL)
89 {
90 for (nr = 0;
91 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
92 ++nr)
93 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
94 {
95 wp->w_popup_pos = poppos_entries[nr].pp_val;
96 nr = -1;
97 break;
98 }
99 if (nr != -1)
100 semsg(_(e_invarg2), str);
101 }
102}
103
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200104 static void
105get_padding_border(dict_T *dict, int *array, char *name, int max_val)
106{
107 dictitem_T *di;
108
109 vim_memset(array, 0, sizeof(int) * 4);
110 di = dict_find(dict, (char_u *)name, -1);
111 if (di != NULL)
112 {
113 if (di->di_tv.v_type != VAR_LIST)
114 emsg(_(e_listreq));
115 else
116 {
117 list_T *list = di->di_tv.vval.v_list;
118 listitem_T *li;
119 int i;
120 int nr;
121
122 for (i = 0; i < 4; ++i)
123 array[i] = 1;
124 if (list != NULL)
125 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
126 ++i, li = li->li_next)
127 {
128 nr = (int)tv_get_number(&li->li_tv);
129 if (nr >= 0)
130 array[i] = nr > max_val ? max_val : nr;
131 }
132 }
133 }
134}
135
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200136/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200137 * Go through the options in "dict" and apply them to buffer "buf" displayed in
138 * popup window "wp".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200139 * When called from f_popup_atcursor() "atcursor" is TRUE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200140 */
141 static void
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200142apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict, int atcursor)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200143{
Bram Moolenaar402502d2019-05-30 22:07:36 +0200144 int nr;
145 char_u *str;
146 dictitem_T *di;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200147
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200148 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
149 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200150 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
151 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200152
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200153 if (atcursor)
154 {
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200155 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200156 setcursor_mayforce(TRUE);
157 wp->w_wantline = screen_screenrow();
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200158 if (wp->w_wantline == 0) // cursor in first line
159 {
160 wp->w_wantline = 2;
161 wp->w_popup_pos = POPPOS_TOPLEFT;
162 }
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200163 wp->w_wantcol = screen_screencol() + 1;
164 }
165
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200166 get_pos_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200167
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200168 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200169
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200170#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200171 // Add timer to close the popup after some time.
172 nr = dict_get_number(dict, (char_u *)"time");
173 if (nr > 0)
174 {
175 char_u cbbuf[50];
176 char_u *ptr = cbbuf;
177 typval_T tv;
178
179 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
180 "{_ -> popup_close(%d)}", wp->w_id);
181 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
182 {
183 wp->w_popup_timer = create_timer(nr, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200184 wp->w_popup_timer->tr_callback = get_callback(&tv);
185 clear_tv(&tv);
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200186 }
187 }
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200188#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200189
Bram Moolenaar402502d2019-05-30 22:07:36 +0200190 // Option values resulting in setting an option.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200191 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200192 if (str != NULL)
193 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
194 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200195
Bram Moolenaar402502d2019-05-30 22:07:36 +0200196 di = dict_find(dict, (char_u *)"wrap", -1);
197 if (di != NULL)
198 {
199 nr = dict_get_number(dict, (char_u *)"wrap");
200 wp->w_p_wrap = nr != 0;
201 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200202
203 di = dict_find(dict, (char_u *)"filter", -1);
204 if (di != NULL)
205 {
206 callback_T callback = get_callback(&di->di_tv);
207
208 if (callback.cb_name != NULL)
209 set_callback(&wp->w_filter_cb, &callback);
210 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200211
212 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
213 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200214}
215
216/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200217 * Add lines to the popup from a list of strings.
218 */
219 static void
220add_popup_strings(buf_T *buf, list_T *l)
221{
222 listitem_T *li;
223 linenr_T lnum = 0;
224 char_u *p;
225
226 for (li = l->lv_first; li != NULL; li = li->li_next)
227 if (li->li_tv.v_type == VAR_STRING)
228 {
229 p = li->li_tv.vval.v_string;
230 ml_append_buf(buf, lnum++,
231 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
232 }
233}
234
235/*
236 * Add lines to the popup from a list of dictionaries.
237 */
238 static void
239add_popup_dicts(buf_T *buf, list_T *l)
240{
241 listitem_T *li;
242 listitem_T *pli;
243 linenr_T lnum = 0;
244 char_u *p;
245 dict_T *dict;
246
247 // first add the text lines
248 for (li = l->lv_first; li != NULL; li = li->li_next)
249 {
250 if (li->li_tv.v_type != VAR_DICT)
251 {
252 emsg(_(e_dictreq));
253 return;
254 }
255 dict = li->li_tv.vval.v_dict;
256 p = dict == NULL ? NULL
257 : dict_get_string(dict, (char_u *)"text", FALSE);
258 ml_append_buf(buf, lnum++,
259 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
260 }
261
262 // add the text properties
263 lnum = 1;
264 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
265 {
266 dictitem_T *di;
267 list_T *plist;
268
269 dict = li->li_tv.vval.v_dict;
270 di = dict_find(dict, (char_u *)"props", -1);
271 if (di != NULL)
272 {
273 if (di->di_tv.v_type != VAR_LIST)
274 {
275 emsg(_(e_listreq));
276 return;
277 }
278 plist = di->di_tv.vval.v_list;
279 if (plist != NULL)
280 {
281 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
282 {
283 if (pli->li_tv.v_type != VAR_DICT)
284 {
285 emsg(_(e_dictreq));
286 return;
287 }
288 dict = pli->li_tv.vval.v_dict;
289 if (dict != NULL)
290 {
291 int col = dict_get_number(dict, (char_u *)"col");
292
293 prop_add_common( lnum, col, dict, buf, NULL);
294 }
295 }
296 }
297 }
298 }
299}
300
301/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200302 * Adjust the position and size of the popup to fit on the screen.
303 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200304 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200305popup_adjust_position(win_T *wp)
306{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200307 linenr_T lnum;
308 int wrapped = 0;
309 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200310 int center_vert = FALSE;
311 int center_hor = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200312
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200313 wp->w_winrow = 0;
314 wp->w_wincol = 0;
315 if (wp->w_popup_pos == POPPOS_CENTER)
316 {
317 // center after computing the size
318 center_vert = TRUE;
319 center_hor = TRUE;
320 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200321 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200322 {
323 if (wp->w_wantline == 0)
324 center_vert = TRUE;
325 else if (wp->w_popup_pos == POPPOS_TOPLEFT
326 || wp->w_popup_pos == POPPOS_TOPRIGHT)
327 {
328 wp->w_winrow = wp->w_wantline - 1;
329 if (wp->w_winrow >= Rows)
330 wp->w_winrow = Rows - 1;
331 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200332
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200333 if (wp->w_wantcol == 0)
334 center_hor = TRUE;
335 else if (wp->w_popup_pos == POPPOS_TOPLEFT
336 || wp->w_popup_pos == POPPOS_BOTLEFT)
337 {
338 wp->w_wincol = wp->w_wantcol - 1;
339 if (wp->w_wincol >= Columns - 3)
340 wp->w_wincol = Columns - 3;
341 }
342 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200343
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200344 // When centering or right aligned, use maximum width.
345 // When left aligned use the space available.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200346 maxwidth = Columns - wp->w_wincol;
347 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
348 maxwidth = wp->w_maxwidth;
349
350 // Compute width based on longest text line and the 'wrap' option.
351 // TODO: more accurate wrapping
352 wp->w_width = 0;
353 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
354 {
355 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
356
357 while (wp->w_p_wrap && len > maxwidth)
358 {
359 ++wrapped;
360 len -= maxwidth;
361 wp->w_width = maxwidth;
362 }
363 if (wp->w_width < len)
364 wp->w_width = len;
365 }
366
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200367 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
368 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200369 if (wp->w_width > maxwidth)
370 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200371 if (center_hor)
372 wp->w_wincol = (Columns - wp->w_width) / 2;
373 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
374 || wp->w_popup_pos == POPPOS_TOPRIGHT)
375 {
376 // Right aligned: move to the right if needed.
377 // No truncation, because that would change the height.
378 if (wp->w_width < wp->w_wantcol)
379 wp->w_wincol = wp->w_wantcol - wp->w_width;
380 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200381
382 if (wp->w_height <= 1)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200383 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200384 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
385 wp->w_height = wp->w_minheight;
386 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
387 wp->w_height = wp->w_maxheight;
388 if (wp->w_height > Rows - wp->w_winrow)
389 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200390
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200391 if (center_vert)
392 wp->w_winrow = (Rows - wp->w_height) / 2;
393 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
394 || wp->w_popup_pos == POPPOS_BOTLEFT)
395 {
396 if (wp->w_height <= wp->w_wantline)
397 // bottom aligned: may move down
398 wp->w_winrow = wp->w_wantline - wp->w_height;
399 else
400 // not enough space, make top aligned
401 wp->w_winrow = wp->w_wantline + 1;
402 }
403
Bram Moolenaar17146962019-05-30 00:12:11 +0200404 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200405}
406
407/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200408 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200409 * popup_atcursor({text}, {options})
410 * When called from f_popup_atcursor() "atcursor" is TRUE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200411 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200412 static void
413popup_create(typval_T *argvars, typval_T *rettv, int atcursor)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200414{
415 win_T *wp;
416 buf_T *buf;
417 dict_T *d;
418 int nr;
419
420 // Check arguments look OK.
421 if (!(argvars[0].v_type == VAR_STRING
422 && argvars[0].vval.v_string != NULL
423 && STRLEN(argvars[0].vval.v_string) > 0)
424 && !(argvars[0].v_type == VAR_LIST
425 && argvars[0].vval.v_list != NULL
426 && argvars[0].vval.v_list->lv_len > 0))
427 {
428 emsg(_(e_listreq));
429 return;
430 }
431 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
432 {
433 emsg(_(e_dictreq));
434 return;
435 }
436 d = argvars[1].vval.v_dict;
437
438 // Create the window and buffer.
439 wp = win_alloc_popup_win();
440 if (wp == NULL)
441 return;
442 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200443 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200444
445 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
446 if (buf == NULL)
447 return;
448 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200449
450 win_init_popup_win(wp, buf);
451
452 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200453 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200454 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200455 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200456 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200457 buf->b_p_ul = -1; // no undo
458 buf->b_p_swf = FALSE; // no swap file
459 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200460 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200461 wp->w_p_wrap = TRUE; // 'wrap' is default on
462
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200463 // Avoid that 'buftype' is reset when this buffer is entered.
464 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200465
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200466 nr = (int)dict_get_number(d, (char_u *)"tab");
467 if (nr == 0)
468 {
469 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200470 wp->w_next = curtab->tp_first_popupwin;
471 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200472 }
473 else if (nr < 0)
474 {
475 // global popup
476 wp->w_next = first_popupwin;
477 first_popupwin = wp;
478 }
479 else
480 // TODO: find tab page "nr"
481 emsg("Not implemented yet");
482
483 // Add text to the buffer.
484 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200485 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200486 // just a string
487 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200488 }
489 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200490 {
491 list_T *l = argvars[0].vval.v_list;
492
493 if (l->lv_first->li_tv.v_type == VAR_STRING)
494 // list of strings
495 add_popup_strings(buf, l);
496 else
497 // list of dictionaries
498 add_popup_dicts(buf, l);
499 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200500
501 // Delete the line of the empty buffer.
502 curbuf = buf;
503 ml_delete(buf->b_ml.ml_line_count, FALSE);
504 curbuf = curwin->w_buffer;
505
506 // Deal with options.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200507 apply_options(wp, buf, argvars[1].vval.v_dict, atcursor);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200508
509 // set default values
510 if (wp->w_zindex == 0)
511 wp->w_zindex = 50;
512
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200513 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200514
515 wp->w_vsep_width = 0;
516
517 redraw_all_later(NOT_VALID);
518}
519
520/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200521 * popup_create({text}, {options})
522 */
523 void
524f_popup_create(typval_T *argvars, typval_T *rettv)
525{
526 popup_create(argvars, rettv, FALSE);
527}
528
529/*
530 * popup_atcursor({text}, {options})
531 */
532 void
533f_popup_atcursor(typval_T *argvars, typval_T *rettv)
534{
535 popup_create(argvars, rettv, TRUE);
536}
537
538/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200539 * Find the popup window with window-ID "id".
540 * If the popup window does not exist NULL is returned.
541 * If the window is not a popup window, and error message is given.
542 */
543 static win_T *
544find_popup_win(int id)
545{
546 win_T *wp = win_id2wp(id);
547
548 if (wp != NULL && !bt_popup(wp->w_buffer))
549 {
550 semsg(_("E993: window %d is not a popup window"), id);
551 return NULL;
552 }
553 return wp;
554}
555
556/*
557 * Return TRUE if there any popups that are not hidden.
558 */
559 int
560popup_any_visible(void)
561{
562 win_T *wp;
563
564 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200565 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200566 return TRUE;
567 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200568 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200569 return TRUE;
570 return FALSE;
571}
572
573/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200574 * popup_close({id})
575 */
576 void
577f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
578{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200579 int id = (int)tv_get_number(argvars);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200580
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200581 popup_close(id);
582}
583
584/*
585 * popup_hide({id})
586 */
587 void
588f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
589{
590 int id = (int)tv_get_number(argvars);
591 win_T *wp = find_popup_win(id);
592
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200593 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200594 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200595 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200596 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200597 redraw_all_later(NOT_VALID);
598 }
599}
600
601/*
602 * popup_show({id})
603 */
604 void
605f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
606{
607 int id = (int)tv_get_number(argvars);
608 win_T *wp = find_popup_win(id);
609
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200610 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200611 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200612 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200613 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200614 redraw_all_later(NOT_VALID);
615 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200616}
617
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200618 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200619popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200620{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200621 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200622 if (wp->w_winrow + wp->w_height >= cmdline_row)
623 clear_cmdline = TRUE;
624 win_free_popup(wp);
625 redraw_all_later(NOT_VALID);
626}
627
Bram Moolenaarec583842019-05-26 14:11:23 +0200628/*
629 * Close a popup window by Window-id.
630 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200631 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200632popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200633{
634 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200635 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200636 win_T *prev = NULL;
637
Bram Moolenaarec583842019-05-26 14:11:23 +0200638 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200639 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200640 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200641 {
642 if (prev == NULL)
643 first_popupwin = wp->w_next;
644 else
645 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200646 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200647 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200648 }
649
Bram Moolenaarec583842019-05-26 14:11:23 +0200650 // go through tab-local popups
651 FOR_ALL_TABPAGES(tp)
652 popup_close_tabpage(tp, id);
653}
654
655/*
656 * Close a popup window with Window-id "id" in tabpage "tp".
657 */
658 void
659popup_close_tabpage(tabpage_T *tp, int id)
660{
661 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200662 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200663 win_T *prev = NULL;
664
Bram Moolenaarec583842019-05-26 14:11:23 +0200665 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
666 if (wp->w_id == id)
667 {
668 if (prev == NULL)
669 *root = wp->w_next;
670 else
671 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200672 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200673 return;
674 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200675}
676
677 void
678close_all_popups(void)
679{
680 while (first_popupwin != NULL)
681 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200682 while (curtab->tp_first_popupwin != NULL)
683 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200684}
685
686 void
687ex_popupclear(exarg_T *eap UNUSED)
688{
689 close_all_popups();
690}
691
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200692/*
693 * popup_move({id}, {options})
694 */
695 void
696f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
697{
698 dict_T *d;
699 int nr;
700 int id = (int)tv_get_number(argvars);
701 win_T *wp = find_popup_win(id);
702
703 if (wp == NULL)
704 return; // invalid {id}
705
706 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
707 {
708 emsg(_(e_dictreq));
709 return;
710 }
711 d = argvars[1].vval.v_dict;
712
713 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
714 wp->w_minwidth = nr;
715 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
716 wp->w_minheight = nr;
717 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
718 wp->w_maxwidth = nr;
719 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
720 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200721 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200722
723 if (wp->w_winrow + wp->w_height >= cmdline_row)
724 clear_cmdline = TRUE;
725 popup_adjust_position(wp);
726 redraw_all_later(NOT_VALID);
727}
728
Bram Moolenaarbc133542019-05-29 20:26:46 +0200729/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200730 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200731 */
732 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200733f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200734{
735 dict_T *dict;
736 int id = (int)tv_get_number(argvars);
737 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200738 int top_extra;
739 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +0200740
741 if (rettv_dict_alloc(rettv) == OK)
742 {
743 if (wp == NULL)
744 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200745 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
746 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
747
Bram Moolenaarbc133542019-05-29 20:26:46 +0200748 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200749
Bram Moolenaarbc133542019-05-29 20:26:46 +0200750 dict_add_number(dict, "line", wp->w_winrow + 1);
751 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200752 dict_add_number(dict, "width", wp->w_width + left_extra + wp->w_popup_border[1] + wp->w_popup_padding[1]);
753 dict_add_number(dict, "height", wp->w_height + top_extra + wp->w_popup_border[2] + wp->w_popup_padding[2]);
754
755 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
756 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
757 dict_add_number(dict, "core_width", wp->w_width);
758 dict_add_number(dict, "core_height", wp->w_height);
759
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200760 dict_add_number(dict, "visible",
761 (wp->w_popup_flags & POPF_HIDDEN) == 0);
762 }
763}
764
765/*
766 * f_popup_getoptions({id})
767 */
768 void
769f_popup_getoptions(typval_T *argvars, typval_T *rettv)
770{
771 dict_T *dict;
772 int id = (int)tv_get_number(argvars);
773 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200774 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200775
776 if (rettv_dict_alloc(rettv) == OK)
777 {
778 if (wp == NULL)
779 return;
780
781 dict = rettv->vval.v_dict;
782 dict_add_number(dict, "line", wp->w_wantline);
783 dict_add_number(dict, "col", wp->w_wantcol);
784 dict_add_number(dict, "minwidth", wp->w_minwidth);
785 dict_add_number(dict, "minheight", wp->w_minheight);
786 dict_add_number(dict, "maxheight", wp->w_maxheight);
787 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
788 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200789
790 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
791 ++i)
792 if (wp->w_popup_pos == poppos_entries[i].pp_val)
793 {
794 dict_add_string(dict, "pos",
795 (char_u *)poppos_entries[i].pp_name);
796 break;
797 }
798
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200799# if defined(FEAT_TIMERS)
800 dict_add_number(dict, "time", wp->w_popup_timer != NULL
801 ? (long)wp->w_popup_timer->tr_interval : 0L);
802# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +0200803 }
804}
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200805
806 int
807not_in_popup_window()
808{
809 if (bt_popup(curwin->w_buffer))
810 {
811 emsg(_("E994: Not allowed in a popup window"));
812 return TRUE;
813 }
814 return FALSE;
815}
816
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200817/*
818 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
819 * in the current tab.
820 */
821 void
822popup_reset_handled()
823{
824 win_T *wp;
825
826 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
827 wp->w_popup_flags &= ~POPF_HANDLED;
828 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
829 wp->w_popup_flags &= ~POPF_HANDLED;
830}
831
832/*
833 * Find the next visible popup where POPF_HANDLED is not set.
834 * Must have called popup_reset_handled() first.
835 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
836 * popup with the highest zindex.
837 */
838 win_T *
839find_next_popup(int lowest)
840{
841 win_T *wp;
842 win_T *found_wp;
843 int found_zindex;
844
845 found_zindex = lowest ? INT_MAX : 0;
846 found_wp = NULL;
847 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
848 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
849 && (lowest ? wp->w_zindex < found_zindex
850 : wp->w_zindex > found_zindex))
851 {
852 found_zindex = wp->w_zindex;
853 found_wp = wp;
854 }
855 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
856 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
857 && (lowest ? wp->w_zindex < found_zindex
858 : wp->w_zindex > found_zindex))
859 {
860 found_zindex = wp->w_zindex;
861 found_wp = wp;
862 }
863
864 if (found_wp != NULL)
865 found_wp->w_popup_flags |= POPF_HANDLED;
866 return found_wp;
867}
868
869/*
870 * Invoke the filter callback for window "wp" with typed character "c".
871 * Uses the global "mod_mask" for modifiers.
872 * Returns the return value of the filter.
873 * Careful: The filter may make "wp" invalid!
874 */
875 static int
876invoke_popup_filter(win_T *wp, int c)
877{
878 int res;
879 typval_T rettv;
880 int dummy;
881 typval_T argv[3];
882 char_u buf[NUMBUFLEN];
883
884 argv[0].v_type = VAR_NUMBER;
885 argv[0].vval.v_number = (varnumber_T)wp->w_id;
886
887 // Convert the number to a string, so that the function can use:
888 // if a:c == "\<F2>"
889 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
890 argv[1].v_type = VAR_STRING;
891 argv[1].vval.v_string = vim_strsave(buf);
892
893 argv[2].v_type = VAR_UNKNOWN;
894
895 call_callback(&wp->w_filter_cb, -1,
896 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
897 res = tv_get_number(&rettv);
898 vim_free(argv[1].vval.v_string);
899 clear_tv(&rettv);
900 return res;
901}
902
903/*
904 * Called when "c" was typed: invoke popup filter callbacks.
905 * Returns TRUE when the character was consumed,
906 */
907 int
908popup_do_filter(int c)
909{
910 int res = FALSE;
911 win_T *wp;
912
913 popup_reset_handled();
914
915 while (!res && (wp = find_next_popup(FALSE)) != NULL)
916 if (wp->w_filter_cb.cb_name != NULL)
917 res = invoke_popup_filter(wp, c);
918
919 return res;
920}
921
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200922#endif // FEAT_TEXT_PROP