blob: 8688f3e3d13e344aade394e79ef15f7af5f3ce00 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar43568642022-08-28 13:02:45 +010031#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +010032// Window used for ":echowindow"
Bram Moolenaar43568642022-08-28 13:02:45 +010033static win_T *message_win = NULL;
34#endif
35
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020036static void popup_adjust_position(win_T *wp);
37
Bram Moolenaar4d784b22019-05-25 19:51:39 +020038/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020039 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020041 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020042 */
43 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020044popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020045{
46 dictitem_T *di;
47 char_u *val;
48 char_u *s;
49 char_u *endp;
50 int n = 0;
51
52 di = dict_find(dict, key, -1);
53 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020054 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020055
56 val = tv_get_string(&di->di_tv);
57 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020058 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059
60 setcursor_mayforce(TRUE);
61 s = val + 6;
62 if (*s != NUL)
63 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020064 endp = s;
65 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
66 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020067 if (endp != NULL && *skipwhite(endp) != NUL)
68 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020069 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020070 return 0;
71 }
72 }
73
74 if (STRCMP(key, "line") == 0)
75 n = screen_screenrow() + 1 + n;
76 else // "col"
77 n = screen_screencol() + 1 + n;
78
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020079 // Zero means "not set", use -1 instead.
80 if (n == 0)
81 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020082 return n;
83}
84
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020085 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020086set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020087{
88 dictitem_T *di;
89
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020090 di = dict_find(dict, (char_u *)name, -1);
91 if (di != NULL)
92 {
93 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000094 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020095 else
96 {
97 list_T *list = di->di_tv.vval.v_list;
98 listitem_T *li;
99 int i;
100 int nr;
101
102 for (i = 0; i < 4; ++i)
103 array[i] = 1;
104 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100105 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200106 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200107 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
108 ++i, li = li->li_next)
109 {
110 nr = (int)tv_get_number(&li->li_tv);
111 if (nr >= 0)
112 array[i] = nr > max_val ? max_val : nr;
113 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100114 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200115 }
116 }
117}
118
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200119/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200120 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200121 */
122 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200123set_moved_values(win_T *wp)
124{
125 wp->w_popup_curwin = curwin;
126 wp->w_popup_lnum = curwin->w_cursor.lnum;
127 wp->w_popup_mincol = curwin->w_cursor.col;
128 wp->w_popup_maxcol = curwin->w_cursor.col;
129}
130
131/*
132 * Used when popup options contain "moved" with "word" or "WORD".
133 */
134 static void
135set_moved_columns(win_T *wp, int flags)
136{
137 char_u *ptr;
138 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
139
140 if (len > 0)
141 {
142 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
143 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
144 }
145}
146
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200147/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200148 * Used when popup options contain "mousemoved": set default moved values.
149 */
150 static void
151set_mousemoved_values(win_T *wp)
152{
153 wp->w_popup_mouse_row = mouse_row;
154 wp->w_popup_mouse_mincol = mouse_col;
155 wp->w_popup_mouse_maxcol = mouse_col;
156}
157
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000158 static void
159update_popup_uses_mouse_move(void)
160{
161 popup_uses_mouse_move = FALSE;
162 if (popup_visible)
163 {
164 win_T *wp;
165
166 FOR_ALL_POPUPWINS(wp)
167 if (wp->w_popup_mouse_row != 0)
168 {
169 popup_uses_mouse_move = TRUE;
170 return;
171 }
172 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
173 if (wp->w_popup_mouse_row != 0)
174 {
175 popup_uses_mouse_move = TRUE;
176 return;
177 }
178 }
179}
180
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200223 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
224 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200225 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200226 * Caller should check the left mouse button was clicked.
227 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200228 */
229 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200230popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200231{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200232 if (wp->w_popup_close == POPCLOSE_BUTTON
233 && row == 0 && col == popup_width(wp) - 1)
234 {
235 popup_close_for_mouse_click(wp);
236 return TRUE;
237 }
238 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200239}
240
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200241// Values set when dragging a popup window starts.
242static int drag_start_row;
243static int drag_start_col;
244static int drag_start_wantline;
245static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200246static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200247
248/*
249 * Mouse down on border of popup window: start dragging it.
250 * Uses mouse_col and mouse_row.
251 */
252 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200253popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200254{
255 drag_start_row = mouse_row;
256 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200257 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200258 drag_start_wantline = wp->w_winrow + 1;
259 else
260 drag_start_wantline = wp->w_wantline;
261 if (wp->w_wantcol == 0)
262 drag_start_wantcol = wp->w_wincol + 1;
263 else
264 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200265
266 // Stop centering the popup
267 if (wp->w_popup_pos == POPPOS_CENTER)
268 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269
270 drag_on_resize_handle = wp->w_popup_border[1] > 0
271 && wp->w_popup_border[2] > 0
272 && row == popup_height(wp) - 1
273 && col == popup_width(wp) - 1;
274
275 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
276 {
277 if (wp->w_popup_pos == POPPOS_TOPRIGHT
278 || wp->w_popup_pos == POPPOS_BOTRIGHT)
279 wp->w_wantcol = wp->w_wincol + 1;
280 if (wp->w_popup_pos == POPPOS_BOTLEFT)
281 wp->w_wantline = wp->w_winrow + 1;
282 wp->w_popup_pos = POPPOS_TOPLEFT;
283 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284}
285
286/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200287 * Mouse moved while dragging a popup window: adjust the window popup position
288 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200289 */
290 void
291popup_drag(win_T *wp)
292{
293 // The popup may be closed before dragging stops.
294 if (!win_valid_popup(wp))
295 return;
296
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200297 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
298 {
299 int width_inc = mouse_col - drag_start_col;
300 int height_inc = mouse_row - drag_start_row;
301
302 if (width_inc != 0)
303 {
304 int width = wp->w_width + width_inc;
305
306 if (width < 1)
307 width = 1;
308 wp->w_minwidth = width;
309 wp->w_maxwidth = width;
310 drag_start_col = mouse_col;
311 }
312
313 if (height_inc != 0)
314 {
315 int height = wp->w_height + height_inc;
316
317 if (height < 1)
318 height = 1;
319 wp->w_minheight = height;
320 wp->w_maxheight = height;
321 drag_start_row = mouse_row;
322 }
323
324 popup_adjust_position(wp);
325 return;
326 }
327
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000328 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200329 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200330 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
331 if (wp->w_wantline < 1)
332 wp->w_wantline = 1;
333 if (wp->w_wantline > Rows)
334 wp->w_wantline = Rows;
335 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
336 if (wp->w_wantcol < 1)
337 wp->w_wantcol = 1;
338 if (wp->w_wantcol > Columns)
339 wp->w_wantcol = Columns;
340
341 popup_adjust_position(wp);
342}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200343
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200344/*
345 * Set w_firstline to match the current "wp->w_topline".
346 */
347 void
348popup_set_firstline(win_T *wp)
349{
350 int height = wp->w_height;
351
352 wp->w_firstline = wp->w_topline;
353 popup_adjust_position(wp);
354
355 // we don't want the popup to get smaller, decrement the first line
356 // until it doesn't
357 while (wp->w_firstline > 1 && wp->w_height < height)
358 {
359 --wp->w_firstline;
360 popup_adjust_position(wp);
361 }
362}
363
364/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200365 * Return TRUE if the position is in the popup window scrollbar.
366 */
367 int
368popup_is_in_scrollbar(win_T *wp, int row, int col)
369{
370 return wp->w_has_scrollbar
371 && row >= wp->w_popup_border[0]
372 && row < popup_height(wp) - wp->w_popup_border[2]
373 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
374}
375
376
377/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200378 * Handle a click in a popup window, if it is in the scrollbar.
379 */
380 void
381popup_handle_scrollbar_click(win_T *wp, int row, int col)
382{
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200383 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200384 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100385 int height = popup_height(wp);
386 int new_topline = wp->w_topline;
387
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200388 if (row >= height / 2)
389 {
390 // Click in lower half, scroll down.
391 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaard28950f2022-05-29 14:13:04 +0100392 ++new_topline;
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200393 }
394 else if (wp->w_topline > 1)
395 // click on upper half, scroll up.
Bram Moolenaard28950f2022-05-29 14:13:04 +0100396 --new_topline;
397 if (new_topline != wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200398 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100399 set_topline(wp, new_topline);
400 if (wp == curwin)
401 {
402 if (wp->w_cursor.lnum < wp->w_topline)
403 {
404 wp->w_cursor.lnum = wp->w_topline;
405 check_cursor();
406 }
407 else if (wp->w_cursor.lnum >= wp->w_botline)
408 {
409 wp->w_cursor.lnum = wp->w_botline - 1;
410 check_cursor();
411 }
412 }
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200413 popup_set_firstline(wp);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100414 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200415 }
416 }
417}
418
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200419#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100420/*
421 * Add a timer to "wp" with "time".
422 * If "close" is true use popup_close(), otherwise popup_hide().
423 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200424 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100425popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200426{
427 char_u cbbuf[50];
428 char_u *ptr = cbbuf;
429 typval_T tv;
430
431 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100432 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
433 wp->w_id);
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200434 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200435 {
436 wp->w_popup_timer = create_timer(time, 0);
437 wp->w_popup_timer->tr_callback = get_callback(&tv);
438 clear_tv(&tv);
439 }
440}
441#endif
442
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100443 static poppos_T
444get_pos_entry(dict_T *d, int give_error)
445{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100446 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100447 int nr;
448
449 if (str == NULL)
450 return POPPOS_NONE;
451
K.Takataeeec2542021-06-02 13:28:16 +0200452 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100453 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
454 return poppos_entries[nr].pp_val;
455
456 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000457 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100458 return POPPOS_NONE;
459}
460
Bram Moolenaar17627312019-06-02 19:53:44 +0200461/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200462 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200463 */
464 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200465apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200466{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200467 int nr;
468 char_u *str;
469 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200470
Bram Moolenaard61efa52022-07-23 09:52:04 +0100471 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200472 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100473 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200474 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100475 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200476 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100477 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200478 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200479
480 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200481 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200482 wp->w_wantline = nr;
483 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200484 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200485 wp->w_wantcol = nr;
486
Bram Moolenaar55881332020-08-18 13:04:15 +0200487
Bram Moolenaard61efa52022-07-23 09:52:04 +0100488 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200489 if (nr != -1)
490 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200491
Bram Moolenaar12034e22019-08-25 22:25:02 +0200492 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100493 poppos_T ppt = get_pos_entry(d, TRUE);
494
495 if (ppt != POPPOS_NONE)
496 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200497 }
498
Bram Moolenaard61efa52022-07-23 09:52:04 +0100499 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200500 if (str != NULL)
501 {
502 wp->w_popup_prop_type = 0;
503 if (*str != NUL)
504 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100505 wp->w_popup_prop_win = curwin;
506 di = dict_find(d, (char_u *)"textpropwin", -1);
507 if (di != NULL)
508 {
509 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100510 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100511 wp->w_popup_prop_win = curwin;
512 }
513
514 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200515 if (nr <= 0)
516 nr = find_prop_type_id(str, NULL);
517 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000518 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200519 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200520 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200521 }
522 }
523
524 di = dict_find(d, (char_u *)"textpropid", -1);
525 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100526 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200527}
528
Bram Moolenaarb0992022020-01-30 14:55:42 +0100529/*
530 * Handle "moved" and "mousemoved" arguments.
531 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200532 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200533handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
534{
535 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
536 {
537 char_u *s = di->di_tv.vval.v_string;
538 int flags = 0;
539
540 if (STRCMP(s, "word") == 0)
541 flags = FIND_IDENT | FIND_STRING;
542 else if (STRCMP(s, "WORD") == 0)
543 flags = FIND_STRING;
544 else if (STRCMP(s, "expr") == 0)
545 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
546 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000547 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200548 if (flags != 0)
549 {
550 if (mousemoved)
551 set_mousemoved_columns(wp, flags);
552 else
553 set_moved_columns(wp, flags);
554 }
555 }
556 else if (di->di_tv.v_type == VAR_LIST
557 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200558 && (di->di_tv.vval.v_list->lv_len == 2
559 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200560 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200561 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100562 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200563 int mincol;
564 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200565
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200566 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100567 li = l->lv_first;
568 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200569 {
570 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
571
572 // Three numbers, might be from popup_getoptions().
573 if (mousemoved)
574 wp->w_popup_mouse_row = nr;
575 else
576 wp->w_popup_lnum = nr;
577 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100578 if (nr == 0)
579 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200580 }
581
582 mincol = tv_get_number(&li->li_tv);
583 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200584 if (mousemoved)
585 {
586 wp->w_popup_mouse_mincol = mincol;
587 wp->w_popup_mouse_maxcol = maxcol;
588 }
589 else
590 {
591 wp->w_popup_mincol = mincol;
592 wp->w_popup_maxcol = maxcol;
593 }
594 }
595 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000596 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200597}
598
599 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200600check_highlight(dict_T *dict, char *name, char_u **pval)
601{
602 dictitem_T *di;
603 char_u *str;
604
605 di = dict_find(dict, (char_u *)name, -1);
606 if (di != NULL)
607 {
608 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000609 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200610 else
611 {
612 str = tv_get_string(&di->di_tv);
613 if (*str != NUL)
614 *pval = vim_strsave(str);
615 }
616 }
617}
618
Bram Moolenaarae943152019-06-16 22:54:14 +0200619/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200620 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200621 */
622 static void
623popup_show_curline(win_T *wp)
624{
625 if (wp->w_cursor.lnum < wp->w_topline)
626 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200627 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200628 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200629 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200630 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200631 if (wp->w_topline < 1)
632 wp->w_topline = 1;
633 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
634 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200635 while (wp->w_topline < wp->w_cursor.lnum
636 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
637 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
638 > wp->w_height)
639 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200640 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200641
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200642 // Don't let "firstline" cause a scroll.
643 if (wp->w_firstline > 0)
644 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200645}
646
647/*
648 * Get the sign group name for window "wp".
649 * Returns a pointer to a static buffer, overwritten on the next call.
650 */
651 static char_u *
652popup_get_sign_name(win_T *wp)
653{
654 static char buf[30];
655
656 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
657 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200658}
659
660/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200661 * Highlight the line with the cursor.
662 * Also scrolls the text to put the cursor line in view.
663 */
664 static void
665popup_highlight_curline(win_T *wp)
666{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200667 int sign_id = 0;
668 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200669
Bram Moolenaar72570732019-11-30 14:21:53 +0100670 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200671
672 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
673 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200674 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200675
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200676 if (!sign_exists_by_name(sign_name))
677 {
678 char *linehl = "PopupSelected";
679
680 if (syn_name2id((char_u *)linehl) == 0)
681 linehl = "PmenuSel";
Bram Moolenaar9198de32022-08-27 21:30:03 +0100682 sign_define_by_name(sign_name, NULL, (char_u *)linehl,
683 NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200684 }
685
Bram Moolenaar72570732019-11-30 14:21:53 +0100686 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200687 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100688 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200689 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200690 else
691 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200692 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200693}
694
695/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200696 * Shared between popup_create() and f_popup_setoptions().
697 */
698 static void
699apply_general_options(win_T *wp, dict_T *dict)
700{
701 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200702 int nr;
703 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200704
Bram Moolenaarae943152019-06-16 22:54:14 +0200705 // TODO: flip
706
707 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200708 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200709 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100710 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200711 if (wp->w_firstline < 0)
712 wp->w_firstline = -1;
713 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200714
Bram Moolenaard61efa52022-07-23 09:52:04 +0100715 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200716 if (nr != -1)
717 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200718
Bram Moolenaard61efa52022-07-23 09:52:04 +0100719 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200720 if (str != NULL)
721 {
722 vim_free(wp->w_popup_title);
723 wp->w_popup_title = vim_strsave(str);
724 }
725
Bram Moolenaard61efa52022-07-23 09:52:04 +0100726 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200727 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200728 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200729
Bram Moolenaard61efa52022-07-23 09:52:04 +0100730 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200731 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200732 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200733 if (nr)
734 wp->w_popup_flags |= POPF_DRAG;
735 else
736 wp->w_popup_flags &= ~POPF_DRAG;
737 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100738 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000739 if (nr != -1)
740 {
741 if (nr)
742 wp->w_popup_flags |= POPF_DRAGALL;
743 else
744 wp->w_popup_flags &= ~POPF_DRAGALL;
745 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200746
Bram Moolenaard61efa52022-07-23 09:52:04 +0100747 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200748 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100749 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100750 if (nr)
751 wp->w_popup_flags |= POPF_POSINVERT;
752 else
753 wp->w_popup_flags &= ~POPF_POSINVERT;
754 }
755
Bram Moolenaard61efa52022-07-23 09:52:04 +0100756 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200757 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200758 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200759 if (nr)
760 wp->w_popup_flags |= POPF_RESIZE;
761 else
762 wp->w_popup_flags &= ~POPF_RESIZE;
763 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200764
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200765 di = dict_find(dict, (char_u *)"close", -1);
766 if (di != NULL)
767 {
768 int ok = TRUE;
769
770 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
771 {
772 char_u *s = di->di_tv.vval.v_string;
773
774 if (STRCMP(s, "none") == 0)
775 wp->w_popup_close = POPCLOSE_NONE;
776 else if (STRCMP(s, "button") == 0)
777 wp->w_popup_close = POPCLOSE_BUTTON;
778 else if (STRCMP(s, "click") == 0)
779 wp->w_popup_close = POPCLOSE_CLICK;
780 else
781 ok = FALSE;
782 }
783 else
784 ok = FALSE;
785 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000786 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200787 }
788
Bram Moolenaard61efa52022-07-23 09:52:04 +0100789 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200790 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000791 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200792 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
793 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000794#ifdef FEAT_TERMINAL
795 term_update_wincolor(wp);
796#endif
797 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200798
Bram Moolenaarae943152019-06-16 22:54:14 +0200799 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
800 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200801
Bram Moolenaar790498b2019-06-01 22:15:29 +0200802 di = dict_find(dict, (char_u *)"borderhighlight", -1);
803 if (di != NULL)
804 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200805 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000806 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200807 else
808 {
809 list_T *list = di->di_tv.vval.v_list;
810 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200811 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200812
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200813 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200814 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
815 ++i, li = li->li_next)
816 {
817 str = tv_get_string(&li->li_tv);
818 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100819 {
820 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200821 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100822 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200823 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200824 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
825 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100826 {
827 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200828 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200829 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100830 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200831 }
832 }
833
Bram Moolenaar790498b2019-06-01 22:15:29 +0200834 di = dict_find(dict, (char_u *)"borderchars", -1);
835 if (di != NULL)
836 {
837 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000838 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200839 else
840 {
841 list_T *list = di->di_tv.vval.v_list;
842 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200843 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200844
845 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100846 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200847 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200848 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
849 ++i, li = li->li_next)
850 {
851 str = tv_get_string(&li->li_tv);
852 if (*str != NUL)
853 wp->w_border_char[i] = mb_ptr2char(str);
854 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200855 if (list->lv_len == 1)
856 for (i = 1; i < 8; ++i)
857 wp->w_border_char[i] = wp->w_border_char[0];
858 if (list->lv_len == 2)
859 {
860 for (i = 4; i < 8; ++i)
861 wp->w_border_char[i] = wp->w_border_char[1];
862 for (i = 1; i < 4; ++i)
863 wp->w_border_char[i] = wp->w_border_char[0];
864 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200865 }
866 }
867 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200868
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200869 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
870 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
871
Bram Moolenaarae943152019-06-16 22:54:14 +0200872 di = dict_find(dict, (char_u *)"zindex", -1);
873 if (di != NULL)
874 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100875 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200876 if (wp->w_zindex < 1)
877 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
878 if (wp->w_zindex > 32000)
879 wp->w_zindex = 32000;
880 }
881
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200882 di = dict_find(dict, (char_u *)"mask", -1);
883 if (di != NULL)
884 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200885 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200886
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200887 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200888 {
889 listitem_T *li;
890
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200891 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200892 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200893 {
894 if (li->li_tv.v_type != VAR_LIST
895 || li->li_tv.vval.v_list == NULL
896 || li->li_tv.vval.v_list->lv_len != 4)
897 {
898 ok = FALSE;
899 break;
900 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100901 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200902 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200903 }
904 }
905 if (ok)
906 {
907 wp->w_popup_mask = di->di_tv.vval.v_list;
908 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200909 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200910 }
911 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000912 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200913 }
914
Bram Moolenaarae943152019-06-16 22:54:14 +0200915#if defined(FEAT_TIMERS)
916 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100917 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200918 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100919 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200920#endif
921
Bram Moolenaar3397f742019-06-02 18:40:06 +0200922 di = dict_find(dict, (char_u *)"moved", -1);
923 if (di != NULL)
924 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200925 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200926 handle_moved_argument(wp, di, FALSE);
927 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200928
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200929 di = dict_find(dict, (char_u *)"mousemoved", -1);
930 if (di != NULL)
931 {
932 set_mousemoved_values(wp);
933 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200934 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200935
Bram Moolenaard61efa52022-07-23 09:52:04 +0100936 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100937 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200938 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100939 if (nr != 0)
940 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200941 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100942 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200943 }
944
Bram Moolenaarae943152019-06-16 22:54:14 +0200945 di = dict_find(dict, (char_u *)"filter", -1);
946 if (di != NULL)
947 {
948 callback_T callback = get_callback(&di->di_tv);
949
950 if (callback.cb_name != NULL)
951 {
952 free_callback(&wp->w_filter_cb);
953 set_callback(&wp->w_filter_cb, &callback);
954 }
955 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100956 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200957 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200958 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200959 if (nr)
960 wp->w_popup_flags |= POPF_MAPPING;
961 else
962 wp->w_popup_flags &= ~POPF_MAPPING;
963 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200964
Bram Moolenaard61efa52022-07-23 09:52:04 +0100965 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200966 if (str != NULL)
967 {
968 if (STRCMP(str, "a") == 0)
969 wp->w_filter_mode = MODE_ALL;
970 else
971 wp->w_filter_mode = mode_str2flags(str);
972 }
973
Bram Moolenaarae943152019-06-16 22:54:14 +0200974 di = dict_find(dict, (char_u *)"callback", -1);
975 if (di != NULL)
976 {
977 callback_T callback = get_callback(&di->di_tv);
978
979 if (callback.cb_name != NULL)
980 {
981 free_callback(&wp->w_close_cb);
982 set_callback(&wp->w_close_cb, &callback);
983 }
984 }
985}
986
987/*
988 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200989 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200990 */
991 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200992apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200993{
994 int nr;
995
996 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200997
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200998 if (create)
999 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001000 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1001
Bram Moolenaarae943152019-06-16 22:54:14 +02001002 apply_general_options(wp, dict);
1003
Bram Moolenaard61efa52022-07-23 09:52:04 +01001004 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001005 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001006 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001007
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001008 // when "firstline" and "cursorline" are both set and the cursor would be
1009 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001010 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1011 {
1012 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1013 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001014 else if (wp->w_cursor.lnum < wp->w_firstline
1015 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001016 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001017 wp->w_topline = wp->w_firstline;
1018 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001019 }
1020
Bram Moolenaar33796b32019-06-08 16:01:13 +02001021 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001022 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001023}
1024
1025/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001026 * Add lines to the popup from a list of strings.
1027 */
1028 static void
1029add_popup_strings(buf_T *buf, list_T *l)
1030{
1031 listitem_T *li;
1032 linenr_T lnum = 0;
1033 char_u *p;
1034
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001035 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001036 if (li->li_tv.v_type == VAR_STRING)
1037 {
1038 p = li->li_tv.vval.v_string;
1039 ml_append_buf(buf, lnum++,
1040 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1041 }
1042}
1043
1044/*
1045 * Add lines to the popup from a list of dictionaries.
1046 */
1047 static void
1048add_popup_dicts(buf_T *buf, list_T *l)
1049{
1050 listitem_T *li;
1051 listitem_T *pli;
1052 linenr_T lnum = 0;
1053 char_u *p;
1054 dict_T *dict;
1055
1056 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001057 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001058 {
1059 if (li->li_tv.v_type != VAR_DICT)
1060 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001061 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001062 return;
1063 }
1064 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001065 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001066 ml_append_buf(buf, lnum++,
1067 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1068 }
1069
1070 // add the text properties
1071 lnum = 1;
1072 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1073 {
1074 dictitem_T *di;
1075 list_T *plist;
1076
1077 dict = li->li_tv.vval.v_dict;
1078 di = dict_find(dict, (char_u *)"props", -1);
1079 if (di != NULL)
1080 {
1081 if (di->di_tv.v_type != VAR_LIST)
1082 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001083 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001084 return;
1085 }
1086 plist = di->di_tv.vval.v_list;
1087 if (plist != NULL)
1088 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001089 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001090 {
1091 if (pli->li_tv.v_type != VAR_DICT)
1092 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001093 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001094 return;
1095 }
1096 dict = pli->li_tv.vval.v_dict;
1097 if (dict != NULL)
1098 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001099 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001100
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001101 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001102 }
1103 }
1104 }
1105 }
1106 }
1107}
1108
1109/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001110 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1111 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001112 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001113popup_top_extra(win_T *wp)
1114{
1115 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1116
1117 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1118 return 1;
1119 return extra;
1120}
1121
1122/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001123 * Get the padding plus border at the left.
1124 */
1125 int
1126popup_left_extra(win_T *wp)
1127{
1128 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1129}
1130
1131/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001132 * Return the height of popup window "wp", including border and padding.
1133 */
1134 int
1135popup_height(win_T *wp)
1136{
1137 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001138 + popup_top_extra(wp)
1139 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001140}
1141
1142/*
1143 * Return the width of popup window "wp", including border, padding and
1144 * scrollbar.
1145 */
1146 int
1147popup_width(win_T *wp)
1148{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001149 // w_leftcol is how many columns of the core are left of the screen
1150 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001151 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001152 + popup_extra_width(wp)
1153 + wp->w_popup_rightoff;
1154}
1155
1156/*
1157 * Return the extra width of popup window "wp": border, padding and scrollbar.
1158 */
1159 int
1160popup_extra_width(win_T *wp)
1161{
1162 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1163 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1164 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001165}
1166
1167/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001168 * Adjust the position and size of the popup to fit on the screen.
1169 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001170 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001171popup_adjust_position(win_T *wp)
1172{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001173 linenr_T lnum;
1174 int wrapped = 0;
1175 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001176 int maxwidth_no_scrollbar;
1177 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001178 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001179 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001180 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001181 int center_vert = FALSE;
1182 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001183 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001184 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001185 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1186 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1187 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1188 int extra_height = top_extra + bot_extra;
1189 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001190 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001191 int org_winrow = wp->w_winrow;
1192 int org_wincol = wp->w_wincol;
1193 int org_width = wp->w_width;
1194 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001195 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001196 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001197 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001198 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001199 int wantline = wp->w_wantline; // adjusted for textprop
1200 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001201 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001202 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001203
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001204 wp->w_winrow = 0;
1205 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001206 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001207 wp->w_popup_leftoff = 0;
1208 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001209
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001210 // May need to update the "cursorline" highlighting, which may also change
1211 // "topline"
1212 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1213 popup_highlight_curline(wp);
1214
Bram Moolenaar12034e22019-08-25 22:25:02 +02001215 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1216 {
1217 win_T *prop_win = wp->w_popup_prop_win;
1218 textprop_T prop;
1219 linenr_T prop_lnum;
1220 pos_T pos;
1221 int screen_row;
1222 int screen_scol;
1223 int screen_ccol;
1224 int screen_ecol;
1225
1226 // Popup window is positioned relative to a text property.
1227 if (find_visible_prop(prop_win,
1228 wp->w_popup_prop_type, wp->w_popup_prop_id,
1229 &prop, &prop_lnum) == FAIL)
1230 {
1231 // Text property is no longer visible, hide the popup.
1232 // Unhiding the popup is done in check_popup_unhidden().
1233 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1234 {
1235 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001236 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001237 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001238 }
1239 return;
1240 }
1241
1242 // Compute the desired position from the position of the text
1243 // property. Use "wantline" and "wantcol" as offsets.
1244 pos.lnum = prop_lnum;
1245 pos.col = prop.tp_col;
1246 if (wp->w_popup_pos == POPPOS_TOPLEFT
1247 || wp->w_popup_pos == POPPOS_BOTLEFT)
1248 pos.col += prop.tp_len - 1;
1249 textpos2screenpos(prop_win, &pos, &screen_row,
1250 &screen_scol, &screen_ccol, &screen_ecol);
1251
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001252 if (screen_scol == 0)
1253 {
1254 // position is off screen, make the width zero to hide it.
1255 wp->w_width = 0;
1256 return;
1257 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001258 if (wp->w_popup_pos == POPPOS_TOPLEFT
1259 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1260 // below the text
1261 wantline = screen_row + wantline + 1;
1262 else
1263 // above the text
1264 wantline = screen_row + wantline - 1;
1265 center_vert = FALSE;
1266 if (wp->w_popup_pos == POPPOS_TOPLEFT
1267 || wp->w_popup_pos == POPPOS_BOTLEFT)
1268 // right of the text
1269 wantcol = screen_ecol + wantcol;
1270 else
1271 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001272 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001273 use_wantcol = TRUE;
1274 }
1275 else
1276 {
1277 // If no line was specified default to vertical centering.
1278 if (wantline == 0)
1279 center_vert = TRUE;
1280 else if (wantline < 0)
1281 // If "wantline" is negative it actually means zero.
1282 wantline = 0;
1283 if (wantcol < 0)
1284 // If "wantcol" is negative it actually means zero.
1285 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001286 }
1287
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001288 if (wp->w_popup_pos == POPPOS_CENTER)
1289 {
1290 // center after computing the size
1291 center_vert = TRUE;
1292 center_hor = TRUE;
1293 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001294 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001295 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001296 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1297 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001298 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001299 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001300 if (wp->w_winrow >= Rows)
1301 wp->w_winrow = Rows - 1;
1302 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001303 if (wp->w_popup_pos == POPPOS_BOTTOM)
1304 // assume that each buffer line takes one screen line
Bram Moolenaar37fef162022-08-29 18:16:32 +01001305 wp->w_winrow = MAX(cmdline_row
1306 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001307
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001308 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001309 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001310 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1311 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001312 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001313 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001314 // Need to see at least one character after the decoration.
1315 if (wp->w_wincol > Columns - left_extra - 1)
1316 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001317 }
1318 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001319
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001320 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001321 // When left aligned use the space available, but shift to the left when we
1322 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001323 maxspace = Columns - wp->w_wincol - left_extra;
1324 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001325 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001326 {
1327 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001328 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001329 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001330
1331 if (wp->w_p_nu || wp->w_p_rnu)
1332 margin_width = number_width(wp) + 1;
1333#ifdef FEAT_FOLDING
1334 margin_width += wp->w_p_fdc;
1335#endif
1336#ifdef FEAT_SIGNS
1337 if (signcolumn_on(wp))
1338 margin_width += 2;
1339#endif
1340 if (margin_width >= maxwidth)
1341 margin_width = maxwidth - 1;
1342
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001343 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001344 minheight = wp->w_minheight;
1345#ifdef FEAT_TERMINAL
1346 // A terminal popup initially does not have content, use a default minimal
1347 // width of 20 characters and height of 5 lines.
1348 if (wp->w_buffer->b_term != NULL)
1349 {
1350 if (minwidth == 0)
1351 minwidth = 20;
1352 if (minheight == 0)
1353 minheight = 5;
1354 }
1355#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001356
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001357 if (wp->w_maxheight > 0)
1358 maxheight = wp->w_maxheight;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01001359 else if (wp->w_popup_pos == POPPOS_BOTTOM)
1360 maxheight = cmdline_row - 1;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001361
Bram Moolenaar8d241042019-06-12 23:40:01 +02001362 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001363 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001364 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001365 if (wp->w_topline < 1)
1366 wp->w_topline = 1;
1367 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001368 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1369
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001370 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001371 // Use a minimum width of one, so that something shows when there is no
1372 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001373 // When "firstline" is -1 then start with the last buffer line and go
1374 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001375 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001376 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001377 if (wp->w_firstline < 0)
1378 lnum = wp->w_buffer->b_ml.ml_line_count;
1379 else
1380 lnum = wp->w_topline;
1381 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001382 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001383 int len;
1384 int w_width = wp->w_width;
1385
1386 // Count Tabs for what they are worth and compute the length based on
1387 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001388 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001389 if (wp->w_width < maxwidth)
1390 wp->w_width = maxwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001391 len = win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001392 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001393 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001394
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001395 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001396 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001397 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001398 {
1399 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001400 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001401 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001402 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001403 }
1404 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001405 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001406 && allow_adjust_left
1407 && (wp->w_popup_pos == POPPOS_TOPLEFT
1408 || wp->w_popup_pos == POPPOS_BOTLEFT))
1409 {
1410 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001411 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001412
Bram Moolenaar51c31312019-06-15 22:27:23 +02001413 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001414 {
1415 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001416
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001417 len -= truncate_shift;
1418 shift_by -= truncate_shift;
1419 }
1420
1421 wp->w_wincol -= shift_by;
1422 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001423 wp->w_width = maxwidth;
1424 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001425 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001426 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001427 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001428 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1429 wp->w_width = wp->w_maxwidth;
1430 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001431
1432 if (wp->w_firstline < 0)
1433 --lnum;
1434 else
1435 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001436
1437 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001438 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001439 && (wp->w_firstline >= 0
1440 ? lnum - wp->w_topline
1441 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001442 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001443 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001444 }
1445
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001446 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001447 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001448
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001449 wp->w_has_scrollbar = wp->w_want_scrollbar
1450 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001451#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001452 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1453 // Terminal window with running job never has a scrollbar, adjusts to
1454 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001455 wp->w_has_scrollbar = FALSE;
1456#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001457 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001458 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001459 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001460 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001461 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001462 // make space for the scrollbar if needed, when lines wrap and when
1463 // applying minwidth
1464 if (maxwidth + right_extra >= maxspace
1465 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1466 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001467 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001468
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001469 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1470 {
1471 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1472
1473 if (minwidth < title_len)
1474 minwidth = title_len;
1475 }
1476
1477 if (minwidth > 0 && wp->w_width < minwidth)
1478 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001479 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001480 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001481 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001482 // some columns cut off on the right
1483 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001484
1485 // If the window doesn't fit because 'minwidth' is set then the
1486 // scrollbar is at the far right of the screen, use the size without
1487 // the scrollbar.
1488 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1489 {
1490 int off = wp->w_width - maxwidth;
1491
1492 if (off > right_extra)
1493 extra_width -= right_extra;
1494 else
1495 extra_width -= off;
1496 wp->w_width = maxwidth_no_scrollbar;
1497 }
1498 else
1499 {
1500 wp->w_width = maxwidth;
1501
1502 // when adding a scrollbar below need to adjust the width
1503 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1504 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001505 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001506 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001507 {
1508 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1509 if (wp->w_wincol < 0)
1510 wp->w_wincol = 0;
1511 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001512 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1513 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1514 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001515 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001516
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001517 // Right aligned: move to the right if needed.
1518 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001519 if (leftoff >= 0)
1520 wp->w_wincol = leftoff;
1521 else if (wp->w_popup_fixed)
1522 {
1523 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001524 // columns when displaying the window, minus left border and
1525 // padding.
1526 if (-leftoff > left_extra)
1527 wp->w_leftcol = -leftoff - left_extra;
1528 wp->w_width -= wp->w_leftcol;
1529 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001530 if (wp->w_width < 0)
1531 wp->w_width = 0;
1532 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001533 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001534
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001535 if (wp->w_p_wrap || (!wp->w_popup_fixed
1536 && (wp->w_popup_pos == POPPOS_TOPLEFT
1537 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001538 {
1539 int want_col = 0;
1540
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001541 // try to show the right border and any scrollbar
1542 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001543 if (want_col > 0 && wp->w_wincol > 0
1544 && wp->w_wincol + want_col >= Columns)
1545 {
1546 wp->w_wincol = Columns - want_col;
1547 if (wp->w_wincol < 0)
1548 wp->w_wincol = 0;
1549 }
1550 }
1551
Bram Moolenaar8d241042019-06-12 23:40:01 +02001552 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1553 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001554 if (minheight > 0 && wp->w_height < minheight)
1555 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001556 if (maxheight > 0 && wp->w_height > maxheight)
1557 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001558 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001559 if (wp->w_height > Rows - wp->w_winrow)
1560 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001561
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001562 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001563 {
1564 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1565 if (wp->w_winrow < 0)
1566 wp->w_winrow = 0;
1567 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001568 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001569 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001570 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001571 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001572 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001573 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001574 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1575 {
1576 // Bottom aligned but does not fit, and less space on the other
1577 // side or "posinvert" is off: reduce height.
1578 wp->w_winrow = 0;
1579 wp->w_height = wantline - extra_height;
1580 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001581 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001582 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001583 // Not enough space and more space on the other side: make top
1584 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001585 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001586 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001587 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001588 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001589 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1590 || wp->w_popup_pos == POPPOS_TOPLEFT)
1591 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001592 if (wp != popup_dragwin
1593 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001594 && wantline * 2 > Rows
1595 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001596 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001597 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001598 // make bottom aligned and recompute the height
1599 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001600 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001601 if (wp->w_winrow < 0)
1602 {
1603 wp->w_height += wp->w_winrow;
1604 wp->w_winrow = 0;
1605 }
1606 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001607 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001608 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001609 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001610 adjust_height_for_top_aligned = TRUE;
1611 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001612 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001613
1614 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1615 && wp->w_winrow + wp->w_height + extra_height > Rows)
1616 {
1617 // Bottom of the popup goes below the last line, reduce the height and
1618 // add a scrollbar.
1619 wp->w_height = Rows - wp->w_winrow - extra_height;
1620#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001621 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001622#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001623 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001624 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001625 if (width_with_scrollbar > 0)
1626 wp->w_width = width_with_scrollbar;
1627 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001628 }
1629
1630 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001631 if (wp->w_winrow >= Rows)
1632 wp->w_winrow = Rows - 1;
1633 else if (wp->w_winrow < 0)
1634 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001635
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001636 if (wp->w_height != org_height)
1637 win_comp_scroll(wp);
1638
Bram Moolenaar17146962019-05-30 00:12:11 +02001639 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001640 if (win_valid(wp->w_popup_prop_win))
1641 {
1642 wp->w_popup_prop_changedtick =
1643 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1644 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1645 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001646
1647 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001648 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001649 if (org_winrow != wp->w_winrow
1650 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001651 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001652 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001653 || org_width != wp->w_width
1654 || org_height != wp->w_height)
1655 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001656 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001657 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1658 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001659 popup_mask_refresh = TRUE;
1660 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001661}
1662
Bram Moolenaar17627312019-06-02 19:53:44 +02001663typedef enum
1664{
1665 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001666 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001667 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001668 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001669 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001670 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001671 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001672 TYPE_PREVIEW, // preview window
1673 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001674} create_type_T;
1675
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001676/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001677 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1678 */
1679 static int
1680popup_is_notification(create_type_T type)
1681{
1682 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1683}
1684
1685/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001686 * Make "buf" empty and set the contents to "text".
1687 * Used by popup_create() and popup_settext().
1688 */
1689 static void
1690popup_set_buffer_text(buf_T *buf, typval_T text)
1691{
1692 int lnum;
1693
1694 // Clear the buffer, then replace the lines.
1695 curbuf = buf;
1696 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001697 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001698 curbuf = curwin->w_buffer;
1699
1700 // Add text to the buffer.
1701 if (text.v_type == VAR_STRING)
1702 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001703 char_u *s = text.vval.v_string;
1704
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001705 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001706 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001707 }
1708 else
1709 {
1710 list_T *l = text.vval.v_list;
1711
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001712 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001713 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001714 if (l->lv_first == &range_list_item)
1715 emsg(_(e_using_number_as_string));
1716 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001717 // list of strings
1718 add_popup_strings(buf, l);
1719 else
1720 // list of dictionaries
1721 add_popup_dicts(buf, l);
1722 }
1723 }
1724
1725 // delete the line that was in the empty buffer
1726 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001727 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001728 curbuf = curwin->w_buffer;
1729}
1730
1731/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001732 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1733 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001734 * Return FAIL if the parsing fails.
1735 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001736 static int
1737parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001738{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001739 char_u *p =
1740#ifdef FEAT_QUICKFIX
1741 !is_preview ? p_cpp :
1742#endif
1743 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001744
Bram Moolenaar258cef52019-08-21 17:29:29 +02001745 if (wp != NULL)
1746 wp->w_popup_flags &= ~POPF_INFO_MENU;
1747
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001748 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001749 {
1750 char_u *e, *dig;
1751 char_u *s = p;
1752 int x;
1753
1754 e = vim_strchr(p, ':');
1755 if (e == NULL || e[1] == NUL)
1756 return FAIL;
1757
1758 p = vim_strchr(e, ',');
1759 if (p == NULL)
1760 p = e + STRLEN(e);
1761 dig = e + 1;
1762 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001763
1764 if (STRNCMP(s, "height:", 7) == 0)
1765 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001766 if (dig != p)
1767 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001768 if (wp != NULL)
1769 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001770 if (is_preview)
1771 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001772 wp->w_maxheight = x;
1773 }
1774 }
1775 else if (STRNCMP(s, "width:", 6) == 0)
1776 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001777 if (dig != p)
1778 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001779 if (wp != NULL)
1780 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001781 if (is_preview)
1782 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001783 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001784 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001785 }
1786 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001787 else if (STRNCMP(s, "highlight:", 10) == 0)
1788 {
1789 if (wp != NULL)
1790 {
1791 int c = *p;
1792
1793 *p = NUL;
1794 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1795 s + 10, OPT_FREE|OPT_LOCAL, 0);
1796 *p = c;
1797 }
1798 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001799 else if (STRNCMP(s, "border:", 7) == 0)
1800 {
1801 char_u *arg = s + 7;
1802 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1803 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1804 int i;
1805
1806 if (!on && !off)
1807 return FAIL;
1808 if (wp != NULL)
1809 {
1810 for (i = 0; i < 4; ++i)
1811 wp->w_popup_border[i] = on ? 1 : 0;
1812 if (off)
1813 // only show the X for close when there is a border
1814 wp->w_popup_close = POPCLOSE_NONE;
1815 }
1816 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001817 else if (STRNCMP(s, "align:", 6) == 0)
1818 {
1819 char_u *arg = s + 6;
1820 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1821 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1822
1823 if (!menu && !item)
1824 return FAIL;
1825 if (wp != NULL && menu)
1826 wp->w_popup_flags |= POPF_INFO_MENU;
1827 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001828 else
1829 return FAIL;
1830 }
1831 return OK;
1832}
1833
1834/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001835 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1836 * is not NULL.
1837 * Return FAIL if the parsing fails.
1838 */
1839 int
1840parse_previewpopup(win_T *wp)
1841{
1842 return parse_popup_option(wp, TRUE);
1843}
1844
1845/*
1846 * Parse the 'completepopup' option and apply the values to window "wp" if it
1847 * is not NULL.
1848 * Return FAIL if the parsing fails.
1849 */
1850 int
1851parse_completepopup(win_T *wp)
1852{
1853 return parse_popup_option(wp, FALSE);
1854}
1855
1856/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001857 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001858 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001859 */
1860 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001861popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001862{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001863 poppos_T ppt = POPPOS_NONE;
1864
1865 if (d != NULL)
1866 ppt = get_pos_entry(d, FALSE);
1867
Bram Moolenaar79648732019-07-18 21:43:07 +02001868 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001869 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001870 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001871 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1872 }
1873 else
1874 {
1875 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1876 if (wp->w_wantline == 0) // cursor in first line
1877 {
1878 wp->w_wantline = 2;
1879 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1880 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1881 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001882 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001883
Bram Moolenaar79648732019-07-18 21:43:07 +02001884 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001885 if (wp->w_wantcol > Columns - width)
1886 {
1887 wp->w_wantcol = Columns - width;
1888 if (wp->w_wantcol < 1)
1889 wp->w_wantcol = 1;
1890 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001891
Bram Moolenaar79648732019-07-18 21:43:07 +02001892 popup_adjust_position(wp);
1893}
1894
1895/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001896 * Set w_wantline and w_wantcol for the a given screen position.
1897 * Caller must take care of running into the window border.
1898 */
1899 void
1900popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1901{
1902 wp->w_wantline = row;
1903 wp->w_wantcol = col;
1904 popup_adjust_position(wp);
1905}
1906
1907/*
1908 * Add a border and lef&right padding.
1909 */
1910 static void
1911add_border_left_right_padding(win_T *wp)
1912{
1913 int i;
1914
1915 for (i = 0; i < 4; ++i)
1916 {
1917 wp->w_popup_border[i] = 1;
1918 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1919 }
1920}
1921
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001922#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001923/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001924 * Return TRUE if there is any popup window with a terminal buffer.
1925 */
1926 static int
1927popup_terminal_exists(void)
1928{
1929 win_T *wp;
1930 tabpage_T *tp;
1931
1932 FOR_ALL_POPUPWINS(wp)
1933 if (wp->w_buffer->b_term != NULL)
1934 return TRUE;
1935 FOR_ALL_TABPAGES(tp)
1936 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1937 if (wp->w_buffer->b_term != NULL)
1938 return TRUE;
1939 return FALSE;
1940}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001941#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001942
1943/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001944 * Mark all popup windows in the current tab and global for redrawing.
1945 */
1946 void
1947popup_redraw_all(void)
1948{
1949 win_T *wp;
1950
1951 FOR_ALL_POPUPWINS(wp)
1952 wp->w_redr_type = UPD_NOT_VALID;
1953 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1954 wp->w_redr_type = UPD_NOT_VALID;
1955}
1956
1957/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001958 * Set the color for a notification window.
1959 */
1960 static void
1961popup_update_color(win_T *wp, create_type_T type)
1962{
1963 char *hiname = type == TYPE_MESSAGE_WIN
1964 ? "MessageWindow" : "PopupNotification";
1965 int nr = syn_name2id((char_u *)hiname);
1966
1967 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1968 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1969 OPT_FREE|OPT_LOCAL, 0);
1970}
1971
1972/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001973 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001974 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001975 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001976 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001977 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001978 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001979popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001980{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001981 win_T *wp;
1982 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001983 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001984 int new_buffer;
1985 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001986 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001987 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001988
Bram Moolenaar79648732019-07-18 21:43:07 +02001989 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001990 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001991 if (in_vim9script()
1992 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1993 || check_for_dict_arg(argvars, 1) == FAIL))
1994 return NULL;
1995
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001996 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001997 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001998 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001999 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002000 if (buf == NULL)
2001 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002002 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002003 return NULL;
2004 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002005#ifdef FEAT_TERMINAL
2006 if (buf->b_term != NULL && popup_terminal_exists())
2007 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002008 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002009 return NULL;
2010 }
2011#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002012 }
2013 else if (!(argvars[0].v_type == VAR_STRING
2014 && argvars[0].vval.v_string != NULL)
2015 && !(argvars[0].v_type == VAR_LIST
2016 && argvars[0].vval.v_list != NULL))
2017 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002018 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002019 return NULL;
2020 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002021 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002022 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002023 d = argvars[1].vval.v_dict;
2024 }
2025
2026 if (d != NULL)
2027 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002028 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002029 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002030 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002031 tabnr = -1; // notifications are global by default
2032 else
2033 tabnr = 0;
2034 if (tabnr > 0)
2035 {
2036 tp = find_tabpage(tabnr);
2037 if (tp == NULL)
2038 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002039 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002040 return NULL;
2041 }
2042 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002043 }
2044
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002045 // Create the window and buffer.
2046 wp = win_alloc_popup_win();
2047 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002048 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002049 if (rettv != NULL)
2050 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002051 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002052 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002053
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002054 if (buf != NULL)
2055 {
2056 // use existing buffer
2057 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002058 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002059 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002060 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002061 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002062 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002063 }
2064 else
2065 {
2066 // create a new buffer associated with the popup
2067 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002068 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002069 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002070 {
2071 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002072 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002073 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002074 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002075
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002076 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002077
Bram Moolenaar46451042019-08-24 15:50:46 +02002078 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002079 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002080 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002081 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002082 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002083 buf->b_p_ul = -1; // no undo
2084 buf->b_p_swf = FALSE; // no swap file
2085 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002086 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002087
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002088 // Avoid that 'buftype' is reset when this buffer is entered.
2089 buf->b_p_initialized = TRUE;
2090 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002091 wp->w_p_wrap = TRUE; // 'wrap' is default on
2092 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002093
Bram Moolenaara3fce622019-06-20 02:31:49 +02002094 if (tp != NULL)
2095 {
2096 // popup on specified tab page
2097 wp->w_next = tp->tp_first_popupwin;
2098 tp->tp_first_popupwin = wp;
2099 }
2100 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002101 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002102 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002103 wp->w_next = curtab->tp_first_popupwin;
2104 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002105 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002106 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002107 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002108 win_T *prev = first_popupwin;
2109
2110 // Global popup: add at the end, so that it gets displayed on top of
2111 // older ones with the same zindex. Matters for notifications.
2112 if (first_popupwin == NULL)
2113 first_popupwin = wp;
2114 else
2115 {
2116 while (prev->w_next != NULL)
2117 prev = prev->w_next;
2118 prev->w_next = wp;
2119 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002120 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002121
Bram Moolenaar79648732019-07-18 21:43:07 +02002122 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002123 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002124
Bram Moolenaar79648732019-07-18 21:43:07 +02002125 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002126 {
2127 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002128 }
2129 if (type == TYPE_ATCURSOR)
2130 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002131 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002132 set_moved_values(wp);
2133 set_moved_columns(wp, FIND_STRING);
2134 }
2135
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002136 if (type == TYPE_BEVAL)
2137 {
2138 wp->w_popup_pos = POPPOS_BOTLEFT;
2139
2140 // by default use the mouse position
2141 wp->w_wantline = mouse_row;
2142 if (wp->w_wantline <= 0) // mouse on first line
2143 {
2144 wp->w_wantline = 2;
2145 wp->w_popup_pos = POPPOS_TOPLEFT;
2146 }
2147 wp->w_wantcol = mouse_col + 1;
2148 set_mousemoved_values(wp);
2149 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2150 }
2151
Bram Moolenaar33796b32019-06-08 16:01:13 +02002152 // set default values
2153 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002154 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002155
Bram Moolenaar9198de32022-08-27 21:30:03 +01002156 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002157 {
2158 win_T *twp, *nextwin;
2159 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002160
2161 // Try to not overlap with another global popup. Guess we need 3
2162 // more screen lines than buffer lines.
2163 wp->w_wantline = 1;
2164 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2165 {
2166 nextwin = twp->w_next;
2167 if (twp != wp
2168 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2169 && twp->w_winrow <= wp->w_wantline - 1 + height
2170 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2171 {
2172 // move to below this popup and restart the loop to check for
2173 // overlap with other popups
2174 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2175 nextwin = first_popupwin;
2176 }
2177 }
2178 if (wp->w_wantline + height > Rows)
2179 {
2180 // can't avoid overlap, put on top in the hope that message goes
2181 // away soon.
2182 wp->w_wantline = 1;
2183 }
2184
2185 wp->w_wantcol = 10;
2186 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002187 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002188 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002189 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002190 for (i = 0; i < 4; ++i)
2191 wp->w_popup_border[i] = 1;
2192 wp->w_popup_padding[1] = 1;
2193 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002194
Bram Moolenaar9198de32022-08-27 21:30:03 +01002195 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002196 }
2197
Bram Moolenaara730e552019-06-16 19:05:31 +02002198 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002199 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002200 wp->w_popup_pos = POPPOS_CENTER;
2201 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002202 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002203 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002204 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002205 }
2206
Bram Moolenaara730e552019-06-16 19:05:31 +02002207 if (type == TYPE_MENU)
2208 {
2209 typval_T tv;
2210 callback_T callback;
2211
2212 tv.v_type = VAR_STRING;
2213 tv.vval.v_string = (char_u *)"popup_filter_menu";
2214 callback = get_callback(&tv);
2215 if (callback.cb_name != NULL)
2216 set_callback(&wp->w_filter_cb, &callback);
2217
2218 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002219 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002220 }
2221
Bram Moolenaar79648732019-07-18 21:43:07 +02002222 if (type == TYPE_PREVIEW)
2223 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002224 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002225 wp->w_popup_close = POPCLOSE_BUTTON;
2226 for (i = 0; i < 4; ++i)
2227 wp->w_popup_border[i] = 1;
2228 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002229 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002230 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002231# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002232 if (type == TYPE_INFO)
2233 {
2234 wp->w_popup_pos = POPPOS_TOPLEFT;
2235 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2236 wp->w_popup_close = POPCLOSE_BUTTON;
2237 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002238 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002239 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002240# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002241
Bram Moolenaarae943152019-06-16 22:54:14 +02002242 for (i = 0; i < 4; ++i)
2243 VIM_CLEAR(wp->w_border_highlight[i]);
2244 for (i = 0; i < 8; ++i)
2245 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002246 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002247 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002248 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002249
Bram Moolenaar79648732019-07-18 21:43:07 +02002250 if (d != NULL)
2251 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002252 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002253
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002254#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002255 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2256 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002257#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002258
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002259 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002260
2261 wp->w_vsep_width = 0;
2262
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002263 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002264 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002265
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002266#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002267 // When running a terminal in the popup it becomes the current window.
2268 if (buf->b_term != NULL)
2269 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002270#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002271
Bram Moolenaara730e552019-06-16 19:05:31 +02002272 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002273}
2274
2275/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002276 * popup_clear()
2277 */
2278 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002279f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002280{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002281 int force = FALSE;
2282
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002283 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2284 return;
2285
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002286 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002287 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002288 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002289}
2290
2291/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002292 * popup_create({text}, {options})
2293 */
2294 void
2295f_popup_create(typval_T *argvars, typval_T *rettv)
2296{
Bram Moolenaar17627312019-06-02 19:53:44 +02002297 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002298}
2299
2300/*
2301 * popup_atcursor({text}, {options})
2302 */
2303 void
2304f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2305{
Bram Moolenaar17627312019-06-02 19:53:44 +02002306 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002307}
2308
2309/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002310 * popup_beval({text}, {options})
2311 */
2312 void
2313f_popup_beval(typval_T *argvars, typval_T *rettv)
2314{
2315 popup_create(argvars, rettv, TYPE_BEVAL);
2316}
2317
2318/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002319 * Invoke the close callback for window "wp" with value "result".
2320 * Careful: The callback may make "wp" invalid!
2321 */
2322 static void
2323invoke_popup_callback(win_T *wp, typval_T *result)
2324{
2325 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002326 typval_T argv[3];
2327
2328 argv[0].v_type = VAR_NUMBER;
2329 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2330
2331 if (result != NULL && result->v_type != VAR_UNKNOWN)
2332 copy_tv(result, &argv[1]);
2333 else
2334 {
2335 argv[1].v_type = VAR_NUMBER;
2336 argv[1].vval.v_number = 0;
2337 }
2338
2339 argv[2].v_type = VAR_UNKNOWN;
2340
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002341 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002342 if (result != NULL)
2343 clear_tv(&argv[1]);
2344 clear_tv(&rettv);
2345}
2346
2347/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002348 * Make "prevwin" the current window, unless it's equal to "wp".
2349 * Otherwise make "firstwin" the current window.
2350 */
2351 static void
2352back_to_prevwin(win_T *wp)
2353{
2354 if (win_valid(prevwin) && wp != prevwin)
2355 win_enter(prevwin, FALSE);
2356 else
2357 win_enter(firstwin, FALSE);
2358}
2359
2360/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002361 * Close popup "wp" and invoke any close callback for it.
2362 */
2363 static void
2364popup_close_and_callback(win_T *wp, typval_T *arg)
2365{
2366 int id = wp->w_id;
2367
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002368#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002369 if (wp == curwin && curbuf->b_term != NULL)
2370 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002371 win_T *owp;
2372
2373 // Closing popup window with a terminal: put focus back on the first
2374 // that works:
2375 // - another popup window with a terminal
2376 // - the previous window
2377 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002378 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002379 if (owp != curwin && owp->w_buffer->b_term != NULL)
2380 break;
2381 if (owp != NULL)
2382 win_enter(owp, FALSE);
2383 else
2384 {
2385 for (owp = curtab->tp_first_popupwin; owp != NULL;
2386 owp = owp->w_next)
2387 if (owp != curwin && owp->w_buffer->b_term != NULL)
2388 break;
2389 if (owp != NULL)
2390 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002391 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002392 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002393 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002394 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002395#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002396
Bram Moolenaar49540192019-12-11 19:34:54 +01002397 // Just in case a check higher up is missing.
2398 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002399 {
2400 // To avoid getting stuck when win_execute() does something that causes
2401 // an error, stop calling the filter callback.
2402 free_callback(&wp->w_filter_cb);
2403
Bram Moolenaar49540192019-12-11 19:34:54 +01002404 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002405 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002406
Bram Moolenaarcee52202020-03-11 14:19:58 +01002407 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002408 if (wp->w_close_cb.cb_name != NULL)
2409 // Careful: This may make "wp" invalid.
2410 invoke_popup_callback(wp, arg);
2411
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002412 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002413 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002414}
2415
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002416 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002417popup_close_with_retval(win_T *wp, int retval)
2418{
2419 typval_T res;
2420
2421 res.v_type = VAR_NUMBER;
2422 res.vval.v_number = retval;
2423 popup_close_and_callback(wp, &res);
2424}
2425
Bram Moolenaar3397f742019-06-02 18:40:06 +02002426/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002427 * Close popup "wp" because of a mouse click.
2428 */
2429 void
2430popup_close_for_mouse_click(win_T *wp)
2431{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002432 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002433}
2434
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002435 static void
2436check_mouse_moved(win_T *wp, win_T *mouse_wp)
2437{
2438 // Close the popup when all if these are true:
2439 // - the mouse is not on this popup
2440 // - "mousemoved" was used
2441 // - the mouse is no longer on the same screen row or the mouse column is
2442 // outside of the relevant text
2443 if (wp != mouse_wp
2444 && wp->w_popup_mouse_row != 0
2445 && (wp->w_popup_mouse_row != mouse_row
2446 || mouse_col < wp->w_popup_mouse_mincol
2447 || mouse_col > wp->w_popup_mouse_maxcol))
2448 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002449 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002450 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002451 }
2452}
2453
2454/*
2455 * Called when the mouse moved: may close a popup with "mousemoved".
2456 */
2457 void
2458popup_handle_mouse_moved(void)
2459{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002460 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002461 win_T *mouse_wp;
2462 int row = mouse_row;
2463 int col = mouse_col;
2464
2465 // find the window where the mouse is in
2466 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2467
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002468 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2469 {
2470 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002471 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002472 }
2473 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2474 {
2475 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002476 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002477 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002478}
2479
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002480/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002481 * In a filter: check if the typed key is a mouse event that is used for
2482 * dragging the popup.
2483 */
2484 static void
2485filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2486{
2487 int row = mouse_row;
2488 int col = mouse_col;
2489
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002490 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002491 && is_mouse_key(c)
2492 && (wp == popup_dragwin
2493 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2494 // do not consume the key, allow for dragging the popup
2495 rettv->vval.v_number = 0;
2496}
2497
Bram Moolenaara730e552019-06-16 19:05:31 +02002498/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002499 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002500 */
2501 void
2502f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2503{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002504 int id;
2505 win_T *wp;
2506 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002507 typval_T res;
2508 int c;
2509 linenr_T old_lnum;
2510
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002511 if (in_vim9script()
2512 && (check_for_number_arg(argvars, 0) == FAIL
2513 || check_for_string_arg(argvars, 1) == FAIL))
2514 return;
2515
2516 id = tv_get_number(&argvars[0]);
2517 wp = win_id2wp(id);
2518 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002519 // If the popup has been closed do not consume the key.
2520 if (wp == NULL)
2521 return;
2522
2523 c = *key;
2524 if (c == K_SPECIAL && key[1] != NUL)
2525 c = TO_SPECIAL(key[1], key[2]);
2526
2527 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002528 rettv->v_type = VAR_BOOL;
2529 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002530 res.v_type = VAR_NUMBER;
2531
2532 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002533 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2534 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002535 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002536 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002537 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2538 ++wp->w_cursor.lnum;
2539 if (old_lnum != wp->w_cursor.lnum)
2540 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002541 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002542 return;
2543 }
2544
2545 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2546 {
2547 // Cancelled, invoke callback with -1
2548 res.vval.v_number = -1;
2549 popup_close_and_callback(wp, &res);
2550 return;
2551 }
2552 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2553 {
2554 // Invoke callback with current index.
2555 res.vval.v_number = wp->w_cursor.lnum;
2556 popup_close_and_callback(wp, &res);
2557 return;
2558 }
2559
2560 filter_handle_drag(wp, c, rettv);
2561}
2562
2563/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002564 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002565 */
2566 void
2567f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2568{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002569 int id;
2570 win_T *wp;
2571 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002572 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002573 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002574
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002575 if (in_vim9script()
2576 && (check_for_number_arg(argvars, 0) == FAIL
2577 || check_for_string_arg(argvars, 1) == FAIL))
2578 return;
2579
2580 id = tv_get_number(&argvars[0]);
2581 wp = win_id2wp(id);
2582 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002583 // If the popup has been closed don't consume the key.
2584 if (wp == NULL)
2585 return;
2586
Bram Moolenaara730e552019-06-16 19:05:31 +02002587 c = *key;
2588 if (c == K_SPECIAL && key[1] != NUL)
2589 c = TO_SPECIAL(key[1], key[2]);
2590
Bram Moolenaara42d9452019-06-15 21:46:30 +02002591 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002592 rettv->v_type = VAR_BOOL;
2593 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002594
Bram Moolenaara730e552019-06-16 19:05:31 +02002595 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002596 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002597 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002598 res.vval.v_number = 0;
2599 else
2600 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002601 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002602 return;
2603 }
2604
2605 // Invoke callback
2606 res.v_type = VAR_NUMBER;
2607 popup_close_and_callback(wp, &res);
2608}
2609
2610/*
2611 * popup_dialog({text}, {options})
2612 */
2613 void
2614f_popup_dialog(typval_T *argvars, typval_T *rettv)
2615{
2616 popup_create(argvars, rettv, TYPE_DIALOG);
2617}
2618
2619/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002620 * popup_menu({text}, {options})
2621 */
2622 void
2623f_popup_menu(typval_T *argvars, typval_T *rettv)
2624{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002625 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002626}
2627
2628/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002629 * popup_notification({text}, {options})
2630 */
2631 void
2632f_popup_notification(typval_T *argvars, typval_T *rettv)
2633{
2634 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2635}
2636
2637/*
2638 * Find the popup window with window-ID "id".
2639 * If the popup window does not exist NULL is returned.
2640 * If the window is not a popup window, and error message is given.
2641 */
2642 static win_T *
2643find_popup_win(int id)
2644{
2645 win_T *wp = win_id2wp(id);
2646
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002647 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002648 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002649 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002650 return NULL;
2651 }
2652 return wp;
2653}
2654
2655/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002656 * popup_close({id})
2657 */
2658 void
2659f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2660{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002661 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002662 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002663
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002664 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2665 return;
2666
2667 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002668 if (
2669# ifdef FEAT_TERMINAL
2670 // if the popup contains a terminal it will become hidden
2671 curbuf->b_term == NULL &&
2672# endif
2673 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002674 return;
2675
2676 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002677 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002678 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002679}
2680
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002681 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002682popup_hide(win_T *wp)
2683{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002684#ifdef FEAT_TERMINAL
2685 if (error_if_term_popup_window())
2686 return;
2687#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002688 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2689 {
2690 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002691 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002692 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002693 popup_mask_refresh = TRUE;
2694 }
2695}
2696
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002697/*
2698 * popup_hide({id})
2699 */
2700 void
2701f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2702{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002703 int id;
2704 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002705
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002706 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2707 return;
2708
2709 id = (int)tv_get_number(argvars);
2710 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002711 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002712 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002713 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002714 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2715 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002716}
2717
2718 void
2719popup_show(win_T *wp)
2720{
2721 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002722 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002723 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002724 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002725 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002726 }
2727}
2728
2729/*
2730 * popup_show({id})
2731 */
2732 void
2733f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2734{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002735 int id;
2736 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002737
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002738 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2739 return;
2740
2741 id = (int)tv_get_number(argvars);
2742 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002743 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002744 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002745 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002746 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002747#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002748 if (wp->w_popup_flags & POPF_INFO)
2749 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002750#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002751 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002752}
2753
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002754/*
2755 * popup_settext({id}, {text})
2756 */
2757 void
2758f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2759{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002760 int id;
2761 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002762
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002763 if (in_vim9script()
2764 && (check_for_number_arg(argvars, 0) == FAIL
2765 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2766 return;
2767
2768 id = (int)tv_get_number(&argvars[0]);
2769 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002770 if (wp != NULL)
2771 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002772 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002773 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002774 else
2775 {
2776 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002777 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002778 popup_adjust_position(wp);
2779 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002780 }
2781}
2782
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002783 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002784popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002785{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002786 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002787 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002788 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002789 clear_cmdline = TRUE;
2790 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002791
Bram Moolenaar43568642022-08-28 13:02:45 +01002792#ifdef HAS_MESSAGE_WINDOW
2793 if (wp == message_win)
2794 message_win = NULL;
2795#endif
2796
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002797 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002798 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002799}
2800
Bram Moolenaar82106932020-03-13 14:34:38 +01002801 static void
2802error_for_popup_window(void)
2803{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002804 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002805}
2806
2807 int
2808error_if_popup_window(int also_with_term UNUSED)
2809{
2810 // win_execute() may set "curwin" to a popup window temporarily, but many
2811 // commands are disallowed then. When a terminal runs in the popup most
2812 // things are allowed. When a terminal is finished it can be closed.
2813 if (WIN_IS_POPUP(curwin)
2814# ifdef FEAT_TERMINAL
2815 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002816# endif
2817 )
2818 {
2819 error_for_popup_window();
2820 return TRUE;
2821 }
2822 return FALSE;
2823}
2824
Bram Moolenaarec583842019-05-26 14:11:23 +02002825/*
2826 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002827 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002828 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002829 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002830 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002831popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002832{
2833 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002834 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002835 win_T *prev = NULL;
2836
Bram Moolenaarec583842019-05-26 14:11:23 +02002837 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002838 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002839 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002840 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002841 if (wp == curwin)
2842 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002843 if (!force)
2844 {
2845 error_for_popup_window();
2846 return FAIL;
2847 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002848 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002849 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002850 if (prev == NULL)
2851 first_popupwin = wp->w_next;
2852 else
2853 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002854 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002855 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002856 }
2857
Bram Moolenaarec583842019-05-26 14:11:23 +02002858 // go through tab-local popups
2859 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002860 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002861 return OK;
2862 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002863}
2864
2865/*
2866 * Close a popup window with Window-id "id" in tabpage "tp".
2867 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002868 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002869popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002870{
2871 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002872 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002873 win_T *prev = NULL;
2874
Bram Moolenaarec583842019-05-26 14:11:23 +02002875 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2876 if (wp->w_id == id)
2877 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002878 if (wp == curwin)
2879 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002880 if (!force)
2881 {
2882 error_for_popup_window();
2883 return FAIL;
2884 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002885 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002886 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002887 if (prev == NULL)
2888 *root = wp->w_next;
2889 else
2890 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002891 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002892 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002893 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002894 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002895}
2896
2897 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002898close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002899{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002900 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002901 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002902 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002903 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002904 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002905 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002906 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002907 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002908}
2909
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002910/*
2911 * popup_move({id}, {options})
2912 */
2913 void
2914f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2915{
Bram Moolenaarae943152019-06-16 22:54:14 +02002916 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002917 int id;
2918 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002919
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002920 if (in_vim9script()
2921 && (check_for_number_arg(argvars, 0) == FAIL
2922 || check_for_dict_arg(argvars, 1) == FAIL))
2923 return;
2924
2925 id = (int)tv_get_number(argvars);
2926 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002927 if (wp == NULL)
2928 return; // invalid {id}
2929
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002930 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002931 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002932 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002933
Bram Moolenaarae943152019-06-16 22:54:14 +02002934 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002935
2936 if (wp->w_winrow + wp->w_height >= cmdline_row)
2937 clear_cmdline = TRUE;
2938 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002939}
2940
Bram Moolenaarbc133542019-05-29 20:26:46 +02002941/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002942 * popup_setoptions({id}, {options})
2943 */
2944 void
2945f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2946{
2947 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002948 int id;
2949 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002950 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002951
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002952 if (in_vim9script()
2953 && (check_for_number_arg(argvars, 0) == FAIL
2954 || check_for_dict_arg(argvars, 1) == FAIL))
2955 return;
2956
2957 id = (int)tv_get_number(argvars);
2958 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002959 if (wp == NULL)
2960 return; // invalid {id}
2961
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002962 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02002963 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002964 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002965 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002966
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002967 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002968
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002969 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002970 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002971 popup_adjust_position(wp);
2972}
2973
2974/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002975 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002976 */
2977 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002978f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002979{
2980 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002981 int id;
2982 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002983 int top_extra;
2984 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002985
2986 if (rettv_dict_alloc(rettv) == OK)
2987 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002988 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2989 return;
2990
2991 id = (int)tv_get_number(argvars);
2992 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002993 if (wp == NULL)
2994 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002995 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002996 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2997
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002998 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002999 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003000 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003001
Bram Moolenaarbc133542019-05-29 20:26:46 +02003002 dict_add_number(dict, "line", wp->w_winrow + 1);
3003 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02003004 dict_add_number(dict, "width", wp->w_width + left_extra
3005 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3006 dict_add_number(dict, "height", wp->w_height + top_extra
3007 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003008
3009 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3010 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3011 dict_add_number(dict, "core_width", wp->w_width);
3012 dict_add_number(dict, "core_height", wp->w_height);
3013
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003014 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02003015 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01003016 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003017 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003018 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003019
3020 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003021 }
3022}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003023
3024/*
3025 * popup_list()
3026 */
3027 void
3028f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3029{
3030 win_T *wp;
3031 tabpage_T *tp;
3032
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003033 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003034 return;
3035 FOR_ALL_POPUPWINS(wp)
3036 list_append_number(rettv->vval.v_list, wp->w_id);
3037 FOR_ALL_TABPAGES(tp)
3038 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3039 list_append_number(rettv->vval.v_list, wp->w_id);
3040}
3041
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003042/*
3043 * popup_locate({row}, {col})
3044 */
3045 void
3046f_popup_locate(typval_T *argvars, typval_T *rettv)
3047{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003048 int row;
3049 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003050 win_T *wp;
3051
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003052 if (in_vim9script()
3053 && (check_for_number_arg(argvars, 0) == FAIL
3054 || check_for_number_arg(argvars, 1) == FAIL))
3055 return;
3056
3057 row = tv_get_number(&argvars[0]) - 1;
3058 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003059 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003060 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003061 rettv->vval.v_number = wp->w_id;
3062}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003063
3064/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003065 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3066 */
3067 static void
3068get_padding_border(dict_T *dict, int *array, char *name)
3069{
3070 list_T *list;
3071 int i;
3072
3073 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3074 return;
3075
3076 list = list_alloc();
3077 if (list != NULL)
3078 {
3079 dict_add_list(dict, name, list);
3080 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3081 for (i = 0; i < 4; ++i)
3082 list_append_number(list, array[i]);
3083 }
3084}
3085
3086/*
3087 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3088 */
3089 static void
3090get_borderhighlight(dict_T *dict, win_T *wp)
3091{
3092 list_T *list;
3093 int i;
3094
3095 for (i = 0; i < 4; ++i)
3096 if (wp->w_border_highlight[i] != NULL)
3097 break;
3098 if (i == 4)
3099 return;
3100
3101 list = list_alloc();
3102 if (list != NULL)
3103 {
3104 dict_add_list(dict, "borderhighlight", list);
3105 for (i = 0; i < 4; ++i)
3106 list_append_string(list, wp->w_border_highlight[i], -1);
3107 }
3108}
3109
3110/*
3111 * For popup_getoptions(): add a "borderchars" entry to "dict".
3112 */
3113 static void
3114get_borderchars(dict_T *dict, win_T *wp)
3115{
3116 list_T *list;
3117 int i;
3118 char_u buf[NUMBUFLEN];
3119 int len;
3120
3121 for (i = 0; i < 8; ++i)
3122 if (wp->w_border_char[i] != 0)
3123 break;
3124 if (i == 8)
3125 return;
3126
3127 list = list_alloc();
3128 if (list != NULL)
3129 {
3130 dict_add_list(dict, "borderchars", list);
3131 for (i = 0; i < 8; ++i)
3132 {
3133 len = mb_char2bytes(wp->w_border_char[i], buf);
3134 list_append_string(list, buf, len);
3135 }
3136 }
3137}
3138
3139/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003140 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003141 */
3142 static void
3143get_moved_list(dict_T *dict, win_T *wp)
3144{
3145 list_T *list;
3146
3147 list = list_alloc();
3148 if (list != NULL)
3149 {
3150 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003151 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003152 list_append_number(list, wp->w_popup_mincol);
3153 list_append_number(list, wp->w_popup_maxcol);
3154 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003155 list = list_alloc();
3156 if (list != NULL)
3157 {
3158 dict_add_list(dict, "mousemoved", list);
3159 list_append_number(list, wp->w_popup_mouse_row);
3160 list_append_number(list, wp->w_popup_mouse_mincol);
3161 list_append_number(list, wp->w_popup_mouse_maxcol);
3162 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003163}
3164
3165/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003166 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003167 */
3168 void
3169f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3170{
3171 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003172 int id;
3173 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003174 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003175 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003176
3177 if (rettv_dict_alloc(rettv) == OK)
3178 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003179 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3180 return;
3181
3182 id = (int)tv_get_number(argvars);
3183 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003184 if (wp == NULL)
3185 return;
3186
3187 dict = rettv->vval.v_dict;
3188 dict_add_number(dict, "line", wp->w_wantline);
3189 dict_add_number(dict, "col", wp->w_wantcol);
3190 dict_add_number(dict, "minwidth", wp->w_minwidth);
3191 dict_add_number(dict, "minheight", wp->w_minheight);
3192 dict_add_number(dict, "maxheight", wp->w_maxheight);
3193 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003194 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003195 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003196 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003197 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003198 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003199 {
3200 proptype_T *pt = text_prop_type_by_id(
3201 wp->w_popup_prop_win->w_buffer,
3202 wp->w_popup_prop_type);
3203
3204 if (pt != NULL)
3205 dict_add_string(dict, "textprop", pt->pt_name);
3206 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3207 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3208 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003209 dict_add_string(dict, "title", wp->w_popup_title);
3210 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003211 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003212 dict_add_number(dict, "dragall",
3213 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003214 dict_add_number(dict, "mapping",
3215 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003216 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003217 dict_add_number(dict, "posinvert",
3218 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003219 dict_add_number(dict, "cursorline",
3220 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003221 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003222 if (wp->w_scrollbar_highlight != NULL)
3223 dict_add_string(dict, "scrollbarhighlight",
3224 wp->w_scrollbar_highlight);
3225 if (wp->w_thumb_highlight != NULL)
3226 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003227
Bram Moolenaara3fce622019-06-20 02:31:49 +02003228 // find the tabpage that holds this popup
3229 i = 1;
3230 FOR_ALL_TABPAGES(tp)
3231 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003232 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003233
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003234 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3235 if (twp->w_id == id)
3236 break;
3237 if (twp != NULL)
3238 break;
3239 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003240 }
3241 if (tp == NULL)
3242 i = -1; // must be global
3243 else if (tp == curtab)
3244 i = 0;
3245 dict_add_number(dict, "tabpage", i);
3246
Bram Moolenaarae943152019-06-16 22:54:14 +02003247 get_padding_border(dict, wp->w_popup_padding, "padding");
3248 get_padding_border(dict, wp->w_popup_border, "border");
3249 get_borderhighlight(dict, wp);
3250 get_borderchars(dict, wp);
3251 get_moved_list(dict, wp);
3252
3253 if (wp->w_filter_cb.cb_name != NULL)
3254 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3255 if (wp->w_close_cb.cb_name != NULL)
3256 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003257
K.Takataeeec2542021-06-02 13:28:16 +02003258 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003259 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3260 {
3261 dict_add_string(dict, "pos",
3262 (char_u *)poppos_entries[i].pp_name);
3263 break;
3264 }
3265
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003266 dict_add_string(dict, "close", (char_u *)(
3267 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3268 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3269
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003270# if defined(FEAT_TIMERS)
3271 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3272 ? (long)wp->w_popup_timer->tr_interval : 0L);
3273# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003274 }
3275}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003276
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003277# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003278/*
3279 * Return TRUE if the current window is running a terminal in a popup window.
3280 * Return FALSE when the job has ended.
3281 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003282 int
3283error_if_term_popup_window()
3284{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003285 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3286 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003287 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003288 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003289 return TRUE;
3290 }
3291 return FALSE;
3292}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003293# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003294
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003295/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003296 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003297 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003298 * Each calling function should use a different flag, see the list at
3299 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003300 */
3301 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003302popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003303{
3304 win_T *wp;
3305
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003306 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003307 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003308 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003309 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003310}
3311
3312/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003313 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003314 * Must have called popup_reset_handled() first.
3315 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3316 * popup with the highest zindex.
3317 */
3318 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003319find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003320{
3321 win_T *wp;
3322 win_T *found_wp;
3323 int found_zindex;
3324
3325 found_zindex = lowest ? INT_MAX : 0;
3326 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003327 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003328 if ((wp->w_popup_handled & handled_flag) == 0
3329 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003330 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003331 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003332 {
3333 found_zindex = wp->w_zindex;
3334 found_wp = wp;
3335 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003336 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003337 if ((wp->w_popup_handled & handled_flag) == 0
3338 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003339 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003340 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003341 {
3342 found_zindex = wp->w_zindex;
3343 found_wp = wp;
3344 }
3345
3346 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003347 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003348 return found_wp;
3349}
3350
3351/*
3352 * Invoke the filter callback for window "wp" with typed character "c".
3353 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003354 * Returns the return value of the filter or -1 for CTRL-C in the current
3355 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003356 * Careful: The filter may make "wp" invalid!
3357 */
3358 static int
3359invoke_popup_filter(win_T *wp, int c)
3360{
3361 int res;
3362 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003363 typval_T argv[3];
3364 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003365 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003366 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003367
Bram Moolenaara42d9452019-06-15 21:46:30 +02003368 // Emergency exit: CTRL-C closes the popup.
3369 if (c == Ctrl_C)
3370 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003371 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003372 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003373
3374 // Reset got_int to avoid the callback isn't called.
3375 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003376 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003377 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003378
3379 // If the popup is the current window it probably fails to close. Then
3380 // do not consume the key.
3381 if (was_curwin && wp == curwin)
3382 return -1;
3383 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003384 }
3385
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003386 argv[0].v_type = VAR_NUMBER;
3387 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3388
3389 // Convert the number to a string, so that the function can use:
3390 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003391 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003392 argv[1].v_type = VAR_STRING;
3393 argv[1].vval.v_string = vim_strsave(buf);
3394
3395 argv[2].v_type = VAR_UNKNOWN;
3396
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003397 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003398 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3399 {
3400 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003401 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003402 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003403 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003404 }
3405 else
3406 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003407 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3408 popup_highlight_curline(wp);
3409
Bram Moolenaar371806e2020-10-22 13:44:54 +02003410 // If an error message was given always return FALSE, so that keys are
3411 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003412 // If we get three errors in a row then close the popup. Decrement the
3413 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3414 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003415 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003416 {
3417 wp->w_filter_errors += 10;
3418 if (wp->w_filter_errors >= 30)
3419 popup_close_with_retval(wp, -1);
3420 res = FALSE;
3421 }
3422 else
3423 {
3424 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3425 --wp->w_filter_errors;
3426 res = tv_get_bool(&rettv);
3427 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003428 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003429
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003430 vim_free(argv[1].vval.v_string);
3431 clear_tv(&rettv);
3432 return res;
3433}
3434
3435/*
3436 * Called when "c" was typed: invoke popup filter callbacks.
3437 * Returns TRUE when the character was consumed,
3438 */
3439 int
3440popup_do_filter(int c)
3441{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003442 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003443 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003444 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003445 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003446 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003447 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003448
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003449#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003450 // Popup window with terminal always gets focus.
3451 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3452 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003453#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003454
Bram Moolenaar934470e2019-09-01 23:27:05 +02003455 if (recursive)
3456 return FALSE;
3457 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003458
Bram Moolenaarf6396232019-08-24 19:36:00 +02003459 if (c == K_LEFTMOUSE)
3460 {
3461 int row = mouse_row;
3462 int col = mouse_col;
3463
3464 wp = mouse_find_win(&row, &col, FIND_POPUP);
3465 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003466 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003467 }
3468
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003469 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003470 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003471 while (res == FALSE
3472 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003473 if (wp->w_filter_cb.cb_name != NULL
3474 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003475 res = invoke_popup_filter(wp, c);
3476
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003477 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003478 {
3479 int save_got_int = got_int;
3480
3481 // Reset got_int to avoid a function used in the statusline aborts.
3482 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003483 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003484 got_int |= save_got_int;
3485 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003486 recursive = FALSE;
3487 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003488
3489 // When interrupted return FALSE to avoid looping.
3490 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003491}
3492
Bram Moolenaar3397f742019-06-02 18:40:06 +02003493/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003494 * Return TRUE if there is a popup visible with a filter callback and the
3495 * "mapping" property off.
3496 */
3497 int
3498popup_no_mapping(void)
3499{
3500 int round;
3501 win_T *wp;
3502
3503 for (round = 1; round <= 2; ++round)
3504 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3505 wp != NULL; wp = wp->w_next)
3506 if (wp->w_filter_cb.cb_name != NULL
3507 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3508 return TRUE;
3509 return FALSE;
3510}
3511
3512/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003513 * Called when the cursor moved: check if any popup needs to be closed if the
3514 * cursor moved far enough.
3515 */
3516 void
3517popup_check_cursor_pos()
3518{
3519 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003520
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003521 popup_reset_handled(POPUP_HANDLED_3);
3522 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003523 if (wp->w_popup_curwin != NULL
3524 && (curwin != wp->w_popup_curwin
3525 || curwin->w_cursor.lnum != wp->w_popup_lnum
3526 || curwin->w_cursor.col < wp->w_popup_mincol
3527 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003528 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003529}
3530
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003531/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003532 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003533 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003534 static void
3535popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003536{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003537 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003538 char_u *cells;
3539 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003540
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003541 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3542 {
3543 vim_free(wp->w_popup_mask_cells);
3544 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003545 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003546 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003547 if (wp->w_popup_mask_cells != NULL
3548 && wp->w_popup_mask_height == height
3549 && wp->w_popup_mask_width == width)
3550 return; // cache is still valid
3551
3552 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003553 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003554 if (wp->w_popup_mask_cells == NULL)
3555 return;
3556 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003557
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003558 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003559 {
3560 int cols, cole;
3561 int lines, linee;
3562
3563 li = lio->li_tv.vval.v_list->lv_first;
3564 cols = tv_get_number(&li->li_tv);
3565 if (cols < 0)
3566 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003567 if (cols <= 0)
3568 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003569 li = li->li_next;
3570 cole = tv_get_number(&li->li_tv);
3571 if (cole < 0)
3572 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003573 if (cole > width)
3574 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003575 li = li->li_next;
3576 lines = tv_get_number(&li->li_tv);
3577 if (lines < 0)
3578 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003579 if (lines <= 0)
3580 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003581 li = li->li_next;
3582 linee = tv_get_number(&li->li_tv);
3583 if (linee < 0)
3584 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003585 if (linee > height)
3586 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003587
Bram Moolenaar4012d262020-12-29 11:57:46 +01003588 for (row = lines - 1; row < linee; ++row)
3589 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003590 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003591 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003592}
3593
3594/*
3595 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3596 * "col" and "line" are screen coordinates.
3597 */
3598 static int
3599popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3600{
3601 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3602 int line = screenline - wp->w_winrow;
3603
3604 return col >= 0 && col < width
3605 && line >= 0 && line < height
3606 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003607}
3608
3609/*
3610 * Set flags in popup_transparent[] for window "wp" to "val".
3611 */
3612 static void
3613update_popup_transparent(win_T *wp, int val)
3614{
3615 if (wp->w_popup_mask != NULL)
3616 {
3617 int width = popup_width(wp);
3618 int height = popup_height(wp);
3619 listitem_T *lio, *li;
3620 int cols, cole;
3621 int lines, linee;
3622 int col, line;
3623
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003624 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003625 {
3626 li = lio->li_tv.vval.v_list->lv_first;
3627 cols = tv_get_number(&li->li_tv);
3628 if (cols < 0)
3629 cols = width + cols + 1;
3630 li = li->li_next;
3631 cole = tv_get_number(&li->li_tv);
3632 if (cole < 0)
3633 cole = width + cole + 1;
3634 li = li->li_next;
3635 lines = tv_get_number(&li->li_tv);
3636 if (lines < 0)
3637 lines = height + lines + 1;
3638 li = li->li_next;
3639 linee = tv_get_number(&li->li_tv);
3640 if (linee < 0)
3641 linee = height + linee + 1;
3642
3643 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003644 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003645 if (cols < 0)
3646 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003647 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003648 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003649 if (lines < 0)
3650 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003651 for (line = lines; line < linee
3652 && line + wp->w_winrow < screen_Rows; ++line)
3653 for (col = cols; col < cole
3654 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003655 popup_transparent[(line + wp->w_winrow) * screen_Columns
3656 + col + wp->w_wincol] = val;
3657 }
3658 }
3659}
3660
3661/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003662 * Only called when popup window "wp" is hidden: If the window is positioned
3663 * next to a text property, and it is now visible, then unhide the popup.
3664 * We don't check if visible popups become hidden, that is done in
3665 * popup_adjust_position().
3666 * Return TRUE if the popup became unhidden.
3667 */
3668 static int
3669check_popup_unhidden(win_T *wp)
3670{
3671 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3672 {
3673 textprop_T prop;
3674 linenr_T lnum;
3675
Bram Moolenaar27724252022-05-08 15:00:04 +01003676 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3677 && find_visible_prop(wp->w_popup_prop_win,
3678 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003679 &prop, &lnum) == OK)
3680 {
3681 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003682 wp->w_popup_prop_topline = 0; // force repositioning
3683 return TRUE;
3684 }
3685 }
3686 return FALSE;
3687}
3688
3689/*
3690 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3691 * That is when the buffer in the popup was changed, or the popup is following
3692 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003693 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003694 */
3695 static int
3696popup_need_position_adjust(win_T *wp)
3697{
3698 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3699 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003700 if (win_valid(wp->w_popup_prop_win)
3701 && (wp->w_popup_prop_changedtick
3702 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3703 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3704 return TRUE;
3705
3706 // May need to adjust the width if the cursor moved.
3707 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003708}
3709
3710/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003711 * Update "popup_mask" if needed.
3712 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003713 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003714 * Also marks window lines for redrawing.
3715 */
3716 void
3717may_update_popup_mask(int type)
3718{
3719 win_T *wp;
3720 short *mask;
3721 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003722 int redraw_all_popups = FALSE;
3723 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003724
3725 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003726 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3727 // basic (such as the screen size) must have changed.
3728 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003729 {
3730 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003731 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003732 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003733
3734 // Check if any popup window buffer has changed and if any popup connected
3735 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003736 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003737 if (wp->w_popup_flags & POPF_HIDDEN)
3738 popup_mask_refresh |= check_popup_unhidden(wp);
3739 else if (popup_need_position_adjust(wp))
3740 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003741 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003742 if (wp->w_popup_flags & POPF_HIDDEN)
3743 popup_mask_refresh |= check_popup_unhidden(wp);
3744 else if (popup_need_position_adjust(wp))
3745 popup_mask_refresh = TRUE;
3746
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003747 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003748 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003749
3750 // Need to update the mask, something has changed.
3751 popup_mask_refresh = FALSE;
3752 popup_mask_tab = curtab;
3753 popup_visible = FALSE;
3754
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003755 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003756 // If redrawing only what is needed, update "popup_mask_next" and then
3757 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003758 redrawing_all_win = TRUE;
3759 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003760 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003761 redrawing_all_win = FALSE;
3762 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003763 mask = popup_mask;
3764 else
3765 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003766 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003767
3768 // Find the window with the lowest zindex that hasn't been handled yet,
3769 // so that the window with a higher zindex overwrites the value in
3770 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003771 popup_reset_handled(POPUP_HANDLED_4);
3772 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003773 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003774 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003775 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003776
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003777 popup_visible = TRUE;
3778
Bram Moolenaar12034e22019-08-25 22:25:02 +02003779 // Recompute the position if the text changed. It may make the popup
3780 // hidden if it's attach to a text property that is no longer visible.
3781 if (redraw_all_popups || popup_need_position_adjust(wp))
3782 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003783 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003784 if (wp->w_popup_flags & POPF_HIDDEN)
3785 continue;
3786 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003787
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003788 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003789 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003790 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003791 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003792 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003793 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003794 col < wp->w_wincol + width - wp->w_popup_leftoff
3795 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003796 if (wp->w_zindex < POPUPMENU_ZINDEX
3797 && pum_visible()
3798 && pum_under_menu(line, col, FALSE))
3799 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3800 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003801 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003802 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003803 }
3804
3805 // Only check which lines are to be updated if not already
3806 // updating all lines.
3807 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003808 {
3809 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3810 win_T *prev_wp = NULL;
3811
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003812 for (line = 0; line < screen_Rows; ++line)
3813 {
3814 int col_done = 0;
3815
3816 for (col = 0; col < screen_Columns; ++col)
3817 {
3818 int off = line * screen_Columns + col;
3819
3820 if (popup_mask[off] != popup_mask_next[off])
3821 {
3822 popup_mask[off] = popup_mask_next[off];
3823
3824 if (line >= cmdline_row)
3825 {
3826 // the command line needs to be cleared if text below
3827 // the popup is now visible.
3828 if (!msg_scrolled && popup_mask_next[off] == 0)
3829 clear_cmdline = TRUE;
3830 }
3831 else if (col >= col_done)
3832 {
3833 linenr_T lnum;
3834 int line_cp = line;
3835 int col_cp = col;
3836
3837 // The screen position "line" / "col" needs to be
3838 // redrawn. Figure out what window that is and update
3839 // w_redraw_top and w_redr_bot. Only needs to be done
3840 // once for each window line.
3841 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3842 if (wp != NULL)
3843 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003844#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003845 // A terminal window needs to be redrawn.
3846 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003847 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003848 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003849#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003850 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003851 if (wp != prev_wp)
3852 {
3853 vim_memset(plines_cache, 0,
3854 sizeof(int) * Rows);
3855 prev_wp = wp;
3856 }
3857
3858 if (line_cp >= wp->w_height)
3859 // In (or below) status line
3860 wp->w_redr_status = TRUE;
3861 else
3862 {
3863 // compute the position in the buffer line
3864 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003865 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003866 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003867 redrawWinline(wp, lnum);
3868 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003869 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003870
3871 // This line is going to be redrawn, no need to
3872 // check until the right side of the window.
3873 col_done = wp->w_wincol + wp->w_width - 1;
3874 }
3875 }
3876 }
3877 }
3878 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003879
3880 vim_free(plines_cache);
3881 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003882
3883 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003884}
3885
3886/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003887 * If the current window is a popup and something relevant changed, recompute
3888 * the position and size.
3889 */
3890 void
3891may_update_popup_position(void)
3892{
3893 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3894 popup_adjust_position(curwin);
3895}
3896
3897/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003898 * Return a string of "len" spaces in IObuff.
3899 */
3900 static char_u *
3901get_spaces(int len)
3902{
3903 vim_memset(IObuff, ' ', (size_t)len);
3904 IObuff[len] = NUL;
3905 return IObuff;
3906}
3907
3908/*
3909 * Update popup windows. They are drawn on top of normal windows.
3910 * "win_update" is called for each popup window, lowest zindex first.
3911 */
3912 void
3913update_popups(void (*win_update)(win_T *wp))
3914{
3915 win_T *wp;
3916 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003917 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003918 int total_width;
3919 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003920 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003921 int popup_attr;
3922 int border_attr[4];
3923 int border_char[8];
3924 char_u buf[MB_MAXBYTES];
3925 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003926 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003927 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003928 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003929 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003930 int sb_thumb_top = 0;
3931 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003932 int attr_scroll = 0;
3933 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003934
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003935 // hide the cursor until redrawing is done.
3936 cursor_off();
3937
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003938 // Find the window with the lowest zindex that hasn't been updated yet,
3939 // so that the window with a higher zindex is drawn later, thus goes on
3940 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003941 popup_reset_handled(POPUP_HANDLED_5);
3942 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003943 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003944 int title_len = 0;
3945 int title_wincol;
3946
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003947 // This drawing uses the zindex of the popup window, so that it's on
3948 // top of the text but doesn't draw when another popup with higher
3949 // zindex is on top of the character.
3950 screen_zindex = wp->w_zindex;
3951
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003952 // Set flags in popup_transparent[] for masked cells.
3953 update_popup_transparent(wp, 1);
3954
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003955 // adjust w_winrow and w_wincol for border and padding, since
3956 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003957 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003958 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3959 - wp->w_popup_leftoff;
3960 if (wp->w_wincol + left_extra < 0)
3961 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003962 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003963 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003964
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003965 // Draw the popup text, unless it's off screen.
3966 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003967 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003968 // May need to update the "cursorline" highlighting, which may also
3969 // change "topline"
3970 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3971 popup_highlight_curline(wp);
3972
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003973 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003974
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003975 // move the cursor into the visible lines, otherwise executing
3976 // commands with win_execute() may cause the text to jump.
3977 if (wp->w_cursor.lnum < wp->w_topline)
3978 wp->w_cursor.lnum = wp->w_topline;
3979 else if (wp->w_cursor.lnum >= wp->w_botline)
3980 wp->w_cursor.lnum = wp->w_botline - 1;
3981 }
3982
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003983 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003984 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003985
3986 // Add offset for border and padding if not done already.
3987 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3988 {
3989 wp->w_wcol += left_extra;
3990 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3991 }
3992 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003993 {
3994 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003995 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003996 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003997
Bram Moolenaareabddc42022-04-02 15:32:16 +01003998 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003999 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004000 popup_attr = get_wcr_attr(wp);
4001
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004002 if (wp->w_winrow + total_height > cmdline_row)
4003 wp->w_popup_flags |= POPF_ON_CMDLINE;
4004 else
4005 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4006
4007
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004008 // We can only use these line drawing characters when 'encoding' is
4009 // "utf-8" and 'ambiwidth' is "single".
4010 if (enc_utf8 && *p_ambw == 's')
4011 {
4012 border_char[0] = border_char[2] = 0x2550;
4013 border_char[1] = border_char[3] = 0x2551;
4014 border_char[4] = 0x2554;
4015 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004016 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4017 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004018 border_char[7] = 0x255a;
4019 }
4020 else
4021 {
4022 border_char[0] = border_char[2] = '-';
4023 border_char[1] = border_char[3] = '|';
4024 for (i = 4; i < 8; ++i)
4025 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004026 if (wp->w_popup_flags & POPF_RESIZE)
4027 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004028 }
4029 for (i = 0; i < 8; ++i)
4030 if (wp->w_border_char[i] != 0)
4031 border_char[i] = wp->w_border_char[i];
4032
4033 for (i = 0; i < 4; ++i)
4034 {
4035 border_attr[i] = popup_attr;
4036 if (wp->w_border_highlight[i] != NULL)
4037 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4038 }
4039
Bram Moolenaard91467f2020-11-21 12:42:09 +01004040 // Title goes on top of border or padding.
4041 title_wincol = wp->w_wincol + 1;
4042 if (wp->w_popup_title != NULL)
4043 {
rbtnnc6119412021-08-07 13:08:45 +02004044 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004045
Ralf Schandlbc869872021-05-28 14:12:14 +02004046 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004047 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004048 {
4049 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4050 char_u *title_text = alloc(title_byte_len + 1);
4051
4052 if (title_text != NULL)
4053 {
4054 trunc_string(wp->w_popup_title, title_text,
4055 total_width - 2, title_byte_len + 1);
4056 screen_puts(title_text, wp->w_winrow, title_wincol,
4057 wp->w_popup_border[0] > 0
4058 ? border_attr[0] : popup_attr);
4059 vim_free(title_text);
4060 }
4061
Bram Moolenaard91467f2020-11-21 12:42:09 +01004062 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004063 }
4064 else
4065 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4066 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004067 }
4068
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004069 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004070 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004071 if (wp->w_popup_border[0] > 0)
4072 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004073 // top border; do not draw over the title
4074 if (title_len > 0)
4075 {
4076 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4077 wincol < 0 ? 0 : wincol, title_wincol,
4078 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004079 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004080 border_char[0], border_attr[0]);
4081 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4082 title_wincol + title_len, wincol + total_width,
4083 border_char[0], border_char[0], border_attr[0]);
4084 }
4085 else
4086 {
4087 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4088 wincol < 0 ? 0 : wincol, wincol + total_width,
4089 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4090 ? border_char[4] : border_char[0],
4091 border_char[0], border_attr[0]);
4092 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004093 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004094 {
4095 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4096 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004097 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004098 }
4099 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004100 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4101 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004102
Bram Moolenaard529ba52019-07-02 23:13:53 +02004103 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4104 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004105 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004106 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004107 - wp->w_has_scrollbar;
4108 if (padcol < 0)
4109 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004110 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004111 padcol = 0;
4112 }
4113 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004114 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004115 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004116 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004117 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004118 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004119 // top padding and no border; do not draw over the title
4120 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004121 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004122 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004123 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004124 row += 1;
4125 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004126 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004127 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004128 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004129 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004130
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004131 // Compute scrollbar thumb position and size.
4132 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004133 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004134 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4135 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004136 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004137
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004138 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4139 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004140 if (wp->w_topline > 1 && sb_thumb_height == height)
4141 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004142 if (sb_thumb_height == 0)
4143 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004144 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004145 // it just fits, avoid divide by zero
4146 sb_thumb_top = 0;
4147 else
4148 sb_thumb_top = (wp->w_topline - 1
4149 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004150 * (wp->w_height - sb_thumb_height)
4151 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004152 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4153 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004154 last = total_height - top_off - wp->w_popup_border[2];
4155 if (sb_thumb_top >= last)
4156 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004157 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004158
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004159 if (wp->w_scrollbar_highlight != NULL)
4160 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4161 else
4162 attr_scroll = highlight_attr[HLF_PSB];
4163 if (wp->w_thumb_highlight != NULL)
4164 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4165 else
4166 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004167 }
4168
4169 for (i = wp->w_popup_border[0];
4170 i < total_height - wp->w_popup_border[2]; ++i)
4171 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004172 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004173 // left and right padding only needed next to the body
4174 int do_padding =
4175 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4176 && i < total_height - wp->w_popup_border[2]
4177 - wp->w_popup_padding[2];
4178
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004179 row = wp->w_winrow + i;
4180
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004181 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004182 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004183 {
4184 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004185 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004186 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004187 if (do_padding && wp->w_popup_padding[3] > 0)
4188 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004189 int col = wincol + wp->w_popup_border[3];
4190
Bram Moolenaard529ba52019-07-02 23:13:53 +02004191 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004192 pad_left = wp->w_popup_padding[3];
4193 if (col < 0)
4194 {
4195 pad_left += col;
4196 col = 0;
4197 }
4198 if (pad_left > 0)
4199 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4200 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004201 // scrollbar
4202 if (wp->w_has_scrollbar)
4203 {
4204 int line = i - top_off;
4205 int scroll_col = wp->w_wincol + total_width - 1
4206 - wp->w_popup_border[1];
4207
4208 if (line >= 0 && line < wp->w_height)
4209 screen_putchar(' ', row, scroll_col,
4210 line >= sb_thumb_top
4211 && line < sb_thumb_top + sb_thumb_height
4212 ? attr_thumb : attr_scroll);
4213 else
4214 screen_putchar(' ', row, scroll_col, popup_attr);
4215 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004216 // right border
4217 if (wp->w_popup_border[1] > 0)
4218 {
4219 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004220 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004221 }
4222 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004223 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004224 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004225 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004226 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4227 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004228 }
4229
4230 if (wp->w_popup_padding[2] > 0)
4231 {
4232 // bottom padding
4233 row = wp->w_winrow + wp->w_popup_border[0]
4234 + wp->w_popup_padding[0] + wp->w_height;
4235 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004236 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004237 }
4238
4239 if (wp->w_popup_border[2] > 0)
4240 {
4241 // bottom border
4242 row = wp->w_winrow + total_height - 1;
4243 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004244 wincol < 0 ? 0 : wincol,
4245 wincol + total_width,
4246 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004247 ? border_char[7] : border_char[2],
4248 border_char[2], border_attr[2]);
4249 if (wp->w_popup_border[1] > 0)
4250 {
4251 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004252 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004253 }
4254 }
4255
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004256 if (wp->w_popup_close == POPCLOSE_BUTTON)
4257 {
4258 // close button goes on top of anything at the top-right corner
4259 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004260 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004261 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4262 }
4263
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004264 update_popup_transparent(wp, 0);
4265
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004266 // Back to the normal zindex.
4267 screen_zindex = 0;
4268 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004269
4270#if defined(FEAT_SEARCH_EXTRA)
4271 // In case win_update() called start_search_hl().
4272 end_search_hl();
4273#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004274}
4275
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004276/*
4277 * Mark references in callbacks of one popup window.
4278 */
4279 static int
4280set_ref_in_one_popup(win_T *wp, int copyID)
4281{
4282 int abort = FALSE;
4283 typval_T tv;
4284
4285 if (wp->w_close_cb.cb_partial != NULL)
4286 {
4287 tv.v_type = VAR_PARTIAL;
4288 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4289 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4290 }
4291 if (wp->w_filter_cb.cb_partial != NULL)
4292 {
4293 tv.v_type = VAR_PARTIAL;
4294 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4295 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4296 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004297 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004298 return abort;
4299}
4300
4301/*
4302 * Set reference in callbacks of popup windows.
4303 */
4304 int
4305set_ref_in_popups(int copyID)
4306{
4307 int abort = FALSE;
4308 win_T *wp;
4309 tabpage_T *tp;
4310
4311 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4312 abort = abort || set_ref_in_one_popup(wp, copyID);
4313
4314 FOR_ALL_TABPAGES(tp)
4315 {
4316 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4317 abort = abort || set_ref_in_one_popup(wp, copyID);
4318 if (abort)
4319 break;
4320 }
4321 return abort;
4322}
Bram Moolenaar79648732019-07-18 21:43:07 +02004323
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004324 int
4325popup_is_popup(win_T *wp)
4326{
4327 return wp->w_popup_flags != 0;
4328}
4329
4330#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004331/*
4332 * Find an existing popup used as the preview window, in the current tab page.
4333 * Return NULL if not found.
4334 */
4335 win_T *
4336popup_find_preview_window(void)
4337{
4338 win_T *wp;
4339
4340 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004341 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004342 if (wp->w_p_pvw)
4343 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004344 return NULL;
4345}
4346
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004347/*
4348 * Find an existing popup used as the info window, in the current tab page.
4349 * Return NULL if not found.
4350 */
4351 win_T *
4352popup_find_info_window(void)
4353{
4354 win_T *wp;
4355
4356 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004357 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004358 if (wp->w_popup_flags & POPF_INFO)
4359 return wp;
4360 return NULL;
4361}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004362#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004363
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004364 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004365f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4366{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004367#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004368 win_T *wp = popup_find_info_window();
4369
4370 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004371#else
4372 rettv->vval.v_number = 0;
4373#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004374}
4375
4376 void
4377f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004378{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004379#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004380 win_T *wp = popup_find_preview_window();
4381
4382 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004383#else
4384 rettv->vval.v_number = 0;
4385#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004386}
4387
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004388#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004389/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004390 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004391 * NOTE: this makes the popup the current window, so that the file can be
4392 * edited. However, it must not remain to be the current window, the caller
4393 * must make sure of that.
4394 */
4395 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004396popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004397{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004398 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004399
4400 if (wp == NULL)
4401 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004402 if (info)
4403 wp->w_popup_flags |= POPF_INFO;
4404 else
4405 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004406
4407 // Set the width to a reasonable value, so that w_topline can be computed.
4408 if (wp->w_minwidth > 0)
4409 wp->w_width = wp->w_minwidth;
4410 else if (wp->w_maxwidth > 0)
4411 wp->w_width = wp->w_maxwidth;
4412 else
4413 wp->w_width = curwin->w_width;
4414
4415 // Will switch to another buffer soon, dummy one can be wiped.
4416 wp->w_buffer->b_locked = FALSE;
4417
4418 win_enter(wp, FALSE);
4419 return OK;
4420}
4421
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004422/*
4423 * Close any preview popup.
4424 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004425 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004426popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004427{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004428 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004429
4430 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004431 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004432}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004433
4434/*
4435 * Hide the info popup.
4436 */
4437 void
4438popup_hide_info(void)
4439{
4440 win_T *wp = popup_find_info_window();
4441
4442 if (wp != NULL)
4443 popup_hide(wp);
4444}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004445
4446/*
4447 * Close any info popup.
4448 */
4449 void
4450popup_close_info(void)
4451{
4452 win_T *wp = popup_find_info_window();
4453
4454 if (wp != NULL)
4455 popup_close_with_retval(wp, -1);
4456}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004457#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004458
Bram Moolenaar9198de32022-08-27 21:30:03 +01004459#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4460
Bram Moolenaar9198de32022-08-27 21:30:03 +01004461/*
4462 * Get the message window.
4463 * Returns NULL if something failed.
4464 */
4465 win_T *
4466popup_get_message_win(void)
4467{
4468 if (message_win == NULL)
4469 {
4470 int i;
4471
4472 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4473
4474 if (message_win == NULL)
4475 return NULL;
4476
4477 // use the full screen width
4478 message_win->w_width = Columns;
4479
4480 // position at bottom of screen
4481 message_win->w_popup_pos = POPPOS_BOTTOM;
4482 message_win->w_wantcol = 1;
4483 message_win->w_minwidth = 9999;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01004484 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004485
4486 // no padding, border at the top
4487 for (i = 0; i < 4; ++i)
4488 message_win->w_popup_padding[i] = 0;
4489 for (i = 1; i < 4; ++i)
4490 message_win->w_popup_border[i] = 0;
4491
4492 if (message_win->w_popup_timer != NULL)
4493 message_win->w_popup_timer->tr_keep = TRUE;
4494 }
4495 return message_win;
4496}
4497
4498/*
4499 * If the message window is not visible: show it
4500 * If the message window is visible: reset the timeout
4501 */
4502 void
4503popup_show_message_win(void)
4504{
4505 if (message_win != NULL)
4506 {
4507 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4508 {
4509 // the highlight may have changed.
4510 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4511 popup_show(message_win);
4512 }
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004513 if (message_win->w_popup_timer != NULL)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004514 timer_start(message_win->w_popup_timer);
4515 }
4516}
4517
4518 int
4519popup_message_win_visible(void)
4520{
4521 return message_win != NULL
4522 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4523}
4524
4525/*
4526 * If the message window is visible: hide it.
4527 */
4528 void
4529popup_hide_message_win(void)
4530{
4531 if (message_win != NULL)
4532 popup_hide(message_win);
4533}
4534
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004535/*
4536 * Invoked before outputting a message for ":echowindow".
4537 */
4538 void
4539start_echowindow(void)
4540{
4541 in_echowindow = TRUE;
4542}
4543
4544/*
4545 * Invoked after outputting a message for ":echowindow".
4546 */
4547 void
4548end_echowindow(void)
4549{
4550 // show the message window now
4551 redraw_cmd(FALSE);
4552
4553 // do not overwrite messages
4554 // TODO: only for message window
4555 msg_didout = TRUE;
4556 if (msg_col == 0)
4557 msg_col = 1;
4558 in_echowindow = FALSE;
4559}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004560#endif
4561
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004562/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004563 * Close any popup for a text property associated with "win".
4564 * Return TRUE if a popup was closed.
4565 */
4566 int
4567popup_win_closed(win_T *win)
4568{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004569 int round;
4570 win_T *wp;
4571 win_T *next;
4572 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004573
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004574 for (round = 1; round <= 2; ++round)
4575 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4576 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004577 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004578 next = wp->w_next;
4579 if (wp->w_popup_prop_win == win)
4580 {
4581 popup_close_with_retval(wp, -1);
4582 ret = TRUE;
4583 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004584 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004585 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004586}
4587
4588/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004589 * Set the title of the popup window to the file name.
4590 */
4591 void
4592popup_set_title(win_T *wp)
4593{
4594 if (wp->w_buffer->b_fname != NULL)
4595 {
4596 char_u dirname[MAXPATHL];
4597 size_t len;
4598
4599 mch_dirname(dirname, MAXPATHL);
4600 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4601
4602 vim_free(wp->w_popup_title);
4603 len = STRLEN(wp->w_buffer->b_fname) + 3;
4604 wp->w_popup_title = alloc((int)len);
4605 if (wp->w_popup_title != NULL)
4606 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4607 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004608 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004609 }
4610}
4611
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004612# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004613/*
4614 * If there is a preview window, update the title.
4615 * Used after changing directory.
4616 */
4617 void
4618popup_update_preview_title(void)
4619{
4620 win_T *wp = popup_find_preview_window();
4621
4622 if (wp != NULL)
4623 popup_set_title(wp);
4624}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004625# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004626
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004627#endif // FEAT_PROP_POPUP