blob: 8292dba32e238c68f3a6af2fca9c91371448cc01 [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 Moolenaar43568642022-08-28 13:02:45 +010031#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +010032// Window used for ":echowindow"
Bram Moolenaar43568642022-08-28 13:02:45 +010033static win_T *message_win = NULL;
34#endif
35
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020036static void popup_adjust_position(win_T *wp);
37
Bram Moolenaar4d784b22019-05-25 19:51:39 +020038/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020039 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020041 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020042 */
43 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020044popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020045{
46 dictitem_T *di;
47 char_u *val;
48 char_u *s;
49 char_u *endp;
50 int n = 0;
51
52 di = dict_find(dict, key, -1);
53 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020054 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020055
56 val = tv_get_string(&di->di_tv);
57 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020058 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059
60 setcursor_mayforce(TRUE);
61 s = val + 6;
62 if (*s != NUL)
63 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020064 endp = s;
65 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
66 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020067 if (endp != NULL && *skipwhite(endp) != NUL)
68 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020069 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020070 return 0;
71 }
72 }
73
74 if (STRCMP(key, "line") == 0)
75 n = screen_screenrow() + 1 + n;
76 else // "col"
77 n = screen_screencol() + 1 + n;
78
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020079 // Zero means "not set", use -1 instead.
80 if (n == 0)
81 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020082 return n;
83}
84
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020085 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020086set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020087{
88 dictitem_T *di;
89
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020090 di = dict_find(dict, (char_u *)name, -1);
91 if (di != NULL)
92 {
93 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000094 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020095 else
96 {
97 list_T *list = di->di_tv.vval.v_list;
98 listitem_T *li;
99 int i;
100 int nr;
101
102 for (i = 0; i < 4; ++i)
103 array[i] = 1;
104 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100105 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200106 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200107 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
108 ++i, li = li->li_next)
109 {
110 nr = (int)tv_get_number(&li->li_tv);
111 if (nr >= 0)
112 array[i] = nr > max_val ? max_val : nr;
113 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100114 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200115 }
116 }
117}
118
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200119/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200120 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200121 */
122 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200123set_moved_values(win_T *wp)
124{
125 wp->w_popup_curwin = curwin;
126 wp->w_popup_lnum = curwin->w_cursor.lnum;
127 wp->w_popup_mincol = curwin->w_cursor.col;
128 wp->w_popup_maxcol = curwin->w_cursor.col;
129}
130
131/*
132 * Used when popup options contain "moved" with "word" or "WORD".
133 */
134 static void
135set_moved_columns(win_T *wp, int flags)
136{
137 char_u *ptr;
138 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
139
140 if (len > 0)
141 {
142 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
143 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
144 }
145}
146
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200147/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200148 * Used when popup options contain "mousemoved": set default moved values.
149 */
150 static void
151set_mousemoved_values(win_T *wp)
152{
153 wp->w_popup_mouse_row = mouse_row;
154 wp->w_popup_mouse_mincol = mouse_col;
155 wp->w_popup_mouse_maxcol = mouse_col;
156}
157
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000158 static void
159update_popup_uses_mouse_move(void)
160{
161 popup_uses_mouse_move = FALSE;
162 if (popup_visible)
163 {
164 win_T *wp;
165
166 FOR_ALL_POPUPWINS(wp)
167 if (wp->w_popup_mouse_row != 0)
168 {
169 popup_uses_mouse_move = TRUE;
170 return;
171 }
172 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
173 if (wp->w_popup_mouse_row != 0)
174 {
175 popup_uses_mouse_move = TRUE;
176 return;
177 }
178 }
179}
180
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200223 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
224 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200225 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200226 * Caller should check the left mouse button was clicked.
227 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200228 */
229 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200230popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200231{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200232 if (wp->w_popup_close == POPCLOSE_BUTTON
233 && row == 0 && col == popup_width(wp) - 1)
234 {
235 popup_close_for_mouse_click(wp);
236 return TRUE;
237 }
238 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200239}
240
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200241// Values set when dragging a popup window starts.
242static int drag_start_row;
243static int drag_start_col;
244static int drag_start_wantline;
245static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200246static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200247
248/*
249 * Mouse down on border of popup window: start dragging it.
250 * Uses mouse_col and mouse_row.
251 */
252 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200253popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200254{
255 drag_start_row = mouse_row;
256 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200257 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200258 drag_start_wantline = wp->w_winrow + 1;
259 else
260 drag_start_wantline = wp->w_wantline;
261 if (wp->w_wantcol == 0)
262 drag_start_wantcol = wp->w_wincol + 1;
263 else
264 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200265
266 // Stop centering the popup
267 if (wp->w_popup_pos == POPPOS_CENTER)
268 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269
270 drag_on_resize_handle = wp->w_popup_border[1] > 0
271 && wp->w_popup_border[2] > 0
272 && row == popup_height(wp) - 1
273 && col == popup_width(wp) - 1;
274
275 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
276 {
277 if (wp->w_popup_pos == POPPOS_TOPRIGHT
278 || wp->w_popup_pos == POPPOS_BOTRIGHT)
279 wp->w_wantcol = wp->w_wincol + 1;
280 if (wp->w_popup_pos == POPPOS_BOTLEFT)
281 wp->w_wantline = wp->w_winrow + 1;
282 wp->w_popup_pos = POPPOS_TOPLEFT;
283 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284}
285
286/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200287 * Mouse moved while dragging a popup window: adjust the window popup position
288 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200289 */
290 void
291popup_drag(win_T *wp)
292{
293 // The popup may be closed before dragging stops.
294 if (!win_valid_popup(wp))
295 return;
296
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200297 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
298 {
299 int width_inc = mouse_col - drag_start_col;
300 int height_inc = mouse_row - drag_start_row;
301
302 if (width_inc != 0)
303 {
304 int width = wp->w_width + width_inc;
305
306 if (width < 1)
307 width = 1;
308 wp->w_minwidth = width;
309 wp->w_maxwidth = width;
310 drag_start_col = mouse_col;
311 }
312
313 if (height_inc != 0)
314 {
315 int height = wp->w_height + height_inc;
316
317 if (height < 1)
318 height = 1;
319 wp->w_minheight = height;
320 wp->w_maxheight = height;
321 drag_start_row = mouse_row;
322 }
323
324 popup_adjust_position(wp);
325 return;
326 }
327
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000328 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200329 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200330 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
331 if (wp->w_wantline < 1)
332 wp->w_wantline = 1;
333 if (wp->w_wantline > Rows)
334 wp->w_wantline = Rows;
335 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
336 if (wp->w_wantcol < 1)
337 wp->w_wantcol = 1;
338 if (wp->w_wantcol > Columns)
339 wp->w_wantcol = Columns;
340
341 popup_adjust_position(wp);
342}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200343
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200344/*
345 * Set w_firstline to match the current "wp->w_topline".
346 */
347 void
348popup_set_firstline(win_T *wp)
349{
350 int height = wp->w_height;
351
352 wp->w_firstline = wp->w_topline;
353 popup_adjust_position(wp);
354
355 // we don't want the popup to get smaller, decrement the first line
356 // until it doesn't
357 while (wp->w_firstline > 1 && wp->w_height < height)
358 {
359 --wp->w_firstline;
360 popup_adjust_position(wp);
361 }
362}
363
364/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200365 * Return TRUE if the position is in the popup window scrollbar.
366 */
367 int
368popup_is_in_scrollbar(win_T *wp, int row, int col)
369{
370 return wp->w_has_scrollbar
371 && row >= wp->w_popup_border[0]
372 && row < popup_height(wp) - wp->w_popup_border[2]
373 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
374}
375
376
377/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200378 * Handle a click in a popup window, if it is in the scrollbar.
379 */
380 void
381popup_handle_scrollbar_click(win_T *wp, int row, int col)
382{
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200383 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200384 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100385 int height = popup_height(wp);
386 int new_topline = wp->w_topline;
387
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200388 if (row >= height / 2)
389 {
390 // Click in lower half, scroll down.
391 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaard28950f2022-05-29 14:13:04 +0100392 ++new_topline;
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200393 }
394 else if (wp->w_topline > 1)
395 // click on upper half, scroll up.
Bram Moolenaard28950f2022-05-29 14:13:04 +0100396 --new_topline;
397 if (new_topline != wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200398 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100399 set_topline(wp, new_topline);
400 if (wp == curwin)
401 {
402 if (wp->w_cursor.lnum < wp->w_topline)
403 {
404 wp->w_cursor.lnum = wp->w_topline;
405 check_cursor();
406 }
407 else if (wp->w_cursor.lnum >= wp->w_botline)
408 {
409 wp->w_cursor.lnum = wp->w_botline - 1;
410 check_cursor();
411 }
412 }
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200413 popup_set_firstline(wp);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100414 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200415 }
416 }
417}
418
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200419#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100420/*
421 * Add a timer to "wp" with "time".
422 * If "close" is true use popup_close(), otherwise popup_hide().
423 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200424 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100425popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200426{
427 char_u cbbuf[50];
428 char_u *ptr = cbbuf;
429 typval_T tv;
430
431 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100432 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
433 wp->w_id);
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200434 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200435 {
436 wp->w_popup_timer = create_timer(time, 0);
437 wp->w_popup_timer->tr_callback = get_callback(&tv);
438 clear_tv(&tv);
439 }
440}
441#endif
442
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100443 static poppos_T
444get_pos_entry(dict_T *d, int give_error)
445{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100446 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100447 int nr;
448
449 if (str == NULL)
450 return POPPOS_NONE;
451
K.Takataeeec2542021-06-02 13:28:16 +0200452 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100453 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
454 return poppos_entries[nr].pp_val;
455
456 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000457 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100458 return POPPOS_NONE;
459}
460
Bram Moolenaar17627312019-06-02 19:53:44 +0200461/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200462 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200463 */
464 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200465apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200466{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200467 int nr;
468 char_u *str;
469 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200470
Bram Moolenaard61efa52022-07-23 09:52:04 +0100471 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200472 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100473 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200474 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100475 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200476 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100477 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200478 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200479
480 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200481 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200482 wp->w_wantline = nr;
483 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200484 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200485 wp->w_wantcol = nr;
486
Bram Moolenaar55881332020-08-18 13:04:15 +0200487
Bram Moolenaard61efa52022-07-23 09:52:04 +0100488 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200489 if (nr != -1)
490 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200491
Bram Moolenaar12034e22019-08-25 22:25:02 +0200492 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100493 poppos_T ppt = get_pos_entry(d, TRUE);
494
495 if (ppt != POPPOS_NONE)
496 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200497 }
498
Bram Moolenaard61efa52022-07-23 09:52:04 +0100499 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200500 if (str != NULL)
501 {
502 wp->w_popup_prop_type = 0;
503 if (*str != NUL)
504 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100505 wp->w_popup_prop_win = curwin;
506 di = dict_find(d, (char_u *)"textpropwin", -1);
507 if (di != NULL)
508 {
509 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100510 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100511 wp->w_popup_prop_win = curwin;
512 }
513
514 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200515 if (nr <= 0)
516 nr = find_prop_type_id(str, NULL);
517 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000518 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200519 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200520 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200521 }
522 }
523
524 di = dict_find(d, (char_u *)"textpropid", -1);
525 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100526 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200527}
528
Bram Moolenaarb0992022020-01-30 14:55:42 +0100529/*
530 * Handle "moved" and "mousemoved" arguments.
531 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200532 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200533handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
534{
535 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
536 {
537 char_u *s = di->di_tv.vval.v_string;
538 int flags = 0;
539
540 if (STRCMP(s, "word") == 0)
541 flags = FIND_IDENT | FIND_STRING;
542 else if (STRCMP(s, "WORD") == 0)
543 flags = FIND_STRING;
544 else if (STRCMP(s, "expr") == 0)
545 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
546 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000547 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200548 if (flags != 0)
549 {
550 if (mousemoved)
551 set_mousemoved_columns(wp, flags);
552 else
553 set_moved_columns(wp, flags);
554 }
555 }
556 else if (di->di_tv.v_type == VAR_LIST
557 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200558 && (di->di_tv.vval.v_list->lv_len == 2
559 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200560 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200561 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100562 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200563 int mincol;
564 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200565
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200566 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100567 li = l->lv_first;
568 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200569 {
570 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
571
572 // Three numbers, might be from popup_getoptions().
573 if (mousemoved)
574 wp->w_popup_mouse_row = nr;
575 else
576 wp->w_popup_lnum = nr;
577 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100578 if (nr == 0)
579 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200580 }
581
582 mincol = tv_get_number(&li->li_tv);
583 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200584 if (mousemoved)
585 {
586 wp->w_popup_mouse_mincol = mincol;
587 wp->w_popup_mouse_maxcol = maxcol;
588 }
589 else
590 {
591 wp->w_popup_mincol = mincol;
592 wp->w_popup_maxcol = maxcol;
593 }
594 }
595 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000596 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200597}
598
599 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200600check_highlight(dict_T *dict, char *name, char_u **pval)
601{
602 dictitem_T *di;
603 char_u *str;
604
605 di = dict_find(dict, (char_u *)name, -1);
606 if (di != NULL)
607 {
608 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000609 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200610 else
611 {
612 str = tv_get_string(&di->di_tv);
613 if (*str != NUL)
614 *pval = vim_strsave(str);
615 }
616 }
617}
618
Bram Moolenaarae943152019-06-16 22:54:14 +0200619/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200620 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200621 */
622 static void
623popup_show_curline(win_T *wp)
624{
625 if (wp->w_cursor.lnum < wp->w_topline)
626 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200627 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200628 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200629 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200630 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200631 if (wp->w_topline < 1)
632 wp->w_topline = 1;
633 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
634 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200635 while (wp->w_topline < wp->w_cursor.lnum
636 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
637 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
638 > wp->w_height)
639 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200640 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200641
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200642 // Don't let "firstline" cause a scroll.
643 if (wp->w_firstline > 0)
644 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200645}
646
647/*
648 * Get the sign group name for window "wp".
649 * Returns a pointer to a static buffer, overwritten on the next call.
650 */
651 static char_u *
652popup_get_sign_name(win_T *wp)
653{
654 static char buf[30];
655
656 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
657 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200658}
659
660/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200661 * Highlight the line with the cursor.
662 * Also scrolls the text to put the cursor line in view.
663 */
664 static void
665popup_highlight_curline(win_T *wp)
666{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200667 int sign_id = 0;
668 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200669
Bram Moolenaar72570732019-11-30 14:21:53 +0100670 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200671
672 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
673 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200674 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200675
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200676 if (!sign_exists_by_name(sign_name))
677 {
678 char *linehl = "PopupSelected";
679
680 if (syn_name2id((char_u *)linehl) == 0)
681 linehl = "PmenuSel";
Bram Moolenaar9198de32022-08-27 21:30:03 +0100682 sign_define_by_name(sign_name, NULL, (char_u *)linehl,
683 NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200684 }
685
Bram Moolenaar72570732019-11-30 14:21:53 +0100686 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200687 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100688 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200689 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200690 else
691 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200692 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200693}
694
695/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200696 * Shared between popup_create() and f_popup_setoptions().
697 */
698 static void
699apply_general_options(win_T *wp, dict_T *dict)
700{
701 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200702 int nr;
703 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200704
Bram Moolenaarae943152019-06-16 22:54:14 +0200705 // TODO: flip
706
707 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200708 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200709 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100710 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200711 if (wp->w_firstline < 0)
712 wp->w_firstline = -1;
713 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200714
Bram Moolenaard61efa52022-07-23 09:52:04 +0100715 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200716 if (nr != -1)
717 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200718
Bram Moolenaard61efa52022-07-23 09:52:04 +0100719 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200720 if (str != NULL)
721 {
722 vim_free(wp->w_popup_title);
723 wp->w_popup_title = vim_strsave(str);
724 }
725
Bram Moolenaard61efa52022-07-23 09:52:04 +0100726 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200727 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200728 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200729
Bram Moolenaard61efa52022-07-23 09:52:04 +0100730 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200731 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200732 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200733 if (nr)
734 wp->w_popup_flags |= POPF_DRAG;
735 else
736 wp->w_popup_flags &= ~POPF_DRAG;
737 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100738 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000739 if (nr != -1)
740 {
741 if (nr)
742 wp->w_popup_flags |= POPF_DRAGALL;
743 else
744 wp->w_popup_flags &= ~POPF_DRAGALL;
745 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200746
Bram Moolenaard61efa52022-07-23 09:52:04 +0100747 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200748 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100749 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100750 if (nr)
751 wp->w_popup_flags |= POPF_POSINVERT;
752 else
753 wp->w_popup_flags &= ~POPF_POSINVERT;
754 }
755
Bram Moolenaard61efa52022-07-23 09:52:04 +0100756 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200757 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200758 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200759 if (nr)
760 wp->w_popup_flags |= POPF_RESIZE;
761 else
762 wp->w_popup_flags &= ~POPF_RESIZE;
763 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200764
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200765 di = dict_find(dict, (char_u *)"close", -1);
766 if (di != NULL)
767 {
768 int ok = TRUE;
769
770 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
771 {
772 char_u *s = di->di_tv.vval.v_string;
773
774 if (STRCMP(s, "none") == 0)
775 wp->w_popup_close = POPCLOSE_NONE;
776 else if (STRCMP(s, "button") == 0)
777 wp->w_popup_close = POPCLOSE_BUTTON;
778 else if (STRCMP(s, "click") == 0)
779 wp->w_popup_close = POPCLOSE_CLICK;
780 else
781 ok = FALSE;
782 }
783 else
784 ok = FALSE;
785 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000786 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200787 }
788
Bram Moolenaard61efa52022-07-23 09:52:04 +0100789 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200790 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000791 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200792 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
793 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000794#ifdef FEAT_TERMINAL
795 term_update_wincolor(wp);
796#endif
797 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200798
Bram Moolenaarae943152019-06-16 22:54:14 +0200799 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
800 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200801
Bram Moolenaar790498b2019-06-01 22:15:29 +0200802 di = dict_find(dict, (char_u *)"borderhighlight", -1);
803 if (di != NULL)
804 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200805 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000806 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200807 else
808 {
809 list_T *list = di->di_tv.vval.v_list;
810 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200811 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200812
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200813 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200814 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
815 ++i, li = li->li_next)
816 {
817 str = tv_get_string(&li->li_tv);
818 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100819 {
820 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200821 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100822 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200823 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200824 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
825 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100826 {
827 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200828 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200829 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100830 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200831 }
832 }
833
Bram Moolenaar790498b2019-06-01 22:15:29 +0200834 di = dict_find(dict, (char_u *)"borderchars", -1);
835 if (di != NULL)
836 {
837 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000838 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200839 else
840 {
841 list_T *list = di->di_tv.vval.v_list;
842 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200843 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200844
845 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100846 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200847 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200848 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
849 ++i, li = li->li_next)
850 {
851 str = tv_get_string(&li->li_tv);
852 if (*str != NUL)
853 wp->w_border_char[i] = mb_ptr2char(str);
854 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200855 if (list->lv_len == 1)
856 for (i = 1; i < 8; ++i)
857 wp->w_border_char[i] = wp->w_border_char[0];
858 if (list->lv_len == 2)
859 {
860 for (i = 4; i < 8; ++i)
861 wp->w_border_char[i] = wp->w_border_char[1];
862 for (i = 1; i < 4; ++i)
863 wp->w_border_char[i] = wp->w_border_char[0];
864 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200865 }
866 }
867 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200868
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200869 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
870 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
871
Bram Moolenaarae943152019-06-16 22:54:14 +0200872 di = dict_find(dict, (char_u *)"zindex", -1);
873 if (di != NULL)
874 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100875 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200876 if (wp->w_zindex < 1)
877 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
878 if (wp->w_zindex > 32000)
879 wp->w_zindex = 32000;
880 }
881
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200882 di = dict_find(dict, (char_u *)"mask", -1);
883 if (di != NULL)
884 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200885 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200886
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200887 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200888 {
889 listitem_T *li;
890
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200891 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200892 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200893 {
894 if (li->li_tv.v_type != VAR_LIST
895 || li->li_tv.vval.v_list == NULL
896 || li->li_tv.vval.v_list->lv_len != 4)
897 {
898 ok = FALSE;
899 break;
900 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100901 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200902 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200903 }
904 }
905 if (ok)
906 {
907 wp->w_popup_mask = di->di_tv.vval.v_list;
908 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200909 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200910 }
911 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000912 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200913 }
914
Bram Moolenaarae943152019-06-16 22:54:14 +0200915#if defined(FEAT_TIMERS)
916 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100917 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200918 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100919 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200920#endif
921
Bram Moolenaar3397f742019-06-02 18:40:06 +0200922 di = dict_find(dict, (char_u *)"moved", -1);
923 if (di != NULL)
924 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200925 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200926 handle_moved_argument(wp, di, FALSE);
927 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200928
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200929 di = dict_find(dict, (char_u *)"mousemoved", -1);
930 if (di != NULL)
931 {
932 set_mousemoved_values(wp);
933 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200934 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200935
Bram Moolenaard61efa52022-07-23 09:52:04 +0100936 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100937 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200938 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100939 if (nr != 0)
940 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200941 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100942 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200943 }
944
Bram Moolenaarae943152019-06-16 22:54:14 +0200945 di = dict_find(dict, (char_u *)"filter", -1);
946 if (di != NULL)
947 {
948 callback_T callback = get_callback(&di->di_tv);
949
950 if (callback.cb_name != NULL)
951 {
952 free_callback(&wp->w_filter_cb);
953 set_callback(&wp->w_filter_cb, &callback);
954 }
955 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100956 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200957 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200958 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200959 if (nr)
960 wp->w_popup_flags |= POPF_MAPPING;
961 else
962 wp->w_popup_flags &= ~POPF_MAPPING;
963 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200964
Bram Moolenaard61efa52022-07-23 09:52:04 +0100965 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200966 if (str != NULL)
967 {
968 if (STRCMP(str, "a") == 0)
969 wp->w_filter_mode = MODE_ALL;
970 else
971 wp->w_filter_mode = mode_str2flags(str);
972 }
973
Bram Moolenaarae943152019-06-16 22:54:14 +0200974 di = dict_find(dict, (char_u *)"callback", -1);
975 if (di != NULL)
976 {
977 callback_T callback = get_callback(&di->di_tv);
978
979 if (callback.cb_name != NULL)
980 {
981 free_callback(&wp->w_close_cb);
982 set_callback(&wp->w_close_cb, &callback);
983 }
984 }
985}
986
987/*
988 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200989 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200990 */
991 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200992apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200993{
994 int nr;
995
996 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200997
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200998 if (create)
999 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001000 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1001
Bram Moolenaarae943152019-06-16 22:54:14 +02001002 apply_general_options(wp, dict);
1003
Bram Moolenaard61efa52022-07-23 09:52:04 +01001004 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001005 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001006 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001007
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001008 // when "firstline" and "cursorline" are both set and the cursor would be
1009 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001010 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1011 {
1012 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1013 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001014 else if (wp->w_cursor.lnum < wp->w_firstline
1015 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001016 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001017 wp->w_topline = wp->w_firstline;
1018 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001019 }
1020
Bram Moolenaar33796b32019-06-08 16:01:13 +02001021 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001022 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001023}
1024
1025/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001026 * Add lines to the popup from a list of strings.
1027 */
1028 static void
1029add_popup_strings(buf_T *buf, list_T *l)
1030{
1031 listitem_T *li;
1032 linenr_T lnum = 0;
1033 char_u *p;
1034
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001035 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001036 if (li->li_tv.v_type == VAR_STRING)
1037 {
1038 p = li->li_tv.vval.v_string;
1039 ml_append_buf(buf, lnum++,
1040 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1041 }
1042}
1043
1044/*
1045 * Add lines to the popup from a list of dictionaries.
1046 */
1047 static void
1048add_popup_dicts(buf_T *buf, list_T *l)
1049{
1050 listitem_T *li;
1051 listitem_T *pli;
1052 linenr_T lnum = 0;
1053 char_u *p;
1054 dict_T *dict;
1055
1056 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001057 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001058 {
1059 if (li->li_tv.v_type != VAR_DICT)
1060 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001061 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001062 return;
1063 }
1064 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001065 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001066 ml_append_buf(buf, lnum++,
1067 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1068 }
1069
1070 // add the text properties
1071 lnum = 1;
1072 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1073 {
1074 dictitem_T *di;
1075 list_T *plist;
1076
1077 dict = li->li_tv.vval.v_dict;
1078 di = dict_find(dict, (char_u *)"props", -1);
1079 if (di != NULL)
1080 {
1081 if (di->di_tv.v_type != VAR_LIST)
1082 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001083 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001084 return;
1085 }
1086 plist = di->di_tv.vval.v_list;
1087 if (plist != NULL)
1088 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001089 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001090 {
1091 if (pli->li_tv.v_type != VAR_DICT)
1092 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001093 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001094 return;
1095 }
1096 dict = pli->li_tv.vval.v_dict;
1097 if (dict != NULL)
1098 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001099 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001100
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001101 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001102 }
1103 }
1104 }
1105 }
1106 }
1107}
1108
1109/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001110 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1111 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001112 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001113popup_top_extra(win_T *wp)
1114{
1115 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1116
1117 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1118 return 1;
1119 return extra;
1120}
1121
1122/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001123 * Get the padding plus border at the left.
1124 */
1125 int
1126popup_left_extra(win_T *wp)
1127{
1128 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1129}
1130
1131/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001132 * Return the height of popup window "wp", including border and padding.
1133 */
1134 int
1135popup_height(win_T *wp)
1136{
1137 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001138 + popup_top_extra(wp)
1139 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001140}
1141
1142/*
1143 * Return the width of popup window "wp", including border, padding and
1144 * scrollbar.
1145 */
1146 int
1147popup_width(win_T *wp)
1148{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001149 // w_leftcol is how many columns of the core are left of the screen
1150 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001151 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001152 + popup_extra_width(wp)
1153 + wp->w_popup_rightoff;
1154}
1155
1156/*
1157 * Return the extra width of popup window "wp": border, padding and scrollbar.
1158 */
1159 int
1160popup_extra_width(win_T *wp)
1161{
1162 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1163 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1164 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001165}
1166
1167/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001168 * Adjust the position and size of the popup to fit on the screen.
1169 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001170 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001171popup_adjust_position(win_T *wp)
1172{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001173 linenr_T lnum;
1174 int wrapped = 0;
1175 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001176 int maxwidth_no_scrollbar;
1177 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001178 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001179 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001180 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001181 int center_vert = FALSE;
1182 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001183 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001184 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001185 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1186 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1187 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1188 int extra_height = top_extra + bot_extra;
1189 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001190 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001191 int org_winrow = wp->w_winrow;
1192 int org_wincol = wp->w_wincol;
1193 int org_width = wp->w_width;
1194 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001195 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001196 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001197 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001198 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001199 int wantline = wp->w_wantline; // adjusted for textprop
1200 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001201 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001202 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001203
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001204 wp->w_winrow = 0;
1205 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001206 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001207 wp->w_popup_leftoff = 0;
1208 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001209
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001210 // May need to update the "cursorline" highlighting, which may also change
1211 // "topline"
1212 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1213 popup_highlight_curline(wp);
1214
Bram Moolenaar12034e22019-08-25 22:25:02 +02001215 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1216 {
1217 win_T *prop_win = wp->w_popup_prop_win;
1218 textprop_T prop;
1219 linenr_T prop_lnum;
1220 pos_T pos;
1221 int screen_row;
1222 int screen_scol;
1223 int screen_ccol;
1224 int screen_ecol;
1225
1226 // Popup window is positioned relative to a text property.
1227 if (find_visible_prop(prop_win,
1228 wp->w_popup_prop_type, wp->w_popup_prop_id,
1229 &prop, &prop_lnum) == FAIL)
1230 {
1231 // Text property is no longer visible, hide the popup.
1232 // Unhiding the popup is done in check_popup_unhidden().
1233 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1234 {
1235 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001236 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001237 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001238 }
1239 return;
1240 }
1241
1242 // Compute the desired position from the position of the text
1243 // property. Use "wantline" and "wantcol" as offsets.
1244 pos.lnum = prop_lnum;
1245 pos.col = prop.tp_col;
1246 if (wp->w_popup_pos == POPPOS_TOPLEFT
1247 || wp->w_popup_pos == POPPOS_BOTLEFT)
1248 pos.col += prop.tp_len - 1;
1249 textpos2screenpos(prop_win, &pos, &screen_row,
1250 &screen_scol, &screen_ccol, &screen_ecol);
1251
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001252 if (screen_scol == 0)
1253 {
1254 // position is off screen, make the width zero to hide it.
1255 wp->w_width = 0;
1256 return;
1257 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001258 if (wp->w_popup_pos == POPPOS_TOPLEFT
1259 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1260 // below the text
1261 wantline = screen_row + wantline + 1;
1262 else
1263 // above the text
1264 wantline = screen_row + wantline - 1;
1265 center_vert = FALSE;
1266 if (wp->w_popup_pos == POPPOS_TOPLEFT
1267 || wp->w_popup_pos == POPPOS_BOTLEFT)
1268 // right of the text
1269 wantcol = screen_ecol + wantcol;
1270 else
1271 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001272 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001273 use_wantcol = TRUE;
1274 }
1275 else
1276 {
1277 // If no line was specified default to vertical centering.
1278 if (wantline == 0)
1279 center_vert = TRUE;
1280 else if (wantline < 0)
1281 // If "wantline" is negative it actually means zero.
1282 wantline = 0;
1283 if (wantcol < 0)
1284 // If "wantcol" is negative it actually means zero.
1285 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001286 }
1287
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001288 if (wp->w_popup_pos == POPPOS_CENTER)
1289 {
1290 // center after computing the size
1291 center_vert = TRUE;
1292 center_hor = TRUE;
1293 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001294 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001295 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001296 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1297 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001298 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001299 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001300 if (wp->w_winrow >= Rows)
1301 wp->w_winrow = Rows - 1;
1302 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001303 if (wp->w_popup_pos == POPPOS_BOTTOM)
1304 // assume that each buffer line takes one screen line
Bram Moolenaar37fef162022-08-29 18:16:32 +01001305 wp->w_winrow = MAX(cmdline_row
1306 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001307
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001308 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001309 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001310 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1311 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001312 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001313 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001314 // Need to see at least one character after the decoration.
1315 if (wp->w_wincol > Columns - left_extra - 1)
1316 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001317 }
1318 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001319
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001320 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001321 // When left aligned use the space available, but shift to the left when we
1322 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001323 maxspace = Columns - wp->w_wincol - left_extra;
1324 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001325 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001326 {
1327 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001328 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001329 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001330
1331 if (wp->w_p_nu || wp->w_p_rnu)
1332 margin_width = number_width(wp) + 1;
1333#ifdef FEAT_FOLDING
1334 margin_width += wp->w_p_fdc;
1335#endif
1336#ifdef FEAT_SIGNS
1337 if (signcolumn_on(wp))
1338 margin_width += 2;
1339#endif
1340 if (margin_width >= maxwidth)
1341 margin_width = maxwidth - 1;
1342
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001343 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001344 minheight = wp->w_minheight;
1345#ifdef FEAT_TERMINAL
1346 // A terminal popup initially does not have content, use a default minimal
1347 // width of 20 characters and height of 5 lines.
1348 if (wp->w_buffer->b_term != NULL)
1349 {
1350 if (minwidth == 0)
1351 minwidth = 20;
1352 if (minheight == 0)
1353 minheight = 5;
1354 }
1355#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001356
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001357 if (wp->w_maxheight > 0)
1358 maxheight = wp->w_maxheight;
1359
Bram Moolenaar8d241042019-06-12 23:40:01 +02001360 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001361 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001362 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001363 if (wp->w_topline < 1)
1364 wp->w_topline = 1;
1365 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001366 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1367
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001368 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001369 // Use a minimum width of one, so that something shows when there is no
1370 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001371 // When "firstline" is -1 then start with the last buffer line and go
1372 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001373 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001374 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001375 if (wp->w_firstline < 0)
1376 lnum = wp->w_buffer->b_ml.ml_line_count;
1377 else
1378 lnum = wp->w_topline;
1379 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001380 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001381 int len;
1382 int w_width = wp->w_width;
1383
1384 // Count Tabs for what they are worth and compute the length based on
1385 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001386 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001387 if (wp->w_width < maxwidth)
1388 wp->w_width = maxwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001389 len = win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001390 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001391 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001392
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001393 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001394 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001395 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001396 {
1397 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001398 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001399 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001400 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001401 }
1402 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001403 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001404 && allow_adjust_left
1405 && (wp->w_popup_pos == POPPOS_TOPLEFT
1406 || wp->w_popup_pos == POPPOS_BOTLEFT))
1407 {
1408 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001409 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001410
Bram Moolenaar51c31312019-06-15 22:27:23 +02001411 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001412 {
1413 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001414
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001415 len -= truncate_shift;
1416 shift_by -= truncate_shift;
1417 }
1418
1419 wp->w_wincol -= shift_by;
1420 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001421 wp->w_width = maxwidth;
1422 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001423 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001424 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001425 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001426 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1427 wp->w_width = wp->w_maxwidth;
1428 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001429
1430 if (wp->w_firstline < 0)
1431 --lnum;
1432 else
1433 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001434
1435 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001436 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001437 && (wp->w_firstline >= 0
1438 ? lnum - wp->w_topline
1439 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001440 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001441 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001442 }
1443
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001444 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001445 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001446
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001447 wp->w_has_scrollbar = wp->w_want_scrollbar
1448 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001449#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001450 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1451 // Terminal window with running job never has a scrollbar, adjusts to
1452 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001453 wp->w_has_scrollbar = FALSE;
1454#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001455 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001456 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001457 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001458 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001459 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001460 // make space for the scrollbar if needed, when lines wrap and when
1461 // applying minwidth
1462 if (maxwidth + right_extra >= maxspace
1463 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1464 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001465 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001466
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001467 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1468 {
1469 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1470
1471 if (minwidth < title_len)
1472 minwidth = title_len;
1473 }
1474
1475 if (minwidth > 0 && wp->w_width < minwidth)
1476 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001477 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001478 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001479 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001480 // some columns cut off on the right
1481 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001482
1483 // If the window doesn't fit because 'minwidth' is set then the
1484 // scrollbar is at the far right of the screen, use the size without
1485 // the scrollbar.
1486 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1487 {
1488 int off = wp->w_width - maxwidth;
1489
1490 if (off > right_extra)
1491 extra_width -= right_extra;
1492 else
1493 extra_width -= off;
1494 wp->w_width = maxwidth_no_scrollbar;
1495 }
1496 else
1497 {
1498 wp->w_width = maxwidth;
1499
1500 // when adding a scrollbar below need to adjust the width
1501 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1502 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001503 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001504 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001505 {
1506 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1507 if (wp->w_wincol < 0)
1508 wp->w_wincol = 0;
1509 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001510 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1511 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1512 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001513 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001514
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001515 // Right aligned: move to the right if needed.
1516 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001517 if (leftoff >= 0)
1518 wp->w_wincol = leftoff;
1519 else if (wp->w_popup_fixed)
1520 {
1521 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001522 // columns when displaying the window, minus left border and
1523 // padding.
1524 if (-leftoff > left_extra)
1525 wp->w_leftcol = -leftoff - left_extra;
1526 wp->w_width -= wp->w_leftcol;
1527 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001528 if (wp->w_width < 0)
1529 wp->w_width = 0;
1530 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001531 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001532
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001533 if (wp->w_p_wrap || (!wp->w_popup_fixed
1534 && (wp->w_popup_pos == POPPOS_TOPLEFT
1535 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001536 {
1537 int want_col = 0;
1538
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001539 // try to show the right border and any scrollbar
1540 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001541 if (want_col > 0 && wp->w_wincol > 0
1542 && wp->w_wincol + want_col >= Columns)
1543 {
1544 wp->w_wincol = Columns - want_col;
1545 if (wp->w_wincol < 0)
1546 wp->w_wincol = 0;
1547 }
1548 }
1549
Bram Moolenaar8d241042019-06-12 23:40:01 +02001550 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1551 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001552 if (minheight > 0 && wp->w_height < minheight)
1553 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001554 if (maxheight > 0 && wp->w_height > maxheight)
1555 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001556 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001557 if (wp->w_height > Rows - wp->w_winrow)
1558 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001559
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001560 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001561 {
1562 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1563 if (wp->w_winrow < 0)
1564 wp->w_winrow = 0;
1565 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001566 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001567 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001568 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001569 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001570 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001571 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001572 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1573 {
1574 // Bottom aligned but does not fit, and less space on the other
1575 // side or "posinvert" is off: reduce height.
1576 wp->w_winrow = 0;
1577 wp->w_height = wantline - extra_height;
1578 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001579 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001580 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001581 // Not enough space and more space on the other side: make top
1582 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001583 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001584 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001585 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001586 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001587 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1588 || wp->w_popup_pos == POPPOS_TOPLEFT)
1589 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001590 if (wp != popup_dragwin
1591 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001592 && wantline * 2 > Rows
1593 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001594 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001595 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001596 // make bottom aligned and recompute the height
1597 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001598 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001599 if (wp->w_winrow < 0)
1600 {
1601 wp->w_height += wp->w_winrow;
1602 wp->w_winrow = 0;
1603 }
1604 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001605 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001606 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001607 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001608 adjust_height_for_top_aligned = TRUE;
1609 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001610 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001611
1612 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1613 && wp->w_winrow + wp->w_height + extra_height > Rows)
1614 {
1615 // Bottom of the popup goes below the last line, reduce the height and
1616 // add a scrollbar.
1617 wp->w_height = Rows - wp->w_winrow - extra_height;
1618#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001619 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001620#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001621 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001622 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001623 if (width_with_scrollbar > 0)
1624 wp->w_width = width_with_scrollbar;
1625 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001626 }
1627
1628 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001629 if (wp->w_winrow >= Rows)
1630 wp->w_winrow = Rows - 1;
1631 else if (wp->w_winrow < 0)
1632 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001633
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001634 if (wp->w_height != org_height)
1635 win_comp_scroll(wp);
1636
Bram Moolenaar17146962019-05-30 00:12:11 +02001637 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001638 if (win_valid(wp->w_popup_prop_win))
1639 {
1640 wp->w_popup_prop_changedtick =
1641 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1642 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1643 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001644
1645 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001646 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001647 if (org_winrow != wp->w_winrow
1648 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001649 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001650 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001651 || org_width != wp->w_width
1652 || org_height != wp->w_height)
1653 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001654 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001655 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1656 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001657 popup_mask_refresh = TRUE;
1658 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001659}
1660
Bram Moolenaar17627312019-06-02 19:53:44 +02001661typedef enum
1662{
1663 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001664 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001665 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001666 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001667 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001668 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001669 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001670 TYPE_PREVIEW, // preview window
1671 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001672} create_type_T;
1673
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001674/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001675 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1676 */
1677 static int
1678popup_is_notification(create_type_T type)
1679{
1680 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1681}
1682
1683/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001684 * Make "buf" empty and set the contents to "text".
1685 * Used by popup_create() and popup_settext().
1686 */
1687 static void
1688popup_set_buffer_text(buf_T *buf, typval_T text)
1689{
1690 int lnum;
1691
1692 // Clear the buffer, then replace the lines.
1693 curbuf = buf;
1694 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001695 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001696 curbuf = curwin->w_buffer;
1697
1698 // Add text to the buffer.
1699 if (text.v_type == VAR_STRING)
1700 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001701 char_u *s = text.vval.v_string;
1702
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001703 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001704 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001705 }
1706 else
1707 {
1708 list_T *l = text.vval.v_list;
1709
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001710 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001711 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001712 if (l->lv_first == &range_list_item)
1713 emsg(_(e_using_number_as_string));
1714 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001715 // list of strings
1716 add_popup_strings(buf, l);
1717 else
1718 // list of dictionaries
1719 add_popup_dicts(buf, l);
1720 }
1721 }
1722
1723 // delete the line that was in the empty buffer
1724 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001725 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001726 curbuf = curwin->w_buffer;
1727}
1728
1729/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001730 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1731 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001732 * Return FAIL if the parsing fails.
1733 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001734 static int
1735parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001736{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001737 char_u *p =
1738#ifdef FEAT_QUICKFIX
1739 !is_preview ? p_cpp :
1740#endif
1741 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001742
Bram Moolenaar258cef52019-08-21 17:29:29 +02001743 if (wp != NULL)
1744 wp->w_popup_flags &= ~POPF_INFO_MENU;
1745
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001746 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001747 {
1748 char_u *e, *dig;
1749 char_u *s = p;
1750 int x;
1751
1752 e = vim_strchr(p, ':');
1753 if (e == NULL || e[1] == NUL)
1754 return FAIL;
1755
1756 p = vim_strchr(e, ',');
1757 if (p == NULL)
1758 p = e + STRLEN(e);
1759 dig = e + 1;
1760 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001761
1762 if (STRNCMP(s, "height:", 7) == 0)
1763 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001764 if (dig != p)
1765 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001766 if (wp != NULL)
1767 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001768 if (is_preview)
1769 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001770 wp->w_maxheight = x;
1771 }
1772 }
1773 else if (STRNCMP(s, "width:", 6) == 0)
1774 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001775 if (dig != p)
1776 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001777 if (wp != NULL)
1778 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001779 if (is_preview)
1780 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001781 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001782 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001783 }
1784 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001785 else if (STRNCMP(s, "highlight:", 10) == 0)
1786 {
1787 if (wp != NULL)
1788 {
1789 int c = *p;
1790
1791 *p = NUL;
1792 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1793 s + 10, OPT_FREE|OPT_LOCAL, 0);
1794 *p = c;
1795 }
1796 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001797 else if (STRNCMP(s, "border:", 7) == 0)
1798 {
1799 char_u *arg = s + 7;
1800 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1801 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1802 int i;
1803
1804 if (!on && !off)
1805 return FAIL;
1806 if (wp != NULL)
1807 {
1808 for (i = 0; i < 4; ++i)
1809 wp->w_popup_border[i] = on ? 1 : 0;
1810 if (off)
1811 // only show the X for close when there is a border
1812 wp->w_popup_close = POPCLOSE_NONE;
1813 }
1814 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001815 else if (STRNCMP(s, "align:", 6) == 0)
1816 {
1817 char_u *arg = s + 6;
1818 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1819 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1820
1821 if (!menu && !item)
1822 return FAIL;
1823 if (wp != NULL && menu)
1824 wp->w_popup_flags |= POPF_INFO_MENU;
1825 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001826 else
1827 return FAIL;
1828 }
1829 return OK;
1830}
1831
1832/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001833 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1834 * is not NULL.
1835 * Return FAIL if the parsing fails.
1836 */
1837 int
1838parse_previewpopup(win_T *wp)
1839{
1840 return parse_popup_option(wp, TRUE);
1841}
1842
1843/*
1844 * Parse the 'completepopup' option and apply the values to window "wp" if it
1845 * is not NULL.
1846 * Return FAIL if the parsing fails.
1847 */
1848 int
1849parse_completepopup(win_T *wp)
1850{
1851 return parse_popup_option(wp, FALSE);
1852}
1853
1854/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001855 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001856 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001857 */
1858 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001859popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001860{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001861 poppos_T ppt = POPPOS_NONE;
1862
1863 if (d != NULL)
1864 ppt = get_pos_entry(d, FALSE);
1865
Bram Moolenaar79648732019-07-18 21:43:07 +02001866 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001867 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001868 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001869 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1870 }
1871 else
1872 {
1873 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1874 if (wp->w_wantline == 0) // cursor in first line
1875 {
1876 wp->w_wantline = 2;
1877 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1878 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1879 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001880 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001881
Bram Moolenaar79648732019-07-18 21:43:07 +02001882 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001883 if (wp->w_wantcol > Columns - width)
1884 {
1885 wp->w_wantcol = Columns - width;
1886 if (wp->w_wantcol < 1)
1887 wp->w_wantcol = 1;
1888 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001889
Bram Moolenaar79648732019-07-18 21:43:07 +02001890 popup_adjust_position(wp);
1891}
1892
1893/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001894 * Set w_wantline and w_wantcol for the a given screen position.
1895 * Caller must take care of running into the window border.
1896 */
1897 void
1898popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1899{
1900 wp->w_wantline = row;
1901 wp->w_wantcol = col;
1902 popup_adjust_position(wp);
1903}
1904
1905/*
1906 * Add a border and lef&right padding.
1907 */
1908 static void
1909add_border_left_right_padding(win_T *wp)
1910{
1911 int i;
1912
1913 for (i = 0; i < 4; ++i)
1914 {
1915 wp->w_popup_border[i] = 1;
1916 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1917 }
1918}
1919
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001920#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001921/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001922 * Return TRUE if there is any popup window with a terminal buffer.
1923 */
1924 static int
1925popup_terminal_exists(void)
1926{
1927 win_T *wp;
1928 tabpage_T *tp;
1929
1930 FOR_ALL_POPUPWINS(wp)
1931 if (wp->w_buffer->b_term != NULL)
1932 return TRUE;
1933 FOR_ALL_TABPAGES(tp)
1934 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1935 if (wp->w_buffer->b_term != NULL)
1936 return TRUE;
1937 return FALSE;
1938}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001939#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001940
1941/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001942 * Mark all popup windows in the current tab and global for redrawing.
1943 */
1944 void
1945popup_redraw_all(void)
1946{
1947 win_T *wp;
1948
1949 FOR_ALL_POPUPWINS(wp)
1950 wp->w_redr_type = UPD_NOT_VALID;
1951 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1952 wp->w_redr_type = UPD_NOT_VALID;
1953}
1954
1955/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001956 * Set the color for a notification window.
1957 */
1958 static void
1959popup_update_color(win_T *wp, create_type_T type)
1960{
1961 char *hiname = type == TYPE_MESSAGE_WIN
1962 ? "MessageWindow" : "PopupNotification";
1963 int nr = syn_name2id((char_u *)hiname);
1964
1965 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1966 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1967 OPT_FREE|OPT_LOCAL, 0);
1968}
1969
1970/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001971 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001972 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001973 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001974 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001975 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001976 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001977popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001978{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001979 win_T *wp;
1980 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001981 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001982 int new_buffer;
1983 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001984 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001985 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001986
Bram Moolenaar79648732019-07-18 21:43:07 +02001987 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001988 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001989 if (in_vim9script()
1990 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1991 || check_for_dict_arg(argvars, 1) == FAIL))
1992 return NULL;
1993
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001994 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001995 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001996 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001997 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001998 if (buf == NULL)
1999 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002000 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002001 return NULL;
2002 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002003#ifdef FEAT_TERMINAL
2004 if (buf->b_term != NULL && popup_terminal_exists())
2005 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002006 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002007 return NULL;
2008 }
2009#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002010 }
2011 else if (!(argvars[0].v_type == VAR_STRING
2012 && argvars[0].vval.v_string != NULL)
2013 && !(argvars[0].v_type == VAR_LIST
2014 && argvars[0].vval.v_list != NULL))
2015 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002016 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002017 return NULL;
2018 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002019 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002020 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002021 d = argvars[1].vval.v_dict;
2022 }
2023
2024 if (d != NULL)
2025 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002026 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002027 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002028 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002029 tabnr = -1; // notifications are global by default
2030 else
2031 tabnr = 0;
2032 if (tabnr > 0)
2033 {
2034 tp = find_tabpage(tabnr);
2035 if (tp == NULL)
2036 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002037 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002038 return NULL;
2039 }
2040 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002041 }
2042
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002043 // Create the window and buffer.
2044 wp = win_alloc_popup_win();
2045 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002046 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002047 if (rettv != NULL)
2048 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002049 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002050 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002051
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002052 if (buf != NULL)
2053 {
2054 // use existing buffer
2055 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002056 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002057 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002058 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002059 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002060 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002061 }
2062 else
2063 {
2064 // create a new buffer associated with the popup
2065 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002066 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002067 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002068 {
2069 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002070 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002071 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002072 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002073
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002074 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002075
Bram Moolenaar46451042019-08-24 15:50:46 +02002076 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002077 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002078 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002079 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002080 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002081 buf->b_p_ul = -1; // no undo
2082 buf->b_p_swf = FALSE; // no swap file
2083 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002084 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002085
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002086 // Avoid that 'buftype' is reset when this buffer is entered.
2087 buf->b_p_initialized = TRUE;
2088 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002089 wp->w_p_wrap = TRUE; // 'wrap' is default on
2090 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002091
Bram Moolenaara3fce622019-06-20 02:31:49 +02002092 if (tp != NULL)
2093 {
2094 // popup on specified tab page
2095 wp->w_next = tp->tp_first_popupwin;
2096 tp->tp_first_popupwin = wp;
2097 }
2098 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002099 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002100 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002101 wp->w_next = curtab->tp_first_popupwin;
2102 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002103 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002104 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002105 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002106 win_T *prev = first_popupwin;
2107
2108 // Global popup: add at the end, so that it gets displayed on top of
2109 // older ones with the same zindex. Matters for notifications.
2110 if (first_popupwin == NULL)
2111 first_popupwin = wp;
2112 else
2113 {
2114 while (prev->w_next != NULL)
2115 prev = prev->w_next;
2116 prev->w_next = wp;
2117 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002118 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002119
Bram Moolenaar79648732019-07-18 21:43:07 +02002120 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002121 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002122
Bram Moolenaar79648732019-07-18 21:43:07 +02002123 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002124 {
2125 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002126 }
2127 if (type == TYPE_ATCURSOR)
2128 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002129 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002130 set_moved_values(wp);
2131 set_moved_columns(wp, FIND_STRING);
2132 }
2133
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002134 if (type == TYPE_BEVAL)
2135 {
2136 wp->w_popup_pos = POPPOS_BOTLEFT;
2137
2138 // by default use the mouse position
2139 wp->w_wantline = mouse_row;
2140 if (wp->w_wantline <= 0) // mouse on first line
2141 {
2142 wp->w_wantline = 2;
2143 wp->w_popup_pos = POPPOS_TOPLEFT;
2144 }
2145 wp->w_wantcol = mouse_col + 1;
2146 set_mousemoved_values(wp);
2147 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2148 }
2149
Bram Moolenaar33796b32019-06-08 16:01:13 +02002150 // set default values
2151 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002152 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002153
Bram Moolenaar9198de32022-08-27 21:30:03 +01002154 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002155 {
2156 win_T *twp, *nextwin;
2157 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002158
2159 // Try to not overlap with another global popup. Guess we need 3
2160 // more screen lines than buffer lines.
2161 wp->w_wantline = 1;
2162 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2163 {
2164 nextwin = twp->w_next;
2165 if (twp != wp
2166 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2167 && twp->w_winrow <= wp->w_wantline - 1 + height
2168 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2169 {
2170 // move to below this popup and restart the loop to check for
2171 // overlap with other popups
2172 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2173 nextwin = first_popupwin;
2174 }
2175 }
2176 if (wp->w_wantline + height > Rows)
2177 {
2178 // can't avoid overlap, put on top in the hope that message goes
2179 // away soon.
2180 wp->w_wantline = 1;
2181 }
2182
2183 wp->w_wantcol = 10;
2184 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002185 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002186 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002187 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002188 for (i = 0; i < 4; ++i)
2189 wp->w_popup_border[i] = 1;
2190 wp->w_popup_padding[1] = 1;
2191 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002192
Bram Moolenaar9198de32022-08-27 21:30:03 +01002193 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002194 }
2195
Bram Moolenaara730e552019-06-16 19:05:31 +02002196 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002197 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002198 wp->w_popup_pos = POPPOS_CENTER;
2199 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002200 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002201 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002202 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002203 }
2204
Bram Moolenaara730e552019-06-16 19:05:31 +02002205 if (type == TYPE_MENU)
2206 {
2207 typval_T tv;
2208 callback_T callback;
2209
2210 tv.v_type = VAR_STRING;
2211 tv.vval.v_string = (char_u *)"popup_filter_menu";
2212 callback = get_callback(&tv);
2213 if (callback.cb_name != NULL)
2214 set_callback(&wp->w_filter_cb, &callback);
2215
2216 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002217 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002218 }
2219
Bram Moolenaar79648732019-07-18 21:43:07 +02002220 if (type == TYPE_PREVIEW)
2221 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002222 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002223 wp->w_popup_close = POPCLOSE_BUTTON;
2224 for (i = 0; i < 4; ++i)
2225 wp->w_popup_border[i] = 1;
2226 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002227 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002228 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002229# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002230 if (type == TYPE_INFO)
2231 {
2232 wp->w_popup_pos = POPPOS_TOPLEFT;
2233 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2234 wp->w_popup_close = POPCLOSE_BUTTON;
2235 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002236 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002237 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002238# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002239
Bram Moolenaarae943152019-06-16 22:54:14 +02002240 for (i = 0; i < 4; ++i)
2241 VIM_CLEAR(wp->w_border_highlight[i]);
2242 for (i = 0; i < 8; ++i)
2243 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002244 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002245 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002246 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002247
Bram Moolenaar79648732019-07-18 21:43:07 +02002248 if (d != NULL)
2249 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002250 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002251
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002252#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002253 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2254 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002255#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002256
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002257 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002258
2259 wp->w_vsep_width = 0;
2260
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002261 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002262 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002263
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002264#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002265 // When running a terminal in the popup it becomes the current window.
2266 if (buf->b_term != NULL)
2267 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002268#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002269
Bram Moolenaara730e552019-06-16 19:05:31 +02002270 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002271}
2272
2273/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002274 * popup_clear()
2275 */
2276 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002277f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002278{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002279 int force = FALSE;
2280
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002281 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2282 return;
2283
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002284 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002285 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002286 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002287}
2288
2289/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002290 * popup_create({text}, {options})
2291 */
2292 void
2293f_popup_create(typval_T *argvars, typval_T *rettv)
2294{
Bram Moolenaar17627312019-06-02 19:53:44 +02002295 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002296}
2297
2298/*
2299 * popup_atcursor({text}, {options})
2300 */
2301 void
2302f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2303{
Bram Moolenaar17627312019-06-02 19:53:44 +02002304 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002305}
2306
2307/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002308 * popup_beval({text}, {options})
2309 */
2310 void
2311f_popup_beval(typval_T *argvars, typval_T *rettv)
2312{
2313 popup_create(argvars, rettv, TYPE_BEVAL);
2314}
2315
2316/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002317 * Invoke the close callback for window "wp" with value "result".
2318 * Careful: The callback may make "wp" invalid!
2319 */
2320 static void
2321invoke_popup_callback(win_T *wp, typval_T *result)
2322{
2323 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002324 typval_T argv[3];
2325
2326 argv[0].v_type = VAR_NUMBER;
2327 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2328
2329 if (result != NULL && result->v_type != VAR_UNKNOWN)
2330 copy_tv(result, &argv[1]);
2331 else
2332 {
2333 argv[1].v_type = VAR_NUMBER;
2334 argv[1].vval.v_number = 0;
2335 }
2336
2337 argv[2].v_type = VAR_UNKNOWN;
2338
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002339 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002340 if (result != NULL)
2341 clear_tv(&argv[1]);
2342 clear_tv(&rettv);
2343}
2344
2345/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002346 * Make "prevwin" the current window, unless it's equal to "wp".
2347 * Otherwise make "firstwin" the current window.
2348 */
2349 static void
2350back_to_prevwin(win_T *wp)
2351{
2352 if (win_valid(prevwin) && wp != prevwin)
2353 win_enter(prevwin, FALSE);
2354 else
2355 win_enter(firstwin, FALSE);
2356}
2357
2358/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002359 * Close popup "wp" and invoke any close callback for it.
2360 */
2361 static void
2362popup_close_and_callback(win_T *wp, typval_T *arg)
2363{
2364 int id = wp->w_id;
2365
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002366#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002367 if (wp == curwin && curbuf->b_term != NULL)
2368 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002369 win_T *owp;
2370
2371 // Closing popup window with a terminal: put focus back on the first
2372 // that works:
2373 // - another popup window with a terminal
2374 // - the previous window
2375 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002376 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002377 if (owp != curwin && owp->w_buffer->b_term != NULL)
2378 break;
2379 if (owp != NULL)
2380 win_enter(owp, FALSE);
2381 else
2382 {
2383 for (owp = curtab->tp_first_popupwin; owp != NULL;
2384 owp = owp->w_next)
2385 if (owp != curwin && owp->w_buffer->b_term != NULL)
2386 break;
2387 if (owp != NULL)
2388 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002389 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002390 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002391 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002392 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002393#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002394
Bram Moolenaar49540192019-12-11 19:34:54 +01002395 // Just in case a check higher up is missing.
2396 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002397 {
2398 // To avoid getting stuck when win_execute() does something that causes
2399 // an error, stop calling the filter callback.
2400 free_callback(&wp->w_filter_cb);
2401
Bram Moolenaar49540192019-12-11 19:34:54 +01002402 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002403 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002404
Bram Moolenaarcee52202020-03-11 14:19:58 +01002405 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002406 if (wp->w_close_cb.cb_name != NULL)
2407 // Careful: This may make "wp" invalid.
2408 invoke_popup_callback(wp, arg);
2409
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002410 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002411 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002412}
2413
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002414 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002415popup_close_with_retval(win_T *wp, int retval)
2416{
2417 typval_T res;
2418
2419 res.v_type = VAR_NUMBER;
2420 res.vval.v_number = retval;
2421 popup_close_and_callback(wp, &res);
2422}
2423
Bram Moolenaar3397f742019-06-02 18:40:06 +02002424/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002425 * Close popup "wp" because of a mouse click.
2426 */
2427 void
2428popup_close_for_mouse_click(win_T *wp)
2429{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002430 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002431}
2432
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002433 static void
2434check_mouse_moved(win_T *wp, win_T *mouse_wp)
2435{
2436 // Close the popup when all if these are true:
2437 // - the mouse is not on this popup
2438 // - "mousemoved" was used
2439 // - the mouse is no longer on the same screen row or the mouse column is
2440 // outside of the relevant text
2441 if (wp != mouse_wp
2442 && wp->w_popup_mouse_row != 0
2443 && (wp->w_popup_mouse_row != mouse_row
2444 || mouse_col < wp->w_popup_mouse_mincol
2445 || mouse_col > wp->w_popup_mouse_maxcol))
2446 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002447 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002448 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002449 }
2450}
2451
2452/*
2453 * Called when the mouse moved: may close a popup with "mousemoved".
2454 */
2455 void
2456popup_handle_mouse_moved(void)
2457{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002458 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002459 win_T *mouse_wp;
2460 int row = mouse_row;
2461 int col = mouse_col;
2462
2463 // find the window where the mouse is in
2464 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2465
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002466 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2467 {
2468 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002469 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002470 }
2471 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2472 {
2473 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002474 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002475 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002476}
2477
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002478/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002479 * In a filter: check if the typed key is a mouse event that is used for
2480 * dragging the popup.
2481 */
2482 static void
2483filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2484{
2485 int row = mouse_row;
2486 int col = mouse_col;
2487
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002488 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002489 && is_mouse_key(c)
2490 && (wp == popup_dragwin
2491 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2492 // do not consume the key, allow for dragging the popup
2493 rettv->vval.v_number = 0;
2494}
2495
Bram Moolenaara730e552019-06-16 19:05:31 +02002496/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002497 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002498 */
2499 void
2500f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2501{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002502 int id;
2503 win_T *wp;
2504 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002505 typval_T res;
2506 int c;
2507 linenr_T old_lnum;
2508
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002509 if (in_vim9script()
2510 && (check_for_number_arg(argvars, 0) == FAIL
2511 || check_for_string_arg(argvars, 1) == FAIL))
2512 return;
2513
2514 id = tv_get_number(&argvars[0]);
2515 wp = win_id2wp(id);
2516 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002517 // If the popup has been closed do not consume the key.
2518 if (wp == NULL)
2519 return;
2520
2521 c = *key;
2522 if (c == K_SPECIAL && key[1] != NUL)
2523 c = TO_SPECIAL(key[1], key[2]);
2524
2525 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002526 rettv->v_type = VAR_BOOL;
2527 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002528 res.v_type = VAR_NUMBER;
2529
2530 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002531 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2532 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002533 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002534 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002535 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2536 ++wp->w_cursor.lnum;
2537 if (old_lnum != wp->w_cursor.lnum)
2538 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002539 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002540 return;
2541 }
2542
2543 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2544 {
2545 // Cancelled, invoke callback with -1
2546 res.vval.v_number = -1;
2547 popup_close_and_callback(wp, &res);
2548 return;
2549 }
2550 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2551 {
2552 // Invoke callback with current index.
2553 res.vval.v_number = wp->w_cursor.lnum;
2554 popup_close_and_callback(wp, &res);
2555 return;
2556 }
2557
2558 filter_handle_drag(wp, c, rettv);
2559}
2560
2561/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002562 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002563 */
2564 void
2565f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2566{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002567 int id;
2568 win_T *wp;
2569 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002570 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002571 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002572
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002573 if (in_vim9script()
2574 && (check_for_number_arg(argvars, 0) == FAIL
2575 || check_for_string_arg(argvars, 1) == FAIL))
2576 return;
2577
2578 id = tv_get_number(&argvars[0]);
2579 wp = win_id2wp(id);
2580 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002581 // If the popup has been closed don't consume the key.
2582 if (wp == NULL)
2583 return;
2584
Bram Moolenaara730e552019-06-16 19:05:31 +02002585 c = *key;
2586 if (c == K_SPECIAL && key[1] != NUL)
2587 c = TO_SPECIAL(key[1], key[2]);
2588
Bram Moolenaara42d9452019-06-15 21:46:30 +02002589 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002590 rettv->v_type = VAR_BOOL;
2591 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002592
Bram Moolenaara730e552019-06-16 19:05:31 +02002593 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002594 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002595 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002596 res.vval.v_number = 0;
2597 else
2598 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002599 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002600 return;
2601 }
2602
2603 // Invoke callback
2604 res.v_type = VAR_NUMBER;
2605 popup_close_and_callback(wp, &res);
2606}
2607
2608/*
2609 * popup_dialog({text}, {options})
2610 */
2611 void
2612f_popup_dialog(typval_T *argvars, typval_T *rettv)
2613{
2614 popup_create(argvars, rettv, TYPE_DIALOG);
2615}
2616
2617/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002618 * popup_menu({text}, {options})
2619 */
2620 void
2621f_popup_menu(typval_T *argvars, typval_T *rettv)
2622{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002623 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002624}
2625
2626/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002627 * popup_notification({text}, {options})
2628 */
2629 void
2630f_popup_notification(typval_T *argvars, typval_T *rettv)
2631{
2632 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2633}
2634
2635/*
2636 * Find the popup window with window-ID "id".
2637 * If the popup window does not exist NULL is returned.
2638 * If the window is not a popup window, and error message is given.
2639 */
2640 static win_T *
2641find_popup_win(int id)
2642{
2643 win_T *wp = win_id2wp(id);
2644
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002645 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002646 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002647 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002648 return NULL;
2649 }
2650 return wp;
2651}
2652
2653/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002654 * popup_close({id})
2655 */
2656 void
2657f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2658{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002659 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002660 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002661
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002662 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2663 return;
2664
2665 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002666 if (
2667# ifdef FEAT_TERMINAL
2668 // if the popup contains a terminal it will become hidden
2669 curbuf->b_term == NULL &&
2670# endif
2671 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002672 return;
2673
2674 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002675 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002676 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002677}
2678
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002679 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002680popup_hide(win_T *wp)
2681{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002682#ifdef FEAT_TERMINAL
2683 if (error_if_term_popup_window())
2684 return;
2685#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002686 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2687 {
2688 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002689 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002690 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002691 popup_mask_refresh = TRUE;
2692 }
2693}
2694
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002695/*
2696 * popup_hide({id})
2697 */
2698 void
2699f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2700{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002701 int id;
2702 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002703
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002704 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2705 return;
2706
2707 id = (int)tv_get_number(argvars);
2708 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002709 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002710 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002711 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002712 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2713 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002714}
2715
2716 void
2717popup_show(win_T *wp)
2718{
2719 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002720 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002721 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002722 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002723 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002724 }
2725}
2726
2727/*
2728 * popup_show({id})
2729 */
2730 void
2731f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2732{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002733 int id;
2734 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002735
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002736 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2737 return;
2738
2739 id = (int)tv_get_number(argvars);
2740 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002741 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002742 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002743 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002744 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002745#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002746 if (wp->w_popup_flags & POPF_INFO)
2747 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002748#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002749 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002750}
2751
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002752/*
2753 * popup_settext({id}, {text})
2754 */
2755 void
2756f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2757{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002758 int id;
2759 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002760
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002761 if (in_vim9script()
2762 && (check_for_number_arg(argvars, 0) == FAIL
2763 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2764 return;
2765
2766 id = (int)tv_get_number(&argvars[0]);
2767 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002768 if (wp != NULL)
2769 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002770 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002771 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002772 else
2773 {
2774 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002775 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002776 popup_adjust_position(wp);
2777 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002778 }
2779}
2780
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002781 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002782popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002783{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002784 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002785 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002786 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002787 clear_cmdline = TRUE;
2788 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002789
Bram Moolenaar43568642022-08-28 13:02:45 +01002790#ifdef HAS_MESSAGE_WINDOW
2791 if (wp == message_win)
2792 message_win = NULL;
2793#endif
2794
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002795 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002796 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002797}
2798
Bram Moolenaar82106932020-03-13 14:34:38 +01002799 static void
2800error_for_popup_window(void)
2801{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002802 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002803}
2804
2805 int
2806error_if_popup_window(int also_with_term UNUSED)
2807{
2808 // win_execute() may set "curwin" to a popup window temporarily, but many
2809 // commands are disallowed then. When a terminal runs in the popup most
2810 // things are allowed. When a terminal is finished it can be closed.
2811 if (WIN_IS_POPUP(curwin)
2812# ifdef FEAT_TERMINAL
2813 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002814# endif
2815 )
2816 {
2817 error_for_popup_window();
2818 return TRUE;
2819 }
2820 return FALSE;
2821}
2822
Bram Moolenaarec583842019-05-26 14:11:23 +02002823/*
2824 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002825 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002826 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002827 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002828 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002829popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002830{
2831 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002832 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002833 win_T *prev = NULL;
2834
Bram Moolenaarec583842019-05-26 14:11:23 +02002835 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002836 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002837 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002838 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002839 if (wp == curwin)
2840 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002841 if (!force)
2842 {
2843 error_for_popup_window();
2844 return FAIL;
2845 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002846 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002847 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002848 if (prev == NULL)
2849 first_popupwin = wp->w_next;
2850 else
2851 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002852 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002853 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002854 }
2855
Bram Moolenaarec583842019-05-26 14:11:23 +02002856 // go through tab-local popups
2857 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002858 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002859 return OK;
2860 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002861}
2862
2863/*
2864 * Close a popup window with Window-id "id" in tabpage "tp".
2865 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002866 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002867popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002868{
2869 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002870 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002871 win_T *prev = NULL;
2872
Bram Moolenaarec583842019-05-26 14:11:23 +02002873 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2874 if (wp->w_id == id)
2875 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002876 if (wp == curwin)
2877 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002878 if (!force)
2879 {
2880 error_for_popup_window();
2881 return FAIL;
2882 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002883 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002884 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002885 if (prev == NULL)
2886 *root = wp->w_next;
2887 else
2888 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002889 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002890 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002891 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002892 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002893}
2894
2895 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002896close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002897{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002898 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002899 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002900 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002901 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002902 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002903 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002904 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002905 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002906}
2907
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002908/*
2909 * popup_move({id}, {options})
2910 */
2911 void
2912f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2913{
Bram Moolenaarae943152019-06-16 22:54:14 +02002914 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002915 int id;
2916 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002917
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002918 if (in_vim9script()
2919 && (check_for_number_arg(argvars, 0) == FAIL
2920 || check_for_dict_arg(argvars, 1) == FAIL))
2921 return;
2922
2923 id = (int)tv_get_number(argvars);
2924 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002925 if (wp == NULL)
2926 return; // invalid {id}
2927
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002928 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002929 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002930 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002931
Bram Moolenaarae943152019-06-16 22:54:14 +02002932 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002933
2934 if (wp->w_winrow + wp->w_height >= cmdline_row)
2935 clear_cmdline = TRUE;
2936 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002937}
2938
Bram Moolenaarbc133542019-05-29 20:26:46 +02002939/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002940 * popup_setoptions({id}, {options})
2941 */
2942 void
2943f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2944{
2945 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002946 int id;
2947 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002948 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002949
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002950 if (in_vim9script()
2951 && (check_for_number_arg(argvars, 0) == FAIL
2952 || check_for_dict_arg(argvars, 1) == FAIL))
2953 return;
2954
2955 id = (int)tv_get_number(argvars);
2956 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002957 if (wp == NULL)
2958 return; // invalid {id}
2959
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002960 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02002961 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002962 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002963 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002964
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002965 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002966
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002967 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002968 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002969 popup_adjust_position(wp);
2970}
2971
2972/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002973 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002974 */
2975 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002976f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002977{
2978 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002979 int id;
2980 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002981 int top_extra;
2982 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002983
2984 if (rettv_dict_alloc(rettv) == OK)
2985 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002986 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2987 return;
2988
2989 id = (int)tv_get_number(argvars);
2990 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002991 if (wp == NULL)
2992 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002993 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002994 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2995
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002996 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002997 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002998 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002999
Bram Moolenaarbc133542019-05-29 20:26:46 +02003000 dict_add_number(dict, "line", wp->w_winrow + 1);
3001 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02003002 dict_add_number(dict, "width", wp->w_width + left_extra
3003 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3004 dict_add_number(dict, "height", wp->w_height + top_extra
3005 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003006
3007 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3008 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3009 dict_add_number(dict, "core_width", wp->w_width);
3010 dict_add_number(dict, "core_height", wp->w_height);
3011
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003012 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02003013 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01003014 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003015 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003016 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003017
3018 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003019 }
3020}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003021
3022/*
3023 * popup_list()
3024 */
3025 void
3026f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3027{
3028 win_T *wp;
3029 tabpage_T *tp;
3030
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003031 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003032 return;
3033 FOR_ALL_POPUPWINS(wp)
3034 list_append_number(rettv->vval.v_list, wp->w_id);
3035 FOR_ALL_TABPAGES(tp)
3036 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3037 list_append_number(rettv->vval.v_list, wp->w_id);
3038}
3039
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003040/*
3041 * popup_locate({row}, {col})
3042 */
3043 void
3044f_popup_locate(typval_T *argvars, typval_T *rettv)
3045{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003046 int row;
3047 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003048 win_T *wp;
3049
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003050 if (in_vim9script()
3051 && (check_for_number_arg(argvars, 0) == FAIL
3052 || check_for_number_arg(argvars, 1) == FAIL))
3053 return;
3054
3055 row = tv_get_number(&argvars[0]) - 1;
3056 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003057 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003058 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003059 rettv->vval.v_number = wp->w_id;
3060}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003061
3062/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003063 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3064 */
3065 static void
3066get_padding_border(dict_T *dict, int *array, char *name)
3067{
3068 list_T *list;
3069 int i;
3070
3071 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3072 return;
3073
3074 list = list_alloc();
3075 if (list != NULL)
3076 {
3077 dict_add_list(dict, name, list);
3078 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3079 for (i = 0; i < 4; ++i)
3080 list_append_number(list, array[i]);
3081 }
3082}
3083
3084/*
3085 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3086 */
3087 static void
3088get_borderhighlight(dict_T *dict, win_T *wp)
3089{
3090 list_T *list;
3091 int i;
3092
3093 for (i = 0; i < 4; ++i)
3094 if (wp->w_border_highlight[i] != NULL)
3095 break;
3096 if (i == 4)
3097 return;
3098
3099 list = list_alloc();
3100 if (list != NULL)
3101 {
3102 dict_add_list(dict, "borderhighlight", list);
3103 for (i = 0; i < 4; ++i)
3104 list_append_string(list, wp->w_border_highlight[i], -1);
3105 }
3106}
3107
3108/*
3109 * For popup_getoptions(): add a "borderchars" entry to "dict".
3110 */
3111 static void
3112get_borderchars(dict_T *dict, win_T *wp)
3113{
3114 list_T *list;
3115 int i;
3116 char_u buf[NUMBUFLEN];
3117 int len;
3118
3119 for (i = 0; i < 8; ++i)
3120 if (wp->w_border_char[i] != 0)
3121 break;
3122 if (i == 8)
3123 return;
3124
3125 list = list_alloc();
3126 if (list != NULL)
3127 {
3128 dict_add_list(dict, "borderchars", list);
3129 for (i = 0; i < 8; ++i)
3130 {
3131 len = mb_char2bytes(wp->w_border_char[i], buf);
3132 list_append_string(list, buf, len);
3133 }
3134 }
3135}
3136
3137/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003138 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003139 */
3140 static void
3141get_moved_list(dict_T *dict, win_T *wp)
3142{
3143 list_T *list;
3144
3145 list = list_alloc();
3146 if (list != NULL)
3147 {
3148 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003149 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003150 list_append_number(list, wp->w_popup_mincol);
3151 list_append_number(list, wp->w_popup_maxcol);
3152 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003153 list = list_alloc();
3154 if (list != NULL)
3155 {
3156 dict_add_list(dict, "mousemoved", list);
3157 list_append_number(list, wp->w_popup_mouse_row);
3158 list_append_number(list, wp->w_popup_mouse_mincol);
3159 list_append_number(list, wp->w_popup_mouse_maxcol);
3160 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003161}
3162
3163/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003164 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003165 */
3166 void
3167f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3168{
3169 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003170 int id;
3171 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003172 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003173 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003174
3175 if (rettv_dict_alloc(rettv) == OK)
3176 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003177 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3178 return;
3179
3180 id = (int)tv_get_number(argvars);
3181 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003182 if (wp == NULL)
3183 return;
3184
3185 dict = rettv->vval.v_dict;
3186 dict_add_number(dict, "line", wp->w_wantline);
3187 dict_add_number(dict, "col", wp->w_wantcol);
3188 dict_add_number(dict, "minwidth", wp->w_minwidth);
3189 dict_add_number(dict, "minheight", wp->w_minheight);
3190 dict_add_number(dict, "maxheight", wp->w_maxheight);
3191 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003192 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003193 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003194 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003195 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003196 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003197 {
3198 proptype_T *pt = text_prop_type_by_id(
3199 wp->w_popup_prop_win->w_buffer,
3200 wp->w_popup_prop_type);
3201
3202 if (pt != NULL)
3203 dict_add_string(dict, "textprop", pt->pt_name);
3204 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3205 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3206 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003207 dict_add_string(dict, "title", wp->w_popup_title);
3208 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003209 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003210 dict_add_number(dict, "dragall",
3211 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003212 dict_add_number(dict, "mapping",
3213 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003214 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003215 dict_add_number(dict, "posinvert",
3216 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003217 dict_add_number(dict, "cursorline",
3218 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003219 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003220 if (wp->w_scrollbar_highlight != NULL)
3221 dict_add_string(dict, "scrollbarhighlight",
3222 wp->w_scrollbar_highlight);
3223 if (wp->w_thumb_highlight != NULL)
3224 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003225
Bram Moolenaara3fce622019-06-20 02:31:49 +02003226 // find the tabpage that holds this popup
3227 i = 1;
3228 FOR_ALL_TABPAGES(tp)
3229 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003230 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003231
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003232 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3233 if (twp->w_id == id)
3234 break;
3235 if (twp != NULL)
3236 break;
3237 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003238 }
3239 if (tp == NULL)
3240 i = -1; // must be global
3241 else if (tp == curtab)
3242 i = 0;
3243 dict_add_number(dict, "tabpage", i);
3244
Bram Moolenaarae943152019-06-16 22:54:14 +02003245 get_padding_border(dict, wp->w_popup_padding, "padding");
3246 get_padding_border(dict, wp->w_popup_border, "border");
3247 get_borderhighlight(dict, wp);
3248 get_borderchars(dict, wp);
3249 get_moved_list(dict, wp);
3250
3251 if (wp->w_filter_cb.cb_name != NULL)
3252 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3253 if (wp->w_close_cb.cb_name != NULL)
3254 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003255
K.Takataeeec2542021-06-02 13:28:16 +02003256 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003257 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3258 {
3259 dict_add_string(dict, "pos",
3260 (char_u *)poppos_entries[i].pp_name);
3261 break;
3262 }
3263
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003264 dict_add_string(dict, "close", (char_u *)(
3265 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3266 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3267
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003268# if defined(FEAT_TIMERS)
3269 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3270 ? (long)wp->w_popup_timer->tr_interval : 0L);
3271# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003272 }
3273}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003274
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003275# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003276/*
3277 * Return TRUE if the current window is running a terminal in a popup window.
3278 * Return FALSE when the job has ended.
3279 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003280 int
3281error_if_term_popup_window()
3282{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003283 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3284 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003285 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003286 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003287 return TRUE;
3288 }
3289 return FALSE;
3290}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003291# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003292
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003293/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003294 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003295 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003296 * Each calling function should use a different flag, see the list at
3297 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003298 */
3299 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003300popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003301{
3302 win_T *wp;
3303
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003304 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003305 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003306 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003307 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003308}
3309
3310/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003311 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003312 * Must have called popup_reset_handled() first.
3313 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3314 * popup with the highest zindex.
3315 */
3316 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003317find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003318{
3319 win_T *wp;
3320 win_T *found_wp;
3321 int found_zindex;
3322
3323 found_zindex = lowest ? INT_MAX : 0;
3324 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003325 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003326 if ((wp->w_popup_handled & handled_flag) == 0
3327 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003328 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003329 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003330 {
3331 found_zindex = wp->w_zindex;
3332 found_wp = wp;
3333 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003334 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003335 if ((wp->w_popup_handled & handled_flag) == 0
3336 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003337 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003338 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003339 {
3340 found_zindex = wp->w_zindex;
3341 found_wp = wp;
3342 }
3343
3344 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003345 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003346 return found_wp;
3347}
3348
3349/*
3350 * Invoke the filter callback for window "wp" with typed character "c".
3351 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003352 * Returns the return value of the filter or -1 for CTRL-C in the current
3353 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003354 * Careful: The filter may make "wp" invalid!
3355 */
3356 static int
3357invoke_popup_filter(win_T *wp, int c)
3358{
3359 int res;
3360 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003361 typval_T argv[3];
3362 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003363 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003364 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003365
Bram Moolenaara42d9452019-06-15 21:46:30 +02003366 // Emergency exit: CTRL-C closes the popup.
3367 if (c == Ctrl_C)
3368 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003369 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003370 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003371
3372 // Reset got_int to avoid the callback isn't called.
3373 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003374 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003375 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003376
3377 // If the popup is the current window it probably fails to close. Then
3378 // do not consume the key.
3379 if (was_curwin && wp == curwin)
3380 return -1;
3381 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003382 }
3383
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003384 argv[0].v_type = VAR_NUMBER;
3385 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3386
3387 // Convert the number to a string, so that the function can use:
3388 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003389 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003390 argv[1].v_type = VAR_STRING;
3391 argv[1].vval.v_string = vim_strsave(buf);
3392
3393 argv[2].v_type = VAR_UNKNOWN;
3394
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003395 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003396 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3397 {
3398 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003399 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003400 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003401 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003402 }
3403 else
3404 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003405 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3406 popup_highlight_curline(wp);
3407
Bram Moolenaar371806e2020-10-22 13:44:54 +02003408 // If an error message was given always return FALSE, so that keys are
3409 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003410 // If we get three errors in a row then close the popup. Decrement the
3411 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3412 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003413 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003414 {
3415 wp->w_filter_errors += 10;
3416 if (wp->w_filter_errors >= 30)
3417 popup_close_with_retval(wp, -1);
3418 res = FALSE;
3419 }
3420 else
3421 {
3422 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3423 --wp->w_filter_errors;
3424 res = tv_get_bool(&rettv);
3425 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003426 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003427
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003428 vim_free(argv[1].vval.v_string);
3429 clear_tv(&rettv);
3430 return res;
3431}
3432
3433/*
3434 * Called when "c" was typed: invoke popup filter callbacks.
3435 * Returns TRUE when the character was consumed,
3436 */
3437 int
3438popup_do_filter(int c)
3439{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003440 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003441 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003442 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003443 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003444 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003445 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003446
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003447#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003448 // Popup window with terminal always gets focus.
3449 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3450 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003451#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003452
Bram Moolenaar934470e2019-09-01 23:27:05 +02003453 if (recursive)
3454 return FALSE;
3455 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003456
Bram Moolenaarf6396232019-08-24 19:36:00 +02003457 if (c == K_LEFTMOUSE)
3458 {
3459 int row = mouse_row;
3460 int col = mouse_col;
3461
3462 wp = mouse_find_win(&row, &col, FIND_POPUP);
3463 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003464 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003465 }
3466
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003467 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003468 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003469 while (res == FALSE
3470 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003471 if (wp->w_filter_cb.cb_name != NULL
3472 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003473 res = invoke_popup_filter(wp, c);
3474
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003475 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003476 {
3477 int save_got_int = got_int;
3478
3479 // Reset got_int to avoid a function used in the statusline aborts.
3480 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003481 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003482 got_int |= save_got_int;
3483 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003484 recursive = FALSE;
3485 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003486
3487 // When interrupted return FALSE to avoid looping.
3488 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003489}
3490
Bram Moolenaar3397f742019-06-02 18:40:06 +02003491/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003492 * Return TRUE if there is a popup visible with a filter callback and the
3493 * "mapping" property off.
3494 */
3495 int
3496popup_no_mapping(void)
3497{
3498 int round;
3499 win_T *wp;
3500
3501 for (round = 1; round <= 2; ++round)
3502 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3503 wp != NULL; wp = wp->w_next)
3504 if (wp->w_filter_cb.cb_name != NULL
3505 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3506 return TRUE;
3507 return FALSE;
3508}
3509
3510/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003511 * Called when the cursor moved: check if any popup needs to be closed if the
3512 * cursor moved far enough.
3513 */
3514 void
3515popup_check_cursor_pos()
3516{
3517 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003518
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003519 popup_reset_handled(POPUP_HANDLED_3);
3520 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003521 if (wp->w_popup_curwin != NULL
3522 && (curwin != wp->w_popup_curwin
3523 || curwin->w_cursor.lnum != wp->w_popup_lnum
3524 || curwin->w_cursor.col < wp->w_popup_mincol
3525 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003526 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003527}
3528
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003529/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003530 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003531 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003532 static void
3533popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003534{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003535 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003536 char_u *cells;
3537 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003538
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003539 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3540 {
3541 vim_free(wp->w_popup_mask_cells);
3542 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003543 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003544 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003545 if (wp->w_popup_mask_cells != NULL
3546 && wp->w_popup_mask_height == height
3547 && wp->w_popup_mask_width == width)
3548 return; // cache is still valid
3549
3550 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003551 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003552 if (wp->w_popup_mask_cells == NULL)
3553 return;
3554 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003555
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003556 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003557 {
3558 int cols, cole;
3559 int lines, linee;
3560
3561 li = lio->li_tv.vval.v_list->lv_first;
3562 cols = tv_get_number(&li->li_tv);
3563 if (cols < 0)
3564 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003565 if (cols <= 0)
3566 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003567 li = li->li_next;
3568 cole = tv_get_number(&li->li_tv);
3569 if (cole < 0)
3570 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003571 if (cole > width)
3572 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003573 li = li->li_next;
3574 lines = tv_get_number(&li->li_tv);
3575 if (lines < 0)
3576 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003577 if (lines <= 0)
3578 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003579 li = li->li_next;
3580 linee = tv_get_number(&li->li_tv);
3581 if (linee < 0)
3582 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003583 if (linee > height)
3584 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003585
Bram Moolenaar4012d262020-12-29 11:57:46 +01003586 for (row = lines - 1; row < linee; ++row)
3587 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003588 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003589 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003590}
3591
3592/*
3593 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3594 * "col" and "line" are screen coordinates.
3595 */
3596 static int
3597popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3598{
3599 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3600 int line = screenline - wp->w_winrow;
3601
3602 return col >= 0 && col < width
3603 && line >= 0 && line < height
3604 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003605}
3606
3607/*
3608 * Set flags in popup_transparent[] for window "wp" to "val".
3609 */
3610 static void
3611update_popup_transparent(win_T *wp, int val)
3612{
3613 if (wp->w_popup_mask != NULL)
3614 {
3615 int width = popup_width(wp);
3616 int height = popup_height(wp);
3617 listitem_T *lio, *li;
3618 int cols, cole;
3619 int lines, linee;
3620 int col, line;
3621
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003622 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003623 {
3624 li = lio->li_tv.vval.v_list->lv_first;
3625 cols = tv_get_number(&li->li_tv);
3626 if (cols < 0)
3627 cols = width + cols + 1;
3628 li = li->li_next;
3629 cole = tv_get_number(&li->li_tv);
3630 if (cole < 0)
3631 cole = width + cole + 1;
3632 li = li->li_next;
3633 lines = tv_get_number(&li->li_tv);
3634 if (lines < 0)
3635 lines = height + lines + 1;
3636 li = li->li_next;
3637 linee = tv_get_number(&li->li_tv);
3638 if (linee < 0)
3639 linee = height + linee + 1;
3640
3641 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003642 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003643 if (cols < 0)
3644 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003645 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003646 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003647 if (lines < 0)
3648 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003649 for (line = lines; line < linee
3650 && line + wp->w_winrow < screen_Rows; ++line)
3651 for (col = cols; col < cole
3652 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003653 popup_transparent[(line + wp->w_winrow) * screen_Columns
3654 + col + wp->w_wincol] = val;
3655 }
3656 }
3657}
3658
3659/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003660 * Only called when popup window "wp" is hidden: If the window is positioned
3661 * next to a text property, and it is now visible, then unhide the popup.
3662 * We don't check if visible popups become hidden, that is done in
3663 * popup_adjust_position().
3664 * Return TRUE if the popup became unhidden.
3665 */
3666 static int
3667check_popup_unhidden(win_T *wp)
3668{
3669 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3670 {
3671 textprop_T prop;
3672 linenr_T lnum;
3673
Bram Moolenaar27724252022-05-08 15:00:04 +01003674 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3675 && find_visible_prop(wp->w_popup_prop_win,
3676 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003677 &prop, &lnum) == OK)
3678 {
3679 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003680 wp->w_popup_prop_topline = 0; // force repositioning
3681 return TRUE;
3682 }
3683 }
3684 return FALSE;
3685}
3686
3687/*
3688 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3689 * That is when the buffer in the popup was changed, or the popup is following
3690 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003691 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003692 */
3693 static int
3694popup_need_position_adjust(win_T *wp)
3695{
3696 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3697 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003698 if (win_valid(wp->w_popup_prop_win)
3699 && (wp->w_popup_prop_changedtick
3700 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3701 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3702 return TRUE;
3703
3704 // May need to adjust the width if the cursor moved.
3705 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003706}
3707
3708/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003709 * Update "popup_mask" if needed.
3710 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003711 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003712 * Also marks window lines for redrawing.
3713 */
3714 void
3715may_update_popup_mask(int type)
3716{
3717 win_T *wp;
3718 short *mask;
3719 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003720 int redraw_all_popups = FALSE;
3721 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003722
3723 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003724 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3725 // basic (such as the screen size) must have changed.
3726 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003727 {
3728 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003729 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003730 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003731
3732 // Check if any popup window buffer has changed and if any popup connected
3733 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003734 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003735 if (wp->w_popup_flags & POPF_HIDDEN)
3736 popup_mask_refresh |= check_popup_unhidden(wp);
3737 else if (popup_need_position_adjust(wp))
3738 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003739 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003740 if (wp->w_popup_flags & POPF_HIDDEN)
3741 popup_mask_refresh |= check_popup_unhidden(wp);
3742 else if (popup_need_position_adjust(wp))
3743 popup_mask_refresh = TRUE;
3744
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003745 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003746 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003747
3748 // Need to update the mask, something has changed.
3749 popup_mask_refresh = FALSE;
3750 popup_mask_tab = curtab;
3751 popup_visible = FALSE;
3752
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003753 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003754 // If redrawing only what is needed, update "popup_mask_next" and then
3755 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003756 redrawing_all_win = TRUE;
3757 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003758 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003759 redrawing_all_win = FALSE;
3760 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003761 mask = popup_mask;
3762 else
3763 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003764 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003765
3766 // Find the window with the lowest zindex that hasn't been handled yet,
3767 // so that the window with a higher zindex overwrites the value in
3768 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003769 popup_reset_handled(POPUP_HANDLED_4);
3770 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003771 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003772 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003773 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003774
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003775 popup_visible = TRUE;
3776
Bram Moolenaar12034e22019-08-25 22:25:02 +02003777 // Recompute the position if the text changed. It may make the popup
3778 // hidden if it's attach to a text property that is no longer visible.
3779 if (redraw_all_popups || popup_need_position_adjust(wp))
3780 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003781 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003782 if (wp->w_popup_flags & POPF_HIDDEN)
3783 continue;
3784 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003785
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003786 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003787 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003788 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003789 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003790 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003791 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003792 col < wp->w_wincol + width - wp->w_popup_leftoff
3793 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003794 if (wp->w_zindex < POPUPMENU_ZINDEX
3795 && pum_visible()
3796 && pum_under_menu(line, col, FALSE))
3797 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3798 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003799 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003800 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003801 }
3802
3803 // Only check which lines are to be updated if not already
3804 // updating all lines.
3805 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003806 {
3807 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3808 win_T *prev_wp = NULL;
3809
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003810 for (line = 0; line < screen_Rows; ++line)
3811 {
3812 int col_done = 0;
3813
3814 for (col = 0; col < screen_Columns; ++col)
3815 {
3816 int off = line * screen_Columns + col;
3817
3818 if (popup_mask[off] != popup_mask_next[off])
3819 {
3820 popup_mask[off] = popup_mask_next[off];
3821
3822 if (line >= cmdline_row)
3823 {
3824 // the command line needs to be cleared if text below
3825 // the popup is now visible.
3826 if (!msg_scrolled && popup_mask_next[off] == 0)
3827 clear_cmdline = TRUE;
3828 }
3829 else if (col >= col_done)
3830 {
3831 linenr_T lnum;
3832 int line_cp = line;
3833 int col_cp = col;
3834
3835 // The screen position "line" / "col" needs to be
3836 // redrawn. Figure out what window that is and update
3837 // w_redraw_top and w_redr_bot. Only needs to be done
3838 // once for each window line.
3839 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3840 if (wp != NULL)
3841 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003842#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003843 // A terminal window needs to be redrawn.
3844 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003845 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003846 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003847#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003848 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003849 if (wp != prev_wp)
3850 {
3851 vim_memset(plines_cache, 0,
3852 sizeof(int) * Rows);
3853 prev_wp = wp;
3854 }
3855
3856 if (line_cp >= wp->w_height)
3857 // In (or below) status line
3858 wp->w_redr_status = TRUE;
3859 else
3860 {
3861 // compute the position in the buffer line
3862 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003863 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003864 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003865 redrawWinline(wp, lnum);
3866 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003867 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003868
3869 // This line is going to be redrawn, no need to
3870 // check until the right side of the window.
3871 col_done = wp->w_wincol + wp->w_width - 1;
3872 }
3873 }
3874 }
3875 }
3876 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003877
3878 vim_free(plines_cache);
3879 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003880
3881 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882}
3883
3884/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003885 * If the current window is a popup and something relevant changed, recompute
3886 * the position and size.
3887 */
3888 void
3889may_update_popup_position(void)
3890{
3891 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3892 popup_adjust_position(curwin);
3893}
3894
3895/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003896 * Return a string of "len" spaces in IObuff.
3897 */
3898 static char_u *
3899get_spaces(int len)
3900{
3901 vim_memset(IObuff, ' ', (size_t)len);
3902 IObuff[len] = NUL;
3903 return IObuff;
3904}
3905
3906/*
3907 * Update popup windows. They are drawn on top of normal windows.
3908 * "win_update" is called for each popup window, lowest zindex first.
3909 */
3910 void
3911update_popups(void (*win_update)(win_T *wp))
3912{
3913 win_T *wp;
3914 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003915 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003916 int total_width;
3917 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003918 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003919 int popup_attr;
3920 int border_attr[4];
3921 int border_char[8];
3922 char_u buf[MB_MAXBYTES];
3923 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003924 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003925 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003926 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003927 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003928 int sb_thumb_top = 0;
3929 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003930 int attr_scroll = 0;
3931 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003932
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003933 // hide the cursor until redrawing is done.
3934 cursor_off();
3935
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003936 // Find the window with the lowest zindex that hasn't been updated yet,
3937 // so that the window with a higher zindex is drawn later, thus goes on
3938 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003939 popup_reset_handled(POPUP_HANDLED_5);
3940 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003941 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003942 int title_len = 0;
3943 int title_wincol;
3944
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003945 // This drawing uses the zindex of the popup window, so that it's on
3946 // top of the text but doesn't draw when another popup with higher
3947 // zindex is on top of the character.
3948 screen_zindex = wp->w_zindex;
3949
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003950 // Set flags in popup_transparent[] for masked cells.
3951 update_popup_transparent(wp, 1);
3952
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003953 // adjust w_winrow and w_wincol for border and padding, since
3954 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003955 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003956 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3957 - wp->w_popup_leftoff;
3958 if (wp->w_wincol + left_extra < 0)
3959 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003960 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003961 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003962
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003963 // Draw the popup text, unless it's off screen.
3964 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003965 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003966 // May need to update the "cursorline" highlighting, which may also
3967 // change "topline"
3968 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3969 popup_highlight_curline(wp);
3970
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003971 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003972
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003973 // move the cursor into the visible lines, otherwise executing
3974 // commands with win_execute() may cause the text to jump.
3975 if (wp->w_cursor.lnum < wp->w_topline)
3976 wp->w_cursor.lnum = wp->w_topline;
3977 else if (wp->w_cursor.lnum >= wp->w_botline)
3978 wp->w_cursor.lnum = wp->w_botline - 1;
3979 }
3980
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003981 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003982 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003983
3984 // Add offset for border and padding if not done already.
3985 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3986 {
3987 wp->w_wcol += left_extra;
3988 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3989 }
3990 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003991 {
3992 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003993 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003994 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003995
Bram Moolenaareabddc42022-04-02 15:32:16 +01003996 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003997 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003998 popup_attr = get_wcr_attr(wp);
3999
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004000 if (wp->w_winrow + total_height > cmdline_row)
4001 wp->w_popup_flags |= POPF_ON_CMDLINE;
4002 else
4003 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4004
4005
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004006 // We can only use these line drawing characters when 'encoding' is
4007 // "utf-8" and 'ambiwidth' is "single".
4008 if (enc_utf8 && *p_ambw == 's')
4009 {
4010 border_char[0] = border_char[2] = 0x2550;
4011 border_char[1] = border_char[3] = 0x2551;
4012 border_char[4] = 0x2554;
4013 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004014 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4015 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004016 border_char[7] = 0x255a;
4017 }
4018 else
4019 {
4020 border_char[0] = border_char[2] = '-';
4021 border_char[1] = border_char[3] = '|';
4022 for (i = 4; i < 8; ++i)
4023 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004024 if (wp->w_popup_flags & POPF_RESIZE)
4025 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004026 }
4027 for (i = 0; i < 8; ++i)
4028 if (wp->w_border_char[i] != 0)
4029 border_char[i] = wp->w_border_char[i];
4030
4031 for (i = 0; i < 4; ++i)
4032 {
4033 border_attr[i] = popup_attr;
4034 if (wp->w_border_highlight[i] != NULL)
4035 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4036 }
4037
Bram Moolenaard91467f2020-11-21 12:42:09 +01004038 // Title goes on top of border or padding.
4039 title_wincol = wp->w_wincol + 1;
4040 if (wp->w_popup_title != NULL)
4041 {
rbtnnc6119412021-08-07 13:08:45 +02004042 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004043
Ralf Schandlbc869872021-05-28 14:12:14 +02004044 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004045 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004046 {
4047 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4048 char_u *title_text = alloc(title_byte_len + 1);
4049
4050 if (title_text != NULL)
4051 {
4052 trunc_string(wp->w_popup_title, title_text,
4053 total_width - 2, title_byte_len + 1);
4054 screen_puts(title_text, wp->w_winrow, title_wincol,
4055 wp->w_popup_border[0] > 0
4056 ? border_attr[0] : popup_attr);
4057 vim_free(title_text);
4058 }
4059
Bram Moolenaard91467f2020-11-21 12:42:09 +01004060 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004061 }
4062 else
4063 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4064 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004065 }
4066
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004067 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004068 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004069 if (wp->w_popup_border[0] > 0)
4070 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004071 // top border; do not draw over the title
4072 if (title_len > 0)
4073 {
4074 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4075 wincol < 0 ? 0 : wincol, title_wincol,
4076 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004077 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004078 border_char[0], border_attr[0]);
4079 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4080 title_wincol + title_len, wincol + total_width,
4081 border_char[0], border_char[0], border_attr[0]);
4082 }
4083 else
4084 {
4085 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4086 wincol < 0 ? 0 : wincol, wincol + total_width,
4087 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4088 ? border_char[4] : border_char[0],
4089 border_char[0], border_attr[0]);
4090 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004091 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004092 {
4093 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4094 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004095 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004096 }
4097 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004098 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4099 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004100
Bram Moolenaard529ba52019-07-02 23:13:53 +02004101 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4102 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004103 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004104 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004105 - wp->w_has_scrollbar;
4106 if (padcol < 0)
4107 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004108 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004109 padcol = 0;
4110 }
4111 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004112 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004113 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004114 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004115 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004116 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004117 // top padding and no border; do not draw over the title
4118 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004119 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004120 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004121 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004122 row += 1;
4123 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004124 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004125 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004126 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004127 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004128
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004129 // Compute scrollbar thumb position and size.
4130 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004131 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004132 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4133 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004134 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004135
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004136 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4137 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004138 if (wp->w_topline > 1 && sb_thumb_height == height)
4139 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004140 if (sb_thumb_height == 0)
4141 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004142 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004143 // it just fits, avoid divide by zero
4144 sb_thumb_top = 0;
4145 else
4146 sb_thumb_top = (wp->w_topline - 1
4147 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004148 * (wp->w_height - sb_thumb_height)
4149 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004150 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4151 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004152 last = total_height - top_off - wp->w_popup_border[2];
4153 if (sb_thumb_top >= last)
4154 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004155 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004156
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004157 if (wp->w_scrollbar_highlight != NULL)
4158 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4159 else
4160 attr_scroll = highlight_attr[HLF_PSB];
4161 if (wp->w_thumb_highlight != NULL)
4162 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4163 else
4164 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004165 }
4166
4167 for (i = wp->w_popup_border[0];
4168 i < total_height - wp->w_popup_border[2]; ++i)
4169 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004170 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004171 // left and right padding only needed next to the body
4172 int do_padding =
4173 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4174 && i < total_height - wp->w_popup_border[2]
4175 - wp->w_popup_padding[2];
4176
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004177 row = wp->w_winrow + i;
4178
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004179 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004180 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004181 {
4182 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004183 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004184 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004185 if (do_padding && wp->w_popup_padding[3] > 0)
4186 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004187 int col = wincol + wp->w_popup_border[3];
4188
Bram Moolenaard529ba52019-07-02 23:13:53 +02004189 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004190 pad_left = wp->w_popup_padding[3];
4191 if (col < 0)
4192 {
4193 pad_left += col;
4194 col = 0;
4195 }
4196 if (pad_left > 0)
4197 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4198 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004199 // scrollbar
4200 if (wp->w_has_scrollbar)
4201 {
4202 int line = i - top_off;
4203 int scroll_col = wp->w_wincol + total_width - 1
4204 - wp->w_popup_border[1];
4205
4206 if (line >= 0 && line < wp->w_height)
4207 screen_putchar(' ', row, scroll_col,
4208 line >= sb_thumb_top
4209 && line < sb_thumb_top + sb_thumb_height
4210 ? attr_thumb : attr_scroll);
4211 else
4212 screen_putchar(' ', row, scroll_col, popup_attr);
4213 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004214 // right border
4215 if (wp->w_popup_border[1] > 0)
4216 {
4217 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004218 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004219 }
4220 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004221 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004222 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004223 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004224 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4225 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004226 }
4227
4228 if (wp->w_popup_padding[2] > 0)
4229 {
4230 // bottom padding
4231 row = wp->w_winrow + wp->w_popup_border[0]
4232 + wp->w_popup_padding[0] + wp->w_height;
4233 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004234 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004235 }
4236
4237 if (wp->w_popup_border[2] > 0)
4238 {
4239 // bottom border
4240 row = wp->w_winrow + total_height - 1;
4241 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004242 wincol < 0 ? 0 : wincol,
4243 wincol + total_width,
4244 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004245 ? border_char[7] : border_char[2],
4246 border_char[2], border_attr[2]);
4247 if (wp->w_popup_border[1] > 0)
4248 {
4249 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004250 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004251 }
4252 }
4253
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004254 if (wp->w_popup_close == POPCLOSE_BUTTON)
4255 {
4256 // close button goes on top of anything at the top-right corner
4257 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004258 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004259 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4260 }
4261
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004262 update_popup_transparent(wp, 0);
4263
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004264 // Back to the normal zindex.
4265 screen_zindex = 0;
4266 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004267
4268#if defined(FEAT_SEARCH_EXTRA)
4269 // In case win_update() called start_search_hl().
4270 end_search_hl();
4271#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004272}
4273
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004274/*
4275 * Mark references in callbacks of one popup window.
4276 */
4277 static int
4278set_ref_in_one_popup(win_T *wp, int copyID)
4279{
4280 int abort = FALSE;
4281 typval_T tv;
4282
4283 if (wp->w_close_cb.cb_partial != NULL)
4284 {
4285 tv.v_type = VAR_PARTIAL;
4286 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4287 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4288 }
4289 if (wp->w_filter_cb.cb_partial != NULL)
4290 {
4291 tv.v_type = VAR_PARTIAL;
4292 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4293 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4294 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004295 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004296 return abort;
4297}
4298
4299/*
4300 * Set reference in callbacks of popup windows.
4301 */
4302 int
4303set_ref_in_popups(int copyID)
4304{
4305 int abort = FALSE;
4306 win_T *wp;
4307 tabpage_T *tp;
4308
4309 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4310 abort = abort || set_ref_in_one_popup(wp, copyID);
4311
4312 FOR_ALL_TABPAGES(tp)
4313 {
4314 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4315 abort = abort || set_ref_in_one_popup(wp, copyID);
4316 if (abort)
4317 break;
4318 }
4319 return abort;
4320}
Bram Moolenaar79648732019-07-18 21:43:07 +02004321
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004322 int
4323popup_is_popup(win_T *wp)
4324{
4325 return wp->w_popup_flags != 0;
4326}
4327
4328#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004329/*
4330 * Find an existing popup used as the preview window, in the current tab page.
4331 * Return NULL if not found.
4332 */
4333 win_T *
4334popup_find_preview_window(void)
4335{
4336 win_T *wp;
4337
4338 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004339 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004340 if (wp->w_p_pvw)
4341 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004342 return NULL;
4343}
4344
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004345/*
4346 * Find an existing popup used as the info window, in the current tab page.
4347 * Return NULL if not found.
4348 */
4349 win_T *
4350popup_find_info_window(void)
4351{
4352 win_T *wp;
4353
4354 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004355 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004356 if (wp->w_popup_flags & POPF_INFO)
4357 return wp;
4358 return NULL;
4359}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004360#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004361
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004362 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004363f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4364{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004365#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004366 win_T *wp = popup_find_info_window();
4367
4368 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004369#else
4370 rettv->vval.v_number = 0;
4371#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004372}
4373
4374 void
4375f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004376{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004377#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004378 win_T *wp = popup_find_preview_window();
4379
4380 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004381#else
4382 rettv->vval.v_number = 0;
4383#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004384}
4385
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004386#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004387/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004388 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004389 * NOTE: this makes the popup the current window, so that the file can be
4390 * edited. However, it must not remain to be the current window, the caller
4391 * must make sure of that.
4392 */
4393 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004394popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004395{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004396 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004397
4398 if (wp == NULL)
4399 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004400 if (info)
4401 wp->w_popup_flags |= POPF_INFO;
4402 else
4403 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004404
4405 // Set the width to a reasonable value, so that w_topline can be computed.
4406 if (wp->w_minwidth > 0)
4407 wp->w_width = wp->w_minwidth;
4408 else if (wp->w_maxwidth > 0)
4409 wp->w_width = wp->w_maxwidth;
4410 else
4411 wp->w_width = curwin->w_width;
4412
4413 // Will switch to another buffer soon, dummy one can be wiped.
4414 wp->w_buffer->b_locked = FALSE;
4415
4416 win_enter(wp, FALSE);
4417 return OK;
4418}
4419
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004420/*
4421 * Close any preview popup.
4422 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004423 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004424popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004425{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004426 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004427
4428 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004429 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004430}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004431
4432/*
4433 * Hide the info popup.
4434 */
4435 void
4436popup_hide_info(void)
4437{
4438 win_T *wp = popup_find_info_window();
4439
4440 if (wp != NULL)
4441 popup_hide(wp);
4442}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004443
4444/*
4445 * Close any info popup.
4446 */
4447 void
4448popup_close_info(void)
4449{
4450 win_T *wp = popup_find_info_window();
4451
4452 if (wp != NULL)
4453 popup_close_with_retval(wp, -1);
4454}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004455#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004456
Bram Moolenaar9198de32022-08-27 21:30:03 +01004457#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4458
Bram Moolenaar9198de32022-08-27 21:30:03 +01004459/*
4460 * Get the message window.
4461 * Returns NULL if something failed.
4462 */
4463 win_T *
4464popup_get_message_win(void)
4465{
4466 if (message_win == NULL)
4467 {
4468 int i;
4469
4470 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4471
4472 if (message_win == NULL)
4473 return NULL;
4474
4475 // use the full screen width
4476 message_win->w_width = Columns;
4477
4478 // position at bottom of screen
4479 message_win->w_popup_pos = POPPOS_BOTTOM;
4480 message_win->w_wantcol = 1;
4481 message_win->w_minwidth = 9999;
4482
4483 // no padding, border at the top
4484 for (i = 0; i < 4; ++i)
4485 message_win->w_popup_padding[i] = 0;
4486 for (i = 1; i < 4; ++i)
4487 message_win->w_popup_border[i] = 0;
4488
4489 if (message_win->w_popup_timer != NULL)
4490 message_win->w_popup_timer->tr_keep = TRUE;
4491 }
4492 return message_win;
4493}
4494
4495/*
4496 * If the message window is not visible: show it
4497 * If the message window is visible: reset the timeout
4498 */
4499 void
4500popup_show_message_win(void)
4501{
4502 if (message_win != NULL)
4503 {
4504 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4505 {
4506 // the highlight may have changed.
4507 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4508 popup_show(message_win);
4509 }
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004510 if (message_win->w_popup_timer != NULL)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004511 timer_start(message_win->w_popup_timer);
4512 }
4513}
4514
4515 int
4516popup_message_win_visible(void)
4517{
4518 return message_win != NULL
4519 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4520}
4521
4522/*
4523 * If the message window is visible: hide it.
4524 */
4525 void
4526popup_hide_message_win(void)
4527{
4528 if (message_win != NULL)
4529 popup_hide(message_win);
4530}
4531
4532#endif
4533
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004534/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004535 * Close any popup for a text property associated with "win".
4536 * Return TRUE if a popup was closed.
4537 */
4538 int
4539popup_win_closed(win_T *win)
4540{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004541 int round;
4542 win_T *wp;
4543 win_T *next;
4544 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004545
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004546 for (round = 1; round <= 2; ++round)
4547 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4548 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004549 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004550 next = wp->w_next;
4551 if (wp->w_popup_prop_win == win)
4552 {
4553 popup_close_with_retval(wp, -1);
4554 ret = TRUE;
4555 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004556 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004557 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004558}
4559
4560/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004561 * Set the title of the popup window to the file name.
4562 */
4563 void
4564popup_set_title(win_T *wp)
4565{
4566 if (wp->w_buffer->b_fname != NULL)
4567 {
4568 char_u dirname[MAXPATHL];
4569 size_t len;
4570
4571 mch_dirname(dirname, MAXPATHL);
4572 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4573
4574 vim_free(wp->w_popup_title);
4575 len = STRLEN(wp->w_buffer->b_fname) + 3;
4576 wp->w_popup_title = alloc((int)len);
4577 if (wp->w_popup_title != NULL)
4578 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4579 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004580 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004581 }
4582}
4583
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004584# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004585/*
4586 * If there is a preview window, update the title.
4587 * Used after changing directory.
4588 */
4589 void
4590popup_update_preview_title(void)
4591{
4592 win_T *wp = popup_find_preview_window();
4593
4594 if (wp != NULL)
4595 popup_set_title(wp);
4596}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004597# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004598
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004599#endif // FEAT_PROP_POPUP