blob: 76418275bd9a27dc67333f7e3ca19fd04e592022 [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 Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || 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 Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020064 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020065 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
89 emsg(_(e_listreq));
90 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100100 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200101 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
103 ++i, li = li->li_next)
104 {
105 nr = (int)tv_get_number(&li->li_tv);
106 if (nr >= 0)
107 array[i] = nr > max_val ? max_val : nr;
108 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100109 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110 }
111 }
112}
113
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200114/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200115 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200116 */
117 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200118set_moved_values(win_T *wp)
119{
120 wp->w_popup_curwin = curwin;
121 wp->w_popup_lnum = curwin->w_cursor.lnum;
122 wp->w_popup_mincol = curwin->w_cursor.col;
123 wp->w_popup_maxcol = curwin->w_cursor.col;
124}
125
126/*
127 * Used when popup options contain "moved" with "word" or "WORD".
128 */
129 static void
130set_moved_columns(win_T *wp, int flags)
131{
132 char_u *ptr;
133 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
134
135 if (len > 0)
136 {
137 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
138 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
139 }
140}
141
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200143 * Used when popup options contain "mousemoved": set default moved values.
144 */
145 static void
146set_mousemoved_values(win_T *wp)
147{
148 wp->w_popup_mouse_row = mouse_row;
149 wp->w_popup_mouse_mincol = mouse_col;
150 wp->w_popup_mouse_maxcol = mouse_col;
151}
152
153/*
154 * Used when popup options contain "moved" with "word" or "WORD".
155 */
156 static void
157set_mousemoved_columns(win_T *wp, int flags)
158{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160 char_u *text;
161 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 pos_T pos;
163 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164
165 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200166 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200167 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200168 // convert text column to mouse column
169 pos.col = col;
170 pos.coladd = 0;
171 getvcol(textwp, &pos, &mcol, NULL, NULL);
172 wp->w_popup_mouse_mincol = mcol;
173
Bram Moolenaar10727682019-07-12 13:59:20 +0200174 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200175 getvcol(textwp, &pos, NULL, NULL, &mcol);
176 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200177 vim_free(text);
178 }
179}
180
181/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200182 * Return TRUE if "row"/"col" is on the border of the popup.
183 * The values are relative to the top-left corner.
184 */
185 int
186popup_on_border(win_T *wp, int row, int col)
187{
188 return (row == 0 && wp->w_popup_border[0] > 0)
189 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
190 || (col == 0 && wp->w_popup_border[3] > 0)
191 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
192}
193
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
196 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198 * Caller should check the left mouse button was clicked.
199 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200 */
201 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200202popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200203{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200204 if (wp->w_popup_close == POPCLOSE_BUTTON
205 && row == 0 && col == popup_width(wp) - 1)
206 {
207 popup_close_for_mouse_click(wp);
208 return TRUE;
209 }
210 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200211}
212
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200213// Values set when dragging a popup window starts.
214static int drag_start_row;
215static int drag_start_col;
216static int drag_start_wantline;
217static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200218static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200219
220/*
221 * Mouse down on border of popup window: start dragging it.
222 * Uses mouse_col and mouse_row.
223 */
224 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200225popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200226{
227 drag_start_row = mouse_row;
228 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200229 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200230 drag_start_wantline = wp->w_winrow + 1;
231 else
232 drag_start_wantline = wp->w_wantline;
233 if (wp->w_wantcol == 0)
234 drag_start_wantcol = wp->w_wincol + 1;
235 else
236 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200237
238 // Stop centering the popup
239 if (wp->w_popup_pos == POPPOS_CENTER)
240 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241
242 drag_on_resize_handle = wp->w_popup_border[1] > 0
243 && wp->w_popup_border[2] > 0
244 && row == popup_height(wp) - 1
245 && col == popup_width(wp) - 1;
246
247 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
248 {
249 if (wp->w_popup_pos == POPPOS_TOPRIGHT
250 || wp->w_popup_pos == POPPOS_BOTRIGHT)
251 wp->w_wantcol = wp->w_wincol + 1;
252 if (wp->w_popup_pos == POPPOS_BOTLEFT)
253 wp->w_wantline = wp->w_winrow + 1;
254 wp->w_popup_pos = POPPOS_TOPLEFT;
255 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200256}
257
258/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200259 * Mouse moved while dragging a popup window: adjust the window popup position
260 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261 */
262 void
263popup_drag(win_T *wp)
264{
265 // The popup may be closed before dragging stops.
266 if (!win_valid_popup(wp))
267 return;
268
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
270 {
271 int width_inc = mouse_col - drag_start_col;
272 int height_inc = mouse_row - drag_start_row;
273
274 if (width_inc != 0)
275 {
276 int width = wp->w_width + width_inc;
277
278 if (width < 1)
279 width = 1;
280 wp->w_minwidth = width;
281 wp->w_maxwidth = width;
282 drag_start_col = mouse_col;
283 }
284
285 if (height_inc != 0)
286 {
287 int height = wp->w_height + height_inc;
288
289 if (height < 1)
290 height = 1;
291 wp->w_minheight = height;
292 wp->w_maxheight = height;
293 drag_start_row = mouse_row;
294 }
295
296 popup_adjust_position(wp);
297 return;
298 }
299
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000300 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200301 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200302 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
303 if (wp->w_wantline < 1)
304 wp->w_wantline = 1;
305 if (wp->w_wantline > Rows)
306 wp->w_wantline = Rows;
307 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
308 if (wp->w_wantcol < 1)
309 wp->w_wantcol = 1;
310 if (wp->w_wantcol > Columns)
311 wp->w_wantcol = Columns;
312
313 popup_adjust_position(wp);
314}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200315
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200316/*
317 * Set w_firstline to match the current "wp->w_topline".
318 */
319 void
320popup_set_firstline(win_T *wp)
321{
322 int height = wp->w_height;
323
324 wp->w_firstline = wp->w_topline;
325 popup_adjust_position(wp);
326
327 // we don't want the popup to get smaller, decrement the first line
328 // until it doesn't
329 while (wp->w_firstline > 1 && wp->w_height < height)
330 {
331 --wp->w_firstline;
332 popup_adjust_position(wp);
333 }
334}
335
336/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200337 * Return TRUE if the position is in the popup window scrollbar.
338 */
339 int
340popup_is_in_scrollbar(win_T *wp, int row, int col)
341{
342 return wp->w_has_scrollbar
343 && row >= wp->w_popup_border[0]
344 && row < popup_height(wp) - wp->w_popup_border[2]
345 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
346}
347
348
349/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200350 * Handle a click in a popup window, if it is in the scrollbar.
351 */
352 void
353popup_handle_scrollbar_click(win_T *wp, int row, int col)
354{
355 int height = popup_height(wp);
356 int old_topline = wp->w_topline;
357
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200358 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200359 {
360 if (row >= height / 2)
361 {
362 // Click in lower half, scroll down.
363 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
364 ++wp->w_topline;
365 }
366 else if (wp->w_topline > 1)
367 // click on upper half, scroll up.
368 --wp->w_topline;
369 if (wp->w_topline != old_topline)
370 {
371 popup_set_firstline(wp);
372 redraw_win_later(wp, NOT_VALID);
373 }
374 }
375}
376
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200377#if defined(FEAT_TIMERS)
378 static void
379popup_add_timeout(win_T *wp, int time)
380{
381 char_u cbbuf[50];
382 char_u *ptr = cbbuf;
383 typval_T tv;
384
385 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200386 "(_) => popup_close(%d)", wp->w_id);
387 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200388 {
389 wp->w_popup_timer = create_timer(time, 0);
390 wp->w_popup_timer->tr_callback = get_callback(&tv);
391 clear_tv(&tv);
392 }
393}
394#endif
395
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100396 static poppos_T
397get_pos_entry(dict_T *d, int give_error)
398{
399 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
400 int nr;
401
402 if (str == NULL)
403 return POPPOS_NONE;
404
K.Takataeeec2542021-06-02 13:28:16 +0200405 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100406 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
407 return poppos_entries[nr].pp_val;
408
409 if (give_error)
410 semsg(_(e_invarg2), str);
411 return POPPOS_NONE;
412}
413
Bram Moolenaar17627312019-06-02 19:53:44 +0200414/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200415 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200416 */
417 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200418apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200419{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200420 int nr;
421 char_u *str;
422 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200423
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200424 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200425 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200426 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200427 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200428 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200429 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200430 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200431 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200432
433 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200434 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200435 wp->w_wantline = nr;
436 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200437 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200438 wp->w_wantcol = nr;
439
Bram Moolenaar55881332020-08-18 13:04:15 +0200440
441 nr = dict_get_bool(d, (char_u *)"fixed", -1);
442 if (nr != -1)
443 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200444
Bram Moolenaar12034e22019-08-25 22:25:02 +0200445 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100446 poppos_T ppt = get_pos_entry(d, TRUE);
447
448 if (ppt != POPPOS_NONE)
449 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200450 }
451
452 str = dict_get_string(d, (char_u *)"textprop", FALSE);
453 if (str != NULL)
454 {
455 wp->w_popup_prop_type = 0;
456 if (*str != NUL)
457 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100458 wp->w_popup_prop_win = curwin;
459 di = dict_find(d, (char_u *)"textpropwin", -1);
460 if (di != NULL)
461 {
462 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100463 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100464 wp->w_popup_prop_win = curwin;
465 }
466
467 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200468 if (nr <= 0)
469 nr = find_prop_type_id(str, NULL);
470 if (nr <= 0)
471 semsg(_(e_invarg2), str);
472 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200473 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200474 }
475 }
476
477 di = dict_find(d, (char_u *)"textpropid", -1);
478 if (di != NULL)
479 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200480}
481
Bram Moolenaarb0992022020-01-30 14:55:42 +0100482/*
483 * Handle "moved" and "mousemoved" arguments.
484 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200485 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200486handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
487{
488 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
489 {
490 char_u *s = di->di_tv.vval.v_string;
491 int flags = 0;
492
493 if (STRCMP(s, "word") == 0)
494 flags = FIND_IDENT | FIND_STRING;
495 else if (STRCMP(s, "WORD") == 0)
496 flags = FIND_STRING;
497 else if (STRCMP(s, "expr") == 0)
498 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
499 else if (STRCMP(s, "any") != 0)
500 semsg(_(e_invarg2), s);
501 if (flags != 0)
502 {
503 if (mousemoved)
504 set_mousemoved_columns(wp, flags);
505 else
506 set_moved_columns(wp, flags);
507 }
508 }
509 else if (di->di_tv.v_type == VAR_LIST
510 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200511 && (di->di_tv.vval.v_list->lv_len == 2
512 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200513 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200514 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100515 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200516 int mincol;
517 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200518
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200519 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100520 li = l->lv_first;
521 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200522 {
523 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
524
525 // Three numbers, might be from popup_getoptions().
526 if (mousemoved)
527 wp->w_popup_mouse_row = nr;
528 else
529 wp->w_popup_lnum = nr;
530 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100531 if (nr == 0)
532 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200533 }
534
535 mincol = tv_get_number(&li->li_tv);
536 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200537 if (mousemoved)
538 {
539 wp->w_popup_mouse_mincol = mincol;
540 wp->w_popup_mouse_maxcol = maxcol;
541 }
542 else
543 {
544 wp->w_popup_mincol = mincol;
545 wp->w_popup_maxcol = maxcol;
546 }
547 }
548 else
549 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
550}
551
552 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200553check_highlight(dict_T *dict, char *name, char_u **pval)
554{
555 dictitem_T *di;
556 char_u *str;
557
558 di = dict_find(dict, (char_u *)name, -1);
559 if (di != NULL)
560 {
561 if (di->di_tv.v_type != VAR_STRING)
562 semsg(_(e_invargval), name);
563 else
564 {
565 str = tv_get_string(&di->di_tv);
566 if (*str != NUL)
567 *pval = vim_strsave(str);
568 }
569 }
570}
571
Bram Moolenaarae943152019-06-16 22:54:14 +0200572/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200573 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200574 */
575 static void
576popup_show_curline(win_T *wp)
577{
578 if (wp->w_cursor.lnum < wp->w_topline)
579 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200580 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200581 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200582 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200583 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200584 if (wp->w_topline < 1)
585 wp->w_topline = 1;
586 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
587 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200588 while (wp->w_topline < wp->w_cursor.lnum
589 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
590 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
591 > wp->w_height)
592 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200593 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200594
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200595 // Don't let "firstline" cause a scroll.
596 if (wp->w_firstline > 0)
597 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200598}
599
600/*
601 * Get the sign group name for window "wp".
602 * Returns a pointer to a static buffer, overwritten on the next call.
603 */
604 static char_u *
605popup_get_sign_name(win_T *wp)
606{
607 static char buf[30];
608
609 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
610 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200611}
612
613/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200614 * Highlight the line with the cursor.
615 * Also scrolls the text to put the cursor line in view.
616 */
617 static void
618popup_highlight_curline(win_T *wp)
619{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200620 int sign_id = 0;
621 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200622
Bram Moolenaar72570732019-11-30 14:21:53 +0100623 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200624
625 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
626 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200627 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200628
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200629 if (!sign_exists_by_name(sign_name))
630 {
631 char *linehl = "PopupSelected";
632
633 if (syn_name2id((char_u *)linehl) == 0)
634 linehl = "PmenuSel";
Bram Moolenaare413ea02021-11-24 16:20:13 +0000635 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200636 }
637
Bram Moolenaar72570732019-11-30 14:21:53 +0100638 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200639 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
640 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200641 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200642 else
643 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200644 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200645}
646
647/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200648 * Shared between popup_create() and f_popup_setoptions().
649 */
650 static void
651apply_general_options(win_T *wp, dict_T *dict)
652{
653 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200654 int nr;
655 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200656
Bram Moolenaarae943152019-06-16 22:54:14 +0200657 // TODO: flip
658
659 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200660 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200661 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200662 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200663 if (wp->w_firstline < 0)
664 wp->w_firstline = -1;
665 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200666
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200667 nr = dict_get_bool(dict, (char_u *)"scrollbar", -1);
668 if (nr != -1)
669 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200670
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200671 str = dict_get_string(dict, (char_u *)"title", FALSE);
672 if (str != NULL)
673 {
674 vim_free(wp->w_popup_title);
675 wp->w_popup_title = vim_strsave(str);
676 }
677
Bram Moolenaar55881332020-08-18 13:04:15 +0200678 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
679 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200680 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200681
Bram Moolenaar55881332020-08-18 13:04:15 +0200682 nr = dict_get_bool(dict, (char_u *)"drag", -1);
683 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200684 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200685 if (nr)
686 wp->w_popup_flags |= POPF_DRAG;
687 else
688 wp->w_popup_flags &= ~POPF_DRAG;
689 }
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000690 nr = dict_get_bool(dict, (char_u *)"dragall", -1);
691 if (nr != -1)
692 {
693 if (nr)
694 wp->w_popup_flags |= POPF_DRAGALL;
695 else
696 wp->w_popup_flags &= ~POPF_DRAGALL;
697 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200698
Bram Moolenaar55881332020-08-18 13:04:15 +0200699 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
700 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100701 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100702 if (nr)
703 wp->w_popup_flags |= POPF_POSINVERT;
704 else
705 wp->w_popup_flags &= ~POPF_POSINVERT;
706 }
707
Bram Moolenaar55881332020-08-18 13:04:15 +0200708 nr = dict_get_bool(dict, (char_u *)"resize", -1);
709 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200710 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200711 if (nr)
712 wp->w_popup_flags |= POPF_RESIZE;
713 else
714 wp->w_popup_flags &= ~POPF_RESIZE;
715 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200716
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200717 di = dict_find(dict, (char_u *)"close", -1);
718 if (di != NULL)
719 {
720 int ok = TRUE;
721
722 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
723 {
724 char_u *s = di->di_tv.vval.v_string;
725
726 if (STRCMP(s, "none") == 0)
727 wp->w_popup_close = POPCLOSE_NONE;
728 else if (STRCMP(s, "button") == 0)
729 wp->w_popup_close = POPCLOSE_BUTTON;
730 else if (STRCMP(s, "click") == 0)
731 wp->w_popup_close = POPCLOSE_CLICK;
732 else
733 ok = FALSE;
734 }
735 else
736 ok = FALSE;
737 if (!ok)
738 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
739 }
740
Bram Moolenaarae943152019-06-16 22:54:14 +0200741 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
742 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000743 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200744 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
745 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000746#ifdef FEAT_TERMINAL
747 term_update_wincolor(wp);
748#endif
749 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200750
Bram Moolenaarae943152019-06-16 22:54:14 +0200751 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
752 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200753
Bram Moolenaar790498b2019-06-01 22:15:29 +0200754 di = dict_find(dict, (char_u *)"borderhighlight", -1);
755 if (di != NULL)
756 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200757 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200758 emsg(_(e_listreq));
759 else
760 {
761 list_T *list = di->di_tv.vval.v_list;
762 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200763 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200764
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200765 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200766 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
767 ++i, li = li->li_next)
768 {
769 str = tv_get_string(&li->li_tv);
770 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100771 {
772 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200773 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100774 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200775 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200776 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
777 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100778 {
779 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200780 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200781 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100782 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200783 }
784 }
785
Bram Moolenaar790498b2019-06-01 22:15:29 +0200786 di = dict_find(dict, (char_u *)"borderchars", -1);
787 if (di != NULL)
788 {
789 if (di->di_tv.v_type != VAR_LIST)
790 emsg(_(e_listreq));
791 else
792 {
793 list_T *list = di->di_tv.vval.v_list;
794 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200795 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200796
797 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100798 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200799 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200800 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
801 ++i, li = li->li_next)
802 {
803 str = tv_get_string(&li->li_tv);
804 if (*str != NUL)
805 wp->w_border_char[i] = mb_ptr2char(str);
806 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200807 if (list->lv_len == 1)
808 for (i = 1; i < 8; ++i)
809 wp->w_border_char[i] = wp->w_border_char[0];
810 if (list->lv_len == 2)
811 {
812 for (i = 4; i < 8; ++i)
813 wp->w_border_char[i] = wp->w_border_char[1];
814 for (i = 1; i < 4; ++i)
815 wp->w_border_char[i] = wp->w_border_char[0];
816 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200817 }
818 }
819 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200820
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200821 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
822 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
823
Bram Moolenaarae943152019-06-16 22:54:14 +0200824 di = dict_find(dict, (char_u *)"zindex", -1);
825 if (di != NULL)
826 {
827 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
828 if (wp->w_zindex < 1)
829 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
830 if (wp->w_zindex > 32000)
831 wp->w_zindex = 32000;
832 }
833
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200834 di = dict_find(dict, (char_u *)"mask", -1);
835 if (di != NULL)
836 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200837 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200838
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200839 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200840 {
841 listitem_T *li;
842
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200843 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200844 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200845 {
846 if (li->li_tv.v_type != VAR_LIST
847 || li->li_tv.vval.v_list == NULL
848 || li->li_tv.vval.v_list->lv_len != 4)
849 {
850 ok = FALSE;
851 break;
852 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100853 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200854 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200855 }
856 }
857 if (ok)
858 {
859 wp->w_popup_mask = di->di_tv.vval.v_list;
860 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200861 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200862 }
863 else
864 semsg(_(e_invargval), "mask");
865 }
866
Bram Moolenaarae943152019-06-16 22:54:14 +0200867#if defined(FEAT_TIMERS)
868 // Add timer to close the popup after some time.
869 nr = dict_get_number(dict, (char_u *)"time");
870 if (nr > 0)
871 popup_add_timeout(wp, nr);
872#endif
873
Bram Moolenaar3397f742019-06-02 18:40:06 +0200874 di = dict_find(dict, (char_u *)"moved", -1);
875 if (di != NULL)
876 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200877 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200878 handle_moved_argument(wp, di, FALSE);
879 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200880
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200881 di = dict_find(dict, (char_u *)"mousemoved", -1);
882 if (di != NULL)
883 {
884 set_mousemoved_values(wp);
885 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200886 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200887
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100888 nr = dict_get_bool(dict, (char_u *)"cursorline", -1);
889 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200890 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100891 if (nr != 0)
892 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200893 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100894 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200895 }
896
Bram Moolenaarae943152019-06-16 22:54:14 +0200897 di = dict_find(dict, (char_u *)"filter", -1);
898 if (di != NULL)
899 {
900 callback_T callback = get_callback(&di->di_tv);
901
902 if (callback.cb_name != NULL)
903 {
904 free_callback(&wp->w_filter_cb);
905 set_callback(&wp->w_filter_cb, &callback);
906 }
907 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200908 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
909 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200910 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200911 if (nr)
912 wp->w_popup_flags |= POPF_MAPPING;
913 else
914 wp->w_popup_flags &= ~POPF_MAPPING;
915 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200916
Bram Moolenaar581ba392019-09-03 22:08:33 +0200917 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
918 if (str != NULL)
919 {
920 if (STRCMP(str, "a") == 0)
921 wp->w_filter_mode = MODE_ALL;
922 else
923 wp->w_filter_mode = mode_str2flags(str);
924 }
925
Bram Moolenaarae943152019-06-16 22:54:14 +0200926 di = dict_find(dict, (char_u *)"callback", -1);
927 if (di != NULL)
928 {
929 callback_T callback = get_callback(&di->di_tv);
930
931 if (callback.cb_name != NULL)
932 {
933 free_callback(&wp->w_close_cb);
934 set_callback(&wp->w_close_cb, &callback);
935 }
936 }
937}
938
939/*
940 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200941 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200942 */
943 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200944apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200945{
946 int nr;
947
948 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200949
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200950 if (create)
951 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200952 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
953
Bram Moolenaarae943152019-06-16 22:54:14 +0200954 apply_general_options(wp, dict);
955
Bram Moolenaar55881332020-08-18 13:04:15 +0200956 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200957 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200958 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200959
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200960 // when "firstline" and "cursorline" are both set and the cursor would be
961 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200962 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
963 {
964 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
965 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200966 else if (wp->w_cursor.lnum < wp->w_firstline
967 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200968 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200969 wp->w_topline = wp->w_firstline;
970 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200971 }
972
Bram Moolenaar33796b32019-06-08 16:01:13 +0200973 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200974 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200975}
976
977/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200978 * Add lines to the popup from a list of strings.
979 */
980 static void
981add_popup_strings(buf_T *buf, list_T *l)
982{
983 listitem_T *li;
984 linenr_T lnum = 0;
985 char_u *p;
986
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200987 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200988 if (li->li_tv.v_type == VAR_STRING)
989 {
990 p = li->li_tv.vval.v_string;
991 ml_append_buf(buf, lnum++,
992 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
993 }
994}
995
996/*
997 * Add lines to the popup from a list of dictionaries.
998 */
999 static void
1000add_popup_dicts(buf_T *buf, list_T *l)
1001{
1002 listitem_T *li;
1003 listitem_T *pli;
1004 linenr_T lnum = 0;
1005 char_u *p;
1006 dict_T *dict;
1007
1008 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001009 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001010 {
1011 if (li->li_tv.v_type != VAR_DICT)
1012 {
1013 emsg(_(e_dictreq));
1014 return;
1015 }
1016 dict = li->li_tv.vval.v_dict;
1017 p = dict == NULL ? NULL
1018 : dict_get_string(dict, (char_u *)"text", FALSE);
1019 ml_append_buf(buf, lnum++,
1020 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1021 }
1022
1023 // add the text properties
1024 lnum = 1;
1025 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1026 {
1027 dictitem_T *di;
1028 list_T *plist;
1029
1030 dict = li->li_tv.vval.v_dict;
1031 di = dict_find(dict, (char_u *)"props", -1);
1032 if (di != NULL)
1033 {
1034 if (di->di_tv.v_type != VAR_LIST)
1035 {
1036 emsg(_(e_listreq));
1037 return;
1038 }
1039 plist = di->di_tv.vval.v_list;
1040 if (plist != NULL)
1041 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001042 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001043 {
1044 if (pli->li_tv.v_type != VAR_DICT)
1045 {
1046 emsg(_(e_dictreq));
1047 return;
1048 }
1049 dict = pli->li_tv.vval.v_dict;
1050 if (dict != NULL)
1051 {
1052 int col = dict_get_number(dict, (char_u *)"col");
1053
1054 prop_add_common( lnum, col, dict, buf, NULL);
1055 }
1056 }
1057 }
1058 }
1059 }
1060}
1061
1062/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001063 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1064 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001065 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001066popup_top_extra(win_T *wp)
1067{
1068 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1069
1070 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1071 return 1;
1072 return extra;
1073}
1074
1075/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001076 * Get the padding plus border at the left.
1077 */
1078 int
1079popup_left_extra(win_T *wp)
1080{
1081 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1082}
1083
1084/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001085 * Return the height of popup window "wp", including border and padding.
1086 */
1087 int
1088popup_height(win_T *wp)
1089{
1090 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001091 + popup_top_extra(wp)
1092 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001093}
1094
1095/*
1096 * Return the width of popup window "wp", including border, padding and
1097 * scrollbar.
1098 */
1099 int
1100popup_width(win_T *wp)
1101{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001102 // w_leftcol is how many columns of the core are left of the screen
1103 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001104 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001105 + popup_extra_width(wp)
1106 + wp->w_popup_rightoff;
1107}
1108
1109/*
1110 * Return the extra width of popup window "wp": border, padding and scrollbar.
1111 */
1112 int
1113popup_extra_width(win_T *wp)
1114{
1115 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1116 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1117 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001118}
1119
1120/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001121 * Adjust the position and size of the popup to fit on the screen.
1122 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001123 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001124popup_adjust_position(win_T *wp)
1125{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001126 linenr_T lnum;
1127 int wrapped = 0;
1128 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001129 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001130 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001131 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001132 int center_vert = FALSE;
1133 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001134 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001135 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001136 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1137 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1138 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1139 int extra_height = top_extra + bot_extra;
1140 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001141 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001142 int org_winrow = wp->w_winrow;
1143 int org_wincol = wp->w_wincol;
1144 int org_width = wp->w_width;
1145 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001146 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001147 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001148 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001149 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001150 int wantline = wp->w_wantline; // adjusted for textprop
1151 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001152 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001153 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001154
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001155 wp->w_winrow = 0;
1156 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001157 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001158 wp->w_popup_leftoff = 0;
1159 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001160
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001161 // May need to update the "cursorline" highlighting, which may also change
1162 // "topline"
1163 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1164 popup_highlight_curline(wp);
1165
Bram Moolenaar12034e22019-08-25 22:25:02 +02001166 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1167 {
1168 win_T *prop_win = wp->w_popup_prop_win;
1169 textprop_T prop;
1170 linenr_T prop_lnum;
1171 pos_T pos;
1172 int screen_row;
1173 int screen_scol;
1174 int screen_ccol;
1175 int screen_ecol;
1176
1177 // Popup window is positioned relative to a text property.
1178 if (find_visible_prop(prop_win,
1179 wp->w_popup_prop_type, wp->w_popup_prop_id,
1180 &prop, &prop_lnum) == FAIL)
1181 {
1182 // Text property is no longer visible, hide the popup.
1183 // Unhiding the popup is done in check_popup_unhidden().
1184 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1185 {
1186 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001187 if (win_valid(wp->w_popup_prop_win))
1188 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1189 }
1190 return;
1191 }
1192
1193 // Compute the desired position from the position of the text
1194 // property. Use "wantline" and "wantcol" as offsets.
1195 pos.lnum = prop_lnum;
1196 pos.col = prop.tp_col;
1197 if (wp->w_popup_pos == POPPOS_TOPLEFT
1198 || wp->w_popup_pos == POPPOS_BOTLEFT)
1199 pos.col += prop.tp_len - 1;
1200 textpos2screenpos(prop_win, &pos, &screen_row,
1201 &screen_scol, &screen_ccol, &screen_ecol);
1202
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001203 if (screen_scol == 0)
1204 {
1205 // position is off screen, make the width zero to hide it.
1206 wp->w_width = 0;
1207 return;
1208 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001209 if (wp->w_popup_pos == POPPOS_TOPLEFT
1210 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1211 // below the text
1212 wantline = screen_row + wantline + 1;
1213 else
1214 // above the text
1215 wantline = screen_row + wantline - 1;
1216 center_vert = FALSE;
1217 if (wp->w_popup_pos == POPPOS_TOPLEFT
1218 || wp->w_popup_pos == POPPOS_BOTLEFT)
1219 // right of the text
1220 wantcol = screen_ecol + wantcol;
1221 else
1222 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001223 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001224 use_wantcol = TRUE;
1225 }
1226 else
1227 {
1228 // If no line was specified default to vertical centering.
1229 if (wantline == 0)
1230 center_vert = TRUE;
1231 else if (wantline < 0)
1232 // If "wantline" is negative it actually means zero.
1233 wantline = 0;
1234 if (wantcol < 0)
1235 // If "wantcol" is negative it actually means zero.
1236 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001237 }
1238
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001239 if (wp->w_popup_pos == POPPOS_CENTER)
1240 {
1241 // center after computing the size
1242 center_vert = TRUE;
1243 center_hor = TRUE;
1244 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001245 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001246 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001247 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1248 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001249 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001250 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001251 if (wp->w_winrow >= Rows)
1252 wp->w_winrow = Rows - 1;
1253 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001254
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001255 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001256 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001257 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1258 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001259 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001260 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001261 // Need to see at least one character after the decoration.
1262 if (wp->w_wincol > Columns - left_extra - 1)
1263 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001264 }
1265 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001266
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001267 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001268 // When left aligned use the space available, but shift to the left when we
1269 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001270 maxspace = Columns - wp->w_wincol - left_extra;
1271 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001272 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001273 {
1274 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001275 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001276 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001277
1278 if (wp->w_p_nu || wp->w_p_rnu)
1279 margin_width = number_width(wp) + 1;
1280#ifdef FEAT_FOLDING
1281 margin_width += wp->w_p_fdc;
1282#endif
1283#ifdef FEAT_SIGNS
1284 if (signcolumn_on(wp))
1285 margin_width += 2;
1286#endif
1287 if (margin_width >= maxwidth)
1288 margin_width = maxwidth - 1;
1289
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001290 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001291 minheight = wp->w_minheight;
1292#ifdef FEAT_TERMINAL
1293 // A terminal popup initially does not have content, use a default minimal
1294 // width of 20 characters and height of 5 lines.
1295 if (wp->w_buffer->b_term != NULL)
1296 {
1297 if (minwidth == 0)
1298 minwidth = 20;
1299 if (minheight == 0)
1300 minheight = 5;
1301 }
1302#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001303
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001304 if (wp->w_maxheight > 0)
1305 maxheight = wp->w_maxheight;
1306
Bram Moolenaar8d241042019-06-12 23:40:01 +02001307 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001308 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001309 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001310 if (wp->w_topline < 1)
1311 wp->w_topline = 1;
1312 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001313 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1314
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001315 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001316 // Use a minimum width of one, so that something shows when there is no
1317 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001318 // When "firstline" is -1 then start with the last buffer line and go
1319 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001320 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001321 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001322 if (wp->w_firstline < 0)
1323 lnum = wp->w_buffer->b_ml.ml_line_count;
1324 else
1325 lnum = wp->w_topline;
1326 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001327 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001328 int len;
1329 int w_width = wp->w_width;
1330
1331 // Count Tabs for what they are worth and compute the length based on
1332 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001333 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001334 if (wp->w_width < maxwidth)
1335 wp->w_width = maxwidth;
1336 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001337 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001338 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001339
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001340 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001341 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001342 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001343 {
1344 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001345 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001346 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001347 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001348 }
1349 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001350 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001351 && allow_adjust_left
1352 && (wp->w_popup_pos == POPPOS_TOPLEFT
1353 || wp->w_popup_pos == POPPOS_BOTLEFT))
1354 {
1355 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001356 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001357
Bram Moolenaar51c31312019-06-15 22:27:23 +02001358 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001359 {
1360 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001361
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001362 len -= truncate_shift;
1363 shift_by -= truncate_shift;
1364 }
1365
1366 wp->w_wincol -= shift_by;
1367 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001368 wp->w_width = maxwidth;
1369 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001370 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001371 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001372 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001373 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1374 wp->w_width = wp->w_maxwidth;
1375 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001376
1377 if (wp->w_firstline < 0)
1378 --lnum;
1379 else
1380 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001381
1382 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001383 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001384 && (wp->w_firstline >= 0
1385 ? lnum - wp->w_topline
1386 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001387 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001388 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001389 }
1390
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001391 if (wp->w_firstline < 0)
1392 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1393
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001394 wp->w_has_scrollbar = wp->w_want_scrollbar
1395 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001396#ifdef FEAT_TERMINAL
1397 if (wp->w_buffer->b_term != NULL)
1398 // Terminal window never has a scrollbar, adjusts to window height.
1399 wp->w_has_scrollbar = FALSE;
1400#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001401 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001402 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001403 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001404 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001405 // make space for the scrollbar if needed, when lines wrap and when
1406 // applying minwidth
1407 if (maxwidth + right_extra >= maxspace
1408 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1409 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001410 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001411
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001412 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1413 {
1414 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1415
1416 if (minwidth < title_len)
1417 minwidth = title_len;
1418 }
1419
1420 if (minwidth > 0 && wp->w_width < minwidth)
1421 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001422 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001423 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001424 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001425 // some columns cut off on the right
1426 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001427 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001428 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001429 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001430 {
1431 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1432 if (wp->w_wincol < 0)
1433 wp->w_wincol = 0;
1434 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001435 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1436 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1437 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001438 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001439
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001440 // Right aligned: move to the right if needed.
1441 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001442 if (leftoff >= 0)
1443 wp->w_wincol = leftoff;
1444 else if (wp->w_popup_fixed)
1445 {
1446 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001447 // columns when displaying the window, minus left border and
1448 // padding.
1449 if (-leftoff > left_extra)
1450 wp->w_leftcol = -leftoff - left_extra;
1451 wp->w_width -= wp->w_leftcol;
1452 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001453 if (wp->w_width < 0)
1454 wp->w_width = 0;
1455 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001456 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001457
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001458 if (wp->w_p_wrap || (!wp->w_popup_fixed
1459 && (wp->w_popup_pos == POPPOS_TOPLEFT
1460 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001461 {
1462 int want_col = 0;
1463
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001464 // try to show the right border and any scrollbar
1465 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001466 if (want_col > 0 && wp->w_wincol > 0
1467 && wp->w_wincol + want_col >= Columns)
1468 {
1469 wp->w_wincol = Columns - want_col;
1470 if (wp->w_wincol < 0)
1471 wp->w_wincol = 0;
1472 }
1473 }
1474
Bram Moolenaar8d241042019-06-12 23:40:01 +02001475 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1476 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001477 if (minheight > 0 && wp->w_height < minheight)
1478 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001479 if (maxheight > 0 && wp->w_height > maxheight)
1480 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001481 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001482 if (wp->w_height > Rows - wp->w_winrow)
1483 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001484
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001485 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001486 {
1487 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1488 if (wp->w_winrow < 0)
1489 wp->w_winrow = 0;
1490 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001491 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001492 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001493 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001494 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001495 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001496 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001497 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1498 {
1499 // Bottom aligned but does not fit, and less space on the other
1500 // side or "posinvert" is off: reduce height.
1501 wp->w_winrow = 0;
1502 wp->w_height = wantline - extra_height;
1503 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001504 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001505 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001506 // Not enough space and more space on the other side: make top
1507 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001508 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001509 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001510 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001511 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001512 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1513 || wp->w_popup_pos == POPPOS_TOPLEFT)
1514 {
1515 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1516 && wantline * 2 > Rows
1517 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001518 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001519 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001520 // make bottom aligned and recompute the height
1521 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001522 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001523 if (wp->w_winrow < 0)
1524 {
1525 wp->w_height += wp->w_winrow;
1526 wp->w_winrow = 0;
1527 }
1528 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001529 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001530 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001531 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001532 adjust_height_for_top_aligned = TRUE;
1533 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001534 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001535
1536 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1537 && wp->w_winrow + wp->w_height + extra_height > Rows)
1538 {
1539 // Bottom of the popup goes below the last line, reduce the height and
1540 // add a scrollbar.
1541 wp->w_height = Rows - wp->w_winrow - extra_height;
1542#ifdef FEAT_TERMINAL
1543 if (wp->w_buffer->b_term == NULL)
1544#endif
1545 wp->w_has_scrollbar = TRUE;
1546 }
1547
1548 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001549 if (wp->w_winrow >= Rows)
1550 wp->w_winrow = Rows - 1;
1551 else if (wp->w_winrow < 0)
1552 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001553
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001554 if (wp->w_height != org_height)
1555 win_comp_scroll(wp);
1556
Bram Moolenaar17146962019-05-30 00:12:11 +02001557 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001558 if (win_valid(wp->w_popup_prop_win))
1559 {
1560 wp->w_popup_prop_changedtick =
1561 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1562 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1563 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001564
1565 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001566 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001567 if (org_winrow != wp->w_winrow
1568 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001569 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001570 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001571 || org_width != wp->w_width
1572 || org_height != wp->w_height)
1573 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001574 redraw_win_later(wp, NOT_VALID);
1575 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1576 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001577 popup_mask_refresh = TRUE;
1578 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001579}
1580
Bram Moolenaar17627312019-06-02 19:53:44 +02001581typedef enum
1582{
1583 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001584 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001585 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001586 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001587 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001588 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001589 TYPE_PREVIEW, // preview window
1590 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001591} create_type_T;
1592
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001593/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001594 * Make "buf" empty and set the contents to "text".
1595 * Used by popup_create() and popup_settext().
1596 */
1597 static void
1598popup_set_buffer_text(buf_T *buf, typval_T text)
1599{
1600 int lnum;
1601
1602 // Clear the buffer, then replace the lines.
1603 curbuf = buf;
1604 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001605 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001606 curbuf = curwin->w_buffer;
1607
1608 // Add text to the buffer.
1609 if (text.v_type == VAR_STRING)
1610 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001611 char_u *s = text.vval.v_string;
1612
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001613 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001614 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001615 }
1616 else
1617 {
1618 list_T *l = text.vval.v_list;
1619
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001620 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001621 {
1622 if (l->lv_first->li_tv.v_type == VAR_STRING)
1623 // list of strings
1624 add_popup_strings(buf, l);
1625 else
1626 // list of dictionaries
1627 add_popup_dicts(buf, l);
1628 }
1629 }
1630
1631 // delete the line that was in the empty buffer
1632 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001633 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001634 curbuf = curwin->w_buffer;
1635}
1636
1637/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001638 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1639 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001640 * Return FAIL if the parsing fails.
1641 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001642 static int
1643parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001644{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001645 char_u *p =
1646#ifdef FEAT_QUICKFIX
1647 !is_preview ? p_cpp :
1648#endif
1649 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001650
Bram Moolenaar258cef52019-08-21 17:29:29 +02001651 if (wp != NULL)
1652 wp->w_popup_flags &= ~POPF_INFO_MENU;
1653
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001654 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001655 {
1656 char_u *e, *dig;
1657 char_u *s = p;
1658 int x;
1659
1660 e = vim_strchr(p, ':');
1661 if (e == NULL || e[1] == NUL)
1662 return FAIL;
1663
1664 p = vim_strchr(e, ',');
1665 if (p == NULL)
1666 p = e + STRLEN(e);
1667 dig = e + 1;
1668 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001669
1670 if (STRNCMP(s, "height:", 7) == 0)
1671 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001672 if (dig != p)
1673 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001674 if (wp != NULL)
1675 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001676 if (is_preview)
1677 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001678 wp->w_maxheight = x;
1679 }
1680 }
1681 else if (STRNCMP(s, "width:", 6) == 0)
1682 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001683 if (dig != p)
1684 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001685 if (wp != NULL)
1686 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001687 if (is_preview)
1688 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001689 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001690 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001691 }
1692 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001693 else if (STRNCMP(s, "highlight:", 10) == 0)
1694 {
1695 if (wp != NULL)
1696 {
1697 int c = *p;
1698
1699 *p = NUL;
1700 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1701 s + 10, OPT_FREE|OPT_LOCAL, 0);
1702 *p = c;
1703 }
1704 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001705 else if (STRNCMP(s, "border:", 7) == 0)
1706 {
1707 char_u *arg = s + 7;
1708 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1709 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1710 int i;
1711
1712 if (!on && !off)
1713 return FAIL;
1714 if (wp != NULL)
1715 {
1716 for (i = 0; i < 4; ++i)
1717 wp->w_popup_border[i] = on ? 1 : 0;
1718 if (off)
1719 // only show the X for close when there is a border
1720 wp->w_popup_close = POPCLOSE_NONE;
1721 }
1722 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001723 else if (STRNCMP(s, "align:", 6) == 0)
1724 {
1725 char_u *arg = s + 6;
1726 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1727 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1728
1729 if (!menu && !item)
1730 return FAIL;
1731 if (wp != NULL && menu)
1732 wp->w_popup_flags |= POPF_INFO_MENU;
1733 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001734 else
1735 return FAIL;
1736 }
1737 return OK;
1738}
1739
1740/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001741 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1742 * is not NULL.
1743 * Return FAIL if the parsing fails.
1744 */
1745 int
1746parse_previewpopup(win_T *wp)
1747{
1748 return parse_popup_option(wp, TRUE);
1749}
1750
1751/*
1752 * Parse the 'completepopup' option and apply the values to window "wp" if it
1753 * is not NULL.
1754 * Return FAIL if the parsing fails.
1755 */
1756 int
1757parse_completepopup(win_T *wp)
1758{
1759 return parse_popup_option(wp, FALSE);
1760}
1761
1762/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001763 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001764 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001765 */
1766 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001767popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001768{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001769 poppos_T ppt = POPPOS_NONE;
1770
1771 if (d != NULL)
1772 ppt = get_pos_entry(d, FALSE);
1773
Bram Moolenaar79648732019-07-18 21:43:07 +02001774 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001775 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001776 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001777 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1778 }
1779 else
1780 {
1781 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1782 if (wp->w_wantline == 0) // cursor in first line
1783 {
1784 wp->w_wantline = 2;
1785 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1786 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1787 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001788 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001789
Bram Moolenaar79648732019-07-18 21:43:07 +02001790 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001791 if (wp->w_wantcol > Columns - width)
1792 {
1793 wp->w_wantcol = Columns - width;
1794 if (wp->w_wantcol < 1)
1795 wp->w_wantcol = 1;
1796 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001797
Bram Moolenaar79648732019-07-18 21:43:07 +02001798 popup_adjust_position(wp);
1799}
1800
1801/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001802 * Set w_wantline and w_wantcol for the a given screen position.
1803 * Caller must take care of running into the window border.
1804 */
1805 void
1806popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1807{
1808 wp->w_wantline = row;
1809 wp->w_wantcol = col;
1810 popup_adjust_position(wp);
1811}
1812
1813/*
1814 * Add a border and lef&right padding.
1815 */
1816 static void
1817add_border_left_right_padding(win_T *wp)
1818{
1819 int i;
1820
1821 for (i = 0; i < 4; ++i)
1822 {
1823 wp->w_popup_border[i] = 1;
1824 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1825 }
1826}
1827
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001828#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001829/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001830 * Return TRUE if there is any popup window with a terminal buffer.
1831 */
1832 static int
1833popup_terminal_exists(void)
1834{
1835 win_T *wp;
1836 tabpage_T *tp;
1837
1838 FOR_ALL_POPUPWINS(wp)
1839 if (wp->w_buffer->b_term != NULL)
1840 return TRUE;
1841 FOR_ALL_TABPAGES(tp)
1842 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1843 if (wp->w_buffer->b_term != NULL)
1844 return TRUE;
1845 return FALSE;
1846}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001847#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001848
1849/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001850 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001851 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001852 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001853 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001854 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001855 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001856popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001857{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001858 win_T *wp;
1859 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001860 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001861 int new_buffer;
1862 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001863 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001864 int nr;
1865 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001866
Bram Moolenaar79648732019-07-18 21:43:07 +02001867 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001868 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001869 if (in_vim9script()
1870 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1871 || check_for_dict_arg(argvars, 1) == FAIL))
1872 return NULL;
1873
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001874 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001875 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001876 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001877 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001878 if (buf == NULL)
1879 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001880 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001881 return NULL;
1882 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001883#ifdef FEAT_TERMINAL
1884 if (buf->b_term != NULL && popup_terminal_exists())
1885 {
1886 emsg(_("E861: Cannot open a second popup with a terminal"));
1887 return NULL;
1888 }
1889#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001890 }
1891 else if (!(argvars[0].v_type == VAR_STRING
1892 && argvars[0].vval.v_string != NULL)
1893 && !(argvars[0].v_type == VAR_LIST
1894 && argvars[0].vval.v_list != NULL))
1895 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001896 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001897 return NULL;
1898 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001899 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001900 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001901 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001902 return NULL;
1903 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001904 d = argvars[1].vval.v_dict;
1905 }
1906
1907 if (d != NULL)
1908 {
1909 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1910 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1911 else if (type == TYPE_NOTIFICATION)
1912 tabnr = -1; // notifications are global by default
1913 else
1914 tabnr = 0;
1915 if (tabnr > 0)
1916 {
1917 tp = find_tabpage(tabnr);
1918 if (tp == NULL)
1919 {
1920 semsg(_("E997: Tabpage not found: %d"), tabnr);
1921 return NULL;
1922 }
1923 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001924 }
1925
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001926 // Create the window and buffer.
1927 wp = win_alloc_popup_win();
1928 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001929 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001930 if (rettv != NULL)
1931 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001932 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001933 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001934
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001935 if (buf != NULL)
1936 {
1937 // use existing buffer
1938 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001939 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001940 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001941 buffer_ensure_loaded(buf);
1942 }
1943 else
1944 {
1945 // create a new buffer associated with the popup
1946 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001947 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001948 if (buf == NULL)
1949 return NULL;
1950 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001951
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001952 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001953
Bram Moolenaar46451042019-08-24 15:50:46 +02001954 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001955 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001956 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001957 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001958 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001959 buf->b_p_ul = -1; // no undo
1960 buf->b_p_swf = FALSE; // no swap file
1961 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001962 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001963
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001964 // Avoid that 'buftype' is reset when this buffer is entered.
1965 buf->b_p_initialized = TRUE;
1966 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001967 wp->w_p_wrap = TRUE; // 'wrap' is default on
1968 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001969
Bram Moolenaara3fce622019-06-20 02:31:49 +02001970 if (tp != NULL)
1971 {
1972 // popup on specified tab page
1973 wp->w_next = tp->tp_first_popupwin;
1974 tp->tp_first_popupwin = wp;
1975 }
1976 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001977 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001978 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001979 wp->w_next = curtab->tp_first_popupwin;
1980 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001981 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001982 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001983 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001984 win_T *prev = first_popupwin;
1985
1986 // Global popup: add at the end, so that it gets displayed on top of
1987 // older ones with the same zindex. Matters for notifications.
1988 if (first_popupwin == NULL)
1989 first_popupwin = wp;
1990 else
1991 {
1992 while (prev->w_next != NULL)
1993 prev = prev->w_next;
1994 prev->w_next = wp;
1995 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001996 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001997
Bram Moolenaar79648732019-07-18 21:43:07 +02001998 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001999 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002000
Bram Moolenaar79648732019-07-18 21:43:07 +02002001 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002002 {
2003 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002004 }
2005 if (type == TYPE_ATCURSOR)
2006 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002007 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002008 set_moved_values(wp);
2009 set_moved_columns(wp, FIND_STRING);
2010 }
2011
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002012 if (type == TYPE_BEVAL)
2013 {
2014 wp->w_popup_pos = POPPOS_BOTLEFT;
2015
2016 // by default use the mouse position
2017 wp->w_wantline = mouse_row;
2018 if (wp->w_wantline <= 0) // mouse on first line
2019 {
2020 wp->w_wantline = 2;
2021 wp->w_popup_pos = POPPOS_TOPLEFT;
2022 }
2023 wp->w_wantcol = mouse_col + 1;
2024 set_mousemoved_values(wp);
2025 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2026 }
2027
Bram Moolenaar33796b32019-06-08 16:01:13 +02002028 // set default values
2029 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002030 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002031
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002032 if (type == TYPE_NOTIFICATION)
2033 {
2034 win_T *twp, *nextwin;
2035 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002036
2037 // Try to not overlap with another global popup. Guess we need 3
2038 // more screen lines than buffer lines.
2039 wp->w_wantline = 1;
2040 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2041 {
2042 nextwin = twp->w_next;
2043 if (twp != wp
2044 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2045 && twp->w_winrow <= wp->w_wantline - 1 + height
2046 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2047 {
2048 // move to below this popup and restart the loop to check for
2049 // overlap with other popups
2050 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2051 nextwin = first_popupwin;
2052 }
2053 }
2054 if (wp->w_wantline + height > Rows)
2055 {
2056 // can't avoid overlap, put on top in the hope that message goes
2057 // away soon.
2058 wp->w_wantline = 1;
2059 }
2060
2061 wp->w_wantcol = 10;
2062 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002063 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002064 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002065 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002066 for (i = 0; i < 4; ++i)
2067 wp->w_popup_border[i] = 1;
2068 wp->w_popup_padding[1] = 1;
2069 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002070
2071 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002072 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002073 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2074 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002075 }
2076
Bram Moolenaara730e552019-06-16 19:05:31 +02002077 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002078 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002079 wp->w_popup_pos = POPPOS_CENTER;
2080 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002081 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002082 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002083 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002084 }
2085
Bram Moolenaara730e552019-06-16 19:05:31 +02002086 if (type == TYPE_MENU)
2087 {
2088 typval_T tv;
2089 callback_T callback;
2090
2091 tv.v_type = VAR_STRING;
2092 tv.vval.v_string = (char_u *)"popup_filter_menu";
2093 callback = get_callback(&tv);
2094 if (callback.cb_name != NULL)
2095 set_callback(&wp->w_filter_cb, &callback);
2096
2097 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002098 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002099 }
2100
Bram Moolenaar79648732019-07-18 21:43:07 +02002101 if (type == TYPE_PREVIEW)
2102 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002103 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002104 wp->w_popup_close = POPCLOSE_BUTTON;
2105 for (i = 0; i < 4; ++i)
2106 wp->w_popup_border[i] = 1;
2107 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002108 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002109 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002110# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002111 if (type == TYPE_INFO)
2112 {
2113 wp->w_popup_pos = POPPOS_TOPLEFT;
2114 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2115 wp->w_popup_close = POPCLOSE_BUTTON;
2116 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002117 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002118 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002119# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002120
Bram Moolenaarae943152019-06-16 22:54:14 +02002121 for (i = 0; i < 4; ++i)
2122 VIM_CLEAR(wp->w_border_highlight[i]);
2123 for (i = 0; i < 8; ++i)
2124 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002125 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002126 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002127 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002128
Bram Moolenaar79648732019-07-18 21:43:07 +02002129 if (d != NULL)
2130 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002131 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002132
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002133#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002134 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2135 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002136#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002137
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002138 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002139
2140 wp->w_vsep_width = 0;
2141
2142 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002143 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002144
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002145#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002146 // When running a terminal in the popup it becomes the current window.
2147 if (buf->b_term != NULL)
2148 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002149#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002150
Bram Moolenaara730e552019-06-16 19:05:31 +02002151 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002152}
2153
2154/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002155 * popup_clear()
2156 */
2157 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002158f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002159{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002160 int force = FALSE;
2161
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002162 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2163 return;
2164
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002165 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002166 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002167 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002168}
2169
2170/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002171 * popup_create({text}, {options})
2172 */
2173 void
2174f_popup_create(typval_T *argvars, typval_T *rettv)
2175{
Bram Moolenaar17627312019-06-02 19:53:44 +02002176 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002177}
2178
2179/*
2180 * popup_atcursor({text}, {options})
2181 */
2182 void
2183f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2184{
Bram Moolenaar17627312019-06-02 19:53:44 +02002185 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002186}
2187
2188/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002189 * popup_beval({text}, {options})
2190 */
2191 void
2192f_popup_beval(typval_T *argvars, typval_T *rettv)
2193{
2194 popup_create(argvars, rettv, TYPE_BEVAL);
2195}
2196
2197/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002198 * Invoke the close callback for window "wp" with value "result".
2199 * Careful: The callback may make "wp" invalid!
2200 */
2201 static void
2202invoke_popup_callback(win_T *wp, typval_T *result)
2203{
2204 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002205 typval_T argv[3];
2206
2207 argv[0].v_type = VAR_NUMBER;
2208 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2209
2210 if (result != NULL && result->v_type != VAR_UNKNOWN)
2211 copy_tv(result, &argv[1]);
2212 else
2213 {
2214 argv[1].v_type = VAR_NUMBER;
2215 argv[1].vval.v_number = 0;
2216 }
2217
2218 argv[2].v_type = VAR_UNKNOWN;
2219
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002220 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002221 if (result != NULL)
2222 clear_tv(&argv[1]);
2223 clear_tv(&rettv);
2224}
2225
2226/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002227 * Make "prevwin" the current window, unless it's equal to "wp".
2228 * Otherwise make "firstwin" the current window.
2229 */
2230 static void
2231back_to_prevwin(win_T *wp)
2232{
2233 if (win_valid(prevwin) && wp != prevwin)
2234 win_enter(prevwin, FALSE);
2235 else
2236 win_enter(firstwin, FALSE);
2237}
2238
2239/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002240 * Close popup "wp" and invoke any close callback for it.
2241 */
2242 static void
2243popup_close_and_callback(win_T *wp, typval_T *arg)
2244{
2245 int id = wp->w_id;
2246
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002247#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002248 if (wp == curwin && curbuf->b_term != NULL)
2249 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002250 win_T *owp;
2251
2252 // Closing popup window with a terminal: put focus back on the first
2253 // that works:
2254 // - another popup window with a terminal
2255 // - the previous window
2256 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002257 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002258 if (owp != curwin && owp->w_buffer->b_term != NULL)
2259 break;
2260 if (owp != NULL)
2261 win_enter(owp, FALSE);
2262 else
2263 {
2264 for (owp = curtab->tp_first_popupwin; owp != NULL;
2265 owp = owp->w_next)
2266 if (owp != curwin && owp->w_buffer->b_term != NULL)
2267 break;
2268 if (owp != NULL)
2269 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002270 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002271 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002272 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002273 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002274#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002275
Bram Moolenaar49540192019-12-11 19:34:54 +01002276 // Just in case a check higher up is missing.
2277 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002278 {
2279 // To avoid getting stuck when win_execute() does something that causes
2280 // an error, stop calling the filter callback.
2281 free_callback(&wp->w_filter_cb);
2282
Bram Moolenaar49540192019-12-11 19:34:54 +01002283 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002284 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002285
Bram Moolenaarcee52202020-03-11 14:19:58 +01002286 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002287 if (wp->w_close_cb.cb_name != NULL)
2288 // Careful: This may make "wp" invalid.
2289 invoke_popup_callback(wp, arg);
2290
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002291 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002292 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002293}
2294
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002295 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002296popup_close_with_retval(win_T *wp, int retval)
2297{
2298 typval_T res;
2299
2300 res.v_type = VAR_NUMBER;
2301 res.vval.v_number = retval;
2302 popup_close_and_callback(wp, &res);
2303}
2304
Bram Moolenaar3397f742019-06-02 18:40:06 +02002305/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002306 * Close popup "wp" because of a mouse click.
2307 */
2308 void
2309popup_close_for_mouse_click(win_T *wp)
2310{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002311 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002312}
2313
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002314 static void
2315check_mouse_moved(win_T *wp, win_T *mouse_wp)
2316{
2317 // Close the popup when all if these are true:
2318 // - the mouse is not on this popup
2319 // - "mousemoved" was used
2320 // - the mouse is no longer on the same screen row or the mouse column is
2321 // outside of the relevant text
2322 if (wp != mouse_wp
2323 && wp->w_popup_mouse_row != 0
2324 && (wp->w_popup_mouse_row != mouse_row
2325 || mouse_col < wp->w_popup_mouse_mincol
2326 || mouse_col > wp->w_popup_mouse_maxcol))
2327 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002328 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002329 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002330 }
2331}
2332
2333/*
2334 * Called when the mouse moved: may close a popup with "mousemoved".
2335 */
2336 void
2337popup_handle_mouse_moved(void)
2338{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002339 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002340 win_T *mouse_wp;
2341 int row = mouse_row;
2342 int col = mouse_col;
2343
2344 // find the window where the mouse is in
2345 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2346
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002347 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2348 {
2349 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002350 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002351 }
2352 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2353 {
2354 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002355 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002356 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002357}
2358
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002359/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002360 * In a filter: check if the typed key is a mouse event that is used for
2361 * dragging the popup.
2362 */
2363 static void
2364filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2365{
2366 int row = mouse_row;
2367 int col = mouse_col;
2368
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002369 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002370 && is_mouse_key(c)
2371 && (wp == popup_dragwin
2372 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2373 // do not consume the key, allow for dragging the popup
2374 rettv->vval.v_number = 0;
2375}
2376
Bram Moolenaara730e552019-06-16 19:05:31 +02002377/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002378 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002379 */
2380 void
2381f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2382{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002383 int id;
2384 win_T *wp;
2385 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002386 typval_T res;
2387 int c;
2388 linenr_T old_lnum;
2389
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002390 if (in_vim9script()
2391 && (check_for_number_arg(argvars, 0) == FAIL
2392 || check_for_string_arg(argvars, 1) == FAIL))
2393 return;
2394
2395 id = tv_get_number(&argvars[0]);
2396 wp = win_id2wp(id);
2397 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002398 // If the popup has been closed do not consume the key.
2399 if (wp == NULL)
2400 return;
2401
2402 c = *key;
2403 if (c == K_SPECIAL && key[1] != NUL)
2404 c = TO_SPECIAL(key[1], key[2]);
2405
2406 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002407 rettv->v_type = VAR_BOOL;
2408 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002409 res.v_type = VAR_NUMBER;
2410
2411 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002412 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2413 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002414 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002415 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002416 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2417 ++wp->w_cursor.lnum;
2418 if (old_lnum != wp->w_cursor.lnum)
2419 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002420 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002421 return;
2422 }
2423
2424 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2425 {
2426 // Cancelled, invoke callback with -1
2427 res.vval.v_number = -1;
2428 popup_close_and_callback(wp, &res);
2429 return;
2430 }
2431 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2432 {
2433 // Invoke callback with current index.
2434 res.vval.v_number = wp->w_cursor.lnum;
2435 popup_close_and_callback(wp, &res);
2436 return;
2437 }
2438
2439 filter_handle_drag(wp, c, rettv);
2440}
2441
2442/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002443 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002444 */
2445 void
2446f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2447{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002448 int id;
2449 win_T *wp;
2450 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002451 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002452 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002453
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002454 if (in_vim9script()
2455 && (check_for_number_arg(argvars, 0) == FAIL
2456 || check_for_string_arg(argvars, 1) == FAIL))
2457 return;
2458
2459 id = tv_get_number(&argvars[0]);
2460 wp = win_id2wp(id);
2461 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002462 // If the popup has been closed don't consume the key.
2463 if (wp == NULL)
2464 return;
2465
Bram Moolenaara730e552019-06-16 19:05:31 +02002466 c = *key;
2467 if (c == K_SPECIAL && key[1] != NUL)
2468 c = TO_SPECIAL(key[1], key[2]);
2469
Bram Moolenaara42d9452019-06-15 21:46:30 +02002470 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002471 rettv->v_type = VAR_BOOL;
2472 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002473
Bram Moolenaara730e552019-06-16 19:05:31 +02002474 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002475 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002476 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002477 res.vval.v_number = 0;
2478 else
2479 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002480 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002481 return;
2482 }
2483
2484 // Invoke callback
2485 res.v_type = VAR_NUMBER;
2486 popup_close_and_callback(wp, &res);
2487}
2488
2489/*
2490 * popup_dialog({text}, {options})
2491 */
2492 void
2493f_popup_dialog(typval_T *argvars, typval_T *rettv)
2494{
2495 popup_create(argvars, rettv, TYPE_DIALOG);
2496}
2497
2498/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002499 * popup_menu({text}, {options})
2500 */
2501 void
2502f_popup_menu(typval_T *argvars, typval_T *rettv)
2503{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002504 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002505}
2506
2507/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002508 * popup_notification({text}, {options})
2509 */
2510 void
2511f_popup_notification(typval_T *argvars, typval_T *rettv)
2512{
2513 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2514}
2515
2516/*
2517 * Find the popup window with window-ID "id".
2518 * If the popup window does not exist NULL is returned.
2519 * If the window is not a popup window, and error message is given.
2520 */
2521 static win_T *
2522find_popup_win(int id)
2523{
2524 win_T *wp = win_id2wp(id);
2525
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002526 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002527 {
2528 semsg(_("E993: window %d is not a popup window"), id);
2529 return NULL;
2530 }
2531 return wp;
2532}
2533
2534/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002535 * popup_close({id})
2536 */
2537 void
2538f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2539{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002540 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002541 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002542
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002543 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2544 return;
2545
2546 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002547 if (
2548# ifdef FEAT_TERMINAL
2549 // if the popup contains a terminal it will become hidden
2550 curbuf->b_term == NULL &&
2551# endif
2552 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002553 return;
2554
2555 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002556 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002557 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002558}
2559
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002560 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002561popup_hide(win_T *wp)
2562{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002563#ifdef FEAT_TERMINAL
2564 if (error_if_term_popup_window())
2565 return;
2566#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002567 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2568 {
2569 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002570 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002571 redraw_all_later(NOT_VALID);
2572 popup_mask_refresh = TRUE;
2573 }
2574}
2575
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002576/*
2577 * popup_hide({id})
2578 */
2579 void
2580f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2581{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002582 int id;
2583 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002584
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002585 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2586 return;
2587
2588 id = (int)tv_get_number(argvars);
2589 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002590 if (wp != NULL)
2591 popup_hide(wp);
2592}
2593
2594 void
2595popup_show(win_T *wp)
2596{
2597 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002598 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002599 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002600 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002601 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002602 }
2603}
2604
2605/*
2606 * popup_show({id})
2607 */
2608 void
2609f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2610{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002611 int id;
2612 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002613
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002614 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2615 return;
2616
2617 id = (int)tv_get_number(argvars);
2618 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002619 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002620 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002621 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002622#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002623 if (wp->w_popup_flags & POPF_INFO)
2624 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002625#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002626 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002627}
2628
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002629/*
2630 * popup_settext({id}, {text})
2631 */
2632 void
2633f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2634{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002635 int id;
2636 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002637
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002638 if (in_vim9script()
2639 && (check_for_number_arg(argvars, 0) == FAIL
2640 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2641 return;
2642
2643 id = (int)tv_get_number(&argvars[0]);
2644 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002645 if (wp != NULL)
2646 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002647 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2648 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2649 else
2650 {
2651 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002652 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002653 popup_adjust_position(wp);
2654 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002655 }
2656}
2657
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002658 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002659popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002660{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002661 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002662 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002663 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002664 clear_cmdline = TRUE;
2665 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002666
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002667 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002668 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002669}
2670
Bram Moolenaar82106932020-03-13 14:34:38 +01002671 static void
2672error_for_popup_window(void)
2673{
2674 emsg(_("E994: Not allowed in a popup window"));
2675}
2676
2677 int
2678error_if_popup_window(int also_with_term UNUSED)
2679{
2680 // win_execute() may set "curwin" to a popup window temporarily, but many
2681 // commands are disallowed then. When a terminal runs in the popup most
2682 // things are allowed. When a terminal is finished it can be closed.
2683 if (WIN_IS_POPUP(curwin)
2684# ifdef FEAT_TERMINAL
2685 && (also_with_term || curbuf->b_term == NULL)
2686 && !term_is_finished(curbuf)
2687# endif
2688 )
2689 {
2690 error_for_popup_window();
2691 return TRUE;
2692 }
2693 return FALSE;
2694}
2695
Bram Moolenaarec583842019-05-26 14:11:23 +02002696/*
2697 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002698 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002699 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002700 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002701 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002702popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002703{
2704 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002705 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002706 win_T *prev = NULL;
2707
Bram Moolenaarec583842019-05-26 14:11:23 +02002708 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002709 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002710 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002711 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002712 if (wp == curwin)
2713 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002714 if (!force)
2715 {
2716 error_for_popup_window();
2717 return FAIL;
2718 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002719 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002720 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002721 if (prev == NULL)
2722 first_popupwin = wp->w_next;
2723 else
2724 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002725 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002726 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002727 }
2728
Bram Moolenaarec583842019-05-26 14:11:23 +02002729 // go through tab-local popups
2730 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002731 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002732 return OK;
2733 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002734}
2735
2736/*
2737 * Close a popup window with Window-id "id" in tabpage "tp".
2738 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002739 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002740popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002741{
2742 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002743 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002744 win_T *prev = NULL;
2745
Bram Moolenaarec583842019-05-26 14:11:23 +02002746 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2747 if (wp->w_id == id)
2748 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002749 if (wp == curwin)
2750 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002751 if (!force)
2752 {
2753 error_for_popup_window();
2754 return FAIL;
2755 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002756 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002757 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002758 if (prev == NULL)
2759 *root = wp->w_next;
2760 else
2761 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002762 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002763 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002764 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002765 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002766}
2767
2768 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002769close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002770{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002771 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002772 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002773 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002774 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002775 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002776 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002777 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002778 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002779}
2780
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002781/*
2782 * popup_move({id}, {options})
2783 */
2784 void
2785f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2786{
Bram Moolenaarae943152019-06-16 22:54:14 +02002787 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002788 int id;
2789 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002790
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002791 if (in_vim9script()
2792 && (check_for_number_arg(argvars, 0) == FAIL
2793 || check_for_dict_arg(argvars, 1) == FAIL))
2794 return;
2795
2796 id = (int)tv_get_number(argvars);
2797 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002798 if (wp == NULL)
2799 return; // invalid {id}
2800
2801 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2802 {
2803 emsg(_(e_dictreq));
2804 return;
2805 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002806 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002807
Bram Moolenaarae943152019-06-16 22:54:14 +02002808 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002809
2810 if (wp->w_winrow + wp->w_height >= cmdline_row)
2811 clear_cmdline = TRUE;
2812 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002813}
2814
Bram Moolenaarbc133542019-05-29 20:26:46 +02002815/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002816 * popup_setoptions({id}, {options})
2817 */
2818 void
2819f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2820{
2821 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002822 int id;
2823 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002824 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002825
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002826 if (in_vim9script()
2827 && (check_for_number_arg(argvars, 0) == FAIL
2828 || check_for_dict_arg(argvars, 1) == FAIL))
2829 return;
2830
2831 id = (int)tv_get_number(argvars);
2832 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002833 if (wp == NULL)
2834 return; // invalid {id}
2835
2836 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2837 {
2838 emsg(_(e_dictreq));
2839 return;
2840 }
2841 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002842 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002843
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002844 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002845
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002846 if (old_firstline != wp->w_firstline)
2847 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002848 popup_adjust_position(wp);
2849}
2850
2851/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002852 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002853 */
2854 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002855f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002856{
2857 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002858 int id;
2859 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002860 int top_extra;
2861 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002862
2863 if (rettv_dict_alloc(rettv) == OK)
2864 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002865 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2866 return;
2867
2868 id = (int)tv_get_number(argvars);
2869 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002870 if (wp == NULL)
2871 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002872 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002873 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2874
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002875 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002876 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002877 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002878
Bram Moolenaarbc133542019-05-29 20:26:46 +02002879 dict_add_number(dict, "line", wp->w_winrow + 1);
2880 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002881 dict_add_number(dict, "width", wp->w_width + left_extra
2882 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2883 dict_add_number(dict, "height", wp->w_height + top_extra
2884 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002885
2886 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2887 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2888 dict_add_number(dict, "core_width", wp->w_width);
2889 dict_add_number(dict, "core_height", wp->w_height);
2890
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002891 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002892 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002893 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002894 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002895 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002896
2897 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002898 }
2899}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002900
2901/*
2902 * popup_list()
2903 */
2904 void
2905f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2906{
2907 win_T *wp;
2908 tabpage_T *tp;
2909
2910 if (rettv_list_alloc(rettv) != OK)
2911 return;
2912 FOR_ALL_POPUPWINS(wp)
2913 list_append_number(rettv->vval.v_list, wp->w_id);
2914 FOR_ALL_TABPAGES(tp)
2915 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2916 list_append_number(rettv->vval.v_list, wp->w_id);
2917}
2918
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002919/*
2920 * popup_locate({row}, {col})
2921 */
2922 void
2923f_popup_locate(typval_T *argvars, typval_T *rettv)
2924{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002925 int row;
2926 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002927 win_T *wp;
2928
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002929 if (in_vim9script()
2930 && (check_for_number_arg(argvars, 0) == FAIL
2931 || check_for_number_arg(argvars, 1) == FAIL))
2932 return;
2933
2934 row = tv_get_number(&argvars[0]) - 1;
2935 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002936 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002937 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002938 rettv->vval.v_number = wp->w_id;
2939}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002940
2941/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002942 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2943 */
2944 static void
2945get_padding_border(dict_T *dict, int *array, char *name)
2946{
2947 list_T *list;
2948 int i;
2949
2950 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2951 return;
2952
2953 list = list_alloc();
2954 if (list != NULL)
2955 {
2956 dict_add_list(dict, name, list);
2957 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2958 for (i = 0; i < 4; ++i)
2959 list_append_number(list, array[i]);
2960 }
2961}
2962
2963/*
2964 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2965 */
2966 static void
2967get_borderhighlight(dict_T *dict, win_T *wp)
2968{
2969 list_T *list;
2970 int i;
2971
2972 for (i = 0; i < 4; ++i)
2973 if (wp->w_border_highlight[i] != NULL)
2974 break;
2975 if (i == 4)
2976 return;
2977
2978 list = list_alloc();
2979 if (list != NULL)
2980 {
2981 dict_add_list(dict, "borderhighlight", list);
2982 for (i = 0; i < 4; ++i)
2983 list_append_string(list, wp->w_border_highlight[i], -1);
2984 }
2985}
2986
2987/*
2988 * For popup_getoptions(): add a "borderchars" entry to "dict".
2989 */
2990 static void
2991get_borderchars(dict_T *dict, win_T *wp)
2992{
2993 list_T *list;
2994 int i;
2995 char_u buf[NUMBUFLEN];
2996 int len;
2997
2998 for (i = 0; i < 8; ++i)
2999 if (wp->w_border_char[i] != 0)
3000 break;
3001 if (i == 8)
3002 return;
3003
3004 list = list_alloc();
3005 if (list != NULL)
3006 {
3007 dict_add_list(dict, "borderchars", list);
3008 for (i = 0; i < 8; ++i)
3009 {
3010 len = mb_char2bytes(wp->w_border_char[i], buf);
3011 list_append_string(list, buf, len);
3012 }
3013 }
3014}
3015
3016/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003017 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003018 */
3019 static void
3020get_moved_list(dict_T *dict, win_T *wp)
3021{
3022 list_T *list;
3023
3024 list = list_alloc();
3025 if (list != NULL)
3026 {
3027 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003028 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003029 list_append_number(list, wp->w_popup_mincol);
3030 list_append_number(list, wp->w_popup_maxcol);
3031 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003032 list = list_alloc();
3033 if (list != NULL)
3034 {
3035 dict_add_list(dict, "mousemoved", list);
3036 list_append_number(list, wp->w_popup_mouse_row);
3037 list_append_number(list, wp->w_popup_mouse_mincol);
3038 list_append_number(list, wp->w_popup_mouse_maxcol);
3039 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003040}
3041
3042/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003043 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003044 */
3045 void
3046f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3047{
3048 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003049 int id;
3050 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003051 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003052 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003053
3054 if (rettv_dict_alloc(rettv) == OK)
3055 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003056 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3057 return;
3058
3059 id = (int)tv_get_number(argvars);
3060 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003061 if (wp == NULL)
3062 return;
3063
3064 dict = rettv->vval.v_dict;
3065 dict_add_number(dict, "line", wp->w_wantline);
3066 dict_add_number(dict, "col", wp->w_wantcol);
3067 dict_add_number(dict, "minwidth", wp->w_minwidth);
3068 dict_add_number(dict, "minheight", wp->w_minheight);
3069 dict_add_number(dict, "maxheight", wp->w_maxheight);
3070 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003071 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003072 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003073 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003074 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003075 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003076 {
3077 proptype_T *pt = text_prop_type_by_id(
3078 wp->w_popup_prop_win->w_buffer,
3079 wp->w_popup_prop_type);
3080
3081 if (pt != NULL)
3082 dict_add_string(dict, "textprop", pt->pt_name);
3083 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3084 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3085 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003086 dict_add_string(dict, "title", wp->w_popup_title);
3087 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003088 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003089 dict_add_number(dict, "dragall",
3090 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003091 dict_add_number(dict, "mapping",
3092 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003093 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003094 dict_add_number(dict, "posinvert",
3095 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003096 dict_add_number(dict, "cursorline",
3097 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003098 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003099 if (wp->w_scrollbar_highlight != NULL)
3100 dict_add_string(dict, "scrollbarhighlight",
3101 wp->w_scrollbar_highlight);
3102 if (wp->w_thumb_highlight != NULL)
3103 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003104
Bram Moolenaara3fce622019-06-20 02:31:49 +02003105 // find the tabpage that holds this popup
3106 i = 1;
3107 FOR_ALL_TABPAGES(tp)
3108 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003109 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003110
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003111 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3112 if (twp->w_id == id)
3113 break;
3114 if (twp != NULL)
3115 break;
3116 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003117 }
3118 if (tp == NULL)
3119 i = -1; // must be global
3120 else if (tp == curtab)
3121 i = 0;
3122 dict_add_number(dict, "tabpage", i);
3123
Bram Moolenaarae943152019-06-16 22:54:14 +02003124 get_padding_border(dict, wp->w_popup_padding, "padding");
3125 get_padding_border(dict, wp->w_popup_border, "border");
3126 get_borderhighlight(dict, wp);
3127 get_borderchars(dict, wp);
3128 get_moved_list(dict, wp);
3129
3130 if (wp->w_filter_cb.cb_name != NULL)
3131 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3132 if (wp->w_close_cb.cb_name != NULL)
3133 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003134
K.Takataeeec2542021-06-02 13:28:16 +02003135 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003136 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3137 {
3138 dict_add_string(dict, "pos",
3139 (char_u *)poppos_entries[i].pp_name);
3140 break;
3141 }
3142
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003143 dict_add_string(dict, "close", (char_u *)(
3144 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3145 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3146
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003147# if defined(FEAT_TIMERS)
3148 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3149 ? (long)wp->w_popup_timer->tr_interval : 0L);
3150# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003151 }
3152}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003153
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003154# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003155/*
3156 * Return TRUE if the current window is running a terminal in a popup window.
3157 * Return FALSE when the job has ended.
3158 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003159 int
3160error_if_term_popup_window()
3161{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003162 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3163 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003164 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01003165 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003166 return TRUE;
3167 }
3168 return FALSE;
3169}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003170# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003171
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003172/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003173 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003174 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003175 * Each calling function should use a different flag, see the list at
3176 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003177 */
3178 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003179popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003180{
3181 win_T *wp;
3182
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003183 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003184 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003185 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003186 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003187}
3188
3189/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003190 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003191 * Must have called popup_reset_handled() first.
3192 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3193 * popup with the highest zindex.
3194 */
3195 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003196find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003197{
3198 win_T *wp;
3199 win_T *found_wp;
3200 int found_zindex;
3201
3202 found_zindex = lowest ? INT_MAX : 0;
3203 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003204 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003205 if ((wp->w_popup_handled & handled_flag) == 0
3206 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003207 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003208 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003209 {
3210 found_zindex = wp->w_zindex;
3211 found_wp = wp;
3212 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003213 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003214 if ((wp->w_popup_handled & handled_flag) == 0
3215 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003216 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003217 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003218 {
3219 found_zindex = wp->w_zindex;
3220 found_wp = wp;
3221 }
3222
3223 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003224 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003225 return found_wp;
3226}
3227
3228/*
3229 * Invoke the filter callback for window "wp" with typed character "c".
3230 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003231 * Returns the return value of the filter or -1 for CTRL-C in the current
3232 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003233 * Careful: The filter may make "wp" invalid!
3234 */
3235 static int
3236invoke_popup_filter(win_T *wp, int c)
3237{
3238 int res;
3239 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003240 typval_T argv[3];
3241 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003242 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003243 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003244
Bram Moolenaara42d9452019-06-15 21:46:30 +02003245 // Emergency exit: CTRL-C closes the popup.
3246 if (c == Ctrl_C)
3247 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003248 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003249 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003250
3251 // Reset got_int to avoid the callback isn't called.
3252 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003253 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003254 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003255
3256 // If the popup is the current window it probably fails to close. Then
3257 // do not consume the key.
3258 if (was_curwin && wp == curwin)
3259 return -1;
3260 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003261 }
3262
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003263 argv[0].v_type = VAR_NUMBER;
3264 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3265
3266 // Convert the number to a string, so that the function can use:
3267 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003268 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003269 argv[1].v_type = VAR_STRING;
3270 argv[1].vval.v_string = vim_strsave(buf);
3271
3272 argv[2].v_type = VAR_UNKNOWN;
3273
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003274 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003275 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3276 {
3277 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003278 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003279 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003280 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003281 }
3282 else
3283 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003284 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3285 popup_highlight_curline(wp);
3286
Bram Moolenaar371806e2020-10-22 13:44:54 +02003287 // If an error message was given always return FALSE, so that keys are
3288 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003289 // If we get three errors in a row then close the popup. Decrement the
3290 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3291 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003292 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003293 {
3294 wp->w_filter_errors += 10;
3295 if (wp->w_filter_errors >= 30)
3296 popup_close_with_retval(wp, -1);
3297 res = FALSE;
3298 }
3299 else
3300 {
3301 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3302 --wp->w_filter_errors;
3303 res = tv_get_bool(&rettv);
3304 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003305 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003306
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003307 vim_free(argv[1].vval.v_string);
3308 clear_tv(&rettv);
3309 return res;
3310}
3311
3312/*
3313 * Called when "c" was typed: invoke popup filter callbacks.
3314 * Returns TRUE when the character was consumed,
3315 */
3316 int
3317popup_do_filter(int c)
3318{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003319 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003320 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003321 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003322 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003323 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003324 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003325
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003326#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003327 // Popup window with terminal always gets focus.
3328 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3329 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003330#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003331
Bram Moolenaar934470e2019-09-01 23:27:05 +02003332 if (recursive)
3333 return FALSE;
3334 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003335
Bram Moolenaarf6396232019-08-24 19:36:00 +02003336 if (c == K_LEFTMOUSE)
3337 {
3338 int row = mouse_row;
3339 int col = mouse_col;
3340
3341 wp = mouse_find_win(&row, &col, FIND_POPUP);
3342 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003343 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003344 }
3345
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003346 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003347 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003348 while (res == FALSE
3349 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003350 if (wp->w_filter_cb.cb_name != NULL
3351 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003352 res = invoke_popup_filter(wp, c);
3353
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003354 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003355 {
3356 int save_got_int = got_int;
3357
3358 // Reset got_int to avoid a function used in the statusline aborts.
3359 got_int = FALSE;
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003360 redraw_after_callback(FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003361 got_int |= save_got_int;
3362 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003363 recursive = FALSE;
3364 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003365
3366 // When interrupted return FALSE to avoid looping.
3367 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003368}
3369
Bram Moolenaar3397f742019-06-02 18:40:06 +02003370/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003371 * Return TRUE if there is a popup visible with a filter callback and the
3372 * "mapping" property off.
3373 */
3374 int
3375popup_no_mapping(void)
3376{
3377 int round;
3378 win_T *wp;
3379
3380 for (round = 1; round <= 2; ++round)
3381 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3382 wp != NULL; wp = wp->w_next)
3383 if (wp->w_filter_cb.cb_name != NULL
3384 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3385 return TRUE;
3386 return FALSE;
3387}
3388
3389/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003390 * Called when the cursor moved: check if any popup needs to be closed if the
3391 * cursor moved far enough.
3392 */
3393 void
3394popup_check_cursor_pos()
3395{
3396 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003397
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003398 popup_reset_handled(POPUP_HANDLED_3);
3399 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003400 if (wp->w_popup_curwin != NULL
3401 && (curwin != wp->w_popup_curwin
3402 || curwin->w_cursor.lnum != wp->w_popup_lnum
3403 || curwin->w_cursor.col < wp->w_popup_mincol
3404 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003405 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003406}
3407
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003408/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003409 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003410 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003411 static void
3412popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003413{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003414 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003415 char_u *cells;
3416 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003417
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003418 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3419 {
3420 vim_free(wp->w_popup_mask_cells);
3421 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003422 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003423 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003424 if (wp->w_popup_mask_cells != NULL
3425 && wp->w_popup_mask_height == height
3426 && wp->w_popup_mask_width == width)
3427 return; // cache is still valid
3428
3429 vim_free(wp->w_popup_mask_cells);
3430 wp->w_popup_mask_cells = alloc_clear(width * height);
3431 if (wp->w_popup_mask_cells == NULL)
3432 return;
3433 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003434
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003435 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003436 {
3437 int cols, cole;
3438 int lines, linee;
3439
3440 li = lio->li_tv.vval.v_list->lv_first;
3441 cols = tv_get_number(&li->li_tv);
3442 if (cols < 0)
3443 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003444 if (cols <= 0)
3445 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003446 li = li->li_next;
3447 cole = tv_get_number(&li->li_tv);
3448 if (cole < 0)
3449 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003450 if (cole > width)
3451 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003452 li = li->li_next;
3453 lines = tv_get_number(&li->li_tv);
3454 if (lines < 0)
3455 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003456 if (lines <= 0)
3457 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003458 li = li->li_next;
3459 linee = tv_get_number(&li->li_tv);
3460 if (linee < 0)
3461 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003462 if (linee > height)
3463 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003464
Bram Moolenaar4012d262020-12-29 11:57:46 +01003465 for (row = lines - 1; row < linee; ++row)
3466 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003467 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003468 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003469}
3470
3471/*
3472 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3473 * "col" and "line" are screen coordinates.
3474 */
3475 static int
3476popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3477{
3478 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3479 int line = screenline - wp->w_winrow;
3480
3481 return col >= 0 && col < width
3482 && line >= 0 && line < height
3483 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003484}
3485
3486/*
3487 * Set flags in popup_transparent[] for window "wp" to "val".
3488 */
3489 static void
3490update_popup_transparent(win_T *wp, int val)
3491{
3492 if (wp->w_popup_mask != NULL)
3493 {
3494 int width = popup_width(wp);
3495 int height = popup_height(wp);
3496 listitem_T *lio, *li;
3497 int cols, cole;
3498 int lines, linee;
3499 int col, line;
3500
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003501 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003502 {
3503 li = lio->li_tv.vval.v_list->lv_first;
3504 cols = tv_get_number(&li->li_tv);
3505 if (cols < 0)
3506 cols = width + cols + 1;
3507 li = li->li_next;
3508 cole = tv_get_number(&li->li_tv);
3509 if (cole < 0)
3510 cole = width + cole + 1;
3511 li = li->li_next;
3512 lines = tv_get_number(&li->li_tv);
3513 if (lines < 0)
3514 lines = height + lines + 1;
3515 li = li->li_next;
3516 linee = tv_get_number(&li->li_tv);
3517 if (linee < 0)
3518 linee = height + linee + 1;
3519
3520 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003521 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003522 if (cols < 0)
3523 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003524 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003525 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003526 if (lines < 0)
3527 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003528 for (line = lines; line < linee
3529 && line + wp->w_winrow < screen_Rows; ++line)
3530 for (col = cols; col < cole
3531 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003532 popup_transparent[(line + wp->w_winrow) * screen_Columns
3533 + col + wp->w_wincol] = val;
3534 }
3535 }
3536}
3537
3538/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003539 * Only called when popup window "wp" is hidden: If the window is positioned
3540 * next to a text property, and it is now visible, then unhide the popup.
3541 * We don't check if visible popups become hidden, that is done in
3542 * popup_adjust_position().
3543 * Return TRUE if the popup became unhidden.
3544 */
3545 static int
3546check_popup_unhidden(win_T *wp)
3547{
3548 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3549 {
3550 textprop_T prop;
3551 linenr_T lnum;
3552
3553 if (find_visible_prop(wp->w_popup_prop_win,
3554 wp->w_popup_prop_type, wp->w_popup_prop_id,
3555 &prop, &lnum) == OK)
3556 {
3557 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003558 wp->w_popup_prop_topline = 0; // force repositioning
3559 return TRUE;
3560 }
3561 }
3562 return FALSE;
3563}
3564
3565/*
3566 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3567 * That is when the buffer in the popup was changed, or the popup is following
3568 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003569 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003570 */
3571 static int
3572popup_need_position_adjust(win_T *wp)
3573{
3574 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3575 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003576 if (win_valid(wp->w_popup_prop_win)
3577 && (wp->w_popup_prop_changedtick
3578 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3579 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3580 return TRUE;
3581
3582 // May need to adjust the width if the cursor moved.
3583 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003584}
3585
3586/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003587 * Update "popup_mask" if needed.
3588 * Also recomputes the popup size and positions.
3589 * Also updates "popup_visible".
3590 * Also marks window lines for redrawing.
3591 */
3592 void
3593may_update_popup_mask(int type)
3594{
3595 win_T *wp;
3596 short *mask;
3597 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003598 int redraw_all_popups = FALSE;
3599 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003600
3601 // Need to recompute when switching tabs.
3602 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3603 // (such as the screen size) must have changed.
3604 if (popup_mask_tab != curtab || type >= NOT_VALID)
3605 {
3606 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003607 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003608 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003609
3610 // Check if any popup window buffer has changed and if any popup connected
3611 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003612 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003613 if (wp->w_popup_flags & POPF_HIDDEN)
3614 popup_mask_refresh |= check_popup_unhidden(wp);
3615 else if (popup_need_position_adjust(wp))
3616 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003617 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003618 if (wp->w_popup_flags & POPF_HIDDEN)
3619 popup_mask_refresh |= check_popup_unhidden(wp);
3620 else if (popup_need_position_adjust(wp))
3621 popup_mask_refresh = TRUE;
3622
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003623 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003624 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003625
3626 // Need to update the mask, something has changed.
3627 popup_mask_refresh = FALSE;
3628 popup_mask_tab = curtab;
3629 popup_visible = FALSE;
3630
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003631 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003632 // If redrawing only what is needed, update "popup_mask_next" and then
3633 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003634 redrawing_all_win = TRUE;
3635 FOR_ALL_WINDOWS(wp)
3636 if (wp->w_redr_type < SOME_VALID)
3637 redrawing_all_win = FALSE;
3638 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003639 mask = popup_mask;
3640 else
3641 mask = popup_mask_next;
3642 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3643
3644 // Find the window with the lowest zindex that hasn't been handled yet,
3645 // so that the window with a higher zindex overwrites the value in
3646 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003647 popup_reset_handled(POPUP_HANDLED_4);
3648 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003649 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003650 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003651 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003652
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003653 popup_visible = TRUE;
3654
Bram Moolenaar12034e22019-08-25 22:25:02 +02003655 // Recompute the position if the text changed. It may make the popup
3656 // hidden if it's attach to a text property that is no longer visible.
3657 if (redraw_all_popups || popup_need_position_adjust(wp))
3658 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003659 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003660 if (wp->w_popup_flags & POPF_HIDDEN)
3661 continue;
3662 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003663
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003664 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003665 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003666 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003667 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003668 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003669 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003670 col < wp->w_wincol + width - wp->w_popup_leftoff
3671 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003672 if (wp->w_zindex < POPUPMENU_ZINDEX
3673 && pum_visible()
3674 && pum_under_menu(line, col, FALSE))
3675 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3676 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003677 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003678 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003679 }
3680
3681 // Only check which lines are to be updated if not already
3682 // updating all lines.
3683 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003684 {
3685 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3686 win_T *prev_wp = NULL;
3687
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003688 for (line = 0; line < screen_Rows; ++line)
3689 {
3690 int col_done = 0;
3691
3692 for (col = 0; col < screen_Columns; ++col)
3693 {
3694 int off = line * screen_Columns + col;
3695
3696 if (popup_mask[off] != popup_mask_next[off])
3697 {
3698 popup_mask[off] = popup_mask_next[off];
3699
3700 if (line >= cmdline_row)
3701 {
3702 // the command line needs to be cleared if text below
3703 // the popup is now visible.
3704 if (!msg_scrolled && popup_mask_next[off] == 0)
3705 clear_cmdline = TRUE;
3706 }
3707 else if (col >= col_done)
3708 {
3709 linenr_T lnum;
3710 int line_cp = line;
3711 int col_cp = col;
3712
3713 // The screen position "line" / "col" needs to be
3714 // redrawn. Figure out what window that is and update
3715 // w_redraw_top and w_redr_bot. Only needs to be done
3716 // once for each window line.
3717 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3718 if (wp != NULL)
3719 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003720#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003721 // A terminal window needs to be redrawn.
3722 if (bt_terminal(wp->w_buffer))
3723 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003724 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003725#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003726 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003727 if (wp != prev_wp)
3728 {
3729 vim_memset(plines_cache, 0,
3730 sizeof(int) * Rows);
3731 prev_wp = wp;
3732 }
3733
3734 if (line_cp >= wp->w_height)
3735 // In (or below) status line
3736 wp->w_redr_status = TRUE;
3737 else
3738 {
3739 // compute the position in the buffer line
3740 // from the position in the window
3741 mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003742 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003743 redrawWinline(wp, lnum);
3744 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003745 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003746
3747 // This line is going to be redrawn, no need to
3748 // check until the right side of the window.
3749 col_done = wp->w_wincol + wp->w_width - 1;
3750 }
3751 }
3752 }
3753 }
3754 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003755
3756 vim_free(plines_cache);
3757 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003758}
3759
3760/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003761 * If the current window is a popup and something relevant changed, recompute
3762 * the position and size.
3763 */
3764 void
3765may_update_popup_position(void)
3766{
3767 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3768 popup_adjust_position(curwin);
3769}
3770
3771/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003772 * Return a string of "len" spaces in IObuff.
3773 */
3774 static char_u *
3775get_spaces(int len)
3776{
3777 vim_memset(IObuff, ' ', (size_t)len);
3778 IObuff[len] = NUL;
3779 return IObuff;
3780}
3781
3782/*
3783 * Update popup windows. They are drawn on top of normal windows.
3784 * "win_update" is called for each popup window, lowest zindex first.
3785 */
3786 void
3787update_popups(void (*win_update)(win_T *wp))
3788{
3789 win_T *wp;
3790 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003791 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003792 int total_width;
3793 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003794 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003795 int popup_attr;
3796 int border_attr[4];
3797 int border_char[8];
3798 char_u buf[MB_MAXBYTES];
3799 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003800 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003801 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003802 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003803 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003804 int sb_thumb_top = 0;
3805 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003806 int attr_scroll = 0;
3807 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003808
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003809 // hide the cursor until redrawing is done.
3810 cursor_off();
3811
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003812 // Find the window with the lowest zindex that hasn't been updated yet,
3813 // so that the window with a higher zindex is drawn later, thus goes on
3814 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003815 popup_reset_handled(POPUP_HANDLED_5);
3816 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003817 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003818 int title_len = 0;
3819 int title_wincol;
3820
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003821 // This drawing uses the zindex of the popup window, so that it's on
3822 // top of the text but doesn't draw when another popup with higher
3823 // zindex is on top of the character.
3824 screen_zindex = wp->w_zindex;
3825
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003826 // Set flags in popup_transparent[] for masked cells.
3827 update_popup_transparent(wp, 1);
3828
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003829 // adjust w_winrow and w_wincol for border and padding, since
3830 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003831 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003832 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3833 - wp->w_popup_leftoff;
3834 if (wp->w_wincol + left_extra < 0)
3835 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003836 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003837 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003838
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003839 // Draw the popup text, unless it's off screen.
3840 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003841 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003842 // May need to update the "cursorline" highlighting, which may also
3843 // change "topline"
3844 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3845 popup_highlight_curline(wp);
3846
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003847 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003848
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003849 // move the cursor into the visible lines, otherwise executing
3850 // commands with win_execute() may cause the text to jump.
3851 if (wp->w_cursor.lnum < wp->w_topline)
3852 wp->w_cursor.lnum = wp->w_topline;
3853 else if (wp->w_cursor.lnum >= wp->w_botline)
3854 wp->w_cursor.lnum = wp->w_botline - 1;
3855 }
3856
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003857 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003858 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003859
3860 // Add offset for border and padding if not done already.
3861 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3862 {
3863 wp->w_wcol += left_extra;
3864 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3865 }
3866 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003867 {
3868 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003869 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003870 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003871
Bram Moolenaard529ba52019-07-02 23:13:53 +02003872 total_width = popup_width(wp);
3873 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003874 popup_attr = get_wcr_attr(wp);
3875
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003876 if (wp->w_winrow + total_height > cmdline_row)
3877 wp->w_popup_flags |= POPF_ON_CMDLINE;
3878 else
3879 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3880
3881
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882 // We can only use these line drawing characters when 'encoding' is
3883 // "utf-8" and 'ambiwidth' is "single".
3884 if (enc_utf8 && *p_ambw == 's')
3885 {
3886 border_char[0] = border_char[2] = 0x2550;
3887 border_char[1] = border_char[3] = 0x2551;
3888 border_char[4] = 0x2554;
3889 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003890 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3891 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003892 border_char[7] = 0x255a;
3893 }
3894 else
3895 {
3896 border_char[0] = border_char[2] = '-';
3897 border_char[1] = border_char[3] = '|';
3898 for (i = 4; i < 8; ++i)
3899 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003900 if (wp->w_popup_flags & POPF_RESIZE)
3901 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003902 }
3903 for (i = 0; i < 8; ++i)
3904 if (wp->w_border_char[i] != 0)
3905 border_char[i] = wp->w_border_char[i];
3906
3907 for (i = 0; i < 4; ++i)
3908 {
3909 border_attr[i] = popup_attr;
3910 if (wp->w_border_highlight[i] != NULL)
3911 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3912 }
3913
Bram Moolenaard91467f2020-11-21 12:42:09 +01003914 // Title goes on top of border or padding.
3915 title_wincol = wp->w_wincol + 1;
3916 if (wp->w_popup_title != NULL)
3917 {
rbtnnc6119412021-08-07 13:08:45 +02003918 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003919
Ralf Schandlbc869872021-05-28 14:12:14 +02003920 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003921 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02003922 {
3923 int title_byte_len = (int)STRLEN(wp->w_popup_title);
3924 char_u *title_text = alloc(title_byte_len + 1);
3925
3926 if (title_text != NULL)
3927 {
3928 trunc_string(wp->w_popup_title, title_text,
3929 total_width - 2, title_byte_len + 1);
3930 screen_puts(title_text, wp->w_winrow, title_wincol,
3931 wp->w_popup_border[0] > 0
3932 ? border_attr[0] : popup_attr);
3933 vim_free(title_text);
3934 }
3935
Bram Moolenaard91467f2020-11-21 12:42:09 +01003936 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02003937 }
3938 else
3939 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
3940 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003941 }
3942
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003943 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003944 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003945 if (wp->w_popup_border[0] > 0)
3946 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003947 // top border; do not draw over the title
3948 if (title_len > 0)
3949 {
3950 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3951 wincol < 0 ? 0 : wincol, title_wincol,
3952 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003953 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01003954 border_char[0], border_attr[0]);
3955 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3956 title_wincol + title_len, wincol + total_width,
3957 border_char[0], border_char[0], border_attr[0]);
3958 }
3959 else
3960 {
3961 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3962 wincol < 0 ? 0 : wincol, wincol + total_width,
3963 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
3964 ? border_char[4] : border_char[0],
3965 border_char[0], border_attr[0]);
3966 }
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003967 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003968 {
3969 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3970 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003971 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003972 }
3973 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003974 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3975 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003976
Bram Moolenaard529ba52019-07-02 23:13:53 +02003977 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3978 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003979 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01003980 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003981 - wp->w_has_scrollbar;
3982 if (padcol < 0)
3983 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003984 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003985 padcol = 0;
3986 }
3987 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003988 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003989 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003990 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003991 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01003992 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003993 // top padding and no border; do not draw over the title
3994 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003995 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003996 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01003997 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003998 row += 1;
3999 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004000 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004001 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004002 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004003 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004004
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004005 // Compute scrollbar thumb position and size.
4006 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004007 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004008 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4009 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004010
Bram Moolenaar076d9882019-09-14 22:23:29 +02004011 sb_thumb_height = (height * height + linecount / 2) / linecount;
4012 if (wp->w_topline > 1 && sb_thumb_height == height)
4013 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004014 if (sb_thumb_height == 0)
4015 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02004016 if (linecount <= wp->w_height)
4017 // it just fits, avoid divide by zero
4018 sb_thumb_top = 0;
4019 else
4020 sb_thumb_top = (wp->w_topline - 1
4021 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004022 * (wp->w_height - sb_thumb_height)
4023 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004024 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4025 sb_thumb_top = 1; // show it's scrolled
4026
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004027 if (wp->w_scrollbar_highlight != NULL)
4028 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4029 else
4030 attr_scroll = highlight_attr[HLF_PSB];
4031 if (wp->w_thumb_highlight != NULL)
4032 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4033 else
4034 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004035 }
4036
4037 for (i = wp->w_popup_border[0];
4038 i < total_height - wp->w_popup_border[2]; ++i)
4039 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004040 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004041 // left and right padding only needed next to the body
4042 int do_padding =
4043 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4044 && i < total_height - wp->w_popup_border[2]
4045 - wp->w_popup_padding[2];
4046
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004047 row = wp->w_winrow + i;
4048
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004049 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004050 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004051 {
4052 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004053 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004054 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004055 if (do_padding && wp->w_popup_padding[3] > 0)
4056 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004057 int col = wincol + wp->w_popup_border[3];
4058
Bram Moolenaard529ba52019-07-02 23:13:53 +02004059 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004060 pad_left = wp->w_popup_padding[3];
4061 if (col < 0)
4062 {
4063 pad_left += col;
4064 col = 0;
4065 }
4066 if (pad_left > 0)
4067 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4068 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004069 // scrollbar
4070 if (wp->w_has_scrollbar)
4071 {
4072 int line = i - top_off;
4073 int scroll_col = wp->w_wincol + total_width - 1
4074 - wp->w_popup_border[1];
4075
4076 if (line >= 0 && line < wp->w_height)
4077 screen_putchar(' ', row, scroll_col,
4078 line >= sb_thumb_top
4079 && line < sb_thumb_top + sb_thumb_height
4080 ? attr_thumb : attr_scroll);
4081 else
4082 screen_putchar(' ', row, scroll_col, popup_attr);
4083 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004084 // right border
4085 if (wp->w_popup_border[1] > 0)
4086 {
4087 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004088 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004089 }
4090 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004091 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004092 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004093 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004094 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4095 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004096 }
4097
4098 if (wp->w_popup_padding[2] > 0)
4099 {
4100 // bottom padding
4101 row = wp->w_winrow + wp->w_popup_border[0]
4102 + wp->w_popup_padding[0] + wp->w_height;
4103 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004104 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004105 }
4106
4107 if (wp->w_popup_border[2] > 0)
4108 {
4109 // bottom border
4110 row = wp->w_winrow + total_height - 1;
4111 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004112 wincol < 0 ? 0 : wincol,
4113 wincol + total_width,
4114 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004115 ? border_char[7] : border_char[2],
4116 border_char[2], border_attr[2]);
4117 if (wp->w_popup_border[1] > 0)
4118 {
4119 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004120 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004121 }
4122 }
4123
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004124 if (wp->w_popup_close == POPCLOSE_BUTTON)
4125 {
4126 // close button goes on top of anything at the top-right corner
4127 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004128 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004129 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4130 }
4131
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004132 update_popup_transparent(wp, 0);
4133
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004134 // Back to the normal zindex.
4135 screen_zindex = 0;
4136 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004137
4138#if defined(FEAT_SEARCH_EXTRA)
4139 // In case win_update() called start_search_hl().
4140 end_search_hl();
4141#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004142}
4143
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004144/*
4145 * Mark references in callbacks of one popup window.
4146 */
4147 static int
4148set_ref_in_one_popup(win_T *wp, int copyID)
4149{
4150 int abort = FALSE;
4151 typval_T tv;
4152
4153 if (wp->w_close_cb.cb_partial != NULL)
4154 {
4155 tv.v_type = VAR_PARTIAL;
4156 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4157 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4158 }
4159 if (wp->w_filter_cb.cb_partial != NULL)
4160 {
4161 tv.v_type = VAR_PARTIAL;
4162 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4163 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4164 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004165 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004166 return abort;
4167}
4168
4169/*
4170 * Set reference in callbacks of popup windows.
4171 */
4172 int
4173set_ref_in_popups(int copyID)
4174{
4175 int abort = FALSE;
4176 win_T *wp;
4177 tabpage_T *tp;
4178
4179 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4180 abort = abort || set_ref_in_one_popup(wp, copyID);
4181
4182 FOR_ALL_TABPAGES(tp)
4183 {
4184 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4185 abort = abort || set_ref_in_one_popup(wp, copyID);
4186 if (abort)
4187 break;
4188 }
4189 return abort;
4190}
Bram Moolenaar79648732019-07-18 21:43:07 +02004191
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004192 int
4193popup_is_popup(win_T *wp)
4194{
4195 return wp->w_popup_flags != 0;
4196}
4197
4198#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004199/*
4200 * Find an existing popup used as the preview window, in the current tab page.
4201 * Return NULL if not found.
4202 */
4203 win_T *
4204popup_find_preview_window(void)
4205{
4206 win_T *wp;
4207
4208 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004209 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004210 if (wp->w_p_pvw)
4211 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004212 return NULL;
4213}
4214
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004215/*
4216 * Find an existing popup used as the info window, in the current tab page.
4217 * Return NULL if not found.
4218 */
4219 win_T *
4220popup_find_info_window(void)
4221{
4222 win_T *wp;
4223
4224 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004225 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004226 if (wp->w_popup_flags & POPF_INFO)
4227 return wp;
4228 return NULL;
4229}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004230#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004231
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004232 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004233f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4234{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004235#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004236 win_T *wp = popup_find_info_window();
4237
4238 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004239#else
4240 rettv->vval.v_number = 0;
4241#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004242}
4243
4244 void
4245f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004246{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004247#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004248 win_T *wp = popup_find_preview_window();
4249
4250 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004251#else
4252 rettv->vval.v_number = 0;
4253#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004254}
4255
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004256#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004257/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004258 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004259 * NOTE: this makes the popup the current window, so that the file can be
4260 * edited. However, it must not remain to be the current window, the caller
4261 * must make sure of that.
4262 */
4263 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004264popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004265{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004266 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004267
4268 if (wp == NULL)
4269 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004270 if (info)
4271 wp->w_popup_flags |= POPF_INFO;
4272 else
4273 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004274
4275 // Set the width to a reasonable value, so that w_topline can be computed.
4276 if (wp->w_minwidth > 0)
4277 wp->w_width = wp->w_minwidth;
4278 else if (wp->w_maxwidth > 0)
4279 wp->w_width = wp->w_maxwidth;
4280 else
4281 wp->w_width = curwin->w_width;
4282
4283 // Will switch to another buffer soon, dummy one can be wiped.
4284 wp->w_buffer->b_locked = FALSE;
4285
4286 win_enter(wp, FALSE);
4287 return OK;
4288}
4289
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004290/*
4291 * Close any preview popup.
4292 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004293 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004294popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004295{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004296 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004297
4298 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004299 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004300}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004301
4302/*
4303 * Hide the info popup.
4304 */
4305 void
4306popup_hide_info(void)
4307{
4308 win_T *wp = popup_find_info_window();
4309
4310 if (wp != NULL)
4311 popup_hide(wp);
4312}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004313
4314/*
4315 * Close any info popup.
4316 */
4317 void
4318popup_close_info(void)
4319{
4320 win_T *wp = popup_find_info_window();
4321
4322 if (wp != NULL)
4323 popup_close_with_retval(wp, -1);
4324}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004325#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004326
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004327/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004328 * Close any popup for a text property associated with "win".
4329 * Return TRUE if a popup was closed.
4330 */
4331 int
4332popup_win_closed(win_T *win)
4333{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004334 int round;
4335 win_T *wp;
4336 win_T *next;
4337 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004338
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004339 for (round = 1; round <= 2; ++round)
4340 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4341 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004342 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004343 next = wp->w_next;
4344 if (wp->w_popup_prop_win == win)
4345 {
4346 popup_close_with_retval(wp, -1);
4347 ret = TRUE;
4348 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004349 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004350 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004351}
4352
4353/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004354 * Set the title of the popup window to the file name.
4355 */
4356 void
4357popup_set_title(win_T *wp)
4358{
4359 if (wp->w_buffer->b_fname != NULL)
4360 {
4361 char_u dirname[MAXPATHL];
4362 size_t len;
4363
4364 mch_dirname(dirname, MAXPATHL);
4365 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4366
4367 vim_free(wp->w_popup_title);
4368 len = STRLEN(wp->w_buffer->b_fname) + 3;
4369 wp->w_popup_title = alloc((int)len);
4370 if (wp->w_popup_title != NULL)
4371 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4372 wp->w_buffer->b_fname);
4373 redraw_win_later(wp, VALID);
4374 }
4375}
4376
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004377# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004378/*
4379 * If there is a preview window, update the title.
4380 * Used after changing directory.
4381 */
4382 void
4383popup_update_preview_title(void)
4384{
4385 win_T *wp = popup_find_preview_window();
4386
4387 if (wp != NULL)
4388 popup_set_title(wp);
4389}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004390# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004391
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004392#endif // FEAT_PROP_POPUP