blob: 7459b98d4b44e3a5852eefc5ebe5546eea98df34 [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
32// Window used for messages when 'winheight' is zero.
33static win_T *message_win = NULL;
34#endif
35
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020036static void popup_adjust_position(win_T *wp);
37
Bram Moolenaar4d784b22019-05-25 19:51:39 +020038/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020039 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020041 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020042 */
43 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020044popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020045{
46 dictitem_T *di;
47 char_u *val;
48 char_u *s;
49 char_u *endp;
50 int n = 0;
51
52 di = dict_find(dict, key, -1);
53 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020054 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020055
56 val = tv_get_string(&di->di_tv);
57 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020058 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059
60 setcursor_mayforce(TRUE);
61 s = val + 6;
62 if (*s != NUL)
63 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020064 endp = s;
65 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
66 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020067 if (endp != NULL && *skipwhite(endp) != NUL)
68 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020069 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020070 return 0;
71 }
72 }
73
74 if (STRCMP(key, "line") == 0)
75 n = screen_screenrow() + 1 + n;
76 else // "col"
77 n = screen_screencol() + 1 + n;
78
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020079 // Zero means "not set", use -1 instead.
80 if (n == 0)
81 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020082 return n;
83}
84
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020085 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020086set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020087{
88 dictitem_T *di;
89
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020090 di = dict_find(dict, (char_u *)name, -1);
91 if (di != NULL)
92 {
93 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000094 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020095 else
96 {
97 list_T *list = di->di_tv.vval.v_list;
98 listitem_T *li;
99 int i;
100 int nr;
101
102 for (i = 0; i < 4; ++i)
103 array[i] = 1;
104 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100105 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200106 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200107 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
108 ++i, li = li->li_next)
109 {
110 nr = (int)tv_get_number(&li->li_tv);
111 if (nr >= 0)
112 array[i] = nr > max_val ? max_val : nr;
113 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100114 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200115 }
116 }
117}
118
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200119/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200120 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200121 */
122 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200123set_moved_values(win_T *wp)
124{
125 wp->w_popup_curwin = curwin;
126 wp->w_popup_lnum = curwin->w_cursor.lnum;
127 wp->w_popup_mincol = curwin->w_cursor.col;
128 wp->w_popup_maxcol = curwin->w_cursor.col;
129}
130
131/*
132 * Used when popup options contain "moved" with "word" or "WORD".
133 */
134 static void
135set_moved_columns(win_T *wp, int flags)
136{
137 char_u *ptr;
138 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
139
140 if (len > 0)
141 {
142 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
143 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
144 }
145}
146
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200147/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200148 * Used when popup options contain "mousemoved": set default moved values.
149 */
150 static void
151set_mousemoved_values(win_T *wp)
152{
153 wp->w_popup_mouse_row = mouse_row;
154 wp->w_popup_mouse_mincol = mouse_col;
155 wp->w_popup_mouse_maxcol = mouse_col;
156}
157
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000158 static void
159update_popup_uses_mouse_move(void)
160{
161 popup_uses_mouse_move = FALSE;
162 if (popup_visible)
163 {
164 win_T *wp;
165
166 FOR_ALL_POPUPWINS(wp)
167 if (wp->w_popup_mouse_row != 0)
168 {
169 popup_uses_mouse_move = TRUE;
170 return;
171 }
172 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
173 if (wp->w_popup_mouse_row != 0)
174 {
175 popup_uses_mouse_move = TRUE;
176 return;
177 }
178 }
179}
180
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200223 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
224 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200225 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200226 * Caller should check the left mouse button was clicked.
227 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200228 */
229 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200230popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200231{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200232 if (wp->w_popup_close == POPCLOSE_BUTTON
233 && row == 0 && col == popup_width(wp) - 1)
234 {
235 popup_close_for_mouse_click(wp);
236 return TRUE;
237 }
238 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200239}
240
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200241// Values set when dragging a popup window starts.
242static int drag_start_row;
243static int drag_start_col;
244static int drag_start_wantline;
245static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200246static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200247
248/*
249 * Mouse down on border of popup window: start dragging it.
250 * Uses mouse_col and mouse_row.
251 */
252 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200253popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200254{
255 drag_start_row = mouse_row;
256 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200257 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200258 drag_start_wantline = wp->w_winrow + 1;
259 else
260 drag_start_wantline = wp->w_wantline;
261 if (wp->w_wantcol == 0)
262 drag_start_wantcol = wp->w_wincol + 1;
263 else
264 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200265
266 // Stop centering the popup
267 if (wp->w_popup_pos == POPPOS_CENTER)
268 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269
270 drag_on_resize_handle = wp->w_popup_border[1] > 0
271 && wp->w_popup_border[2] > 0
272 && row == popup_height(wp) - 1
273 && col == popup_width(wp) - 1;
274
275 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
276 {
277 if (wp->w_popup_pos == POPPOS_TOPRIGHT
278 || wp->w_popup_pos == POPPOS_BOTRIGHT)
279 wp->w_wantcol = wp->w_wincol + 1;
280 if (wp->w_popup_pos == POPPOS_BOTLEFT)
281 wp->w_wantline = wp->w_winrow + 1;
282 wp->w_popup_pos = POPPOS_TOPLEFT;
283 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284}
285
286/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200287 * Mouse moved while dragging a popup window: adjust the window popup position
288 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200289 */
290 void
291popup_drag(win_T *wp)
292{
293 // The popup may be closed before dragging stops.
294 if (!win_valid_popup(wp))
295 return;
296
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200297 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
298 {
299 int width_inc = mouse_col - drag_start_col;
300 int height_inc = mouse_row - drag_start_row;
301
302 if (width_inc != 0)
303 {
304 int width = wp->w_width + width_inc;
305
306 if (width < 1)
307 width = 1;
308 wp->w_minwidth = width;
309 wp->w_maxwidth = width;
310 drag_start_col = mouse_col;
311 }
312
313 if (height_inc != 0)
314 {
315 int height = wp->w_height + height_inc;
316
317 if (height < 1)
318 height = 1;
319 wp->w_minheight = height;
320 wp->w_maxheight = height;
321 drag_start_row = mouse_row;
322 }
323
324 popup_adjust_position(wp);
325 return;
326 }
327
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000328 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200329 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200330 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
331 if (wp->w_wantline < 1)
332 wp->w_wantline = 1;
333 if (wp->w_wantline > Rows)
334 wp->w_wantline = Rows;
335 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
336 if (wp->w_wantcol < 1)
337 wp->w_wantcol = 1;
338 if (wp->w_wantcol > Columns)
339 wp->w_wantcol = Columns;
340
341 popup_adjust_position(wp);
342}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200343
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200344/*
345 * Set w_firstline to match the current "wp->w_topline".
346 */
347 void
348popup_set_firstline(win_T *wp)
349{
350 int height = wp->w_height;
351
352 wp->w_firstline = wp->w_topline;
353 popup_adjust_position(wp);
354
355 // we don't want the popup to get smaller, decrement the first line
356 // until it doesn't
357 while (wp->w_firstline > 1 && wp->w_height < height)
358 {
359 --wp->w_firstline;
360 popup_adjust_position(wp);
361 }
362}
363
364/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200365 * Return TRUE if the position is in the popup window scrollbar.
366 */
367 int
368popup_is_in_scrollbar(win_T *wp, int row, int col)
369{
370 return wp->w_has_scrollbar
371 && row >= wp->w_popup_border[0]
372 && row < popup_height(wp) - wp->w_popup_border[2]
373 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
374}
375
376
377/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200378 * Handle a click in a popup window, if it is in the scrollbar.
379 */
380 void
381popup_handle_scrollbar_click(win_T *wp, int row, int col)
382{
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200383 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200384 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100385 int height = popup_height(wp);
386 int new_topline = wp->w_topline;
387
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200388 if (row >= height / 2)
389 {
390 // Click in lower half, scroll down.
391 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaard28950f2022-05-29 14:13:04 +0100392 ++new_topline;
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200393 }
394 else if (wp->w_topline > 1)
395 // click on upper half, scroll up.
Bram Moolenaard28950f2022-05-29 14:13:04 +0100396 --new_topline;
397 if (new_topline != wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200398 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100399 set_topline(wp, new_topline);
400 if (wp == curwin)
401 {
402 if (wp->w_cursor.lnum < wp->w_topline)
403 {
404 wp->w_cursor.lnum = wp->w_topline;
405 check_cursor();
406 }
407 else if (wp->w_cursor.lnum >= wp->w_botline)
408 {
409 wp->w_cursor.lnum = wp->w_botline - 1;
410 check_cursor();
411 }
412 }
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200413 popup_set_firstline(wp);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100414 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200415 }
416 }
417}
418
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200419#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100420/*
421 * Add a timer to "wp" with "time".
422 * If "close" is true use popup_close(), otherwise popup_hide().
423 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200424 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100425popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200426{
427 char_u cbbuf[50];
428 char_u *ptr = cbbuf;
429 typval_T tv;
430
431 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100432 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
433 wp->w_id);
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200434 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200435 {
436 wp->w_popup_timer = create_timer(time, 0);
437 wp->w_popup_timer->tr_callback = get_callback(&tv);
438 clear_tv(&tv);
439 }
440}
441#endif
442
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100443 static poppos_T
444get_pos_entry(dict_T *d, int give_error)
445{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100446 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100447 int nr;
448
449 if (str == NULL)
450 return POPPOS_NONE;
451
K.Takataeeec2542021-06-02 13:28:16 +0200452 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100453 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
454 return poppos_entries[nr].pp_val;
455
456 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000457 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100458 return POPPOS_NONE;
459}
460
Bram Moolenaar17627312019-06-02 19:53:44 +0200461/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200462 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200463 */
464 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200465apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200466{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200467 int nr;
468 char_u *str;
469 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200470
Bram Moolenaard61efa52022-07-23 09:52:04 +0100471 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200472 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100473 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200474 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100475 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200476 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100477 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200478 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200479
480 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200481 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200482 wp->w_wantline = nr;
483 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200484 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200485 wp->w_wantcol = nr;
486
Bram Moolenaar55881332020-08-18 13:04:15 +0200487
Bram Moolenaard61efa52022-07-23 09:52:04 +0100488 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200489 if (nr != -1)
490 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200491
Bram Moolenaar12034e22019-08-25 22:25:02 +0200492 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100493 poppos_T ppt = get_pos_entry(d, TRUE);
494
495 if (ppt != POPPOS_NONE)
496 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200497 }
498
Bram Moolenaard61efa52022-07-23 09:52:04 +0100499 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200500 if (str != NULL)
501 {
502 wp->w_popup_prop_type = 0;
503 if (*str != NUL)
504 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100505 wp->w_popup_prop_win = curwin;
506 di = dict_find(d, (char_u *)"textpropwin", -1);
507 if (di != NULL)
508 {
509 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100510 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100511 wp->w_popup_prop_win = curwin;
512 }
513
514 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200515 if (nr <= 0)
516 nr = find_prop_type_id(str, NULL);
517 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000518 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200519 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200520 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200521 }
522 }
523
524 di = dict_find(d, (char_u *)"textpropid", -1);
525 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100526 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200527}
528
Bram Moolenaarb0992022020-01-30 14:55:42 +0100529/*
530 * Handle "moved" and "mousemoved" arguments.
531 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200532 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200533handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
534{
535 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
536 {
537 char_u *s = di->di_tv.vval.v_string;
538 int flags = 0;
539
540 if (STRCMP(s, "word") == 0)
541 flags = FIND_IDENT | FIND_STRING;
542 else if (STRCMP(s, "WORD") == 0)
543 flags = FIND_STRING;
544 else if (STRCMP(s, "expr") == 0)
545 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
546 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000547 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200548 if (flags != 0)
549 {
550 if (mousemoved)
551 set_mousemoved_columns(wp, flags);
552 else
553 set_moved_columns(wp, flags);
554 }
555 }
556 else if (di->di_tv.v_type == VAR_LIST
557 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200558 && (di->di_tv.vval.v_list->lv_len == 2
559 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200560 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200561 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100562 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200563 int mincol;
564 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200565
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200566 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100567 li = l->lv_first;
568 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200569 {
570 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
571
572 // Three numbers, might be from popup_getoptions().
573 if (mousemoved)
574 wp->w_popup_mouse_row = nr;
575 else
576 wp->w_popup_lnum = nr;
577 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100578 if (nr == 0)
579 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200580 }
581
582 mincol = tv_get_number(&li->li_tv);
583 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200584 if (mousemoved)
585 {
586 wp->w_popup_mouse_mincol = mincol;
587 wp->w_popup_mouse_maxcol = maxcol;
588 }
589 else
590 {
591 wp->w_popup_mincol = mincol;
592 wp->w_popup_maxcol = maxcol;
593 }
594 }
595 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000596 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200597}
598
599 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200600check_highlight(dict_T *dict, char *name, char_u **pval)
601{
602 dictitem_T *di;
603 char_u *str;
604
605 di = dict_find(dict, (char_u *)name, -1);
606 if (di != NULL)
607 {
608 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000609 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200610 else
611 {
612 str = tv_get_string(&di->di_tv);
613 if (*str != NUL)
614 *pval = vim_strsave(str);
615 }
616 }
617}
618
Bram Moolenaarae943152019-06-16 22:54:14 +0200619/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200620 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200621 */
622 static void
623popup_show_curline(win_T *wp)
624{
625 if (wp->w_cursor.lnum < wp->w_topline)
626 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200627 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200628 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200629 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200630 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200631 if (wp->w_topline < 1)
632 wp->w_topline = 1;
633 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
634 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200635 while (wp->w_topline < wp->w_cursor.lnum
636 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
637 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
638 > wp->w_height)
639 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200640 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200641
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200642 // Don't let "firstline" cause a scroll.
643 if (wp->w_firstline > 0)
644 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200645}
646
647/*
648 * Get the sign group name for window "wp".
649 * Returns a pointer to a static buffer, overwritten on the next call.
650 */
651 static char_u *
652popup_get_sign_name(win_T *wp)
653{
654 static char buf[30];
655
656 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
657 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200658}
659
660/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200661 * Highlight the line with the cursor.
662 * Also scrolls the text to put the cursor line in view.
663 */
664 static void
665popup_highlight_curline(win_T *wp)
666{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200667 int sign_id = 0;
668 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200669
Bram Moolenaar72570732019-11-30 14:21:53 +0100670 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200671
672 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
673 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200674 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200675
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200676 if (!sign_exists_by_name(sign_name))
677 {
678 char *linehl = "PopupSelected";
679
680 if (syn_name2id((char_u *)linehl) == 0)
681 linehl = "PmenuSel";
Bram Moolenaar9198de32022-08-27 21:30:03 +0100682 sign_define_by_name(sign_name, NULL, (char_u *)linehl,
683 NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200684 }
685
Bram Moolenaar72570732019-11-30 14:21:53 +0100686 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200687 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100688 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200689 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200690 else
691 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200692 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200693}
694
695/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200696 * Shared between popup_create() and f_popup_setoptions().
697 */
698 static void
699apply_general_options(win_T *wp, dict_T *dict)
700{
701 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200702 int nr;
703 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200704
Bram Moolenaarae943152019-06-16 22:54:14 +0200705 // TODO: flip
706
707 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200708 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200709 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100710 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200711 if (wp->w_firstline < 0)
712 wp->w_firstline = -1;
713 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200714
Bram Moolenaard61efa52022-07-23 09:52:04 +0100715 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200716 if (nr != -1)
717 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200718
Bram Moolenaard61efa52022-07-23 09:52:04 +0100719 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200720 if (str != NULL)
721 {
722 vim_free(wp->w_popup_title);
723 wp->w_popup_title = vim_strsave(str);
724 }
725
Bram Moolenaard61efa52022-07-23 09:52:04 +0100726 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200727 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200728 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200729
Bram Moolenaard61efa52022-07-23 09:52:04 +0100730 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200731 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200732 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200733 if (nr)
734 wp->w_popup_flags |= POPF_DRAG;
735 else
736 wp->w_popup_flags &= ~POPF_DRAG;
737 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100738 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000739 if (nr != -1)
740 {
741 if (nr)
742 wp->w_popup_flags |= POPF_DRAGALL;
743 else
744 wp->w_popup_flags &= ~POPF_DRAGALL;
745 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200746
Bram Moolenaard61efa52022-07-23 09:52:04 +0100747 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200748 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100749 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100750 if (nr)
751 wp->w_popup_flags |= POPF_POSINVERT;
752 else
753 wp->w_popup_flags &= ~POPF_POSINVERT;
754 }
755
Bram Moolenaard61efa52022-07-23 09:52:04 +0100756 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200757 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200758 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200759 if (nr)
760 wp->w_popup_flags |= POPF_RESIZE;
761 else
762 wp->w_popup_flags &= ~POPF_RESIZE;
763 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200764
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200765 di = dict_find(dict, (char_u *)"close", -1);
766 if (di != NULL)
767 {
768 int ok = TRUE;
769
770 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
771 {
772 char_u *s = di->di_tv.vval.v_string;
773
774 if (STRCMP(s, "none") == 0)
775 wp->w_popup_close = POPCLOSE_NONE;
776 else if (STRCMP(s, "button") == 0)
777 wp->w_popup_close = POPCLOSE_BUTTON;
778 else if (STRCMP(s, "click") == 0)
779 wp->w_popup_close = POPCLOSE_CLICK;
780 else
781 ok = FALSE;
782 }
783 else
784 ok = FALSE;
785 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000786 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200787 }
788
Bram Moolenaard61efa52022-07-23 09:52:04 +0100789 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200790 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000791 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200792 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
793 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000794#ifdef FEAT_TERMINAL
795 term_update_wincolor(wp);
796#endif
797 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200798
Bram Moolenaarae943152019-06-16 22:54:14 +0200799 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
800 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200801
Bram Moolenaar790498b2019-06-01 22:15:29 +0200802 di = dict_find(dict, (char_u *)"borderhighlight", -1);
803 if (di != NULL)
804 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200805 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000806 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200807 else
808 {
809 list_T *list = di->di_tv.vval.v_list;
810 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200811 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200812
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200813 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200814 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
815 ++i, li = li->li_next)
816 {
817 str = tv_get_string(&li->li_tv);
818 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100819 {
820 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200821 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100822 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200823 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200824 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
825 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100826 {
827 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200828 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200829 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100830 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200831 }
832 }
833
Bram Moolenaar790498b2019-06-01 22:15:29 +0200834 di = dict_find(dict, (char_u *)"borderchars", -1);
835 if (di != NULL)
836 {
837 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000838 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200839 else
840 {
841 list_T *list = di->di_tv.vval.v_list;
842 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200843 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200844
845 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100846 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200847 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200848 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
849 ++i, li = li->li_next)
850 {
851 str = tv_get_string(&li->li_tv);
852 if (*str != NUL)
853 wp->w_border_char[i] = mb_ptr2char(str);
854 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200855 if (list->lv_len == 1)
856 for (i = 1; i < 8; ++i)
857 wp->w_border_char[i] = wp->w_border_char[0];
858 if (list->lv_len == 2)
859 {
860 for (i = 4; i < 8; ++i)
861 wp->w_border_char[i] = wp->w_border_char[1];
862 for (i = 1; i < 4; ++i)
863 wp->w_border_char[i] = wp->w_border_char[0];
864 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200865 }
866 }
867 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200868
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200869 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
870 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
871
Bram Moolenaarae943152019-06-16 22:54:14 +0200872 di = dict_find(dict, (char_u *)"zindex", -1);
873 if (di != NULL)
874 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100875 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200876 if (wp->w_zindex < 1)
877 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
878 if (wp->w_zindex > 32000)
879 wp->w_zindex = 32000;
880 }
881
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200882 di = dict_find(dict, (char_u *)"mask", -1);
883 if (di != NULL)
884 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200885 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200886
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200887 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200888 {
889 listitem_T *li;
890
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200891 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200892 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200893 {
894 if (li->li_tv.v_type != VAR_LIST
895 || li->li_tv.vval.v_list == NULL
896 || li->li_tv.vval.v_list->lv_len != 4)
897 {
898 ok = FALSE;
899 break;
900 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100901 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200902 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200903 }
904 }
905 if (ok)
906 {
907 wp->w_popup_mask = di->di_tv.vval.v_list;
908 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200909 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200910 }
911 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000912 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200913 }
914
Bram Moolenaarae943152019-06-16 22:54:14 +0200915#if defined(FEAT_TIMERS)
916 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100917 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200918 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100919 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200920#endif
921
Bram Moolenaar3397f742019-06-02 18:40:06 +0200922 di = dict_find(dict, (char_u *)"moved", -1);
923 if (di != NULL)
924 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200925 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200926 handle_moved_argument(wp, di, FALSE);
927 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200928
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200929 di = dict_find(dict, (char_u *)"mousemoved", -1);
930 if (di != NULL)
931 {
932 set_mousemoved_values(wp);
933 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200934 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200935
Bram Moolenaard61efa52022-07-23 09:52:04 +0100936 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100937 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200938 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100939 if (nr != 0)
940 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200941 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100942 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200943 }
944
Bram Moolenaarae943152019-06-16 22:54:14 +0200945 di = dict_find(dict, (char_u *)"filter", -1);
946 if (di != NULL)
947 {
948 callback_T callback = get_callback(&di->di_tv);
949
950 if (callback.cb_name != NULL)
951 {
952 free_callback(&wp->w_filter_cb);
953 set_callback(&wp->w_filter_cb, &callback);
954 }
955 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100956 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200957 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200958 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200959 if (nr)
960 wp->w_popup_flags |= POPF_MAPPING;
961 else
962 wp->w_popup_flags &= ~POPF_MAPPING;
963 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200964
Bram Moolenaard61efa52022-07-23 09:52:04 +0100965 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200966 if (str != NULL)
967 {
968 if (STRCMP(str, "a") == 0)
969 wp->w_filter_mode = MODE_ALL;
970 else
971 wp->w_filter_mode = mode_str2flags(str);
972 }
973
Bram Moolenaarae943152019-06-16 22:54:14 +0200974 di = dict_find(dict, (char_u *)"callback", -1);
975 if (di != NULL)
976 {
977 callback_T callback = get_callback(&di->di_tv);
978
979 if (callback.cb_name != NULL)
980 {
981 free_callback(&wp->w_close_cb);
982 set_callback(&wp->w_close_cb, &callback);
983 }
984 }
985}
986
987/*
988 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200989 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200990 */
991 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200992apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200993{
994 int nr;
995
996 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200997
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200998 if (create)
999 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001000 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1001
Bram Moolenaarae943152019-06-16 22:54:14 +02001002 apply_general_options(wp, dict);
1003
Bram Moolenaard61efa52022-07-23 09:52:04 +01001004 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001005 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001006 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001007
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001008 // when "firstline" and "cursorline" are both set and the cursor would be
1009 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001010 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1011 {
1012 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1013 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001014 else if (wp->w_cursor.lnum < wp->w_firstline
1015 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001016 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001017 wp->w_topline = wp->w_firstline;
1018 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001019 }
1020
Bram Moolenaar33796b32019-06-08 16:01:13 +02001021 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001022 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001023}
1024
1025/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001026 * Add lines to the popup from a list of strings.
1027 */
1028 static void
1029add_popup_strings(buf_T *buf, list_T *l)
1030{
1031 listitem_T *li;
1032 linenr_T lnum = 0;
1033 char_u *p;
1034
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001035 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001036 if (li->li_tv.v_type == VAR_STRING)
1037 {
1038 p = li->li_tv.vval.v_string;
1039 ml_append_buf(buf, lnum++,
1040 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1041 }
1042}
1043
1044/*
1045 * Add lines to the popup from a list of dictionaries.
1046 */
1047 static void
1048add_popup_dicts(buf_T *buf, list_T *l)
1049{
1050 listitem_T *li;
1051 listitem_T *pli;
1052 linenr_T lnum = 0;
1053 char_u *p;
1054 dict_T *dict;
1055
1056 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001057 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001058 {
1059 if (li->li_tv.v_type != VAR_DICT)
1060 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001061 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001062 return;
1063 }
1064 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001065 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001066 ml_append_buf(buf, lnum++,
1067 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1068 }
1069
1070 // add the text properties
1071 lnum = 1;
1072 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1073 {
1074 dictitem_T *di;
1075 list_T *plist;
1076
1077 dict = li->li_tv.vval.v_dict;
1078 di = dict_find(dict, (char_u *)"props", -1);
1079 if (di != NULL)
1080 {
1081 if (di->di_tv.v_type != VAR_LIST)
1082 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001083 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001084 return;
1085 }
1086 plist = di->di_tv.vval.v_list;
1087 if (plist != NULL)
1088 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001089 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001090 {
1091 if (pli->li_tv.v_type != VAR_DICT)
1092 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001093 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001094 return;
1095 }
1096 dict = pli->li_tv.vval.v_dict;
1097 if (dict != NULL)
1098 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001099 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001100
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001101 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001102 }
1103 }
1104 }
1105 }
1106 }
1107}
1108
1109/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001110 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1111 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001112 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001113popup_top_extra(win_T *wp)
1114{
1115 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1116
1117 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1118 return 1;
1119 return extra;
1120}
1121
1122/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001123 * Get the padding plus border at the left.
1124 */
1125 int
1126popup_left_extra(win_T *wp)
1127{
1128 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1129}
1130
1131/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001132 * Return the height of popup window "wp", including border and padding.
1133 */
1134 int
1135popup_height(win_T *wp)
1136{
1137 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001138 + popup_top_extra(wp)
1139 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001140}
1141
1142/*
1143 * Return the width of popup window "wp", including border, padding and
1144 * scrollbar.
1145 */
1146 int
1147popup_width(win_T *wp)
1148{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001149 // w_leftcol is how many columns of the core are left of the screen
1150 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001151 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001152 + popup_extra_width(wp)
1153 + wp->w_popup_rightoff;
1154}
1155
1156/*
1157 * Return the extra width of popup window "wp": border, padding and scrollbar.
1158 */
1159 int
1160popup_extra_width(win_T *wp)
1161{
1162 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1163 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1164 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001165}
1166
1167/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001168 * Adjust the position and size of the popup to fit on the screen.
1169 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001170 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001171popup_adjust_position(win_T *wp)
1172{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001173 linenr_T lnum;
1174 int wrapped = 0;
1175 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001176 int maxwidth_no_scrollbar;
1177 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001178 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001179 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001180 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001181 int center_vert = FALSE;
1182 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001183 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001184 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001185 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1186 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1187 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1188 int extra_height = top_extra + bot_extra;
1189 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001190 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001191 int org_winrow = wp->w_winrow;
1192 int org_wincol = wp->w_wincol;
1193 int org_width = wp->w_width;
1194 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001195 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001196 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001197 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001198 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001199 int wantline = wp->w_wantline; // adjusted for textprop
1200 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001201 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001202 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001203
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001204 wp->w_winrow = 0;
1205 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001206 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001207 wp->w_popup_leftoff = 0;
1208 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001209
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001210 // May need to update the "cursorline" highlighting, which may also change
1211 // "topline"
1212 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1213 popup_highlight_curline(wp);
1214
Bram Moolenaar12034e22019-08-25 22:25:02 +02001215 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1216 {
1217 win_T *prop_win = wp->w_popup_prop_win;
1218 textprop_T prop;
1219 linenr_T prop_lnum;
1220 pos_T pos;
1221 int screen_row;
1222 int screen_scol;
1223 int screen_ccol;
1224 int screen_ecol;
1225
1226 // Popup window is positioned relative to a text property.
1227 if (find_visible_prop(prop_win,
1228 wp->w_popup_prop_type, wp->w_popup_prop_id,
1229 &prop, &prop_lnum) == FAIL)
1230 {
1231 // Text property is no longer visible, hide the popup.
1232 // Unhiding the popup is done in check_popup_unhidden().
1233 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1234 {
1235 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001236 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001237 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001238 }
1239 return;
1240 }
1241
1242 // Compute the desired position from the position of the text
1243 // property. Use "wantline" and "wantcol" as offsets.
1244 pos.lnum = prop_lnum;
1245 pos.col = prop.tp_col;
1246 if (wp->w_popup_pos == POPPOS_TOPLEFT
1247 || wp->w_popup_pos == POPPOS_BOTLEFT)
1248 pos.col += prop.tp_len - 1;
1249 textpos2screenpos(prop_win, &pos, &screen_row,
1250 &screen_scol, &screen_ccol, &screen_ecol);
1251
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001252 if (screen_scol == 0)
1253 {
1254 // position is off screen, make the width zero to hide it.
1255 wp->w_width = 0;
1256 return;
1257 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001258 if (wp->w_popup_pos == POPPOS_TOPLEFT
1259 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1260 // below the text
1261 wantline = screen_row + wantline + 1;
1262 else
1263 // above the text
1264 wantline = screen_row + wantline - 1;
1265 center_vert = FALSE;
1266 if (wp->w_popup_pos == POPPOS_TOPLEFT
1267 || wp->w_popup_pos == POPPOS_BOTLEFT)
1268 // right of the text
1269 wantcol = screen_ecol + wantcol;
1270 else
1271 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001272 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001273 use_wantcol = TRUE;
1274 }
1275 else
1276 {
1277 // If no line was specified default to vertical centering.
1278 if (wantline == 0)
1279 center_vert = TRUE;
1280 else if (wantline < 0)
1281 // If "wantline" is negative it actually means zero.
1282 wantline = 0;
1283 if (wantcol < 0)
1284 // If "wantcol" is negative it actually means zero.
1285 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001286 }
1287
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001288 if (wp->w_popup_pos == POPPOS_CENTER)
1289 {
1290 // center after computing the size
1291 center_vert = TRUE;
1292 center_hor = TRUE;
1293 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001294 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001295 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001296 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1297 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001298 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001299 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001300 if (wp->w_winrow >= Rows)
1301 wp->w_winrow = Rows - 1;
1302 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001303 if (wp->w_popup_pos == POPPOS_BOTTOM)
1304 // assume that each buffer line takes one screen line
Bram Moolenaar37fef162022-08-29 18:16:32 +01001305 wp->w_winrow = MAX(cmdline_row
1306 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001307
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001308 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001309 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001310 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1311 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001312 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001313 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001314 // Need to see at least one character after the decoration.
1315 if (wp->w_wincol > Columns - left_extra - 1)
1316 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001317 }
1318 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001319
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001320 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001321 // When left aligned use the space available, but shift to the left when we
1322 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001323 maxspace = Columns - wp->w_wincol - left_extra;
1324 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001325 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001326 {
1327 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001328 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001329 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001330
1331 if (wp->w_p_nu || wp->w_p_rnu)
1332 margin_width = number_width(wp) + 1;
1333#ifdef FEAT_FOLDING
1334 margin_width += wp->w_p_fdc;
1335#endif
1336#ifdef FEAT_SIGNS
1337 if (signcolumn_on(wp))
1338 margin_width += 2;
1339#endif
1340 if (margin_width >= maxwidth)
1341 margin_width = maxwidth - 1;
1342
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001343 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001344 minheight = wp->w_minheight;
1345#ifdef FEAT_TERMINAL
1346 // A terminal popup initially does not have content, use a default minimal
1347 // width of 20 characters and height of 5 lines.
1348 if (wp->w_buffer->b_term != NULL)
1349 {
1350 if (minwidth == 0)
1351 minwidth = 20;
1352 if (minheight == 0)
1353 minheight = 5;
1354 }
1355#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001356
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001357 if (wp->w_maxheight > 0)
1358 maxheight = wp->w_maxheight;
1359
Bram Moolenaar8d241042019-06-12 23:40:01 +02001360 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001361 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001362 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001363 if (wp->w_topline < 1)
1364 wp->w_topline = 1;
1365 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001366 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1367
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001368 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001369 // Use a minimum width of one, so that something shows when there is no
1370 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001371 // When "firstline" is -1 then start with the last buffer line and go
1372 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001373 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001374 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001375 if (wp->w_firstline < 0)
1376 lnum = wp->w_buffer->b_ml.ml_line_count;
1377 else
1378 lnum = wp->w_topline;
1379 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001380 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001381 int len;
1382 int w_width = wp->w_width;
1383
1384 // Count Tabs for what they are worth and compute the length based on
1385 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001386 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001387 if (wp->w_width < maxwidth)
1388 wp->w_width = maxwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001389 len = win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001390 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001391 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001392
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001393 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001394 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001395 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001396 {
1397 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001398 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001399 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001400 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001401 }
1402 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001403 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001404 && allow_adjust_left
1405 && (wp->w_popup_pos == POPPOS_TOPLEFT
1406 || wp->w_popup_pos == POPPOS_BOTLEFT))
1407 {
1408 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001409 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001410
Bram Moolenaar51c31312019-06-15 22:27:23 +02001411 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001412 {
1413 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001414
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001415 len -= truncate_shift;
1416 shift_by -= truncate_shift;
1417 }
1418
1419 wp->w_wincol -= shift_by;
1420 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001421 wp->w_width = maxwidth;
1422 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001423 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001424 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001425 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001426 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1427 wp->w_width = wp->w_maxwidth;
1428 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001429
1430 if (wp->w_firstline < 0)
1431 --lnum;
1432 else
1433 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001434
1435 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001436 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001437 && (wp->w_firstline >= 0
1438 ? lnum - wp->w_topline
1439 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001440 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001441 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001442 }
1443
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001444 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001445 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001446
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001447 wp->w_has_scrollbar = wp->w_want_scrollbar
1448 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001449#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001450 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1451 // Terminal window with running job never has a scrollbar, adjusts to
1452 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001453 wp->w_has_scrollbar = FALSE;
1454#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001455 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001456 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001457 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001458 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001459 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001460 // make space for the scrollbar if needed, when lines wrap and when
1461 // applying minwidth
1462 if (maxwidth + right_extra >= maxspace
1463 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1464 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001465 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001466
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001467 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1468 {
1469 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1470
1471 if (minwidth < title_len)
1472 minwidth = title_len;
1473 }
1474
1475 if (minwidth > 0 && wp->w_width < minwidth)
1476 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001477 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001478 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001479 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001480 // some columns cut off on the right
1481 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001482
1483 // If the window doesn't fit because 'minwidth' is set then the
1484 // scrollbar is at the far right of the screen, use the size without
1485 // the scrollbar.
1486 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1487 {
1488 int off = wp->w_width - maxwidth;
1489
1490 if (off > right_extra)
1491 extra_width -= right_extra;
1492 else
1493 extra_width -= off;
1494 wp->w_width = maxwidth_no_scrollbar;
1495 }
1496 else
1497 {
1498 wp->w_width = maxwidth;
1499
1500 // when adding a scrollbar below need to adjust the width
1501 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1502 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001503 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001504 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001505 {
1506 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1507 if (wp->w_wincol < 0)
1508 wp->w_wincol = 0;
1509 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001510 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1511 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1512 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001513 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001514
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001515 // Right aligned: move to the right if needed.
1516 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001517 if (leftoff >= 0)
1518 wp->w_wincol = leftoff;
1519 else if (wp->w_popup_fixed)
1520 {
1521 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001522 // columns when displaying the window, minus left border and
1523 // padding.
1524 if (-leftoff > left_extra)
1525 wp->w_leftcol = -leftoff - left_extra;
1526 wp->w_width -= wp->w_leftcol;
1527 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001528 if (wp->w_width < 0)
1529 wp->w_width = 0;
1530 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001531 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001532
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001533 if (wp->w_p_wrap || (!wp->w_popup_fixed
1534 && (wp->w_popup_pos == POPPOS_TOPLEFT
1535 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001536 {
1537 int want_col = 0;
1538
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001539 // try to show the right border and any scrollbar
1540 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001541 if (want_col > 0 && wp->w_wincol > 0
1542 && wp->w_wincol + want_col >= Columns)
1543 {
1544 wp->w_wincol = Columns - want_col;
1545 if (wp->w_wincol < 0)
1546 wp->w_wincol = 0;
1547 }
1548 }
1549
Bram Moolenaar8d241042019-06-12 23:40:01 +02001550 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1551 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001552 if (minheight > 0 && wp->w_height < minheight)
1553 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001554 if (maxheight > 0 && wp->w_height > maxheight)
1555 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001556 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001557 if (wp->w_height > Rows - wp->w_winrow)
1558 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001559
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001560 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001561 {
1562 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1563 if (wp->w_winrow < 0)
1564 wp->w_winrow = 0;
1565 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001566 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001567 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001568 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001569 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001570 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001571 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001572 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1573 {
1574 // Bottom aligned but does not fit, and less space on the other
1575 // side or "posinvert" is off: reduce height.
1576 wp->w_winrow = 0;
1577 wp->w_height = wantline - extra_height;
1578 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001579 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001580 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001581 // Not enough space and more space on the other side: make top
1582 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001583 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001584 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001585 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001586 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001587 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1588 || wp->w_popup_pos == POPPOS_TOPLEFT)
1589 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001590 if (wp != popup_dragwin
1591 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001592 && wantline * 2 > Rows
1593 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001594 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001595 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001596 // make bottom aligned and recompute the height
1597 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001598 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001599 if (wp->w_winrow < 0)
1600 {
1601 wp->w_height += wp->w_winrow;
1602 wp->w_winrow = 0;
1603 }
1604 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001605 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001606 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001607 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001608 adjust_height_for_top_aligned = TRUE;
1609 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001610 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001611
1612 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1613 && wp->w_winrow + wp->w_height + extra_height > Rows)
1614 {
1615 // Bottom of the popup goes below the last line, reduce the height and
1616 // add a scrollbar.
1617 wp->w_height = Rows - wp->w_winrow - extra_height;
1618#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001619 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001620#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001621 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001622 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001623 if (width_with_scrollbar > 0)
1624 wp->w_width = width_with_scrollbar;
1625 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001626 }
1627
1628 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001629 if (wp->w_winrow >= Rows)
1630 wp->w_winrow = Rows - 1;
1631 else if (wp->w_winrow < 0)
1632 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001633
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001634 if (wp->w_height != org_height)
1635 win_comp_scroll(wp);
1636
Bram Moolenaar17146962019-05-30 00:12:11 +02001637 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001638 if (win_valid(wp->w_popup_prop_win))
1639 {
1640 wp->w_popup_prop_changedtick =
1641 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1642 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1643 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001644
1645 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001646 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001647 if (org_winrow != wp->w_winrow
1648 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001649 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001650 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001651 || org_width != wp->w_width
1652 || org_height != wp->w_height)
1653 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001654 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001655 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1656 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001657 popup_mask_refresh = TRUE;
1658 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001659}
1660
Bram Moolenaar17627312019-06-02 19:53:44 +02001661typedef enum
1662{
1663 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001664 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001665 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001666 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001667 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001668 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001669 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001670 TYPE_PREVIEW, // preview window
1671 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001672} create_type_T;
1673
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001674/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001675 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1676 */
1677 static int
1678popup_is_notification(create_type_T type)
1679{
1680 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1681}
1682
1683/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001684 * Make "buf" empty and set the contents to "text".
1685 * Used by popup_create() and popup_settext().
1686 */
1687 static void
1688popup_set_buffer_text(buf_T *buf, typval_T text)
1689{
1690 int lnum;
1691
1692 // Clear the buffer, then replace the lines.
1693 curbuf = buf;
1694 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001695 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001696 curbuf = curwin->w_buffer;
1697
1698 // Add text to the buffer.
1699 if (text.v_type == VAR_STRING)
1700 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001701 char_u *s = text.vval.v_string;
1702
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001703 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001704 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001705 }
1706 else
1707 {
1708 list_T *l = text.vval.v_list;
1709
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001710 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001711 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001712 if (l->lv_first == &range_list_item)
1713 emsg(_(e_using_number_as_string));
1714 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001715 // list of strings
1716 add_popup_strings(buf, l);
1717 else
1718 // list of dictionaries
1719 add_popup_dicts(buf, l);
1720 }
1721 }
1722
1723 // delete the line that was in the empty buffer
1724 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001725 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001726 curbuf = curwin->w_buffer;
1727}
1728
1729/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001730 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1731 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001732 * Return FAIL if the parsing fails.
1733 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001734 static int
1735parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001736{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001737 char_u *p =
1738#ifdef FEAT_QUICKFIX
1739 !is_preview ? p_cpp :
1740#endif
1741 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001742
Bram Moolenaar258cef52019-08-21 17:29:29 +02001743 if (wp != NULL)
1744 wp->w_popup_flags &= ~POPF_INFO_MENU;
1745
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001746 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001747 {
1748 char_u *e, *dig;
1749 char_u *s = p;
1750 int x;
1751
1752 e = vim_strchr(p, ':');
1753 if (e == NULL || e[1] == NUL)
1754 return FAIL;
1755
1756 p = vim_strchr(e, ',');
1757 if (p == NULL)
1758 p = e + STRLEN(e);
1759 dig = e + 1;
1760 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001761
1762 if (STRNCMP(s, "height:", 7) == 0)
1763 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001764 if (dig != p)
1765 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001766 if (wp != NULL)
1767 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001768 if (is_preview)
1769 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001770 wp->w_maxheight = x;
1771 }
1772 }
1773 else if (STRNCMP(s, "width:", 6) == 0)
1774 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001775 if (dig != p)
1776 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001777 if (wp != NULL)
1778 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001779 if (is_preview)
1780 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001781 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001782 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001783 }
1784 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001785 else if (STRNCMP(s, "highlight:", 10) == 0)
1786 {
1787 if (wp != NULL)
1788 {
1789 int c = *p;
1790
1791 *p = NUL;
1792 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1793 s + 10, OPT_FREE|OPT_LOCAL, 0);
1794 *p = c;
1795 }
1796 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001797 else if (STRNCMP(s, "border:", 7) == 0)
1798 {
1799 char_u *arg = s + 7;
1800 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1801 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1802 int i;
1803
1804 if (!on && !off)
1805 return FAIL;
1806 if (wp != NULL)
1807 {
1808 for (i = 0; i < 4; ++i)
1809 wp->w_popup_border[i] = on ? 1 : 0;
1810 if (off)
1811 // only show the X for close when there is a border
1812 wp->w_popup_close = POPCLOSE_NONE;
1813 }
1814 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001815 else if (STRNCMP(s, "align:", 6) == 0)
1816 {
1817 char_u *arg = s + 6;
1818 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1819 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1820
1821 if (!menu && !item)
1822 return FAIL;
1823 if (wp != NULL && menu)
1824 wp->w_popup_flags |= POPF_INFO_MENU;
1825 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001826 else
1827 return FAIL;
1828 }
1829 return OK;
1830}
1831
1832/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001833 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1834 * is not NULL.
1835 * Return FAIL if the parsing fails.
1836 */
1837 int
1838parse_previewpopup(win_T *wp)
1839{
1840 return parse_popup_option(wp, TRUE);
1841}
1842
1843/*
1844 * Parse the 'completepopup' option and apply the values to window "wp" if it
1845 * is not NULL.
1846 * Return FAIL if the parsing fails.
1847 */
1848 int
1849parse_completepopup(win_T *wp)
1850{
1851 return parse_popup_option(wp, FALSE);
1852}
1853
1854/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001855 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001856 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001857 */
1858 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001859popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001860{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001861 poppos_T ppt = POPPOS_NONE;
1862
1863 if (d != NULL)
1864 ppt = get_pos_entry(d, FALSE);
1865
Bram Moolenaar79648732019-07-18 21:43:07 +02001866 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001867 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001868 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001869 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1870 }
1871 else
1872 {
1873 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1874 if (wp->w_wantline == 0) // cursor in first line
1875 {
1876 wp->w_wantline = 2;
1877 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1878 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1879 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001880 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001881
Bram Moolenaar79648732019-07-18 21:43:07 +02001882 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001883 if (wp->w_wantcol > Columns - width)
1884 {
1885 wp->w_wantcol = Columns - width;
1886 if (wp->w_wantcol < 1)
1887 wp->w_wantcol = 1;
1888 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001889
Bram Moolenaar79648732019-07-18 21:43:07 +02001890 popup_adjust_position(wp);
1891}
1892
1893/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001894 * Set w_wantline and w_wantcol for the a given screen position.
1895 * Caller must take care of running into the window border.
1896 */
1897 void
1898popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1899{
1900 wp->w_wantline = row;
1901 wp->w_wantcol = col;
1902 popup_adjust_position(wp);
1903}
1904
1905/*
1906 * Add a border and lef&right padding.
1907 */
1908 static void
1909add_border_left_right_padding(win_T *wp)
1910{
1911 int i;
1912
1913 for (i = 0; i < 4; ++i)
1914 {
1915 wp->w_popup_border[i] = 1;
1916 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1917 }
1918}
1919
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001920#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001921/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001922 * Return TRUE if there is any popup window with a terminal buffer.
1923 */
1924 static int
1925popup_terminal_exists(void)
1926{
1927 win_T *wp;
1928 tabpage_T *tp;
1929
1930 FOR_ALL_POPUPWINS(wp)
1931 if (wp->w_buffer->b_term != NULL)
1932 return TRUE;
1933 FOR_ALL_TABPAGES(tp)
1934 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1935 if (wp->w_buffer->b_term != NULL)
1936 return TRUE;
1937 return FALSE;
1938}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001939#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001940
1941/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001942 * Mark all popup windows in the current tab and global for redrawing.
1943 */
1944 void
1945popup_redraw_all(void)
1946{
1947 win_T *wp;
1948
1949 FOR_ALL_POPUPWINS(wp)
1950 wp->w_redr_type = UPD_NOT_VALID;
1951 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1952 wp->w_redr_type = UPD_NOT_VALID;
1953}
1954
1955/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001956 * Set the color for a notification window.
1957 */
1958 static void
1959popup_update_color(win_T *wp, create_type_T type)
1960{
1961 char *hiname = type == TYPE_MESSAGE_WIN
1962 ? "MessageWindow" : "PopupNotification";
1963 int nr = syn_name2id((char_u *)hiname);
1964
1965 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1966 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1967 OPT_FREE|OPT_LOCAL, 0);
1968}
1969
1970/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001971 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001972 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001973 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001974 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001975 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001976 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001977popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001978{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001979 win_T *wp;
1980 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001981 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001982 int new_buffer;
1983 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001984 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001985 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001986
Bram Moolenaar79648732019-07-18 21:43:07 +02001987 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001988 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001989 if (in_vim9script()
1990 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1991 || check_for_dict_arg(argvars, 1) == FAIL))
1992 return NULL;
1993
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001994 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001995 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001996 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001997 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001998 if (buf == NULL)
1999 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002000 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002001 return NULL;
2002 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002003#ifdef FEAT_TERMINAL
2004 if (buf->b_term != NULL && popup_terminal_exists())
2005 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002006 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002007 return NULL;
2008 }
2009#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002010 }
2011 else if (!(argvars[0].v_type == VAR_STRING
2012 && argvars[0].vval.v_string != NULL)
2013 && !(argvars[0].v_type == VAR_LIST
2014 && argvars[0].vval.v_list != NULL))
2015 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002016 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002017 return NULL;
2018 }
Bram Moolenaar79648732019-07-18 21:43:07 +02002019 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002020 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002021 emsg(_(e_dictionary_required));
Bram Moolenaara3fce622019-06-20 02:31:49 +02002022 return NULL;
2023 }
Bram Moolenaar79648732019-07-18 21:43:07 +02002024 d = argvars[1].vval.v_dict;
2025 }
2026
2027 if (d != NULL)
2028 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002029 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002030 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002031 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002032 tabnr = -1; // notifications are global by default
2033 else
2034 tabnr = 0;
2035 if (tabnr > 0)
2036 {
2037 tp = find_tabpage(tabnr);
2038 if (tp == NULL)
2039 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002040 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002041 return NULL;
2042 }
2043 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002044 }
2045
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002046 // Create the window and buffer.
2047 wp = win_alloc_popup_win();
2048 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002049 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002050 if (rettv != NULL)
2051 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002052 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002053 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002054
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002055 if (buf != NULL)
2056 {
2057 // use existing buffer
2058 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002059 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002060 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002061 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002062 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002063 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002064 }
2065 else
2066 {
2067 // create a new buffer associated with the popup
2068 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002069 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002070 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002071 {
2072 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002073 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002074 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002075 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002076
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002077 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002078
Bram Moolenaar46451042019-08-24 15:50:46 +02002079 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002080 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002081 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002082 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002083 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002084 buf->b_p_ul = -1; // no undo
2085 buf->b_p_swf = FALSE; // no swap file
2086 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002087 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002088
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002089 // Avoid that 'buftype' is reset when this buffer is entered.
2090 buf->b_p_initialized = TRUE;
2091 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002092 wp->w_p_wrap = TRUE; // 'wrap' is default on
2093 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002094
Bram Moolenaara3fce622019-06-20 02:31:49 +02002095 if (tp != NULL)
2096 {
2097 // popup on specified tab page
2098 wp->w_next = tp->tp_first_popupwin;
2099 tp->tp_first_popupwin = wp;
2100 }
2101 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002102 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002103 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002104 wp->w_next = curtab->tp_first_popupwin;
2105 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002106 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002107 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002108 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002109 win_T *prev = first_popupwin;
2110
2111 // Global popup: add at the end, so that it gets displayed on top of
2112 // older ones with the same zindex. Matters for notifications.
2113 if (first_popupwin == NULL)
2114 first_popupwin = wp;
2115 else
2116 {
2117 while (prev->w_next != NULL)
2118 prev = prev->w_next;
2119 prev->w_next = wp;
2120 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002121 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002122
Bram Moolenaar79648732019-07-18 21:43:07 +02002123 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002124 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002125
Bram Moolenaar79648732019-07-18 21:43:07 +02002126 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002127 {
2128 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002129 }
2130 if (type == TYPE_ATCURSOR)
2131 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002132 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002133 set_moved_values(wp);
2134 set_moved_columns(wp, FIND_STRING);
2135 }
2136
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002137 if (type == TYPE_BEVAL)
2138 {
2139 wp->w_popup_pos = POPPOS_BOTLEFT;
2140
2141 // by default use the mouse position
2142 wp->w_wantline = mouse_row;
2143 if (wp->w_wantline <= 0) // mouse on first line
2144 {
2145 wp->w_wantline = 2;
2146 wp->w_popup_pos = POPPOS_TOPLEFT;
2147 }
2148 wp->w_wantcol = mouse_col + 1;
2149 set_mousemoved_values(wp);
2150 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2151 }
2152
Bram Moolenaar33796b32019-06-08 16:01:13 +02002153 // set default values
2154 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002155 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002156
Bram Moolenaar9198de32022-08-27 21:30:03 +01002157 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002158 {
2159 win_T *twp, *nextwin;
2160 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002161
2162 // Try to not overlap with another global popup. Guess we need 3
2163 // more screen lines than buffer lines.
2164 wp->w_wantline = 1;
2165 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2166 {
2167 nextwin = twp->w_next;
2168 if (twp != wp
2169 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2170 && twp->w_winrow <= wp->w_wantline - 1 + height
2171 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2172 {
2173 // move to below this popup and restart the loop to check for
2174 // overlap with other popups
2175 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2176 nextwin = first_popupwin;
2177 }
2178 }
2179 if (wp->w_wantline + height > Rows)
2180 {
2181 // can't avoid overlap, put on top in the hope that message goes
2182 // away soon.
2183 wp->w_wantline = 1;
2184 }
2185
2186 wp->w_wantcol = 10;
2187 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002188 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002189 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002190 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002191 for (i = 0; i < 4; ++i)
2192 wp->w_popup_border[i] = 1;
2193 wp->w_popup_padding[1] = 1;
2194 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002195
Bram Moolenaar9198de32022-08-27 21:30:03 +01002196 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002197 }
2198
Bram Moolenaara730e552019-06-16 19:05:31 +02002199 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002200 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002201 wp->w_popup_pos = POPPOS_CENTER;
2202 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002203 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002204 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002205 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002206 }
2207
Bram Moolenaara730e552019-06-16 19:05:31 +02002208 if (type == TYPE_MENU)
2209 {
2210 typval_T tv;
2211 callback_T callback;
2212
2213 tv.v_type = VAR_STRING;
2214 tv.vval.v_string = (char_u *)"popup_filter_menu";
2215 callback = get_callback(&tv);
2216 if (callback.cb_name != NULL)
2217 set_callback(&wp->w_filter_cb, &callback);
2218
2219 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002220 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002221 }
2222
Bram Moolenaar79648732019-07-18 21:43:07 +02002223 if (type == TYPE_PREVIEW)
2224 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002225 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002226 wp->w_popup_close = POPCLOSE_BUTTON;
2227 for (i = 0; i < 4; ++i)
2228 wp->w_popup_border[i] = 1;
2229 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002230 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002231 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002232# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002233 if (type == TYPE_INFO)
2234 {
2235 wp->w_popup_pos = POPPOS_TOPLEFT;
2236 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2237 wp->w_popup_close = POPCLOSE_BUTTON;
2238 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002239 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002240 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002241# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002242
Bram Moolenaarae943152019-06-16 22:54:14 +02002243 for (i = 0; i < 4; ++i)
2244 VIM_CLEAR(wp->w_border_highlight[i]);
2245 for (i = 0; i < 8; ++i)
2246 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002247 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002248 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002249 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002250
Bram Moolenaar79648732019-07-18 21:43:07 +02002251 if (d != NULL)
2252 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002253 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002254
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002255#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002256 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2257 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002258#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002259
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002260 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002261
2262 wp->w_vsep_width = 0;
2263
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002264 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002265 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002266
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002267#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002268 // When running a terminal in the popup it becomes the current window.
2269 if (buf->b_term != NULL)
2270 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002271#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002272
Bram Moolenaara730e552019-06-16 19:05:31 +02002273 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002274}
2275
2276/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002277 * popup_clear()
2278 */
2279 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002280f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002281{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002282 int force = FALSE;
2283
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002284 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2285 return;
2286
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002287 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002288 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002289 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002290}
2291
2292/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002293 * popup_create({text}, {options})
2294 */
2295 void
2296f_popup_create(typval_T *argvars, typval_T *rettv)
2297{
Bram Moolenaar17627312019-06-02 19:53:44 +02002298 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002299}
2300
2301/*
2302 * popup_atcursor({text}, {options})
2303 */
2304 void
2305f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2306{
Bram Moolenaar17627312019-06-02 19:53:44 +02002307 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002308}
2309
2310/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002311 * popup_beval({text}, {options})
2312 */
2313 void
2314f_popup_beval(typval_T *argvars, typval_T *rettv)
2315{
2316 popup_create(argvars, rettv, TYPE_BEVAL);
2317}
2318
2319/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002320 * Invoke the close callback for window "wp" with value "result".
2321 * Careful: The callback may make "wp" invalid!
2322 */
2323 static void
2324invoke_popup_callback(win_T *wp, typval_T *result)
2325{
2326 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002327 typval_T argv[3];
2328
2329 argv[0].v_type = VAR_NUMBER;
2330 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2331
2332 if (result != NULL && result->v_type != VAR_UNKNOWN)
2333 copy_tv(result, &argv[1]);
2334 else
2335 {
2336 argv[1].v_type = VAR_NUMBER;
2337 argv[1].vval.v_number = 0;
2338 }
2339
2340 argv[2].v_type = VAR_UNKNOWN;
2341
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002342 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002343 if (result != NULL)
2344 clear_tv(&argv[1]);
2345 clear_tv(&rettv);
2346}
2347
2348/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002349 * Make "prevwin" the current window, unless it's equal to "wp".
2350 * Otherwise make "firstwin" the current window.
2351 */
2352 static void
2353back_to_prevwin(win_T *wp)
2354{
2355 if (win_valid(prevwin) && wp != prevwin)
2356 win_enter(prevwin, FALSE);
2357 else
2358 win_enter(firstwin, FALSE);
2359}
2360
2361/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002362 * Close popup "wp" and invoke any close callback for it.
2363 */
2364 static void
2365popup_close_and_callback(win_T *wp, typval_T *arg)
2366{
2367 int id = wp->w_id;
2368
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002369#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002370 if (wp == curwin && curbuf->b_term != NULL)
2371 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002372 win_T *owp;
2373
2374 // Closing popup window with a terminal: put focus back on the first
2375 // that works:
2376 // - another popup window with a terminal
2377 // - the previous window
2378 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002379 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002380 if (owp != curwin && owp->w_buffer->b_term != NULL)
2381 break;
2382 if (owp != NULL)
2383 win_enter(owp, FALSE);
2384 else
2385 {
2386 for (owp = curtab->tp_first_popupwin; owp != NULL;
2387 owp = owp->w_next)
2388 if (owp != curwin && owp->w_buffer->b_term != NULL)
2389 break;
2390 if (owp != NULL)
2391 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002392 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002393 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002394 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002395 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002396#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002397
Bram Moolenaar49540192019-12-11 19:34:54 +01002398 // Just in case a check higher up is missing.
2399 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002400 {
2401 // To avoid getting stuck when win_execute() does something that causes
2402 // an error, stop calling the filter callback.
2403 free_callback(&wp->w_filter_cb);
2404
Bram Moolenaar49540192019-12-11 19:34:54 +01002405 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002406 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002407
Bram Moolenaarcee52202020-03-11 14:19:58 +01002408 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002409 if (wp->w_close_cb.cb_name != NULL)
2410 // Careful: This may make "wp" invalid.
2411 invoke_popup_callback(wp, arg);
2412
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002413 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002414 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002415}
2416
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002417 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002418popup_close_with_retval(win_T *wp, int retval)
2419{
2420 typval_T res;
2421
2422 res.v_type = VAR_NUMBER;
2423 res.vval.v_number = retval;
2424 popup_close_and_callback(wp, &res);
2425}
2426
Bram Moolenaar3397f742019-06-02 18:40:06 +02002427/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002428 * Close popup "wp" because of a mouse click.
2429 */
2430 void
2431popup_close_for_mouse_click(win_T *wp)
2432{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002433 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002434}
2435
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002436 static void
2437check_mouse_moved(win_T *wp, win_T *mouse_wp)
2438{
2439 // Close the popup when all if these are true:
2440 // - the mouse is not on this popup
2441 // - "mousemoved" was used
2442 // - the mouse is no longer on the same screen row or the mouse column is
2443 // outside of the relevant text
2444 if (wp != mouse_wp
2445 && wp->w_popup_mouse_row != 0
2446 && (wp->w_popup_mouse_row != mouse_row
2447 || mouse_col < wp->w_popup_mouse_mincol
2448 || mouse_col > wp->w_popup_mouse_maxcol))
2449 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002450 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002451 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002452 }
2453}
2454
2455/*
2456 * Called when the mouse moved: may close a popup with "mousemoved".
2457 */
2458 void
2459popup_handle_mouse_moved(void)
2460{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002461 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002462 win_T *mouse_wp;
2463 int row = mouse_row;
2464 int col = mouse_col;
2465
2466 // find the window where the mouse is in
2467 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2468
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002469 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2470 {
2471 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002472 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002473 }
2474 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2475 {
2476 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002477 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002478 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002479}
2480
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002481/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002482 * In a filter: check if the typed key is a mouse event that is used for
2483 * dragging the popup.
2484 */
2485 static void
2486filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2487{
2488 int row = mouse_row;
2489 int col = mouse_col;
2490
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002491 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002492 && is_mouse_key(c)
2493 && (wp == popup_dragwin
2494 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2495 // do not consume the key, allow for dragging the popup
2496 rettv->vval.v_number = 0;
2497}
2498
Bram Moolenaara730e552019-06-16 19:05:31 +02002499/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002500 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002501 */
2502 void
2503f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2504{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002505 int id;
2506 win_T *wp;
2507 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002508 typval_T res;
2509 int c;
2510 linenr_T old_lnum;
2511
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002512 if (in_vim9script()
2513 && (check_for_number_arg(argvars, 0) == FAIL
2514 || check_for_string_arg(argvars, 1) == FAIL))
2515 return;
2516
2517 id = tv_get_number(&argvars[0]);
2518 wp = win_id2wp(id);
2519 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002520 // If the popup has been closed do not consume the key.
2521 if (wp == NULL)
2522 return;
2523
2524 c = *key;
2525 if (c == K_SPECIAL && key[1] != NUL)
2526 c = TO_SPECIAL(key[1], key[2]);
2527
2528 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002529 rettv->v_type = VAR_BOOL;
2530 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002531 res.v_type = VAR_NUMBER;
2532
2533 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002534 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2535 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002536 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002537 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002538 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2539 ++wp->w_cursor.lnum;
2540 if (old_lnum != wp->w_cursor.lnum)
2541 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002542 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002543 return;
2544 }
2545
2546 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2547 {
2548 // Cancelled, invoke callback with -1
2549 res.vval.v_number = -1;
2550 popup_close_and_callback(wp, &res);
2551 return;
2552 }
2553 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2554 {
2555 // Invoke callback with current index.
2556 res.vval.v_number = wp->w_cursor.lnum;
2557 popup_close_and_callback(wp, &res);
2558 return;
2559 }
2560
2561 filter_handle_drag(wp, c, rettv);
2562}
2563
2564/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002565 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002566 */
2567 void
2568f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2569{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002570 int id;
2571 win_T *wp;
2572 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002573 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002574 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002575
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002576 if (in_vim9script()
2577 && (check_for_number_arg(argvars, 0) == FAIL
2578 || check_for_string_arg(argvars, 1) == FAIL))
2579 return;
2580
2581 id = tv_get_number(&argvars[0]);
2582 wp = win_id2wp(id);
2583 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002584 // If the popup has been closed don't consume the key.
2585 if (wp == NULL)
2586 return;
2587
Bram Moolenaara730e552019-06-16 19:05:31 +02002588 c = *key;
2589 if (c == K_SPECIAL && key[1] != NUL)
2590 c = TO_SPECIAL(key[1], key[2]);
2591
Bram Moolenaara42d9452019-06-15 21:46:30 +02002592 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002593 rettv->v_type = VAR_BOOL;
2594 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002595
Bram Moolenaara730e552019-06-16 19:05:31 +02002596 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002597 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002598 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002599 res.vval.v_number = 0;
2600 else
2601 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002602 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002603 return;
2604 }
2605
2606 // Invoke callback
2607 res.v_type = VAR_NUMBER;
2608 popup_close_and_callback(wp, &res);
2609}
2610
2611/*
2612 * popup_dialog({text}, {options})
2613 */
2614 void
2615f_popup_dialog(typval_T *argvars, typval_T *rettv)
2616{
2617 popup_create(argvars, rettv, TYPE_DIALOG);
2618}
2619
2620/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002621 * popup_menu({text}, {options})
2622 */
2623 void
2624f_popup_menu(typval_T *argvars, typval_T *rettv)
2625{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002626 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002627}
2628
2629/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002630 * popup_notification({text}, {options})
2631 */
2632 void
2633f_popup_notification(typval_T *argvars, typval_T *rettv)
2634{
2635 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2636}
2637
2638/*
2639 * Find the popup window with window-ID "id".
2640 * If the popup window does not exist NULL is returned.
2641 * If the window is not a popup window, and error message is given.
2642 */
2643 static win_T *
2644find_popup_win(int id)
2645{
2646 win_T *wp = win_id2wp(id);
2647
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002648 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002649 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002650 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002651 return NULL;
2652 }
2653 return wp;
2654}
2655
2656/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002657 * popup_close({id})
2658 */
2659 void
2660f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2661{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002662 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002663 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002664
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002665 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2666 return;
2667
2668 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002669 if (
2670# ifdef FEAT_TERMINAL
2671 // if the popup contains a terminal it will become hidden
2672 curbuf->b_term == NULL &&
2673# endif
2674 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002675 return;
2676
2677 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002678 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002679 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002680}
2681
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002682 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002683popup_hide(win_T *wp)
2684{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002685#ifdef FEAT_TERMINAL
2686 if (error_if_term_popup_window())
2687 return;
2688#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002689 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2690 {
2691 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002692 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002693 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002694 popup_mask_refresh = TRUE;
2695 }
2696}
2697
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002698/*
2699 * popup_hide({id})
2700 */
2701 void
2702f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2703{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002704 int id;
2705 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002706
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002707 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2708 return;
2709
2710 id = (int)tv_get_number(argvars);
2711 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002712 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002713 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002714 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002715 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2716 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002717}
2718
2719 void
2720popup_show(win_T *wp)
2721{
2722 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002723 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002724 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002725 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002726 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002727 }
2728}
2729
2730/*
2731 * popup_show({id})
2732 */
2733 void
2734f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2735{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002736 int id;
2737 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002738
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002739 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2740 return;
2741
2742 id = (int)tv_get_number(argvars);
2743 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002744 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002745 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002746 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002747 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002748#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002749 if (wp->w_popup_flags & POPF_INFO)
2750 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002751#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002752 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002753}
2754
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002755/*
2756 * popup_settext({id}, {text})
2757 */
2758 void
2759f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2760{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002761 int id;
2762 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002763
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002764 if (in_vim9script()
2765 && (check_for_number_arg(argvars, 0) == FAIL
2766 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2767 return;
2768
2769 id = (int)tv_get_number(&argvars[0]);
2770 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002771 if (wp != NULL)
2772 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002773 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002774 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002775 else
2776 {
2777 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002778 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002779 popup_adjust_position(wp);
2780 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002781 }
2782}
2783
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002784 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002785popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002786{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002787 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002788 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002789 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002790 clear_cmdline = TRUE;
2791 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002792
Bram Moolenaar43568642022-08-28 13:02:45 +01002793#ifdef HAS_MESSAGE_WINDOW
2794 if (wp == message_win)
2795 message_win = NULL;
2796#endif
2797
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002798 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002799 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002800}
2801
Bram Moolenaar82106932020-03-13 14:34:38 +01002802 static void
2803error_for_popup_window(void)
2804{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002805 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002806}
2807
2808 int
2809error_if_popup_window(int also_with_term UNUSED)
2810{
2811 // win_execute() may set "curwin" to a popup window temporarily, but many
2812 // commands are disallowed then. When a terminal runs in the popup most
2813 // things are allowed. When a terminal is finished it can be closed.
2814 if (WIN_IS_POPUP(curwin)
2815# ifdef FEAT_TERMINAL
2816 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002817# endif
2818 )
2819 {
2820 error_for_popup_window();
2821 return TRUE;
2822 }
2823 return FALSE;
2824}
2825
Bram Moolenaarec583842019-05-26 14:11:23 +02002826/*
2827 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002828 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002829 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002830 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002831 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002832popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002833{
2834 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002835 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002836 win_T *prev = NULL;
2837
Bram Moolenaarec583842019-05-26 14:11:23 +02002838 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002839 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002840 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002841 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002842 if (wp == curwin)
2843 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002844 if (!force)
2845 {
2846 error_for_popup_window();
2847 return FAIL;
2848 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002849 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002850 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002851 if (prev == NULL)
2852 first_popupwin = wp->w_next;
2853 else
2854 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002855 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002856 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002857 }
2858
Bram Moolenaarec583842019-05-26 14:11:23 +02002859 // go through tab-local popups
2860 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002861 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002862 return OK;
2863 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002864}
2865
2866/*
2867 * Close a popup window with Window-id "id" in tabpage "tp".
2868 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002869 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002870popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002871{
2872 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002873 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002874 win_T *prev = NULL;
2875
Bram Moolenaarec583842019-05-26 14:11:23 +02002876 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2877 if (wp->w_id == id)
2878 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002879 if (wp == curwin)
2880 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002881 if (!force)
2882 {
2883 error_for_popup_window();
2884 return FAIL;
2885 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002886 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002887 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002888 if (prev == NULL)
2889 *root = wp->w_next;
2890 else
2891 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002892 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002893 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002894 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002895 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002896}
2897
2898 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002899close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002900{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002901 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002902 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002903 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002904 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002905 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002906 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002907 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002908 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002909}
2910
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002911/*
2912 * popup_move({id}, {options})
2913 */
2914 void
2915f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2916{
Bram Moolenaarae943152019-06-16 22:54:14 +02002917 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002918 int id;
2919 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002920
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002921 if (in_vim9script()
2922 && (check_for_number_arg(argvars, 0) == FAIL
2923 || check_for_dict_arg(argvars, 1) == FAIL))
2924 return;
2925
2926 id = (int)tv_get_number(argvars);
2927 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002928 if (wp == NULL)
2929 return; // invalid {id}
2930
2931 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2932 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002933 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002934 return;
2935 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002936 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002937
Bram Moolenaarae943152019-06-16 22:54:14 +02002938 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002939
2940 if (wp->w_winrow + wp->w_height >= cmdline_row)
2941 clear_cmdline = TRUE;
2942 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002943}
2944
Bram Moolenaarbc133542019-05-29 20:26:46 +02002945/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002946 * popup_setoptions({id}, {options})
2947 */
2948 void
2949f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2950{
2951 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002952 int id;
2953 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002954 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002955
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002956 if (in_vim9script()
2957 && (check_for_number_arg(argvars, 0) == FAIL
2958 || check_for_dict_arg(argvars, 1) == FAIL))
2959 return;
2960
2961 id = (int)tv_get_number(argvars);
2962 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002963 if (wp == NULL)
2964 return; // invalid {id}
2965
2966 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2967 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002968 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002969 return;
2970 }
2971 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002972 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002973
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002974 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002975
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002976 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002977 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002978 popup_adjust_position(wp);
2979}
2980
2981/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002982 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002983 */
2984 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002985f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002986{
2987 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002988 int id;
2989 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002990 int top_extra;
2991 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002992
2993 if (rettv_dict_alloc(rettv) == OK)
2994 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002995 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2996 return;
2997
2998 id = (int)tv_get_number(argvars);
2999 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02003000 if (wp == NULL)
3001 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003002 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003003 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
3004
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003005 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02003006 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003007 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003008
Bram Moolenaarbc133542019-05-29 20:26:46 +02003009 dict_add_number(dict, "line", wp->w_winrow + 1);
3010 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02003011 dict_add_number(dict, "width", wp->w_width + left_extra
3012 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3013 dict_add_number(dict, "height", wp->w_height + top_extra
3014 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003015
3016 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3017 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3018 dict_add_number(dict, "core_width", wp->w_width);
3019 dict_add_number(dict, "core_height", wp->w_height);
3020
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003021 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02003022 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01003023 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003024 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003025 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003026
3027 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003028 }
3029}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003030
3031/*
3032 * popup_list()
3033 */
3034 void
3035f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3036{
3037 win_T *wp;
3038 tabpage_T *tp;
3039
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003040 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003041 return;
3042 FOR_ALL_POPUPWINS(wp)
3043 list_append_number(rettv->vval.v_list, wp->w_id);
3044 FOR_ALL_TABPAGES(tp)
3045 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3046 list_append_number(rettv->vval.v_list, wp->w_id);
3047}
3048
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003049/*
3050 * popup_locate({row}, {col})
3051 */
3052 void
3053f_popup_locate(typval_T *argvars, typval_T *rettv)
3054{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003055 int row;
3056 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003057 win_T *wp;
3058
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003059 if (in_vim9script()
3060 && (check_for_number_arg(argvars, 0) == FAIL
3061 || check_for_number_arg(argvars, 1) == FAIL))
3062 return;
3063
3064 row = tv_get_number(&argvars[0]) - 1;
3065 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003066 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003067 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003068 rettv->vval.v_number = wp->w_id;
3069}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003070
3071/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003072 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3073 */
3074 static void
3075get_padding_border(dict_T *dict, int *array, char *name)
3076{
3077 list_T *list;
3078 int i;
3079
3080 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3081 return;
3082
3083 list = list_alloc();
3084 if (list != NULL)
3085 {
3086 dict_add_list(dict, name, list);
3087 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3088 for (i = 0; i < 4; ++i)
3089 list_append_number(list, array[i]);
3090 }
3091}
3092
3093/*
3094 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3095 */
3096 static void
3097get_borderhighlight(dict_T *dict, win_T *wp)
3098{
3099 list_T *list;
3100 int i;
3101
3102 for (i = 0; i < 4; ++i)
3103 if (wp->w_border_highlight[i] != NULL)
3104 break;
3105 if (i == 4)
3106 return;
3107
3108 list = list_alloc();
3109 if (list != NULL)
3110 {
3111 dict_add_list(dict, "borderhighlight", list);
3112 for (i = 0; i < 4; ++i)
3113 list_append_string(list, wp->w_border_highlight[i], -1);
3114 }
3115}
3116
3117/*
3118 * For popup_getoptions(): add a "borderchars" entry to "dict".
3119 */
3120 static void
3121get_borderchars(dict_T *dict, win_T *wp)
3122{
3123 list_T *list;
3124 int i;
3125 char_u buf[NUMBUFLEN];
3126 int len;
3127
3128 for (i = 0; i < 8; ++i)
3129 if (wp->w_border_char[i] != 0)
3130 break;
3131 if (i == 8)
3132 return;
3133
3134 list = list_alloc();
3135 if (list != NULL)
3136 {
3137 dict_add_list(dict, "borderchars", list);
3138 for (i = 0; i < 8; ++i)
3139 {
3140 len = mb_char2bytes(wp->w_border_char[i], buf);
3141 list_append_string(list, buf, len);
3142 }
3143 }
3144}
3145
3146/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003147 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003148 */
3149 static void
3150get_moved_list(dict_T *dict, win_T *wp)
3151{
3152 list_T *list;
3153
3154 list = list_alloc();
3155 if (list != NULL)
3156 {
3157 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003158 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003159 list_append_number(list, wp->w_popup_mincol);
3160 list_append_number(list, wp->w_popup_maxcol);
3161 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003162 list = list_alloc();
3163 if (list != NULL)
3164 {
3165 dict_add_list(dict, "mousemoved", list);
3166 list_append_number(list, wp->w_popup_mouse_row);
3167 list_append_number(list, wp->w_popup_mouse_mincol);
3168 list_append_number(list, wp->w_popup_mouse_maxcol);
3169 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003170}
3171
3172/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003173 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003174 */
3175 void
3176f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3177{
3178 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003179 int id;
3180 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003181 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003182 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003183
3184 if (rettv_dict_alloc(rettv) == OK)
3185 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003186 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3187 return;
3188
3189 id = (int)tv_get_number(argvars);
3190 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003191 if (wp == NULL)
3192 return;
3193
3194 dict = rettv->vval.v_dict;
3195 dict_add_number(dict, "line", wp->w_wantline);
3196 dict_add_number(dict, "col", wp->w_wantcol);
3197 dict_add_number(dict, "minwidth", wp->w_minwidth);
3198 dict_add_number(dict, "minheight", wp->w_minheight);
3199 dict_add_number(dict, "maxheight", wp->w_maxheight);
3200 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003201 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003202 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003203 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003204 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003205 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003206 {
3207 proptype_T *pt = text_prop_type_by_id(
3208 wp->w_popup_prop_win->w_buffer,
3209 wp->w_popup_prop_type);
3210
3211 if (pt != NULL)
3212 dict_add_string(dict, "textprop", pt->pt_name);
3213 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3214 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3215 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003216 dict_add_string(dict, "title", wp->w_popup_title);
3217 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003218 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003219 dict_add_number(dict, "dragall",
3220 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003221 dict_add_number(dict, "mapping",
3222 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003223 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003224 dict_add_number(dict, "posinvert",
3225 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003226 dict_add_number(dict, "cursorline",
3227 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003228 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003229 if (wp->w_scrollbar_highlight != NULL)
3230 dict_add_string(dict, "scrollbarhighlight",
3231 wp->w_scrollbar_highlight);
3232 if (wp->w_thumb_highlight != NULL)
3233 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003234
Bram Moolenaara3fce622019-06-20 02:31:49 +02003235 // find the tabpage that holds this popup
3236 i = 1;
3237 FOR_ALL_TABPAGES(tp)
3238 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003239 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003240
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003241 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3242 if (twp->w_id == id)
3243 break;
3244 if (twp != NULL)
3245 break;
3246 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003247 }
3248 if (tp == NULL)
3249 i = -1; // must be global
3250 else if (tp == curtab)
3251 i = 0;
3252 dict_add_number(dict, "tabpage", i);
3253
Bram Moolenaarae943152019-06-16 22:54:14 +02003254 get_padding_border(dict, wp->w_popup_padding, "padding");
3255 get_padding_border(dict, wp->w_popup_border, "border");
3256 get_borderhighlight(dict, wp);
3257 get_borderchars(dict, wp);
3258 get_moved_list(dict, wp);
3259
3260 if (wp->w_filter_cb.cb_name != NULL)
3261 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3262 if (wp->w_close_cb.cb_name != NULL)
3263 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003264
K.Takataeeec2542021-06-02 13:28:16 +02003265 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003266 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3267 {
3268 dict_add_string(dict, "pos",
3269 (char_u *)poppos_entries[i].pp_name);
3270 break;
3271 }
3272
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003273 dict_add_string(dict, "close", (char_u *)(
3274 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3275 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3276
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003277# if defined(FEAT_TIMERS)
3278 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3279 ? (long)wp->w_popup_timer->tr_interval : 0L);
3280# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003281 }
3282}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003283
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003284# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003285/*
3286 * Return TRUE if the current window is running a terminal in a popup window.
3287 * Return FALSE when the job has ended.
3288 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003289 int
3290error_if_term_popup_window()
3291{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003292 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3293 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003294 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003295 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003296 return TRUE;
3297 }
3298 return FALSE;
3299}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003300# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003301
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003302/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003303 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003304 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003305 * Each calling function should use a different flag, see the list at
3306 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003307 */
3308 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003309popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003310{
3311 win_T *wp;
3312
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003313 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003314 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003315 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003316 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003317}
3318
3319/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003320 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003321 * Must have called popup_reset_handled() first.
3322 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3323 * popup with the highest zindex.
3324 */
3325 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003326find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003327{
3328 win_T *wp;
3329 win_T *found_wp;
3330 int found_zindex;
3331
3332 found_zindex = lowest ? INT_MAX : 0;
3333 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003334 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003335 if ((wp->w_popup_handled & handled_flag) == 0
3336 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003337 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003338 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003339 {
3340 found_zindex = wp->w_zindex;
3341 found_wp = wp;
3342 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003343 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003344 if ((wp->w_popup_handled & handled_flag) == 0
3345 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003346 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003347 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003348 {
3349 found_zindex = wp->w_zindex;
3350 found_wp = wp;
3351 }
3352
3353 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003354 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003355 return found_wp;
3356}
3357
3358/*
3359 * Invoke the filter callback for window "wp" with typed character "c".
3360 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003361 * Returns the return value of the filter or -1 for CTRL-C in the current
3362 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003363 * Careful: The filter may make "wp" invalid!
3364 */
3365 static int
3366invoke_popup_filter(win_T *wp, int c)
3367{
3368 int res;
3369 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003370 typval_T argv[3];
3371 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003372 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003373 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003374
Bram Moolenaara42d9452019-06-15 21:46:30 +02003375 // Emergency exit: CTRL-C closes the popup.
3376 if (c == Ctrl_C)
3377 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003378 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003379 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003380
3381 // Reset got_int to avoid the callback isn't called.
3382 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003383 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003384 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003385
3386 // If the popup is the current window it probably fails to close. Then
3387 // do not consume the key.
3388 if (was_curwin && wp == curwin)
3389 return -1;
3390 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003391 }
3392
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003393 argv[0].v_type = VAR_NUMBER;
3394 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3395
3396 // Convert the number to a string, so that the function can use:
3397 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003398 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003399 argv[1].v_type = VAR_STRING;
3400 argv[1].vval.v_string = vim_strsave(buf);
3401
3402 argv[2].v_type = VAR_UNKNOWN;
3403
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003404 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003405 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3406 {
3407 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003408 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003409 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003410 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003411 }
3412 else
3413 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003414 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3415 popup_highlight_curline(wp);
3416
Bram Moolenaar371806e2020-10-22 13:44:54 +02003417 // If an error message was given always return FALSE, so that keys are
3418 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003419 // If we get three errors in a row then close the popup. Decrement the
3420 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3421 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003422 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003423 {
3424 wp->w_filter_errors += 10;
3425 if (wp->w_filter_errors >= 30)
3426 popup_close_with_retval(wp, -1);
3427 res = FALSE;
3428 }
3429 else
3430 {
3431 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3432 --wp->w_filter_errors;
3433 res = tv_get_bool(&rettv);
3434 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003435 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003436
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003437 vim_free(argv[1].vval.v_string);
3438 clear_tv(&rettv);
3439 return res;
3440}
3441
3442/*
3443 * Called when "c" was typed: invoke popup filter callbacks.
3444 * Returns TRUE when the character was consumed,
3445 */
3446 int
3447popup_do_filter(int c)
3448{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003449 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003450 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003451 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003452 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003453 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003454 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003455
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003456#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003457 // Popup window with terminal always gets focus.
3458 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3459 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003460#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003461
Bram Moolenaar934470e2019-09-01 23:27:05 +02003462 if (recursive)
3463 return FALSE;
3464 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003465
Bram Moolenaarf6396232019-08-24 19:36:00 +02003466 if (c == K_LEFTMOUSE)
3467 {
3468 int row = mouse_row;
3469 int col = mouse_col;
3470
3471 wp = mouse_find_win(&row, &col, FIND_POPUP);
3472 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003473 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003474 }
3475
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003476 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003477 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003478 while (res == FALSE
3479 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003480 if (wp->w_filter_cb.cb_name != NULL
3481 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003482 res = invoke_popup_filter(wp, c);
3483
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003484 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003485 {
3486 int save_got_int = got_int;
3487
3488 // Reset got_int to avoid a function used in the statusline aborts.
3489 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003490 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003491 got_int |= save_got_int;
3492 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003493 recursive = FALSE;
3494 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003495
3496 // When interrupted return FALSE to avoid looping.
3497 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003498}
3499
Bram Moolenaar3397f742019-06-02 18:40:06 +02003500/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003501 * Return TRUE if there is a popup visible with a filter callback and the
3502 * "mapping" property off.
3503 */
3504 int
3505popup_no_mapping(void)
3506{
3507 int round;
3508 win_T *wp;
3509
3510 for (round = 1; round <= 2; ++round)
3511 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3512 wp != NULL; wp = wp->w_next)
3513 if (wp->w_filter_cb.cb_name != NULL
3514 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3515 return TRUE;
3516 return FALSE;
3517}
3518
3519/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003520 * Called when the cursor moved: check if any popup needs to be closed if the
3521 * cursor moved far enough.
3522 */
3523 void
3524popup_check_cursor_pos()
3525{
3526 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003527
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003528 popup_reset_handled(POPUP_HANDLED_3);
3529 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003530 if (wp->w_popup_curwin != NULL
3531 && (curwin != wp->w_popup_curwin
3532 || curwin->w_cursor.lnum != wp->w_popup_lnum
3533 || curwin->w_cursor.col < wp->w_popup_mincol
3534 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003535 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003536}
3537
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003538/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003539 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003540 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003541 static void
3542popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003543{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003544 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003545 char_u *cells;
3546 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003547
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003548 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3549 {
3550 vim_free(wp->w_popup_mask_cells);
3551 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003552 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003553 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003554 if (wp->w_popup_mask_cells != NULL
3555 && wp->w_popup_mask_height == height
3556 && wp->w_popup_mask_width == width)
3557 return; // cache is still valid
3558
3559 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003560 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003561 if (wp->w_popup_mask_cells == NULL)
3562 return;
3563 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003564
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003565 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003566 {
3567 int cols, cole;
3568 int lines, linee;
3569
3570 li = lio->li_tv.vval.v_list->lv_first;
3571 cols = tv_get_number(&li->li_tv);
3572 if (cols < 0)
3573 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003574 if (cols <= 0)
3575 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003576 li = li->li_next;
3577 cole = tv_get_number(&li->li_tv);
3578 if (cole < 0)
3579 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003580 if (cole > width)
3581 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003582 li = li->li_next;
3583 lines = tv_get_number(&li->li_tv);
3584 if (lines < 0)
3585 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003586 if (lines <= 0)
3587 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003588 li = li->li_next;
3589 linee = tv_get_number(&li->li_tv);
3590 if (linee < 0)
3591 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003592 if (linee > height)
3593 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003594
Bram Moolenaar4012d262020-12-29 11:57:46 +01003595 for (row = lines - 1; row < linee; ++row)
3596 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003597 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003598 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003599}
3600
3601/*
3602 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3603 * "col" and "line" are screen coordinates.
3604 */
3605 static int
3606popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3607{
3608 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3609 int line = screenline - wp->w_winrow;
3610
3611 return col >= 0 && col < width
3612 && line >= 0 && line < height
3613 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003614}
3615
3616/*
3617 * Set flags in popup_transparent[] for window "wp" to "val".
3618 */
3619 static void
3620update_popup_transparent(win_T *wp, int val)
3621{
3622 if (wp->w_popup_mask != NULL)
3623 {
3624 int width = popup_width(wp);
3625 int height = popup_height(wp);
3626 listitem_T *lio, *li;
3627 int cols, cole;
3628 int lines, linee;
3629 int col, line;
3630
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003631 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003632 {
3633 li = lio->li_tv.vval.v_list->lv_first;
3634 cols = tv_get_number(&li->li_tv);
3635 if (cols < 0)
3636 cols = width + cols + 1;
3637 li = li->li_next;
3638 cole = tv_get_number(&li->li_tv);
3639 if (cole < 0)
3640 cole = width + cole + 1;
3641 li = li->li_next;
3642 lines = tv_get_number(&li->li_tv);
3643 if (lines < 0)
3644 lines = height + lines + 1;
3645 li = li->li_next;
3646 linee = tv_get_number(&li->li_tv);
3647 if (linee < 0)
3648 linee = height + linee + 1;
3649
3650 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003651 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003652 if (cols < 0)
3653 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003654 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003655 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003656 if (lines < 0)
3657 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003658 for (line = lines; line < linee
3659 && line + wp->w_winrow < screen_Rows; ++line)
3660 for (col = cols; col < cole
3661 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003662 popup_transparent[(line + wp->w_winrow) * screen_Columns
3663 + col + wp->w_wincol] = val;
3664 }
3665 }
3666}
3667
3668/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003669 * Only called when popup window "wp" is hidden: If the window is positioned
3670 * next to a text property, and it is now visible, then unhide the popup.
3671 * We don't check if visible popups become hidden, that is done in
3672 * popup_adjust_position().
3673 * Return TRUE if the popup became unhidden.
3674 */
3675 static int
3676check_popup_unhidden(win_T *wp)
3677{
3678 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3679 {
3680 textprop_T prop;
3681 linenr_T lnum;
3682
Bram Moolenaar27724252022-05-08 15:00:04 +01003683 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3684 && find_visible_prop(wp->w_popup_prop_win,
3685 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003686 &prop, &lnum) == OK)
3687 {
3688 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003689 wp->w_popup_prop_topline = 0; // force repositioning
3690 return TRUE;
3691 }
3692 }
3693 return FALSE;
3694}
3695
3696/*
3697 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3698 * That is when the buffer in the popup was changed, or the popup is following
3699 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003700 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003701 */
3702 static int
3703popup_need_position_adjust(win_T *wp)
3704{
3705 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3706 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003707 if (win_valid(wp->w_popup_prop_win)
3708 && (wp->w_popup_prop_changedtick
3709 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3710 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3711 return TRUE;
3712
3713 // May need to adjust the width if the cursor moved.
3714 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003715}
3716
3717/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003718 * Update "popup_mask" if needed.
3719 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003720 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003721 * Also marks window lines for redrawing.
3722 */
3723 void
3724may_update_popup_mask(int type)
3725{
3726 win_T *wp;
3727 short *mask;
3728 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003729 int redraw_all_popups = FALSE;
3730 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003731
3732 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003733 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3734 // basic (such as the screen size) must have changed.
3735 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003736 {
3737 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003738 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003739 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003740
3741 // Check if any popup window buffer has changed and if any popup connected
3742 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003743 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003744 if (wp->w_popup_flags & POPF_HIDDEN)
3745 popup_mask_refresh |= check_popup_unhidden(wp);
3746 else if (popup_need_position_adjust(wp))
3747 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003748 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003749 if (wp->w_popup_flags & POPF_HIDDEN)
3750 popup_mask_refresh |= check_popup_unhidden(wp);
3751 else if (popup_need_position_adjust(wp))
3752 popup_mask_refresh = TRUE;
3753
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003754 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003755 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003756
3757 // Need to update the mask, something has changed.
3758 popup_mask_refresh = FALSE;
3759 popup_mask_tab = curtab;
3760 popup_visible = FALSE;
3761
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003762 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003763 // If redrawing only what is needed, update "popup_mask_next" and then
3764 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003765 redrawing_all_win = TRUE;
3766 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003767 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003768 redrawing_all_win = FALSE;
3769 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003770 mask = popup_mask;
3771 else
3772 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003773 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003774
3775 // Find the window with the lowest zindex that hasn't been handled yet,
3776 // so that the window with a higher zindex overwrites the value in
3777 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003778 popup_reset_handled(POPUP_HANDLED_4);
3779 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003780 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003781 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003782 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003783
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003784 popup_visible = TRUE;
3785
Bram Moolenaar12034e22019-08-25 22:25:02 +02003786 // Recompute the position if the text changed. It may make the popup
3787 // hidden if it's attach to a text property that is no longer visible.
3788 if (redraw_all_popups || popup_need_position_adjust(wp))
3789 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003790 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003791 if (wp->w_popup_flags & POPF_HIDDEN)
3792 continue;
3793 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003794
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003795 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003796 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003797 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003798 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003799 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003800 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003801 col < wp->w_wincol + width - wp->w_popup_leftoff
3802 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003803 if (wp->w_zindex < POPUPMENU_ZINDEX
3804 && pum_visible()
3805 && pum_under_menu(line, col, FALSE))
3806 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3807 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003808 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003809 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003810 }
3811
3812 // Only check which lines are to be updated if not already
3813 // updating all lines.
3814 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003815 {
3816 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3817 win_T *prev_wp = NULL;
3818
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003819 for (line = 0; line < screen_Rows; ++line)
3820 {
3821 int col_done = 0;
3822
3823 for (col = 0; col < screen_Columns; ++col)
3824 {
3825 int off = line * screen_Columns + col;
3826
3827 if (popup_mask[off] != popup_mask_next[off])
3828 {
3829 popup_mask[off] = popup_mask_next[off];
3830
3831 if (line >= cmdline_row)
3832 {
3833 // the command line needs to be cleared if text below
3834 // the popup is now visible.
3835 if (!msg_scrolled && popup_mask_next[off] == 0)
3836 clear_cmdline = TRUE;
3837 }
3838 else if (col >= col_done)
3839 {
3840 linenr_T lnum;
3841 int line_cp = line;
3842 int col_cp = col;
3843
3844 // The screen position "line" / "col" needs to be
3845 // redrawn. Figure out what window that is and update
3846 // w_redraw_top and w_redr_bot. Only needs to be done
3847 // once for each window line.
3848 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3849 if (wp != NULL)
3850 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003851#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003852 // A terminal window needs to be redrawn.
3853 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003854 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003855 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003856#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003857 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003858 if (wp != prev_wp)
3859 {
3860 vim_memset(plines_cache, 0,
3861 sizeof(int) * Rows);
3862 prev_wp = wp;
3863 }
3864
3865 if (line_cp >= wp->w_height)
3866 // In (or below) status line
3867 wp->w_redr_status = TRUE;
3868 else
3869 {
3870 // compute the position in the buffer line
3871 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003872 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003873 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003874 redrawWinline(wp, lnum);
3875 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003876 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003877
3878 // This line is going to be redrawn, no need to
3879 // check until the right side of the window.
3880 col_done = wp->w_wincol + wp->w_width - 1;
3881 }
3882 }
3883 }
3884 }
3885 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003886
3887 vim_free(plines_cache);
3888 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003889
3890 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003891}
3892
3893/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003894 * If the current window is a popup and something relevant changed, recompute
3895 * the position and size.
3896 */
3897 void
3898may_update_popup_position(void)
3899{
3900 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3901 popup_adjust_position(curwin);
3902}
3903
3904/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003905 * Return a string of "len" spaces in IObuff.
3906 */
3907 static char_u *
3908get_spaces(int len)
3909{
3910 vim_memset(IObuff, ' ', (size_t)len);
3911 IObuff[len] = NUL;
3912 return IObuff;
3913}
3914
3915/*
3916 * Update popup windows. They are drawn on top of normal windows.
3917 * "win_update" is called for each popup window, lowest zindex first.
3918 */
3919 void
3920update_popups(void (*win_update)(win_T *wp))
3921{
3922 win_T *wp;
3923 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003924 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003925 int total_width;
3926 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003927 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003928 int popup_attr;
3929 int border_attr[4];
3930 int border_char[8];
3931 char_u buf[MB_MAXBYTES];
3932 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003933 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003934 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003935 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003936 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003937 int sb_thumb_top = 0;
3938 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003939 int attr_scroll = 0;
3940 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003941
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003942 // hide the cursor until redrawing is done.
3943 cursor_off();
3944
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003945 // Find the window with the lowest zindex that hasn't been updated yet,
3946 // so that the window with a higher zindex is drawn later, thus goes on
3947 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003948 popup_reset_handled(POPUP_HANDLED_5);
3949 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003950 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003951 int title_len = 0;
3952 int title_wincol;
3953
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003954 // This drawing uses the zindex of the popup window, so that it's on
3955 // top of the text but doesn't draw when another popup with higher
3956 // zindex is on top of the character.
3957 screen_zindex = wp->w_zindex;
3958
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003959 // Set flags in popup_transparent[] for masked cells.
3960 update_popup_transparent(wp, 1);
3961
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003962 // adjust w_winrow and w_wincol for border and padding, since
3963 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003964 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003965 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3966 - wp->w_popup_leftoff;
3967 if (wp->w_wincol + left_extra < 0)
3968 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003969 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003970 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003971
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003972 // Draw the popup text, unless it's off screen.
3973 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003974 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003975 // May need to update the "cursorline" highlighting, which may also
3976 // change "topline"
3977 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3978 popup_highlight_curline(wp);
3979
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003980 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003981
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003982 // move the cursor into the visible lines, otherwise executing
3983 // commands with win_execute() may cause the text to jump.
3984 if (wp->w_cursor.lnum < wp->w_topline)
3985 wp->w_cursor.lnum = wp->w_topline;
3986 else if (wp->w_cursor.lnum >= wp->w_botline)
3987 wp->w_cursor.lnum = wp->w_botline - 1;
3988 }
3989
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003990 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003991 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003992
3993 // Add offset for border and padding if not done already.
3994 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3995 {
3996 wp->w_wcol += left_extra;
3997 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3998 }
3999 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004000 {
4001 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004002 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004003 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004004
Bram Moolenaareabddc42022-04-02 15:32:16 +01004005 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004006 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004007 popup_attr = get_wcr_attr(wp);
4008
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004009 if (wp->w_winrow + total_height > cmdline_row)
4010 wp->w_popup_flags |= POPF_ON_CMDLINE;
4011 else
4012 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4013
4014
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004015 // We can only use these line drawing characters when 'encoding' is
4016 // "utf-8" and 'ambiwidth' is "single".
4017 if (enc_utf8 && *p_ambw == 's')
4018 {
4019 border_char[0] = border_char[2] = 0x2550;
4020 border_char[1] = border_char[3] = 0x2551;
4021 border_char[4] = 0x2554;
4022 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004023 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4024 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004025 border_char[7] = 0x255a;
4026 }
4027 else
4028 {
4029 border_char[0] = border_char[2] = '-';
4030 border_char[1] = border_char[3] = '|';
4031 for (i = 4; i < 8; ++i)
4032 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004033 if (wp->w_popup_flags & POPF_RESIZE)
4034 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004035 }
4036 for (i = 0; i < 8; ++i)
4037 if (wp->w_border_char[i] != 0)
4038 border_char[i] = wp->w_border_char[i];
4039
4040 for (i = 0; i < 4; ++i)
4041 {
4042 border_attr[i] = popup_attr;
4043 if (wp->w_border_highlight[i] != NULL)
4044 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4045 }
4046
Bram Moolenaard91467f2020-11-21 12:42:09 +01004047 // Title goes on top of border or padding.
4048 title_wincol = wp->w_wincol + 1;
4049 if (wp->w_popup_title != NULL)
4050 {
rbtnnc6119412021-08-07 13:08:45 +02004051 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004052
Ralf Schandlbc869872021-05-28 14:12:14 +02004053 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004054 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004055 {
4056 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4057 char_u *title_text = alloc(title_byte_len + 1);
4058
4059 if (title_text != NULL)
4060 {
4061 trunc_string(wp->w_popup_title, title_text,
4062 total_width - 2, title_byte_len + 1);
4063 screen_puts(title_text, wp->w_winrow, title_wincol,
4064 wp->w_popup_border[0] > 0
4065 ? border_attr[0] : popup_attr);
4066 vim_free(title_text);
4067 }
4068
Bram Moolenaard91467f2020-11-21 12:42:09 +01004069 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004070 }
4071 else
4072 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4073 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004074 }
4075
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004076 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004077 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004078 if (wp->w_popup_border[0] > 0)
4079 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004080 // top border; do not draw over the title
4081 if (title_len > 0)
4082 {
4083 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4084 wincol < 0 ? 0 : wincol, title_wincol,
4085 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004086 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004087 border_char[0], border_attr[0]);
4088 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4089 title_wincol + title_len, wincol + total_width,
4090 border_char[0], border_char[0], border_attr[0]);
4091 }
4092 else
4093 {
4094 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4095 wincol < 0 ? 0 : wincol, wincol + total_width,
4096 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4097 ? border_char[4] : border_char[0],
4098 border_char[0], border_attr[0]);
4099 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004100 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004101 {
4102 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4103 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004104 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004105 }
4106 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004107 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4108 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004109
Bram Moolenaard529ba52019-07-02 23:13:53 +02004110 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4111 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004112 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004113 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004114 - wp->w_has_scrollbar;
4115 if (padcol < 0)
4116 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004117 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004118 padcol = 0;
4119 }
4120 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004121 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004122 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004123 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004124 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004125 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004126 // top padding and no border; do not draw over the title
4127 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004128 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004129 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004130 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004131 row += 1;
4132 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004133 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004134 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004135 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004136 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004137
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004138 // Compute scrollbar thumb position and size.
4139 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004140 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004141 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4142 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004143 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004144
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004145 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4146 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004147 if (wp->w_topline > 1 && sb_thumb_height == height)
4148 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004149 if (sb_thumb_height == 0)
4150 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004151 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004152 // it just fits, avoid divide by zero
4153 sb_thumb_top = 0;
4154 else
4155 sb_thumb_top = (wp->w_topline - 1
4156 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004157 * (wp->w_height - sb_thumb_height)
4158 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004159 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4160 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004161 last = total_height - top_off - wp->w_popup_border[2];
4162 if (sb_thumb_top >= last)
4163 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004164 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004165
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004166 if (wp->w_scrollbar_highlight != NULL)
4167 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4168 else
4169 attr_scroll = highlight_attr[HLF_PSB];
4170 if (wp->w_thumb_highlight != NULL)
4171 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4172 else
4173 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004174 }
4175
4176 for (i = wp->w_popup_border[0];
4177 i < total_height - wp->w_popup_border[2]; ++i)
4178 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004179 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004180 // left and right padding only needed next to the body
4181 int do_padding =
4182 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4183 && i < total_height - wp->w_popup_border[2]
4184 - wp->w_popup_padding[2];
4185
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004186 row = wp->w_winrow + i;
4187
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004188 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004189 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004190 {
4191 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004192 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004193 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004194 if (do_padding && wp->w_popup_padding[3] > 0)
4195 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004196 int col = wincol + wp->w_popup_border[3];
4197
Bram Moolenaard529ba52019-07-02 23:13:53 +02004198 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004199 pad_left = wp->w_popup_padding[3];
4200 if (col < 0)
4201 {
4202 pad_left += col;
4203 col = 0;
4204 }
4205 if (pad_left > 0)
4206 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4207 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004208 // scrollbar
4209 if (wp->w_has_scrollbar)
4210 {
4211 int line = i - top_off;
4212 int scroll_col = wp->w_wincol + total_width - 1
4213 - wp->w_popup_border[1];
4214
4215 if (line >= 0 && line < wp->w_height)
4216 screen_putchar(' ', row, scroll_col,
4217 line >= sb_thumb_top
4218 && line < sb_thumb_top + sb_thumb_height
4219 ? attr_thumb : attr_scroll);
4220 else
4221 screen_putchar(' ', row, scroll_col, popup_attr);
4222 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004223 // right border
4224 if (wp->w_popup_border[1] > 0)
4225 {
4226 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004227 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004228 }
4229 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004230 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004231 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004232 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004233 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4234 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004235 }
4236
4237 if (wp->w_popup_padding[2] > 0)
4238 {
4239 // bottom padding
4240 row = wp->w_winrow + wp->w_popup_border[0]
4241 + wp->w_popup_padding[0] + wp->w_height;
4242 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004243 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004244 }
4245
4246 if (wp->w_popup_border[2] > 0)
4247 {
4248 // bottom border
4249 row = wp->w_winrow + total_height - 1;
4250 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004251 wincol < 0 ? 0 : wincol,
4252 wincol + total_width,
4253 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004254 ? border_char[7] : border_char[2],
4255 border_char[2], border_attr[2]);
4256 if (wp->w_popup_border[1] > 0)
4257 {
4258 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004259 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004260 }
4261 }
4262
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004263 if (wp->w_popup_close == POPCLOSE_BUTTON)
4264 {
4265 // close button goes on top of anything at the top-right corner
4266 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004267 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004268 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4269 }
4270
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004271 update_popup_transparent(wp, 0);
4272
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004273 // Back to the normal zindex.
4274 screen_zindex = 0;
4275 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004276
4277#if defined(FEAT_SEARCH_EXTRA)
4278 // In case win_update() called start_search_hl().
4279 end_search_hl();
4280#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004281}
4282
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004283/*
4284 * Mark references in callbacks of one popup window.
4285 */
4286 static int
4287set_ref_in_one_popup(win_T *wp, int copyID)
4288{
4289 int abort = FALSE;
4290 typval_T tv;
4291
4292 if (wp->w_close_cb.cb_partial != NULL)
4293 {
4294 tv.v_type = VAR_PARTIAL;
4295 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4296 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4297 }
4298 if (wp->w_filter_cb.cb_partial != NULL)
4299 {
4300 tv.v_type = VAR_PARTIAL;
4301 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4302 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4303 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004304 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004305 return abort;
4306}
4307
4308/*
4309 * Set reference in callbacks of popup windows.
4310 */
4311 int
4312set_ref_in_popups(int copyID)
4313{
4314 int abort = FALSE;
4315 win_T *wp;
4316 tabpage_T *tp;
4317
4318 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4319 abort = abort || set_ref_in_one_popup(wp, copyID);
4320
4321 FOR_ALL_TABPAGES(tp)
4322 {
4323 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4324 abort = abort || set_ref_in_one_popup(wp, copyID);
4325 if (abort)
4326 break;
4327 }
4328 return abort;
4329}
Bram Moolenaar79648732019-07-18 21:43:07 +02004330
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004331 int
4332popup_is_popup(win_T *wp)
4333{
4334 return wp->w_popup_flags != 0;
4335}
4336
4337#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004338/*
4339 * Find an existing popup used as the preview window, in the current tab page.
4340 * Return NULL if not found.
4341 */
4342 win_T *
4343popup_find_preview_window(void)
4344{
4345 win_T *wp;
4346
4347 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004348 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004349 if (wp->w_p_pvw)
4350 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004351 return NULL;
4352}
4353
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004354/*
4355 * Find an existing popup used as the info window, in the current tab page.
4356 * Return NULL if not found.
4357 */
4358 win_T *
4359popup_find_info_window(void)
4360{
4361 win_T *wp;
4362
4363 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004364 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004365 if (wp->w_popup_flags & POPF_INFO)
4366 return wp;
4367 return NULL;
4368}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004369#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004370
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004371 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004372f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4373{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004374#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004375 win_T *wp = popup_find_info_window();
4376
4377 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004378#else
4379 rettv->vval.v_number = 0;
4380#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004381}
4382
4383 void
4384f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004385{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004386#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004387 win_T *wp = popup_find_preview_window();
4388
4389 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004390#else
4391 rettv->vval.v_number = 0;
4392#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004393}
4394
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004395#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004396/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004397 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004398 * NOTE: this makes the popup the current window, so that the file can be
4399 * edited. However, it must not remain to be the current window, the caller
4400 * must make sure of that.
4401 */
4402 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004403popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004404{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004405 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004406
4407 if (wp == NULL)
4408 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004409 if (info)
4410 wp->w_popup_flags |= POPF_INFO;
4411 else
4412 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004413
4414 // Set the width to a reasonable value, so that w_topline can be computed.
4415 if (wp->w_minwidth > 0)
4416 wp->w_width = wp->w_minwidth;
4417 else if (wp->w_maxwidth > 0)
4418 wp->w_width = wp->w_maxwidth;
4419 else
4420 wp->w_width = curwin->w_width;
4421
4422 // Will switch to another buffer soon, dummy one can be wiped.
4423 wp->w_buffer->b_locked = FALSE;
4424
4425 win_enter(wp, FALSE);
4426 return OK;
4427}
4428
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004429/*
4430 * Close any preview popup.
4431 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004432 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004433popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004434{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004435 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004436
4437 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004438 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004439}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004440
4441/*
4442 * Hide the info popup.
4443 */
4444 void
4445popup_hide_info(void)
4446{
4447 win_T *wp = popup_find_info_window();
4448
4449 if (wp != NULL)
4450 popup_hide(wp);
4451}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004452
4453/*
4454 * Close any info popup.
4455 */
4456 void
4457popup_close_info(void)
4458{
4459 win_T *wp = popup_find_info_window();
4460
4461 if (wp != NULL)
4462 popup_close_with_retval(wp, -1);
4463}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004464#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004465
Bram Moolenaar9198de32022-08-27 21:30:03 +01004466#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4467
Bram Moolenaar9198de32022-08-27 21:30:03 +01004468/*
4469 * Get the message window.
4470 * Returns NULL if something failed.
4471 */
4472 win_T *
4473popup_get_message_win(void)
4474{
4475 if (message_win == NULL)
4476 {
4477 int i;
4478
4479 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4480
4481 if (message_win == NULL)
4482 return NULL;
4483
4484 // use the full screen width
4485 message_win->w_width = Columns;
4486
4487 // position at bottom of screen
4488 message_win->w_popup_pos = POPPOS_BOTTOM;
4489 message_win->w_wantcol = 1;
4490 message_win->w_minwidth = 9999;
4491
4492 // no padding, border at the top
4493 for (i = 0; i < 4; ++i)
4494 message_win->w_popup_padding[i] = 0;
4495 for (i = 1; i < 4; ++i)
4496 message_win->w_popup_border[i] = 0;
4497
4498 if (message_win->w_popup_timer != NULL)
4499 message_win->w_popup_timer->tr_keep = TRUE;
4500 }
4501 return message_win;
4502}
4503
4504/*
4505 * If the message window is not visible: show it
4506 * If the message window is visible: reset the timeout
4507 */
4508 void
4509popup_show_message_win(void)
4510{
4511 if (message_win != NULL)
4512 {
4513 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4514 {
4515 // the highlight may have changed.
4516 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4517 popup_show(message_win);
4518 }
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004519 if (message_win->w_popup_timer != NULL)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004520 timer_start(message_win->w_popup_timer);
4521 }
4522}
4523
4524 int
4525popup_message_win_visible(void)
4526{
4527 return message_win != NULL
4528 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4529}
4530
4531/*
4532 * If the message window is visible: hide it.
4533 */
4534 void
4535popup_hide_message_win(void)
4536{
4537 if (message_win != NULL)
4538 popup_hide(message_win);
4539}
4540
Bram Moolenaar43568642022-08-28 13:02:45 +01004541/*
4542 * If the message window exists: close it.
4543 */
4544 void
4545popup_close_message_win(void)
4546{
4547 if (message_win != NULL)
4548 popup_close(message_win->w_id, TRUE);
4549}
4550
Bram Moolenaar9198de32022-08-27 21:30:03 +01004551#endif
4552
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004553/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004554 * Close any popup for a text property associated with "win".
4555 * Return TRUE if a popup was closed.
4556 */
4557 int
4558popup_win_closed(win_T *win)
4559{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004560 int round;
4561 win_T *wp;
4562 win_T *next;
4563 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004564
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004565 for (round = 1; round <= 2; ++round)
4566 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4567 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004568 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004569 next = wp->w_next;
4570 if (wp->w_popup_prop_win == win)
4571 {
4572 popup_close_with_retval(wp, -1);
4573 ret = TRUE;
4574 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004575 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004576 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004577}
4578
4579/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004580 * Set the title of the popup window to the file name.
4581 */
4582 void
4583popup_set_title(win_T *wp)
4584{
4585 if (wp->w_buffer->b_fname != NULL)
4586 {
4587 char_u dirname[MAXPATHL];
4588 size_t len;
4589
4590 mch_dirname(dirname, MAXPATHL);
4591 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4592
4593 vim_free(wp->w_popup_title);
4594 len = STRLEN(wp->w_buffer->b_fname) + 3;
4595 wp->w_popup_title = alloc((int)len);
4596 if (wp->w_popup_title != NULL)
4597 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4598 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004599 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004600 }
4601}
4602
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004603# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004604/*
4605 * If there is a preview window, update the title.
4606 * Used after changing directory.
4607 */
4608 void
4609popup_update_preview_title(void)
4610{
4611 win_T *wp = popup_find_preview_window();
4612
4613 if (wp != NULL)
4614 popup_set_title(wp);
4615}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004616# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004617
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004618#endif // FEAT_PROP_POPUP