blob: d02196ef50be447dd92e1cf68de732b29ded2b75 [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)
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001304 {
1305 // Assume that each buffer line takes one screen line, and one line
1306 // for the top border. First make sure cmdline_row is valid,
1307 // calling update_screen() will set it only later.
1308 compute_cmdrow();
Bram Moolenaar37fef162022-08-29 18:16:32 +01001309 wp->w_winrow = MAX(cmdline_row
1310 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001311 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001312
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001313 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001314 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001315 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1316 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001317 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001318 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001319 // Need to see at least one character after the decoration.
1320 if (wp->w_wincol > Columns - left_extra - 1)
1321 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001322 }
1323 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001324
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001325 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001326 // When left aligned use the space available, but shift to the left when we
1327 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001328 maxspace = Columns - wp->w_wincol - left_extra;
1329 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001330 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001331 {
1332 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001333 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001334 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001335
1336 if (wp->w_p_nu || wp->w_p_rnu)
1337 margin_width = number_width(wp) + 1;
1338#ifdef FEAT_FOLDING
1339 margin_width += wp->w_p_fdc;
1340#endif
1341#ifdef FEAT_SIGNS
1342 if (signcolumn_on(wp))
1343 margin_width += 2;
1344#endif
1345 if (margin_width >= maxwidth)
1346 margin_width = maxwidth - 1;
1347
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001348 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001349 minheight = wp->w_minheight;
1350#ifdef FEAT_TERMINAL
1351 // A terminal popup initially does not have content, use a default minimal
1352 // width of 20 characters and height of 5 lines.
1353 if (wp->w_buffer->b_term != NULL)
1354 {
1355 if (minwidth == 0)
1356 minwidth = 20;
1357 if (minheight == 0)
1358 minheight = 5;
1359 }
1360#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001361
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001362 if (wp->w_maxheight > 0)
1363 maxheight = wp->w_maxheight;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01001364 else if (wp->w_popup_pos == POPPOS_BOTTOM)
1365 maxheight = cmdline_row - 1;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001366
Bram Moolenaar8d241042019-06-12 23:40:01 +02001367 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001368 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001369 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001370 if (wp->w_topline < 1)
1371 wp->w_topline = 1;
1372 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001373 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1374
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001375 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001376 // Use a minimum width of one, so that something shows when there is no
1377 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001378 // When "firstline" is -1 then start with the last buffer line and go
1379 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001380 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001381 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001382 if (wp->w_firstline < 0)
1383 lnum = wp->w_buffer->b_ml.ml_line_count;
1384 else
1385 lnum = wp->w_topline;
1386 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001387 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001388 int len;
1389 int w_width = wp->w_width;
1390
1391 // Count Tabs for what they are worth and compute the length based on
1392 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001393 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001394 if (wp->w_width < maxwidth)
1395 wp->w_width = maxwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001396 len = win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001397 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001398 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001399
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001400 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001401 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001402 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001403 {
1404 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001405 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001406 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001407 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001408 }
1409 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001410 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001411 && allow_adjust_left
1412 && (wp->w_popup_pos == POPPOS_TOPLEFT
1413 || wp->w_popup_pos == POPPOS_BOTLEFT))
1414 {
1415 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001416 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001417
Bram Moolenaar51c31312019-06-15 22:27:23 +02001418 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001419 {
1420 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001421
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001422 len -= truncate_shift;
1423 shift_by -= truncate_shift;
1424 }
1425
1426 wp->w_wincol -= shift_by;
1427 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001428 wp->w_width = maxwidth;
1429 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001430 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001431 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001432 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001433 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1434 wp->w_width = wp->w_maxwidth;
1435 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001436
1437 if (wp->w_firstline < 0)
1438 --lnum;
1439 else
1440 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001441
1442 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001443 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001444 && (wp->w_firstline >= 0
1445 ? lnum - wp->w_topline
1446 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001447 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001448 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001449 }
1450
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001451 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001452 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001453
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001454 wp->w_has_scrollbar = wp->w_want_scrollbar
1455 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001456#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001457 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1458 // Terminal window with running job never has a scrollbar, adjusts to
1459 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001460 wp->w_has_scrollbar = FALSE;
1461#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001462 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001463 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001464 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001465 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001466 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001467 // make space for the scrollbar if needed, when lines wrap and when
1468 // applying minwidth
1469 if (maxwidth + right_extra >= maxspace
1470 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1471 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001472 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001473
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001474 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1475 {
1476 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1477
1478 if (minwidth < title_len)
1479 minwidth = title_len;
1480 }
1481
1482 if (minwidth > 0 && wp->w_width < minwidth)
1483 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001484 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001485 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001486 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001487 // some columns cut off on the right
1488 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001489
1490 // If the window doesn't fit because 'minwidth' is set then the
1491 // scrollbar is at the far right of the screen, use the size without
1492 // the scrollbar.
1493 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1494 {
1495 int off = wp->w_width - maxwidth;
1496
1497 if (off > right_extra)
1498 extra_width -= right_extra;
1499 else
1500 extra_width -= off;
1501 wp->w_width = maxwidth_no_scrollbar;
1502 }
1503 else
1504 {
1505 wp->w_width = maxwidth;
1506
1507 // when adding a scrollbar below need to adjust the width
1508 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1509 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001510 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001511 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001512 {
1513 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1514 if (wp->w_wincol < 0)
1515 wp->w_wincol = 0;
1516 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001517 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1518 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1519 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001520 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001521
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001522 // Right aligned: move to the right if needed.
1523 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001524 if (leftoff >= 0)
1525 wp->w_wincol = leftoff;
1526 else if (wp->w_popup_fixed)
1527 {
1528 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001529 // columns when displaying the window, minus left border and
1530 // padding.
1531 if (-leftoff > left_extra)
1532 wp->w_leftcol = -leftoff - left_extra;
1533 wp->w_width -= wp->w_leftcol;
1534 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001535 if (wp->w_width < 0)
1536 wp->w_width = 0;
1537 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001538 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001539
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001540 if (wp->w_p_wrap || (!wp->w_popup_fixed
1541 && (wp->w_popup_pos == POPPOS_TOPLEFT
1542 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001543 {
1544 int want_col = 0;
1545
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001546 // try to show the right border and any scrollbar
1547 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001548 if (want_col > 0 && wp->w_wincol > 0
1549 && wp->w_wincol + want_col >= Columns)
1550 {
1551 wp->w_wincol = Columns - want_col;
1552 if (wp->w_wincol < 0)
1553 wp->w_wincol = 0;
1554 }
1555 }
1556
Bram Moolenaar8d241042019-06-12 23:40:01 +02001557 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1558 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001559 if (minheight > 0 && wp->w_height < minheight)
1560 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001561 if (maxheight > 0 && wp->w_height > maxheight)
1562 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001563 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001564 if (wp->w_height > Rows - wp->w_winrow)
1565 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001566
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001567 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001568 {
1569 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1570 if (wp->w_winrow < 0)
1571 wp->w_winrow = 0;
1572 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001573 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001574 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001575 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001576 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001577 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001578 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001579 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1580 {
1581 // Bottom aligned but does not fit, and less space on the other
1582 // side or "posinvert" is off: reduce height.
1583 wp->w_winrow = 0;
1584 wp->w_height = wantline - extra_height;
1585 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001586 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001587 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001588 // Not enough space and more space on the other side: make top
1589 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001590 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001591 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001592 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001593 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001594 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1595 || wp->w_popup_pos == POPPOS_TOPLEFT)
1596 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001597 if (wp != popup_dragwin
1598 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001599 && wantline * 2 > Rows
1600 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001601 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001602 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001603 // make bottom aligned and recompute the height
1604 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001605 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001606 if (wp->w_winrow < 0)
1607 {
1608 wp->w_height += wp->w_winrow;
1609 wp->w_winrow = 0;
1610 }
1611 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001612 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001613 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001614 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001615 adjust_height_for_top_aligned = TRUE;
1616 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001617 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001618
1619 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1620 && wp->w_winrow + wp->w_height + extra_height > Rows)
1621 {
1622 // Bottom of the popup goes below the last line, reduce the height and
1623 // add a scrollbar.
1624 wp->w_height = Rows - wp->w_winrow - extra_height;
1625#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001626 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001627#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001628 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001629 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001630 if (width_with_scrollbar > 0)
1631 wp->w_width = width_with_scrollbar;
1632 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001633 }
1634
1635 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001636 if (wp->w_winrow >= Rows)
1637 wp->w_winrow = Rows - 1;
1638 else if (wp->w_winrow < 0)
1639 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001640
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001641 if (wp->w_height != org_height)
1642 win_comp_scroll(wp);
1643
Bram Moolenaar17146962019-05-30 00:12:11 +02001644 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001645 if (win_valid(wp->w_popup_prop_win))
1646 {
1647 wp->w_popup_prop_changedtick =
1648 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1649 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1650 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001651
1652 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001653 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001654 if (org_winrow != wp->w_winrow
1655 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001656 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001657 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001658 || org_width != wp->w_width
1659 || org_height != wp->w_height)
1660 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001661 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001662 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1663 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001664 popup_mask_refresh = TRUE;
1665 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001666}
1667
Bram Moolenaar17627312019-06-02 19:53:44 +02001668typedef enum
1669{
1670 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001671 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001672 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001673 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001674 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001675 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001676 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001677 TYPE_PREVIEW, // preview window
1678 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001679} create_type_T;
1680
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001681/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001682 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1683 */
1684 static int
1685popup_is_notification(create_type_T type)
1686{
1687 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1688}
1689
1690/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001691 * Make "buf" empty and set the contents to "text".
1692 * Used by popup_create() and popup_settext().
1693 */
1694 static void
1695popup_set_buffer_text(buf_T *buf, typval_T text)
1696{
1697 int lnum;
1698
1699 // Clear the buffer, then replace the lines.
1700 curbuf = buf;
1701 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001702 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001703 curbuf = curwin->w_buffer;
1704
1705 // Add text to the buffer.
1706 if (text.v_type == VAR_STRING)
1707 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001708 char_u *s = text.vval.v_string;
1709
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001710 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001711 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001712 }
1713 else
1714 {
1715 list_T *l = text.vval.v_list;
1716
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001717 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001718 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001719 if (l->lv_first == &range_list_item)
1720 emsg(_(e_using_number_as_string));
1721 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001722 // list of strings
1723 add_popup_strings(buf, l);
1724 else
1725 // list of dictionaries
1726 add_popup_dicts(buf, l);
1727 }
1728 }
1729
1730 // delete the line that was in the empty buffer
1731 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001732 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001733 curbuf = curwin->w_buffer;
1734}
1735
1736/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001737 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1738 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001739 * Return FAIL if the parsing fails.
1740 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001741 static int
1742parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001743{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001744 char_u *p =
1745#ifdef FEAT_QUICKFIX
1746 !is_preview ? p_cpp :
1747#endif
1748 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001749
Bram Moolenaar258cef52019-08-21 17:29:29 +02001750 if (wp != NULL)
1751 wp->w_popup_flags &= ~POPF_INFO_MENU;
1752
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001753 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001754 {
1755 char_u *e, *dig;
1756 char_u *s = p;
1757 int x;
1758
1759 e = vim_strchr(p, ':');
1760 if (e == NULL || e[1] == NUL)
1761 return FAIL;
1762
1763 p = vim_strchr(e, ',');
1764 if (p == NULL)
1765 p = e + STRLEN(e);
1766 dig = e + 1;
1767 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001768
1769 if (STRNCMP(s, "height:", 7) == 0)
1770 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001771 if (dig != p)
1772 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001773 if (wp != NULL)
1774 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001775 if (is_preview)
1776 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001777 wp->w_maxheight = x;
1778 }
1779 }
1780 else if (STRNCMP(s, "width:", 6) == 0)
1781 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001782 if (dig != p)
1783 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001784 if (wp != NULL)
1785 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001786 if (is_preview)
1787 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001788 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001789 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001790 }
1791 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001792 else if (STRNCMP(s, "highlight:", 10) == 0)
1793 {
1794 if (wp != NULL)
1795 {
1796 int c = *p;
1797
1798 *p = NUL;
1799 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1800 s + 10, OPT_FREE|OPT_LOCAL, 0);
1801 *p = c;
1802 }
1803 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001804 else if (STRNCMP(s, "border:", 7) == 0)
1805 {
1806 char_u *arg = s + 7;
1807 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1808 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1809 int i;
1810
1811 if (!on && !off)
1812 return FAIL;
1813 if (wp != NULL)
1814 {
1815 for (i = 0; i < 4; ++i)
1816 wp->w_popup_border[i] = on ? 1 : 0;
1817 if (off)
1818 // only show the X for close when there is a border
1819 wp->w_popup_close = POPCLOSE_NONE;
1820 }
1821 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001822 else if (STRNCMP(s, "align:", 6) == 0)
1823 {
1824 char_u *arg = s + 6;
1825 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1826 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1827
1828 if (!menu && !item)
1829 return FAIL;
1830 if (wp != NULL && menu)
1831 wp->w_popup_flags |= POPF_INFO_MENU;
1832 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001833 else
1834 return FAIL;
1835 }
1836 return OK;
1837}
1838
1839/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001840 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1841 * is not NULL.
1842 * Return FAIL if the parsing fails.
1843 */
1844 int
1845parse_previewpopup(win_T *wp)
1846{
1847 return parse_popup_option(wp, TRUE);
1848}
1849
1850/*
1851 * Parse the 'completepopup' option and apply the values to window "wp" if it
1852 * is not NULL.
1853 * Return FAIL if the parsing fails.
1854 */
1855 int
1856parse_completepopup(win_T *wp)
1857{
1858 return parse_popup_option(wp, FALSE);
1859}
1860
1861/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001862 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001863 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001864 */
1865 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001866popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001867{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001868 poppos_T ppt = POPPOS_NONE;
1869
1870 if (d != NULL)
1871 ppt = get_pos_entry(d, FALSE);
1872
Bram Moolenaar79648732019-07-18 21:43:07 +02001873 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001874 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001875 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001876 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1877 }
1878 else
1879 {
1880 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1881 if (wp->w_wantline == 0) // cursor in first line
1882 {
1883 wp->w_wantline = 2;
1884 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1885 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1886 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001887 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001888
Bram Moolenaar79648732019-07-18 21:43:07 +02001889 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001890 if (wp->w_wantcol > Columns - width)
1891 {
1892 wp->w_wantcol = Columns - width;
1893 if (wp->w_wantcol < 1)
1894 wp->w_wantcol = 1;
1895 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001896
Bram Moolenaar79648732019-07-18 21:43:07 +02001897 popup_adjust_position(wp);
1898}
1899
1900/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001901 * Set w_wantline and w_wantcol for the a given screen position.
1902 * Caller must take care of running into the window border.
1903 */
1904 void
1905popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1906{
1907 wp->w_wantline = row;
1908 wp->w_wantcol = col;
1909 popup_adjust_position(wp);
1910}
1911
1912/*
1913 * Add a border and lef&right padding.
1914 */
1915 static void
1916add_border_left_right_padding(win_T *wp)
1917{
1918 int i;
1919
1920 for (i = 0; i < 4; ++i)
1921 {
1922 wp->w_popup_border[i] = 1;
1923 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1924 }
1925}
1926
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001927#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001928/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001929 * Return TRUE if there is any popup window with a terminal buffer.
1930 */
1931 static int
1932popup_terminal_exists(void)
1933{
1934 win_T *wp;
1935 tabpage_T *tp;
1936
1937 FOR_ALL_POPUPWINS(wp)
1938 if (wp->w_buffer->b_term != NULL)
1939 return TRUE;
1940 FOR_ALL_TABPAGES(tp)
1941 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1942 if (wp->w_buffer->b_term != NULL)
1943 return TRUE;
1944 return FALSE;
1945}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001946#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001947
1948/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001949 * Mark all popup windows in the current tab and global for redrawing.
1950 */
1951 void
1952popup_redraw_all(void)
1953{
1954 win_T *wp;
1955
1956 FOR_ALL_POPUPWINS(wp)
1957 wp->w_redr_type = UPD_NOT_VALID;
1958 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1959 wp->w_redr_type = UPD_NOT_VALID;
1960}
1961
1962/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001963 * Set the color for a notification window.
1964 */
1965 static void
1966popup_update_color(win_T *wp, create_type_T type)
1967{
1968 char *hiname = type == TYPE_MESSAGE_WIN
1969 ? "MessageWindow" : "PopupNotification";
1970 int nr = syn_name2id((char_u *)hiname);
1971
1972 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1973 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1974 OPT_FREE|OPT_LOCAL, 0);
1975}
1976
1977/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001978 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001979 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001980 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001981 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001982 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001983 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001984popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001985{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001986 win_T *wp;
1987 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001988 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001989 int new_buffer;
1990 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001991 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001992 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001993
Bram Moolenaar79648732019-07-18 21:43:07 +02001994 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001995 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001996 if (in_vim9script()
1997 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1998 || check_for_dict_arg(argvars, 1) == FAIL))
1999 return NULL;
2000
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002001 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002002 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002003 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002004 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002005 if (buf == NULL)
2006 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002007 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002008 return NULL;
2009 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002010#ifdef FEAT_TERMINAL
2011 if (buf->b_term != NULL && popup_terminal_exists())
2012 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002013 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002014 return NULL;
2015 }
2016#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002017 }
2018 else if (!(argvars[0].v_type == VAR_STRING
2019 && argvars[0].vval.v_string != NULL)
2020 && !(argvars[0].v_type == VAR_LIST
2021 && argvars[0].vval.v_list != NULL))
2022 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002023 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002024 return NULL;
2025 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002026 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002027 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002028 d = argvars[1].vval.v_dict;
2029 }
2030
2031 if (d != NULL)
2032 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002033 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002034 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002035 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002036 tabnr = -1; // notifications are global by default
2037 else
2038 tabnr = 0;
2039 if (tabnr > 0)
2040 {
2041 tp = find_tabpage(tabnr);
2042 if (tp == NULL)
2043 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002044 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002045 return NULL;
2046 }
2047 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002048 }
2049
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002050 // Create the window and buffer.
2051 wp = win_alloc_popup_win();
2052 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002053 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002054 if (rettv != NULL)
2055 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002056 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002057 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002058
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002059 if (buf != NULL)
2060 {
2061 // use existing buffer
2062 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002063 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002064 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002065 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002066 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002067 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002068 }
2069 else
2070 {
2071 // create a new buffer associated with the popup
2072 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002073 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002074 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002075 {
2076 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002077 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002078 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002079 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002080
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002081 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002082
Bram Moolenaar46451042019-08-24 15:50:46 +02002083 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002084 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002085 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002086 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002087 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002088 buf->b_p_ul = -1; // no undo
2089 buf->b_p_swf = FALSE; // no swap file
2090 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002091 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002092
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002093 // Avoid that 'buftype' is reset when this buffer is entered.
2094 buf->b_p_initialized = TRUE;
2095 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002096 wp->w_p_wrap = TRUE; // 'wrap' is default on
2097 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002098
Bram Moolenaara3fce622019-06-20 02:31:49 +02002099 if (tp != NULL)
2100 {
2101 // popup on specified tab page
2102 wp->w_next = tp->tp_first_popupwin;
2103 tp->tp_first_popupwin = wp;
2104 }
2105 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002106 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002107 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002108 wp->w_next = curtab->tp_first_popupwin;
2109 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002110 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002111 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002112 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002113 win_T *prev = first_popupwin;
2114
2115 // Global popup: add at the end, so that it gets displayed on top of
2116 // older ones with the same zindex. Matters for notifications.
2117 if (first_popupwin == NULL)
2118 first_popupwin = wp;
2119 else
2120 {
2121 while (prev->w_next != NULL)
2122 prev = prev->w_next;
2123 prev->w_next = wp;
2124 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002125 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002126
Bram Moolenaar79648732019-07-18 21:43:07 +02002127 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002128 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002129
Bram Moolenaar79648732019-07-18 21:43:07 +02002130 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002131 {
2132 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002133 }
2134 if (type == TYPE_ATCURSOR)
2135 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002136 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002137 set_moved_values(wp);
2138 set_moved_columns(wp, FIND_STRING);
2139 }
2140
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002141 if (type == TYPE_BEVAL)
2142 {
2143 wp->w_popup_pos = POPPOS_BOTLEFT;
2144
2145 // by default use the mouse position
2146 wp->w_wantline = mouse_row;
2147 if (wp->w_wantline <= 0) // mouse on first line
2148 {
2149 wp->w_wantline = 2;
2150 wp->w_popup_pos = POPPOS_TOPLEFT;
2151 }
2152 wp->w_wantcol = mouse_col + 1;
2153 set_mousemoved_values(wp);
2154 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2155 }
2156
Bram Moolenaar33796b32019-06-08 16:01:13 +02002157 // set default values
2158 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002159 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002160
Bram Moolenaar9198de32022-08-27 21:30:03 +01002161 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002162 {
2163 win_T *twp, *nextwin;
2164 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002165
2166 // Try to not overlap with another global popup. Guess we need 3
2167 // more screen lines than buffer lines.
2168 wp->w_wantline = 1;
2169 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2170 {
2171 nextwin = twp->w_next;
2172 if (twp != wp
2173 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2174 && twp->w_winrow <= wp->w_wantline - 1 + height
2175 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2176 {
2177 // move to below this popup and restart the loop to check for
2178 // overlap with other popups
2179 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2180 nextwin = first_popupwin;
2181 }
2182 }
2183 if (wp->w_wantline + height > Rows)
2184 {
2185 // can't avoid overlap, put on top in the hope that message goes
2186 // away soon.
2187 wp->w_wantline = 1;
2188 }
2189
2190 wp->w_wantcol = 10;
2191 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002192 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002193 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002194 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002195 for (i = 0; i < 4; ++i)
2196 wp->w_popup_border[i] = 1;
2197 wp->w_popup_padding[1] = 1;
2198 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002199
Bram Moolenaar9198de32022-08-27 21:30:03 +01002200 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002201 }
2202
Bram Moolenaara730e552019-06-16 19:05:31 +02002203 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002204 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002205 wp->w_popup_pos = POPPOS_CENTER;
2206 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002207 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002208 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002209 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002210 }
2211
Bram Moolenaara730e552019-06-16 19:05:31 +02002212 if (type == TYPE_MENU)
2213 {
2214 typval_T tv;
2215 callback_T callback;
2216
2217 tv.v_type = VAR_STRING;
2218 tv.vval.v_string = (char_u *)"popup_filter_menu";
2219 callback = get_callback(&tv);
2220 if (callback.cb_name != NULL)
2221 set_callback(&wp->w_filter_cb, &callback);
2222
2223 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002224 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002225 }
2226
Bram Moolenaar79648732019-07-18 21:43:07 +02002227 if (type == TYPE_PREVIEW)
2228 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002229 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002230 wp->w_popup_close = POPCLOSE_BUTTON;
2231 for (i = 0; i < 4; ++i)
2232 wp->w_popup_border[i] = 1;
2233 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002234 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002235 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002236# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002237 if (type == TYPE_INFO)
2238 {
2239 wp->w_popup_pos = POPPOS_TOPLEFT;
2240 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2241 wp->w_popup_close = POPCLOSE_BUTTON;
2242 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002243 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002244 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002245# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002246
Bram Moolenaarae943152019-06-16 22:54:14 +02002247 for (i = 0; i < 4; ++i)
2248 VIM_CLEAR(wp->w_border_highlight[i]);
2249 for (i = 0; i < 8; ++i)
2250 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002251 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002252 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002253 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002254
Bram Moolenaar79648732019-07-18 21:43:07 +02002255 if (d != NULL)
2256 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002257 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002258
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002259#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002260 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2261 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002262#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002263
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002264 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002265
2266 wp->w_vsep_width = 0;
2267
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002268 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002269 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002270
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002271#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002272 // When running a terminal in the popup it becomes the current window.
2273 if (buf->b_term != NULL)
2274 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002275#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002276
Bram Moolenaara730e552019-06-16 19:05:31 +02002277 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002278}
2279
2280/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002281 * popup_clear()
2282 */
2283 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002284f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002285{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002286 int force = FALSE;
2287
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002288 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2289 return;
2290
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002291 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002292 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002293 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002294}
2295
2296/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002297 * popup_create({text}, {options})
2298 */
2299 void
2300f_popup_create(typval_T *argvars, typval_T *rettv)
2301{
Bram Moolenaar17627312019-06-02 19:53:44 +02002302 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002303}
2304
2305/*
2306 * popup_atcursor({text}, {options})
2307 */
2308 void
2309f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2310{
Bram Moolenaar17627312019-06-02 19:53:44 +02002311 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002312}
2313
2314/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002315 * popup_beval({text}, {options})
2316 */
2317 void
2318f_popup_beval(typval_T *argvars, typval_T *rettv)
2319{
2320 popup_create(argvars, rettv, TYPE_BEVAL);
2321}
2322
2323/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002324 * Invoke the close callback for window "wp" with value "result".
2325 * Careful: The callback may make "wp" invalid!
2326 */
2327 static void
2328invoke_popup_callback(win_T *wp, typval_T *result)
2329{
2330 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002331 typval_T argv[3];
2332
2333 argv[0].v_type = VAR_NUMBER;
2334 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2335
2336 if (result != NULL && result->v_type != VAR_UNKNOWN)
2337 copy_tv(result, &argv[1]);
2338 else
2339 {
2340 argv[1].v_type = VAR_NUMBER;
2341 argv[1].vval.v_number = 0;
2342 }
2343
2344 argv[2].v_type = VAR_UNKNOWN;
2345
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002346 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002347 if (result != NULL)
2348 clear_tv(&argv[1]);
2349 clear_tv(&rettv);
2350}
2351
2352/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002353 * Make "prevwin" the current window, unless it's equal to "wp".
2354 * Otherwise make "firstwin" the current window.
2355 */
2356 static void
2357back_to_prevwin(win_T *wp)
2358{
2359 if (win_valid(prevwin) && wp != prevwin)
2360 win_enter(prevwin, FALSE);
2361 else
2362 win_enter(firstwin, FALSE);
2363}
2364
2365/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002366 * Close popup "wp" and invoke any close callback for it.
2367 */
2368 static void
2369popup_close_and_callback(win_T *wp, typval_T *arg)
2370{
2371 int id = wp->w_id;
2372
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002373#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002374 if (wp == curwin && curbuf->b_term != NULL)
2375 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002376 win_T *owp;
2377
2378 // Closing popup window with a terminal: put focus back on the first
2379 // that works:
2380 // - another popup window with a terminal
2381 // - the previous window
2382 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002383 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002384 if (owp != curwin && owp->w_buffer->b_term != NULL)
2385 break;
2386 if (owp != NULL)
2387 win_enter(owp, FALSE);
2388 else
2389 {
2390 for (owp = curtab->tp_first_popupwin; owp != NULL;
2391 owp = owp->w_next)
2392 if (owp != curwin && owp->w_buffer->b_term != NULL)
2393 break;
2394 if (owp != NULL)
2395 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002396 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002397 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002398 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002399 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002400#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002401
Bram Moolenaar49540192019-12-11 19:34:54 +01002402 // Just in case a check higher up is missing.
2403 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002404 {
2405 // To avoid getting stuck when win_execute() does something that causes
2406 // an error, stop calling the filter callback.
2407 free_callback(&wp->w_filter_cb);
2408
Bram Moolenaar49540192019-12-11 19:34:54 +01002409 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002410 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002411
Bram Moolenaarcee52202020-03-11 14:19:58 +01002412 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002413 if (wp->w_close_cb.cb_name != NULL)
2414 // Careful: This may make "wp" invalid.
2415 invoke_popup_callback(wp, arg);
2416
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002417 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002418 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002419}
2420
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002421 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002422popup_close_with_retval(win_T *wp, int retval)
2423{
2424 typval_T res;
2425
2426 res.v_type = VAR_NUMBER;
2427 res.vval.v_number = retval;
2428 popup_close_and_callback(wp, &res);
2429}
2430
Bram Moolenaar3397f742019-06-02 18:40:06 +02002431/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002432 * Close popup "wp" because of a mouse click.
2433 */
2434 void
2435popup_close_for_mouse_click(win_T *wp)
2436{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002437 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002438}
2439
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002440 static void
2441check_mouse_moved(win_T *wp, win_T *mouse_wp)
2442{
2443 // Close the popup when all if these are true:
2444 // - the mouse is not on this popup
2445 // - "mousemoved" was used
2446 // - the mouse is no longer on the same screen row or the mouse column is
2447 // outside of the relevant text
2448 if (wp != mouse_wp
2449 && wp->w_popup_mouse_row != 0
2450 && (wp->w_popup_mouse_row != mouse_row
2451 || mouse_col < wp->w_popup_mouse_mincol
2452 || mouse_col > wp->w_popup_mouse_maxcol))
2453 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002454 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002455 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002456 }
2457}
2458
2459/*
2460 * Called when the mouse moved: may close a popup with "mousemoved".
2461 */
2462 void
2463popup_handle_mouse_moved(void)
2464{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002465 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002466 win_T *mouse_wp;
2467 int row = mouse_row;
2468 int col = mouse_col;
2469
2470 // find the window where the mouse is in
2471 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2472
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002473 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2474 {
2475 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002476 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002477 }
2478 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2479 {
2480 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002481 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002482 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002483}
2484
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002485/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002486 * In a filter: check if the typed key is a mouse event that is used for
2487 * dragging the popup.
2488 */
2489 static void
2490filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2491{
2492 int row = mouse_row;
2493 int col = mouse_col;
2494
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002495 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002496 && is_mouse_key(c)
2497 && (wp == popup_dragwin
2498 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2499 // do not consume the key, allow for dragging the popup
2500 rettv->vval.v_number = 0;
2501}
2502
Bram Moolenaara730e552019-06-16 19:05:31 +02002503/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002504 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002505 */
2506 void
2507f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2508{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002509 int id;
2510 win_T *wp;
2511 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002512 typval_T res;
2513 int c;
2514 linenr_T old_lnum;
2515
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002516 if (in_vim9script()
2517 && (check_for_number_arg(argvars, 0) == FAIL
2518 || check_for_string_arg(argvars, 1) == FAIL))
2519 return;
2520
2521 id = tv_get_number(&argvars[0]);
2522 wp = win_id2wp(id);
2523 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002524 // If the popup has been closed do not consume the key.
2525 if (wp == NULL)
2526 return;
2527
2528 c = *key;
2529 if (c == K_SPECIAL && key[1] != NUL)
2530 c = TO_SPECIAL(key[1], key[2]);
2531
2532 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002533 rettv->v_type = VAR_BOOL;
2534 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002535 res.v_type = VAR_NUMBER;
2536
2537 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002538 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2539 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002540 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002541 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002542 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2543 ++wp->w_cursor.lnum;
2544 if (old_lnum != wp->w_cursor.lnum)
2545 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002546 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002547 return;
2548 }
2549
2550 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2551 {
2552 // Cancelled, invoke callback with -1
2553 res.vval.v_number = -1;
2554 popup_close_and_callback(wp, &res);
2555 return;
2556 }
2557 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2558 {
2559 // Invoke callback with current index.
2560 res.vval.v_number = wp->w_cursor.lnum;
2561 popup_close_and_callback(wp, &res);
2562 return;
2563 }
2564
2565 filter_handle_drag(wp, c, rettv);
2566}
2567
2568/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002569 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002570 */
2571 void
2572f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2573{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002574 int id;
2575 win_T *wp;
2576 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002577 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002578 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002579
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002580 if (in_vim9script()
2581 && (check_for_number_arg(argvars, 0) == FAIL
2582 || check_for_string_arg(argvars, 1) == FAIL))
2583 return;
2584
2585 id = tv_get_number(&argvars[0]);
2586 wp = win_id2wp(id);
2587 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002588 // If the popup has been closed don't consume the key.
2589 if (wp == NULL)
2590 return;
2591
Bram Moolenaara730e552019-06-16 19:05:31 +02002592 c = *key;
2593 if (c == K_SPECIAL && key[1] != NUL)
2594 c = TO_SPECIAL(key[1], key[2]);
2595
Bram Moolenaara42d9452019-06-15 21:46:30 +02002596 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002597 rettv->v_type = VAR_BOOL;
2598 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002599
Bram Moolenaara730e552019-06-16 19:05:31 +02002600 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002601 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002602 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002603 res.vval.v_number = 0;
2604 else
2605 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002606 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002607 return;
2608 }
2609
2610 // Invoke callback
2611 res.v_type = VAR_NUMBER;
2612 popup_close_and_callback(wp, &res);
2613}
2614
2615/*
2616 * popup_dialog({text}, {options})
2617 */
2618 void
2619f_popup_dialog(typval_T *argvars, typval_T *rettv)
2620{
2621 popup_create(argvars, rettv, TYPE_DIALOG);
2622}
2623
2624/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002625 * popup_menu({text}, {options})
2626 */
2627 void
2628f_popup_menu(typval_T *argvars, typval_T *rettv)
2629{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002630 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002631}
2632
2633/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002634 * popup_notification({text}, {options})
2635 */
2636 void
2637f_popup_notification(typval_T *argvars, typval_T *rettv)
2638{
2639 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2640}
2641
2642/*
2643 * Find the popup window with window-ID "id".
2644 * If the popup window does not exist NULL is returned.
2645 * If the window is not a popup window, and error message is given.
2646 */
2647 static win_T *
2648find_popup_win(int id)
2649{
2650 win_T *wp = win_id2wp(id);
2651
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002652 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002653 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002654 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002655 return NULL;
2656 }
2657 return wp;
2658}
2659
2660/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002661 * popup_close({id})
2662 */
2663 void
2664f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2665{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002666 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002667 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002668
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002669 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2670 return;
2671
2672 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002673 if (
2674# ifdef FEAT_TERMINAL
2675 // if the popup contains a terminal it will become hidden
2676 curbuf->b_term == NULL &&
2677# endif
2678 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002679 return;
2680
2681 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002682 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002683 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002684}
2685
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002686 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002687popup_hide(win_T *wp)
2688{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002689#ifdef FEAT_TERMINAL
2690 if (error_if_term_popup_window())
2691 return;
2692#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002693 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2694 {
2695 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002696 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002697 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002698 popup_mask_refresh = TRUE;
2699 }
2700}
2701
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002702/*
2703 * popup_hide({id})
2704 */
2705 void
2706f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2707{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002708 int id;
2709 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002710
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002711 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2712 return;
2713
2714 id = (int)tv_get_number(argvars);
2715 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002716 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002717 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002718 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002719 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2720 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002721}
2722
2723 void
2724popup_show(win_T *wp)
2725{
2726 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002727 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002728 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002729 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002730 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002731 }
2732}
2733
2734/*
2735 * popup_show({id})
2736 */
2737 void
2738f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2739{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002740 int id;
2741 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002742
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002743 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2744 return;
2745
2746 id = (int)tv_get_number(argvars);
2747 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002748 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002749 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002750 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002751 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002752#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002753 if (wp->w_popup_flags & POPF_INFO)
2754 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002755#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002756 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002757}
2758
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002759/*
2760 * popup_settext({id}, {text})
2761 */
2762 void
2763f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2764{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002765 int id;
2766 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002767
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002768 if (in_vim9script()
2769 && (check_for_number_arg(argvars, 0) == FAIL
2770 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2771 return;
2772
2773 id = (int)tv_get_number(&argvars[0]);
2774 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002775 if (wp != NULL)
2776 {
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01002777 if (check_for_string_or_list_arg(argvars, 1) != FAIL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002778 {
2779 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002780 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002781 popup_adjust_position(wp);
2782 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002783 }
2784}
2785
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002786 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002787popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002788{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002789 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002790 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002791 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002792 clear_cmdline = TRUE;
2793 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002794
Bram Moolenaar43568642022-08-28 13:02:45 +01002795#ifdef HAS_MESSAGE_WINDOW
2796 if (wp == message_win)
2797 message_win = NULL;
2798#endif
2799
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002800 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002801 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002802}
2803
Bram Moolenaar82106932020-03-13 14:34:38 +01002804 static void
2805error_for_popup_window(void)
2806{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002807 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002808}
2809
2810 int
2811error_if_popup_window(int also_with_term UNUSED)
2812{
2813 // win_execute() may set "curwin" to a popup window temporarily, but many
2814 // commands are disallowed then. When a terminal runs in the popup most
2815 // things are allowed. When a terminal is finished it can be closed.
2816 if (WIN_IS_POPUP(curwin)
2817# ifdef FEAT_TERMINAL
2818 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002819# endif
2820 )
2821 {
2822 error_for_popup_window();
2823 return TRUE;
2824 }
2825 return FALSE;
2826}
2827
Bram Moolenaarec583842019-05-26 14:11:23 +02002828/*
2829 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002830 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002831 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002832 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002833 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002834popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002835{
2836 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002837 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002838 win_T *prev = NULL;
2839
Bram Moolenaarec583842019-05-26 14:11:23 +02002840 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002841 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002842 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002843 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002844 if (wp == curwin)
2845 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002846 if (!force)
2847 {
2848 error_for_popup_window();
2849 return FAIL;
2850 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002851 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002852 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002853 if (prev == NULL)
2854 first_popupwin = wp->w_next;
2855 else
2856 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002857 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002858 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002859 }
2860
Bram Moolenaarec583842019-05-26 14:11:23 +02002861 // go through tab-local popups
2862 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002863 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002864 return OK;
2865 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002866}
2867
2868/*
2869 * Close a popup window with Window-id "id" in tabpage "tp".
2870 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002871 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002872popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002873{
2874 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002875 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002876 win_T *prev = NULL;
2877
Bram Moolenaarec583842019-05-26 14:11:23 +02002878 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2879 if (wp->w_id == id)
2880 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002881 if (wp == curwin)
2882 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002883 if (!force)
2884 {
2885 error_for_popup_window();
2886 return FAIL;
2887 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002888 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002889 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002890 if (prev == NULL)
2891 *root = wp->w_next;
2892 else
2893 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002894 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002895 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002896 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002897 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002898}
2899
2900 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002901close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002902{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002903 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002904 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002905 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002906 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002907 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002908 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002909 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002910 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002911}
2912
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002913/*
2914 * popup_move({id}, {options})
2915 */
2916 void
2917f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2918{
Bram Moolenaarae943152019-06-16 22:54:14 +02002919 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002920 int id;
2921 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002922
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002923 if (in_vim9script()
2924 && (check_for_number_arg(argvars, 0) == FAIL
2925 || check_for_dict_arg(argvars, 1) == FAIL))
2926 return;
2927
2928 id = (int)tv_get_number(argvars);
2929 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002930 if (wp == NULL)
2931 return; // invalid {id}
2932
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002933 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002934 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002935 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002936
Bram Moolenaarae943152019-06-16 22:54:14 +02002937 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002938
2939 if (wp->w_winrow + wp->w_height >= cmdline_row)
2940 clear_cmdline = TRUE;
2941 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002942}
2943
Bram Moolenaarbc133542019-05-29 20:26:46 +02002944/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002945 * popup_setoptions({id}, {options})
2946 */
2947 void
2948f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2949{
2950 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002951 int id;
2952 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002953 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002954
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002955 if (in_vim9script()
2956 && (check_for_number_arg(argvars, 0) == FAIL
2957 || check_for_dict_arg(argvars, 1) == FAIL))
2958 return;
2959
2960 id = (int)tv_get_number(argvars);
2961 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002962 if (wp == NULL)
2963 return; // invalid {id}
2964
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002965 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02002966 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002967 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002968 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002969
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002970 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002971
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002972 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002973 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002974 popup_adjust_position(wp);
2975}
2976
2977/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002978 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002979 */
2980 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002981f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002982{
2983 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002984 int id;
2985 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002986 int top_extra;
2987 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002988
2989 if (rettv_dict_alloc(rettv) == OK)
2990 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002991 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2992 return;
2993
2994 id = (int)tv_get_number(argvars);
2995 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002996 if (wp == NULL)
2997 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002998 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002999 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
3000
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003001 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02003002 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003003 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003004
Bram Moolenaarbc133542019-05-29 20:26:46 +02003005 dict_add_number(dict, "line", wp->w_winrow + 1);
3006 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02003007 dict_add_number(dict, "width", wp->w_width + left_extra
3008 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3009 dict_add_number(dict, "height", wp->w_height + top_extra
3010 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003011
3012 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3013 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3014 dict_add_number(dict, "core_width", wp->w_width);
3015 dict_add_number(dict, "core_height", wp->w_height);
3016
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003017 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02003018 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01003019 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003020 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003021 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003022
3023 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003024 }
3025}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003026
3027/*
3028 * popup_list()
3029 */
3030 void
3031f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3032{
3033 win_T *wp;
3034 tabpage_T *tp;
3035
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003036 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003037 return;
3038 FOR_ALL_POPUPWINS(wp)
3039 list_append_number(rettv->vval.v_list, wp->w_id);
3040 FOR_ALL_TABPAGES(tp)
3041 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3042 list_append_number(rettv->vval.v_list, wp->w_id);
3043}
3044
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003045/*
3046 * popup_locate({row}, {col})
3047 */
3048 void
3049f_popup_locate(typval_T *argvars, typval_T *rettv)
3050{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003051 int row;
3052 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003053 win_T *wp;
3054
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003055 if (in_vim9script()
3056 && (check_for_number_arg(argvars, 0) == FAIL
3057 || check_for_number_arg(argvars, 1) == FAIL))
3058 return;
3059
3060 row = tv_get_number(&argvars[0]) - 1;
3061 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003062 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003063 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003064 rettv->vval.v_number = wp->w_id;
3065}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003066
3067/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003068 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3069 */
3070 static void
3071get_padding_border(dict_T *dict, int *array, char *name)
3072{
3073 list_T *list;
3074 int i;
3075
3076 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3077 return;
3078
3079 list = list_alloc();
3080 if (list != NULL)
3081 {
3082 dict_add_list(dict, name, list);
3083 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3084 for (i = 0; i < 4; ++i)
3085 list_append_number(list, array[i]);
3086 }
3087}
3088
3089/*
3090 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3091 */
3092 static void
3093get_borderhighlight(dict_T *dict, win_T *wp)
3094{
3095 list_T *list;
3096 int i;
3097
3098 for (i = 0; i < 4; ++i)
3099 if (wp->w_border_highlight[i] != NULL)
3100 break;
3101 if (i == 4)
3102 return;
3103
3104 list = list_alloc();
3105 if (list != NULL)
3106 {
3107 dict_add_list(dict, "borderhighlight", list);
3108 for (i = 0; i < 4; ++i)
3109 list_append_string(list, wp->w_border_highlight[i], -1);
3110 }
3111}
3112
3113/*
3114 * For popup_getoptions(): add a "borderchars" entry to "dict".
3115 */
3116 static void
3117get_borderchars(dict_T *dict, win_T *wp)
3118{
3119 list_T *list;
3120 int i;
3121 char_u buf[NUMBUFLEN];
3122 int len;
3123
3124 for (i = 0; i < 8; ++i)
3125 if (wp->w_border_char[i] != 0)
3126 break;
3127 if (i == 8)
3128 return;
3129
3130 list = list_alloc();
3131 if (list != NULL)
3132 {
3133 dict_add_list(dict, "borderchars", list);
3134 for (i = 0; i < 8; ++i)
3135 {
3136 len = mb_char2bytes(wp->w_border_char[i], buf);
3137 list_append_string(list, buf, len);
3138 }
3139 }
3140}
3141
3142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003143 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003144 */
3145 static void
3146get_moved_list(dict_T *dict, win_T *wp)
3147{
3148 list_T *list;
3149
3150 list = list_alloc();
3151 if (list != NULL)
3152 {
3153 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003154 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003155 list_append_number(list, wp->w_popup_mincol);
3156 list_append_number(list, wp->w_popup_maxcol);
3157 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003158 list = list_alloc();
3159 if (list != NULL)
3160 {
3161 dict_add_list(dict, "mousemoved", list);
3162 list_append_number(list, wp->w_popup_mouse_row);
3163 list_append_number(list, wp->w_popup_mouse_mincol);
3164 list_append_number(list, wp->w_popup_mouse_maxcol);
3165 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003166}
3167
3168/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003169 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003170 */
3171 void
3172f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3173{
3174 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003175 int id;
3176 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003177 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003178 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003179
3180 if (rettv_dict_alloc(rettv) == OK)
3181 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003182 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3183 return;
3184
3185 id = (int)tv_get_number(argvars);
3186 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003187 if (wp == NULL)
3188 return;
3189
3190 dict = rettv->vval.v_dict;
3191 dict_add_number(dict, "line", wp->w_wantline);
3192 dict_add_number(dict, "col", wp->w_wantcol);
3193 dict_add_number(dict, "minwidth", wp->w_minwidth);
3194 dict_add_number(dict, "minheight", wp->w_minheight);
3195 dict_add_number(dict, "maxheight", wp->w_maxheight);
3196 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003197 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003198 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003199 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003200 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003201 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003202 {
3203 proptype_T *pt = text_prop_type_by_id(
3204 wp->w_popup_prop_win->w_buffer,
3205 wp->w_popup_prop_type);
3206
3207 if (pt != NULL)
3208 dict_add_string(dict, "textprop", pt->pt_name);
3209 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3210 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3211 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003212 dict_add_string(dict, "title", wp->w_popup_title);
3213 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003214 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003215 dict_add_number(dict, "dragall",
3216 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003217 dict_add_number(dict, "mapping",
3218 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003219 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003220 dict_add_number(dict, "posinvert",
3221 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003222 dict_add_number(dict, "cursorline",
3223 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003224 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003225 if (wp->w_scrollbar_highlight != NULL)
3226 dict_add_string(dict, "scrollbarhighlight",
3227 wp->w_scrollbar_highlight);
3228 if (wp->w_thumb_highlight != NULL)
3229 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003230
Bram Moolenaara3fce622019-06-20 02:31:49 +02003231 // find the tabpage that holds this popup
3232 i = 1;
3233 FOR_ALL_TABPAGES(tp)
3234 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003235 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003236
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003237 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3238 if (twp->w_id == id)
3239 break;
3240 if (twp != NULL)
3241 break;
3242 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003243 }
3244 if (tp == NULL)
3245 i = -1; // must be global
3246 else if (tp == curtab)
3247 i = 0;
3248 dict_add_number(dict, "tabpage", i);
3249
Bram Moolenaarae943152019-06-16 22:54:14 +02003250 get_padding_border(dict, wp->w_popup_padding, "padding");
3251 get_padding_border(dict, wp->w_popup_border, "border");
3252 get_borderhighlight(dict, wp);
3253 get_borderchars(dict, wp);
3254 get_moved_list(dict, wp);
3255
3256 if (wp->w_filter_cb.cb_name != NULL)
3257 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3258 if (wp->w_close_cb.cb_name != NULL)
3259 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003260
K.Takataeeec2542021-06-02 13:28:16 +02003261 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003262 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3263 {
3264 dict_add_string(dict, "pos",
3265 (char_u *)poppos_entries[i].pp_name);
3266 break;
3267 }
3268
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003269 dict_add_string(dict, "close", (char_u *)(
3270 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3271 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3272
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003273# if defined(FEAT_TIMERS)
3274 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3275 ? (long)wp->w_popup_timer->tr_interval : 0L);
3276# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003277 }
3278}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003279
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003280# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003281/*
3282 * Return TRUE if the current window is running a terminal in a popup window.
3283 * Return FALSE when the job has ended.
3284 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003285 int
3286error_if_term_popup_window()
3287{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003288 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3289 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003290 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003291 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003292 return TRUE;
3293 }
3294 return FALSE;
3295}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003296# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003297
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003298/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003299 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003300 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003301 * Each calling function should use a different flag, see the list at
3302 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003303 */
3304 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003305popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003306{
3307 win_T *wp;
3308
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003309 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003310 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003311 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003312 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003313}
3314
3315/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003316 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003317 * Must have called popup_reset_handled() first.
3318 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3319 * popup with the highest zindex.
3320 */
3321 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003322find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003323{
3324 win_T *wp;
3325 win_T *found_wp;
3326 int found_zindex;
3327
3328 found_zindex = lowest ? INT_MAX : 0;
3329 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003330 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003331 if ((wp->w_popup_handled & handled_flag) == 0
3332 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003333 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003334 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003335 {
3336 found_zindex = wp->w_zindex;
3337 found_wp = wp;
3338 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003339 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003340 if ((wp->w_popup_handled & handled_flag) == 0
3341 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003342 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003343 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003344 {
3345 found_zindex = wp->w_zindex;
3346 found_wp = wp;
3347 }
3348
3349 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003350 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003351 return found_wp;
3352}
3353
3354/*
3355 * Invoke the filter callback for window "wp" with typed character "c".
3356 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003357 * Returns the return value of the filter or -1 for CTRL-C in the current
3358 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003359 * Careful: The filter may make "wp" invalid!
3360 */
3361 static int
3362invoke_popup_filter(win_T *wp, int c)
3363{
3364 int res;
3365 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003366 typval_T argv[3];
3367 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003368 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003369 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003370
Bram Moolenaara42d9452019-06-15 21:46:30 +02003371 // Emergency exit: CTRL-C closes the popup.
3372 if (c == Ctrl_C)
3373 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003374 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003375 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003376
3377 // Reset got_int to avoid the callback isn't called.
3378 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003379 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003380 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003381
3382 // If the popup is the current window it probably fails to close. Then
3383 // do not consume the key.
3384 if (was_curwin && wp == curwin)
3385 return -1;
3386 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003387 }
3388
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003389 argv[0].v_type = VAR_NUMBER;
3390 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3391
3392 // Convert the number to a string, so that the function can use:
3393 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003394 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003395 argv[1].v_type = VAR_STRING;
3396 argv[1].vval.v_string = vim_strsave(buf);
3397
3398 argv[2].v_type = VAR_UNKNOWN;
3399
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003400 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003401 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3402 {
3403 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003404 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003405 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003406 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003407 }
3408 else
3409 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003410 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3411 popup_highlight_curline(wp);
3412
Bram Moolenaar371806e2020-10-22 13:44:54 +02003413 // If an error message was given always return FALSE, so that keys are
3414 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003415 // If we get three errors in a row then close the popup. Decrement the
3416 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3417 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003418 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003419 {
3420 wp->w_filter_errors += 10;
3421 if (wp->w_filter_errors >= 30)
3422 popup_close_with_retval(wp, -1);
3423 res = FALSE;
3424 }
3425 else
3426 {
3427 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3428 --wp->w_filter_errors;
3429 res = tv_get_bool(&rettv);
3430 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003431 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003432
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003433 vim_free(argv[1].vval.v_string);
3434 clear_tv(&rettv);
3435 return res;
3436}
3437
3438/*
3439 * Called when "c" was typed: invoke popup filter callbacks.
3440 * Returns TRUE when the character was consumed,
3441 */
3442 int
3443popup_do_filter(int c)
3444{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003445 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003446 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003447 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003448 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003449 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003450 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003451
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003452#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003453 // Popup window with terminal always gets focus.
3454 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3455 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003456#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003457
Bram Moolenaar934470e2019-09-01 23:27:05 +02003458 if (recursive)
3459 return FALSE;
3460 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003461
Bram Moolenaarf6396232019-08-24 19:36:00 +02003462 if (c == K_LEFTMOUSE)
3463 {
3464 int row = mouse_row;
3465 int col = mouse_col;
3466
3467 wp = mouse_find_win(&row, &col, FIND_POPUP);
3468 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003469 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003470 }
3471
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003472 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003473 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003474 while (res == FALSE
3475 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003476 if (wp->w_filter_cb.cb_name != NULL
3477 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003478 res = invoke_popup_filter(wp, c);
3479
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003480 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003481 {
3482 int save_got_int = got_int;
3483
3484 // Reset got_int to avoid a function used in the statusline aborts.
3485 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003486 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003487 got_int |= save_got_int;
3488 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003489 recursive = FALSE;
3490 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003491
3492 // When interrupted return FALSE to avoid looping.
3493 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003494}
3495
Bram Moolenaar3397f742019-06-02 18:40:06 +02003496/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003497 * Return TRUE if there is a popup visible with a filter callback and the
3498 * "mapping" property off.
3499 */
3500 int
3501popup_no_mapping(void)
3502{
3503 int round;
3504 win_T *wp;
3505
3506 for (round = 1; round <= 2; ++round)
3507 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3508 wp != NULL; wp = wp->w_next)
3509 if (wp->w_filter_cb.cb_name != NULL
3510 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3511 return TRUE;
3512 return FALSE;
3513}
3514
3515/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003516 * Called when the cursor moved: check if any popup needs to be closed if the
3517 * cursor moved far enough.
3518 */
3519 void
3520popup_check_cursor_pos()
3521{
3522 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003523
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003524 popup_reset_handled(POPUP_HANDLED_3);
3525 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003526 if (wp->w_popup_curwin != NULL
3527 && (curwin != wp->w_popup_curwin
3528 || curwin->w_cursor.lnum != wp->w_popup_lnum
3529 || curwin->w_cursor.col < wp->w_popup_mincol
3530 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003531 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003532}
3533
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003534/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003535 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003536 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003537 static void
3538popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003539{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003540 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003541 char_u *cells;
3542 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003543
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003544 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3545 {
3546 vim_free(wp->w_popup_mask_cells);
3547 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003548 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003549 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003550 if (wp->w_popup_mask_cells != NULL
3551 && wp->w_popup_mask_height == height
3552 && wp->w_popup_mask_width == width)
3553 return; // cache is still valid
3554
3555 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003556 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003557 if (wp->w_popup_mask_cells == NULL)
3558 return;
3559 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003560
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003561 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003562 {
3563 int cols, cole;
3564 int lines, linee;
3565
3566 li = lio->li_tv.vval.v_list->lv_first;
3567 cols = tv_get_number(&li->li_tv);
3568 if (cols < 0)
3569 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003570 if (cols <= 0)
3571 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003572 li = li->li_next;
3573 cole = tv_get_number(&li->li_tv);
3574 if (cole < 0)
3575 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003576 if (cole > width)
3577 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003578 li = li->li_next;
3579 lines = tv_get_number(&li->li_tv);
3580 if (lines < 0)
3581 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003582 if (lines <= 0)
3583 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003584 li = li->li_next;
3585 linee = tv_get_number(&li->li_tv);
3586 if (linee < 0)
3587 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003588 if (linee > height)
3589 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003590
Bram Moolenaar4012d262020-12-29 11:57:46 +01003591 for (row = lines - 1; row < linee; ++row)
3592 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003593 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003594 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003595}
3596
3597/*
3598 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3599 * "col" and "line" are screen coordinates.
3600 */
3601 static int
3602popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3603{
3604 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3605 int line = screenline - wp->w_winrow;
3606
3607 return col >= 0 && col < width
3608 && line >= 0 && line < height
3609 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003610}
3611
3612/*
3613 * Set flags in popup_transparent[] for window "wp" to "val".
3614 */
3615 static void
3616update_popup_transparent(win_T *wp, int val)
3617{
3618 if (wp->w_popup_mask != NULL)
3619 {
3620 int width = popup_width(wp);
3621 int height = popup_height(wp);
3622 listitem_T *lio, *li;
3623 int cols, cole;
3624 int lines, linee;
3625 int col, line;
3626
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003627 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003628 {
3629 li = lio->li_tv.vval.v_list->lv_first;
3630 cols = tv_get_number(&li->li_tv);
3631 if (cols < 0)
3632 cols = width + cols + 1;
3633 li = li->li_next;
3634 cole = tv_get_number(&li->li_tv);
3635 if (cole < 0)
3636 cole = width + cole + 1;
3637 li = li->li_next;
3638 lines = tv_get_number(&li->li_tv);
3639 if (lines < 0)
3640 lines = height + lines + 1;
3641 li = li->li_next;
3642 linee = tv_get_number(&li->li_tv);
3643 if (linee < 0)
3644 linee = height + linee + 1;
3645
3646 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003647 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003648 if (cols < 0)
3649 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003650 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003651 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003652 if (lines < 0)
3653 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003654 for (line = lines; line < linee
3655 && line + wp->w_winrow < screen_Rows; ++line)
3656 for (col = cols; col < cole
3657 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003658 popup_transparent[(line + wp->w_winrow) * screen_Columns
3659 + col + wp->w_wincol] = val;
3660 }
3661 }
3662}
3663
3664/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003665 * Only called when popup window "wp" is hidden: If the window is positioned
3666 * next to a text property, and it is now visible, then unhide the popup.
3667 * We don't check if visible popups become hidden, that is done in
3668 * popup_adjust_position().
3669 * Return TRUE if the popup became unhidden.
3670 */
3671 static int
3672check_popup_unhidden(win_T *wp)
3673{
3674 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3675 {
3676 textprop_T prop;
3677 linenr_T lnum;
3678
Bram Moolenaar27724252022-05-08 15:00:04 +01003679 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3680 && find_visible_prop(wp->w_popup_prop_win,
3681 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003682 &prop, &lnum) == OK)
3683 {
3684 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003685 wp->w_popup_prop_topline = 0; // force repositioning
3686 return TRUE;
3687 }
3688 }
3689 return FALSE;
3690}
3691
3692/*
3693 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3694 * That is when the buffer in the popup was changed, or the popup is following
3695 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003696 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003697 */
3698 static int
3699popup_need_position_adjust(win_T *wp)
3700{
3701 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3702 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003703 if (win_valid(wp->w_popup_prop_win)
3704 && (wp->w_popup_prop_changedtick
3705 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3706 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3707 return TRUE;
3708
3709 // May need to adjust the width if the cursor moved.
3710 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003711}
3712
3713/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003714 * Update "popup_mask" if needed.
3715 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003716 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003717 * Also marks window lines for redrawing.
3718 */
3719 void
3720may_update_popup_mask(int type)
3721{
3722 win_T *wp;
3723 short *mask;
3724 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003725 int redraw_all_popups = FALSE;
3726 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003727
3728 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003729 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3730 // basic (such as the screen size) must have changed.
3731 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003732 {
3733 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003734 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003735 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003736
3737 // Check if any popup window buffer has changed and if any popup connected
3738 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003739 FOR_ALL_POPUPWINS(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;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003744 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003745 if (wp->w_popup_flags & POPF_HIDDEN)
3746 popup_mask_refresh |= check_popup_unhidden(wp);
3747 else if (popup_need_position_adjust(wp))
3748 popup_mask_refresh = TRUE;
3749
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003750 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003751 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003752
3753 // Need to update the mask, something has changed.
3754 popup_mask_refresh = FALSE;
3755 popup_mask_tab = curtab;
3756 popup_visible = FALSE;
3757
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003758 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003759 // If redrawing only what is needed, update "popup_mask_next" and then
3760 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003761 redrawing_all_win = TRUE;
3762 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003763 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003764 redrawing_all_win = FALSE;
3765 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003766 mask = popup_mask;
3767 else
3768 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003769 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003770
3771 // Find the window with the lowest zindex that hasn't been handled yet,
3772 // so that the window with a higher zindex overwrites the value in
3773 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003774 popup_reset_handled(POPUP_HANDLED_4);
3775 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003776 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003777 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003778 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003779
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003780 popup_visible = TRUE;
3781
Bram Moolenaar12034e22019-08-25 22:25:02 +02003782 // Recompute the position if the text changed. It may make the popup
3783 // hidden if it's attach to a text property that is no longer visible.
3784 if (redraw_all_popups || popup_need_position_adjust(wp))
3785 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003786 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003787 if (wp->w_popup_flags & POPF_HIDDEN)
3788 continue;
3789 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003790
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003791 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003792 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003793 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003794 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003795 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003796 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003797 col < wp->w_wincol + width - wp->w_popup_leftoff
3798 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003799 if (wp->w_zindex < POPUPMENU_ZINDEX
3800 && pum_visible()
3801 && pum_under_menu(line, col, FALSE))
3802 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3803 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003804 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003805 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003806 }
3807
3808 // Only check which lines are to be updated if not already
3809 // updating all lines.
3810 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003811 {
3812 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3813 win_T *prev_wp = NULL;
3814
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003815 for (line = 0; line < screen_Rows; ++line)
3816 {
3817 int col_done = 0;
3818
3819 for (col = 0; col < screen_Columns; ++col)
3820 {
3821 int off = line * screen_Columns + col;
3822
3823 if (popup_mask[off] != popup_mask_next[off])
3824 {
3825 popup_mask[off] = popup_mask_next[off];
3826
3827 if (line >= cmdline_row)
3828 {
3829 // the command line needs to be cleared if text below
3830 // the popup is now visible.
3831 if (!msg_scrolled && popup_mask_next[off] == 0)
3832 clear_cmdline = TRUE;
3833 }
3834 else if (col >= col_done)
3835 {
3836 linenr_T lnum;
3837 int line_cp = line;
3838 int col_cp = col;
3839
3840 // The screen position "line" / "col" needs to be
3841 // redrawn. Figure out what window that is and update
3842 // w_redraw_top and w_redr_bot. Only needs to be done
3843 // once for each window line.
3844 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3845 if (wp != NULL)
3846 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003847#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003848 // A terminal window needs to be redrawn.
3849 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003850 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003851 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003852#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003853 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003854 if (wp != prev_wp)
3855 {
3856 vim_memset(plines_cache, 0,
3857 sizeof(int) * Rows);
3858 prev_wp = wp;
3859 }
3860
3861 if (line_cp >= wp->w_height)
3862 // In (or below) status line
3863 wp->w_redr_status = TRUE;
3864 else
3865 {
3866 // compute the position in the buffer line
3867 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003868 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003869 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003870 redrawWinline(wp, lnum);
3871 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003872 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003873
3874 // This line is going to be redrawn, no need to
3875 // check until the right side of the window.
3876 col_done = wp->w_wincol + wp->w_width - 1;
3877 }
3878 }
3879 }
3880 }
3881 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003882
3883 vim_free(plines_cache);
3884 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003885
3886 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003887}
3888
3889/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003890 * If the current window is a popup and something relevant changed, recompute
3891 * the position and size.
3892 */
3893 void
3894may_update_popup_position(void)
3895{
3896 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3897 popup_adjust_position(curwin);
3898}
3899
3900/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003901 * Return a string of "len" spaces in IObuff.
3902 */
3903 static char_u *
3904get_spaces(int len)
3905{
3906 vim_memset(IObuff, ' ', (size_t)len);
3907 IObuff[len] = NUL;
3908 return IObuff;
3909}
3910
3911/*
3912 * Update popup windows. They are drawn on top of normal windows.
3913 * "win_update" is called for each popup window, lowest zindex first.
3914 */
3915 void
3916update_popups(void (*win_update)(win_T *wp))
3917{
3918 win_T *wp;
3919 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003920 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003921 int total_width;
3922 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003923 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003924 int popup_attr;
3925 int border_attr[4];
3926 int border_char[8];
3927 char_u buf[MB_MAXBYTES];
3928 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003929 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003930 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003931 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003932 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003933 int sb_thumb_top = 0;
3934 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003935 int attr_scroll = 0;
3936 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003937
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003938 // hide the cursor until redrawing is done.
3939 cursor_off();
3940
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003941 // Find the window with the lowest zindex that hasn't been updated yet,
3942 // so that the window with a higher zindex is drawn later, thus goes on
3943 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003944 popup_reset_handled(POPUP_HANDLED_5);
3945 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003946 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003947 int title_len = 0;
3948 int title_wincol;
3949
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003950 // This drawing uses the zindex of the popup window, so that it's on
3951 // top of the text but doesn't draw when another popup with higher
3952 // zindex is on top of the character.
3953 screen_zindex = wp->w_zindex;
3954
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003955 // Set flags in popup_transparent[] for masked cells.
3956 update_popup_transparent(wp, 1);
3957
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003958 // adjust w_winrow and w_wincol for border and padding, since
3959 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003960 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003961 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3962 - wp->w_popup_leftoff;
3963 if (wp->w_wincol + left_extra < 0)
3964 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003965 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003966 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003967
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003968 // Draw the popup text, unless it's off screen.
3969 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003970 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003971 // May need to update the "cursorline" highlighting, which may also
3972 // change "topline"
3973 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3974 popup_highlight_curline(wp);
3975
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003976 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003977
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003978 // move the cursor into the visible lines, otherwise executing
3979 // commands with win_execute() may cause the text to jump.
3980 if (wp->w_cursor.lnum < wp->w_topline)
3981 wp->w_cursor.lnum = wp->w_topline;
3982 else if (wp->w_cursor.lnum >= wp->w_botline)
3983 wp->w_cursor.lnum = wp->w_botline - 1;
3984 }
3985
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003986 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003987 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003988
3989 // Add offset for border and padding if not done already.
3990 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3991 {
3992 wp->w_wcol += left_extra;
3993 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3994 }
3995 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003996 {
3997 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003998 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003999 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004000
Bram Moolenaareabddc42022-04-02 15:32:16 +01004001 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004002 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004003 popup_attr = get_wcr_attr(wp);
4004
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004005 if (wp->w_winrow + total_height > cmdline_row)
4006 wp->w_popup_flags |= POPF_ON_CMDLINE;
4007 else
4008 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4009
4010
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004011 // We can only use these line drawing characters when 'encoding' is
4012 // "utf-8" and 'ambiwidth' is "single".
4013 if (enc_utf8 && *p_ambw == 's')
4014 {
4015 border_char[0] = border_char[2] = 0x2550;
4016 border_char[1] = border_char[3] = 0x2551;
4017 border_char[4] = 0x2554;
4018 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004019 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4020 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004021 border_char[7] = 0x255a;
4022 }
4023 else
4024 {
4025 border_char[0] = border_char[2] = '-';
4026 border_char[1] = border_char[3] = '|';
4027 for (i = 4; i < 8; ++i)
4028 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004029 if (wp->w_popup_flags & POPF_RESIZE)
4030 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004031 }
4032 for (i = 0; i < 8; ++i)
4033 if (wp->w_border_char[i] != 0)
4034 border_char[i] = wp->w_border_char[i];
4035
4036 for (i = 0; i < 4; ++i)
4037 {
4038 border_attr[i] = popup_attr;
4039 if (wp->w_border_highlight[i] != NULL)
4040 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4041 }
4042
Bram Moolenaard91467f2020-11-21 12:42:09 +01004043 // Title goes on top of border or padding.
4044 title_wincol = wp->w_wincol + 1;
4045 if (wp->w_popup_title != NULL)
4046 {
rbtnnc6119412021-08-07 13:08:45 +02004047 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004048
Ralf Schandlbc869872021-05-28 14:12:14 +02004049 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004050 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004051 {
4052 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4053 char_u *title_text = alloc(title_byte_len + 1);
4054
4055 if (title_text != NULL)
4056 {
4057 trunc_string(wp->w_popup_title, title_text,
4058 total_width - 2, title_byte_len + 1);
4059 screen_puts(title_text, wp->w_winrow, title_wincol,
4060 wp->w_popup_border[0] > 0
4061 ? border_attr[0] : popup_attr);
4062 vim_free(title_text);
4063 }
4064
Bram Moolenaard91467f2020-11-21 12:42:09 +01004065 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004066 }
4067 else
4068 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4069 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004070 }
4071
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004072 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004073 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004074 if (wp->w_popup_border[0] > 0)
4075 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004076 // top border; do not draw over the title
4077 if (title_len > 0)
4078 {
4079 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4080 wincol < 0 ? 0 : wincol, title_wincol,
4081 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004082 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004083 border_char[0], border_attr[0]);
4084 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4085 title_wincol + title_len, wincol + total_width,
4086 border_char[0], border_char[0], border_attr[0]);
4087 }
4088 else
4089 {
4090 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4091 wincol < 0 ? 0 : wincol, wincol + total_width,
4092 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4093 ? border_char[4] : border_char[0],
4094 border_char[0], border_attr[0]);
4095 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004096 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004097 {
4098 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4099 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004100 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004101 }
4102 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004103 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4104 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004105
Bram Moolenaard529ba52019-07-02 23:13:53 +02004106 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4107 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004108 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004109 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004110 - wp->w_has_scrollbar;
4111 if (padcol < 0)
4112 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004113 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004114 padcol = 0;
4115 }
4116 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004117 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004118 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004119 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004120 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004121 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004122 // top padding and no border; do not draw over the title
4123 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004124 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004125 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004126 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004127 row += 1;
4128 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004129 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004130 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004131 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004132 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004133
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004134 // Compute scrollbar thumb position and size.
4135 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004136 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004137 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4138 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004139 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004140
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004141 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4142 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004143 if (wp->w_topline > 1 && sb_thumb_height == height)
4144 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004145 if (sb_thumb_height == 0)
4146 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004147 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004148 // it just fits, avoid divide by zero
4149 sb_thumb_top = 0;
4150 else
4151 sb_thumb_top = (wp->w_topline - 1
4152 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004153 * (wp->w_height - sb_thumb_height)
4154 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004155 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4156 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004157 last = total_height - top_off - wp->w_popup_border[2];
4158 if (sb_thumb_top >= last)
4159 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004160 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004161
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004162 if (wp->w_scrollbar_highlight != NULL)
4163 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4164 else
4165 attr_scroll = highlight_attr[HLF_PSB];
4166 if (wp->w_thumb_highlight != NULL)
4167 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4168 else
4169 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004170 }
4171
4172 for (i = wp->w_popup_border[0];
4173 i < total_height - wp->w_popup_border[2]; ++i)
4174 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004175 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004176 // left and right padding only needed next to the body
4177 int do_padding =
4178 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4179 && i < total_height - wp->w_popup_border[2]
4180 - wp->w_popup_padding[2];
4181
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004182 row = wp->w_winrow + i;
4183
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004184 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004185 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004186 {
4187 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004188 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004189 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004190 if (do_padding && wp->w_popup_padding[3] > 0)
4191 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004192 int col = wincol + wp->w_popup_border[3];
4193
Bram Moolenaard529ba52019-07-02 23:13:53 +02004194 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004195 pad_left = wp->w_popup_padding[3];
4196 if (col < 0)
4197 {
4198 pad_left += col;
4199 col = 0;
4200 }
4201 if (pad_left > 0)
4202 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4203 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004204 // scrollbar
4205 if (wp->w_has_scrollbar)
4206 {
4207 int line = i - top_off;
4208 int scroll_col = wp->w_wincol + total_width - 1
4209 - wp->w_popup_border[1];
4210
4211 if (line >= 0 && line < wp->w_height)
4212 screen_putchar(' ', row, scroll_col,
4213 line >= sb_thumb_top
4214 && line < sb_thumb_top + sb_thumb_height
4215 ? attr_thumb : attr_scroll);
4216 else
4217 screen_putchar(' ', row, scroll_col, popup_attr);
4218 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004219 // right border
4220 if (wp->w_popup_border[1] > 0)
4221 {
4222 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004223 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004224 }
4225 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004226 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004227 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004228 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004229 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4230 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004231 }
4232
4233 if (wp->w_popup_padding[2] > 0)
4234 {
4235 // bottom padding
4236 row = wp->w_winrow + wp->w_popup_border[0]
4237 + wp->w_popup_padding[0] + wp->w_height;
4238 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004239 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004240 }
4241
4242 if (wp->w_popup_border[2] > 0)
4243 {
4244 // bottom border
4245 row = wp->w_winrow + total_height - 1;
4246 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004247 wincol < 0 ? 0 : wincol,
4248 wincol + total_width,
4249 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004250 ? border_char[7] : border_char[2],
4251 border_char[2], border_attr[2]);
4252 if (wp->w_popup_border[1] > 0)
4253 {
4254 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004255 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004256 }
4257 }
4258
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004259 if (wp->w_popup_close == POPCLOSE_BUTTON)
4260 {
4261 // close button goes on top of anything at the top-right corner
4262 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004263 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004264 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4265 }
4266
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004267 update_popup_transparent(wp, 0);
4268
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004269 // Back to the normal zindex.
4270 screen_zindex = 0;
4271 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004272
4273#if defined(FEAT_SEARCH_EXTRA)
4274 // In case win_update() called start_search_hl().
4275 end_search_hl();
4276#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004277}
4278
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004279/*
4280 * Mark references in callbacks of one popup window.
4281 */
4282 static int
4283set_ref_in_one_popup(win_T *wp, int copyID)
4284{
4285 int abort = FALSE;
4286 typval_T tv;
4287
4288 if (wp->w_close_cb.cb_partial != NULL)
4289 {
4290 tv.v_type = VAR_PARTIAL;
4291 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4292 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4293 }
4294 if (wp->w_filter_cb.cb_partial != NULL)
4295 {
4296 tv.v_type = VAR_PARTIAL;
4297 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4298 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4299 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004300 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004301 return abort;
4302}
4303
4304/*
4305 * Set reference in callbacks of popup windows.
4306 */
4307 int
4308set_ref_in_popups(int copyID)
4309{
4310 int abort = FALSE;
4311 win_T *wp;
4312 tabpage_T *tp;
4313
4314 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4315 abort = abort || set_ref_in_one_popup(wp, copyID);
4316
4317 FOR_ALL_TABPAGES(tp)
4318 {
4319 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4320 abort = abort || set_ref_in_one_popup(wp, copyID);
4321 if (abort)
4322 break;
4323 }
4324 return abort;
4325}
Bram Moolenaar79648732019-07-18 21:43:07 +02004326
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004327 int
4328popup_is_popup(win_T *wp)
4329{
4330 return wp->w_popup_flags != 0;
4331}
4332
4333#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004334/*
4335 * Find an existing popup used as the preview window, in the current tab page.
4336 * Return NULL if not found.
4337 */
4338 win_T *
4339popup_find_preview_window(void)
4340{
4341 win_T *wp;
4342
4343 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004344 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004345 if (wp->w_p_pvw)
4346 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004347 return NULL;
4348}
4349
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004350/*
4351 * Find an existing popup used as the info window, in the current tab page.
4352 * Return NULL if not found.
4353 */
4354 win_T *
4355popup_find_info_window(void)
4356{
4357 win_T *wp;
4358
4359 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004360 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004361 if (wp->w_popup_flags & POPF_INFO)
4362 return wp;
4363 return NULL;
4364}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004365#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004366
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004367 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004368f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4369{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004370#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004371 win_T *wp = popup_find_info_window();
4372
4373 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004374#else
4375 rettv->vval.v_number = 0;
4376#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004377}
4378
4379 void
4380f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004381{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004382#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004383 win_T *wp = popup_find_preview_window();
4384
4385 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004386#else
4387 rettv->vval.v_number = 0;
4388#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004389}
4390
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004391#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004392/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004393 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004394 * NOTE: this makes the popup the current window, so that the file can be
4395 * edited. However, it must not remain to be the current window, the caller
4396 * must make sure of that.
4397 */
4398 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004399popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004400{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004401 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004402
4403 if (wp == NULL)
4404 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004405 if (info)
4406 wp->w_popup_flags |= POPF_INFO;
4407 else
4408 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004409
4410 // Set the width to a reasonable value, so that w_topline can be computed.
4411 if (wp->w_minwidth > 0)
4412 wp->w_width = wp->w_minwidth;
4413 else if (wp->w_maxwidth > 0)
4414 wp->w_width = wp->w_maxwidth;
4415 else
4416 wp->w_width = curwin->w_width;
4417
4418 // Will switch to another buffer soon, dummy one can be wiped.
4419 wp->w_buffer->b_locked = FALSE;
4420
4421 win_enter(wp, FALSE);
4422 return OK;
4423}
4424
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004425/*
4426 * Close any preview popup.
4427 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004428 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004429popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004430{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004431 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004432
4433 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004434 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004435}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004436
4437/*
4438 * Hide the info popup.
4439 */
4440 void
4441popup_hide_info(void)
4442{
4443 win_T *wp = popup_find_info_window();
4444
4445 if (wp != NULL)
4446 popup_hide(wp);
4447}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004448
4449/*
4450 * Close any info popup.
4451 */
4452 void
4453popup_close_info(void)
4454{
4455 win_T *wp = popup_find_info_window();
4456
4457 if (wp != NULL)
4458 popup_close_with_retval(wp, -1);
4459}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004460#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004461
Bram Moolenaar9198de32022-08-27 21:30:03 +01004462#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4463
Bram Moolenaar9198de32022-08-27 21:30:03 +01004464/*
4465 * Get the message window.
4466 * Returns NULL if something failed.
4467 */
4468 win_T *
4469popup_get_message_win(void)
4470{
4471 if (message_win == NULL)
4472 {
4473 int i;
4474
4475 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4476
4477 if (message_win == NULL)
4478 return NULL;
4479
4480 // use the full screen width
4481 message_win->w_width = Columns;
4482
4483 // position at bottom of screen
4484 message_win->w_popup_pos = POPPOS_BOTTOM;
4485 message_win->w_wantcol = 1;
4486 message_win->w_minwidth = 9999;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01004487 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004488
4489 // no padding, border at the top
4490 for (i = 0; i < 4; ++i)
4491 message_win->w_popup_padding[i] = 0;
4492 for (i = 1; i < 4; ++i)
4493 message_win->w_popup_border[i] = 0;
4494
4495 if (message_win->w_popup_timer != NULL)
4496 message_win->w_popup_timer->tr_keep = TRUE;
4497 }
4498 return message_win;
4499}
4500
4501/*
4502 * If the message window is not visible: show it
4503 * If the message window is visible: reset the timeout
4504 */
4505 void
4506popup_show_message_win(void)
4507{
4508 if (message_win != NULL)
4509 {
4510 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4511 {
4512 // the highlight may have changed.
4513 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4514 popup_show(message_win);
4515 }
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004516 if (message_win->w_popup_timer != NULL)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004517 timer_start(message_win->w_popup_timer);
4518 }
4519}
4520
4521 int
4522popup_message_win_visible(void)
4523{
4524 return message_win != NULL
4525 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4526}
4527
4528/*
4529 * If the message window is visible: hide it.
4530 */
4531 void
4532popup_hide_message_win(void)
4533{
4534 if (message_win != NULL)
4535 popup_hide(message_win);
4536}
4537
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004538/*
4539 * Invoked before outputting a message for ":echowindow".
4540 */
4541 void
4542start_echowindow(void)
4543{
4544 in_echowindow = TRUE;
4545}
4546
4547/*
4548 * Invoked after outputting a message for ":echowindow".
4549 */
4550 void
4551end_echowindow(void)
4552{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004553 in_echowindow = FALSE;
4554
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004555 // show the message window now
4556 redraw_cmd(FALSE);
4557
4558 // do not overwrite messages
4559 // TODO: only for message window
4560 msg_didout = TRUE;
4561 if (msg_col == 0)
4562 msg_col = 1;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004563}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004564#endif
4565
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004566/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004567 * Close any popup for a text property associated with "win".
4568 * Return TRUE if a popup was closed.
4569 */
4570 int
4571popup_win_closed(win_T *win)
4572{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004573 int round;
4574 win_T *wp;
4575 win_T *next;
4576 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004577
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004578 for (round = 1; round <= 2; ++round)
4579 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4580 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004581 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004582 next = wp->w_next;
4583 if (wp->w_popup_prop_win == win)
4584 {
4585 popup_close_with_retval(wp, -1);
4586 ret = TRUE;
4587 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004588 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004589 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004590}
4591
4592/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004593 * Set the title of the popup window to the file name.
4594 */
4595 void
4596popup_set_title(win_T *wp)
4597{
4598 if (wp->w_buffer->b_fname != NULL)
4599 {
4600 char_u dirname[MAXPATHL];
4601 size_t len;
4602
4603 mch_dirname(dirname, MAXPATHL);
4604 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4605
4606 vim_free(wp->w_popup_title);
4607 len = STRLEN(wp->w_buffer->b_fname) + 3;
4608 wp->w_popup_title = alloc((int)len);
4609 if (wp->w_popup_title != NULL)
4610 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4611 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004612 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004613 }
4614}
4615
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004616# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004617/*
4618 * If there is a preview window, update the title.
4619 * Used after changing directory.
4620 */
4621 void
4622popup_update_preview_title(void)
4623{
4624 win_T *wp = popup_find_preview_window();
4625
4626 if (wp != NULL)
4627 popup_set_title(wp);
4628}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004629# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004630
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004631#endif // FEAT_PROP_POPUP