blob: 9222119445b8f4dc248617d3554024e6565eeecf [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 Moolenaar4d784b22019-05-25 19:51:39 +020028 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
29 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
30 wp->w_winrow = dict_get_number(dict, (char_u *)"line");
31 wp->w_wincol = dict_get_number(dict, (char_u *)"col");
32 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020033
Bram Moolenaar35d5af62019-05-26 20:44:10 +020034#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020035 // Add timer to close the popup after some time.
36 nr = dict_get_number(dict, (char_u *)"time");
37 if (nr > 0)
38 {
39 char_u cbbuf[50];
40 char_u *ptr = cbbuf;
41 typval_T tv;
42
43 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
44 "{_ -> popup_close(%d)}", wp->w_id);
45 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
46 {
47 wp->w_popup_timer = create_timer(nr, 0);
48 wp->w_popup_timer->tr_callback =
49 vim_strsave(partial_name(tv.vval.v_partial));
50 func_ref(wp->w_popup_timer->tr_callback);
51 wp->w_popup_timer->tr_partial = tv.vval.v_partial;
52 }
53 }
Bram Moolenaar35d5af62019-05-26 20:44:10 +020054#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020055
Bram Moolenaar20c023a2019-05-26 21:03:24 +020056 str = dict_get_string(dict, (char_u *)"highlight", TRUE);
57 if (str != NULL)
58 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
59 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +020060}
61
62/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020063 * Add lines to the popup from a list of strings.
64 */
65 static void
66add_popup_strings(buf_T *buf, list_T *l)
67{
68 listitem_T *li;
69 linenr_T lnum = 0;
70 char_u *p;
71
72 for (li = l->lv_first; li != NULL; li = li->li_next)
73 if (li->li_tv.v_type == VAR_STRING)
74 {
75 p = li->li_tv.vval.v_string;
76 ml_append_buf(buf, lnum++,
77 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
78 }
79}
80
81/*
82 * Add lines to the popup from a list of dictionaries.
83 */
84 static void
85add_popup_dicts(buf_T *buf, list_T *l)
86{
87 listitem_T *li;
88 listitem_T *pli;
89 linenr_T lnum = 0;
90 char_u *p;
91 dict_T *dict;
92
93 // first add the text lines
94 for (li = l->lv_first; li != NULL; li = li->li_next)
95 {
96 if (li->li_tv.v_type != VAR_DICT)
97 {
98 emsg(_(e_dictreq));
99 return;
100 }
101 dict = li->li_tv.vval.v_dict;
102 p = dict == NULL ? NULL
103 : dict_get_string(dict, (char_u *)"text", FALSE);
104 ml_append_buf(buf, lnum++,
105 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
106 }
107
108 // add the text properties
109 lnum = 1;
110 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
111 {
112 dictitem_T *di;
113 list_T *plist;
114
115 dict = li->li_tv.vval.v_dict;
116 di = dict_find(dict, (char_u *)"props", -1);
117 if (di != NULL)
118 {
119 if (di->di_tv.v_type != VAR_LIST)
120 {
121 emsg(_(e_listreq));
122 return;
123 }
124 plist = di->di_tv.vval.v_list;
125 if (plist != NULL)
126 {
127 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
128 {
129 if (pli->li_tv.v_type != VAR_DICT)
130 {
131 emsg(_(e_dictreq));
132 return;
133 }
134 dict = pli->li_tv.vval.v_dict;
135 if (dict != NULL)
136 {
137 int col = dict_get_number(dict, (char_u *)"col");
138
139 prop_add_common( lnum, col, dict, buf, NULL);
140 }
141 }
142 }
143 }
144 }
145}
146
147/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200148 * popup_create({text}, {options})
149 */
150 void
151f_popup_create(typval_T *argvars, typval_T *rettv)
152{
153 win_T *wp;
154 buf_T *buf;
155 dict_T *d;
156 int nr;
157
158 // Check arguments look OK.
159 if (!(argvars[0].v_type == VAR_STRING
160 && argvars[0].vval.v_string != NULL
161 && STRLEN(argvars[0].vval.v_string) > 0)
162 && !(argvars[0].v_type == VAR_LIST
163 && argvars[0].vval.v_list != NULL
164 && argvars[0].vval.v_list->lv_len > 0))
165 {
166 emsg(_(e_listreq));
167 return;
168 }
169 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
170 {
171 emsg(_(e_dictreq));
172 return;
173 }
174 d = argvars[1].vval.v_dict;
175
176 // Create the window and buffer.
177 wp = win_alloc_popup_win();
178 if (wp == NULL)
179 return;
180 rettv->vval.v_number = wp->w_id;
181 wp->w_p_wrap = TRUE; // 'wrap' is default on
182
183 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
184 if (buf == NULL)
185 return;
186 ml_open(buf);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200187 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200188 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200189 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200190 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200191 buf->b_p_ul = -1; // no undo
192 buf->b_p_swf = FALSE; // no swap file
193 buf->b_p_bl = FALSE; // unlisted buffer
194
195 win_init_popup_win(wp, buf);
196
197 nr = (int)dict_get_number(d, (char_u *)"tab");
198 if (nr == 0)
199 {
200 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200201 wp->w_next = curtab->tp_first_popupwin;
202 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200203 }
204 else if (nr < 0)
205 {
206 // global popup
207 wp->w_next = first_popupwin;
208 first_popupwin = wp;
209 }
210 else
211 // TODO: find tab page "nr"
212 emsg("Not implemented yet");
213
214 // Add text to the buffer.
215 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200216 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200217 // just a string
218 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200219 }
220 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200221 {
222 list_T *l = argvars[0].vval.v_list;
223
224 if (l->lv_first->li_tv.v_type == VAR_STRING)
225 // list of strings
226 add_popup_strings(buf, l);
227 else
228 // list of dictionaries
229 add_popup_dicts(buf, l);
230 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200231
232 // Delete the line of the empty buffer.
233 curbuf = buf;
234 ml_delete(buf->b_ml.ml_line_count, FALSE);
235 curbuf = curwin->w_buffer;
236
237 // Deal with options.
238 apply_options(wp, buf, argvars[1].vval.v_dict);
239
240 // set default values
241 if (wp->w_zindex == 0)
242 wp->w_zindex = 50;
243
244 // TODO: Compute the size and position properly.
245
246 // Default position is in middle of the screen, assuming a small popup
247 if (wp->w_winrow == 0)
248 wp->w_winrow = Rows > 5 ? Rows / 2 - 2 : 0;
249 else
250 --wp->w_winrow; // option value is one-based
251 if (wp->w_wincol == 0)
252 wp->w_wincol = Columns > 20 ? Columns / 2 - 10 : 0;
253 else
254 --wp->w_wincol; // option value is one-based
255
256
257 // TODO: set width based on longest text line and the 'wrap' option
258 wp->w_width = wp->w_maxwidth == 0 ? 20 : wp->w_maxwidth;
259 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
260 wp->w_width = wp->w_maxwidth;
261 if (wp->w_width > Columns - wp->w_wincol)
262 wp->w_width = Columns - wp->w_wincol;
263
264 // TODO: adjust height for wrapped lines
265 wp->w_height = buf->b_ml.ml_line_count;
266 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
267 wp->w_height = wp->w_maxheight;
268 if (wp->w_height > Rows - wp->w_winrow)
269 wp->w_height = Rows - wp->w_winrow;
270
271 wp->w_vsep_width = 0;
272
273 redraw_all_later(NOT_VALID);
274}
275
276/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200277 * Find the popup window with window-ID "id".
278 * If the popup window does not exist NULL is returned.
279 * If the window is not a popup window, and error message is given.
280 */
281 static win_T *
282find_popup_win(int id)
283{
284 win_T *wp = win_id2wp(id);
285
286 if (wp != NULL && !bt_popup(wp->w_buffer))
287 {
288 semsg(_("E993: window %d is not a popup window"), id);
289 return NULL;
290 }
291 return wp;
292}
293
294/*
295 * Return TRUE if there any popups that are not hidden.
296 */
297 int
298popup_any_visible(void)
299{
300 win_T *wp;
301
302 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200303 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200304 return TRUE;
305 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200306 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200307 return TRUE;
308 return FALSE;
309}
310
311/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200312 * popup_close({id})
313 */
314 void
315f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
316{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200317 int id = (int)tv_get_number(argvars);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200318
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200319 popup_close(id);
320}
321
322/*
323 * popup_hide({id})
324 */
325 void
326f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
327{
328 int id = (int)tv_get_number(argvars);
329 win_T *wp = find_popup_win(id);
330
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200331 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200332 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200333 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200334 redraw_all_later(NOT_VALID);
335 }
336}
337
338/*
339 * popup_show({id})
340 */
341 void
342f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
343{
344 int id = (int)tv_get_number(argvars);
345 win_T *wp = find_popup_win(id);
346
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200347 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200348 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200349 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200350 redraw_all_later(NOT_VALID);
351 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200352}
353
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200354 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200355popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200356{
357 if (wp->w_winrow + wp->w_height >= cmdline_row)
358 clear_cmdline = TRUE;
359 win_free_popup(wp);
360 redraw_all_later(NOT_VALID);
361}
362
Bram Moolenaarec583842019-05-26 14:11:23 +0200363/*
364 * Close a popup window by Window-id.
365 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200366 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200367popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200368{
369 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200370 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200371 win_T *prev = NULL;
372
Bram Moolenaarec583842019-05-26 14:11:23 +0200373 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200374 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200375 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200376 {
377 if (prev == NULL)
378 first_popupwin = wp->w_next;
379 else
380 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200381 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200382 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200383 }
384
Bram Moolenaarec583842019-05-26 14:11:23 +0200385 // go through tab-local popups
386 FOR_ALL_TABPAGES(tp)
387 popup_close_tabpage(tp, id);
388}
389
390/*
391 * Close a popup window with Window-id "id" in tabpage "tp".
392 */
393 void
394popup_close_tabpage(tabpage_T *tp, int id)
395{
396 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200397 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200398 win_T *prev = NULL;
399
Bram Moolenaarec583842019-05-26 14:11:23 +0200400 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
401 if (wp->w_id == id)
402 {
403 if (prev == NULL)
404 *root = wp->w_next;
405 else
406 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200407 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200408 return;
409 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200410}
411
412 void
413close_all_popups(void)
414{
415 while (first_popupwin != NULL)
416 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200417 while (curtab->tp_first_popupwin != NULL)
418 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200419}
420
421 void
422ex_popupclear(exarg_T *eap UNUSED)
423{
424 close_all_popups();
425}
426
427#endif // FEAT_TEXT_PROP