blob: 4113b55679edb868bde52acd771bae87fa40c0ce [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;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200454
455 // Don't use "firstline" now.
456 wp->w_firstline = 0;
457}
458
459/*
460 * Get the sign group name for window "wp".
461 * Returns a pointer to a static buffer, overwritten on the next call.
462 */
463 static char_u *
464popup_get_sign_name(win_T *wp)
465{
466 static char buf[30];
467
468 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
469 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200470}
471
472/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200473 * Highlight the line with the cursor.
474 * Also scrolls the text to put the cursor line in view.
475 */
476 static void
477popup_highlight_curline(win_T *wp)
478{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200479 int sign_id = 0;
480 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200481
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200482 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200483
484 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
485 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200486 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200487
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200488 if (!sign_exists_by_name(sign_name))
489 {
490 char *linehl = "PopupSelected";
491
492 if (syn_name2id((char_u *)linehl) == 0)
493 linehl = "PmenuSel";
494 sign_define_by_name(sign_name, NULL,
495 (char_u *)linehl, NULL, NULL);
496 }
497
498 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
499 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
500 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200501 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200502 else
503 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200504}
505
506/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200507 * Shared between popup_create() and f_popup_setoptions().
508 */
509 static void
510apply_general_options(win_T *wp, dict_T *dict)
511{
512 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200513 int nr;
514 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 // TODO: flip
517
518 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200519 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200520 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
521 if (wp->w_firstline < 1)
522 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200523
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200524 di = dict_find(dict, (char_u *)"scrollbar", -1);
525 if (di != NULL)
526 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
527
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200528 str = dict_get_string(dict, (char_u *)"title", FALSE);
529 if (str != NULL)
530 {
531 vim_free(wp->w_popup_title);
532 wp->w_popup_title = vim_strsave(str);
533 }
534
Bram Moolenaar402502d2019-05-30 22:07:36 +0200535 di = dict_find(dict, (char_u *)"wrap", -1);
536 if (di != NULL)
537 {
538 nr = dict_get_number(dict, (char_u *)"wrap");
539 wp->w_p_wrap = nr != 0;
540 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200541
Bram Moolenaara42d9452019-06-15 21:46:30 +0200542 di = dict_find(dict, (char_u *)"drag", -1);
543 if (di != NULL)
544 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200545
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200546 di = dict_find(dict, (char_u *)"close", -1);
547 if (di != NULL)
548 {
549 int ok = TRUE;
550
551 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
552 {
553 char_u *s = di->di_tv.vval.v_string;
554
555 if (STRCMP(s, "none") == 0)
556 wp->w_popup_close = POPCLOSE_NONE;
557 else if (STRCMP(s, "button") == 0)
558 wp->w_popup_close = POPCLOSE_BUTTON;
559 else if (STRCMP(s, "click") == 0)
560 wp->w_popup_close = POPCLOSE_CLICK;
561 else
562 ok = FALSE;
563 }
564 else
565 ok = FALSE;
566 if (!ok)
567 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
568 }
569
Bram Moolenaarae943152019-06-16 22:54:14 +0200570 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
571 if (str != NULL)
572 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
573 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200574
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200575 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
576 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200577 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
578 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200579
Bram Moolenaar790498b2019-06-01 22:15:29 +0200580 di = dict_find(dict, (char_u *)"borderhighlight", -1);
581 if (di != NULL)
582 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200583 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200584 emsg(_(e_listreq));
585 else
586 {
587 list_T *list = di->di_tv.vval.v_list;
588 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200589 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200590
Bram Moolenaar403d0902019-07-17 21:37:32 +0200591 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
592 ++i, li = li->li_next)
593 {
594 str = tv_get_string(&li->li_tv);
595 if (*str != NUL)
596 wp->w_border_highlight[i] = vim_strsave(str);
597 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200598 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
599 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200600 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200601 vim_strsave(wp->w_border_highlight[0]);
602 }
603 }
604
Bram Moolenaar790498b2019-06-01 22:15:29 +0200605 di = dict_find(dict, (char_u *)"borderchars", -1);
606 if (di != NULL)
607 {
608 if (di->di_tv.v_type != VAR_LIST)
609 emsg(_(e_listreq));
610 else
611 {
612 list_T *list = di->di_tv.vval.v_list;
613 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200614 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200615
616 if (list != NULL)
617 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
618 ++i, li = li->li_next)
619 {
620 str = tv_get_string(&li->li_tv);
621 if (*str != NUL)
622 wp->w_border_char[i] = mb_ptr2char(str);
623 }
624 if (list->lv_len == 1)
625 for (i = 1; i < 8; ++i)
626 wp->w_border_char[i] = wp->w_border_char[0];
627 if (list->lv_len == 2)
628 {
629 for (i = 4; i < 8; ++i)
630 wp->w_border_char[i] = wp->w_border_char[1];
631 for (i = 1; i < 4; ++i)
632 wp->w_border_char[i] = wp->w_border_char[0];
633 }
634 }
635 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200636
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200637 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
638 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
639
Bram Moolenaarae943152019-06-16 22:54:14 +0200640 di = dict_find(dict, (char_u *)"zindex", -1);
641 if (di != NULL)
642 {
643 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
644 if (wp->w_zindex < 1)
645 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
646 if (wp->w_zindex > 32000)
647 wp->w_zindex = 32000;
648 }
649
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200650 di = dict_find(dict, (char_u *)"mask", -1);
651 if (di != NULL)
652 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200653 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200654
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200655 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200656 {
657 listitem_T *li;
658
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200659 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200660 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
661 li = li->li_next)
662 {
663 if (li->li_tv.v_type != VAR_LIST
664 || li->li_tv.vval.v_list == NULL
665 || li->li_tv.vval.v_list->lv_len != 4)
666 {
667 ok = FALSE;
668 break;
669 }
670 }
671 }
672 if (ok)
673 {
674 wp->w_popup_mask = di->di_tv.vval.v_list;
675 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200676 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200677 }
678 else
679 semsg(_(e_invargval), "mask");
680 }
681
Bram Moolenaarae943152019-06-16 22:54:14 +0200682#if defined(FEAT_TIMERS)
683 // Add timer to close the popup after some time.
684 nr = dict_get_number(dict, (char_u *)"time");
685 if (nr > 0)
686 popup_add_timeout(wp, nr);
687#endif
688
Bram Moolenaar3397f742019-06-02 18:40:06 +0200689 di = dict_find(dict, (char_u *)"moved", -1);
690 if (di != NULL)
691 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200692 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200693 handle_moved_argument(wp, di, FALSE);
694 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200695
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200696 di = dict_find(dict, (char_u *)"mousemoved", -1);
697 if (di != NULL)
698 {
699 set_mousemoved_values(wp);
700 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200701 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200702
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200703 di = dict_find(dict, (char_u *)"cursorline", -1);
704 if (di != NULL)
705 {
706 if (di->di_tv.v_type == VAR_NUMBER)
707 {
708 if (di->di_tv.vval.v_number != 0)
709 wp->w_popup_flags |= POPF_CURSORLINE;
710 else
711 wp->w_popup_flags &= ~POPF_CURSORLINE;
712 }
713 else
714 semsg(_(e_invargval), "cursorline");
715 }
716
Bram Moolenaarae943152019-06-16 22:54:14 +0200717 di = dict_find(dict, (char_u *)"filter", -1);
718 if (di != NULL)
719 {
720 callback_T callback = get_callback(&di->di_tv);
721
722 if (callback.cb_name != NULL)
723 {
724 free_callback(&wp->w_filter_cb);
725 set_callback(&wp->w_filter_cb, &callback);
726 }
727 }
728
729 di = dict_find(dict, (char_u *)"callback", -1);
730 if (di != NULL)
731 {
732 callback_T callback = get_callback(&di->di_tv);
733
734 if (callback.cb_name != NULL)
735 {
736 free_callback(&wp->w_close_cb);
737 set_callback(&wp->w_close_cb, &callback);
738 }
739 }
740}
741
742/*
743 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200744 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200745 */
746 static void
747apply_options(win_T *wp, dict_T *dict)
748{
749 int nr;
750
751 apply_move_options(wp, dict);
752 apply_general_options(wp, dict);
753
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200754 nr = dict_get_number(dict, (char_u *)"hidden");
755 if (nr > 0)
756 {
757 wp->w_popup_flags |= POPF_HIDDEN;
758 --wp->w_buffer->b_nwindows;
759 }
760
Bram Moolenaar33796b32019-06-08 16:01:13 +0200761 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200762 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200763}
764
765/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200766 * Add lines to the popup from a list of strings.
767 */
768 static void
769add_popup_strings(buf_T *buf, list_T *l)
770{
771 listitem_T *li;
772 linenr_T lnum = 0;
773 char_u *p;
774
775 for (li = l->lv_first; li != NULL; li = li->li_next)
776 if (li->li_tv.v_type == VAR_STRING)
777 {
778 p = li->li_tv.vval.v_string;
779 ml_append_buf(buf, lnum++,
780 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
781 }
782}
783
784/*
785 * Add lines to the popup from a list of dictionaries.
786 */
787 static void
788add_popup_dicts(buf_T *buf, list_T *l)
789{
790 listitem_T *li;
791 listitem_T *pli;
792 linenr_T lnum = 0;
793 char_u *p;
794 dict_T *dict;
795
796 // first add the text lines
797 for (li = l->lv_first; li != NULL; li = li->li_next)
798 {
799 if (li->li_tv.v_type != VAR_DICT)
800 {
801 emsg(_(e_dictreq));
802 return;
803 }
804 dict = li->li_tv.vval.v_dict;
805 p = dict == NULL ? NULL
806 : dict_get_string(dict, (char_u *)"text", FALSE);
807 ml_append_buf(buf, lnum++,
808 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
809 }
810
811 // add the text properties
812 lnum = 1;
813 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
814 {
815 dictitem_T *di;
816 list_T *plist;
817
818 dict = li->li_tv.vval.v_dict;
819 di = dict_find(dict, (char_u *)"props", -1);
820 if (di != NULL)
821 {
822 if (di->di_tv.v_type != VAR_LIST)
823 {
824 emsg(_(e_listreq));
825 return;
826 }
827 plist = di->di_tv.vval.v_list;
828 if (plist != NULL)
829 {
830 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
831 {
832 if (pli->li_tv.v_type != VAR_DICT)
833 {
834 emsg(_(e_dictreq));
835 return;
836 }
837 dict = pli->li_tv.vval.v_dict;
838 if (dict != NULL)
839 {
840 int col = dict_get_number(dict, (char_u *)"col");
841
842 prop_add_common( lnum, col, dict, buf, NULL);
843 }
844 }
845 }
846 }
847 }
848}
849
850/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200851 * Get the padding plus border at the top, adjusted to 1 if there is a title.
852 */
853 static int
854popup_top_extra(win_T *wp)
855{
856 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
857
858 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
859 return 1;
860 return extra;
861}
862
863/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200864 * Return the height of popup window "wp", including border and padding.
865 */
866 int
867popup_height(win_T *wp)
868{
869 return wp->w_height
870 + popup_top_extra(wp)
871 + wp->w_popup_padding[2] + wp->w_popup_border[2];
872}
873
874/*
875 * Return the width of popup window "wp", including border, padding and
876 * scrollbar.
877 */
878 int
879popup_width(win_T *wp)
880{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200881 // w_leftcol is how many columns of the core are left of the screen
882 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200883 return wp->w_width + wp->w_leftcol
884 + wp->w_popup_padding[3] + wp->w_popup_border[3]
885 + wp->w_popup_padding[1] + wp->w_popup_border[1]
886 + wp->w_has_scrollbar
887 + wp->w_popup_rightoff;
888}
889
890/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200891 * Adjust the position and size of the popup to fit on the screen.
892 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200893 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200894popup_adjust_position(win_T *wp)
895{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200896 linenr_T lnum;
897 int wrapped = 0;
898 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200899 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200900 int center_vert = FALSE;
901 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200902 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200903 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200904 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
905 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
906 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
907 int extra_height = top_extra + bot_extra;
908 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200909 int org_winrow = wp->w_winrow;
910 int org_wincol = wp->w_wincol;
911 int org_width = wp->w_width;
912 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200913 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200914 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200915 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200916
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200917 wp->w_winrow = 0;
918 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200919 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200920 wp->w_popup_leftoff = 0;
921 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200922 if (wp->w_popup_pos == POPPOS_CENTER)
923 {
924 // center after computing the size
925 center_vert = TRUE;
926 center_hor = TRUE;
927 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200928 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200929 {
930 if (wp->w_wantline == 0)
931 center_vert = TRUE;
932 else if (wp->w_popup_pos == POPPOS_TOPLEFT
933 || wp->w_popup_pos == POPPOS_TOPRIGHT)
934 {
935 wp->w_winrow = wp->w_wantline - 1;
936 if (wp->w_winrow >= Rows)
937 wp->w_winrow = Rows - 1;
938 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200939
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200940 if (wp->w_wantcol == 0)
941 center_hor = TRUE;
942 else if (wp->w_popup_pos == POPPOS_TOPLEFT
943 || wp->w_popup_pos == POPPOS_BOTLEFT)
944 {
945 wp->w_wincol = wp->w_wantcol - 1;
946 if (wp->w_wincol >= Columns - 3)
947 wp->w_wincol = Columns - 3;
948 }
949 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200950
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200951 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200952 // When left aligned use the space available, but shift to the left when we
953 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200954 maxspace = Columns - wp->w_wincol - left_extra;
955 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200956 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200957 {
958 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200959 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200960 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200961
Bram Moolenaar8d241042019-06-12 23:40:01 +0200962 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +0200963 if (wp->w_firstline != 0)
964 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200965 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
966 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
967
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200968 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200969 // Use a minimum width of one, so that something shows when there is no
970 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200971 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200972 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200973 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200974 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200975 int len;
976 int w_width = wp->w_width;
977
978 // Count Tabs for what they are worth and compute the length based on
979 // the maximum width (matters when 'showbreak' is set).
980 if (wp->w_width < maxwidth)
981 wp->w_width = maxwidth;
982 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200983 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200984 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200985
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200986 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200987 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200988 while (len > maxwidth)
989 {
990 ++wrapped;
991 len -= maxwidth;
992 wp->w_width = maxwidth;
993 }
994 }
995 else if (len > maxwidth
996 && allow_adjust_left
997 && (wp->w_popup_pos == POPPOS_TOPLEFT
998 || wp->w_popup_pos == POPPOS_BOTLEFT))
999 {
1000 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001001 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001002
Bram Moolenaar51c31312019-06-15 22:27:23 +02001003 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001004 {
1005 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001006
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001007 len -= truncate_shift;
1008 shift_by -= truncate_shift;
1009 }
1010
1011 wp->w_wincol -= shift_by;
1012 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001013 wp->w_width = maxwidth;
1014 }
1015 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001016 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001017 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001018 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1019 wp->w_width = wp->w_maxwidth;
1020 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001021 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001022 if (wp->w_maxheight > 0
1023 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001024 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001025 }
1026
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001027 wp->w_has_scrollbar = wp->w_want_scrollbar
1028 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001029 if (wp->w_has_scrollbar)
1030 ++right_extra;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001031
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001032 minwidth = wp->w_minwidth;
1033 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1034 {
1035 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1036
1037 if (minwidth < title_len)
1038 minwidth = title_len;
1039 }
1040
1041 if (minwidth > 0 && wp->w_width < minwidth)
1042 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001043 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001044 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001045 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001046 // some columns cut off on the right
1047 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001048 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001049 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001050 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001051 {
1052 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1053 if (wp->w_wincol < 0)
1054 wp->w_wincol = 0;
1055 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001056 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1057 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1058 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001059 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1060
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001061 // Right aligned: move to the right if needed.
1062 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001063 if (leftoff >= 0)
1064 wp->w_wincol = leftoff;
1065 else if (wp->w_popup_fixed)
1066 {
1067 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001068 // columns when displaying the window, minus left border and
1069 // padding.
1070 if (-leftoff > left_extra)
1071 wp->w_leftcol = -leftoff - left_extra;
1072 wp->w_width -= wp->w_leftcol;
1073 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001074 if (wp->w_width < 0)
1075 wp->w_width = 0;
1076 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001077 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001078
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001079 if (wp->w_p_wrap)
1080 {
1081 int want_col = 0;
1082
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001083 // try to show the right border and any scrollbar
1084 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001085 if (want_col > 0 && wp->w_wincol > 0
1086 && wp->w_wincol + want_col >= Columns)
1087 {
1088 wp->w_wincol = Columns - want_col;
1089 if (wp->w_wincol < 0)
1090 wp->w_wincol = 0;
1091 }
1092 }
1093
Bram Moolenaar8d241042019-06-12 23:40:01 +02001094 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1095 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001096 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1097 wp->w_height = wp->w_minheight;
1098 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1099 wp->w_height = wp->w_maxheight;
1100 if (wp->w_height > Rows - wp->w_winrow)
1101 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001102
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001103 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001104 {
1105 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1106 if (wp->w_winrow < 0)
1107 wp->w_winrow = 0;
1108 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001109 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1110 || wp->w_popup_pos == POPPOS_BOTLEFT)
1111 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001112 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001113 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001114 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001115 else
1116 // not enough space, make top aligned
1117 wp->w_winrow = wp->w_wantline + 1;
1118 }
1119
Bram Moolenaar17146962019-05-30 00:12:11 +02001120 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001121
1122 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001123 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001124 if (org_winrow != wp->w_winrow
1125 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001126 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001127 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001128 || org_width != wp->w_width
1129 || org_height != wp->w_height)
1130 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001131 redraw_all_later(VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001132 redraw_win_later(wp, NOT_VALID);
1133 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1134 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001135 popup_mask_refresh = TRUE;
1136 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001137}
1138
Bram Moolenaar17627312019-06-02 19:53:44 +02001139typedef enum
1140{
1141 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001142 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001143 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001144 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001145 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001146 TYPE_MENU,
1147 TYPE_PREVIEW
Bram Moolenaar17627312019-06-02 19:53:44 +02001148} create_type_T;
1149
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001150/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001151 * Make "buf" empty and set the contents to "text".
1152 * Used by popup_create() and popup_settext().
1153 */
1154 static void
1155popup_set_buffer_text(buf_T *buf, typval_T text)
1156{
1157 int lnum;
1158
1159 // Clear the buffer, then replace the lines.
1160 curbuf = buf;
1161 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1162 ml_delete(lnum, FALSE);
1163 curbuf = curwin->w_buffer;
1164
1165 // Add text to the buffer.
1166 if (text.v_type == VAR_STRING)
1167 {
1168 // just a string
1169 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1170 }
1171 else
1172 {
1173 list_T *l = text.vval.v_list;
1174
1175 if (l->lv_len > 0)
1176 {
1177 if (l->lv_first->li_tv.v_type == VAR_STRING)
1178 // list of strings
1179 add_popup_strings(buf, l);
1180 else
1181 // list of dictionaries
1182 add_popup_dicts(buf, l);
1183 }
1184 }
1185
1186 // delete the line that was in the empty buffer
1187 curbuf = buf;
1188 ml_delete(buf->b_ml.ml_line_count, FALSE);
1189 curbuf = curwin->w_buffer;
1190}
1191
1192/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001193 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1194 * not NULL.
1195 * Return FAIL if the parsing fails.
1196 */
1197 int
1198parse_previewpopup(win_T *wp)
1199{
1200 char_u *p;
1201
1202 for (p = p_pvp; *p != NUL; p += (*p == ',' ? 1 : 0))
1203 {
1204 char_u *e, *dig;
1205 char_u *s = p;
1206 int x;
1207
1208 e = vim_strchr(p, ':');
1209 if (e == NULL || e[1] == NUL)
1210 return FAIL;
1211
1212 p = vim_strchr(e, ',');
1213 if (p == NULL)
1214 p = e + STRLEN(e);
1215 dig = e + 1;
1216 x = getdigits(&dig);
1217 if (dig != p)
1218 return FAIL;
1219
1220 if (STRNCMP(s, "height:", 7) == 0)
1221 {
1222 if (wp != NULL)
1223 {
1224 wp->w_minheight = x;
1225 wp->w_maxheight = x;
1226 }
1227 }
1228 else if (STRNCMP(s, "width:", 6) == 0)
1229 {
1230 if (wp != NULL)
1231 {
1232 wp->w_minwidth = x;
1233 wp->w_maxwidth = x;
1234 }
1235 }
1236 else
1237 return FAIL;
1238 }
1239 return OK;
1240}
1241
1242/*
1243 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001244 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001245 */
1246 void
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001247popup_set_wantpos(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001248{
1249 setcursor_mayforce(TRUE);
1250 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1251 if (wp->w_wantline == 0) // cursor in first line
1252 {
1253 wp->w_wantline = 2;
1254 wp->w_popup_pos = POPPOS_TOPLEFT;
1255 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001256
Bram Moolenaar79648732019-07-18 21:43:07 +02001257 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001258 if (wp->w_wantcol > Columns - width)
1259 {
1260 wp->w_wantcol = Columns - width;
1261 if (wp->w_wantcol < 1)
1262 wp->w_wantcol = 1;
1263 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001264 popup_adjust_position(wp);
1265}
1266
1267/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001268 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001269 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001270 * etc.
1271 * When creating a preview window popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001272 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001273 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001274popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001275{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001276 win_T *wp;
1277 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001278 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001279 int new_buffer;
1280 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001281 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001282 int nr;
1283 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001284
Bram Moolenaar79648732019-07-18 21:43:07 +02001285 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001286 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001287 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001288 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001289 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001290 buf = buflist_findnr( argvars[0].vval.v_number);
1291 if (buf == NULL)
1292 {
1293 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1294 return NULL;
1295 }
1296 }
1297 else if (!(argvars[0].v_type == VAR_STRING
1298 && argvars[0].vval.v_string != NULL)
1299 && !(argvars[0].v_type == VAR_LIST
1300 && argvars[0].vval.v_list != NULL))
1301 {
1302 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001303 return NULL;
1304 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001305 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001306 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001307 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001308 return NULL;
1309 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001310 d = argvars[1].vval.v_dict;
1311 }
1312
1313 if (d != NULL)
1314 {
1315 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1316 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1317 else if (type == TYPE_NOTIFICATION)
1318 tabnr = -1; // notifications are global by default
1319 else
1320 tabnr = 0;
1321 if (tabnr > 0)
1322 {
1323 tp = find_tabpage(tabnr);
1324 if (tp == NULL)
1325 {
1326 semsg(_("E997: Tabpage not found: %d"), tabnr);
1327 return NULL;
1328 }
1329 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001330 }
1331
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001332 // Create the window and buffer.
1333 wp = win_alloc_popup_win();
1334 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001335 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001336 if (rettv != NULL)
1337 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001338 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001339 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001340
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001341 if (buf != NULL)
1342 {
1343 // use existing buffer
1344 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001345 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001346 buffer_ensure_loaded(buf);
1347 }
1348 else
1349 {
1350 // create a new buffer associated with the popup
1351 new_buffer = TRUE;
1352 buf = buflist_new(NULL, NULL, (linenr_T)0,
1353 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1354 if (buf == NULL)
1355 return NULL;
1356 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001357
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001358 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001359
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001360 set_local_options_default(wp);
1361 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001362 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001363 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001364 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001365 buf->b_p_ul = -1; // no undo
1366 buf->b_p_swf = FALSE; // no swap file
1367 buf->b_p_bl = FALSE; // unlisted buffer
1368 buf->b_locked = TRUE;
1369 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001370
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001371 // Avoid that 'buftype' is reset when this buffer is entered.
1372 buf->b_p_initialized = TRUE;
1373 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001374
Bram Moolenaara3fce622019-06-20 02:31:49 +02001375 if (tp != NULL)
1376 {
1377 // popup on specified tab page
1378 wp->w_next = tp->tp_first_popupwin;
1379 tp->tp_first_popupwin = wp;
1380 }
1381 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001382 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001383 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001384 wp->w_next = curtab->tp_first_popupwin;
1385 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001386 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001387 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001388 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001389 win_T *prev = first_popupwin;
1390
1391 // Global popup: add at the end, so that it gets displayed on top of
1392 // older ones with the same zindex. Matters for notifications.
1393 if (first_popupwin == NULL)
1394 first_popupwin = wp;
1395 else
1396 {
1397 while (prev->w_next != NULL)
1398 prev = prev->w_next;
1399 prev->w_next = wp;
1400 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001401 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001402
Bram Moolenaar79648732019-07-18 21:43:07 +02001403 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001404 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001405
Bram Moolenaar79648732019-07-18 21:43:07 +02001406 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001407 {
1408 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001409 }
1410 if (type == TYPE_ATCURSOR)
1411 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001412 popup_set_wantpos(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001413 set_moved_values(wp);
1414 set_moved_columns(wp, FIND_STRING);
1415 }
1416
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001417 if (type == TYPE_BEVAL)
1418 {
1419 wp->w_popup_pos = POPPOS_BOTLEFT;
1420
1421 // by default use the mouse position
1422 wp->w_wantline = mouse_row;
1423 if (wp->w_wantline <= 0) // mouse on first line
1424 {
1425 wp->w_wantline = 2;
1426 wp->w_popup_pos = POPPOS_TOPLEFT;
1427 }
1428 wp->w_wantcol = mouse_col + 1;
1429 set_mousemoved_values(wp);
1430 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1431 }
1432
Bram Moolenaar33796b32019-06-08 16:01:13 +02001433 // set default values
1434 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001435 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001436
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001437 if (type == TYPE_NOTIFICATION)
1438 {
1439 win_T *twp, *nextwin;
1440 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001441
1442 // Try to not overlap with another global popup. Guess we need 3
1443 // more screen lines than buffer lines.
1444 wp->w_wantline = 1;
1445 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1446 {
1447 nextwin = twp->w_next;
1448 if (twp != wp
1449 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1450 && twp->w_winrow <= wp->w_wantline - 1 + height
1451 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1452 {
1453 // move to below this popup and restart the loop to check for
1454 // overlap with other popups
1455 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1456 nextwin = first_popupwin;
1457 }
1458 }
1459 if (wp->w_wantline + height > Rows)
1460 {
1461 // can't avoid overlap, put on top in the hope that message goes
1462 // away soon.
1463 wp->w_wantline = 1;
1464 }
1465
1466 wp->w_wantcol = 10;
1467 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001468 wp->w_minwidth = 20;
1469 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001470 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001471 for (i = 0; i < 4; ++i)
1472 wp->w_popup_border[i] = 1;
1473 wp->w_popup_padding[1] = 1;
1474 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001475
1476 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001477 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001478 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1479 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001480 }
1481
Bram Moolenaara730e552019-06-16 19:05:31 +02001482 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001483 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001484 wp->w_popup_pos = POPPOS_CENTER;
1485 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1486 wp->w_popup_drag = 1;
1487 for (i = 0; i < 4; ++i)
1488 {
1489 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001490 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001491 }
1492 }
1493
Bram Moolenaara730e552019-06-16 19:05:31 +02001494 if (type == TYPE_MENU)
1495 {
1496 typval_T tv;
1497 callback_T callback;
1498
1499 tv.v_type = VAR_STRING;
1500 tv.vval.v_string = (char_u *)"popup_filter_menu";
1501 callback = get_callback(&tv);
1502 if (callback.cb_name != NULL)
1503 set_callback(&wp->w_filter_cb, &callback);
1504
1505 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001506 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001507 }
1508
Bram Moolenaar79648732019-07-18 21:43:07 +02001509 if (type == TYPE_PREVIEW)
1510 {
1511 wp->w_popup_drag = 1;
1512 wp->w_popup_close = POPCLOSE_BUTTON;
1513 for (i = 0; i < 4; ++i)
1514 wp->w_popup_border[i] = 1;
1515 parse_previewpopup(wp);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001516 popup_set_wantpos(wp, wp->w_minwidth);
Bram Moolenaar79648732019-07-18 21:43:07 +02001517 }
1518
Bram Moolenaarae943152019-06-16 22:54:14 +02001519 for (i = 0; i < 4; ++i)
1520 VIM_CLEAR(wp->w_border_highlight[i]);
1521 for (i = 0; i < 8; ++i)
1522 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001523 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001524 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001525
Bram Moolenaar79648732019-07-18 21:43:07 +02001526 if (d != NULL)
1527 // Deal with options.
1528 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001529
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001530#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001531 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1532 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001533#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001534
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001535 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001536
1537 wp->w_vsep_width = 0;
1538
1539 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001540 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001541
1542 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001543}
1544
1545/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001546 * popup_clear()
1547 */
1548 void
1549f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1550{
1551 close_all_popups();
1552}
1553
1554/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001555 * popup_create({text}, {options})
1556 */
1557 void
1558f_popup_create(typval_T *argvars, typval_T *rettv)
1559{
Bram Moolenaar17627312019-06-02 19:53:44 +02001560 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001561}
1562
1563/*
1564 * popup_atcursor({text}, {options})
1565 */
1566 void
1567f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1568{
Bram Moolenaar17627312019-06-02 19:53:44 +02001569 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001570}
1571
1572/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001573 * popup_beval({text}, {options})
1574 */
1575 void
1576f_popup_beval(typval_T *argvars, typval_T *rettv)
1577{
1578 popup_create(argvars, rettv, TYPE_BEVAL);
1579}
1580
1581/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001582 * Invoke the close callback for window "wp" with value "result".
1583 * Careful: The callback may make "wp" invalid!
1584 */
1585 static void
1586invoke_popup_callback(win_T *wp, typval_T *result)
1587{
1588 typval_T rettv;
1589 int dummy;
1590 typval_T argv[3];
1591
1592 argv[0].v_type = VAR_NUMBER;
1593 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1594
1595 if (result != NULL && result->v_type != VAR_UNKNOWN)
1596 copy_tv(result, &argv[1]);
1597 else
1598 {
1599 argv[1].v_type = VAR_NUMBER;
1600 argv[1].vval.v_number = 0;
1601 }
1602
1603 argv[2].v_type = VAR_UNKNOWN;
1604
1605 call_callback(&wp->w_close_cb, -1,
1606 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1607 if (result != NULL)
1608 clear_tv(&argv[1]);
1609 clear_tv(&rettv);
1610}
1611
1612/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001613 * Close popup "wp" and invoke any close callback for it.
1614 */
1615 static void
1616popup_close_and_callback(win_T *wp, typval_T *arg)
1617{
1618 int id = wp->w_id;
1619
1620 if (wp->w_close_cb.cb_name != NULL)
1621 // Careful: This may make "wp" invalid.
1622 invoke_popup_callback(wp, arg);
1623
1624 popup_close(id);
1625}
1626
1627/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001628 * Close popup "wp" because of a mouse click.
1629 */
1630 void
1631popup_close_for_mouse_click(win_T *wp)
1632{
1633 typval_T res;
1634
1635 res.v_type = VAR_NUMBER;
1636 res.vval.v_number = -2;
1637 popup_close_and_callback(wp, &res);
1638}
1639
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001640 static void
1641check_mouse_moved(win_T *wp, win_T *mouse_wp)
1642{
1643 // Close the popup when all if these are true:
1644 // - the mouse is not on this popup
1645 // - "mousemoved" was used
1646 // - the mouse is no longer on the same screen row or the mouse column is
1647 // outside of the relevant text
1648 if (wp != mouse_wp
1649 && wp->w_popup_mouse_row != 0
1650 && (wp->w_popup_mouse_row != mouse_row
1651 || mouse_col < wp->w_popup_mouse_mincol
1652 || mouse_col > wp->w_popup_mouse_maxcol))
1653 {
1654 typval_T res;
1655
1656 res.v_type = VAR_NUMBER;
1657 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001658 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001659 popup_close_and_callback(wp, &res);
1660 }
1661}
1662
1663/*
1664 * Called when the mouse moved: may close a popup with "mousemoved".
1665 */
1666 void
1667popup_handle_mouse_moved(void)
1668{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001669 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001670 win_T *mouse_wp;
1671 int row = mouse_row;
1672 int col = mouse_col;
1673
1674 // find the window where the mouse is in
1675 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1676
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001677 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1678 {
1679 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001680 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001681 }
1682 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1683 {
1684 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001685 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001686 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001687}
1688
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001689/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001690 * In a filter: check if the typed key is a mouse event that is used for
1691 * dragging the popup.
1692 */
1693 static void
1694filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1695{
1696 int row = mouse_row;
1697 int col = mouse_col;
1698
1699 if (wp->w_popup_drag
1700 && is_mouse_key(c)
1701 && (wp == popup_dragwin
1702 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1703 // do not consume the key, allow for dragging the popup
1704 rettv->vval.v_number = 0;
1705}
1706
Bram Moolenaara730e552019-06-16 19:05:31 +02001707/*
1708 * popup_filter_menu({text}, {options})
1709 */
1710 void
1711f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1712{
1713 int id = tv_get_number(&argvars[0]);
1714 win_T *wp = win_id2wp(id);
1715 char_u *key = tv_get_string(&argvars[1]);
1716 typval_T res;
1717 int c;
1718 linenr_T old_lnum;
1719
1720 // If the popup has been closed do not consume the key.
1721 if (wp == NULL)
1722 return;
1723
1724 c = *key;
1725 if (c == K_SPECIAL && key[1] != NUL)
1726 c = TO_SPECIAL(key[1], key[2]);
1727
1728 // consume all keys until done
1729 rettv->vval.v_number = 1;
1730 res.v_type = VAR_NUMBER;
1731
1732 old_lnum = wp->w_cursor.lnum;
1733 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1734 --wp->w_cursor.lnum;
1735 if ((c == 'j' || c == 'J' || c == K_DOWN)
1736 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1737 ++wp->w_cursor.lnum;
1738 if (old_lnum != wp->w_cursor.lnum)
1739 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001740 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001741 return;
1742 }
1743
1744 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1745 {
1746 // Cancelled, invoke callback with -1
1747 res.vval.v_number = -1;
1748 popup_close_and_callback(wp, &res);
1749 return;
1750 }
1751 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1752 {
1753 // Invoke callback with current index.
1754 res.vval.v_number = wp->w_cursor.lnum;
1755 popup_close_and_callback(wp, &res);
1756 return;
1757 }
1758
1759 filter_handle_drag(wp, c, rettv);
1760}
1761
1762/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001763 * popup_filter_yesno({text}, {options})
1764 */
1765 void
1766f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1767{
1768 int id = tv_get_number(&argvars[0]);
1769 win_T *wp = win_id2wp(id);
1770 char_u *key = tv_get_string(&argvars[1]);
1771 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001772 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001773
1774 // If the popup has been closed don't consume the key.
1775 if (wp == NULL)
1776 return;
1777
Bram Moolenaara730e552019-06-16 19:05:31 +02001778 c = *key;
1779 if (c == K_SPECIAL && key[1] != NUL)
1780 c = TO_SPECIAL(key[1], key[2]);
1781
Bram Moolenaara42d9452019-06-15 21:46:30 +02001782 // consume all keys until done
1783 rettv->vval.v_number = 1;
1784
Bram Moolenaara730e552019-06-16 19:05:31 +02001785 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001786 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001787 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001788 res.vval.v_number = 0;
1789 else
1790 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001791 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001792 return;
1793 }
1794
1795 // Invoke callback
1796 res.v_type = VAR_NUMBER;
1797 popup_close_and_callback(wp, &res);
1798}
1799
1800/*
1801 * popup_dialog({text}, {options})
1802 */
1803 void
1804f_popup_dialog(typval_T *argvars, typval_T *rettv)
1805{
1806 popup_create(argvars, rettv, TYPE_DIALOG);
1807}
1808
1809/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001810 * popup_menu({text}, {options})
1811 */
1812 void
1813f_popup_menu(typval_T *argvars, typval_T *rettv)
1814{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001815 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001816}
1817
1818/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001819 * popup_notification({text}, {options})
1820 */
1821 void
1822f_popup_notification(typval_T *argvars, typval_T *rettv)
1823{
1824 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1825}
1826
1827/*
1828 * Find the popup window with window-ID "id".
1829 * If the popup window does not exist NULL is returned.
1830 * If the window is not a popup window, and error message is given.
1831 */
1832 static win_T *
1833find_popup_win(int id)
1834{
1835 win_T *wp = win_id2wp(id);
1836
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001837 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001838 {
1839 semsg(_("E993: window %d is not a popup window"), id);
1840 return NULL;
1841 }
1842 return wp;
1843}
1844
1845/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001846 * popup_close({id})
1847 */
1848 void
1849f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1850{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001851 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001852 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001853
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001854 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001855 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001856}
1857
1858/*
1859 * popup_hide({id})
1860 */
1861 void
1862f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1863{
1864 int id = (int)tv_get_number(argvars);
1865 win_T *wp = find_popup_win(id);
1866
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001867 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001868 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001869 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001870 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001871 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001872 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001873 }
1874}
1875
1876/*
1877 * popup_show({id})
1878 */
1879 void
1880f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1881{
1882 int id = (int)tv_get_number(argvars);
1883 win_T *wp = find_popup_win(id);
1884
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001885 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001886 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001887 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001888 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001889 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001890 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001891 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001892}
1893
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001894/*
1895 * popup_settext({id}, {text})
1896 */
1897 void
1898f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1899{
1900 int id = (int)tv_get_number(&argvars[0]);
1901 win_T *wp = find_popup_win(id);
1902
1903 if (wp != NULL)
1904 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001905 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1906 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1907 else
1908 {
1909 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001910 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001911 popup_adjust_position(wp);
1912 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001913 }
1914}
1915
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001916 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001917popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001918{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001919 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001920 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001921 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001922 clear_cmdline = TRUE;
1923 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001924
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001925 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001926 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001927}
1928
Bram Moolenaarec583842019-05-26 14:11:23 +02001929/*
1930 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001931 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001932 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001933 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001934popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001935{
1936 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001937 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001938 win_T *prev = NULL;
1939
Bram Moolenaarec583842019-05-26 14:11:23 +02001940 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001941 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001942 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001943 {
1944 if (prev == NULL)
1945 first_popupwin = wp->w_next;
1946 else
1947 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001948 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001949 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001950 }
1951
Bram Moolenaarec583842019-05-26 14:11:23 +02001952 // go through tab-local popups
1953 FOR_ALL_TABPAGES(tp)
1954 popup_close_tabpage(tp, id);
1955}
1956
1957/*
1958 * Close a popup window with Window-id "id" in tabpage "tp".
1959 */
1960 void
1961popup_close_tabpage(tabpage_T *tp, int id)
1962{
1963 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001964 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001965 win_T *prev = NULL;
1966
Bram Moolenaarec583842019-05-26 14:11:23 +02001967 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1968 if (wp->w_id == id)
1969 {
1970 if (prev == NULL)
1971 *root = wp->w_next;
1972 else
1973 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001974 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001975 return;
1976 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001977}
1978
1979 void
1980close_all_popups(void)
1981{
1982 while (first_popupwin != NULL)
1983 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001984 while (curtab->tp_first_popupwin != NULL)
1985 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001986}
1987
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001988/*
1989 * popup_move({id}, {options})
1990 */
1991 void
1992f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1993{
Bram Moolenaarae943152019-06-16 22:54:14 +02001994 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001995 int id = (int)tv_get_number(argvars);
1996 win_T *wp = find_popup_win(id);
1997
1998 if (wp == NULL)
1999 return; // invalid {id}
2000
2001 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2002 {
2003 emsg(_(e_dictreq));
2004 return;
2005 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002006 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002007
Bram Moolenaarae943152019-06-16 22:54:14 +02002008 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002009
2010 if (wp->w_winrow + wp->w_height >= cmdline_row)
2011 clear_cmdline = TRUE;
2012 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002013}
2014
Bram Moolenaarbc133542019-05-29 20:26:46 +02002015/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002016 * popup_setoptions({id}, {options})
2017 */
2018 void
2019f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2020{
2021 dict_T *dict;
2022 int id = (int)tv_get_number(argvars);
2023 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002024 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002025
2026 if (wp == NULL)
2027 return; // invalid {id}
2028
2029 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2030 {
2031 emsg(_(e_dictreq));
2032 return;
2033 }
2034 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002035 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002036
2037 apply_move_options(wp, dict);
2038 apply_general_options(wp, dict);
2039
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002040 if (old_firstline != wp->w_firstline)
2041 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002042 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002043 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002044 popup_adjust_position(wp);
2045}
2046
2047/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002048 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002049 */
2050 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002051f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002052{
2053 dict_T *dict;
2054 int id = (int)tv_get_number(argvars);
2055 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002056 int top_extra;
2057 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002058
2059 if (rettv_dict_alloc(rettv) == OK)
2060 {
2061 if (wp == NULL)
2062 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002063 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002064 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2065
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002066 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002067 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002068 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002069
Bram Moolenaarbc133542019-05-29 20:26:46 +02002070 dict_add_number(dict, "line", wp->w_winrow + 1);
2071 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002072 dict_add_number(dict, "width", wp->w_width + left_extra
2073 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2074 dict_add_number(dict, "height", wp->w_height + top_extra
2075 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002076
2077 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2078 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2079 dict_add_number(dict, "core_width", wp->w_width);
2080 dict_add_number(dict, "core_height", wp->w_height);
2081
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002082 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002083 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002084 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002085 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002086
2087 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002088 }
2089}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002090/*
2091 * popup_locate({row}, {col})
2092 */
2093 void
2094f_popup_locate(typval_T *argvars, typval_T *rettv)
2095{
2096 int row = tv_get_number(&argvars[0]) - 1;
2097 int col = tv_get_number(&argvars[1]) - 1;
2098 win_T *wp;
2099
2100 wp = mouse_find_win(&row, &col, FIND_POPUP);
2101 if (WIN_IS_POPUP(wp))
2102 rettv->vval.v_number = wp->w_id;
2103}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002104
2105/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002106 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2107 */
2108 static void
2109get_padding_border(dict_T *dict, int *array, char *name)
2110{
2111 list_T *list;
2112 int i;
2113
2114 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2115 return;
2116
2117 list = list_alloc();
2118 if (list != NULL)
2119 {
2120 dict_add_list(dict, name, list);
2121 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2122 for (i = 0; i < 4; ++i)
2123 list_append_number(list, array[i]);
2124 }
2125}
2126
2127/*
2128 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2129 */
2130 static void
2131get_borderhighlight(dict_T *dict, win_T *wp)
2132{
2133 list_T *list;
2134 int i;
2135
2136 for (i = 0; i < 4; ++i)
2137 if (wp->w_border_highlight[i] != NULL)
2138 break;
2139 if (i == 4)
2140 return;
2141
2142 list = list_alloc();
2143 if (list != NULL)
2144 {
2145 dict_add_list(dict, "borderhighlight", list);
2146 for (i = 0; i < 4; ++i)
2147 list_append_string(list, wp->w_border_highlight[i], -1);
2148 }
2149}
2150
2151/*
2152 * For popup_getoptions(): add a "borderchars" entry to "dict".
2153 */
2154 static void
2155get_borderchars(dict_T *dict, win_T *wp)
2156{
2157 list_T *list;
2158 int i;
2159 char_u buf[NUMBUFLEN];
2160 int len;
2161
2162 for (i = 0; i < 8; ++i)
2163 if (wp->w_border_char[i] != 0)
2164 break;
2165 if (i == 8)
2166 return;
2167
2168 list = list_alloc();
2169 if (list != NULL)
2170 {
2171 dict_add_list(dict, "borderchars", list);
2172 for (i = 0; i < 8; ++i)
2173 {
2174 len = mb_char2bytes(wp->w_border_char[i], buf);
2175 list_append_string(list, buf, len);
2176 }
2177 }
2178}
2179
2180/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002181 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002182 */
2183 static void
2184get_moved_list(dict_T *dict, win_T *wp)
2185{
2186 list_T *list;
2187
2188 list = list_alloc();
2189 if (list != NULL)
2190 {
2191 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002192 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002193 list_append_number(list, wp->w_popup_mincol);
2194 list_append_number(list, wp->w_popup_maxcol);
2195 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002196 list = list_alloc();
2197 if (list != NULL)
2198 {
2199 dict_add_list(dict, "mousemoved", list);
2200 list_append_number(list, wp->w_popup_mouse_row);
2201 list_append_number(list, wp->w_popup_mouse_mincol);
2202 list_append_number(list, wp->w_popup_mouse_maxcol);
2203 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002204}
2205
2206/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002207 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002208 */
2209 void
2210f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2211{
2212 dict_T *dict;
2213 int id = (int)tv_get_number(argvars);
2214 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002215 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002216 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002217
2218 if (rettv_dict_alloc(rettv) == OK)
2219 {
2220 if (wp == NULL)
2221 return;
2222
2223 dict = rettv->vval.v_dict;
2224 dict_add_number(dict, "line", wp->w_wantline);
2225 dict_add_number(dict, "col", wp->w_wantcol);
2226 dict_add_number(dict, "minwidth", wp->w_minwidth);
2227 dict_add_number(dict, "minheight", wp->w_minheight);
2228 dict_add_number(dict, "maxheight", wp->w_maxheight);
2229 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002230 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002231 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002232 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002233 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002234 dict_add_string(dict, "title", wp->w_popup_title);
2235 dict_add_number(dict, "wrap", wp->w_p_wrap);
2236 dict_add_number(dict, "drag", wp->w_popup_drag);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002237 dict_add_number(dict, "cursorline",
2238 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002239 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002240 if (wp->w_scrollbar_highlight != NULL)
2241 dict_add_string(dict, "scrollbarhighlight",
2242 wp->w_scrollbar_highlight);
2243 if (wp->w_thumb_highlight != NULL)
2244 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002245
Bram Moolenaara3fce622019-06-20 02:31:49 +02002246 // find the tabpage that holds this popup
2247 i = 1;
2248 FOR_ALL_TABPAGES(tp)
2249 {
2250 win_T *p;
2251
2252 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2253 if (p->w_id == id)
2254 break;
2255 if (p != NULL)
2256 break;
2257 ++i;
2258 }
2259 if (tp == NULL)
2260 i = -1; // must be global
2261 else if (tp == curtab)
2262 i = 0;
2263 dict_add_number(dict, "tabpage", i);
2264
Bram Moolenaarae943152019-06-16 22:54:14 +02002265 get_padding_border(dict, wp->w_popup_padding, "padding");
2266 get_padding_border(dict, wp->w_popup_border, "border");
2267 get_borderhighlight(dict, wp);
2268 get_borderchars(dict, wp);
2269 get_moved_list(dict, wp);
2270
2271 if (wp->w_filter_cb.cb_name != NULL)
2272 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2273 if (wp->w_close_cb.cb_name != NULL)
2274 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002275
2276 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2277 ++i)
2278 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2279 {
2280 dict_add_string(dict, "pos",
2281 (char_u *)poppos_entries[i].pp_name);
2282 break;
2283 }
2284
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002285 dict_add_string(dict, "close", (char_u *)(
2286 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2287 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2288
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002289# if defined(FEAT_TIMERS)
2290 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2291 ? (long)wp->w_popup_timer->tr_interval : 0L);
2292# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002293 }
2294}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002295
2296 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002297error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002298{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002299 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002300 {
2301 emsg(_("E994: Not allowed in a popup window"));
2302 return TRUE;
2303 }
2304 return FALSE;
2305}
2306
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002307/*
2308 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002309 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002310 */
2311 void
2312popup_reset_handled()
2313{
2314 win_T *wp;
2315
2316 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2317 wp->w_popup_flags &= ~POPF_HANDLED;
2318 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2319 wp->w_popup_flags &= ~POPF_HANDLED;
2320}
2321
2322/*
2323 * Find the next visible popup where POPF_HANDLED is not set.
2324 * Must have called popup_reset_handled() first.
2325 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2326 * popup with the highest zindex.
2327 */
2328 win_T *
2329find_next_popup(int lowest)
2330{
2331 win_T *wp;
2332 win_T *found_wp;
2333 int found_zindex;
2334
2335 found_zindex = lowest ? INT_MAX : 0;
2336 found_wp = NULL;
2337 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2338 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2339 && (lowest ? wp->w_zindex < found_zindex
2340 : wp->w_zindex > found_zindex))
2341 {
2342 found_zindex = wp->w_zindex;
2343 found_wp = wp;
2344 }
2345 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2346 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2347 && (lowest ? wp->w_zindex < found_zindex
2348 : wp->w_zindex > found_zindex))
2349 {
2350 found_zindex = wp->w_zindex;
2351 found_wp = wp;
2352 }
2353
2354 if (found_wp != NULL)
2355 found_wp->w_popup_flags |= POPF_HANDLED;
2356 return found_wp;
2357}
2358
2359/*
2360 * Invoke the filter callback for window "wp" with typed character "c".
2361 * Uses the global "mod_mask" for modifiers.
2362 * Returns the return value of the filter.
2363 * Careful: The filter may make "wp" invalid!
2364 */
2365 static int
2366invoke_popup_filter(win_T *wp, int c)
2367{
2368 int res;
2369 typval_T rettv;
2370 int dummy;
2371 typval_T argv[3];
2372 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002373 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002374
Bram Moolenaara42d9452019-06-15 21:46:30 +02002375 // Emergency exit: CTRL-C closes the popup.
2376 if (c == Ctrl_C)
2377 {
2378 rettv.v_type = VAR_NUMBER;
2379 rettv.vval.v_number = -1;
2380 popup_close_and_callback(wp, &rettv);
2381 return 1;
2382 }
2383
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002384 argv[0].v_type = VAR_NUMBER;
2385 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2386
2387 // Convert the number to a string, so that the function can use:
2388 // if a:c == "\<F2>"
2389 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2390 argv[1].v_type = VAR_STRING;
2391 argv[1].vval.v_string = vim_strsave(buf);
2392
2393 argv[2].v_type = VAR_UNKNOWN;
2394
Bram Moolenaara42d9452019-06-15 21:46:30 +02002395 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002396 call_callback(&wp->w_filter_cb, -1,
2397 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002398 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002399 popup_highlight_curline(wp);
2400
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002401 res = tv_get_number(&rettv);
2402 vim_free(argv[1].vval.v_string);
2403 clear_tv(&rettv);
2404 return res;
2405}
2406
2407/*
2408 * Called when "c" was typed: invoke popup filter callbacks.
2409 * Returns TRUE when the character was consumed,
2410 */
2411 int
2412popup_do_filter(int c)
2413{
2414 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002415 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002416
2417 popup_reset_handled();
2418
2419 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2420 if (wp->w_filter_cb.cb_name != NULL)
2421 res = invoke_popup_filter(wp, c);
2422
2423 return res;
2424}
2425
Bram Moolenaar3397f742019-06-02 18:40:06 +02002426/*
2427 * Called when the cursor moved: check if any popup needs to be closed if the
2428 * cursor moved far enough.
2429 */
2430 void
2431popup_check_cursor_pos()
2432{
2433 win_T *wp;
2434 typval_T tv;
2435
2436 popup_reset_handled();
2437 while ((wp = find_next_popup(TRUE)) != NULL)
2438 if (wp->w_popup_curwin != NULL
2439 && (curwin != wp->w_popup_curwin
2440 || curwin->w_cursor.lnum != wp->w_popup_lnum
2441 || curwin->w_cursor.col < wp->w_popup_mincol
2442 || curwin->w_cursor.col > wp->w_popup_maxcol))
2443 {
2444 tv.v_type = VAR_NUMBER;
2445 tv.vval.v_number = -1;
2446 popup_close_and_callback(wp, &tv);
2447 }
2448}
2449
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002450/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002451 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002452 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002453 static void
2454popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002455{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002456 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002457 char_u *cells;
2458 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002459
2460 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002461 return;
2462 if (wp->w_popup_mask_cells != NULL
2463 && wp->w_popup_mask_height == height
2464 && wp->w_popup_mask_width == width)
2465 return; // cache is still valid
2466
2467 vim_free(wp->w_popup_mask_cells);
2468 wp->w_popup_mask_cells = alloc_clear(width * height);
2469 if (wp->w_popup_mask_cells == NULL)
2470 return;
2471 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002472
2473 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2474 {
2475 int cols, cole;
2476 int lines, linee;
2477
2478 li = lio->li_tv.vval.v_list->lv_first;
2479 cols = tv_get_number(&li->li_tv);
2480 if (cols < 0)
2481 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002482 li = li->li_next;
2483 cole = tv_get_number(&li->li_tv);
2484 if (cole < 0)
2485 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002486 li = li->li_next;
2487 lines = tv_get_number(&li->li_tv);
2488 if (lines < 0)
2489 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002490 li = li->li_next;
2491 linee = tv_get_number(&li->li_tv);
2492 if (linee < 0)
2493 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002494
2495 for (row = lines - 1; row < linee && row < height; ++row)
2496 for (col = cols - 1; col < cole && col < width; ++col)
2497 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002498 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002499}
2500
2501/*
2502 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2503 * "col" and "line" are screen coordinates.
2504 */
2505 static int
2506popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2507{
2508 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2509 int line = screenline - wp->w_winrow;
2510
2511 return col >= 0 && col < width
2512 && line >= 0 && line < height
2513 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002514}
2515
2516/*
2517 * Set flags in popup_transparent[] for window "wp" to "val".
2518 */
2519 static void
2520update_popup_transparent(win_T *wp, int val)
2521{
2522 if (wp->w_popup_mask != NULL)
2523 {
2524 int width = popup_width(wp);
2525 int height = popup_height(wp);
2526 listitem_T *lio, *li;
2527 int cols, cole;
2528 int lines, linee;
2529 int col, line;
2530
2531 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2532 {
2533 li = lio->li_tv.vval.v_list->lv_first;
2534 cols = tv_get_number(&li->li_tv);
2535 if (cols < 0)
2536 cols = width + cols + 1;
2537 li = li->li_next;
2538 cole = tv_get_number(&li->li_tv);
2539 if (cole < 0)
2540 cole = width + cole + 1;
2541 li = li->li_next;
2542 lines = tv_get_number(&li->li_tv);
2543 if (lines < 0)
2544 lines = height + lines + 1;
2545 li = li->li_next;
2546 linee = tv_get_number(&li->li_tv);
2547 if (linee < 0)
2548 linee = height + linee + 1;
2549
2550 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002551 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002552 if (cols < 0)
2553 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002554 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002555 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002556 if (lines < 0)
2557 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002558 for (line = lines; line < linee
2559 && line + wp->w_winrow < screen_Rows; ++line)
2560 for (col = cols; col < cole
2561 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002562 popup_transparent[(line + wp->w_winrow) * screen_Columns
2563 + col + wp->w_wincol] = val;
2564 }
2565 }
2566}
2567
2568/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002569 * Update "popup_mask" if needed.
2570 * Also recomputes the popup size and positions.
2571 * Also updates "popup_visible".
2572 * Also marks window lines for redrawing.
2573 */
2574 void
2575may_update_popup_mask(int type)
2576{
2577 win_T *wp;
2578 short *mask;
2579 int line, col;
2580 int redraw_all = FALSE;
2581
2582 // Need to recompute when switching tabs.
2583 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2584 // (such as the screen size) must have changed.
2585 if (popup_mask_tab != curtab || type >= NOT_VALID)
2586 {
2587 popup_mask_refresh = TRUE;
2588 redraw_all = TRUE;
2589 }
2590 if (!popup_mask_refresh)
2591 {
2592 // Check if any buffer has changed.
2593 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2594 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2595 popup_mask_refresh = TRUE;
2596 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2597 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2598 popup_mask_refresh = TRUE;
2599 if (!popup_mask_refresh)
2600 return;
2601 }
2602
2603 // Need to update the mask, something has changed.
2604 popup_mask_refresh = FALSE;
2605 popup_mask_tab = curtab;
2606 popup_visible = FALSE;
2607
2608 // If redrawing everything, just update "popup_mask".
2609 // If redrawing only what is needed, update "popup_mask_next" and then
2610 // compare with "popup_mask" to see what changed.
2611 if (type >= SOME_VALID)
2612 mask = popup_mask;
2613 else
2614 mask = popup_mask_next;
2615 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2616
2617 // Find the window with the lowest zindex that hasn't been handled yet,
2618 // so that the window with a higher zindex overwrites the value in
2619 // popup_mask.
2620 popup_reset_handled();
2621 while ((wp = find_next_popup(TRUE)) != NULL)
2622 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002623 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002624 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002625
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002626 popup_visible = TRUE;
2627
2628 // Recompute the position if the text changed.
2629 if (redraw_all
2630 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2631 popup_adjust_position(wp);
2632
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002633 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002634 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002635 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002636 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002637 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002638 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002639 col < wp->w_wincol + width - wp->w_popup_leftoff
2640 && col < screen_Columns; ++col)
2641 if (wp->w_popup_mask_cells == NULL
2642 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002643 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002644 }
2645
2646 // Only check which lines are to be updated if not already
2647 // updating all lines.
2648 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002649 {
2650 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2651 win_T *prev_wp = NULL;
2652
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002653 for (line = 0; line < screen_Rows; ++line)
2654 {
2655 int col_done = 0;
2656
2657 for (col = 0; col < screen_Columns; ++col)
2658 {
2659 int off = line * screen_Columns + col;
2660
2661 if (popup_mask[off] != popup_mask_next[off])
2662 {
2663 popup_mask[off] = popup_mask_next[off];
2664
2665 if (line >= cmdline_row)
2666 {
2667 // the command line needs to be cleared if text below
2668 // the popup is now visible.
2669 if (!msg_scrolled && popup_mask_next[off] == 0)
2670 clear_cmdline = TRUE;
2671 }
2672 else if (col >= col_done)
2673 {
2674 linenr_T lnum;
2675 int line_cp = line;
2676 int col_cp = col;
2677
2678 // The screen position "line" / "col" needs to be
2679 // redrawn. Figure out what window that is and update
2680 // w_redraw_top and w_redr_bot. Only needs to be done
2681 // once for each window line.
2682 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2683 if (wp != NULL)
2684 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002685 if (wp != prev_wp)
2686 {
2687 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2688 prev_wp = wp;
2689 }
2690
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002691 if (line_cp >= wp->w_height)
2692 // In (or below) status line
2693 wp->w_redr_status = TRUE;
2694 // compute the position in the buffer line from the
2695 // position on the screen
2696 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002697 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002698 // past bottom
2699 wp->w_redr_status = TRUE;
2700 else
2701 redrawWinline(wp, lnum);
2702
2703 // This line is going to be redrawn, no need to
2704 // check until the right side of the window.
2705 col_done = wp->w_wincol + wp->w_width - 1;
2706 }
2707 }
2708 }
2709 }
2710 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002711
2712 vim_free(plines_cache);
2713 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002714}
2715
2716/*
2717 * Return a string of "len" spaces in IObuff.
2718 */
2719 static char_u *
2720get_spaces(int len)
2721{
2722 vim_memset(IObuff, ' ', (size_t)len);
2723 IObuff[len] = NUL;
2724 return IObuff;
2725}
2726
2727/*
2728 * Update popup windows. They are drawn on top of normal windows.
2729 * "win_update" is called for each popup window, lowest zindex first.
2730 */
2731 void
2732update_popups(void (*win_update)(win_T *wp))
2733{
2734 win_T *wp;
2735 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002736 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002737 int total_width;
2738 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002739 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002740 int popup_attr;
2741 int border_attr[4];
2742 int border_char[8];
2743 char_u buf[MB_MAXBYTES];
2744 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002745 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002746 int padcol = 0;
2747 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002748 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002749 int sb_thumb_top = 0;
2750 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002751 int attr_scroll = 0;
2752 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002753
2754 // Find the window with the lowest zindex that hasn't been updated yet,
2755 // so that the window with a higher zindex is drawn later, thus goes on
2756 // top.
2757 popup_reset_handled();
2758 while ((wp = find_next_popup(TRUE)) != NULL)
2759 {
2760 // This drawing uses the zindex of the popup window, so that it's on
2761 // top of the text but doesn't draw when another popup with higher
2762 // zindex is on top of the character.
2763 screen_zindex = wp->w_zindex;
2764
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002765 // Set flags in popup_transparent[] for masked cells.
2766 update_popup_transparent(wp, 1);
2767
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002768 // adjust w_winrow and w_wincol for border and padding, since
2769 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002770 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002771 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2772 - wp->w_popup_leftoff;
2773 if (wp->w_wincol + left_extra < 0)
2774 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002775 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002776 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002777
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002778 // Draw the popup text, unless it's off screen.
2779 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2780 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002781
2782 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002783 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002784
Bram Moolenaard529ba52019-07-02 23:13:53 +02002785 total_width = popup_width(wp);
2786 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002787 popup_attr = get_wcr_attr(wp);
2788
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002789 if (wp->w_winrow + total_height > cmdline_row)
2790 wp->w_popup_flags |= POPF_ON_CMDLINE;
2791 else
2792 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
2793
2794
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002795 // We can only use these line drawing characters when 'encoding' is
2796 // "utf-8" and 'ambiwidth' is "single".
2797 if (enc_utf8 && *p_ambw == 's')
2798 {
2799 border_char[0] = border_char[2] = 0x2550;
2800 border_char[1] = border_char[3] = 0x2551;
2801 border_char[4] = 0x2554;
2802 border_char[5] = 0x2557;
2803 border_char[6] = 0x255d;
2804 border_char[7] = 0x255a;
2805 }
2806 else
2807 {
2808 border_char[0] = border_char[2] = '-';
2809 border_char[1] = border_char[3] = '|';
2810 for (i = 4; i < 8; ++i)
2811 border_char[i] = '+';
2812 }
2813 for (i = 0; i < 8; ++i)
2814 if (wp->w_border_char[i] != 0)
2815 border_char[i] = wp->w_border_char[i];
2816
2817 for (i = 0; i < 4; ++i)
2818 {
2819 border_attr[i] = popup_attr;
2820 if (wp->w_border_highlight[i] != NULL)
2821 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2822 }
2823
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002824 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002825 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002826 if (wp->w_popup_border[0] > 0)
2827 {
2828 // top border
2829 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002830 wincol < 0 ? 0 : wincol, wincol + total_width,
2831 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002832 ? border_char[4] : border_char[0],
2833 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002834 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002835 {
2836 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2837 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002838 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002839 }
2840 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002841 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2842 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002843
Bram Moolenaard529ba52019-07-02 23:13:53 +02002844 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2845 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002846 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002847 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2848 - wp->w_has_scrollbar;
2849 if (padcol < 0)
2850 {
2851 padwidth += padcol;
2852 padcol = 0;
2853 }
2854 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002855 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002856 {
2857 // top padding
2858 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002859 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002860 ' ', ' ', popup_attr);
2861 }
2862
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002863 // Title goes on top of border or padding.
2864 if (wp->w_popup_title != NULL)
2865 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2866 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2867
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002868 // Compute scrollbar thumb position and size.
2869 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002870 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002871 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2872
Bram Moolenaar68acb412019-06-26 03:40:36 +02002873 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2874 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002875 if (sb_thumb_height == 0)
2876 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002877 if (linecount <= wp->w_height)
2878 // it just fits, avoid divide by zero
2879 sb_thumb_top = 0;
2880 else
2881 sb_thumb_top = (wp->w_topline - 1
2882 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002883 * (wp->w_height - sb_thumb_height)
2884 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002885 if (wp->w_scrollbar_highlight != NULL)
2886 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2887 else
2888 attr_scroll = highlight_attr[HLF_PSB];
2889 if (wp->w_thumb_highlight != NULL)
2890 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2891 else
2892 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002893 }
2894
2895 for (i = wp->w_popup_border[0];
2896 i < total_height - wp->w_popup_border[2]; ++i)
2897 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002898 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002899 // left and right padding only needed next to the body
2900 int do_padding =
2901 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2902 && i < total_height - wp->w_popup_border[2]
2903 - wp->w_popup_padding[2];
2904
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002905 row = wp->w_winrow + i;
2906
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002907 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002908 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002909 {
2910 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002911 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002912 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002913 if (do_padding && wp->w_popup_padding[3] > 0)
2914 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002915 int col = wincol + wp->w_popup_border[3];
2916
Bram Moolenaard529ba52019-07-02 23:13:53 +02002917 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002918 pad_left = wp->w_popup_padding[3];
2919 if (col < 0)
2920 {
2921 pad_left += col;
2922 col = 0;
2923 }
2924 if (pad_left > 0)
2925 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2926 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002927 // scrollbar
2928 if (wp->w_has_scrollbar)
2929 {
2930 int line = i - top_off;
2931 int scroll_col = wp->w_wincol + total_width - 1
2932 - wp->w_popup_border[1];
2933
2934 if (line >= 0 && line < wp->w_height)
2935 screen_putchar(' ', row, scroll_col,
2936 line >= sb_thumb_top
2937 && line < sb_thumb_top + sb_thumb_height
2938 ? attr_thumb : attr_scroll);
2939 else
2940 screen_putchar(' ', row, scroll_col, popup_attr);
2941 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002942 // right border
2943 if (wp->w_popup_border[1] > 0)
2944 {
2945 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002946 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002947 }
2948 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002949 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002950 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002951 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002952 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2953 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002954 }
2955
2956 if (wp->w_popup_padding[2] > 0)
2957 {
2958 // bottom padding
2959 row = wp->w_winrow + wp->w_popup_border[0]
2960 + wp->w_popup_padding[0] + wp->w_height;
2961 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002962 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002963 }
2964
2965 if (wp->w_popup_border[2] > 0)
2966 {
2967 // bottom border
2968 row = wp->w_winrow + total_height - 1;
2969 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002970 wincol < 0 ? 0 : wincol,
2971 wincol + total_width,
2972 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002973 ? border_char[7] : border_char[2],
2974 border_char[2], border_attr[2]);
2975 if (wp->w_popup_border[1] > 0)
2976 {
2977 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002978 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002979 }
2980 }
2981
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002982 if (wp->w_popup_close == POPCLOSE_BUTTON)
2983 {
2984 // close button goes on top of anything at the top-right corner
2985 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002986 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002987 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2988 }
2989
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002990 update_popup_transparent(wp, 0);
2991
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002992 // Back to the normal zindex.
2993 screen_zindex = 0;
2994 }
2995}
2996
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002997/*
2998 * Mark references in callbacks of one popup window.
2999 */
3000 static int
3001set_ref_in_one_popup(win_T *wp, int copyID)
3002{
3003 int abort = FALSE;
3004 typval_T tv;
3005
3006 if (wp->w_close_cb.cb_partial != NULL)
3007 {
3008 tv.v_type = VAR_PARTIAL;
3009 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3010 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3011 }
3012 if (wp->w_filter_cb.cb_partial != NULL)
3013 {
3014 tv.v_type = VAR_PARTIAL;
3015 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3016 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3017 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003018 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003019 return abort;
3020}
3021
3022/*
3023 * Set reference in callbacks of popup windows.
3024 */
3025 int
3026set_ref_in_popups(int copyID)
3027{
3028 int abort = FALSE;
3029 win_T *wp;
3030 tabpage_T *tp;
3031
3032 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3033 abort = abort || set_ref_in_one_popup(wp, copyID);
3034
3035 FOR_ALL_TABPAGES(tp)
3036 {
3037 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3038 abort = abort || set_ref_in_one_popup(wp, copyID);
3039 if (abort)
3040 break;
3041 }
3042 return abort;
3043}
Bram Moolenaar79648732019-07-18 21:43:07 +02003044
3045/*
3046 * Find an existing popup used as the preview window, in the current tab page.
3047 * Return NULL if not found.
3048 */
3049 win_T *
3050popup_find_preview_window(void)
3051{
3052 win_T *wp;
3053
3054 // Preview window popup is always local to tab page.
3055 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3056 if (wp->w_p_pvw)
3057 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003058 return NULL;
3059}
3060
3061 void
3062f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv)
3063{
3064 win_T *wp = popup_find_preview_window();
3065
3066 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003067}
3068
3069 int
3070popup_is_popup(win_T *wp)
3071{
3072 return wp->w_popup_flags != 0;
3073}
3074
3075/*
3076 * Create a popup to be used as the preview window.
3077 * NOTE: this makes the popup the current window, so that the file can be
3078 * edited. However, it must not remain to be the current window, the caller
3079 * must make sure of that.
3080 */
3081 int
3082popup_create_preview_window(void)
3083{
3084 win_T *wp = popup_create(NULL, NULL, TYPE_PREVIEW);
3085
3086 if (wp == NULL)
3087 return FAIL;
3088 wp->w_p_pvw = TRUE;
3089
3090 // Set the width to a reasonable value, so that w_topline can be computed.
3091 if (wp->w_minwidth > 0)
3092 wp->w_width = wp->w_minwidth;
3093 else if (wp->w_maxwidth > 0)
3094 wp->w_width = wp->w_maxwidth;
3095 else
3096 wp->w_width = curwin->w_width;
3097
3098 // Will switch to another buffer soon, dummy one can be wiped.
3099 wp->w_buffer->b_locked = FALSE;
3100
3101 win_enter(wp, FALSE);
3102 return OK;
3103}
3104
3105 void
3106popup_close_preview()
3107{
3108 win_T *wp = popup_find_preview_window();
3109
3110 if (wp != NULL)
3111 {
3112 typval_T res;
3113
3114 res.v_type = VAR_NUMBER;
3115 res.vval.v_number = -1;
3116 popup_close_and_callback(wp, &res);
3117 }
3118}
3119
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003120#endif // FEAT_TEXT_PROP