blob: 96874011e6203fb0072bd4a53a67ee762b9ec4cf [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020064 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020065 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000089 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020090 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100100 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200101 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
103 ++i, li = li->li_next)
104 {
105 nr = (int)tv_get_number(&li->li_tv);
106 if (nr >= 0)
107 array[i] = nr > max_val ? max_val : nr;
108 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100109 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110 }
111 }
112}
113
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200114/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200115 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200116 */
117 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200118set_moved_values(win_T *wp)
119{
120 wp->w_popup_curwin = curwin;
121 wp->w_popup_lnum = curwin->w_cursor.lnum;
122 wp->w_popup_mincol = curwin->w_cursor.col;
123 wp->w_popup_maxcol = curwin->w_cursor.col;
124}
125
126/*
127 * Used when popup options contain "moved" with "word" or "WORD".
128 */
129 static void
130set_moved_columns(win_T *wp, int flags)
131{
132 char_u *ptr;
133 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
134
135 if (len > 0)
136 {
137 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
138 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
139 }
140}
141
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200143 * Used when popup options contain "mousemoved": set default moved values.
144 */
145 static void
146set_mousemoved_values(win_T *wp)
147{
148 wp->w_popup_mouse_row = mouse_row;
149 wp->w_popup_mouse_mincol = mouse_col;
150 wp->w_popup_mouse_maxcol = mouse_col;
151}
152
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000153 static void
154update_popup_uses_mouse_move(void)
155{
156 popup_uses_mouse_move = FALSE;
157 if (popup_visible)
158 {
159 win_T *wp;
160
161 FOR_ALL_POPUPWINS(wp)
162 if (wp->w_popup_mouse_row != 0)
163 {
164 popup_uses_mouse_move = TRUE;
165 return;
166 }
167 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
168 if (wp->w_popup_mouse_row != 0)
169 {
170 popup_uses_mouse_move = TRUE;
171 return;
172 }
173 }
174}
175
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200176/*
177 * Used when popup options contain "moved" with "word" or "WORD".
178 */
179 static void
180set_mousemoved_columns(win_T *wp, int flags)
181{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200182 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200183 char_u *text;
184 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200185 pos_T pos;
186 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200187
188 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200189 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200190 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200191 // convert text column to mouse column
192 pos.col = col;
193 pos.coladd = 0;
194 getvcol(textwp, &pos, &mcol, NULL, NULL);
195 wp->w_popup_mouse_mincol = mcol;
196
Bram Moolenaar10727682019-07-12 13:59:20 +0200197 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200198 getvcol(textwp, &pos, NULL, NULL, &mcol);
199 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200200 vim_free(text);
201 }
202}
203
204/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200205 * Return TRUE if "row"/"col" is on the border of the popup.
206 * The values are relative to the top-left corner.
207 */
208 int
209popup_on_border(win_T *wp, int row, int col)
210{
211 return (row == 0 && wp->w_popup_border[0] > 0)
212 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
213 || (col == 0 && wp->w_popup_border[3] > 0)
214 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
215}
216
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200217/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200218 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
219 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200220 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200221 * Caller should check the left mouse button was clicked.
222 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200223 */
224 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200225popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200226{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200227 if (wp->w_popup_close == POPCLOSE_BUTTON
228 && row == 0 && col == popup_width(wp) - 1)
229 {
230 popup_close_for_mouse_click(wp);
231 return TRUE;
232 }
233 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200234}
235
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200236// Values set when dragging a popup window starts.
237static int drag_start_row;
238static int drag_start_col;
239static int drag_start_wantline;
240static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200242
243/*
244 * Mouse down on border of popup window: start dragging it.
245 * Uses mouse_col and mouse_row.
246 */
247 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200248popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200249{
250 drag_start_row = mouse_row;
251 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200252 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200253 drag_start_wantline = wp->w_winrow + 1;
254 else
255 drag_start_wantline = wp->w_wantline;
256 if (wp->w_wantcol == 0)
257 drag_start_wantcol = wp->w_wincol + 1;
258 else
259 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200260
261 // Stop centering the popup
262 if (wp->w_popup_pos == POPPOS_CENTER)
263 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200264
265 drag_on_resize_handle = wp->w_popup_border[1] > 0
266 && wp->w_popup_border[2] > 0
267 && row == popup_height(wp) - 1
268 && col == popup_width(wp) - 1;
269
270 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
271 {
272 if (wp->w_popup_pos == POPPOS_TOPRIGHT
273 || wp->w_popup_pos == POPPOS_BOTRIGHT)
274 wp->w_wantcol = wp->w_wincol + 1;
275 if (wp->w_popup_pos == POPPOS_BOTLEFT)
276 wp->w_wantline = wp->w_winrow + 1;
277 wp->w_popup_pos = POPPOS_TOPLEFT;
278 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200279}
280
281/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200282 * Mouse moved while dragging a popup window: adjust the window popup position
283 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284 */
285 void
286popup_drag(win_T *wp)
287{
288 // The popup may be closed before dragging stops.
289 if (!win_valid_popup(wp))
290 return;
291
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200292 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
293 {
294 int width_inc = mouse_col - drag_start_col;
295 int height_inc = mouse_row - drag_start_row;
296
297 if (width_inc != 0)
298 {
299 int width = wp->w_width + width_inc;
300
301 if (width < 1)
302 width = 1;
303 wp->w_minwidth = width;
304 wp->w_maxwidth = width;
305 drag_start_col = mouse_col;
306 }
307
308 if (height_inc != 0)
309 {
310 int height = wp->w_height + height_inc;
311
312 if (height < 1)
313 height = 1;
314 wp->w_minheight = height;
315 wp->w_maxheight = height;
316 drag_start_row = mouse_row;
317 }
318
319 popup_adjust_position(wp);
320 return;
321 }
322
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000323 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200324 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200325 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
326 if (wp->w_wantline < 1)
327 wp->w_wantline = 1;
328 if (wp->w_wantline > Rows)
329 wp->w_wantline = Rows;
330 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
331 if (wp->w_wantcol < 1)
332 wp->w_wantcol = 1;
333 if (wp->w_wantcol > Columns)
334 wp->w_wantcol = Columns;
335
336 popup_adjust_position(wp);
337}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200338
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200339/*
340 * Set w_firstline to match the current "wp->w_topline".
341 */
342 void
343popup_set_firstline(win_T *wp)
344{
345 int height = wp->w_height;
346
347 wp->w_firstline = wp->w_topline;
348 popup_adjust_position(wp);
349
350 // we don't want the popup to get smaller, decrement the first line
351 // until it doesn't
352 while (wp->w_firstline > 1 && wp->w_height < height)
353 {
354 --wp->w_firstline;
355 popup_adjust_position(wp);
356 }
357}
358
359/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200360 * Return TRUE if the position is in the popup window scrollbar.
361 */
362 int
363popup_is_in_scrollbar(win_T *wp, int row, int col)
364{
365 return wp->w_has_scrollbar
366 && row >= wp->w_popup_border[0]
367 && row < popup_height(wp) - wp->w_popup_border[2]
368 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
369}
370
371
372/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200373 * Handle a click in a popup window, if it is in the scrollbar.
374 */
375 void
376popup_handle_scrollbar_click(win_T *wp, int row, int col)
377{
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200378 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200379 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100380 int height = popup_height(wp);
381 int new_topline = wp->w_topline;
382
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200383 if (row >= height / 2)
384 {
385 // Click in lower half, scroll down.
386 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaard28950f2022-05-29 14:13:04 +0100387 ++new_topline;
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200388 }
389 else if (wp->w_topline > 1)
390 // click on upper half, scroll up.
Bram Moolenaard28950f2022-05-29 14:13:04 +0100391 --new_topline;
392 if (new_topline != wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200393 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100394 set_topline(wp, new_topline);
395 if (wp == curwin)
396 {
397 if (wp->w_cursor.lnum < wp->w_topline)
398 {
399 wp->w_cursor.lnum = wp->w_topline;
400 check_cursor();
401 }
402 else if (wp->w_cursor.lnum >= wp->w_botline)
403 {
404 wp->w_cursor.lnum = wp->w_botline - 1;
405 check_cursor();
406 }
407 }
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200408 popup_set_firstline(wp);
409 redraw_win_later(wp, NOT_VALID);
410 }
411 }
412}
413
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200414#if defined(FEAT_TIMERS)
415 static void
416popup_add_timeout(win_T *wp, int time)
417{
418 char_u cbbuf[50];
419 char_u *ptr = cbbuf;
420 typval_T tv;
421
422 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200423 "(_) => popup_close(%d)", wp->w_id);
424 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200425 {
426 wp->w_popup_timer = create_timer(time, 0);
427 wp->w_popup_timer->tr_callback = get_callback(&tv);
428 clear_tv(&tv);
429 }
430}
431#endif
432
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100433 static poppos_T
434get_pos_entry(dict_T *d, int give_error)
435{
436 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
437 int nr;
438
439 if (str == NULL)
440 return POPPOS_NONE;
441
K.Takataeeec2542021-06-02 13:28:16 +0200442 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100443 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
444 return poppos_entries[nr].pp_val;
445
446 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000447 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100448 return POPPOS_NONE;
449}
450
Bram Moolenaar17627312019-06-02 19:53:44 +0200451/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200452 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200453 */
454 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200455apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200456{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200457 int nr;
458 char_u *str;
459 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200460
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200461 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200462 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200463 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200464 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200465 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200466 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200467 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200468 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200469
470 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200471 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200472 wp->w_wantline = nr;
473 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200474 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200475 wp->w_wantcol = nr;
476
Bram Moolenaar55881332020-08-18 13:04:15 +0200477
478 nr = dict_get_bool(d, (char_u *)"fixed", -1);
479 if (nr != -1)
480 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200481
Bram Moolenaar12034e22019-08-25 22:25:02 +0200482 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100483 poppos_T ppt = get_pos_entry(d, TRUE);
484
485 if (ppt != POPPOS_NONE)
486 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200487 }
488
489 str = dict_get_string(d, (char_u *)"textprop", FALSE);
490 if (str != NULL)
491 {
492 wp->w_popup_prop_type = 0;
493 if (*str != NUL)
494 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100495 wp->w_popup_prop_win = curwin;
496 di = dict_find(d, (char_u *)"textpropwin", -1);
497 if (di != NULL)
498 {
499 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100500 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100501 wp->w_popup_prop_win = curwin;
502 }
503
504 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200505 if (nr <= 0)
506 nr = find_prop_type_id(str, NULL);
507 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000508 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200509 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200510 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200511 }
512 }
513
514 di = dict_find(d, (char_u *)"textpropid", -1);
515 if (di != NULL)
516 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200517}
518
Bram Moolenaarb0992022020-01-30 14:55:42 +0100519/*
520 * Handle "moved" and "mousemoved" arguments.
521 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200522 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200523handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
524{
525 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
526 {
527 char_u *s = di->di_tv.vval.v_string;
528 int flags = 0;
529
530 if (STRCMP(s, "word") == 0)
531 flags = FIND_IDENT | FIND_STRING;
532 else if (STRCMP(s, "WORD") == 0)
533 flags = FIND_STRING;
534 else if (STRCMP(s, "expr") == 0)
535 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
536 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000537 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200538 if (flags != 0)
539 {
540 if (mousemoved)
541 set_mousemoved_columns(wp, flags);
542 else
543 set_moved_columns(wp, flags);
544 }
545 }
546 else if (di->di_tv.v_type == VAR_LIST
547 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200548 && (di->di_tv.vval.v_list->lv_len == 2
549 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200550 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200551 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100552 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200553 int mincol;
554 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200555
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200556 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100557 li = l->lv_first;
558 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200559 {
560 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
561
562 // Three numbers, might be from popup_getoptions().
563 if (mousemoved)
564 wp->w_popup_mouse_row = nr;
565 else
566 wp->w_popup_lnum = nr;
567 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100568 if (nr == 0)
569 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200570 }
571
572 mincol = tv_get_number(&li->li_tv);
573 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200574 if (mousemoved)
575 {
576 wp->w_popup_mouse_mincol = mincol;
577 wp->w_popup_mouse_maxcol = maxcol;
578 }
579 else
580 {
581 wp->w_popup_mincol = mincol;
582 wp->w_popup_maxcol = maxcol;
583 }
584 }
585 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000586 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200587}
588
589 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200590check_highlight(dict_T *dict, char *name, char_u **pval)
591{
592 dictitem_T *di;
593 char_u *str;
594
595 di = dict_find(dict, (char_u *)name, -1);
596 if (di != NULL)
597 {
598 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000599 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200600 else
601 {
602 str = tv_get_string(&di->di_tv);
603 if (*str != NUL)
604 *pval = vim_strsave(str);
605 }
606 }
607}
608
Bram Moolenaarae943152019-06-16 22:54:14 +0200609/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200610 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200611 */
612 static void
613popup_show_curline(win_T *wp)
614{
615 if (wp->w_cursor.lnum < wp->w_topline)
616 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200617 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200618 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200619 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200620 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200621 if (wp->w_topline < 1)
622 wp->w_topline = 1;
623 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
624 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200625 while (wp->w_topline < wp->w_cursor.lnum
626 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
627 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
628 > wp->w_height)
629 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200630 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200631
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200632 // Don't let "firstline" cause a scroll.
633 if (wp->w_firstline > 0)
634 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200635}
636
637/*
638 * Get the sign group name for window "wp".
639 * Returns a pointer to a static buffer, overwritten on the next call.
640 */
641 static char_u *
642popup_get_sign_name(win_T *wp)
643{
644 static char buf[30];
645
646 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
647 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200648}
649
650/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200651 * Highlight the line with the cursor.
652 * Also scrolls the text to put the cursor line in view.
653 */
654 static void
655popup_highlight_curline(win_T *wp)
656{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200657 int sign_id = 0;
658 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200659
Bram Moolenaar72570732019-11-30 14:21:53 +0100660 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200661
662 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
663 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200664 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200665
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200666 if (!sign_exists_by_name(sign_name))
667 {
668 char *linehl = "PopupSelected";
669
670 if (syn_name2id((char_u *)linehl) == 0)
671 linehl = "PmenuSel";
James McCoya80aad72021-12-22 19:45:28 +0000672 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200673 }
674
Bram Moolenaar72570732019-11-30 14:21:53 +0100675 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200676 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
677 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200678 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200679 else
680 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200681 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200682}
683
684/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200685 * Shared between popup_create() and f_popup_setoptions().
686 */
687 static void
688apply_general_options(win_T *wp, dict_T *dict)
689{
690 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200691 int nr;
692 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200693
Bram Moolenaarae943152019-06-16 22:54:14 +0200694 // TODO: flip
695
696 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200697 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200698 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200699 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200700 if (wp->w_firstline < 0)
701 wp->w_firstline = -1;
702 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200703
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200704 nr = dict_get_bool(dict, (char_u *)"scrollbar", -1);
705 if (nr != -1)
706 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200707
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200708 str = dict_get_string(dict, (char_u *)"title", FALSE);
709 if (str != NULL)
710 {
711 vim_free(wp->w_popup_title);
712 wp->w_popup_title = vim_strsave(str);
713 }
714
Bram Moolenaar55881332020-08-18 13:04:15 +0200715 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
716 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200717 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200718
Bram Moolenaar55881332020-08-18 13:04:15 +0200719 nr = dict_get_bool(dict, (char_u *)"drag", -1);
720 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200721 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200722 if (nr)
723 wp->w_popup_flags |= POPF_DRAG;
724 else
725 wp->w_popup_flags &= ~POPF_DRAG;
726 }
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000727 nr = dict_get_bool(dict, (char_u *)"dragall", -1);
728 if (nr != -1)
729 {
730 if (nr)
731 wp->w_popup_flags |= POPF_DRAGALL;
732 else
733 wp->w_popup_flags &= ~POPF_DRAGALL;
734 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200735
Bram Moolenaar55881332020-08-18 13:04:15 +0200736 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
737 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100738 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100739 if (nr)
740 wp->w_popup_flags |= POPF_POSINVERT;
741 else
742 wp->w_popup_flags &= ~POPF_POSINVERT;
743 }
744
Bram Moolenaar55881332020-08-18 13:04:15 +0200745 nr = dict_get_bool(dict, (char_u *)"resize", -1);
746 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200747 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200748 if (nr)
749 wp->w_popup_flags |= POPF_RESIZE;
750 else
751 wp->w_popup_flags &= ~POPF_RESIZE;
752 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200753
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200754 di = dict_find(dict, (char_u *)"close", -1);
755 if (di != NULL)
756 {
757 int ok = TRUE;
758
759 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
760 {
761 char_u *s = di->di_tv.vval.v_string;
762
763 if (STRCMP(s, "none") == 0)
764 wp->w_popup_close = POPCLOSE_NONE;
765 else if (STRCMP(s, "button") == 0)
766 wp->w_popup_close = POPCLOSE_BUTTON;
767 else if (STRCMP(s, "click") == 0)
768 wp->w_popup_close = POPCLOSE_CLICK;
769 else
770 ok = FALSE;
771 }
772 else
773 ok = FALSE;
774 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000775 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200776 }
777
Bram Moolenaarae943152019-06-16 22:54:14 +0200778 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
779 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000780 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200781 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
782 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000783#ifdef FEAT_TERMINAL
784 term_update_wincolor(wp);
785#endif
786 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200787
Bram Moolenaarae943152019-06-16 22:54:14 +0200788 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
789 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200790
Bram Moolenaar790498b2019-06-01 22:15:29 +0200791 di = dict_find(dict, (char_u *)"borderhighlight", -1);
792 if (di != NULL)
793 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200794 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000795 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200796 else
797 {
798 list_T *list = di->di_tv.vval.v_list;
799 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200800 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200801
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200802 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200803 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
804 ++i, li = li->li_next)
805 {
806 str = tv_get_string(&li->li_tv);
807 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100808 {
809 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200810 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100811 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200812 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200813 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
814 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100815 {
816 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200817 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200818 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100819 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200820 }
821 }
822
Bram Moolenaar790498b2019-06-01 22:15:29 +0200823 di = dict_find(dict, (char_u *)"borderchars", -1);
824 if (di != NULL)
825 {
826 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000827 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200828 else
829 {
830 list_T *list = di->di_tv.vval.v_list;
831 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200832 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200833
834 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100835 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200836 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200837 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
838 ++i, li = li->li_next)
839 {
840 str = tv_get_string(&li->li_tv);
841 if (*str != NUL)
842 wp->w_border_char[i] = mb_ptr2char(str);
843 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200844 if (list->lv_len == 1)
845 for (i = 1; i < 8; ++i)
846 wp->w_border_char[i] = wp->w_border_char[0];
847 if (list->lv_len == 2)
848 {
849 for (i = 4; i < 8; ++i)
850 wp->w_border_char[i] = wp->w_border_char[1];
851 for (i = 1; i < 4; ++i)
852 wp->w_border_char[i] = wp->w_border_char[0];
853 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200854 }
855 }
856 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200857
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200858 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
859 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
860
Bram Moolenaarae943152019-06-16 22:54:14 +0200861 di = dict_find(dict, (char_u *)"zindex", -1);
862 if (di != NULL)
863 {
864 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
865 if (wp->w_zindex < 1)
866 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
867 if (wp->w_zindex > 32000)
868 wp->w_zindex = 32000;
869 }
870
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200871 di = dict_find(dict, (char_u *)"mask", -1);
872 if (di != NULL)
873 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200874 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200875
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200876 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200877 {
878 listitem_T *li;
879
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200880 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200881 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200882 {
883 if (li->li_tv.v_type != VAR_LIST
884 || li->li_tv.vval.v_list == NULL
885 || li->li_tv.vval.v_list->lv_len != 4)
886 {
887 ok = FALSE;
888 break;
889 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100890 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200891 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200892 }
893 }
894 if (ok)
895 {
896 wp->w_popup_mask = di->di_tv.vval.v_list;
897 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200898 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200899 }
900 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000901 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200902 }
903
Bram Moolenaarae943152019-06-16 22:54:14 +0200904#if defined(FEAT_TIMERS)
905 // Add timer to close the popup after some time.
906 nr = dict_get_number(dict, (char_u *)"time");
907 if (nr > 0)
908 popup_add_timeout(wp, nr);
909#endif
910
Bram Moolenaar3397f742019-06-02 18:40:06 +0200911 di = dict_find(dict, (char_u *)"moved", -1);
912 if (di != NULL)
913 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200914 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200915 handle_moved_argument(wp, di, FALSE);
916 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200917
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200918 di = dict_find(dict, (char_u *)"mousemoved", -1);
919 if (di != NULL)
920 {
921 set_mousemoved_values(wp);
922 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200923 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200924
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100925 nr = dict_get_bool(dict, (char_u *)"cursorline", -1);
926 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200927 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100928 if (nr != 0)
929 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200930 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100931 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200932 }
933
Bram Moolenaarae943152019-06-16 22:54:14 +0200934 di = dict_find(dict, (char_u *)"filter", -1);
935 if (di != NULL)
936 {
937 callback_T callback = get_callback(&di->di_tv);
938
939 if (callback.cb_name != NULL)
940 {
941 free_callback(&wp->w_filter_cb);
942 set_callback(&wp->w_filter_cb, &callback);
943 }
944 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200945 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
946 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200947 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200948 if (nr)
949 wp->w_popup_flags |= POPF_MAPPING;
950 else
951 wp->w_popup_flags &= ~POPF_MAPPING;
952 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200953
Bram Moolenaar581ba392019-09-03 22:08:33 +0200954 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
955 if (str != NULL)
956 {
957 if (STRCMP(str, "a") == 0)
958 wp->w_filter_mode = MODE_ALL;
959 else
960 wp->w_filter_mode = mode_str2flags(str);
961 }
962
Bram Moolenaarae943152019-06-16 22:54:14 +0200963 di = dict_find(dict, (char_u *)"callback", -1);
964 if (di != NULL)
965 {
966 callback_T callback = get_callback(&di->di_tv);
967
968 if (callback.cb_name != NULL)
969 {
970 free_callback(&wp->w_close_cb);
971 set_callback(&wp->w_close_cb, &callback);
972 }
973 }
974}
975
976/*
977 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200978 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200979 */
980 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200981apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200982{
983 int nr;
984
985 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200986
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200987 if (create)
988 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200989 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
990
Bram Moolenaarae943152019-06-16 22:54:14 +0200991 apply_general_options(wp, dict);
992
Bram Moolenaar55881332020-08-18 13:04:15 +0200993 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200994 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +0100995 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200996
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200997 // when "firstline" and "cursorline" are both set and the cursor would be
998 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200999 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1000 {
1001 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1002 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001003 else if (wp->w_cursor.lnum < wp->w_firstline
1004 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001005 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001006 wp->w_topline = wp->w_firstline;
1007 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001008 }
1009
Bram Moolenaar33796b32019-06-08 16:01:13 +02001010 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001011 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001012}
1013
1014/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001015 * Add lines to the popup from a list of strings.
1016 */
1017 static void
1018add_popup_strings(buf_T *buf, list_T *l)
1019{
1020 listitem_T *li;
1021 linenr_T lnum = 0;
1022 char_u *p;
1023
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001024 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001025 if (li->li_tv.v_type == VAR_STRING)
1026 {
1027 p = li->li_tv.vval.v_string;
1028 ml_append_buf(buf, lnum++,
1029 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1030 }
1031}
1032
1033/*
1034 * Add lines to the popup from a list of dictionaries.
1035 */
1036 static void
1037add_popup_dicts(buf_T *buf, list_T *l)
1038{
1039 listitem_T *li;
1040 listitem_T *pli;
1041 linenr_T lnum = 0;
1042 char_u *p;
1043 dict_T *dict;
1044
1045 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001046 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001047 {
1048 if (li->li_tv.v_type != VAR_DICT)
1049 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001050 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001051 return;
1052 }
1053 dict = li->li_tv.vval.v_dict;
1054 p = dict == NULL ? NULL
1055 : dict_get_string(dict, (char_u *)"text", FALSE);
1056 ml_append_buf(buf, lnum++,
1057 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1058 }
1059
1060 // add the text properties
1061 lnum = 1;
1062 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1063 {
1064 dictitem_T *di;
1065 list_T *plist;
1066
1067 dict = li->li_tv.vval.v_dict;
1068 di = dict_find(dict, (char_u *)"props", -1);
1069 if (di != NULL)
1070 {
1071 if (di->di_tv.v_type != VAR_LIST)
1072 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001073 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001074 return;
1075 }
1076 plist = di->di_tv.vval.v_list;
1077 if (plist != NULL)
1078 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001079 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001080 {
1081 if (pli->li_tv.v_type != VAR_DICT)
1082 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001083 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001084 return;
1085 }
1086 dict = pli->li_tv.vval.v_dict;
1087 if (dict != NULL)
1088 {
1089 int col = dict_get_number(dict, (char_u *)"col");
1090
1091 prop_add_common( lnum, col, dict, buf, NULL);
1092 }
1093 }
1094 }
1095 }
1096 }
1097}
1098
1099/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001100 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1101 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001102 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001103popup_top_extra(win_T *wp)
1104{
1105 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1106
1107 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1108 return 1;
1109 return extra;
1110}
1111
1112/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001113 * Get the padding plus border at the left.
1114 */
1115 int
1116popup_left_extra(win_T *wp)
1117{
1118 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1119}
1120
1121/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001122 * Return the height of popup window "wp", including border and padding.
1123 */
1124 int
1125popup_height(win_T *wp)
1126{
1127 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001128 + popup_top_extra(wp)
1129 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001130}
1131
1132/*
1133 * Return the width of popup window "wp", including border, padding and
1134 * scrollbar.
1135 */
1136 int
1137popup_width(win_T *wp)
1138{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001139 // w_leftcol is how many columns of the core are left of the screen
1140 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001141 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001142 + popup_extra_width(wp)
1143 + wp->w_popup_rightoff;
1144}
1145
1146/*
1147 * Return the extra width of popup window "wp": border, padding and scrollbar.
1148 */
1149 int
1150popup_extra_width(win_T *wp)
1151{
1152 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1153 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1154 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001155}
1156
1157/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001158 * Adjust the position and size of the popup to fit on the screen.
1159 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001160 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001161popup_adjust_position(win_T *wp)
1162{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001163 linenr_T lnum;
1164 int wrapped = 0;
1165 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001166 int maxwidth_no_scrollbar;
1167 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001168 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001169 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001170 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001171 int center_vert = FALSE;
1172 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001173 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001174 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001175 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1176 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1177 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1178 int extra_height = top_extra + bot_extra;
1179 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001180 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001181 int org_winrow = wp->w_winrow;
1182 int org_wincol = wp->w_wincol;
1183 int org_width = wp->w_width;
1184 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001185 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001186 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001187 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001188 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001189 int wantline = wp->w_wantline; // adjusted for textprop
1190 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001191 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001192 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001193
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001194 wp->w_winrow = 0;
1195 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001196 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001197 wp->w_popup_leftoff = 0;
1198 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001199
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001200 // May need to update the "cursorline" highlighting, which may also change
1201 // "topline"
1202 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1203 popup_highlight_curline(wp);
1204
Bram Moolenaar12034e22019-08-25 22:25:02 +02001205 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1206 {
1207 win_T *prop_win = wp->w_popup_prop_win;
1208 textprop_T prop;
1209 linenr_T prop_lnum;
1210 pos_T pos;
1211 int screen_row;
1212 int screen_scol;
1213 int screen_ccol;
1214 int screen_ecol;
1215
1216 // Popup window is positioned relative to a text property.
1217 if (find_visible_prop(prop_win,
1218 wp->w_popup_prop_type, wp->w_popup_prop_id,
1219 &prop, &prop_lnum) == FAIL)
1220 {
1221 // Text property is no longer visible, hide the popup.
1222 // Unhiding the popup is done in check_popup_unhidden().
1223 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1224 {
1225 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001226 if (win_valid(wp->w_popup_prop_win))
1227 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1228 }
1229 return;
1230 }
1231
1232 // Compute the desired position from the position of the text
1233 // property. Use "wantline" and "wantcol" as offsets.
1234 pos.lnum = prop_lnum;
1235 pos.col = prop.tp_col;
1236 if (wp->w_popup_pos == POPPOS_TOPLEFT
1237 || wp->w_popup_pos == POPPOS_BOTLEFT)
1238 pos.col += prop.tp_len - 1;
1239 textpos2screenpos(prop_win, &pos, &screen_row,
1240 &screen_scol, &screen_ccol, &screen_ecol);
1241
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001242 if (screen_scol == 0)
1243 {
1244 // position is off screen, make the width zero to hide it.
1245 wp->w_width = 0;
1246 return;
1247 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001248 if (wp->w_popup_pos == POPPOS_TOPLEFT
1249 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1250 // below the text
1251 wantline = screen_row + wantline + 1;
1252 else
1253 // above the text
1254 wantline = screen_row + wantline - 1;
1255 center_vert = FALSE;
1256 if (wp->w_popup_pos == POPPOS_TOPLEFT
1257 || wp->w_popup_pos == POPPOS_BOTLEFT)
1258 // right of the text
1259 wantcol = screen_ecol + wantcol;
1260 else
1261 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001262 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001263 use_wantcol = TRUE;
1264 }
1265 else
1266 {
1267 // If no line was specified default to vertical centering.
1268 if (wantline == 0)
1269 center_vert = TRUE;
1270 else if (wantline < 0)
1271 // If "wantline" is negative it actually means zero.
1272 wantline = 0;
1273 if (wantcol < 0)
1274 // If "wantcol" is negative it actually means zero.
1275 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001276 }
1277
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001278 if (wp->w_popup_pos == POPPOS_CENTER)
1279 {
1280 // center after computing the size
1281 center_vert = TRUE;
1282 center_hor = TRUE;
1283 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001284 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001285 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001286 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1287 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001288 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001289 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001290 if (wp->w_winrow >= Rows)
1291 wp->w_winrow = Rows - 1;
1292 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001293
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001294 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001295 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001296 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1297 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001298 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001299 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001300 // Need to see at least one character after the decoration.
1301 if (wp->w_wincol > Columns - left_extra - 1)
1302 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001303 }
1304 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001305
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001306 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001307 // When left aligned use the space available, but shift to the left when we
1308 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001309 maxspace = Columns - wp->w_wincol - left_extra;
1310 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001311 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001312 {
1313 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001314 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001315 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001316
1317 if (wp->w_p_nu || wp->w_p_rnu)
1318 margin_width = number_width(wp) + 1;
1319#ifdef FEAT_FOLDING
1320 margin_width += wp->w_p_fdc;
1321#endif
1322#ifdef FEAT_SIGNS
1323 if (signcolumn_on(wp))
1324 margin_width += 2;
1325#endif
1326 if (margin_width >= maxwidth)
1327 margin_width = maxwidth - 1;
1328
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001329 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001330 minheight = wp->w_minheight;
1331#ifdef FEAT_TERMINAL
1332 // A terminal popup initially does not have content, use a default minimal
1333 // width of 20 characters and height of 5 lines.
1334 if (wp->w_buffer->b_term != NULL)
1335 {
1336 if (minwidth == 0)
1337 minwidth = 20;
1338 if (minheight == 0)
1339 minheight = 5;
1340 }
1341#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001342
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001343 if (wp->w_maxheight > 0)
1344 maxheight = wp->w_maxheight;
1345
Bram Moolenaar8d241042019-06-12 23:40:01 +02001346 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001347 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001348 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001349 if (wp->w_topline < 1)
1350 wp->w_topline = 1;
1351 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001352 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1353
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001354 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001355 // Use a minimum width of one, so that something shows when there is no
1356 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001357 // When "firstline" is -1 then start with the last buffer line and go
1358 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001359 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001360 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001361 if (wp->w_firstline < 0)
1362 lnum = wp->w_buffer->b_ml.ml_line_count;
1363 else
1364 lnum = wp->w_topline;
1365 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001366 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001367 int len;
1368 int w_width = wp->w_width;
1369
1370 // Count Tabs for what they are worth and compute the length based on
1371 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001372 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001373 if (wp->w_width < maxwidth)
1374 wp->w_width = maxwidth;
1375 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001376 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001377 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001378
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001379 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001380 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001381 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001382 {
1383 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001384 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001385 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001386 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001387 }
1388 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001389 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001390 && allow_adjust_left
1391 && (wp->w_popup_pos == POPPOS_TOPLEFT
1392 || wp->w_popup_pos == POPPOS_BOTLEFT))
1393 {
1394 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001395 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001396
Bram Moolenaar51c31312019-06-15 22:27:23 +02001397 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001398 {
1399 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001400
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001401 len -= truncate_shift;
1402 shift_by -= truncate_shift;
1403 }
1404
1405 wp->w_wincol -= shift_by;
1406 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001407 wp->w_width = maxwidth;
1408 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001409 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001410 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001411 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001412 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1413 wp->w_width = wp->w_maxwidth;
1414 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001415
1416 if (wp->w_firstline < 0)
1417 --lnum;
1418 else
1419 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001420
1421 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001422 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001423 && (wp->w_firstline >= 0
1424 ? lnum - wp->w_topline
1425 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001426 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001427 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001428 }
1429
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001430 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001431 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001432
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001433 wp->w_has_scrollbar = wp->w_want_scrollbar
1434 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001435#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001436 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1437 // Terminal window with running job never has a scrollbar, adjusts to
1438 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001439 wp->w_has_scrollbar = FALSE;
1440#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001441 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001442 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001443 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001444 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001445 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001446 // make space for the scrollbar if needed, when lines wrap and when
1447 // applying minwidth
1448 if (maxwidth + right_extra >= maxspace
1449 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1450 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001451 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001452
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001453 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1454 {
1455 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1456
1457 if (minwidth < title_len)
1458 minwidth = title_len;
1459 }
1460
1461 if (minwidth > 0 && wp->w_width < minwidth)
1462 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001463 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001464 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001465 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001466 // some columns cut off on the right
1467 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001468
1469 // If the window doesn't fit because 'minwidth' is set then the
1470 // scrollbar is at the far right of the screen, use the size without
1471 // the scrollbar.
1472 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1473 {
1474 int off = wp->w_width - maxwidth;
1475
1476 if (off > right_extra)
1477 extra_width -= right_extra;
1478 else
1479 extra_width -= off;
1480 wp->w_width = maxwidth_no_scrollbar;
1481 }
1482 else
1483 {
1484 wp->w_width = maxwidth;
1485
1486 // when adding a scrollbar below need to adjust the width
1487 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1488 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001489 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001490 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001491 {
1492 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1493 if (wp->w_wincol < 0)
1494 wp->w_wincol = 0;
1495 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001496 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1497 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1498 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001499 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001500
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001501 // Right aligned: move to the right if needed.
1502 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001503 if (leftoff >= 0)
1504 wp->w_wincol = leftoff;
1505 else if (wp->w_popup_fixed)
1506 {
1507 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001508 // columns when displaying the window, minus left border and
1509 // padding.
1510 if (-leftoff > left_extra)
1511 wp->w_leftcol = -leftoff - left_extra;
1512 wp->w_width -= wp->w_leftcol;
1513 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001514 if (wp->w_width < 0)
1515 wp->w_width = 0;
1516 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001517 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001518
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001519 if (wp->w_p_wrap || (!wp->w_popup_fixed
1520 && (wp->w_popup_pos == POPPOS_TOPLEFT
1521 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001522 {
1523 int want_col = 0;
1524
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001525 // try to show the right border and any scrollbar
1526 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001527 if (want_col > 0 && wp->w_wincol > 0
1528 && wp->w_wincol + want_col >= Columns)
1529 {
1530 wp->w_wincol = Columns - want_col;
1531 if (wp->w_wincol < 0)
1532 wp->w_wincol = 0;
1533 }
1534 }
1535
Bram Moolenaar8d241042019-06-12 23:40:01 +02001536 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1537 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001538 if (minheight > 0 && wp->w_height < minheight)
1539 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001540 if (maxheight > 0 && wp->w_height > maxheight)
1541 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001542 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001543 if (wp->w_height > Rows - wp->w_winrow)
1544 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001545
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001546 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001547 {
1548 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1549 if (wp->w_winrow < 0)
1550 wp->w_winrow = 0;
1551 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001552 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001553 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001554 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001555 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001556 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001557 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001558 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1559 {
1560 // Bottom aligned but does not fit, and less space on the other
1561 // side or "posinvert" is off: reduce height.
1562 wp->w_winrow = 0;
1563 wp->w_height = wantline - extra_height;
1564 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001565 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001566 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001567 // Not enough space and more space on the other side: make top
1568 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001569 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001570 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001571 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001572 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001573 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1574 || wp->w_popup_pos == POPPOS_TOPLEFT)
1575 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001576 if (wp != popup_dragwin
1577 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001578 && wantline * 2 > Rows
1579 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001580 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001581 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001582 // make bottom aligned and recompute the height
1583 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001584 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001585 if (wp->w_winrow < 0)
1586 {
1587 wp->w_height += wp->w_winrow;
1588 wp->w_winrow = 0;
1589 }
1590 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001591 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001592 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001593 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001594 adjust_height_for_top_aligned = TRUE;
1595 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001596 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001597
1598 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1599 && wp->w_winrow + wp->w_height + extra_height > Rows)
1600 {
1601 // Bottom of the popup goes below the last line, reduce the height and
1602 // add a scrollbar.
1603 wp->w_height = Rows - wp->w_winrow - extra_height;
1604#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001605 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001606#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001607 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001608 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001609 if (width_with_scrollbar > 0)
1610 wp->w_width = width_with_scrollbar;
1611 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001612 }
1613
1614 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001615 if (wp->w_winrow >= Rows)
1616 wp->w_winrow = Rows - 1;
1617 else if (wp->w_winrow < 0)
1618 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001619
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001620 if (wp->w_height != org_height)
1621 win_comp_scroll(wp);
1622
Bram Moolenaar17146962019-05-30 00:12:11 +02001623 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001624 if (win_valid(wp->w_popup_prop_win))
1625 {
1626 wp->w_popup_prop_changedtick =
1627 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1628 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1629 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001630
1631 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001632 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001633 if (org_winrow != wp->w_winrow
1634 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001635 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001636 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001637 || org_width != wp->w_width
1638 || org_height != wp->w_height)
1639 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001640 redraw_win_later(wp, NOT_VALID);
1641 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1642 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001643 popup_mask_refresh = TRUE;
1644 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001645}
1646
Bram Moolenaar17627312019-06-02 19:53:44 +02001647typedef enum
1648{
1649 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001650 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001651 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001652 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001653 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001654 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001655 TYPE_PREVIEW, // preview window
1656 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001657} create_type_T;
1658
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001659/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001660 * Make "buf" empty and set the contents to "text".
1661 * Used by popup_create() and popup_settext().
1662 */
1663 static void
1664popup_set_buffer_text(buf_T *buf, typval_T text)
1665{
1666 int lnum;
1667
1668 // Clear the buffer, then replace the lines.
1669 curbuf = buf;
1670 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001671 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001672 curbuf = curwin->w_buffer;
1673
1674 // Add text to the buffer.
1675 if (text.v_type == VAR_STRING)
1676 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001677 char_u *s = text.vval.v_string;
1678
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001679 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001680 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001681 }
1682 else
1683 {
1684 list_T *l = text.vval.v_list;
1685
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001686 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001687 {
1688 if (l->lv_first->li_tv.v_type == VAR_STRING)
1689 // list of strings
1690 add_popup_strings(buf, l);
1691 else
1692 // list of dictionaries
1693 add_popup_dicts(buf, l);
1694 }
1695 }
1696
1697 // delete the line that was in the empty buffer
1698 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001699 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001700 curbuf = curwin->w_buffer;
1701}
1702
1703/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001704 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1705 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001706 * Return FAIL if the parsing fails.
1707 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001708 static int
1709parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001710{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001711 char_u *p =
1712#ifdef FEAT_QUICKFIX
1713 !is_preview ? p_cpp :
1714#endif
1715 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001716
Bram Moolenaar258cef52019-08-21 17:29:29 +02001717 if (wp != NULL)
1718 wp->w_popup_flags &= ~POPF_INFO_MENU;
1719
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001720 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001721 {
1722 char_u *e, *dig;
1723 char_u *s = p;
1724 int x;
1725
1726 e = vim_strchr(p, ':');
1727 if (e == NULL || e[1] == NUL)
1728 return FAIL;
1729
1730 p = vim_strchr(e, ',');
1731 if (p == NULL)
1732 p = e + STRLEN(e);
1733 dig = e + 1;
1734 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001735
1736 if (STRNCMP(s, "height:", 7) == 0)
1737 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001738 if (dig != p)
1739 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001740 if (wp != NULL)
1741 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001742 if (is_preview)
1743 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001744 wp->w_maxheight = x;
1745 }
1746 }
1747 else if (STRNCMP(s, "width:", 6) == 0)
1748 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001749 if (dig != p)
1750 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001751 if (wp != NULL)
1752 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001753 if (is_preview)
1754 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001755 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001756 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001757 }
1758 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001759 else if (STRNCMP(s, "highlight:", 10) == 0)
1760 {
1761 if (wp != NULL)
1762 {
1763 int c = *p;
1764
1765 *p = NUL;
1766 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1767 s + 10, OPT_FREE|OPT_LOCAL, 0);
1768 *p = c;
1769 }
1770 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001771 else if (STRNCMP(s, "border:", 7) == 0)
1772 {
1773 char_u *arg = s + 7;
1774 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1775 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1776 int i;
1777
1778 if (!on && !off)
1779 return FAIL;
1780 if (wp != NULL)
1781 {
1782 for (i = 0; i < 4; ++i)
1783 wp->w_popup_border[i] = on ? 1 : 0;
1784 if (off)
1785 // only show the X for close when there is a border
1786 wp->w_popup_close = POPCLOSE_NONE;
1787 }
1788 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001789 else if (STRNCMP(s, "align:", 6) == 0)
1790 {
1791 char_u *arg = s + 6;
1792 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1793 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1794
1795 if (!menu && !item)
1796 return FAIL;
1797 if (wp != NULL && menu)
1798 wp->w_popup_flags |= POPF_INFO_MENU;
1799 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001800 else
1801 return FAIL;
1802 }
1803 return OK;
1804}
1805
1806/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001807 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1808 * is not NULL.
1809 * Return FAIL if the parsing fails.
1810 */
1811 int
1812parse_previewpopup(win_T *wp)
1813{
1814 return parse_popup_option(wp, TRUE);
1815}
1816
1817/*
1818 * Parse the 'completepopup' option and apply the values to window "wp" if it
1819 * is not NULL.
1820 * Return FAIL if the parsing fails.
1821 */
1822 int
1823parse_completepopup(win_T *wp)
1824{
1825 return parse_popup_option(wp, FALSE);
1826}
1827
1828/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001829 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001830 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001831 */
1832 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001833popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001834{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001835 poppos_T ppt = POPPOS_NONE;
1836
1837 if (d != NULL)
1838 ppt = get_pos_entry(d, FALSE);
1839
Bram Moolenaar79648732019-07-18 21:43:07 +02001840 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001841 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001842 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001843 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1844 }
1845 else
1846 {
1847 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1848 if (wp->w_wantline == 0) // cursor in first line
1849 {
1850 wp->w_wantline = 2;
1851 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1852 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1853 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001854 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001855
Bram Moolenaar79648732019-07-18 21:43:07 +02001856 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001857 if (wp->w_wantcol > Columns - width)
1858 {
1859 wp->w_wantcol = Columns - width;
1860 if (wp->w_wantcol < 1)
1861 wp->w_wantcol = 1;
1862 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001863
Bram Moolenaar79648732019-07-18 21:43:07 +02001864 popup_adjust_position(wp);
1865}
1866
1867/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001868 * Set w_wantline and w_wantcol for the a given screen position.
1869 * Caller must take care of running into the window border.
1870 */
1871 void
1872popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1873{
1874 wp->w_wantline = row;
1875 wp->w_wantcol = col;
1876 popup_adjust_position(wp);
1877}
1878
1879/*
1880 * Add a border and lef&right padding.
1881 */
1882 static void
1883add_border_left_right_padding(win_T *wp)
1884{
1885 int i;
1886
1887 for (i = 0; i < 4; ++i)
1888 {
1889 wp->w_popup_border[i] = 1;
1890 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1891 }
1892}
1893
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001894#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001895/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001896 * Return TRUE if there is any popup window with a terminal buffer.
1897 */
1898 static int
1899popup_terminal_exists(void)
1900{
1901 win_T *wp;
1902 tabpage_T *tp;
1903
1904 FOR_ALL_POPUPWINS(wp)
1905 if (wp->w_buffer->b_term != NULL)
1906 return TRUE;
1907 FOR_ALL_TABPAGES(tp)
1908 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1909 if (wp->w_buffer->b_term != NULL)
1910 return TRUE;
1911 return FALSE;
1912}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001913#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001914
1915/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001916 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001917 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001918 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001919 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001920 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001921 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001922popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001923{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001924 win_T *wp;
1925 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001926 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001927 int new_buffer;
1928 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001929 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001930 int nr;
1931 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001932
Bram Moolenaar79648732019-07-18 21:43:07 +02001933 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001934 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001935 if (in_vim9script()
1936 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1937 || check_for_dict_arg(argvars, 1) == FAIL))
1938 return NULL;
1939
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001940 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001941 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001942 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001943 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001944 if (buf == NULL)
1945 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001946 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001947 return NULL;
1948 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001949#ifdef FEAT_TERMINAL
1950 if (buf->b_term != NULL && popup_terminal_exists())
1951 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001952 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001953 return NULL;
1954 }
1955#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001956 }
1957 else if (!(argvars[0].v_type == VAR_STRING
1958 && argvars[0].vval.v_string != NULL)
1959 && !(argvars[0].v_type == VAR_LIST
1960 && argvars[0].vval.v_list != NULL))
1961 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001962 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001963 return NULL;
1964 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001965 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001966 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001967 emsg(_(e_dictionary_required));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001968 return NULL;
1969 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001970 d = argvars[1].vval.v_dict;
1971 }
1972
1973 if (d != NULL)
1974 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001975 if (dict_has_key(d, "tabpage"))
Bram Moolenaar79648732019-07-18 21:43:07 +02001976 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1977 else if (type == TYPE_NOTIFICATION)
1978 tabnr = -1; // notifications are global by default
1979 else
1980 tabnr = 0;
1981 if (tabnr > 0)
1982 {
1983 tp = find_tabpage(tabnr);
1984 if (tp == NULL)
1985 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00001986 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02001987 return NULL;
1988 }
1989 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001990 }
1991
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001992 // Create the window and buffer.
1993 wp = win_alloc_popup_win();
1994 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001995 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001996 if (rettv != NULL)
1997 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001998 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001999 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002000
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002001 if (buf != NULL)
2002 {
2003 // use existing buffer
2004 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002005 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002006 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002007 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002008 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002009 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002010 }
2011 else
2012 {
2013 // create a new buffer associated with the popup
2014 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002015 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002016 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002017 {
2018 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002019 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002020 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002021 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002022
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002023 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002024
Bram Moolenaar46451042019-08-24 15:50:46 +02002025 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002026 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002027 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002028 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002029 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002030 buf->b_p_ul = -1; // no undo
2031 buf->b_p_swf = FALSE; // no swap file
2032 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002033 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002034
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002035 // Avoid that 'buftype' is reset when this buffer is entered.
2036 buf->b_p_initialized = TRUE;
2037 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002038 wp->w_p_wrap = TRUE; // 'wrap' is default on
2039 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002040
Bram Moolenaara3fce622019-06-20 02:31:49 +02002041 if (tp != NULL)
2042 {
2043 // popup on specified tab page
2044 wp->w_next = tp->tp_first_popupwin;
2045 tp->tp_first_popupwin = wp;
2046 }
2047 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002048 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002049 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002050 wp->w_next = curtab->tp_first_popupwin;
2051 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002052 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002053 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002054 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002055 win_T *prev = first_popupwin;
2056
2057 // Global popup: add at the end, so that it gets displayed on top of
2058 // older ones with the same zindex. Matters for notifications.
2059 if (first_popupwin == NULL)
2060 first_popupwin = wp;
2061 else
2062 {
2063 while (prev->w_next != NULL)
2064 prev = prev->w_next;
2065 prev->w_next = wp;
2066 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002067 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002068
Bram Moolenaar79648732019-07-18 21:43:07 +02002069 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002070 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002071
Bram Moolenaar79648732019-07-18 21:43:07 +02002072 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002073 {
2074 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002075 }
2076 if (type == TYPE_ATCURSOR)
2077 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002078 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002079 set_moved_values(wp);
2080 set_moved_columns(wp, FIND_STRING);
2081 }
2082
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002083 if (type == TYPE_BEVAL)
2084 {
2085 wp->w_popup_pos = POPPOS_BOTLEFT;
2086
2087 // by default use the mouse position
2088 wp->w_wantline = mouse_row;
2089 if (wp->w_wantline <= 0) // mouse on first line
2090 {
2091 wp->w_wantline = 2;
2092 wp->w_popup_pos = POPPOS_TOPLEFT;
2093 }
2094 wp->w_wantcol = mouse_col + 1;
2095 set_mousemoved_values(wp);
2096 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2097 }
2098
Bram Moolenaar33796b32019-06-08 16:01:13 +02002099 // set default values
2100 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002101 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002102
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002103 if (type == TYPE_NOTIFICATION)
2104 {
2105 win_T *twp, *nextwin;
2106 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002107
2108 // Try to not overlap with another global popup. Guess we need 3
2109 // more screen lines than buffer lines.
2110 wp->w_wantline = 1;
2111 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2112 {
2113 nextwin = twp->w_next;
2114 if (twp != wp
2115 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2116 && twp->w_winrow <= wp->w_wantline - 1 + height
2117 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2118 {
2119 // move to below this popup and restart the loop to check for
2120 // overlap with other popups
2121 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2122 nextwin = first_popupwin;
2123 }
2124 }
2125 if (wp->w_wantline + height > Rows)
2126 {
2127 // can't avoid overlap, put on top in the hope that message goes
2128 // away soon.
2129 wp->w_wantline = 1;
2130 }
2131
2132 wp->w_wantcol = 10;
2133 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002134 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002135 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002136 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002137 for (i = 0; i < 4; ++i)
2138 wp->w_popup_border[i] = 1;
2139 wp->w_popup_padding[1] = 1;
2140 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002141
2142 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002143 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002144 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2145 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002146 }
2147
Bram Moolenaara730e552019-06-16 19:05:31 +02002148 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002149 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002150 wp->w_popup_pos = POPPOS_CENTER;
2151 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002152 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002153 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002154 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002155 }
2156
Bram Moolenaara730e552019-06-16 19:05:31 +02002157 if (type == TYPE_MENU)
2158 {
2159 typval_T tv;
2160 callback_T callback;
2161
2162 tv.v_type = VAR_STRING;
2163 tv.vval.v_string = (char_u *)"popup_filter_menu";
2164 callback = get_callback(&tv);
2165 if (callback.cb_name != NULL)
2166 set_callback(&wp->w_filter_cb, &callback);
2167
2168 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002169 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002170 }
2171
Bram Moolenaar79648732019-07-18 21:43:07 +02002172 if (type == TYPE_PREVIEW)
2173 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002174 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002175 wp->w_popup_close = POPCLOSE_BUTTON;
2176 for (i = 0; i < 4; ++i)
2177 wp->w_popup_border[i] = 1;
2178 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002179 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002180 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002181# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002182 if (type == TYPE_INFO)
2183 {
2184 wp->w_popup_pos = POPPOS_TOPLEFT;
2185 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2186 wp->w_popup_close = POPCLOSE_BUTTON;
2187 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002188 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002189 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002190# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002191
Bram Moolenaarae943152019-06-16 22:54:14 +02002192 for (i = 0; i < 4; ++i)
2193 VIM_CLEAR(wp->w_border_highlight[i]);
2194 for (i = 0; i < 8; ++i)
2195 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002196 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002197 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002198 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002199
Bram Moolenaar79648732019-07-18 21:43:07 +02002200 if (d != NULL)
2201 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002202 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002203
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002204#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002205 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2206 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002207#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002208
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002209 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002210
2211 wp->w_vsep_width = 0;
2212
2213 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002214 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002215
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002216#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002217 // When running a terminal in the popup it becomes the current window.
2218 if (buf->b_term != NULL)
2219 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002220#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002221
Bram Moolenaara730e552019-06-16 19:05:31 +02002222 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002223}
2224
2225/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002226 * popup_clear()
2227 */
2228 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002229f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002230{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002231 int force = FALSE;
2232
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002233 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2234 return;
2235
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002236 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002237 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002238 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002239}
2240
2241/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002242 * popup_create({text}, {options})
2243 */
2244 void
2245f_popup_create(typval_T *argvars, typval_T *rettv)
2246{
Bram Moolenaar17627312019-06-02 19:53:44 +02002247 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002248}
2249
2250/*
2251 * popup_atcursor({text}, {options})
2252 */
2253 void
2254f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2255{
Bram Moolenaar17627312019-06-02 19:53:44 +02002256 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002257}
2258
2259/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002260 * popup_beval({text}, {options})
2261 */
2262 void
2263f_popup_beval(typval_T *argvars, typval_T *rettv)
2264{
2265 popup_create(argvars, rettv, TYPE_BEVAL);
2266}
2267
2268/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002269 * Invoke the close callback for window "wp" with value "result".
2270 * Careful: The callback may make "wp" invalid!
2271 */
2272 static void
2273invoke_popup_callback(win_T *wp, typval_T *result)
2274{
2275 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002276 typval_T argv[3];
2277
2278 argv[0].v_type = VAR_NUMBER;
2279 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2280
2281 if (result != NULL && result->v_type != VAR_UNKNOWN)
2282 copy_tv(result, &argv[1]);
2283 else
2284 {
2285 argv[1].v_type = VAR_NUMBER;
2286 argv[1].vval.v_number = 0;
2287 }
2288
2289 argv[2].v_type = VAR_UNKNOWN;
2290
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002291 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002292 if (result != NULL)
2293 clear_tv(&argv[1]);
2294 clear_tv(&rettv);
2295}
2296
2297/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002298 * Make "prevwin" the current window, unless it's equal to "wp".
2299 * Otherwise make "firstwin" the current window.
2300 */
2301 static void
2302back_to_prevwin(win_T *wp)
2303{
2304 if (win_valid(prevwin) && wp != prevwin)
2305 win_enter(prevwin, FALSE);
2306 else
2307 win_enter(firstwin, FALSE);
2308}
2309
2310/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002311 * Close popup "wp" and invoke any close callback for it.
2312 */
2313 static void
2314popup_close_and_callback(win_T *wp, typval_T *arg)
2315{
2316 int id = wp->w_id;
2317
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002318#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002319 if (wp == curwin && curbuf->b_term != NULL)
2320 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002321 win_T *owp;
2322
2323 // Closing popup window with a terminal: put focus back on the first
2324 // that works:
2325 // - another popup window with a terminal
2326 // - the previous window
2327 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002328 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002329 if (owp != curwin && owp->w_buffer->b_term != NULL)
2330 break;
2331 if (owp != NULL)
2332 win_enter(owp, FALSE);
2333 else
2334 {
2335 for (owp = curtab->tp_first_popupwin; owp != NULL;
2336 owp = owp->w_next)
2337 if (owp != curwin && owp->w_buffer->b_term != NULL)
2338 break;
2339 if (owp != NULL)
2340 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002341 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002342 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002343 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002344 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002345#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002346
Bram Moolenaar49540192019-12-11 19:34:54 +01002347 // Just in case a check higher up is missing.
2348 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002349 {
2350 // To avoid getting stuck when win_execute() does something that causes
2351 // an error, stop calling the filter callback.
2352 free_callback(&wp->w_filter_cb);
2353
Bram Moolenaar49540192019-12-11 19:34:54 +01002354 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002355 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002356
Bram Moolenaarcee52202020-03-11 14:19:58 +01002357 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002358 if (wp->w_close_cb.cb_name != NULL)
2359 // Careful: This may make "wp" invalid.
2360 invoke_popup_callback(wp, arg);
2361
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002362 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002363 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002364}
2365
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002366 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002367popup_close_with_retval(win_T *wp, int retval)
2368{
2369 typval_T res;
2370
2371 res.v_type = VAR_NUMBER;
2372 res.vval.v_number = retval;
2373 popup_close_and_callback(wp, &res);
2374}
2375
Bram Moolenaar3397f742019-06-02 18:40:06 +02002376/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002377 * Close popup "wp" because of a mouse click.
2378 */
2379 void
2380popup_close_for_mouse_click(win_T *wp)
2381{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002382 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002383}
2384
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002385 static void
2386check_mouse_moved(win_T *wp, win_T *mouse_wp)
2387{
2388 // Close the popup when all if these are true:
2389 // - the mouse is not on this popup
2390 // - "mousemoved" was used
2391 // - the mouse is no longer on the same screen row or the mouse column is
2392 // outside of the relevant text
2393 if (wp != mouse_wp
2394 && wp->w_popup_mouse_row != 0
2395 && (wp->w_popup_mouse_row != mouse_row
2396 || mouse_col < wp->w_popup_mouse_mincol
2397 || mouse_col > wp->w_popup_mouse_maxcol))
2398 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002399 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002400 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002401 }
2402}
2403
2404/*
2405 * Called when the mouse moved: may close a popup with "mousemoved".
2406 */
2407 void
2408popup_handle_mouse_moved(void)
2409{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002410 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002411 win_T *mouse_wp;
2412 int row = mouse_row;
2413 int col = mouse_col;
2414
2415 // find the window where the mouse is in
2416 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2417
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002418 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2419 {
2420 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002421 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002422 }
2423 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2424 {
2425 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002426 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002427 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002428}
2429
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002430/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002431 * In a filter: check if the typed key is a mouse event that is used for
2432 * dragging the popup.
2433 */
2434 static void
2435filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2436{
2437 int row = mouse_row;
2438 int col = mouse_col;
2439
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002440 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002441 && is_mouse_key(c)
2442 && (wp == popup_dragwin
2443 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2444 // do not consume the key, allow for dragging the popup
2445 rettv->vval.v_number = 0;
2446}
2447
Bram Moolenaara730e552019-06-16 19:05:31 +02002448/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002449 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002450 */
2451 void
2452f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2453{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002454 int id;
2455 win_T *wp;
2456 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002457 typval_T res;
2458 int c;
2459 linenr_T old_lnum;
2460
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002461 if (in_vim9script()
2462 && (check_for_number_arg(argvars, 0) == FAIL
2463 || check_for_string_arg(argvars, 1) == FAIL))
2464 return;
2465
2466 id = tv_get_number(&argvars[0]);
2467 wp = win_id2wp(id);
2468 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002469 // If the popup has been closed do not consume the key.
2470 if (wp == NULL)
2471 return;
2472
2473 c = *key;
2474 if (c == K_SPECIAL && key[1] != NUL)
2475 c = TO_SPECIAL(key[1], key[2]);
2476
2477 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002478 rettv->v_type = VAR_BOOL;
2479 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002480 res.v_type = VAR_NUMBER;
2481
2482 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002483 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2484 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002485 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002486 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002487 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2488 ++wp->w_cursor.lnum;
2489 if (old_lnum != wp->w_cursor.lnum)
2490 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002491 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002492 return;
2493 }
2494
2495 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2496 {
2497 // Cancelled, invoke callback with -1
2498 res.vval.v_number = -1;
2499 popup_close_and_callback(wp, &res);
2500 return;
2501 }
2502 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2503 {
2504 // Invoke callback with current index.
2505 res.vval.v_number = wp->w_cursor.lnum;
2506 popup_close_and_callback(wp, &res);
2507 return;
2508 }
2509
2510 filter_handle_drag(wp, c, rettv);
2511}
2512
2513/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002514 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002515 */
2516 void
2517f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2518{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002519 int id;
2520 win_T *wp;
2521 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002522 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002523 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002524
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002525 if (in_vim9script()
2526 && (check_for_number_arg(argvars, 0) == FAIL
2527 || check_for_string_arg(argvars, 1) == FAIL))
2528 return;
2529
2530 id = tv_get_number(&argvars[0]);
2531 wp = win_id2wp(id);
2532 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002533 // If the popup has been closed don't consume the key.
2534 if (wp == NULL)
2535 return;
2536
Bram Moolenaara730e552019-06-16 19:05:31 +02002537 c = *key;
2538 if (c == K_SPECIAL && key[1] != NUL)
2539 c = TO_SPECIAL(key[1], key[2]);
2540
Bram Moolenaara42d9452019-06-15 21:46:30 +02002541 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002542 rettv->v_type = VAR_BOOL;
2543 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002544
Bram Moolenaara730e552019-06-16 19:05:31 +02002545 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002546 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002547 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002548 res.vval.v_number = 0;
2549 else
2550 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002551 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002552 return;
2553 }
2554
2555 // Invoke callback
2556 res.v_type = VAR_NUMBER;
2557 popup_close_and_callback(wp, &res);
2558}
2559
2560/*
2561 * popup_dialog({text}, {options})
2562 */
2563 void
2564f_popup_dialog(typval_T *argvars, typval_T *rettv)
2565{
2566 popup_create(argvars, rettv, TYPE_DIALOG);
2567}
2568
2569/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002570 * popup_menu({text}, {options})
2571 */
2572 void
2573f_popup_menu(typval_T *argvars, typval_T *rettv)
2574{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002575 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002576}
2577
2578/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002579 * popup_notification({text}, {options})
2580 */
2581 void
2582f_popup_notification(typval_T *argvars, typval_T *rettv)
2583{
2584 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2585}
2586
2587/*
2588 * Find the popup window with window-ID "id".
2589 * If the popup window does not exist NULL is returned.
2590 * If the window is not a popup window, and error message is given.
2591 */
2592 static win_T *
2593find_popup_win(int id)
2594{
2595 win_T *wp = win_id2wp(id);
2596
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002597 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002598 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002599 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002600 return NULL;
2601 }
2602 return wp;
2603}
2604
2605/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002606 * popup_close({id})
2607 */
2608 void
2609f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2610{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002611 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002612 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002613
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002614 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2615 return;
2616
2617 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002618 if (
2619# ifdef FEAT_TERMINAL
2620 // if the popup contains a terminal it will become hidden
2621 curbuf->b_term == NULL &&
2622# endif
2623 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002624 return;
2625
2626 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002627 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002628 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002629}
2630
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002631 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002632popup_hide(win_T *wp)
2633{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002634#ifdef FEAT_TERMINAL
2635 if (error_if_term_popup_window())
2636 return;
2637#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002638 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2639 {
2640 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002641 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002642 redraw_all_later(NOT_VALID);
2643 popup_mask_refresh = TRUE;
2644 }
2645}
2646
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002647/*
2648 * popup_hide({id})
2649 */
2650 void
2651f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2652{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002653 int id;
2654 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002655
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002656 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2657 return;
2658
2659 id = (int)tv_get_number(argvars);
2660 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002661 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002662 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002663 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002664 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2665 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002666}
2667
2668 void
2669popup_show(win_T *wp)
2670{
2671 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002672 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002673 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002674 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002675 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002676 }
2677}
2678
2679/*
2680 * popup_show({id})
2681 */
2682 void
2683f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2684{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002685 int id;
2686 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002687
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002688 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2689 return;
2690
2691 id = (int)tv_get_number(argvars);
2692 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002693 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002694 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002695 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002696 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002697#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002698 if (wp->w_popup_flags & POPF_INFO)
2699 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002700#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002701 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002702}
2703
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002704/*
2705 * popup_settext({id}, {text})
2706 */
2707 void
2708f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2709{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002710 int id;
2711 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002712
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002713 if (in_vim9script()
2714 && (check_for_number_arg(argvars, 0) == FAIL
2715 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2716 return;
2717
2718 id = (int)tv_get_number(&argvars[0]);
2719 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002720 if (wp != NULL)
2721 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002722 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002723 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002724 else
2725 {
2726 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002727 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002728 popup_adjust_position(wp);
2729 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002730 }
2731}
2732
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002733 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002734popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002735{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002736 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002737 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002738 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002739 clear_cmdline = TRUE;
2740 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002741
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002742 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002743 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002744}
2745
Bram Moolenaar82106932020-03-13 14:34:38 +01002746 static void
2747error_for_popup_window(void)
2748{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002749 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002750}
2751
2752 int
2753error_if_popup_window(int also_with_term UNUSED)
2754{
2755 // win_execute() may set "curwin" to a popup window temporarily, but many
2756 // commands are disallowed then. When a terminal runs in the popup most
2757 // things are allowed. When a terminal is finished it can be closed.
2758 if (WIN_IS_POPUP(curwin)
2759# ifdef FEAT_TERMINAL
2760 && (also_with_term || curbuf->b_term == NULL)
2761 && !term_is_finished(curbuf)
2762# endif
2763 )
2764 {
2765 error_for_popup_window();
2766 return TRUE;
2767 }
2768 return FALSE;
2769}
2770
Bram Moolenaarec583842019-05-26 14:11:23 +02002771/*
2772 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002773 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002774 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002775 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002776 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002777popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002778{
2779 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002780 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002781 win_T *prev = NULL;
2782
Bram Moolenaarec583842019-05-26 14:11:23 +02002783 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002784 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002785 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002786 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002787 if (wp == curwin)
2788 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002789 if (!force)
2790 {
2791 error_for_popup_window();
2792 return FAIL;
2793 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002794 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002795 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002796 if (prev == NULL)
2797 first_popupwin = wp->w_next;
2798 else
2799 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002800 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002801 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002802 }
2803
Bram Moolenaarec583842019-05-26 14:11:23 +02002804 // go through tab-local popups
2805 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002806 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002807 return OK;
2808 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002809}
2810
2811/*
2812 * Close a popup window with Window-id "id" in tabpage "tp".
2813 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002814 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002815popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002816{
2817 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002818 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002819 win_T *prev = NULL;
2820
Bram Moolenaarec583842019-05-26 14:11:23 +02002821 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2822 if (wp->w_id == id)
2823 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002824 if (wp == curwin)
2825 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002826 if (!force)
2827 {
2828 error_for_popup_window();
2829 return FAIL;
2830 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002831 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002832 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002833 if (prev == NULL)
2834 *root = wp->w_next;
2835 else
2836 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002837 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002838 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002839 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002840 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002841}
2842
2843 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002844close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002845{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002846 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002847 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002848 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002849 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002850 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002851 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002852 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002853 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002854}
2855
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002856/*
2857 * popup_move({id}, {options})
2858 */
2859 void
2860f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2861{
Bram Moolenaarae943152019-06-16 22:54:14 +02002862 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002863 int id;
2864 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002865
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002866 if (in_vim9script()
2867 && (check_for_number_arg(argvars, 0) == FAIL
2868 || check_for_dict_arg(argvars, 1) == FAIL))
2869 return;
2870
2871 id = (int)tv_get_number(argvars);
2872 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002873 if (wp == NULL)
2874 return; // invalid {id}
2875
2876 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2877 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002878 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002879 return;
2880 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002881 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002882
Bram Moolenaarae943152019-06-16 22:54:14 +02002883 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002884
2885 if (wp->w_winrow + wp->w_height >= cmdline_row)
2886 clear_cmdline = TRUE;
2887 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002888}
2889
Bram Moolenaarbc133542019-05-29 20:26:46 +02002890/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002891 * popup_setoptions({id}, {options})
2892 */
2893 void
2894f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2895{
2896 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002897 int id;
2898 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002899 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002900
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002901 if (in_vim9script()
2902 && (check_for_number_arg(argvars, 0) == FAIL
2903 || check_for_dict_arg(argvars, 1) == FAIL))
2904 return;
2905
2906 id = (int)tv_get_number(argvars);
2907 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002908 if (wp == NULL)
2909 return; // invalid {id}
2910
2911 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2912 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002913 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002914 return;
2915 }
2916 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002917 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002918
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002919 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002920
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002921 if (old_firstline != wp->w_firstline)
2922 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002923 popup_adjust_position(wp);
2924}
2925
2926/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002927 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002928 */
2929 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002930f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002931{
2932 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002933 int id;
2934 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002935 int top_extra;
2936 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002937
2938 if (rettv_dict_alloc(rettv) == OK)
2939 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002940 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2941 return;
2942
2943 id = (int)tv_get_number(argvars);
2944 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002945 if (wp == NULL)
2946 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002947 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002948 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2949
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002950 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002951 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002952 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002953
Bram Moolenaarbc133542019-05-29 20:26:46 +02002954 dict_add_number(dict, "line", wp->w_winrow + 1);
2955 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002956 dict_add_number(dict, "width", wp->w_width + left_extra
2957 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2958 dict_add_number(dict, "height", wp->w_height + top_extra
2959 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002960
2961 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2962 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2963 dict_add_number(dict, "core_width", wp->w_width);
2964 dict_add_number(dict, "core_height", wp->w_height);
2965
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002966 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002967 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002968 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002969 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002970 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002971
2972 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002973 }
2974}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002975
2976/*
2977 * popup_list()
2978 */
2979 void
2980f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2981{
2982 win_T *wp;
2983 tabpage_T *tp;
2984
2985 if (rettv_list_alloc(rettv) != OK)
2986 return;
2987 FOR_ALL_POPUPWINS(wp)
2988 list_append_number(rettv->vval.v_list, wp->w_id);
2989 FOR_ALL_TABPAGES(tp)
2990 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2991 list_append_number(rettv->vval.v_list, wp->w_id);
2992}
2993
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002994/*
2995 * popup_locate({row}, {col})
2996 */
2997 void
2998f_popup_locate(typval_T *argvars, typval_T *rettv)
2999{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003000 int row;
3001 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003002 win_T *wp;
3003
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003004 if (in_vim9script()
3005 && (check_for_number_arg(argvars, 0) == FAIL
3006 || check_for_number_arg(argvars, 1) == FAIL))
3007 return;
3008
3009 row = tv_get_number(&argvars[0]) - 1;
3010 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003011 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003012 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003013 rettv->vval.v_number = wp->w_id;
3014}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003015
3016/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003017 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3018 */
3019 static void
3020get_padding_border(dict_T *dict, int *array, char *name)
3021{
3022 list_T *list;
3023 int i;
3024
3025 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3026 return;
3027
3028 list = list_alloc();
3029 if (list != NULL)
3030 {
3031 dict_add_list(dict, name, list);
3032 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3033 for (i = 0; i < 4; ++i)
3034 list_append_number(list, array[i]);
3035 }
3036}
3037
3038/*
3039 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3040 */
3041 static void
3042get_borderhighlight(dict_T *dict, win_T *wp)
3043{
3044 list_T *list;
3045 int i;
3046
3047 for (i = 0; i < 4; ++i)
3048 if (wp->w_border_highlight[i] != NULL)
3049 break;
3050 if (i == 4)
3051 return;
3052
3053 list = list_alloc();
3054 if (list != NULL)
3055 {
3056 dict_add_list(dict, "borderhighlight", list);
3057 for (i = 0; i < 4; ++i)
3058 list_append_string(list, wp->w_border_highlight[i], -1);
3059 }
3060}
3061
3062/*
3063 * For popup_getoptions(): add a "borderchars" entry to "dict".
3064 */
3065 static void
3066get_borderchars(dict_T *dict, win_T *wp)
3067{
3068 list_T *list;
3069 int i;
3070 char_u buf[NUMBUFLEN];
3071 int len;
3072
3073 for (i = 0; i < 8; ++i)
3074 if (wp->w_border_char[i] != 0)
3075 break;
3076 if (i == 8)
3077 return;
3078
3079 list = list_alloc();
3080 if (list != NULL)
3081 {
3082 dict_add_list(dict, "borderchars", list);
3083 for (i = 0; i < 8; ++i)
3084 {
3085 len = mb_char2bytes(wp->w_border_char[i], buf);
3086 list_append_string(list, buf, len);
3087 }
3088 }
3089}
3090
3091/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003092 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003093 */
3094 static void
3095get_moved_list(dict_T *dict, win_T *wp)
3096{
3097 list_T *list;
3098
3099 list = list_alloc();
3100 if (list != NULL)
3101 {
3102 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003103 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003104 list_append_number(list, wp->w_popup_mincol);
3105 list_append_number(list, wp->w_popup_maxcol);
3106 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003107 list = list_alloc();
3108 if (list != NULL)
3109 {
3110 dict_add_list(dict, "mousemoved", list);
3111 list_append_number(list, wp->w_popup_mouse_row);
3112 list_append_number(list, wp->w_popup_mouse_mincol);
3113 list_append_number(list, wp->w_popup_mouse_maxcol);
3114 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003115}
3116
3117/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003118 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003119 */
3120 void
3121f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3122{
3123 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003124 int id;
3125 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003126 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003127 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003128
3129 if (rettv_dict_alloc(rettv) == OK)
3130 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003131 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3132 return;
3133
3134 id = (int)tv_get_number(argvars);
3135 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003136 if (wp == NULL)
3137 return;
3138
3139 dict = rettv->vval.v_dict;
3140 dict_add_number(dict, "line", wp->w_wantline);
3141 dict_add_number(dict, "col", wp->w_wantcol);
3142 dict_add_number(dict, "minwidth", wp->w_minwidth);
3143 dict_add_number(dict, "minheight", wp->w_minheight);
3144 dict_add_number(dict, "maxheight", wp->w_maxheight);
3145 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003146 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003147 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003148 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003149 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003150 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003151 {
3152 proptype_T *pt = text_prop_type_by_id(
3153 wp->w_popup_prop_win->w_buffer,
3154 wp->w_popup_prop_type);
3155
3156 if (pt != NULL)
3157 dict_add_string(dict, "textprop", pt->pt_name);
3158 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3159 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3160 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003161 dict_add_string(dict, "title", wp->w_popup_title);
3162 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003163 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003164 dict_add_number(dict, "dragall",
3165 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003166 dict_add_number(dict, "mapping",
3167 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003168 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003169 dict_add_number(dict, "posinvert",
3170 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003171 dict_add_number(dict, "cursorline",
3172 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003173 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003174 if (wp->w_scrollbar_highlight != NULL)
3175 dict_add_string(dict, "scrollbarhighlight",
3176 wp->w_scrollbar_highlight);
3177 if (wp->w_thumb_highlight != NULL)
3178 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003179
Bram Moolenaara3fce622019-06-20 02:31:49 +02003180 // find the tabpage that holds this popup
3181 i = 1;
3182 FOR_ALL_TABPAGES(tp)
3183 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003184 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003185
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003186 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3187 if (twp->w_id == id)
3188 break;
3189 if (twp != NULL)
3190 break;
3191 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003192 }
3193 if (tp == NULL)
3194 i = -1; // must be global
3195 else if (tp == curtab)
3196 i = 0;
3197 dict_add_number(dict, "tabpage", i);
3198
Bram Moolenaarae943152019-06-16 22:54:14 +02003199 get_padding_border(dict, wp->w_popup_padding, "padding");
3200 get_padding_border(dict, wp->w_popup_border, "border");
3201 get_borderhighlight(dict, wp);
3202 get_borderchars(dict, wp);
3203 get_moved_list(dict, wp);
3204
3205 if (wp->w_filter_cb.cb_name != NULL)
3206 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3207 if (wp->w_close_cb.cb_name != NULL)
3208 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003209
K.Takataeeec2542021-06-02 13:28:16 +02003210 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003211 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3212 {
3213 dict_add_string(dict, "pos",
3214 (char_u *)poppos_entries[i].pp_name);
3215 break;
3216 }
3217
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003218 dict_add_string(dict, "close", (char_u *)(
3219 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3220 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3221
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003222# if defined(FEAT_TIMERS)
3223 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3224 ? (long)wp->w_popup_timer->tr_interval : 0L);
3225# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003226 }
3227}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003228
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003229# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003230/*
3231 * Return TRUE if the current window is running a terminal in a popup window.
3232 * Return FALSE when the job has ended.
3233 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003234 int
3235error_if_term_popup_window()
3236{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003237 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3238 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003239 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003240 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003241 return TRUE;
3242 }
3243 return FALSE;
3244}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003245# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003246
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003247/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003248 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003249 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003250 * Each calling function should use a different flag, see the list at
3251 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003252 */
3253 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003254popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003255{
3256 win_T *wp;
3257
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003258 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003259 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003260 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003261 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003262}
3263
3264/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003265 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003266 * Must have called popup_reset_handled() first.
3267 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3268 * popup with the highest zindex.
3269 */
3270 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003271find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003272{
3273 win_T *wp;
3274 win_T *found_wp;
3275 int found_zindex;
3276
3277 found_zindex = lowest ? INT_MAX : 0;
3278 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003279 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003280 if ((wp->w_popup_handled & handled_flag) == 0
3281 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003282 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003283 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003284 {
3285 found_zindex = wp->w_zindex;
3286 found_wp = wp;
3287 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003288 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003289 if ((wp->w_popup_handled & handled_flag) == 0
3290 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003291 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003292 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003293 {
3294 found_zindex = wp->w_zindex;
3295 found_wp = wp;
3296 }
3297
3298 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003299 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003300 return found_wp;
3301}
3302
3303/*
3304 * Invoke the filter callback for window "wp" with typed character "c".
3305 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003306 * Returns the return value of the filter or -1 for CTRL-C in the current
3307 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003308 * Careful: The filter may make "wp" invalid!
3309 */
3310 static int
3311invoke_popup_filter(win_T *wp, int c)
3312{
3313 int res;
3314 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003315 typval_T argv[3];
3316 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003317 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003318 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003319
Bram Moolenaara42d9452019-06-15 21:46:30 +02003320 // Emergency exit: CTRL-C closes the popup.
3321 if (c == Ctrl_C)
3322 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003323 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003324 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003325
3326 // Reset got_int to avoid the callback isn't called.
3327 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003328 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003329 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003330
3331 // If the popup is the current window it probably fails to close. Then
3332 // do not consume the key.
3333 if (was_curwin && wp == curwin)
3334 return -1;
3335 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003336 }
3337
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003338 argv[0].v_type = VAR_NUMBER;
3339 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3340
3341 // Convert the number to a string, so that the function can use:
3342 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003343 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003344 argv[1].v_type = VAR_STRING;
3345 argv[1].vval.v_string = vim_strsave(buf);
3346
3347 argv[2].v_type = VAR_UNKNOWN;
3348
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003349 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003350 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3351 {
3352 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003353 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003354 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003355 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003356 }
3357 else
3358 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003359 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3360 popup_highlight_curline(wp);
3361
Bram Moolenaar371806e2020-10-22 13:44:54 +02003362 // If an error message was given always return FALSE, so that keys are
3363 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003364 // If we get three errors in a row then close the popup. Decrement the
3365 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3366 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003367 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003368 {
3369 wp->w_filter_errors += 10;
3370 if (wp->w_filter_errors >= 30)
3371 popup_close_with_retval(wp, -1);
3372 res = FALSE;
3373 }
3374 else
3375 {
3376 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3377 --wp->w_filter_errors;
3378 res = tv_get_bool(&rettv);
3379 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003380 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003381
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003382 vim_free(argv[1].vval.v_string);
3383 clear_tv(&rettv);
3384 return res;
3385}
3386
3387/*
3388 * Called when "c" was typed: invoke popup filter callbacks.
3389 * Returns TRUE when the character was consumed,
3390 */
3391 int
3392popup_do_filter(int c)
3393{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003394 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003395 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003396 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003397 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003398 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003399 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003400
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003401#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003402 // Popup window with terminal always gets focus.
3403 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3404 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003405#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003406
Bram Moolenaar934470e2019-09-01 23:27:05 +02003407 if (recursive)
3408 return FALSE;
3409 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003410
Bram Moolenaarf6396232019-08-24 19:36:00 +02003411 if (c == K_LEFTMOUSE)
3412 {
3413 int row = mouse_row;
3414 int col = mouse_col;
3415
3416 wp = mouse_find_win(&row, &col, FIND_POPUP);
3417 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003418 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003419 }
3420
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003421 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003422 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003423 while (res == FALSE
3424 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003425 if (wp->w_filter_cb.cb_name != NULL
3426 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003427 res = invoke_popup_filter(wp, c);
3428
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003429 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003430 {
3431 int save_got_int = got_int;
3432
3433 // Reset got_int to avoid a function used in the statusline aborts.
3434 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003435 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003436 got_int |= save_got_int;
3437 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003438 recursive = FALSE;
3439 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003440
3441 // When interrupted return FALSE to avoid looping.
3442 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003443}
3444
Bram Moolenaar3397f742019-06-02 18:40:06 +02003445/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003446 * Return TRUE if there is a popup visible with a filter callback and the
3447 * "mapping" property off.
3448 */
3449 int
3450popup_no_mapping(void)
3451{
3452 int round;
3453 win_T *wp;
3454
3455 for (round = 1; round <= 2; ++round)
3456 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3457 wp != NULL; wp = wp->w_next)
3458 if (wp->w_filter_cb.cb_name != NULL
3459 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3460 return TRUE;
3461 return FALSE;
3462}
3463
3464/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003465 * Called when the cursor moved: check if any popup needs to be closed if the
3466 * cursor moved far enough.
3467 */
3468 void
3469popup_check_cursor_pos()
3470{
3471 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003472
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003473 popup_reset_handled(POPUP_HANDLED_3);
3474 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003475 if (wp->w_popup_curwin != NULL
3476 && (curwin != wp->w_popup_curwin
3477 || curwin->w_cursor.lnum != wp->w_popup_lnum
3478 || curwin->w_cursor.col < wp->w_popup_mincol
3479 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003480 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003481}
3482
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003483/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003484 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003485 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003486 static void
3487popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003488{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003489 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003490 char_u *cells;
3491 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003492
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003493 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3494 {
3495 vim_free(wp->w_popup_mask_cells);
3496 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003497 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003498 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003499 if (wp->w_popup_mask_cells != NULL
3500 && wp->w_popup_mask_height == height
3501 && wp->w_popup_mask_width == width)
3502 return; // cache is still valid
3503
3504 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003505 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003506 if (wp->w_popup_mask_cells == NULL)
3507 return;
3508 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003509
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003510 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003511 {
3512 int cols, cole;
3513 int lines, linee;
3514
3515 li = lio->li_tv.vval.v_list->lv_first;
3516 cols = tv_get_number(&li->li_tv);
3517 if (cols < 0)
3518 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003519 if (cols <= 0)
3520 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003521 li = li->li_next;
3522 cole = tv_get_number(&li->li_tv);
3523 if (cole < 0)
3524 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003525 if (cole > width)
3526 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003527 li = li->li_next;
3528 lines = tv_get_number(&li->li_tv);
3529 if (lines < 0)
3530 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003531 if (lines <= 0)
3532 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003533 li = li->li_next;
3534 linee = tv_get_number(&li->li_tv);
3535 if (linee < 0)
3536 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003537 if (linee > height)
3538 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003539
Bram Moolenaar4012d262020-12-29 11:57:46 +01003540 for (row = lines - 1; row < linee; ++row)
3541 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003542 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003543 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003544}
3545
3546/*
3547 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3548 * "col" and "line" are screen coordinates.
3549 */
3550 static int
3551popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3552{
3553 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3554 int line = screenline - wp->w_winrow;
3555
3556 return col >= 0 && col < width
3557 && line >= 0 && line < height
3558 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003559}
3560
3561/*
3562 * Set flags in popup_transparent[] for window "wp" to "val".
3563 */
3564 static void
3565update_popup_transparent(win_T *wp, int val)
3566{
3567 if (wp->w_popup_mask != NULL)
3568 {
3569 int width = popup_width(wp);
3570 int height = popup_height(wp);
3571 listitem_T *lio, *li;
3572 int cols, cole;
3573 int lines, linee;
3574 int col, line;
3575
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003576 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003577 {
3578 li = lio->li_tv.vval.v_list->lv_first;
3579 cols = tv_get_number(&li->li_tv);
3580 if (cols < 0)
3581 cols = width + cols + 1;
3582 li = li->li_next;
3583 cole = tv_get_number(&li->li_tv);
3584 if (cole < 0)
3585 cole = width + cole + 1;
3586 li = li->li_next;
3587 lines = tv_get_number(&li->li_tv);
3588 if (lines < 0)
3589 lines = height + lines + 1;
3590 li = li->li_next;
3591 linee = tv_get_number(&li->li_tv);
3592 if (linee < 0)
3593 linee = height + linee + 1;
3594
3595 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003596 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003597 if (cols < 0)
3598 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003599 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003600 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003601 if (lines < 0)
3602 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003603 for (line = lines; line < linee
3604 && line + wp->w_winrow < screen_Rows; ++line)
3605 for (col = cols; col < cole
3606 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003607 popup_transparent[(line + wp->w_winrow) * screen_Columns
3608 + col + wp->w_wincol] = val;
3609 }
3610 }
3611}
3612
3613/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003614 * Only called when popup window "wp" is hidden: If the window is positioned
3615 * next to a text property, and it is now visible, then unhide the popup.
3616 * We don't check if visible popups become hidden, that is done in
3617 * popup_adjust_position().
3618 * Return TRUE if the popup became unhidden.
3619 */
3620 static int
3621check_popup_unhidden(win_T *wp)
3622{
3623 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3624 {
3625 textprop_T prop;
3626 linenr_T lnum;
3627
Bram Moolenaar27724252022-05-08 15:00:04 +01003628 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3629 && find_visible_prop(wp->w_popup_prop_win,
3630 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003631 &prop, &lnum) == OK)
3632 {
3633 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003634 wp->w_popup_prop_topline = 0; // force repositioning
3635 return TRUE;
3636 }
3637 }
3638 return FALSE;
3639}
3640
3641/*
3642 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3643 * That is when the buffer in the popup was changed, or the popup is following
3644 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003645 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003646 */
3647 static int
3648popup_need_position_adjust(win_T *wp)
3649{
3650 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3651 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003652 if (win_valid(wp->w_popup_prop_win)
3653 && (wp->w_popup_prop_changedtick
3654 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3655 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3656 return TRUE;
3657
3658 // May need to adjust the width if the cursor moved.
3659 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003660}
3661
3662/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003663 * Update "popup_mask" if needed.
3664 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003665 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003666 * Also marks window lines for redrawing.
3667 */
3668 void
3669may_update_popup_mask(int type)
3670{
3671 win_T *wp;
3672 short *mask;
3673 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003674 int redraw_all_popups = FALSE;
3675 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003676
3677 // Need to recompute when switching tabs.
3678 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3679 // (such as the screen size) must have changed.
3680 if (popup_mask_tab != curtab || type >= NOT_VALID)
3681 {
3682 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003683 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003684 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003685
3686 // Check if any popup window buffer has changed and if any popup connected
3687 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003688 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003689 if (wp->w_popup_flags & POPF_HIDDEN)
3690 popup_mask_refresh |= check_popup_unhidden(wp);
3691 else if (popup_need_position_adjust(wp))
3692 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003693 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003694 if (wp->w_popup_flags & POPF_HIDDEN)
3695 popup_mask_refresh |= check_popup_unhidden(wp);
3696 else if (popup_need_position_adjust(wp))
3697 popup_mask_refresh = TRUE;
3698
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003699 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003700 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003701
3702 // Need to update the mask, something has changed.
3703 popup_mask_refresh = FALSE;
3704 popup_mask_tab = curtab;
3705 popup_visible = FALSE;
3706
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003707 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003708 // If redrawing only what is needed, update "popup_mask_next" and then
3709 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003710 redrawing_all_win = TRUE;
3711 FOR_ALL_WINDOWS(wp)
3712 if (wp->w_redr_type < SOME_VALID)
3713 redrawing_all_win = FALSE;
3714 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003715 mask = popup_mask;
3716 else
3717 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003718 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003719
3720 // Find the window with the lowest zindex that hasn't been handled yet,
3721 // so that the window with a higher zindex overwrites the value in
3722 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003723 popup_reset_handled(POPUP_HANDLED_4);
3724 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003725 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003726 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003727 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003728
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003729 popup_visible = TRUE;
3730
Bram Moolenaar12034e22019-08-25 22:25:02 +02003731 // Recompute the position if the text changed. It may make the popup
3732 // hidden if it's attach to a text property that is no longer visible.
3733 if (redraw_all_popups || popup_need_position_adjust(wp))
3734 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003735 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003736 if (wp->w_popup_flags & POPF_HIDDEN)
3737 continue;
3738 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003739
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003740 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003741 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003742 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003743 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003744 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003745 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003746 col < wp->w_wincol + width - wp->w_popup_leftoff
3747 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003748 if (wp->w_zindex < POPUPMENU_ZINDEX
3749 && pum_visible()
3750 && pum_under_menu(line, col, FALSE))
3751 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3752 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003753 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003754 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003755 }
3756
3757 // Only check which lines are to be updated if not already
3758 // updating all lines.
3759 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003760 {
3761 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3762 win_T *prev_wp = NULL;
3763
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003764 for (line = 0; line < screen_Rows; ++line)
3765 {
3766 int col_done = 0;
3767
3768 for (col = 0; col < screen_Columns; ++col)
3769 {
3770 int off = line * screen_Columns + col;
3771
3772 if (popup_mask[off] != popup_mask_next[off])
3773 {
3774 popup_mask[off] = popup_mask_next[off];
3775
3776 if (line >= cmdline_row)
3777 {
3778 // the command line needs to be cleared if text below
3779 // the popup is now visible.
3780 if (!msg_scrolled && popup_mask_next[off] == 0)
3781 clear_cmdline = TRUE;
3782 }
3783 else if (col >= col_done)
3784 {
3785 linenr_T lnum;
3786 int line_cp = line;
3787 int col_cp = col;
3788
3789 // The screen position "line" / "col" needs to be
3790 // redrawn. Figure out what window that is and update
3791 // w_redraw_top and w_redr_bot. Only needs to be done
3792 // once for each window line.
3793 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3794 if (wp != NULL)
3795 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003796#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003797 // A terminal window needs to be redrawn.
3798 if (bt_terminal(wp->w_buffer))
3799 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003800 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003801#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003802 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003803 if (wp != prev_wp)
3804 {
3805 vim_memset(plines_cache, 0,
3806 sizeof(int) * Rows);
3807 prev_wp = wp;
3808 }
3809
3810 if (line_cp >= wp->w_height)
3811 // In (or below) status line
3812 wp->w_redr_status = TRUE;
3813 else
3814 {
3815 // compute the position in the buffer line
3816 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003817 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003818 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003819 redrawWinline(wp, lnum);
3820 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003821 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003822
3823 // This line is going to be redrawn, no need to
3824 // check until the right side of the window.
3825 col_done = wp->w_wincol + wp->w_width - 1;
3826 }
3827 }
3828 }
3829 }
3830 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003831
3832 vim_free(plines_cache);
3833 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003834
3835 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003836}
3837
3838/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003839 * If the current window is a popup and something relevant changed, recompute
3840 * the position and size.
3841 */
3842 void
3843may_update_popup_position(void)
3844{
3845 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3846 popup_adjust_position(curwin);
3847}
3848
3849/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003850 * Return a string of "len" spaces in IObuff.
3851 */
3852 static char_u *
3853get_spaces(int len)
3854{
3855 vim_memset(IObuff, ' ', (size_t)len);
3856 IObuff[len] = NUL;
3857 return IObuff;
3858}
3859
3860/*
3861 * Update popup windows. They are drawn on top of normal windows.
3862 * "win_update" is called for each popup window, lowest zindex first.
3863 */
3864 void
3865update_popups(void (*win_update)(win_T *wp))
3866{
3867 win_T *wp;
3868 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003869 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003870 int total_width;
3871 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003872 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003873 int popup_attr;
3874 int border_attr[4];
3875 int border_char[8];
3876 char_u buf[MB_MAXBYTES];
3877 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003878 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003879 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003880 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003881 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003882 int sb_thumb_top = 0;
3883 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003884 int attr_scroll = 0;
3885 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003886
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003887 // hide the cursor until redrawing is done.
3888 cursor_off();
3889
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003890 // Find the window with the lowest zindex that hasn't been updated yet,
3891 // so that the window with a higher zindex is drawn later, thus goes on
3892 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003893 popup_reset_handled(POPUP_HANDLED_5);
3894 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003895 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003896 int title_len = 0;
3897 int title_wincol;
3898
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003899 // This drawing uses the zindex of the popup window, so that it's on
3900 // top of the text but doesn't draw when another popup with higher
3901 // zindex is on top of the character.
3902 screen_zindex = wp->w_zindex;
3903
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003904 // Set flags in popup_transparent[] for masked cells.
3905 update_popup_transparent(wp, 1);
3906
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003907 // adjust w_winrow and w_wincol for border and padding, since
3908 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003909 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003910 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3911 - wp->w_popup_leftoff;
3912 if (wp->w_wincol + left_extra < 0)
3913 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003914 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003915 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003916
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003917 // Draw the popup text, unless it's off screen.
3918 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003919 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003920 // May need to update the "cursorline" highlighting, which may also
3921 // change "topline"
3922 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3923 popup_highlight_curline(wp);
3924
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003925 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003926
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003927 // move the cursor into the visible lines, otherwise executing
3928 // commands with win_execute() may cause the text to jump.
3929 if (wp->w_cursor.lnum < wp->w_topline)
3930 wp->w_cursor.lnum = wp->w_topline;
3931 else if (wp->w_cursor.lnum >= wp->w_botline)
3932 wp->w_cursor.lnum = wp->w_botline - 1;
3933 }
3934
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003935 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003936 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003937
3938 // Add offset for border and padding if not done already.
3939 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3940 {
3941 wp->w_wcol += left_extra;
3942 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3943 }
3944 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003945 {
3946 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003947 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003948 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003949
Bram Moolenaareabddc42022-04-02 15:32:16 +01003950 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003951 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003952 popup_attr = get_wcr_attr(wp);
3953
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003954 if (wp->w_winrow + total_height > cmdline_row)
3955 wp->w_popup_flags |= POPF_ON_CMDLINE;
3956 else
3957 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3958
3959
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003960 // We can only use these line drawing characters when 'encoding' is
3961 // "utf-8" and 'ambiwidth' is "single".
3962 if (enc_utf8 && *p_ambw == 's')
3963 {
3964 border_char[0] = border_char[2] = 0x2550;
3965 border_char[1] = border_char[3] = 0x2551;
3966 border_char[4] = 0x2554;
3967 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003968 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3969 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003970 border_char[7] = 0x255a;
3971 }
3972 else
3973 {
3974 border_char[0] = border_char[2] = '-';
3975 border_char[1] = border_char[3] = '|';
3976 for (i = 4; i < 8; ++i)
3977 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003978 if (wp->w_popup_flags & POPF_RESIZE)
3979 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003980 }
3981 for (i = 0; i < 8; ++i)
3982 if (wp->w_border_char[i] != 0)
3983 border_char[i] = wp->w_border_char[i];
3984
3985 for (i = 0; i < 4; ++i)
3986 {
3987 border_attr[i] = popup_attr;
3988 if (wp->w_border_highlight[i] != NULL)
3989 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3990 }
3991
Bram Moolenaard91467f2020-11-21 12:42:09 +01003992 // Title goes on top of border or padding.
3993 title_wincol = wp->w_wincol + 1;
3994 if (wp->w_popup_title != NULL)
3995 {
rbtnnc6119412021-08-07 13:08:45 +02003996 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003997
Ralf Schandlbc869872021-05-28 14:12:14 +02003998 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003999 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004000 {
4001 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4002 char_u *title_text = alloc(title_byte_len + 1);
4003
4004 if (title_text != NULL)
4005 {
4006 trunc_string(wp->w_popup_title, title_text,
4007 total_width - 2, title_byte_len + 1);
4008 screen_puts(title_text, wp->w_winrow, title_wincol,
4009 wp->w_popup_border[0] > 0
4010 ? border_attr[0] : popup_attr);
4011 vim_free(title_text);
4012 }
4013
Bram Moolenaard91467f2020-11-21 12:42:09 +01004014 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004015 }
4016 else
4017 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4018 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004019 }
4020
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004021 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004022 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004023 if (wp->w_popup_border[0] > 0)
4024 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004025 // top border; do not draw over the title
4026 if (title_len > 0)
4027 {
4028 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4029 wincol < 0 ? 0 : wincol, title_wincol,
4030 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004031 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004032 border_char[0], border_attr[0]);
4033 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4034 title_wincol + title_len, wincol + total_width,
4035 border_char[0], border_char[0], border_attr[0]);
4036 }
4037 else
4038 {
4039 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4040 wincol < 0 ? 0 : wincol, wincol + total_width,
4041 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4042 ? border_char[4] : border_char[0],
4043 border_char[0], border_attr[0]);
4044 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004045 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004046 {
4047 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4048 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004049 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004050 }
4051 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004052 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4053 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004054
Bram Moolenaard529ba52019-07-02 23:13:53 +02004055 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4056 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004057 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004058 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004059 - wp->w_has_scrollbar;
4060 if (padcol < 0)
4061 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004062 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004063 padcol = 0;
4064 }
4065 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004066 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004067 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004068 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004069 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004070 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004071 // top padding and no border; do not draw over the title
4072 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004073 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004074 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004075 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004076 row += 1;
4077 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004078 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004079 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004080 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004081 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004082
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004083 // Compute scrollbar thumb position and size.
4084 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004085 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004086 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4087 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004088
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004089 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4090 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004091 if (wp->w_topline > 1 && sb_thumb_height == height)
4092 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004093 if (sb_thumb_height == 0)
4094 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004095 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004096 // it just fits, avoid divide by zero
4097 sb_thumb_top = 0;
4098 else
4099 sb_thumb_top = (wp->w_topline - 1
4100 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004101 * (wp->w_height - sb_thumb_height)
4102 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004103 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4104 sb_thumb_top = 1; // show it's scrolled
4105
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004106 if (wp->w_scrollbar_highlight != NULL)
4107 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4108 else
4109 attr_scroll = highlight_attr[HLF_PSB];
4110 if (wp->w_thumb_highlight != NULL)
4111 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4112 else
4113 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004114 }
4115
4116 for (i = wp->w_popup_border[0];
4117 i < total_height - wp->w_popup_border[2]; ++i)
4118 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004119 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004120 // left and right padding only needed next to the body
4121 int do_padding =
4122 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4123 && i < total_height - wp->w_popup_border[2]
4124 - wp->w_popup_padding[2];
4125
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004126 row = wp->w_winrow + i;
4127
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004128 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004129 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004130 {
4131 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004132 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004133 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004134 if (do_padding && wp->w_popup_padding[3] > 0)
4135 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004136 int col = wincol + wp->w_popup_border[3];
4137
Bram Moolenaard529ba52019-07-02 23:13:53 +02004138 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004139 pad_left = wp->w_popup_padding[3];
4140 if (col < 0)
4141 {
4142 pad_left += col;
4143 col = 0;
4144 }
4145 if (pad_left > 0)
4146 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4147 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004148 // scrollbar
4149 if (wp->w_has_scrollbar)
4150 {
4151 int line = i - top_off;
4152 int scroll_col = wp->w_wincol + total_width - 1
4153 - wp->w_popup_border[1];
4154
4155 if (line >= 0 && line < wp->w_height)
4156 screen_putchar(' ', row, scroll_col,
4157 line >= sb_thumb_top
4158 && line < sb_thumb_top + sb_thumb_height
4159 ? attr_thumb : attr_scroll);
4160 else
4161 screen_putchar(' ', row, scroll_col, popup_attr);
4162 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004163 // right border
4164 if (wp->w_popup_border[1] > 0)
4165 {
4166 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004167 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004168 }
4169 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004170 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004171 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004172 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004173 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4174 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004175 }
4176
4177 if (wp->w_popup_padding[2] > 0)
4178 {
4179 // bottom padding
4180 row = wp->w_winrow + wp->w_popup_border[0]
4181 + wp->w_popup_padding[0] + wp->w_height;
4182 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004183 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004184 }
4185
4186 if (wp->w_popup_border[2] > 0)
4187 {
4188 // bottom border
4189 row = wp->w_winrow + total_height - 1;
4190 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004191 wincol < 0 ? 0 : wincol,
4192 wincol + total_width,
4193 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004194 ? border_char[7] : border_char[2],
4195 border_char[2], border_attr[2]);
4196 if (wp->w_popup_border[1] > 0)
4197 {
4198 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004199 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004200 }
4201 }
4202
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004203 if (wp->w_popup_close == POPCLOSE_BUTTON)
4204 {
4205 // close button goes on top of anything at the top-right corner
4206 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004207 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004208 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4209 }
4210
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004211 update_popup_transparent(wp, 0);
4212
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004213 // Back to the normal zindex.
4214 screen_zindex = 0;
4215 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004216
4217#if defined(FEAT_SEARCH_EXTRA)
4218 // In case win_update() called start_search_hl().
4219 end_search_hl();
4220#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004221}
4222
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004223/*
4224 * Mark references in callbacks of one popup window.
4225 */
4226 static int
4227set_ref_in_one_popup(win_T *wp, int copyID)
4228{
4229 int abort = FALSE;
4230 typval_T tv;
4231
4232 if (wp->w_close_cb.cb_partial != NULL)
4233 {
4234 tv.v_type = VAR_PARTIAL;
4235 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4236 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4237 }
4238 if (wp->w_filter_cb.cb_partial != NULL)
4239 {
4240 tv.v_type = VAR_PARTIAL;
4241 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4242 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4243 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004244 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004245 return abort;
4246}
4247
4248/*
4249 * Set reference in callbacks of popup windows.
4250 */
4251 int
4252set_ref_in_popups(int copyID)
4253{
4254 int abort = FALSE;
4255 win_T *wp;
4256 tabpage_T *tp;
4257
4258 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4259 abort = abort || set_ref_in_one_popup(wp, copyID);
4260
4261 FOR_ALL_TABPAGES(tp)
4262 {
4263 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4264 abort = abort || set_ref_in_one_popup(wp, copyID);
4265 if (abort)
4266 break;
4267 }
4268 return abort;
4269}
Bram Moolenaar79648732019-07-18 21:43:07 +02004270
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004271 int
4272popup_is_popup(win_T *wp)
4273{
4274 return wp->w_popup_flags != 0;
4275}
4276
4277#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004278/*
4279 * Find an existing popup used as the preview window, in the current tab page.
4280 * Return NULL if not found.
4281 */
4282 win_T *
4283popup_find_preview_window(void)
4284{
4285 win_T *wp;
4286
4287 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004288 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004289 if (wp->w_p_pvw)
4290 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004291 return NULL;
4292}
4293
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004294/*
4295 * Find an existing popup used as the info window, in the current tab page.
4296 * Return NULL if not found.
4297 */
4298 win_T *
4299popup_find_info_window(void)
4300{
4301 win_T *wp;
4302
4303 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004304 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004305 if (wp->w_popup_flags & POPF_INFO)
4306 return wp;
4307 return NULL;
4308}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004309#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004310
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004311 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004312f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4313{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004314#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004315 win_T *wp = popup_find_info_window();
4316
4317 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004318#else
4319 rettv->vval.v_number = 0;
4320#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004321}
4322
4323 void
4324f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004325{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004326#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004327 win_T *wp = popup_find_preview_window();
4328
4329 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004330#else
4331 rettv->vval.v_number = 0;
4332#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004333}
4334
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004335#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004336/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004337 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004338 * NOTE: this makes the popup the current window, so that the file can be
4339 * edited. However, it must not remain to be the current window, the caller
4340 * must make sure of that.
4341 */
4342 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004343popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004344{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004345 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004346
4347 if (wp == NULL)
4348 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004349 if (info)
4350 wp->w_popup_flags |= POPF_INFO;
4351 else
4352 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004353
4354 // Set the width to a reasonable value, so that w_topline can be computed.
4355 if (wp->w_minwidth > 0)
4356 wp->w_width = wp->w_minwidth;
4357 else if (wp->w_maxwidth > 0)
4358 wp->w_width = wp->w_maxwidth;
4359 else
4360 wp->w_width = curwin->w_width;
4361
4362 // Will switch to another buffer soon, dummy one can be wiped.
4363 wp->w_buffer->b_locked = FALSE;
4364
4365 win_enter(wp, FALSE);
4366 return OK;
4367}
4368
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004369/*
4370 * Close any preview popup.
4371 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004372 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004373popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004374{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004375 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004376
4377 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004378 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004379}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004380
4381/*
4382 * Hide the info popup.
4383 */
4384 void
4385popup_hide_info(void)
4386{
4387 win_T *wp = popup_find_info_window();
4388
4389 if (wp != NULL)
4390 popup_hide(wp);
4391}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004392
4393/*
4394 * Close any info popup.
4395 */
4396 void
4397popup_close_info(void)
4398{
4399 win_T *wp = popup_find_info_window();
4400
4401 if (wp != NULL)
4402 popup_close_with_retval(wp, -1);
4403}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004404#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004405
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004406/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004407 * Close any popup for a text property associated with "win".
4408 * Return TRUE if a popup was closed.
4409 */
4410 int
4411popup_win_closed(win_T *win)
4412{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004413 int round;
4414 win_T *wp;
4415 win_T *next;
4416 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004417
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004418 for (round = 1; round <= 2; ++round)
4419 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4420 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004421 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004422 next = wp->w_next;
4423 if (wp->w_popup_prop_win == win)
4424 {
4425 popup_close_with_retval(wp, -1);
4426 ret = TRUE;
4427 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004428 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004429 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004430}
4431
4432/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004433 * Set the title of the popup window to the file name.
4434 */
4435 void
4436popup_set_title(win_T *wp)
4437{
4438 if (wp->w_buffer->b_fname != NULL)
4439 {
4440 char_u dirname[MAXPATHL];
4441 size_t len;
4442
4443 mch_dirname(dirname, MAXPATHL);
4444 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4445
4446 vim_free(wp->w_popup_title);
4447 len = STRLEN(wp->w_buffer->b_fname) + 3;
4448 wp->w_popup_title = alloc((int)len);
4449 if (wp->w_popup_title != NULL)
4450 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4451 wp->w_buffer->b_fname);
4452 redraw_win_later(wp, VALID);
4453 }
4454}
4455
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004456# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004457/*
4458 * If there is a preview window, update the title.
4459 * Used after changing directory.
4460 */
4461 void
4462popup_update_preview_title(void)
4463{
4464 win_T *wp = popup_find_preview_window();
4465
4466 if (wp != NULL)
4467 popup_set_title(wp);
4468}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004469# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004470
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004471#endif // FEAT_PROP_POPUP