blob: e6792c8528f22b21ca50e4228e845c11d615fded [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{
378 int height = popup_height(wp);
379 int old_topline = wp->w_topline;
380
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200381 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200382 {
383 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)
387 ++wp->w_topline;
388 }
389 else if (wp->w_topline > 1)
390 // click on upper half, scroll up.
391 --wp->w_topline;
392 if (wp->w_topline != old_topline)
393 {
394 popup_set_firstline(wp);
395 redraw_win_later(wp, NOT_VALID);
396 }
397 }
398}
399
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200400#if defined(FEAT_TIMERS)
401 static void
402popup_add_timeout(win_T *wp, int time)
403{
404 char_u cbbuf[50];
405 char_u *ptr = cbbuf;
406 typval_T tv;
407
408 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200409 "(_) => popup_close(%d)", wp->w_id);
410 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200411 {
412 wp->w_popup_timer = create_timer(time, 0);
413 wp->w_popup_timer->tr_callback = get_callback(&tv);
414 clear_tv(&tv);
415 }
416}
417#endif
418
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100419 static poppos_T
420get_pos_entry(dict_T *d, int give_error)
421{
422 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
423 int nr;
424
425 if (str == NULL)
426 return POPPOS_NONE;
427
K.Takataeeec2542021-06-02 13:28:16 +0200428 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100429 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
430 return poppos_entries[nr].pp_val;
431
432 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000433 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100434 return POPPOS_NONE;
435}
436
Bram Moolenaar17627312019-06-02 19:53:44 +0200437/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200438 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200439 */
440 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200441apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200442{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200443 int nr;
444 char_u *str;
445 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200446
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200447 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200448 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200449 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200450 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200451 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200452 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200453 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200454 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200455
456 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200457 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200458 wp->w_wantline = nr;
459 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200460 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200461 wp->w_wantcol = nr;
462
Bram Moolenaar55881332020-08-18 13:04:15 +0200463
464 nr = dict_get_bool(d, (char_u *)"fixed", -1);
465 if (nr != -1)
466 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200467
Bram Moolenaar12034e22019-08-25 22:25:02 +0200468 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100469 poppos_T ppt = get_pos_entry(d, TRUE);
470
471 if (ppt != POPPOS_NONE)
472 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200473 }
474
475 str = dict_get_string(d, (char_u *)"textprop", FALSE);
476 if (str != NULL)
477 {
478 wp->w_popup_prop_type = 0;
479 if (*str != NUL)
480 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100481 wp->w_popup_prop_win = curwin;
482 di = dict_find(d, (char_u *)"textpropwin", -1);
483 if (di != NULL)
484 {
485 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100486 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100487 wp->w_popup_prop_win = curwin;
488 }
489
490 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200491 if (nr <= 0)
492 nr = find_prop_type_id(str, NULL);
493 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000494 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200495 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200496 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200497 }
498 }
499
500 di = dict_find(d, (char_u *)"textpropid", -1);
501 if (di != NULL)
502 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200503}
504
Bram Moolenaarb0992022020-01-30 14:55:42 +0100505/*
506 * Handle "moved" and "mousemoved" arguments.
507 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200508 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200509handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
510{
511 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
512 {
513 char_u *s = di->di_tv.vval.v_string;
514 int flags = 0;
515
516 if (STRCMP(s, "word") == 0)
517 flags = FIND_IDENT | FIND_STRING;
518 else if (STRCMP(s, "WORD") == 0)
519 flags = FIND_STRING;
520 else if (STRCMP(s, "expr") == 0)
521 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
522 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000523 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200524 if (flags != 0)
525 {
526 if (mousemoved)
527 set_mousemoved_columns(wp, flags);
528 else
529 set_moved_columns(wp, flags);
530 }
531 }
532 else if (di->di_tv.v_type == VAR_LIST
533 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200534 && (di->di_tv.vval.v_list->lv_len == 2
535 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200536 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200537 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100538 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200539 int mincol;
540 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200541
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200542 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100543 li = l->lv_first;
544 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200545 {
546 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
547
548 // Three numbers, might be from popup_getoptions().
549 if (mousemoved)
550 wp->w_popup_mouse_row = nr;
551 else
552 wp->w_popup_lnum = nr;
553 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100554 if (nr == 0)
555 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200556 }
557
558 mincol = tv_get_number(&li->li_tv);
559 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200560 if (mousemoved)
561 {
562 wp->w_popup_mouse_mincol = mincol;
563 wp->w_popup_mouse_maxcol = maxcol;
564 }
565 else
566 {
567 wp->w_popup_mincol = mincol;
568 wp->w_popup_maxcol = maxcol;
569 }
570 }
571 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000572 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200573}
574
575 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200576check_highlight(dict_T *dict, char *name, char_u **pval)
577{
578 dictitem_T *di;
579 char_u *str;
580
581 di = dict_find(dict, (char_u *)name, -1);
582 if (di != NULL)
583 {
584 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000585 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200586 else
587 {
588 str = tv_get_string(&di->di_tv);
589 if (*str != NUL)
590 *pval = vim_strsave(str);
591 }
592 }
593}
594
Bram Moolenaarae943152019-06-16 22:54:14 +0200595/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200596 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200597 */
598 static void
599popup_show_curline(win_T *wp)
600{
601 if (wp->w_cursor.lnum < wp->w_topline)
602 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200603 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200604 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200605 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200606 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200607 if (wp->w_topline < 1)
608 wp->w_topline = 1;
609 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
610 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200611 while (wp->w_topline < wp->w_cursor.lnum
612 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
613 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
614 > wp->w_height)
615 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200616 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200617
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200618 // Don't let "firstline" cause a scroll.
619 if (wp->w_firstline > 0)
620 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200621}
622
623/*
624 * Get the sign group name for window "wp".
625 * Returns a pointer to a static buffer, overwritten on the next call.
626 */
627 static char_u *
628popup_get_sign_name(win_T *wp)
629{
630 static char buf[30];
631
632 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
633 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200634}
635
636/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200637 * Highlight the line with the cursor.
638 * Also scrolls the text to put the cursor line in view.
639 */
640 static void
641popup_highlight_curline(win_T *wp)
642{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200643 int sign_id = 0;
644 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200645
Bram Moolenaar72570732019-11-30 14:21:53 +0100646 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200647
648 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
649 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200650 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200651
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200652 if (!sign_exists_by_name(sign_name))
653 {
654 char *linehl = "PopupSelected";
655
656 if (syn_name2id((char_u *)linehl) == 0)
657 linehl = "PmenuSel";
James McCoya80aad72021-12-22 19:45:28 +0000658 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200659 }
660
Bram Moolenaar72570732019-11-30 14:21:53 +0100661 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200662 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
663 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200664 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200665 else
666 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200667 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200668}
669
670/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200671 * Shared between popup_create() and f_popup_setoptions().
672 */
673 static void
674apply_general_options(win_T *wp, dict_T *dict)
675{
676 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 int nr;
678 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200679
Bram Moolenaarae943152019-06-16 22:54:14 +0200680 // TODO: flip
681
682 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200683 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200684 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200685 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200686 if (wp->w_firstline < 0)
687 wp->w_firstline = -1;
688 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200689
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200690 nr = dict_get_bool(dict, (char_u *)"scrollbar", -1);
691 if (nr != -1)
692 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200693
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200694 str = dict_get_string(dict, (char_u *)"title", FALSE);
695 if (str != NULL)
696 {
697 vim_free(wp->w_popup_title);
698 wp->w_popup_title = vim_strsave(str);
699 }
700
Bram Moolenaar55881332020-08-18 13:04:15 +0200701 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
702 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200703 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200704
Bram Moolenaar55881332020-08-18 13:04:15 +0200705 nr = dict_get_bool(dict, (char_u *)"drag", -1);
706 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200707 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200708 if (nr)
709 wp->w_popup_flags |= POPF_DRAG;
710 else
711 wp->w_popup_flags &= ~POPF_DRAG;
712 }
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000713 nr = dict_get_bool(dict, (char_u *)"dragall", -1);
714 if (nr != -1)
715 {
716 if (nr)
717 wp->w_popup_flags |= POPF_DRAGALL;
718 else
719 wp->w_popup_flags &= ~POPF_DRAGALL;
720 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200721
Bram Moolenaar55881332020-08-18 13:04:15 +0200722 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
723 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100724 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100725 if (nr)
726 wp->w_popup_flags |= POPF_POSINVERT;
727 else
728 wp->w_popup_flags &= ~POPF_POSINVERT;
729 }
730
Bram Moolenaar55881332020-08-18 13:04:15 +0200731 nr = dict_get_bool(dict, (char_u *)"resize", -1);
732 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200733 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200734 if (nr)
735 wp->w_popup_flags |= POPF_RESIZE;
736 else
737 wp->w_popup_flags &= ~POPF_RESIZE;
738 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200739
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200740 di = dict_find(dict, (char_u *)"close", -1);
741 if (di != NULL)
742 {
743 int ok = TRUE;
744
745 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
746 {
747 char_u *s = di->di_tv.vval.v_string;
748
749 if (STRCMP(s, "none") == 0)
750 wp->w_popup_close = POPCLOSE_NONE;
751 else if (STRCMP(s, "button") == 0)
752 wp->w_popup_close = POPCLOSE_BUTTON;
753 else if (STRCMP(s, "click") == 0)
754 wp->w_popup_close = POPCLOSE_CLICK;
755 else
756 ok = FALSE;
757 }
758 else
759 ok = FALSE;
760 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000761 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200762 }
763
Bram Moolenaarae943152019-06-16 22:54:14 +0200764 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
765 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000766 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200767 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
768 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000769#ifdef FEAT_TERMINAL
770 term_update_wincolor(wp);
771#endif
772 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200773
Bram Moolenaarae943152019-06-16 22:54:14 +0200774 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
775 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200776
Bram Moolenaar790498b2019-06-01 22:15:29 +0200777 di = dict_find(dict, (char_u *)"borderhighlight", -1);
778 if (di != NULL)
779 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200780 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000781 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200782 else
783 {
784 list_T *list = di->di_tv.vval.v_list;
785 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200786 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200787
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200788 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200789 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
790 ++i, li = li->li_next)
791 {
792 str = tv_get_string(&li->li_tv);
793 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100794 {
795 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200796 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100797 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200798 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200799 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
800 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100801 {
802 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200803 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200804 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100805 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200806 }
807 }
808
Bram Moolenaar790498b2019-06-01 22:15:29 +0200809 di = dict_find(dict, (char_u *)"borderchars", -1);
810 if (di != NULL)
811 {
812 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000813 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200814 else
815 {
816 list_T *list = di->di_tv.vval.v_list;
817 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200818 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200819
820 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100821 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200822 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200823 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
824 ++i, li = li->li_next)
825 {
826 str = tv_get_string(&li->li_tv);
827 if (*str != NUL)
828 wp->w_border_char[i] = mb_ptr2char(str);
829 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200830 if (list->lv_len == 1)
831 for (i = 1; i < 8; ++i)
832 wp->w_border_char[i] = wp->w_border_char[0];
833 if (list->lv_len == 2)
834 {
835 for (i = 4; i < 8; ++i)
836 wp->w_border_char[i] = wp->w_border_char[1];
837 for (i = 1; i < 4; ++i)
838 wp->w_border_char[i] = wp->w_border_char[0];
839 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200840 }
841 }
842 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200843
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200844 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
845 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
846
Bram Moolenaarae943152019-06-16 22:54:14 +0200847 di = dict_find(dict, (char_u *)"zindex", -1);
848 if (di != NULL)
849 {
850 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
851 if (wp->w_zindex < 1)
852 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
853 if (wp->w_zindex > 32000)
854 wp->w_zindex = 32000;
855 }
856
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200857 di = dict_find(dict, (char_u *)"mask", -1);
858 if (di != NULL)
859 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200860 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200861
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200862 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200863 {
864 listitem_T *li;
865
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200866 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200867 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200868 {
869 if (li->li_tv.v_type != VAR_LIST
870 || li->li_tv.vval.v_list == NULL
871 || li->li_tv.vval.v_list->lv_len != 4)
872 {
873 ok = FALSE;
874 break;
875 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100876 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200877 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200878 }
879 }
880 if (ok)
881 {
882 wp->w_popup_mask = di->di_tv.vval.v_list;
883 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200884 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200885 }
886 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000887 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200888 }
889
Bram Moolenaarae943152019-06-16 22:54:14 +0200890#if defined(FEAT_TIMERS)
891 // Add timer to close the popup after some time.
892 nr = dict_get_number(dict, (char_u *)"time");
893 if (nr > 0)
894 popup_add_timeout(wp, nr);
895#endif
896
Bram Moolenaar3397f742019-06-02 18:40:06 +0200897 di = dict_find(dict, (char_u *)"moved", -1);
898 if (di != NULL)
899 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200900 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200901 handle_moved_argument(wp, di, FALSE);
902 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200903
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200904 di = dict_find(dict, (char_u *)"mousemoved", -1);
905 if (di != NULL)
906 {
907 set_mousemoved_values(wp);
908 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200909 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200910
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100911 nr = dict_get_bool(dict, (char_u *)"cursorline", -1);
912 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200913 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100914 if (nr != 0)
915 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200916 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100917 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200918 }
919
Bram Moolenaarae943152019-06-16 22:54:14 +0200920 di = dict_find(dict, (char_u *)"filter", -1);
921 if (di != NULL)
922 {
923 callback_T callback = get_callback(&di->di_tv);
924
925 if (callback.cb_name != NULL)
926 {
927 free_callback(&wp->w_filter_cb);
928 set_callback(&wp->w_filter_cb, &callback);
929 }
930 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200931 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
932 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200933 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200934 if (nr)
935 wp->w_popup_flags |= POPF_MAPPING;
936 else
937 wp->w_popup_flags &= ~POPF_MAPPING;
938 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200939
Bram Moolenaar581ba392019-09-03 22:08:33 +0200940 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
941 if (str != NULL)
942 {
943 if (STRCMP(str, "a") == 0)
944 wp->w_filter_mode = MODE_ALL;
945 else
946 wp->w_filter_mode = mode_str2flags(str);
947 }
948
Bram Moolenaarae943152019-06-16 22:54:14 +0200949 di = dict_find(dict, (char_u *)"callback", -1);
950 if (di != NULL)
951 {
952 callback_T callback = get_callback(&di->di_tv);
953
954 if (callback.cb_name != NULL)
955 {
956 free_callback(&wp->w_close_cb);
957 set_callback(&wp->w_close_cb, &callback);
958 }
959 }
960}
961
962/*
963 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200964 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200965 */
966 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200967apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200968{
969 int nr;
970
971 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200972
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200973 if (create)
974 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200975 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
976
Bram Moolenaarae943152019-06-16 22:54:14 +0200977 apply_general_options(wp, dict);
978
Bram Moolenaar55881332020-08-18 13:04:15 +0200979 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200980 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200981 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200982
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200983 // when "firstline" and "cursorline" are both set and the cursor would be
984 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200985 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
986 {
987 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
988 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200989 else if (wp->w_cursor.lnum < wp->w_firstline
990 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200991 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200992 wp->w_topline = wp->w_firstline;
993 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200994 }
995
Bram Moolenaar33796b32019-06-08 16:01:13 +0200996 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200997 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200998}
999
1000/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001001 * Add lines to the popup from a list of strings.
1002 */
1003 static void
1004add_popup_strings(buf_T *buf, list_T *l)
1005{
1006 listitem_T *li;
1007 linenr_T lnum = 0;
1008 char_u *p;
1009
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001010 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001011 if (li->li_tv.v_type == VAR_STRING)
1012 {
1013 p = li->li_tv.vval.v_string;
1014 ml_append_buf(buf, lnum++,
1015 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1016 }
1017}
1018
1019/*
1020 * Add lines to the popup from a list of dictionaries.
1021 */
1022 static void
1023add_popup_dicts(buf_T *buf, list_T *l)
1024{
1025 listitem_T *li;
1026 listitem_T *pli;
1027 linenr_T lnum = 0;
1028 char_u *p;
1029 dict_T *dict;
1030
1031 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001032 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001033 {
1034 if (li->li_tv.v_type != VAR_DICT)
1035 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001036 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001037 return;
1038 }
1039 dict = li->li_tv.vval.v_dict;
1040 p = dict == NULL ? NULL
1041 : dict_get_string(dict, (char_u *)"text", FALSE);
1042 ml_append_buf(buf, lnum++,
1043 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1044 }
1045
1046 // add the text properties
1047 lnum = 1;
1048 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1049 {
1050 dictitem_T *di;
1051 list_T *plist;
1052
1053 dict = li->li_tv.vval.v_dict;
1054 di = dict_find(dict, (char_u *)"props", -1);
1055 if (di != NULL)
1056 {
1057 if (di->di_tv.v_type != VAR_LIST)
1058 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001059 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001060 return;
1061 }
1062 plist = di->di_tv.vval.v_list;
1063 if (plist != NULL)
1064 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001065 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001066 {
1067 if (pli->li_tv.v_type != VAR_DICT)
1068 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001069 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001070 return;
1071 }
1072 dict = pli->li_tv.vval.v_dict;
1073 if (dict != NULL)
1074 {
1075 int col = dict_get_number(dict, (char_u *)"col");
1076
1077 prop_add_common( lnum, col, dict, buf, NULL);
1078 }
1079 }
1080 }
1081 }
1082 }
1083}
1084
1085/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001086 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1087 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001088 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001089popup_top_extra(win_T *wp)
1090{
1091 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1092
1093 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1094 return 1;
1095 return extra;
1096}
1097
1098/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001099 * Get the padding plus border at the left.
1100 */
1101 int
1102popup_left_extra(win_T *wp)
1103{
1104 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1105}
1106
1107/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001108 * Return the height of popup window "wp", including border and padding.
1109 */
1110 int
1111popup_height(win_T *wp)
1112{
1113 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001114 + popup_top_extra(wp)
1115 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001116}
1117
1118/*
1119 * Return the width of popup window "wp", including border, padding and
1120 * scrollbar.
1121 */
1122 int
1123popup_width(win_T *wp)
1124{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001125 // w_leftcol is how many columns of the core are left of the screen
1126 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001127 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001128 + popup_extra_width(wp)
1129 + wp->w_popup_rightoff;
1130}
1131
1132/*
1133 * Return the extra width of popup window "wp": border, padding and scrollbar.
1134 */
1135 int
1136popup_extra_width(win_T *wp)
1137{
1138 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1139 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1140 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001141}
1142
1143/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001144 * Adjust the position and size of the popup to fit on the screen.
1145 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001146 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001147popup_adjust_position(win_T *wp)
1148{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001149 linenr_T lnum;
1150 int wrapped = 0;
1151 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001152 int maxwidth_no_scrollbar;
1153 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001154 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001155 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001156 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001157 int center_vert = FALSE;
1158 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001159 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001160 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001161 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1162 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1163 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1164 int extra_height = top_extra + bot_extra;
1165 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001166 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001167 int org_winrow = wp->w_winrow;
1168 int org_wincol = wp->w_wincol;
1169 int org_width = wp->w_width;
1170 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001171 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001172 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001173 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001174 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001175 int wantline = wp->w_wantline; // adjusted for textprop
1176 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001177 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001178 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001179
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001180 wp->w_winrow = 0;
1181 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001182 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001183 wp->w_popup_leftoff = 0;
1184 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001185
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001186 // May need to update the "cursorline" highlighting, which may also change
1187 // "topline"
1188 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1189 popup_highlight_curline(wp);
1190
Bram Moolenaar12034e22019-08-25 22:25:02 +02001191 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1192 {
1193 win_T *prop_win = wp->w_popup_prop_win;
1194 textprop_T prop;
1195 linenr_T prop_lnum;
1196 pos_T pos;
1197 int screen_row;
1198 int screen_scol;
1199 int screen_ccol;
1200 int screen_ecol;
1201
1202 // Popup window is positioned relative to a text property.
1203 if (find_visible_prop(prop_win,
1204 wp->w_popup_prop_type, wp->w_popup_prop_id,
1205 &prop, &prop_lnum) == FAIL)
1206 {
1207 // Text property is no longer visible, hide the popup.
1208 // Unhiding the popup is done in check_popup_unhidden().
1209 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1210 {
1211 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001212 if (win_valid(wp->w_popup_prop_win))
1213 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1214 }
1215 return;
1216 }
1217
1218 // Compute the desired position from the position of the text
1219 // property. Use "wantline" and "wantcol" as offsets.
1220 pos.lnum = prop_lnum;
1221 pos.col = prop.tp_col;
1222 if (wp->w_popup_pos == POPPOS_TOPLEFT
1223 || wp->w_popup_pos == POPPOS_BOTLEFT)
1224 pos.col += prop.tp_len - 1;
1225 textpos2screenpos(prop_win, &pos, &screen_row,
1226 &screen_scol, &screen_ccol, &screen_ecol);
1227
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001228 if (screen_scol == 0)
1229 {
1230 // position is off screen, make the width zero to hide it.
1231 wp->w_width = 0;
1232 return;
1233 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001234 if (wp->w_popup_pos == POPPOS_TOPLEFT
1235 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1236 // below the text
1237 wantline = screen_row + wantline + 1;
1238 else
1239 // above the text
1240 wantline = screen_row + wantline - 1;
1241 center_vert = FALSE;
1242 if (wp->w_popup_pos == POPPOS_TOPLEFT
1243 || wp->w_popup_pos == POPPOS_BOTLEFT)
1244 // right of the text
1245 wantcol = screen_ecol + wantcol;
1246 else
1247 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001248 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001249 use_wantcol = TRUE;
1250 }
1251 else
1252 {
1253 // If no line was specified default to vertical centering.
1254 if (wantline == 0)
1255 center_vert = TRUE;
1256 else if (wantline < 0)
1257 // If "wantline" is negative it actually means zero.
1258 wantline = 0;
1259 if (wantcol < 0)
1260 // If "wantcol" is negative it actually means zero.
1261 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001262 }
1263
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001264 if (wp->w_popup_pos == POPPOS_CENTER)
1265 {
1266 // center after computing the size
1267 center_vert = TRUE;
1268 center_hor = TRUE;
1269 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001270 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001271 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001272 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1273 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001274 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001275 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001276 if (wp->w_winrow >= Rows)
1277 wp->w_winrow = Rows - 1;
1278 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001279
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001280 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001281 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001282 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1283 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001284 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001285 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001286 // Need to see at least one character after the decoration.
1287 if (wp->w_wincol > Columns - left_extra - 1)
1288 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001289 }
1290 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001291
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001292 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001293 // When left aligned use the space available, but shift to the left when we
1294 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001295 maxspace = Columns - wp->w_wincol - left_extra;
1296 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001297 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001298 {
1299 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001300 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001301 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001302
1303 if (wp->w_p_nu || wp->w_p_rnu)
1304 margin_width = number_width(wp) + 1;
1305#ifdef FEAT_FOLDING
1306 margin_width += wp->w_p_fdc;
1307#endif
1308#ifdef FEAT_SIGNS
1309 if (signcolumn_on(wp))
1310 margin_width += 2;
1311#endif
1312 if (margin_width >= maxwidth)
1313 margin_width = maxwidth - 1;
1314
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001315 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001316 minheight = wp->w_minheight;
1317#ifdef FEAT_TERMINAL
1318 // A terminal popup initially does not have content, use a default minimal
1319 // width of 20 characters and height of 5 lines.
1320 if (wp->w_buffer->b_term != NULL)
1321 {
1322 if (minwidth == 0)
1323 minwidth = 20;
1324 if (minheight == 0)
1325 minheight = 5;
1326 }
1327#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001328
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001329 if (wp->w_maxheight > 0)
1330 maxheight = wp->w_maxheight;
1331
Bram Moolenaar8d241042019-06-12 23:40:01 +02001332 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001333 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001334 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001335 if (wp->w_topline < 1)
1336 wp->w_topline = 1;
1337 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001338 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1339
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001340 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001341 // Use a minimum width of one, so that something shows when there is no
1342 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001343 // When "firstline" is -1 then start with the last buffer line and go
1344 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001345 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001346 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001347 if (wp->w_firstline < 0)
1348 lnum = wp->w_buffer->b_ml.ml_line_count;
1349 else
1350 lnum = wp->w_topline;
1351 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001352 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001353 int len;
1354 int w_width = wp->w_width;
1355
1356 // Count Tabs for what they are worth and compute the length based on
1357 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001358 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001359 if (wp->w_width < maxwidth)
1360 wp->w_width = maxwidth;
1361 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001362 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001363 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001364
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001365 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001366 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001367 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001368 {
1369 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001370 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001371 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001372 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001373 }
1374 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001375 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001376 && allow_adjust_left
1377 && (wp->w_popup_pos == POPPOS_TOPLEFT
1378 || wp->w_popup_pos == POPPOS_BOTLEFT))
1379 {
1380 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001381 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001382
Bram Moolenaar51c31312019-06-15 22:27:23 +02001383 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001384 {
1385 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001386
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001387 len -= truncate_shift;
1388 shift_by -= truncate_shift;
1389 }
1390
1391 wp->w_wincol -= shift_by;
1392 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001393 wp->w_width = maxwidth;
1394 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001395 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001396 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001397 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001398 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1399 wp->w_width = wp->w_maxwidth;
1400 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001401
1402 if (wp->w_firstline < 0)
1403 --lnum;
1404 else
1405 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001406
1407 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001408 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001409 && (wp->w_firstline >= 0
1410 ? lnum - wp->w_topline
1411 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001412 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001413 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001414 }
1415
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001416 if (wp->w_firstline < 0)
1417 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1418
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001419 wp->w_has_scrollbar = wp->w_want_scrollbar
1420 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001421#ifdef FEAT_TERMINAL
1422 if (wp->w_buffer->b_term != NULL)
1423 // Terminal window never has a scrollbar, adjusts to window height.
1424 wp->w_has_scrollbar = FALSE;
1425#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001426 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001427 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001428 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001429 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001430 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001431 // make space for the scrollbar if needed, when lines wrap and when
1432 // applying minwidth
1433 if (maxwidth + right_extra >= maxspace
1434 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1435 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001436 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001437
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001438 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1439 {
1440 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1441
1442 if (minwidth < title_len)
1443 minwidth = title_len;
1444 }
1445
1446 if (minwidth > 0 && wp->w_width < minwidth)
1447 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001448 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001449 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001450 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001451 // some columns cut off on the right
1452 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001453
1454 // If the window doesn't fit because 'minwidth' is set then the
1455 // scrollbar is at the far right of the screen, use the size without
1456 // the scrollbar.
1457 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1458 {
1459 int off = wp->w_width - maxwidth;
1460
1461 if (off > right_extra)
1462 extra_width -= right_extra;
1463 else
1464 extra_width -= off;
1465 wp->w_width = maxwidth_no_scrollbar;
1466 }
1467 else
1468 {
1469 wp->w_width = maxwidth;
1470
1471 // when adding a scrollbar below need to adjust the width
1472 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1473 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001474 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001475 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001476 {
1477 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1478 if (wp->w_wincol < 0)
1479 wp->w_wincol = 0;
1480 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001481 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1482 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1483 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001484 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001485
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001486 // Right aligned: move to the right if needed.
1487 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001488 if (leftoff >= 0)
1489 wp->w_wincol = leftoff;
1490 else if (wp->w_popup_fixed)
1491 {
1492 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001493 // columns when displaying the window, minus left border and
1494 // padding.
1495 if (-leftoff > left_extra)
1496 wp->w_leftcol = -leftoff - left_extra;
1497 wp->w_width -= wp->w_leftcol;
1498 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001499 if (wp->w_width < 0)
1500 wp->w_width = 0;
1501 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001502 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001503
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001504 if (wp->w_p_wrap || (!wp->w_popup_fixed
1505 && (wp->w_popup_pos == POPPOS_TOPLEFT
1506 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001507 {
1508 int want_col = 0;
1509
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001510 // try to show the right border and any scrollbar
1511 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001512 if (want_col > 0 && wp->w_wincol > 0
1513 && wp->w_wincol + want_col >= Columns)
1514 {
1515 wp->w_wincol = Columns - want_col;
1516 if (wp->w_wincol < 0)
1517 wp->w_wincol = 0;
1518 }
1519 }
1520
Bram Moolenaar8d241042019-06-12 23:40:01 +02001521 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1522 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001523 if (minheight > 0 && wp->w_height < minheight)
1524 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001525 if (maxheight > 0 && wp->w_height > maxheight)
1526 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001527 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001528 if (wp->w_height > Rows - wp->w_winrow)
1529 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001530
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001531 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001532 {
1533 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1534 if (wp->w_winrow < 0)
1535 wp->w_winrow = 0;
1536 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001537 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001538 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001539 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001540 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001541 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001542 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001543 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1544 {
1545 // Bottom aligned but does not fit, and less space on the other
1546 // side or "posinvert" is off: reduce height.
1547 wp->w_winrow = 0;
1548 wp->w_height = wantline - extra_height;
1549 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001550 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001551 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001552 // Not enough space and more space on the other side: make top
1553 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001554 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001555 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001556 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001557 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001558 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1559 || wp->w_popup_pos == POPPOS_TOPLEFT)
1560 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001561 if (wp != popup_dragwin
1562 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001563 && wantline * 2 > Rows
1564 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001565 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001566 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001567 // make bottom aligned and recompute the height
1568 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001569 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001570 if (wp->w_winrow < 0)
1571 {
1572 wp->w_height += wp->w_winrow;
1573 wp->w_winrow = 0;
1574 }
1575 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001576 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001577 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001578 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001579 adjust_height_for_top_aligned = TRUE;
1580 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001581 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001582
1583 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1584 && wp->w_winrow + wp->w_height + extra_height > Rows)
1585 {
1586 // Bottom of the popup goes below the last line, reduce the height and
1587 // add a scrollbar.
1588 wp->w_height = Rows - wp->w_winrow - extra_height;
1589#ifdef FEAT_TERMINAL
1590 if (wp->w_buffer->b_term == NULL)
1591#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001592 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001593 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001594 if (width_with_scrollbar > 0)
1595 wp->w_width = width_with_scrollbar;
1596 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001597 }
1598
1599 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001600 if (wp->w_winrow >= Rows)
1601 wp->w_winrow = Rows - 1;
1602 else if (wp->w_winrow < 0)
1603 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001604
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001605 if (wp->w_height != org_height)
1606 win_comp_scroll(wp);
1607
Bram Moolenaar17146962019-05-30 00:12:11 +02001608 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001609 if (win_valid(wp->w_popup_prop_win))
1610 {
1611 wp->w_popup_prop_changedtick =
1612 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1613 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1614 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001615
1616 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001617 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001618 if (org_winrow != wp->w_winrow
1619 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001620 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001621 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001622 || org_width != wp->w_width
1623 || org_height != wp->w_height)
1624 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001625 redraw_win_later(wp, NOT_VALID);
1626 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1627 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001628 popup_mask_refresh = TRUE;
1629 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001630}
1631
Bram Moolenaar17627312019-06-02 19:53:44 +02001632typedef enum
1633{
1634 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001635 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001636 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001637 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001638 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001639 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001640 TYPE_PREVIEW, // preview window
1641 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001642} create_type_T;
1643
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001644/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001645 * Make "buf" empty and set the contents to "text".
1646 * Used by popup_create() and popup_settext().
1647 */
1648 static void
1649popup_set_buffer_text(buf_T *buf, typval_T text)
1650{
1651 int lnum;
1652
1653 // Clear the buffer, then replace the lines.
1654 curbuf = buf;
1655 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001656 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001657 curbuf = curwin->w_buffer;
1658
1659 // Add text to the buffer.
1660 if (text.v_type == VAR_STRING)
1661 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001662 char_u *s = text.vval.v_string;
1663
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001664 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001665 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001666 }
1667 else
1668 {
1669 list_T *l = text.vval.v_list;
1670
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001671 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001672 {
1673 if (l->lv_first->li_tv.v_type == VAR_STRING)
1674 // list of strings
1675 add_popup_strings(buf, l);
1676 else
1677 // list of dictionaries
1678 add_popup_dicts(buf, l);
1679 }
1680 }
1681
1682 // delete the line that was in the empty buffer
1683 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001684 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001685 curbuf = curwin->w_buffer;
1686}
1687
1688/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001689 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1690 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001691 * Return FAIL if the parsing fails.
1692 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001693 static int
1694parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001695{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001696 char_u *p =
1697#ifdef FEAT_QUICKFIX
1698 !is_preview ? p_cpp :
1699#endif
1700 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001701
Bram Moolenaar258cef52019-08-21 17:29:29 +02001702 if (wp != NULL)
1703 wp->w_popup_flags &= ~POPF_INFO_MENU;
1704
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001705 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001706 {
1707 char_u *e, *dig;
1708 char_u *s = p;
1709 int x;
1710
1711 e = vim_strchr(p, ':');
1712 if (e == NULL || e[1] == NUL)
1713 return FAIL;
1714
1715 p = vim_strchr(e, ',');
1716 if (p == NULL)
1717 p = e + STRLEN(e);
1718 dig = e + 1;
1719 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001720
1721 if (STRNCMP(s, "height:", 7) == 0)
1722 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001723 if (dig != p)
1724 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001725 if (wp != NULL)
1726 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001727 if (is_preview)
1728 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001729 wp->w_maxheight = x;
1730 }
1731 }
1732 else if (STRNCMP(s, "width:", 6) == 0)
1733 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001734 if (dig != p)
1735 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001736 if (wp != NULL)
1737 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001738 if (is_preview)
1739 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001740 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001741 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001742 }
1743 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001744 else if (STRNCMP(s, "highlight:", 10) == 0)
1745 {
1746 if (wp != NULL)
1747 {
1748 int c = *p;
1749
1750 *p = NUL;
1751 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1752 s + 10, OPT_FREE|OPT_LOCAL, 0);
1753 *p = c;
1754 }
1755 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001756 else if (STRNCMP(s, "border:", 7) == 0)
1757 {
1758 char_u *arg = s + 7;
1759 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1760 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1761 int i;
1762
1763 if (!on && !off)
1764 return FAIL;
1765 if (wp != NULL)
1766 {
1767 for (i = 0; i < 4; ++i)
1768 wp->w_popup_border[i] = on ? 1 : 0;
1769 if (off)
1770 // only show the X for close when there is a border
1771 wp->w_popup_close = POPCLOSE_NONE;
1772 }
1773 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001774 else if (STRNCMP(s, "align:", 6) == 0)
1775 {
1776 char_u *arg = s + 6;
1777 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1778 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1779
1780 if (!menu && !item)
1781 return FAIL;
1782 if (wp != NULL && menu)
1783 wp->w_popup_flags |= POPF_INFO_MENU;
1784 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001785 else
1786 return FAIL;
1787 }
1788 return OK;
1789}
1790
1791/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001792 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1793 * is not NULL.
1794 * Return FAIL if the parsing fails.
1795 */
1796 int
1797parse_previewpopup(win_T *wp)
1798{
1799 return parse_popup_option(wp, TRUE);
1800}
1801
1802/*
1803 * Parse the 'completepopup' option and apply the values to window "wp" if it
1804 * is not NULL.
1805 * Return FAIL if the parsing fails.
1806 */
1807 int
1808parse_completepopup(win_T *wp)
1809{
1810 return parse_popup_option(wp, FALSE);
1811}
1812
1813/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001814 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001815 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001816 */
1817 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001818popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001819{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001820 poppos_T ppt = POPPOS_NONE;
1821
1822 if (d != NULL)
1823 ppt = get_pos_entry(d, FALSE);
1824
Bram Moolenaar79648732019-07-18 21:43:07 +02001825 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001826 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001827 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001828 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1829 }
1830 else
1831 {
1832 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1833 if (wp->w_wantline == 0) // cursor in first line
1834 {
1835 wp->w_wantline = 2;
1836 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1837 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1838 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001839 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001840
Bram Moolenaar79648732019-07-18 21:43:07 +02001841 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001842 if (wp->w_wantcol > Columns - width)
1843 {
1844 wp->w_wantcol = Columns - width;
1845 if (wp->w_wantcol < 1)
1846 wp->w_wantcol = 1;
1847 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001848
Bram Moolenaar79648732019-07-18 21:43:07 +02001849 popup_adjust_position(wp);
1850}
1851
1852/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001853 * Set w_wantline and w_wantcol for the a given screen position.
1854 * Caller must take care of running into the window border.
1855 */
1856 void
1857popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1858{
1859 wp->w_wantline = row;
1860 wp->w_wantcol = col;
1861 popup_adjust_position(wp);
1862}
1863
1864/*
1865 * Add a border and lef&right padding.
1866 */
1867 static void
1868add_border_left_right_padding(win_T *wp)
1869{
1870 int i;
1871
1872 for (i = 0; i < 4; ++i)
1873 {
1874 wp->w_popup_border[i] = 1;
1875 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1876 }
1877}
1878
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001879#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001880/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001881 * Return TRUE if there is any popup window with a terminal buffer.
1882 */
1883 static int
1884popup_terminal_exists(void)
1885{
1886 win_T *wp;
1887 tabpage_T *tp;
1888
1889 FOR_ALL_POPUPWINS(wp)
1890 if (wp->w_buffer->b_term != NULL)
1891 return TRUE;
1892 FOR_ALL_TABPAGES(tp)
1893 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1894 if (wp->w_buffer->b_term != NULL)
1895 return TRUE;
1896 return FALSE;
1897}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001898#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001899
1900/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001901 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001902 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001903 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001904 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001905 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001906 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001907popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001908{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001909 win_T *wp;
1910 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001911 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001912 int new_buffer;
1913 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001914 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001915 int nr;
1916 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001917
Bram Moolenaar79648732019-07-18 21:43:07 +02001918 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001919 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001920 if (in_vim9script()
1921 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1922 || check_for_dict_arg(argvars, 1) == FAIL))
1923 return NULL;
1924
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001925 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001926 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001927 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001928 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001929 if (buf == NULL)
1930 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001931 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001932 return NULL;
1933 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001934#ifdef FEAT_TERMINAL
1935 if (buf->b_term != NULL && popup_terminal_exists())
1936 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001937 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001938 return NULL;
1939 }
1940#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001941 }
1942 else if (!(argvars[0].v_type == VAR_STRING
1943 && argvars[0].vval.v_string != NULL)
1944 && !(argvars[0].v_type == VAR_LIST
1945 && argvars[0].vval.v_list != NULL))
1946 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001947 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001948 return NULL;
1949 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001950 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001951 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001952 emsg(_(e_dictionary_required));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001953 return NULL;
1954 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001955 d = argvars[1].vval.v_dict;
1956 }
1957
1958 if (d != NULL)
1959 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001960 if (dict_has_key(d, "tabpage"))
Bram Moolenaar79648732019-07-18 21:43:07 +02001961 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1962 else if (type == TYPE_NOTIFICATION)
1963 tabnr = -1; // notifications are global by default
1964 else
1965 tabnr = 0;
1966 if (tabnr > 0)
1967 {
1968 tp = find_tabpage(tabnr);
1969 if (tp == NULL)
1970 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00001971 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02001972 return NULL;
1973 }
1974 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001975 }
1976
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001977 // Create the window and buffer.
1978 wp = win_alloc_popup_win();
1979 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001980 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001981 if (rettv != NULL)
1982 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001983 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001984 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001985
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001986 if (buf != NULL)
1987 {
1988 // use existing buffer
1989 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001990 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001991 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001992 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001993 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001994 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001995 }
1996 else
1997 {
1998 // create a new buffer associated with the popup
1999 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002000 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002001 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002002 {
2003 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002004 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002005 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002006 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002007
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002008 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002009
Bram Moolenaar46451042019-08-24 15:50:46 +02002010 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002011 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002012 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002013 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002014 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002015 buf->b_p_ul = -1; // no undo
2016 buf->b_p_swf = FALSE; // no swap file
2017 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002018 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002019
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002020 // Avoid that 'buftype' is reset when this buffer is entered.
2021 buf->b_p_initialized = TRUE;
2022 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002023 wp->w_p_wrap = TRUE; // 'wrap' is default on
2024 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002025
Bram Moolenaara3fce622019-06-20 02:31:49 +02002026 if (tp != NULL)
2027 {
2028 // popup on specified tab page
2029 wp->w_next = tp->tp_first_popupwin;
2030 tp->tp_first_popupwin = wp;
2031 }
2032 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002033 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002034 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002035 wp->w_next = curtab->tp_first_popupwin;
2036 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002037 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002038 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002039 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002040 win_T *prev = first_popupwin;
2041
2042 // Global popup: add at the end, so that it gets displayed on top of
2043 // older ones with the same zindex. Matters for notifications.
2044 if (first_popupwin == NULL)
2045 first_popupwin = wp;
2046 else
2047 {
2048 while (prev->w_next != NULL)
2049 prev = prev->w_next;
2050 prev->w_next = wp;
2051 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002052 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002053
Bram Moolenaar79648732019-07-18 21:43:07 +02002054 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002055 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002056
Bram Moolenaar79648732019-07-18 21:43:07 +02002057 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002058 {
2059 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002060 }
2061 if (type == TYPE_ATCURSOR)
2062 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002063 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002064 set_moved_values(wp);
2065 set_moved_columns(wp, FIND_STRING);
2066 }
2067
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002068 if (type == TYPE_BEVAL)
2069 {
2070 wp->w_popup_pos = POPPOS_BOTLEFT;
2071
2072 // by default use the mouse position
2073 wp->w_wantline = mouse_row;
2074 if (wp->w_wantline <= 0) // mouse on first line
2075 {
2076 wp->w_wantline = 2;
2077 wp->w_popup_pos = POPPOS_TOPLEFT;
2078 }
2079 wp->w_wantcol = mouse_col + 1;
2080 set_mousemoved_values(wp);
2081 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2082 }
2083
Bram Moolenaar33796b32019-06-08 16:01:13 +02002084 // set default values
2085 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002086 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002087
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002088 if (type == TYPE_NOTIFICATION)
2089 {
2090 win_T *twp, *nextwin;
2091 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002092
2093 // Try to not overlap with another global popup. Guess we need 3
2094 // more screen lines than buffer lines.
2095 wp->w_wantline = 1;
2096 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2097 {
2098 nextwin = twp->w_next;
2099 if (twp != wp
2100 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2101 && twp->w_winrow <= wp->w_wantline - 1 + height
2102 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2103 {
2104 // move to below this popup and restart the loop to check for
2105 // overlap with other popups
2106 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2107 nextwin = first_popupwin;
2108 }
2109 }
2110 if (wp->w_wantline + height > Rows)
2111 {
2112 // can't avoid overlap, put on top in the hope that message goes
2113 // away soon.
2114 wp->w_wantline = 1;
2115 }
2116
2117 wp->w_wantcol = 10;
2118 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002119 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002120 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002121 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002122 for (i = 0; i < 4; ++i)
2123 wp->w_popup_border[i] = 1;
2124 wp->w_popup_padding[1] = 1;
2125 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002126
2127 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002128 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002129 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2130 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002131 }
2132
Bram Moolenaara730e552019-06-16 19:05:31 +02002133 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002134 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002135 wp->w_popup_pos = POPPOS_CENTER;
2136 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002137 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002138 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002139 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002140 }
2141
Bram Moolenaara730e552019-06-16 19:05:31 +02002142 if (type == TYPE_MENU)
2143 {
2144 typval_T tv;
2145 callback_T callback;
2146
2147 tv.v_type = VAR_STRING;
2148 tv.vval.v_string = (char_u *)"popup_filter_menu";
2149 callback = get_callback(&tv);
2150 if (callback.cb_name != NULL)
2151 set_callback(&wp->w_filter_cb, &callback);
2152
2153 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002154 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002155 }
2156
Bram Moolenaar79648732019-07-18 21:43:07 +02002157 if (type == TYPE_PREVIEW)
2158 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002159 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002160 wp->w_popup_close = POPCLOSE_BUTTON;
2161 for (i = 0; i < 4; ++i)
2162 wp->w_popup_border[i] = 1;
2163 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002164 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002165 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002166# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002167 if (type == TYPE_INFO)
2168 {
2169 wp->w_popup_pos = POPPOS_TOPLEFT;
2170 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2171 wp->w_popup_close = POPCLOSE_BUTTON;
2172 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002173 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002174 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002175# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002176
Bram Moolenaarae943152019-06-16 22:54:14 +02002177 for (i = 0; i < 4; ++i)
2178 VIM_CLEAR(wp->w_border_highlight[i]);
2179 for (i = 0; i < 8; ++i)
2180 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002181 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002182 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002183 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002184
Bram Moolenaar79648732019-07-18 21:43:07 +02002185 if (d != NULL)
2186 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002187 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002188
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002189#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002190 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2191 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002192#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002193
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002194 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002195
2196 wp->w_vsep_width = 0;
2197
2198 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002199 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002200
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002201#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002202 // When running a terminal in the popup it becomes the current window.
2203 if (buf->b_term != NULL)
2204 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002205#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002206
Bram Moolenaara730e552019-06-16 19:05:31 +02002207 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002208}
2209
2210/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002211 * popup_clear()
2212 */
2213 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002214f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002215{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002216 int force = FALSE;
2217
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002218 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2219 return;
2220
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002221 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002222 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002223 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002224}
2225
2226/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002227 * popup_create({text}, {options})
2228 */
2229 void
2230f_popup_create(typval_T *argvars, typval_T *rettv)
2231{
Bram Moolenaar17627312019-06-02 19:53:44 +02002232 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002233}
2234
2235/*
2236 * popup_atcursor({text}, {options})
2237 */
2238 void
2239f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2240{
Bram Moolenaar17627312019-06-02 19:53:44 +02002241 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002242}
2243
2244/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002245 * popup_beval({text}, {options})
2246 */
2247 void
2248f_popup_beval(typval_T *argvars, typval_T *rettv)
2249{
2250 popup_create(argvars, rettv, TYPE_BEVAL);
2251}
2252
2253/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002254 * Invoke the close callback for window "wp" with value "result".
2255 * Careful: The callback may make "wp" invalid!
2256 */
2257 static void
2258invoke_popup_callback(win_T *wp, typval_T *result)
2259{
2260 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002261 typval_T argv[3];
2262
2263 argv[0].v_type = VAR_NUMBER;
2264 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2265
2266 if (result != NULL && result->v_type != VAR_UNKNOWN)
2267 copy_tv(result, &argv[1]);
2268 else
2269 {
2270 argv[1].v_type = VAR_NUMBER;
2271 argv[1].vval.v_number = 0;
2272 }
2273
2274 argv[2].v_type = VAR_UNKNOWN;
2275
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002276 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002277 if (result != NULL)
2278 clear_tv(&argv[1]);
2279 clear_tv(&rettv);
2280}
2281
2282/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002283 * Make "prevwin" the current window, unless it's equal to "wp".
2284 * Otherwise make "firstwin" the current window.
2285 */
2286 static void
2287back_to_prevwin(win_T *wp)
2288{
2289 if (win_valid(prevwin) && wp != prevwin)
2290 win_enter(prevwin, FALSE);
2291 else
2292 win_enter(firstwin, FALSE);
2293}
2294
2295/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002296 * Close popup "wp" and invoke any close callback for it.
2297 */
2298 static void
2299popup_close_and_callback(win_T *wp, typval_T *arg)
2300{
2301 int id = wp->w_id;
2302
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002303#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002304 if (wp == curwin && curbuf->b_term != NULL)
2305 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002306 win_T *owp;
2307
2308 // Closing popup window with a terminal: put focus back on the first
2309 // that works:
2310 // - another popup window with a terminal
2311 // - the previous window
2312 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002313 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002314 if (owp != curwin && owp->w_buffer->b_term != NULL)
2315 break;
2316 if (owp != NULL)
2317 win_enter(owp, FALSE);
2318 else
2319 {
2320 for (owp = curtab->tp_first_popupwin; owp != NULL;
2321 owp = owp->w_next)
2322 if (owp != curwin && owp->w_buffer->b_term != NULL)
2323 break;
2324 if (owp != NULL)
2325 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002326 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002327 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002328 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002329 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002330#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002331
Bram Moolenaar49540192019-12-11 19:34:54 +01002332 // Just in case a check higher up is missing.
2333 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002334 {
2335 // To avoid getting stuck when win_execute() does something that causes
2336 // an error, stop calling the filter callback.
2337 free_callback(&wp->w_filter_cb);
2338
Bram Moolenaar49540192019-12-11 19:34:54 +01002339 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002340 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002341
Bram Moolenaarcee52202020-03-11 14:19:58 +01002342 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002343 if (wp->w_close_cb.cb_name != NULL)
2344 // Careful: This may make "wp" invalid.
2345 invoke_popup_callback(wp, arg);
2346
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002347 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002348 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002349}
2350
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002351 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002352popup_close_with_retval(win_T *wp, int retval)
2353{
2354 typval_T res;
2355
2356 res.v_type = VAR_NUMBER;
2357 res.vval.v_number = retval;
2358 popup_close_and_callback(wp, &res);
2359}
2360
Bram Moolenaar3397f742019-06-02 18:40:06 +02002361/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002362 * Close popup "wp" because of a mouse click.
2363 */
2364 void
2365popup_close_for_mouse_click(win_T *wp)
2366{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002367 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002368}
2369
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002370 static void
2371check_mouse_moved(win_T *wp, win_T *mouse_wp)
2372{
2373 // Close the popup when all if these are true:
2374 // - the mouse is not on this popup
2375 // - "mousemoved" was used
2376 // - the mouse is no longer on the same screen row or the mouse column is
2377 // outside of the relevant text
2378 if (wp != mouse_wp
2379 && wp->w_popup_mouse_row != 0
2380 && (wp->w_popup_mouse_row != mouse_row
2381 || mouse_col < wp->w_popup_mouse_mincol
2382 || mouse_col > wp->w_popup_mouse_maxcol))
2383 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002384 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002385 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002386 }
2387}
2388
2389/*
2390 * Called when the mouse moved: may close a popup with "mousemoved".
2391 */
2392 void
2393popup_handle_mouse_moved(void)
2394{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002395 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002396 win_T *mouse_wp;
2397 int row = mouse_row;
2398 int col = mouse_col;
2399
2400 // find the window where the mouse is in
2401 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2402
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002403 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2404 {
2405 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002406 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002407 }
2408 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2409 {
2410 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002411 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002412 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002413}
2414
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002415/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002416 * In a filter: check if the typed key is a mouse event that is used for
2417 * dragging the popup.
2418 */
2419 static void
2420filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2421{
2422 int row = mouse_row;
2423 int col = mouse_col;
2424
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002425 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002426 && is_mouse_key(c)
2427 && (wp == popup_dragwin
2428 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2429 // do not consume the key, allow for dragging the popup
2430 rettv->vval.v_number = 0;
2431}
2432
Bram Moolenaara730e552019-06-16 19:05:31 +02002433/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002434 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002435 */
2436 void
2437f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2438{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002439 int id;
2440 win_T *wp;
2441 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002442 typval_T res;
2443 int c;
2444 linenr_T old_lnum;
2445
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002446 if (in_vim9script()
2447 && (check_for_number_arg(argvars, 0) == FAIL
2448 || check_for_string_arg(argvars, 1) == FAIL))
2449 return;
2450
2451 id = tv_get_number(&argvars[0]);
2452 wp = win_id2wp(id);
2453 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002454 // If the popup has been closed do not consume the key.
2455 if (wp == NULL)
2456 return;
2457
2458 c = *key;
2459 if (c == K_SPECIAL && key[1] != NUL)
2460 c = TO_SPECIAL(key[1], key[2]);
2461
2462 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002463 rettv->v_type = VAR_BOOL;
2464 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002465 res.v_type = VAR_NUMBER;
2466
2467 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002468 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2469 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002470 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002471 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002472 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2473 ++wp->w_cursor.lnum;
2474 if (old_lnum != wp->w_cursor.lnum)
2475 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002476 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002477 return;
2478 }
2479
2480 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2481 {
2482 // Cancelled, invoke callback with -1
2483 res.vval.v_number = -1;
2484 popup_close_and_callback(wp, &res);
2485 return;
2486 }
2487 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2488 {
2489 // Invoke callback with current index.
2490 res.vval.v_number = wp->w_cursor.lnum;
2491 popup_close_and_callback(wp, &res);
2492 return;
2493 }
2494
2495 filter_handle_drag(wp, c, rettv);
2496}
2497
2498/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002499 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002500 */
2501 void
2502f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2503{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002504 int id;
2505 win_T *wp;
2506 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002507 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002508 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002509
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002510 if (in_vim9script()
2511 && (check_for_number_arg(argvars, 0) == FAIL
2512 || check_for_string_arg(argvars, 1) == FAIL))
2513 return;
2514
2515 id = tv_get_number(&argvars[0]);
2516 wp = win_id2wp(id);
2517 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002518 // If the popup has been closed don't consume the key.
2519 if (wp == NULL)
2520 return;
2521
Bram Moolenaara730e552019-06-16 19:05:31 +02002522 c = *key;
2523 if (c == K_SPECIAL && key[1] != NUL)
2524 c = TO_SPECIAL(key[1], key[2]);
2525
Bram Moolenaara42d9452019-06-15 21:46:30 +02002526 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002527 rettv->v_type = VAR_BOOL;
2528 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002529
Bram Moolenaara730e552019-06-16 19:05:31 +02002530 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002531 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002532 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002533 res.vval.v_number = 0;
2534 else
2535 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002536 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002537 return;
2538 }
2539
2540 // Invoke callback
2541 res.v_type = VAR_NUMBER;
2542 popup_close_and_callback(wp, &res);
2543}
2544
2545/*
2546 * popup_dialog({text}, {options})
2547 */
2548 void
2549f_popup_dialog(typval_T *argvars, typval_T *rettv)
2550{
2551 popup_create(argvars, rettv, TYPE_DIALOG);
2552}
2553
2554/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002555 * popup_menu({text}, {options})
2556 */
2557 void
2558f_popup_menu(typval_T *argvars, typval_T *rettv)
2559{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002560 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002561}
2562
2563/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002564 * popup_notification({text}, {options})
2565 */
2566 void
2567f_popup_notification(typval_T *argvars, typval_T *rettv)
2568{
2569 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2570}
2571
2572/*
2573 * Find the popup window with window-ID "id".
2574 * If the popup window does not exist NULL is returned.
2575 * If the window is not a popup window, and error message is given.
2576 */
2577 static win_T *
2578find_popup_win(int id)
2579{
2580 win_T *wp = win_id2wp(id);
2581
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002582 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002583 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002584 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002585 return NULL;
2586 }
2587 return wp;
2588}
2589
2590/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002591 * popup_close({id})
2592 */
2593 void
2594f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2595{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002596 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002597 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002598
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002599 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2600 return;
2601
2602 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002603 if (
2604# ifdef FEAT_TERMINAL
2605 // if the popup contains a terminal it will become hidden
2606 curbuf->b_term == NULL &&
2607# endif
2608 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002609 return;
2610
2611 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002612 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002613 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002614}
2615
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002616 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002617popup_hide(win_T *wp)
2618{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002619#ifdef FEAT_TERMINAL
2620 if (error_if_term_popup_window())
2621 return;
2622#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002623 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2624 {
2625 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002626 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002627 redraw_all_later(NOT_VALID);
2628 popup_mask_refresh = TRUE;
2629 }
2630}
2631
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002632/*
2633 * popup_hide({id})
2634 */
2635 void
2636f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2637{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002638 int id;
2639 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002640
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002641 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2642 return;
2643
2644 id = (int)tv_get_number(argvars);
2645 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002646 if (wp != NULL)
2647 popup_hide(wp);
2648}
2649
2650 void
2651popup_show(win_T *wp)
2652{
2653 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002654 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002655 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002656 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002657 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002658 }
2659}
2660
2661/*
2662 * popup_show({id})
2663 */
2664 void
2665f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2666{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002667 int id;
2668 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002669
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002670 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2671 return;
2672
2673 id = (int)tv_get_number(argvars);
2674 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002675 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002676 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002677 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002678#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002679 if (wp->w_popup_flags & POPF_INFO)
2680 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002681#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002682 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002683}
2684
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002685/*
2686 * popup_settext({id}, {text})
2687 */
2688 void
2689f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2690{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002691 int id;
2692 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002693
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002694 if (in_vim9script()
2695 && (check_for_number_arg(argvars, 0) == FAIL
2696 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2697 return;
2698
2699 id = (int)tv_get_number(&argvars[0]);
2700 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002701 if (wp != NULL)
2702 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002703 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002704 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002705 else
2706 {
2707 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002708 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002709 popup_adjust_position(wp);
2710 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002711 }
2712}
2713
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002714 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002715popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002716{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002717 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002718 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002719 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002720 clear_cmdline = TRUE;
2721 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002722
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002723 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002724 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002725}
2726
Bram Moolenaar82106932020-03-13 14:34:38 +01002727 static void
2728error_for_popup_window(void)
2729{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002730 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002731}
2732
2733 int
2734error_if_popup_window(int also_with_term UNUSED)
2735{
2736 // win_execute() may set "curwin" to a popup window temporarily, but many
2737 // commands are disallowed then. When a terminal runs in the popup most
2738 // things are allowed. When a terminal is finished it can be closed.
2739 if (WIN_IS_POPUP(curwin)
2740# ifdef FEAT_TERMINAL
2741 && (also_with_term || curbuf->b_term == NULL)
2742 && !term_is_finished(curbuf)
2743# endif
2744 )
2745 {
2746 error_for_popup_window();
2747 return TRUE;
2748 }
2749 return FALSE;
2750}
2751
Bram Moolenaarec583842019-05-26 14:11:23 +02002752/*
2753 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002754 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002755 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002756 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002757 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002758popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002759{
2760 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002761 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002762 win_T *prev = NULL;
2763
Bram Moolenaarec583842019-05-26 14:11:23 +02002764 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002765 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002766 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002767 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002768 if (wp == curwin)
2769 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002770 if (!force)
2771 {
2772 error_for_popup_window();
2773 return FAIL;
2774 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002775 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002776 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002777 if (prev == NULL)
2778 first_popupwin = wp->w_next;
2779 else
2780 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002781 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002782 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002783 }
2784
Bram Moolenaarec583842019-05-26 14:11:23 +02002785 // go through tab-local popups
2786 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002787 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002788 return OK;
2789 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002790}
2791
2792/*
2793 * Close a popup window with Window-id "id" in tabpage "tp".
2794 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002795 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002796popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002797{
2798 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002799 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002800 win_T *prev = NULL;
2801
Bram Moolenaarec583842019-05-26 14:11:23 +02002802 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2803 if (wp->w_id == id)
2804 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002805 if (wp == curwin)
2806 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002807 if (!force)
2808 {
2809 error_for_popup_window();
2810 return FAIL;
2811 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002812 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002813 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002814 if (prev == NULL)
2815 *root = wp->w_next;
2816 else
2817 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002818 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002819 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002820 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002821 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002822}
2823
2824 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002825close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002826{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002827 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002828 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002829 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002830 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002831 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002832 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002833 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002834 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002835}
2836
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002837/*
2838 * popup_move({id}, {options})
2839 */
2840 void
2841f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2842{
Bram Moolenaarae943152019-06-16 22:54:14 +02002843 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002844 int id;
2845 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002846
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002847 if (in_vim9script()
2848 && (check_for_number_arg(argvars, 0) == FAIL
2849 || check_for_dict_arg(argvars, 1) == FAIL))
2850 return;
2851
2852 id = (int)tv_get_number(argvars);
2853 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002854 if (wp == NULL)
2855 return; // invalid {id}
2856
2857 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2858 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002859 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002860 return;
2861 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002862 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002863
Bram Moolenaarae943152019-06-16 22:54:14 +02002864 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002865
2866 if (wp->w_winrow + wp->w_height >= cmdline_row)
2867 clear_cmdline = TRUE;
2868 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002869}
2870
Bram Moolenaarbc133542019-05-29 20:26:46 +02002871/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002872 * popup_setoptions({id}, {options})
2873 */
2874 void
2875f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2876{
2877 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002878 int id;
2879 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002880 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002881
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002882 if (in_vim9script()
2883 && (check_for_number_arg(argvars, 0) == FAIL
2884 || check_for_dict_arg(argvars, 1) == FAIL))
2885 return;
2886
2887 id = (int)tv_get_number(argvars);
2888 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002889 if (wp == NULL)
2890 return; // invalid {id}
2891
2892 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2893 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002894 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002895 return;
2896 }
2897 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002898 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002899
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002900 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002901
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002902 if (old_firstline != wp->w_firstline)
2903 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002904 popup_adjust_position(wp);
2905}
2906
2907/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002908 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002909 */
2910 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002911f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002912{
2913 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002914 int id;
2915 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002916 int top_extra;
2917 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002918
2919 if (rettv_dict_alloc(rettv) == OK)
2920 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002921 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2922 return;
2923
2924 id = (int)tv_get_number(argvars);
2925 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002926 if (wp == NULL)
2927 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002928 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002929 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2930
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002931 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002932 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002933 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002934
Bram Moolenaarbc133542019-05-29 20:26:46 +02002935 dict_add_number(dict, "line", wp->w_winrow + 1);
2936 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002937 dict_add_number(dict, "width", wp->w_width + left_extra
2938 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2939 dict_add_number(dict, "height", wp->w_height + top_extra
2940 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002941
2942 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2943 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2944 dict_add_number(dict, "core_width", wp->w_width);
2945 dict_add_number(dict, "core_height", wp->w_height);
2946
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002947 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002948 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002949 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002950 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002951 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002952
2953 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002954 }
2955}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002956
2957/*
2958 * popup_list()
2959 */
2960 void
2961f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2962{
2963 win_T *wp;
2964 tabpage_T *tp;
2965
2966 if (rettv_list_alloc(rettv) != OK)
2967 return;
2968 FOR_ALL_POPUPWINS(wp)
2969 list_append_number(rettv->vval.v_list, wp->w_id);
2970 FOR_ALL_TABPAGES(tp)
2971 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2972 list_append_number(rettv->vval.v_list, wp->w_id);
2973}
2974
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002975/*
2976 * popup_locate({row}, {col})
2977 */
2978 void
2979f_popup_locate(typval_T *argvars, typval_T *rettv)
2980{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002981 int row;
2982 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002983 win_T *wp;
2984
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002985 if (in_vim9script()
2986 && (check_for_number_arg(argvars, 0) == FAIL
2987 || check_for_number_arg(argvars, 1) == FAIL))
2988 return;
2989
2990 row = tv_get_number(&argvars[0]) - 1;
2991 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002992 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002993 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002994 rettv->vval.v_number = wp->w_id;
2995}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002996
2997/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002998 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2999 */
3000 static void
3001get_padding_border(dict_T *dict, int *array, char *name)
3002{
3003 list_T *list;
3004 int i;
3005
3006 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3007 return;
3008
3009 list = list_alloc();
3010 if (list != NULL)
3011 {
3012 dict_add_list(dict, name, list);
3013 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3014 for (i = 0; i < 4; ++i)
3015 list_append_number(list, array[i]);
3016 }
3017}
3018
3019/*
3020 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3021 */
3022 static void
3023get_borderhighlight(dict_T *dict, win_T *wp)
3024{
3025 list_T *list;
3026 int i;
3027
3028 for (i = 0; i < 4; ++i)
3029 if (wp->w_border_highlight[i] != NULL)
3030 break;
3031 if (i == 4)
3032 return;
3033
3034 list = list_alloc();
3035 if (list != NULL)
3036 {
3037 dict_add_list(dict, "borderhighlight", list);
3038 for (i = 0; i < 4; ++i)
3039 list_append_string(list, wp->w_border_highlight[i], -1);
3040 }
3041}
3042
3043/*
3044 * For popup_getoptions(): add a "borderchars" entry to "dict".
3045 */
3046 static void
3047get_borderchars(dict_T *dict, win_T *wp)
3048{
3049 list_T *list;
3050 int i;
3051 char_u buf[NUMBUFLEN];
3052 int len;
3053
3054 for (i = 0; i < 8; ++i)
3055 if (wp->w_border_char[i] != 0)
3056 break;
3057 if (i == 8)
3058 return;
3059
3060 list = list_alloc();
3061 if (list != NULL)
3062 {
3063 dict_add_list(dict, "borderchars", list);
3064 for (i = 0; i < 8; ++i)
3065 {
3066 len = mb_char2bytes(wp->w_border_char[i], buf);
3067 list_append_string(list, buf, len);
3068 }
3069 }
3070}
3071
3072/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003073 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003074 */
3075 static void
3076get_moved_list(dict_T *dict, win_T *wp)
3077{
3078 list_T *list;
3079
3080 list = list_alloc();
3081 if (list != NULL)
3082 {
3083 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003084 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003085 list_append_number(list, wp->w_popup_mincol);
3086 list_append_number(list, wp->w_popup_maxcol);
3087 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003088 list = list_alloc();
3089 if (list != NULL)
3090 {
3091 dict_add_list(dict, "mousemoved", list);
3092 list_append_number(list, wp->w_popup_mouse_row);
3093 list_append_number(list, wp->w_popup_mouse_mincol);
3094 list_append_number(list, wp->w_popup_mouse_maxcol);
3095 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003096}
3097
3098/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003099 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003100 */
3101 void
3102f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3103{
3104 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003105 int id;
3106 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003107 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003108 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003109
3110 if (rettv_dict_alloc(rettv) == OK)
3111 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003112 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3113 return;
3114
3115 id = (int)tv_get_number(argvars);
3116 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003117 if (wp == NULL)
3118 return;
3119
3120 dict = rettv->vval.v_dict;
3121 dict_add_number(dict, "line", wp->w_wantline);
3122 dict_add_number(dict, "col", wp->w_wantcol);
3123 dict_add_number(dict, "minwidth", wp->w_minwidth);
3124 dict_add_number(dict, "minheight", wp->w_minheight);
3125 dict_add_number(dict, "maxheight", wp->w_maxheight);
3126 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003127 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003128 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003129 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003130 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003131 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003132 {
3133 proptype_T *pt = text_prop_type_by_id(
3134 wp->w_popup_prop_win->w_buffer,
3135 wp->w_popup_prop_type);
3136
3137 if (pt != NULL)
3138 dict_add_string(dict, "textprop", pt->pt_name);
3139 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3140 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3141 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003142 dict_add_string(dict, "title", wp->w_popup_title);
3143 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003144 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003145 dict_add_number(dict, "dragall",
3146 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003147 dict_add_number(dict, "mapping",
3148 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003149 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003150 dict_add_number(dict, "posinvert",
3151 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003152 dict_add_number(dict, "cursorline",
3153 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003154 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003155 if (wp->w_scrollbar_highlight != NULL)
3156 dict_add_string(dict, "scrollbarhighlight",
3157 wp->w_scrollbar_highlight);
3158 if (wp->w_thumb_highlight != NULL)
3159 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003160
Bram Moolenaara3fce622019-06-20 02:31:49 +02003161 // find the tabpage that holds this popup
3162 i = 1;
3163 FOR_ALL_TABPAGES(tp)
3164 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003165 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003166
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003167 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3168 if (twp->w_id == id)
3169 break;
3170 if (twp != NULL)
3171 break;
3172 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003173 }
3174 if (tp == NULL)
3175 i = -1; // must be global
3176 else if (tp == curtab)
3177 i = 0;
3178 dict_add_number(dict, "tabpage", i);
3179
Bram Moolenaarae943152019-06-16 22:54:14 +02003180 get_padding_border(dict, wp->w_popup_padding, "padding");
3181 get_padding_border(dict, wp->w_popup_border, "border");
3182 get_borderhighlight(dict, wp);
3183 get_borderchars(dict, wp);
3184 get_moved_list(dict, wp);
3185
3186 if (wp->w_filter_cb.cb_name != NULL)
3187 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3188 if (wp->w_close_cb.cb_name != NULL)
3189 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003190
K.Takataeeec2542021-06-02 13:28:16 +02003191 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003192 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3193 {
3194 dict_add_string(dict, "pos",
3195 (char_u *)poppos_entries[i].pp_name);
3196 break;
3197 }
3198
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003199 dict_add_string(dict, "close", (char_u *)(
3200 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3201 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3202
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003203# if defined(FEAT_TIMERS)
3204 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3205 ? (long)wp->w_popup_timer->tr_interval : 0L);
3206# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003207 }
3208}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003209
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003210# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003211/*
3212 * Return TRUE if the current window is running a terminal in a popup window.
3213 * Return FALSE when the job has ended.
3214 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003215 int
3216error_if_term_popup_window()
3217{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003218 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3219 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003220 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003221 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003222 return TRUE;
3223 }
3224 return FALSE;
3225}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003226# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003227
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003228/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003229 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003230 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003231 * Each calling function should use a different flag, see the list at
3232 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003233 */
3234 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003235popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003236{
3237 win_T *wp;
3238
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003239 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003240 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003241 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003242 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003243}
3244
3245/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003246 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003247 * Must have called popup_reset_handled() first.
3248 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3249 * popup with the highest zindex.
3250 */
3251 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003252find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003253{
3254 win_T *wp;
3255 win_T *found_wp;
3256 int found_zindex;
3257
3258 found_zindex = lowest ? INT_MAX : 0;
3259 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003260 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003261 if ((wp->w_popup_handled & handled_flag) == 0
3262 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003263 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003264 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003265 {
3266 found_zindex = wp->w_zindex;
3267 found_wp = wp;
3268 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003269 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003270 if ((wp->w_popup_handled & handled_flag) == 0
3271 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003272 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003273 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003274 {
3275 found_zindex = wp->w_zindex;
3276 found_wp = wp;
3277 }
3278
3279 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003280 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003281 return found_wp;
3282}
3283
3284/*
3285 * Invoke the filter callback for window "wp" with typed character "c".
3286 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003287 * Returns the return value of the filter or -1 for CTRL-C in the current
3288 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003289 * Careful: The filter may make "wp" invalid!
3290 */
3291 static int
3292invoke_popup_filter(win_T *wp, int c)
3293{
3294 int res;
3295 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003296 typval_T argv[3];
3297 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003298 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003299 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003300
Bram Moolenaara42d9452019-06-15 21:46:30 +02003301 // Emergency exit: CTRL-C closes the popup.
3302 if (c == Ctrl_C)
3303 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003304 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003305 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003306
3307 // Reset got_int to avoid the callback isn't called.
3308 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003309 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003310 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003311
3312 // If the popup is the current window it probably fails to close. Then
3313 // do not consume the key.
3314 if (was_curwin && wp == curwin)
3315 return -1;
3316 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003317 }
3318
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003319 argv[0].v_type = VAR_NUMBER;
3320 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3321
3322 // Convert the number to a string, so that the function can use:
3323 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003324 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003325 argv[1].v_type = VAR_STRING;
3326 argv[1].vval.v_string = vim_strsave(buf);
3327
3328 argv[2].v_type = VAR_UNKNOWN;
3329
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003330 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003331 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3332 {
3333 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003334 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003335 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003336 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003337 }
3338 else
3339 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003340 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3341 popup_highlight_curline(wp);
3342
Bram Moolenaar371806e2020-10-22 13:44:54 +02003343 // If an error message was given always return FALSE, so that keys are
3344 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003345 // If we get three errors in a row then close the popup. Decrement the
3346 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3347 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003348 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003349 {
3350 wp->w_filter_errors += 10;
3351 if (wp->w_filter_errors >= 30)
3352 popup_close_with_retval(wp, -1);
3353 res = FALSE;
3354 }
3355 else
3356 {
3357 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3358 --wp->w_filter_errors;
3359 res = tv_get_bool(&rettv);
3360 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003361 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003362
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003363 vim_free(argv[1].vval.v_string);
3364 clear_tv(&rettv);
3365 return res;
3366}
3367
3368/*
3369 * Called when "c" was typed: invoke popup filter callbacks.
3370 * Returns TRUE when the character was consumed,
3371 */
3372 int
3373popup_do_filter(int c)
3374{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003375 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003376 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003377 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003378 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003379 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003380 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003381
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003382#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003383 // Popup window with terminal always gets focus.
3384 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3385 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003386#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003387
Bram Moolenaar934470e2019-09-01 23:27:05 +02003388 if (recursive)
3389 return FALSE;
3390 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003391
Bram Moolenaarf6396232019-08-24 19:36:00 +02003392 if (c == K_LEFTMOUSE)
3393 {
3394 int row = mouse_row;
3395 int col = mouse_col;
3396
3397 wp = mouse_find_win(&row, &col, FIND_POPUP);
3398 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003399 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003400 }
3401
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003402 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003403 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003404 while (res == FALSE
3405 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003406 if (wp->w_filter_cb.cb_name != NULL
3407 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003408 res = invoke_popup_filter(wp, c);
3409
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003410 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003411 {
3412 int save_got_int = got_int;
3413
3414 // Reset got_int to avoid a function used in the statusline aborts.
3415 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003416 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003417 got_int |= save_got_int;
3418 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003419 recursive = FALSE;
3420 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003421
3422 // When interrupted return FALSE to avoid looping.
3423 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003424}
3425
Bram Moolenaar3397f742019-06-02 18:40:06 +02003426/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003427 * Return TRUE if there is a popup visible with a filter callback and the
3428 * "mapping" property off.
3429 */
3430 int
3431popup_no_mapping(void)
3432{
3433 int round;
3434 win_T *wp;
3435
3436 for (round = 1; round <= 2; ++round)
3437 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3438 wp != NULL; wp = wp->w_next)
3439 if (wp->w_filter_cb.cb_name != NULL
3440 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3441 return TRUE;
3442 return FALSE;
3443}
3444
3445/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003446 * Called when the cursor moved: check if any popup needs to be closed if the
3447 * cursor moved far enough.
3448 */
3449 void
3450popup_check_cursor_pos()
3451{
3452 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003453
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003454 popup_reset_handled(POPUP_HANDLED_3);
3455 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003456 if (wp->w_popup_curwin != NULL
3457 && (curwin != wp->w_popup_curwin
3458 || curwin->w_cursor.lnum != wp->w_popup_lnum
3459 || curwin->w_cursor.col < wp->w_popup_mincol
3460 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003461 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003462}
3463
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003464/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003465 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003466 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003467 static void
3468popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003469{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003470 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003471 char_u *cells;
3472 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003473
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003474 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3475 {
3476 vim_free(wp->w_popup_mask_cells);
3477 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003478 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003479 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003480 if (wp->w_popup_mask_cells != NULL
3481 && wp->w_popup_mask_height == height
3482 && wp->w_popup_mask_width == width)
3483 return; // cache is still valid
3484
3485 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003486 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003487 if (wp->w_popup_mask_cells == NULL)
3488 return;
3489 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003490
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003491 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003492 {
3493 int cols, cole;
3494 int lines, linee;
3495
3496 li = lio->li_tv.vval.v_list->lv_first;
3497 cols = tv_get_number(&li->li_tv);
3498 if (cols < 0)
3499 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003500 if (cols <= 0)
3501 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003502 li = li->li_next;
3503 cole = tv_get_number(&li->li_tv);
3504 if (cole < 0)
3505 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003506 if (cole > width)
3507 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003508 li = li->li_next;
3509 lines = tv_get_number(&li->li_tv);
3510 if (lines < 0)
3511 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003512 if (lines <= 0)
3513 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003514 li = li->li_next;
3515 linee = tv_get_number(&li->li_tv);
3516 if (linee < 0)
3517 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003518 if (linee > height)
3519 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003520
Bram Moolenaar4012d262020-12-29 11:57:46 +01003521 for (row = lines - 1; row < linee; ++row)
3522 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003523 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003524 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003525}
3526
3527/*
3528 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3529 * "col" and "line" are screen coordinates.
3530 */
3531 static int
3532popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3533{
3534 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3535 int line = screenline - wp->w_winrow;
3536
3537 return col >= 0 && col < width
3538 && line >= 0 && line < height
3539 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003540}
3541
3542/*
3543 * Set flags in popup_transparent[] for window "wp" to "val".
3544 */
3545 static void
3546update_popup_transparent(win_T *wp, int val)
3547{
3548 if (wp->w_popup_mask != NULL)
3549 {
3550 int width = popup_width(wp);
3551 int height = popup_height(wp);
3552 listitem_T *lio, *li;
3553 int cols, cole;
3554 int lines, linee;
3555 int col, line;
3556
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003557 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003558 {
3559 li = lio->li_tv.vval.v_list->lv_first;
3560 cols = tv_get_number(&li->li_tv);
3561 if (cols < 0)
3562 cols = width + cols + 1;
3563 li = li->li_next;
3564 cole = tv_get_number(&li->li_tv);
3565 if (cole < 0)
3566 cole = width + cole + 1;
3567 li = li->li_next;
3568 lines = tv_get_number(&li->li_tv);
3569 if (lines < 0)
3570 lines = height + lines + 1;
3571 li = li->li_next;
3572 linee = tv_get_number(&li->li_tv);
3573 if (linee < 0)
3574 linee = height + linee + 1;
3575
3576 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003577 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003578 if (cols < 0)
3579 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003580 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003581 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003582 if (lines < 0)
3583 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003584 for (line = lines; line < linee
3585 && line + wp->w_winrow < screen_Rows; ++line)
3586 for (col = cols; col < cole
3587 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003588 popup_transparent[(line + wp->w_winrow) * screen_Columns
3589 + col + wp->w_wincol] = val;
3590 }
3591 }
3592}
3593
3594/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003595 * Only called when popup window "wp" is hidden: If the window is positioned
3596 * next to a text property, and it is now visible, then unhide the popup.
3597 * We don't check if visible popups become hidden, that is done in
3598 * popup_adjust_position().
3599 * Return TRUE if the popup became unhidden.
3600 */
3601 static int
3602check_popup_unhidden(win_T *wp)
3603{
3604 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3605 {
3606 textprop_T prop;
3607 linenr_T lnum;
3608
3609 if (find_visible_prop(wp->w_popup_prop_win,
3610 wp->w_popup_prop_type, wp->w_popup_prop_id,
3611 &prop, &lnum) == OK)
3612 {
3613 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003614 wp->w_popup_prop_topline = 0; // force repositioning
3615 return TRUE;
3616 }
3617 }
3618 return FALSE;
3619}
3620
3621/*
3622 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3623 * That is when the buffer in the popup was changed, or the popup is following
3624 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003625 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003626 */
3627 static int
3628popup_need_position_adjust(win_T *wp)
3629{
3630 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3631 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003632 if (win_valid(wp->w_popup_prop_win)
3633 && (wp->w_popup_prop_changedtick
3634 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3635 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3636 return TRUE;
3637
3638 // May need to adjust the width if the cursor moved.
3639 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003640}
3641
3642/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003643 * Update "popup_mask" if needed.
3644 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003645 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003646 * Also marks window lines for redrawing.
3647 */
3648 void
3649may_update_popup_mask(int type)
3650{
3651 win_T *wp;
3652 short *mask;
3653 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003654 int redraw_all_popups = FALSE;
3655 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003656
3657 // Need to recompute when switching tabs.
3658 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3659 // (such as the screen size) must have changed.
3660 if (popup_mask_tab != curtab || type >= NOT_VALID)
3661 {
3662 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003663 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003664 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003665
3666 // Check if any popup window buffer has changed and if any popup connected
3667 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003668 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003669 if (wp->w_popup_flags & POPF_HIDDEN)
3670 popup_mask_refresh |= check_popup_unhidden(wp);
3671 else if (popup_need_position_adjust(wp))
3672 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003673 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003674 if (wp->w_popup_flags & POPF_HIDDEN)
3675 popup_mask_refresh |= check_popup_unhidden(wp);
3676 else if (popup_need_position_adjust(wp))
3677 popup_mask_refresh = TRUE;
3678
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003679 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003680 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003681
3682 // Need to update the mask, something has changed.
3683 popup_mask_refresh = FALSE;
3684 popup_mask_tab = curtab;
3685 popup_visible = FALSE;
3686
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003687 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003688 // If redrawing only what is needed, update "popup_mask_next" and then
3689 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003690 redrawing_all_win = TRUE;
3691 FOR_ALL_WINDOWS(wp)
3692 if (wp->w_redr_type < SOME_VALID)
3693 redrawing_all_win = FALSE;
3694 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003695 mask = popup_mask;
3696 else
3697 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003698 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003699
3700 // Find the window with the lowest zindex that hasn't been handled yet,
3701 // so that the window with a higher zindex overwrites the value in
3702 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003703 popup_reset_handled(POPUP_HANDLED_4);
3704 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003705 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003706 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003707 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003708
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003709 popup_visible = TRUE;
3710
Bram Moolenaar12034e22019-08-25 22:25:02 +02003711 // Recompute the position if the text changed. It may make the popup
3712 // hidden if it's attach to a text property that is no longer visible.
3713 if (redraw_all_popups || popup_need_position_adjust(wp))
3714 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003715 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003716 if (wp->w_popup_flags & POPF_HIDDEN)
3717 continue;
3718 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003719
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003720 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003721 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003722 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003723 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003724 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003725 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003726 col < wp->w_wincol + width - wp->w_popup_leftoff
3727 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003728 if (wp->w_zindex < POPUPMENU_ZINDEX
3729 && pum_visible()
3730 && pum_under_menu(line, col, FALSE))
3731 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3732 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003733 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003734 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003735 }
3736
3737 // Only check which lines are to be updated if not already
3738 // updating all lines.
3739 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003740 {
3741 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3742 win_T *prev_wp = NULL;
3743
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003744 for (line = 0; line < screen_Rows; ++line)
3745 {
3746 int col_done = 0;
3747
3748 for (col = 0; col < screen_Columns; ++col)
3749 {
3750 int off = line * screen_Columns + col;
3751
3752 if (popup_mask[off] != popup_mask_next[off])
3753 {
3754 popup_mask[off] = popup_mask_next[off];
3755
3756 if (line >= cmdline_row)
3757 {
3758 // the command line needs to be cleared if text below
3759 // the popup is now visible.
3760 if (!msg_scrolled && popup_mask_next[off] == 0)
3761 clear_cmdline = TRUE;
3762 }
3763 else if (col >= col_done)
3764 {
3765 linenr_T lnum;
3766 int line_cp = line;
3767 int col_cp = col;
3768
3769 // The screen position "line" / "col" needs to be
3770 // redrawn. Figure out what window that is and update
3771 // w_redraw_top and w_redr_bot. Only needs to be done
3772 // once for each window line.
3773 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3774 if (wp != NULL)
3775 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003776#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003777 // A terminal window needs to be redrawn.
3778 if (bt_terminal(wp->w_buffer))
3779 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003780 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003781#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003782 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003783 if (wp != prev_wp)
3784 {
3785 vim_memset(plines_cache, 0,
3786 sizeof(int) * Rows);
3787 prev_wp = wp;
3788 }
3789
3790 if (line_cp >= wp->w_height)
3791 // In (or below) status line
3792 wp->w_redr_status = TRUE;
3793 else
3794 {
3795 // compute the position in the buffer line
3796 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003797 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003798 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003799 redrawWinline(wp, lnum);
3800 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003801 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003802
3803 // This line is going to be redrawn, no need to
3804 // check until the right side of the window.
3805 col_done = wp->w_wincol + wp->w_width - 1;
3806 }
3807 }
3808 }
3809 }
3810 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003811
3812 vim_free(plines_cache);
3813 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003814
3815 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003816}
3817
3818/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003819 * If the current window is a popup and something relevant changed, recompute
3820 * the position and size.
3821 */
3822 void
3823may_update_popup_position(void)
3824{
3825 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3826 popup_adjust_position(curwin);
3827}
3828
3829/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003830 * Return a string of "len" spaces in IObuff.
3831 */
3832 static char_u *
3833get_spaces(int len)
3834{
3835 vim_memset(IObuff, ' ', (size_t)len);
3836 IObuff[len] = NUL;
3837 return IObuff;
3838}
3839
3840/*
3841 * Update popup windows. They are drawn on top of normal windows.
3842 * "win_update" is called for each popup window, lowest zindex first.
3843 */
3844 void
3845update_popups(void (*win_update)(win_T *wp))
3846{
3847 win_T *wp;
3848 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003849 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003850 int total_width;
3851 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003852 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003853 int popup_attr;
3854 int border_attr[4];
3855 int border_char[8];
3856 char_u buf[MB_MAXBYTES];
3857 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003858 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003859 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003860 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003861 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003862 int sb_thumb_top = 0;
3863 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003864 int attr_scroll = 0;
3865 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003866
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003867 // hide the cursor until redrawing is done.
3868 cursor_off();
3869
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003870 // Find the window with the lowest zindex that hasn't been updated yet,
3871 // so that the window with a higher zindex is drawn later, thus goes on
3872 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003873 popup_reset_handled(POPUP_HANDLED_5);
3874 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003875 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003876 int title_len = 0;
3877 int title_wincol;
3878
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003879 // This drawing uses the zindex of the popup window, so that it's on
3880 // top of the text but doesn't draw when another popup with higher
3881 // zindex is on top of the character.
3882 screen_zindex = wp->w_zindex;
3883
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003884 // Set flags in popup_transparent[] for masked cells.
3885 update_popup_transparent(wp, 1);
3886
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003887 // adjust w_winrow and w_wincol for border and padding, since
3888 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003889 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003890 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3891 - wp->w_popup_leftoff;
3892 if (wp->w_wincol + left_extra < 0)
3893 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003894 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003895 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003896
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003897 // Draw the popup text, unless it's off screen.
3898 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003899 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003900 // May need to update the "cursorline" highlighting, which may also
3901 // change "topline"
3902 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3903 popup_highlight_curline(wp);
3904
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003905 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003906
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003907 // move the cursor into the visible lines, otherwise executing
3908 // commands with win_execute() may cause the text to jump.
3909 if (wp->w_cursor.lnum < wp->w_topline)
3910 wp->w_cursor.lnum = wp->w_topline;
3911 else if (wp->w_cursor.lnum >= wp->w_botline)
3912 wp->w_cursor.lnum = wp->w_botline - 1;
3913 }
3914
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003915 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003916 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003917
3918 // Add offset for border and padding if not done already.
3919 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3920 {
3921 wp->w_wcol += left_extra;
3922 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3923 }
3924 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003925 {
3926 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003927 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003928 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003929
Bram Moolenaareabddc42022-04-02 15:32:16 +01003930 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003931 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003932 popup_attr = get_wcr_attr(wp);
3933
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003934 if (wp->w_winrow + total_height > cmdline_row)
3935 wp->w_popup_flags |= POPF_ON_CMDLINE;
3936 else
3937 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3938
3939
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003940 // We can only use these line drawing characters when 'encoding' is
3941 // "utf-8" and 'ambiwidth' is "single".
3942 if (enc_utf8 && *p_ambw == 's')
3943 {
3944 border_char[0] = border_char[2] = 0x2550;
3945 border_char[1] = border_char[3] = 0x2551;
3946 border_char[4] = 0x2554;
3947 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003948 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3949 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003950 border_char[7] = 0x255a;
3951 }
3952 else
3953 {
3954 border_char[0] = border_char[2] = '-';
3955 border_char[1] = border_char[3] = '|';
3956 for (i = 4; i < 8; ++i)
3957 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003958 if (wp->w_popup_flags & POPF_RESIZE)
3959 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003960 }
3961 for (i = 0; i < 8; ++i)
3962 if (wp->w_border_char[i] != 0)
3963 border_char[i] = wp->w_border_char[i];
3964
3965 for (i = 0; i < 4; ++i)
3966 {
3967 border_attr[i] = popup_attr;
3968 if (wp->w_border_highlight[i] != NULL)
3969 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3970 }
3971
Bram Moolenaard91467f2020-11-21 12:42:09 +01003972 // Title goes on top of border or padding.
3973 title_wincol = wp->w_wincol + 1;
3974 if (wp->w_popup_title != NULL)
3975 {
rbtnnc6119412021-08-07 13:08:45 +02003976 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003977
Ralf Schandlbc869872021-05-28 14:12:14 +02003978 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003979 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02003980 {
3981 int title_byte_len = (int)STRLEN(wp->w_popup_title);
3982 char_u *title_text = alloc(title_byte_len + 1);
3983
3984 if (title_text != NULL)
3985 {
3986 trunc_string(wp->w_popup_title, title_text,
3987 total_width - 2, title_byte_len + 1);
3988 screen_puts(title_text, wp->w_winrow, title_wincol,
3989 wp->w_popup_border[0] > 0
3990 ? border_attr[0] : popup_attr);
3991 vim_free(title_text);
3992 }
3993
Bram Moolenaard91467f2020-11-21 12:42:09 +01003994 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02003995 }
3996 else
3997 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
3998 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003999 }
4000
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004001 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004002 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004003 if (wp->w_popup_border[0] > 0)
4004 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004005 // top border; do not draw over the title
4006 if (title_len > 0)
4007 {
4008 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4009 wincol < 0 ? 0 : wincol, title_wincol,
4010 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004011 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004012 border_char[0], border_attr[0]);
4013 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4014 title_wincol + title_len, wincol + total_width,
4015 border_char[0], border_char[0], border_attr[0]);
4016 }
4017 else
4018 {
4019 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4020 wincol < 0 ? 0 : wincol, wincol + total_width,
4021 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4022 ? border_char[4] : border_char[0],
4023 border_char[0], border_attr[0]);
4024 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004025 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004026 {
4027 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4028 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004029 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004030 }
4031 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004032 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4033 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004034
Bram Moolenaard529ba52019-07-02 23:13:53 +02004035 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4036 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004037 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004038 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004039 - wp->w_has_scrollbar;
4040 if (padcol < 0)
4041 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004042 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004043 padcol = 0;
4044 }
4045 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004046 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004047 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004048 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004049 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004050 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004051 // top padding and no border; do not draw over the title
4052 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004053 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004054 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004055 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004056 row += 1;
4057 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004058 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004059 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004060 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004061 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004062
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004063 // Compute scrollbar thumb position and size.
4064 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004065 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004066 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4067 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004068
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004069 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4070 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004071 if (wp->w_topline > 1 && sb_thumb_height == height)
4072 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004073 if (sb_thumb_height == 0)
4074 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004075 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004076 // it just fits, avoid divide by zero
4077 sb_thumb_top = 0;
4078 else
4079 sb_thumb_top = (wp->w_topline - 1
4080 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004081 * (wp->w_height - sb_thumb_height)
4082 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004083 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4084 sb_thumb_top = 1; // show it's scrolled
4085
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004086 if (wp->w_scrollbar_highlight != NULL)
4087 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4088 else
4089 attr_scroll = highlight_attr[HLF_PSB];
4090 if (wp->w_thumb_highlight != NULL)
4091 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4092 else
4093 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004094 }
4095
4096 for (i = wp->w_popup_border[0];
4097 i < total_height - wp->w_popup_border[2]; ++i)
4098 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004099 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004100 // left and right padding only needed next to the body
4101 int do_padding =
4102 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4103 && i < total_height - wp->w_popup_border[2]
4104 - wp->w_popup_padding[2];
4105
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004106 row = wp->w_winrow + i;
4107
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004108 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004109 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004110 {
4111 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004112 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004113 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004114 if (do_padding && wp->w_popup_padding[3] > 0)
4115 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004116 int col = wincol + wp->w_popup_border[3];
4117
Bram Moolenaard529ba52019-07-02 23:13:53 +02004118 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004119 pad_left = wp->w_popup_padding[3];
4120 if (col < 0)
4121 {
4122 pad_left += col;
4123 col = 0;
4124 }
4125 if (pad_left > 0)
4126 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4127 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004128 // scrollbar
4129 if (wp->w_has_scrollbar)
4130 {
4131 int line = i - top_off;
4132 int scroll_col = wp->w_wincol + total_width - 1
4133 - wp->w_popup_border[1];
4134
4135 if (line >= 0 && line < wp->w_height)
4136 screen_putchar(' ', row, scroll_col,
4137 line >= sb_thumb_top
4138 && line < sb_thumb_top + sb_thumb_height
4139 ? attr_thumb : attr_scroll);
4140 else
4141 screen_putchar(' ', row, scroll_col, popup_attr);
4142 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004143 // right border
4144 if (wp->w_popup_border[1] > 0)
4145 {
4146 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004147 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004148 }
4149 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004150 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004151 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004152 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004153 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4154 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004155 }
4156
4157 if (wp->w_popup_padding[2] > 0)
4158 {
4159 // bottom padding
4160 row = wp->w_winrow + wp->w_popup_border[0]
4161 + wp->w_popup_padding[0] + wp->w_height;
4162 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004163 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004164 }
4165
4166 if (wp->w_popup_border[2] > 0)
4167 {
4168 // bottom border
4169 row = wp->w_winrow + total_height - 1;
4170 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004171 wincol < 0 ? 0 : wincol,
4172 wincol + total_width,
4173 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004174 ? border_char[7] : border_char[2],
4175 border_char[2], border_attr[2]);
4176 if (wp->w_popup_border[1] > 0)
4177 {
4178 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004179 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004180 }
4181 }
4182
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004183 if (wp->w_popup_close == POPCLOSE_BUTTON)
4184 {
4185 // close button goes on top of anything at the top-right corner
4186 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004187 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004188 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4189 }
4190
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004191 update_popup_transparent(wp, 0);
4192
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004193 // Back to the normal zindex.
4194 screen_zindex = 0;
4195 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004196
4197#if defined(FEAT_SEARCH_EXTRA)
4198 // In case win_update() called start_search_hl().
4199 end_search_hl();
4200#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004201}
4202
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004203/*
4204 * Mark references in callbacks of one popup window.
4205 */
4206 static int
4207set_ref_in_one_popup(win_T *wp, int copyID)
4208{
4209 int abort = FALSE;
4210 typval_T tv;
4211
4212 if (wp->w_close_cb.cb_partial != NULL)
4213 {
4214 tv.v_type = VAR_PARTIAL;
4215 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4216 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4217 }
4218 if (wp->w_filter_cb.cb_partial != NULL)
4219 {
4220 tv.v_type = VAR_PARTIAL;
4221 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4222 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4223 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004224 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004225 return abort;
4226}
4227
4228/*
4229 * Set reference in callbacks of popup windows.
4230 */
4231 int
4232set_ref_in_popups(int copyID)
4233{
4234 int abort = FALSE;
4235 win_T *wp;
4236 tabpage_T *tp;
4237
4238 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4239 abort = abort || set_ref_in_one_popup(wp, copyID);
4240
4241 FOR_ALL_TABPAGES(tp)
4242 {
4243 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4244 abort = abort || set_ref_in_one_popup(wp, copyID);
4245 if (abort)
4246 break;
4247 }
4248 return abort;
4249}
Bram Moolenaar79648732019-07-18 21:43:07 +02004250
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004251 int
4252popup_is_popup(win_T *wp)
4253{
4254 return wp->w_popup_flags != 0;
4255}
4256
4257#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004258/*
4259 * Find an existing popup used as the preview window, in the current tab page.
4260 * Return NULL if not found.
4261 */
4262 win_T *
4263popup_find_preview_window(void)
4264{
4265 win_T *wp;
4266
4267 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004268 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004269 if (wp->w_p_pvw)
4270 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004271 return NULL;
4272}
4273
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004274/*
4275 * Find an existing popup used as the info window, in the current tab page.
4276 * Return NULL if not found.
4277 */
4278 win_T *
4279popup_find_info_window(void)
4280{
4281 win_T *wp;
4282
4283 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004284 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004285 if (wp->w_popup_flags & POPF_INFO)
4286 return wp;
4287 return NULL;
4288}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004289#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004290
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004291 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004292f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4293{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004294#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004295 win_T *wp = popup_find_info_window();
4296
4297 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004298#else
4299 rettv->vval.v_number = 0;
4300#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004301}
4302
4303 void
4304f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004305{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004306#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004307 win_T *wp = popup_find_preview_window();
4308
4309 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004310#else
4311 rettv->vval.v_number = 0;
4312#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004313}
4314
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004315#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004316/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004317 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004318 * NOTE: this makes the popup the current window, so that the file can be
4319 * edited. However, it must not remain to be the current window, the caller
4320 * must make sure of that.
4321 */
4322 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004323popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004324{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004325 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004326
4327 if (wp == NULL)
4328 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004329 if (info)
4330 wp->w_popup_flags |= POPF_INFO;
4331 else
4332 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004333
4334 // Set the width to a reasonable value, so that w_topline can be computed.
4335 if (wp->w_minwidth > 0)
4336 wp->w_width = wp->w_minwidth;
4337 else if (wp->w_maxwidth > 0)
4338 wp->w_width = wp->w_maxwidth;
4339 else
4340 wp->w_width = curwin->w_width;
4341
4342 // Will switch to another buffer soon, dummy one can be wiped.
4343 wp->w_buffer->b_locked = FALSE;
4344
4345 win_enter(wp, FALSE);
4346 return OK;
4347}
4348
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004349/*
4350 * Close any preview popup.
4351 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004352 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004353popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004354{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004355 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004356
4357 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004358 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004359}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004360
4361/*
4362 * Hide the info popup.
4363 */
4364 void
4365popup_hide_info(void)
4366{
4367 win_T *wp = popup_find_info_window();
4368
4369 if (wp != NULL)
4370 popup_hide(wp);
4371}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004372
4373/*
4374 * Close any info popup.
4375 */
4376 void
4377popup_close_info(void)
4378{
4379 win_T *wp = popup_find_info_window();
4380
4381 if (wp != NULL)
4382 popup_close_with_retval(wp, -1);
4383}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004384#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004385
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004386/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004387 * Close any popup for a text property associated with "win".
4388 * Return TRUE if a popup was closed.
4389 */
4390 int
4391popup_win_closed(win_T *win)
4392{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004393 int round;
4394 win_T *wp;
4395 win_T *next;
4396 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004397
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004398 for (round = 1; round <= 2; ++round)
4399 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4400 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004401 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004402 next = wp->w_next;
4403 if (wp->w_popup_prop_win == win)
4404 {
4405 popup_close_with_retval(wp, -1);
4406 ret = TRUE;
4407 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004408 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004409 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004410}
4411
4412/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004413 * Set the title of the popup window to the file name.
4414 */
4415 void
4416popup_set_title(win_T *wp)
4417{
4418 if (wp->w_buffer->b_fname != NULL)
4419 {
4420 char_u dirname[MAXPATHL];
4421 size_t len;
4422
4423 mch_dirname(dirname, MAXPATHL);
4424 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4425
4426 vim_free(wp->w_popup_title);
4427 len = STRLEN(wp->w_buffer->b_fname) + 3;
4428 wp->w_popup_title = alloc((int)len);
4429 if (wp->w_popup_title != NULL)
4430 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4431 wp->w_buffer->b_fname);
4432 redraw_win_later(wp, VALID);
4433 }
4434}
4435
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004436# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004437/*
4438 * If there is a preview window, update the title.
4439 * Used after changing directory.
4440 */
4441 void
4442popup_update_preview_title(void)
4443{
4444 win_T *wp = popup_find_preview_window();
4445
4446 if (wp != NULL)
4447 popup_set_title(wp);
4448}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004449# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004450
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004451#endif // FEAT_PROP_POPUP