blob: dd87705504b408f9ceb42aef951a4e69eba14e5a [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 Moolenaar83bd7a92022-05-29 17:13:24 +01001050 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
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 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001688 if (l->lv_first == &range_list_item)
1689 emsg(_(e_using_number_as_string));
1690 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001691 // list of strings
1692 add_popup_strings(buf, l);
1693 else
1694 // list of dictionaries
1695 add_popup_dicts(buf, l);
1696 }
1697 }
1698
1699 // delete the line that was in the empty buffer
1700 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001701 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001702 curbuf = curwin->w_buffer;
1703}
1704
1705/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001706 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1707 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001708 * Return FAIL if the parsing fails.
1709 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001710 static int
1711parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001712{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001713 char_u *p =
1714#ifdef FEAT_QUICKFIX
1715 !is_preview ? p_cpp :
1716#endif
1717 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001718
Bram Moolenaar258cef52019-08-21 17:29:29 +02001719 if (wp != NULL)
1720 wp->w_popup_flags &= ~POPF_INFO_MENU;
1721
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001722 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001723 {
1724 char_u *e, *dig;
1725 char_u *s = p;
1726 int x;
1727
1728 e = vim_strchr(p, ':');
1729 if (e == NULL || e[1] == NUL)
1730 return FAIL;
1731
1732 p = vim_strchr(e, ',');
1733 if (p == NULL)
1734 p = e + STRLEN(e);
1735 dig = e + 1;
1736 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001737
1738 if (STRNCMP(s, "height:", 7) == 0)
1739 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001740 if (dig != p)
1741 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001742 if (wp != NULL)
1743 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001744 if (is_preview)
1745 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001746 wp->w_maxheight = x;
1747 }
1748 }
1749 else if (STRNCMP(s, "width:", 6) == 0)
1750 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001751 if (dig != p)
1752 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001753 if (wp != NULL)
1754 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001755 if (is_preview)
1756 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001757 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001758 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001759 }
1760 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001761 else if (STRNCMP(s, "highlight:", 10) == 0)
1762 {
1763 if (wp != NULL)
1764 {
1765 int c = *p;
1766
1767 *p = NUL;
1768 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1769 s + 10, OPT_FREE|OPT_LOCAL, 0);
1770 *p = c;
1771 }
1772 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001773 else if (STRNCMP(s, "border:", 7) == 0)
1774 {
1775 char_u *arg = s + 7;
1776 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1777 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1778 int i;
1779
1780 if (!on && !off)
1781 return FAIL;
1782 if (wp != NULL)
1783 {
1784 for (i = 0; i < 4; ++i)
1785 wp->w_popup_border[i] = on ? 1 : 0;
1786 if (off)
1787 // only show the X for close when there is a border
1788 wp->w_popup_close = POPCLOSE_NONE;
1789 }
1790 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001791 else if (STRNCMP(s, "align:", 6) == 0)
1792 {
1793 char_u *arg = s + 6;
1794 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1795 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1796
1797 if (!menu && !item)
1798 return FAIL;
1799 if (wp != NULL && menu)
1800 wp->w_popup_flags |= POPF_INFO_MENU;
1801 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001802 else
1803 return FAIL;
1804 }
1805 return OK;
1806}
1807
1808/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001809 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1810 * is not NULL.
1811 * Return FAIL if the parsing fails.
1812 */
1813 int
1814parse_previewpopup(win_T *wp)
1815{
1816 return parse_popup_option(wp, TRUE);
1817}
1818
1819/*
1820 * Parse the 'completepopup' option and apply the values to window "wp" if it
1821 * is not NULL.
1822 * Return FAIL if the parsing fails.
1823 */
1824 int
1825parse_completepopup(win_T *wp)
1826{
1827 return parse_popup_option(wp, FALSE);
1828}
1829
1830/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001831 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001832 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001833 */
1834 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001835popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001836{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001837 poppos_T ppt = POPPOS_NONE;
1838
1839 if (d != NULL)
1840 ppt = get_pos_entry(d, FALSE);
1841
Bram Moolenaar79648732019-07-18 21:43:07 +02001842 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001843 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001844 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001845 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1846 }
1847 else
1848 {
1849 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1850 if (wp->w_wantline == 0) // cursor in first line
1851 {
1852 wp->w_wantline = 2;
1853 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1854 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1855 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001856 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001857
Bram Moolenaar79648732019-07-18 21:43:07 +02001858 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001859 if (wp->w_wantcol > Columns - width)
1860 {
1861 wp->w_wantcol = Columns - width;
1862 if (wp->w_wantcol < 1)
1863 wp->w_wantcol = 1;
1864 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001865
Bram Moolenaar79648732019-07-18 21:43:07 +02001866 popup_adjust_position(wp);
1867}
1868
1869/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001870 * Set w_wantline and w_wantcol for the a given screen position.
1871 * Caller must take care of running into the window border.
1872 */
1873 void
1874popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1875{
1876 wp->w_wantline = row;
1877 wp->w_wantcol = col;
1878 popup_adjust_position(wp);
1879}
1880
1881/*
1882 * Add a border and lef&right padding.
1883 */
1884 static void
1885add_border_left_right_padding(win_T *wp)
1886{
1887 int i;
1888
1889 for (i = 0; i < 4; ++i)
1890 {
1891 wp->w_popup_border[i] = 1;
1892 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1893 }
1894}
1895
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001896#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001897/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001898 * Return TRUE if there is any popup window with a terminal buffer.
1899 */
1900 static int
1901popup_terminal_exists(void)
1902{
1903 win_T *wp;
1904 tabpage_T *tp;
1905
1906 FOR_ALL_POPUPWINS(wp)
1907 if (wp->w_buffer->b_term != NULL)
1908 return TRUE;
1909 FOR_ALL_TABPAGES(tp)
1910 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1911 if (wp->w_buffer->b_term != NULL)
1912 return TRUE;
1913 return FALSE;
1914}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001915#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001916
1917/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001918 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001919 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001920 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001921 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001922 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001923 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001924popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001925{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001926 win_T *wp;
1927 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001928 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001929 int new_buffer;
1930 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001931 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001932 int nr;
1933 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001934
Bram Moolenaar79648732019-07-18 21:43:07 +02001935 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001936 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001937 if (in_vim9script()
1938 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1939 || check_for_dict_arg(argvars, 1) == FAIL))
1940 return NULL;
1941
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001942 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001943 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001944 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001945 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001946 if (buf == NULL)
1947 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001948 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001949 return NULL;
1950 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001951#ifdef FEAT_TERMINAL
1952 if (buf->b_term != NULL && popup_terminal_exists())
1953 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001954 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001955 return NULL;
1956 }
1957#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001958 }
1959 else if (!(argvars[0].v_type == VAR_STRING
1960 && argvars[0].vval.v_string != NULL)
1961 && !(argvars[0].v_type == VAR_LIST
1962 && argvars[0].vval.v_list != NULL))
1963 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001964 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001965 return NULL;
1966 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001967 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001968 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001969 emsg(_(e_dictionary_required));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001970 return NULL;
1971 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001972 d = argvars[1].vval.v_dict;
1973 }
1974
1975 if (d != NULL)
1976 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001977 if (dict_has_key(d, "tabpage"))
Bram Moolenaar79648732019-07-18 21:43:07 +02001978 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1979 else if (type == TYPE_NOTIFICATION)
1980 tabnr = -1; // notifications are global by default
1981 else
1982 tabnr = 0;
1983 if (tabnr > 0)
1984 {
1985 tp = find_tabpage(tabnr);
1986 if (tp == NULL)
1987 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00001988 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02001989 return NULL;
1990 }
1991 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001992 }
1993
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001994 // Create the window and buffer.
1995 wp = win_alloc_popup_win();
1996 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001997 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001998 if (rettv != NULL)
1999 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002000 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002001 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002002
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002003 if (buf != NULL)
2004 {
2005 // use existing buffer
2006 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002007 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002008 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002009 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002010 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002011 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002012 }
2013 else
2014 {
2015 // create a new buffer associated with the popup
2016 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002017 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002018 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002019 {
2020 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002021 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002022 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002023 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002024
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002025 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002026
Bram Moolenaar46451042019-08-24 15:50:46 +02002027 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002028 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002029 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002030 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002031 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002032 buf->b_p_ul = -1; // no undo
2033 buf->b_p_swf = FALSE; // no swap file
2034 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002035 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002036
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002037 // Avoid that 'buftype' is reset when this buffer is entered.
2038 buf->b_p_initialized = TRUE;
2039 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002040 wp->w_p_wrap = TRUE; // 'wrap' is default on
2041 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002042
Bram Moolenaara3fce622019-06-20 02:31:49 +02002043 if (tp != NULL)
2044 {
2045 // popup on specified tab page
2046 wp->w_next = tp->tp_first_popupwin;
2047 tp->tp_first_popupwin = wp;
2048 }
2049 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002050 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002051 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002052 wp->w_next = curtab->tp_first_popupwin;
2053 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002054 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002055 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002056 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002057 win_T *prev = first_popupwin;
2058
2059 // Global popup: add at the end, so that it gets displayed on top of
2060 // older ones with the same zindex. Matters for notifications.
2061 if (first_popupwin == NULL)
2062 first_popupwin = wp;
2063 else
2064 {
2065 while (prev->w_next != NULL)
2066 prev = prev->w_next;
2067 prev->w_next = wp;
2068 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002069 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002070
Bram Moolenaar79648732019-07-18 21:43:07 +02002071 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002072 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002073
Bram Moolenaar79648732019-07-18 21:43:07 +02002074 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002075 {
2076 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002077 }
2078 if (type == TYPE_ATCURSOR)
2079 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002080 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002081 set_moved_values(wp);
2082 set_moved_columns(wp, FIND_STRING);
2083 }
2084
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002085 if (type == TYPE_BEVAL)
2086 {
2087 wp->w_popup_pos = POPPOS_BOTLEFT;
2088
2089 // by default use the mouse position
2090 wp->w_wantline = mouse_row;
2091 if (wp->w_wantline <= 0) // mouse on first line
2092 {
2093 wp->w_wantline = 2;
2094 wp->w_popup_pos = POPPOS_TOPLEFT;
2095 }
2096 wp->w_wantcol = mouse_col + 1;
2097 set_mousemoved_values(wp);
2098 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2099 }
2100
Bram Moolenaar33796b32019-06-08 16:01:13 +02002101 // set default values
2102 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002103 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002104
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002105 if (type == TYPE_NOTIFICATION)
2106 {
2107 win_T *twp, *nextwin;
2108 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002109
2110 // Try to not overlap with another global popup. Guess we need 3
2111 // more screen lines than buffer lines.
2112 wp->w_wantline = 1;
2113 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2114 {
2115 nextwin = twp->w_next;
2116 if (twp != wp
2117 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2118 && twp->w_winrow <= wp->w_wantline - 1 + height
2119 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2120 {
2121 // move to below this popup and restart the loop to check for
2122 // overlap with other popups
2123 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2124 nextwin = first_popupwin;
2125 }
2126 }
2127 if (wp->w_wantline + height > Rows)
2128 {
2129 // can't avoid overlap, put on top in the hope that message goes
2130 // away soon.
2131 wp->w_wantline = 1;
2132 }
2133
2134 wp->w_wantcol = 10;
2135 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002136 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002137 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002138 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002139 for (i = 0; i < 4; ++i)
2140 wp->w_popup_border[i] = 1;
2141 wp->w_popup_padding[1] = 1;
2142 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002143
2144 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002145 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002146 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2147 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002148 }
2149
Bram Moolenaara730e552019-06-16 19:05:31 +02002150 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002151 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002152 wp->w_popup_pos = POPPOS_CENTER;
2153 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002154 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002155 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002156 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002157 }
2158
Bram Moolenaara730e552019-06-16 19:05:31 +02002159 if (type == TYPE_MENU)
2160 {
2161 typval_T tv;
2162 callback_T callback;
2163
2164 tv.v_type = VAR_STRING;
2165 tv.vval.v_string = (char_u *)"popup_filter_menu";
2166 callback = get_callback(&tv);
2167 if (callback.cb_name != NULL)
2168 set_callback(&wp->w_filter_cb, &callback);
2169
2170 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002171 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002172 }
2173
Bram Moolenaar79648732019-07-18 21:43:07 +02002174 if (type == TYPE_PREVIEW)
2175 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002176 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002177 wp->w_popup_close = POPCLOSE_BUTTON;
2178 for (i = 0; i < 4; ++i)
2179 wp->w_popup_border[i] = 1;
2180 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002181 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002182 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002183# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002184 if (type == TYPE_INFO)
2185 {
2186 wp->w_popup_pos = POPPOS_TOPLEFT;
2187 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2188 wp->w_popup_close = POPCLOSE_BUTTON;
2189 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002190 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002191 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002192# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002193
Bram Moolenaarae943152019-06-16 22:54:14 +02002194 for (i = 0; i < 4; ++i)
2195 VIM_CLEAR(wp->w_border_highlight[i]);
2196 for (i = 0; i < 8; ++i)
2197 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002198 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002199 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002200 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002201
Bram Moolenaar79648732019-07-18 21:43:07 +02002202 if (d != NULL)
2203 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002204 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002205
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002206#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002207 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2208 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002209#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002210
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002211 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002212
2213 wp->w_vsep_width = 0;
2214
2215 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002216 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002217
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002218#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002219 // When running a terminal in the popup it becomes the current window.
2220 if (buf->b_term != NULL)
2221 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002222#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002223
Bram Moolenaara730e552019-06-16 19:05:31 +02002224 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002225}
2226
2227/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002228 * popup_clear()
2229 */
2230 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002231f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002232{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002233 int force = FALSE;
2234
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002235 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2236 return;
2237
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002238 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002239 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002240 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002241}
2242
2243/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002244 * popup_create({text}, {options})
2245 */
2246 void
2247f_popup_create(typval_T *argvars, typval_T *rettv)
2248{
Bram Moolenaar17627312019-06-02 19:53:44 +02002249 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002250}
2251
2252/*
2253 * popup_atcursor({text}, {options})
2254 */
2255 void
2256f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2257{
Bram Moolenaar17627312019-06-02 19:53:44 +02002258 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002259}
2260
2261/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002262 * popup_beval({text}, {options})
2263 */
2264 void
2265f_popup_beval(typval_T *argvars, typval_T *rettv)
2266{
2267 popup_create(argvars, rettv, TYPE_BEVAL);
2268}
2269
2270/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002271 * Invoke the close callback for window "wp" with value "result".
2272 * Careful: The callback may make "wp" invalid!
2273 */
2274 static void
2275invoke_popup_callback(win_T *wp, typval_T *result)
2276{
2277 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002278 typval_T argv[3];
2279
2280 argv[0].v_type = VAR_NUMBER;
2281 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2282
2283 if (result != NULL && result->v_type != VAR_UNKNOWN)
2284 copy_tv(result, &argv[1]);
2285 else
2286 {
2287 argv[1].v_type = VAR_NUMBER;
2288 argv[1].vval.v_number = 0;
2289 }
2290
2291 argv[2].v_type = VAR_UNKNOWN;
2292
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002293 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002294 if (result != NULL)
2295 clear_tv(&argv[1]);
2296 clear_tv(&rettv);
2297}
2298
2299/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002300 * Make "prevwin" the current window, unless it's equal to "wp".
2301 * Otherwise make "firstwin" the current window.
2302 */
2303 static void
2304back_to_prevwin(win_T *wp)
2305{
2306 if (win_valid(prevwin) && wp != prevwin)
2307 win_enter(prevwin, FALSE);
2308 else
2309 win_enter(firstwin, FALSE);
2310}
2311
2312/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002313 * Close popup "wp" and invoke any close callback for it.
2314 */
2315 static void
2316popup_close_and_callback(win_T *wp, typval_T *arg)
2317{
2318 int id = wp->w_id;
2319
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002320#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002321 if (wp == curwin && curbuf->b_term != NULL)
2322 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002323 win_T *owp;
2324
2325 // Closing popup window with a terminal: put focus back on the first
2326 // that works:
2327 // - another popup window with a terminal
2328 // - the previous window
2329 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002330 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002331 if (owp != curwin && owp->w_buffer->b_term != NULL)
2332 break;
2333 if (owp != NULL)
2334 win_enter(owp, FALSE);
2335 else
2336 {
2337 for (owp = curtab->tp_first_popupwin; owp != NULL;
2338 owp = owp->w_next)
2339 if (owp != curwin && owp->w_buffer->b_term != NULL)
2340 break;
2341 if (owp != NULL)
2342 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002343 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002344 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002345 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002346 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002347#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002348
Bram Moolenaar49540192019-12-11 19:34:54 +01002349 // Just in case a check higher up is missing.
2350 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002351 {
2352 // To avoid getting stuck when win_execute() does something that causes
2353 // an error, stop calling the filter callback.
2354 free_callback(&wp->w_filter_cb);
2355
Bram Moolenaar49540192019-12-11 19:34:54 +01002356 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002357 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002358
Bram Moolenaarcee52202020-03-11 14:19:58 +01002359 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002360 if (wp->w_close_cb.cb_name != NULL)
2361 // Careful: This may make "wp" invalid.
2362 invoke_popup_callback(wp, arg);
2363
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002364 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002365 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002366}
2367
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002368 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002369popup_close_with_retval(win_T *wp, int retval)
2370{
2371 typval_T res;
2372
2373 res.v_type = VAR_NUMBER;
2374 res.vval.v_number = retval;
2375 popup_close_and_callback(wp, &res);
2376}
2377
Bram Moolenaar3397f742019-06-02 18:40:06 +02002378/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002379 * Close popup "wp" because of a mouse click.
2380 */
2381 void
2382popup_close_for_mouse_click(win_T *wp)
2383{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002384 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002385}
2386
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002387 static void
2388check_mouse_moved(win_T *wp, win_T *mouse_wp)
2389{
2390 // Close the popup when all if these are true:
2391 // - the mouse is not on this popup
2392 // - "mousemoved" was used
2393 // - the mouse is no longer on the same screen row or the mouse column is
2394 // outside of the relevant text
2395 if (wp != mouse_wp
2396 && wp->w_popup_mouse_row != 0
2397 && (wp->w_popup_mouse_row != mouse_row
2398 || mouse_col < wp->w_popup_mouse_mincol
2399 || mouse_col > wp->w_popup_mouse_maxcol))
2400 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002401 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002402 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002403 }
2404}
2405
2406/*
2407 * Called when the mouse moved: may close a popup with "mousemoved".
2408 */
2409 void
2410popup_handle_mouse_moved(void)
2411{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002412 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002413 win_T *mouse_wp;
2414 int row = mouse_row;
2415 int col = mouse_col;
2416
2417 // find the window where the mouse is in
2418 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2419
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002420 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2421 {
2422 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002423 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002424 }
2425 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2426 {
2427 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002428 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002429 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002430}
2431
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002432/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002433 * In a filter: check if the typed key is a mouse event that is used for
2434 * dragging the popup.
2435 */
2436 static void
2437filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2438{
2439 int row = mouse_row;
2440 int col = mouse_col;
2441
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002442 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002443 && is_mouse_key(c)
2444 && (wp == popup_dragwin
2445 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2446 // do not consume the key, allow for dragging the popup
2447 rettv->vval.v_number = 0;
2448}
2449
Bram Moolenaara730e552019-06-16 19:05:31 +02002450/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002451 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002452 */
2453 void
2454f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2455{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002456 int id;
2457 win_T *wp;
2458 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002459 typval_T res;
2460 int c;
2461 linenr_T old_lnum;
2462
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002463 if (in_vim9script()
2464 && (check_for_number_arg(argvars, 0) == FAIL
2465 || check_for_string_arg(argvars, 1) == FAIL))
2466 return;
2467
2468 id = tv_get_number(&argvars[0]);
2469 wp = win_id2wp(id);
2470 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002471 // If the popup has been closed do not consume the key.
2472 if (wp == NULL)
2473 return;
2474
2475 c = *key;
2476 if (c == K_SPECIAL && key[1] != NUL)
2477 c = TO_SPECIAL(key[1], key[2]);
2478
2479 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002480 rettv->v_type = VAR_BOOL;
2481 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002482 res.v_type = VAR_NUMBER;
2483
2484 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002485 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2486 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002487 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002488 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002489 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2490 ++wp->w_cursor.lnum;
2491 if (old_lnum != wp->w_cursor.lnum)
2492 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002493 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002494 return;
2495 }
2496
2497 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2498 {
2499 // Cancelled, invoke callback with -1
2500 res.vval.v_number = -1;
2501 popup_close_and_callback(wp, &res);
2502 return;
2503 }
2504 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2505 {
2506 // Invoke callback with current index.
2507 res.vval.v_number = wp->w_cursor.lnum;
2508 popup_close_and_callback(wp, &res);
2509 return;
2510 }
2511
2512 filter_handle_drag(wp, c, rettv);
2513}
2514
2515/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002516 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002517 */
2518 void
2519f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2520{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002521 int id;
2522 win_T *wp;
2523 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002524 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002525 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002526
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002527 if (in_vim9script()
2528 && (check_for_number_arg(argvars, 0) == FAIL
2529 || check_for_string_arg(argvars, 1) == FAIL))
2530 return;
2531
2532 id = tv_get_number(&argvars[0]);
2533 wp = win_id2wp(id);
2534 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002535 // If the popup has been closed don't consume the key.
2536 if (wp == NULL)
2537 return;
2538
Bram Moolenaara730e552019-06-16 19:05:31 +02002539 c = *key;
2540 if (c == K_SPECIAL && key[1] != NUL)
2541 c = TO_SPECIAL(key[1], key[2]);
2542
Bram Moolenaara42d9452019-06-15 21:46:30 +02002543 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002544 rettv->v_type = VAR_BOOL;
2545 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002546
Bram Moolenaara730e552019-06-16 19:05:31 +02002547 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002548 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002549 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002550 res.vval.v_number = 0;
2551 else
2552 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002553 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002554 return;
2555 }
2556
2557 // Invoke callback
2558 res.v_type = VAR_NUMBER;
2559 popup_close_and_callback(wp, &res);
2560}
2561
2562/*
2563 * popup_dialog({text}, {options})
2564 */
2565 void
2566f_popup_dialog(typval_T *argvars, typval_T *rettv)
2567{
2568 popup_create(argvars, rettv, TYPE_DIALOG);
2569}
2570
2571/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002572 * popup_menu({text}, {options})
2573 */
2574 void
2575f_popup_menu(typval_T *argvars, typval_T *rettv)
2576{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002577 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002578}
2579
2580/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002581 * popup_notification({text}, {options})
2582 */
2583 void
2584f_popup_notification(typval_T *argvars, typval_T *rettv)
2585{
2586 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2587}
2588
2589/*
2590 * Find the popup window with window-ID "id".
2591 * If the popup window does not exist NULL is returned.
2592 * If the window is not a popup window, and error message is given.
2593 */
2594 static win_T *
2595find_popup_win(int id)
2596{
2597 win_T *wp = win_id2wp(id);
2598
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002599 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002600 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002601 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002602 return NULL;
2603 }
2604 return wp;
2605}
2606
2607/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002608 * popup_close({id})
2609 */
2610 void
2611f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2612{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002613 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002614 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002615
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002616 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2617 return;
2618
2619 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002620 if (
2621# ifdef FEAT_TERMINAL
2622 // if the popup contains a terminal it will become hidden
2623 curbuf->b_term == NULL &&
2624# endif
2625 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002626 return;
2627
2628 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002629 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002630 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002631}
2632
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002633 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002634popup_hide(win_T *wp)
2635{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002636#ifdef FEAT_TERMINAL
2637 if (error_if_term_popup_window())
2638 return;
2639#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002640 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2641 {
2642 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002643 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002644 redraw_all_later(NOT_VALID);
2645 popup_mask_refresh = TRUE;
2646 }
2647}
2648
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002649/*
2650 * popup_hide({id})
2651 */
2652 void
2653f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2654{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002655 int id;
2656 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002657
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002658 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2659 return;
2660
2661 id = (int)tv_get_number(argvars);
2662 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002663 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002664 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002665 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002666 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2667 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002668}
2669
2670 void
2671popup_show(win_T *wp)
2672{
2673 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002674 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002675 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002676 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002677 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002678 }
2679}
2680
2681/*
2682 * popup_show({id})
2683 */
2684 void
2685f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2686{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002687 int id;
2688 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002689
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002690 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2691 return;
2692
2693 id = (int)tv_get_number(argvars);
2694 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002695 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002696 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002697 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002698 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002699#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002700 if (wp->w_popup_flags & POPF_INFO)
2701 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002702#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002703 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002704}
2705
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002706/*
2707 * popup_settext({id}, {text})
2708 */
2709 void
2710f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2711{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002712 int id;
2713 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002714
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002715 if (in_vim9script()
2716 && (check_for_number_arg(argvars, 0) == FAIL
2717 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2718 return;
2719
2720 id = (int)tv_get_number(&argvars[0]);
2721 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002722 if (wp != NULL)
2723 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002724 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002725 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002726 else
2727 {
2728 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002729 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002730 popup_adjust_position(wp);
2731 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002732 }
2733}
2734
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002735 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002736popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002737{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002738 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002739 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002740 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002741 clear_cmdline = TRUE;
2742 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002743
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002744 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002745 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002746}
2747
Bram Moolenaar82106932020-03-13 14:34:38 +01002748 static void
2749error_for_popup_window(void)
2750{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002751 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002752}
2753
2754 int
2755error_if_popup_window(int also_with_term UNUSED)
2756{
2757 // win_execute() may set "curwin" to a popup window temporarily, but many
2758 // commands are disallowed then. When a terminal runs in the popup most
2759 // things are allowed. When a terminal is finished it can be closed.
2760 if (WIN_IS_POPUP(curwin)
2761# ifdef FEAT_TERMINAL
2762 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002763# endif
2764 )
2765 {
2766 error_for_popup_window();
2767 return TRUE;
2768 }
2769 return FALSE;
2770}
2771
Bram Moolenaarec583842019-05-26 14:11:23 +02002772/*
2773 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002774 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002775 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002776 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002777 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002778popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002779{
2780 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002781 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002782 win_T *prev = NULL;
2783
Bram Moolenaarec583842019-05-26 14:11:23 +02002784 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002785 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002786 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002787 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002788 if (wp == curwin)
2789 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002790 if (!force)
2791 {
2792 error_for_popup_window();
2793 return FAIL;
2794 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002795 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002796 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002797 if (prev == NULL)
2798 first_popupwin = wp->w_next;
2799 else
2800 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002801 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002802 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002803 }
2804
Bram Moolenaarec583842019-05-26 14:11:23 +02002805 // go through tab-local popups
2806 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002807 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002808 return OK;
2809 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002810}
2811
2812/*
2813 * Close a popup window with Window-id "id" in tabpage "tp".
2814 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002815 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002816popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002817{
2818 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002819 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002820 win_T *prev = NULL;
2821
Bram Moolenaarec583842019-05-26 14:11:23 +02002822 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2823 if (wp->w_id == id)
2824 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002825 if (wp == curwin)
2826 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002827 if (!force)
2828 {
2829 error_for_popup_window();
2830 return FAIL;
2831 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002832 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002833 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002834 if (prev == NULL)
2835 *root = wp->w_next;
2836 else
2837 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002838 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002839 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002840 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002841 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002842}
2843
2844 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002845close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002846{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002847 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002848 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002849 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002850 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002851 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002852 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002853 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002854 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002855}
2856
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002857/*
2858 * popup_move({id}, {options})
2859 */
2860 void
2861f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2862{
Bram Moolenaarae943152019-06-16 22:54:14 +02002863 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002864 int id;
2865 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002866
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002867 if (in_vim9script()
2868 && (check_for_number_arg(argvars, 0) == FAIL
2869 || check_for_dict_arg(argvars, 1) == FAIL))
2870 return;
2871
2872 id = (int)tv_get_number(argvars);
2873 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002874 if (wp == NULL)
2875 return; // invalid {id}
2876
2877 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2878 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002879 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002880 return;
2881 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002882 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002883
Bram Moolenaarae943152019-06-16 22:54:14 +02002884 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002885
2886 if (wp->w_winrow + wp->w_height >= cmdline_row)
2887 clear_cmdline = TRUE;
2888 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002889}
2890
Bram Moolenaarbc133542019-05-29 20:26:46 +02002891/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002892 * popup_setoptions({id}, {options})
2893 */
2894 void
2895f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2896{
2897 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002898 int id;
2899 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002900 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002901
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002902 if (in_vim9script()
2903 && (check_for_number_arg(argvars, 0) == FAIL
2904 || check_for_dict_arg(argvars, 1) == FAIL))
2905 return;
2906
2907 id = (int)tv_get_number(argvars);
2908 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002909 if (wp == NULL)
2910 return; // invalid {id}
2911
2912 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2913 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002914 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002915 return;
2916 }
2917 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002918 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002919
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002920 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002921
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002922 if (old_firstline != wp->w_firstline)
2923 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002924 popup_adjust_position(wp);
2925}
2926
2927/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002928 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002929 */
2930 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002931f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002932{
2933 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002934 int id;
2935 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002936 int top_extra;
2937 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002938
2939 if (rettv_dict_alloc(rettv) == OK)
2940 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002941 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2942 return;
2943
2944 id = (int)tv_get_number(argvars);
2945 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002946 if (wp == NULL)
2947 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002948 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002949 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2950
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002951 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002952 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002953 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002954
Bram Moolenaarbc133542019-05-29 20:26:46 +02002955 dict_add_number(dict, "line", wp->w_winrow + 1);
2956 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002957 dict_add_number(dict, "width", wp->w_width + left_extra
2958 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2959 dict_add_number(dict, "height", wp->w_height + top_extra
2960 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002961
2962 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2963 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2964 dict_add_number(dict, "core_width", wp->w_width);
2965 dict_add_number(dict, "core_height", wp->w_height);
2966
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002967 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002968 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002969 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002970 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002971 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002972
2973 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002974 }
2975}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002976
2977/*
2978 * popup_list()
2979 */
2980 void
2981f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2982{
2983 win_T *wp;
2984 tabpage_T *tp;
2985
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002986 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002987 return;
2988 FOR_ALL_POPUPWINS(wp)
2989 list_append_number(rettv->vval.v_list, wp->w_id);
2990 FOR_ALL_TABPAGES(tp)
2991 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2992 list_append_number(rettv->vval.v_list, wp->w_id);
2993}
2994
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002995/*
2996 * popup_locate({row}, {col})
2997 */
2998 void
2999f_popup_locate(typval_T *argvars, typval_T *rettv)
3000{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003001 int row;
3002 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003003 win_T *wp;
3004
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003005 if (in_vim9script()
3006 && (check_for_number_arg(argvars, 0) == FAIL
3007 || check_for_number_arg(argvars, 1) == FAIL))
3008 return;
3009
3010 row = tv_get_number(&argvars[0]) - 1;
3011 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003012 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003013 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003014 rettv->vval.v_number = wp->w_id;
3015}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003016
3017/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003018 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3019 */
3020 static void
3021get_padding_border(dict_T *dict, int *array, char *name)
3022{
3023 list_T *list;
3024 int i;
3025
3026 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3027 return;
3028
3029 list = list_alloc();
3030 if (list != NULL)
3031 {
3032 dict_add_list(dict, name, list);
3033 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3034 for (i = 0; i < 4; ++i)
3035 list_append_number(list, array[i]);
3036 }
3037}
3038
3039/*
3040 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3041 */
3042 static void
3043get_borderhighlight(dict_T *dict, win_T *wp)
3044{
3045 list_T *list;
3046 int i;
3047
3048 for (i = 0; i < 4; ++i)
3049 if (wp->w_border_highlight[i] != NULL)
3050 break;
3051 if (i == 4)
3052 return;
3053
3054 list = list_alloc();
3055 if (list != NULL)
3056 {
3057 dict_add_list(dict, "borderhighlight", list);
3058 for (i = 0; i < 4; ++i)
3059 list_append_string(list, wp->w_border_highlight[i], -1);
3060 }
3061}
3062
3063/*
3064 * For popup_getoptions(): add a "borderchars" entry to "dict".
3065 */
3066 static void
3067get_borderchars(dict_T *dict, win_T *wp)
3068{
3069 list_T *list;
3070 int i;
3071 char_u buf[NUMBUFLEN];
3072 int len;
3073
3074 for (i = 0; i < 8; ++i)
3075 if (wp->w_border_char[i] != 0)
3076 break;
3077 if (i == 8)
3078 return;
3079
3080 list = list_alloc();
3081 if (list != NULL)
3082 {
3083 dict_add_list(dict, "borderchars", list);
3084 for (i = 0; i < 8; ++i)
3085 {
3086 len = mb_char2bytes(wp->w_border_char[i], buf);
3087 list_append_string(list, buf, len);
3088 }
3089 }
3090}
3091
3092/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003093 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003094 */
3095 static void
3096get_moved_list(dict_T *dict, win_T *wp)
3097{
3098 list_T *list;
3099
3100 list = list_alloc();
3101 if (list != NULL)
3102 {
3103 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003104 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003105 list_append_number(list, wp->w_popup_mincol);
3106 list_append_number(list, wp->w_popup_maxcol);
3107 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003108 list = list_alloc();
3109 if (list != NULL)
3110 {
3111 dict_add_list(dict, "mousemoved", list);
3112 list_append_number(list, wp->w_popup_mouse_row);
3113 list_append_number(list, wp->w_popup_mouse_mincol);
3114 list_append_number(list, wp->w_popup_mouse_maxcol);
3115 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003116}
3117
3118/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003119 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003120 */
3121 void
3122f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3123{
3124 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003125 int id;
3126 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003127 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003128 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003129
3130 if (rettv_dict_alloc(rettv) == OK)
3131 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003132 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3133 return;
3134
3135 id = (int)tv_get_number(argvars);
3136 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003137 if (wp == NULL)
3138 return;
3139
3140 dict = rettv->vval.v_dict;
3141 dict_add_number(dict, "line", wp->w_wantline);
3142 dict_add_number(dict, "col", wp->w_wantcol);
3143 dict_add_number(dict, "minwidth", wp->w_minwidth);
3144 dict_add_number(dict, "minheight", wp->w_minheight);
3145 dict_add_number(dict, "maxheight", wp->w_maxheight);
3146 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003147 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003148 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003149 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003150 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003151 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003152 {
3153 proptype_T *pt = text_prop_type_by_id(
3154 wp->w_popup_prop_win->w_buffer,
3155 wp->w_popup_prop_type);
3156
3157 if (pt != NULL)
3158 dict_add_string(dict, "textprop", pt->pt_name);
3159 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3160 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3161 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003162 dict_add_string(dict, "title", wp->w_popup_title);
3163 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003164 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003165 dict_add_number(dict, "dragall",
3166 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003167 dict_add_number(dict, "mapping",
3168 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003169 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003170 dict_add_number(dict, "posinvert",
3171 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003172 dict_add_number(dict, "cursorline",
3173 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003174 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003175 if (wp->w_scrollbar_highlight != NULL)
3176 dict_add_string(dict, "scrollbarhighlight",
3177 wp->w_scrollbar_highlight);
3178 if (wp->w_thumb_highlight != NULL)
3179 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003180
Bram Moolenaara3fce622019-06-20 02:31:49 +02003181 // find the tabpage that holds this popup
3182 i = 1;
3183 FOR_ALL_TABPAGES(tp)
3184 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003185 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003186
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003187 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3188 if (twp->w_id == id)
3189 break;
3190 if (twp != NULL)
3191 break;
3192 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003193 }
3194 if (tp == NULL)
3195 i = -1; // must be global
3196 else if (tp == curtab)
3197 i = 0;
3198 dict_add_number(dict, "tabpage", i);
3199
Bram Moolenaarae943152019-06-16 22:54:14 +02003200 get_padding_border(dict, wp->w_popup_padding, "padding");
3201 get_padding_border(dict, wp->w_popup_border, "border");
3202 get_borderhighlight(dict, wp);
3203 get_borderchars(dict, wp);
3204 get_moved_list(dict, wp);
3205
3206 if (wp->w_filter_cb.cb_name != NULL)
3207 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3208 if (wp->w_close_cb.cb_name != NULL)
3209 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003210
K.Takataeeec2542021-06-02 13:28:16 +02003211 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003212 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3213 {
3214 dict_add_string(dict, "pos",
3215 (char_u *)poppos_entries[i].pp_name);
3216 break;
3217 }
3218
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003219 dict_add_string(dict, "close", (char_u *)(
3220 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3221 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3222
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003223# if defined(FEAT_TIMERS)
3224 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3225 ? (long)wp->w_popup_timer->tr_interval : 0L);
3226# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003227 }
3228}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003229
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003230# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003231/*
3232 * Return TRUE if the current window is running a terminal in a popup window.
3233 * Return FALSE when the job has ended.
3234 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003235 int
3236error_if_term_popup_window()
3237{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003238 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3239 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003240 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003241 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003242 return TRUE;
3243 }
3244 return FALSE;
3245}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003246# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003247
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003248/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003249 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003250 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003251 * Each calling function should use a different flag, see the list at
3252 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003253 */
3254 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003255popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003256{
3257 win_T *wp;
3258
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003259 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003260 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003261 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003262 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003263}
3264
3265/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003266 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003267 * Must have called popup_reset_handled() first.
3268 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3269 * popup with the highest zindex.
3270 */
3271 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003272find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003273{
3274 win_T *wp;
3275 win_T *found_wp;
3276 int found_zindex;
3277
3278 found_zindex = lowest ? INT_MAX : 0;
3279 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003280 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003281 if ((wp->w_popup_handled & handled_flag) == 0
3282 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003283 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003284 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003285 {
3286 found_zindex = wp->w_zindex;
3287 found_wp = wp;
3288 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003289 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003290 if ((wp->w_popup_handled & handled_flag) == 0
3291 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003292 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003293 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003294 {
3295 found_zindex = wp->w_zindex;
3296 found_wp = wp;
3297 }
3298
3299 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003300 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003301 return found_wp;
3302}
3303
3304/*
3305 * Invoke the filter callback for window "wp" with typed character "c".
3306 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003307 * Returns the return value of the filter or -1 for CTRL-C in the current
3308 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003309 * Careful: The filter may make "wp" invalid!
3310 */
3311 static int
3312invoke_popup_filter(win_T *wp, int c)
3313{
3314 int res;
3315 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003316 typval_T argv[3];
3317 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003318 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003319 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003320
Bram Moolenaara42d9452019-06-15 21:46:30 +02003321 // Emergency exit: CTRL-C closes the popup.
3322 if (c == Ctrl_C)
3323 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003324 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003325 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003326
3327 // Reset got_int to avoid the callback isn't called.
3328 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003329 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003330 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003331
3332 // If the popup is the current window it probably fails to close. Then
3333 // do not consume the key.
3334 if (was_curwin && wp == curwin)
3335 return -1;
3336 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003337 }
3338
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003339 argv[0].v_type = VAR_NUMBER;
3340 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3341
3342 // Convert the number to a string, so that the function can use:
3343 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003344 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003345 argv[1].v_type = VAR_STRING;
3346 argv[1].vval.v_string = vim_strsave(buf);
3347
3348 argv[2].v_type = VAR_UNKNOWN;
3349
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003350 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003351 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3352 {
3353 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003354 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003355 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003356 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003357 }
3358 else
3359 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003360 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3361 popup_highlight_curline(wp);
3362
Bram Moolenaar371806e2020-10-22 13:44:54 +02003363 // If an error message was given always return FALSE, so that keys are
3364 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003365 // If we get three errors in a row then close the popup. Decrement the
3366 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3367 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003368 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003369 {
3370 wp->w_filter_errors += 10;
3371 if (wp->w_filter_errors >= 30)
3372 popup_close_with_retval(wp, -1);
3373 res = FALSE;
3374 }
3375 else
3376 {
3377 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3378 --wp->w_filter_errors;
3379 res = tv_get_bool(&rettv);
3380 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003381 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003382
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003383 vim_free(argv[1].vval.v_string);
3384 clear_tv(&rettv);
3385 return res;
3386}
3387
3388/*
3389 * Called when "c" was typed: invoke popup filter callbacks.
3390 * Returns TRUE when the character was consumed,
3391 */
3392 int
3393popup_do_filter(int c)
3394{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003395 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003396 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003397 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003398 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003399 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003400 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003401
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003402#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003403 // Popup window with terminal always gets focus.
3404 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3405 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003406#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003407
Bram Moolenaar934470e2019-09-01 23:27:05 +02003408 if (recursive)
3409 return FALSE;
3410 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003411
Bram Moolenaarf6396232019-08-24 19:36:00 +02003412 if (c == K_LEFTMOUSE)
3413 {
3414 int row = mouse_row;
3415 int col = mouse_col;
3416
3417 wp = mouse_find_win(&row, &col, FIND_POPUP);
3418 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003419 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003420 }
3421
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003422 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003423 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003424 while (res == FALSE
3425 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003426 if (wp->w_filter_cb.cb_name != NULL
3427 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003428 res = invoke_popup_filter(wp, c);
3429
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003430 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003431 {
3432 int save_got_int = got_int;
3433
3434 // Reset got_int to avoid a function used in the statusline aborts.
3435 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003436 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003437 got_int |= save_got_int;
3438 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003439 recursive = FALSE;
3440 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003441
3442 // When interrupted return FALSE to avoid looping.
3443 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003444}
3445
Bram Moolenaar3397f742019-06-02 18:40:06 +02003446/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003447 * Return TRUE if there is a popup visible with a filter callback and the
3448 * "mapping" property off.
3449 */
3450 int
3451popup_no_mapping(void)
3452{
3453 int round;
3454 win_T *wp;
3455
3456 for (round = 1; round <= 2; ++round)
3457 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3458 wp != NULL; wp = wp->w_next)
3459 if (wp->w_filter_cb.cb_name != NULL
3460 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3461 return TRUE;
3462 return FALSE;
3463}
3464
3465/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003466 * Called when the cursor moved: check if any popup needs to be closed if the
3467 * cursor moved far enough.
3468 */
3469 void
3470popup_check_cursor_pos()
3471{
3472 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003473
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003474 popup_reset_handled(POPUP_HANDLED_3);
3475 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003476 if (wp->w_popup_curwin != NULL
3477 && (curwin != wp->w_popup_curwin
3478 || curwin->w_cursor.lnum != wp->w_popup_lnum
3479 || curwin->w_cursor.col < wp->w_popup_mincol
3480 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003481 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003482}
3483
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003484/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003485 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003486 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003487 static void
3488popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003489{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003490 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003491 char_u *cells;
3492 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003493
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003494 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3495 {
3496 vim_free(wp->w_popup_mask_cells);
3497 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003498 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003499 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003500 if (wp->w_popup_mask_cells != NULL
3501 && wp->w_popup_mask_height == height
3502 && wp->w_popup_mask_width == width)
3503 return; // cache is still valid
3504
3505 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003506 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003507 if (wp->w_popup_mask_cells == NULL)
3508 return;
3509 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003510
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003511 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003512 {
3513 int cols, cole;
3514 int lines, linee;
3515
3516 li = lio->li_tv.vval.v_list->lv_first;
3517 cols = tv_get_number(&li->li_tv);
3518 if (cols < 0)
3519 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003520 if (cols <= 0)
3521 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003522 li = li->li_next;
3523 cole = tv_get_number(&li->li_tv);
3524 if (cole < 0)
3525 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003526 if (cole > width)
3527 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003528 li = li->li_next;
3529 lines = tv_get_number(&li->li_tv);
3530 if (lines < 0)
3531 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003532 if (lines <= 0)
3533 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003534 li = li->li_next;
3535 linee = tv_get_number(&li->li_tv);
3536 if (linee < 0)
3537 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003538 if (linee > height)
3539 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003540
Bram Moolenaar4012d262020-12-29 11:57:46 +01003541 for (row = lines - 1; row < linee; ++row)
3542 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003543 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003544 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003545}
3546
3547/*
3548 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3549 * "col" and "line" are screen coordinates.
3550 */
3551 static int
3552popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3553{
3554 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3555 int line = screenline - wp->w_winrow;
3556
3557 return col >= 0 && col < width
3558 && line >= 0 && line < height
3559 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003560}
3561
3562/*
3563 * Set flags in popup_transparent[] for window "wp" to "val".
3564 */
3565 static void
3566update_popup_transparent(win_T *wp, int val)
3567{
3568 if (wp->w_popup_mask != NULL)
3569 {
3570 int width = popup_width(wp);
3571 int height = popup_height(wp);
3572 listitem_T *lio, *li;
3573 int cols, cole;
3574 int lines, linee;
3575 int col, line;
3576
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003577 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003578 {
3579 li = lio->li_tv.vval.v_list->lv_first;
3580 cols = tv_get_number(&li->li_tv);
3581 if (cols < 0)
3582 cols = width + cols + 1;
3583 li = li->li_next;
3584 cole = tv_get_number(&li->li_tv);
3585 if (cole < 0)
3586 cole = width + cole + 1;
3587 li = li->li_next;
3588 lines = tv_get_number(&li->li_tv);
3589 if (lines < 0)
3590 lines = height + lines + 1;
3591 li = li->li_next;
3592 linee = tv_get_number(&li->li_tv);
3593 if (linee < 0)
3594 linee = height + linee + 1;
3595
3596 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003597 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003598 if (cols < 0)
3599 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003600 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003601 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003602 if (lines < 0)
3603 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003604 for (line = lines; line < linee
3605 && line + wp->w_winrow < screen_Rows; ++line)
3606 for (col = cols; col < cole
3607 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003608 popup_transparent[(line + wp->w_winrow) * screen_Columns
3609 + col + wp->w_wincol] = val;
3610 }
3611 }
3612}
3613
3614/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003615 * Only called when popup window "wp" is hidden: If the window is positioned
3616 * next to a text property, and it is now visible, then unhide the popup.
3617 * We don't check if visible popups become hidden, that is done in
3618 * popup_adjust_position().
3619 * Return TRUE if the popup became unhidden.
3620 */
3621 static int
3622check_popup_unhidden(win_T *wp)
3623{
3624 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3625 {
3626 textprop_T prop;
3627 linenr_T lnum;
3628
Bram Moolenaar27724252022-05-08 15:00:04 +01003629 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3630 && find_visible_prop(wp->w_popup_prop_win,
3631 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003632 &prop, &lnum) == OK)
3633 {
3634 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003635 wp->w_popup_prop_topline = 0; // force repositioning
3636 return TRUE;
3637 }
3638 }
3639 return FALSE;
3640}
3641
3642/*
3643 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3644 * That is when the buffer in the popup was changed, or the popup is following
3645 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003646 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003647 */
3648 static int
3649popup_need_position_adjust(win_T *wp)
3650{
3651 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3652 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003653 if (win_valid(wp->w_popup_prop_win)
3654 && (wp->w_popup_prop_changedtick
3655 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3656 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3657 return TRUE;
3658
3659 // May need to adjust the width if the cursor moved.
3660 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003661}
3662
3663/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003664 * Update "popup_mask" if needed.
3665 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003666 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003667 * Also marks window lines for redrawing.
3668 */
3669 void
3670may_update_popup_mask(int type)
3671{
3672 win_T *wp;
3673 short *mask;
3674 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003675 int redraw_all_popups = FALSE;
3676 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003677
3678 // Need to recompute when switching tabs.
3679 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3680 // (such as the screen size) must have changed.
3681 if (popup_mask_tab != curtab || type >= NOT_VALID)
3682 {
3683 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003684 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003685 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003686
3687 // Check if any popup window buffer has changed and if any popup connected
3688 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003689 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003690 if (wp->w_popup_flags & POPF_HIDDEN)
3691 popup_mask_refresh |= check_popup_unhidden(wp);
3692 else if (popup_need_position_adjust(wp))
3693 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003694 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003695 if (wp->w_popup_flags & POPF_HIDDEN)
3696 popup_mask_refresh |= check_popup_unhidden(wp);
3697 else if (popup_need_position_adjust(wp))
3698 popup_mask_refresh = TRUE;
3699
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003700 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003701 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003702
3703 // Need to update the mask, something has changed.
3704 popup_mask_refresh = FALSE;
3705 popup_mask_tab = curtab;
3706 popup_visible = FALSE;
3707
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003708 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003709 // If redrawing only what is needed, update "popup_mask_next" and then
3710 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003711 redrawing_all_win = TRUE;
3712 FOR_ALL_WINDOWS(wp)
3713 if (wp->w_redr_type < SOME_VALID)
3714 redrawing_all_win = FALSE;
3715 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003716 mask = popup_mask;
3717 else
3718 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003719 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003720
3721 // Find the window with the lowest zindex that hasn't been handled yet,
3722 // so that the window with a higher zindex overwrites the value in
3723 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003724 popup_reset_handled(POPUP_HANDLED_4);
3725 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003726 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003727 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003728 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003729
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003730 popup_visible = TRUE;
3731
Bram Moolenaar12034e22019-08-25 22:25:02 +02003732 // Recompute the position if the text changed. It may make the popup
3733 // hidden if it's attach to a text property that is no longer visible.
3734 if (redraw_all_popups || popup_need_position_adjust(wp))
3735 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003736 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003737 if (wp->w_popup_flags & POPF_HIDDEN)
3738 continue;
3739 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003740
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003741 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003742 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003743 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003744 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003745 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003746 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003747 col < wp->w_wincol + width - wp->w_popup_leftoff
3748 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003749 if (wp->w_zindex < POPUPMENU_ZINDEX
3750 && pum_visible()
3751 && pum_under_menu(line, col, FALSE))
3752 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3753 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003754 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003755 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003756 }
3757
3758 // Only check which lines are to be updated if not already
3759 // updating all lines.
3760 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003761 {
3762 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3763 win_T *prev_wp = NULL;
3764
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003765 for (line = 0; line < screen_Rows; ++line)
3766 {
3767 int col_done = 0;
3768
3769 for (col = 0; col < screen_Columns; ++col)
3770 {
3771 int off = line * screen_Columns + col;
3772
3773 if (popup_mask[off] != popup_mask_next[off])
3774 {
3775 popup_mask[off] = popup_mask_next[off];
3776
3777 if (line >= cmdline_row)
3778 {
3779 // the command line needs to be cleared if text below
3780 // the popup is now visible.
3781 if (!msg_scrolled && popup_mask_next[off] == 0)
3782 clear_cmdline = TRUE;
3783 }
3784 else if (col >= col_done)
3785 {
3786 linenr_T lnum;
3787 int line_cp = line;
3788 int col_cp = col;
3789
3790 // The screen position "line" / "col" needs to be
3791 // redrawn. Figure out what window that is and update
3792 // w_redraw_top and w_redr_bot. Only needs to be done
3793 // once for each window line.
3794 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3795 if (wp != NULL)
3796 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003797#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003798 // A terminal window needs to be redrawn.
3799 if (bt_terminal(wp->w_buffer))
3800 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003801 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003802#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003803 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003804 if (wp != prev_wp)
3805 {
3806 vim_memset(plines_cache, 0,
3807 sizeof(int) * Rows);
3808 prev_wp = wp;
3809 }
3810
3811 if (line_cp >= wp->w_height)
3812 // In (or below) status line
3813 wp->w_redr_status = TRUE;
3814 else
3815 {
3816 // compute the position in the buffer line
3817 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003818 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003819 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003820 redrawWinline(wp, lnum);
3821 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003822 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003823
3824 // This line is going to be redrawn, no need to
3825 // check until the right side of the window.
3826 col_done = wp->w_wincol + wp->w_width - 1;
3827 }
3828 }
3829 }
3830 }
3831 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003832
3833 vim_free(plines_cache);
3834 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003835
3836 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003837}
3838
3839/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003840 * If the current window is a popup and something relevant changed, recompute
3841 * the position and size.
3842 */
3843 void
3844may_update_popup_position(void)
3845{
3846 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3847 popup_adjust_position(curwin);
3848}
3849
3850/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003851 * Return a string of "len" spaces in IObuff.
3852 */
3853 static char_u *
3854get_spaces(int len)
3855{
3856 vim_memset(IObuff, ' ', (size_t)len);
3857 IObuff[len] = NUL;
3858 return IObuff;
3859}
3860
3861/*
3862 * Update popup windows. They are drawn on top of normal windows.
3863 * "win_update" is called for each popup window, lowest zindex first.
3864 */
3865 void
3866update_popups(void (*win_update)(win_T *wp))
3867{
3868 win_T *wp;
3869 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003870 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003871 int total_width;
3872 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003873 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003874 int popup_attr;
3875 int border_attr[4];
3876 int border_char[8];
3877 char_u buf[MB_MAXBYTES];
3878 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003879 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003880 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003881 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003883 int sb_thumb_top = 0;
3884 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003885 int attr_scroll = 0;
3886 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003887
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003888 // hide the cursor until redrawing is done.
3889 cursor_off();
3890
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003891 // Find the window with the lowest zindex that hasn't been updated yet,
3892 // so that the window with a higher zindex is drawn later, thus goes on
3893 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003894 popup_reset_handled(POPUP_HANDLED_5);
3895 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003896 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003897 int title_len = 0;
3898 int title_wincol;
3899
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003900 // This drawing uses the zindex of the popup window, so that it's on
3901 // top of the text but doesn't draw when another popup with higher
3902 // zindex is on top of the character.
3903 screen_zindex = wp->w_zindex;
3904
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003905 // Set flags in popup_transparent[] for masked cells.
3906 update_popup_transparent(wp, 1);
3907
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003908 // adjust w_winrow and w_wincol for border and padding, since
3909 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003910 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003911 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3912 - wp->w_popup_leftoff;
3913 if (wp->w_wincol + left_extra < 0)
3914 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003915 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003916 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003917
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003918 // Draw the popup text, unless it's off screen.
3919 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003920 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003921 // May need to update the "cursorline" highlighting, which may also
3922 // change "topline"
3923 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3924 popup_highlight_curline(wp);
3925
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003926 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003927
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003928 // move the cursor into the visible lines, otherwise executing
3929 // commands with win_execute() may cause the text to jump.
3930 if (wp->w_cursor.lnum < wp->w_topline)
3931 wp->w_cursor.lnum = wp->w_topline;
3932 else if (wp->w_cursor.lnum >= wp->w_botline)
3933 wp->w_cursor.lnum = wp->w_botline - 1;
3934 }
3935
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003936 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003937 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003938
3939 // Add offset for border and padding if not done already.
3940 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3941 {
3942 wp->w_wcol += left_extra;
3943 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3944 }
3945 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003946 {
3947 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003948 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003949 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003950
Bram Moolenaareabddc42022-04-02 15:32:16 +01003951 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003952 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003953 popup_attr = get_wcr_attr(wp);
3954
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003955 if (wp->w_winrow + total_height > cmdline_row)
3956 wp->w_popup_flags |= POPF_ON_CMDLINE;
3957 else
3958 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3959
3960
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003961 // We can only use these line drawing characters when 'encoding' is
3962 // "utf-8" and 'ambiwidth' is "single".
3963 if (enc_utf8 && *p_ambw == 's')
3964 {
3965 border_char[0] = border_char[2] = 0x2550;
3966 border_char[1] = border_char[3] = 0x2551;
3967 border_char[4] = 0x2554;
3968 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003969 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3970 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003971 border_char[7] = 0x255a;
3972 }
3973 else
3974 {
3975 border_char[0] = border_char[2] = '-';
3976 border_char[1] = border_char[3] = '|';
3977 for (i = 4; i < 8; ++i)
3978 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003979 if (wp->w_popup_flags & POPF_RESIZE)
3980 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003981 }
3982 for (i = 0; i < 8; ++i)
3983 if (wp->w_border_char[i] != 0)
3984 border_char[i] = wp->w_border_char[i];
3985
3986 for (i = 0; i < 4; ++i)
3987 {
3988 border_attr[i] = popup_attr;
3989 if (wp->w_border_highlight[i] != NULL)
3990 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3991 }
3992
Bram Moolenaard91467f2020-11-21 12:42:09 +01003993 // Title goes on top of border or padding.
3994 title_wincol = wp->w_wincol + 1;
3995 if (wp->w_popup_title != NULL)
3996 {
rbtnnc6119412021-08-07 13:08:45 +02003997 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003998
Ralf Schandlbc869872021-05-28 14:12:14 +02003999 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004000 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004001 {
4002 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4003 char_u *title_text = alloc(title_byte_len + 1);
4004
4005 if (title_text != NULL)
4006 {
4007 trunc_string(wp->w_popup_title, title_text,
4008 total_width - 2, title_byte_len + 1);
4009 screen_puts(title_text, wp->w_winrow, title_wincol,
4010 wp->w_popup_border[0] > 0
4011 ? border_attr[0] : popup_attr);
4012 vim_free(title_text);
4013 }
4014
Bram Moolenaard91467f2020-11-21 12:42:09 +01004015 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004016 }
4017 else
4018 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4019 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004020 }
4021
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004022 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004023 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004024 if (wp->w_popup_border[0] > 0)
4025 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004026 // top border; do not draw over the title
4027 if (title_len > 0)
4028 {
4029 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4030 wincol < 0 ? 0 : wincol, title_wincol,
4031 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004032 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004033 border_char[0], border_attr[0]);
4034 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4035 title_wincol + title_len, wincol + total_width,
4036 border_char[0], border_char[0], border_attr[0]);
4037 }
4038 else
4039 {
4040 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4041 wincol < 0 ? 0 : wincol, wincol + total_width,
4042 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4043 ? border_char[4] : border_char[0],
4044 border_char[0], border_attr[0]);
4045 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004046 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004047 {
4048 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4049 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004050 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004051 }
4052 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004053 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4054 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004055
Bram Moolenaard529ba52019-07-02 23:13:53 +02004056 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4057 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004058 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004059 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004060 - wp->w_has_scrollbar;
4061 if (padcol < 0)
4062 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004063 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004064 padcol = 0;
4065 }
4066 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004067 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004068 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004069 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004070 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004071 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004072 // top padding and no border; do not draw over the title
4073 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004074 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004075 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004076 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004077 row += 1;
4078 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004079 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004080 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004081 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004082 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004083
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004084 // Compute scrollbar thumb position and size.
4085 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004086 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004087 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4088 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004089 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004090
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004091 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4092 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004093 if (wp->w_topline > 1 && sb_thumb_height == height)
4094 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004095 if (sb_thumb_height == 0)
4096 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004097 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004098 // it just fits, avoid divide by zero
4099 sb_thumb_top = 0;
4100 else
4101 sb_thumb_top = (wp->w_topline - 1
4102 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004103 * (wp->w_height - sb_thumb_height)
4104 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004105 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4106 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004107 last = total_height - top_off - wp->w_popup_border[2];
4108 if (sb_thumb_top >= last)
4109 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004110 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004111
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004112 if (wp->w_scrollbar_highlight != NULL)
4113 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4114 else
4115 attr_scroll = highlight_attr[HLF_PSB];
4116 if (wp->w_thumb_highlight != NULL)
4117 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4118 else
4119 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004120 }
4121
4122 for (i = wp->w_popup_border[0];
4123 i < total_height - wp->w_popup_border[2]; ++i)
4124 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004125 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004126 // left and right padding only needed next to the body
4127 int do_padding =
4128 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4129 && i < total_height - wp->w_popup_border[2]
4130 - wp->w_popup_padding[2];
4131
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004132 row = wp->w_winrow + i;
4133
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004134 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004135 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004136 {
4137 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004138 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004139 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004140 if (do_padding && wp->w_popup_padding[3] > 0)
4141 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004142 int col = wincol + wp->w_popup_border[3];
4143
Bram Moolenaard529ba52019-07-02 23:13:53 +02004144 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004145 pad_left = wp->w_popup_padding[3];
4146 if (col < 0)
4147 {
4148 pad_left += col;
4149 col = 0;
4150 }
4151 if (pad_left > 0)
4152 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4153 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004154 // scrollbar
4155 if (wp->w_has_scrollbar)
4156 {
4157 int line = i - top_off;
4158 int scroll_col = wp->w_wincol + total_width - 1
4159 - wp->w_popup_border[1];
4160
4161 if (line >= 0 && line < wp->w_height)
4162 screen_putchar(' ', row, scroll_col,
4163 line >= sb_thumb_top
4164 && line < sb_thumb_top + sb_thumb_height
4165 ? attr_thumb : attr_scroll);
4166 else
4167 screen_putchar(' ', row, scroll_col, popup_attr);
4168 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004169 // right border
4170 if (wp->w_popup_border[1] > 0)
4171 {
4172 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004173 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004174 }
4175 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004176 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004177 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004178 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004179 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4180 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004181 }
4182
4183 if (wp->w_popup_padding[2] > 0)
4184 {
4185 // bottom padding
4186 row = wp->w_winrow + wp->w_popup_border[0]
4187 + wp->w_popup_padding[0] + wp->w_height;
4188 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004189 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004190 }
4191
4192 if (wp->w_popup_border[2] > 0)
4193 {
4194 // bottom border
4195 row = wp->w_winrow + total_height - 1;
4196 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004197 wincol < 0 ? 0 : wincol,
4198 wincol + total_width,
4199 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004200 ? border_char[7] : border_char[2],
4201 border_char[2], border_attr[2]);
4202 if (wp->w_popup_border[1] > 0)
4203 {
4204 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004205 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004206 }
4207 }
4208
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004209 if (wp->w_popup_close == POPCLOSE_BUTTON)
4210 {
4211 // close button goes on top of anything at the top-right corner
4212 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004213 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004214 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4215 }
4216
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004217 update_popup_transparent(wp, 0);
4218
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004219 // Back to the normal zindex.
4220 screen_zindex = 0;
4221 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004222
4223#if defined(FEAT_SEARCH_EXTRA)
4224 // In case win_update() called start_search_hl().
4225 end_search_hl();
4226#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004227}
4228
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004229/*
4230 * Mark references in callbacks of one popup window.
4231 */
4232 static int
4233set_ref_in_one_popup(win_T *wp, int copyID)
4234{
4235 int abort = FALSE;
4236 typval_T tv;
4237
4238 if (wp->w_close_cb.cb_partial != NULL)
4239 {
4240 tv.v_type = VAR_PARTIAL;
4241 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4242 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4243 }
4244 if (wp->w_filter_cb.cb_partial != NULL)
4245 {
4246 tv.v_type = VAR_PARTIAL;
4247 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4248 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4249 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004250 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004251 return abort;
4252}
4253
4254/*
4255 * Set reference in callbacks of popup windows.
4256 */
4257 int
4258set_ref_in_popups(int copyID)
4259{
4260 int abort = FALSE;
4261 win_T *wp;
4262 tabpage_T *tp;
4263
4264 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4265 abort = abort || set_ref_in_one_popup(wp, copyID);
4266
4267 FOR_ALL_TABPAGES(tp)
4268 {
4269 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4270 abort = abort || set_ref_in_one_popup(wp, copyID);
4271 if (abort)
4272 break;
4273 }
4274 return abort;
4275}
Bram Moolenaar79648732019-07-18 21:43:07 +02004276
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004277 int
4278popup_is_popup(win_T *wp)
4279{
4280 return wp->w_popup_flags != 0;
4281}
4282
4283#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004284/*
4285 * Find an existing popup used as the preview window, in the current tab page.
4286 * Return NULL if not found.
4287 */
4288 win_T *
4289popup_find_preview_window(void)
4290{
4291 win_T *wp;
4292
4293 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004294 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004295 if (wp->w_p_pvw)
4296 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004297 return NULL;
4298}
4299
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004300/*
4301 * Find an existing popup used as the info window, in the current tab page.
4302 * Return NULL if not found.
4303 */
4304 win_T *
4305popup_find_info_window(void)
4306{
4307 win_T *wp;
4308
4309 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004310 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004311 if (wp->w_popup_flags & POPF_INFO)
4312 return wp;
4313 return NULL;
4314}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004315#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004316
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004317 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004318f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4319{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004320#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004321 win_T *wp = popup_find_info_window();
4322
4323 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004324#else
4325 rettv->vval.v_number = 0;
4326#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004327}
4328
4329 void
4330f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004331{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004332#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004333 win_T *wp = popup_find_preview_window();
4334
4335 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004336#else
4337 rettv->vval.v_number = 0;
4338#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004339}
4340
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004341#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004342/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004343 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004344 * NOTE: this makes the popup the current window, so that the file can be
4345 * edited. However, it must not remain to be the current window, the caller
4346 * must make sure of that.
4347 */
4348 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004349popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004350{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004351 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004352
4353 if (wp == NULL)
4354 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004355 if (info)
4356 wp->w_popup_flags |= POPF_INFO;
4357 else
4358 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004359
4360 // Set the width to a reasonable value, so that w_topline can be computed.
4361 if (wp->w_minwidth > 0)
4362 wp->w_width = wp->w_minwidth;
4363 else if (wp->w_maxwidth > 0)
4364 wp->w_width = wp->w_maxwidth;
4365 else
4366 wp->w_width = curwin->w_width;
4367
4368 // Will switch to another buffer soon, dummy one can be wiped.
4369 wp->w_buffer->b_locked = FALSE;
4370
4371 win_enter(wp, FALSE);
4372 return OK;
4373}
4374
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004375/*
4376 * Close any preview popup.
4377 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004378 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004379popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004380{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004381 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004382
4383 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004384 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004385}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004386
4387/*
4388 * Hide the info popup.
4389 */
4390 void
4391popup_hide_info(void)
4392{
4393 win_T *wp = popup_find_info_window();
4394
4395 if (wp != NULL)
4396 popup_hide(wp);
4397}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004398
4399/*
4400 * Close any info popup.
4401 */
4402 void
4403popup_close_info(void)
4404{
4405 win_T *wp = popup_find_info_window();
4406
4407 if (wp != NULL)
4408 popup_close_with_retval(wp, -1);
4409}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004410#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004411
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004412/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004413 * Close any popup for a text property associated with "win".
4414 * Return TRUE if a popup was closed.
4415 */
4416 int
4417popup_win_closed(win_T *win)
4418{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004419 int round;
4420 win_T *wp;
4421 win_T *next;
4422 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004423
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004424 for (round = 1; round <= 2; ++round)
4425 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4426 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004427 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004428 next = wp->w_next;
4429 if (wp->w_popup_prop_win == win)
4430 {
4431 popup_close_with_retval(wp, -1);
4432 ret = TRUE;
4433 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004434 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004435 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004436}
4437
4438/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004439 * Set the title of the popup window to the file name.
4440 */
4441 void
4442popup_set_title(win_T *wp)
4443{
4444 if (wp->w_buffer->b_fname != NULL)
4445 {
4446 char_u dirname[MAXPATHL];
4447 size_t len;
4448
4449 mch_dirname(dirname, MAXPATHL);
4450 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4451
4452 vim_free(wp->w_popup_title);
4453 len = STRLEN(wp->w_buffer->b_fname) + 3;
4454 wp->w_popup_title = alloc((int)len);
4455 if (wp->w_popup_title != NULL)
4456 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4457 wp->w_buffer->b_fname);
4458 redraw_win_later(wp, VALID);
4459 }
4460}
4461
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004462# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004463/*
4464 * If there is a preview window, update the title.
4465 * Used after changing directory.
4466 */
4467 void
4468popup_update_preview_title(void)
4469{
4470 win_T *wp = popup_find_preview_window();
4471
4472 if (wp != NULL)
4473 popup_set_title(wp);
4474}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004475# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004476
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004477#endif // FEAT_PROP_POPUP