blob: d5f8d26ee058efc373118e250a64f6b5e361195b [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)
89 emsg(_(e_listreq));
90 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
153/*
154 * Used when popup options contain "moved" with "word" or "WORD".
155 */
156 static void
157set_mousemoved_columns(win_T *wp, int flags)
158{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160 char_u *text;
161 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 pos_T pos;
163 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164
165 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200166 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200167 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200168 // convert text column to mouse column
169 pos.col = col;
170 pos.coladd = 0;
171 getvcol(textwp, &pos, &mcol, NULL, NULL);
172 wp->w_popup_mouse_mincol = mcol;
173
Bram Moolenaar10727682019-07-12 13:59:20 +0200174 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200175 getvcol(textwp, &pos, NULL, NULL, &mcol);
176 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200177 vim_free(text);
178 }
179}
180
181/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200182 * Return TRUE if "row"/"col" is on the border of the popup.
183 * The values are relative to the top-left corner.
184 */
185 int
186popup_on_border(win_T *wp, int row, int col)
187{
188 return (row == 0 && wp->w_popup_border[0] > 0)
189 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
190 || (col == 0 && wp->w_popup_border[3] > 0)
191 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
192}
193
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
196 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198 * Caller should check the left mouse button was clicked.
199 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200 */
201 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200202popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200203{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200204 if (wp->w_popup_close == POPCLOSE_BUTTON
205 && row == 0 && col == popup_width(wp) - 1)
206 {
207 popup_close_for_mouse_click(wp);
208 return TRUE;
209 }
210 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200211}
212
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200213// Values set when dragging a popup window starts.
214static int drag_start_row;
215static int drag_start_col;
216static int drag_start_wantline;
217static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200218static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200219
220/*
221 * Mouse down on border of popup window: start dragging it.
222 * Uses mouse_col and mouse_row.
223 */
224 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200225popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200226{
227 drag_start_row = mouse_row;
228 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200229 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200230 drag_start_wantline = wp->w_winrow + 1;
231 else
232 drag_start_wantline = wp->w_wantline;
233 if (wp->w_wantcol == 0)
234 drag_start_wantcol = wp->w_wincol + 1;
235 else
236 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200237
238 // Stop centering the popup
239 if (wp->w_popup_pos == POPPOS_CENTER)
240 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241
242 drag_on_resize_handle = wp->w_popup_border[1] > 0
243 && wp->w_popup_border[2] > 0
244 && row == popup_height(wp) - 1
245 && col == popup_width(wp) - 1;
246
247 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
248 {
249 if (wp->w_popup_pos == POPPOS_TOPRIGHT
250 || wp->w_popup_pos == POPPOS_BOTRIGHT)
251 wp->w_wantcol = wp->w_wincol + 1;
252 if (wp->w_popup_pos == POPPOS_BOTLEFT)
253 wp->w_wantline = wp->w_winrow + 1;
254 wp->w_popup_pos = POPPOS_TOPLEFT;
255 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200256}
257
258/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200259 * Mouse moved while dragging a popup window: adjust the window popup position
260 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261 */
262 void
263popup_drag(win_T *wp)
264{
265 // The popup may be closed before dragging stops.
266 if (!win_valid_popup(wp))
267 return;
268
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
270 {
271 int width_inc = mouse_col - drag_start_col;
272 int height_inc = mouse_row - drag_start_row;
273
274 if (width_inc != 0)
275 {
276 int width = wp->w_width + width_inc;
277
278 if (width < 1)
279 width = 1;
280 wp->w_minwidth = width;
281 wp->w_maxwidth = width;
282 drag_start_col = mouse_col;
283 }
284
285 if (height_inc != 0)
286 {
287 int height = wp->w_height + height_inc;
288
289 if (height < 1)
290 height = 1;
291 wp->w_minheight = height;
292 wp->w_maxheight = height;
293 drag_start_row = mouse_row;
294 }
295
296 popup_adjust_position(wp);
297 return;
298 }
299
300 if (!(wp->w_popup_flags & POPF_DRAG))
301 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200302 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
303 if (wp->w_wantline < 1)
304 wp->w_wantline = 1;
305 if (wp->w_wantline > Rows)
306 wp->w_wantline = Rows;
307 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
308 if (wp->w_wantcol < 1)
309 wp->w_wantcol = 1;
310 if (wp->w_wantcol > Columns)
311 wp->w_wantcol = Columns;
312
313 popup_adjust_position(wp);
314}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200315
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200316/*
317 * Set w_firstline to match the current "wp->w_topline".
318 */
319 void
320popup_set_firstline(win_T *wp)
321{
322 int height = wp->w_height;
323
324 wp->w_firstline = wp->w_topline;
325 popup_adjust_position(wp);
326
327 // we don't want the popup to get smaller, decrement the first line
328 // until it doesn't
329 while (wp->w_firstline > 1 && wp->w_height < height)
330 {
331 --wp->w_firstline;
332 popup_adjust_position(wp);
333 }
334}
335
336/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200337 * Return TRUE if the position is in the popup window scrollbar.
338 */
339 int
340popup_is_in_scrollbar(win_T *wp, int row, int col)
341{
342 return wp->w_has_scrollbar
343 && row >= wp->w_popup_border[0]
344 && row < popup_height(wp) - wp->w_popup_border[2]
345 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
346}
347
348
349/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200350 * Handle a click in a popup window, if it is in the scrollbar.
351 */
352 void
353popup_handle_scrollbar_click(win_T *wp, int row, int col)
354{
355 int height = popup_height(wp);
356 int old_topline = wp->w_topline;
357
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200358 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200359 {
360 if (row >= height / 2)
361 {
362 // Click in lower half, scroll down.
363 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
364 ++wp->w_topline;
365 }
366 else if (wp->w_topline > 1)
367 // click on upper half, scroll up.
368 --wp->w_topline;
369 if (wp->w_topline != old_topline)
370 {
371 popup_set_firstline(wp);
372 redraw_win_later(wp, NOT_VALID);
373 }
374 }
375}
376
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200377#if defined(FEAT_TIMERS)
378 static void
379popup_add_timeout(win_T *wp, int time)
380{
381 char_u cbbuf[50];
382 char_u *ptr = cbbuf;
383 typval_T tv;
384
385 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200386 "(_) => popup_close(%d)", wp->w_id);
387 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200388 {
389 wp->w_popup_timer = create_timer(time, 0);
390 wp->w_popup_timer->tr_callback = get_callback(&tv);
391 clear_tv(&tv);
392 }
393}
394#endif
395
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100396 static poppos_T
397get_pos_entry(dict_T *d, int give_error)
398{
399 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
400 int nr;
401
402 if (str == NULL)
403 return POPPOS_NONE;
404
K.Takataeeec2542021-06-02 13:28:16 +0200405 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100406 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
407 return poppos_entries[nr].pp_val;
408
409 if (give_error)
410 semsg(_(e_invarg2), str);
411 return POPPOS_NONE;
412}
413
Bram Moolenaar17627312019-06-02 19:53:44 +0200414/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200415 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200416 */
417 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200418apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200419{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200420 int nr;
421 char_u *str;
422 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200423
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200424 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200425 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200426 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200427 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200428 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200429 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200430 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200431 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200432
433 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200434 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200435 wp->w_wantline = nr;
436 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200437 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200438 wp->w_wantcol = nr;
439
Bram Moolenaar55881332020-08-18 13:04:15 +0200440
441 nr = dict_get_bool(d, (char_u *)"fixed", -1);
442 if (nr != -1)
443 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200444
Bram Moolenaar12034e22019-08-25 22:25:02 +0200445 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100446 poppos_T ppt = get_pos_entry(d, TRUE);
447
448 if (ppt != POPPOS_NONE)
449 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200450 }
451
452 str = dict_get_string(d, (char_u *)"textprop", FALSE);
453 if (str != NULL)
454 {
455 wp->w_popup_prop_type = 0;
456 if (*str != NUL)
457 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100458 wp->w_popup_prop_win = curwin;
459 di = dict_find(d, (char_u *)"textpropwin", -1);
460 if (di != NULL)
461 {
462 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100463 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100464 wp->w_popup_prop_win = curwin;
465 }
466
467 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200468 if (nr <= 0)
469 nr = find_prop_type_id(str, NULL);
470 if (nr <= 0)
471 semsg(_(e_invarg2), str);
472 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200473 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200474 }
475 }
476
477 di = dict_find(d, (char_u *)"textpropid", -1);
478 if (di != NULL)
479 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200480}
481
Bram Moolenaarb0992022020-01-30 14:55:42 +0100482/*
483 * Handle "moved" and "mousemoved" arguments.
484 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200485 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200486handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
487{
488 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
489 {
490 char_u *s = di->di_tv.vval.v_string;
491 int flags = 0;
492
493 if (STRCMP(s, "word") == 0)
494 flags = FIND_IDENT | FIND_STRING;
495 else if (STRCMP(s, "WORD") == 0)
496 flags = FIND_STRING;
497 else if (STRCMP(s, "expr") == 0)
498 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
499 else if (STRCMP(s, "any") != 0)
500 semsg(_(e_invarg2), s);
501 if (flags != 0)
502 {
503 if (mousemoved)
504 set_mousemoved_columns(wp, flags);
505 else
506 set_moved_columns(wp, flags);
507 }
508 }
509 else if (di->di_tv.v_type == VAR_LIST
510 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200511 && (di->di_tv.vval.v_list->lv_len == 2
512 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200513 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200514 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100515 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200516 int mincol;
517 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200518
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200519 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100520 li = l->lv_first;
521 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200522 {
523 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
524
525 // Three numbers, might be from popup_getoptions().
526 if (mousemoved)
527 wp->w_popup_mouse_row = nr;
528 else
529 wp->w_popup_lnum = nr;
530 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100531 if (nr == 0)
532 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200533 }
534
535 mincol = tv_get_number(&li->li_tv);
536 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200537 if (mousemoved)
538 {
539 wp->w_popup_mouse_mincol = mincol;
540 wp->w_popup_mouse_maxcol = maxcol;
541 }
542 else
543 {
544 wp->w_popup_mincol = mincol;
545 wp->w_popup_maxcol = maxcol;
546 }
547 }
548 else
549 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
550}
551
552 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200553check_highlight(dict_T *dict, char *name, char_u **pval)
554{
555 dictitem_T *di;
556 char_u *str;
557
558 di = dict_find(dict, (char_u *)name, -1);
559 if (di != NULL)
560 {
561 if (di->di_tv.v_type != VAR_STRING)
562 semsg(_(e_invargval), name);
563 else
564 {
565 str = tv_get_string(&di->di_tv);
566 if (*str != NUL)
567 *pval = vim_strsave(str);
568 }
569 }
570}
571
Bram Moolenaarae943152019-06-16 22:54:14 +0200572/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200573 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200574 */
575 static void
576popup_show_curline(win_T *wp)
577{
578 if (wp->w_cursor.lnum < wp->w_topline)
579 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200580 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200581 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200582 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200583 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200584 if (wp->w_topline < 1)
585 wp->w_topline = 1;
586 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
587 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200588 while (wp->w_topline < wp->w_cursor.lnum
589 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
590 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
591 > wp->w_height)
592 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200593 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200594
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200595 // Don't let "firstline" cause a scroll.
596 if (wp->w_firstline > 0)
597 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200598}
599
600/*
601 * Get the sign group name for window "wp".
602 * Returns a pointer to a static buffer, overwritten on the next call.
603 */
604 static char_u *
605popup_get_sign_name(win_T *wp)
606{
607 static char buf[30];
608
609 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
610 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200611}
612
613/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200614 * Highlight the line with the cursor.
615 * Also scrolls the text to put the cursor line in view.
616 */
617 static void
618popup_highlight_curline(win_T *wp)
619{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200620 int sign_id = 0;
621 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200622
Bram Moolenaar72570732019-11-30 14:21:53 +0100623 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200624
625 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
626 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200627 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200628
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200629 if (!sign_exists_by_name(sign_name))
630 {
631 char *linehl = "PopupSelected";
632
633 if (syn_name2id((char_u *)linehl) == 0)
634 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200635 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200636 }
637
Bram Moolenaar72570732019-11-30 14:21:53 +0100638 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200639 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
640 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200641 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200642 else
643 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200644 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200645}
646
647/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200648 * Shared between popup_create() and f_popup_setoptions().
649 */
650 static void
651apply_general_options(win_T *wp, dict_T *dict)
652{
653 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200654 int nr;
655 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200656
Bram Moolenaarae943152019-06-16 22:54:14 +0200657 // TODO: flip
658
659 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200660 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200661 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200662 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200663 if (wp->w_firstline < 0)
664 wp->w_firstline = -1;
665 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200666
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200667 nr = dict_get_bool(dict, (char_u *)"scrollbar", -1);
668 if (nr != -1)
669 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200670
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200671 str = dict_get_string(dict, (char_u *)"title", FALSE);
672 if (str != NULL)
673 {
674 vim_free(wp->w_popup_title);
675 wp->w_popup_title = vim_strsave(str);
676 }
677
Bram Moolenaar55881332020-08-18 13:04:15 +0200678 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
679 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200680 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200681
Bram Moolenaar55881332020-08-18 13:04:15 +0200682 nr = dict_get_bool(dict, (char_u *)"drag", -1);
683 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200684 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200685 if (nr)
686 wp->w_popup_flags |= POPF_DRAG;
687 else
688 wp->w_popup_flags &= ~POPF_DRAG;
689 }
690
Bram Moolenaar55881332020-08-18 13:04:15 +0200691 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
692 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100693 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100694 if (nr)
695 wp->w_popup_flags |= POPF_POSINVERT;
696 else
697 wp->w_popup_flags &= ~POPF_POSINVERT;
698 }
699
Bram Moolenaar55881332020-08-18 13:04:15 +0200700 nr = dict_get_bool(dict, (char_u *)"resize", -1);
701 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200702 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200703 if (nr)
704 wp->w_popup_flags |= POPF_RESIZE;
705 else
706 wp->w_popup_flags &= ~POPF_RESIZE;
707 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200708
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200709 di = dict_find(dict, (char_u *)"close", -1);
710 if (di != NULL)
711 {
712 int ok = TRUE;
713
714 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
715 {
716 char_u *s = di->di_tv.vval.v_string;
717
718 if (STRCMP(s, "none") == 0)
719 wp->w_popup_close = POPCLOSE_NONE;
720 else if (STRCMP(s, "button") == 0)
721 wp->w_popup_close = POPCLOSE_BUTTON;
722 else if (STRCMP(s, "click") == 0)
723 wp->w_popup_close = POPCLOSE_CLICK;
724 else
725 ok = FALSE;
726 }
727 else
728 ok = FALSE;
729 if (!ok)
730 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
731 }
732
Bram Moolenaarae943152019-06-16 22:54:14 +0200733 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
734 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000735 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200736 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
737 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000738#ifdef FEAT_TERMINAL
739 term_update_wincolor(wp);
740#endif
741 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200742
Bram Moolenaarae943152019-06-16 22:54:14 +0200743 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
744 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200745
Bram Moolenaar790498b2019-06-01 22:15:29 +0200746 di = dict_find(dict, (char_u *)"borderhighlight", -1);
747 if (di != NULL)
748 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200749 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200750 emsg(_(e_listreq));
751 else
752 {
753 list_T *list = di->di_tv.vval.v_list;
754 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200755 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200756
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200757 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200758 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
759 ++i, li = li->li_next)
760 {
761 str = tv_get_string(&li->li_tv);
762 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100763 {
764 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200765 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100766 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200767 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200768 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
769 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100770 {
771 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200772 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200773 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100774 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200775 }
776 }
777
Bram Moolenaar790498b2019-06-01 22:15:29 +0200778 di = dict_find(dict, (char_u *)"borderchars", -1);
779 if (di != NULL)
780 {
781 if (di->di_tv.v_type != VAR_LIST)
782 emsg(_(e_listreq));
783 else
784 {
785 list_T *list = di->di_tv.vval.v_list;
786 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200787 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200788
789 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100790 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200791 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200792 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
793 ++i, li = li->li_next)
794 {
795 str = tv_get_string(&li->li_tv);
796 if (*str != NUL)
797 wp->w_border_char[i] = mb_ptr2char(str);
798 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200799 if (list->lv_len == 1)
800 for (i = 1; i < 8; ++i)
801 wp->w_border_char[i] = wp->w_border_char[0];
802 if (list->lv_len == 2)
803 {
804 for (i = 4; i < 8; ++i)
805 wp->w_border_char[i] = wp->w_border_char[1];
806 for (i = 1; i < 4; ++i)
807 wp->w_border_char[i] = wp->w_border_char[0];
808 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200809 }
810 }
811 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200812
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200813 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
814 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
815
Bram Moolenaarae943152019-06-16 22:54:14 +0200816 di = dict_find(dict, (char_u *)"zindex", -1);
817 if (di != NULL)
818 {
819 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
820 if (wp->w_zindex < 1)
821 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
822 if (wp->w_zindex > 32000)
823 wp->w_zindex = 32000;
824 }
825
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200826 di = dict_find(dict, (char_u *)"mask", -1);
827 if (di != NULL)
828 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200829 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200830
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200831 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200832 {
833 listitem_T *li;
834
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200835 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200836 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200837 {
838 if (li->li_tv.v_type != VAR_LIST
839 || li->li_tv.vval.v_list == NULL
840 || li->li_tv.vval.v_list->lv_len != 4)
841 {
842 ok = FALSE;
843 break;
844 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100845 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200846 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200847 }
848 }
849 if (ok)
850 {
851 wp->w_popup_mask = di->di_tv.vval.v_list;
852 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200853 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200854 }
855 else
856 semsg(_(e_invargval), "mask");
857 }
858
Bram Moolenaarae943152019-06-16 22:54:14 +0200859#if defined(FEAT_TIMERS)
860 // Add timer to close the popup after some time.
861 nr = dict_get_number(dict, (char_u *)"time");
862 if (nr > 0)
863 popup_add_timeout(wp, nr);
864#endif
865
Bram Moolenaar3397f742019-06-02 18:40:06 +0200866 di = dict_find(dict, (char_u *)"moved", -1);
867 if (di != NULL)
868 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200869 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200870 handle_moved_argument(wp, di, FALSE);
871 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200872
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200873 di = dict_find(dict, (char_u *)"mousemoved", -1);
874 if (di != NULL)
875 {
876 set_mousemoved_values(wp);
877 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200878 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200879
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100880 nr = dict_get_bool(dict, (char_u *)"cursorline", -1);
881 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200882 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100883 if (nr != 0)
884 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200885 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100886 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200887 }
888
Bram Moolenaarae943152019-06-16 22:54:14 +0200889 di = dict_find(dict, (char_u *)"filter", -1);
890 if (di != NULL)
891 {
892 callback_T callback = get_callback(&di->di_tv);
893
894 if (callback.cb_name != NULL)
895 {
896 free_callback(&wp->w_filter_cb);
897 set_callback(&wp->w_filter_cb, &callback);
898 }
899 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200900 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
901 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200902 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200903 if (nr)
904 wp->w_popup_flags |= POPF_MAPPING;
905 else
906 wp->w_popup_flags &= ~POPF_MAPPING;
907 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200908
Bram Moolenaar581ba392019-09-03 22:08:33 +0200909 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
910 if (str != NULL)
911 {
912 if (STRCMP(str, "a") == 0)
913 wp->w_filter_mode = MODE_ALL;
914 else
915 wp->w_filter_mode = mode_str2flags(str);
916 }
917
Bram Moolenaarae943152019-06-16 22:54:14 +0200918 di = dict_find(dict, (char_u *)"callback", -1);
919 if (di != NULL)
920 {
921 callback_T callback = get_callback(&di->di_tv);
922
923 if (callback.cb_name != NULL)
924 {
925 free_callback(&wp->w_close_cb);
926 set_callback(&wp->w_close_cb, &callback);
927 }
928 }
929}
930
931/*
932 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200933 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200934 */
935 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200936apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200937{
938 int nr;
939
940 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200941
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200942 if (create)
943 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200944 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
945
Bram Moolenaarae943152019-06-16 22:54:14 +0200946 apply_general_options(wp, dict);
947
Bram Moolenaar55881332020-08-18 13:04:15 +0200948 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200949 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200950 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200951
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200952 // when "firstline" and "cursorline" are both set and the cursor would be
953 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200954 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
955 {
956 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
957 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200958 else if (wp->w_cursor.lnum < wp->w_firstline
959 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200960 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200961 wp->w_topline = wp->w_firstline;
962 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200963 }
964
Bram Moolenaar33796b32019-06-08 16:01:13 +0200965 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200966 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200967}
968
969/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200970 * Add lines to the popup from a list of strings.
971 */
972 static void
973add_popup_strings(buf_T *buf, list_T *l)
974{
975 listitem_T *li;
976 linenr_T lnum = 0;
977 char_u *p;
978
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200979 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200980 if (li->li_tv.v_type == VAR_STRING)
981 {
982 p = li->li_tv.vval.v_string;
983 ml_append_buf(buf, lnum++,
984 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
985 }
986}
987
988/*
989 * Add lines to the popup from a list of dictionaries.
990 */
991 static void
992add_popup_dicts(buf_T *buf, list_T *l)
993{
994 listitem_T *li;
995 listitem_T *pli;
996 linenr_T lnum = 0;
997 char_u *p;
998 dict_T *dict;
999
1000 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001001 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001002 {
1003 if (li->li_tv.v_type != VAR_DICT)
1004 {
1005 emsg(_(e_dictreq));
1006 return;
1007 }
1008 dict = li->li_tv.vval.v_dict;
1009 p = dict == NULL ? NULL
1010 : dict_get_string(dict, (char_u *)"text", FALSE);
1011 ml_append_buf(buf, lnum++,
1012 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1013 }
1014
1015 // add the text properties
1016 lnum = 1;
1017 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1018 {
1019 dictitem_T *di;
1020 list_T *plist;
1021
1022 dict = li->li_tv.vval.v_dict;
1023 di = dict_find(dict, (char_u *)"props", -1);
1024 if (di != NULL)
1025 {
1026 if (di->di_tv.v_type != VAR_LIST)
1027 {
1028 emsg(_(e_listreq));
1029 return;
1030 }
1031 plist = di->di_tv.vval.v_list;
1032 if (plist != NULL)
1033 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001034 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001035 {
1036 if (pli->li_tv.v_type != VAR_DICT)
1037 {
1038 emsg(_(e_dictreq));
1039 return;
1040 }
1041 dict = pli->li_tv.vval.v_dict;
1042 if (dict != NULL)
1043 {
1044 int col = dict_get_number(dict, (char_u *)"col");
1045
1046 prop_add_common( lnum, col, dict, buf, NULL);
1047 }
1048 }
1049 }
1050 }
1051 }
1052}
1053
1054/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001055 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1056 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001057 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001058popup_top_extra(win_T *wp)
1059{
1060 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1061
1062 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1063 return 1;
1064 return extra;
1065}
1066
1067/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001068 * Get the padding plus border at the left.
1069 */
1070 int
1071popup_left_extra(win_T *wp)
1072{
1073 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1074}
1075
1076/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001077 * Return the height of popup window "wp", including border and padding.
1078 */
1079 int
1080popup_height(win_T *wp)
1081{
1082 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001083 + popup_top_extra(wp)
1084 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001085}
1086
1087/*
1088 * Return the width of popup window "wp", including border, padding and
1089 * scrollbar.
1090 */
1091 int
1092popup_width(win_T *wp)
1093{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001094 // w_leftcol is how many columns of the core are left of the screen
1095 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001096 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001097 + popup_extra_width(wp)
1098 + wp->w_popup_rightoff;
1099}
1100
1101/*
1102 * Return the extra width of popup window "wp": border, padding and scrollbar.
1103 */
1104 int
1105popup_extra_width(win_T *wp)
1106{
1107 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1108 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1109 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001110}
1111
1112/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001113 * Adjust the position and size of the popup to fit on the screen.
1114 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001115 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001116popup_adjust_position(win_T *wp)
1117{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001118 linenr_T lnum;
1119 int wrapped = 0;
1120 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001121 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001122 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001123 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001124 int center_vert = FALSE;
1125 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001126 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001127 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001128 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1129 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1130 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1131 int extra_height = top_extra + bot_extra;
1132 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001133 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001134 int org_winrow = wp->w_winrow;
1135 int org_wincol = wp->w_wincol;
1136 int org_width = wp->w_width;
1137 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001138 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001139 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001140 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001141 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001142 int wantline = wp->w_wantline; // adjusted for textprop
1143 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001144 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001145 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001146
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001147 wp->w_winrow = 0;
1148 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001149 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001150 wp->w_popup_leftoff = 0;
1151 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001152
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001153 // May need to update the "cursorline" highlighting, which may also change
1154 // "topline"
1155 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1156 popup_highlight_curline(wp);
1157
Bram Moolenaar12034e22019-08-25 22:25:02 +02001158 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1159 {
1160 win_T *prop_win = wp->w_popup_prop_win;
1161 textprop_T prop;
1162 linenr_T prop_lnum;
1163 pos_T pos;
1164 int screen_row;
1165 int screen_scol;
1166 int screen_ccol;
1167 int screen_ecol;
1168
1169 // Popup window is positioned relative to a text property.
1170 if (find_visible_prop(prop_win,
1171 wp->w_popup_prop_type, wp->w_popup_prop_id,
1172 &prop, &prop_lnum) == FAIL)
1173 {
1174 // Text property is no longer visible, hide the popup.
1175 // Unhiding the popup is done in check_popup_unhidden().
1176 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1177 {
1178 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001179 if (win_valid(wp->w_popup_prop_win))
1180 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1181 }
1182 return;
1183 }
1184
1185 // Compute the desired position from the position of the text
1186 // property. Use "wantline" and "wantcol" as offsets.
1187 pos.lnum = prop_lnum;
1188 pos.col = prop.tp_col;
1189 if (wp->w_popup_pos == POPPOS_TOPLEFT
1190 || wp->w_popup_pos == POPPOS_BOTLEFT)
1191 pos.col += prop.tp_len - 1;
1192 textpos2screenpos(prop_win, &pos, &screen_row,
1193 &screen_scol, &screen_ccol, &screen_ecol);
1194
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001195 if (screen_scol == 0)
1196 {
1197 // position is off screen, make the width zero to hide it.
1198 wp->w_width = 0;
1199 return;
1200 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001201 if (wp->w_popup_pos == POPPOS_TOPLEFT
1202 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1203 // below the text
1204 wantline = screen_row + wantline + 1;
1205 else
1206 // above the text
1207 wantline = screen_row + wantline - 1;
1208 center_vert = FALSE;
1209 if (wp->w_popup_pos == POPPOS_TOPLEFT
1210 || wp->w_popup_pos == POPPOS_BOTLEFT)
1211 // right of the text
1212 wantcol = screen_ecol + wantcol;
1213 else
1214 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001215 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001216 use_wantcol = TRUE;
1217 }
1218 else
1219 {
1220 // If no line was specified default to vertical centering.
1221 if (wantline == 0)
1222 center_vert = TRUE;
1223 else if (wantline < 0)
1224 // If "wantline" is negative it actually means zero.
1225 wantline = 0;
1226 if (wantcol < 0)
1227 // If "wantcol" is negative it actually means zero.
1228 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001229 }
1230
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001231 if (wp->w_popup_pos == POPPOS_CENTER)
1232 {
1233 // center after computing the size
1234 center_vert = TRUE;
1235 center_hor = TRUE;
1236 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001237 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001238 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001239 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1240 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001241 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001242 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001243 if (wp->w_winrow >= Rows)
1244 wp->w_winrow = Rows - 1;
1245 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001246
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001247 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001248 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001249 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1250 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001251 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001252 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001253 // Need to see at least one character after the decoration.
1254 if (wp->w_wincol > Columns - left_extra - 1)
1255 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001256 }
1257 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001258
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001259 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001260 // When left aligned use the space available, but shift to the left when we
1261 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001262 maxspace = Columns - wp->w_wincol - left_extra;
1263 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001264 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001265 {
1266 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001267 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001268 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001269
1270 if (wp->w_p_nu || wp->w_p_rnu)
1271 margin_width = number_width(wp) + 1;
1272#ifdef FEAT_FOLDING
1273 margin_width += wp->w_p_fdc;
1274#endif
1275#ifdef FEAT_SIGNS
1276 if (signcolumn_on(wp))
1277 margin_width += 2;
1278#endif
1279 if (margin_width >= maxwidth)
1280 margin_width = maxwidth - 1;
1281
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001282 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001283 minheight = wp->w_minheight;
1284#ifdef FEAT_TERMINAL
1285 // A terminal popup initially does not have content, use a default minimal
1286 // width of 20 characters and height of 5 lines.
1287 if (wp->w_buffer->b_term != NULL)
1288 {
1289 if (minwidth == 0)
1290 minwidth = 20;
1291 if (minheight == 0)
1292 minheight = 5;
1293 }
1294#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001295
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001296 if (wp->w_maxheight > 0)
1297 maxheight = wp->w_maxheight;
1298
Bram Moolenaar8d241042019-06-12 23:40:01 +02001299 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001300 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001301 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001302 if (wp->w_topline < 1)
1303 wp->w_topline = 1;
1304 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001305 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1306
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001307 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001308 // Use a minimum width of one, so that something shows when there is no
1309 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001310 // When "firstline" is -1 then start with the last buffer line and go
1311 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001312 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001313 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001314 if (wp->w_firstline < 0)
1315 lnum = wp->w_buffer->b_ml.ml_line_count;
1316 else
1317 lnum = wp->w_topline;
1318 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001319 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001320 int len;
1321 int w_width = wp->w_width;
1322
1323 // Count Tabs for what they are worth and compute the length based on
1324 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001325 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001326 if (wp->w_width < maxwidth)
1327 wp->w_width = maxwidth;
1328 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001329 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001330 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001331
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001332 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001333 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001334 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001335 {
1336 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001337 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001338 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001339 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001340 }
1341 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001342 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001343 && allow_adjust_left
1344 && (wp->w_popup_pos == POPPOS_TOPLEFT
1345 || wp->w_popup_pos == POPPOS_BOTLEFT))
1346 {
1347 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001348 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001349
Bram Moolenaar51c31312019-06-15 22:27:23 +02001350 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001351 {
1352 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001353
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001354 len -= truncate_shift;
1355 shift_by -= truncate_shift;
1356 }
1357
1358 wp->w_wincol -= shift_by;
1359 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001360 wp->w_width = maxwidth;
1361 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001362 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001363 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001364 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001365 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1366 wp->w_width = wp->w_maxwidth;
1367 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001368
1369 if (wp->w_firstline < 0)
1370 --lnum;
1371 else
1372 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001373
1374 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001375 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001376 && (wp->w_firstline >= 0
1377 ? lnum - wp->w_topline
1378 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001379 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001380 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001381 }
1382
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001383 if (wp->w_firstline < 0)
1384 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1385
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001386 wp->w_has_scrollbar = wp->w_want_scrollbar
1387 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001388#ifdef FEAT_TERMINAL
1389 if (wp->w_buffer->b_term != NULL)
1390 // Terminal window never has a scrollbar, adjusts to window height.
1391 wp->w_has_scrollbar = FALSE;
1392#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001393 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001394 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001395 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001396 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001397 // make space for the scrollbar if needed, when lines wrap and when
1398 // applying minwidth
1399 if (maxwidth + right_extra >= maxspace
1400 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1401 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001402 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001403
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001404 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1405 {
1406 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1407
1408 if (minwidth < title_len)
1409 minwidth = title_len;
1410 }
1411
1412 if (minwidth > 0 && wp->w_width < minwidth)
1413 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001414 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001415 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001416 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001417 // some columns cut off on the right
1418 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001419 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001420 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001421 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001422 {
1423 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1424 if (wp->w_wincol < 0)
1425 wp->w_wincol = 0;
1426 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001427 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1428 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1429 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001430 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001431
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001432 // Right aligned: move to the right if needed.
1433 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001434 if (leftoff >= 0)
1435 wp->w_wincol = leftoff;
1436 else if (wp->w_popup_fixed)
1437 {
1438 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001439 // columns when displaying the window, minus left border and
1440 // padding.
1441 if (-leftoff > left_extra)
1442 wp->w_leftcol = -leftoff - left_extra;
1443 wp->w_width -= wp->w_leftcol;
1444 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001445 if (wp->w_width < 0)
1446 wp->w_width = 0;
1447 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001448 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001449
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001450 if (wp->w_p_wrap || (!wp->w_popup_fixed
1451 && (wp->w_popup_pos == POPPOS_TOPLEFT
1452 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001453 {
1454 int want_col = 0;
1455
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001456 // try to show the right border and any scrollbar
1457 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001458 if (want_col > 0 && wp->w_wincol > 0
1459 && wp->w_wincol + want_col >= Columns)
1460 {
1461 wp->w_wincol = Columns - want_col;
1462 if (wp->w_wincol < 0)
1463 wp->w_wincol = 0;
1464 }
1465 }
1466
Bram Moolenaar8d241042019-06-12 23:40:01 +02001467 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1468 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001469 if (minheight > 0 && wp->w_height < minheight)
1470 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001471 if (maxheight > 0 && wp->w_height > maxheight)
1472 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001473 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001474 if (wp->w_height > Rows - wp->w_winrow)
1475 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001476
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001477 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001478 {
1479 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1480 if (wp->w_winrow < 0)
1481 wp->w_winrow = 0;
1482 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001483 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001484 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001485 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001486 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001487 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001488 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001489 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1490 {
1491 // Bottom aligned but does not fit, and less space on the other
1492 // side or "posinvert" is off: reduce height.
1493 wp->w_winrow = 0;
1494 wp->w_height = wantline - extra_height;
1495 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001496 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001497 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001498 // Not enough space and more space on the other side: make top
1499 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001500 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001501 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001502 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001503 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001504 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1505 || wp->w_popup_pos == POPPOS_TOPLEFT)
1506 {
1507 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1508 && wantline * 2 > Rows
1509 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001510 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001511 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001512 // make bottom aligned and recompute the height
1513 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001514 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001515 if (wp->w_winrow < 0)
1516 {
1517 wp->w_height += wp->w_winrow;
1518 wp->w_winrow = 0;
1519 }
1520 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001521 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001522 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001523 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001524 adjust_height_for_top_aligned = TRUE;
1525 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001526 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001527
1528 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1529 && wp->w_winrow + wp->w_height + extra_height > Rows)
1530 {
1531 // Bottom of the popup goes below the last line, reduce the height and
1532 // add a scrollbar.
1533 wp->w_height = Rows - wp->w_winrow - extra_height;
1534#ifdef FEAT_TERMINAL
1535 if (wp->w_buffer->b_term == NULL)
1536#endif
1537 wp->w_has_scrollbar = TRUE;
1538 }
1539
1540 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001541 if (wp->w_winrow >= Rows)
1542 wp->w_winrow = Rows - 1;
1543 else if (wp->w_winrow < 0)
1544 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001545
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001546 if (wp->w_height != org_height)
1547 win_comp_scroll(wp);
1548
Bram Moolenaar17146962019-05-30 00:12:11 +02001549 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001550 if (win_valid(wp->w_popup_prop_win))
1551 {
1552 wp->w_popup_prop_changedtick =
1553 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1554 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1555 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001556
1557 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001558 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001559 if (org_winrow != wp->w_winrow
1560 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001561 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001562 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001563 || org_width != wp->w_width
1564 || org_height != wp->w_height)
1565 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001566 redraw_win_later(wp, NOT_VALID);
1567 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1568 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001569 popup_mask_refresh = TRUE;
1570 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001571}
1572
Bram Moolenaar17627312019-06-02 19:53:44 +02001573typedef enum
1574{
1575 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001576 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001577 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001578 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001579 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001580 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001581 TYPE_PREVIEW, // preview window
1582 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001583} create_type_T;
1584
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001585/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001586 * Make "buf" empty and set the contents to "text".
1587 * Used by popup_create() and popup_settext().
1588 */
1589 static void
1590popup_set_buffer_text(buf_T *buf, typval_T text)
1591{
1592 int lnum;
1593
1594 // Clear the buffer, then replace the lines.
1595 curbuf = buf;
1596 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001597 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001598 curbuf = curwin->w_buffer;
1599
1600 // Add text to the buffer.
1601 if (text.v_type == VAR_STRING)
1602 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001603 char_u *s = text.vval.v_string;
1604
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001605 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001606 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001607 }
1608 else
1609 {
1610 list_T *l = text.vval.v_list;
1611
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001612 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001613 {
1614 if (l->lv_first->li_tv.v_type == VAR_STRING)
1615 // list of strings
1616 add_popup_strings(buf, l);
1617 else
1618 // list of dictionaries
1619 add_popup_dicts(buf, l);
1620 }
1621 }
1622
1623 // delete the line that was in the empty buffer
1624 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001625 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001626 curbuf = curwin->w_buffer;
1627}
1628
1629/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001630 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1631 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001632 * Return FAIL if the parsing fails.
1633 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001634 static int
1635parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001636{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001637 char_u *p =
1638#ifdef FEAT_QUICKFIX
1639 !is_preview ? p_cpp :
1640#endif
1641 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001642
Bram Moolenaar258cef52019-08-21 17:29:29 +02001643 if (wp != NULL)
1644 wp->w_popup_flags &= ~POPF_INFO_MENU;
1645
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001646 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001647 {
1648 char_u *e, *dig;
1649 char_u *s = p;
1650 int x;
1651
1652 e = vim_strchr(p, ':');
1653 if (e == NULL || e[1] == NUL)
1654 return FAIL;
1655
1656 p = vim_strchr(e, ',');
1657 if (p == NULL)
1658 p = e + STRLEN(e);
1659 dig = e + 1;
1660 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001661
1662 if (STRNCMP(s, "height:", 7) == 0)
1663 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001664 if (dig != p)
1665 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001666 if (wp != NULL)
1667 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001668 if (is_preview)
1669 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001670 wp->w_maxheight = x;
1671 }
1672 }
1673 else if (STRNCMP(s, "width:", 6) == 0)
1674 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001675 if (dig != p)
1676 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001677 if (wp != NULL)
1678 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001679 if (is_preview)
1680 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001681 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001682 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001683 }
1684 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001685 else if (STRNCMP(s, "highlight:", 10) == 0)
1686 {
1687 if (wp != NULL)
1688 {
1689 int c = *p;
1690
1691 *p = NUL;
1692 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1693 s + 10, OPT_FREE|OPT_LOCAL, 0);
1694 *p = c;
1695 }
1696 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001697 else if (STRNCMP(s, "border:", 7) == 0)
1698 {
1699 char_u *arg = s + 7;
1700 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1701 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1702 int i;
1703
1704 if (!on && !off)
1705 return FAIL;
1706 if (wp != NULL)
1707 {
1708 for (i = 0; i < 4; ++i)
1709 wp->w_popup_border[i] = on ? 1 : 0;
1710 if (off)
1711 // only show the X for close when there is a border
1712 wp->w_popup_close = POPCLOSE_NONE;
1713 }
1714 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001715 else if (STRNCMP(s, "align:", 6) == 0)
1716 {
1717 char_u *arg = s + 6;
1718 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1719 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1720
1721 if (!menu && !item)
1722 return FAIL;
1723 if (wp != NULL && menu)
1724 wp->w_popup_flags |= POPF_INFO_MENU;
1725 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001726 else
1727 return FAIL;
1728 }
1729 return OK;
1730}
1731
1732/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001733 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1734 * is not NULL.
1735 * Return FAIL if the parsing fails.
1736 */
1737 int
1738parse_previewpopup(win_T *wp)
1739{
1740 return parse_popup_option(wp, TRUE);
1741}
1742
1743/*
1744 * Parse the 'completepopup' option and apply the values to window "wp" if it
1745 * is not NULL.
1746 * Return FAIL if the parsing fails.
1747 */
1748 int
1749parse_completepopup(win_T *wp)
1750{
1751 return parse_popup_option(wp, FALSE);
1752}
1753
1754/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001755 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001756 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001757 */
1758 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001759popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001760{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001761 poppos_T ppt = POPPOS_NONE;
1762
1763 if (d != NULL)
1764 ppt = get_pos_entry(d, FALSE);
1765
Bram Moolenaar79648732019-07-18 21:43:07 +02001766 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001767 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001768 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001769 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1770 }
1771 else
1772 {
1773 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1774 if (wp->w_wantline == 0) // cursor in first line
1775 {
1776 wp->w_wantline = 2;
1777 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1778 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1779 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001780 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001781
Bram Moolenaar79648732019-07-18 21:43:07 +02001782 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001783 if (wp->w_wantcol > Columns - width)
1784 {
1785 wp->w_wantcol = Columns - width;
1786 if (wp->w_wantcol < 1)
1787 wp->w_wantcol = 1;
1788 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001789
Bram Moolenaar79648732019-07-18 21:43:07 +02001790 popup_adjust_position(wp);
1791}
1792
1793/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001794 * Set w_wantline and w_wantcol for the a given screen position.
1795 * Caller must take care of running into the window border.
1796 */
1797 void
1798popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1799{
1800 wp->w_wantline = row;
1801 wp->w_wantcol = col;
1802 popup_adjust_position(wp);
1803}
1804
1805/*
1806 * Add a border and lef&right padding.
1807 */
1808 static void
1809add_border_left_right_padding(win_T *wp)
1810{
1811 int i;
1812
1813 for (i = 0; i < 4; ++i)
1814 {
1815 wp->w_popup_border[i] = 1;
1816 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1817 }
1818}
1819
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001820#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001821/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001822 * Return TRUE if there is any popup window with a terminal buffer.
1823 */
1824 static int
1825popup_terminal_exists(void)
1826{
1827 win_T *wp;
1828 tabpage_T *tp;
1829
1830 FOR_ALL_POPUPWINS(wp)
1831 if (wp->w_buffer->b_term != NULL)
1832 return TRUE;
1833 FOR_ALL_TABPAGES(tp)
1834 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1835 if (wp->w_buffer->b_term != NULL)
1836 return TRUE;
1837 return FALSE;
1838}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001839#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001840
1841/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001842 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001843 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001844 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001845 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001846 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001847 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001848popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001849{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001850 win_T *wp;
1851 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001852 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001853 int new_buffer;
1854 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001855 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001856 int nr;
1857 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001858
Bram Moolenaar79648732019-07-18 21:43:07 +02001859 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001860 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001861 if (in_vim9script()
1862 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1863 || check_for_dict_arg(argvars, 1) == FAIL))
1864 return NULL;
1865
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001866 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001867 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001868 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001869 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001870 if (buf == NULL)
1871 {
1872 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1873 return NULL;
1874 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001875#ifdef FEAT_TERMINAL
1876 if (buf->b_term != NULL && popup_terminal_exists())
1877 {
1878 emsg(_("E861: Cannot open a second popup with a terminal"));
1879 return NULL;
1880 }
1881#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001882 }
1883 else if (!(argvars[0].v_type == VAR_STRING
1884 && argvars[0].vval.v_string != NULL)
1885 && !(argvars[0].v_type == VAR_LIST
1886 && argvars[0].vval.v_list != NULL))
1887 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001888 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001889 return NULL;
1890 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001891 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001892 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001893 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001894 return NULL;
1895 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001896 d = argvars[1].vval.v_dict;
1897 }
1898
1899 if (d != NULL)
1900 {
1901 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1902 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1903 else if (type == TYPE_NOTIFICATION)
1904 tabnr = -1; // notifications are global by default
1905 else
1906 tabnr = 0;
1907 if (tabnr > 0)
1908 {
1909 tp = find_tabpage(tabnr);
1910 if (tp == NULL)
1911 {
1912 semsg(_("E997: Tabpage not found: %d"), tabnr);
1913 return NULL;
1914 }
1915 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001916 }
1917
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001918 // Create the window and buffer.
1919 wp = win_alloc_popup_win();
1920 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001921 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001922 if (rettv != NULL)
1923 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001924 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001925 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001926
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001927 if (buf != NULL)
1928 {
1929 // use existing buffer
1930 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001931 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001932 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001933 buffer_ensure_loaded(buf);
1934 }
1935 else
1936 {
1937 // create a new buffer associated with the popup
1938 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001939 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001940 if (buf == NULL)
1941 return NULL;
1942 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001943
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001944 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001945
Bram Moolenaar46451042019-08-24 15:50:46 +02001946 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001947 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001948 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001949 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001950 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001951 buf->b_p_ul = -1; // no undo
1952 buf->b_p_swf = FALSE; // no swap file
1953 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001954 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001955
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001956 // Avoid that 'buftype' is reset when this buffer is entered.
1957 buf->b_p_initialized = TRUE;
1958 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001959 wp->w_p_wrap = TRUE; // 'wrap' is default on
1960 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001961
Bram Moolenaara3fce622019-06-20 02:31:49 +02001962 if (tp != NULL)
1963 {
1964 // popup on specified tab page
1965 wp->w_next = tp->tp_first_popupwin;
1966 tp->tp_first_popupwin = wp;
1967 }
1968 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001969 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001970 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001971 wp->w_next = curtab->tp_first_popupwin;
1972 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001973 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001974 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001975 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001976 win_T *prev = first_popupwin;
1977
1978 // Global popup: add at the end, so that it gets displayed on top of
1979 // older ones with the same zindex. Matters for notifications.
1980 if (first_popupwin == NULL)
1981 first_popupwin = wp;
1982 else
1983 {
1984 while (prev->w_next != NULL)
1985 prev = prev->w_next;
1986 prev->w_next = wp;
1987 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001988 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001989
Bram Moolenaar79648732019-07-18 21:43:07 +02001990 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001991 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001992
Bram Moolenaar79648732019-07-18 21:43:07 +02001993 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001994 {
1995 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001996 }
1997 if (type == TYPE_ATCURSOR)
1998 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001999 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002000 set_moved_values(wp);
2001 set_moved_columns(wp, FIND_STRING);
2002 }
2003
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002004 if (type == TYPE_BEVAL)
2005 {
2006 wp->w_popup_pos = POPPOS_BOTLEFT;
2007
2008 // by default use the mouse position
2009 wp->w_wantline = mouse_row;
2010 if (wp->w_wantline <= 0) // mouse on first line
2011 {
2012 wp->w_wantline = 2;
2013 wp->w_popup_pos = POPPOS_TOPLEFT;
2014 }
2015 wp->w_wantcol = mouse_col + 1;
2016 set_mousemoved_values(wp);
2017 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2018 }
2019
Bram Moolenaar33796b32019-06-08 16:01:13 +02002020 // set default values
2021 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002022 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002023
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002024 if (type == TYPE_NOTIFICATION)
2025 {
2026 win_T *twp, *nextwin;
2027 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002028
2029 // Try to not overlap with another global popup. Guess we need 3
2030 // more screen lines than buffer lines.
2031 wp->w_wantline = 1;
2032 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2033 {
2034 nextwin = twp->w_next;
2035 if (twp != wp
2036 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2037 && twp->w_winrow <= wp->w_wantline - 1 + height
2038 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2039 {
2040 // move to below this popup and restart the loop to check for
2041 // overlap with other popups
2042 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2043 nextwin = first_popupwin;
2044 }
2045 }
2046 if (wp->w_wantline + height > Rows)
2047 {
2048 // can't avoid overlap, put on top in the hope that message goes
2049 // away soon.
2050 wp->w_wantline = 1;
2051 }
2052
2053 wp->w_wantcol = 10;
2054 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002055 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002056 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002057 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002058 for (i = 0; i < 4; ++i)
2059 wp->w_popup_border[i] = 1;
2060 wp->w_popup_padding[1] = 1;
2061 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002062
2063 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002064 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002065 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2066 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002067 }
2068
Bram Moolenaara730e552019-06-16 19:05:31 +02002069 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002070 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002071 wp->w_popup_pos = POPPOS_CENTER;
2072 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002073 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002074 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002075 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002076 }
2077
Bram Moolenaara730e552019-06-16 19:05:31 +02002078 if (type == TYPE_MENU)
2079 {
2080 typval_T tv;
2081 callback_T callback;
2082
2083 tv.v_type = VAR_STRING;
2084 tv.vval.v_string = (char_u *)"popup_filter_menu";
2085 callback = get_callback(&tv);
2086 if (callback.cb_name != NULL)
2087 set_callback(&wp->w_filter_cb, &callback);
2088
2089 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002090 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002091 }
2092
Bram Moolenaar79648732019-07-18 21:43:07 +02002093 if (type == TYPE_PREVIEW)
2094 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002095 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002096 wp->w_popup_close = POPCLOSE_BUTTON;
2097 for (i = 0; i < 4; ++i)
2098 wp->w_popup_border[i] = 1;
2099 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002100 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002101 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002102# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002103 if (type == TYPE_INFO)
2104 {
2105 wp->w_popup_pos = POPPOS_TOPLEFT;
2106 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2107 wp->w_popup_close = POPCLOSE_BUTTON;
2108 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002109 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002110 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002111# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002112
Bram Moolenaarae943152019-06-16 22:54:14 +02002113 for (i = 0; i < 4; ++i)
2114 VIM_CLEAR(wp->w_border_highlight[i]);
2115 for (i = 0; i < 8; ++i)
2116 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002117 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002118 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002119 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002120
Bram Moolenaar79648732019-07-18 21:43:07 +02002121 if (d != NULL)
2122 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002123 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002124
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002125#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002126 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2127 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002128#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002129
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002130 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002131
2132 wp->w_vsep_width = 0;
2133
2134 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002135 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002136
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002137#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002138 // When running a terminal in the popup it becomes the current window.
2139 if (buf->b_term != NULL)
2140 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002141#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002142
Bram Moolenaara730e552019-06-16 19:05:31 +02002143 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002144}
2145
2146/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002147 * popup_clear()
2148 */
2149 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002150f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002151{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002152 int force = FALSE;
2153
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002154 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2155 return;
2156
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002157 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002158 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002159 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002160}
2161
2162/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002163 * popup_create({text}, {options})
2164 */
2165 void
2166f_popup_create(typval_T *argvars, typval_T *rettv)
2167{
Bram Moolenaar17627312019-06-02 19:53:44 +02002168 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002169}
2170
2171/*
2172 * popup_atcursor({text}, {options})
2173 */
2174 void
2175f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2176{
Bram Moolenaar17627312019-06-02 19:53:44 +02002177 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002178}
2179
2180/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002181 * popup_beval({text}, {options})
2182 */
2183 void
2184f_popup_beval(typval_T *argvars, typval_T *rettv)
2185{
2186 popup_create(argvars, rettv, TYPE_BEVAL);
2187}
2188
2189/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002190 * Invoke the close callback for window "wp" with value "result".
2191 * Careful: The callback may make "wp" invalid!
2192 */
2193 static void
2194invoke_popup_callback(win_T *wp, typval_T *result)
2195{
2196 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002197 typval_T argv[3];
2198
2199 argv[0].v_type = VAR_NUMBER;
2200 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2201
2202 if (result != NULL && result->v_type != VAR_UNKNOWN)
2203 copy_tv(result, &argv[1]);
2204 else
2205 {
2206 argv[1].v_type = VAR_NUMBER;
2207 argv[1].vval.v_number = 0;
2208 }
2209
2210 argv[2].v_type = VAR_UNKNOWN;
2211
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002212 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002213 if (result != NULL)
2214 clear_tv(&argv[1]);
2215 clear_tv(&rettv);
2216}
2217
2218/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002219 * Make "prevwin" the current window, unless it's equal to "wp".
2220 * Otherwise make "firstwin" the current window.
2221 */
2222 static void
2223back_to_prevwin(win_T *wp)
2224{
2225 if (win_valid(prevwin) && wp != prevwin)
2226 win_enter(prevwin, FALSE);
2227 else
2228 win_enter(firstwin, FALSE);
2229}
2230
2231/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002232 * Close popup "wp" and invoke any close callback for it.
2233 */
2234 static void
2235popup_close_and_callback(win_T *wp, typval_T *arg)
2236{
2237 int id = wp->w_id;
2238
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002239#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002240 if (wp == curwin && curbuf->b_term != NULL)
2241 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002242 win_T *owp;
2243
2244 // Closing popup window with a terminal: put focus back on the first
2245 // that works:
2246 // - another popup window with a terminal
2247 // - the previous window
2248 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002249 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002250 if (owp != curwin && owp->w_buffer->b_term != NULL)
2251 break;
2252 if (owp != NULL)
2253 win_enter(owp, FALSE);
2254 else
2255 {
2256 for (owp = curtab->tp_first_popupwin; owp != NULL;
2257 owp = owp->w_next)
2258 if (owp != curwin && owp->w_buffer->b_term != NULL)
2259 break;
2260 if (owp != NULL)
2261 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002262 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002263 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002264 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002265 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002266#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002267
Bram Moolenaar49540192019-12-11 19:34:54 +01002268 // Just in case a check higher up is missing.
2269 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002270 {
2271 // To avoid getting stuck when win_execute() does something that causes
2272 // an error, stop calling the filter callback.
2273 free_callback(&wp->w_filter_cb);
2274
Bram Moolenaar49540192019-12-11 19:34:54 +01002275 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002276 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002277
Bram Moolenaarcee52202020-03-11 14:19:58 +01002278 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002279 if (wp->w_close_cb.cb_name != NULL)
2280 // Careful: This may make "wp" invalid.
2281 invoke_popup_callback(wp, arg);
2282
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002283 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002284 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002285}
2286
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002287 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002288popup_close_with_retval(win_T *wp, int retval)
2289{
2290 typval_T res;
2291
2292 res.v_type = VAR_NUMBER;
2293 res.vval.v_number = retval;
2294 popup_close_and_callback(wp, &res);
2295}
2296
Bram Moolenaar3397f742019-06-02 18:40:06 +02002297/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002298 * Close popup "wp" because of a mouse click.
2299 */
2300 void
2301popup_close_for_mouse_click(win_T *wp)
2302{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002303 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002304}
2305
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002306 static void
2307check_mouse_moved(win_T *wp, win_T *mouse_wp)
2308{
2309 // Close the popup when all if these are true:
2310 // - the mouse is not on this popup
2311 // - "mousemoved" was used
2312 // - the mouse is no longer on the same screen row or the mouse column is
2313 // outside of the relevant text
2314 if (wp != mouse_wp
2315 && wp->w_popup_mouse_row != 0
2316 && (wp->w_popup_mouse_row != mouse_row
2317 || mouse_col < wp->w_popup_mouse_mincol
2318 || mouse_col > wp->w_popup_mouse_maxcol))
2319 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002320 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002321 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002322 }
2323}
2324
2325/*
2326 * Called when the mouse moved: may close a popup with "mousemoved".
2327 */
2328 void
2329popup_handle_mouse_moved(void)
2330{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002331 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002332 win_T *mouse_wp;
2333 int row = mouse_row;
2334 int col = mouse_col;
2335
2336 // find the window where the mouse is in
2337 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2338
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002339 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2340 {
2341 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002342 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002343 }
2344 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2345 {
2346 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002347 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002348 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002349}
2350
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002351/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002352 * In a filter: check if the typed key is a mouse event that is used for
2353 * dragging the popup.
2354 */
2355 static void
2356filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2357{
2358 int row = mouse_row;
2359 int col = mouse_col;
2360
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002361 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002362 && is_mouse_key(c)
2363 && (wp == popup_dragwin
2364 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2365 // do not consume the key, allow for dragging the popup
2366 rettv->vval.v_number = 0;
2367}
2368
Bram Moolenaara730e552019-06-16 19:05:31 +02002369/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002370 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002371 */
2372 void
2373f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2374{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002375 int id;
2376 win_T *wp;
2377 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002378 typval_T res;
2379 int c;
2380 linenr_T old_lnum;
2381
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002382 if (in_vim9script()
2383 && (check_for_number_arg(argvars, 0) == FAIL
2384 || check_for_string_arg(argvars, 1) == FAIL))
2385 return;
2386
2387 id = tv_get_number(&argvars[0]);
2388 wp = win_id2wp(id);
2389 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002390 // If the popup has been closed do not consume the key.
2391 if (wp == NULL)
2392 return;
2393
2394 c = *key;
2395 if (c == K_SPECIAL && key[1] != NUL)
2396 c = TO_SPECIAL(key[1], key[2]);
2397
2398 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002399 rettv->v_type = VAR_BOOL;
2400 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002401 res.v_type = VAR_NUMBER;
2402
2403 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002404 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2405 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002406 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002407 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002408 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2409 ++wp->w_cursor.lnum;
2410 if (old_lnum != wp->w_cursor.lnum)
2411 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002412 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002413 return;
2414 }
2415
2416 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2417 {
2418 // Cancelled, invoke callback with -1
2419 res.vval.v_number = -1;
2420 popup_close_and_callback(wp, &res);
2421 return;
2422 }
2423 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2424 {
2425 // Invoke callback with current index.
2426 res.vval.v_number = wp->w_cursor.lnum;
2427 popup_close_and_callback(wp, &res);
2428 return;
2429 }
2430
2431 filter_handle_drag(wp, c, rettv);
2432}
2433
2434/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002435 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002436 */
2437 void
2438f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2439{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002440 int id;
2441 win_T *wp;
2442 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002443 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002444 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002445
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002446 if (in_vim9script()
2447 && (check_for_number_arg(argvars, 0) == FAIL
2448 || check_for_string_arg(argvars, 1) == FAIL))
2449 return;
2450
2451 id = tv_get_number(&argvars[0]);
2452 wp = win_id2wp(id);
2453 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002454 // If the popup has been closed don't consume the key.
2455 if (wp == NULL)
2456 return;
2457
Bram Moolenaara730e552019-06-16 19:05:31 +02002458 c = *key;
2459 if (c == K_SPECIAL && key[1] != NUL)
2460 c = TO_SPECIAL(key[1], key[2]);
2461
Bram Moolenaara42d9452019-06-15 21:46:30 +02002462 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002463 rettv->v_type = VAR_BOOL;
2464 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002465
Bram Moolenaara730e552019-06-16 19:05:31 +02002466 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002467 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002468 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002469 res.vval.v_number = 0;
2470 else
2471 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002472 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002473 return;
2474 }
2475
2476 // Invoke callback
2477 res.v_type = VAR_NUMBER;
2478 popup_close_and_callback(wp, &res);
2479}
2480
2481/*
2482 * popup_dialog({text}, {options})
2483 */
2484 void
2485f_popup_dialog(typval_T *argvars, typval_T *rettv)
2486{
2487 popup_create(argvars, rettv, TYPE_DIALOG);
2488}
2489
2490/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002491 * popup_menu({text}, {options})
2492 */
2493 void
2494f_popup_menu(typval_T *argvars, typval_T *rettv)
2495{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002496 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002497}
2498
2499/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002500 * popup_notification({text}, {options})
2501 */
2502 void
2503f_popup_notification(typval_T *argvars, typval_T *rettv)
2504{
2505 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2506}
2507
2508/*
2509 * Find the popup window with window-ID "id".
2510 * If the popup window does not exist NULL is returned.
2511 * If the window is not a popup window, and error message is given.
2512 */
2513 static win_T *
2514find_popup_win(int id)
2515{
2516 win_T *wp = win_id2wp(id);
2517
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002518 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002519 {
2520 semsg(_("E993: window %d is not a popup window"), id);
2521 return NULL;
2522 }
2523 return wp;
2524}
2525
2526/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002527 * popup_close({id})
2528 */
2529 void
2530f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2531{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002532 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002533 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002534
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002535 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2536 return;
2537
2538 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002539 if (
2540# ifdef FEAT_TERMINAL
2541 // if the popup contains a terminal it will become hidden
2542 curbuf->b_term == NULL &&
2543# endif
2544 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002545 return;
2546
2547 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002548 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002549 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002550}
2551
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002552 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002553popup_hide(win_T *wp)
2554{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002555#ifdef FEAT_TERMINAL
2556 if (error_if_term_popup_window())
2557 return;
2558#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002559 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2560 {
2561 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002562 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002563 redraw_all_later(NOT_VALID);
2564 popup_mask_refresh = TRUE;
2565 }
2566}
2567
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002568/*
2569 * popup_hide({id})
2570 */
2571 void
2572f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2573{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002574 int id;
2575 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002576
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002577 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2578 return;
2579
2580 id = (int)tv_get_number(argvars);
2581 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002582 if (wp != NULL)
2583 popup_hide(wp);
2584}
2585
2586 void
2587popup_show(win_T *wp)
2588{
2589 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002590 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002591 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002592 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002593 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002594 }
2595}
2596
2597/*
2598 * popup_show({id})
2599 */
2600 void
2601f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2602{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002603 int id;
2604 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002605
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002606 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2607 return;
2608
2609 id = (int)tv_get_number(argvars);
2610 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002611 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002612 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002613 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002614#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002615 if (wp->w_popup_flags & POPF_INFO)
2616 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002617#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002618 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002619}
2620
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002621/*
2622 * popup_settext({id}, {text})
2623 */
2624 void
2625f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2626{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002627 int id;
2628 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002629
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002630 if (in_vim9script()
2631 && (check_for_number_arg(argvars, 0) == FAIL
2632 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2633 return;
2634
2635 id = (int)tv_get_number(&argvars[0]);
2636 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002637 if (wp != NULL)
2638 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002639 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2640 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2641 else
2642 {
2643 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002644 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002645 popup_adjust_position(wp);
2646 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002647 }
2648}
2649
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002650 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002651popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002652{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002653 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002654 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002655 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002656 clear_cmdline = TRUE;
2657 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002658
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002659 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002660 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002661}
2662
Bram Moolenaar82106932020-03-13 14:34:38 +01002663 static void
2664error_for_popup_window(void)
2665{
2666 emsg(_("E994: Not allowed in a popup window"));
2667}
2668
2669 int
2670error_if_popup_window(int also_with_term UNUSED)
2671{
2672 // win_execute() may set "curwin" to a popup window temporarily, but many
2673 // commands are disallowed then. When a terminal runs in the popup most
2674 // things are allowed. When a terminal is finished it can be closed.
2675 if (WIN_IS_POPUP(curwin)
2676# ifdef FEAT_TERMINAL
2677 && (also_with_term || curbuf->b_term == NULL)
2678 && !term_is_finished(curbuf)
2679# endif
2680 )
2681 {
2682 error_for_popup_window();
2683 return TRUE;
2684 }
2685 return FALSE;
2686}
2687
Bram Moolenaarec583842019-05-26 14:11:23 +02002688/*
2689 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002690 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002691 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002692 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002693 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002694popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002695{
2696 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002697 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002698 win_T *prev = NULL;
2699
Bram Moolenaarec583842019-05-26 14:11:23 +02002700 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002701 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002702 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002703 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002704 if (wp == curwin)
2705 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002706 if (!force)
2707 {
2708 error_for_popup_window();
2709 return FAIL;
2710 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002711 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002712 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002713 if (prev == NULL)
2714 first_popupwin = wp->w_next;
2715 else
2716 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002717 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002718 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002719 }
2720
Bram Moolenaarec583842019-05-26 14:11:23 +02002721 // go through tab-local popups
2722 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002723 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002724 return OK;
2725 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002726}
2727
2728/*
2729 * Close a popup window with Window-id "id" in tabpage "tp".
2730 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002731 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002732popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002733{
2734 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002735 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002736 win_T *prev = NULL;
2737
Bram Moolenaarec583842019-05-26 14:11:23 +02002738 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2739 if (wp->w_id == id)
2740 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002741 if (wp == curwin)
2742 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002743 if (!force)
2744 {
2745 error_for_popup_window();
2746 return FAIL;
2747 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002748 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002749 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002750 if (prev == NULL)
2751 *root = wp->w_next;
2752 else
2753 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002754 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002755 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002756 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002757 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002758}
2759
2760 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002761close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002762{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002763 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002764 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002765 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002766 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002767 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002768 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002769 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002770 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002771}
2772
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002773/*
2774 * popup_move({id}, {options})
2775 */
2776 void
2777f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2778{
Bram Moolenaarae943152019-06-16 22:54:14 +02002779 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002780 int id;
2781 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002782
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002783 if (in_vim9script()
2784 && (check_for_number_arg(argvars, 0) == FAIL
2785 || check_for_dict_arg(argvars, 1) == FAIL))
2786 return;
2787
2788 id = (int)tv_get_number(argvars);
2789 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002790 if (wp == NULL)
2791 return; // invalid {id}
2792
2793 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2794 {
2795 emsg(_(e_dictreq));
2796 return;
2797 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002798 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002799
Bram Moolenaarae943152019-06-16 22:54:14 +02002800 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002801
2802 if (wp->w_winrow + wp->w_height >= cmdline_row)
2803 clear_cmdline = TRUE;
2804 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002805}
2806
Bram Moolenaarbc133542019-05-29 20:26:46 +02002807/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002808 * popup_setoptions({id}, {options})
2809 */
2810 void
2811f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2812{
2813 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002814 int id;
2815 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002816 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002817
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002818 if (in_vim9script()
2819 && (check_for_number_arg(argvars, 0) == FAIL
2820 || check_for_dict_arg(argvars, 1) == FAIL))
2821 return;
2822
2823 id = (int)tv_get_number(argvars);
2824 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002825 if (wp == NULL)
2826 return; // invalid {id}
2827
2828 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2829 {
2830 emsg(_(e_dictreq));
2831 return;
2832 }
2833 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002834 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002835
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002836 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002837
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002838 if (old_firstline != wp->w_firstline)
2839 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002840 popup_adjust_position(wp);
2841}
2842
2843/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002844 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002845 */
2846 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002847f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002848{
2849 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002850 int id;
2851 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002852 int top_extra;
2853 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002854
2855 if (rettv_dict_alloc(rettv) == OK)
2856 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002857 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2858 return;
2859
2860 id = (int)tv_get_number(argvars);
2861 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002862 if (wp == NULL)
2863 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002864 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002865 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2866
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002867 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002868 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002869 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002870
Bram Moolenaarbc133542019-05-29 20:26:46 +02002871 dict_add_number(dict, "line", wp->w_winrow + 1);
2872 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002873 dict_add_number(dict, "width", wp->w_width + left_extra
2874 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2875 dict_add_number(dict, "height", wp->w_height + top_extra
2876 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002877
2878 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2879 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2880 dict_add_number(dict, "core_width", wp->w_width);
2881 dict_add_number(dict, "core_height", wp->w_height);
2882
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002883 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002884 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002885 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002886 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002887 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002888
2889 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002890 }
2891}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002892
2893/*
2894 * popup_list()
2895 */
2896 void
2897f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2898{
2899 win_T *wp;
2900 tabpage_T *tp;
2901
2902 if (rettv_list_alloc(rettv) != OK)
2903 return;
2904 FOR_ALL_POPUPWINS(wp)
2905 list_append_number(rettv->vval.v_list, wp->w_id);
2906 FOR_ALL_TABPAGES(tp)
2907 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2908 list_append_number(rettv->vval.v_list, wp->w_id);
2909}
2910
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002911/*
2912 * popup_locate({row}, {col})
2913 */
2914 void
2915f_popup_locate(typval_T *argvars, typval_T *rettv)
2916{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002917 int row;
2918 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002919 win_T *wp;
2920
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002921 if (in_vim9script()
2922 && (check_for_number_arg(argvars, 0) == FAIL
2923 || check_for_number_arg(argvars, 1) == FAIL))
2924 return;
2925
2926 row = tv_get_number(&argvars[0]) - 1;
2927 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002928 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002929 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002930 rettv->vval.v_number = wp->w_id;
2931}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002932
2933/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002934 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2935 */
2936 static void
2937get_padding_border(dict_T *dict, int *array, char *name)
2938{
2939 list_T *list;
2940 int i;
2941
2942 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2943 return;
2944
2945 list = list_alloc();
2946 if (list != NULL)
2947 {
2948 dict_add_list(dict, name, list);
2949 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2950 for (i = 0; i < 4; ++i)
2951 list_append_number(list, array[i]);
2952 }
2953}
2954
2955/*
2956 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2957 */
2958 static void
2959get_borderhighlight(dict_T *dict, win_T *wp)
2960{
2961 list_T *list;
2962 int i;
2963
2964 for (i = 0; i < 4; ++i)
2965 if (wp->w_border_highlight[i] != NULL)
2966 break;
2967 if (i == 4)
2968 return;
2969
2970 list = list_alloc();
2971 if (list != NULL)
2972 {
2973 dict_add_list(dict, "borderhighlight", list);
2974 for (i = 0; i < 4; ++i)
2975 list_append_string(list, wp->w_border_highlight[i], -1);
2976 }
2977}
2978
2979/*
2980 * For popup_getoptions(): add a "borderchars" entry to "dict".
2981 */
2982 static void
2983get_borderchars(dict_T *dict, win_T *wp)
2984{
2985 list_T *list;
2986 int i;
2987 char_u buf[NUMBUFLEN];
2988 int len;
2989
2990 for (i = 0; i < 8; ++i)
2991 if (wp->w_border_char[i] != 0)
2992 break;
2993 if (i == 8)
2994 return;
2995
2996 list = list_alloc();
2997 if (list != NULL)
2998 {
2999 dict_add_list(dict, "borderchars", list);
3000 for (i = 0; i < 8; ++i)
3001 {
3002 len = mb_char2bytes(wp->w_border_char[i], buf);
3003 list_append_string(list, buf, len);
3004 }
3005 }
3006}
3007
3008/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003009 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003010 */
3011 static void
3012get_moved_list(dict_T *dict, win_T *wp)
3013{
3014 list_T *list;
3015
3016 list = list_alloc();
3017 if (list != NULL)
3018 {
3019 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003020 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003021 list_append_number(list, wp->w_popup_mincol);
3022 list_append_number(list, wp->w_popup_maxcol);
3023 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003024 list = list_alloc();
3025 if (list != NULL)
3026 {
3027 dict_add_list(dict, "mousemoved", list);
3028 list_append_number(list, wp->w_popup_mouse_row);
3029 list_append_number(list, wp->w_popup_mouse_mincol);
3030 list_append_number(list, wp->w_popup_mouse_maxcol);
3031 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003032}
3033
3034/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003035 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003036 */
3037 void
3038f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3039{
3040 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003041 int id;
3042 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003043 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003044 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003045
3046 if (rettv_dict_alloc(rettv) == OK)
3047 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003048 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3049 return;
3050
3051 id = (int)tv_get_number(argvars);
3052 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003053 if (wp == NULL)
3054 return;
3055
3056 dict = rettv->vval.v_dict;
3057 dict_add_number(dict, "line", wp->w_wantline);
3058 dict_add_number(dict, "col", wp->w_wantcol);
3059 dict_add_number(dict, "minwidth", wp->w_minwidth);
3060 dict_add_number(dict, "minheight", wp->w_minheight);
3061 dict_add_number(dict, "maxheight", wp->w_maxheight);
3062 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003063 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003064 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003065 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003066 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003067 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003068 {
3069 proptype_T *pt = text_prop_type_by_id(
3070 wp->w_popup_prop_win->w_buffer,
3071 wp->w_popup_prop_type);
3072
3073 if (pt != NULL)
3074 dict_add_string(dict, "textprop", pt->pt_name);
3075 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3076 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3077 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003078 dict_add_string(dict, "title", wp->w_popup_title);
3079 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003080 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003081 dict_add_number(dict, "mapping",
3082 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003083 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003084 dict_add_number(dict, "posinvert",
3085 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003086 dict_add_number(dict, "cursorline",
3087 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003088 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003089 if (wp->w_scrollbar_highlight != NULL)
3090 dict_add_string(dict, "scrollbarhighlight",
3091 wp->w_scrollbar_highlight);
3092 if (wp->w_thumb_highlight != NULL)
3093 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003094
Bram Moolenaara3fce622019-06-20 02:31:49 +02003095 // find the tabpage that holds this popup
3096 i = 1;
3097 FOR_ALL_TABPAGES(tp)
3098 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003099 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003100
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003101 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3102 if (twp->w_id == id)
3103 break;
3104 if (twp != NULL)
3105 break;
3106 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003107 }
3108 if (tp == NULL)
3109 i = -1; // must be global
3110 else if (tp == curtab)
3111 i = 0;
3112 dict_add_number(dict, "tabpage", i);
3113
Bram Moolenaarae943152019-06-16 22:54:14 +02003114 get_padding_border(dict, wp->w_popup_padding, "padding");
3115 get_padding_border(dict, wp->w_popup_border, "border");
3116 get_borderhighlight(dict, wp);
3117 get_borderchars(dict, wp);
3118 get_moved_list(dict, wp);
3119
3120 if (wp->w_filter_cb.cb_name != NULL)
3121 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3122 if (wp->w_close_cb.cb_name != NULL)
3123 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003124
K.Takataeeec2542021-06-02 13:28:16 +02003125 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003126 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3127 {
3128 dict_add_string(dict, "pos",
3129 (char_u *)poppos_entries[i].pp_name);
3130 break;
3131 }
3132
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003133 dict_add_string(dict, "close", (char_u *)(
3134 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3135 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3136
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003137# if defined(FEAT_TIMERS)
3138 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3139 ? (long)wp->w_popup_timer->tr_interval : 0L);
3140# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003141 }
3142}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003143
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003144# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003145/*
3146 * Return TRUE if the current window is running a terminal in a popup window.
3147 * Return FALSE when the job has ended.
3148 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003149 int
3150error_if_term_popup_window()
3151{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003152 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3153 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003154 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01003155 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003156 return TRUE;
3157 }
3158 return FALSE;
3159}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003160# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003161
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003162/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003163 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003164 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003165 * Each calling function should use a different flag, see the list at
3166 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003167 */
3168 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003169popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003170{
3171 win_T *wp;
3172
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003173 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003174 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003175 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003176 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003177}
3178
3179/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003180 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003181 * Must have called popup_reset_handled() first.
3182 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3183 * popup with the highest zindex.
3184 */
3185 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003186find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003187{
3188 win_T *wp;
3189 win_T *found_wp;
3190 int found_zindex;
3191
3192 found_zindex = lowest ? INT_MAX : 0;
3193 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003194 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003195 if ((wp->w_popup_handled & handled_flag) == 0
3196 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003197 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003198 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003199 {
3200 found_zindex = wp->w_zindex;
3201 found_wp = wp;
3202 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003203 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003204 if ((wp->w_popup_handled & handled_flag) == 0
3205 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003206 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003207 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003208 {
3209 found_zindex = wp->w_zindex;
3210 found_wp = wp;
3211 }
3212
3213 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003214 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003215 return found_wp;
3216}
3217
3218/*
3219 * Invoke the filter callback for window "wp" with typed character "c".
3220 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003221 * Returns the return value of the filter or -1 for CTRL-C in the current
3222 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003223 * Careful: The filter may make "wp" invalid!
3224 */
3225 static int
3226invoke_popup_filter(win_T *wp, int c)
3227{
3228 int res;
3229 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003230 typval_T argv[3];
3231 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003232 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003233 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003234
Bram Moolenaara42d9452019-06-15 21:46:30 +02003235 // Emergency exit: CTRL-C closes the popup.
3236 if (c == Ctrl_C)
3237 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003238 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003239 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003240
3241 // Reset got_int to avoid the callback isn't called.
3242 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003243 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003244 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003245
3246 // If the popup is the current window it probably fails to close. Then
3247 // do not consume the key.
3248 if (was_curwin && wp == curwin)
3249 return -1;
3250 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003251 }
3252
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003253 argv[0].v_type = VAR_NUMBER;
3254 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3255
3256 // Convert the number to a string, so that the function can use:
3257 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003258 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003259 argv[1].v_type = VAR_STRING;
3260 argv[1].vval.v_string = vim_strsave(buf);
3261
3262 argv[2].v_type = VAR_UNKNOWN;
3263
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003264 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003265 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3266 {
3267 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003268 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003269 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003270 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003271 }
3272 else
3273 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003274 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3275 popup_highlight_curline(wp);
3276
Bram Moolenaar371806e2020-10-22 13:44:54 +02003277 // If an error message was given always return FALSE, so that keys are
3278 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003279 // If we get three errors in a row then close the popup. Decrement the
3280 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3281 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003282 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003283 {
3284 wp->w_filter_errors += 10;
3285 if (wp->w_filter_errors >= 30)
3286 popup_close_with_retval(wp, -1);
3287 res = FALSE;
3288 }
3289 else
3290 {
3291 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3292 --wp->w_filter_errors;
3293 res = tv_get_bool(&rettv);
3294 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003295 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003296
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003297 vim_free(argv[1].vval.v_string);
3298 clear_tv(&rettv);
3299 return res;
3300}
3301
3302/*
3303 * Called when "c" was typed: invoke popup filter callbacks.
3304 * Returns TRUE when the character was consumed,
3305 */
3306 int
3307popup_do_filter(int c)
3308{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003309 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003310 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003311 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003312 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003313 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003314 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003315
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003316#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003317 // Popup window with terminal always gets focus.
3318 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3319 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003320#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003321
Bram Moolenaar934470e2019-09-01 23:27:05 +02003322 if (recursive)
3323 return FALSE;
3324 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003325
Bram Moolenaarf6396232019-08-24 19:36:00 +02003326 if (c == K_LEFTMOUSE)
3327 {
3328 int row = mouse_row;
3329 int col = mouse_col;
3330
3331 wp = mouse_find_win(&row, &col, FIND_POPUP);
3332 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003333 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003334 }
3335
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003336 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003337 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003338 while (res == FALSE
3339 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003340 if (wp->w_filter_cb.cb_name != NULL
3341 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003342 res = invoke_popup_filter(wp, c);
3343
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003344 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003345 {
3346 int save_got_int = got_int;
3347
3348 // Reset got_int to avoid a function used in the statusline aborts.
3349 got_int = FALSE;
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003350 redraw_after_callback(FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003351 got_int |= save_got_int;
3352 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003353 recursive = FALSE;
3354 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003355
3356 // When interrupted return FALSE to avoid looping.
3357 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003358}
3359
Bram Moolenaar3397f742019-06-02 18:40:06 +02003360/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003361 * Return TRUE if there is a popup visible with a filter callback and the
3362 * "mapping" property off.
3363 */
3364 int
3365popup_no_mapping(void)
3366{
3367 int round;
3368 win_T *wp;
3369
3370 for (round = 1; round <= 2; ++round)
3371 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3372 wp != NULL; wp = wp->w_next)
3373 if (wp->w_filter_cb.cb_name != NULL
3374 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3375 return TRUE;
3376 return FALSE;
3377}
3378
3379/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003380 * Called when the cursor moved: check if any popup needs to be closed if the
3381 * cursor moved far enough.
3382 */
3383 void
3384popup_check_cursor_pos()
3385{
3386 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003387
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003388 popup_reset_handled(POPUP_HANDLED_3);
3389 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003390 if (wp->w_popup_curwin != NULL
3391 && (curwin != wp->w_popup_curwin
3392 || curwin->w_cursor.lnum != wp->w_popup_lnum
3393 || curwin->w_cursor.col < wp->w_popup_mincol
3394 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003395 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003396}
3397
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003398/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003399 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003400 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003401 static void
3402popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003403{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003404 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003405 char_u *cells;
3406 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003407
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003408 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3409 {
3410 vim_free(wp->w_popup_mask_cells);
3411 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003412 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003413 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003414 if (wp->w_popup_mask_cells != NULL
3415 && wp->w_popup_mask_height == height
3416 && wp->w_popup_mask_width == width)
3417 return; // cache is still valid
3418
3419 vim_free(wp->w_popup_mask_cells);
3420 wp->w_popup_mask_cells = alloc_clear(width * height);
3421 if (wp->w_popup_mask_cells == NULL)
3422 return;
3423 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003424
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003425 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003426 {
3427 int cols, cole;
3428 int lines, linee;
3429
3430 li = lio->li_tv.vval.v_list->lv_first;
3431 cols = tv_get_number(&li->li_tv);
3432 if (cols < 0)
3433 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003434 if (cols <= 0)
3435 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003436 li = li->li_next;
3437 cole = tv_get_number(&li->li_tv);
3438 if (cole < 0)
3439 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003440 if (cole > width)
3441 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003442 li = li->li_next;
3443 lines = tv_get_number(&li->li_tv);
3444 if (lines < 0)
3445 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003446 if (lines <= 0)
3447 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003448 li = li->li_next;
3449 linee = tv_get_number(&li->li_tv);
3450 if (linee < 0)
3451 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003452 if (linee > height)
3453 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003454
Bram Moolenaar4012d262020-12-29 11:57:46 +01003455 for (row = lines - 1; row < linee; ++row)
3456 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003457 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003458 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003459}
3460
3461/*
3462 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3463 * "col" and "line" are screen coordinates.
3464 */
3465 static int
3466popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3467{
3468 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3469 int line = screenline - wp->w_winrow;
3470
3471 return col >= 0 && col < width
3472 && line >= 0 && line < height
3473 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003474}
3475
3476/*
3477 * Set flags in popup_transparent[] for window "wp" to "val".
3478 */
3479 static void
3480update_popup_transparent(win_T *wp, int val)
3481{
3482 if (wp->w_popup_mask != NULL)
3483 {
3484 int width = popup_width(wp);
3485 int height = popup_height(wp);
3486 listitem_T *lio, *li;
3487 int cols, cole;
3488 int lines, linee;
3489 int col, line;
3490
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003491 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003492 {
3493 li = lio->li_tv.vval.v_list->lv_first;
3494 cols = tv_get_number(&li->li_tv);
3495 if (cols < 0)
3496 cols = width + cols + 1;
3497 li = li->li_next;
3498 cole = tv_get_number(&li->li_tv);
3499 if (cole < 0)
3500 cole = width + cole + 1;
3501 li = li->li_next;
3502 lines = tv_get_number(&li->li_tv);
3503 if (lines < 0)
3504 lines = height + lines + 1;
3505 li = li->li_next;
3506 linee = tv_get_number(&li->li_tv);
3507 if (linee < 0)
3508 linee = height + linee + 1;
3509
3510 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003511 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003512 if (cols < 0)
3513 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003514 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003515 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003516 if (lines < 0)
3517 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003518 for (line = lines; line < linee
3519 && line + wp->w_winrow < screen_Rows; ++line)
3520 for (col = cols; col < cole
3521 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003522 popup_transparent[(line + wp->w_winrow) * screen_Columns
3523 + col + wp->w_wincol] = val;
3524 }
3525 }
3526}
3527
3528/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003529 * Only called when popup window "wp" is hidden: If the window is positioned
3530 * next to a text property, and it is now visible, then unhide the popup.
3531 * We don't check if visible popups become hidden, that is done in
3532 * popup_adjust_position().
3533 * Return TRUE if the popup became unhidden.
3534 */
3535 static int
3536check_popup_unhidden(win_T *wp)
3537{
3538 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3539 {
3540 textprop_T prop;
3541 linenr_T lnum;
3542
3543 if (find_visible_prop(wp->w_popup_prop_win,
3544 wp->w_popup_prop_type, wp->w_popup_prop_id,
3545 &prop, &lnum) == OK)
3546 {
3547 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003548 wp->w_popup_prop_topline = 0; // force repositioning
3549 return TRUE;
3550 }
3551 }
3552 return FALSE;
3553}
3554
3555/*
3556 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3557 * That is when the buffer in the popup was changed, or the popup is following
3558 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003559 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003560 */
3561 static int
3562popup_need_position_adjust(win_T *wp)
3563{
3564 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3565 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003566 if (win_valid(wp->w_popup_prop_win)
3567 && (wp->w_popup_prop_changedtick
3568 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3569 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3570 return TRUE;
3571
3572 // May need to adjust the width if the cursor moved.
3573 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003574}
3575
3576/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003577 * Update "popup_mask" if needed.
3578 * Also recomputes the popup size and positions.
3579 * Also updates "popup_visible".
3580 * Also marks window lines for redrawing.
3581 */
3582 void
3583may_update_popup_mask(int type)
3584{
3585 win_T *wp;
3586 short *mask;
3587 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003588 int redraw_all_popups = FALSE;
3589 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003590
3591 // Need to recompute when switching tabs.
3592 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3593 // (such as the screen size) must have changed.
3594 if (popup_mask_tab != curtab || type >= NOT_VALID)
3595 {
3596 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003597 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003598 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003599
3600 // Check if any popup window buffer has changed and if any popup connected
3601 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003602 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003603 if (wp->w_popup_flags & POPF_HIDDEN)
3604 popup_mask_refresh |= check_popup_unhidden(wp);
3605 else if (popup_need_position_adjust(wp))
3606 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003607 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003608 if (wp->w_popup_flags & POPF_HIDDEN)
3609 popup_mask_refresh |= check_popup_unhidden(wp);
3610 else if (popup_need_position_adjust(wp))
3611 popup_mask_refresh = TRUE;
3612
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003613 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003614 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003615
3616 // Need to update the mask, something has changed.
3617 popup_mask_refresh = FALSE;
3618 popup_mask_tab = curtab;
3619 popup_visible = FALSE;
3620
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003621 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003622 // If redrawing only what is needed, update "popup_mask_next" and then
3623 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003624 redrawing_all_win = TRUE;
3625 FOR_ALL_WINDOWS(wp)
3626 if (wp->w_redr_type < SOME_VALID)
3627 redrawing_all_win = FALSE;
3628 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003629 mask = popup_mask;
3630 else
3631 mask = popup_mask_next;
3632 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3633
3634 // Find the window with the lowest zindex that hasn't been handled yet,
3635 // so that the window with a higher zindex overwrites the value in
3636 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003637 popup_reset_handled(POPUP_HANDLED_4);
3638 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003639 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003640 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003641 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003642
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003643 popup_visible = TRUE;
3644
Bram Moolenaar12034e22019-08-25 22:25:02 +02003645 // Recompute the position if the text changed. It may make the popup
3646 // hidden if it's attach to a text property that is no longer visible.
3647 if (redraw_all_popups || popup_need_position_adjust(wp))
3648 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003649 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003650 if (wp->w_popup_flags & POPF_HIDDEN)
3651 continue;
3652 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003653
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003654 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003655 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003656 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003657 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003658 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003659 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003660 col < wp->w_wincol + width - wp->w_popup_leftoff
3661 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003662 if (wp->w_zindex < POPUPMENU_ZINDEX
3663 && pum_visible()
3664 && pum_under_menu(line, col, FALSE))
3665 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3666 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003667 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003668 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003669 }
3670
3671 // Only check which lines are to be updated if not already
3672 // updating all lines.
3673 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003674 {
3675 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3676 win_T *prev_wp = NULL;
3677
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003678 for (line = 0; line < screen_Rows; ++line)
3679 {
3680 int col_done = 0;
3681
3682 for (col = 0; col < screen_Columns; ++col)
3683 {
3684 int off = line * screen_Columns + col;
3685
3686 if (popup_mask[off] != popup_mask_next[off])
3687 {
3688 popup_mask[off] = popup_mask_next[off];
3689
3690 if (line >= cmdline_row)
3691 {
3692 // the command line needs to be cleared if text below
3693 // the popup is now visible.
3694 if (!msg_scrolled && popup_mask_next[off] == 0)
3695 clear_cmdline = TRUE;
3696 }
3697 else if (col >= col_done)
3698 {
3699 linenr_T lnum;
3700 int line_cp = line;
3701 int col_cp = col;
3702
3703 // The screen position "line" / "col" needs to be
3704 // redrawn. Figure out what window that is and update
3705 // w_redraw_top and w_redr_bot. Only needs to be done
3706 // once for each window line.
3707 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3708 if (wp != NULL)
3709 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003710#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003711 // A terminal window needs to be redrawn.
3712 if (bt_terminal(wp->w_buffer))
3713 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003714 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003715#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003716 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003717 if (wp != prev_wp)
3718 {
3719 vim_memset(plines_cache, 0,
3720 sizeof(int) * Rows);
3721 prev_wp = wp;
3722 }
3723
3724 if (line_cp >= wp->w_height)
3725 // In (or below) status line
3726 wp->w_redr_status = TRUE;
3727 else
3728 {
3729 // compute the position in the buffer line
3730 // from the position in the window
3731 mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003732 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003733 redrawWinline(wp, lnum);
3734 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003735 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003736
3737 // This line is going to be redrawn, no need to
3738 // check until the right side of the window.
3739 col_done = wp->w_wincol + wp->w_width - 1;
3740 }
3741 }
3742 }
3743 }
3744 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003745
3746 vim_free(plines_cache);
3747 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003748}
3749
3750/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003751 * If the current window is a popup and something relevant changed, recompute
3752 * the position and size.
3753 */
3754 void
3755may_update_popup_position(void)
3756{
3757 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3758 popup_adjust_position(curwin);
3759}
3760
3761/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003762 * Return a string of "len" spaces in IObuff.
3763 */
3764 static char_u *
3765get_spaces(int len)
3766{
3767 vim_memset(IObuff, ' ', (size_t)len);
3768 IObuff[len] = NUL;
3769 return IObuff;
3770}
3771
3772/*
3773 * Update popup windows. They are drawn on top of normal windows.
3774 * "win_update" is called for each popup window, lowest zindex first.
3775 */
3776 void
3777update_popups(void (*win_update)(win_T *wp))
3778{
3779 win_T *wp;
3780 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003781 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003782 int total_width;
3783 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003784 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003785 int popup_attr;
3786 int border_attr[4];
3787 int border_char[8];
3788 char_u buf[MB_MAXBYTES];
3789 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003790 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003791 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003792 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003793 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003794 int sb_thumb_top = 0;
3795 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003796 int attr_scroll = 0;
3797 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003798
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003799 // hide the cursor until redrawing is done.
3800 cursor_off();
3801
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003802 // Find the window with the lowest zindex that hasn't been updated yet,
3803 // so that the window with a higher zindex is drawn later, thus goes on
3804 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003805 popup_reset_handled(POPUP_HANDLED_5);
3806 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003807 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003808 int title_len = 0;
3809 int title_wincol;
3810
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003811 // This drawing uses the zindex of the popup window, so that it's on
3812 // top of the text but doesn't draw when another popup with higher
3813 // zindex is on top of the character.
3814 screen_zindex = wp->w_zindex;
3815
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003816 // Set flags in popup_transparent[] for masked cells.
3817 update_popup_transparent(wp, 1);
3818
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003819 // adjust w_winrow and w_wincol for border and padding, since
3820 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003821 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003822 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3823 - wp->w_popup_leftoff;
3824 if (wp->w_wincol + left_extra < 0)
3825 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003826 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003827 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003828
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003829 // Draw the popup text, unless it's off screen.
3830 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003831 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003832 // May need to update the "cursorline" highlighting, which may also
3833 // change "topline"
3834 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3835 popup_highlight_curline(wp);
3836
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003837 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003838
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003839 // move the cursor into the visible lines, otherwise executing
3840 // commands with win_execute() may cause the text to jump.
3841 if (wp->w_cursor.lnum < wp->w_topline)
3842 wp->w_cursor.lnum = wp->w_topline;
3843 else if (wp->w_cursor.lnum >= wp->w_botline)
3844 wp->w_cursor.lnum = wp->w_botline - 1;
3845 }
3846
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003847 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003848 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003849
3850 // Add offset for border and padding if not done already.
3851 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3852 {
3853 wp->w_wcol += left_extra;
3854 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3855 }
3856 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003857 {
3858 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003859 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003860 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003861
Bram Moolenaard529ba52019-07-02 23:13:53 +02003862 total_width = popup_width(wp);
3863 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003864 popup_attr = get_wcr_attr(wp);
3865
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003866 if (wp->w_winrow + total_height > cmdline_row)
3867 wp->w_popup_flags |= POPF_ON_CMDLINE;
3868 else
3869 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3870
3871
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003872 // We can only use these line drawing characters when 'encoding' is
3873 // "utf-8" and 'ambiwidth' is "single".
3874 if (enc_utf8 && *p_ambw == 's')
3875 {
3876 border_char[0] = border_char[2] = 0x2550;
3877 border_char[1] = border_char[3] = 0x2551;
3878 border_char[4] = 0x2554;
3879 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003880 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3881 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882 border_char[7] = 0x255a;
3883 }
3884 else
3885 {
3886 border_char[0] = border_char[2] = '-';
3887 border_char[1] = border_char[3] = '|';
3888 for (i = 4; i < 8; ++i)
3889 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003890 if (wp->w_popup_flags & POPF_RESIZE)
3891 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003892 }
3893 for (i = 0; i < 8; ++i)
3894 if (wp->w_border_char[i] != 0)
3895 border_char[i] = wp->w_border_char[i];
3896
3897 for (i = 0; i < 4; ++i)
3898 {
3899 border_attr[i] = popup_attr;
3900 if (wp->w_border_highlight[i] != NULL)
3901 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3902 }
3903
Bram Moolenaard91467f2020-11-21 12:42:09 +01003904 // Title goes on top of border or padding.
3905 title_wincol = wp->w_wincol + 1;
3906 if (wp->w_popup_title != NULL)
3907 {
rbtnnc6119412021-08-07 13:08:45 +02003908 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003909
Ralf Schandlbc869872021-05-28 14:12:14 +02003910 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003911 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02003912 {
3913 int title_byte_len = (int)STRLEN(wp->w_popup_title);
3914 char_u *title_text = alloc(title_byte_len + 1);
3915
3916 if (title_text != NULL)
3917 {
3918 trunc_string(wp->w_popup_title, title_text,
3919 total_width - 2, title_byte_len + 1);
3920 screen_puts(title_text, wp->w_winrow, title_wincol,
3921 wp->w_popup_border[0] > 0
3922 ? border_attr[0] : popup_attr);
3923 vim_free(title_text);
3924 }
3925
Bram Moolenaard91467f2020-11-21 12:42:09 +01003926 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02003927 }
3928 else
3929 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
3930 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003931 }
3932
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003933 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003934 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003935 if (wp->w_popup_border[0] > 0)
3936 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003937 // top border; do not draw over the title
3938 if (title_len > 0)
3939 {
3940 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3941 wincol < 0 ? 0 : wincol, title_wincol,
3942 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003943 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01003944 border_char[0], border_attr[0]);
3945 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3946 title_wincol + title_len, wincol + total_width,
3947 border_char[0], border_char[0], border_attr[0]);
3948 }
3949 else
3950 {
3951 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3952 wincol < 0 ? 0 : wincol, wincol + total_width,
3953 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
3954 ? border_char[4] : border_char[0],
3955 border_char[0], border_attr[0]);
3956 }
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003957 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003958 {
3959 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3960 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003961 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003962 }
3963 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003964 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3965 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003966
Bram Moolenaard529ba52019-07-02 23:13:53 +02003967 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3968 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003969 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01003970 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003971 - wp->w_has_scrollbar;
3972 if (padcol < 0)
3973 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003974 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003975 padcol = 0;
3976 }
3977 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003978 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003979 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003980 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003981 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01003982 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003983 // top padding and no border; do not draw over the title
3984 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003985 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003986 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01003987 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003988 row += 1;
3989 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003990 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003991 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01003992 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003993 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003994
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003995 // Compute scrollbar thumb position and size.
3996 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003997 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003998 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3999 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004000
Bram Moolenaar076d9882019-09-14 22:23:29 +02004001 sb_thumb_height = (height * height + linecount / 2) / linecount;
4002 if (wp->w_topline > 1 && sb_thumb_height == height)
4003 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004004 if (sb_thumb_height == 0)
4005 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02004006 if (linecount <= wp->w_height)
4007 // it just fits, avoid divide by zero
4008 sb_thumb_top = 0;
4009 else
4010 sb_thumb_top = (wp->w_topline - 1
4011 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004012 * (wp->w_height - sb_thumb_height)
4013 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004014 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4015 sb_thumb_top = 1; // show it's scrolled
4016
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004017 if (wp->w_scrollbar_highlight != NULL)
4018 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4019 else
4020 attr_scroll = highlight_attr[HLF_PSB];
4021 if (wp->w_thumb_highlight != NULL)
4022 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4023 else
4024 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004025 }
4026
4027 for (i = wp->w_popup_border[0];
4028 i < total_height - wp->w_popup_border[2]; ++i)
4029 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004030 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004031 // left and right padding only needed next to the body
4032 int do_padding =
4033 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4034 && i < total_height - wp->w_popup_border[2]
4035 - wp->w_popup_padding[2];
4036
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004037 row = wp->w_winrow + i;
4038
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004039 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004040 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004041 {
4042 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004043 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004044 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004045 if (do_padding && wp->w_popup_padding[3] > 0)
4046 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004047 int col = wincol + wp->w_popup_border[3];
4048
Bram Moolenaard529ba52019-07-02 23:13:53 +02004049 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004050 pad_left = wp->w_popup_padding[3];
4051 if (col < 0)
4052 {
4053 pad_left += col;
4054 col = 0;
4055 }
4056 if (pad_left > 0)
4057 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4058 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004059 // scrollbar
4060 if (wp->w_has_scrollbar)
4061 {
4062 int line = i - top_off;
4063 int scroll_col = wp->w_wincol + total_width - 1
4064 - wp->w_popup_border[1];
4065
4066 if (line >= 0 && line < wp->w_height)
4067 screen_putchar(' ', row, scroll_col,
4068 line >= sb_thumb_top
4069 && line < sb_thumb_top + sb_thumb_height
4070 ? attr_thumb : attr_scroll);
4071 else
4072 screen_putchar(' ', row, scroll_col, popup_attr);
4073 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004074 // right border
4075 if (wp->w_popup_border[1] > 0)
4076 {
4077 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004078 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004079 }
4080 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004081 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004082 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004083 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004084 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4085 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004086 }
4087
4088 if (wp->w_popup_padding[2] > 0)
4089 {
4090 // bottom padding
4091 row = wp->w_winrow + wp->w_popup_border[0]
4092 + wp->w_popup_padding[0] + wp->w_height;
4093 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004094 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004095 }
4096
4097 if (wp->w_popup_border[2] > 0)
4098 {
4099 // bottom border
4100 row = wp->w_winrow + total_height - 1;
4101 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004102 wincol < 0 ? 0 : wincol,
4103 wincol + total_width,
4104 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004105 ? border_char[7] : border_char[2],
4106 border_char[2], border_attr[2]);
4107 if (wp->w_popup_border[1] > 0)
4108 {
4109 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004110 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004111 }
4112 }
4113
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004114 if (wp->w_popup_close == POPCLOSE_BUTTON)
4115 {
4116 // close button goes on top of anything at the top-right corner
4117 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004118 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004119 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4120 }
4121
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004122 update_popup_transparent(wp, 0);
4123
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004124 // Back to the normal zindex.
4125 screen_zindex = 0;
4126 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004127
4128#if defined(FEAT_SEARCH_EXTRA)
4129 // In case win_update() called start_search_hl().
4130 end_search_hl();
4131#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004132}
4133
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004134/*
4135 * Mark references in callbacks of one popup window.
4136 */
4137 static int
4138set_ref_in_one_popup(win_T *wp, int copyID)
4139{
4140 int abort = FALSE;
4141 typval_T tv;
4142
4143 if (wp->w_close_cb.cb_partial != NULL)
4144 {
4145 tv.v_type = VAR_PARTIAL;
4146 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4147 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4148 }
4149 if (wp->w_filter_cb.cb_partial != NULL)
4150 {
4151 tv.v_type = VAR_PARTIAL;
4152 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4153 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4154 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004155 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004156 return abort;
4157}
4158
4159/*
4160 * Set reference in callbacks of popup windows.
4161 */
4162 int
4163set_ref_in_popups(int copyID)
4164{
4165 int abort = FALSE;
4166 win_T *wp;
4167 tabpage_T *tp;
4168
4169 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4170 abort = abort || set_ref_in_one_popup(wp, copyID);
4171
4172 FOR_ALL_TABPAGES(tp)
4173 {
4174 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4175 abort = abort || set_ref_in_one_popup(wp, copyID);
4176 if (abort)
4177 break;
4178 }
4179 return abort;
4180}
Bram Moolenaar79648732019-07-18 21:43:07 +02004181
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004182 int
4183popup_is_popup(win_T *wp)
4184{
4185 return wp->w_popup_flags != 0;
4186}
4187
4188#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004189/*
4190 * Find an existing popup used as the preview window, in the current tab page.
4191 * Return NULL if not found.
4192 */
4193 win_T *
4194popup_find_preview_window(void)
4195{
4196 win_T *wp;
4197
4198 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004199 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004200 if (wp->w_p_pvw)
4201 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004202 return NULL;
4203}
4204
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004205/*
4206 * Find an existing popup used as the info window, in the current tab page.
4207 * Return NULL if not found.
4208 */
4209 win_T *
4210popup_find_info_window(void)
4211{
4212 win_T *wp;
4213
4214 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004215 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004216 if (wp->w_popup_flags & POPF_INFO)
4217 return wp;
4218 return NULL;
4219}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004220#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004221
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004222 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004223f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4224{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004225#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004226 win_T *wp = popup_find_info_window();
4227
4228 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004229#else
4230 rettv->vval.v_number = 0;
4231#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004232}
4233
4234 void
4235f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004236{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004237#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004238 win_T *wp = popup_find_preview_window();
4239
4240 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004241#else
4242 rettv->vval.v_number = 0;
4243#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004244}
4245
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004246#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004247/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004248 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004249 * NOTE: this makes the popup the current window, so that the file can be
4250 * edited. However, it must not remain to be the current window, the caller
4251 * must make sure of that.
4252 */
4253 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004254popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004255{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004256 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004257
4258 if (wp == NULL)
4259 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004260 if (info)
4261 wp->w_popup_flags |= POPF_INFO;
4262 else
4263 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004264
4265 // Set the width to a reasonable value, so that w_topline can be computed.
4266 if (wp->w_minwidth > 0)
4267 wp->w_width = wp->w_minwidth;
4268 else if (wp->w_maxwidth > 0)
4269 wp->w_width = wp->w_maxwidth;
4270 else
4271 wp->w_width = curwin->w_width;
4272
4273 // Will switch to another buffer soon, dummy one can be wiped.
4274 wp->w_buffer->b_locked = FALSE;
4275
4276 win_enter(wp, FALSE);
4277 return OK;
4278}
4279
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004280/*
4281 * Close any preview popup.
4282 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004283 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004284popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004285{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004286 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004287
4288 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004289 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004290}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004291
4292/*
4293 * Hide the info popup.
4294 */
4295 void
4296popup_hide_info(void)
4297{
4298 win_T *wp = popup_find_info_window();
4299
4300 if (wp != NULL)
4301 popup_hide(wp);
4302}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004303
4304/*
4305 * Close any info popup.
4306 */
4307 void
4308popup_close_info(void)
4309{
4310 win_T *wp = popup_find_info_window();
4311
4312 if (wp != NULL)
4313 popup_close_with_retval(wp, -1);
4314}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004315#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004316
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004317/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004318 * Close any popup for a text property associated with "win".
4319 * Return TRUE if a popup was closed.
4320 */
4321 int
4322popup_win_closed(win_T *win)
4323{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004324 int round;
4325 win_T *wp;
4326 win_T *next;
4327 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004328
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004329 for (round = 1; round <= 2; ++round)
4330 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4331 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004332 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004333 next = wp->w_next;
4334 if (wp->w_popup_prop_win == win)
4335 {
4336 popup_close_with_retval(wp, -1);
4337 ret = TRUE;
4338 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004339 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004340 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004341}
4342
4343/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004344 * Set the title of the popup window to the file name.
4345 */
4346 void
4347popup_set_title(win_T *wp)
4348{
4349 if (wp->w_buffer->b_fname != NULL)
4350 {
4351 char_u dirname[MAXPATHL];
4352 size_t len;
4353
4354 mch_dirname(dirname, MAXPATHL);
4355 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4356
4357 vim_free(wp->w_popup_title);
4358 len = STRLEN(wp->w_buffer->b_fname) + 3;
4359 wp->w_popup_title = alloc((int)len);
4360 if (wp->w_popup_title != NULL)
4361 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4362 wp->w_buffer->b_fname);
4363 redraw_win_later(wp, VALID);
4364 }
4365}
4366
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004367# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004368/*
4369 * If there is a preview window, update the title.
4370 * Used after changing directory.
4371 */
4372 void
4373popup_update_preview_title(void)
4374{
4375 win_T *wp = popup_find_preview_window();
4376
4377 if (wp != NULL)
4378 popup_set_title(wp);
4379}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004380# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004381
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004382#endif // FEAT_PROP_POPUP