blob: 9e9bf27e7fee2a0d39427fccc26ade37b18b0dfc [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
18/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020019 * Get option value for"key", which is "line" or "col".
20 * Handles "cursor+N" and "cursor-N".
21 */
22 static int
23popup_options_pos(dict_T *dict, char_u *key)
24{
25 dictitem_T *di;
26 char_u *val;
27 char_u *s;
28 char_u *endp;
29 int n = 0;
30
31 di = dict_find(dict, key, -1);
32 if (di == NULL)
33 return 0;
34
35 val = tv_get_string(&di->di_tv);
36 if (STRNCMP(val, "cursor", 6) != 0)
37 return dict_get_number(dict, key);
38
39 setcursor_mayforce(TRUE);
40 s = val + 6;
41 if (*s != NUL)
42 {
43 n = strtol((char *)s, (char **)&endp, 10);
44 if (endp != NULL && *skipwhite(endp) != NUL)
45 {
46 semsg(_(e_invexpr2), val);
47 return 0;
48 }
49 }
50
51 if (STRCMP(key, "line") == 0)
52 n = screen_screenrow() + 1 + n;
53 else // "col"
54 n = screen_screencol() + 1 + n;
55
56 if (n < 1)
57 n = 1;
58 return n;
59}
60
61/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +020062 * Go through the options in "dict" and apply them to buffer "buf" displayed in
63 * popup window "wp".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020064 * When called from f_popup_atcursor() "atcursor" is TRUE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +020065 */
66 static void
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020067apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict, int atcursor)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020068{
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020069 int nr;
Bram Moolenaar20c023a2019-05-26 21:03:24 +020070 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020071
Bram Moolenaar60cdb302019-05-27 21:54:10 +020072 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
73 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
75 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +020076
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 if (atcursor)
78 {
79 setcursor_mayforce(TRUE);
80 wp->w_wantline = screen_screenrow();
81 wp->w_wantcol = screen_screencol() + 1;
82 }
83
84 nr = popup_options_pos(dict, (char_u *)"line");
85 if (nr > 0)
86 wp->w_wantline = nr;
87 nr = popup_options_pos(dict, (char_u *)"col");
88 if (nr > 0)
89 wp->w_wantcol = nr;
Bram Moolenaar60cdb302019-05-27 21:54:10 +020090
Bram Moolenaar4d784b22019-05-25 19:51:39 +020091 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020092
Bram Moolenaar35d5af62019-05-26 20:44:10 +020093#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020094 // Add timer to close the popup after some time.
95 nr = dict_get_number(dict, (char_u *)"time");
96 if (nr > 0)
97 {
98 char_u cbbuf[50];
99 char_u *ptr = cbbuf;
100 typval_T tv;
101
102 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
103 "{_ -> popup_close(%d)}", wp->w_id);
104 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
105 {
106 wp->w_popup_timer = create_timer(nr, 0);
107 wp->w_popup_timer->tr_callback =
108 vim_strsave(partial_name(tv.vval.v_partial));
109 func_ref(wp->w_popup_timer->tr_callback);
110 wp->w_popup_timer->tr_partial = tv.vval.v_partial;
111 }
112 }
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200113#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200114
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200115 str = dict_get_string(dict, (char_u *)"highlight", TRUE);
116 if (str != NULL)
117 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
118 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200119}
120
121/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200122 * Add lines to the popup from a list of strings.
123 */
124 static void
125add_popup_strings(buf_T *buf, list_T *l)
126{
127 listitem_T *li;
128 linenr_T lnum = 0;
129 char_u *p;
130
131 for (li = l->lv_first; li != NULL; li = li->li_next)
132 if (li->li_tv.v_type == VAR_STRING)
133 {
134 p = li->li_tv.vval.v_string;
135 ml_append_buf(buf, lnum++,
136 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
137 }
138}
139
140/*
141 * Add lines to the popup from a list of dictionaries.
142 */
143 static void
144add_popup_dicts(buf_T *buf, list_T *l)
145{
146 listitem_T *li;
147 listitem_T *pli;
148 linenr_T lnum = 0;
149 char_u *p;
150 dict_T *dict;
151
152 // first add the text lines
153 for (li = l->lv_first; li != NULL; li = li->li_next)
154 {
155 if (li->li_tv.v_type != VAR_DICT)
156 {
157 emsg(_(e_dictreq));
158 return;
159 }
160 dict = li->li_tv.vval.v_dict;
161 p = dict == NULL ? NULL
162 : dict_get_string(dict, (char_u *)"text", FALSE);
163 ml_append_buf(buf, lnum++,
164 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
165 }
166
167 // add the text properties
168 lnum = 1;
169 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
170 {
171 dictitem_T *di;
172 list_T *plist;
173
174 dict = li->li_tv.vval.v_dict;
175 di = dict_find(dict, (char_u *)"props", -1);
176 if (di != NULL)
177 {
178 if (di->di_tv.v_type != VAR_LIST)
179 {
180 emsg(_(e_listreq));
181 return;
182 }
183 plist = di->di_tv.vval.v_list;
184 if (plist != NULL)
185 {
186 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
187 {
188 if (pli->li_tv.v_type != VAR_DICT)
189 {
190 emsg(_(e_dictreq));
191 return;
192 }
193 dict = pli->li_tv.vval.v_dict;
194 if (dict != NULL)
195 {
196 int col = dict_get_number(dict, (char_u *)"col");
197
198 prop_add_common( lnum, col, dict, buf, NULL);
199 }
200 }
201 }
202 }
203 }
204}
205
206/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200207 * Adjust the position and size of the popup to fit on the screen.
208 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200209 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200210popup_adjust_position(win_T *wp)
211{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200212 linenr_T lnum;
213 int wrapped = 0;
214 int maxwidth;
215
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200216 // TODO: Compute the size and position properly.
217 if (wp->w_wantline > 0)
218 wp->w_winrow = wp->w_wantline - 1;
219 else
220 // TODO: better default
221 wp->w_winrow = Rows > 5 ? Rows / 2 - 2 : 0;
222 if (wp->w_winrow >= Rows)
223 wp->w_winrow = Rows - 1;
224
225 if (wp->w_wantcol > 0)
226 wp->w_wincol = wp->w_wantcol - 1;
227 else
228 // TODO: better default
229 wp->w_wincol = Columns > 20 ? Columns / 2 - 10 : 0;
230 if (wp->w_wincol >= Columns - 3)
231 wp->w_wincol = Columns - 3;
232
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200233 maxwidth = Columns - wp->w_wincol;
234 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
235 maxwidth = wp->w_maxwidth;
236
237 // Compute width based on longest text line and the 'wrap' option.
238 // TODO: more accurate wrapping
239 wp->w_width = 0;
240 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
241 {
242 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
243
244 while (wp->w_p_wrap && len > maxwidth)
245 {
246 ++wrapped;
247 len -= maxwidth;
248 wp->w_width = maxwidth;
249 }
250 if (wp->w_width < len)
251 wp->w_width = len;
252 }
253
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200254 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
255 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200256 if (wp->w_width > maxwidth)
257 wp->w_width = maxwidth;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200258
259 if (wp->w_height <= 1)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200260 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200261 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
262 wp->w_height = wp->w_minheight;
263 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
264 wp->w_height = wp->w_maxheight;
265 if (wp->w_height > Rows - wp->w_winrow)
266 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200267
268 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200269}
270
271/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200272 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200273 * popup_atcursor({text}, {options})
274 * When called from f_popup_atcursor() "atcursor" is TRUE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200275 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200276 static void
277popup_create(typval_T *argvars, typval_T *rettv, int atcursor)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200278{
279 win_T *wp;
280 buf_T *buf;
281 dict_T *d;
282 int nr;
283
284 // Check arguments look OK.
285 if (!(argvars[0].v_type == VAR_STRING
286 && argvars[0].vval.v_string != NULL
287 && STRLEN(argvars[0].vval.v_string) > 0)
288 && !(argvars[0].v_type == VAR_LIST
289 && argvars[0].vval.v_list != NULL
290 && argvars[0].vval.v_list->lv_len > 0))
291 {
292 emsg(_(e_listreq));
293 return;
294 }
295 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
296 {
297 emsg(_(e_dictreq));
298 return;
299 }
300 d = argvars[1].vval.v_dict;
301
302 // Create the window and buffer.
303 wp = win_alloc_popup_win();
304 if (wp == NULL)
305 return;
306 rettv->vval.v_number = wp->w_id;
307 wp->w_p_wrap = TRUE; // 'wrap' is default on
308
309 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
310 if (buf == NULL)
311 return;
312 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200313
314 win_init_popup_win(wp, buf);
315
316 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200317 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200318 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200319 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200320 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200321 buf->b_p_ul = -1; // no undo
322 buf->b_p_swf = FALSE; // no swap file
323 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200324 buf->b_locked = TRUE;
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200325 // Avoid that 'buftype' is reset when this buffer is entered.
326 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200327
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200328 nr = (int)dict_get_number(d, (char_u *)"tab");
329 if (nr == 0)
330 {
331 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200332 wp->w_next = curtab->tp_first_popupwin;
333 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200334 }
335 else if (nr < 0)
336 {
337 // global popup
338 wp->w_next = first_popupwin;
339 first_popupwin = wp;
340 }
341 else
342 // TODO: find tab page "nr"
343 emsg("Not implemented yet");
344
345 // Add text to the buffer.
346 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200347 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200348 // just a string
349 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200350 }
351 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200352 {
353 list_T *l = argvars[0].vval.v_list;
354
355 if (l->lv_first->li_tv.v_type == VAR_STRING)
356 // list of strings
357 add_popup_strings(buf, l);
358 else
359 // list of dictionaries
360 add_popup_dicts(buf, l);
361 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200362
363 // Delete the line of the empty buffer.
364 curbuf = buf;
365 ml_delete(buf->b_ml.ml_line_count, FALSE);
366 curbuf = curwin->w_buffer;
367
368 // Deal with options.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200369 apply_options(wp, buf, argvars[1].vval.v_dict, atcursor);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200370
371 // set default values
372 if (wp->w_zindex == 0)
373 wp->w_zindex = 50;
374
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200375 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200376
377 wp->w_vsep_width = 0;
378
379 redraw_all_later(NOT_VALID);
380}
381
382/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200383 * popup_create({text}, {options})
384 */
385 void
386f_popup_create(typval_T *argvars, typval_T *rettv)
387{
388 popup_create(argvars, rettv, FALSE);
389}
390
391/*
392 * popup_atcursor({text}, {options})
393 */
394 void
395f_popup_atcursor(typval_T *argvars, typval_T *rettv)
396{
397 popup_create(argvars, rettv, TRUE);
398}
399
400/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200401 * Find the popup window with window-ID "id".
402 * If the popup window does not exist NULL is returned.
403 * If the window is not a popup window, and error message is given.
404 */
405 static win_T *
406find_popup_win(int id)
407{
408 win_T *wp = win_id2wp(id);
409
410 if (wp != NULL && !bt_popup(wp->w_buffer))
411 {
412 semsg(_("E993: window %d is not a popup window"), id);
413 return NULL;
414 }
415 return wp;
416}
417
418/*
419 * Return TRUE if there any popups that are not hidden.
420 */
421 int
422popup_any_visible(void)
423{
424 win_T *wp;
425
426 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200427 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200428 return TRUE;
429 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200430 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200431 return TRUE;
432 return FALSE;
433}
434
435/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200436 * popup_close({id})
437 */
438 void
439f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
440{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200441 int id = (int)tv_get_number(argvars);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200442
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200443 popup_close(id);
444}
445
446/*
447 * popup_hide({id})
448 */
449 void
450f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
451{
452 int id = (int)tv_get_number(argvars);
453 win_T *wp = find_popup_win(id);
454
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200455 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200456 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200457 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200458 redraw_all_later(NOT_VALID);
459 }
460}
461
462/*
463 * popup_show({id})
464 */
465 void
466f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
467{
468 int id = (int)tv_get_number(argvars);
469 win_T *wp = find_popup_win(id);
470
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200471 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200472 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200473 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200474 redraw_all_later(NOT_VALID);
475 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200476}
477
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200478 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200479popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200480{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200481 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200482 if (wp->w_winrow + wp->w_height >= cmdline_row)
483 clear_cmdline = TRUE;
484 win_free_popup(wp);
485 redraw_all_later(NOT_VALID);
486}
487
Bram Moolenaarec583842019-05-26 14:11:23 +0200488/*
489 * Close a popup window by Window-id.
490 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200491 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200492popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200493{
494 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200495 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200496 win_T *prev = NULL;
497
Bram Moolenaarec583842019-05-26 14:11:23 +0200498 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200499 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200500 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200501 {
502 if (prev == NULL)
503 first_popupwin = wp->w_next;
504 else
505 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200506 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200507 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200508 }
509
Bram Moolenaarec583842019-05-26 14:11:23 +0200510 // go through tab-local popups
511 FOR_ALL_TABPAGES(tp)
512 popup_close_tabpage(tp, id);
513}
514
515/*
516 * Close a popup window with Window-id "id" in tabpage "tp".
517 */
518 void
519popup_close_tabpage(tabpage_T *tp, int id)
520{
521 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200522 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200523 win_T *prev = NULL;
524
Bram Moolenaarec583842019-05-26 14:11:23 +0200525 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
526 if (wp->w_id == id)
527 {
528 if (prev == NULL)
529 *root = wp->w_next;
530 else
531 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200532 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200533 return;
534 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200535}
536
537 void
538close_all_popups(void)
539{
540 while (first_popupwin != NULL)
541 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200542 while (curtab->tp_first_popupwin != NULL)
543 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200544}
545
546 void
547ex_popupclear(exarg_T *eap UNUSED)
548{
549 close_all_popups();
550}
551
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200552/*
553 * popup_move({id}, {options})
554 */
555 void
556f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
557{
558 dict_T *d;
559 int nr;
560 int id = (int)tv_get_number(argvars);
561 win_T *wp = find_popup_win(id);
562
563 if (wp == NULL)
564 return; // invalid {id}
565
566 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
567 {
568 emsg(_(e_dictreq));
569 return;
570 }
571 d = argvars[1].vval.v_dict;
572
573 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
574 wp->w_minwidth = nr;
575 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
576 wp->w_minheight = nr;
577 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
578 wp->w_maxwidth = nr;
579 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
580 wp->w_maxheight = nr;
581 if ((nr = dict_get_number(d, (char_u *)"line")) > 0)
582 wp->w_wantline = nr;
583 if ((nr = dict_get_number(d, (char_u *)"col")) > 0)
584 wp->w_wantcol = nr;
585 // TODO: "pos"
586
587 if (wp->w_winrow + wp->w_height >= cmdline_row)
588 clear_cmdline = TRUE;
589 popup_adjust_position(wp);
590 redraw_all_later(NOT_VALID);
591}
592
Bram Moolenaarbc133542019-05-29 20:26:46 +0200593/*
594 * popup_getposition({id})
595 */
596 void
597f_popup_getposition(typval_T *argvars, typval_T *rettv)
598{
599 dict_T *dict;
600 int id = (int)tv_get_number(argvars);
601 win_T *wp = find_popup_win(id);
602
603 if (rettv_dict_alloc(rettv) == OK)
604 {
605 if (wp == NULL)
606 return; // invalid {id}
607 dict = rettv->vval.v_dict;
608 dict_add_number(dict, "line", wp->w_winrow + 1);
609 dict_add_number(dict, "col", wp->w_wincol + 1);
610 dict_add_number(dict, "width", wp->w_width);
611 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200612 dict_add_number(dict, "visible",
613 (wp->w_popup_flags & POPF_HIDDEN) == 0);
614 }
615}
616
617/*
618 * f_popup_getoptions({id})
619 */
620 void
621f_popup_getoptions(typval_T *argvars, typval_T *rettv)
622{
623 dict_T *dict;
624 int id = (int)tv_get_number(argvars);
625 win_T *wp = find_popup_win(id);
626
627 if (rettv_dict_alloc(rettv) == OK)
628 {
629 if (wp == NULL)
630 return;
631
632 dict = rettv->vval.v_dict;
633 dict_add_number(dict, "line", wp->w_wantline);
634 dict_add_number(dict, "col", wp->w_wantcol);
635 dict_add_number(dict, "minwidth", wp->w_minwidth);
636 dict_add_number(dict, "minheight", wp->w_minheight);
637 dict_add_number(dict, "maxheight", wp->w_maxheight);
638 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
639 dict_add_number(dict, "zindex", wp->w_zindex);
640# if defined(FEAT_TIMERS)
641 dict_add_number(dict, "time", wp->w_popup_timer != NULL
642 ? (long)wp->w_popup_timer->tr_interval : 0L);
643# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +0200644 }
645}
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200646#endif // FEAT_TEXT_PROP