blob: 322537bf87111374c390541994b1dfd3cc4d0440 [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 {
519 if (di->di_tv.v_type != VAR_LIST)
520 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
527 if (list != NULL)
528 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
529 ++i, li = li->li_next)
530 {
531 str = tv_get_string(&li->li_tv);
532 if (*str != NUL)
533 wp->w_border_highlight[i] = vim_strsave(str);
534 }
535 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
536 for (i = 1; i < 4; ++i)
537 wp->w_border_highlight[i] =
538 vim_strsave(wp->w_border_highlight[0]);
539 }
540 }
541
Bram Moolenaar790498b2019-06-01 22:15:29 +0200542 di = dict_find(dict, (char_u *)"borderchars", -1);
543 if (di != NULL)
544 {
545 if (di->di_tv.v_type != VAR_LIST)
546 emsg(_(e_listreq));
547 else
548 {
549 list_T *list = di->di_tv.vval.v_list;
550 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200551 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200552
553 if (list != NULL)
554 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
555 ++i, li = li->li_next)
556 {
557 str = tv_get_string(&li->li_tv);
558 if (*str != NUL)
559 wp->w_border_char[i] = mb_ptr2char(str);
560 }
561 if (list->lv_len == 1)
562 for (i = 1; i < 8; ++i)
563 wp->w_border_char[i] = wp->w_border_char[0];
564 if (list->lv_len == 2)
565 {
566 for (i = 4; i < 8; ++i)
567 wp->w_border_char[i] = wp->w_border_char[1];
568 for (i = 1; i < 4; ++i)
569 wp->w_border_char[i] = wp->w_border_char[0];
570 }
571 }
572 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200573
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200574 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
575 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
576
Bram Moolenaarae943152019-06-16 22:54:14 +0200577 di = dict_find(dict, (char_u *)"zindex", -1);
578 if (di != NULL)
579 {
580 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
581 if (wp->w_zindex < 1)
582 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
583 if (wp->w_zindex > 32000)
584 wp->w_zindex = 32000;
585 }
586
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200587 di = dict_find(dict, (char_u *)"mask", -1);
588 if (di != NULL)
589 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200590 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200591
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200592 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200593 {
594 listitem_T *li;
595
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200596 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200597 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
598 li = li->li_next)
599 {
600 if (li->li_tv.v_type != VAR_LIST
601 || li->li_tv.vval.v_list == NULL
602 || li->li_tv.vval.v_list->lv_len != 4)
603 {
604 ok = FALSE;
605 break;
606 }
607 }
608 }
609 if (ok)
610 {
611 wp->w_popup_mask = di->di_tv.vval.v_list;
612 ++wp->w_popup_mask->lv_refcount;
613 }
614 else
615 semsg(_(e_invargval), "mask");
616 }
617
Bram Moolenaarae943152019-06-16 22:54:14 +0200618#if defined(FEAT_TIMERS)
619 // Add timer to close the popup after some time.
620 nr = dict_get_number(dict, (char_u *)"time");
621 if (nr > 0)
622 popup_add_timeout(wp, nr);
623#endif
624
Bram Moolenaar3397f742019-06-02 18:40:06 +0200625 di = dict_find(dict, (char_u *)"moved", -1);
626 if (di != NULL)
627 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200628 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200629 handle_moved_argument(wp, di, FALSE);
630 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200631
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200632 di = dict_find(dict, (char_u *)"mousemoved", -1);
633 if (di != NULL)
634 {
635 set_mousemoved_values(wp);
636 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200637 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200638
Bram Moolenaarae943152019-06-16 22:54:14 +0200639 di = dict_find(dict, (char_u *)"filter", -1);
640 if (di != NULL)
641 {
642 callback_T callback = get_callback(&di->di_tv);
643
644 if (callback.cb_name != NULL)
645 {
646 free_callback(&wp->w_filter_cb);
647 set_callback(&wp->w_filter_cb, &callback);
648 }
649 }
650
651 di = dict_find(dict, (char_u *)"callback", -1);
652 if (di != NULL)
653 {
654 callback_T callback = get_callback(&di->di_tv);
655
656 if (callback.cb_name != NULL)
657 {
658 free_callback(&wp->w_close_cb);
659 set_callback(&wp->w_close_cb, &callback);
660 }
661 }
662}
663
664/*
665 * Go through the options in "dict" and apply them to popup window "wp".
666 */
667 static void
668apply_options(win_T *wp, dict_T *dict)
669{
670 int nr;
671
672 apply_move_options(wp, dict);
673 apply_general_options(wp, dict);
674
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200675 nr = dict_get_number(dict, (char_u *)"hidden");
676 if (nr > 0)
677 {
678 wp->w_popup_flags |= POPF_HIDDEN;
679 --wp->w_buffer->b_nwindows;
680 }
681
Bram Moolenaar33796b32019-06-08 16:01:13 +0200682 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200683}
684
685/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200686 * Add lines to the popup from a list of strings.
687 */
688 static void
689add_popup_strings(buf_T *buf, list_T *l)
690{
691 listitem_T *li;
692 linenr_T lnum = 0;
693 char_u *p;
694
695 for (li = l->lv_first; li != NULL; li = li->li_next)
696 if (li->li_tv.v_type == VAR_STRING)
697 {
698 p = li->li_tv.vval.v_string;
699 ml_append_buf(buf, lnum++,
700 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
701 }
702}
703
704/*
705 * Add lines to the popup from a list of dictionaries.
706 */
707 static void
708add_popup_dicts(buf_T *buf, list_T *l)
709{
710 listitem_T *li;
711 listitem_T *pli;
712 linenr_T lnum = 0;
713 char_u *p;
714 dict_T *dict;
715
716 // first add the text lines
717 for (li = l->lv_first; li != NULL; li = li->li_next)
718 {
719 if (li->li_tv.v_type != VAR_DICT)
720 {
721 emsg(_(e_dictreq));
722 return;
723 }
724 dict = li->li_tv.vval.v_dict;
725 p = dict == NULL ? NULL
726 : dict_get_string(dict, (char_u *)"text", FALSE);
727 ml_append_buf(buf, lnum++,
728 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
729 }
730
731 // add the text properties
732 lnum = 1;
733 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
734 {
735 dictitem_T *di;
736 list_T *plist;
737
738 dict = li->li_tv.vval.v_dict;
739 di = dict_find(dict, (char_u *)"props", -1);
740 if (di != NULL)
741 {
742 if (di->di_tv.v_type != VAR_LIST)
743 {
744 emsg(_(e_listreq));
745 return;
746 }
747 plist = di->di_tv.vval.v_list;
748 if (plist != NULL)
749 {
750 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
751 {
752 if (pli->li_tv.v_type != VAR_DICT)
753 {
754 emsg(_(e_dictreq));
755 return;
756 }
757 dict = pli->li_tv.vval.v_dict;
758 if (dict != NULL)
759 {
760 int col = dict_get_number(dict, (char_u *)"col");
761
762 prop_add_common( lnum, col, dict, buf, NULL);
763 }
764 }
765 }
766 }
767 }
768}
769
770/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200771 * Get the padding plus border at the top, adjusted to 1 if there is a title.
772 */
773 static int
774popup_top_extra(win_T *wp)
775{
776 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
777
778 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
779 return 1;
780 return extra;
781}
782
783/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200784 * Return the height of popup window "wp", including border and padding.
785 */
786 int
787popup_height(win_T *wp)
788{
789 return wp->w_height
790 + popup_top_extra(wp)
791 + wp->w_popup_padding[2] + wp->w_popup_border[2];
792}
793
794/*
795 * Return the width of popup window "wp", including border, padding and
796 * scrollbar.
797 */
798 int
799popup_width(win_T *wp)
800{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200801 // w_leftcol is how many columns of the core are left of the screen
802 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200803 return wp->w_width + wp->w_leftcol
804 + wp->w_popup_padding[3] + wp->w_popup_border[3]
805 + wp->w_popup_padding[1] + wp->w_popup_border[1]
806 + wp->w_has_scrollbar
807 + wp->w_popup_rightoff;
808}
809
810/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200811 * Adjust the position and size of the popup to fit on the screen.
812 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200813 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200814popup_adjust_position(win_T *wp)
815{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200816 linenr_T lnum;
817 int wrapped = 0;
818 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200819 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200820 int center_vert = FALSE;
821 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200822 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200823 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200824 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
825 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
826 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
827 int extra_height = top_extra + bot_extra;
828 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200829 int org_winrow = wp->w_winrow;
830 int org_wincol = wp->w_wincol;
831 int org_width = wp->w_width;
832 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200833 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200834 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200835 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200836
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200837 wp->w_winrow = 0;
838 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200839 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200840 wp->w_popup_leftoff = 0;
841 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200842 if (wp->w_popup_pos == POPPOS_CENTER)
843 {
844 // center after computing the size
845 center_vert = TRUE;
846 center_hor = TRUE;
847 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200848 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200849 {
850 if (wp->w_wantline == 0)
851 center_vert = TRUE;
852 else if (wp->w_popup_pos == POPPOS_TOPLEFT
853 || wp->w_popup_pos == POPPOS_TOPRIGHT)
854 {
855 wp->w_winrow = wp->w_wantline - 1;
856 if (wp->w_winrow >= Rows)
857 wp->w_winrow = Rows - 1;
858 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200859
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200860 if (wp->w_wantcol == 0)
861 center_hor = TRUE;
862 else if (wp->w_popup_pos == POPPOS_TOPLEFT
863 || wp->w_popup_pos == POPPOS_BOTLEFT)
864 {
865 wp->w_wincol = wp->w_wantcol - 1;
866 if (wp->w_wincol >= Columns - 3)
867 wp->w_wincol = Columns - 3;
868 }
869 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200870
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200871 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200872 // When left aligned use the space available, but shift to the left when we
873 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200874 maxspace = Columns - wp->w_wincol - left_extra;
875 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200876 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200877 {
878 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200879 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200880 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200881
Bram Moolenaar8d241042019-06-12 23:40:01 +0200882 // start at the desired first line
883 wp->w_topline = wp->w_firstline;
884 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
885 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
886
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200887 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200888 // Use a minimum width of one, so that something shows when there is no
889 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200890 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200891 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200892 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200893 {
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200894 // count Tabs for what they are worth
895 int len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
896 (colnr_T)MAXCOL);
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200897
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200898 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200899 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200900 while (len > maxwidth)
901 {
902 ++wrapped;
903 len -= maxwidth;
904 wp->w_width = maxwidth;
905 }
906 }
907 else if (len > maxwidth
908 && allow_adjust_left
909 && (wp->w_popup_pos == POPPOS_TOPLEFT
910 || wp->w_popup_pos == POPPOS_BOTLEFT))
911 {
912 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200913 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200914
Bram Moolenaar51c31312019-06-15 22:27:23 +0200915 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200916 {
917 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200918
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200919 len -= truncate_shift;
920 shift_by -= truncate_shift;
921 }
922
923 wp->w_wincol -= shift_by;
924 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200925 wp->w_width = maxwidth;
926 }
927 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +0200928 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200929 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +0200930 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
931 wp->w_width = wp->w_maxwidth;
932 }
Bram Moolenaar8d241042019-06-12 23:40:01 +0200933 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200934 if (wp->w_maxheight > 0
935 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200936 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200937 }
938
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200939 wp->w_has_scrollbar = wp->w_want_scrollbar
940 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
941
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200942 minwidth = wp->w_minwidth;
943 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
944 {
945 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
946
947 if (minwidth < title_len)
948 minwidth = title_len;
949 }
950
951 if (minwidth > 0 && wp->w_width < minwidth)
952 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200953 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +0200954 {
955 if (wp->w_width > maxspace)
956 // some columns cut off on the right
957 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200958 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200959 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200960 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200961 {
962 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
963 if (wp->w_wincol < 0)
964 wp->w_wincol = 0;
965 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200966 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
967 || wp->w_popup_pos == POPPOS_TOPRIGHT)
968 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200969 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
970
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200971 // Right aligned: move to the right if needed.
972 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200973 if (leftoff >= 0)
974 wp->w_wincol = leftoff;
975 else if (wp->w_popup_fixed)
976 {
977 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +0200978 // columns when displaying the window, minus left border and
979 // padding.
980 if (-leftoff > left_extra)
981 wp->w_leftcol = -leftoff - left_extra;
982 wp->w_width -= wp->w_leftcol;
983 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200984 if (wp->w_width < 0)
985 wp->w_width = 0;
986 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200987 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200988
Bram Moolenaar8d241042019-06-12 23:40:01 +0200989 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
990 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200991 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
992 wp->w_height = wp->w_minheight;
993 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
994 wp->w_height = wp->w_maxheight;
995 if (wp->w_height > Rows - wp->w_winrow)
996 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200997
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200998 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200999 {
1000 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1001 if (wp->w_winrow < 0)
1002 wp->w_winrow = 0;
1003 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001004 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1005 || wp->w_popup_pos == POPPOS_BOTLEFT)
1006 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001007 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001008 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001009 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001010 else
1011 // not enough space, make top aligned
1012 wp->w_winrow = wp->w_wantline + 1;
1013 }
1014
Bram Moolenaar17146962019-05-30 00:12:11 +02001015 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001016
1017 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001018 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001019 if (org_winrow != wp->w_winrow
1020 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001021 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001022 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001023 || org_width != wp->w_width
1024 || org_height != wp->w_height)
1025 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001026 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001027 popup_mask_refresh = TRUE;
1028 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001029}
1030
Bram Moolenaar17627312019-06-02 19:53:44 +02001031typedef enum
1032{
1033 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001034 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001035 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001036 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001037 TYPE_DIALOG,
1038 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +02001039} create_type_T;
1040
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001041/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001042 * Make "buf" empty and set the contents to "text".
1043 * Used by popup_create() and popup_settext().
1044 */
1045 static void
1046popup_set_buffer_text(buf_T *buf, typval_T text)
1047{
1048 int lnum;
1049
1050 // Clear the buffer, then replace the lines.
1051 curbuf = buf;
1052 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1053 ml_delete(lnum, FALSE);
1054 curbuf = curwin->w_buffer;
1055
1056 // Add text to the buffer.
1057 if (text.v_type == VAR_STRING)
1058 {
1059 // just a string
1060 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1061 }
1062 else
1063 {
1064 list_T *l = text.vval.v_list;
1065
1066 if (l->lv_len > 0)
1067 {
1068 if (l->lv_first->li_tv.v_type == VAR_STRING)
1069 // list of strings
1070 add_popup_strings(buf, l);
1071 else
1072 // list of dictionaries
1073 add_popup_dicts(buf, l);
1074 }
1075 }
1076
1077 // delete the line that was in the empty buffer
1078 curbuf = buf;
1079 ml_delete(buf->b_ml.ml_line_count, FALSE);
1080 curbuf = curwin->w_buffer;
1081}
1082
1083/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001084 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001085 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001086 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001087 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001088popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001089{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001090 win_T *wp;
1091 tabpage_T *tp = NULL;
1092 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001093 int new_buffer;
1094 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001095 dict_T *d;
1096 int nr;
1097 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001098
1099 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001100 if (argvars[0].v_type == VAR_NUMBER)
1101 {
1102 buf = buflist_findnr( argvars[0].vval.v_number);
1103 if (buf == NULL)
1104 {
1105 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1106 return NULL;
1107 }
1108 }
1109 else if (!(argvars[0].v_type == VAR_STRING
1110 && argvars[0].vval.v_string != NULL)
1111 && !(argvars[0].v_type == VAR_LIST
1112 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001113 {
1114 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001115 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001116 }
1117 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1118 {
1119 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001120 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001121 }
1122 d = argvars[1].vval.v_dict;
1123
Bram Moolenaara3fce622019-06-20 02:31:49 +02001124 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1125 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1126 else if (type == TYPE_NOTIFICATION)
1127 tabnr = -1; // notifications are global by default
1128 else
1129 tabnr = 0;
1130 if (tabnr > 0)
1131 {
1132 tp = find_tabpage(tabnr);
1133 if (tp == NULL)
1134 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001135 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001136 return NULL;
1137 }
1138 }
1139
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001140 // Create the window and buffer.
1141 wp = win_alloc_popup_win();
1142 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001143 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001144 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001145 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001146 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001147
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001148 if (buf != NULL)
1149 {
1150 // use existing buffer
1151 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001152 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001153 buffer_ensure_loaded(buf);
1154 }
1155 else
1156 {
1157 // create a new buffer associated with the popup
1158 new_buffer = TRUE;
1159 buf = buflist_new(NULL, NULL, (linenr_T)0,
1160 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1161 if (buf == NULL)
1162 return NULL;
1163 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001164
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001165 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001166
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001167 set_local_options_default(wp);
1168 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001169 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001170 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1171 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1172 buf->b_p_ul = -1; // no undo
1173 buf->b_p_swf = FALSE; // no swap file
1174 buf->b_p_bl = FALSE; // unlisted buffer
1175 buf->b_locked = TRUE;
1176 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001177
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001178 // Avoid that 'buftype' is reset when this buffer is entered.
1179 buf->b_p_initialized = TRUE;
1180 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001181
Bram Moolenaara3fce622019-06-20 02:31:49 +02001182 if (tp != NULL)
1183 {
1184 // popup on specified tab page
1185 wp->w_next = tp->tp_first_popupwin;
1186 tp->tp_first_popupwin = wp;
1187 }
1188 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001189 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001190 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001191 wp->w_next = curtab->tp_first_popupwin;
1192 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001193 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001194 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001195 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001196 win_T *prev = first_popupwin;
1197
1198 // Global popup: add at the end, so that it gets displayed on top of
1199 // older ones with the same zindex. Matters for notifications.
1200 if (first_popupwin == NULL)
1201 first_popupwin = wp;
1202 else
1203 {
1204 while (prev->w_next != NULL)
1205 prev = prev->w_next;
1206 prev->w_next = wp;
1207 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001208 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001209
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001210 if (new_buffer)
1211 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001212
Bram Moolenaar17627312019-06-02 19:53:44 +02001213 if (type == TYPE_ATCURSOR)
1214 {
1215 wp->w_popup_pos = POPPOS_BOTLEFT;
1216 setcursor_mayforce(TRUE);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001217 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
Bram Moolenaar17627312019-06-02 19:53:44 +02001218 if (wp->w_wantline == 0) // cursor in first line
1219 {
1220 wp->w_wantline = 2;
1221 wp->w_popup_pos = POPPOS_TOPLEFT;
1222 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001223 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar17627312019-06-02 19:53:44 +02001224 set_moved_values(wp);
1225 set_moved_columns(wp, FIND_STRING);
1226 }
1227
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001228 if (type == TYPE_BEVAL)
1229 {
1230 wp->w_popup_pos = POPPOS_BOTLEFT;
1231
1232 // by default use the mouse position
1233 wp->w_wantline = mouse_row;
1234 if (wp->w_wantline <= 0) // mouse on first line
1235 {
1236 wp->w_wantline = 2;
1237 wp->w_popup_pos = POPPOS_TOPLEFT;
1238 }
1239 wp->w_wantcol = mouse_col + 1;
1240 set_mousemoved_values(wp);
1241 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1242 }
1243
Bram Moolenaar33796b32019-06-08 16:01:13 +02001244 // set default values
1245 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001246 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001247
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001248 if (type == TYPE_NOTIFICATION)
1249 {
1250 win_T *twp, *nextwin;
1251 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001252
1253 // Try to not overlap with another global popup. Guess we need 3
1254 // more screen lines than buffer lines.
1255 wp->w_wantline = 1;
1256 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1257 {
1258 nextwin = twp->w_next;
1259 if (twp != wp
1260 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1261 && twp->w_winrow <= wp->w_wantline - 1 + height
1262 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1263 {
1264 // move to below this popup and restart the loop to check for
1265 // overlap with other popups
1266 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1267 nextwin = first_popupwin;
1268 }
1269 }
1270 if (wp->w_wantline + height > Rows)
1271 {
1272 // can't avoid overlap, put on top in the hope that message goes
1273 // away soon.
1274 wp->w_wantline = 1;
1275 }
1276
1277 wp->w_wantcol = 10;
1278 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001279 wp->w_minwidth = 20;
1280 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001281 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001282 for (i = 0; i < 4; ++i)
1283 wp->w_popup_border[i] = 1;
1284 wp->w_popup_padding[1] = 1;
1285 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001286
1287 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001288 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001289 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1290 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001291 }
1292
Bram Moolenaara730e552019-06-16 19:05:31 +02001293 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001294 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001295 wp->w_popup_pos = POPPOS_CENTER;
1296 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1297 wp->w_popup_drag = 1;
1298 for (i = 0; i < 4; ++i)
1299 {
1300 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001301 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001302 }
1303 }
1304
Bram Moolenaara730e552019-06-16 19:05:31 +02001305 if (type == TYPE_MENU)
1306 {
1307 typval_T tv;
1308 callback_T callback;
1309
1310 tv.v_type = VAR_STRING;
1311 tv.vval.v_string = (char_u *)"popup_filter_menu";
1312 callback = get_callback(&tv);
1313 if (callback.cb_name != NULL)
1314 set_callback(&wp->w_filter_cb, &callback);
1315
1316 wp->w_p_wrap = 0;
1317 }
1318
Bram Moolenaarae943152019-06-16 22:54:14 +02001319 for (i = 0; i < 4; ++i)
1320 VIM_CLEAR(wp->w_border_highlight[i]);
1321 for (i = 0; i < 8; ++i)
1322 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001323 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001324 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001325
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001326 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001327 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001328
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001329#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001330 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1331 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001332#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001333
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001334 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001335
1336 wp->w_vsep_width = 0;
1337
1338 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001339 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001340
1341 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001342}
1343
1344/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001345 * popup_clear()
1346 */
1347 void
1348f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1349{
1350 close_all_popups();
1351}
1352
1353/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001354 * popup_create({text}, {options})
1355 */
1356 void
1357f_popup_create(typval_T *argvars, typval_T *rettv)
1358{
Bram Moolenaar17627312019-06-02 19:53:44 +02001359 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001360}
1361
1362/*
1363 * popup_atcursor({text}, {options})
1364 */
1365 void
1366f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1367{
Bram Moolenaar17627312019-06-02 19:53:44 +02001368 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001369}
1370
1371/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001372 * popup_beval({text}, {options})
1373 */
1374 void
1375f_popup_beval(typval_T *argvars, typval_T *rettv)
1376{
1377 popup_create(argvars, rettv, TYPE_BEVAL);
1378}
1379
1380/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001381 * Invoke the close callback for window "wp" with value "result".
1382 * Careful: The callback may make "wp" invalid!
1383 */
1384 static void
1385invoke_popup_callback(win_T *wp, typval_T *result)
1386{
1387 typval_T rettv;
1388 int dummy;
1389 typval_T argv[3];
1390
1391 argv[0].v_type = VAR_NUMBER;
1392 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1393
1394 if (result != NULL && result->v_type != VAR_UNKNOWN)
1395 copy_tv(result, &argv[1]);
1396 else
1397 {
1398 argv[1].v_type = VAR_NUMBER;
1399 argv[1].vval.v_number = 0;
1400 }
1401
1402 argv[2].v_type = VAR_UNKNOWN;
1403
1404 call_callback(&wp->w_close_cb, -1,
1405 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1406 if (result != NULL)
1407 clear_tv(&argv[1]);
1408 clear_tv(&rettv);
1409}
1410
1411/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001412 * Close popup "wp" and invoke any close callback for it.
1413 */
1414 static void
1415popup_close_and_callback(win_T *wp, typval_T *arg)
1416{
1417 int id = wp->w_id;
1418
1419 if (wp->w_close_cb.cb_name != NULL)
1420 // Careful: This may make "wp" invalid.
1421 invoke_popup_callback(wp, arg);
1422
1423 popup_close(id);
1424}
1425
1426/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001427 * Close popup "wp" because of a mouse click.
1428 */
1429 void
1430popup_close_for_mouse_click(win_T *wp)
1431{
1432 typval_T res;
1433
1434 res.v_type = VAR_NUMBER;
1435 res.vval.v_number = -2;
1436 popup_close_and_callback(wp, &res);
1437}
1438
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001439 static void
1440check_mouse_moved(win_T *wp, win_T *mouse_wp)
1441{
1442 // Close the popup when all if these are true:
1443 // - the mouse is not on this popup
1444 // - "mousemoved" was used
1445 // - the mouse is no longer on the same screen row or the mouse column is
1446 // outside of the relevant text
1447 if (wp != mouse_wp
1448 && wp->w_popup_mouse_row != 0
1449 && (wp->w_popup_mouse_row != mouse_row
1450 || mouse_col < wp->w_popup_mouse_mincol
1451 || mouse_col > wp->w_popup_mouse_maxcol))
1452 {
1453 typval_T res;
1454
1455 res.v_type = VAR_NUMBER;
1456 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001457 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001458 popup_close_and_callback(wp, &res);
1459 }
1460}
1461
1462/*
1463 * Called when the mouse moved: may close a popup with "mousemoved".
1464 */
1465 void
1466popup_handle_mouse_moved(void)
1467{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001468 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001469 win_T *mouse_wp;
1470 int row = mouse_row;
1471 int col = mouse_col;
1472
1473 // find the window where the mouse is in
1474 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1475
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001476 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1477 {
1478 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001479 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001480 }
1481 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1482 {
1483 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001484 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001485 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001486}
1487
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001488/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001489 * In a filter: check if the typed key is a mouse event that is used for
1490 * dragging the popup.
1491 */
1492 static void
1493filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1494{
1495 int row = mouse_row;
1496 int col = mouse_col;
1497
1498 if (wp->w_popup_drag
1499 && is_mouse_key(c)
1500 && (wp == popup_dragwin
1501 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1502 // do not consume the key, allow for dragging the popup
1503 rettv->vval.v_number = 0;
1504}
1505
1506 static void
1507popup_highlight_curline(win_T *wp)
1508{
1509 int id;
1510 char buf[100];
1511
1512 match_delete(wp, 1, FALSE);
1513
Bram Moolenaara901a372019-07-13 16:38:50 +02001514 // Scroll to show the line with the cursor. This assumes lines don't wrap.
1515 while (wp->w_topline + wp->w_height - 1 < wp->w_cursor.lnum)
1516 wp->w_topline++;
1517 while (wp->w_cursor.lnum < wp->w_topline)
1518 wp->w_topline--;
1519
Bram Moolenaara730e552019-06-16 19:05:31 +02001520 id = syn_name2id((char_u *)"PopupSelected");
1521 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1522 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1523 (char_u *)buf, 10, 1, NULL, NULL);
1524}
1525
1526/*
1527 * popup_filter_menu({text}, {options})
1528 */
1529 void
1530f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1531{
1532 int id = tv_get_number(&argvars[0]);
1533 win_T *wp = win_id2wp(id);
1534 char_u *key = tv_get_string(&argvars[1]);
1535 typval_T res;
1536 int c;
1537 linenr_T old_lnum;
1538
1539 // If the popup has been closed do not consume the key.
1540 if (wp == NULL)
1541 return;
1542
1543 c = *key;
1544 if (c == K_SPECIAL && key[1] != NUL)
1545 c = TO_SPECIAL(key[1], key[2]);
1546
1547 // consume all keys until done
1548 rettv->vval.v_number = 1;
1549 res.v_type = VAR_NUMBER;
1550
1551 old_lnum = wp->w_cursor.lnum;
1552 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1553 --wp->w_cursor.lnum;
1554 if ((c == 'j' || c == 'J' || c == K_DOWN)
1555 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1556 ++wp->w_cursor.lnum;
1557 if (old_lnum != wp->w_cursor.lnum)
1558 {
1559 popup_highlight_curline(wp);
1560 return;
1561 }
1562
1563 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1564 {
1565 // Cancelled, invoke callback with -1
1566 res.vval.v_number = -1;
1567 popup_close_and_callback(wp, &res);
1568 return;
1569 }
1570 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1571 {
1572 // Invoke callback with current index.
1573 res.vval.v_number = wp->w_cursor.lnum;
1574 popup_close_and_callback(wp, &res);
1575 return;
1576 }
1577
1578 filter_handle_drag(wp, c, rettv);
1579}
1580
1581/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001582 * popup_filter_yesno({text}, {options})
1583 */
1584 void
1585f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1586{
1587 int id = tv_get_number(&argvars[0]);
1588 win_T *wp = win_id2wp(id);
1589 char_u *key = tv_get_string(&argvars[1]);
1590 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001591 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001592
1593 // If the popup has been closed don't consume the key.
1594 if (wp == NULL)
1595 return;
1596
Bram Moolenaara730e552019-06-16 19:05:31 +02001597 c = *key;
1598 if (c == K_SPECIAL && key[1] != NUL)
1599 c = TO_SPECIAL(key[1], key[2]);
1600
Bram Moolenaara42d9452019-06-15 21:46:30 +02001601 // consume all keys until done
1602 rettv->vval.v_number = 1;
1603
Bram Moolenaara730e552019-06-16 19:05:31 +02001604 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001605 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001606 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001607 res.vval.v_number = 0;
1608 else
1609 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001610 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001611 return;
1612 }
1613
1614 // Invoke callback
1615 res.v_type = VAR_NUMBER;
1616 popup_close_and_callback(wp, &res);
1617}
1618
1619/*
1620 * popup_dialog({text}, {options})
1621 */
1622 void
1623f_popup_dialog(typval_T *argvars, typval_T *rettv)
1624{
1625 popup_create(argvars, rettv, TYPE_DIALOG);
1626}
1627
1628/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001629 * popup_menu({text}, {options})
1630 */
1631 void
1632f_popup_menu(typval_T *argvars, typval_T *rettv)
1633{
1634 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1635
1636 if (wp != NULL)
1637 popup_highlight_curline(wp);
1638}
1639
1640/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001641 * popup_notification({text}, {options})
1642 */
1643 void
1644f_popup_notification(typval_T *argvars, typval_T *rettv)
1645{
1646 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1647}
1648
1649/*
1650 * Find the popup window with window-ID "id".
1651 * If the popup window does not exist NULL is returned.
1652 * If the window is not a popup window, and error message is given.
1653 */
1654 static win_T *
1655find_popup_win(int id)
1656{
1657 win_T *wp = win_id2wp(id);
1658
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001659 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001660 {
1661 semsg(_("E993: window %d is not a popup window"), id);
1662 return NULL;
1663 }
1664 return wp;
1665}
1666
1667/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001668 * popup_close({id})
1669 */
1670 void
1671f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1672{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001673 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001674 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001675
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001676 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001677 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001678}
1679
1680/*
1681 * popup_hide({id})
1682 */
1683 void
1684f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1685{
1686 int id = (int)tv_get_number(argvars);
1687 win_T *wp = find_popup_win(id);
1688
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001689 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001690 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001691 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001692 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001693 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001694 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001695 }
1696}
1697
1698/*
1699 * popup_show({id})
1700 */
1701 void
1702f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1703{
1704 int id = (int)tv_get_number(argvars);
1705 win_T *wp = find_popup_win(id);
1706
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001707 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001708 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001709 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001710 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001711 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001712 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001713 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001714}
1715
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001716/*
1717 * popup_settext({id}, {text})
1718 */
1719 void
1720f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1721{
1722 int id = (int)tv_get_number(&argvars[0]);
1723 win_T *wp = find_popup_win(id);
1724
1725 if (wp != NULL)
1726 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001727 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1728 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1729 else
1730 {
1731 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1732 popup_adjust_position(wp);
1733 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001734 }
1735}
1736
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001737 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001738popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001739{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001740 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001741 if (wp->w_winrow + wp->w_height >= cmdline_row)
1742 clear_cmdline = TRUE;
1743 win_free_popup(wp);
1744 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001745 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001746}
1747
Bram Moolenaarec583842019-05-26 14:11:23 +02001748/*
1749 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001750 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001751 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001752 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001753popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001754{
1755 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001756 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001757 win_T *prev = NULL;
1758
Bram Moolenaarec583842019-05-26 14:11:23 +02001759 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001760 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001761 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001762 {
1763 if (prev == NULL)
1764 first_popupwin = wp->w_next;
1765 else
1766 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001767 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001768 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001769 }
1770
Bram Moolenaarec583842019-05-26 14:11:23 +02001771 // go through tab-local popups
1772 FOR_ALL_TABPAGES(tp)
1773 popup_close_tabpage(tp, id);
1774}
1775
1776/*
1777 * Close a popup window with Window-id "id" in tabpage "tp".
1778 */
1779 void
1780popup_close_tabpage(tabpage_T *tp, int id)
1781{
1782 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001783 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001784 win_T *prev = NULL;
1785
Bram Moolenaarec583842019-05-26 14:11:23 +02001786 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1787 if (wp->w_id == id)
1788 {
1789 if (prev == NULL)
1790 *root = wp->w_next;
1791 else
1792 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001793 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001794 return;
1795 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001796}
1797
1798 void
1799close_all_popups(void)
1800{
1801 while (first_popupwin != NULL)
1802 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001803 while (curtab->tp_first_popupwin != NULL)
1804 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001805}
1806
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001807/*
1808 * popup_move({id}, {options})
1809 */
1810 void
1811f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1812{
Bram Moolenaarae943152019-06-16 22:54:14 +02001813 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001814 int id = (int)tv_get_number(argvars);
1815 win_T *wp = find_popup_win(id);
1816
1817 if (wp == NULL)
1818 return; // invalid {id}
1819
1820 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1821 {
1822 emsg(_(e_dictreq));
1823 return;
1824 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001825 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001826
Bram Moolenaarae943152019-06-16 22:54:14 +02001827 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001828
1829 if (wp->w_winrow + wp->w_height >= cmdline_row)
1830 clear_cmdline = TRUE;
1831 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001832}
1833
Bram Moolenaarbc133542019-05-29 20:26:46 +02001834/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001835 * popup_setoptions({id}, {options})
1836 */
1837 void
1838f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1839{
1840 dict_T *dict;
1841 int id = (int)tv_get_number(argvars);
1842 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001843 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001844
1845 if (wp == NULL)
1846 return; // invalid {id}
1847
1848 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1849 {
1850 emsg(_(e_dictreq));
1851 return;
1852 }
1853 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001854 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001855
1856 apply_move_options(wp, dict);
1857 apply_general_options(wp, dict);
1858
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001859 if (old_firstline != wp->w_firstline)
1860 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001861 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001862 popup_adjust_position(wp);
1863}
1864
1865/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001866 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001867 */
1868 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001869f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001870{
1871 dict_T *dict;
1872 int id = (int)tv_get_number(argvars);
1873 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001874 int top_extra;
1875 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001876
1877 if (rettv_dict_alloc(rettv) == OK)
1878 {
1879 if (wp == NULL)
1880 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001881 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001882 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1883
Bram Moolenaarbc133542019-05-29 20:26:46 +02001884 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001885
Bram Moolenaarbc133542019-05-29 20:26:46 +02001886 dict_add_number(dict, "line", wp->w_winrow + 1);
1887 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001888 dict_add_number(dict, "width", wp->w_width + left_extra
1889 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1890 dict_add_number(dict, "height", wp->w_height + top_extra
1891 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001892
1893 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1894 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1895 dict_add_number(dict, "core_width", wp->w_width);
1896 dict_add_number(dict, "core_height", wp->w_height);
1897
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001898 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001899 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001900 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001901 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001902 }
1903}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001904/*
1905 * popup_locate({row}, {col})
1906 */
1907 void
1908f_popup_locate(typval_T *argvars, typval_T *rettv)
1909{
1910 int row = tv_get_number(&argvars[0]) - 1;
1911 int col = tv_get_number(&argvars[1]) - 1;
1912 win_T *wp;
1913
1914 wp = mouse_find_win(&row, &col, FIND_POPUP);
1915 if (WIN_IS_POPUP(wp))
1916 rettv->vval.v_number = wp->w_id;
1917}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001918
1919/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001920 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1921 */
1922 static void
1923get_padding_border(dict_T *dict, int *array, char *name)
1924{
1925 list_T *list;
1926 int i;
1927
1928 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1929 return;
1930
1931 list = list_alloc();
1932 if (list != NULL)
1933 {
1934 dict_add_list(dict, name, list);
1935 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1936 for (i = 0; i < 4; ++i)
1937 list_append_number(list, array[i]);
1938 }
1939}
1940
1941/*
1942 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1943 */
1944 static void
1945get_borderhighlight(dict_T *dict, win_T *wp)
1946{
1947 list_T *list;
1948 int i;
1949
1950 for (i = 0; i < 4; ++i)
1951 if (wp->w_border_highlight[i] != NULL)
1952 break;
1953 if (i == 4)
1954 return;
1955
1956 list = list_alloc();
1957 if (list != NULL)
1958 {
1959 dict_add_list(dict, "borderhighlight", list);
1960 for (i = 0; i < 4; ++i)
1961 list_append_string(list, wp->w_border_highlight[i], -1);
1962 }
1963}
1964
1965/*
1966 * For popup_getoptions(): add a "borderchars" entry to "dict".
1967 */
1968 static void
1969get_borderchars(dict_T *dict, win_T *wp)
1970{
1971 list_T *list;
1972 int i;
1973 char_u buf[NUMBUFLEN];
1974 int len;
1975
1976 for (i = 0; i < 8; ++i)
1977 if (wp->w_border_char[i] != 0)
1978 break;
1979 if (i == 8)
1980 return;
1981
1982 list = list_alloc();
1983 if (list != NULL)
1984 {
1985 dict_add_list(dict, "borderchars", list);
1986 for (i = 0; i < 8; ++i)
1987 {
1988 len = mb_char2bytes(wp->w_border_char[i], buf);
1989 list_append_string(list, buf, len);
1990 }
1991 }
1992}
1993
1994/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001995 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001996 */
1997 static void
1998get_moved_list(dict_T *dict, win_T *wp)
1999{
2000 list_T *list;
2001
2002 list = list_alloc();
2003 if (list != NULL)
2004 {
2005 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002006 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002007 list_append_number(list, wp->w_popup_mincol);
2008 list_append_number(list, wp->w_popup_maxcol);
2009 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002010 list = list_alloc();
2011 if (list != NULL)
2012 {
2013 dict_add_list(dict, "mousemoved", list);
2014 list_append_number(list, wp->w_popup_mouse_row);
2015 list_append_number(list, wp->w_popup_mouse_mincol);
2016 list_append_number(list, wp->w_popup_mouse_maxcol);
2017 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002018}
2019
2020/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002021 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002022 */
2023 void
2024f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2025{
2026 dict_T *dict;
2027 int id = (int)tv_get_number(argvars);
2028 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002029 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002030 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002031
2032 if (rettv_dict_alloc(rettv) == OK)
2033 {
2034 if (wp == NULL)
2035 return;
2036
2037 dict = rettv->vval.v_dict;
2038 dict_add_number(dict, "line", wp->w_wantline);
2039 dict_add_number(dict, "col", wp->w_wantcol);
2040 dict_add_number(dict, "minwidth", wp->w_minwidth);
2041 dict_add_number(dict, "minheight", wp->w_minheight);
2042 dict_add_number(dict, "maxheight", wp->w_maxheight);
2043 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002044 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002045 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002046 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002047 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002048 dict_add_string(dict, "title", wp->w_popup_title);
2049 dict_add_number(dict, "wrap", wp->w_p_wrap);
2050 dict_add_number(dict, "drag", wp->w_popup_drag);
2051 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002052 if (wp->w_scrollbar_highlight != NULL)
2053 dict_add_string(dict, "scrollbarhighlight",
2054 wp->w_scrollbar_highlight);
2055 if (wp->w_thumb_highlight != NULL)
2056 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002057
Bram Moolenaara3fce622019-06-20 02:31:49 +02002058 // find the tabpage that holds this popup
2059 i = 1;
2060 FOR_ALL_TABPAGES(tp)
2061 {
2062 win_T *p;
2063
2064 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2065 if (p->w_id == id)
2066 break;
2067 if (p != NULL)
2068 break;
2069 ++i;
2070 }
2071 if (tp == NULL)
2072 i = -1; // must be global
2073 else if (tp == curtab)
2074 i = 0;
2075 dict_add_number(dict, "tabpage", i);
2076
Bram Moolenaarae943152019-06-16 22:54:14 +02002077 get_padding_border(dict, wp->w_popup_padding, "padding");
2078 get_padding_border(dict, wp->w_popup_border, "border");
2079 get_borderhighlight(dict, wp);
2080 get_borderchars(dict, wp);
2081 get_moved_list(dict, wp);
2082
2083 if (wp->w_filter_cb.cb_name != NULL)
2084 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2085 if (wp->w_close_cb.cb_name != NULL)
2086 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002087
2088 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2089 ++i)
2090 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2091 {
2092 dict_add_string(dict, "pos",
2093 (char_u *)poppos_entries[i].pp_name);
2094 break;
2095 }
2096
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002097 dict_add_string(dict, "close", (char_u *)(
2098 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2099 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2100
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002101# if defined(FEAT_TIMERS)
2102 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2103 ? (long)wp->w_popup_timer->tr_interval : 0L);
2104# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002105 }
2106}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002107
2108 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002109error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002110{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002111 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002112 {
2113 emsg(_("E994: Not allowed in a popup window"));
2114 return TRUE;
2115 }
2116 return FALSE;
2117}
2118
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002119/*
2120 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002121 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002122 */
2123 void
2124popup_reset_handled()
2125{
2126 win_T *wp;
2127
2128 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2129 wp->w_popup_flags &= ~POPF_HANDLED;
2130 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2131 wp->w_popup_flags &= ~POPF_HANDLED;
2132}
2133
2134/*
2135 * Find the next visible popup where POPF_HANDLED is not set.
2136 * Must have called popup_reset_handled() first.
2137 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2138 * popup with the highest zindex.
2139 */
2140 win_T *
2141find_next_popup(int lowest)
2142{
2143 win_T *wp;
2144 win_T *found_wp;
2145 int found_zindex;
2146
2147 found_zindex = lowest ? INT_MAX : 0;
2148 found_wp = NULL;
2149 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2150 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2151 && (lowest ? wp->w_zindex < found_zindex
2152 : wp->w_zindex > found_zindex))
2153 {
2154 found_zindex = wp->w_zindex;
2155 found_wp = wp;
2156 }
2157 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2158 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2159 && (lowest ? wp->w_zindex < found_zindex
2160 : wp->w_zindex > found_zindex))
2161 {
2162 found_zindex = wp->w_zindex;
2163 found_wp = wp;
2164 }
2165
2166 if (found_wp != NULL)
2167 found_wp->w_popup_flags |= POPF_HANDLED;
2168 return found_wp;
2169}
2170
2171/*
2172 * Invoke the filter callback for window "wp" with typed character "c".
2173 * Uses the global "mod_mask" for modifiers.
2174 * Returns the return value of the filter.
2175 * Careful: The filter may make "wp" invalid!
2176 */
2177 static int
2178invoke_popup_filter(win_T *wp, int c)
2179{
2180 int res;
2181 typval_T rettv;
2182 int dummy;
2183 typval_T argv[3];
2184 char_u buf[NUMBUFLEN];
2185
Bram Moolenaara42d9452019-06-15 21:46:30 +02002186 // Emergency exit: CTRL-C closes the popup.
2187 if (c == Ctrl_C)
2188 {
2189 rettv.v_type = VAR_NUMBER;
2190 rettv.vval.v_number = -1;
2191 popup_close_and_callback(wp, &rettv);
2192 return 1;
2193 }
2194
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002195 argv[0].v_type = VAR_NUMBER;
2196 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2197
2198 // Convert the number to a string, so that the function can use:
2199 // if a:c == "\<F2>"
2200 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2201 argv[1].v_type = VAR_STRING;
2202 argv[1].vval.v_string = vim_strsave(buf);
2203
2204 argv[2].v_type = VAR_UNKNOWN;
2205
Bram Moolenaara42d9452019-06-15 21:46:30 +02002206 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002207 call_callback(&wp->w_filter_cb, -1,
2208 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2209 res = tv_get_number(&rettv);
2210 vim_free(argv[1].vval.v_string);
2211 clear_tv(&rettv);
2212 return res;
2213}
2214
2215/*
2216 * Called when "c" was typed: invoke popup filter callbacks.
2217 * Returns TRUE when the character was consumed,
2218 */
2219 int
2220popup_do_filter(int c)
2221{
2222 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002223 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002224
2225 popup_reset_handled();
2226
2227 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2228 if (wp->w_filter_cb.cb_name != NULL)
2229 res = invoke_popup_filter(wp, c);
2230
2231 return res;
2232}
2233
Bram Moolenaar3397f742019-06-02 18:40:06 +02002234/*
2235 * Called when the cursor moved: check if any popup needs to be closed if the
2236 * cursor moved far enough.
2237 */
2238 void
2239popup_check_cursor_pos()
2240{
2241 win_T *wp;
2242 typval_T tv;
2243
2244 popup_reset_handled();
2245 while ((wp = find_next_popup(TRUE)) != NULL)
2246 if (wp->w_popup_curwin != NULL
2247 && (curwin != wp->w_popup_curwin
2248 || curwin->w_cursor.lnum != wp->w_popup_lnum
2249 || curwin->w_cursor.col < wp->w_popup_mincol
2250 || curwin->w_cursor.col > wp->w_popup_maxcol))
2251 {
2252 tv.v_type = VAR_NUMBER;
2253 tv.vval.v_number = -1;
2254 popup_close_and_callback(wp, &tv);
2255 }
2256}
2257
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002258/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002259 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2260 * "col" and "line" are screen coordinates.
2261 */
2262 static int
2263popup_masked(win_T *wp, int screencol, int screenline)
2264{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002265 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002266 int line = screenline - wp->w_winrow + 1;
2267 listitem_T *lio, *li;
2268 int width, height;
2269
2270 if (wp->w_popup_mask == NULL)
2271 return FALSE;
2272 width = popup_width(wp);
2273 height = popup_height(wp);
2274
2275 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2276 {
2277 int cols, cole;
2278 int lines, linee;
2279
2280 li = lio->li_tv.vval.v_list->lv_first;
2281 cols = tv_get_number(&li->li_tv);
2282 if (cols < 0)
2283 cols = width + cols + 1;
2284 if (col < cols)
2285 continue;
2286 li = li->li_next;
2287 cole = tv_get_number(&li->li_tv);
2288 if (cole < 0)
2289 cole = width + cole + 1;
2290 if (col > cole)
2291 continue;
2292 li = li->li_next;
2293 lines = tv_get_number(&li->li_tv);
2294 if (lines < 0)
2295 lines = height + lines + 1;
2296 if (line < lines)
2297 continue;
2298 li = li->li_next;
2299 linee = tv_get_number(&li->li_tv);
2300 if (linee < 0)
2301 linee = height + linee + 1;
2302 if (line > linee)
2303 continue;
2304 return TRUE;
2305 }
2306 return FALSE;
2307}
2308
2309/*
2310 * Set flags in popup_transparent[] for window "wp" to "val".
2311 */
2312 static void
2313update_popup_transparent(win_T *wp, int val)
2314{
2315 if (wp->w_popup_mask != NULL)
2316 {
2317 int width = popup_width(wp);
2318 int height = popup_height(wp);
2319 listitem_T *lio, *li;
2320 int cols, cole;
2321 int lines, linee;
2322 int col, line;
2323
2324 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2325 {
2326 li = lio->li_tv.vval.v_list->lv_first;
2327 cols = tv_get_number(&li->li_tv);
2328 if (cols < 0)
2329 cols = width + cols + 1;
2330 li = li->li_next;
2331 cole = tv_get_number(&li->li_tv);
2332 if (cole < 0)
2333 cole = width + cole + 1;
2334 li = li->li_next;
2335 lines = tv_get_number(&li->li_tv);
2336 if (lines < 0)
2337 lines = height + lines + 1;
2338 li = li->li_next;
2339 linee = tv_get_number(&li->li_tv);
2340 if (linee < 0)
2341 linee = height + linee + 1;
2342
2343 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002344 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002345 if (cols < 0)
2346 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002347 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002348 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002349 if (lines < 0)
2350 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002351 for (line = lines; line < linee
2352 && line + wp->w_winrow < screen_Rows; ++line)
2353 for (col = cols; col < cole
2354 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002355 popup_transparent[(line + wp->w_winrow) * screen_Columns
2356 + col + wp->w_wincol] = val;
2357 }
2358 }
2359}
2360
2361/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002362 * Update "popup_mask" if needed.
2363 * Also recomputes the popup size and positions.
2364 * Also updates "popup_visible".
2365 * Also marks window lines for redrawing.
2366 */
2367 void
2368may_update_popup_mask(int type)
2369{
2370 win_T *wp;
2371 short *mask;
2372 int line, col;
2373 int redraw_all = FALSE;
2374
2375 // Need to recompute when switching tabs.
2376 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2377 // (such as the screen size) must have changed.
2378 if (popup_mask_tab != curtab || type >= NOT_VALID)
2379 {
2380 popup_mask_refresh = TRUE;
2381 redraw_all = TRUE;
2382 }
2383 if (!popup_mask_refresh)
2384 {
2385 // Check if any buffer has changed.
2386 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2387 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2388 popup_mask_refresh = TRUE;
2389 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2390 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2391 popup_mask_refresh = TRUE;
2392 if (!popup_mask_refresh)
2393 return;
2394 }
2395
2396 // Need to update the mask, something has changed.
2397 popup_mask_refresh = FALSE;
2398 popup_mask_tab = curtab;
2399 popup_visible = FALSE;
2400
2401 // If redrawing everything, just update "popup_mask".
2402 // If redrawing only what is needed, update "popup_mask_next" and then
2403 // compare with "popup_mask" to see what changed.
2404 if (type >= SOME_VALID)
2405 mask = popup_mask;
2406 else
2407 mask = popup_mask_next;
2408 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2409
2410 // Find the window with the lowest zindex that hasn't been handled yet,
2411 // so that the window with a higher zindex overwrites the value in
2412 // popup_mask.
2413 popup_reset_handled();
2414 while ((wp = find_next_popup(TRUE)) != NULL)
2415 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002416 int height;
2417 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002418
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002419 popup_visible = TRUE;
2420
2421 // Recompute the position if the text changed.
2422 if (redraw_all
2423 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2424 popup_adjust_position(wp);
2425
Bram Moolenaard529ba52019-07-02 23:13:53 +02002426 height = popup_height(wp);
2427 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002428 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002429 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002430 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002431 col < wp->w_wincol + width && col < screen_Columns; ++col)
2432 if (!popup_masked(wp, col, line))
2433 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002434 }
2435
2436 // Only check which lines are to be updated if not already
2437 // updating all lines.
2438 if (mask == popup_mask_next)
2439 for (line = 0; line < screen_Rows; ++line)
2440 {
2441 int col_done = 0;
2442
2443 for (col = 0; col < screen_Columns; ++col)
2444 {
2445 int off = line * screen_Columns + col;
2446
2447 if (popup_mask[off] != popup_mask_next[off])
2448 {
2449 popup_mask[off] = popup_mask_next[off];
2450
2451 if (line >= cmdline_row)
2452 {
2453 // the command line needs to be cleared if text below
2454 // the popup is now visible.
2455 if (!msg_scrolled && popup_mask_next[off] == 0)
2456 clear_cmdline = TRUE;
2457 }
2458 else if (col >= col_done)
2459 {
2460 linenr_T lnum;
2461 int line_cp = line;
2462 int col_cp = col;
2463
2464 // The screen position "line" / "col" needs to be
2465 // redrawn. Figure out what window that is and update
2466 // w_redraw_top and w_redr_bot. Only needs to be done
2467 // once for each window line.
2468 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2469 if (wp != NULL)
2470 {
2471 if (line_cp >= wp->w_height)
2472 // In (or below) status line
2473 wp->w_redr_status = TRUE;
2474 // compute the position in the buffer line from the
2475 // position on the screen
2476 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2477 &lnum))
2478 // past bottom
2479 wp->w_redr_status = TRUE;
2480 else
2481 redrawWinline(wp, lnum);
2482
2483 // This line is going to be redrawn, no need to
2484 // check until the right side of the window.
2485 col_done = wp->w_wincol + wp->w_width - 1;
2486 }
2487 }
2488 }
2489 }
2490 }
2491}
2492
2493/*
2494 * Return a string of "len" spaces in IObuff.
2495 */
2496 static char_u *
2497get_spaces(int len)
2498{
2499 vim_memset(IObuff, ' ', (size_t)len);
2500 IObuff[len] = NUL;
2501 return IObuff;
2502}
2503
2504/*
2505 * Update popup windows. They are drawn on top of normal windows.
2506 * "win_update" is called for each popup window, lowest zindex first.
2507 */
2508 void
2509update_popups(void (*win_update)(win_T *wp))
2510{
2511 win_T *wp;
2512 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002513 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002514 int total_width;
2515 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002516 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002517 int popup_attr;
2518 int border_attr[4];
2519 int border_char[8];
2520 char_u buf[MB_MAXBYTES];
2521 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002522 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002523 int padcol = 0;
2524 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002525 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002526 int sb_thumb_top = 0;
2527 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002528 int attr_scroll = 0;
2529 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002530
2531 // Find the window with the lowest zindex that hasn't been updated yet,
2532 // so that the window with a higher zindex is drawn later, thus goes on
2533 // top.
2534 popup_reset_handled();
2535 while ((wp = find_next_popup(TRUE)) != NULL)
2536 {
2537 // This drawing uses the zindex of the popup window, so that it's on
2538 // top of the text but doesn't draw when another popup with higher
2539 // zindex is on top of the character.
2540 screen_zindex = wp->w_zindex;
2541
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002542 // Set flags in popup_transparent[] for masked cells.
2543 update_popup_transparent(wp, 1);
2544
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002545 // adjust w_winrow and w_wincol for border and padding, since
2546 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002547 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002548 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2549 - wp->w_popup_leftoff;
2550 if (wp->w_wincol + left_extra < 0)
2551 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002552 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002553 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002554
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002555 // Draw the popup text, unless it's off screen.
2556 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2557 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002558
2559 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002560 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002561
Bram Moolenaard529ba52019-07-02 23:13:53 +02002562 total_width = popup_width(wp);
2563 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002564 popup_attr = get_wcr_attr(wp);
2565
2566 // We can only use these line drawing characters when 'encoding' is
2567 // "utf-8" and 'ambiwidth' is "single".
2568 if (enc_utf8 && *p_ambw == 's')
2569 {
2570 border_char[0] = border_char[2] = 0x2550;
2571 border_char[1] = border_char[3] = 0x2551;
2572 border_char[4] = 0x2554;
2573 border_char[5] = 0x2557;
2574 border_char[6] = 0x255d;
2575 border_char[7] = 0x255a;
2576 }
2577 else
2578 {
2579 border_char[0] = border_char[2] = '-';
2580 border_char[1] = border_char[3] = '|';
2581 for (i = 4; i < 8; ++i)
2582 border_char[i] = '+';
2583 }
2584 for (i = 0; i < 8; ++i)
2585 if (wp->w_border_char[i] != 0)
2586 border_char[i] = wp->w_border_char[i];
2587
2588 for (i = 0; i < 4; ++i)
2589 {
2590 border_attr[i] = popup_attr;
2591 if (wp->w_border_highlight[i] != NULL)
2592 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2593 }
2594
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002595 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002596 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002597 if (wp->w_popup_border[0] > 0)
2598 {
2599 // top border
2600 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002601 wincol < 0 ? 0 : wincol, wincol + total_width,
2602 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002603 ? border_char[4] : border_char[0],
2604 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002605 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002606 {
2607 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2608 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002609 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002610 }
2611 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002612 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2613 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002614
Bram Moolenaard529ba52019-07-02 23:13:53 +02002615 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2616 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002617 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002618 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2619 - wp->w_has_scrollbar;
2620 if (padcol < 0)
2621 {
2622 padwidth += padcol;
2623 padcol = 0;
2624 }
2625 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002626 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002627 {
2628 // top padding
2629 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002630 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002631 ' ', ' ', popup_attr);
2632 }
2633
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002634 // Title goes on top of border or padding.
2635 if (wp->w_popup_title != NULL)
2636 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2637 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2638
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002639 // Compute scrollbar thumb position and size.
2640 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002641 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002642 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2643
Bram Moolenaar68acb412019-06-26 03:40:36 +02002644 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2645 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002646 if (sb_thumb_height == 0)
2647 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002648 if (linecount <= wp->w_height)
2649 // it just fits, avoid divide by zero
2650 sb_thumb_top = 0;
2651 else
2652 sb_thumb_top = (wp->w_topline - 1
2653 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002654 * (wp->w_height - sb_thumb_height)
2655 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002656 if (wp->w_scrollbar_highlight != NULL)
2657 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2658 else
2659 attr_scroll = highlight_attr[HLF_PSB];
2660 if (wp->w_thumb_highlight != NULL)
2661 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2662 else
2663 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002664 }
2665
2666 for (i = wp->w_popup_border[0];
2667 i < total_height - wp->w_popup_border[2]; ++i)
2668 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002669 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002670 // left and right padding only needed next to the body
2671 int do_padding =
2672 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2673 && i < total_height - wp->w_popup_border[2]
2674 - wp->w_popup_padding[2];
2675
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002676 row = wp->w_winrow + i;
2677
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002678 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002679 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002680 {
2681 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002682 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002683 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002684 if (do_padding && wp->w_popup_padding[3] > 0)
2685 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002686 int col = wincol + wp->w_popup_border[3];
2687
Bram Moolenaard529ba52019-07-02 23:13:53 +02002688 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002689 pad_left = wp->w_popup_padding[3];
2690 if (col < 0)
2691 {
2692 pad_left += col;
2693 col = 0;
2694 }
2695 if (pad_left > 0)
2696 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2697 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002698 // scrollbar
2699 if (wp->w_has_scrollbar)
2700 {
2701 int line = i - top_off;
2702 int scroll_col = wp->w_wincol + total_width - 1
2703 - wp->w_popup_border[1];
2704
2705 if (line >= 0 && line < wp->w_height)
2706 screen_putchar(' ', row, scroll_col,
2707 line >= sb_thumb_top
2708 && line < sb_thumb_top + sb_thumb_height
2709 ? attr_thumb : attr_scroll);
2710 else
2711 screen_putchar(' ', row, scroll_col, popup_attr);
2712 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002713 // right border
2714 if (wp->w_popup_border[1] > 0)
2715 {
2716 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002717 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002718 }
2719 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002720 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002721 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002722 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002723 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2724 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002725 }
2726
2727 if (wp->w_popup_padding[2] > 0)
2728 {
2729 // bottom padding
2730 row = wp->w_winrow + wp->w_popup_border[0]
2731 + wp->w_popup_padding[0] + wp->w_height;
2732 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002733 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002734 }
2735
2736 if (wp->w_popup_border[2] > 0)
2737 {
2738 // bottom border
2739 row = wp->w_winrow + total_height - 1;
2740 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002741 wincol < 0 ? 0 : wincol,
2742 wincol + total_width,
2743 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002744 ? border_char[7] : border_char[2],
2745 border_char[2], border_attr[2]);
2746 if (wp->w_popup_border[1] > 0)
2747 {
2748 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002749 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002750 }
2751 }
2752
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002753 if (wp->w_popup_close == POPCLOSE_BUTTON)
2754 {
2755 // close button goes on top of anything at the top-right corner
2756 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002757 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002758 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2759 }
2760
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002761 update_popup_transparent(wp, 0);
2762
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002763 // Back to the normal zindex.
2764 screen_zindex = 0;
2765 }
2766}
2767
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002768/*
2769 * Mark references in callbacks of one popup window.
2770 */
2771 static int
2772set_ref_in_one_popup(win_T *wp, int copyID)
2773{
2774 int abort = FALSE;
2775 typval_T tv;
2776
2777 if (wp->w_close_cb.cb_partial != NULL)
2778 {
2779 tv.v_type = VAR_PARTIAL;
2780 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2781 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2782 }
2783 if (wp->w_filter_cb.cb_partial != NULL)
2784 {
2785 tv.v_type = VAR_PARTIAL;
2786 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2787 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2788 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002789 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002790 return abort;
2791}
2792
2793/*
2794 * Set reference in callbacks of popup windows.
2795 */
2796 int
2797set_ref_in_popups(int copyID)
2798{
2799 int abort = FALSE;
2800 win_T *wp;
2801 tabpage_T *tp;
2802
2803 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2804 abort = abort || set_ref_in_one_popup(wp, copyID);
2805
2806 FOR_ALL_TABPAGES(tp)
2807 {
2808 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2809 abort = abort || set_ref_in_one_popup(wp, copyID);
2810 if (abort)
2811 break;
2812 }
2813 return abort;
2814}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002815#endif // FEAT_TEXT_PROP