blob: f9775172620d01afa903a8279672540185601892 [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/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200171 * Used when popup options contain "mousemoved": set default moved values.
172 */
173 static void
174set_mousemoved_values(win_T *wp)
175{
176 wp->w_popup_mouse_row = mouse_row;
177 wp->w_popup_mouse_mincol = mouse_col;
178 wp->w_popup_mouse_maxcol = mouse_col;
179}
180
181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
223 * Return TRUE if "row"/"col" is on the "X" button of the popup.
224 * The values are relative to the top-left corner.
225 * Caller should check w_popup_close is POPCLOSE_BUTTON.
226 */
227 int
228popup_on_X_button(win_T *wp, int row, int col)
229{
230 return row == 0 && col == popup_width(wp) - 1;
231}
232
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200233// Values set when dragging a popup window starts.
234static int drag_start_row;
235static int drag_start_col;
236static int drag_start_wantline;
237static int drag_start_wantcol;
238
239/*
240 * Mouse down on border of popup window: start dragging it.
241 * Uses mouse_col and mouse_row.
242 */
243 void
244popup_start_drag(win_T *wp)
245{
246 drag_start_row = mouse_row;
247 drag_start_col = mouse_col;
248 // TODO: handle using different corner
249 if (wp->w_wantline == 0)
250 drag_start_wantline = wp->w_winrow + 1;
251 else
252 drag_start_wantline = wp->w_wantline;
253 if (wp->w_wantcol == 0)
254 drag_start_wantcol = wp->w_wincol + 1;
255 else
256 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200257
258 // Stop centering the popup
259 if (wp->w_popup_pos == POPPOS_CENTER)
260 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261}
262
263/*
264 * Mouse moved while dragging a popup window: adjust the window popup position.
265 */
266 void
267popup_drag(win_T *wp)
268{
269 // The popup may be closed before dragging stops.
270 if (!win_valid_popup(wp))
271 return;
272
273 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
274 if (wp->w_wantline < 1)
275 wp->w_wantline = 1;
276 if (wp->w_wantline > Rows)
277 wp->w_wantline = Rows;
278 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
279 if (wp->w_wantcol < 1)
280 wp->w_wantcol = 1;
281 if (wp->w_wantcol > Columns)
282 wp->w_wantcol = Columns;
283
284 popup_adjust_position(wp);
285}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200286
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200287/*
288 * Set w_firstline to match the current "wp->w_topline".
289 */
290 void
291popup_set_firstline(win_T *wp)
292{
293 int height = wp->w_height;
294
295 wp->w_firstline = wp->w_topline;
296 popup_adjust_position(wp);
297
298 // we don't want the popup to get smaller, decrement the first line
299 // until it doesn't
300 while (wp->w_firstline > 1 && wp->w_height < height)
301 {
302 --wp->w_firstline;
303 popup_adjust_position(wp);
304 }
305}
306
307/*
308 * Handle a click in a popup window, if it is in the scrollbar.
309 */
310 void
311popup_handle_scrollbar_click(win_T *wp, int row, int col)
312{
313 int height = popup_height(wp);
314 int old_topline = wp->w_topline;
315
316 if (wp->w_has_scrollbar == 0)
317 return;
318 if (row >= wp->w_popup_border[0]
319 && row < height - wp->w_popup_border[2]
Bram Moolenaarbd42b312019-07-12 16:35:34 +0200320 && col == popup_width(wp) - wp->w_popup_border[1] - 1)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200321 {
322 if (row >= height / 2)
323 {
324 // Click in lower half, scroll down.
325 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
326 ++wp->w_topline;
327 }
328 else if (wp->w_topline > 1)
329 // click on upper half, scroll up.
330 --wp->w_topline;
331 if (wp->w_topline != old_topline)
332 {
333 popup_set_firstline(wp);
334 redraw_win_later(wp, NOT_VALID);
335 }
336 }
337}
338
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200339#if defined(FEAT_TIMERS)
340 static void
341popup_add_timeout(win_T *wp, int time)
342{
343 char_u cbbuf[50];
344 char_u *ptr = cbbuf;
345 typval_T tv;
346
347 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
348 "{_ -> popup_close(%d)}", wp->w_id);
349 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
350 {
351 wp->w_popup_timer = create_timer(time, 0);
352 wp->w_popup_timer->tr_callback = get_callback(&tv);
353 clear_tv(&tv);
354 }
355}
356#endif
357
Bram Moolenaar17627312019-06-02 19:53:44 +0200358/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200359 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200360 */
361 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200362apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200363{
Bram Moolenaarae943152019-06-16 22:54:14 +0200364 int nr;
365
366 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
367 wp->w_minwidth = nr;
368 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
369 wp->w_minheight = nr;
370 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
371 wp->w_maxwidth = nr;
372 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
373 wp->w_maxheight = nr;
374 get_pos_options(wp, d);
375}
376
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200377 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200378handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
379{
380 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
381 {
382 char_u *s = di->di_tv.vval.v_string;
383 int flags = 0;
384
385 if (STRCMP(s, "word") == 0)
386 flags = FIND_IDENT | FIND_STRING;
387 else if (STRCMP(s, "WORD") == 0)
388 flags = FIND_STRING;
389 else if (STRCMP(s, "expr") == 0)
390 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
391 else if (STRCMP(s, "any") != 0)
392 semsg(_(e_invarg2), s);
393 if (flags != 0)
394 {
395 if (mousemoved)
396 set_mousemoved_columns(wp, flags);
397 else
398 set_moved_columns(wp, flags);
399 }
400 }
401 else if (di->di_tv.v_type == VAR_LIST
402 && di->di_tv.vval.v_list != NULL
403 && di->di_tv.vval.v_list->lv_len == 2)
404 {
405 list_T *l = di->di_tv.vval.v_list;
406 int mincol = tv_get_number(&l->lv_first->li_tv);
407 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
408
409 if (mousemoved)
410 {
411 wp->w_popup_mouse_mincol = mincol;
412 wp->w_popup_mouse_maxcol = maxcol;
413 }
414 else
415 {
416 wp->w_popup_mincol = mincol;
417 wp->w_popup_maxcol = maxcol;
418 }
419 }
420 else
421 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
422}
423
424 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200425check_highlight(dict_T *dict, char *name, char_u **pval)
426{
427 dictitem_T *di;
428 char_u *str;
429
430 di = dict_find(dict, (char_u *)name, -1);
431 if (di != NULL)
432 {
433 if (di->di_tv.v_type != VAR_STRING)
434 semsg(_(e_invargval), name);
435 else
436 {
437 str = tv_get_string(&di->di_tv);
438 if (*str != NUL)
439 *pval = vim_strsave(str);
440 }
441 }
442}
443
Bram Moolenaarae943152019-06-16 22:54:14 +0200444/*
445 * Shared between popup_create() and f_popup_setoptions().
446 */
447 static void
448apply_general_options(win_T *wp, dict_T *dict)
449{
450 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200451 int nr;
452 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200453
Bram Moolenaarae943152019-06-16 22:54:14 +0200454 // TODO: flip
455
456 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200457 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
459 if (wp->w_firstline < 1)
460 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200461
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200462 di = dict_find(dict, (char_u *)"scrollbar", -1);
463 if (di != NULL)
464 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
465
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200466 str = dict_get_string(dict, (char_u *)"title", FALSE);
467 if (str != NULL)
468 {
469 vim_free(wp->w_popup_title);
470 wp->w_popup_title = vim_strsave(str);
471 }
472
Bram Moolenaar402502d2019-05-30 22:07:36 +0200473 di = dict_find(dict, (char_u *)"wrap", -1);
474 if (di != NULL)
475 {
476 nr = dict_get_number(dict, (char_u *)"wrap");
477 wp->w_p_wrap = nr != 0;
478 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200479
Bram Moolenaara42d9452019-06-15 21:46:30 +0200480 di = dict_find(dict, (char_u *)"drag", -1);
481 if (di != NULL)
482 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200483
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200484 di = dict_find(dict, (char_u *)"close", -1);
485 if (di != NULL)
486 {
487 int ok = TRUE;
488
489 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
490 {
491 char_u *s = di->di_tv.vval.v_string;
492
493 if (STRCMP(s, "none") == 0)
494 wp->w_popup_close = POPCLOSE_NONE;
495 else if (STRCMP(s, "button") == 0)
496 wp->w_popup_close = POPCLOSE_BUTTON;
497 else if (STRCMP(s, "click") == 0)
498 wp->w_popup_close = POPCLOSE_CLICK;
499 else
500 ok = FALSE;
501 }
502 else
503 ok = FALSE;
504 if (!ok)
505 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
506 }
507
Bram Moolenaarae943152019-06-16 22:54:14 +0200508 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
509 if (str != NULL)
510 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
511 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200512
Bram Moolenaarae943152019-06-16 22:54:14 +0200513 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
514 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200515
Bram Moolenaar790498b2019-06-01 22:15:29 +0200516 di = dict_find(dict, (char_u *)"borderhighlight", -1);
517 if (di != NULL)
518 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200519 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200520 emsg(_(e_listreq));
521 else
522 {
523 list_T *list = di->di_tv.vval.v_list;
524 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200525 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200526
Bram Moolenaar403d0902019-07-17 21:37:32 +0200527 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
528 ++i, li = li->li_next)
529 {
530 str = tv_get_string(&li->li_tv);
531 if (*str != NUL)
532 wp->w_border_highlight[i] = vim_strsave(str);
533 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200534 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
535 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200536 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200537 vim_strsave(wp->w_border_highlight[0]);
538 }
539 }
540
Bram Moolenaar790498b2019-06-01 22:15:29 +0200541 di = dict_find(dict, (char_u *)"borderchars", -1);
542 if (di != NULL)
543 {
544 if (di->di_tv.v_type != VAR_LIST)
545 emsg(_(e_listreq));
546 else
547 {
548 list_T *list = di->di_tv.vval.v_list;
549 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200550 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200551
552 if (list != NULL)
553 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
554 ++i, li = li->li_next)
555 {
556 str = tv_get_string(&li->li_tv);
557 if (*str != NUL)
558 wp->w_border_char[i] = mb_ptr2char(str);
559 }
560 if (list->lv_len == 1)
561 for (i = 1; i < 8; ++i)
562 wp->w_border_char[i] = wp->w_border_char[0];
563 if (list->lv_len == 2)
564 {
565 for (i = 4; i < 8; ++i)
566 wp->w_border_char[i] = wp->w_border_char[1];
567 for (i = 1; i < 4; ++i)
568 wp->w_border_char[i] = wp->w_border_char[0];
569 }
570 }
571 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200572
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200573 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
574 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
575
Bram Moolenaarae943152019-06-16 22:54:14 +0200576 di = dict_find(dict, (char_u *)"zindex", -1);
577 if (di != NULL)
578 {
579 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
580 if (wp->w_zindex < 1)
581 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
582 if (wp->w_zindex > 32000)
583 wp->w_zindex = 32000;
584 }
585
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200586 di = dict_find(dict, (char_u *)"mask", -1);
587 if (di != NULL)
588 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200589 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200590
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200591 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200592 {
593 listitem_T *li;
594
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200595 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200596 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
597 li = li->li_next)
598 {
599 if (li->li_tv.v_type != VAR_LIST
600 || li->li_tv.vval.v_list == NULL
601 || li->li_tv.vval.v_list->lv_len != 4)
602 {
603 ok = FALSE;
604 break;
605 }
606 }
607 }
608 if (ok)
609 {
610 wp->w_popup_mask = di->di_tv.vval.v_list;
611 ++wp->w_popup_mask->lv_refcount;
612 }
613 else
614 semsg(_(e_invargval), "mask");
615 }
616
Bram Moolenaarae943152019-06-16 22:54:14 +0200617#if defined(FEAT_TIMERS)
618 // Add timer to close the popup after some time.
619 nr = dict_get_number(dict, (char_u *)"time");
620 if (nr > 0)
621 popup_add_timeout(wp, nr);
622#endif
623
Bram Moolenaar3397f742019-06-02 18:40:06 +0200624 di = dict_find(dict, (char_u *)"moved", -1);
625 if (di != NULL)
626 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200627 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200628 handle_moved_argument(wp, di, FALSE);
629 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200630
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200631 di = dict_find(dict, (char_u *)"mousemoved", -1);
632 if (di != NULL)
633 {
634 set_mousemoved_values(wp);
635 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200636 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200637
Bram Moolenaarae943152019-06-16 22:54:14 +0200638 di = dict_find(dict, (char_u *)"filter", -1);
639 if (di != NULL)
640 {
641 callback_T callback = get_callback(&di->di_tv);
642
643 if (callback.cb_name != NULL)
644 {
645 free_callback(&wp->w_filter_cb);
646 set_callback(&wp->w_filter_cb, &callback);
647 }
648 }
649
650 di = dict_find(dict, (char_u *)"callback", -1);
651 if (di != NULL)
652 {
653 callback_T callback = get_callback(&di->di_tv);
654
655 if (callback.cb_name != NULL)
656 {
657 free_callback(&wp->w_close_cb);
658 set_callback(&wp->w_close_cb, &callback);
659 }
660 }
661}
662
663/*
664 * Go through the options in "dict" and apply them to popup window "wp".
665 */
666 static void
667apply_options(win_T *wp, dict_T *dict)
668{
669 int nr;
670
671 apply_move_options(wp, dict);
672 apply_general_options(wp, dict);
673
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200674 nr = dict_get_number(dict, (char_u *)"hidden");
675 if (nr > 0)
676 {
677 wp->w_popup_flags |= POPF_HIDDEN;
678 --wp->w_buffer->b_nwindows;
679 }
680
Bram Moolenaar33796b32019-06-08 16:01:13 +0200681 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200682}
683
684/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200685 * Add lines to the popup from a list of strings.
686 */
687 static void
688add_popup_strings(buf_T *buf, list_T *l)
689{
690 listitem_T *li;
691 linenr_T lnum = 0;
692 char_u *p;
693
694 for (li = l->lv_first; li != NULL; li = li->li_next)
695 if (li->li_tv.v_type == VAR_STRING)
696 {
697 p = li->li_tv.vval.v_string;
698 ml_append_buf(buf, lnum++,
699 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
700 }
701}
702
703/*
704 * Add lines to the popup from a list of dictionaries.
705 */
706 static void
707add_popup_dicts(buf_T *buf, list_T *l)
708{
709 listitem_T *li;
710 listitem_T *pli;
711 linenr_T lnum = 0;
712 char_u *p;
713 dict_T *dict;
714
715 // first add the text lines
716 for (li = l->lv_first; li != NULL; li = li->li_next)
717 {
718 if (li->li_tv.v_type != VAR_DICT)
719 {
720 emsg(_(e_dictreq));
721 return;
722 }
723 dict = li->li_tv.vval.v_dict;
724 p = dict == NULL ? NULL
725 : dict_get_string(dict, (char_u *)"text", FALSE);
726 ml_append_buf(buf, lnum++,
727 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
728 }
729
730 // add the text properties
731 lnum = 1;
732 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
733 {
734 dictitem_T *di;
735 list_T *plist;
736
737 dict = li->li_tv.vval.v_dict;
738 di = dict_find(dict, (char_u *)"props", -1);
739 if (di != NULL)
740 {
741 if (di->di_tv.v_type != VAR_LIST)
742 {
743 emsg(_(e_listreq));
744 return;
745 }
746 plist = di->di_tv.vval.v_list;
747 if (plist != NULL)
748 {
749 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
750 {
751 if (pli->li_tv.v_type != VAR_DICT)
752 {
753 emsg(_(e_dictreq));
754 return;
755 }
756 dict = pli->li_tv.vval.v_dict;
757 if (dict != NULL)
758 {
759 int col = dict_get_number(dict, (char_u *)"col");
760
761 prop_add_common( lnum, col, dict, buf, NULL);
762 }
763 }
764 }
765 }
766 }
767}
768
769/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200770 * Get the padding plus border at the top, adjusted to 1 if there is a title.
771 */
772 static int
773popup_top_extra(win_T *wp)
774{
775 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
776
777 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
778 return 1;
779 return extra;
780}
781
782/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200783 * Return the height of popup window "wp", including border and padding.
784 */
785 int
786popup_height(win_T *wp)
787{
788 return wp->w_height
789 + popup_top_extra(wp)
790 + wp->w_popup_padding[2] + wp->w_popup_border[2];
791}
792
793/*
794 * Return the width of popup window "wp", including border, padding and
795 * scrollbar.
796 */
797 int
798popup_width(win_T *wp)
799{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200800 // w_leftcol is how many columns of the core are left of the screen
801 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200802 return wp->w_width + wp->w_leftcol
803 + wp->w_popup_padding[3] + wp->w_popup_border[3]
804 + wp->w_popup_padding[1] + wp->w_popup_border[1]
805 + wp->w_has_scrollbar
806 + wp->w_popup_rightoff;
807}
808
809/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200810 * Adjust the position and size of the popup to fit on the screen.
811 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200812 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200813popup_adjust_position(win_T *wp)
814{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200815 linenr_T lnum;
816 int wrapped = 0;
817 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200818 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200819 int center_vert = FALSE;
820 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200821 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200822 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200823 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
824 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
825 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
826 int extra_height = top_extra + bot_extra;
827 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200828 int org_winrow = wp->w_winrow;
829 int org_wincol = wp->w_wincol;
830 int org_width = wp->w_width;
831 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200832 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200833 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200834 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200835
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200836 wp->w_winrow = 0;
837 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200838 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200839 wp->w_popup_leftoff = 0;
840 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200841 if (wp->w_popup_pos == POPPOS_CENTER)
842 {
843 // center after computing the size
844 center_vert = TRUE;
845 center_hor = TRUE;
846 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200847 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200848 {
849 if (wp->w_wantline == 0)
850 center_vert = TRUE;
851 else if (wp->w_popup_pos == POPPOS_TOPLEFT
852 || wp->w_popup_pos == POPPOS_TOPRIGHT)
853 {
854 wp->w_winrow = wp->w_wantline - 1;
855 if (wp->w_winrow >= Rows)
856 wp->w_winrow = Rows - 1;
857 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200858
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200859 if (wp->w_wantcol == 0)
860 center_hor = TRUE;
861 else if (wp->w_popup_pos == POPPOS_TOPLEFT
862 || wp->w_popup_pos == POPPOS_BOTLEFT)
863 {
864 wp->w_wincol = wp->w_wantcol - 1;
865 if (wp->w_wincol >= Columns - 3)
866 wp->w_wincol = Columns - 3;
867 }
868 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200869
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200870 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200871 // When left aligned use the space available, but shift to the left when we
872 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200873 maxspace = Columns - wp->w_wincol - left_extra;
874 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200875 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200876 {
877 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200878 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200879 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200880
Bram Moolenaar8d241042019-06-12 23:40:01 +0200881 // start at the desired first line
882 wp->w_topline = wp->w_firstline;
883 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
884 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
885
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200886 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200887 // Use a minimum width of one, so that something shows when there is no
888 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200889 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200890 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200891 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200892 {
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200893 // count Tabs for what they are worth
894 int len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
895 (colnr_T)MAXCOL);
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200896
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200897 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200898 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200899 while (len > maxwidth)
900 {
901 ++wrapped;
902 len -= maxwidth;
903 wp->w_width = maxwidth;
904 }
905 }
906 else if (len > maxwidth
907 && allow_adjust_left
908 && (wp->w_popup_pos == POPPOS_TOPLEFT
909 || wp->w_popup_pos == POPPOS_BOTLEFT))
910 {
911 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200912 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200913
Bram Moolenaar51c31312019-06-15 22:27:23 +0200914 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200915 {
916 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200917
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200918 len -= truncate_shift;
919 shift_by -= truncate_shift;
920 }
921
922 wp->w_wincol -= shift_by;
923 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200924 wp->w_width = maxwidth;
925 }
926 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +0200927 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200928 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +0200929 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
930 wp->w_width = wp->w_maxwidth;
931 }
Bram Moolenaar8d241042019-06-12 23:40:01 +0200932 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200933 if (wp->w_maxheight > 0
934 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200935 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200936 }
937
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200938 wp->w_has_scrollbar = wp->w_want_scrollbar
939 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
940
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200941 minwidth = wp->w_minwidth;
942 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
943 {
944 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
945
946 if (minwidth < title_len)
947 minwidth = title_len;
948 }
949
950 if (minwidth > 0 && wp->w_width < minwidth)
951 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200952 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +0200953 {
954 if (wp->w_width > maxspace)
955 // some columns cut off on the right
956 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200957 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200958 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200959 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200960 {
961 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
962 if (wp->w_wincol < 0)
963 wp->w_wincol = 0;
964 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200965 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
966 || wp->w_popup_pos == POPPOS_TOPRIGHT)
967 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200968 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
969
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200970 // Right aligned: move to the right if needed.
971 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200972 if (leftoff >= 0)
973 wp->w_wincol = leftoff;
974 else if (wp->w_popup_fixed)
975 {
976 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +0200977 // columns when displaying the window, minus left border and
978 // padding.
979 if (-leftoff > left_extra)
980 wp->w_leftcol = -leftoff - left_extra;
981 wp->w_width -= wp->w_leftcol;
982 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200983 if (wp->w_width < 0)
984 wp->w_width = 0;
985 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200986 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200987
Bram Moolenaar8d241042019-06-12 23:40:01 +0200988 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
989 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200990 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
991 wp->w_height = wp->w_minheight;
992 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
993 wp->w_height = wp->w_maxheight;
994 if (wp->w_height > Rows - wp->w_winrow)
995 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200996
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200997 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200998 {
999 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1000 if (wp->w_winrow < 0)
1001 wp->w_winrow = 0;
1002 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001003 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1004 || wp->w_popup_pos == POPPOS_BOTLEFT)
1005 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001006 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001007 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001008 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001009 else
1010 // not enough space, make top aligned
1011 wp->w_winrow = wp->w_wantline + 1;
1012 }
1013
Bram Moolenaar17146962019-05-30 00:12:11 +02001014 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001015
1016 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001017 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001018 if (org_winrow != wp->w_winrow
1019 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001020 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001021 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001022 || org_width != wp->w_width
1023 || org_height != wp->w_height)
1024 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001025 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001026 popup_mask_refresh = TRUE;
1027 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001028}
1029
Bram Moolenaar17627312019-06-02 19:53:44 +02001030typedef enum
1031{
1032 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001033 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001034 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001035 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001036 TYPE_DIALOG,
1037 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +02001038} create_type_T;
1039
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001040/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001041 * Make "buf" empty and set the contents to "text".
1042 * Used by popup_create() and popup_settext().
1043 */
1044 static void
1045popup_set_buffer_text(buf_T *buf, typval_T text)
1046{
1047 int lnum;
1048
1049 // Clear the buffer, then replace the lines.
1050 curbuf = buf;
1051 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1052 ml_delete(lnum, FALSE);
1053 curbuf = curwin->w_buffer;
1054
1055 // Add text to the buffer.
1056 if (text.v_type == VAR_STRING)
1057 {
1058 // just a string
1059 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1060 }
1061 else
1062 {
1063 list_T *l = text.vval.v_list;
1064
1065 if (l->lv_len > 0)
1066 {
1067 if (l->lv_first->li_tv.v_type == VAR_STRING)
1068 // list of strings
1069 add_popup_strings(buf, l);
1070 else
1071 // list of dictionaries
1072 add_popup_dicts(buf, l);
1073 }
1074 }
1075
1076 // delete the line that was in the empty buffer
1077 curbuf = buf;
1078 ml_delete(buf->b_ml.ml_line_count, FALSE);
1079 curbuf = curwin->w_buffer;
1080}
1081
1082/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001083 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001084 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001085 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001086 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001087popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001088{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001089 win_T *wp;
1090 tabpage_T *tp = NULL;
1091 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001092 int new_buffer;
1093 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001094 dict_T *d;
1095 int nr;
1096 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001097
1098 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001099 if (argvars[0].v_type == VAR_NUMBER)
1100 {
1101 buf = buflist_findnr( argvars[0].vval.v_number);
1102 if (buf == NULL)
1103 {
1104 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1105 return NULL;
1106 }
1107 }
1108 else if (!(argvars[0].v_type == VAR_STRING
1109 && argvars[0].vval.v_string != NULL)
1110 && !(argvars[0].v_type == VAR_LIST
1111 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001112 {
1113 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001114 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001115 }
1116 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1117 {
1118 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001119 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001120 }
1121 d = argvars[1].vval.v_dict;
1122
Bram Moolenaara3fce622019-06-20 02:31:49 +02001123 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1124 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1125 else if (type == TYPE_NOTIFICATION)
1126 tabnr = -1; // notifications are global by default
1127 else
1128 tabnr = 0;
1129 if (tabnr > 0)
1130 {
1131 tp = find_tabpage(tabnr);
1132 if (tp == NULL)
1133 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001134 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001135 return NULL;
1136 }
1137 }
1138
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001139 // Create the window and buffer.
1140 wp = win_alloc_popup_win();
1141 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001142 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001143 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001144 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001145 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001146
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001147 if (buf != NULL)
1148 {
1149 // use existing buffer
1150 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001151 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001152 buffer_ensure_loaded(buf);
1153 }
1154 else
1155 {
1156 // create a new buffer associated with the popup
1157 new_buffer = TRUE;
1158 buf = buflist_new(NULL, NULL, (linenr_T)0,
1159 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1160 if (buf == NULL)
1161 return NULL;
1162 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001163
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001164 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001165
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001166 set_local_options_default(wp);
1167 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001168 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001169 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1170 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1171 buf->b_p_ul = -1; // no undo
1172 buf->b_p_swf = FALSE; // no swap file
1173 buf->b_p_bl = FALSE; // unlisted buffer
1174 buf->b_locked = TRUE;
1175 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001176
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001177 // Avoid that 'buftype' is reset when this buffer is entered.
1178 buf->b_p_initialized = TRUE;
1179 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001180
Bram Moolenaara3fce622019-06-20 02:31:49 +02001181 if (tp != NULL)
1182 {
1183 // popup on specified tab page
1184 wp->w_next = tp->tp_first_popupwin;
1185 tp->tp_first_popupwin = wp;
1186 }
1187 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001188 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001189 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001190 wp->w_next = curtab->tp_first_popupwin;
1191 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001192 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001193 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001194 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001195 win_T *prev = first_popupwin;
1196
1197 // Global popup: add at the end, so that it gets displayed on top of
1198 // older ones with the same zindex. Matters for notifications.
1199 if (first_popupwin == NULL)
1200 first_popupwin = wp;
1201 else
1202 {
1203 while (prev->w_next != NULL)
1204 prev = prev->w_next;
1205 prev->w_next = wp;
1206 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001207 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001208
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001209 if (new_buffer)
1210 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001211
Bram Moolenaar17627312019-06-02 19:53:44 +02001212 if (type == TYPE_ATCURSOR)
1213 {
1214 wp->w_popup_pos = POPPOS_BOTLEFT;
1215 setcursor_mayforce(TRUE);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001216 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
Bram Moolenaar17627312019-06-02 19:53:44 +02001217 if (wp->w_wantline == 0) // cursor in first line
1218 {
1219 wp->w_wantline = 2;
1220 wp->w_popup_pos = POPPOS_TOPLEFT;
1221 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001222 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar17627312019-06-02 19:53:44 +02001223 set_moved_values(wp);
1224 set_moved_columns(wp, FIND_STRING);
1225 }
1226
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001227 if (type == TYPE_BEVAL)
1228 {
1229 wp->w_popup_pos = POPPOS_BOTLEFT;
1230
1231 // by default use the mouse position
1232 wp->w_wantline = mouse_row;
1233 if (wp->w_wantline <= 0) // mouse on first line
1234 {
1235 wp->w_wantline = 2;
1236 wp->w_popup_pos = POPPOS_TOPLEFT;
1237 }
1238 wp->w_wantcol = mouse_col + 1;
1239 set_mousemoved_values(wp);
1240 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1241 }
1242
Bram Moolenaar33796b32019-06-08 16:01:13 +02001243 // set default values
1244 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001245 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001246
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001247 if (type == TYPE_NOTIFICATION)
1248 {
1249 win_T *twp, *nextwin;
1250 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001251
1252 // Try to not overlap with another global popup. Guess we need 3
1253 // more screen lines than buffer lines.
1254 wp->w_wantline = 1;
1255 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1256 {
1257 nextwin = twp->w_next;
1258 if (twp != wp
1259 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1260 && twp->w_winrow <= wp->w_wantline - 1 + height
1261 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1262 {
1263 // move to below this popup and restart the loop to check for
1264 // overlap with other popups
1265 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1266 nextwin = first_popupwin;
1267 }
1268 }
1269 if (wp->w_wantline + height > Rows)
1270 {
1271 // can't avoid overlap, put on top in the hope that message goes
1272 // away soon.
1273 wp->w_wantline = 1;
1274 }
1275
1276 wp->w_wantcol = 10;
1277 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001278 wp->w_minwidth = 20;
1279 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001280 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001281 for (i = 0; i < 4; ++i)
1282 wp->w_popup_border[i] = 1;
1283 wp->w_popup_padding[1] = 1;
1284 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001285
1286 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001287 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001288 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1289 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001290 }
1291
Bram Moolenaara730e552019-06-16 19:05:31 +02001292 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001293 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001294 wp->w_popup_pos = POPPOS_CENTER;
1295 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1296 wp->w_popup_drag = 1;
1297 for (i = 0; i < 4; ++i)
1298 {
1299 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001300 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001301 }
1302 }
1303
Bram Moolenaara730e552019-06-16 19:05:31 +02001304 if (type == TYPE_MENU)
1305 {
1306 typval_T tv;
1307 callback_T callback;
1308
1309 tv.v_type = VAR_STRING;
1310 tv.vval.v_string = (char_u *)"popup_filter_menu";
1311 callback = get_callback(&tv);
1312 if (callback.cb_name != NULL)
1313 set_callback(&wp->w_filter_cb, &callback);
1314
1315 wp->w_p_wrap = 0;
1316 }
1317
Bram Moolenaarae943152019-06-16 22:54:14 +02001318 for (i = 0; i < 4; ++i)
1319 VIM_CLEAR(wp->w_border_highlight[i]);
1320 for (i = 0; i < 8; ++i)
1321 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001322 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001323 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001324
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001325 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001326 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001327
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001328#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001329 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1330 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001331#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001332
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001333 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001334
1335 wp->w_vsep_width = 0;
1336
1337 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001338 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001339
1340 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001341}
1342
1343/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001344 * popup_clear()
1345 */
1346 void
1347f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1348{
1349 close_all_popups();
1350}
1351
1352/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001353 * popup_create({text}, {options})
1354 */
1355 void
1356f_popup_create(typval_T *argvars, typval_T *rettv)
1357{
Bram Moolenaar17627312019-06-02 19:53:44 +02001358 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001359}
1360
1361/*
1362 * popup_atcursor({text}, {options})
1363 */
1364 void
1365f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1366{
Bram Moolenaar17627312019-06-02 19:53:44 +02001367 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001368}
1369
1370/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001371 * popup_beval({text}, {options})
1372 */
1373 void
1374f_popup_beval(typval_T *argvars, typval_T *rettv)
1375{
1376 popup_create(argvars, rettv, TYPE_BEVAL);
1377}
1378
1379/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001380 * Invoke the close callback for window "wp" with value "result".
1381 * Careful: The callback may make "wp" invalid!
1382 */
1383 static void
1384invoke_popup_callback(win_T *wp, typval_T *result)
1385{
1386 typval_T rettv;
1387 int dummy;
1388 typval_T argv[3];
1389
1390 argv[0].v_type = VAR_NUMBER;
1391 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1392
1393 if (result != NULL && result->v_type != VAR_UNKNOWN)
1394 copy_tv(result, &argv[1]);
1395 else
1396 {
1397 argv[1].v_type = VAR_NUMBER;
1398 argv[1].vval.v_number = 0;
1399 }
1400
1401 argv[2].v_type = VAR_UNKNOWN;
1402
1403 call_callback(&wp->w_close_cb, -1,
1404 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1405 if (result != NULL)
1406 clear_tv(&argv[1]);
1407 clear_tv(&rettv);
1408}
1409
1410/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001411 * Close popup "wp" and invoke any close callback for it.
1412 */
1413 static void
1414popup_close_and_callback(win_T *wp, typval_T *arg)
1415{
1416 int id = wp->w_id;
1417
1418 if (wp->w_close_cb.cb_name != NULL)
1419 // Careful: This may make "wp" invalid.
1420 invoke_popup_callback(wp, arg);
1421
1422 popup_close(id);
1423}
1424
1425/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001426 * Close popup "wp" because of a mouse click.
1427 */
1428 void
1429popup_close_for_mouse_click(win_T *wp)
1430{
1431 typval_T res;
1432
1433 res.v_type = VAR_NUMBER;
1434 res.vval.v_number = -2;
1435 popup_close_and_callback(wp, &res);
1436}
1437
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001438 static void
1439check_mouse_moved(win_T *wp, win_T *mouse_wp)
1440{
1441 // Close the popup when all if these are true:
1442 // - the mouse is not on this popup
1443 // - "mousemoved" was used
1444 // - the mouse is no longer on the same screen row or the mouse column is
1445 // outside of the relevant text
1446 if (wp != mouse_wp
1447 && wp->w_popup_mouse_row != 0
1448 && (wp->w_popup_mouse_row != mouse_row
1449 || mouse_col < wp->w_popup_mouse_mincol
1450 || mouse_col > wp->w_popup_mouse_maxcol))
1451 {
1452 typval_T res;
1453
1454 res.v_type = VAR_NUMBER;
1455 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001456 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001457 popup_close_and_callback(wp, &res);
1458 }
1459}
1460
1461/*
1462 * Called when the mouse moved: may close a popup with "mousemoved".
1463 */
1464 void
1465popup_handle_mouse_moved(void)
1466{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001467 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001468 win_T *mouse_wp;
1469 int row = mouse_row;
1470 int col = mouse_col;
1471
1472 // find the window where the mouse is in
1473 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1474
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001475 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1476 {
1477 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001478 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001479 }
1480 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1481 {
1482 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001483 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001484 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001485}
1486
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001487/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001488 * In a filter: check if the typed key is a mouse event that is used for
1489 * dragging the popup.
1490 */
1491 static void
1492filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1493{
1494 int row = mouse_row;
1495 int col = mouse_col;
1496
1497 if (wp->w_popup_drag
1498 && is_mouse_key(c)
1499 && (wp == popup_dragwin
1500 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1501 // do not consume the key, allow for dragging the popup
1502 rettv->vval.v_number = 0;
1503}
1504
1505 static void
1506popup_highlight_curline(win_T *wp)
1507{
1508 int id;
1509 char buf[100];
1510
1511 match_delete(wp, 1, FALSE);
1512
Bram Moolenaara901a372019-07-13 16:38:50 +02001513 // Scroll to show the line with the cursor. This assumes lines don't wrap.
1514 while (wp->w_topline + wp->w_height - 1 < wp->w_cursor.lnum)
1515 wp->w_topline++;
1516 while (wp->w_cursor.lnum < wp->w_topline)
1517 wp->w_topline--;
1518
Bram Moolenaara730e552019-06-16 19:05:31 +02001519 id = syn_name2id((char_u *)"PopupSelected");
1520 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1521 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1522 (char_u *)buf, 10, 1, NULL, NULL);
1523}
1524
1525/*
1526 * popup_filter_menu({text}, {options})
1527 */
1528 void
1529f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1530{
1531 int id = tv_get_number(&argvars[0]);
1532 win_T *wp = win_id2wp(id);
1533 char_u *key = tv_get_string(&argvars[1]);
1534 typval_T res;
1535 int c;
1536 linenr_T old_lnum;
1537
1538 // If the popup has been closed do not consume the key.
1539 if (wp == NULL)
1540 return;
1541
1542 c = *key;
1543 if (c == K_SPECIAL && key[1] != NUL)
1544 c = TO_SPECIAL(key[1], key[2]);
1545
1546 // consume all keys until done
1547 rettv->vval.v_number = 1;
1548 res.v_type = VAR_NUMBER;
1549
1550 old_lnum = wp->w_cursor.lnum;
1551 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1552 --wp->w_cursor.lnum;
1553 if ((c == 'j' || c == 'J' || c == K_DOWN)
1554 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1555 ++wp->w_cursor.lnum;
1556 if (old_lnum != wp->w_cursor.lnum)
1557 {
1558 popup_highlight_curline(wp);
1559 return;
1560 }
1561
1562 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1563 {
1564 // Cancelled, invoke callback with -1
1565 res.vval.v_number = -1;
1566 popup_close_and_callback(wp, &res);
1567 return;
1568 }
1569 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1570 {
1571 // Invoke callback with current index.
1572 res.vval.v_number = wp->w_cursor.lnum;
1573 popup_close_and_callback(wp, &res);
1574 return;
1575 }
1576
1577 filter_handle_drag(wp, c, rettv);
1578}
1579
1580/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001581 * popup_filter_yesno({text}, {options})
1582 */
1583 void
1584f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1585{
1586 int id = tv_get_number(&argvars[0]);
1587 win_T *wp = win_id2wp(id);
1588 char_u *key = tv_get_string(&argvars[1]);
1589 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001590 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001591
1592 // If the popup has been closed don't consume the key.
1593 if (wp == NULL)
1594 return;
1595
Bram Moolenaara730e552019-06-16 19:05:31 +02001596 c = *key;
1597 if (c == K_SPECIAL && key[1] != NUL)
1598 c = TO_SPECIAL(key[1], key[2]);
1599
Bram Moolenaara42d9452019-06-15 21:46:30 +02001600 // consume all keys until done
1601 rettv->vval.v_number = 1;
1602
Bram Moolenaara730e552019-06-16 19:05:31 +02001603 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001604 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001605 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001606 res.vval.v_number = 0;
1607 else
1608 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001609 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001610 return;
1611 }
1612
1613 // Invoke callback
1614 res.v_type = VAR_NUMBER;
1615 popup_close_and_callback(wp, &res);
1616}
1617
1618/*
1619 * popup_dialog({text}, {options})
1620 */
1621 void
1622f_popup_dialog(typval_T *argvars, typval_T *rettv)
1623{
1624 popup_create(argvars, rettv, TYPE_DIALOG);
1625}
1626
1627/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001628 * popup_menu({text}, {options})
1629 */
1630 void
1631f_popup_menu(typval_T *argvars, typval_T *rettv)
1632{
1633 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1634
1635 if (wp != NULL)
1636 popup_highlight_curline(wp);
1637}
1638
1639/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001640 * popup_notification({text}, {options})
1641 */
1642 void
1643f_popup_notification(typval_T *argvars, typval_T *rettv)
1644{
1645 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1646}
1647
1648/*
1649 * Find the popup window with window-ID "id".
1650 * If the popup window does not exist NULL is returned.
1651 * If the window is not a popup window, and error message is given.
1652 */
1653 static win_T *
1654find_popup_win(int id)
1655{
1656 win_T *wp = win_id2wp(id);
1657
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001658 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001659 {
1660 semsg(_("E993: window %d is not a popup window"), id);
1661 return NULL;
1662 }
1663 return wp;
1664}
1665
1666/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001667 * popup_close({id})
1668 */
1669 void
1670f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1671{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001672 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001673 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001674
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001675 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001676 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001677}
1678
1679/*
1680 * popup_hide({id})
1681 */
1682 void
1683f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1684{
1685 int id = (int)tv_get_number(argvars);
1686 win_T *wp = find_popup_win(id);
1687
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001688 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001689 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001690 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001691 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001692 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001693 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001694 }
1695}
1696
1697/*
1698 * popup_show({id})
1699 */
1700 void
1701f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1702{
1703 int id = (int)tv_get_number(argvars);
1704 win_T *wp = find_popup_win(id);
1705
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001706 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001707 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001708 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001709 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001710 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001711 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001712 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001713}
1714
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001715/*
1716 * popup_settext({id}, {text})
1717 */
1718 void
1719f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1720{
1721 int id = (int)tv_get_number(&argvars[0]);
1722 win_T *wp = find_popup_win(id);
1723
1724 if (wp != NULL)
1725 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001726 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1727 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1728 else
1729 {
1730 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1731 popup_adjust_position(wp);
1732 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001733 }
1734}
1735
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001736 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001737popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001738{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001739 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001740 if (wp->w_winrow + wp->w_height >= cmdline_row)
1741 clear_cmdline = TRUE;
1742 win_free_popup(wp);
1743 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001744 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001745}
1746
Bram Moolenaarec583842019-05-26 14:11:23 +02001747/*
1748 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001749 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001750 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001751 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001752popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001753{
1754 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001755 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001756 win_T *prev = NULL;
1757
Bram Moolenaarec583842019-05-26 14:11:23 +02001758 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001759 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001760 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001761 {
1762 if (prev == NULL)
1763 first_popupwin = wp->w_next;
1764 else
1765 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001766 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001767 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001768 }
1769
Bram Moolenaarec583842019-05-26 14:11:23 +02001770 // go through tab-local popups
1771 FOR_ALL_TABPAGES(tp)
1772 popup_close_tabpage(tp, id);
1773}
1774
1775/*
1776 * Close a popup window with Window-id "id" in tabpage "tp".
1777 */
1778 void
1779popup_close_tabpage(tabpage_T *tp, int id)
1780{
1781 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001782 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001783 win_T *prev = NULL;
1784
Bram Moolenaarec583842019-05-26 14:11:23 +02001785 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1786 if (wp->w_id == id)
1787 {
1788 if (prev == NULL)
1789 *root = wp->w_next;
1790 else
1791 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001792 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001793 return;
1794 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001795}
1796
1797 void
1798close_all_popups(void)
1799{
1800 while (first_popupwin != NULL)
1801 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001802 while (curtab->tp_first_popupwin != NULL)
1803 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001804}
1805
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001806/*
1807 * popup_move({id}, {options})
1808 */
1809 void
1810f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1811{
Bram Moolenaarae943152019-06-16 22:54:14 +02001812 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001813 int id = (int)tv_get_number(argvars);
1814 win_T *wp = find_popup_win(id);
1815
1816 if (wp == NULL)
1817 return; // invalid {id}
1818
1819 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1820 {
1821 emsg(_(e_dictreq));
1822 return;
1823 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001824 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001825
Bram Moolenaarae943152019-06-16 22:54:14 +02001826 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001827
1828 if (wp->w_winrow + wp->w_height >= cmdline_row)
1829 clear_cmdline = TRUE;
1830 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001831}
1832
Bram Moolenaarbc133542019-05-29 20:26:46 +02001833/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001834 * popup_setoptions({id}, {options})
1835 */
1836 void
1837f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1838{
1839 dict_T *dict;
1840 int id = (int)tv_get_number(argvars);
1841 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001842 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001843
1844 if (wp == NULL)
1845 return; // invalid {id}
1846
1847 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1848 {
1849 emsg(_(e_dictreq));
1850 return;
1851 }
1852 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001853 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001854
1855 apply_move_options(wp, dict);
1856 apply_general_options(wp, dict);
1857
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001858 if (old_firstline != wp->w_firstline)
1859 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001860 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001861 popup_adjust_position(wp);
1862}
1863
1864/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001865 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001866 */
1867 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001868f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001869{
1870 dict_T *dict;
1871 int id = (int)tv_get_number(argvars);
1872 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001873 int top_extra;
1874 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001875
1876 if (rettv_dict_alloc(rettv) == OK)
1877 {
1878 if (wp == NULL)
1879 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001880 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001881 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1882
Bram Moolenaarbc133542019-05-29 20:26:46 +02001883 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001884
Bram Moolenaarbc133542019-05-29 20:26:46 +02001885 dict_add_number(dict, "line", wp->w_winrow + 1);
1886 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001887 dict_add_number(dict, "width", wp->w_width + left_extra
1888 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1889 dict_add_number(dict, "height", wp->w_height + top_extra
1890 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001891
1892 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1893 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1894 dict_add_number(dict, "core_width", wp->w_width);
1895 dict_add_number(dict, "core_height", wp->w_height);
1896
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001897 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001898 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001899 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001900 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001901 }
1902}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001903/*
1904 * popup_locate({row}, {col})
1905 */
1906 void
1907f_popup_locate(typval_T *argvars, typval_T *rettv)
1908{
1909 int row = tv_get_number(&argvars[0]) - 1;
1910 int col = tv_get_number(&argvars[1]) - 1;
1911 win_T *wp;
1912
1913 wp = mouse_find_win(&row, &col, FIND_POPUP);
1914 if (WIN_IS_POPUP(wp))
1915 rettv->vval.v_number = wp->w_id;
1916}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001917
1918/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001919 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1920 */
1921 static void
1922get_padding_border(dict_T *dict, int *array, char *name)
1923{
1924 list_T *list;
1925 int i;
1926
1927 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1928 return;
1929
1930 list = list_alloc();
1931 if (list != NULL)
1932 {
1933 dict_add_list(dict, name, list);
1934 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1935 for (i = 0; i < 4; ++i)
1936 list_append_number(list, array[i]);
1937 }
1938}
1939
1940/*
1941 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1942 */
1943 static void
1944get_borderhighlight(dict_T *dict, win_T *wp)
1945{
1946 list_T *list;
1947 int i;
1948
1949 for (i = 0; i < 4; ++i)
1950 if (wp->w_border_highlight[i] != NULL)
1951 break;
1952 if (i == 4)
1953 return;
1954
1955 list = list_alloc();
1956 if (list != NULL)
1957 {
1958 dict_add_list(dict, "borderhighlight", list);
1959 for (i = 0; i < 4; ++i)
1960 list_append_string(list, wp->w_border_highlight[i], -1);
1961 }
1962}
1963
1964/*
1965 * For popup_getoptions(): add a "borderchars" entry to "dict".
1966 */
1967 static void
1968get_borderchars(dict_T *dict, win_T *wp)
1969{
1970 list_T *list;
1971 int i;
1972 char_u buf[NUMBUFLEN];
1973 int len;
1974
1975 for (i = 0; i < 8; ++i)
1976 if (wp->w_border_char[i] != 0)
1977 break;
1978 if (i == 8)
1979 return;
1980
1981 list = list_alloc();
1982 if (list != NULL)
1983 {
1984 dict_add_list(dict, "borderchars", list);
1985 for (i = 0; i < 8; ++i)
1986 {
1987 len = mb_char2bytes(wp->w_border_char[i], buf);
1988 list_append_string(list, buf, len);
1989 }
1990 }
1991}
1992
1993/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001994 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001995 */
1996 static void
1997get_moved_list(dict_T *dict, win_T *wp)
1998{
1999 list_T *list;
2000
2001 list = list_alloc();
2002 if (list != NULL)
2003 {
2004 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002005 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002006 list_append_number(list, wp->w_popup_mincol);
2007 list_append_number(list, wp->w_popup_maxcol);
2008 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002009 list = list_alloc();
2010 if (list != NULL)
2011 {
2012 dict_add_list(dict, "mousemoved", list);
2013 list_append_number(list, wp->w_popup_mouse_row);
2014 list_append_number(list, wp->w_popup_mouse_mincol);
2015 list_append_number(list, wp->w_popup_mouse_maxcol);
2016 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002017}
2018
2019/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002020 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002021 */
2022 void
2023f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2024{
2025 dict_T *dict;
2026 int id = (int)tv_get_number(argvars);
2027 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002028 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002029 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002030
2031 if (rettv_dict_alloc(rettv) == OK)
2032 {
2033 if (wp == NULL)
2034 return;
2035
2036 dict = rettv->vval.v_dict;
2037 dict_add_number(dict, "line", wp->w_wantline);
2038 dict_add_number(dict, "col", wp->w_wantcol);
2039 dict_add_number(dict, "minwidth", wp->w_minwidth);
2040 dict_add_number(dict, "minheight", wp->w_minheight);
2041 dict_add_number(dict, "maxheight", wp->w_maxheight);
2042 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002043 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002044 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002045 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002046 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002047 dict_add_string(dict, "title", wp->w_popup_title);
2048 dict_add_number(dict, "wrap", wp->w_p_wrap);
2049 dict_add_number(dict, "drag", wp->w_popup_drag);
2050 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002051 if (wp->w_scrollbar_highlight != NULL)
2052 dict_add_string(dict, "scrollbarhighlight",
2053 wp->w_scrollbar_highlight);
2054 if (wp->w_thumb_highlight != NULL)
2055 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002056
Bram Moolenaara3fce622019-06-20 02:31:49 +02002057 // find the tabpage that holds this popup
2058 i = 1;
2059 FOR_ALL_TABPAGES(tp)
2060 {
2061 win_T *p;
2062
2063 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2064 if (p->w_id == id)
2065 break;
2066 if (p != NULL)
2067 break;
2068 ++i;
2069 }
2070 if (tp == NULL)
2071 i = -1; // must be global
2072 else if (tp == curtab)
2073 i = 0;
2074 dict_add_number(dict, "tabpage", i);
2075
Bram Moolenaarae943152019-06-16 22:54:14 +02002076 get_padding_border(dict, wp->w_popup_padding, "padding");
2077 get_padding_border(dict, wp->w_popup_border, "border");
2078 get_borderhighlight(dict, wp);
2079 get_borderchars(dict, wp);
2080 get_moved_list(dict, wp);
2081
2082 if (wp->w_filter_cb.cb_name != NULL)
2083 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2084 if (wp->w_close_cb.cb_name != NULL)
2085 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002086
2087 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2088 ++i)
2089 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2090 {
2091 dict_add_string(dict, "pos",
2092 (char_u *)poppos_entries[i].pp_name);
2093 break;
2094 }
2095
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002096 dict_add_string(dict, "close", (char_u *)(
2097 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2098 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2099
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002100# if defined(FEAT_TIMERS)
2101 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2102 ? (long)wp->w_popup_timer->tr_interval : 0L);
2103# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002104 }
2105}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002106
2107 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002108error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002109{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002110 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002111 {
2112 emsg(_("E994: Not allowed in a popup window"));
2113 return TRUE;
2114 }
2115 return FALSE;
2116}
2117
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002118/*
2119 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002120 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002121 */
2122 void
2123popup_reset_handled()
2124{
2125 win_T *wp;
2126
2127 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2128 wp->w_popup_flags &= ~POPF_HANDLED;
2129 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2130 wp->w_popup_flags &= ~POPF_HANDLED;
2131}
2132
2133/*
2134 * Find the next visible popup where POPF_HANDLED is not set.
2135 * Must have called popup_reset_handled() first.
2136 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2137 * popup with the highest zindex.
2138 */
2139 win_T *
2140find_next_popup(int lowest)
2141{
2142 win_T *wp;
2143 win_T *found_wp;
2144 int found_zindex;
2145
2146 found_zindex = lowest ? INT_MAX : 0;
2147 found_wp = NULL;
2148 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2149 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2150 && (lowest ? wp->w_zindex < found_zindex
2151 : wp->w_zindex > found_zindex))
2152 {
2153 found_zindex = wp->w_zindex;
2154 found_wp = wp;
2155 }
2156 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2157 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2158 && (lowest ? wp->w_zindex < found_zindex
2159 : wp->w_zindex > found_zindex))
2160 {
2161 found_zindex = wp->w_zindex;
2162 found_wp = wp;
2163 }
2164
2165 if (found_wp != NULL)
2166 found_wp->w_popup_flags |= POPF_HANDLED;
2167 return found_wp;
2168}
2169
2170/*
2171 * Invoke the filter callback for window "wp" with typed character "c".
2172 * Uses the global "mod_mask" for modifiers.
2173 * Returns the return value of the filter.
2174 * Careful: The filter may make "wp" invalid!
2175 */
2176 static int
2177invoke_popup_filter(win_T *wp, int c)
2178{
2179 int res;
2180 typval_T rettv;
2181 int dummy;
2182 typval_T argv[3];
2183 char_u buf[NUMBUFLEN];
2184
Bram Moolenaara42d9452019-06-15 21:46:30 +02002185 // Emergency exit: CTRL-C closes the popup.
2186 if (c == Ctrl_C)
2187 {
2188 rettv.v_type = VAR_NUMBER;
2189 rettv.vval.v_number = -1;
2190 popup_close_and_callback(wp, &rettv);
2191 return 1;
2192 }
2193
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002194 argv[0].v_type = VAR_NUMBER;
2195 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2196
2197 // Convert the number to a string, so that the function can use:
2198 // if a:c == "\<F2>"
2199 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2200 argv[1].v_type = VAR_STRING;
2201 argv[1].vval.v_string = vim_strsave(buf);
2202
2203 argv[2].v_type = VAR_UNKNOWN;
2204
Bram Moolenaara42d9452019-06-15 21:46:30 +02002205 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002206 call_callback(&wp->w_filter_cb, -1,
2207 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2208 res = tv_get_number(&rettv);
2209 vim_free(argv[1].vval.v_string);
2210 clear_tv(&rettv);
2211 return res;
2212}
2213
2214/*
2215 * Called when "c" was typed: invoke popup filter callbacks.
2216 * Returns TRUE when the character was consumed,
2217 */
2218 int
2219popup_do_filter(int c)
2220{
2221 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002222 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002223
2224 popup_reset_handled();
2225
2226 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2227 if (wp->w_filter_cb.cb_name != NULL)
2228 res = invoke_popup_filter(wp, c);
2229
2230 return res;
2231}
2232
Bram Moolenaar3397f742019-06-02 18:40:06 +02002233/*
2234 * Called when the cursor moved: check if any popup needs to be closed if the
2235 * cursor moved far enough.
2236 */
2237 void
2238popup_check_cursor_pos()
2239{
2240 win_T *wp;
2241 typval_T tv;
2242
2243 popup_reset_handled();
2244 while ((wp = find_next_popup(TRUE)) != NULL)
2245 if (wp->w_popup_curwin != NULL
2246 && (curwin != wp->w_popup_curwin
2247 || curwin->w_cursor.lnum != wp->w_popup_lnum
2248 || curwin->w_cursor.col < wp->w_popup_mincol
2249 || curwin->w_cursor.col > wp->w_popup_maxcol))
2250 {
2251 tv.v_type = VAR_NUMBER;
2252 tv.vval.v_number = -1;
2253 popup_close_and_callback(wp, &tv);
2254 }
2255}
2256
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002257/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002258 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2259 * "col" and "line" are screen coordinates.
2260 */
2261 static int
2262popup_masked(win_T *wp, int screencol, int screenline)
2263{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002264 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002265 int line = screenline - wp->w_winrow + 1;
2266 listitem_T *lio, *li;
2267 int width, height;
2268
2269 if (wp->w_popup_mask == NULL)
2270 return FALSE;
2271 width = popup_width(wp);
2272 height = popup_height(wp);
2273
2274 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2275 {
2276 int cols, cole;
2277 int lines, linee;
2278
2279 li = lio->li_tv.vval.v_list->lv_first;
2280 cols = tv_get_number(&li->li_tv);
2281 if (cols < 0)
2282 cols = width + cols + 1;
2283 if (col < cols)
2284 continue;
2285 li = li->li_next;
2286 cole = tv_get_number(&li->li_tv);
2287 if (cole < 0)
2288 cole = width + cole + 1;
2289 if (col > cole)
2290 continue;
2291 li = li->li_next;
2292 lines = tv_get_number(&li->li_tv);
2293 if (lines < 0)
2294 lines = height + lines + 1;
2295 if (line < lines)
2296 continue;
2297 li = li->li_next;
2298 linee = tv_get_number(&li->li_tv);
2299 if (linee < 0)
2300 linee = height + linee + 1;
2301 if (line > linee)
2302 continue;
2303 return TRUE;
2304 }
2305 return FALSE;
2306}
2307
2308/*
2309 * Set flags in popup_transparent[] for window "wp" to "val".
2310 */
2311 static void
2312update_popup_transparent(win_T *wp, int val)
2313{
2314 if (wp->w_popup_mask != NULL)
2315 {
2316 int width = popup_width(wp);
2317 int height = popup_height(wp);
2318 listitem_T *lio, *li;
2319 int cols, cole;
2320 int lines, linee;
2321 int col, line;
2322
2323 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2324 {
2325 li = lio->li_tv.vval.v_list->lv_first;
2326 cols = tv_get_number(&li->li_tv);
2327 if (cols < 0)
2328 cols = width + cols + 1;
2329 li = li->li_next;
2330 cole = tv_get_number(&li->li_tv);
2331 if (cole < 0)
2332 cole = width + cole + 1;
2333 li = li->li_next;
2334 lines = tv_get_number(&li->li_tv);
2335 if (lines < 0)
2336 lines = height + lines + 1;
2337 li = li->li_next;
2338 linee = tv_get_number(&li->li_tv);
2339 if (linee < 0)
2340 linee = height + linee + 1;
2341
2342 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002343 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002344 if (cols < 0)
2345 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002346 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002347 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002348 if (lines < 0)
2349 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002350 for (line = lines; line < linee
2351 && line + wp->w_winrow < screen_Rows; ++line)
2352 for (col = cols; col < cole
2353 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002354 popup_transparent[(line + wp->w_winrow) * screen_Columns
2355 + col + wp->w_wincol] = val;
2356 }
2357 }
2358}
2359
2360/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002361 * Update "popup_mask" if needed.
2362 * Also recomputes the popup size and positions.
2363 * Also updates "popup_visible".
2364 * Also marks window lines for redrawing.
2365 */
2366 void
2367may_update_popup_mask(int type)
2368{
2369 win_T *wp;
2370 short *mask;
2371 int line, col;
2372 int redraw_all = FALSE;
2373
2374 // Need to recompute when switching tabs.
2375 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2376 // (such as the screen size) must have changed.
2377 if (popup_mask_tab != curtab || type >= NOT_VALID)
2378 {
2379 popup_mask_refresh = TRUE;
2380 redraw_all = TRUE;
2381 }
2382 if (!popup_mask_refresh)
2383 {
2384 // Check if any buffer has changed.
2385 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2386 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2387 popup_mask_refresh = TRUE;
2388 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2389 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2390 popup_mask_refresh = TRUE;
2391 if (!popup_mask_refresh)
2392 return;
2393 }
2394
2395 // Need to update the mask, something has changed.
2396 popup_mask_refresh = FALSE;
2397 popup_mask_tab = curtab;
2398 popup_visible = FALSE;
2399
2400 // If redrawing everything, just update "popup_mask".
2401 // If redrawing only what is needed, update "popup_mask_next" and then
2402 // compare with "popup_mask" to see what changed.
2403 if (type >= SOME_VALID)
2404 mask = popup_mask;
2405 else
2406 mask = popup_mask_next;
2407 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2408
2409 // Find the window with the lowest zindex that hasn't been handled yet,
2410 // so that the window with a higher zindex overwrites the value in
2411 // popup_mask.
2412 popup_reset_handled();
2413 while ((wp = find_next_popup(TRUE)) != NULL)
2414 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002415 int height;
2416 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002417
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002418 popup_visible = TRUE;
2419
2420 // Recompute the position if the text changed.
2421 if (redraw_all
2422 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2423 popup_adjust_position(wp);
2424
Bram Moolenaard529ba52019-07-02 23:13:53 +02002425 height = popup_height(wp);
2426 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002427 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002428 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002429 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002430 col < wp->w_wincol + width && col < screen_Columns; ++col)
2431 if (!popup_masked(wp, col, line))
2432 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002433 }
2434
2435 // Only check which lines are to be updated if not already
2436 // updating all lines.
2437 if (mask == popup_mask_next)
2438 for (line = 0; line < screen_Rows; ++line)
2439 {
2440 int col_done = 0;
2441
2442 for (col = 0; col < screen_Columns; ++col)
2443 {
2444 int off = line * screen_Columns + col;
2445
2446 if (popup_mask[off] != popup_mask_next[off])
2447 {
2448 popup_mask[off] = popup_mask_next[off];
2449
2450 if (line >= cmdline_row)
2451 {
2452 // the command line needs to be cleared if text below
2453 // the popup is now visible.
2454 if (!msg_scrolled && popup_mask_next[off] == 0)
2455 clear_cmdline = TRUE;
2456 }
2457 else if (col >= col_done)
2458 {
2459 linenr_T lnum;
2460 int line_cp = line;
2461 int col_cp = col;
2462
2463 // The screen position "line" / "col" needs to be
2464 // redrawn. Figure out what window that is and update
2465 // w_redraw_top and w_redr_bot. Only needs to be done
2466 // once for each window line.
2467 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2468 if (wp != NULL)
2469 {
2470 if (line_cp >= wp->w_height)
2471 // In (or below) status line
2472 wp->w_redr_status = TRUE;
2473 // compute the position in the buffer line from the
2474 // position on the screen
2475 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2476 &lnum))
2477 // past bottom
2478 wp->w_redr_status = TRUE;
2479 else
2480 redrawWinline(wp, lnum);
2481
2482 // This line is going to be redrawn, no need to
2483 // check until the right side of the window.
2484 col_done = wp->w_wincol + wp->w_width - 1;
2485 }
2486 }
2487 }
2488 }
2489 }
2490}
2491
2492/*
2493 * Return a string of "len" spaces in IObuff.
2494 */
2495 static char_u *
2496get_spaces(int len)
2497{
2498 vim_memset(IObuff, ' ', (size_t)len);
2499 IObuff[len] = NUL;
2500 return IObuff;
2501}
2502
2503/*
2504 * Update popup windows. They are drawn on top of normal windows.
2505 * "win_update" is called for each popup window, lowest zindex first.
2506 */
2507 void
2508update_popups(void (*win_update)(win_T *wp))
2509{
2510 win_T *wp;
2511 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002512 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002513 int total_width;
2514 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002515 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002516 int popup_attr;
2517 int border_attr[4];
2518 int border_char[8];
2519 char_u buf[MB_MAXBYTES];
2520 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002521 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002522 int padcol = 0;
2523 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002524 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002525 int sb_thumb_top = 0;
2526 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002527 int attr_scroll = 0;
2528 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002529
2530 // Find the window with the lowest zindex that hasn't been updated yet,
2531 // so that the window with a higher zindex is drawn later, thus goes on
2532 // top.
2533 popup_reset_handled();
2534 while ((wp = find_next_popup(TRUE)) != NULL)
2535 {
2536 // This drawing uses the zindex of the popup window, so that it's on
2537 // top of the text but doesn't draw when another popup with higher
2538 // zindex is on top of the character.
2539 screen_zindex = wp->w_zindex;
2540
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002541 // Set flags in popup_transparent[] for masked cells.
2542 update_popup_transparent(wp, 1);
2543
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002544 // adjust w_winrow and w_wincol for border and padding, since
2545 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002546 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002547 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2548 - wp->w_popup_leftoff;
2549 if (wp->w_wincol + left_extra < 0)
2550 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002551 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002552 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002553
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002554 // Draw the popup text, unless it's off screen.
2555 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2556 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002557
2558 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002559 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002560
Bram Moolenaard529ba52019-07-02 23:13:53 +02002561 total_width = popup_width(wp);
2562 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002563 popup_attr = get_wcr_attr(wp);
2564
2565 // We can only use these line drawing characters when 'encoding' is
2566 // "utf-8" and 'ambiwidth' is "single".
2567 if (enc_utf8 && *p_ambw == 's')
2568 {
2569 border_char[0] = border_char[2] = 0x2550;
2570 border_char[1] = border_char[3] = 0x2551;
2571 border_char[4] = 0x2554;
2572 border_char[5] = 0x2557;
2573 border_char[6] = 0x255d;
2574 border_char[7] = 0x255a;
2575 }
2576 else
2577 {
2578 border_char[0] = border_char[2] = '-';
2579 border_char[1] = border_char[3] = '|';
2580 for (i = 4; i < 8; ++i)
2581 border_char[i] = '+';
2582 }
2583 for (i = 0; i < 8; ++i)
2584 if (wp->w_border_char[i] != 0)
2585 border_char[i] = wp->w_border_char[i];
2586
2587 for (i = 0; i < 4; ++i)
2588 {
2589 border_attr[i] = popup_attr;
2590 if (wp->w_border_highlight[i] != NULL)
2591 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2592 }
2593
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002594 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002595 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002596 if (wp->w_popup_border[0] > 0)
2597 {
2598 // top border
2599 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002600 wincol < 0 ? 0 : wincol, wincol + total_width,
2601 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002602 ? border_char[4] : border_char[0],
2603 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002604 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002605 {
2606 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2607 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002608 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002609 }
2610 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002611 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2612 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002613
Bram Moolenaard529ba52019-07-02 23:13:53 +02002614 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2615 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002616 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002617 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2618 - wp->w_has_scrollbar;
2619 if (padcol < 0)
2620 {
2621 padwidth += padcol;
2622 padcol = 0;
2623 }
2624 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002625 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002626 {
2627 // top padding
2628 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002629 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002630 ' ', ' ', popup_attr);
2631 }
2632
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002633 // Title goes on top of border or padding.
2634 if (wp->w_popup_title != NULL)
2635 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2636 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2637
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002638 // Compute scrollbar thumb position and size.
2639 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002640 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002641 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2642
Bram Moolenaar68acb412019-06-26 03:40:36 +02002643 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2644 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002645 if (sb_thumb_height == 0)
2646 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002647 if (linecount <= wp->w_height)
2648 // it just fits, avoid divide by zero
2649 sb_thumb_top = 0;
2650 else
2651 sb_thumb_top = (wp->w_topline - 1
2652 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002653 * (wp->w_height - sb_thumb_height)
2654 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002655 if (wp->w_scrollbar_highlight != NULL)
2656 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2657 else
2658 attr_scroll = highlight_attr[HLF_PSB];
2659 if (wp->w_thumb_highlight != NULL)
2660 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2661 else
2662 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002663 }
2664
2665 for (i = wp->w_popup_border[0];
2666 i < total_height - wp->w_popup_border[2]; ++i)
2667 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002668 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002669 // left and right padding only needed next to the body
2670 int do_padding =
2671 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2672 && i < total_height - wp->w_popup_border[2]
2673 - wp->w_popup_padding[2];
2674
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002675 row = wp->w_winrow + i;
2676
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002677 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002678 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002679 {
2680 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002681 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002682 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002683 if (do_padding && wp->w_popup_padding[3] > 0)
2684 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002685 int col = wincol + wp->w_popup_border[3];
2686
Bram Moolenaard529ba52019-07-02 23:13:53 +02002687 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002688 pad_left = wp->w_popup_padding[3];
2689 if (col < 0)
2690 {
2691 pad_left += col;
2692 col = 0;
2693 }
2694 if (pad_left > 0)
2695 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2696 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002697 // scrollbar
2698 if (wp->w_has_scrollbar)
2699 {
2700 int line = i - top_off;
2701 int scroll_col = wp->w_wincol + total_width - 1
2702 - wp->w_popup_border[1];
2703
2704 if (line >= 0 && line < wp->w_height)
2705 screen_putchar(' ', row, scroll_col,
2706 line >= sb_thumb_top
2707 && line < sb_thumb_top + sb_thumb_height
2708 ? attr_thumb : attr_scroll);
2709 else
2710 screen_putchar(' ', row, scroll_col, popup_attr);
2711 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002712 // right border
2713 if (wp->w_popup_border[1] > 0)
2714 {
2715 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002716 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002717 }
2718 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002719 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002720 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002721 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002722 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2723 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002724 }
2725
2726 if (wp->w_popup_padding[2] > 0)
2727 {
2728 // bottom padding
2729 row = wp->w_winrow + wp->w_popup_border[0]
2730 + wp->w_popup_padding[0] + wp->w_height;
2731 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002732 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002733 }
2734
2735 if (wp->w_popup_border[2] > 0)
2736 {
2737 // bottom border
2738 row = wp->w_winrow + total_height - 1;
2739 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002740 wincol < 0 ? 0 : wincol,
2741 wincol + total_width,
2742 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002743 ? border_char[7] : border_char[2],
2744 border_char[2], border_attr[2]);
2745 if (wp->w_popup_border[1] > 0)
2746 {
2747 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002748 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002749 }
2750 }
2751
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002752 if (wp->w_popup_close == POPCLOSE_BUTTON)
2753 {
2754 // close button goes on top of anything at the top-right corner
2755 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002756 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002757 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2758 }
2759
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002760 update_popup_transparent(wp, 0);
2761
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002762 // Back to the normal zindex.
2763 screen_zindex = 0;
2764 }
2765}
2766
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002767/*
2768 * Mark references in callbacks of one popup window.
2769 */
2770 static int
2771set_ref_in_one_popup(win_T *wp, int copyID)
2772{
2773 int abort = FALSE;
2774 typval_T tv;
2775
2776 if (wp->w_close_cb.cb_partial != NULL)
2777 {
2778 tv.v_type = VAR_PARTIAL;
2779 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2780 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2781 }
2782 if (wp->w_filter_cb.cb_partial != NULL)
2783 {
2784 tv.v_type = VAR_PARTIAL;
2785 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2786 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2787 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002788 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002789 return abort;
2790}
2791
2792/*
2793 * Set reference in callbacks of popup windows.
2794 */
2795 int
2796set_ref_in_popups(int copyID)
2797{
2798 int abort = FALSE;
2799 win_T *wp;
2800 tabpage_T *tp;
2801
2802 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2803 abort = abort || set_ref_in_one_popup(wp, copyID);
2804
2805 FOR_ALL_TABPAGES(tp)
2806 {
2807 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2808 abort = abort || set_ref_in_one_popup(wp, copyID);
2809 if (abort)
2810 break;
2811 }
2812 return abort;
2813}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002814#endif // FEAT_TEXT_PROP