blob: 2306dacc1ac224e0ca9390226b29958818171669 [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 Moolenaar27724252022-05-08 15:00:04 +0100981 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
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)
LemonBoy0044e512022-04-20 19:47:37 +01001417 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001418
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)
Bram Moolenaar27724252022-05-08 15:00:04 +01002647 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002648 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002649 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2650 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002651}
2652
2653 void
2654popup_show(win_T *wp)
2655{
2656 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002657 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002658 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002659 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002660 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002661 }
2662}
2663
2664/*
2665 * popup_show({id})
2666 */
2667 void
2668f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2669{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002670 int id;
2671 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002672
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002673 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2674 return;
2675
2676 id = (int)tv_get_number(argvars);
2677 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002678 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002679 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002680 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002681 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002682#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002683 if (wp->w_popup_flags & POPF_INFO)
2684 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002685#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002686 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002687}
2688
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002689/*
2690 * popup_settext({id}, {text})
2691 */
2692 void
2693f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2694{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002695 int id;
2696 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002697
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002698 if (in_vim9script()
2699 && (check_for_number_arg(argvars, 0) == FAIL
2700 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2701 return;
2702
2703 id = (int)tv_get_number(&argvars[0]);
2704 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002705 if (wp != NULL)
2706 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002707 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002708 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002709 else
2710 {
2711 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002712 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002713 popup_adjust_position(wp);
2714 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002715 }
2716}
2717
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002718 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002719popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002720{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002721 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002722 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002723 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002724 clear_cmdline = TRUE;
2725 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002726
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002727 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002728 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002729}
2730
Bram Moolenaar82106932020-03-13 14:34:38 +01002731 static void
2732error_for_popup_window(void)
2733{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002734 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002735}
2736
2737 int
2738error_if_popup_window(int also_with_term UNUSED)
2739{
2740 // win_execute() may set "curwin" to a popup window temporarily, but many
2741 // commands are disallowed then. When a terminal runs in the popup most
2742 // things are allowed. When a terminal is finished it can be closed.
2743 if (WIN_IS_POPUP(curwin)
2744# ifdef FEAT_TERMINAL
2745 && (also_with_term || curbuf->b_term == NULL)
2746 && !term_is_finished(curbuf)
2747# endif
2748 )
2749 {
2750 error_for_popup_window();
2751 return TRUE;
2752 }
2753 return FALSE;
2754}
2755
Bram Moolenaarec583842019-05-26 14:11:23 +02002756/*
2757 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002758 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002759 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002760 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002761 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002762popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002763{
2764 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002765 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002766 win_T *prev = NULL;
2767
Bram Moolenaarec583842019-05-26 14:11:23 +02002768 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002769 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002770 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002771 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002772 if (wp == curwin)
2773 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002774 if (!force)
2775 {
2776 error_for_popup_window();
2777 return FAIL;
2778 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002779 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002780 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002781 if (prev == NULL)
2782 first_popupwin = wp->w_next;
2783 else
2784 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002785 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002786 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002787 }
2788
Bram Moolenaarec583842019-05-26 14:11:23 +02002789 // go through tab-local popups
2790 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002791 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002792 return OK;
2793 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002794}
2795
2796/*
2797 * Close a popup window with Window-id "id" in tabpage "tp".
2798 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002799 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002800popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002801{
2802 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002803 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002804 win_T *prev = NULL;
2805
Bram Moolenaarec583842019-05-26 14:11:23 +02002806 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2807 if (wp->w_id == id)
2808 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002809 if (wp == curwin)
2810 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002811 if (!force)
2812 {
2813 error_for_popup_window();
2814 return FAIL;
2815 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002816 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002817 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002818 if (prev == NULL)
2819 *root = wp->w_next;
2820 else
2821 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002822 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002823 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002824 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002825 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002826}
2827
2828 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002829close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002830{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002831 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002832 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002833 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002834 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002835 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002836 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002837 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002838 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002839}
2840
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002841/*
2842 * popup_move({id}, {options})
2843 */
2844 void
2845f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2846{
Bram Moolenaarae943152019-06-16 22:54:14 +02002847 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002848 int id;
2849 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002850
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002851 if (in_vim9script()
2852 && (check_for_number_arg(argvars, 0) == FAIL
2853 || check_for_dict_arg(argvars, 1) == FAIL))
2854 return;
2855
2856 id = (int)tv_get_number(argvars);
2857 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002858 if (wp == NULL)
2859 return; // invalid {id}
2860
2861 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2862 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002863 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002864 return;
2865 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002866 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002867
Bram Moolenaarae943152019-06-16 22:54:14 +02002868 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002869
2870 if (wp->w_winrow + wp->w_height >= cmdline_row)
2871 clear_cmdline = TRUE;
2872 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002873}
2874
Bram Moolenaarbc133542019-05-29 20:26:46 +02002875/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002876 * popup_setoptions({id}, {options})
2877 */
2878 void
2879f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2880{
2881 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002882 int id;
2883 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002884 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002885
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002886 if (in_vim9script()
2887 && (check_for_number_arg(argvars, 0) == FAIL
2888 || check_for_dict_arg(argvars, 1) == FAIL))
2889 return;
2890
2891 id = (int)tv_get_number(argvars);
2892 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002893 if (wp == NULL)
2894 return; // invalid {id}
2895
2896 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2897 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002898 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002899 return;
2900 }
2901 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002902 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002903
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002904 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002905
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002906 if (old_firstline != wp->w_firstline)
2907 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002908 popup_adjust_position(wp);
2909}
2910
2911/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002912 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002913 */
2914 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002915f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002916{
2917 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002918 int id;
2919 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002920 int top_extra;
2921 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002922
2923 if (rettv_dict_alloc(rettv) == OK)
2924 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002925 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2926 return;
2927
2928 id = (int)tv_get_number(argvars);
2929 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002930 if (wp == NULL)
2931 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002932 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002933 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2934
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002935 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002936 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002937 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002938
Bram Moolenaarbc133542019-05-29 20:26:46 +02002939 dict_add_number(dict, "line", wp->w_winrow + 1);
2940 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002941 dict_add_number(dict, "width", wp->w_width + left_extra
2942 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2943 dict_add_number(dict, "height", wp->w_height + top_extra
2944 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002945
2946 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2947 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2948 dict_add_number(dict, "core_width", wp->w_width);
2949 dict_add_number(dict, "core_height", wp->w_height);
2950
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002951 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002952 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002953 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002954 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002955 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002956
2957 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002958 }
2959}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002960
2961/*
2962 * popup_list()
2963 */
2964 void
2965f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2966{
2967 win_T *wp;
2968 tabpage_T *tp;
2969
2970 if (rettv_list_alloc(rettv) != OK)
2971 return;
2972 FOR_ALL_POPUPWINS(wp)
2973 list_append_number(rettv->vval.v_list, wp->w_id);
2974 FOR_ALL_TABPAGES(tp)
2975 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2976 list_append_number(rettv->vval.v_list, wp->w_id);
2977}
2978
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002979/*
2980 * popup_locate({row}, {col})
2981 */
2982 void
2983f_popup_locate(typval_T *argvars, typval_T *rettv)
2984{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002985 int row;
2986 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002987 win_T *wp;
2988
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002989 if (in_vim9script()
2990 && (check_for_number_arg(argvars, 0) == FAIL
2991 || check_for_number_arg(argvars, 1) == FAIL))
2992 return;
2993
2994 row = tv_get_number(&argvars[0]) - 1;
2995 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002996 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002997 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002998 rettv->vval.v_number = wp->w_id;
2999}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003000
3001/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003002 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3003 */
3004 static void
3005get_padding_border(dict_T *dict, int *array, char *name)
3006{
3007 list_T *list;
3008 int i;
3009
3010 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3011 return;
3012
3013 list = list_alloc();
3014 if (list != NULL)
3015 {
3016 dict_add_list(dict, name, list);
3017 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3018 for (i = 0; i < 4; ++i)
3019 list_append_number(list, array[i]);
3020 }
3021}
3022
3023/*
3024 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3025 */
3026 static void
3027get_borderhighlight(dict_T *dict, win_T *wp)
3028{
3029 list_T *list;
3030 int i;
3031
3032 for (i = 0; i < 4; ++i)
3033 if (wp->w_border_highlight[i] != NULL)
3034 break;
3035 if (i == 4)
3036 return;
3037
3038 list = list_alloc();
3039 if (list != NULL)
3040 {
3041 dict_add_list(dict, "borderhighlight", list);
3042 for (i = 0; i < 4; ++i)
3043 list_append_string(list, wp->w_border_highlight[i], -1);
3044 }
3045}
3046
3047/*
3048 * For popup_getoptions(): add a "borderchars" entry to "dict".
3049 */
3050 static void
3051get_borderchars(dict_T *dict, win_T *wp)
3052{
3053 list_T *list;
3054 int i;
3055 char_u buf[NUMBUFLEN];
3056 int len;
3057
3058 for (i = 0; i < 8; ++i)
3059 if (wp->w_border_char[i] != 0)
3060 break;
3061 if (i == 8)
3062 return;
3063
3064 list = list_alloc();
3065 if (list != NULL)
3066 {
3067 dict_add_list(dict, "borderchars", list);
3068 for (i = 0; i < 8; ++i)
3069 {
3070 len = mb_char2bytes(wp->w_border_char[i], buf);
3071 list_append_string(list, buf, len);
3072 }
3073 }
3074}
3075
3076/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003077 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003078 */
3079 static void
3080get_moved_list(dict_T *dict, win_T *wp)
3081{
3082 list_T *list;
3083
3084 list = list_alloc();
3085 if (list != NULL)
3086 {
3087 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003088 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003089 list_append_number(list, wp->w_popup_mincol);
3090 list_append_number(list, wp->w_popup_maxcol);
3091 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003092 list = list_alloc();
3093 if (list != NULL)
3094 {
3095 dict_add_list(dict, "mousemoved", list);
3096 list_append_number(list, wp->w_popup_mouse_row);
3097 list_append_number(list, wp->w_popup_mouse_mincol);
3098 list_append_number(list, wp->w_popup_mouse_maxcol);
3099 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003100}
3101
3102/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003103 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003104 */
3105 void
3106f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3107{
3108 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003109 int id;
3110 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003111 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003112 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003113
3114 if (rettv_dict_alloc(rettv) == OK)
3115 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003116 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3117 return;
3118
3119 id = (int)tv_get_number(argvars);
3120 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003121 if (wp == NULL)
3122 return;
3123
3124 dict = rettv->vval.v_dict;
3125 dict_add_number(dict, "line", wp->w_wantline);
3126 dict_add_number(dict, "col", wp->w_wantcol);
3127 dict_add_number(dict, "minwidth", wp->w_minwidth);
3128 dict_add_number(dict, "minheight", wp->w_minheight);
3129 dict_add_number(dict, "maxheight", wp->w_maxheight);
3130 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003131 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003132 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003133 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003134 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003135 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003136 {
3137 proptype_T *pt = text_prop_type_by_id(
3138 wp->w_popup_prop_win->w_buffer,
3139 wp->w_popup_prop_type);
3140
3141 if (pt != NULL)
3142 dict_add_string(dict, "textprop", pt->pt_name);
3143 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3144 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3145 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003146 dict_add_string(dict, "title", wp->w_popup_title);
3147 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003148 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003149 dict_add_number(dict, "dragall",
3150 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003151 dict_add_number(dict, "mapping",
3152 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003153 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003154 dict_add_number(dict, "posinvert",
3155 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003156 dict_add_number(dict, "cursorline",
3157 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003158 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003159 if (wp->w_scrollbar_highlight != NULL)
3160 dict_add_string(dict, "scrollbarhighlight",
3161 wp->w_scrollbar_highlight);
3162 if (wp->w_thumb_highlight != NULL)
3163 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003164
Bram Moolenaara3fce622019-06-20 02:31:49 +02003165 // find the tabpage that holds this popup
3166 i = 1;
3167 FOR_ALL_TABPAGES(tp)
3168 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003169 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003170
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003171 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3172 if (twp->w_id == id)
3173 break;
3174 if (twp != NULL)
3175 break;
3176 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003177 }
3178 if (tp == NULL)
3179 i = -1; // must be global
3180 else if (tp == curtab)
3181 i = 0;
3182 dict_add_number(dict, "tabpage", i);
3183
Bram Moolenaarae943152019-06-16 22:54:14 +02003184 get_padding_border(dict, wp->w_popup_padding, "padding");
3185 get_padding_border(dict, wp->w_popup_border, "border");
3186 get_borderhighlight(dict, wp);
3187 get_borderchars(dict, wp);
3188 get_moved_list(dict, wp);
3189
3190 if (wp->w_filter_cb.cb_name != NULL)
3191 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3192 if (wp->w_close_cb.cb_name != NULL)
3193 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003194
K.Takataeeec2542021-06-02 13:28:16 +02003195 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003196 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3197 {
3198 dict_add_string(dict, "pos",
3199 (char_u *)poppos_entries[i].pp_name);
3200 break;
3201 }
3202
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003203 dict_add_string(dict, "close", (char_u *)(
3204 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3205 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3206
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003207# if defined(FEAT_TIMERS)
3208 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3209 ? (long)wp->w_popup_timer->tr_interval : 0L);
3210# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003211 }
3212}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003213
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003214# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003215/*
3216 * Return TRUE if the current window is running a terminal in a popup window.
3217 * Return FALSE when the job has ended.
3218 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003219 int
3220error_if_term_popup_window()
3221{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003222 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3223 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003224 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003225 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003226 return TRUE;
3227 }
3228 return FALSE;
3229}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003230# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003231
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003232/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003233 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003234 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003235 * Each calling function should use a different flag, see the list at
3236 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003237 */
3238 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003239popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003240{
3241 win_T *wp;
3242
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003243 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003244 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003245 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003246 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003247}
3248
3249/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003250 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003251 * Must have called popup_reset_handled() first.
3252 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3253 * popup with the highest zindex.
3254 */
3255 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003256find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003257{
3258 win_T *wp;
3259 win_T *found_wp;
3260 int found_zindex;
3261
3262 found_zindex = lowest ? INT_MAX : 0;
3263 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003264 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003265 if ((wp->w_popup_handled & handled_flag) == 0
3266 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003267 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003268 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003269 {
3270 found_zindex = wp->w_zindex;
3271 found_wp = wp;
3272 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003273 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003274 if ((wp->w_popup_handled & handled_flag) == 0
3275 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003276 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003277 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003278 {
3279 found_zindex = wp->w_zindex;
3280 found_wp = wp;
3281 }
3282
3283 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003284 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003285 return found_wp;
3286}
3287
3288/*
3289 * Invoke the filter callback for window "wp" with typed character "c".
3290 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003291 * Returns the return value of the filter or -1 for CTRL-C in the current
3292 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003293 * Careful: The filter may make "wp" invalid!
3294 */
3295 static int
3296invoke_popup_filter(win_T *wp, int c)
3297{
3298 int res;
3299 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003300 typval_T argv[3];
3301 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003302 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003303 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003304
Bram Moolenaara42d9452019-06-15 21:46:30 +02003305 // Emergency exit: CTRL-C closes the popup.
3306 if (c == Ctrl_C)
3307 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003308 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003309 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003310
3311 // Reset got_int to avoid the callback isn't called.
3312 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003313 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003314 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003315
3316 // If the popup is the current window it probably fails to close. Then
3317 // do not consume the key.
3318 if (was_curwin && wp == curwin)
3319 return -1;
3320 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003321 }
3322
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003323 argv[0].v_type = VAR_NUMBER;
3324 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3325
3326 // Convert the number to a string, so that the function can use:
3327 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003328 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003329 argv[1].v_type = VAR_STRING;
3330 argv[1].vval.v_string = vim_strsave(buf);
3331
3332 argv[2].v_type = VAR_UNKNOWN;
3333
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003334 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003335 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3336 {
3337 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003338 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003339 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003340 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003341 }
3342 else
3343 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003344 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3345 popup_highlight_curline(wp);
3346
Bram Moolenaar371806e2020-10-22 13:44:54 +02003347 // If an error message was given always return FALSE, so that keys are
3348 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003349 // If we get three errors in a row then close the popup. Decrement the
3350 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3351 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003352 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003353 {
3354 wp->w_filter_errors += 10;
3355 if (wp->w_filter_errors >= 30)
3356 popup_close_with_retval(wp, -1);
3357 res = FALSE;
3358 }
3359 else
3360 {
3361 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3362 --wp->w_filter_errors;
3363 res = tv_get_bool(&rettv);
3364 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003365 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003366
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003367 vim_free(argv[1].vval.v_string);
3368 clear_tv(&rettv);
3369 return res;
3370}
3371
3372/*
3373 * Called when "c" was typed: invoke popup filter callbacks.
3374 * Returns TRUE when the character was consumed,
3375 */
3376 int
3377popup_do_filter(int c)
3378{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003379 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003380 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003381 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003382 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003383 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003384 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003385
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003386#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003387 // Popup window with terminal always gets focus.
3388 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3389 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003390#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003391
Bram Moolenaar934470e2019-09-01 23:27:05 +02003392 if (recursive)
3393 return FALSE;
3394 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003395
Bram Moolenaarf6396232019-08-24 19:36:00 +02003396 if (c == K_LEFTMOUSE)
3397 {
3398 int row = mouse_row;
3399 int col = mouse_col;
3400
3401 wp = mouse_find_win(&row, &col, FIND_POPUP);
3402 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003403 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003404 }
3405
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003406 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003407 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003408 while (res == FALSE
3409 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003410 if (wp->w_filter_cb.cb_name != NULL
3411 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003412 res = invoke_popup_filter(wp, c);
3413
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003414 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003415 {
3416 int save_got_int = got_int;
3417
3418 // Reset got_int to avoid a function used in the statusline aborts.
3419 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003420 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003421 got_int |= save_got_int;
3422 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003423 recursive = FALSE;
3424 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003425
3426 // When interrupted return FALSE to avoid looping.
3427 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003428}
3429
Bram Moolenaar3397f742019-06-02 18:40:06 +02003430/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003431 * Return TRUE if there is a popup visible with a filter callback and the
3432 * "mapping" property off.
3433 */
3434 int
3435popup_no_mapping(void)
3436{
3437 int round;
3438 win_T *wp;
3439
3440 for (round = 1; round <= 2; ++round)
3441 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3442 wp != NULL; wp = wp->w_next)
3443 if (wp->w_filter_cb.cb_name != NULL
3444 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3445 return TRUE;
3446 return FALSE;
3447}
3448
3449/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003450 * Called when the cursor moved: check if any popup needs to be closed if the
3451 * cursor moved far enough.
3452 */
3453 void
3454popup_check_cursor_pos()
3455{
3456 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003457
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003458 popup_reset_handled(POPUP_HANDLED_3);
3459 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003460 if (wp->w_popup_curwin != NULL
3461 && (curwin != wp->w_popup_curwin
3462 || curwin->w_cursor.lnum != wp->w_popup_lnum
3463 || curwin->w_cursor.col < wp->w_popup_mincol
3464 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003465 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003466}
3467
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003468/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003469 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003470 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003471 static void
3472popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003473{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003474 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003475 char_u *cells;
3476 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003477
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003478 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3479 {
3480 vim_free(wp->w_popup_mask_cells);
3481 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003482 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003483 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003484 if (wp->w_popup_mask_cells != NULL
3485 && wp->w_popup_mask_height == height
3486 && wp->w_popup_mask_width == width)
3487 return; // cache is still valid
3488
3489 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003490 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003491 if (wp->w_popup_mask_cells == NULL)
3492 return;
3493 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003494
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003495 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003496 {
3497 int cols, cole;
3498 int lines, linee;
3499
3500 li = lio->li_tv.vval.v_list->lv_first;
3501 cols = tv_get_number(&li->li_tv);
3502 if (cols < 0)
3503 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003504 if (cols <= 0)
3505 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003506 li = li->li_next;
3507 cole = tv_get_number(&li->li_tv);
3508 if (cole < 0)
3509 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003510 if (cole > width)
3511 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003512 li = li->li_next;
3513 lines = tv_get_number(&li->li_tv);
3514 if (lines < 0)
3515 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003516 if (lines <= 0)
3517 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003518 li = li->li_next;
3519 linee = tv_get_number(&li->li_tv);
3520 if (linee < 0)
3521 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003522 if (linee > height)
3523 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003524
Bram Moolenaar4012d262020-12-29 11:57:46 +01003525 for (row = lines - 1; row < linee; ++row)
3526 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003527 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003528 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003529}
3530
3531/*
3532 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3533 * "col" and "line" are screen coordinates.
3534 */
3535 static int
3536popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3537{
3538 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3539 int line = screenline - wp->w_winrow;
3540
3541 return col >= 0 && col < width
3542 && line >= 0 && line < height
3543 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003544}
3545
3546/*
3547 * Set flags in popup_transparent[] for window "wp" to "val".
3548 */
3549 static void
3550update_popup_transparent(win_T *wp, int val)
3551{
3552 if (wp->w_popup_mask != NULL)
3553 {
3554 int width = popup_width(wp);
3555 int height = popup_height(wp);
3556 listitem_T *lio, *li;
3557 int cols, cole;
3558 int lines, linee;
3559 int col, line;
3560
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003561 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003562 {
3563 li = lio->li_tv.vval.v_list->lv_first;
3564 cols = tv_get_number(&li->li_tv);
3565 if (cols < 0)
3566 cols = width + cols + 1;
3567 li = li->li_next;
3568 cole = tv_get_number(&li->li_tv);
3569 if (cole < 0)
3570 cole = width + cole + 1;
3571 li = li->li_next;
3572 lines = tv_get_number(&li->li_tv);
3573 if (lines < 0)
3574 lines = height + lines + 1;
3575 li = li->li_next;
3576 linee = tv_get_number(&li->li_tv);
3577 if (linee < 0)
3578 linee = height + linee + 1;
3579
3580 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003581 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003582 if (cols < 0)
3583 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003584 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003585 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003586 if (lines < 0)
3587 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003588 for (line = lines; line < linee
3589 && line + wp->w_winrow < screen_Rows; ++line)
3590 for (col = cols; col < cole
3591 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003592 popup_transparent[(line + wp->w_winrow) * screen_Columns
3593 + col + wp->w_wincol] = val;
3594 }
3595 }
3596}
3597
3598/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003599 * Only called when popup window "wp" is hidden: If the window is positioned
3600 * next to a text property, and it is now visible, then unhide the popup.
3601 * We don't check if visible popups become hidden, that is done in
3602 * popup_adjust_position().
3603 * Return TRUE if the popup became unhidden.
3604 */
3605 static int
3606check_popup_unhidden(win_T *wp)
3607{
3608 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3609 {
3610 textprop_T prop;
3611 linenr_T lnum;
3612
Bram Moolenaar27724252022-05-08 15:00:04 +01003613 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3614 && find_visible_prop(wp->w_popup_prop_win,
3615 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003616 &prop, &lnum) == OK)
3617 {
3618 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003619 wp->w_popup_prop_topline = 0; // force repositioning
3620 return TRUE;
3621 }
3622 }
3623 return FALSE;
3624}
3625
3626/*
3627 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3628 * That is when the buffer in the popup was changed, or the popup is following
3629 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003630 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003631 */
3632 static int
3633popup_need_position_adjust(win_T *wp)
3634{
3635 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3636 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003637 if (win_valid(wp->w_popup_prop_win)
3638 && (wp->w_popup_prop_changedtick
3639 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3640 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3641 return TRUE;
3642
3643 // May need to adjust the width if the cursor moved.
3644 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003645}
3646
3647/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003648 * Update "popup_mask" if needed.
3649 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003650 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003651 * Also marks window lines for redrawing.
3652 */
3653 void
3654may_update_popup_mask(int type)
3655{
3656 win_T *wp;
3657 short *mask;
3658 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003659 int redraw_all_popups = FALSE;
3660 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003661
3662 // Need to recompute when switching tabs.
3663 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3664 // (such as the screen size) must have changed.
3665 if (popup_mask_tab != curtab || type >= NOT_VALID)
3666 {
3667 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003668 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003669 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003670
3671 // Check if any popup window buffer has changed and if any popup connected
3672 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003673 FOR_ALL_POPUPWINS(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;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003678 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003679 if (wp->w_popup_flags & POPF_HIDDEN)
3680 popup_mask_refresh |= check_popup_unhidden(wp);
3681 else if (popup_need_position_adjust(wp))
3682 popup_mask_refresh = TRUE;
3683
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003684 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003685 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003686
3687 // Need to update the mask, something has changed.
3688 popup_mask_refresh = FALSE;
3689 popup_mask_tab = curtab;
3690 popup_visible = FALSE;
3691
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003692 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003693 // If redrawing only what is needed, update "popup_mask_next" and then
3694 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003695 redrawing_all_win = TRUE;
3696 FOR_ALL_WINDOWS(wp)
3697 if (wp->w_redr_type < SOME_VALID)
3698 redrawing_all_win = FALSE;
3699 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003700 mask = popup_mask;
3701 else
3702 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003703 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003704
3705 // Find the window with the lowest zindex that hasn't been handled yet,
3706 // so that the window with a higher zindex overwrites the value in
3707 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003708 popup_reset_handled(POPUP_HANDLED_4);
3709 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003710 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003711 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003712 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003713
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003714 popup_visible = TRUE;
3715
Bram Moolenaar12034e22019-08-25 22:25:02 +02003716 // Recompute the position if the text changed. It may make the popup
3717 // hidden if it's attach to a text property that is no longer visible.
3718 if (redraw_all_popups || popup_need_position_adjust(wp))
3719 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003720 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003721 if (wp->w_popup_flags & POPF_HIDDEN)
3722 continue;
3723 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003724
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003725 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003726 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003727 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003728 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003729 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003730 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003731 col < wp->w_wincol + width - wp->w_popup_leftoff
3732 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003733 if (wp->w_zindex < POPUPMENU_ZINDEX
3734 && pum_visible()
3735 && pum_under_menu(line, col, FALSE))
3736 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3737 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003738 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003739 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003740 }
3741
3742 // Only check which lines are to be updated if not already
3743 // updating all lines.
3744 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003745 {
3746 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3747 win_T *prev_wp = NULL;
3748
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003749 for (line = 0; line < screen_Rows; ++line)
3750 {
3751 int col_done = 0;
3752
3753 for (col = 0; col < screen_Columns; ++col)
3754 {
3755 int off = line * screen_Columns + col;
3756
3757 if (popup_mask[off] != popup_mask_next[off])
3758 {
3759 popup_mask[off] = popup_mask_next[off];
3760
3761 if (line >= cmdline_row)
3762 {
3763 // the command line needs to be cleared if text below
3764 // the popup is now visible.
3765 if (!msg_scrolled && popup_mask_next[off] == 0)
3766 clear_cmdline = TRUE;
3767 }
3768 else if (col >= col_done)
3769 {
3770 linenr_T lnum;
3771 int line_cp = line;
3772 int col_cp = col;
3773
3774 // The screen position "line" / "col" needs to be
3775 // redrawn. Figure out what window that is and update
3776 // w_redraw_top and w_redr_bot. Only needs to be done
3777 // once for each window line.
3778 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3779 if (wp != NULL)
3780 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003781#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003782 // A terminal window needs to be redrawn.
3783 if (bt_terminal(wp->w_buffer))
3784 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003785 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003786#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003787 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003788 if (wp != prev_wp)
3789 {
3790 vim_memset(plines_cache, 0,
3791 sizeof(int) * Rows);
3792 prev_wp = wp;
3793 }
3794
3795 if (line_cp >= wp->w_height)
3796 // In (or below) status line
3797 wp->w_redr_status = TRUE;
3798 else
3799 {
3800 // compute the position in the buffer line
3801 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003802 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003803 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003804 redrawWinline(wp, lnum);
3805 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003806 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003807
3808 // This line is going to be redrawn, no need to
3809 // check until the right side of the window.
3810 col_done = wp->w_wincol + wp->w_width - 1;
3811 }
3812 }
3813 }
3814 }
3815 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003816
3817 vim_free(plines_cache);
3818 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003819
3820 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003821}
3822
3823/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003824 * If the current window is a popup and something relevant changed, recompute
3825 * the position and size.
3826 */
3827 void
3828may_update_popup_position(void)
3829{
3830 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3831 popup_adjust_position(curwin);
3832}
3833
3834/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003835 * Return a string of "len" spaces in IObuff.
3836 */
3837 static char_u *
3838get_spaces(int len)
3839{
3840 vim_memset(IObuff, ' ', (size_t)len);
3841 IObuff[len] = NUL;
3842 return IObuff;
3843}
3844
3845/*
3846 * Update popup windows. They are drawn on top of normal windows.
3847 * "win_update" is called for each popup window, lowest zindex first.
3848 */
3849 void
3850update_popups(void (*win_update)(win_T *wp))
3851{
3852 win_T *wp;
3853 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003854 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003855 int total_width;
3856 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003857 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003858 int popup_attr;
3859 int border_attr[4];
3860 int border_char[8];
3861 char_u buf[MB_MAXBYTES];
3862 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003863 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003864 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003865 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003866 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003867 int sb_thumb_top = 0;
3868 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003869 int attr_scroll = 0;
3870 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003871
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003872 // hide the cursor until redrawing is done.
3873 cursor_off();
3874
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003875 // Find the window with the lowest zindex that hasn't been updated yet,
3876 // so that the window with a higher zindex is drawn later, thus goes on
3877 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003878 popup_reset_handled(POPUP_HANDLED_5);
3879 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003880 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003881 int title_len = 0;
3882 int title_wincol;
3883
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003884 // This drawing uses the zindex of the popup window, so that it's on
3885 // top of the text but doesn't draw when another popup with higher
3886 // zindex is on top of the character.
3887 screen_zindex = wp->w_zindex;
3888
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003889 // Set flags in popup_transparent[] for masked cells.
3890 update_popup_transparent(wp, 1);
3891
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003892 // adjust w_winrow and w_wincol for border and padding, since
3893 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003894 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003895 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3896 - wp->w_popup_leftoff;
3897 if (wp->w_wincol + left_extra < 0)
3898 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003899 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003900 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003901
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003902 // Draw the popup text, unless it's off screen.
3903 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003904 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003905 // May need to update the "cursorline" highlighting, which may also
3906 // change "topline"
3907 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3908 popup_highlight_curline(wp);
3909
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003910 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003911
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003912 // move the cursor into the visible lines, otherwise executing
3913 // commands with win_execute() may cause the text to jump.
3914 if (wp->w_cursor.lnum < wp->w_topline)
3915 wp->w_cursor.lnum = wp->w_topline;
3916 else if (wp->w_cursor.lnum >= wp->w_botline)
3917 wp->w_cursor.lnum = wp->w_botline - 1;
3918 }
3919
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003920 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003921 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003922
3923 // Add offset for border and padding if not done already.
3924 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3925 {
3926 wp->w_wcol += left_extra;
3927 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3928 }
3929 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003930 {
3931 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003932 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003933 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003934
Bram Moolenaareabddc42022-04-02 15:32:16 +01003935 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003936 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003937 popup_attr = get_wcr_attr(wp);
3938
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003939 if (wp->w_winrow + total_height > cmdline_row)
3940 wp->w_popup_flags |= POPF_ON_CMDLINE;
3941 else
3942 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3943
3944
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003945 // We can only use these line drawing characters when 'encoding' is
3946 // "utf-8" and 'ambiwidth' is "single".
3947 if (enc_utf8 && *p_ambw == 's')
3948 {
3949 border_char[0] = border_char[2] = 0x2550;
3950 border_char[1] = border_char[3] = 0x2551;
3951 border_char[4] = 0x2554;
3952 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003953 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3954 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003955 border_char[7] = 0x255a;
3956 }
3957 else
3958 {
3959 border_char[0] = border_char[2] = '-';
3960 border_char[1] = border_char[3] = '|';
3961 for (i = 4; i < 8; ++i)
3962 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003963 if (wp->w_popup_flags & POPF_RESIZE)
3964 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003965 }
3966 for (i = 0; i < 8; ++i)
3967 if (wp->w_border_char[i] != 0)
3968 border_char[i] = wp->w_border_char[i];
3969
3970 for (i = 0; i < 4; ++i)
3971 {
3972 border_attr[i] = popup_attr;
3973 if (wp->w_border_highlight[i] != NULL)
3974 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3975 }
3976
Bram Moolenaard91467f2020-11-21 12:42:09 +01003977 // Title goes on top of border or padding.
3978 title_wincol = wp->w_wincol + 1;
3979 if (wp->w_popup_title != NULL)
3980 {
rbtnnc6119412021-08-07 13:08:45 +02003981 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003982
Ralf Schandlbc869872021-05-28 14:12:14 +02003983 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003984 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02003985 {
3986 int title_byte_len = (int)STRLEN(wp->w_popup_title);
3987 char_u *title_text = alloc(title_byte_len + 1);
3988
3989 if (title_text != NULL)
3990 {
3991 trunc_string(wp->w_popup_title, title_text,
3992 total_width - 2, title_byte_len + 1);
3993 screen_puts(title_text, wp->w_winrow, title_wincol,
3994 wp->w_popup_border[0] > 0
3995 ? border_attr[0] : popup_attr);
3996 vim_free(title_text);
3997 }
3998
Bram Moolenaard91467f2020-11-21 12:42:09 +01003999 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004000 }
4001 else
4002 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4003 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004004 }
4005
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004006 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004007 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004008 if (wp->w_popup_border[0] > 0)
4009 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004010 // top border; do not draw over the title
4011 if (title_len > 0)
4012 {
4013 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4014 wincol < 0 ? 0 : wincol, title_wincol,
4015 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004016 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004017 border_char[0], border_attr[0]);
4018 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4019 title_wincol + title_len, wincol + total_width,
4020 border_char[0], border_char[0], border_attr[0]);
4021 }
4022 else
4023 {
4024 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4025 wincol < 0 ? 0 : wincol, wincol + total_width,
4026 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4027 ? border_char[4] : border_char[0],
4028 border_char[0], border_attr[0]);
4029 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004030 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004031 {
4032 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4033 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004034 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004035 }
4036 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004037 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4038 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004039
Bram Moolenaard529ba52019-07-02 23:13:53 +02004040 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4041 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004042 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004043 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004044 - wp->w_has_scrollbar;
4045 if (padcol < 0)
4046 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004047 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004048 padcol = 0;
4049 }
4050 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004051 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004052 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004053 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004054 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004055 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004056 // top padding and no border; do not draw over the title
4057 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004058 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004059 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004060 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004061 row += 1;
4062 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004063 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004064 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004065 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004066 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004067
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004068 // Compute scrollbar thumb position and size.
4069 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004070 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004071 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4072 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004073
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004074 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4075 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004076 if (wp->w_topline > 1 && sb_thumb_height == height)
4077 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004078 if (sb_thumb_height == 0)
4079 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004080 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004081 // it just fits, avoid divide by zero
4082 sb_thumb_top = 0;
4083 else
4084 sb_thumb_top = (wp->w_topline - 1
4085 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004086 * (wp->w_height - sb_thumb_height)
4087 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004088 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4089 sb_thumb_top = 1; // show it's scrolled
4090
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004091 if (wp->w_scrollbar_highlight != NULL)
4092 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4093 else
4094 attr_scroll = highlight_attr[HLF_PSB];
4095 if (wp->w_thumb_highlight != NULL)
4096 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4097 else
4098 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004099 }
4100
4101 for (i = wp->w_popup_border[0];
4102 i < total_height - wp->w_popup_border[2]; ++i)
4103 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004104 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004105 // left and right padding only needed next to the body
4106 int do_padding =
4107 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4108 && i < total_height - wp->w_popup_border[2]
4109 - wp->w_popup_padding[2];
4110
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004111 row = wp->w_winrow + i;
4112
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004113 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004114 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004115 {
4116 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004117 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004118 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004119 if (do_padding && wp->w_popup_padding[3] > 0)
4120 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004121 int col = wincol + wp->w_popup_border[3];
4122
Bram Moolenaard529ba52019-07-02 23:13:53 +02004123 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004124 pad_left = wp->w_popup_padding[3];
4125 if (col < 0)
4126 {
4127 pad_left += col;
4128 col = 0;
4129 }
4130 if (pad_left > 0)
4131 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4132 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004133 // scrollbar
4134 if (wp->w_has_scrollbar)
4135 {
4136 int line = i - top_off;
4137 int scroll_col = wp->w_wincol + total_width - 1
4138 - wp->w_popup_border[1];
4139
4140 if (line >= 0 && line < wp->w_height)
4141 screen_putchar(' ', row, scroll_col,
4142 line >= sb_thumb_top
4143 && line < sb_thumb_top + sb_thumb_height
4144 ? attr_thumb : attr_scroll);
4145 else
4146 screen_putchar(' ', row, scroll_col, popup_attr);
4147 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004148 // right border
4149 if (wp->w_popup_border[1] > 0)
4150 {
4151 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004152 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004153 }
4154 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004155 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004156 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004157 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004158 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4159 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004160 }
4161
4162 if (wp->w_popup_padding[2] > 0)
4163 {
4164 // bottom padding
4165 row = wp->w_winrow + wp->w_popup_border[0]
4166 + wp->w_popup_padding[0] + wp->w_height;
4167 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004168 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004169 }
4170
4171 if (wp->w_popup_border[2] > 0)
4172 {
4173 // bottom border
4174 row = wp->w_winrow + total_height - 1;
4175 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004176 wincol < 0 ? 0 : wincol,
4177 wincol + total_width,
4178 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004179 ? border_char[7] : border_char[2],
4180 border_char[2], border_attr[2]);
4181 if (wp->w_popup_border[1] > 0)
4182 {
4183 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004184 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004185 }
4186 }
4187
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004188 if (wp->w_popup_close == POPCLOSE_BUTTON)
4189 {
4190 // close button goes on top of anything at the top-right corner
4191 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004192 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004193 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4194 }
4195
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004196 update_popup_transparent(wp, 0);
4197
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004198 // Back to the normal zindex.
4199 screen_zindex = 0;
4200 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004201
4202#if defined(FEAT_SEARCH_EXTRA)
4203 // In case win_update() called start_search_hl().
4204 end_search_hl();
4205#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004206}
4207
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004208/*
4209 * Mark references in callbacks of one popup window.
4210 */
4211 static int
4212set_ref_in_one_popup(win_T *wp, int copyID)
4213{
4214 int abort = FALSE;
4215 typval_T tv;
4216
4217 if (wp->w_close_cb.cb_partial != NULL)
4218 {
4219 tv.v_type = VAR_PARTIAL;
4220 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4221 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4222 }
4223 if (wp->w_filter_cb.cb_partial != NULL)
4224 {
4225 tv.v_type = VAR_PARTIAL;
4226 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4227 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4228 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004229 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004230 return abort;
4231}
4232
4233/*
4234 * Set reference in callbacks of popup windows.
4235 */
4236 int
4237set_ref_in_popups(int copyID)
4238{
4239 int abort = FALSE;
4240 win_T *wp;
4241 tabpage_T *tp;
4242
4243 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4244 abort = abort || set_ref_in_one_popup(wp, copyID);
4245
4246 FOR_ALL_TABPAGES(tp)
4247 {
4248 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4249 abort = abort || set_ref_in_one_popup(wp, copyID);
4250 if (abort)
4251 break;
4252 }
4253 return abort;
4254}
Bram Moolenaar79648732019-07-18 21:43:07 +02004255
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004256 int
4257popup_is_popup(win_T *wp)
4258{
4259 return wp->w_popup_flags != 0;
4260}
4261
4262#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004263/*
4264 * Find an existing popup used as the preview window, in the current tab page.
4265 * Return NULL if not found.
4266 */
4267 win_T *
4268popup_find_preview_window(void)
4269{
4270 win_T *wp;
4271
4272 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004273 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004274 if (wp->w_p_pvw)
4275 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004276 return NULL;
4277}
4278
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004279/*
4280 * Find an existing popup used as the info window, in the current tab page.
4281 * Return NULL if not found.
4282 */
4283 win_T *
4284popup_find_info_window(void)
4285{
4286 win_T *wp;
4287
4288 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004289 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004290 if (wp->w_popup_flags & POPF_INFO)
4291 return wp;
4292 return NULL;
4293}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004294#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004295
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004296 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004297f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4298{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004299#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004300 win_T *wp = popup_find_info_window();
4301
4302 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004303#else
4304 rettv->vval.v_number = 0;
4305#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004306}
4307
4308 void
4309f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004310{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004311#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004312 win_T *wp = popup_find_preview_window();
4313
4314 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004315#else
4316 rettv->vval.v_number = 0;
4317#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004318}
4319
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004320#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004321/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004322 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004323 * NOTE: this makes the popup the current window, so that the file can be
4324 * edited. However, it must not remain to be the current window, the caller
4325 * must make sure of that.
4326 */
4327 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004328popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004329{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004330 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004331
4332 if (wp == NULL)
4333 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004334 if (info)
4335 wp->w_popup_flags |= POPF_INFO;
4336 else
4337 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004338
4339 // Set the width to a reasonable value, so that w_topline can be computed.
4340 if (wp->w_minwidth > 0)
4341 wp->w_width = wp->w_minwidth;
4342 else if (wp->w_maxwidth > 0)
4343 wp->w_width = wp->w_maxwidth;
4344 else
4345 wp->w_width = curwin->w_width;
4346
4347 // Will switch to another buffer soon, dummy one can be wiped.
4348 wp->w_buffer->b_locked = FALSE;
4349
4350 win_enter(wp, FALSE);
4351 return OK;
4352}
4353
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004354/*
4355 * Close any preview popup.
4356 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004357 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004358popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004359{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004360 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004361
4362 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004363 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004364}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004365
4366/*
4367 * Hide the info popup.
4368 */
4369 void
4370popup_hide_info(void)
4371{
4372 win_T *wp = popup_find_info_window();
4373
4374 if (wp != NULL)
4375 popup_hide(wp);
4376}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004377
4378/*
4379 * Close any info popup.
4380 */
4381 void
4382popup_close_info(void)
4383{
4384 win_T *wp = popup_find_info_window();
4385
4386 if (wp != NULL)
4387 popup_close_with_retval(wp, -1);
4388}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004389#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004390
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004391/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004392 * Close any popup for a text property associated with "win".
4393 * Return TRUE if a popup was closed.
4394 */
4395 int
4396popup_win_closed(win_T *win)
4397{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004398 int round;
4399 win_T *wp;
4400 win_T *next;
4401 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004402
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004403 for (round = 1; round <= 2; ++round)
4404 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4405 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004406 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004407 next = wp->w_next;
4408 if (wp->w_popup_prop_win == win)
4409 {
4410 popup_close_with_retval(wp, -1);
4411 ret = TRUE;
4412 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004413 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004414 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004415}
4416
4417/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004418 * Set the title of the popup window to the file name.
4419 */
4420 void
4421popup_set_title(win_T *wp)
4422{
4423 if (wp->w_buffer->b_fname != NULL)
4424 {
4425 char_u dirname[MAXPATHL];
4426 size_t len;
4427
4428 mch_dirname(dirname, MAXPATHL);
4429 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4430
4431 vim_free(wp->w_popup_title);
4432 len = STRLEN(wp->w_buffer->b_fname) + 3;
4433 wp->w_popup_title = alloc((int)len);
4434 if (wp->w_popup_title != NULL)
4435 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4436 wp->w_buffer->b_fname);
4437 redraw_win_later(wp, VALID);
4438 }
4439}
4440
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004441# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004442/*
4443 * If there is a preview window, update the title.
4444 * Used after changing directory.
4445 */
4446 void
4447popup_update_preview_title(void)
4448{
4449 win_T *wp = popup_find_preview_window();
4450
4451 if (wp != NULL)
4452 popup_set_title(wp);
4453}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004454# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004455
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004456#endif // FEAT_PROP_POPUP