blob: 0857f6b0a3ee176e14bd9591061950413f54e809 [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/*
63 * popup_create({text}, {options})
64 */
65 void
66f_popup_create(typval_T *argvars, typval_T *rettv)
67{
68 win_T *wp;
69 buf_T *buf;
70 dict_T *d;
71 int nr;
72
73 // Check arguments look OK.
74 if (!(argvars[0].v_type == VAR_STRING
75 && argvars[0].vval.v_string != NULL
76 && STRLEN(argvars[0].vval.v_string) > 0)
77 && !(argvars[0].v_type == VAR_LIST
78 && argvars[0].vval.v_list != NULL
79 && argvars[0].vval.v_list->lv_len > 0))
80 {
81 emsg(_(e_listreq));
82 return;
83 }
84 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
85 {
86 emsg(_(e_dictreq));
87 return;
88 }
89 d = argvars[1].vval.v_dict;
90
91 // Create the window and buffer.
92 wp = win_alloc_popup_win();
93 if (wp == NULL)
94 return;
95 rettv->vval.v_number = wp->w_id;
96 wp->w_p_wrap = TRUE; // 'wrap' is default on
97
98 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
99 if (buf == NULL)
100 return;
101 ml_open(buf);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200102 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200103 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200104 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200105 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200106 buf->b_p_ul = -1; // no undo
107 buf->b_p_swf = FALSE; // no swap file
108 buf->b_p_bl = FALSE; // unlisted buffer
109
110 win_init_popup_win(wp, buf);
111
112 nr = (int)dict_get_number(d, (char_u *)"tab");
113 if (nr == 0)
114 {
115 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200116 wp->w_next = curtab->tp_first_popupwin;
117 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200118 }
119 else if (nr < 0)
120 {
121 // global popup
122 wp->w_next = first_popupwin;
123 first_popupwin = wp;
124 }
125 else
126 // TODO: find tab page "nr"
127 emsg("Not implemented yet");
128
129 // Add text to the buffer.
130 if (argvars[0].v_type == VAR_STRING)
131 // just a string
132 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
133 else if (argvars[0].vval.v_list->lv_first->li_tv.v_type == VAR_STRING)
134 {
135 listitem_T *li;
136 linenr_T lnum = 0;
137 char_u *p;
138
139 // list of strings
140 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
141 li = li->li_next)
142 if (li->li_tv.v_type == VAR_STRING)
143 {
144 p = li->li_tv.vval.v_string;
145 ml_append_buf(buf, lnum++,
146 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
147 }
148 }
149 else
150 // TODO: handle a list of dictionaries
151 emsg("Not implemented yet");
152
153 // Delete the line of the empty buffer.
154 curbuf = buf;
155 ml_delete(buf->b_ml.ml_line_count, FALSE);
156 curbuf = curwin->w_buffer;
157
158 // Deal with options.
159 apply_options(wp, buf, argvars[1].vval.v_dict);
160
161 // set default values
162 if (wp->w_zindex == 0)
163 wp->w_zindex = 50;
164
165 // TODO: Compute the size and position properly.
166
167 // Default position is in middle of the screen, assuming a small popup
168 if (wp->w_winrow == 0)
169 wp->w_winrow = Rows > 5 ? Rows / 2 - 2 : 0;
170 else
171 --wp->w_winrow; // option value is one-based
172 if (wp->w_wincol == 0)
173 wp->w_wincol = Columns > 20 ? Columns / 2 - 10 : 0;
174 else
175 --wp->w_wincol; // option value is one-based
176
177
178 // TODO: set width based on longest text line and the 'wrap' option
179 wp->w_width = wp->w_maxwidth == 0 ? 20 : wp->w_maxwidth;
180 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
181 wp->w_width = wp->w_maxwidth;
182 if (wp->w_width > Columns - wp->w_wincol)
183 wp->w_width = Columns - wp->w_wincol;
184
185 // TODO: adjust height for wrapped lines
186 wp->w_height = buf->b_ml.ml_line_count;
187 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
188 wp->w_height = wp->w_maxheight;
189 if (wp->w_height > Rows - wp->w_winrow)
190 wp->w_height = Rows - wp->w_winrow;
191
192 wp->w_vsep_width = 0;
193
194 redraw_all_later(NOT_VALID);
195}
196
197/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200198 * Find the popup window with window-ID "id".
199 * If the popup window does not exist NULL is returned.
200 * If the window is not a popup window, and error message is given.
201 */
202 static win_T *
203find_popup_win(int id)
204{
205 win_T *wp = win_id2wp(id);
206
207 if (wp != NULL && !bt_popup(wp->w_buffer))
208 {
209 semsg(_("E993: window %d is not a popup window"), id);
210 return NULL;
211 }
212 return wp;
213}
214
215/*
216 * Return TRUE if there any popups that are not hidden.
217 */
218 int
219popup_any_visible(void)
220{
221 win_T *wp;
222
223 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
224 if ((wp->w_popup_flags & PFL_HIDDEN) == 0)
225 return TRUE;
226 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
227 if ((wp->w_popup_flags & PFL_HIDDEN) == 0)
228 return TRUE;
229 return FALSE;
230}
231
232/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200233 * popup_close({id})
234 */
235 void
236f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
237{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200238 int id = (int)tv_get_number(argvars);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200239
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200240 popup_close(id);
241}
242
243/*
244 * popup_hide({id})
245 */
246 void
247f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
248{
249 int id = (int)tv_get_number(argvars);
250 win_T *wp = find_popup_win(id);
251
252 if (wp != NULL && (wp->w_popup_flags & PFL_HIDDEN) == 0)
253 {
254 wp->w_popup_flags |= PFL_HIDDEN;
255 redraw_all_later(NOT_VALID);
256 }
257}
258
259/*
260 * popup_show({id})
261 */
262 void
263f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
264{
265 int id = (int)tv_get_number(argvars);
266 win_T *wp = find_popup_win(id);
267
268 if (wp != NULL && (wp->w_popup_flags & PFL_HIDDEN) != 0)
269 {
270 wp->w_popup_flags &= ~PFL_HIDDEN;
271 redraw_all_later(NOT_VALID);
272 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200273}
274
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200275 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200276popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200277{
278 if (wp->w_winrow + wp->w_height >= cmdline_row)
279 clear_cmdline = TRUE;
280 win_free_popup(wp);
281 redraw_all_later(NOT_VALID);
282}
283
Bram Moolenaarec583842019-05-26 14:11:23 +0200284/*
285 * Close a popup window by Window-id.
286 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200287 void
Bram Moolenaarec583842019-05-26 14:11:23 +0200288popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200289{
290 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +0200291 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200292 win_T *prev = NULL;
293
Bram Moolenaarec583842019-05-26 14:11:23 +0200294 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200295 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +0200296 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200297 {
298 if (prev == NULL)
299 first_popupwin = wp->w_next;
300 else
301 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200302 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200303 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200304 }
305
Bram Moolenaarec583842019-05-26 14:11:23 +0200306 // go through tab-local popups
307 FOR_ALL_TABPAGES(tp)
308 popup_close_tabpage(tp, id);
309}
310
311/*
312 * Close a popup window with Window-id "id" in tabpage "tp".
313 */
314 void
315popup_close_tabpage(tabpage_T *tp, int id)
316{
317 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200318 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +0200319 win_T *prev = NULL;
320
Bram Moolenaarec583842019-05-26 14:11:23 +0200321 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
322 if (wp->w_id == id)
323 {
324 if (prev == NULL)
325 *root = wp->w_next;
326 else
327 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200328 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +0200329 return;
330 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200331}
332
333 void
334close_all_popups(void)
335{
336 while (first_popupwin != NULL)
337 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200338 while (curtab->tp_first_popupwin != NULL)
339 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200340}
341
342 void
343ex_popupclear(exarg_T *eap UNUSED)
344{
345 close_all_popups();
346}
347
348#endif // FEAT_TEXT_PROP