blob: cdd7677e77fb1632af460dced13de6216c8a48d6 [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 {
64 semsg(_(e_invexpr2), val);
65 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),
386 "{_ -> popup_close(%d)}", wp->w_id);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200387 if (get_lambda_tv(&ptr, &tv, &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
405 for (nr = 0; nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
406 ++nr)
407 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
408 return poppos_entries[nr].pp_val;
409
410 if (give_error)
411 semsg(_(e_invarg2), str);
412 return POPPOS_NONE;
413}
414
Bram Moolenaar17627312019-06-02 19:53:44 +0200415/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200416 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200417 */
418 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200419apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200420{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200421 int nr;
422 char_u *str;
423 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200424
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200425 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200426 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200427 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200428 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200429 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200430 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200431 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200432 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200433
434 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200435 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200436 wp->w_wantline = nr;
437 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200438 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200439 wp->w_wantcol = nr;
440
Bram Moolenaar55881332020-08-18 13:04:15 +0200441
442 nr = dict_get_bool(d, (char_u *)"fixed", -1);
443 if (nr != -1)
444 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200445
Bram Moolenaar12034e22019-08-25 22:25:02 +0200446 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100447 poppos_T ppt = get_pos_entry(d, TRUE);
448
449 if (ppt != POPPOS_NONE)
450 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200451 }
452
453 str = dict_get_string(d, (char_u *)"textprop", FALSE);
454 if (str != NULL)
455 {
456 wp->w_popup_prop_type = 0;
457 if (*str != NUL)
458 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100459 wp->w_popup_prop_win = curwin;
460 di = dict_find(d, (char_u *)"textpropwin", -1);
461 if (di != NULL)
462 {
463 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
464 if (!win_valid(wp->w_popup_prop_win))
465 wp->w_popup_prop_win = curwin;
466 }
467
468 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200469 if (nr <= 0)
470 nr = find_prop_type_id(str, NULL);
471 if (nr <= 0)
472 semsg(_(e_invarg2), str);
473 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200474 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200475 }
476 }
477
478 di = dict_find(d, (char_u *)"textpropid", -1);
479 if (di != NULL)
480 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200481}
482
Bram Moolenaarb0992022020-01-30 14:55:42 +0100483/*
484 * Handle "moved" and "mousemoved" arguments.
485 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200486 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200487handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
488{
489 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
490 {
491 char_u *s = di->di_tv.vval.v_string;
492 int flags = 0;
493
494 if (STRCMP(s, "word") == 0)
495 flags = FIND_IDENT | FIND_STRING;
496 else if (STRCMP(s, "WORD") == 0)
497 flags = FIND_STRING;
498 else if (STRCMP(s, "expr") == 0)
499 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
500 else if (STRCMP(s, "any") != 0)
501 semsg(_(e_invarg2), s);
502 if (flags != 0)
503 {
504 if (mousemoved)
505 set_mousemoved_columns(wp, flags);
506 else
507 set_moved_columns(wp, flags);
508 }
509 }
510 else if (di->di_tv.v_type == VAR_LIST
511 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200512 && (di->di_tv.vval.v_list->lv_len == 2
513 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200514 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200515 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100516 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200517 int mincol;
518 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200519
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200520 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100521 li = l->lv_first;
522 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200523 {
524 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
525
526 // Three numbers, might be from popup_getoptions().
527 if (mousemoved)
528 wp->w_popup_mouse_row = nr;
529 else
530 wp->w_popup_lnum = nr;
531 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100532 if (nr == 0)
533 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200534 }
535
536 mincol = tv_get_number(&li->li_tv);
537 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200538 if (mousemoved)
539 {
540 wp->w_popup_mouse_mincol = mincol;
541 wp->w_popup_mouse_maxcol = maxcol;
542 }
543 else
544 {
545 wp->w_popup_mincol = mincol;
546 wp->w_popup_maxcol = maxcol;
547 }
548 }
549 else
550 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
551}
552
553 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200554check_highlight(dict_T *dict, char *name, char_u **pval)
555{
556 dictitem_T *di;
557 char_u *str;
558
559 di = dict_find(dict, (char_u *)name, -1);
560 if (di != NULL)
561 {
562 if (di->di_tv.v_type != VAR_STRING)
563 semsg(_(e_invargval), name);
564 else
565 {
566 str = tv_get_string(&di->di_tv);
567 if (*str != NUL)
568 *pval = vim_strsave(str);
569 }
570 }
571}
572
Bram Moolenaarae943152019-06-16 22:54:14 +0200573/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200574 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200575 */
576 static void
577popup_show_curline(win_T *wp)
578{
579 if (wp->w_cursor.lnum < wp->w_topline)
580 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200581 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200582 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200583 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200584 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200585 if (wp->w_topline < 1)
586 wp->w_topline = 1;
587 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
588 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200589 while (wp->w_topline < wp->w_cursor.lnum
590 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
591 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
592 > wp->w_height)
593 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200594 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200595
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200596 // Don't let "firstline" cause a scroll.
597 if (wp->w_firstline > 0)
598 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200599}
600
601/*
602 * Get the sign group name for window "wp".
603 * Returns a pointer to a static buffer, overwritten on the next call.
604 */
605 static char_u *
606popup_get_sign_name(win_T *wp)
607{
608 static char buf[30];
609
610 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
611 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200612}
613
614/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200615 * Highlight the line with the cursor.
616 * Also scrolls the text to put the cursor line in view.
617 */
618 static void
619popup_highlight_curline(win_T *wp)
620{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200621 int sign_id = 0;
622 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200623
Bram Moolenaar72570732019-11-30 14:21:53 +0100624 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200625
626 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
627 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200628 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200629
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200630 if (!sign_exists_by_name(sign_name))
631 {
632 char *linehl = "PopupSelected";
633
634 if (syn_name2id((char_u *)linehl) == 0)
635 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200636 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200637 }
638
Bram Moolenaar72570732019-11-30 14:21:53 +0100639 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200640 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
641 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200642 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200643 else
644 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200645 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200646}
647
648/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200649 * Shared between popup_create() and f_popup_setoptions().
650 */
651 static void
652apply_general_options(win_T *wp, dict_T *dict)
653{
654 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200655 int nr;
656 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200657
Bram Moolenaarae943152019-06-16 22:54:14 +0200658 // TODO: flip
659
660 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200661 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200662 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200663 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200664 if (wp->w_firstline < 0)
665 wp->w_firstline = -1;
666 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200667
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200668 di = dict_find(dict, (char_u *)"scrollbar", -1);
669 if (di != NULL)
670 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
671
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200672 str = dict_get_string(dict, (char_u *)"title", FALSE);
673 if (str != NULL)
674 {
675 vim_free(wp->w_popup_title);
676 wp->w_popup_title = vim_strsave(str);
677 }
678
Bram Moolenaar55881332020-08-18 13:04:15 +0200679 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
680 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200681 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200682
Bram Moolenaar55881332020-08-18 13:04:15 +0200683 nr = dict_get_bool(dict, (char_u *)"drag", -1);
684 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200685 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200686 if (nr)
687 wp->w_popup_flags |= POPF_DRAG;
688 else
689 wp->w_popup_flags &= ~POPF_DRAG;
690 }
691
Bram Moolenaar55881332020-08-18 13:04:15 +0200692 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
693 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100694 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100695 if (nr)
696 wp->w_popup_flags |= POPF_POSINVERT;
697 else
698 wp->w_popup_flags &= ~POPF_POSINVERT;
699 }
700
Bram Moolenaar55881332020-08-18 13:04:15 +0200701 nr = dict_get_bool(dict, (char_u *)"resize", -1);
702 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200703 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200704 if (nr)
705 wp->w_popup_flags |= POPF_RESIZE;
706 else
707 wp->w_popup_flags &= ~POPF_RESIZE;
708 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200709
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200710 di = dict_find(dict, (char_u *)"close", -1);
711 if (di != NULL)
712 {
713 int ok = TRUE;
714
715 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
716 {
717 char_u *s = di->di_tv.vval.v_string;
718
719 if (STRCMP(s, "none") == 0)
720 wp->w_popup_close = POPCLOSE_NONE;
721 else if (STRCMP(s, "button") == 0)
722 wp->w_popup_close = POPCLOSE_BUTTON;
723 else if (STRCMP(s, "click") == 0)
724 wp->w_popup_close = POPCLOSE_CLICK;
725 else
726 ok = FALSE;
727 }
728 else
729 ok = FALSE;
730 if (!ok)
731 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
732 }
733
Bram Moolenaarae943152019-06-16 22:54:14 +0200734 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
735 if (str != NULL)
736 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
737 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200738
Bram Moolenaarae943152019-06-16 22:54:14 +0200739 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
740 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200741
Bram Moolenaar790498b2019-06-01 22:15:29 +0200742 di = dict_find(dict, (char_u *)"borderhighlight", -1);
743 if (di != NULL)
744 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200745 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200746 emsg(_(e_listreq));
747 else
748 {
749 list_T *list = di->di_tv.vval.v_list;
750 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200751 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200752
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200753 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200754 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
755 ++i, li = li->li_next)
756 {
757 str = tv_get_string(&li->li_tv);
758 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100759 {
760 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200761 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100762 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200763 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200764 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
765 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100766 {
767 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200768 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200769 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100770 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200771 }
772 }
773
Bram Moolenaar790498b2019-06-01 22:15:29 +0200774 di = dict_find(dict, (char_u *)"borderchars", -1);
775 if (di != NULL)
776 {
777 if (di->di_tv.v_type != VAR_LIST)
778 emsg(_(e_listreq));
779 else
780 {
781 list_T *list = di->di_tv.vval.v_list;
782 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200783 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200784
785 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100786 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200787 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200788 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
789 ++i, li = li->li_next)
790 {
791 str = tv_get_string(&li->li_tv);
792 if (*str != NUL)
793 wp->w_border_char[i] = mb_ptr2char(str);
794 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200795 if (list->lv_len == 1)
796 for (i = 1; i < 8; ++i)
797 wp->w_border_char[i] = wp->w_border_char[0];
798 if (list->lv_len == 2)
799 {
800 for (i = 4; i < 8; ++i)
801 wp->w_border_char[i] = wp->w_border_char[1];
802 for (i = 1; i < 4; ++i)
803 wp->w_border_char[i] = wp->w_border_char[0];
804 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200805 }
806 }
807 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200808
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200809 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
810 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
811
Bram Moolenaarae943152019-06-16 22:54:14 +0200812 di = dict_find(dict, (char_u *)"zindex", -1);
813 if (di != NULL)
814 {
815 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
816 if (wp->w_zindex < 1)
817 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
818 if (wp->w_zindex > 32000)
819 wp->w_zindex = 32000;
820 }
821
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200822 di = dict_find(dict, (char_u *)"mask", -1);
823 if (di != NULL)
824 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200825 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200826
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200827 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200828 {
829 listitem_T *li;
830
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200831 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200832 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200833 {
834 if (li->li_tv.v_type != VAR_LIST
835 || li->li_tv.vval.v_list == NULL
836 || li->li_tv.vval.v_list->lv_len != 4)
837 {
838 ok = FALSE;
839 break;
840 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100841 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200842 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200843 }
844 }
845 if (ok)
846 {
847 wp->w_popup_mask = di->di_tv.vval.v_list;
848 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200849 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200850 }
851 else
852 semsg(_(e_invargval), "mask");
853 }
854
Bram Moolenaarae943152019-06-16 22:54:14 +0200855#if defined(FEAT_TIMERS)
856 // Add timer to close the popup after some time.
857 nr = dict_get_number(dict, (char_u *)"time");
858 if (nr > 0)
859 popup_add_timeout(wp, nr);
860#endif
861
Bram Moolenaar3397f742019-06-02 18:40:06 +0200862 di = dict_find(dict, (char_u *)"moved", -1);
863 if (di != NULL)
864 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200865 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200866 handle_moved_argument(wp, di, FALSE);
867 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200868
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200869 di = dict_find(dict, (char_u *)"mousemoved", -1);
870 if (di != NULL)
871 {
872 set_mousemoved_values(wp);
873 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200874 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200875
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200876 di = dict_find(dict, (char_u *)"cursorline", -1);
877 if (di != NULL)
878 {
879 if (di->di_tv.v_type == VAR_NUMBER)
880 {
881 if (di->di_tv.vval.v_number != 0)
882 wp->w_popup_flags |= POPF_CURSORLINE;
883 else
884 wp->w_popup_flags &= ~POPF_CURSORLINE;
885 }
886 else
887 semsg(_(e_invargval), "cursorline");
888 }
889
Bram Moolenaarae943152019-06-16 22:54:14 +0200890 di = dict_find(dict, (char_u *)"filter", -1);
891 if (di != NULL)
892 {
893 callback_T callback = get_callback(&di->di_tv);
894
895 if (callback.cb_name != NULL)
896 {
897 free_callback(&wp->w_filter_cb);
898 set_callback(&wp->w_filter_cb, &callback);
899 }
900 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200901 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
902 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200903 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200904 if (nr)
905 wp->w_popup_flags |= POPF_MAPPING;
906 else
907 wp->w_popup_flags &= ~POPF_MAPPING;
908 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200909
Bram Moolenaar581ba392019-09-03 22:08:33 +0200910 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
911 if (str != NULL)
912 {
913 if (STRCMP(str, "a") == 0)
914 wp->w_filter_mode = MODE_ALL;
915 else
916 wp->w_filter_mode = mode_str2flags(str);
917 }
918
Bram Moolenaarae943152019-06-16 22:54:14 +0200919 di = dict_find(dict, (char_u *)"callback", -1);
920 if (di != NULL)
921 {
922 callback_T callback = get_callback(&di->di_tv);
923
924 if (callback.cb_name != NULL)
925 {
926 free_callback(&wp->w_close_cb);
927 set_callback(&wp->w_close_cb, &callback);
928 }
929 }
930}
931
932/*
933 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200934 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200935 */
936 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200937apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200938{
939 int nr;
940
941 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200942
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200943 if (create)
944 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200945 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
946
Bram Moolenaarae943152019-06-16 22:54:14 +0200947 apply_general_options(wp, dict);
948
Bram Moolenaar55881332020-08-18 13:04:15 +0200949 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200950 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200951 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200952
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200953 // when "firstline" and "cursorline" are both set and the cursor would be
954 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200955 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
956 {
957 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
958 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200959 else if (wp->w_cursor.lnum < wp->w_firstline
960 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200961 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200962 wp->w_topline = wp->w_firstline;
963 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200964 }
965
Bram Moolenaar33796b32019-06-08 16:01:13 +0200966 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200967 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200968}
969
970/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200971 * Add lines to the popup from a list of strings.
972 */
973 static void
974add_popup_strings(buf_T *buf, list_T *l)
975{
976 listitem_T *li;
977 linenr_T lnum = 0;
978 char_u *p;
979
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200980 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200981 if (li->li_tv.v_type == VAR_STRING)
982 {
983 p = li->li_tv.vval.v_string;
984 ml_append_buf(buf, lnum++,
985 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
986 }
987}
988
989/*
990 * Add lines to the popup from a list of dictionaries.
991 */
992 static void
993add_popup_dicts(buf_T *buf, list_T *l)
994{
995 listitem_T *li;
996 listitem_T *pli;
997 linenr_T lnum = 0;
998 char_u *p;
999 dict_T *dict;
1000
1001 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001002 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001003 {
1004 if (li->li_tv.v_type != VAR_DICT)
1005 {
1006 emsg(_(e_dictreq));
1007 return;
1008 }
1009 dict = li->li_tv.vval.v_dict;
1010 p = dict == NULL ? NULL
1011 : dict_get_string(dict, (char_u *)"text", FALSE);
1012 ml_append_buf(buf, lnum++,
1013 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1014 }
1015
1016 // add the text properties
1017 lnum = 1;
1018 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1019 {
1020 dictitem_T *di;
1021 list_T *plist;
1022
1023 dict = li->li_tv.vval.v_dict;
1024 di = dict_find(dict, (char_u *)"props", -1);
1025 if (di != NULL)
1026 {
1027 if (di->di_tv.v_type != VAR_LIST)
1028 {
1029 emsg(_(e_listreq));
1030 return;
1031 }
1032 plist = di->di_tv.vval.v_list;
1033 if (plist != NULL)
1034 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001035 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001036 {
1037 if (pli->li_tv.v_type != VAR_DICT)
1038 {
1039 emsg(_(e_dictreq));
1040 return;
1041 }
1042 dict = pli->li_tv.vval.v_dict;
1043 if (dict != NULL)
1044 {
1045 int col = dict_get_number(dict, (char_u *)"col");
1046
1047 prop_add_common( lnum, col, dict, buf, NULL);
1048 }
1049 }
1050 }
1051 }
1052 }
1053}
1054
1055/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001056 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1057 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001058 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001059popup_top_extra(win_T *wp)
1060{
1061 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1062
1063 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1064 return 1;
1065 return extra;
1066}
1067
1068/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001069 * Get the padding plus border at the left.
1070 */
1071 int
1072popup_left_extra(win_T *wp)
1073{
1074 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1075}
1076
1077/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001078 * Return the height of popup window "wp", including border and padding.
1079 */
1080 int
1081popup_height(win_T *wp)
1082{
1083 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001084 + popup_top_extra(wp)
1085 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001086}
1087
1088/*
1089 * Return the width of popup window "wp", including border, padding and
1090 * scrollbar.
1091 */
1092 int
1093popup_width(win_T *wp)
1094{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001095 // w_leftcol is how many columns of the core are left of the screen
1096 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001097 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001098 + popup_extra_width(wp)
1099 + wp->w_popup_rightoff;
1100}
1101
1102/*
1103 * Return the extra width of popup window "wp": border, padding and scrollbar.
1104 */
1105 int
1106popup_extra_width(win_T *wp)
1107{
1108 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1109 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1110 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001111}
1112
1113/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001114 * Adjust the position and size of the popup to fit on the screen.
1115 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001116 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001117popup_adjust_position(win_T *wp)
1118{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001119 linenr_T lnum;
1120 int wrapped = 0;
1121 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001122 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001123 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001124 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001125 int center_vert = FALSE;
1126 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001127 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001128 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001129 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1130 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1131 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1132 int extra_height = top_extra + bot_extra;
1133 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001134 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001135 int org_winrow = wp->w_winrow;
1136 int org_wincol = wp->w_wincol;
1137 int org_width = wp->w_width;
1138 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001139 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001140 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001141 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001142 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001143 int wantline = wp->w_wantline; // adjusted for textprop
1144 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001145 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001146 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001147
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001148 wp->w_winrow = 0;
1149 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001150 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001151 wp->w_popup_leftoff = 0;
1152 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001153
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001154 // May need to update the "cursorline" highlighting, which may also change
1155 // "topline"
1156 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1157 popup_highlight_curline(wp);
1158
Bram Moolenaar12034e22019-08-25 22:25:02 +02001159 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1160 {
1161 win_T *prop_win = wp->w_popup_prop_win;
1162 textprop_T prop;
1163 linenr_T prop_lnum;
1164 pos_T pos;
1165 int screen_row;
1166 int screen_scol;
1167 int screen_ccol;
1168 int screen_ecol;
1169
1170 // Popup window is positioned relative to a text property.
1171 if (find_visible_prop(prop_win,
1172 wp->w_popup_prop_type, wp->w_popup_prop_id,
1173 &prop, &prop_lnum) == FAIL)
1174 {
1175 // Text property is no longer visible, hide the popup.
1176 // Unhiding the popup is done in check_popup_unhidden().
1177 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1178 {
1179 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001180 if (win_valid(wp->w_popup_prop_win))
1181 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1182 }
1183 return;
1184 }
1185
1186 // Compute the desired position from the position of the text
1187 // property. Use "wantline" and "wantcol" as offsets.
1188 pos.lnum = prop_lnum;
1189 pos.col = prop.tp_col;
1190 if (wp->w_popup_pos == POPPOS_TOPLEFT
1191 || wp->w_popup_pos == POPPOS_BOTLEFT)
1192 pos.col += prop.tp_len - 1;
1193 textpos2screenpos(prop_win, &pos, &screen_row,
1194 &screen_scol, &screen_ccol, &screen_ecol);
1195
1196 if (wp->w_popup_pos == POPPOS_TOPLEFT
1197 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1198 // below the text
1199 wantline = screen_row + wantline + 1;
1200 else
1201 // above the text
1202 wantline = screen_row + wantline - 1;
1203 center_vert = FALSE;
1204 if (wp->w_popup_pos == POPPOS_TOPLEFT
1205 || wp->w_popup_pos == POPPOS_BOTLEFT)
1206 // right of the text
1207 wantcol = screen_ecol + wantcol;
1208 else
1209 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001210 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001211 use_wantcol = TRUE;
1212 }
1213 else
1214 {
1215 // If no line was specified default to vertical centering.
1216 if (wantline == 0)
1217 center_vert = TRUE;
1218 else if (wantline < 0)
1219 // If "wantline" is negative it actually means zero.
1220 wantline = 0;
1221 if (wantcol < 0)
1222 // If "wantcol" is negative it actually means zero.
1223 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001224 }
1225
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001226 if (wp->w_popup_pos == POPPOS_CENTER)
1227 {
1228 // center after computing the size
1229 center_vert = TRUE;
1230 center_hor = TRUE;
1231 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001232 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001233 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001234 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1235 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001236 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001237 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001238 if (wp->w_winrow >= Rows)
1239 wp->w_winrow = Rows - 1;
1240 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001241
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001242 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001243 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001244 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1245 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001246 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001247 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001248 // Need to see at least one character after the decoration.
1249 if (wp->w_wincol > Columns - left_extra - 1)
1250 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001251 }
1252 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001253
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001254 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001255 // When left aligned use the space available, but shift to the left when we
1256 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001257 maxspace = Columns - wp->w_wincol - left_extra;
1258 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001259 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001260 {
1261 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001262 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001263 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001264
1265 if (wp->w_p_nu || wp->w_p_rnu)
1266 margin_width = number_width(wp) + 1;
1267#ifdef FEAT_FOLDING
1268 margin_width += wp->w_p_fdc;
1269#endif
1270#ifdef FEAT_SIGNS
1271 if (signcolumn_on(wp))
1272 margin_width += 2;
1273#endif
1274 if (margin_width >= maxwidth)
1275 margin_width = maxwidth - 1;
1276
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001277 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001278 minheight = wp->w_minheight;
1279#ifdef FEAT_TERMINAL
1280 // A terminal popup initially does not have content, use a default minimal
1281 // width of 20 characters and height of 5 lines.
1282 if (wp->w_buffer->b_term != NULL)
1283 {
1284 if (minwidth == 0)
1285 minwidth = 20;
1286 if (minheight == 0)
1287 minheight = 5;
1288 }
1289#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001290
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001291 if (wp->w_maxheight > 0)
1292 maxheight = wp->w_maxheight;
1293
Bram Moolenaar8d241042019-06-12 23:40:01 +02001294 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001295 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001296 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001297 if (wp->w_topline < 1)
1298 wp->w_topline = 1;
1299 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001300 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1301
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001302 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001303 // Use a minimum width of one, so that something shows when there is no
1304 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001305 // When "firstline" is -1 then start with the last buffer line and go
1306 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001307 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001308 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001309 if (wp->w_firstline < 0)
1310 lnum = wp->w_buffer->b_ml.ml_line_count;
1311 else
1312 lnum = wp->w_topline;
1313 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001314 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001315 int len;
1316 int w_width = wp->w_width;
1317
1318 // Count Tabs for what they are worth and compute the length based on
1319 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001320 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001321 if (wp->w_width < maxwidth)
1322 wp->w_width = maxwidth;
1323 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001324 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001325 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001326
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001327 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001328 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001329 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001330 {
1331 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001332 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001333 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001334 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001335 }
1336 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001337 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001338 && allow_adjust_left
1339 && (wp->w_popup_pos == POPPOS_TOPLEFT
1340 || wp->w_popup_pos == POPPOS_BOTLEFT))
1341 {
1342 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001343 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001344
Bram Moolenaar51c31312019-06-15 22:27:23 +02001345 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001346 {
1347 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001348
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001349 len -= truncate_shift;
1350 shift_by -= truncate_shift;
1351 }
1352
1353 wp->w_wincol -= shift_by;
1354 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001355 wp->w_width = maxwidth;
1356 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001357 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001358 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001359 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001360 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1361 wp->w_width = wp->w_maxwidth;
1362 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001363
1364 if (wp->w_firstline < 0)
1365 --lnum;
1366 else
1367 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001368
1369 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001370 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001371 && (wp->w_firstline >= 0
1372 ? lnum - wp->w_topline
1373 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001374 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001375 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001376 }
1377
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001378 if (wp->w_firstline < 0)
1379 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1380
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001381 wp->w_has_scrollbar = wp->w_want_scrollbar
1382 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001383#ifdef FEAT_TERMINAL
1384 if (wp->w_buffer->b_term != NULL)
1385 // Terminal window never has a scrollbar, adjusts to window height.
1386 wp->w_has_scrollbar = FALSE;
1387#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001388 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001389 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001390 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001391 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001392 // make space for the scrollbar if needed, when lines wrap and when
1393 // applying minwidth
1394 if (maxwidth + right_extra >= maxspace
1395 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1396 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001397 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001398
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001399 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1400 {
1401 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1402
1403 if (minwidth < title_len)
1404 minwidth = title_len;
1405 }
1406
1407 if (minwidth > 0 && wp->w_width < minwidth)
1408 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001409 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001410 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001411 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001412 // some columns cut off on the right
1413 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001414 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001415 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001416 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001417 {
1418 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1419 if (wp->w_wincol < 0)
1420 wp->w_wincol = 0;
1421 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001422 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1423 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1424 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001425 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001426
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001427 // Right aligned: move to the right if needed.
1428 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001429 if (leftoff >= 0)
1430 wp->w_wincol = leftoff;
1431 else if (wp->w_popup_fixed)
1432 {
1433 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001434 // columns when displaying the window, minus left border and
1435 // padding.
1436 if (-leftoff > left_extra)
1437 wp->w_leftcol = -leftoff - left_extra;
1438 wp->w_width -= wp->w_leftcol;
1439 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001440 if (wp->w_width < 0)
1441 wp->w_width = 0;
1442 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001443 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001444
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001445 if (wp->w_p_wrap || (!wp->w_popup_fixed
1446 && (wp->w_popup_pos == POPPOS_TOPLEFT
1447 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001448 {
1449 int want_col = 0;
1450
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001451 // try to show the right border and any scrollbar
1452 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001453 if (want_col > 0 && wp->w_wincol > 0
1454 && wp->w_wincol + want_col >= Columns)
1455 {
1456 wp->w_wincol = Columns - want_col;
1457 if (wp->w_wincol < 0)
1458 wp->w_wincol = 0;
1459 }
1460 }
1461
Bram Moolenaar8d241042019-06-12 23:40:01 +02001462 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1463 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001464 if (minheight > 0 && wp->w_height < minheight)
1465 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001466 if (maxheight > 0 && wp->w_height > maxheight)
1467 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001468 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001469 if (wp->w_height > Rows - wp->w_winrow)
1470 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001471
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001472 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001473 {
1474 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1475 if (wp->w_winrow < 0)
1476 wp->w_winrow = 0;
1477 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001478 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001479 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001480 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001481 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001482 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001483 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001484 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1485 {
1486 // Bottom aligned but does not fit, and less space on the other
1487 // side or "posinvert" is off: reduce height.
1488 wp->w_winrow = 0;
1489 wp->w_height = wantline - extra_height;
1490 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001491 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001492 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001493 // Not enough space and more space on the other side: make top
1494 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001495 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001496 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001497 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001498 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001499 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1500 || wp->w_popup_pos == POPPOS_TOPLEFT)
1501 {
1502 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1503 && wantline * 2 > Rows
1504 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001505 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001506 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001507 // make bottom aligned and recompute the height
1508 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001509 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001510 if (wp->w_winrow < 0)
1511 {
1512 wp->w_height += wp->w_winrow;
1513 wp->w_winrow = 0;
1514 }
1515 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001516 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001517 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001518 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001519 adjust_height_for_top_aligned = TRUE;
1520 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001521 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001522
1523 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1524 && wp->w_winrow + wp->w_height + extra_height > Rows)
1525 {
1526 // Bottom of the popup goes below the last line, reduce the height and
1527 // add a scrollbar.
1528 wp->w_height = Rows - wp->w_winrow - extra_height;
1529#ifdef FEAT_TERMINAL
1530 if (wp->w_buffer->b_term == NULL)
1531#endif
1532 wp->w_has_scrollbar = TRUE;
1533 }
1534
1535 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001536 if (wp->w_winrow >= Rows)
1537 wp->w_winrow = Rows - 1;
1538 else if (wp->w_winrow < 0)
1539 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001540
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001541 if (wp->w_height != org_height)
1542 win_comp_scroll(wp);
1543
Bram Moolenaar17146962019-05-30 00:12:11 +02001544 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001545 if (win_valid(wp->w_popup_prop_win))
1546 {
1547 wp->w_popup_prop_changedtick =
1548 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1549 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1550 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001551
1552 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001553 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001554 if (org_winrow != wp->w_winrow
1555 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001556 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001557 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001558 || org_width != wp->w_width
1559 || org_height != wp->w_height)
1560 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001561 redraw_win_later(wp, NOT_VALID);
1562 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1563 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001564 popup_mask_refresh = TRUE;
1565 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001566}
1567
Bram Moolenaar17627312019-06-02 19:53:44 +02001568typedef enum
1569{
1570 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001571 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001572 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001573 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001574 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001575 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001576 TYPE_PREVIEW, // preview window
1577 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001578} create_type_T;
1579
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001580/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001581 * Make "buf" empty and set the contents to "text".
1582 * Used by popup_create() and popup_settext().
1583 */
1584 static void
1585popup_set_buffer_text(buf_T *buf, typval_T text)
1586{
1587 int lnum;
1588
1589 // Clear the buffer, then replace the lines.
1590 curbuf = buf;
1591 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001592 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001593 curbuf = curwin->w_buffer;
1594
1595 // Add text to the buffer.
1596 if (text.v_type == VAR_STRING)
1597 {
1598 // just a string
1599 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1600 }
1601 else
1602 {
1603 list_T *l = text.vval.v_list;
1604
1605 if (l->lv_len > 0)
1606 {
1607 if (l->lv_first->li_tv.v_type == VAR_STRING)
1608 // list of strings
1609 add_popup_strings(buf, l);
1610 else
1611 // list of dictionaries
1612 add_popup_dicts(buf, l);
1613 }
1614 }
1615
1616 // delete the line that was in the empty buffer
1617 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001618 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001619 curbuf = curwin->w_buffer;
1620}
1621
1622/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001623 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1624 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001625 * Return FAIL if the parsing fails.
1626 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001627 static int
1628parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001629{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001630 char_u *p =
1631#ifdef FEAT_QUICKFIX
1632 !is_preview ? p_cpp :
1633#endif
1634 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001635
Bram Moolenaar258cef52019-08-21 17:29:29 +02001636 if (wp != NULL)
1637 wp->w_popup_flags &= ~POPF_INFO_MENU;
1638
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001639 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001640 {
1641 char_u *e, *dig;
1642 char_u *s = p;
1643 int x;
1644
1645 e = vim_strchr(p, ':');
1646 if (e == NULL || e[1] == NUL)
1647 return FAIL;
1648
1649 p = vim_strchr(e, ',');
1650 if (p == NULL)
1651 p = e + STRLEN(e);
1652 dig = e + 1;
1653 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001654
1655 if (STRNCMP(s, "height:", 7) == 0)
1656 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001657 if (dig != p)
1658 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001659 if (wp != NULL)
1660 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001661 if (is_preview)
1662 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001663 wp->w_maxheight = x;
1664 }
1665 }
1666 else if (STRNCMP(s, "width:", 6) == 0)
1667 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001668 if (dig != p)
1669 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001670 if (wp != NULL)
1671 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001672 if (is_preview)
1673 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001674 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001675 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001676 }
1677 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001678 else if (STRNCMP(s, "highlight:", 10) == 0)
1679 {
1680 if (wp != NULL)
1681 {
1682 int c = *p;
1683
1684 *p = NUL;
1685 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1686 s + 10, OPT_FREE|OPT_LOCAL, 0);
1687 *p = c;
1688 }
1689 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001690 else if (STRNCMP(s, "border:", 7) == 0)
1691 {
1692 char_u *arg = s + 7;
1693 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1694 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1695 int i;
1696
1697 if (!on && !off)
1698 return FAIL;
1699 if (wp != NULL)
1700 {
1701 for (i = 0; i < 4; ++i)
1702 wp->w_popup_border[i] = on ? 1 : 0;
1703 if (off)
1704 // only show the X for close when there is a border
1705 wp->w_popup_close = POPCLOSE_NONE;
1706 }
1707 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001708 else if (STRNCMP(s, "align:", 6) == 0)
1709 {
1710 char_u *arg = s + 6;
1711 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1712 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1713
1714 if (!menu && !item)
1715 return FAIL;
1716 if (wp != NULL && menu)
1717 wp->w_popup_flags |= POPF_INFO_MENU;
1718 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001719 else
1720 return FAIL;
1721 }
1722 return OK;
1723}
1724
1725/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001726 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1727 * is not NULL.
1728 * Return FAIL if the parsing fails.
1729 */
1730 int
1731parse_previewpopup(win_T *wp)
1732{
1733 return parse_popup_option(wp, TRUE);
1734}
1735
1736/*
1737 * Parse the 'completepopup' option and apply the values to window "wp" if it
1738 * is not NULL.
1739 * Return FAIL if the parsing fails.
1740 */
1741 int
1742parse_completepopup(win_T *wp)
1743{
1744 return parse_popup_option(wp, FALSE);
1745}
1746
1747/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001748 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001749 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001750 */
1751 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001752popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001753{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001754 poppos_T ppt = POPPOS_NONE;
1755
1756 if (d != NULL)
1757 ppt = get_pos_entry(d, FALSE);
1758
Bram Moolenaar79648732019-07-18 21:43:07 +02001759 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001760 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001761 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001762 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1763 }
1764 else
1765 {
1766 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1767 if (wp->w_wantline == 0) // cursor in first line
1768 {
1769 wp->w_wantline = 2;
1770 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1771 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1772 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001773 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001774
Bram Moolenaar79648732019-07-18 21:43:07 +02001775 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001776 if (wp->w_wantcol > Columns - width)
1777 {
1778 wp->w_wantcol = Columns - width;
1779 if (wp->w_wantcol < 1)
1780 wp->w_wantcol = 1;
1781 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001782
Bram Moolenaar79648732019-07-18 21:43:07 +02001783 popup_adjust_position(wp);
1784}
1785
1786/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001787 * Set w_wantline and w_wantcol for the a given screen position.
1788 * Caller must take care of running into the window border.
1789 */
1790 void
1791popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1792{
1793 wp->w_wantline = row;
1794 wp->w_wantcol = col;
1795 popup_adjust_position(wp);
1796}
1797
1798/*
1799 * Add a border and lef&right padding.
1800 */
1801 static void
1802add_border_left_right_padding(win_T *wp)
1803{
1804 int i;
1805
1806 for (i = 0; i < 4; ++i)
1807 {
1808 wp->w_popup_border[i] = 1;
1809 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1810 }
1811}
1812
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001813#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001814/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001815 * Return TRUE if there is any popup window with a terminal buffer.
1816 */
1817 static int
1818popup_terminal_exists(void)
1819{
1820 win_T *wp;
1821 tabpage_T *tp;
1822
1823 FOR_ALL_POPUPWINS(wp)
1824 if (wp->w_buffer->b_term != NULL)
1825 return TRUE;
1826 FOR_ALL_TABPAGES(tp)
1827 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1828 if (wp->w_buffer->b_term != NULL)
1829 return TRUE;
1830 return FALSE;
1831}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001832#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001833
1834/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001835 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001836 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001837 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001838 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001839 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001840 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001841popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001842{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001843 win_T *wp;
1844 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001845 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001846 int new_buffer;
1847 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001848 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001849 int nr;
1850 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001851
Bram Moolenaar79648732019-07-18 21:43:07 +02001852 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001853 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001854 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001855 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001856 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001857 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001858 if (buf == NULL)
1859 {
1860 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1861 return NULL;
1862 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001863#ifdef FEAT_TERMINAL
1864 if (buf->b_term != NULL && popup_terminal_exists())
1865 {
1866 emsg(_("E861: Cannot open a second popup with a terminal"));
1867 return NULL;
1868 }
1869#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001870 }
1871 else if (!(argvars[0].v_type == VAR_STRING
1872 && argvars[0].vval.v_string != NULL)
1873 && !(argvars[0].v_type == VAR_LIST
1874 && argvars[0].vval.v_list != NULL))
1875 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001876 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001877 return NULL;
1878 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001879 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001880 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001881 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001882 return NULL;
1883 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001884 d = argvars[1].vval.v_dict;
1885 }
1886
1887 if (d != NULL)
1888 {
1889 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1890 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1891 else if (type == TYPE_NOTIFICATION)
1892 tabnr = -1; // notifications are global by default
1893 else
1894 tabnr = 0;
1895 if (tabnr > 0)
1896 {
1897 tp = find_tabpage(tabnr);
1898 if (tp == NULL)
1899 {
1900 semsg(_("E997: Tabpage not found: %d"), tabnr);
1901 return NULL;
1902 }
1903 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001904 }
1905
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001906 // Create the window and buffer.
1907 wp = win_alloc_popup_win();
1908 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001909 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001910 if (rettv != NULL)
1911 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001912 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001913 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001914
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001915 if (buf != NULL)
1916 {
1917 // use existing buffer
1918 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001919 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001920 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001921 buffer_ensure_loaded(buf);
1922 }
1923 else
1924 {
1925 // create a new buffer associated with the popup
1926 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001927 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001928 if (buf == NULL)
1929 return NULL;
1930 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001931
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001932 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001933
Bram Moolenaar46451042019-08-24 15:50:46 +02001934 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001935 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001936 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001937 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001938 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001939 buf->b_p_ul = -1; // no undo
1940 buf->b_p_swf = FALSE; // no swap file
1941 buf->b_p_bl = FALSE; // unlisted buffer
1942 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001943
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001944 // Avoid that 'buftype' is reset when this buffer is entered.
1945 buf->b_p_initialized = TRUE;
1946 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001947 wp->w_p_wrap = TRUE; // 'wrap' is default on
1948 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001949
Bram Moolenaara3fce622019-06-20 02:31:49 +02001950 if (tp != NULL)
1951 {
1952 // popup on specified tab page
1953 wp->w_next = tp->tp_first_popupwin;
1954 tp->tp_first_popupwin = wp;
1955 }
1956 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001957 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001958 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001959 wp->w_next = curtab->tp_first_popupwin;
1960 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001961 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001962 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001963 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001964 win_T *prev = first_popupwin;
1965
1966 // Global popup: add at the end, so that it gets displayed on top of
1967 // older ones with the same zindex. Matters for notifications.
1968 if (first_popupwin == NULL)
1969 first_popupwin = wp;
1970 else
1971 {
1972 while (prev->w_next != NULL)
1973 prev = prev->w_next;
1974 prev->w_next = wp;
1975 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001976 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001977
Bram Moolenaar79648732019-07-18 21:43:07 +02001978 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001979 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001980
Bram Moolenaar79648732019-07-18 21:43:07 +02001981 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001982 {
1983 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001984 }
1985 if (type == TYPE_ATCURSOR)
1986 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001987 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001988 set_moved_values(wp);
1989 set_moved_columns(wp, FIND_STRING);
1990 }
1991
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001992 if (type == TYPE_BEVAL)
1993 {
1994 wp->w_popup_pos = POPPOS_BOTLEFT;
1995
1996 // by default use the mouse position
1997 wp->w_wantline = mouse_row;
1998 if (wp->w_wantline <= 0) // mouse on first line
1999 {
2000 wp->w_wantline = 2;
2001 wp->w_popup_pos = POPPOS_TOPLEFT;
2002 }
2003 wp->w_wantcol = mouse_col + 1;
2004 set_mousemoved_values(wp);
2005 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2006 }
2007
Bram Moolenaar33796b32019-06-08 16:01:13 +02002008 // set default values
2009 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002010 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002011
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002012 if (type == TYPE_NOTIFICATION)
2013 {
2014 win_T *twp, *nextwin;
2015 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002016
2017 // Try to not overlap with another global popup. Guess we need 3
2018 // more screen lines than buffer lines.
2019 wp->w_wantline = 1;
2020 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2021 {
2022 nextwin = twp->w_next;
2023 if (twp != wp
2024 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2025 && twp->w_winrow <= wp->w_wantline - 1 + height
2026 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2027 {
2028 // move to below this popup and restart the loop to check for
2029 // overlap with other popups
2030 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2031 nextwin = first_popupwin;
2032 }
2033 }
2034 if (wp->w_wantline + height > Rows)
2035 {
2036 // can't avoid overlap, put on top in the hope that message goes
2037 // away soon.
2038 wp->w_wantline = 1;
2039 }
2040
2041 wp->w_wantcol = 10;
2042 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002043 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002044 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002045 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002046 for (i = 0; i < 4; ++i)
2047 wp->w_popup_border[i] = 1;
2048 wp->w_popup_padding[1] = 1;
2049 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002050
2051 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002052 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002053 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2054 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002055 }
2056
Bram Moolenaara730e552019-06-16 19:05:31 +02002057 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002058 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002059 wp->w_popup_pos = POPPOS_CENTER;
2060 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002061 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002062 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002063 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002064 }
2065
Bram Moolenaara730e552019-06-16 19:05:31 +02002066 if (type == TYPE_MENU)
2067 {
2068 typval_T tv;
2069 callback_T callback;
2070
2071 tv.v_type = VAR_STRING;
2072 tv.vval.v_string = (char_u *)"popup_filter_menu";
2073 callback = get_callback(&tv);
2074 if (callback.cb_name != NULL)
2075 set_callback(&wp->w_filter_cb, &callback);
2076
2077 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002078 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002079 }
2080
Bram Moolenaar79648732019-07-18 21:43:07 +02002081 if (type == TYPE_PREVIEW)
2082 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002083 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002084 wp->w_popup_close = POPCLOSE_BUTTON;
2085 for (i = 0; i < 4; ++i)
2086 wp->w_popup_border[i] = 1;
2087 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002088 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002089 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002090# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002091 if (type == TYPE_INFO)
2092 {
2093 wp->w_popup_pos = POPPOS_TOPLEFT;
2094 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2095 wp->w_popup_close = POPCLOSE_BUTTON;
2096 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002097 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002098 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002099# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002100
Bram Moolenaarae943152019-06-16 22:54:14 +02002101 for (i = 0; i < 4; ++i)
2102 VIM_CLEAR(wp->w_border_highlight[i]);
2103 for (i = 0; i < 8; ++i)
2104 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002105 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002106 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002107 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002108
Bram Moolenaar79648732019-07-18 21:43:07 +02002109 if (d != NULL)
2110 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002111 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002112
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002113#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002114 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2115 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002116#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002117
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002118 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002119
2120 wp->w_vsep_width = 0;
2121
2122 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002123 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002124
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002125#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002126 // When running a terminal in the popup it becomes the current window.
2127 if (buf->b_term != NULL)
2128 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002129#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002130
Bram Moolenaara730e552019-06-16 19:05:31 +02002131 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002132}
2133
2134/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002135 * popup_clear()
2136 */
2137 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002138f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002139{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002140 int force = FALSE;
2141
2142 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002143 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002144 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002145}
2146
2147/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002148 * popup_create({text}, {options})
2149 */
2150 void
2151f_popup_create(typval_T *argvars, typval_T *rettv)
2152{
Bram Moolenaar17627312019-06-02 19:53:44 +02002153 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002154}
2155
2156/*
2157 * popup_atcursor({text}, {options})
2158 */
2159 void
2160f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2161{
Bram Moolenaar17627312019-06-02 19:53:44 +02002162 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002163}
2164
2165/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002166 * popup_beval({text}, {options})
2167 */
2168 void
2169f_popup_beval(typval_T *argvars, typval_T *rettv)
2170{
2171 popup_create(argvars, rettv, TYPE_BEVAL);
2172}
2173
2174/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002175 * Invoke the close callback for window "wp" with value "result".
2176 * Careful: The callback may make "wp" invalid!
2177 */
2178 static void
2179invoke_popup_callback(win_T *wp, typval_T *result)
2180{
2181 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002182 typval_T argv[3];
2183
2184 argv[0].v_type = VAR_NUMBER;
2185 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2186
2187 if (result != NULL && result->v_type != VAR_UNKNOWN)
2188 copy_tv(result, &argv[1]);
2189 else
2190 {
2191 argv[1].v_type = VAR_NUMBER;
2192 argv[1].vval.v_number = 0;
2193 }
2194
2195 argv[2].v_type = VAR_UNKNOWN;
2196
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002197 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002198 if (result != NULL)
2199 clear_tv(&argv[1]);
2200 clear_tv(&rettv);
2201}
2202
2203/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002204 * Make "prevwin" the current window, unless it's equal to "wp".
2205 * Otherwise make "firstwin" the current window.
2206 */
2207 static void
2208back_to_prevwin(win_T *wp)
2209{
2210 if (win_valid(prevwin) && wp != prevwin)
2211 win_enter(prevwin, FALSE);
2212 else
2213 win_enter(firstwin, FALSE);
2214}
2215
2216/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002217 * Close popup "wp" and invoke any close callback for it.
2218 */
2219 static void
2220popup_close_and_callback(win_T *wp, typval_T *arg)
2221{
2222 int id = wp->w_id;
2223
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002224#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002225 if (wp == curwin && curbuf->b_term != NULL)
2226 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002227 win_T *owp;
2228
2229 // Closing popup window with a terminal: put focus back on the first
2230 // that works:
2231 // - another popup window with a terminal
2232 // - the previous window
2233 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002234 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002235 if (owp != curwin && owp->w_buffer->b_term != NULL)
2236 break;
2237 if (owp != NULL)
2238 win_enter(owp, FALSE);
2239 else
2240 {
2241 for (owp = curtab->tp_first_popupwin; owp != NULL;
2242 owp = owp->w_next)
2243 if (owp != curwin && owp->w_buffer->b_term != NULL)
2244 break;
2245 if (owp != NULL)
2246 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002247 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002248 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002249 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002250 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002251#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002252
Bram Moolenaar49540192019-12-11 19:34:54 +01002253 // Just in case a check higher up is missing.
2254 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002255 {
2256 // To avoid getting stuck when win_execute() does something that causes
2257 // an error, stop calling the filter callback.
2258 free_callback(&wp->w_filter_cb);
2259
Bram Moolenaar49540192019-12-11 19:34:54 +01002260 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002261 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002262
Bram Moolenaarcee52202020-03-11 14:19:58 +01002263 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002264 if (wp->w_close_cb.cb_name != NULL)
2265 // Careful: This may make "wp" invalid.
2266 invoke_popup_callback(wp, arg);
2267
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002268 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002269 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002270}
2271
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002272 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002273popup_close_with_retval(win_T *wp, int retval)
2274{
2275 typval_T res;
2276
2277 res.v_type = VAR_NUMBER;
2278 res.vval.v_number = retval;
2279 popup_close_and_callback(wp, &res);
2280}
2281
Bram Moolenaar3397f742019-06-02 18:40:06 +02002282/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002283 * Close popup "wp" because of a mouse click.
2284 */
2285 void
2286popup_close_for_mouse_click(win_T *wp)
2287{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002288 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002289}
2290
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002291 static void
2292check_mouse_moved(win_T *wp, win_T *mouse_wp)
2293{
2294 // Close the popup when all if these are true:
2295 // - the mouse is not on this popup
2296 // - "mousemoved" was used
2297 // - the mouse is no longer on the same screen row or the mouse column is
2298 // outside of the relevant text
2299 if (wp != mouse_wp
2300 && wp->w_popup_mouse_row != 0
2301 && (wp->w_popup_mouse_row != mouse_row
2302 || mouse_col < wp->w_popup_mouse_mincol
2303 || mouse_col > wp->w_popup_mouse_maxcol))
2304 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002305 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002306 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002307 }
2308}
2309
2310/*
2311 * Called when the mouse moved: may close a popup with "mousemoved".
2312 */
2313 void
2314popup_handle_mouse_moved(void)
2315{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002316 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002317 win_T *mouse_wp;
2318 int row = mouse_row;
2319 int col = mouse_col;
2320
2321 // find the window where the mouse is in
2322 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2323
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002324 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2325 {
2326 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002327 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002328 }
2329 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2330 {
2331 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002332 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002333 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002334}
2335
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002336/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002337 * In a filter: check if the typed key is a mouse event that is used for
2338 * dragging the popup.
2339 */
2340 static void
2341filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2342{
2343 int row = mouse_row;
2344 int col = mouse_col;
2345
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002346 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002347 && is_mouse_key(c)
2348 && (wp == popup_dragwin
2349 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2350 // do not consume the key, allow for dragging the popup
2351 rettv->vval.v_number = 0;
2352}
2353
Bram Moolenaara730e552019-06-16 19:05:31 +02002354/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002355 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002356 */
2357 void
2358f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2359{
2360 int id = tv_get_number(&argvars[0]);
2361 win_T *wp = win_id2wp(id);
2362 char_u *key = tv_get_string(&argvars[1]);
2363 typval_T res;
2364 int c;
2365 linenr_T old_lnum;
2366
2367 // If the popup has been closed do not consume the key.
2368 if (wp == NULL)
2369 return;
2370
2371 c = *key;
2372 if (c == K_SPECIAL && key[1] != NUL)
2373 c = TO_SPECIAL(key[1], key[2]);
2374
2375 // consume all keys until done
2376 rettv->vval.v_number = 1;
2377 res.v_type = VAR_NUMBER;
2378
2379 old_lnum = wp->w_cursor.lnum;
2380 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2381 --wp->w_cursor.lnum;
2382 if ((c == 'j' || c == 'J' || c == K_DOWN)
2383 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2384 ++wp->w_cursor.lnum;
2385 if (old_lnum != wp->w_cursor.lnum)
2386 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002387 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002388 return;
2389 }
2390
2391 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2392 {
2393 // Cancelled, invoke callback with -1
2394 res.vval.v_number = -1;
2395 popup_close_and_callback(wp, &res);
2396 return;
2397 }
2398 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2399 {
2400 // Invoke callback with current index.
2401 res.vval.v_number = wp->w_cursor.lnum;
2402 popup_close_and_callback(wp, &res);
2403 return;
2404 }
2405
2406 filter_handle_drag(wp, c, rettv);
2407}
2408
2409/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002410 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002411 */
2412 void
2413f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2414{
2415 int id = tv_get_number(&argvars[0]);
2416 win_T *wp = win_id2wp(id);
2417 char_u *key = tv_get_string(&argvars[1]);
2418 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002419 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002420
2421 // If the popup has been closed don't consume the key.
2422 if (wp == NULL)
2423 return;
2424
Bram Moolenaara730e552019-06-16 19:05:31 +02002425 c = *key;
2426 if (c == K_SPECIAL && key[1] != NUL)
2427 c = TO_SPECIAL(key[1], key[2]);
2428
Bram Moolenaara42d9452019-06-15 21:46:30 +02002429 // consume all keys until done
2430 rettv->vval.v_number = 1;
2431
Bram Moolenaara730e552019-06-16 19:05:31 +02002432 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002433 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002434 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002435 res.vval.v_number = 0;
2436 else
2437 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002438 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002439 return;
2440 }
2441
2442 // Invoke callback
2443 res.v_type = VAR_NUMBER;
2444 popup_close_and_callback(wp, &res);
2445}
2446
2447/*
2448 * popup_dialog({text}, {options})
2449 */
2450 void
2451f_popup_dialog(typval_T *argvars, typval_T *rettv)
2452{
2453 popup_create(argvars, rettv, TYPE_DIALOG);
2454}
2455
2456/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002457 * popup_menu({text}, {options})
2458 */
2459 void
2460f_popup_menu(typval_T *argvars, typval_T *rettv)
2461{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002462 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002463}
2464
2465/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002466 * popup_notification({text}, {options})
2467 */
2468 void
2469f_popup_notification(typval_T *argvars, typval_T *rettv)
2470{
2471 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2472}
2473
2474/*
2475 * Find the popup window with window-ID "id".
2476 * If the popup window does not exist NULL is returned.
2477 * If the window is not a popup window, and error message is given.
2478 */
2479 static win_T *
2480find_popup_win(int id)
2481{
2482 win_T *wp = win_id2wp(id);
2483
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002484 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002485 {
2486 semsg(_("E993: window %d is not a popup window"), id);
2487 return NULL;
2488 }
2489 return wp;
2490}
2491
2492/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002493 * popup_close({id})
2494 */
2495 void
2496f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2497{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002498 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002499 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002500
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002501 if (
2502# ifdef FEAT_TERMINAL
2503 // if the popup contains a terminal it will become hidden
2504 curbuf->b_term == NULL &&
2505# endif
2506 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002507 return;
2508
2509 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002510 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002511 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002512}
2513
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002514 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002515popup_hide(win_T *wp)
2516{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002517#ifdef FEAT_TERMINAL
2518 if (error_if_term_popup_window())
2519 return;
2520#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002521 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2522 {
2523 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002524 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002525 redraw_all_later(NOT_VALID);
2526 popup_mask_refresh = TRUE;
2527 }
2528}
2529
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002530/*
2531 * popup_hide({id})
2532 */
2533 void
2534f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2535{
2536 int id = (int)tv_get_number(argvars);
2537 win_T *wp = find_popup_win(id);
2538
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002539 if (wp != NULL)
2540 popup_hide(wp);
2541}
2542
2543 void
2544popup_show(win_T *wp)
2545{
2546 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002547 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002548 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002549 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002550 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002551 }
2552}
2553
2554/*
2555 * popup_show({id})
2556 */
2557 void
2558f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2559{
2560 int id = (int)tv_get_number(argvars);
2561 win_T *wp = find_popup_win(id);
2562
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002563 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002564 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002565 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002566#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002567 if (wp->w_popup_flags & POPF_INFO)
2568 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002569#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002570 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002571}
2572
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002573/*
2574 * popup_settext({id}, {text})
2575 */
2576 void
2577f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2578{
2579 int id = (int)tv_get_number(&argvars[0]);
2580 win_T *wp = find_popup_win(id);
2581
2582 if (wp != NULL)
2583 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002584 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2585 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2586 else
2587 {
2588 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002589 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002590 popup_adjust_position(wp);
2591 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002592 }
2593}
2594
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002595 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002596popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002597{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002598 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002599 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002600 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002601 clear_cmdline = TRUE;
2602 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002603
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002604 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002605 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002606}
2607
Bram Moolenaar82106932020-03-13 14:34:38 +01002608 static void
2609error_for_popup_window(void)
2610{
2611 emsg(_("E994: Not allowed in a popup window"));
2612}
2613
2614 int
2615error_if_popup_window(int also_with_term UNUSED)
2616{
2617 // win_execute() may set "curwin" to a popup window temporarily, but many
2618 // commands are disallowed then. When a terminal runs in the popup most
2619 // things are allowed. When a terminal is finished it can be closed.
2620 if (WIN_IS_POPUP(curwin)
2621# ifdef FEAT_TERMINAL
2622 && (also_with_term || curbuf->b_term == NULL)
2623 && !term_is_finished(curbuf)
2624# endif
2625 )
2626 {
2627 error_for_popup_window();
2628 return TRUE;
2629 }
2630 return FALSE;
2631}
2632
Bram Moolenaarec583842019-05-26 14:11:23 +02002633/*
2634 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002635 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002636 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002637 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002638 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002639popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002640{
2641 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002642 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002643 win_T *prev = NULL;
2644
Bram Moolenaarec583842019-05-26 14:11:23 +02002645 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002646 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002647 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002648 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002649 if (wp == curwin)
2650 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002651 if (!force)
2652 {
2653 error_for_popup_window();
2654 return FAIL;
2655 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002656 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002657 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002658 if (prev == NULL)
2659 first_popupwin = wp->w_next;
2660 else
2661 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002662 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002663 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002664 }
2665
Bram Moolenaarec583842019-05-26 14:11:23 +02002666 // go through tab-local popups
2667 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002668 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002669 return OK;
2670 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002671}
2672
2673/*
2674 * Close a popup window with Window-id "id" in tabpage "tp".
2675 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002676 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002677popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002678{
2679 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002680 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002681 win_T *prev = NULL;
2682
Bram Moolenaarec583842019-05-26 14:11:23 +02002683 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2684 if (wp->w_id == id)
2685 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002686 if (wp == curwin)
2687 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002688 if (!force)
2689 {
2690 error_for_popup_window();
2691 return FAIL;
2692 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002693 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002694 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002695 if (prev == NULL)
2696 *root = wp->w_next;
2697 else
2698 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002699 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002700 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002701 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002702 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002703}
2704
2705 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002706close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002707{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002708 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002709 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002710 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002711 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002712 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002713 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002714 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002715 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002716}
2717
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002718/*
2719 * popup_move({id}, {options})
2720 */
2721 void
2722f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2723{
Bram Moolenaarae943152019-06-16 22:54:14 +02002724 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002725 int id = (int)tv_get_number(argvars);
2726 win_T *wp = find_popup_win(id);
2727
2728 if (wp == NULL)
2729 return; // invalid {id}
2730
2731 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2732 {
2733 emsg(_(e_dictreq));
2734 return;
2735 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002736 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002737
Bram Moolenaarae943152019-06-16 22:54:14 +02002738 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002739
2740 if (wp->w_winrow + wp->w_height >= cmdline_row)
2741 clear_cmdline = TRUE;
2742 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002743}
2744
Bram Moolenaarbc133542019-05-29 20:26:46 +02002745/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002746 * popup_setoptions({id}, {options})
2747 */
2748 void
2749f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2750{
2751 dict_T *dict;
2752 int id = (int)tv_get_number(argvars);
2753 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002754 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002755
2756 if (wp == NULL)
2757 return; // invalid {id}
2758
2759 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2760 {
2761 emsg(_(e_dictreq));
2762 return;
2763 }
2764 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002765 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002766
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002767 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002768
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002769 if (old_firstline != wp->w_firstline)
2770 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002771 popup_adjust_position(wp);
2772}
2773
2774/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002775 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002776 */
2777 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002778f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002779{
2780 dict_T *dict;
2781 int id = (int)tv_get_number(argvars);
2782 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002783 int top_extra;
2784 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002785
2786 if (rettv_dict_alloc(rettv) == OK)
2787 {
2788 if (wp == NULL)
2789 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002790 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002791 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2792
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002793 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002794 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002795 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002796
Bram Moolenaarbc133542019-05-29 20:26:46 +02002797 dict_add_number(dict, "line", wp->w_winrow + 1);
2798 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002799 dict_add_number(dict, "width", wp->w_width + left_extra
2800 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2801 dict_add_number(dict, "height", wp->w_height + top_extra
2802 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002803
2804 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2805 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2806 dict_add_number(dict, "core_width", wp->w_width);
2807 dict_add_number(dict, "core_height", wp->w_height);
2808
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002809 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002810 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002811 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002812 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002813 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002814
2815 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002816 }
2817}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002818
2819/*
2820 * popup_list()
2821 */
2822 void
2823f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2824{
2825 win_T *wp;
2826 tabpage_T *tp;
2827
2828 if (rettv_list_alloc(rettv) != OK)
2829 return;
2830 FOR_ALL_POPUPWINS(wp)
2831 list_append_number(rettv->vval.v_list, wp->w_id);
2832 FOR_ALL_TABPAGES(tp)
2833 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2834 list_append_number(rettv->vval.v_list, wp->w_id);
2835}
2836
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002837/*
2838 * popup_locate({row}, {col})
2839 */
2840 void
2841f_popup_locate(typval_T *argvars, typval_T *rettv)
2842{
2843 int row = tv_get_number(&argvars[0]) - 1;
2844 int col = tv_get_number(&argvars[1]) - 1;
2845 win_T *wp;
2846
2847 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002848 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002849 rettv->vval.v_number = wp->w_id;
2850}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002851
2852/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002853 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2854 */
2855 static void
2856get_padding_border(dict_T *dict, int *array, char *name)
2857{
2858 list_T *list;
2859 int i;
2860
2861 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2862 return;
2863
2864 list = list_alloc();
2865 if (list != NULL)
2866 {
2867 dict_add_list(dict, name, list);
2868 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2869 for (i = 0; i < 4; ++i)
2870 list_append_number(list, array[i]);
2871 }
2872}
2873
2874/*
2875 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2876 */
2877 static void
2878get_borderhighlight(dict_T *dict, win_T *wp)
2879{
2880 list_T *list;
2881 int i;
2882
2883 for (i = 0; i < 4; ++i)
2884 if (wp->w_border_highlight[i] != NULL)
2885 break;
2886 if (i == 4)
2887 return;
2888
2889 list = list_alloc();
2890 if (list != NULL)
2891 {
2892 dict_add_list(dict, "borderhighlight", list);
2893 for (i = 0; i < 4; ++i)
2894 list_append_string(list, wp->w_border_highlight[i], -1);
2895 }
2896}
2897
2898/*
2899 * For popup_getoptions(): add a "borderchars" entry to "dict".
2900 */
2901 static void
2902get_borderchars(dict_T *dict, win_T *wp)
2903{
2904 list_T *list;
2905 int i;
2906 char_u buf[NUMBUFLEN];
2907 int len;
2908
2909 for (i = 0; i < 8; ++i)
2910 if (wp->w_border_char[i] != 0)
2911 break;
2912 if (i == 8)
2913 return;
2914
2915 list = list_alloc();
2916 if (list != NULL)
2917 {
2918 dict_add_list(dict, "borderchars", list);
2919 for (i = 0; i < 8; ++i)
2920 {
2921 len = mb_char2bytes(wp->w_border_char[i], buf);
2922 list_append_string(list, buf, len);
2923 }
2924 }
2925}
2926
2927/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002928 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002929 */
2930 static void
2931get_moved_list(dict_T *dict, win_T *wp)
2932{
2933 list_T *list;
2934
2935 list = list_alloc();
2936 if (list != NULL)
2937 {
2938 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002939 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002940 list_append_number(list, wp->w_popup_mincol);
2941 list_append_number(list, wp->w_popup_maxcol);
2942 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002943 list = list_alloc();
2944 if (list != NULL)
2945 {
2946 dict_add_list(dict, "mousemoved", list);
2947 list_append_number(list, wp->w_popup_mouse_row);
2948 list_append_number(list, wp->w_popup_mouse_mincol);
2949 list_append_number(list, wp->w_popup_mouse_maxcol);
2950 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002951}
2952
2953/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002954 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002955 */
2956 void
2957f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2958{
2959 dict_T *dict;
2960 int id = (int)tv_get_number(argvars);
2961 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002962 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002963 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002964
2965 if (rettv_dict_alloc(rettv) == OK)
2966 {
2967 if (wp == NULL)
2968 return;
2969
2970 dict = rettv->vval.v_dict;
2971 dict_add_number(dict, "line", wp->w_wantline);
2972 dict_add_number(dict, "col", wp->w_wantcol);
2973 dict_add_number(dict, "minwidth", wp->w_minwidth);
2974 dict_add_number(dict, "minheight", wp->w_minheight);
2975 dict_add_number(dict, "maxheight", wp->w_maxheight);
2976 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002977 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002978 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002979 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002980 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002981 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2982 {
2983 proptype_T *pt = text_prop_type_by_id(
2984 wp->w_popup_prop_win->w_buffer,
2985 wp->w_popup_prop_type);
2986
2987 if (pt != NULL)
2988 dict_add_string(dict, "textprop", pt->pt_name);
2989 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2990 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2991 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002992 dict_add_string(dict, "title", wp->w_popup_title);
2993 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002994 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002995 dict_add_number(dict, "mapping",
2996 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002997 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002998 dict_add_number(dict, "posinvert",
2999 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003000 dict_add_number(dict, "cursorline",
3001 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003002 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003003 if (wp->w_scrollbar_highlight != NULL)
3004 dict_add_string(dict, "scrollbarhighlight",
3005 wp->w_scrollbar_highlight);
3006 if (wp->w_thumb_highlight != NULL)
3007 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003008
Bram Moolenaara3fce622019-06-20 02:31:49 +02003009 // find the tabpage that holds this popup
3010 i = 1;
3011 FOR_ALL_TABPAGES(tp)
3012 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003013 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003014
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003015 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3016 if (twp->w_id == id)
3017 break;
3018 if (twp != NULL)
3019 break;
3020 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003021 }
3022 if (tp == NULL)
3023 i = -1; // must be global
3024 else if (tp == curtab)
3025 i = 0;
3026 dict_add_number(dict, "tabpage", i);
3027
Bram Moolenaarae943152019-06-16 22:54:14 +02003028 get_padding_border(dict, wp->w_popup_padding, "padding");
3029 get_padding_border(dict, wp->w_popup_border, "border");
3030 get_borderhighlight(dict, wp);
3031 get_borderchars(dict, wp);
3032 get_moved_list(dict, wp);
3033
3034 if (wp->w_filter_cb.cb_name != NULL)
3035 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3036 if (wp->w_close_cb.cb_name != NULL)
3037 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003038
3039 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
3040 ++i)
3041 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3042 {
3043 dict_add_string(dict, "pos",
3044 (char_u *)poppos_entries[i].pp_name);
3045 break;
3046 }
3047
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003048 dict_add_string(dict, "close", (char_u *)(
3049 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3050 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3051
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003052# if defined(FEAT_TIMERS)
3053 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3054 ? (long)wp->w_popup_timer->tr_interval : 0L);
3055# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003056 }
3057}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003058
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003059# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003060/*
3061 * Return TRUE if the current window is running a terminal in a popup window.
3062 * Return FALSE when the job has ended.
3063 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003064 int
3065error_if_term_popup_window()
3066{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003067 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3068 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003069 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01003070 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003071 return TRUE;
3072 }
3073 return FALSE;
3074}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003075# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003076
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003077/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003078 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003079 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003080 * Each calling function should use a different flag, see the list at
3081 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003082 */
3083 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003084popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003085{
3086 win_T *wp;
3087
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003088 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003089 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003090 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003091 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003092}
3093
3094/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003095 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003096 * Must have called popup_reset_handled() first.
3097 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3098 * popup with the highest zindex.
3099 */
3100 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003101find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003102{
3103 win_T *wp;
3104 win_T *found_wp;
3105 int found_zindex;
3106
3107 found_zindex = lowest ? INT_MAX : 0;
3108 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003109 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003110 if ((wp->w_popup_handled & handled_flag) == 0
3111 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003112 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003113 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003114 {
3115 found_zindex = wp->w_zindex;
3116 found_wp = wp;
3117 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003118 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003119 if ((wp->w_popup_handled & handled_flag) == 0
3120 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003121 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003122 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003123 {
3124 found_zindex = wp->w_zindex;
3125 found_wp = wp;
3126 }
3127
3128 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003129 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003130 return found_wp;
3131}
3132
3133/*
3134 * Invoke the filter callback for window "wp" with typed character "c".
3135 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003136 * Returns the return value of the filter or -1 for CTRL-C in the current
3137 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003138 * Careful: The filter may make "wp" invalid!
3139 */
3140 static int
3141invoke_popup_filter(win_T *wp, int c)
3142{
3143 int res;
3144 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003145 typval_T argv[3];
3146 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003147 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003148 int prev_called_emsg = called_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003149
Bram Moolenaara42d9452019-06-15 21:46:30 +02003150 // Emergency exit: CTRL-C closes the popup.
3151 if (c == Ctrl_C)
3152 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003153 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003154 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003155
3156 // Reset got_int to avoid the callback isn't called.
3157 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003158 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003159 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003160
3161 // If the popup is the current window it probably fails to close. Then
3162 // do not consume the key.
3163 if (was_curwin && wp == curwin)
3164 return -1;
3165 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003166 }
3167
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003168 argv[0].v_type = VAR_NUMBER;
3169 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3170
3171 // Convert the number to a string, so that the function can use:
3172 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003173 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003174 argv[1].v_type = VAR_STRING;
3175 argv[1].vval.v_string = vim_strsave(buf);
3176
3177 argv[2].v_type = VAR_UNKNOWN;
3178
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003179 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003180 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3181 {
3182 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003183 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003184 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003185 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003186 }
3187 else
3188 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003189 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3190 popup_highlight_curline(wp);
3191
3192 // If an error was given always return FALSE, so that keys are not
3193 // consumed and the user can type something.
3194 // If we get three errors in a row then close the popup. Decrement the
3195 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3196 // 10 calls to cause an error.
3197 if (win_valid_popup(wp) && called_emsg > prev_called_emsg)
3198 {
3199 wp->w_filter_errors += 10;
3200 if (wp->w_filter_errors >= 30)
3201 popup_close_with_retval(wp, -1);
3202 res = FALSE;
3203 }
3204 else
3205 {
3206 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3207 --wp->w_filter_errors;
3208 res = tv_get_bool(&rettv);
3209 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003210 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003211
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003212 vim_free(argv[1].vval.v_string);
3213 clear_tv(&rettv);
3214 return res;
3215}
3216
3217/*
3218 * Called when "c" was typed: invoke popup filter callbacks.
3219 * Returns TRUE when the character was consumed,
3220 */
3221 int
3222popup_do_filter(int c)
3223{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003224 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003225 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003226 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003227 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003228 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003229 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003230
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003231#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003232 // Popup window with terminal always gets focus.
3233 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3234 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003235#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003236
Bram Moolenaar934470e2019-09-01 23:27:05 +02003237 if (recursive)
3238 return FALSE;
3239 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003240
Bram Moolenaarf6396232019-08-24 19:36:00 +02003241 if (c == K_LEFTMOUSE)
3242 {
3243 int row = mouse_row;
3244 int col = mouse_col;
3245
3246 wp = mouse_find_win(&row, &col, FIND_POPUP);
3247 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003248 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003249 }
3250
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003251 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003252 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003253 while (res == FALSE
3254 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003255 if (wp->w_filter_cb.cb_name != NULL
3256 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003257 res = invoke_popup_filter(wp, c);
3258
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003259 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003260 {
3261 int save_got_int = got_int;
3262
3263 // Reset got_int to avoid a function used in the statusline aborts.
3264 got_int = FALSE;
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003265 redraw_after_callback(FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003266 got_int |= save_got_int;
3267 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003268 recursive = FALSE;
3269 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003270
3271 // When interrupted return FALSE to avoid looping.
3272 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003273}
3274
Bram Moolenaar3397f742019-06-02 18:40:06 +02003275/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003276 * Return TRUE if there is a popup visible with a filter callback and the
3277 * "mapping" property off.
3278 */
3279 int
3280popup_no_mapping(void)
3281{
3282 int round;
3283 win_T *wp;
3284
3285 for (round = 1; round <= 2; ++round)
3286 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3287 wp != NULL; wp = wp->w_next)
3288 if (wp->w_filter_cb.cb_name != NULL
3289 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3290 return TRUE;
3291 return FALSE;
3292}
3293
3294/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003295 * Called when the cursor moved: check if any popup needs to be closed if the
3296 * cursor moved far enough.
3297 */
3298 void
3299popup_check_cursor_pos()
3300{
3301 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003302
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003303 popup_reset_handled(POPUP_HANDLED_3);
3304 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003305 if (wp->w_popup_curwin != NULL
3306 && (curwin != wp->w_popup_curwin
3307 || curwin->w_cursor.lnum != wp->w_popup_lnum
3308 || curwin->w_cursor.col < wp->w_popup_mincol
3309 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003310 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003311}
3312
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003313/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003314 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003315 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003316 static void
3317popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003318{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003319 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003320 char_u *cells;
3321 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003322
3323 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003324 return;
3325 if (wp->w_popup_mask_cells != NULL
3326 && wp->w_popup_mask_height == height
3327 && wp->w_popup_mask_width == width)
3328 return; // cache is still valid
3329
3330 vim_free(wp->w_popup_mask_cells);
3331 wp->w_popup_mask_cells = alloc_clear(width * height);
3332 if (wp->w_popup_mask_cells == NULL)
3333 return;
3334 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003335
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003336 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003337 {
3338 int cols, cole;
3339 int lines, linee;
3340
3341 li = lio->li_tv.vval.v_list->lv_first;
3342 cols = tv_get_number(&li->li_tv);
3343 if (cols < 0)
3344 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003345 li = li->li_next;
3346 cole = tv_get_number(&li->li_tv);
3347 if (cole < 0)
3348 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003349 li = li->li_next;
3350 lines = tv_get_number(&li->li_tv);
3351 if (lines < 0)
3352 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003353 li = li->li_next;
3354 linee = tv_get_number(&li->li_tv);
3355 if (linee < 0)
3356 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003357
3358 for (row = lines - 1; row < linee && row < height; ++row)
3359 for (col = cols - 1; col < cole && col < width; ++col)
3360 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003361 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003362}
3363
3364/*
3365 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3366 * "col" and "line" are screen coordinates.
3367 */
3368 static int
3369popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3370{
3371 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3372 int line = screenline - wp->w_winrow;
3373
3374 return col >= 0 && col < width
3375 && line >= 0 && line < height
3376 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003377}
3378
3379/*
3380 * Set flags in popup_transparent[] for window "wp" to "val".
3381 */
3382 static void
3383update_popup_transparent(win_T *wp, int val)
3384{
3385 if (wp->w_popup_mask != NULL)
3386 {
3387 int width = popup_width(wp);
3388 int height = popup_height(wp);
3389 listitem_T *lio, *li;
3390 int cols, cole;
3391 int lines, linee;
3392 int col, line;
3393
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003394 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003395 {
3396 li = lio->li_tv.vval.v_list->lv_first;
3397 cols = tv_get_number(&li->li_tv);
3398 if (cols < 0)
3399 cols = width + cols + 1;
3400 li = li->li_next;
3401 cole = tv_get_number(&li->li_tv);
3402 if (cole < 0)
3403 cole = width + cole + 1;
3404 li = li->li_next;
3405 lines = tv_get_number(&li->li_tv);
3406 if (lines < 0)
3407 lines = height + lines + 1;
3408 li = li->li_next;
3409 linee = tv_get_number(&li->li_tv);
3410 if (linee < 0)
3411 linee = height + linee + 1;
3412
3413 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003414 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003415 if (cols < 0)
3416 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003417 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003418 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003419 if (lines < 0)
3420 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003421 for (line = lines; line < linee
3422 && line + wp->w_winrow < screen_Rows; ++line)
3423 for (col = cols; col < cole
3424 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003425 popup_transparent[(line + wp->w_winrow) * screen_Columns
3426 + col + wp->w_wincol] = val;
3427 }
3428 }
3429}
3430
3431/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003432 * Only called when popup window "wp" is hidden: If the window is positioned
3433 * next to a text property, and it is now visible, then unhide the popup.
3434 * We don't check if visible popups become hidden, that is done in
3435 * popup_adjust_position().
3436 * Return TRUE if the popup became unhidden.
3437 */
3438 static int
3439check_popup_unhidden(win_T *wp)
3440{
3441 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3442 {
3443 textprop_T prop;
3444 linenr_T lnum;
3445
3446 if (find_visible_prop(wp->w_popup_prop_win,
3447 wp->w_popup_prop_type, wp->w_popup_prop_id,
3448 &prop, &lnum) == OK)
3449 {
3450 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003451 wp->w_popup_prop_topline = 0; // force repositioning
3452 return TRUE;
3453 }
3454 }
3455 return FALSE;
3456}
3457
3458/*
3459 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3460 * That is when the buffer in the popup was changed, or the popup is following
3461 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003462 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003463 */
3464 static int
3465popup_need_position_adjust(win_T *wp)
3466{
3467 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3468 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003469 if (win_valid(wp->w_popup_prop_win)
3470 && (wp->w_popup_prop_changedtick
3471 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3472 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3473 return TRUE;
3474
3475 // May need to adjust the width if the cursor moved.
3476 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003477}
3478
3479/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003480 * Update "popup_mask" if needed.
3481 * Also recomputes the popup size and positions.
3482 * Also updates "popup_visible".
3483 * Also marks window lines for redrawing.
3484 */
3485 void
3486may_update_popup_mask(int type)
3487{
3488 win_T *wp;
3489 short *mask;
3490 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003491 int redraw_all_popups = FALSE;
3492 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003493
3494 // Need to recompute when switching tabs.
3495 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3496 // (such as the screen size) must have changed.
3497 if (popup_mask_tab != curtab || type >= NOT_VALID)
3498 {
3499 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003500 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003501 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003502
3503 // Check if any popup window buffer has changed and if any popup connected
3504 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003505 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003506 if (wp->w_popup_flags & POPF_HIDDEN)
3507 popup_mask_refresh |= check_popup_unhidden(wp);
3508 else if (popup_need_position_adjust(wp))
3509 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003510 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003511 if (wp->w_popup_flags & POPF_HIDDEN)
3512 popup_mask_refresh |= check_popup_unhidden(wp);
3513 else if (popup_need_position_adjust(wp))
3514 popup_mask_refresh = TRUE;
3515
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003516 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003517 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003518
3519 // Need to update the mask, something has changed.
3520 popup_mask_refresh = FALSE;
3521 popup_mask_tab = curtab;
3522 popup_visible = FALSE;
3523
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003524 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003525 // If redrawing only what is needed, update "popup_mask_next" and then
3526 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003527 redrawing_all_win = TRUE;
3528 FOR_ALL_WINDOWS(wp)
3529 if (wp->w_redr_type < SOME_VALID)
3530 redrawing_all_win = FALSE;
3531 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003532 mask = popup_mask;
3533 else
3534 mask = popup_mask_next;
3535 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3536
3537 // Find the window with the lowest zindex that hasn't been handled yet,
3538 // so that the window with a higher zindex overwrites the value in
3539 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003540 popup_reset_handled(POPUP_HANDLED_4);
3541 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003542 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003543 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003544 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003545
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003546 popup_visible = TRUE;
3547
Bram Moolenaar12034e22019-08-25 22:25:02 +02003548 // Recompute the position if the text changed. It may make the popup
3549 // hidden if it's attach to a text property that is no longer visible.
3550 if (redraw_all_popups || popup_need_position_adjust(wp))
3551 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003552 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003553 if (wp->w_popup_flags & POPF_HIDDEN)
3554 continue;
3555 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003556
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003557 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003558 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003559 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003560 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003561 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003562 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003563 col < wp->w_wincol + width - wp->w_popup_leftoff
3564 && col < screen_Columns; ++col)
3565 if (wp->w_popup_mask_cells == NULL
3566 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003567 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003568 }
3569
3570 // Only check which lines are to be updated if not already
3571 // updating all lines.
3572 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003573 {
3574 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3575 win_T *prev_wp = NULL;
3576
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003577 for (line = 0; line < screen_Rows; ++line)
3578 {
3579 int col_done = 0;
3580
3581 for (col = 0; col < screen_Columns; ++col)
3582 {
3583 int off = line * screen_Columns + col;
3584
3585 if (popup_mask[off] != popup_mask_next[off])
3586 {
3587 popup_mask[off] = popup_mask_next[off];
3588
3589 if (line >= cmdline_row)
3590 {
3591 // the command line needs to be cleared if text below
3592 // the popup is now visible.
3593 if (!msg_scrolled && popup_mask_next[off] == 0)
3594 clear_cmdline = TRUE;
3595 }
3596 else if (col >= col_done)
3597 {
3598 linenr_T lnum;
3599 int line_cp = line;
3600 int col_cp = col;
3601
3602 // The screen position "line" / "col" needs to be
3603 // redrawn. Figure out what window that is and update
3604 // w_redraw_top and w_redr_bot. Only needs to be done
3605 // once for each window line.
3606 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3607 if (wp != NULL)
3608 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003609#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003610 // A terminal window needs to be redrawn.
3611 if (bt_terminal(wp->w_buffer))
3612 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003613 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003614#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003615 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003616 if (wp != prev_wp)
3617 {
3618 vim_memset(plines_cache, 0,
3619 sizeof(int) * Rows);
3620 prev_wp = wp;
3621 }
3622
3623 if (line_cp >= wp->w_height)
3624 // In (or below) status line
3625 wp->w_redr_status = TRUE;
3626 else
3627 {
3628 // compute the position in the buffer line
3629 // from the position in the window
3630 mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003631 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003632 redrawWinline(wp, lnum);
3633 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003634 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003635
3636 // This line is going to be redrawn, no need to
3637 // check until the right side of the window.
3638 col_done = wp->w_wincol + wp->w_width - 1;
3639 }
3640 }
3641 }
3642 }
3643 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003644
3645 vim_free(plines_cache);
3646 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003647}
3648
3649/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003650 * If the current window is a popup and something relevant changed, recompute
3651 * the position and size.
3652 */
3653 void
3654may_update_popup_position(void)
3655{
3656 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3657 popup_adjust_position(curwin);
3658}
3659
3660/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003661 * Return a string of "len" spaces in IObuff.
3662 */
3663 static char_u *
3664get_spaces(int len)
3665{
3666 vim_memset(IObuff, ' ', (size_t)len);
3667 IObuff[len] = NUL;
3668 return IObuff;
3669}
3670
3671/*
3672 * Update popup windows. They are drawn on top of normal windows.
3673 * "win_update" is called for each popup window, lowest zindex first.
3674 */
3675 void
3676update_popups(void (*win_update)(win_T *wp))
3677{
3678 win_T *wp;
3679 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003680 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003681 int total_width;
3682 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003683 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003684 int popup_attr;
3685 int border_attr[4];
3686 int border_char[8];
3687 char_u buf[MB_MAXBYTES];
3688 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003689 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003690 int padcol = 0;
3691 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003692 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003693 int sb_thumb_top = 0;
3694 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003695 int attr_scroll = 0;
3696 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003697
3698 // Find the window with the lowest zindex that hasn't been updated yet,
3699 // so that the window with a higher zindex is drawn later, thus goes on
3700 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003701 popup_reset_handled(POPUP_HANDLED_5);
3702 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003703 {
3704 // This drawing uses the zindex of the popup window, so that it's on
3705 // top of the text but doesn't draw when another popup with higher
3706 // zindex is on top of the character.
3707 screen_zindex = wp->w_zindex;
3708
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003709 // Set flags in popup_transparent[] for masked cells.
3710 update_popup_transparent(wp, 1);
3711
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003712 // adjust w_winrow and w_wincol for border and padding, since
3713 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003714 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003715 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3716 - wp->w_popup_leftoff;
3717 if (wp->w_wincol + left_extra < 0)
3718 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003719 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003720 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003721
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003722 // Draw the popup text, unless it's off screen.
3723 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003724 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003725 // May need to update the "cursorline" highlighting, which may also
3726 // change "topline"
3727 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3728 popup_highlight_curline(wp);
3729
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003730 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003731
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003732 // move the cursor into the visible lines, otherwise executing
3733 // commands with win_execute() may cause the text to jump.
3734 if (wp->w_cursor.lnum < wp->w_topline)
3735 wp->w_cursor.lnum = wp->w_topline;
3736 else if (wp->w_cursor.lnum >= wp->w_botline)
3737 wp->w_cursor.lnum = wp->w_botline - 1;
3738 }
3739
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003740 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003741 wp->w_wincol -= left_extra;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003742 // cursor position matters in terminal in job mode
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003743#ifdef FEAT_TERMINAL
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003744 if (wp != curwin || !term_in_normal_mode())
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003745#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003746 {
3747 wp->w_wrow += top_off;
3748 wp->w_wcol += left_extra;
3749 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003750
Bram Moolenaard529ba52019-07-02 23:13:53 +02003751 total_width = popup_width(wp);
3752 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003753 popup_attr = get_wcr_attr(wp);
3754
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003755 if (wp->w_winrow + total_height > cmdline_row)
3756 wp->w_popup_flags |= POPF_ON_CMDLINE;
3757 else
3758 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3759
3760
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003761 // We can only use these line drawing characters when 'encoding' is
3762 // "utf-8" and 'ambiwidth' is "single".
3763 if (enc_utf8 && *p_ambw == 's')
3764 {
3765 border_char[0] = border_char[2] = 0x2550;
3766 border_char[1] = border_char[3] = 0x2551;
3767 border_char[4] = 0x2554;
3768 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003769 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3770 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003771 border_char[7] = 0x255a;
3772 }
3773 else
3774 {
3775 border_char[0] = border_char[2] = '-';
3776 border_char[1] = border_char[3] = '|';
3777 for (i = 4; i < 8; ++i)
3778 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003779 if (wp->w_popup_flags & POPF_RESIZE)
3780 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003781 }
3782 for (i = 0; i < 8; ++i)
3783 if (wp->w_border_char[i] != 0)
3784 border_char[i] = wp->w_border_char[i];
3785
3786 for (i = 0; i < 4; ++i)
3787 {
3788 border_attr[i] = popup_attr;
3789 if (wp->w_border_highlight[i] != NULL)
3790 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3791 }
3792
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003793 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003794 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003795 if (wp->w_popup_border[0] > 0)
3796 {
3797 // top border
3798 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003799 wincol < 0 ? 0 : wincol, wincol + total_width,
3800 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003801 ? border_char[4] : border_char[0],
3802 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003803 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003804 {
3805 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3806 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003807 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003808 }
3809 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003810 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3811 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003812
Bram Moolenaard529ba52019-07-02 23:13:53 +02003813 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3814 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003815 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003816 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3817 - wp->w_has_scrollbar;
3818 if (padcol < 0)
3819 {
3820 padwidth += padcol;
3821 padcol = 0;
3822 }
3823 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003824 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003825 {
3826 // top padding
3827 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003828 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003829 ' ', ' ', popup_attr);
3830 }
3831
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003832 // Title goes on top of border or padding.
3833 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003834 {
3835 int len = (int)STRLEN(wp->w_popup_title) + 1;
3836 char_u *title = alloc(len);
3837
3838 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3839 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003840 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003841 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003842 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003843
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003844 // Compute scrollbar thumb position and size.
3845 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003846 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003847 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3848 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003849
Bram Moolenaar076d9882019-09-14 22:23:29 +02003850 sb_thumb_height = (height * height + linecount / 2) / linecount;
3851 if (wp->w_topline > 1 && sb_thumb_height == height)
3852 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003853 if (sb_thumb_height == 0)
3854 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003855 if (linecount <= wp->w_height)
3856 // it just fits, avoid divide by zero
3857 sb_thumb_top = 0;
3858 else
3859 sb_thumb_top = (wp->w_topline - 1
3860 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003861 * (wp->w_height - sb_thumb_height)
3862 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003863 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3864 sb_thumb_top = 1; // show it's scrolled
3865
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003866 if (wp->w_scrollbar_highlight != NULL)
3867 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3868 else
3869 attr_scroll = highlight_attr[HLF_PSB];
3870 if (wp->w_thumb_highlight != NULL)
3871 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3872 else
3873 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003874 }
3875
3876 for (i = wp->w_popup_border[0];
3877 i < total_height - wp->w_popup_border[2]; ++i)
3878 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003879 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003880 // left and right padding only needed next to the body
3881 int do_padding =
3882 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3883 && i < total_height - wp->w_popup_border[2]
3884 - wp->w_popup_padding[2];
3885
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003886 row = wp->w_winrow + i;
3887
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003888 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003889 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003890 {
3891 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003892 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003893 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003894 if (do_padding && wp->w_popup_padding[3] > 0)
3895 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003896 int col = wincol + wp->w_popup_border[3];
3897
Bram Moolenaard529ba52019-07-02 23:13:53 +02003898 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003899 pad_left = wp->w_popup_padding[3];
3900 if (col < 0)
3901 {
3902 pad_left += col;
3903 col = 0;
3904 }
3905 if (pad_left > 0)
3906 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3907 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003908 // scrollbar
3909 if (wp->w_has_scrollbar)
3910 {
3911 int line = i - top_off;
3912 int scroll_col = wp->w_wincol + total_width - 1
3913 - wp->w_popup_border[1];
3914
3915 if (line >= 0 && line < wp->w_height)
3916 screen_putchar(' ', row, scroll_col,
3917 line >= sb_thumb_top
3918 && line < sb_thumb_top + sb_thumb_height
3919 ? attr_thumb : attr_scroll);
3920 else
3921 screen_putchar(' ', row, scroll_col, popup_attr);
3922 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003923 // right border
3924 if (wp->w_popup_border[1] > 0)
3925 {
3926 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003927 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003928 }
3929 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003930 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003931 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003932 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003933 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3934 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003935 }
3936
3937 if (wp->w_popup_padding[2] > 0)
3938 {
3939 // bottom padding
3940 row = wp->w_winrow + wp->w_popup_border[0]
3941 + wp->w_popup_padding[0] + wp->w_height;
3942 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003943 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003944 }
3945
3946 if (wp->w_popup_border[2] > 0)
3947 {
3948 // bottom border
3949 row = wp->w_winrow + total_height - 1;
3950 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003951 wincol < 0 ? 0 : wincol,
3952 wincol + total_width,
3953 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003954 ? border_char[7] : border_char[2],
3955 border_char[2], border_attr[2]);
3956 if (wp->w_popup_border[1] > 0)
3957 {
3958 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003959 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003960 }
3961 }
3962
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003963 if (wp->w_popup_close == POPCLOSE_BUTTON)
3964 {
3965 // close button goes on top of anything at the top-right corner
3966 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003967 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003968 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3969 }
3970
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003971 update_popup_transparent(wp, 0);
3972
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003973 // Back to the normal zindex.
3974 screen_zindex = 0;
3975 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02003976
3977#if defined(FEAT_SEARCH_EXTRA)
3978 // In case win_update() called start_search_hl().
3979 end_search_hl();
3980#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003981}
3982
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003983/*
3984 * Mark references in callbacks of one popup window.
3985 */
3986 static int
3987set_ref_in_one_popup(win_T *wp, int copyID)
3988{
3989 int abort = FALSE;
3990 typval_T tv;
3991
3992 if (wp->w_close_cb.cb_partial != NULL)
3993 {
3994 tv.v_type = VAR_PARTIAL;
3995 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3996 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3997 }
3998 if (wp->w_filter_cb.cb_partial != NULL)
3999 {
4000 tv.v_type = VAR_PARTIAL;
4001 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4002 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4003 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004004 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004005 return abort;
4006}
4007
4008/*
4009 * Set reference in callbacks of popup windows.
4010 */
4011 int
4012set_ref_in_popups(int copyID)
4013{
4014 int abort = FALSE;
4015 win_T *wp;
4016 tabpage_T *tp;
4017
4018 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4019 abort = abort || set_ref_in_one_popup(wp, copyID);
4020
4021 FOR_ALL_TABPAGES(tp)
4022 {
4023 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4024 abort = abort || set_ref_in_one_popup(wp, copyID);
4025 if (abort)
4026 break;
4027 }
4028 return abort;
4029}
Bram Moolenaar79648732019-07-18 21:43:07 +02004030
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004031 int
4032popup_is_popup(win_T *wp)
4033{
4034 return wp->w_popup_flags != 0;
4035}
4036
4037#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004038/*
4039 * Find an existing popup used as the preview window, in the current tab page.
4040 * Return NULL if not found.
4041 */
4042 win_T *
4043popup_find_preview_window(void)
4044{
4045 win_T *wp;
4046
4047 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004048 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004049 if (wp->w_p_pvw)
4050 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004051 return NULL;
4052}
4053
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004054/*
4055 * Find an existing popup used as the info window, in the current tab page.
4056 * Return NULL if not found.
4057 */
4058 win_T *
4059popup_find_info_window(void)
4060{
4061 win_T *wp;
4062
4063 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004064 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004065 if (wp->w_popup_flags & POPF_INFO)
4066 return wp;
4067 return NULL;
4068}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004069#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004070
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004071 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004072f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4073{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004074#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004075 win_T *wp = popup_find_info_window();
4076
4077 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004078#else
4079 rettv->vval.v_number = 0;
4080#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004081}
4082
4083 void
4084f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004085{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004086#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004087 win_T *wp = popup_find_preview_window();
4088
4089 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004090#else
4091 rettv->vval.v_number = 0;
4092#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004093}
4094
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004095#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004096/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004097 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004098 * NOTE: this makes the popup the current window, so that the file can be
4099 * edited. However, it must not remain to be the current window, the caller
4100 * must make sure of that.
4101 */
4102 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004103popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004104{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004105 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004106
4107 if (wp == NULL)
4108 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004109 if (info)
4110 wp->w_popup_flags |= POPF_INFO;
4111 else
4112 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004113
4114 // Set the width to a reasonable value, so that w_topline can be computed.
4115 if (wp->w_minwidth > 0)
4116 wp->w_width = wp->w_minwidth;
4117 else if (wp->w_maxwidth > 0)
4118 wp->w_width = wp->w_maxwidth;
4119 else
4120 wp->w_width = curwin->w_width;
4121
4122 // Will switch to another buffer soon, dummy one can be wiped.
4123 wp->w_buffer->b_locked = FALSE;
4124
4125 win_enter(wp, FALSE);
4126 return OK;
4127}
4128
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004129/*
4130 * Close any preview popup.
4131 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004132 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004133popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004134{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004135 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004136
4137 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004138 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004139}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004140
4141/*
4142 * Hide the info popup.
4143 */
4144 void
4145popup_hide_info(void)
4146{
4147 win_T *wp = popup_find_info_window();
4148
4149 if (wp != NULL)
4150 popup_hide(wp);
4151}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004152
4153/*
4154 * Close any info popup.
4155 */
4156 void
4157popup_close_info(void)
4158{
4159 win_T *wp = popup_find_info_window();
4160
4161 if (wp != NULL)
4162 popup_close_with_retval(wp, -1);
4163}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004164#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004165
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004166/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004167 * Close any popup for a text property associated with "win".
4168 * Return TRUE if a popup was closed.
4169 */
4170 int
4171popup_win_closed(win_T *win)
4172{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004173 int round;
4174 win_T *wp;
4175 win_T *next;
4176 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004177
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004178 for (round = 1; round <= 2; ++round)
4179 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4180 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004181 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004182 next = wp->w_next;
4183 if (wp->w_popup_prop_win == win)
4184 {
4185 popup_close_with_retval(wp, -1);
4186 ret = TRUE;
4187 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004188 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004189 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004190}
4191
4192/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004193 * Set the title of the popup window to the file name.
4194 */
4195 void
4196popup_set_title(win_T *wp)
4197{
4198 if (wp->w_buffer->b_fname != NULL)
4199 {
4200 char_u dirname[MAXPATHL];
4201 size_t len;
4202
4203 mch_dirname(dirname, MAXPATHL);
4204 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4205
4206 vim_free(wp->w_popup_title);
4207 len = STRLEN(wp->w_buffer->b_fname) + 3;
4208 wp->w_popup_title = alloc((int)len);
4209 if (wp->w_popup_title != NULL)
4210 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4211 wp->w_buffer->b_fname);
4212 redraw_win_later(wp, VALID);
4213 }
4214}
4215
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004216# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004217/*
4218 * If there is a preview window, update the title.
4219 * Used after changing directory.
4220 */
4221 void
4222popup_update_preview_title(void)
4223{
4224 win_T *wp = popup_find_preview_window();
4225
4226 if (wp != NULL)
4227 popup_set_title(wp);
4228}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004229# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004230
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004231#endif // FEAT_PROP_POPUP