blob: dc3b56468bd21e6c78b6665f18adf9272d3abe36 [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.
491 if (!(argvars[0].v_type == VAR_STRING
492 && argvars[0].vval.v_string != NULL
493 && STRLEN(argvars[0].vval.v_string) > 0)
494 && !(argvars[0].v_type == VAR_LIST
495 && argvars[0].vval.v_list != NULL
496 && argvars[0].vval.v_list->lv_len > 0))
497 {
498 emsg(_(e_listreq));
499 return;
500 }
501 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
502 {
503 emsg(_(e_dictreq));
504 return;
505 }
506 d = argvars[1].vval.v_dict;
507
508 // Create the window and buffer.
509 wp = win_alloc_popup_win();
510 if (wp == NULL)
511 return;
512 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200513 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200514
515 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
516 if (buf == NULL)
517 return;
518 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200519
520 win_init_popup_win(wp, buf);
521
522 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200523 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200524 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200525 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200526 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200527 buf->b_p_ul = -1; // no undo
528 buf->b_p_swf = FALSE; // no swap file
529 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200530 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200531 wp->w_p_wrap = TRUE; // 'wrap' is default on
532
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200533 // Avoid that 'buftype' is reset when this buffer is entered.
534 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200535
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200536 nr = (int)dict_get_number(d, (char_u *)"tab");
537 if (nr == 0)
538 {
539 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200540 wp->w_next = curtab->tp_first_popupwin;
541 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200542 }
543 else if (nr < 0)
544 {
545 // global popup
546 wp->w_next = first_popupwin;
547 first_popupwin = wp;
548 }
549 else
550 // TODO: find tab page "nr"
551 emsg("Not implemented yet");
552
553 // Add text to the buffer.
554 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200555 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200556 // just a string
557 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200558 }
559 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200560 {
561 list_T *l = argvars[0].vval.v_list;
562
563 if (l->lv_first->li_tv.v_type == VAR_STRING)
564 // list of strings
565 add_popup_strings(buf, l);
566 else
567 // list of dictionaries
568 add_popup_dicts(buf, l);
569 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200570
571 // Delete the line of the empty buffer.
572 curbuf = buf;
573 ml_delete(buf->b_ml.ml_line_count, FALSE);
574 curbuf = curwin->w_buffer;
575
576 // Deal with options.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200577 apply_options(wp, buf, argvars[1].vval.v_dict, atcursor);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200578
579 // set default values
580 if (wp->w_zindex == 0)
581 wp->w_zindex = 50;
582
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200583 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200584
585 wp->w_vsep_width = 0;
586
587 redraw_all_later(NOT_VALID);
588}
589
590/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200591 * popup_create({text}, {options})
592 */
593 void
594f_popup_create(typval_T *argvars, typval_T *rettv)
595{
596 popup_create(argvars, rettv, FALSE);
597}
598
599/*
600 * popup_atcursor({text}, {options})
601 */
602 void
603f_popup_atcursor(typval_T *argvars, typval_T *rettv)
604{
605 popup_create(argvars, rettv, TRUE);
606}
607
608/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200609 * Find the popup window with window-ID "id".
610 * If the popup window does not exist NULL is returned.
611 * If the window is not a popup window, and error message is given.
612 */
613 static win_T *
614find_popup_win(int id)
615{
616 win_T *wp = win_id2wp(id);
617
618 if (wp != NULL && !bt_popup(wp->w_buffer))
619 {
620 semsg(_("E993: window %d is not a popup window"), id);
621 return NULL;
622 }
623 return wp;
624}
625
626/*
627 * Return TRUE if there any popups that are not hidden.
628 */
629 int
630popup_any_visible(void)
631{
632 win_T *wp;
633
634 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200635 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200636 return TRUE;
637 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200638 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200639 return TRUE;
640 return FALSE;
641}
642
643/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200644 * Invoke the close callback for window "wp" with value "result".
645 * Careful: The callback may make "wp" invalid!
646 */
647 static void
648invoke_popup_callback(win_T *wp, typval_T *result)
649{
650 typval_T rettv;
651 int dummy;
652 typval_T argv[3];
653
654 argv[0].v_type = VAR_NUMBER;
655 argv[0].vval.v_number = (varnumber_T)wp->w_id;
656
657 if (result != NULL && result->v_type != VAR_UNKNOWN)
658 copy_tv(result, &argv[1]);
659 else
660 {
661 argv[1].v_type = VAR_NUMBER;
662 argv[1].vval.v_number = 0;
663 }
664
665 argv[2].v_type = VAR_UNKNOWN;
666
667 call_callback(&wp->w_close_cb, -1,
668 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
669 if (result != NULL)
670 clear_tv(&argv[1]);
671 clear_tv(&rettv);
672}
673
674/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200675 * popup_close({id})
676 */
677 void
678f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
679{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200680 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200681 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200682
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200683 if (wp != NULL)
684 {
685 if (wp->w_close_cb.cb_name != NULL)
686 // Careful: This may make "wp" invalid.
687 invoke_popup_callback(wp, &argvars[1]);
688
689 popup_close(id);
690 }
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200691}
692
693/*
694 * popup_hide({id})
695 */
696 void
697f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
698{
699 int id = (int)tv_get_number(argvars);
700 win_T *wp = find_popup_win(id);
701
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200702 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200703 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200704 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200705 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200706 redraw_all_later(NOT_VALID);
707 }
708}
709
710/*
711 * popup_show({id})
712 */
713 void
714f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
715{
716 int id = (int)tv_get_number(argvars);
717 win_T *wp = find_popup_win(id);
718
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200719 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200720 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200721 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200722 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200723 redraw_all_later(NOT_VALID);
724 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200725}
726
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200727 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200728popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200729{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200730 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200731 if (wp->w_winrow + wp->w_height >= cmdline_row)
732 clear_cmdline = TRUE;
733 win_free_popup(wp);
734 redraw_all_later(NOT_VALID);
735}
736
Bram Moolenaarec583842019-05-26 14:11:23 +0200737/*
738 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200739 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +0200740 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200741 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200742popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200743{
744 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200745 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200746 win_T *prev = NULL;
747
Bram Moolenaarec583842019-05-26 14:11:23 +0200748 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200749 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200750 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200751 {
752 if (prev == NULL)
753 first_popupwin = wp->w_next;
754 else
755 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200756 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200757 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200758 }
759
Bram Moolenaarec583842019-05-26 14:11:23 +0200760 // go through tab-local popups
761 FOR_ALL_TABPAGES(tp)
762 popup_close_tabpage(tp, id);
763}
764
765/*
766 * Close a popup window with Window-id "id" in tabpage "tp".
767 */
768 void
769popup_close_tabpage(tabpage_T *tp, int id)
770{
771 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200772 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200773 win_T *prev = NULL;
774
Bram Moolenaarec583842019-05-26 14:11:23 +0200775 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
776 if (wp->w_id == id)
777 {
778 if (prev == NULL)
779 *root = wp->w_next;
780 else
781 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200782 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200783 return;
784 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200785}
786
787 void
788close_all_popups(void)
789{
790 while (first_popupwin != NULL)
791 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200792 while (curtab->tp_first_popupwin != NULL)
793 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200794}
795
796 void
797ex_popupclear(exarg_T *eap UNUSED)
798{
799 close_all_popups();
800}
801
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200802/*
803 * popup_move({id}, {options})
804 */
805 void
806f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
807{
808 dict_T *d;
809 int nr;
810 int id = (int)tv_get_number(argvars);
811 win_T *wp = find_popup_win(id);
812
813 if (wp == NULL)
814 return; // invalid {id}
815
816 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
817 {
818 emsg(_(e_dictreq));
819 return;
820 }
821 d = argvars[1].vval.v_dict;
822
823 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
824 wp->w_minwidth = nr;
825 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
826 wp->w_minheight = nr;
827 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
828 wp->w_maxwidth = nr;
829 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
830 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200831 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200832
833 if (wp->w_winrow + wp->w_height >= cmdline_row)
834 clear_cmdline = TRUE;
835 popup_adjust_position(wp);
836 redraw_all_later(NOT_VALID);
837}
838
Bram Moolenaarbc133542019-05-29 20:26:46 +0200839/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200840 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200841 */
842 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200843f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200844{
845 dict_T *dict;
846 int id = (int)tv_get_number(argvars);
847 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200848 int top_extra;
849 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +0200850
851 if (rettv_dict_alloc(rettv) == OK)
852 {
853 if (wp == NULL)
854 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200855 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
856 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
857
Bram Moolenaarbc133542019-05-29 20:26:46 +0200858 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200859
Bram Moolenaarbc133542019-05-29 20:26:46 +0200860 dict_add_number(dict, "line", wp->w_winrow + 1);
861 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200862 dict_add_number(dict, "width", wp->w_width + left_extra + wp->w_popup_border[1] + wp->w_popup_padding[1]);
863 dict_add_number(dict, "height", wp->w_height + top_extra + wp->w_popup_border[2] + wp->w_popup_padding[2]);
864
865 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
866 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
867 dict_add_number(dict, "core_width", wp->w_width);
868 dict_add_number(dict, "core_height", wp->w_height);
869
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200870 dict_add_number(dict, "visible",
871 (wp->w_popup_flags & POPF_HIDDEN) == 0);
872 }
873}
874
875/*
876 * f_popup_getoptions({id})
877 */
878 void
879f_popup_getoptions(typval_T *argvars, typval_T *rettv)
880{
881 dict_T *dict;
882 int id = (int)tv_get_number(argvars);
883 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200884 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200885
886 if (rettv_dict_alloc(rettv) == OK)
887 {
888 if (wp == NULL)
889 return;
890
891 dict = rettv->vval.v_dict;
892 dict_add_number(dict, "line", wp->w_wantline);
893 dict_add_number(dict, "col", wp->w_wantcol);
894 dict_add_number(dict, "minwidth", wp->w_minwidth);
895 dict_add_number(dict, "minheight", wp->w_minheight);
896 dict_add_number(dict, "maxheight", wp->w_maxheight);
897 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
898 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200899
900 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
901 ++i)
902 if (wp->w_popup_pos == poppos_entries[i].pp_val)
903 {
904 dict_add_string(dict, "pos",
905 (char_u *)poppos_entries[i].pp_name);
906 break;
907 }
908
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200909# if defined(FEAT_TIMERS)
910 dict_add_number(dict, "time", wp->w_popup_timer != NULL
911 ? (long)wp->w_popup_timer->tr_interval : 0L);
912# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +0200913 }
914}
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200915
916 int
917not_in_popup_window()
918{
919 if (bt_popup(curwin->w_buffer))
920 {
921 emsg(_("E994: Not allowed in a popup window"));
922 return TRUE;
923 }
924 return FALSE;
925}
926
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200927/*
928 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
929 * in the current tab.
930 */
931 void
932popup_reset_handled()
933{
934 win_T *wp;
935
936 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
937 wp->w_popup_flags &= ~POPF_HANDLED;
938 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
939 wp->w_popup_flags &= ~POPF_HANDLED;
940}
941
942/*
943 * Find the next visible popup where POPF_HANDLED is not set.
944 * Must have called popup_reset_handled() first.
945 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
946 * popup with the highest zindex.
947 */
948 win_T *
949find_next_popup(int lowest)
950{
951 win_T *wp;
952 win_T *found_wp;
953 int found_zindex;
954
955 found_zindex = lowest ? INT_MAX : 0;
956 found_wp = NULL;
957 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
958 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
959 && (lowest ? wp->w_zindex < found_zindex
960 : wp->w_zindex > found_zindex))
961 {
962 found_zindex = wp->w_zindex;
963 found_wp = wp;
964 }
965 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
966 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
967 && (lowest ? wp->w_zindex < found_zindex
968 : wp->w_zindex > found_zindex))
969 {
970 found_zindex = wp->w_zindex;
971 found_wp = wp;
972 }
973
974 if (found_wp != NULL)
975 found_wp->w_popup_flags |= POPF_HANDLED;
976 return found_wp;
977}
978
979/*
980 * Invoke the filter callback for window "wp" with typed character "c".
981 * Uses the global "mod_mask" for modifiers.
982 * Returns the return value of the filter.
983 * Careful: The filter may make "wp" invalid!
984 */
985 static int
986invoke_popup_filter(win_T *wp, int c)
987{
988 int res;
989 typval_T rettv;
990 int dummy;
991 typval_T argv[3];
992 char_u buf[NUMBUFLEN];
993
994 argv[0].v_type = VAR_NUMBER;
995 argv[0].vval.v_number = (varnumber_T)wp->w_id;
996
997 // Convert the number to a string, so that the function can use:
998 // if a:c == "\<F2>"
999 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1000 argv[1].v_type = VAR_STRING;
1001 argv[1].vval.v_string = vim_strsave(buf);
1002
1003 argv[2].v_type = VAR_UNKNOWN;
1004
1005 call_callback(&wp->w_filter_cb, -1,
1006 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1007 res = tv_get_number(&rettv);
1008 vim_free(argv[1].vval.v_string);
1009 clear_tv(&rettv);
1010 return res;
1011}
1012
1013/*
1014 * Called when "c" was typed: invoke popup filter callbacks.
1015 * Returns TRUE when the character was consumed,
1016 */
1017 int
1018popup_do_filter(int c)
1019{
1020 int res = FALSE;
1021 win_T *wp;
1022
1023 popup_reset_handled();
1024
1025 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1026 if (wp->w_filter_cb.cb_name != NULL)
1027 res = invoke_popup_filter(wp, c);
1028
1029 return res;
1030}
1031
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001032#endif // FEAT_TEXT_PROP