blob: 3af35b6f05597630f07d15ceb1c62e25827a2eee [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;
Bram Moolenaar711d02c2019-06-28 04:06:50 +020081 dictitem_T *di;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020082
83 nr = popup_options_one(dict, (char_u *)"line");
84 if (nr > 0)
85 wp->w_wantline = nr;
86 nr = popup_options_one(dict, (char_u *)"col");
87 if (nr > 0)
88 wp->w_wantcol = nr;
89
Bram Moolenaar711d02c2019-06-28 04:06:50 +020090 di = dict_find(dict, (char_u *)"fixed", -1);
91 if (di != NULL)
92 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020093
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020094 str = dict_get_string(dict, (char_u *)"pos", FALSE);
95 if (str != NULL)
96 {
97 for (nr = 0;
98 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
99 ++nr)
100 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
101 {
102 wp->w_popup_pos = poppos_entries[nr].pp_val;
103 nr = -1;
104 break;
105 }
106 if (nr != -1)
107 semsg(_(e_invarg2), str);
108 }
109}
110
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200111 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200112set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113{
114 dictitem_T *di;
115
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200116 di = dict_find(dict, (char_u *)name, -1);
117 if (di != NULL)
118 {
119 if (di->di_tv.v_type != VAR_LIST)
120 emsg(_(e_listreq));
121 else
122 {
123 list_T *list = di->di_tv.vval.v_list;
124 listitem_T *li;
125 int i;
126 int nr;
127
128 for (i = 0; i < 4; ++i)
129 array[i] = 1;
130 if (list != NULL)
131 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
132 ++i, li = li->li_next)
133 {
134 nr = (int)tv_get_number(&li->li_tv);
135 if (nr >= 0)
136 array[i] = nr > max_val ? max_val : nr;
137 }
138 }
139 }
140}
141
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200142/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200143 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200144 */
145 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200146set_moved_values(win_T *wp)
147{
148 wp->w_popup_curwin = curwin;
149 wp->w_popup_lnum = curwin->w_cursor.lnum;
150 wp->w_popup_mincol = curwin->w_cursor.col;
151 wp->w_popup_maxcol = curwin->w_cursor.col;
152}
153
154/*
155 * Used when popup options contain "moved" with "word" or "WORD".
156 */
157 static void
158set_moved_columns(win_T *wp, int flags)
159{
160 char_u *ptr;
161 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
162
163 if (len > 0)
164 {
165 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
166 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
167 }
168}
169
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200170/*
171 * Return TRUE if "row"/"col" is on the border of the popup.
172 * The values are relative to the top-left corner.
173 */
174 int
175popup_on_border(win_T *wp, int row, int col)
176{
177 return (row == 0 && wp->w_popup_border[0] > 0)
178 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
179 || (col == 0 && wp->w_popup_border[3] > 0)
180 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
181}
182
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200183/*
184 * Return TRUE if "row"/"col" is on the "X" button of the popup.
185 * The values are relative to the top-left corner.
186 * Caller should check w_popup_close is POPCLOSE_BUTTON.
187 */
188 int
189popup_on_X_button(win_T *wp, int row, int col)
190{
191 return row == 0 && col == popup_width(wp) - 1;
192}
193
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200194// Values set when dragging a popup window starts.
195static int drag_start_row;
196static int drag_start_col;
197static int drag_start_wantline;
198static int drag_start_wantcol;
199
200/*
201 * Mouse down on border of popup window: start dragging it.
202 * Uses mouse_col and mouse_row.
203 */
204 void
205popup_start_drag(win_T *wp)
206{
207 drag_start_row = mouse_row;
208 drag_start_col = mouse_col;
209 // TODO: handle using different corner
210 if (wp->w_wantline == 0)
211 drag_start_wantline = wp->w_winrow + 1;
212 else
213 drag_start_wantline = wp->w_wantline;
214 if (wp->w_wantcol == 0)
215 drag_start_wantcol = wp->w_wincol + 1;
216 else
217 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200218
219 // Stop centering the popup
220 if (wp->w_popup_pos == POPPOS_CENTER)
221 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200222}
223
224/*
225 * Mouse moved while dragging a popup window: adjust the window popup position.
226 */
227 void
228popup_drag(win_T *wp)
229{
230 // The popup may be closed before dragging stops.
231 if (!win_valid_popup(wp))
232 return;
233
234 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
235 if (wp->w_wantline < 1)
236 wp->w_wantline = 1;
237 if (wp->w_wantline > Rows)
238 wp->w_wantline = Rows;
239 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
240 if (wp->w_wantcol < 1)
241 wp->w_wantcol = 1;
242 if (wp->w_wantcol > Columns)
243 wp->w_wantcol = Columns;
244
245 popup_adjust_position(wp);
246}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200247
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200248/*
249 * Set w_firstline to match the current "wp->w_topline".
250 */
251 void
252popup_set_firstline(win_T *wp)
253{
254 int height = wp->w_height;
255
256 wp->w_firstline = wp->w_topline;
257 popup_adjust_position(wp);
258
259 // we don't want the popup to get smaller, decrement the first line
260 // until it doesn't
261 while (wp->w_firstline > 1 && wp->w_height < height)
262 {
263 --wp->w_firstline;
264 popup_adjust_position(wp);
265 }
266}
267
268/*
269 * Handle a click in a popup window, if it is in the scrollbar.
270 */
271 void
272popup_handle_scrollbar_click(win_T *wp, int row, int col)
273{
274 int height = popup_height(wp);
275 int old_topline = wp->w_topline;
276
277 if (wp->w_has_scrollbar == 0)
278 return;
279 if (row >= wp->w_popup_border[0]
280 && row < height - wp->w_popup_border[2]
281 && col == popup_width(wp) - 1)
282 {
283 if (row >= height / 2)
284 {
285 // Click in lower half, scroll down.
286 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
287 ++wp->w_topline;
288 }
289 else if (wp->w_topline > 1)
290 // click on upper half, scroll up.
291 --wp->w_topline;
292 if (wp->w_topline != old_topline)
293 {
294 popup_set_firstline(wp);
295 redraw_win_later(wp, NOT_VALID);
296 }
297 }
298}
299
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200300#if defined(FEAT_TIMERS)
301 static void
302popup_add_timeout(win_T *wp, int time)
303{
304 char_u cbbuf[50];
305 char_u *ptr = cbbuf;
306 typval_T tv;
307
308 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
309 "{_ -> popup_close(%d)}", wp->w_id);
310 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
311 {
312 wp->w_popup_timer = create_timer(time, 0);
313 wp->w_popup_timer->tr_callback = get_callback(&tv);
314 clear_tv(&tv);
315 }
316}
317#endif
318
Bram Moolenaar17627312019-06-02 19:53:44 +0200319/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200320 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200321 */
322 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200323apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200324{
Bram Moolenaarae943152019-06-16 22:54:14 +0200325 int nr;
326
327 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
328 wp->w_minwidth = nr;
329 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
330 wp->w_minheight = nr;
331 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
332 wp->w_maxwidth = nr;
333 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
334 wp->w_maxheight = nr;
335 get_pos_options(wp, d);
336}
337
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200338 static void
339check_highlight(dict_T *dict, char *name, char_u **pval)
340{
341 dictitem_T *di;
342 char_u *str;
343
344 di = dict_find(dict, (char_u *)name, -1);
345 if (di != NULL)
346 {
347 if (di->di_tv.v_type != VAR_STRING)
348 semsg(_(e_invargval), name);
349 else
350 {
351 str = tv_get_string(&di->di_tv);
352 if (*str != NUL)
353 *pval = vim_strsave(str);
354 }
355 }
356}
357
Bram Moolenaarae943152019-06-16 22:54:14 +0200358/*
359 * Shared between popup_create() and f_popup_setoptions().
360 */
361 static void
362apply_general_options(win_T *wp, dict_T *dict)
363{
364 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200365 int nr;
366 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200367
Bram Moolenaarae943152019-06-16 22:54:14 +0200368 // TODO: flip
369
370 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200371 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200372 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
373 if (wp->w_firstline < 1)
374 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200375
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200376 di = dict_find(dict, (char_u *)"scrollbar", -1);
377 if (di != NULL)
378 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
379
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200380 str = dict_get_string(dict, (char_u *)"title", FALSE);
381 if (str != NULL)
382 {
383 vim_free(wp->w_popup_title);
384 wp->w_popup_title = vim_strsave(str);
385 }
386
Bram Moolenaar402502d2019-05-30 22:07:36 +0200387 di = dict_find(dict, (char_u *)"wrap", -1);
388 if (di != NULL)
389 {
390 nr = dict_get_number(dict, (char_u *)"wrap");
391 wp->w_p_wrap = nr != 0;
392 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200393
Bram Moolenaara42d9452019-06-15 21:46:30 +0200394 di = dict_find(dict, (char_u *)"drag", -1);
395 if (di != NULL)
396 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200397
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200398 di = dict_find(dict, (char_u *)"close", -1);
399 if (di != NULL)
400 {
401 int ok = TRUE;
402
403 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
404 {
405 char_u *s = di->di_tv.vval.v_string;
406
407 if (STRCMP(s, "none") == 0)
408 wp->w_popup_close = POPCLOSE_NONE;
409 else if (STRCMP(s, "button") == 0)
410 wp->w_popup_close = POPCLOSE_BUTTON;
411 else if (STRCMP(s, "click") == 0)
412 wp->w_popup_close = POPCLOSE_CLICK;
413 else
414 ok = FALSE;
415 }
416 else
417 ok = FALSE;
418 if (!ok)
419 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
420 }
421
Bram Moolenaarae943152019-06-16 22:54:14 +0200422 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
423 if (str != NULL)
424 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
425 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200426
Bram Moolenaarae943152019-06-16 22:54:14 +0200427 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
428 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200429
Bram Moolenaar790498b2019-06-01 22:15:29 +0200430 di = dict_find(dict, (char_u *)"borderhighlight", -1);
431 if (di != NULL)
432 {
433 if (di->di_tv.v_type != VAR_LIST)
434 emsg(_(e_listreq));
435 else
436 {
437 list_T *list = di->di_tv.vval.v_list;
438 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200439 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200440
441 if (list != NULL)
442 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
443 ++i, li = li->li_next)
444 {
445 str = tv_get_string(&li->li_tv);
446 if (*str != NUL)
447 wp->w_border_highlight[i] = vim_strsave(str);
448 }
449 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
450 for (i = 1; i < 4; ++i)
451 wp->w_border_highlight[i] =
452 vim_strsave(wp->w_border_highlight[0]);
453 }
454 }
455
Bram Moolenaar790498b2019-06-01 22:15:29 +0200456 di = dict_find(dict, (char_u *)"borderchars", -1);
457 if (di != NULL)
458 {
459 if (di->di_tv.v_type != VAR_LIST)
460 emsg(_(e_listreq));
461 else
462 {
463 list_T *list = di->di_tv.vval.v_list;
464 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200465 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200466
467 if (list != NULL)
468 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
469 ++i, li = li->li_next)
470 {
471 str = tv_get_string(&li->li_tv);
472 if (*str != NUL)
473 wp->w_border_char[i] = mb_ptr2char(str);
474 }
475 if (list->lv_len == 1)
476 for (i = 1; i < 8; ++i)
477 wp->w_border_char[i] = wp->w_border_char[0];
478 if (list->lv_len == 2)
479 {
480 for (i = 4; i < 8; ++i)
481 wp->w_border_char[i] = wp->w_border_char[1];
482 for (i = 1; i < 4; ++i)
483 wp->w_border_char[i] = wp->w_border_char[0];
484 }
485 }
486 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200487
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200488 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
489 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
490
Bram Moolenaarae943152019-06-16 22:54:14 +0200491 di = dict_find(dict, (char_u *)"zindex", -1);
492 if (di != NULL)
493 {
494 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
495 if (wp->w_zindex < 1)
496 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
497 if (wp->w_zindex > 32000)
498 wp->w_zindex = 32000;
499 }
500
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200501 di = dict_find(dict, (char_u *)"mask", -1);
502 if (di != NULL)
503 {
504 int ok = TRUE;
505
506 if (di->di_tv.v_type != VAR_LIST)
507 ok = FALSE;
508 else if (di->di_tv.vval.v_list != NULL)
509 {
510 listitem_T *li;
511
512 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
513 li = li->li_next)
514 {
515 if (li->li_tv.v_type != VAR_LIST
516 || li->li_tv.vval.v_list == NULL
517 || li->li_tv.vval.v_list->lv_len != 4)
518 {
519 ok = FALSE;
520 break;
521 }
522 }
523 }
524 if (ok)
525 {
526 wp->w_popup_mask = di->di_tv.vval.v_list;
527 ++wp->w_popup_mask->lv_refcount;
528 }
529 else
530 semsg(_(e_invargval), "mask");
531 }
532
Bram Moolenaarae943152019-06-16 22:54:14 +0200533#if defined(FEAT_TIMERS)
534 // Add timer to close the popup after some time.
535 nr = dict_get_number(dict, (char_u *)"time");
536 if (nr > 0)
537 popup_add_timeout(wp, nr);
538#endif
539
Bram Moolenaar3397f742019-06-02 18:40:06 +0200540 di = dict_find(dict, (char_u *)"moved", -1);
541 if (di != NULL)
542 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200543 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200544 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
545 {
546 char_u *s = di->di_tv.vval.v_string;
547 int flags = 0;
548
549 if (STRCMP(s, "word") == 0)
550 flags = FIND_IDENT | FIND_STRING;
551 else if (STRCMP(s, "WORD") == 0)
552 flags = FIND_STRING;
553 else if (STRCMP(s, "any") != 0)
554 semsg(_(e_invarg2), s);
555 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200556 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200557 }
558 else if (di->di_tv.v_type == VAR_LIST
559 && di->di_tv.vval.v_list != NULL
560 && di->di_tv.vval.v_list->lv_len == 2)
561 {
562 list_T *l = di->di_tv.vval.v_list;
563
564 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
565 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
566 }
567 else
568 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
569 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200570
Bram Moolenaarae943152019-06-16 22:54:14 +0200571 di = dict_find(dict, (char_u *)"filter", -1);
572 if (di != NULL)
573 {
574 callback_T callback = get_callback(&di->di_tv);
575
576 if (callback.cb_name != NULL)
577 {
578 free_callback(&wp->w_filter_cb);
579 set_callback(&wp->w_filter_cb, &callback);
580 }
581 }
582
583 di = dict_find(dict, (char_u *)"callback", -1);
584 if (di != NULL)
585 {
586 callback_T callback = get_callback(&di->di_tv);
587
588 if (callback.cb_name != NULL)
589 {
590 free_callback(&wp->w_close_cb);
591 set_callback(&wp->w_close_cb, &callback);
592 }
593 }
594}
595
596/*
597 * Go through the options in "dict" and apply them to popup window "wp".
598 */
599 static void
600apply_options(win_T *wp, dict_T *dict)
601{
602 int nr;
603
604 apply_move_options(wp, dict);
605 apply_general_options(wp, dict);
606
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200607 nr = dict_get_number(dict, (char_u *)"hidden");
608 if (nr > 0)
609 {
610 wp->w_popup_flags |= POPF_HIDDEN;
611 --wp->w_buffer->b_nwindows;
612 }
613
Bram Moolenaar33796b32019-06-08 16:01:13 +0200614 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200615}
616
617/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200618 * Add lines to the popup from a list of strings.
619 */
620 static void
621add_popup_strings(buf_T *buf, list_T *l)
622{
623 listitem_T *li;
624 linenr_T lnum = 0;
625 char_u *p;
626
627 for (li = l->lv_first; li != NULL; li = li->li_next)
628 if (li->li_tv.v_type == VAR_STRING)
629 {
630 p = li->li_tv.vval.v_string;
631 ml_append_buf(buf, lnum++,
632 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
633 }
634}
635
636/*
637 * Add lines to the popup from a list of dictionaries.
638 */
639 static void
640add_popup_dicts(buf_T *buf, list_T *l)
641{
642 listitem_T *li;
643 listitem_T *pli;
644 linenr_T lnum = 0;
645 char_u *p;
646 dict_T *dict;
647
648 // first add the text lines
649 for (li = l->lv_first; li != NULL; li = li->li_next)
650 {
651 if (li->li_tv.v_type != VAR_DICT)
652 {
653 emsg(_(e_dictreq));
654 return;
655 }
656 dict = li->li_tv.vval.v_dict;
657 p = dict == NULL ? NULL
658 : dict_get_string(dict, (char_u *)"text", FALSE);
659 ml_append_buf(buf, lnum++,
660 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
661 }
662
663 // add the text properties
664 lnum = 1;
665 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
666 {
667 dictitem_T *di;
668 list_T *plist;
669
670 dict = li->li_tv.vval.v_dict;
671 di = dict_find(dict, (char_u *)"props", -1);
672 if (di != NULL)
673 {
674 if (di->di_tv.v_type != VAR_LIST)
675 {
676 emsg(_(e_listreq));
677 return;
678 }
679 plist = di->di_tv.vval.v_list;
680 if (plist != NULL)
681 {
682 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
683 {
684 if (pli->li_tv.v_type != VAR_DICT)
685 {
686 emsg(_(e_dictreq));
687 return;
688 }
689 dict = pli->li_tv.vval.v_dict;
690 if (dict != NULL)
691 {
692 int col = dict_get_number(dict, (char_u *)"col");
693
694 prop_add_common( lnum, col, dict, buf, NULL);
695 }
696 }
697 }
698 }
699 }
700}
701
702/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200703 * Return the height of popup window "wp", including border and padding.
704 */
705 int
706popup_height(win_T *wp)
707{
708 return wp->w_height
709 + wp->w_popup_padding[0] + wp->w_popup_border[0]
710 + wp->w_popup_padding[2] + wp->w_popup_border[2];
711}
712
713/*
714 * Return the width of popup window "wp", including border and padding.
715 */
716 int
717popup_width(win_T *wp)
718{
719 return wp->w_width
720 + wp->w_popup_padding[3] + wp->w_popup_border[3]
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200721 + wp->w_popup_padding[1] + wp->w_popup_border[1]
722 + wp->w_has_scrollbar;
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200723}
724
725/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200726 * Get the padding plus border at the top, adjusted to 1 if there is a title.
727 */
728 static int
729popup_top_extra(win_T *wp)
730{
731 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
732
733 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
734 return 1;
735 return extra;
736}
737
738/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200739 * Adjust the position and size of the popup to fit on the screen.
740 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200741 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200742popup_adjust_position(win_T *wp)
743{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200744 linenr_T lnum;
745 int wrapped = 0;
746 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200747 int center_vert = FALSE;
748 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200749 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200750 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200751 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
752 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
753 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
754 int extra_height = top_extra + bot_extra;
755 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200756 int org_winrow = wp->w_winrow;
757 int org_wincol = wp->w_wincol;
758 int org_width = wp->w_width;
759 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200760 int org_leftcol = wp->w_leftcol;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200761 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200762
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200763 wp->w_winrow = 0;
764 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200765 wp->w_leftcol = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200766 if (wp->w_popup_pos == POPPOS_CENTER)
767 {
768 // center after computing the size
769 center_vert = TRUE;
770 center_hor = TRUE;
771 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200772 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200773 {
774 if (wp->w_wantline == 0)
775 center_vert = TRUE;
776 else if (wp->w_popup_pos == POPPOS_TOPLEFT
777 || wp->w_popup_pos == POPPOS_TOPRIGHT)
778 {
779 wp->w_winrow = wp->w_wantline - 1;
780 if (wp->w_winrow >= Rows)
781 wp->w_winrow = Rows - 1;
782 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200783
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200784 if (wp->w_wantcol == 0)
785 center_hor = TRUE;
786 else if (wp->w_popup_pos == POPPOS_TOPLEFT
787 || wp->w_popup_pos == POPPOS_BOTLEFT)
788 {
789 wp->w_wincol = wp->w_wantcol - 1;
790 if (wp->w_wincol >= Columns - 3)
791 wp->w_wincol = Columns - 3;
792 }
793 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200794
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200795 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200796 // When left aligned use the space available, but shift to the left when we
797 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200798 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200799 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200800 {
801 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200802 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200803 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200804
Bram Moolenaar8d241042019-06-12 23:40:01 +0200805 // start at the desired first line
806 wp->w_topline = wp->w_firstline;
807 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
808 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
809
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200810 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200811 // Use a minimum width of one, so that something shows when there is no
812 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200813 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200814 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200815 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200816 {
817 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
818
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200819 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200820 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200821 while (len > maxwidth)
822 {
823 ++wrapped;
824 len -= maxwidth;
825 wp->w_width = maxwidth;
826 }
827 }
828 else if (len > maxwidth
829 && allow_adjust_left
830 && (wp->w_popup_pos == POPPOS_TOPLEFT
831 || wp->w_popup_pos == POPPOS_BOTLEFT))
832 {
833 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200834 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200835
Bram Moolenaar51c31312019-06-15 22:27:23 +0200836 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200837 {
838 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200839
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200840 len -= truncate_shift;
841 shift_by -= truncate_shift;
842 }
843
844 wp->w_wincol -= shift_by;
845 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200846 wp->w_width = maxwidth;
847 }
848 if (wp->w_width < len)
849 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200850 // do not use the width of lines we're not going to show
851 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
852 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
853 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200854 }
855
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200856 wp->w_has_scrollbar = wp->w_want_scrollbar
857 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
858
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200859 minwidth = wp->w_minwidth;
860 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
861 {
862 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
863
864 if (minwidth < title_len)
865 minwidth = title_len;
866 }
867
868 if (minwidth > 0 && wp->w_width < minwidth)
869 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200870 if (wp->w_width > maxwidth)
871 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200872 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200873 {
874 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
875 if (wp->w_wincol < 0)
876 wp->w_wincol = 0;
877 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200878 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
879 || wp->w_popup_pos == POPPOS_TOPRIGHT)
880 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200881 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
882
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200883 // Right aligned: move to the right if needed.
884 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200885 if (leftoff >= 0)
886 wp->w_wincol = leftoff;
887 else if (wp->w_popup_fixed)
888 {
889 // "col" specifies the right edge, but popup doesn't fit, skip some
890 // columns when displaying the window.
891 wp->w_leftcol = -leftoff;
892 wp->w_width += leftoff;
893 if (wp->w_width < 0)
894 wp->w_width = 0;
895 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200896 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200897
Bram Moolenaar8d241042019-06-12 23:40:01 +0200898 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
899 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200900 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
901 wp->w_height = wp->w_minheight;
902 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
903 wp->w_height = wp->w_maxheight;
904 if (wp->w_height > Rows - wp->w_winrow)
905 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200906
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200907 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200908 {
909 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
910 if (wp->w_winrow < 0)
911 wp->w_winrow = 0;
912 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200913 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
914 || wp->w_popup_pos == POPPOS_BOTLEFT)
915 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200916 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200917 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200918 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200919 else
920 // not enough space, make top aligned
921 wp->w_winrow = wp->w_wantline + 1;
922 }
923
Bram Moolenaar17146962019-05-30 00:12:11 +0200924 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200925
926 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200927 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200928 if (org_winrow != wp->w_winrow
929 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200930 || org_leftcol != wp->w_leftcol
Bram Moolenaar33796b32019-06-08 16:01:13 +0200931 || org_width != wp->w_width
932 || org_height != wp->w_height)
933 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200934 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200935 popup_mask_refresh = TRUE;
936 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200937}
938
Bram Moolenaar17627312019-06-02 19:53:44 +0200939typedef enum
940{
941 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200942 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200943 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200944 TYPE_DIALOG,
945 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200946} create_type_T;
947
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200948/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200949 * Make "buf" empty and set the contents to "text".
950 * Used by popup_create() and popup_settext().
951 */
952 static void
953popup_set_buffer_text(buf_T *buf, typval_T text)
954{
955 int lnum;
956
957 // Clear the buffer, then replace the lines.
958 curbuf = buf;
959 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
960 ml_delete(lnum, FALSE);
961 curbuf = curwin->w_buffer;
962
963 // Add text to the buffer.
964 if (text.v_type == VAR_STRING)
965 {
966 // just a string
967 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
968 }
969 else
970 {
971 list_T *l = text.vval.v_list;
972
973 if (l->lv_len > 0)
974 {
975 if (l->lv_first->li_tv.v_type == VAR_STRING)
976 // list of strings
977 add_popup_strings(buf, l);
978 else
979 // list of dictionaries
980 add_popup_dicts(buf, l);
981 }
982 }
983
984 // delete the line that was in the empty buffer
985 curbuf = buf;
986 ml_delete(buf->b_ml.ml_line_count, FALSE);
987 curbuf = curwin->w_buffer;
988}
989
990/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200991 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200992 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200993 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200994 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200995popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200996{
Bram Moolenaara3fce622019-06-20 02:31:49 +0200997 win_T *wp;
998 tabpage_T *tp = NULL;
999 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001000 int new_buffer;
1001 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001002 dict_T *d;
1003 int nr;
1004 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001005
1006 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001007 if (argvars[0].v_type == VAR_NUMBER)
1008 {
1009 buf = buflist_findnr( argvars[0].vval.v_number);
1010 if (buf == NULL)
1011 {
1012 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1013 return NULL;
1014 }
1015 }
1016 else if (!(argvars[0].v_type == VAR_STRING
1017 && argvars[0].vval.v_string != NULL)
1018 && !(argvars[0].v_type == VAR_LIST
1019 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001020 {
1021 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001022 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001023 }
1024 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1025 {
1026 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001027 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001028 }
1029 d = argvars[1].vval.v_dict;
1030
Bram Moolenaara3fce622019-06-20 02:31:49 +02001031 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1032 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1033 else if (type == TYPE_NOTIFICATION)
1034 tabnr = -1; // notifications are global by default
1035 else
1036 tabnr = 0;
1037 if (tabnr > 0)
1038 {
1039 tp = find_tabpage(tabnr);
1040 if (tp == NULL)
1041 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001042 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001043 return NULL;
1044 }
1045 }
1046
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001047 // Create the window and buffer.
1048 wp = win_alloc_popup_win();
1049 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001050 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001051 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001052 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001053 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001054
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001055 if (buf != NULL)
1056 {
1057 // use existing buffer
1058 new_buffer = FALSE;
1059 wp->w_buffer = buf;
1060 ++buf->b_nwindows;
1061 buffer_ensure_loaded(buf);
1062 }
1063 else
1064 {
1065 // create a new buffer associated with the popup
1066 new_buffer = TRUE;
1067 buf = buflist_new(NULL, NULL, (linenr_T)0,
1068 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1069 if (buf == NULL)
1070 return NULL;
1071 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001072
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001073 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001074
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001075 set_local_options_default(wp);
1076 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001077 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001078 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1079 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1080 buf->b_p_ul = -1; // no undo
1081 buf->b_p_swf = FALSE; // no swap file
1082 buf->b_p_bl = FALSE; // unlisted buffer
1083 buf->b_locked = TRUE;
1084 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001085
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001086 // Avoid that 'buftype' is reset when this buffer is entered.
1087 buf->b_p_initialized = TRUE;
1088 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001089
Bram Moolenaara3fce622019-06-20 02:31:49 +02001090 if (tp != NULL)
1091 {
1092 // popup on specified tab page
1093 wp->w_next = tp->tp_first_popupwin;
1094 tp->tp_first_popupwin = wp;
1095 }
1096 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001097 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001098 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001099 wp->w_next = curtab->tp_first_popupwin;
1100 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001101 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001102 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001103 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001104 win_T *prev = first_popupwin;
1105
1106 // Global popup: add at the end, so that it gets displayed on top of
1107 // older ones with the same zindex. Matters for notifications.
1108 if (first_popupwin == NULL)
1109 first_popupwin = wp;
1110 else
1111 {
1112 while (prev->w_next != NULL)
1113 prev = prev->w_next;
1114 prev->w_next = wp;
1115 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001116 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001117
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001118 if (new_buffer)
1119 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001120
Bram Moolenaar17627312019-06-02 19:53:44 +02001121 if (type == TYPE_ATCURSOR)
1122 {
1123 wp->w_popup_pos = POPPOS_BOTLEFT;
1124 setcursor_mayforce(TRUE);
1125 wp->w_wantline = screen_screenrow();
1126 if (wp->w_wantline == 0) // cursor in first line
1127 {
1128 wp->w_wantline = 2;
1129 wp->w_popup_pos = POPPOS_TOPLEFT;
1130 }
1131 wp->w_wantcol = screen_screencol() + 1;
1132 set_moved_values(wp);
1133 set_moved_columns(wp, FIND_STRING);
1134 }
1135
Bram Moolenaar33796b32019-06-08 16:01:13 +02001136 // set default values
1137 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001138 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001139
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001140 if (type == TYPE_NOTIFICATION)
1141 {
1142 win_T *twp, *nextwin;
1143 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001144
1145 // Try to not overlap with another global popup. Guess we need 3
1146 // more screen lines than buffer lines.
1147 wp->w_wantline = 1;
1148 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1149 {
1150 nextwin = twp->w_next;
1151 if (twp != wp
1152 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1153 && twp->w_winrow <= wp->w_wantline - 1 + height
1154 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1155 {
1156 // move to below this popup and restart the loop to check for
1157 // overlap with other popups
1158 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1159 nextwin = first_popupwin;
1160 }
1161 }
1162 if (wp->w_wantline + height > Rows)
1163 {
1164 // can't avoid overlap, put on top in the hope that message goes
1165 // away soon.
1166 wp->w_wantline = 1;
1167 }
1168
1169 wp->w_wantcol = 10;
1170 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001171 wp->w_minwidth = 20;
1172 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001173 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001174 for (i = 0; i < 4; ++i)
1175 wp->w_popup_border[i] = 1;
1176 wp->w_popup_padding[1] = 1;
1177 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001178
1179 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001180 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001181 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1182 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001183 }
1184
Bram Moolenaara730e552019-06-16 19:05:31 +02001185 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001186 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001187 wp->w_popup_pos = POPPOS_CENTER;
1188 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1189 wp->w_popup_drag = 1;
1190 for (i = 0; i < 4; ++i)
1191 {
1192 wp->w_popup_border[i] = 1;
1193 wp->w_popup_padding[i] = 1;
1194 }
1195 }
1196
Bram Moolenaara730e552019-06-16 19:05:31 +02001197 if (type == TYPE_MENU)
1198 {
1199 typval_T tv;
1200 callback_T callback;
1201
1202 tv.v_type = VAR_STRING;
1203 tv.vval.v_string = (char_u *)"popup_filter_menu";
1204 callback = get_callback(&tv);
1205 if (callback.cb_name != NULL)
1206 set_callback(&wp->w_filter_cb, &callback);
1207
1208 wp->w_p_wrap = 0;
1209 }
1210
Bram Moolenaarae943152019-06-16 22:54:14 +02001211 for (i = 0; i < 4; ++i)
1212 VIM_CLEAR(wp->w_border_highlight[i]);
1213 for (i = 0; i < 8; ++i)
1214 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001215 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001216 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001217
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001218 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001219 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001220
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001221#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001222 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1223 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001224#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001225
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001226 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001227
1228 wp->w_vsep_width = 0;
1229
1230 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001231 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001232
1233 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001234}
1235
1236/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001237 * popup_clear()
1238 */
1239 void
1240f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1241{
1242 close_all_popups();
1243}
1244
1245/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001246 * popup_create({text}, {options})
1247 */
1248 void
1249f_popup_create(typval_T *argvars, typval_T *rettv)
1250{
Bram Moolenaar17627312019-06-02 19:53:44 +02001251 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001252}
1253
1254/*
1255 * popup_atcursor({text}, {options})
1256 */
1257 void
1258f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1259{
Bram Moolenaar17627312019-06-02 19:53:44 +02001260 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001261}
1262
1263/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001264 * Invoke the close callback for window "wp" with value "result".
1265 * Careful: The callback may make "wp" invalid!
1266 */
1267 static void
1268invoke_popup_callback(win_T *wp, typval_T *result)
1269{
1270 typval_T rettv;
1271 int dummy;
1272 typval_T argv[3];
1273
1274 argv[0].v_type = VAR_NUMBER;
1275 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1276
1277 if (result != NULL && result->v_type != VAR_UNKNOWN)
1278 copy_tv(result, &argv[1]);
1279 else
1280 {
1281 argv[1].v_type = VAR_NUMBER;
1282 argv[1].vval.v_number = 0;
1283 }
1284
1285 argv[2].v_type = VAR_UNKNOWN;
1286
1287 call_callback(&wp->w_close_cb, -1,
1288 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1289 if (result != NULL)
1290 clear_tv(&argv[1]);
1291 clear_tv(&rettv);
1292}
1293
1294/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001295 * Close popup "wp" and invoke any close callback for it.
1296 */
1297 static void
1298popup_close_and_callback(win_T *wp, typval_T *arg)
1299{
1300 int id = wp->w_id;
1301
1302 if (wp->w_close_cb.cb_name != NULL)
1303 // Careful: This may make "wp" invalid.
1304 invoke_popup_callback(wp, arg);
1305
1306 popup_close(id);
1307}
1308
1309/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001310 * Close popup "wp" because of a mouse click.
1311 */
1312 void
1313popup_close_for_mouse_click(win_T *wp)
1314{
1315 typval_T res;
1316
1317 res.v_type = VAR_NUMBER;
1318 res.vval.v_number = -2;
1319 popup_close_and_callback(wp, &res);
1320}
1321
1322/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001323 * In a filter: check if the typed key is a mouse event that is used for
1324 * dragging the popup.
1325 */
1326 static void
1327filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1328{
1329 int row = mouse_row;
1330 int col = mouse_col;
1331
1332 if (wp->w_popup_drag
1333 && is_mouse_key(c)
1334 && (wp == popup_dragwin
1335 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1336 // do not consume the key, allow for dragging the popup
1337 rettv->vval.v_number = 0;
1338}
1339
1340 static void
1341popup_highlight_curline(win_T *wp)
1342{
1343 int id;
1344 char buf[100];
1345
1346 match_delete(wp, 1, FALSE);
1347
1348 id = syn_name2id((char_u *)"PopupSelected");
1349 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1350 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1351 (char_u *)buf, 10, 1, NULL, NULL);
1352}
1353
1354/*
1355 * popup_filter_menu({text}, {options})
1356 */
1357 void
1358f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1359{
1360 int id = tv_get_number(&argvars[0]);
1361 win_T *wp = win_id2wp(id);
1362 char_u *key = tv_get_string(&argvars[1]);
1363 typval_T res;
1364 int c;
1365 linenr_T old_lnum;
1366
1367 // If the popup has been closed do not consume the key.
1368 if (wp == NULL)
1369 return;
1370
1371 c = *key;
1372 if (c == K_SPECIAL && key[1] != NUL)
1373 c = TO_SPECIAL(key[1], key[2]);
1374
1375 // consume all keys until done
1376 rettv->vval.v_number = 1;
1377 res.v_type = VAR_NUMBER;
1378
1379 old_lnum = wp->w_cursor.lnum;
1380 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1381 --wp->w_cursor.lnum;
1382 if ((c == 'j' || c == 'J' || c == K_DOWN)
1383 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1384 ++wp->w_cursor.lnum;
1385 if (old_lnum != wp->w_cursor.lnum)
1386 {
1387 popup_highlight_curline(wp);
1388 return;
1389 }
1390
1391 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1392 {
1393 // Cancelled, invoke callback with -1
1394 res.vval.v_number = -1;
1395 popup_close_and_callback(wp, &res);
1396 return;
1397 }
1398 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1399 {
1400 // Invoke callback with current index.
1401 res.vval.v_number = wp->w_cursor.lnum;
1402 popup_close_and_callback(wp, &res);
1403 return;
1404 }
1405
1406 filter_handle_drag(wp, c, rettv);
1407}
1408
1409/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001410 * popup_filter_yesno({text}, {options})
1411 */
1412 void
1413f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1414{
1415 int id = tv_get_number(&argvars[0]);
1416 win_T *wp = win_id2wp(id);
1417 char_u *key = tv_get_string(&argvars[1]);
1418 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001419 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001420
1421 // If the popup has been closed don't consume the key.
1422 if (wp == NULL)
1423 return;
1424
Bram Moolenaara730e552019-06-16 19:05:31 +02001425 c = *key;
1426 if (c == K_SPECIAL && key[1] != NUL)
1427 c = TO_SPECIAL(key[1], key[2]);
1428
Bram Moolenaara42d9452019-06-15 21:46:30 +02001429 // consume all keys until done
1430 rettv->vval.v_number = 1;
1431
Bram Moolenaara730e552019-06-16 19:05:31 +02001432 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001433 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001434 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001435 res.vval.v_number = 0;
1436 else
1437 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001438 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001439 return;
1440 }
1441
1442 // Invoke callback
1443 res.v_type = VAR_NUMBER;
1444 popup_close_and_callback(wp, &res);
1445}
1446
1447/*
1448 * popup_dialog({text}, {options})
1449 */
1450 void
1451f_popup_dialog(typval_T *argvars, typval_T *rettv)
1452{
1453 popup_create(argvars, rettv, TYPE_DIALOG);
1454}
1455
1456/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001457 * popup_menu({text}, {options})
1458 */
1459 void
1460f_popup_menu(typval_T *argvars, typval_T *rettv)
1461{
1462 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1463
1464 if (wp != NULL)
1465 popup_highlight_curline(wp);
1466}
1467
1468/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001469 * popup_notification({text}, {options})
1470 */
1471 void
1472f_popup_notification(typval_T *argvars, typval_T *rettv)
1473{
1474 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1475}
1476
1477/*
1478 * Find the popup window with window-ID "id".
1479 * If the popup window does not exist NULL is returned.
1480 * If the window is not a popup window, and error message is given.
1481 */
1482 static win_T *
1483find_popup_win(int id)
1484{
1485 win_T *wp = win_id2wp(id);
1486
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001487 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001488 {
1489 semsg(_("E993: window %d is not a popup window"), id);
1490 return NULL;
1491 }
1492 return wp;
1493}
1494
1495/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001496 * popup_close({id})
1497 */
1498 void
1499f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1500{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001501 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001502 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001503
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001504 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001505 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001506}
1507
1508/*
1509 * popup_hide({id})
1510 */
1511 void
1512f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1513{
1514 int id = (int)tv_get_number(argvars);
1515 win_T *wp = find_popup_win(id);
1516
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001517 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001518 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001519 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001520 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001521 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001522 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001523 }
1524}
1525
1526/*
1527 * popup_show({id})
1528 */
1529 void
1530f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1531{
1532 int id = (int)tv_get_number(argvars);
1533 win_T *wp = find_popup_win(id);
1534
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001535 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001536 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001537 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001538 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001539 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001540 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001541 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001542}
1543
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001544/*
1545 * popup_settext({id}, {text})
1546 */
1547 void
1548f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1549{
1550 int id = (int)tv_get_number(&argvars[0]);
1551 win_T *wp = find_popup_win(id);
1552
1553 if (wp != NULL)
1554 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001555 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1556 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1557 else
1558 {
1559 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1560 popup_adjust_position(wp);
1561 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001562 }
1563}
1564
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001565 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001566popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001567{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001568 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001569 if (wp->w_winrow + wp->w_height >= cmdline_row)
1570 clear_cmdline = TRUE;
1571 win_free_popup(wp);
1572 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001573 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001574}
1575
Bram Moolenaarec583842019-05-26 14:11:23 +02001576/*
1577 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001578 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001579 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001580 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001581popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001582{
1583 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001584 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001585 win_T *prev = NULL;
1586
Bram Moolenaarec583842019-05-26 14:11:23 +02001587 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001588 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001589 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001590 {
1591 if (prev == NULL)
1592 first_popupwin = wp->w_next;
1593 else
1594 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001595 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001596 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001597 }
1598
Bram Moolenaarec583842019-05-26 14:11:23 +02001599 // go through tab-local popups
1600 FOR_ALL_TABPAGES(tp)
1601 popup_close_tabpage(tp, id);
1602}
1603
1604/*
1605 * Close a popup window with Window-id "id" in tabpage "tp".
1606 */
1607 void
1608popup_close_tabpage(tabpage_T *tp, int id)
1609{
1610 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001611 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001612 win_T *prev = NULL;
1613
Bram Moolenaarec583842019-05-26 14:11:23 +02001614 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1615 if (wp->w_id == id)
1616 {
1617 if (prev == NULL)
1618 *root = wp->w_next;
1619 else
1620 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001621 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001622 return;
1623 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001624}
1625
1626 void
1627close_all_popups(void)
1628{
1629 while (first_popupwin != NULL)
1630 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001631 while (curtab->tp_first_popupwin != NULL)
1632 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001633}
1634
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001635/*
1636 * popup_move({id}, {options})
1637 */
1638 void
1639f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1640{
Bram Moolenaarae943152019-06-16 22:54:14 +02001641 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001642 int id = (int)tv_get_number(argvars);
1643 win_T *wp = find_popup_win(id);
1644
1645 if (wp == NULL)
1646 return; // invalid {id}
1647
1648 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1649 {
1650 emsg(_(e_dictreq));
1651 return;
1652 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001653 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001654
Bram Moolenaarae943152019-06-16 22:54:14 +02001655 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001656
1657 if (wp->w_winrow + wp->w_height >= cmdline_row)
1658 clear_cmdline = TRUE;
1659 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001660}
1661
Bram Moolenaarbc133542019-05-29 20:26:46 +02001662/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001663 * popup_setoptions({id}, {options})
1664 */
1665 void
1666f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1667{
1668 dict_T *dict;
1669 int id = (int)tv_get_number(argvars);
1670 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001671 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001672
1673 if (wp == NULL)
1674 return; // invalid {id}
1675
1676 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1677 {
1678 emsg(_(e_dictreq));
1679 return;
1680 }
1681 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001682 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001683
1684 apply_move_options(wp, dict);
1685 apply_general_options(wp, dict);
1686
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001687 if (old_firstline != wp->w_firstline)
1688 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001689 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001690 popup_adjust_position(wp);
1691}
1692
1693/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001694 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001695 */
1696 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001697f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001698{
1699 dict_T *dict;
1700 int id = (int)tv_get_number(argvars);
1701 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001702 int top_extra;
1703 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001704
1705 if (rettv_dict_alloc(rettv) == OK)
1706 {
1707 if (wp == NULL)
1708 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001709 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001710 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1711
Bram Moolenaarbc133542019-05-29 20:26:46 +02001712 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001713
Bram Moolenaarbc133542019-05-29 20:26:46 +02001714 dict_add_number(dict, "line", wp->w_winrow + 1);
1715 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001716 dict_add_number(dict, "width", wp->w_width + left_extra
1717 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1718 dict_add_number(dict, "height", wp->w_height + top_extra
1719 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001720
1721 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1722 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1723 dict_add_number(dict, "core_width", wp->w_width);
1724 dict_add_number(dict, "core_height", wp->w_height);
1725
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001726 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001727 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001728 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001729 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001730 }
1731}
1732
1733/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001734 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1735 */
1736 static void
1737get_padding_border(dict_T *dict, int *array, char *name)
1738{
1739 list_T *list;
1740 int i;
1741
1742 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1743 return;
1744
1745 list = list_alloc();
1746 if (list != NULL)
1747 {
1748 dict_add_list(dict, name, list);
1749 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1750 for (i = 0; i < 4; ++i)
1751 list_append_number(list, array[i]);
1752 }
1753}
1754
1755/*
1756 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1757 */
1758 static void
1759get_borderhighlight(dict_T *dict, win_T *wp)
1760{
1761 list_T *list;
1762 int i;
1763
1764 for (i = 0; i < 4; ++i)
1765 if (wp->w_border_highlight[i] != NULL)
1766 break;
1767 if (i == 4)
1768 return;
1769
1770 list = list_alloc();
1771 if (list != NULL)
1772 {
1773 dict_add_list(dict, "borderhighlight", list);
1774 for (i = 0; i < 4; ++i)
1775 list_append_string(list, wp->w_border_highlight[i], -1);
1776 }
1777}
1778
1779/*
1780 * For popup_getoptions(): add a "borderchars" entry to "dict".
1781 */
1782 static void
1783get_borderchars(dict_T *dict, win_T *wp)
1784{
1785 list_T *list;
1786 int i;
1787 char_u buf[NUMBUFLEN];
1788 int len;
1789
1790 for (i = 0; i < 8; ++i)
1791 if (wp->w_border_char[i] != 0)
1792 break;
1793 if (i == 8)
1794 return;
1795
1796 list = list_alloc();
1797 if (list != NULL)
1798 {
1799 dict_add_list(dict, "borderchars", list);
1800 for (i = 0; i < 8; ++i)
1801 {
1802 len = mb_char2bytes(wp->w_border_char[i], buf);
1803 list_append_string(list, buf, len);
1804 }
1805 }
1806}
1807
1808/*
1809 * For popup_getoptions(): add a "moved" entry to "dict".
1810 */
1811 static void
1812get_moved_list(dict_T *dict, win_T *wp)
1813{
1814 list_T *list;
1815
1816 list = list_alloc();
1817 if (list != NULL)
1818 {
1819 dict_add_list(dict, "moved", list);
1820 list_append_number(list, wp->w_popup_mincol);
1821 list_append_number(list, wp->w_popup_maxcol);
1822 }
1823}
1824
1825/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001826 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001827 */
1828 void
1829f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1830{
1831 dict_T *dict;
1832 int id = (int)tv_get_number(argvars);
1833 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001834 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001835 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001836
1837 if (rettv_dict_alloc(rettv) == OK)
1838 {
1839 if (wp == NULL)
1840 return;
1841
1842 dict = rettv->vval.v_dict;
1843 dict_add_number(dict, "line", wp->w_wantline);
1844 dict_add_number(dict, "col", wp->w_wantcol);
1845 dict_add_number(dict, "minwidth", wp->w_minwidth);
1846 dict_add_number(dict, "minheight", wp->w_minheight);
1847 dict_add_number(dict, "maxheight", wp->w_maxheight);
1848 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001849 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001850 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001851 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001852 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001853 dict_add_string(dict, "title", wp->w_popup_title);
1854 dict_add_number(dict, "wrap", wp->w_p_wrap);
1855 dict_add_number(dict, "drag", wp->w_popup_drag);
1856 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02001857 if (wp->w_scrollbar_highlight != NULL)
1858 dict_add_string(dict, "scrollbarhighlight",
1859 wp->w_scrollbar_highlight);
1860 if (wp->w_thumb_highlight != NULL)
1861 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02001862
Bram Moolenaara3fce622019-06-20 02:31:49 +02001863 // find the tabpage that holds this popup
1864 i = 1;
1865 FOR_ALL_TABPAGES(tp)
1866 {
1867 win_T *p;
1868
1869 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
1870 if (p->w_id == id)
1871 break;
1872 if (p != NULL)
1873 break;
1874 ++i;
1875 }
1876 if (tp == NULL)
1877 i = -1; // must be global
1878 else if (tp == curtab)
1879 i = 0;
1880 dict_add_number(dict, "tabpage", i);
1881
Bram Moolenaarae943152019-06-16 22:54:14 +02001882 get_padding_border(dict, wp->w_popup_padding, "padding");
1883 get_padding_border(dict, wp->w_popup_border, "border");
1884 get_borderhighlight(dict, wp);
1885 get_borderchars(dict, wp);
1886 get_moved_list(dict, wp);
1887
1888 if (wp->w_filter_cb.cb_name != NULL)
1889 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1890 if (wp->w_close_cb.cb_name != NULL)
1891 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001892
1893 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1894 ++i)
1895 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1896 {
1897 dict_add_string(dict, "pos",
1898 (char_u *)poppos_entries[i].pp_name);
1899 break;
1900 }
1901
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001902 dict_add_string(dict, "close", (char_u *)(
1903 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
1904 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
1905
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001906# if defined(FEAT_TIMERS)
1907 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1908 ? (long)wp->w_popup_timer->tr_interval : 0L);
1909# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001910 }
1911}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001912
1913 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001914error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001915{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001916 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001917 {
1918 emsg(_("E994: Not allowed in a popup window"));
1919 return TRUE;
1920 }
1921 return FALSE;
1922}
1923
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001924/*
1925 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001926 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001927 */
1928 void
1929popup_reset_handled()
1930{
1931 win_T *wp;
1932
1933 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1934 wp->w_popup_flags &= ~POPF_HANDLED;
1935 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1936 wp->w_popup_flags &= ~POPF_HANDLED;
1937}
1938
1939/*
1940 * Find the next visible popup where POPF_HANDLED is not set.
1941 * Must have called popup_reset_handled() first.
1942 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1943 * popup with the highest zindex.
1944 */
1945 win_T *
1946find_next_popup(int lowest)
1947{
1948 win_T *wp;
1949 win_T *found_wp;
1950 int found_zindex;
1951
1952 found_zindex = lowest ? INT_MAX : 0;
1953 found_wp = NULL;
1954 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1955 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1956 && (lowest ? wp->w_zindex < found_zindex
1957 : wp->w_zindex > found_zindex))
1958 {
1959 found_zindex = wp->w_zindex;
1960 found_wp = wp;
1961 }
1962 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1963 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1964 && (lowest ? wp->w_zindex < found_zindex
1965 : wp->w_zindex > found_zindex))
1966 {
1967 found_zindex = wp->w_zindex;
1968 found_wp = wp;
1969 }
1970
1971 if (found_wp != NULL)
1972 found_wp->w_popup_flags |= POPF_HANDLED;
1973 return found_wp;
1974}
1975
1976/*
1977 * Invoke the filter callback for window "wp" with typed character "c".
1978 * Uses the global "mod_mask" for modifiers.
1979 * Returns the return value of the filter.
1980 * Careful: The filter may make "wp" invalid!
1981 */
1982 static int
1983invoke_popup_filter(win_T *wp, int c)
1984{
1985 int res;
1986 typval_T rettv;
1987 int dummy;
1988 typval_T argv[3];
1989 char_u buf[NUMBUFLEN];
1990
Bram Moolenaara42d9452019-06-15 21:46:30 +02001991 // Emergency exit: CTRL-C closes the popup.
1992 if (c == Ctrl_C)
1993 {
1994 rettv.v_type = VAR_NUMBER;
1995 rettv.vval.v_number = -1;
1996 popup_close_and_callback(wp, &rettv);
1997 return 1;
1998 }
1999
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002000 argv[0].v_type = VAR_NUMBER;
2001 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2002
2003 // Convert the number to a string, so that the function can use:
2004 // if a:c == "\<F2>"
2005 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2006 argv[1].v_type = VAR_STRING;
2007 argv[1].vval.v_string = vim_strsave(buf);
2008
2009 argv[2].v_type = VAR_UNKNOWN;
2010
Bram Moolenaara42d9452019-06-15 21:46:30 +02002011 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002012 call_callback(&wp->w_filter_cb, -1,
2013 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2014 res = tv_get_number(&rettv);
2015 vim_free(argv[1].vval.v_string);
2016 clear_tv(&rettv);
2017 return res;
2018}
2019
2020/*
2021 * Called when "c" was typed: invoke popup filter callbacks.
2022 * Returns TRUE when the character was consumed,
2023 */
2024 int
2025popup_do_filter(int c)
2026{
2027 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002028 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002029
2030 popup_reset_handled();
2031
2032 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2033 if (wp->w_filter_cb.cb_name != NULL)
2034 res = invoke_popup_filter(wp, c);
2035
2036 return res;
2037}
2038
Bram Moolenaar3397f742019-06-02 18:40:06 +02002039/*
2040 * Called when the cursor moved: check if any popup needs to be closed if the
2041 * cursor moved far enough.
2042 */
2043 void
2044popup_check_cursor_pos()
2045{
2046 win_T *wp;
2047 typval_T tv;
2048
2049 popup_reset_handled();
2050 while ((wp = find_next_popup(TRUE)) != NULL)
2051 if (wp->w_popup_curwin != NULL
2052 && (curwin != wp->w_popup_curwin
2053 || curwin->w_cursor.lnum != wp->w_popup_lnum
2054 || curwin->w_cursor.col < wp->w_popup_mincol
2055 || curwin->w_cursor.col > wp->w_popup_maxcol))
2056 {
2057 tv.v_type = VAR_NUMBER;
2058 tv.vval.v_number = -1;
2059 popup_close_and_callback(wp, &tv);
2060 }
2061}
2062
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002063/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002064 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2065 * "col" and "line" are screen coordinates.
2066 */
2067 static int
2068popup_masked(win_T *wp, int screencol, int screenline)
2069{
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002070 int col = screencol - wp->w_wincol + 1 + wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002071 int line = screenline - wp->w_winrow + 1;
2072 listitem_T *lio, *li;
2073 int width, height;
2074
2075 if (wp->w_popup_mask == NULL)
2076 return FALSE;
2077 width = popup_width(wp);
2078 height = popup_height(wp);
2079
2080 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2081 {
2082 int cols, cole;
2083 int lines, linee;
2084
2085 li = lio->li_tv.vval.v_list->lv_first;
2086 cols = tv_get_number(&li->li_tv);
2087 if (cols < 0)
2088 cols = width + cols + 1;
2089 if (col < cols)
2090 continue;
2091 li = li->li_next;
2092 cole = tv_get_number(&li->li_tv);
2093 if (cole < 0)
2094 cole = width + cole + 1;
2095 if (col > cole)
2096 continue;
2097 li = li->li_next;
2098 lines = tv_get_number(&li->li_tv);
2099 if (lines < 0)
2100 lines = height + lines + 1;
2101 if (line < lines)
2102 continue;
2103 li = li->li_next;
2104 linee = tv_get_number(&li->li_tv);
2105 if (linee < 0)
2106 linee = height + linee + 1;
2107 if (line > linee)
2108 continue;
2109 return TRUE;
2110 }
2111 return FALSE;
2112}
2113
2114/*
2115 * Set flags in popup_transparent[] for window "wp" to "val".
2116 */
2117 static void
2118update_popup_transparent(win_T *wp, int val)
2119{
2120 if (wp->w_popup_mask != NULL)
2121 {
2122 int width = popup_width(wp);
2123 int height = popup_height(wp);
2124 listitem_T *lio, *li;
2125 int cols, cole;
2126 int lines, linee;
2127 int col, line;
2128
2129 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2130 {
2131 li = lio->li_tv.vval.v_list->lv_first;
2132 cols = tv_get_number(&li->li_tv);
2133 if (cols < 0)
2134 cols = width + cols + 1;
2135 li = li->li_next;
2136 cole = tv_get_number(&li->li_tv);
2137 if (cole < 0)
2138 cole = width + cole + 1;
2139 li = li->li_next;
2140 lines = tv_get_number(&li->li_tv);
2141 if (lines < 0)
2142 lines = height + lines + 1;
2143 li = li->li_next;
2144 linee = tv_get_number(&li->li_tv);
2145 if (linee < 0)
2146 linee = height + linee + 1;
2147
2148 --cols;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002149 cols -= wp->w_leftcol;
2150 if (cols < 0)
2151 cols = 0;
2152 cole -= wp->w_leftcol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002153 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002154 if (lines < 0)
2155 lines = 0;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002156 for (line = lines; line < linee && line < screen_Rows; ++line)
2157 for (col = cols; col < cole && col < screen_Columns; ++col)
2158 popup_transparent[(line + wp->w_winrow) * screen_Columns
2159 + col + wp->w_wincol] = val;
2160 }
2161 }
2162}
2163
2164/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002165 * Update "popup_mask" if needed.
2166 * Also recomputes the popup size and positions.
2167 * Also updates "popup_visible".
2168 * Also marks window lines for redrawing.
2169 */
2170 void
2171may_update_popup_mask(int type)
2172{
2173 win_T *wp;
2174 short *mask;
2175 int line, col;
2176 int redraw_all = FALSE;
2177
2178 // Need to recompute when switching tabs.
2179 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2180 // (such as the screen size) must have changed.
2181 if (popup_mask_tab != curtab || type >= NOT_VALID)
2182 {
2183 popup_mask_refresh = TRUE;
2184 redraw_all = TRUE;
2185 }
2186 if (!popup_mask_refresh)
2187 {
2188 // Check if any buffer has changed.
2189 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2190 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2191 popup_mask_refresh = TRUE;
2192 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2193 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2194 popup_mask_refresh = TRUE;
2195 if (!popup_mask_refresh)
2196 return;
2197 }
2198
2199 // Need to update the mask, something has changed.
2200 popup_mask_refresh = FALSE;
2201 popup_mask_tab = curtab;
2202 popup_visible = FALSE;
2203
2204 // If redrawing everything, just update "popup_mask".
2205 // If redrawing only what is needed, update "popup_mask_next" and then
2206 // compare with "popup_mask" to see what changed.
2207 if (type >= SOME_VALID)
2208 mask = popup_mask;
2209 else
2210 mask = popup_mask_next;
2211 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2212
2213 // Find the window with the lowest zindex that hasn't been handled yet,
2214 // so that the window with a higher zindex overwrites the value in
2215 // popup_mask.
2216 popup_reset_handled();
2217 while ((wp = find_next_popup(TRUE)) != NULL)
2218 {
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002219 int height = popup_height(wp);
2220 int width = popup_width(wp);
2221
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002222 popup_visible = TRUE;
2223
2224 // Recompute the position if the text changed.
2225 if (redraw_all
2226 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2227 popup_adjust_position(wp);
2228
2229 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002230 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002231 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002232 col < wp->w_wincol + width && col < screen_Columns; ++col)
2233 if (!popup_masked(wp, col, line))
2234 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002235 }
2236
2237 // Only check which lines are to be updated if not already
2238 // updating all lines.
2239 if (mask == popup_mask_next)
2240 for (line = 0; line < screen_Rows; ++line)
2241 {
2242 int col_done = 0;
2243
2244 for (col = 0; col < screen_Columns; ++col)
2245 {
2246 int off = line * screen_Columns + col;
2247
2248 if (popup_mask[off] != popup_mask_next[off])
2249 {
2250 popup_mask[off] = popup_mask_next[off];
2251
2252 if (line >= cmdline_row)
2253 {
2254 // the command line needs to be cleared if text below
2255 // the popup is now visible.
2256 if (!msg_scrolled && popup_mask_next[off] == 0)
2257 clear_cmdline = TRUE;
2258 }
2259 else if (col >= col_done)
2260 {
2261 linenr_T lnum;
2262 int line_cp = line;
2263 int col_cp = col;
2264
2265 // The screen position "line" / "col" needs to be
2266 // redrawn. Figure out what window that is and update
2267 // w_redraw_top and w_redr_bot. Only needs to be done
2268 // once for each window line.
2269 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2270 if (wp != NULL)
2271 {
2272 if (line_cp >= wp->w_height)
2273 // In (or below) status line
2274 wp->w_redr_status = TRUE;
2275 // compute the position in the buffer line from the
2276 // position on the screen
2277 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2278 &lnum))
2279 // past bottom
2280 wp->w_redr_status = TRUE;
2281 else
2282 redrawWinline(wp, lnum);
2283
2284 // This line is going to be redrawn, no need to
2285 // check until the right side of the window.
2286 col_done = wp->w_wincol + wp->w_width - 1;
2287 }
2288 }
2289 }
2290 }
2291 }
2292}
2293
2294/*
2295 * Return a string of "len" spaces in IObuff.
2296 */
2297 static char_u *
2298get_spaces(int len)
2299{
2300 vim_memset(IObuff, ' ', (size_t)len);
2301 IObuff[len] = NUL;
2302 return IObuff;
2303}
2304
2305/*
2306 * Update popup windows. They are drawn on top of normal windows.
2307 * "win_update" is called for each popup window, lowest zindex first.
2308 */
2309 void
2310update_popups(void (*win_update)(win_T *wp))
2311{
2312 win_T *wp;
2313 int top_off;
2314 int left_off;
2315 int total_width;
2316 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002317 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002318 int popup_attr;
2319 int border_attr[4];
2320 int border_char[8];
2321 char_u buf[MB_MAXBYTES];
2322 int row;
2323 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002324 int sb_thumb_top = 0;
2325 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002326 int attr_scroll = 0;
2327 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002328
2329 // Find the window with the lowest zindex that hasn't been updated yet,
2330 // so that the window with a higher zindex is drawn later, thus goes on
2331 // top.
2332 popup_reset_handled();
2333 while ((wp = find_next_popup(TRUE)) != NULL)
2334 {
2335 // This drawing uses the zindex of the popup window, so that it's on
2336 // top of the text but doesn't draw when another popup with higher
2337 // zindex is on top of the character.
2338 screen_zindex = wp->w_zindex;
2339
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002340 // Set flags in popup_transparent[] for masked cells.
2341 update_popup_transparent(wp, 1);
2342
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002343 // adjust w_winrow and w_wincol for border and padding, since
2344 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002345 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002346 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
2347 wp->w_winrow += top_off;
2348 wp->w_wincol += left_off;
2349
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002350 // Draw the popup text, unless it's off screen.
2351 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2352 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002353
2354 wp->w_winrow -= top_off;
2355 wp->w_wincol -= left_off;
2356
2357 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002358 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1]
2359 + wp->w_has_scrollbar;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002360 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002361 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
2362 popup_attr = get_wcr_attr(wp);
2363
2364 // We can only use these line drawing characters when 'encoding' is
2365 // "utf-8" and 'ambiwidth' is "single".
2366 if (enc_utf8 && *p_ambw == 's')
2367 {
2368 border_char[0] = border_char[2] = 0x2550;
2369 border_char[1] = border_char[3] = 0x2551;
2370 border_char[4] = 0x2554;
2371 border_char[5] = 0x2557;
2372 border_char[6] = 0x255d;
2373 border_char[7] = 0x255a;
2374 }
2375 else
2376 {
2377 border_char[0] = border_char[2] = '-';
2378 border_char[1] = border_char[3] = '|';
2379 for (i = 4; i < 8; ++i)
2380 border_char[i] = '+';
2381 }
2382 for (i = 0; i < 8; ++i)
2383 if (wp->w_border_char[i] != 0)
2384 border_char[i] = wp->w_border_char[i];
2385
2386 for (i = 0; i < 4; ++i)
2387 {
2388 border_attr[i] = popup_attr;
2389 if (wp->w_border_highlight[i] != NULL)
2390 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2391 }
2392
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002393 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002394 if (wp->w_popup_border[0] > 0)
2395 {
2396 // top border
2397 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2398 wp->w_wincol,
2399 wp->w_wincol + total_width,
2400 wp->w_popup_border[3] != 0
2401 ? border_char[4] : border_char[0],
2402 border_char[0], border_attr[0]);
2403 if (wp->w_popup_border[1] > 0)
2404 {
2405 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2406 screen_puts(buf, wp->w_winrow,
2407 wp->w_wincol + total_width - 1, border_attr[1]);
2408 }
2409 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002410 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2411 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002412
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002413 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002414 {
2415 // top padding
2416 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002417 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002418 wp->w_wincol + wp->w_popup_border[3],
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002419 wp->w_wincol + total_width - wp->w_popup_border[1]
2420 - wp->w_has_scrollbar,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002421 ' ', ' ', popup_attr);
2422 }
2423
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002424 // Title goes on top of border or padding.
2425 if (wp->w_popup_title != NULL)
2426 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2427 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2428
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002429 // Compute scrollbar thumb position and size.
2430 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002431 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002432 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2433
Bram Moolenaar68acb412019-06-26 03:40:36 +02002434 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2435 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002436 if (sb_thumb_height == 0)
2437 sb_thumb_height = 1;
Bram Moolenaar68acb412019-06-26 03:40:36 +02002438 sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2)
2439 * (wp->w_height - sb_thumb_height)
2440 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002441 if (wp->w_scrollbar_highlight != NULL)
2442 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2443 else
2444 attr_scroll = highlight_attr[HLF_PSB];
2445 if (wp->w_thumb_highlight != NULL)
2446 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2447 else
2448 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002449 }
2450
2451 for (i = wp->w_popup_border[0];
2452 i < total_height - wp->w_popup_border[2]; ++i)
2453 {
2454 row = wp->w_winrow + i;
2455
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002456 // left border
2457 if (wp->w_popup_border[3] > 0)
2458 {
2459 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2460 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2461 }
2462 // left padding
2463 if (wp->w_popup_padding[3] > 0)
2464 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2465 wp->w_wincol + wp->w_popup_border[3], popup_attr);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002466 // scrollbar
2467 if (wp->w_has_scrollbar)
2468 {
2469 int line = i - top_off;
2470 int scroll_col = wp->w_wincol + total_width - 1
2471 - wp->w_popup_border[1];
2472
2473 if (line >= 0 && line < wp->w_height)
2474 screen_putchar(' ', row, scroll_col,
2475 line >= sb_thumb_top
2476 && line < sb_thumb_top + sb_thumb_height
2477 ? attr_thumb : attr_scroll);
2478 else
2479 screen_putchar(' ', row, scroll_col, popup_attr);
2480 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002481 // right border
2482 if (wp->w_popup_border[1] > 0)
2483 {
2484 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2485 screen_puts(buf, row,
2486 wp->w_wincol + total_width - 1, border_attr[1]);
2487 }
2488 // right padding
2489 if (wp->w_popup_padding[1] > 0)
2490 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2491 wp->w_wincol + wp->w_popup_border[3]
2492 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2493 }
2494
2495 if (wp->w_popup_padding[2] > 0)
2496 {
2497 // bottom padding
2498 row = wp->w_winrow + wp->w_popup_border[0]
2499 + wp->w_popup_padding[0] + wp->w_height;
2500 screen_fill(row, row + wp->w_popup_padding[2],
2501 wp->w_wincol + wp->w_popup_border[3],
2502 wp->w_wincol + total_width - wp->w_popup_border[1],
2503 ' ', ' ', popup_attr);
2504 }
2505
2506 if (wp->w_popup_border[2] > 0)
2507 {
2508 // bottom border
2509 row = wp->w_winrow + total_height - 1;
2510 screen_fill(row , row + 1,
2511 wp->w_wincol,
2512 wp->w_wincol + total_width,
2513 wp->w_popup_border[3] != 0
2514 ? border_char[7] : border_char[2],
2515 border_char[2], border_attr[2]);
2516 if (wp->w_popup_border[1] > 0)
2517 {
2518 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2519 screen_puts(buf, row,
2520 wp->w_wincol + total_width - 1, border_attr[2]);
2521 }
2522 }
2523
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002524 if (wp->w_popup_close == POPCLOSE_BUTTON)
2525 {
2526 // close button goes on top of anything at the top-right corner
2527 buf[mb_char2bytes('X', buf)] = NUL;
2528 screen_puts(buf, wp->w_winrow, wp->w_wincol + total_width - 1,
2529 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2530 }
2531
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002532 update_popup_transparent(wp, 0);
2533
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002534 // Back to the normal zindex.
2535 screen_zindex = 0;
2536 }
2537}
2538
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002539/*
2540 * Mark references in callbacks of one popup window.
2541 */
2542 static int
2543set_ref_in_one_popup(win_T *wp, int copyID)
2544{
2545 int abort = FALSE;
2546 typval_T tv;
2547
2548 if (wp->w_close_cb.cb_partial != NULL)
2549 {
2550 tv.v_type = VAR_PARTIAL;
2551 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2552 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2553 }
2554 if (wp->w_filter_cb.cb_partial != NULL)
2555 {
2556 tv.v_type = VAR_PARTIAL;
2557 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2558 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2559 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002560 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002561 return abort;
2562}
2563
2564/*
2565 * Set reference in callbacks of popup windows.
2566 */
2567 int
2568set_ref_in_popups(int copyID)
2569{
2570 int abort = FALSE;
2571 win_T *wp;
2572 tabpage_T *tp;
2573
2574 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2575 abort = abort || set_ref_in_one_popup(wp, copyID);
2576
2577 FOR_ALL_TABPAGES(tp)
2578 {
2579 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2580 abort = abort || set_ref_in_one_popup(wp, copyID);
2581 if (abort)
2582 break;
2583 }
2584 return abort;
2585}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002586#endif // FEAT_TEXT_PROP