blob: 883372c47fdf6e39e1ea8c963ff2f26bd79f957b [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)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000089 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020090 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
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000153 static void
154update_popup_uses_mouse_move(void)
155{
156 popup_uses_mouse_move = FALSE;
157 if (popup_visible)
158 {
159 win_T *wp;
160
161 FOR_ALL_POPUPWINS(wp)
162 if (wp->w_popup_mouse_row != 0)
163 {
164 popup_uses_mouse_move = TRUE;
165 return;
166 }
167 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
168 if (wp->w_popup_mouse_row != 0)
169 {
170 popup_uses_mouse_move = TRUE;
171 return;
172 }
173 }
174}
175
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200176/*
177 * Used when popup options contain "moved" with "word" or "WORD".
178 */
179 static void
180set_mousemoved_columns(win_T *wp, int flags)
181{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200182 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200183 char_u *text;
184 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200185 pos_T pos;
186 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200187
188 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200189 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200190 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200191 // convert text column to mouse column
192 pos.col = col;
193 pos.coladd = 0;
194 getvcol(textwp, &pos, &mcol, NULL, NULL);
195 wp->w_popup_mouse_mincol = mcol;
196
Bram Moolenaar10727682019-07-12 13:59:20 +0200197 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200198 getvcol(textwp, &pos, NULL, NULL, &mcol);
199 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200200 vim_free(text);
201 }
202}
203
204/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200205 * Return TRUE if "row"/"col" is on the border of the popup.
206 * The values are relative to the top-left corner.
207 */
208 int
209popup_on_border(win_T *wp, int row, int col)
210{
211 return (row == 0 && wp->w_popup_border[0] > 0)
212 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
213 || (col == 0 && wp->w_popup_border[3] > 0)
214 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
215}
216
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200217/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200218 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
219 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200220 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200221 * Caller should check the left mouse button was clicked.
222 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200223 */
224 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200225popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200226{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200227 if (wp->w_popup_close == POPCLOSE_BUTTON
228 && row == 0 && col == popup_width(wp) - 1)
229 {
230 popup_close_for_mouse_click(wp);
231 return TRUE;
232 }
233 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200234}
235
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200236// Values set when dragging a popup window starts.
237static int drag_start_row;
238static int drag_start_col;
239static int drag_start_wantline;
240static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200242
243/*
244 * Mouse down on border of popup window: start dragging it.
245 * Uses mouse_col and mouse_row.
246 */
247 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200248popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200249{
250 drag_start_row = mouse_row;
251 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200252 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200253 drag_start_wantline = wp->w_winrow + 1;
254 else
255 drag_start_wantline = wp->w_wantline;
256 if (wp->w_wantcol == 0)
257 drag_start_wantcol = wp->w_wincol + 1;
258 else
259 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200260
261 // Stop centering the popup
262 if (wp->w_popup_pos == POPPOS_CENTER)
263 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200264
265 drag_on_resize_handle = wp->w_popup_border[1] > 0
266 && wp->w_popup_border[2] > 0
267 && row == popup_height(wp) - 1
268 && col == popup_width(wp) - 1;
269
270 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
271 {
272 if (wp->w_popup_pos == POPPOS_TOPRIGHT
273 || wp->w_popup_pos == POPPOS_BOTRIGHT)
274 wp->w_wantcol = wp->w_wincol + 1;
275 if (wp->w_popup_pos == POPPOS_BOTLEFT)
276 wp->w_wantline = wp->w_winrow + 1;
277 wp->w_popup_pos = POPPOS_TOPLEFT;
278 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200279}
280
281/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200282 * Mouse moved while dragging a popup window: adjust the window popup position
283 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284 */
285 void
286popup_drag(win_T *wp)
287{
288 // The popup may be closed before dragging stops.
289 if (!win_valid_popup(wp))
290 return;
291
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200292 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
293 {
294 int width_inc = mouse_col - drag_start_col;
295 int height_inc = mouse_row - drag_start_row;
296
297 if (width_inc != 0)
298 {
299 int width = wp->w_width + width_inc;
300
301 if (width < 1)
302 width = 1;
303 wp->w_minwidth = width;
304 wp->w_maxwidth = width;
305 drag_start_col = mouse_col;
306 }
307
308 if (height_inc != 0)
309 {
310 int height = wp->w_height + height_inc;
311
312 if (height < 1)
313 height = 1;
314 wp->w_minheight = height;
315 wp->w_maxheight = height;
316 drag_start_row = mouse_row;
317 }
318
319 popup_adjust_position(wp);
320 return;
321 }
322
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000323 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200324 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200325 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
326 if (wp->w_wantline < 1)
327 wp->w_wantline = 1;
328 if (wp->w_wantline > Rows)
329 wp->w_wantline = Rows;
330 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
331 if (wp->w_wantcol < 1)
332 wp->w_wantcol = 1;
333 if (wp->w_wantcol > Columns)
334 wp->w_wantcol = Columns;
335
336 popup_adjust_position(wp);
337}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200338
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200339/*
340 * Set w_firstline to match the current "wp->w_topline".
341 */
342 void
343popup_set_firstline(win_T *wp)
344{
345 int height = wp->w_height;
346
347 wp->w_firstline = wp->w_topline;
348 popup_adjust_position(wp);
349
350 // we don't want the popup to get smaller, decrement the first line
351 // until it doesn't
352 while (wp->w_firstline > 1 && wp->w_height < height)
353 {
354 --wp->w_firstline;
355 popup_adjust_position(wp);
356 }
357}
358
359/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200360 * Return TRUE if the position is in the popup window scrollbar.
361 */
362 int
363popup_is_in_scrollbar(win_T *wp, int row, int col)
364{
365 return wp->w_has_scrollbar
366 && row >= wp->w_popup_border[0]
367 && row < popup_height(wp) - wp->w_popup_border[2]
368 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
369}
370
371
372/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200373 * Handle a click in a popup window, if it is in the scrollbar.
374 */
375 void
376popup_handle_scrollbar_click(win_T *wp, int row, int col)
377{
378 int height = popup_height(wp);
379 int old_topline = wp->w_topline;
380
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200381 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200382 {
383 if (row >= height / 2)
384 {
385 // Click in lower half, scroll down.
386 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
387 ++wp->w_topline;
388 }
389 else if (wp->w_topline > 1)
390 // click on upper half, scroll up.
391 --wp->w_topline;
392 if (wp->w_topline != old_topline)
393 {
394 popup_set_firstline(wp);
395 redraw_win_later(wp, NOT_VALID);
396 }
397 }
398}
399
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200400#if defined(FEAT_TIMERS)
401 static void
402popup_add_timeout(win_T *wp, int time)
403{
404 char_u cbbuf[50];
405 char_u *ptr = cbbuf;
406 typval_T tv;
407
408 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200409 "(_) => popup_close(%d)", wp->w_id);
410 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200411 {
412 wp->w_popup_timer = create_timer(time, 0);
413 wp->w_popup_timer->tr_callback = get_callback(&tv);
414 clear_tv(&tv);
415 }
416}
417#endif
418
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100419 static poppos_T
420get_pos_entry(dict_T *d, int give_error)
421{
422 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
423 int nr;
424
425 if (str == NULL)
426 return POPPOS_NONE;
427
K.Takataeeec2542021-06-02 13:28:16 +0200428 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100429 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
430 return poppos_entries[nr].pp_val;
431
432 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000433 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100434 return POPPOS_NONE;
435}
436
Bram Moolenaar17627312019-06-02 19:53:44 +0200437/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200438 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200439 */
440 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200441apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200442{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200443 int nr;
444 char_u *str;
445 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200446
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200447 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200448 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200449 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200450 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200451 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200452 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200453 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200454 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200455
456 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200457 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200458 wp->w_wantline = nr;
459 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200460 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200461 wp->w_wantcol = nr;
462
Bram Moolenaar55881332020-08-18 13:04:15 +0200463
464 nr = dict_get_bool(d, (char_u *)"fixed", -1);
465 if (nr != -1)
466 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200467
Bram Moolenaar12034e22019-08-25 22:25:02 +0200468 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100469 poppos_T ppt = get_pos_entry(d, TRUE);
470
471 if (ppt != POPPOS_NONE)
472 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200473 }
474
475 str = dict_get_string(d, (char_u *)"textprop", FALSE);
476 if (str != NULL)
477 {
478 wp->w_popup_prop_type = 0;
479 if (*str != NUL)
480 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100481 wp->w_popup_prop_win = curwin;
482 di = dict_find(d, (char_u *)"textpropwin", -1);
483 if (di != NULL)
484 {
485 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100486 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100487 wp->w_popup_prop_win = curwin;
488 }
489
490 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200491 if (nr <= 0)
492 nr = find_prop_type_id(str, NULL);
493 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000494 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200495 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200496 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200497 }
498 }
499
500 di = dict_find(d, (char_u *)"textpropid", -1);
501 if (di != NULL)
502 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200503}
504
Bram Moolenaarb0992022020-01-30 14:55:42 +0100505/*
506 * Handle "moved" and "mousemoved" arguments.
507 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200508 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200509handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
510{
511 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
512 {
513 char_u *s = di->di_tv.vval.v_string;
514 int flags = 0;
515
516 if (STRCMP(s, "word") == 0)
517 flags = FIND_IDENT | FIND_STRING;
518 else if (STRCMP(s, "WORD") == 0)
519 flags = FIND_STRING;
520 else if (STRCMP(s, "expr") == 0)
521 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
522 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000523 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200524 if (flags != 0)
525 {
526 if (mousemoved)
527 set_mousemoved_columns(wp, flags);
528 else
529 set_moved_columns(wp, flags);
530 }
531 }
532 else if (di->di_tv.v_type == VAR_LIST
533 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200534 && (di->di_tv.vval.v_list->lv_len == 2
535 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200536 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200537 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100538 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200539 int mincol;
540 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200541
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200542 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100543 li = l->lv_first;
544 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200545 {
546 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
547
548 // Three numbers, might be from popup_getoptions().
549 if (mousemoved)
550 wp->w_popup_mouse_row = nr;
551 else
552 wp->w_popup_lnum = nr;
553 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100554 if (nr == 0)
555 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200556 }
557
558 mincol = tv_get_number(&li->li_tv);
559 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200560 if (mousemoved)
561 {
562 wp->w_popup_mouse_mincol = mincol;
563 wp->w_popup_mouse_maxcol = maxcol;
564 }
565 else
566 {
567 wp->w_popup_mincol = mincol;
568 wp->w_popup_maxcol = maxcol;
569 }
570 }
571 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000572 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200573}
574
575 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200576check_highlight(dict_T *dict, char *name, char_u **pval)
577{
578 dictitem_T *di;
579 char_u *str;
580
581 di = dict_find(dict, (char_u *)name, -1);
582 if (di != NULL)
583 {
584 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000585 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200586 else
587 {
588 str = tv_get_string(&di->di_tv);
589 if (*str != NUL)
590 *pval = vim_strsave(str);
591 }
592 }
593}
594
Bram Moolenaarae943152019-06-16 22:54:14 +0200595/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200596 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200597 */
598 static void
599popup_show_curline(win_T *wp)
600{
601 if (wp->w_cursor.lnum < wp->w_topline)
602 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200603 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200604 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200605 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200606 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200607 if (wp->w_topline < 1)
608 wp->w_topline = 1;
609 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
610 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200611 while (wp->w_topline < wp->w_cursor.lnum
612 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
613 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
614 > wp->w_height)
615 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200616 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200617
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200618 // Don't let "firstline" cause a scroll.
619 if (wp->w_firstline > 0)
620 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200621}
622
623/*
624 * Get the sign group name for window "wp".
625 * Returns a pointer to a static buffer, overwritten on the next call.
626 */
627 static char_u *
628popup_get_sign_name(win_T *wp)
629{
630 static char buf[30];
631
632 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
633 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200634}
635
636/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200637 * Highlight the line with the cursor.
638 * Also scrolls the text to put the cursor line in view.
639 */
640 static void
641popup_highlight_curline(win_T *wp)
642{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200643 int sign_id = 0;
644 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200645
Bram Moolenaar72570732019-11-30 14:21:53 +0100646 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200647
648 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
649 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200650 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200651
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200652 if (!sign_exists_by_name(sign_name))
653 {
654 char *linehl = "PopupSelected";
655
656 if (syn_name2id((char_u *)linehl) == 0)
657 linehl = "PmenuSel";
James McCoya80aad72021-12-22 19:45:28 +0000658 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200659 }
660
Bram Moolenaar72570732019-11-30 14:21:53 +0100661 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200662 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
663 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200664 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200665 else
666 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200667 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200668}
669
670/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200671 * Shared between popup_create() and f_popup_setoptions().
672 */
673 static void
674apply_general_options(win_T *wp, dict_T *dict)
675{
676 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 int nr;
678 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200679
Bram Moolenaarae943152019-06-16 22:54:14 +0200680 // TODO: flip
681
682 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200683 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200684 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200685 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200686 if (wp->w_firstline < 0)
687 wp->w_firstline = -1;
688 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200689
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200690 nr = dict_get_bool(dict, (char_u *)"scrollbar", -1);
691 if (nr != -1)
692 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200693
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200694 str = dict_get_string(dict, (char_u *)"title", FALSE);
695 if (str != NULL)
696 {
697 vim_free(wp->w_popup_title);
698 wp->w_popup_title = vim_strsave(str);
699 }
700
Bram Moolenaar55881332020-08-18 13:04:15 +0200701 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
702 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200703 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200704
Bram Moolenaar55881332020-08-18 13:04:15 +0200705 nr = dict_get_bool(dict, (char_u *)"drag", -1);
706 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200707 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200708 if (nr)
709 wp->w_popup_flags |= POPF_DRAG;
710 else
711 wp->w_popup_flags &= ~POPF_DRAG;
712 }
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000713 nr = dict_get_bool(dict, (char_u *)"dragall", -1);
714 if (nr != -1)
715 {
716 if (nr)
717 wp->w_popup_flags |= POPF_DRAGALL;
718 else
719 wp->w_popup_flags &= ~POPF_DRAGALL;
720 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200721
Bram Moolenaar55881332020-08-18 13:04:15 +0200722 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
723 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100724 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100725 if (nr)
726 wp->w_popup_flags |= POPF_POSINVERT;
727 else
728 wp->w_popup_flags &= ~POPF_POSINVERT;
729 }
730
Bram Moolenaar55881332020-08-18 13:04:15 +0200731 nr = dict_get_bool(dict, (char_u *)"resize", -1);
732 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200733 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200734 if (nr)
735 wp->w_popup_flags |= POPF_RESIZE;
736 else
737 wp->w_popup_flags &= ~POPF_RESIZE;
738 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200739
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200740 di = dict_find(dict, (char_u *)"close", -1);
741 if (di != NULL)
742 {
743 int ok = TRUE;
744
745 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
746 {
747 char_u *s = di->di_tv.vval.v_string;
748
749 if (STRCMP(s, "none") == 0)
750 wp->w_popup_close = POPCLOSE_NONE;
751 else if (STRCMP(s, "button") == 0)
752 wp->w_popup_close = POPCLOSE_BUTTON;
753 else if (STRCMP(s, "click") == 0)
754 wp->w_popup_close = POPCLOSE_CLICK;
755 else
756 ok = FALSE;
757 }
758 else
759 ok = FALSE;
760 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000761 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200762 }
763
Bram Moolenaarae943152019-06-16 22:54:14 +0200764 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
765 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000766 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200767 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
768 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000769#ifdef FEAT_TERMINAL
770 term_update_wincolor(wp);
771#endif
772 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200773
Bram Moolenaarae943152019-06-16 22:54:14 +0200774 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
775 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200776
Bram Moolenaar790498b2019-06-01 22:15:29 +0200777 di = dict_find(dict, (char_u *)"borderhighlight", -1);
778 if (di != NULL)
779 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200780 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000781 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200782 else
783 {
784 list_T *list = di->di_tv.vval.v_list;
785 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200786 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200787
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200788 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200789 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
790 ++i, li = li->li_next)
791 {
792 str = tv_get_string(&li->li_tv);
793 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100794 {
795 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200796 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100797 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200798 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200799 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
800 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100801 {
802 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200803 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200804 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100805 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200806 }
807 }
808
Bram Moolenaar790498b2019-06-01 22:15:29 +0200809 di = dict_find(dict, (char_u *)"borderchars", -1);
810 if (di != NULL)
811 {
812 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000813 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200814 else
815 {
816 list_T *list = di->di_tv.vval.v_list;
817 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200818 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200819
820 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100821 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200822 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200823 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
824 ++i, li = li->li_next)
825 {
826 str = tv_get_string(&li->li_tv);
827 if (*str != NUL)
828 wp->w_border_char[i] = mb_ptr2char(str);
829 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200830 if (list->lv_len == 1)
831 for (i = 1; i < 8; ++i)
832 wp->w_border_char[i] = wp->w_border_char[0];
833 if (list->lv_len == 2)
834 {
835 for (i = 4; i < 8; ++i)
836 wp->w_border_char[i] = wp->w_border_char[1];
837 for (i = 1; i < 4; ++i)
838 wp->w_border_char[i] = wp->w_border_char[0];
839 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200840 }
841 }
842 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200843
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200844 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
845 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
846
Bram Moolenaarae943152019-06-16 22:54:14 +0200847 di = dict_find(dict, (char_u *)"zindex", -1);
848 if (di != NULL)
849 {
850 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
851 if (wp->w_zindex < 1)
852 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
853 if (wp->w_zindex > 32000)
854 wp->w_zindex = 32000;
855 }
856
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200857 di = dict_find(dict, (char_u *)"mask", -1);
858 if (di != NULL)
859 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200860 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200861
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200862 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200863 {
864 listitem_T *li;
865
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200866 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200867 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200868 {
869 if (li->li_tv.v_type != VAR_LIST
870 || li->li_tv.vval.v_list == NULL
871 || li->li_tv.vval.v_list->lv_len != 4)
872 {
873 ok = FALSE;
874 break;
875 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100876 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200877 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200878 }
879 }
880 if (ok)
881 {
882 wp->w_popup_mask = di->di_tv.vval.v_list;
883 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200884 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200885 }
886 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000887 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200888 }
889
Bram Moolenaarae943152019-06-16 22:54:14 +0200890#if defined(FEAT_TIMERS)
891 // Add timer to close the popup after some time.
892 nr = dict_get_number(dict, (char_u *)"time");
893 if (nr > 0)
894 popup_add_timeout(wp, nr);
895#endif
896
Bram Moolenaar3397f742019-06-02 18:40:06 +0200897 di = dict_find(dict, (char_u *)"moved", -1);
898 if (di != NULL)
899 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200900 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200901 handle_moved_argument(wp, di, FALSE);
902 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200903
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200904 di = dict_find(dict, (char_u *)"mousemoved", -1);
905 if (di != NULL)
906 {
907 set_mousemoved_values(wp);
908 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200909 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200910
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100911 nr = dict_get_bool(dict, (char_u *)"cursorline", -1);
912 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200913 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100914 if (nr != 0)
915 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200916 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100917 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200918 }
919
Bram Moolenaarae943152019-06-16 22:54:14 +0200920 di = dict_find(dict, (char_u *)"filter", -1);
921 if (di != NULL)
922 {
923 callback_T callback = get_callback(&di->di_tv);
924
925 if (callback.cb_name != NULL)
926 {
927 free_callback(&wp->w_filter_cb);
928 set_callback(&wp->w_filter_cb, &callback);
929 }
930 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200931 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
932 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200933 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200934 if (nr)
935 wp->w_popup_flags |= POPF_MAPPING;
936 else
937 wp->w_popup_flags &= ~POPF_MAPPING;
938 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200939
Bram Moolenaar581ba392019-09-03 22:08:33 +0200940 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
941 if (str != NULL)
942 {
943 if (STRCMP(str, "a") == 0)
944 wp->w_filter_mode = MODE_ALL;
945 else
946 wp->w_filter_mode = mode_str2flags(str);
947 }
948
Bram Moolenaarae943152019-06-16 22:54:14 +0200949 di = dict_find(dict, (char_u *)"callback", -1);
950 if (di != NULL)
951 {
952 callback_T callback = get_callback(&di->di_tv);
953
954 if (callback.cb_name != NULL)
955 {
956 free_callback(&wp->w_close_cb);
957 set_callback(&wp->w_close_cb, &callback);
958 }
959 }
960}
961
962/*
963 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200964 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200965 */
966 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200967apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200968{
969 int nr;
970
971 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200972
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200973 if (create)
974 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200975 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
976
Bram Moolenaarae943152019-06-16 22:54:14 +0200977 apply_general_options(wp, dict);
978
Bram Moolenaar55881332020-08-18 13:04:15 +0200979 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200980 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200981 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200982
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200983 // when "firstline" and "cursorline" are both set and the cursor would be
984 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200985 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
986 {
987 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
988 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200989 else if (wp->w_cursor.lnum < wp->w_firstline
990 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200991 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200992 wp->w_topline = wp->w_firstline;
993 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200994 }
995
Bram Moolenaar33796b32019-06-08 16:01:13 +0200996 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200997 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200998}
999
1000/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001001 * Add lines to the popup from a list of strings.
1002 */
1003 static void
1004add_popup_strings(buf_T *buf, list_T *l)
1005{
1006 listitem_T *li;
1007 linenr_T lnum = 0;
1008 char_u *p;
1009
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001010 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001011 if (li->li_tv.v_type == VAR_STRING)
1012 {
1013 p = li->li_tv.vval.v_string;
1014 ml_append_buf(buf, lnum++,
1015 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1016 }
1017}
1018
1019/*
1020 * Add lines to the popup from a list of dictionaries.
1021 */
1022 static void
1023add_popup_dicts(buf_T *buf, list_T *l)
1024{
1025 listitem_T *li;
1026 listitem_T *pli;
1027 linenr_T lnum = 0;
1028 char_u *p;
1029 dict_T *dict;
1030
1031 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001032 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001033 {
1034 if (li->li_tv.v_type != VAR_DICT)
1035 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001036 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001037 return;
1038 }
1039 dict = li->li_tv.vval.v_dict;
1040 p = dict == NULL ? NULL
1041 : dict_get_string(dict, (char_u *)"text", FALSE);
1042 ml_append_buf(buf, lnum++,
1043 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1044 }
1045
1046 // add the text properties
1047 lnum = 1;
1048 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1049 {
1050 dictitem_T *di;
1051 list_T *plist;
1052
1053 dict = li->li_tv.vval.v_dict;
1054 di = dict_find(dict, (char_u *)"props", -1);
1055 if (di != NULL)
1056 {
1057 if (di->di_tv.v_type != VAR_LIST)
1058 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001059 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001060 return;
1061 }
1062 plist = di->di_tv.vval.v_list;
1063 if (plist != NULL)
1064 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001065 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001066 {
1067 if (pli->li_tv.v_type != VAR_DICT)
1068 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001069 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001070 return;
1071 }
1072 dict = pli->li_tv.vval.v_dict;
1073 if (dict != NULL)
1074 {
1075 int col = dict_get_number(dict, (char_u *)"col");
1076
1077 prop_add_common( lnum, col, dict, buf, NULL);
1078 }
1079 }
1080 }
1081 }
1082 }
1083}
1084
1085/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001086 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1087 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001088 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001089popup_top_extra(win_T *wp)
1090{
1091 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1092
1093 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1094 return 1;
1095 return extra;
1096}
1097
1098/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001099 * Get the padding plus border at the left.
1100 */
1101 int
1102popup_left_extra(win_T *wp)
1103{
1104 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1105}
1106
1107/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001108 * Return the height of popup window "wp", including border and padding.
1109 */
1110 int
1111popup_height(win_T *wp)
1112{
1113 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001114 + popup_top_extra(wp)
1115 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001116}
1117
1118/*
1119 * Return the width of popup window "wp", including border, padding and
1120 * scrollbar.
1121 */
1122 int
1123popup_width(win_T *wp)
1124{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001125 // w_leftcol is how many columns of the core are left of the screen
1126 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001127 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001128 + popup_extra_width(wp)
1129 + wp->w_popup_rightoff;
1130}
1131
1132/*
1133 * Return the extra width of popup window "wp": border, padding and scrollbar.
1134 */
1135 int
1136popup_extra_width(win_T *wp)
1137{
1138 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1139 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1140 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001141}
1142
1143/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001144 * Adjust the position and size of the popup to fit on the screen.
1145 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001146 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001147popup_adjust_position(win_T *wp)
1148{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001149 linenr_T lnum;
1150 int wrapped = 0;
1151 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001152 int maxwidth_no_scrollbar;
1153 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001154 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001155 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001156 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001157 int center_vert = FALSE;
1158 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001159 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001160 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001161 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1162 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1163 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1164 int extra_height = top_extra + bot_extra;
1165 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001166 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001167 int org_winrow = wp->w_winrow;
1168 int org_wincol = wp->w_wincol;
1169 int org_width = wp->w_width;
1170 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001171 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001172 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001173 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001174 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001175 int wantline = wp->w_wantline; // adjusted for textprop
1176 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001177 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001178 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001179
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001180 wp->w_winrow = 0;
1181 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001182 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001183 wp->w_popup_leftoff = 0;
1184 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001185
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001186 // May need to update the "cursorline" highlighting, which may also change
1187 // "topline"
1188 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1189 popup_highlight_curline(wp);
1190
Bram Moolenaar12034e22019-08-25 22:25:02 +02001191 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1192 {
1193 win_T *prop_win = wp->w_popup_prop_win;
1194 textprop_T prop;
1195 linenr_T prop_lnum;
1196 pos_T pos;
1197 int screen_row;
1198 int screen_scol;
1199 int screen_ccol;
1200 int screen_ecol;
1201
1202 // Popup window is positioned relative to a text property.
1203 if (find_visible_prop(prop_win,
1204 wp->w_popup_prop_type, wp->w_popup_prop_id,
1205 &prop, &prop_lnum) == FAIL)
1206 {
1207 // Text property is no longer visible, hide the popup.
1208 // Unhiding the popup is done in check_popup_unhidden().
1209 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1210 {
1211 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001212 if (win_valid(wp->w_popup_prop_win))
1213 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1214 }
1215 return;
1216 }
1217
1218 // Compute the desired position from the position of the text
1219 // property. Use "wantline" and "wantcol" as offsets.
1220 pos.lnum = prop_lnum;
1221 pos.col = prop.tp_col;
1222 if (wp->w_popup_pos == POPPOS_TOPLEFT
1223 || wp->w_popup_pos == POPPOS_BOTLEFT)
1224 pos.col += prop.tp_len - 1;
1225 textpos2screenpos(prop_win, &pos, &screen_row,
1226 &screen_scol, &screen_ccol, &screen_ecol);
1227
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001228 if (screen_scol == 0)
1229 {
1230 // position is off screen, make the width zero to hide it.
1231 wp->w_width = 0;
1232 return;
1233 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001234 if (wp->w_popup_pos == POPPOS_TOPLEFT
1235 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1236 // below the text
1237 wantline = screen_row + wantline + 1;
1238 else
1239 // above the text
1240 wantline = screen_row + wantline - 1;
1241 center_vert = FALSE;
1242 if (wp->w_popup_pos == POPPOS_TOPLEFT
1243 || wp->w_popup_pos == POPPOS_BOTLEFT)
1244 // right of the text
1245 wantcol = screen_ecol + wantcol;
1246 else
1247 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001248 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001249 use_wantcol = TRUE;
1250 }
1251 else
1252 {
1253 // If no line was specified default to vertical centering.
1254 if (wantline == 0)
1255 center_vert = TRUE;
1256 else if (wantline < 0)
1257 // If "wantline" is negative it actually means zero.
1258 wantline = 0;
1259 if (wantcol < 0)
1260 // If "wantcol" is negative it actually means zero.
1261 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001262 }
1263
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001264 if (wp->w_popup_pos == POPPOS_CENTER)
1265 {
1266 // center after computing the size
1267 center_vert = TRUE;
1268 center_hor = TRUE;
1269 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001270 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001271 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001272 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1273 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001274 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001275 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001276 if (wp->w_winrow >= Rows)
1277 wp->w_winrow = Rows - 1;
1278 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001279
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001280 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001281 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001282 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1283 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001284 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001285 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001286 // Need to see at least one character after the decoration.
1287 if (wp->w_wincol > Columns - left_extra - 1)
1288 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001289 }
1290 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001291
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001292 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001293 // When left aligned use the space available, but shift to the left when we
1294 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001295 maxspace = Columns - wp->w_wincol - left_extra;
1296 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001297 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001298 {
1299 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001300 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001301 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001302
1303 if (wp->w_p_nu || wp->w_p_rnu)
1304 margin_width = number_width(wp) + 1;
1305#ifdef FEAT_FOLDING
1306 margin_width += wp->w_p_fdc;
1307#endif
1308#ifdef FEAT_SIGNS
1309 if (signcolumn_on(wp))
1310 margin_width += 2;
1311#endif
1312 if (margin_width >= maxwidth)
1313 margin_width = maxwidth - 1;
1314
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001315 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001316 minheight = wp->w_minheight;
1317#ifdef FEAT_TERMINAL
1318 // A terminal popup initially does not have content, use a default minimal
1319 // width of 20 characters and height of 5 lines.
1320 if (wp->w_buffer->b_term != NULL)
1321 {
1322 if (minwidth == 0)
1323 minwidth = 20;
1324 if (minheight == 0)
1325 minheight = 5;
1326 }
1327#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001328
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001329 if (wp->w_maxheight > 0)
1330 maxheight = wp->w_maxheight;
1331
Bram Moolenaar8d241042019-06-12 23:40:01 +02001332 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001333 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001334 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001335 if (wp->w_topline < 1)
1336 wp->w_topline = 1;
1337 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001338 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1339
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001340 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001341 // Use a minimum width of one, so that something shows when there is no
1342 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001343 // When "firstline" is -1 then start with the last buffer line and go
1344 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001345 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001346 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001347 if (wp->w_firstline < 0)
1348 lnum = wp->w_buffer->b_ml.ml_line_count;
1349 else
1350 lnum = wp->w_topline;
1351 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001352 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001353 int len;
1354 int w_width = wp->w_width;
1355
1356 // Count Tabs for what they are worth and compute the length based on
1357 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001358 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001359 if (wp->w_width < maxwidth)
1360 wp->w_width = maxwidth;
1361 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001362 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001363 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001364
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001365 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001366 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001367 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001368 {
1369 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001370 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001371 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001372 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001373 }
1374 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001375 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001376 && allow_adjust_left
1377 && (wp->w_popup_pos == POPPOS_TOPLEFT
1378 || wp->w_popup_pos == POPPOS_BOTLEFT))
1379 {
1380 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001381 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001382
Bram Moolenaar51c31312019-06-15 22:27:23 +02001383 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001384 {
1385 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001386
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001387 len -= truncate_shift;
1388 shift_by -= truncate_shift;
1389 }
1390
1391 wp->w_wincol -= shift_by;
1392 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001393 wp->w_width = maxwidth;
1394 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001395 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001396 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001397 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001398 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1399 wp->w_width = wp->w_maxwidth;
1400 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001401
1402 if (wp->w_firstline < 0)
1403 --lnum;
1404 else
1405 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001406
1407 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001408 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001409 && (wp->w_firstline >= 0
1410 ? lnum - wp->w_topline
1411 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001412 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001413 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001414 }
1415
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001416 if (wp->w_firstline < 0)
1417 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1418
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001419 wp->w_has_scrollbar = wp->w_want_scrollbar
1420 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001421#ifdef FEAT_TERMINAL
1422 if (wp->w_buffer->b_term != NULL)
1423 // Terminal window never has a scrollbar, adjusts to window height.
1424 wp->w_has_scrollbar = FALSE;
1425#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001426 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001427 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001428 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001429 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001430 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001431 // make space for the scrollbar if needed, when lines wrap and when
1432 // applying minwidth
1433 if (maxwidth + right_extra >= maxspace
1434 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1435 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001436 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001437
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001438 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1439 {
1440 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1441
1442 if (minwidth < title_len)
1443 minwidth = title_len;
1444 }
1445
1446 if (minwidth > 0 && wp->w_width < minwidth)
1447 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001448 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001449 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001450 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001451 // some columns cut off on the right
1452 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001453
1454 // If the window doesn't fit because 'minwidth' is set then the
1455 // scrollbar is at the far right of the screen, use the size without
1456 // the scrollbar.
1457 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1458 {
1459 int off = wp->w_width - maxwidth;
1460
1461 if (off > right_extra)
1462 extra_width -= right_extra;
1463 else
1464 extra_width -= off;
1465 wp->w_width = maxwidth_no_scrollbar;
1466 }
1467 else
1468 {
1469 wp->w_width = maxwidth;
1470
1471 // when adding a scrollbar below need to adjust the width
1472 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1473 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001474 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001475 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001476 {
1477 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1478 if (wp->w_wincol < 0)
1479 wp->w_wincol = 0;
1480 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001481 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1482 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1483 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001484 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001485
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001486 // Right aligned: move to the right if needed.
1487 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001488 if (leftoff >= 0)
1489 wp->w_wincol = leftoff;
1490 else if (wp->w_popup_fixed)
1491 {
1492 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001493 // columns when displaying the window, minus left border and
1494 // padding.
1495 if (-leftoff > left_extra)
1496 wp->w_leftcol = -leftoff - left_extra;
1497 wp->w_width -= wp->w_leftcol;
1498 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001499 if (wp->w_width < 0)
1500 wp->w_width = 0;
1501 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001502 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001503
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001504 if (wp->w_p_wrap || (!wp->w_popup_fixed
1505 && (wp->w_popup_pos == POPPOS_TOPLEFT
1506 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001507 {
1508 int want_col = 0;
1509
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001510 // try to show the right border and any scrollbar
1511 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001512 if (want_col > 0 && wp->w_wincol > 0
1513 && wp->w_wincol + want_col >= Columns)
1514 {
1515 wp->w_wincol = Columns - want_col;
1516 if (wp->w_wincol < 0)
1517 wp->w_wincol = 0;
1518 }
1519 }
1520
Bram Moolenaar8d241042019-06-12 23:40:01 +02001521 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1522 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001523 if (minheight > 0 && wp->w_height < minheight)
1524 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001525 if (maxheight > 0 && wp->w_height > maxheight)
1526 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001527 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001528 if (wp->w_height > Rows - wp->w_winrow)
1529 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001530
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001531 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001532 {
1533 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1534 if (wp->w_winrow < 0)
1535 wp->w_winrow = 0;
1536 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001537 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001538 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001539 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001540 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001541 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001542 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001543 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1544 {
1545 // Bottom aligned but does not fit, and less space on the other
1546 // side or "posinvert" is off: reduce height.
1547 wp->w_winrow = 0;
1548 wp->w_height = wantline - extra_height;
1549 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001550 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001551 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001552 // Not enough space and more space on the other side: make top
1553 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001554 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001555 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001556 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001557 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001558 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1559 || wp->w_popup_pos == POPPOS_TOPLEFT)
1560 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001561 if (wp != popup_dragwin
1562 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001563 && wantline * 2 > Rows
1564 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001565 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001566 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001567 // make bottom aligned and recompute the height
1568 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001569 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001570 if (wp->w_winrow < 0)
1571 {
1572 wp->w_height += wp->w_winrow;
1573 wp->w_winrow = 0;
1574 }
1575 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001576 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001577 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001578 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001579 adjust_height_for_top_aligned = TRUE;
1580 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001581 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001582
1583 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1584 && wp->w_winrow + wp->w_height + extra_height > Rows)
1585 {
1586 // Bottom of the popup goes below the last line, reduce the height and
1587 // add a scrollbar.
1588 wp->w_height = Rows - wp->w_winrow - extra_height;
1589#ifdef FEAT_TERMINAL
1590 if (wp->w_buffer->b_term == NULL)
1591#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001592 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001593 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001594 if (width_with_scrollbar > 0)
1595 wp->w_width = width_with_scrollbar;
1596 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001597 }
1598
1599 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001600 if (wp->w_winrow >= Rows)
1601 wp->w_winrow = Rows - 1;
1602 else if (wp->w_winrow < 0)
1603 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001604
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001605 if (wp->w_height != org_height)
1606 win_comp_scroll(wp);
1607
Bram Moolenaar17146962019-05-30 00:12:11 +02001608 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001609 if (win_valid(wp->w_popup_prop_win))
1610 {
1611 wp->w_popup_prop_changedtick =
1612 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1613 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1614 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001615
1616 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001617 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001618 if (org_winrow != wp->w_winrow
1619 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001620 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001621 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001622 || org_width != wp->w_width
1623 || org_height != wp->w_height)
1624 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001625 redraw_win_later(wp, NOT_VALID);
1626 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1627 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001628 popup_mask_refresh = TRUE;
1629 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001630}
1631
Bram Moolenaar17627312019-06-02 19:53:44 +02001632typedef enum
1633{
1634 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001635 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001636 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001637 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001638 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001639 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001640 TYPE_PREVIEW, // preview window
1641 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001642} create_type_T;
1643
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001644/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001645 * Make "buf" empty and set the contents to "text".
1646 * Used by popup_create() and popup_settext().
1647 */
1648 static void
1649popup_set_buffer_text(buf_T *buf, typval_T text)
1650{
1651 int lnum;
1652
1653 // Clear the buffer, then replace the lines.
1654 curbuf = buf;
1655 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001656 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001657 curbuf = curwin->w_buffer;
1658
1659 // Add text to the buffer.
1660 if (text.v_type == VAR_STRING)
1661 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001662 char_u *s = text.vval.v_string;
1663
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001664 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001665 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001666 }
1667 else
1668 {
1669 list_T *l = text.vval.v_list;
1670
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001671 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001672 {
1673 if (l->lv_first->li_tv.v_type == VAR_STRING)
1674 // list of strings
1675 add_popup_strings(buf, l);
1676 else
1677 // list of dictionaries
1678 add_popup_dicts(buf, l);
1679 }
1680 }
1681
1682 // delete the line that was in the empty buffer
1683 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001684 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001685 curbuf = curwin->w_buffer;
1686}
1687
1688/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001689 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1690 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001691 * Return FAIL if the parsing fails.
1692 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001693 static int
1694parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001695{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001696 char_u *p =
1697#ifdef FEAT_QUICKFIX
1698 !is_preview ? p_cpp :
1699#endif
1700 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001701
Bram Moolenaar258cef52019-08-21 17:29:29 +02001702 if (wp != NULL)
1703 wp->w_popup_flags &= ~POPF_INFO_MENU;
1704
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001705 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001706 {
1707 char_u *e, *dig;
1708 char_u *s = p;
1709 int x;
1710
1711 e = vim_strchr(p, ':');
1712 if (e == NULL || e[1] == NUL)
1713 return FAIL;
1714
1715 p = vim_strchr(e, ',');
1716 if (p == NULL)
1717 p = e + STRLEN(e);
1718 dig = e + 1;
1719 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001720
1721 if (STRNCMP(s, "height:", 7) == 0)
1722 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001723 if (dig != p)
1724 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001725 if (wp != NULL)
1726 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001727 if (is_preview)
1728 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001729 wp->w_maxheight = x;
1730 }
1731 }
1732 else if (STRNCMP(s, "width:", 6) == 0)
1733 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001734 if (dig != p)
1735 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001736 if (wp != NULL)
1737 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001738 if (is_preview)
1739 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001740 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001741 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001742 }
1743 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001744 else if (STRNCMP(s, "highlight:", 10) == 0)
1745 {
1746 if (wp != NULL)
1747 {
1748 int c = *p;
1749
1750 *p = NUL;
1751 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1752 s + 10, OPT_FREE|OPT_LOCAL, 0);
1753 *p = c;
1754 }
1755 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001756 else if (STRNCMP(s, "border:", 7) == 0)
1757 {
1758 char_u *arg = s + 7;
1759 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1760 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1761 int i;
1762
1763 if (!on && !off)
1764 return FAIL;
1765 if (wp != NULL)
1766 {
1767 for (i = 0; i < 4; ++i)
1768 wp->w_popup_border[i] = on ? 1 : 0;
1769 if (off)
1770 // only show the X for close when there is a border
1771 wp->w_popup_close = POPCLOSE_NONE;
1772 }
1773 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001774 else if (STRNCMP(s, "align:", 6) == 0)
1775 {
1776 char_u *arg = s + 6;
1777 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1778 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1779
1780 if (!menu && !item)
1781 return FAIL;
1782 if (wp != NULL && menu)
1783 wp->w_popup_flags |= POPF_INFO_MENU;
1784 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001785 else
1786 return FAIL;
1787 }
1788 return OK;
1789}
1790
1791/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001792 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1793 * is not NULL.
1794 * Return FAIL if the parsing fails.
1795 */
1796 int
1797parse_previewpopup(win_T *wp)
1798{
1799 return parse_popup_option(wp, TRUE);
1800}
1801
1802/*
1803 * Parse the 'completepopup' option and apply the values to window "wp" if it
1804 * is not NULL.
1805 * Return FAIL if the parsing fails.
1806 */
1807 int
1808parse_completepopup(win_T *wp)
1809{
1810 return parse_popup_option(wp, FALSE);
1811}
1812
1813/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001814 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001815 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001816 */
1817 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001818popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001819{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001820 poppos_T ppt = POPPOS_NONE;
1821
1822 if (d != NULL)
1823 ppt = get_pos_entry(d, FALSE);
1824
Bram Moolenaar79648732019-07-18 21:43:07 +02001825 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001826 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001827 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001828 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1829 }
1830 else
1831 {
1832 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1833 if (wp->w_wantline == 0) // cursor in first line
1834 {
1835 wp->w_wantline = 2;
1836 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1837 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1838 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001839 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001840
Bram Moolenaar79648732019-07-18 21:43:07 +02001841 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001842 if (wp->w_wantcol > Columns - width)
1843 {
1844 wp->w_wantcol = Columns - width;
1845 if (wp->w_wantcol < 1)
1846 wp->w_wantcol = 1;
1847 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001848
Bram Moolenaar79648732019-07-18 21:43:07 +02001849 popup_adjust_position(wp);
1850}
1851
1852/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001853 * Set w_wantline and w_wantcol for the a given screen position.
1854 * Caller must take care of running into the window border.
1855 */
1856 void
1857popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1858{
1859 wp->w_wantline = row;
1860 wp->w_wantcol = col;
1861 popup_adjust_position(wp);
1862}
1863
1864/*
1865 * Add a border and lef&right padding.
1866 */
1867 static void
1868add_border_left_right_padding(win_T *wp)
1869{
1870 int i;
1871
1872 for (i = 0; i < 4; ++i)
1873 {
1874 wp->w_popup_border[i] = 1;
1875 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1876 }
1877}
1878
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001879#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001880/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001881 * Return TRUE if there is any popup window with a terminal buffer.
1882 */
1883 static int
1884popup_terminal_exists(void)
1885{
1886 win_T *wp;
1887 tabpage_T *tp;
1888
1889 FOR_ALL_POPUPWINS(wp)
1890 if (wp->w_buffer->b_term != NULL)
1891 return TRUE;
1892 FOR_ALL_TABPAGES(tp)
1893 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1894 if (wp->w_buffer->b_term != NULL)
1895 return TRUE;
1896 return FALSE;
1897}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001898#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001899
1900/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001901 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001902 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001903 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001904 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001905 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001906 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001907popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001908{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001909 win_T *wp;
1910 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001911 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001912 int new_buffer;
1913 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001914 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001915 int nr;
1916 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001917
Bram Moolenaar79648732019-07-18 21:43:07 +02001918 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001919 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001920 if (in_vim9script()
1921 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1922 || check_for_dict_arg(argvars, 1) == FAIL))
1923 return NULL;
1924
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001925 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001926 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001927 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001928 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001929 if (buf == NULL)
1930 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001931 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001932 return NULL;
1933 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001934#ifdef FEAT_TERMINAL
1935 if (buf->b_term != NULL && popup_terminal_exists())
1936 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001937 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001938 return NULL;
1939 }
1940#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001941 }
1942 else if (!(argvars[0].v_type == VAR_STRING
1943 && argvars[0].vval.v_string != NULL)
1944 && !(argvars[0].v_type == VAR_LIST
1945 && argvars[0].vval.v_list != NULL))
1946 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001947 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001948 return NULL;
1949 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001950 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001951 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001952 emsg(_(e_dictionary_required));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001953 return NULL;
1954 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001955 d = argvars[1].vval.v_dict;
1956 }
1957
1958 if (d != NULL)
1959 {
1960 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1961 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1962 else if (type == TYPE_NOTIFICATION)
1963 tabnr = -1; // notifications are global by default
1964 else
1965 tabnr = 0;
1966 if (tabnr > 0)
1967 {
1968 tp = find_tabpage(tabnr);
1969 if (tp == NULL)
1970 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00001971 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02001972 return NULL;
1973 }
1974 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001975 }
1976
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001977 // Create the window and buffer.
1978 wp = win_alloc_popup_win();
1979 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001980 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001981 if (rettv != NULL)
1982 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001983 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001984 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001985
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001986 if (buf != NULL)
1987 {
1988 // use existing buffer
1989 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001990 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001991 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001992 buffer_ensure_loaded(buf);
1993 }
1994 else
1995 {
1996 // create a new buffer associated with the popup
1997 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001998 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001999 if (buf == NULL)
2000 return NULL;
2001 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002002
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002003 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002004
Bram Moolenaar46451042019-08-24 15:50:46 +02002005 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002006 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002007 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002008 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002009 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002010 buf->b_p_ul = -1; // no undo
2011 buf->b_p_swf = FALSE; // no swap file
2012 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002013 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002014
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002015 // Avoid that 'buftype' is reset when this buffer is entered.
2016 buf->b_p_initialized = TRUE;
2017 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002018 wp->w_p_wrap = TRUE; // 'wrap' is default on
2019 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002020
Bram Moolenaara3fce622019-06-20 02:31:49 +02002021 if (tp != NULL)
2022 {
2023 // popup on specified tab page
2024 wp->w_next = tp->tp_first_popupwin;
2025 tp->tp_first_popupwin = wp;
2026 }
2027 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002028 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002029 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002030 wp->w_next = curtab->tp_first_popupwin;
2031 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002032 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002033 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002034 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002035 win_T *prev = first_popupwin;
2036
2037 // Global popup: add at the end, so that it gets displayed on top of
2038 // older ones with the same zindex. Matters for notifications.
2039 if (first_popupwin == NULL)
2040 first_popupwin = wp;
2041 else
2042 {
2043 while (prev->w_next != NULL)
2044 prev = prev->w_next;
2045 prev->w_next = wp;
2046 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002047 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002048
Bram Moolenaar79648732019-07-18 21:43:07 +02002049 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002050 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002051
Bram Moolenaar79648732019-07-18 21:43:07 +02002052 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002053 {
2054 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002055 }
2056 if (type == TYPE_ATCURSOR)
2057 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002058 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002059 set_moved_values(wp);
2060 set_moved_columns(wp, FIND_STRING);
2061 }
2062
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002063 if (type == TYPE_BEVAL)
2064 {
2065 wp->w_popup_pos = POPPOS_BOTLEFT;
2066
2067 // by default use the mouse position
2068 wp->w_wantline = mouse_row;
2069 if (wp->w_wantline <= 0) // mouse on first line
2070 {
2071 wp->w_wantline = 2;
2072 wp->w_popup_pos = POPPOS_TOPLEFT;
2073 }
2074 wp->w_wantcol = mouse_col + 1;
2075 set_mousemoved_values(wp);
2076 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2077 }
2078
Bram Moolenaar33796b32019-06-08 16:01:13 +02002079 // set default values
2080 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002081 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002082
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002083 if (type == TYPE_NOTIFICATION)
2084 {
2085 win_T *twp, *nextwin;
2086 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002087
2088 // Try to not overlap with another global popup. Guess we need 3
2089 // more screen lines than buffer lines.
2090 wp->w_wantline = 1;
2091 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2092 {
2093 nextwin = twp->w_next;
2094 if (twp != wp
2095 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2096 && twp->w_winrow <= wp->w_wantline - 1 + height
2097 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2098 {
2099 // move to below this popup and restart the loop to check for
2100 // overlap with other popups
2101 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2102 nextwin = first_popupwin;
2103 }
2104 }
2105 if (wp->w_wantline + height > Rows)
2106 {
2107 // can't avoid overlap, put on top in the hope that message goes
2108 // away soon.
2109 wp->w_wantline = 1;
2110 }
2111
2112 wp->w_wantcol = 10;
2113 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002114 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002115 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002116 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002117 for (i = 0; i < 4; ++i)
2118 wp->w_popup_border[i] = 1;
2119 wp->w_popup_padding[1] = 1;
2120 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002121
2122 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002123 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002124 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2125 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002126 }
2127
Bram Moolenaara730e552019-06-16 19:05:31 +02002128 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002129 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002130 wp->w_popup_pos = POPPOS_CENTER;
2131 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002132 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002133 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002134 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002135 }
2136
Bram Moolenaara730e552019-06-16 19:05:31 +02002137 if (type == TYPE_MENU)
2138 {
2139 typval_T tv;
2140 callback_T callback;
2141
2142 tv.v_type = VAR_STRING;
2143 tv.vval.v_string = (char_u *)"popup_filter_menu";
2144 callback = get_callback(&tv);
2145 if (callback.cb_name != NULL)
2146 set_callback(&wp->w_filter_cb, &callback);
2147
2148 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002149 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002150 }
2151
Bram Moolenaar79648732019-07-18 21:43:07 +02002152 if (type == TYPE_PREVIEW)
2153 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002154 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002155 wp->w_popup_close = POPCLOSE_BUTTON;
2156 for (i = 0; i < 4; ++i)
2157 wp->w_popup_border[i] = 1;
2158 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002159 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002160 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002161# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002162 if (type == TYPE_INFO)
2163 {
2164 wp->w_popup_pos = POPPOS_TOPLEFT;
2165 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2166 wp->w_popup_close = POPCLOSE_BUTTON;
2167 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002168 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002169 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002170# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002171
Bram Moolenaarae943152019-06-16 22:54:14 +02002172 for (i = 0; i < 4; ++i)
2173 VIM_CLEAR(wp->w_border_highlight[i]);
2174 for (i = 0; i < 8; ++i)
2175 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002176 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002177 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002178 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002179
Bram Moolenaar79648732019-07-18 21:43:07 +02002180 if (d != NULL)
2181 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002182 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002183
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002184#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002185 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2186 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002187#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002188
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002189 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002190
2191 wp->w_vsep_width = 0;
2192
2193 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002194 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002195
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002196#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002197 // When running a terminal in the popup it becomes the current window.
2198 if (buf->b_term != NULL)
2199 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002200#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002201
Bram Moolenaara730e552019-06-16 19:05:31 +02002202 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002203}
2204
2205/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002206 * popup_clear()
2207 */
2208 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002209f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002210{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002211 int force = FALSE;
2212
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002213 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2214 return;
2215
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002216 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002217 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002218 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002219}
2220
2221/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002222 * popup_create({text}, {options})
2223 */
2224 void
2225f_popup_create(typval_T *argvars, typval_T *rettv)
2226{
Bram Moolenaar17627312019-06-02 19:53:44 +02002227 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002228}
2229
2230/*
2231 * popup_atcursor({text}, {options})
2232 */
2233 void
2234f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2235{
Bram Moolenaar17627312019-06-02 19:53:44 +02002236 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002237}
2238
2239/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002240 * popup_beval({text}, {options})
2241 */
2242 void
2243f_popup_beval(typval_T *argvars, typval_T *rettv)
2244{
2245 popup_create(argvars, rettv, TYPE_BEVAL);
2246}
2247
2248/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002249 * Invoke the close callback for window "wp" with value "result".
2250 * Careful: The callback may make "wp" invalid!
2251 */
2252 static void
2253invoke_popup_callback(win_T *wp, typval_T *result)
2254{
2255 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002256 typval_T argv[3];
2257
2258 argv[0].v_type = VAR_NUMBER;
2259 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2260
2261 if (result != NULL && result->v_type != VAR_UNKNOWN)
2262 copy_tv(result, &argv[1]);
2263 else
2264 {
2265 argv[1].v_type = VAR_NUMBER;
2266 argv[1].vval.v_number = 0;
2267 }
2268
2269 argv[2].v_type = VAR_UNKNOWN;
2270
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002271 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002272 if (result != NULL)
2273 clear_tv(&argv[1]);
2274 clear_tv(&rettv);
2275}
2276
2277/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002278 * Make "prevwin" the current window, unless it's equal to "wp".
2279 * Otherwise make "firstwin" the current window.
2280 */
2281 static void
2282back_to_prevwin(win_T *wp)
2283{
2284 if (win_valid(prevwin) && wp != prevwin)
2285 win_enter(prevwin, FALSE);
2286 else
2287 win_enter(firstwin, FALSE);
2288}
2289
2290/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002291 * Close popup "wp" and invoke any close callback for it.
2292 */
2293 static void
2294popup_close_and_callback(win_T *wp, typval_T *arg)
2295{
2296 int id = wp->w_id;
2297
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002298#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002299 if (wp == curwin && curbuf->b_term != NULL)
2300 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002301 win_T *owp;
2302
2303 // Closing popup window with a terminal: put focus back on the first
2304 // that works:
2305 // - another popup window with a terminal
2306 // - the previous window
2307 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002308 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002309 if (owp != curwin && owp->w_buffer->b_term != NULL)
2310 break;
2311 if (owp != NULL)
2312 win_enter(owp, FALSE);
2313 else
2314 {
2315 for (owp = curtab->tp_first_popupwin; owp != NULL;
2316 owp = owp->w_next)
2317 if (owp != curwin && owp->w_buffer->b_term != NULL)
2318 break;
2319 if (owp != NULL)
2320 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002321 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002322 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002323 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002324 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002325#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002326
Bram Moolenaar49540192019-12-11 19:34:54 +01002327 // Just in case a check higher up is missing.
2328 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002329 {
2330 // To avoid getting stuck when win_execute() does something that causes
2331 // an error, stop calling the filter callback.
2332 free_callback(&wp->w_filter_cb);
2333
Bram Moolenaar49540192019-12-11 19:34:54 +01002334 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002335 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002336
Bram Moolenaarcee52202020-03-11 14:19:58 +01002337 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002338 if (wp->w_close_cb.cb_name != NULL)
2339 // Careful: This may make "wp" invalid.
2340 invoke_popup_callback(wp, arg);
2341
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002342 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002343 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002344}
2345
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002346 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002347popup_close_with_retval(win_T *wp, int retval)
2348{
2349 typval_T res;
2350
2351 res.v_type = VAR_NUMBER;
2352 res.vval.v_number = retval;
2353 popup_close_and_callback(wp, &res);
2354}
2355
Bram Moolenaar3397f742019-06-02 18:40:06 +02002356/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002357 * Close popup "wp" because of a mouse click.
2358 */
2359 void
2360popup_close_for_mouse_click(win_T *wp)
2361{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002362 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002363}
2364
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002365 static void
2366check_mouse_moved(win_T *wp, win_T *mouse_wp)
2367{
2368 // Close the popup when all if these are true:
2369 // - the mouse is not on this popup
2370 // - "mousemoved" was used
2371 // - the mouse is no longer on the same screen row or the mouse column is
2372 // outside of the relevant text
2373 if (wp != mouse_wp
2374 && wp->w_popup_mouse_row != 0
2375 && (wp->w_popup_mouse_row != mouse_row
2376 || mouse_col < wp->w_popup_mouse_mincol
2377 || mouse_col > wp->w_popup_mouse_maxcol))
2378 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002379 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002380 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002381 }
2382}
2383
2384/*
2385 * Called when the mouse moved: may close a popup with "mousemoved".
2386 */
2387 void
2388popup_handle_mouse_moved(void)
2389{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002390 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002391 win_T *mouse_wp;
2392 int row = mouse_row;
2393 int col = mouse_col;
2394
2395 // find the window where the mouse is in
2396 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2397
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002398 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2399 {
2400 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002401 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002402 }
2403 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2404 {
2405 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002406 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002407 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002408}
2409
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002410/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002411 * In a filter: check if the typed key is a mouse event that is used for
2412 * dragging the popup.
2413 */
2414 static void
2415filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2416{
2417 int row = mouse_row;
2418 int col = mouse_col;
2419
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002420 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002421 && is_mouse_key(c)
2422 && (wp == popup_dragwin
2423 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2424 // do not consume the key, allow for dragging the popup
2425 rettv->vval.v_number = 0;
2426}
2427
Bram Moolenaara730e552019-06-16 19:05:31 +02002428/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002429 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002430 */
2431 void
2432f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2433{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002434 int id;
2435 win_T *wp;
2436 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002437 typval_T res;
2438 int c;
2439 linenr_T old_lnum;
2440
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002441 if (in_vim9script()
2442 && (check_for_number_arg(argvars, 0) == FAIL
2443 || check_for_string_arg(argvars, 1) == FAIL))
2444 return;
2445
2446 id = tv_get_number(&argvars[0]);
2447 wp = win_id2wp(id);
2448 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002449 // If the popup has been closed do not consume the key.
2450 if (wp == NULL)
2451 return;
2452
2453 c = *key;
2454 if (c == K_SPECIAL && key[1] != NUL)
2455 c = TO_SPECIAL(key[1], key[2]);
2456
2457 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002458 rettv->v_type = VAR_BOOL;
2459 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002460 res.v_type = VAR_NUMBER;
2461
2462 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002463 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2464 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002465 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002466 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002467 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2468 ++wp->w_cursor.lnum;
2469 if (old_lnum != wp->w_cursor.lnum)
2470 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002471 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002472 return;
2473 }
2474
2475 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2476 {
2477 // Cancelled, invoke callback with -1
2478 res.vval.v_number = -1;
2479 popup_close_and_callback(wp, &res);
2480 return;
2481 }
2482 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2483 {
2484 // Invoke callback with current index.
2485 res.vval.v_number = wp->w_cursor.lnum;
2486 popup_close_and_callback(wp, &res);
2487 return;
2488 }
2489
2490 filter_handle_drag(wp, c, rettv);
2491}
2492
2493/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002494 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002495 */
2496 void
2497f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2498{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002499 int id;
2500 win_T *wp;
2501 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002502 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002503 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002504
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002505 if (in_vim9script()
2506 && (check_for_number_arg(argvars, 0) == FAIL
2507 || check_for_string_arg(argvars, 1) == FAIL))
2508 return;
2509
2510 id = tv_get_number(&argvars[0]);
2511 wp = win_id2wp(id);
2512 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002513 // If the popup has been closed don't consume the key.
2514 if (wp == NULL)
2515 return;
2516
Bram Moolenaara730e552019-06-16 19:05:31 +02002517 c = *key;
2518 if (c == K_SPECIAL && key[1] != NUL)
2519 c = TO_SPECIAL(key[1], key[2]);
2520
Bram Moolenaara42d9452019-06-15 21:46:30 +02002521 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002522 rettv->v_type = VAR_BOOL;
2523 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002524
Bram Moolenaara730e552019-06-16 19:05:31 +02002525 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002526 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002527 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002528 res.vval.v_number = 0;
2529 else
2530 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002531 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002532 return;
2533 }
2534
2535 // Invoke callback
2536 res.v_type = VAR_NUMBER;
2537 popup_close_and_callback(wp, &res);
2538}
2539
2540/*
2541 * popup_dialog({text}, {options})
2542 */
2543 void
2544f_popup_dialog(typval_T *argvars, typval_T *rettv)
2545{
2546 popup_create(argvars, rettv, TYPE_DIALOG);
2547}
2548
2549/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002550 * popup_menu({text}, {options})
2551 */
2552 void
2553f_popup_menu(typval_T *argvars, typval_T *rettv)
2554{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002555 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002556}
2557
2558/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002559 * popup_notification({text}, {options})
2560 */
2561 void
2562f_popup_notification(typval_T *argvars, typval_T *rettv)
2563{
2564 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2565}
2566
2567/*
2568 * Find the popup window with window-ID "id".
2569 * If the popup window does not exist NULL is returned.
2570 * If the window is not a popup window, and error message is given.
2571 */
2572 static win_T *
2573find_popup_win(int id)
2574{
2575 win_T *wp = win_id2wp(id);
2576
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002577 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002578 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002579 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002580 return NULL;
2581 }
2582 return wp;
2583}
2584
2585/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002586 * popup_close({id})
2587 */
2588 void
2589f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2590{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002591 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002592 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002593
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002594 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2595 return;
2596
2597 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002598 if (
2599# ifdef FEAT_TERMINAL
2600 // if the popup contains a terminal it will become hidden
2601 curbuf->b_term == NULL &&
2602# endif
2603 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002604 return;
2605
2606 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002607 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002608 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002609}
2610
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002611 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002612popup_hide(win_T *wp)
2613{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002614#ifdef FEAT_TERMINAL
2615 if (error_if_term_popup_window())
2616 return;
2617#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002618 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2619 {
2620 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002621 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002622 redraw_all_later(NOT_VALID);
2623 popup_mask_refresh = TRUE;
2624 }
2625}
2626
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002627/*
2628 * popup_hide({id})
2629 */
2630 void
2631f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2632{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002633 int id;
2634 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002635
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002636 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2637 return;
2638
2639 id = (int)tv_get_number(argvars);
2640 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002641 if (wp != NULL)
2642 popup_hide(wp);
2643}
2644
2645 void
2646popup_show(win_T *wp)
2647{
2648 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002649 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002650 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002651 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002652 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002653 }
2654}
2655
2656/*
2657 * popup_show({id})
2658 */
2659 void
2660f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2661{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002662 int id;
2663 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002664
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002665 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2666 return;
2667
2668 id = (int)tv_get_number(argvars);
2669 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002670 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002671 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002672 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002673#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002674 if (wp->w_popup_flags & POPF_INFO)
2675 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002676#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002677 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002678}
2679
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002680/*
2681 * popup_settext({id}, {text})
2682 */
2683 void
2684f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2685{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002686 int id;
2687 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002688
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002689 if (in_vim9script()
2690 && (check_for_number_arg(argvars, 0) == FAIL
2691 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2692 return;
2693
2694 id = (int)tv_get_number(&argvars[0]);
2695 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002696 if (wp != NULL)
2697 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002698 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002699 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002700 else
2701 {
2702 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002703 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002704 popup_adjust_position(wp);
2705 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002706 }
2707}
2708
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002709 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002710popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002711{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002712 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002713 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002714 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002715 clear_cmdline = TRUE;
2716 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002717
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002718 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002719 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002720}
2721
Bram Moolenaar82106932020-03-13 14:34:38 +01002722 static void
2723error_for_popup_window(void)
2724{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002725 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002726}
2727
2728 int
2729error_if_popup_window(int also_with_term UNUSED)
2730{
2731 // win_execute() may set "curwin" to a popup window temporarily, but many
2732 // commands are disallowed then. When a terminal runs in the popup most
2733 // things are allowed. When a terminal is finished it can be closed.
2734 if (WIN_IS_POPUP(curwin)
2735# ifdef FEAT_TERMINAL
2736 && (also_with_term || curbuf->b_term == NULL)
2737 && !term_is_finished(curbuf)
2738# endif
2739 )
2740 {
2741 error_for_popup_window();
2742 return TRUE;
2743 }
2744 return FALSE;
2745}
2746
Bram Moolenaarec583842019-05-26 14:11:23 +02002747/*
2748 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002749 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002750 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002751 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002752 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002753popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002754{
2755 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002756 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002757 win_T *prev = NULL;
2758
Bram Moolenaarec583842019-05-26 14:11:23 +02002759 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002760 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002761 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002762 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002763 if (wp == curwin)
2764 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002765 if (!force)
2766 {
2767 error_for_popup_window();
2768 return FAIL;
2769 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002770 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002771 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002772 if (prev == NULL)
2773 first_popupwin = wp->w_next;
2774 else
2775 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002776 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002777 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002778 }
2779
Bram Moolenaarec583842019-05-26 14:11:23 +02002780 // go through tab-local popups
2781 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002782 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002783 return OK;
2784 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002785}
2786
2787/*
2788 * Close a popup window with Window-id "id" in tabpage "tp".
2789 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002790 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002791popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002792{
2793 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002794 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002795 win_T *prev = NULL;
2796
Bram Moolenaarec583842019-05-26 14:11:23 +02002797 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2798 if (wp->w_id == id)
2799 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002800 if (wp == curwin)
2801 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002802 if (!force)
2803 {
2804 error_for_popup_window();
2805 return FAIL;
2806 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002807 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002808 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002809 if (prev == NULL)
2810 *root = wp->w_next;
2811 else
2812 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002813 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002814 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002815 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002816 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002817}
2818
2819 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002820close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002821{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002822 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002823 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002824 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002825 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002826 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002827 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002828 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002829 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002830}
2831
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002832/*
2833 * popup_move({id}, {options})
2834 */
2835 void
2836f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2837{
Bram Moolenaarae943152019-06-16 22:54:14 +02002838 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002839 int id;
2840 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002841
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002842 if (in_vim9script()
2843 && (check_for_number_arg(argvars, 0) == FAIL
2844 || check_for_dict_arg(argvars, 1) == FAIL))
2845 return;
2846
2847 id = (int)tv_get_number(argvars);
2848 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002849 if (wp == NULL)
2850 return; // invalid {id}
2851
2852 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2853 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002854 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002855 return;
2856 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002857 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002858
Bram Moolenaarae943152019-06-16 22:54:14 +02002859 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002860
2861 if (wp->w_winrow + wp->w_height >= cmdline_row)
2862 clear_cmdline = TRUE;
2863 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002864}
2865
Bram Moolenaarbc133542019-05-29 20:26:46 +02002866/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002867 * popup_setoptions({id}, {options})
2868 */
2869 void
2870f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2871{
2872 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002873 int id;
2874 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002875 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002876
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002877 if (in_vim9script()
2878 && (check_for_number_arg(argvars, 0) == FAIL
2879 || check_for_dict_arg(argvars, 1) == FAIL))
2880 return;
2881
2882 id = (int)tv_get_number(argvars);
2883 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002884 if (wp == NULL)
2885 return; // invalid {id}
2886
2887 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2888 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002889 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002890 return;
2891 }
2892 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002893 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002894
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002895 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002896
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002897 if (old_firstline != wp->w_firstline)
2898 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002899 popup_adjust_position(wp);
2900}
2901
2902/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002903 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002904 */
2905 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002906f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002907{
2908 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002909 int id;
2910 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002911 int top_extra;
2912 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002913
2914 if (rettv_dict_alloc(rettv) == OK)
2915 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002916 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2917 return;
2918
2919 id = (int)tv_get_number(argvars);
2920 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002921 if (wp == NULL)
2922 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002923 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002924 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2925
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002926 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002927 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002928 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002929
Bram Moolenaarbc133542019-05-29 20:26:46 +02002930 dict_add_number(dict, "line", wp->w_winrow + 1);
2931 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002932 dict_add_number(dict, "width", wp->w_width + left_extra
2933 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2934 dict_add_number(dict, "height", wp->w_height + top_extra
2935 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002936
2937 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2938 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2939 dict_add_number(dict, "core_width", wp->w_width);
2940 dict_add_number(dict, "core_height", wp->w_height);
2941
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002942 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002943 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002944 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002945 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002946 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002947
2948 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002949 }
2950}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002951
2952/*
2953 * popup_list()
2954 */
2955 void
2956f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2957{
2958 win_T *wp;
2959 tabpage_T *tp;
2960
2961 if (rettv_list_alloc(rettv) != OK)
2962 return;
2963 FOR_ALL_POPUPWINS(wp)
2964 list_append_number(rettv->vval.v_list, wp->w_id);
2965 FOR_ALL_TABPAGES(tp)
2966 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2967 list_append_number(rettv->vval.v_list, wp->w_id);
2968}
2969
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002970/*
2971 * popup_locate({row}, {col})
2972 */
2973 void
2974f_popup_locate(typval_T *argvars, typval_T *rettv)
2975{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002976 int row;
2977 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002978 win_T *wp;
2979
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002980 if (in_vim9script()
2981 && (check_for_number_arg(argvars, 0) == FAIL
2982 || check_for_number_arg(argvars, 1) == FAIL))
2983 return;
2984
2985 row = tv_get_number(&argvars[0]) - 1;
2986 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002987 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002988 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002989 rettv->vval.v_number = wp->w_id;
2990}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002991
2992/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002993 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2994 */
2995 static void
2996get_padding_border(dict_T *dict, int *array, char *name)
2997{
2998 list_T *list;
2999 int i;
3000
3001 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3002 return;
3003
3004 list = list_alloc();
3005 if (list != NULL)
3006 {
3007 dict_add_list(dict, name, list);
3008 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3009 for (i = 0; i < 4; ++i)
3010 list_append_number(list, array[i]);
3011 }
3012}
3013
3014/*
3015 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3016 */
3017 static void
3018get_borderhighlight(dict_T *dict, win_T *wp)
3019{
3020 list_T *list;
3021 int i;
3022
3023 for (i = 0; i < 4; ++i)
3024 if (wp->w_border_highlight[i] != NULL)
3025 break;
3026 if (i == 4)
3027 return;
3028
3029 list = list_alloc();
3030 if (list != NULL)
3031 {
3032 dict_add_list(dict, "borderhighlight", list);
3033 for (i = 0; i < 4; ++i)
3034 list_append_string(list, wp->w_border_highlight[i], -1);
3035 }
3036}
3037
3038/*
3039 * For popup_getoptions(): add a "borderchars" entry to "dict".
3040 */
3041 static void
3042get_borderchars(dict_T *dict, win_T *wp)
3043{
3044 list_T *list;
3045 int i;
3046 char_u buf[NUMBUFLEN];
3047 int len;
3048
3049 for (i = 0; i < 8; ++i)
3050 if (wp->w_border_char[i] != 0)
3051 break;
3052 if (i == 8)
3053 return;
3054
3055 list = list_alloc();
3056 if (list != NULL)
3057 {
3058 dict_add_list(dict, "borderchars", list);
3059 for (i = 0; i < 8; ++i)
3060 {
3061 len = mb_char2bytes(wp->w_border_char[i], buf);
3062 list_append_string(list, buf, len);
3063 }
3064 }
3065}
3066
3067/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003068 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003069 */
3070 static void
3071get_moved_list(dict_T *dict, win_T *wp)
3072{
3073 list_T *list;
3074
3075 list = list_alloc();
3076 if (list != NULL)
3077 {
3078 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003079 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003080 list_append_number(list, wp->w_popup_mincol);
3081 list_append_number(list, wp->w_popup_maxcol);
3082 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003083 list = list_alloc();
3084 if (list != NULL)
3085 {
3086 dict_add_list(dict, "mousemoved", list);
3087 list_append_number(list, wp->w_popup_mouse_row);
3088 list_append_number(list, wp->w_popup_mouse_mincol);
3089 list_append_number(list, wp->w_popup_mouse_maxcol);
3090 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003091}
3092
3093/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003094 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003095 */
3096 void
3097f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3098{
3099 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003100 int id;
3101 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003102 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003103 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003104
3105 if (rettv_dict_alloc(rettv) == OK)
3106 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003107 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3108 return;
3109
3110 id = (int)tv_get_number(argvars);
3111 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003112 if (wp == NULL)
3113 return;
3114
3115 dict = rettv->vval.v_dict;
3116 dict_add_number(dict, "line", wp->w_wantline);
3117 dict_add_number(dict, "col", wp->w_wantcol);
3118 dict_add_number(dict, "minwidth", wp->w_minwidth);
3119 dict_add_number(dict, "minheight", wp->w_minheight);
3120 dict_add_number(dict, "maxheight", wp->w_maxheight);
3121 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003122 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003123 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003124 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003125 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003126 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003127 {
3128 proptype_T *pt = text_prop_type_by_id(
3129 wp->w_popup_prop_win->w_buffer,
3130 wp->w_popup_prop_type);
3131
3132 if (pt != NULL)
3133 dict_add_string(dict, "textprop", pt->pt_name);
3134 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3135 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3136 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003137 dict_add_string(dict, "title", wp->w_popup_title);
3138 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003139 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003140 dict_add_number(dict, "dragall",
3141 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003142 dict_add_number(dict, "mapping",
3143 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003144 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003145 dict_add_number(dict, "posinvert",
3146 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003147 dict_add_number(dict, "cursorline",
3148 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003149 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003150 if (wp->w_scrollbar_highlight != NULL)
3151 dict_add_string(dict, "scrollbarhighlight",
3152 wp->w_scrollbar_highlight);
3153 if (wp->w_thumb_highlight != NULL)
3154 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003155
Bram Moolenaara3fce622019-06-20 02:31:49 +02003156 // find the tabpage that holds this popup
3157 i = 1;
3158 FOR_ALL_TABPAGES(tp)
3159 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003160 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003161
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003162 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3163 if (twp->w_id == id)
3164 break;
3165 if (twp != NULL)
3166 break;
3167 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003168 }
3169 if (tp == NULL)
3170 i = -1; // must be global
3171 else if (tp == curtab)
3172 i = 0;
3173 dict_add_number(dict, "tabpage", i);
3174
Bram Moolenaarae943152019-06-16 22:54:14 +02003175 get_padding_border(dict, wp->w_popup_padding, "padding");
3176 get_padding_border(dict, wp->w_popup_border, "border");
3177 get_borderhighlight(dict, wp);
3178 get_borderchars(dict, wp);
3179 get_moved_list(dict, wp);
3180
3181 if (wp->w_filter_cb.cb_name != NULL)
3182 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3183 if (wp->w_close_cb.cb_name != NULL)
3184 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003185
K.Takataeeec2542021-06-02 13:28:16 +02003186 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003187 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3188 {
3189 dict_add_string(dict, "pos",
3190 (char_u *)poppos_entries[i].pp_name);
3191 break;
3192 }
3193
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003194 dict_add_string(dict, "close", (char_u *)(
3195 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3196 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3197
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003198# if defined(FEAT_TIMERS)
3199 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3200 ? (long)wp->w_popup_timer->tr_interval : 0L);
3201# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003202 }
3203}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003204
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003205# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003206/*
3207 * Return TRUE if the current window is running a terminal in a popup window.
3208 * Return FALSE when the job has ended.
3209 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003210 int
3211error_if_term_popup_window()
3212{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003213 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3214 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003215 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003216 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003217 return TRUE;
3218 }
3219 return FALSE;
3220}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003221# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003222
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003223/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003224 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003225 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003226 * Each calling function should use a different flag, see the list at
3227 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003228 */
3229 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003230popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003231{
3232 win_T *wp;
3233
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003234 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003235 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003236 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003237 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003238}
3239
3240/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003241 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003242 * Must have called popup_reset_handled() first.
3243 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3244 * popup with the highest zindex.
3245 */
3246 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003247find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003248{
3249 win_T *wp;
3250 win_T *found_wp;
3251 int found_zindex;
3252
3253 found_zindex = lowest ? INT_MAX : 0;
3254 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003255 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003256 if ((wp->w_popup_handled & handled_flag) == 0
3257 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003258 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003259 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003260 {
3261 found_zindex = wp->w_zindex;
3262 found_wp = wp;
3263 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003264 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003265 if ((wp->w_popup_handled & handled_flag) == 0
3266 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003267 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003268 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003269 {
3270 found_zindex = wp->w_zindex;
3271 found_wp = wp;
3272 }
3273
3274 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003275 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003276 return found_wp;
3277}
3278
3279/*
3280 * Invoke the filter callback for window "wp" with typed character "c".
3281 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003282 * Returns the return value of the filter or -1 for CTRL-C in the current
3283 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003284 * Careful: The filter may make "wp" invalid!
3285 */
3286 static int
3287invoke_popup_filter(win_T *wp, int c)
3288{
3289 int res;
3290 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003291 typval_T argv[3];
3292 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003293 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003294 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003295
Bram Moolenaara42d9452019-06-15 21:46:30 +02003296 // Emergency exit: CTRL-C closes the popup.
3297 if (c == Ctrl_C)
3298 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003299 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003300 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003301
3302 // Reset got_int to avoid the callback isn't called.
3303 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003304 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003305 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003306
3307 // If the popup is the current window it probably fails to close. Then
3308 // do not consume the key.
3309 if (was_curwin && wp == curwin)
3310 return -1;
3311 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003312 }
3313
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003314 argv[0].v_type = VAR_NUMBER;
3315 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3316
3317 // Convert the number to a string, so that the function can use:
3318 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003319 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003320 argv[1].v_type = VAR_STRING;
3321 argv[1].vval.v_string = vim_strsave(buf);
3322
3323 argv[2].v_type = VAR_UNKNOWN;
3324
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003325 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003326 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3327 {
3328 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003329 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003330 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003331 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003332 }
3333 else
3334 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003335 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3336 popup_highlight_curline(wp);
3337
Bram Moolenaar371806e2020-10-22 13:44:54 +02003338 // If an error message was given always return FALSE, so that keys are
3339 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003340 // If we get three errors in a row then close the popup. Decrement the
3341 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3342 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003343 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003344 {
3345 wp->w_filter_errors += 10;
3346 if (wp->w_filter_errors >= 30)
3347 popup_close_with_retval(wp, -1);
3348 res = FALSE;
3349 }
3350 else
3351 {
3352 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3353 --wp->w_filter_errors;
3354 res = tv_get_bool(&rettv);
3355 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003356 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003357
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003358 vim_free(argv[1].vval.v_string);
3359 clear_tv(&rettv);
3360 return res;
3361}
3362
3363/*
3364 * Called when "c" was typed: invoke popup filter callbacks.
3365 * Returns TRUE when the character was consumed,
3366 */
3367 int
3368popup_do_filter(int c)
3369{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003370 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003371 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003372 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003373 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003374 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003375 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003376
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003377#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003378 // Popup window with terminal always gets focus.
3379 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3380 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003381#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003382
Bram Moolenaar934470e2019-09-01 23:27:05 +02003383 if (recursive)
3384 return FALSE;
3385 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003386
Bram Moolenaarf6396232019-08-24 19:36:00 +02003387 if (c == K_LEFTMOUSE)
3388 {
3389 int row = mouse_row;
3390 int col = mouse_col;
3391
3392 wp = mouse_find_win(&row, &col, FIND_POPUP);
3393 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003394 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003395 }
3396
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003397 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003398 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003399 while (res == FALSE
3400 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003401 if (wp->w_filter_cb.cb_name != NULL
3402 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003403 res = invoke_popup_filter(wp, c);
3404
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003405 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003406 {
3407 int save_got_int = got_int;
3408
3409 // Reset got_int to avoid a function used in the statusline aborts.
3410 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003411 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003412 got_int |= save_got_int;
3413 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003414 recursive = FALSE;
3415 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003416
3417 // When interrupted return FALSE to avoid looping.
3418 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003419}
3420
Bram Moolenaar3397f742019-06-02 18:40:06 +02003421/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003422 * Return TRUE if there is a popup visible with a filter callback and the
3423 * "mapping" property off.
3424 */
3425 int
3426popup_no_mapping(void)
3427{
3428 int round;
3429 win_T *wp;
3430
3431 for (round = 1; round <= 2; ++round)
3432 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3433 wp != NULL; wp = wp->w_next)
3434 if (wp->w_filter_cb.cb_name != NULL
3435 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3436 return TRUE;
3437 return FALSE;
3438}
3439
3440/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003441 * Called when the cursor moved: check if any popup needs to be closed if the
3442 * cursor moved far enough.
3443 */
3444 void
3445popup_check_cursor_pos()
3446{
3447 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003448
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003449 popup_reset_handled(POPUP_HANDLED_3);
3450 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003451 if (wp->w_popup_curwin != NULL
3452 && (curwin != wp->w_popup_curwin
3453 || curwin->w_cursor.lnum != wp->w_popup_lnum
3454 || curwin->w_cursor.col < wp->w_popup_mincol
3455 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003456 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003457}
3458
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003459/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003460 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003461 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003462 static void
3463popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003464{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003465 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003466 char_u *cells;
3467 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003468
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003469 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3470 {
3471 vim_free(wp->w_popup_mask_cells);
3472 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003473 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003474 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003475 if (wp->w_popup_mask_cells != NULL
3476 && wp->w_popup_mask_height == height
3477 && wp->w_popup_mask_width == width)
3478 return; // cache is still valid
3479
3480 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003481 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003482 if (wp->w_popup_mask_cells == NULL)
3483 return;
3484 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003485
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003486 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003487 {
3488 int cols, cole;
3489 int lines, linee;
3490
3491 li = lio->li_tv.vval.v_list->lv_first;
3492 cols = tv_get_number(&li->li_tv);
3493 if (cols < 0)
3494 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003495 if (cols <= 0)
3496 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003497 li = li->li_next;
3498 cole = tv_get_number(&li->li_tv);
3499 if (cole < 0)
3500 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003501 if (cole > width)
3502 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003503 li = li->li_next;
3504 lines = tv_get_number(&li->li_tv);
3505 if (lines < 0)
3506 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003507 if (lines <= 0)
3508 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003509 li = li->li_next;
3510 linee = tv_get_number(&li->li_tv);
3511 if (linee < 0)
3512 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003513 if (linee > height)
3514 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003515
Bram Moolenaar4012d262020-12-29 11:57:46 +01003516 for (row = lines - 1; row < linee; ++row)
3517 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003518 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003519 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003520}
3521
3522/*
3523 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3524 * "col" and "line" are screen coordinates.
3525 */
3526 static int
3527popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3528{
3529 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3530 int line = screenline - wp->w_winrow;
3531
3532 return col >= 0 && col < width
3533 && line >= 0 && line < height
3534 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003535}
3536
3537/*
3538 * Set flags in popup_transparent[] for window "wp" to "val".
3539 */
3540 static void
3541update_popup_transparent(win_T *wp, int val)
3542{
3543 if (wp->w_popup_mask != NULL)
3544 {
3545 int width = popup_width(wp);
3546 int height = popup_height(wp);
3547 listitem_T *lio, *li;
3548 int cols, cole;
3549 int lines, linee;
3550 int col, line;
3551
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003552 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003553 {
3554 li = lio->li_tv.vval.v_list->lv_first;
3555 cols = tv_get_number(&li->li_tv);
3556 if (cols < 0)
3557 cols = width + cols + 1;
3558 li = li->li_next;
3559 cole = tv_get_number(&li->li_tv);
3560 if (cole < 0)
3561 cole = width + cole + 1;
3562 li = li->li_next;
3563 lines = tv_get_number(&li->li_tv);
3564 if (lines < 0)
3565 lines = height + lines + 1;
3566 li = li->li_next;
3567 linee = tv_get_number(&li->li_tv);
3568 if (linee < 0)
3569 linee = height + linee + 1;
3570
3571 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003572 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003573 if (cols < 0)
3574 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003575 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003576 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003577 if (lines < 0)
3578 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003579 for (line = lines; line < linee
3580 && line + wp->w_winrow < screen_Rows; ++line)
3581 for (col = cols; col < cole
3582 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003583 popup_transparent[(line + wp->w_winrow) * screen_Columns
3584 + col + wp->w_wincol] = val;
3585 }
3586 }
3587}
3588
3589/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003590 * Only called when popup window "wp" is hidden: If the window is positioned
3591 * next to a text property, and it is now visible, then unhide the popup.
3592 * We don't check if visible popups become hidden, that is done in
3593 * popup_adjust_position().
3594 * Return TRUE if the popup became unhidden.
3595 */
3596 static int
3597check_popup_unhidden(win_T *wp)
3598{
3599 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3600 {
3601 textprop_T prop;
3602 linenr_T lnum;
3603
3604 if (find_visible_prop(wp->w_popup_prop_win,
3605 wp->w_popup_prop_type, wp->w_popup_prop_id,
3606 &prop, &lnum) == OK)
3607 {
3608 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003609 wp->w_popup_prop_topline = 0; // force repositioning
3610 return TRUE;
3611 }
3612 }
3613 return FALSE;
3614}
3615
3616/*
3617 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3618 * That is when the buffer in the popup was changed, or the popup is following
3619 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003620 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003621 */
3622 static int
3623popup_need_position_adjust(win_T *wp)
3624{
3625 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3626 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003627 if (win_valid(wp->w_popup_prop_win)
3628 && (wp->w_popup_prop_changedtick
3629 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3630 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3631 return TRUE;
3632
3633 // May need to adjust the width if the cursor moved.
3634 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003635}
3636
3637/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003638 * Update "popup_mask" if needed.
3639 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003640 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003641 * Also marks window lines for redrawing.
3642 */
3643 void
3644may_update_popup_mask(int type)
3645{
3646 win_T *wp;
3647 short *mask;
3648 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003649 int redraw_all_popups = FALSE;
3650 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003651
3652 // Need to recompute when switching tabs.
3653 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3654 // (such as the screen size) must have changed.
3655 if (popup_mask_tab != curtab || type >= NOT_VALID)
3656 {
3657 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003658 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003659 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003660
3661 // Check if any popup window buffer has changed and if any popup connected
3662 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003663 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003664 if (wp->w_popup_flags & POPF_HIDDEN)
3665 popup_mask_refresh |= check_popup_unhidden(wp);
3666 else if (popup_need_position_adjust(wp))
3667 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003668 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003669 if (wp->w_popup_flags & POPF_HIDDEN)
3670 popup_mask_refresh |= check_popup_unhidden(wp);
3671 else if (popup_need_position_adjust(wp))
3672 popup_mask_refresh = TRUE;
3673
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003674 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003675 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003676
3677 // Need to update the mask, something has changed.
3678 popup_mask_refresh = FALSE;
3679 popup_mask_tab = curtab;
3680 popup_visible = FALSE;
3681
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003682 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003683 // If redrawing only what is needed, update "popup_mask_next" and then
3684 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003685 redrawing_all_win = TRUE;
3686 FOR_ALL_WINDOWS(wp)
3687 if (wp->w_redr_type < SOME_VALID)
3688 redrawing_all_win = FALSE;
3689 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003690 mask = popup_mask;
3691 else
3692 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003693 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003694
3695 // Find the window with the lowest zindex that hasn't been handled yet,
3696 // so that the window with a higher zindex overwrites the value in
3697 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003698 popup_reset_handled(POPUP_HANDLED_4);
3699 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003700 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003701 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003702 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003703
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003704 popup_visible = TRUE;
3705
Bram Moolenaar12034e22019-08-25 22:25:02 +02003706 // Recompute the position if the text changed. It may make the popup
3707 // hidden if it's attach to a text property that is no longer visible.
3708 if (redraw_all_popups || popup_need_position_adjust(wp))
3709 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003710 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003711 if (wp->w_popup_flags & POPF_HIDDEN)
3712 continue;
3713 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003714
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003715 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003716 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003717 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003718 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003719 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003720 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003721 col < wp->w_wincol + width - wp->w_popup_leftoff
3722 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003723 if (wp->w_zindex < POPUPMENU_ZINDEX
3724 && pum_visible()
3725 && pum_under_menu(line, col, FALSE))
3726 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3727 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003728 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003729 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003730 }
3731
3732 // Only check which lines are to be updated if not already
3733 // updating all lines.
3734 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003735 {
3736 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3737 win_T *prev_wp = NULL;
3738
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003739 for (line = 0; line < screen_Rows; ++line)
3740 {
3741 int col_done = 0;
3742
3743 for (col = 0; col < screen_Columns; ++col)
3744 {
3745 int off = line * screen_Columns + col;
3746
3747 if (popup_mask[off] != popup_mask_next[off])
3748 {
3749 popup_mask[off] = popup_mask_next[off];
3750
3751 if (line >= cmdline_row)
3752 {
3753 // the command line needs to be cleared if text below
3754 // the popup is now visible.
3755 if (!msg_scrolled && popup_mask_next[off] == 0)
3756 clear_cmdline = TRUE;
3757 }
3758 else if (col >= col_done)
3759 {
3760 linenr_T lnum;
3761 int line_cp = line;
3762 int col_cp = col;
3763
3764 // The screen position "line" / "col" needs to be
3765 // redrawn. Figure out what window that is and update
3766 // w_redraw_top and w_redr_bot. Only needs to be done
3767 // once for each window line.
3768 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3769 if (wp != NULL)
3770 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003771#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003772 // A terminal window needs to be redrawn.
3773 if (bt_terminal(wp->w_buffer))
3774 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003775 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003776#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003777 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003778 if (wp != prev_wp)
3779 {
3780 vim_memset(plines_cache, 0,
3781 sizeof(int) * Rows);
3782 prev_wp = wp;
3783 }
3784
3785 if (line_cp >= wp->w_height)
3786 // In (or below) status line
3787 wp->w_redr_status = TRUE;
3788 else
3789 {
3790 // compute the position in the buffer line
3791 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003792 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003793 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003794 redrawWinline(wp, lnum);
3795 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003796 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003797
3798 // This line is going to be redrawn, no need to
3799 // check until the right side of the window.
3800 col_done = wp->w_wincol + wp->w_width - 1;
3801 }
3802 }
3803 }
3804 }
3805 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003806
3807 vim_free(plines_cache);
3808 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003809
3810 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003811}
3812
3813/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003814 * If the current window is a popup and something relevant changed, recompute
3815 * the position and size.
3816 */
3817 void
3818may_update_popup_position(void)
3819{
3820 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3821 popup_adjust_position(curwin);
3822}
3823
3824/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003825 * Return a string of "len" spaces in IObuff.
3826 */
3827 static char_u *
3828get_spaces(int len)
3829{
3830 vim_memset(IObuff, ' ', (size_t)len);
3831 IObuff[len] = NUL;
3832 return IObuff;
3833}
3834
3835/*
3836 * Update popup windows. They are drawn on top of normal windows.
3837 * "win_update" is called for each popup window, lowest zindex first.
3838 */
3839 void
3840update_popups(void (*win_update)(win_T *wp))
3841{
3842 win_T *wp;
3843 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003844 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003845 int total_width;
3846 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003847 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003848 int popup_attr;
3849 int border_attr[4];
3850 int border_char[8];
3851 char_u buf[MB_MAXBYTES];
3852 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003853 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003854 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003855 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003856 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003857 int sb_thumb_top = 0;
3858 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003859 int attr_scroll = 0;
3860 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003861
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003862 // hide the cursor until redrawing is done.
3863 cursor_off();
3864
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003865 // Find the window with the lowest zindex that hasn't been updated yet,
3866 // so that the window with a higher zindex is drawn later, thus goes on
3867 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003868 popup_reset_handled(POPUP_HANDLED_5);
3869 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003870 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003871 int title_len = 0;
3872 int title_wincol;
3873
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003874 // This drawing uses the zindex of the popup window, so that it's on
3875 // top of the text but doesn't draw when another popup with higher
3876 // zindex is on top of the character.
3877 screen_zindex = wp->w_zindex;
3878
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003879 // Set flags in popup_transparent[] for masked cells.
3880 update_popup_transparent(wp, 1);
3881
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882 // adjust w_winrow and w_wincol for border and padding, since
3883 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003884 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003885 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3886 - wp->w_popup_leftoff;
3887 if (wp->w_wincol + left_extra < 0)
3888 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003889 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003890 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003891
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003892 // Draw the popup text, unless it's off screen.
3893 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003894 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003895 // May need to update the "cursorline" highlighting, which may also
3896 // change "topline"
3897 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3898 popup_highlight_curline(wp);
3899
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003900 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003901
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003902 // move the cursor into the visible lines, otherwise executing
3903 // commands with win_execute() may cause the text to jump.
3904 if (wp->w_cursor.lnum < wp->w_topline)
3905 wp->w_cursor.lnum = wp->w_topline;
3906 else if (wp->w_cursor.lnum >= wp->w_botline)
3907 wp->w_cursor.lnum = wp->w_botline - 1;
3908 }
3909
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003910 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003911 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003912
3913 // Add offset for border and padding if not done already.
3914 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3915 {
3916 wp->w_wcol += left_extra;
3917 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3918 }
3919 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003920 {
3921 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003922 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003923 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003924
Bram Moolenaareabddc42022-04-02 15:32:16 +01003925 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003926 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003927 popup_attr = get_wcr_attr(wp);
3928
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003929 if (wp->w_winrow + total_height > cmdline_row)
3930 wp->w_popup_flags |= POPF_ON_CMDLINE;
3931 else
3932 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3933
3934
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003935 // We can only use these line drawing characters when 'encoding' is
3936 // "utf-8" and 'ambiwidth' is "single".
3937 if (enc_utf8 && *p_ambw == 's')
3938 {
3939 border_char[0] = border_char[2] = 0x2550;
3940 border_char[1] = border_char[3] = 0x2551;
3941 border_char[4] = 0x2554;
3942 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003943 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3944 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003945 border_char[7] = 0x255a;
3946 }
3947 else
3948 {
3949 border_char[0] = border_char[2] = '-';
3950 border_char[1] = border_char[3] = '|';
3951 for (i = 4; i < 8; ++i)
3952 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003953 if (wp->w_popup_flags & POPF_RESIZE)
3954 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003955 }
3956 for (i = 0; i < 8; ++i)
3957 if (wp->w_border_char[i] != 0)
3958 border_char[i] = wp->w_border_char[i];
3959
3960 for (i = 0; i < 4; ++i)
3961 {
3962 border_attr[i] = popup_attr;
3963 if (wp->w_border_highlight[i] != NULL)
3964 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3965 }
3966
Bram Moolenaard91467f2020-11-21 12:42:09 +01003967 // Title goes on top of border or padding.
3968 title_wincol = wp->w_wincol + 1;
3969 if (wp->w_popup_title != NULL)
3970 {
rbtnnc6119412021-08-07 13:08:45 +02003971 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003972
Ralf Schandlbc869872021-05-28 14:12:14 +02003973 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003974 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02003975 {
3976 int title_byte_len = (int)STRLEN(wp->w_popup_title);
3977 char_u *title_text = alloc(title_byte_len + 1);
3978
3979 if (title_text != NULL)
3980 {
3981 trunc_string(wp->w_popup_title, title_text,
3982 total_width - 2, title_byte_len + 1);
3983 screen_puts(title_text, wp->w_winrow, title_wincol,
3984 wp->w_popup_border[0] > 0
3985 ? border_attr[0] : popup_attr);
3986 vim_free(title_text);
3987 }
3988
Bram Moolenaard91467f2020-11-21 12:42:09 +01003989 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02003990 }
3991 else
3992 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
3993 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003994 }
3995
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003996 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003997 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003998 if (wp->w_popup_border[0] > 0)
3999 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004000 // top border; do not draw over the title
4001 if (title_len > 0)
4002 {
4003 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4004 wincol < 0 ? 0 : wincol, title_wincol,
4005 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004006 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004007 border_char[0], border_attr[0]);
4008 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4009 title_wincol + title_len, wincol + total_width,
4010 border_char[0], border_char[0], border_attr[0]);
4011 }
4012 else
4013 {
4014 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4015 wincol < 0 ? 0 : wincol, wincol + total_width,
4016 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4017 ? border_char[4] : border_char[0],
4018 border_char[0], border_attr[0]);
4019 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004020 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004021 {
4022 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4023 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004024 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004025 }
4026 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004027 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4028 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004029
Bram Moolenaard529ba52019-07-02 23:13:53 +02004030 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4031 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004032 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004033 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004034 - wp->w_has_scrollbar;
4035 if (padcol < 0)
4036 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004037 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004038 padcol = 0;
4039 }
4040 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004041 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004042 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004043 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004044 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004045 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004046 // top padding and no border; do not draw over the title
4047 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004048 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004049 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004050 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004051 row += 1;
4052 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004053 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004054 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004055 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004056 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004057
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004058 // Compute scrollbar thumb position and size.
4059 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004060 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004061 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4062 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004063
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004064 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4065 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004066 if (wp->w_topline > 1 && sb_thumb_height == height)
4067 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004068 if (sb_thumb_height == 0)
4069 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004070 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004071 // it just fits, avoid divide by zero
4072 sb_thumb_top = 0;
4073 else
4074 sb_thumb_top = (wp->w_topline - 1
4075 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004076 * (wp->w_height - sb_thumb_height)
4077 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004078 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4079 sb_thumb_top = 1; // show it's scrolled
4080
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004081 if (wp->w_scrollbar_highlight != NULL)
4082 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4083 else
4084 attr_scroll = highlight_attr[HLF_PSB];
4085 if (wp->w_thumb_highlight != NULL)
4086 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4087 else
4088 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004089 }
4090
4091 for (i = wp->w_popup_border[0];
4092 i < total_height - wp->w_popup_border[2]; ++i)
4093 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004094 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004095 // left and right padding only needed next to the body
4096 int do_padding =
4097 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4098 && i < total_height - wp->w_popup_border[2]
4099 - wp->w_popup_padding[2];
4100
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004101 row = wp->w_winrow + i;
4102
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004103 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004104 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004105 {
4106 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004107 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004108 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004109 if (do_padding && wp->w_popup_padding[3] > 0)
4110 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004111 int col = wincol + wp->w_popup_border[3];
4112
Bram Moolenaard529ba52019-07-02 23:13:53 +02004113 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004114 pad_left = wp->w_popup_padding[3];
4115 if (col < 0)
4116 {
4117 pad_left += col;
4118 col = 0;
4119 }
4120 if (pad_left > 0)
4121 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4122 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004123 // scrollbar
4124 if (wp->w_has_scrollbar)
4125 {
4126 int line = i - top_off;
4127 int scroll_col = wp->w_wincol + total_width - 1
4128 - wp->w_popup_border[1];
4129
4130 if (line >= 0 && line < wp->w_height)
4131 screen_putchar(' ', row, scroll_col,
4132 line >= sb_thumb_top
4133 && line < sb_thumb_top + sb_thumb_height
4134 ? attr_thumb : attr_scroll);
4135 else
4136 screen_putchar(' ', row, scroll_col, popup_attr);
4137 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004138 // right border
4139 if (wp->w_popup_border[1] > 0)
4140 {
4141 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004142 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004143 }
4144 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004145 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004146 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004147 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004148 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4149 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004150 }
4151
4152 if (wp->w_popup_padding[2] > 0)
4153 {
4154 // bottom padding
4155 row = wp->w_winrow + wp->w_popup_border[0]
4156 + wp->w_popup_padding[0] + wp->w_height;
4157 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004158 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004159 }
4160
4161 if (wp->w_popup_border[2] > 0)
4162 {
4163 // bottom border
4164 row = wp->w_winrow + total_height - 1;
4165 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004166 wincol < 0 ? 0 : wincol,
4167 wincol + total_width,
4168 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004169 ? border_char[7] : border_char[2],
4170 border_char[2], border_attr[2]);
4171 if (wp->w_popup_border[1] > 0)
4172 {
4173 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004174 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004175 }
4176 }
4177
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004178 if (wp->w_popup_close == POPCLOSE_BUTTON)
4179 {
4180 // close button goes on top of anything at the top-right corner
4181 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004182 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004183 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4184 }
4185
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004186 update_popup_transparent(wp, 0);
4187
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004188 // Back to the normal zindex.
4189 screen_zindex = 0;
4190 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004191
4192#if defined(FEAT_SEARCH_EXTRA)
4193 // In case win_update() called start_search_hl().
4194 end_search_hl();
4195#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004196}
4197
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004198/*
4199 * Mark references in callbacks of one popup window.
4200 */
4201 static int
4202set_ref_in_one_popup(win_T *wp, int copyID)
4203{
4204 int abort = FALSE;
4205 typval_T tv;
4206
4207 if (wp->w_close_cb.cb_partial != NULL)
4208 {
4209 tv.v_type = VAR_PARTIAL;
4210 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4211 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4212 }
4213 if (wp->w_filter_cb.cb_partial != NULL)
4214 {
4215 tv.v_type = VAR_PARTIAL;
4216 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4217 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4218 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004219 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004220 return abort;
4221}
4222
4223/*
4224 * Set reference in callbacks of popup windows.
4225 */
4226 int
4227set_ref_in_popups(int copyID)
4228{
4229 int abort = FALSE;
4230 win_T *wp;
4231 tabpage_T *tp;
4232
4233 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4234 abort = abort || set_ref_in_one_popup(wp, copyID);
4235
4236 FOR_ALL_TABPAGES(tp)
4237 {
4238 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4239 abort = abort || set_ref_in_one_popup(wp, copyID);
4240 if (abort)
4241 break;
4242 }
4243 return abort;
4244}
Bram Moolenaar79648732019-07-18 21:43:07 +02004245
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004246 int
4247popup_is_popup(win_T *wp)
4248{
4249 return wp->w_popup_flags != 0;
4250}
4251
4252#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004253/*
4254 * Find an existing popup used as the preview window, in the current tab page.
4255 * Return NULL if not found.
4256 */
4257 win_T *
4258popup_find_preview_window(void)
4259{
4260 win_T *wp;
4261
4262 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004263 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004264 if (wp->w_p_pvw)
4265 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004266 return NULL;
4267}
4268
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004269/*
4270 * Find an existing popup used as the info window, in the current tab page.
4271 * Return NULL if not found.
4272 */
4273 win_T *
4274popup_find_info_window(void)
4275{
4276 win_T *wp;
4277
4278 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004279 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004280 if (wp->w_popup_flags & POPF_INFO)
4281 return wp;
4282 return NULL;
4283}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004284#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004285
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004286 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004287f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4288{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004289#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004290 win_T *wp = popup_find_info_window();
4291
4292 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004293#else
4294 rettv->vval.v_number = 0;
4295#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004296}
4297
4298 void
4299f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004300{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004301#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004302 win_T *wp = popup_find_preview_window();
4303
4304 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004305#else
4306 rettv->vval.v_number = 0;
4307#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004308}
4309
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004310#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004311/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004312 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004313 * NOTE: this makes the popup the current window, so that the file can be
4314 * edited. However, it must not remain to be the current window, the caller
4315 * must make sure of that.
4316 */
4317 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004318popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004319{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004320 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004321
4322 if (wp == NULL)
4323 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004324 if (info)
4325 wp->w_popup_flags |= POPF_INFO;
4326 else
4327 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004328
4329 // Set the width to a reasonable value, so that w_topline can be computed.
4330 if (wp->w_minwidth > 0)
4331 wp->w_width = wp->w_minwidth;
4332 else if (wp->w_maxwidth > 0)
4333 wp->w_width = wp->w_maxwidth;
4334 else
4335 wp->w_width = curwin->w_width;
4336
4337 // Will switch to another buffer soon, dummy one can be wiped.
4338 wp->w_buffer->b_locked = FALSE;
4339
4340 win_enter(wp, FALSE);
4341 return OK;
4342}
4343
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004344/*
4345 * Close any preview popup.
4346 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004347 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004348popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004349{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004350 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004351
4352 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004353 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004354}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004355
4356/*
4357 * Hide the info popup.
4358 */
4359 void
4360popup_hide_info(void)
4361{
4362 win_T *wp = popup_find_info_window();
4363
4364 if (wp != NULL)
4365 popup_hide(wp);
4366}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004367
4368/*
4369 * Close any info popup.
4370 */
4371 void
4372popup_close_info(void)
4373{
4374 win_T *wp = popup_find_info_window();
4375
4376 if (wp != NULL)
4377 popup_close_with_retval(wp, -1);
4378}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004379#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004380
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004381/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004382 * Close any popup for a text property associated with "win".
4383 * Return TRUE if a popup was closed.
4384 */
4385 int
4386popup_win_closed(win_T *win)
4387{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004388 int round;
4389 win_T *wp;
4390 win_T *next;
4391 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004392
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004393 for (round = 1; round <= 2; ++round)
4394 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4395 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004396 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004397 next = wp->w_next;
4398 if (wp->w_popup_prop_win == win)
4399 {
4400 popup_close_with_retval(wp, -1);
4401 ret = TRUE;
4402 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004403 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004404 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004405}
4406
4407/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004408 * Set the title of the popup window to the file name.
4409 */
4410 void
4411popup_set_title(win_T *wp)
4412{
4413 if (wp->w_buffer->b_fname != NULL)
4414 {
4415 char_u dirname[MAXPATHL];
4416 size_t len;
4417
4418 mch_dirname(dirname, MAXPATHL);
4419 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4420
4421 vim_free(wp->w_popup_title);
4422 len = STRLEN(wp->w_buffer->b_fname) + 3;
4423 wp->w_popup_title = alloc((int)len);
4424 if (wp->w_popup_title != NULL)
4425 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4426 wp->w_buffer->b_fname);
4427 redraw_win_later(wp, VALID);
4428 }
4429}
4430
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004431# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004432/*
4433 * If there is a preview window, update the title.
4434 * Used after changing directory.
4435 */
4436 void
4437popup_update_preview_title(void)
4438{
4439 win_T *wp = popup_find_preview_window();
4440
4441 if (wp != NULL)
4442 popup_set_title(wp);
4443}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004444# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004445
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004446#endif // FEAT_PROP_POPUP