blob: 55f98f8a6f94778a552634c326b659bda811fbf5 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020064 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020065 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000089 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020090 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100100 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200101 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
103 ++i, li = li->li_next)
104 {
105 nr = (int)tv_get_number(&li->li_tv);
106 if (nr >= 0)
107 array[i] = nr > max_val ? max_val : nr;
108 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100109 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110 }
111 }
112}
113
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200114/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200115 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200116 */
117 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200118set_moved_values(win_T *wp)
119{
120 wp->w_popup_curwin = curwin;
121 wp->w_popup_lnum = curwin->w_cursor.lnum;
122 wp->w_popup_mincol = curwin->w_cursor.col;
123 wp->w_popup_maxcol = curwin->w_cursor.col;
124}
125
126/*
127 * Used when popup options contain "moved" with "word" or "WORD".
128 */
129 static void
130set_moved_columns(win_T *wp, int flags)
131{
132 char_u *ptr;
133 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
134
135 if (len > 0)
136 {
137 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
138 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
139 }
140}
141
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200143 * Used when popup options contain "mousemoved": set default moved values.
144 */
145 static void
146set_mousemoved_values(win_T *wp)
147{
148 wp->w_popup_mouse_row = mouse_row;
149 wp->w_popup_mouse_mincol = mouse_col;
150 wp->w_popup_mouse_maxcol = mouse_col;
151}
152
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000153 static void
154update_popup_uses_mouse_move(void)
155{
156 popup_uses_mouse_move = FALSE;
157 if (popup_visible)
158 {
159 win_T *wp;
160
161 FOR_ALL_POPUPWINS(wp)
162 if (wp->w_popup_mouse_row != 0)
163 {
164 popup_uses_mouse_move = TRUE;
165 return;
166 }
167 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
168 if (wp->w_popup_mouse_row != 0)
169 {
170 popup_uses_mouse_move = TRUE;
171 return;
172 }
173 }
174}
175
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200176/*
177 * Used when popup options contain "moved" with "word" or "WORD".
178 */
179 static void
180set_mousemoved_columns(win_T *wp, int flags)
181{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200182 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200183 char_u *text;
184 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200185 pos_T pos;
186 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200187
188 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200189 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200190 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200191 // convert text column to mouse column
192 pos.col = col;
193 pos.coladd = 0;
194 getvcol(textwp, &pos, &mcol, NULL, NULL);
195 wp->w_popup_mouse_mincol = mcol;
196
Bram Moolenaar10727682019-07-12 13:59:20 +0200197 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200198 getvcol(textwp, &pos, NULL, NULL, &mcol);
199 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200200 vim_free(text);
201 }
202}
203
204/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200205 * Return TRUE if "row"/"col" is on the border of the popup.
206 * The values are relative to the top-left corner.
207 */
208 int
209popup_on_border(win_T *wp, int row, int col)
210{
211 return (row == 0 && wp->w_popup_border[0] > 0)
212 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
213 || (col == 0 && wp->w_popup_border[3] > 0)
214 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
215}
216
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200217/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200218 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
219 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200220 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200221 * Caller should check the left mouse button was clicked.
222 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200223 */
224 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200225popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200226{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200227 if (wp->w_popup_close == POPCLOSE_BUTTON
228 && row == 0 && col == popup_width(wp) - 1)
229 {
230 popup_close_for_mouse_click(wp);
231 return TRUE;
232 }
233 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200234}
235
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200236// Values set when dragging a popup window starts.
237static int drag_start_row;
238static int drag_start_col;
239static int drag_start_wantline;
240static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200242
243/*
244 * Mouse down on border of popup window: start dragging it.
245 * Uses mouse_col and mouse_row.
246 */
247 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200248popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200249{
250 drag_start_row = mouse_row;
251 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200252 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200253 drag_start_wantline = wp->w_winrow + 1;
254 else
255 drag_start_wantline = wp->w_wantline;
256 if (wp->w_wantcol == 0)
257 drag_start_wantcol = wp->w_wincol + 1;
258 else
259 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200260
261 // Stop centering the popup
262 if (wp->w_popup_pos == POPPOS_CENTER)
263 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200264
265 drag_on_resize_handle = wp->w_popup_border[1] > 0
266 && wp->w_popup_border[2] > 0
267 && row == popup_height(wp) - 1
268 && col == popup_width(wp) - 1;
269
270 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
271 {
272 if (wp->w_popup_pos == POPPOS_TOPRIGHT
273 || wp->w_popup_pos == POPPOS_BOTRIGHT)
274 wp->w_wantcol = wp->w_wincol + 1;
275 if (wp->w_popup_pos == POPPOS_BOTLEFT)
276 wp->w_wantline = wp->w_winrow + 1;
277 wp->w_popup_pos = POPPOS_TOPLEFT;
278 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200279}
280
281/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200282 * Mouse moved while dragging a popup window: adjust the window popup position
283 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284 */
285 void
286popup_drag(win_T *wp)
287{
288 // The popup may be closed before dragging stops.
289 if (!win_valid_popup(wp))
290 return;
291
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200292 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
293 {
294 int width_inc = mouse_col - drag_start_col;
295 int height_inc = mouse_row - drag_start_row;
296
297 if (width_inc != 0)
298 {
299 int width = wp->w_width + width_inc;
300
301 if (width < 1)
302 width = 1;
303 wp->w_minwidth = width;
304 wp->w_maxwidth = width;
305 drag_start_col = mouse_col;
306 }
307
308 if (height_inc != 0)
309 {
310 int height = wp->w_height + height_inc;
311
312 if (height < 1)
313 height = 1;
314 wp->w_minheight = height;
315 wp->w_maxheight = height;
316 drag_start_row = mouse_row;
317 }
318
319 popup_adjust_position(wp);
320 return;
321 }
322
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000323 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200324 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200325 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
326 if (wp->w_wantline < 1)
327 wp->w_wantline = 1;
328 if (wp->w_wantline > Rows)
329 wp->w_wantline = Rows;
330 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
331 if (wp->w_wantcol < 1)
332 wp->w_wantcol = 1;
333 if (wp->w_wantcol > Columns)
334 wp->w_wantcol = Columns;
335
336 popup_adjust_position(wp);
337}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200338
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200339/*
340 * Set w_firstline to match the current "wp->w_topline".
341 */
342 void
343popup_set_firstline(win_T *wp)
344{
345 int height = wp->w_height;
346
347 wp->w_firstline = wp->w_topline;
348 popup_adjust_position(wp);
349
350 // we don't want the popup to get smaller, decrement the first line
351 // until it doesn't
352 while (wp->w_firstline > 1 && wp->w_height < height)
353 {
354 --wp->w_firstline;
355 popup_adjust_position(wp);
356 }
357}
358
359/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200360 * Return TRUE if the position is in the popup window scrollbar.
361 */
362 int
363popup_is_in_scrollbar(win_T *wp, int row, int col)
364{
365 return wp->w_has_scrollbar
366 && row >= wp->w_popup_border[0]
367 && row < popup_height(wp) - wp->w_popup_border[2]
368 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
369}
370
371
372/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200373 * Handle a click in a popup window, if it is in the scrollbar.
374 */
375 void
376popup_handle_scrollbar_click(win_T *wp, int row, int col)
377{
378 int height = popup_height(wp);
379 int old_topline = wp->w_topline;
380
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200381 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200382 {
383 if (row >= height / 2)
384 {
385 // Click in lower half, scroll down.
386 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
387 ++wp->w_topline;
388 }
389 else if (wp->w_topline > 1)
390 // click on upper half, scroll up.
391 --wp->w_topline;
392 if (wp->w_topline != old_topline)
393 {
394 popup_set_firstline(wp);
395 redraw_win_later(wp, NOT_VALID);
396 }
397 }
398}
399
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200400#if defined(FEAT_TIMERS)
401 static void
402popup_add_timeout(win_T *wp, int time)
403{
404 char_u cbbuf[50];
405 char_u *ptr = cbbuf;
406 typval_T tv;
407
408 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200409 "(_) => popup_close(%d)", wp->w_id);
410 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200411 {
412 wp->w_popup_timer = create_timer(time, 0);
413 wp->w_popup_timer->tr_callback = get_callback(&tv);
414 clear_tv(&tv);
415 }
416}
417#endif
418
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100419 static poppos_T
420get_pos_entry(dict_T *d, int give_error)
421{
422 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
423 int nr;
424
425 if (str == NULL)
426 return POPPOS_NONE;
427
K.Takataeeec2542021-06-02 13:28:16 +0200428 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100429 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
430 return poppos_entries[nr].pp_val;
431
432 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000433 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100434 return POPPOS_NONE;
435}
436
Bram Moolenaar17627312019-06-02 19:53:44 +0200437/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200438 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200439 */
440 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200441apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200442{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200443 int nr;
444 char_u *str;
445 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200446
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200447 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200448 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200449 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200450 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200451 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200452 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200453 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200454 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200455
456 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200457 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200458 wp->w_wantline = nr;
459 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200460 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200461 wp->w_wantcol = nr;
462
Bram Moolenaar55881332020-08-18 13:04:15 +0200463
464 nr = dict_get_bool(d, (char_u *)"fixed", -1);
465 if (nr != -1)
466 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200467
Bram Moolenaar12034e22019-08-25 22:25:02 +0200468 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100469 poppos_T ppt = get_pos_entry(d, TRUE);
470
471 if (ppt != POPPOS_NONE)
472 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200473 }
474
475 str = dict_get_string(d, (char_u *)"textprop", FALSE);
476 if (str != NULL)
477 {
478 wp->w_popup_prop_type = 0;
479 if (*str != NUL)
480 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100481 wp->w_popup_prop_win = curwin;
482 di = dict_find(d, (char_u *)"textpropwin", -1);
483 if (di != NULL)
484 {
485 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100486 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100487 wp->w_popup_prop_win = curwin;
488 }
489
490 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200491 if (nr <= 0)
492 nr = find_prop_type_id(str, NULL);
493 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000494 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200495 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200496 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200497 }
498 }
499
500 di = dict_find(d, (char_u *)"textpropid", -1);
501 if (di != NULL)
502 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200503}
504
Bram Moolenaarb0992022020-01-30 14:55:42 +0100505/*
506 * Handle "moved" and "mousemoved" arguments.
507 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200508 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200509handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
510{
511 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
512 {
513 char_u *s = di->di_tv.vval.v_string;
514 int flags = 0;
515
516 if (STRCMP(s, "word") == 0)
517 flags = FIND_IDENT | FIND_STRING;
518 else if (STRCMP(s, "WORD") == 0)
519 flags = FIND_STRING;
520 else if (STRCMP(s, "expr") == 0)
521 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
522 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000523 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200524 if (flags != 0)
525 {
526 if (mousemoved)
527 set_mousemoved_columns(wp, flags);
528 else
529 set_moved_columns(wp, flags);
530 }
531 }
532 else if (di->di_tv.v_type == VAR_LIST
533 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200534 && (di->di_tv.vval.v_list->lv_len == 2
535 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200536 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200537 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100538 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200539 int mincol;
540 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200541
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200542 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100543 li = l->lv_first;
544 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200545 {
546 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
547
548 // Three numbers, might be from popup_getoptions().
549 if (mousemoved)
550 wp->w_popup_mouse_row = nr;
551 else
552 wp->w_popup_lnum = nr;
553 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100554 if (nr == 0)
555 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200556 }
557
558 mincol = tv_get_number(&li->li_tv);
559 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200560 if (mousemoved)
561 {
562 wp->w_popup_mouse_mincol = mincol;
563 wp->w_popup_mouse_maxcol = maxcol;
564 }
565 else
566 {
567 wp->w_popup_mincol = mincol;
568 wp->w_popup_maxcol = maxcol;
569 }
570 }
571 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000572 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200573}
574
575 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200576check_highlight(dict_T *dict, char *name, char_u **pval)
577{
578 dictitem_T *di;
579 char_u *str;
580
581 di = dict_find(dict, (char_u *)name, -1);
582 if (di != NULL)
583 {
584 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000585 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200586 else
587 {
588 str = tv_get_string(&di->di_tv);
589 if (*str != NUL)
590 *pval = vim_strsave(str);
591 }
592 }
593}
594
Bram Moolenaarae943152019-06-16 22:54:14 +0200595/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200596 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200597 */
598 static void
599popup_show_curline(win_T *wp)
600{
601 if (wp->w_cursor.lnum < wp->w_topline)
602 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200603 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200604 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200605 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200606 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200607 if (wp->w_topline < 1)
608 wp->w_topline = 1;
609 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
610 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200611 while (wp->w_topline < wp->w_cursor.lnum
612 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
613 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
614 > wp->w_height)
615 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200616 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200617
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200618 // Don't let "firstline" cause a scroll.
619 if (wp->w_firstline > 0)
620 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200621}
622
623/*
624 * Get the sign group name for window "wp".
625 * Returns a pointer to a static buffer, overwritten on the next call.
626 */
627 static char_u *
628popup_get_sign_name(win_T *wp)
629{
630 static char buf[30];
631
632 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
633 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200634}
635
636/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200637 * Highlight the line with the cursor.
638 * Also scrolls the text to put the cursor line in view.
639 */
640 static void
641popup_highlight_curline(win_T *wp)
642{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200643 int sign_id = 0;
644 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200645
Bram Moolenaar72570732019-11-30 14:21:53 +0100646 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200647
648 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
649 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200650 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200651
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200652 if (!sign_exists_by_name(sign_name))
653 {
654 char *linehl = "PopupSelected";
655
656 if (syn_name2id((char_u *)linehl) == 0)
657 linehl = "PmenuSel";
James McCoya80aad72021-12-22 19:45:28 +0000658 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200659 }
660
Bram Moolenaar72570732019-11-30 14:21:53 +0100661 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200662 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
663 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200664 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200665 else
666 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200667 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200668}
669
670/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200671 * Shared between popup_create() and f_popup_setoptions().
672 */
673 static void
674apply_general_options(win_T *wp, dict_T *dict)
675{
676 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 int nr;
678 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200679
Bram Moolenaarae943152019-06-16 22:54:14 +0200680 // TODO: flip
681
682 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200683 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200684 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200685 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200686 if (wp->w_firstline < 0)
687 wp->w_firstline = -1;
688 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200689
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200690 nr = dict_get_bool(dict, (char_u *)"scrollbar", -1);
691 if (nr != -1)
692 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200693
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200694 str = dict_get_string(dict, (char_u *)"title", FALSE);
695 if (str != NULL)
696 {
697 vim_free(wp->w_popup_title);
698 wp->w_popup_title = vim_strsave(str);
699 }
700
Bram Moolenaar55881332020-08-18 13:04:15 +0200701 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
702 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200703 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200704
Bram Moolenaar55881332020-08-18 13:04:15 +0200705 nr = dict_get_bool(dict, (char_u *)"drag", -1);
706 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200707 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200708 if (nr)
709 wp->w_popup_flags |= POPF_DRAG;
710 else
711 wp->w_popup_flags &= ~POPF_DRAG;
712 }
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000713 nr = dict_get_bool(dict, (char_u *)"dragall", -1);
714 if (nr != -1)
715 {
716 if (nr)
717 wp->w_popup_flags |= POPF_DRAGALL;
718 else
719 wp->w_popup_flags &= ~POPF_DRAGALL;
720 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200721
Bram Moolenaar55881332020-08-18 13:04:15 +0200722 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
723 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100724 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100725 if (nr)
726 wp->w_popup_flags |= POPF_POSINVERT;
727 else
728 wp->w_popup_flags &= ~POPF_POSINVERT;
729 }
730
Bram Moolenaar55881332020-08-18 13:04:15 +0200731 nr = dict_get_bool(dict, (char_u *)"resize", -1);
732 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200733 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200734 if (nr)
735 wp->w_popup_flags |= POPF_RESIZE;
736 else
737 wp->w_popup_flags &= ~POPF_RESIZE;
738 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200739
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200740 di = dict_find(dict, (char_u *)"close", -1);
741 if (di != NULL)
742 {
743 int ok = TRUE;
744
745 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
746 {
747 char_u *s = di->di_tv.vval.v_string;
748
749 if (STRCMP(s, "none") == 0)
750 wp->w_popup_close = POPCLOSE_NONE;
751 else if (STRCMP(s, "button") == 0)
752 wp->w_popup_close = POPCLOSE_BUTTON;
753 else if (STRCMP(s, "click") == 0)
754 wp->w_popup_close = POPCLOSE_CLICK;
755 else
756 ok = FALSE;
757 }
758 else
759 ok = FALSE;
760 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000761 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200762 }
763
Bram Moolenaarae943152019-06-16 22:54:14 +0200764 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
765 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000766 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200767 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
768 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000769#ifdef FEAT_TERMINAL
770 term_update_wincolor(wp);
771#endif
772 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200773
Bram Moolenaarae943152019-06-16 22:54:14 +0200774 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
775 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200776
Bram Moolenaar790498b2019-06-01 22:15:29 +0200777 di = dict_find(dict, (char_u *)"borderhighlight", -1);
778 if (di != NULL)
779 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200780 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000781 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200782 else
783 {
784 list_T *list = di->di_tv.vval.v_list;
785 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200786 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200787
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200788 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200789 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
790 ++i, li = li->li_next)
791 {
792 str = tv_get_string(&li->li_tv);
793 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100794 {
795 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200796 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100797 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200798 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200799 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
800 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100801 {
802 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200803 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200804 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100805 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200806 }
807 }
808
Bram Moolenaar790498b2019-06-01 22:15:29 +0200809 di = dict_find(dict, (char_u *)"borderchars", -1);
810 if (di != NULL)
811 {
812 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000813 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200814 else
815 {
816 list_T *list = di->di_tv.vval.v_list;
817 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200818 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200819
820 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100821 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200822 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200823 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
824 ++i, li = li->li_next)
825 {
826 str = tv_get_string(&li->li_tv);
827 if (*str != NUL)
828 wp->w_border_char[i] = mb_ptr2char(str);
829 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200830 if (list->lv_len == 1)
831 for (i = 1; i < 8; ++i)
832 wp->w_border_char[i] = wp->w_border_char[0];
833 if (list->lv_len == 2)
834 {
835 for (i = 4; i < 8; ++i)
836 wp->w_border_char[i] = wp->w_border_char[1];
837 for (i = 1; i < 4; ++i)
838 wp->w_border_char[i] = wp->w_border_char[0];
839 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200840 }
841 }
842 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200843
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200844 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
845 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
846
Bram Moolenaarae943152019-06-16 22:54:14 +0200847 di = dict_find(dict, (char_u *)"zindex", -1);
848 if (di != NULL)
849 {
850 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
851 if (wp->w_zindex < 1)
852 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
853 if (wp->w_zindex > 32000)
854 wp->w_zindex = 32000;
855 }
856
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200857 di = dict_find(dict, (char_u *)"mask", -1);
858 if (di != NULL)
859 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200860 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200861
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200862 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200863 {
864 listitem_T *li;
865
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200866 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200867 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200868 {
869 if (li->li_tv.v_type != VAR_LIST
870 || li->li_tv.vval.v_list == NULL
871 || li->li_tv.vval.v_list->lv_len != 4)
872 {
873 ok = FALSE;
874 break;
875 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100876 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200877 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200878 }
879 }
880 if (ok)
881 {
882 wp->w_popup_mask = di->di_tv.vval.v_list;
883 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200884 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200885 }
886 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000887 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200888 }
889
Bram Moolenaarae943152019-06-16 22:54:14 +0200890#if defined(FEAT_TIMERS)
891 // Add timer to close the popup after some time.
892 nr = dict_get_number(dict, (char_u *)"time");
893 if (nr > 0)
894 popup_add_timeout(wp, nr);
895#endif
896
Bram Moolenaar3397f742019-06-02 18:40:06 +0200897 di = dict_find(dict, (char_u *)"moved", -1);
898 if (di != NULL)
899 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200900 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200901 handle_moved_argument(wp, di, FALSE);
902 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200903
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200904 di = dict_find(dict, (char_u *)"mousemoved", -1);
905 if (di != NULL)
906 {
907 set_mousemoved_values(wp);
908 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200909 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200910
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100911 nr = dict_get_bool(dict, (char_u *)"cursorline", -1);
912 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200913 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100914 if (nr != 0)
915 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200916 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100917 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200918 }
919
Bram Moolenaarae943152019-06-16 22:54:14 +0200920 di = dict_find(dict, (char_u *)"filter", -1);
921 if (di != NULL)
922 {
923 callback_T callback = get_callback(&di->di_tv);
924
925 if (callback.cb_name != NULL)
926 {
927 free_callback(&wp->w_filter_cb);
928 set_callback(&wp->w_filter_cb, &callback);
929 }
930 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200931 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
932 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200933 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200934 if (nr)
935 wp->w_popup_flags |= POPF_MAPPING;
936 else
937 wp->w_popup_flags &= ~POPF_MAPPING;
938 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200939
Bram Moolenaar581ba392019-09-03 22:08:33 +0200940 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
941 if (str != NULL)
942 {
943 if (STRCMP(str, "a") == 0)
944 wp->w_filter_mode = MODE_ALL;
945 else
946 wp->w_filter_mode = mode_str2flags(str);
947 }
948
Bram Moolenaarae943152019-06-16 22:54:14 +0200949 di = dict_find(dict, (char_u *)"callback", -1);
950 if (di != NULL)
951 {
952 callback_T callback = get_callback(&di->di_tv);
953
954 if (callback.cb_name != NULL)
955 {
956 free_callback(&wp->w_close_cb);
957 set_callback(&wp->w_close_cb, &callback);
958 }
959 }
960}
961
962/*
963 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200964 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200965 */
966 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200967apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200968{
969 int nr;
970
971 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200972
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200973 if (create)
974 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200975 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
976
Bram Moolenaarae943152019-06-16 22:54:14 +0200977 apply_general_options(wp, dict);
978
Bram Moolenaar55881332020-08-18 13:04:15 +0200979 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200980 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200981 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200982
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200983 // when "firstline" and "cursorline" are both set and the cursor would be
984 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200985 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
986 {
987 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
988 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200989 else if (wp->w_cursor.lnum < wp->w_firstline
990 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200991 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200992 wp->w_topline = wp->w_firstline;
993 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200994 }
995
Bram Moolenaar33796b32019-06-08 16:01:13 +0200996 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200997 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200998}
999
1000/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001001 * Add lines to the popup from a list of strings.
1002 */
1003 static void
1004add_popup_strings(buf_T *buf, list_T *l)
1005{
1006 listitem_T *li;
1007 linenr_T lnum = 0;
1008 char_u *p;
1009
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001010 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001011 if (li->li_tv.v_type == VAR_STRING)
1012 {
1013 p = li->li_tv.vval.v_string;
1014 ml_append_buf(buf, lnum++,
1015 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1016 }
1017}
1018
1019/*
1020 * Add lines to the popup from a list of dictionaries.
1021 */
1022 static void
1023add_popup_dicts(buf_T *buf, list_T *l)
1024{
1025 listitem_T *li;
1026 listitem_T *pli;
1027 linenr_T lnum = 0;
1028 char_u *p;
1029 dict_T *dict;
1030
1031 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001032 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001033 {
1034 if (li->li_tv.v_type != VAR_DICT)
1035 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001036 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001037 return;
1038 }
1039 dict = li->li_tv.vval.v_dict;
1040 p = dict == NULL ? NULL
1041 : dict_get_string(dict, (char_u *)"text", FALSE);
1042 ml_append_buf(buf, lnum++,
1043 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1044 }
1045
1046 // add the text properties
1047 lnum = 1;
1048 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1049 {
1050 dictitem_T *di;
1051 list_T *plist;
1052
1053 dict = li->li_tv.vval.v_dict;
1054 di = dict_find(dict, (char_u *)"props", -1);
1055 if (di != NULL)
1056 {
1057 if (di->di_tv.v_type != VAR_LIST)
1058 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001059 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001060 return;
1061 }
1062 plist = di->di_tv.vval.v_list;
1063 if (plist != NULL)
1064 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001065 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001066 {
1067 if (pli->li_tv.v_type != VAR_DICT)
1068 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001069 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001070 return;
1071 }
1072 dict = pli->li_tv.vval.v_dict;
1073 if (dict != NULL)
1074 {
1075 int col = dict_get_number(dict, (char_u *)"col");
1076
1077 prop_add_common( lnum, col, dict, buf, NULL);
1078 }
1079 }
1080 }
1081 }
1082 }
1083}
1084
1085/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001086 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1087 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001088 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001089popup_top_extra(win_T *wp)
1090{
1091 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1092
1093 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1094 return 1;
1095 return extra;
1096}
1097
1098/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001099 * Get the padding plus border at the left.
1100 */
1101 int
1102popup_left_extra(win_T *wp)
1103{
1104 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1105}
1106
1107/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001108 * Return the height of popup window "wp", including border and padding.
1109 */
1110 int
1111popup_height(win_T *wp)
1112{
1113 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001114 + popup_top_extra(wp)
1115 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001116}
1117
1118/*
1119 * Return the width of popup window "wp", including border, padding and
1120 * scrollbar.
1121 */
1122 int
1123popup_width(win_T *wp)
1124{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001125 // w_leftcol is how many columns of the core are left of the screen
1126 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001127 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001128 + popup_extra_width(wp)
1129 + wp->w_popup_rightoff;
1130}
1131
1132/*
1133 * Return the extra width of popup window "wp": border, padding and scrollbar.
1134 */
1135 int
1136popup_extra_width(win_T *wp)
1137{
1138 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1139 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1140 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001141}
1142
1143/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001144 * Adjust the position and size of the popup to fit on the screen.
1145 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001146 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001147popup_adjust_position(win_T *wp)
1148{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001149 linenr_T lnum;
1150 int wrapped = 0;
1151 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001152 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001153 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001154 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001155 int center_vert = FALSE;
1156 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001157 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001158 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001159 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1160 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1161 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1162 int extra_height = top_extra + bot_extra;
1163 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001164 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001165 int org_winrow = wp->w_winrow;
1166 int org_wincol = wp->w_wincol;
1167 int org_width = wp->w_width;
1168 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001169 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001170 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001171 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001172 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001173 int wantline = wp->w_wantline; // adjusted for textprop
1174 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001175 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001176 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001177
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001178 wp->w_winrow = 0;
1179 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001180 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001181 wp->w_popup_leftoff = 0;
1182 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001183
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001184 // May need to update the "cursorline" highlighting, which may also change
1185 // "topline"
1186 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1187 popup_highlight_curline(wp);
1188
Bram Moolenaar12034e22019-08-25 22:25:02 +02001189 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1190 {
1191 win_T *prop_win = wp->w_popup_prop_win;
1192 textprop_T prop;
1193 linenr_T prop_lnum;
1194 pos_T pos;
1195 int screen_row;
1196 int screen_scol;
1197 int screen_ccol;
1198 int screen_ecol;
1199
1200 // Popup window is positioned relative to a text property.
1201 if (find_visible_prop(prop_win,
1202 wp->w_popup_prop_type, wp->w_popup_prop_id,
1203 &prop, &prop_lnum) == FAIL)
1204 {
1205 // Text property is no longer visible, hide the popup.
1206 // Unhiding the popup is done in check_popup_unhidden().
1207 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1208 {
1209 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001210 if (win_valid(wp->w_popup_prop_win))
1211 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1212 }
1213 return;
1214 }
1215
1216 // Compute the desired position from the position of the text
1217 // property. Use "wantline" and "wantcol" as offsets.
1218 pos.lnum = prop_lnum;
1219 pos.col = prop.tp_col;
1220 if (wp->w_popup_pos == POPPOS_TOPLEFT
1221 || wp->w_popup_pos == POPPOS_BOTLEFT)
1222 pos.col += prop.tp_len - 1;
1223 textpos2screenpos(prop_win, &pos, &screen_row,
1224 &screen_scol, &screen_ccol, &screen_ecol);
1225
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001226 if (screen_scol == 0)
1227 {
1228 // position is off screen, make the width zero to hide it.
1229 wp->w_width = 0;
1230 return;
1231 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001232 if (wp->w_popup_pos == POPPOS_TOPLEFT
1233 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1234 // below the text
1235 wantline = screen_row + wantline + 1;
1236 else
1237 // above the text
1238 wantline = screen_row + wantline - 1;
1239 center_vert = FALSE;
1240 if (wp->w_popup_pos == POPPOS_TOPLEFT
1241 || wp->w_popup_pos == POPPOS_BOTLEFT)
1242 // right of the text
1243 wantcol = screen_ecol + wantcol;
1244 else
1245 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001246 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001247 use_wantcol = TRUE;
1248 }
1249 else
1250 {
1251 // If no line was specified default to vertical centering.
1252 if (wantline == 0)
1253 center_vert = TRUE;
1254 else if (wantline < 0)
1255 // If "wantline" is negative it actually means zero.
1256 wantline = 0;
1257 if (wantcol < 0)
1258 // If "wantcol" is negative it actually means zero.
1259 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001260 }
1261
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001262 if (wp->w_popup_pos == POPPOS_CENTER)
1263 {
1264 // center after computing the size
1265 center_vert = TRUE;
1266 center_hor = TRUE;
1267 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001268 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001269 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001270 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1271 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001272 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001273 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001274 if (wp->w_winrow >= Rows)
1275 wp->w_winrow = Rows - 1;
1276 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001277
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001278 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001279 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001280 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1281 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001282 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001283 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001284 // Need to see at least one character after the decoration.
1285 if (wp->w_wincol > Columns - left_extra - 1)
1286 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001287 }
1288 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001289
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001290 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001291 // When left aligned use the space available, but shift to the left when we
1292 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001293 maxspace = Columns - wp->w_wincol - left_extra;
1294 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001295 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001296 {
1297 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001298 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001299 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001300
1301 if (wp->w_p_nu || wp->w_p_rnu)
1302 margin_width = number_width(wp) + 1;
1303#ifdef FEAT_FOLDING
1304 margin_width += wp->w_p_fdc;
1305#endif
1306#ifdef FEAT_SIGNS
1307 if (signcolumn_on(wp))
1308 margin_width += 2;
1309#endif
1310 if (margin_width >= maxwidth)
1311 margin_width = maxwidth - 1;
1312
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001313 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001314 minheight = wp->w_minheight;
1315#ifdef FEAT_TERMINAL
1316 // A terminal popup initially does not have content, use a default minimal
1317 // width of 20 characters and height of 5 lines.
1318 if (wp->w_buffer->b_term != NULL)
1319 {
1320 if (minwidth == 0)
1321 minwidth = 20;
1322 if (minheight == 0)
1323 minheight = 5;
1324 }
1325#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001326
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001327 if (wp->w_maxheight > 0)
1328 maxheight = wp->w_maxheight;
1329
Bram Moolenaar8d241042019-06-12 23:40:01 +02001330 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001331 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001332 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001333 if (wp->w_topline < 1)
1334 wp->w_topline = 1;
1335 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001336 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1337
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001338 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001339 // Use a minimum width of one, so that something shows when there is no
1340 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001341 // When "firstline" is -1 then start with the last buffer line and go
1342 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001343 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001344 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001345 if (wp->w_firstline < 0)
1346 lnum = wp->w_buffer->b_ml.ml_line_count;
1347 else
1348 lnum = wp->w_topline;
1349 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001350 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001351 int len;
1352 int w_width = wp->w_width;
1353
1354 // Count Tabs for what they are worth and compute the length based on
1355 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001356 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001357 if (wp->w_width < maxwidth)
1358 wp->w_width = maxwidth;
1359 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001360 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001361 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001362
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001363 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001364 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001365 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001366 {
1367 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001368 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001369 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001370 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001371 }
1372 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001373 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001374 && allow_adjust_left
1375 && (wp->w_popup_pos == POPPOS_TOPLEFT
1376 || wp->w_popup_pos == POPPOS_BOTLEFT))
1377 {
1378 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001379 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001380
Bram Moolenaar51c31312019-06-15 22:27:23 +02001381 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001382 {
1383 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001384
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001385 len -= truncate_shift;
1386 shift_by -= truncate_shift;
1387 }
1388
1389 wp->w_wincol -= shift_by;
1390 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001391 wp->w_width = maxwidth;
1392 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001393 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001394 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001395 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001396 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1397 wp->w_width = wp->w_maxwidth;
1398 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001399
1400 if (wp->w_firstline < 0)
1401 --lnum;
1402 else
1403 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001404
1405 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001406 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001407 && (wp->w_firstline >= 0
1408 ? lnum - wp->w_topline
1409 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001410 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001411 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001412 }
1413
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001414 if (wp->w_firstline < 0)
1415 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1416
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001417 wp->w_has_scrollbar = wp->w_want_scrollbar
1418 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001419#ifdef FEAT_TERMINAL
1420 if (wp->w_buffer->b_term != NULL)
1421 // Terminal window never has a scrollbar, adjusts to window height.
1422 wp->w_has_scrollbar = FALSE;
1423#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001424 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001425 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001426 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001427 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001428 // make space for the scrollbar if needed, when lines wrap and when
1429 // applying minwidth
1430 if (maxwidth + right_extra >= maxspace
1431 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1432 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001433 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001434
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001435 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1436 {
1437 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1438
1439 if (minwidth < title_len)
1440 minwidth = title_len;
1441 }
1442
1443 if (minwidth > 0 && wp->w_width < minwidth)
1444 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001445 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001446 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001447 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001448 // some columns cut off on the right
1449 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001450 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001451 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001452 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001453 {
1454 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1455 if (wp->w_wincol < 0)
1456 wp->w_wincol = 0;
1457 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001458 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1459 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1460 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001461 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001462
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001463 // Right aligned: move to the right if needed.
1464 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001465 if (leftoff >= 0)
1466 wp->w_wincol = leftoff;
1467 else if (wp->w_popup_fixed)
1468 {
1469 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001470 // columns when displaying the window, minus left border and
1471 // padding.
1472 if (-leftoff > left_extra)
1473 wp->w_leftcol = -leftoff - left_extra;
1474 wp->w_width -= wp->w_leftcol;
1475 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001476 if (wp->w_width < 0)
1477 wp->w_width = 0;
1478 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001479 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001480
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001481 if (wp->w_p_wrap || (!wp->w_popup_fixed
1482 && (wp->w_popup_pos == POPPOS_TOPLEFT
1483 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001484 {
1485 int want_col = 0;
1486
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001487 // try to show the right border and any scrollbar
1488 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001489 if (want_col > 0 && wp->w_wincol > 0
1490 && wp->w_wincol + want_col >= Columns)
1491 {
1492 wp->w_wincol = Columns - want_col;
1493 if (wp->w_wincol < 0)
1494 wp->w_wincol = 0;
1495 }
1496 }
1497
Bram Moolenaar8d241042019-06-12 23:40:01 +02001498 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1499 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001500 if (minheight > 0 && wp->w_height < minheight)
1501 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001502 if (maxheight > 0 && wp->w_height > maxheight)
1503 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001504 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001505 if (wp->w_height > Rows - wp->w_winrow)
1506 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001507
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001508 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001509 {
1510 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1511 if (wp->w_winrow < 0)
1512 wp->w_winrow = 0;
1513 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001514 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001515 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001516 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001517 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001518 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001519 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001520 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1521 {
1522 // Bottom aligned but does not fit, and less space on the other
1523 // side or "posinvert" is off: reduce height.
1524 wp->w_winrow = 0;
1525 wp->w_height = wantline - extra_height;
1526 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001527 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001528 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001529 // Not enough space and more space on the other side: make top
1530 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001531 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001532 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001533 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001534 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001535 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1536 || wp->w_popup_pos == POPPOS_TOPLEFT)
1537 {
1538 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1539 && wantline * 2 > Rows
1540 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001541 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001542 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001543 // make bottom aligned and recompute the height
1544 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001545 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001546 if (wp->w_winrow < 0)
1547 {
1548 wp->w_height += wp->w_winrow;
1549 wp->w_winrow = 0;
1550 }
1551 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001552 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001553 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001554 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001555 adjust_height_for_top_aligned = TRUE;
1556 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001557 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001558
1559 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1560 && wp->w_winrow + wp->w_height + extra_height > Rows)
1561 {
1562 // Bottom of the popup goes below the last line, reduce the height and
1563 // add a scrollbar.
1564 wp->w_height = Rows - wp->w_winrow - extra_height;
1565#ifdef FEAT_TERMINAL
1566 if (wp->w_buffer->b_term == NULL)
1567#endif
1568 wp->w_has_scrollbar = TRUE;
1569 }
1570
1571 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001572 if (wp->w_winrow >= Rows)
1573 wp->w_winrow = Rows - 1;
1574 else if (wp->w_winrow < 0)
1575 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001576
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001577 if (wp->w_height != org_height)
1578 win_comp_scroll(wp);
1579
Bram Moolenaar17146962019-05-30 00:12:11 +02001580 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001581 if (win_valid(wp->w_popup_prop_win))
1582 {
1583 wp->w_popup_prop_changedtick =
1584 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1585 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1586 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001587
1588 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001589 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001590 if (org_winrow != wp->w_winrow
1591 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001592 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001593 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001594 || org_width != wp->w_width
1595 || org_height != wp->w_height)
1596 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001597 redraw_win_later(wp, NOT_VALID);
1598 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1599 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001600 popup_mask_refresh = TRUE;
1601 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001602}
1603
Bram Moolenaar17627312019-06-02 19:53:44 +02001604typedef enum
1605{
1606 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001607 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001608 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001609 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001610 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001611 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001612 TYPE_PREVIEW, // preview window
1613 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001614} create_type_T;
1615
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001616/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001617 * Make "buf" empty and set the contents to "text".
1618 * Used by popup_create() and popup_settext().
1619 */
1620 static void
1621popup_set_buffer_text(buf_T *buf, typval_T text)
1622{
1623 int lnum;
1624
1625 // Clear the buffer, then replace the lines.
1626 curbuf = buf;
1627 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001628 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001629 curbuf = curwin->w_buffer;
1630
1631 // Add text to the buffer.
1632 if (text.v_type == VAR_STRING)
1633 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001634 char_u *s = text.vval.v_string;
1635
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001636 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001637 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001638 }
1639 else
1640 {
1641 list_T *l = text.vval.v_list;
1642
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001643 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001644 {
1645 if (l->lv_first->li_tv.v_type == VAR_STRING)
1646 // list of strings
1647 add_popup_strings(buf, l);
1648 else
1649 // list of dictionaries
1650 add_popup_dicts(buf, l);
1651 }
1652 }
1653
1654 // delete the line that was in the empty buffer
1655 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001656 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001657 curbuf = curwin->w_buffer;
1658}
1659
1660/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001661 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1662 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001663 * Return FAIL if the parsing fails.
1664 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001665 static int
1666parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001667{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001668 char_u *p =
1669#ifdef FEAT_QUICKFIX
1670 !is_preview ? p_cpp :
1671#endif
1672 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001673
Bram Moolenaar258cef52019-08-21 17:29:29 +02001674 if (wp != NULL)
1675 wp->w_popup_flags &= ~POPF_INFO_MENU;
1676
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001677 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001678 {
1679 char_u *e, *dig;
1680 char_u *s = p;
1681 int x;
1682
1683 e = vim_strchr(p, ':');
1684 if (e == NULL || e[1] == NUL)
1685 return FAIL;
1686
1687 p = vim_strchr(e, ',');
1688 if (p == NULL)
1689 p = e + STRLEN(e);
1690 dig = e + 1;
1691 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001692
1693 if (STRNCMP(s, "height:", 7) == 0)
1694 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001695 if (dig != p)
1696 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001697 if (wp != NULL)
1698 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001699 if (is_preview)
1700 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001701 wp->w_maxheight = x;
1702 }
1703 }
1704 else if (STRNCMP(s, "width:", 6) == 0)
1705 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001706 if (dig != p)
1707 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001708 if (wp != NULL)
1709 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001710 if (is_preview)
1711 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001712 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001713 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001714 }
1715 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001716 else if (STRNCMP(s, "highlight:", 10) == 0)
1717 {
1718 if (wp != NULL)
1719 {
1720 int c = *p;
1721
1722 *p = NUL;
1723 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1724 s + 10, OPT_FREE|OPT_LOCAL, 0);
1725 *p = c;
1726 }
1727 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001728 else if (STRNCMP(s, "border:", 7) == 0)
1729 {
1730 char_u *arg = s + 7;
1731 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1732 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1733 int i;
1734
1735 if (!on && !off)
1736 return FAIL;
1737 if (wp != NULL)
1738 {
1739 for (i = 0; i < 4; ++i)
1740 wp->w_popup_border[i] = on ? 1 : 0;
1741 if (off)
1742 // only show the X for close when there is a border
1743 wp->w_popup_close = POPCLOSE_NONE;
1744 }
1745 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001746 else if (STRNCMP(s, "align:", 6) == 0)
1747 {
1748 char_u *arg = s + 6;
1749 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1750 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1751
1752 if (!menu && !item)
1753 return FAIL;
1754 if (wp != NULL && menu)
1755 wp->w_popup_flags |= POPF_INFO_MENU;
1756 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001757 else
1758 return FAIL;
1759 }
1760 return OK;
1761}
1762
1763/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001764 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1765 * is not NULL.
1766 * Return FAIL if the parsing fails.
1767 */
1768 int
1769parse_previewpopup(win_T *wp)
1770{
1771 return parse_popup_option(wp, TRUE);
1772}
1773
1774/*
1775 * Parse the 'completepopup' option and apply the values to window "wp" if it
1776 * is not NULL.
1777 * Return FAIL if the parsing fails.
1778 */
1779 int
1780parse_completepopup(win_T *wp)
1781{
1782 return parse_popup_option(wp, FALSE);
1783}
1784
1785/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001786 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001787 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001788 */
1789 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001790popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001791{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001792 poppos_T ppt = POPPOS_NONE;
1793
1794 if (d != NULL)
1795 ppt = get_pos_entry(d, FALSE);
1796
Bram Moolenaar79648732019-07-18 21:43:07 +02001797 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001798 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001799 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001800 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1801 }
1802 else
1803 {
1804 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1805 if (wp->w_wantline == 0) // cursor in first line
1806 {
1807 wp->w_wantline = 2;
1808 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1809 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1810 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001811 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001812
Bram Moolenaar79648732019-07-18 21:43:07 +02001813 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001814 if (wp->w_wantcol > Columns - width)
1815 {
1816 wp->w_wantcol = Columns - width;
1817 if (wp->w_wantcol < 1)
1818 wp->w_wantcol = 1;
1819 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001820
Bram Moolenaar79648732019-07-18 21:43:07 +02001821 popup_adjust_position(wp);
1822}
1823
1824/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001825 * Set w_wantline and w_wantcol for the a given screen position.
1826 * Caller must take care of running into the window border.
1827 */
1828 void
1829popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1830{
1831 wp->w_wantline = row;
1832 wp->w_wantcol = col;
1833 popup_adjust_position(wp);
1834}
1835
1836/*
1837 * Add a border and lef&right padding.
1838 */
1839 static void
1840add_border_left_right_padding(win_T *wp)
1841{
1842 int i;
1843
1844 for (i = 0; i < 4; ++i)
1845 {
1846 wp->w_popup_border[i] = 1;
1847 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1848 }
1849}
1850
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001851#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001852/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001853 * Return TRUE if there is any popup window with a terminal buffer.
1854 */
1855 static int
1856popup_terminal_exists(void)
1857{
1858 win_T *wp;
1859 tabpage_T *tp;
1860
1861 FOR_ALL_POPUPWINS(wp)
1862 if (wp->w_buffer->b_term != NULL)
1863 return TRUE;
1864 FOR_ALL_TABPAGES(tp)
1865 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1866 if (wp->w_buffer->b_term != NULL)
1867 return TRUE;
1868 return FALSE;
1869}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001870#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001871
1872/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001873 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001874 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001875 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001876 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001877 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001878 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001879popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001880{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001881 win_T *wp;
1882 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001883 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001884 int new_buffer;
1885 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001886 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001887 int nr;
1888 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001889
Bram Moolenaar79648732019-07-18 21:43:07 +02001890 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001891 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001892 if (in_vim9script()
1893 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1894 || check_for_dict_arg(argvars, 1) == FAIL))
1895 return NULL;
1896
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001897 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001898 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001899 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001900 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001901 if (buf == NULL)
1902 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001903 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001904 return NULL;
1905 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001906#ifdef FEAT_TERMINAL
1907 if (buf->b_term != NULL && popup_terminal_exists())
1908 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001909 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001910 return NULL;
1911 }
1912#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001913 }
1914 else if (!(argvars[0].v_type == VAR_STRING
1915 && argvars[0].vval.v_string != NULL)
1916 && !(argvars[0].v_type == VAR_LIST
1917 && argvars[0].vval.v_list != NULL))
1918 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001919 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001920 return NULL;
1921 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001922 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001923 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001924 emsg(_(e_dictionary_required));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001925 return NULL;
1926 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001927 d = argvars[1].vval.v_dict;
1928 }
1929
1930 if (d != NULL)
1931 {
1932 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1933 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1934 else if (type == TYPE_NOTIFICATION)
1935 tabnr = -1; // notifications are global by default
1936 else
1937 tabnr = 0;
1938 if (tabnr > 0)
1939 {
1940 tp = find_tabpage(tabnr);
1941 if (tp == NULL)
1942 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00001943 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02001944 return NULL;
1945 }
1946 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001947 }
1948
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001949 // Create the window and buffer.
1950 wp = win_alloc_popup_win();
1951 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001952 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001953 if (rettv != NULL)
1954 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001955 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001956 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001957
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001958 if (buf != NULL)
1959 {
1960 // use existing buffer
1961 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001962 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001963 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001964 buffer_ensure_loaded(buf);
1965 }
1966 else
1967 {
1968 // create a new buffer associated with the popup
1969 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001970 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001971 if (buf == NULL)
1972 return NULL;
1973 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001974
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001975 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001976
Bram Moolenaar46451042019-08-24 15:50:46 +02001977 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001978 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001979 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001980 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001981 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001982 buf->b_p_ul = -1; // no undo
1983 buf->b_p_swf = FALSE; // no swap file
1984 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001985 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001986
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001987 // Avoid that 'buftype' is reset when this buffer is entered.
1988 buf->b_p_initialized = TRUE;
1989 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001990 wp->w_p_wrap = TRUE; // 'wrap' is default on
1991 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001992
Bram Moolenaara3fce622019-06-20 02:31:49 +02001993 if (tp != NULL)
1994 {
1995 // popup on specified tab page
1996 wp->w_next = tp->tp_first_popupwin;
1997 tp->tp_first_popupwin = wp;
1998 }
1999 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002000 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002001 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002002 wp->w_next = curtab->tp_first_popupwin;
2003 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002004 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002005 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002006 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002007 win_T *prev = first_popupwin;
2008
2009 // Global popup: add at the end, so that it gets displayed on top of
2010 // older ones with the same zindex. Matters for notifications.
2011 if (first_popupwin == NULL)
2012 first_popupwin = wp;
2013 else
2014 {
2015 while (prev->w_next != NULL)
2016 prev = prev->w_next;
2017 prev->w_next = wp;
2018 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002019 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002020
Bram Moolenaar79648732019-07-18 21:43:07 +02002021 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002022 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002023
Bram Moolenaar79648732019-07-18 21:43:07 +02002024 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002025 {
2026 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002027 }
2028 if (type == TYPE_ATCURSOR)
2029 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002030 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002031 set_moved_values(wp);
2032 set_moved_columns(wp, FIND_STRING);
2033 }
2034
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002035 if (type == TYPE_BEVAL)
2036 {
2037 wp->w_popup_pos = POPPOS_BOTLEFT;
2038
2039 // by default use the mouse position
2040 wp->w_wantline = mouse_row;
2041 if (wp->w_wantline <= 0) // mouse on first line
2042 {
2043 wp->w_wantline = 2;
2044 wp->w_popup_pos = POPPOS_TOPLEFT;
2045 }
2046 wp->w_wantcol = mouse_col + 1;
2047 set_mousemoved_values(wp);
2048 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2049 }
2050
Bram Moolenaar33796b32019-06-08 16:01:13 +02002051 // set default values
2052 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002053 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002054
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002055 if (type == TYPE_NOTIFICATION)
2056 {
2057 win_T *twp, *nextwin;
2058 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002059
2060 // Try to not overlap with another global popup. Guess we need 3
2061 // more screen lines than buffer lines.
2062 wp->w_wantline = 1;
2063 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2064 {
2065 nextwin = twp->w_next;
2066 if (twp != wp
2067 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2068 && twp->w_winrow <= wp->w_wantline - 1 + height
2069 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2070 {
2071 // move to below this popup and restart the loop to check for
2072 // overlap with other popups
2073 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2074 nextwin = first_popupwin;
2075 }
2076 }
2077 if (wp->w_wantline + height > Rows)
2078 {
2079 // can't avoid overlap, put on top in the hope that message goes
2080 // away soon.
2081 wp->w_wantline = 1;
2082 }
2083
2084 wp->w_wantcol = 10;
2085 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002086 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002087 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002088 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002089 for (i = 0; i < 4; ++i)
2090 wp->w_popup_border[i] = 1;
2091 wp->w_popup_padding[1] = 1;
2092 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002093
2094 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002095 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002096 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2097 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002098 }
2099
Bram Moolenaara730e552019-06-16 19:05:31 +02002100 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002101 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002102 wp->w_popup_pos = POPPOS_CENTER;
2103 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002104 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002105 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002106 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002107 }
2108
Bram Moolenaara730e552019-06-16 19:05:31 +02002109 if (type == TYPE_MENU)
2110 {
2111 typval_T tv;
2112 callback_T callback;
2113
2114 tv.v_type = VAR_STRING;
2115 tv.vval.v_string = (char_u *)"popup_filter_menu";
2116 callback = get_callback(&tv);
2117 if (callback.cb_name != NULL)
2118 set_callback(&wp->w_filter_cb, &callback);
2119
2120 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002121 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002122 }
2123
Bram Moolenaar79648732019-07-18 21:43:07 +02002124 if (type == TYPE_PREVIEW)
2125 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002126 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002127 wp->w_popup_close = POPCLOSE_BUTTON;
2128 for (i = 0; i < 4; ++i)
2129 wp->w_popup_border[i] = 1;
2130 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002131 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002132 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002133# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002134 if (type == TYPE_INFO)
2135 {
2136 wp->w_popup_pos = POPPOS_TOPLEFT;
2137 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2138 wp->w_popup_close = POPCLOSE_BUTTON;
2139 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002140 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002141 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002142# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002143
Bram Moolenaarae943152019-06-16 22:54:14 +02002144 for (i = 0; i < 4; ++i)
2145 VIM_CLEAR(wp->w_border_highlight[i]);
2146 for (i = 0; i < 8; ++i)
2147 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002148 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002149 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002150 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002151
Bram Moolenaar79648732019-07-18 21:43:07 +02002152 if (d != NULL)
2153 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002154 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002155
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002156#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002157 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2158 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002159#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002160
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002161 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002162
2163 wp->w_vsep_width = 0;
2164
2165 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002166 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002167
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002168#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002169 // When running a terminal in the popup it becomes the current window.
2170 if (buf->b_term != NULL)
2171 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002172#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002173
Bram Moolenaara730e552019-06-16 19:05:31 +02002174 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002175}
2176
2177/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002178 * popup_clear()
2179 */
2180 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002181f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002182{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002183 int force = FALSE;
2184
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002185 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2186 return;
2187
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002188 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002189 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002190 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002191}
2192
2193/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002194 * popup_create({text}, {options})
2195 */
2196 void
2197f_popup_create(typval_T *argvars, typval_T *rettv)
2198{
Bram Moolenaar17627312019-06-02 19:53:44 +02002199 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002200}
2201
2202/*
2203 * popup_atcursor({text}, {options})
2204 */
2205 void
2206f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2207{
Bram Moolenaar17627312019-06-02 19:53:44 +02002208 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002209}
2210
2211/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002212 * popup_beval({text}, {options})
2213 */
2214 void
2215f_popup_beval(typval_T *argvars, typval_T *rettv)
2216{
2217 popup_create(argvars, rettv, TYPE_BEVAL);
2218}
2219
2220/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002221 * Invoke the close callback for window "wp" with value "result".
2222 * Careful: The callback may make "wp" invalid!
2223 */
2224 static void
2225invoke_popup_callback(win_T *wp, typval_T *result)
2226{
2227 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002228 typval_T argv[3];
2229
2230 argv[0].v_type = VAR_NUMBER;
2231 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2232
2233 if (result != NULL && result->v_type != VAR_UNKNOWN)
2234 copy_tv(result, &argv[1]);
2235 else
2236 {
2237 argv[1].v_type = VAR_NUMBER;
2238 argv[1].vval.v_number = 0;
2239 }
2240
2241 argv[2].v_type = VAR_UNKNOWN;
2242
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002243 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002244 if (result != NULL)
2245 clear_tv(&argv[1]);
2246 clear_tv(&rettv);
2247}
2248
2249/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002250 * Make "prevwin" the current window, unless it's equal to "wp".
2251 * Otherwise make "firstwin" the current window.
2252 */
2253 static void
2254back_to_prevwin(win_T *wp)
2255{
2256 if (win_valid(prevwin) && wp != prevwin)
2257 win_enter(prevwin, FALSE);
2258 else
2259 win_enter(firstwin, FALSE);
2260}
2261
2262/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002263 * Close popup "wp" and invoke any close callback for it.
2264 */
2265 static void
2266popup_close_and_callback(win_T *wp, typval_T *arg)
2267{
2268 int id = wp->w_id;
2269
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002270#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002271 if (wp == curwin && curbuf->b_term != NULL)
2272 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002273 win_T *owp;
2274
2275 // Closing popup window with a terminal: put focus back on the first
2276 // that works:
2277 // - another popup window with a terminal
2278 // - the previous window
2279 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002280 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002281 if (owp != curwin && owp->w_buffer->b_term != NULL)
2282 break;
2283 if (owp != NULL)
2284 win_enter(owp, FALSE);
2285 else
2286 {
2287 for (owp = curtab->tp_first_popupwin; owp != NULL;
2288 owp = owp->w_next)
2289 if (owp != curwin && owp->w_buffer->b_term != NULL)
2290 break;
2291 if (owp != NULL)
2292 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002293 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002294 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002295 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002296 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002297#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002298
Bram Moolenaar49540192019-12-11 19:34:54 +01002299 // Just in case a check higher up is missing.
2300 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002301 {
2302 // To avoid getting stuck when win_execute() does something that causes
2303 // an error, stop calling the filter callback.
2304 free_callback(&wp->w_filter_cb);
2305
Bram Moolenaar49540192019-12-11 19:34:54 +01002306 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002307 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002308
Bram Moolenaarcee52202020-03-11 14:19:58 +01002309 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002310 if (wp->w_close_cb.cb_name != NULL)
2311 // Careful: This may make "wp" invalid.
2312 invoke_popup_callback(wp, arg);
2313
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002314 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002315 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002316}
2317
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002318 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002319popup_close_with_retval(win_T *wp, int retval)
2320{
2321 typval_T res;
2322
2323 res.v_type = VAR_NUMBER;
2324 res.vval.v_number = retval;
2325 popup_close_and_callback(wp, &res);
2326}
2327
Bram Moolenaar3397f742019-06-02 18:40:06 +02002328/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002329 * Close popup "wp" because of a mouse click.
2330 */
2331 void
2332popup_close_for_mouse_click(win_T *wp)
2333{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002334 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002335}
2336
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002337 static void
2338check_mouse_moved(win_T *wp, win_T *mouse_wp)
2339{
2340 // Close the popup when all if these are true:
2341 // - the mouse is not on this popup
2342 // - "mousemoved" was used
2343 // - the mouse is no longer on the same screen row or the mouse column is
2344 // outside of the relevant text
2345 if (wp != mouse_wp
2346 && wp->w_popup_mouse_row != 0
2347 && (wp->w_popup_mouse_row != mouse_row
2348 || mouse_col < wp->w_popup_mouse_mincol
2349 || mouse_col > wp->w_popup_mouse_maxcol))
2350 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002351 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002352 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002353 }
2354}
2355
2356/*
2357 * Called when the mouse moved: may close a popup with "mousemoved".
2358 */
2359 void
2360popup_handle_mouse_moved(void)
2361{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002362 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002363 win_T *mouse_wp;
2364 int row = mouse_row;
2365 int col = mouse_col;
2366
2367 // find the window where the mouse is in
2368 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2369
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002370 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2371 {
2372 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002373 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002374 }
2375 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2376 {
2377 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002378 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002379 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002380}
2381
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002382/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002383 * In a filter: check if the typed key is a mouse event that is used for
2384 * dragging the popup.
2385 */
2386 static void
2387filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2388{
2389 int row = mouse_row;
2390 int col = mouse_col;
2391
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002392 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002393 && is_mouse_key(c)
2394 && (wp == popup_dragwin
2395 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2396 // do not consume the key, allow for dragging the popup
2397 rettv->vval.v_number = 0;
2398}
2399
Bram Moolenaara730e552019-06-16 19:05:31 +02002400/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002401 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002402 */
2403 void
2404f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2405{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002406 int id;
2407 win_T *wp;
2408 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002409 typval_T res;
2410 int c;
2411 linenr_T old_lnum;
2412
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002413 if (in_vim9script()
2414 && (check_for_number_arg(argvars, 0) == FAIL
2415 || check_for_string_arg(argvars, 1) == FAIL))
2416 return;
2417
2418 id = tv_get_number(&argvars[0]);
2419 wp = win_id2wp(id);
2420 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002421 // If the popup has been closed do not consume the key.
2422 if (wp == NULL)
2423 return;
2424
2425 c = *key;
2426 if (c == K_SPECIAL && key[1] != NUL)
2427 c = TO_SPECIAL(key[1], key[2]);
2428
2429 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002430 rettv->v_type = VAR_BOOL;
2431 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002432 res.v_type = VAR_NUMBER;
2433
2434 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002435 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2436 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002437 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002438 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002439 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2440 ++wp->w_cursor.lnum;
2441 if (old_lnum != wp->w_cursor.lnum)
2442 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002443 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002444 return;
2445 }
2446
2447 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2448 {
2449 // Cancelled, invoke callback with -1
2450 res.vval.v_number = -1;
2451 popup_close_and_callback(wp, &res);
2452 return;
2453 }
2454 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2455 {
2456 // Invoke callback with current index.
2457 res.vval.v_number = wp->w_cursor.lnum;
2458 popup_close_and_callback(wp, &res);
2459 return;
2460 }
2461
2462 filter_handle_drag(wp, c, rettv);
2463}
2464
2465/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002466 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002467 */
2468 void
2469f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2470{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002471 int id;
2472 win_T *wp;
2473 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002474 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002475 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002476
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002477 if (in_vim9script()
2478 && (check_for_number_arg(argvars, 0) == FAIL
2479 || check_for_string_arg(argvars, 1) == FAIL))
2480 return;
2481
2482 id = tv_get_number(&argvars[0]);
2483 wp = win_id2wp(id);
2484 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002485 // If the popup has been closed don't consume the key.
2486 if (wp == NULL)
2487 return;
2488
Bram Moolenaara730e552019-06-16 19:05:31 +02002489 c = *key;
2490 if (c == K_SPECIAL && key[1] != NUL)
2491 c = TO_SPECIAL(key[1], key[2]);
2492
Bram Moolenaara42d9452019-06-15 21:46:30 +02002493 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002494 rettv->v_type = VAR_BOOL;
2495 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002496
Bram Moolenaara730e552019-06-16 19:05:31 +02002497 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002498 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002499 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002500 res.vval.v_number = 0;
2501 else
2502 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002503 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002504 return;
2505 }
2506
2507 // Invoke callback
2508 res.v_type = VAR_NUMBER;
2509 popup_close_and_callback(wp, &res);
2510}
2511
2512/*
2513 * popup_dialog({text}, {options})
2514 */
2515 void
2516f_popup_dialog(typval_T *argvars, typval_T *rettv)
2517{
2518 popup_create(argvars, rettv, TYPE_DIALOG);
2519}
2520
2521/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002522 * popup_menu({text}, {options})
2523 */
2524 void
2525f_popup_menu(typval_T *argvars, typval_T *rettv)
2526{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002527 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002528}
2529
2530/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002531 * popup_notification({text}, {options})
2532 */
2533 void
2534f_popup_notification(typval_T *argvars, typval_T *rettv)
2535{
2536 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2537}
2538
2539/*
2540 * Find the popup window with window-ID "id".
2541 * If the popup window does not exist NULL is returned.
2542 * If the window is not a popup window, and error message is given.
2543 */
2544 static win_T *
2545find_popup_win(int id)
2546{
2547 win_T *wp = win_id2wp(id);
2548
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002549 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002550 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002551 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002552 return NULL;
2553 }
2554 return wp;
2555}
2556
2557/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002558 * popup_close({id})
2559 */
2560 void
2561f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2562{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002563 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002564 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002565
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002566 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2567 return;
2568
2569 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002570 if (
2571# ifdef FEAT_TERMINAL
2572 // if the popup contains a terminal it will become hidden
2573 curbuf->b_term == NULL &&
2574# endif
2575 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002576 return;
2577
2578 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002579 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002580 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002581}
2582
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002583 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002584popup_hide(win_T *wp)
2585{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002586#ifdef FEAT_TERMINAL
2587 if (error_if_term_popup_window())
2588 return;
2589#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002590 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2591 {
2592 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002593 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002594 redraw_all_later(NOT_VALID);
2595 popup_mask_refresh = TRUE;
2596 }
2597}
2598
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002599/*
2600 * popup_hide({id})
2601 */
2602 void
2603f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2604{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002605 int id;
2606 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002607
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002608 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2609 return;
2610
2611 id = (int)tv_get_number(argvars);
2612 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002613 if (wp != NULL)
2614 popup_hide(wp);
2615}
2616
2617 void
2618popup_show(win_T *wp)
2619{
2620 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002621 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002622 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002623 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002624 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002625 }
2626}
2627
2628/*
2629 * popup_show({id})
2630 */
2631 void
2632f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2633{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002634 int id;
2635 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002636
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002637 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2638 return;
2639
2640 id = (int)tv_get_number(argvars);
2641 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002642 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002643 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002644 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002645#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002646 if (wp->w_popup_flags & POPF_INFO)
2647 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002648#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002649 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002650}
2651
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002652/*
2653 * popup_settext({id}, {text})
2654 */
2655 void
2656f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2657{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002658 int id;
2659 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002660
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002661 if (in_vim9script()
2662 && (check_for_number_arg(argvars, 0) == FAIL
2663 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2664 return;
2665
2666 id = (int)tv_get_number(&argvars[0]);
2667 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002668 if (wp != NULL)
2669 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002670 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002671 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002672 else
2673 {
2674 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002675 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002676 popup_adjust_position(wp);
2677 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002678 }
2679}
2680
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002681 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002682popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002683{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002684 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002685 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002686 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002687 clear_cmdline = TRUE;
2688 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002689
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002690 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002691 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002692}
2693
Bram Moolenaar82106932020-03-13 14:34:38 +01002694 static void
2695error_for_popup_window(void)
2696{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002697 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002698}
2699
2700 int
2701error_if_popup_window(int also_with_term UNUSED)
2702{
2703 // win_execute() may set "curwin" to a popup window temporarily, but many
2704 // commands are disallowed then. When a terminal runs in the popup most
2705 // things are allowed. When a terminal is finished it can be closed.
2706 if (WIN_IS_POPUP(curwin)
2707# ifdef FEAT_TERMINAL
2708 && (also_with_term || curbuf->b_term == NULL)
2709 && !term_is_finished(curbuf)
2710# endif
2711 )
2712 {
2713 error_for_popup_window();
2714 return TRUE;
2715 }
2716 return FALSE;
2717}
2718
Bram Moolenaarec583842019-05-26 14:11:23 +02002719/*
2720 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002721 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002722 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002723 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002724 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002725popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002726{
2727 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002728 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002729 win_T *prev = NULL;
2730
Bram Moolenaarec583842019-05-26 14:11:23 +02002731 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002732 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002733 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002734 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002735 if (wp == curwin)
2736 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002737 if (!force)
2738 {
2739 error_for_popup_window();
2740 return FAIL;
2741 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002742 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002743 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002744 if (prev == NULL)
2745 first_popupwin = wp->w_next;
2746 else
2747 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002748 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002749 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002750 }
2751
Bram Moolenaarec583842019-05-26 14:11:23 +02002752 // go through tab-local popups
2753 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002754 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002755 return OK;
2756 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002757}
2758
2759/*
2760 * Close a popup window with Window-id "id" in tabpage "tp".
2761 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002762 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002763popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002764{
2765 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002766 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002767 win_T *prev = NULL;
2768
Bram Moolenaarec583842019-05-26 14:11:23 +02002769 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2770 if (wp->w_id == id)
2771 {
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 Moolenaarec583842019-05-26 14:11:23 +02002781 if (prev == NULL)
2782 *root = 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 Moolenaarec583842019-05-26 14:11:23 +02002787 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002788 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002789}
2790
2791 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002792close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002793{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002794 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002795 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002796 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002797 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002798 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002799 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002800 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002801 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002802}
2803
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002804/*
2805 * popup_move({id}, {options})
2806 */
2807 void
2808f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2809{
Bram Moolenaarae943152019-06-16 22:54:14 +02002810 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002811 int id;
2812 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002813
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002814 if (in_vim9script()
2815 && (check_for_number_arg(argvars, 0) == FAIL
2816 || check_for_dict_arg(argvars, 1) == FAIL))
2817 return;
2818
2819 id = (int)tv_get_number(argvars);
2820 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002821 if (wp == NULL)
2822 return; // invalid {id}
2823
2824 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2825 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002826 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002827 return;
2828 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002829 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002830
Bram Moolenaarae943152019-06-16 22:54:14 +02002831 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002832
2833 if (wp->w_winrow + wp->w_height >= cmdline_row)
2834 clear_cmdline = TRUE;
2835 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002836}
2837
Bram Moolenaarbc133542019-05-29 20:26:46 +02002838/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002839 * popup_setoptions({id}, {options})
2840 */
2841 void
2842f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2843{
2844 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002845 int id;
2846 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002847 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002848
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002849 if (in_vim9script()
2850 && (check_for_number_arg(argvars, 0) == FAIL
2851 || check_for_dict_arg(argvars, 1) == FAIL))
2852 return;
2853
2854 id = (int)tv_get_number(argvars);
2855 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002856 if (wp == NULL)
2857 return; // invalid {id}
2858
2859 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2860 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002861 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002862 return;
2863 }
2864 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002865 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002866
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002867 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002868
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002869 if (old_firstline != wp->w_firstline)
2870 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002871 popup_adjust_position(wp);
2872}
2873
2874/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002875 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002876 */
2877 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002878f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002879{
2880 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002881 int id;
2882 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002883 int top_extra;
2884 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002885
2886 if (rettv_dict_alloc(rettv) == OK)
2887 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002888 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2889 return;
2890
2891 id = (int)tv_get_number(argvars);
2892 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002893 if (wp == NULL)
2894 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002895 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002896 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2897
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002898 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002899 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002900 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002901
Bram Moolenaarbc133542019-05-29 20:26:46 +02002902 dict_add_number(dict, "line", wp->w_winrow + 1);
2903 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002904 dict_add_number(dict, "width", wp->w_width + left_extra
2905 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2906 dict_add_number(dict, "height", wp->w_height + top_extra
2907 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002908
2909 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2910 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2911 dict_add_number(dict, "core_width", wp->w_width);
2912 dict_add_number(dict, "core_height", wp->w_height);
2913
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002914 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002915 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002916 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002917 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002918 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002919
2920 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002921 }
2922}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002923
2924/*
2925 * popup_list()
2926 */
2927 void
2928f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2929{
2930 win_T *wp;
2931 tabpage_T *tp;
2932
2933 if (rettv_list_alloc(rettv) != OK)
2934 return;
2935 FOR_ALL_POPUPWINS(wp)
2936 list_append_number(rettv->vval.v_list, wp->w_id);
2937 FOR_ALL_TABPAGES(tp)
2938 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2939 list_append_number(rettv->vval.v_list, wp->w_id);
2940}
2941
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002942/*
2943 * popup_locate({row}, {col})
2944 */
2945 void
2946f_popup_locate(typval_T *argvars, typval_T *rettv)
2947{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002948 int row;
2949 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002950 win_T *wp;
2951
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002952 if (in_vim9script()
2953 && (check_for_number_arg(argvars, 0) == FAIL
2954 || check_for_number_arg(argvars, 1) == FAIL))
2955 return;
2956
2957 row = tv_get_number(&argvars[0]) - 1;
2958 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002959 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002960 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002961 rettv->vval.v_number = wp->w_id;
2962}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002963
2964/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002965 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2966 */
2967 static void
2968get_padding_border(dict_T *dict, int *array, char *name)
2969{
2970 list_T *list;
2971 int i;
2972
2973 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2974 return;
2975
2976 list = list_alloc();
2977 if (list != NULL)
2978 {
2979 dict_add_list(dict, name, list);
2980 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2981 for (i = 0; i < 4; ++i)
2982 list_append_number(list, array[i]);
2983 }
2984}
2985
2986/*
2987 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2988 */
2989 static void
2990get_borderhighlight(dict_T *dict, win_T *wp)
2991{
2992 list_T *list;
2993 int i;
2994
2995 for (i = 0; i < 4; ++i)
2996 if (wp->w_border_highlight[i] != NULL)
2997 break;
2998 if (i == 4)
2999 return;
3000
3001 list = list_alloc();
3002 if (list != NULL)
3003 {
3004 dict_add_list(dict, "borderhighlight", list);
3005 for (i = 0; i < 4; ++i)
3006 list_append_string(list, wp->w_border_highlight[i], -1);
3007 }
3008}
3009
3010/*
3011 * For popup_getoptions(): add a "borderchars" entry to "dict".
3012 */
3013 static void
3014get_borderchars(dict_T *dict, win_T *wp)
3015{
3016 list_T *list;
3017 int i;
3018 char_u buf[NUMBUFLEN];
3019 int len;
3020
3021 for (i = 0; i < 8; ++i)
3022 if (wp->w_border_char[i] != 0)
3023 break;
3024 if (i == 8)
3025 return;
3026
3027 list = list_alloc();
3028 if (list != NULL)
3029 {
3030 dict_add_list(dict, "borderchars", list);
3031 for (i = 0; i < 8; ++i)
3032 {
3033 len = mb_char2bytes(wp->w_border_char[i], buf);
3034 list_append_string(list, buf, len);
3035 }
3036 }
3037}
3038
3039/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003040 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003041 */
3042 static void
3043get_moved_list(dict_T *dict, win_T *wp)
3044{
3045 list_T *list;
3046
3047 list = list_alloc();
3048 if (list != NULL)
3049 {
3050 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003051 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003052 list_append_number(list, wp->w_popup_mincol);
3053 list_append_number(list, wp->w_popup_maxcol);
3054 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003055 list = list_alloc();
3056 if (list != NULL)
3057 {
3058 dict_add_list(dict, "mousemoved", list);
3059 list_append_number(list, wp->w_popup_mouse_row);
3060 list_append_number(list, wp->w_popup_mouse_mincol);
3061 list_append_number(list, wp->w_popup_mouse_maxcol);
3062 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003063}
3064
3065/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003066 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003067 */
3068 void
3069f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3070{
3071 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003072 int id;
3073 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003074 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003075 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003076
3077 if (rettv_dict_alloc(rettv) == OK)
3078 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003079 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3080 return;
3081
3082 id = (int)tv_get_number(argvars);
3083 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003084 if (wp == NULL)
3085 return;
3086
3087 dict = rettv->vval.v_dict;
3088 dict_add_number(dict, "line", wp->w_wantline);
3089 dict_add_number(dict, "col", wp->w_wantcol);
3090 dict_add_number(dict, "minwidth", wp->w_minwidth);
3091 dict_add_number(dict, "minheight", wp->w_minheight);
3092 dict_add_number(dict, "maxheight", wp->w_maxheight);
3093 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003094 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003095 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003096 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003097 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003098 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003099 {
3100 proptype_T *pt = text_prop_type_by_id(
3101 wp->w_popup_prop_win->w_buffer,
3102 wp->w_popup_prop_type);
3103
3104 if (pt != NULL)
3105 dict_add_string(dict, "textprop", pt->pt_name);
3106 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3107 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3108 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003109 dict_add_string(dict, "title", wp->w_popup_title);
3110 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003111 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003112 dict_add_number(dict, "dragall",
3113 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003114 dict_add_number(dict, "mapping",
3115 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003116 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003117 dict_add_number(dict, "posinvert",
3118 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003119 dict_add_number(dict, "cursorline",
3120 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003121 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003122 if (wp->w_scrollbar_highlight != NULL)
3123 dict_add_string(dict, "scrollbarhighlight",
3124 wp->w_scrollbar_highlight);
3125 if (wp->w_thumb_highlight != NULL)
3126 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003127
Bram Moolenaara3fce622019-06-20 02:31:49 +02003128 // find the tabpage that holds this popup
3129 i = 1;
3130 FOR_ALL_TABPAGES(tp)
3131 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003132 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003133
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003134 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3135 if (twp->w_id == id)
3136 break;
3137 if (twp != NULL)
3138 break;
3139 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003140 }
3141 if (tp == NULL)
3142 i = -1; // must be global
3143 else if (tp == curtab)
3144 i = 0;
3145 dict_add_number(dict, "tabpage", i);
3146
Bram Moolenaarae943152019-06-16 22:54:14 +02003147 get_padding_border(dict, wp->w_popup_padding, "padding");
3148 get_padding_border(dict, wp->w_popup_border, "border");
3149 get_borderhighlight(dict, wp);
3150 get_borderchars(dict, wp);
3151 get_moved_list(dict, wp);
3152
3153 if (wp->w_filter_cb.cb_name != NULL)
3154 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3155 if (wp->w_close_cb.cb_name != NULL)
3156 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003157
K.Takataeeec2542021-06-02 13:28:16 +02003158 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003159 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3160 {
3161 dict_add_string(dict, "pos",
3162 (char_u *)poppos_entries[i].pp_name);
3163 break;
3164 }
3165
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003166 dict_add_string(dict, "close", (char_u *)(
3167 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3168 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3169
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003170# if defined(FEAT_TIMERS)
3171 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3172 ? (long)wp->w_popup_timer->tr_interval : 0L);
3173# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003174 }
3175}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003176
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003177# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003178/*
3179 * Return TRUE if the current window is running a terminal in a popup window.
3180 * Return FALSE when the job has ended.
3181 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003182 int
3183error_if_term_popup_window()
3184{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003185 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3186 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003187 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003188 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003189 return TRUE;
3190 }
3191 return FALSE;
3192}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003193# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003194
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003195/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003196 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003197 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003198 * Each calling function should use a different flag, see the list at
3199 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003200 */
3201 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003202popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003203{
3204 win_T *wp;
3205
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003206 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003207 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003208 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003209 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003210}
3211
3212/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003213 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003214 * Must have called popup_reset_handled() first.
3215 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3216 * popup with the highest zindex.
3217 */
3218 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003219find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003220{
3221 win_T *wp;
3222 win_T *found_wp;
3223 int found_zindex;
3224
3225 found_zindex = lowest ? INT_MAX : 0;
3226 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003227 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003228 if ((wp->w_popup_handled & handled_flag) == 0
3229 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003230 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003231 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003232 {
3233 found_zindex = wp->w_zindex;
3234 found_wp = wp;
3235 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003236 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003237 if ((wp->w_popup_handled & handled_flag) == 0
3238 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003239 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003240 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003241 {
3242 found_zindex = wp->w_zindex;
3243 found_wp = wp;
3244 }
3245
3246 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003247 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003248 return found_wp;
3249}
3250
3251/*
3252 * Invoke the filter callback for window "wp" with typed character "c".
3253 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003254 * Returns the return value of the filter or -1 for CTRL-C in the current
3255 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003256 * Careful: The filter may make "wp" invalid!
3257 */
3258 static int
3259invoke_popup_filter(win_T *wp, int c)
3260{
3261 int res;
3262 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003263 typval_T argv[3];
3264 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003265 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003266 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003267
Bram Moolenaara42d9452019-06-15 21:46:30 +02003268 // Emergency exit: CTRL-C closes the popup.
3269 if (c == Ctrl_C)
3270 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003271 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003272 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003273
3274 // Reset got_int to avoid the callback isn't called.
3275 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003276 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003277 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003278
3279 // If the popup is the current window it probably fails to close. Then
3280 // do not consume the key.
3281 if (was_curwin && wp == curwin)
3282 return -1;
3283 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003284 }
3285
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003286 argv[0].v_type = VAR_NUMBER;
3287 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3288
3289 // Convert the number to a string, so that the function can use:
3290 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003291 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003292 argv[1].v_type = VAR_STRING;
3293 argv[1].vval.v_string = vim_strsave(buf);
3294
3295 argv[2].v_type = VAR_UNKNOWN;
3296
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003297 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003298 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3299 {
3300 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003301 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003302 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003303 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003304 }
3305 else
3306 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003307 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3308 popup_highlight_curline(wp);
3309
Bram Moolenaar371806e2020-10-22 13:44:54 +02003310 // If an error message was given always return FALSE, so that keys are
3311 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003312 // If we get three errors in a row then close the popup. Decrement the
3313 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3314 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003315 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003316 {
3317 wp->w_filter_errors += 10;
3318 if (wp->w_filter_errors >= 30)
3319 popup_close_with_retval(wp, -1);
3320 res = FALSE;
3321 }
3322 else
3323 {
3324 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3325 --wp->w_filter_errors;
3326 res = tv_get_bool(&rettv);
3327 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003328 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003329
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003330 vim_free(argv[1].vval.v_string);
3331 clear_tv(&rettv);
3332 return res;
3333}
3334
3335/*
3336 * Called when "c" was typed: invoke popup filter callbacks.
3337 * Returns TRUE when the character was consumed,
3338 */
3339 int
3340popup_do_filter(int c)
3341{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003342 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003343 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003344 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003345 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003346 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003347 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003348
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003349#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003350 // Popup window with terminal always gets focus.
3351 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3352 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003353#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003354
Bram Moolenaar934470e2019-09-01 23:27:05 +02003355 if (recursive)
3356 return FALSE;
3357 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003358
Bram Moolenaarf6396232019-08-24 19:36:00 +02003359 if (c == K_LEFTMOUSE)
3360 {
3361 int row = mouse_row;
3362 int col = mouse_col;
3363
3364 wp = mouse_find_win(&row, &col, FIND_POPUP);
3365 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003366 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003367 }
3368
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003369 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003370 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003371 while (res == FALSE
3372 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003373 if (wp->w_filter_cb.cb_name != NULL
3374 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003375 res = invoke_popup_filter(wp, c);
3376
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003377 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003378 {
3379 int save_got_int = got_int;
3380
3381 // Reset got_int to avoid a function used in the statusline aborts.
3382 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003383 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003384 got_int |= save_got_int;
3385 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003386 recursive = FALSE;
3387 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003388
3389 // When interrupted return FALSE to avoid looping.
3390 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003391}
3392
Bram Moolenaar3397f742019-06-02 18:40:06 +02003393/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003394 * Return TRUE if there is a popup visible with a filter callback and the
3395 * "mapping" property off.
3396 */
3397 int
3398popup_no_mapping(void)
3399{
3400 int round;
3401 win_T *wp;
3402
3403 for (round = 1; round <= 2; ++round)
3404 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3405 wp != NULL; wp = wp->w_next)
3406 if (wp->w_filter_cb.cb_name != NULL
3407 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3408 return TRUE;
3409 return FALSE;
3410}
3411
3412/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003413 * Called when the cursor moved: check if any popup needs to be closed if the
3414 * cursor moved far enough.
3415 */
3416 void
3417popup_check_cursor_pos()
3418{
3419 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003420
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003421 popup_reset_handled(POPUP_HANDLED_3);
3422 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003423 if (wp->w_popup_curwin != NULL
3424 && (curwin != wp->w_popup_curwin
3425 || curwin->w_cursor.lnum != wp->w_popup_lnum
3426 || curwin->w_cursor.col < wp->w_popup_mincol
3427 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003428 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003429}
3430
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003431/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003432 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003433 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003434 static void
3435popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003436{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003437 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003438 char_u *cells;
3439 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003440
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003441 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3442 {
3443 vim_free(wp->w_popup_mask_cells);
3444 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003445 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003446 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003447 if (wp->w_popup_mask_cells != NULL
3448 && wp->w_popup_mask_height == height
3449 && wp->w_popup_mask_width == width)
3450 return; // cache is still valid
3451
3452 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003453 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003454 if (wp->w_popup_mask_cells == NULL)
3455 return;
3456 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003457
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003458 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003459 {
3460 int cols, cole;
3461 int lines, linee;
3462
3463 li = lio->li_tv.vval.v_list->lv_first;
3464 cols = tv_get_number(&li->li_tv);
3465 if (cols < 0)
3466 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003467 if (cols <= 0)
3468 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003469 li = li->li_next;
3470 cole = tv_get_number(&li->li_tv);
3471 if (cole < 0)
3472 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003473 if (cole > width)
3474 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003475 li = li->li_next;
3476 lines = tv_get_number(&li->li_tv);
3477 if (lines < 0)
3478 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003479 if (lines <= 0)
3480 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003481 li = li->li_next;
3482 linee = tv_get_number(&li->li_tv);
3483 if (linee < 0)
3484 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003485 if (linee > height)
3486 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003487
Bram Moolenaar4012d262020-12-29 11:57:46 +01003488 for (row = lines - 1; row < linee; ++row)
3489 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003490 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003491 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003492}
3493
3494/*
3495 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3496 * "col" and "line" are screen coordinates.
3497 */
3498 static int
3499popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3500{
3501 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3502 int line = screenline - wp->w_winrow;
3503
3504 return col >= 0 && col < width
3505 && line >= 0 && line < height
3506 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003507}
3508
3509/*
3510 * Set flags in popup_transparent[] for window "wp" to "val".
3511 */
3512 static void
3513update_popup_transparent(win_T *wp, int val)
3514{
3515 if (wp->w_popup_mask != NULL)
3516 {
3517 int width = popup_width(wp);
3518 int height = popup_height(wp);
3519 listitem_T *lio, *li;
3520 int cols, cole;
3521 int lines, linee;
3522 int col, line;
3523
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003524 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003525 {
3526 li = lio->li_tv.vval.v_list->lv_first;
3527 cols = tv_get_number(&li->li_tv);
3528 if (cols < 0)
3529 cols = width + cols + 1;
3530 li = li->li_next;
3531 cole = tv_get_number(&li->li_tv);
3532 if (cole < 0)
3533 cole = width + cole + 1;
3534 li = li->li_next;
3535 lines = tv_get_number(&li->li_tv);
3536 if (lines < 0)
3537 lines = height + lines + 1;
3538 li = li->li_next;
3539 linee = tv_get_number(&li->li_tv);
3540 if (linee < 0)
3541 linee = height + linee + 1;
3542
3543 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003544 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003545 if (cols < 0)
3546 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003547 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003548 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003549 if (lines < 0)
3550 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003551 for (line = lines; line < linee
3552 && line + wp->w_winrow < screen_Rows; ++line)
3553 for (col = cols; col < cole
3554 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003555 popup_transparent[(line + wp->w_winrow) * screen_Columns
3556 + col + wp->w_wincol] = val;
3557 }
3558 }
3559}
3560
3561/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003562 * Only called when popup window "wp" is hidden: If the window is positioned
3563 * next to a text property, and it is now visible, then unhide the popup.
3564 * We don't check if visible popups become hidden, that is done in
3565 * popup_adjust_position().
3566 * Return TRUE if the popup became unhidden.
3567 */
3568 static int
3569check_popup_unhidden(win_T *wp)
3570{
3571 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3572 {
3573 textprop_T prop;
3574 linenr_T lnum;
3575
3576 if (find_visible_prop(wp->w_popup_prop_win,
3577 wp->w_popup_prop_type, wp->w_popup_prop_id,
3578 &prop, &lnum) == OK)
3579 {
3580 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003581 wp->w_popup_prop_topline = 0; // force repositioning
3582 return TRUE;
3583 }
3584 }
3585 return FALSE;
3586}
3587
3588/*
3589 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3590 * That is when the buffer in the popup was changed, or the popup is following
3591 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003592 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003593 */
3594 static int
3595popup_need_position_adjust(win_T *wp)
3596{
3597 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3598 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003599 if (win_valid(wp->w_popup_prop_win)
3600 && (wp->w_popup_prop_changedtick
3601 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3602 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3603 return TRUE;
3604
3605 // May need to adjust the width if the cursor moved.
3606 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003607}
3608
3609/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003610 * Update "popup_mask" if needed.
3611 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003612 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003613 * Also marks window lines for redrawing.
3614 */
3615 void
3616may_update_popup_mask(int type)
3617{
3618 win_T *wp;
3619 short *mask;
3620 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003621 int redraw_all_popups = FALSE;
3622 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003623
3624 // Need to recompute when switching tabs.
3625 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3626 // (such as the screen size) must have changed.
3627 if (popup_mask_tab != curtab || type >= NOT_VALID)
3628 {
3629 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003630 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003631 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003632
3633 // Check if any popup window buffer has changed and if any popup connected
3634 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003635 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003636 if (wp->w_popup_flags & POPF_HIDDEN)
3637 popup_mask_refresh |= check_popup_unhidden(wp);
3638 else if (popup_need_position_adjust(wp))
3639 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003640 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003641 if (wp->w_popup_flags & POPF_HIDDEN)
3642 popup_mask_refresh |= check_popup_unhidden(wp);
3643 else if (popup_need_position_adjust(wp))
3644 popup_mask_refresh = TRUE;
3645
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003646 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003647 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003648
3649 // Need to update the mask, something has changed.
3650 popup_mask_refresh = FALSE;
3651 popup_mask_tab = curtab;
3652 popup_visible = FALSE;
3653
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003654 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003655 // If redrawing only what is needed, update "popup_mask_next" and then
3656 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003657 redrawing_all_win = TRUE;
3658 FOR_ALL_WINDOWS(wp)
3659 if (wp->w_redr_type < SOME_VALID)
3660 redrawing_all_win = FALSE;
3661 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003662 mask = popup_mask;
3663 else
3664 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003665 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003666
3667 // Find the window with the lowest zindex that hasn't been handled yet,
3668 // so that the window with a higher zindex overwrites the value in
3669 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003670 popup_reset_handled(POPUP_HANDLED_4);
3671 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003672 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003673 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003674 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003675
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003676 popup_visible = TRUE;
3677
Bram Moolenaar12034e22019-08-25 22:25:02 +02003678 // Recompute the position if the text changed. It may make the popup
3679 // hidden if it's attach to a text property that is no longer visible.
3680 if (redraw_all_popups || popup_need_position_adjust(wp))
3681 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003682 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003683 if (wp->w_popup_flags & POPF_HIDDEN)
3684 continue;
3685 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003686
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003687 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003688 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003689 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003690 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003691 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003692 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003693 col < wp->w_wincol + width - wp->w_popup_leftoff
3694 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003695 if (wp->w_zindex < POPUPMENU_ZINDEX
3696 && pum_visible()
3697 && pum_under_menu(line, col, FALSE))
3698 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3699 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003700 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003701 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003702 }
3703
3704 // Only check which lines are to be updated if not already
3705 // updating all lines.
3706 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003707 {
3708 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3709 win_T *prev_wp = NULL;
3710
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003711 for (line = 0; line < screen_Rows; ++line)
3712 {
3713 int col_done = 0;
3714
3715 for (col = 0; col < screen_Columns; ++col)
3716 {
3717 int off = line * screen_Columns + col;
3718
3719 if (popup_mask[off] != popup_mask_next[off])
3720 {
3721 popup_mask[off] = popup_mask_next[off];
3722
3723 if (line >= cmdline_row)
3724 {
3725 // the command line needs to be cleared if text below
3726 // the popup is now visible.
3727 if (!msg_scrolled && popup_mask_next[off] == 0)
3728 clear_cmdline = TRUE;
3729 }
3730 else if (col >= col_done)
3731 {
3732 linenr_T lnum;
3733 int line_cp = line;
3734 int col_cp = col;
3735
3736 // The screen position "line" / "col" needs to be
3737 // redrawn. Figure out what window that is and update
3738 // w_redraw_top and w_redr_bot. Only needs to be done
3739 // once for each window line.
3740 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3741 if (wp != NULL)
3742 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003743#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003744 // A terminal window needs to be redrawn.
3745 if (bt_terminal(wp->w_buffer))
3746 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003747 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003748#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003749 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003750 if (wp != prev_wp)
3751 {
3752 vim_memset(plines_cache, 0,
3753 sizeof(int) * Rows);
3754 prev_wp = wp;
3755 }
3756
3757 if (line_cp >= wp->w_height)
3758 // In (or below) status line
3759 wp->w_redr_status = TRUE;
3760 else
3761 {
3762 // compute the position in the buffer line
3763 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003764 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003765 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003766 redrawWinline(wp, lnum);
3767 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003768 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003769
3770 // This line is going to be redrawn, no need to
3771 // check until the right side of the window.
3772 col_done = wp->w_wincol + wp->w_width - 1;
3773 }
3774 }
3775 }
3776 }
3777 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003778
3779 vim_free(plines_cache);
3780 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003781
3782 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003783}
3784
3785/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003786 * If the current window is a popup and something relevant changed, recompute
3787 * the position and size.
3788 */
3789 void
3790may_update_popup_position(void)
3791{
3792 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3793 popup_adjust_position(curwin);
3794}
3795
3796/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003797 * Return a string of "len" spaces in IObuff.
3798 */
3799 static char_u *
3800get_spaces(int len)
3801{
3802 vim_memset(IObuff, ' ', (size_t)len);
3803 IObuff[len] = NUL;
3804 return IObuff;
3805}
3806
3807/*
3808 * Update popup windows. They are drawn on top of normal windows.
3809 * "win_update" is called for each popup window, lowest zindex first.
3810 */
3811 void
3812update_popups(void (*win_update)(win_T *wp))
3813{
3814 win_T *wp;
3815 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003816 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003817 int total_width;
3818 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003819 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003820 int popup_attr;
3821 int border_attr[4];
3822 int border_char[8];
3823 char_u buf[MB_MAXBYTES];
3824 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003825 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003826 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003827 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003828 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003829 int sb_thumb_top = 0;
3830 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003831 int attr_scroll = 0;
3832 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003833
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003834 // hide the cursor until redrawing is done.
3835 cursor_off();
3836
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003837 // Find the window with the lowest zindex that hasn't been updated yet,
3838 // so that the window with a higher zindex is drawn later, thus goes on
3839 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003840 popup_reset_handled(POPUP_HANDLED_5);
3841 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003842 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003843 int title_len = 0;
3844 int title_wincol;
3845
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003846 // This drawing uses the zindex of the popup window, so that it's on
3847 // top of the text but doesn't draw when another popup with higher
3848 // zindex is on top of the character.
3849 screen_zindex = wp->w_zindex;
3850
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003851 // Set flags in popup_transparent[] for masked cells.
3852 update_popup_transparent(wp, 1);
3853
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003854 // adjust w_winrow and w_wincol for border and padding, since
3855 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003856 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003857 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3858 - wp->w_popup_leftoff;
3859 if (wp->w_wincol + left_extra < 0)
3860 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003861 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003862 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003863
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003864 // Draw the popup text, unless it's off screen.
3865 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003866 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003867 // May need to update the "cursorline" highlighting, which may also
3868 // change "topline"
3869 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3870 popup_highlight_curline(wp);
3871
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003872 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003873
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003874 // move the cursor into the visible lines, otherwise executing
3875 // commands with win_execute() may cause the text to jump.
3876 if (wp->w_cursor.lnum < wp->w_topline)
3877 wp->w_cursor.lnum = wp->w_topline;
3878 else if (wp->w_cursor.lnum >= wp->w_botline)
3879 wp->w_cursor.lnum = wp->w_botline - 1;
3880 }
3881
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003883 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003884
3885 // Add offset for border and padding if not done already.
3886 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3887 {
3888 wp->w_wcol += left_extra;
3889 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3890 }
3891 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003892 {
3893 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003894 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003895 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003896
Bram Moolenaard529ba52019-07-02 23:13:53 +02003897 total_width = popup_width(wp);
3898 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003899 popup_attr = get_wcr_attr(wp);
3900
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003901 if (wp->w_winrow + total_height > cmdline_row)
3902 wp->w_popup_flags |= POPF_ON_CMDLINE;
3903 else
3904 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3905
3906
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003907 // We can only use these line drawing characters when 'encoding' is
3908 // "utf-8" and 'ambiwidth' is "single".
3909 if (enc_utf8 && *p_ambw == 's')
3910 {
3911 border_char[0] = border_char[2] = 0x2550;
3912 border_char[1] = border_char[3] = 0x2551;
3913 border_char[4] = 0x2554;
3914 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003915 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3916 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003917 border_char[7] = 0x255a;
3918 }
3919 else
3920 {
3921 border_char[0] = border_char[2] = '-';
3922 border_char[1] = border_char[3] = '|';
3923 for (i = 4; i < 8; ++i)
3924 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003925 if (wp->w_popup_flags & POPF_RESIZE)
3926 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003927 }
3928 for (i = 0; i < 8; ++i)
3929 if (wp->w_border_char[i] != 0)
3930 border_char[i] = wp->w_border_char[i];
3931
3932 for (i = 0; i < 4; ++i)
3933 {
3934 border_attr[i] = popup_attr;
3935 if (wp->w_border_highlight[i] != NULL)
3936 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3937 }
3938
Bram Moolenaard91467f2020-11-21 12:42:09 +01003939 // Title goes on top of border or padding.
3940 title_wincol = wp->w_wincol + 1;
3941 if (wp->w_popup_title != NULL)
3942 {
rbtnnc6119412021-08-07 13:08:45 +02003943 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003944
Ralf Schandlbc869872021-05-28 14:12:14 +02003945 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003946 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02003947 {
3948 int title_byte_len = (int)STRLEN(wp->w_popup_title);
3949 char_u *title_text = alloc(title_byte_len + 1);
3950
3951 if (title_text != NULL)
3952 {
3953 trunc_string(wp->w_popup_title, title_text,
3954 total_width - 2, title_byte_len + 1);
3955 screen_puts(title_text, wp->w_winrow, title_wincol,
3956 wp->w_popup_border[0] > 0
3957 ? border_attr[0] : popup_attr);
3958 vim_free(title_text);
3959 }
3960
Bram Moolenaard91467f2020-11-21 12:42:09 +01003961 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02003962 }
3963 else
3964 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
3965 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003966 }
3967
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003968 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003969 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003970 if (wp->w_popup_border[0] > 0)
3971 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003972 // top border; do not draw over the title
3973 if (title_len > 0)
3974 {
3975 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3976 wincol < 0 ? 0 : wincol, title_wincol,
3977 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003978 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01003979 border_char[0], border_attr[0]);
3980 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3981 title_wincol + title_len, wincol + total_width,
3982 border_char[0], border_char[0], border_attr[0]);
3983 }
3984 else
3985 {
3986 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3987 wincol < 0 ? 0 : wincol, wincol + total_width,
3988 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
3989 ? border_char[4] : border_char[0],
3990 border_char[0], border_attr[0]);
3991 }
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003992 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003993 {
3994 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3995 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003996 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003997 }
3998 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003999 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4000 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004001
Bram Moolenaard529ba52019-07-02 23:13:53 +02004002 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4003 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004004 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004005 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004006 - wp->w_has_scrollbar;
4007 if (padcol < 0)
4008 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004009 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004010 padcol = 0;
4011 }
4012 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004013 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004014 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004015 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004016 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004017 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004018 // top padding and no border; do not draw over the title
4019 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004020 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004021 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004022 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004023 row += 1;
4024 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004025 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004026 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004027 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004028 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004029
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004030 // Compute scrollbar thumb position and size.
4031 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004032 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004033 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4034 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004035
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004036 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4037 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004038 if (wp->w_topline > 1 && sb_thumb_height == height)
4039 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004040 if (sb_thumb_height == 0)
4041 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02004042 if (linecount <= wp->w_height)
4043 // it just fits, avoid divide by zero
4044 sb_thumb_top = 0;
4045 else
4046 sb_thumb_top = (wp->w_topline - 1
4047 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004048 * (wp->w_height - sb_thumb_height)
4049 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004050 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4051 sb_thumb_top = 1; // show it's scrolled
4052
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004053 if (wp->w_scrollbar_highlight != NULL)
4054 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4055 else
4056 attr_scroll = highlight_attr[HLF_PSB];
4057 if (wp->w_thumb_highlight != NULL)
4058 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4059 else
4060 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004061 }
4062
4063 for (i = wp->w_popup_border[0];
4064 i < total_height - wp->w_popup_border[2]; ++i)
4065 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004066 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004067 // left and right padding only needed next to the body
4068 int do_padding =
4069 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4070 && i < total_height - wp->w_popup_border[2]
4071 - wp->w_popup_padding[2];
4072
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004073 row = wp->w_winrow + i;
4074
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004075 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004076 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004077 {
4078 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004079 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004080 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004081 if (do_padding && wp->w_popup_padding[3] > 0)
4082 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004083 int col = wincol + wp->w_popup_border[3];
4084
Bram Moolenaard529ba52019-07-02 23:13:53 +02004085 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004086 pad_left = wp->w_popup_padding[3];
4087 if (col < 0)
4088 {
4089 pad_left += col;
4090 col = 0;
4091 }
4092 if (pad_left > 0)
4093 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4094 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004095 // scrollbar
4096 if (wp->w_has_scrollbar)
4097 {
4098 int line = i - top_off;
4099 int scroll_col = wp->w_wincol + total_width - 1
4100 - wp->w_popup_border[1];
4101
4102 if (line >= 0 && line < wp->w_height)
4103 screen_putchar(' ', row, scroll_col,
4104 line >= sb_thumb_top
4105 && line < sb_thumb_top + sb_thumb_height
4106 ? attr_thumb : attr_scroll);
4107 else
4108 screen_putchar(' ', row, scroll_col, popup_attr);
4109 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004110 // right border
4111 if (wp->w_popup_border[1] > 0)
4112 {
4113 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004114 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004115 }
4116 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004117 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004118 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004119 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004120 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4121 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004122 }
4123
4124 if (wp->w_popup_padding[2] > 0)
4125 {
4126 // bottom padding
4127 row = wp->w_winrow + wp->w_popup_border[0]
4128 + wp->w_popup_padding[0] + wp->w_height;
4129 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004130 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004131 }
4132
4133 if (wp->w_popup_border[2] > 0)
4134 {
4135 // bottom border
4136 row = wp->w_winrow + total_height - 1;
4137 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004138 wincol < 0 ? 0 : wincol,
4139 wincol + total_width,
4140 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004141 ? border_char[7] : border_char[2],
4142 border_char[2], border_attr[2]);
4143 if (wp->w_popup_border[1] > 0)
4144 {
4145 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004146 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004147 }
4148 }
4149
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004150 if (wp->w_popup_close == POPCLOSE_BUTTON)
4151 {
4152 // close button goes on top of anything at the top-right corner
4153 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004154 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004155 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4156 }
4157
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004158 update_popup_transparent(wp, 0);
4159
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004160 // Back to the normal zindex.
4161 screen_zindex = 0;
4162 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004163
4164#if defined(FEAT_SEARCH_EXTRA)
4165 // In case win_update() called start_search_hl().
4166 end_search_hl();
4167#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004168}
4169
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004170/*
4171 * Mark references in callbacks of one popup window.
4172 */
4173 static int
4174set_ref_in_one_popup(win_T *wp, int copyID)
4175{
4176 int abort = FALSE;
4177 typval_T tv;
4178
4179 if (wp->w_close_cb.cb_partial != NULL)
4180 {
4181 tv.v_type = VAR_PARTIAL;
4182 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4183 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4184 }
4185 if (wp->w_filter_cb.cb_partial != NULL)
4186 {
4187 tv.v_type = VAR_PARTIAL;
4188 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4189 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4190 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004191 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004192 return abort;
4193}
4194
4195/*
4196 * Set reference in callbacks of popup windows.
4197 */
4198 int
4199set_ref_in_popups(int copyID)
4200{
4201 int abort = FALSE;
4202 win_T *wp;
4203 tabpage_T *tp;
4204
4205 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4206 abort = abort || set_ref_in_one_popup(wp, copyID);
4207
4208 FOR_ALL_TABPAGES(tp)
4209 {
4210 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4211 abort = abort || set_ref_in_one_popup(wp, copyID);
4212 if (abort)
4213 break;
4214 }
4215 return abort;
4216}
Bram Moolenaar79648732019-07-18 21:43:07 +02004217
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004218 int
4219popup_is_popup(win_T *wp)
4220{
4221 return wp->w_popup_flags != 0;
4222}
4223
4224#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004225/*
4226 * Find an existing popup used as the preview window, in the current tab page.
4227 * Return NULL if not found.
4228 */
4229 win_T *
4230popup_find_preview_window(void)
4231{
4232 win_T *wp;
4233
4234 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004235 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004236 if (wp->w_p_pvw)
4237 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004238 return NULL;
4239}
4240
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004241/*
4242 * Find an existing popup used as the info window, in the current tab page.
4243 * Return NULL if not found.
4244 */
4245 win_T *
4246popup_find_info_window(void)
4247{
4248 win_T *wp;
4249
4250 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004251 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004252 if (wp->w_popup_flags & POPF_INFO)
4253 return wp;
4254 return NULL;
4255}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004256#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004257
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004258 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004259f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4260{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004261#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004262 win_T *wp = popup_find_info_window();
4263
4264 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004265#else
4266 rettv->vval.v_number = 0;
4267#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004268}
4269
4270 void
4271f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004272{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004273#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004274 win_T *wp = popup_find_preview_window();
4275
4276 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004277#else
4278 rettv->vval.v_number = 0;
4279#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004280}
4281
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004282#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004283/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004284 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004285 * NOTE: this makes the popup the current window, so that the file can be
4286 * edited. However, it must not remain to be the current window, the caller
4287 * must make sure of that.
4288 */
4289 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004290popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004291{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004292 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004293
4294 if (wp == NULL)
4295 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004296 if (info)
4297 wp->w_popup_flags |= POPF_INFO;
4298 else
4299 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004300
4301 // Set the width to a reasonable value, so that w_topline can be computed.
4302 if (wp->w_minwidth > 0)
4303 wp->w_width = wp->w_minwidth;
4304 else if (wp->w_maxwidth > 0)
4305 wp->w_width = wp->w_maxwidth;
4306 else
4307 wp->w_width = curwin->w_width;
4308
4309 // Will switch to another buffer soon, dummy one can be wiped.
4310 wp->w_buffer->b_locked = FALSE;
4311
4312 win_enter(wp, FALSE);
4313 return OK;
4314}
4315
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004316/*
4317 * Close any preview popup.
4318 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004319 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004320popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004321{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004322 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004323
4324 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004325 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004326}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004327
4328/*
4329 * Hide the info popup.
4330 */
4331 void
4332popup_hide_info(void)
4333{
4334 win_T *wp = popup_find_info_window();
4335
4336 if (wp != NULL)
4337 popup_hide(wp);
4338}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004339
4340/*
4341 * Close any info popup.
4342 */
4343 void
4344popup_close_info(void)
4345{
4346 win_T *wp = popup_find_info_window();
4347
4348 if (wp != NULL)
4349 popup_close_with_retval(wp, -1);
4350}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004351#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004352
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004353/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004354 * Close any popup for a text property associated with "win".
4355 * Return TRUE if a popup was closed.
4356 */
4357 int
4358popup_win_closed(win_T *win)
4359{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004360 int round;
4361 win_T *wp;
4362 win_T *next;
4363 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004364
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004365 for (round = 1; round <= 2; ++round)
4366 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4367 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004368 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004369 next = wp->w_next;
4370 if (wp->w_popup_prop_win == win)
4371 {
4372 popup_close_with_retval(wp, -1);
4373 ret = TRUE;
4374 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004375 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004376 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004377}
4378
4379/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004380 * Set the title of the popup window to the file name.
4381 */
4382 void
4383popup_set_title(win_T *wp)
4384{
4385 if (wp->w_buffer->b_fname != NULL)
4386 {
4387 char_u dirname[MAXPATHL];
4388 size_t len;
4389
4390 mch_dirname(dirname, MAXPATHL);
4391 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4392
4393 vim_free(wp->w_popup_title);
4394 len = STRLEN(wp->w_buffer->b_fname) + 3;
4395 wp->w_popup_title = alloc((int)len);
4396 if (wp->w_popup_title != NULL)
4397 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4398 wp->w_buffer->b_fname);
4399 redraw_win_later(wp, VALID);
4400 }
4401}
4402
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004403# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004404/*
4405 * If there is a preview window, update the title.
4406 * Used after changing directory.
4407 */
4408 void
4409popup_update_preview_title(void)
4410{
4411 win_T *wp = popup_find_preview_window();
4412
4413 if (wp != NULL)
4414 popup_set_title(wp);
4415}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004416# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004417
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004418#endif // FEAT_PROP_POPUP