blob: 68ec64a2dfd01b20a87a4ebf41210bfdf7af2577 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020064 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020065 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000089 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020090 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100100 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200101 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
103 ++i, li = li->li_next)
104 {
105 nr = (int)tv_get_number(&li->li_tv);
106 if (nr >= 0)
107 array[i] = nr > max_val ? max_val : nr;
108 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100109 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110 }
111 }
112}
113
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200114/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200115 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200116 */
117 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200118set_moved_values(win_T *wp)
119{
120 wp->w_popup_curwin = curwin;
121 wp->w_popup_lnum = curwin->w_cursor.lnum;
122 wp->w_popup_mincol = curwin->w_cursor.col;
123 wp->w_popup_maxcol = curwin->w_cursor.col;
124}
125
126/*
127 * Used when popup options contain "moved" with "word" or "WORD".
128 */
129 static void
130set_moved_columns(win_T *wp, int flags)
131{
132 char_u *ptr;
133 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
134
135 if (len > 0)
136 {
137 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
138 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
139 }
140}
141
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200143 * Used when popup options contain "mousemoved": set default moved values.
144 */
145 static void
146set_mousemoved_values(win_T *wp)
147{
148 wp->w_popup_mouse_row = mouse_row;
149 wp->w_popup_mouse_mincol = mouse_col;
150 wp->w_popup_mouse_maxcol = mouse_col;
151}
152
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000153 static void
154update_popup_uses_mouse_move(void)
155{
156 popup_uses_mouse_move = FALSE;
157 if (popup_visible)
158 {
159 win_T *wp;
160
161 FOR_ALL_POPUPWINS(wp)
162 if (wp->w_popup_mouse_row != 0)
163 {
164 popup_uses_mouse_move = TRUE;
165 return;
166 }
167 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
168 if (wp->w_popup_mouse_row != 0)
169 {
170 popup_uses_mouse_move = TRUE;
171 return;
172 }
173 }
174}
175
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200176/*
177 * Used when popup options contain "moved" with "word" or "WORD".
178 */
179 static void
180set_mousemoved_columns(win_T *wp, int flags)
181{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200182 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200183 char_u *text;
184 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200185 pos_T pos;
186 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200187
188 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200189 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200190 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200191 // convert text column to mouse column
192 pos.col = col;
193 pos.coladd = 0;
194 getvcol(textwp, &pos, &mcol, NULL, NULL);
195 wp->w_popup_mouse_mincol = mcol;
196
Bram Moolenaar10727682019-07-12 13:59:20 +0200197 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200198 getvcol(textwp, &pos, NULL, NULL, &mcol);
199 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200200 vim_free(text);
201 }
202}
203
204/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200205 * Return TRUE if "row"/"col" is on the border of the popup.
206 * The values are relative to the top-left corner.
207 */
208 int
209popup_on_border(win_T *wp, int row, int col)
210{
211 return (row == 0 && wp->w_popup_border[0] > 0)
212 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
213 || (col == 0 && wp->w_popup_border[3] > 0)
214 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
215}
216
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200217/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200218 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
219 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200220 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200221 * Caller should check the left mouse button was clicked.
222 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200223 */
224 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200225popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200226{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200227 if (wp->w_popup_close == POPCLOSE_BUTTON
228 && row == 0 && col == popup_width(wp) - 1)
229 {
230 popup_close_for_mouse_click(wp);
231 return TRUE;
232 }
233 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200234}
235
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200236// Values set when dragging a popup window starts.
237static int drag_start_row;
238static int drag_start_col;
239static int drag_start_wantline;
240static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200242
243/*
244 * Mouse down on border of popup window: start dragging it.
245 * Uses mouse_col and mouse_row.
246 */
247 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200248popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200249{
250 drag_start_row = mouse_row;
251 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200252 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200253 drag_start_wantline = wp->w_winrow + 1;
254 else
255 drag_start_wantline = wp->w_wantline;
256 if (wp->w_wantcol == 0)
257 drag_start_wantcol = wp->w_wincol + 1;
258 else
259 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200260
261 // Stop centering the popup
262 if (wp->w_popup_pos == POPPOS_CENTER)
263 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200264
265 drag_on_resize_handle = wp->w_popup_border[1] > 0
266 && wp->w_popup_border[2] > 0
267 && row == popup_height(wp) - 1
268 && col == popup_width(wp) - 1;
269
270 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
271 {
272 if (wp->w_popup_pos == POPPOS_TOPRIGHT
273 || wp->w_popup_pos == POPPOS_BOTRIGHT)
274 wp->w_wantcol = wp->w_wincol + 1;
275 if (wp->w_popup_pos == POPPOS_BOTLEFT)
276 wp->w_wantline = wp->w_winrow + 1;
277 wp->w_popup_pos = POPPOS_TOPLEFT;
278 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200279}
280
281/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200282 * Mouse moved while dragging a popup window: adjust the window popup position
283 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284 */
285 void
286popup_drag(win_T *wp)
287{
288 // The popup may be closed before dragging stops.
289 if (!win_valid_popup(wp))
290 return;
291
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200292 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
293 {
294 int width_inc = mouse_col - drag_start_col;
295 int height_inc = mouse_row - drag_start_row;
296
297 if (width_inc != 0)
298 {
299 int width = wp->w_width + width_inc;
300
301 if (width < 1)
302 width = 1;
303 wp->w_minwidth = width;
304 wp->w_maxwidth = width;
305 drag_start_col = mouse_col;
306 }
307
308 if (height_inc != 0)
309 {
310 int height = wp->w_height + height_inc;
311
312 if (height < 1)
313 height = 1;
314 wp->w_minheight = height;
315 wp->w_maxheight = height;
316 drag_start_row = mouse_row;
317 }
318
319 popup_adjust_position(wp);
320 return;
321 }
322
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000323 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200324 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200325 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
326 if (wp->w_wantline < 1)
327 wp->w_wantline = 1;
328 if (wp->w_wantline > Rows)
329 wp->w_wantline = Rows;
330 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
331 if (wp->w_wantcol < 1)
332 wp->w_wantcol = 1;
333 if (wp->w_wantcol > Columns)
334 wp->w_wantcol = Columns;
335
336 popup_adjust_position(wp);
337}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200338
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200339/*
340 * Set w_firstline to match the current "wp->w_topline".
341 */
342 void
343popup_set_firstline(win_T *wp)
344{
345 int height = wp->w_height;
346
347 wp->w_firstline = wp->w_topline;
348 popup_adjust_position(wp);
349
350 // we don't want the popup to get smaller, decrement the first line
351 // until it doesn't
352 while (wp->w_firstline > 1 && wp->w_height < height)
353 {
354 --wp->w_firstline;
355 popup_adjust_position(wp);
356 }
357}
358
359/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200360 * Return TRUE if the position is in the popup window scrollbar.
361 */
362 int
363popup_is_in_scrollbar(win_T *wp, int row, int col)
364{
365 return wp->w_has_scrollbar
366 && row >= wp->w_popup_border[0]
367 && row < popup_height(wp) - wp->w_popup_border[2]
368 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
369}
370
371
372/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200373 * Handle a click in a popup window, if it is in the scrollbar.
374 */
375 void
376popup_handle_scrollbar_click(win_T *wp, int row, int col)
377{
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200378 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200379 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100380 int height = popup_height(wp);
381 int new_topline = wp->w_topline;
382
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200383 if (row >= height / 2)
384 {
385 // Click in lower half, scroll down.
386 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaard28950f2022-05-29 14:13:04 +0100387 ++new_topline;
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200388 }
389 else if (wp->w_topline > 1)
390 // click on upper half, scroll up.
Bram Moolenaard28950f2022-05-29 14:13:04 +0100391 --new_topline;
392 if (new_topline != wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200393 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100394 set_topline(wp, new_topline);
395 if (wp == curwin)
396 {
397 if (wp->w_cursor.lnum < wp->w_topline)
398 {
399 wp->w_cursor.lnum = wp->w_topline;
400 check_cursor();
401 }
402 else if (wp->w_cursor.lnum >= wp->w_botline)
403 {
404 wp->w_cursor.lnum = wp->w_botline - 1;
405 check_cursor();
406 }
407 }
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200408 popup_set_firstline(wp);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100409 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200410 }
411 }
412}
413
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200414#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100415/*
416 * Add a timer to "wp" with "time".
417 * If "close" is true use popup_close(), otherwise popup_hide().
418 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200419 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100420popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200421{
422 char_u cbbuf[50];
423 char_u *ptr = cbbuf;
424 typval_T tv;
425
426 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100427 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
428 wp->w_id);
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200429 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200430 {
431 wp->w_popup_timer = create_timer(time, 0);
432 wp->w_popup_timer->tr_callback = get_callback(&tv);
433 clear_tv(&tv);
434 }
435}
436#endif
437
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100438 static poppos_T
439get_pos_entry(dict_T *d, int give_error)
440{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100441 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100442 int nr;
443
444 if (str == NULL)
445 return POPPOS_NONE;
446
K.Takataeeec2542021-06-02 13:28:16 +0200447 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100448 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
449 return poppos_entries[nr].pp_val;
450
451 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000452 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100453 return POPPOS_NONE;
454}
455
Bram Moolenaar17627312019-06-02 19:53:44 +0200456/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200457 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200458 */
459 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200460apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200461{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200462 int nr;
463 char_u *str;
464 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200465
Bram Moolenaard61efa52022-07-23 09:52:04 +0100466 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200467 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100468 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200469 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100470 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200471 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100472 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200473 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200474
475 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200476 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200477 wp->w_wantline = nr;
478 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200479 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200480 wp->w_wantcol = nr;
481
Bram Moolenaar55881332020-08-18 13:04:15 +0200482
Bram Moolenaard61efa52022-07-23 09:52:04 +0100483 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200484 if (nr != -1)
485 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200486
Bram Moolenaar12034e22019-08-25 22:25:02 +0200487 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100488 poppos_T ppt = get_pos_entry(d, TRUE);
489
490 if (ppt != POPPOS_NONE)
491 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200492 }
493
Bram Moolenaard61efa52022-07-23 09:52:04 +0100494 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200495 if (str != NULL)
496 {
497 wp->w_popup_prop_type = 0;
498 if (*str != NUL)
499 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100500 wp->w_popup_prop_win = curwin;
501 di = dict_find(d, (char_u *)"textpropwin", -1);
502 if (di != NULL)
503 {
504 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100505 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100506 wp->w_popup_prop_win = curwin;
507 }
508
509 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200510 if (nr <= 0)
511 nr = find_prop_type_id(str, NULL);
512 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000513 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200514 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200515 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200516 }
517 }
518
519 di = dict_find(d, (char_u *)"textpropid", -1);
520 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100521 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200522}
523
Bram Moolenaarb0992022020-01-30 14:55:42 +0100524/*
525 * Handle "moved" and "mousemoved" arguments.
526 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200527 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200528handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
529{
530 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
531 {
532 char_u *s = di->di_tv.vval.v_string;
533 int flags = 0;
534
535 if (STRCMP(s, "word") == 0)
536 flags = FIND_IDENT | FIND_STRING;
537 else if (STRCMP(s, "WORD") == 0)
538 flags = FIND_STRING;
539 else if (STRCMP(s, "expr") == 0)
540 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
541 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000542 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200543 if (flags != 0)
544 {
545 if (mousemoved)
546 set_mousemoved_columns(wp, flags);
547 else
548 set_moved_columns(wp, flags);
549 }
550 }
551 else if (di->di_tv.v_type == VAR_LIST
552 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200553 && (di->di_tv.vval.v_list->lv_len == 2
554 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200555 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200556 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100557 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200558 int mincol;
559 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200560
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200561 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100562 li = l->lv_first;
563 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200564 {
565 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
566
567 // Three numbers, might be from popup_getoptions().
568 if (mousemoved)
569 wp->w_popup_mouse_row = nr;
570 else
571 wp->w_popup_lnum = nr;
572 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100573 if (nr == 0)
574 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200575 }
576
577 mincol = tv_get_number(&li->li_tv);
578 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200579 if (mousemoved)
580 {
581 wp->w_popup_mouse_mincol = mincol;
582 wp->w_popup_mouse_maxcol = maxcol;
583 }
584 else
585 {
586 wp->w_popup_mincol = mincol;
587 wp->w_popup_maxcol = maxcol;
588 }
589 }
590 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000591 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200592}
593
594 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200595check_highlight(dict_T *dict, char *name, char_u **pval)
596{
597 dictitem_T *di;
598 char_u *str;
599
600 di = dict_find(dict, (char_u *)name, -1);
601 if (di != NULL)
602 {
603 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000604 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200605 else
606 {
607 str = tv_get_string(&di->di_tv);
608 if (*str != NUL)
609 *pval = vim_strsave(str);
610 }
611 }
612}
613
Bram Moolenaarae943152019-06-16 22:54:14 +0200614/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200615 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200616 */
617 static void
618popup_show_curline(win_T *wp)
619{
620 if (wp->w_cursor.lnum < wp->w_topline)
621 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200622 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200623 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200624 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200625 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200626 if (wp->w_topline < 1)
627 wp->w_topline = 1;
628 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
629 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200630 while (wp->w_topline < wp->w_cursor.lnum
631 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
632 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
633 > wp->w_height)
634 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200635 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200636
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200637 // Don't let "firstline" cause a scroll.
638 if (wp->w_firstline > 0)
639 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200640}
641
642/*
643 * Get the sign group name for window "wp".
644 * Returns a pointer to a static buffer, overwritten on the next call.
645 */
646 static char_u *
647popup_get_sign_name(win_T *wp)
648{
649 static char buf[30];
650
651 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
652 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200653}
654
655/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200656 * Highlight the line with the cursor.
657 * Also scrolls the text to put the cursor line in view.
658 */
659 static void
660popup_highlight_curline(win_T *wp)
661{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200662 int sign_id = 0;
663 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200664
Bram Moolenaar72570732019-11-30 14:21:53 +0100665 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200666
667 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
668 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200669 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200670
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200671 if (!sign_exists_by_name(sign_name))
672 {
673 char *linehl = "PopupSelected";
674
675 if (syn_name2id((char_u *)linehl) == 0)
676 linehl = "PmenuSel";
Bram Moolenaar9198de32022-08-27 21:30:03 +0100677 sign_define_by_name(sign_name, NULL, (char_u *)linehl,
678 NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200679 }
680
Bram Moolenaar72570732019-11-30 14:21:53 +0100681 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200682 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100683 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200684 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200685 else
686 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200687 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200688}
689
690/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200691 * Shared between popup_create() and f_popup_setoptions().
692 */
693 static void
694apply_general_options(win_T *wp, dict_T *dict)
695{
696 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200697 int nr;
698 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200699
Bram Moolenaarae943152019-06-16 22:54:14 +0200700 // TODO: flip
701
702 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200703 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200704 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100705 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200706 if (wp->w_firstline < 0)
707 wp->w_firstline = -1;
708 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200709
Bram Moolenaard61efa52022-07-23 09:52:04 +0100710 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200711 if (nr != -1)
712 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200713
Bram Moolenaard61efa52022-07-23 09:52:04 +0100714 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200715 if (str != NULL)
716 {
717 vim_free(wp->w_popup_title);
718 wp->w_popup_title = vim_strsave(str);
719 }
720
Bram Moolenaard61efa52022-07-23 09:52:04 +0100721 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200722 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200723 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200724
Bram Moolenaard61efa52022-07-23 09:52:04 +0100725 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200726 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200727 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200728 if (nr)
729 wp->w_popup_flags |= POPF_DRAG;
730 else
731 wp->w_popup_flags &= ~POPF_DRAG;
732 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100733 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000734 if (nr != -1)
735 {
736 if (nr)
737 wp->w_popup_flags |= POPF_DRAGALL;
738 else
739 wp->w_popup_flags &= ~POPF_DRAGALL;
740 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200741
Bram Moolenaard61efa52022-07-23 09:52:04 +0100742 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200743 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100744 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100745 if (nr)
746 wp->w_popup_flags |= POPF_POSINVERT;
747 else
748 wp->w_popup_flags &= ~POPF_POSINVERT;
749 }
750
Bram Moolenaard61efa52022-07-23 09:52:04 +0100751 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200752 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200753 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200754 if (nr)
755 wp->w_popup_flags |= POPF_RESIZE;
756 else
757 wp->w_popup_flags &= ~POPF_RESIZE;
758 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200759
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200760 di = dict_find(dict, (char_u *)"close", -1);
761 if (di != NULL)
762 {
763 int ok = TRUE;
764
765 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
766 {
767 char_u *s = di->di_tv.vval.v_string;
768
769 if (STRCMP(s, "none") == 0)
770 wp->w_popup_close = POPCLOSE_NONE;
771 else if (STRCMP(s, "button") == 0)
772 wp->w_popup_close = POPCLOSE_BUTTON;
773 else if (STRCMP(s, "click") == 0)
774 wp->w_popup_close = POPCLOSE_CLICK;
775 else
776 ok = FALSE;
777 }
778 else
779 ok = FALSE;
780 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000781 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200782 }
783
Bram Moolenaard61efa52022-07-23 09:52:04 +0100784 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200785 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000786 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200787 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
788 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000789#ifdef FEAT_TERMINAL
790 term_update_wincolor(wp);
791#endif
792 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200793
Bram Moolenaarae943152019-06-16 22:54:14 +0200794 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
795 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200796
Bram Moolenaar790498b2019-06-01 22:15:29 +0200797 di = dict_find(dict, (char_u *)"borderhighlight", -1);
798 if (di != NULL)
799 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200800 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000801 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200802 else
803 {
804 list_T *list = di->di_tv.vval.v_list;
805 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200806 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200807
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200808 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200809 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
810 ++i, li = li->li_next)
811 {
812 str = tv_get_string(&li->li_tv);
813 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100814 {
815 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200816 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100817 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200818 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200819 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
820 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100821 {
822 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200823 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200824 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100825 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200826 }
827 }
828
Bram Moolenaar790498b2019-06-01 22:15:29 +0200829 di = dict_find(dict, (char_u *)"borderchars", -1);
830 if (di != NULL)
831 {
832 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000833 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200834 else
835 {
836 list_T *list = di->di_tv.vval.v_list;
837 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200838 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200839
840 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100841 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200842 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200843 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
844 ++i, li = li->li_next)
845 {
846 str = tv_get_string(&li->li_tv);
847 if (*str != NUL)
848 wp->w_border_char[i] = mb_ptr2char(str);
849 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200850 if (list->lv_len == 1)
851 for (i = 1; i < 8; ++i)
852 wp->w_border_char[i] = wp->w_border_char[0];
853 if (list->lv_len == 2)
854 {
855 for (i = 4; i < 8; ++i)
856 wp->w_border_char[i] = wp->w_border_char[1];
857 for (i = 1; i < 4; ++i)
858 wp->w_border_char[i] = wp->w_border_char[0];
859 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200860 }
861 }
862 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200863
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200864 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
865 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
866
Bram Moolenaarae943152019-06-16 22:54:14 +0200867 di = dict_find(dict, (char_u *)"zindex", -1);
868 if (di != NULL)
869 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100870 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200871 if (wp->w_zindex < 1)
872 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
873 if (wp->w_zindex > 32000)
874 wp->w_zindex = 32000;
875 }
876
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200877 di = dict_find(dict, (char_u *)"mask", -1);
878 if (di != NULL)
879 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200880 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200881
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200882 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200883 {
884 listitem_T *li;
885
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200886 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200887 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200888 {
889 if (li->li_tv.v_type != VAR_LIST
890 || li->li_tv.vval.v_list == NULL
891 || li->li_tv.vval.v_list->lv_len != 4)
892 {
893 ok = FALSE;
894 break;
895 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100896 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200897 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200898 }
899 }
900 if (ok)
901 {
902 wp->w_popup_mask = di->di_tv.vval.v_list;
903 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200904 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200905 }
906 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000907 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200908 }
909
Bram Moolenaarae943152019-06-16 22:54:14 +0200910#if defined(FEAT_TIMERS)
911 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100912 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200913 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100914 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200915#endif
916
Bram Moolenaar3397f742019-06-02 18:40:06 +0200917 di = dict_find(dict, (char_u *)"moved", -1);
918 if (di != NULL)
919 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200920 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200921 handle_moved_argument(wp, di, FALSE);
922 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200923
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200924 di = dict_find(dict, (char_u *)"mousemoved", -1);
925 if (di != NULL)
926 {
927 set_mousemoved_values(wp);
928 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200929 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200930
Bram Moolenaard61efa52022-07-23 09:52:04 +0100931 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100932 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200933 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100934 if (nr != 0)
935 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200936 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100937 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200938 }
939
Bram Moolenaarae943152019-06-16 22:54:14 +0200940 di = dict_find(dict, (char_u *)"filter", -1);
941 if (di != NULL)
942 {
943 callback_T callback = get_callback(&di->di_tv);
944
945 if (callback.cb_name != NULL)
946 {
947 free_callback(&wp->w_filter_cb);
948 set_callback(&wp->w_filter_cb, &callback);
949 }
950 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100951 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200952 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200953 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200954 if (nr)
955 wp->w_popup_flags |= POPF_MAPPING;
956 else
957 wp->w_popup_flags &= ~POPF_MAPPING;
958 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200959
Bram Moolenaard61efa52022-07-23 09:52:04 +0100960 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200961 if (str != NULL)
962 {
963 if (STRCMP(str, "a") == 0)
964 wp->w_filter_mode = MODE_ALL;
965 else
966 wp->w_filter_mode = mode_str2flags(str);
967 }
968
Bram Moolenaarae943152019-06-16 22:54:14 +0200969 di = dict_find(dict, (char_u *)"callback", -1);
970 if (di != NULL)
971 {
972 callback_T callback = get_callback(&di->di_tv);
973
974 if (callback.cb_name != NULL)
975 {
976 free_callback(&wp->w_close_cb);
977 set_callback(&wp->w_close_cb, &callback);
978 }
979 }
980}
981
982/*
983 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200984 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200985 */
986 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200987apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200988{
989 int nr;
990
991 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200992
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200993 if (create)
994 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200995 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
996
Bram Moolenaarae943152019-06-16 22:54:14 +0200997 apply_general_options(wp, dict);
998
Bram Moolenaard61efa52022-07-23 09:52:04 +0100999 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001000 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001001 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001002
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001003 // when "firstline" and "cursorline" are both set and the cursor would be
1004 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001005 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1006 {
1007 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1008 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001009 else if (wp->w_cursor.lnum < wp->w_firstline
1010 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001011 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001012 wp->w_topline = wp->w_firstline;
1013 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001014 }
1015
Bram Moolenaar33796b32019-06-08 16:01:13 +02001016 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001017 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001018}
1019
1020/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001021 * Add lines to the popup from a list of strings.
1022 */
1023 static void
1024add_popup_strings(buf_T *buf, list_T *l)
1025{
1026 listitem_T *li;
1027 linenr_T lnum = 0;
1028 char_u *p;
1029
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001030 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001031 if (li->li_tv.v_type == VAR_STRING)
1032 {
1033 p = li->li_tv.vval.v_string;
1034 ml_append_buf(buf, lnum++,
1035 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1036 }
1037}
1038
1039/*
1040 * Add lines to the popup from a list of dictionaries.
1041 */
1042 static void
1043add_popup_dicts(buf_T *buf, list_T *l)
1044{
1045 listitem_T *li;
1046 listitem_T *pli;
1047 linenr_T lnum = 0;
1048 char_u *p;
1049 dict_T *dict;
1050
1051 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001052 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001053 {
1054 if (li->li_tv.v_type != VAR_DICT)
1055 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001056 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001057 return;
1058 }
1059 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001060 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001061 ml_append_buf(buf, lnum++,
1062 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1063 }
1064
1065 // add the text properties
1066 lnum = 1;
1067 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1068 {
1069 dictitem_T *di;
1070 list_T *plist;
1071
1072 dict = li->li_tv.vval.v_dict;
1073 di = dict_find(dict, (char_u *)"props", -1);
1074 if (di != NULL)
1075 {
1076 if (di->di_tv.v_type != VAR_LIST)
1077 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001078 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001079 return;
1080 }
1081 plist = di->di_tv.vval.v_list;
1082 if (plist != NULL)
1083 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001084 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001085 {
1086 if (pli->li_tv.v_type != VAR_DICT)
1087 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001088 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001089 return;
1090 }
1091 dict = pli->li_tv.vval.v_dict;
1092 if (dict != NULL)
1093 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001094 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001095
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001096 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001097 }
1098 }
1099 }
1100 }
1101 }
1102}
1103
1104/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001105 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1106 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001107 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001108popup_top_extra(win_T *wp)
1109{
1110 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1111
1112 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1113 return 1;
1114 return extra;
1115}
1116
1117/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001118 * Get the padding plus border at the left.
1119 */
1120 int
1121popup_left_extra(win_T *wp)
1122{
1123 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1124}
1125
1126/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001127 * Return the height of popup window "wp", including border and padding.
1128 */
1129 int
1130popup_height(win_T *wp)
1131{
1132 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001133 + popup_top_extra(wp)
1134 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001135}
1136
1137/*
1138 * Return the width of popup window "wp", including border, padding and
1139 * scrollbar.
1140 */
1141 int
1142popup_width(win_T *wp)
1143{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001144 // w_leftcol is how many columns of the core are left of the screen
1145 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001146 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001147 + popup_extra_width(wp)
1148 + wp->w_popup_rightoff;
1149}
1150
1151/*
1152 * Return the extra width of popup window "wp": border, padding and scrollbar.
1153 */
1154 int
1155popup_extra_width(win_T *wp)
1156{
1157 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1158 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1159 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001160}
1161
1162/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001163 * Adjust the position and size of the popup to fit on the screen.
1164 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001165 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001166popup_adjust_position(win_T *wp)
1167{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001168 linenr_T lnum;
1169 int wrapped = 0;
1170 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001171 int maxwidth_no_scrollbar;
1172 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001173 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001174 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001175 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001176 int center_vert = FALSE;
1177 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001178 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001179 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001180 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1181 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1182 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1183 int extra_height = top_extra + bot_extra;
1184 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001185 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001186 int org_winrow = wp->w_winrow;
1187 int org_wincol = wp->w_wincol;
1188 int org_width = wp->w_width;
1189 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001190 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001191 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001192 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001193 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001194 int wantline = wp->w_wantline; // adjusted for textprop
1195 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001196 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001197 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001198
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001199 wp->w_winrow = 0;
1200 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001201 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001202 wp->w_popup_leftoff = 0;
1203 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001204
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001205 // May need to update the "cursorline" highlighting, which may also change
1206 // "topline"
1207 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1208 popup_highlight_curline(wp);
1209
Bram Moolenaar12034e22019-08-25 22:25:02 +02001210 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1211 {
1212 win_T *prop_win = wp->w_popup_prop_win;
1213 textprop_T prop;
1214 linenr_T prop_lnum;
1215 pos_T pos;
1216 int screen_row;
1217 int screen_scol;
1218 int screen_ccol;
1219 int screen_ecol;
1220
1221 // Popup window is positioned relative to a text property.
1222 if (find_visible_prop(prop_win,
1223 wp->w_popup_prop_type, wp->w_popup_prop_id,
1224 &prop, &prop_lnum) == FAIL)
1225 {
1226 // Text property is no longer visible, hide the popup.
1227 // Unhiding the popup is done in check_popup_unhidden().
1228 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1229 {
1230 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001231 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001232 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001233 }
1234 return;
1235 }
1236
1237 // Compute the desired position from the position of the text
1238 // property. Use "wantline" and "wantcol" as offsets.
1239 pos.lnum = prop_lnum;
1240 pos.col = prop.tp_col;
1241 if (wp->w_popup_pos == POPPOS_TOPLEFT
1242 || wp->w_popup_pos == POPPOS_BOTLEFT)
1243 pos.col += prop.tp_len - 1;
1244 textpos2screenpos(prop_win, &pos, &screen_row,
1245 &screen_scol, &screen_ccol, &screen_ecol);
1246
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001247 if (screen_scol == 0)
1248 {
1249 // position is off screen, make the width zero to hide it.
1250 wp->w_width = 0;
1251 return;
1252 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001253 if (wp->w_popup_pos == POPPOS_TOPLEFT
1254 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1255 // below the text
1256 wantline = screen_row + wantline + 1;
1257 else
1258 // above the text
1259 wantline = screen_row + wantline - 1;
1260 center_vert = FALSE;
1261 if (wp->w_popup_pos == POPPOS_TOPLEFT
1262 || wp->w_popup_pos == POPPOS_BOTLEFT)
1263 // right of the text
1264 wantcol = screen_ecol + wantcol;
1265 else
1266 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001267 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001268 use_wantcol = TRUE;
1269 }
1270 else
1271 {
1272 // If no line was specified default to vertical centering.
1273 if (wantline == 0)
1274 center_vert = TRUE;
1275 else if (wantline < 0)
1276 // If "wantline" is negative it actually means zero.
1277 wantline = 0;
1278 if (wantcol < 0)
1279 // If "wantcol" is negative it actually means zero.
1280 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001281 }
1282
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001283 if (wp->w_popup_pos == POPPOS_CENTER)
1284 {
1285 // center after computing the size
1286 center_vert = TRUE;
1287 center_hor = TRUE;
1288 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001289 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001290 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001291 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1292 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001293 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001294 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001295 if (wp->w_winrow >= Rows)
1296 wp->w_winrow = Rows - 1;
1297 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001298 if (wp->w_popup_pos == POPPOS_BOTTOM)
1299 // assume that each buffer line takes one screen line
1300 wp->w_winrow = MAX(Rows - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001301
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001302 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001303 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001304 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1305 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001306 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001307 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001308 // Need to see at least one character after the decoration.
1309 if (wp->w_wincol > Columns - left_extra - 1)
1310 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001311 }
1312 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001313
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001314 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001315 // When left aligned use the space available, but shift to the left when we
1316 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001317 maxspace = Columns - wp->w_wincol - left_extra;
1318 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001319 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001320 {
1321 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001322 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001323 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001324
1325 if (wp->w_p_nu || wp->w_p_rnu)
1326 margin_width = number_width(wp) + 1;
1327#ifdef FEAT_FOLDING
1328 margin_width += wp->w_p_fdc;
1329#endif
1330#ifdef FEAT_SIGNS
1331 if (signcolumn_on(wp))
1332 margin_width += 2;
1333#endif
1334 if (margin_width >= maxwidth)
1335 margin_width = maxwidth - 1;
1336
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001337 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001338 minheight = wp->w_minheight;
1339#ifdef FEAT_TERMINAL
1340 // A terminal popup initially does not have content, use a default minimal
1341 // width of 20 characters and height of 5 lines.
1342 if (wp->w_buffer->b_term != NULL)
1343 {
1344 if (minwidth == 0)
1345 minwidth = 20;
1346 if (minheight == 0)
1347 minheight = 5;
1348 }
1349#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001350
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001351 if (wp->w_maxheight > 0)
1352 maxheight = wp->w_maxheight;
1353
Bram Moolenaar8d241042019-06-12 23:40:01 +02001354 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001355 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001356 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001357 if (wp->w_topline < 1)
1358 wp->w_topline = 1;
1359 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001360 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1361
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001362 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001363 // Use a minimum width of one, so that something shows when there is no
1364 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001365 // When "firstline" is -1 then start with the last buffer line and go
1366 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001367 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001368 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001369 if (wp->w_firstline < 0)
1370 lnum = wp->w_buffer->b_ml.ml_line_count;
1371 else
1372 lnum = wp->w_topline;
1373 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001374 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001375 int len;
1376 int w_width = wp->w_width;
1377
1378 // Count Tabs for what they are worth and compute the length based on
1379 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001380 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001381 if (wp->w_width < maxwidth)
1382 wp->w_width = maxwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001383 len = win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001384 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001385 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001386
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001387 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001388 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001389 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001390 {
1391 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001392 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001393 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001394 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001395 }
1396 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001397 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001398 && allow_adjust_left
1399 && (wp->w_popup_pos == POPPOS_TOPLEFT
1400 || wp->w_popup_pos == POPPOS_BOTLEFT))
1401 {
1402 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001403 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001404
Bram Moolenaar51c31312019-06-15 22:27:23 +02001405 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001406 {
1407 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001408
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001409 len -= truncate_shift;
1410 shift_by -= truncate_shift;
1411 }
1412
1413 wp->w_wincol -= shift_by;
1414 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001415 wp->w_width = maxwidth;
1416 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001417 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001418 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001419 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001420 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1421 wp->w_width = wp->w_maxwidth;
1422 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001423
1424 if (wp->w_firstline < 0)
1425 --lnum;
1426 else
1427 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001428
1429 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001430 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001431 && (wp->w_firstline >= 0
1432 ? lnum - wp->w_topline
1433 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001434 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001435 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001436 }
1437
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001438 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001439 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001440
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001441 wp->w_has_scrollbar = wp->w_want_scrollbar
1442 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001443#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001444 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1445 // Terminal window with running job never has a scrollbar, adjusts to
1446 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001447 wp->w_has_scrollbar = FALSE;
1448#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001449 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001450 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001451 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001452 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001453 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001454 // make space for the scrollbar if needed, when lines wrap and when
1455 // applying minwidth
1456 if (maxwidth + right_extra >= maxspace
1457 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1458 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001459 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001460
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001461 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1462 {
1463 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1464
1465 if (minwidth < title_len)
1466 minwidth = title_len;
1467 }
1468
1469 if (minwidth > 0 && wp->w_width < minwidth)
1470 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001471 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001472 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001473 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001474 // some columns cut off on the right
1475 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001476
1477 // If the window doesn't fit because 'minwidth' is set then the
1478 // scrollbar is at the far right of the screen, use the size without
1479 // the scrollbar.
1480 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1481 {
1482 int off = wp->w_width - maxwidth;
1483
1484 if (off > right_extra)
1485 extra_width -= right_extra;
1486 else
1487 extra_width -= off;
1488 wp->w_width = maxwidth_no_scrollbar;
1489 }
1490 else
1491 {
1492 wp->w_width = maxwidth;
1493
1494 // when adding a scrollbar below need to adjust the width
1495 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1496 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001497 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001498 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001499 {
1500 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1501 if (wp->w_wincol < 0)
1502 wp->w_wincol = 0;
1503 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001504 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1505 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1506 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001507 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001508
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001509 // Right aligned: move to the right if needed.
1510 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001511 if (leftoff >= 0)
1512 wp->w_wincol = leftoff;
1513 else if (wp->w_popup_fixed)
1514 {
1515 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001516 // columns when displaying the window, minus left border and
1517 // padding.
1518 if (-leftoff > left_extra)
1519 wp->w_leftcol = -leftoff - left_extra;
1520 wp->w_width -= wp->w_leftcol;
1521 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001522 if (wp->w_width < 0)
1523 wp->w_width = 0;
1524 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001525 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001526
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001527 if (wp->w_p_wrap || (!wp->w_popup_fixed
1528 && (wp->w_popup_pos == POPPOS_TOPLEFT
1529 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001530 {
1531 int want_col = 0;
1532
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001533 // try to show the right border and any scrollbar
1534 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001535 if (want_col > 0 && wp->w_wincol > 0
1536 && wp->w_wincol + want_col >= Columns)
1537 {
1538 wp->w_wincol = Columns - want_col;
1539 if (wp->w_wincol < 0)
1540 wp->w_wincol = 0;
1541 }
1542 }
1543
Bram Moolenaar8d241042019-06-12 23:40:01 +02001544 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1545 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001546 if (minheight > 0 && wp->w_height < minheight)
1547 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001548 if (maxheight > 0 && wp->w_height > maxheight)
1549 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001550 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001551 if (wp->w_height > Rows - wp->w_winrow)
1552 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001553
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001554 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001555 {
1556 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1557 if (wp->w_winrow < 0)
1558 wp->w_winrow = 0;
1559 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001560 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001561 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001562 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001563 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001564 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001565 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001566 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1567 {
1568 // Bottom aligned but does not fit, and less space on the other
1569 // side or "posinvert" is off: reduce height.
1570 wp->w_winrow = 0;
1571 wp->w_height = wantline - extra_height;
1572 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001573 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001574 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001575 // Not enough space and more space on the other side: make top
1576 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001577 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001578 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001579 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001580 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001581 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1582 || wp->w_popup_pos == POPPOS_TOPLEFT)
1583 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001584 if (wp != popup_dragwin
1585 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001586 && wantline * 2 > Rows
1587 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001588 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001589 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001590 // make bottom aligned and recompute the height
1591 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001592 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001593 if (wp->w_winrow < 0)
1594 {
1595 wp->w_height += wp->w_winrow;
1596 wp->w_winrow = 0;
1597 }
1598 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001599 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001600 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001601 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001602 adjust_height_for_top_aligned = TRUE;
1603 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001604 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001605
1606 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1607 && wp->w_winrow + wp->w_height + extra_height > Rows)
1608 {
1609 // Bottom of the popup goes below the last line, reduce the height and
1610 // add a scrollbar.
1611 wp->w_height = Rows - wp->w_winrow - extra_height;
1612#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001613 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001614#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001615 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001616 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001617 if (width_with_scrollbar > 0)
1618 wp->w_width = width_with_scrollbar;
1619 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001620 }
1621
1622 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001623 if (wp->w_winrow >= Rows)
1624 wp->w_winrow = Rows - 1;
1625 else if (wp->w_winrow < 0)
1626 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001627
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001628 if (wp->w_height != org_height)
1629 win_comp_scroll(wp);
1630
Bram Moolenaar17146962019-05-30 00:12:11 +02001631 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001632 if (win_valid(wp->w_popup_prop_win))
1633 {
1634 wp->w_popup_prop_changedtick =
1635 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1636 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1637 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001638
1639 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001640 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001641 if (org_winrow != wp->w_winrow
1642 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001643 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001644 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001645 || org_width != wp->w_width
1646 || org_height != wp->w_height)
1647 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001648 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001649 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1650 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001651 popup_mask_refresh = TRUE;
1652 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001653}
1654
Bram Moolenaar17627312019-06-02 19:53:44 +02001655typedef enum
1656{
1657 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001658 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001659 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001660 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001661 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001662 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001663 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001664 TYPE_PREVIEW, // preview window
1665 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001666} create_type_T;
1667
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001668/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001669 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1670 */
1671 static int
1672popup_is_notification(create_type_T type)
1673{
1674 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1675}
1676
1677/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001678 * Make "buf" empty and set the contents to "text".
1679 * Used by popup_create() and popup_settext().
1680 */
1681 static void
1682popup_set_buffer_text(buf_T *buf, typval_T text)
1683{
1684 int lnum;
1685
1686 // Clear the buffer, then replace the lines.
1687 curbuf = buf;
1688 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001689 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001690 curbuf = curwin->w_buffer;
1691
1692 // Add text to the buffer.
1693 if (text.v_type == VAR_STRING)
1694 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001695 char_u *s = text.vval.v_string;
1696
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001697 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001698 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001699 }
1700 else
1701 {
1702 list_T *l = text.vval.v_list;
1703
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001704 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001705 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001706 if (l->lv_first == &range_list_item)
1707 emsg(_(e_using_number_as_string));
1708 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001709 // list of strings
1710 add_popup_strings(buf, l);
1711 else
1712 // list of dictionaries
1713 add_popup_dicts(buf, l);
1714 }
1715 }
1716
1717 // delete the line that was in the empty buffer
1718 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001719 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001720 curbuf = curwin->w_buffer;
1721}
1722
1723/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001724 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1725 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001726 * Return FAIL if the parsing fails.
1727 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001728 static int
1729parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001730{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001731 char_u *p =
1732#ifdef FEAT_QUICKFIX
1733 !is_preview ? p_cpp :
1734#endif
1735 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001736
Bram Moolenaar258cef52019-08-21 17:29:29 +02001737 if (wp != NULL)
1738 wp->w_popup_flags &= ~POPF_INFO_MENU;
1739
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001740 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001741 {
1742 char_u *e, *dig;
1743 char_u *s = p;
1744 int x;
1745
1746 e = vim_strchr(p, ':');
1747 if (e == NULL || e[1] == NUL)
1748 return FAIL;
1749
1750 p = vim_strchr(e, ',');
1751 if (p == NULL)
1752 p = e + STRLEN(e);
1753 dig = e + 1;
1754 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001755
1756 if (STRNCMP(s, "height:", 7) == 0)
1757 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001758 if (dig != p)
1759 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001760 if (wp != NULL)
1761 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001762 if (is_preview)
1763 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001764 wp->w_maxheight = x;
1765 }
1766 }
1767 else if (STRNCMP(s, "width:", 6) == 0)
1768 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001769 if (dig != p)
1770 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001771 if (wp != NULL)
1772 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001773 if (is_preview)
1774 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001775 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001776 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001777 }
1778 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001779 else if (STRNCMP(s, "highlight:", 10) == 0)
1780 {
1781 if (wp != NULL)
1782 {
1783 int c = *p;
1784
1785 *p = NUL;
1786 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1787 s + 10, OPT_FREE|OPT_LOCAL, 0);
1788 *p = c;
1789 }
1790 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001791 else if (STRNCMP(s, "border:", 7) == 0)
1792 {
1793 char_u *arg = s + 7;
1794 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1795 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1796 int i;
1797
1798 if (!on && !off)
1799 return FAIL;
1800 if (wp != NULL)
1801 {
1802 for (i = 0; i < 4; ++i)
1803 wp->w_popup_border[i] = on ? 1 : 0;
1804 if (off)
1805 // only show the X for close when there is a border
1806 wp->w_popup_close = POPCLOSE_NONE;
1807 }
1808 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001809 else if (STRNCMP(s, "align:", 6) == 0)
1810 {
1811 char_u *arg = s + 6;
1812 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1813 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1814
1815 if (!menu && !item)
1816 return FAIL;
1817 if (wp != NULL && menu)
1818 wp->w_popup_flags |= POPF_INFO_MENU;
1819 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001820 else
1821 return FAIL;
1822 }
1823 return OK;
1824}
1825
1826/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001827 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1828 * is not NULL.
1829 * Return FAIL if the parsing fails.
1830 */
1831 int
1832parse_previewpopup(win_T *wp)
1833{
1834 return parse_popup_option(wp, TRUE);
1835}
1836
1837/*
1838 * Parse the 'completepopup' option and apply the values to window "wp" if it
1839 * is not NULL.
1840 * Return FAIL if the parsing fails.
1841 */
1842 int
1843parse_completepopup(win_T *wp)
1844{
1845 return parse_popup_option(wp, FALSE);
1846}
1847
1848/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001849 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001850 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001851 */
1852 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001853popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001854{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001855 poppos_T ppt = POPPOS_NONE;
1856
1857 if (d != NULL)
1858 ppt = get_pos_entry(d, FALSE);
1859
Bram Moolenaar79648732019-07-18 21:43:07 +02001860 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001861 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001862 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001863 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1864 }
1865 else
1866 {
1867 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1868 if (wp->w_wantline == 0) // cursor in first line
1869 {
1870 wp->w_wantline = 2;
1871 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1872 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1873 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001874 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001875
Bram Moolenaar79648732019-07-18 21:43:07 +02001876 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001877 if (wp->w_wantcol > Columns - width)
1878 {
1879 wp->w_wantcol = Columns - width;
1880 if (wp->w_wantcol < 1)
1881 wp->w_wantcol = 1;
1882 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001883
Bram Moolenaar79648732019-07-18 21:43:07 +02001884 popup_adjust_position(wp);
1885}
1886
1887/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001888 * Set w_wantline and w_wantcol for the a given screen position.
1889 * Caller must take care of running into the window border.
1890 */
1891 void
1892popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1893{
1894 wp->w_wantline = row;
1895 wp->w_wantcol = col;
1896 popup_adjust_position(wp);
1897}
1898
1899/*
1900 * Add a border and lef&right padding.
1901 */
1902 static void
1903add_border_left_right_padding(win_T *wp)
1904{
1905 int i;
1906
1907 for (i = 0; i < 4; ++i)
1908 {
1909 wp->w_popup_border[i] = 1;
1910 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1911 }
1912}
1913
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001914#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001915/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001916 * Return TRUE if there is any popup window with a terminal buffer.
1917 */
1918 static int
1919popup_terminal_exists(void)
1920{
1921 win_T *wp;
1922 tabpage_T *tp;
1923
1924 FOR_ALL_POPUPWINS(wp)
1925 if (wp->w_buffer->b_term != NULL)
1926 return TRUE;
1927 FOR_ALL_TABPAGES(tp)
1928 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1929 if (wp->w_buffer->b_term != NULL)
1930 return TRUE;
1931 return FALSE;
1932}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001933#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001934
1935/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001936 * Set the color for a notification window.
1937 */
1938 static void
1939popup_update_color(win_T *wp, create_type_T type)
1940{
1941 char *hiname = type == TYPE_MESSAGE_WIN
1942 ? "MessageWindow" : "PopupNotification";
1943 int nr = syn_name2id((char_u *)hiname);
1944
1945 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1946 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1947 OPT_FREE|OPT_LOCAL, 0);
1948}
1949
1950/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001951 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001952 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001953 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001954 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001955 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001956 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001957popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001958{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001959 win_T *wp;
1960 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001961 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001962 int new_buffer;
1963 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001964 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001965 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001966
Bram Moolenaar79648732019-07-18 21:43:07 +02001967 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001968 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001969 if (in_vim9script()
1970 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1971 || check_for_dict_arg(argvars, 1) == FAIL))
1972 return NULL;
1973
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001974 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001975 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001976 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001977 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001978 if (buf == NULL)
1979 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001980 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001981 return NULL;
1982 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001983#ifdef FEAT_TERMINAL
1984 if (buf->b_term != NULL && popup_terminal_exists())
1985 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001986 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001987 return NULL;
1988 }
1989#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001990 }
1991 else if (!(argvars[0].v_type == VAR_STRING
1992 && argvars[0].vval.v_string != NULL)
1993 && !(argvars[0].v_type == VAR_LIST
1994 && argvars[0].vval.v_list != NULL))
1995 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001996 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001997 return NULL;
1998 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001999 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002000 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002001 emsg(_(e_dictionary_required));
Bram Moolenaara3fce622019-06-20 02:31:49 +02002002 return NULL;
2003 }
Bram Moolenaar79648732019-07-18 21:43:07 +02002004 d = argvars[1].vval.v_dict;
2005 }
2006
2007 if (d != NULL)
2008 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002009 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002010 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002011 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002012 tabnr = -1; // notifications are global by default
2013 else
2014 tabnr = 0;
2015 if (tabnr > 0)
2016 {
2017 tp = find_tabpage(tabnr);
2018 if (tp == NULL)
2019 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002020 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002021 return NULL;
2022 }
2023 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002024 }
2025
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002026 // Create the window and buffer.
2027 wp = win_alloc_popup_win();
2028 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002029 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002030 if (rettv != NULL)
2031 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002032 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002033 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002034
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002035 if (buf != NULL)
2036 {
2037 // use existing buffer
2038 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002039 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002040 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002041 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002042 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002043 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002044 }
2045 else
2046 {
2047 // create a new buffer associated with the popup
2048 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002049 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002050 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002051 {
2052 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002053 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002054 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002055 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002056
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002057 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002058
Bram Moolenaar46451042019-08-24 15:50:46 +02002059 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002060 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002061 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002062 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002063 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002064 buf->b_p_ul = -1; // no undo
2065 buf->b_p_swf = FALSE; // no swap file
2066 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002067 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002068
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002069 // Avoid that 'buftype' is reset when this buffer is entered.
2070 buf->b_p_initialized = TRUE;
2071 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002072 wp->w_p_wrap = TRUE; // 'wrap' is default on
2073 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002074
Bram Moolenaara3fce622019-06-20 02:31:49 +02002075 if (tp != NULL)
2076 {
2077 // popup on specified tab page
2078 wp->w_next = tp->tp_first_popupwin;
2079 tp->tp_first_popupwin = wp;
2080 }
2081 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002082 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002083 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002084 wp->w_next = curtab->tp_first_popupwin;
2085 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002086 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002087 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002088 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002089 win_T *prev = first_popupwin;
2090
2091 // Global popup: add at the end, so that it gets displayed on top of
2092 // older ones with the same zindex. Matters for notifications.
2093 if (first_popupwin == NULL)
2094 first_popupwin = wp;
2095 else
2096 {
2097 while (prev->w_next != NULL)
2098 prev = prev->w_next;
2099 prev->w_next = wp;
2100 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002101 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002102
Bram Moolenaar79648732019-07-18 21:43:07 +02002103 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002104 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002105
Bram Moolenaar79648732019-07-18 21:43:07 +02002106 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002107 {
2108 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002109 }
2110 if (type == TYPE_ATCURSOR)
2111 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002112 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002113 set_moved_values(wp);
2114 set_moved_columns(wp, FIND_STRING);
2115 }
2116
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002117 if (type == TYPE_BEVAL)
2118 {
2119 wp->w_popup_pos = POPPOS_BOTLEFT;
2120
2121 // by default use the mouse position
2122 wp->w_wantline = mouse_row;
2123 if (wp->w_wantline <= 0) // mouse on first line
2124 {
2125 wp->w_wantline = 2;
2126 wp->w_popup_pos = POPPOS_TOPLEFT;
2127 }
2128 wp->w_wantcol = mouse_col + 1;
2129 set_mousemoved_values(wp);
2130 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2131 }
2132
Bram Moolenaar33796b32019-06-08 16:01:13 +02002133 // set default values
2134 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002135 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002136
Bram Moolenaar9198de32022-08-27 21:30:03 +01002137 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002138 {
2139 win_T *twp, *nextwin;
2140 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002141
2142 // Try to not overlap with another global popup. Guess we need 3
2143 // more screen lines than buffer lines.
2144 wp->w_wantline = 1;
2145 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2146 {
2147 nextwin = twp->w_next;
2148 if (twp != wp
2149 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2150 && twp->w_winrow <= wp->w_wantline - 1 + height
2151 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2152 {
2153 // move to below this popup and restart the loop to check for
2154 // overlap with other popups
2155 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2156 nextwin = first_popupwin;
2157 }
2158 }
2159 if (wp->w_wantline + height > Rows)
2160 {
2161 // can't avoid overlap, put on top in the hope that message goes
2162 // away soon.
2163 wp->w_wantline = 1;
2164 }
2165
2166 wp->w_wantcol = 10;
2167 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002168 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002169 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002170 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002171 for (i = 0; i < 4; ++i)
2172 wp->w_popup_border[i] = 1;
2173 wp->w_popup_padding[1] = 1;
2174 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002175
Bram Moolenaar9198de32022-08-27 21:30:03 +01002176 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002177 }
2178
Bram Moolenaara730e552019-06-16 19:05:31 +02002179 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002180 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002181 wp->w_popup_pos = POPPOS_CENTER;
2182 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002183 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002184 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002185 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002186 }
2187
Bram Moolenaara730e552019-06-16 19:05:31 +02002188 if (type == TYPE_MENU)
2189 {
2190 typval_T tv;
2191 callback_T callback;
2192
2193 tv.v_type = VAR_STRING;
2194 tv.vval.v_string = (char_u *)"popup_filter_menu";
2195 callback = get_callback(&tv);
2196 if (callback.cb_name != NULL)
2197 set_callback(&wp->w_filter_cb, &callback);
2198
2199 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002200 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002201 }
2202
Bram Moolenaar79648732019-07-18 21:43:07 +02002203 if (type == TYPE_PREVIEW)
2204 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002205 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002206 wp->w_popup_close = POPCLOSE_BUTTON;
2207 for (i = 0; i < 4; ++i)
2208 wp->w_popup_border[i] = 1;
2209 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002210 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002211 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002212# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002213 if (type == TYPE_INFO)
2214 {
2215 wp->w_popup_pos = POPPOS_TOPLEFT;
2216 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2217 wp->w_popup_close = POPCLOSE_BUTTON;
2218 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002219 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002220 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002221# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002222
Bram Moolenaarae943152019-06-16 22:54:14 +02002223 for (i = 0; i < 4; ++i)
2224 VIM_CLEAR(wp->w_border_highlight[i]);
2225 for (i = 0; i < 8; ++i)
2226 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002227 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002228 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002229 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002230
Bram Moolenaar79648732019-07-18 21:43:07 +02002231 if (d != NULL)
2232 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002233 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002234
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002235#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002236 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2237 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002238#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002239
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002240 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002241
2242 wp->w_vsep_width = 0;
2243
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002244 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002245 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002246
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002247#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002248 // When running a terminal in the popup it becomes the current window.
2249 if (buf->b_term != NULL)
2250 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002251#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002252
Bram Moolenaara730e552019-06-16 19:05:31 +02002253 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002254}
2255
2256/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002257 * popup_clear()
2258 */
2259 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002260f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002261{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002262 int force = FALSE;
2263
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002264 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2265 return;
2266
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002267 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002268 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002269 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002270}
2271
2272/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002273 * popup_create({text}, {options})
2274 */
2275 void
2276f_popup_create(typval_T *argvars, typval_T *rettv)
2277{
Bram Moolenaar17627312019-06-02 19:53:44 +02002278 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002279}
2280
2281/*
2282 * popup_atcursor({text}, {options})
2283 */
2284 void
2285f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2286{
Bram Moolenaar17627312019-06-02 19:53:44 +02002287 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002288}
2289
2290/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002291 * popup_beval({text}, {options})
2292 */
2293 void
2294f_popup_beval(typval_T *argvars, typval_T *rettv)
2295{
2296 popup_create(argvars, rettv, TYPE_BEVAL);
2297}
2298
2299/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002300 * Invoke the close callback for window "wp" with value "result".
2301 * Careful: The callback may make "wp" invalid!
2302 */
2303 static void
2304invoke_popup_callback(win_T *wp, typval_T *result)
2305{
2306 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002307 typval_T argv[3];
2308
2309 argv[0].v_type = VAR_NUMBER;
2310 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2311
2312 if (result != NULL && result->v_type != VAR_UNKNOWN)
2313 copy_tv(result, &argv[1]);
2314 else
2315 {
2316 argv[1].v_type = VAR_NUMBER;
2317 argv[1].vval.v_number = 0;
2318 }
2319
2320 argv[2].v_type = VAR_UNKNOWN;
2321
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002322 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002323 if (result != NULL)
2324 clear_tv(&argv[1]);
2325 clear_tv(&rettv);
2326}
2327
2328/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002329 * Make "prevwin" the current window, unless it's equal to "wp".
2330 * Otherwise make "firstwin" the current window.
2331 */
2332 static void
2333back_to_prevwin(win_T *wp)
2334{
2335 if (win_valid(prevwin) && wp != prevwin)
2336 win_enter(prevwin, FALSE);
2337 else
2338 win_enter(firstwin, FALSE);
2339}
2340
2341/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002342 * Close popup "wp" and invoke any close callback for it.
2343 */
2344 static void
2345popup_close_and_callback(win_T *wp, typval_T *arg)
2346{
2347 int id = wp->w_id;
2348
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002349#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002350 if (wp == curwin && curbuf->b_term != NULL)
2351 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002352 win_T *owp;
2353
2354 // Closing popup window with a terminal: put focus back on the first
2355 // that works:
2356 // - another popup window with a terminal
2357 // - the previous window
2358 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002359 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002360 if (owp != curwin && owp->w_buffer->b_term != NULL)
2361 break;
2362 if (owp != NULL)
2363 win_enter(owp, FALSE);
2364 else
2365 {
2366 for (owp = curtab->tp_first_popupwin; owp != NULL;
2367 owp = owp->w_next)
2368 if (owp != curwin && owp->w_buffer->b_term != NULL)
2369 break;
2370 if (owp != NULL)
2371 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002372 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002373 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002374 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002375 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002376#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002377
Bram Moolenaar49540192019-12-11 19:34:54 +01002378 // Just in case a check higher up is missing.
2379 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002380 {
2381 // To avoid getting stuck when win_execute() does something that causes
2382 // an error, stop calling the filter callback.
2383 free_callback(&wp->w_filter_cb);
2384
Bram Moolenaar49540192019-12-11 19:34:54 +01002385 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002386 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002387
Bram Moolenaarcee52202020-03-11 14:19:58 +01002388 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002389 if (wp->w_close_cb.cb_name != NULL)
2390 // Careful: This may make "wp" invalid.
2391 invoke_popup_callback(wp, arg);
2392
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002393 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002394 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002395}
2396
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002397 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002398popup_close_with_retval(win_T *wp, int retval)
2399{
2400 typval_T res;
2401
2402 res.v_type = VAR_NUMBER;
2403 res.vval.v_number = retval;
2404 popup_close_and_callback(wp, &res);
2405}
2406
Bram Moolenaar3397f742019-06-02 18:40:06 +02002407/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002408 * Close popup "wp" because of a mouse click.
2409 */
2410 void
2411popup_close_for_mouse_click(win_T *wp)
2412{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002413 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002414}
2415
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002416 static void
2417check_mouse_moved(win_T *wp, win_T *mouse_wp)
2418{
2419 // Close the popup when all if these are true:
2420 // - the mouse is not on this popup
2421 // - "mousemoved" was used
2422 // - the mouse is no longer on the same screen row or the mouse column is
2423 // outside of the relevant text
2424 if (wp != mouse_wp
2425 && wp->w_popup_mouse_row != 0
2426 && (wp->w_popup_mouse_row != mouse_row
2427 || mouse_col < wp->w_popup_mouse_mincol
2428 || mouse_col > wp->w_popup_mouse_maxcol))
2429 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002430 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002431 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002432 }
2433}
2434
2435/*
2436 * Called when the mouse moved: may close a popup with "mousemoved".
2437 */
2438 void
2439popup_handle_mouse_moved(void)
2440{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002441 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002442 win_T *mouse_wp;
2443 int row = mouse_row;
2444 int col = mouse_col;
2445
2446 // find the window where the mouse is in
2447 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2448
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002449 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2450 {
2451 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002452 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002453 }
2454 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2455 {
2456 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002457 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002458 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002459}
2460
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002461/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002462 * In a filter: check if the typed key is a mouse event that is used for
2463 * dragging the popup.
2464 */
2465 static void
2466filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2467{
2468 int row = mouse_row;
2469 int col = mouse_col;
2470
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002471 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002472 && is_mouse_key(c)
2473 && (wp == popup_dragwin
2474 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2475 // do not consume the key, allow for dragging the popup
2476 rettv->vval.v_number = 0;
2477}
2478
Bram Moolenaara730e552019-06-16 19:05:31 +02002479/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002480 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002481 */
2482 void
2483f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2484{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002485 int id;
2486 win_T *wp;
2487 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002488 typval_T res;
2489 int c;
2490 linenr_T old_lnum;
2491
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002492 if (in_vim9script()
2493 && (check_for_number_arg(argvars, 0) == FAIL
2494 || check_for_string_arg(argvars, 1) == FAIL))
2495 return;
2496
2497 id = tv_get_number(&argvars[0]);
2498 wp = win_id2wp(id);
2499 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002500 // If the popup has been closed do not consume the key.
2501 if (wp == NULL)
2502 return;
2503
2504 c = *key;
2505 if (c == K_SPECIAL && key[1] != NUL)
2506 c = TO_SPECIAL(key[1], key[2]);
2507
2508 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002509 rettv->v_type = VAR_BOOL;
2510 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002511 res.v_type = VAR_NUMBER;
2512
2513 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002514 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2515 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002516 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002517 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002518 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2519 ++wp->w_cursor.lnum;
2520 if (old_lnum != wp->w_cursor.lnum)
2521 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002522 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002523 return;
2524 }
2525
2526 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2527 {
2528 // Cancelled, invoke callback with -1
2529 res.vval.v_number = -1;
2530 popup_close_and_callback(wp, &res);
2531 return;
2532 }
2533 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2534 {
2535 // Invoke callback with current index.
2536 res.vval.v_number = wp->w_cursor.lnum;
2537 popup_close_and_callback(wp, &res);
2538 return;
2539 }
2540
2541 filter_handle_drag(wp, c, rettv);
2542}
2543
2544/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002545 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002546 */
2547 void
2548f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2549{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002550 int id;
2551 win_T *wp;
2552 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002553 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002554 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002555
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002556 if (in_vim9script()
2557 && (check_for_number_arg(argvars, 0) == FAIL
2558 || check_for_string_arg(argvars, 1) == FAIL))
2559 return;
2560
2561 id = tv_get_number(&argvars[0]);
2562 wp = win_id2wp(id);
2563 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002564 // If the popup has been closed don't consume the key.
2565 if (wp == NULL)
2566 return;
2567
Bram Moolenaara730e552019-06-16 19:05:31 +02002568 c = *key;
2569 if (c == K_SPECIAL && key[1] != NUL)
2570 c = TO_SPECIAL(key[1], key[2]);
2571
Bram Moolenaara42d9452019-06-15 21:46:30 +02002572 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002573 rettv->v_type = VAR_BOOL;
2574 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002575
Bram Moolenaara730e552019-06-16 19:05:31 +02002576 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002577 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002578 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002579 res.vval.v_number = 0;
2580 else
2581 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002582 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002583 return;
2584 }
2585
2586 // Invoke callback
2587 res.v_type = VAR_NUMBER;
2588 popup_close_and_callback(wp, &res);
2589}
2590
2591/*
2592 * popup_dialog({text}, {options})
2593 */
2594 void
2595f_popup_dialog(typval_T *argvars, typval_T *rettv)
2596{
2597 popup_create(argvars, rettv, TYPE_DIALOG);
2598}
2599
2600/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002601 * popup_menu({text}, {options})
2602 */
2603 void
2604f_popup_menu(typval_T *argvars, typval_T *rettv)
2605{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002606 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002607}
2608
2609/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002610 * popup_notification({text}, {options})
2611 */
2612 void
2613f_popup_notification(typval_T *argvars, typval_T *rettv)
2614{
2615 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2616}
2617
2618/*
2619 * Find the popup window with window-ID "id".
2620 * If the popup window does not exist NULL is returned.
2621 * If the window is not a popup window, and error message is given.
2622 */
2623 static win_T *
2624find_popup_win(int id)
2625{
2626 win_T *wp = win_id2wp(id);
2627
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002628 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002629 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002630 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002631 return NULL;
2632 }
2633 return wp;
2634}
2635
2636/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002637 * popup_close({id})
2638 */
2639 void
2640f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2641{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002642 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002643 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002644
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002645 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2646 return;
2647
2648 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002649 if (
2650# ifdef FEAT_TERMINAL
2651 // if the popup contains a terminal it will become hidden
2652 curbuf->b_term == NULL &&
2653# endif
2654 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002655 return;
2656
2657 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002658 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002659 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002660}
2661
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002662 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002663popup_hide(win_T *wp)
2664{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002665#ifdef FEAT_TERMINAL
2666 if (error_if_term_popup_window())
2667 return;
2668#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002669 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2670 {
2671 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002672 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002673 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002674 popup_mask_refresh = TRUE;
2675 }
2676}
2677
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002678/*
2679 * popup_hide({id})
2680 */
2681 void
2682f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2683{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002684 int id;
2685 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002686
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002687 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2688 return;
2689
2690 id = (int)tv_get_number(argvars);
2691 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002692 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002693 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002694 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002695 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2696 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002697}
2698
2699 void
2700popup_show(win_T *wp)
2701{
2702 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002703 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002704 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002705 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002706 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002707 }
2708}
2709
2710/*
2711 * popup_show({id})
2712 */
2713 void
2714f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2715{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002716 int id;
2717 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002718
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002719 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2720 return;
2721
2722 id = (int)tv_get_number(argvars);
2723 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002724 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002725 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002726 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002727 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002728#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002729 if (wp->w_popup_flags & POPF_INFO)
2730 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002731#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002732 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002733}
2734
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002735/*
2736 * popup_settext({id}, {text})
2737 */
2738 void
2739f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2740{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002741 int id;
2742 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002743
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002744 if (in_vim9script()
2745 && (check_for_number_arg(argvars, 0) == FAIL
2746 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2747 return;
2748
2749 id = (int)tv_get_number(&argvars[0]);
2750 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002751 if (wp != NULL)
2752 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002753 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002754 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002755 else
2756 {
2757 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002758 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002759 popup_adjust_position(wp);
2760 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002761 }
2762}
2763
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002764 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002765popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002766{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002767 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002768 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002769 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002770 clear_cmdline = TRUE;
2771 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002772
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002773 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002774 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002775}
2776
Bram Moolenaar82106932020-03-13 14:34:38 +01002777 static void
2778error_for_popup_window(void)
2779{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002780 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002781}
2782
2783 int
2784error_if_popup_window(int also_with_term UNUSED)
2785{
2786 // win_execute() may set "curwin" to a popup window temporarily, but many
2787 // commands are disallowed then. When a terminal runs in the popup most
2788 // things are allowed. When a terminal is finished it can be closed.
2789 if (WIN_IS_POPUP(curwin)
2790# ifdef FEAT_TERMINAL
2791 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002792# endif
2793 )
2794 {
2795 error_for_popup_window();
2796 return TRUE;
2797 }
2798 return FALSE;
2799}
2800
Bram Moolenaarec583842019-05-26 14:11:23 +02002801/*
2802 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002803 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002804 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002805 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002806 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002807popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002808{
2809 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002810 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002811 win_T *prev = NULL;
2812
Bram Moolenaarec583842019-05-26 14:11:23 +02002813 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002814 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002815 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002816 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002817 if (wp == curwin)
2818 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002819 if (!force)
2820 {
2821 error_for_popup_window();
2822 return FAIL;
2823 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002824 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002825 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002826 if (prev == NULL)
2827 first_popupwin = wp->w_next;
2828 else
2829 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002830 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002831 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002832 }
2833
Bram Moolenaarec583842019-05-26 14:11:23 +02002834 // go through tab-local popups
2835 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002836 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002837 return OK;
2838 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002839}
2840
2841/*
2842 * Close a popup window with Window-id "id" in tabpage "tp".
2843 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002844 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002845popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002846{
2847 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002848 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002849 win_T *prev = NULL;
2850
Bram Moolenaarec583842019-05-26 14:11:23 +02002851 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2852 if (wp->w_id == id)
2853 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002854 if (wp == curwin)
2855 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002856 if (!force)
2857 {
2858 error_for_popup_window();
2859 return FAIL;
2860 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002861 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002862 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002863 if (prev == NULL)
2864 *root = wp->w_next;
2865 else
2866 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002867 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002868 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002869 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002870 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002871}
2872
2873 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002874close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002875{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002876 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002877 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002878 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002879 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002880 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002881 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002882 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002883 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002884}
2885
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002886/*
2887 * popup_move({id}, {options})
2888 */
2889 void
2890f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2891{
Bram Moolenaarae943152019-06-16 22:54:14 +02002892 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002893 int id;
2894 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002895
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002896 if (in_vim9script()
2897 && (check_for_number_arg(argvars, 0) == FAIL
2898 || check_for_dict_arg(argvars, 1) == FAIL))
2899 return;
2900
2901 id = (int)tv_get_number(argvars);
2902 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002903 if (wp == NULL)
2904 return; // invalid {id}
2905
2906 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2907 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002908 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002909 return;
2910 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002911 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002912
Bram Moolenaarae943152019-06-16 22:54:14 +02002913 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002914
2915 if (wp->w_winrow + wp->w_height >= cmdline_row)
2916 clear_cmdline = TRUE;
2917 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002918}
2919
Bram Moolenaarbc133542019-05-29 20:26:46 +02002920/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002921 * popup_setoptions({id}, {options})
2922 */
2923 void
2924f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2925{
2926 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002927 int id;
2928 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002929 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002930
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002931 if (in_vim9script()
2932 && (check_for_number_arg(argvars, 0) == FAIL
2933 || check_for_dict_arg(argvars, 1) == FAIL))
2934 return;
2935
2936 id = (int)tv_get_number(argvars);
2937 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002938 if (wp == NULL)
2939 return; // invalid {id}
2940
2941 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2942 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002943 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002944 return;
2945 }
2946 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002947 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002948
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002949 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002950
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002951 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002952 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002953 popup_adjust_position(wp);
2954}
2955
2956/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002957 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002958 */
2959 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002960f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002961{
2962 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002963 int id;
2964 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002965 int top_extra;
2966 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002967
2968 if (rettv_dict_alloc(rettv) == OK)
2969 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002970 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2971 return;
2972
2973 id = (int)tv_get_number(argvars);
2974 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002975 if (wp == NULL)
2976 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002977 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002978 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2979
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002980 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002981 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002982 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002983
Bram Moolenaarbc133542019-05-29 20:26:46 +02002984 dict_add_number(dict, "line", wp->w_winrow + 1);
2985 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002986 dict_add_number(dict, "width", wp->w_width + left_extra
2987 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2988 dict_add_number(dict, "height", wp->w_height + top_extra
2989 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002990
2991 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2992 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2993 dict_add_number(dict, "core_width", wp->w_width);
2994 dict_add_number(dict, "core_height", wp->w_height);
2995
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002996 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002997 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002998 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002999 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003000 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003001
3002 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003003 }
3004}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003005
3006/*
3007 * popup_list()
3008 */
3009 void
3010f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3011{
3012 win_T *wp;
3013 tabpage_T *tp;
3014
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003015 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003016 return;
3017 FOR_ALL_POPUPWINS(wp)
3018 list_append_number(rettv->vval.v_list, wp->w_id);
3019 FOR_ALL_TABPAGES(tp)
3020 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3021 list_append_number(rettv->vval.v_list, wp->w_id);
3022}
3023
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003024/*
3025 * popup_locate({row}, {col})
3026 */
3027 void
3028f_popup_locate(typval_T *argvars, typval_T *rettv)
3029{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003030 int row;
3031 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003032 win_T *wp;
3033
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003034 if (in_vim9script()
3035 && (check_for_number_arg(argvars, 0) == FAIL
3036 || check_for_number_arg(argvars, 1) == FAIL))
3037 return;
3038
3039 row = tv_get_number(&argvars[0]) - 1;
3040 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003041 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003042 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003043 rettv->vval.v_number = wp->w_id;
3044}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003045
3046/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003047 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3048 */
3049 static void
3050get_padding_border(dict_T *dict, int *array, char *name)
3051{
3052 list_T *list;
3053 int i;
3054
3055 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3056 return;
3057
3058 list = list_alloc();
3059 if (list != NULL)
3060 {
3061 dict_add_list(dict, name, list);
3062 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3063 for (i = 0; i < 4; ++i)
3064 list_append_number(list, array[i]);
3065 }
3066}
3067
3068/*
3069 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3070 */
3071 static void
3072get_borderhighlight(dict_T *dict, win_T *wp)
3073{
3074 list_T *list;
3075 int i;
3076
3077 for (i = 0; i < 4; ++i)
3078 if (wp->w_border_highlight[i] != NULL)
3079 break;
3080 if (i == 4)
3081 return;
3082
3083 list = list_alloc();
3084 if (list != NULL)
3085 {
3086 dict_add_list(dict, "borderhighlight", list);
3087 for (i = 0; i < 4; ++i)
3088 list_append_string(list, wp->w_border_highlight[i], -1);
3089 }
3090}
3091
3092/*
3093 * For popup_getoptions(): add a "borderchars" entry to "dict".
3094 */
3095 static void
3096get_borderchars(dict_T *dict, win_T *wp)
3097{
3098 list_T *list;
3099 int i;
3100 char_u buf[NUMBUFLEN];
3101 int len;
3102
3103 for (i = 0; i < 8; ++i)
3104 if (wp->w_border_char[i] != 0)
3105 break;
3106 if (i == 8)
3107 return;
3108
3109 list = list_alloc();
3110 if (list != NULL)
3111 {
3112 dict_add_list(dict, "borderchars", list);
3113 for (i = 0; i < 8; ++i)
3114 {
3115 len = mb_char2bytes(wp->w_border_char[i], buf);
3116 list_append_string(list, buf, len);
3117 }
3118 }
3119}
3120
3121/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003122 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003123 */
3124 static void
3125get_moved_list(dict_T *dict, win_T *wp)
3126{
3127 list_T *list;
3128
3129 list = list_alloc();
3130 if (list != NULL)
3131 {
3132 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003133 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003134 list_append_number(list, wp->w_popup_mincol);
3135 list_append_number(list, wp->w_popup_maxcol);
3136 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003137 list = list_alloc();
3138 if (list != NULL)
3139 {
3140 dict_add_list(dict, "mousemoved", list);
3141 list_append_number(list, wp->w_popup_mouse_row);
3142 list_append_number(list, wp->w_popup_mouse_mincol);
3143 list_append_number(list, wp->w_popup_mouse_maxcol);
3144 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003145}
3146
3147/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003148 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003149 */
3150 void
3151f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3152{
3153 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003154 int id;
3155 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003156 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003157 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003158
3159 if (rettv_dict_alloc(rettv) == OK)
3160 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003161 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3162 return;
3163
3164 id = (int)tv_get_number(argvars);
3165 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003166 if (wp == NULL)
3167 return;
3168
3169 dict = rettv->vval.v_dict;
3170 dict_add_number(dict, "line", wp->w_wantline);
3171 dict_add_number(dict, "col", wp->w_wantcol);
3172 dict_add_number(dict, "minwidth", wp->w_minwidth);
3173 dict_add_number(dict, "minheight", wp->w_minheight);
3174 dict_add_number(dict, "maxheight", wp->w_maxheight);
3175 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003176 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003177 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003178 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003179 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003180 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003181 {
3182 proptype_T *pt = text_prop_type_by_id(
3183 wp->w_popup_prop_win->w_buffer,
3184 wp->w_popup_prop_type);
3185
3186 if (pt != NULL)
3187 dict_add_string(dict, "textprop", pt->pt_name);
3188 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3189 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3190 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003191 dict_add_string(dict, "title", wp->w_popup_title);
3192 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003193 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003194 dict_add_number(dict, "dragall",
3195 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003196 dict_add_number(dict, "mapping",
3197 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003198 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003199 dict_add_number(dict, "posinvert",
3200 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003201 dict_add_number(dict, "cursorline",
3202 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003203 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003204 if (wp->w_scrollbar_highlight != NULL)
3205 dict_add_string(dict, "scrollbarhighlight",
3206 wp->w_scrollbar_highlight);
3207 if (wp->w_thumb_highlight != NULL)
3208 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003209
Bram Moolenaara3fce622019-06-20 02:31:49 +02003210 // find the tabpage that holds this popup
3211 i = 1;
3212 FOR_ALL_TABPAGES(tp)
3213 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003214 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003215
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003216 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3217 if (twp->w_id == id)
3218 break;
3219 if (twp != NULL)
3220 break;
3221 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003222 }
3223 if (tp == NULL)
3224 i = -1; // must be global
3225 else if (tp == curtab)
3226 i = 0;
3227 dict_add_number(dict, "tabpage", i);
3228
Bram Moolenaarae943152019-06-16 22:54:14 +02003229 get_padding_border(dict, wp->w_popup_padding, "padding");
3230 get_padding_border(dict, wp->w_popup_border, "border");
3231 get_borderhighlight(dict, wp);
3232 get_borderchars(dict, wp);
3233 get_moved_list(dict, wp);
3234
3235 if (wp->w_filter_cb.cb_name != NULL)
3236 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3237 if (wp->w_close_cb.cb_name != NULL)
3238 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003239
K.Takataeeec2542021-06-02 13:28:16 +02003240 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003241 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3242 {
3243 dict_add_string(dict, "pos",
3244 (char_u *)poppos_entries[i].pp_name);
3245 break;
3246 }
3247
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003248 dict_add_string(dict, "close", (char_u *)(
3249 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3250 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3251
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003252# if defined(FEAT_TIMERS)
3253 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3254 ? (long)wp->w_popup_timer->tr_interval : 0L);
3255# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003256 }
3257}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003258
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003259# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003260/*
3261 * Return TRUE if the current window is running a terminal in a popup window.
3262 * Return FALSE when the job has ended.
3263 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003264 int
3265error_if_term_popup_window()
3266{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003267 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3268 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003269 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003270 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003271 return TRUE;
3272 }
3273 return FALSE;
3274}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003275# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003276
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003277/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003278 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003279 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003280 * Each calling function should use a different flag, see the list at
3281 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003282 */
3283 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003284popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003285{
3286 win_T *wp;
3287
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003288 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003289 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003290 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003291 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003292}
3293
3294/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003295 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003296 * Must have called popup_reset_handled() first.
3297 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3298 * popup with the highest zindex.
3299 */
3300 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003301find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003302{
3303 win_T *wp;
3304 win_T *found_wp;
3305 int found_zindex;
3306
3307 found_zindex = lowest ? INT_MAX : 0;
3308 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003309 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003310 if ((wp->w_popup_handled & handled_flag) == 0
3311 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003312 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003313 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003314 {
3315 found_zindex = wp->w_zindex;
3316 found_wp = wp;
3317 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003318 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003319 if ((wp->w_popup_handled & handled_flag) == 0
3320 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003321 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003322 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003323 {
3324 found_zindex = wp->w_zindex;
3325 found_wp = wp;
3326 }
3327
3328 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003329 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003330 return found_wp;
3331}
3332
3333/*
3334 * Invoke the filter callback for window "wp" with typed character "c".
3335 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003336 * Returns the return value of the filter or -1 for CTRL-C in the current
3337 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003338 * Careful: The filter may make "wp" invalid!
3339 */
3340 static int
3341invoke_popup_filter(win_T *wp, int c)
3342{
3343 int res;
3344 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003345 typval_T argv[3];
3346 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003347 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003348 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003349
Bram Moolenaara42d9452019-06-15 21:46:30 +02003350 // Emergency exit: CTRL-C closes the popup.
3351 if (c == Ctrl_C)
3352 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003353 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003354 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003355
3356 // Reset got_int to avoid the callback isn't called.
3357 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003358 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003359 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003360
3361 // If the popup is the current window it probably fails to close. Then
3362 // do not consume the key.
3363 if (was_curwin && wp == curwin)
3364 return -1;
3365 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003366 }
3367
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003368 argv[0].v_type = VAR_NUMBER;
3369 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3370
3371 // Convert the number to a string, so that the function can use:
3372 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003373 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003374 argv[1].v_type = VAR_STRING;
3375 argv[1].vval.v_string = vim_strsave(buf);
3376
3377 argv[2].v_type = VAR_UNKNOWN;
3378
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003379 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003380 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3381 {
3382 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003383 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003384 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003385 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003386 }
3387 else
3388 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003389 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3390 popup_highlight_curline(wp);
3391
Bram Moolenaar371806e2020-10-22 13:44:54 +02003392 // If an error message was given always return FALSE, so that keys are
3393 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003394 // If we get three errors in a row then close the popup. Decrement the
3395 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3396 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003397 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003398 {
3399 wp->w_filter_errors += 10;
3400 if (wp->w_filter_errors >= 30)
3401 popup_close_with_retval(wp, -1);
3402 res = FALSE;
3403 }
3404 else
3405 {
3406 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3407 --wp->w_filter_errors;
3408 res = tv_get_bool(&rettv);
3409 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003410 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003411
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003412 vim_free(argv[1].vval.v_string);
3413 clear_tv(&rettv);
3414 return res;
3415}
3416
3417/*
3418 * Called when "c" was typed: invoke popup filter callbacks.
3419 * Returns TRUE when the character was consumed,
3420 */
3421 int
3422popup_do_filter(int c)
3423{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003424 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003425 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003426 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003427 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003428 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003429 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003430
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003431#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003432 // Popup window with terminal always gets focus.
3433 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3434 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003435#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003436
Bram Moolenaar934470e2019-09-01 23:27:05 +02003437 if (recursive)
3438 return FALSE;
3439 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003440
Bram Moolenaarf6396232019-08-24 19:36:00 +02003441 if (c == K_LEFTMOUSE)
3442 {
3443 int row = mouse_row;
3444 int col = mouse_col;
3445
3446 wp = mouse_find_win(&row, &col, FIND_POPUP);
3447 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003448 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003449 }
3450
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003451 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003452 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003453 while (res == FALSE
3454 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003455 if (wp->w_filter_cb.cb_name != NULL
3456 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003457 res = invoke_popup_filter(wp, c);
3458
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003459 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003460 {
3461 int save_got_int = got_int;
3462
3463 // Reset got_int to avoid a function used in the statusline aborts.
3464 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003465 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003466 got_int |= save_got_int;
3467 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003468 recursive = FALSE;
3469 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003470
3471 // When interrupted return FALSE to avoid looping.
3472 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003473}
3474
Bram Moolenaar3397f742019-06-02 18:40:06 +02003475/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003476 * Return TRUE if there is a popup visible with a filter callback and the
3477 * "mapping" property off.
3478 */
3479 int
3480popup_no_mapping(void)
3481{
3482 int round;
3483 win_T *wp;
3484
3485 for (round = 1; round <= 2; ++round)
3486 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3487 wp != NULL; wp = wp->w_next)
3488 if (wp->w_filter_cb.cb_name != NULL
3489 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3490 return TRUE;
3491 return FALSE;
3492}
3493
3494/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003495 * Called when the cursor moved: check if any popup needs to be closed if the
3496 * cursor moved far enough.
3497 */
3498 void
3499popup_check_cursor_pos()
3500{
3501 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003502
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003503 popup_reset_handled(POPUP_HANDLED_3);
3504 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003505 if (wp->w_popup_curwin != NULL
3506 && (curwin != wp->w_popup_curwin
3507 || curwin->w_cursor.lnum != wp->w_popup_lnum
3508 || curwin->w_cursor.col < wp->w_popup_mincol
3509 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003510 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003511}
3512
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003513/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003514 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003515 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003516 static void
3517popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003518{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003519 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003520 char_u *cells;
3521 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003522
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003523 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3524 {
3525 vim_free(wp->w_popup_mask_cells);
3526 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003527 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003528 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003529 if (wp->w_popup_mask_cells != NULL
3530 && wp->w_popup_mask_height == height
3531 && wp->w_popup_mask_width == width)
3532 return; // cache is still valid
3533
3534 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003535 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003536 if (wp->w_popup_mask_cells == NULL)
3537 return;
3538 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003539
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003540 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003541 {
3542 int cols, cole;
3543 int lines, linee;
3544
3545 li = lio->li_tv.vval.v_list->lv_first;
3546 cols = tv_get_number(&li->li_tv);
3547 if (cols < 0)
3548 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003549 if (cols <= 0)
3550 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003551 li = li->li_next;
3552 cole = tv_get_number(&li->li_tv);
3553 if (cole < 0)
3554 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003555 if (cole > width)
3556 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003557 li = li->li_next;
3558 lines = tv_get_number(&li->li_tv);
3559 if (lines < 0)
3560 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003561 if (lines <= 0)
3562 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003563 li = li->li_next;
3564 linee = tv_get_number(&li->li_tv);
3565 if (linee < 0)
3566 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003567 if (linee > height)
3568 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003569
Bram Moolenaar4012d262020-12-29 11:57:46 +01003570 for (row = lines - 1; row < linee; ++row)
3571 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003572 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003573 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003574}
3575
3576/*
3577 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3578 * "col" and "line" are screen coordinates.
3579 */
3580 static int
3581popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3582{
3583 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3584 int line = screenline - wp->w_winrow;
3585
3586 return col >= 0 && col < width
3587 && line >= 0 && line < height
3588 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003589}
3590
3591/*
3592 * Set flags in popup_transparent[] for window "wp" to "val".
3593 */
3594 static void
3595update_popup_transparent(win_T *wp, int val)
3596{
3597 if (wp->w_popup_mask != NULL)
3598 {
3599 int width = popup_width(wp);
3600 int height = popup_height(wp);
3601 listitem_T *lio, *li;
3602 int cols, cole;
3603 int lines, linee;
3604 int col, line;
3605
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003606 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003607 {
3608 li = lio->li_tv.vval.v_list->lv_first;
3609 cols = tv_get_number(&li->li_tv);
3610 if (cols < 0)
3611 cols = width + cols + 1;
3612 li = li->li_next;
3613 cole = tv_get_number(&li->li_tv);
3614 if (cole < 0)
3615 cole = width + cole + 1;
3616 li = li->li_next;
3617 lines = tv_get_number(&li->li_tv);
3618 if (lines < 0)
3619 lines = height + lines + 1;
3620 li = li->li_next;
3621 linee = tv_get_number(&li->li_tv);
3622 if (linee < 0)
3623 linee = height + linee + 1;
3624
3625 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003626 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003627 if (cols < 0)
3628 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003629 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003630 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003631 if (lines < 0)
3632 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003633 for (line = lines; line < linee
3634 && line + wp->w_winrow < screen_Rows; ++line)
3635 for (col = cols; col < cole
3636 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003637 popup_transparent[(line + wp->w_winrow) * screen_Columns
3638 + col + wp->w_wincol] = val;
3639 }
3640 }
3641}
3642
3643/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003644 * Only called when popup window "wp" is hidden: If the window is positioned
3645 * next to a text property, and it is now visible, then unhide the popup.
3646 * We don't check if visible popups become hidden, that is done in
3647 * popup_adjust_position().
3648 * Return TRUE if the popup became unhidden.
3649 */
3650 static int
3651check_popup_unhidden(win_T *wp)
3652{
3653 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3654 {
3655 textprop_T prop;
3656 linenr_T lnum;
3657
Bram Moolenaar27724252022-05-08 15:00:04 +01003658 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3659 && find_visible_prop(wp->w_popup_prop_win,
3660 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003661 &prop, &lnum) == OK)
3662 {
3663 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003664 wp->w_popup_prop_topline = 0; // force repositioning
3665 return TRUE;
3666 }
3667 }
3668 return FALSE;
3669}
3670
3671/*
3672 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3673 * That is when the buffer in the popup was changed, or the popup is following
3674 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003675 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003676 */
3677 static int
3678popup_need_position_adjust(win_T *wp)
3679{
3680 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3681 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003682 if (win_valid(wp->w_popup_prop_win)
3683 && (wp->w_popup_prop_changedtick
3684 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3685 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3686 return TRUE;
3687
3688 // May need to adjust the width if the cursor moved.
3689 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003690}
3691
3692/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003693 * Update "popup_mask" if needed.
3694 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003695 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003696 * Also marks window lines for redrawing.
3697 */
3698 void
3699may_update_popup_mask(int type)
3700{
3701 win_T *wp;
3702 short *mask;
3703 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003704 int redraw_all_popups = FALSE;
3705 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003706
3707 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003708 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3709 // basic (such as the screen size) must have changed.
3710 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003711 {
3712 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003713 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003714 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003715
3716 // Check if any popup window buffer has changed and if any popup connected
3717 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003718 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003719 if (wp->w_popup_flags & POPF_HIDDEN)
3720 popup_mask_refresh |= check_popup_unhidden(wp);
3721 else if (popup_need_position_adjust(wp))
3722 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003723 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003724 if (wp->w_popup_flags & POPF_HIDDEN)
3725 popup_mask_refresh |= check_popup_unhidden(wp);
3726 else if (popup_need_position_adjust(wp))
3727 popup_mask_refresh = TRUE;
3728
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003729 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003730 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003731
3732 // Need to update the mask, something has changed.
3733 popup_mask_refresh = FALSE;
3734 popup_mask_tab = curtab;
3735 popup_visible = FALSE;
3736
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003737 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003738 // If redrawing only what is needed, update "popup_mask_next" and then
3739 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003740 redrawing_all_win = TRUE;
3741 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003742 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003743 redrawing_all_win = FALSE;
3744 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003745 mask = popup_mask;
3746 else
3747 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003748 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003749
3750 // Find the window with the lowest zindex that hasn't been handled yet,
3751 // so that the window with a higher zindex overwrites the value in
3752 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003753 popup_reset_handled(POPUP_HANDLED_4);
3754 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003755 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003756 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003757 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003758
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003759 popup_visible = TRUE;
3760
Bram Moolenaar12034e22019-08-25 22:25:02 +02003761 // Recompute the position if the text changed. It may make the popup
3762 // hidden if it's attach to a text property that is no longer visible.
3763 if (redraw_all_popups || popup_need_position_adjust(wp))
3764 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003765 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003766 if (wp->w_popup_flags & POPF_HIDDEN)
3767 continue;
3768 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003769
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003770 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003771 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003772 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003773 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003774 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003775 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003776 col < wp->w_wincol + width - wp->w_popup_leftoff
3777 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003778 if (wp->w_zindex < POPUPMENU_ZINDEX
3779 && pum_visible()
3780 && pum_under_menu(line, col, FALSE))
3781 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3782 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003783 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003784 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003785 }
3786
3787 // Only check which lines are to be updated if not already
3788 // updating all lines.
3789 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003790 {
3791 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3792 win_T *prev_wp = NULL;
3793
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003794 for (line = 0; line < screen_Rows; ++line)
3795 {
3796 int col_done = 0;
3797
3798 for (col = 0; col < screen_Columns; ++col)
3799 {
3800 int off = line * screen_Columns + col;
3801
3802 if (popup_mask[off] != popup_mask_next[off])
3803 {
3804 popup_mask[off] = popup_mask_next[off];
3805
3806 if (line >= cmdline_row)
3807 {
3808 // the command line needs to be cleared if text below
3809 // the popup is now visible.
3810 if (!msg_scrolled && popup_mask_next[off] == 0)
3811 clear_cmdline = TRUE;
3812 }
3813 else if (col >= col_done)
3814 {
3815 linenr_T lnum;
3816 int line_cp = line;
3817 int col_cp = col;
3818
3819 // The screen position "line" / "col" needs to be
3820 // redrawn. Figure out what window that is and update
3821 // w_redraw_top and w_redr_bot. Only needs to be done
3822 // once for each window line.
3823 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3824 if (wp != NULL)
3825 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003826#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003827 // A terminal window needs to be redrawn.
3828 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003829 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003830 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003831#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003832 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003833 if (wp != prev_wp)
3834 {
3835 vim_memset(plines_cache, 0,
3836 sizeof(int) * Rows);
3837 prev_wp = wp;
3838 }
3839
3840 if (line_cp >= wp->w_height)
3841 // In (or below) status line
3842 wp->w_redr_status = TRUE;
3843 else
3844 {
3845 // compute the position in the buffer line
3846 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003847 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003848 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003849 redrawWinline(wp, lnum);
3850 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003851 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003852
3853 // This line is going to be redrawn, no need to
3854 // check until the right side of the window.
3855 col_done = wp->w_wincol + wp->w_width - 1;
3856 }
3857 }
3858 }
3859 }
3860 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003861
3862 vim_free(plines_cache);
3863 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003864
3865 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003866}
3867
3868/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003869 * If the current window is a popup and something relevant changed, recompute
3870 * the position and size.
3871 */
3872 void
3873may_update_popup_position(void)
3874{
3875 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3876 popup_adjust_position(curwin);
3877}
3878
3879/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003880 * Return a string of "len" spaces in IObuff.
3881 */
3882 static char_u *
3883get_spaces(int len)
3884{
3885 vim_memset(IObuff, ' ', (size_t)len);
3886 IObuff[len] = NUL;
3887 return IObuff;
3888}
3889
3890/*
3891 * Update popup windows. They are drawn on top of normal windows.
3892 * "win_update" is called for each popup window, lowest zindex first.
3893 */
3894 void
3895update_popups(void (*win_update)(win_T *wp))
3896{
3897 win_T *wp;
3898 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003899 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003900 int total_width;
3901 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003902 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003903 int popup_attr;
3904 int border_attr[4];
3905 int border_char[8];
3906 char_u buf[MB_MAXBYTES];
3907 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003908 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003909 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003910 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003911 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003912 int sb_thumb_top = 0;
3913 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003914 int attr_scroll = 0;
3915 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003916
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003917 // hide the cursor until redrawing is done.
3918 cursor_off();
3919
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003920 // Find the window with the lowest zindex that hasn't been updated yet,
3921 // so that the window with a higher zindex is drawn later, thus goes on
3922 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003923 popup_reset_handled(POPUP_HANDLED_5);
3924 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003925 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003926 int title_len = 0;
3927 int title_wincol;
3928
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003929 // This drawing uses the zindex of the popup window, so that it's on
3930 // top of the text but doesn't draw when another popup with higher
3931 // zindex is on top of the character.
3932 screen_zindex = wp->w_zindex;
3933
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003934 // Set flags in popup_transparent[] for masked cells.
3935 update_popup_transparent(wp, 1);
3936
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003937 // adjust w_winrow and w_wincol for border and padding, since
3938 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003939 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003940 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3941 - wp->w_popup_leftoff;
3942 if (wp->w_wincol + left_extra < 0)
3943 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003944 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003945 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003946
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003947 // Draw the popup text, unless it's off screen.
3948 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003949 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003950 // May need to update the "cursorline" highlighting, which may also
3951 // change "topline"
3952 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3953 popup_highlight_curline(wp);
3954
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003955 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003956
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003957 // move the cursor into the visible lines, otherwise executing
3958 // commands with win_execute() may cause the text to jump.
3959 if (wp->w_cursor.lnum < wp->w_topline)
3960 wp->w_cursor.lnum = wp->w_topline;
3961 else if (wp->w_cursor.lnum >= wp->w_botline)
3962 wp->w_cursor.lnum = wp->w_botline - 1;
3963 }
3964
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003965 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003966 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003967
3968 // Add offset for border and padding if not done already.
3969 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3970 {
3971 wp->w_wcol += left_extra;
3972 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3973 }
3974 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003975 {
3976 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003977 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003978 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003979
Bram Moolenaareabddc42022-04-02 15:32:16 +01003980 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003981 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003982 popup_attr = get_wcr_attr(wp);
3983
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003984 if (wp->w_winrow + total_height > cmdline_row)
3985 wp->w_popup_flags |= POPF_ON_CMDLINE;
3986 else
3987 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3988
3989
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003990 // We can only use these line drawing characters when 'encoding' is
3991 // "utf-8" and 'ambiwidth' is "single".
3992 if (enc_utf8 && *p_ambw == 's')
3993 {
3994 border_char[0] = border_char[2] = 0x2550;
3995 border_char[1] = border_char[3] = 0x2551;
3996 border_char[4] = 0x2554;
3997 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003998 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3999 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004000 border_char[7] = 0x255a;
4001 }
4002 else
4003 {
4004 border_char[0] = border_char[2] = '-';
4005 border_char[1] = border_char[3] = '|';
4006 for (i = 4; i < 8; ++i)
4007 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004008 if (wp->w_popup_flags & POPF_RESIZE)
4009 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004010 }
4011 for (i = 0; i < 8; ++i)
4012 if (wp->w_border_char[i] != 0)
4013 border_char[i] = wp->w_border_char[i];
4014
4015 for (i = 0; i < 4; ++i)
4016 {
4017 border_attr[i] = popup_attr;
4018 if (wp->w_border_highlight[i] != NULL)
4019 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4020 }
4021
Bram Moolenaard91467f2020-11-21 12:42:09 +01004022 // Title goes on top of border or padding.
4023 title_wincol = wp->w_wincol + 1;
4024 if (wp->w_popup_title != NULL)
4025 {
rbtnnc6119412021-08-07 13:08:45 +02004026 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004027
Ralf Schandlbc869872021-05-28 14:12:14 +02004028 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004029 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004030 {
4031 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4032 char_u *title_text = alloc(title_byte_len + 1);
4033
4034 if (title_text != NULL)
4035 {
4036 trunc_string(wp->w_popup_title, title_text,
4037 total_width - 2, title_byte_len + 1);
4038 screen_puts(title_text, wp->w_winrow, title_wincol,
4039 wp->w_popup_border[0] > 0
4040 ? border_attr[0] : popup_attr);
4041 vim_free(title_text);
4042 }
4043
Bram Moolenaard91467f2020-11-21 12:42:09 +01004044 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004045 }
4046 else
4047 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4048 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004049 }
4050
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004051 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004052 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004053 if (wp->w_popup_border[0] > 0)
4054 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004055 // top border; do not draw over the title
4056 if (title_len > 0)
4057 {
4058 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4059 wincol < 0 ? 0 : wincol, title_wincol,
4060 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004061 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004062 border_char[0], border_attr[0]);
4063 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4064 title_wincol + title_len, wincol + total_width,
4065 border_char[0], border_char[0], border_attr[0]);
4066 }
4067 else
4068 {
4069 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4070 wincol < 0 ? 0 : wincol, wincol + total_width,
4071 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4072 ? border_char[4] : border_char[0],
4073 border_char[0], border_attr[0]);
4074 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004075 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004076 {
4077 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4078 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004079 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004080 }
4081 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004082 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4083 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004084
Bram Moolenaard529ba52019-07-02 23:13:53 +02004085 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4086 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004087 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004088 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004089 - wp->w_has_scrollbar;
4090 if (padcol < 0)
4091 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004092 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004093 padcol = 0;
4094 }
4095 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004096 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004097 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004098 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004099 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004100 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004101 // top padding and no border; do not draw over the title
4102 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004103 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004104 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004105 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004106 row += 1;
4107 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004108 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004109 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004110 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004111 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004112
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004113 // Compute scrollbar thumb position and size.
4114 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004115 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004116 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4117 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004118 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004119
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004120 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4121 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004122 if (wp->w_topline > 1 && sb_thumb_height == height)
4123 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004124 if (sb_thumb_height == 0)
4125 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004126 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004127 // it just fits, avoid divide by zero
4128 sb_thumb_top = 0;
4129 else
4130 sb_thumb_top = (wp->w_topline - 1
4131 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004132 * (wp->w_height - sb_thumb_height)
4133 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004134 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4135 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004136 last = total_height - top_off - wp->w_popup_border[2];
4137 if (sb_thumb_top >= last)
4138 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004139 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004140
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004141 if (wp->w_scrollbar_highlight != NULL)
4142 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4143 else
4144 attr_scroll = highlight_attr[HLF_PSB];
4145 if (wp->w_thumb_highlight != NULL)
4146 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4147 else
4148 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004149 }
4150
4151 for (i = wp->w_popup_border[0];
4152 i < total_height - wp->w_popup_border[2]; ++i)
4153 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004154 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004155 // left and right padding only needed next to the body
4156 int do_padding =
4157 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4158 && i < total_height - wp->w_popup_border[2]
4159 - wp->w_popup_padding[2];
4160
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004161 row = wp->w_winrow + i;
4162
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004163 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004164 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004165 {
4166 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004167 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004168 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004169 if (do_padding && wp->w_popup_padding[3] > 0)
4170 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004171 int col = wincol + wp->w_popup_border[3];
4172
Bram Moolenaard529ba52019-07-02 23:13:53 +02004173 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004174 pad_left = wp->w_popup_padding[3];
4175 if (col < 0)
4176 {
4177 pad_left += col;
4178 col = 0;
4179 }
4180 if (pad_left > 0)
4181 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4182 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004183 // scrollbar
4184 if (wp->w_has_scrollbar)
4185 {
4186 int line = i - top_off;
4187 int scroll_col = wp->w_wincol + total_width - 1
4188 - wp->w_popup_border[1];
4189
4190 if (line >= 0 && line < wp->w_height)
4191 screen_putchar(' ', row, scroll_col,
4192 line >= sb_thumb_top
4193 && line < sb_thumb_top + sb_thumb_height
4194 ? attr_thumb : attr_scroll);
4195 else
4196 screen_putchar(' ', row, scroll_col, popup_attr);
4197 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004198 // right border
4199 if (wp->w_popup_border[1] > 0)
4200 {
4201 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004202 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004203 }
4204 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004205 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004206 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004207 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004208 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4209 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004210 }
4211
4212 if (wp->w_popup_padding[2] > 0)
4213 {
4214 // bottom padding
4215 row = wp->w_winrow + wp->w_popup_border[0]
4216 + wp->w_popup_padding[0] + wp->w_height;
4217 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004218 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004219 }
4220
4221 if (wp->w_popup_border[2] > 0)
4222 {
4223 // bottom border
4224 row = wp->w_winrow + total_height - 1;
4225 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004226 wincol < 0 ? 0 : wincol,
4227 wincol + total_width,
4228 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004229 ? border_char[7] : border_char[2],
4230 border_char[2], border_attr[2]);
4231 if (wp->w_popup_border[1] > 0)
4232 {
4233 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004234 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004235 }
4236 }
4237
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004238 if (wp->w_popup_close == POPCLOSE_BUTTON)
4239 {
4240 // close button goes on top of anything at the top-right corner
4241 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004242 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004243 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4244 }
4245
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004246 update_popup_transparent(wp, 0);
4247
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004248 // Back to the normal zindex.
4249 screen_zindex = 0;
4250 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004251
4252#if defined(FEAT_SEARCH_EXTRA)
4253 // In case win_update() called start_search_hl().
4254 end_search_hl();
4255#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004256}
4257
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004258/*
4259 * Mark references in callbacks of one popup window.
4260 */
4261 static int
4262set_ref_in_one_popup(win_T *wp, int copyID)
4263{
4264 int abort = FALSE;
4265 typval_T tv;
4266
4267 if (wp->w_close_cb.cb_partial != NULL)
4268 {
4269 tv.v_type = VAR_PARTIAL;
4270 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4271 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4272 }
4273 if (wp->w_filter_cb.cb_partial != NULL)
4274 {
4275 tv.v_type = VAR_PARTIAL;
4276 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4277 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4278 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004279 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004280 return abort;
4281}
4282
4283/*
4284 * Set reference in callbacks of popup windows.
4285 */
4286 int
4287set_ref_in_popups(int copyID)
4288{
4289 int abort = FALSE;
4290 win_T *wp;
4291 tabpage_T *tp;
4292
4293 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4294 abort = abort || set_ref_in_one_popup(wp, copyID);
4295
4296 FOR_ALL_TABPAGES(tp)
4297 {
4298 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4299 abort = abort || set_ref_in_one_popup(wp, copyID);
4300 if (abort)
4301 break;
4302 }
4303 return abort;
4304}
Bram Moolenaar79648732019-07-18 21:43:07 +02004305
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004306 int
4307popup_is_popup(win_T *wp)
4308{
4309 return wp->w_popup_flags != 0;
4310}
4311
4312#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004313/*
4314 * Find an existing popup used as the preview window, in the current tab page.
4315 * Return NULL if not found.
4316 */
4317 win_T *
4318popup_find_preview_window(void)
4319{
4320 win_T *wp;
4321
4322 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004323 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004324 if (wp->w_p_pvw)
4325 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004326 return NULL;
4327}
4328
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004329/*
4330 * Find an existing popup used as the info window, in the current tab page.
4331 * Return NULL if not found.
4332 */
4333 win_T *
4334popup_find_info_window(void)
4335{
4336 win_T *wp;
4337
4338 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004339 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004340 if (wp->w_popup_flags & POPF_INFO)
4341 return wp;
4342 return NULL;
4343}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004344#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004345
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004346 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004347f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4348{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004349#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004350 win_T *wp = popup_find_info_window();
4351
4352 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004353#else
4354 rettv->vval.v_number = 0;
4355#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004356}
4357
4358 void
4359f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004360{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004361#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004362 win_T *wp = popup_find_preview_window();
4363
4364 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004365#else
4366 rettv->vval.v_number = 0;
4367#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004368}
4369
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004370#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004371/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004372 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004373 * NOTE: this makes the popup the current window, so that the file can be
4374 * edited. However, it must not remain to be the current window, the caller
4375 * must make sure of that.
4376 */
4377 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004378popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004379{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004380 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004381
4382 if (wp == NULL)
4383 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004384 if (info)
4385 wp->w_popup_flags |= POPF_INFO;
4386 else
4387 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004388
4389 // Set the width to a reasonable value, so that w_topline can be computed.
4390 if (wp->w_minwidth > 0)
4391 wp->w_width = wp->w_minwidth;
4392 else if (wp->w_maxwidth > 0)
4393 wp->w_width = wp->w_maxwidth;
4394 else
4395 wp->w_width = curwin->w_width;
4396
4397 // Will switch to another buffer soon, dummy one can be wiped.
4398 wp->w_buffer->b_locked = FALSE;
4399
4400 win_enter(wp, FALSE);
4401 return OK;
4402}
4403
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004404/*
4405 * Close any preview popup.
4406 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004407 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004408popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004409{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004410 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004411
4412 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004413 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004414}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004415
4416/*
4417 * Hide the info popup.
4418 */
4419 void
4420popup_hide_info(void)
4421{
4422 win_T *wp = popup_find_info_window();
4423
4424 if (wp != NULL)
4425 popup_hide(wp);
4426}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004427
4428/*
4429 * Close any info popup.
4430 */
4431 void
4432popup_close_info(void)
4433{
4434 win_T *wp = popup_find_info_window();
4435
4436 if (wp != NULL)
4437 popup_close_with_retval(wp, -1);
4438}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004439#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004440
Bram Moolenaar9198de32022-08-27 21:30:03 +01004441#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4442
4443// Window used for messages when 'winheight' is zero.
4444static win_T *message_win = NULL;
4445
4446/*
4447 * Get the message window.
4448 * Returns NULL if something failed.
4449 */
4450 win_T *
4451popup_get_message_win(void)
4452{
4453 if (message_win == NULL)
4454 {
4455 int i;
4456
4457 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4458
4459 if (message_win == NULL)
4460 return NULL;
4461
4462 // use the full screen width
4463 message_win->w_width = Columns;
4464
4465 // position at bottom of screen
4466 message_win->w_popup_pos = POPPOS_BOTTOM;
4467 message_win->w_wantcol = 1;
4468 message_win->w_minwidth = 9999;
4469
4470 // no padding, border at the top
4471 for (i = 0; i < 4; ++i)
4472 message_win->w_popup_padding[i] = 0;
4473 for (i = 1; i < 4; ++i)
4474 message_win->w_popup_border[i] = 0;
4475
4476 if (message_win->w_popup_timer != NULL)
4477 message_win->w_popup_timer->tr_keep = TRUE;
4478 }
4479 return message_win;
4480}
4481
4482/*
4483 * If the message window is not visible: show it
4484 * If the message window is visible: reset the timeout
4485 */
4486 void
4487popup_show_message_win(void)
4488{
4489 if (message_win != NULL)
4490 {
4491 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4492 {
4493 // the highlight may have changed.
4494 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4495 popup_show(message_win);
4496 }
4497 else if (message_win->w_popup_timer != NULL)
4498 timer_start(message_win->w_popup_timer);
4499 }
4500}
4501
4502 int
4503popup_message_win_visible(void)
4504{
4505 return message_win != NULL
4506 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4507}
4508
4509/*
4510 * If the message window is visible: hide it.
4511 */
4512 void
4513popup_hide_message_win(void)
4514{
4515 if (message_win != NULL)
4516 popup_hide(message_win);
4517}
4518
4519#endif
4520
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004521/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004522 * Close any popup for a text property associated with "win".
4523 * Return TRUE if a popup was closed.
4524 */
4525 int
4526popup_win_closed(win_T *win)
4527{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004528 int round;
4529 win_T *wp;
4530 win_T *next;
4531 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004532
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004533 for (round = 1; round <= 2; ++round)
4534 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4535 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004536 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004537 next = wp->w_next;
4538 if (wp->w_popup_prop_win == win)
4539 {
4540 popup_close_with_retval(wp, -1);
4541 ret = TRUE;
4542 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004543 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004544 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004545}
4546
4547/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004548 * Set the title of the popup window to the file name.
4549 */
4550 void
4551popup_set_title(win_T *wp)
4552{
4553 if (wp->w_buffer->b_fname != NULL)
4554 {
4555 char_u dirname[MAXPATHL];
4556 size_t len;
4557
4558 mch_dirname(dirname, MAXPATHL);
4559 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4560
4561 vim_free(wp->w_popup_title);
4562 len = STRLEN(wp->w_buffer->b_fname) + 3;
4563 wp->w_popup_title = alloc((int)len);
4564 if (wp->w_popup_title != NULL)
4565 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4566 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004567 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004568 }
4569}
4570
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004571# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004572/*
4573 * If there is a preview window, update the title.
4574 * Used after changing directory.
4575 */
4576 void
4577popup_update_preview_title(void)
4578{
4579 win_T *wp = popup_find_preview_window();
4580
4581 if (wp != NULL)
4582 popup_set_title(wp);
4583}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004584# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004585
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004586#endif // FEAT_PROP_POPUP