blob: 810987aace23ffb22ed73f6dea6c34be480fdff1 [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
204 di = dict_find(dict, (char_u *)"filter", -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_filter_cb, &callback);
211 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200212
213 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
214 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200215
216 for (i = 0; i < 4; ++i)
217 VIM_CLEAR(wp->w_border_highlight[i]);
218 di = dict_find(dict, (char_u *)"borderhighlight", -1);
219 if (di != NULL)
220 {
221 if (di->di_tv.v_type != VAR_LIST)
222 emsg(_(e_listreq));
223 else
224 {
225 list_T *list = di->di_tv.vval.v_list;
226 listitem_T *li;
227
228 if (list != NULL)
229 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
230 ++i, li = li->li_next)
231 {
232 str = tv_get_string(&li->li_tv);
233 if (*str != NUL)
234 wp->w_border_highlight[i] = vim_strsave(str);
235 }
236 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
237 for (i = 1; i < 4; ++i)
238 wp->w_border_highlight[i] =
239 vim_strsave(wp->w_border_highlight[0]);
240 }
241 }
242
243 for (i = 0; i < 8; ++i)
244 wp->w_border_char[i] = 0;
245 di = dict_find(dict, (char_u *)"borderchars", -1);
246 if (di != NULL)
247 {
248 if (di->di_tv.v_type != VAR_LIST)
249 emsg(_(e_listreq));
250 else
251 {
252 list_T *list = di->di_tv.vval.v_list;
253 listitem_T *li;
254
255 if (list != NULL)
256 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
257 ++i, li = li->li_next)
258 {
259 str = tv_get_string(&li->li_tv);
260 if (*str != NUL)
261 wp->w_border_char[i] = mb_ptr2char(str);
262 }
263 if (list->lv_len == 1)
264 for (i = 1; i < 8; ++i)
265 wp->w_border_char[i] = wp->w_border_char[0];
266 if (list->lv_len == 2)
267 {
268 for (i = 4; i < 8; ++i)
269 wp->w_border_char[i] = wp->w_border_char[1];
270 for (i = 1; i < 4; ++i)
271 wp->w_border_char[i] = wp->w_border_char[0];
272 }
273 }
274 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200275}
276
277/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200278 * Add lines to the popup from a list of strings.
279 */
280 static void
281add_popup_strings(buf_T *buf, list_T *l)
282{
283 listitem_T *li;
284 linenr_T lnum = 0;
285 char_u *p;
286
287 for (li = l->lv_first; li != NULL; li = li->li_next)
288 if (li->li_tv.v_type == VAR_STRING)
289 {
290 p = li->li_tv.vval.v_string;
291 ml_append_buf(buf, lnum++,
292 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
293 }
294}
295
296/*
297 * Add lines to the popup from a list of dictionaries.
298 */
299 static void
300add_popup_dicts(buf_T *buf, list_T *l)
301{
302 listitem_T *li;
303 listitem_T *pli;
304 linenr_T lnum = 0;
305 char_u *p;
306 dict_T *dict;
307
308 // first add the text lines
309 for (li = l->lv_first; li != NULL; li = li->li_next)
310 {
311 if (li->li_tv.v_type != VAR_DICT)
312 {
313 emsg(_(e_dictreq));
314 return;
315 }
316 dict = li->li_tv.vval.v_dict;
317 p = dict == NULL ? NULL
318 : dict_get_string(dict, (char_u *)"text", FALSE);
319 ml_append_buf(buf, lnum++,
320 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
321 }
322
323 // add the text properties
324 lnum = 1;
325 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
326 {
327 dictitem_T *di;
328 list_T *plist;
329
330 dict = li->li_tv.vval.v_dict;
331 di = dict_find(dict, (char_u *)"props", -1);
332 if (di != NULL)
333 {
334 if (di->di_tv.v_type != VAR_LIST)
335 {
336 emsg(_(e_listreq));
337 return;
338 }
339 plist = di->di_tv.vval.v_list;
340 if (plist != NULL)
341 {
342 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
343 {
344 if (pli->li_tv.v_type != VAR_DICT)
345 {
346 emsg(_(e_dictreq));
347 return;
348 }
349 dict = pli->li_tv.vval.v_dict;
350 if (dict != NULL)
351 {
352 int col = dict_get_number(dict, (char_u *)"col");
353
354 prop_add_common( lnum, col, dict, buf, NULL);
355 }
356 }
357 }
358 }
359 }
360}
361
362/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200363 * Adjust the position and size of the popup to fit on the screen.
364 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200365 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200366popup_adjust_position(win_T *wp)
367{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200368 linenr_T lnum;
369 int wrapped = 0;
370 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200371 int center_vert = FALSE;
372 int center_hor = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200373
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200374 wp->w_winrow = 0;
375 wp->w_wincol = 0;
376 if (wp->w_popup_pos == POPPOS_CENTER)
377 {
378 // center after computing the size
379 center_vert = TRUE;
380 center_hor = TRUE;
381 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200382 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200383 {
384 if (wp->w_wantline == 0)
385 center_vert = TRUE;
386 else if (wp->w_popup_pos == POPPOS_TOPLEFT
387 || wp->w_popup_pos == POPPOS_TOPRIGHT)
388 {
389 wp->w_winrow = wp->w_wantline - 1;
390 if (wp->w_winrow >= Rows)
391 wp->w_winrow = Rows - 1;
392 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200393
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200394 if (wp->w_wantcol == 0)
395 center_hor = TRUE;
396 else if (wp->w_popup_pos == POPPOS_TOPLEFT
397 || wp->w_popup_pos == POPPOS_BOTLEFT)
398 {
399 wp->w_wincol = wp->w_wantcol - 1;
400 if (wp->w_wincol >= Columns - 3)
401 wp->w_wincol = Columns - 3;
402 }
403 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200404
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200405 // When centering or right aligned, use maximum width.
406 // When left aligned use the space available.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200407 maxwidth = Columns - wp->w_wincol;
408 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
409 maxwidth = wp->w_maxwidth;
410
411 // Compute width based on longest text line and the 'wrap' option.
412 // TODO: more accurate wrapping
413 wp->w_width = 0;
414 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
415 {
416 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
417
418 while (wp->w_p_wrap && len > maxwidth)
419 {
420 ++wrapped;
421 len -= maxwidth;
422 wp->w_width = maxwidth;
423 }
424 if (wp->w_width < len)
425 wp->w_width = len;
426 }
427
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200428 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
429 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200430 if (wp->w_width > maxwidth)
431 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200432 if (center_hor)
433 wp->w_wincol = (Columns - wp->w_width) / 2;
434 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
435 || wp->w_popup_pos == POPPOS_TOPRIGHT)
436 {
437 // Right aligned: move to the right if needed.
438 // No truncation, because that would change the height.
439 if (wp->w_width < wp->w_wantcol)
440 wp->w_wincol = wp->w_wantcol - wp->w_width;
441 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200442
443 if (wp->w_height <= 1)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200444 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200445 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
446 wp->w_height = wp->w_minheight;
447 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
448 wp->w_height = wp->w_maxheight;
449 if (wp->w_height > Rows - wp->w_winrow)
450 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200451
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200452 if (center_vert)
453 wp->w_winrow = (Rows - wp->w_height) / 2;
454 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
455 || wp->w_popup_pos == POPPOS_BOTLEFT)
456 {
457 if (wp->w_height <= wp->w_wantline)
458 // bottom aligned: may move down
459 wp->w_winrow = wp->w_wantline - wp->w_height;
460 else
461 // not enough space, make top aligned
462 wp->w_winrow = wp->w_wantline + 1;
463 }
464
Bram Moolenaar17146962019-05-30 00:12:11 +0200465 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200466}
467
468/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200469 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200470 * popup_atcursor({text}, {options})
471 * When called from f_popup_atcursor() "atcursor" is TRUE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200472 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200473 static void
474popup_create(typval_T *argvars, typval_T *rettv, int atcursor)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200475{
476 win_T *wp;
477 buf_T *buf;
478 dict_T *d;
479 int nr;
480
481 // Check arguments look OK.
482 if (!(argvars[0].v_type == VAR_STRING
483 && argvars[0].vval.v_string != NULL
484 && STRLEN(argvars[0].vval.v_string) > 0)
485 && !(argvars[0].v_type == VAR_LIST
486 && argvars[0].vval.v_list != NULL
487 && argvars[0].vval.v_list->lv_len > 0))
488 {
489 emsg(_(e_listreq));
490 return;
491 }
492 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
493 {
494 emsg(_(e_dictreq));
495 return;
496 }
497 d = argvars[1].vval.v_dict;
498
499 // Create the window and buffer.
500 wp = win_alloc_popup_win();
501 if (wp == NULL)
502 return;
503 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200504 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200505
506 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
507 if (buf == NULL)
508 return;
509 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200510
511 win_init_popup_win(wp, buf);
512
513 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200514 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200515 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200516 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200517 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200518 buf->b_p_ul = -1; // no undo
519 buf->b_p_swf = FALSE; // no swap file
520 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200521 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200522 wp->w_p_wrap = TRUE; // 'wrap' is default on
523
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200524 // Avoid that 'buftype' is reset when this buffer is entered.
525 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200526
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200527 nr = (int)dict_get_number(d, (char_u *)"tab");
528 if (nr == 0)
529 {
530 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200531 wp->w_next = curtab->tp_first_popupwin;
532 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200533 }
534 else if (nr < 0)
535 {
536 // global popup
537 wp->w_next = first_popupwin;
538 first_popupwin = wp;
539 }
540 else
541 // TODO: find tab page "nr"
542 emsg("Not implemented yet");
543
544 // Add text to the buffer.
545 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200546 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200547 // just a string
548 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200549 }
550 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200551 {
552 list_T *l = argvars[0].vval.v_list;
553
554 if (l->lv_first->li_tv.v_type == VAR_STRING)
555 // list of strings
556 add_popup_strings(buf, l);
557 else
558 // list of dictionaries
559 add_popup_dicts(buf, l);
560 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200561
562 // Delete the line of the empty buffer.
563 curbuf = buf;
564 ml_delete(buf->b_ml.ml_line_count, FALSE);
565 curbuf = curwin->w_buffer;
566
567 // Deal with options.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200568 apply_options(wp, buf, argvars[1].vval.v_dict, atcursor);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200569
570 // set default values
571 if (wp->w_zindex == 0)
572 wp->w_zindex = 50;
573
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200574 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200575
576 wp->w_vsep_width = 0;
577
578 redraw_all_later(NOT_VALID);
579}
580
581/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200582 * popup_create({text}, {options})
583 */
584 void
585f_popup_create(typval_T *argvars, typval_T *rettv)
586{
587 popup_create(argvars, rettv, FALSE);
588}
589
590/*
591 * popup_atcursor({text}, {options})
592 */
593 void
594f_popup_atcursor(typval_T *argvars, typval_T *rettv)
595{
596 popup_create(argvars, rettv, TRUE);
597}
598
599/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200600 * Find the popup window with window-ID "id".
601 * If the popup window does not exist NULL is returned.
602 * If the window is not a popup window, and error message is given.
603 */
604 static win_T *
605find_popup_win(int id)
606{
607 win_T *wp = win_id2wp(id);
608
609 if (wp != NULL && !bt_popup(wp->w_buffer))
610 {
611 semsg(_("E993: window %d is not a popup window"), id);
612 return NULL;
613 }
614 return wp;
615}
616
617/*
618 * Return TRUE if there any popups that are not hidden.
619 */
620 int
621popup_any_visible(void)
622{
623 win_T *wp;
624
625 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200626 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200627 return TRUE;
628 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200629 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200630 return TRUE;
631 return FALSE;
632}
633
634/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200635 * popup_close({id})
636 */
637 void
638f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
639{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200640 int id = (int)tv_get_number(argvars);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200641
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200642 popup_close(id);
643}
644
645/*
646 * popup_hide({id})
647 */
648 void
649f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
650{
651 int id = (int)tv_get_number(argvars);
652 win_T *wp = find_popup_win(id);
653
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200654 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200655 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200656 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200657 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200658 redraw_all_later(NOT_VALID);
659 }
660}
661
662/*
663 * popup_show({id})
664 */
665 void
666f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
667{
668 int id = (int)tv_get_number(argvars);
669 win_T *wp = find_popup_win(id);
670
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200671 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200672 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200673 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200674 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200675 redraw_all_later(NOT_VALID);
676 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200677}
678
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200679 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200680popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200681{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200682 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200683 if (wp->w_winrow + wp->w_height >= cmdline_row)
684 clear_cmdline = TRUE;
685 win_free_popup(wp);
686 redraw_all_later(NOT_VALID);
687}
688
Bram Moolenaarec583842019-05-26 14:11:23 +0200689/*
690 * Close a popup window by Window-id.
691 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200692 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200693popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200694{
695 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200696 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200697 win_T *prev = NULL;
698
Bram Moolenaarec583842019-05-26 14:11:23 +0200699 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200700 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200701 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200702 {
703 if (prev == NULL)
704 first_popupwin = wp->w_next;
705 else
706 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200707 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200708 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200709 }
710
Bram Moolenaarec583842019-05-26 14:11:23 +0200711 // go through tab-local popups
712 FOR_ALL_TABPAGES(tp)
713 popup_close_tabpage(tp, id);
714}
715
716/*
717 * Close a popup window with Window-id "id" in tabpage "tp".
718 */
719 void
720popup_close_tabpage(tabpage_T *tp, int id)
721{
722 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200723 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200724 win_T *prev = NULL;
725
Bram Moolenaarec583842019-05-26 14:11:23 +0200726 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
727 if (wp->w_id == id)
728 {
729 if (prev == NULL)
730 *root = wp->w_next;
731 else
732 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200733 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200734 return;
735 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200736}
737
738 void
739close_all_popups(void)
740{
741 while (first_popupwin != NULL)
742 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200743 while (curtab->tp_first_popupwin != NULL)
744 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200745}
746
747 void
748ex_popupclear(exarg_T *eap UNUSED)
749{
750 close_all_popups();
751}
752
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200753/*
754 * popup_move({id}, {options})
755 */
756 void
757f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
758{
759 dict_T *d;
760 int nr;
761 int id = (int)tv_get_number(argvars);
762 win_T *wp = find_popup_win(id);
763
764 if (wp == NULL)
765 return; // invalid {id}
766
767 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
768 {
769 emsg(_(e_dictreq));
770 return;
771 }
772 d = argvars[1].vval.v_dict;
773
774 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
775 wp->w_minwidth = nr;
776 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
777 wp->w_minheight = nr;
778 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
779 wp->w_maxwidth = nr;
780 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
781 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200782 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200783
784 if (wp->w_winrow + wp->w_height >= cmdline_row)
785 clear_cmdline = TRUE;
786 popup_adjust_position(wp);
787 redraw_all_later(NOT_VALID);
788}
789
Bram Moolenaarbc133542019-05-29 20:26:46 +0200790/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200791 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +0200792 */
793 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200794f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200795{
796 dict_T *dict;
797 int id = (int)tv_get_number(argvars);
798 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200799 int top_extra;
800 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +0200801
802 if (rettv_dict_alloc(rettv) == OK)
803 {
804 if (wp == NULL)
805 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200806 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
807 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
808
Bram Moolenaarbc133542019-05-29 20:26:46 +0200809 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200810
Bram Moolenaarbc133542019-05-29 20:26:46 +0200811 dict_add_number(dict, "line", wp->w_winrow + 1);
812 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200813 dict_add_number(dict, "width", wp->w_width + left_extra + wp->w_popup_border[1] + wp->w_popup_padding[1]);
814 dict_add_number(dict, "height", wp->w_height + top_extra + wp->w_popup_border[2] + wp->w_popup_padding[2]);
815
816 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
817 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
818 dict_add_number(dict, "core_width", wp->w_width);
819 dict_add_number(dict, "core_height", wp->w_height);
820
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200821 dict_add_number(dict, "visible",
822 (wp->w_popup_flags & POPF_HIDDEN) == 0);
823 }
824}
825
826/*
827 * f_popup_getoptions({id})
828 */
829 void
830f_popup_getoptions(typval_T *argvars, typval_T *rettv)
831{
832 dict_T *dict;
833 int id = (int)tv_get_number(argvars);
834 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200835 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200836
837 if (rettv_dict_alloc(rettv) == OK)
838 {
839 if (wp == NULL)
840 return;
841
842 dict = rettv->vval.v_dict;
843 dict_add_number(dict, "line", wp->w_wantline);
844 dict_add_number(dict, "col", wp->w_wantcol);
845 dict_add_number(dict, "minwidth", wp->w_minwidth);
846 dict_add_number(dict, "minheight", wp->w_minheight);
847 dict_add_number(dict, "maxheight", wp->w_maxheight);
848 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
849 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200850
851 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
852 ++i)
853 if (wp->w_popup_pos == poppos_entries[i].pp_val)
854 {
855 dict_add_string(dict, "pos",
856 (char_u *)poppos_entries[i].pp_name);
857 break;
858 }
859
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200860# if defined(FEAT_TIMERS)
861 dict_add_number(dict, "time", wp->w_popup_timer != NULL
862 ? (long)wp->w_popup_timer->tr_interval : 0L);
863# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +0200864 }
865}
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200866
867 int
868not_in_popup_window()
869{
870 if (bt_popup(curwin->w_buffer))
871 {
872 emsg(_("E994: Not allowed in a popup window"));
873 return TRUE;
874 }
875 return FALSE;
876}
877
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200878/*
879 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
880 * in the current tab.
881 */
882 void
883popup_reset_handled()
884{
885 win_T *wp;
886
887 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
888 wp->w_popup_flags &= ~POPF_HANDLED;
889 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
890 wp->w_popup_flags &= ~POPF_HANDLED;
891}
892
893/*
894 * Find the next visible popup where POPF_HANDLED is not set.
895 * Must have called popup_reset_handled() first.
896 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
897 * popup with the highest zindex.
898 */
899 win_T *
900find_next_popup(int lowest)
901{
902 win_T *wp;
903 win_T *found_wp;
904 int found_zindex;
905
906 found_zindex = lowest ? INT_MAX : 0;
907 found_wp = NULL;
908 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
909 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
910 && (lowest ? wp->w_zindex < found_zindex
911 : wp->w_zindex > found_zindex))
912 {
913 found_zindex = wp->w_zindex;
914 found_wp = wp;
915 }
916 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
917 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
918 && (lowest ? wp->w_zindex < found_zindex
919 : wp->w_zindex > found_zindex))
920 {
921 found_zindex = wp->w_zindex;
922 found_wp = wp;
923 }
924
925 if (found_wp != NULL)
926 found_wp->w_popup_flags |= POPF_HANDLED;
927 return found_wp;
928}
929
930/*
931 * Invoke the filter callback for window "wp" with typed character "c".
932 * Uses the global "mod_mask" for modifiers.
933 * Returns the return value of the filter.
934 * Careful: The filter may make "wp" invalid!
935 */
936 static int
937invoke_popup_filter(win_T *wp, int c)
938{
939 int res;
940 typval_T rettv;
941 int dummy;
942 typval_T argv[3];
943 char_u buf[NUMBUFLEN];
944
945 argv[0].v_type = VAR_NUMBER;
946 argv[0].vval.v_number = (varnumber_T)wp->w_id;
947
948 // Convert the number to a string, so that the function can use:
949 // if a:c == "\<F2>"
950 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
951 argv[1].v_type = VAR_STRING;
952 argv[1].vval.v_string = vim_strsave(buf);
953
954 argv[2].v_type = VAR_UNKNOWN;
955
956 call_callback(&wp->w_filter_cb, -1,
957 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
958 res = tv_get_number(&rettv);
959 vim_free(argv[1].vval.v_string);
960 clear_tv(&rettv);
961 return res;
962}
963
964/*
965 * Called when "c" was typed: invoke popup filter callbacks.
966 * Returns TRUE when the character was consumed,
967 */
968 int
969popup_do_filter(int c)
970{
971 int res = FALSE;
972 win_T *wp;
973
974 popup_reset_handled();
975
976 while (!res && (wp = find_next_popup(FALSE)) != NULL)
977 if (wp->w_filter_cb.cb_name != NULL)
978 res = invoke_popup_filter(wp, c);
979
980 return res;
981}
982
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200983#endif // FEAT_TEXT_PROP