blob: aa19a5d63d6cba94849d601fb302a19be3100758 [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 {
590 int ok = TRUE;
591
592 if (di->di_tv.v_type != VAR_LIST)
593 ok = FALSE;
594 else if (di->di_tv.vval.v_list != NULL)
595 {
596 listitem_T *li;
597
598 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
599 li = li->li_next)
600 {
601 if (li->li_tv.v_type != VAR_LIST
602 || li->li_tv.vval.v_list == NULL
603 || li->li_tv.vval.v_list->lv_len != 4)
604 {
605 ok = FALSE;
606 break;
607 }
608 }
609 }
610 if (ok)
611 {
612 wp->w_popup_mask = di->di_tv.vval.v_list;
613 ++wp->w_popup_mask->lv_refcount;
614 }
615 else
616 semsg(_(e_invargval), "mask");
617 }
618
Bram Moolenaarae943152019-06-16 22:54:14 +0200619#if defined(FEAT_TIMERS)
620 // Add timer to close the popup after some time.
621 nr = dict_get_number(dict, (char_u *)"time");
622 if (nr > 0)
623 popup_add_timeout(wp, nr);
624#endif
625
Bram Moolenaar3397f742019-06-02 18:40:06 +0200626 di = dict_find(dict, (char_u *)"moved", -1);
627 if (di != NULL)
628 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200629 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200630 handle_moved_argument(wp, di, FALSE);
631 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200632
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200633 di = dict_find(dict, (char_u *)"mousemoved", -1);
634 if (di != NULL)
635 {
636 set_mousemoved_values(wp);
637 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200638 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200639
Bram Moolenaarae943152019-06-16 22:54:14 +0200640 di = dict_find(dict, (char_u *)"filter", -1);
641 if (di != NULL)
642 {
643 callback_T callback = get_callback(&di->di_tv);
644
645 if (callback.cb_name != NULL)
646 {
647 free_callback(&wp->w_filter_cb);
648 set_callback(&wp->w_filter_cb, &callback);
649 }
650 }
651
652 di = dict_find(dict, (char_u *)"callback", -1);
653 if (di != NULL)
654 {
655 callback_T callback = get_callback(&di->di_tv);
656
657 if (callback.cb_name != NULL)
658 {
659 free_callback(&wp->w_close_cb);
660 set_callback(&wp->w_close_cb, &callback);
661 }
662 }
663}
664
665/*
666 * Go through the options in "dict" and apply them to popup window "wp".
667 */
668 static void
669apply_options(win_T *wp, dict_T *dict)
670{
671 int nr;
672
673 apply_move_options(wp, dict);
674 apply_general_options(wp, dict);
675
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200676 nr = dict_get_number(dict, (char_u *)"hidden");
677 if (nr > 0)
678 {
679 wp->w_popup_flags |= POPF_HIDDEN;
680 --wp->w_buffer->b_nwindows;
681 }
682
Bram Moolenaar33796b32019-06-08 16:01:13 +0200683 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200684}
685
686/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200687 * Add lines to the popup from a list of strings.
688 */
689 static void
690add_popup_strings(buf_T *buf, list_T *l)
691{
692 listitem_T *li;
693 linenr_T lnum = 0;
694 char_u *p;
695
696 for (li = l->lv_first; li != NULL; li = li->li_next)
697 if (li->li_tv.v_type == VAR_STRING)
698 {
699 p = li->li_tv.vval.v_string;
700 ml_append_buf(buf, lnum++,
701 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
702 }
703}
704
705/*
706 * Add lines to the popup from a list of dictionaries.
707 */
708 static void
709add_popup_dicts(buf_T *buf, list_T *l)
710{
711 listitem_T *li;
712 listitem_T *pli;
713 linenr_T lnum = 0;
714 char_u *p;
715 dict_T *dict;
716
717 // first add the text lines
718 for (li = l->lv_first; li != NULL; li = li->li_next)
719 {
720 if (li->li_tv.v_type != VAR_DICT)
721 {
722 emsg(_(e_dictreq));
723 return;
724 }
725 dict = li->li_tv.vval.v_dict;
726 p = dict == NULL ? NULL
727 : dict_get_string(dict, (char_u *)"text", FALSE);
728 ml_append_buf(buf, lnum++,
729 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
730 }
731
732 // add the text properties
733 lnum = 1;
734 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
735 {
736 dictitem_T *di;
737 list_T *plist;
738
739 dict = li->li_tv.vval.v_dict;
740 di = dict_find(dict, (char_u *)"props", -1);
741 if (di != NULL)
742 {
743 if (di->di_tv.v_type != VAR_LIST)
744 {
745 emsg(_(e_listreq));
746 return;
747 }
748 plist = di->di_tv.vval.v_list;
749 if (plist != NULL)
750 {
751 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
752 {
753 if (pli->li_tv.v_type != VAR_DICT)
754 {
755 emsg(_(e_dictreq));
756 return;
757 }
758 dict = pli->li_tv.vval.v_dict;
759 if (dict != NULL)
760 {
761 int col = dict_get_number(dict, (char_u *)"col");
762
763 prop_add_common( lnum, col, dict, buf, NULL);
764 }
765 }
766 }
767 }
768 }
769}
770
771/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200772 * Get the padding plus border at the top, adjusted to 1 if there is a title.
773 */
774 static int
775popup_top_extra(win_T *wp)
776{
777 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
778
779 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
780 return 1;
781 return extra;
782}
783
784/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200785 * Return the height of popup window "wp", including border and padding.
786 */
787 int
788popup_height(win_T *wp)
789{
790 return wp->w_height
791 + popup_top_extra(wp)
792 + wp->w_popup_padding[2] + wp->w_popup_border[2];
793}
794
795/*
796 * Return the width of popup window "wp", including border, padding and
797 * scrollbar.
798 */
799 int
800popup_width(win_T *wp)
801{
802 return wp->w_width + wp->w_leftcol
803 + wp->w_popup_padding[3] + wp->w_popup_border[3]
804 + wp->w_popup_padding[1] + wp->w_popup_border[1]
805 + wp->w_has_scrollbar
806 + wp->w_popup_rightoff;
807}
808
809/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200810 * Adjust the position and size of the popup to fit on the screen.
811 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200812 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200813popup_adjust_position(win_T *wp)
814{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200815 linenr_T lnum;
816 int wrapped = 0;
817 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200818 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200819 int center_vert = FALSE;
820 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200821 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200822 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200823 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
824 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
825 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
826 int extra_height = top_extra + bot_extra;
827 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200828 int org_winrow = wp->w_winrow;
829 int org_wincol = wp->w_wincol;
830 int org_width = wp->w_width;
831 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200832 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200833 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200834 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200835
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200836 wp->w_winrow = 0;
837 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200838 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200839 wp->w_popup_leftoff = 0;
840 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200841 if (wp->w_popup_pos == POPPOS_CENTER)
842 {
843 // center after computing the size
844 center_vert = TRUE;
845 center_hor = TRUE;
846 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200847 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200848 {
849 if (wp->w_wantline == 0)
850 center_vert = TRUE;
851 else if (wp->w_popup_pos == POPPOS_TOPLEFT
852 || wp->w_popup_pos == POPPOS_TOPRIGHT)
853 {
854 wp->w_winrow = wp->w_wantline - 1;
855 if (wp->w_winrow >= Rows)
856 wp->w_winrow = Rows - 1;
857 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200858
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200859 if (wp->w_wantcol == 0)
860 center_hor = TRUE;
861 else if (wp->w_popup_pos == POPPOS_TOPLEFT
862 || wp->w_popup_pos == POPPOS_BOTLEFT)
863 {
864 wp->w_wincol = wp->w_wantcol - 1;
865 if (wp->w_wincol >= Columns - 3)
866 wp->w_wincol = Columns - 3;
867 }
868 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200869
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200870 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200871 // When left aligned use the space available, but shift to the left when we
872 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200873 maxspace = Columns - wp->w_wincol - left_extra;
874 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200875 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200876 {
877 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200878 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200879 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200880
Bram Moolenaar8d241042019-06-12 23:40:01 +0200881 // start at the desired first line
882 wp->w_topline = wp->w_firstline;
883 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
884 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
885
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200886 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200887 // Use a minimum width of one, so that something shows when there is no
888 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200889 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200890 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200891 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200892 {
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200893 // count Tabs for what they are worth
894 int len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
895 (colnr_T)MAXCOL);
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200896
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200897 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200898 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200899 while (len > maxwidth)
900 {
901 ++wrapped;
902 len -= maxwidth;
903 wp->w_width = maxwidth;
904 }
905 }
906 else if (len > maxwidth
907 && allow_adjust_left
908 && (wp->w_popup_pos == POPPOS_TOPLEFT
909 || wp->w_popup_pos == POPPOS_BOTLEFT))
910 {
911 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200912 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200913
Bram Moolenaar51c31312019-06-15 22:27:23 +0200914 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200915 {
916 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200917
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200918 len -= truncate_shift;
919 shift_by -= truncate_shift;
920 }
921
922 wp->w_wincol -= shift_by;
923 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200924 wp->w_width = maxwidth;
925 }
926 if (wp->w_width < len)
927 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200928 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200929 if (wp->w_maxheight > 0
930 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200931 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200932 }
933
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200934 wp->w_has_scrollbar = wp->w_want_scrollbar
935 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
936
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200937 minwidth = wp->w_minwidth;
938 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
939 {
940 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
941
942 if (minwidth < title_len)
943 minwidth = title_len;
944 }
945
946 if (minwidth > 0 && wp->w_width < minwidth)
947 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200948 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +0200949 {
950 if (wp->w_width > maxspace)
951 // some columns cut off on the right
952 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200953 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200954 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200955 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200956 {
957 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
958 if (wp->w_wincol < 0)
959 wp->w_wincol = 0;
960 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200961 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
962 || wp->w_popup_pos == POPPOS_TOPRIGHT)
963 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200964 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
965
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200966 // Right aligned: move to the right if needed.
967 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200968 if (leftoff >= 0)
969 wp->w_wincol = leftoff;
970 else if (wp->w_popup_fixed)
971 {
972 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +0200973 // columns when displaying the window, minus left border and
974 // padding.
975 if (-leftoff > left_extra)
976 wp->w_leftcol = -leftoff - left_extra;
977 wp->w_width -= wp->w_leftcol;
978 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200979 if (wp->w_width < 0)
980 wp->w_width = 0;
981 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200982 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200983
Bram Moolenaar8d241042019-06-12 23:40:01 +0200984 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
985 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200986 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
987 wp->w_height = wp->w_minheight;
988 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
989 wp->w_height = wp->w_maxheight;
990 if (wp->w_height > Rows - wp->w_winrow)
991 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200992
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200993 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200994 {
995 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
996 if (wp->w_winrow < 0)
997 wp->w_winrow = 0;
998 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200999 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1000 || wp->w_popup_pos == POPPOS_BOTLEFT)
1001 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001002 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001003 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001004 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001005 else
1006 // not enough space, make top aligned
1007 wp->w_winrow = wp->w_wantline + 1;
1008 }
1009
Bram Moolenaar17146962019-05-30 00:12:11 +02001010 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001011
1012 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001013 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001014 if (org_winrow != wp->w_winrow
1015 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001016 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001017 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001018 || org_width != wp->w_width
1019 || org_height != wp->w_height)
1020 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001021 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001022 popup_mask_refresh = TRUE;
1023 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001024}
1025
Bram Moolenaar17627312019-06-02 19:53:44 +02001026typedef enum
1027{
1028 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001029 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001030 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001031 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001032 TYPE_DIALOG,
1033 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +02001034} create_type_T;
1035
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001036/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001037 * Make "buf" empty and set the contents to "text".
1038 * Used by popup_create() and popup_settext().
1039 */
1040 static void
1041popup_set_buffer_text(buf_T *buf, typval_T text)
1042{
1043 int lnum;
1044
1045 // Clear the buffer, then replace the lines.
1046 curbuf = buf;
1047 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1048 ml_delete(lnum, FALSE);
1049 curbuf = curwin->w_buffer;
1050
1051 // Add text to the buffer.
1052 if (text.v_type == VAR_STRING)
1053 {
1054 // just a string
1055 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1056 }
1057 else
1058 {
1059 list_T *l = text.vval.v_list;
1060
1061 if (l->lv_len > 0)
1062 {
1063 if (l->lv_first->li_tv.v_type == VAR_STRING)
1064 // list of strings
1065 add_popup_strings(buf, l);
1066 else
1067 // list of dictionaries
1068 add_popup_dicts(buf, l);
1069 }
1070 }
1071
1072 // delete the line that was in the empty buffer
1073 curbuf = buf;
1074 ml_delete(buf->b_ml.ml_line_count, FALSE);
1075 curbuf = curwin->w_buffer;
1076}
1077
1078/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001079 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001080 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001081 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001082 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001083popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001084{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001085 win_T *wp;
1086 tabpage_T *tp = NULL;
1087 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001088 int new_buffer;
1089 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001090 dict_T *d;
1091 int nr;
1092 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001093
1094 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001095 if (argvars[0].v_type == VAR_NUMBER)
1096 {
1097 buf = buflist_findnr( argvars[0].vval.v_number);
1098 if (buf == NULL)
1099 {
1100 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1101 return NULL;
1102 }
1103 }
1104 else if (!(argvars[0].v_type == VAR_STRING
1105 && argvars[0].vval.v_string != NULL)
1106 && !(argvars[0].v_type == VAR_LIST
1107 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001108 {
1109 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001110 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001111 }
1112 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1113 {
1114 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001115 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001116 }
1117 d = argvars[1].vval.v_dict;
1118
Bram Moolenaara3fce622019-06-20 02:31:49 +02001119 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1120 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1121 else if (type == TYPE_NOTIFICATION)
1122 tabnr = -1; // notifications are global by default
1123 else
1124 tabnr = 0;
1125 if (tabnr > 0)
1126 {
1127 tp = find_tabpage(tabnr);
1128 if (tp == NULL)
1129 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001130 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001131 return NULL;
1132 }
1133 }
1134
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001135 // Create the window and buffer.
1136 wp = win_alloc_popup_win();
1137 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001138 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001139 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001140 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001141 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001142
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001143 if (buf != NULL)
1144 {
1145 // use existing buffer
1146 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001147 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001148 buffer_ensure_loaded(buf);
1149 }
1150 else
1151 {
1152 // create a new buffer associated with the popup
1153 new_buffer = TRUE;
1154 buf = buflist_new(NULL, NULL, (linenr_T)0,
1155 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1156 if (buf == NULL)
1157 return NULL;
1158 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001159
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001160 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001161
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001162 set_local_options_default(wp);
1163 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001164 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001165 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1166 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1167 buf->b_p_ul = -1; // no undo
1168 buf->b_p_swf = FALSE; // no swap file
1169 buf->b_p_bl = FALSE; // unlisted buffer
1170 buf->b_locked = TRUE;
1171 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001172
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001173 // Avoid that 'buftype' is reset when this buffer is entered.
1174 buf->b_p_initialized = TRUE;
1175 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001176
Bram Moolenaara3fce622019-06-20 02:31:49 +02001177 if (tp != NULL)
1178 {
1179 // popup on specified tab page
1180 wp->w_next = tp->tp_first_popupwin;
1181 tp->tp_first_popupwin = wp;
1182 }
1183 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001184 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001185 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001186 wp->w_next = curtab->tp_first_popupwin;
1187 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001188 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001189 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001190 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001191 win_T *prev = first_popupwin;
1192
1193 // Global popup: add at the end, so that it gets displayed on top of
1194 // older ones with the same zindex. Matters for notifications.
1195 if (first_popupwin == NULL)
1196 first_popupwin = wp;
1197 else
1198 {
1199 while (prev->w_next != NULL)
1200 prev = prev->w_next;
1201 prev->w_next = wp;
1202 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001203 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001204
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001205 if (new_buffer)
1206 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001207
Bram Moolenaar17627312019-06-02 19:53:44 +02001208 if (type == TYPE_ATCURSOR)
1209 {
1210 wp->w_popup_pos = POPPOS_BOTLEFT;
1211 setcursor_mayforce(TRUE);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001212 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
Bram Moolenaar17627312019-06-02 19:53:44 +02001213 if (wp->w_wantline == 0) // cursor in first line
1214 {
1215 wp->w_wantline = 2;
1216 wp->w_popup_pos = POPPOS_TOPLEFT;
1217 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001218 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar17627312019-06-02 19:53:44 +02001219 set_moved_values(wp);
1220 set_moved_columns(wp, FIND_STRING);
1221 }
1222
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001223 if (type == TYPE_BEVAL)
1224 {
1225 wp->w_popup_pos = POPPOS_BOTLEFT;
1226
1227 // by default use the mouse position
1228 wp->w_wantline = mouse_row;
1229 if (wp->w_wantline <= 0) // mouse on first line
1230 {
1231 wp->w_wantline = 2;
1232 wp->w_popup_pos = POPPOS_TOPLEFT;
1233 }
1234 wp->w_wantcol = mouse_col + 1;
1235 set_mousemoved_values(wp);
1236 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1237 }
1238
Bram Moolenaar33796b32019-06-08 16:01:13 +02001239 // set default values
1240 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001241 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001242
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001243 if (type == TYPE_NOTIFICATION)
1244 {
1245 win_T *twp, *nextwin;
1246 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001247
1248 // Try to not overlap with another global popup. Guess we need 3
1249 // more screen lines than buffer lines.
1250 wp->w_wantline = 1;
1251 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1252 {
1253 nextwin = twp->w_next;
1254 if (twp != wp
1255 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1256 && twp->w_winrow <= wp->w_wantline - 1 + height
1257 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1258 {
1259 // move to below this popup and restart the loop to check for
1260 // overlap with other popups
1261 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1262 nextwin = first_popupwin;
1263 }
1264 }
1265 if (wp->w_wantline + height > Rows)
1266 {
1267 // can't avoid overlap, put on top in the hope that message goes
1268 // away soon.
1269 wp->w_wantline = 1;
1270 }
1271
1272 wp->w_wantcol = 10;
1273 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001274 wp->w_minwidth = 20;
1275 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001276 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001277 for (i = 0; i < 4; ++i)
1278 wp->w_popup_border[i] = 1;
1279 wp->w_popup_padding[1] = 1;
1280 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001281
1282 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001283 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001284 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1285 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001286 }
1287
Bram Moolenaara730e552019-06-16 19:05:31 +02001288 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001289 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001290 wp->w_popup_pos = POPPOS_CENTER;
1291 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1292 wp->w_popup_drag = 1;
1293 for (i = 0; i < 4; ++i)
1294 {
1295 wp->w_popup_border[i] = 1;
1296 wp->w_popup_padding[i] = 1;
1297 }
1298 }
1299
Bram Moolenaara730e552019-06-16 19:05:31 +02001300 if (type == TYPE_MENU)
1301 {
1302 typval_T tv;
1303 callback_T callback;
1304
1305 tv.v_type = VAR_STRING;
1306 tv.vval.v_string = (char_u *)"popup_filter_menu";
1307 callback = get_callback(&tv);
1308 if (callback.cb_name != NULL)
1309 set_callback(&wp->w_filter_cb, &callback);
1310
1311 wp->w_p_wrap = 0;
1312 }
1313
Bram Moolenaarae943152019-06-16 22:54:14 +02001314 for (i = 0; i < 4; ++i)
1315 VIM_CLEAR(wp->w_border_highlight[i]);
1316 for (i = 0; i < 8; ++i)
1317 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001318 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001319 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001320
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001321 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001322 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001323
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001324#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001325 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1326 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001327#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001328
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001329 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001330
1331 wp->w_vsep_width = 0;
1332
1333 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001334 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001335
1336 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001337}
1338
1339/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001340 * popup_clear()
1341 */
1342 void
1343f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1344{
1345 close_all_popups();
1346}
1347
1348/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001349 * popup_create({text}, {options})
1350 */
1351 void
1352f_popup_create(typval_T *argvars, typval_T *rettv)
1353{
Bram Moolenaar17627312019-06-02 19:53:44 +02001354 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001355}
1356
1357/*
1358 * popup_atcursor({text}, {options})
1359 */
1360 void
1361f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1362{
Bram Moolenaar17627312019-06-02 19:53:44 +02001363 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001364}
1365
1366/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001367 * popup_beval({text}, {options})
1368 */
1369 void
1370f_popup_beval(typval_T *argvars, typval_T *rettv)
1371{
1372 popup_create(argvars, rettv, TYPE_BEVAL);
1373}
1374
1375/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001376 * Invoke the close callback for window "wp" with value "result".
1377 * Careful: The callback may make "wp" invalid!
1378 */
1379 static void
1380invoke_popup_callback(win_T *wp, typval_T *result)
1381{
1382 typval_T rettv;
1383 int dummy;
1384 typval_T argv[3];
1385
1386 argv[0].v_type = VAR_NUMBER;
1387 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1388
1389 if (result != NULL && result->v_type != VAR_UNKNOWN)
1390 copy_tv(result, &argv[1]);
1391 else
1392 {
1393 argv[1].v_type = VAR_NUMBER;
1394 argv[1].vval.v_number = 0;
1395 }
1396
1397 argv[2].v_type = VAR_UNKNOWN;
1398
1399 call_callback(&wp->w_close_cb, -1,
1400 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1401 if (result != NULL)
1402 clear_tv(&argv[1]);
1403 clear_tv(&rettv);
1404}
1405
1406/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001407 * Close popup "wp" and invoke any close callback for it.
1408 */
1409 static void
1410popup_close_and_callback(win_T *wp, typval_T *arg)
1411{
1412 int id = wp->w_id;
1413
1414 if (wp->w_close_cb.cb_name != NULL)
1415 // Careful: This may make "wp" invalid.
1416 invoke_popup_callback(wp, arg);
1417
1418 popup_close(id);
1419}
1420
1421/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001422 * Close popup "wp" because of a mouse click.
1423 */
1424 void
1425popup_close_for_mouse_click(win_T *wp)
1426{
1427 typval_T res;
1428
1429 res.v_type = VAR_NUMBER;
1430 res.vval.v_number = -2;
1431 popup_close_and_callback(wp, &res);
1432}
1433
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001434 static void
1435check_mouse_moved(win_T *wp, win_T *mouse_wp)
1436{
1437 // Close the popup when all if these are true:
1438 // - the mouse is not on this popup
1439 // - "mousemoved" was used
1440 // - the mouse is no longer on the same screen row or the mouse column is
1441 // outside of the relevant text
1442 if (wp != mouse_wp
1443 && wp->w_popup_mouse_row != 0
1444 && (wp->w_popup_mouse_row != mouse_row
1445 || mouse_col < wp->w_popup_mouse_mincol
1446 || mouse_col > wp->w_popup_mouse_maxcol))
1447 {
1448 typval_T res;
1449
1450 res.v_type = VAR_NUMBER;
1451 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001452 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001453 popup_close_and_callback(wp, &res);
1454 }
1455}
1456
1457/*
1458 * Called when the mouse moved: may close a popup with "mousemoved".
1459 */
1460 void
1461popup_handle_mouse_moved(void)
1462{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001463 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001464 win_T *mouse_wp;
1465 int row = mouse_row;
1466 int col = mouse_col;
1467
1468 // find the window where the mouse is in
1469 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1470
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001471 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1472 {
1473 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001474 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001475 }
1476 for (wp = curtab->tp_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 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001481}
1482
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001483/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001484 * In a filter: check if the typed key is a mouse event that is used for
1485 * dragging the popup.
1486 */
1487 static void
1488filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1489{
1490 int row = mouse_row;
1491 int col = mouse_col;
1492
1493 if (wp->w_popup_drag
1494 && is_mouse_key(c)
1495 && (wp == popup_dragwin
1496 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1497 // do not consume the key, allow for dragging the popup
1498 rettv->vval.v_number = 0;
1499}
1500
1501 static void
1502popup_highlight_curline(win_T *wp)
1503{
1504 int id;
1505 char buf[100];
1506
1507 match_delete(wp, 1, FALSE);
1508
1509 id = syn_name2id((char_u *)"PopupSelected");
1510 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1511 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1512 (char_u *)buf, 10, 1, NULL, NULL);
1513}
1514
1515/*
1516 * popup_filter_menu({text}, {options})
1517 */
1518 void
1519f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1520{
1521 int id = tv_get_number(&argvars[0]);
1522 win_T *wp = win_id2wp(id);
1523 char_u *key = tv_get_string(&argvars[1]);
1524 typval_T res;
1525 int c;
1526 linenr_T old_lnum;
1527
1528 // If the popup has been closed do not consume the key.
1529 if (wp == NULL)
1530 return;
1531
1532 c = *key;
1533 if (c == K_SPECIAL && key[1] != NUL)
1534 c = TO_SPECIAL(key[1], key[2]);
1535
1536 // consume all keys until done
1537 rettv->vval.v_number = 1;
1538 res.v_type = VAR_NUMBER;
1539
1540 old_lnum = wp->w_cursor.lnum;
1541 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1542 --wp->w_cursor.lnum;
1543 if ((c == 'j' || c == 'J' || c == K_DOWN)
1544 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1545 ++wp->w_cursor.lnum;
1546 if (old_lnum != wp->w_cursor.lnum)
1547 {
1548 popup_highlight_curline(wp);
1549 return;
1550 }
1551
1552 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1553 {
1554 // Cancelled, invoke callback with -1
1555 res.vval.v_number = -1;
1556 popup_close_and_callback(wp, &res);
1557 return;
1558 }
1559 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1560 {
1561 // Invoke callback with current index.
1562 res.vval.v_number = wp->w_cursor.lnum;
1563 popup_close_and_callback(wp, &res);
1564 return;
1565 }
1566
1567 filter_handle_drag(wp, c, rettv);
1568}
1569
1570/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001571 * popup_filter_yesno({text}, {options})
1572 */
1573 void
1574f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1575{
1576 int id = tv_get_number(&argvars[0]);
1577 win_T *wp = win_id2wp(id);
1578 char_u *key = tv_get_string(&argvars[1]);
1579 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001580 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001581
1582 // If the popup has been closed don't consume the key.
1583 if (wp == NULL)
1584 return;
1585
Bram Moolenaara730e552019-06-16 19:05:31 +02001586 c = *key;
1587 if (c == K_SPECIAL && key[1] != NUL)
1588 c = TO_SPECIAL(key[1], key[2]);
1589
Bram Moolenaara42d9452019-06-15 21:46:30 +02001590 // consume all keys until done
1591 rettv->vval.v_number = 1;
1592
Bram Moolenaara730e552019-06-16 19:05:31 +02001593 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001594 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001595 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001596 res.vval.v_number = 0;
1597 else
1598 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001599 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001600 return;
1601 }
1602
1603 // Invoke callback
1604 res.v_type = VAR_NUMBER;
1605 popup_close_and_callback(wp, &res);
1606}
1607
1608/*
1609 * popup_dialog({text}, {options})
1610 */
1611 void
1612f_popup_dialog(typval_T *argvars, typval_T *rettv)
1613{
1614 popup_create(argvars, rettv, TYPE_DIALOG);
1615}
1616
1617/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001618 * popup_menu({text}, {options})
1619 */
1620 void
1621f_popup_menu(typval_T *argvars, typval_T *rettv)
1622{
1623 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1624
1625 if (wp != NULL)
1626 popup_highlight_curline(wp);
1627}
1628
1629/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001630 * popup_notification({text}, {options})
1631 */
1632 void
1633f_popup_notification(typval_T *argvars, typval_T *rettv)
1634{
1635 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1636}
1637
1638/*
1639 * Find the popup window with window-ID "id".
1640 * If the popup window does not exist NULL is returned.
1641 * If the window is not a popup window, and error message is given.
1642 */
1643 static win_T *
1644find_popup_win(int id)
1645{
1646 win_T *wp = win_id2wp(id);
1647
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001648 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001649 {
1650 semsg(_("E993: window %d is not a popup window"), id);
1651 return NULL;
1652 }
1653 return wp;
1654}
1655
1656/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001657 * popup_close({id})
1658 */
1659 void
1660f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1661{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001662 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001663 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001664
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001665 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001666 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001667}
1668
1669/*
1670 * popup_hide({id})
1671 */
1672 void
1673f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1674{
1675 int id = (int)tv_get_number(argvars);
1676 win_T *wp = find_popup_win(id);
1677
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001678 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001679 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001680 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001681 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001682 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001683 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001684 }
1685}
1686
1687/*
1688 * popup_show({id})
1689 */
1690 void
1691f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1692{
1693 int id = (int)tv_get_number(argvars);
1694 win_T *wp = find_popup_win(id);
1695
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001696 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001697 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001698 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001699 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001700 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001701 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001702 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001703}
1704
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001705/*
1706 * popup_settext({id}, {text})
1707 */
1708 void
1709f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1710{
1711 int id = (int)tv_get_number(&argvars[0]);
1712 win_T *wp = find_popup_win(id);
1713
1714 if (wp != NULL)
1715 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001716 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1717 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1718 else
1719 {
1720 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1721 popup_adjust_position(wp);
1722 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001723 }
1724}
1725
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001726 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001727popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001728{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001729 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001730 if (wp->w_winrow + wp->w_height >= cmdline_row)
1731 clear_cmdline = TRUE;
1732 win_free_popup(wp);
1733 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001734 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001735}
1736
Bram Moolenaarec583842019-05-26 14:11:23 +02001737/*
1738 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001739 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001740 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001741 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001742popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001743{
1744 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001745 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001746 win_T *prev = NULL;
1747
Bram Moolenaarec583842019-05-26 14:11:23 +02001748 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001749 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001750 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001751 {
1752 if (prev == NULL)
1753 first_popupwin = wp->w_next;
1754 else
1755 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001756 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001757 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001758 }
1759
Bram Moolenaarec583842019-05-26 14:11:23 +02001760 // go through tab-local popups
1761 FOR_ALL_TABPAGES(tp)
1762 popup_close_tabpage(tp, id);
1763}
1764
1765/*
1766 * Close a popup window with Window-id "id" in tabpage "tp".
1767 */
1768 void
1769popup_close_tabpage(tabpage_T *tp, int id)
1770{
1771 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001772 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001773 win_T *prev = NULL;
1774
Bram Moolenaarec583842019-05-26 14:11:23 +02001775 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1776 if (wp->w_id == id)
1777 {
1778 if (prev == NULL)
1779 *root = wp->w_next;
1780 else
1781 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001782 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001783 return;
1784 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001785}
1786
1787 void
1788close_all_popups(void)
1789{
1790 while (first_popupwin != NULL)
1791 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001792 while (curtab->tp_first_popupwin != NULL)
1793 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001794}
1795
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001796/*
1797 * popup_move({id}, {options})
1798 */
1799 void
1800f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1801{
Bram Moolenaarae943152019-06-16 22:54:14 +02001802 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001803 int id = (int)tv_get_number(argvars);
1804 win_T *wp = find_popup_win(id);
1805
1806 if (wp == NULL)
1807 return; // invalid {id}
1808
1809 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1810 {
1811 emsg(_(e_dictreq));
1812 return;
1813 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001814 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001815
Bram Moolenaarae943152019-06-16 22:54:14 +02001816 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001817
1818 if (wp->w_winrow + wp->w_height >= cmdline_row)
1819 clear_cmdline = TRUE;
1820 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001821}
1822
Bram Moolenaarbc133542019-05-29 20:26:46 +02001823/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001824 * popup_setoptions({id}, {options})
1825 */
1826 void
1827f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1828{
1829 dict_T *dict;
1830 int id = (int)tv_get_number(argvars);
1831 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001832 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001833
1834 if (wp == NULL)
1835 return; // invalid {id}
1836
1837 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1838 {
1839 emsg(_(e_dictreq));
1840 return;
1841 }
1842 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001843 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001844
1845 apply_move_options(wp, dict);
1846 apply_general_options(wp, dict);
1847
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001848 if (old_firstline != wp->w_firstline)
1849 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001850 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001851 popup_adjust_position(wp);
1852}
1853
1854/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001855 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001856 */
1857 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001858f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001859{
1860 dict_T *dict;
1861 int id = (int)tv_get_number(argvars);
1862 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001863 int top_extra;
1864 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001865
1866 if (rettv_dict_alloc(rettv) == OK)
1867 {
1868 if (wp == NULL)
1869 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001870 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001871 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1872
Bram Moolenaarbc133542019-05-29 20:26:46 +02001873 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001874
Bram Moolenaarbc133542019-05-29 20:26:46 +02001875 dict_add_number(dict, "line", wp->w_winrow + 1);
1876 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001877 dict_add_number(dict, "width", wp->w_width + left_extra
1878 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1879 dict_add_number(dict, "height", wp->w_height + top_extra
1880 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001881
1882 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1883 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1884 dict_add_number(dict, "core_width", wp->w_width);
1885 dict_add_number(dict, "core_height", wp->w_height);
1886
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001887 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001888 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001889 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001890 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001891 }
1892}
1893
1894/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001895 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1896 */
1897 static void
1898get_padding_border(dict_T *dict, int *array, char *name)
1899{
1900 list_T *list;
1901 int i;
1902
1903 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1904 return;
1905
1906 list = list_alloc();
1907 if (list != NULL)
1908 {
1909 dict_add_list(dict, name, list);
1910 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1911 for (i = 0; i < 4; ++i)
1912 list_append_number(list, array[i]);
1913 }
1914}
1915
1916/*
1917 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1918 */
1919 static void
1920get_borderhighlight(dict_T *dict, win_T *wp)
1921{
1922 list_T *list;
1923 int i;
1924
1925 for (i = 0; i < 4; ++i)
1926 if (wp->w_border_highlight[i] != NULL)
1927 break;
1928 if (i == 4)
1929 return;
1930
1931 list = list_alloc();
1932 if (list != NULL)
1933 {
1934 dict_add_list(dict, "borderhighlight", list);
1935 for (i = 0; i < 4; ++i)
1936 list_append_string(list, wp->w_border_highlight[i], -1);
1937 }
1938}
1939
1940/*
1941 * For popup_getoptions(): add a "borderchars" entry to "dict".
1942 */
1943 static void
1944get_borderchars(dict_T *dict, win_T *wp)
1945{
1946 list_T *list;
1947 int i;
1948 char_u buf[NUMBUFLEN];
1949 int len;
1950
1951 for (i = 0; i < 8; ++i)
1952 if (wp->w_border_char[i] != 0)
1953 break;
1954 if (i == 8)
1955 return;
1956
1957 list = list_alloc();
1958 if (list != NULL)
1959 {
1960 dict_add_list(dict, "borderchars", list);
1961 for (i = 0; i < 8; ++i)
1962 {
1963 len = mb_char2bytes(wp->w_border_char[i], buf);
1964 list_append_string(list, buf, len);
1965 }
1966 }
1967}
1968
1969/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001970 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02001971 */
1972 static void
1973get_moved_list(dict_T *dict, win_T *wp)
1974{
1975 list_T *list;
1976
1977 list = list_alloc();
1978 if (list != NULL)
1979 {
1980 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001981 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02001982 list_append_number(list, wp->w_popup_mincol);
1983 list_append_number(list, wp->w_popup_maxcol);
1984 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001985 list = list_alloc();
1986 if (list != NULL)
1987 {
1988 dict_add_list(dict, "mousemoved", list);
1989 list_append_number(list, wp->w_popup_mouse_row);
1990 list_append_number(list, wp->w_popup_mouse_mincol);
1991 list_append_number(list, wp->w_popup_mouse_maxcol);
1992 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001993}
1994
1995/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001996 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001997 */
1998 void
1999f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2000{
2001 dict_T *dict;
2002 int id = (int)tv_get_number(argvars);
2003 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002004 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002005 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002006
2007 if (rettv_dict_alloc(rettv) == OK)
2008 {
2009 if (wp == NULL)
2010 return;
2011
2012 dict = rettv->vval.v_dict;
2013 dict_add_number(dict, "line", wp->w_wantline);
2014 dict_add_number(dict, "col", wp->w_wantcol);
2015 dict_add_number(dict, "minwidth", wp->w_minwidth);
2016 dict_add_number(dict, "minheight", wp->w_minheight);
2017 dict_add_number(dict, "maxheight", wp->w_maxheight);
2018 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002019 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002020 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002021 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002022 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002023 dict_add_string(dict, "title", wp->w_popup_title);
2024 dict_add_number(dict, "wrap", wp->w_p_wrap);
2025 dict_add_number(dict, "drag", wp->w_popup_drag);
2026 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002027 if (wp->w_scrollbar_highlight != NULL)
2028 dict_add_string(dict, "scrollbarhighlight",
2029 wp->w_scrollbar_highlight);
2030 if (wp->w_thumb_highlight != NULL)
2031 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002032
Bram Moolenaara3fce622019-06-20 02:31:49 +02002033 // find the tabpage that holds this popup
2034 i = 1;
2035 FOR_ALL_TABPAGES(tp)
2036 {
2037 win_T *p;
2038
2039 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2040 if (p->w_id == id)
2041 break;
2042 if (p != NULL)
2043 break;
2044 ++i;
2045 }
2046 if (tp == NULL)
2047 i = -1; // must be global
2048 else if (tp == curtab)
2049 i = 0;
2050 dict_add_number(dict, "tabpage", i);
2051
Bram Moolenaarae943152019-06-16 22:54:14 +02002052 get_padding_border(dict, wp->w_popup_padding, "padding");
2053 get_padding_border(dict, wp->w_popup_border, "border");
2054 get_borderhighlight(dict, wp);
2055 get_borderchars(dict, wp);
2056 get_moved_list(dict, wp);
2057
2058 if (wp->w_filter_cb.cb_name != NULL)
2059 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2060 if (wp->w_close_cb.cb_name != NULL)
2061 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002062
2063 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2064 ++i)
2065 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2066 {
2067 dict_add_string(dict, "pos",
2068 (char_u *)poppos_entries[i].pp_name);
2069 break;
2070 }
2071
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002072 dict_add_string(dict, "close", (char_u *)(
2073 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2074 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2075
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002076# if defined(FEAT_TIMERS)
2077 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2078 ? (long)wp->w_popup_timer->tr_interval : 0L);
2079# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002080 }
2081}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002082
2083 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002084error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002085{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002086 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002087 {
2088 emsg(_("E994: Not allowed in a popup window"));
2089 return TRUE;
2090 }
2091 return FALSE;
2092}
2093
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002094/*
2095 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002096 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002097 */
2098 void
2099popup_reset_handled()
2100{
2101 win_T *wp;
2102
2103 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2104 wp->w_popup_flags &= ~POPF_HANDLED;
2105 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2106 wp->w_popup_flags &= ~POPF_HANDLED;
2107}
2108
2109/*
2110 * Find the next visible popup where POPF_HANDLED is not set.
2111 * Must have called popup_reset_handled() first.
2112 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2113 * popup with the highest zindex.
2114 */
2115 win_T *
2116find_next_popup(int lowest)
2117{
2118 win_T *wp;
2119 win_T *found_wp;
2120 int found_zindex;
2121
2122 found_zindex = lowest ? INT_MAX : 0;
2123 found_wp = NULL;
2124 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2125 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2126 && (lowest ? wp->w_zindex < found_zindex
2127 : wp->w_zindex > found_zindex))
2128 {
2129 found_zindex = wp->w_zindex;
2130 found_wp = wp;
2131 }
2132 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2133 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2134 && (lowest ? wp->w_zindex < found_zindex
2135 : wp->w_zindex > found_zindex))
2136 {
2137 found_zindex = wp->w_zindex;
2138 found_wp = wp;
2139 }
2140
2141 if (found_wp != NULL)
2142 found_wp->w_popup_flags |= POPF_HANDLED;
2143 return found_wp;
2144}
2145
2146/*
2147 * Invoke the filter callback for window "wp" with typed character "c".
2148 * Uses the global "mod_mask" for modifiers.
2149 * Returns the return value of the filter.
2150 * Careful: The filter may make "wp" invalid!
2151 */
2152 static int
2153invoke_popup_filter(win_T *wp, int c)
2154{
2155 int res;
2156 typval_T rettv;
2157 int dummy;
2158 typval_T argv[3];
2159 char_u buf[NUMBUFLEN];
2160
Bram Moolenaara42d9452019-06-15 21:46:30 +02002161 // Emergency exit: CTRL-C closes the popup.
2162 if (c == Ctrl_C)
2163 {
2164 rettv.v_type = VAR_NUMBER;
2165 rettv.vval.v_number = -1;
2166 popup_close_and_callback(wp, &rettv);
2167 return 1;
2168 }
2169
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002170 argv[0].v_type = VAR_NUMBER;
2171 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2172
2173 // Convert the number to a string, so that the function can use:
2174 // if a:c == "\<F2>"
2175 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2176 argv[1].v_type = VAR_STRING;
2177 argv[1].vval.v_string = vim_strsave(buf);
2178
2179 argv[2].v_type = VAR_UNKNOWN;
2180
Bram Moolenaara42d9452019-06-15 21:46:30 +02002181 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002182 call_callback(&wp->w_filter_cb, -1,
2183 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
2184 res = tv_get_number(&rettv);
2185 vim_free(argv[1].vval.v_string);
2186 clear_tv(&rettv);
2187 return res;
2188}
2189
2190/*
2191 * Called when "c" was typed: invoke popup filter callbacks.
2192 * Returns TRUE when the character was consumed,
2193 */
2194 int
2195popup_do_filter(int c)
2196{
2197 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002198 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002199
2200 popup_reset_handled();
2201
2202 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2203 if (wp->w_filter_cb.cb_name != NULL)
2204 res = invoke_popup_filter(wp, c);
2205
2206 return res;
2207}
2208
Bram Moolenaar3397f742019-06-02 18:40:06 +02002209/*
2210 * Called when the cursor moved: check if any popup needs to be closed if the
2211 * cursor moved far enough.
2212 */
2213 void
2214popup_check_cursor_pos()
2215{
2216 win_T *wp;
2217 typval_T tv;
2218
2219 popup_reset_handled();
2220 while ((wp = find_next_popup(TRUE)) != NULL)
2221 if (wp->w_popup_curwin != NULL
2222 && (curwin != wp->w_popup_curwin
2223 || curwin->w_cursor.lnum != wp->w_popup_lnum
2224 || curwin->w_cursor.col < wp->w_popup_mincol
2225 || curwin->w_cursor.col > wp->w_popup_maxcol))
2226 {
2227 tv.v_type = VAR_NUMBER;
2228 tv.vval.v_number = -1;
2229 popup_close_and_callback(wp, &tv);
2230 }
2231}
2232
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002233/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002234 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2235 * "col" and "line" are screen coordinates.
2236 */
2237 static int
2238popup_masked(win_T *wp, int screencol, int screenline)
2239{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002240 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002241 int line = screenline - wp->w_winrow + 1;
2242 listitem_T *lio, *li;
2243 int width, height;
2244
2245 if (wp->w_popup_mask == NULL)
2246 return FALSE;
2247 width = popup_width(wp);
2248 height = popup_height(wp);
2249
2250 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2251 {
2252 int cols, cole;
2253 int lines, linee;
2254
2255 li = lio->li_tv.vval.v_list->lv_first;
2256 cols = tv_get_number(&li->li_tv);
2257 if (cols < 0)
2258 cols = width + cols + 1;
2259 if (col < cols)
2260 continue;
2261 li = li->li_next;
2262 cole = tv_get_number(&li->li_tv);
2263 if (cole < 0)
2264 cole = width + cole + 1;
2265 if (col > cole)
2266 continue;
2267 li = li->li_next;
2268 lines = tv_get_number(&li->li_tv);
2269 if (lines < 0)
2270 lines = height + lines + 1;
2271 if (line < lines)
2272 continue;
2273 li = li->li_next;
2274 linee = tv_get_number(&li->li_tv);
2275 if (linee < 0)
2276 linee = height + linee + 1;
2277 if (line > linee)
2278 continue;
2279 return TRUE;
2280 }
2281 return FALSE;
2282}
2283
2284/*
2285 * Set flags in popup_transparent[] for window "wp" to "val".
2286 */
2287 static void
2288update_popup_transparent(win_T *wp, int val)
2289{
2290 if (wp->w_popup_mask != NULL)
2291 {
2292 int width = popup_width(wp);
2293 int height = popup_height(wp);
2294 listitem_T *lio, *li;
2295 int cols, cole;
2296 int lines, linee;
2297 int col, line;
2298
2299 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2300 {
2301 li = lio->li_tv.vval.v_list->lv_first;
2302 cols = tv_get_number(&li->li_tv);
2303 if (cols < 0)
2304 cols = width + cols + 1;
2305 li = li->li_next;
2306 cole = tv_get_number(&li->li_tv);
2307 if (cole < 0)
2308 cole = width + cole + 1;
2309 li = li->li_next;
2310 lines = tv_get_number(&li->li_tv);
2311 if (lines < 0)
2312 lines = height + lines + 1;
2313 li = li->li_next;
2314 linee = tv_get_number(&li->li_tv);
2315 if (linee < 0)
2316 linee = height + linee + 1;
2317
2318 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002319 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002320 if (cols < 0)
2321 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002322 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002323 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002324 if (lines < 0)
2325 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002326 for (line = lines; line < linee
2327 && line + wp->w_winrow < screen_Rows; ++line)
2328 for (col = cols; col < cole
2329 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002330 popup_transparent[(line + wp->w_winrow) * screen_Columns
2331 + col + wp->w_wincol] = val;
2332 }
2333 }
2334}
2335
2336/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002337 * Update "popup_mask" if needed.
2338 * Also recomputes the popup size and positions.
2339 * Also updates "popup_visible".
2340 * Also marks window lines for redrawing.
2341 */
2342 void
2343may_update_popup_mask(int type)
2344{
2345 win_T *wp;
2346 short *mask;
2347 int line, col;
2348 int redraw_all = FALSE;
2349
2350 // Need to recompute when switching tabs.
2351 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2352 // (such as the screen size) must have changed.
2353 if (popup_mask_tab != curtab || type >= NOT_VALID)
2354 {
2355 popup_mask_refresh = TRUE;
2356 redraw_all = TRUE;
2357 }
2358 if (!popup_mask_refresh)
2359 {
2360 // Check if any buffer has changed.
2361 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2362 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2363 popup_mask_refresh = TRUE;
2364 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2365 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2366 popup_mask_refresh = TRUE;
2367 if (!popup_mask_refresh)
2368 return;
2369 }
2370
2371 // Need to update the mask, something has changed.
2372 popup_mask_refresh = FALSE;
2373 popup_mask_tab = curtab;
2374 popup_visible = FALSE;
2375
2376 // If redrawing everything, just update "popup_mask".
2377 // If redrawing only what is needed, update "popup_mask_next" and then
2378 // compare with "popup_mask" to see what changed.
2379 if (type >= SOME_VALID)
2380 mask = popup_mask;
2381 else
2382 mask = popup_mask_next;
2383 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2384
2385 // Find the window with the lowest zindex that hasn't been handled yet,
2386 // so that the window with a higher zindex overwrites the value in
2387 // popup_mask.
2388 popup_reset_handled();
2389 while ((wp = find_next_popup(TRUE)) != NULL)
2390 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002391 int height;
2392 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002393
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002394 popup_visible = TRUE;
2395
2396 // Recompute the position if the text changed.
2397 if (redraw_all
2398 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2399 popup_adjust_position(wp);
2400
Bram Moolenaard529ba52019-07-02 23:13:53 +02002401 height = popup_height(wp);
2402 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002403 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002404 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002405 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002406 col < wp->w_wincol + width && col < screen_Columns; ++col)
2407 if (!popup_masked(wp, col, line))
2408 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002409 }
2410
2411 // Only check which lines are to be updated if not already
2412 // updating all lines.
2413 if (mask == popup_mask_next)
2414 for (line = 0; line < screen_Rows; ++line)
2415 {
2416 int col_done = 0;
2417
2418 for (col = 0; col < screen_Columns; ++col)
2419 {
2420 int off = line * screen_Columns + col;
2421
2422 if (popup_mask[off] != popup_mask_next[off])
2423 {
2424 popup_mask[off] = popup_mask_next[off];
2425
2426 if (line >= cmdline_row)
2427 {
2428 // the command line needs to be cleared if text below
2429 // the popup is now visible.
2430 if (!msg_scrolled && popup_mask_next[off] == 0)
2431 clear_cmdline = TRUE;
2432 }
2433 else if (col >= col_done)
2434 {
2435 linenr_T lnum;
2436 int line_cp = line;
2437 int col_cp = col;
2438
2439 // The screen position "line" / "col" needs to be
2440 // redrawn. Figure out what window that is and update
2441 // w_redraw_top and w_redr_bot. Only needs to be done
2442 // once for each window line.
2443 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2444 if (wp != NULL)
2445 {
2446 if (line_cp >= wp->w_height)
2447 // In (or below) status line
2448 wp->w_redr_status = TRUE;
2449 // compute the position in the buffer line from the
2450 // position on the screen
2451 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2452 &lnum))
2453 // past bottom
2454 wp->w_redr_status = TRUE;
2455 else
2456 redrawWinline(wp, lnum);
2457
2458 // This line is going to be redrawn, no need to
2459 // check until the right side of the window.
2460 col_done = wp->w_wincol + wp->w_width - 1;
2461 }
2462 }
2463 }
2464 }
2465 }
2466}
2467
2468/*
2469 * Return a string of "len" spaces in IObuff.
2470 */
2471 static char_u *
2472get_spaces(int len)
2473{
2474 vim_memset(IObuff, ' ', (size_t)len);
2475 IObuff[len] = NUL;
2476 return IObuff;
2477}
2478
2479/*
2480 * Update popup windows. They are drawn on top of normal windows.
2481 * "win_update" is called for each popup window, lowest zindex first.
2482 */
2483 void
2484update_popups(void (*win_update)(win_T *wp))
2485{
2486 win_T *wp;
2487 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002488 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002489 int total_width;
2490 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002491 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002492 int popup_attr;
2493 int border_attr[4];
2494 int border_char[8];
2495 char_u buf[MB_MAXBYTES];
2496 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002497 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002498 int padcol = 0;
2499 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002500 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002501 int sb_thumb_top = 0;
2502 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002503 int attr_scroll = 0;
2504 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002505
2506 // Find the window with the lowest zindex that hasn't been updated yet,
2507 // so that the window with a higher zindex is drawn later, thus goes on
2508 // top.
2509 popup_reset_handled();
2510 while ((wp = find_next_popup(TRUE)) != NULL)
2511 {
2512 // This drawing uses the zindex of the popup window, so that it's on
2513 // top of the text but doesn't draw when another popup with higher
2514 // zindex is on top of the character.
2515 screen_zindex = wp->w_zindex;
2516
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002517 // Set flags in popup_transparent[] for masked cells.
2518 update_popup_transparent(wp, 1);
2519
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002520 // adjust w_winrow and w_wincol for border and padding, since
2521 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002522 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002523 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2524 - wp->w_popup_leftoff;
2525 if (wp->w_wincol + left_extra < 0)
2526 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002527 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002528 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002529
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002530 // Draw the popup text, unless it's off screen.
2531 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2532 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002533
2534 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002535 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002536
Bram Moolenaard529ba52019-07-02 23:13:53 +02002537 total_width = popup_width(wp);
2538 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002539 popup_attr = get_wcr_attr(wp);
2540
2541 // We can only use these line drawing characters when 'encoding' is
2542 // "utf-8" and 'ambiwidth' is "single".
2543 if (enc_utf8 && *p_ambw == 's')
2544 {
2545 border_char[0] = border_char[2] = 0x2550;
2546 border_char[1] = border_char[3] = 0x2551;
2547 border_char[4] = 0x2554;
2548 border_char[5] = 0x2557;
2549 border_char[6] = 0x255d;
2550 border_char[7] = 0x255a;
2551 }
2552 else
2553 {
2554 border_char[0] = border_char[2] = '-';
2555 border_char[1] = border_char[3] = '|';
2556 for (i = 4; i < 8; ++i)
2557 border_char[i] = '+';
2558 }
2559 for (i = 0; i < 8; ++i)
2560 if (wp->w_border_char[i] != 0)
2561 border_char[i] = wp->w_border_char[i];
2562
2563 for (i = 0; i < 4; ++i)
2564 {
2565 border_attr[i] = popup_attr;
2566 if (wp->w_border_highlight[i] != NULL)
2567 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2568 }
2569
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002570 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002571 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002572 if (wp->w_popup_border[0] > 0)
2573 {
2574 // top border
2575 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002576 wincol < 0 ? 0 : wincol, wincol + total_width,
2577 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002578 ? border_char[4] : border_char[0],
2579 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002580 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002581 {
2582 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2583 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002584 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002585 }
2586 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002587 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2588 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002589
Bram Moolenaard529ba52019-07-02 23:13:53 +02002590 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2591 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002592 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002593 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2594 - wp->w_has_scrollbar;
2595 if (padcol < 0)
2596 {
2597 padwidth += padcol;
2598 padcol = 0;
2599 }
2600 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002601 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002602 {
2603 // top padding
2604 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002605 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002606 ' ', ' ', popup_attr);
2607 }
2608
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002609 // Title goes on top of border or padding.
2610 if (wp->w_popup_title != NULL)
2611 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2612 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2613
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002614 // Compute scrollbar thumb position and size.
2615 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002616 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002617 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2618
Bram Moolenaar68acb412019-06-26 03:40:36 +02002619 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2620 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002621 if (sb_thumb_height == 0)
2622 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002623 if (linecount <= wp->w_height)
2624 // it just fits, avoid divide by zero
2625 sb_thumb_top = 0;
2626 else
2627 sb_thumb_top = (wp->w_topline - 1
2628 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002629 * (wp->w_height - sb_thumb_height)
2630 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002631 if (wp->w_scrollbar_highlight != NULL)
2632 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2633 else
2634 attr_scroll = highlight_attr[HLF_PSB];
2635 if (wp->w_thumb_highlight != NULL)
2636 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2637 else
2638 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002639 }
2640
2641 for (i = wp->w_popup_border[0];
2642 i < total_height - wp->w_popup_border[2]; ++i)
2643 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002644 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002645 // left and right padding only needed next to the body
2646 int do_padding =
2647 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2648 && i < total_height - wp->w_popup_border[2]
2649 - wp->w_popup_padding[2];
2650
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002651 row = wp->w_winrow + i;
2652
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002653 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002654 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002655 {
2656 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002657 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002658 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002659 if (do_padding && wp->w_popup_padding[3] > 0)
2660 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002661 int col = wincol + wp->w_popup_border[3];
2662
Bram Moolenaard529ba52019-07-02 23:13:53 +02002663 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002664 pad_left = wp->w_popup_padding[3];
2665 if (col < 0)
2666 {
2667 pad_left += col;
2668 col = 0;
2669 }
2670 if (pad_left > 0)
2671 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2672 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002673 // scrollbar
2674 if (wp->w_has_scrollbar)
2675 {
2676 int line = i - top_off;
2677 int scroll_col = wp->w_wincol + total_width - 1
2678 - wp->w_popup_border[1];
2679
2680 if (line >= 0 && line < wp->w_height)
2681 screen_putchar(' ', row, scroll_col,
2682 line >= sb_thumb_top
2683 && line < sb_thumb_top + sb_thumb_height
2684 ? attr_thumb : attr_scroll);
2685 else
2686 screen_putchar(' ', row, scroll_col, popup_attr);
2687 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002688 // right border
2689 if (wp->w_popup_border[1] > 0)
2690 {
2691 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002692 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002693 }
2694 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002695 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002696 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002697 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002698 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2699 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002700 }
2701
2702 if (wp->w_popup_padding[2] > 0)
2703 {
2704 // bottom padding
2705 row = wp->w_winrow + wp->w_popup_border[0]
2706 + wp->w_popup_padding[0] + wp->w_height;
2707 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002708 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002709 }
2710
2711 if (wp->w_popup_border[2] > 0)
2712 {
2713 // bottom border
2714 row = wp->w_winrow + total_height - 1;
2715 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002716 wincol < 0 ? 0 : wincol,
2717 wincol + total_width,
2718 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002719 ? border_char[7] : border_char[2],
2720 border_char[2], border_attr[2]);
2721 if (wp->w_popup_border[1] > 0)
2722 {
2723 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002724 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002725 }
2726 }
2727
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002728 if (wp->w_popup_close == POPCLOSE_BUTTON)
2729 {
2730 // close button goes on top of anything at the top-right corner
2731 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002732 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002733 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2734 }
2735
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002736 update_popup_transparent(wp, 0);
2737
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002738 // Back to the normal zindex.
2739 screen_zindex = 0;
2740 }
2741}
2742
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002743/*
2744 * Mark references in callbacks of one popup window.
2745 */
2746 static int
2747set_ref_in_one_popup(win_T *wp, int copyID)
2748{
2749 int abort = FALSE;
2750 typval_T tv;
2751
2752 if (wp->w_close_cb.cb_partial != NULL)
2753 {
2754 tv.v_type = VAR_PARTIAL;
2755 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2756 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2757 }
2758 if (wp->w_filter_cb.cb_partial != NULL)
2759 {
2760 tv.v_type = VAR_PARTIAL;
2761 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2762 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2763 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002764 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002765 return abort;
2766}
2767
2768/*
2769 * Set reference in callbacks of popup windows.
2770 */
2771 int
2772set_ref_in_popups(int copyID)
2773{
2774 int abort = FALSE;
2775 win_T *wp;
2776 tabpage_T *tp;
2777
2778 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2779 abort = abort || set_ref_in_one_popup(wp, copyID);
2780
2781 FOR_ALL_TABPAGES(tp)
2782 {
2783 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2784 abort = abort || set_ref_in_one_popup(wp, copyID);
2785 if (abort)
2786 break;
2787 }
2788 return abort;
2789}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002790#endif // FEAT_TEXT_PROP