blob: 47a5c789dce1445870904ecad7f9376c46acde4a [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
Bram Moolenaar79648732019-07-18 21:43:07 +020016#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
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/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200445 * Scroll to show the line with the cursor. This assumes lines don't wrap.
446 */
447 static void
448popup_show_curline(win_T *wp)
449{
450 if (wp->w_cursor.lnum < wp->w_topline)
451 wp->w_topline = wp->w_cursor.lnum;
452 else if (wp->w_cursor.lnum >= wp->w_botline)
453 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
454}
455
456/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200457 * Highlight the line with the cursor.
458 * Also scrolls the text to put the cursor line in view.
459 */
460 static void
461popup_highlight_curline(win_T *wp)
462{
463 int id;
464 char buf[100];
465
466 match_delete(wp, 1, FALSE);
467
468 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
469 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200470 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200471
472 id = syn_name2id((char_u *)"PopupSelected");
473 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
474 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
475 (char_u *)buf, 10, 1, NULL, NULL);
476 }
477}
478
479/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200480 * Shared between popup_create() and f_popup_setoptions().
481 */
482 static void
483apply_general_options(win_T *wp, dict_T *dict)
484{
485 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200486 int nr;
487 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200488
Bram Moolenaarae943152019-06-16 22:54:14 +0200489 // TODO: flip
490
491 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200492 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200493 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
494 if (wp->w_firstline < 1)
495 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200496
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200497 di = dict_find(dict, (char_u *)"scrollbar", -1);
498 if (di != NULL)
499 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
500
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200501 str = dict_get_string(dict, (char_u *)"title", FALSE);
502 if (str != NULL)
503 {
504 vim_free(wp->w_popup_title);
505 wp->w_popup_title = vim_strsave(str);
506 }
507
Bram Moolenaar402502d2019-05-30 22:07:36 +0200508 di = dict_find(dict, (char_u *)"wrap", -1);
509 if (di != NULL)
510 {
511 nr = dict_get_number(dict, (char_u *)"wrap");
512 wp->w_p_wrap = nr != 0;
513 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200514
Bram Moolenaara42d9452019-06-15 21:46:30 +0200515 di = dict_find(dict, (char_u *)"drag", -1);
516 if (di != NULL)
517 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200518
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200519 di = dict_find(dict, (char_u *)"close", -1);
520 if (di != NULL)
521 {
522 int ok = TRUE;
523
524 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
525 {
526 char_u *s = di->di_tv.vval.v_string;
527
528 if (STRCMP(s, "none") == 0)
529 wp->w_popup_close = POPCLOSE_NONE;
530 else if (STRCMP(s, "button") == 0)
531 wp->w_popup_close = POPCLOSE_BUTTON;
532 else if (STRCMP(s, "click") == 0)
533 wp->w_popup_close = POPCLOSE_CLICK;
534 else
535 ok = FALSE;
536 }
537 else
538 ok = FALSE;
539 if (!ok)
540 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
541 }
542
Bram Moolenaarae943152019-06-16 22:54:14 +0200543 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
544 if (str != NULL)
545 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
546 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200547
Bram Moolenaarae943152019-06-16 22:54:14 +0200548 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
549 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200550
Bram Moolenaar790498b2019-06-01 22:15:29 +0200551 di = dict_find(dict, (char_u *)"borderhighlight", -1);
552 if (di != NULL)
553 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200554 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200555 emsg(_(e_listreq));
556 else
557 {
558 list_T *list = di->di_tv.vval.v_list;
559 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200560 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200561
Bram Moolenaar403d0902019-07-17 21:37:32 +0200562 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
563 ++i, li = li->li_next)
564 {
565 str = tv_get_string(&li->li_tv);
566 if (*str != NUL)
567 wp->w_border_highlight[i] = vim_strsave(str);
568 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200569 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
570 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200571 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200572 vim_strsave(wp->w_border_highlight[0]);
573 }
574 }
575
Bram Moolenaar790498b2019-06-01 22:15:29 +0200576 di = dict_find(dict, (char_u *)"borderchars", -1);
577 if (di != NULL)
578 {
579 if (di->di_tv.v_type != VAR_LIST)
580 emsg(_(e_listreq));
581 else
582 {
583 list_T *list = di->di_tv.vval.v_list;
584 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200585 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200586
587 if (list != NULL)
588 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
589 ++i, li = li->li_next)
590 {
591 str = tv_get_string(&li->li_tv);
592 if (*str != NUL)
593 wp->w_border_char[i] = mb_ptr2char(str);
594 }
595 if (list->lv_len == 1)
596 for (i = 1; i < 8; ++i)
597 wp->w_border_char[i] = wp->w_border_char[0];
598 if (list->lv_len == 2)
599 {
600 for (i = 4; i < 8; ++i)
601 wp->w_border_char[i] = wp->w_border_char[1];
602 for (i = 1; i < 4; ++i)
603 wp->w_border_char[i] = wp->w_border_char[0];
604 }
605 }
606 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200607
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200608 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
609 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
610
Bram Moolenaarae943152019-06-16 22:54:14 +0200611 di = dict_find(dict, (char_u *)"zindex", -1);
612 if (di != NULL)
613 {
614 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
615 if (wp->w_zindex < 1)
616 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
617 if (wp->w_zindex > 32000)
618 wp->w_zindex = 32000;
619 }
620
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200621 di = dict_find(dict, (char_u *)"mask", -1);
622 if (di != NULL)
623 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200624 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200625
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200626 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200627 {
628 listitem_T *li;
629
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200630 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200631 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
632 li = li->li_next)
633 {
634 if (li->li_tv.v_type != VAR_LIST
635 || li->li_tv.vval.v_list == NULL
636 || li->li_tv.vval.v_list->lv_len != 4)
637 {
638 ok = FALSE;
639 break;
640 }
641 }
642 }
643 if (ok)
644 {
645 wp->w_popup_mask = di->di_tv.vval.v_list;
646 ++wp->w_popup_mask->lv_refcount;
647 }
648 else
649 semsg(_(e_invargval), "mask");
650 }
651
Bram Moolenaarae943152019-06-16 22:54:14 +0200652#if defined(FEAT_TIMERS)
653 // Add timer to close the popup after some time.
654 nr = dict_get_number(dict, (char_u *)"time");
655 if (nr > 0)
656 popup_add_timeout(wp, nr);
657#endif
658
Bram Moolenaar3397f742019-06-02 18:40:06 +0200659 di = dict_find(dict, (char_u *)"moved", -1);
660 if (di != NULL)
661 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200662 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200663 handle_moved_argument(wp, di, FALSE);
664 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200665
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200666 di = dict_find(dict, (char_u *)"mousemoved", -1);
667 if (di != NULL)
668 {
669 set_mousemoved_values(wp);
670 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200671 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200672
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200673 di = dict_find(dict, (char_u *)"cursorline", -1);
674 if (di != NULL)
675 {
676 if (di->di_tv.v_type == VAR_NUMBER)
677 {
678 if (di->di_tv.vval.v_number != 0)
679 wp->w_popup_flags |= POPF_CURSORLINE;
680 else
681 wp->w_popup_flags &= ~POPF_CURSORLINE;
682 }
683 else
684 semsg(_(e_invargval), "cursorline");
685 }
686
Bram Moolenaarae943152019-06-16 22:54:14 +0200687 di = dict_find(dict, (char_u *)"filter", -1);
688 if (di != NULL)
689 {
690 callback_T callback = get_callback(&di->di_tv);
691
692 if (callback.cb_name != NULL)
693 {
694 free_callback(&wp->w_filter_cb);
695 set_callback(&wp->w_filter_cb, &callback);
696 }
697 }
698
699 di = dict_find(dict, (char_u *)"callback", -1);
700 if (di != NULL)
701 {
702 callback_T callback = get_callback(&di->di_tv);
703
704 if (callback.cb_name != NULL)
705 {
706 free_callback(&wp->w_close_cb);
707 set_callback(&wp->w_close_cb, &callback);
708 }
709 }
710}
711
712/*
713 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200714 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200715 */
716 static void
717apply_options(win_T *wp, dict_T *dict)
718{
719 int nr;
720
721 apply_move_options(wp, dict);
722 apply_general_options(wp, dict);
723
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200724 nr = dict_get_number(dict, (char_u *)"hidden");
725 if (nr > 0)
726 {
727 wp->w_popup_flags |= POPF_HIDDEN;
728 --wp->w_buffer->b_nwindows;
729 }
730
Bram Moolenaar33796b32019-06-08 16:01:13 +0200731 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200732 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200733}
734
735/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200736 * Add lines to the popup from a list of strings.
737 */
738 static void
739add_popup_strings(buf_T *buf, list_T *l)
740{
741 listitem_T *li;
742 linenr_T lnum = 0;
743 char_u *p;
744
745 for (li = l->lv_first; li != NULL; li = li->li_next)
746 if (li->li_tv.v_type == VAR_STRING)
747 {
748 p = li->li_tv.vval.v_string;
749 ml_append_buf(buf, lnum++,
750 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
751 }
752}
753
754/*
755 * Add lines to the popup from a list of dictionaries.
756 */
757 static void
758add_popup_dicts(buf_T *buf, list_T *l)
759{
760 listitem_T *li;
761 listitem_T *pli;
762 linenr_T lnum = 0;
763 char_u *p;
764 dict_T *dict;
765
766 // first add the text lines
767 for (li = l->lv_first; li != NULL; li = li->li_next)
768 {
769 if (li->li_tv.v_type != VAR_DICT)
770 {
771 emsg(_(e_dictreq));
772 return;
773 }
774 dict = li->li_tv.vval.v_dict;
775 p = dict == NULL ? NULL
776 : dict_get_string(dict, (char_u *)"text", FALSE);
777 ml_append_buf(buf, lnum++,
778 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
779 }
780
781 // add the text properties
782 lnum = 1;
783 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
784 {
785 dictitem_T *di;
786 list_T *plist;
787
788 dict = li->li_tv.vval.v_dict;
789 di = dict_find(dict, (char_u *)"props", -1);
790 if (di != NULL)
791 {
792 if (di->di_tv.v_type != VAR_LIST)
793 {
794 emsg(_(e_listreq));
795 return;
796 }
797 plist = di->di_tv.vval.v_list;
798 if (plist != NULL)
799 {
800 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
801 {
802 if (pli->li_tv.v_type != VAR_DICT)
803 {
804 emsg(_(e_dictreq));
805 return;
806 }
807 dict = pli->li_tv.vval.v_dict;
808 if (dict != NULL)
809 {
810 int col = dict_get_number(dict, (char_u *)"col");
811
812 prop_add_common( lnum, col, dict, buf, NULL);
813 }
814 }
815 }
816 }
817 }
818}
819
820/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200821 * Get the padding plus border at the top, adjusted to 1 if there is a title.
822 */
823 static int
824popup_top_extra(win_T *wp)
825{
826 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
827
828 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
829 return 1;
830 return extra;
831}
832
833/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200834 * Return the height of popup window "wp", including border and padding.
835 */
836 int
837popup_height(win_T *wp)
838{
839 return wp->w_height
840 + popup_top_extra(wp)
841 + wp->w_popup_padding[2] + wp->w_popup_border[2];
842}
843
844/*
845 * Return the width of popup window "wp", including border, padding and
846 * scrollbar.
847 */
848 int
849popup_width(win_T *wp)
850{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200851 // w_leftcol is how many columns of the core are left of the screen
852 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200853 return wp->w_width + wp->w_leftcol
854 + wp->w_popup_padding[3] + wp->w_popup_border[3]
855 + wp->w_popup_padding[1] + wp->w_popup_border[1]
856 + wp->w_has_scrollbar
857 + wp->w_popup_rightoff;
858}
859
860/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200861 * Adjust the position and size of the popup to fit on the screen.
862 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200863 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200864popup_adjust_position(win_T *wp)
865{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200866 linenr_T lnum;
867 int wrapped = 0;
868 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200869 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200870 int center_vert = FALSE;
871 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200872 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200873 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200874 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
875 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
876 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
877 int extra_height = top_extra + bot_extra;
878 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200879 int org_winrow = wp->w_winrow;
880 int org_wincol = wp->w_wincol;
881 int org_width = wp->w_width;
882 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200883 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200884 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200885 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200886
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200887 wp->w_winrow = 0;
888 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200889 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200890 wp->w_popup_leftoff = 0;
891 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200892 if (wp->w_popup_pos == POPPOS_CENTER)
893 {
894 // center after computing the size
895 center_vert = TRUE;
896 center_hor = TRUE;
897 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200898 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200899 {
900 if (wp->w_wantline == 0)
901 center_vert = TRUE;
902 else if (wp->w_popup_pos == POPPOS_TOPLEFT
903 || wp->w_popup_pos == POPPOS_TOPRIGHT)
904 {
905 wp->w_winrow = wp->w_wantline - 1;
906 if (wp->w_winrow >= Rows)
907 wp->w_winrow = Rows - 1;
908 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200909
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200910 if (wp->w_wantcol == 0)
911 center_hor = TRUE;
912 else if (wp->w_popup_pos == POPPOS_TOPLEFT
913 || wp->w_popup_pos == POPPOS_BOTLEFT)
914 {
915 wp->w_wincol = wp->w_wantcol - 1;
916 if (wp->w_wincol >= Columns - 3)
917 wp->w_wincol = Columns - 3;
918 }
919 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200920
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200921 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200922 // When left aligned use the space available, but shift to the left when we
923 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200924 maxspace = Columns - wp->w_wincol - left_extra;
925 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200926 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200927 {
928 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200929 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200930 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200931
Bram Moolenaar8d241042019-06-12 23:40:01 +0200932 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +0200933 if (wp->w_firstline != 0)
934 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200935 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
936 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
937
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200938 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200939 // Use a minimum width of one, so that something shows when there is no
940 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200941 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200942 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200943 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200944 {
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200945 // count Tabs for what they are worth
946 int len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
947 (colnr_T)MAXCOL);
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200948
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200949 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200950 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200951 while (len > maxwidth)
952 {
953 ++wrapped;
954 len -= maxwidth;
955 wp->w_width = maxwidth;
956 }
957 }
958 else if (len > maxwidth
959 && allow_adjust_left
960 && (wp->w_popup_pos == POPPOS_TOPLEFT
961 || wp->w_popup_pos == POPPOS_BOTLEFT))
962 {
963 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200964 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200965
Bram Moolenaar51c31312019-06-15 22:27:23 +0200966 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200967 {
968 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200969
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200970 len -= truncate_shift;
971 shift_by -= truncate_shift;
972 }
973
974 wp->w_wincol -= shift_by;
975 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200976 wp->w_width = maxwidth;
977 }
978 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +0200979 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200980 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +0200981 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
982 wp->w_width = wp->w_maxwidth;
983 }
Bram Moolenaar8d241042019-06-12 23:40:01 +0200984 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200985 if (wp->w_maxheight > 0
986 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200987 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200988 }
989
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200990 wp->w_has_scrollbar = wp->w_want_scrollbar
991 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
992
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200993 minwidth = wp->w_minwidth;
994 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
995 {
996 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
997
998 if (minwidth < title_len)
999 minwidth = title_len;
1000 }
1001
1002 if (minwidth > 0 && wp->w_width < minwidth)
1003 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001004 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001005 {
1006 if (wp->w_width > maxspace)
1007 // some columns cut off on the right
1008 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001009 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001010 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001011 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001012 {
1013 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1014 if (wp->w_wincol < 0)
1015 wp->w_wincol = 0;
1016 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001017 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1018 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1019 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001020 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1021
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001022 // Right aligned: move to the right if needed.
1023 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001024 if (leftoff >= 0)
1025 wp->w_wincol = leftoff;
1026 else if (wp->w_popup_fixed)
1027 {
1028 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001029 // columns when displaying the window, minus left border and
1030 // padding.
1031 if (-leftoff > left_extra)
1032 wp->w_leftcol = -leftoff - left_extra;
1033 wp->w_width -= wp->w_leftcol;
1034 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001035 if (wp->w_width < 0)
1036 wp->w_width = 0;
1037 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001038 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001039
Bram Moolenaar8d241042019-06-12 23:40:01 +02001040 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1041 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001042 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1043 wp->w_height = wp->w_minheight;
1044 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1045 wp->w_height = wp->w_maxheight;
1046 if (wp->w_height > Rows - wp->w_winrow)
1047 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001048
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001049 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001050 {
1051 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1052 if (wp->w_winrow < 0)
1053 wp->w_winrow = 0;
1054 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001055 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1056 || wp->w_popup_pos == POPPOS_BOTLEFT)
1057 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001058 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001059 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001060 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001061 else
1062 // not enough space, make top aligned
1063 wp->w_winrow = wp->w_wantline + 1;
1064 }
1065
Bram Moolenaar17146962019-05-30 00:12:11 +02001066 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001067
1068 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001069 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001070 if (org_winrow != wp->w_winrow
1071 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001072 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001073 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001074 || org_width != wp->w_width
1075 || org_height != wp->w_height)
1076 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001077 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001078 popup_mask_refresh = TRUE;
1079 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001080}
1081
Bram Moolenaar17627312019-06-02 19:53:44 +02001082typedef enum
1083{
1084 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001085 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001086 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001087 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001088 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001089 TYPE_MENU,
1090 TYPE_PREVIEW
Bram Moolenaar17627312019-06-02 19:53:44 +02001091} create_type_T;
1092
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001093/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001094 * Make "buf" empty and set the contents to "text".
1095 * Used by popup_create() and popup_settext().
1096 */
1097 static void
1098popup_set_buffer_text(buf_T *buf, typval_T text)
1099{
1100 int lnum;
1101
1102 // Clear the buffer, then replace the lines.
1103 curbuf = buf;
1104 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1105 ml_delete(lnum, FALSE);
1106 curbuf = curwin->w_buffer;
1107
1108 // Add text to the buffer.
1109 if (text.v_type == VAR_STRING)
1110 {
1111 // just a string
1112 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1113 }
1114 else
1115 {
1116 list_T *l = text.vval.v_list;
1117
1118 if (l->lv_len > 0)
1119 {
1120 if (l->lv_first->li_tv.v_type == VAR_STRING)
1121 // list of strings
1122 add_popup_strings(buf, l);
1123 else
1124 // list of dictionaries
1125 add_popup_dicts(buf, l);
1126 }
1127 }
1128
1129 // delete the line that was in the empty buffer
1130 curbuf = buf;
1131 ml_delete(buf->b_ml.ml_line_count, FALSE);
1132 curbuf = curwin->w_buffer;
1133}
1134
1135/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001136 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1137 * not NULL.
1138 * Return FAIL if the parsing fails.
1139 */
1140 int
1141parse_previewpopup(win_T *wp)
1142{
1143 char_u *p;
1144
1145 for (p = p_pvp; *p != NUL; p += (*p == ',' ? 1 : 0))
1146 {
1147 char_u *e, *dig;
1148 char_u *s = p;
1149 int x;
1150
1151 e = vim_strchr(p, ':');
1152 if (e == NULL || e[1] == NUL)
1153 return FAIL;
1154
1155 p = vim_strchr(e, ',');
1156 if (p == NULL)
1157 p = e + STRLEN(e);
1158 dig = e + 1;
1159 x = getdigits(&dig);
1160 if (dig != p)
1161 return FAIL;
1162
1163 if (STRNCMP(s, "height:", 7) == 0)
1164 {
1165 if (wp != NULL)
1166 {
1167 wp->w_minheight = x;
1168 wp->w_maxheight = x;
1169 }
1170 }
1171 else if (STRNCMP(s, "width:", 6) == 0)
1172 {
1173 if (wp != NULL)
1174 {
1175 wp->w_minwidth = x;
1176 wp->w_maxwidth = x;
1177 }
1178 }
1179 else
1180 return FAIL;
1181 }
1182 return OK;
1183}
1184
1185/*
1186 * Set w_wantline and w_wantcol for the cursor position in the current window.
1187 */
1188 void
1189popup_set_wantpos(win_T *wp)
1190{
1191 setcursor_mayforce(TRUE);
1192 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1193 if (wp->w_wantline == 0) // cursor in first line
1194 {
1195 wp->w_wantline = 2;
1196 wp->w_popup_pos = POPPOS_TOPLEFT;
1197 }
1198 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
1199 popup_adjust_position(wp);
1200}
1201
1202/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001203 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001204 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001205 * etc.
1206 * When creating a preview window popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001207 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001208 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001209popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001210{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001211 win_T *wp;
1212 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001213 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001214 int new_buffer;
1215 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001216 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001217 int nr;
1218 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001219
Bram Moolenaar79648732019-07-18 21:43:07 +02001220 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001221 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001222 // Check arguments look OK.
1223 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001224 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001225 buf = buflist_findnr( argvars[0].vval.v_number);
1226 if (buf == NULL)
1227 {
1228 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1229 return NULL;
1230 }
1231 }
1232 else if (!(argvars[0].v_type == VAR_STRING
1233 && argvars[0].vval.v_string != NULL)
1234 && !(argvars[0].v_type == VAR_LIST
1235 && argvars[0].vval.v_list != NULL))
1236 {
1237 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001238 return NULL;
1239 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001240 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001241 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001242 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001243 return NULL;
1244 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001245 d = argvars[1].vval.v_dict;
1246 }
1247
1248 if (d != NULL)
1249 {
1250 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1251 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1252 else if (type == TYPE_NOTIFICATION)
1253 tabnr = -1; // notifications are global by default
1254 else
1255 tabnr = 0;
1256 if (tabnr > 0)
1257 {
1258 tp = find_tabpage(tabnr);
1259 if (tp == NULL)
1260 {
1261 semsg(_("E997: Tabpage not found: %d"), tabnr);
1262 return NULL;
1263 }
1264 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001265 }
1266
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001267 // Create the window and buffer.
1268 wp = win_alloc_popup_win();
1269 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001270 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001271 if (rettv != NULL)
1272 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001273 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001274 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001275
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001276 if (buf != NULL)
1277 {
1278 // use existing buffer
1279 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001280 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001281 buffer_ensure_loaded(buf);
1282 }
1283 else
1284 {
1285 // create a new buffer associated with the popup
1286 new_buffer = TRUE;
1287 buf = buflist_new(NULL, NULL, (linenr_T)0,
1288 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1289 if (buf == NULL)
1290 return NULL;
1291 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001292
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001293 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001294
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001295 set_local_options_default(wp);
1296 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001297 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001298 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001299 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001300 buf->b_p_ul = -1; // no undo
1301 buf->b_p_swf = FALSE; // no swap file
1302 buf->b_p_bl = FALSE; // unlisted buffer
1303 buf->b_locked = TRUE;
1304 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001305
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001306 // Avoid that 'buftype' is reset when this buffer is entered.
1307 buf->b_p_initialized = TRUE;
1308 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001309
Bram Moolenaara3fce622019-06-20 02:31:49 +02001310 if (tp != NULL)
1311 {
1312 // popup on specified tab page
1313 wp->w_next = tp->tp_first_popupwin;
1314 tp->tp_first_popupwin = wp;
1315 }
1316 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001317 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001318 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001319 wp->w_next = curtab->tp_first_popupwin;
1320 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001321 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001322 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001323 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001324 win_T *prev = first_popupwin;
1325
1326 // Global popup: add at the end, so that it gets displayed on top of
1327 // older ones with the same zindex. Matters for notifications.
1328 if (first_popupwin == NULL)
1329 first_popupwin = wp;
1330 else
1331 {
1332 while (prev->w_next != NULL)
1333 prev = prev->w_next;
1334 prev->w_next = wp;
1335 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001336 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001337
Bram Moolenaar79648732019-07-18 21:43:07 +02001338 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001339 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001340
Bram Moolenaar79648732019-07-18 21:43:07 +02001341 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001342 {
1343 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001344 popup_set_wantpos(wp);
1345 }
1346 if (type == TYPE_ATCURSOR)
1347 {
Bram Moolenaar17627312019-06-02 19:53:44 +02001348 set_moved_values(wp);
1349 set_moved_columns(wp, FIND_STRING);
1350 }
1351
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001352 if (type == TYPE_BEVAL)
1353 {
1354 wp->w_popup_pos = POPPOS_BOTLEFT;
1355
1356 // by default use the mouse position
1357 wp->w_wantline = mouse_row;
1358 if (wp->w_wantline <= 0) // mouse on first line
1359 {
1360 wp->w_wantline = 2;
1361 wp->w_popup_pos = POPPOS_TOPLEFT;
1362 }
1363 wp->w_wantcol = mouse_col + 1;
1364 set_mousemoved_values(wp);
1365 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1366 }
1367
Bram Moolenaar33796b32019-06-08 16:01:13 +02001368 // set default values
1369 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001370 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001371
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001372 if (type == TYPE_NOTIFICATION)
1373 {
1374 win_T *twp, *nextwin;
1375 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001376
1377 // Try to not overlap with another global popup. Guess we need 3
1378 // more screen lines than buffer lines.
1379 wp->w_wantline = 1;
1380 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1381 {
1382 nextwin = twp->w_next;
1383 if (twp != wp
1384 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1385 && twp->w_winrow <= wp->w_wantline - 1 + height
1386 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1387 {
1388 // move to below this popup and restart the loop to check for
1389 // overlap with other popups
1390 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1391 nextwin = first_popupwin;
1392 }
1393 }
1394 if (wp->w_wantline + height > Rows)
1395 {
1396 // can't avoid overlap, put on top in the hope that message goes
1397 // away soon.
1398 wp->w_wantline = 1;
1399 }
1400
1401 wp->w_wantcol = 10;
1402 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001403 wp->w_minwidth = 20;
1404 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001405 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001406 for (i = 0; i < 4; ++i)
1407 wp->w_popup_border[i] = 1;
1408 wp->w_popup_padding[1] = 1;
1409 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001410
1411 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001412 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001413 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1414 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001415 }
1416
Bram Moolenaara730e552019-06-16 19:05:31 +02001417 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001418 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001419 wp->w_popup_pos = POPPOS_CENTER;
1420 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1421 wp->w_popup_drag = 1;
1422 for (i = 0; i < 4; ++i)
1423 {
1424 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001425 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001426 }
1427 }
1428
Bram Moolenaara730e552019-06-16 19:05:31 +02001429 if (type == TYPE_MENU)
1430 {
1431 typval_T tv;
1432 callback_T callback;
1433
1434 tv.v_type = VAR_STRING;
1435 tv.vval.v_string = (char_u *)"popup_filter_menu";
1436 callback = get_callback(&tv);
1437 if (callback.cb_name != NULL)
1438 set_callback(&wp->w_filter_cb, &callback);
1439
1440 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001441 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001442 }
1443
Bram Moolenaar79648732019-07-18 21:43:07 +02001444 if (type == TYPE_PREVIEW)
1445 {
1446 wp->w_popup_drag = 1;
1447 wp->w_popup_close = POPCLOSE_BUTTON;
1448 for (i = 0; i < 4; ++i)
1449 wp->w_popup_border[i] = 1;
1450 parse_previewpopup(wp);
1451 }
1452
Bram Moolenaarae943152019-06-16 22:54:14 +02001453 for (i = 0; i < 4; ++i)
1454 VIM_CLEAR(wp->w_border_highlight[i]);
1455 for (i = 0; i < 8; ++i)
1456 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001457 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001458 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001459
Bram Moolenaar79648732019-07-18 21:43:07 +02001460 if (d != NULL)
1461 // Deal with options.
1462 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001463
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001464#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001465 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1466 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001467#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001468
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001469 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001470
1471 wp->w_vsep_width = 0;
1472
1473 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001474 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001475
1476 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001477}
1478
1479/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001480 * popup_clear()
1481 */
1482 void
1483f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1484{
1485 close_all_popups();
1486}
1487
1488/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001489 * popup_create({text}, {options})
1490 */
1491 void
1492f_popup_create(typval_T *argvars, typval_T *rettv)
1493{
Bram Moolenaar17627312019-06-02 19:53:44 +02001494 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001495}
1496
1497/*
1498 * popup_atcursor({text}, {options})
1499 */
1500 void
1501f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1502{
Bram Moolenaar17627312019-06-02 19:53:44 +02001503 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001504}
1505
1506/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001507 * popup_beval({text}, {options})
1508 */
1509 void
1510f_popup_beval(typval_T *argvars, typval_T *rettv)
1511{
1512 popup_create(argvars, rettv, TYPE_BEVAL);
1513}
1514
1515/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001516 * Invoke the close callback for window "wp" with value "result".
1517 * Careful: The callback may make "wp" invalid!
1518 */
1519 static void
1520invoke_popup_callback(win_T *wp, typval_T *result)
1521{
1522 typval_T rettv;
1523 int dummy;
1524 typval_T argv[3];
1525
1526 argv[0].v_type = VAR_NUMBER;
1527 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1528
1529 if (result != NULL && result->v_type != VAR_UNKNOWN)
1530 copy_tv(result, &argv[1]);
1531 else
1532 {
1533 argv[1].v_type = VAR_NUMBER;
1534 argv[1].vval.v_number = 0;
1535 }
1536
1537 argv[2].v_type = VAR_UNKNOWN;
1538
1539 call_callback(&wp->w_close_cb, -1,
1540 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1541 if (result != NULL)
1542 clear_tv(&argv[1]);
1543 clear_tv(&rettv);
1544}
1545
1546/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001547 * Close popup "wp" and invoke any close callback for it.
1548 */
1549 static void
1550popup_close_and_callback(win_T *wp, typval_T *arg)
1551{
1552 int id = wp->w_id;
1553
1554 if (wp->w_close_cb.cb_name != NULL)
1555 // Careful: This may make "wp" invalid.
1556 invoke_popup_callback(wp, arg);
1557
1558 popup_close(id);
1559}
1560
1561/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001562 * Close popup "wp" because of a mouse click.
1563 */
1564 void
1565popup_close_for_mouse_click(win_T *wp)
1566{
1567 typval_T res;
1568
1569 res.v_type = VAR_NUMBER;
1570 res.vval.v_number = -2;
1571 popup_close_and_callback(wp, &res);
1572}
1573
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001574 static void
1575check_mouse_moved(win_T *wp, win_T *mouse_wp)
1576{
1577 // Close the popup when all if these are true:
1578 // - the mouse is not on this popup
1579 // - "mousemoved" was used
1580 // - the mouse is no longer on the same screen row or the mouse column is
1581 // outside of the relevant text
1582 if (wp != mouse_wp
1583 && wp->w_popup_mouse_row != 0
1584 && (wp->w_popup_mouse_row != mouse_row
1585 || mouse_col < wp->w_popup_mouse_mincol
1586 || mouse_col > wp->w_popup_mouse_maxcol))
1587 {
1588 typval_T res;
1589
1590 res.v_type = VAR_NUMBER;
1591 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001592 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001593 popup_close_and_callback(wp, &res);
1594 }
1595}
1596
1597/*
1598 * Called when the mouse moved: may close a popup with "mousemoved".
1599 */
1600 void
1601popup_handle_mouse_moved(void)
1602{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001603 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001604 win_T *mouse_wp;
1605 int row = mouse_row;
1606 int col = mouse_col;
1607
1608 // find the window where the mouse is in
1609 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1610
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001611 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1612 {
1613 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001614 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001615 }
1616 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1617 {
1618 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001619 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001620 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001621}
1622
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001623/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001624 * In a filter: check if the typed key is a mouse event that is used for
1625 * dragging the popup.
1626 */
1627 static void
1628filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1629{
1630 int row = mouse_row;
1631 int col = mouse_col;
1632
1633 if (wp->w_popup_drag
1634 && is_mouse_key(c)
1635 && (wp == popup_dragwin
1636 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1637 // do not consume the key, allow for dragging the popup
1638 rettv->vval.v_number = 0;
1639}
1640
Bram Moolenaara730e552019-06-16 19:05:31 +02001641/*
1642 * popup_filter_menu({text}, {options})
1643 */
1644 void
1645f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1646{
1647 int id = tv_get_number(&argvars[0]);
1648 win_T *wp = win_id2wp(id);
1649 char_u *key = tv_get_string(&argvars[1]);
1650 typval_T res;
1651 int c;
1652 linenr_T old_lnum;
1653
1654 // If the popup has been closed do not consume the key.
1655 if (wp == NULL)
1656 return;
1657
1658 c = *key;
1659 if (c == K_SPECIAL && key[1] != NUL)
1660 c = TO_SPECIAL(key[1], key[2]);
1661
1662 // consume all keys until done
1663 rettv->vval.v_number = 1;
1664 res.v_type = VAR_NUMBER;
1665
1666 old_lnum = wp->w_cursor.lnum;
1667 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1668 --wp->w_cursor.lnum;
1669 if ((c == 'j' || c == 'J' || c == K_DOWN)
1670 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1671 ++wp->w_cursor.lnum;
1672 if (old_lnum != wp->w_cursor.lnum)
1673 {
1674 popup_highlight_curline(wp);
1675 return;
1676 }
1677
1678 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1679 {
1680 // Cancelled, invoke callback with -1
1681 res.vval.v_number = -1;
1682 popup_close_and_callback(wp, &res);
1683 return;
1684 }
1685 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1686 {
1687 // Invoke callback with current index.
1688 res.vval.v_number = wp->w_cursor.lnum;
1689 popup_close_and_callback(wp, &res);
1690 return;
1691 }
1692
1693 filter_handle_drag(wp, c, rettv);
1694}
1695
1696/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001697 * popup_filter_yesno({text}, {options})
1698 */
1699 void
1700f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1701{
1702 int id = tv_get_number(&argvars[0]);
1703 win_T *wp = win_id2wp(id);
1704 char_u *key = tv_get_string(&argvars[1]);
1705 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001706 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001707
1708 // If the popup has been closed don't consume the key.
1709 if (wp == NULL)
1710 return;
1711
Bram Moolenaara730e552019-06-16 19:05:31 +02001712 c = *key;
1713 if (c == K_SPECIAL && key[1] != NUL)
1714 c = TO_SPECIAL(key[1], key[2]);
1715
Bram Moolenaara42d9452019-06-15 21:46:30 +02001716 // consume all keys until done
1717 rettv->vval.v_number = 1;
1718
Bram Moolenaara730e552019-06-16 19:05:31 +02001719 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001720 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001721 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001722 res.vval.v_number = 0;
1723 else
1724 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001725 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001726 return;
1727 }
1728
1729 // Invoke callback
1730 res.v_type = VAR_NUMBER;
1731 popup_close_and_callback(wp, &res);
1732}
1733
1734/*
1735 * popup_dialog({text}, {options})
1736 */
1737 void
1738f_popup_dialog(typval_T *argvars, typval_T *rettv)
1739{
1740 popup_create(argvars, rettv, TYPE_DIALOG);
1741}
1742
1743/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001744 * popup_menu({text}, {options})
1745 */
1746 void
1747f_popup_menu(typval_T *argvars, typval_T *rettv)
1748{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001749 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001750}
1751
1752/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001753 * popup_notification({text}, {options})
1754 */
1755 void
1756f_popup_notification(typval_T *argvars, typval_T *rettv)
1757{
1758 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1759}
1760
1761/*
1762 * Find the popup window with window-ID "id".
1763 * If the popup window does not exist NULL is returned.
1764 * If the window is not a popup window, and error message is given.
1765 */
1766 static win_T *
1767find_popup_win(int id)
1768{
1769 win_T *wp = win_id2wp(id);
1770
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001771 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001772 {
1773 semsg(_("E993: window %d is not a popup window"), id);
1774 return NULL;
1775 }
1776 return wp;
1777}
1778
1779/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001780 * popup_close({id})
1781 */
1782 void
1783f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1784{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001785 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001786 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001787
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001788 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001789 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001790}
1791
1792/*
1793 * popup_hide({id})
1794 */
1795 void
1796f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1797{
1798 int id = (int)tv_get_number(argvars);
1799 win_T *wp = find_popup_win(id);
1800
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001801 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001802 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001803 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001804 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001805 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001806 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001807 }
1808}
1809
1810/*
1811 * popup_show({id})
1812 */
1813 void
1814f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1815{
1816 int id = (int)tv_get_number(argvars);
1817 win_T *wp = find_popup_win(id);
1818
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001819 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001820 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001821 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001822 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001823 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001824 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001825 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001826}
1827
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001828/*
1829 * popup_settext({id}, {text})
1830 */
1831 void
1832f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1833{
1834 int id = (int)tv_get_number(&argvars[0]);
1835 win_T *wp = find_popup_win(id);
1836
1837 if (wp != NULL)
1838 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001839 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1840 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1841 else
1842 {
1843 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1844 popup_adjust_position(wp);
1845 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001846 }
1847}
1848
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001849 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001850popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001851{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001852 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001853 if (wp->w_winrow + wp->w_height >= cmdline_row)
1854 clear_cmdline = TRUE;
1855 win_free_popup(wp);
1856 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001857 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001858}
1859
Bram Moolenaarec583842019-05-26 14:11:23 +02001860/*
1861 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001862 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001863 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001864 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001865popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001866{
1867 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001868 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001869 win_T *prev = NULL;
1870
Bram Moolenaarec583842019-05-26 14:11:23 +02001871 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001872 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001873 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001874 {
1875 if (prev == NULL)
1876 first_popupwin = wp->w_next;
1877 else
1878 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001879 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001880 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001881 }
1882
Bram Moolenaarec583842019-05-26 14:11:23 +02001883 // go through tab-local popups
1884 FOR_ALL_TABPAGES(tp)
1885 popup_close_tabpage(tp, id);
1886}
1887
1888/*
1889 * Close a popup window with Window-id "id" in tabpage "tp".
1890 */
1891 void
1892popup_close_tabpage(tabpage_T *tp, int id)
1893{
1894 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001895 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001896 win_T *prev = NULL;
1897
Bram Moolenaarec583842019-05-26 14:11:23 +02001898 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1899 if (wp->w_id == id)
1900 {
1901 if (prev == NULL)
1902 *root = wp->w_next;
1903 else
1904 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001905 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001906 return;
1907 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001908}
1909
1910 void
1911close_all_popups(void)
1912{
1913 while (first_popupwin != NULL)
1914 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001915 while (curtab->tp_first_popupwin != NULL)
1916 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001917}
1918
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001919/*
1920 * popup_move({id}, {options})
1921 */
1922 void
1923f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1924{
Bram Moolenaarae943152019-06-16 22:54:14 +02001925 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001926 int id = (int)tv_get_number(argvars);
1927 win_T *wp = find_popup_win(id);
1928
1929 if (wp == NULL)
1930 return; // invalid {id}
1931
1932 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1933 {
1934 emsg(_(e_dictreq));
1935 return;
1936 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001937 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001938
Bram Moolenaarae943152019-06-16 22:54:14 +02001939 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001940
1941 if (wp->w_winrow + wp->w_height >= cmdline_row)
1942 clear_cmdline = TRUE;
1943 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001944}
1945
Bram Moolenaarbc133542019-05-29 20:26:46 +02001946/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001947 * popup_setoptions({id}, {options})
1948 */
1949 void
1950f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1951{
1952 dict_T *dict;
1953 int id = (int)tv_get_number(argvars);
1954 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001955 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001956
1957 if (wp == NULL)
1958 return; // invalid {id}
1959
1960 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1961 {
1962 emsg(_(e_dictreq));
1963 return;
1964 }
1965 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001966 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001967
1968 apply_move_options(wp, dict);
1969 apply_general_options(wp, dict);
1970
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001971 if (old_firstline != wp->w_firstline)
1972 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001973 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001974 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02001975 popup_adjust_position(wp);
1976}
1977
1978/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001979 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001980 */
1981 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001982f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001983{
1984 dict_T *dict;
1985 int id = (int)tv_get_number(argvars);
1986 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001987 int top_extra;
1988 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001989
1990 if (rettv_dict_alloc(rettv) == OK)
1991 {
1992 if (wp == NULL)
1993 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001994 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001995 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1996
Bram Moolenaarbc133542019-05-29 20:26:46 +02001997 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001998
Bram Moolenaarbc133542019-05-29 20:26:46 +02001999 dict_add_number(dict, "line", wp->w_winrow + 1);
2000 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002001 dict_add_number(dict, "width", wp->w_width + left_extra
2002 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2003 dict_add_number(dict, "height", wp->w_height + top_extra
2004 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002005
2006 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2007 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2008 dict_add_number(dict, "core_width", wp->w_width);
2009 dict_add_number(dict, "core_height", wp->w_height);
2010
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002011 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002012 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002013 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002014 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002015 }
2016}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002017/*
2018 * popup_locate({row}, {col})
2019 */
2020 void
2021f_popup_locate(typval_T *argvars, typval_T *rettv)
2022{
2023 int row = tv_get_number(&argvars[0]) - 1;
2024 int col = tv_get_number(&argvars[1]) - 1;
2025 win_T *wp;
2026
2027 wp = mouse_find_win(&row, &col, FIND_POPUP);
2028 if (WIN_IS_POPUP(wp))
2029 rettv->vval.v_number = wp->w_id;
2030}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002031
2032/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002033 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2034 */
2035 static void
2036get_padding_border(dict_T *dict, int *array, char *name)
2037{
2038 list_T *list;
2039 int i;
2040
2041 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2042 return;
2043
2044 list = list_alloc();
2045 if (list != NULL)
2046 {
2047 dict_add_list(dict, name, list);
2048 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2049 for (i = 0; i < 4; ++i)
2050 list_append_number(list, array[i]);
2051 }
2052}
2053
2054/*
2055 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2056 */
2057 static void
2058get_borderhighlight(dict_T *dict, win_T *wp)
2059{
2060 list_T *list;
2061 int i;
2062
2063 for (i = 0; i < 4; ++i)
2064 if (wp->w_border_highlight[i] != NULL)
2065 break;
2066 if (i == 4)
2067 return;
2068
2069 list = list_alloc();
2070 if (list != NULL)
2071 {
2072 dict_add_list(dict, "borderhighlight", list);
2073 for (i = 0; i < 4; ++i)
2074 list_append_string(list, wp->w_border_highlight[i], -1);
2075 }
2076}
2077
2078/*
2079 * For popup_getoptions(): add a "borderchars" entry to "dict".
2080 */
2081 static void
2082get_borderchars(dict_T *dict, win_T *wp)
2083{
2084 list_T *list;
2085 int i;
2086 char_u buf[NUMBUFLEN];
2087 int len;
2088
2089 for (i = 0; i < 8; ++i)
2090 if (wp->w_border_char[i] != 0)
2091 break;
2092 if (i == 8)
2093 return;
2094
2095 list = list_alloc();
2096 if (list != NULL)
2097 {
2098 dict_add_list(dict, "borderchars", list);
2099 for (i = 0; i < 8; ++i)
2100 {
2101 len = mb_char2bytes(wp->w_border_char[i], buf);
2102 list_append_string(list, buf, len);
2103 }
2104 }
2105}
2106
2107/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002108 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002109 */
2110 static void
2111get_moved_list(dict_T *dict, win_T *wp)
2112{
2113 list_T *list;
2114
2115 list = list_alloc();
2116 if (list != NULL)
2117 {
2118 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002119 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002120 list_append_number(list, wp->w_popup_mincol);
2121 list_append_number(list, wp->w_popup_maxcol);
2122 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002123 list = list_alloc();
2124 if (list != NULL)
2125 {
2126 dict_add_list(dict, "mousemoved", list);
2127 list_append_number(list, wp->w_popup_mouse_row);
2128 list_append_number(list, wp->w_popup_mouse_mincol);
2129 list_append_number(list, wp->w_popup_mouse_maxcol);
2130 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002131}
2132
2133/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002134 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002135 */
2136 void
2137f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2138{
2139 dict_T *dict;
2140 int id = (int)tv_get_number(argvars);
2141 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002142 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002143 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002144
2145 if (rettv_dict_alloc(rettv) == OK)
2146 {
2147 if (wp == NULL)
2148 return;
2149
2150 dict = rettv->vval.v_dict;
2151 dict_add_number(dict, "line", wp->w_wantline);
2152 dict_add_number(dict, "col", wp->w_wantcol);
2153 dict_add_number(dict, "minwidth", wp->w_minwidth);
2154 dict_add_number(dict, "minheight", wp->w_minheight);
2155 dict_add_number(dict, "maxheight", wp->w_maxheight);
2156 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002157 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002158 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002159 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002160 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002161 dict_add_string(dict, "title", wp->w_popup_title);
2162 dict_add_number(dict, "wrap", wp->w_p_wrap);
2163 dict_add_number(dict, "drag", wp->w_popup_drag);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002164 dict_add_number(dict, "cursorline", (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002165 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002166 if (wp->w_scrollbar_highlight != NULL)
2167 dict_add_string(dict, "scrollbarhighlight",
2168 wp->w_scrollbar_highlight);
2169 if (wp->w_thumb_highlight != NULL)
2170 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002171
Bram Moolenaara3fce622019-06-20 02:31:49 +02002172 // find the tabpage that holds this popup
2173 i = 1;
2174 FOR_ALL_TABPAGES(tp)
2175 {
2176 win_T *p;
2177
2178 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2179 if (p->w_id == id)
2180 break;
2181 if (p != NULL)
2182 break;
2183 ++i;
2184 }
2185 if (tp == NULL)
2186 i = -1; // must be global
2187 else if (tp == curtab)
2188 i = 0;
2189 dict_add_number(dict, "tabpage", i);
2190
Bram Moolenaarae943152019-06-16 22:54:14 +02002191 get_padding_border(dict, wp->w_popup_padding, "padding");
2192 get_padding_border(dict, wp->w_popup_border, "border");
2193 get_borderhighlight(dict, wp);
2194 get_borderchars(dict, wp);
2195 get_moved_list(dict, wp);
2196
2197 if (wp->w_filter_cb.cb_name != NULL)
2198 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2199 if (wp->w_close_cb.cb_name != NULL)
2200 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002201
2202 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2203 ++i)
2204 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2205 {
2206 dict_add_string(dict, "pos",
2207 (char_u *)poppos_entries[i].pp_name);
2208 break;
2209 }
2210
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002211 dict_add_string(dict, "close", (char_u *)(
2212 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2213 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2214
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002215# if defined(FEAT_TIMERS)
2216 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2217 ? (long)wp->w_popup_timer->tr_interval : 0L);
2218# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002219 }
2220}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002221
2222 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002223error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002224{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002225 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002226 {
2227 emsg(_("E994: Not allowed in a popup window"));
2228 return TRUE;
2229 }
2230 return FALSE;
2231}
2232
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002233/*
2234 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002235 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002236 */
2237 void
2238popup_reset_handled()
2239{
2240 win_T *wp;
2241
2242 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2243 wp->w_popup_flags &= ~POPF_HANDLED;
2244 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2245 wp->w_popup_flags &= ~POPF_HANDLED;
2246}
2247
2248/*
2249 * Find the next visible popup where POPF_HANDLED is not set.
2250 * Must have called popup_reset_handled() first.
2251 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2252 * popup with the highest zindex.
2253 */
2254 win_T *
2255find_next_popup(int lowest)
2256{
2257 win_T *wp;
2258 win_T *found_wp;
2259 int found_zindex;
2260
2261 found_zindex = lowest ? INT_MAX : 0;
2262 found_wp = NULL;
2263 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2264 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2265 && (lowest ? wp->w_zindex < found_zindex
2266 : wp->w_zindex > found_zindex))
2267 {
2268 found_zindex = wp->w_zindex;
2269 found_wp = wp;
2270 }
2271 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2272 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2273 && (lowest ? wp->w_zindex < found_zindex
2274 : wp->w_zindex > found_zindex))
2275 {
2276 found_zindex = wp->w_zindex;
2277 found_wp = wp;
2278 }
2279
2280 if (found_wp != NULL)
2281 found_wp->w_popup_flags |= POPF_HANDLED;
2282 return found_wp;
2283}
2284
2285/*
2286 * Invoke the filter callback for window "wp" with typed character "c".
2287 * Uses the global "mod_mask" for modifiers.
2288 * Returns the return value of the filter.
2289 * Careful: The filter may make "wp" invalid!
2290 */
2291 static int
2292invoke_popup_filter(win_T *wp, int c)
2293{
2294 int res;
2295 typval_T rettv;
2296 int dummy;
2297 typval_T argv[3];
2298 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002299 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002300
Bram Moolenaara42d9452019-06-15 21:46:30 +02002301 // Emergency exit: CTRL-C closes the popup.
2302 if (c == Ctrl_C)
2303 {
2304 rettv.v_type = VAR_NUMBER;
2305 rettv.vval.v_number = -1;
2306 popup_close_and_callback(wp, &rettv);
2307 return 1;
2308 }
2309
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002310 argv[0].v_type = VAR_NUMBER;
2311 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2312
2313 // Convert the number to a string, so that the function can use:
2314 // if a:c == "\<F2>"
2315 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2316 argv[1].v_type = VAR_STRING;
2317 argv[1].vval.v_string = vim_strsave(buf);
2318
2319 argv[2].v_type = VAR_UNKNOWN;
2320
Bram Moolenaara42d9452019-06-15 21:46:30 +02002321 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002322 call_callback(&wp->w_filter_cb, -1,
2323 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002324 if (old_lnum != wp->w_cursor.lnum)
2325 popup_highlight_curline(wp);
2326
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002327 res = tv_get_number(&rettv);
2328 vim_free(argv[1].vval.v_string);
2329 clear_tv(&rettv);
2330 return res;
2331}
2332
2333/*
2334 * Called when "c" was typed: invoke popup filter callbacks.
2335 * Returns TRUE when the character was consumed,
2336 */
2337 int
2338popup_do_filter(int c)
2339{
2340 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002341 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002342
2343 popup_reset_handled();
2344
2345 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2346 if (wp->w_filter_cb.cb_name != NULL)
2347 res = invoke_popup_filter(wp, c);
2348
2349 return res;
2350}
2351
Bram Moolenaar3397f742019-06-02 18:40:06 +02002352/*
2353 * Called when the cursor moved: check if any popup needs to be closed if the
2354 * cursor moved far enough.
2355 */
2356 void
2357popup_check_cursor_pos()
2358{
2359 win_T *wp;
2360 typval_T tv;
2361
2362 popup_reset_handled();
2363 while ((wp = find_next_popup(TRUE)) != NULL)
2364 if (wp->w_popup_curwin != NULL
2365 && (curwin != wp->w_popup_curwin
2366 || curwin->w_cursor.lnum != wp->w_popup_lnum
2367 || curwin->w_cursor.col < wp->w_popup_mincol
2368 || curwin->w_cursor.col > wp->w_popup_maxcol))
2369 {
2370 tv.v_type = VAR_NUMBER;
2371 tv.vval.v_number = -1;
2372 popup_close_and_callback(wp, &tv);
2373 }
2374}
2375
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002376/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002377 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2378 * "col" and "line" are screen coordinates.
2379 */
2380 static int
2381popup_masked(win_T *wp, int screencol, int screenline)
2382{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002383 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002384 int line = screenline - wp->w_winrow + 1;
2385 listitem_T *lio, *li;
2386 int width, height;
2387
2388 if (wp->w_popup_mask == NULL)
2389 return FALSE;
2390 width = popup_width(wp);
2391 height = popup_height(wp);
2392
2393 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2394 {
2395 int cols, cole;
2396 int lines, linee;
2397
2398 li = lio->li_tv.vval.v_list->lv_first;
2399 cols = tv_get_number(&li->li_tv);
2400 if (cols < 0)
2401 cols = width + cols + 1;
2402 if (col < cols)
2403 continue;
2404 li = li->li_next;
2405 cole = tv_get_number(&li->li_tv);
2406 if (cole < 0)
2407 cole = width + cole + 1;
2408 if (col > cole)
2409 continue;
2410 li = li->li_next;
2411 lines = tv_get_number(&li->li_tv);
2412 if (lines < 0)
2413 lines = height + lines + 1;
2414 if (line < lines)
2415 continue;
2416 li = li->li_next;
2417 linee = tv_get_number(&li->li_tv);
2418 if (linee < 0)
2419 linee = height + linee + 1;
2420 if (line > linee)
2421 continue;
2422 return TRUE;
2423 }
2424 return FALSE;
2425}
2426
2427/*
2428 * Set flags in popup_transparent[] for window "wp" to "val".
2429 */
2430 static void
2431update_popup_transparent(win_T *wp, int val)
2432{
2433 if (wp->w_popup_mask != NULL)
2434 {
2435 int width = popup_width(wp);
2436 int height = popup_height(wp);
2437 listitem_T *lio, *li;
2438 int cols, cole;
2439 int lines, linee;
2440 int col, line;
2441
2442 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2443 {
2444 li = lio->li_tv.vval.v_list->lv_first;
2445 cols = tv_get_number(&li->li_tv);
2446 if (cols < 0)
2447 cols = width + cols + 1;
2448 li = li->li_next;
2449 cole = tv_get_number(&li->li_tv);
2450 if (cole < 0)
2451 cole = width + cole + 1;
2452 li = li->li_next;
2453 lines = tv_get_number(&li->li_tv);
2454 if (lines < 0)
2455 lines = height + lines + 1;
2456 li = li->li_next;
2457 linee = tv_get_number(&li->li_tv);
2458 if (linee < 0)
2459 linee = height + linee + 1;
2460
2461 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002462 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002463 if (cols < 0)
2464 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002465 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002466 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002467 if (lines < 0)
2468 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002469 for (line = lines; line < linee
2470 && line + wp->w_winrow < screen_Rows; ++line)
2471 for (col = cols; col < cole
2472 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002473 popup_transparent[(line + wp->w_winrow) * screen_Columns
2474 + col + wp->w_wincol] = val;
2475 }
2476 }
2477}
2478
2479/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002480 * Update "popup_mask" if needed.
2481 * Also recomputes the popup size and positions.
2482 * Also updates "popup_visible".
2483 * Also marks window lines for redrawing.
2484 */
2485 void
2486may_update_popup_mask(int type)
2487{
2488 win_T *wp;
2489 short *mask;
2490 int line, col;
2491 int redraw_all = FALSE;
2492
2493 // Need to recompute when switching tabs.
2494 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2495 // (such as the screen size) must have changed.
2496 if (popup_mask_tab != curtab || type >= NOT_VALID)
2497 {
2498 popup_mask_refresh = TRUE;
2499 redraw_all = TRUE;
2500 }
2501 if (!popup_mask_refresh)
2502 {
2503 // Check if any buffer has changed.
2504 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2505 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2506 popup_mask_refresh = TRUE;
2507 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2508 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2509 popup_mask_refresh = TRUE;
2510 if (!popup_mask_refresh)
2511 return;
2512 }
2513
2514 // Need to update the mask, something has changed.
2515 popup_mask_refresh = FALSE;
2516 popup_mask_tab = curtab;
2517 popup_visible = FALSE;
2518
2519 // If redrawing everything, just update "popup_mask".
2520 // If redrawing only what is needed, update "popup_mask_next" and then
2521 // compare with "popup_mask" to see what changed.
2522 if (type >= SOME_VALID)
2523 mask = popup_mask;
2524 else
2525 mask = popup_mask_next;
2526 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2527
2528 // Find the window with the lowest zindex that hasn't been handled yet,
2529 // so that the window with a higher zindex overwrites the value in
2530 // popup_mask.
2531 popup_reset_handled();
2532 while ((wp = find_next_popup(TRUE)) != NULL)
2533 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002534 int height;
2535 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002536
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002537 popup_visible = TRUE;
2538
2539 // Recompute the position if the text changed.
2540 if (redraw_all
2541 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2542 popup_adjust_position(wp);
2543
Bram Moolenaard529ba52019-07-02 23:13:53 +02002544 height = popup_height(wp);
2545 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002546 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002547 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002548 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002549 col < wp->w_wincol + width && col < screen_Columns; ++col)
2550 if (!popup_masked(wp, col, line))
2551 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002552 }
2553
2554 // Only check which lines are to be updated if not already
2555 // updating all lines.
2556 if (mask == popup_mask_next)
2557 for (line = 0; line < screen_Rows; ++line)
2558 {
2559 int col_done = 0;
2560
2561 for (col = 0; col < screen_Columns; ++col)
2562 {
2563 int off = line * screen_Columns + col;
2564
2565 if (popup_mask[off] != popup_mask_next[off])
2566 {
2567 popup_mask[off] = popup_mask_next[off];
2568
2569 if (line >= cmdline_row)
2570 {
2571 // the command line needs to be cleared if text below
2572 // the popup is now visible.
2573 if (!msg_scrolled && popup_mask_next[off] == 0)
2574 clear_cmdline = TRUE;
2575 }
2576 else if (col >= col_done)
2577 {
2578 linenr_T lnum;
2579 int line_cp = line;
2580 int col_cp = col;
2581
2582 // The screen position "line" / "col" needs to be
2583 // redrawn. Figure out what window that is and update
2584 // w_redraw_top and w_redr_bot. Only needs to be done
2585 // once for each window line.
2586 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2587 if (wp != NULL)
2588 {
2589 if (line_cp >= wp->w_height)
2590 // In (or below) status line
2591 wp->w_redr_status = TRUE;
2592 // compute the position in the buffer line from the
2593 // position on the screen
2594 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2595 &lnum))
2596 // past bottom
2597 wp->w_redr_status = TRUE;
2598 else
2599 redrawWinline(wp, lnum);
2600
2601 // This line is going to be redrawn, no need to
2602 // check until the right side of the window.
2603 col_done = wp->w_wincol + wp->w_width - 1;
2604 }
2605 }
2606 }
2607 }
2608 }
2609}
2610
2611/*
2612 * Return a string of "len" spaces in IObuff.
2613 */
2614 static char_u *
2615get_spaces(int len)
2616{
2617 vim_memset(IObuff, ' ', (size_t)len);
2618 IObuff[len] = NUL;
2619 return IObuff;
2620}
2621
2622/*
2623 * Update popup windows. They are drawn on top of normal windows.
2624 * "win_update" is called for each popup window, lowest zindex first.
2625 */
2626 void
2627update_popups(void (*win_update)(win_T *wp))
2628{
2629 win_T *wp;
2630 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002631 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002632 int total_width;
2633 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002634 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002635 int popup_attr;
2636 int border_attr[4];
2637 int border_char[8];
2638 char_u buf[MB_MAXBYTES];
2639 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002640 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002641 int padcol = 0;
2642 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002643 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002644 int sb_thumb_top = 0;
2645 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002646 int attr_scroll = 0;
2647 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002648
2649 // Find the window with the lowest zindex that hasn't been updated yet,
2650 // so that the window with a higher zindex is drawn later, thus goes on
2651 // top.
2652 popup_reset_handled();
2653 while ((wp = find_next_popup(TRUE)) != NULL)
2654 {
2655 // This drawing uses the zindex of the popup window, so that it's on
2656 // top of the text but doesn't draw when another popup with higher
2657 // zindex is on top of the character.
2658 screen_zindex = wp->w_zindex;
2659
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002660 // Set flags in popup_transparent[] for masked cells.
2661 update_popup_transparent(wp, 1);
2662
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002663 // adjust w_winrow and w_wincol for border and padding, since
2664 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002665 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002666 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2667 - wp->w_popup_leftoff;
2668 if (wp->w_wincol + left_extra < 0)
2669 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002670 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002671 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002672
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002673 // Draw the popup text, unless it's off screen.
2674 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2675 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002676
2677 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002678 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002679
Bram Moolenaard529ba52019-07-02 23:13:53 +02002680 total_width = popup_width(wp);
2681 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002682 popup_attr = get_wcr_attr(wp);
2683
2684 // We can only use these line drawing characters when 'encoding' is
2685 // "utf-8" and 'ambiwidth' is "single".
2686 if (enc_utf8 && *p_ambw == 's')
2687 {
2688 border_char[0] = border_char[2] = 0x2550;
2689 border_char[1] = border_char[3] = 0x2551;
2690 border_char[4] = 0x2554;
2691 border_char[5] = 0x2557;
2692 border_char[6] = 0x255d;
2693 border_char[7] = 0x255a;
2694 }
2695 else
2696 {
2697 border_char[0] = border_char[2] = '-';
2698 border_char[1] = border_char[3] = '|';
2699 for (i = 4; i < 8; ++i)
2700 border_char[i] = '+';
2701 }
2702 for (i = 0; i < 8; ++i)
2703 if (wp->w_border_char[i] != 0)
2704 border_char[i] = wp->w_border_char[i];
2705
2706 for (i = 0; i < 4; ++i)
2707 {
2708 border_attr[i] = popup_attr;
2709 if (wp->w_border_highlight[i] != NULL)
2710 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2711 }
2712
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002713 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002714 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002715 if (wp->w_popup_border[0] > 0)
2716 {
2717 // top border
2718 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002719 wincol < 0 ? 0 : wincol, wincol + total_width,
2720 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002721 ? border_char[4] : border_char[0],
2722 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002723 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002724 {
2725 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2726 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002727 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002728 }
2729 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002730 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2731 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002732
Bram Moolenaard529ba52019-07-02 23:13:53 +02002733 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2734 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002735 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002736 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2737 - wp->w_has_scrollbar;
2738 if (padcol < 0)
2739 {
2740 padwidth += padcol;
2741 padcol = 0;
2742 }
2743 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002744 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002745 {
2746 // top padding
2747 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002748 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002749 ' ', ' ', popup_attr);
2750 }
2751
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002752 // Title goes on top of border or padding.
2753 if (wp->w_popup_title != NULL)
2754 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2755 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2756
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002757 // Compute scrollbar thumb position and size.
2758 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002759 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002760 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2761
Bram Moolenaar68acb412019-06-26 03:40:36 +02002762 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2763 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002764 if (sb_thumb_height == 0)
2765 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002766 if (linecount <= wp->w_height)
2767 // it just fits, avoid divide by zero
2768 sb_thumb_top = 0;
2769 else
2770 sb_thumb_top = (wp->w_topline - 1
2771 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002772 * (wp->w_height - sb_thumb_height)
2773 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002774 if (wp->w_scrollbar_highlight != NULL)
2775 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2776 else
2777 attr_scroll = highlight_attr[HLF_PSB];
2778 if (wp->w_thumb_highlight != NULL)
2779 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2780 else
2781 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002782 }
2783
2784 for (i = wp->w_popup_border[0];
2785 i < total_height - wp->w_popup_border[2]; ++i)
2786 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002787 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002788 // left and right padding only needed next to the body
2789 int do_padding =
2790 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2791 && i < total_height - wp->w_popup_border[2]
2792 - wp->w_popup_padding[2];
2793
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002794 row = wp->w_winrow + i;
2795
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002796 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002797 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002798 {
2799 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002800 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002801 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002802 if (do_padding && wp->w_popup_padding[3] > 0)
2803 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002804 int col = wincol + wp->w_popup_border[3];
2805
Bram Moolenaard529ba52019-07-02 23:13:53 +02002806 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002807 pad_left = wp->w_popup_padding[3];
2808 if (col < 0)
2809 {
2810 pad_left += col;
2811 col = 0;
2812 }
2813 if (pad_left > 0)
2814 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2815 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002816 // scrollbar
2817 if (wp->w_has_scrollbar)
2818 {
2819 int line = i - top_off;
2820 int scroll_col = wp->w_wincol + total_width - 1
2821 - wp->w_popup_border[1];
2822
2823 if (line >= 0 && line < wp->w_height)
2824 screen_putchar(' ', row, scroll_col,
2825 line >= sb_thumb_top
2826 && line < sb_thumb_top + sb_thumb_height
2827 ? attr_thumb : attr_scroll);
2828 else
2829 screen_putchar(' ', row, scroll_col, popup_attr);
2830 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002831 // right border
2832 if (wp->w_popup_border[1] > 0)
2833 {
2834 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002835 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002836 }
2837 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002838 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002839 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002840 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002841 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2842 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002843 }
2844
2845 if (wp->w_popup_padding[2] > 0)
2846 {
2847 // bottom padding
2848 row = wp->w_winrow + wp->w_popup_border[0]
2849 + wp->w_popup_padding[0] + wp->w_height;
2850 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002851 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002852 }
2853
2854 if (wp->w_popup_border[2] > 0)
2855 {
2856 // bottom border
2857 row = wp->w_winrow + total_height - 1;
2858 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002859 wincol < 0 ? 0 : wincol,
2860 wincol + total_width,
2861 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002862 ? border_char[7] : border_char[2],
2863 border_char[2], border_attr[2]);
2864 if (wp->w_popup_border[1] > 0)
2865 {
2866 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002867 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002868 }
2869 }
2870
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002871 if (wp->w_popup_close == POPCLOSE_BUTTON)
2872 {
2873 // close button goes on top of anything at the top-right corner
2874 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002875 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002876 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2877 }
2878
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002879 update_popup_transparent(wp, 0);
2880
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002881 // Back to the normal zindex.
2882 screen_zindex = 0;
2883 }
2884}
2885
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002886/*
2887 * Mark references in callbacks of one popup window.
2888 */
2889 static int
2890set_ref_in_one_popup(win_T *wp, int copyID)
2891{
2892 int abort = FALSE;
2893 typval_T tv;
2894
2895 if (wp->w_close_cb.cb_partial != NULL)
2896 {
2897 tv.v_type = VAR_PARTIAL;
2898 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2899 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2900 }
2901 if (wp->w_filter_cb.cb_partial != NULL)
2902 {
2903 tv.v_type = VAR_PARTIAL;
2904 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2905 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2906 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002907 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002908 return abort;
2909}
2910
2911/*
2912 * Set reference in callbacks of popup windows.
2913 */
2914 int
2915set_ref_in_popups(int copyID)
2916{
2917 int abort = FALSE;
2918 win_T *wp;
2919 tabpage_T *tp;
2920
2921 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2922 abort = abort || set_ref_in_one_popup(wp, copyID);
2923
2924 FOR_ALL_TABPAGES(tp)
2925 {
2926 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2927 abort = abort || set_ref_in_one_popup(wp, copyID);
2928 if (abort)
2929 break;
2930 }
2931 return abort;
2932}
Bram Moolenaar79648732019-07-18 21:43:07 +02002933
2934/*
2935 * Find an existing popup used as the preview window, in the current tab page.
2936 * Return NULL if not found.
2937 */
2938 win_T *
2939popup_find_preview_window(void)
2940{
2941 win_T *wp;
2942
2943 // Preview window popup is always local to tab page.
2944 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2945 if (wp->w_p_pvw)
2946 return wp;
2947 return wp;
2948}
2949
2950 int
2951popup_is_popup(win_T *wp)
2952{
2953 return wp->w_popup_flags != 0;
2954}
2955
2956/*
2957 * Create a popup to be used as the preview window.
2958 * NOTE: this makes the popup the current window, so that the file can be
2959 * edited. However, it must not remain to be the current window, the caller
2960 * must make sure of that.
2961 */
2962 int
2963popup_create_preview_window(void)
2964{
2965 win_T *wp = popup_create(NULL, NULL, TYPE_PREVIEW);
2966
2967 if (wp == NULL)
2968 return FAIL;
2969 wp->w_p_pvw = TRUE;
2970
2971 // Set the width to a reasonable value, so that w_topline can be computed.
2972 if (wp->w_minwidth > 0)
2973 wp->w_width = wp->w_minwidth;
2974 else if (wp->w_maxwidth > 0)
2975 wp->w_width = wp->w_maxwidth;
2976 else
2977 wp->w_width = curwin->w_width;
2978
2979 // Will switch to another buffer soon, dummy one can be wiped.
2980 wp->w_buffer->b_locked = FALSE;
2981
2982 win_enter(wp, FALSE);
2983 return OK;
2984}
2985
2986 void
2987popup_close_preview()
2988{
2989 win_T *wp = popup_find_preview_window();
2990
2991 if (wp != NULL)
2992 {
2993 typval_T res;
2994
2995 res.v_type = VAR_NUMBER;
2996 res.vval.v_number = -1;
2997 popup_close_and_callback(wp, &res);
2998 }
2999}
3000
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003001#endif // FEAT_TEXT_PROP