blob: 9970c5afe9957480774c7a14a19cfe4f448cbd4c [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 */
154 static void
155popup_adjust_position(win_T *wp)
156{
157 // TODO: Compute the size and position properly.
158 if (wp->w_wantline > 0)
159 wp->w_winrow = wp->w_wantline - 1;
160 else
161 // TODO: better default
162 wp->w_winrow = Rows > 5 ? Rows / 2 - 2 : 0;
163 if (wp->w_winrow >= Rows)
164 wp->w_winrow = Rows - 1;
165
166 if (wp->w_wantcol > 0)
167 wp->w_wincol = wp->w_wantcol - 1;
168 else
169 // TODO: better default
170 wp->w_wincol = Columns > 20 ? Columns / 2 - 10 : 0;
171 if (wp->w_wincol >= Columns - 3)
172 wp->w_wincol = Columns - 3;
173
174 // TODO: set width based on longest text line and the 'wrap' option
175 wp->w_width = vim_strsize(ml_get_buf(wp->w_buffer, 1, FALSE));
176 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
177 wp->w_width = wp->w_minwidth;
178 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
179 wp->w_width = wp->w_maxwidth;
180 if (wp->w_width > Columns - wp->w_wincol)
181 wp->w_width = Columns - wp->w_wincol;
182
183 if (wp->w_height <= 1)
184 // TODO: adjust height for wrapped lines
185 wp->w_height = wp->w_buffer->b_ml.ml_line_count;
186 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
187 wp->w_height = wp->w_minheight;
188 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
189 wp->w_height = wp->w_maxheight;
190 if (wp->w_height > Rows - wp->w_winrow)
191 wp->w_height = Rows - wp->w_winrow;
192}
193
194/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200195 * popup_create({text}, {options})
196 */
197 void
198f_popup_create(typval_T *argvars, typval_T *rettv)
199{
200 win_T *wp;
201 buf_T *buf;
202 dict_T *d;
203 int nr;
204
205 // Check arguments look OK.
206 if (!(argvars[0].v_type == VAR_STRING
207 && argvars[0].vval.v_string != NULL
208 && STRLEN(argvars[0].vval.v_string) > 0)
209 && !(argvars[0].v_type == VAR_LIST
210 && argvars[0].vval.v_list != NULL
211 && argvars[0].vval.v_list->lv_len > 0))
212 {
213 emsg(_(e_listreq));
214 return;
215 }
216 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
217 {
218 emsg(_(e_dictreq));
219 return;
220 }
221 d = argvars[1].vval.v_dict;
222
223 // Create the window and buffer.
224 wp = win_alloc_popup_win();
225 if (wp == NULL)
226 return;
227 rettv->vval.v_number = wp->w_id;
228 wp->w_p_wrap = TRUE; // 'wrap' is default on
229
230 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
231 if (buf == NULL)
232 return;
233 ml_open(buf);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200234 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200235 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200236 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200237 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200238 buf->b_p_ul = -1; // no undo
239 buf->b_p_swf = FALSE; // no swap file
240 buf->b_p_bl = FALSE; // unlisted buffer
241
242 win_init_popup_win(wp, buf);
243
244 nr = (int)dict_get_number(d, (char_u *)"tab");
245 if (nr == 0)
246 {
247 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200248 wp->w_next = curtab->tp_first_popupwin;
249 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200250 }
251 else if (nr < 0)
252 {
253 // global popup
254 wp->w_next = first_popupwin;
255 first_popupwin = wp;
256 }
257 else
258 // TODO: find tab page "nr"
259 emsg("Not implemented yet");
260
261 // Add text to the buffer.
262 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200263 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200264 // just a string
265 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200266 }
267 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200268 {
269 list_T *l = argvars[0].vval.v_list;
270
271 if (l->lv_first->li_tv.v_type == VAR_STRING)
272 // list of strings
273 add_popup_strings(buf, l);
274 else
275 // list of dictionaries
276 add_popup_dicts(buf, l);
277 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200278
279 // Delete the line of the empty buffer.
280 curbuf = buf;
281 ml_delete(buf->b_ml.ml_line_count, FALSE);
282 curbuf = curwin->w_buffer;
283
284 // Deal with options.
285 apply_options(wp, buf, argvars[1].vval.v_dict);
286
287 // set default values
288 if (wp->w_zindex == 0)
289 wp->w_zindex = 50;
290
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200291 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200292
293 wp->w_vsep_width = 0;
294
295 redraw_all_later(NOT_VALID);
296}
297
298/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200299 * Find the popup window with window-ID "id".
300 * If the popup window does not exist NULL is returned.
301 * If the window is not a popup window, and error message is given.
302 */
303 static win_T *
304find_popup_win(int id)
305{
306 win_T *wp = win_id2wp(id);
307
308 if (wp != NULL && !bt_popup(wp->w_buffer))
309 {
310 semsg(_("E993: window %d is not a popup window"), id);
311 return NULL;
312 }
313 return wp;
314}
315
316/*
317 * Return TRUE if there any popups that are not hidden.
318 */
319 int
320popup_any_visible(void)
321{
322 win_T *wp;
323
324 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200325 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200326 return TRUE;
327 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200328 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200329 return TRUE;
330 return FALSE;
331}
332
333/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200334 * popup_close({id})
335 */
336 void
337f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
338{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200339 int id = (int)tv_get_number(argvars);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200340
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200341 popup_close(id);
342}
343
344/*
345 * popup_hide({id})
346 */
347 void
348f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
349{
350 int id = (int)tv_get_number(argvars);
351 win_T *wp = find_popup_win(id);
352
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200353 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200354 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200355 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200356 redraw_all_later(NOT_VALID);
357 }
358}
359
360/*
361 * popup_show({id})
362 */
363 void
364f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
365{
366 int id = (int)tv_get_number(argvars);
367 win_T *wp = find_popup_win(id);
368
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200369 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200370 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200371 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200372 redraw_all_later(NOT_VALID);
373 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200374}
375
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200376 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200377popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200378{
379 if (wp->w_winrow + wp->w_height >= cmdline_row)
380 clear_cmdline = TRUE;
381 win_free_popup(wp);
382 redraw_all_later(NOT_VALID);
383}
384
Bram Moolenaarec583842019-05-26 14:11:23 +0200385/*
386 * Close a popup window by Window-id.
387 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200388 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200389popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200390{
391 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200392 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200393 win_T *prev = NULL;
394
Bram Moolenaarec583842019-05-26 14:11:23 +0200395 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200396 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200397 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200398 {
399 if (prev == NULL)
400 first_popupwin = wp->w_next;
401 else
402 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200403 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200404 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200405 }
406
Bram Moolenaarec583842019-05-26 14:11:23 +0200407 // go through tab-local popups
408 FOR_ALL_TABPAGES(tp)
409 popup_close_tabpage(tp, id);
410}
411
412/*
413 * Close a popup window with Window-id "id" in tabpage "tp".
414 */
415 void
416popup_close_tabpage(tabpage_T *tp, int id)
417{
418 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200419 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200420 win_T *prev = NULL;
421
Bram Moolenaarec583842019-05-26 14:11:23 +0200422 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
423 if (wp->w_id == id)
424 {
425 if (prev == NULL)
426 *root = wp->w_next;
427 else
428 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200429 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200430 return;
431 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200432}
433
434 void
435close_all_popups(void)
436{
437 while (first_popupwin != NULL)
438 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200439 while (curtab->tp_first_popupwin != NULL)
440 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200441}
442
443 void
444ex_popupclear(exarg_T *eap UNUSED)
445{
446 close_all_popups();
447}
448
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200449/*
450 * popup_move({id}, {options})
451 */
452 void
453f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
454{
455 dict_T *d;
456 int nr;
457 int id = (int)tv_get_number(argvars);
458 win_T *wp = find_popup_win(id);
459
460 if (wp == NULL)
461 return; // invalid {id}
462
463 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
464 {
465 emsg(_(e_dictreq));
466 return;
467 }
468 d = argvars[1].vval.v_dict;
469
470 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
471 wp->w_minwidth = nr;
472 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
473 wp->w_minheight = nr;
474 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
475 wp->w_maxwidth = nr;
476 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
477 wp->w_maxheight = nr;
478 if ((nr = dict_get_number(d, (char_u *)"line")) > 0)
479 wp->w_wantline = nr;
480 if ((nr = dict_get_number(d, (char_u *)"col")) > 0)
481 wp->w_wantcol = nr;
482 // TODO: "pos"
483
484 if (wp->w_winrow + wp->w_height >= cmdline_row)
485 clear_cmdline = TRUE;
486 popup_adjust_position(wp);
487 redraw_all_later(NOT_VALID);
488}
489
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200490#endif // FEAT_TEXT_PROP