blob: 35c4b0af5e50e8c86452973ba3ac91c58948c75b [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 Moolenaarb885b432020-11-05 19:34:41 +0100387 if (get_lambda_tv(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200388 {
389 wp->w_popup_timer = create_timer(time, 0);
390 wp->w_popup_timer->tr_callback = get_callback(&tv);
391 clear_tv(&tv);
392 }
393}
394#endif
395
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100396 static poppos_T
397get_pos_entry(dict_T *d, int give_error)
398{
399 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
400 int nr;
401
402 if (str == NULL)
403 return POPPOS_NONE;
404
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);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100464 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100465 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 Moolenaar6c542f72020-09-27 21:16:45 +0200668 nr = dict_get_bool(dict, (char_u *)"scrollbar", -1);
669 if (nr != -1)
670 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200671
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 Moolenaar6bfc4752021-02-21 23:12:18 +0100876 nr = dict_get_bool(dict, (char_u *)"cursorline", -1);
877 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200878 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100879 if (nr != 0)
880 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200881 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100882 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200883 }
884
Bram Moolenaarae943152019-06-16 22:54:14 +0200885 di = dict_find(dict, (char_u *)"filter", -1);
886 if (di != NULL)
887 {
888 callback_T callback = get_callback(&di->di_tv);
889
890 if (callback.cb_name != NULL)
891 {
892 free_callback(&wp->w_filter_cb);
893 set_callback(&wp->w_filter_cb, &callback);
894 }
895 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200896 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
897 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200898 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200899 if (nr)
900 wp->w_popup_flags |= POPF_MAPPING;
901 else
902 wp->w_popup_flags &= ~POPF_MAPPING;
903 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200904
Bram Moolenaar581ba392019-09-03 22:08:33 +0200905 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
906 if (str != NULL)
907 {
908 if (STRCMP(str, "a") == 0)
909 wp->w_filter_mode = MODE_ALL;
910 else
911 wp->w_filter_mode = mode_str2flags(str);
912 }
913
Bram Moolenaarae943152019-06-16 22:54:14 +0200914 di = dict_find(dict, (char_u *)"callback", -1);
915 if (di != NULL)
916 {
917 callback_T callback = get_callback(&di->di_tv);
918
919 if (callback.cb_name != NULL)
920 {
921 free_callback(&wp->w_close_cb);
922 set_callback(&wp->w_close_cb, &callback);
923 }
924 }
925}
926
927/*
928 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200929 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200930 */
931 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200932apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200933{
934 int nr;
935
936 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200937
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200938 if (create)
939 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200940 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
941
Bram Moolenaarae943152019-06-16 22:54:14 +0200942 apply_general_options(wp, dict);
943
Bram Moolenaar55881332020-08-18 13:04:15 +0200944 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200945 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200946 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200947
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200948 // when "firstline" and "cursorline" are both set and the cursor would be
949 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200950 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
951 {
952 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
953 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200954 else if (wp->w_cursor.lnum < wp->w_firstline
955 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200956 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200957 wp->w_topline = wp->w_firstline;
958 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200959 }
960
Bram Moolenaar33796b32019-06-08 16:01:13 +0200961 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200962 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200963}
964
965/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200966 * Add lines to the popup from a list of strings.
967 */
968 static void
969add_popup_strings(buf_T *buf, list_T *l)
970{
971 listitem_T *li;
972 linenr_T lnum = 0;
973 char_u *p;
974
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200975 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200976 if (li->li_tv.v_type == VAR_STRING)
977 {
978 p = li->li_tv.vval.v_string;
979 ml_append_buf(buf, lnum++,
980 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
981 }
982}
983
984/*
985 * Add lines to the popup from a list of dictionaries.
986 */
987 static void
988add_popup_dicts(buf_T *buf, list_T *l)
989{
990 listitem_T *li;
991 listitem_T *pli;
992 linenr_T lnum = 0;
993 char_u *p;
994 dict_T *dict;
995
996 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200997 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200998 {
999 if (li->li_tv.v_type != VAR_DICT)
1000 {
1001 emsg(_(e_dictreq));
1002 return;
1003 }
1004 dict = li->li_tv.vval.v_dict;
1005 p = dict == NULL ? NULL
1006 : dict_get_string(dict, (char_u *)"text", FALSE);
1007 ml_append_buf(buf, lnum++,
1008 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1009 }
1010
1011 // add the text properties
1012 lnum = 1;
1013 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1014 {
1015 dictitem_T *di;
1016 list_T *plist;
1017
1018 dict = li->li_tv.vval.v_dict;
1019 di = dict_find(dict, (char_u *)"props", -1);
1020 if (di != NULL)
1021 {
1022 if (di->di_tv.v_type != VAR_LIST)
1023 {
1024 emsg(_(e_listreq));
1025 return;
1026 }
1027 plist = di->di_tv.vval.v_list;
1028 if (plist != NULL)
1029 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001030 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001031 {
1032 if (pli->li_tv.v_type != VAR_DICT)
1033 {
1034 emsg(_(e_dictreq));
1035 return;
1036 }
1037 dict = pli->li_tv.vval.v_dict;
1038 if (dict != NULL)
1039 {
1040 int col = dict_get_number(dict, (char_u *)"col");
1041
1042 prop_add_common( lnum, col, dict, buf, NULL);
1043 }
1044 }
1045 }
1046 }
1047 }
1048}
1049
1050/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001051 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1052 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001053 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001054popup_top_extra(win_T *wp)
1055{
1056 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1057
1058 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1059 return 1;
1060 return extra;
1061}
1062
1063/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001064 * Get the padding plus border at the left.
1065 */
1066 int
1067popup_left_extra(win_T *wp)
1068{
1069 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1070}
1071
1072/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001073 * Return the height of popup window "wp", including border and padding.
1074 */
1075 int
1076popup_height(win_T *wp)
1077{
1078 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001079 + popup_top_extra(wp)
1080 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001081}
1082
1083/*
1084 * Return the width of popup window "wp", including border, padding and
1085 * scrollbar.
1086 */
1087 int
1088popup_width(win_T *wp)
1089{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001090 // w_leftcol is how many columns of the core are left of the screen
1091 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001092 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001093 + popup_extra_width(wp)
1094 + wp->w_popup_rightoff;
1095}
1096
1097/*
1098 * Return the extra width of popup window "wp": border, padding and scrollbar.
1099 */
1100 int
1101popup_extra_width(win_T *wp)
1102{
1103 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1104 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1105 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001106}
1107
1108/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001109 * Adjust the position and size of the popup to fit on the screen.
1110 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001111 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001112popup_adjust_position(win_T *wp)
1113{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001114 linenr_T lnum;
1115 int wrapped = 0;
1116 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001117 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001118 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001119 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001120 int center_vert = FALSE;
1121 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001122 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001123 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001124 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1125 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1126 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1127 int extra_height = top_extra + bot_extra;
1128 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001129 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001130 int org_winrow = wp->w_winrow;
1131 int org_wincol = wp->w_wincol;
1132 int org_width = wp->w_width;
1133 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001134 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001135 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001136 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001137 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001138 int wantline = wp->w_wantline; // adjusted for textprop
1139 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001140 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001141 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001142
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001143 wp->w_winrow = 0;
1144 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001145 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001146 wp->w_popup_leftoff = 0;
1147 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001148
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001149 // May need to update the "cursorline" highlighting, which may also change
1150 // "topline"
1151 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1152 popup_highlight_curline(wp);
1153
Bram Moolenaar12034e22019-08-25 22:25:02 +02001154 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1155 {
1156 win_T *prop_win = wp->w_popup_prop_win;
1157 textprop_T prop;
1158 linenr_T prop_lnum;
1159 pos_T pos;
1160 int screen_row;
1161 int screen_scol;
1162 int screen_ccol;
1163 int screen_ecol;
1164
1165 // Popup window is positioned relative to a text property.
1166 if (find_visible_prop(prop_win,
1167 wp->w_popup_prop_type, wp->w_popup_prop_id,
1168 &prop, &prop_lnum) == FAIL)
1169 {
1170 // Text property is no longer visible, hide the popup.
1171 // Unhiding the popup is done in check_popup_unhidden().
1172 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1173 {
1174 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001175 if (win_valid(wp->w_popup_prop_win))
1176 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1177 }
1178 return;
1179 }
1180
1181 // Compute the desired position from the position of the text
1182 // property. Use "wantline" and "wantcol" as offsets.
1183 pos.lnum = prop_lnum;
1184 pos.col = prop.tp_col;
1185 if (wp->w_popup_pos == POPPOS_TOPLEFT
1186 || wp->w_popup_pos == POPPOS_BOTLEFT)
1187 pos.col += prop.tp_len - 1;
1188 textpos2screenpos(prop_win, &pos, &screen_row,
1189 &screen_scol, &screen_ccol, &screen_ecol);
1190
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001191 if (screen_scol == 0)
1192 {
1193 // position is off screen, make the width zero to hide it.
1194 wp->w_width = 0;
1195 return;
1196 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001197 if (wp->w_popup_pos == POPPOS_TOPLEFT
1198 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1199 // below the text
1200 wantline = screen_row + wantline + 1;
1201 else
1202 // above the text
1203 wantline = screen_row + wantline - 1;
1204 center_vert = FALSE;
1205 if (wp->w_popup_pos == POPPOS_TOPLEFT
1206 || wp->w_popup_pos == POPPOS_BOTLEFT)
1207 // right of the text
1208 wantcol = screen_ecol + wantcol;
1209 else
1210 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001211 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001212 use_wantcol = TRUE;
1213 }
1214 else
1215 {
1216 // If no line was specified default to vertical centering.
1217 if (wantline == 0)
1218 center_vert = TRUE;
1219 else if (wantline < 0)
1220 // If "wantline" is negative it actually means zero.
1221 wantline = 0;
1222 if (wantcol < 0)
1223 // If "wantcol" is negative it actually means zero.
1224 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001225 }
1226
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001227 if (wp->w_popup_pos == POPPOS_CENTER)
1228 {
1229 // center after computing the size
1230 center_vert = TRUE;
1231 center_hor = TRUE;
1232 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001233 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001234 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001235 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1236 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001237 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001238 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001239 if (wp->w_winrow >= Rows)
1240 wp->w_winrow = Rows - 1;
1241 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001242
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001243 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001244 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001245 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1246 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001247 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001248 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001249 // Need to see at least one character after the decoration.
1250 if (wp->w_wincol > Columns - left_extra - 1)
1251 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001252 }
1253 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001254
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001255 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001256 // When left aligned use the space available, but shift to the left when we
1257 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001258 maxspace = Columns - wp->w_wincol - left_extra;
1259 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001260 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001261 {
1262 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001263 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001264 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001265
1266 if (wp->w_p_nu || wp->w_p_rnu)
1267 margin_width = number_width(wp) + 1;
1268#ifdef FEAT_FOLDING
1269 margin_width += wp->w_p_fdc;
1270#endif
1271#ifdef FEAT_SIGNS
1272 if (signcolumn_on(wp))
1273 margin_width += 2;
1274#endif
1275 if (margin_width >= maxwidth)
1276 margin_width = maxwidth - 1;
1277
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001278 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001279 minheight = wp->w_minheight;
1280#ifdef FEAT_TERMINAL
1281 // A terminal popup initially does not have content, use a default minimal
1282 // width of 20 characters and height of 5 lines.
1283 if (wp->w_buffer->b_term != NULL)
1284 {
1285 if (minwidth == 0)
1286 minwidth = 20;
1287 if (minheight == 0)
1288 minheight = 5;
1289 }
1290#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001291
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001292 if (wp->w_maxheight > 0)
1293 maxheight = wp->w_maxheight;
1294
Bram Moolenaar8d241042019-06-12 23:40:01 +02001295 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001296 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001297 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001298 if (wp->w_topline < 1)
1299 wp->w_topline = 1;
1300 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001301 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1302
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001303 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001304 // Use a minimum width of one, so that something shows when there is no
1305 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001306 // When "firstline" is -1 then start with the last buffer line and go
1307 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001308 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001309 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001310 if (wp->w_firstline < 0)
1311 lnum = wp->w_buffer->b_ml.ml_line_count;
1312 else
1313 lnum = wp->w_topline;
1314 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001315 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001316 int len;
1317 int w_width = wp->w_width;
1318
1319 // Count Tabs for what they are worth and compute the length based on
1320 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001321 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001322 if (wp->w_width < maxwidth)
1323 wp->w_width = maxwidth;
1324 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001325 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001326 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001327
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001328 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001329 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001330 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001331 {
1332 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001333 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001334 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001335 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001336 }
1337 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001338 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001339 && allow_adjust_left
1340 && (wp->w_popup_pos == POPPOS_TOPLEFT
1341 || wp->w_popup_pos == POPPOS_BOTLEFT))
1342 {
1343 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001344 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001345
Bram Moolenaar51c31312019-06-15 22:27:23 +02001346 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001347 {
1348 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001349
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001350 len -= truncate_shift;
1351 shift_by -= truncate_shift;
1352 }
1353
1354 wp->w_wincol -= shift_by;
1355 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001356 wp->w_width = maxwidth;
1357 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001358 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001359 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001360 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001361 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1362 wp->w_width = wp->w_maxwidth;
1363 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001364
1365 if (wp->w_firstline < 0)
1366 --lnum;
1367 else
1368 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001369
1370 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001371 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001372 && (wp->w_firstline >= 0
1373 ? lnum - wp->w_topline
1374 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001375 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001376 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001377 }
1378
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001379 if (wp->w_firstline < 0)
1380 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1381
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001382 wp->w_has_scrollbar = wp->w_want_scrollbar
1383 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001384#ifdef FEAT_TERMINAL
1385 if (wp->w_buffer->b_term != NULL)
1386 // Terminal window never has a scrollbar, adjusts to window height.
1387 wp->w_has_scrollbar = FALSE;
1388#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001389 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001390 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001391 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001392 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001393 // make space for the scrollbar if needed, when lines wrap and when
1394 // applying minwidth
1395 if (maxwidth + right_extra >= maxspace
1396 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1397 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001398 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001399
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001400 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1401 {
1402 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1403
1404 if (minwidth < title_len)
1405 minwidth = title_len;
1406 }
1407
1408 if (minwidth > 0 && wp->w_width < minwidth)
1409 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001410 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001411 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001412 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001413 // some columns cut off on the right
1414 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001415 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001416 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001417 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001418 {
1419 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1420 if (wp->w_wincol < 0)
1421 wp->w_wincol = 0;
1422 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001423 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1424 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1425 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001426 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001427
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001428 // Right aligned: move to the right if needed.
1429 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001430 if (leftoff >= 0)
1431 wp->w_wincol = leftoff;
1432 else if (wp->w_popup_fixed)
1433 {
1434 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001435 // columns when displaying the window, minus left border and
1436 // padding.
1437 if (-leftoff > left_extra)
1438 wp->w_leftcol = -leftoff - left_extra;
1439 wp->w_width -= wp->w_leftcol;
1440 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001441 if (wp->w_width < 0)
1442 wp->w_width = 0;
1443 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001444 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001445
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001446 if (wp->w_p_wrap || (!wp->w_popup_fixed
1447 && (wp->w_popup_pos == POPPOS_TOPLEFT
1448 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001449 {
1450 int want_col = 0;
1451
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001452 // try to show the right border and any scrollbar
1453 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001454 if (want_col > 0 && wp->w_wincol > 0
1455 && wp->w_wincol + want_col >= Columns)
1456 {
1457 wp->w_wincol = Columns - want_col;
1458 if (wp->w_wincol < 0)
1459 wp->w_wincol = 0;
1460 }
1461 }
1462
Bram Moolenaar8d241042019-06-12 23:40:01 +02001463 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1464 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001465 if (minheight > 0 && wp->w_height < minheight)
1466 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001467 if (maxheight > 0 && wp->w_height > maxheight)
1468 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001469 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001470 if (wp->w_height > Rows - wp->w_winrow)
1471 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001472
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001473 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001474 {
1475 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1476 if (wp->w_winrow < 0)
1477 wp->w_winrow = 0;
1478 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001479 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001480 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001481 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001482 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001483 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001484 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001485 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1486 {
1487 // Bottom aligned but does not fit, and less space on the other
1488 // side or "posinvert" is off: reduce height.
1489 wp->w_winrow = 0;
1490 wp->w_height = wantline - extra_height;
1491 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001492 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001493 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001494 // Not enough space and more space on the other side: make top
1495 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001496 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001497 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001498 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001499 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001500 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1501 || wp->w_popup_pos == POPPOS_TOPLEFT)
1502 {
1503 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1504 && wantline * 2 > Rows
1505 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001506 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001507 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001508 // make bottom aligned and recompute the height
1509 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001510 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001511 if (wp->w_winrow < 0)
1512 {
1513 wp->w_height += wp->w_winrow;
1514 wp->w_winrow = 0;
1515 }
1516 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001517 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001518 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001519 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001520 adjust_height_for_top_aligned = TRUE;
1521 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001522 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001523
1524 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1525 && wp->w_winrow + wp->w_height + extra_height > Rows)
1526 {
1527 // Bottom of the popup goes below the last line, reduce the height and
1528 // add a scrollbar.
1529 wp->w_height = Rows - wp->w_winrow - extra_height;
1530#ifdef FEAT_TERMINAL
1531 if (wp->w_buffer->b_term == NULL)
1532#endif
1533 wp->w_has_scrollbar = TRUE;
1534 }
1535
1536 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001537 if (wp->w_winrow >= Rows)
1538 wp->w_winrow = Rows - 1;
1539 else if (wp->w_winrow < 0)
1540 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001541
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001542 if (wp->w_height != org_height)
1543 win_comp_scroll(wp);
1544
Bram Moolenaar17146962019-05-30 00:12:11 +02001545 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001546 if (win_valid(wp->w_popup_prop_win))
1547 {
1548 wp->w_popup_prop_changedtick =
1549 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1550 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1551 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001552
1553 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001554 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001555 if (org_winrow != wp->w_winrow
1556 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001557 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001558 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001559 || org_width != wp->w_width
1560 || org_height != wp->w_height)
1561 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001562 redraw_win_later(wp, NOT_VALID);
1563 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1564 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001565 popup_mask_refresh = TRUE;
1566 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001567}
1568
Bram Moolenaar17627312019-06-02 19:53:44 +02001569typedef enum
1570{
1571 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001572 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001573 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001574 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001575 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001576 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001577 TYPE_PREVIEW, // preview window
1578 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001579} create_type_T;
1580
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001581/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001582 * Make "buf" empty and set the contents to "text".
1583 * Used by popup_create() and popup_settext().
1584 */
1585 static void
1586popup_set_buffer_text(buf_T *buf, typval_T text)
1587{
1588 int lnum;
1589
1590 // Clear the buffer, then replace the lines.
1591 curbuf = buf;
1592 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001593 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001594 curbuf = curwin->w_buffer;
1595
1596 // Add text to the buffer.
1597 if (text.v_type == VAR_STRING)
1598 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001599 char_u *s = text.vval.v_string;
1600
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001601 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001602 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001603 }
1604 else
1605 {
1606 list_T *l = text.vval.v_list;
1607
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001608 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001609 {
1610 if (l->lv_first->li_tv.v_type == VAR_STRING)
1611 // list of strings
1612 add_popup_strings(buf, l);
1613 else
1614 // list of dictionaries
1615 add_popup_dicts(buf, l);
1616 }
1617 }
1618
1619 // delete the line that was in the empty buffer
1620 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001621 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001622 curbuf = curwin->w_buffer;
1623}
1624
1625/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001626 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1627 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001628 * Return FAIL if the parsing fails.
1629 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001630 static int
1631parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001632{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001633 char_u *p =
1634#ifdef FEAT_QUICKFIX
1635 !is_preview ? p_cpp :
1636#endif
1637 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001638
Bram Moolenaar258cef52019-08-21 17:29:29 +02001639 if (wp != NULL)
1640 wp->w_popup_flags &= ~POPF_INFO_MENU;
1641
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001642 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001643 {
1644 char_u *e, *dig;
1645 char_u *s = p;
1646 int x;
1647
1648 e = vim_strchr(p, ':');
1649 if (e == NULL || e[1] == NUL)
1650 return FAIL;
1651
1652 p = vim_strchr(e, ',');
1653 if (p == NULL)
1654 p = e + STRLEN(e);
1655 dig = e + 1;
1656 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001657
1658 if (STRNCMP(s, "height:", 7) == 0)
1659 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001660 if (dig != p)
1661 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001662 if (wp != NULL)
1663 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001664 if (is_preview)
1665 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001666 wp->w_maxheight = x;
1667 }
1668 }
1669 else if (STRNCMP(s, "width:", 6) == 0)
1670 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001671 if (dig != p)
1672 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001673 if (wp != NULL)
1674 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001675 if (is_preview)
1676 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001677 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001678 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001679 }
1680 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001681 else if (STRNCMP(s, "highlight:", 10) == 0)
1682 {
1683 if (wp != NULL)
1684 {
1685 int c = *p;
1686
1687 *p = NUL;
1688 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1689 s + 10, OPT_FREE|OPT_LOCAL, 0);
1690 *p = c;
1691 }
1692 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001693 else if (STRNCMP(s, "border:", 7) == 0)
1694 {
1695 char_u *arg = s + 7;
1696 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1697 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1698 int i;
1699
1700 if (!on && !off)
1701 return FAIL;
1702 if (wp != NULL)
1703 {
1704 for (i = 0; i < 4; ++i)
1705 wp->w_popup_border[i] = on ? 1 : 0;
1706 if (off)
1707 // only show the X for close when there is a border
1708 wp->w_popup_close = POPCLOSE_NONE;
1709 }
1710 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001711 else if (STRNCMP(s, "align:", 6) == 0)
1712 {
1713 char_u *arg = s + 6;
1714 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1715 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1716
1717 if (!menu && !item)
1718 return FAIL;
1719 if (wp != NULL && menu)
1720 wp->w_popup_flags |= POPF_INFO_MENU;
1721 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001722 else
1723 return FAIL;
1724 }
1725 return OK;
1726}
1727
1728/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001729 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1730 * is not NULL.
1731 * Return FAIL if the parsing fails.
1732 */
1733 int
1734parse_previewpopup(win_T *wp)
1735{
1736 return parse_popup_option(wp, TRUE);
1737}
1738
1739/*
1740 * Parse the 'completepopup' option and apply the values to window "wp" if it
1741 * is not NULL.
1742 * Return FAIL if the parsing fails.
1743 */
1744 int
1745parse_completepopup(win_T *wp)
1746{
1747 return parse_popup_option(wp, FALSE);
1748}
1749
1750/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001751 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001752 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001753 */
1754 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001755popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001756{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001757 poppos_T ppt = POPPOS_NONE;
1758
1759 if (d != NULL)
1760 ppt = get_pos_entry(d, FALSE);
1761
Bram Moolenaar79648732019-07-18 21:43:07 +02001762 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001763 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001764 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001765 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1766 }
1767 else
1768 {
1769 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1770 if (wp->w_wantline == 0) // cursor in first line
1771 {
1772 wp->w_wantline = 2;
1773 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1774 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1775 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001776 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001777
Bram Moolenaar79648732019-07-18 21:43:07 +02001778 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001779 if (wp->w_wantcol > Columns - width)
1780 {
1781 wp->w_wantcol = Columns - width;
1782 if (wp->w_wantcol < 1)
1783 wp->w_wantcol = 1;
1784 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001785
Bram Moolenaar79648732019-07-18 21:43:07 +02001786 popup_adjust_position(wp);
1787}
1788
1789/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001790 * Set w_wantline and w_wantcol for the a given screen position.
1791 * Caller must take care of running into the window border.
1792 */
1793 void
1794popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1795{
1796 wp->w_wantline = row;
1797 wp->w_wantcol = col;
1798 popup_adjust_position(wp);
1799}
1800
1801/*
1802 * Add a border and lef&right padding.
1803 */
1804 static void
1805add_border_left_right_padding(win_T *wp)
1806{
1807 int i;
1808
1809 for (i = 0; i < 4; ++i)
1810 {
1811 wp->w_popup_border[i] = 1;
1812 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1813 }
1814}
1815
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001816#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001817/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001818 * Return TRUE if there is any popup window with a terminal buffer.
1819 */
1820 static int
1821popup_terminal_exists(void)
1822{
1823 win_T *wp;
1824 tabpage_T *tp;
1825
1826 FOR_ALL_POPUPWINS(wp)
1827 if (wp->w_buffer->b_term != NULL)
1828 return TRUE;
1829 FOR_ALL_TABPAGES(tp)
1830 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1831 if (wp->w_buffer->b_term != NULL)
1832 return TRUE;
1833 return FALSE;
1834}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001835#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001836
1837/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001838 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001839 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001840 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001841 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001842 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001843 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001844popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001845{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001846 win_T *wp;
1847 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001848 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001849 int new_buffer;
1850 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001851 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001852 int nr;
1853 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001854
Bram Moolenaar79648732019-07-18 21:43:07 +02001855 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001856 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001857 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001858 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001859 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001860 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001861 if (buf == NULL)
1862 {
1863 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1864 return NULL;
1865 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001866#ifdef FEAT_TERMINAL
1867 if (buf->b_term != NULL && popup_terminal_exists())
1868 {
1869 emsg(_("E861: Cannot open a second popup with a terminal"));
1870 return NULL;
1871 }
1872#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001873 }
1874 else if (!(argvars[0].v_type == VAR_STRING
1875 && argvars[0].vval.v_string != NULL)
1876 && !(argvars[0].v_type == VAR_LIST
1877 && argvars[0].vval.v_list != NULL))
1878 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001879 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001880 return NULL;
1881 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001882 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001883 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001884 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001885 return NULL;
1886 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001887 d = argvars[1].vval.v_dict;
1888 }
1889
1890 if (d != NULL)
1891 {
1892 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1893 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1894 else if (type == TYPE_NOTIFICATION)
1895 tabnr = -1; // notifications are global by default
1896 else
1897 tabnr = 0;
1898 if (tabnr > 0)
1899 {
1900 tp = find_tabpage(tabnr);
1901 if (tp == NULL)
1902 {
1903 semsg(_("E997: Tabpage not found: %d"), tabnr);
1904 return NULL;
1905 }
1906 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001907 }
1908
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001909 // Create the window and buffer.
1910 wp = win_alloc_popup_win();
1911 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001912 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001913 if (rettv != NULL)
1914 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001915 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001916 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001917
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001918 if (buf != NULL)
1919 {
1920 // use existing buffer
1921 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001922 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001923 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001924 buffer_ensure_loaded(buf);
1925 }
1926 else
1927 {
1928 // create a new buffer associated with the popup
1929 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001930 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001931 if (buf == NULL)
1932 return NULL;
1933 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001934
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001935 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001936
Bram Moolenaar46451042019-08-24 15:50:46 +02001937 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001938 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001939 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001940 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001941 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001942 buf->b_p_ul = -1; // no undo
1943 buf->b_p_swf = FALSE; // no swap file
1944 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001945 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001946
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001947 // Avoid that 'buftype' is reset when this buffer is entered.
1948 buf->b_p_initialized = TRUE;
1949 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001950 wp->w_p_wrap = TRUE; // 'wrap' is default on
1951 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001952
Bram Moolenaara3fce622019-06-20 02:31:49 +02001953 if (tp != NULL)
1954 {
1955 // popup on specified tab page
1956 wp->w_next = tp->tp_first_popupwin;
1957 tp->tp_first_popupwin = wp;
1958 }
1959 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001960 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001961 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001962 wp->w_next = curtab->tp_first_popupwin;
1963 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001964 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001965 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001966 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001967 win_T *prev = first_popupwin;
1968
1969 // Global popup: add at the end, so that it gets displayed on top of
1970 // older ones with the same zindex. Matters for notifications.
1971 if (first_popupwin == NULL)
1972 first_popupwin = wp;
1973 else
1974 {
1975 while (prev->w_next != NULL)
1976 prev = prev->w_next;
1977 prev->w_next = wp;
1978 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001979 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001980
Bram Moolenaar79648732019-07-18 21:43:07 +02001981 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001982 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001983
Bram Moolenaar79648732019-07-18 21:43:07 +02001984 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001985 {
1986 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001987 }
1988 if (type == TYPE_ATCURSOR)
1989 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001990 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001991 set_moved_values(wp);
1992 set_moved_columns(wp, FIND_STRING);
1993 }
1994
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001995 if (type == TYPE_BEVAL)
1996 {
1997 wp->w_popup_pos = POPPOS_BOTLEFT;
1998
1999 // by default use the mouse position
2000 wp->w_wantline = mouse_row;
2001 if (wp->w_wantline <= 0) // mouse on first line
2002 {
2003 wp->w_wantline = 2;
2004 wp->w_popup_pos = POPPOS_TOPLEFT;
2005 }
2006 wp->w_wantcol = mouse_col + 1;
2007 set_mousemoved_values(wp);
2008 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2009 }
2010
Bram Moolenaar33796b32019-06-08 16:01:13 +02002011 // set default values
2012 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002013 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002014
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002015 if (type == TYPE_NOTIFICATION)
2016 {
2017 win_T *twp, *nextwin;
2018 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002019
2020 // Try to not overlap with another global popup. Guess we need 3
2021 // more screen lines than buffer lines.
2022 wp->w_wantline = 1;
2023 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2024 {
2025 nextwin = twp->w_next;
2026 if (twp != wp
2027 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2028 && twp->w_winrow <= wp->w_wantline - 1 + height
2029 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2030 {
2031 // move to below this popup and restart the loop to check for
2032 // overlap with other popups
2033 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2034 nextwin = first_popupwin;
2035 }
2036 }
2037 if (wp->w_wantline + height > Rows)
2038 {
2039 // can't avoid overlap, put on top in the hope that message goes
2040 // away soon.
2041 wp->w_wantline = 1;
2042 }
2043
2044 wp->w_wantcol = 10;
2045 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002046 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002047 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002048 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002049 for (i = 0; i < 4; ++i)
2050 wp->w_popup_border[i] = 1;
2051 wp->w_popup_padding[1] = 1;
2052 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002053
2054 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002055 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002056 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2057 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002058 }
2059
Bram Moolenaara730e552019-06-16 19:05:31 +02002060 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002061 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002062 wp->w_popup_pos = POPPOS_CENTER;
2063 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002064 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002065 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002066 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002067 }
2068
Bram Moolenaara730e552019-06-16 19:05:31 +02002069 if (type == TYPE_MENU)
2070 {
2071 typval_T tv;
2072 callback_T callback;
2073
2074 tv.v_type = VAR_STRING;
2075 tv.vval.v_string = (char_u *)"popup_filter_menu";
2076 callback = get_callback(&tv);
2077 if (callback.cb_name != NULL)
2078 set_callback(&wp->w_filter_cb, &callback);
2079
2080 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002081 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002082 }
2083
Bram Moolenaar79648732019-07-18 21:43:07 +02002084 if (type == TYPE_PREVIEW)
2085 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002086 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002087 wp->w_popup_close = POPCLOSE_BUTTON;
2088 for (i = 0; i < 4; ++i)
2089 wp->w_popup_border[i] = 1;
2090 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002091 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002092 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002093# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002094 if (type == TYPE_INFO)
2095 {
2096 wp->w_popup_pos = POPPOS_TOPLEFT;
2097 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2098 wp->w_popup_close = POPCLOSE_BUTTON;
2099 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002100 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002101 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002102# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002103
Bram Moolenaarae943152019-06-16 22:54:14 +02002104 for (i = 0; i < 4; ++i)
2105 VIM_CLEAR(wp->w_border_highlight[i]);
2106 for (i = 0; i < 8; ++i)
2107 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002108 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002109 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002110 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002111
Bram Moolenaar79648732019-07-18 21:43:07 +02002112 if (d != NULL)
2113 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002114 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002115
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002116#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002117 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2118 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002119#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002120
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002121 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002122
2123 wp->w_vsep_width = 0;
2124
2125 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002126 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002127
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002128#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002129 // When running a terminal in the popup it becomes the current window.
2130 if (buf->b_term != NULL)
2131 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002132#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002133
Bram Moolenaara730e552019-06-16 19:05:31 +02002134 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002135}
2136
2137/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002138 * popup_clear()
2139 */
2140 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002141f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002142{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002143 int force = FALSE;
2144
2145 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002146 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002147 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002148}
2149
2150/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002151 * popup_create({text}, {options})
2152 */
2153 void
2154f_popup_create(typval_T *argvars, typval_T *rettv)
2155{
Bram Moolenaar17627312019-06-02 19:53:44 +02002156 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002157}
2158
2159/*
2160 * popup_atcursor({text}, {options})
2161 */
2162 void
2163f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2164{
Bram Moolenaar17627312019-06-02 19:53:44 +02002165 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002166}
2167
2168/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002169 * popup_beval({text}, {options})
2170 */
2171 void
2172f_popup_beval(typval_T *argvars, typval_T *rettv)
2173{
2174 popup_create(argvars, rettv, TYPE_BEVAL);
2175}
2176
2177/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002178 * Invoke the close callback for window "wp" with value "result".
2179 * Careful: The callback may make "wp" invalid!
2180 */
2181 static void
2182invoke_popup_callback(win_T *wp, typval_T *result)
2183{
2184 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002185 typval_T argv[3];
2186
2187 argv[0].v_type = VAR_NUMBER;
2188 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2189
2190 if (result != NULL && result->v_type != VAR_UNKNOWN)
2191 copy_tv(result, &argv[1]);
2192 else
2193 {
2194 argv[1].v_type = VAR_NUMBER;
2195 argv[1].vval.v_number = 0;
2196 }
2197
2198 argv[2].v_type = VAR_UNKNOWN;
2199
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002200 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002201 if (result != NULL)
2202 clear_tv(&argv[1]);
2203 clear_tv(&rettv);
2204}
2205
2206/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002207 * Make "prevwin" the current window, unless it's equal to "wp".
2208 * Otherwise make "firstwin" the current window.
2209 */
2210 static void
2211back_to_prevwin(win_T *wp)
2212{
2213 if (win_valid(prevwin) && wp != prevwin)
2214 win_enter(prevwin, FALSE);
2215 else
2216 win_enter(firstwin, FALSE);
2217}
2218
2219/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002220 * Close popup "wp" and invoke any close callback for it.
2221 */
2222 static void
2223popup_close_and_callback(win_T *wp, typval_T *arg)
2224{
2225 int id = wp->w_id;
2226
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002227#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002228 if (wp == curwin && curbuf->b_term != NULL)
2229 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002230 win_T *owp;
2231
2232 // Closing popup window with a terminal: put focus back on the first
2233 // that works:
2234 // - another popup window with a terminal
2235 // - the previous window
2236 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002237 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002238 if (owp != curwin && owp->w_buffer->b_term != NULL)
2239 break;
2240 if (owp != NULL)
2241 win_enter(owp, FALSE);
2242 else
2243 {
2244 for (owp = curtab->tp_first_popupwin; owp != NULL;
2245 owp = owp->w_next)
2246 if (owp != curwin && owp->w_buffer->b_term != NULL)
2247 break;
2248 if (owp != NULL)
2249 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002250 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002251 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002252 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002253 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002254#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002255
Bram Moolenaar49540192019-12-11 19:34:54 +01002256 // Just in case a check higher up is missing.
2257 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002258 {
2259 // To avoid getting stuck when win_execute() does something that causes
2260 // an error, stop calling the filter callback.
2261 free_callback(&wp->w_filter_cb);
2262
Bram Moolenaar49540192019-12-11 19:34:54 +01002263 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002264 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002265
Bram Moolenaarcee52202020-03-11 14:19:58 +01002266 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002267 if (wp->w_close_cb.cb_name != NULL)
2268 // Careful: This may make "wp" invalid.
2269 invoke_popup_callback(wp, arg);
2270
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002271 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002272 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002273}
2274
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002275 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002276popup_close_with_retval(win_T *wp, int retval)
2277{
2278 typval_T res;
2279
2280 res.v_type = VAR_NUMBER;
2281 res.vval.v_number = retval;
2282 popup_close_and_callback(wp, &res);
2283}
2284
Bram Moolenaar3397f742019-06-02 18:40:06 +02002285/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002286 * Close popup "wp" because of a mouse click.
2287 */
2288 void
2289popup_close_for_mouse_click(win_T *wp)
2290{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002291 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002292}
2293
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002294 static void
2295check_mouse_moved(win_T *wp, win_T *mouse_wp)
2296{
2297 // Close the popup when all if these are true:
2298 // - the mouse is not on this popup
2299 // - "mousemoved" was used
2300 // - the mouse is no longer on the same screen row or the mouse column is
2301 // outside of the relevant text
2302 if (wp != mouse_wp
2303 && wp->w_popup_mouse_row != 0
2304 && (wp->w_popup_mouse_row != mouse_row
2305 || mouse_col < wp->w_popup_mouse_mincol
2306 || mouse_col > wp->w_popup_mouse_maxcol))
2307 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002308 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002309 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002310 }
2311}
2312
2313/*
2314 * Called when the mouse moved: may close a popup with "mousemoved".
2315 */
2316 void
2317popup_handle_mouse_moved(void)
2318{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002319 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002320 win_T *mouse_wp;
2321 int row = mouse_row;
2322 int col = mouse_col;
2323
2324 // find the window where the mouse is in
2325 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2326
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002327 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2328 {
2329 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002330 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002331 }
2332 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2333 {
2334 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002335 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002336 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002337}
2338
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002339/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002340 * In a filter: check if the typed key is a mouse event that is used for
2341 * dragging the popup.
2342 */
2343 static void
2344filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2345{
2346 int row = mouse_row;
2347 int col = mouse_col;
2348
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002349 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002350 && is_mouse_key(c)
2351 && (wp == popup_dragwin
2352 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2353 // do not consume the key, allow for dragging the popup
2354 rettv->vval.v_number = 0;
2355}
2356
Bram Moolenaara730e552019-06-16 19:05:31 +02002357/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002358 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002359 */
2360 void
2361f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2362{
2363 int id = tv_get_number(&argvars[0]);
2364 win_T *wp = win_id2wp(id);
2365 char_u *key = tv_get_string(&argvars[1]);
2366 typval_T res;
2367 int c;
2368 linenr_T old_lnum;
2369
2370 // If the popup has been closed do not consume the key.
2371 if (wp == NULL)
2372 return;
2373
2374 c = *key;
2375 if (c == K_SPECIAL && key[1] != NUL)
2376 c = TO_SPECIAL(key[1], key[2]);
2377
2378 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002379 rettv->v_type = VAR_BOOL;
2380 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002381 res.v_type = VAR_NUMBER;
2382
2383 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002384 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2385 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002386 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002387 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002388 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2389 ++wp->w_cursor.lnum;
2390 if (old_lnum != wp->w_cursor.lnum)
2391 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002392 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002393 return;
2394 }
2395
2396 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2397 {
2398 // Cancelled, invoke callback with -1
2399 res.vval.v_number = -1;
2400 popup_close_and_callback(wp, &res);
2401 return;
2402 }
2403 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2404 {
2405 // Invoke callback with current index.
2406 res.vval.v_number = wp->w_cursor.lnum;
2407 popup_close_and_callback(wp, &res);
2408 return;
2409 }
2410
2411 filter_handle_drag(wp, c, rettv);
2412}
2413
2414/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002415 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002416 */
2417 void
2418f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2419{
2420 int id = tv_get_number(&argvars[0]);
2421 win_T *wp = win_id2wp(id);
2422 char_u *key = tv_get_string(&argvars[1]);
2423 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002424 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002425
2426 // If the popup has been closed don't consume the key.
2427 if (wp == NULL)
2428 return;
2429
Bram Moolenaara730e552019-06-16 19:05:31 +02002430 c = *key;
2431 if (c == K_SPECIAL && key[1] != NUL)
2432 c = TO_SPECIAL(key[1], key[2]);
2433
Bram Moolenaara42d9452019-06-15 21:46:30 +02002434 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002435 rettv->v_type = VAR_BOOL;
2436 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002437
Bram Moolenaara730e552019-06-16 19:05:31 +02002438 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002439 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002440 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002441 res.vval.v_number = 0;
2442 else
2443 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002444 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002445 return;
2446 }
2447
2448 // Invoke callback
2449 res.v_type = VAR_NUMBER;
2450 popup_close_and_callback(wp, &res);
2451}
2452
2453/*
2454 * popup_dialog({text}, {options})
2455 */
2456 void
2457f_popup_dialog(typval_T *argvars, typval_T *rettv)
2458{
2459 popup_create(argvars, rettv, TYPE_DIALOG);
2460}
2461
2462/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002463 * popup_menu({text}, {options})
2464 */
2465 void
2466f_popup_menu(typval_T *argvars, typval_T *rettv)
2467{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002468 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002469}
2470
2471/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002472 * popup_notification({text}, {options})
2473 */
2474 void
2475f_popup_notification(typval_T *argvars, typval_T *rettv)
2476{
2477 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2478}
2479
2480/*
2481 * Find the popup window with window-ID "id".
2482 * If the popup window does not exist NULL is returned.
2483 * If the window is not a popup window, and error message is given.
2484 */
2485 static win_T *
2486find_popup_win(int id)
2487{
2488 win_T *wp = win_id2wp(id);
2489
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002490 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002491 {
2492 semsg(_("E993: window %d is not a popup window"), id);
2493 return NULL;
2494 }
2495 return wp;
2496}
2497
2498/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002499 * popup_close({id})
2500 */
2501 void
2502f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2503{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002504 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002505 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002506
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002507 if (
2508# ifdef FEAT_TERMINAL
2509 // if the popup contains a terminal it will become hidden
2510 curbuf->b_term == NULL &&
2511# endif
2512 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002513 return;
2514
2515 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002516 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002517 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002518}
2519
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002520 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002521popup_hide(win_T *wp)
2522{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002523#ifdef FEAT_TERMINAL
2524 if (error_if_term_popup_window())
2525 return;
2526#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002527 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2528 {
2529 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002530 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002531 redraw_all_later(NOT_VALID);
2532 popup_mask_refresh = TRUE;
2533 }
2534}
2535
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002536/*
2537 * popup_hide({id})
2538 */
2539 void
2540f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2541{
2542 int id = (int)tv_get_number(argvars);
2543 win_T *wp = find_popup_win(id);
2544
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002545 if (wp != NULL)
2546 popup_hide(wp);
2547}
2548
2549 void
2550popup_show(win_T *wp)
2551{
2552 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002553 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002554 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002555 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002556 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002557 }
2558}
2559
2560/*
2561 * popup_show({id})
2562 */
2563 void
2564f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2565{
2566 int id = (int)tv_get_number(argvars);
2567 win_T *wp = find_popup_win(id);
2568
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002569 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002570 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002571 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002572#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002573 if (wp->w_popup_flags & POPF_INFO)
2574 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002575#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002576 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002577}
2578
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002579/*
2580 * popup_settext({id}, {text})
2581 */
2582 void
2583f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2584{
2585 int id = (int)tv_get_number(&argvars[0]);
2586 win_T *wp = find_popup_win(id);
2587
2588 if (wp != NULL)
2589 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002590 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2591 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2592 else
2593 {
2594 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002595 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002596 popup_adjust_position(wp);
2597 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002598 }
2599}
2600
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002601 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002602popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002603{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002604 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002605 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002606 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002607 clear_cmdline = TRUE;
2608 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002609
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002610 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002611 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002612}
2613
Bram Moolenaar82106932020-03-13 14:34:38 +01002614 static void
2615error_for_popup_window(void)
2616{
2617 emsg(_("E994: Not allowed in a popup window"));
2618}
2619
2620 int
2621error_if_popup_window(int also_with_term UNUSED)
2622{
2623 // win_execute() may set "curwin" to a popup window temporarily, but many
2624 // commands are disallowed then. When a terminal runs in the popup most
2625 // things are allowed. When a terminal is finished it can be closed.
2626 if (WIN_IS_POPUP(curwin)
2627# ifdef FEAT_TERMINAL
2628 && (also_with_term || curbuf->b_term == NULL)
2629 && !term_is_finished(curbuf)
2630# endif
2631 )
2632 {
2633 error_for_popup_window();
2634 return TRUE;
2635 }
2636 return FALSE;
2637}
2638
Bram Moolenaarec583842019-05-26 14:11:23 +02002639/*
2640 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002641 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002642 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002643 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002644 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002645popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002646{
2647 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002648 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002649 win_T *prev = NULL;
2650
Bram Moolenaarec583842019-05-26 14:11:23 +02002651 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002652 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002653 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002654 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002655 if (wp == curwin)
2656 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002657 if (!force)
2658 {
2659 error_for_popup_window();
2660 return FAIL;
2661 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002662 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002663 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002664 if (prev == NULL)
2665 first_popupwin = wp->w_next;
2666 else
2667 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002668 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002669 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002670 }
2671
Bram Moolenaarec583842019-05-26 14:11:23 +02002672 // go through tab-local popups
2673 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002674 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002675 return OK;
2676 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002677}
2678
2679/*
2680 * Close a popup window with Window-id "id" in tabpage "tp".
2681 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002682 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002683popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002684{
2685 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002686 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002687 win_T *prev = NULL;
2688
Bram Moolenaarec583842019-05-26 14:11:23 +02002689 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2690 if (wp->w_id == id)
2691 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002692 if (wp == curwin)
2693 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002694 if (!force)
2695 {
2696 error_for_popup_window();
2697 return FAIL;
2698 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002699 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002700 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002701 if (prev == NULL)
2702 *root = wp->w_next;
2703 else
2704 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002705 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002706 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002707 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002708 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002709}
2710
2711 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002712close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002713{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002714 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002715 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002716 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002717 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002718 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002719 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002720 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002721 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002722}
2723
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002724/*
2725 * popup_move({id}, {options})
2726 */
2727 void
2728f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2729{
Bram Moolenaarae943152019-06-16 22:54:14 +02002730 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002731 int id = (int)tv_get_number(argvars);
2732 win_T *wp = find_popup_win(id);
2733
2734 if (wp == NULL)
2735 return; // invalid {id}
2736
2737 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2738 {
2739 emsg(_(e_dictreq));
2740 return;
2741 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002742 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002743
Bram Moolenaarae943152019-06-16 22:54:14 +02002744 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002745
2746 if (wp->w_winrow + wp->w_height >= cmdline_row)
2747 clear_cmdline = TRUE;
2748 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002749}
2750
Bram Moolenaarbc133542019-05-29 20:26:46 +02002751/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002752 * popup_setoptions({id}, {options})
2753 */
2754 void
2755f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2756{
2757 dict_T *dict;
2758 int id = (int)tv_get_number(argvars);
2759 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002760 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002761
2762 if (wp == NULL)
2763 return; // invalid {id}
2764
2765 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2766 {
2767 emsg(_(e_dictreq));
2768 return;
2769 }
2770 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002771 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002772
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002773 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002774
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002775 if (old_firstline != wp->w_firstline)
2776 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002777 popup_adjust_position(wp);
2778}
2779
2780/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002781 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002782 */
2783 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002784f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002785{
2786 dict_T *dict;
2787 int id = (int)tv_get_number(argvars);
2788 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002789 int top_extra;
2790 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002791
2792 if (rettv_dict_alloc(rettv) == OK)
2793 {
2794 if (wp == NULL)
2795 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002796 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002797 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2798
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002799 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002800 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002801 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002802
Bram Moolenaarbc133542019-05-29 20:26:46 +02002803 dict_add_number(dict, "line", wp->w_winrow + 1);
2804 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002805 dict_add_number(dict, "width", wp->w_width + left_extra
2806 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2807 dict_add_number(dict, "height", wp->w_height + top_extra
2808 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002809
2810 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2811 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2812 dict_add_number(dict, "core_width", wp->w_width);
2813 dict_add_number(dict, "core_height", wp->w_height);
2814
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002815 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002816 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002817 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002818 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002819 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002820
2821 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002822 }
2823}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002824
2825/*
2826 * popup_list()
2827 */
2828 void
2829f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2830{
2831 win_T *wp;
2832 tabpage_T *tp;
2833
2834 if (rettv_list_alloc(rettv) != OK)
2835 return;
2836 FOR_ALL_POPUPWINS(wp)
2837 list_append_number(rettv->vval.v_list, wp->w_id);
2838 FOR_ALL_TABPAGES(tp)
2839 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2840 list_append_number(rettv->vval.v_list, wp->w_id);
2841}
2842
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002843/*
2844 * popup_locate({row}, {col})
2845 */
2846 void
2847f_popup_locate(typval_T *argvars, typval_T *rettv)
2848{
2849 int row = tv_get_number(&argvars[0]) - 1;
2850 int col = tv_get_number(&argvars[1]) - 1;
2851 win_T *wp;
2852
2853 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002854 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002855 rettv->vval.v_number = wp->w_id;
2856}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002857
2858/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002859 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2860 */
2861 static void
2862get_padding_border(dict_T *dict, int *array, char *name)
2863{
2864 list_T *list;
2865 int i;
2866
2867 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2868 return;
2869
2870 list = list_alloc();
2871 if (list != NULL)
2872 {
2873 dict_add_list(dict, name, list);
2874 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2875 for (i = 0; i < 4; ++i)
2876 list_append_number(list, array[i]);
2877 }
2878}
2879
2880/*
2881 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2882 */
2883 static void
2884get_borderhighlight(dict_T *dict, win_T *wp)
2885{
2886 list_T *list;
2887 int i;
2888
2889 for (i = 0; i < 4; ++i)
2890 if (wp->w_border_highlight[i] != NULL)
2891 break;
2892 if (i == 4)
2893 return;
2894
2895 list = list_alloc();
2896 if (list != NULL)
2897 {
2898 dict_add_list(dict, "borderhighlight", list);
2899 for (i = 0; i < 4; ++i)
2900 list_append_string(list, wp->w_border_highlight[i], -1);
2901 }
2902}
2903
2904/*
2905 * For popup_getoptions(): add a "borderchars" entry to "dict".
2906 */
2907 static void
2908get_borderchars(dict_T *dict, win_T *wp)
2909{
2910 list_T *list;
2911 int i;
2912 char_u buf[NUMBUFLEN];
2913 int len;
2914
2915 for (i = 0; i < 8; ++i)
2916 if (wp->w_border_char[i] != 0)
2917 break;
2918 if (i == 8)
2919 return;
2920
2921 list = list_alloc();
2922 if (list != NULL)
2923 {
2924 dict_add_list(dict, "borderchars", list);
2925 for (i = 0; i < 8; ++i)
2926 {
2927 len = mb_char2bytes(wp->w_border_char[i], buf);
2928 list_append_string(list, buf, len);
2929 }
2930 }
2931}
2932
2933/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002934 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002935 */
2936 static void
2937get_moved_list(dict_T *dict, win_T *wp)
2938{
2939 list_T *list;
2940
2941 list = list_alloc();
2942 if (list != NULL)
2943 {
2944 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002945 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002946 list_append_number(list, wp->w_popup_mincol);
2947 list_append_number(list, wp->w_popup_maxcol);
2948 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002949 list = list_alloc();
2950 if (list != NULL)
2951 {
2952 dict_add_list(dict, "mousemoved", list);
2953 list_append_number(list, wp->w_popup_mouse_row);
2954 list_append_number(list, wp->w_popup_mouse_mincol);
2955 list_append_number(list, wp->w_popup_mouse_maxcol);
2956 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002957}
2958
2959/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002960 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002961 */
2962 void
2963f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2964{
2965 dict_T *dict;
2966 int id = (int)tv_get_number(argvars);
2967 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002968 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002969 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002970
2971 if (rettv_dict_alloc(rettv) == OK)
2972 {
2973 if (wp == NULL)
2974 return;
2975
2976 dict = rettv->vval.v_dict;
2977 dict_add_number(dict, "line", wp->w_wantline);
2978 dict_add_number(dict, "col", wp->w_wantcol);
2979 dict_add_number(dict, "minwidth", wp->w_minwidth);
2980 dict_add_number(dict, "minheight", wp->w_minheight);
2981 dict_add_number(dict, "maxheight", wp->w_maxheight);
2982 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002983 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002984 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002985 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002986 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01002987 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02002988 {
2989 proptype_T *pt = text_prop_type_by_id(
2990 wp->w_popup_prop_win->w_buffer,
2991 wp->w_popup_prop_type);
2992
2993 if (pt != NULL)
2994 dict_add_string(dict, "textprop", pt->pt_name);
2995 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2996 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2997 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002998 dict_add_string(dict, "title", wp->w_popup_title);
2999 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003000 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003001 dict_add_number(dict, "mapping",
3002 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003003 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003004 dict_add_number(dict, "posinvert",
3005 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003006 dict_add_number(dict, "cursorline",
3007 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003008 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003009 if (wp->w_scrollbar_highlight != NULL)
3010 dict_add_string(dict, "scrollbarhighlight",
3011 wp->w_scrollbar_highlight);
3012 if (wp->w_thumb_highlight != NULL)
3013 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003014
Bram Moolenaara3fce622019-06-20 02:31:49 +02003015 // find the tabpage that holds this popup
3016 i = 1;
3017 FOR_ALL_TABPAGES(tp)
3018 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003019 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003020
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003021 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3022 if (twp->w_id == id)
3023 break;
3024 if (twp != NULL)
3025 break;
3026 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003027 }
3028 if (tp == NULL)
3029 i = -1; // must be global
3030 else if (tp == curtab)
3031 i = 0;
3032 dict_add_number(dict, "tabpage", i);
3033
Bram Moolenaarae943152019-06-16 22:54:14 +02003034 get_padding_border(dict, wp->w_popup_padding, "padding");
3035 get_padding_border(dict, wp->w_popup_border, "border");
3036 get_borderhighlight(dict, wp);
3037 get_borderchars(dict, wp);
3038 get_moved_list(dict, wp);
3039
3040 if (wp->w_filter_cb.cb_name != NULL)
3041 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3042 if (wp->w_close_cb.cb_name != NULL)
3043 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003044
3045 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
3046 ++i)
3047 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3048 {
3049 dict_add_string(dict, "pos",
3050 (char_u *)poppos_entries[i].pp_name);
3051 break;
3052 }
3053
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003054 dict_add_string(dict, "close", (char_u *)(
3055 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3056 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3057
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003058# if defined(FEAT_TIMERS)
3059 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3060 ? (long)wp->w_popup_timer->tr_interval : 0L);
3061# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003062 }
3063}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003064
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003065# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003066/*
3067 * Return TRUE if the current window is running a terminal in a popup window.
3068 * Return FALSE when the job has ended.
3069 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003070 int
3071error_if_term_popup_window()
3072{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003073 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3074 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003075 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01003076 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003077 return TRUE;
3078 }
3079 return FALSE;
3080}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003081# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003082
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003083/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003084 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003085 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003086 * Each calling function should use a different flag, see the list at
3087 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003088 */
3089 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003090popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003091{
3092 win_T *wp;
3093
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003094 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003095 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003096 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003097 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003098}
3099
3100/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003101 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003102 * Must have called popup_reset_handled() first.
3103 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3104 * popup with the highest zindex.
3105 */
3106 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003107find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003108{
3109 win_T *wp;
3110 win_T *found_wp;
3111 int found_zindex;
3112
3113 found_zindex = lowest ? INT_MAX : 0;
3114 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003115 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003116 if ((wp->w_popup_handled & handled_flag) == 0
3117 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003118 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003119 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003120 {
3121 found_zindex = wp->w_zindex;
3122 found_wp = wp;
3123 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003124 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003125 if ((wp->w_popup_handled & handled_flag) == 0
3126 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003127 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003128 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003129 {
3130 found_zindex = wp->w_zindex;
3131 found_wp = wp;
3132 }
3133
3134 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003135 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003136 return found_wp;
3137}
3138
3139/*
3140 * Invoke the filter callback for window "wp" with typed character "c".
3141 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003142 * Returns the return value of the filter or -1 for CTRL-C in the current
3143 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003144 * Careful: The filter may make "wp" invalid!
3145 */
3146 static int
3147invoke_popup_filter(win_T *wp, int c)
3148{
3149 int res;
3150 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003151 typval_T argv[3];
3152 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003153 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003154 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003155
Bram Moolenaara42d9452019-06-15 21:46:30 +02003156 // Emergency exit: CTRL-C closes the popup.
3157 if (c == Ctrl_C)
3158 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003159 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003160 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003161
3162 // Reset got_int to avoid the callback isn't called.
3163 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003164 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003165 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003166
3167 // If the popup is the current window it probably fails to close. Then
3168 // do not consume the key.
3169 if (was_curwin && wp == curwin)
3170 return -1;
3171 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003172 }
3173
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003174 argv[0].v_type = VAR_NUMBER;
3175 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3176
3177 // Convert the number to a string, so that the function can use:
3178 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003179 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003180 argv[1].v_type = VAR_STRING;
3181 argv[1].vval.v_string = vim_strsave(buf);
3182
3183 argv[2].v_type = VAR_UNKNOWN;
3184
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003185 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003186 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3187 {
3188 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003189 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003190 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003191 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003192 }
3193 else
3194 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003195 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3196 popup_highlight_curline(wp);
3197
Bram Moolenaar371806e2020-10-22 13:44:54 +02003198 // If an error message was given always return FALSE, so that keys are
3199 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003200 // If we get three errors in a row then close the popup. Decrement the
3201 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3202 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003203 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003204 {
3205 wp->w_filter_errors += 10;
3206 if (wp->w_filter_errors >= 30)
3207 popup_close_with_retval(wp, -1);
3208 res = FALSE;
3209 }
3210 else
3211 {
3212 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3213 --wp->w_filter_errors;
3214 res = tv_get_bool(&rettv);
3215 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003216 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003217
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003218 vim_free(argv[1].vval.v_string);
3219 clear_tv(&rettv);
3220 return res;
3221}
3222
3223/*
3224 * Called when "c" was typed: invoke popup filter callbacks.
3225 * Returns TRUE when the character was consumed,
3226 */
3227 int
3228popup_do_filter(int c)
3229{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003230 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003231 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003232 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003233 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003234 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003235 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003236
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003237#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003238 // Popup window with terminal always gets focus.
3239 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3240 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003241#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003242
Bram Moolenaar934470e2019-09-01 23:27:05 +02003243 if (recursive)
3244 return FALSE;
3245 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003246
Bram Moolenaarf6396232019-08-24 19:36:00 +02003247 if (c == K_LEFTMOUSE)
3248 {
3249 int row = mouse_row;
3250 int col = mouse_col;
3251
3252 wp = mouse_find_win(&row, &col, FIND_POPUP);
3253 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003254 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003255 }
3256
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003257 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003258 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003259 while (res == FALSE
3260 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003261 if (wp->w_filter_cb.cb_name != NULL
3262 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003263 res = invoke_popup_filter(wp, c);
3264
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003265 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003266 {
3267 int save_got_int = got_int;
3268
3269 // Reset got_int to avoid a function used in the statusline aborts.
3270 got_int = FALSE;
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003271 redraw_after_callback(FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003272 got_int |= save_got_int;
3273 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003274 recursive = FALSE;
3275 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003276
3277 // When interrupted return FALSE to avoid looping.
3278 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003279}
3280
Bram Moolenaar3397f742019-06-02 18:40:06 +02003281/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003282 * Return TRUE if there is a popup visible with a filter callback and the
3283 * "mapping" property off.
3284 */
3285 int
3286popup_no_mapping(void)
3287{
3288 int round;
3289 win_T *wp;
3290
3291 for (round = 1; round <= 2; ++round)
3292 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3293 wp != NULL; wp = wp->w_next)
3294 if (wp->w_filter_cb.cb_name != NULL
3295 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3296 return TRUE;
3297 return FALSE;
3298}
3299
3300/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003301 * Called when the cursor moved: check if any popup needs to be closed if the
3302 * cursor moved far enough.
3303 */
3304 void
3305popup_check_cursor_pos()
3306{
3307 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003308
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003309 popup_reset_handled(POPUP_HANDLED_3);
3310 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003311 if (wp->w_popup_curwin != NULL
3312 && (curwin != wp->w_popup_curwin
3313 || curwin->w_cursor.lnum != wp->w_popup_lnum
3314 || curwin->w_cursor.col < wp->w_popup_mincol
3315 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003316 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003317}
3318
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003319/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003320 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003321 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003322 static void
3323popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003324{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003325 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003326 char_u *cells;
3327 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003328
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003329 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3330 {
3331 vim_free(wp->w_popup_mask_cells);
3332 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003333 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003334 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003335 if (wp->w_popup_mask_cells != NULL
3336 && wp->w_popup_mask_height == height
3337 && wp->w_popup_mask_width == width)
3338 return; // cache is still valid
3339
3340 vim_free(wp->w_popup_mask_cells);
3341 wp->w_popup_mask_cells = alloc_clear(width * height);
3342 if (wp->w_popup_mask_cells == NULL)
3343 return;
3344 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003345
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003346 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003347 {
3348 int cols, cole;
3349 int lines, linee;
3350
3351 li = lio->li_tv.vval.v_list->lv_first;
3352 cols = tv_get_number(&li->li_tv);
3353 if (cols < 0)
3354 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003355 if (cols <= 0)
3356 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003357 li = li->li_next;
3358 cole = tv_get_number(&li->li_tv);
3359 if (cole < 0)
3360 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003361 if (cole > width)
3362 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003363 li = li->li_next;
3364 lines = tv_get_number(&li->li_tv);
3365 if (lines < 0)
3366 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003367 if (lines <= 0)
3368 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003369 li = li->li_next;
3370 linee = tv_get_number(&li->li_tv);
3371 if (linee < 0)
3372 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003373 if (linee > height)
3374 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003375
Bram Moolenaar4012d262020-12-29 11:57:46 +01003376 for (row = lines - 1; row < linee; ++row)
3377 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003378 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003379 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003380}
3381
3382/*
3383 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3384 * "col" and "line" are screen coordinates.
3385 */
3386 static int
3387popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3388{
3389 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3390 int line = screenline - wp->w_winrow;
3391
3392 return col >= 0 && col < width
3393 && line >= 0 && line < height
3394 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003395}
3396
3397/*
3398 * Set flags in popup_transparent[] for window "wp" to "val".
3399 */
3400 static void
3401update_popup_transparent(win_T *wp, int val)
3402{
3403 if (wp->w_popup_mask != NULL)
3404 {
3405 int width = popup_width(wp);
3406 int height = popup_height(wp);
3407 listitem_T *lio, *li;
3408 int cols, cole;
3409 int lines, linee;
3410 int col, line;
3411
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003412 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003413 {
3414 li = lio->li_tv.vval.v_list->lv_first;
3415 cols = tv_get_number(&li->li_tv);
3416 if (cols < 0)
3417 cols = width + cols + 1;
3418 li = li->li_next;
3419 cole = tv_get_number(&li->li_tv);
3420 if (cole < 0)
3421 cole = width + cole + 1;
3422 li = li->li_next;
3423 lines = tv_get_number(&li->li_tv);
3424 if (lines < 0)
3425 lines = height + lines + 1;
3426 li = li->li_next;
3427 linee = tv_get_number(&li->li_tv);
3428 if (linee < 0)
3429 linee = height + linee + 1;
3430
3431 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003432 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003433 if (cols < 0)
3434 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003435 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003436 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003437 if (lines < 0)
3438 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003439 for (line = lines; line < linee
3440 && line + wp->w_winrow < screen_Rows; ++line)
3441 for (col = cols; col < cole
3442 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003443 popup_transparent[(line + wp->w_winrow) * screen_Columns
3444 + col + wp->w_wincol] = val;
3445 }
3446 }
3447}
3448
3449/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003450 * Only called when popup window "wp" is hidden: If the window is positioned
3451 * next to a text property, and it is now visible, then unhide the popup.
3452 * We don't check if visible popups become hidden, that is done in
3453 * popup_adjust_position().
3454 * Return TRUE if the popup became unhidden.
3455 */
3456 static int
3457check_popup_unhidden(win_T *wp)
3458{
3459 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3460 {
3461 textprop_T prop;
3462 linenr_T lnum;
3463
3464 if (find_visible_prop(wp->w_popup_prop_win,
3465 wp->w_popup_prop_type, wp->w_popup_prop_id,
3466 &prop, &lnum) == OK)
3467 {
3468 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003469 wp->w_popup_prop_topline = 0; // force repositioning
3470 return TRUE;
3471 }
3472 }
3473 return FALSE;
3474}
3475
3476/*
3477 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3478 * That is when the buffer in the popup was changed, or the popup is following
3479 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003480 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003481 */
3482 static int
3483popup_need_position_adjust(win_T *wp)
3484{
3485 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3486 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003487 if (win_valid(wp->w_popup_prop_win)
3488 && (wp->w_popup_prop_changedtick
3489 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3490 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3491 return TRUE;
3492
3493 // May need to adjust the width if the cursor moved.
3494 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003495}
3496
3497/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003498 * Update "popup_mask" if needed.
3499 * Also recomputes the popup size and positions.
3500 * Also updates "popup_visible".
3501 * Also marks window lines for redrawing.
3502 */
3503 void
3504may_update_popup_mask(int type)
3505{
3506 win_T *wp;
3507 short *mask;
3508 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003509 int redraw_all_popups = FALSE;
3510 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003511
3512 // Need to recompute when switching tabs.
3513 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3514 // (such as the screen size) must have changed.
3515 if (popup_mask_tab != curtab || type >= NOT_VALID)
3516 {
3517 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003518 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003519 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003520
3521 // Check if any popup window buffer has changed and if any popup connected
3522 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003523 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003524 if (wp->w_popup_flags & POPF_HIDDEN)
3525 popup_mask_refresh |= check_popup_unhidden(wp);
3526 else if (popup_need_position_adjust(wp))
3527 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003528 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003529 if (wp->w_popup_flags & POPF_HIDDEN)
3530 popup_mask_refresh |= check_popup_unhidden(wp);
3531 else if (popup_need_position_adjust(wp))
3532 popup_mask_refresh = TRUE;
3533
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003534 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003535 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003536
3537 // Need to update the mask, something has changed.
3538 popup_mask_refresh = FALSE;
3539 popup_mask_tab = curtab;
3540 popup_visible = FALSE;
3541
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003542 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003543 // If redrawing only what is needed, update "popup_mask_next" and then
3544 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003545 redrawing_all_win = TRUE;
3546 FOR_ALL_WINDOWS(wp)
3547 if (wp->w_redr_type < SOME_VALID)
3548 redrawing_all_win = FALSE;
3549 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003550 mask = popup_mask;
3551 else
3552 mask = popup_mask_next;
3553 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3554
3555 // Find the window with the lowest zindex that hasn't been handled yet,
3556 // so that the window with a higher zindex overwrites the value in
3557 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003558 popup_reset_handled(POPUP_HANDLED_4);
3559 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003560 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003561 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003562 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003563
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003564 popup_visible = TRUE;
3565
Bram Moolenaar12034e22019-08-25 22:25:02 +02003566 // Recompute the position if the text changed. It may make the popup
3567 // hidden if it's attach to a text property that is no longer visible.
3568 if (redraw_all_popups || popup_need_position_adjust(wp))
3569 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003570 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003571 if (wp->w_popup_flags & POPF_HIDDEN)
3572 continue;
3573 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003574
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003575 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003576 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003577 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003578 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003579 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003580 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003581 col < wp->w_wincol + width - wp->w_popup_leftoff
3582 && col < screen_Columns; ++col)
3583 if (wp->w_popup_mask_cells == NULL
3584 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003585 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003586 }
3587
3588 // Only check which lines are to be updated if not already
3589 // updating all lines.
3590 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003591 {
3592 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3593 win_T *prev_wp = NULL;
3594
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003595 for (line = 0; line < screen_Rows; ++line)
3596 {
3597 int col_done = 0;
3598
3599 for (col = 0; col < screen_Columns; ++col)
3600 {
3601 int off = line * screen_Columns + col;
3602
3603 if (popup_mask[off] != popup_mask_next[off])
3604 {
3605 popup_mask[off] = popup_mask_next[off];
3606
3607 if (line >= cmdline_row)
3608 {
3609 // the command line needs to be cleared if text below
3610 // the popup is now visible.
3611 if (!msg_scrolled && popup_mask_next[off] == 0)
3612 clear_cmdline = TRUE;
3613 }
3614 else if (col >= col_done)
3615 {
3616 linenr_T lnum;
3617 int line_cp = line;
3618 int col_cp = col;
3619
3620 // The screen position "line" / "col" needs to be
3621 // redrawn. Figure out what window that is and update
3622 // w_redraw_top and w_redr_bot. Only needs to be done
3623 // once for each window line.
3624 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3625 if (wp != NULL)
3626 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003627#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003628 // A terminal window needs to be redrawn.
3629 if (bt_terminal(wp->w_buffer))
3630 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003631 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003632#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003633 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003634 if (wp != prev_wp)
3635 {
3636 vim_memset(plines_cache, 0,
3637 sizeof(int) * Rows);
3638 prev_wp = wp;
3639 }
3640
3641 if (line_cp >= wp->w_height)
3642 // In (or below) status line
3643 wp->w_redr_status = TRUE;
3644 else
3645 {
3646 // compute the position in the buffer line
3647 // from the position in the window
3648 mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003649 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003650 redrawWinline(wp, lnum);
3651 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003652 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003653
3654 // This line is going to be redrawn, no need to
3655 // check until the right side of the window.
3656 col_done = wp->w_wincol + wp->w_width - 1;
3657 }
3658 }
3659 }
3660 }
3661 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003662
3663 vim_free(plines_cache);
3664 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003665}
3666
3667/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003668 * If the current window is a popup and something relevant changed, recompute
3669 * the position and size.
3670 */
3671 void
3672may_update_popup_position(void)
3673{
3674 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3675 popup_adjust_position(curwin);
3676}
3677
3678/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003679 * Return a string of "len" spaces in IObuff.
3680 */
3681 static char_u *
3682get_spaces(int len)
3683{
3684 vim_memset(IObuff, ' ', (size_t)len);
3685 IObuff[len] = NUL;
3686 return IObuff;
3687}
3688
3689/*
3690 * Update popup windows. They are drawn on top of normal windows.
3691 * "win_update" is called for each popup window, lowest zindex first.
3692 */
3693 void
3694update_popups(void (*win_update)(win_T *wp))
3695{
3696 win_T *wp;
3697 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003698 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003699 int total_width;
3700 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003701 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003702 int popup_attr;
3703 int border_attr[4];
3704 int border_char[8];
3705 char_u buf[MB_MAXBYTES];
3706 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003707 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003708 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003709 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003710 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003711 int sb_thumb_top = 0;
3712 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003713 int attr_scroll = 0;
3714 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003715
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003716 // hide the cursor until redrawing is done.
3717 cursor_off();
3718
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003719 // Find the window with the lowest zindex that hasn't been updated yet,
3720 // so that the window with a higher zindex is drawn later, thus goes on
3721 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003722 popup_reset_handled(POPUP_HANDLED_5);
3723 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003724 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003725 int title_len = 0;
3726 int title_wincol;
3727
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003728 // This drawing uses the zindex of the popup window, so that it's on
3729 // top of the text but doesn't draw when another popup with higher
3730 // zindex is on top of the character.
3731 screen_zindex = wp->w_zindex;
3732
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003733 // Set flags in popup_transparent[] for masked cells.
3734 update_popup_transparent(wp, 1);
3735
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003736 // adjust w_winrow and w_wincol for border and padding, since
3737 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003738 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003739 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3740 - wp->w_popup_leftoff;
3741 if (wp->w_wincol + left_extra < 0)
3742 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003743 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003744 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003745
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003746 // Draw the popup text, unless it's off screen.
3747 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003748 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003749 // May need to update the "cursorline" highlighting, which may also
3750 // change "topline"
3751 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3752 popup_highlight_curline(wp);
3753
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003754 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003755
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003756 // move the cursor into the visible lines, otherwise executing
3757 // commands with win_execute() may cause the text to jump.
3758 if (wp->w_cursor.lnum < wp->w_topline)
3759 wp->w_cursor.lnum = wp->w_topline;
3760 else if (wp->w_cursor.lnum >= wp->w_botline)
3761 wp->w_cursor.lnum = wp->w_botline - 1;
3762 }
3763
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003764 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003765 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003766
3767 // Add offset for border and padding if not done already.
3768 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3769 {
3770 wp->w_wcol += left_extra;
3771 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3772 }
3773 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003774 {
3775 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003776 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003777 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003778
Bram Moolenaard529ba52019-07-02 23:13:53 +02003779 total_width = popup_width(wp);
3780 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003781 popup_attr = get_wcr_attr(wp);
3782
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003783 if (wp->w_winrow + total_height > cmdline_row)
3784 wp->w_popup_flags |= POPF_ON_CMDLINE;
3785 else
3786 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3787
3788
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003789 // We can only use these line drawing characters when 'encoding' is
3790 // "utf-8" and 'ambiwidth' is "single".
3791 if (enc_utf8 && *p_ambw == 's')
3792 {
3793 border_char[0] = border_char[2] = 0x2550;
3794 border_char[1] = border_char[3] = 0x2551;
3795 border_char[4] = 0x2554;
3796 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003797 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3798 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003799 border_char[7] = 0x255a;
3800 }
3801 else
3802 {
3803 border_char[0] = border_char[2] = '-';
3804 border_char[1] = border_char[3] = '|';
3805 for (i = 4; i < 8; ++i)
3806 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003807 if (wp->w_popup_flags & POPF_RESIZE)
3808 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003809 }
3810 for (i = 0; i < 8; ++i)
3811 if (wp->w_border_char[i] != 0)
3812 border_char[i] = wp->w_border_char[i];
3813
3814 for (i = 0; i < 4; ++i)
3815 {
3816 border_attr[i] = popup_attr;
3817 if (wp->w_border_highlight[i] != NULL)
3818 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3819 }
3820
Bram Moolenaard91467f2020-11-21 12:42:09 +01003821 // Title goes on top of border or padding.
3822 title_wincol = wp->w_wincol + 1;
3823 if (wp->w_popup_title != NULL)
3824 {
Ralf Schandlbc869872021-05-28 14:12:14 +02003825 title_len = (int)MB_CHARLEN(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003826
Ralf Schandlbc869872021-05-28 14:12:14 +02003827 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003828 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02003829 {
3830 int title_byte_len = (int)STRLEN(wp->w_popup_title);
3831 char_u *title_text = alloc(title_byte_len + 1);
3832
3833 if (title_text != NULL)
3834 {
3835 trunc_string(wp->w_popup_title, title_text,
3836 total_width - 2, title_byte_len + 1);
3837 screen_puts(title_text, wp->w_winrow, title_wincol,
3838 wp->w_popup_border[0] > 0
3839 ? border_attr[0] : popup_attr);
3840 vim_free(title_text);
3841 }
3842
Bram Moolenaard91467f2020-11-21 12:42:09 +01003843 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02003844 }
3845 else
3846 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
3847 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003848 }
3849
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003850 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003851 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003852 if (wp->w_popup_border[0] > 0)
3853 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003854 // top border; do not draw over the title
3855 if (title_len > 0)
3856 {
3857 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3858 wincol < 0 ? 0 : wincol, title_wincol,
3859 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003860 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01003861 border_char[0], border_attr[0]);
3862 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3863 title_wincol + title_len, wincol + total_width,
3864 border_char[0], border_char[0], border_attr[0]);
3865 }
3866 else
3867 {
3868 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3869 wincol < 0 ? 0 : wincol, wincol + total_width,
3870 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
3871 ? border_char[4] : border_char[0],
3872 border_char[0], border_attr[0]);
3873 }
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003874 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003875 {
3876 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3877 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003878 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003879 }
3880 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003881 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3882 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003883
Bram Moolenaard529ba52019-07-02 23:13:53 +02003884 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3885 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003886 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01003887 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003888 - wp->w_has_scrollbar;
3889 if (padcol < 0)
3890 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003891 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003892 padcol = 0;
3893 }
3894 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003895 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003896 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003897 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003898 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01003899 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003900 // top padding and no border; do not draw over the title
3901 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003902 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003903 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01003904 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003905 row += 1;
3906 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003907 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003908 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01003909 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003910 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003911
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003912 // Compute scrollbar thumb position and size.
3913 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003914 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003915 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3916 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003917
Bram Moolenaar076d9882019-09-14 22:23:29 +02003918 sb_thumb_height = (height * height + linecount / 2) / linecount;
3919 if (wp->w_topline > 1 && sb_thumb_height == height)
3920 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003921 if (sb_thumb_height == 0)
3922 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003923 if (linecount <= wp->w_height)
3924 // it just fits, avoid divide by zero
3925 sb_thumb_top = 0;
3926 else
3927 sb_thumb_top = (wp->w_topline - 1
3928 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003929 * (wp->w_height - sb_thumb_height)
3930 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003931 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3932 sb_thumb_top = 1; // show it's scrolled
3933
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003934 if (wp->w_scrollbar_highlight != NULL)
3935 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3936 else
3937 attr_scroll = highlight_attr[HLF_PSB];
3938 if (wp->w_thumb_highlight != NULL)
3939 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3940 else
3941 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003942 }
3943
3944 for (i = wp->w_popup_border[0];
3945 i < total_height - wp->w_popup_border[2]; ++i)
3946 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003947 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003948 // left and right padding only needed next to the body
3949 int do_padding =
3950 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3951 && i < total_height - wp->w_popup_border[2]
3952 - wp->w_popup_padding[2];
3953
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003954 row = wp->w_winrow + i;
3955
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003956 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003957 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003958 {
3959 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003960 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003961 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003962 if (do_padding && wp->w_popup_padding[3] > 0)
3963 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003964 int col = wincol + wp->w_popup_border[3];
3965
Bram Moolenaard529ba52019-07-02 23:13:53 +02003966 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003967 pad_left = wp->w_popup_padding[3];
3968 if (col < 0)
3969 {
3970 pad_left += col;
3971 col = 0;
3972 }
3973 if (pad_left > 0)
3974 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3975 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003976 // scrollbar
3977 if (wp->w_has_scrollbar)
3978 {
3979 int line = i - top_off;
3980 int scroll_col = wp->w_wincol + total_width - 1
3981 - wp->w_popup_border[1];
3982
3983 if (line >= 0 && line < wp->w_height)
3984 screen_putchar(' ', row, scroll_col,
3985 line >= sb_thumb_top
3986 && line < sb_thumb_top + sb_thumb_height
3987 ? attr_thumb : attr_scroll);
3988 else
3989 screen_putchar(' ', row, scroll_col, popup_attr);
3990 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003991 // right border
3992 if (wp->w_popup_border[1] > 0)
3993 {
3994 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003995 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003996 }
3997 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003998 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003999 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004000 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004001 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4002 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004003 }
4004
4005 if (wp->w_popup_padding[2] > 0)
4006 {
4007 // bottom padding
4008 row = wp->w_winrow + wp->w_popup_border[0]
4009 + wp->w_popup_padding[0] + wp->w_height;
4010 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004011 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004012 }
4013
4014 if (wp->w_popup_border[2] > 0)
4015 {
4016 // bottom border
4017 row = wp->w_winrow + total_height - 1;
4018 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004019 wincol < 0 ? 0 : wincol,
4020 wincol + total_width,
4021 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004022 ? border_char[7] : border_char[2],
4023 border_char[2], border_attr[2]);
4024 if (wp->w_popup_border[1] > 0)
4025 {
4026 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004027 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004028 }
4029 }
4030
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004031 if (wp->w_popup_close == POPCLOSE_BUTTON)
4032 {
4033 // close button goes on top of anything at the top-right corner
4034 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004035 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004036 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4037 }
4038
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004039 update_popup_transparent(wp, 0);
4040
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004041 // Back to the normal zindex.
4042 screen_zindex = 0;
4043 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004044
4045#if defined(FEAT_SEARCH_EXTRA)
4046 // In case win_update() called start_search_hl().
4047 end_search_hl();
4048#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004049}
4050
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004051/*
4052 * Mark references in callbacks of one popup window.
4053 */
4054 static int
4055set_ref_in_one_popup(win_T *wp, int copyID)
4056{
4057 int abort = FALSE;
4058 typval_T tv;
4059
4060 if (wp->w_close_cb.cb_partial != NULL)
4061 {
4062 tv.v_type = VAR_PARTIAL;
4063 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4064 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4065 }
4066 if (wp->w_filter_cb.cb_partial != NULL)
4067 {
4068 tv.v_type = VAR_PARTIAL;
4069 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4070 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4071 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004072 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004073 return abort;
4074}
4075
4076/*
4077 * Set reference in callbacks of popup windows.
4078 */
4079 int
4080set_ref_in_popups(int copyID)
4081{
4082 int abort = FALSE;
4083 win_T *wp;
4084 tabpage_T *tp;
4085
4086 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4087 abort = abort || set_ref_in_one_popup(wp, copyID);
4088
4089 FOR_ALL_TABPAGES(tp)
4090 {
4091 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4092 abort = abort || set_ref_in_one_popup(wp, copyID);
4093 if (abort)
4094 break;
4095 }
4096 return abort;
4097}
Bram Moolenaar79648732019-07-18 21:43:07 +02004098
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004099 int
4100popup_is_popup(win_T *wp)
4101{
4102 return wp->w_popup_flags != 0;
4103}
4104
4105#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004106/*
4107 * Find an existing popup used as the preview window, in the current tab page.
4108 * Return NULL if not found.
4109 */
4110 win_T *
4111popup_find_preview_window(void)
4112{
4113 win_T *wp;
4114
4115 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004116 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004117 if (wp->w_p_pvw)
4118 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004119 return NULL;
4120}
4121
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004122/*
4123 * Find an existing popup used as the info window, in the current tab page.
4124 * Return NULL if not found.
4125 */
4126 win_T *
4127popup_find_info_window(void)
4128{
4129 win_T *wp;
4130
4131 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004132 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004133 if (wp->w_popup_flags & POPF_INFO)
4134 return wp;
4135 return NULL;
4136}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004137#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004138
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004139 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004140f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4141{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004142#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004143 win_T *wp = popup_find_info_window();
4144
4145 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004146#else
4147 rettv->vval.v_number = 0;
4148#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004149}
4150
4151 void
4152f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004153{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004154#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004155 win_T *wp = popup_find_preview_window();
4156
4157 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004158#else
4159 rettv->vval.v_number = 0;
4160#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004161}
4162
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004163#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004164/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004165 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004166 * NOTE: this makes the popup the current window, so that the file can be
4167 * edited. However, it must not remain to be the current window, the caller
4168 * must make sure of that.
4169 */
4170 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004171popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004172{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004173 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004174
4175 if (wp == NULL)
4176 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004177 if (info)
4178 wp->w_popup_flags |= POPF_INFO;
4179 else
4180 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004181
4182 // Set the width to a reasonable value, so that w_topline can be computed.
4183 if (wp->w_minwidth > 0)
4184 wp->w_width = wp->w_minwidth;
4185 else if (wp->w_maxwidth > 0)
4186 wp->w_width = wp->w_maxwidth;
4187 else
4188 wp->w_width = curwin->w_width;
4189
4190 // Will switch to another buffer soon, dummy one can be wiped.
4191 wp->w_buffer->b_locked = FALSE;
4192
4193 win_enter(wp, FALSE);
4194 return OK;
4195}
4196
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004197/*
4198 * Close any preview popup.
4199 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004200 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004201popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004202{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004203 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004204
4205 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004206 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004207}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004208
4209/*
4210 * Hide the info popup.
4211 */
4212 void
4213popup_hide_info(void)
4214{
4215 win_T *wp = popup_find_info_window();
4216
4217 if (wp != NULL)
4218 popup_hide(wp);
4219}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004220
4221/*
4222 * Close any info popup.
4223 */
4224 void
4225popup_close_info(void)
4226{
4227 win_T *wp = popup_find_info_window();
4228
4229 if (wp != NULL)
4230 popup_close_with_retval(wp, -1);
4231}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004232#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004233
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004234/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004235 * Close any popup for a text property associated with "win".
4236 * Return TRUE if a popup was closed.
4237 */
4238 int
4239popup_win_closed(win_T *win)
4240{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004241 int round;
4242 win_T *wp;
4243 win_T *next;
4244 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004245
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004246 for (round = 1; round <= 2; ++round)
4247 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4248 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004249 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004250 next = wp->w_next;
4251 if (wp->w_popup_prop_win == win)
4252 {
4253 popup_close_with_retval(wp, -1);
4254 ret = TRUE;
4255 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004256 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004257 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004258}
4259
4260/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004261 * Set the title of the popup window to the file name.
4262 */
4263 void
4264popup_set_title(win_T *wp)
4265{
4266 if (wp->w_buffer->b_fname != NULL)
4267 {
4268 char_u dirname[MAXPATHL];
4269 size_t len;
4270
4271 mch_dirname(dirname, MAXPATHL);
4272 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4273
4274 vim_free(wp->w_popup_title);
4275 len = STRLEN(wp->w_buffer->b_fname) + 3;
4276 wp->w_popup_title = alloc((int)len);
4277 if (wp->w_popup_title != NULL)
4278 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4279 wp->w_buffer->b_fname);
4280 redraw_win_later(wp, VALID);
4281 }
4282}
4283
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004284# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004285/*
4286 * If there is a preview window, update the title.
4287 * Used after changing directory.
4288 */
4289 void
4290popup_update_preview_title(void)
4291{
4292 win_T *wp = popup_find_preview_window();
4293
4294 if (wp != NULL)
4295 popup_set_title(wp);
4296}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004297# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004298
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004299#endif // FEAT_PROP_POPUP