blob: 64309ff8b794bf0efbae80d927444fb22a104c54 [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/*
19 * Go through the options in "dict" and apply them to buffer "buf" displayed in
20 * popup window "wp".
21 */
22 static void
23apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict)
24{
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020025 int nr;
Bram Moolenaar20c023a2019-05-26 21:03:24 +020026 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020027
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
29 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +020030 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
31 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +020032
33 wp->w_wantline = dict_get_number(dict, (char_u *)"line");
34 wp->w_wantcol = dict_get_number(dict, (char_u *)"col");
35
Bram Moolenaar4d784b22019-05-25 19:51:39 +020036 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020037
Bram Moolenaar35d5af62019-05-26 20:44:10 +020038#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020039 // Add timer to close the popup after some time.
40 nr = dict_get_number(dict, (char_u *)"time");
41 if (nr > 0)
42 {
43 char_u cbbuf[50];
44 char_u *ptr = cbbuf;
45 typval_T tv;
46
47 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
48 "{_ -> popup_close(%d)}", wp->w_id);
49 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
50 {
51 wp->w_popup_timer = create_timer(nr, 0);
52 wp->w_popup_timer->tr_callback =
53 vim_strsave(partial_name(tv.vval.v_partial));
54 func_ref(wp->w_popup_timer->tr_callback);
55 wp->w_popup_timer->tr_partial = tv.vval.v_partial;
56 }
57 }
Bram Moolenaar35d5af62019-05-26 20:44:10 +020058#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020059
Bram Moolenaar20c023a2019-05-26 21:03:24 +020060 str = dict_get_string(dict, (char_u *)"highlight", TRUE);
61 if (str != NULL)
62 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
63 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +020064}
65
66/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020067 * Add lines to the popup from a list of strings.
68 */
69 static void
70add_popup_strings(buf_T *buf, list_T *l)
71{
72 listitem_T *li;
73 linenr_T lnum = 0;
74 char_u *p;
75
76 for (li = l->lv_first; li != NULL; li = li->li_next)
77 if (li->li_tv.v_type == VAR_STRING)
78 {
79 p = li->li_tv.vval.v_string;
80 ml_append_buf(buf, lnum++,
81 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
82 }
83}
84
85/*
86 * Add lines to the popup from a list of dictionaries.
87 */
88 static void
89add_popup_dicts(buf_T *buf, list_T *l)
90{
91 listitem_T *li;
92 listitem_T *pli;
93 linenr_T lnum = 0;
94 char_u *p;
95 dict_T *dict;
96
97 // first add the text lines
98 for (li = l->lv_first; li != NULL; li = li->li_next)
99 {
100 if (li->li_tv.v_type != VAR_DICT)
101 {
102 emsg(_(e_dictreq));
103 return;
104 }
105 dict = li->li_tv.vval.v_dict;
106 p = dict == NULL ? NULL
107 : dict_get_string(dict, (char_u *)"text", FALSE);
108 ml_append_buf(buf, lnum++,
109 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
110 }
111
112 // add the text properties
113 lnum = 1;
114 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
115 {
116 dictitem_T *di;
117 list_T *plist;
118
119 dict = li->li_tv.vval.v_dict;
120 di = dict_find(dict, (char_u *)"props", -1);
121 if (di != NULL)
122 {
123 if (di->di_tv.v_type != VAR_LIST)
124 {
125 emsg(_(e_listreq));
126 return;
127 }
128 plist = di->di_tv.vval.v_list;
129 if (plist != NULL)
130 {
131 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
132 {
133 if (pli->li_tv.v_type != VAR_DICT)
134 {
135 emsg(_(e_dictreq));
136 return;
137 }
138 dict = pli->li_tv.vval.v_dict;
139 if (dict != NULL)
140 {
141 int col = dict_get_number(dict, (char_u *)"col");
142
143 prop_add_common( lnum, col, dict, buf, NULL);
144 }
145 }
146 }
147 }
148 }
149}
150
151/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200152 * Adjust the position and size of the popup to fit on the screen.
153 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200154 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200155popup_adjust_position(win_T *wp)
156{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200157 linenr_T lnum;
158 int wrapped = 0;
159 int maxwidth;
160
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200161 // TODO: Compute the size and position properly.
162 if (wp->w_wantline > 0)
163 wp->w_winrow = wp->w_wantline - 1;
164 else
165 // TODO: better default
166 wp->w_winrow = Rows > 5 ? Rows / 2 - 2 : 0;
167 if (wp->w_winrow >= Rows)
168 wp->w_winrow = Rows - 1;
169
170 if (wp->w_wantcol > 0)
171 wp->w_wincol = wp->w_wantcol - 1;
172 else
173 // TODO: better default
174 wp->w_wincol = Columns > 20 ? Columns / 2 - 10 : 0;
175 if (wp->w_wincol >= Columns - 3)
176 wp->w_wincol = Columns - 3;
177
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200178 maxwidth = Columns - wp->w_wincol;
179 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
180 maxwidth = wp->w_maxwidth;
181
182 // Compute width based on longest text line and the 'wrap' option.
183 // TODO: more accurate wrapping
184 wp->w_width = 0;
185 for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
186 {
187 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
188
189 while (wp->w_p_wrap && len > maxwidth)
190 {
191 ++wrapped;
192 len -= maxwidth;
193 wp->w_width = maxwidth;
194 }
195 if (wp->w_width < len)
196 wp->w_width = len;
197 }
198
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200199 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
200 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200201 if (wp->w_width > maxwidth)
202 wp->w_width = maxwidth;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200203
204 if (wp->w_height <= 1)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200205 wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200206 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
207 wp->w_height = wp->w_minheight;
208 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
209 wp->w_height = wp->w_maxheight;
210 if (wp->w_height > Rows - wp->w_winrow)
211 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200212
213 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200214}
215
216/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200217 * popup_create({text}, {options})
218 */
219 void
220f_popup_create(typval_T *argvars, typval_T *rettv)
221{
222 win_T *wp;
223 buf_T *buf;
224 dict_T *d;
225 int nr;
226
227 // Check arguments look OK.
228 if (!(argvars[0].v_type == VAR_STRING
229 && argvars[0].vval.v_string != NULL
230 && STRLEN(argvars[0].vval.v_string) > 0)
231 && !(argvars[0].v_type == VAR_LIST
232 && argvars[0].vval.v_list != NULL
233 && argvars[0].vval.v_list->lv_len > 0))
234 {
235 emsg(_(e_listreq));
236 return;
237 }
238 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
239 {
240 emsg(_(e_dictreq));
241 return;
242 }
243 d = argvars[1].vval.v_dict;
244
245 // Create the window and buffer.
246 wp = win_alloc_popup_win();
247 if (wp == NULL)
248 return;
249 rettv->vval.v_number = wp->w_id;
250 wp->w_p_wrap = TRUE; // 'wrap' is default on
251
252 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
253 if (buf == NULL)
254 return;
255 ml_open(buf);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200256 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200257 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200258 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200259 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200260 buf->b_p_ul = -1; // no undo
261 buf->b_p_swf = FALSE; // no swap file
262 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200263 buf->b_locked = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200264
265 win_init_popup_win(wp, buf);
266
267 nr = (int)dict_get_number(d, (char_u *)"tab");
268 if (nr == 0)
269 {
270 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200271 wp->w_next = curtab->tp_first_popupwin;
272 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200273 }
274 else if (nr < 0)
275 {
276 // global popup
277 wp->w_next = first_popupwin;
278 first_popupwin = wp;
279 }
280 else
281 // TODO: find tab page "nr"
282 emsg("Not implemented yet");
283
284 // Add text to the buffer.
285 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200286 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200287 // just a string
288 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200289 }
290 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200291 {
292 list_T *l = argvars[0].vval.v_list;
293
294 if (l->lv_first->li_tv.v_type == VAR_STRING)
295 // list of strings
296 add_popup_strings(buf, l);
297 else
298 // list of dictionaries
299 add_popup_dicts(buf, l);
300 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200301
302 // Delete the line of the empty buffer.
303 curbuf = buf;
304 ml_delete(buf->b_ml.ml_line_count, FALSE);
305 curbuf = curwin->w_buffer;
306
307 // Deal with options.
308 apply_options(wp, buf, argvars[1].vval.v_dict);
309
310 // set default values
311 if (wp->w_zindex == 0)
312 wp->w_zindex = 50;
313
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200314 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200315
316 wp->w_vsep_width = 0;
317
318 redraw_all_later(NOT_VALID);
319}
320
321/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200322 * Find the popup window with window-ID "id".
323 * If the popup window does not exist NULL is returned.
324 * If the window is not a popup window, and error message is given.
325 */
326 static win_T *
327find_popup_win(int id)
328{
329 win_T *wp = win_id2wp(id);
330
331 if (wp != NULL && !bt_popup(wp->w_buffer))
332 {
333 semsg(_("E993: window %d is not a popup window"), id);
334 return NULL;
335 }
336 return wp;
337}
338
339/*
340 * Return TRUE if there any popups that are not hidden.
341 */
342 int
343popup_any_visible(void)
344{
345 win_T *wp;
346
347 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200348 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200349 return TRUE;
350 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200351 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200352 return TRUE;
353 return FALSE;
354}
355
356/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200357 * popup_close({id})
358 */
359 void
360f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
361{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200362 int id = (int)tv_get_number(argvars);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200363
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200364 popup_close(id);
365}
366
367/*
368 * popup_hide({id})
369 */
370 void
371f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
372{
373 int id = (int)tv_get_number(argvars);
374 win_T *wp = find_popup_win(id);
375
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200376 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200377 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200378 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200379 redraw_all_later(NOT_VALID);
380 }
381}
382
383/*
384 * popup_show({id})
385 */
386 void
387f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
388{
389 int id = (int)tv_get_number(argvars);
390 win_T *wp = find_popup_win(id);
391
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200392 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200393 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200394 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200395 redraw_all_later(NOT_VALID);
396 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200397}
398
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200399 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200400popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200401{
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200402 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200403 if (wp->w_winrow + wp->w_height >= cmdline_row)
404 clear_cmdline = TRUE;
405 win_free_popup(wp);
406 redraw_all_later(NOT_VALID);
407}
408
Bram Moolenaarec583842019-05-26 14:11:23 +0200409/*
410 * Close a popup window by Window-id.
411 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200412 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200413popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200414{
415 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200416 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200417 win_T *prev = NULL;
418
Bram Moolenaarec583842019-05-26 14:11:23 +0200419 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200420 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200421 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200422 {
423 if (prev == NULL)
424 first_popupwin = wp->w_next;
425 else
426 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200427 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200428 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200429 }
430
Bram Moolenaarec583842019-05-26 14:11:23 +0200431 // go through tab-local popups
432 FOR_ALL_TABPAGES(tp)
433 popup_close_tabpage(tp, id);
434}
435
436/*
437 * Close a popup window with Window-id "id" in tabpage "tp".
438 */
439 void
440popup_close_tabpage(tabpage_T *tp, int id)
441{
442 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200443 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200444 win_T *prev = NULL;
445
Bram Moolenaarec583842019-05-26 14:11:23 +0200446 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
447 if (wp->w_id == id)
448 {
449 if (prev == NULL)
450 *root = wp->w_next;
451 else
452 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200453 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200454 return;
455 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200456}
457
458 void
459close_all_popups(void)
460{
461 while (first_popupwin != NULL)
462 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200463 while (curtab->tp_first_popupwin != NULL)
464 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200465}
466
467 void
468ex_popupclear(exarg_T *eap UNUSED)
469{
470 close_all_popups();
471}
472
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200473/*
474 * popup_move({id}, {options})
475 */
476 void
477f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
478{
479 dict_T *d;
480 int nr;
481 int id = (int)tv_get_number(argvars);
482 win_T *wp = find_popup_win(id);
483
484 if (wp == NULL)
485 return; // invalid {id}
486
487 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
488 {
489 emsg(_(e_dictreq));
490 return;
491 }
492 d = argvars[1].vval.v_dict;
493
494 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
495 wp->w_minwidth = nr;
496 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
497 wp->w_minheight = nr;
498 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
499 wp->w_maxwidth = nr;
500 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
501 wp->w_maxheight = nr;
502 if ((nr = dict_get_number(d, (char_u *)"line")) > 0)
503 wp->w_wantline = nr;
504 if ((nr = dict_get_number(d, (char_u *)"col")) > 0)
505 wp->w_wantcol = nr;
506 // TODO: "pos"
507
508 if (wp->w_winrow + wp->w_height >= cmdline_row)
509 clear_cmdline = TRUE;
510 popup_adjust_position(wp);
511 redraw_all_later(NOT_VALID);
512}
513
Bram Moolenaarbc133542019-05-29 20:26:46 +0200514/*
515 * popup_getposition({id})
516 */
517 void
518f_popup_getposition(typval_T *argvars, typval_T *rettv)
519{
520 dict_T *dict;
521 int id = (int)tv_get_number(argvars);
522 win_T *wp = find_popup_win(id);
523
524 if (rettv_dict_alloc(rettv) == OK)
525 {
526 if (wp == NULL)
527 return; // invalid {id}
528 dict = rettv->vval.v_dict;
529 dict_add_number(dict, "line", wp->w_winrow + 1);
530 dict_add_number(dict, "col", wp->w_wincol + 1);
531 dict_add_number(dict, "width", wp->w_width);
532 dict_add_number(dict, "height", wp->w_height);
533 }
534}
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200535#endif // FEAT_TEXT_PROP