blob: f773958bee1c19ef3ff31de0fb2d99c95340a9c4 [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
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar4d784b22019-05-25 19:51:39 +020031/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020032 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020033 * Handles "cursor+N" and "cursor-N".
34 */
35 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020036popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037{
38 dictitem_T *di;
39 char_u *val;
40 char_u *s;
41 char_u *endp;
42 int n = 0;
43
44 di = dict_find(dict, key, -1);
45 if (di == NULL)
46 return 0;
47
48 val = tv_get_string(&di->di_tv);
49 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020050 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020051
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020056 endp = s;
57 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
58 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059 if (endp != NULL && *skipwhite(endp) != NUL)
60 {
61 semsg(_(e_invexpr2), val);
62 return 0;
63 }
64 }
65
66 if (STRCMP(key, "line") == 0)
67 n = screen_screenrow() + 1 + n;
68 else // "col"
69 n = screen_screencol() + 1 + n;
70
71 if (n < 1)
72 n = 1;
73 return n;
74}
75
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020076 static void
77get_pos_options(win_T *wp, dict_T *dict)
78{
79 char_u *str;
80 int nr;
81
82 nr = popup_options_one(dict, (char_u *)"line");
83 if (nr > 0)
84 wp->w_wantline = nr;
85 nr = popup_options_one(dict, (char_u *)"col");
86 if (nr > 0)
87 wp->w_wantcol = nr;
88
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020089 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
90
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020091 str = dict_get_string(dict, (char_u *)"pos", FALSE);
92 if (str != NULL)
93 {
94 for (nr = 0;
95 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
96 ++nr)
97 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
98 {
99 wp->w_popup_pos = poppos_entries[nr].pp_val;
100 nr = -1;
101 break;
102 }
103 if (nr != -1)
104 semsg(_(e_invarg2), str);
105 }
106}
107
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200108 static void
109get_padding_border(dict_T *dict, int *array, char *name, int max_val)
110{
111 dictitem_T *di;
112
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113 di = dict_find(dict, (char_u *)name, -1);
114 if (di != NULL)
115 {
116 if (di->di_tv.v_type != VAR_LIST)
117 emsg(_(e_listreq));
118 else
119 {
120 list_T *list = di->di_tv.vval.v_list;
121 listitem_T *li;
122 int i;
123 int nr;
124
125 for (i = 0; i < 4; ++i)
126 array[i] = 1;
127 if (list != NULL)
128 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
129 ++i, li = li->li_next)
130 {
131 nr = (int)tv_get_number(&li->li_tv);
132 if (nr >= 0)
133 array[i] = nr > max_val ? max_val : nr;
134 }
135 }
136 }
137}
138
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200139/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200140 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200141 */
142 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200143set_moved_values(win_T *wp)
144{
145 wp->w_popup_curwin = curwin;
146 wp->w_popup_lnum = curwin->w_cursor.lnum;
147 wp->w_popup_mincol = curwin->w_cursor.col;
148 wp->w_popup_maxcol = curwin->w_cursor.col;
149}
150
151/*
152 * Used when popup options contain "moved" with "word" or "WORD".
153 */
154 static void
155set_moved_columns(win_T *wp, int flags)
156{
157 char_u *ptr;
158 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
159
160 if (len > 0)
161 {
162 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
163 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
164 }
165}
166
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200167/*
168 * Return TRUE if "row"/"col" is on the border of the popup.
169 * The values are relative to the top-left corner.
170 */
171 int
172popup_on_border(win_T *wp, int row, int col)
173{
174 return (row == 0 && wp->w_popup_border[0] > 0)
175 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
176 || (col == 0 && wp->w_popup_border[3] > 0)
177 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
178}
179
180// Values set when dragging a popup window starts.
181static int drag_start_row;
182static int drag_start_col;
183static int drag_start_wantline;
184static int drag_start_wantcol;
185
186/*
187 * Mouse down on border of popup window: start dragging it.
188 * Uses mouse_col and mouse_row.
189 */
190 void
191popup_start_drag(win_T *wp)
192{
193 drag_start_row = mouse_row;
194 drag_start_col = mouse_col;
195 // TODO: handle using different corner
196 if (wp->w_wantline == 0)
197 drag_start_wantline = wp->w_winrow + 1;
198 else
199 drag_start_wantline = wp->w_wantline;
200 if (wp->w_wantcol == 0)
201 drag_start_wantcol = wp->w_wincol + 1;
202 else
203 drag_start_wantcol = wp->w_wantcol;
204}
205
206/*
207 * Mouse moved while dragging a popup window: adjust the window popup position.
208 */
209 void
210popup_drag(win_T *wp)
211{
212 // The popup may be closed before dragging stops.
213 if (!win_valid_popup(wp))
214 return;
215
216 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
217 if (wp->w_wantline < 1)
218 wp->w_wantline = 1;
219 if (wp->w_wantline > Rows)
220 wp->w_wantline = Rows;
221 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
222 if (wp->w_wantcol < 1)
223 wp->w_wantcol = 1;
224 if (wp->w_wantcol > Columns)
225 wp->w_wantcol = Columns;
226
227 popup_adjust_position(wp);
228}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200229
230#if defined(FEAT_TIMERS)
231 static void
232popup_add_timeout(win_T *wp, int time)
233{
234 char_u cbbuf[50];
235 char_u *ptr = cbbuf;
236 typval_T tv;
237
238 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
239 "{_ -> popup_close(%d)}", wp->w_id);
240 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
241 {
242 wp->w_popup_timer = create_timer(time, 0);
243 wp->w_popup_timer->tr_callback = get_callback(&tv);
244 clear_tv(&tv);
245 }
246}
247#endif
248
Bram Moolenaar17627312019-06-02 19:53:44 +0200249/*
250 * Go through the options in "dict" and apply them to buffer "buf" displayed in
251 * popup window "wp".
252 */
253 static void
254apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200255{
Bram Moolenaar402502d2019-05-30 22:07:36 +0200256 int nr;
257 char_u *str;
258 dictitem_T *di;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200259 int i;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200260
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200261 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
262 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200263 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
264 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200265
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200266 get_pos_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200267
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200268 di = dict_find(dict, (char_u *)"zindex", -1);
269 if (di != NULL)
270 {
271 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
272 if (wp->w_zindex < 1)
273 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
274 if (wp->w_zindex > 32000)
275 wp->w_zindex = 32000;
276 }
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200277
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200278#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200279 // Add timer to close the popup after some time.
280 nr = dict_get_number(dict, (char_u *)"time");
281 if (nr > 0)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200282 popup_add_timeout(wp, nr);
283#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200284
Bram Moolenaar402502d2019-05-30 22:07:36 +0200285 // Option values resulting in setting an option.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200286 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200287 if (str != NULL)
288 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
289 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200290
Bram Moolenaar8d241042019-06-12 23:40:01 +0200291 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
292 if (wp->w_firstline < 1)
293 wp->w_firstline = 1;
294
Bram Moolenaar402502d2019-05-30 22:07:36 +0200295 di = dict_find(dict, (char_u *)"wrap", -1);
296 if (di != NULL)
297 {
298 nr = dict_get_number(dict, (char_u *)"wrap");
299 wp->w_p_wrap = nr != 0;
300 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200301
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200302 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
303
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200304 di = dict_find(dict, (char_u *)"callback", -1);
305 if (di != NULL)
306 {
307 callback_T callback = get_callback(&di->di_tv);
308
309 if (callback.cb_name != NULL)
310 set_callback(&wp->w_close_cb, &callback);
311 }
312
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200313 di = dict_find(dict, (char_u *)"filter", -1);
314 if (di != NULL)
315 {
316 callback_T callback = get_callback(&di->di_tv);
317
318 if (callback.cb_name != NULL)
319 set_callback(&wp->w_filter_cb, &callback);
320 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200321
322 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
323 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200324
325 for (i = 0; i < 4; ++i)
326 VIM_CLEAR(wp->w_border_highlight[i]);
327 di = dict_find(dict, (char_u *)"borderhighlight", -1);
328 if (di != NULL)
329 {
330 if (di->di_tv.v_type != VAR_LIST)
331 emsg(_(e_listreq));
332 else
333 {
334 list_T *list = di->di_tv.vval.v_list;
335 listitem_T *li;
336
337 if (list != NULL)
338 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
339 ++i, li = li->li_next)
340 {
341 str = tv_get_string(&li->li_tv);
342 if (*str != NUL)
343 wp->w_border_highlight[i] = vim_strsave(str);
344 }
345 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
346 for (i = 1; i < 4; ++i)
347 wp->w_border_highlight[i] =
348 vim_strsave(wp->w_border_highlight[0]);
349 }
350 }
351
352 for (i = 0; i < 8; ++i)
353 wp->w_border_char[i] = 0;
354 di = dict_find(dict, (char_u *)"borderchars", -1);
355 if (di != NULL)
356 {
357 if (di->di_tv.v_type != VAR_LIST)
358 emsg(_(e_listreq));
359 else
360 {
361 list_T *list = di->di_tv.vval.v_list;
362 listitem_T *li;
363
364 if (list != NULL)
365 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
366 ++i, li = li->li_next)
367 {
368 str = tv_get_string(&li->li_tv);
369 if (*str != NUL)
370 wp->w_border_char[i] = mb_ptr2char(str);
371 }
372 if (list->lv_len == 1)
373 for (i = 1; i < 8; ++i)
374 wp->w_border_char[i] = wp->w_border_char[0];
375 if (list->lv_len == 2)
376 {
377 for (i = 4; i < 8; ++i)
378 wp->w_border_char[i] = wp->w_border_char[1];
379 for (i = 1; i < 4; ++i)
380 wp->w_border_char[i] = wp->w_border_char[0];
381 }
382 }
383 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200384
385 di = dict_find(dict, (char_u *)"moved", -1);
386 if (di != NULL)
387 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200388 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200389 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
390 {
391 char_u *s = di->di_tv.vval.v_string;
392 int flags = 0;
393
394 if (STRCMP(s, "word") == 0)
395 flags = FIND_IDENT | FIND_STRING;
396 else if (STRCMP(s, "WORD") == 0)
397 flags = FIND_STRING;
398 else if (STRCMP(s, "any") != 0)
399 semsg(_(e_invarg2), s);
400 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200401 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200402 }
403 else if (di->di_tv.v_type == VAR_LIST
404 && di->di_tv.vval.v_list != NULL
405 && di->di_tv.vval.v_list->lv_len == 2)
406 {
407 list_T *l = di->di_tv.vval.v_list;
408
409 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
410 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
411 }
412 else
413 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
414 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200415
416 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200417}
418
419/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200420 * Add lines to the popup from a list of strings.
421 */
422 static void
423add_popup_strings(buf_T *buf, list_T *l)
424{
425 listitem_T *li;
426 linenr_T lnum = 0;
427 char_u *p;
428
429 for (li = l->lv_first; li != NULL; li = li->li_next)
430 if (li->li_tv.v_type == VAR_STRING)
431 {
432 p = li->li_tv.vval.v_string;
433 ml_append_buf(buf, lnum++,
434 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
435 }
436}
437
438/*
439 * Add lines to the popup from a list of dictionaries.
440 */
441 static void
442add_popup_dicts(buf_T *buf, list_T *l)
443{
444 listitem_T *li;
445 listitem_T *pli;
446 linenr_T lnum = 0;
447 char_u *p;
448 dict_T *dict;
449
450 // first add the text lines
451 for (li = l->lv_first; li != NULL; li = li->li_next)
452 {
453 if (li->li_tv.v_type != VAR_DICT)
454 {
455 emsg(_(e_dictreq));
456 return;
457 }
458 dict = li->li_tv.vval.v_dict;
459 p = dict == NULL ? NULL
460 : dict_get_string(dict, (char_u *)"text", FALSE);
461 ml_append_buf(buf, lnum++,
462 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
463 }
464
465 // add the text properties
466 lnum = 1;
467 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
468 {
469 dictitem_T *di;
470 list_T *plist;
471
472 dict = li->li_tv.vval.v_dict;
473 di = dict_find(dict, (char_u *)"props", -1);
474 if (di != NULL)
475 {
476 if (di->di_tv.v_type != VAR_LIST)
477 {
478 emsg(_(e_listreq));
479 return;
480 }
481 plist = di->di_tv.vval.v_list;
482 if (plist != NULL)
483 {
484 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
485 {
486 if (pli->li_tv.v_type != VAR_DICT)
487 {
488 emsg(_(e_dictreq));
489 return;
490 }
491 dict = pli->li_tv.vval.v_dict;
492 if (dict != NULL)
493 {
494 int col = dict_get_number(dict, (char_u *)"col");
495
496 prop_add_common( lnum, col, dict, buf, NULL);
497 }
498 }
499 }
500 }
501 }
502}
503
504/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200505 * Return the height of popup window "wp", including border and padding.
506 */
507 int
508popup_height(win_T *wp)
509{
510 return wp->w_height
511 + wp->w_popup_padding[0] + wp->w_popup_border[0]
512 + wp->w_popup_padding[2] + wp->w_popup_border[2];
513}
514
515/*
516 * Return the width of popup window "wp", including border and padding.
517 */
518 int
519popup_width(win_T *wp)
520{
521 return wp->w_width
522 + wp->w_popup_padding[3] + wp->w_popup_border[3]
523 + wp->w_popup_padding[1] + wp->w_popup_border[1];
524}
525
526/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200527 * Adjust the position and size of the popup to fit on the screen.
528 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200529 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200530popup_adjust_position(win_T *wp)
531{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200532 linenr_T lnum;
533 int wrapped = 0;
534 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200535 int center_vert = FALSE;
536 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200537 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaar399d8982019-06-02 15:34:29 +0200538 int top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
539 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
540 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
541 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
542 int extra_height = top_extra + bot_extra;
543 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200544 int org_winrow = wp->w_winrow;
545 int org_wincol = wp->w_wincol;
546 int org_width = wp->w_width;
547 int org_height = wp->w_height;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200548
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200549 wp->w_winrow = 0;
550 wp->w_wincol = 0;
551 if (wp->w_popup_pos == POPPOS_CENTER)
552 {
553 // center after computing the size
554 center_vert = TRUE;
555 center_hor = TRUE;
556 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200557 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200558 {
559 if (wp->w_wantline == 0)
560 center_vert = TRUE;
561 else if (wp->w_popup_pos == POPPOS_TOPLEFT
562 || wp->w_popup_pos == POPPOS_TOPRIGHT)
563 {
564 wp->w_winrow = wp->w_wantline - 1;
565 if (wp->w_winrow >= Rows)
566 wp->w_winrow = Rows - 1;
567 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200568
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200569 if (wp->w_wantcol == 0)
570 center_hor = TRUE;
571 else if (wp->w_popup_pos == POPPOS_TOPLEFT
572 || wp->w_popup_pos == POPPOS_BOTLEFT)
573 {
574 wp->w_wincol = wp->w_wantcol - 1;
575 if (wp->w_wincol >= Columns - 3)
576 wp->w_wincol = Columns - 3;
577 }
578 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200579
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200580 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200581 // When left aligned use the space available, but shift to the left when we
582 // hit the right of the screen.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200583 maxwidth = Columns - wp->w_wincol;
584 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200585 {
586 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200587 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200588 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200589
Bram Moolenaar8d241042019-06-12 23:40:01 +0200590 // start at the desired first line
591 wp->w_topline = wp->w_firstline;
592 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
593 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
594
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200595 // Compute width based on longest text line and the 'wrap' option.
596 // TODO: more accurate wrapping
597 wp->w_width = 0;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200598 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200599 {
600 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
601
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200602 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200603 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200604 while (len > maxwidth)
605 {
606 ++wrapped;
607 len -= maxwidth;
608 wp->w_width = maxwidth;
609 }
610 }
611 else if (len > maxwidth
612 && allow_adjust_left
613 && (wp->w_popup_pos == POPPOS_TOPLEFT
614 || wp->w_popup_pos == POPPOS_BOTLEFT))
615 {
616 // adjust leftwise to fit text on screen
617 int shift_by = ( len - maxwidth );
618
619 if ( shift_by > wp->w_wincol )
620 {
621 int truncate_shift = shift_by - wp->w_wincol;
622 len -= truncate_shift;
623 shift_by -= truncate_shift;
624 }
625
626 wp->w_wincol -= shift_by;
627 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200628 wp->w_width = maxwidth;
629 }
630 if (wp->w_width < len)
631 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200632 // do not use the width of lines we're not going to show
633 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
634 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
635 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200636 }
637
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200638 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
639 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200640 if (wp->w_width > maxwidth)
641 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200642 if (center_hor)
643 wp->w_wincol = (Columns - wp->w_width) / 2;
644 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
645 || wp->w_popup_pos == POPPOS_TOPRIGHT)
646 {
647 // Right aligned: move to the right if needed.
648 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200649 if (wp->w_width + extra_width < wp->w_wantcol)
650 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200651 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200652
Bram Moolenaar8d241042019-06-12 23:40:01 +0200653 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
654 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200655 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
656 wp->w_height = wp->w_minheight;
657 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
658 wp->w_height = wp->w_maxheight;
659 if (wp->w_height > Rows - wp->w_winrow)
660 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200661
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200662 if (center_vert)
663 wp->w_winrow = (Rows - wp->w_height) / 2;
664 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
665 || wp->w_popup_pos == POPPOS_BOTLEFT)
666 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200667 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200668 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200669 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200670 else
671 // not enough space, make top aligned
672 wp->w_winrow = wp->w_wantline + 1;
673 }
674
Bram Moolenaar17146962019-05-30 00:12:11 +0200675 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200676
677 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200678 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200679 if (org_winrow != wp->w_winrow
680 || org_wincol != wp->w_wincol
681 || org_width != wp->w_width
682 || org_height != wp->w_height)
683 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200684 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200685 popup_mask_refresh = TRUE;
686 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200687}
688
Bram Moolenaar17627312019-06-02 19:53:44 +0200689typedef enum
690{
691 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200692 TYPE_ATCURSOR,
693 TYPE_NOTIFICATION
Bram Moolenaar17627312019-06-02 19:53:44 +0200694} create_type_T;
695
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200696/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200697 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200698 * popup_atcursor({text}, {options})
Bram Moolenaar33796b32019-06-08 16:01:13 +0200699 * When called from f_popup_atcursor() "type" is TYPE_ATCURSOR.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200700 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200701 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200702popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200703{
704 win_T *wp;
705 buf_T *buf;
706 dict_T *d;
707 int nr;
708
709 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200710 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
711 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200712 {
713 emsg(_(e_listreq));
714 return;
715 }
716 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
717 {
718 emsg(_(e_dictreq));
719 return;
720 }
721 d = argvars[1].vval.v_dict;
722
723 // Create the window and buffer.
724 wp = win_alloc_popup_win();
725 if (wp == NULL)
726 return;
727 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200728 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200729
730 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
731 if (buf == NULL)
732 return;
733 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200734
735 win_init_popup_win(wp, buf);
736
737 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200738 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200739 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200740 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200741 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200742 buf->b_p_ul = -1; // no undo
743 buf->b_p_swf = FALSE; // no swap file
744 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200745 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200746 wp->w_p_wrap = TRUE; // 'wrap' is default on
747
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200748 // Avoid that 'buftype' is reset when this buffer is entered.
749 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200750
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200751 if (dict_find(d, (char_u *)"tab", -1) != NULL)
752 nr = (int)dict_get_number(d, (char_u *)"tab");
753 else if (type == TYPE_NOTIFICATION)
754 nr = -1; // notifications are global by default
755 else
756 nr = 0;
757
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200758 if (nr == 0)
759 {
760 // popup on current tab
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200761 wp->w_next = curtab->tp_first_popupwin;
762 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200763 }
764 else if (nr < 0)
765 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200766 win_T *prev = first_popupwin;
767
768 // Global popup: add at the end, so that it gets displayed on top of
769 // older ones with the same zindex. Matters for notifications.
770 if (first_popupwin == NULL)
771 first_popupwin = wp;
772 else
773 {
774 while (prev->w_next != NULL)
775 prev = prev->w_next;
776 prev->w_next = wp;
777 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200778 }
779 else
780 // TODO: find tab page "nr"
781 emsg("Not implemented yet");
782
783 // Add text to the buffer.
784 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200785 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200786 // just a string
787 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200788 }
789 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200790 {
791 list_T *l = argvars[0].vval.v_list;
792
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200793 if (l->lv_len > 0)
794 {
795 if (l->lv_first->li_tv.v_type == VAR_STRING)
796 // list of strings
797 add_popup_strings(buf, l);
798 else
799 // list of dictionaries
800 add_popup_dicts(buf, l);
801 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200802 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200803
804 // Delete the line of the empty buffer.
805 curbuf = buf;
806 ml_delete(buf->b_ml.ml_line_count, FALSE);
807 curbuf = curwin->w_buffer;
808
Bram Moolenaar17627312019-06-02 19:53:44 +0200809 if (type == TYPE_ATCURSOR)
810 {
811 wp->w_popup_pos = POPPOS_BOTLEFT;
812 setcursor_mayforce(TRUE);
813 wp->w_wantline = screen_screenrow();
814 if (wp->w_wantline == 0) // cursor in first line
815 {
816 wp->w_wantline = 2;
817 wp->w_popup_pos = POPPOS_TOPLEFT;
818 }
819 wp->w_wantcol = screen_screencol() + 1;
820 set_moved_values(wp);
821 set_moved_columns(wp, FIND_STRING);
822 }
823
Bram Moolenaar33796b32019-06-08 16:01:13 +0200824 // set default values
825 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
826
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200827 if (type == TYPE_NOTIFICATION)
828 {
829 win_T *twp, *nextwin;
830 int height = buf->b_ml.ml_line_count + 3;
831 int i;
832
833 // Try to not overlap with another global popup. Guess we need 3
834 // more screen lines than buffer lines.
835 wp->w_wantline = 1;
836 for (twp = first_popupwin; twp != NULL; twp = nextwin)
837 {
838 nextwin = twp->w_next;
839 if (twp != wp
840 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
841 && twp->w_winrow <= wp->w_wantline - 1 + height
842 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
843 {
844 // move to below this popup and restart the loop to check for
845 // overlap with other popups
846 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
847 nextwin = first_popupwin;
848 }
849 }
850 if (wp->w_wantline + height > Rows)
851 {
852 // can't avoid overlap, put on top in the hope that message goes
853 // away soon.
854 wp->w_wantline = 1;
855 }
856
857 wp->w_wantcol = 10;
858 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
859 for (i = 0; i < 4; ++i)
860 wp->w_popup_border[i] = 1;
861 wp->w_popup_padding[1] = 1;
862 wp->w_popup_padding[3] = 1;
863 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
864 (char_u *)"WarningMsg", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200865 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200866 }
867
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200868 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200869 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200870
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200871 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
872 popup_add_timeout(wp, 3000);
873
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200874 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200875
876 wp->w_vsep_width = 0;
877
878 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200879 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200880}
881
882/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200883 * popup_clear()
884 */
885 void
886f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
887{
888 close_all_popups();
889}
890
891/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200892 * popup_create({text}, {options})
893 */
894 void
895f_popup_create(typval_T *argvars, typval_T *rettv)
896{
Bram Moolenaar17627312019-06-02 19:53:44 +0200897 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200898}
899
900/*
901 * popup_atcursor({text}, {options})
902 */
903 void
904f_popup_atcursor(typval_T *argvars, typval_T *rettv)
905{
Bram Moolenaar17627312019-06-02 19:53:44 +0200906 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200907}
908
909/*
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200910 * popup_notification({text}, {options})
911 */
912 void
913f_popup_notification(typval_T *argvars, typval_T *rettv)
914{
915 popup_create(argvars, rettv, TYPE_NOTIFICATION);
916}
917
918/*
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200919 * Find the popup window with window-ID "id".
920 * If the popup window does not exist NULL is returned.
921 * If the window is not a popup window, and error message is given.
922 */
923 static win_T *
924find_popup_win(int id)
925{
926 win_T *wp = win_id2wp(id);
927
928 if (wp != NULL && !bt_popup(wp->w_buffer))
929 {
930 semsg(_("E993: window %d is not a popup window"), id);
931 return NULL;
932 }
933 return wp;
934}
935
936/*
937 * Return TRUE if there any popups that are not hidden.
938 */
939 int
940popup_any_visible(void)
941{
942 win_T *wp;
943
944 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200945 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200946 return TRUE;
947 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +0200948 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200949 return TRUE;
950 return FALSE;
951}
952
953/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200954 * Invoke the close callback for window "wp" with value "result".
955 * Careful: The callback may make "wp" invalid!
956 */
957 static void
958invoke_popup_callback(win_T *wp, typval_T *result)
959{
960 typval_T rettv;
961 int dummy;
962 typval_T argv[3];
963
964 argv[0].v_type = VAR_NUMBER;
965 argv[0].vval.v_number = (varnumber_T)wp->w_id;
966
967 if (result != NULL && result->v_type != VAR_UNKNOWN)
968 copy_tv(result, &argv[1]);
969 else
970 {
971 argv[1].v_type = VAR_NUMBER;
972 argv[1].vval.v_number = 0;
973 }
974
975 argv[2].v_type = VAR_UNKNOWN;
976
977 call_callback(&wp->w_close_cb, -1,
978 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
979 if (result != NULL)
980 clear_tv(&argv[1]);
981 clear_tv(&rettv);
982}
983
984/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200985 * Close popup "wp" and invoke any close callback for it.
986 */
987 static void
988popup_close_and_callback(win_T *wp, typval_T *arg)
989{
990 int id = wp->w_id;
991
992 if (wp->w_close_cb.cb_name != NULL)
993 // Careful: This may make "wp" invalid.
994 invoke_popup_callback(wp, arg);
995
996 popup_close(id);
997}
998
999/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001000 * popup_close({id})
1001 */
1002 void
1003f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1004{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001005 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001006 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001007
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001008 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001009 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001010}
1011
1012/*
1013 * popup_hide({id})
1014 */
1015 void
1016f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1017{
1018 int id = (int)tv_get_number(argvars);
1019 win_T *wp = find_popup_win(id);
1020
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001021 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001022 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001023 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001024 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001025 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001026 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001027 }
1028}
1029
1030/*
1031 * popup_show({id})
1032 */
1033 void
1034f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1035{
1036 int id = (int)tv_get_number(argvars);
1037 win_T *wp = find_popup_win(id);
1038
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001039 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001040 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001041 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001042 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001043 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001044 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001045 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001046}
1047
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001048 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001049popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001050{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001051 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001052 if (wp->w_winrow + wp->w_height >= cmdline_row)
1053 clear_cmdline = TRUE;
1054 win_free_popup(wp);
1055 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001056 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001057}
1058
Bram Moolenaarec583842019-05-26 14:11:23 +02001059/*
1060 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001061 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001062 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001063 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001064popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001065{
1066 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001067 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001068 win_T *prev = NULL;
1069
Bram Moolenaarec583842019-05-26 14:11:23 +02001070 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001071 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001072 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001073 {
1074 if (prev == NULL)
1075 first_popupwin = wp->w_next;
1076 else
1077 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001078 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001079 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001080 }
1081
Bram Moolenaarec583842019-05-26 14:11:23 +02001082 // go through tab-local popups
1083 FOR_ALL_TABPAGES(tp)
1084 popup_close_tabpage(tp, id);
1085}
1086
1087/*
1088 * Close a popup window with Window-id "id" in tabpage "tp".
1089 */
1090 void
1091popup_close_tabpage(tabpage_T *tp, int id)
1092{
1093 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001094 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001095 win_T *prev = NULL;
1096
Bram Moolenaarec583842019-05-26 14:11:23 +02001097 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1098 if (wp->w_id == id)
1099 {
1100 if (prev == NULL)
1101 *root = wp->w_next;
1102 else
1103 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001104 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001105 return;
1106 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001107}
1108
1109 void
1110close_all_popups(void)
1111{
1112 while (first_popupwin != NULL)
1113 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001114 while (curtab->tp_first_popupwin != NULL)
1115 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001116}
1117
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001118/*
1119 * popup_move({id}, {options})
1120 */
1121 void
1122f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1123{
1124 dict_T *d;
1125 int nr;
1126 int id = (int)tv_get_number(argvars);
1127 win_T *wp = find_popup_win(id);
1128
1129 if (wp == NULL)
1130 return; // invalid {id}
1131
1132 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1133 {
1134 emsg(_(e_dictreq));
1135 return;
1136 }
1137 d = argvars[1].vval.v_dict;
1138
1139 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1140 wp->w_minwidth = nr;
1141 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1142 wp->w_minheight = nr;
1143 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1144 wp->w_maxwidth = nr;
1145 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1146 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001147 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001148
1149 if (wp->w_winrow + wp->w_height >= cmdline_row)
1150 clear_cmdline = TRUE;
1151 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001152}
1153
Bram Moolenaarbc133542019-05-29 20:26:46 +02001154/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001155 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001156 */
1157 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001158f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001159{
1160 dict_T *dict;
1161 int id = (int)tv_get_number(argvars);
1162 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001163 int top_extra;
1164 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001165
1166 if (rettv_dict_alloc(rettv) == OK)
1167 {
1168 if (wp == NULL)
1169 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001170 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1171 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1172
Bram Moolenaarbc133542019-05-29 20:26:46 +02001173 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001174
Bram Moolenaarbc133542019-05-29 20:26:46 +02001175 dict_add_number(dict, "line", wp->w_winrow + 1);
1176 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001177 dict_add_number(dict, "width", wp->w_width + left_extra
1178 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1179 dict_add_number(dict, "height", wp->w_height + top_extra
1180 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001181
1182 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1183 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1184 dict_add_number(dict, "core_width", wp->w_width);
1185 dict_add_number(dict, "core_height", wp->w_height);
1186
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001187 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001188 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001189 }
1190}
1191
1192/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001193 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001194 */
1195 void
1196f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1197{
1198 dict_T *dict;
1199 int id = (int)tv_get_number(argvars);
1200 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001201 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001202
1203 if (rettv_dict_alloc(rettv) == OK)
1204 {
1205 if (wp == NULL)
1206 return;
1207
1208 dict = rettv->vval.v_dict;
1209 dict_add_number(dict, "line", wp->w_wantline);
1210 dict_add_number(dict, "col", wp->w_wantcol);
1211 dict_add_number(dict, "minwidth", wp->w_minwidth);
1212 dict_add_number(dict, "minheight", wp->w_minheight);
1213 dict_add_number(dict, "maxheight", wp->w_maxheight);
1214 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001215 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001216 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001217 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001218
1219 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1220 ++i)
1221 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1222 {
1223 dict_add_string(dict, "pos",
1224 (char_u *)poppos_entries[i].pp_name);
1225 break;
1226 }
1227
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001228# if defined(FEAT_TIMERS)
1229 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1230 ? (long)wp->w_popup_timer->tr_interval : 0L);
1231# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001232 }
1233}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001234
1235 int
1236not_in_popup_window()
1237{
1238 if (bt_popup(curwin->w_buffer))
1239 {
1240 emsg(_("E994: Not allowed in a popup window"));
1241 return TRUE;
1242 }
1243 return FALSE;
1244}
1245
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001246/*
1247 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
1248 * in the current tab.
1249 */
1250 void
1251popup_reset_handled()
1252{
1253 win_T *wp;
1254
1255 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1256 wp->w_popup_flags &= ~POPF_HANDLED;
1257 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1258 wp->w_popup_flags &= ~POPF_HANDLED;
1259}
1260
1261/*
1262 * Find the next visible popup where POPF_HANDLED is not set.
1263 * Must have called popup_reset_handled() first.
1264 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1265 * popup with the highest zindex.
1266 */
1267 win_T *
1268find_next_popup(int lowest)
1269{
1270 win_T *wp;
1271 win_T *found_wp;
1272 int found_zindex;
1273
1274 found_zindex = lowest ? INT_MAX : 0;
1275 found_wp = NULL;
1276 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1277 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1278 && (lowest ? wp->w_zindex < found_zindex
1279 : wp->w_zindex > found_zindex))
1280 {
1281 found_zindex = wp->w_zindex;
1282 found_wp = wp;
1283 }
1284 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1285 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1286 && (lowest ? wp->w_zindex < found_zindex
1287 : wp->w_zindex > found_zindex))
1288 {
1289 found_zindex = wp->w_zindex;
1290 found_wp = wp;
1291 }
1292
1293 if (found_wp != NULL)
1294 found_wp->w_popup_flags |= POPF_HANDLED;
1295 return found_wp;
1296}
1297
1298/*
1299 * Invoke the filter callback for window "wp" with typed character "c".
1300 * Uses the global "mod_mask" for modifiers.
1301 * Returns the return value of the filter.
1302 * Careful: The filter may make "wp" invalid!
1303 */
1304 static int
1305invoke_popup_filter(win_T *wp, int c)
1306{
1307 int res;
1308 typval_T rettv;
1309 int dummy;
1310 typval_T argv[3];
1311 char_u buf[NUMBUFLEN];
1312
1313 argv[0].v_type = VAR_NUMBER;
1314 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1315
1316 // Convert the number to a string, so that the function can use:
1317 // if a:c == "\<F2>"
1318 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1319 argv[1].v_type = VAR_STRING;
1320 argv[1].vval.v_string = vim_strsave(buf);
1321
1322 argv[2].v_type = VAR_UNKNOWN;
1323
1324 call_callback(&wp->w_filter_cb, -1,
1325 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1326 res = tv_get_number(&rettv);
1327 vim_free(argv[1].vval.v_string);
1328 clear_tv(&rettv);
1329 return res;
1330}
1331
1332/*
1333 * Called when "c" was typed: invoke popup filter callbacks.
1334 * Returns TRUE when the character was consumed,
1335 */
1336 int
1337popup_do_filter(int c)
1338{
1339 int res = FALSE;
1340 win_T *wp;
1341
1342 popup_reset_handled();
1343
1344 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1345 if (wp->w_filter_cb.cb_name != NULL)
1346 res = invoke_popup_filter(wp, c);
1347
1348 return res;
1349}
1350
Bram Moolenaar3397f742019-06-02 18:40:06 +02001351/*
1352 * Called when the cursor moved: check if any popup needs to be closed if the
1353 * cursor moved far enough.
1354 */
1355 void
1356popup_check_cursor_pos()
1357{
1358 win_T *wp;
1359 typval_T tv;
1360
1361 popup_reset_handled();
1362 while ((wp = find_next_popup(TRUE)) != NULL)
1363 if (wp->w_popup_curwin != NULL
1364 && (curwin != wp->w_popup_curwin
1365 || curwin->w_cursor.lnum != wp->w_popup_lnum
1366 || curwin->w_cursor.col < wp->w_popup_mincol
1367 || curwin->w_cursor.col > wp->w_popup_maxcol))
1368 {
1369 tv.v_type = VAR_NUMBER;
1370 tv.vval.v_number = -1;
1371 popup_close_and_callback(wp, &tv);
1372 }
1373}
1374
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001375#endif // FEAT_TEXT_PROP