blob: 4e07bdb597d64c428e48d25d8b9eb3fe69316b36 [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 Moolenaar790498b2019-06-01 22:15:29 +0200147 int i;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200148
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200149 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
150 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200151 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
152 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200153
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200154 if (atcursor)
155 {
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200156 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200157 setcursor_mayforce(TRUE);
158 wp->w_wantline = screen_screenrow();
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200159 if (wp->w_wantline == 0) // cursor in first line
160 {
161 wp->w_wantline = 2;
162 wp->w_popup_pos = POPPOS_TOPLEFT;
163 }
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200164 wp->w_wantcol = screen_screencol() + 1;
165 }
166
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200167 get_pos_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200168
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200169 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200170
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200171#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200172 // Add timer to close the popup after some time.
173 nr = dict_get_number(dict, (char_u *)"time");
174 if (nr > 0)
175 {
176 char_u cbbuf[50];
177 char_u *ptr = cbbuf;
178 typval_T tv;
179
180 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
181 "{_ -> popup_close(%d)}", wp->w_id);
182 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
183 {
184 wp->w_popup_timer = create_timer(nr, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200185 wp->w_popup_timer->tr_callback = get_callback(&tv);
186 clear_tv(&tv);
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200187 }
188 }
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200189#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200190
Bram Moolenaar402502d2019-05-30 22:07:36 +0200191 // Option values resulting in setting an option.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200192 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200193 if (str != NULL)
194 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
195 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200196
Bram Moolenaar402502d2019-05-30 22:07:36 +0200197 di = dict_find(dict, (char_u *)"wrap", -1);
198 if (di != NULL)
199 {
200 nr = dict_get_number(dict, (char_u *)"wrap");
201 wp->w_p_wrap = nr != 0;
202 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200203
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200204 di = dict_find(dict, (char_u *)"callback", -1);
205 if (di != NULL)
206 {
207 callback_T callback = get_callback(&di->di_tv);
208
209 if (callback.cb_name != NULL)
210 set_callback(&wp->w_close_cb, &callback);
211 }
212
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200213 di = dict_find(dict, (char_u *)"filter", -1);
214 if (di != NULL)
215 {
216 callback_T callback = get_callback(&di->di_tv);
217
218 if (callback.cb_name != NULL)
219 set_callback(&wp->w_filter_cb, &callback);
220 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200221
222 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
223 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200224
225 for (i = 0; i < 4; ++i)
226 VIM_CLEAR(wp->w_border_highlight[i]);
227 di = dict_find(dict, (char_u *)"borderhighlight", -1);
228 if (di != NULL)
229 {
230 if (di->di_tv.v_type != VAR_LIST)
231 emsg(_(e_listreq));
232 else
233 {
234 list_T *list = di->di_tv.vval.v_list;
235 listitem_T *li;
236
237 if (list != NULL)
238 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
239 ++i, li = li->li_next)
240 {
241 str = tv_get_string(&li->li_tv);
242 if (*str != NUL)
243 wp->w_border_highlight[i] = vim_strsave(str);
244 }
245 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
246 for (i = 1; i < 4; ++i)
247 wp->w_border_highlight[i] =
248 vim_strsave(wp->w_border_highlight[0]);
249 }
250 }
251
252 for (i = 0; i < 8; ++i)
253 wp->w_border_char[i] = 0;
254 di = dict_find(dict, (char_u *)"borderchars", -1);
255 if (di != NULL)
256 {
257 if (di->di_tv.v_type != VAR_LIST)
258 emsg(_(e_listreq));
259 else
260 {
261 list_T *list = di->di_tv.vval.v_list;
262 listitem_T *li;
263
264 if (list != NULL)
265 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
266 ++i, li = li->li_next)
267 {
268 str = tv_get_string(&li->li_tv);
269 if (*str != NUL)
270 wp->w_border_char[i] = mb_ptr2char(str);
271 }
272 if (list->lv_len == 1)
273 for (i = 1; i < 8; ++i)
274 wp->w_border_char[i] = wp->w_border_char[0];
275 if (list->lv_len == 2)
276 {
277 for (i = 4; i < 8; ++i)
278 wp->w_border_char[i] = wp->w_border_char[1];
279 for (i = 1; i < 4; ++i)
280 wp->w_border_char[i] = wp->w_border_char[0];
281 }
282 }
283 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200284}
285
286/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200287 * Add lines to the popup from a list of strings.
288 */
289 static void
290add_popup_strings(buf_T *buf, list_T *l)
291{
292 listitem_T *li;
293 linenr_T lnum = 0;
294 char_u *p;
295
296 for (li = l->lv_first; li != NULL; li = li->li_next)
297 if (li->li_tv.v_type == VAR_STRING)
298 {
299 p = li->li_tv.vval.v_string;
300 ml_append_buf(buf, lnum++,
301 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
302 }
303}
304
305/*
306 * Add lines to the popup from a list of dictionaries.
307 */
308 static void
309add_popup_dicts(buf_T *buf, list_T *l)
310{
311 listitem_T *li;
312 listitem_T *pli;
313 linenr_T lnum = 0;
314 char_u *p;
315 dict_T *dict;
316
317 // first add the text lines
318 for (li = l->lv_first; li != NULL; li = li->li_next)
319 {
320 if (li->li_tv.v_type != VAR_DICT)
321 {
322 emsg(_(e_dictreq));
323 return;
324 }
325 dict = li->li_tv.vval.v_dict;
326 p = dict == NULL ? NULL
327 : dict_get_string(dict, (char_u *)"text", FALSE);
328 ml_append_buf(buf, lnum++,
329 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
330 }
331
332 // add the text properties
333 lnum = 1;
334 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
335 {
336 dictitem_T *di;
337 list_T *plist;
338
339 dict = li->li_tv.vval.v_dict;
340 di = dict_find(dict, (char_u *)"props", -1);
341 if (di != NULL)
342 {
343 if (di->di_tv.v_type != VAR_LIST)
344 {
345 emsg(_(e_listreq));
346 return;
347 }
348 plist = di->di_tv.vval.v_list;
349 if (plist != NULL)
350 {
351 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
352 {
353 if (pli->li_tv.v_type != VAR_DICT)
354 {
355 emsg(_(e_dictreq));
356 return;
357 }
358 dict = pli->li_tv.vval.v_dict;
359 if (dict != NULL)
360 {
361 int col = dict_get_number(dict, (char_u *)"col");
362
363 prop_add_common( lnum, col, dict, buf, NULL);
364 }
365 }
366 }
367 }
368 }
369}
370
371/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200372 * Adjust the position and size of the popup to fit on the screen.
373 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200374 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200375popup_adjust_position(win_T *wp)
376{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200377 linenr_T lnum;
378 int wrapped = 0;
379 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200380 int center_vert = FALSE;
381 int center_hor = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200382
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200383 wp->w_winrow = 0;
384 wp->w_wincol = 0;
385 if (wp->w_popup_pos == POPPOS_CENTER)
386 {
387 // center after computing the size
388 center_vert = TRUE;
389 center_hor = TRUE;
390 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200391 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200392 {
393 if (wp->w_wantline == 0)
394 center_vert = TRUE;
395 else if (wp->w_popup_pos == POPPOS_TOPLEFT
396 || wp->w_popup_pos == POPPOS_TOPRIGHT)
397 {
398 wp->w_winrow = wp->w_wantline - 1;
399 if (wp->w_winrow >= Rows)
400 wp->w_winrow = Rows - 1;
401 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200402
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200403 if (wp->w_wantcol == 0)
404 center_hor = TRUE;
405 else if (wp->w_popup_pos == POPPOS_TOPLEFT
406 || wp->w_popup_pos == POPPOS_BOTLEFT)
407 {
408 wp->w_wincol = wp->w_wantcol - 1;
409 if (wp->w_wincol >= Columns - 3)
410 wp->w_wincol = Columns - 3;
411 }
412 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200413
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200414 // When centering or right aligned, use maximum width.
415 // When left aligned use the space available.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200416 maxwidth = Columns - wp->w_wincol;
417 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
418 maxwidth = wp->w_maxwidth;
419
420 // Compute width based on longest text line and the 'wrap' option.
421 // TODO: more accurate wrapping
422 wp->w_width = 0;
423 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
424 {
425 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
426
427 while (wp->w_p_wrap && len > maxwidth)
428 {
429 ++wrapped;
430 len -= maxwidth;
431 wp->w_width = maxwidth;
432 }
433 if (wp->w_width < len)
434 wp->w_width = len;
435 }
436
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200437 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
438 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200439 if (wp->w_width > maxwidth)
440 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200441 if (center_hor)
442 wp->w_wincol = (Columns - wp->w_width) / 2;
443 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
444 || wp->w_popup_pos == POPPOS_TOPRIGHT)
445 {
446 // Right aligned: move to the right if needed.
447 // No truncation, because that would change the height.
448 if (wp->w_width < wp->w_wantcol)
449 wp->w_wincol = wp->w_wantcol - wp->w_width;
450 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200451
452 if (wp->w_height <= 1)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200453 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200454 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
455 wp->w_height = wp->w_minheight;
456 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
457 wp->w_height = wp->w_maxheight;
458 if (wp->w_height > Rows - wp->w_winrow)
459 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200460
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200461 if (center_vert)
462 wp->w_winrow = (Rows - wp->w_height) / 2;
463 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
464 || wp->w_popup_pos == POPPOS_BOTLEFT)
465 {
466 if (wp->w_height <= wp->w_wantline)
467 // bottom aligned: may move down
468 wp->w_winrow = wp->w_wantline - wp->w_height;
469 else
470 // not enough space, make top aligned
471 wp->w_winrow = wp->w_wantline + 1;
472 }
473
Bram Moolenaar17146962019-05-30 00:12:11 +0200474 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200475}
476
477/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200478 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200479 * popup_atcursor({text}, {options})
480 * When called from f_popup_atcursor() "atcursor" is TRUE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200481 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200482 static void
483popup_create(typval_T *argvars, typval_T *rettv, int atcursor)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200484{
485 win_T *wp;
486 buf_T *buf;
487 dict_T *d;
488 int nr;
489
490 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200491 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
492 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200493 {
494 emsg(_(e_listreq));
495 return;
496 }
497 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
498 {
499 emsg(_(e_dictreq));
500 return;
501 }
502 d = argvars[1].vval.v_dict;
503
504 // Create the window and buffer.
505 wp = win_alloc_popup_win();
506 if (wp == NULL)
507 return;
508 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200509 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200510
511 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
512 if (buf == NULL)
513 return;
514 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200515
516 win_init_popup_win(wp, buf);
517
518 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200519 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200520 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200521 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200522 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200523 buf->b_p_ul = -1; // no undo
524 buf->b_p_swf = FALSE; // no swap file
525 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200526 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200527 wp->w_p_wrap = TRUE; // 'wrap' is default on
528
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200529 // Avoid that 'buftype' is reset when this buffer is entered.
530 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200531
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200532 nr = (int)dict_get_number(d, (char_u *)"tab");
533 if (nr == 0)
534 {
535 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200536 wp->w_next = curtab->tp_first_popupwin;
537 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200538 }
539 else if (nr < 0)
540 {
541 // global popup
542 wp->w_next = first_popupwin;
543 first_popupwin = wp;
544 }
545 else
546 // TODO: find tab page "nr"
547 emsg("Not implemented yet");
548
549 // Add text to the buffer.
550 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200551 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200552 // just a string
553 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200554 }
555 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200556 {
557 list_T *l = argvars[0].vval.v_list;
558
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200559 if (l->lv_len > 0)
560 {
561 if (l->lv_first->li_tv.v_type == VAR_STRING)
562 // list of strings
563 add_popup_strings(buf, l);
564 else
565 // list of dictionaries
566 add_popup_dicts(buf, l);
567 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200568 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200569
570 // Delete the line of the empty buffer.
571 curbuf = buf;
572 ml_delete(buf->b_ml.ml_line_count, FALSE);
573 curbuf = curwin->w_buffer;
574
575 // Deal with options.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200576 apply_options(wp, buf, argvars[1].vval.v_dict, atcursor);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200577
578 // set default values
579 if (wp->w_zindex == 0)
580 wp->w_zindex = 50;
581
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200582 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200583
584 wp->w_vsep_width = 0;
585
586 redraw_all_later(NOT_VALID);
587}
588
589/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200590 * popup_create({text}, {options})
591 */
592 void
593f_popup_create(typval_T *argvars, typval_T *rettv)
594{
595 popup_create(argvars, rettv, FALSE);
596}
597
598/*
599 * popup_atcursor({text}, {options})
600 */
601 void
602f_popup_atcursor(typval_T *argvars, typval_T *rettv)
603{
604 popup_create(argvars, rettv, TRUE);
605}
606
607/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200608 * Find the popup window with window-ID "id".
609 * If the popup window does not exist NULL is returned.
610 * If the window is not a popup window, and error message is given.
611 */
612 static win_T *
613find_popup_win(int id)
614{
615 win_T *wp = win_id2wp(id);
616
617 if (wp != NULL && !bt_popup(wp->w_buffer))
618 {
619 semsg(_("E993: window %d is not a popup window"), id);
620 return NULL;
621 }
622 return wp;
623}
624
625/*
626 * Return TRUE if there any popups that are not hidden.
627 */
628 int
629popup_any_visible(void)
630{
631 win_T *wp;
632
633 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200634 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200635 return TRUE;
636 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200637 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200638 return TRUE;
639 return FALSE;
640}
641
642/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200643 * Invoke the close callback for window "wp" with value "result".
644 * Careful: The callback may make "wp" invalid!
645 */
646 static void
647invoke_popup_callback(win_T *wp, typval_T *result)
648{
649 typval_T rettv;
650 int dummy;
651 typval_T argv[3];
652
653 argv[0].v_type = VAR_NUMBER;
654 argv[0].vval.v_number = (varnumber_T)wp->w_id;
655
656 if (result != NULL && result->v_type != VAR_UNKNOWN)
657 copy_tv(result, &argv[1]);
658 else
659 {
660 argv[1].v_type = VAR_NUMBER;
661 argv[1].vval.v_number = 0;
662 }
663
664 argv[2].v_type = VAR_UNKNOWN;
665
666 call_callback(&wp->w_close_cb, -1,
667 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
668 if (result != NULL)
669 clear_tv(&argv[1]);
670 clear_tv(&rettv);
671}
672
673/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200674 * popup_close({id})
675 */
676 void
677f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
678{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200679 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200680 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200681
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200682 if (wp != NULL)
683 {
684 if (wp->w_close_cb.cb_name != NULL)
685 // Careful: This may make "wp" invalid.
686 invoke_popup_callback(wp, &argvars[1]);
687
688 popup_close(id);
689 }
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200690}
691
692/*
693 * popup_hide({id})
694 */
695 void
696f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
697{
698 int id = (int)tv_get_number(argvars);
699 win_T *wp = find_popup_win(id);
700
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200701 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200702 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200703 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200704 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200705 redraw_all_later(NOT_VALID);
706 }
707}
708
709/*
710 * popup_show({id})
711 */
712 void
713f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
714{
715 int id = (int)tv_get_number(argvars);
716 win_T *wp = find_popup_win(id);
717
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200718 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200719 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200720 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200721 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200722 redraw_all_later(NOT_VALID);
723 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200724}
725
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200726 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200727popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200728{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200729 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200730 if (wp->w_winrow + wp->w_height >= cmdline_row)
731 clear_cmdline = TRUE;
732 win_free_popup(wp);
733 redraw_all_later(NOT_VALID);
734}
735
Bram Moolenaarec583842019-05-26 14:11:23 +0200736/*
737 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200738 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +0200739 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200740 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200741popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200742{
743 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200744 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200745 win_T *prev = NULL;
746
Bram Moolenaarec583842019-05-26 14:11:23 +0200747 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200748 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200749 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200750 {
751 if (prev == NULL)
752 first_popupwin = wp->w_next;
753 else
754 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200755 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200756 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200757 }
758
Bram Moolenaarec583842019-05-26 14:11:23 +0200759 // go through tab-local popups
760 FOR_ALL_TABPAGES(tp)
761 popup_close_tabpage(tp, id);
762}
763
764/*
765 * Close a popup window with Window-id "id" in tabpage "tp".
766 */
767 void
768popup_close_tabpage(tabpage_T *tp, int id)
769{
770 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200771 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200772 win_T *prev = NULL;
773
Bram Moolenaarec583842019-05-26 14:11:23 +0200774 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
775 if (wp->w_id == id)
776 {
777 if (prev == NULL)
778 *root = wp->w_next;
779 else
780 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200781 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200782 return;
783 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200784}
785
786 void
787close_all_popups(void)
788{
789 while (first_popupwin != NULL)
790 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200791 while (curtab->tp_first_popupwin != NULL)
792 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200793}
794
795 void
796ex_popupclear(exarg_T *eap UNUSED)
797{
798 close_all_popups();
799}
800
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200801/*
802 * popup_move({id}, {options})
803 */
804 void
805f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
806{
807 dict_T *d;
808 int nr;
809 int id = (int)tv_get_number(argvars);
810 win_T *wp = find_popup_win(id);
811
812 if (wp == NULL)
813 return; // invalid {id}
814
815 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
816 {
817 emsg(_(e_dictreq));
818 return;
819 }
820 d = argvars[1].vval.v_dict;
821
822 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
823 wp->w_minwidth = nr;
824 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
825 wp->w_minheight = nr;
826 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
827 wp->w_maxwidth = nr;
828 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
829 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200830 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200831
832 if (wp->w_winrow + wp->w_height >= cmdline_row)
833 clear_cmdline = TRUE;
834 popup_adjust_position(wp);
835 redraw_all_later(NOT_VALID);
836}
837
Bram Moolenaarbc133542019-05-29 20:26:46 +0200838/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200839 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200840 */
841 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200842f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200843{
844 dict_T *dict;
845 int id = (int)tv_get_number(argvars);
846 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200847 int top_extra;
848 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +0200849
850 if (rettv_dict_alloc(rettv) == OK)
851 {
852 if (wp == NULL)
853 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200854 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
855 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
856
Bram Moolenaarbc133542019-05-29 20:26:46 +0200857 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200858
Bram Moolenaarbc133542019-05-29 20:26:46 +0200859 dict_add_number(dict, "line", wp->w_winrow + 1);
860 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200861 dict_add_number(dict, "width", wp->w_width + left_extra + wp->w_popup_border[1] + wp->w_popup_padding[1]);
862 dict_add_number(dict, "height", wp->w_height + top_extra + wp->w_popup_border[2] + wp->w_popup_padding[2]);
863
864 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
865 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
866 dict_add_number(dict, "core_width", wp->w_width);
867 dict_add_number(dict, "core_height", wp->w_height);
868
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200869 dict_add_number(dict, "visible",
870 (wp->w_popup_flags & POPF_HIDDEN) == 0);
871 }
872}
873
874/*
875 * f_popup_getoptions({id})
876 */
877 void
878f_popup_getoptions(typval_T *argvars, typval_T *rettv)
879{
880 dict_T *dict;
881 int id = (int)tv_get_number(argvars);
882 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200883 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200884
885 if (rettv_dict_alloc(rettv) == OK)
886 {
887 if (wp == NULL)
888 return;
889
890 dict = rettv->vval.v_dict;
891 dict_add_number(dict, "line", wp->w_wantline);
892 dict_add_number(dict, "col", wp->w_wantcol);
893 dict_add_number(dict, "minwidth", wp->w_minwidth);
894 dict_add_number(dict, "minheight", wp->w_minheight);
895 dict_add_number(dict, "maxheight", wp->w_maxheight);
896 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
897 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200898
899 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
900 ++i)
901 if (wp->w_popup_pos == poppos_entries[i].pp_val)
902 {
903 dict_add_string(dict, "pos",
904 (char_u *)poppos_entries[i].pp_name);
905 break;
906 }
907
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200908# if defined(FEAT_TIMERS)
909 dict_add_number(dict, "time", wp->w_popup_timer != NULL
910 ? (long)wp->w_popup_timer->tr_interval : 0L);
911# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +0200912 }
913}
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200914
915 int
916not_in_popup_window()
917{
918 if (bt_popup(curwin->w_buffer))
919 {
920 emsg(_("E994: Not allowed in a popup window"));
921 return TRUE;
922 }
923 return FALSE;
924}
925
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200926/*
927 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
928 * in the current tab.
929 */
930 void
931popup_reset_handled()
932{
933 win_T *wp;
934
935 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
936 wp->w_popup_flags &= ~POPF_HANDLED;
937 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
938 wp->w_popup_flags &= ~POPF_HANDLED;
939}
940
941/*
942 * Find the next visible popup where POPF_HANDLED is not set.
943 * Must have called popup_reset_handled() first.
944 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
945 * popup with the highest zindex.
946 */
947 win_T *
948find_next_popup(int lowest)
949{
950 win_T *wp;
951 win_T *found_wp;
952 int found_zindex;
953
954 found_zindex = lowest ? INT_MAX : 0;
955 found_wp = NULL;
956 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
957 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
958 && (lowest ? wp->w_zindex < found_zindex
959 : wp->w_zindex > found_zindex))
960 {
961 found_zindex = wp->w_zindex;
962 found_wp = wp;
963 }
964 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
965 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
966 && (lowest ? wp->w_zindex < found_zindex
967 : wp->w_zindex > found_zindex))
968 {
969 found_zindex = wp->w_zindex;
970 found_wp = wp;
971 }
972
973 if (found_wp != NULL)
974 found_wp->w_popup_flags |= POPF_HANDLED;
975 return found_wp;
976}
977
978/*
979 * Invoke the filter callback for window "wp" with typed character "c".
980 * Uses the global "mod_mask" for modifiers.
981 * Returns the return value of the filter.
982 * Careful: The filter may make "wp" invalid!
983 */
984 static int
985invoke_popup_filter(win_T *wp, int c)
986{
987 int res;
988 typval_T rettv;
989 int dummy;
990 typval_T argv[3];
991 char_u buf[NUMBUFLEN];
992
993 argv[0].v_type = VAR_NUMBER;
994 argv[0].vval.v_number = (varnumber_T)wp->w_id;
995
996 // Convert the number to a string, so that the function can use:
997 // if a:c == "\<F2>"
998 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
999 argv[1].v_type = VAR_STRING;
1000 argv[1].vval.v_string = vim_strsave(buf);
1001
1002 argv[2].v_type = VAR_UNKNOWN;
1003
1004 call_callback(&wp->w_filter_cb, -1,
1005 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1006 res = tv_get_number(&rettv);
1007 vim_free(argv[1].vval.v_string);
1008 clear_tv(&rettv);
1009 return res;
1010}
1011
1012/*
1013 * Called when "c" was typed: invoke popup filter callbacks.
1014 * Returns TRUE when the character was consumed,
1015 */
1016 int
1017popup_do_filter(int c)
1018{
1019 int res = FALSE;
1020 win_T *wp;
1021
1022 popup_reset_handled();
1023
1024 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1025 if (wp->w_filter_cb.cb_name != NULL)
1026 res = invoke_popup_filter(wp, c);
1027
1028 return res;
1029}
1030
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001031#endif // FEAT_TEXT_PROP